gpio-pxa.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  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. u32 gplr = readl_relaxed(gpio_chip_base(chip) + GPLR_OFFSET);
  226. return !!(gplr & (1 << offset));
  227. }
  228. static void pxa_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  229. {
  230. writel_relaxed(1 << offset, gpio_chip_base(chip) +
  231. (value ? GPSR_OFFSET : GPCR_OFFSET));
  232. }
  233. #ifdef CONFIG_OF_GPIO
  234. static int pxa_gpio_of_xlate(struct gpio_chip *gc,
  235. const struct of_phandle_args *gpiospec,
  236. u32 *flags)
  237. {
  238. if (gpiospec->args[0] > pxa_last_gpio)
  239. return -EINVAL;
  240. if (gc != &pxa_gpio_chips[gpiospec->args[0] / 32].chip)
  241. return -EINVAL;
  242. if (flags)
  243. *flags = gpiospec->args[1];
  244. return gpiospec->args[0] % 32;
  245. }
  246. #endif
  247. static int pxa_init_gpio_chip(int gpio_end,
  248. int (*set_wake)(unsigned int, unsigned int))
  249. {
  250. int i, gpio, nbanks = gpio_to_bank(gpio_end) + 1;
  251. struct pxa_gpio_chip *chips;
  252. chips = kzalloc(nbanks * sizeof(struct pxa_gpio_chip), GFP_KERNEL);
  253. if (chips == NULL) {
  254. pr_err("%s: failed to allocate GPIO chips\n", __func__);
  255. return -ENOMEM;
  256. }
  257. for (i = 0, gpio = 0; i < nbanks; i++, gpio += 32) {
  258. struct gpio_chip *c = &chips[i].chip;
  259. sprintf(chips[i].label, "gpio-%d", i);
  260. chips[i].regbase = gpio_reg_base + BANK_OFF(i);
  261. chips[i].set_wake = set_wake;
  262. c->base = gpio;
  263. c->label = chips[i].label;
  264. c->direction_input = pxa_gpio_direction_input;
  265. c->direction_output = pxa_gpio_direction_output;
  266. c->get = pxa_gpio_get;
  267. c->set = pxa_gpio_set;
  268. c->to_irq = pxa_gpio_to_irq;
  269. #ifdef CONFIG_OF_GPIO
  270. c->of_node = pxa_gpio_of_node;
  271. c->of_xlate = pxa_gpio_of_xlate;
  272. c->of_gpio_n_cells = 2;
  273. #endif
  274. /* number of GPIOs on last bank may be less than 32 */
  275. c->ngpio = (gpio + 31 > gpio_end) ? (gpio_end - gpio + 1) : 32;
  276. gpiochip_add(c);
  277. }
  278. pxa_gpio_chips = chips;
  279. return 0;
  280. }
  281. /* Update only those GRERx and GFERx edge detection register bits if those
  282. * bits are set in c->irq_mask
  283. */
  284. static inline void update_edge_detect(struct pxa_gpio_chip *c)
  285. {
  286. uint32_t grer, gfer;
  287. grer = readl_relaxed(c->regbase + GRER_OFFSET) & ~c->irq_mask;
  288. gfer = readl_relaxed(c->regbase + GFER_OFFSET) & ~c->irq_mask;
  289. grer |= c->irq_edge_rise & c->irq_mask;
  290. gfer |= c->irq_edge_fall & c->irq_mask;
  291. writel_relaxed(grer, c->regbase + GRER_OFFSET);
  292. writel_relaxed(gfer, c->regbase + GFER_OFFSET);
  293. }
  294. static int pxa_gpio_irq_type(struct irq_data *d, unsigned int type)
  295. {
  296. struct pxa_gpio_chip *c;
  297. int gpio = pxa_irq_to_gpio(d->irq);
  298. unsigned long gpdr, mask = GPIO_bit(gpio);
  299. c = gpio_to_pxachip(gpio);
  300. if (type == IRQ_TYPE_PROBE) {
  301. /* Don't mess with enabled GPIOs using preconfigured edges or
  302. * GPIOs set to alternate function or to output during probe
  303. */
  304. if ((c->irq_edge_rise | c->irq_edge_fall) & GPIO_bit(gpio))
  305. return 0;
  306. if (__gpio_is_occupied(gpio))
  307. return 0;
  308. type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
  309. }
  310. gpdr = readl_relaxed(c->regbase + GPDR_OFFSET);
  311. if (__gpio_is_inverted(gpio))
  312. writel_relaxed(gpdr | mask, c->regbase + GPDR_OFFSET);
  313. else
  314. writel_relaxed(gpdr & ~mask, c->regbase + GPDR_OFFSET);
  315. if (type & IRQ_TYPE_EDGE_RISING)
  316. c->irq_edge_rise |= mask;
  317. else
  318. c->irq_edge_rise &= ~mask;
  319. if (type & IRQ_TYPE_EDGE_FALLING)
  320. c->irq_edge_fall |= mask;
  321. else
  322. c->irq_edge_fall &= ~mask;
  323. update_edge_detect(c);
  324. pr_debug("%s: IRQ%d (GPIO%d) - edge%s%s\n", __func__, d->irq, gpio,
  325. ((type & IRQ_TYPE_EDGE_RISING) ? " rising" : ""),
  326. ((type & IRQ_TYPE_EDGE_FALLING) ? " falling" : ""));
  327. return 0;
  328. }
  329. static void pxa_gpio_demux_handler(unsigned int irq, struct irq_desc *desc)
  330. {
  331. struct pxa_gpio_chip *c;
  332. int loop, gpio, gpio_base, n;
  333. unsigned long gedr;
  334. struct irq_chip *chip = irq_desc_get_chip(desc);
  335. chained_irq_enter(chip, desc);
  336. do {
  337. loop = 0;
  338. for_each_gpio_chip(gpio, c) {
  339. gpio_base = c->chip.base;
  340. gedr = readl_relaxed(c->regbase + GEDR_OFFSET);
  341. gedr = gedr & c->irq_mask;
  342. writel_relaxed(gedr, c->regbase + GEDR_OFFSET);
  343. for_each_set_bit(n, &gedr, BITS_PER_LONG) {
  344. loop = 1;
  345. generic_handle_irq(gpio_to_irq(gpio_base + n));
  346. }
  347. }
  348. } while (loop);
  349. chained_irq_exit(chip, desc);
  350. }
  351. static void pxa_ack_muxed_gpio(struct irq_data *d)
  352. {
  353. int gpio = pxa_irq_to_gpio(d->irq);
  354. struct pxa_gpio_chip *c = gpio_to_pxachip(gpio);
  355. writel_relaxed(GPIO_bit(gpio), c->regbase + GEDR_OFFSET);
  356. }
  357. static void pxa_mask_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. uint32_t grer, gfer;
  362. c->irq_mask &= ~GPIO_bit(gpio);
  363. grer = readl_relaxed(c->regbase + GRER_OFFSET) & ~GPIO_bit(gpio);
  364. gfer = readl_relaxed(c->regbase + GFER_OFFSET) & ~GPIO_bit(gpio);
  365. writel_relaxed(grer, c->regbase + GRER_OFFSET);
  366. writel_relaxed(gfer, c->regbase + GFER_OFFSET);
  367. }
  368. static int pxa_gpio_set_wake(struct irq_data *d, unsigned int on)
  369. {
  370. int gpio = pxa_irq_to_gpio(d->irq);
  371. struct pxa_gpio_chip *c = gpio_to_pxachip(gpio);
  372. if (c->set_wake)
  373. return c->set_wake(gpio, on);
  374. else
  375. return 0;
  376. }
  377. static void pxa_unmask_muxed_gpio(struct irq_data *d)
  378. {
  379. int gpio = pxa_irq_to_gpio(d->irq);
  380. struct pxa_gpio_chip *c = gpio_to_pxachip(gpio);
  381. c->irq_mask |= GPIO_bit(gpio);
  382. update_edge_detect(c);
  383. }
  384. static struct irq_chip pxa_muxed_gpio_chip = {
  385. .name = "GPIO",
  386. .irq_ack = pxa_ack_muxed_gpio,
  387. .irq_mask = pxa_mask_muxed_gpio,
  388. .irq_unmask = pxa_unmask_muxed_gpio,
  389. .irq_set_type = pxa_gpio_irq_type,
  390. .irq_set_wake = pxa_gpio_set_wake,
  391. };
  392. static int pxa_gpio_nums(struct platform_device *pdev)
  393. {
  394. const struct platform_device_id *id = platform_get_device_id(pdev);
  395. struct pxa_gpio_id *pxa_id = (struct pxa_gpio_id *)id->driver_data;
  396. int count = 0;
  397. switch (pxa_id->type) {
  398. case PXA25X_GPIO:
  399. case PXA26X_GPIO:
  400. case PXA27X_GPIO:
  401. case PXA3XX_GPIO:
  402. case PXA93X_GPIO:
  403. case MMP_GPIO:
  404. case MMP2_GPIO:
  405. gpio_type = pxa_id->type;
  406. count = pxa_id->gpio_nums - 1;
  407. break;
  408. default:
  409. count = -EINVAL;
  410. break;
  411. }
  412. return count;
  413. }
  414. #ifdef CONFIG_OF
  415. static struct of_device_id pxa_gpio_dt_ids[] = {
  416. { .compatible = "intel,pxa25x-gpio", .data = &pxa25x_id, },
  417. { .compatible = "intel,pxa26x-gpio", .data = &pxa26x_id, },
  418. { .compatible = "intel,pxa27x-gpio", .data = &pxa27x_id, },
  419. { .compatible = "intel,pxa3xx-gpio", .data = &pxa3xx_id, },
  420. { .compatible = "marvell,pxa93x-gpio", .data = &pxa93x_id, },
  421. { .compatible = "marvell,mmp-gpio", .data = &mmp_id, },
  422. { .compatible = "marvell,mmp2-gpio", .data = &mmp2_id, },
  423. {}
  424. };
  425. static int pxa_irq_domain_map(struct irq_domain *d, unsigned int irq,
  426. irq_hw_number_t hw)
  427. {
  428. irq_set_chip_and_handler(irq, &pxa_muxed_gpio_chip,
  429. handle_edge_irq);
  430. set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
  431. return 0;
  432. }
  433. const struct irq_domain_ops pxa_irq_domain_ops = {
  434. .map = pxa_irq_domain_map,
  435. .xlate = irq_domain_xlate_twocell,
  436. };
  437. static int pxa_gpio_probe_dt(struct platform_device *pdev)
  438. {
  439. int ret = 0, nr_gpios;
  440. struct device_node *np = pdev->dev.of_node;
  441. const struct of_device_id *of_id =
  442. of_match_device(pxa_gpio_dt_ids, &pdev->dev);
  443. const struct pxa_gpio_id *gpio_id;
  444. if (!of_id || !of_id->data) {
  445. dev_err(&pdev->dev, "Failed to find gpio controller\n");
  446. return -EFAULT;
  447. }
  448. gpio_id = of_id->data;
  449. gpio_type = gpio_id->type;
  450. nr_gpios = gpio_id->gpio_nums;
  451. pxa_last_gpio = nr_gpios - 1;
  452. irq_base = irq_alloc_descs(-1, 0, nr_gpios, 0);
  453. if (irq_base < 0) {
  454. dev_err(&pdev->dev, "Failed to allocate IRQ numbers\n");
  455. ret = irq_base;
  456. goto err;
  457. }
  458. domain = irq_domain_add_legacy(np, nr_gpios, irq_base, 0,
  459. &pxa_irq_domain_ops, NULL);
  460. pxa_gpio_of_node = np;
  461. return 0;
  462. err:
  463. iounmap(gpio_reg_base);
  464. return ret;
  465. }
  466. #else
  467. #define pxa_gpio_probe_dt(pdev) (-1)
  468. #endif
  469. static int pxa_gpio_probe(struct platform_device *pdev)
  470. {
  471. struct pxa_gpio_chip *c;
  472. struct resource *res;
  473. struct clk *clk;
  474. struct pxa_gpio_platform_data *info;
  475. int gpio, irq, ret, use_of = 0;
  476. int irq0 = 0, irq1 = 0, irq_mux, gpio_offset = 0;
  477. info = dev_get_platdata(&pdev->dev);
  478. if (info) {
  479. irq_base = info->irq_base;
  480. if (irq_base <= 0)
  481. return -EINVAL;
  482. pxa_last_gpio = pxa_gpio_nums(pdev);
  483. } else {
  484. irq_base = 0;
  485. use_of = 1;
  486. ret = pxa_gpio_probe_dt(pdev);
  487. if (ret < 0)
  488. return -EINVAL;
  489. }
  490. if (!pxa_last_gpio)
  491. return -EINVAL;
  492. irq0 = platform_get_irq_byname(pdev, "gpio0");
  493. irq1 = platform_get_irq_byname(pdev, "gpio1");
  494. irq_mux = platform_get_irq_byname(pdev, "gpio_mux");
  495. if ((irq0 > 0 && irq1 <= 0) || (irq0 <= 0 && irq1 > 0)
  496. || (irq_mux <= 0))
  497. return -EINVAL;
  498. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  499. if (!res)
  500. return -EINVAL;
  501. gpio_reg_base = ioremap(res->start, resource_size(res));
  502. if (!gpio_reg_base)
  503. return -EINVAL;
  504. if (irq0 > 0)
  505. gpio_offset = 2;
  506. clk = clk_get(&pdev->dev, NULL);
  507. if (IS_ERR(clk)) {
  508. dev_err(&pdev->dev, "Error %ld to get gpio clock\n",
  509. PTR_ERR(clk));
  510. iounmap(gpio_reg_base);
  511. return PTR_ERR(clk);
  512. }
  513. ret = clk_prepare_enable(clk);
  514. if (ret) {
  515. clk_put(clk);
  516. iounmap(gpio_reg_base);
  517. return ret;
  518. }
  519. /* Initialize GPIO chips */
  520. pxa_init_gpio_chip(pxa_last_gpio, info ? info->gpio_set_wake : NULL);
  521. /* clear all GPIO edge detects */
  522. for_each_gpio_chip(gpio, c) {
  523. writel_relaxed(0, c->regbase + GFER_OFFSET);
  524. writel_relaxed(0, c->regbase + GRER_OFFSET);
  525. writel_relaxed(~0, c->regbase + GEDR_OFFSET);
  526. /* unmask GPIO edge detect for AP side */
  527. if (gpio_is_mmp_type(gpio_type))
  528. writel_relaxed(~0, c->regbase + ED_MASK_OFFSET);
  529. }
  530. if (!use_of) {
  531. #ifdef CONFIG_ARCH_PXA
  532. irq = gpio_to_irq(0);
  533. irq_set_chip_and_handler(irq, &pxa_muxed_gpio_chip,
  534. handle_edge_irq);
  535. set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
  536. irq_set_chained_handler(IRQ_GPIO0, pxa_gpio_demux_handler);
  537. irq = gpio_to_irq(1);
  538. irq_set_chip_and_handler(irq, &pxa_muxed_gpio_chip,
  539. handle_edge_irq);
  540. set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
  541. irq_set_chained_handler(IRQ_GPIO1, pxa_gpio_demux_handler);
  542. #endif
  543. for (irq = gpio_to_irq(gpio_offset);
  544. irq <= gpio_to_irq(pxa_last_gpio); irq++) {
  545. irq_set_chip_and_handler(irq, &pxa_muxed_gpio_chip,
  546. handle_edge_irq);
  547. set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
  548. }
  549. }
  550. irq_set_chained_handler(irq_mux, pxa_gpio_demux_handler);
  551. return 0;
  552. }
  553. static const struct platform_device_id gpio_id_table[] = {
  554. { "pxa25x-gpio", (unsigned long)&pxa25x_id },
  555. { "pxa26x-gpio", (unsigned long)&pxa26x_id },
  556. { "pxa27x-gpio", (unsigned long)&pxa27x_id },
  557. { "pxa3xx-gpio", (unsigned long)&pxa3xx_id },
  558. { "pxa93x-gpio", (unsigned long)&pxa93x_id },
  559. { "mmp-gpio", (unsigned long)&mmp_id },
  560. { "mmp2-gpio", (unsigned long)&mmp2_id },
  561. { },
  562. };
  563. static struct platform_driver pxa_gpio_driver = {
  564. .probe = pxa_gpio_probe,
  565. .driver = {
  566. .name = "pxa-gpio",
  567. .of_match_table = of_match_ptr(pxa_gpio_dt_ids),
  568. },
  569. .id_table = gpio_id_table,
  570. };
  571. static int __init pxa_gpio_init(void)
  572. {
  573. return platform_driver_register(&pxa_gpio_driver);
  574. }
  575. postcore_initcall(pxa_gpio_init);
  576. #ifdef CONFIG_PM
  577. static int pxa_gpio_suspend(void)
  578. {
  579. struct pxa_gpio_chip *c;
  580. int gpio;
  581. for_each_gpio_chip(gpio, c) {
  582. c->saved_gplr = readl_relaxed(c->regbase + GPLR_OFFSET);
  583. c->saved_gpdr = readl_relaxed(c->regbase + GPDR_OFFSET);
  584. c->saved_grer = readl_relaxed(c->regbase + GRER_OFFSET);
  585. c->saved_gfer = readl_relaxed(c->regbase + GFER_OFFSET);
  586. /* Clear GPIO transition detect bits */
  587. writel_relaxed(0xffffffff, c->regbase + GEDR_OFFSET);
  588. }
  589. return 0;
  590. }
  591. static void pxa_gpio_resume(void)
  592. {
  593. struct pxa_gpio_chip *c;
  594. int gpio;
  595. for_each_gpio_chip(gpio, c) {
  596. /* restore level with set/clear */
  597. writel_relaxed(c->saved_gplr, c->regbase + GPSR_OFFSET);
  598. writel_relaxed(~c->saved_gplr, c->regbase + GPCR_OFFSET);
  599. writel_relaxed(c->saved_grer, c->regbase + GRER_OFFSET);
  600. writel_relaxed(c->saved_gfer, c->regbase + GFER_OFFSET);
  601. writel_relaxed(c->saved_gpdr, c->regbase + GPDR_OFFSET);
  602. }
  603. }
  604. #else
  605. #define pxa_gpio_suspend NULL
  606. #define pxa_gpio_resume NULL
  607. #endif
  608. struct syscore_ops pxa_gpio_syscore_ops = {
  609. .suspend = pxa_gpio_suspend,
  610. .resume = pxa_gpio_resume,
  611. };
  612. static int __init pxa_gpio_sysinit(void)
  613. {
  614. register_syscore_ops(&pxa_gpio_syscore_ops);
  615. return 0;
  616. }
  617. postcore_initcall(pxa_gpio_sysinit);