gpio.c 16 KB

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