regmap-irq.c 7.5 KB

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