gpio-pxa.c 18 KB

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