gpio.c 13 KB

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