gpio-omap.c 42 KB

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