gpio-mvebu.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. /*
  2. * GPIO driver for Marvell SoCs
  3. *
  4. * Copyright (C) 2012 Marvell
  5. *
  6. * Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
  7. * Andrew Lunn <andrew@lunn.ch>
  8. * Sebastian Hesselbarth <sebastian.hesselbarth@gmail.com>
  9. *
  10. * This file is licensed under the terms of the GNU General Public
  11. * License version 2. This program is licensed "as is" without any
  12. * warranty of any kind, whether express or implied.
  13. *
  14. * This driver is a fairly straightforward GPIO driver for the
  15. * complete family of Marvell EBU SoC platforms (Orion, Dove,
  16. * Kirkwood, Discovery, Armada 370/XP). The only complexity of this
  17. * driver is the different register layout that exists between the
  18. * non-SMP platforms (Orion, Dove, Kirkwood, Armada 370) and the SMP
  19. * platforms (MV78200 from the Discovery family and the Armada
  20. * XP). Therefore, this driver handles three variants of the GPIO
  21. * block:
  22. * - the basic variant, called "orion-gpio", with the simplest
  23. * register set. Used on Orion, Dove, Kirkwoord, Armada 370 and
  24. * non-SMP Discovery systems
  25. * - the mv78200 variant for MV78200 Discovery systems. This variant
  26. * turns the edge mask and level mask registers into CPU0 edge
  27. * mask/level mask registers, and adds CPU1 edge mask/level mask
  28. * registers.
  29. * - the armadaxp variant for Armada XP systems. This variant keeps
  30. * the normal cause/edge mask/level mask registers when the global
  31. * interrupts are used, but adds per-CPU cause/edge mask/level mask
  32. * registers n a separate memory area for the per-CPU GPIO
  33. * interrupts.
  34. */
  35. #include <linux/err.h>
  36. #include <linux/module.h>
  37. #include <linux/gpio.h>
  38. #include <linux/irq.h>
  39. #include <linux/slab.h>
  40. #include <linux/irqdomain.h>
  41. #include <linux/io.h>
  42. #include <linux/of_irq.h>
  43. #include <linux/of_device.h>
  44. #include <linux/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_blink(struct mvebu_gpio_chip *mvchip)
  87. {
  88. return mvchip->membase + GPIO_BLINK_EN_OFF;
  89. }
  90. static inline void __iomem *mvebu_gpioreg_io_conf(struct mvebu_gpio_chip *mvchip)
  91. {
  92. return mvchip->membase + GPIO_IO_CONF_OFF;
  93. }
  94. static inline void __iomem *mvebu_gpioreg_in_pol(struct mvebu_gpio_chip *mvchip)
  95. {
  96. return mvchip->membase + GPIO_IN_POL_OFF;
  97. }
  98. static inline void __iomem *mvebu_gpioreg_data_in(struct mvebu_gpio_chip *mvchip)
  99. {
  100. return mvchip->membase + GPIO_DATA_IN_OFF;
  101. }
  102. static inline void __iomem *mvebu_gpioreg_edge_cause(struct mvebu_gpio_chip *mvchip)
  103. {
  104. int cpu;
  105. switch(mvchip->soc_variant) {
  106. case MVEBU_GPIO_SOC_VARIANT_ORION:
  107. case MVEBU_GPIO_SOC_VARIANT_MV78200:
  108. return mvchip->membase + GPIO_EDGE_CAUSE_OFF;
  109. case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
  110. cpu = smp_processor_id();
  111. return mvchip->percpu_membase + GPIO_EDGE_CAUSE_ARMADAXP_OFF(cpu);
  112. default:
  113. BUG();
  114. }
  115. }
  116. static inline void __iomem *mvebu_gpioreg_edge_mask(struct mvebu_gpio_chip *mvchip)
  117. {
  118. int cpu;
  119. switch(mvchip->soc_variant) {
  120. case MVEBU_GPIO_SOC_VARIANT_ORION:
  121. return mvchip->membase + GPIO_EDGE_MASK_OFF;
  122. case MVEBU_GPIO_SOC_VARIANT_MV78200:
  123. cpu = smp_processor_id();
  124. return mvchip->membase + GPIO_EDGE_MASK_MV78200_OFF(cpu);
  125. case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
  126. cpu = smp_processor_id();
  127. return mvchip->percpu_membase + GPIO_EDGE_MASK_ARMADAXP_OFF(cpu);
  128. default:
  129. BUG();
  130. }
  131. }
  132. static void __iomem *mvebu_gpioreg_level_mask(struct mvebu_gpio_chip *mvchip)
  133. {
  134. int cpu;
  135. switch(mvchip->soc_variant) {
  136. case MVEBU_GPIO_SOC_VARIANT_ORION:
  137. return mvchip->membase + GPIO_LEVEL_MASK_OFF;
  138. case MVEBU_GPIO_SOC_VARIANT_MV78200:
  139. cpu = smp_processor_id();
  140. return mvchip->membase + GPIO_LEVEL_MASK_MV78200_OFF(cpu);
  141. case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
  142. cpu = smp_processor_id();
  143. return mvchip->percpu_membase + GPIO_LEVEL_MASK_ARMADAXP_OFF(cpu);
  144. default:
  145. BUG();
  146. }
  147. }
  148. /*
  149. * Functions implementing the gpio_chip methods
  150. */
  151. static int mvebu_gpio_request(struct gpio_chip *chip, unsigned pin)
  152. {
  153. return pinctrl_request_gpio(chip->base + pin);
  154. }
  155. static void mvebu_gpio_free(struct gpio_chip *chip, unsigned pin)
  156. {
  157. pinctrl_free_gpio(chip->base + pin);
  158. }
  159. static void mvebu_gpio_set(struct gpio_chip *chip, unsigned pin, int value)
  160. {
  161. struct mvebu_gpio_chip *mvchip =
  162. container_of(chip, struct mvebu_gpio_chip, chip);
  163. unsigned long flags;
  164. u32 u;
  165. spin_lock_irqsave(&mvchip->lock, flags);
  166. u = readl_relaxed(mvebu_gpioreg_out(mvchip));
  167. if (value)
  168. u |= 1 << pin;
  169. else
  170. u &= ~(1 << pin);
  171. writel_relaxed(u, mvebu_gpioreg_out(mvchip));
  172. spin_unlock_irqrestore(&mvchip->lock, flags);
  173. }
  174. static int mvebu_gpio_get(struct gpio_chip *chip, unsigned pin)
  175. {
  176. struct mvebu_gpio_chip *mvchip =
  177. container_of(chip, struct mvebu_gpio_chip, chip);
  178. u32 u;
  179. if (readl_relaxed(mvebu_gpioreg_io_conf(mvchip)) & (1 << pin)) {
  180. u = readl_relaxed(mvebu_gpioreg_data_in(mvchip)) ^
  181. readl_relaxed(mvebu_gpioreg_in_pol(mvchip));
  182. } else {
  183. u = readl_relaxed(mvebu_gpioreg_out(mvchip));
  184. }
  185. return (u >> pin) & 1;
  186. }
  187. static void mvebu_gpio_blink(struct gpio_chip *chip, unsigned pin, int value)
  188. {
  189. struct mvebu_gpio_chip *mvchip =
  190. container_of(chip, struct mvebu_gpio_chip, chip);
  191. unsigned long flags;
  192. u32 u;
  193. spin_lock_irqsave(&mvchip->lock, flags);
  194. u = readl_relaxed(mvebu_gpioreg_blink(mvchip));
  195. if (value)
  196. u |= 1 << pin;
  197. else
  198. u &= ~(1 << pin);
  199. writel_relaxed(u, mvebu_gpioreg_blink(mvchip));
  200. spin_unlock_irqrestore(&mvchip->lock, flags);
  201. }
  202. static int mvebu_gpio_direction_input(struct gpio_chip *chip, unsigned pin)
  203. {
  204. struct mvebu_gpio_chip *mvchip =
  205. container_of(chip, struct mvebu_gpio_chip, chip);
  206. unsigned long flags;
  207. int ret;
  208. u32 u;
  209. /* Check with the pinctrl driver whether this pin is usable as
  210. * an input GPIO */
  211. ret = pinctrl_gpio_direction_input(chip->base + pin);
  212. if (ret)
  213. return ret;
  214. spin_lock_irqsave(&mvchip->lock, flags);
  215. u = readl_relaxed(mvebu_gpioreg_io_conf(mvchip));
  216. u |= 1 << pin;
  217. writel_relaxed(u, mvebu_gpioreg_io_conf(mvchip));
  218. spin_unlock_irqrestore(&mvchip->lock, flags);
  219. return 0;
  220. }
  221. static int mvebu_gpio_direction_output(struct gpio_chip *chip, unsigned pin,
  222. int value)
  223. {
  224. struct mvebu_gpio_chip *mvchip =
  225. container_of(chip, struct mvebu_gpio_chip, chip);
  226. unsigned long flags;
  227. int ret;
  228. u32 u;
  229. /* Check with the pinctrl driver whether this pin is usable as
  230. * an output GPIO */
  231. ret = pinctrl_gpio_direction_output(chip->base + pin);
  232. if (ret)
  233. return ret;
  234. mvebu_gpio_blink(chip, pin, 0);
  235. mvebu_gpio_set(chip, pin, value);
  236. spin_lock_irqsave(&mvchip->lock, flags);
  237. u = readl_relaxed(mvebu_gpioreg_io_conf(mvchip));
  238. u &= ~(1 << pin);
  239. writel_relaxed(u, mvebu_gpioreg_io_conf(mvchip));
  240. spin_unlock_irqrestore(&mvchip->lock, flags);
  241. return 0;
  242. }
  243. static int mvebu_gpio_to_irq(struct gpio_chip *chip, unsigned pin)
  244. {
  245. struct mvebu_gpio_chip *mvchip =
  246. container_of(chip, struct mvebu_gpio_chip, chip);
  247. return irq_create_mapping(mvchip->domain, pin);
  248. }
  249. /*
  250. * Functions implementing the irq_chip methods
  251. */
  252. static void mvebu_gpio_irq_ack(struct irq_data *d)
  253. {
  254. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  255. struct mvebu_gpio_chip *mvchip = gc->private;
  256. u32 mask = ~(1 << (d->irq - gc->irq_base));
  257. irq_gc_lock(gc);
  258. writel_relaxed(mask, mvebu_gpioreg_edge_cause(mvchip));
  259. irq_gc_unlock(gc);
  260. }
  261. static void mvebu_gpio_edge_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_edge_mask(mvchip));
  269. irq_gc_unlock(gc);
  270. }
  271. static void mvebu_gpio_edge_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_edge_mask(mvchip));
  279. irq_gc_unlock(gc);
  280. }
  281. static void mvebu_gpio_level_irq_mask(struct irq_data *d)
  282. {
  283. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  284. struct mvebu_gpio_chip *mvchip = gc->private;
  285. u32 mask = 1 << (d->irq - gc->irq_base);
  286. irq_gc_lock(gc);
  287. gc->mask_cache &= ~mask;
  288. writel_relaxed(gc->mask_cache, mvebu_gpioreg_level_mask(mvchip));
  289. irq_gc_unlock(gc);
  290. }
  291. static void mvebu_gpio_level_irq_unmask(struct irq_data *d)
  292. {
  293. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  294. struct mvebu_gpio_chip *mvchip = gc->private;
  295. u32 mask = 1 << (d->irq - gc->irq_base);
  296. irq_gc_lock(gc);
  297. gc->mask_cache |= mask;
  298. writel_relaxed(gc->mask_cache, mvebu_gpioreg_level_mask(mvchip));
  299. irq_gc_unlock(gc);
  300. }
  301. /*****************************************************************************
  302. * MVEBU GPIO IRQ
  303. *
  304. * GPIO_IN_POL register controls whether GPIO_DATA_IN will hold the same
  305. * value of the line or the opposite value.
  306. *
  307. * Level IRQ handlers: DATA_IN is used directly as cause register.
  308. * Interrupt are masked by LEVEL_MASK registers.
  309. * Edge IRQ handlers: Change in DATA_IN are latched in EDGE_CAUSE.
  310. * Interrupt are masked by EDGE_MASK registers.
  311. * Both-edge handlers: Similar to regular Edge handlers, but also swaps
  312. * the polarity to catch the next line transaction.
  313. * This is a race condition that might not perfectly
  314. * work on some use cases.
  315. *
  316. * Every eight GPIO lines are grouped (OR'ed) before going up to main
  317. * cause register.
  318. *
  319. * EDGE cause mask
  320. * data-in /--------| |-----| |----\
  321. * -----| |----- ---- to main cause reg
  322. * X \----------------| |----/
  323. * polarity LEVEL mask
  324. *
  325. ****************************************************************************/
  326. static int mvebu_gpio_irq_set_type(struct irq_data *d, unsigned int type)
  327. {
  328. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  329. struct irq_chip_type *ct = irq_data_get_chip_type(d);
  330. struct mvebu_gpio_chip *mvchip = gc->private;
  331. int pin;
  332. u32 u;
  333. pin = d->hwirq;
  334. u = readl_relaxed(mvebu_gpioreg_io_conf(mvchip)) & (1 << pin);
  335. if (!u) {
  336. return -EINVAL;
  337. }
  338. type &= IRQ_TYPE_SENSE_MASK;
  339. if (type == IRQ_TYPE_NONE)
  340. return -EINVAL;
  341. /* Check if we need to change chip and handler */
  342. if (!(ct->type & type))
  343. if (irq_setup_alt_chip(d, type))
  344. return -EINVAL;
  345. /*
  346. * Configure interrupt polarity.
  347. */
  348. switch(type) {
  349. case IRQ_TYPE_EDGE_RISING:
  350. case IRQ_TYPE_LEVEL_HIGH:
  351. u = readl_relaxed(mvebu_gpioreg_in_pol(mvchip));
  352. u &= ~(1 << pin);
  353. writel_relaxed(u, mvebu_gpioreg_in_pol(mvchip));
  354. break;
  355. case IRQ_TYPE_EDGE_FALLING:
  356. case IRQ_TYPE_LEVEL_LOW:
  357. u = readl_relaxed(mvebu_gpioreg_in_pol(mvchip));
  358. u |= 1 << pin;
  359. writel_relaxed(u, mvebu_gpioreg_in_pol(mvchip));
  360. break;
  361. case IRQ_TYPE_EDGE_BOTH: {
  362. u32 v;
  363. v = readl_relaxed(mvebu_gpioreg_in_pol(mvchip)) ^
  364. readl_relaxed(mvebu_gpioreg_data_in(mvchip));
  365. /*
  366. * set initial polarity based on current input level
  367. */
  368. u = readl_relaxed(mvebu_gpioreg_in_pol(mvchip));
  369. if (v & (1 << pin))
  370. u |= 1 << pin; /* falling */
  371. else
  372. u &= ~(1 << pin); /* rising */
  373. writel_relaxed(u, mvebu_gpioreg_in_pol(mvchip));
  374. break;
  375. }
  376. }
  377. return 0;
  378. }
  379. static void mvebu_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
  380. {
  381. struct mvebu_gpio_chip *mvchip = irq_get_handler_data(irq);
  382. u32 cause, type;
  383. int i;
  384. if (mvchip == NULL)
  385. return;
  386. cause = readl_relaxed(mvebu_gpioreg_data_in(mvchip)) &
  387. readl_relaxed(mvebu_gpioreg_level_mask(mvchip));
  388. cause |= readl_relaxed(mvebu_gpioreg_edge_cause(mvchip)) &
  389. readl_relaxed(mvebu_gpioreg_edge_mask(mvchip));
  390. for (i = 0; i < mvchip->chip.ngpio; i++) {
  391. int irq;
  392. irq = mvchip->irqbase + i;
  393. if (!(cause & (1 << i)))
  394. continue;
  395. type = irqd_get_trigger_type(irq_get_irq_data(irq));
  396. if ((type & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH) {
  397. /* Swap polarity (race with GPIO line) */
  398. u32 polarity;
  399. polarity = readl_relaxed(mvebu_gpioreg_in_pol(mvchip));
  400. polarity ^= 1 << i;
  401. writel_relaxed(polarity, mvebu_gpioreg_in_pol(mvchip));
  402. }
  403. generic_handle_irq(irq);
  404. }
  405. }
  406. static struct of_device_id mvebu_gpio_of_match[] = {
  407. {
  408. .compatible = "marvell,orion-gpio",
  409. .data = (void*) MVEBU_GPIO_SOC_VARIANT_ORION,
  410. },
  411. {
  412. .compatible = "marvell,mv78200-gpio",
  413. .data = (void*) MVEBU_GPIO_SOC_VARIANT_MV78200,
  414. },
  415. {
  416. .compatible = "marvell,armadaxp-gpio",
  417. .data = (void*) MVEBU_GPIO_SOC_VARIANT_ARMADAXP,
  418. },
  419. {
  420. /* sentinel */
  421. },
  422. };
  423. MODULE_DEVICE_TABLE(of, mvebu_gpio_of_match);
  424. static int mvebu_gpio_probe(struct platform_device *pdev)
  425. {
  426. struct mvebu_gpio_chip *mvchip;
  427. const struct of_device_id *match;
  428. struct device_node *np = pdev->dev.of_node;
  429. struct resource *res;
  430. struct irq_chip_generic *gc;
  431. struct irq_chip_type *ct;
  432. unsigned int ngpios;
  433. int soc_variant;
  434. int i, cpu, id;
  435. match = of_match_device(mvebu_gpio_of_match, &pdev->dev);
  436. if (match)
  437. soc_variant = (int) match->data;
  438. else
  439. soc_variant = MVEBU_GPIO_SOC_VARIANT_ORION;
  440. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  441. if (! res) {
  442. dev_err(&pdev->dev, "Cannot get memory resource\n");
  443. return -ENODEV;
  444. }
  445. mvchip = devm_kzalloc(&pdev->dev, sizeof(struct mvebu_gpio_chip), GFP_KERNEL);
  446. if (! mvchip){
  447. dev_err(&pdev->dev, "Cannot allocate memory\n");
  448. return -ENOMEM;
  449. }
  450. if (of_property_read_u32(pdev->dev.of_node, "ngpios", &ngpios)) {
  451. dev_err(&pdev->dev, "Missing ngpios OF property\n");
  452. return -ENODEV;
  453. }
  454. id = of_alias_get_id(pdev->dev.of_node, "gpio");
  455. if (id < 0) {
  456. dev_err(&pdev->dev, "Couldn't get OF id\n");
  457. return id;
  458. }
  459. mvchip->soc_variant = soc_variant;
  460. mvchip->chip.label = dev_name(&pdev->dev);
  461. mvchip->chip.dev = &pdev->dev;
  462. mvchip->chip.request = mvebu_gpio_request;
  463. mvchip->chip.free = mvebu_gpio_free;
  464. mvchip->chip.direction_input = mvebu_gpio_direction_input;
  465. mvchip->chip.get = mvebu_gpio_get;
  466. mvchip->chip.direction_output = mvebu_gpio_direction_output;
  467. mvchip->chip.set = mvebu_gpio_set;
  468. mvchip->chip.to_irq = mvebu_gpio_to_irq;
  469. mvchip->chip.base = id * MVEBU_MAX_GPIO_PER_BANK;
  470. mvchip->chip.ngpio = ngpios;
  471. mvchip->chip.can_sleep = 0;
  472. mvchip->chip.of_node = np;
  473. spin_lock_init(&mvchip->lock);
  474. mvchip->membase = devm_ioremap_resource(&pdev->dev, res);
  475. if (IS_ERR(mvchip->membase))
  476. return PTR_ERR(mvchip->membase);
  477. /* The Armada XP has a second range of registers for the
  478. * per-CPU registers */
  479. if (soc_variant == MVEBU_GPIO_SOC_VARIANT_ARMADAXP) {
  480. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  481. if (! res) {
  482. dev_err(&pdev->dev, "Cannot get memory resource\n");
  483. return -ENODEV;
  484. }
  485. mvchip->percpu_membase = devm_ioremap_resource(&pdev->dev,
  486. res);
  487. if (IS_ERR(mvchip->percpu_membase))
  488. return PTR_ERR(mvchip->percpu_membase);
  489. }
  490. /*
  491. * Mask and clear GPIO interrupts.
  492. */
  493. switch(soc_variant) {
  494. case MVEBU_GPIO_SOC_VARIANT_ORION:
  495. writel_relaxed(0, mvchip->membase + GPIO_EDGE_CAUSE_OFF);
  496. writel_relaxed(0, mvchip->membase + GPIO_EDGE_MASK_OFF);
  497. writel_relaxed(0, mvchip->membase + GPIO_LEVEL_MASK_OFF);
  498. break;
  499. case MVEBU_GPIO_SOC_VARIANT_MV78200:
  500. writel_relaxed(0, mvchip->membase + GPIO_EDGE_CAUSE_OFF);
  501. for (cpu = 0; cpu < 2; cpu++) {
  502. writel_relaxed(0, mvchip->membase +
  503. GPIO_EDGE_MASK_MV78200_OFF(cpu));
  504. writel_relaxed(0, mvchip->membase +
  505. GPIO_LEVEL_MASK_MV78200_OFF(cpu));
  506. }
  507. break;
  508. case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
  509. writel_relaxed(0, mvchip->membase + GPIO_EDGE_CAUSE_OFF);
  510. writel_relaxed(0, mvchip->membase + GPIO_EDGE_MASK_OFF);
  511. writel_relaxed(0, mvchip->membase + GPIO_LEVEL_MASK_OFF);
  512. for (cpu = 0; cpu < 4; cpu++) {
  513. writel_relaxed(0, mvchip->percpu_membase +
  514. GPIO_EDGE_CAUSE_ARMADAXP_OFF(cpu));
  515. writel_relaxed(0, mvchip->percpu_membase +
  516. GPIO_EDGE_MASK_ARMADAXP_OFF(cpu));
  517. writel_relaxed(0, mvchip->percpu_membase +
  518. GPIO_LEVEL_MASK_ARMADAXP_OFF(cpu));
  519. }
  520. break;
  521. default:
  522. BUG();
  523. }
  524. gpiochip_add(&mvchip->chip);
  525. /* Some gpio controllers do not provide irq support */
  526. if (!of_irq_count(np))
  527. return 0;
  528. /* Setup the interrupt handlers. Each chip can have up to 4
  529. * interrupt handlers, with each handler dealing with 8 GPIO
  530. * pins. */
  531. for (i = 0; i < 4; i++) {
  532. int irq;
  533. irq = platform_get_irq(pdev, i);
  534. if (irq < 0)
  535. continue;
  536. irq_set_handler_data(irq, mvchip);
  537. irq_set_chained_handler(irq, mvebu_gpio_irq_handler);
  538. }
  539. mvchip->irqbase = irq_alloc_descs(-1, 0, ngpios, -1);
  540. if (mvchip->irqbase < 0) {
  541. dev_err(&pdev->dev, "no irqs\n");
  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. 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), 0,
  566. IRQ_NOREQUEST, IRQ_LEVEL | IRQ_NOPROBE);
  567. /* Setup irq domain on top of the generic chip. */
  568. mvchip->domain = irq_domain_add_simple(np, mvchip->chip.ngpio,
  569. mvchip->irqbase,
  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. return -ENODEV;
  579. }
  580. return 0;
  581. }
  582. static struct platform_driver mvebu_gpio_driver = {
  583. .driver = {
  584. .name = "mvebu-gpio",
  585. .owner = THIS_MODULE,
  586. .of_match_table = mvebu_gpio_of_match,
  587. },
  588. .probe = mvebu_gpio_probe,
  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);