gpio-mvebu.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  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. /* Used to preserve GPIO registers accross suspend/resume */
  80. u32 out_reg;
  81. u32 io_conf_reg;
  82. u32 blink_en_reg;
  83. u32 in_pol_reg;
  84. u32 edge_mask_regs[4];
  85. u32 level_mask_regs[4];
  86. };
  87. /*
  88. * Functions returning addresses of individual registers for a given
  89. * GPIO controller.
  90. */
  91. static inline void __iomem *mvebu_gpioreg_out(struct mvebu_gpio_chip *mvchip)
  92. {
  93. return mvchip->membase + GPIO_OUT_OFF;
  94. }
  95. static inline void __iomem *mvebu_gpioreg_blink(struct mvebu_gpio_chip *mvchip)
  96. {
  97. return mvchip->membase + GPIO_BLINK_EN_OFF;
  98. }
  99. static inline void __iomem *mvebu_gpioreg_io_conf(struct mvebu_gpio_chip *mvchip)
  100. {
  101. return mvchip->membase + GPIO_IO_CONF_OFF;
  102. }
  103. static inline void __iomem *mvebu_gpioreg_in_pol(struct mvebu_gpio_chip *mvchip)
  104. {
  105. return mvchip->membase + GPIO_IN_POL_OFF;
  106. }
  107. static inline void __iomem *mvebu_gpioreg_data_in(struct mvebu_gpio_chip *mvchip)
  108. {
  109. return mvchip->membase + GPIO_DATA_IN_OFF;
  110. }
  111. static inline void __iomem *mvebu_gpioreg_edge_cause(struct mvebu_gpio_chip *mvchip)
  112. {
  113. int cpu;
  114. switch (mvchip->soc_variant) {
  115. case MVEBU_GPIO_SOC_VARIANT_ORION:
  116. case MVEBU_GPIO_SOC_VARIANT_MV78200:
  117. return mvchip->membase + GPIO_EDGE_CAUSE_OFF;
  118. case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
  119. cpu = smp_processor_id();
  120. return mvchip->percpu_membase + GPIO_EDGE_CAUSE_ARMADAXP_OFF(cpu);
  121. default:
  122. BUG();
  123. }
  124. }
  125. static inline void __iomem *mvebu_gpioreg_edge_mask(struct mvebu_gpio_chip *mvchip)
  126. {
  127. int cpu;
  128. switch (mvchip->soc_variant) {
  129. case MVEBU_GPIO_SOC_VARIANT_ORION:
  130. return mvchip->membase + GPIO_EDGE_MASK_OFF;
  131. case MVEBU_GPIO_SOC_VARIANT_MV78200:
  132. cpu = smp_processor_id();
  133. return mvchip->membase + GPIO_EDGE_MASK_MV78200_OFF(cpu);
  134. case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
  135. cpu = smp_processor_id();
  136. return mvchip->percpu_membase + GPIO_EDGE_MASK_ARMADAXP_OFF(cpu);
  137. default:
  138. BUG();
  139. }
  140. }
  141. static void __iomem *mvebu_gpioreg_level_mask(struct mvebu_gpio_chip *mvchip)
  142. {
  143. int cpu;
  144. switch (mvchip->soc_variant) {
  145. case MVEBU_GPIO_SOC_VARIANT_ORION:
  146. return mvchip->membase + GPIO_LEVEL_MASK_OFF;
  147. case MVEBU_GPIO_SOC_VARIANT_MV78200:
  148. cpu = smp_processor_id();
  149. return mvchip->membase + GPIO_LEVEL_MASK_MV78200_OFF(cpu);
  150. case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
  151. cpu = smp_processor_id();
  152. return mvchip->percpu_membase + GPIO_LEVEL_MASK_ARMADAXP_OFF(cpu);
  153. default:
  154. BUG();
  155. }
  156. }
  157. /*
  158. * Functions implementing the gpio_chip methods
  159. */
  160. static int mvebu_gpio_request(struct gpio_chip *chip, unsigned pin)
  161. {
  162. return pinctrl_request_gpio(chip->base + pin);
  163. }
  164. static void mvebu_gpio_free(struct gpio_chip *chip, unsigned pin)
  165. {
  166. pinctrl_free_gpio(chip->base + pin);
  167. }
  168. static void mvebu_gpio_set(struct gpio_chip *chip, unsigned pin, int value)
  169. {
  170. struct mvebu_gpio_chip *mvchip =
  171. container_of(chip, struct mvebu_gpio_chip, chip);
  172. unsigned long flags;
  173. u32 u;
  174. spin_lock_irqsave(&mvchip->lock, flags);
  175. u = readl_relaxed(mvebu_gpioreg_out(mvchip));
  176. if (value)
  177. u |= 1 << pin;
  178. else
  179. u &= ~(1 << pin);
  180. writel_relaxed(u, mvebu_gpioreg_out(mvchip));
  181. spin_unlock_irqrestore(&mvchip->lock, flags);
  182. }
  183. static int mvebu_gpio_get(struct gpio_chip *chip, unsigned pin)
  184. {
  185. struct mvebu_gpio_chip *mvchip =
  186. container_of(chip, struct mvebu_gpio_chip, chip);
  187. u32 u;
  188. if (readl_relaxed(mvebu_gpioreg_io_conf(mvchip)) & (1 << pin)) {
  189. u = readl_relaxed(mvebu_gpioreg_data_in(mvchip)) ^
  190. readl_relaxed(mvebu_gpioreg_in_pol(mvchip));
  191. } else {
  192. u = readl_relaxed(mvebu_gpioreg_out(mvchip));
  193. }
  194. return (u >> pin) & 1;
  195. }
  196. static void mvebu_gpio_blink(struct gpio_chip *chip, unsigned pin, int value)
  197. {
  198. struct mvebu_gpio_chip *mvchip =
  199. container_of(chip, struct mvebu_gpio_chip, chip);
  200. unsigned long flags;
  201. u32 u;
  202. spin_lock_irqsave(&mvchip->lock, flags);
  203. u = readl_relaxed(mvebu_gpioreg_blink(mvchip));
  204. if (value)
  205. u |= 1 << pin;
  206. else
  207. u &= ~(1 << pin);
  208. writel_relaxed(u, mvebu_gpioreg_blink(mvchip));
  209. spin_unlock_irqrestore(&mvchip->lock, flags);
  210. }
  211. static int mvebu_gpio_direction_input(struct gpio_chip *chip, unsigned pin)
  212. {
  213. struct mvebu_gpio_chip *mvchip =
  214. container_of(chip, struct mvebu_gpio_chip, chip);
  215. unsigned long flags;
  216. int ret;
  217. u32 u;
  218. /* Check with the pinctrl driver whether this pin is usable as
  219. * an input GPIO */
  220. ret = pinctrl_gpio_direction_input(chip->base + pin);
  221. if (ret)
  222. return ret;
  223. spin_lock_irqsave(&mvchip->lock, flags);
  224. u = readl_relaxed(mvebu_gpioreg_io_conf(mvchip));
  225. u |= 1 << pin;
  226. writel_relaxed(u, mvebu_gpioreg_io_conf(mvchip));
  227. spin_unlock_irqrestore(&mvchip->lock, flags);
  228. return 0;
  229. }
  230. static int mvebu_gpio_direction_output(struct gpio_chip *chip, unsigned pin,
  231. int value)
  232. {
  233. struct mvebu_gpio_chip *mvchip =
  234. container_of(chip, struct mvebu_gpio_chip, chip);
  235. unsigned long flags;
  236. int ret;
  237. u32 u;
  238. /* Check with the pinctrl driver whether this pin is usable as
  239. * an output GPIO */
  240. ret = pinctrl_gpio_direction_output(chip->base + pin);
  241. if (ret)
  242. return ret;
  243. mvebu_gpio_blink(chip, pin, 0);
  244. mvebu_gpio_set(chip, pin, value);
  245. spin_lock_irqsave(&mvchip->lock, flags);
  246. u = readl_relaxed(mvebu_gpioreg_io_conf(mvchip));
  247. u &= ~(1 << pin);
  248. writel_relaxed(u, mvebu_gpioreg_io_conf(mvchip));
  249. spin_unlock_irqrestore(&mvchip->lock, flags);
  250. return 0;
  251. }
  252. static int mvebu_gpio_to_irq(struct gpio_chip *chip, unsigned pin)
  253. {
  254. struct mvebu_gpio_chip *mvchip =
  255. container_of(chip, struct mvebu_gpio_chip, chip);
  256. return irq_create_mapping(mvchip->domain, pin);
  257. }
  258. /*
  259. * Functions implementing the irq_chip methods
  260. */
  261. static void mvebu_gpio_irq_ack(struct irq_data *d)
  262. {
  263. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  264. struct mvebu_gpio_chip *mvchip = gc->private;
  265. u32 mask = ~(1 << (d->irq - gc->irq_base));
  266. irq_gc_lock(gc);
  267. writel_relaxed(mask, mvebu_gpioreg_edge_cause(mvchip));
  268. irq_gc_unlock(gc);
  269. }
  270. static void mvebu_gpio_edge_irq_mask(struct irq_data *d)
  271. {
  272. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  273. struct mvebu_gpio_chip *mvchip = gc->private;
  274. u32 mask = 1 << (d->irq - gc->irq_base);
  275. irq_gc_lock(gc);
  276. gc->mask_cache &= ~mask;
  277. writel_relaxed(gc->mask_cache, mvebu_gpioreg_edge_mask(mvchip));
  278. irq_gc_unlock(gc);
  279. }
  280. static void mvebu_gpio_edge_irq_unmask(struct irq_data *d)
  281. {
  282. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  283. struct mvebu_gpio_chip *mvchip = gc->private;
  284. u32 mask = 1 << (d->irq - gc->irq_base);
  285. irq_gc_lock(gc);
  286. gc->mask_cache |= mask;
  287. writel_relaxed(gc->mask_cache, mvebu_gpioreg_edge_mask(mvchip));
  288. irq_gc_unlock(gc);
  289. }
  290. static void mvebu_gpio_level_irq_mask(struct irq_data *d)
  291. {
  292. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  293. struct mvebu_gpio_chip *mvchip = gc->private;
  294. u32 mask = 1 << (d->irq - gc->irq_base);
  295. irq_gc_lock(gc);
  296. gc->mask_cache &= ~mask;
  297. writel_relaxed(gc->mask_cache, mvebu_gpioreg_level_mask(mvchip));
  298. irq_gc_unlock(gc);
  299. }
  300. static void mvebu_gpio_level_irq_unmask(struct irq_data *d)
  301. {
  302. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  303. struct mvebu_gpio_chip *mvchip = gc->private;
  304. u32 mask = 1 << (d->irq - gc->irq_base);
  305. irq_gc_lock(gc);
  306. gc->mask_cache |= mask;
  307. writel_relaxed(gc->mask_cache, mvebu_gpioreg_level_mask(mvchip));
  308. irq_gc_unlock(gc);
  309. }
  310. /*****************************************************************************
  311. * MVEBU GPIO IRQ
  312. *
  313. * GPIO_IN_POL register controls whether GPIO_DATA_IN will hold the same
  314. * value of the line or the opposite value.
  315. *
  316. * Level IRQ handlers: DATA_IN is used directly as cause register.
  317. * Interrupt are masked by LEVEL_MASK registers.
  318. * Edge IRQ handlers: Change in DATA_IN are latched in EDGE_CAUSE.
  319. * Interrupt are masked by EDGE_MASK registers.
  320. * Both-edge handlers: Similar to regular Edge handlers, but also swaps
  321. * the polarity to catch the next line transaction.
  322. * This is a race condition that might not perfectly
  323. * work on some use cases.
  324. *
  325. * Every eight GPIO lines are grouped (OR'ed) before going up to main
  326. * cause register.
  327. *
  328. * EDGE cause mask
  329. * data-in /--------| |-----| |----\
  330. * -----| |----- ---- to main cause reg
  331. * X \----------------| |----/
  332. * polarity LEVEL mask
  333. *
  334. ****************************************************************************/
  335. static int mvebu_gpio_irq_set_type(struct irq_data *d, unsigned int type)
  336. {
  337. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  338. struct irq_chip_type *ct = irq_data_get_chip_type(d);
  339. struct mvebu_gpio_chip *mvchip = gc->private;
  340. int pin;
  341. u32 u;
  342. pin = d->hwirq;
  343. u = readl_relaxed(mvebu_gpioreg_io_conf(mvchip)) & (1 << pin);
  344. if (!u) {
  345. return -EINVAL;
  346. }
  347. type &= IRQ_TYPE_SENSE_MASK;
  348. if (type == IRQ_TYPE_NONE)
  349. return -EINVAL;
  350. /* Check if we need to change chip and handler */
  351. if (!(ct->type & type))
  352. if (irq_setup_alt_chip(d, type))
  353. return -EINVAL;
  354. /*
  355. * Configure interrupt polarity.
  356. */
  357. switch (type) {
  358. case IRQ_TYPE_EDGE_RISING:
  359. case IRQ_TYPE_LEVEL_HIGH:
  360. u = readl_relaxed(mvebu_gpioreg_in_pol(mvchip));
  361. u &= ~(1 << pin);
  362. writel_relaxed(u, mvebu_gpioreg_in_pol(mvchip));
  363. break;
  364. case IRQ_TYPE_EDGE_FALLING:
  365. case IRQ_TYPE_LEVEL_LOW:
  366. u = readl_relaxed(mvebu_gpioreg_in_pol(mvchip));
  367. u |= 1 << pin;
  368. writel_relaxed(u, mvebu_gpioreg_in_pol(mvchip));
  369. break;
  370. case IRQ_TYPE_EDGE_BOTH: {
  371. u32 v;
  372. v = readl_relaxed(mvebu_gpioreg_in_pol(mvchip)) ^
  373. readl_relaxed(mvebu_gpioreg_data_in(mvchip));
  374. /*
  375. * set initial polarity based on current input level
  376. */
  377. u = readl_relaxed(mvebu_gpioreg_in_pol(mvchip));
  378. if (v & (1 << pin))
  379. u |= 1 << pin; /* falling */
  380. else
  381. u &= ~(1 << pin); /* rising */
  382. writel_relaxed(u, mvebu_gpioreg_in_pol(mvchip));
  383. break;
  384. }
  385. }
  386. return 0;
  387. }
  388. static void mvebu_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
  389. {
  390. struct mvebu_gpio_chip *mvchip = irq_get_handler_data(irq);
  391. struct irq_chip *chip = irq_desc_get_chip(desc);
  392. u32 cause, type;
  393. int i;
  394. if (mvchip == NULL)
  395. return;
  396. chained_irq_enter(chip, desc);
  397. cause = readl_relaxed(mvebu_gpioreg_data_in(mvchip)) &
  398. readl_relaxed(mvebu_gpioreg_level_mask(mvchip));
  399. cause |= readl_relaxed(mvebu_gpioreg_edge_cause(mvchip)) &
  400. readl_relaxed(mvebu_gpioreg_edge_mask(mvchip));
  401. for (i = 0; i < mvchip->chip.ngpio; i++) {
  402. int irq;
  403. irq = mvchip->irqbase + i;
  404. if (!(cause & (1 << i)))
  405. continue;
  406. type = irq_get_trigger_type(irq);
  407. if ((type & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH) {
  408. /* Swap polarity (race with GPIO line) */
  409. u32 polarity;
  410. polarity = readl_relaxed(mvebu_gpioreg_in_pol(mvchip));
  411. polarity ^= 1 << i;
  412. writel_relaxed(polarity, mvebu_gpioreg_in_pol(mvchip));
  413. }
  414. generic_handle_irq(irq);
  415. }
  416. chained_irq_exit(chip, desc);
  417. }
  418. #ifdef CONFIG_DEBUG_FS
  419. #include <linux/seq_file.h>
  420. static void mvebu_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  421. {
  422. struct mvebu_gpio_chip *mvchip =
  423. container_of(chip, struct mvebu_gpio_chip, chip);
  424. u32 out, io_conf, blink, in_pol, data_in, cause, edg_msk, lvl_msk;
  425. int i;
  426. out = readl_relaxed(mvebu_gpioreg_out(mvchip));
  427. io_conf = readl_relaxed(mvebu_gpioreg_io_conf(mvchip));
  428. blink = readl_relaxed(mvebu_gpioreg_blink(mvchip));
  429. in_pol = readl_relaxed(mvebu_gpioreg_in_pol(mvchip));
  430. data_in = readl_relaxed(mvebu_gpioreg_data_in(mvchip));
  431. cause = readl_relaxed(mvebu_gpioreg_edge_cause(mvchip));
  432. edg_msk = readl_relaxed(mvebu_gpioreg_edge_mask(mvchip));
  433. lvl_msk = readl_relaxed(mvebu_gpioreg_level_mask(mvchip));
  434. for (i = 0; i < chip->ngpio; i++) {
  435. const char *label;
  436. u32 msk;
  437. bool is_out;
  438. label = gpiochip_is_requested(chip, i);
  439. if (!label)
  440. continue;
  441. msk = 1 << i;
  442. is_out = !(io_conf & msk);
  443. seq_printf(s, " gpio-%-3d (%-20.20s)", chip->base + i, label);
  444. if (is_out) {
  445. seq_printf(s, " out %s %s\n",
  446. out & msk ? "hi" : "lo",
  447. blink & msk ? "(blink )" : "");
  448. continue;
  449. }
  450. seq_printf(s, " in %s (act %s) - IRQ",
  451. (data_in ^ in_pol) & msk ? "hi" : "lo",
  452. in_pol & msk ? "lo" : "hi");
  453. if (!((edg_msk | lvl_msk) & msk)) {
  454. seq_printf(s, " disabled\n");
  455. continue;
  456. }
  457. if (edg_msk & msk)
  458. seq_printf(s, " edge ");
  459. if (lvl_msk & msk)
  460. seq_printf(s, " level");
  461. seq_printf(s, " (%s)\n", cause & msk ? "pending" : "clear ");
  462. }
  463. }
  464. #else
  465. #define mvebu_gpio_dbg_show NULL
  466. #endif
  467. static const struct of_device_id mvebu_gpio_of_match[] = {
  468. {
  469. .compatible = "marvell,orion-gpio",
  470. .data = (void *) MVEBU_GPIO_SOC_VARIANT_ORION,
  471. },
  472. {
  473. .compatible = "marvell,mv78200-gpio",
  474. .data = (void *) MVEBU_GPIO_SOC_VARIANT_MV78200,
  475. },
  476. {
  477. .compatible = "marvell,armadaxp-gpio",
  478. .data = (void *) MVEBU_GPIO_SOC_VARIANT_ARMADAXP,
  479. },
  480. {
  481. /* sentinel */
  482. },
  483. };
  484. MODULE_DEVICE_TABLE(of, mvebu_gpio_of_match);
  485. static int mvebu_gpio_suspend(struct platform_device *pdev, pm_message_t state)
  486. {
  487. struct mvebu_gpio_chip *mvchip = platform_get_drvdata(pdev);
  488. int i;
  489. mvchip->out_reg = readl(mvebu_gpioreg_out(mvchip));
  490. mvchip->io_conf_reg = readl(mvebu_gpioreg_io_conf(mvchip));
  491. mvchip->blink_en_reg = readl(mvebu_gpioreg_blink(mvchip));
  492. mvchip->in_pol_reg = readl(mvebu_gpioreg_in_pol(mvchip));
  493. switch (mvchip->soc_variant) {
  494. case MVEBU_GPIO_SOC_VARIANT_ORION:
  495. mvchip->edge_mask_regs[0] =
  496. readl(mvchip->membase + GPIO_EDGE_MASK_OFF);
  497. mvchip->level_mask_regs[0] =
  498. readl(mvchip->membase + GPIO_LEVEL_MASK_OFF);
  499. break;
  500. case MVEBU_GPIO_SOC_VARIANT_MV78200:
  501. for (i = 0; i < 2; i++) {
  502. mvchip->edge_mask_regs[i] =
  503. readl(mvchip->membase +
  504. GPIO_EDGE_MASK_MV78200_OFF(i));
  505. mvchip->level_mask_regs[i] =
  506. readl(mvchip->membase +
  507. GPIO_LEVEL_MASK_MV78200_OFF(i));
  508. }
  509. break;
  510. case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
  511. for (i = 0; i < 4; i++) {
  512. mvchip->edge_mask_regs[i] =
  513. readl(mvchip->membase +
  514. GPIO_EDGE_MASK_ARMADAXP_OFF(i));
  515. mvchip->level_mask_regs[i] =
  516. readl(mvchip->membase +
  517. GPIO_LEVEL_MASK_ARMADAXP_OFF(i));
  518. }
  519. break;
  520. default:
  521. BUG();
  522. }
  523. return 0;
  524. }
  525. static int mvebu_gpio_resume(struct platform_device *pdev)
  526. {
  527. struct mvebu_gpio_chip *mvchip = platform_get_drvdata(pdev);
  528. int i;
  529. writel(mvchip->out_reg, mvebu_gpioreg_out(mvchip));
  530. writel(mvchip->io_conf_reg, mvebu_gpioreg_io_conf(mvchip));
  531. writel(mvchip->blink_en_reg, mvebu_gpioreg_blink(mvchip));
  532. writel(mvchip->in_pol_reg, mvebu_gpioreg_in_pol(mvchip));
  533. switch (mvchip->soc_variant) {
  534. case MVEBU_GPIO_SOC_VARIANT_ORION:
  535. writel(mvchip->edge_mask_regs[0],
  536. mvchip->membase + GPIO_EDGE_MASK_OFF);
  537. writel(mvchip->level_mask_regs[0],
  538. mvchip->membase + GPIO_LEVEL_MASK_OFF);
  539. break;
  540. case MVEBU_GPIO_SOC_VARIANT_MV78200:
  541. for (i = 0; i < 2; i++) {
  542. writel(mvchip->edge_mask_regs[i],
  543. mvchip->membase + GPIO_EDGE_MASK_MV78200_OFF(i));
  544. writel(mvchip->level_mask_regs[i],
  545. mvchip->membase +
  546. GPIO_LEVEL_MASK_MV78200_OFF(i));
  547. }
  548. break;
  549. case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
  550. for (i = 0; i < 4; i++) {
  551. writel(mvchip->edge_mask_regs[i],
  552. mvchip->membase +
  553. GPIO_EDGE_MASK_ARMADAXP_OFF(i));
  554. writel(mvchip->level_mask_regs[i],
  555. mvchip->membase +
  556. GPIO_LEVEL_MASK_ARMADAXP_OFF(i));
  557. }
  558. break;
  559. default:
  560. BUG();
  561. }
  562. return 0;
  563. }
  564. static int mvebu_gpio_probe(struct platform_device *pdev)
  565. {
  566. struct mvebu_gpio_chip *mvchip;
  567. const struct of_device_id *match;
  568. struct device_node *np = pdev->dev.of_node;
  569. struct resource *res;
  570. struct irq_chip_generic *gc;
  571. struct irq_chip_type *ct;
  572. struct clk *clk;
  573. unsigned int ngpios;
  574. int soc_variant;
  575. int i, cpu, id;
  576. match = of_match_device(mvebu_gpio_of_match, &pdev->dev);
  577. if (match)
  578. soc_variant = (int) match->data;
  579. else
  580. soc_variant = MVEBU_GPIO_SOC_VARIANT_ORION;
  581. mvchip = devm_kzalloc(&pdev->dev, sizeof(struct mvebu_gpio_chip), GFP_KERNEL);
  582. if (!mvchip)
  583. return -ENOMEM;
  584. platform_set_drvdata(pdev, mvchip);
  585. if (of_property_read_u32(pdev->dev.of_node, "ngpios", &ngpios)) {
  586. dev_err(&pdev->dev, "Missing ngpios OF property\n");
  587. return -ENODEV;
  588. }
  589. id = of_alias_get_id(pdev->dev.of_node, "gpio");
  590. if (id < 0) {
  591. dev_err(&pdev->dev, "Couldn't get OF id\n");
  592. return id;
  593. }
  594. clk = devm_clk_get(&pdev->dev, NULL);
  595. /* Not all SoCs require a clock.*/
  596. if (!IS_ERR(clk))
  597. clk_prepare_enable(clk);
  598. mvchip->soc_variant = soc_variant;
  599. mvchip->chip.label = dev_name(&pdev->dev);
  600. mvchip->chip.dev = &pdev->dev;
  601. mvchip->chip.request = mvebu_gpio_request;
  602. mvchip->chip.free = mvebu_gpio_free;
  603. mvchip->chip.direction_input = mvebu_gpio_direction_input;
  604. mvchip->chip.get = mvebu_gpio_get;
  605. mvchip->chip.direction_output = mvebu_gpio_direction_output;
  606. mvchip->chip.set = mvebu_gpio_set;
  607. mvchip->chip.to_irq = mvebu_gpio_to_irq;
  608. mvchip->chip.base = id * MVEBU_MAX_GPIO_PER_BANK;
  609. mvchip->chip.ngpio = ngpios;
  610. mvchip->chip.can_sleep = false;
  611. mvchip->chip.of_node = np;
  612. mvchip->chip.dbg_show = mvebu_gpio_dbg_show;
  613. spin_lock_init(&mvchip->lock);
  614. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  615. mvchip->membase = devm_ioremap_resource(&pdev->dev, res);
  616. if (IS_ERR(mvchip->membase))
  617. return PTR_ERR(mvchip->membase);
  618. /* The Armada XP has a second range of registers for the
  619. * per-CPU registers */
  620. if (soc_variant == MVEBU_GPIO_SOC_VARIANT_ARMADAXP) {
  621. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  622. mvchip->percpu_membase = devm_ioremap_resource(&pdev->dev,
  623. res);
  624. if (IS_ERR(mvchip->percpu_membase))
  625. return PTR_ERR(mvchip->percpu_membase);
  626. }
  627. /*
  628. * Mask and clear GPIO interrupts.
  629. */
  630. switch (soc_variant) {
  631. case MVEBU_GPIO_SOC_VARIANT_ORION:
  632. writel_relaxed(0, mvchip->membase + GPIO_EDGE_CAUSE_OFF);
  633. writel_relaxed(0, mvchip->membase + GPIO_EDGE_MASK_OFF);
  634. writel_relaxed(0, mvchip->membase + GPIO_LEVEL_MASK_OFF);
  635. break;
  636. case MVEBU_GPIO_SOC_VARIANT_MV78200:
  637. writel_relaxed(0, mvchip->membase + GPIO_EDGE_CAUSE_OFF);
  638. for (cpu = 0; cpu < 2; cpu++) {
  639. writel_relaxed(0, mvchip->membase +
  640. GPIO_EDGE_MASK_MV78200_OFF(cpu));
  641. writel_relaxed(0, mvchip->membase +
  642. GPIO_LEVEL_MASK_MV78200_OFF(cpu));
  643. }
  644. break;
  645. case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
  646. writel_relaxed(0, mvchip->membase + GPIO_EDGE_CAUSE_OFF);
  647. writel_relaxed(0, mvchip->membase + GPIO_EDGE_MASK_OFF);
  648. writel_relaxed(0, mvchip->membase + GPIO_LEVEL_MASK_OFF);
  649. for (cpu = 0; cpu < 4; cpu++) {
  650. writel_relaxed(0, mvchip->percpu_membase +
  651. GPIO_EDGE_CAUSE_ARMADAXP_OFF(cpu));
  652. writel_relaxed(0, mvchip->percpu_membase +
  653. GPIO_EDGE_MASK_ARMADAXP_OFF(cpu));
  654. writel_relaxed(0, mvchip->percpu_membase +
  655. GPIO_LEVEL_MASK_ARMADAXP_OFF(cpu));
  656. }
  657. break;
  658. default:
  659. BUG();
  660. }
  661. gpiochip_add(&mvchip->chip);
  662. /* Some gpio controllers do not provide irq support */
  663. if (!of_irq_count(np))
  664. return 0;
  665. /* Setup the interrupt handlers. Each chip can have up to 4
  666. * interrupt handlers, with each handler dealing with 8 GPIO
  667. * pins. */
  668. for (i = 0; i < 4; i++) {
  669. int irq;
  670. irq = platform_get_irq(pdev, i);
  671. if (irq < 0)
  672. continue;
  673. irq_set_handler_data(irq, mvchip);
  674. irq_set_chained_handler(irq, mvebu_gpio_irq_handler);
  675. }
  676. mvchip->irqbase = irq_alloc_descs(-1, 0, ngpios, -1);
  677. if (mvchip->irqbase < 0) {
  678. dev_err(&pdev->dev, "no irqs\n");
  679. return mvchip->irqbase;
  680. }
  681. gc = irq_alloc_generic_chip("mvebu_gpio_irq", 2, mvchip->irqbase,
  682. mvchip->membase, handle_level_irq);
  683. if (!gc) {
  684. dev_err(&pdev->dev, "Cannot allocate generic irq_chip\n");
  685. return -ENOMEM;
  686. }
  687. gc->private = mvchip;
  688. ct = &gc->chip_types[0];
  689. ct->type = IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW;
  690. ct->chip.irq_mask = mvebu_gpio_level_irq_mask;
  691. ct->chip.irq_unmask = mvebu_gpio_level_irq_unmask;
  692. ct->chip.irq_set_type = mvebu_gpio_irq_set_type;
  693. ct->chip.name = mvchip->chip.label;
  694. ct = &gc->chip_types[1];
  695. ct->type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
  696. ct->chip.irq_ack = mvebu_gpio_irq_ack;
  697. ct->chip.irq_mask = mvebu_gpio_edge_irq_mask;
  698. ct->chip.irq_unmask = mvebu_gpio_edge_irq_unmask;
  699. ct->chip.irq_set_type = mvebu_gpio_irq_set_type;
  700. ct->handler = handle_edge_irq;
  701. ct->chip.name = mvchip->chip.label;
  702. irq_setup_generic_chip(gc, IRQ_MSK(ngpios), 0,
  703. IRQ_NOREQUEST, IRQ_LEVEL | IRQ_NOPROBE);
  704. /* Setup irq domain on top of the generic chip. */
  705. mvchip->domain = irq_domain_add_simple(np, mvchip->chip.ngpio,
  706. mvchip->irqbase,
  707. &irq_domain_simple_ops,
  708. mvchip);
  709. if (!mvchip->domain) {
  710. dev_err(&pdev->dev, "couldn't allocate irq domain %s (DT).\n",
  711. mvchip->chip.label);
  712. irq_remove_generic_chip(gc, IRQ_MSK(ngpios), IRQ_NOREQUEST,
  713. IRQ_LEVEL | IRQ_NOPROBE);
  714. kfree(gc);
  715. return -ENODEV;
  716. }
  717. return 0;
  718. }
  719. static struct platform_driver mvebu_gpio_driver = {
  720. .driver = {
  721. .name = "mvebu-gpio",
  722. .of_match_table = mvebu_gpio_of_match,
  723. },
  724. .probe = mvebu_gpio_probe,
  725. .suspend = mvebu_gpio_suspend,
  726. .resume = mvebu_gpio_resume,
  727. };
  728. module_platform_driver(mvebu_gpio_driver);