gpio.c 28 KB

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