gpio-omap.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010
  1. /*
  2. * Support functions for OMAP GPIO
  3. *
  4. * Copyright (C) 2003-2005 Nokia Corporation
  5. * Written by Juha Yrjölä <juha.yrjola@nokia.com>
  6. *
  7. * Copyright (C) 2009 Texas Instruments
  8. * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/syscore_ops.h>
  18. #include <linux/err.h>
  19. #include <linux/clk.h>
  20. #include <linux/io.h>
  21. #include <linux/slab.h>
  22. #include <linux/pm_runtime.h>
  23. #include <mach/hardware.h>
  24. #include <asm/irq.h>
  25. #include <mach/irqs.h>
  26. #include <mach/gpio.h>
  27. #include <asm/mach/irq.h>
  28. struct gpio_bank {
  29. unsigned long pbase;
  30. void __iomem *base;
  31. u16 irq;
  32. u16 virtual_irq_start;
  33. int method;
  34. #if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP2PLUS)
  35. u32 suspend_wakeup;
  36. u32 saved_wakeup;
  37. #endif
  38. u32 non_wakeup_gpios;
  39. u32 enabled_non_wakeup_gpios;
  40. u32 saved_datain;
  41. u32 saved_fallingdetect;
  42. u32 saved_risingdetect;
  43. u32 level_mask;
  44. u32 toggle_mask;
  45. spinlock_t lock;
  46. struct gpio_chip chip;
  47. struct clk *dbck;
  48. u32 mod_usage;
  49. u32 dbck_enable_mask;
  50. struct device *dev;
  51. bool dbck_flag;
  52. int stride;
  53. };
  54. #ifdef CONFIG_ARCH_OMAP3
  55. struct omap3_gpio_regs {
  56. u32 irqenable1;
  57. u32 irqenable2;
  58. u32 wake_en;
  59. u32 ctrl;
  60. u32 oe;
  61. u32 leveldetect0;
  62. u32 leveldetect1;
  63. u32 risingdetect;
  64. u32 fallingdetect;
  65. u32 dataout;
  66. };
  67. static struct omap3_gpio_regs gpio_context[OMAP34XX_NR_GPIOS];
  68. #endif
  69. /*
  70. * TODO: Cleanup gpio_bank usage as it is having information
  71. * related to all instances of the device
  72. */
  73. static struct gpio_bank *gpio_bank;
  74. static int bank_width;
  75. /* TODO: Analyze removing gpio_bank_count usage from driver code */
  76. int gpio_bank_count;
  77. static inline struct gpio_bank *get_gpio_bank(int gpio)
  78. {
  79. if (cpu_is_omap15xx()) {
  80. if (OMAP_GPIO_IS_MPUIO(gpio))
  81. return &gpio_bank[0];
  82. return &gpio_bank[1];
  83. }
  84. if (cpu_is_omap16xx()) {
  85. if (OMAP_GPIO_IS_MPUIO(gpio))
  86. return &gpio_bank[0];
  87. return &gpio_bank[1 + (gpio >> 4)];
  88. }
  89. if (cpu_is_omap7xx()) {
  90. if (OMAP_GPIO_IS_MPUIO(gpio))
  91. return &gpio_bank[0];
  92. return &gpio_bank[1 + (gpio >> 5)];
  93. }
  94. if (cpu_is_omap24xx())
  95. return &gpio_bank[gpio >> 5];
  96. if (cpu_is_omap34xx() || cpu_is_omap44xx())
  97. return &gpio_bank[gpio >> 5];
  98. BUG();
  99. return NULL;
  100. }
  101. static inline int get_gpio_index(int gpio)
  102. {
  103. if (cpu_is_omap7xx())
  104. return gpio & 0x1f;
  105. if (cpu_is_omap24xx())
  106. return gpio & 0x1f;
  107. if (cpu_is_omap34xx() || cpu_is_omap44xx())
  108. return gpio & 0x1f;
  109. return gpio & 0x0f;
  110. }
  111. static inline int gpio_valid(int gpio)
  112. {
  113. if (gpio < 0)
  114. return -1;
  115. if (cpu_class_is_omap1() && OMAP_GPIO_IS_MPUIO(gpio)) {
  116. if (gpio >= OMAP_MAX_GPIO_LINES + 16)
  117. return -1;
  118. return 0;
  119. }
  120. if (cpu_is_omap15xx() && gpio < 16)
  121. return 0;
  122. if ((cpu_is_omap16xx()) && gpio < 64)
  123. return 0;
  124. if (cpu_is_omap7xx() && gpio < 192)
  125. return 0;
  126. if (cpu_is_omap2420() && gpio < 128)
  127. return 0;
  128. if (cpu_is_omap2430() && gpio < 160)
  129. return 0;
  130. if ((cpu_is_omap34xx() || cpu_is_omap44xx()) && gpio < 192)
  131. return 0;
  132. return -1;
  133. }
  134. static int check_gpio(int gpio)
  135. {
  136. if (unlikely(gpio_valid(gpio) < 0)) {
  137. printk(KERN_ERR "omap-gpio: invalid GPIO %d\n", gpio);
  138. dump_stack();
  139. return -1;
  140. }
  141. return 0;
  142. }
  143. static void _set_gpio_direction(struct gpio_bank *bank, int gpio, int is_input)
  144. {
  145. void __iomem *reg = bank->base;
  146. u32 l;
  147. switch (bank->method) {
  148. #ifdef CONFIG_ARCH_OMAP1
  149. case METHOD_MPUIO:
  150. reg += OMAP_MPUIO_IO_CNTL / bank->stride;
  151. break;
  152. #endif
  153. #ifdef CONFIG_ARCH_OMAP15XX
  154. case METHOD_GPIO_1510:
  155. reg += OMAP1510_GPIO_DIR_CONTROL;
  156. break;
  157. #endif
  158. #ifdef CONFIG_ARCH_OMAP16XX
  159. case METHOD_GPIO_1610:
  160. reg += OMAP1610_GPIO_DIRECTION;
  161. break;
  162. #endif
  163. #if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
  164. case METHOD_GPIO_7XX:
  165. reg += OMAP7XX_GPIO_DIR_CONTROL;
  166. break;
  167. #endif
  168. #if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
  169. case METHOD_GPIO_24XX:
  170. reg += OMAP24XX_GPIO_OE;
  171. break;
  172. #endif
  173. #if defined(CONFIG_ARCH_OMAP4)
  174. case METHOD_GPIO_44XX:
  175. reg += OMAP4_GPIO_OE;
  176. break;
  177. #endif
  178. default:
  179. WARN_ON(1);
  180. return;
  181. }
  182. l = __raw_readl(reg);
  183. if (is_input)
  184. l |= 1 << gpio;
  185. else
  186. l &= ~(1 << gpio);
  187. __raw_writel(l, reg);
  188. }
  189. static void _set_gpio_dataout(struct gpio_bank *bank, int gpio, int enable)
  190. {
  191. void __iomem *reg = bank->base;
  192. u32 l = 0;
  193. switch (bank->method) {
  194. #ifdef CONFIG_ARCH_OMAP1
  195. case METHOD_MPUIO:
  196. reg += OMAP_MPUIO_OUTPUT / bank->stride;
  197. l = __raw_readl(reg);
  198. if (enable)
  199. l |= 1 << gpio;
  200. else
  201. l &= ~(1 << gpio);
  202. break;
  203. #endif
  204. #ifdef CONFIG_ARCH_OMAP15XX
  205. case METHOD_GPIO_1510:
  206. reg += OMAP1510_GPIO_DATA_OUTPUT;
  207. l = __raw_readl(reg);
  208. if (enable)
  209. l |= 1 << gpio;
  210. else
  211. l &= ~(1 << gpio);
  212. break;
  213. #endif
  214. #ifdef CONFIG_ARCH_OMAP16XX
  215. case METHOD_GPIO_1610:
  216. if (enable)
  217. reg += OMAP1610_GPIO_SET_DATAOUT;
  218. else
  219. reg += OMAP1610_GPIO_CLEAR_DATAOUT;
  220. l = 1 << gpio;
  221. break;
  222. #endif
  223. #if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
  224. case METHOD_GPIO_7XX:
  225. reg += OMAP7XX_GPIO_DATA_OUTPUT;
  226. l = __raw_readl(reg);
  227. if (enable)
  228. l |= 1 << gpio;
  229. else
  230. l &= ~(1 << gpio);
  231. break;
  232. #endif
  233. #if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
  234. case METHOD_GPIO_24XX:
  235. if (enable)
  236. reg += OMAP24XX_GPIO_SETDATAOUT;
  237. else
  238. reg += OMAP24XX_GPIO_CLEARDATAOUT;
  239. l = 1 << gpio;
  240. break;
  241. #endif
  242. #ifdef CONFIG_ARCH_OMAP4
  243. case METHOD_GPIO_44XX:
  244. if (enable)
  245. reg += OMAP4_GPIO_SETDATAOUT;
  246. else
  247. reg += OMAP4_GPIO_CLEARDATAOUT;
  248. l = 1 << gpio;
  249. break;
  250. #endif
  251. default:
  252. WARN_ON(1);
  253. return;
  254. }
  255. __raw_writel(l, reg);
  256. }
  257. static int _get_gpio_datain(struct gpio_bank *bank, int gpio)
  258. {
  259. void __iomem *reg;
  260. if (check_gpio(gpio) < 0)
  261. return -EINVAL;
  262. reg = bank->base;
  263. switch (bank->method) {
  264. #ifdef CONFIG_ARCH_OMAP1
  265. case METHOD_MPUIO:
  266. reg += OMAP_MPUIO_INPUT_LATCH / bank->stride;
  267. break;
  268. #endif
  269. #ifdef CONFIG_ARCH_OMAP15XX
  270. case METHOD_GPIO_1510:
  271. reg += OMAP1510_GPIO_DATA_INPUT;
  272. break;
  273. #endif
  274. #ifdef CONFIG_ARCH_OMAP16XX
  275. case METHOD_GPIO_1610:
  276. reg += OMAP1610_GPIO_DATAIN;
  277. break;
  278. #endif
  279. #if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
  280. case METHOD_GPIO_7XX:
  281. reg += OMAP7XX_GPIO_DATA_INPUT;
  282. break;
  283. #endif
  284. #if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
  285. case METHOD_GPIO_24XX:
  286. reg += OMAP24XX_GPIO_DATAIN;
  287. break;
  288. #endif
  289. #ifdef CONFIG_ARCH_OMAP4
  290. case METHOD_GPIO_44XX:
  291. reg += OMAP4_GPIO_DATAIN;
  292. break;
  293. #endif
  294. default:
  295. return -EINVAL;
  296. }
  297. return (__raw_readl(reg)
  298. & (1 << get_gpio_index(gpio))) != 0;
  299. }
  300. static int _get_gpio_dataout(struct gpio_bank *bank, int gpio)
  301. {
  302. void __iomem *reg;
  303. if (check_gpio(gpio) < 0)
  304. return -EINVAL;
  305. reg = bank->base;
  306. switch (bank->method) {
  307. #ifdef CONFIG_ARCH_OMAP1
  308. case METHOD_MPUIO:
  309. reg += OMAP_MPUIO_OUTPUT / bank->stride;
  310. break;
  311. #endif
  312. #ifdef CONFIG_ARCH_OMAP15XX
  313. case METHOD_GPIO_1510:
  314. reg += OMAP1510_GPIO_DATA_OUTPUT;
  315. break;
  316. #endif
  317. #ifdef CONFIG_ARCH_OMAP16XX
  318. case METHOD_GPIO_1610:
  319. reg += OMAP1610_GPIO_DATAOUT;
  320. break;
  321. #endif
  322. #if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
  323. case METHOD_GPIO_7XX:
  324. reg += OMAP7XX_GPIO_DATA_OUTPUT;
  325. break;
  326. #endif
  327. #if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
  328. case METHOD_GPIO_24XX:
  329. reg += OMAP24XX_GPIO_DATAOUT;
  330. break;
  331. #endif
  332. #ifdef CONFIG_ARCH_OMAP4
  333. case METHOD_GPIO_44XX:
  334. reg += OMAP4_GPIO_DATAOUT;
  335. break;
  336. #endif
  337. default:
  338. return -EINVAL;
  339. }
  340. return (__raw_readl(reg) & (1 << get_gpio_index(gpio))) != 0;
  341. }
  342. #define MOD_REG_BIT(reg, bit_mask, set) \
  343. do { \
  344. int l = __raw_readl(base + reg); \
  345. if (set) l |= bit_mask; \
  346. else l &= ~bit_mask; \
  347. __raw_writel(l, base + reg); \
  348. } while(0)
  349. /**
  350. * _set_gpio_debounce - low level gpio debounce time
  351. * @bank: the gpio bank we're acting upon
  352. * @gpio: the gpio number on this @gpio
  353. * @debounce: debounce time to use
  354. *
  355. * OMAP's debounce time is in 31us steps so we need
  356. * to convert and round up to the closest unit.
  357. */
  358. static void _set_gpio_debounce(struct gpio_bank *bank, unsigned gpio,
  359. unsigned debounce)
  360. {
  361. void __iomem *reg = bank->base;
  362. u32 val;
  363. u32 l;
  364. if (!bank->dbck_flag)
  365. return;
  366. if (debounce < 32)
  367. debounce = 0x01;
  368. else if (debounce > 7936)
  369. debounce = 0xff;
  370. else
  371. debounce = (debounce / 0x1f) - 1;
  372. l = 1 << get_gpio_index(gpio);
  373. if (bank->method == METHOD_GPIO_44XX)
  374. reg += OMAP4_GPIO_DEBOUNCINGTIME;
  375. else
  376. reg += OMAP24XX_GPIO_DEBOUNCE_VAL;
  377. __raw_writel(debounce, reg);
  378. reg = bank->base;
  379. if (bank->method == METHOD_GPIO_44XX)
  380. reg += OMAP4_GPIO_DEBOUNCENABLE;
  381. else
  382. reg += OMAP24XX_GPIO_DEBOUNCE_EN;
  383. val = __raw_readl(reg);
  384. if (debounce) {
  385. val |= l;
  386. clk_enable(bank->dbck);
  387. } else {
  388. val &= ~l;
  389. clk_disable(bank->dbck);
  390. }
  391. bank->dbck_enable_mask = val;
  392. __raw_writel(val, reg);
  393. }
  394. #ifdef CONFIG_ARCH_OMAP2PLUS
  395. static inline void set_24xx_gpio_triggering(struct gpio_bank *bank, int gpio,
  396. int trigger)
  397. {
  398. void __iomem *base = bank->base;
  399. u32 gpio_bit = 1 << gpio;
  400. if (cpu_is_omap44xx()) {
  401. MOD_REG_BIT(OMAP4_GPIO_LEVELDETECT0, gpio_bit,
  402. trigger & IRQ_TYPE_LEVEL_LOW);
  403. MOD_REG_BIT(OMAP4_GPIO_LEVELDETECT1, gpio_bit,
  404. trigger & IRQ_TYPE_LEVEL_HIGH);
  405. MOD_REG_BIT(OMAP4_GPIO_RISINGDETECT, gpio_bit,
  406. trigger & IRQ_TYPE_EDGE_RISING);
  407. MOD_REG_BIT(OMAP4_GPIO_FALLINGDETECT, gpio_bit,
  408. trigger & IRQ_TYPE_EDGE_FALLING);
  409. } else {
  410. MOD_REG_BIT(OMAP24XX_GPIO_LEVELDETECT0, gpio_bit,
  411. trigger & IRQ_TYPE_LEVEL_LOW);
  412. MOD_REG_BIT(OMAP24XX_GPIO_LEVELDETECT1, gpio_bit,
  413. trigger & IRQ_TYPE_LEVEL_HIGH);
  414. MOD_REG_BIT(OMAP24XX_GPIO_RISINGDETECT, gpio_bit,
  415. trigger & IRQ_TYPE_EDGE_RISING);
  416. MOD_REG_BIT(OMAP24XX_GPIO_FALLINGDETECT, gpio_bit,
  417. trigger & IRQ_TYPE_EDGE_FALLING);
  418. }
  419. if (likely(!(bank->non_wakeup_gpios & gpio_bit))) {
  420. if (cpu_is_omap44xx()) {
  421. MOD_REG_BIT(OMAP4_GPIO_IRQWAKEN0, gpio_bit,
  422. trigger != 0);
  423. } else {
  424. /*
  425. * GPIO wakeup request can only be generated on edge
  426. * transitions
  427. */
  428. if (trigger & IRQ_TYPE_EDGE_BOTH)
  429. __raw_writel(1 << gpio, bank->base
  430. + OMAP24XX_GPIO_SETWKUENA);
  431. else
  432. __raw_writel(1 << gpio, bank->base
  433. + OMAP24XX_GPIO_CLEARWKUENA);
  434. }
  435. }
  436. /* This part needs to be executed always for OMAP{34xx, 44xx} */
  437. if (cpu_is_omap34xx() || cpu_is_omap44xx() ||
  438. (bank->non_wakeup_gpios & gpio_bit)) {
  439. /*
  440. * Log the edge gpio and manually trigger the IRQ
  441. * after resume if the input level changes
  442. * to avoid irq lost during PER RET/OFF mode
  443. * Applies for omap2 non-wakeup gpio and all omap3 gpios
  444. */
  445. if (trigger & IRQ_TYPE_EDGE_BOTH)
  446. bank->enabled_non_wakeup_gpios |= gpio_bit;
  447. else
  448. bank->enabled_non_wakeup_gpios &= ~gpio_bit;
  449. }
  450. if (cpu_is_omap44xx()) {
  451. bank->level_mask =
  452. __raw_readl(bank->base + OMAP4_GPIO_LEVELDETECT0) |
  453. __raw_readl(bank->base + OMAP4_GPIO_LEVELDETECT1);
  454. } else {
  455. bank->level_mask =
  456. __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT0) |
  457. __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT1);
  458. }
  459. }
  460. #endif
  461. #ifdef CONFIG_ARCH_OMAP1
  462. /*
  463. * This only applies to chips that can't do both rising and falling edge
  464. * detection at once. For all other chips, this function is a noop.
  465. */
  466. static void _toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio)
  467. {
  468. void __iomem *reg = bank->base;
  469. u32 l = 0;
  470. switch (bank->method) {
  471. case METHOD_MPUIO:
  472. reg += OMAP_MPUIO_GPIO_INT_EDGE / bank->stride;
  473. break;
  474. #ifdef CONFIG_ARCH_OMAP15XX
  475. case METHOD_GPIO_1510:
  476. reg += OMAP1510_GPIO_INT_CONTROL;
  477. break;
  478. #endif
  479. #if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
  480. case METHOD_GPIO_7XX:
  481. reg += OMAP7XX_GPIO_INT_CONTROL;
  482. break;
  483. #endif
  484. default:
  485. return;
  486. }
  487. l = __raw_readl(reg);
  488. if ((l >> gpio) & 1)
  489. l &= ~(1 << gpio);
  490. else
  491. l |= 1 << gpio;
  492. __raw_writel(l, reg);
  493. }
  494. #endif
  495. static int _set_gpio_triggering(struct gpio_bank *bank, int gpio, int trigger)
  496. {
  497. void __iomem *reg = bank->base;
  498. u32 l = 0;
  499. switch (bank->method) {
  500. #ifdef CONFIG_ARCH_OMAP1
  501. case METHOD_MPUIO:
  502. reg += OMAP_MPUIO_GPIO_INT_EDGE / bank->stride;
  503. l = __raw_readl(reg);
  504. if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
  505. bank->toggle_mask |= 1 << gpio;
  506. if (trigger & IRQ_TYPE_EDGE_RISING)
  507. l |= 1 << gpio;
  508. else if (trigger & IRQ_TYPE_EDGE_FALLING)
  509. l &= ~(1 << gpio);
  510. else
  511. goto bad;
  512. break;
  513. #endif
  514. #ifdef CONFIG_ARCH_OMAP15XX
  515. case METHOD_GPIO_1510:
  516. reg += OMAP1510_GPIO_INT_CONTROL;
  517. l = __raw_readl(reg);
  518. if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
  519. bank->toggle_mask |= 1 << gpio;
  520. if (trigger & IRQ_TYPE_EDGE_RISING)
  521. l |= 1 << gpio;
  522. else if (trigger & IRQ_TYPE_EDGE_FALLING)
  523. l &= ~(1 << gpio);
  524. else
  525. goto bad;
  526. break;
  527. #endif
  528. #ifdef CONFIG_ARCH_OMAP16XX
  529. case METHOD_GPIO_1610:
  530. if (gpio & 0x08)
  531. reg += OMAP1610_GPIO_EDGE_CTRL2;
  532. else
  533. reg += OMAP1610_GPIO_EDGE_CTRL1;
  534. gpio &= 0x07;
  535. l = __raw_readl(reg);
  536. l &= ~(3 << (gpio << 1));
  537. if (trigger & IRQ_TYPE_EDGE_RISING)
  538. l |= 2 << (gpio << 1);
  539. if (trigger & IRQ_TYPE_EDGE_FALLING)
  540. l |= 1 << (gpio << 1);
  541. if (trigger)
  542. /* Enable wake-up during idle for dynamic tick */
  543. __raw_writel(1 << gpio, bank->base + OMAP1610_GPIO_SET_WAKEUPENA);
  544. else
  545. __raw_writel(1 << gpio, bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA);
  546. break;
  547. #endif
  548. #if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
  549. case METHOD_GPIO_7XX:
  550. reg += OMAP7XX_GPIO_INT_CONTROL;
  551. l = __raw_readl(reg);
  552. if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
  553. bank->toggle_mask |= 1 << gpio;
  554. if (trigger & IRQ_TYPE_EDGE_RISING)
  555. l |= 1 << gpio;
  556. else if (trigger & IRQ_TYPE_EDGE_FALLING)
  557. l &= ~(1 << gpio);
  558. else
  559. goto bad;
  560. break;
  561. #endif
  562. #ifdef CONFIG_ARCH_OMAP2PLUS
  563. case METHOD_GPIO_24XX:
  564. case METHOD_GPIO_44XX:
  565. set_24xx_gpio_triggering(bank, gpio, trigger);
  566. return 0;
  567. #endif
  568. default:
  569. goto bad;
  570. }
  571. __raw_writel(l, reg);
  572. return 0;
  573. bad:
  574. return -EINVAL;
  575. }
  576. static int gpio_irq_type(struct irq_data *d, unsigned type)
  577. {
  578. struct gpio_bank *bank;
  579. unsigned gpio;
  580. int retval;
  581. unsigned long flags;
  582. if (!cpu_class_is_omap2() && d->irq > IH_MPUIO_BASE)
  583. gpio = OMAP_MPUIO(d->irq - IH_MPUIO_BASE);
  584. else
  585. gpio = d->irq - IH_GPIO_BASE;
  586. if (check_gpio(gpio) < 0)
  587. return -EINVAL;
  588. if (type & ~IRQ_TYPE_SENSE_MASK)
  589. return -EINVAL;
  590. /* OMAP1 allows only only edge triggering */
  591. if (!cpu_class_is_omap2()
  592. && (type & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH)))
  593. return -EINVAL;
  594. bank = irq_data_get_irq_chip_data(d);
  595. spin_lock_irqsave(&bank->lock, flags);
  596. retval = _set_gpio_triggering(bank, get_gpio_index(gpio), type);
  597. spin_unlock_irqrestore(&bank->lock, flags);
  598. if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
  599. __irq_set_handler_locked(d->irq, handle_level_irq);
  600. else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
  601. __irq_set_handler_locked(d->irq, handle_edge_irq);
  602. return retval;
  603. }
  604. static void _clear_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
  605. {
  606. void __iomem *reg = bank->base;
  607. switch (bank->method) {
  608. #ifdef CONFIG_ARCH_OMAP1
  609. case METHOD_MPUIO:
  610. /* MPUIO irqstatus is reset by reading the status register,
  611. * so do nothing here */
  612. return;
  613. #endif
  614. #ifdef CONFIG_ARCH_OMAP15XX
  615. case METHOD_GPIO_1510:
  616. reg += OMAP1510_GPIO_INT_STATUS;
  617. break;
  618. #endif
  619. #ifdef CONFIG_ARCH_OMAP16XX
  620. case METHOD_GPIO_1610:
  621. reg += OMAP1610_GPIO_IRQSTATUS1;
  622. break;
  623. #endif
  624. #if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
  625. case METHOD_GPIO_7XX:
  626. reg += OMAP7XX_GPIO_INT_STATUS;
  627. break;
  628. #endif
  629. #if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
  630. case METHOD_GPIO_24XX:
  631. reg += OMAP24XX_GPIO_IRQSTATUS1;
  632. break;
  633. #endif
  634. #if defined(CONFIG_ARCH_OMAP4)
  635. case METHOD_GPIO_44XX:
  636. reg += OMAP4_GPIO_IRQSTATUS0;
  637. break;
  638. #endif
  639. default:
  640. WARN_ON(1);
  641. return;
  642. }
  643. __raw_writel(gpio_mask, reg);
  644. /* Workaround for clearing DSP GPIO interrupts to allow retention */
  645. if (cpu_is_omap24xx() || cpu_is_omap34xx())
  646. reg = bank->base + OMAP24XX_GPIO_IRQSTATUS2;
  647. else if (cpu_is_omap44xx())
  648. reg = bank->base + OMAP4_GPIO_IRQSTATUS1;
  649. if (cpu_is_omap24xx() || cpu_is_omap34xx() || cpu_is_omap44xx()) {
  650. __raw_writel(gpio_mask, reg);
  651. /* Flush posted write for the irq status to avoid spurious interrupts */
  652. __raw_readl(reg);
  653. }
  654. }
  655. static inline void _clear_gpio_irqstatus(struct gpio_bank *bank, int gpio)
  656. {
  657. _clear_gpio_irqbank(bank, 1 << get_gpio_index(gpio));
  658. }
  659. static u32 _get_gpio_irqbank_mask(struct gpio_bank *bank)
  660. {
  661. void __iomem *reg = bank->base;
  662. int inv = 0;
  663. u32 l;
  664. u32 mask;
  665. switch (bank->method) {
  666. #ifdef CONFIG_ARCH_OMAP1
  667. case METHOD_MPUIO:
  668. reg += OMAP_MPUIO_GPIO_MASKIT / bank->stride;
  669. mask = 0xffff;
  670. inv = 1;
  671. break;
  672. #endif
  673. #ifdef CONFIG_ARCH_OMAP15XX
  674. case METHOD_GPIO_1510:
  675. reg += OMAP1510_GPIO_INT_MASK;
  676. mask = 0xffff;
  677. inv = 1;
  678. break;
  679. #endif
  680. #ifdef CONFIG_ARCH_OMAP16XX
  681. case METHOD_GPIO_1610:
  682. reg += OMAP1610_GPIO_IRQENABLE1;
  683. mask = 0xffff;
  684. break;
  685. #endif
  686. #if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
  687. case METHOD_GPIO_7XX:
  688. reg += OMAP7XX_GPIO_INT_MASK;
  689. mask = 0xffffffff;
  690. inv = 1;
  691. break;
  692. #endif
  693. #if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
  694. case METHOD_GPIO_24XX:
  695. reg += OMAP24XX_GPIO_IRQENABLE1;
  696. mask = 0xffffffff;
  697. break;
  698. #endif
  699. #if defined(CONFIG_ARCH_OMAP4)
  700. case METHOD_GPIO_44XX:
  701. reg += OMAP4_GPIO_IRQSTATUSSET0;
  702. mask = 0xffffffff;
  703. break;
  704. #endif
  705. default:
  706. WARN_ON(1);
  707. return 0;
  708. }
  709. l = __raw_readl(reg);
  710. if (inv)
  711. l = ~l;
  712. l &= mask;
  713. return l;
  714. }
  715. static void _enable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask, int enable)
  716. {
  717. void __iomem *reg = bank->base;
  718. u32 l;
  719. switch (bank->method) {
  720. #ifdef CONFIG_ARCH_OMAP1
  721. case METHOD_MPUIO:
  722. reg += OMAP_MPUIO_GPIO_MASKIT / bank->stride;
  723. l = __raw_readl(reg);
  724. if (enable)
  725. l &= ~(gpio_mask);
  726. else
  727. l |= gpio_mask;
  728. break;
  729. #endif
  730. #ifdef CONFIG_ARCH_OMAP15XX
  731. case METHOD_GPIO_1510:
  732. reg += OMAP1510_GPIO_INT_MASK;
  733. l = __raw_readl(reg);
  734. if (enable)
  735. l &= ~(gpio_mask);
  736. else
  737. l |= gpio_mask;
  738. break;
  739. #endif
  740. #ifdef CONFIG_ARCH_OMAP16XX
  741. case METHOD_GPIO_1610:
  742. if (enable)
  743. reg += OMAP1610_GPIO_SET_IRQENABLE1;
  744. else
  745. reg += OMAP1610_GPIO_CLEAR_IRQENABLE1;
  746. l = gpio_mask;
  747. break;
  748. #endif
  749. #if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
  750. case METHOD_GPIO_7XX:
  751. reg += OMAP7XX_GPIO_INT_MASK;
  752. l = __raw_readl(reg);
  753. if (enable)
  754. l &= ~(gpio_mask);
  755. else
  756. l |= gpio_mask;
  757. break;
  758. #endif
  759. #if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
  760. case METHOD_GPIO_24XX:
  761. if (enable)
  762. reg += OMAP24XX_GPIO_SETIRQENABLE1;
  763. else
  764. reg += OMAP24XX_GPIO_CLEARIRQENABLE1;
  765. l = gpio_mask;
  766. break;
  767. #endif
  768. #ifdef CONFIG_ARCH_OMAP4
  769. case METHOD_GPIO_44XX:
  770. if (enable)
  771. reg += OMAP4_GPIO_IRQSTATUSSET0;
  772. else
  773. reg += OMAP4_GPIO_IRQSTATUSCLR0;
  774. l = gpio_mask;
  775. break;
  776. #endif
  777. default:
  778. WARN_ON(1);
  779. return;
  780. }
  781. __raw_writel(l, reg);
  782. }
  783. static inline void _set_gpio_irqenable(struct gpio_bank *bank, int gpio, int enable)
  784. {
  785. _enable_gpio_irqbank(bank, 1 << get_gpio_index(gpio), enable);
  786. }
  787. /*
  788. * Note that ENAWAKEUP needs to be enabled in GPIO_SYSCONFIG register.
  789. * 1510 does not seem to have a wake-up register. If JTAG is connected
  790. * to the target, system will wake up always on GPIO events. While
  791. * system is running all registered GPIO interrupts need to have wake-up
  792. * enabled. When system is suspended, only selected GPIO interrupts need
  793. * to have wake-up enabled.
  794. */
  795. static int _set_gpio_wakeup(struct gpio_bank *bank, int gpio, int enable)
  796. {
  797. unsigned long uninitialized_var(flags);
  798. switch (bank->method) {
  799. #ifdef CONFIG_ARCH_OMAP16XX
  800. case METHOD_MPUIO:
  801. case METHOD_GPIO_1610:
  802. spin_lock_irqsave(&bank->lock, flags);
  803. if (enable)
  804. bank->suspend_wakeup |= (1 << gpio);
  805. else
  806. bank->suspend_wakeup &= ~(1 << gpio);
  807. spin_unlock_irqrestore(&bank->lock, flags);
  808. return 0;
  809. #endif
  810. #ifdef CONFIG_ARCH_OMAP2PLUS
  811. case METHOD_GPIO_24XX:
  812. case METHOD_GPIO_44XX:
  813. if (bank->non_wakeup_gpios & (1 << gpio)) {
  814. printk(KERN_ERR "Unable to modify wakeup on "
  815. "non-wakeup GPIO%d\n",
  816. (bank - gpio_bank) * 32 + gpio);
  817. return -EINVAL;
  818. }
  819. spin_lock_irqsave(&bank->lock, flags);
  820. if (enable)
  821. bank->suspend_wakeup |= (1 << gpio);
  822. else
  823. bank->suspend_wakeup &= ~(1 << gpio);
  824. spin_unlock_irqrestore(&bank->lock, flags);
  825. return 0;
  826. #endif
  827. default:
  828. printk(KERN_ERR "Can't enable GPIO wakeup for method %i\n",
  829. bank->method);
  830. return -EINVAL;
  831. }
  832. }
  833. static void _reset_gpio(struct gpio_bank *bank, int gpio)
  834. {
  835. _set_gpio_direction(bank, get_gpio_index(gpio), 1);
  836. _set_gpio_irqenable(bank, gpio, 0);
  837. _clear_gpio_irqstatus(bank, gpio);
  838. _set_gpio_triggering(bank, get_gpio_index(gpio), IRQ_TYPE_NONE);
  839. }
  840. /* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
  841. static int gpio_wake_enable(struct irq_data *d, unsigned int enable)
  842. {
  843. unsigned int gpio = d->irq - IH_GPIO_BASE;
  844. struct gpio_bank *bank;
  845. int retval;
  846. if (check_gpio(gpio) < 0)
  847. return -ENODEV;
  848. bank = irq_data_get_irq_chip_data(d);
  849. retval = _set_gpio_wakeup(bank, get_gpio_index(gpio), enable);
  850. return retval;
  851. }
  852. static int omap_gpio_request(struct gpio_chip *chip, unsigned offset)
  853. {
  854. struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
  855. unsigned long flags;
  856. spin_lock_irqsave(&bank->lock, flags);
  857. /* Set trigger to none. You need to enable the desired trigger with
  858. * request_irq() or set_irq_type().
  859. */
  860. _set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
  861. #ifdef CONFIG_ARCH_OMAP15XX
  862. if (bank->method == METHOD_GPIO_1510) {
  863. void __iomem *reg;
  864. /* Claim the pin for MPU */
  865. reg = bank->base + OMAP1510_GPIO_PIN_CONTROL;
  866. __raw_writel(__raw_readl(reg) | (1 << offset), reg);
  867. }
  868. #endif
  869. if (!cpu_class_is_omap1()) {
  870. if (!bank->mod_usage) {
  871. void __iomem *reg = bank->base;
  872. u32 ctrl;
  873. if (cpu_is_omap24xx() || cpu_is_omap34xx())
  874. reg += OMAP24XX_GPIO_CTRL;
  875. else if (cpu_is_omap44xx())
  876. reg += OMAP4_GPIO_CTRL;
  877. ctrl = __raw_readl(reg);
  878. /* Module is enabled, clocks are not gated */
  879. ctrl &= 0xFFFFFFFE;
  880. __raw_writel(ctrl, reg);
  881. }
  882. bank->mod_usage |= 1 << offset;
  883. }
  884. spin_unlock_irqrestore(&bank->lock, flags);
  885. return 0;
  886. }
  887. static void omap_gpio_free(struct gpio_chip *chip, unsigned offset)
  888. {
  889. struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
  890. unsigned long flags;
  891. spin_lock_irqsave(&bank->lock, flags);
  892. #ifdef CONFIG_ARCH_OMAP16XX
  893. if (bank->method == METHOD_GPIO_1610) {
  894. /* Disable wake-up during idle for dynamic tick */
  895. void __iomem *reg = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
  896. __raw_writel(1 << offset, reg);
  897. }
  898. #endif
  899. #if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
  900. if (bank->method == METHOD_GPIO_24XX) {
  901. /* Disable wake-up during idle for dynamic tick */
  902. void __iomem *reg = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
  903. __raw_writel(1 << offset, reg);
  904. }
  905. #endif
  906. #ifdef CONFIG_ARCH_OMAP4
  907. if (bank->method == METHOD_GPIO_44XX) {
  908. /* Disable wake-up during idle for dynamic tick */
  909. void __iomem *reg = bank->base + OMAP4_GPIO_IRQWAKEN0;
  910. __raw_writel(1 << offset, reg);
  911. }
  912. #endif
  913. if (!cpu_class_is_omap1()) {
  914. bank->mod_usage &= ~(1 << offset);
  915. if (!bank->mod_usage) {
  916. void __iomem *reg = bank->base;
  917. u32 ctrl;
  918. if (cpu_is_omap24xx() || cpu_is_omap34xx())
  919. reg += OMAP24XX_GPIO_CTRL;
  920. else if (cpu_is_omap44xx())
  921. reg += OMAP4_GPIO_CTRL;
  922. ctrl = __raw_readl(reg);
  923. /* Module is disabled, clocks are gated */
  924. ctrl |= 1;
  925. __raw_writel(ctrl, reg);
  926. }
  927. }
  928. _reset_gpio(bank, bank->chip.base + offset);
  929. spin_unlock_irqrestore(&bank->lock, flags);
  930. }
  931. /*
  932. * We need to unmask the GPIO bank interrupt as soon as possible to
  933. * avoid missing GPIO interrupts for other lines in the bank.
  934. * Then we need to mask-read-clear-unmask the triggered GPIO lines
  935. * in the bank to avoid missing nested interrupts for a GPIO line.
  936. * If we wait to unmask individual GPIO lines in the bank after the
  937. * line's interrupt handler has been run, we may miss some nested
  938. * interrupts.
  939. */
  940. static void gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
  941. {
  942. void __iomem *isr_reg = NULL;
  943. u32 isr;
  944. unsigned int gpio_irq, gpio_index;
  945. struct gpio_bank *bank;
  946. u32 retrigger = 0;
  947. int unmasked = 0;
  948. struct irq_chip *chip = irq_desc_get_chip(desc);
  949. chained_irq_enter(chip, desc);
  950. bank = irq_get_handler_data(irq);
  951. #ifdef CONFIG_ARCH_OMAP1
  952. if (bank->method == METHOD_MPUIO)
  953. isr_reg = bank->base +
  954. OMAP_MPUIO_GPIO_INT / bank->stride;
  955. #endif
  956. #ifdef CONFIG_ARCH_OMAP15XX
  957. if (bank->method == METHOD_GPIO_1510)
  958. isr_reg = bank->base + OMAP1510_GPIO_INT_STATUS;
  959. #endif
  960. #if defined(CONFIG_ARCH_OMAP16XX)
  961. if (bank->method == METHOD_GPIO_1610)
  962. isr_reg = bank->base + OMAP1610_GPIO_IRQSTATUS1;
  963. #endif
  964. #if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
  965. if (bank->method == METHOD_GPIO_7XX)
  966. isr_reg = bank->base + OMAP7XX_GPIO_INT_STATUS;
  967. #endif
  968. #if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
  969. if (bank->method == METHOD_GPIO_24XX)
  970. isr_reg = bank->base + OMAP24XX_GPIO_IRQSTATUS1;
  971. #endif
  972. #if defined(CONFIG_ARCH_OMAP4)
  973. if (bank->method == METHOD_GPIO_44XX)
  974. isr_reg = bank->base + OMAP4_GPIO_IRQSTATUS0;
  975. #endif
  976. if (WARN_ON(!isr_reg))
  977. goto exit;
  978. while(1) {
  979. u32 isr_saved, level_mask = 0;
  980. u32 enabled;
  981. enabled = _get_gpio_irqbank_mask(bank);
  982. isr_saved = isr = __raw_readl(isr_reg) & enabled;
  983. if (cpu_is_omap15xx() && (bank->method == METHOD_MPUIO))
  984. isr &= 0x0000ffff;
  985. if (cpu_class_is_omap2()) {
  986. level_mask = bank->level_mask & enabled;
  987. }
  988. /* clear edge sensitive interrupts before handler(s) are
  989. called so that we don't miss any interrupt occurred while
  990. executing them */
  991. _enable_gpio_irqbank(bank, isr_saved & ~level_mask, 0);
  992. _clear_gpio_irqbank(bank, isr_saved & ~level_mask);
  993. _enable_gpio_irqbank(bank, isr_saved & ~level_mask, 1);
  994. /* if there is only edge sensitive GPIO pin interrupts
  995. configured, we could unmask GPIO bank interrupt immediately */
  996. if (!level_mask && !unmasked) {
  997. unmasked = 1;
  998. chained_irq_exit(chip, desc);
  999. }
  1000. isr |= retrigger;
  1001. retrigger = 0;
  1002. if (!isr)
  1003. break;
  1004. gpio_irq = bank->virtual_irq_start;
  1005. for (; isr != 0; isr >>= 1, gpio_irq++) {
  1006. gpio_index = get_gpio_index(irq_to_gpio(gpio_irq));
  1007. if (!(isr & 1))
  1008. continue;
  1009. #ifdef CONFIG_ARCH_OMAP1
  1010. /*
  1011. * Some chips can't respond to both rising and falling
  1012. * at the same time. If this irq was requested with
  1013. * both flags, we need to flip the ICR data for the IRQ
  1014. * to respond to the IRQ for the opposite direction.
  1015. * This will be indicated in the bank toggle_mask.
  1016. */
  1017. if (bank->toggle_mask & (1 << gpio_index))
  1018. _toggle_gpio_edge_triggering(bank, gpio_index);
  1019. #endif
  1020. generic_handle_irq(gpio_irq);
  1021. }
  1022. }
  1023. /* if bank has any level sensitive GPIO pin interrupt
  1024. configured, we must unmask the bank interrupt only after
  1025. handler(s) are executed in order to avoid spurious bank
  1026. interrupt */
  1027. exit:
  1028. if (!unmasked)
  1029. chained_irq_exit(chip, desc);
  1030. }
  1031. static void gpio_irq_shutdown(struct irq_data *d)
  1032. {
  1033. unsigned int gpio = d->irq - IH_GPIO_BASE;
  1034. struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
  1035. unsigned long flags;
  1036. spin_lock_irqsave(&bank->lock, flags);
  1037. _reset_gpio(bank, gpio);
  1038. spin_unlock_irqrestore(&bank->lock, flags);
  1039. }
  1040. static void gpio_ack_irq(struct irq_data *d)
  1041. {
  1042. unsigned int gpio = d->irq - IH_GPIO_BASE;
  1043. struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
  1044. _clear_gpio_irqstatus(bank, gpio);
  1045. }
  1046. static void gpio_mask_irq(struct irq_data *d)
  1047. {
  1048. unsigned int gpio = d->irq - IH_GPIO_BASE;
  1049. struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
  1050. unsigned long flags;
  1051. spin_lock_irqsave(&bank->lock, flags);
  1052. _set_gpio_irqenable(bank, gpio, 0);
  1053. _set_gpio_triggering(bank, get_gpio_index(gpio), IRQ_TYPE_NONE);
  1054. spin_unlock_irqrestore(&bank->lock, flags);
  1055. }
  1056. static void gpio_unmask_irq(struct irq_data *d)
  1057. {
  1058. unsigned int gpio = d->irq - IH_GPIO_BASE;
  1059. struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
  1060. unsigned int irq_mask = 1 << get_gpio_index(gpio);
  1061. u32 trigger = irqd_get_trigger_type(d);
  1062. unsigned long flags;
  1063. spin_lock_irqsave(&bank->lock, flags);
  1064. if (trigger)
  1065. _set_gpio_triggering(bank, get_gpio_index(gpio), trigger);
  1066. /* For level-triggered GPIOs, the clearing must be done after
  1067. * the HW source is cleared, thus after the handler has run */
  1068. if (bank->level_mask & irq_mask) {
  1069. _set_gpio_irqenable(bank, gpio, 0);
  1070. _clear_gpio_irqstatus(bank, gpio);
  1071. }
  1072. _set_gpio_irqenable(bank, gpio, 1);
  1073. spin_unlock_irqrestore(&bank->lock, flags);
  1074. }
  1075. static struct irq_chip gpio_irq_chip = {
  1076. .name = "GPIO",
  1077. .irq_shutdown = gpio_irq_shutdown,
  1078. .irq_ack = gpio_ack_irq,
  1079. .irq_mask = gpio_mask_irq,
  1080. .irq_unmask = gpio_unmask_irq,
  1081. .irq_set_type = gpio_irq_type,
  1082. .irq_set_wake = gpio_wake_enable,
  1083. };
  1084. /*---------------------------------------------------------------------*/
  1085. #ifdef CONFIG_ARCH_OMAP1
  1086. /* MPUIO uses the always-on 32k clock */
  1087. static void mpuio_ack_irq(struct irq_data *d)
  1088. {
  1089. /* The ISR is reset automatically, so do nothing here. */
  1090. }
  1091. static void mpuio_mask_irq(struct irq_data *d)
  1092. {
  1093. unsigned int gpio = OMAP_MPUIO(d->irq - IH_MPUIO_BASE);
  1094. struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
  1095. _set_gpio_irqenable(bank, gpio, 0);
  1096. }
  1097. static void mpuio_unmask_irq(struct irq_data *d)
  1098. {
  1099. unsigned int gpio = OMAP_MPUIO(d->irq - IH_MPUIO_BASE);
  1100. struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
  1101. _set_gpio_irqenable(bank, gpio, 1);
  1102. }
  1103. static struct irq_chip mpuio_irq_chip = {
  1104. .name = "MPUIO",
  1105. .irq_ack = mpuio_ack_irq,
  1106. .irq_mask = mpuio_mask_irq,
  1107. .irq_unmask = mpuio_unmask_irq,
  1108. .irq_set_type = gpio_irq_type,
  1109. #ifdef CONFIG_ARCH_OMAP16XX
  1110. /* REVISIT: assuming only 16xx supports MPUIO wake events */
  1111. .irq_set_wake = gpio_wake_enable,
  1112. #endif
  1113. };
  1114. #define bank_is_mpuio(bank) ((bank)->method == METHOD_MPUIO)
  1115. #ifdef CONFIG_ARCH_OMAP16XX
  1116. #include <linux/platform_device.h>
  1117. static int omap_mpuio_suspend_noirq(struct device *dev)
  1118. {
  1119. struct platform_device *pdev = to_platform_device(dev);
  1120. struct gpio_bank *bank = platform_get_drvdata(pdev);
  1121. void __iomem *mask_reg = bank->base +
  1122. OMAP_MPUIO_GPIO_MASKIT / bank->stride;
  1123. unsigned long flags;
  1124. spin_lock_irqsave(&bank->lock, flags);
  1125. bank->saved_wakeup = __raw_readl(mask_reg);
  1126. __raw_writel(0xffff & ~bank->suspend_wakeup, mask_reg);
  1127. spin_unlock_irqrestore(&bank->lock, flags);
  1128. return 0;
  1129. }
  1130. static int omap_mpuio_resume_noirq(struct device *dev)
  1131. {
  1132. struct platform_device *pdev = to_platform_device(dev);
  1133. struct gpio_bank *bank = platform_get_drvdata(pdev);
  1134. void __iomem *mask_reg = bank->base +
  1135. OMAP_MPUIO_GPIO_MASKIT / bank->stride;
  1136. unsigned long flags;
  1137. spin_lock_irqsave(&bank->lock, flags);
  1138. __raw_writel(bank->saved_wakeup, mask_reg);
  1139. spin_unlock_irqrestore(&bank->lock, flags);
  1140. return 0;
  1141. }
  1142. static const struct dev_pm_ops omap_mpuio_dev_pm_ops = {
  1143. .suspend_noirq = omap_mpuio_suspend_noirq,
  1144. .resume_noirq = omap_mpuio_resume_noirq,
  1145. };
  1146. /* use platform_driver for this. */
  1147. static struct platform_driver omap_mpuio_driver = {
  1148. .driver = {
  1149. .name = "mpuio",
  1150. .pm = &omap_mpuio_dev_pm_ops,
  1151. },
  1152. };
  1153. static struct platform_device omap_mpuio_device = {
  1154. .name = "mpuio",
  1155. .id = -1,
  1156. .dev = {
  1157. .driver = &omap_mpuio_driver.driver,
  1158. }
  1159. /* could list the /proc/iomem resources */
  1160. };
  1161. static inline void mpuio_init(void)
  1162. {
  1163. struct gpio_bank *bank = get_gpio_bank(OMAP_MPUIO(0));
  1164. platform_set_drvdata(&omap_mpuio_device, bank);
  1165. if (platform_driver_register(&omap_mpuio_driver) == 0)
  1166. (void) platform_device_register(&omap_mpuio_device);
  1167. }
  1168. #else
  1169. static inline void mpuio_init(void) {}
  1170. #endif /* 16xx */
  1171. #else
  1172. extern struct irq_chip mpuio_irq_chip;
  1173. #define bank_is_mpuio(bank) 0
  1174. static inline void mpuio_init(void) {}
  1175. #endif
  1176. /*---------------------------------------------------------------------*/
  1177. /* REVISIT these are stupid implementations! replace by ones that
  1178. * don't switch on METHOD_* and which mostly avoid spinlocks
  1179. */
  1180. static int gpio_input(struct gpio_chip *chip, unsigned offset)
  1181. {
  1182. struct gpio_bank *bank;
  1183. unsigned long flags;
  1184. bank = container_of(chip, struct gpio_bank, chip);
  1185. spin_lock_irqsave(&bank->lock, flags);
  1186. _set_gpio_direction(bank, offset, 1);
  1187. spin_unlock_irqrestore(&bank->lock, flags);
  1188. return 0;
  1189. }
  1190. static int gpio_is_input(struct gpio_bank *bank, int mask)
  1191. {
  1192. void __iomem *reg = bank->base;
  1193. switch (bank->method) {
  1194. case METHOD_MPUIO:
  1195. reg += OMAP_MPUIO_IO_CNTL / bank->stride;
  1196. break;
  1197. case METHOD_GPIO_1510:
  1198. reg += OMAP1510_GPIO_DIR_CONTROL;
  1199. break;
  1200. case METHOD_GPIO_1610:
  1201. reg += OMAP1610_GPIO_DIRECTION;
  1202. break;
  1203. case METHOD_GPIO_7XX:
  1204. reg += OMAP7XX_GPIO_DIR_CONTROL;
  1205. break;
  1206. case METHOD_GPIO_24XX:
  1207. reg += OMAP24XX_GPIO_OE;
  1208. break;
  1209. case METHOD_GPIO_44XX:
  1210. reg += OMAP4_GPIO_OE;
  1211. break;
  1212. default:
  1213. WARN_ONCE(1, "gpio_is_input: incorrect OMAP GPIO method");
  1214. return -EINVAL;
  1215. }
  1216. return __raw_readl(reg) & mask;
  1217. }
  1218. static int gpio_get(struct gpio_chip *chip, unsigned offset)
  1219. {
  1220. struct gpio_bank *bank;
  1221. void __iomem *reg;
  1222. int gpio;
  1223. u32 mask;
  1224. gpio = chip->base + offset;
  1225. bank = get_gpio_bank(gpio);
  1226. reg = bank->base;
  1227. mask = 1 << get_gpio_index(gpio);
  1228. if (gpio_is_input(bank, mask))
  1229. return _get_gpio_datain(bank, gpio);
  1230. else
  1231. return _get_gpio_dataout(bank, gpio);
  1232. }
  1233. static int gpio_output(struct gpio_chip *chip, unsigned offset, int value)
  1234. {
  1235. struct gpio_bank *bank;
  1236. unsigned long flags;
  1237. bank = container_of(chip, struct gpio_bank, chip);
  1238. spin_lock_irqsave(&bank->lock, flags);
  1239. _set_gpio_dataout(bank, offset, value);
  1240. _set_gpio_direction(bank, offset, 0);
  1241. spin_unlock_irqrestore(&bank->lock, flags);
  1242. return 0;
  1243. }
  1244. static int gpio_debounce(struct gpio_chip *chip, unsigned offset,
  1245. unsigned debounce)
  1246. {
  1247. struct gpio_bank *bank;
  1248. unsigned long flags;
  1249. bank = container_of(chip, struct gpio_bank, chip);
  1250. if (!bank->dbck) {
  1251. bank->dbck = clk_get(bank->dev, "dbclk");
  1252. if (IS_ERR(bank->dbck))
  1253. dev_err(bank->dev, "Could not get gpio dbck\n");
  1254. }
  1255. spin_lock_irqsave(&bank->lock, flags);
  1256. _set_gpio_debounce(bank, offset, debounce);
  1257. spin_unlock_irqrestore(&bank->lock, flags);
  1258. return 0;
  1259. }
  1260. static void gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  1261. {
  1262. struct gpio_bank *bank;
  1263. unsigned long flags;
  1264. bank = container_of(chip, struct gpio_bank, chip);
  1265. spin_lock_irqsave(&bank->lock, flags);
  1266. _set_gpio_dataout(bank, offset, value);
  1267. spin_unlock_irqrestore(&bank->lock, flags);
  1268. }
  1269. static int gpio_2irq(struct gpio_chip *chip, unsigned offset)
  1270. {
  1271. struct gpio_bank *bank;
  1272. bank = container_of(chip, struct gpio_bank, chip);
  1273. return bank->virtual_irq_start + offset;
  1274. }
  1275. /*---------------------------------------------------------------------*/
  1276. static void __init omap_gpio_show_rev(struct gpio_bank *bank)
  1277. {
  1278. u32 rev;
  1279. if (cpu_is_omap16xx() && !(bank->method != METHOD_MPUIO))
  1280. rev = __raw_readw(bank->base + OMAP1610_GPIO_REVISION);
  1281. else if (cpu_is_omap24xx() || cpu_is_omap34xx())
  1282. rev = __raw_readl(bank->base + OMAP24XX_GPIO_REVISION);
  1283. else if (cpu_is_omap44xx())
  1284. rev = __raw_readl(bank->base + OMAP4_GPIO_REVISION);
  1285. else
  1286. return;
  1287. printk(KERN_INFO "OMAP GPIO hardware version %d.%d\n",
  1288. (rev >> 4) & 0x0f, rev & 0x0f);
  1289. }
  1290. /* This lock class tells lockdep that GPIO irqs are in a different
  1291. * category than their parents, so it won't report false recursion.
  1292. */
  1293. static struct lock_class_key gpio_lock_class;
  1294. static inline int init_gpio_info(struct platform_device *pdev)
  1295. {
  1296. /* TODO: Analyze removing gpio_bank_count usage from driver code */
  1297. gpio_bank = kzalloc(gpio_bank_count * sizeof(struct gpio_bank),
  1298. GFP_KERNEL);
  1299. if (!gpio_bank) {
  1300. dev_err(&pdev->dev, "Memory alloc failed for gpio_bank\n");
  1301. return -ENOMEM;
  1302. }
  1303. return 0;
  1304. }
  1305. /* TODO: Cleanup cpu_is_* checks */
  1306. static void omap_gpio_mod_init(struct gpio_bank *bank, int id)
  1307. {
  1308. if (cpu_class_is_omap2()) {
  1309. if (cpu_is_omap44xx()) {
  1310. __raw_writel(0xffffffff, bank->base +
  1311. OMAP4_GPIO_IRQSTATUSCLR0);
  1312. __raw_writel(0x00000000, bank->base +
  1313. OMAP4_GPIO_DEBOUNCENABLE);
  1314. /* Initialize interface clk ungated, module enabled */
  1315. __raw_writel(0, bank->base + OMAP4_GPIO_CTRL);
  1316. } else if (cpu_is_omap34xx()) {
  1317. __raw_writel(0x00000000, bank->base +
  1318. OMAP24XX_GPIO_IRQENABLE1);
  1319. __raw_writel(0xffffffff, bank->base +
  1320. OMAP24XX_GPIO_IRQSTATUS1);
  1321. __raw_writel(0x00000000, bank->base +
  1322. OMAP24XX_GPIO_DEBOUNCE_EN);
  1323. /* Initialize interface clk ungated, module enabled */
  1324. __raw_writel(0, bank->base + OMAP24XX_GPIO_CTRL);
  1325. } else if (cpu_is_omap24xx()) {
  1326. static const u32 non_wakeup_gpios[] = {
  1327. 0xe203ffc0, 0x08700040
  1328. };
  1329. if (id < ARRAY_SIZE(non_wakeup_gpios))
  1330. bank->non_wakeup_gpios = non_wakeup_gpios[id];
  1331. }
  1332. } else if (cpu_class_is_omap1()) {
  1333. if (bank_is_mpuio(bank))
  1334. __raw_writew(0xffff, bank->base +
  1335. OMAP_MPUIO_GPIO_MASKIT / bank->stride);
  1336. if (cpu_is_omap15xx() && bank->method == METHOD_GPIO_1510) {
  1337. __raw_writew(0xffff, bank->base
  1338. + OMAP1510_GPIO_INT_MASK);
  1339. __raw_writew(0x0000, bank->base
  1340. + OMAP1510_GPIO_INT_STATUS);
  1341. }
  1342. if (cpu_is_omap16xx() && bank->method == METHOD_GPIO_1610) {
  1343. __raw_writew(0x0000, bank->base
  1344. + OMAP1610_GPIO_IRQENABLE1);
  1345. __raw_writew(0xffff, bank->base
  1346. + OMAP1610_GPIO_IRQSTATUS1);
  1347. __raw_writew(0x0014, bank->base
  1348. + OMAP1610_GPIO_SYSCONFIG);
  1349. /*
  1350. * Enable system clock for GPIO module.
  1351. * The CAM_CLK_CTRL *is* really the right place.
  1352. */
  1353. omap_writel(omap_readl(ULPD_CAM_CLK_CTRL) | 0x04,
  1354. ULPD_CAM_CLK_CTRL);
  1355. }
  1356. if (cpu_is_omap7xx() && bank->method == METHOD_GPIO_7XX) {
  1357. __raw_writel(0xffffffff, bank->base
  1358. + OMAP7XX_GPIO_INT_MASK);
  1359. __raw_writel(0x00000000, bank->base
  1360. + OMAP7XX_GPIO_INT_STATUS);
  1361. }
  1362. }
  1363. }
  1364. static void __devinit omap_gpio_chip_init(struct gpio_bank *bank)
  1365. {
  1366. int j;
  1367. static int gpio;
  1368. bank->mod_usage = 0;
  1369. /*
  1370. * REVISIT eventually switch from OMAP-specific gpio structs
  1371. * over to the generic ones
  1372. */
  1373. bank->chip.request = omap_gpio_request;
  1374. bank->chip.free = omap_gpio_free;
  1375. bank->chip.direction_input = gpio_input;
  1376. bank->chip.get = gpio_get;
  1377. bank->chip.direction_output = gpio_output;
  1378. bank->chip.set_debounce = gpio_debounce;
  1379. bank->chip.set = gpio_set;
  1380. bank->chip.to_irq = gpio_2irq;
  1381. if (bank_is_mpuio(bank)) {
  1382. bank->chip.label = "mpuio";
  1383. #ifdef CONFIG_ARCH_OMAP16XX
  1384. bank->chip.dev = &omap_mpuio_device.dev;
  1385. #endif
  1386. bank->chip.base = OMAP_MPUIO(0);
  1387. } else {
  1388. bank->chip.label = "gpio";
  1389. bank->chip.base = gpio;
  1390. gpio += bank_width;
  1391. }
  1392. bank->chip.ngpio = bank_width;
  1393. gpiochip_add(&bank->chip);
  1394. for (j = bank->virtual_irq_start;
  1395. j < bank->virtual_irq_start + bank_width; j++) {
  1396. irq_set_lockdep_class(j, &gpio_lock_class);
  1397. irq_set_chip_data(j, bank);
  1398. if (bank_is_mpuio(bank))
  1399. irq_set_chip(j, &mpuio_irq_chip);
  1400. else
  1401. irq_set_chip(j, &gpio_irq_chip);
  1402. irq_set_handler(j, handle_simple_irq);
  1403. set_irq_flags(j, IRQF_VALID);
  1404. }
  1405. irq_set_chained_handler(bank->irq, gpio_irq_handler);
  1406. irq_set_handler_data(bank->irq, bank);
  1407. }
  1408. static int __devinit omap_gpio_probe(struct platform_device *pdev)
  1409. {
  1410. static int gpio_init_done;
  1411. struct omap_gpio_platform_data *pdata;
  1412. struct resource *res;
  1413. int id;
  1414. struct gpio_bank *bank;
  1415. if (!pdev->dev.platform_data)
  1416. return -EINVAL;
  1417. pdata = pdev->dev.platform_data;
  1418. if (!gpio_init_done) {
  1419. int ret;
  1420. ret = init_gpio_info(pdev);
  1421. if (ret)
  1422. return ret;
  1423. }
  1424. id = pdev->id;
  1425. bank = &gpio_bank[id];
  1426. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1427. if (unlikely(!res)) {
  1428. dev_err(&pdev->dev, "GPIO Bank %i Invalid IRQ resource\n", id);
  1429. return -ENODEV;
  1430. }
  1431. bank->irq = res->start;
  1432. bank->virtual_irq_start = pdata->virtual_irq_start;
  1433. bank->method = pdata->bank_type;
  1434. bank->dev = &pdev->dev;
  1435. bank->dbck_flag = pdata->dbck_flag;
  1436. bank->stride = pdata->bank_stride;
  1437. bank_width = pdata->bank_width;
  1438. spin_lock_init(&bank->lock);
  1439. /* Static mapping, never released */
  1440. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1441. if (unlikely(!res)) {
  1442. dev_err(&pdev->dev, "GPIO Bank %i Invalid mem resource\n", id);
  1443. return -ENODEV;
  1444. }
  1445. bank->base = ioremap(res->start, resource_size(res));
  1446. if (!bank->base) {
  1447. dev_err(&pdev->dev, "Could not ioremap gpio bank%i\n", id);
  1448. return -ENOMEM;
  1449. }
  1450. pm_runtime_enable(bank->dev);
  1451. pm_runtime_get_sync(bank->dev);
  1452. omap_gpio_mod_init(bank, id);
  1453. omap_gpio_chip_init(bank);
  1454. omap_gpio_show_rev(bank);
  1455. if (!gpio_init_done)
  1456. gpio_init_done = 1;
  1457. return 0;
  1458. }
  1459. #if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP2PLUS)
  1460. static int omap_gpio_suspend(void)
  1461. {
  1462. int i;
  1463. if (!cpu_class_is_omap2() && !cpu_is_omap16xx())
  1464. return 0;
  1465. for (i = 0; i < gpio_bank_count; i++) {
  1466. struct gpio_bank *bank = &gpio_bank[i];
  1467. void __iomem *wake_status;
  1468. void __iomem *wake_clear;
  1469. void __iomem *wake_set;
  1470. unsigned long flags;
  1471. switch (bank->method) {
  1472. #ifdef CONFIG_ARCH_OMAP16XX
  1473. case METHOD_GPIO_1610:
  1474. wake_status = bank->base + OMAP1610_GPIO_WAKEUPENABLE;
  1475. wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
  1476. wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
  1477. break;
  1478. #endif
  1479. #if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
  1480. case METHOD_GPIO_24XX:
  1481. wake_status = bank->base + OMAP24XX_GPIO_WAKE_EN;
  1482. wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
  1483. wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
  1484. break;
  1485. #endif
  1486. #ifdef CONFIG_ARCH_OMAP4
  1487. case METHOD_GPIO_44XX:
  1488. wake_status = bank->base + OMAP4_GPIO_IRQWAKEN0;
  1489. wake_clear = bank->base + OMAP4_GPIO_IRQWAKEN0;
  1490. wake_set = bank->base + OMAP4_GPIO_IRQWAKEN0;
  1491. break;
  1492. #endif
  1493. default:
  1494. continue;
  1495. }
  1496. spin_lock_irqsave(&bank->lock, flags);
  1497. bank->saved_wakeup = __raw_readl(wake_status);
  1498. __raw_writel(0xffffffff, wake_clear);
  1499. __raw_writel(bank->suspend_wakeup, wake_set);
  1500. spin_unlock_irqrestore(&bank->lock, flags);
  1501. }
  1502. return 0;
  1503. }
  1504. static void omap_gpio_resume(void)
  1505. {
  1506. int i;
  1507. if (!cpu_class_is_omap2() && !cpu_is_omap16xx())
  1508. return;
  1509. for (i = 0; i < gpio_bank_count; i++) {
  1510. struct gpio_bank *bank = &gpio_bank[i];
  1511. void __iomem *wake_clear;
  1512. void __iomem *wake_set;
  1513. unsigned long flags;
  1514. switch (bank->method) {
  1515. #ifdef CONFIG_ARCH_OMAP16XX
  1516. case METHOD_GPIO_1610:
  1517. wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
  1518. wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
  1519. break;
  1520. #endif
  1521. #if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
  1522. case METHOD_GPIO_24XX:
  1523. wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
  1524. wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
  1525. break;
  1526. #endif
  1527. #ifdef CONFIG_ARCH_OMAP4
  1528. case METHOD_GPIO_44XX:
  1529. wake_clear = bank->base + OMAP4_GPIO_IRQWAKEN0;
  1530. wake_set = bank->base + OMAP4_GPIO_IRQWAKEN0;
  1531. break;
  1532. #endif
  1533. default:
  1534. continue;
  1535. }
  1536. spin_lock_irqsave(&bank->lock, flags);
  1537. __raw_writel(0xffffffff, wake_clear);
  1538. __raw_writel(bank->saved_wakeup, wake_set);
  1539. spin_unlock_irqrestore(&bank->lock, flags);
  1540. }
  1541. }
  1542. static struct syscore_ops omap_gpio_syscore_ops = {
  1543. .suspend = omap_gpio_suspend,
  1544. .resume = omap_gpio_resume,
  1545. };
  1546. #endif
  1547. #ifdef CONFIG_ARCH_OMAP2PLUS
  1548. static int workaround_enabled;
  1549. void omap2_gpio_prepare_for_idle(int off_mode)
  1550. {
  1551. int i, c = 0;
  1552. int min = 0;
  1553. if (cpu_is_omap34xx())
  1554. min = 1;
  1555. for (i = min; i < gpio_bank_count; i++) {
  1556. struct gpio_bank *bank = &gpio_bank[i];
  1557. u32 l1 = 0, l2 = 0;
  1558. int j;
  1559. for (j = 0; j < hweight_long(bank->dbck_enable_mask); j++)
  1560. clk_disable(bank->dbck);
  1561. if (!off_mode)
  1562. continue;
  1563. /* If going to OFF, remove triggering for all
  1564. * non-wakeup GPIOs. Otherwise spurious IRQs will be
  1565. * generated. See OMAP2420 Errata item 1.101. */
  1566. if (!(bank->enabled_non_wakeup_gpios))
  1567. continue;
  1568. if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
  1569. bank->saved_datain = __raw_readl(bank->base +
  1570. OMAP24XX_GPIO_DATAIN);
  1571. l1 = __raw_readl(bank->base +
  1572. OMAP24XX_GPIO_FALLINGDETECT);
  1573. l2 = __raw_readl(bank->base +
  1574. OMAP24XX_GPIO_RISINGDETECT);
  1575. }
  1576. if (cpu_is_omap44xx()) {
  1577. bank->saved_datain = __raw_readl(bank->base +
  1578. OMAP4_GPIO_DATAIN);
  1579. l1 = __raw_readl(bank->base +
  1580. OMAP4_GPIO_FALLINGDETECT);
  1581. l2 = __raw_readl(bank->base +
  1582. OMAP4_GPIO_RISINGDETECT);
  1583. }
  1584. bank->saved_fallingdetect = l1;
  1585. bank->saved_risingdetect = l2;
  1586. l1 &= ~bank->enabled_non_wakeup_gpios;
  1587. l2 &= ~bank->enabled_non_wakeup_gpios;
  1588. if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
  1589. __raw_writel(l1, bank->base +
  1590. OMAP24XX_GPIO_FALLINGDETECT);
  1591. __raw_writel(l2, bank->base +
  1592. OMAP24XX_GPIO_RISINGDETECT);
  1593. }
  1594. if (cpu_is_omap44xx()) {
  1595. __raw_writel(l1, bank->base + OMAP4_GPIO_FALLINGDETECT);
  1596. __raw_writel(l2, bank->base + OMAP4_GPIO_RISINGDETECT);
  1597. }
  1598. c++;
  1599. }
  1600. if (!c) {
  1601. workaround_enabled = 0;
  1602. return;
  1603. }
  1604. workaround_enabled = 1;
  1605. }
  1606. void omap2_gpio_resume_after_idle(void)
  1607. {
  1608. int i;
  1609. int min = 0;
  1610. if (cpu_is_omap34xx())
  1611. min = 1;
  1612. for (i = min; i < gpio_bank_count; i++) {
  1613. struct gpio_bank *bank = &gpio_bank[i];
  1614. u32 l = 0, gen, gen0, gen1;
  1615. int j;
  1616. for (j = 0; j < hweight_long(bank->dbck_enable_mask); j++)
  1617. clk_enable(bank->dbck);
  1618. if (!workaround_enabled)
  1619. continue;
  1620. if (!(bank->enabled_non_wakeup_gpios))
  1621. continue;
  1622. if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
  1623. __raw_writel(bank->saved_fallingdetect,
  1624. bank->base + OMAP24XX_GPIO_FALLINGDETECT);
  1625. __raw_writel(bank->saved_risingdetect,
  1626. bank->base + OMAP24XX_GPIO_RISINGDETECT);
  1627. l = __raw_readl(bank->base + OMAP24XX_GPIO_DATAIN);
  1628. }
  1629. if (cpu_is_omap44xx()) {
  1630. __raw_writel(bank->saved_fallingdetect,
  1631. bank->base + OMAP4_GPIO_FALLINGDETECT);
  1632. __raw_writel(bank->saved_risingdetect,
  1633. bank->base + OMAP4_GPIO_RISINGDETECT);
  1634. l = __raw_readl(bank->base + OMAP4_GPIO_DATAIN);
  1635. }
  1636. /* Check if any of the non-wakeup interrupt GPIOs have changed
  1637. * state. If so, generate an IRQ by software. This is
  1638. * horribly racy, but it's the best we can do to work around
  1639. * this silicon bug. */
  1640. l ^= bank->saved_datain;
  1641. l &= bank->enabled_non_wakeup_gpios;
  1642. /*
  1643. * No need to generate IRQs for the rising edge for gpio IRQs
  1644. * configured with falling edge only; and vice versa.
  1645. */
  1646. gen0 = l & bank->saved_fallingdetect;
  1647. gen0 &= bank->saved_datain;
  1648. gen1 = l & bank->saved_risingdetect;
  1649. gen1 &= ~(bank->saved_datain);
  1650. /* FIXME: Consider GPIO IRQs with level detections properly! */
  1651. gen = l & (~(bank->saved_fallingdetect) &
  1652. ~(bank->saved_risingdetect));
  1653. /* Consider all GPIO IRQs needed to be updated */
  1654. gen |= gen0 | gen1;
  1655. if (gen) {
  1656. u32 old0, old1;
  1657. if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
  1658. old0 = __raw_readl(bank->base +
  1659. OMAP24XX_GPIO_LEVELDETECT0);
  1660. old1 = __raw_readl(bank->base +
  1661. OMAP24XX_GPIO_LEVELDETECT1);
  1662. __raw_writel(old0 | gen, bank->base +
  1663. OMAP24XX_GPIO_LEVELDETECT0);
  1664. __raw_writel(old1 | gen, bank->base +
  1665. OMAP24XX_GPIO_LEVELDETECT1);
  1666. __raw_writel(old0, bank->base +
  1667. OMAP24XX_GPIO_LEVELDETECT0);
  1668. __raw_writel(old1, bank->base +
  1669. OMAP24XX_GPIO_LEVELDETECT1);
  1670. }
  1671. if (cpu_is_omap44xx()) {
  1672. old0 = __raw_readl(bank->base +
  1673. OMAP4_GPIO_LEVELDETECT0);
  1674. old1 = __raw_readl(bank->base +
  1675. OMAP4_GPIO_LEVELDETECT1);
  1676. __raw_writel(old0 | l, bank->base +
  1677. OMAP4_GPIO_LEVELDETECT0);
  1678. __raw_writel(old1 | l, bank->base +
  1679. OMAP4_GPIO_LEVELDETECT1);
  1680. __raw_writel(old0, bank->base +
  1681. OMAP4_GPIO_LEVELDETECT0);
  1682. __raw_writel(old1, bank->base +
  1683. OMAP4_GPIO_LEVELDETECT1);
  1684. }
  1685. }
  1686. }
  1687. }
  1688. #endif
  1689. #ifdef CONFIG_ARCH_OMAP3
  1690. /* save the registers of bank 2-6 */
  1691. void omap_gpio_save_context(void)
  1692. {
  1693. int i;
  1694. /* saving banks from 2-6 only since GPIO1 is in WKUP */
  1695. for (i = 1; i < gpio_bank_count; i++) {
  1696. struct gpio_bank *bank = &gpio_bank[i];
  1697. gpio_context[i].irqenable1 =
  1698. __raw_readl(bank->base + OMAP24XX_GPIO_IRQENABLE1);
  1699. gpio_context[i].irqenable2 =
  1700. __raw_readl(bank->base + OMAP24XX_GPIO_IRQENABLE2);
  1701. gpio_context[i].wake_en =
  1702. __raw_readl(bank->base + OMAP24XX_GPIO_WAKE_EN);
  1703. gpio_context[i].ctrl =
  1704. __raw_readl(bank->base + OMAP24XX_GPIO_CTRL);
  1705. gpio_context[i].oe =
  1706. __raw_readl(bank->base + OMAP24XX_GPIO_OE);
  1707. gpio_context[i].leveldetect0 =
  1708. __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT0);
  1709. gpio_context[i].leveldetect1 =
  1710. __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT1);
  1711. gpio_context[i].risingdetect =
  1712. __raw_readl(bank->base + OMAP24XX_GPIO_RISINGDETECT);
  1713. gpio_context[i].fallingdetect =
  1714. __raw_readl(bank->base + OMAP24XX_GPIO_FALLINGDETECT);
  1715. gpio_context[i].dataout =
  1716. __raw_readl(bank->base + OMAP24XX_GPIO_DATAOUT);
  1717. }
  1718. }
  1719. /* restore the required registers of bank 2-6 */
  1720. void omap_gpio_restore_context(void)
  1721. {
  1722. int i;
  1723. for (i = 1; i < gpio_bank_count; i++) {
  1724. struct gpio_bank *bank = &gpio_bank[i];
  1725. __raw_writel(gpio_context[i].irqenable1,
  1726. bank->base + OMAP24XX_GPIO_IRQENABLE1);
  1727. __raw_writel(gpio_context[i].irqenable2,
  1728. bank->base + OMAP24XX_GPIO_IRQENABLE2);
  1729. __raw_writel(gpio_context[i].wake_en,
  1730. bank->base + OMAP24XX_GPIO_WAKE_EN);
  1731. __raw_writel(gpio_context[i].ctrl,
  1732. bank->base + OMAP24XX_GPIO_CTRL);
  1733. __raw_writel(gpio_context[i].oe,
  1734. bank->base + OMAP24XX_GPIO_OE);
  1735. __raw_writel(gpio_context[i].leveldetect0,
  1736. bank->base + OMAP24XX_GPIO_LEVELDETECT0);
  1737. __raw_writel(gpio_context[i].leveldetect1,
  1738. bank->base + OMAP24XX_GPIO_LEVELDETECT1);
  1739. __raw_writel(gpio_context[i].risingdetect,
  1740. bank->base + OMAP24XX_GPIO_RISINGDETECT);
  1741. __raw_writel(gpio_context[i].fallingdetect,
  1742. bank->base + OMAP24XX_GPIO_FALLINGDETECT);
  1743. __raw_writel(gpio_context[i].dataout,
  1744. bank->base + OMAP24XX_GPIO_DATAOUT);
  1745. }
  1746. }
  1747. #endif
  1748. static struct platform_driver omap_gpio_driver = {
  1749. .probe = omap_gpio_probe,
  1750. .driver = {
  1751. .name = "omap_gpio",
  1752. },
  1753. };
  1754. /*
  1755. * gpio driver register needs to be done before
  1756. * machine_init functions access gpio APIs.
  1757. * Hence omap_gpio_drv_reg() is a postcore_initcall.
  1758. */
  1759. static int __init omap_gpio_drv_reg(void)
  1760. {
  1761. return platform_driver_register(&omap_gpio_driver);
  1762. }
  1763. postcore_initcall(omap_gpio_drv_reg);
  1764. static int __init omap_gpio_sysinit(void)
  1765. {
  1766. mpuio_init();
  1767. #if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP2PLUS)
  1768. if (cpu_is_omap16xx() || cpu_class_is_omap2())
  1769. register_syscore_ops(&omap_gpio_syscore_ops);
  1770. #endif
  1771. return 0;
  1772. }
  1773. arch_initcall(omap_gpio_sysinit);