gpio.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862
  1. /*
  2. * linux/arch/arm/plat-omap/gpio.c
  3. *
  4. * Support functions for OMAP GPIO
  5. *
  6. * Copyright (C) 2003-2005 Nokia Corporation
  7. * Written by Juha Yrjölä <juha.yrjola@nokia.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/init.h>
  14. #include <linux/module.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/sysdev.h>
  17. #include <linux/err.h>
  18. #include <linux/clk.h>
  19. #include <linux/io.h>
  20. #include <mach/hardware.h>
  21. #include <asm/irq.h>
  22. #include <mach/irqs.h>
  23. #include <mach/gpio.h>
  24. #include <asm/mach/irq.h>
  25. /*
  26. * OMAP1510 GPIO registers
  27. */
  28. #define OMAP1510_GPIO_BASE IO_ADDRESS(0xfffce000)
  29. #define OMAP1510_GPIO_DATA_INPUT 0x00
  30. #define OMAP1510_GPIO_DATA_OUTPUT 0x04
  31. #define OMAP1510_GPIO_DIR_CONTROL 0x08
  32. #define OMAP1510_GPIO_INT_CONTROL 0x0c
  33. #define OMAP1510_GPIO_INT_MASK 0x10
  34. #define OMAP1510_GPIO_INT_STATUS 0x14
  35. #define OMAP1510_GPIO_PIN_CONTROL 0x18
  36. #define OMAP1510_IH_GPIO_BASE 64
  37. /*
  38. * OMAP1610 specific GPIO registers
  39. */
  40. #define OMAP1610_GPIO1_BASE IO_ADDRESS(0xfffbe400)
  41. #define OMAP1610_GPIO2_BASE IO_ADDRESS(0xfffbec00)
  42. #define OMAP1610_GPIO3_BASE IO_ADDRESS(0xfffbb400)
  43. #define OMAP1610_GPIO4_BASE IO_ADDRESS(0xfffbbc00)
  44. #define OMAP1610_GPIO_REVISION 0x0000
  45. #define OMAP1610_GPIO_SYSCONFIG 0x0010
  46. #define OMAP1610_GPIO_SYSSTATUS 0x0014
  47. #define OMAP1610_GPIO_IRQSTATUS1 0x0018
  48. #define OMAP1610_GPIO_IRQENABLE1 0x001c
  49. #define OMAP1610_GPIO_WAKEUPENABLE 0x0028
  50. #define OMAP1610_GPIO_DATAIN 0x002c
  51. #define OMAP1610_GPIO_DATAOUT 0x0030
  52. #define OMAP1610_GPIO_DIRECTION 0x0034
  53. #define OMAP1610_GPIO_EDGE_CTRL1 0x0038
  54. #define OMAP1610_GPIO_EDGE_CTRL2 0x003c
  55. #define OMAP1610_GPIO_CLEAR_IRQENABLE1 0x009c
  56. #define OMAP1610_GPIO_CLEAR_WAKEUPENA 0x00a8
  57. #define OMAP1610_GPIO_CLEAR_DATAOUT 0x00b0
  58. #define OMAP1610_GPIO_SET_IRQENABLE1 0x00dc
  59. #define OMAP1610_GPIO_SET_WAKEUPENA 0x00e8
  60. #define OMAP1610_GPIO_SET_DATAOUT 0x00f0
  61. /*
  62. * OMAP730 specific GPIO registers
  63. */
  64. #define OMAP730_GPIO1_BASE IO_ADDRESS(0xfffbc000)
  65. #define OMAP730_GPIO2_BASE IO_ADDRESS(0xfffbc800)
  66. #define OMAP730_GPIO3_BASE IO_ADDRESS(0xfffbd000)
  67. #define OMAP730_GPIO4_BASE IO_ADDRESS(0xfffbd800)
  68. #define OMAP730_GPIO5_BASE IO_ADDRESS(0xfffbe000)
  69. #define OMAP730_GPIO6_BASE IO_ADDRESS(0xfffbe800)
  70. #define OMAP730_GPIO_DATA_INPUT 0x00
  71. #define OMAP730_GPIO_DATA_OUTPUT 0x04
  72. #define OMAP730_GPIO_DIR_CONTROL 0x08
  73. #define OMAP730_GPIO_INT_CONTROL 0x0c
  74. #define OMAP730_GPIO_INT_MASK 0x10
  75. #define OMAP730_GPIO_INT_STATUS 0x14
  76. /*
  77. * omap24xx specific GPIO registers
  78. */
  79. #define OMAP242X_GPIO1_BASE IO_ADDRESS(0x48018000)
  80. #define OMAP242X_GPIO2_BASE IO_ADDRESS(0x4801a000)
  81. #define OMAP242X_GPIO3_BASE IO_ADDRESS(0x4801c000)
  82. #define OMAP242X_GPIO4_BASE IO_ADDRESS(0x4801e000)
  83. #define OMAP243X_GPIO1_BASE IO_ADDRESS(0x4900C000)
  84. #define OMAP243X_GPIO2_BASE IO_ADDRESS(0x4900E000)
  85. #define OMAP243X_GPIO3_BASE IO_ADDRESS(0x49010000)
  86. #define OMAP243X_GPIO4_BASE IO_ADDRESS(0x49012000)
  87. #define OMAP243X_GPIO5_BASE IO_ADDRESS(0x480B6000)
  88. #define OMAP24XX_GPIO_REVISION 0x0000
  89. #define OMAP24XX_GPIO_SYSCONFIG 0x0010
  90. #define OMAP24XX_GPIO_SYSSTATUS 0x0014
  91. #define OMAP24XX_GPIO_IRQSTATUS1 0x0018
  92. #define OMAP24XX_GPIO_IRQSTATUS2 0x0028
  93. #define OMAP24XX_GPIO_IRQENABLE2 0x002c
  94. #define OMAP24XX_GPIO_IRQENABLE1 0x001c
  95. #define OMAP24XX_GPIO_WAKE_EN 0x0020
  96. #define OMAP24XX_GPIO_CTRL 0x0030
  97. #define OMAP24XX_GPIO_OE 0x0034
  98. #define OMAP24XX_GPIO_DATAIN 0x0038
  99. #define OMAP24XX_GPIO_DATAOUT 0x003c
  100. #define OMAP24XX_GPIO_LEVELDETECT0 0x0040
  101. #define OMAP24XX_GPIO_LEVELDETECT1 0x0044
  102. #define OMAP24XX_GPIO_RISINGDETECT 0x0048
  103. #define OMAP24XX_GPIO_FALLINGDETECT 0x004c
  104. #define OMAP24XX_GPIO_DEBOUNCE_EN 0x0050
  105. #define OMAP24XX_GPIO_DEBOUNCE_VAL 0x0054
  106. #define OMAP24XX_GPIO_CLEARIRQENABLE1 0x0060
  107. #define OMAP24XX_GPIO_SETIRQENABLE1 0x0064
  108. #define OMAP24XX_GPIO_CLEARWKUENA 0x0080
  109. #define OMAP24XX_GPIO_SETWKUENA 0x0084
  110. #define OMAP24XX_GPIO_CLEARDATAOUT 0x0090
  111. #define OMAP24XX_GPIO_SETDATAOUT 0x0094
  112. /*
  113. * omap34xx specific GPIO registers
  114. */
  115. #define OMAP34XX_GPIO1_BASE IO_ADDRESS(0x48310000)
  116. #define OMAP34XX_GPIO2_BASE IO_ADDRESS(0x49050000)
  117. #define OMAP34XX_GPIO3_BASE IO_ADDRESS(0x49052000)
  118. #define OMAP34XX_GPIO4_BASE IO_ADDRESS(0x49054000)
  119. #define OMAP34XX_GPIO5_BASE IO_ADDRESS(0x49056000)
  120. #define OMAP34XX_GPIO6_BASE IO_ADDRESS(0x49058000)
  121. #define OMAP_MPUIO_VBASE IO_ADDRESS(OMAP_MPUIO_BASE)
  122. struct gpio_bank {
  123. void __iomem *base;
  124. u16 irq;
  125. u16 virtual_irq_start;
  126. int method;
  127. #if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
  128. u32 suspend_wakeup;
  129. u32 saved_wakeup;
  130. #endif
  131. #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
  132. u32 non_wakeup_gpios;
  133. u32 enabled_non_wakeup_gpios;
  134. u32 saved_datain;
  135. u32 saved_fallingdetect;
  136. u32 saved_risingdetect;
  137. #endif
  138. u32 level_mask;
  139. spinlock_t lock;
  140. struct gpio_chip chip;
  141. struct clk *dbck;
  142. };
  143. #define METHOD_MPUIO 0
  144. #define METHOD_GPIO_1510 1
  145. #define METHOD_GPIO_1610 2
  146. #define METHOD_GPIO_730 3
  147. #define METHOD_GPIO_24XX 4
  148. #ifdef CONFIG_ARCH_OMAP16XX
  149. static struct gpio_bank gpio_bank_1610[5] = {
  150. { OMAP_MPUIO_VBASE, INT_MPUIO, IH_MPUIO_BASE, METHOD_MPUIO},
  151. { OMAP1610_GPIO1_BASE, INT_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_1610 },
  152. { OMAP1610_GPIO2_BASE, INT_1610_GPIO_BANK2, IH_GPIO_BASE + 16, METHOD_GPIO_1610 },
  153. { OMAP1610_GPIO3_BASE, INT_1610_GPIO_BANK3, IH_GPIO_BASE + 32, METHOD_GPIO_1610 },
  154. { OMAP1610_GPIO4_BASE, INT_1610_GPIO_BANK4, IH_GPIO_BASE + 48, METHOD_GPIO_1610 },
  155. };
  156. #endif
  157. #ifdef CONFIG_ARCH_OMAP15XX
  158. static struct gpio_bank gpio_bank_1510[2] = {
  159. { OMAP_MPUIO_VBASE, INT_MPUIO, IH_MPUIO_BASE, METHOD_MPUIO },
  160. { OMAP1510_GPIO_BASE, INT_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_1510 }
  161. };
  162. #endif
  163. #ifdef CONFIG_ARCH_OMAP730
  164. static struct gpio_bank gpio_bank_730[7] = {
  165. { OMAP_MPUIO_VBASE, INT_730_MPUIO, IH_MPUIO_BASE, METHOD_MPUIO },
  166. { OMAP730_GPIO1_BASE, INT_730_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_730 },
  167. { OMAP730_GPIO2_BASE, INT_730_GPIO_BANK2, IH_GPIO_BASE + 32, METHOD_GPIO_730 },
  168. { OMAP730_GPIO3_BASE, INT_730_GPIO_BANK3, IH_GPIO_BASE + 64, METHOD_GPIO_730 },
  169. { OMAP730_GPIO4_BASE, INT_730_GPIO_BANK4, IH_GPIO_BASE + 96, METHOD_GPIO_730 },
  170. { OMAP730_GPIO5_BASE, INT_730_GPIO_BANK5, IH_GPIO_BASE + 128, METHOD_GPIO_730 },
  171. { OMAP730_GPIO6_BASE, INT_730_GPIO_BANK6, IH_GPIO_BASE + 160, METHOD_GPIO_730 },
  172. };
  173. #endif
  174. #ifdef CONFIG_ARCH_OMAP24XX
  175. static struct gpio_bank gpio_bank_242x[4] = {
  176. { OMAP242X_GPIO1_BASE, INT_24XX_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_24XX },
  177. { OMAP242X_GPIO2_BASE, INT_24XX_GPIO_BANK2, IH_GPIO_BASE + 32, METHOD_GPIO_24XX },
  178. { OMAP242X_GPIO3_BASE, INT_24XX_GPIO_BANK3, IH_GPIO_BASE + 64, METHOD_GPIO_24XX },
  179. { OMAP242X_GPIO4_BASE, INT_24XX_GPIO_BANK4, IH_GPIO_BASE + 96, METHOD_GPIO_24XX },
  180. };
  181. static struct gpio_bank gpio_bank_243x[5] = {
  182. { OMAP243X_GPIO1_BASE, INT_24XX_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_24XX },
  183. { OMAP243X_GPIO2_BASE, INT_24XX_GPIO_BANK2, IH_GPIO_BASE + 32, METHOD_GPIO_24XX },
  184. { OMAP243X_GPIO3_BASE, INT_24XX_GPIO_BANK3, IH_GPIO_BASE + 64, METHOD_GPIO_24XX },
  185. { OMAP243X_GPIO4_BASE, INT_24XX_GPIO_BANK4, IH_GPIO_BASE + 96, METHOD_GPIO_24XX },
  186. { OMAP243X_GPIO5_BASE, INT_24XX_GPIO_BANK5, IH_GPIO_BASE + 128, METHOD_GPIO_24XX },
  187. };
  188. #endif
  189. #ifdef CONFIG_ARCH_OMAP34XX
  190. static struct gpio_bank gpio_bank_34xx[6] = {
  191. { OMAP34XX_GPIO1_BASE, INT_34XX_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_24XX },
  192. { OMAP34XX_GPIO2_BASE, INT_34XX_GPIO_BANK2, IH_GPIO_BASE + 32, METHOD_GPIO_24XX },
  193. { OMAP34XX_GPIO3_BASE, INT_34XX_GPIO_BANK3, IH_GPIO_BASE + 64, METHOD_GPIO_24XX },
  194. { OMAP34XX_GPIO4_BASE, INT_34XX_GPIO_BANK4, IH_GPIO_BASE + 96, METHOD_GPIO_24XX },
  195. { OMAP34XX_GPIO5_BASE, INT_34XX_GPIO_BANK5, IH_GPIO_BASE + 128, METHOD_GPIO_24XX },
  196. { OMAP34XX_GPIO6_BASE, INT_34XX_GPIO_BANK6, IH_GPIO_BASE + 160, METHOD_GPIO_24XX },
  197. };
  198. #endif
  199. static struct gpio_bank *gpio_bank;
  200. static int gpio_bank_count;
  201. static inline struct gpio_bank *get_gpio_bank(int gpio)
  202. {
  203. if (cpu_is_omap15xx()) {
  204. if (OMAP_GPIO_IS_MPUIO(gpio))
  205. return &gpio_bank[0];
  206. return &gpio_bank[1];
  207. }
  208. if (cpu_is_omap16xx()) {
  209. if (OMAP_GPIO_IS_MPUIO(gpio))
  210. return &gpio_bank[0];
  211. return &gpio_bank[1 + (gpio >> 4)];
  212. }
  213. if (cpu_is_omap730()) {
  214. if (OMAP_GPIO_IS_MPUIO(gpio))
  215. return &gpio_bank[0];
  216. return &gpio_bank[1 + (gpio >> 5)];
  217. }
  218. if (cpu_is_omap24xx())
  219. return &gpio_bank[gpio >> 5];
  220. if (cpu_is_omap34xx())
  221. return &gpio_bank[gpio >> 5];
  222. }
  223. static inline int get_gpio_index(int gpio)
  224. {
  225. if (cpu_is_omap730())
  226. return gpio & 0x1f;
  227. if (cpu_is_omap24xx())
  228. return gpio & 0x1f;
  229. if (cpu_is_omap34xx())
  230. return gpio & 0x1f;
  231. return gpio & 0x0f;
  232. }
  233. static inline int gpio_valid(int gpio)
  234. {
  235. if (gpio < 0)
  236. return -1;
  237. if (cpu_class_is_omap1() && OMAP_GPIO_IS_MPUIO(gpio)) {
  238. if (gpio >= OMAP_MAX_GPIO_LINES + 16)
  239. return -1;
  240. return 0;
  241. }
  242. if (cpu_is_omap15xx() && gpio < 16)
  243. return 0;
  244. if ((cpu_is_omap16xx()) && gpio < 64)
  245. return 0;
  246. if (cpu_is_omap730() && gpio < 192)
  247. return 0;
  248. if (cpu_is_omap24xx() && gpio < 128)
  249. return 0;
  250. if (cpu_is_omap34xx() && gpio < 160)
  251. return 0;
  252. return -1;
  253. }
  254. static int check_gpio(int gpio)
  255. {
  256. if (unlikely(gpio_valid(gpio)) < 0) {
  257. printk(KERN_ERR "omap-gpio: invalid GPIO %d\n", gpio);
  258. dump_stack();
  259. return -1;
  260. }
  261. return 0;
  262. }
  263. static void _set_gpio_direction(struct gpio_bank *bank, int gpio, int is_input)
  264. {
  265. void __iomem *reg = bank->base;
  266. u32 l;
  267. switch (bank->method) {
  268. #ifdef CONFIG_ARCH_OMAP1
  269. case METHOD_MPUIO:
  270. reg += OMAP_MPUIO_IO_CNTL;
  271. break;
  272. #endif
  273. #ifdef CONFIG_ARCH_OMAP15XX
  274. case METHOD_GPIO_1510:
  275. reg += OMAP1510_GPIO_DIR_CONTROL;
  276. break;
  277. #endif
  278. #ifdef CONFIG_ARCH_OMAP16XX
  279. case METHOD_GPIO_1610:
  280. reg += OMAP1610_GPIO_DIRECTION;
  281. break;
  282. #endif
  283. #ifdef CONFIG_ARCH_OMAP730
  284. case METHOD_GPIO_730:
  285. reg += OMAP730_GPIO_DIR_CONTROL;
  286. break;
  287. #endif
  288. #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
  289. case METHOD_GPIO_24XX:
  290. reg += OMAP24XX_GPIO_OE;
  291. break;
  292. #endif
  293. default:
  294. WARN_ON(1);
  295. return;
  296. }
  297. l = __raw_readl(reg);
  298. if (is_input)
  299. l |= 1 << gpio;
  300. else
  301. l &= ~(1 << gpio);
  302. __raw_writel(l, reg);
  303. }
  304. static void _set_gpio_dataout(struct gpio_bank *bank, int gpio, int enable)
  305. {
  306. void __iomem *reg = bank->base;
  307. u32 l = 0;
  308. switch (bank->method) {
  309. #ifdef CONFIG_ARCH_OMAP1
  310. case METHOD_MPUIO:
  311. reg += OMAP_MPUIO_OUTPUT;
  312. l = __raw_readl(reg);
  313. if (enable)
  314. l |= 1 << gpio;
  315. else
  316. l &= ~(1 << gpio);
  317. break;
  318. #endif
  319. #ifdef CONFIG_ARCH_OMAP15XX
  320. case METHOD_GPIO_1510:
  321. reg += OMAP1510_GPIO_DATA_OUTPUT;
  322. l = __raw_readl(reg);
  323. if (enable)
  324. l |= 1 << gpio;
  325. else
  326. l &= ~(1 << gpio);
  327. break;
  328. #endif
  329. #ifdef CONFIG_ARCH_OMAP16XX
  330. case METHOD_GPIO_1610:
  331. if (enable)
  332. reg += OMAP1610_GPIO_SET_DATAOUT;
  333. else
  334. reg += OMAP1610_GPIO_CLEAR_DATAOUT;
  335. l = 1 << gpio;
  336. break;
  337. #endif
  338. #ifdef CONFIG_ARCH_OMAP730
  339. case METHOD_GPIO_730:
  340. reg += OMAP730_GPIO_DATA_OUTPUT;
  341. l = __raw_readl(reg);
  342. if (enable)
  343. l |= 1 << gpio;
  344. else
  345. l &= ~(1 << gpio);
  346. break;
  347. #endif
  348. #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
  349. case METHOD_GPIO_24XX:
  350. if (enable)
  351. reg += OMAP24XX_GPIO_SETDATAOUT;
  352. else
  353. reg += OMAP24XX_GPIO_CLEARDATAOUT;
  354. l = 1 << gpio;
  355. break;
  356. #endif
  357. default:
  358. WARN_ON(1);
  359. return;
  360. }
  361. __raw_writel(l, reg);
  362. }
  363. static int __omap_get_gpio_datain(int gpio)
  364. {
  365. struct gpio_bank *bank;
  366. void __iomem *reg;
  367. if (check_gpio(gpio) < 0)
  368. return -EINVAL;
  369. bank = get_gpio_bank(gpio);
  370. reg = bank->base;
  371. switch (bank->method) {
  372. #ifdef CONFIG_ARCH_OMAP1
  373. case METHOD_MPUIO:
  374. reg += OMAP_MPUIO_INPUT_LATCH;
  375. break;
  376. #endif
  377. #ifdef CONFIG_ARCH_OMAP15XX
  378. case METHOD_GPIO_1510:
  379. reg += OMAP1510_GPIO_DATA_INPUT;
  380. break;
  381. #endif
  382. #ifdef CONFIG_ARCH_OMAP16XX
  383. case METHOD_GPIO_1610:
  384. reg += OMAP1610_GPIO_DATAIN;
  385. break;
  386. #endif
  387. #ifdef CONFIG_ARCH_OMAP730
  388. case METHOD_GPIO_730:
  389. reg += OMAP730_GPIO_DATA_INPUT;
  390. break;
  391. #endif
  392. #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
  393. case METHOD_GPIO_24XX:
  394. reg += OMAP24XX_GPIO_DATAIN;
  395. break;
  396. #endif
  397. default:
  398. return -EINVAL;
  399. }
  400. return (__raw_readl(reg)
  401. & (1 << get_gpio_index(gpio))) != 0;
  402. }
  403. #define MOD_REG_BIT(reg, bit_mask, set) \
  404. do { \
  405. int l = __raw_readl(base + reg); \
  406. if (set) l |= bit_mask; \
  407. else l &= ~bit_mask; \
  408. __raw_writel(l, base + reg); \
  409. } while(0)
  410. void omap_set_gpio_debounce(int gpio, int enable)
  411. {
  412. struct gpio_bank *bank;
  413. void __iomem *reg;
  414. u32 val, l = 1 << get_gpio_index(gpio);
  415. if (cpu_class_is_omap1())
  416. return;
  417. bank = get_gpio_bank(gpio);
  418. reg = bank->base;
  419. reg += OMAP24XX_GPIO_DEBOUNCE_EN;
  420. val = __raw_readl(reg);
  421. if (enable && !(val & l))
  422. val |= l;
  423. else if (!enable && val & l)
  424. val &= ~l;
  425. else
  426. return;
  427. if (cpu_is_omap34xx())
  428. enable ? clk_enable(bank->dbck) : clk_disable(bank->dbck);
  429. __raw_writel(val, reg);
  430. }
  431. EXPORT_SYMBOL(omap_set_gpio_debounce);
  432. void omap_set_gpio_debounce_time(int gpio, int enc_time)
  433. {
  434. struct gpio_bank *bank;
  435. void __iomem *reg;
  436. if (cpu_class_is_omap1())
  437. return;
  438. bank = get_gpio_bank(gpio);
  439. reg = bank->base;
  440. enc_time &= 0xff;
  441. reg += OMAP24XX_GPIO_DEBOUNCE_VAL;
  442. __raw_writel(enc_time, reg);
  443. }
  444. EXPORT_SYMBOL(omap_set_gpio_debounce_time);
  445. #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
  446. static inline void set_24xx_gpio_triggering(struct gpio_bank *bank, int gpio,
  447. int trigger)
  448. {
  449. void __iomem *base = bank->base;
  450. u32 gpio_bit = 1 << gpio;
  451. MOD_REG_BIT(OMAP24XX_GPIO_LEVELDETECT0, gpio_bit,
  452. trigger & IRQ_TYPE_LEVEL_LOW);
  453. MOD_REG_BIT(OMAP24XX_GPIO_LEVELDETECT1, gpio_bit,
  454. trigger & IRQ_TYPE_LEVEL_HIGH);
  455. MOD_REG_BIT(OMAP24XX_GPIO_RISINGDETECT, gpio_bit,
  456. trigger & IRQ_TYPE_EDGE_RISING);
  457. MOD_REG_BIT(OMAP24XX_GPIO_FALLINGDETECT, gpio_bit,
  458. trigger & IRQ_TYPE_EDGE_FALLING);
  459. if (likely(!(bank->non_wakeup_gpios & gpio_bit))) {
  460. if (trigger != 0)
  461. __raw_writel(1 << gpio, bank->base
  462. + OMAP24XX_GPIO_SETWKUENA);
  463. else
  464. __raw_writel(1 << gpio, bank->base
  465. + OMAP24XX_GPIO_CLEARWKUENA);
  466. } else {
  467. if (trigger != 0)
  468. bank->enabled_non_wakeup_gpios |= gpio_bit;
  469. else
  470. bank->enabled_non_wakeup_gpios &= ~gpio_bit;
  471. }
  472. bank->level_mask =
  473. __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT0) |
  474. __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT1);
  475. }
  476. #endif
  477. static int _set_gpio_triggering(struct gpio_bank *bank, int gpio, int trigger)
  478. {
  479. void __iomem *reg = bank->base;
  480. u32 l = 0;
  481. switch (bank->method) {
  482. #ifdef CONFIG_ARCH_OMAP1
  483. case METHOD_MPUIO:
  484. reg += OMAP_MPUIO_GPIO_INT_EDGE;
  485. l = __raw_readl(reg);
  486. if (trigger & IRQ_TYPE_EDGE_RISING)
  487. l |= 1 << gpio;
  488. else if (trigger & IRQ_TYPE_EDGE_FALLING)
  489. l &= ~(1 << gpio);
  490. else
  491. goto bad;
  492. break;
  493. #endif
  494. #ifdef CONFIG_ARCH_OMAP15XX
  495. case METHOD_GPIO_1510:
  496. reg += OMAP1510_GPIO_INT_CONTROL;
  497. l = __raw_readl(reg);
  498. if (trigger & IRQ_TYPE_EDGE_RISING)
  499. l |= 1 << gpio;
  500. else if (trigger & IRQ_TYPE_EDGE_FALLING)
  501. l &= ~(1 << gpio);
  502. else
  503. goto bad;
  504. break;
  505. #endif
  506. #ifdef CONFIG_ARCH_OMAP16XX
  507. case METHOD_GPIO_1610:
  508. if (gpio & 0x08)
  509. reg += OMAP1610_GPIO_EDGE_CTRL2;
  510. else
  511. reg += OMAP1610_GPIO_EDGE_CTRL1;
  512. gpio &= 0x07;
  513. l = __raw_readl(reg);
  514. l &= ~(3 << (gpio << 1));
  515. if (trigger & IRQ_TYPE_EDGE_RISING)
  516. l |= 2 << (gpio << 1);
  517. if (trigger & IRQ_TYPE_EDGE_FALLING)
  518. l |= 1 << (gpio << 1);
  519. if (trigger)
  520. /* Enable wake-up during idle for dynamic tick */
  521. __raw_writel(1 << gpio, bank->base + OMAP1610_GPIO_SET_WAKEUPENA);
  522. else
  523. __raw_writel(1 << gpio, bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA);
  524. break;
  525. #endif
  526. #ifdef CONFIG_ARCH_OMAP730
  527. case METHOD_GPIO_730:
  528. reg += OMAP730_GPIO_INT_CONTROL;
  529. l = __raw_readl(reg);
  530. if (trigger & IRQ_TYPE_EDGE_RISING)
  531. l |= 1 << gpio;
  532. else if (trigger & IRQ_TYPE_EDGE_FALLING)
  533. l &= ~(1 << gpio);
  534. else
  535. goto bad;
  536. break;
  537. #endif
  538. #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
  539. case METHOD_GPIO_24XX:
  540. set_24xx_gpio_triggering(bank, gpio, trigger);
  541. break;
  542. #endif
  543. default:
  544. goto bad;
  545. }
  546. __raw_writel(l, reg);
  547. return 0;
  548. bad:
  549. return -EINVAL;
  550. }
  551. static int gpio_irq_type(unsigned irq, unsigned type)
  552. {
  553. struct gpio_bank *bank;
  554. unsigned gpio;
  555. int retval;
  556. unsigned long flags;
  557. if (!cpu_class_is_omap2() && irq > IH_MPUIO_BASE)
  558. gpio = OMAP_MPUIO(irq - IH_MPUIO_BASE);
  559. else
  560. gpio = irq - IH_GPIO_BASE;
  561. if (check_gpio(gpio) < 0)
  562. return -EINVAL;
  563. if (type & ~IRQ_TYPE_SENSE_MASK)
  564. return -EINVAL;
  565. /* OMAP1 allows only only edge triggering */
  566. if (!cpu_class_is_omap2()
  567. && (type & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH)))
  568. return -EINVAL;
  569. bank = get_irq_chip_data(irq);
  570. spin_lock_irqsave(&bank->lock, flags);
  571. retval = _set_gpio_triggering(bank, get_gpio_index(gpio), type);
  572. if (retval == 0) {
  573. irq_desc[irq].status &= ~IRQ_TYPE_SENSE_MASK;
  574. irq_desc[irq].status |= type;
  575. }
  576. spin_unlock_irqrestore(&bank->lock, flags);
  577. if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
  578. __set_irq_handler_unlocked(irq, handle_level_irq);
  579. else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
  580. __set_irq_handler_unlocked(irq, handle_edge_irq);
  581. return retval;
  582. }
  583. static void _clear_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
  584. {
  585. void __iomem *reg = bank->base;
  586. switch (bank->method) {
  587. #ifdef CONFIG_ARCH_OMAP1
  588. case METHOD_MPUIO:
  589. /* MPUIO irqstatus is reset by reading the status register,
  590. * so do nothing here */
  591. return;
  592. #endif
  593. #ifdef CONFIG_ARCH_OMAP15XX
  594. case METHOD_GPIO_1510:
  595. reg += OMAP1510_GPIO_INT_STATUS;
  596. break;
  597. #endif
  598. #ifdef CONFIG_ARCH_OMAP16XX
  599. case METHOD_GPIO_1610:
  600. reg += OMAP1610_GPIO_IRQSTATUS1;
  601. break;
  602. #endif
  603. #ifdef CONFIG_ARCH_OMAP730
  604. case METHOD_GPIO_730:
  605. reg += OMAP730_GPIO_INT_STATUS;
  606. break;
  607. #endif
  608. #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
  609. case METHOD_GPIO_24XX:
  610. reg += OMAP24XX_GPIO_IRQSTATUS1;
  611. break;
  612. #endif
  613. default:
  614. WARN_ON(1);
  615. return;
  616. }
  617. __raw_writel(gpio_mask, reg);
  618. /* Workaround for clearing DSP GPIO interrupts to allow retention */
  619. #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
  620. if (cpu_is_omap24xx() || cpu_is_omap34xx())
  621. __raw_writel(gpio_mask, bank->base + OMAP24XX_GPIO_IRQSTATUS2);
  622. #endif
  623. }
  624. static inline void _clear_gpio_irqstatus(struct gpio_bank *bank, int gpio)
  625. {
  626. _clear_gpio_irqbank(bank, 1 << get_gpio_index(gpio));
  627. }
  628. static u32 _get_gpio_irqbank_mask(struct gpio_bank *bank)
  629. {
  630. void __iomem *reg = bank->base;
  631. int inv = 0;
  632. u32 l;
  633. u32 mask;
  634. switch (bank->method) {
  635. #ifdef CONFIG_ARCH_OMAP1
  636. case METHOD_MPUIO:
  637. reg += OMAP_MPUIO_GPIO_MASKIT;
  638. mask = 0xffff;
  639. inv = 1;
  640. break;
  641. #endif
  642. #ifdef CONFIG_ARCH_OMAP15XX
  643. case METHOD_GPIO_1510:
  644. reg += OMAP1510_GPIO_INT_MASK;
  645. mask = 0xffff;
  646. inv = 1;
  647. break;
  648. #endif
  649. #ifdef CONFIG_ARCH_OMAP16XX
  650. case METHOD_GPIO_1610:
  651. reg += OMAP1610_GPIO_IRQENABLE1;
  652. mask = 0xffff;
  653. break;
  654. #endif
  655. #ifdef CONFIG_ARCH_OMAP730
  656. case METHOD_GPIO_730:
  657. reg += OMAP730_GPIO_INT_MASK;
  658. mask = 0xffffffff;
  659. inv = 1;
  660. break;
  661. #endif
  662. #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
  663. case METHOD_GPIO_24XX:
  664. reg += OMAP24XX_GPIO_IRQENABLE1;
  665. mask = 0xffffffff;
  666. break;
  667. #endif
  668. default:
  669. WARN_ON(1);
  670. return 0;
  671. }
  672. l = __raw_readl(reg);
  673. if (inv)
  674. l = ~l;
  675. l &= mask;
  676. return l;
  677. }
  678. static void _enable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask, int enable)
  679. {
  680. void __iomem *reg = bank->base;
  681. u32 l;
  682. switch (bank->method) {
  683. #ifdef CONFIG_ARCH_OMAP1
  684. case METHOD_MPUIO:
  685. reg += OMAP_MPUIO_GPIO_MASKIT;
  686. l = __raw_readl(reg);
  687. if (enable)
  688. l &= ~(gpio_mask);
  689. else
  690. l |= gpio_mask;
  691. break;
  692. #endif
  693. #ifdef CONFIG_ARCH_OMAP15XX
  694. case METHOD_GPIO_1510:
  695. reg += OMAP1510_GPIO_INT_MASK;
  696. l = __raw_readl(reg);
  697. if (enable)
  698. l &= ~(gpio_mask);
  699. else
  700. l |= gpio_mask;
  701. break;
  702. #endif
  703. #ifdef CONFIG_ARCH_OMAP16XX
  704. case METHOD_GPIO_1610:
  705. if (enable)
  706. reg += OMAP1610_GPIO_SET_IRQENABLE1;
  707. else
  708. reg += OMAP1610_GPIO_CLEAR_IRQENABLE1;
  709. l = gpio_mask;
  710. break;
  711. #endif
  712. #ifdef CONFIG_ARCH_OMAP730
  713. case METHOD_GPIO_730:
  714. reg += OMAP730_GPIO_INT_MASK;
  715. l = __raw_readl(reg);
  716. if (enable)
  717. l &= ~(gpio_mask);
  718. else
  719. l |= gpio_mask;
  720. break;
  721. #endif
  722. #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
  723. case METHOD_GPIO_24XX:
  724. if (enable)
  725. reg += OMAP24XX_GPIO_SETIRQENABLE1;
  726. else
  727. reg += OMAP24XX_GPIO_CLEARIRQENABLE1;
  728. l = gpio_mask;
  729. break;
  730. #endif
  731. default:
  732. WARN_ON(1);
  733. return;
  734. }
  735. __raw_writel(l, reg);
  736. }
  737. static inline void _set_gpio_irqenable(struct gpio_bank *bank, int gpio, int enable)
  738. {
  739. _enable_gpio_irqbank(bank, 1 << get_gpio_index(gpio), enable);
  740. }
  741. /*
  742. * Note that ENAWAKEUP needs to be enabled in GPIO_SYSCONFIG register.
  743. * 1510 does not seem to have a wake-up register. If JTAG is connected
  744. * to the target, system will wake up always on GPIO events. While
  745. * system is running all registered GPIO interrupts need to have wake-up
  746. * enabled. When system is suspended, only selected GPIO interrupts need
  747. * to have wake-up enabled.
  748. */
  749. static int _set_gpio_wakeup(struct gpio_bank *bank, int gpio, int enable)
  750. {
  751. unsigned long flags;
  752. switch (bank->method) {
  753. #ifdef CONFIG_ARCH_OMAP16XX
  754. case METHOD_MPUIO:
  755. case METHOD_GPIO_1610:
  756. spin_lock_irqsave(&bank->lock, flags);
  757. if (enable) {
  758. bank->suspend_wakeup |= (1 << gpio);
  759. enable_irq_wake(bank->irq);
  760. } else {
  761. disable_irq_wake(bank->irq);
  762. bank->suspend_wakeup &= ~(1 << gpio);
  763. }
  764. spin_unlock_irqrestore(&bank->lock, flags);
  765. return 0;
  766. #endif
  767. #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
  768. case METHOD_GPIO_24XX:
  769. if (bank->non_wakeup_gpios & (1 << gpio)) {
  770. printk(KERN_ERR "Unable to modify wakeup on "
  771. "non-wakeup GPIO%d\n",
  772. (bank - gpio_bank) * 32 + gpio);
  773. return -EINVAL;
  774. }
  775. spin_lock_irqsave(&bank->lock, flags);
  776. if (enable) {
  777. bank->suspend_wakeup |= (1 << gpio);
  778. enable_irq_wake(bank->irq);
  779. } else {
  780. disable_irq_wake(bank->irq);
  781. bank->suspend_wakeup &= ~(1 << gpio);
  782. }
  783. spin_unlock_irqrestore(&bank->lock, flags);
  784. return 0;
  785. #endif
  786. default:
  787. printk(KERN_ERR "Can't enable GPIO wakeup for method %i\n",
  788. bank->method);
  789. return -EINVAL;
  790. }
  791. }
  792. static void _reset_gpio(struct gpio_bank *bank, int gpio)
  793. {
  794. _set_gpio_direction(bank, get_gpio_index(gpio), 1);
  795. _set_gpio_irqenable(bank, gpio, 0);
  796. _clear_gpio_irqstatus(bank, gpio);
  797. _set_gpio_triggering(bank, get_gpio_index(gpio), IRQ_TYPE_NONE);
  798. }
  799. /* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
  800. static int gpio_wake_enable(unsigned int irq, unsigned int enable)
  801. {
  802. unsigned int gpio = irq - IH_GPIO_BASE;
  803. struct gpio_bank *bank;
  804. int retval;
  805. if (check_gpio(gpio) < 0)
  806. return -ENODEV;
  807. bank = get_irq_chip_data(irq);
  808. retval = _set_gpio_wakeup(bank, get_gpio_index(gpio), enable);
  809. return retval;
  810. }
  811. int omap_request_gpio(int gpio)
  812. {
  813. struct gpio_bank *bank;
  814. unsigned long flags;
  815. int status;
  816. if (check_gpio(gpio) < 0)
  817. return -EINVAL;
  818. status = gpio_request(gpio, NULL);
  819. if (status < 0)
  820. return status;
  821. bank = get_gpio_bank(gpio);
  822. spin_lock_irqsave(&bank->lock, flags);
  823. /* Set trigger to none. You need to enable the desired trigger with
  824. * request_irq() or set_irq_type().
  825. */
  826. _set_gpio_triggering(bank, get_gpio_index(gpio), IRQ_TYPE_NONE);
  827. #ifdef CONFIG_ARCH_OMAP15XX
  828. if (bank->method == METHOD_GPIO_1510) {
  829. void __iomem *reg;
  830. /* Claim the pin for MPU */
  831. reg = bank->base + OMAP1510_GPIO_PIN_CONTROL;
  832. __raw_writel(__raw_readl(reg) | (1 << get_gpio_index(gpio)), reg);
  833. }
  834. #endif
  835. spin_unlock_irqrestore(&bank->lock, flags);
  836. return 0;
  837. }
  838. void omap_free_gpio(int gpio)
  839. {
  840. struct gpio_bank *bank;
  841. unsigned long flags;
  842. if (check_gpio(gpio) < 0)
  843. return;
  844. bank = get_gpio_bank(gpio);
  845. spin_lock_irqsave(&bank->lock, flags);
  846. if (unlikely(!gpiochip_is_requested(&bank->chip,
  847. get_gpio_index(gpio)))) {
  848. spin_unlock_irqrestore(&bank->lock, flags);
  849. printk(KERN_ERR "omap-gpio: GPIO %d wasn't reserved!\n", gpio);
  850. dump_stack();
  851. return;
  852. }
  853. #ifdef CONFIG_ARCH_OMAP16XX
  854. if (bank->method == METHOD_GPIO_1610) {
  855. /* Disable wake-up during idle for dynamic tick */
  856. void __iomem *reg = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
  857. __raw_writel(1 << get_gpio_index(gpio), reg);
  858. }
  859. #endif
  860. #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
  861. if (bank->method == METHOD_GPIO_24XX) {
  862. /* Disable wake-up during idle for dynamic tick */
  863. void __iomem *reg = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
  864. __raw_writel(1 << get_gpio_index(gpio), reg);
  865. }
  866. #endif
  867. _reset_gpio(bank, gpio);
  868. spin_unlock_irqrestore(&bank->lock, flags);
  869. gpio_free(gpio);
  870. }
  871. /*
  872. * We need to unmask the GPIO bank interrupt as soon as possible to
  873. * avoid missing GPIO interrupts for other lines in the bank.
  874. * Then we need to mask-read-clear-unmask the triggered GPIO lines
  875. * in the bank to avoid missing nested interrupts for a GPIO line.
  876. * If we wait to unmask individual GPIO lines in the bank after the
  877. * line's interrupt handler has been run, we may miss some nested
  878. * interrupts.
  879. */
  880. static void gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
  881. {
  882. void __iomem *isr_reg = NULL;
  883. u32 isr;
  884. unsigned int gpio_irq;
  885. struct gpio_bank *bank;
  886. u32 retrigger = 0;
  887. int unmasked = 0;
  888. desc->chip->ack(irq);
  889. bank = get_irq_data(irq);
  890. #ifdef CONFIG_ARCH_OMAP1
  891. if (bank->method == METHOD_MPUIO)
  892. isr_reg = bank->base + OMAP_MPUIO_GPIO_INT;
  893. #endif
  894. #ifdef CONFIG_ARCH_OMAP15XX
  895. if (bank->method == METHOD_GPIO_1510)
  896. isr_reg = bank->base + OMAP1510_GPIO_INT_STATUS;
  897. #endif
  898. #if defined(CONFIG_ARCH_OMAP16XX)
  899. if (bank->method == METHOD_GPIO_1610)
  900. isr_reg = bank->base + OMAP1610_GPIO_IRQSTATUS1;
  901. #endif
  902. #ifdef CONFIG_ARCH_OMAP730
  903. if (bank->method == METHOD_GPIO_730)
  904. isr_reg = bank->base + OMAP730_GPIO_INT_STATUS;
  905. #endif
  906. #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
  907. if (bank->method == METHOD_GPIO_24XX)
  908. isr_reg = bank->base + OMAP24XX_GPIO_IRQSTATUS1;
  909. #endif
  910. while(1) {
  911. u32 isr_saved, level_mask = 0;
  912. u32 enabled;
  913. enabled = _get_gpio_irqbank_mask(bank);
  914. isr_saved = isr = __raw_readl(isr_reg) & enabled;
  915. if (cpu_is_omap15xx() && (bank->method == METHOD_MPUIO))
  916. isr &= 0x0000ffff;
  917. if (cpu_class_is_omap2()) {
  918. level_mask = bank->level_mask & enabled;
  919. }
  920. /* clear edge sensitive interrupts before handler(s) are
  921. called so that we don't miss any interrupt occurred while
  922. executing them */
  923. _enable_gpio_irqbank(bank, isr_saved & ~level_mask, 0);
  924. _clear_gpio_irqbank(bank, isr_saved & ~level_mask);
  925. _enable_gpio_irqbank(bank, isr_saved & ~level_mask, 1);
  926. /* if there is only edge sensitive GPIO pin interrupts
  927. configured, we could unmask GPIO bank interrupt immediately */
  928. if (!level_mask && !unmasked) {
  929. unmasked = 1;
  930. desc->chip->unmask(irq);
  931. }
  932. isr |= retrigger;
  933. retrigger = 0;
  934. if (!isr)
  935. break;
  936. gpio_irq = bank->virtual_irq_start;
  937. for (; isr != 0; isr >>= 1, gpio_irq++) {
  938. if (!(isr & 1))
  939. continue;
  940. generic_handle_irq(gpio_irq);
  941. }
  942. }
  943. /* if bank has any level sensitive GPIO pin interrupt
  944. configured, we must unmask the bank interrupt only after
  945. handler(s) are executed in order to avoid spurious bank
  946. interrupt */
  947. if (!unmasked)
  948. desc->chip->unmask(irq);
  949. }
  950. static void gpio_irq_shutdown(unsigned int irq)
  951. {
  952. unsigned int gpio = irq - IH_GPIO_BASE;
  953. struct gpio_bank *bank = get_irq_chip_data(irq);
  954. _reset_gpio(bank, gpio);
  955. }
  956. static void gpio_ack_irq(unsigned int irq)
  957. {
  958. unsigned int gpio = irq - IH_GPIO_BASE;
  959. struct gpio_bank *bank = get_irq_chip_data(irq);
  960. _clear_gpio_irqstatus(bank, gpio);
  961. }
  962. static void gpio_mask_irq(unsigned int irq)
  963. {
  964. unsigned int gpio = irq - IH_GPIO_BASE;
  965. struct gpio_bank *bank = get_irq_chip_data(irq);
  966. _set_gpio_irqenable(bank, gpio, 0);
  967. }
  968. static void gpio_unmask_irq(unsigned int irq)
  969. {
  970. unsigned int gpio = irq - IH_GPIO_BASE;
  971. struct gpio_bank *bank = get_irq_chip_data(irq);
  972. unsigned int irq_mask = 1 << get_gpio_index(gpio);
  973. /* For level-triggered GPIOs, the clearing must be done after
  974. * the HW source is cleared, thus after the handler has run */
  975. if (bank->level_mask & irq_mask) {
  976. _set_gpio_irqenable(bank, gpio, 0);
  977. _clear_gpio_irqstatus(bank, gpio);
  978. }
  979. _set_gpio_irqenable(bank, gpio, 1);
  980. }
  981. static struct irq_chip gpio_irq_chip = {
  982. .name = "GPIO",
  983. .shutdown = gpio_irq_shutdown,
  984. .ack = gpio_ack_irq,
  985. .mask = gpio_mask_irq,
  986. .unmask = gpio_unmask_irq,
  987. .set_type = gpio_irq_type,
  988. .set_wake = gpio_wake_enable,
  989. };
  990. /*---------------------------------------------------------------------*/
  991. #ifdef CONFIG_ARCH_OMAP1
  992. /* MPUIO uses the always-on 32k clock */
  993. static void mpuio_ack_irq(unsigned int irq)
  994. {
  995. /* The ISR is reset automatically, so do nothing here. */
  996. }
  997. static void mpuio_mask_irq(unsigned int irq)
  998. {
  999. unsigned int gpio = OMAP_MPUIO(irq - IH_MPUIO_BASE);
  1000. struct gpio_bank *bank = get_irq_chip_data(irq);
  1001. _set_gpio_irqenable(bank, gpio, 0);
  1002. }
  1003. static void mpuio_unmask_irq(unsigned int irq)
  1004. {
  1005. unsigned int gpio = OMAP_MPUIO(irq - IH_MPUIO_BASE);
  1006. struct gpio_bank *bank = get_irq_chip_data(irq);
  1007. _set_gpio_irqenable(bank, gpio, 1);
  1008. }
  1009. static struct irq_chip mpuio_irq_chip = {
  1010. .name = "MPUIO",
  1011. .ack = mpuio_ack_irq,
  1012. .mask = mpuio_mask_irq,
  1013. .unmask = mpuio_unmask_irq,
  1014. .set_type = gpio_irq_type,
  1015. #ifdef CONFIG_ARCH_OMAP16XX
  1016. /* REVISIT: assuming only 16xx supports MPUIO wake events */
  1017. .set_wake = gpio_wake_enable,
  1018. #endif
  1019. };
  1020. #define bank_is_mpuio(bank) ((bank)->method == METHOD_MPUIO)
  1021. #ifdef CONFIG_ARCH_OMAP16XX
  1022. #include <linux/platform_device.h>
  1023. static int omap_mpuio_suspend_late(struct platform_device *pdev, pm_message_t mesg)
  1024. {
  1025. struct gpio_bank *bank = platform_get_drvdata(pdev);
  1026. void __iomem *mask_reg = bank->base + OMAP_MPUIO_GPIO_MASKIT;
  1027. unsigned long flags;
  1028. spin_lock_irqsave(&bank->lock, flags);
  1029. bank->saved_wakeup = __raw_readl(mask_reg);
  1030. __raw_writel(0xffff & ~bank->suspend_wakeup, mask_reg);
  1031. spin_unlock_irqrestore(&bank->lock, flags);
  1032. return 0;
  1033. }
  1034. static int omap_mpuio_resume_early(struct platform_device *pdev)
  1035. {
  1036. struct gpio_bank *bank = platform_get_drvdata(pdev);
  1037. void __iomem *mask_reg = bank->base + OMAP_MPUIO_GPIO_MASKIT;
  1038. unsigned long flags;
  1039. spin_lock_irqsave(&bank->lock, flags);
  1040. __raw_writel(bank->saved_wakeup, mask_reg);
  1041. spin_unlock_irqrestore(&bank->lock, flags);
  1042. return 0;
  1043. }
  1044. /* use platform_driver for this, now that there's no longer any
  1045. * point to sys_device (other than not disturbing old code).
  1046. */
  1047. static struct platform_driver omap_mpuio_driver = {
  1048. .suspend_late = omap_mpuio_suspend_late,
  1049. .resume_early = omap_mpuio_resume_early,
  1050. .driver = {
  1051. .name = "mpuio",
  1052. },
  1053. };
  1054. static struct platform_device omap_mpuio_device = {
  1055. .name = "mpuio",
  1056. .id = -1,
  1057. .dev = {
  1058. .driver = &omap_mpuio_driver.driver,
  1059. }
  1060. /* could list the /proc/iomem resources */
  1061. };
  1062. static inline void mpuio_init(void)
  1063. {
  1064. platform_set_drvdata(&omap_mpuio_device, &gpio_bank_1610[0]);
  1065. if (platform_driver_register(&omap_mpuio_driver) == 0)
  1066. (void) platform_device_register(&omap_mpuio_device);
  1067. }
  1068. #else
  1069. static inline void mpuio_init(void) {}
  1070. #endif /* 16xx */
  1071. #else
  1072. extern struct irq_chip mpuio_irq_chip;
  1073. #define bank_is_mpuio(bank) 0
  1074. static inline void mpuio_init(void) {}
  1075. #endif
  1076. /*---------------------------------------------------------------------*/
  1077. /* REVISIT these are stupid implementations! replace by ones that
  1078. * don't switch on METHOD_* and which mostly avoid spinlocks
  1079. */
  1080. static int gpio_input(struct gpio_chip *chip, unsigned offset)
  1081. {
  1082. struct gpio_bank *bank;
  1083. unsigned long flags;
  1084. bank = container_of(chip, struct gpio_bank, chip);
  1085. spin_lock_irqsave(&bank->lock, flags);
  1086. _set_gpio_direction(bank, offset, 1);
  1087. spin_unlock_irqrestore(&bank->lock, flags);
  1088. return 0;
  1089. }
  1090. static int gpio_get(struct gpio_chip *chip, unsigned offset)
  1091. {
  1092. return __omap_get_gpio_datain(chip->base + offset);
  1093. }
  1094. static int gpio_output(struct gpio_chip *chip, unsigned offset, int value)
  1095. {
  1096. struct gpio_bank *bank;
  1097. unsigned long flags;
  1098. bank = container_of(chip, struct gpio_bank, chip);
  1099. spin_lock_irqsave(&bank->lock, flags);
  1100. _set_gpio_dataout(bank, offset, value);
  1101. _set_gpio_direction(bank, offset, 0);
  1102. spin_unlock_irqrestore(&bank->lock, flags);
  1103. return 0;
  1104. }
  1105. static void gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  1106. {
  1107. struct gpio_bank *bank;
  1108. unsigned long flags;
  1109. bank = container_of(chip, struct gpio_bank, chip);
  1110. spin_lock_irqsave(&bank->lock, flags);
  1111. _set_gpio_dataout(bank, offset, value);
  1112. spin_unlock_irqrestore(&bank->lock, flags);
  1113. }
  1114. static int gpio_2irq(struct gpio_chip *chip, unsigned offset)
  1115. {
  1116. struct gpio_bank *bank;
  1117. bank = container_of(chip, struct gpio_bank, chip);
  1118. return bank->virtual_irq_start + offset;
  1119. }
  1120. /*---------------------------------------------------------------------*/
  1121. static int initialized;
  1122. #if !defined(CONFIG_ARCH_OMAP3)
  1123. static struct clk * gpio_ick;
  1124. #endif
  1125. #if defined(CONFIG_ARCH_OMAP2)
  1126. static struct clk * gpio_fck;
  1127. #endif
  1128. #if defined(CONFIG_ARCH_OMAP2430)
  1129. static struct clk * gpio5_ick;
  1130. static struct clk * gpio5_fck;
  1131. #endif
  1132. #if defined(CONFIG_ARCH_OMAP3)
  1133. static struct clk *gpio_iclks[OMAP34XX_NR_GPIOS];
  1134. #endif
  1135. /* This lock class tells lockdep that GPIO irqs are in a different
  1136. * category than their parents, so it won't report false recursion.
  1137. */
  1138. static struct lock_class_key gpio_lock_class;
  1139. static int __init _omap_gpio_init(void)
  1140. {
  1141. int i;
  1142. int gpio = 0;
  1143. struct gpio_bank *bank;
  1144. char clk_name[11];
  1145. initialized = 1;
  1146. #if defined(CONFIG_ARCH_OMAP1)
  1147. if (cpu_is_omap15xx()) {
  1148. gpio_ick = clk_get(NULL, "arm_gpio_ck");
  1149. if (IS_ERR(gpio_ick))
  1150. printk("Could not get arm_gpio_ck\n");
  1151. else
  1152. clk_enable(gpio_ick);
  1153. }
  1154. #endif
  1155. #if defined(CONFIG_ARCH_OMAP2)
  1156. if (cpu_class_is_omap2()) {
  1157. gpio_ick = clk_get(NULL, "gpios_ick");
  1158. if (IS_ERR(gpio_ick))
  1159. printk("Could not get gpios_ick\n");
  1160. else
  1161. clk_enable(gpio_ick);
  1162. gpio_fck = clk_get(NULL, "gpios_fck");
  1163. if (IS_ERR(gpio_fck))
  1164. printk("Could not get gpios_fck\n");
  1165. else
  1166. clk_enable(gpio_fck);
  1167. /*
  1168. * On 2430 & 3430 GPIO 5 uses CORE L4 ICLK
  1169. */
  1170. #if defined(CONFIG_ARCH_OMAP2430)
  1171. if (cpu_is_omap2430()) {
  1172. gpio5_ick = clk_get(NULL, "gpio5_ick");
  1173. if (IS_ERR(gpio5_ick))
  1174. printk("Could not get gpio5_ick\n");
  1175. else
  1176. clk_enable(gpio5_ick);
  1177. gpio5_fck = clk_get(NULL, "gpio5_fck");
  1178. if (IS_ERR(gpio5_fck))
  1179. printk("Could not get gpio5_fck\n");
  1180. else
  1181. clk_enable(gpio5_fck);
  1182. }
  1183. #endif
  1184. }
  1185. #endif
  1186. #if defined(CONFIG_ARCH_OMAP3)
  1187. if (cpu_is_omap34xx()) {
  1188. for (i = 0; i < OMAP34XX_NR_GPIOS; i++) {
  1189. sprintf(clk_name, "gpio%d_ick", i + 1);
  1190. gpio_iclks[i] = clk_get(NULL, clk_name);
  1191. if (IS_ERR(gpio_iclks[i]))
  1192. printk(KERN_ERR "Could not get %s\n", clk_name);
  1193. else
  1194. clk_enable(gpio_iclks[i]);
  1195. }
  1196. }
  1197. #endif
  1198. #ifdef CONFIG_ARCH_OMAP15XX
  1199. if (cpu_is_omap15xx()) {
  1200. printk(KERN_INFO "OMAP1510 GPIO hardware\n");
  1201. gpio_bank_count = 2;
  1202. gpio_bank = gpio_bank_1510;
  1203. }
  1204. #endif
  1205. #if defined(CONFIG_ARCH_OMAP16XX)
  1206. if (cpu_is_omap16xx()) {
  1207. u32 rev;
  1208. gpio_bank_count = 5;
  1209. gpio_bank = gpio_bank_1610;
  1210. rev = __raw_readw(gpio_bank[1].base + OMAP1610_GPIO_REVISION);
  1211. printk(KERN_INFO "OMAP GPIO hardware version %d.%d\n",
  1212. (rev >> 4) & 0x0f, rev & 0x0f);
  1213. }
  1214. #endif
  1215. #ifdef CONFIG_ARCH_OMAP730
  1216. if (cpu_is_omap730()) {
  1217. printk(KERN_INFO "OMAP730 GPIO hardware\n");
  1218. gpio_bank_count = 7;
  1219. gpio_bank = gpio_bank_730;
  1220. }
  1221. #endif
  1222. #ifdef CONFIG_ARCH_OMAP24XX
  1223. if (cpu_is_omap242x()) {
  1224. int rev;
  1225. gpio_bank_count = 4;
  1226. gpio_bank = gpio_bank_242x;
  1227. rev = __raw_readl(gpio_bank[0].base + OMAP24XX_GPIO_REVISION);
  1228. printk(KERN_INFO "OMAP242x GPIO hardware version %d.%d\n",
  1229. (rev >> 4) & 0x0f, rev & 0x0f);
  1230. }
  1231. if (cpu_is_omap243x()) {
  1232. int rev;
  1233. gpio_bank_count = 5;
  1234. gpio_bank = gpio_bank_243x;
  1235. rev = __raw_readl(gpio_bank[0].base + OMAP24XX_GPIO_REVISION);
  1236. printk(KERN_INFO "OMAP243x GPIO hardware version %d.%d\n",
  1237. (rev >> 4) & 0x0f, rev & 0x0f);
  1238. }
  1239. #endif
  1240. #ifdef CONFIG_ARCH_OMAP34XX
  1241. if (cpu_is_omap34xx()) {
  1242. int rev;
  1243. gpio_bank_count = OMAP34XX_NR_GPIOS;
  1244. gpio_bank = gpio_bank_34xx;
  1245. rev = __raw_readl(gpio_bank[0].base + OMAP24XX_GPIO_REVISION);
  1246. printk(KERN_INFO "OMAP34xx GPIO hardware version %d.%d\n",
  1247. (rev >> 4) & 0x0f, rev & 0x0f);
  1248. }
  1249. #endif
  1250. for (i = 0; i < gpio_bank_count; i++) {
  1251. int j, gpio_count = 16;
  1252. bank = &gpio_bank[i];
  1253. spin_lock_init(&bank->lock);
  1254. if (bank_is_mpuio(bank))
  1255. __raw_writew(0xffff, bank->base + OMAP_MPUIO_GPIO_MASKIT);
  1256. if (cpu_is_omap15xx() && bank->method == METHOD_GPIO_1510) {
  1257. __raw_writew(0xffff, bank->base + OMAP1510_GPIO_INT_MASK);
  1258. __raw_writew(0x0000, bank->base + OMAP1510_GPIO_INT_STATUS);
  1259. }
  1260. if (cpu_is_omap16xx() && bank->method == METHOD_GPIO_1610) {
  1261. __raw_writew(0x0000, bank->base + OMAP1610_GPIO_IRQENABLE1);
  1262. __raw_writew(0xffff, bank->base + OMAP1610_GPIO_IRQSTATUS1);
  1263. __raw_writew(0x0014, bank->base + OMAP1610_GPIO_SYSCONFIG);
  1264. }
  1265. if (cpu_is_omap730() && bank->method == METHOD_GPIO_730) {
  1266. __raw_writel(0xffffffff, bank->base + OMAP730_GPIO_INT_MASK);
  1267. __raw_writel(0x00000000, bank->base + OMAP730_GPIO_INT_STATUS);
  1268. gpio_count = 32; /* 730 has 32-bit GPIOs */
  1269. }
  1270. #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
  1271. if (bank->method == METHOD_GPIO_24XX) {
  1272. static const u32 non_wakeup_gpios[] = {
  1273. 0xe203ffc0, 0x08700040
  1274. };
  1275. __raw_writel(0x00000000, bank->base + OMAP24XX_GPIO_IRQENABLE1);
  1276. __raw_writel(0xffffffff, bank->base + OMAP24XX_GPIO_IRQSTATUS1);
  1277. __raw_writew(0x0015, bank->base + OMAP24XX_GPIO_SYSCONFIG);
  1278. /* Initialize interface clock ungated, module enabled */
  1279. __raw_writel(0, bank->base + OMAP24XX_GPIO_CTRL);
  1280. if (i < ARRAY_SIZE(non_wakeup_gpios))
  1281. bank->non_wakeup_gpios = non_wakeup_gpios[i];
  1282. gpio_count = 32;
  1283. }
  1284. #endif
  1285. /* REVISIT eventually switch from OMAP-specific gpio structs
  1286. * over to the generic ones
  1287. */
  1288. bank->chip.direction_input = gpio_input;
  1289. bank->chip.get = gpio_get;
  1290. bank->chip.direction_output = gpio_output;
  1291. bank->chip.set = gpio_set;
  1292. bank->chip.to_irq = gpio_2irq;
  1293. if (bank_is_mpuio(bank)) {
  1294. bank->chip.label = "mpuio";
  1295. #ifdef CONFIG_ARCH_OMAP16XX
  1296. bank->chip.dev = &omap_mpuio_device.dev;
  1297. #endif
  1298. bank->chip.base = OMAP_MPUIO(0);
  1299. } else {
  1300. bank->chip.label = "gpio";
  1301. bank->chip.base = gpio;
  1302. gpio += gpio_count;
  1303. }
  1304. bank->chip.ngpio = gpio_count;
  1305. gpiochip_add(&bank->chip);
  1306. for (j = bank->virtual_irq_start;
  1307. j < bank->virtual_irq_start + gpio_count; j++) {
  1308. lockdep_set_class(&irq_desc[j].lock, &gpio_lock_class);
  1309. set_irq_chip_data(j, bank);
  1310. if (bank_is_mpuio(bank))
  1311. set_irq_chip(j, &mpuio_irq_chip);
  1312. else
  1313. set_irq_chip(j, &gpio_irq_chip);
  1314. set_irq_handler(j, handle_simple_irq);
  1315. set_irq_flags(j, IRQF_VALID);
  1316. }
  1317. set_irq_chained_handler(bank->irq, gpio_irq_handler);
  1318. set_irq_data(bank->irq, bank);
  1319. if (cpu_is_omap34xx()) {
  1320. sprintf(clk_name, "gpio%d_dbck", i + 1);
  1321. bank->dbck = clk_get(NULL, clk_name);
  1322. if (IS_ERR(bank->dbck))
  1323. printk(KERN_ERR "Could not get %s\n", clk_name);
  1324. }
  1325. }
  1326. /* Enable system clock for GPIO module.
  1327. * The CAM_CLK_CTRL *is* really the right place. */
  1328. if (cpu_is_omap16xx())
  1329. omap_writel(omap_readl(ULPD_CAM_CLK_CTRL) | 0x04, ULPD_CAM_CLK_CTRL);
  1330. /* Enable autoidle for the OCP interface */
  1331. if (cpu_is_omap24xx())
  1332. omap_writel(1 << 0, 0x48019010);
  1333. if (cpu_is_omap34xx())
  1334. omap_writel(1 << 0, 0x48306814);
  1335. return 0;
  1336. }
  1337. #if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
  1338. static int omap_gpio_suspend(struct sys_device *dev, pm_message_t mesg)
  1339. {
  1340. int i;
  1341. if (!cpu_class_is_omap2() && !cpu_is_omap16xx())
  1342. return 0;
  1343. for (i = 0; i < gpio_bank_count; i++) {
  1344. struct gpio_bank *bank = &gpio_bank[i];
  1345. void __iomem *wake_status;
  1346. void __iomem *wake_clear;
  1347. void __iomem *wake_set;
  1348. unsigned long flags;
  1349. switch (bank->method) {
  1350. #ifdef CONFIG_ARCH_OMAP16XX
  1351. case METHOD_GPIO_1610:
  1352. wake_status = bank->base + OMAP1610_GPIO_WAKEUPENABLE;
  1353. wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
  1354. wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
  1355. break;
  1356. #endif
  1357. #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
  1358. case METHOD_GPIO_24XX:
  1359. wake_status = bank->base + OMAP24XX_GPIO_WAKE_EN;
  1360. wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
  1361. wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
  1362. break;
  1363. #endif
  1364. default:
  1365. continue;
  1366. }
  1367. spin_lock_irqsave(&bank->lock, flags);
  1368. bank->saved_wakeup = __raw_readl(wake_status);
  1369. __raw_writel(0xffffffff, wake_clear);
  1370. __raw_writel(bank->suspend_wakeup, wake_set);
  1371. spin_unlock_irqrestore(&bank->lock, flags);
  1372. }
  1373. return 0;
  1374. }
  1375. static int omap_gpio_resume(struct sys_device *dev)
  1376. {
  1377. int i;
  1378. if (!cpu_class_is_omap2() && !cpu_is_omap16xx())
  1379. return 0;
  1380. for (i = 0; i < gpio_bank_count; i++) {
  1381. struct gpio_bank *bank = &gpio_bank[i];
  1382. void __iomem *wake_clear;
  1383. void __iomem *wake_set;
  1384. unsigned long flags;
  1385. switch (bank->method) {
  1386. #ifdef CONFIG_ARCH_OMAP16XX
  1387. case METHOD_GPIO_1610:
  1388. wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
  1389. wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
  1390. break;
  1391. #endif
  1392. #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
  1393. case METHOD_GPIO_24XX:
  1394. wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
  1395. wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
  1396. break;
  1397. #endif
  1398. default:
  1399. continue;
  1400. }
  1401. spin_lock_irqsave(&bank->lock, flags);
  1402. __raw_writel(0xffffffff, wake_clear);
  1403. __raw_writel(bank->saved_wakeup, wake_set);
  1404. spin_unlock_irqrestore(&bank->lock, flags);
  1405. }
  1406. return 0;
  1407. }
  1408. static struct sysdev_class omap_gpio_sysclass = {
  1409. .name = "gpio",
  1410. .suspend = omap_gpio_suspend,
  1411. .resume = omap_gpio_resume,
  1412. };
  1413. static struct sys_device omap_gpio_device = {
  1414. .id = 0,
  1415. .cls = &omap_gpio_sysclass,
  1416. };
  1417. #endif
  1418. #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
  1419. static int workaround_enabled;
  1420. void omap2_gpio_prepare_for_retention(void)
  1421. {
  1422. int i, c = 0;
  1423. /* Remove triggering for all non-wakeup GPIOs. Otherwise spurious
  1424. * IRQs will be generated. See OMAP2420 Errata item 1.101. */
  1425. for (i = 0; i < gpio_bank_count; i++) {
  1426. struct gpio_bank *bank = &gpio_bank[i];
  1427. u32 l1, l2;
  1428. if (!(bank->enabled_non_wakeup_gpios))
  1429. continue;
  1430. #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
  1431. bank->saved_datain = __raw_readl(bank->base + OMAP24XX_GPIO_DATAIN);
  1432. l1 = __raw_readl(bank->base + OMAP24XX_GPIO_FALLINGDETECT);
  1433. l2 = __raw_readl(bank->base + OMAP24XX_GPIO_RISINGDETECT);
  1434. #endif
  1435. bank->saved_fallingdetect = l1;
  1436. bank->saved_risingdetect = l2;
  1437. l1 &= ~bank->enabled_non_wakeup_gpios;
  1438. l2 &= ~bank->enabled_non_wakeup_gpios;
  1439. #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
  1440. __raw_writel(l1, bank->base + OMAP24XX_GPIO_FALLINGDETECT);
  1441. __raw_writel(l2, bank->base + OMAP24XX_GPIO_RISINGDETECT);
  1442. #endif
  1443. c++;
  1444. }
  1445. if (!c) {
  1446. workaround_enabled = 0;
  1447. return;
  1448. }
  1449. workaround_enabled = 1;
  1450. }
  1451. void omap2_gpio_resume_after_retention(void)
  1452. {
  1453. int i;
  1454. if (!workaround_enabled)
  1455. return;
  1456. for (i = 0; i < gpio_bank_count; i++) {
  1457. struct gpio_bank *bank = &gpio_bank[i];
  1458. u32 l;
  1459. if (!(bank->enabled_non_wakeup_gpios))
  1460. continue;
  1461. #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
  1462. __raw_writel(bank->saved_fallingdetect,
  1463. bank->base + OMAP24XX_GPIO_FALLINGDETECT);
  1464. __raw_writel(bank->saved_risingdetect,
  1465. bank->base + OMAP24XX_GPIO_RISINGDETECT);
  1466. #endif
  1467. /* Check if any of the non-wakeup interrupt GPIOs have changed
  1468. * state. If so, generate an IRQ by software. This is
  1469. * horribly racy, but it's the best we can do to work around
  1470. * this silicon bug. */
  1471. #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
  1472. l = __raw_readl(bank->base + OMAP24XX_GPIO_DATAIN);
  1473. #endif
  1474. l ^= bank->saved_datain;
  1475. l &= bank->non_wakeup_gpios;
  1476. if (l) {
  1477. u32 old0, old1;
  1478. #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
  1479. old0 = __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT0);
  1480. old1 = __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT1);
  1481. __raw_writel(old0 | l, bank->base + OMAP24XX_GPIO_LEVELDETECT0);
  1482. __raw_writel(old1 | l, bank->base + OMAP24XX_GPIO_LEVELDETECT1);
  1483. __raw_writel(old0, bank->base + OMAP24XX_GPIO_LEVELDETECT0);
  1484. __raw_writel(old1, bank->base + OMAP24XX_GPIO_LEVELDETECT1);
  1485. #endif
  1486. }
  1487. }
  1488. }
  1489. #endif
  1490. /*
  1491. * This may get called early from board specific init
  1492. * for boards that have interrupts routed via FPGA.
  1493. */
  1494. int __init omap_gpio_init(void)
  1495. {
  1496. if (!initialized)
  1497. return _omap_gpio_init();
  1498. else
  1499. return 0;
  1500. }
  1501. static int __init omap_gpio_sysinit(void)
  1502. {
  1503. int ret = 0;
  1504. if (!initialized)
  1505. ret = _omap_gpio_init();
  1506. mpuio_init();
  1507. #if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
  1508. if (cpu_is_omap16xx() || cpu_class_is_omap2()) {
  1509. if (ret == 0) {
  1510. ret = sysdev_class_register(&omap_gpio_sysclass);
  1511. if (ret == 0)
  1512. ret = sysdev_register(&omap_gpio_device);
  1513. }
  1514. }
  1515. #endif
  1516. return ret;
  1517. }
  1518. EXPORT_SYMBOL(omap_request_gpio);
  1519. EXPORT_SYMBOL(omap_free_gpio);
  1520. arch_initcall(omap_gpio_sysinit);
  1521. #ifdef CONFIG_DEBUG_FS
  1522. #include <linux/debugfs.h>
  1523. #include <linux/seq_file.h>
  1524. static int gpio_is_input(struct gpio_bank *bank, int mask)
  1525. {
  1526. void __iomem *reg = bank->base;
  1527. switch (bank->method) {
  1528. case METHOD_MPUIO:
  1529. reg += OMAP_MPUIO_IO_CNTL;
  1530. break;
  1531. case METHOD_GPIO_1510:
  1532. reg += OMAP1510_GPIO_DIR_CONTROL;
  1533. break;
  1534. case METHOD_GPIO_1610:
  1535. reg += OMAP1610_GPIO_DIRECTION;
  1536. break;
  1537. case METHOD_GPIO_730:
  1538. reg += OMAP730_GPIO_DIR_CONTROL;
  1539. break;
  1540. case METHOD_GPIO_24XX:
  1541. reg += OMAP24XX_GPIO_OE;
  1542. break;
  1543. }
  1544. return __raw_readl(reg) & mask;
  1545. }
  1546. static int dbg_gpio_show(struct seq_file *s, void *unused)
  1547. {
  1548. unsigned i, j, gpio;
  1549. for (i = 0, gpio = 0; i < gpio_bank_count; i++) {
  1550. struct gpio_bank *bank = gpio_bank + i;
  1551. unsigned bankwidth = 16;
  1552. u32 mask = 1;
  1553. if (bank_is_mpuio(bank))
  1554. gpio = OMAP_MPUIO(0);
  1555. else if (cpu_class_is_omap2() || cpu_is_omap730())
  1556. bankwidth = 32;
  1557. for (j = 0; j < bankwidth; j++, gpio++, mask <<= 1) {
  1558. unsigned irq, value, is_in, irqstat;
  1559. const char *label;
  1560. label = gpiochip_is_requested(&bank->chip, j);
  1561. if (!label)
  1562. continue;
  1563. irq = bank->virtual_irq_start + j;
  1564. value = gpio_get_value(gpio);
  1565. is_in = gpio_is_input(bank, mask);
  1566. if (bank_is_mpuio(bank))
  1567. seq_printf(s, "MPUIO %2d ", j);
  1568. else
  1569. seq_printf(s, "GPIO %3d ", gpio);
  1570. seq_printf(s, "(%-20.20s): %s %s",
  1571. label,
  1572. is_in ? "in " : "out",
  1573. value ? "hi" : "lo");
  1574. /* FIXME for at least omap2, show pullup/pulldown state */
  1575. irqstat = irq_desc[irq].status;
  1576. if (is_in && ((bank->suspend_wakeup & mask)
  1577. || irqstat & IRQ_TYPE_SENSE_MASK)) {
  1578. char *trigger = NULL;
  1579. switch (irqstat & IRQ_TYPE_SENSE_MASK) {
  1580. case IRQ_TYPE_EDGE_FALLING:
  1581. trigger = "falling";
  1582. break;
  1583. case IRQ_TYPE_EDGE_RISING:
  1584. trigger = "rising";
  1585. break;
  1586. case IRQ_TYPE_EDGE_BOTH:
  1587. trigger = "bothedge";
  1588. break;
  1589. case IRQ_TYPE_LEVEL_LOW:
  1590. trigger = "low";
  1591. break;
  1592. case IRQ_TYPE_LEVEL_HIGH:
  1593. trigger = "high";
  1594. break;
  1595. case IRQ_TYPE_NONE:
  1596. trigger = "(?)";
  1597. break;
  1598. }
  1599. seq_printf(s, ", irq-%d %-8s%s",
  1600. irq, trigger,
  1601. (bank->suspend_wakeup & mask)
  1602. ? " wakeup" : "");
  1603. }
  1604. seq_printf(s, "\n");
  1605. }
  1606. if (bank_is_mpuio(bank)) {
  1607. seq_printf(s, "\n");
  1608. gpio = 0;
  1609. }
  1610. }
  1611. return 0;
  1612. }
  1613. static int dbg_gpio_open(struct inode *inode, struct file *file)
  1614. {
  1615. return single_open(file, dbg_gpio_show, &inode->i_private);
  1616. }
  1617. static const struct file_operations debug_fops = {
  1618. .open = dbg_gpio_open,
  1619. .read = seq_read,
  1620. .llseek = seq_lseek,
  1621. .release = single_release,
  1622. };
  1623. static int __init omap_gpio_debuginit(void)
  1624. {
  1625. (void) debugfs_create_file("omap_gpio", S_IRUGO,
  1626. NULL, NULL, &debug_fops);
  1627. return 0;
  1628. }
  1629. late_initcall(omap_gpio_debuginit);
  1630. #endif