gpio-mvebu.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  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/init.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 = d->mask;
  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 = d->mask;
  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 = d->mask;
  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 = d->mask;
  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 = d->mask;
  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 = irq_find_mapping(mvchip->domain, 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. static int mvebu_gpio_suspend(struct platform_device *pdev, pm_message_t state)
  480. {
  481. struct mvebu_gpio_chip *mvchip = platform_get_drvdata(pdev);
  482. int i;
  483. mvchip->out_reg = readl(mvebu_gpioreg_out(mvchip));
  484. mvchip->io_conf_reg = readl(mvebu_gpioreg_io_conf(mvchip));
  485. mvchip->blink_en_reg = readl(mvebu_gpioreg_blink(mvchip));
  486. mvchip->in_pol_reg = readl(mvebu_gpioreg_in_pol(mvchip));
  487. switch (mvchip->soc_variant) {
  488. case MVEBU_GPIO_SOC_VARIANT_ORION:
  489. mvchip->edge_mask_regs[0] =
  490. readl(mvchip->membase + GPIO_EDGE_MASK_OFF);
  491. mvchip->level_mask_regs[0] =
  492. readl(mvchip->membase + GPIO_LEVEL_MASK_OFF);
  493. break;
  494. case MVEBU_GPIO_SOC_VARIANT_MV78200:
  495. for (i = 0; i < 2; i++) {
  496. mvchip->edge_mask_regs[i] =
  497. readl(mvchip->membase +
  498. GPIO_EDGE_MASK_MV78200_OFF(i));
  499. mvchip->level_mask_regs[i] =
  500. readl(mvchip->membase +
  501. GPIO_LEVEL_MASK_MV78200_OFF(i));
  502. }
  503. break;
  504. case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
  505. for (i = 0; i < 4; i++) {
  506. mvchip->edge_mask_regs[i] =
  507. readl(mvchip->membase +
  508. GPIO_EDGE_MASK_ARMADAXP_OFF(i));
  509. mvchip->level_mask_regs[i] =
  510. readl(mvchip->membase +
  511. GPIO_LEVEL_MASK_ARMADAXP_OFF(i));
  512. }
  513. break;
  514. default:
  515. BUG();
  516. }
  517. return 0;
  518. }
  519. static int mvebu_gpio_resume(struct platform_device *pdev)
  520. {
  521. struct mvebu_gpio_chip *mvchip = platform_get_drvdata(pdev);
  522. int i;
  523. writel(mvchip->out_reg, mvebu_gpioreg_out(mvchip));
  524. writel(mvchip->io_conf_reg, mvebu_gpioreg_io_conf(mvchip));
  525. writel(mvchip->blink_en_reg, mvebu_gpioreg_blink(mvchip));
  526. writel(mvchip->in_pol_reg, mvebu_gpioreg_in_pol(mvchip));
  527. switch (mvchip->soc_variant) {
  528. case MVEBU_GPIO_SOC_VARIANT_ORION:
  529. writel(mvchip->edge_mask_regs[0],
  530. mvchip->membase + GPIO_EDGE_MASK_OFF);
  531. writel(mvchip->level_mask_regs[0],
  532. mvchip->membase + GPIO_LEVEL_MASK_OFF);
  533. break;
  534. case MVEBU_GPIO_SOC_VARIANT_MV78200:
  535. for (i = 0; i < 2; i++) {
  536. writel(mvchip->edge_mask_regs[i],
  537. mvchip->membase + GPIO_EDGE_MASK_MV78200_OFF(i));
  538. writel(mvchip->level_mask_regs[i],
  539. mvchip->membase +
  540. GPIO_LEVEL_MASK_MV78200_OFF(i));
  541. }
  542. break;
  543. case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
  544. for (i = 0; i < 4; i++) {
  545. writel(mvchip->edge_mask_regs[i],
  546. mvchip->membase +
  547. GPIO_EDGE_MASK_ARMADAXP_OFF(i));
  548. writel(mvchip->level_mask_regs[i],
  549. mvchip->membase +
  550. GPIO_LEVEL_MASK_ARMADAXP_OFF(i));
  551. }
  552. break;
  553. default:
  554. BUG();
  555. }
  556. return 0;
  557. }
  558. static int mvebu_gpio_probe(struct platform_device *pdev)
  559. {
  560. struct mvebu_gpio_chip *mvchip;
  561. const struct of_device_id *match;
  562. struct device_node *np = pdev->dev.of_node;
  563. struct resource *res;
  564. struct irq_chip_generic *gc;
  565. struct irq_chip_type *ct;
  566. struct clk *clk;
  567. unsigned int ngpios;
  568. bool have_irqs;
  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. /* Some gpio controllers do not provide irq support */
  578. have_irqs = of_irq_count(np) != 0;
  579. mvchip = devm_kzalloc(&pdev->dev, sizeof(struct mvebu_gpio_chip),
  580. GFP_KERNEL);
  581. if (!mvchip)
  582. return -ENOMEM;
  583. platform_set_drvdata(pdev, mvchip);
  584. if (of_property_read_u32(pdev->dev.of_node, "ngpios", &ngpios)) {
  585. dev_err(&pdev->dev, "Missing ngpios OF property\n");
  586. return -ENODEV;
  587. }
  588. id = of_alias_get_id(pdev->dev.of_node, "gpio");
  589. if (id < 0) {
  590. dev_err(&pdev->dev, "Couldn't get OF id\n");
  591. return id;
  592. }
  593. clk = devm_clk_get(&pdev->dev, NULL);
  594. /* Not all SoCs require a clock.*/
  595. if (!IS_ERR(clk))
  596. clk_prepare_enable(clk);
  597. mvchip->soc_variant = soc_variant;
  598. mvchip->chip.label = dev_name(&pdev->dev);
  599. mvchip->chip.parent = &pdev->dev;
  600. mvchip->chip.request = gpiochip_generic_request;
  601. mvchip->chip.free = gpiochip_generic_free;
  602. mvchip->chip.direction_input = mvebu_gpio_direction_input;
  603. mvchip->chip.get = mvebu_gpio_get;
  604. mvchip->chip.direction_output = mvebu_gpio_direction_output;
  605. mvchip->chip.set = mvebu_gpio_set;
  606. if (have_irqs)
  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. devm_gpiochip_add_data(&pdev->dev, &mvchip->chip, mvchip);
  662. /* Some gpio controllers do not provide irq support */
  663. if (!have_irqs)
  664. return 0;
  665. mvchip->domain =
  666. irq_domain_add_linear(np, ngpios, &irq_generic_chip_ops, NULL);
  667. if (!mvchip->domain) {
  668. dev_err(&pdev->dev, "couldn't allocate irq domain %s (DT).\n",
  669. mvchip->chip.label);
  670. return -ENODEV;
  671. }
  672. err = irq_alloc_domain_generic_chips(
  673. mvchip->domain, ngpios, 2, np->name, handle_level_irq,
  674. IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_LEVEL, 0, 0);
  675. if (err) {
  676. dev_err(&pdev->dev, "couldn't allocate irq chips %s (DT).\n",
  677. mvchip->chip.label);
  678. goto err_domain;
  679. }
  680. /* NOTE: The common accessors cannot be used because of the percpu
  681. * access to the mask registers
  682. */
  683. gc = irq_get_domain_generic_chip(mvchip->domain, 0);
  684. gc->private = mvchip;
  685. ct = &gc->chip_types[0];
  686. ct->type = IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW;
  687. ct->chip.irq_mask = mvebu_gpio_level_irq_mask;
  688. ct->chip.irq_unmask = mvebu_gpio_level_irq_unmask;
  689. ct->chip.irq_set_type = mvebu_gpio_irq_set_type;
  690. ct->chip.name = mvchip->chip.label;
  691. ct = &gc->chip_types[1];
  692. ct->type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
  693. ct->chip.irq_ack = mvebu_gpio_irq_ack;
  694. ct->chip.irq_mask = mvebu_gpio_edge_irq_mask;
  695. ct->chip.irq_unmask = mvebu_gpio_edge_irq_unmask;
  696. ct->chip.irq_set_type = mvebu_gpio_irq_set_type;
  697. ct->handler = handle_edge_irq;
  698. ct->chip.name = mvchip->chip.label;
  699. /* Setup the interrupt handlers. Each chip can have up to 4
  700. * interrupt handlers, with each handler dealing with 8 GPIO
  701. * pins.
  702. */
  703. for (i = 0; i < 4; i++) {
  704. int irq = platform_get_irq(pdev, i);
  705. if (irq < 0)
  706. continue;
  707. irq_set_chained_handler_and_data(irq, mvebu_gpio_irq_handler,
  708. mvchip);
  709. }
  710. return 0;
  711. err_domain:
  712. irq_domain_remove(mvchip->domain);
  713. return err;
  714. }
  715. static struct platform_driver mvebu_gpio_driver = {
  716. .driver = {
  717. .name = "mvebu-gpio",
  718. .of_match_table = mvebu_gpio_of_match,
  719. },
  720. .probe = mvebu_gpio_probe,
  721. .suspend = mvebu_gpio_suspend,
  722. .resume = mvebu_gpio_resume,
  723. };
  724. builtin_platform_driver(mvebu_gpio_driver);