regmap-irq.c 11 KB

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