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