gpio.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  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/device.h>
  14. #include <linux/gpio.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/irq.h>
  17. #include <linux/debugfs.h>
  18. #include <linux/seq_file.h>
  19. #include <linux/kernel.h>
  20. #include <linux/list.h>
  21. #include <linux/module.h>
  22. #include <linux/io.h>
  23. #include <linux/irqdomain.h>
  24. #include <linux/of_address.h>
  25. #include <linux/of_irq.h>
  26. #include <mach/hardware.h>
  27. #include <mach/at91_pio.h>
  28. #include "generic.h"
  29. struct at91_gpio_chip {
  30. struct gpio_chip chip;
  31. struct at91_gpio_chip *next; /* Bank sharing same clock */
  32. int pioc_hwirq; /* PIO bank interrupt identifier on AIC */
  33. int pioc_idx; /* PIO bank index */
  34. void __iomem *regbase; /* PIO bank virtual address */
  35. struct clk *clock; /* associated clock */
  36. struct irq_domain *domain; /* associated irq domain */
  37. };
  38. #define to_at91_gpio_chip(c) container_of(c, struct at91_gpio_chip, chip)
  39. static void at91_gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip);
  40. static void at91_gpiolib_set(struct gpio_chip *chip, unsigned offset, int val);
  41. static int at91_gpiolib_get(struct gpio_chip *chip, unsigned offset);
  42. static int at91_gpiolib_direction_output(struct gpio_chip *chip,
  43. unsigned offset, int val);
  44. static int at91_gpiolib_direction_input(struct gpio_chip *chip,
  45. unsigned offset);
  46. static int at91_gpiolib_to_irq(struct gpio_chip *chip, unsigned offset);
  47. #define AT91_GPIO_CHIP(name, base_gpio, nr_gpio) \
  48. { \
  49. .chip = { \
  50. .label = name, \
  51. .direction_input = at91_gpiolib_direction_input, \
  52. .direction_output = at91_gpiolib_direction_output, \
  53. .get = at91_gpiolib_get, \
  54. .set = at91_gpiolib_set, \
  55. .dbg_show = at91_gpiolib_dbg_show, \
  56. .base = base_gpio, \
  57. .to_irq = at91_gpiolib_to_irq, \
  58. .ngpio = nr_gpio, \
  59. }, \
  60. }
  61. static struct at91_gpio_chip gpio_chip[] = {
  62. AT91_GPIO_CHIP("pioA", 0x00, 32),
  63. AT91_GPIO_CHIP("pioB", 0x20, 32),
  64. AT91_GPIO_CHIP("pioC", 0x40, 32),
  65. AT91_GPIO_CHIP("pioD", 0x60, 32),
  66. AT91_GPIO_CHIP("pioE", 0x80, 32),
  67. };
  68. static int gpio_banks;
  69. static inline void __iomem *pin_to_controller(unsigned pin)
  70. {
  71. pin /= 32;
  72. if (likely(pin < gpio_banks))
  73. return gpio_chip[pin].regbase;
  74. return NULL;
  75. }
  76. static inline unsigned pin_to_mask(unsigned pin)
  77. {
  78. return 1 << (pin % 32);
  79. }
  80. /*--------------------------------------------------------------------------*/
  81. /* Not all hardware capabilities are exposed through these calls; they
  82. * only encapsulate the most common features and modes. (So if you
  83. * want to change signals in groups, do it directly.)
  84. *
  85. * Bootloaders will usually handle some of the pin multiplexing setup.
  86. * The intent is certainly that by the time Linux is fully booted, all
  87. * pins should have been fully initialized. These setup calls should
  88. * only be used by board setup routines, or possibly in driver probe().
  89. *
  90. * For bootloaders doing all that setup, these calls could be inlined
  91. * as NOPs so Linux won't duplicate any setup code
  92. */
  93. /*
  94. * mux the pin to the "GPIO" peripheral role.
  95. */
  96. int __init_or_module at91_set_GPIO_periph(unsigned pin, int use_pullup)
  97. {
  98. void __iomem *pio = pin_to_controller(pin);
  99. unsigned mask = pin_to_mask(pin);
  100. if (!pio)
  101. return -EINVAL;
  102. __raw_writel(mask, pio + PIO_IDR);
  103. __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
  104. __raw_writel(mask, pio + PIO_PER);
  105. return 0;
  106. }
  107. EXPORT_SYMBOL(at91_set_GPIO_periph);
  108. /*
  109. * mux the pin to the "A" internal peripheral role.
  110. */
  111. int __init_or_module at91_set_A_periph(unsigned pin, int use_pullup)
  112. {
  113. void __iomem *pio = pin_to_controller(pin);
  114. unsigned mask = pin_to_mask(pin);
  115. if (!pio)
  116. return -EINVAL;
  117. __raw_writel(mask, pio + PIO_IDR);
  118. __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
  119. __raw_writel(mask, pio + PIO_ASR);
  120. __raw_writel(mask, pio + PIO_PDR);
  121. return 0;
  122. }
  123. EXPORT_SYMBOL(at91_set_A_periph);
  124. /*
  125. * mux the pin to the "B" internal peripheral role.
  126. */
  127. int __init_or_module at91_set_B_periph(unsigned pin, int use_pullup)
  128. {
  129. void __iomem *pio = pin_to_controller(pin);
  130. unsigned mask = pin_to_mask(pin);
  131. if (!pio)
  132. return -EINVAL;
  133. __raw_writel(mask, pio + PIO_IDR);
  134. __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
  135. __raw_writel(mask, pio + PIO_BSR);
  136. __raw_writel(mask, pio + PIO_PDR);
  137. return 0;
  138. }
  139. EXPORT_SYMBOL(at91_set_B_periph);
  140. /*
  141. * mux the pin to the gpio controller (instead of "A" or "B" peripheral), and
  142. * configure it for an input.
  143. */
  144. int __init_or_module at91_set_gpio_input(unsigned pin, int use_pullup)
  145. {
  146. void __iomem *pio = pin_to_controller(pin);
  147. unsigned mask = pin_to_mask(pin);
  148. if (!pio)
  149. return -EINVAL;
  150. __raw_writel(mask, pio + PIO_IDR);
  151. __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
  152. __raw_writel(mask, pio + PIO_ODR);
  153. __raw_writel(mask, pio + PIO_PER);
  154. return 0;
  155. }
  156. EXPORT_SYMBOL(at91_set_gpio_input);
  157. /*
  158. * mux the pin to the gpio controller (instead of "A" or "B" peripheral),
  159. * and configure it for an output.
  160. */
  161. int __init_or_module at91_set_gpio_output(unsigned pin, int value)
  162. {
  163. void __iomem *pio = pin_to_controller(pin);
  164. unsigned mask = pin_to_mask(pin);
  165. if (!pio)
  166. return -EINVAL;
  167. __raw_writel(mask, pio + PIO_IDR);
  168. __raw_writel(mask, pio + PIO_PUDR);
  169. __raw_writel(mask, pio + (value ? PIO_SODR : PIO_CODR));
  170. __raw_writel(mask, pio + PIO_OER);
  171. __raw_writel(mask, pio + PIO_PER);
  172. return 0;
  173. }
  174. EXPORT_SYMBOL(at91_set_gpio_output);
  175. /*
  176. * enable/disable the glitch filter; mostly used with IRQ handling.
  177. */
  178. int __init_or_module at91_set_deglitch(unsigned pin, int is_on)
  179. {
  180. void __iomem *pio = pin_to_controller(pin);
  181. unsigned mask = pin_to_mask(pin);
  182. if (!pio)
  183. return -EINVAL;
  184. __raw_writel(mask, pio + (is_on ? PIO_IFER : PIO_IFDR));
  185. return 0;
  186. }
  187. EXPORT_SYMBOL(at91_set_deglitch);
  188. /*
  189. * enable/disable the multi-driver; This is only valid for output and
  190. * allows the output pin to run as an open collector output.
  191. */
  192. int __init_or_module at91_set_multi_drive(unsigned pin, int is_on)
  193. {
  194. void __iomem *pio = pin_to_controller(pin);
  195. unsigned mask = pin_to_mask(pin);
  196. if (!pio)
  197. return -EINVAL;
  198. __raw_writel(mask, pio + (is_on ? PIO_MDER : PIO_MDDR));
  199. return 0;
  200. }
  201. EXPORT_SYMBOL(at91_set_multi_drive);
  202. /*
  203. * assuming the pin is muxed as a gpio output, set its value.
  204. */
  205. int at91_set_gpio_value(unsigned pin, int value)
  206. {
  207. void __iomem *pio = pin_to_controller(pin);
  208. unsigned mask = pin_to_mask(pin);
  209. if (!pio)
  210. return -EINVAL;
  211. __raw_writel(mask, pio + (value ? PIO_SODR : PIO_CODR));
  212. return 0;
  213. }
  214. EXPORT_SYMBOL(at91_set_gpio_value);
  215. /*
  216. * read the pin's value (works even if it's not muxed as a gpio).
  217. */
  218. int at91_get_gpio_value(unsigned pin)
  219. {
  220. void __iomem *pio = pin_to_controller(pin);
  221. unsigned mask = pin_to_mask(pin);
  222. u32 pdsr;
  223. if (!pio)
  224. return -EINVAL;
  225. pdsr = __raw_readl(pio + PIO_PDSR);
  226. return (pdsr & mask) != 0;
  227. }
  228. EXPORT_SYMBOL(at91_get_gpio_value);
  229. /*--------------------------------------------------------------------------*/
  230. #ifdef CONFIG_PM
  231. static u32 wakeups[MAX_GPIO_BANKS];
  232. static u32 backups[MAX_GPIO_BANKS];
  233. static int gpio_irq_set_wake(struct irq_data *d, unsigned state)
  234. {
  235. struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
  236. unsigned mask = 1 << d->hwirq;
  237. unsigned bank = at91_gpio->pioc_idx;
  238. if (unlikely(bank >= MAX_GPIO_BANKS))
  239. return -EINVAL;
  240. if (state)
  241. wakeups[bank] |= mask;
  242. else
  243. wakeups[bank] &= ~mask;
  244. irq_set_irq_wake(gpio_chip[bank].pioc_hwirq, state);
  245. return 0;
  246. }
  247. void at91_gpio_suspend(void)
  248. {
  249. int i;
  250. for (i = 0; i < gpio_banks; i++) {
  251. void __iomem *pio = gpio_chip[i].regbase;
  252. backups[i] = __raw_readl(pio + PIO_IMR);
  253. __raw_writel(backups[i], pio + PIO_IDR);
  254. __raw_writel(wakeups[i], pio + PIO_IER);
  255. if (!wakeups[i]) {
  256. clk_unprepare(gpio_chip[i].clock);
  257. clk_disable(gpio_chip[i].clock);
  258. } else {
  259. #ifdef CONFIG_PM_DEBUG
  260. printk(KERN_DEBUG "GPIO-%c may wake for %08x\n", 'A'+i, wakeups[i]);
  261. #endif
  262. }
  263. }
  264. }
  265. void at91_gpio_resume(void)
  266. {
  267. int i;
  268. for (i = 0; i < gpio_banks; i++) {
  269. void __iomem *pio = gpio_chip[i].regbase;
  270. if (!wakeups[i]) {
  271. if (clk_prepare(gpio_chip[i].clock) == 0)
  272. clk_enable(gpio_chip[i].clock);
  273. }
  274. __raw_writel(wakeups[i], pio + PIO_IDR);
  275. __raw_writel(backups[i], pio + PIO_IER);
  276. }
  277. }
  278. #else
  279. #define gpio_irq_set_wake NULL
  280. #endif
  281. /* Several AIC controller irqs are dispatched through this GPIO handler.
  282. * To use any AT91_PIN_* as an externally triggered IRQ, first call
  283. * at91_set_gpio_input() then maybe enable its glitch filter.
  284. * Then just request_irq() with the pin ID; it works like any ARM IRQ
  285. * handler, though it always triggers on rising and falling edges.
  286. *
  287. * Alternatively, certain pins may be used directly as IRQ0..IRQ6 after
  288. * configuring them with at91_set_a_periph() or at91_set_b_periph().
  289. * IRQ0..IRQ6 should be configurable, e.g. level vs edge triggering.
  290. */
  291. static void gpio_irq_mask(struct irq_data *d)
  292. {
  293. struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
  294. void __iomem *pio = at91_gpio->regbase;
  295. unsigned mask = 1 << d->hwirq;
  296. if (pio)
  297. __raw_writel(mask, pio + PIO_IDR);
  298. }
  299. static void gpio_irq_unmask(struct irq_data *d)
  300. {
  301. struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
  302. void __iomem *pio = at91_gpio->regbase;
  303. unsigned mask = 1 << d->hwirq;
  304. if (pio)
  305. __raw_writel(mask, pio + PIO_IER);
  306. }
  307. static int gpio_irq_type(struct irq_data *d, unsigned type)
  308. {
  309. switch (type) {
  310. case IRQ_TYPE_NONE:
  311. case IRQ_TYPE_EDGE_BOTH:
  312. return 0;
  313. default:
  314. return -EINVAL;
  315. }
  316. }
  317. static struct irq_chip gpio_irqchip = {
  318. .name = "GPIO",
  319. .irq_disable = gpio_irq_mask,
  320. .irq_mask = gpio_irq_mask,
  321. .irq_unmask = gpio_irq_unmask,
  322. .irq_set_type = gpio_irq_type,
  323. .irq_set_wake = gpio_irq_set_wake,
  324. };
  325. static void gpio_irq_handler(unsigned irq, struct irq_desc *desc)
  326. {
  327. unsigned virq;
  328. struct irq_data *idata = irq_desc_get_irq_data(desc);
  329. struct irq_chip *chip = irq_data_get_irq_chip(idata);
  330. struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(idata);
  331. void __iomem *pio = at91_gpio->regbase;
  332. u32 isr;
  333. /* temporarily mask (level sensitive) parent IRQ */
  334. chip->irq_ack(idata);
  335. for (;;) {
  336. /* Reading ISR acks pending (edge triggered) GPIO interrupts.
  337. * When there none are pending, we're finished unless we need
  338. * to process multiple banks (like ID_PIOCDE on sam9263).
  339. */
  340. isr = __raw_readl(pio + PIO_ISR) & __raw_readl(pio + PIO_IMR);
  341. if (!isr) {
  342. if (!at91_gpio->next)
  343. break;
  344. at91_gpio = at91_gpio->next;
  345. pio = at91_gpio->regbase;
  346. continue;
  347. }
  348. virq = gpio_to_irq(at91_gpio->chip.base);
  349. while (isr) {
  350. if (isr & 1)
  351. generic_handle_irq(virq);
  352. virq++;
  353. isr >>= 1;
  354. }
  355. }
  356. chip->irq_unmask(idata);
  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 = (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. * irqdomain initialization: pile up irqdomains on top of AIC range
  408. */
  409. static void __init at91_gpio_irqdomain(struct at91_gpio_chip *at91_gpio)
  410. {
  411. int irq_base;
  412. #if defined(CONFIG_OF)
  413. struct device_node *of_node = at91_gpio->chip.of_node;
  414. #else
  415. struct device_node *of_node = NULL;
  416. #endif
  417. irq_base = irq_alloc_descs(-1, 0, at91_gpio->chip.ngpio, 0);
  418. if (irq_base < 0)
  419. panic("at91_gpio.%d: error %d: couldn't allocate IRQ numbers.\n",
  420. at91_gpio->pioc_idx, irq_base);
  421. at91_gpio->domain = irq_domain_add_legacy(of_node,
  422. at91_gpio->chip.ngpio,
  423. irq_base, 0,
  424. &irq_domain_simple_ops, NULL);
  425. if (!at91_gpio->domain)
  426. panic("at91_gpio.%d: couldn't allocate irq domain.\n",
  427. at91_gpio->pioc_idx);
  428. }
  429. /*
  430. * This lock class tells lockdep that GPIO irqs are in a different
  431. * category than their parents, so it won't report false recursion.
  432. */
  433. static struct lock_class_key gpio_lock_class;
  434. /*
  435. * Called from the processor-specific init to enable GPIO interrupt support.
  436. */
  437. void __init at91_gpio_irq_setup(void)
  438. {
  439. unsigned pioc;
  440. int gpio_irqnbr = 0;
  441. struct at91_gpio_chip *this, *prev;
  442. for (pioc = 0, this = gpio_chip, prev = NULL;
  443. pioc++ < gpio_banks;
  444. prev = this, this++) {
  445. unsigned pioc_hwirq = this->pioc_hwirq;
  446. int offset;
  447. __raw_writel(~0, this->regbase + PIO_IDR);
  448. /* setup irq domain for this GPIO controller */
  449. at91_gpio_irqdomain(this);
  450. for (offset = 0; offset < this->chip.ngpio; offset++) {
  451. unsigned int virq = irq_find_mapping(this->domain, offset);
  452. irq_set_lockdep_class(virq, &gpio_lock_class);
  453. /*
  454. * Can use the "simple" and not "edge" handler since it's
  455. * shorter, and the AIC handles interrupts sanely.
  456. */
  457. irq_set_chip_and_handler(virq, &gpio_irqchip,
  458. handle_simple_irq);
  459. set_irq_flags(virq, IRQF_VALID);
  460. irq_set_chip_data(virq, this);
  461. gpio_irqnbr++;
  462. }
  463. /* The toplevel handler handles one bank of GPIOs, except
  464. * on some SoC it can handles up to three...
  465. * We only set up the handler for the first of the list.
  466. */
  467. if (prev && prev->next == this)
  468. continue;
  469. irq_set_chip_data(pioc_hwirq, this);
  470. irq_set_chained_handler(pioc_hwirq, gpio_irq_handler);
  471. }
  472. pr_info("AT91: %d gpio irqs in %d banks\n", gpio_irqnbr, gpio_banks);
  473. }
  474. /* gpiolib support */
  475. static int at91_gpiolib_direction_input(struct gpio_chip *chip,
  476. unsigned offset)
  477. {
  478. struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
  479. void __iomem *pio = at91_gpio->regbase;
  480. unsigned mask = 1 << offset;
  481. __raw_writel(mask, pio + PIO_ODR);
  482. return 0;
  483. }
  484. static int at91_gpiolib_direction_output(struct gpio_chip *chip,
  485. unsigned offset, int val)
  486. {
  487. struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
  488. void __iomem *pio = at91_gpio->regbase;
  489. unsigned mask = 1 << offset;
  490. __raw_writel(mask, pio + (val ? PIO_SODR : PIO_CODR));
  491. __raw_writel(mask, pio + PIO_OER);
  492. return 0;
  493. }
  494. static int at91_gpiolib_get(struct gpio_chip *chip, unsigned offset)
  495. {
  496. struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
  497. void __iomem *pio = at91_gpio->regbase;
  498. unsigned mask = 1 << offset;
  499. u32 pdsr;
  500. pdsr = __raw_readl(pio + PIO_PDSR);
  501. return (pdsr & mask) != 0;
  502. }
  503. static void at91_gpiolib_set(struct gpio_chip *chip, unsigned offset, int val)
  504. {
  505. struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
  506. void __iomem *pio = at91_gpio->regbase;
  507. unsigned mask = 1 << offset;
  508. __raw_writel(mask, pio + (val ? PIO_SODR : PIO_CODR));
  509. }
  510. static void at91_gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  511. {
  512. int i;
  513. for (i = 0; i < chip->ngpio; i++) {
  514. unsigned pin = chip->base + i;
  515. void __iomem *pio = pin_to_controller(pin);
  516. unsigned mask = pin_to_mask(pin);
  517. const char *gpio_label;
  518. gpio_label = gpiochip_is_requested(chip, i);
  519. if (gpio_label) {
  520. seq_printf(s, "[%s] GPIO%s%d: ",
  521. gpio_label, chip->label, i);
  522. if (__raw_readl(pio + PIO_PSR) & mask)
  523. seq_printf(s, "[gpio] %s\n",
  524. at91_get_gpio_value(pin) ?
  525. "set" : "clear");
  526. else
  527. seq_printf(s, "[periph %s]\n",
  528. __raw_readl(pio + PIO_ABSR) &
  529. mask ? "B" : "A");
  530. }
  531. }
  532. }
  533. static int at91_gpiolib_to_irq(struct gpio_chip *chip, unsigned offset)
  534. {
  535. struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
  536. int virq = irq_find_mapping(at91_gpio->domain, offset);
  537. dev_dbg(chip->dev, "%s: request IRQ for GPIO %d, return %d\n",
  538. chip->label, offset + chip->base, virq);
  539. return virq;
  540. }
  541. static int __init at91_gpio_setup_clk(int idx)
  542. {
  543. struct at91_gpio_chip *at91_gpio = &gpio_chip[idx];
  544. /* retreive PIO controller's clock */
  545. at91_gpio->clock = clk_get_sys(NULL, at91_gpio->chip.label);
  546. if (IS_ERR(at91_gpio->clock)) {
  547. pr_err("at91_gpio.%d, failed to get clock, ignoring.\n", idx);
  548. goto err;
  549. }
  550. if (clk_prepare(at91_gpio->clock))
  551. goto clk_prep_err;
  552. /* enable PIO controller's clock */
  553. if (clk_enable(at91_gpio->clock)) {
  554. pr_err("at91_gpio.%d, failed to enable clock, ignoring.\n", idx);
  555. goto clk_err;
  556. }
  557. return 0;
  558. clk_err:
  559. clk_unprepare(at91_gpio->clock);
  560. clk_prep_err:
  561. clk_put(at91_gpio->clock);
  562. err:
  563. return -EINVAL;
  564. }
  565. #ifdef CONFIG_OF_GPIO
  566. static void __init of_at91_gpio_init_one(struct device_node *np)
  567. {
  568. int alias_idx;
  569. struct at91_gpio_chip *at91_gpio;
  570. if (!np)
  571. return;
  572. alias_idx = of_alias_get_id(np, "gpio");
  573. if (alias_idx >= MAX_GPIO_BANKS) {
  574. pr_err("at91_gpio, failed alias idx(%d) > MAX_GPIO_BANKS(%d), ignoring.\n",
  575. alias_idx, MAX_GPIO_BANKS);
  576. return;
  577. }
  578. at91_gpio = &gpio_chip[alias_idx];
  579. at91_gpio->chip.base = alias_idx * at91_gpio->chip.ngpio;
  580. at91_gpio->regbase = of_iomap(np, 0);
  581. if (!at91_gpio->regbase) {
  582. pr_err("at91_gpio.%d, failed to map registers, ignoring.\n",
  583. alias_idx);
  584. return;
  585. }
  586. /* Get the interrupts property */
  587. if (of_property_read_u32(np, "interrupts", &at91_gpio->pioc_hwirq)) {
  588. pr_err("at91_gpio.%d, failed to get interrupts property, ignoring.\n",
  589. alias_idx);
  590. goto ioremap_err;
  591. }
  592. /* Setup clock */
  593. if (at91_gpio_setup_clk(alias_idx))
  594. goto ioremap_err;
  595. at91_gpio->chip.of_node = np;
  596. gpio_banks = max(gpio_banks, alias_idx + 1);
  597. at91_gpio->pioc_idx = alias_idx;
  598. return;
  599. ioremap_err:
  600. iounmap(at91_gpio->regbase);
  601. }
  602. static int __init of_at91_gpio_init(void)
  603. {
  604. struct device_node *np = NULL;
  605. /*
  606. * This isn't ideal, but it gets things hooked up until this
  607. * driver is converted into a platform_device
  608. */
  609. for_each_compatible_node(np, NULL, "atmel,at91rm9200-gpio")
  610. of_at91_gpio_init_one(np);
  611. return gpio_banks > 0 ? 0 : -EINVAL;
  612. }
  613. #else
  614. static int __init of_at91_gpio_init(void)
  615. {
  616. return -EINVAL;
  617. }
  618. #endif
  619. static void __init at91_gpio_init_one(int idx, u32 regbase, int pioc_hwirq)
  620. {
  621. struct at91_gpio_chip *at91_gpio = &gpio_chip[idx];
  622. at91_gpio->chip.base = idx * at91_gpio->chip.ngpio;
  623. at91_gpio->pioc_hwirq = pioc_hwirq;
  624. at91_gpio->pioc_idx = idx;
  625. at91_gpio->regbase = ioremap(regbase, 512);
  626. if (!at91_gpio->regbase) {
  627. pr_err("at91_gpio.%d, failed to map registers, ignoring.\n", idx);
  628. return;
  629. }
  630. if (at91_gpio_setup_clk(idx))
  631. goto ioremap_err;
  632. gpio_banks = max(gpio_banks, idx + 1);
  633. return;
  634. ioremap_err:
  635. iounmap(at91_gpio->regbase);
  636. }
  637. /*
  638. * Called from the processor-specific init to enable GPIO pin support.
  639. */
  640. void __init at91_gpio_init(struct at91_gpio_bank *data, int nr_banks)
  641. {
  642. unsigned i;
  643. struct at91_gpio_chip *at91_gpio, *last = NULL;
  644. BUG_ON(nr_banks > MAX_GPIO_BANKS);
  645. if (of_at91_gpio_init() < 0) {
  646. /* No GPIO controller found in device tree */
  647. for (i = 0; i < nr_banks; i++)
  648. at91_gpio_init_one(i, data[i].regbase, data[i].id);
  649. }
  650. for (i = 0; i < gpio_banks; i++) {
  651. at91_gpio = &gpio_chip[i];
  652. /*
  653. * GPIO controller are grouped on some SoC:
  654. * PIOC, PIOD and PIOE can share the same IRQ line
  655. */
  656. if (last && last->pioc_hwirq == at91_gpio->pioc_hwirq)
  657. last->next = at91_gpio;
  658. last = at91_gpio;
  659. gpiochip_add(&at91_gpio->chip);
  660. }
  661. }