gpio-pxa.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. /*
  2. * linux/arch/arm/plat-pxa/gpio.c
  3. *
  4. * Generic PXA GPIO handling
  5. *
  6. * Author: Nicolas Pitre
  7. * Created: Jun 15, 2001
  8. * Copyright: MontaVista Software Inc.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/clk.h>
  16. #include <linux/err.h>
  17. #include <linux/gpio.h>
  18. #include <linux/gpio-pxa.h>
  19. #include <linux/init.h>
  20. #include <linux/irq.h>
  21. #include <linux/irqdomain.h>
  22. #include <linux/io.h>
  23. #include <linux/of.h>
  24. #include <linux/of_device.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/syscore_ops.h>
  27. #include <linux/slab.h>
  28. #include <mach/irqs.h>
  29. /*
  30. * We handle the GPIOs by banks, each bank covers up to 32 GPIOs with
  31. * one set of registers. The register offsets are organized below:
  32. *
  33. * GPLR GPDR GPSR GPCR GRER GFER GEDR
  34. * BANK 0 - 0x0000 0x000C 0x0018 0x0024 0x0030 0x003C 0x0048
  35. * BANK 1 - 0x0004 0x0010 0x001C 0x0028 0x0034 0x0040 0x004C
  36. * BANK 2 - 0x0008 0x0014 0x0020 0x002C 0x0038 0x0044 0x0050
  37. *
  38. * BANK 3 - 0x0100 0x010C 0x0118 0x0124 0x0130 0x013C 0x0148
  39. * BANK 4 - 0x0104 0x0110 0x011C 0x0128 0x0134 0x0140 0x014C
  40. * BANK 5 - 0x0108 0x0114 0x0120 0x012C 0x0138 0x0144 0x0150
  41. *
  42. * NOTE:
  43. * BANK 3 is only available on PXA27x and later processors.
  44. * BANK 4 and 5 are only available on PXA935
  45. */
  46. #define GPLR_OFFSET 0x00
  47. #define GPDR_OFFSET 0x0C
  48. #define GPSR_OFFSET 0x18
  49. #define GPCR_OFFSET 0x24
  50. #define GRER_OFFSET 0x30
  51. #define GFER_OFFSET 0x3C
  52. #define GEDR_OFFSET 0x48
  53. #define GAFR_OFFSET 0x54
  54. #define ED_MASK_OFFSET 0x9C /* GPIO edge detection for AP side */
  55. #define BANK_OFF(n) (((n) < 3) ? (n) << 2 : 0x100 + (((n) - 3) << 2))
  56. int pxa_last_gpio;
  57. #ifdef CONFIG_OF
  58. static struct irq_domain *domain;
  59. static struct device_node *pxa_gpio_of_node;
  60. #endif
  61. struct pxa_gpio_chip {
  62. struct gpio_chip chip;
  63. void __iomem *regbase;
  64. char label[10];
  65. unsigned long irq_mask;
  66. unsigned long irq_edge_rise;
  67. unsigned long irq_edge_fall;
  68. int (*set_wake)(unsigned int gpio, unsigned int on);
  69. #ifdef CONFIG_PM
  70. unsigned long saved_gplr;
  71. unsigned long saved_gpdr;
  72. unsigned long saved_grer;
  73. unsigned long saved_gfer;
  74. #endif
  75. };
  76. enum {
  77. PXA25X_GPIO = 0,
  78. PXA26X_GPIO,
  79. PXA27X_GPIO,
  80. PXA3XX_GPIO,
  81. PXA93X_GPIO,
  82. MMP_GPIO = 0x10,
  83. };
  84. static DEFINE_SPINLOCK(gpio_lock);
  85. static struct pxa_gpio_chip *pxa_gpio_chips;
  86. static int gpio_type;
  87. static void __iomem *gpio_reg_base;
  88. #define for_each_gpio_chip(i, c) \
  89. for (i = 0, c = &pxa_gpio_chips[0]; i <= pxa_last_gpio; i += 32, c++)
  90. static inline void __iomem *gpio_chip_base(struct gpio_chip *c)
  91. {
  92. return container_of(c, struct pxa_gpio_chip, chip)->regbase;
  93. }
  94. static inline struct pxa_gpio_chip *gpio_to_pxachip(unsigned gpio)
  95. {
  96. return &pxa_gpio_chips[gpio_to_bank(gpio)];
  97. }
  98. static inline int gpio_is_pxa_type(int type)
  99. {
  100. return (type & MMP_GPIO) == 0;
  101. }
  102. static inline int gpio_is_mmp_type(int type)
  103. {
  104. return (type & MMP_GPIO) != 0;
  105. }
  106. /* GPIO86/87/88/89 on PXA26x have their direction bits in PXA_GPDR(2 inverted,
  107. * as well as their Alternate Function value being '1' for GPIO in GAFRx.
  108. */
  109. static inline int __gpio_is_inverted(int gpio)
  110. {
  111. if ((gpio_type == PXA26X_GPIO) && (gpio > 85))
  112. return 1;
  113. return 0;
  114. }
  115. /*
  116. * On PXA25x and PXA27x, GAFRx and GPDRx together decide the alternate
  117. * function of a GPIO, and GPDRx cannot be altered once configured. It
  118. * is attributed as "occupied" here (I know this terminology isn't
  119. * accurate, you are welcome to propose a better one :-)
  120. */
  121. static inline int __gpio_is_occupied(unsigned gpio)
  122. {
  123. struct pxa_gpio_chip *pxachip;
  124. void __iomem *base;
  125. unsigned long gafr = 0, gpdr = 0;
  126. int ret, af = 0, dir = 0;
  127. pxachip = gpio_to_pxachip(gpio);
  128. base = gpio_chip_base(&pxachip->chip);
  129. gpdr = readl_relaxed(base + GPDR_OFFSET);
  130. switch (gpio_type) {
  131. case PXA25X_GPIO:
  132. case PXA26X_GPIO:
  133. case PXA27X_GPIO:
  134. gafr = readl_relaxed(base + GAFR_OFFSET);
  135. af = (gafr >> ((gpio & 0xf) * 2)) & 0x3;
  136. dir = gpdr & GPIO_bit(gpio);
  137. if (__gpio_is_inverted(gpio))
  138. ret = (af != 1) || (dir == 0);
  139. else
  140. ret = (af != 0) || (dir != 0);
  141. break;
  142. default:
  143. ret = gpdr & GPIO_bit(gpio);
  144. break;
  145. }
  146. return ret;
  147. }
  148. #ifdef CONFIG_ARCH_PXA
  149. static inline int __pxa_gpio_to_irq(int gpio)
  150. {
  151. if (gpio_is_pxa_type(gpio_type))
  152. return PXA_GPIO_TO_IRQ(gpio);
  153. return -1;
  154. }
  155. static inline int __pxa_irq_to_gpio(int irq)
  156. {
  157. if (gpio_is_pxa_type(gpio_type))
  158. return irq - PXA_GPIO_TO_IRQ(0);
  159. return -1;
  160. }
  161. #else
  162. static inline int __pxa_gpio_to_irq(int gpio) { return -1; }
  163. static inline int __pxa_irq_to_gpio(int irq) { return -1; }
  164. #endif
  165. #ifdef CONFIG_ARCH_MMP
  166. static inline int __mmp_gpio_to_irq(int gpio)
  167. {
  168. if (gpio_is_mmp_type(gpio_type))
  169. return MMP_GPIO_TO_IRQ(gpio);
  170. return -1;
  171. }
  172. static inline int __mmp_irq_to_gpio(int irq)
  173. {
  174. if (gpio_is_mmp_type(gpio_type))
  175. return irq - MMP_GPIO_TO_IRQ(0);
  176. return -1;
  177. }
  178. #else
  179. static inline int __mmp_gpio_to_irq(int gpio) { return -1; }
  180. static inline int __mmp_irq_to_gpio(int irq) { return -1; }
  181. #endif
  182. static int pxa_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  183. {
  184. int gpio, ret;
  185. gpio = chip->base + offset;
  186. ret = __pxa_gpio_to_irq(gpio);
  187. if (ret >= 0)
  188. return ret;
  189. return __mmp_gpio_to_irq(gpio);
  190. }
  191. int pxa_irq_to_gpio(int irq)
  192. {
  193. int ret;
  194. ret = __pxa_irq_to_gpio(irq);
  195. if (ret >= 0)
  196. return ret;
  197. return __mmp_irq_to_gpio(irq);
  198. }
  199. static int pxa_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  200. {
  201. void __iomem *base = gpio_chip_base(chip);
  202. uint32_t value, mask = 1 << offset;
  203. unsigned long flags;
  204. spin_lock_irqsave(&gpio_lock, flags);
  205. value = readl_relaxed(base + GPDR_OFFSET);
  206. if (__gpio_is_inverted(chip->base + offset))
  207. value |= mask;
  208. else
  209. value &= ~mask;
  210. writel_relaxed(value, base + GPDR_OFFSET);
  211. spin_unlock_irqrestore(&gpio_lock, flags);
  212. return 0;
  213. }
  214. static int pxa_gpio_direction_output(struct gpio_chip *chip,
  215. unsigned offset, int value)
  216. {
  217. void __iomem *base = gpio_chip_base(chip);
  218. uint32_t tmp, mask = 1 << offset;
  219. unsigned long flags;
  220. writel_relaxed(mask, base + (value ? GPSR_OFFSET : GPCR_OFFSET));
  221. spin_lock_irqsave(&gpio_lock, flags);
  222. tmp = readl_relaxed(base + GPDR_OFFSET);
  223. if (__gpio_is_inverted(chip->base + offset))
  224. tmp &= ~mask;
  225. else
  226. tmp |= mask;
  227. writel_relaxed(tmp, base + GPDR_OFFSET);
  228. spin_unlock_irqrestore(&gpio_lock, flags);
  229. return 0;
  230. }
  231. static int pxa_gpio_get(struct gpio_chip *chip, unsigned offset)
  232. {
  233. return readl_relaxed(gpio_chip_base(chip) + GPLR_OFFSET) & (1 << offset);
  234. }
  235. static void pxa_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  236. {
  237. writel_relaxed(1 << offset, gpio_chip_base(chip) +
  238. (value ? GPSR_OFFSET : GPCR_OFFSET));
  239. }
  240. #ifdef CONFIG_OF_GPIO
  241. static int pxa_gpio_of_xlate(struct gpio_chip *gc,
  242. const struct of_phandle_args *gpiospec,
  243. u32 *flags)
  244. {
  245. if (gpiospec->args[0] > pxa_last_gpio)
  246. return -EINVAL;
  247. if (gc != &pxa_gpio_chips[gpiospec->args[0] / 32].chip)
  248. return -EINVAL;
  249. if (flags)
  250. *flags = gpiospec->args[1];
  251. return gpiospec->args[0] % 32;
  252. }
  253. #endif
  254. static int __devinit pxa_init_gpio_chip(int gpio_end,
  255. int (*set_wake)(unsigned int, unsigned int))
  256. {
  257. int i, gpio, nbanks = gpio_to_bank(gpio_end) + 1;
  258. struct pxa_gpio_chip *chips;
  259. chips = kzalloc(nbanks * sizeof(struct pxa_gpio_chip), GFP_KERNEL);
  260. if (chips == NULL) {
  261. pr_err("%s: failed to allocate GPIO chips\n", __func__);
  262. return -ENOMEM;
  263. }
  264. for (i = 0, gpio = 0; i < nbanks; i++, gpio += 32) {
  265. struct gpio_chip *c = &chips[i].chip;
  266. sprintf(chips[i].label, "gpio-%d", i);
  267. chips[i].regbase = gpio_reg_base + BANK_OFF(i);
  268. chips[i].set_wake = set_wake;
  269. c->base = gpio;
  270. c->label = chips[i].label;
  271. c->direction_input = pxa_gpio_direction_input;
  272. c->direction_output = pxa_gpio_direction_output;
  273. c->get = pxa_gpio_get;
  274. c->set = pxa_gpio_set;
  275. c->to_irq = pxa_gpio_to_irq;
  276. #ifdef CONFIG_OF_GPIO
  277. c->of_node = pxa_gpio_of_node;
  278. c->of_xlate = pxa_gpio_of_xlate;
  279. c->of_gpio_n_cells = 2;
  280. #endif
  281. /* number of GPIOs on last bank may be less than 32 */
  282. c->ngpio = (gpio + 31 > gpio_end) ? (gpio_end - gpio + 1) : 32;
  283. gpiochip_add(c);
  284. }
  285. pxa_gpio_chips = chips;
  286. return 0;
  287. }
  288. /* Update only those GRERx and GFERx edge detection register bits if those
  289. * bits are set in c->irq_mask
  290. */
  291. static inline void update_edge_detect(struct pxa_gpio_chip *c)
  292. {
  293. uint32_t grer, gfer;
  294. grer = readl_relaxed(c->regbase + GRER_OFFSET) & ~c->irq_mask;
  295. gfer = readl_relaxed(c->regbase + GFER_OFFSET) & ~c->irq_mask;
  296. grer |= c->irq_edge_rise & c->irq_mask;
  297. gfer |= c->irq_edge_fall & c->irq_mask;
  298. writel_relaxed(grer, c->regbase + GRER_OFFSET);
  299. writel_relaxed(gfer, c->regbase + GFER_OFFSET);
  300. }
  301. static int pxa_gpio_irq_type(struct irq_data *d, unsigned int type)
  302. {
  303. struct pxa_gpio_chip *c;
  304. int gpio = pxa_irq_to_gpio(d->irq);
  305. unsigned long gpdr, mask = GPIO_bit(gpio);
  306. c = gpio_to_pxachip(gpio);
  307. if (type == IRQ_TYPE_PROBE) {
  308. /* Don't mess with enabled GPIOs using preconfigured edges or
  309. * GPIOs set to alternate function or to output during probe
  310. */
  311. if ((c->irq_edge_rise | c->irq_edge_fall) & GPIO_bit(gpio))
  312. return 0;
  313. if (__gpio_is_occupied(gpio))
  314. return 0;
  315. type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
  316. }
  317. gpdr = readl_relaxed(c->regbase + GPDR_OFFSET);
  318. if (__gpio_is_inverted(gpio))
  319. writel_relaxed(gpdr | mask, c->regbase + GPDR_OFFSET);
  320. else
  321. writel_relaxed(gpdr & ~mask, c->regbase + GPDR_OFFSET);
  322. if (type & IRQ_TYPE_EDGE_RISING)
  323. c->irq_edge_rise |= mask;
  324. else
  325. c->irq_edge_rise &= ~mask;
  326. if (type & IRQ_TYPE_EDGE_FALLING)
  327. c->irq_edge_fall |= mask;
  328. else
  329. c->irq_edge_fall &= ~mask;
  330. update_edge_detect(c);
  331. pr_debug("%s: IRQ%d (GPIO%d) - edge%s%s\n", __func__, d->irq, gpio,
  332. ((type & IRQ_TYPE_EDGE_RISING) ? " rising" : ""),
  333. ((type & IRQ_TYPE_EDGE_FALLING) ? " falling" : ""));
  334. return 0;
  335. }
  336. static void pxa_gpio_demux_handler(unsigned int irq, struct irq_desc *desc)
  337. {
  338. struct pxa_gpio_chip *c;
  339. int loop, gpio, gpio_base, n;
  340. unsigned long gedr;
  341. do {
  342. loop = 0;
  343. for_each_gpio_chip(gpio, c) {
  344. gpio_base = c->chip.base;
  345. gedr = readl_relaxed(c->regbase + GEDR_OFFSET);
  346. gedr = gedr & c->irq_mask;
  347. writel_relaxed(gedr, c->regbase + GEDR_OFFSET);
  348. for_each_set_bit(n, &gedr, BITS_PER_LONG) {
  349. loop = 1;
  350. generic_handle_irq(gpio_to_irq(gpio_base + n));
  351. }
  352. }
  353. } while (loop);
  354. }
  355. static void pxa_ack_muxed_gpio(struct irq_data *d)
  356. {
  357. int gpio = pxa_irq_to_gpio(d->irq);
  358. struct pxa_gpio_chip *c = gpio_to_pxachip(gpio);
  359. writel_relaxed(GPIO_bit(gpio), c->regbase + GEDR_OFFSET);
  360. }
  361. static void pxa_mask_muxed_gpio(struct irq_data *d)
  362. {
  363. int gpio = pxa_irq_to_gpio(d->irq);
  364. struct pxa_gpio_chip *c = gpio_to_pxachip(gpio);
  365. uint32_t grer, gfer;
  366. c->irq_mask &= ~GPIO_bit(gpio);
  367. grer = readl_relaxed(c->regbase + GRER_OFFSET) & ~GPIO_bit(gpio);
  368. gfer = readl_relaxed(c->regbase + GFER_OFFSET) & ~GPIO_bit(gpio);
  369. writel_relaxed(grer, c->regbase + GRER_OFFSET);
  370. writel_relaxed(gfer, c->regbase + GFER_OFFSET);
  371. }
  372. static int pxa_gpio_set_wake(struct irq_data *d, unsigned int on)
  373. {
  374. int gpio = pxa_irq_to_gpio(d->irq);
  375. struct pxa_gpio_chip *c = gpio_to_pxachip(gpio);
  376. if (c->set_wake)
  377. return c->set_wake(gpio, on);
  378. else
  379. return 0;
  380. }
  381. static void pxa_unmask_muxed_gpio(struct irq_data *d)
  382. {
  383. int gpio = pxa_irq_to_gpio(d->irq);
  384. struct pxa_gpio_chip *c = gpio_to_pxachip(gpio);
  385. c->irq_mask |= GPIO_bit(gpio);
  386. update_edge_detect(c);
  387. }
  388. static struct irq_chip pxa_muxed_gpio_chip = {
  389. .name = "GPIO",
  390. .irq_ack = pxa_ack_muxed_gpio,
  391. .irq_mask = pxa_mask_muxed_gpio,
  392. .irq_unmask = pxa_unmask_muxed_gpio,
  393. .irq_set_type = pxa_gpio_irq_type,
  394. .irq_set_wake = pxa_gpio_set_wake,
  395. };
  396. static int pxa_gpio_nums(void)
  397. {
  398. int count = 0;
  399. #ifdef CONFIG_ARCH_PXA
  400. if (cpu_is_pxa25x()) {
  401. #ifdef CONFIG_CPU_PXA26x
  402. count = 89;
  403. gpio_type = PXA26X_GPIO;
  404. #elif defined(CONFIG_PXA25x)
  405. count = 84;
  406. gpio_type = PXA26X_GPIO;
  407. #endif /* CONFIG_CPU_PXA26x */
  408. } else if (cpu_is_pxa27x()) {
  409. count = 120;
  410. gpio_type = PXA27X_GPIO;
  411. } else if (cpu_is_pxa93x() || cpu_is_pxa95x()) {
  412. count = 191;
  413. gpio_type = PXA93X_GPIO;
  414. } else if (cpu_is_pxa3xx()) {
  415. count = 127;
  416. gpio_type = PXA3XX_GPIO;
  417. }
  418. #endif /* CONFIG_ARCH_PXA */
  419. #ifdef CONFIG_ARCH_MMP
  420. if (cpu_is_pxa168() || cpu_is_pxa910()) {
  421. count = 127;
  422. gpio_type = MMP_GPIO;
  423. } else if (cpu_is_mmp2()) {
  424. count = 191;
  425. gpio_type = MMP_GPIO;
  426. }
  427. #endif /* CONFIG_ARCH_MMP */
  428. return count;
  429. }
  430. #ifdef CONFIG_OF
  431. static struct of_device_id pxa_gpio_dt_ids[] = {
  432. { .compatible = "mrvl,pxa-gpio" },
  433. { .compatible = "mrvl,mmp-gpio", .data = (void *)MMP_GPIO },
  434. {}
  435. };
  436. static int pxa_irq_domain_map(struct irq_domain *d, unsigned int irq,
  437. irq_hw_number_t hw)
  438. {
  439. irq_set_chip_and_handler(irq, &pxa_muxed_gpio_chip,
  440. handle_edge_irq);
  441. set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
  442. return 0;
  443. }
  444. const struct irq_domain_ops pxa_irq_domain_ops = {
  445. .map = pxa_irq_domain_map,
  446. .xlate = irq_domain_xlate_twocell,
  447. };
  448. static int __devinit pxa_gpio_probe_dt(struct platform_device *pdev)
  449. {
  450. int ret, nr_banks, nr_gpios, irq_base;
  451. struct device_node *prev, *next, *np = pdev->dev.of_node;
  452. const struct of_device_id *of_id =
  453. of_match_device(pxa_gpio_dt_ids, &pdev->dev);
  454. if (!of_id) {
  455. dev_err(&pdev->dev, "Failed to find gpio controller\n");
  456. return -EFAULT;
  457. }
  458. gpio_type = (int)of_id->data;
  459. next = of_get_next_child(np, NULL);
  460. prev = next;
  461. if (!next) {
  462. dev_err(&pdev->dev, "Failed to find child gpio node\n");
  463. ret = -EINVAL;
  464. goto err;
  465. }
  466. for (nr_banks = 1; ; nr_banks++) {
  467. next = of_get_next_child(np, prev);
  468. if (!next)
  469. break;
  470. prev = next;
  471. }
  472. of_node_put(prev);
  473. nr_gpios = nr_banks << 5;
  474. pxa_last_gpio = nr_gpios - 1;
  475. irq_base = irq_alloc_descs(-1, 0, nr_gpios, 0);
  476. if (irq_base < 0) {
  477. dev_err(&pdev->dev, "Failed to allocate IRQ numbers\n");
  478. goto err;
  479. }
  480. domain = irq_domain_add_legacy(np, nr_gpios, irq_base, 0,
  481. &pxa_irq_domain_ops, NULL);
  482. pxa_gpio_of_node = np;
  483. return 0;
  484. err:
  485. iounmap(gpio_reg_base);
  486. return ret;
  487. }
  488. #else
  489. #define pxa_gpio_probe_dt(pdev) (-1)
  490. #endif
  491. static int __devinit pxa_gpio_probe(struct platform_device *pdev)
  492. {
  493. struct pxa_gpio_chip *c;
  494. struct resource *res;
  495. struct clk *clk;
  496. struct pxa_gpio_platform_data *info;
  497. int gpio, irq, ret, use_of = 0;
  498. int irq0 = 0, irq1 = 0, irq_mux, gpio_offset = 0;
  499. ret = pxa_gpio_probe_dt(pdev);
  500. if (ret < 0)
  501. pxa_last_gpio = pxa_gpio_nums();
  502. else
  503. use_of = 1;
  504. if (!pxa_last_gpio)
  505. return -EINVAL;
  506. irq0 = platform_get_irq_byname(pdev, "gpio0");
  507. irq1 = platform_get_irq_byname(pdev, "gpio1");
  508. irq_mux = platform_get_irq_byname(pdev, "gpio_mux");
  509. if ((irq0 > 0 && irq1 <= 0) || (irq0 <= 0 && irq1 > 0)
  510. || (irq_mux <= 0))
  511. return -EINVAL;
  512. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  513. if (!res)
  514. return -EINVAL;
  515. gpio_reg_base = ioremap(res->start, resource_size(res));
  516. if (!gpio_reg_base)
  517. return -EINVAL;
  518. if (irq0 > 0)
  519. gpio_offset = 2;
  520. clk = clk_get(&pdev->dev, NULL);
  521. if (IS_ERR(clk)) {
  522. dev_err(&pdev->dev, "Error %ld to get gpio clock\n",
  523. PTR_ERR(clk));
  524. iounmap(gpio_reg_base);
  525. return PTR_ERR(clk);
  526. }
  527. ret = clk_prepare_enable(clk);
  528. if (ret) {
  529. clk_put(clk);
  530. iounmap(gpio_reg_base);
  531. return ret;
  532. }
  533. /* Initialize GPIO chips */
  534. info = dev_get_platdata(&pdev->dev);
  535. pxa_init_gpio_chip(pxa_last_gpio, info ? info->gpio_set_wake : NULL);
  536. /* clear all GPIO edge detects */
  537. for_each_gpio_chip(gpio, c) {
  538. writel_relaxed(0, c->regbase + GFER_OFFSET);
  539. writel_relaxed(0, c->regbase + GRER_OFFSET);
  540. writel_relaxed(~0,c->regbase + GEDR_OFFSET);
  541. /* unmask GPIO edge detect for AP side */
  542. if (gpio_is_mmp_type(gpio_type))
  543. writel_relaxed(~0, c->regbase + ED_MASK_OFFSET);
  544. }
  545. if (!use_of) {
  546. #ifdef CONFIG_ARCH_PXA
  547. irq = gpio_to_irq(0);
  548. irq_set_chip_and_handler(irq, &pxa_muxed_gpio_chip,
  549. handle_edge_irq);
  550. set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
  551. irq_set_chained_handler(IRQ_GPIO0, pxa_gpio_demux_handler);
  552. irq = gpio_to_irq(1);
  553. irq_set_chip_and_handler(irq, &pxa_muxed_gpio_chip,
  554. handle_edge_irq);
  555. set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
  556. irq_set_chained_handler(IRQ_GPIO1, pxa_gpio_demux_handler);
  557. #endif
  558. for (irq = gpio_to_irq(gpio_offset);
  559. irq <= gpio_to_irq(pxa_last_gpio); irq++) {
  560. irq_set_chip_and_handler(irq, &pxa_muxed_gpio_chip,
  561. handle_edge_irq);
  562. set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
  563. }
  564. }
  565. irq_set_chained_handler(irq_mux, pxa_gpio_demux_handler);
  566. return 0;
  567. }
  568. static struct platform_driver pxa_gpio_driver = {
  569. .probe = pxa_gpio_probe,
  570. .driver = {
  571. .name = "pxa-gpio",
  572. .of_match_table = of_match_ptr(pxa_gpio_dt_ids),
  573. },
  574. };
  575. static int __init pxa_gpio_init(void)
  576. {
  577. return platform_driver_register(&pxa_gpio_driver);
  578. }
  579. postcore_initcall(pxa_gpio_init);
  580. #ifdef CONFIG_PM
  581. static int pxa_gpio_suspend(void)
  582. {
  583. struct pxa_gpio_chip *c;
  584. int gpio;
  585. for_each_gpio_chip(gpio, c) {
  586. c->saved_gplr = readl_relaxed(c->regbase + GPLR_OFFSET);
  587. c->saved_gpdr = readl_relaxed(c->regbase + GPDR_OFFSET);
  588. c->saved_grer = readl_relaxed(c->regbase + GRER_OFFSET);
  589. c->saved_gfer = readl_relaxed(c->regbase + GFER_OFFSET);
  590. /* Clear GPIO transition detect bits */
  591. writel_relaxed(0xffffffff, c->regbase + GEDR_OFFSET);
  592. }
  593. return 0;
  594. }
  595. static void pxa_gpio_resume(void)
  596. {
  597. struct pxa_gpio_chip *c;
  598. int gpio;
  599. for_each_gpio_chip(gpio, c) {
  600. /* restore level with set/clear */
  601. writel_relaxed( c->saved_gplr, c->regbase + GPSR_OFFSET);
  602. writel_relaxed(~c->saved_gplr, c->regbase + GPCR_OFFSET);
  603. writel_relaxed(c->saved_grer, c->regbase + GRER_OFFSET);
  604. writel_relaxed(c->saved_gfer, c->regbase + GFER_OFFSET);
  605. writel_relaxed(c->saved_gpdr, c->regbase + GPDR_OFFSET);
  606. }
  607. }
  608. #else
  609. #define pxa_gpio_suspend NULL
  610. #define pxa_gpio_resume NULL
  611. #endif
  612. struct syscore_ops pxa_gpio_syscore_ops = {
  613. .suspend = pxa_gpio_suspend,
  614. .resume = pxa_gpio_resume,
  615. };
  616. static int __init pxa_gpio_sysinit(void)
  617. {
  618. register_syscore_ops(&pxa_gpio_syscore_ops);
  619. return 0;
  620. }
  621. postcore_initcall(pxa_gpio_sysinit);