gpio-omap.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546
  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/device.h>
  22. #include <linux/pm_runtime.h>
  23. #include <linux/pm.h>
  24. #include <linux/of.h>
  25. #include <linux/of_device.h>
  26. #include <linux/irqdomain.h>
  27. #include <mach/hardware.h>
  28. #include <asm/irq.h>
  29. #include <mach/irqs.h>
  30. #include <asm/gpio.h>
  31. #include <asm/mach/irq.h>
  32. #define OFF_MODE 1
  33. static LIST_HEAD(omap_gpio_list);
  34. struct gpio_regs {
  35. u32 irqenable1;
  36. u32 irqenable2;
  37. u32 wake_en;
  38. u32 ctrl;
  39. u32 oe;
  40. u32 leveldetect0;
  41. u32 leveldetect1;
  42. u32 risingdetect;
  43. u32 fallingdetect;
  44. u32 dataout;
  45. u32 debounce;
  46. u32 debounce_en;
  47. };
  48. struct gpio_bank {
  49. struct list_head node;
  50. void __iomem *base;
  51. u16 irq;
  52. int irq_base;
  53. struct irq_domain *domain;
  54. u32 suspend_wakeup;
  55. u32 saved_wakeup;
  56. u32 non_wakeup_gpios;
  57. u32 enabled_non_wakeup_gpios;
  58. struct gpio_regs context;
  59. u32 saved_datain;
  60. u32 saved_fallingdetect;
  61. u32 saved_risingdetect;
  62. u32 level_mask;
  63. u32 toggle_mask;
  64. spinlock_t lock;
  65. struct gpio_chip chip;
  66. struct clk *dbck;
  67. u32 mod_usage;
  68. u32 dbck_enable_mask;
  69. bool dbck_enabled;
  70. struct device *dev;
  71. bool is_mpuio;
  72. bool dbck_flag;
  73. bool loses_context;
  74. int stride;
  75. u32 width;
  76. int context_loss_count;
  77. int power_mode;
  78. bool workaround_enabled;
  79. void (*set_dataout)(struct gpio_bank *bank, int gpio, int enable);
  80. int (*get_context_loss_count)(struct device *dev);
  81. struct omap_gpio_reg_offs *regs;
  82. };
  83. #define GPIO_INDEX(bank, gpio) (gpio % bank->width)
  84. #define GPIO_BIT(bank, gpio) (1 << GPIO_INDEX(bank, gpio))
  85. #define GPIO_MOD_CTRL_BIT BIT(0)
  86. static int irq_to_gpio(struct gpio_bank *bank, unsigned int gpio_irq)
  87. {
  88. return gpio_irq - bank->irq_base + bank->chip.base;
  89. }
  90. static void _set_gpio_direction(struct gpio_bank *bank, int gpio, int is_input)
  91. {
  92. void __iomem *reg = bank->base;
  93. u32 l;
  94. reg += bank->regs->direction;
  95. l = __raw_readl(reg);
  96. if (is_input)
  97. l |= 1 << gpio;
  98. else
  99. l &= ~(1 << gpio);
  100. __raw_writel(l, reg);
  101. bank->context.oe = l;
  102. }
  103. /* set data out value using dedicate set/clear register */
  104. static void _set_gpio_dataout_reg(struct gpio_bank *bank, int gpio, int enable)
  105. {
  106. void __iomem *reg = bank->base;
  107. u32 l = GPIO_BIT(bank, gpio);
  108. if (enable) {
  109. reg += bank->regs->set_dataout;
  110. bank->context.dataout |= l;
  111. } else {
  112. reg += bank->regs->clr_dataout;
  113. bank->context.dataout &= ~l;
  114. }
  115. __raw_writel(l, reg);
  116. }
  117. /* set data out value using mask register */
  118. static void _set_gpio_dataout_mask(struct gpio_bank *bank, int gpio, int enable)
  119. {
  120. void __iomem *reg = bank->base + bank->regs->dataout;
  121. u32 gpio_bit = GPIO_BIT(bank, gpio);
  122. u32 l;
  123. l = __raw_readl(reg);
  124. if (enable)
  125. l |= gpio_bit;
  126. else
  127. l &= ~gpio_bit;
  128. __raw_writel(l, reg);
  129. bank->context.dataout = l;
  130. }
  131. static int _get_gpio_datain(struct gpio_bank *bank, int offset)
  132. {
  133. void __iomem *reg = bank->base + bank->regs->datain;
  134. return (__raw_readl(reg) & (1 << offset)) != 0;
  135. }
  136. static int _get_gpio_dataout(struct gpio_bank *bank, int offset)
  137. {
  138. void __iomem *reg = bank->base + bank->regs->dataout;
  139. return (__raw_readl(reg) & (1 << offset)) != 0;
  140. }
  141. static inline void _gpio_rmw(void __iomem *base, u32 reg, u32 mask, bool set)
  142. {
  143. int l = __raw_readl(base + reg);
  144. if (set)
  145. l |= mask;
  146. else
  147. l &= ~mask;
  148. __raw_writel(l, base + reg);
  149. }
  150. static inline void _gpio_dbck_enable(struct gpio_bank *bank)
  151. {
  152. if (bank->dbck_enable_mask && !bank->dbck_enabled) {
  153. clk_enable(bank->dbck);
  154. bank->dbck_enabled = true;
  155. }
  156. }
  157. static inline void _gpio_dbck_disable(struct gpio_bank *bank)
  158. {
  159. if (bank->dbck_enable_mask && bank->dbck_enabled) {
  160. clk_disable(bank->dbck);
  161. bank->dbck_enabled = false;
  162. }
  163. }
  164. /**
  165. * _set_gpio_debounce - low level gpio debounce time
  166. * @bank: the gpio bank we're acting upon
  167. * @gpio: the gpio number on this @gpio
  168. * @debounce: debounce time to use
  169. *
  170. * OMAP's debounce time is in 31us steps so we need
  171. * to convert and round up to the closest unit.
  172. */
  173. static void _set_gpio_debounce(struct gpio_bank *bank, unsigned gpio,
  174. unsigned debounce)
  175. {
  176. void __iomem *reg;
  177. u32 val;
  178. u32 l;
  179. if (!bank->dbck_flag)
  180. return;
  181. if (debounce < 32)
  182. debounce = 0x01;
  183. else if (debounce > 7936)
  184. debounce = 0xff;
  185. else
  186. debounce = (debounce / 0x1f) - 1;
  187. l = GPIO_BIT(bank, gpio);
  188. clk_enable(bank->dbck);
  189. reg = bank->base + bank->regs->debounce;
  190. __raw_writel(debounce, reg);
  191. reg = bank->base + bank->regs->debounce_en;
  192. val = __raw_readl(reg);
  193. if (debounce)
  194. val |= l;
  195. else
  196. val &= ~l;
  197. bank->dbck_enable_mask = val;
  198. __raw_writel(val, reg);
  199. clk_disable(bank->dbck);
  200. /*
  201. * Enable debounce clock per module.
  202. * This call is mandatory because in omap_gpio_request() when
  203. * *_runtime_get_sync() is called, _gpio_dbck_enable() within
  204. * runtime callbck fails to turn on dbck because dbck_enable_mask
  205. * used within _gpio_dbck_enable() is still not initialized at
  206. * that point. Therefore we have to enable dbck here.
  207. */
  208. _gpio_dbck_enable(bank);
  209. if (bank->dbck_enable_mask) {
  210. bank->context.debounce = debounce;
  211. bank->context.debounce_en = val;
  212. }
  213. }
  214. static inline void set_gpio_trigger(struct gpio_bank *bank, int gpio,
  215. unsigned trigger)
  216. {
  217. void __iomem *base = bank->base;
  218. u32 gpio_bit = 1 << gpio;
  219. _gpio_rmw(base, bank->regs->leveldetect0, gpio_bit,
  220. trigger & IRQ_TYPE_LEVEL_LOW);
  221. _gpio_rmw(base, bank->regs->leveldetect1, gpio_bit,
  222. trigger & IRQ_TYPE_LEVEL_HIGH);
  223. _gpio_rmw(base, bank->regs->risingdetect, gpio_bit,
  224. trigger & IRQ_TYPE_EDGE_RISING);
  225. _gpio_rmw(base, bank->regs->fallingdetect, gpio_bit,
  226. trigger & IRQ_TYPE_EDGE_FALLING);
  227. bank->context.leveldetect0 =
  228. __raw_readl(bank->base + bank->regs->leveldetect0);
  229. bank->context.leveldetect1 =
  230. __raw_readl(bank->base + bank->regs->leveldetect1);
  231. bank->context.risingdetect =
  232. __raw_readl(bank->base + bank->regs->risingdetect);
  233. bank->context.fallingdetect =
  234. __raw_readl(bank->base + bank->regs->fallingdetect);
  235. if (likely(!(bank->non_wakeup_gpios & gpio_bit))) {
  236. _gpio_rmw(base, bank->regs->wkup_en, gpio_bit, trigger != 0);
  237. bank->context.wake_en =
  238. __raw_readl(bank->base + bank->regs->wkup_en);
  239. }
  240. /* This part needs to be executed always for OMAP{34xx, 44xx} */
  241. if (!bank->regs->irqctrl) {
  242. /* On omap24xx proceed only when valid GPIO bit is set */
  243. if (bank->non_wakeup_gpios) {
  244. if (!(bank->non_wakeup_gpios & gpio_bit))
  245. goto exit;
  246. }
  247. /*
  248. * Log the edge gpio and manually trigger the IRQ
  249. * after resume if the input level changes
  250. * to avoid irq lost during PER RET/OFF mode
  251. * Applies for omap2 non-wakeup gpio and all omap3 gpios
  252. */
  253. if (trigger & IRQ_TYPE_EDGE_BOTH)
  254. bank->enabled_non_wakeup_gpios |= gpio_bit;
  255. else
  256. bank->enabled_non_wakeup_gpios &= ~gpio_bit;
  257. }
  258. exit:
  259. bank->level_mask =
  260. __raw_readl(bank->base + bank->regs->leveldetect0) |
  261. __raw_readl(bank->base + bank->regs->leveldetect1);
  262. }
  263. #ifdef CONFIG_ARCH_OMAP1
  264. /*
  265. * This only applies to chips that can't do both rising and falling edge
  266. * detection at once. For all other chips, this function is a noop.
  267. */
  268. static void _toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio)
  269. {
  270. void __iomem *reg = bank->base;
  271. u32 l = 0;
  272. if (!bank->regs->irqctrl)
  273. return;
  274. reg += bank->regs->irqctrl;
  275. l = __raw_readl(reg);
  276. if ((l >> gpio) & 1)
  277. l &= ~(1 << gpio);
  278. else
  279. l |= 1 << gpio;
  280. __raw_writel(l, reg);
  281. }
  282. #else
  283. static void _toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio) {}
  284. #endif
  285. static int _set_gpio_triggering(struct gpio_bank *bank, int gpio,
  286. unsigned trigger)
  287. {
  288. void __iomem *reg = bank->base;
  289. void __iomem *base = bank->base;
  290. u32 l = 0;
  291. if (bank->regs->leveldetect0 && bank->regs->wkup_en) {
  292. set_gpio_trigger(bank, gpio, trigger);
  293. } else if (bank->regs->irqctrl) {
  294. reg += bank->regs->irqctrl;
  295. l = __raw_readl(reg);
  296. if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
  297. bank->toggle_mask |= 1 << gpio;
  298. if (trigger & IRQ_TYPE_EDGE_RISING)
  299. l |= 1 << gpio;
  300. else if (trigger & IRQ_TYPE_EDGE_FALLING)
  301. l &= ~(1 << gpio);
  302. else
  303. return -EINVAL;
  304. __raw_writel(l, reg);
  305. } else if (bank->regs->edgectrl1) {
  306. if (gpio & 0x08)
  307. reg += bank->regs->edgectrl2;
  308. else
  309. reg += bank->regs->edgectrl1;
  310. gpio &= 0x07;
  311. l = __raw_readl(reg);
  312. l &= ~(3 << (gpio << 1));
  313. if (trigger & IRQ_TYPE_EDGE_RISING)
  314. l |= 2 << (gpio << 1);
  315. if (trigger & IRQ_TYPE_EDGE_FALLING)
  316. l |= 1 << (gpio << 1);
  317. /* Enable wake-up during idle for dynamic tick */
  318. _gpio_rmw(base, bank->regs->wkup_en, 1 << gpio, trigger);
  319. bank->context.wake_en =
  320. __raw_readl(bank->base + bank->regs->wkup_en);
  321. __raw_writel(l, reg);
  322. }
  323. return 0;
  324. }
  325. static int gpio_irq_type(struct irq_data *d, unsigned type)
  326. {
  327. struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
  328. unsigned gpio;
  329. int retval;
  330. unsigned long flags;
  331. if (!cpu_class_is_omap2() && d->irq > IH_MPUIO_BASE)
  332. gpio = OMAP_MPUIO(d->irq - IH_MPUIO_BASE);
  333. else
  334. gpio = irq_to_gpio(bank, d->irq);
  335. if (type & ~IRQ_TYPE_SENSE_MASK)
  336. return -EINVAL;
  337. if (!bank->regs->leveldetect0 &&
  338. (type & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH)))
  339. return -EINVAL;
  340. spin_lock_irqsave(&bank->lock, flags);
  341. retval = _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), type);
  342. spin_unlock_irqrestore(&bank->lock, flags);
  343. if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
  344. __irq_set_handler_locked(d->irq, handle_level_irq);
  345. else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
  346. __irq_set_handler_locked(d->irq, handle_edge_irq);
  347. return retval;
  348. }
  349. static void _clear_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
  350. {
  351. void __iomem *reg = bank->base;
  352. reg += bank->regs->irqstatus;
  353. __raw_writel(gpio_mask, reg);
  354. /* Workaround for clearing DSP GPIO interrupts to allow retention */
  355. if (bank->regs->irqstatus2) {
  356. reg = bank->base + bank->regs->irqstatus2;
  357. __raw_writel(gpio_mask, reg);
  358. }
  359. /* Flush posted write for the irq status to avoid spurious interrupts */
  360. __raw_readl(reg);
  361. }
  362. static inline void _clear_gpio_irqstatus(struct gpio_bank *bank, int gpio)
  363. {
  364. _clear_gpio_irqbank(bank, GPIO_BIT(bank, gpio));
  365. }
  366. static u32 _get_gpio_irqbank_mask(struct gpio_bank *bank)
  367. {
  368. void __iomem *reg = bank->base;
  369. u32 l;
  370. u32 mask = (1 << bank->width) - 1;
  371. reg += bank->regs->irqenable;
  372. l = __raw_readl(reg);
  373. if (bank->regs->irqenable_inv)
  374. l = ~l;
  375. l &= mask;
  376. return l;
  377. }
  378. static void _enable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
  379. {
  380. void __iomem *reg = bank->base;
  381. u32 l;
  382. if (bank->regs->set_irqenable) {
  383. reg += bank->regs->set_irqenable;
  384. l = gpio_mask;
  385. bank->context.irqenable1 |= gpio_mask;
  386. } else {
  387. reg += bank->regs->irqenable;
  388. l = __raw_readl(reg);
  389. if (bank->regs->irqenable_inv)
  390. l &= ~gpio_mask;
  391. else
  392. l |= gpio_mask;
  393. bank->context.irqenable1 = l;
  394. }
  395. __raw_writel(l, reg);
  396. }
  397. static void _disable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
  398. {
  399. void __iomem *reg = bank->base;
  400. u32 l;
  401. if (bank->regs->clr_irqenable) {
  402. reg += bank->regs->clr_irqenable;
  403. l = gpio_mask;
  404. bank->context.irqenable1 &= ~gpio_mask;
  405. } else {
  406. reg += bank->regs->irqenable;
  407. l = __raw_readl(reg);
  408. if (bank->regs->irqenable_inv)
  409. l |= gpio_mask;
  410. else
  411. l &= ~gpio_mask;
  412. bank->context.irqenable1 = l;
  413. }
  414. __raw_writel(l, reg);
  415. }
  416. static inline void _set_gpio_irqenable(struct gpio_bank *bank, int gpio, int enable)
  417. {
  418. if (enable)
  419. _enable_gpio_irqbank(bank, GPIO_BIT(bank, gpio));
  420. else
  421. _disable_gpio_irqbank(bank, GPIO_BIT(bank, gpio));
  422. }
  423. /*
  424. * Note that ENAWAKEUP needs to be enabled in GPIO_SYSCONFIG register.
  425. * 1510 does not seem to have a wake-up register. If JTAG is connected
  426. * to the target, system will wake up always on GPIO events. While
  427. * system is running all registered GPIO interrupts need to have wake-up
  428. * enabled. When system is suspended, only selected GPIO interrupts need
  429. * to have wake-up enabled.
  430. */
  431. static int _set_gpio_wakeup(struct gpio_bank *bank, int gpio, int enable)
  432. {
  433. u32 gpio_bit = GPIO_BIT(bank, gpio);
  434. unsigned long flags;
  435. if (bank->non_wakeup_gpios & gpio_bit) {
  436. dev_err(bank->dev,
  437. "Unable to modify wakeup on non-wakeup GPIO%d\n", gpio);
  438. return -EINVAL;
  439. }
  440. spin_lock_irqsave(&bank->lock, flags);
  441. if (enable)
  442. bank->suspend_wakeup |= gpio_bit;
  443. else
  444. bank->suspend_wakeup &= ~gpio_bit;
  445. __raw_writel(bank->suspend_wakeup, bank->base + bank->regs->wkup_en);
  446. spin_unlock_irqrestore(&bank->lock, flags);
  447. return 0;
  448. }
  449. static void _reset_gpio(struct gpio_bank *bank, int gpio)
  450. {
  451. _set_gpio_direction(bank, GPIO_INDEX(bank, gpio), 1);
  452. _set_gpio_irqenable(bank, gpio, 0);
  453. _clear_gpio_irqstatus(bank, gpio);
  454. _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), IRQ_TYPE_NONE);
  455. }
  456. /* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
  457. static int gpio_wake_enable(struct irq_data *d, unsigned int enable)
  458. {
  459. struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
  460. unsigned int gpio = irq_to_gpio(bank, d->irq);
  461. return _set_gpio_wakeup(bank, gpio, enable);
  462. }
  463. static int omap_gpio_request(struct gpio_chip *chip, unsigned offset)
  464. {
  465. struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
  466. unsigned long flags;
  467. /*
  468. * If this is the first gpio_request for the bank,
  469. * enable the bank module.
  470. */
  471. if (!bank->mod_usage)
  472. pm_runtime_get_sync(bank->dev);
  473. spin_lock_irqsave(&bank->lock, flags);
  474. /* Set trigger to none. You need to enable the desired trigger with
  475. * request_irq() or set_irq_type().
  476. */
  477. _set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
  478. if (bank->regs->pinctrl) {
  479. void __iomem *reg = bank->base + bank->regs->pinctrl;
  480. /* Claim the pin for MPU */
  481. __raw_writel(__raw_readl(reg) | (1 << offset), reg);
  482. }
  483. if (bank->regs->ctrl && !bank->mod_usage) {
  484. void __iomem *reg = bank->base + bank->regs->ctrl;
  485. u32 ctrl;
  486. ctrl = __raw_readl(reg);
  487. /* Module is enabled, clocks are not gated */
  488. ctrl &= ~GPIO_MOD_CTRL_BIT;
  489. __raw_writel(ctrl, reg);
  490. bank->context.ctrl = ctrl;
  491. }
  492. bank->mod_usage |= 1 << offset;
  493. spin_unlock_irqrestore(&bank->lock, flags);
  494. return 0;
  495. }
  496. static void omap_gpio_free(struct gpio_chip *chip, unsigned offset)
  497. {
  498. struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
  499. void __iomem *base = bank->base;
  500. unsigned long flags;
  501. spin_lock_irqsave(&bank->lock, flags);
  502. if (bank->regs->wkup_en) {
  503. /* Disable wake-up during idle for dynamic tick */
  504. _gpio_rmw(base, bank->regs->wkup_en, 1 << offset, 0);
  505. bank->context.wake_en =
  506. __raw_readl(bank->base + bank->regs->wkup_en);
  507. }
  508. bank->mod_usage &= ~(1 << offset);
  509. if (bank->regs->ctrl && !bank->mod_usage) {
  510. void __iomem *reg = bank->base + bank->regs->ctrl;
  511. u32 ctrl;
  512. ctrl = __raw_readl(reg);
  513. /* Module is disabled, clocks are gated */
  514. ctrl |= GPIO_MOD_CTRL_BIT;
  515. __raw_writel(ctrl, reg);
  516. bank->context.ctrl = ctrl;
  517. }
  518. _reset_gpio(bank, bank->chip.base + offset);
  519. spin_unlock_irqrestore(&bank->lock, flags);
  520. /*
  521. * If this is the last gpio to be freed in the bank,
  522. * disable the bank module.
  523. */
  524. if (!bank->mod_usage)
  525. pm_runtime_put(bank->dev);
  526. }
  527. /*
  528. * We need to unmask the GPIO bank interrupt as soon as possible to
  529. * avoid missing GPIO interrupts for other lines in the bank.
  530. * Then we need to mask-read-clear-unmask the triggered GPIO lines
  531. * in the bank to avoid missing nested interrupts for a GPIO line.
  532. * If we wait to unmask individual GPIO lines in the bank after the
  533. * line's interrupt handler has been run, we may miss some nested
  534. * interrupts.
  535. */
  536. static void gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
  537. {
  538. void __iomem *isr_reg = NULL;
  539. u32 isr;
  540. unsigned int gpio_irq, gpio_index;
  541. struct gpio_bank *bank;
  542. u32 retrigger = 0;
  543. int unmasked = 0;
  544. struct irq_chip *chip = irq_desc_get_chip(desc);
  545. chained_irq_enter(chip, desc);
  546. bank = irq_get_handler_data(irq);
  547. isr_reg = bank->base + bank->regs->irqstatus;
  548. pm_runtime_get_sync(bank->dev);
  549. if (WARN_ON(!isr_reg))
  550. goto exit;
  551. while(1) {
  552. u32 isr_saved, level_mask = 0;
  553. u32 enabled;
  554. enabled = _get_gpio_irqbank_mask(bank);
  555. isr_saved = isr = __raw_readl(isr_reg) & enabled;
  556. if (bank->level_mask)
  557. level_mask = bank->level_mask & enabled;
  558. /* clear edge sensitive interrupts before handler(s) are
  559. called so that we don't miss any interrupt occurred while
  560. executing them */
  561. _disable_gpio_irqbank(bank, isr_saved & ~level_mask);
  562. _clear_gpio_irqbank(bank, isr_saved & ~level_mask);
  563. _enable_gpio_irqbank(bank, isr_saved & ~level_mask);
  564. /* if there is only edge sensitive GPIO pin interrupts
  565. configured, we could unmask GPIO bank interrupt immediately */
  566. if (!level_mask && !unmasked) {
  567. unmasked = 1;
  568. chained_irq_exit(chip, desc);
  569. }
  570. isr |= retrigger;
  571. retrigger = 0;
  572. if (!isr)
  573. break;
  574. gpio_irq = bank->irq_base;
  575. for (; isr != 0; isr >>= 1, gpio_irq++) {
  576. int gpio = irq_to_gpio(bank, gpio_irq);
  577. if (!(isr & 1))
  578. continue;
  579. gpio_index = GPIO_INDEX(bank, gpio);
  580. /*
  581. * Some chips can't respond to both rising and falling
  582. * at the same time. If this irq was requested with
  583. * both flags, we need to flip the ICR data for the IRQ
  584. * to respond to the IRQ for the opposite direction.
  585. * This will be indicated in the bank toggle_mask.
  586. */
  587. if (bank->toggle_mask & (1 << gpio_index))
  588. _toggle_gpio_edge_triggering(bank, gpio_index);
  589. generic_handle_irq(gpio_irq);
  590. }
  591. }
  592. /* if bank has any level sensitive GPIO pin interrupt
  593. configured, we must unmask the bank interrupt only after
  594. handler(s) are executed in order to avoid spurious bank
  595. interrupt */
  596. exit:
  597. if (!unmasked)
  598. chained_irq_exit(chip, desc);
  599. pm_runtime_put(bank->dev);
  600. }
  601. static void gpio_irq_shutdown(struct irq_data *d)
  602. {
  603. struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
  604. unsigned int gpio = irq_to_gpio(bank, d->irq);
  605. unsigned long flags;
  606. spin_lock_irqsave(&bank->lock, flags);
  607. _reset_gpio(bank, gpio);
  608. spin_unlock_irqrestore(&bank->lock, flags);
  609. }
  610. static void gpio_ack_irq(struct irq_data *d)
  611. {
  612. struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
  613. unsigned int gpio = irq_to_gpio(bank, d->irq);
  614. _clear_gpio_irqstatus(bank, gpio);
  615. }
  616. static void gpio_mask_irq(struct irq_data *d)
  617. {
  618. struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
  619. unsigned int gpio = irq_to_gpio(bank, d->irq);
  620. unsigned long flags;
  621. spin_lock_irqsave(&bank->lock, flags);
  622. _set_gpio_irqenable(bank, gpio, 0);
  623. _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), IRQ_TYPE_NONE);
  624. spin_unlock_irqrestore(&bank->lock, flags);
  625. }
  626. static void gpio_unmask_irq(struct irq_data *d)
  627. {
  628. struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
  629. unsigned int gpio = irq_to_gpio(bank, d->irq);
  630. unsigned int irq_mask = GPIO_BIT(bank, gpio);
  631. u32 trigger = irqd_get_trigger_type(d);
  632. unsigned long flags;
  633. spin_lock_irqsave(&bank->lock, flags);
  634. if (trigger)
  635. _set_gpio_triggering(bank, GPIO_INDEX(bank, gpio), trigger);
  636. /* For level-triggered GPIOs, the clearing must be done after
  637. * the HW source is cleared, thus after the handler has run */
  638. if (bank->level_mask & irq_mask) {
  639. _set_gpio_irqenable(bank, gpio, 0);
  640. _clear_gpio_irqstatus(bank, gpio);
  641. }
  642. _set_gpio_irqenable(bank, gpio, 1);
  643. spin_unlock_irqrestore(&bank->lock, flags);
  644. }
  645. static struct irq_chip gpio_irq_chip = {
  646. .name = "GPIO",
  647. .irq_shutdown = gpio_irq_shutdown,
  648. .irq_ack = gpio_ack_irq,
  649. .irq_mask = gpio_mask_irq,
  650. .irq_unmask = gpio_unmask_irq,
  651. .irq_set_type = gpio_irq_type,
  652. .irq_set_wake = gpio_wake_enable,
  653. };
  654. /*---------------------------------------------------------------------*/
  655. static int omap_mpuio_suspend_noirq(struct device *dev)
  656. {
  657. struct platform_device *pdev = to_platform_device(dev);
  658. struct gpio_bank *bank = platform_get_drvdata(pdev);
  659. void __iomem *mask_reg = bank->base +
  660. OMAP_MPUIO_GPIO_MASKIT / bank->stride;
  661. unsigned long flags;
  662. spin_lock_irqsave(&bank->lock, flags);
  663. bank->saved_wakeup = __raw_readl(mask_reg);
  664. __raw_writel(0xffff & ~bank->suspend_wakeup, mask_reg);
  665. spin_unlock_irqrestore(&bank->lock, flags);
  666. return 0;
  667. }
  668. static int omap_mpuio_resume_noirq(struct device *dev)
  669. {
  670. struct platform_device *pdev = to_platform_device(dev);
  671. struct gpio_bank *bank = platform_get_drvdata(pdev);
  672. void __iomem *mask_reg = bank->base +
  673. OMAP_MPUIO_GPIO_MASKIT / bank->stride;
  674. unsigned long flags;
  675. spin_lock_irqsave(&bank->lock, flags);
  676. __raw_writel(bank->saved_wakeup, mask_reg);
  677. spin_unlock_irqrestore(&bank->lock, flags);
  678. return 0;
  679. }
  680. static const struct dev_pm_ops omap_mpuio_dev_pm_ops = {
  681. .suspend_noirq = omap_mpuio_suspend_noirq,
  682. .resume_noirq = omap_mpuio_resume_noirq,
  683. };
  684. /* use platform_driver for this. */
  685. static struct platform_driver omap_mpuio_driver = {
  686. .driver = {
  687. .name = "mpuio",
  688. .pm = &omap_mpuio_dev_pm_ops,
  689. },
  690. };
  691. static struct platform_device omap_mpuio_device = {
  692. .name = "mpuio",
  693. .id = -1,
  694. .dev = {
  695. .driver = &omap_mpuio_driver.driver,
  696. }
  697. /* could list the /proc/iomem resources */
  698. };
  699. static inline void mpuio_init(struct gpio_bank *bank)
  700. {
  701. platform_set_drvdata(&omap_mpuio_device, bank);
  702. if (platform_driver_register(&omap_mpuio_driver) == 0)
  703. (void) platform_device_register(&omap_mpuio_device);
  704. }
  705. /*---------------------------------------------------------------------*/
  706. static int gpio_input(struct gpio_chip *chip, unsigned offset)
  707. {
  708. struct gpio_bank *bank;
  709. unsigned long flags;
  710. bank = container_of(chip, struct gpio_bank, chip);
  711. spin_lock_irqsave(&bank->lock, flags);
  712. _set_gpio_direction(bank, offset, 1);
  713. spin_unlock_irqrestore(&bank->lock, flags);
  714. return 0;
  715. }
  716. static int gpio_is_input(struct gpio_bank *bank, int mask)
  717. {
  718. void __iomem *reg = bank->base + bank->regs->direction;
  719. return __raw_readl(reg) & mask;
  720. }
  721. static int gpio_get(struct gpio_chip *chip, unsigned offset)
  722. {
  723. struct gpio_bank *bank;
  724. u32 mask;
  725. bank = container_of(chip, struct gpio_bank, chip);
  726. mask = (1 << offset);
  727. if (gpio_is_input(bank, mask))
  728. return _get_gpio_datain(bank, offset);
  729. else
  730. return _get_gpio_dataout(bank, offset);
  731. }
  732. static int gpio_output(struct gpio_chip *chip, unsigned offset, int value)
  733. {
  734. struct gpio_bank *bank;
  735. unsigned long flags;
  736. bank = container_of(chip, struct gpio_bank, chip);
  737. spin_lock_irqsave(&bank->lock, flags);
  738. bank->set_dataout(bank, offset, value);
  739. _set_gpio_direction(bank, offset, 0);
  740. spin_unlock_irqrestore(&bank->lock, flags);
  741. return 0;
  742. }
  743. static int gpio_debounce(struct gpio_chip *chip, unsigned offset,
  744. unsigned debounce)
  745. {
  746. struct gpio_bank *bank;
  747. unsigned long flags;
  748. bank = container_of(chip, struct gpio_bank, chip);
  749. if (!bank->dbck) {
  750. bank->dbck = clk_get(bank->dev, "dbclk");
  751. if (IS_ERR(bank->dbck))
  752. dev_err(bank->dev, "Could not get gpio dbck\n");
  753. }
  754. spin_lock_irqsave(&bank->lock, flags);
  755. _set_gpio_debounce(bank, offset, debounce);
  756. spin_unlock_irqrestore(&bank->lock, flags);
  757. return 0;
  758. }
  759. static void gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  760. {
  761. struct gpio_bank *bank;
  762. unsigned long flags;
  763. bank = container_of(chip, struct gpio_bank, chip);
  764. spin_lock_irqsave(&bank->lock, flags);
  765. bank->set_dataout(bank, offset, value);
  766. spin_unlock_irqrestore(&bank->lock, flags);
  767. }
  768. static int gpio_2irq(struct gpio_chip *chip, unsigned offset)
  769. {
  770. struct gpio_bank *bank;
  771. bank = container_of(chip, struct gpio_bank, chip);
  772. return bank->irq_base + offset;
  773. }
  774. /*---------------------------------------------------------------------*/
  775. static void __init omap_gpio_show_rev(struct gpio_bank *bank)
  776. {
  777. static bool called;
  778. u32 rev;
  779. if (called || bank->regs->revision == USHRT_MAX)
  780. return;
  781. rev = __raw_readw(bank->base + bank->regs->revision);
  782. pr_info("OMAP GPIO hardware version %d.%d\n",
  783. (rev >> 4) & 0x0f, rev & 0x0f);
  784. called = true;
  785. }
  786. /* This lock class tells lockdep that GPIO irqs are in a different
  787. * category than their parents, so it won't report false recursion.
  788. */
  789. static struct lock_class_key gpio_lock_class;
  790. static void omap_gpio_mod_init(struct gpio_bank *bank)
  791. {
  792. void __iomem *base = bank->base;
  793. u32 l = 0xffffffff;
  794. if (bank->width == 16)
  795. l = 0xffff;
  796. if (bank->is_mpuio) {
  797. __raw_writel(l, bank->base + bank->regs->irqenable);
  798. return;
  799. }
  800. _gpio_rmw(base, bank->regs->irqenable, l, bank->regs->irqenable_inv);
  801. _gpio_rmw(base, bank->regs->irqstatus, l, !bank->regs->irqenable_inv);
  802. if (bank->regs->debounce_en)
  803. __raw_writel(0, base + bank->regs->debounce_en);
  804. /* Save OE default value (0xffffffff) in the context */
  805. bank->context.oe = __raw_readl(bank->base + bank->regs->direction);
  806. /* Initialize interface clk ungated, module enabled */
  807. if (bank->regs->ctrl)
  808. __raw_writel(0, base + bank->regs->ctrl);
  809. }
  810. static __devinit void
  811. omap_mpuio_alloc_gc(struct gpio_bank *bank, unsigned int irq_start,
  812. unsigned int num)
  813. {
  814. struct irq_chip_generic *gc;
  815. struct irq_chip_type *ct;
  816. gc = irq_alloc_generic_chip("MPUIO", 1, irq_start, bank->base,
  817. handle_simple_irq);
  818. if (!gc) {
  819. dev_err(bank->dev, "Memory alloc failed for gc\n");
  820. return;
  821. }
  822. ct = gc->chip_types;
  823. /* NOTE: No ack required, reading IRQ status clears it. */
  824. ct->chip.irq_mask = irq_gc_mask_set_bit;
  825. ct->chip.irq_unmask = irq_gc_mask_clr_bit;
  826. ct->chip.irq_set_type = gpio_irq_type;
  827. if (bank->regs->wkup_en)
  828. ct->chip.irq_set_wake = gpio_wake_enable,
  829. ct->regs.mask = OMAP_MPUIO_GPIO_INT / bank->stride;
  830. irq_setup_generic_chip(gc, IRQ_MSK(num), IRQ_GC_INIT_MASK_CACHE,
  831. IRQ_NOREQUEST | IRQ_NOPROBE, 0);
  832. }
  833. static void __devinit omap_gpio_chip_init(struct gpio_bank *bank)
  834. {
  835. int j;
  836. static int gpio;
  837. /*
  838. * REVISIT eventually switch from OMAP-specific gpio structs
  839. * over to the generic ones
  840. */
  841. bank->chip.request = omap_gpio_request;
  842. bank->chip.free = omap_gpio_free;
  843. bank->chip.direction_input = gpio_input;
  844. bank->chip.get = gpio_get;
  845. bank->chip.direction_output = gpio_output;
  846. bank->chip.set_debounce = gpio_debounce;
  847. bank->chip.set = gpio_set;
  848. bank->chip.to_irq = gpio_2irq;
  849. if (bank->is_mpuio) {
  850. bank->chip.label = "mpuio";
  851. if (bank->regs->wkup_en)
  852. bank->chip.dev = &omap_mpuio_device.dev;
  853. bank->chip.base = OMAP_MPUIO(0);
  854. } else {
  855. bank->chip.label = "gpio";
  856. bank->chip.base = gpio;
  857. gpio += bank->width;
  858. }
  859. bank->chip.ngpio = bank->width;
  860. gpiochip_add(&bank->chip);
  861. for (j = bank->irq_base; j < bank->irq_base + bank->width; j++) {
  862. irq_set_lockdep_class(j, &gpio_lock_class);
  863. irq_set_chip_data(j, bank);
  864. if (bank->is_mpuio) {
  865. omap_mpuio_alloc_gc(bank, j, bank->width);
  866. } else {
  867. irq_set_chip(j, &gpio_irq_chip);
  868. irq_set_handler(j, handle_simple_irq);
  869. set_irq_flags(j, IRQF_VALID);
  870. }
  871. }
  872. irq_set_chained_handler(bank->irq, gpio_irq_handler);
  873. irq_set_handler_data(bank->irq, bank);
  874. }
  875. static const struct of_device_id omap_gpio_match[];
  876. static int __devinit omap_gpio_probe(struct platform_device *pdev)
  877. {
  878. struct device *dev = &pdev->dev;
  879. struct device_node *node = dev->of_node;
  880. const struct of_device_id *match;
  881. struct omap_gpio_platform_data *pdata;
  882. struct resource *res;
  883. struct gpio_bank *bank;
  884. int ret = 0;
  885. match = of_match_device(of_match_ptr(omap_gpio_match), dev);
  886. pdata = match ? match->data : dev->platform_data;
  887. if (!pdata)
  888. return -EINVAL;
  889. bank = devm_kzalloc(&pdev->dev, sizeof(struct gpio_bank), GFP_KERNEL);
  890. if (!bank) {
  891. dev_err(dev, "Memory alloc failed\n");
  892. return -ENOMEM;
  893. }
  894. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  895. if (unlikely(!res)) {
  896. dev_err(dev, "Invalid IRQ resource\n");
  897. return -ENODEV;
  898. }
  899. bank->irq = res->start;
  900. bank->dev = dev;
  901. bank->dbck_flag = pdata->dbck_flag;
  902. bank->stride = pdata->bank_stride;
  903. bank->width = pdata->bank_width;
  904. bank->is_mpuio = pdata->is_mpuio;
  905. bank->non_wakeup_gpios = pdata->non_wakeup_gpios;
  906. bank->loses_context = pdata->loses_context;
  907. bank->get_context_loss_count = pdata->get_context_loss_count;
  908. bank->regs = pdata->regs;
  909. #ifdef CONFIG_OF_GPIO
  910. bank->chip.of_node = of_node_get(node);
  911. #endif
  912. bank->irq_base = irq_alloc_descs(-1, 0, bank->width, 0);
  913. if (bank->irq_base < 0) {
  914. dev_err(dev, "Couldn't allocate IRQ numbers\n");
  915. return -ENODEV;
  916. }
  917. bank->domain = irq_domain_add_legacy(node, bank->width, bank->irq_base,
  918. 0, &irq_domain_simple_ops, NULL);
  919. if (bank->regs->set_dataout && bank->regs->clr_dataout)
  920. bank->set_dataout = _set_gpio_dataout_reg;
  921. else
  922. bank->set_dataout = _set_gpio_dataout_mask;
  923. spin_lock_init(&bank->lock);
  924. /* Static mapping, never released */
  925. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  926. if (unlikely(!res)) {
  927. dev_err(dev, "Invalid mem resource\n");
  928. return -ENODEV;
  929. }
  930. if (!devm_request_mem_region(dev, res->start, resource_size(res),
  931. pdev->name)) {
  932. dev_err(dev, "Region already claimed\n");
  933. return -EBUSY;
  934. }
  935. bank->base = devm_ioremap(dev, res->start, resource_size(res));
  936. if (!bank->base) {
  937. dev_err(dev, "Could not ioremap\n");
  938. return -ENOMEM;
  939. }
  940. platform_set_drvdata(pdev, bank);
  941. pm_runtime_enable(bank->dev);
  942. pm_runtime_irq_safe(bank->dev);
  943. pm_runtime_get_sync(bank->dev);
  944. if (bank->is_mpuio)
  945. mpuio_init(bank);
  946. omap_gpio_mod_init(bank);
  947. omap_gpio_chip_init(bank);
  948. omap_gpio_show_rev(bank);
  949. pm_runtime_put(bank->dev);
  950. list_add_tail(&bank->node, &omap_gpio_list);
  951. return ret;
  952. }
  953. #ifdef CONFIG_ARCH_OMAP2PLUS
  954. #if defined(CONFIG_PM_SLEEP)
  955. static int omap_gpio_suspend(struct device *dev)
  956. {
  957. struct platform_device *pdev = to_platform_device(dev);
  958. struct gpio_bank *bank = platform_get_drvdata(pdev);
  959. void __iomem *base = bank->base;
  960. void __iomem *wakeup_enable;
  961. unsigned long flags;
  962. if (!bank->mod_usage || !bank->loses_context)
  963. return 0;
  964. if (!bank->regs->wkup_en || !bank->suspend_wakeup)
  965. return 0;
  966. wakeup_enable = bank->base + bank->regs->wkup_en;
  967. spin_lock_irqsave(&bank->lock, flags);
  968. bank->saved_wakeup = __raw_readl(wakeup_enable);
  969. _gpio_rmw(base, bank->regs->wkup_en, 0xffffffff, 0);
  970. _gpio_rmw(base, bank->regs->wkup_en, bank->suspend_wakeup, 1);
  971. spin_unlock_irqrestore(&bank->lock, flags);
  972. return 0;
  973. }
  974. static int omap_gpio_resume(struct device *dev)
  975. {
  976. struct platform_device *pdev = to_platform_device(dev);
  977. struct gpio_bank *bank = platform_get_drvdata(pdev);
  978. void __iomem *base = bank->base;
  979. unsigned long flags;
  980. if (!bank->mod_usage || !bank->loses_context)
  981. return 0;
  982. if (!bank->regs->wkup_en || !bank->saved_wakeup)
  983. return 0;
  984. spin_lock_irqsave(&bank->lock, flags);
  985. _gpio_rmw(base, bank->regs->wkup_en, 0xffffffff, 0);
  986. _gpio_rmw(base, bank->regs->wkup_en, bank->saved_wakeup, 1);
  987. spin_unlock_irqrestore(&bank->lock, flags);
  988. return 0;
  989. }
  990. #endif /* CONFIG_PM_SLEEP */
  991. #if defined(CONFIG_PM_RUNTIME)
  992. static void omap_gpio_restore_context(struct gpio_bank *bank);
  993. static int omap_gpio_runtime_suspend(struct device *dev)
  994. {
  995. struct platform_device *pdev = to_platform_device(dev);
  996. struct gpio_bank *bank = platform_get_drvdata(pdev);
  997. u32 l1 = 0, l2 = 0;
  998. unsigned long flags;
  999. u32 wake_low, wake_hi;
  1000. spin_lock_irqsave(&bank->lock, flags);
  1001. /*
  1002. * Only edges can generate a wakeup event to the PRCM.
  1003. *
  1004. * Therefore, ensure any wake-up capable GPIOs have
  1005. * edge-detection enabled before going idle to ensure a wakeup
  1006. * to the PRCM is generated on a GPIO transition. (c.f. 34xx
  1007. * NDA TRM 25.5.3.1)
  1008. *
  1009. * The normal values will be restored upon ->runtime_resume()
  1010. * by writing back the values saved in bank->context.
  1011. */
  1012. wake_low = bank->context.leveldetect0 & bank->context.wake_en;
  1013. if (wake_low)
  1014. __raw_writel(wake_low | bank->context.fallingdetect,
  1015. bank->base + bank->regs->fallingdetect);
  1016. wake_hi = bank->context.leveldetect1 & bank->context.wake_en;
  1017. if (wake_hi)
  1018. __raw_writel(wake_hi | bank->context.risingdetect,
  1019. bank->base + bank->regs->risingdetect);
  1020. if (bank->power_mode != OFF_MODE) {
  1021. bank->power_mode = 0;
  1022. goto update_gpio_context_count;
  1023. }
  1024. /*
  1025. * If going to OFF, remove triggering for all
  1026. * non-wakeup GPIOs. Otherwise spurious IRQs will be
  1027. * generated. See OMAP2420 Errata item 1.101.
  1028. */
  1029. bank->saved_datain = __raw_readl(bank->base +
  1030. bank->regs->datain);
  1031. l1 = __raw_readl(bank->base + bank->regs->fallingdetect);
  1032. l2 = __raw_readl(bank->base + bank->regs->risingdetect);
  1033. bank->saved_fallingdetect = l1;
  1034. bank->saved_risingdetect = l2;
  1035. l1 &= ~bank->enabled_non_wakeup_gpios;
  1036. l2 &= ~bank->enabled_non_wakeup_gpios;
  1037. __raw_writel(l1, bank->base + bank->regs->fallingdetect);
  1038. __raw_writel(l2, bank->base + bank->regs->risingdetect);
  1039. bank->workaround_enabled = true;
  1040. update_gpio_context_count:
  1041. if (bank->get_context_loss_count)
  1042. bank->context_loss_count =
  1043. bank->get_context_loss_count(bank->dev);
  1044. _gpio_dbck_disable(bank);
  1045. spin_unlock_irqrestore(&bank->lock, flags);
  1046. return 0;
  1047. }
  1048. static int omap_gpio_runtime_resume(struct device *dev)
  1049. {
  1050. struct platform_device *pdev = to_platform_device(dev);
  1051. struct gpio_bank *bank = platform_get_drvdata(pdev);
  1052. int context_lost_cnt_after;
  1053. u32 l = 0, gen, gen0, gen1;
  1054. unsigned long flags;
  1055. spin_lock_irqsave(&bank->lock, flags);
  1056. _gpio_dbck_enable(bank);
  1057. /*
  1058. * In ->runtime_suspend(), level-triggered, wakeup-enabled
  1059. * GPIOs were set to edge trigger also in order to be able to
  1060. * generate a PRCM wakeup. Here we restore the
  1061. * pre-runtime_suspend() values for edge triggering.
  1062. */
  1063. __raw_writel(bank->context.fallingdetect,
  1064. bank->base + bank->regs->fallingdetect);
  1065. __raw_writel(bank->context.risingdetect,
  1066. bank->base + bank->regs->risingdetect);
  1067. if (!bank->workaround_enabled) {
  1068. spin_unlock_irqrestore(&bank->lock, flags);
  1069. return 0;
  1070. }
  1071. if (bank->get_context_loss_count) {
  1072. context_lost_cnt_after =
  1073. bank->get_context_loss_count(bank->dev);
  1074. if (context_lost_cnt_after != bank->context_loss_count ||
  1075. !context_lost_cnt_after) {
  1076. omap_gpio_restore_context(bank);
  1077. } else {
  1078. spin_unlock_irqrestore(&bank->lock, flags);
  1079. return 0;
  1080. }
  1081. }
  1082. __raw_writel(bank->saved_fallingdetect,
  1083. bank->base + bank->regs->fallingdetect);
  1084. __raw_writel(bank->saved_risingdetect,
  1085. bank->base + bank->regs->risingdetect);
  1086. l = __raw_readl(bank->base + bank->regs->datain);
  1087. /*
  1088. * Check if any of the non-wakeup interrupt GPIOs have changed
  1089. * state. If so, generate an IRQ by software. This is
  1090. * horribly racy, but it's the best we can do to work around
  1091. * this silicon bug.
  1092. */
  1093. l ^= bank->saved_datain;
  1094. l &= bank->enabled_non_wakeup_gpios;
  1095. /*
  1096. * No need to generate IRQs for the rising edge for gpio IRQs
  1097. * configured with falling edge only; and vice versa.
  1098. */
  1099. gen0 = l & bank->saved_fallingdetect;
  1100. gen0 &= bank->saved_datain;
  1101. gen1 = l & bank->saved_risingdetect;
  1102. gen1 &= ~(bank->saved_datain);
  1103. /* FIXME: Consider GPIO IRQs with level detections properly! */
  1104. gen = l & (~(bank->saved_fallingdetect) & ~(bank->saved_risingdetect));
  1105. /* Consider all GPIO IRQs needed to be updated */
  1106. gen |= gen0 | gen1;
  1107. if (gen) {
  1108. u32 old0, old1;
  1109. old0 = __raw_readl(bank->base + bank->regs->leveldetect0);
  1110. old1 = __raw_readl(bank->base + bank->regs->leveldetect1);
  1111. if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
  1112. __raw_writel(old0 | gen, bank->base +
  1113. bank->regs->leveldetect0);
  1114. __raw_writel(old1 | gen, bank->base +
  1115. bank->regs->leveldetect1);
  1116. }
  1117. if (cpu_is_omap44xx()) {
  1118. __raw_writel(old0 | l, bank->base +
  1119. bank->regs->leveldetect0);
  1120. __raw_writel(old1 | l, bank->base +
  1121. bank->regs->leveldetect1);
  1122. }
  1123. __raw_writel(old0, bank->base + bank->regs->leveldetect0);
  1124. __raw_writel(old1, bank->base + bank->regs->leveldetect1);
  1125. }
  1126. bank->workaround_enabled = false;
  1127. spin_unlock_irqrestore(&bank->lock, flags);
  1128. return 0;
  1129. }
  1130. #endif /* CONFIG_PM_RUNTIME */
  1131. void omap2_gpio_prepare_for_idle(int pwr_mode)
  1132. {
  1133. struct gpio_bank *bank;
  1134. list_for_each_entry(bank, &omap_gpio_list, node) {
  1135. if (!bank->mod_usage || !bank->loses_context)
  1136. continue;
  1137. bank->power_mode = pwr_mode;
  1138. pm_runtime_put_sync_suspend(bank->dev);
  1139. }
  1140. }
  1141. void omap2_gpio_resume_after_idle(void)
  1142. {
  1143. struct gpio_bank *bank;
  1144. list_for_each_entry(bank, &omap_gpio_list, node) {
  1145. if (!bank->mod_usage || !bank->loses_context)
  1146. continue;
  1147. pm_runtime_get_sync(bank->dev);
  1148. }
  1149. }
  1150. #if defined(CONFIG_PM_RUNTIME)
  1151. static void omap_gpio_restore_context(struct gpio_bank *bank)
  1152. {
  1153. __raw_writel(bank->context.wake_en,
  1154. bank->base + bank->regs->wkup_en);
  1155. __raw_writel(bank->context.ctrl, bank->base + bank->regs->ctrl);
  1156. __raw_writel(bank->context.leveldetect0,
  1157. bank->base + bank->regs->leveldetect0);
  1158. __raw_writel(bank->context.leveldetect1,
  1159. bank->base + bank->regs->leveldetect1);
  1160. __raw_writel(bank->context.risingdetect,
  1161. bank->base + bank->regs->risingdetect);
  1162. __raw_writel(bank->context.fallingdetect,
  1163. bank->base + bank->regs->fallingdetect);
  1164. if (bank->regs->set_dataout && bank->regs->clr_dataout)
  1165. __raw_writel(bank->context.dataout,
  1166. bank->base + bank->regs->set_dataout);
  1167. else
  1168. __raw_writel(bank->context.dataout,
  1169. bank->base + bank->regs->dataout);
  1170. __raw_writel(bank->context.oe, bank->base + bank->regs->direction);
  1171. if (bank->dbck_enable_mask) {
  1172. __raw_writel(bank->context.debounce, bank->base +
  1173. bank->regs->debounce);
  1174. __raw_writel(bank->context.debounce_en,
  1175. bank->base + bank->regs->debounce_en);
  1176. }
  1177. __raw_writel(bank->context.irqenable1,
  1178. bank->base + bank->regs->irqenable);
  1179. __raw_writel(bank->context.irqenable2,
  1180. bank->base + bank->regs->irqenable2);
  1181. }
  1182. #endif /* CONFIG_PM_RUNTIME */
  1183. #else
  1184. #define omap_gpio_suspend NULL
  1185. #define omap_gpio_resume NULL
  1186. #define omap_gpio_runtime_suspend NULL
  1187. #define omap_gpio_runtime_resume NULL
  1188. #endif
  1189. static const struct dev_pm_ops gpio_pm_ops = {
  1190. SET_SYSTEM_SLEEP_PM_OPS(omap_gpio_suspend, omap_gpio_resume)
  1191. SET_RUNTIME_PM_OPS(omap_gpio_runtime_suspend, omap_gpio_runtime_resume,
  1192. NULL)
  1193. };
  1194. #if defined(CONFIG_OF)
  1195. static struct omap_gpio_reg_offs omap2_gpio_regs = {
  1196. .revision = OMAP24XX_GPIO_REVISION,
  1197. .direction = OMAP24XX_GPIO_OE,
  1198. .datain = OMAP24XX_GPIO_DATAIN,
  1199. .dataout = OMAP24XX_GPIO_DATAOUT,
  1200. .set_dataout = OMAP24XX_GPIO_SETDATAOUT,
  1201. .clr_dataout = OMAP24XX_GPIO_CLEARDATAOUT,
  1202. .irqstatus = OMAP24XX_GPIO_IRQSTATUS1,
  1203. .irqstatus2 = OMAP24XX_GPIO_IRQSTATUS2,
  1204. .irqenable = OMAP24XX_GPIO_IRQENABLE1,
  1205. .irqenable2 = OMAP24XX_GPIO_IRQENABLE2,
  1206. .set_irqenable = OMAP24XX_GPIO_SETIRQENABLE1,
  1207. .clr_irqenable = OMAP24XX_GPIO_CLEARIRQENABLE1,
  1208. .debounce = OMAP24XX_GPIO_DEBOUNCE_VAL,
  1209. .debounce_en = OMAP24XX_GPIO_DEBOUNCE_EN,
  1210. .ctrl = OMAP24XX_GPIO_CTRL,
  1211. .wkup_en = OMAP24XX_GPIO_WAKE_EN,
  1212. .leveldetect0 = OMAP24XX_GPIO_LEVELDETECT0,
  1213. .leveldetect1 = OMAP24XX_GPIO_LEVELDETECT1,
  1214. .risingdetect = OMAP24XX_GPIO_RISINGDETECT,
  1215. .fallingdetect = OMAP24XX_GPIO_FALLINGDETECT,
  1216. };
  1217. static struct omap_gpio_reg_offs omap4_gpio_regs = {
  1218. .revision = OMAP4_GPIO_REVISION,
  1219. .direction = OMAP4_GPIO_OE,
  1220. .datain = OMAP4_GPIO_DATAIN,
  1221. .dataout = OMAP4_GPIO_DATAOUT,
  1222. .set_dataout = OMAP4_GPIO_SETDATAOUT,
  1223. .clr_dataout = OMAP4_GPIO_CLEARDATAOUT,
  1224. .irqstatus = OMAP4_GPIO_IRQSTATUS0,
  1225. .irqstatus2 = OMAP4_GPIO_IRQSTATUS1,
  1226. .irqenable = OMAP4_GPIO_IRQSTATUSSET0,
  1227. .irqenable2 = OMAP4_GPIO_IRQSTATUSSET1,
  1228. .set_irqenable = OMAP4_GPIO_IRQSTATUSSET0,
  1229. .clr_irqenable = OMAP4_GPIO_IRQSTATUSCLR0,
  1230. .debounce = OMAP4_GPIO_DEBOUNCINGTIME,
  1231. .debounce_en = OMAP4_GPIO_DEBOUNCENABLE,
  1232. .ctrl = OMAP4_GPIO_CTRL,
  1233. .wkup_en = OMAP4_GPIO_IRQWAKEN0,
  1234. .leveldetect0 = OMAP4_GPIO_LEVELDETECT0,
  1235. .leveldetect1 = OMAP4_GPIO_LEVELDETECT1,
  1236. .risingdetect = OMAP4_GPIO_RISINGDETECT,
  1237. .fallingdetect = OMAP4_GPIO_FALLINGDETECT,
  1238. };
  1239. static struct omap_gpio_platform_data omap2_pdata = {
  1240. .regs = &omap2_gpio_regs,
  1241. .bank_width = 32,
  1242. .dbck_flag = false,
  1243. };
  1244. static struct omap_gpio_platform_data omap3_pdata = {
  1245. .regs = &omap2_gpio_regs,
  1246. .bank_width = 32,
  1247. .dbck_flag = true,
  1248. };
  1249. static struct omap_gpio_platform_data omap4_pdata = {
  1250. .regs = &omap4_gpio_regs,
  1251. .bank_width = 32,
  1252. .dbck_flag = true,
  1253. };
  1254. static const struct of_device_id omap_gpio_match[] = {
  1255. {
  1256. .compatible = "ti,omap4-gpio",
  1257. .data = &omap4_pdata,
  1258. },
  1259. {
  1260. .compatible = "ti,omap3-gpio",
  1261. .data = &omap3_pdata,
  1262. },
  1263. {
  1264. .compatible = "ti,omap2-gpio",
  1265. .data = &omap2_pdata,
  1266. },
  1267. { },
  1268. };
  1269. MODULE_DEVICE_TABLE(of, omap_gpio_match);
  1270. #endif
  1271. static struct platform_driver omap_gpio_driver = {
  1272. .probe = omap_gpio_probe,
  1273. .driver = {
  1274. .name = "omap_gpio",
  1275. .pm = &gpio_pm_ops,
  1276. .of_match_table = of_match_ptr(omap_gpio_match),
  1277. },
  1278. };
  1279. /*
  1280. * gpio driver register needs to be done before
  1281. * machine_init functions access gpio APIs.
  1282. * Hence omap_gpio_drv_reg() is a postcore_initcall.
  1283. */
  1284. static int __init omap_gpio_drv_reg(void)
  1285. {
  1286. return platform_driver_register(&omap_gpio_driver);
  1287. }
  1288. postcore_initcall(omap_gpio_drv_reg);