gpio.c 25 KB

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