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