gpio.c 28 KB

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