gpio-mvebu.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. /*
  2. * GPIO driver for Marvell SoCs
  3. *
  4. * Copyright (C) 2012 Marvell
  5. *
  6. * Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
  7. * Andrew Lunn <andrew@lunn.ch>
  8. * Sebastian Hesselbarth <sebastian.hesselbarth@gmail.com>
  9. *
  10. * This file is licensed under the terms of the GNU General Public
  11. * License version 2. This program is licensed "as is" without any
  12. * warranty of any kind, whether express or implied.
  13. *
  14. * This driver is a fairly straightforward GPIO driver for the
  15. * complete family of Marvell EBU SoC platforms (Orion, Dove,
  16. * Kirkwood, Discovery, Armada 370/XP). The only complexity of this
  17. * driver is the different register layout that exists between the
  18. * non-SMP platforms (Orion, Dove, Kirkwood, Armada 370) and the SMP
  19. * platforms (MV78200 from the Discovery family and the Armada
  20. * XP). Therefore, this driver handles three variants of the GPIO
  21. * block:
  22. * - the basic variant, called "orion-gpio", with the simplest
  23. * register set. Used on Orion, Dove, Kirkwoord, Armada 370 and
  24. * non-SMP Discovery systems
  25. * - the mv78200 variant for MV78200 Discovery systems. This variant
  26. * turns the edge mask and level mask registers into CPU0 edge
  27. * mask/level mask registers, and adds CPU1 edge mask/level mask
  28. * registers.
  29. * - the armadaxp variant for Armada XP systems. This variant keeps
  30. * the normal cause/edge mask/level mask registers when the global
  31. * interrupts are used, but adds per-CPU cause/edge mask/level mask
  32. * registers n a separate memory area for the per-CPU GPIO
  33. * interrupts.
  34. */
  35. #include <linux/err.h>
  36. #include <linux/module.h>
  37. #include <linux/gpio.h>
  38. #include <linux/irq.h>
  39. #include <linux/slab.h>
  40. #include <linux/irqdomain.h>
  41. #include <linux/io.h>
  42. #include <linux/of_irq.h>
  43. #include <linux/of_device.h>
  44. #include <linux/clk.h>
  45. #include <linux/pinctrl/consumer.h>
  46. #include <linux/irqchip/chained_irq.h>
  47. /*
  48. * GPIO unit register offsets.
  49. */
  50. #define GPIO_OUT_OFF 0x0000
  51. #define GPIO_IO_CONF_OFF 0x0004
  52. #define GPIO_BLINK_EN_OFF 0x0008
  53. #define GPIO_IN_POL_OFF 0x000c
  54. #define GPIO_DATA_IN_OFF 0x0010
  55. #define GPIO_EDGE_CAUSE_OFF 0x0014
  56. #define GPIO_EDGE_MASK_OFF 0x0018
  57. #define GPIO_LEVEL_MASK_OFF 0x001c
  58. /* The MV78200 has per-CPU registers for edge mask and level mask */
  59. #define GPIO_EDGE_MASK_MV78200_OFF(cpu) ((cpu) ? 0x30 : 0x18)
  60. #define GPIO_LEVEL_MASK_MV78200_OFF(cpu) ((cpu) ? 0x34 : 0x1C)
  61. /* The Armada XP has per-CPU registers for interrupt cause, interrupt
  62. * mask and interrupt level mask. Those are relative to the
  63. * percpu_membase. */
  64. #define GPIO_EDGE_CAUSE_ARMADAXP_OFF(cpu) ((cpu) * 0x4)
  65. #define GPIO_EDGE_MASK_ARMADAXP_OFF(cpu) (0x10 + (cpu) * 0x4)
  66. #define GPIO_LEVEL_MASK_ARMADAXP_OFF(cpu) (0x20 + (cpu) * 0x4)
  67. #define MVEBU_GPIO_SOC_VARIANT_ORION 0x1
  68. #define MVEBU_GPIO_SOC_VARIANT_MV78200 0x2
  69. #define MVEBU_GPIO_SOC_VARIANT_ARMADAXP 0x3
  70. #define MVEBU_MAX_GPIO_PER_BANK 32
  71. struct mvebu_gpio_chip {
  72. struct gpio_chip chip;
  73. spinlock_t lock;
  74. void __iomem *membase;
  75. void __iomem *percpu_membase;
  76. int irqbase;
  77. struct irq_domain *domain;
  78. int soc_variant;
  79. };
  80. /*
  81. * Functions returning addresses of individual registers for a given
  82. * GPIO controller.
  83. */
  84. static inline void __iomem *mvebu_gpioreg_out(struct mvebu_gpio_chip *mvchip)
  85. {
  86. return mvchip->membase + GPIO_OUT_OFF;
  87. }
  88. static inline void __iomem *mvebu_gpioreg_blink(struct mvebu_gpio_chip *mvchip)
  89. {
  90. return mvchip->membase + GPIO_BLINK_EN_OFF;
  91. }
  92. static inline void __iomem *mvebu_gpioreg_io_conf(struct mvebu_gpio_chip *mvchip)
  93. {
  94. return mvchip->membase + GPIO_IO_CONF_OFF;
  95. }
  96. static inline void __iomem *mvebu_gpioreg_in_pol(struct mvebu_gpio_chip *mvchip)
  97. {
  98. return mvchip->membase + GPIO_IN_POL_OFF;
  99. }
  100. static inline void __iomem *mvebu_gpioreg_data_in(struct mvebu_gpio_chip *mvchip)
  101. {
  102. return mvchip->membase + GPIO_DATA_IN_OFF;
  103. }
  104. static inline void __iomem *mvebu_gpioreg_edge_cause(struct mvebu_gpio_chip *mvchip)
  105. {
  106. int cpu;
  107. switch (mvchip->soc_variant) {
  108. case MVEBU_GPIO_SOC_VARIANT_ORION:
  109. case MVEBU_GPIO_SOC_VARIANT_MV78200:
  110. return mvchip->membase + GPIO_EDGE_CAUSE_OFF;
  111. case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
  112. cpu = smp_processor_id();
  113. return mvchip->percpu_membase + GPIO_EDGE_CAUSE_ARMADAXP_OFF(cpu);
  114. default:
  115. BUG();
  116. }
  117. }
  118. static inline void __iomem *mvebu_gpioreg_edge_mask(struct mvebu_gpio_chip *mvchip)
  119. {
  120. int cpu;
  121. switch (mvchip->soc_variant) {
  122. case MVEBU_GPIO_SOC_VARIANT_ORION:
  123. return mvchip->membase + GPIO_EDGE_MASK_OFF;
  124. case MVEBU_GPIO_SOC_VARIANT_MV78200:
  125. cpu = smp_processor_id();
  126. return mvchip->membase + GPIO_EDGE_MASK_MV78200_OFF(cpu);
  127. case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
  128. cpu = smp_processor_id();
  129. return mvchip->percpu_membase + GPIO_EDGE_MASK_ARMADAXP_OFF(cpu);
  130. default:
  131. BUG();
  132. }
  133. }
  134. static void __iomem *mvebu_gpioreg_level_mask(struct mvebu_gpio_chip *mvchip)
  135. {
  136. int cpu;
  137. switch (mvchip->soc_variant) {
  138. case MVEBU_GPIO_SOC_VARIANT_ORION:
  139. return mvchip->membase + GPIO_LEVEL_MASK_OFF;
  140. case MVEBU_GPIO_SOC_VARIANT_MV78200:
  141. cpu = smp_processor_id();
  142. return mvchip->membase + GPIO_LEVEL_MASK_MV78200_OFF(cpu);
  143. case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
  144. cpu = smp_processor_id();
  145. return mvchip->percpu_membase + GPIO_LEVEL_MASK_ARMADAXP_OFF(cpu);
  146. default:
  147. BUG();
  148. }
  149. }
  150. /*
  151. * Functions implementing the gpio_chip methods
  152. */
  153. static int mvebu_gpio_request(struct gpio_chip *chip, unsigned pin)
  154. {
  155. return pinctrl_request_gpio(chip->base + pin);
  156. }
  157. static void mvebu_gpio_free(struct gpio_chip *chip, unsigned pin)
  158. {
  159. pinctrl_free_gpio(chip->base + pin);
  160. }
  161. static void mvebu_gpio_set(struct gpio_chip *chip, unsigned pin, int value)
  162. {
  163. struct mvebu_gpio_chip *mvchip =
  164. container_of(chip, struct mvebu_gpio_chip, chip);
  165. unsigned long flags;
  166. u32 u;
  167. spin_lock_irqsave(&mvchip->lock, flags);
  168. u = readl_relaxed(mvebu_gpioreg_out(mvchip));
  169. if (value)
  170. u |= 1 << pin;
  171. else
  172. u &= ~(1 << pin);
  173. writel_relaxed(u, mvebu_gpioreg_out(mvchip));
  174. spin_unlock_irqrestore(&mvchip->lock, flags);
  175. }
  176. static int mvebu_gpio_get(struct gpio_chip *chip, unsigned pin)
  177. {
  178. struct mvebu_gpio_chip *mvchip =
  179. container_of(chip, struct mvebu_gpio_chip, chip);
  180. u32 u;
  181. if (readl_relaxed(mvebu_gpioreg_io_conf(mvchip)) & (1 << pin)) {
  182. u = readl_relaxed(mvebu_gpioreg_data_in(mvchip)) ^
  183. readl_relaxed(mvebu_gpioreg_in_pol(mvchip));
  184. } else {
  185. u = readl_relaxed(mvebu_gpioreg_out(mvchip));
  186. }
  187. return (u >> pin) & 1;
  188. }
  189. static void mvebu_gpio_blink(struct gpio_chip *chip, unsigned pin, int value)
  190. {
  191. struct mvebu_gpio_chip *mvchip =
  192. container_of(chip, struct mvebu_gpio_chip, chip);
  193. unsigned long flags;
  194. u32 u;
  195. spin_lock_irqsave(&mvchip->lock, flags);
  196. u = readl_relaxed(mvebu_gpioreg_blink(mvchip));
  197. if (value)
  198. u |= 1 << pin;
  199. else
  200. u &= ~(1 << pin);
  201. writel_relaxed(u, mvebu_gpioreg_blink(mvchip));
  202. spin_unlock_irqrestore(&mvchip->lock, flags);
  203. }
  204. static int mvebu_gpio_direction_input(struct gpio_chip *chip, unsigned pin)
  205. {
  206. struct mvebu_gpio_chip *mvchip =
  207. container_of(chip, struct mvebu_gpio_chip, chip);
  208. unsigned long flags;
  209. int ret;
  210. u32 u;
  211. /* Check with the pinctrl driver whether this pin is usable as
  212. * an input GPIO */
  213. ret = pinctrl_gpio_direction_input(chip->base + pin);
  214. if (ret)
  215. return ret;
  216. spin_lock_irqsave(&mvchip->lock, flags);
  217. u = readl_relaxed(mvebu_gpioreg_io_conf(mvchip));
  218. u |= 1 << pin;
  219. writel_relaxed(u, mvebu_gpioreg_io_conf(mvchip));
  220. spin_unlock_irqrestore(&mvchip->lock, flags);
  221. return 0;
  222. }
  223. static int mvebu_gpio_direction_output(struct gpio_chip *chip, unsigned pin,
  224. int value)
  225. {
  226. struct mvebu_gpio_chip *mvchip =
  227. container_of(chip, struct mvebu_gpio_chip, chip);
  228. unsigned long flags;
  229. int ret;
  230. u32 u;
  231. /* Check with the pinctrl driver whether this pin is usable as
  232. * an output GPIO */
  233. ret = pinctrl_gpio_direction_output(chip->base + pin);
  234. if (ret)
  235. return ret;
  236. mvebu_gpio_blink(chip, pin, 0);
  237. mvebu_gpio_set(chip, pin, value);
  238. spin_lock_irqsave(&mvchip->lock, flags);
  239. u = readl_relaxed(mvebu_gpioreg_io_conf(mvchip));
  240. u &= ~(1 << pin);
  241. writel_relaxed(u, mvebu_gpioreg_io_conf(mvchip));
  242. spin_unlock_irqrestore(&mvchip->lock, flags);
  243. return 0;
  244. }
  245. static int mvebu_gpio_to_irq(struct gpio_chip *chip, unsigned pin)
  246. {
  247. struct mvebu_gpio_chip *mvchip =
  248. container_of(chip, struct mvebu_gpio_chip, chip);
  249. return irq_create_mapping(mvchip->domain, pin);
  250. }
  251. /*
  252. * Functions implementing the irq_chip methods
  253. */
  254. static void mvebu_gpio_irq_ack(struct irq_data *d)
  255. {
  256. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  257. struct mvebu_gpio_chip *mvchip = gc->private;
  258. u32 mask = ~(1 << (d->irq - gc->irq_base));
  259. irq_gc_lock(gc);
  260. writel_relaxed(mask, mvebu_gpioreg_edge_cause(mvchip));
  261. irq_gc_unlock(gc);
  262. }
  263. static void mvebu_gpio_edge_irq_mask(struct irq_data *d)
  264. {
  265. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  266. struct mvebu_gpio_chip *mvchip = gc->private;
  267. struct irq_chip_type *ct = irq_data_get_chip_type(d);
  268. u32 mask = 1 << (d->irq - gc->irq_base);
  269. irq_gc_lock(gc);
  270. ct->mask_cache_priv &= ~mask;
  271. writel_relaxed(ct->mask_cache_priv, mvebu_gpioreg_edge_mask(mvchip));
  272. irq_gc_unlock(gc);
  273. }
  274. static void mvebu_gpio_edge_irq_unmask(struct irq_data *d)
  275. {
  276. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  277. struct mvebu_gpio_chip *mvchip = gc->private;
  278. struct irq_chip_type *ct = irq_data_get_chip_type(d);
  279. u32 mask = 1 << (d->irq - gc->irq_base);
  280. irq_gc_lock(gc);
  281. ct->mask_cache_priv |= mask;
  282. writel_relaxed(ct->mask_cache_priv, mvebu_gpioreg_edge_mask(mvchip));
  283. irq_gc_unlock(gc);
  284. }
  285. static void mvebu_gpio_level_irq_mask(struct irq_data *d)
  286. {
  287. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  288. struct mvebu_gpio_chip *mvchip = gc->private;
  289. struct irq_chip_type *ct = irq_data_get_chip_type(d);
  290. u32 mask = 1 << (d->irq - gc->irq_base);
  291. irq_gc_lock(gc);
  292. ct->mask_cache_priv &= ~mask;
  293. writel_relaxed(ct->mask_cache_priv, mvebu_gpioreg_level_mask(mvchip));
  294. irq_gc_unlock(gc);
  295. }
  296. static void mvebu_gpio_level_irq_unmask(struct irq_data *d)
  297. {
  298. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  299. struct mvebu_gpio_chip *mvchip = gc->private;
  300. struct irq_chip_type *ct = irq_data_get_chip_type(d);
  301. u32 mask = 1 << (d->irq - gc->irq_base);
  302. irq_gc_lock(gc);
  303. ct->mask_cache_priv |= mask;
  304. writel_relaxed(ct->mask_cache_priv, mvebu_gpioreg_level_mask(mvchip));
  305. irq_gc_unlock(gc);
  306. }
  307. /*****************************************************************************
  308. * MVEBU GPIO IRQ
  309. *
  310. * GPIO_IN_POL register controls whether GPIO_DATA_IN will hold the same
  311. * value of the line or the opposite value.
  312. *
  313. * Level IRQ handlers: DATA_IN is used directly as cause register.
  314. * Interrupt are masked by LEVEL_MASK registers.
  315. * Edge IRQ handlers: Change in DATA_IN are latched in EDGE_CAUSE.
  316. * Interrupt are masked by EDGE_MASK registers.
  317. * Both-edge handlers: Similar to regular Edge handlers, but also swaps
  318. * the polarity to catch the next line transaction.
  319. * This is a race condition that might not perfectly
  320. * work on some use cases.
  321. *
  322. * Every eight GPIO lines are grouped (OR'ed) before going up to main
  323. * cause register.
  324. *
  325. * EDGE cause mask
  326. * data-in /--------| |-----| |----\
  327. * -----| |----- ---- to main cause reg
  328. * X \----------------| |----/
  329. * polarity LEVEL mask
  330. *
  331. ****************************************************************************/
  332. static int mvebu_gpio_irq_set_type(struct irq_data *d, unsigned int type)
  333. {
  334. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  335. struct irq_chip_type *ct = irq_data_get_chip_type(d);
  336. struct mvebu_gpio_chip *mvchip = gc->private;
  337. int pin;
  338. u32 u;
  339. pin = d->hwirq;
  340. u = readl_relaxed(mvebu_gpioreg_io_conf(mvchip)) & (1 << pin);
  341. if (!u) {
  342. return -EINVAL;
  343. }
  344. type &= IRQ_TYPE_SENSE_MASK;
  345. if (type == IRQ_TYPE_NONE)
  346. return -EINVAL;
  347. /* Check if we need to change chip and handler */
  348. if (!(ct->type & type))
  349. if (irq_setup_alt_chip(d, type))
  350. return -EINVAL;
  351. /*
  352. * Configure interrupt polarity.
  353. */
  354. switch (type) {
  355. case IRQ_TYPE_EDGE_RISING:
  356. case IRQ_TYPE_LEVEL_HIGH:
  357. u = readl_relaxed(mvebu_gpioreg_in_pol(mvchip));
  358. u &= ~(1 << pin);
  359. writel_relaxed(u, mvebu_gpioreg_in_pol(mvchip));
  360. break;
  361. case IRQ_TYPE_EDGE_FALLING:
  362. case IRQ_TYPE_LEVEL_LOW:
  363. u = readl_relaxed(mvebu_gpioreg_in_pol(mvchip));
  364. u |= 1 << pin;
  365. writel_relaxed(u, mvebu_gpioreg_in_pol(mvchip));
  366. break;
  367. case IRQ_TYPE_EDGE_BOTH: {
  368. u32 v;
  369. v = readl_relaxed(mvebu_gpioreg_in_pol(mvchip)) ^
  370. readl_relaxed(mvebu_gpioreg_data_in(mvchip));
  371. /*
  372. * set initial polarity based on current input level
  373. */
  374. u = readl_relaxed(mvebu_gpioreg_in_pol(mvchip));
  375. if (v & (1 << pin))
  376. u |= 1 << pin; /* falling */
  377. else
  378. u &= ~(1 << pin); /* rising */
  379. writel_relaxed(u, mvebu_gpioreg_in_pol(mvchip));
  380. break;
  381. }
  382. }
  383. return 0;
  384. }
  385. static void mvebu_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
  386. {
  387. struct mvebu_gpio_chip *mvchip = irq_get_handler_data(irq);
  388. struct irq_chip *chip = irq_desc_get_chip(desc);
  389. u32 cause, type;
  390. int i;
  391. if (mvchip == NULL)
  392. return;
  393. chained_irq_enter(chip, desc);
  394. cause = readl_relaxed(mvebu_gpioreg_data_in(mvchip)) &
  395. readl_relaxed(mvebu_gpioreg_level_mask(mvchip));
  396. cause |= readl_relaxed(mvebu_gpioreg_edge_cause(mvchip)) &
  397. readl_relaxed(mvebu_gpioreg_edge_mask(mvchip));
  398. for (i = 0; i < mvchip->chip.ngpio; i++) {
  399. int irq;
  400. irq = mvchip->irqbase + i;
  401. if (!(cause & (1 << i)))
  402. continue;
  403. type = irq_get_trigger_type(irq);
  404. if ((type & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH) {
  405. /* Swap polarity (race with GPIO line) */
  406. u32 polarity;
  407. polarity = readl_relaxed(mvebu_gpioreg_in_pol(mvchip));
  408. polarity ^= 1 << i;
  409. writel_relaxed(polarity, mvebu_gpioreg_in_pol(mvchip));
  410. }
  411. generic_handle_irq(irq);
  412. }
  413. chained_irq_exit(chip, desc);
  414. }
  415. #ifdef CONFIG_DEBUG_FS
  416. #include <linux/seq_file.h>
  417. static void mvebu_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  418. {
  419. struct mvebu_gpio_chip *mvchip =
  420. container_of(chip, struct mvebu_gpio_chip, chip);
  421. u32 out, io_conf, blink, in_pol, data_in, cause, edg_msk, lvl_msk;
  422. int i;
  423. out = readl_relaxed(mvebu_gpioreg_out(mvchip));
  424. io_conf = readl_relaxed(mvebu_gpioreg_io_conf(mvchip));
  425. blink = readl_relaxed(mvebu_gpioreg_blink(mvchip));
  426. in_pol = readl_relaxed(mvebu_gpioreg_in_pol(mvchip));
  427. data_in = readl_relaxed(mvebu_gpioreg_data_in(mvchip));
  428. cause = readl_relaxed(mvebu_gpioreg_edge_cause(mvchip));
  429. edg_msk = readl_relaxed(mvebu_gpioreg_edge_mask(mvchip));
  430. lvl_msk = readl_relaxed(mvebu_gpioreg_level_mask(mvchip));
  431. for (i = 0; i < chip->ngpio; i++) {
  432. const char *label;
  433. u32 msk;
  434. bool is_out;
  435. label = gpiochip_is_requested(chip, i);
  436. if (!label)
  437. continue;
  438. msk = 1 << i;
  439. is_out = !(io_conf & msk);
  440. seq_printf(s, " gpio-%-3d (%-20.20s)", chip->base + i, label);
  441. if (is_out) {
  442. seq_printf(s, " out %s %s\n",
  443. out & msk ? "hi" : "lo",
  444. blink & msk ? "(blink )" : "");
  445. continue;
  446. }
  447. seq_printf(s, " in %s (act %s) - IRQ",
  448. (data_in ^ in_pol) & msk ? "hi" : "lo",
  449. in_pol & msk ? "lo" : "hi");
  450. if (!((edg_msk | lvl_msk) & msk)) {
  451. seq_printf(s, " disabled\n");
  452. continue;
  453. }
  454. if (edg_msk & msk)
  455. seq_printf(s, " edge ");
  456. if (lvl_msk & msk)
  457. seq_printf(s, " level");
  458. seq_printf(s, " (%s)\n", cause & msk ? "pending" : "clear ");
  459. }
  460. }
  461. #else
  462. #define mvebu_gpio_dbg_show NULL
  463. #endif
  464. static const struct of_device_id mvebu_gpio_of_match[] = {
  465. {
  466. .compatible = "marvell,orion-gpio",
  467. .data = (void *) MVEBU_GPIO_SOC_VARIANT_ORION,
  468. },
  469. {
  470. .compatible = "marvell,mv78200-gpio",
  471. .data = (void *) MVEBU_GPIO_SOC_VARIANT_MV78200,
  472. },
  473. {
  474. .compatible = "marvell,armadaxp-gpio",
  475. .data = (void *) MVEBU_GPIO_SOC_VARIANT_ARMADAXP,
  476. },
  477. {
  478. /* sentinel */
  479. },
  480. };
  481. MODULE_DEVICE_TABLE(of, mvebu_gpio_of_match);
  482. static int mvebu_gpio_probe(struct platform_device *pdev)
  483. {
  484. struct mvebu_gpio_chip *mvchip;
  485. const struct of_device_id *match;
  486. struct device_node *np = pdev->dev.of_node;
  487. struct resource *res;
  488. struct irq_chip_generic *gc;
  489. struct irq_chip_type *ct;
  490. struct clk *clk;
  491. unsigned int ngpios;
  492. int soc_variant;
  493. int i, cpu, id;
  494. match = of_match_device(mvebu_gpio_of_match, &pdev->dev);
  495. if (match)
  496. soc_variant = (int) match->data;
  497. else
  498. soc_variant = MVEBU_GPIO_SOC_VARIANT_ORION;
  499. mvchip = devm_kzalloc(&pdev->dev, sizeof(struct mvebu_gpio_chip), GFP_KERNEL);
  500. if (!mvchip)
  501. return -ENOMEM;
  502. if (of_property_read_u32(pdev->dev.of_node, "ngpios", &ngpios)) {
  503. dev_err(&pdev->dev, "Missing ngpios OF property\n");
  504. return -ENODEV;
  505. }
  506. id = of_alias_get_id(pdev->dev.of_node, "gpio");
  507. if (id < 0) {
  508. dev_err(&pdev->dev, "Couldn't get OF id\n");
  509. return id;
  510. }
  511. clk = devm_clk_get(&pdev->dev, NULL);
  512. /* Not all SoCs require a clock.*/
  513. if (!IS_ERR(clk))
  514. clk_prepare_enable(clk);
  515. mvchip->soc_variant = soc_variant;
  516. mvchip->chip.label = dev_name(&pdev->dev);
  517. mvchip->chip.dev = &pdev->dev;
  518. mvchip->chip.request = mvebu_gpio_request;
  519. mvchip->chip.free = mvebu_gpio_free;
  520. mvchip->chip.direction_input = mvebu_gpio_direction_input;
  521. mvchip->chip.get = mvebu_gpio_get;
  522. mvchip->chip.direction_output = mvebu_gpio_direction_output;
  523. mvchip->chip.set = mvebu_gpio_set;
  524. mvchip->chip.to_irq = mvebu_gpio_to_irq;
  525. mvchip->chip.base = id * MVEBU_MAX_GPIO_PER_BANK;
  526. mvchip->chip.ngpio = ngpios;
  527. mvchip->chip.can_sleep = false;
  528. mvchip->chip.of_node = np;
  529. mvchip->chip.dbg_show = mvebu_gpio_dbg_show;
  530. spin_lock_init(&mvchip->lock);
  531. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  532. mvchip->membase = devm_ioremap_resource(&pdev->dev, res);
  533. if (IS_ERR(mvchip->membase))
  534. return PTR_ERR(mvchip->membase);
  535. /* The Armada XP has a second range of registers for the
  536. * per-CPU registers */
  537. if (soc_variant == MVEBU_GPIO_SOC_VARIANT_ARMADAXP) {
  538. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  539. mvchip->percpu_membase = devm_ioremap_resource(&pdev->dev,
  540. res);
  541. if (IS_ERR(mvchip->percpu_membase))
  542. return PTR_ERR(mvchip->percpu_membase);
  543. }
  544. /*
  545. * Mask and clear GPIO interrupts.
  546. */
  547. switch (soc_variant) {
  548. case MVEBU_GPIO_SOC_VARIANT_ORION:
  549. writel_relaxed(0, mvchip->membase + GPIO_EDGE_CAUSE_OFF);
  550. writel_relaxed(0, mvchip->membase + GPIO_EDGE_MASK_OFF);
  551. writel_relaxed(0, mvchip->membase + GPIO_LEVEL_MASK_OFF);
  552. break;
  553. case MVEBU_GPIO_SOC_VARIANT_MV78200:
  554. writel_relaxed(0, mvchip->membase + GPIO_EDGE_CAUSE_OFF);
  555. for (cpu = 0; cpu < 2; cpu++) {
  556. writel_relaxed(0, mvchip->membase +
  557. GPIO_EDGE_MASK_MV78200_OFF(cpu));
  558. writel_relaxed(0, mvchip->membase +
  559. GPIO_LEVEL_MASK_MV78200_OFF(cpu));
  560. }
  561. break;
  562. case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
  563. writel_relaxed(0, mvchip->membase + GPIO_EDGE_CAUSE_OFF);
  564. writel_relaxed(0, mvchip->membase + GPIO_EDGE_MASK_OFF);
  565. writel_relaxed(0, mvchip->membase + GPIO_LEVEL_MASK_OFF);
  566. for (cpu = 0; cpu < 4; cpu++) {
  567. writel_relaxed(0, mvchip->percpu_membase +
  568. GPIO_EDGE_CAUSE_ARMADAXP_OFF(cpu));
  569. writel_relaxed(0, mvchip->percpu_membase +
  570. GPIO_EDGE_MASK_ARMADAXP_OFF(cpu));
  571. writel_relaxed(0, mvchip->percpu_membase +
  572. GPIO_LEVEL_MASK_ARMADAXP_OFF(cpu));
  573. }
  574. break;
  575. default:
  576. BUG();
  577. }
  578. gpiochip_add(&mvchip->chip);
  579. /* Some gpio controllers do not provide irq support */
  580. if (!of_irq_count(np))
  581. return 0;
  582. /* Setup the interrupt handlers. Each chip can have up to 4
  583. * interrupt handlers, with each handler dealing with 8 GPIO
  584. * pins. */
  585. for (i = 0; i < 4; i++) {
  586. int irq;
  587. irq = platform_get_irq(pdev, i);
  588. if (irq < 0)
  589. continue;
  590. irq_set_handler_data(irq, mvchip);
  591. irq_set_chained_handler(irq, mvebu_gpio_irq_handler);
  592. }
  593. mvchip->irqbase = irq_alloc_descs(-1, 0, ngpios, -1);
  594. if (mvchip->irqbase < 0) {
  595. dev_err(&pdev->dev, "no irqs\n");
  596. return mvchip->irqbase;
  597. }
  598. gc = irq_alloc_generic_chip("mvebu_gpio_irq", 2, mvchip->irqbase,
  599. mvchip->membase, handle_level_irq);
  600. if (!gc) {
  601. dev_err(&pdev->dev, "Cannot allocate generic irq_chip\n");
  602. return -ENOMEM;
  603. }
  604. gc->private = mvchip;
  605. ct = &gc->chip_types[0];
  606. ct->type = IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW;
  607. ct->chip.irq_mask = mvebu_gpio_level_irq_mask;
  608. ct->chip.irq_unmask = mvebu_gpio_level_irq_unmask;
  609. ct->chip.irq_set_type = mvebu_gpio_irq_set_type;
  610. ct->chip.name = mvchip->chip.label;
  611. ct = &gc->chip_types[1];
  612. ct->type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
  613. ct->chip.irq_ack = mvebu_gpio_irq_ack;
  614. ct->chip.irq_mask = mvebu_gpio_edge_irq_mask;
  615. ct->chip.irq_unmask = mvebu_gpio_edge_irq_unmask;
  616. ct->chip.irq_set_type = mvebu_gpio_irq_set_type;
  617. ct->handler = handle_edge_irq;
  618. ct->chip.name = mvchip->chip.label;
  619. irq_setup_generic_chip(gc, IRQ_MSK(ngpios), 0,
  620. IRQ_NOREQUEST, IRQ_LEVEL | IRQ_NOPROBE);
  621. /* Setup irq domain on top of the generic chip. */
  622. mvchip->domain = irq_domain_add_simple(np, mvchip->chip.ngpio,
  623. mvchip->irqbase,
  624. &irq_domain_simple_ops,
  625. mvchip);
  626. if (!mvchip->domain) {
  627. dev_err(&pdev->dev, "couldn't allocate irq domain %s (DT).\n",
  628. mvchip->chip.label);
  629. irq_remove_generic_chip(gc, IRQ_MSK(ngpios), IRQ_NOREQUEST,
  630. IRQ_LEVEL | IRQ_NOPROBE);
  631. kfree(gc);
  632. return -ENODEV;
  633. }
  634. return 0;
  635. }
  636. static struct platform_driver mvebu_gpio_driver = {
  637. .driver = {
  638. .name = "mvebu-gpio",
  639. .owner = THIS_MODULE,
  640. .of_match_table = mvebu_gpio_of_match,
  641. },
  642. .probe = mvebu_gpio_probe,
  643. };
  644. module_platform_driver(mvebu_gpio_driver);