arizona-irq.c 9.4 KB

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