arizona-irq.c 9.9 KB

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