gpio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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 "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_OER);
  172. return 0;
  173. }
  174. EXPORT_SYMBOL(gpio_direction_input);
  175. int gpio_direction_output(unsigned pin)
  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 + PIO_OER);
  182. return 0;
  183. }
  184. EXPORT_SYMBOL(gpio_direction_output);
  185. /*--------------------------------------------------------------------------*/
  186. /*
  187. * assuming the pin is muxed as a gpio output, set its value.
  188. */
  189. int at91_set_gpio_value(unsigned pin, int value)
  190. {
  191. void __iomem *pio = pin_to_controller(pin);
  192. unsigned mask = pin_to_mask(pin);
  193. if (!pio)
  194. return -EINVAL;
  195. __raw_writel(mask, pio + (value ? PIO_SODR : PIO_CODR));
  196. return 0;
  197. }
  198. EXPORT_SYMBOL(at91_set_gpio_value);
  199. /*
  200. * read the pin's value (works even if it's not muxed as a gpio).
  201. */
  202. int at91_get_gpio_value(unsigned pin)
  203. {
  204. void __iomem *pio = pin_to_controller(pin);
  205. unsigned mask = pin_to_mask(pin);
  206. u32 pdsr;
  207. if (!pio)
  208. return -EINVAL;
  209. pdsr = __raw_readl(pio + PIO_PDSR);
  210. return (pdsr & mask) != 0;
  211. }
  212. EXPORT_SYMBOL(at91_get_gpio_value);
  213. /*--------------------------------------------------------------------------*/
  214. #ifdef CONFIG_PM
  215. static u32 wakeups[MAX_GPIO_BANKS];
  216. static u32 backups[MAX_GPIO_BANKS];
  217. static int gpio_irq_set_wake(unsigned pin, unsigned state)
  218. {
  219. unsigned mask = pin_to_mask(pin);
  220. unsigned bank = (pin - PIN_BASE) / 32;
  221. if (unlikely(bank >= MAX_GPIO_BANKS))
  222. return -EINVAL;
  223. if (state)
  224. wakeups[bank] |= mask;
  225. else
  226. wakeups[bank] &= ~mask;
  227. set_irq_wake(gpio[bank].id, state);
  228. return 0;
  229. }
  230. void at91_gpio_suspend(void)
  231. {
  232. int i;
  233. for (i = 0; i < gpio_banks; i++) {
  234. u32 pio = gpio[i].offset;
  235. backups[i] = at91_sys_read(pio + PIO_IMR);
  236. at91_sys_write(pio + PIO_IDR, backups[i]);
  237. at91_sys_write(pio + PIO_IER, wakeups[i]);
  238. if (!wakeups[i])
  239. clk_disable(gpio[i].clock);
  240. else {
  241. #ifdef CONFIG_PM_DEBUG
  242. printk(KERN_DEBUG "GPIO-%c may wake for %08x\n", 'A'+i, wakeups[i]);
  243. #endif
  244. }
  245. }
  246. }
  247. void at91_gpio_resume(void)
  248. {
  249. int i;
  250. for (i = 0; i < gpio_banks; i++) {
  251. u32 pio = gpio[i].offset;
  252. if (!wakeups[i])
  253. clk_enable(gpio[i].clock);
  254. at91_sys_write(pio + PIO_IDR, wakeups[i]);
  255. at91_sys_write(pio + PIO_IER, backups[i]);
  256. }
  257. }
  258. #else
  259. #define gpio_irq_set_wake NULL
  260. #endif
  261. /* Several AIC controller irqs are dispatched through this GPIO handler.
  262. * To use any AT91_PIN_* as an externally triggered IRQ, first call
  263. * at91_set_gpio_input() then maybe enable its glitch filter.
  264. * Then just request_irq() with the pin ID; it works like any ARM IRQ
  265. * handler, though it always triggers on rising and falling edges.
  266. *
  267. * Alternatively, certain pins may be used directly as IRQ0..IRQ6 after
  268. * configuring them with at91_set_a_periph() or at91_set_b_periph().
  269. * IRQ0..IRQ6 should be configurable, e.g. level vs edge triggering.
  270. */
  271. static void gpio_irq_mask(unsigned pin)
  272. {
  273. void __iomem *pio = pin_to_controller(pin);
  274. unsigned mask = pin_to_mask(pin);
  275. if (pio)
  276. __raw_writel(mask, pio + PIO_IDR);
  277. }
  278. static void gpio_irq_unmask(unsigned pin)
  279. {
  280. void __iomem *pio = pin_to_controller(pin);
  281. unsigned mask = pin_to_mask(pin);
  282. if (pio)
  283. __raw_writel(mask, pio + PIO_IER);
  284. }
  285. static int gpio_irq_type(unsigned pin, unsigned type)
  286. {
  287. return (type == IRQT_BOTHEDGE) ? 0 : -EINVAL;
  288. }
  289. static struct irq_chip gpio_irqchip = {
  290. .name = "GPIO",
  291. .mask = gpio_irq_mask,
  292. .unmask = gpio_irq_unmask,
  293. .set_type = gpio_irq_type,
  294. .set_wake = gpio_irq_set_wake,
  295. };
  296. static void gpio_irq_handler(unsigned irq, struct irq_desc *desc)
  297. {
  298. unsigned pin;
  299. struct irq_desc *gpio;
  300. void __iomem *pio;
  301. u32 isr;
  302. pio = get_irq_chip_data(irq);
  303. /* temporarily mask (level sensitive) parent IRQ */
  304. desc->chip->ack(irq);
  305. for (;;) {
  306. /* reading ISR acks the pending (edge triggered) GPIO interrupt */
  307. isr = __raw_readl(pio + PIO_ISR) & __raw_readl(pio + PIO_IMR);
  308. if (!isr)
  309. break;
  310. pin = (unsigned) get_irq_data(irq);
  311. gpio = &irq_desc[pin];
  312. while (isr) {
  313. if (isr & 1) {
  314. if (unlikely(gpio->depth)) {
  315. /*
  316. * The core ARM interrupt handler lazily disables IRQs so
  317. * another IRQ must be generated before it actually gets
  318. * here to be disabled on the GPIO controller.
  319. */
  320. gpio_irq_mask(pin);
  321. }
  322. else
  323. desc_handle_irq(pin, gpio);
  324. }
  325. pin++;
  326. gpio++;
  327. isr >>= 1;
  328. }
  329. }
  330. desc->chip->unmask(irq);
  331. /* now it may re-trigger */
  332. }
  333. /*--------------------------------------------------------------------------*/
  334. /*
  335. * Called from the processor-specific init to enable GPIO interrupt support.
  336. */
  337. void __init at91_gpio_irq_setup(void)
  338. {
  339. unsigned pioc, pin;
  340. for (pioc = 0, pin = PIN_BASE;
  341. pioc < gpio_banks;
  342. pioc++) {
  343. void __iomem *controller;
  344. unsigned id = gpio[pioc].id;
  345. unsigned i;
  346. clk_enable(gpio[pioc].clock); /* enable PIO controller's clock */
  347. controller = (void __iomem *) AT91_VA_BASE_SYS + gpio[pioc].offset;
  348. __raw_writel(~0, controller + PIO_IDR);
  349. set_irq_data(id, (void *) pin);
  350. set_irq_chip_data(id, controller);
  351. for (i = 0; i < 32; i++, pin++) {
  352. /*
  353. * Can use the "simple" and not "edge" handler since it's
  354. * shorter, and the AIC handles interupts sanely.
  355. */
  356. set_irq_chip(pin, &gpio_irqchip);
  357. set_irq_handler(pin, handle_simple_irq);
  358. set_irq_flags(pin, IRQF_VALID);
  359. }
  360. set_irq_chained_handler(id, gpio_irq_handler);
  361. }
  362. pr_info("AT91: %d gpio irqs in %d banks\n", pin - PIN_BASE, gpio_banks);
  363. }
  364. /*
  365. * Called from the processor-specific init to enable GPIO pin support.
  366. */
  367. void __init at91_gpio_init(struct at91_gpio_bank *data, int nr_banks)
  368. {
  369. BUG_ON(nr_banks > MAX_GPIO_BANKS);
  370. gpio = data;
  371. gpio_banks = nr_banks;
  372. }