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 <asm/mach/irq.h>
  28. #include <mach/hardware.h>
  29. #include <mach/at91_pio.h>
  30. #include "generic.h"
  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 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, nr_gpio) \
  51. { \
  52. .chip = { \
  53. .label = name, \
  54. .direction_input = at91_gpiolib_direction_input, \
  55. .direction_output = at91_gpiolib_direction_output, \
  56. .get = at91_gpiolib_get, \
  57. .set = at91_gpiolib_set, \
  58. .dbg_show = at91_gpiolib_dbg_show, \
  59. .to_irq = at91_gpiolib_to_irq, \
  60. .ngpio = nr_gpio, \
  61. }, \
  62. }
  63. static struct at91_gpio_chip gpio_chip[] = {
  64. AT91_GPIO_CHIP("pioA", 32),
  65. AT91_GPIO_CHIP("pioB", 32),
  66. AT91_GPIO_CHIP("pioC", 32),
  67. AT91_GPIO_CHIP("pioD", 32),
  68. AT91_GPIO_CHIP("pioE", 32),
  69. };
  70. static int gpio_banks;
  71. static unsigned long at91_gpio_caps;
  72. /* All PIO controllers support PIO3 features */
  73. #define AT91_GPIO_CAP_PIO3 (1 << 0)
  74. #define has_pio3() (at91_gpio_caps & AT91_GPIO_CAP_PIO3)
  75. /*--------------------------------------------------------------------------*/
  76. static inline void __iomem *pin_to_controller(unsigned pin)
  77. {
  78. pin /= 32;
  79. if (likely(pin < gpio_banks))
  80. return gpio_chip[pin].regbase;
  81. return NULL;
  82. }
  83. static inline unsigned pin_to_mask(unsigned pin)
  84. {
  85. return 1 << (pin % 32);
  86. }
  87. static char peripheral_function(void __iomem *pio, unsigned mask)
  88. {
  89. char ret = 'X';
  90. u8 select;
  91. if (pio) {
  92. if (has_pio3()) {
  93. select = !!(__raw_readl(pio + PIO_ABCDSR1) & mask);
  94. select |= (!!(__raw_readl(pio + PIO_ABCDSR2) & mask) << 1);
  95. ret = 'A' + select;
  96. } else {
  97. ret = __raw_readl(pio + PIO_ABSR) & mask ?
  98. 'B' : 'A';
  99. }
  100. }
  101. return ret;
  102. }
  103. /*--------------------------------------------------------------------------*/
  104. /* Not all hardware capabilities are exposed through these calls; they
  105. * only encapsulate the most common features and modes. (So if you
  106. * want to change signals in groups, do it directly.)
  107. *
  108. * Bootloaders will usually handle some of the pin multiplexing setup.
  109. * The intent is certainly that by the time Linux is fully booted, all
  110. * pins should have been fully initialized. These setup calls should
  111. * only be used by board setup routines, or possibly in driver probe().
  112. *
  113. * For bootloaders doing all that setup, these calls could be inlined
  114. * as NOPs so Linux won't duplicate any setup code
  115. */
  116. /*
  117. * mux the pin to the "GPIO" peripheral role.
  118. */
  119. int __init_or_module at91_set_GPIO_periph(unsigned pin, int use_pullup)
  120. {
  121. void __iomem *pio = pin_to_controller(pin);
  122. unsigned mask = pin_to_mask(pin);
  123. if (!pio)
  124. return -EINVAL;
  125. __raw_writel(mask, pio + PIO_IDR);
  126. __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
  127. __raw_writel(mask, pio + PIO_PER);
  128. return 0;
  129. }
  130. EXPORT_SYMBOL(at91_set_GPIO_periph);
  131. /*
  132. * mux the pin to the "A" internal peripheral role.
  133. */
  134. int __init_or_module at91_set_A_periph(unsigned pin, int use_pullup)
  135. {
  136. void __iomem *pio = pin_to_controller(pin);
  137. unsigned mask = pin_to_mask(pin);
  138. if (!pio)
  139. return -EINVAL;
  140. __raw_writel(mask, pio + PIO_IDR);
  141. __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
  142. if (has_pio3()) {
  143. __raw_writel(__raw_readl(pio + PIO_ABCDSR1) & ~mask,
  144. pio + PIO_ABCDSR1);
  145. __raw_writel(__raw_readl(pio + PIO_ABCDSR2) & ~mask,
  146. pio + PIO_ABCDSR2);
  147. } else {
  148. __raw_writel(mask, pio + PIO_ASR);
  149. }
  150. __raw_writel(mask, pio + PIO_PDR);
  151. return 0;
  152. }
  153. EXPORT_SYMBOL(at91_set_A_periph);
  154. /*
  155. * mux the pin to the "B" internal peripheral role.
  156. */
  157. int __init_or_module at91_set_B_periph(unsigned pin, int use_pullup)
  158. {
  159. void __iomem *pio = pin_to_controller(pin);
  160. unsigned mask = pin_to_mask(pin);
  161. if (!pio)
  162. return -EINVAL;
  163. __raw_writel(mask, pio + PIO_IDR);
  164. __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
  165. if (has_pio3()) {
  166. __raw_writel(__raw_readl(pio + PIO_ABCDSR1) | mask,
  167. pio + PIO_ABCDSR1);
  168. __raw_writel(__raw_readl(pio + PIO_ABCDSR2) & ~mask,
  169. pio + PIO_ABCDSR2);
  170. } else {
  171. __raw_writel(mask, pio + PIO_BSR);
  172. }
  173. __raw_writel(mask, pio + PIO_PDR);
  174. return 0;
  175. }
  176. EXPORT_SYMBOL(at91_set_B_periph);
  177. /*
  178. * mux the pin to the "C" internal peripheral role.
  179. */
  180. int __init_or_module at91_set_C_periph(unsigned pin, int use_pullup)
  181. {
  182. void __iomem *pio = pin_to_controller(pin);
  183. unsigned mask = pin_to_mask(pin);
  184. if (!pio || !has_pio3())
  185. return -EINVAL;
  186. __raw_writel(mask, pio + PIO_IDR);
  187. __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
  188. __raw_writel(__raw_readl(pio + PIO_ABCDSR1) & ~mask, pio + PIO_ABCDSR1);
  189. __raw_writel(__raw_readl(pio + PIO_ABCDSR2) | mask, pio + PIO_ABCDSR2);
  190. __raw_writel(mask, pio + PIO_PDR);
  191. return 0;
  192. }
  193. EXPORT_SYMBOL(at91_set_C_periph);
  194. /*
  195. * mux the pin to the "D" internal peripheral role.
  196. */
  197. int __init_or_module at91_set_D_periph(unsigned pin, int use_pullup)
  198. {
  199. void __iomem *pio = pin_to_controller(pin);
  200. unsigned mask = pin_to_mask(pin);
  201. if (!pio || !has_pio3())
  202. return -EINVAL;
  203. __raw_writel(mask, pio + PIO_IDR);
  204. __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
  205. __raw_writel(__raw_readl(pio + PIO_ABCDSR1) | mask, pio + PIO_ABCDSR1);
  206. __raw_writel(__raw_readl(pio + PIO_ABCDSR2) | mask, pio + PIO_ABCDSR2);
  207. __raw_writel(mask, pio + PIO_PDR);
  208. return 0;
  209. }
  210. EXPORT_SYMBOL(at91_set_D_periph);
  211. /*
  212. * mux the pin to the gpio controller (instead of "A", "B", "C"
  213. * or "D" peripheral), and configure it for an input.
  214. */
  215. int __init_or_module at91_set_gpio_input(unsigned pin, int use_pullup)
  216. {
  217. void __iomem *pio = pin_to_controller(pin);
  218. unsigned mask = pin_to_mask(pin);
  219. if (!pio)
  220. return -EINVAL;
  221. __raw_writel(mask, pio + PIO_IDR);
  222. __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
  223. __raw_writel(mask, pio + PIO_ODR);
  224. __raw_writel(mask, pio + PIO_PER);
  225. return 0;
  226. }
  227. EXPORT_SYMBOL(at91_set_gpio_input);
  228. /*
  229. * mux the pin to the gpio controller (instead of "A", "B", "C"
  230. * or "D" peripheral), and configure it for an output.
  231. */
  232. int __init_or_module at91_set_gpio_output(unsigned pin, int value)
  233. {
  234. void __iomem *pio = pin_to_controller(pin);
  235. unsigned mask = pin_to_mask(pin);
  236. if (!pio)
  237. return -EINVAL;
  238. __raw_writel(mask, pio + PIO_IDR);
  239. __raw_writel(mask, pio + PIO_PUDR);
  240. __raw_writel(mask, pio + (value ? PIO_SODR : PIO_CODR));
  241. __raw_writel(mask, pio + PIO_OER);
  242. __raw_writel(mask, pio + PIO_PER);
  243. return 0;
  244. }
  245. EXPORT_SYMBOL(at91_set_gpio_output);
  246. /*
  247. * enable/disable the glitch filter; mostly used with IRQ handling.
  248. */
  249. int __init_or_module at91_set_deglitch(unsigned pin, int is_on)
  250. {
  251. void __iomem *pio = pin_to_controller(pin);
  252. unsigned mask = pin_to_mask(pin);
  253. if (!pio)
  254. return -EINVAL;
  255. if (has_pio3() && is_on)
  256. __raw_writel(mask, pio + PIO_IFSCDR);
  257. __raw_writel(mask, pio + (is_on ? PIO_IFER : PIO_IFDR));
  258. return 0;
  259. }
  260. EXPORT_SYMBOL(at91_set_deglitch);
  261. /*
  262. * enable/disable the debounce filter;
  263. */
  264. int __init_or_module at91_set_debounce(unsigned pin, int is_on, int div)
  265. {
  266. void __iomem *pio = pin_to_controller(pin);
  267. unsigned mask = pin_to_mask(pin);
  268. if (!pio || !has_pio3())
  269. return -EINVAL;
  270. if (is_on) {
  271. __raw_writel(mask, pio + PIO_IFSCER);
  272. __raw_writel(div & PIO_SCDR_DIV, pio + PIO_SCDR);
  273. __raw_writel(mask, pio + PIO_IFER);
  274. } else {
  275. __raw_writel(mask, pio + PIO_IFDR);
  276. }
  277. return 0;
  278. }
  279. EXPORT_SYMBOL(at91_set_debounce);
  280. /*
  281. * enable/disable the multi-driver; This is only valid for output and
  282. * allows the output pin to run as an open collector output.
  283. */
  284. int __init_or_module at91_set_multi_drive(unsigned pin, int is_on)
  285. {
  286. void __iomem *pio = pin_to_controller(pin);
  287. unsigned mask = pin_to_mask(pin);
  288. if (!pio)
  289. return -EINVAL;
  290. __raw_writel(mask, pio + (is_on ? PIO_MDER : PIO_MDDR));
  291. return 0;
  292. }
  293. EXPORT_SYMBOL(at91_set_multi_drive);
  294. /*
  295. * enable/disable the pull-down.
  296. * If pull-up already enabled while calling the function, we disable it.
  297. */
  298. int __init_or_module at91_set_pulldown(unsigned pin, int is_on)
  299. {
  300. void __iomem *pio = pin_to_controller(pin);
  301. unsigned mask = pin_to_mask(pin);
  302. if (!pio || !has_pio3())
  303. return -EINVAL;
  304. /* Disable pull-up anyway */
  305. __raw_writel(mask, pio + PIO_PUDR);
  306. __raw_writel(mask, pio + (is_on ? PIO_PPDER : PIO_PPDDR));
  307. return 0;
  308. }
  309. EXPORT_SYMBOL(at91_set_pulldown);
  310. /*
  311. * disable Schmitt trigger
  312. */
  313. int __init_or_module at91_disable_schmitt_trig(unsigned pin)
  314. {
  315. void __iomem *pio = pin_to_controller(pin);
  316. unsigned mask = pin_to_mask(pin);
  317. if (!pio || !has_pio3())
  318. return -EINVAL;
  319. __raw_writel(__raw_readl(pio + PIO_SCHMITT) | mask, pio + PIO_SCHMITT);
  320. return 0;
  321. }
  322. EXPORT_SYMBOL(at91_disable_schmitt_trig);
  323. /*
  324. * assuming the pin is muxed as a gpio output, set its value.
  325. */
  326. int at91_set_gpio_value(unsigned pin, int value)
  327. {
  328. void __iomem *pio = pin_to_controller(pin);
  329. unsigned mask = pin_to_mask(pin);
  330. if (!pio)
  331. return -EINVAL;
  332. __raw_writel(mask, pio + (value ? PIO_SODR : PIO_CODR));
  333. return 0;
  334. }
  335. EXPORT_SYMBOL(at91_set_gpio_value);
  336. /*
  337. * read the pin's value (works even if it's not muxed as a gpio).
  338. */
  339. int at91_get_gpio_value(unsigned pin)
  340. {
  341. void __iomem *pio = pin_to_controller(pin);
  342. unsigned mask = pin_to_mask(pin);
  343. u32 pdsr;
  344. if (!pio)
  345. return -EINVAL;
  346. pdsr = __raw_readl(pio + PIO_PDSR);
  347. return (pdsr & mask) != 0;
  348. }
  349. EXPORT_SYMBOL(at91_get_gpio_value);
  350. /*--------------------------------------------------------------------------*/
  351. #ifdef CONFIG_PM
  352. static u32 wakeups[MAX_GPIO_BANKS];
  353. static u32 backups[MAX_GPIO_BANKS];
  354. static int gpio_irq_set_wake(struct irq_data *d, unsigned state)
  355. {
  356. struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
  357. unsigned mask = 1 << d->hwirq;
  358. unsigned bank = at91_gpio->pioc_idx;
  359. if (unlikely(bank >= MAX_GPIO_BANKS))
  360. return -EINVAL;
  361. if (state)
  362. wakeups[bank] |= mask;
  363. else
  364. wakeups[bank] &= ~mask;
  365. irq_set_irq_wake(at91_gpio->pioc_virq, state);
  366. return 0;
  367. }
  368. void at91_gpio_suspend(void)
  369. {
  370. int i;
  371. for (i = 0; i < gpio_banks; i++) {
  372. void __iomem *pio = gpio_chip[i].regbase;
  373. backups[i] = __raw_readl(pio + PIO_IMR);
  374. __raw_writel(backups[i], pio + PIO_IDR);
  375. __raw_writel(wakeups[i], pio + PIO_IER);
  376. if (!wakeups[i]) {
  377. clk_unprepare(gpio_chip[i].clock);
  378. clk_disable(gpio_chip[i].clock);
  379. } else {
  380. #ifdef CONFIG_PM_DEBUG
  381. printk(KERN_DEBUG "GPIO-%c may wake for %08x\n", 'A'+i, wakeups[i]);
  382. #endif
  383. }
  384. }
  385. }
  386. void at91_gpio_resume(void)
  387. {
  388. int i;
  389. for (i = 0; i < gpio_banks; i++) {
  390. void __iomem *pio = gpio_chip[i].regbase;
  391. if (!wakeups[i]) {
  392. if (clk_prepare(gpio_chip[i].clock) == 0)
  393. clk_enable(gpio_chip[i].clock);
  394. }
  395. __raw_writel(wakeups[i], pio + PIO_IDR);
  396. __raw_writel(backups[i], pio + PIO_IER);
  397. }
  398. }
  399. #else
  400. #define gpio_irq_set_wake NULL
  401. #endif
  402. /* Several AIC controller irqs are dispatched through this GPIO handler.
  403. * To use any AT91_PIN_* as an externally triggered IRQ, first call
  404. * at91_set_gpio_input() then maybe enable its glitch filter.
  405. * Then just request_irq() with the pin ID; it works like any ARM IRQ
  406. * handler.
  407. * First implementation always triggers on rising and falling edges
  408. * whereas the newer PIO3 can be additionally configured to trigger on
  409. * level, edge with any polarity.
  410. *
  411. * Alternatively, certain pins may be used directly as IRQ0..IRQ6 after
  412. * configuring them with at91_set_a_periph() or at91_set_b_periph().
  413. * IRQ0..IRQ6 should be configurable, e.g. level vs edge triggering.
  414. */
  415. static void gpio_irq_mask(struct irq_data *d)
  416. {
  417. struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
  418. void __iomem *pio = at91_gpio->regbase;
  419. unsigned mask = 1 << d->hwirq;
  420. if (pio)
  421. __raw_writel(mask, pio + PIO_IDR);
  422. }
  423. static void gpio_irq_unmask(struct irq_data *d)
  424. {
  425. struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
  426. void __iomem *pio = at91_gpio->regbase;
  427. unsigned mask = 1 << d->hwirq;
  428. if (pio)
  429. __raw_writel(mask, pio + PIO_IER);
  430. }
  431. static int gpio_irq_type(struct irq_data *d, unsigned type)
  432. {
  433. switch (type) {
  434. case IRQ_TYPE_NONE:
  435. case IRQ_TYPE_EDGE_BOTH:
  436. return 0;
  437. default:
  438. return -EINVAL;
  439. }
  440. }
  441. /* Alternate irq type for PIO3 support */
  442. static int alt_gpio_irq_type(struct irq_data *d, unsigned type)
  443. {
  444. struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
  445. void __iomem *pio = at91_gpio->regbase;
  446. unsigned mask = 1 << d->hwirq;
  447. switch (type) {
  448. case IRQ_TYPE_EDGE_RISING:
  449. __raw_writel(mask, pio + PIO_ESR);
  450. __raw_writel(mask, pio + PIO_REHLSR);
  451. break;
  452. case IRQ_TYPE_EDGE_FALLING:
  453. __raw_writel(mask, pio + PIO_ESR);
  454. __raw_writel(mask, pio + PIO_FELLSR);
  455. break;
  456. case IRQ_TYPE_LEVEL_LOW:
  457. __raw_writel(mask, pio + PIO_LSR);
  458. __raw_writel(mask, pio + PIO_FELLSR);
  459. break;
  460. case IRQ_TYPE_LEVEL_HIGH:
  461. __raw_writel(mask, pio + PIO_LSR);
  462. __raw_writel(mask, pio + PIO_REHLSR);
  463. break;
  464. case IRQ_TYPE_EDGE_BOTH:
  465. /*
  466. * disable additional interrupt modes:
  467. * fall back to default behavior
  468. */
  469. __raw_writel(mask, pio + PIO_AIMDR);
  470. return 0;
  471. case IRQ_TYPE_NONE:
  472. default:
  473. pr_warn("AT91: No type for irq %d\n", gpio_to_irq(d->irq));
  474. return -EINVAL;
  475. }
  476. /* enable additional interrupt modes */
  477. __raw_writel(mask, pio + PIO_AIMER);
  478. return 0;
  479. }
  480. static struct irq_chip gpio_irqchip = {
  481. .name = "GPIO",
  482. .irq_disable = gpio_irq_mask,
  483. .irq_mask = gpio_irq_mask,
  484. .irq_unmask = gpio_irq_unmask,
  485. /* .irq_set_type is set dynamically */
  486. .irq_set_wake = gpio_irq_set_wake,
  487. };
  488. static void gpio_irq_handler(unsigned irq, struct irq_desc *desc)
  489. {
  490. struct irq_chip *chip = irq_desc_get_chip(desc);
  491. struct irq_data *idata = irq_desc_get_irq_data(desc);
  492. struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(idata);
  493. void __iomem *pio = at91_gpio->regbase;
  494. unsigned long isr;
  495. int n;
  496. chained_irq_enter(chip, desc);
  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. chained_irq_exit(chip, desc);
  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. }