Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 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 | |
Paul Gortmaker | 51990e8 | 2012-01-22 11:23:42 -0500 | [diff] [blame] | 13 | #include <linux/device.h> |
Xiubo Li | e128920 | 2014-05-19 15:13:45 +0800 | [diff] [blame] | 14 | #include <linux/export.h> |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 15 | #include <linux/interrupt.h> |
Xiubo Li | e128920 | 2014-05-19 15:13:45 +0800 | [diff] [blame] | 16 | #include <linux/irq.h> |
Mark Brown | 4af8be6 | 2012-05-13 10:59:56 +0100 | [diff] [blame] | 17 | #include <linux/irqdomain.h> |
Mark Brown | 0c00c50 | 2012-07-24 15:41:19 +0100 | [diff] [blame] | 18 | #include <linux/pm_runtime.h> |
Xiubo Li | e128920 | 2014-05-19 15:13:45 +0800 | [diff] [blame] | 19 | #include <linux/regmap.h> |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 20 | #include <linux/slab.h> |
| 21 | |
| 22 | #include "internal.h" |
| 23 | |
| 24 | struct regmap_irq_chip_data { |
| 25 | struct mutex lock; |
Stephen Warren | 7ac140e | 2012-08-01 11:40:47 -0600 | [diff] [blame] | 26 | struct irq_chip irq_chip; |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 27 | |
| 28 | struct regmap *map; |
Mark Brown | b026ddb | 2012-05-31 21:01:46 +0100 | [diff] [blame] | 29 | const struct regmap_irq_chip *chip; |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 30 | |
| 31 | int irq_base; |
Mark Brown | 4af8be6 | 2012-05-13 10:59:56 +0100 | [diff] [blame] | 32 | struct irq_domain *domain; |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 33 | |
Mark Brown | a43fd50 | 2012-06-05 14:34:03 +0100 | [diff] [blame] | 34 | int irq; |
| 35 | int wake_count; |
| 36 | |
Mark Brown | a7440ea | 2013-01-03 14:27:15 +0000 | [diff] [blame] | 37 | void *status_reg_buf; |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 38 | unsigned int *status_buf; |
| 39 | unsigned int *mask_buf; |
| 40 | unsigned int *mask_buf_def; |
Mark Brown | a43fd50 | 2012-06-05 14:34:03 +0100 | [diff] [blame] | 41 | unsigned int *wake_buf; |
Graeme Gregory | 022f926a | 2012-05-14 22:40:43 +0900 | [diff] [blame] | 42 | |
| 43 | unsigned int irq_reg_stride; |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | static inline const |
| 47 | struct regmap_irq *irq_to_regmap_irq(struct regmap_irq_chip_data *data, |
| 48 | int irq) |
| 49 | { |
Mark Brown | 4af8be6 | 2012-05-13 10:59:56 +0100 | [diff] [blame] | 50 | return &data->chip->irqs[irq]; |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | static void regmap_irq_lock(struct irq_data *data) |
| 54 | { |
| 55 | struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data); |
| 56 | |
| 57 | mutex_lock(&d->lock); |
| 58 | } |
| 59 | |
| 60 | static void regmap_irq_sync_unlock(struct irq_data *data) |
| 61 | { |
| 62 | struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data); |
Stephen Warren | 5680655 | 2012-04-10 23:37:22 -0600 | [diff] [blame] | 63 | struct regmap *map = d->map; |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 64 | int i, ret; |
Stephen Warren | 1603262 | 2012-07-27 13:01:54 -0600 | [diff] [blame] | 65 | u32 reg; |
Guo Zeng | 7b7d196 | 2015-09-17 05:23:20 +0000 | [diff] [blame] | 66 | u32 unmask_offset; |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 67 | |
Mark Brown | 0c00c50 | 2012-07-24 15:41:19 +0100 | [diff] [blame] | 68 | if (d->chip->runtime_pm) { |
| 69 | ret = pm_runtime_get_sync(map->dev); |
| 70 | if (ret < 0) |
| 71 | dev_err(map->dev, "IRQ sync failed to resume: %d\n", |
| 72 | ret); |
| 73 | } |
| 74 | |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 75 | /* |
| 76 | * If there's been a change in the mask write it back to the |
| 77 | * hardware. We rely on the use of the regmap core cache to |
| 78 | * suppress pointless writes. |
| 79 | */ |
| 80 | for (i = 0; i < d->chip->num_regs; i++) { |
Stephen Warren | 1603262 | 2012-07-27 13:01:54 -0600 | [diff] [blame] | 81 | reg = d->chip->mask_base + |
| 82 | (i * map->reg_stride * d->irq_reg_stride); |
Guo Zeng | 7b7d196 | 2015-09-17 05:23:20 +0000 | [diff] [blame] | 83 | if (d->chip->mask_invert) { |
Xiaofan Tian | 36ac914 | 2012-08-30 17:03:35 +0800 | [diff] [blame] | 84 | ret = regmap_update_bits(d->map, reg, |
| 85 | d->mask_buf_def[i], ~d->mask_buf[i]); |
Guo Zeng | 7b7d196 | 2015-09-17 05:23:20 +0000 | [diff] [blame] | 86 | } else if (d->chip->unmask_base) { |
| 87 | /* set mask with mask_base register */ |
| 88 | ret = regmap_update_bits(d->map, reg, |
| 89 | d->mask_buf_def[i], ~d->mask_buf[i]); |
| 90 | if (ret < 0) |
| 91 | dev_err(d->map->dev, |
| 92 | "Failed to sync unmasks in %x\n", |
| 93 | reg); |
| 94 | unmask_offset = d->chip->unmask_base - |
| 95 | d->chip->mask_base; |
| 96 | /* clear mask with unmask_base register */ |
| 97 | ret = regmap_update_bits(d->map, |
| 98 | reg + unmask_offset, |
| 99 | d->mask_buf_def[i], |
| 100 | d->mask_buf[i]); |
| 101 | } else { |
Xiaofan Tian | 36ac914 | 2012-08-30 17:03:35 +0800 | [diff] [blame] | 102 | ret = regmap_update_bits(d->map, reg, |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 103 | d->mask_buf_def[i], d->mask_buf[i]); |
Guo Zeng | 7b7d196 | 2015-09-17 05:23:20 +0000 | [diff] [blame] | 104 | } |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 105 | if (ret != 0) |
| 106 | dev_err(d->map->dev, "Failed to sync masks in %x\n", |
Stephen Warren | 1603262 | 2012-07-27 13:01:54 -0600 | [diff] [blame] | 107 | reg); |
Mark Brown | 33be493 | 2013-01-04 16:32:54 +0000 | [diff] [blame] | 108 | |
| 109 | reg = d->chip->wake_base + |
| 110 | (i * map->reg_stride * d->irq_reg_stride); |
| 111 | if (d->wake_buf) { |
Mark Brown | 9442490 | 2013-01-04 16:35:07 +0000 | [diff] [blame] | 112 | if (d->chip->wake_invert) |
| 113 | ret = regmap_update_bits(d->map, reg, |
| 114 | d->mask_buf_def[i], |
| 115 | ~d->wake_buf[i]); |
| 116 | else |
| 117 | ret = regmap_update_bits(d->map, reg, |
| 118 | d->mask_buf_def[i], |
| 119 | d->wake_buf[i]); |
Mark Brown | 33be493 | 2013-01-04 16:32:54 +0000 | [diff] [blame] | 120 | if (ret != 0) |
| 121 | dev_err(d->map->dev, |
| 122 | "Failed to sync wakes in %x: %d\n", |
| 123 | reg, ret); |
| 124 | } |
Yi Zhang | 4bd7145 | 2013-10-22 18:44:32 +0800 | [diff] [blame] | 125 | |
| 126 | if (!d->chip->init_ack_masked) |
| 127 | continue; |
| 128 | /* |
dashsriram | 7043f5f | 2015-05-27 00:55:13 +0530 | [diff] [blame] | 129 | * Ack all the masked interrupts unconditionally, |
Yi Zhang | 4bd7145 | 2013-10-22 18:44:32 +0800 | [diff] [blame] | 130 | * OR if there is masked interrupt which hasn't been Acked, |
| 131 | * it'll be ignored in irq handler, then may introduce irq storm |
| 132 | */ |
Alexander Shiyan | d323343 | 2013-12-15 13:36:51 +0400 | [diff] [blame] | 133 | if (d->mask_buf[i] && (d->chip->ack_base || d->chip->use_ack)) { |
Yi Zhang | 4bd7145 | 2013-10-22 18:44:32 +0800 | [diff] [blame] | 134 | reg = d->chip->ack_base + |
| 135 | (i * map->reg_stride * d->irq_reg_stride); |
Guo Zeng | a650fdd | 2015-09-17 05:23:21 +0000 | [diff] [blame^] | 136 | /* some chips ack by write 0 */ |
| 137 | if (d->chip->ack_invert) |
| 138 | ret = regmap_write(map, reg, ~d->mask_buf[i]); |
| 139 | else |
| 140 | ret = regmap_write(map, reg, d->mask_buf[i]); |
Yi Zhang | 4bd7145 | 2013-10-22 18:44:32 +0800 | [diff] [blame] | 141 | if (ret != 0) |
| 142 | dev_err(d->map->dev, "Failed to ack 0x%x: %d\n", |
| 143 | reg, ret); |
| 144 | } |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 145 | } |
| 146 | |
Mark Brown | 0c00c50 | 2012-07-24 15:41:19 +0100 | [diff] [blame] | 147 | if (d->chip->runtime_pm) |
| 148 | pm_runtime_put(map->dev); |
| 149 | |
Mark Brown | a43fd50 | 2012-06-05 14:34:03 +0100 | [diff] [blame] | 150 | /* If we've changed our wakeup count propagate it to the parent */ |
| 151 | if (d->wake_count < 0) |
| 152 | for (i = d->wake_count; i < 0; i++) |
| 153 | irq_set_irq_wake(d->irq, 0); |
| 154 | else if (d->wake_count > 0) |
| 155 | for (i = 0; i < d->wake_count; i++) |
| 156 | irq_set_irq_wake(d->irq, 1); |
| 157 | |
| 158 | d->wake_count = 0; |
| 159 | |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 160 | mutex_unlock(&d->lock); |
| 161 | } |
| 162 | |
| 163 | static void regmap_irq_enable(struct irq_data *data) |
| 164 | { |
| 165 | struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data); |
Stephen Warren | 5680655 | 2012-04-10 23:37:22 -0600 | [diff] [blame] | 166 | struct regmap *map = d->map; |
Mark Brown | 4af8be6 | 2012-05-13 10:59:56 +0100 | [diff] [blame] | 167 | const struct regmap_irq *irq_data = irq_to_regmap_irq(d, data->hwirq); |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 168 | |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 169 | d->mask_buf[irq_data->reg_offset / map->reg_stride] &= ~irq_data->mask; |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | static void regmap_irq_disable(struct irq_data *data) |
| 173 | { |
| 174 | struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data); |
Stephen Warren | 5680655 | 2012-04-10 23:37:22 -0600 | [diff] [blame] | 175 | struct regmap *map = d->map; |
Mark Brown | 4af8be6 | 2012-05-13 10:59:56 +0100 | [diff] [blame] | 176 | const struct regmap_irq *irq_data = irq_to_regmap_irq(d, data->hwirq); |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 177 | |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 178 | d->mask_buf[irq_data->reg_offset / map->reg_stride] |= irq_data->mask; |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 179 | } |
| 180 | |
Mark Brown | a43fd50 | 2012-06-05 14:34:03 +0100 | [diff] [blame] | 181 | static int regmap_irq_set_wake(struct irq_data *data, unsigned int on) |
| 182 | { |
| 183 | struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data); |
| 184 | struct regmap *map = d->map; |
| 185 | const struct regmap_irq *irq_data = irq_to_regmap_irq(d, data->hwirq); |
| 186 | |
Mark Brown | a43fd50 | 2012-06-05 14:34:03 +0100 | [diff] [blame] | 187 | if (on) { |
Laxman Dewangan | 55ac85e | 2012-12-19 19:42:28 +0530 | [diff] [blame] | 188 | if (d->wake_buf) |
| 189 | d->wake_buf[irq_data->reg_offset / map->reg_stride] |
| 190 | &= ~irq_data->mask; |
Mark Brown | a43fd50 | 2012-06-05 14:34:03 +0100 | [diff] [blame] | 191 | d->wake_count++; |
| 192 | } else { |
Laxman Dewangan | 55ac85e | 2012-12-19 19:42:28 +0530 | [diff] [blame] | 193 | if (d->wake_buf) |
| 194 | d->wake_buf[irq_data->reg_offset / map->reg_stride] |
| 195 | |= irq_data->mask; |
Mark Brown | a43fd50 | 2012-06-05 14:34:03 +0100 | [diff] [blame] | 196 | d->wake_count--; |
| 197 | } |
| 198 | |
| 199 | return 0; |
| 200 | } |
| 201 | |
Stephen Warren | 7ac140e | 2012-08-01 11:40:47 -0600 | [diff] [blame] | 202 | static const struct irq_chip regmap_irq_chip = { |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 203 | .irq_bus_lock = regmap_irq_lock, |
| 204 | .irq_bus_sync_unlock = regmap_irq_sync_unlock, |
| 205 | .irq_disable = regmap_irq_disable, |
| 206 | .irq_enable = regmap_irq_enable, |
Mark Brown | a43fd50 | 2012-06-05 14:34:03 +0100 | [diff] [blame] | 207 | .irq_set_wake = regmap_irq_set_wake, |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 208 | }; |
| 209 | |
| 210 | static irqreturn_t regmap_irq_thread(int irq, void *d) |
| 211 | { |
| 212 | struct regmap_irq_chip_data *data = d; |
Mark Brown | b026ddb | 2012-05-31 21:01:46 +0100 | [diff] [blame] | 213 | const struct regmap_irq_chip *chip = data->chip; |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 214 | struct regmap *map = data->map; |
| 215 | int ret, i; |
Mark Brown | d23511f | 2011-11-28 18:50:39 +0000 | [diff] [blame] | 216 | bool handled = false; |
Stephen Warren | 1603262 | 2012-07-27 13:01:54 -0600 | [diff] [blame] | 217 | u32 reg; |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 218 | |
Mark Brown | 0c00c50 | 2012-07-24 15:41:19 +0100 | [diff] [blame] | 219 | if (chip->runtime_pm) { |
| 220 | ret = pm_runtime_get_sync(map->dev); |
| 221 | if (ret < 0) { |
| 222 | dev_err(map->dev, "IRQ thread failed to resume: %d\n", |
| 223 | ret); |
Li Fei | 283189d | 2013-02-28 15:37:11 +0800 | [diff] [blame] | 224 | pm_runtime_put(map->dev); |
Mark Brown | 0c00c50 | 2012-07-24 15:41:19 +0100 | [diff] [blame] | 225 | return IRQ_NONE; |
| 226 | } |
| 227 | } |
| 228 | |
Mark Brown | a7440ea | 2013-01-03 14:27:15 +0000 | [diff] [blame] | 229 | /* |
| 230 | * Read in the statuses, using a single bulk read if possible |
| 231 | * in order to reduce the I/O overheads. |
| 232 | */ |
Markus Pargmann | 67921a1 | 2015-08-21 10:26:42 +0200 | [diff] [blame] | 233 | if (!map->use_single_read && map->reg_stride == 1 && |
Mark Brown | a7440ea | 2013-01-03 14:27:15 +0000 | [diff] [blame] | 234 | data->irq_reg_stride == 1) { |
| 235 | u8 *buf8 = data->status_reg_buf; |
| 236 | u16 *buf16 = data->status_reg_buf; |
| 237 | u32 *buf32 = data->status_reg_buf; |
Graeme Gregory | 022f926a | 2012-05-14 22:40:43 +0900 | [diff] [blame] | 238 | |
Mark Brown | a7440ea | 2013-01-03 14:27:15 +0000 | [diff] [blame] | 239 | BUG_ON(!data->status_reg_buf); |
| 240 | |
| 241 | ret = regmap_bulk_read(map, chip->status_base, |
| 242 | data->status_reg_buf, |
| 243 | chip->num_regs); |
Graeme Gregory | 022f926a | 2012-05-14 22:40:43 +0900 | [diff] [blame] | 244 | if (ret != 0) { |
| 245 | dev_err(map->dev, "Failed to read IRQ status: %d\n", |
Mark Brown | a7440ea | 2013-01-03 14:27:15 +0000 | [diff] [blame] | 246 | ret); |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 247 | return IRQ_NONE; |
| 248 | } |
Mark Brown | a7440ea | 2013-01-03 14:27:15 +0000 | [diff] [blame] | 249 | |
| 250 | for (i = 0; i < data->chip->num_regs; i++) { |
| 251 | switch (map->format.val_bytes) { |
| 252 | case 1: |
| 253 | data->status_buf[i] = buf8[i]; |
| 254 | break; |
| 255 | case 2: |
| 256 | data->status_buf[i] = buf16[i]; |
| 257 | break; |
| 258 | case 4: |
| 259 | data->status_buf[i] = buf32[i]; |
| 260 | break; |
| 261 | default: |
| 262 | BUG(); |
| 263 | return IRQ_NONE; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | } else { |
| 268 | for (i = 0; i < data->chip->num_regs; i++) { |
| 269 | ret = regmap_read(map, chip->status_base + |
| 270 | (i * map->reg_stride |
| 271 | * data->irq_reg_stride), |
| 272 | &data->status_buf[i]); |
| 273 | |
| 274 | if (ret != 0) { |
| 275 | dev_err(map->dev, |
| 276 | "Failed to read IRQ status: %d\n", |
| 277 | ret); |
| 278 | if (chip->runtime_pm) |
| 279 | pm_runtime_put(map->dev); |
| 280 | return IRQ_NONE; |
| 281 | } |
| 282 | } |
Mark Brown | bbae92c | 2013-01-03 13:58:33 +0000 | [diff] [blame] | 283 | } |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 284 | |
Mark Brown | bbae92c | 2013-01-03 13:58:33 +0000 | [diff] [blame] | 285 | /* |
| 286 | * Ignore masked IRQs and ack if we need to; we ack early so |
| 287 | * there is no race between handling and acknowleding the |
| 288 | * interrupt. We assume that typically few of the interrupts |
| 289 | * will fire simultaneously so don't worry about overhead from |
| 290 | * doing a write per register. |
| 291 | */ |
| 292 | for (i = 0; i < data->chip->num_regs; i++) { |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 293 | data->status_buf[i] &= ~data->mask_buf[i]; |
| 294 | |
Alexander Shiyan | d323343 | 2013-12-15 13:36:51 +0400 | [diff] [blame] | 295 | if (data->status_buf[i] && (chip->ack_base || chip->use_ack)) { |
Stephen Warren | 1603262 | 2012-07-27 13:01:54 -0600 | [diff] [blame] | 296 | reg = chip->ack_base + |
| 297 | (i * map->reg_stride * data->irq_reg_stride); |
| 298 | ret = regmap_write(map, reg, data->status_buf[i]); |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 299 | if (ret != 0) |
| 300 | dev_err(map->dev, "Failed to ack 0x%x: %d\n", |
Stephen Warren | 1603262 | 2012-07-27 13:01:54 -0600 | [diff] [blame] | 301 | reg, ret); |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 302 | } |
| 303 | } |
| 304 | |
| 305 | for (i = 0; i < chip->num_irqs; i++) { |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 306 | if (data->status_buf[chip->irqs[i].reg_offset / |
| 307 | map->reg_stride] & chip->irqs[i].mask) { |
Mark Brown | 4af8be6 | 2012-05-13 10:59:56 +0100 | [diff] [blame] | 308 | handle_nested_irq(irq_find_mapping(data->domain, i)); |
Mark Brown | d23511f | 2011-11-28 18:50:39 +0000 | [diff] [blame] | 309 | handled = true; |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | |
Mark Brown | 0c00c50 | 2012-07-24 15:41:19 +0100 | [diff] [blame] | 313 | if (chip->runtime_pm) |
| 314 | pm_runtime_put(map->dev); |
| 315 | |
Mark Brown | d23511f | 2011-11-28 18:50:39 +0000 | [diff] [blame] | 316 | if (handled) |
| 317 | return IRQ_HANDLED; |
| 318 | else |
| 319 | return IRQ_NONE; |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 320 | } |
| 321 | |
Mark Brown | 4af8be6 | 2012-05-13 10:59:56 +0100 | [diff] [blame] | 322 | static int regmap_irq_map(struct irq_domain *h, unsigned int virq, |
| 323 | irq_hw_number_t hw) |
| 324 | { |
| 325 | struct regmap_irq_chip_data *data = h->host_data; |
| 326 | |
| 327 | irq_set_chip_data(virq, data); |
Yunfan Zhang | 8138073 | 2012-09-08 03:53:25 -0700 | [diff] [blame] | 328 | irq_set_chip(virq, &data->irq_chip); |
Mark Brown | 4af8be6 | 2012-05-13 10:59:56 +0100 | [diff] [blame] | 329 | irq_set_nested_thread(virq, 1); |
Mark Brown | 4af8be6 | 2012-05-13 10:59:56 +0100 | [diff] [blame] | 330 | irq_set_noprobe(virq); |
Mark Brown | 4af8be6 | 2012-05-13 10:59:56 +0100 | [diff] [blame] | 331 | |
| 332 | return 0; |
| 333 | } |
| 334 | |
Krzysztof Kozlowski | 77f5f3e | 2015-04-27 21:52:10 +0900 | [diff] [blame] | 335 | static const struct irq_domain_ops regmap_domain_ops = { |
Mark Brown | 4af8be6 | 2012-05-13 10:59:56 +0100 | [diff] [blame] | 336 | .map = regmap_irq_map, |
| 337 | .xlate = irq_domain_xlate_twocell, |
| 338 | }; |
| 339 | |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 340 | /** |
| 341 | * regmap_add_irq_chip(): Use standard regmap IRQ controller handling |
| 342 | * |
| 343 | * map: The regmap for the device. |
| 344 | * irq: The IRQ the device uses to signal interrupts |
| 345 | * irq_flags: The IRQF_ flags to use for the primary interrupt. |
| 346 | * chip: Configuration for the interrupt controller. |
| 347 | * data: Runtime data structure for the controller, allocated on success |
| 348 | * |
| 349 | * Returns 0 on success or an errno on failure. |
| 350 | * |
| 351 | * In order for this to be efficient the chip really should use a |
| 352 | * register cache. The chip driver is responsible for restoring the |
| 353 | * register values used by the IRQ controller over suspend and resume. |
| 354 | */ |
| 355 | int regmap_add_irq_chip(struct regmap *map, int irq, int irq_flags, |
Mark Brown | b026ddb | 2012-05-31 21:01:46 +0100 | [diff] [blame] | 356 | int irq_base, const struct regmap_irq_chip *chip, |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 357 | struct regmap_irq_chip_data **data) |
| 358 | { |
| 359 | struct regmap_irq_chip_data *d; |
Mark Brown | 4af8be6 | 2012-05-13 10:59:56 +0100 | [diff] [blame] | 360 | int i; |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 361 | int ret = -ENOMEM; |
Stephen Warren | 1603262 | 2012-07-27 13:01:54 -0600 | [diff] [blame] | 362 | u32 reg; |
Guo Zeng | 7b7d196 | 2015-09-17 05:23:20 +0000 | [diff] [blame] | 363 | u32 unmask_offset; |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 364 | |
Xiubo Li | e128920 | 2014-05-19 15:13:45 +0800 | [diff] [blame] | 365 | if (chip->num_regs <= 0) |
| 366 | return -EINVAL; |
| 367 | |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 368 | for (i = 0; i < chip->num_irqs; i++) { |
| 369 | if (chip->irqs[i].reg_offset % map->reg_stride) |
| 370 | return -EINVAL; |
| 371 | if (chip->irqs[i].reg_offset / map->reg_stride >= |
| 372 | chip->num_regs) |
| 373 | return -EINVAL; |
| 374 | } |
| 375 | |
Mark Brown | 4af8be6 | 2012-05-13 10:59:56 +0100 | [diff] [blame] | 376 | if (irq_base) { |
| 377 | irq_base = irq_alloc_descs(irq_base, 0, chip->num_irqs, 0); |
| 378 | if (irq_base < 0) { |
| 379 | dev_warn(map->dev, "Failed to allocate IRQs: %d\n", |
| 380 | irq_base); |
| 381 | return irq_base; |
| 382 | } |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | d = kzalloc(sizeof(*d), GFP_KERNEL); |
| 386 | if (!d) |
| 387 | return -ENOMEM; |
| 388 | |
| 389 | d->status_buf = kzalloc(sizeof(unsigned int) * chip->num_regs, |
| 390 | GFP_KERNEL); |
| 391 | if (!d->status_buf) |
| 392 | goto err_alloc; |
| 393 | |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 394 | d->mask_buf = kzalloc(sizeof(unsigned int) * chip->num_regs, |
| 395 | GFP_KERNEL); |
| 396 | if (!d->mask_buf) |
| 397 | goto err_alloc; |
| 398 | |
| 399 | d->mask_buf_def = kzalloc(sizeof(unsigned int) * chip->num_regs, |
| 400 | GFP_KERNEL); |
| 401 | if (!d->mask_buf_def) |
| 402 | goto err_alloc; |
| 403 | |
Mark Brown | a43fd50 | 2012-06-05 14:34:03 +0100 | [diff] [blame] | 404 | if (chip->wake_base) { |
| 405 | d->wake_buf = kzalloc(sizeof(unsigned int) * chip->num_regs, |
| 406 | GFP_KERNEL); |
| 407 | if (!d->wake_buf) |
| 408 | goto err_alloc; |
| 409 | } |
| 410 | |
Stephen Warren | 7ac140e | 2012-08-01 11:40:47 -0600 | [diff] [blame] | 411 | d->irq_chip = regmap_irq_chip; |
Stephen Warren | ca14275 | 2012-08-01 11:40:48 -0600 | [diff] [blame] | 412 | d->irq_chip.name = chip->name; |
Mark Brown | a43fd50 | 2012-06-05 14:34:03 +0100 | [diff] [blame] | 413 | d->irq = irq; |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 414 | d->map = map; |
| 415 | d->chip = chip; |
| 416 | d->irq_base = irq_base; |
Graeme Gregory | 022f926a | 2012-05-14 22:40:43 +0900 | [diff] [blame] | 417 | |
| 418 | if (chip->irq_reg_stride) |
| 419 | d->irq_reg_stride = chip->irq_reg_stride; |
| 420 | else |
| 421 | d->irq_reg_stride = 1; |
| 422 | |
Markus Pargmann | 67921a1 | 2015-08-21 10:26:42 +0200 | [diff] [blame] | 423 | if (!map->use_single_read && map->reg_stride == 1 && |
Mark Brown | a7440ea | 2013-01-03 14:27:15 +0000 | [diff] [blame] | 424 | d->irq_reg_stride == 1) { |
| 425 | d->status_reg_buf = kmalloc(map->format.val_bytes * |
| 426 | chip->num_regs, GFP_KERNEL); |
| 427 | if (!d->status_reg_buf) |
| 428 | goto err_alloc; |
| 429 | } |
| 430 | |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 431 | mutex_init(&d->lock); |
| 432 | |
| 433 | for (i = 0; i < chip->num_irqs; i++) |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 434 | d->mask_buf_def[chip->irqs[i].reg_offset / map->reg_stride] |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 435 | |= chip->irqs[i].mask; |
| 436 | |
| 437 | /* Mask all the interrupts by default */ |
| 438 | for (i = 0; i < chip->num_regs; i++) { |
| 439 | d->mask_buf[i] = d->mask_buf_def[i]; |
Stephen Warren | 1603262 | 2012-07-27 13:01:54 -0600 | [diff] [blame] | 440 | reg = chip->mask_base + |
| 441 | (i * map->reg_stride * d->irq_reg_stride); |
Xiaofan Tian | 36ac914 | 2012-08-30 17:03:35 +0800 | [diff] [blame] | 442 | if (chip->mask_invert) |
| 443 | ret = regmap_update_bits(map, reg, |
| 444 | d->mask_buf[i], ~d->mask_buf[i]); |
Guo Zeng | 7b7d196 | 2015-09-17 05:23:20 +0000 | [diff] [blame] | 445 | else if (d->chip->unmask_base) { |
| 446 | unmask_offset = d->chip->unmask_base - |
| 447 | d->chip->mask_base; |
| 448 | ret = regmap_update_bits(d->map, |
| 449 | reg + unmask_offset, |
| 450 | d->mask_buf[i], |
| 451 | d->mask_buf[i]); |
| 452 | } else |
Xiaofan Tian | 36ac914 | 2012-08-30 17:03:35 +0800 | [diff] [blame] | 453 | ret = regmap_update_bits(map, reg, |
Mark Brown | 0eb46ad | 2012-08-01 20:29:14 +0100 | [diff] [blame] | 454 | d->mask_buf[i], d->mask_buf[i]); |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 455 | if (ret != 0) { |
| 456 | dev_err(map->dev, "Failed to set masks in 0x%x: %d\n", |
Stephen Warren | 1603262 | 2012-07-27 13:01:54 -0600 | [diff] [blame] | 457 | reg, ret); |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 458 | goto err_alloc; |
| 459 | } |
Philipp Zabel | 2753e6f | 2013-07-22 17:15:52 +0200 | [diff] [blame] | 460 | |
| 461 | if (!chip->init_ack_masked) |
| 462 | continue; |
| 463 | |
| 464 | /* Ack masked but set interrupts */ |
| 465 | reg = chip->status_base + |
| 466 | (i * map->reg_stride * d->irq_reg_stride); |
| 467 | ret = regmap_read(map, reg, &d->status_buf[i]); |
| 468 | if (ret != 0) { |
| 469 | dev_err(map->dev, "Failed to read IRQ status: %d\n", |
| 470 | ret); |
| 471 | goto err_alloc; |
| 472 | } |
| 473 | |
Alexander Shiyan | d323343 | 2013-12-15 13:36:51 +0400 | [diff] [blame] | 474 | if (d->status_buf[i] && (chip->ack_base || chip->use_ack)) { |
Philipp Zabel | 2753e6f | 2013-07-22 17:15:52 +0200 | [diff] [blame] | 475 | reg = chip->ack_base + |
| 476 | (i * map->reg_stride * d->irq_reg_stride); |
Guo Zeng | a650fdd | 2015-09-17 05:23:21 +0000 | [diff] [blame^] | 477 | if (chip->ack_invert) |
| 478 | ret = regmap_write(map, reg, |
| 479 | ~(d->status_buf[i] & d->mask_buf[i])); |
| 480 | else |
| 481 | ret = regmap_write(map, reg, |
Philipp Zabel | 2753e6f | 2013-07-22 17:15:52 +0200 | [diff] [blame] | 482 | d->status_buf[i] & d->mask_buf[i]); |
| 483 | if (ret != 0) { |
| 484 | dev_err(map->dev, "Failed to ack 0x%x: %d\n", |
| 485 | reg, ret); |
| 486 | goto err_alloc; |
| 487 | } |
| 488 | } |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 489 | } |
| 490 | |
Stephen Warren | 40052ca | 2012-08-01 13:57:24 -0600 | [diff] [blame] | 491 | /* Wake is disabled by default */ |
| 492 | if (d->wake_buf) { |
| 493 | for (i = 0; i < chip->num_regs; i++) { |
| 494 | d->wake_buf[i] = d->mask_buf_def[i]; |
| 495 | reg = chip->wake_base + |
| 496 | (i * map->reg_stride * d->irq_reg_stride); |
Mark Brown | 9442490 | 2013-01-04 16:35:07 +0000 | [diff] [blame] | 497 | |
| 498 | if (chip->wake_invert) |
| 499 | ret = regmap_update_bits(map, reg, |
| 500 | d->mask_buf_def[i], |
| 501 | 0); |
| 502 | else |
| 503 | ret = regmap_update_bits(map, reg, |
| 504 | d->mask_buf_def[i], |
| 505 | d->wake_buf[i]); |
Stephen Warren | 40052ca | 2012-08-01 13:57:24 -0600 | [diff] [blame] | 506 | if (ret != 0) { |
| 507 | dev_err(map->dev, "Failed to set masks in 0x%x: %d\n", |
| 508 | reg, ret); |
| 509 | goto err_alloc; |
| 510 | } |
| 511 | } |
| 512 | } |
| 513 | |
Mark Brown | 4af8be6 | 2012-05-13 10:59:56 +0100 | [diff] [blame] | 514 | if (irq_base) |
| 515 | d->domain = irq_domain_add_legacy(map->dev->of_node, |
| 516 | chip->num_irqs, irq_base, 0, |
| 517 | ®map_domain_ops, d); |
| 518 | else |
| 519 | d->domain = irq_domain_add_linear(map->dev->of_node, |
| 520 | chip->num_irqs, |
| 521 | ®map_domain_ops, d); |
| 522 | if (!d->domain) { |
| 523 | dev_err(map->dev, "Failed to create IRQ domain\n"); |
| 524 | ret = -ENOMEM; |
| 525 | goto err_alloc; |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 526 | } |
| 527 | |
Valentin Rothberg | 09cadf6 | 2015-02-11 16:37:57 +0100 | [diff] [blame] | 528 | ret = request_threaded_irq(irq, NULL, regmap_irq_thread, |
| 529 | irq_flags | IRQF_ONESHOT, |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 530 | chip->name, d); |
| 531 | if (ret != 0) { |
Mark Brown | eed456f | 2013-03-19 10:45:04 +0000 | [diff] [blame] | 532 | dev_err(map->dev, "Failed to request IRQ %d for %s: %d\n", |
| 533 | irq, chip->name, ret); |
Mark Brown | 4af8be6 | 2012-05-13 10:59:56 +0100 | [diff] [blame] | 534 | goto err_domain; |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 535 | } |
| 536 | |
Krzysztof Kozlowski | 72a6a5d | 2014-03-13 09:06:01 +0100 | [diff] [blame] | 537 | *data = d; |
| 538 | |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 539 | return 0; |
| 540 | |
Mark Brown | 4af8be6 | 2012-05-13 10:59:56 +0100 | [diff] [blame] | 541 | err_domain: |
| 542 | /* Should really dispose of the domain but... */ |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 543 | err_alloc: |
Mark Brown | a43fd50 | 2012-06-05 14:34:03 +0100 | [diff] [blame] | 544 | kfree(d->wake_buf); |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 545 | kfree(d->mask_buf_def); |
| 546 | kfree(d->mask_buf); |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 547 | kfree(d->status_buf); |
Mark Brown | a7440ea | 2013-01-03 14:27:15 +0000 | [diff] [blame] | 548 | kfree(d->status_reg_buf); |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 549 | kfree(d); |
| 550 | return ret; |
| 551 | } |
| 552 | EXPORT_SYMBOL_GPL(regmap_add_irq_chip); |
| 553 | |
| 554 | /** |
| 555 | * regmap_del_irq_chip(): Stop interrupt handling for a regmap IRQ chip |
| 556 | * |
| 557 | * @irq: Primary IRQ for the device |
| 558 | * @d: regmap_irq_chip_data allocated by regmap_add_irq_chip() |
| 559 | */ |
| 560 | void regmap_del_irq_chip(int irq, struct regmap_irq_chip_data *d) |
| 561 | { |
| 562 | if (!d) |
| 563 | return; |
| 564 | |
| 565 | free_irq(irq, d); |
Mark Brown | b5ab3e5 | 2014-01-22 20:25:48 +0000 | [diff] [blame] | 566 | irq_domain_remove(d->domain); |
Mark Brown | a43fd50 | 2012-06-05 14:34:03 +0100 | [diff] [blame] | 567 | kfree(d->wake_buf); |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 568 | kfree(d->mask_buf_def); |
| 569 | kfree(d->mask_buf); |
Mark Brown | a7440ea | 2013-01-03 14:27:15 +0000 | [diff] [blame] | 570 | kfree(d->status_reg_buf); |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 571 | kfree(d->status_buf); |
| 572 | kfree(d); |
| 573 | } |
| 574 | EXPORT_SYMBOL_GPL(regmap_del_irq_chip); |
Mark Brown | 209a600 | 2011-12-05 16:10:15 +0000 | [diff] [blame] | 575 | |
| 576 | /** |
| 577 | * regmap_irq_chip_get_base(): Retrieve interrupt base for a regmap IRQ chip |
| 578 | * |
| 579 | * Useful for drivers to request their own IRQs. |
| 580 | * |
| 581 | * @data: regmap_irq controller to operate on. |
| 582 | */ |
| 583 | int regmap_irq_chip_get_base(struct regmap_irq_chip_data *data) |
| 584 | { |
Mark Brown | 4af8be6 | 2012-05-13 10:59:56 +0100 | [diff] [blame] | 585 | WARN_ON(!data->irq_base); |
Mark Brown | 209a600 | 2011-12-05 16:10:15 +0000 | [diff] [blame] | 586 | return data->irq_base; |
| 587 | } |
| 588 | EXPORT_SYMBOL_GPL(regmap_irq_chip_get_base); |
Mark Brown | 4af8be6 | 2012-05-13 10:59:56 +0100 | [diff] [blame] | 589 | |
| 590 | /** |
| 591 | * regmap_irq_get_virq(): Map an interrupt on a chip to a virtual IRQ |
| 592 | * |
| 593 | * Useful for drivers to request their own IRQs. |
| 594 | * |
| 595 | * @data: regmap_irq controller to operate on. |
| 596 | * @irq: index of the interrupt requested in the chip IRQs |
| 597 | */ |
| 598 | int regmap_irq_get_virq(struct regmap_irq_chip_data *data, int irq) |
| 599 | { |
Mark Brown | bfd6185d | 2012-06-05 14:29:36 +0100 | [diff] [blame] | 600 | /* Handle holes in the IRQ list */ |
| 601 | if (!data->chip->irqs[irq].mask) |
| 602 | return -EINVAL; |
| 603 | |
Mark Brown | 4af8be6 | 2012-05-13 10:59:56 +0100 | [diff] [blame] | 604 | return irq_create_mapping(data->domain, irq); |
| 605 | } |
| 606 | EXPORT_SYMBOL_GPL(regmap_irq_get_virq); |
Mark Brown | 90f790d | 2012-08-20 21:45:05 +0100 | [diff] [blame] | 607 | |
| 608 | /** |
| 609 | * regmap_irq_get_domain(): Retrieve the irq_domain for the chip |
| 610 | * |
| 611 | * Useful for drivers to request their own IRQs and for integration |
| 612 | * with subsystems. For ease of integration NULL is accepted as a |
| 613 | * domain, allowing devices to just call this even if no domain is |
| 614 | * allocated. |
| 615 | * |
| 616 | * @data: regmap_irq controller to operate on. |
| 617 | */ |
| 618 | struct irq_domain *regmap_irq_get_domain(struct regmap_irq_chip_data *data) |
| 619 | { |
| 620 | if (data) |
| 621 | return data->domain; |
| 622 | else |
| 623 | return NULL; |
| 624 | } |
| 625 | EXPORT_SYMBOL_GPL(regmap_irq_get_domain); |