gpio-pxa.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  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/pinctrl/consumer.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/syscore_ops.h>
  30. #include <linux/slab.h>
  31. /*
  32. * We handle the GPIOs by banks, each bank covers up to 32 GPIOs with
  33. * one set of registers. The register offsets are organized below:
  34. *
  35. * GPLR GPDR GPSR GPCR GRER GFER GEDR
  36. * BANK 0 - 0x0000 0x000C 0x0018 0x0024 0x0030 0x003C 0x0048
  37. * BANK 1 - 0x0004 0x0010 0x001C 0x0028 0x0034 0x0040 0x004C
  38. * BANK 2 - 0x0008 0x0014 0x0020 0x002C 0x0038 0x0044 0x0050
  39. *
  40. * BANK 3 - 0x0100 0x010C 0x0118 0x0124 0x0130 0x013C 0x0148
  41. * BANK 4 - 0x0104 0x0110 0x011C 0x0128 0x0134 0x0140 0x014C
  42. * BANK 5 - 0x0108 0x0114 0x0120 0x012C 0x0138 0x0144 0x0150
  43. *
  44. * BANK 6 - 0x0200 0x020C 0x0218 0x0224 0x0230 0x023C 0x0248
  45. *
  46. * NOTE:
  47. * BANK 3 is only available on PXA27x and later processors.
  48. * BANK 4 and 5 are only available on PXA935, PXA1928
  49. * BANK 6 is only available on PXA1928
  50. */
  51. #define GPLR_OFFSET 0x00
  52. #define GPDR_OFFSET 0x0C
  53. #define GPSR_OFFSET 0x18
  54. #define GPCR_OFFSET 0x24
  55. #define GRER_OFFSET 0x30
  56. #define GFER_OFFSET 0x3C
  57. #define GEDR_OFFSET 0x48
  58. #define GAFR_OFFSET 0x54
  59. #define ED_MASK_OFFSET 0x9C /* GPIO edge detection for AP side */
  60. #define BANK_OFF(n) (((n) / 3) << 8) + (((n) % 3) << 2)
  61. int pxa_last_gpio;
  62. static int irq_base;
  63. struct pxa_gpio_bank {
  64. void __iomem *regbase;
  65. unsigned long irq_mask;
  66. unsigned long irq_edge_rise;
  67. unsigned long irq_edge_fall;
  68. #ifdef CONFIG_PM
  69. unsigned long saved_gplr;
  70. unsigned long saved_gpdr;
  71. unsigned long saved_grer;
  72. unsigned long saved_gfer;
  73. #endif
  74. };
  75. struct pxa_gpio_chip {
  76. struct device *dev;
  77. struct gpio_chip chip;
  78. struct pxa_gpio_bank *banks;
  79. struct irq_domain *irqdomain;
  80. int irq0;
  81. int irq1;
  82. int (*set_wake)(unsigned int gpio, unsigned int on);
  83. };
  84. enum pxa_gpio_type {
  85. PXA25X_GPIO = 0,
  86. PXA26X_GPIO,
  87. PXA27X_GPIO,
  88. PXA3XX_GPIO,
  89. PXA93X_GPIO,
  90. MMP_GPIO = 0x10,
  91. MMP2_GPIO,
  92. PXA1928_GPIO,
  93. };
  94. struct pxa_gpio_id {
  95. enum pxa_gpio_type type;
  96. int gpio_nums;
  97. };
  98. static DEFINE_SPINLOCK(gpio_lock);
  99. static struct pxa_gpio_chip *pxa_gpio_chip;
  100. static enum pxa_gpio_type gpio_type;
  101. static struct pxa_gpio_id pxa25x_id = {
  102. .type = PXA25X_GPIO,
  103. .gpio_nums = 85,
  104. };
  105. static struct pxa_gpio_id pxa26x_id = {
  106. .type = PXA26X_GPIO,
  107. .gpio_nums = 90,
  108. };
  109. static struct pxa_gpio_id pxa27x_id = {
  110. .type = PXA27X_GPIO,
  111. .gpio_nums = 121,
  112. };
  113. static struct pxa_gpio_id pxa3xx_id = {
  114. .type = PXA3XX_GPIO,
  115. .gpio_nums = 128,
  116. };
  117. static struct pxa_gpio_id pxa93x_id = {
  118. .type = PXA93X_GPIO,
  119. .gpio_nums = 192,
  120. };
  121. static struct pxa_gpio_id mmp_id = {
  122. .type = MMP_GPIO,
  123. .gpio_nums = 128,
  124. };
  125. static struct pxa_gpio_id mmp2_id = {
  126. .type = MMP2_GPIO,
  127. .gpio_nums = 192,
  128. };
  129. static struct pxa_gpio_id pxa1928_id = {
  130. .type = PXA1928_GPIO,
  131. .gpio_nums = 224,
  132. };
  133. #define for_each_gpio_bank(i, b, pc) \
  134. for (i = 0, b = pc->banks; i <= pxa_last_gpio; i += 32, b++)
  135. static inline struct pxa_gpio_chip *chip_to_pxachip(struct gpio_chip *c)
  136. {
  137. struct pxa_gpio_chip *pxa_chip = gpiochip_get_data(c);
  138. return pxa_chip;
  139. }
  140. static inline void __iomem *gpio_bank_base(struct gpio_chip *c, int gpio)
  141. {
  142. struct pxa_gpio_chip *p = gpiochip_get_data(c);
  143. struct pxa_gpio_bank *bank = p->banks + (gpio / 32);
  144. return bank->regbase;
  145. }
  146. static inline struct pxa_gpio_bank *gpio_to_pxabank(struct gpio_chip *c,
  147. unsigned gpio)
  148. {
  149. return chip_to_pxachip(c)->banks + gpio / 32;
  150. }
  151. static inline int gpio_is_pxa_type(int type)
  152. {
  153. return (type & MMP_GPIO) == 0;
  154. }
  155. static inline int gpio_is_mmp_type(int type)
  156. {
  157. return (type & MMP_GPIO) != 0;
  158. }
  159. /* GPIO86/87/88/89 on PXA26x have their direction bits in PXA_GPDR(2 inverted,
  160. * as well as their Alternate Function value being '1' for GPIO in GAFRx.
  161. */
  162. static inline int __gpio_is_inverted(int gpio)
  163. {
  164. if ((gpio_type == PXA26X_GPIO) && (gpio > 85))
  165. return 1;
  166. return 0;
  167. }
  168. /*
  169. * On PXA25x and PXA27x, GAFRx and GPDRx together decide the alternate
  170. * function of a GPIO, and GPDRx cannot be altered once configured. It
  171. * is attributed as "occupied" here (I know this terminology isn't
  172. * accurate, you are welcome to propose a better one :-)
  173. */
  174. static inline int __gpio_is_occupied(struct pxa_gpio_chip *pchip, unsigned gpio)
  175. {
  176. void __iomem *base;
  177. unsigned long gafr = 0, gpdr = 0;
  178. int ret, af = 0, dir = 0;
  179. base = gpio_bank_base(&pchip->chip, gpio);
  180. gpdr = readl_relaxed(base + GPDR_OFFSET);
  181. switch (gpio_type) {
  182. case PXA25X_GPIO:
  183. case PXA26X_GPIO:
  184. case PXA27X_GPIO:
  185. gafr = readl_relaxed(base + GAFR_OFFSET);
  186. af = (gafr >> ((gpio & 0xf) * 2)) & 0x3;
  187. dir = gpdr & GPIO_bit(gpio);
  188. if (__gpio_is_inverted(gpio))
  189. ret = (af != 1) || (dir == 0);
  190. else
  191. ret = (af != 0) || (dir != 0);
  192. break;
  193. default:
  194. ret = gpdr & GPIO_bit(gpio);
  195. break;
  196. }
  197. return ret;
  198. }
  199. int pxa_irq_to_gpio(int irq)
  200. {
  201. struct pxa_gpio_chip *pchip = pxa_gpio_chip;
  202. int irq_gpio0;
  203. irq_gpio0 = irq_find_mapping(pchip->irqdomain, 0);
  204. if (irq_gpio0 > 0)
  205. return irq - irq_gpio0;
  206. return irq_gpio0;
  207. }
  208. static int pxa_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  209. {
  210. struct pxa_gpio_chip *pchip = chip_to_pxachip(chip);
  211. return irq_find_mapping(pchip->irqdomain, offset);
  212. }
  213. static int pxa_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  214. {
  215. void __iomem *base = gpio_bank_base(chip, offset);
  216. uint32_t value, mask = GPIO_bit(offset);
  217. unsigned long flags;
  218. int ret;
  219. ret = pinctrl_gpio_direction_input(chip->base + offset);
  220. if (!ret)
  221. return 0;
  222. spin_lock_irqsave(&gpio_lock, flags);
  223. value = readl_relaxed(base + GPDR_OFFSET);
  224. if (__gpio_is_inverted(chip->base + offset))
  225. value |= mask;
  226. else
  227. value &= ~mask;
  228. writel_relaxed(value, base + GPDR_OFFSET);
  229. spin_unlock_irqrestore(&gpio_lock, flags);
  230. return 0;
  231. }
  232. static int pxa_gpio_direction_output(struct gpio_chip *chip,
  233. unsigned offset, int value)
  234. {
  235. void __iomem *base = gpio_bank_base(chip, offset);
  236. uint32_t tmp, mask = GPIO_bit(offset);
  237. unsigned long flags;
  238. int ret;
  239. writel_relaxed(mask, base + (value ? GPSR_OFFSET : GPCR_OFFSET));
  240. ret = pinctrl_gpio_direction_output(chip->base + offset);
  241. if (ret)
  242. return ret;
  243. spin_lock_irqsave(&gpio_lock, flags);
  244. tmp = readl_relaxed(base + GPDR_OFFSET);
  245. if (__gpio_is_inverted(chip->base + offset))
  246. tmp &= ~mask;
  247. else
  248. tmp |= mask;
  249. writel_relaxed(tmp, base + GPDR_OFFSET);
  250. spin_unlock_irqrestore(&gpio_lock, flags);
  251. return 0;
  252. }
  253. static int pxa_gpio_get(struct gpio_chip *chip, unsigned offset)
  254. {
  255. void __iomem *base = gpio_bank_base(chip, offset);
  256. u32 gplr = readl_relaxed(base + GPLR_OFFSET);
  257. return !!(gplr & GPIO_bit(offset));
  258. }
  259. static void pxa_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  260. {
  261. void __iomem *base = gpio_bank_base(chip, offset);
  262. writel_relaxed(GPIO_bit(offset),
  263. base + (value ? GPSR_OFFSET : GPCR_OFFSET));
  264. }
  265. #ifdef CONFIG_OF_GPIO
  266. static int pxa_gpio_of_xlate(struct gpio_chip *gc,
  267. const struct of_phandle_args *gpiospec,
  268. u32 *flags)
  269. {
  270. if (gpiospec->args[0] > pxa_last_gpio)
  271. return -EINVAL;
  272. if (flags)
  273. *flags = gpiospec->args[1];
  274. return gpiospec->args[0];
  275. }
  276. #endif
  277. static int pxa_init_gpio_chip(struct pxa_gpio_chip *pchip, int ngpio,
  278. struct device_node *np, void __iomem *regbase)
  279. {
  280. int i, gpio, nbanks = DIV_ROUND_UP(ngpio, 32);
  281. struct pxa_gpio_bank *bank;
  282. pchip->banks = devm_kcalloc(pchip->dev, nbanks, sizeof(*pchip->banks),
  283. GFP_KERNEL);
  284. if (!pchip->banks)
  285. return -ENOMEM;
  286. pchip->chip.label = "gpio-pxa";
  287. pchip->chip.direction_input = pxa_gpio_direction_input;
  288. pchip->chip.direction_output = pxa_gpio_direction_output;
  289. pchip->chip.get = pxa_gpio_get;
  290. pchip->chip.set = pxa_gpio_set;
  291. pchip->chip.to_irq = pxa_gpio_to_irq;
  292. pchip->chip.ngpio = ngpio;
  293. pchip->chip.request = gpiochip_generic_request;
  294. pchip->chip.free = gpiochip_generic_free;
  295. #ifdef CONFIG_OF_GPIO
  296. pchip->chip.of_node = np;
  297. pchip->chip.of_xlate = pxa_gpio_of_xlate;
  298. pchip->chip.of_gpio_n_cells = 2;
  299. #endif
  300. for (i = 0, gpio = 0; i < nbanks; i++, gpio += 32) {
  301. bank = pchip->banks + i;
  302. bank->regbase = regbase + BANK_OFF(i);
  303. }
  304. return gpiochip_add_data(&pchip->chip, pchip);
  305. }
  306. /* Update only those GRERx and GFERx edge detection register bits if those
  307. * bits are set in c->irq_mask
  308. */
  309. static inline void update_edge_detect(struct pxa_gpio_bank *c)
  310. {
  311. uint32_t grer, gfer;
  312. grer = readl_relaxed(c->regbase + GRER_OFFSET) & ~c->irq_mask;
  313. gfer = readl_relaxed(c->regbase + GFER_OFFSET) & ~c->irq_mask;
  314. grer |= c->irq_edge_rise & c->irq_mask;
  315. gfer |= c->irq_edge_fall & c->irq_mask;
  316. writel_relaxed(grer, c->regbase + GRER_OFFSET);
  317. writel_relaxed(gfer, c->regbase + GFER_OFFSET);
  318. }
  319. static int pxa_gpio_irq_type(struct irq_data *d, unsigned int type)
  320. {
  321. struct pxa_gpio_chip *pchip = irq_data_get_irq_chip_data(d);
  322. unsigned int gpio = irqd_to_hwirq(d);
  323. struct pxa_gpio_bank *c = gpio_to_pxabank(&pchip->chip, gpio);
  324. unsigned long gpdr, mask = GPIO_bit(gpio);
  325. if (type == IRQ_TYPE_PROBE) {
  326. /* Don't mess with enabled GPIOs using preconfigured edges or
  327. * GPIOs set to alternate function or to output during probe
  328. */
  329. if ((c->irq_edge_rise | c->irq_edge_fall) & GPIO_bit(gpio))
  330. return 0;
  331. if (__gpio_is_occupied(pchip, gpio))
  332. return 0;
  333. type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
  334. }
  335. gpdr = readl_relaxed(c->regbase + GPDR_OFFSET);
  336. if (__gpio_is_inverted(gpio))
  337. writel_relaxed(gpdr | mask, c->regbase + GPDR_OFFSET);
  338. else
  339. writel_relaxed(gpdr & ~mask, c->regbase + GPDR_OFFSET);
  340. if (type & IRQ_TYPE_EDGE_RISING)
  341. c->irq_edge_rise |= mask;
  342. else
  343. c->irq_edge_rise &= ~mask;
  344. if (type & IRQ_TYPE_EDGE_FALLING)
  345. c->irq_edge_fall |= mask;
  346. else
  347. c->irq_edge_fall &= ~mask;
  348. update_edge_detect(c);
  349. pr_debug("%s: IRQ%d (GPIO%d) - edge%s%s\n", __func__, d->irq, gpio,
  350. ((type & IRQ_TYPE_EDGE_RISING) ? " rising" : ""),
  351. ((type & IRQ_TYPE_EDGE_FALLING) ? " falling" : ""));
  352. return 0;
  353. }
  354. static irqreturn_t pxa_gpio_demux_handler(int in_irq, void *d)
  355. {
  356. int loop, gpio, n, handled = 0;
  357. unsigned long gedr;
  358. struct pxa_gpio_chip *pchip = d;
  359. struct pxa_gpio_bank *c;
  360. do {
  361. loop = 0;
  362. for_each_gpio_bank(gpio, c, pchip) {
  363. gedr = readl_relaxed(c->regbase + GEDR_OFFSET);
  364. gedr = gedr & c->irq_mask;
  365. writel_relaxed(gedr, c->regbase + GEDR_OFFSET);
  366. for_each_set_bit(n, &gedr, BITS_PER_LONG) {
  367. loop = 1;
  368. generic_handle_irq(
  369. irq_find_mapping(pchip->irqdomain,
  370. gpio + n));
  371. }
  372. }
  373. handled += loop;
  374. } while (loop);
  375. return handled ? IRQ_HANDLED : IRQ_NONE;
  376. }
  377. static irqreturn_t pxa_gpio_direct_handler(int in_irq, void *d)
  378. {
  379. struct pxa_gpio_chip *pchip = d;
  380. if (in_irq == pchip->irq0) {
  381. generic_handle_irq(irq_find_mapping(pchip->irqdomain, 0));
  382. } else if (in_irq == pchip->irq1) {
  383. generic_handle_irq(irq_find_mapping(pchip->irqdomain, 1));
  384. } else {
  385. pr_err("%s() unknown irq %d\n", __func__, in_irq);
  386. return IRQ_NONE;
  387. }
  388. return IRQ_HANDLED;
  389. }
  390. static void pxa_ack_muxed_gpio(struct irq_data *d)
  391. {
  392. struct pxa_gpio_chip *pchip = irq_data_get_irq_chip_data(d);
  393. unsigned int gpio = irqd_to_hwirq(d);
  394. void __iomem *base = gpio_bank_base(&pchip->chip, gpio);
  395. writel_relaxed(GPIO_bit(gpio), base + GEDR_OFFSET);
  396. }
  397. static void pxa_mask_muxed_gpio(struct irq_data *d)
  398. {
  399. struct pxa_gpio_chip *pchip = irq_data_get_irq_chip_data(d);
  400. unsigned int gpio = irqd_to_hwirq(d);
  401. struct pxa_gpio_bank *b = gpio_to_pxabank(&pchip->chip, gpio);
  402. void __iomem *base = gpio_bank_base(&pchip->chip, gpio);
  403. uint32_t grer, gfer;
  404. b->irq_mask &= ~GPIO_bit(gpio);
  405. grer = readl_relaxed(base + GRER_OFFSET) & ~GPIO_bit(gpio);
  406. gfer = readl_relaxed(base + GFER_OFFSET) & ~GPIO_bit(gpio);
  407. writel_relaxed(grer, base + GRER_OFFSET);
  408. writel_relaxed(gfer, base + GFER_OFFSET);
  409. }
  410. static int pxa_gpio_set_wake(struct irq_data *d, unsigned int on)
  411. {
  412. struct pxa_gpio_chip *pchip = irq_data_get_irq_chip_data(d);
  413. unsigned int gpio = irqd_to_hwirq(d);
  414. if (pchip->set_wake)
  415. return pchip->set_wake(gpio, on);
  416. else
  417. return 0;
  418. }
  419. static void pxa_unmask_muxed_gpio(struct irq_data *d)
  420. {
  421. struct pxa_gpio_chip *pchip = irq_data_get_irq_chip_data(d);
  422. unsigned int gpio = irqd_to_hwirq(d);
  423. struct pxa_gpio_bank *c = gpio_to_pxabank(&pchip->chip, gpio);
  424. c->irq_mask |= GPIO_bit(gpio);
  425. update_edge_detect(c);
  426. }
  427. static struct irq_chip pxa_muxed_gpio_chip = {
  428. .name = "GPIO",
  429. .irq_ack = pxa_ack_muxed_gpio,
  430. .irq_mask = pxa_mask_muxed_gpio,
  431. .irq_unmask = pxa_unmask_muxed_gpio,
  432. .irq_set_type = pxa_gpio_irq_type,
  433. .irq_set_wake = pxa_gpio_set_wake,
  434. };
  435. static int pxa_gpio_nums(struct platform_device *pdev)
  436. {
  437. const struct platform_device_id *id = platform_get_device_id(pdev);
  438. struct pxa_gpio_id *pxa_id = (struct pxa_gpio_id *)id->driver_data;
  439. int count = 0;
  440. switch (pxa_id->type) {
  441. case PXA25X_GPIO:
  442. case PXA26X_GPIO:
  443. case PXA27X_GPIO:
  444. case PXA3XX_GPIO:
  445. case PXA93X_GPIO:
  446. case MMP_GPIO:
  447. case MMP2_GPIO:
  448. case PXA1928_GPIO:
  449. gpio_type = pxa_id->type;
  450. count = pxa_id->gpio_nums - 1;
  451. break;
  452. default:
  453. count = -EINVAL;
  454. break;
  455. }
  456. return count;
  457. }
  458. static int pxa_irq_domain_map(struct irq_domain *d, unsigned int irq,
  459. irq_hw_number_t hw)
  460. {
  461. irq_set_chip_and_handler(irq, &pxa_muxed_gpio_chip,
  462. handle_edge_irq);
  463. irq_set_chip_data(irq, d->host_data);
  464. irq_set_noprobe(irq);
  465. return 0;
  466. }
  467. const struct irq_domain_ops pxa_irq_domain_ops = {
  468. .map = pxa_irq_domain_map,
  469. .xlate = irq_domain_xlate_twocell,
  470. };
  471. #ifdef CONFIG_OF
  472. static const struct of_device_id pxa_gpio_dt_ids[] = {
  473. { .compatible = "intel,pxa25x-gpio", .data = &pxa25x_id, },
  474. { .compatible = "intel,pxa26x-gpio", .data = &pxa26x_id, },
  475. { .compatible = "intel,pxa27x-gpio", .data = &pxa27x_id, },
  476. { .compatible = "intel,pxa3xx-gpio", .data = &pxa3xx_id, },
  477. { .compatible = "marvell,pxa93x-gpio", .data = &pxa93x_id, },
  478. { .compatible = "marvell,mmp-gpio", .data = &mmp_id, },
  479. { .compatible = "marvell,mmp2-gpio", .data = &mmp2_id, },
  480. { .compatible = "marvell,pxa1928-gpio", .data = &pxa1928_id, },
  481. {}
  482. };
  483. static int pxa_gpio_probe_dt(struct platform_device *pdev,
  484. struct pxa_gpio_chip *pchip)
  485. {
  486. int nr_gpios;
  487. const struct of_device_id *of_id =
  488. of_match_device(pxa_gpio_dt_ids, &pdev->dev);
  489. const struct pxa_gpio_id *gpio_id;
  490. if (!of_id || !of_id->data) {
  491. dev_err(&pdev->dev, "Failed to find gpio controller\n");
  492. return -EFAULT;
  493. }
  494. gpio_id = of_id->data;
  495. gpio_type = gpio_id->type;
  496. nr_gpios = gpio_id->gpio_nums;
  497. pxa_last_gpio = nr_gpios - 1;
  498. irq_base = devm_irq_alloc_descs(&pdev->dev, -1, 0, nr_gpios, 0);
  499. if (irq_base < 0) {
  500. dev_err(&pdev->dev, "Failed to allocate IRQ numbers\n");
  501. return irq_base;
  502. }
  503. return irq_base;
  504. }
  505. #else
  506. #define pxa_gpio_probe_dt(pdev, pchip) (-1)
  507. #endif
  508. static int pxa_gpio_probe(struct platform_device *pdev)
  509. {
  510. struct pxa_gpio_chip *pchip;
  511. struct pxa_gpio_bank *c;
  512. struct resource *res;
  513. struct clk *clk;
  514. struct pxa_gpio_platform_data *info;
  515. void __iomem *gpio_reg_base;
  516. int gpio, ret;
  517. int irq0 = 0, irq1 = 0, irq_mux, gpio_offset = 0;
  518. pchip = devm_kzalloc(&pdev->dev, sizeof(*pchip), GFP_KERNEL);
  519. if (!pchip)
  520. return -ENOMEM;
  521. pchip->dev = &pdev->dev;
  522. info = dev_get_platdata(&pdev->dev);
  523. if (info) {
  524. irq_base = info->irq_base;
  525. if (irq_base <= 0)
  526. return -EINVAL;
  527. pxa_last_gpio = pxa_gpio_nums(pdev);
  528. pchip->set_wake = info->gpio_set_wake;
  529. } else {
  530. irq_base = pxa_gpio_probe_dt(pdev, pchip);
  531. if (irq_base < 0)
  532. return -EINVAL;
  533. }
  534. if (!pxa_last_gpio)
  535. return -EINVAL;
  536. pchip->irqdomain = irq_domain_add_legacy(pdev->dev.of_node,
  537. pxa_last_gpio + 1, irq_base,
  538. 0, &pxa_irq_domain_ops, pchip);
  539. if (!pchip->irqdomain)
  540. return -ENOMEM;
  541. irq0 = platform_get_irq_byname(pdev, "gpio0");
  542. irq1 = platform_get_irq_byname(pdev, "gpio1");
  543. irq_mux = platform_get_irq_byname(pdev, "gpio_mux");
  544. if ((irq0 > 0 && irq1 <= 0) || (irq0 <= 0 && irq1 > 0)
  545. || (irq_mux <= 0))
  546. return -EINVAL;
  547. pchip->irq0 = irq0;
  548. pchip->irq1 = irq1;
  549. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  550. gpio_reg_base = devm_ioremap(&pdev->dev, res->start,
  551. resource_size(res));
  552. if (!gpio_reg_base)
  553. return -EINVAL;
  554. if (irq0 > 0)
  555. gpio_offset = 2;
  556. clk = clk_get(&pdev->dev, NULL);
  557. if (IS_ERR(clk)) {
  558. dev_err(&pdev->dev, "Error %ld to get gpio clock\n",
  559. PTR_ERR(clk));
  560. return PTR_ERR(clk);
  561. }
  562. ret = clk_prepare_enable(clk);
  563. if (ret) {
  564. clk_put(clk);
  565. return ret;
  566. }
  567. /* Initialize GPIO chips */
  568. ret = pxa_init_gpio_chip(pchip, pxa_last_gpio + 1, pdev->dev.of_node,
  569. gpio_reg_base);
  570. if (ret) {
  571. clk_put(clk);
  572. return ret;
  573. }
  574. /* clear all GPIO edge detects */
  575. for_each_gpio_bank(gpio, c, pchip) {
  576. writel_relaxed(0, c->regbase + GFER_OFFSET);
  577. writel_relaxed(0, c->regbase + GRER_OFFSET);
  578. writel_relaxed(~0, c->regbase + GEDR_OFFSET);
  579. /* unmask GPIO edge detect for AP side */
  580. if (gpio_is_mmp_type(gpio_type))
  581. writel_relaxed(~0, c->regbase + ED_MASK_OFFSET);
  582. }
  583. if (irq0 > 0) {
  584. ret = devm_request_irq(&pdev->dev,
  585. irq0, pxa_gpio_direct_handler, 0,
  586. "gpio-0", pchip);
  587. if (ret)
  588. dev_err(&pdev->dev, "request of gpio0 irq failed: %d\n",
  589. ret);
  590. }
  591. if (irq1 > 0) {
  592. ret = devm_request_irq(&pdev->dev,
  593. irq1, pxa_gpio_direct_handler, 0,
  594. "gpio-1", pchip);
  595. if (ret)
  596. dev_err(&pdev->dev, "request of gpio1 irq failed: %d\n",
  597. ret);
  598. }
  599. ret = devm_request_irq(&pdev->dev,
  600. irq_mux, pxa_gpio_demux_handler, 0,
  601. "gpio-mux", pchip);
  602. if (ret)
  603. dev_err(&pdev->dev, "request of gpio-mux irq failed: %d\n",
  604. ret);
  605. pxa_gpio_chip = pchip;
  606. return 0;
  607. }
  608. static const struct platform_device_id gpio_id_table[] = {
  609. { "pxa25x-gpio", (unsigned long)&pxa25x_id },
  610. { "pxa26x-gpio", (unsigned long)&pxa26x_id },
  611. { "pxa27x-gpio", (unsigned long)&pxa27x_id },
  612. { "pxa3xx-gpio", (unsigned long)&pxa3xx_id },
  613. { "pxa93x-gpio", (unsigned long)&pxa93x_id },
  614. { "mmp-gpio", (unsigned long)&mmp_id },
  615. { "mmp2-gpio", (unsigned long)&mmp2_id },
  616. { "pxa1928-gpio", (unsigned long)&pxa1928_id },
  617. { },
  618. };
  619. static struct platform_driver pxa_gpio_driver = {
  620. .probe = pxa_gpio_probe,
  621. .driver = {
  622. .name = "pxa-gpio",
  623. .of_match_table = of_match_ptr(pxa_gpio_dt_ids),
  624. },
  625. .id_table = gpio_id_table,
  626. };
  627. static int __init pxa_gpio_legacy_init(void)
  628. {
  629. if (of_have_populated_dt())
  630. return 0;
  631. return platform_driver_register(&pxa_gpio_driver);
  632. }
  633. postcore_initcall(pxa_gpio_legacy_init);
  634. static int __init pxa_gpio_dt_init(void)
  635. {
  636. if (of_have_populated_dt())
  637. return platform_driver_register(&pxa_gpio_driver);
  638. return 0;
  639. }
  640. device_initcall(pxa_gpio_dt_init);
  641. #ifdef CONFIG_PM
  642. static int pxa_gpio_suspend(void)
  643. {
  644. struct pxa_gpio_chip *pchip = pxa_gpio_chip;
  645. struct pxa_gpio_bank *c;
  646. int gpio;
  647. for_each_gpio_bank(gpio, c, pchip) {
  648. c->saved_gplr = readl_relaxed(c->regbase + GPLR_OFFSET);
  649. c->saved_gpdr = readl_relaxed(c->regbase + GPDR_OFFSET);
  650. c->saved_grer = readl_relaxed(c->regbase + GRER_OFFSET);
  651. c->saved_gfer = readl_relaxed(c->regbase + GFER_OFFSET);
  652. /* Clear GPIO transition detect bits */
  653. writel_relaxed(0xffffffff, c->regbase + GEDR_OFFSET);
  654. }
  655. return 0;
  656. }
  657. static void pxa_gpio_resume(void)
  658. {
  659. struct pxa_gpio_chip *pchip = pxa_gpio_chip;
  660. struct pxa_gpio_bank *c;
  661. int gpio;
  662. for_each_gpio_bank(gpio, c, pchip) {
  663. /* restore level with set/clear */
  664. writel_relaxed(c->saved_gplr, c->regbase + GPSR_OFFSET);
  665. writel_relaxed(~c->saved_gplr, c->regbase + GPCR_OFFSET);
  666. writel_relaxed(c->saved_grer, c->regbase + GRER_OFFSET);
  667. writel_relaxed(c->saved_gfer, c->regbase + GFER_OFFSET);
  668. writel_relaxed(c->saved_gpdr, c->regbase + GPDR_OFFSET);
  669. }
  670. }
  671. #else
  672. #define pxa_gpio_suspend NULL
  673. #define pxa_gpio_resume NULL
  674. #endif
  675. struct syscore_ops pxa_gpio_syscore_ops = {
  676. .suspend = pxa_gpio_suspend,
  677. .resume = pxa_gpio_resume,
  678. };
  679. static int __init pxa_gpio_sysinit(void)
  680. {
  681. register_syscore_ops(&pxa_gpio_syscore_ops);
  682. return 0;
  683. }
  684. postcore_initcall(pxa_gpio_sysinit);