gpio-mvebu.c 23 KB

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