gpio.c 28 KB

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