gpio-pxa.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  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(gpio_to_irq(gpio + n));
  377. }
  378. }
  379. handled += loop;
  380. } while (loop);
  381. return handled ? IRQ_HANDLED : IRQ_NONE;
  382. }
  383. static irqreturn_t pxa_gpio_direct_handler(int in_irq, void *d)
  384. {
  385. struct pxa_gpio_chip *pchip = d;
  386. if (in_irq == pchip->irq0) {
  387. generic_handle_irq(gpio_to_irq(0));
  388. } else if (in_irq == pchip->irq1) {
  389. generic_handle_irq(gpio_to_irq(1));
  390. } else {
  391. pr_err("%s() unknown irq %d\n", __func__, in_irq);
  392. return IRQ_NONE;
  393. }
  394. return IRQ_HANDLED;
  395. }
  396. static void pxa_ack_muxed_gpio(struct irq_data *d)
  397. {
  398. struct pxa_gpio_chip *pchip = irq_data_get_irq_chip_data(d);
  399. unsigned int gpio = irqd_to_hwirq(d);
  400. void __iomem *base = gpio_bank_base(&pchip->chip, gpio);
  401. writel_relaxed(GPIO_bit(gpio), base + GEDR_OFFSET);
  402. }
  403. static void pxa_mask_muxed_gpio(struct irq_data *d)
  404. {
  405. struct pxa_gpio_chip *pchip = irq_data_get_irq_chip_data(d);
  406. unsigned int gpio = irqd_to_hwirq(d);
  407. struct pxa_gpio_bank *b = gpio_to_pxabank(&pchip->chip, gpio);
  408. void __iomem *base = gpio_bank_base(&pchip->chip, gpio);
  409. uint32_t grer, gfer;
  410. b->irq_mask &= ~GPIO_bit(gpio);
  411. grer = readl_relaxed(base + GRER_OFFSET) & ~GPIO_bit(gpio);
  412. gfer = readl_relaxed(base + GFER_OFFSET) & ~GPIO_bit(gpio);
  413. writel_relaxed(grer, base + GRER_OFFSET);
  414. writel_relaxed(gfer, base + GFER_OFFSET);
  415. }
  416. static int pxa_gpio_set_wake(struct irq_data *d, unsigned int on)
  417. {
  418. struct pxa_gpio_chip *pchip = irq_data_get_irq_chip_data(d);
  419. unsigned int gpio = irqd_to_hwirq(d);
  420. if (pchip->set_wake)
  421. return pchip->set_wake(gpio, on);
  422. else
  423. return 0;
  424. }
  425. static void pxa_unmask_muxed_gpio(struct irq_data *d)
  426. {
  427. struct pxa_gpio_chip *pchip = irq_data_get_irq_chip_data(d);
  428. unsigned int gpio = irqd_to_hwirq(d);
  429. struct pxa_gpio_bank *c = gpio_to_pxabank(&pchip->chip, gpio);
  430. c->irq_mask |= GPIO_bit(gpio);
  431. update_edge_detect(c);
  432. }
  433. static struct irq_chip pxa_muxed_gpio_chip = {
  434. .name = "GPIO",
  435. .irq_ack = pxa_ack_muxed_gpio,
  436. .irq_mask = pxa_mask_muxed_gpio,
  437. .irq_unmask = pxa_unmask_muxed_gpio,
  438. .irq_set_type = pxa_gpio_irq_type,
  439. .irq_set_wake = pxa_gpio_set_wake,
  440. };
  441. static int pxa_gpio_nums(struct platform_device *pdev)
  442. {
  443. const struct platform_device_id *id = platform_get_device_id(pdev);
  444. struct pxa_gpio_id *pxa_id = (struct pxa_gpio_id *)id->driver_data;
  445. int count = 0;
  446. switch (pxa_id->type) {
  447. case PXA25X_GPIO:
  448. case PXA26X_GPIO:
  449. case PXA27X_GPIO:
  450. case PXA3XX_GPIO:
  451. case PXA93X_GPIO:
  452. case MMP_GPIO:
  453. case MMP2_GPIO:
  454. case PXA1928_GPIO:
  455. gpio_type = pxa_id->type;
  456. count = pxa_id->gpio_nums - 1;
  457. break;
  458. default:
  459. count = -EINVAL;
  460. break;
  461. }
  462. return count;
  463. }
  464. static int pxa_irq_domain_map(struct irq_domain *d, unsigned int irq,
  465. irq_hw_number_t hw)
  466. {
  467. irq_set_chip_and_handler(irq, &pxa_muxed_gpio_chip,
  468. handle_edge_irq);
  469. irq_set_chip_data(irq, d->host_data);
  470. irq_set_noprobe(irq);
  471. return 0;
  472. }
  473. const struct irq_domain_ops pxa_irq_domain_ops = {
  474. .map = pxa_irq_domain_map,
  475. .xlate = irq_domain_xlate_twocell,
  476. };
  477. #ifdef CONFIG_OF
  478. static const struct of_device_id pxa_gpio_dt_ids[] = {
  479. { .compatible = "intel,pxa25x-gpio", .data = &pxa25x_id, },
  480. { .compatible = "intel,pxa26x-gpio", .data = &pxa26x_id, },
  481. { .compatible = "intel,pxa27x-gpio", .data = &pxa27x_id, },
  482. { .compatible = "intel,pxa3xx-gpio", .data = &pxa3xx_id, },
  483. { .compatible = "marvell,pxa93x-gpio", .data = &pxa93x_id, },
  484. { .compatible = "marvell,mmp-gpio", .data = &mmp_id, },
  485. { .compatible = "marvell,mmp2-gpio", .data = &mmp2_id, },
  486. { .compatible = "marvell,pxa1928-gpio", .data = &pxa1928_id, },
  487. {}
  488. };
  489. static int pxa_gpio_probe_dt(struct platform_device *pdev,
  490. struct pxa_gpio_chip *pchip)
  491. {
  492. int nr_gpios;
  493. const struct of_device_id *of_id =
  494. of_match_device(pxa_gpio_dt_ids, &pdev->dev);
  495. const struct pxa_gpio_id *gpio_id;
  496. if (!of_id || !of_id->data) {
  497. dev_err(&pdev->dev, "Failed to find gpio controller\n");
  498. return -EFAULT;
  499. }
  500. gpio_id = of_id->data;
  501. gpio_type = gpio_id->type;
  502. nr_gpios = gpio_id->gpio_nums;
  503. pxa_last_gpio = nr_gpios - 1;
  504. irq_base = irq_alloc_descs(-1, 0, nr_gpios, 0);
  505. if (irq_base < 0) {
  506. dev_err(&pdev->dev, "Failed to allocate IRQ numbers\n");
  507. return irq_base;
  508. }
  509. return irq_base;
  510. }
  511. #else
  512. #define pxa_gpio_probe_dt(pdev, pchip) (-1)
  513. #endif
  514. static int pxa_gpio_probe(struct platform_device *pdev)
  515. {
  516. struct pxa_gpio_chip *pchip;
  517. struct pxa_gpio_bank *c;
  518. struct resource *res;
  519. struct clk *clk;
  520. struct pxa_gpio_platform_data *info;
  521. void __iomem *gpio_reg_base;
  522. int gpio, ret;
  523. int irq0 = 0, irq1 = 0, irq_mux, gpio_offset = 0;
  524. pchip = devm_kzalloc(&pdev->dev, sizeof(*pchip), GFP_KERNEL);
  525. if (!pchip)
  526. return -ENOMEM;
  527. pchip->dev = &pdev->dev;
  528. info = dev_get_platdata(&pdev->dev);
  529. if (info) {
  530. irq_base = info->irq_base;
  531. if (irq_base <= 0)
  532. return -EINVAL;
  533. pxa_last_gpio = pxa_gpio_nums(pdev);
  534. pchip->set_wake = info->gpio_set_wake;
  535. } else {
  536. irq_base = pxa_gpio_probe_dt(pdev, pchip);
  537. if (irq_base < 0)
  538. return -EINVAL;
  539. }
  540. if (!pxa_last_gpio)
  541. return -EINVAL;
  542. pchip->irqdomain = irq_domain_add_legacy(pdev->dev.of_node,
  543. pxa_last_gpio + 1, irq_base,
  544. 0, &pxa_irq_domain_ops, pchip);
  545. if (!pchip->irqdomain)
  546. return -ENOMEM;
  547. irq0 = platform_get_irq_byname(pdev, "gpio0");
  548. irq1 = platform_get_irq_byname(pdev, "gpio1");
  549. irq_mux = platform_get_irq_byname(pdev, "gpio_mux");
  550. if ((irq0 > 0 && irq1 <= 0) || (irq0 <= 0 && irq1 > 0)
  551. || (irq_mux <= 0))
  552. return -EINVAL;
  553. pchip->irq0 = irq0;
  554. pchip->irq1 = irq1;
  555. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  556. gpio_reg_base = devm_ioremap(&pdev->dev, res->start,
  557. resource_size(res));
  558. if (!gpio_reg_base)
  559. return -EINVAL;
  560. if (irq0 > 0)
  561. gpio_offset = 2;
  562. clk = clk_get(&pdev->dev, NULL);
  563. if (IS_ERR(clk)) {
  564. dev_err(&pdev->dev, "Error %ld to get gpio clock\n",
  565. PTR_ERR(clk));
  566. return PTR_ERR(clk);
  567. }
  568. ret = clk_prepare_enable(clk);
  569. if (ret) {
  570. clk_put(clk);
  571. return ret;
  572. }
  573. /* Initialize GPIO chips */
  574. ret = pxa_init_gpio_chip(pchip, pxa_last_gpio + 1, pdev->dev.of_node,
  575. gpio_reg_base);
  576. if (ret) {
  577. clk_put(clk);
  578. return ret;
  579. }
  580. /* clear all GPIO edge detects */
  581. for_each_gpio_bank(gpio, c, pchip) {
  582. writel_relaxed(0, c->regbase + GFER_OFFSET);
  583. writel_relaxed(0, c->regbase + GRER_OFFSET);
  584. writel_relaxed(~0, c->regbase + GEDR_OFFSET);
  585. /* unmask GPIO edge detect for AP side */
  586. if (gpio_is_mmp_type(gpio_type))
  587. writel_relaxed(~0, c->regbase + ED_MASK_OFFSET);
  588. }
  589. if (irq0 > 0) {
  590. ret = devm_request_irq(&pdev->dev,
  591. irq0, pxa_gpio_direct_handler, 0,
  592. "gpio-0", pchip);
  593. if (ret)
  594. dev_err(&pdev->dev, "request of gpio0 irq failed: %d\n",
  595. ret);
  596. }
  597. if (irq1 > 0) {
  598. ret = devm_request_irq(&pdev->dev,
  599. irq1, pxa_gpio_direct_handler, 0,
  600. "gpio-1", pchip);
  601. if (ret)
  602. dev_err(&pdev->dev, "request of gpio1 irq failed: %d\n",
  603. ret);
  604. }
  605. ret = devm_request_irq(&pdev->dev,
  606. irq_mux, pxa_gpio_demux_handler, 0,
  607. "gpio-mux", pchip);
  608. if (ret)
  609. dev_err(&pdev->dev, "request of gpio-mux irq failed: %d\n",
  610. ret);
  611. pxa_gpio_chip = pchip;
  612. return 0;
  613. }
  614. static const struct platform_device_id gpio_id_table[] = {
  615. { "pxa25x-gpio", (unsigned long)&pxa25x_id },
  616. { "pxa26x-gpio", (unsigned long)&pxa26x_id },
  617. { "pxa27x-gpio", (unsigned long)&pxa27x_id },
  618. { "pxa3xx-gpio", (unsigned long)&pxa3xx_id },
  619. { "pxa93x-gpio", (unsigned long)&pxa93x_id },
  620. { "mmp-gpio", (unsigned long)&mmp_id },
  621. { "mmp2-gpio", (unsigned long)&mmp2_id },
  622. { "pxa1928-gpio", (unsigned long)&pxa1928_id },
  623. { },
  624. };
  625. static struct platform_driver pxa_gpio_driver = {
  626. .probe = pxa_gpio_probe,
  627. .driver = {
  628. .name = "pxa-gpio",
  629. .of_match_table = of_match_ptr(pxa_gpio_dt_ids),
  630. },
  631. .id_table = gpio_id_table,
  632. };
  633. static int __init pxa_gpio_legacy_init(void)
  634. {
  635. if (of_have_populated_dt())
  636. return 0;
  637. return platform_driver_register(&pxa_gpio_driver);
  638. }
  639. postcore_initcall(pxa_gpio_legacy_init);
  640. static int __init pxa_gpio_dt_init(void)
  641. {
  642. if (of_have_populated_dt())
  643. return platform_driver_register(&pxa_gpio_driver);
  644. return 0;
  645. }
  646. device_initcall(pxa_gpio_dt_init);
  647. #ifdef CONFIG_PM
  648. static int pxa_gpio_suspend(void)
  649. {
  650. struct pxa_gpio_chip *pchip = pxa_gpio_chip;
  651. struct pxa_gpio_bank *c;
  652. int gpio;
  653. for_each_gpio_bank(gpio, c, pchip) {
  654. c->saved_gplr = readl_relaxed(c->regbase + GPLR_OFFSET);
  655. c->saved_gpdr = readl_relaxed(c->regbase + GPDR_OFFSET);
  656. c->saved_grer = readl_relaxed(c->regbase + GRER_OFFSET);
  657. c->saved_gfer = readl_relaxed(c->regbase + GFER_OFFSET);
  658. /* Clear GPIO transition detect bits */
  659. writel_relaxed(0xffffffff, c->regbase + GEDR_OFFSET);
  660. }
  661. return 0;
  662. }
  663. static void pxa_gpio_resume(void)
  664. {
  665. struct pxa_gpio_chip *pchip = pxa_gpio_chip;
  666. struct pxa_gpio_bank *c;
  667. int gpio;
  668. for_each_gpio_bank(gpio, c, pchip) {
  669. /* restore level with set/clear */
  670. writel_relaxed(c->saved_gplr, c->regbase + GPSR_OFFSET);
  671. writel_relaxed(~c->saved_gplr, c->regbase + GPCR_OFFSET);
  672. writel_relaxed(c->saved_grer, c->regbase + GRER_OFFSET);
  673. writel_relaxed(c->saved_gfer, c->regbase + GFER_OFFSET);
  674. writel_relaxed(c->saved_gpdr, c->regbase + GPDR_OFFSET);
  675. }
  676. }
  677. #else
  678. #define pxa_gpio_suspend NULL
  679. #define pxa_gpio_resume NULL
  680. #endif
  681. struct syscore_ops pxa_gpio_syscore_ops = {
  682. .suspend = pxa_gpio_suspend,
  683. .resume = pxa_gpio_resume,
  684. };
  685. static int __init pxa_gpio_sysinit(void)
  686. {
  687. register_syscore_ops(&pxa_gpio_syscore_ops);
  688. return 0;
  689. }
  690. postcore_initcall(pxa_gpio_sysinit);