gpio.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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/interrupt.h>
  14. #include <linux/irq.h>
  15. #include <linux/debugfs.h>
  16. #include <linux/seq_file.h>
  17. #include <linux/kernel.h>
  18. #include <linux/list.h>
  19. #include <linux/module.h>
  20. #include <asm/io.h>
  21. #include <asm/hardware.h>
  22. #include <asm/arch/at91_pio.h>
  23. #include <asm/arch/gpio.h>
  24. #include "generic.h"
  25. static struct at91_gpio_bank *gpio;
  26. static int gpio_banks;
  27. static inline void __iomem *pin_to_controller(unsigned pin)
  28. {
  29. void __iomem *sys_base = (void __iomem *) AT91_VA_BASE_SYS;
  30. pin -= PIN_BASE;
  31. pin /= 32;
  32. if (likely(pin < gpio_banks))
  33. return sys_base + gpio[pin].offset;
  34. return NULL;
  35. }
  36. static inline unsigned pin_to_mask(unsigned pin)
  37. {
  38. pin -= PIN_BASE;
  39. return 1 << (pin % 32);
  40. }
  41. /*--------------------------------------------------------------------------*/
  42. /* Not all hardware capabilities are exposed through these calls; they
  43. * only encapsulate the most common features and modes. (So if you
  44. * want to change signals in groups, do it directly.)
  45. *
  46. * Bootloaders will usually handle some of the pin multiplexing setup.
  47. * The intent is certainly that by the time Linux is fully booted, all
  48. * pins should have been fully initialized. These setup calls should
  49. * only be used by board setup routines, or possibly in driver probe().
  50. *
  51. * For bootloaders doing all that setup, these calls could be inlined
  52. * as NOPs so Linux won't duplicate any setup code
  53. */
  54. /*
  55. * mux the pin to the "GPIO" peripheral role.
  56. */
  57. int __init_or_module at91_set_GPIO_periph(unsigned pin, int use_pullup)
  58. {
  59. void __iomem *pio = pin_to_controller(pin);
  60. unsigned mask = pin_to_mask(pin);
  61. if (!pio)
  62. return -EINVAL;
  63. __raw_writel(mask, pio + PIO_IDR);
  64. __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
  65. __raw_writel(mask, pio + PIO_PER);
  66. return 0;
  67. }
  68. EXPORT_SYMBOL(at91_set_GPIO_periph);
  69. /*
  70. * mux the pin to the "A" internal peripheral role.
  71. */
  72. int __init_or_module at91_set_A_periph(unsigned pin, int use_pullup)
  73. {
  74. void __iomem *pio = pin_to_controller(pin);
  75. unsigned mask = pin_to_mask(pin);
  76. if (!pio)
  77. return -EINVAL;
  78. __raw_writel(mask, pio + PIO_IDR);
  79. __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
  80. __raw_writel(mask, pio + PIO_ASR);
  81. __raw_writel(mask, pio + PIO_PDR);
  82. return 0;
  83. }
  84. EXPORT_SYMBOL(at91_set_A_periph);
  85. /*
  86. * mux the pin to the "B" internal peripheral role.
  87. */
  88. int __init_or_module at91_set_B_periph(unsigned pin, int use_pullup)
  89. {
  90. void __iomem *pio = pin_to_controller(pin);
  91. unsigned mask = pin_to_mask(pin);
  92. if (!pio)
  93. return -EINVAL;
  94. __raw_writel(mask, pio + PIO_IDR);
  95. __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
  96. __raw_writel(mask, pio + PIO_BSR);
  97. __raw_writel(mask, pio + PIO_PDR);
  98. return 0;
  99. }
  100. EXPORT_SYMBOL(at91_set_B_periph);
  101. /*
  102. * mux the pin to the gpio controller (instead of "A" or "B" peripheral), and
  103. * configure it for an input.
  104. */
  105. int __init_or_module at91_set_gpio_input(unsigned pin, int use_pullup)
  106. {
  107. void __iomem *pio = pin_to_controller(pin);
  108. unsigned mask = pin_to_mask(pin);
  109. if (!pio)
  110. return -EINVAL;
  111. __raw_writel(mask, pio + PIO_IDR);
  112. __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
  113. __raw_writel(mask, pio + PIO_ODR);
  114. __raw_writel(mask, pio + PIO_PER);
  115. return 0;
  116. }
  117. EXPORT_SYMBOL(at91_set_gpio_input);
  118. /*
  119. * mux the pin to the gpio controller (instead of "A" or "B" peripheral),
  120. * and configure it for an output.
  121. */
  122. int __init_or_module at91_set_gpio_output(unsigned pin, int value)
  123. {
  124. void __iomem *pio = pin_to_controller(pin);
  125. unsigned mask = pin_to_mask(pin);
  126. if (!pio)
  127. return -EINVAL;
  128. __raw_writel(mask, pio + PIO_IDR);
  129. __raw_writel(mask, pio + PIO_PUDR);
  130. __raw_writel(mask, pio + (value ? PIO_SODR : PIO_CODR));
  131. __raw_writel(mask, pio + PIO_OER);
  132. __raw_writel(mask, pio + PIO_PER);
  133. return 0;
  134. }
  135. EXPORT_SYMBOL(at91_set_gpio_output);
  136. /*
  137. * enable/disable the glitch filter; mostly used with IRQ handling.
  138. */
  139. int __init_or_module at91_set_deglitch(unsigned pin, int is_on)
  140. {
  141. void __iomem *pio = pin_to_controller(pin);
  142. unsigned mask = pin_to_mask(pin);
  143. if (!pio)
  144. return -EINVAL;
  145. __raw_writel(mask, pio + (is_on ? PIO_IFER : PIO_IFDR));
  146. return 0;
  147. }
  148. EXPORT_SYMBOL(at91_set_deglitch);
  149. /*
  150. * enable/disable the multi-driver; This is only valid for output and
  151. * allows the output pin to run as an open collector output.
  152. */
  153. int __init_or_module at91_set_multi_drive(unsigned pin, int is_on)
  154. {
  155. void __iomem *pio = pin_to_controller(pin);
  156. unsigned mask = pin_to_mask(pin);
  157. if (!pio)
  158. return -EINVAL;
  159. __raw_writel(mask, pio + (is_on ? PIO_MDER : PIO_MDDR));
  160. return 0;
  161. }
  162. EXPORT_SYMBOL(at91_set_multi_drive);
  163. /*--------------------------------------------------------------------------*/
  164. /* new-style GPIO calls; these expect at91_set_GPIO_periph to have been
  165. * called, and maybe at91_set_multi_drive() for putout pins.
  166. */
  167. int gpio_direction_input(unsigned pin)
  168. {
  169. void __iomem *pio = pin_to_controller(pin);
  170. unsigned mask = pin_to_mask(pin);
  171. if (!pio || !(__raw_readl(pio + PIO_PSR) & mask))
  172. return -EINVAL;
  173. __raw_writel(mask, pio + PIO_ODR);
  174. return 0;
  175. }
  176. EXPORT_SYMBOL(gpio_direction_input);
  177. int gpio_direction_output(unsigned pin, int value)
  178. {
  179. void __iomem *pio = pin_to_controller(pin);
  180. unsigned mask = pin_to_mask(pin);
  181. if (!pio || !(__raw_readl(pio + PIO_PSR) & mask))
  182. return -EINVAL;
  183. __raw_writel(mask, pio + (value ? PIO_SODR : PIO_CODR));
  184. __raw_writel(mask, pio + PIO_OER);
  185. return 0;
  186. }
  187. EXPORT_SYMBOL(gpio_direction_output);
  188. /*--------------------------------------------------------------------------*/
  189. /*
  190. * assuming the pin is muxed as a gpio output, set its value.
  191. */
  192. int at91_set_gpio_value(unsigned pin, int value)
  193. {
  194. void __iomem *pio = pin_to_controller(pin);
  195. unsigned mask = pin_to_mask(pin);
  196. if (!pio)
  197. return -EINVAL;
  198. __raw_writel(mask, pio + (value ? PIO_SODR : PIO_CODR));
  199. return 0;
  200. }
  201. EXPORT_SYMBOL(at91_set_gpio_value);
  202. /*
  203. * read the pin's value (works even if it's not muxed as a gpio).
  204. */
  205. int at91_get_gpio_value(unsigned pin)
  206. {
  207. void __iomem *pio = pin_to_controller(pin);
  208. unsigned mask = pin_to_mask(pin);
  209. u32 pdsr;
  210. if (!pio)
  211. return -EINVAL;
  212. pdsr = __raw_readl(pio + PIO_PDSR);
  213. return (pdsr & mask) != 0;
  214. }
  215. EXPORT_SYMBOL(at91_get_gpio_value);
  216. /*--------------------------------------------------------------------------*/
  217. #ifdef CONFIG_PM
  218. static u32 wakeups[MAX_GPIO_BANKS];
  219. static u32 backups[MAX_GPIO_BANKS];
  220. static int gpio_irq_set_wake(unsigned pin, unsigned state)
  221. {
  222. unsigned mask = pin_to_mask(pin);
  223. unsigned bank = (pin - PIN_BASE) / 32;
  224. if (unlikely(bank >= MAX_GPIO_BANKS))
  225. return -EINVAL;
  226. if (state)
  227. wakeups[bank] |= mask;
  228. else
  229. wakeups[bank] &= ~mask;
  230. set_irq_wake(gpio[bank].id, state);
  231. return 0;
  232. }
  233. void at91_gpio_suspend(void)
  234. {
  235. int i;
  236. for (i = 0; i < gpio_banks; i++) {
  237. u32 pio = gpio[i].offset;
  238. backups[i] = at91_sys_read(pio + PIO_IMR);
  239. at91_sys_write(pio + PIO_IDR, backups[i]);
  240. at91_sys_write(pio + PIO_IER, wakeups[i]);
  241. if (!wakeups[i])
  242. clk_disable(gpio[i].clock);
  243. else {
  244. #ifdef CONFIG_PM_DEBUG
  245. printk(KERN_DEBUG "GPIO-%c may wake for %08x\n", 'A'+i, wakeups[i]);
  246. #endif
  247. }
  248. }
  249. }
  250. void at91_gpio_resume(void)
  251. {
  252. int i;
  253. for (i = 0; i < gpio_banks; i++) {
  254. u32 pio = gpio[i].offset;
  255. if (!wakeups[i])
  256. clk_enable(gpio[i].clock);
  257. at91_sys_write(pio + PIO_IDR, wakeups[i]);
  258. at91_sys_write(pio + PIO_IER, backups[i]);
  259. }
  260. }
  261. #else
  262. #define gpio_irq_set_wake NULL
  263. #endif
  264. /* Several AIC controller irqs are dispatched through this GPIO handler.
  265. * To use any AT91_PIN_* as an externally triggered IRQ, first call
  266. * at91_set_gpio_input() then maybe enable its glitch filter.
  267. * Then just request_irq() with the pin ID; it works like any ARM IRQ
  268. * handler, though it always triggers on rising and falling edges.
  269. *
  270. * Alternatively, certain pins may be used directly as IRQ0..IRQ6 after
  271. * configuring them with at91_set_a_periph() or at91_set_b_periph().
  272. * IRQ0..IRQ6 should be configurable, e.g. level vs edge triggering.
  273. */
  274. static void gpio_irq_mask(unsigned pin)
  275. {
  276. void __iomem *pio = pin_to_controller(pin);
  277. unsigned mask = pin_to_mask(pin);
  278. if (pio)
  279. __raw_writel(mask, pio + PIO_IDR);
  280. }
  281. static void gpio_irq_unmask(unsigned pin)
  282. {
  283. void __iomem *pio = pin_to_controller(pin);
  284. unsigned mask = pin_to_mask(pin);
  285. if (pio)
  286. __raw_writel(mask, pio + PIO_IER);
  287. }
  288. static int gpio_irq_type(unsigned pin, unsigned type)
  289. {
  290. return (type == IRQT_BOTHEDGE) ? 0 : -EINVAL;
  291. }
  292. static struct irq_chip gpio_irqchip = {
  293. .name = "GPIO",
  294. .mask = gpio_irq_mask,
  295. .unmask = gpio_irq_unmask,
  296. .set_type = gpio_irq_type,
  297. .set_wake = gpio_irq_set_wake,
  298. };
  299. static void gpio_irq_handler(unsigned irq, struct irq_desc *desc)
  300. {
  301. unsigned pin;
  302. struct irq_desc *gpio;
  303. void __iomem *pio;
  304. u32 isr;
  305. pio = get_irq_chip_data(irq);
  306. /* temporarily mask (level sensitive) parent IRQ */
  307. desc->chip->ack(irq);
  308. for (;;) {
  309. /* reading ISR acks the pending (edge triggered) GPIO interrupt */
  310. isr = __raw_readl(pio + PIO_ISR) & __raw_readl(pio + PIO_IMR);
  311. if (!isr)
  312. break;
  313. pin = (unsigned) get_irq_data(irq);
  314. gpio = &irq_desc[pin];
  315. while (isr) {
  316. if (isr & 1) {
  317. if (unlikely(gpio->depth)) {
  318. /*
  319. * The core ARM interrupt handler lazily disables IRQs so
  320. * another IRQ must be generated before it actually gets
  321. * here to be disabled on the GPIO controller.
  322. */
  323. gpio_irq_mask(pin);
  324. }
  325. else
  326. desc_handle_irq(pin, gpio);
  327. }
  328. pin++;
  329. gpio++;
  330. isr >>= 1;
  331. }
  332. }
  333. desc->chip->unmask(irq);
  334. /* now it may re-trigger */
  335. }
  336. /*--------------------------------------------------------------------------*/
  337. #ifdef CONFIG_DEBUG_FS
  338. static int at91_gpio_show(struct seq_file *s, void *unused)
  339. {
  340. int bank, j;
  341. /* print heading */
  342. seq_printf(s, "Pin\t");
  343. for (bank = 0; bank < gpio_banks; bank++) {
  344. seq_printf(s, "PIO%c\t", 'A' + bank);
  345. };
  346. seq_printf(s, "\n\n");
  347. /* print pin status */
  348. for (j = 0; j < 32; j++) {
  349. seq_printf(s, "%i:\t", j);
  350. for (bank = 0; bank < gpio_banks; bank++) {
  351. unsigned pin = PIN_BASE + (32 * bank) + j;
  352. void __iomem *pio = pin_to_controller(pin);
  353. unsigned mask = pin_to_mask(pin);
  354. if (__raw_readl(pio + PIO_PSR) & mask)
  355. seq_printf(s, "GPIO:%s", __raw_readl(pio + PIO_PDSR) & mask ? "1" : "0");
  356. else
  357. seq_printf(s, "%s", __raw_readl(pio + PIO_ABSR) & mask ? "B" : "A");
  358. seq_printf(s, "\t");
  359. }
  360. seq_printf(s, "\n");
  361. }
  362. return 0;
  363. }
  364. static int at91_gpio_open(struct inode *inode, struct file *file)
  365. {
  366. return single_open(file, at91_gpio_show, NULL);
  367. }
  368. static const struct file_operations at91_gpio_operations = {
  369. .open = at91_gpio_open,
  370. .read = seq_read,
  371. .llseek = seq_lseek,
  372. .release = single_release,
  373. };
  374. static int __init at91_gpio_debugfs_init(void)
  375. {
  376. /* /sys/kernel/debug/at91_gpio */
  377. (void) debugfs_create_file("at91_gpio", S_IFREG | S_IRUGO, NULL, NULL, &at91_gpio_operations);
  378. return 0;
  379. }
  380. postcore_initcall(at91_gpio_debugfs_init);
  381. #endif
  382. /*--------------------------------------------------------------------------*/
  383. /*
  384. * Called from the processor-specific init to enable GPIO interrupt support.
  385. */
  386. void __init at91_gpio_irq_setup(void)
  387. {
  388. unsigned pioc, pin;
  389. for (pioc = 0, pin = PIN_BASE;
  390. pioc < gpio_banks;
  391. pioc++) {
  392. void __iomem *controller;
  393. unsigned id = gpio[pioc].id;
  394. unsigned i;
  395. clk_enable(gpio[pioc].clock); /* enable PIO controller's clock */
  396. controller = (void __iomem *) AT91_VA_BASE_SYS + gpio[pioc].offset;
  397. __raw_writel(~0, controller + PIO_IDR);
  398. set_irq_data(id, (void *) pin);
  399. set_irq_chip_data(id, controller);
  400. for (i = 0; i < 32; i++, pin++) {
  401. /*
  402. * Can use the "simple" and not "edge" handler since it's
  403. * shorter, and the AIC handles interrupts sanely.
  404. */
  405. set_irq_chip(pin, &gpio_irqchip);
  406. set_irq_handler(pin, handle_simple_irq);
  407. set_irq_flags(pin, IRQF_VALID);
  408. }
  409. set_irq_chained_handler(id, gpio_irq_handler);
  410. }
  411. pr_info("AT91: %d gpio irqs in %d banks\n", pin - PIN_BASE, gpio_banks);
  412. }
  413. /*
  414. * Called from the processor-specific init to enable GPIO pin support.
  415. */
  416. void __init at91_gpio_init(struct at91_gpio_bank *data, int nr_banks)
  417. {
  418. BUG_ON(nr_banks > MAX_GPIO_BANKS);
  419. gpio = data;
  420. gpio_banks = nr_banks;
  421. }