gpio-mvebu.c 19 KB

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