gpio.c 20 KB

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