gpio-pxa.c 17 KB

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