gpio.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113
  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 <linux/of_gpio.h>
  27. #include <asm/mach/irq.h>
  28. #include <mach/hardware.h>
  29. #include <mach/at91_pio.h>
  30. #include "generic.h"
  31. struct at91_gpio_chip {
  32. struct gpio_chip chip;
  33. struct at91_gpio_chip *next; /* Bank sharing same clock */
  34. int pioc_hwirq; /* PIO bank interrupt identifier on AIC */
  35. int pioc_virq; /* PIO bank Linux virtual interrupt */
  36. int pioc_idx; /* PIO bank index */
  37. void __iomem *regbase; /* PIO bank virtual address */
  38. struct clk *clock; /* associated clock */
  39. struct irq_domain *domain; /* associated irq domain */
  40. };
  41. #define to_at91_gpio_chip(c) container_of(c, struct at91_gpio_chip, chip)
  42. static int at91_gpiolib_request(struct gpio_chip *chip, unsigned offset);
  43. static void at91_gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip);
  44. static void at91_gpiolib_set(struct gpio_chip *chip, unsigned offset, int val);
  45. static int at91_gpiolib_get(struct gpio_chip *chip, unsigned offset);
  46. static int at91_gpiolib_direction_output(struct gpio_chip *chip,
  47. unsigned offset, int val);
  48. static int at91_gpiolib_direction_input(struct gpio_chip *chip,
  49. unsigned offset);
  50. static int at91_gpiolib_to_irq(struct gpio_chip *chip, unsigned offset);
  51. #define AT91_GPIO_CHIP(name, nr_gpio) \
  52. { \
  53. .chip = { \
  54. .label = name, \
  55. .request = at91_gpiolib_request, \
  56. .direction_input = at91_gpiolib_direction_input, \
  57. .direction_output = at91_gpiolib_direction_output, \
  58. .get = at91_gpiolib_get, \
  59. .set = at91_gpiolib_set, \
  60. .dbg_show = at91_gpiolib_dbg_show, \
  61. .to_irq = at91_gpiolib_to_irq, \
  62. .ngpio = nr_gpio, \
  63. }, \
  64. }
  65. static struct at91_gpio_chip gpio_chip[] = {
  66. AT91_GPIO_CHIP("pioA", 32),
  67. AT91_GPIO_CHIP("pioB", 32),
  68. AT91_GPIO_CHIP("pioC", 32),
  69. AT91_GPIO_CHIP("pioD", 32),
  70. AT91_GPIO_CHIP("pioE", 32),
  71. };
  72. static int gpio_banks;
  73. static unsigned long at91_gpio_caps;
  74. /* All PIO controllers support PIO3 features */
  75. #define AT91_GPIO_CAP_PIO3 (1 << 0)
  76. #define has_pio3() (at91_gpio_caps & AT91_GPIO_CAP_PIO3)
  77. /*--------------------------------------------------------------------------*/
  78. static inline void __iomem *pin_to_controller(unsigned pin)
  79. {
  80. pin /= 32;
  81. if (likely(pin < gpio_banks))
  82. return gpio_chip[pin].regbase;
  83. return NULL;
  84. }
  85. static inline unsigned pin_to_mask(unsigned pin)
  86. {
  87. return 1 << (pin % 32);
  88. }
  89. static char peripheral_function(void __iomem *pio, unsigned mask)
  90. {
  91. char ret = 'X';
  92. u8 select;
  93. if (pio) {
  94. if (has_pio3()) {
  95. select = !!(__raw_readl(pio + PIO_ABCDSR1) & mask);
  96. select |= (!!(__raw_readl(pio + PIO_ABCDSR2) & mask) << 1);
  97. ret = 'A' + select;
  98. } else {
  99. ret = __raw_readl(pio + PIO_ABSR) & mask ?
  100. 'B' : 'A';
  101. }
  102. }
  103. return ret;
  104. }
  105. /*--------------------------------------------------------------------------*/
  106. /* Not all hardware capabilities are exposed through these calls; they
  107. * only encapsulate the most common features and modes. (So if you
  108. * want to change signals in groups, do it directly.)
  109. *
  110. * Bootloaders will usually handle some of the pin multiplexing setup.
  111. * The intent is certainly that by the time Linux is fully booted, all
  112. * pins should have been fully initialized. These setup calls should
  113. * only be used by board setup routines, or possibly in driver probe().
  114. *
  115. * For bootloaders doing all that setup, these calls could be inlined
  116. * as NOPs so Linux won't duplicate any setup code
  117. */
  118. /*
  119. * mux the pin to the "GPIO" peripheral role.
  120. */
  121. int __init_or_module at91_set_GPIO_periph(unsigned pin, int use_pullup)
  122. {
  123. void __iomem *pio = pin_to_controller(pin);
  124. unsigned mask = pin_to_mask(pin);
  125. if (!pio)
  126. return -EINVAL;
  127. __raw_writel(mask, pio + PIO_IDR);
  128. __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
  129. __raw_writel(mask, pio + PIO_PER);
  130. return 0;
  131. }
  132. EXPORT_SYMBOL(at91_set_GPIO_periph);
  133. /*
  134. * mux the pin to the "A" internal peripheral role.
  135. */
  136. int __init_or_module at91_set_A_periph(unsigned pin, int use_pullup)
  137. {
  138. void __iomem *pio = pin_to_controller(pin);
  139. unsigned mask = pin_to_mask(pin);
  140. if (!pio)
  141. return -EINVAL;
  142. __raw_writel(mask, pio + PIO_IDR);
  143. __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
  144. if (has_pio3()) {
  145. __raw_writel(__raw_readl(pio + PIO_ABCDSR1) & ~mask,
  146. pio + PIO_ABCDSR1);
  147. __raw_writel(__raw_readl(pio + PIO_ABCDSR2) & ~mask,
  148. pio + PIO_ABCDSR2);
  149. } else {
  150. __raw_writel(mask, pio + PIO_ASR);
  151. }
  152. __raw_writel(mask, pio + PIO_PDR);
  153. return 0;
  154. }
  155. EXPORT_SYMBOL(at91_set_A_periph);
  156. /*
  157. * mux the pin to the "B" internal peripheral role.
  158. */
  159. int __init_or_module at91_set_B_periph(unsigned pin, int use_pullup)
  160. {
  161. void __iomem *pio = pin_to_controller(pin);
  162. unsigned mask = pin_to_mask(pin);
  163. if (!pio)
  164. return -EINVAL;
  165. __raw_writel(mask, pio + PIO_IDR);
  166. __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
  167. if (has_pio3()) {
  168. __raw_writel(__raw_readl(pio + PIO_ABCDSR1) | mask,
  169. pio + PIO_ABCDSR1);
  170. __raw_writel(__raw_readl(pio + PIO_ABCDSR2) & ~mask,
  171. pio + PIO_ABCDSR2);
  172. } else {
  173. __raw_writel(mask, pio + PIO_BSR);
  174. }
  175. __raw_writel(mask, pio + PIO_PDR);
  176. return 0;
  177. }
  178. EXPORT_SYMBOL(at91_set_B_periph);
  179. /*
  180. * mux the pin to the "C" internal peripheral role.
  181. */
  182. int __init_or_module at91_set_C_periph(unsigned pin, int use_pullup)
  183. {
  184. void __iomem *pio = pin_to_controller(pin);
  185. unsigned mask = pin_to_mask(pin);
  186. if (!pio || !has_pio3())
  187. return -EINVAL;
  188. __raw_writel(mask, pio + PIO_IDR);
  189. __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
  190. __raw_writel(__raw_readl(pio + PIO_ABCDSR1) & ~mask, pio + PIO_ABCDSR1);
  191. __raw_writel(__raw_readl(pio + PIO_ABCDSR2) | mask, pio + PIO_ABCDSR2);
  192. __raw_writel(mask, pio + PIO_PDR);
  193. return 0;
  194. }
  195. EXPORT_SYMBOL(at91_set_C_periph);
  196. /*
  197. * mux the pin to the "D" internal peripheral role.
  198. */
  199. int __init_or_module at91_set_D_periph(unsigned pin, int use_pullup)
  200. {
  201. void __iomem *pio = pin_to_controller(pin);
  202. unsigned mask = pin_to_mask(pin);
  203. if (!pio || !has_pio3())
  204. return -EINVAL;
  205. __raw_writel(mask, pio + PIO_IDR);
  206. __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
  207. __raw_writel(__raw_readl(pio + PIO_ABCDSR1) | mask, pio + PIO_ABCDSR1);
  208. __raw_writel(__raw_readl(pio + PIO_ABCDSR2) | mask, pio + PIO_ABCDSR2);
  209. __raw_writel(mask, pio + PIO_PDR);
  210. return 0;
  211. }
  212. EXPORT_SYMBOL(at91_set_D_periph);
  213. /*
  214. * mux the pin to the gpio controller (instead of "A", "B", "C"
  215. * or "D" peripheral), and configure it for an input.
  216. */
  217. int __init_or_module at91_set_gpio_input(unsigned pin, int use_pullup)
  218. {
  219. void __iomem *pio = pin_to_controller(pin);
  220. unsigned mask = pin_to_mask(pin);
  221. if (!pio)
  222. return -EINVAL;
  223. __raw_writel(mask, pio + PIO_IDR);
  224. __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
  225. __raw_writel(mask, pio + PIO_ODR);
  226. __raw_writel(mask, pio + PIO_PER);
  227. return 0;
  228. }
  229. EXPORT_SYMBOL(at91_set_gpio_input);
  230. /*
  231. * mux the pin to the gpio controller (instead of "A", "B", "C"
  232. * or "D" peripheral), and configure it for an output.
  233. */
  234. int __init_or_module at91_set_gpio_output(unsigned pin, int value)
  235. {
  236. void __iomem *pio = pin_to_controller(pin);
  237. unsigned mask = pin_to_mask(pin);
  238. if (!pio)
  239. return -EINVAL;
  240. __raw_writel(mask, pio + PIO_IDR);
  241. __raw_writel(mask, pio + PIO_PUDR);
  242. __raw_writel(mask, pio + (value ? PIO_SODR : PIO_CODR));
  243. __raw_writel(mask, pio + PIO_OER);
  244. __raw_writel(mask, pio + PIO_PER);
  245. return 0;
  246. }
  247. EXPORT_SYMBOL(at91_set_gpio_output);
  248. /*
  249. * enable/disable the glitch filter; mostly used with IRQ handling.
  250. */
  251. int __init_or_module at91_set_deglitch(unsigned pin, int is_on)
  252. {
  253. void __iomem *pio = pin_to_controller(pin);
  254. unsigned mask = pin_to_mask(pin);
  255. if (!pio)
  256. return -EINVAL;
  257. if (has_pio3() && is_on)
  258. __raw_writel(mask, pio + PIO_IFSCDR);
  259. __raw_writel(mask, pio + (is_on ? PIO_IFER : PIO_IFDR));
  260. return 0;
  261. }
  262. EXPORT_SYMBOL(at91_set_deglitch);
  263. /*
  264. * enable/disable the debounce filter;
  265. */
  266. int __init_or_module at91_set_debounce(unsigned pin, int is_on, int div)
  267. {
  268. void __iomem *pio = pin_to_controller(pin);
  269. unsigned mask = pin_to_mask(pin);
  270. if (!pio || !has_pio3())
  271. return -EINVAL;
  272. if (is_on) {
  273. __raw_writel(mask, pio + PIO_IFSCER);
  274. __raw_writel(div & PIO_SCDR_DIV, pio + PIO_SCDR);
  275. __raw_writel(mask, pio + PIO_IFER);
  276. } else {
  277. __raw_writel(mask, pio + PIO_IFDR);
  278. }
  279. return 0;
  280. }
  281. EXPORT_SYMBOL(at91_set_debounce);
  282. /*
  283. * enable/disable the multi-driver; This is only valid for output and
  284. * allows the output pin to run as an open collector output.
  285. */
  286. int __init_or_module at91_set_multi_drive(unsigned pin, int is_on)
  287. {
  288. void __iomem *pio = pin_to_controller(pin);
  289. unsigned mask = pin_to_mask(pin);
  290. if (!pio)
  291. return -EINVAL;
  292. __raw_writel(mask, pio + (is_on ? PIO_MDER : PIO_MDDR));
  293. return 0;
  294. }
  295. EXPORT_SYMBOL(at91_set_multi_drive);
  296. /*
  297. * enable/disable the pull-down.
  298. * If pull-up already enabled while calling the function, we disable it.
  299. */
  300. int __init_or_module at91_set_pulldown(unsigned pin, int is_on)
  301. {
  302. void __iomem *pio = pin_to_controller(pin);
  303. unsigned mask = pin_to_mask(pin);
  304. if (!pio || !has_pio3())
  305. return -EINVAL;
  306. /* Disable pull-up anyway */
  307. __raw_writel(mask, pio + PIO_PUDR);
  308. __raw_writel(mask, pio + (is_on ? PIO_PPDER : PIO_PPDDR));
  309. return 0;
  310. }
  311. EXPORT_SYMBOL(at91_set_pulldown);
  312. /*
  313. * disable Schmitt trigger
  314. */
  315. int __init_or_module at91_disable_schmitt_trig(unsigned pin)
  316. {
  317. void __iomem *pio = pin_to_controller(pin);
  318. unsigned mask = pin_to_mask(pin);
  319. if (!pio || !has_pio3())
  320. return -EINVAL;
  321. __raw_writel(__raw_readl(pio + PIO_SCHMITT) | mask, pio + PIO_SCHMITT);
  322. return 0;
  323. }
  324. EXPORT_SYMBOL(at91_disable_schmitt_trig);
  325. /*
  326. * assuming the pin is muxed as a gpio output, set its value.
  327. */
  328. int at91_set_gpio_value(unsigned pin, int value)
  329. {
  330. void __iomem *pio = pin_to_controller(pin);
  331. unsigned mask = pin_to_mask(pin);
  332. if (!pio)
  333. return -EINVAL;
  334. __raw_writel(mask, pio + (value ? PIO_SODR : PIO_CODR));
  335. return 0;
  336. }
  337. EXPORT_SYMBOL(at91_set_gpio_value);
  338. /*
  339. * read the pin's value (works even if it's not muxed as a gpio).
  340. */
  341. int at91_get_gpio_value(unsigned pin)
  342. {
  343. void __iomem *pio = pin_to_controller(pin);
  344. unsigned mask = pin_to_mask(pin);
  345. u32 pdsr;
  346. if (!pio)
  347. return -EINVAL;
  348. pdsr = __raw_readl(pio + PIO_PDSR);
  349. return (pdsr & mask) != 0;
  350. }
  351. EXPORT_SYMBOL(at91_get_gpio_value);
  352. /*--------------------------------------------------------------------------*/
  353. #ifdef CONFIG_PM
  354. static u32 wakeups[MAX_GPIO_BANKS];
  355. static u32 backups[MAX_GPIO_BANKS];
  356. static int gpio_irq_set_wake(struct irq_data *d, unsigned state)
  357. {
  358. struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
  359. unsigned mask = 1 << d->hwirq;
  360. unsigned bank = at91_gpio->pioc_idx;
  361. if (unlikely(bank >= MAX_GPIO_BANKS))
  362. return -EINVAL;
  363. if (state)
  364. wakeups[bank] |= mask;
  365. else
  366. wakeups[bank] &= ~mask;
  367. irq_set_irq_wake(at91_gpio->pioc_virq, state);
  368. return 0;
  369. }
  370. void at91_gpio_suspend(void)
  371. {
  372. int i;
  373. for (i = 0; i < gpio_banks; i++) {
  374. void __iomem *pio = gpio_chip[i].regbase;
  375. backups[i] = __raw_readl(pio + PIO_IMR);
  376. __raw_writel(backups[i], pio + PIO_IDR);
  377. __raw_writel(wakeups[i], pio + PIO_IER);
  378. if (!wakeups[i]) {
  379. clk_unprepare(gpio_chip[i].clock);
  380. clk_disable(gpio_chip[i].clock);
  381. } else {
  382. #ifdef CONFIG_PM_DEBUG
  383. printk(KERN_DEBUG "GPIO-%c may wake for %08x\n", 'A'+i, wakeups[i]);
  384. #endif
  385. }
  386. }
  387. }
  388. void at91_gpio_resume(void)
  389. {
  390. int i;
  391. for (i = 0; i < gpio_banks; i++) {
  392. void __iomem *pio = gpio_chip[i].regbase;
  393. if (!wakeups[i]) {
  394. if (clk_prepare(gpio_chip[i].clock) == 0)
  395. clk_enable(gpio_chip[i].clock);
  396. }
  397. __raw_writel(wakeups[i], pio + PIO_IDR);
  398. __raw_writel(backups[i], pio + PIO_IER);
  399. }
  400. }
  401. #else
  402. #define gpio_irq_set_wake NULL
  403. #endif
  404. /* Several AIC controller irqs are dispatched through this GPIO handler.
  405. * To use any AT91_PIN_* as an externally triggered IRQ, first call
  406. * at91_set_gpio_input() then maybe enable its glitch filter.
  407. * Then just request_irq() with the pin ID; it works like any ARM IRQ
  408. * handler.
  409. * First implementation always triggers on rising and falling edges
  410. * whereas the newer PIO3 can be additionally configured to trigger on
  411. * level, edge with any polarity.
  412. *
  413. * Alternatively, certain pins may be used directly as IRQ0..IRQ6 after
  414. * configuring them with at91_set_a_periph() or at91_set_b_periph().
  415. * IRQ0..IRQ6 should be configurable, e.g. level vs edge triggering.
  416. */
  417. static void gpio_irq_mask(struct irq_data *d)
  418. {
  419. struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
  420. void __iomem *pio = at91_gpio->regbase;
  421. unsigned mask = 1 << d->hwirq;
  422. if (pio)
  423. __raw_writel(mask, pio + PIO_IDR);
  424. }
  425. static void gpio_irq_unmask(struct irq_data *d)
  426. {
  427. struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
  428. void __iomem *pio = at91_gpio->regbase;
  429. unsigned mask = 1 << d->hwirq;
  430. if (pio)
  431. __raw_writel(mask, pio + PIO_IER);
  432. }
  433. static int gpio_irq_type(struct irq_data *d, unsigned type)
  434. {
  435. switch (type) {
  436. case IRQ_TYPE_NONE:
  437. case IRQ_TYPE_EDGE_BOTH:
  438. return 0;
  439. default:
  440. return -EINVAL;
  441. }
  442. }
  443. /* Alternate irq type for PIO3 support */
  444. static int alt_gpio_irq_type(struct irq_data *d, unsigned type)
  445. {
  446. struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
  447. void __iomem *pio = at91_gpio->regbase;
  448. unsigned mask = 1 << d->hwirq;
  449. switch (type) {
  450. case IRQ_TYPE_EDGE_RISING:
  451. __raw_writel(mask, pio + PIO_ESR);
  452. __raw_writel(mask, pio + PIO_REHLSR);
  453. break;
  454. case IRQ_TYPE_EDGE_FALLING:
  455. __raw_writel(mask, pio + PIO_ESR);
  456. __raw_writel(mask, pio + PIO_FELLSR);
  457. break;
  458. case IRQ_TYPE_LEVEL_LOW:
  459. __raw_writel(mask, pio + PIO_LSR);
  460. __raw_writel(mask, pio + PIO_FELLSR);
  461. break;
  462. case IRQ_TYPE_LEVEL_HIGH:
  463. __raw_writel(mask, pio + PIO_LSR);
  464. __raw_writel(mask, pio + PIO_REHLSR);
  465. break;
  466. case IRQ_TYPE_EDGE_BOTH:
  467. /*
  468. * disable additional interrupt modes:
  469. * fall back to default behavior
  470. */
  471. __raw_writel(mask, pio + PIO_AIMDR);
  472. return 0;
  473. case IRQ_TYPE_NONE:
  474. default:
  475. pr_warn("AT91: No type for irq %d\n", gpio_to_irq(d->irq));
  476. return -EINVAL;
  477. }
  478. /* enable additional interrupt modes */
  479. __raw_writel(mask, pio + PIO_AIMER);
  480. return 0;
  481. }
  482. static struct irq_chip gpio_irqchip = {
  483. .name = "GPIO",
  484. .irq_disable = gpio_irq_mask,
  485. .irq_mask = gpio_irq_mask,
  486. .irq_unmask = gpio_irq_unmask,
  487. /* .irq_set_type is set dynamically */
  488. .irq_set_wake = gpio_irq_set_wake,
  489. };
  490. static void gpio_irq_handler(unsigned irq, struct irq_desc *desc)
  491. {
  492. struct irq_chip *chip = irq_desc_get_chip(desc);
  493. struct irq_data *idata = irq_desc_get_irq_data(desc);
  494. struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(idata);
  495. void __iomem *pio = at91_gpio->regbase;
  496. unsigned long isr;
  497. int n;
  498. chained_irq_enter(chip, desc);
  499. for (;;) {
  500. /* Reading ISR acks pending (edge triggered) GPIO interrupts.
  501. * When there none are pending, we're finished unless we need
  502. * to process multiple banks (like ID_PIOCDE on sam9263).
  503. */
  504. isr = __raw_readl(pio + PIO_ISR) & __raw_readl(pio + PIO_IMR);
  505. if (!isr) {
  506. if (!at91_gpio->next)
  507. break;
  508. at91_gpio = at91_gpio->next;
  509. pio = at91_gpio->regbase;
  510. continue;
  511. }
  512. n = find_first_bit(&isr, BITS_PER_LONG);
  513. while (n < BITS_PER_LONG) {
  514. generic_handle_irq(irq_find_mapping(at91_gpio->domain, n));
  515. n = find_next_bit(&isr, BITS_PER_LONG, n + 1);
  516. }
  517. }
  518. chained_irq_exit(chip, desc);
  519. /* now it may re-trigger */
  520. }
  521. /*--------------------------------------------------------------------------*/
  522. #ifdef CONFIG_DEBUG_FS
  523. static void gpio_printf(struct seq_file *s, void __iomem *pio, unsigned mask)
  524. {
  525. char *trigger = NULL;
  526. char *polarity = NULL;
  527. if (__raw_readl(pio + PIO_IMR) & mask) {
  528. if (!has_pio3() || !(__raw_readl(pio + PIO_AIMMR) & mask )) {
  529. trigger = "edge";
  530. polarity = "both";
  531. } else {
  532. if (__raw_readl(pio + PIO_ELSR) & mask) {
  533. trigger = "level";
  534. polarity = __raw_readl(pio + PIO_FRLHSR) & mask ?
  535. "high" : "low";
  536. } else {
  537. trigger = "edge";
  538. polarity = __raw_readl(pio + PIO_FRLHSR) & mask ?
  539. "rising" : "falling";
  540. }
  541. }
  542. seq_printf(s, "IRQ:%s-%s\t", trigger, polarity);
  543. } else {
  544. seq_printf(s, "GPIO:%s\t\t",
  545. __raw_readl(pio + PIO_PDSR) & mask ? "1" : "0");
  546. }
  547. }
  548. static int at91_gpio_show(struct seq_file *s, void *unused)
  549. {
  550. int bank, j;
  551. /* print heading */
  552. seq_printf(s, "Pin\t");
  553. for (bank = 0; bank < gpio_banks; bank++) {
  554. seq_printf(s, "PIO%c\t\t", 'A' + bank);
  555. };
  556. seq_printf(s, "\n\n");
  557. /* print pin status */
  558. for (j = 0; j < 32; j++) {
  559. seq_printf(s, "%i:\t", j);
  560. for (bank = 0; bank < gpio_banks; bank++) {
  561. unsigned pin = (32 * bank) + j;
  562. void __iomem *pio = pin_to_controller(pin);
  563. unsigned mask = pin_to_mask(pin);
  564. if (__raw_readl(pio + PIO_PSR) & mask)
  565. gpio_printf(s, pio, mask);
  566. else
  567. seq_printf(s, "%c\t\t",
  568. peripheral_function(pio, mask));
  569. }
  570. seq_printf(s, "\n");
  571. }
  572. return 0;
  573. }
  574. static int at91_gpio_open(struct inode *inode, struct file *file)
  575. {
  576. return single_open(file, at91_gpio_show, NULL);
  577. }
  578. static const struct file_operations at91_gpio_operations = {
  579. .open = at91_gpio_open,
  580. .read = seq_read,
  581. .llseek = seq_lseek,
  582. .release = single_release,
  583. };
  584. static int __init at91_gpio_debugfs_init(void)
  585. {
  586. /* /sys/kernel/debug/at91_gpio */
  587. (void) debugfs_create_file("at91_gpio", S_IFREG | S_IRUGO, NULL, NULL, &at91_gpio_operations);
  588. return 0;
  589. }
  590. postcore_initcall(at91_gpio_debugfs_init);
  591. #endif
  592. /*--------------------------------------------------------------------------*/
  593. /*
  594. * This lock class tells lockdep that GPIO irqs are in a different
  595. * category than their parents, so it won't report false recursion.
  596. */
  597. static struct lock_class_key gpio_lock_class;
  598. #if defined(CONFIG_OF)
  599. static int at91_gpio_irq_map(struct irq_domain *h, unsigned int virq,
  600. irq_hw_number_t hw)
  601. {
  602. struct at91_gpio_chip *at91_gpio = h->host_data;
  603. irq_set_lockdep_class(virq, &gpio_lock_class);
  604. /*
  605. * Can use the "simple" and not "edge" handler since it's
  606. * shorter, and the AIC handles interrupts sanely.
  607. */
  608. irq_set_chip_and_handler(virq, &gpio_irqchip,
  609. handle_simple_irq);
  610. set_irq_flags(virq, IRQF_VALID);
  611. irq_set_chip_data(virq, at91_gpio);
  612. return 0;
  613. }
  614. static struct irq_domain_ops at91_gpio_ops = {
  615. .map = at91_gpio_irq_map,
  616. .xlate = irq_domain_xlate_twocell,
  617. };
  618. int __init at91_gpio_of_irq_setup(struct device_node *node,
  619. struct device_node *parent)
  620. {
  621. struct at91_gpio_chip *prev = NULL;
  622. int alias_idx = of_alias_get_id(node, "gpio");
  623. struct at91_gpio_chip *at91_gpio = &gpio_chip[alias_idx];
  624. /* Setup proper .irq_set_type function */
  625. if (has_pio3())
  626. gpio_irqchip.irq_set_type = alt_gpio_irq_type;
  627. else
  628. gpio_irqchip.irq_set_type = gpio_irq_type;
  629. /* Disable irqs of this PIO controller */
  630. __raw_writel(~0, at91_gpio->regbase + PIO_IDR);
  631. /* Setup irq domain */
  632. at91_gpio->domain = irq_domain_add_linear(node, at91_gpio->chip.ngpio,
  633. &at91_gpio_ops, at91_gpio);
  634. if (!at91_gpio->domain)
  635. panic("at91_gpio.%d: couldn't allocate irq domain (DT).\n",
  636. at91_gpio->pioc_idx);
  637. /* Setup chained handler */
  638. if (at91_gpio->pioc_idx)
  639. prev = &gpio_chip[at91_gpio->pioc_idx - 1];
  640. /* The toplevel handler handles one bank of GPIOs, except
  641. * on some SoC it can handles up to three...
  642. * We only set up the handler for the first of the list.
  643. */
  644. if (prev && prev->next == at91_gpio)
  645. return 0;
  646. at91_gpio->pioc_virq = irq_create_mapping(irq_find_host(parent),
  647. at91_gpio->pioc_hwirq);
  648. irq_set_chip_data(at91_gpio->pioc_virq, at91_gpio);
  649. irq_set_chained_handler(at91_gpio->pioc_virq, gpio_irq_handler);
  650. return 0;
  651. }
  652. #else
  653. int __init at91_gpio_of_irq_setup(struct device_node *node,
  654. struct device_node *parent)
  655. {
  656. return -EINVAL;
  657. }
  658. #endif
  659. /*
  660. * irqdomain initialization: pile up irqdomains on top of AIC range
  661. */
  662. static void __init at91_gpio_irqdomain(struct at91_gpio_chip *at91_gpio)
  663. {
  664. int irq_base;
  665. irq_base = irq_alloc_descs(-1, 0, at91_gpio->chip.ngpio, 0);
  666. if (irq_base < 0)
  667. panic("at91_gpio.%d: error %d: couldn't allocate IRQ numbers.\n",
  668. at91_gpio->pioc_idx, irq_base);
  669. at91_gpio->domain = irq_domain_add_legacy(NULL, at91_gpio->chip.ngpio,
  670. irq_base, 0,
  671. &irq_domain_simple_ops, NULL);
  672. if (!at91_gpio->domain)
  673. panic("at91_gpio.%d: couldn't allocate irq domain.\n",
  674. at91_gpio->pioc_idx);
  675. }
  676. /*
  677. * Called from the processor-specific init to enable GPIO interrupt support.
  678. */
  679. void __init at91_gpio_irq_setup(void)
  680. {
  681. unsigned pioc;
  682. int gpio_irqnbr = 0;
  683. struct at91_gpio_chip *this, *prev;
  684. /* Setup proper .irq_set_type function */
  685. if (has_pio3())
  686. gpio_irqchip.irq_set_type = alt_gpio_irq_type;
  687. else
  688. gpio_irqchip.irq_set_type = gpio_irq_type;
  689. for (pioc = 0, this = gpio_chip, prev = NULL;
  690. pioc++ < gpio_banks;
  691. prev = this, this++) {
  692. int offset;
  693. __raw_writel(~0, this->regbase + PIO_IDR);
  694. /* setup irq domain for this GPIO controller */
  695. at91_gpio_irqdomain(this);
  696. for (offset = 0; offset < this->chip.ngpio; offset++) {
  697. unsigned int virq = irq_find_mapping(this->domain, offset);
  698. irq_set_lockdep_class(virq, &gpio_lock_class);
  699. /*
  700. * Can use the "simple" and not "edge" handler since it's
  701. * shorter, and the AIC handles interrupts sanely.
  702. */
  703. irq_set_chip_and_handler(virq, &gpio_irqchip,
  704. handle_simple_irq);
  705. set_irq_flags(virq, IRQF_VALID);
  706. irq_set_chip_data(virq, this);
  707. gpio_irqnbr++;
  708. }
  709. /* The toplevel handler handles one bank of GPIOs, except
  710. * on some SoC it can handles up to three...
  711. * We only set up the handler for the first of the list.
  712. */
  713. if (prev && prev->next == this)
  714. continue;
  715. this->pioc_virq = irq_create_mapping(NULL, this->pioc_hwirq);
  716. irq_set_chip_data(this->pioc_virq, this);
  717. irq_set_chained_handler(this->pioc_virq, gpio_irq_handler);
  718. }
  719. pr_info("AT91: %d gpio irqs in %d banks\n", gpio_irqnbr, gpio_banks);
  720. }
  721. /* gpiolib support */
  722. static int at91_gpiolib_request(struct gpio_chip *chip, unsigned offset)
  723. {
  724. struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
  725. void __iomem *pio = at91_gpio->regbase;
  726. unsigned mask = 1 << offset;
  727. __raw_writel(mask, pio + PIO_PER);
  728. return 0;
  729. }
  730. static int at91_gpiolib_direction_input(struct gpio_chip *chip,
  731. unsigned offset)
  732. {
  733. struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
  734. void __iomem *pio = at91_gpio->regbase;
  735. unsigned mask = 1 << offset;
  736. __raw_writel(mask, pio + PIO_ODR);
  737. return 0;
  738. }
  739. static int at91_gpiolib_direction_output(struct gpio_chip *chip,
  740. unsigned offset, int val)
  741. {
  742. struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
  743. void __iomem *pio = at91_gpio->regbase;
  744. unsigned mask = 1 << offset;
  745. __raw_writel(mask, pio + (val ? PIO_SODR : PIO_CODR));
  746. __raw_writel(mask, pio + PIO_OER);
  747. return 0;
  748. }
  749. static int at91_gpiolib_get(struct gpio_chip *chip, unsigned offset)
  750. {
  751. struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
  752. void __iomem *pio = at91_gpio->regbase;
  753. unsigned mask = 1 << offset;
  754. u32 pdsr;
  755. pdsr = __raw_readl(pio + PIO_PDSR);
  756. return (pdsr & mask) != 0;
  757. }
  758. static void at91_gpiolib_set(struct gpio_chip *chip, unsigned offset, int val)
  759. {
  760. struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
  761. void __iomem *pio = at91_gpio->regbase;
  762. unsigned mask = 1 << offset;
  763. __raw_writel(mask, pio + (val ? PIO_SODR : PIO_CODR));
  764. }
  765. static void at91_gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  766. {
  767. int i;
  768. for (i = 0; i < chip->ngpio; i++) {
  769. unsigned pin = chip->base + i;
  770. void __iomem *pio = pin_to_controller(pin);
  771. unsigned mask = pin_to_mask(pin);
  772. const char *gpio_label;
  773. gpio_label = gpiochip_is_requested(chip, i);
  774. if (gpio_label) {
  775. seq_printf(s, "[%s] GPIO%s%d: ",
  776. gpio_label, chip->label, i);
  777. if (__raw_readl(pio + PIO_PSR) & mask)
  778. seq_printf(s, "[gpio] %s\n",
  779. at91_get_gpio_value(pin) ?
  780. "set" : "clear");
  781. else
  782. seq_printf(s, "[periph %c]\n",
  783. peripheral_function(pio, mask));
  784. }
  785. }
  786. }
  787. static int at91_gpiolib_to_irq(struct gpio_chip *chip, unsigned offset)
  788. {
  789. struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
  790. int virq;
  791. if (offset < chip->ngpio)
  792. virq = irq_create_mapping(at91_gpio->domain, offset);
  793. else
  794. virq = -ENXIO;
  795. dev_dbg(chip->dev, "%s: request IRQ for GPIO %d, return %d\n",
  796. chip->label, offset + chip->base, virq);
  797. return virq;
  798. }
  799. static int __init at91_gpio_setup_clk(int idx)
  800. {
  801. struct at91_gpio_chip *at91_gpio = &gpio_chip[idx];
  802. /* retreive PIO controller's clock */
  803. at91_gpio->clock = clk_get_sys(NULL, at91_gpio->chip.label);
  804. if (IS_ERR(at91_gpio->clock)) {
  805. pr_err("at91_gpio.%d, failed to get clock, ignoring.\n", idx);
  806. goto err;
  807. }
  808. if (clk_prepare(at91_gpio->clock))
  809. goto clk_prep_err;
  810. /* enable PIO controller's clock */
  811. if (clk_enable(at91_gpio->clock)) {
  812. pr_err("at91_gpio.%d, failed to enable clock, ignoring.\n", idx);
  813. goto clk_err;
  814. }
  815. return 0;
  816. clk_err:
  817. clk_unprepare(at91_gpio->clock);
  818. clk_prep_err:
  819. clk_put(at91_gpio->clock);
  820. err:
  821. return -EINVAL;
  822. }
  823. #ifdef CONFIG_OF_GPIO
  824. static void __init of_at91_gpio_init_one(struct device_node *np)
  825. {
  826. int alias_idx;
  827. struct at91_gpio_chip *at91_gpio;
  828. if (!np)
  829. return;
  830. alias_idx = of_alias_get_id(np, "gpio");
  831. if (alias_idx >= MAX_GPIO_BANKS) {
  832. pr_err("at91_gpio, failed alias idx(%d) > MAX_GPIO_BANKS(%d), ignoring.\n",
  833. alias_idx, MAX_GPIO_BANKS);
  834. return;
  835. }
  836. at91_gpio = &gpio_chip[alias_idx];
  837. at91_gpio->chip.base = alias_idx * at91_gpio->chip.ngpio;
  838. at91_gpio->regbase = of_iomap(np, 0);
  839. if (!at91_gpio->regbase) {
  840. pr_err("at91_gpio.%d, failed to map registers, ignoring.\n",
  841. alias_idx);
  842. return;
  843. }
  844. /* Get the interrupts property */
  845. if (of_property_read_u32(np, "interrupts", &at91_gpio->pioc_hwirq)) {
  846. pr_err("at91_gpio.%d, failed to get interrupts property, ignoring.\n",
  847. alias_idx);
  848. goto ioremap_err;
  849. }
  850. /* Get capabilities from compatibility property */
  851. if (of_device_is_compatible(np, "atmel,at91sam9x5-gpio"))
  852. at91_gpio_caps |= AT91_GPIO_CAP_PIO3;
  853. /* Setup clock */
  854. if (at91_gpio_setup_clk(alias_idx))
  855. goto ioremap_err;
  856. at91_gpio->chip.of_node = np;
  857. gpio_banks = max(gpio_banks, alias_idx + 1);
  858. at91_gpio->pioc_idx = alias_idx;
  859. return;
  860. ioremap_err:
  861. iounmap(at91_gpio->regbase);
  862. }
  863. static int __init of_at91_gpio_init(void)
  864. {
  865. struct device_node *np = NULL;
  866. /*
  867. * This isn't ideal, but it gets things hooked up until this
  868. * driver is converted into a platform_device
  869. */
  870. for_each_compatible_node(np, NULL, "atmel,at91rm9200-gpio")
  871. of_at91_gpio_init_one(np);
  872. return gpio_banks > 0 ? 0 : -EINVAL;
  873. }
  874. #else
  875. static int __init of_at91_gpio_init(void)
  876. {
  877. return -EINVAL;
  878. }
  879. #endif
  880. static void __init at91_gpio_init_one(int idx, u32 regbase, int pioc_hwirq)
  881. {
  882. struct at91_gpio_chip *at91_gpio = &gpio_chip[idx];
  883. at91_gpio->chip.base = idx * at91_gpio->chip.ngpio;
  884. at91_gpio->pioc_hwirq = pioc_hwirq;
  885. at91_gpio->pioc_idx = idx;
  886. at91_gpio->regbase = ioremap(regbase, 512);
  887. if (!at91_gpio->regbase) {
  888. pr_err("at91_gpio.%d, failed to map registers, ignoring.\n", idx);
  889. return;
  890. }
  891. if (at91_gpio_setup_clk(idx))
  892. goto ioremap_err;
  893. gpio_banks = max(gpio_banks, idx + 1);
  894. return;
  895. ioremap_err:
  896. iounmap(at91_gpio->regbase);
  897. }
  898. /*
  899. * Called from the processor-specific init to enable GPIO pin support.
  900. */
  901. void __init at91_gpio_init(struct at91_gpio_bank *data, int nr_banks)
  902. {
  903. unsigned i;
  904. struct at91_gpio_chip *at91_gpio, *last = NULL;
  905. BUG_ON(nr_banks > MAX_GPIO_BANKS);
  906. if (of_at91_gpio_init() < 0) {
  907. /* No GPIO controller found in device tree */
  908. for (i = 0; i < nr_banks; i++)
  909. at91_gpio_init_one(i, data[i].regbase, data[i].id);
  910. }
  911. for (i = 0; i < gpio_banks; i++) {
  912. at91_gpio = &gpio_chip[i];
  913. /*
  914. * GPIO controller are grouped on some SoC:
  915. * PIOC, PIOD and PIOE can share the same IRQ line
  916. */
  917. if (last && last->pioc_hwirq == at91_gpio->pioc_hwirq)
  918. last->next = at91_gpio;
  919. last = at91_gpio;
  920. gpiochip_add(&at91_gpio->chip);
  921. }
  922. }