gpio-mvebu.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  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. u32 mask = 1 << (d->irq - gc->irq_base);
  268. irq_gc_lock(gc);
  269. gc->mask_cache &= ~mask;
  270. writel_relaxed(gc->mask_cache, mvebu_gpioreg_edge_mask(mvchip));
  271. irq_gc_unlock(gc);
  272. }
  273. static void mvebu_gpio_edge_irq_unmask(struct irq_data *d)
  274. {
  275. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  276. struct mvebu_gpio_chip *mvchip = gc->private;
  277. u32 mask = 1 << (d->irq - gc->irq_base);
  278. irq_gc_lock(gc);
  279. gc->mask_cache |= mask;
  280. writel_relaxed(gc->mask_cache, mvebu_gpioreg_edge_mask(mvchip));
  281. irq_gc_unlock(gc);
  282. }
  283. static void mvebu_gpio_level_irq_mask(struct irq_data *d)
  284. {
  285. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  286. struct mvebu_gpio_chip *mvchip = gc->private;
  287. u32 mask = 1 << (d->irq - gc->irq_base);
  288. irq_gc_lock(gc);
  289. gc->mask_cache &= ~mask;
  290. writel_relaxed(gc->mask_cache, mvebu_gpioreg_level_mask(mvchip));
  291. irq_gc_unlock(gc);
  292. }
  293. static void mvebu_gpio_level_irq_unmask(struct irq_data *d)
  294. {
  295. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  296. struct mvebu_gpio_chip *mvchip = gc->private;
  297. u32 mask = 1 << (d->irq - gc->irq_base);
  298. irq_gc_lock(gc);
  299. gc->mask_cache |= mask;
  300. writel_relaxed(gc->mask_cache, mvebu_gpioreg_level_mask(mvchip));
  301. irq_gc_unlock(gc);
  302. }
  303. /*****************************************************************************
  304. * MVEBU GPIO IRQ
  305. *
  306. * GPIO_IN_POL register controls whether GPIO_DATA_IN will hold the same
  307. * value of the line or the opposite value.
  308. *
  309. * Level IRQ handlers: DATA_IN is used directly as cause register.
  310. * Interrupt are masked by LEVEL_MASK registers.
  311. * Edge IRQ handlers: Change in DATA_IN are latched in EDGE_CAUSE.
  312. * Interrupt are masked by EDGE_MASK registers.
  313. * Both-edge handlers: Similar to regular Edge handlers, but also swaps
  314. * the polarity to catch the next line transaction.
  315. * This is a race condition that might not perfectly
  316. * work on some use cases.
  317. *
  318. * Every eight GPIO lines are grouped (OR'ed) before going up to main
  319. * cause register.
  320. *
  321. * EDGE cause mask
  322. * data-in /--------| |-----| |----\
  323. * -----| |----- ---- to main cause reg
  324. * X \----------------| |----/
  325. * polarity LEVEL mask
  326. *
  327. ****************************************************************************/
  328. static int mvebu_gpio_irq_set_type(struct irq_data *d, unsigned int type)
  329. {
  330. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  331. struct irq_chip_type *ct = irq_data_get_chip_type(d);
  332. struct mvebu_gpio_chip *mvchip = gc->private;
  333. int pin;
  334. u32 u;
  335. pin = d->hwirq;
  336. u = readl_relaxed(mvebu_gpioreg_io_conf(mvchip)) & (1 << pin);
  337. if (!u) {
  338. return -EINVAL;
  339. }
  340. type &= IRQ_TYPE_SENSE_MASK;
  341. if (type == IRQ_TYPE_NONE)
  342. return -EINVAL;
  343. /* Check if we need to change chip and handler */
  344. if (!(ct->type & type))
  345. if (irq_setup_alt_chip(d, type))
  346. return -EINVAL;
  347. /*
  348. * Configure interrupt polarity.
  349. */
  350. switch (type) {
  351. case IRQ_TYPE_EDGE_RISING:
  352. case IRQ_TYPE_LEVEL_HIGH:
  353. u = readl_relaxed(mvebu_gpioreg_in_pol(mvchip));
  354. u &= ~(1 << pin);
  355. writel_relaxed(u, mvebu_gpioreg_in_pol(mvchip));
  356. break;
  357. case IRQ_TYPE_EDGE_FALLING:
  358. case IRQ_TYPE_LEVEL_LOW:
  359. u = readl_relaxed(mvebu_gpioreg_in_pol(mvchip));
  360. u |= 1 << pin;
  361. writel_relaxed(u, mvebu_gpioreg_in_pol(mvchip));
  362. break;
  363. case IRQ_TYPE_EDGE_BOTH: {
  364. u32 v;
  365. v = readl_relaxed(mvebu_gpioreg_in_pol(mvchip)) ^
  366. readl_relaxed(mvebu_gpioreg_data_in(mvchip));
  367. /*
  368. * set initial polarity based on current input level
  369. */
  370. u = readl_relaxed(mvebu_gpioreg_in_pol(mvchip));
  371. if (v & (1 << pin))
  372. u |= 1 << pin; /* falling */
  373. else
  374. u &= ~(1 << pin); /* rising */
  375. writel_relaxed(u, mvebu_gpioreg_in_pol(mvchip));
  376. break;
  377. }
  378. }
  379. return 0;
  380. }
  381. static void mvebu_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
  382. {
  383. struct mvebu_gpio_chip *mvchip = irq_get_handler_data(irq);
  384. struct irq_chip *chip = irq_desc_get_chip(desc);
  385. u32 cause, type;
  386. int i;
  387. if (mvchip == NULL)
  388. return;
  389. chained_irq_enter(chip, desc);
  390. cause = readl_relaxed(mvebu_gpioreg_data_in(mvchip)) &
  391. readl_relaxed(mvebu_gpioreg_level_mask(mvchip));
  392. cause |= readl_relaxed(mvebu_gpioreg_edge_cause(mvchip)) &
  393. readl_relaxed(mvebu_gpioreg_edge_mask(mvchip));
  394. for (i = 0; i < mvchip->chip.ngpio; i++) {
  395. int irq;
  396. irq = mvchip->irqbase + i;
  397. if (!(cause & (1 << i)))
  398. continue;
  399. type = irq_get_trigger_type(irq);
  400. if ((type & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH) {
  401. /* Swap polarity (race with GPIO line) */
  402. u32 polarity;
  403. polarity = readl_relaxed(mvebu_gpioreg_in_pol(mvchip));
  404. polarity ^= 1 << i;
  405. writel_relaxed(polarity, mvebu_gpioreg_in_pol(mvchip));
  406. }
  407. generic_handle_irq(irq);
  408. }
  409. chained_irq_exit(chip, desc);
  410. }
  411. #ifdef CONFIG_DEBUG_FS
  412. #include <linux/seq_file.h>
  413. static void mvebu_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  414. {
  415. struct mvebu_gpio_chip *mvchip =
  416. container_of(chip, struct mvebu_gpio_chip, chip);
  417. u32 out, io_conf, blink, in_pol, data_in, cause, edg_msk, lvl_msk;
  418. int i;
  419. out = readl_relaxed(mvebu_gpioreg_out(mvchip));
  420. io_conf = readl_relaxed(mvebu_gpioreg_io_conf(mvchip));
  421. blink = readl_relaxed(mvebu_gpioreg_blink(mvchip));
  422. in_pol = readl_relaxed(mvebu_gpioreg_in_pol(mvchip));
  423. data_in = readl_relaxed(mvebu_gpioreg_data_in(mvchip));
  424. cause = readl_relaxed(mvebu_gpioreg_edge_cause(mvchip));
  425. edg_msk = readl_relaxed(mvebu_gpioreg_edge_mask(mvchip));
  426. lvl_msk = readl_relaxed(mvebu_gpioreg_level_mask(mvchip));
  427. for (i = 0; i < chip->ngpio; i++) {
  428. const char *label;
  429. u32 msk;
  430. bool is_out;
  431. label = gpiochip_is_requested(chip, i);
  432. if (!label)
  433. continue;
  434. msk = 1 << i;
  435. is_out = !(io_conf & msk);
  436. seq_printf(s, " gpio-%-3d (%-20.20s)", chip->base + i, label);
  437. if (is_out) {
  438. seq_printf(s, " out %s %s\n",
  439. out & msk ? "hi" : "lo",
  440. blink & msk ? "(blink )" : "");
  441. continue;
  442. }
  443. seq_printf(s, " in %s (act %s) - IRQ",
  444. (data_in ^ in_pol) & msk ? "hi" : "lo",
  445. in_pol & msk ? "lo" : "hi");
  446. if (!((edg_msk | lvl_msk) & msk)) {
  447. seq_printf(s, " disabled\n");
  448. continue;
  449. }
  450. if (edg_msk & msk)
  451. seq_printf(s, " edge ");
  452. if (lvl_msk & msk)
  453. seq_printf(s, " level");
  454. seq_printf(s, " (%s)\n", cause & msk ? "pending" : "clear ");
  455. }
  456. }
  457. #else
  458. #define mvebu_gpio_dbg_show NULL
  459. #endif
  460. static const struct of_device_id mvebu_gpio_of_match[] = {
  461. {
  462. .compatible = "marvell,orion-gpio",
  463. .data = (void *) MVEBU_GPIO_SOC_VARIANT_ORION,
  464. },
  465. {
  466. .compatible = "marvell,mv78200-gpio",
  467. .data = (void *) MVEBU_GPIO_SOC_VARIANT_MV78200,
  468. },
  469. {
  470. .compatible = "marvell,armadaxp-gpio",
  471. .data = (void *) MVEBU_GPIO_SOC_VARIANT_ARMADAXP,
  472. },
  473. {
  474. /* sentinel */
  475. },
  476. };
  477. MODULE_DEVICE_TABLE(of, mvebu_gpio_of_match);
  478. static int mvebu_gpio_probe(struct platform_device *pdev)
  479. {
  480. struct mvebu_gpio_chip *mvchip;
  481. const struct of_device_id *match;
  482. struct device_node *np = pdev->dev.of_node;
  483. struct resource *res;
  484. struct irq_chip_generic *gc;
  485. struct irq_chip_type *ct;
  486. struct clk *clk;
  487. unsigned int ngpios;
  488. int soc_variant;
  489. int i, cpu, id;
  490. match = of_match_device(mvebu_gpio_of_match, &pdev->dev);
  491. if (match)
  492. soc_variant = (int) match->data;
  493. else
  494. soc_variant = MVEBU_GPIO_SOC_VARIANT_ORION;
  495. mvchip = devm_kzalloc(&pdev->dev, sizeof(struct mvebu_gpio_chip), GFP_KERNEL);
  496. if (!mvchip)
  497. return -ENOMEM;
  498. if (of_property_read_u32(pdev->dev.of_node, "ngpios", &ngpios)) {
  499. dev_err(&pdev->dev, "Missing ngpios OF property\n");
  500. return -ENODEV;
  501. }
  502. id = of_alias_get_id(pdev->dev.of_node, "gpio");
  503. if (id < 0) {
  504. dev_err(&pdev->dev, "Couldn't get OF id\n");
  505. return id;
  506. }
  507. clk = devm_clk_get(&pdev->dev, NULL);
  508. /* Not all SoCs require a clock.*/
  509. if (!IS_ERR(clk))
  510. clk_prepare_enable(clk);
  511. mvchip->soc_variant = soc_variant;
  512. mvchip->chip.label = dev_name(&pdev->dev);
  513. mvchip->chip.dev = &pdev->dev;
  514. mvchip->chip.request = mvebu_gpio_request;
  515. mvchip->chip.free = mvebu_gpio_free;
  516. mvchip->chip.direction_input = mvebu_gpio_direction_input;
  517. mvchip->chip.get = mvebu_gpio_get;
  518. mvchip->chip.direction_output = mvebu_gpio_direction_output;
  519. mvchip->chip.set = mvebu_gpio_set;
  520. mvchip->chip.to_irq = mvebu_gpio_to_irq;
  521. mvchip->chip.base = id * MVEBU_MAX_GPIO_PER_BANK;
  522. mvchip->chip.ngpio = ngpios;
  523. mvchip->chip.can_sleep = false;
  524. mvchip->chip.of_node = np;
  525. mvchip->chip.dbg_show = mvebu_gpio_dbg_show;
  526. spin_lock_init(&mvchip->lock);
  527. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  528. mvchip->membase = devm_ioremap_resource(&pdev->dev, res);
  529. if (IS_ERR(mvchip->membase))
  530. return PTR_ERR(mvchip->membase);
  531. /* The Armada XP has a second range of registers for the
  532. * per-CPU registers */
  533. if (soc_variant == MVEBU_GPIO_SOC_VARIANT_ARMADAXP) {
  534. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  535. mvchip->percpu_membase = devm_ioremap_resource(&pdev->dev,
  536. res);
  537. if (IS_ERR(mvchip->percpu_membase))
  538. return PTR_ERR(mvchip->percpu_membase);
  539. }
  540. /*
  541. * Mask and clear GPIO interrupts.
  542. */
  543. switch (soc_variant) {
  544. case MVEBU_GPIO_SOC_VARIANT_ORION:
  545. writel_relaxed(0, mvchip->membase + GPIO_EDGE_CAUSE_OFF);
  546. writel_relaxed(0, mvchip->membase + GPIO_EDGE_MASK_OFF);
  547. writel_relaxed(0, mvchip->membase + GPIO_LEVEL_MASK_OFF);
  548. break;
  549. case MVEBU_GPIO_SOC_VARIANT_MV78200:
  550. writel_relaxed(0, mvchip->membase + GPIO_EDGE_CAUSE_OFF);
  551. for (cpu = 0; cpu < 2; cpu++) {
  552. writel_relaxed(0, mvchip->membase +
  553. GPIO_EDGE_MASK_MV78200_OFF(cpu));
  554. writel_relaxed(0, mvchip->membase +
  555. GPIO_LEVEL_MASK_MV78200_OFF(cpu));
  556. }
  557. break;
  558. case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
  559. writel_relaxed(0, mvchip->membase + GPIO_EDGE_CAUSE_OFF);
  560. writel_relaxed(0, mvchip->membase + GPIO_EDGE_MASK_OFF);
  561. writel_relaxed(0, mvchip->membase + GPIO_LEVEL_MASK_OFF);
  562. for (cpu = 0; cpu < 4; cpu++) {
  563. writel_relaxed(0, mvchip->percpu_membase +
  564. GPIO_EDGE_CAUSE_ARMADAXP_OFF(cpu));
  565. writel_relaxed(0, mvchip->percpu_membase +
  566. GPIO_EDGE_MASK_ARMADAXP_OFF(cpu));
  567. writel_relaxed(0, mvchip->percpu_membase +
  568. GPIO_LEVEL_MASK_ARMADAXP_OFF(cpu));
  569. }
  570. break;
  571. default:
  572. BUG();
  573. }
  574. gpiochip_add(&mvchip->chip);
  575. /* Some gpio controllers do not provide irq support */
  576. if (!of_irq_count(np))
  577. return 0;
  578. /* Setup the interrupt handlers. Each chip can have up to 4
  579. * interrupt handlers, with each handler dealing with 8 GPIO
  580. * pins. */
  581. for (i = 0; i < 4; i++) {
  582. int irq;
  583. irq = platform_get_irq(pdev, i);
  584. if (irq < 0)
  585. continue;
  586. irq_set_handler_data(irq, mvchip);
  587. irq_set_chained_handler(irq, mvebu_gpio_irq_handler);
  588. }
  589. mvchip->irqbase = irq_alloc_descs(-1, 0, ngpios, -1);
  590. if (mvchip->irqbase < 0) {
  591. dev_err(&pdev->dev, "no irqs\n");
  592. return mvchip->irqbase;
  593. }
  594. gc = irq_alloc_generic_chip("mvebu_gpio_irq", 2, mvchip->irqbase,
  595. mvchip->membase, handle_level_irq);
  596. if (!gc) {
  597. dev_err(&pdev->dev, "Cannot allocate generic irq_chip\n");
  598. return -ENOMEM;
  599. }
  600. gc->private = mvchip;
  601. ct = &gc->chip_types[0];
  602. ct->type = IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW;
  603. ct->chip.irq_mask = mvebu_gpio_level_irq_mask;
  604. ct->chip.irq_unmask = mvebu_gpio_level_irq_unmask;
  605. ct->chip.irq_set_type = mvebu_gpio_irq_set_type;
  606. ct->chip.name = mvchip->chip.label;
  607. ct = &gc->chip_types[1];
  608. ct->type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
  609. ct->chip.irq_ack = mvebu_gpio_irq_ack;
  610. ct->chip.irq_mask = mvebu_gpio_edge_irq_mask;
  611. ct->chip.irq_unmask = mvebu_gpio_edge_irq_unmask;
  612. ct->chip.irq_set_type = mvebu_gpio_irq_set_type;
  613. ct->handler = handle_edge_irq;
  614. ct->chip.name = mvchip->chip.label;
  615. irq_setup_generic_chip(gc, IRQ_MSK(ngpios), 0,
  616. IRQ_NOREQUEST, IRQ_LEVEL | IRQ_NOPROBE);
  617. /* Setup irq domain on top of the generic chip. */
  618. mvchip->domain = irq_domain_add_simple(np, mvchip->chip.ngpio,
  619. mvchip->irqbase,
  620. &irq_domain_simple_ops,
  621. mvchip);
  622. if (!mvchip->domain) {
  623. dev_err(&pdev->dev, "couldn't allocate irq domain %s (DT).\n",
  624. mvchip->chip.label);
  625. irq_remove_generic_chip(gc, IRQ_MSK(ngpios), IRQ_NOREQUEST,
  626. IRQ_LEVEL | IRQ_NOPROBE);
  627. kfree(gc);
  628. return -ENODEV;
  629. }
  630. return 0;
  631. }
  632. static struct platform_driver mvebu_gpio_driver = {
  633. .driver = {
  634. .name = "mvebu-gpio",
  635. .owner = THIS_MODULE,
  636. .of_match_table = mvebu_gpio_of_match,
  637. },
  638. .probe = mvebu_gpio_probe,
  639. };
  640. module_platform_driver(mvebu_gpio_driver);