gpio-mvebu.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  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. spin_lock_irqsave(&mvchip->lock, flags);
  216. u = readl_relaxed(mvebu_gpioreg_io_conf(mvchip));
  217. u &= ~(1 << pin);
  218. writel_relaxed(u, mvebu_gpioreg_io_conf(mvchip));
  219. spin_unlock_irqrestore(&mvchip->lock, flags);
  220. return 0;
  221. }
  222. static int mvebu_gpio_to_irq(struct gpio_chip *chip, unsigned pin)
  223. {
  224. struct mvebu_gpio_chip *mvchip =
  225. container_of(chip, struct mvebu_gpio_chip, chip);
  226. return irq_create_mapping(mvchip->domain, pin);
  227. }
  228. /*
  229. * Functions implementing the irq_chip methods
  230. */
  231. static void mvebu_gpio_irq_ack(struct irq_data *d)
  232. {
  233. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  234. struct mvebu_gpio_chip *mvchip = gc->private;
  235. u32 mask = ~(1 << (d->irq - gc->irq_base));
  236. irq_gc_lock(gc);
  237. writel_relaxed(mask, mvebu_gpioreg_edge_cause(mvchip));
  238. irq_gc_unlock(gc);
  239. }
  240. static void mvebu_gpio_edge_irq_mask(struct irq_data *d)
  241. {
  242. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  243. struct mvebu_gpio_chip *mvchip = gc->private;
  244. u32 mask = 1 << (d->irq - gc->irq_base);
  245. irq_gc_lock(gc);
  246. gc->mask_cache &= ~mask;
  247. writel_relaxed(gc->mask_cache, mvebu_gpioreg_edge_mask(mvchip));
  248. irq_gc_unlock(gc);
  249. }
  250. static void mvebu_gpio_edge_irq_unmask(struct irq_data *d)
  251. {
  252. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  253. struct mvebu_gpio_chip *mvchip = gc->private;
  254. u32 mask = 1 << (d->irq - gc->irq_base);
  255. irq_gc_lock(gc);
  256. gc->mask_cache |= mask;
  257. writel_relaxed(gc->mask_cache, mvebu_gpioreg_edge_mask(mvchip));
  258. irq_gc_unlock(gc);
  259. }
  260. static void mvebu_gpio_level_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_level_mask(mvchip));
  268. irq_gc_unlock(gc);
  269. }
  270. static void mvebu_gpio_level_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_level_mask(mvchip));
  278. irq_gc_unlock(gc);
  279. }
  280. /*****************************************************************************
  281. * MVEBU GPIO IRQ
  282. *
  283. * GPIO_IN_POL register controls whether GPIO_DATA_IN will hold the same
  284. * value of the line or the opposite value.
  285. *
  286. * Level IRQ handlers: DATA_IN is used directly as cause register.
  287. * Interrupt are masked by LEVEL_MASK registers.
  288. * Edge IRQ handlers: Change in DATA_IN are latched in EDGE_CAUSE.
  289. * Interrupt are masked by EDGE_MASK registers.
  290. * Both-edge handlers: Similar to regular Edge handlers, but also swaps
  291. * the polarity to catch the next line transaction.
  292. * This is a race condition that might not perfectly
  293. * work on some use cases.
  294. *
  295. * Every eight GPIO lines are grouped (OR'ed) before going up to main
  296. * cause register.
  297. *
  298. * EDGE cause mask
  299. * data-in /--------| |-----| |----\
  300. * -----| |----- ---- to main cause reg
  301. * X \----------------| |----/
  302. * polarity LEVEL mask
  303. *
  304. ****************************************************************************/
  305. static int mvebu_gpio_irq_set_type(struct irq_data *d, unsigned int type)
  306. {
  307. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  308. struct irq_chip_type *ct = irq_data_get_chip_type(d);
  309. struct mvebu_gpio_chip *mvchip = gc->private;
  310. int pin;
  311. u32 u;
  312. pin = d->hwirq;
  313. u = readl_relaxed(mvebu_gpioreg_io_conf(mvchip)) & (1 << pin);
  314. if (!u) {
  315. return -EINVAL;
  316. }
  317. type &= IRQ_TYPE_SENSE_MASK;
  318. if (type == IRQ_TYPE_NONE)
  319. return -EINVAL;
  320. /* Check if we need to change chip and handler */
  321. if (!(ct->type & type))
  322. if (irq_setup_alt_chip(d, type))
  323. return -EINVAL;
  324. /*
  325. * Configure interrupt polarity.
  326. */
  327. switch(type) {
  328. case IRQ_TYPE_EDGE_RISING:
  329. case IRQ_TYPE_LEVEL_HIGH:
  330. u = readl_relaxed(mvebu_gpioreg_in_pol(mvchip));
  331. u &= ~(1 << pin);
  332. writel_relaxed(u, mvebu_gpioreg_in_pol(mvchip));
  333. break;
  334. case IRQ_TYPE_EDGE_FALLING:
  335. case IRQ_TYPE_LEVEL_LOW:
  336. u = readl_relaxed(mvebu_gpioreg_in_pol(mvchip));
  337. u |= 1 << pin;
  338. writel_relaxed(u, mvebu_gpioreg_in_pol(mvchip));
  339. break;
  340. case IRQ_TYPE_EDGE_BOTH: {
  341. u32 v;
  342. v = readl_relaxed(mvebu_gpioreg_in_pol(mvchip)) ^
  343. readl_relaxed(mvebu_gpioreg_data_in(mvchip));
  344. /*
  345. * set initial polarity based on current input level
  346. */
  347. u = readl_relaxed(mvebu_gpioreg_in_pol(mvchip));
  348. if (v & (1 << pin))
  349. u |= 1 << pin; /* falling */
  350. else
  351. u &= ~(1 << pin); /* rising */
  352. writel_relaxed(u, mvebu_gpioreg_in_pol(mvchip));
  353. break;
  354. }
  355. }
  356. return 0;
  357. }
  358. static void mvebu_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
  359. {
  360. struct mvebu_gpio_chip *mvchip = irq_get_handler_data(irq);
  361. u32 cause, type;
  362. int i;
  363. if (mvchip == NULL)
  364. return;
  365. cause = readl_relaxed(mvebu_gpioreg_data_in(mvchip)) &
  366. readl_relaxed(mvebu_gpioreg_level_mask(mvchip));
  367. cause |= readl_relaxed(mvebu_gpioreg_edge_cause(mvchip)) &
  368. readl_relaxed(mvebu_gpioreg_edge_mask(mvchip));
  369. for (i = 0; i < mvchip->chip.ngpio; i++) {
  370. int irq;
  371. irq = mvchip->irqbase + i;
  372. if (!(cause & (1 << i)))
  373. continue;
  374. type = irqd_get_trigger_type(irq_get_irq_data(irq));
  375. if ((type & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH) {
  376. /* Swap polarity (race with GPIO line) */
  377. u32 polarity;
  378. polarity = readl_relaxed(mvebu_gpioreg_in_pol(mvchip));
  379. polarity ^= 1 << i;
  380. writel_relaxed(polarity, mvebu_gpioreg_in_pol(mvchip));
  381. }
  382. generic_handle_irq(irq);
  383. }
  384. }
  385. static struct platform_device_id mvebu_gpio_ids[] = {
  386. {
  387. .name = "orion-gpio",
  388. }, {
  389. .name = "mv78200-gpio",
  390. }, {
  391. .name = "armadaxp-gpio",
  392. }, {
  393. /* sentinel */
  394. },
  395. };
  396. MODULE_DEVICE_TABLE(platform, mvebu_gpio_ids);
  397. static struct of_device_id mvebu_gpio_of_match[] __devinitdata = {
  398. {
  399. .compatible = "marvell,orion-gpio",
  400. .data = (void*) MVEBU_GPIO_SOC_VARIANT_ORION,
  401. },
  402. {
  403. .compatible = "marvell,mv78200-gpio",
  404. .data = (void*) MVEBU_GPIO_SOC_VARIANT_MV78200,
  405. },
  406. {
  407. .compatible = "marvell,armadaxp-gpio",
  408. .data = (void*) MVEBU_GPIO_SOC_VARIANT_ARMADAXP,
  409. },
  410. {
  411. /* sentinel */
  412. },
  413. };
  414. MODULE_DEVICE_TABLE(of, mvebu_gpio_of_match);
  415. static int __devinit mvebu_gpio_probe(struct platform_device *pdev)
  416. {
  417. struct mvebu_gpio_chip *mvchip;
  418. const struct of_device_id *match;
  419. struct device_node *np = pdev->dev.of_node;
  420. struct resource *res;
  421. struct irq_chip_generic *gc;
  422. struct irq_chip_type *ct;
  423. unsigned int ngpios;
  424. int soc_variant;
  425. int i, cpu, id;
  426. match = of_match_device(mvebu_gpio_of_match, &pdev->dev);
  427. if (match)
  428. soc_variant = (int) match->data;
  429. else
  430. soc_variant = MVEBU_GPIO_SOC_VARIANT_ORION;
  431. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  432. if (! res) {
  433. dev_err(&pdev->dev, "Cannot get memory resource\n");
  434. return -ENODEV;
  435. }
  436. mvchip = devm_kzalloc(&pdev->dev, sizeof(struct mvebu_gpio_chip), GFP_KERNEL);
  437. if (! mvchip){
  438. dev_err(&pdev->dev, "Cannot allocate memory\n");
  439. return -ENOMEM;
  440. }
  441. if (of_property_read_u32(pdev->dev.of_node, "ngpios", &ngpios)) {
  442. dev_err(&pdev->dev, "Missing ngpios OF property\n");
  443. return -ENODEV;
  444. }
  445. id = of_alias_get_id(pdev->dev.of_node, "gpio");
  446. if (id < 0) {
  447. dev_err(&pdev->dev, "Couldn't get OF id\n");
  448. return id;
  449. }
  450. mvchip->soc_variant = soc_variant;
  451. mvchip->chip.label = dev_name(&pdev->dev);
  452. mvchip->chip.dev = &pdev->dev;
  453. mvchip->chip.request = mvebu_gpio_request;
  454. mvchip->chip.direction_input = mvebu_gpio_direction_input;
  455. mvchip->chip.get = mvebu_gpio_get;
  456. mvchip->chip.direction_output = mvebu_gpio_direction_output;
  457. mvchip->chip.set = mvebu_gpio_set;
  458. mvchip->chip.to_irq = mvebu_gpio_to_irq;
  459. mvchip->chip.base = id * MVEBU_MAX_GPIO_PER_BANK;
  460. mvchip->chip.ngpio = ngpios;
  461. mvchip->chip.can_sleep = 0;
  462. #ifdef CONFIG_OF
  463. mvchip->chip.of_node = np;
  464. #endif
  465. spin_lock_init(&mvchip->lock);
  466. mvchip->membase = devm_request_and_ioremap(&pdev->dev, res);
  467. if (! mvchip->membase) {
  468. dev_err(&pdev->dev, "Cannot ioremap\n");
  469. kfree(mvchip->chip.label);
  470. return -ENOMEM;
  471. }
  472. /* The Armada XP has a second range of registers for the
  473. * per-CPU registers */
  474. if (soc_variant == MVEBU_GPIO_SOC_VARIANT_ARMADAXP) {
  475. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  476. if (! res) {
  477. dev_err(&pdev->dev, "Cannot get memory resource\n");
  478. kfree(mvchip->chip.label);
  479. return -ENODEV;
  480. }
  481. mvchip->percpu_membase = devm_request_and_ioremap(&pdev->dev, res);
  482. if (! mvchip->percpu_membase) {
  483. dev_err(&pdev->dev, "Cannot ioremap\n");
  484. kfree(mvchip->chip.label);
  485. return -ENOMEM;
  486. }
  487. }
  488. /*
  489. * Mask and clear GPIO interrupts.
  490. */
  491. switch(soc_variant) {
  492. case MVEBU_GPIO_SOC_VARIANT_ORION:
  493. writel_relaxed(0, mvchip->membase + GPIO_EDGE_CAUSE_OFF);
  494. writel_relaxed(0, mvchip->membase + GPIO_EDGE_MASK_OFF);
  495. writel_relaxed(0, mvchip->membase + GPIO_LEVEL_MASK_OFF);
  496. break;
  497. case MVEBU_GPIO_SOC_VARIANT_MV78200:
  498. writel_relaxed(0, mvchip->membase + GPIO_EDGE_CAUSE_OFF);
  499. for (cpu = 0; cpu < 2; cpu++) {
  500. writel_relaxed(0, mvchip->membase +
  501. GPIO_EDGE_MASK_MV78200_OFF(cpu));
  502. writel_relaxed(0, mvchip->membase +
  503. GPIO_LEVEL_MASK_MV78200_OFF(cpu));
  504. }
  505. break;
  506. case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
  507. writel_relaxed(0, mvchip->membase + GPIO_EDGE_CAUSE_OFF);
  508. writel_relaxed(0, mvchip->membase + GPIO_EDGE_MASK_OFF);
  509. writel_relaxed(0, mvchip->membase + GPIO_LEVEL_MASK_OFF);
  510. for (cpu = 0; cpu < 4; cpu++) {
  511. writel_relaxed(0, mvchip->percpu_membase +
  512. GPIO_EDGE_CAUSE_ARMADAXP_OFF(cpu));
  513. writel_relaxed(0, mvchip->percpu_membase +
  514. GPIO_EDGE_MASK_ARMADAXP_OFF(cpu));
  515. writel_relaxed(0, mvchip->percpu_membase +
  516. GPIO_LEVEL_MASK_ARMADAXP_OFF(cpu));
  517. }
  518. break;
  519. default:
  520. BUG();
  521. }
  522. gpiochip_add(&mvchip->chip);
  523. /* Some gpio controllers do not provide irq support */
  524. if (!of_irq_count(np))
  525. return 0;
  526. /* Setup the interrupt handlers. Each chip can have up to 4
  527. * interrupt handlers, with each handler dealing with 8 GPIO
  528. * pins. */
  529. for (i = 0; i < 4; i++) {
  530. int irq;
  531. irq = platform_get_irq(pdev, i);
  532. if (irq < 0)
  533. continue;
  534. irq_set_handler_data(irq, mvchip);
  535. irq_set_chained_handler(irq, mvebu_gpio_irq_handler);
  536. }
  537. mvchip->irqbase = irq_alloc_descs(-1, 0, ngpios, -1);
  538. if (mvchip->irqbase < 0) {
  539. dev_err(&pdev->dev, "no irqs\n");
  540. kfree(mvchip->chip.label);
  541. return -ENOMEM;
  542. }
  543. gc = irq_alloc_generic_chip("mvebu_gpio_irq", 2, mvchip->irqbase,
  544. mvchip->membase, handle_level_irq);
  545. if (! gc) {
  546. dev_err(&pdev->dev, "Cannot allocate generic irq_chip\n");
  547. kfree(mvchip->chip.label);
  548. return -ENOMEM;
  549. }
  550. gc->private = mvchip;
  551. ct = &gc->chip_types[0];
  552. ct->type = IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW;
  553. ct->chip.irq_mask = mvebu_gpio_level_irq_mask;
  554. ct->chip.irq_unmask = mvebu_gpio_level_irq_unmask;
  555. ct->chip.irq_set_type = mvebu_gpio_irq_set_type;
  556. ct->chip.name = mvchip->chip.label;
  557. ct = &gc->chip_types[1];
  558. ct->type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
  559. ct->chip.irq_ack = mvebu_gpio_irq_ack;
  560. ct->chip.irq_mask = mvebu_gpio_edge_irq_mask;
  561. ct->chip.irq_unmask = mvebu_gpio_edge_irq_unmask;
  562. ct->chip.irq_set_type = mvebu_gpio_irq_set_type;
  563. ct->handler = handle_edge_irq;
  564. ct->chip.name = mvchip->chip.label;
  565. irq_setup_generic_chip(gc, IRQ_MSK(ngpios), IRQ_GC_INIT_MASK_CACHE,
  566. IRQ_NOREQUEST, IRQ_LEVEL | IRQ_NOPROBE);
  567. /* Setup irq domain on top of the generic chip. */
  568. mvchip->domain = irq_domain_add_legacy(np, mvchip->chip.ngpio,
  569. mvchip->irqbase, 0,
  570. &irq_domain_simple_ops,
  571. mvchip);
  572. if (!mvchip->domain) {
  573. dev_err(&pdev->dev, "couldn't allocate irq domain %s (DT).\n",
  574. mvchip->chip.label);
  575. irq_remove_generic_chip(gc, IRQ_MSK(ngpios), IRQ_NOREQUEST,
  576. IRQ_LEVEL | IRQ_NOPROBE);
  577. kfree(gc);
  578. kfree(mvchip->chip.label);
  579. return -ENODEV;
  580. }
  581. return 0;
  582. }
  583. static struct platform_driver mvebu_gpio_driver = {
  584. .driver = {
  585. .name = "mvebu-gpio",
  586. .owner = THIS_MODULE,
  587. .of_match_table = mvebu_gpio_of_match,
  588. },
  589. .probe = mvebu_gpio_probe,
  590. .id_table = mvebu_gpio_ids,
  591. };
  592. static int __init mvebu_gpio_init(void)
  593. {
  594. return platform_driver_register(&mvebu_gpio_driver);
  595. }
  596. postcore_initcall(mvebu_gpio_init);