gpio-mvebu.c 24 KB

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