gpio.c 19 KB

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