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