gpio-omap.c 43 KB

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