gpio-mvebu.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  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. 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. case IRQ_TYPE_EDGE_BOTH: {
  340. u32 v;
  341. v = readl_relaxed(mvebu_gpioreg_in_pol(mvchip)) ^
  342. readl_relaxed(mvebu_gpioreg_data_in(mvchip));
  343. /*
  344. * set initial polarity based on current input level
  345. */
  346. u = readl_relaxed(mvebu_gpioreg_in_pol(mvchip));
  347. if (v & (1 << pin))
  348. u |= 1 << pin; /* falling */
  349. else
  350. u &= ~(1 << pin); /* rising */
  351. writel_relaxed(u, mvebu_gpioreg_in_pol(mvchip));
  352. }
  353. }
  354. return 0;
  355. }
  356. static void mvebu_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
  357. {
  358. struct mvebu_gpio_chip *mvchip = irq_get_handler_data(irq);
  359. u32 cause, type;
  360. int i;
  361. if (mvchip == NULL)
  362. return;
  363. cause = readl_relaxed(mvebu_gpioreg_data_in(mvchip)) &
  364. readl_relaxed(mvebu_gpioreg_level_mask(mvchip));
  365. cause |= readl_relaxed(mvebu_gpioreg_edge_cause(mvchip)) &
  366. readl_relaxed(mvebu_gpioreg_edge_mask(mvchip));
  367. for (i = 0; i < mvchip->chip.ngpio; i++) {
  368. int irq;
  369. irq = mvchip->irqbase + i;
  370. if (!(cause & (1 << i)))
  371. continue;
  372. type = irqd_get_trigger_type(irq_get_irq_data(irq));
  373. if ((type & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH) {
  374. /* Swap polarity (race with GPIO line) */
  375. u32 polarity;
  376. polarity = readl_relaxed(mvebu_gpioreg_in_pol(mvchip));
  377. polarity ^= 1 << i;
  378. writel_relaxed(polarity, mvebu_gpioreg_in_pol(mvchip));
  379. }
  380. generic_handle_irq(irq);
  381. }
  382. }
  383. static struct platform_device_id mvebu_gpio_ids[] = {
  384. {
  385. .name = "orion-gpio",
  386. }, {
  387. .name = "mv78200-gpio",
  388. }, {
  389. .name = "armadaxp-gpio",
  390. }, {
  391. /* sentinel */
  392. },
  393. };
  394. MODULE_DEVICE_TABLE(platform, mvebu_gpio_ids);
  395. static struct of_device_id mvebu_gpio_of_match[] __devinitdata = {
  396. {
  397. .compatible = "marvell,orion-gpio",
  398. .data = (void*) MVEBU_GPIO_SOC_VARIANT_ORION,
  399. },
  400. {
  401. .compatible = "marvell,mv78200-gpio",
  402. .data = (void*) MVEBU_GPIO_SOC_VARIANT_MV78200,
  403. },
  404. {
  405. .compatible = "marvell,armadaxp-gpio",
  406. .data = (void*) MVEBU_GPIO_SOC_VARIANT_ARMADAXP,
  407. },
  408. {
  409. /* sentinel */
  410. },
  411. };
  412. MODULE_DEVICE_TABLE(of, mvebu_gpio_of_match);
  413. static int __devinit mvebu_gpio_probe(struct platform_device *pdev)
  414. {
  415. struct mvebu_gpio_chip *mvchip;
  416. const struct of_device_id *match;
  417. struct device_node *np = pdev->dev.of_node;
  418. struct resource *res;
  419. struct irq_chip_generic *gc;
  420. struct irq_chip_type *ct;
  421. unsigned int ngpios;
  422. int soc_variant;
  423. int i, cpu, id;
  424. match = of_match_device(mvebu_gpio_of_match, &pdev->dev);
  425. if (match)
  426. soc_variant = (int) match->data;
  427. else
  428. soc_variant = MVEBU_GPIO_SOC_VARIANT_ORION;
  429. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  430. if (! res) {
  431. dev_err(&pdev->dev, "Cannot get memory resource\n");
  432. return -ENODEV;
  433. }
  434. mvchip = devm_kzalloc(&pdev->dev, sizeof(struct mvebu_gpio_chip), GFP_KERNEL);
  435. if (! mvchip){
  436. dev_err(&pdev->dev, "Cannot allocate memory\n");
  437. return -ENOMEM;
  438. }
  439. if (of_property_read_u32(pdev->dev.of_node, "ngpios", &ngpios)) {
  440. dev_err(&pdev->dev, "Missing ngpios OF property\n");
  441. return -ENODEV;
  442. }
  443. id = of_alias_get_id(pdev->dev.of_node, "gpio");
  444. if (id < 0) {
  445. dev_err(&pdev->dev, "Couldn't get OF id\n");
  446. return id;
  447. }
  448. mvchip->soc_variant = soc_variant;
  449. mvchip->chip.label = dev_name(&pdev->dev);
  450. mvchip->chip.dev = &pdev->dev;
  451. mvchip->chip.request = mvebu_gpio_request;
  452. mvchip->chip.direction_input = mvebu_gpio_direction_input;
  453. mvchip->chip.get = mvebu_gpio_get;
  454. mvchip->chip.direction_output = mvebu_gpio_direction_output;
  455. mvchip->chip.set = mvebu_gpio_set;
  456. mvchip->chip.to_irq = mvebu_gpio_to_irq;
  457. mvchip->chip.base = id * MVEBU_MAX_GPIO_PER_BANK;
  458. mvchip->chip.ngpio = ngpios;
  459. mvchip->chip.can_sleep = 0;
  460. #ifdef CONFIG_OF
  461. mvchip->chip.of_node = np;
  462. #endif
  463. spin_lock_init(&mvchip->lock);
  464. mvchip->membase = devm_request_and_ioremap(&pdev->dev, res);
  465. if (! mvchip->membase) {
  466. dev_err(&pdev->dev, "Cannot ioremap\n");
  467. kfree(mvchip->chip.label);
  468. return -ENOMEM;
  469. }
  470. /* The Armada XP has a second range of registers for the
  471. * per-CPU registers */
  472. if (soc_variant == MVEBU_GPIO_SOC_VARIANT_ARMADAXP) {
  473. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  474. if (! res) {
  475. dev_err(&pdev->dev, "Cannot get memory resource\n");
  476. kfree(mvchip->chip.label);
  477. return -ENODEV;
  478. }
  479. mvchip->percpu_membase = devm_request_and_ioremap(&pdev->dev, res);
  480. if (! mvchip->percpu_membase) {
  481. dev_err(&pdev->dev, "Cannot ioremap\n");
  482. kfree(mvchip->chip.label);
  483. return -ENOMEM;
  484. }
  485. }
  486. /*
  487. * Mask and clear GPIO interrupts.
  488. */
  489. switch(soc_variant) {
  490. case MVEBU_GPIO_SOC_VARIANT_ORION:
  491. writel_relaxed(0, mvchip->membase + GPIO_EDGE_CAUSE_OFF);
  492. writel_relaxed(0, mvchip->membase + GPIO_EDGE_MASK_OFF);
  493. writel_relaxed(0, mvchip->membase + GPIO_LEVEL_MASK_OFF);
  494. break;
  495. case MVEBU_GPIO_SOC_VARIANT_MV78200:
  496. writel_relaxed(0, mvchip->membase + GPIO_EDGE_CAUSE_OFF);
  497. for (cpu = 0; cpu < 2; cpu++) {
  498. writel_relaxed(0, mvchip->membase +
  499. GPIO_EDGE_MASK_MV78200_OFF(cpu));
  500. writel_relaxed(0, mvchip->membase +
  501. GPIO_LEVEL_MASK_MV78200_OFF(cpu));
  502. }
  503. break;
  504. case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
  505. writel_relaxed(0, mvchip->membase + GPIO_EDGE_CAUSE_OFF);
  506. writel_relaxed(0, mvchip->membase + GPIO_EDGE_MASK_OFF);
  507. writel_relaxed(0, mvchip->membase + GPIO_LEVEL_MASK_OFF);
  508. for (cpu = 0; cpu < 4; cpu++) {
  509. writel_relaxed(0, mvchip->percpu_membase +
  510. GPIO_EDGE_CAUSE_ARMADAXP_OFF(cpu));
  511. writel_relaxed(0, mvchip->percpu_membase +
  512. GPIO_EDGE_MASK_ARMADAXP_OFF(cpu));
  513. writel_relaxed(0, mvchip->percpu_membase +
  514. GPIO_LEVEL_MASK_ARMADAXP_OFF(cpu));
  515. }
  516. break;
  517. default:
  518. BUG();
  519. }
  520. gpiochip_add(&mvchip->chip);
  521. /* Some gpio controllers do not provide irq support */
  522. if (!of_irq_count(np))
  523. return 0;
  524. /* Setup the interrupt handlers. Each chip can have up to 4
  525. * interrupt handlers, with each handler dealing with 8 GPIO
  526. * pins. */
  527. for (i = 0; i < 4; i++) {
  528. int irq;
  529. irq = platform_get_irq(pdev, i);
  530. if (irq < 0)
  531. continue;
  532. irq_set_handler_data(irq, mvchip);
  533. irq_set_chained_handler(irq, mvebu_gpio_irq_handler);
  534. }
  535. mvchip->irqbase = irq_alloc_descs(-1, 0, ngpios, -1);
  536. if (mvchip->irqbase < 0) {
  537. dev_err(&pdev->dev, "no irqs\n");
  538. kfree(mvchip->chip.label);
  539. return -ENOMEM;
  540. }
  541. gc = irq_alloc_generic_chip("mvebu_gpio_irq", 2, mvchip->irqbase,
  542. mvchip->membase, handle_level_irq);
  543. if (! gc) {
  544. dev_err(&pdev->dev, "Cannot allocate generic irq_chip\n");
  545. kfree(mvchip->chip.label);
  546. return -ENOMEM;
  547. }
  548. gc->private = mvchip;
  549. ct = &gc->chip_types[0];
  550. ct->type = IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW;
  551. ct->chip.irq_mask = mvebu_gpio_level_irq_mask;
  552. ct->chip.irq_unmask = mvebu_gpio_level_irq_unmask;
  553. ct->chip.irq_set_type = mvebu_gpio_irq_set_type;
  554. ct->chip.name = mvchip->chip.label;
  555. ct = &gc->chip_types[1];
  556. ct->type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
  557. ct->chip.irq_ack = mvebu_gpio_irq_ack;
  558. ct->chip.irq_mask = mvebu_gpio_edge_irq_mask;
  559. ct->chip.irq_unmask = mvebu_gpio_edge_irq_unmask;
  560. ct->chip.irq_set_type = mvebu_gpio_irq_set_type;
  561. ct->handler = handle_edge_irq;
  562. ct->chip.name = mvchip->chip.label;
  563. irq_setup_generic_chip(gc, IRQ_MSK(ngpios), 0,
  564. IRQ_NOREQUEST, IRQ_LEVEL | IRQ_NOPROBE);
  565. /* Setup irq domain on top of the generic chip. */
  566. mvchip->domain = irq_domain_add_legacy(np, mvchip->chip.ngpio,
  567. mvchip->irqbase, 0,
  568. &irq_domain_simple_ops,
  569. mvchip);
  570. if (!mvchip->domain) {
  571. dev_err(&pdev->dev, "couldn't allocate irq domain %s (DT).\n",
  572. mvchip->chip.label);
  573. irq_remove_generic_chip(gc, IRQ_MSK(ngpios), IRQ_NOREQUEST,
  574. IRQ_LEVEL | IRQ_NOPROBE);
  575. kfree(gc);
  576. kfree(mvchip->chip.label);
  577. return -ENODEV;
  578. }
  579. return 0;
  580. }
  581. static struct platform_driver mvebu_gpio_driver = {
  582. .driver = {
  583. .name = "mvebu-gpio",
  584. .owner = THIS_MODULE,
  585. .of_match_table = mvebu_gpio_of_match,
  586. },
  587. .probe = mvebu_gpio_probe,
  588. .id_table = mvebu_gpio_ids,
  589. };
  590. static int __init mvebu_gpio_init(void)
  591. {
  592. return platform_driver_register(&mvebu_gpio_driver);
  593. }
  594. postcore_initcall(mvebu_gpio_init);