gpio-pxa.c 18 KB

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