gpio.c 16 KB

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