regmap-irq.c 13 KB

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