gpio-omap.c 46 KB

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