gpio.c 11 KB

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