gpio.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*
  2. * linux/arch/arm/mach-at91rm9200/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 "A" internal peripheral role.
  54. */
  55. int __init_or_module at91_set_A_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_ASR);
  64. __raw_writel(mask, pio + PIO_PDR);
  65. return 0;
  66. }
  67. EXPORT_SYMBOL(at91_set_A_periph);
  68. /*
  69. * mux the pin to the "B" internal peripheral role.
  70. */
  71. int __init_or_module at91_set_B_periph(unsigned pin, int use_pullup)
  72. {
  73. void __iomem *pio = pin_to_controller(pin);
  74. unsigned mask = pin_to_mask(pin);
  75. if (!pio)
  76. return -EINVAL;
  77. __raw_writel(mask, pio + PIO_IDR);
  78. __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
  79. __raw_writel(mask, pio + PIO_BSR);
  80. __raw_writel(mask, pio + PIO_PDR);
  81. return 0;
  82. }
  83. EXPORT_SYMBOL(at91_set_B_periph);
  84. /*
  85. * mux the pin to the gpio controller (instead of "A" or "B" peripheral), and
  86. * configure it for an input.
  87. */
  88. int __init_or_module at91_set_gpio_input(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_ODR);
  97. __raw_writel(mask, pio + PIO_PER);
  98. return 0;
  99. }
  100. EXPORT_SYMBOL(at91_set_gpio_input);
  101. /*
  102. * mux the pin to the gpio controller (instead of "A" or "B" peripheral),
  103. * and configure it for an output.
  104. */
  105. int __init_or_module at91_set_gpio_output(unsigned pin, int value)
  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 + PIO_PUDR);
  113. __raw_writel(mask, pio + (value ? PIO_SODR : PIO_CODR));
  114. __raw_writel(mask, pio + PIO_OER);
  115. __raw_writel(mask, pio + PIO_PER);
  116. return 0;
  117. }
  118. EXPORT_SYMBOL(at91_set_gpio_output);
  119. /*
  120. * enable/disable the glitch filter; mostly used with IRQ handling.
  121. */
  122. int __init_or_module at91_set_deglitch(unsigned pin, int is_on)
  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 + (is_on ? PIO_IFER : PIO_IFDR));
  129. return 0;
  130. }
  131. EXPORT_SYMBOL(at91_set_deglitch);
  132. /*
  133. * enable/disable the multi-driver; This is only valid for output and
  134. * allows the output pin to run as an open collector output.
  135. */
  136. int __init_or_module at91_set_multi_drive(unsigned pin, int is_on)
  137. {
  138. void __iomem *pio = pin_to_controller(pin);
  139. unsigned mask = pin_to_mask(pin);
  140. if (!pio)
  141. return -EINVAL;
  142. __raw_writel(mask, pio + (is_on ? PIO_MDER : PIO_MDDR));
  143. return 0;
  144. }
  145. EXPORT_SYMBOL(at91_set_multi_drive);
  146. /*--------------------------------------------------------------------------*/
  147. /*
  148. * assuming the pin is muxed as a gpio output, set its value.
  149. */
  150. int at91_set_gpio_value(unsigned pin, int value)
  151. {
  152. void __iomem *pio = pin_to_controller(pin);
  153. unsigned mask = pin_to_mask(pin);
  154. if (!pio)
  155. return -EINVAL;
  156. __raw_writel(mask, pio + (value ? PIO_SODR : PIO_CODR));
  157. return 0;
  158. }
  159. EXPORT_SYMBOL(at91_set_gpio_value);
  160. /*
  161. * read the pin's value (works even if it's not muxed as a gpio).
  162. */
  163. int at91_get_gpio_value(unsigned pin)
  164. {
  165. void __iomem *pio = pin_to_controller(pin);
  166. unsigned mask = pin_to_mask(pin);
  167. u32 pdsr;
  168. if (!pio)
  169. return -EINVAL;
  170. pdsr = __raw_readl(pio + PIO_PDSR);
  171. return (pdsr & mask) != 0;
  172. }
  173. EXPORT_SYMBOL(at91_get_gpio_value);
  174. /*--------------------------------------------------------------------------*/
  175. #ifdef CONFIG_PM
  176. static u32 wakeups[MAX_GPIO_BANKS];
  177. static u32 backups[MAX_GPIO_BANKS];
  178. static int gpio_irq_set_wake(unsigned pin, unsigned state)
  179. {
  180. unsigned mask = pin_to_mask(pin);
  181. unsigned bank = (pin - PIN_BASE) / 32;
  182. if (unlikely(bank >= MAX_GPIO_BANKS))
  183. return -EINVAL;
  184. if (state)
  185. wakeups[bank] |= mask;
  186. else
  187. wakeups[bank] &= ~mask;
  188. set_irq_wake(gpio[bank].id, state);
  189. return 0;
  190. }
  191. void at91_gpio_suspend(void)
  192. {
  193. int i;
  194. for (i = 0; i < gpio_banks; i++) {
  195. u32 pio = gpio[i].offset;
  196. backups[i] = at91_sys_read(pio + PIO_IMR);
  197. at91_sys_write(pio + PIO_IDR, backups[i]);
  198. at91_sys_write(pio + PIO_IER, wakeups[i]);
  199. if (!wakeups[i])
  200. clk_disable(gpio[i].clock);
  201. else {
  202. #ifdef CONFIG_PM_DEBUG
  203. printk(KERN_DEBUG "GPIO-%c may wake for %08x\n", 'A'+i, wakeups[i]);
  204. #endif
  205. }
  206. }
  207. }
  208. void at91_gpio_resume(void)
  209. {
  210. int i;
  211. for (i = 0; i < gpio_banks; i++) {
  212. u32 pio = gpio[i].offset;
  213. if (!wakeups[i])
  214. clk_enable(gpio[i].clock);
  215. at91_sys_write(pio + PIO_IDR, wakeups[i]);
  216. at91_sys_write(pio + PIO_IER, backups[i]);
  217. }
  218. }
  219. #else
  220. #define gpio_irq_set_wake NULL
  221. #endif
  222. /* Several AIC controller irqs are dispatched through this GPIO handler.
  223. * To use any AT91_PIN_* as an externally triggered IRQ, first call
  224. * at91_set_gpio_input() then maybe enable its glitch filter.
  225. * Then just request_irq() with the pin ID; it works like any ARM IRQ
  226. * handler, though it always triggers on rising and falling edges.
  227. *
  228. * Alternatively, certain pins may be used directly as IRQ0..IRQ6 after
  229. * configuring them with at91_set_a_periph() or at91_set_b_periph().
  230. * IRQ0..IRQ6 should be configurable, e.g. level vs edge triggering.
  231. */
  232. static void gpio_irq_mask(unsigned pin)
  233. {
  234. void __iomem *pio = pin_to_controller(pin);
  235. unsigned mask = pin_to_mask(pin);
  236. if (pio)
  237. __raw_writel(mask, pio + PIO_IDR);
  238. }
  239. static void gpio_irq_unmask(unsigned pin)
  240. {
  241. void __iomem *pio = pin_to_controller(pin);
  242. unsigned mask = pin_to_mask(pin);
  243. if (pio)
  244. __raw_writel(mask, pio + PIO_IER);
  245. }
  246. static int gpio_irq_type(unsigned pin, unsigned type)
  247. {
  248. return (type == IRQT_BOTHEDGE) ? 0 : -EINVAL;
  249. }
  250. static struct irq_chip gpio_irqchip = {
  251. .name = "GPIO",
  252. .mask = gpio_irq_mask,
  253. .unmask = gpio_irq_unmask,
  254. .set_type = gpio_irq_type,
  255. .set_wake = gpio_irq_set_wake,
  256. };
  257. static void gpio_irq_handler(unsigned irq, struct irq_desc *desc)
  258. {
  259. unsigned pin;
  260. struct irq_desc *gpio;
  261. void __iomem *pio;
  262. u32 isr;
  263. pio = get_irq_chip_data(irq);
  264. /* temporarily mask (level sensitive) parent IRQ */
  265. desc->chip->ack(irq);
  266. for (;;) {
  267. /* reading ISR acks the pending (edge triggered) GPIO interrupt */
  268. isr = __raw_readl(pio + PIO_ISR) & __raw_readl(pio + PIO_IMR);
  269. if (!isr)
  270. break;
  271. pin = (unsigned) get_irq_data(irq);
  272. gpio = &irq_desc[pin];
  273. while (isr) {
  274. if (isr & 1) {
  275. if (unlikely(gpio->depth)) {
  276. /*
  277. * The core ARM interrupt handler lazily disables IRQs so
  278. * another IRQ must be generated before it actually gets
  279. * here to be disabled on the GPIO controller.
  280. */
  281. gpio_irq_mask(pin);
  282. }
  283. else
  284. desc_handle_irq(pin, gpio);
  285. }
  286. pin++;
  287. gpio++;
  288. isr >>= 1;
  289. }
  290. }
  291. desc->chip->unmask(irq);
  292. /* now it may re-trigger */
  293. }
  294. /*--------------------------------------------------------------------------*/
  295. /*
  296. * Called from the processor-specific init to enable GPIO interrupt support.
  297. */
  298. void __init at91_gpio_irq_setup(void)
  299. {
  300. unsigned pioc, pin;
  301. for (pioc = 0, pin = PIN_BASE;
  302. pioc < gpio_banks;
  303. pioc++) {
  304. void __iomem *controller;
  305. unsigned id = gpio[pioc].id;
  306. unsigned i;
  307. clk_enable(gpio[pioc].clock); /* enable PIO controller's clock */
  308. controller = (void __iomem *) AT91_VA_BASE_SYS + gpio[pioc].offset;
  309. __raw_writel(~0, controller + PIO_IDR);
  310. set_irq_data(id, (void *) pin);
  311. set_irq_chip_data(id, controller);
  312. for (i = 0; i < 32; i++, pin++) {
  313. /*
  314. * Can use the "simple" and not "edge" handler since it's
  315. * shorter, and the AIC handles interupts sanely.
  316. */
  317. set_irq_chip(pin, &gpio_irqchip);
  318. set_irq_handler(pin, handle_simple_irq);
  319. set_irq_flags(pin, IRQF_VALID);
  320. }
  321. set_irq_chained_handler(id, gpio_irq_handler);
  322. }
  323. pr_info("AT91: %d gpio irqs in %d banks\n", pin - PIN_BASE, gpio_banks);
  324. }
  325. /*
  326. * Called from the processor-specific init to enable GPIO pin support.
  327. */
  328. void __init at91_gpio_init(struct at91_gpio_bank *data, int nr_banks)
  329. {
  330. BUG_ON(nr_banks > MAX_GPIO_BANKS);
  331. gpio = data;
  332. gpio_banks = nr_banks;
  333. }