regmap-irq.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. bool handled = false;
  86. ret = regmap_bulk_read(map, chip->status_base, data->status_reg_buf,
  87. chip->num_regs);
  88. if (ret != 0) {
  89. dev_err(map->dev, "Failed to read IRQ status: %d\n", ret);
  90. return IRQ_NONE;
  91. }
  92. /*
  93. * Ignore masked IRQs and ack if we need to; we ack early so
  94. * there is no race between handling and acknowleding the
  95. * interrupt. We assume that typically few of the interrupts
  96. * will fire simultaneously so don't worry about overhead from
  97. * doing a write per register.
  98. */
  99. for (i = 0; i < data->chip->num_regs; i++) {
  100. switch (map->format.val_bytes) {
  101. case 1:
  102. data->status_buf[i] = buf8[i];
  103. break;
  104. case 2:
  105. data->status_buf[i] = buf16[i];
  106. break;
  107. case 4:
  108. data->status_buf[i] = buf32[i];
  109. break;
  110. default:
  111. BUG();
  112. return IRQ_NONE;
  113. }
  114. data->status_buf[i] &= ~data->mask_buf[i];
  115. if (data->status_buf[i] && chip->ack_base) {
  116. ret = regmap_write(map, chip->ack_base + i,
  117. data->status_buf[i]);
  118. if (ret != 0)
  119. dev_err(map->dev, "Failed to ack 0x%x: %d\n",
  120. chip->ack_base + i, ret);
  121. }
  122. }
  123. for (i = 0; i < chip->num_irqs; i++) {
  124. if (data->status_buf[chip->irqs[i].reg_offset] &
  125. chip->irqs[i].mask) {
  126. handle_nested_irq(data->irq_base + i);
  127. handled = true;
  128. }
  129. }
  130. if (handled)
  131. return IRQ_HANDLED;
  132. else
  133. return IRQ_NONE;
  134. }
  135. /**
  136. * regmap_add_irq_chip(): Use standard regmap IRQ controller handling
  137. *
  138. * map: The regmap for the device.
  139. * irq: The IRQ the device uses to signal interrupts
  140. * irq_flags: The IRQF_ flags to use for the primary interrupt.
  141. * chip: Configuration for the interrupt controller.
  142. * data: Runtime data structure for the controller, allocated on success
  143. *
  144. * Returns 0 on success or an errno on failure.
  145. *
  146. * In order for this to be efficient the chip really should use a
  147. * register cache. The chip driver is responsible for restoring the
  148. * register values used by the IRQ controller over suspend and resume.
  149. */
  150. int regmap_add_irq_chip(struct regmap *map, int irq, int irq_flags,
  151. int irq_base, struct regmap_irq_chip *chip,
  152. struct regmap_irq_chip_data **data)
  153. {
  154. struct regmap_irq_chip_data *d;
  155. int cur_irq, i;
  156. int ret = -ENOMEM;
  157. irq_base = irq_alloc_descs(irq_base, 0, chip->num_irqs, 0);
  158. if (irq_base < 0) {
  159. dev_warn(map->dev, "Failed to allocate IRQs: %d\n",
  160. irq_base);
  161. return irq_base;
  162. }
  163. d = kzalloc(sizeof(*d), GFP_KERNEL);
  164. if (!d)
  165. return -ENOMEM;
  166. d->status_buf = kzalloc(sizeof(unsigned int) * chip->num_regs,
  167. GFP_KERNEL);
  168. if (!d->status_buf)
  169. goto err_alloc;
  170. d->status_reg_buf = kzalloc(map->format.val_bytes * chip->num_regs,
  171. GFP_KERNEL);
  172. if (!d->status_reg_buf)
  173. goto err_alloc;
  174. d->mask_buf = kzalloc(sizeof(unsigned int) * chip->num_regs,
  175. GFP_KERNEL);
  176. if (!d->mask_buf)
  177. goto err_alloc;
  178. d->mask_buf_def = kzalloc(sizeof(unsigned int) * chip->num_regs,
  179. GFP_KERNEL);
  180. if (!d->mask_buf_def)
  181. goto err_alloc;
  182. d->map = map;
  183. d->chip = chip;
  184. d->irq_base = irq_base;
  185. mutex_init(&d->lock);
  186. for (i = 0; i < chip->num_irqs; i++)
  187. d->mask_buf_def[chip->irqs[i].reg_offset]
  188. |= chip->irqs[i].mask;
  189. /* Mask all the interrupts by default */
  190. for (i = 0; i < chip->num_regs; i++) {
  191. d->mask_buf[i] = d->mask_buf_def[i];
  192. ret = regmap_write(map, chip->mask_base + i, d->mask_buf[i]);
  193. if (ret != 0) {
  194. dev_err(map->dev, "Failed to set masks in 0x%x: %d\n",
  195. chip->mask_base + i, ret);
  196. goto err_alloc;
  197. }
  198. }
  199. /* Register them with genirq */
  200. for (cur_irq = irq_base;
  201. cur_irq < chip->num_irqs + irq_base;
  202. cur_irq++) {
  203. irq_set_chip_data(cur_irq, d);
  204. irq_set_chip_and_handler(cur_irq, &regmap_irq_chip,
  205. handle_edge_irq);
  206. irq_set_nested_thread(cur_irq, 1);
  207. /* ARM needs us to explicitly flag the IRQ as valid
  208. * and will set them noprobe when we do so. */
  209. #ifdef CONFIG_ARM
  210. set_irq_flags(cur_irq, IRQF_VALID);
  211. #else
  212. irq_set_noprobe(cur_irq);
  213. #endif
  214. }
  215. ret = request_threaded_irq(irq, NULL, regmap_irq_thread, irq_flags,
  216. chip->name, d);
  217. if (ret != 0) {
  218. dev_err(map->dev, "Failed to request IRQ %d: %d\n", irq, ret);
  219. goto err_alloc;
  220. }
  221. return 0;
  222. err_alloc:
  223. kfree(d->mask_buf_def);
  224. kfree(d->mask_buf);
  225. kfree(d->status_reg_buf);
  226. kfree(d->status_buf);
  227. kfree(d);
  228. return ret;
  229. }
  230. EXPORT_SYMBOL_GPL(regmap_add_irq_chip);
  231. /**
  232. * regmap_del_irq_chip(): Stop interrupt handling for a regmap IRQ chip
  233. *
  234. * @irq: Primary IRQ for the device
  235. * @d: regmap_irq_chip_data allocated by regmap_add_irq_chip()
  236. */
  237. void regmap_del_irq_chip(int irq, struct regmap_irq_chip_data *d)
  238. {
  239. if (!d)
  240. return;
  241. free_irq(irq, d);
  242. kfree(d->mask_buf_def);
  243. kfree(d->mask_buf);
  244. kfree(d->status_reg_buf);
  245. kfree(d->status_buf);
  246. kfree(d);
  247. }
  248. EXPORT_SYMBOL_GPL(regmap_del_irq_chip);
  249. /**
  250. * regmap_irq_chip_get_base(): Retrieve interrupt base for a regmap IRQ chip
  251. *
  252. * Useful for drivers to request their own IRQs.
  253. *
  254. * @data: regmap_irq controller to operate on.
  255. */
  256. int regmap_irq_chip_get_base(struct regmap_irq_chip_data *data)
  257. {
  258. return data->irq_base;
  259. }
  260. EXPORT_SYMBOL_GPL(regmap_irq_chip_get_base);