arizona-irq.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*
  2. * Arizona interrupt support
  3. *
  4. * Copyright 2012 Wolfson Microelectronics plc
  5. *
  6. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/delay.h>
  13. #include <linux/gpio.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/irq.h>
  16. #include <linux/irqdomain.h>
  17. #include <linux/module.h>
  18. #include <linux/pm_runtime.h>
  19. #include <linux/regmap.h>
  20. #include <linux/regulator/consumer.h>
  21. #include <linux/slab.h>
  22. #include <linux/mfd/arizona/core.h>
  23. #include <linux/mfd/arizona/registers.h>
  24. #include "arizona.h"
  25. #define ARIZONA_AOD_IRQ_INDEX 0
  26. #define ARIZONA_MAIN_IRQ_INDEX 1
  27. static int arizona_map_irq(struct arizona *arizona, int irq)
  28. {
  29. int ret;
  30. if (arizona->aod_irq_chip) {
  31. ret = regmap_irq_get_virq(arizona->aod_irq_chip, irq);
  32. if (ret >= 0)
  33. return ret;
  34. }
  35. return regmap_irq_get_virq(arizona->irq_chip, irq);
  36. }
  37. int arizona_request_irq(struct arizona *arizona, int irq, char *name,
  38. irq_handler_t handler, void *data)
  39. {
  40. irq = arizona_map_irq(arizona, irq);
  41. if (irq < 0)
  42. return irq;
  43. return request_threaded_irq(irq, NULL, handler, IRQF_ONESHOT,
  44. name, data);
  45. }
  46. EXPORT_SYMBOL_GPL(arizona_request_irq);
  47. void arizona_free_irq(struct arizona *arizona, int irq, void *data)
  48. {
  49. irq = arizona_map_irq(arizona, irq);
  50. if (irq < 0)
  51. return;
  52. free_irq(irq, data);
  53. }
  54. EXPORT_SYMBOL_GPL(arizona_free_irq);
  55. int arizona_set_irq_wake(struct arizona *arizona, int irq, int on)
  56. {
  57. irq = arizona_map_irq(arizona, irq);
  58. if (irq < 0)
  59. return irq;
  60. return irq_set_irq_wake(irq, on);
  61. }
  62. EXPORT_SYMBOL_GPL(arizona_set_irq_wake);
  63. static irqreturn_t arizona_boot_done(int irq, void *data)
  64. {
  65. struct arizona *arizona = data;
  66. dev_dbg(arizona->dev, "Boot done\n");
  67. return IRQ_HANDLED;
  68. }
  69. static irqreturn_t arizona_ctrlif_err(int irq, void *data)
  70. {
  71. struct arizona *arizona = data;
  72. /*
  73. * For pretty much all potential sources a register cache sync
  74. * won't help, we've just got a software bug somewhere.
  75. */
  76. dev_err(arizona->dev, "Control interface error\n");
  77. return IRQ_HANDLED;
  78. }
  79. static irqreturn_t arizona_irq_thread(int irq, void *data)
  80. {
  81. struct arizona *arizona = data;
  82. bool poll;
  83. unsigned int val;
  84. int ret;
  85. ret = pm_runtime_get_sync(arizona->dev);
  86. if (ret < 0) {
  87. dev_err(arizona->dev, "Failed to resume device: %d\n", ret);
  88. return IRQ_NONE;
  89. }
  90. do {
  91. poll = false;
  92. if (arizona->aod_irq_chip) {
  93. /*
  94. * Check the AOD status register to determine whether
  95. * the nested IRQ handler should be called.
  96. */
  97. ret = regmap_read(arizona->regmap,
  98. ARIZONA_AOD_IRQ1, &val);
  99. if (ret)
  100. dev_warn(arizona->dev,
  101. "Failed to read AOD IRQ1 %d\n", ret);
  102. else if (val)
  103. handle_nested_irq(
  104. irq_find_mapping(arizona->virq, 0));
  105. }
  106. /*
  107. * Check if one of the main interrupts is asserted and only
  108. * check that domain if it is.
  109. */
  110. ret = regmap_read(arizona->regmap, ARIZONA_IRQ_PIN_STATUS,
  111. &val);
  112. if (ret == 0 && val & ARIZONA_IRQ1_STS) {
  113. handle_nested_irq(irq_find_mapping(arizona->virq, 1));
  114. } else if (ret != 0) {
  115. dev_err(arizona->dev,
  116. "Failed to read main IRQ status: %d\n", ret);
  117. }
  118. /*
  119. * Poll the IRQ pin status to see if we're really done
  120. * if the interrupt controller can't do it for us.
  121. */
  122. if (!arizona->pdata.irq_gpio) {
  123. break;
  124. } else if (arizona->pdata.irq_flags & IRQF_TRIGGER_RISING &&
  125. gpio_get_value_cansleep(arizona->pdata.irq_gpio)) {
  126. poll = true;
  127. } else if (arizona->pdata.irq_flags & IRQF_TRIGGER_FALLING &&
  128. !gpio_get_value_cansleep(arizona->pdata.irq_gpio)) {
  129. poll = true;
  130. }
  131. } while (poll);
  132. pm_runtime_mark_last_busy(arizona->dev);
  133. pm_runtime_put_autosuspend(arizona->dev);
  134. return IRQ_HANDLED;
  135. }
  136. static void arizona_irq_enable(struct irq_data *data)
  137. {
  138. }
  139. static void arizona_irq_disable(struct irq_data *data)
  140. {
  141. }
  142. static int arizona_irq_set_wake(struct irq_data *data, unsigned int on)
  143. {
  144. struct arizona *arizona = irq_data_get_irq_chip_data(data);
  145. return irq_set_irq_wake(arizona->irq, on);
  146. }
  147. static struct irq_chip arizona_irq_chip = {
  148. .name = "arizona",
  149. .irq_disable = arizona_irq_disable,
  150. .irq_enable = arizona_irq_enable,
  151. .irq_set_wake = arizona_irq_set_wake,
  152. };
  153. static struct lock_class_key arizona_irq_lock_class;
  154. static struct lock_class_key arizona_irq_request_class;
  155. static int arizona_irq_map(struct irq_domain *h, unsigned int virq,
  156. irq_hw_number_t hw)
  157. {
  158. struct arizona *data = h->host_data;
  159. irq_set_chip_data(virq, data);
  160. irq_set_lockdep_class(virq, &arizona_irq_lock_class,
  161. &arizona_irq_request_class);
  162. irq_set_chip_and_handler(virq, &arizona_irq_chip, handle_simple_irq);
  163. irq_set_nested_thread(virq, 1);
  164. irq_set_noprobe(virq);
  165. return 0;
  166. }
  167. static const struct irq_domain_ops arizona_domain_ops = {
  168. .map = arizona_irq_map,
  169. .xlate = irq_domain_xlate_twocell,
  170. };
  171. int arizona_irq_init(struct arizona *arizona)
  172. {
  173. int flags = IRQF_ONESHOT;
  174. int ret;
  175. const struct regmap_irq_chip *aod, *irq;
  176. struct irq_data *irq_data;
  177. unsigned int virq;
  178. arizona->ctrlif_error = true;
  179. switch (arizona->type) {
  180. #ifdef CONFIG_MFD_WM5102
  181. case WM5102:
  182. aod = &wm5102_aod;
  183. irq = &wm5102_irq;
  184. arizona->ctrlif_error = false;
  185. break;
  186. #endif
  187. #ifdef CONFIG_MFD_WM5110
  188. case WM5110:
  189. case WM8280:
  190. aod = &wm5110_aod;
  191. switch (arizona->rev) {
  192. case 0 ... 2:
  193. irq = &wm5110_irq;
  194. break;
  195. default:
  196. irq = &wm5110_revd_irq;
  197. break;
  198. }
  199. arizona->ctrlif_error = false;
  200. break;
  201. #endif
  202. #ifdef CONFIG_MFD_CS47L24
  203. case WM1831:
  204. case CS47L24:
  205. aod = NULL;
  206. irq = &cs47l24_irq;
  207. arizona->ctrlif_error = false;
  208. break;
  209. #endif
  210. #ifdef CONFIG_MFD_WM8997
  211. case WM8997:
  212. aod = &wm8997_aod;
  213. irq = &wm8997_irq;
  214. arizona->ctrlif_error = false;
  215. break;
  216. #endif
  217. #ifdef CONFIG_MFD_WM8998
  218. case WM8998:
  219. case WM1814:
  220. aod = &wm8998_aod;
  221. irq = &wm8998_irq;
  222. arizona->ctrlif_error = false;
  223. break;
  224. #endif
  225. default:
  226. BUG_ON("Unknown Arizona class device" == NULL);
  227. return -EINVAL;
  228. }
  229. /* Disable all wake sources by default */
  230. regmap_write(arizona->regmap, ARIZONA_WAKE_CONTROL, 0);
  231. /* Read the flags from the interrupt controller if not specified */
  232. if (!arizona->pdata.irq_flags) {
  233. irq_data = irq_get_irq_data(arizona->irq);
  234. if (!irq_data) {
  235. dev_err(arizona->dev, "Invalid IRQ: %d\n",
  236. arizona->irq);
  237. return -EINVAL;
  238. }
  239. arizona->pdata.irq_flags = irqd_get_trigger_type(irq_data);
  240. switch (arizona->pdata.irq_flags) {
  241. case IRQF_TRIGGER_LOW:
  242. case IRQF_TRIGGER_HIGH:
  243. case IRQF_TRIGGER_RISING:
  244. case IRQF_TRIGGER_FALLING:
  245. break;
  246. case IRQ_TYPE_NONE:
  247. default:
  248. /* Device default */
  249. arizona->pdata.irq_flags = IRQF_TRIGGER_LOW;
  250. break;
  251. }
  252. }
  253. if (arizona->pdata.irq_flags & (IRQF_TRIGGER_HIGH |
  254. IRQF_TRIGGER_RISING)) {
  255. ret = regmap_update_bits(arizona->regmap, ARIZONA_IRQ_CTRL_1,
  256. ARIZONA_IRQ_POL, 0);
  257. if (ret != 0) {
  258. dev_err(arizona->dev, "Couldn't set IRQ polarity: %d\n",
  259. ret);
  260. goto err;
  261. }
  262. }
  263. flags |= arizona->pdata.irq_flags;
  264. /* Allocate a virtual IRQ domain to distribute to the regmap domains */
  265. arizona->virq = irq_domain_add_linear(NULL, 2, &arizona_domain_ops,
  266. arizona);
  267. if (!arizona->virq) {
  268. dev_err(arizona->dev, "Failed to add core IRQ domain\n");
  269. ret = -EINVAL;
  270. goto err;
  271. }
  272. if (aod) {
  273. virq = irq_create_mapping(arizona->virq, ARIZONA_AOD_IRQ_INDEX);
  274. if (!virq) {
  275. dev_err(arizona->dev, "Failed to map AOD IRQs\n");
  276. ret = -EINVAL;
  277. goto err_domain;
  278. }
  279. ret = regmap_add_irq_chip(arizona->regmap, virq, IRQF_ONESHOT,
  280. 0, aod, &arizona->aod_irq_chip);
  281. if (ret != 0) {
  282. dev_err(arizona->dev,
  283. "Failed to add AOD IRQs: %d\n", ret);
  284. goto err_map_aod;
  285. }
  286. }
  287. virq = irq_create_mapping(arizona->virq, ARIZONA_MAIN_IRQ_INDEX);
  288. if (!virq) {
  289. dev_err(arizona->dev, "Failed to map main IRQs\n");
  290. ret = -EINVAL;
  291. goto err_aod;
  292. }
  293. ret = regmap_add_irq_chip(arizona->regmap, virq, IRQF_ONESHOT,
  294. 0, irq, &arizona->irq_chip);
  295. if (ret != 0) {
  296. dev_err(arizona->dev, "Failed to add main IRQs: %d\n", ret);
  297. goto err_map_main_irq;
  298. }
  299. /* Used to emulate edge trigger and to work around broken pinmux */
  300. if (arizona->pdata.irq_gpio) {
  301. if (gpio_to_irq(arizona->pdata.irq_gpio) != arizona->irq) {
  302. dev_warn(arizona->dev, "IRQ %d is not GPIO %d (%d)\n",
  303. arizona->irq, arizona->pdata.irq_gpio,
  304. gpio_to_irq(arizona->pdata.irq_gpio));
  305. arizona->irq = gpio_to_irq(arizona->pdata.irq_gpio);
  306. }
  307. ret = devm_gpio_request_one(arizona->dev,
  308. arizona->pdata.irq_gpio,
  309. GPIOF_IN, "arizona IRQ");
  310. if (ret != 0) {
  311. dev_err(arizona->dev,
  312. "Failed to request IRQ GPIO %d:: %d\n",
  313. arizona->pdata.irq_gpio, ret);
  314. arizona->pdata.irq_gpio = 0;
  315. }
  316. }
  317. ret = request_threaded_irq(arizona->irq, NULL, arizona_irq_thread,
  318. flags, "arizona", arizona);
  319. if (ret != 0) {
  320. dev_err(arizona->dev, "Failed to request primary IRQ %d: %d\n",
  321. arizona->irq, ret);
  322. goto err_main_irq;
  323. }
  324. /* Make sure the boot done IRQ is unmasked for resumes */
  325. ret = arizona_request_irq(arizona, ARIZONA_IRQ_BOOT_DONE, "Boot done",
  326. arizona_boot_done, arizona);
  327. if (ret != 0) {
  328. dev_err(arizona->dev, "Failed to request boot done %d: %d\n",
  329. arizona->irq, ret);
  330. goto err_boot_done;
  331. }
  332. /* Handle control interface errors in the core */
  333. if (arizona->ctrlif_error) {
  334. ret = arizona_request_irq(arizona, ARIZONA_IRQ_CTRLIF_ERR,
  335. "Control interface error",
  336. arizona_ctrlif_err, arizona);
  337. if (ret != 0) {
  338. dev_err(arizona->dev,
  339. "Failed to request CTRLIF_ERR %d: %d\n",
  340. arizona->irq, ret);
  341. goto err_ctrlif;
  342. }
  343. }
  344. return 0;
  345. err_ctrlif:
  346. arizona_free_irq(arizona, ARIZONA_IRQ_BOOT_DONE, arizona);
  347. err_boot_done:
  348. free_irq(arizona->irq, arizona);
  349. err_main_irq:
  350. regmap_del_irq_chip(irq_find_mapping(arizona->virq,
  351. ARIZONA_MAIN_IRQ_INDEX),
  352. arizona->irq_chip);
  353. err_map_main_irq:
  354. irq_dispose_mapping(irq_find_mapping(arizona->virq,
  355. ARIZONA_MAIN_IRQ_INDEX));
  356. err_aod:
  357. regmap_del_irq_chip(irq_find_mapping(arizona->virq,
  358. ARIZONA_AOD_IRQ_INDEX),
  359. arizona->aod_irq_chip);
  360. err_map_aod:
  361. irq_dispose_mapping(irq_find_mapping(arizona->virq,
  362. ARIZONA_AOD_IRQ_INDEX));
  363. err_domain:
  364. irq_domain_remove(arizona->virq);
  365. err:
  366. return ret;
  367. }
  368. int arizona_irq_exit(struct arizona *arizona)
  369. {
  370. unsigned int virq;
  371. if (arizona->ctrlif_error)
  372. arizona_free_irq(arizona, ARIZONA_IRQ_CTRLIF_ERR, arizona);
  373. arizona_free_irq(arizona, ARIZONA_IRQ_BOOT_DONE, arizona);
  374. virq = irq_find_mapping(arizona->virq, ARIZONA_MAIN_IRQ_INDEX);
  375. regmap_del_irq_chip(virq, arizona->irq_chip);
  376. irq_dispose_mapping(virq);
  377. virq = irq_find_mapping(arizona->virq, ARIZONA_AOD_IRQ_INDEX);
  378. regmap_del_irq_chip(virq, arizona->aod_irq_chip);
  379. irq_dispose_mapping(virq);
  380. irq_domain_remove(arizona->virq);
  381. free_irq(arizona->irq, arizona);
  382. return 0;
  383. }