gpio-pxa.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  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_gpio_request(struct gpio_chip *chip, unsigned int offset)
  278. {
  279. return pinctrl_request_gpio(chip->base + offset);
  280. }
  281. static void pxa_gpio_free(struct gpio_chip *chip, unsigned int offset)
  282. {
  283. pinctrl_free_gpio(chip->base + offset);
  284. }
  285. static int pxa_init_gpio_chip(struct pxa_gpio_chip *pchip, int ngpio,
  286. struct device_node *np, void __iomem *regbase)
  287. {
  288. int i, gpio, nbanks = DIV_ROUND_UP(ngpio, 32);
  289. struct pxa_gpio_bank *bank;
  290. pchip->banks = devm_kcalloc(pchip->dev, nbanks, sizeof(*pchip->banks),
  291. GFP_KERNEL);
  292. if (!pchip->banks)
  293. return -ENOMEM;
  294. pchip->chip.label = "gpio-pxa";
  295. pchip->chip.direction_input = pxa_gpio_direction_input;
  296. pchip->chip.direction_output = pxa_gpio_direction_output;
  297. pchip->chip.get = pxa_gpio_get;
  298. pchip->chip.set = pxa_gpio_set;
  299. pchip->chip.to_irq = pxa_gpio_to_irq;
  300. pchip->chip.ngpio = ngpio;
  301. pchip->chip.request = pxa_gpio_request;
  302. pchip->chip.free = pxa_gpio_free;
  303. #ifdef CONFIG_OF_GPIO
  304. pchip->chip.of_node = np;
  305. pchip->chip.of_xlate = pxa_gpio_of_xlate;
  306. pchip->chip.of_gpio_n_cells = 2;
  307. #endif
  308. for (i = 0, gpio = 0; i < nbanks; i++, gpio += 32) {
  309. bank = pchip->banks + i;
  310. bank->regbase = regbase + BANK_OFF(i);
  311. }
  312. return gpiochip_add_data(&pchip->chip, pchip);
  313. }
  314. /* Update only those GRERx and GFERx edge detection register bits if those
  315. * bits are set in c->irq_mask
  316. */
  317. static inline void update_edge_detect(struct pxa_gpio_bank *c)
  318. {
  319. uint32_t grer, gfer;
  320. grer = readl_relaxed(c->regbase + GRER_OFFSET) & ~c->irq_mask;
  321. gfer = readl_relaxed(c->regbase + GFER_OFFSET) & ~c->irq_mask;
  322. grer |= c->irq_edge_rise & c->irq_mask;
  323. gfer |= c->irq_edge_fall & c->irq_mask;
  324. writel_relaxed(grer, c->regbase + GRER_OFFSET);
  325. writel_relaxed(gfer, c->regbase + GFER_OFFSET);
  326. }
  327. static int pxa_gpio_irq_type(struct irq_data *d, unsigned int type)
  328. {
  329. struct pxa_gpio_chip *pchip = irq_data_get_irq_chip_data(d);
  330. unsigned int gpio = irqd_to_hwirq(d);
  331. struct pxa_gpio_bank *c = gpio_to_pxabank(&pchip->chip, gpio);
  332. unsigned long gpdr, mask = GPIO_bit(gpio);
  333. if (type == IRQ_TYPE_PROBE) {
  334. /* Don't mess with enabled GPIOs using preconfigured edges or
  335. * GPIOs set to alternate function or to output during probe
  336. */
  337. if ((c->irq_edge_rise | c->irq_edge_fall) & GPIO_bit(gpio))
  338. return 0;
  339. if (__gpio_is_occupied(pchip, gpio))
  340. return 0;
  341. type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
  342. }
  343. gpdr = readl_relaxed(c->regbase + GPDR_OFFSET);
  344. if (__gpio_is_inverted(gpio))
  345. writel_relaxed(gpdr | mask, c->regbase + GPDR_OFFSET);
  346. else
  347. writel_relaxed(gpdr & ~mask, c->regbase + GPDR_OFFSET);
  348. if (type & IRQ_TYPE_EDGE_RISING)
  349. c->irq_edge_rise |= mask;
  350. else
  351. c->irq_edge_rise &= ~mask;
  352. if (type & IRQ_TYPE_EDGE_FALLING)
  353. c->irq_edge_fall |= mask;
  354. else
  355. c->irq_edge_fall &= ~mask;
  356. update_edge_detect(c);
  357. pr_debug("%s: IRQ%d (GPIO%d) - edge%s%s\n", __func__, d->irq, gpio,
  358. ((type & IRQ_TYPE_EDGE_RISING) ? " rising" : ""),
  359. ((type & IRQ_TYPE_EDGE_FALLING) ? " falling" : ""));
  360. return 0;
  361. }
  362. static irqreturn_t pxa_gpio_demux_handler(int in_irq, void *d)
  363. {
  364. int loop, gpio, n, handled = 0;
  365. unsigned long gedr;
  366. struct pxa_gpio_chip *pchip = d;
  367. struct pxa_gpio_bank *c;
  368. do {
  369. loop = 0;
  370. for_each_gpio_bank(gpio, c, pchip) {
  371. gedr = readl_relaxed(c->regbase + GEDR_OFFSET);
  372. gedr = gedr & c->irq_mask;
  373. writel_relaxed(gedr, c->regbase + GEDR_OFFSET);
  374. for_each_set_bit(n, &gedr, BITS_PER_LONG) {
  375. loop = 1;
  376. generic_handle_irq(
  377. irq_find_mapping(pchip->irqdomain,
  378. gpio + n));
  379. }
  380. }
  381. handled += loop;
  382. } while (loop);
  383. return handled ? IRQ_HANDLED : IRQ_NONE;
  384. }
  385. static irqreturn_t pxa_gpio_direct_handler(int in_irq, void *d)
  386. {
  387. struct pxa_gpio_chip *pchip = d;
  388. if (in_irq == pchip->irq0) {
  389. generic_handle_irq(irq_find_mapping(pchip->irqdomain, 0));
  390. } else if (in_irq == pchip->irq1) {
  391. generic_handle_irq(irq_find_mapping(pchip->irqdomain, 1));
  392. } else {
  393. pr_err("%s() unknown irq %d\n", __func__, in_irq);
  394. return IRQ_NONE;
  395. }
  396. return IRQ_HANDLED;
  397. }
  398. static void pxa_ack_muxed_gpio(struct irq_data *d)
  399. {
  400. struct pxa_gpio_chip *pchip = irq_data_get_irq_chip_data(d);
  401. unsigned int gpio = irqd_to_hwirq(d);
  402. void __iomem *base = gpio_bank_base(&pchip->chip, gpio);
  403. writel_relaxed(GPIO_bit(gpio), base + GEDR_OFFSET);
  404. }
  405. static void pxa_mask_muxed_gpio(struct irq_data *d)
  406. {
  407. struct pxa_gpio_chip *pchip = irq_data_get_irq_chip_data(d);
  408. unsigned int gpio = irqd_to_hwirq(d);
  409. struct pxa_gpio_bank *b = gpio_to_pxabank(&pchip->chip, gpio);
  410. void __iomem *base = gpio_bank_base(&pchip->chip, gpio);
  411. uint32_t grer, gfer;
  412. b->irq_mask &= ~GPIO_bit(gpio);
  413. grer = readl_relaxed(base + GRER_OFFSET) & ~GPIO_bit(gpio);
  414. gfer = readl_relaxed(base + GFER_OFFSET) & ~GPIO_bit(gpio);
  415. writel_relaxed(grer, base + GRER_OFFSET);
  416. writel_relaxed(gfer, base + GFER_OFFSET);
  417. }
  418. static int pxa_gpio_set_wake(struct irq_data *d, unsigned int on)
  419. {
  420. struct pxa_gpio_chip *pchip = irq_data_get_irq_chip_data(d);
  421. unsigned int gpio = irqd_to_hwirq(d);
  422. if (pchip->set_wake)
  423. return pchip->set_wake(gpio, on);
  424. else
  425. return 0;
  426. }
  427. static void pxa_unmask_muxed_gpio(struct irq_data *d)
  428. {
  429. struct pxa_gpio_chip *pchip = irq_data_get_irq_chip_data(d);
  430. unsigned int gpio = irqd_to_hwirq(d);
  431. struct pxa_gpio_bank *c = gpio_to_pxabank(&pchip->chip, gpio);
  432. c->irq_mask |= GPIO_bit(gpio);
  433. update_edge_detect(c);
  434. }
  435. static struct irq_chip pxa_muxed_gpio_chip = {
  436. .name = "GPIO",
  437. .irq_ack = pxa_ack_muxed_gpio,
  438. .irq_mask = pxa_mask_muxed_gpio,
  439. .irq_unmask = pxa_unmask_muxed_gpio,
  440. .irq_set_type = pxa_gpio_irq_type,
  441. .irq_set_wake = pxa_gpio_set_wake,
  442. };
  443. static int pxa_gpio_nums(struct platform_device *pdev)
  444. {
  445. const struct platform_device_id *id = platform_get_device_id(pdev);
  446. struct pxa_gpio_id *pxa_id = (struct pxa_gpio_id *)id->driver_data;
  447. int count = 0;
  448. switch (pxa_id->type) {
  449. case PXA25X_GPIO:
  450. case PXA26X_GPIO:
  451. case PXA27X_GPIO:
  452. case PXA3XX_GPIO:
  453. case PXA93X_GPIO:
  454. case MMP_GPIO:
  455. case MMP2_GPIO:
  456. case PXA1928_GPIO:
  457. gpio_type = pxa_id->type;
  458. count = pxa_id->gpio_nums - 1;
  459. break;
  460. default:
  461. count = -EINVAL;
  462. break;
  463. }
  464. return count;
  465. }
  466. static int pxa_irq_domain_map(struct irq_domain *d, unsigned int irq,
  467. irq_hw_number_t hw)
  468. {
  469. irq_set_chip_and_handler(irq, &pxa_muxed_gpio_chip,
  470. handle_edge_irq);
  471. irq_set_chip_data(irq, d->host_data);
  472. irq_set_noprobe(irq);
  473. return 0;
  474. }
  475. const struct irq_domain_ops pxa_irq_domain_ops = {
  476. .map = pxa_irq_domain_map,
  477. .xlate = irq_domain_xlate_twocell,
  478. };
  479. #ifdef CONFIG_OF
  480. static const struct of_device_id pxa_gpio_dt_ids[] = {
  481. { .compatible = "intel,pxa25x-gpio", .data = &pxa25x_id, },
  482. { .compatible = "intel,pxa26x-gpio", .data = &pxa26x_id, },
  483. { .compatible = "intel,pxa27x-gpio", .data = &pxa27x_id, },
  484. { .compatible = "intel,pxa3xx-gpio", .data = &pxa3xx_id, },
  485. { .compatible = "marvell,pxa93x-gpio", .data = &pxa93x_id, },
  486. { .compatible = "marvell,mmp-gpio", .data = &mmp_id, },
  487. { .compatible = "marvell,mmp2-gpio", .data = &mmp2_id, },
  488. { .compatible = "marvell,pxa1928-gpio", .data = &pxa1928_id, },
  489. {}
  490. };
  491. static int pxa_gpio_probe_dt(struct platform_device *pdev,
  492. struct pxa_gpio_chip *pchip)
  493. {
  494. int nr_gpios;
  495. const struct of_device_id *of_id =
  496. of_match_device(pxa_gpio_dt_ids, &pdev->dev);
  497. const struct pxa_gpio_id *gpio_id;
  498. if (!of_id || !of_id->data) {
  499. dev_err(&pdev->dev, "Failed to find gpio controller\n");
  500. return -EFAULT;
  501. }
  502. gpio_id = of_id->data;
  503. gpio_type = gpio_id->type;
  504. nr_gpios = gpio_id->gpio_nums;
  505. pxa_last_gpio = nr_gpios - 1;
  506. irq_base = devm_irq_alloc_descs(&pdev->dev, -1, 0, nr_gpios, 0);
  507. if (irq_base < 0) {
  508. dev_err(&pdev->dev, "Failed to allocate IRQ numbers\n");
  509. return irq_base;
  510. }
  511. return irq_base;
  512. }
  513. #else
  514. #define pxa_gpio_probe_dt(pdev, pchip) (-1)
  515. #endif
  516. static int pxa_gpio_probe(struct platform_device *pdev)
  517. {
  518. struct pxa_gpio_chip *pchip;
  519. struct pxa_gpio_bank *c;
  520. struct resource *res;
  521. struct clk *clk;
  522. struct pxa_gpio_platform_data *info;
  523. void __iomem *gpio_reg_base;
  524. int gpio, ret;
  525. int irq0 = 0, irq1 = 0, irq_mux, gpio_offset = 0;
  526. pchip = devm_kzalloc(&pdev->dev, sizeof(*pchip), GFP_KERNEL);
  527. if (!pchip)
  528. return -ENOMEM;
  529. pchip->dev = &pdev->dev;
  530. info = dev_get_platdata(&pdev->dev);
  531. if (info) {
  532. irq_base = info->irq_base;
  533. if (irq_base <= 0)
  534. return -EINVAL;
  535. pxa_last_gpio = pxa_gpio_nums(pdev);
  536. pchip->set_wake = info->gpio_set_wake;
  537. } else {
  538. irq_base = pxa_gpio_probe_dt(pdev, pchip);
  539. if (irq_base < 0)
  540. return -EINVAL;
  541. }
  542. if (!pxa_last_gpio)
  543. return -EINVAL;
  544. pchip->irqdomain = irq_domain_add_legacy(pdev->dev.of_node,
  545. pxa_last_gpio + 1, irq_base,
  546. 0, &pxa_irq_domain_ops, pchip);
  547. if (!pchip->irqdomain)
  548. return -ENOMEM;
  549. irq0 = platform_get_irq_byname(pdev, "gpio0");
  550. irq1 = platform_get_irq_byname(pdev, "gpio1");
  551. irq_mux = platform_get_irq_byname(pdev, "gpio_mux");
  552. if ((irq0 > 0 && irq1 <= 0) || (irq0 <= 0 && irq1 > 0)
  553. || (irq_mux <= 0))
  554. return -EINVAL;
  555. pchip->irq0 = irq0;
  556. pchip->irq1 = irq1;
  557. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  558. if (!res)
  559. return -EINVAL;
  560. gpio_reg_base = devm_ioremap(&pdev->dev, res->start,
  561. resource_size(res));
  562. if (!gpio_reg_base)
  563. return -EINVAL;
  564. if (irq0 > 0)
  565. gpio_offset = 2;
  566. clk = clk_get(&pdev->dev, NULL);
  567. if (IS_ERR(clk)) {
  568. dev_err(&pdev->dev, "Error %ld to get gpio clock\n",
  569. PTR_ERR(clk));
  570. return PTR_ERR(clk);
  571. }
  572. ret = clk_prepare_enable(clk);
  573. if (ret) {
  574. clk_put(clk);
  575. return ret;
  576. }
  577. /* Initialize GPIO chips */
  578. ret = pxa_init_gpio_chip(pchip, pxa_last_gpio + 1, pdev->dev.of_node,
  579. gpio_reg_base);
  580. if (ret) {
  581. clk_put(clk);
  582. return ret;
  583. }
  584. /* clear all GPIO edge detects */
  585. for_each_gpio_bank(gpio, c, pchip) {
  586. writel_relaxed(0, c->regbase + GFER_OFFSET);
  587. writel_relaxed(0, c->regbase + GRER_OFFSET);
  588. writel_relaxed(~0, c->regbase + GEDR_OFFSET);
  589. /* unmask GPIO edge detect for AP side */
  590. if (gpio_is_mmp_type(gpio_type))
  591. writel_relaxed(~0, c->regbase + ED_MASK_OFFSET);
  592. }
  593. if (irq0 > 0) {
  594. ret = devm_request_irq(&pdev->dev,
  595. irq0, pxa_gpio_direct_handler, 0,
  596. "gpio-0", pchip);
  597. if (ret)
  598. dev_err(&pdev->dev, "request of gpio0 irq failed: %d\n",
  599. ret);
  600. }
  601. if (irq1 > 0) {
  602. ret = devm_request_irq(&pdev->dev,
  603. irq1, pxa_gpio_direct_handler, 0,
  604. "gpio-1", pchip);
  605. if (ret)
  606. dev_err(&pdev->dev, "request of gpio1 irq failed: %d\n",
  607. ret);
  608. }
  609. ret = devm_request_irq(&pdev->dev,
  610. irq_mux, pxa_gpio_demux_handler, 0,
  611. "gpio-mux", pchip);
  612. if (ret)
  613. dev_err(&pdev->dev, "request of gpio-mux irq failed: %d\n",
  614. ret);
  615. pxa_gpio_chip = pchip;
  616. return 0;
  617. }
  618. static const struct platform_device_id gpio_id_table[] = {
  619. { "pxa25x-gpio", (unsigned long)&pxa25x_id },
  620. { "pxa26x-gpio", (unsigned long)&pxa26x_id },
  621. { "pxa27x-gpio", (unsigned long)&pxa27x_id },
  622. { "pxa3xx-gpio", (unsigned long)&pxa3xx_id },
  623. { "pxa93x-gpio", (unsigned long)&pxa93x_id },
  624. { "mmp-gpio", (unsigned long)&mmp_id },
  625. { "mmp2-gpio", (unsigned long)&mmp2_id },
  626. { "pxa1928-gpio", (unsigned long)&pxa1928_id },
  627. { },
  628. };
  629. static struct platform_driver pxa_gpio_driver = {
  630. .probe = pxa_gpio_probe,
  631. .driver = {
  632. .name = "pxa-gpio",
  633. .of_match_table = of_match_ptr(pxa_gpio_dt_ids),
  634. },
  635. .id_table = gpio_id_table,
  636. };
  637. static int __init pxa_gpio_legacy_init(void)
  638. {
  639. if (of_have_populated_dt())
  640. return 0;
  641. return platform_driver_register(&pxa_gpio_driver);
  642. }
  643. postcore_initcall(pxa_gpio_legacy_init);
  644. static int __init pxa_gpio_dt_init(void)
  645. {
  646. if (of_have_populated_dt())
  647. return platform_driver_register(&pxa_gpio_driver);
  648. return 0;
  649. }
  650. device_initcall(pxa_gpio_dt_init);
  651. #ifdef CONFIG_PM
  652. static int pxa_gpio_suspend(void)
  653. {
  654. struct pxa_gpio_chip *pchip = pxa_gpio_chip;
  655. struct pxa_gpio_bank *c;
  656. int gpio;
  657. if (!pchip)
  658. return 0;
  659. for_each_gpio_bank(gpio, c, pchip) {
  660. c->saved_gplr = readl_relaxed(c->regbase + GPLR_OFFSET);
  661. c->saved_gpdr = readl_relaxed(c->regbase + GPDR_OFFSET);
  662. c->saved_grer = readl_relaxed(c->regbase + GRER_OFFSET);
  663. c->saved_gfer = readl_relaxed(c->regbase + GFER_OFFSET);
  664. /* Clear GPIO transition detect bits */
  665. writel_relaxed(0xffffffff, c->regbase + GEDR_OFFSET);
  666. }
  667. return 0;
  668. }
  669. static void pxa_gpio_resume(void)
  670. {
  671. struct pxa_gpio_chip *pchip = pxa_gpio_chip;
  672. struct pxa_gpio_bank *c;
  673. int gpio;
  674. if (!pchip)
  675. return;
  676. for_each_gpio_bank(gpio, c, pchip) {
  677. /* restore level with set/clear */
  678. writel_relaxed(c->saved_gplr, c->regbase + GPSR_OFFSET);
  679. writel_relaxed(~c->saved_gplr, c->regbase + GPCR_OFFSET);
  680. writel_relaxed(c->saved_grer, c->regbase + GRER_OFFSET);
  681. writel_relaxed(c->saved_gfer, c->regbase + GFER_OFFSET);
  682. writel_relaxed(c->saved_gpdr, c->regbase + GPDR_OFFSET);
  683. }
  684. }
  685. #else
  686. #define pxa_gpio_suspend NULL
  687. #define pxa_gpio_resume NULL
  688. #endif
  689. struct syscore_ops pxa_gpio_syscore_ops = {
  690. .suspend = pxa_gpio_suspend,
  691. .resume = pxa_gpio_resume,
  692. };
  693. static int __init pxa_gpio_sysinit(void)
  694. {
  695. register_syscore_ops(&pxa_gpio_syscore_ops);
  696. return 0;
  697. }
  698. postcore_initcall(pxa_gpio_sysinit);