gpio-pxa.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  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. n = find_first_bit(&gedr, BITS_PER_LONG);
  349. while (n < BITS_PER_LONG) {
  350. loop = 1;
  351. generic_handle_irq(gpio_to_irq(gpio_base + n));
  352. n = find_next_bit(&gedr, BITS_PER_LONG, n + 1);
  353. }
  354. }
  355. } while (loop);
  356. }
  357. static void pxa_ack_muxed_gpio(struct irq_data *d)
  358. {
  359. int gpio = pxa_irq_to_gpio(d->irq);
  360. struct pxa_gpio_chip *c = gpio_to_pxachip(gpio);
  361. writel_relaxed(GPIO_bit(gpio), c->regbase + GEDR_OFFSET);
  362. }
  363. static void pxa_mask_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. uint32_t grer, gfer;
  368. c->irq_mask &= ~GPIO_bit(gpio);
  369. grer = readl_relaxed(c->regbase + GRER_OFFSET) & ~GPIO_bit(gpio);
  370. gfer = readl_relaxed(c->regbase + GFER_OFFSET) & ~GPIO_bit(gpio);
  371. writel_relaxed(grer, c->regbase + GRER_OFFSET);
  372. writel_relaxed(gfer, c->regbase + GFER_OFFSET);
  373. }
  374. static int pxa_gpio_set_wake(struct irq_data *d, unsigned int on)
  375. {
  376. int gpio = pxa_irq_to_gpio(d->irq);
  377. struct pxa_gpio_chip *c = gpio_to_pxachip(gpio);
  378. if (c->set_wake)
  379. return c->set_wake(gpio, on);
  380. else
  381. return 0;
  382. }
  383. static void pxa_unmask_muxed_gpio(struct irq_data *d)
  384. {
  385. int gpio = pxa_irq_to_gpio(d->irq);
  386. struct pxa_gpio_chip *c = gpio_to_pxachip(gpio);
  387. c->irq_mask |= GPIO_bit(gpio);
  388. update_edge_detect(c);
  389. }
  390. static struct irq_chip pxa_muxed_gpio_chip = {
  391. .name = "GPIO",
  392. .irq_ack = pxa_ack_muxed_gpio,
  393. .irq_mask = pxa_mask_muxed_gpio,
  394. .irq_unmask = pxa_unmask_muxed_gpio,
  395. .irq_set_type = pxa_gpio_irq_type,
  396. .irq_set_wake = pxa_gpio_set_wake,
  397. };
  398. static int pxa_gpio_nums(void)
  399. {
  400. int count = 0;
  401. #ifdef CONFIG_ARCH_PXA
  402. if (cpu_is_pxa25x()) {
  403. #ifdef CONFIG_CPU_PXA26x
  404. count = 89;
  405. gpio_type = PXA26X_GPIO;
  406. #elif defined(CONFIG_PXA25x)
  407. count = 84;
  408. gpio_type = PXA26X_GPIO;
  409. #endif /* CONFIG_CPU_PXA26x */
  410. } else if (cpu_is_pxa27x()) {
  411. count = 120;
  412. gpio_type = PXA27X_GPIO;
  413. } else if (cpu_is_pxa93x() || cpu_is_pxa95x()) {
  414. count = 191;
  415. gpio_type = PXA93X_GPIO;
  416. } else if (cpu_is_pxa3xx()) {
  417. count = 127;
  418. gpio_type = PXA3XX_GPIO;
  419. }
  420. #endif /* CONFIG_ARCH_PXA */
  421. #ifdef CONFIG_ARCH_MMP
  422. if (cpu_is_pxa168() || cpu_is_pxa910()) {
  423. count = 127;
  424. gpio_type = MMP_GPIO;
  425. } else if (cpu_is_mmp2()) {
  426. count = 191;
  427. gpio_type = MMP_GPIO;
  428. }
  429. #endif /* CONFIG_ARCH_MMP */
  430. return count;
  431. }
  432. static struct of_device_id pxa_gpio_dt_ids[] = {
  433. { .compatible = "mrvl,pxa-gpio" },
  434. { .compatible = "mrvl,mmp-gpio", .data = (void *)MMP_GPIO },
  435. {}
  436. };
  437. static int pxa_irq_domain_map(struct irq_domain *d, unsigned int irq,
  438. irq_hw_number_t hw)
  439. {
  440. irq_set_chip_and_handler(irq, &pxa_muxed_gpio_chip,
  441. handle_edge_irq);
  442. set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
  443. return 0;
  444. }
  445. const struct irq_domain_ops pxa_irq_domain_ops = {
  446. .map = pxa_irq_domain_map,
  447. .xlate = irq_domain_xlate_twocell,
  448. };
  449. #ifdef CONFIG_OF
  450. static int __devinit pxa_gpio_probe_dt(struct platform_device *pdev)
  451. {
  452. int ret, nr_banks, nr_gpios, irq_base;
  453. struct device_node *prev, *next, *np = pdev->dev.of_node;
  454. const struct of_device_id *of_id =
  455. of_match_device(pxa_gpio_dt_ids, &pdev->dev);
  456. if (!of_id) {
  457. dev_err(&pdev->dev, "Failed to find gpio controller\n");
  458. return -EFAULT;
  459. }
  460. gpio_type = (int)of_id->data;
  461. next = of_get_next_child(np, NULL);
  462. prev = next;
  463. if (!next) {
  464. dev_err(&pdev->dev, "Failed to find child gpio node\n");
  465. ret = -EINVAL;
  466. goto err;
  467. }
  468. for (nr_banks = 1; ; nr_banks++) {
  469. next = of_get_next_child(np, prev);
  470. if (!next)
  471. break;
  472. prev = next;
  473. }
  474. of_node_put(prev);
  475. nr_gpios = nr_banks << 5;
  476. pxa_last_gpio = nr_gpios - 1;
  477. irq_base = irq_alloc_descs(-1, 0, nr_gpios, 0);
  478. if (irq_base < 0) {
  479. dev_err(&pdev->dev, "Failed to allocate IRQ numbers\n");
  480. goto err;
  481. }
  482. domain = irq_domain_add_legacy(np, nr_gpios, irq_base, 0,
  483. &pxa_irq_domain_ops, NULL);
  484. pxa_gpio_of_node = np;
  485. return 0;
  486. err:
  487. iounmap(gpio_reg_base);
  488. return ret;
  489. }
  490. #else
  491. #define pxa_gpio_probe_dt(pdev) (-1)
  492. #endif
  493. static int __devinit pxa_gpio_probe(struct platform_device *pdev)
  494. {
  495. struct pxa_gpio_chip *c;
  496. struct resource *res;
  497. struct clk *clk;
  498. struct pxa_gpio_platform_data *info;
  499. int gpio, irq, ret, use_of = 0;
  500. int irq0 = 0, irq1 = 0, irq_mux, gpio_offset = 0;
  501. ret = pxa_gpio_probe_dt(pdev);
  502. if (ret < 0)
  503. pxa_last_gpio = pxa_gpio_nums();
  504. else
  505. use_of = 1;
  506. if (!pxa_last_gpio)
  507. return -EINVAL;
  508. irq0 = platform_get_irq_byname(pdev, "gpio0");
  509. irq1 = platform_get_irq_byname(pdev, "gpio1");
  510. irq_mux = platform_get_irq_byname(pdev, "gpio_mux");
  511. if ((irq0 > 0 && irq1 <= 0) || (irq0 <= 0 && irq1 > 0)
  512. || (irq_mux <= 0))
  513. return -EINVAL;
  514. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  515. if (!res)
  516. return -EINVAL;
  517. gpio_reg_base = ioremap(res->start, resource_size(res));
  518. if (!gpio_reg_base)
  519. return -EINVAL;
  520. if (irq0 > 0)
  521. gpio_offset = 2;
  522. clk = clk_get(&pdev->dev, NULL);
  523. if (IS_ERR(clk)) {
  524. dev_err(&pdev->dev, "Error %ld to get gpio clock\n",
  525. PTR_ERR(clk));
  526. iounmap(gpio_reg_base);
  527. return PTR_ERR(clk);
  528. }
  529. ret = clk_prepare(clk);
  530. if (ret) {
  531. clk_put(clk);
  532. iounmap(gpio_reg_base);
  533. return ret;
  534. }
  535. ret = clk_enable(clk);
  536. if (ret) {
  537. clk_unprepare(clk);
  538. clk_put(clk);
  539. iounmap(gpio_reg_base);
  540. return ret;
  541. }
  542. /* Initialize GPIO chips */
  543. info = dev_get_platdata(&pdev->dev);
  544. pxa_init_gpio_chip(pxa_last_gpio, info ? info->gpio_set_wake : NULL);
  545. /* clear all GPIO edge detects */
  546. for_each_gpio_chip(gpio, c) {
  547. writel_relaxed(0, c->regbase + GFER_OFFSET);
  548. writel_relaxed(0, c->regbase + GRER_OFFSET);
  549. writel_relaxed(~0,c->regbase + GEDR_OFFSET);
  550. /* unmask GPIO edge detect for AP side */
  551. if (gpio_is_mmp_type(gpio_type))
  552. writel_relaxed(~0, c->regbase + ED_MASK_OFFSET);
  553. }
  554. if (!use_of) {
  555. #ifdef CONFIG_ARCH_PXA
  556. irq = gpio_to_irq(0);
  557. irq_set_chip_and_handler(irq, &pxa_muxed_gpio_chip,
  558. handle_edge_irq);
  559. set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
  560. irq_set_chained_handler(IRQ_GPIO0, pxa_gpio_demux_handler);
  561. irq = gpio_to_irq(1);
  562. irq_set_chip_and_handler(irq, &pxa_muxed_gpio_chip,
  563. handle_edge_irq);
  564. set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
  565. irq_set_chained_handler(IRQ_GPIO1, pxa_gpio_demux_handler);
  566. #endif
  567. for (irq = gpio_to_irq(gpio_offset);
  568. irq <= gpio_to_irq(pxa_last_gpio); irq++) {
  569. irq_set_chip_and_handler(irq, &pxa_muxed_gpio_chip,
  570. handle_edge_irq);
  571. set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
  572. }
  573. }
  574. irq_set_chained_handler(irq_mux, pxa_gpio_demux_handler);
  575. return 0;
  576. }
  577. static struct platform_driver pxa_gpio_driver = {
  578. .probe = pxa_gpio_probe,
  579. .driver = {
  580. .name = "pxa-gpio",
  581. .of_match_table = pxa_gpio_dt_ids,
  582. },
  583. };
  584. static int __init pxa_gpio_init(void)
  585. {
  586. return platform_driver_register(&pxa_gpio_driver);
  587. }
  588. postcore_initcall(pxa_gpio_init);
  589. #ifdef CONFIG_PM
  590. static int pxa_gpio_suspend(void)
  591. {
  592. struct pxa_gpio_chip *c;
  593. int gpio;
  594. for_each_gpio_chip(gpio, c) {
  595. c->saved_gplr = readl_relaxed(c->regbase + GPLR_OFFSET);
  596. c->saved_gpdr = readl_relaxed(c->regbase + GPDR_OFFSET);
  597. c->saved_grer = readl_relaxed(c->regbase + GRER_OFFSET);
  598. c->saved_gfer = readl_relaxed(c->regbase + GFER_OFFSET);
  599. /* Clear GPIO transition detect bits */
  600. writel_relaxed(0xffffffff, c->regbase + GEDR_OFFSET);
  601. }
  602. return 0;
  603. }
  604. static void pxa_gpio_resume(void)
  605. {
  606. struct pxa_gpio_chip *c;
  607. int gpio;
  608. for_each_gpio_chip(gpio, c) {
  609. /* restore level with set/clear */
  610. writel_relaxed( c->saved_gplr, c->regbase + GPSR_OFFSET);
  611. writel_relaxed(~c->saved_gplr, c->regbase + GPCR_OFFSET);
  612. writel_relaxed(c->saved_grer, c->regbase + GRER_OFFSET);
  613. writel_relaxed(c->saved_gfer, c->regbase + GFER_OFFSET);
  614. writel_relaxed(c->saved_gpdr, c->regbase + GPDR_OFFSET);
  615. }
  616. }
  617. #else
  618. #define pxa_gpio_suspend NULL
  619. #define pxa_gpio_resume NULL
  620. #endif
  621. struct syscore_ops pxa_gpio_syscore_ops = {
  622. .suspend = pxa_gpio_suspend,
  623. .resume = pxa_gpio_resume,
  624. };
  625. static int __init pxa_gpio_sysinit(void)
  626. {
  627. register_syscore_ops(&pxa_gpio_syscore_ops);
  628. return 0;
  629. }
  630. postcore_initcall(pxa_gpio_sysinit);