gpio-mvebu.c 24 KB

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