gpio-mvebu.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  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/module.h>
  36. #include <linux/gpio.h>
  37. #include <linux/irq.h>
  38. #include <linux/slab.h>
  39. #include <linux/irqdomain.h>
  40. #include <linux/io.h>
  41. #include <linux/of_irq.h>
  42. #include <linux/of_device.h>
  43. #include <linux/pinctrl/consumer.h>
  44. /*
  45. * GPIO unit register offsets.
  46. */
  47. #define GPIO_OUT_OFF 0x0000
  48. #define GPIO_IO_CONF_OFF 0x0004
  49. #define GPIO_BLINK_EN_OFF 0x0008
  50. #define GPIO_IN_POL_OFF 0x000c
  51. #define GPIO_DATA_IN_OFF 0x0010
  52. #define GPIO_EDGE_CAUSE_OFF 0x0014
  53. #define GPIO_EDGE_MASK_OFF 0x0018
  54. #define GPIO_LEVEL_MASK_OFF 0x001c
  55. /* The MV78200 has per-CPU registers for edge mask and level mask */
  56. #define GPIO_EDGE_MASK_MV78200_OFF(cpu) ((cpu) ? 0x30 : 0x18)
  57. #define GPIO_LEVEL_MASK_MV78200_OFF(cpu) ((cpu) ? 0x34 : 0x1C)
  58. /* The Armada XP has per-CPU registers for interrupt cause, interrupt
  59. * mask and interrupt level mask. Those are relative to the
  60. * percpu_membase. */
  61. #define GPIO_EDGE_CAUSE_ARMADAXP_OFF(cpu) ((cpu) * 0x4)
  62. #define GPIO_EDGE_MASK_ARMADAXP_OFF(cpu) (0x10 + (cpu) * 0x4)
  63. #define GPIO_LEVEL_MASK_ARMADAXP_OFF(cpu) (0x20 + (cpu) * 0x4)
  64. #define MVEBU_GPIO_SOC_VARIANT_ORION 0x1
  65. #define MVEBU_GPIO_SOC_VARIANT_MV78200 0x2
  66. #define MVEBU_GPIO_SOC_VARIANT_ARMADAXP 0x3
  67. #define MVEBU_MAX_GPIO_PER_BANK 32
  68. struct mvebu_gpio_chip {
  69. struct gpio_chip chip;
  70. spinlock_t lock;
  71. void __iomem *membase;
  72. void __iomem *percpu_membase;
  73. unsigned int irqbase;
  74. struct irq_domain *domain;
  75. int soc_variant;
  76. };
  77. /*
  78. * Functions returning addresses of individual registers for a given
  79. * GPIO controller.
  80. */
  81. static inline void __iomem *mvebu_gpioreg_out(struct mvebu_gpio_chip *mvchip)
  82. {
  83. return mvchip->membase + GPIO_OUT_OFF;
  84. }
  85. static inline void __iomem *mvebu_gpioreg_blink(struct mvebu_gpio_chip *mvchip)
  86. {
  87. return mvchip->membase + GPIO_BLINK_EN_OFF;
  88. }
  89. static inline void __iomem *mvebu_gpioreg_io_conf(struct mvebu_gpio_chip *mvchip)
  90. {
  91. return mvchip->membase + GPIO_IO_CONF_OFF;
  92. }
  93. static inline void __iomem *mvebu_gpioreg_in_pol(struct mvebu_gpio_chip *mvchip)
  94. {
  95. return mvchip->membase + GPIO_IN_POL_OFF;
  96. }
  97. static inline void __iomem *mvebu_gpioreg_data_in(struct mvebu_gpio_chip *mvchip)
  98. {
  99. return mvchip->membase + GPIO_DATA_IN_OFF;
  100. }
  101. static inline void __iomem *mvebu_gpioreg_edge_cause(struct mvebu_gpio_chip *mvchip)
  102. {
  103. int cpu;
  104. switch(mvchip->soc_variant) {
  105. case MVEBU_GPIO_SOC_VARIANT_ORION:
  106. case MVEBU_GPIO_SOC_VARIANT_MV78200:
  107. return mvchip->membase + GPIO_EDGE_CAUSE_OFF;
  108. case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
  109. cpu = smp_processor_id();
  110. return mvchip->percpu_membase + GPIO_EDGE_CAUSE_ARMADAXP_OFF(cpu);
  111. default:
  112. BUG();
  113. }
  114. }
  115. static inline void __iomem *mvebu_gpioreg_edge_mask(struct mvebu_gpio_chip *mvchip)
  116. {
  117. int cpu;
  118. switch(mvchip->soc_variant) {
  119. case MVEBU_GPIO_SOC_VARIANT_ORION:
  120. return mvchip->membase + GPIO_EDGE_MASK_OFF;
  121. case MVEBU_GPIO_SOC_VARIANT_MV78200:
  122. cpu = smp_processor_id();
  123. return mvchip->membase + GPIO_EDGE_MASK_MV78200_OFF(cpu);
  124. case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
  125. cpu = smp_processor_id();
  126. return mvchip->percpu_membase + GPIO_EDGE_MASK_ARMADAXP_OFF(cpu);
  127. default:
  128. BUG();
  129. }
  130. }
  131. static void __iomem *mvebu_gpioreg_level_mask(struct mvebu_gpio_chip *mvchip)
  132. {
  133. int cpu;
  134. switch(mvchip->soc_variant) {
  135. case MVEBU_GPIO_SOC_VARIANT_ORION:
  136. return mvchip->membase + GPIO_LEVEL_MASK_OFF;
  137. case MVEBU_GPIO_SOC_VARIANT_MV78200:
  138. cpu = smp_processor_id();
  139. return mvchip->membase + GPIO_LEVEL_MASK_MV78200_OFF(cpu);
  140. case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
  141. cpu = smp_processor_id();
  142. return mvchip->percpu_membase + GPIO_LEVEL_MASK_ARMADAXP_OFF(cpu);
  143. default:
  144. BUG();
  145. }
  146. }
  147. /*
  148. * Functions implementing the gpio_chip methods
  149. */
  150. static int mvebu_gpio_request(struct gpio_chip *chip, unsigned pin)
  151. {
  152. return pinctrl_request_gpio(chip->base + pin);
  153. }
  154. static void mvebu_gpio_free(struct gpio_chip *chip, unsigned pin)
  155. {
  156. pinctrl_free_gpio(chip->base + pin);
  157. }
  158. static void mvebu_gpio_set(struct gpio_chip *chip, unsigned pin, int value)
  159. {
  160. struct mvebu_gpio_chip *mvchip =
  161. container_of(chip, struct mvebu_gpio_chip, chip);
  162. unsigned long flags;
  163. u32 u;
  164. spin_lock_irqsave(&mvchip->lock, flags);
  165. u = readl_relaxed(mvebu_gpioreg_out(mvchip));
  166. if (value)
  167. u |= 1 << pin;
  168. else
  169. u &= ~(1 << pin);
  170. writel_relaxed(u, mvebu_gpioreg_out(mvchip));
  171. spin_unlock_irqrestore(&mvchip->lock, flags);
  172. }
  173. static int mvebu_gpio_get(struct gpio_chip *chip, unsigned pin)
  174. {
  175. struct mvebu_gpio_chip *mvchip =
  176. container_of(chip, struct mvebu_gpio_chip, chip);
  177. u32 u;
  178. if (readl_relaxed(mvebu_gpioreg_io_conf(mvchip)) & (1 << pin)) {
  179. u = readl_relaxed(mvebu_gpioreg_data_in(mvchip)) ^
  180. readl_relaxed(mvebu_gpioreg_in_pol(mvchip));
  181. } else {
  182. u = readl_relaxed(mvebu_gpioreg_out(mvchip));
  183. }
  184. return (u >> pin) & 1;
  185. }
  186. static void mvebu_gpio_blink(struct gpio_chip *chip, unsigned pin, int value)
  187. {
  188. struct mvebu_gpio_chip *mvchip =
  189. container_of(chip, struct mvebu_gpio_chip, chip);
  190. unsigned long flags;
  191. u32 u;
  192. spin_lock_irqsave(&mvchip->lock, flags);
  193. u = readl_relaxed(mvebu_gpioreg_blink(mvchip));
  194. if (value)
  195. u |= 1 << pin;
  196. else
  197. u &= ~(1 << pin);
  198. writel_relaxed(u, mvebu_gpioreg_blink(mvchip));
  199. spin_unlock_irqrestore(&mvchip->lock, flags);
  200. }
  201. static int mvebu_gpio_direction_input(struct gpio_chip *chip, unsigned pin)
  202. {
  203. struct mvebu_gpio_chip *mvchip =
  204. container_of(chip, struct mvebu_gpio_chip, chip);
  205. unsigned long flags;
  206. int ret;
  207. u32 u;
  208. /* Check with the pinctrl driver whether this pin is usable as
  209. * an input GPIO */
  210. ret = pinctrl_gpio_direction_input(chip->base + pin);
  211. if (ret)
  212. return ret;
  213. spin_lock_irqsave(&mvchip->lock, flags);
  214. u = readl_relaxed(mvebu_gpioreg_io_conf(mvchip));
  215. u |= 1 << pin;
  216. writel_relaxed(u, mvebu_gpioreg_io_conf(mvchip));
  217. spin_unlock_irqrestore(&mvchip->lock, flags);
  218. return 0;
  219. }
  220. static int mvebu_gpio_direction_output(struct gpio_chip *chip, unsigned pin,
  221. int value)
  222. {
  223. struct mvebu_gpio_chip *mvchip =
  224. container_of(chip, struct mvebu_gpio_chip, chip);
  225. unsigned long flags;
  226. int ret;
  227. u32 u;
  228. /* Check with the pinctrl driver whether this pin is usable as
  229. * an output GPIO */
  230. ret = pinctrl_gpio_direction_output(chip->base + pin);
  231. if (ret)
  232. return ret;
  233. mvebu_gpio_blink(chip, pin, 0);
  234. mvebu_gpio_set(chip, pin, value);
  235. spin_lock_irqsave(&mvchip->lock, flags);
  236. u = readl_relaxed(mvebu_gpioreg_io_conf(mvchip));
  237. u &= ~(1 << pin);
  238. writel_relaxed(u, mvebu_gpioreg_io_conf(mvchip));
  239. spin_unlock_irqrestore(&mvchip->lock, flags);
  240. return 0;
  241. }
  242. static int mvebu_gpio_to_irq(struct gpio_chip *chip, unsigned pin)
  243. {
  244. struct mvebu_gpio_chip *mvchip =
  245. container_of(chip, struct mvebu_gpio_chip, chip);
  246. return irq_create_mapping(mvchip->domain, pin);
  247. }
  248. /*
  249. * Functions implementing the irq_chip methods
  250. */
  251. static void mvebu_gpio_irq_ack(struct irq_data *d)
  252. {
  253. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  254. struct mvebu_gpio_chip *mvchip = gc->private;
  255. u32 mask = ~(1 << (d->irq - gc->irq_base));
  256. irq_gc_lock(gc);
  257. writel_relaxed(mask, mvebu_gpioreg_edge_cause(mvchip));
  258. irq_gc_unlock(gc);
  259. }
  260. static void mvebu_gpio_edge_irq_mask(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. gc->mask_cache &= ~mask;
  267. writel_relaxed(gc->mask_cache, mvebu_gpioreg_edge_mask(mvchip));
  268. irq_gc_unlock(gc);
  269. }
  270. static void mvebu_gpio_edge_irq_unmask(struct irq_data *d)
  271. {
  272. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  273. struct mvebu_gpio_chip *mvchip = gc->private;
  274. u32 mask = 1 << (d->irq - gc->irq_base);
  275. irq_gc_lock(gc);
  276. gc->mask_cache |= mask;
  277. writel_relaxed(gc->mask_cache, mvebu_gpioreg_edge_mask(mvchip));
  278. irq_gc_unlock(gc);
  279. }
  280. static void mvebu_gpio_level_irq_mask(struct irq_data *d)
  281. {
  282. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  283. struct mvebu_gpio_chip *mvchip = gc->private;
  284. u32 mask = 1 << (d->irq - gc->irq_base);
  285. irq_gc_lock(gc);
  286. gc->mask_cache &= ~mask;
  287. writel_relaxed(gc->mask_cache, mvebu_gpioreg_level_mask(mvchip));
  288. irq_gc_unlock(gc);
  289. }
  290. static void mvebu_gpio_level_irq_unmask(struct irq_data *d)
  291. {
  292. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  293. struct mvebu_gpio_chip *mvchip = gc->private;
  294. u32 mask = 1 << (d->irq - gc->irq_base);
  295. irq_gc_lock(gc);
  296. gc->mask_cache |= mask;
  297. writel_relaxed(gc->mask_cache, mvebu_gpioreg_level_mask(mvchip));
  298. irq_gc_unlock(gc);
  299. }
  300. /*****************************************************************************
  301. * MVEBU GPIO IRQ
  302. *
  303. * GPIO_IN_POL register controls whether GPIO_DATA_IN will hold the same
  304. * value of the line or the opposite value.
  305. *
  306. * Level IRQ handlers: DATA_IN is used directly as cause register.
  307. * Interrupt are masked by LEVEL_MASK registers.
  308. * Edge IRQ handlers: Change in DATA_IN are latched in EDGE_CAUSE.
  309. * Interrupt are masked by EDGE_MASK registers.
  310. * Both-edge handlers: Similar to regular Edge handlers, but also swaps
  311. * the polarity to catch the next line transaction.
  312. * This is a race condition that might not perfectly
  313. * work on some use cases.
  314. *
  315. * Every eight GPIO lines are grouped (OR'ed) before going up to main
  316. * cause register.
  317. *
  318. * EDGE cause mask
  319. * data-in /--------| |-----| |----\
  320. * -----| |----- ---- to main cause reg
  321. * X \----------------| |----/
  322. * polarity LEVEL mask
  323. *
  324. ****************************************************************************/
  325. static int mvebu_gpio_irq_set_type(struct irq_data *d, unsigned int type)
  326. {
  327. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  328. struct irq_chip_type *ct = irq_data_get_chip_type(d);
  329. struct mvebu_gpio_chip *mvchip = gc->private;
  330. int pin;
  331. u32 u;
  332. pin = d->hwirq;
  333. u = readl_relaxed(mvebu_gpioreg_io_conf(mvchip)) & (1 << pin);
  334. if (!u) {
  335. return -EINVAL;
  336. }
  337. type &= IRQ_TYPE_SENSE_MASK;
  338. if (type == IRQ_TYPE_NONE)
  339. return -EINVAL;
  340. /* Check if we need to change chip and handler */
  341. if (!(ct->type & type))
  342. if (irq_setup_alt_chip(d, type))
  343. return -EINVAL;
  344. /*
  345. * Configure interrupt polarity.
  346. */
  347. switch(type) {
  348. case IRQ_TYPE_EDGE_RISING:
  349. case IRQ_TYPE_LEVEL_HIGH:
  350. u = readl_relaxed(mvebu_gpioreg_in_pol(mvchip));
  351. u &= ~(1 << pin);
  352. writel_relaxed(u, mvebu_gpioreg_in_pol(mvchip));
  353. break;
  354. case IRQ_TYPE_EDGE_FALLING:
  355. case IRQ_TYPE_LEVEL_LOW:
  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_BOTH: {
  361. u32 v;
  362. v = readl_relaxed(mvebu_gpioreg_in_pol(mvchip)) ^
  363. readl_relaxed(mvebu_gpioreg_data_in(mvchip));
  364. /*
  365. * set initial polarity based on current input level
  366. */
  367. u = readl_relaxed(mvebu_gpioreg_in_pol(mvchip));
  368. if (v & (1 << pin))
  369. u |= 1 << pin; /* falling */
  370. else
  371. u &= ~(1 << pin); /* rising */
  372. writel_relaxed(u, mvebu_gpioreg_in_pol(mvchip));
  373. break;
  374. }
  375. }
  376. return 0;
  377. }
  378. static void mvebu_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
  379. {
  380. struct mvebu_gpio_chip *mvchip = irq_get_handler_data(irq);
  381. u32 cause, type;
  382. int i;
  383. if (mvchip == NULL)
  384. return;
  385. cause = readl_relaxed(mvebu_gpioreg_data_in(mvchip)) &
  386. readl_relaxed(mvebu_gpioreg_level_mask(mvchip));
  387. cause |= readl_relaxed(mvebu_gpioreg_edge_cause(mvchip)) &
  388. readl_relaxed(mvebu_gpioreg_edge_mask(mvchip));
  389. for (i = 0; i < mvchip->chip.ngpio; i++) {
  390. int irq;
  391. irq = mvchip->irqbase + i;
  392. if (!(cause & (1 << i)))
  393. continue;
  394. type = irqd_get_trigger_type(irq_get_irq_data(irq));
  395. if ((type & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH) {
  396. /* Swap polarity (race with GPIO line) */
  397. u32 polarity;
  398. polarity = readl_relaxed(mvebu_gpioreg_in_pol(mvchip));
  399. polarity ^= 1 << i;
  400. writel_relaxed(polarity, mvebu_gpioreg_in_pol(mvchip));
  401. }
  402. generic_handle_irq(irq);
  403. }
  404. }
  405. static struct of_device_id mvebu_gpio_of_match[] = {
  406. {
  407. .compatible = "marvell,orion-gpio",
  408. .data = (void*) MVEBU_GPIO_SOC_VARIANT_ORION,
  409. },
  410. {
  411. .compatible = "marvell,mv78200-gpio",
  412. .data = (void*) MVEBU_GPIO_SOC_VARIANT_MV78200,
  413. },
  414. {
  415. .compatible = "marvell,armadaxp-gpio",
  416. .data = (void*) MVEBU_GPIO_SOC_VARIANT_ARMADAXP,
  417. },
  418. {
  419. /* sentinel */
  420. },
  421. };
  422. MODULE_DEVICE_TABLE(of, mvebu_gpio_of_match);
  423. static int mvebu_gpio_probe(struct platform_device *pdev)
  424. {
  425. struct mvebu_gpio_chip *mvchip;
  426. const struct of_device_id *match;
  427. struct device_node *np = pdev->dev.of_node;
  428. struct resource *res;
  429. struct irq_chip_generic *gc;
  430. struct irq_chip_type *ct;
  431. unsigned int ngpios;
  432. int soc_variant;
  433. int i, cpu, id;
  434. match = of_match_device(mvebu_gpio_of_match, &pdev->dev);
  435. if (match)
  436. soc_variant = (int) match->data;
  437. else
  438. soc_variant = MVEBU_GPIO_SOC_VARIANT_ORION;
  439. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  440. if (! res) {
  441. dev_err(&pdev->dev, "Cannot get memory resource\n");
  442. return -ENODEV;
  443. }
  444. mvchip = devm_kzalloc(&pdev->dev, sizeof(struct mvebu_gpio_chip), GFP_KERNEL);
  445. if (! mvchip){
  446. dev_err(&pdev->dev, "Cannot allocate memory\n");
  447. return -ENOMEM;
  448. }
  449. if (of_property_read_u32(pdev->dev.of_node, "ngpios", &ngpios)) {
  450. dev_err(&pdev->dev, "Missing ngpios OF property\n");
  451. return -ENODEV;
  452. }
  453. id = of_alias_get_id(pdev->dev.of_node, "gpio");
  454. if (id < 0) {
  455. dev_err(&pdev->dev, "Couldn't get OF id\n");
  456. return id;
  457. }
  458. mvchip->soc_variant = soc_variant;
  459. mvchip->chip.label = dev_name(&pdev->dev);
  460. mvchip->chip.dev = &pdev->dev;
  461. mvchip->chip.request = mvebu_gpio_request;
  462. mvchip->chip.free = mvebu_gpio_free;
  463. mvchip->chip.direction_input = mvebu_gpio_direction_input;
  464. mvchip->chip.get = mvebu_gpio_get;
  465. mvchip->chip.direction_output = mvebu_gpio_direction_output;
  466. mvchip->chip.set = mvebu_gpio_set;
  467. mvchip->chip.to_irq = mvebu_gpio_to_irq;
  468. mvchip->chip.base = id * MVEBU_MAX_GPIO_PER_BANK;
  469. mvchip->chip.ngpio = ngpios;
  470. mvchip->chip.can_sleep = 0;
  471. mvchip->chip.of_node = np;
  472. spin_lock_init(&mvchip->lock);
  473. mvchip->membase = devm_request_and_ioremap(&pdev->dev, res);
  474. if (! mvchip->membase) {
  475. dev_err(&pdev->dev, "Cannot ioremap\n");
  476. kfree(mvchip->chip.label);
  477. return -ENOMEM;
  478. }
  479. /* The Armada XP has a second range of registers for the
  480. * per-CPU registers */
  481. if (soc_variant == MVEBU_GPIO_SOC_VARIANT_ARMADAXP) {
  482. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  483. if (! res) {
  484. dev_err(&pdev->dev, "Cannot get memory resource\n");
  485. kfree(mvchip->chip.label);
  486. return -ENODEV;
  487. }
  488. mvchip->percpu_membase = devm_request_and_ioremap(&pdev->dev, res);
  489. if (! mvchip->percpu_membase) {
  490. dev_err(&pdev->dev, "Cannot ioremap\n");
  491. kfree(mvchip->chip.label);
  492. return -ENOMEM;
  493. }
  494. }
  495. /*
  496. * Mask and clear GPIO interrupts.
  497. */
  498. switch(soc_variant) {
  499. case MVEBU_GPIO_SOC_VARIANT_ORION:
  500. writel_relaxed(0, mvchip->membase + GPIO_EDGE_CAUSE_OFF);
  501. writel_relaxed(0, mvchip->membase + GPIO_EDGE_MASK_OFF);
  502. writel_relaxed(0, mvchip->membase + GPIO_LEVEL_MASK_OFF);
  503. break;
  504. case MVEBU_GPIO_SOC_VARIANT_MV78200:
  505. writel_relaxed(0, mvchip->membase + GPIO_EDGE_CAUSE_OFF);
  506. for (cpu = 0; cpu < 2; cpu++) {
  507. writel_relaxed(0, mvchip->membase +
  508. GPIO_EDGE_MASK_MV78200_OFF(cpu));
  509. writel_relaxed(0, mvchip->membase +
  510. GPIO_LEVEL_MASK_MV78200_OFF(cpu));
  511. }
  512. break;
  513. case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
  514. writel_relaxed(0, mvchip->membase + GPIO_EDGE_CAUSE_OFF);
  515. writel_relaxed(0, mvchip->membase + GPIO_EDGE_MASK_OFF);
  516. writel_relaxed(0, mvchip->membase + GPIO_LEVEL_MASK_OFF);
  517. for (cpu = 0; cpu < 4; cpu++) {
  518. writel_relaxed(0, mvchip->percpu_membase +
  519. GPIO_EDGE_CAUSE_ARMADAXP_OFF(cpu));
  520. writel_relaxed(0, mvchip->percpu_membase +
  521. GPIO_EDGE_MASK_ARMADAXP_OFF(cpu));
  522. writel_relaxed(0, mvchip->percpu_membase +
  523. GPIO_LEVEL_MASK_ARMADAXP_OFF(cpu));
  524. }
  525. break;
  526. default:
  527. BUG();
  528. }
  529. gpiochip_add(&mvchip->chip);
  530. /* Some gpio controllers do not provide irq support */
  531. if (!of_irq_count(np))
  532. return 0;
  533. /* Setup the interrupt handlers. Each chip can have up to 4
  534. * interrupt handlers, with each handler dealing with 8 GPIO
  535. * pins. */
  536. for (i = 0; i < 4; i++) {
  537. int irq;
  538. irq = platform_get_irq(pdev, i);
  539. if (irq < 0)
  540. continue;
  541. irq_set_handler_data(irq, mvchip);
  542. irq_set_chained_handler(irq, mvebu_gpio_irq_handler);
  543. }
  544. mvchip->irqbase = irq_alloc_descs(-1, 0, ngpios, -1);
  545. if (mvchip->irqbase < 0) {
  546. dev_err(&pdev->dev, "no irqs\n");
  547. kfree(mvchip->chip.label);
  548. return -ENOMEM;
  549. }
  550. gc = irq_alloc_generic_chip("mvebu_gpio_irq", 2, mvchip->irqbase,
  551. mvchip->membase, handle_level_irq);
  552. if (! gc) {
  553. dev_err(&pdev->dev, "Cannot allocate generic irq_chip\n");
  554. kfree(mvchip->chip.label);
  555. return -ENOMEM;
  556. }
  557. gc->private = mvchip;
  558. ct = &gc->chip_types[0];
  559. ct->type = IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW;
  560. ct->chip.irq_mask = mvebu_gpio_level_irq_mask;
  561. ct->chip.irq_unmask = mvebu_gpio_level_irq_unmask;
  562. ct->chip.irq_set_type = mvebu_gpio_irq_set_type;
  563. ct->chip.name = mvchip->chip.label;
  564. ct = &gc->chip_types[1];
  565. ct->type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
  566. ct->chip.irq_ack = mvebu_gpio_irq_ack;
  567. ct->chip.irq_mask = mvebu_gpio_edge_irq_mask;
  568. ct->chip.irq_unmask = mvebu_gpio_edge_irq_unmask;
  569. ct->chip.irq_set_type = mvebu_gpio_irq_set_type;
  570. ct->handler = handle_edge_irq;
  571. ct->chip.name = mvchip->chip.label;
  572. irq_setup_generic_chip(gc, IRQ_MSK(ngpios), 0,
  573. IRQ_NOREQUEST, IRQ_LEVEL | IRQ_NOPROBE);
  574. /* Setup irq domain on top of the generic chip. */
  575. mvchip->domain = irq_domain_add_simple(np, mvchip->chip.ngpio,
  576. mvchip->irqbase,
  577. &irq_domain_simple_ops,
  578. mvchip);
  579. if (!mvchip->domain) {
  580. dev_err(&pdev->dev, "couldn't allocate irq domain %s (DT).\n",
  581. mvchip->chip.label);
  582. irq_remove_generic_chip(gc, IRQ_MSK(ngpios), IRQ_NOREQUEST,
  583. IRQ_LEVEL | IRQ_NOPROBE);
  584. kfree(gc);
  585. kfree(mvchip->chip.label);
  586. return -ENODEV;
  587. }
  588. return 0;
  589. }
  590. static struct platform_driver mvebu_gpio_driver = {
  591. .driver = {
  592. .name = "mvebu-gpio",
  593. .owner = THIS_MODULE,
  594. .of_match_table = mvebu_gpio_of_match,
  595. },
  596. .probe = mvebu_gpio_probe,
  597. };
  598. static int __init mvebu_gpio_init(void)
  599. {
  600. return platform_driver_register(&mvebu_gpio_driver);
  601. }
  602. postcore_initcall(mvebu_gpio_init);