gpio-omap.c 43 KB

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