gpio-omap.c 44 KB

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