gpio-pxa.c 17 KB

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