gpio-pxa.c 20 KB

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