regmap-irq.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /*
  2. * regmap based irq_chip
  3. *
  4. * Copyright 2011 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/export.h>
  13. #include <linux/device.h>
  14. #include <linux/regmap.h>
  15. #include <linux/irq.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/irqdomain.h>
  18. #include <linux/pm_runtime.h>
  19. #include <linux/slab.h>
  20. #include "internal.h"
  21. struct regmap_irq_chip_data {
  22. struct mutex lock;
  23. struct irq_chip irq_chip;
  24. struct regmap *map;
  25. const struct regmap_irq_chip *chip;
  26. int irq_base;
  27. struct irq_domain *domain;
  28. int irq;
  29. int wake_count;
  30. unsigned int *status_buf;
  31. unsigned int *mask_buf;
  32. unsigned int *mask_buf_def;
  33. unsigned int *wake_buf;
  34. unsigned int irq_reg_stride;
  35. };
  36. static inline const
  37. struct regmap_irq *irq_to_regmap_irq(struct regmap_irq_chip_data *data,
  38. int irq)
  39. {
  40. return &data->chip->irqs[irq];
  41. }
  42. static void regmap_irq_lock(struct irq_data *data)
  43. {
  44. struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data);
  45. mutex_lock(&d->lock);
  46. }
  47. static void regmap_irq_sync_unlock(struct irq_data *data)
  48. {
  49. struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data);
  50. struct regmap *map = d->map;
  51. int i, ret;
  52. u32 reg;
  53. if (d->chip->runtime_pm) {
  54. ret = pm_runtime_get_sync(map->dev);
  55. if (ret < 0)
  56. dev_err(map->dev, "IRQ sync failed to resume: %d\n",
  57. ret);
  58. }
  59. /*
  60. * If there's been a change in the mask write it back to the
  61. * hardware. We rely on the use of the regmap core cache to
  62. * suppress pointless writes.
  63. */
  64. for (i = 0; i < d->chip->num_regs; i++) {
  65. reg = d->chip->mask_base +
  66. (i * map->reg_stride * d->irq_reg_stride);
  67. if (d->chip->mask_invert)
  68. ret = regmap_update_bits(d->map, reg,
  69. d->mask_buf_def[i], ~d->mask_buf[i]);
  70. else
  71. ret = regmap_update_bits(d->map, reg,
  72. d->mask_buf_def[i], d->mask_buf[i]);
  73. if (ret != 0)
  74. dev_err(d->map->dev, "Failed to sync masks in %x\n",
  75. reg);
  76. }
  77. if (d->chip->runtime_pm)
  78. pm_runtime_put(map->dev);
  79. /* If we've changed our wakeup count propagate it to the parent */
  80. if (d->wake_count < 0)
  81. for (i = d->wake_count; i < 0; i++)
  82. irq_set_irq_wake(d->irq, 0);
  83. else if (d->wake_count > 0)
  84. for (i = 0; i < d->wake_count; i++)
  85. irq_set_irq_wake(d->irq, 1);
  86. d->wake_count = 0;
  87. mutex_unlock(&d->lock);
  88. }
  89. static void regmap_irq_enable(struct irq_data *data)
  90. {
  91. struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data);
  92. struct regmap *map = d->map;
  93. const struct regmap_irq *irq_data = irq_to_regmap_irq(d, data->hwirq);
  94. d->mask_buf[irq_data->reg_offset / map->reg_stride] &= ~irq_data->mask;
  95. }
  96. static void regmap_irq_disable(struct irq_data *data)
  97. {
  98. struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data);
  99. struct regmap *map = d->map;
  100. const struct regmap_irq *irq_data = irq_to_regmap_irq(d, data->hwirq);
  101. d->mask_buf[irq_data->reg_offset / map->reg_stride] |= irq_data->mask;
  102. }
  103. static int regmap_irq_set_wake(struct irq_data *data, unsigned int on)
  104. {
  105. struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data);
  106. struct regmap *map = d->map;
  107. const struct regmap_irq *irq_data = irq_to_regmap_irq(d, data->hwirq);
  108. if (on) {
  109. if (d->wake_buf)
  110. d->wake_buf[irq_data->reg_offset / map->reg_stride]
  111. &= ~irq_data->mask;
  112. d->wake_count++;
  113. } else {
  114. if (d->wake_buf)
  115. d->wake_buf[irq_data->reg_offset / map->reg_stride]
  116. |= irq_data->mask;
  117. d->wake_count--;
  118. }
  119. return 0;
  120. }
  121. static const struct irq_chip regmap_irq_chip = {
  122. .irq_bus_lock = regmap_irq_lock,
  123. .irq_bus_sync_unlock = regmap_irq_sync_unlock,
  124. .irq_disable = regmap_irq_disable,
  125. .irq_enable = regmap_irq_enable,
  126. .irq_set_wake = regmap_irq_set_wake,
  127. };
  128. static irqreturn_t regmap_irq_thread(int irq, void *d)
  129. {
  130. struct regmap_irq_chip_data *data = d;
  131. const struct regmap_irq_chip *chip = data->chip;
  132. struct regmap *map = data->map;
  133. int ret, i;
  134. bool handled = false;
  135. u32 reg;
  136. if (chip->runtime_pm) {
  137. ret = pm_runtime_get_sync(map->dev);
  138. if (ret < 0) {
  139. dev_err(map->dev, "IRQ thread failed to resume: %d\n",
  140. ret);
  141. return IRQ_NONE;
  142. }
  143. }
  144. for (i = 0; i < data->chip->num_regs; i++) {
  145. ret = regmap_read(map, chip->status_base + (i * map->reg_stride
  146. * data->irq_reg_stride),
  147. &data->status_buf[i]);
  148. if (ret != 0) {
  149. dev_err(map->dev, "Failed to read IRQ status: %d\n",
  150. ret);
  151. if (chip->runtime_pm)
  152. pm_runtime_put(map->dev);
  153. return IRQ_NONE;
  154. }
  155. }
  156. /*
  157. * Ignore masked IRQs and ack if we need to; we ack early so
  158. * there is no race between handling and acknowleding the
  159. * interrupt. We assume that typically few of the interrupts
  160. * will fire simultaneously so don't worry about overhead from
  161. * doing a write per register.
  162. */
  163. for (i = 0; i < data->chip->num_regs; i++) {
  164. data->status_buf[i] &= ~data->mask_buf[i];
  165. if (data->status_buf[i] && chip->ack_base) {
  166. reg = chip->ack_base +
  167. (i * map->reg_stride * data->irq_reg_stride);
  168. ret = regmap_write(map, reg, data->status_buf[i]);
  169. if (ret != 0)
  170. dev_err(map->dev, "Failed to ack 0x%x: %d\n",
  171. reg, ret);
  172. }
  173. }
  174. for (i = 0; i < chip->num_irqs; i++) {
  175. if (data->status_buf[chip->irqs[i].reg_offset /
  176. map->reg_stride] & chip->irqs[i].mask) {
  177. handle_nested_irq(irq_find_mapping(data->domain, i));
  178. handled = true;
  179. }
  180. }
  181. if (chip->runtime_pm)
  182. pm_runtime_put(map->dev);
  183. if (handled)
  184. return IRQ_HANDLED;
  185. else
  186. return IRQ_NONE;
  187. }
  188. static int regmap_irq_map(struct irq_domain *h, unsigned int virq,
  189. irq_hw_number_t hw)
  190. {
  191. struct regmap_irq_chip_data *data = h->host_data;
  192. irq_set_chip_data(virq, data);
  193. irq_set_chip(virq, &data->irq_chip);
  194. irq_set_nested_thread(virq, 1);
  195. /* ARM needs us to explicitly flag the IRQ as valid
  196. * and will set them noprobe when we do so. */
  197. #ifdef CONFIG_ARM
  198. set_irq_flags(virq, IRQF_VALID);
  199. #else
  200. irq_set_noprobe(virq);
  201. #endif
  202. return 0;
  203. }
  204. static struct irq_domain_ops regmap_domain_ops = {
  205. .map = regmap_irq_map,
  206. .xlate = irq_domain_xlate_twocell,
  207. };
  208. /**
  209. * regmap_add_irq_chip(): Use standard regmap IRQ controller handling
  210. *
  211. * map: The regmap for the device.
  212. * irq: The IRQ the device uses to signal interrupts
  213. * irq_flags: The IRQF_ flags to use for the primary interrupt.
  214. * chip: Configuration for the interrupt controller.
  215. * data: Runtime data structure for the controller, allocated on success
  216. *
  217. * Returns 0 on success or an errno on failure.
  218. *
  219. * In order for this to be efficient the chip really should use a
  220. * register cache. The chip driver is responsible for restoring the
  221. * register values used by the IRQ controller over suspend and resume.
  222. */
  223. int regmap_add_irq_chip(struct regmap *map, int irq, int irq_flags,
  224. int irq_base, const struct regmap_irq_chip *chip,
  225. struct regmap_irq_chip_data **data)
  226. {
  227. struct regmap_irq_chip_data *d;
  228. int i;
  229. int ret = -ENOMEM;
  230. u32 reg;
  231. for (i = 0; i < chip->num_irqs; i++) {
  232. if (chip->irqs[i].reg_offset % map->reg_stride)
  233. return -EINVAL;
  234. if (chip->irqs[i].reg_offset / map->reg_stride >=
  235. chip->num_regs)
  236. return -EINVAL;
  237. }
  238. if (irq_base) {
  239. irq_base = irq_alloc_descs(irq_base, 0, chip->num_irqs, 0);
  240. if (irq_base < 0) {
  241. dev_warn(map->dev, "Failed to allocate IRQs: %d\n",
  242. irq_base);
  243. return irq_base;
  244. }
  245. }
  246. d = kzalloc(sizeof(*d), GFP_KERNEL);
  247. if (!d)
  248. return -ENOMEM;
  249. *data = d;
  250. d->status_buf = kzalloc(sizeof(unsigned int) * chip->num_regs,
  251. GFP_KERNEL);
  252. if (!d->status_buf)
  253. goto err_alloc;
  254. d->mask_buf = kzalloc(sizeof(unsigned int) * chip->num_regs,
  255. GFP_KERNEL);
  256. if (!d->mask_buf)
  257. goto err_alloc;
  258. d->mask_buf_def = kzalloc(sizeof(unsigned int) * chip->num_regs,
  259. GFP_KERNEL);
  260. if (!d->mask_buf_def)
  261. goto err_alloc;
  262. if (chip->wake_base) {
  263. d->wake_buf = kzalloc(sizeof(unsigned int) * chip->num_regs,
  264. GFP_KERNEL);
  265. if (!d->wake_buf)
  266. goto err_alloc;
  267. }
  268. d->irq_chip = regmap_irq_chip;
  269. d->irq_chip.name = chip->name;
  270. d->irq = irq;
  271. d->map = map;
  272. d->chip = chip;
  273. d->irq_base = irq_base;
  274. if (chip->irq_reg_stride)
  275. d->irq_reg_stride = chip->irq_reg_stride;
  276. else
  277. d->irq_reg_stride = 1;
  278. mutex_init(&d->lock);
  279. for (i = 0; i < chip->num_irqs; i++)
  280. d->mask_buf_def[chip->irqs[i].reg_offset / map->reg_stride]
  281. |= chip->irqs[i].mask;
  282. /* Mask all the interrupts by default */
  283. for (i = 0; i < chip->num_regs; i++) {
  284. d->mask_buf[i] = d->mask_buf_def[i];
  285. reg = chip->mask_base +
  286. (i * map->reg_stride * d->irq_reg_stride);
  287. if (chip->mask_invert)
  288. ret = regmap_update_bits(map, reg,
  289. d->mask_buf[i], ~d->mask_buf[i]);
  290. else
  291. ret = regmap_update_bits(map, reg,
  292. d->mask_buf[i], d->mask_buf[i]);
  293. if (ret != 0) {
  294. dev_err(map->dev, "Failed to set masks in 0x%x: %d\n",
  295. reg, ret);
  296. goto err_alloc;
  297. }
  298. }
  299. /* Wake is disabled by default */
  300. if (d->wake_buf) {
  301. for (i = 0; i < chip->num_regs; i++) {
  302. d->wake_buf[i] = d->mask_buf_def[i];
  303. reg = chip->wake_base +
  304. (i * map->reg_stride * d->irq_reg_stride);
  305. ret = regmap_update_bits(map, reg, d->wake_buf[i],
  306. d->wake_buf[i]);
  307. if (ret != 0) {
  308. dev_err(map->dev, "Failed to set masks in 0x%x: %d\n",
  309. reg, ret);
  310. goto err_alloc;
  311. }
  312. }
  313. }
  314. if (irq_base)
  315. d->domain = irq_domain_add_legacy(map->dev->of_node,
  316. chip->num_irqs, irq_base, 0,
  317. &regmap_domain_ops, d);
  318. else
  319. d->domain = irq_domain_add_linear(map->dev->of_node,
  320. chip->num_irqs,
  321. &regmap_domain_ops, d);
  322. if (!d->domain) {
  323. dev_err(map->dev, "Failed to create IRQ domain\n");
  324. ret = -ENOMEM;
  325. goto err_alloc;
  326. }
  327. ret = request_threaded_irq(irq, NULL, regmap_irq_thread, irq_flags,
  328. chip->name, d);
  329. if (ret != 0) {
  330. dev_err(map->dev, "Failed to request IRQ %d: %d\n", irq, ret);
  331. goto err_domain;
  332. }
  333. return 0;
  334. err_domain:
  335. /* Should really dispose of the domain but... */
  336. err_alloc:
  337. kfree(d->wake_buf);
  338. kfree(d->mask_buf_def);
  339. kfree(d->mask_buf);
  340. kfree(d->status_buf);
  341. kfree(d);
  342. return ret;
  343. }
  344. EXPORT_SYMBOL_GPL(regmap_add_irq_chip);
  345. /**
  346. * regmap_del_irq_chip(): Stop interrupt handling for a regmap IRQ chip
  347. *
  348. * @irq: Primary IRQ for the device
  349. * @d: regmap_irq_chip_data allocated by regmap_add_irq_chip()
  350. */
  351. void regmap_del_irq_chip(int irq, struct regmap_irq_chip_data *d)
  352. {
  353. if (!d)
  354. return;
  355. free_irq(irq, d);
  356. /* We should unmap the domain but... */
  357. kfree(d->wake_buf);
  358. kfree(d->mask_buf_def);
  359. kfree(d->mask_buf);
  360. kfree(d->status_buf);
  361. kfree(d);
  362. }
  363. EXPORT_SYMBOL_GPL(regmap_del_irq_chip);
  364. /**
  365. * regmap_irq_chip_get_base(): Retrieve interrupt base for a regmap IRQ chip
  366. *
  367. * Useful for drivers to request their own IRQs.
  368. *
  369. * @data: regmap_irq controller to operate on.
  370. */
  371. int regmap_irq_chip_get_base(struct regmap_irq_chip_data *data)
  372. {
  373. WARN_ON(!data->irq_base);
  374. return data->irq_base;
  375. }
  376. EXPORT_SYMBOL_GPL(regmap_irq_chip_get_base);
  377. /**
  378. * regmap_irq_get_virq(): Map an interrupt on a chip to a virtual IRQ
  379. *
  380. * Useful for drivers to request their own IRQs.
  381. *
  382. * @data: regmap_irq controller to operate on.
  383. * @irq: index of the interrupt requested in the chip IRQs
  384. */
  385. int regmap_irq_get_virq(struct regmap_irq_chip_data *data, int irq)
  386. {
  387. /* Handle holes in the IRQ list */
  388. if (!data->chip->irqs[irq].mask)
  389. return -EINVAL;
  390. return irq_create_mapping(data->domain, irq);
  391. }
  392. EXPORT_SYMBOL_GPL(regmap_irq_get_virq);
  393. /**
  394. * regmap_irq_get_domain(): Retrieve the irq_domain for the chip
  395. *
  396. * Useful for drivers to request their own IRQs and for integration
  397. * with subsystems. For ease of integration NULL is accepted as a
  398. * domain, allowing devices to just call this even if no domain is
  399. * allocated.
  400. *
  401. * @data: regmap_irq controller to operate on.
  402. */
  403. struct irq_domain *regmap_irq_get_domain(struct regmap_irq_chip_data *data)
  404. {
  405. if (data)
  406. return data->domain;
  407. else
  408. return NULL;
  409. }
  410. EXPORT_SYMBOL_GPL(regmap_irq_get_domain);