gpio.c 16 KB

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