gpio-mvebu.c 19 KB

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