gpio.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  1. /*
  2. * linux/arch/arm/mach-at91/gpio.c
  3. *
  4. * Copyright (C) 2005 HP Labs
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/clk.h>
  12. #include <linux/errno.h>
  13. #include <linux/device.h>
  14. #include <linux/gpio.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/irq.h>
  17. #include <linux/debugfs.h>
  18. #include <linux/seq_file.h>
  19. #include <linux/kernel.h>
  20. #include <linux/list.h>
  21. #include <linux/module.h>
  22. #include <linux/io.h>
  23. #include <linux/irqdomain.h>
  24. #include <linux/of_address.h>
  25. #include <asm/mach/irq.h>
  26. #include <mach/hardware.h>
  27. #include <mach/at91_pio.h>
  28. #include "generic.h"
  29. #define MAX_NB_GPIO_PER_BANK 32
  30. struct at91_gpio_chip {
  31. struct gpio_chip chip;
  32. struct at91_gpio_chip *next; /* Bank sharing same clock */
  33. int pioc_hwirq; /* PIO bank interrupt identifier on AIC */
  34. int pioc_virq; /* PIO bank Linux virtual interrupt */
  35. int pioc_idx; /* PIO bank index */
  36. void __iomem *regbase; /* PIO bank virtual address */
  37. struct clk *clock; /* associated clock */
  38. struct irq_domain *domain; /* associated irq domain */
  39. };
  40. #define to_at91_gpio_chip(c) container_of(c, struct at91_gpio_chip, chip)
  41. static int at91_gpiolib_request(struct gpio_chip *chip, unsigned offset);
  42. static void at91_gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip);
  43. static void at91_gpiolib_set(struct gpio_chip *chip, unsigned offset, int val);
  44. static int at91_gpiolib_get(struct gpio_chip *chip, unsigned offset);
  45. static int at91_gpiolib_direction_output(struct gpio_chip *chip,
  46. unsigned offset, int val);
  47. static int at91_gpiolib_direction_input(struct gpio_chip *chip,
  48. unsigned offset);
  49. static int at91_gpiolib_to_irq(struct gpio_chip *chip, unsigned offset);
  50. #define AT91_GPIO_CHIP(name) \
  51. { \
  52. .chip = { \
  53. .label = name, \
  54. .request = at91_gpiolib_request, \
  55. .direction_input = at91_gpiolib_direction_input, \
  56. .direction_output = at91_gpiolib_direction_output, \
  57. .get = at91_gpiolib_get, \
  58. .set = at91_gpiolib_set, \
  59. .dbg_show = at91_gpiolib_dbg_show, \
  60. .to_irq = at91_gpiolib_to_irq, \
  61. .ngpio = MAX_NB_GPIO_PER_BANK, \
  62. }, \
  63. }
  64. static struct at91_gpio_chip gpio_chip[] = {
  65. AT91_GPIO_CHIP("pioA"),
  66. AT91_GPIO_CHIP("pioB"),
  67. AT91_GPIO_CHIP("pioC"),
  68. AT91_GPIO_CHIP("pioD"),
  69. AT91_GPIO_CHIP("pioE"),
  70. };
  71. static int gpio_banks;
  72. static unsigned long at91_gpio_caps;
  73. /* All PIO controllers support PIO3 features */
  74. #define AT91_GPIO_CAP_PIO3 (1 << 0)
  75. #define has_pio3() (at91_gpio_caps & AT91_GPIO_CAP_PIO3)
  76. /*--------------------------------------------------------------------------*/
  77. static inline void __iomem *pin_to_controller(unsigned pin)
  78. {
  79. pin /= MAX_NB_GPIO_PER_BANK;
  80. if (likely(pin < gpio_banks))
  81. return gpio_chip[pin].regbase;
  82. return NULL;
  83. }
  84. static inline unsigned pin_to_mask(unsigned pin)
  85. {
  86. return 1 << (pin % MAX_NB_GPIO_PER_BANK);
  87. }
  88. static char peripheral_function(void __iomem *pio, unsigned mask)
  89. {
  90. char ret = 'X';
  91. u8 select;
  92. if (pio) {
  93. if (has_pio3()) {
  94. select = !!(__raw_readl(pio + PIO_ABCDSR1) & mask);
  95. select |= (!!(__raw_readl(pio + PIO_ABCDSR2) & mask) << 1);
  96. ret = 'A' + select;
  97. } else {
  98. ret = __raw_readl(pio + PIO_ABSR) & mask ?
  99. 'B' : 'A';
  100. }
  101. }
  102. return ret;
  103. }
  104. /*--------------------------------------------------------------------------*/
  105. /* Not all hardware capabilities are exposed through these calls; they
  106. * only encapsulate the most common features and modes. (So if you
  107. * want to change signals in groups, do it directly.)
  108. *
  109. * Bootloaders will usually handle some of the pin multiplexing setup.
  110. * The intent is certainly that by the time Linux is fully booted, all
  111. * pins should have been fully initialized. These setup calls should
  112. * only be used by board setup routines, or possibly in driver probe().
  113. *
  114. * For bootloaders doing all that setup, these calls could be inlined
  115. * as NOPs so Linux won't duplicate any setup code
  116. */
  117. /*
  118. * mux the pin to the "GPIO" peripheral role.
  119. */
  120. int __init_or_module at91_set_GPIO_periph(unsigned pin, int use_pullup)
  121. {
  122. void __iomem *pio = pin_to_controller(pin);
  123. unsigned mask = pin_to_mask(pin);
  124. if (!pio)
  125. return -EINVAL;
  126. __raw_writel(mask, pio + PIO_IDR);
  127. __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
  128. __raw_writel(mask, pio + PIO_PER);
  129. return 0;
  130. }
  131. EXPORT_SYMBOL(at91_set_GPIO_periph);
  132. /*
  133. * mux the pin to the "A" internal peripheral role.
  134. */
  135. int __init_or_module at91_set_A_periph(unsigned pin, int use_pullup)
  136. {
  137. void __iomem *pio = pin_to_controller(pin);
  138. unsigned mask = pin_to_mask(pin);
  139. if (!pio)
  140. return -EINVAL;
  141. __raw_writel(mask, pio + PIO_IDR);
  142. __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
  143. if (has_pio3()) {
  144. __raw_writel(__raw_readl(pio + PIO_ABCDSR1) & ~mask,
  145. pio + PIO_ABCDSR1);
  146. __raw_writel(__raw_readl(pio + PIO_ABCDSR2) & ~mask,
  147. pio + PIO_ABCDSR2);
  148. } else {
  149. __raw_writel(mask, pio + PIO_ASR);
  150. }
  151. __raw_writel(mask, pio + PIO_PDR);
  152. return 0;
  153. }
  154. EXPORT_SYMBOL(at91_set_A_periph);
  155. /*
  156. * mux the pin to the "B" internal peripheral role.
  157. */
  158. int __init_or_module at91_set_B_periph(unsigned pin, int use_pullup)
  159. {
  160. void __iomem *pio = pin_to_controller(pin);
  161. unsigned mask = pin_to_mask(pin);
  162. if (!pio)
  163. return -EINVAL;
  164. __raw_writel(mask, pio + PIO_IDR);
  165. __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
  166. if (has_pio3()) {
  167. __raw_writel(__raw_readl(pio + PIO_ABCDSR1) | mask,
  168. pio + PIO_ABCDSR1);
  169. __raw_writel(__raw_readl(pio + PIO_ABCDSR2) & ~mask,
  170. pio + PIO_ABCDSR2);
  171. } else {
  172. __raw_writel(mask, pio + PIO_BSR);
  173. }
  174. __raw_writel(mask, pio + PIO_PDR);
  175. return 0;
  176. }
  177. EXPORT_SYMBOL(at91_set_B_periph);
  178. /*
  179. * mux the pin to the "C" internal peripheral role.
  180. */
  181. int __init_or_module at91_set_C_periph(unsigned pin, int use_pullup)
  182. {
  183. void __iomem *pio = pin_to_controller(pin);
  184. unsigned mask = pin_to_mask(pin);
  185. if (!pio || !has_pio3())
  186. return -EINVAL;
  187. __raw_writel(mask, pio + PIO_IDR);
  188. __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
  189. __raw_writel(__raw_readl(pio + PIO_ABCDSR1) & ~mask, pio + PIO_ABCDSR1);
  190. __raw_writel(__raw_readl(pio + PIO_ABCDSR2) | mask, pio + PIO_ABCDSR2);
  191. __raw_writel(mask, pio + PIO_PDR);
  192. return 0;
  193. }
  194. EXPORT_SYMBOL(at91_set_C_periph);
  195. /*
  196. * mux the pin to the "D" internal peripheral role.
  197. */
  198. int __init_or_module at91_set_D_periph(unsigned pin, int use_pullup)
  199. {
  200. void __iomem *pio = pin_to_controller(pin);
  201. unsigned mask = pin_to_mask(pin);
  202. if (!pio || !has_pio3())
  203. return -EINVAL;
  204. __raw_writel(mask, pio + PIO_IDR);
  205. __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
  206. __raw_writel(__raw_readl(pio + PIO_ABCDSR1) | mask, pio + PIO_ABCDSR1);
  207. __raw_writel(__raw_readl(pio + PIO_ABCDSR2) | mask, pio + PIO_ABCDSR2);
  208. __raw_writel(mask, pio + PIO_PDR);
  209. return 0;
  210. }
  211. EXPORT_SYMBOL(at91_set_D_periph);
  212. /*
  213. * mux the pin to the gpio controller (instead of "A", "B", "C"
  214. * or "D" peripheral), and configure it for an input.
  215. */
  216. int __init_or_module at91_set_gpio_input(unsigned pin, int use_pullup)
  217. {
  218. void __iomem *pio = pin_to_controller(pin);
  219. unsigned mask = pin_to_mask(pin);
  220. if (!pio)
  221. return -EINVAL;
  222. __raw_writel(mask, pio + PIO_IDR);
  223. __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
  224. __raw_writel(mask, pio + PIO_ODR);
  225. __raw_writel(mask, pio + PIO_PER);
  226. return 0;
  227. }
  228. EXPORT_SYMBOL(at91_set_gpio_input);
  229. /*
  230. * mux the pin to the gpio controller (instead of "A", "B", "C"
  231. * or "D" peripheral), and configure it for an output.
  232. */
  233. int __init_or_module at91_set_gpio_output(unsigned pin, int value)
  234. {
  235. void __iomem *pio = pin_to_controller(pin);
  236. unsigned mask = pin_to_mask(pin);
  237. if (!pio)
  238. return -EINVAL;
  239. __raw_writel(mask, pio + PIO_IDR);
  240. __raw_writel(mask, pio + PIO_PUDR);
  241. __raw_writel(mask, pio + (value ? PIO_SODR : PIO_CODR));
  242. __raw_writel(mask, pio + PIO_OER);
  243. __raw_writel(mask, pio + PIO_PER);
  244. return 0;
  245. }
  246. EXPORT_SYMBOL(at91_set_gpio_output);
  247. /*
  248. * enable/disable the glitch filter; mostly used with IRQ handling.
  249. */
  250. int __init_or_module at91_set_deglitch(unsigned pin, int is_on)
  251. {
  252. void __iomem *pio = pin_to_controller(pin);
  253. unsigned mask = pin_to_mask(pin);
  254. if (!pio)
  255. return -EINVAL;
  256. if (has_pio3() && is_on)
  257. __raw_writel(mask, pio + PIO_IFSCDR);
  258. __raw_writel(mask, pio + (is_on ? PIO_IFER : PIO_IFDR));
  259. return 0;
  260. }
  261. EXPORT_SYMBOL(at91_set_deglitch);
  262. /*
  263. * enable/disable the debounce filter;
  264. */
  265. int __init_or_module at91_set_debounce(unsigned pin, int is_on, int div)
  266. {
  267. void __iomem *pio = pin_to_controller(pin);
  268. unsigned mask = pin_to_mask(pin);
  269. if (!pio || !has_pio3())
  270. return -EINVAL;
  271. if (is_on) {
  272. __raw_writel(mask, pio + PIO_IFSCER);
  273. __raw_writel(div & PIO_SCDR_DIV, pio + PIO_SCDR);
  274. __raw_writel(mask, pio + PIO_IFER);
  275. } else {
  276. __raw_writel(mask, pio + PIO_IFDR);
  277. }
  278. return 0;
  279. }
  280. EXPORT_SYMBOL(at91_set_debounce);
  281. /*
  282. * enable/disable the multi-driver; This is only valid for output and
  283. * allows the output pin to run as an open collector output.
  284. */
  285. int __init_or_module at91_set_multi_drive(unsigned pin, int is_on)
  286. {
  287. void __iomem *pio = pin_to_controller(pin);
  288. unsigned mask = pin_to_mask(pin);
  289. if (!pio)
  290. return -EINVAL;
  291. __raw_writel(mask, pio + (is_on ? PIO_MDER : PIO_MDDR));
  292. return 0;
  293. }
  294. EXPORT_SYMBOL(at91_set_multi_drive);
  295. /*
  296. * enable/disable the pull-down.
  297. * If pull-up already enabled while calling the function, we disable it.
  298. */
  299. int __init_or_module at91_set_pulldown(unsigned pin, int is_on)
  300. {
  301. void __iomem *pio = pin_to_controller(pin);
  302. unsigned mask = pin_to_mask(pin);
  303. if (!pio || !has_pio3())
  304. return -EINVAL;
  305. /* Disable pull-up anyway */
  306. __raw_writel(mask, pio + PIO_PUDR);
  307. __raw_writel(mask, pio + (is_on ? PIO_PPDER : PIO_PPDDR));
  308. return 0;
  309. }
  310. EXPORT_SYMBOL(at91_set_pulldown);
  311. /*
  312. * disable Schmitt trigger
  313. */
  314. int __init_or_module at91_disable_schmitt_trig(unsigned pin)
  315. {
  316. void __iomem *pio = pin_to_controller(pin);
  317. unsigned mask = pin_to_mask(pin);
  318. if (!pio || !has_pio3())
  319. return -EINVAL;
  320. __raw_writel(__raw_readl(pio + PIO_SCHMITT) | mask, pio + PIO_SCHMITT);
  321. return 0;
  322. }
  323. EXPORT_SYMBOL(at91_disable_schmitt_trig);
  324. /*
  325. * assuming the pin is muxed as a gpio output, set its value.
  326. */
  327. int at91_set_gpio_value(unsigned pin, int value)
  328. {
  329. void __iomem *pio = pin_to_controller(pin);
  330. unsigned mask = pin_to_mask(pin);
  331. if (!pio)
  332. return -EINVAL;
  333. __raw_writel(mask, pio + (value ? PIO_SODR : PIO_CODR));
  334. return 0;
  335. }
  336. EXPORT_SYMBOL(at91_set_gpio_value);
  337. /*
  338. * read the pin's value (works even if it's not muxed as a gpio).
  339. */
  340. int at91_get_gpio_value(unsigned pin)
  341. {
  342. void __iomem *pio = pin_to_controller(pin);
  343. unsigned mask = pin_to_mask(pin);
  344. u32 pdsr;
  345. if (!pio)
  346. return -EINVAL;
  347. pdsr = __raw_readl(pio + PIO_PDSR);
  348. return (pdsr & mask) != 0;
  349. }
  350. EXPORT_SYMBOL(at91_get_gpio_value);
  351. /*--------------------------------------------------------------------------*/
  352. #ifdef CONFIG_PM
  353. static u32 wakeups[MAX_GPIO_BANKS];
  354. static u32 backups[MAX_GPIO_BANKS];
  355. static int gpio_irq_set_wake(struct irq_data *d, unsigned state)
  356. {
  357. struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
  358. unsigned mask = 1 << d->hwirq;
  359. unsigned bank = at91_gpio->pioc_idx;
  360. if (unlikely(bank >= MAX_GPIO_BANKS))
  361. return -EINVAL;
  362. if (state)
  363. wakeups[bank] |= mask;
  364. else
  365. wakeups[bank] &= ~mask;
  366. irq_set_irq_wake(at91_gpio->pioc_virq, state);
  367. return 0;
  368. }
  369. void at91_gpio_suspend(void)
  370. {
  371. int i;
  372. for (i = 0; i < gpio_banks; i++) {
  373. void __iomem *pio = gpio_chip[i].regbase;
  374. backups[i] = __raw_readl(pio + PIO_IMR);
  375. __raw_writel(backups[i], pio + PIO_IDR);
  376. __raw_writel(wakeups[i], pio + PIO_IER);
  377. if (!wakeups[i]) {
  378. clk_unprepare(gpio_chip[i].clock);
  379. clk_disable(gpio_chip[i].clock);
  380. } else {
  381. #ifdef CONFIG_PM_DEBUG
  382. printk(KERN_DEBUG "GPIO-%c may wake for %08x\n", 'A'+i, wakeups[i]);
  383. #endif
  384. }
  385. }
  386. }
  387. void at91_gpio_resume(void)
  388. {
  389. int i;
  390. for (i = 0; i < gpio_banks; i++) {
  391. void __iomem *pio = gpio_chip[i].regbase;
  392. if (!wakeups[i]) {
  393. if (clk_prepare(gpio_chip[i].clock) == 0)
  394. clk_enable(gpio_chip[i].clock);
  395. }
  396. __raw_writel(wakeups[i], pio + PIO_IDR);
  397. __raw_writel(backups[i], pio + PIO_IER);
  398. }
  399. }
  400. #else
  401. #define gpio_irq_set_wake NULL
  402. #endif
  403. /* Several AIC controller irqs are dispatched through this GPIO handler.
  404. * To use any AT91_PIN_* as an externally triggered IRQ, first call
  405. * at91_set_gpio_input() then maybe enable its glitch filter.
  406. * Then just request_irq() with the pin ID; it works like any ARM IRQ
  407. * handler.
  408. * First implementation always triggers on rising and falling edges
  409. * whereas the newer PIO3 can be additionally configured to trigger on
  410. * level, edge with any polarity.
  411. *
  412. * Alternatively, certain pins may be used directly as IRQ0..IRQ6 after
  413. * configuring them with at91_set_a_periph() or at91_set_b_periph().
  414. * IRQ0..IRQ6 should be configurable, e.g. level vs edge triggering.
  415. */
  416. static void gpio_irq_mask(struct irq_data *d)
  417. {
  418. struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
  419. void __iomem *pio = at91_gpio->regbase;
  420. unsigned mask = 1 << d->hwirq;
  421. if (pio)
  422. __raw_writel(mask, pio + PIO_IDR);
  423. }
  424. static void gpio_irq_unmask(struct irq_data *d)
  425. {
  426. struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
  427. void __iomem *pio = at91_gpio->regbase;
  428. unsigned mask = 1 << d->hwirq;
  429. if (pio)
  430. __raw_writel(mask, pio + PIO_IER);
  431. }
  432. static int gpio_irq_type(struct irq_data *d, unsigned type)
  433. {
  434. switch (type) {
  435. case IRQ_TYPE_NONE:
  436. case IRQ_TYPE_EDGE_BOTH:
  437. return 0;
  438. default:
  439. return -EINVAL;
  440. }
  441. }
  442. /* Alternate irq type for PIO3 support */
  443. static int alt_gpio_irq_type(struct irq_data *d, unsigned type)
  444. {
  445. struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
  446. void __iomem *pio = at91_gpio->regbase;
  447. unsigned mask = 1 << d->hwirq;
  448. switch (type) {
  449. case IRQ_TYPE_EDGE_RISING:
  450. __raw_writel(mask, pio + PIO_ESR);
  451. __raw_writel(mask, pio + PIO_REHLSR);
  452. break;
  453. case IRQ_TYPE_EDGE_FALLING:
  454. __raw_writel(mask, pio + PIO_ESR);
  455. __raw_writel(mask, pio + PIO_FELLSR);
  456. break;
  457. case IRQ_TYPE_LEVEL_LOW:
  458. __raw_writel(mask, pio + PIO_LSR);
  459. __raw_writel(mask, pio + PIO_FELLSR);
  460. break;
  461. case IRQ_TYPE_LEVEL_HIGH:
  462. __raw_writel(mask, pio + PIO_LSR);
  463. __raw_writel(mask, pio + PIO_REHLSR);
  464. break;
  465. case IRQ_TYPE_EDGE_BOTH:
  466. /*
  467. * disable additional interrupt modes:
  468. * fall back to default behavior
  469. */
  470. __raw_writel(mask, pio + PIO_AIMDR);
  471. return 0;
  472. case IRQ_TYPE_NONE:
  473. default:
  474. pr_warn("AT91: No type for irq %d\n", gpio_to_irq(d->irq));
  475. return -EINVAL;
  476. }
  477. /* enable additional interrupt modes */
  478. __raw_writel(mask, pio + PIO_AIMER);
  479. return 0;
  480. }
  481. static struct irq_chip gpio_irqchip = {
  482. .name = "GPIO",
  483. .irq_disable = gpio_irq_mask,
  484. .irq_mask = gpio_irq_mask,
  485. .irq_unmask = gpio_irq_unmask,
  486. /* .irq_set_type is set dynamically */
  487. .irq_set_wake = gpio_irq_set_wake,
  488. };
  489. static void gpio_irq_handler(unsigned irq, struct irq_desc *desc)
  490. {
  491. struct irq_chip *chip = irq_desc_get_chip(desc);
  492. struct irq_data *idata = irq_desc_get_irq_data(desc);
  493. struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(idata);
  494. void __iomem *pio = at91_gpio->regbase;
  495. unsigned long isr;
  496. int n;
  497. chained_irq_enter(chip, desc);
  498. for (;;) {
  499. /* Reading ISR acks pending (edge triggered) GPIO interrupts.
  500. * When there none are pending, we're finished unless we need
  501. * to process multiple banks (like ID_PIOCDE on sam9263).
  502. */
  503. isr = __raw_readl(pio + PIO_ISR) & __raw_readl(pio + PIO_IMR);
  504. if (!isr) {
  505. if (!at91_gpio->next)
  506. break;
  507. at91_gpio = at91_gpio->next;
  508. pio = at91_gpio->regbase;
  509. continue;
  510. }
  511. n = find_first_bit(&isr, BITS_PER_LONG);
  512. while (n < BITS_PER_LONG) {
  513. generic_handle_irq(irq_find_mapping(at91_gpio->domain, n));
  514. n = find_next_bit(&isr, BITS_PER_LONG, n + 1);
  515. }
  516. }
  517. chained_irq_exit(chip, desc);
  518. /* now it may re-trigger */
  519. }
  520. /*--------------------------------------------------------------------------*/
  521. #ifdef CONFIG_DEBUG_FS
  522. static void gpio_printf(struct seq_file *s, void __iomem *pio, unsigned mask)
  523. {
  524. char *trigger = NULL;
  525. char *polarity = NULL;
  526. if (__raw_readl(pio + PIO_IMR) & mask) {
  527. if (!has_pio3() || !(__raw_readl(pio + PIO_AIMMR) & mask )) {
  528. trigger = "edge";
  529. polarity = "both";
  530. } else {
  531. if (__raw_readl(pio + PIO_ELSR) & mask) {
  532. trigger = "level";
  533. polarity = __raw_readl(pio + PIO_FRLHSR) & mask ?
  534. "high" : "low";
  535. } else {
  536. trigger = "edge";
  537. polarity = __raw_readl(pio + PIO_FRLHSR) & mask ?
  538. "rising" : "falling";
  539. }
  540. }
  541. seq_printf(s, "IRQ:%s-%s\t", trigger, polarity);
  542. } else {
  543. seq_printf(s, "GPIO:%s\t\t",
  544. __raw_readl(pio + PIO_PDSR) & mask ? "1" : "0");
  545. }
  546. }
  547. static int at91_gpio_show(struct seq_file *s, void *unused)
  548. {
  549. int bank, j;
  550. /* print heading */
  551. seq_printf(s, "Pin\t");
  552. for (bank = 0; bank < gpio_banks; bank++) {
  553. seq_printf(s, "PIO%c\t\t", 'A' + bank);
  554. };
  555. seq_printf(s, "\n\n");
  556. /* print pin status */
  557. for (j = 0; j < 32; j++) {
  558. seq_printf(s, "%i:\t", j);
  559. for (bank = 0; bank < gpio_banks; bank++) {
  560. unsigned pin = (32 * bank) + j;
  561. void __iomem *pio = pin_to_controller(pin);
  562. unsigned mask = pin_to_mask(pin);
  563. if (__raw_readl(pio + PIO_PSR) & mask)
  564. gpio_printf(s, pio, mask);
  565. else
  566. seq_printf(s, "%c\t\t",
  567. peripheral_function(pio, mask));
  568. }
  569. seq_printf(s, "\n");
  570. }
  571. return 0;
  572. }
  573. static int at91_gpio_open(struct inode *inode, struct file *file)
  574. {
  575. return single_open(file, at91_gpio_show, NULL);
  576. }
  577. static const struct file_operations at91_gpio_operations = {
  578. .open = at91_gpio_open,
  579. .read = seq_read,
  580. .llseek = seq_lseek,
  581. .release = single_release,
  582. };
  583. static int __init at91_gpio_debugfs_init(void)
  584. {
  585. /* /sys/kernel/debug/at91_gpio */
  586. (void) debugfs_create_file("at91_gpio", S_IFREG | S_IRUGO, NULL, NULL, &at91_gpio_operations);
  587. return 0;
  588. }
  589. postcore_initcall(at91_gpio_debugfs_init);
  590. #endif
  591. /*--------------------------------------------------------------------------*/
  592. /*
  593. * This lock class tells lockdep that GPIO irqs are in a different
  594. * category than their parents, so it won't report false recursion.
  595. */
  596. static struct lock_class_key gpio_lock_class;
  597. /*
  598. * irqdomain initialization: pile up irqdomains on top of AIC range
  599. */
  600. static void __init at91_gpio_irqdomain(struct at91_gpio_chip *at91_gpio)
  601. {
  602. int irq_base;
  603. irq_base = irq_alloc_descs(-1, 0, at91_gpio->chip.ngpio, 0);
  604. if (irq_base < 0)
  605. panic("at91_gpio.%d: error %d: couldn't allocate IRQ numbers.\n",
  606. at91_gpio->pioc_idx, irq_base);
  607. at91_gpio->domain = irq_domain_add_legacy(NULL, at91_gpio->chip.ngpio,
  608. irq_base, 0,
  609. &irq_domain_simple_ops, NULL);
  610. if (!at91_gpio->domain)
  611. panic("at91_gpio.%d: couldn't allocate irq domain.\n",
  612. at91_gpio->pioc_idx);
  613. }
  614. /*
  615. * Called from the processor-specific init to enable GPIO interrupt support.
  616. */
  617. void __init at91_gpio_irq_setup(void)
  618. {
  619. unsigned pioc;
  620. int gpio_irqnbr = 0;
  621. struct at91_gpio_chip *this, *prev;
  622. /* Setup proper .irq_set_type function */
  623. if (has_pio3())
  624. gpio_irqchip.irq_set_type = alt_gpio_irq_type;
  625. else
  626. gpio_irqchip.irq_set_type = gpio_irq_type;
  627. for (pioc = 0, this = gpio_chip, prev = NULL;
  628. pioc++ < gpio_banks;
  629. prev = this, this++) {
  630. int offset;
  631. __raw_writel(~0, this->regbase + PIO_IDR);
  632. /* setup irq domain for this GPIO controller */
  633. at91_gpio_irqdomain(this);
  634. for (offset = 0; offset < this->chip.ngpio; offset++) {
  635. unsigned int virq = irq_find_mapping(this->domain, offset);
  636. irq_set_lockdep_class(virq, &gpio_lock_class);
  637. /*
  638. * Can use the "simple" and not "edge" handler since it's
  639. * shorter, and the AIC handles interrupts sanely.
  640. */
  641. irq_set_chip_and_handler(virq, &gpio_irqchip,
  642. handle_simple_irq);
  643. set_irq_flags(virq, IRQF_VALID);
  644. irq_set_chip_data(virq, this);
  645. gpio_irqnbr++;
  646. }
  647. /* The toplevel handler handles one bank of GPIOs, except
  648. * on some SoC it can handles up to three...
  649. * We only set up the handler for the first of the list.
  650. */
  651. if (prev && prev->next == this)
  652. continue;
  653. this->pioc_virq = irq_create_mapping(NULL, this->pioc_hwirq);
  654. irq_set_chip_data(this->pioc_virq, this);
  655. irq_set_chained_handler(this->pioc_virq, gpio_irq_handler);
  656. }
  657. pr_info("AT91: %d gpio irqs in %d banks\n", gpio_irqnbr, gpio_banks);
  658. }
  659. /* gpiolib support */
  660. static int at91_gpiolib_request(struct gpio_chip *chip, unsigned offset)
  661. {
  662. struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
  663. void __iomem *pio = at91_gpio->regbase;
  664. unsigned mask = 1 << offset;
  665. __raw_writel(mask, pio + PIO_PER);
  666. return 0;
  667. }
  668. static int at91_gpiolib_direction_input(struct gpio_chip *chip,
  669. unsigned offset)
  670. {
  671. struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
  672. void __iomem *pio = at91_gpio->regbase;
  673. unsigned mask = 1 << offset;
  674. __raw_writel(mask, pio + PIO_ODR);
  675. return 0;
  676. }
  677. static int at91_gpiolib_direction_output(struct gpio_chip *chip,
  678. unsigned offset, int val)
  679. {
  680. struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
  681. void __iomem *pio = at91_gpio->regbase;
  682. unsigned mask = 1 << offset;
  683. __raw_writel(mask, pio + (val ? PIO_SODR : PIO_CODR));
  684. __raw_writel(mask, pio + PIO_OER);
  685. return 0;
  686. }
  687. static int at91_gpiolib_get(struct gpio_chip *chip, unsigned offset)
  688. {
  689. struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
  690. void __iomem *pio = at91_gpio->regbase;
  691. unsigned mask = 1 << offset;
  692. u32 pdsr;
  693. pdsr = __raw_readl(pio + PIO_PDSR);
  694. return (pdsr & mask) != 0;
  695. }
  696. static void at91_gpiolib_set(struct gpio_chip *chip, unsigned offset, int val)
  697. {
  698. struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
  699. void __iomem *pio = at91_gpio->regbase;
  700. unsigned mask = 1 << offset;
  701. __raw_writel(mask, pio + (val ? PIO_SODR : PIO_CODR));
  702. }
  703. static void at91_gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  704. {
  705. int i;
  706. for (i = 0; i < chip->ngpio; i++) {
  707. unsigned pin = chip->base + i;
  708. void __iomem *pio = pin_to_controller(pin);
  709. unsigned mask = pin_to_mask(pin);
  710. const char *gpio_label;
  711. gpio_label = gpiochip_is_requested(chip, i);
  712. if (gpio_label) {
  713. seq_printf(s, "[%s] GPIO%s%d: ",
  714. gpio_label, chip->label, i);
  715. if (__raw_readl(pio + PIO_PSR) & mask)
  716. seq_printf(s, "[gpio] %s\n",
  717. at91_get_gpio_value(pin) ?
  718. "set" : "clear");
  719. else
  720. seq_printf(s, "[periph %c]\n",
  721. peripheral_function(pio, mask));
  722. }
  723. }
  724. }
  725. static int at91_gpiolib_to_irq(struct gpio_chip *chip, unsigned offset)
  726. {
  727. struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
  728. int virq;
  729. if (offset < chip->ngpio)
  730. virq = irq_create_mapping(at91_gpio->domain, offset);
  731. else
  732. virq = -ENXIO;
  733. dev_dbg(chip->dev, "%s: request IRQ for GPIO %d, return %d\n",
  734. chip->label, offset + chip->base, virq);
  735. return virq;
  736. }
  737. static int __init at91_gpio_setup_clk(int idx)
  738. {
  739. struct at91_gpio_chip *at91_gpio = &gpio_chip[idx];
  740. /* retreive PIO controller's clock */
  741. at91_gpio->clock = clk_get_sys(NULL, at91_gpio->chip.label);
  742. if (IS_ERR(at91_gpio->clock)) {
  743. pr_err("at91_gpio.%d, failed to get clock, ignoring.\n", idx);
  744. goto err;
  745. }
  746. if (clk_prepare(at91_gpio->clock))
  747. goto clk_prep_err;
  748. /* enable PIO controller's clock */
  749. if (clk_enable(at91_gpio->clock)) {
  750. pr_err("at91_gpio.%d, failed to enable clock, ignoring.\n", idx);
  751. goto clk_err;
  752. }
  753. return 0;
  754. clk_err:
  755. clk_unprepare(at91_gpio->clock);
  756. clk_prep_err:
  757. clk_put(at91_gpio->clock);
  758. err:
  759. return -EINVAL;
  760. }
  761. static void __init at91_gpio_init_one(int idx, u32 regbase, int pioc_hwirq)
  762. {
  763. struct at91_gpio_chip *at91_gpio = &gpio_chip[idx];
  764. at91_gpio->chip.base = idx * MAX_NB_GPIO_PER_BANK;
  765. at91_gpio->pioc_hwirq = pioc_hwirq;
  766. at91_gpio->pioc_idx = idx;
  767. at91_gpio->regbase = ioremap(regbase, 512);
  768. if (!at91_gpio->regbase) {
  769. pr_err("at91_gpio.%d, failed to map registers, ignoring.\n", idx);
  770. return;
  771. }
  772. if (at91_gpio_setup_clk(idx))
  773. goto ioremap_err;
  774. gpio_banks = max(gpio_banks, idx + 1);
  775. return;
  776. ioremap_err:
  777. iounmap(at91_gpio->regbase);
  778. }
  779. /*
  780. * Called from the processor-specific init to enable GPIO pin support.
  781. */
  782. void __init at91_gpio_init(struct at91_gpio_bank *data, int nr_banks)
  783. {
  784. unsigned i;
  785. struct at91_gpio_chip *at91_gpio, *last = NULL;
  786. BUG_ON(nr_banks > MAX_GPIO_BANKS);
  787. if (of_have_populated_dt())
  788. return;
  789. for (i = 0; i < nr_banks; i++)
  790. at91_gpio_init_one(i, data[i].regbase, data[i].id);
  791. for (i = 0; i < gpio_banks; i++) {
  792. at91_gpio = &gpio_chip[i];
  793. /*
  794. * GPIO controller are grouped on some SoC:
  795. * PIOC, PIOD and PIOE can share the same IRQ line
  796. */
  797. if (last && last->pioc_hwirq == at91_gpio->pioc_hwirq)
  798. last->next = at91_gpio;
  799. last = at91_gpio;
  800. gpiochip_add(&at91_gpio->chip);
  801. }
  802. }