regmap-irq.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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. return IRQ_NONE;
  159. }
  160. }
  161. /*
  162. * Read in the statuses, using a single bulk read if possible
  163. * in order to reduce the I/O overheads.
  164. */
  165. if (!map->use_single_rw && map->reg_stride == 1 &&
  166. data->irq_reg_stride == 1) {
  167. u8 *buf8 = data->status_reg_buf;
  168. u16 *buf16 = data->status_reg_buf;
  169. u32 *buf32 = data->status_reg_buf;
  170. BUG_ON(!data->status_reg_buf);
  171. ret = regmap_bulk_read(map, chip->status_base,
  172. data->status_reg_buf,
  173. chip->num_regs);
  174. if (ret != 0) {
  175. dev_err(map->dev, "Failed to read IRQ status: %d\n",
  176. ret);
  177. return IRQ_NONE;
  178. }
  179. for (i = 0; i < data->chip->num_regs; i++) {
  180. switch (map->format.val_bytes) {
  181. case 1:
  182. data->status_buf[i] = buf8[i];
  183. break;
  184. case 2:
  185. data->status_buf[i] = buf16[i];
  186. break;
  187. case 4:
  188. data->status_buf[i] = buf32[i];
  189. break;
  190. default:
  191. BUG();
  192. return IRQ_NONE;
  193. }
  194. }
  195. } else {
  196. for (i = 0; i < data->chip->num_regs; i++) {
  197. ret = regmap_read(map, chip->status_base +
  198. (i * map->reg_stride
  199. * data->irq_reg_stride),
  200. &data->status_buf[i]);
  201. if (ret != 0) {
  202. dev_err(map->dev,
  203. "Failed to read IRQ status: %d\n",
  204. ret);
  205. if (chip->runtime_pm)
  206. pm_runtime_put(map->dev);
  207. return IRQ_NONE;
  208. }
  209. }
  210. }
  211. /*
  212. * Ignore masked IRQs and ack if we need to; we ack early so
  213. * there is no race between handling and acknowleding the
  214. * interrupt. We assume that typically few of the interrupts
  215. * will fire simultaneously so don't worry about overhead from
  216. * doing a write per register.
  217. */
  218. for (i = 0; i < data->chip->num_regs; i++) {
  219. data->status_buf[i] &= ~data->mask_buf[i];
  220. if (data->status_buf[i] && chip->ack_base) {
  221. reg = chip->ack_base +
  222. (i * map->reg_stride * data->irq_reg_stride);
  223. ret = regmap_write(map, reg, data->status_buf[i]);
  224. if (ret != 0)
  225. dev_err(map->dev, "Failed to ack 0x%x: %d\n",
  226. reg, ret);
  227. }
  228. }
  229. for (i = 0; i < chip->num_irqs; i++) {
  230. if (data->status_buf[chip->irqs[i].reg_offset /
  231. map->reg_stride] & chip->irqs[i].mask) {
  232. handle_nested_irq(irq_find_mapping(data->domain, i));
  233. handled = true;
  234. }
  235. }
  236. if (chip->runtime_pm)
  237. pm_runtime_put(map->dev);
  238. if (handled)
  239. return IRQ_HANDLED;
  240. else
  241. return IRQ_NONE;
  242. }
  243. static int regmap_irq_map(struct irq_domain *h, unsigned int virq,
  244. irq_hw_number_t hw)
  245. {
  246. struct regmap_irq_chip_data *data = h->host_data;
  247. irq_set_chip_data(virq, data);
  248. irq_set_chip(virq, &data->irq_chip);
  249. irq_set_nested_thread(virq, 1);
  250. /* ARM needs us to explicitly flag the IRQ as valid
  251. * and will set them noprobe when we do so. */
  252. #ifdef CONFIG_ARM
  253. set_irq_flags(virq, IRQF_VALID);
  254. #else
  255. irq_set_noprobe(virq);
  256. #endif
  257. return 0;
  258. }
  259. static struct irq_domain_ops regmap_domain_ops = {
  260. .map = regmap_irq_map,
  261. .xlate = irq_domain_xlate_twocell,
  262. };
  263. /**
  264. * regmap_add_irq_chip(): Use standard regmap IRQ controller handling
  265. *
  266. * map: The regmap for the device.
  267. * irq: The IRQ the device uses to signal interrupts
  268. * irq_flags: The IRQF_ flags to use for the primary interrupt.
  269. * chip: Configuration for the interrupt controller.
  270. * data: Runtime data structure for the controller, allocated on success
  271. *
  272. * Returns 0 on success or an errno on failure.
  273. *
  274. * In order for this to be efficient the chip really should use a
  275. * register cache. The chip driver is responsible for restoring the
  276. * register values used by the IRQ controller over suspend and resume.
  277. */
  278. int regmap_add_irq_chip(struct regmap *map, int irq, int irq_flags,
  279. int irq_base, const struct regmap_irq_chip *chip,
  280. struct regmap_irq_chip_data **data)
  281. {
  282. struct regmap_irq_chip_data *d;
  283. int i;
  284. int ret = -ENOMEM;
  285. u32 reg;
  286. for (i = 0; i < chip->num_irqs; i++) {
  287. if (chip->irqs[i].reg_offset % map->reg_stride)
  288. return -EINVAL;
  289. if (chip->irqs[i].reg_offset / map->reg_stride >=
  290. chip->num_regs)
  291. return -EINVAL;
  292. }
  293. if (irq_base) {
  294. irq_base = irq_alloc_descs(irq_base, 0, chip->num_irqs, 0);
  295. if (irq_base < 0) {
  296. dev_warn(map->dev, "Failed to allocate IRQs: %d\n",
  297. irq_base);
  298. return irq_base;
  299. }
  300. }
  301. d = kzalloc(sizeof(*d), GFP_KERNEL);
  302. if (!d)
  303. return -ENOMEM;
  304. *data = d;
  305. d->status_buf = kzalloc(sizeof(unsigned int) * chip->num_regs,
  306. GFP_KERNEL);
  307. if (!d->status_buf)
  308. goto err_alloc;
  309. d->mask_buf = kzalloc(sizeof(unsigned int) * chip->num_regs,
  310. GFP_KERNEL);
  311. if (!d->mask_buf)
  312. goto err_alloc;
  313. d->mask_buf_def = kzalloc(sizeof(unsigned int) * chip->num_regs,
  314. GFP_KERNEL);
  315. if (!d->mask_buf_def)
  316. goto err_alloc;
  317. if (chip->wake_base) {
  318. d->wake_buf = kzalloc(sizeof(unsigned int) * chip->num_regs,
  319. GFP_KERNEL);
  320. if (!d->wake_buf)
  321. goto err_alloc;
  322. }
  323. d->irq_chip = regmap_irq_chip;
  324. d->irq_chip.name = chip->name;
  325. d->irq = irq;
  326. d->map = map;
  327. d->chip = chip;
  328. d->irq_base = irq_base;
  329. if (chip->irq_reg_stride)
  330. d->irq_reg_stride = chip->irq_reg_stride;
  331. else
  332. d->irq_reg_stride = 1;
  333. if (!map->use_single_rw && map->reg_stride == 1 &&
  334. d->irq_reg_stride == 1) {
  335. d->status_reg_buf = kmalloc(map->format.val_bytes *
  336. chip->num_regs, GFP_KERNEL);
  337. if (!d->status_reg_buf)
  338. goto err_alloc;
  339. }
  340. mutex_init(&d->lock);
  341. for (i = 0; i < chip->num_irqs; i++)
  342. d->mask_buf_def[chip->irqs[i].reg_offset / map->reg_stride]
  343. |= chip->irqs[i].mask;
  344. /* Mask all the interrupts by default */
  345. for (i = 0; i < chip->num_regs; i++) {
  346. d->mask_buf[i] = d->mask_buf_def[i];
  347. reg = chip->mask_base +
  348. (i * map->reg_stride * d->irq_reg_stride);
  349. if (chip->mask_invert)
  350. ret = regmap_update_bits(map, reg,
  351. d->mask_buf[i], ~d->mask_buf[i]);
  352. else
  353. ret = regmap_update_bits(map, reg,
  354. d->mask_buf[i], d->mask_buf[i]);
  355. if (ret != 0) {
  356. dev_err(map->dev, "Failed to set masks in 0x%x: %d\n",
  357. reg, ret);
  358. goto err_alloc;
  359. }
  360. }
  361. /* Wake is disabled by default */
  362. if (d->wake_buf) {
  363. for (i = 0; i < chip->num_regs; i++) {
  364. d->wake_buf[i] = d->mask_buf_def[i];
  365. reg = chip->wake_base +
  366. (i * map->reg_stride * d->irq_reg_stride);
  367. if (chip->wake_invert)
  368. ret = regmap_update_bits(map, reg,
  369. d->mask_buf_def[i],
  370. 0);
  371. else
  372. ret = regmap_update_bits(map, reg,
  373. d->mask_buf_def[i],
  374. d->wake_buf[i]);
  375. if (ret != 0) {
  376. dev_err(map->dev, "Failed to set masks in 0x%x: %d\n",
  377. reg, ret);
  378. goto err_alloc;
  379. }
  380. }
  381. }
  382. if (irq_base)
  383. d->domain = irq_domain_add_legacy(map->dev->of_node,
  384. chip->num_irqs, irq_base, 0,
  385. &regmap_domain_ops, d);
  386. else
  387. d->domain = irq_domain_add_linear(map->dev->of_node,
  388. chip->num_irqs,
  389. &regmap_domain_ops, d);
  390. if (!d->domain) {
  391. dev_err(map->dev, "Failed to create IRQ domain\n");
  392. ret = -ENOMEM;
  393. goto err_alloc;
  394. }
  395. ret = request_threaded_irq(irq, NULL, regmap_irq_thread, irq_flags,
  396. chip->name, d);
  397. if (ret != 0) {
  398. dev_err(map->dev, "Failed to request IRQ %d: %d\n", irq, ret);
  399. goto err_domain;
  400. }
  401. return 0;
  402. err_domain:
  403. /* Should really dispose of the domain but... */
  404. err_alloc:
  405. kfree(d->wake_buf);
  406. kfree(d->mask_buf_def);
  407. kfree(d->mask_buf);
  408. kfree(d->status_buf);
  409. kfree(d->status_reg_buf);
  410. kfree(d);
  411. return ret;
  412. }
  413. EXPORT_SYMBOL_GPL(regmap_add_irq_chip);
  414. /**
  415. * regmap_del_irq_chip(): Stop interrupt handling for a regmap IRQ chip
  416. *
  417. * @irq: Primary IRQ for the device
  418. * @d: regmap_irq_chip_data allocated by regmap_add_irq_chip()
  419. */
  420. void regmap_del_irq_chip(int irq, struct regmap_irq_chip_data *d)
  421. {
  422. if (!d)
  423. return;
  424. free_irq(irq, d);
  425. /* We should unmap the domain but... */
  426. kfree(d->wake_buf);
  427. kfree(d->mask_buf_def);
  428. kfree(d->mask_buf);
  429. kfree(d->status_reg_buf);
  430. kfree(d->status_buf);
  431. kfree(d);
  432. }
  433. EXPORT_SYMBOL_GPL(regmap_del_irq_chip);
  434. /**
  435. * regmap_irq_chip_get_base(): Retrieve interrupt base for a regmap IRQ chip
  436. *
  437. * Useful for drivers to request their own IRQs.
  438. *
  439. * @data: regmap_irq controller to operate on.
  440. */
  441. int regmap_irq_chip_get_base(struct regmap_irq_chip_data *data)
  442. {
  443. WARN_ON(!data->irq_base);
  444. return data->irq_base;
  445. }
  446. EXPORT_SYMBOL_GPL(regmap_irq_chip_get_base);
  447. /**
  448. * regmap_irq_get_virq(): Map an interrupt on a chip to a virtual IRQ
  449. *
  450. * Useful for drivers to request their own IRQs.
  451. *
  452. * @data: regmap_irq controller to operate on.
  453. * @irq: index of the interrupt requested in the chip IRQs
  454. */
  455. int regmap_irq_get_virq(struct regmap_irq_chip_data *data, int irq)
  456. {
  457. /* Handle holes in the IRQ list */
  458. if (!data->chip->irqs[irq].mask)
  459. return -EINVAL;
  460. return irq_create_mapping(data->domain, irq);
  461. }
  462. EXPORT_SYMBOL_GPL(regmap_irq_get_virq);
  463. /**
  464. * regmap_irq_get_domain(): Retrieve the irq_domain for the chip
  465. *
  466. * Useful for drivers to request their own IRQs and for integration
  467. * with subsystems. For ease of integration NULL is accepted as a
  468. * domain, allowing devices to just call this even if no domain is
  469. * allocated.
  470. *
  471. * @data: regmap_irq controller to operate on.
  472. */
  473. struct irq_domain *regmap_irq_get_domain(struct regmap_irq_chip_data *data)
  474. {
  475. if (data)
  476. return data->domain;
  477. else
  478. return NULL;
  479. }
  480. EXPORT_SYMBOL_GPL(regmap_irq_get_domain);