gpio-pxa.c 20 KB

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