regmap-irq.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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/regmap.h>
  14. #include <linux/irq.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/slab.h>
  17. #include "internal.h"
  18. struct regmap_irq_chip_data {
  19. struct mutex lock;
  20. struct regmap *map;
  21. struct regmap_irq_chip *chip;
  22. int irq_base;
  23. void *status_reg_buf;
  24. unsigned int *status_buf;
  25. unsigned int *mask_buf;
  26. unsigned int *mask_buf_def;
  27. };
  28. static inline const
  29. struct regmap_irq *irq_to_regmap_irq(struct regmap_irq_chip_data *data,
  30. int irq)
  31. {
  32. return &data->chip->irqs[irq - data->irq_base];
  33. }
  34. static void regmap_irq_lock(struct irq_data *data)
  35. {
  36. struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data);
  37. mutex_lock(&d->lock);
  38. }
  39. static void regmap_irq_sync_unlock(struct irq_data *data)
  40. {
  41. struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data);
  42. int i, ret;
  43. /*
  44. * If there's been a change in the mask write it back to the
  45. * hardware. We rely on the use of the regmap core cache to
  46. * suppress pointless writes.
  47. */
  48. for (i = 0; i < d->chip->num_regs; i++) {
  49. ret = regmap_update_bits(d->map, d->chip->mask_base + i,
  50. d->mask_buf_def[i], d->mask_buf[i]);
  51. if (ret != 0)
  52. dev_err(d->map->dev, "Failed to sync masks in %x\n",
  53. d->chip->mask_base + i);
  54. }
  55. mutex_unlock(&d->lock);
  56. }
  57. static void regmap_irq_enable(struct irq_data *data)
  58. {
  59. struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data);
  60. const struct regmap_irq *irq_data = irq_to_regmap_irq(d, data->irq);
  61. d->mask_buf[irq_data->reg_offset] &= ~irq_data->mask;
  62. }
  63. static void regmap_irq_disable(struct irq_data *data)
  64. {
  65. struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data);
  66. const struct regmap_irq *irq_data = irq_to_regmap_irq(d, data->irq);
  67. d->mask_buf[irq_data->reg_offset] |= irq_data->mask;
  68. }
  69. static struct irq_chip regmap_irq_chip = {
  70. .name = "regmap",
  71. .irq_bus_lock = regmap_irq_lock,
  72. .irq_bus_sync_unlock = regmap_irq_sync_unlock,
  73. .irq_disable = regmap_irq_disable,
  74. .irq_enable = regmap_irq_enable,
  75. };
  76. static irqreturn_t regmap_irq_thread(int irq, void *d)
  77. {
  78. struct regmap_irq_chip_data *data = d;
  79. struct regmap_irq_chip *chip = data->chip;
  80. struct regmap *map = data->map;
  81. int ret, i;
  82. u8 *buf8 = data->status_reg_buf;
  83. u16 *buf16 = data->status_reg_buf;
  84. u32 *buf32 = data->status_reg_buf;
  85. ret = regmap_bulk_read(map, chip->status_base, data->status_reg_buf,
  86. chip->num_regs);
  87. if (ret != 0) {
  88. dev_err(map->dev, "Failed to read IRQ status: %d\n", ret);
  89. return IRQ_NONE;
  90. }
  91. /*
  92. * Ignore masked IRQs and ack if we need to; we ack early so
  93. * there is no race between handling and acknowleding the
  94. * interrupt. We assume that typically few of the interrupts
  95. * will fire simultaneously so don't worry about overhead from
  96. * doing a write per register.
  97. */
  98. for (i = 0; i < data->chip->num_regs; i++) {
  99. switch (map->format.val_bytes) {
  100. case 1:
  101. data->status_buf[i] = buf8[i];
  102. break;
  103. case 2:
  104. data->status_buf[i] = buf16[i];
  105. break;
  106. case 4:
  107. data->status_buf[i] = buf32[i];
  108. break;
  109. default:
  110. BUG();
  111. return IRQ_NONE;
  112. }
  113. data->status_buf[i] &= ~data->mask_buf[i];
  114. if (data->status_buf[i] && chip->ack_base) {
  115. ret = regmap_write(map, chip->ack_base + i,
  116. data->status_buf[i]);
  117. if (ret != 0)
  118. dev_err(map->dev, "Failed to ack 0x%x: %d\n",
  119. chip->ack_base + i, ret);
  120. }
  121. }
  122. for (i = 0; i < chip->num_irqs; i++) {
  123. if (data->status_buf[chip->irqs[i].reg_offset] &
  124. chip->irqs[i].mask) {
  125. handle_nested_irq(data->irq_base + i);
  126. }
  127. }
  128. return IRQ_HANDLED;
  129. }
  130. /**
  131. * regmap_add_irq_chip(): Use standard regmap IRQ controller handling
  132. *
  133. * map: The regmap for the device.
  134. * irq: The IRQ the device uses to signal interrupts
  135. * irq_flags: The IRQF_ flags to use for the primary interrupt.
  136. * chip: Configuration for the interrupt controller.
  137. * data: Runtime data structure for the controller, allocated on success
  138. *
  139. * Returns 0 on success or an errno on failure.
  140. *
  141. * In order for this to be efficient the chip really should use a
  142. * register cache. The chip driver is responsible for restoring the
  143. * register values used by the IRQ controller over suspend and resume.
  144. */
  145. int regmap_add_irq_chip(struct regmap *map, int irq, int irq_flags,
  146. int irq_base, struct regmap_irq_chip *chip,
  147. struct regmap_irq_chip_data **data)
  148. {
  149. struct regmap_irq_chip_data *d;
  150. int cur_irq, i;
  151. int ret = -ENOMEM;
  152. irq_base = irq_alloc_descs(irq_base, 0, chip->num_irqs, 0);
  153. if (irq_base < 0) {
  154. dev_warn(map->dev, "Failed to allocate IRQs: %d\n",
  155. irq_base);
  156. return irq_base;
  157. }
  158. d = kzalloc(sizeof(*d), GFP_KERNEL);
  159. if (!d)
  160. return -ENOMEM;
  161. d->status_buf = kzalloc(sizeof(unsigned int) * chip->num_regs,
  162. GFP_KERNEL);
  163. if (!d->status_buf)
  164. goto err_alloc;
  165. d->status_reg_buf = kzalloc(map->format.val_bytes * chip->num_regs,
  166. GFP_KERNEL);
  167. if (!d->status_reg_buf)
  168. goto err_alloc;
  169. d->mask_buf = kzalloc(sizeof(unsigned int) * chip->num_regs,
  170. GFP_KERNEL);
  171. if (!d->mask_buf)
  172. goto err_alloc;
  173. d->mask_buf_def = kzalloc(sizeof(unsigned int) * chip->num_regs,
  174. GFP_KERNEL);
  175. if (!d->mask_buf_def)
  176. goto err_alloc;
  177. d->map = map;
  178. d->chip = chip;
  179. d->irq_base = irq_base;
  180. mutex_init(&d->lock);
  181. for (i = 0; i < chip->num_irqs; i++)
  182. d->mask_buf_def[chip->irqs[i].reg_offset]
  183. |= chip->irqs[i].mask;
  184. /* Mask all the interrupts by default */
  185. for (i = 0; i < chip->num_regs; i++) {
  186. d->mask_buf[i] = d->mask_buf_def[i];
  187. ret = regmap_write(map, chip->mask_base + i, d->mask_buf[i]);
  188. if (ret != 0) {
  189. dev_err(map->dev, "Failed to set masks in 0x%x: %d\n",
  190. chip->mask_base + i, ret);
  191. goto err_alloc;
  192. }
  193. }
  194. /* Register them with genirq */
  195. for (cur_irq = irq_base;
  196. cur_irq < chip->num_irqs + irq_base;
  197. cur_irq++) {
  198. irq_set_chip_data(cur_irq, d);
  199. irq_set_chip_and_handler(cur_irq, &regmap_irq_chip,
  200. handle_edge_irq);
  201. irq_set_nested_thread(cur_irq, 1);
  202. /* ARM needs us to explicitly flag the IRQ as valid
  203. * and will set them noprobe when we do so. */
  204. #ifdef CONFIG_ARM
  205. set_irq_flags(cur_irq, IRQF_VALID);
  206. #else
  207. irq_set_noprobe(cur_irq);
  208. #endif
  209. }
  210. ret = request_threaded_irq(irq, NULL, regmap_irq_thread, irq_flags,
  211. chip->name, d);
  212. if (ret != 0) {
  213. dev_err(map->dev, "Failed to request IRQ %d: %d\n", irq, ret);
  214. goto err_alloc;
  215. }
  216. return 0;
  217. err_alloc:
  218. kfree(d->mask_buf_def);
  219. kfree(d->mask_buf);
  220. kfree(d->status_reg_buf);
  221. kfree(d->status_buf);
  222. kfree(d);
  223. return ret;
  224. }
  225. EXPORT_SYMBOL_GPL(regmap_add_irq_chip);
  226. /**
  227. * regmap_del_irq_chip(): Stop interrupt handling for a regmap IRQ chip
  228. *
  229. * @irq: Primary IRQ for the device
  230. * @d: regmap_irq_chip_data allocated by regmap_add_irq_chip()
  231. */
  232. void regmap_del_irq_chip(int irq, struct regmap_irq_chip_data *d)
  233. {
  234. if (!d)
  235. return;
  236. free_irq(irq, d);
  237. kfree(d->mask_buf_def);
  238. kfree(d->mask_buf);
  239. kfree(d->status_reg_buf);
  240. kfree(d->status_buf);
  241. kfree(d);
  242. }
  243. EXPORT_SYMBOL_GPL(regmap_del_irq_chip);