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; |
Laxman Dewangan | 7a78479f | 2015-12-22 18:25:26 +0530 | [diff] [blame] | 42 | unsigned int *type_buf; |
| 43 | unsigned int *type_buf_def; |
Graeme Gregory | 022f926a | 2012-05-14 22:40:43 +0900 | [diff] [blame] | 44 | |
| 45 | unsigned int irq_reg_stride; |
Laxman Dewangan | 7a78479f | 2015-12-22 18:25:26 +0530 | [diff] [blame] | 46 | unsigned int type_reg_stride; |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | static inline const |
| 50 | struct regmap_irq *irq_to_regmap_irq(struct regmap_irq_chip_data *data, |
| 51 | int irq) |
| 52 | { |
Mark Brown | 4af8be6 | 2012-05-13 10:59:56 +0100 | [diff] [blame] | 53 | return &data->chip->irqs[irq]; |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | static void regmap_irq_lock(struct irq_data *data) |
| 57 | { |
| 58 | struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data); |
| 59 | |
| 60 | mutex_lock(&d->lock); |
| 61 | } |
| 62 | |
Michael Grzeschik | a71411d | 2017-06-23 14:35:09 +0200 | [diff] [blame] | 63 | static int regmap_irq_update_bits(struct regmap_irq_chip_data *d, |
| 64 | unsigned int reg, unsigned int mask, |
| 65 | unsigned int val) |
| 66 | { |
| 67 | if (d->chip->mask_writeonly) |
| 68 | return regmap_write_bits(d->map, reg, mask, val); |
| 69 | else |
| 70 | return regmap_update_bits(d->map, reg, mask, val); |
| 71 | } |
| 72 | |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 73 | static void regmap_irq_sync_unlock(struct irq_data *data) |
| 74 | { |
| 75 | 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] | 76 | struct regmap *map = d->map; |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 77 | int i, ret; |
Stephen Warren | 1603262 | 2012-07-27 13:01:54 -0600 | [diff] [blame] | 78 | u32 reg; |
Guo Zeng | 7b7d196 | 2015-09-17 05:23:20 +0000 | [diff] [blame] | 79 | u32 unmask_offset; |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 80 | |
Mark Brown | 0c00c50 | 2012-07-24 15:41:19 +0100 | [diff] [blame] | 81 | if (d->chip->runtime_pm) { |
| 82 | ret = pm_runtime_get_sync(map->dev); |
| 83 | if (ret < 0) |
| 84 | dev_err(map->dev, "IRQ sync failed to resume: %d\n", |
| 85 | ret); |
| 86 | } |
| 87 | |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 88 | /* |
| 89 | * If there's been a change in the mask write it back to the |
| 90 | * hardware. We rely on the use of the regmap core cache to |
| 91 | * suppress pointless writes. |
| 92 | */ |
| 93 | for (i = 0; i < d->chip->num_regs; i++) { |
Stephen Warren | 1603262 | 2012-07-27 13:01:54 -0600 | [diff] [blame] | 94 | reg = d->chip->mask_base + |
| 95 | (i * map->reg_stride * d->irq_reg_stride); |
Guo Zeng | 7b7d196 | 2015-09-17 05:23:20 +0000 | [diff] [blame] | 96 | if (d->chip->mask_invert) { |
Michael Grzeschik | a71411d | 2017-06-23 14:35:09 +0200 | [diff] [blame] | 97 | ret = regmap_irq_update_bits(d, reg, |
Xiaofan Tian | 36ac914 | 2012-08-30 17:03:35 +0800 | [diff] [blame] | 98 | d->mask_buf_def[i], ~d->mask_buf[i]); |
Guo Zeng | 7b7d196 | 2015-09-17 05:23:20 +0000 | [diff] [blame] | 99 | } else if (d->chip->unmask_base) { |
| 100 | /* set mask with mask_base register */ |
Michael Grzeschik | a71411d | 2017-06-23 14:35:09 +0200 | [diff] [blame] | 101 | ret = regmap_irq_update_bits(d, reg, |
Guo Zeng | 7b7d196 | 2015-09-17 05:23:20 +0000 | [diff] [blame] | 102 | d->mask_buf_def[i], ~d->mask_buf[i]); |
| 103 | if (ret < 0) |
| 104 | dev_err(d->map->dev, |
| 105 | "Failed to sync unmasks in %x\n", |
| 106 | reg); |
| 107 | unmask_offset = d->chip->unmask_base - |
| 108 | d->chip->mask_base; |
| 109 | /* clear mask with unmask_base register */ |
Michael Grzeschik | a71411d | 2017-06-23 14:35:09 +0200 | [diff] [blame] | 110 | ret = regmap_irq_update_bits(d, |
Guo Zeng | 7b7d196 | 2015-09-17 05:23:20 +0000 | [diff] [blame] | 111 | reg + unmask_offset, |
| 112 | d->mask_buf_def[i], |
| 113 | d->mask_buf[i]); |
| 114 | } else { |
Michael Grzeschik | a71411d | 2017-06-23 14:35:09 +0200 | [diff] [blame] | 115 | ret = regmap_irq_update_bits(d, reg, |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 116 | d->mask_buf_def[i], d->mask_buf[i]); |
Guo Zeng | 7b7d196 | 2015-09-17 05:23:20 +0000 | [diff] [blame] | 117 | } |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 118 | if (ret != 0) |
| 119 | dev_err(d->map->dev, "Failed to sync masks in %x\n", |
Stephen Warren | 1603262 | 2012-07-27 13:01:54 -0600 | [diff] [blame] | 120 | reg); |
Mark Brown | 33be493 | 2013-01-04 16:32:54 +0000 | [diff] [blame] | 121 | |
| 122 | reg = d->chip->wake_base + |
| 123 | (i * map->reg_stride * d->irq_reg_stride); |
| 124 | if (d->wake_buf) { |
Mark Brown | 9442490 | 2013-01-04 16:35:07 +0000 | [diff] [blame] | 125 | if (d->chip->wake_invert) |
Michael Grzeschik | a71411d | 2017-06-23 14:35:09 +0200 | [diff] [blame] | 126 | ret = regmap_irq_update_bits(d, reg, |
Mark Brown | 9442490 | 2013-01-04 16:35:07 +0000 | [diff] [blame] | 127 | d->mask_buf_def[i], |
| 128 | ~d->wake_buf[i]); |
| 129 | else |
Michael Grzeschik | a71411d | 2017-06-23 14:35:09 +0200 | [diff] [blame] | 130 | ret = regmap_irq_update_bits(d, reg, |
Mark Brown | 9442490 | 2013-01-04 16:35:07 +0000 | [diff] [blame] | 131 | d->mask_buf_def[i], |
| 132 | d->wake_buf[i]); |
Mark Brown | 33be493 | 2013-01-04 16:32:54 +0000 | [diff] [blame] | 133 | if (ret != 0) |
| 134 | dev_err(d->map->dev, |
| 135 | "Failed to sync wakes in %x: %d\n", |
| 136 | reg, ret); |
| 137 | } |
Yi Zhang | 4bd7145 | 2013-10-22 18:44:32 +0800 | [diff] [blame] | 138 | |
| 139 | if (!d->chip->init_ack_masked) |
| 140 | continue; |
| 141 | /* |
dashsriram | 7043f5f | 2015-05-27 00:55:13 +0530 | [diff] [blame] | 142 | * Ack all the masked interrupts unconditionally, |
Yi Zhang | 4bd7145 | 2013-10-22 18:44:32 +0800 | [diff] [blame] | 143 | * OR if there is masked interrupt which hasn't been Acked, |
| 144 | * it'll be ignored in irq handler, then may introduce irq storm |
| 145 | */ |
Alexander Shiyan | d323343 | 2013-12-15 13:36:51 +0400 | [diff] [blame] | 146 | 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] | 147 | reg = d->chip->ack_base + |
| 148 | (i * map->reg_stride * d->irq_reg_stride); |
Guo Zeng | a650fdd | 2015-09-17 05:23:21 +0000 | [diff] [blame] | 149 | /* some chips ack by write 0 */ |
| 150 | if (d->chip->ack_invert) |
| 151 | ret = regmap_write(map, reg, ~d->mask_buf[i]); |
| 152 | else |
| 153 | ret = regmap_write(map, reg, d->mask_buf[i]); |
Yi Zhang | 4bd7145 | 2013-10-22 18:44:32 +0800 | [diff] [blame] | 154 | if (ret != 0) |
| 155 | dev_err(d->map->dev, "Failed to ack 0x%x: %d\n", |
| 156 | reg, ret); |
| 157 | } |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 158 | } |
| 159 | |
Laxman Dewangan | 7a78479f | 2015-12-22 18:25:26 +0530 | [diff] [blame] | 160 | for (i = 0; i < d->chip->num_type_reg; i++) { |
| 161 | if (!d->type_buf_def[i]) |
| 162 | continue; |
| 163 | reg = d->chip->type_base + |
| 164 | (i * map->reg_stride * d->type_reg_stride); |
| 165 | if (d->chip->type_invert) |
Michael Grzeschik | a71411d | 2017-06-23 14:35:09 +0200 | [diff] [blame] | 166 | ret = regmap_irq_update_bits(d, reg, |
Laxman Dewangan | 7a78479f | 2015-12-22 18:25:26 +0530 | [diff] [blame] | 167 | d->type_buf_def[i], ~d->type_buf[i]); |
| 168 | else |
Michael Grzeschik | a71411d | 2017-06-23 14:35:09 +0200 | [diff] [blame] | 169 | ret = regmap_irq_update_bits(d, reg, |
Laxman Dewangan | 7a78479f | 2015-12-22 18:25:26 +0530 | [diff] [blame] | 170 | d->type_buf_def[i], d->type_buf[i]); |
| 171 | if (ret != 0) |
| 172 | dev_err(d->map->dev, "Failed to sync type in %x\n", |
| 173 | reg); |
| 174 | } |
| 175 | |
Mark Brown | 0c00c50 | 2012-07-24 15:41:19 +0100 | [diff] [blame] | 176 | if (d->chip->runtime_pm) |
| 177 | pm_runtime_put(map->dev); |
| 178 | |
Mark Brown | a43fd50 | 2012-06-05 14:34:03 +0100 | [diff] [blame] | 179 | /* If we've changed our wakeup count propagate it to the parent */ |
| 180 | if (d->wake_count < 0) |
| 181 | for (i = d->wake_count; i < 0; i++) |
| 182 | irq_set_irq_wake(d->irq, 0); |
| 183 | else if (d->wake_count > 0) |
| 184 | for (i = 0; i < d->wake_count; i++) |
| 185 | irq_set_irq_wake(d->irq, 1); |
| 186 | |
| 187 | d->wake_count = 0; |
| 188 | |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 189 | mutex_unlock(&d->lock); |
| 190 | } |
| 191 | |
| 192 | static void regmap_irq_enable(struct irq_data *data) |
| 193 | { |
| 194 | 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] | 195 | struct regmap *map = d->map; |
Mark Brown | 4af8be6 | 2012-05-13 10:59:56 +0100 | [diff] [blame] | 196 | 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] | 197 | |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 198 | 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] | 199 | } |
| 200 | |
| 201 | static void regmap_irq_disable(struct irq_data *data) |
| 202 | { |
| 203 | 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] | 204 | struct regmap *map = d->map; |
Mark Brown | 4af8be6 | 2012-05-13 10:59:56 +0100 | [diff] [blame] | 205 | 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] | 206 | |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 207 | 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] | 208 | } |
| 209 | |
Laxman Dewangan | 7a78479f | 2015-12-22 18:25:26 +0530 | [diff] [blame] | 210 | static int regmap_irq_set_type(struct irq_data *data, unsigned int type) |
| 211 | { |
| 212 | struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data); |
| 213 | struct regmap *map = d->map; |
| 214 | const struct regmap_irq *irq_data = irq_to_regmap_irq(d, data->hwirq); |
| 215 | int reg = irq_data->type_reg_offset / map->reg_stride; |
| 216 | |
| 217 | if (!(irq_data->type_rising_mask | irq_data->type_falling_mask)) |
| 218 | return 0; |
| 219 | |
| 220 | d->type_buf[reg] &= ~(irq_data->type_falling_mask | |
| 221 | irq_data->type_rising_mask); |
| 222 | switch (type) { |
| 223 | case IRQ_TYPE_EDGE_FALLING: |
| 224 | d->type_buf[reg] |= irq_data->type_falling_mask; |
| 225 | break; |
| 226 | |
| 227 | case IRQ_TYPE_EDGE_RISING: |
| 228 | d->type_buf[reg] |= irq_data->type_rising_mask; |
| 229 | break; |
| 230 | |
| 231 | case IRQ_TYPE_EDGE_BOTH: |
| 232 | d->type_buf[reg] |= (irq_data->type_falling_mask | |
| 233 | irq_data->type_rising_mask); |
| 234 | break; |
| 235 | |
| 236 | default: |
| 237 | return -EINVAL; |
| 238 | } |
| 239 | return 0; |
| 240 | } |
| 241 | |
Mark Brown | a43fd50 | 2012-06-05 14:34:03 +0100 | [diff] [blame] | 242 | static int regmap_irq_set_wake(struct irq_data *data, unsigned int on) |
| 243 | { |
| 244 | struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data); |
| 245 | struct regmap *map = d->map; |
| 246 | const struct regmap_irq *irq_data = irq_to_regmap_irq(d, data->hwirq); |
| 247 | |
Mark Brown | a43fd50 | 2012-06-05 14:34:03 +0100 | [diff] [blame] | 248 | if (on) { |
Laxman Dewangan | 55ac85e | 2012-12-19 19:42:28 +0530 | [diff] [blame] | 249 | if (d->wake_buf) |
| 250 | d->wake_buf[irq_data->reg_offset / map->reg_stride] |
| 251 | &= ~irq_data->mask; |
Mark Brown | a43fd50 | 2012-06-05 14:34:03 +0100 | [diff] [blame] | 252 | d->wake_count++; |
| 253 | } else { |
Laxman Dewangan | 55ac85e | 2012-12-19 19:42:28 +0530 | [diff] [blame] | 254 | if (d->wake_buf) |
| 255 | d->wake_buf[irq_data->reg_offset / map->reg_stride] |
| 256 | |= irq_data->mask; |
Mark Brown | a43fd50 | 2012-06-05 14:34:03 +0100 | [diff] [blame] | 257 | d->wake_count--; |
| 258 | } |
| 259 | |
| 260 | return 0; |
| 261 | } |
| 262 | |
Stephen Warren | 7ac140e | 2012-08-01 11:40:47 -0600 | [diff] [blame] | 263 | static const struct irq_chip regmap_irq_chip = { |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 264 | .irq_bus_lock = regmap_irq_lock, |
| 265 | .irq_bus_sync_unlock = regmap_irq_sync_unlock, |
| 266 | .irq_disable = regmap_irq_disable, |
| 267 | .irq_enable = regmap_irq_enable, |
Laxman Dewangan | 7a78479f | 2015-12-22 18:25:26 +0530 | [diff] [blame] | 268 | .irq_set_type = regmap_irq_set_type, |
Mark Brown | a43fd50 | 2012-06-05 14:34:03 +0100 | [diff] [blame] | 269 | .irq_set_wake = regmap_irq_set_wake, |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 270 | }; |
| 271 | |
| 272 | static irqreturn_t regmap_irq_thread(int irq, void *d) |
| 273 | { |
| 274 | struct regmap_irq_chip_data *data = d; |
Mark Brown | b026ddb | 2012-05-31 21:01:46 +0100 | [diff] [blame] | 275 | const struct regmap_irq_chip *chip = data->chip; |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 276 | struct regmap *map = data->map; |
| 277 | int ret, i; |
Mark Brown | d23511f | 2011-11-28 18:50:39 +0000 | [diff] [blame] | 278 | bool handled = false; |
Stephen Warren | 1603262 | 2012-07-27 13:01:54 -0600 | [diff] [blame] | 279 | u32 reg; |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 280 | |
Laxman Dewangan | ccc1256 | 2016-05-20 20:40:26 +0530 | [diff] [blame] | 281 | if (chip->handle_pre_irq) |
| 282 | chip->handle_pre_irq(chip->irq_drv_data); |
| 283 | |
Mark Brown | 0c00c50 | 2012-07-24 15:41:19 +0100 | [diff] [blame] | 284 | if (chip->runtime_pm) { |
| 285 | ret = pm_runtime_get_sync(map->dev); |
| 286 | if (ret < 0) { |
| 287 | dev_err(map->dev, "IRQ thread failed to resume: %d\n", |
| 288 | ret); |
Li Fei | 283189d | 2013-02-28 15:37:11 +0800 | [diff] [blame] | 289 | pm_runtime_put(map->dev); |
Laxman Dewangan | ccc1256 | 2016-05-20 20:40:26 +0530 | [diff] [blame] | 290 | goto exit; |
Mark Brown | 0c00c50 | 2012-07-24 15:41:19 +0100 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
Mark Brown | a7440ea | 2013-01-03 14:27:15 +0000 | [diff] [blame] | 294 | /* |
| 295 | * Read in the statuses, using a single bulk read if possible |
| 296 | * in order to reduce the I/O overheads. |
| 297 | */ |
Markus Pargmann | 67921a1 | 2015-08-21 10:26:42 +0200 | [diff] [blame] | 298 | if (!map->use_single_read && map->reg_stride == 1 && |
Mark Brown | a7440ea | 2013-01-03 14:27:15 +0000 | [diff] [blame] | 299 | data->irq_reg_stride == 1) { |
| 300 | u8 *buf8 = data->status_reg_buf; |
| 301 | u16 *buf16 = data->status_reg_buf; |
| 302 | u32 *buf32 = data->status_reg_buf; |
Graeme Gregory | 022f926a | 2012-05-14 22:40:43 +0900 | [diff] [blame] | 303 | |
Mark Brown | a7440ea | 2013-01-03 14:27:15 +0000 | [diff] [blame] | 304 | BUG_ON(!data->status_reg_buf); |
| 305 | |
| 306 | ret = regmap_bulk_read(map, chip->status_base, |
| 307 | data->status_reg_buf, |
| 308 | chip->num_regs); |
Graeme Gregory | 022f926a | 2012-05-14 22:40:43 +0900 | [diff] [blame] | 309 | if (ret != 0) { |
| 310 | dev_err(map->dev, "Failed to read IRQ status: %d\n", |
Mark Brown | a7440ea | 2013-01-03 14:27:15 +0000 | [diff] [blame] | 311 | ret); |
Laxman Dewangan | ccc1256 | 2016-05-20 20:40:26 +0530 | [diff] [blame] | 312 | goto exit; |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 313 | } |
Mark Brown | a7440ea | 2013-01-03 14:27:15 +0000 | [diff] [blame] | 314 | |
| 315 | for (i = 0; i < data->chip->num_regs; i++) { |
| 316 | switch (map->format.val_bytes) { |
| 317 | case 1: |
| 318 | data->status_buf[i] = buf8[i]; |
| 319 | break; |
| 320 | case 2: |
| 321 | data->status_buf[i] = buf16[i]; |
| 322 | break; |
| 323 | case 4: |
| 324 | data->status_buf[i] = buf32[i]; |
| 325 | break; |
| 326 | default: |
| 327 | BUG(); |
Laxman Dewangan | ccc1256 | 2016-05-20 20:40:26 +0530 | [diff] [blame] | 328 | goto exit; |
Mark Brown | a7440ea | 2013-01-03 14:27:15 +0000 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | |
| 332 | } else { |
| 333 | for (i = 0; i < data->chip->num_regs; i++) { |
| 334 | ret = regmap_read(map, chip->status_base + |
| 335 | (i * map->reg_stride |
| 336 | * data->irq_reg_stride), |
| 337 | &data->status_buf[i]); |
| 338 | |
| 339 | if (ret != 0) { |
| 340 | dev_err(map->dev, |
| 341 | "Failed to read IRQ status: %d\n", |
| 342 | ret); |
| 343 | if (chip->runtime_pm) |
| 344 | pm_runtime_put(map->dev); |
Laxman Dewangan | ccc1256 | 2016-05-20 20:40:26 +0530 | [diff] [blame] | 345 | goto exit; |
Mark Brown | a7440ea | 2013-01-03 14:27:15 +0000 | [diff] [blame] | 346 | } |
| 347 | } |
Mark Brown | bbae92c | 2013-01-03 13:58:33 +0000 | [diff] [blame] | 348 | } |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 349 | |
Mark Brown | bbae92c | 2013-01-03 13:58:33 +0000 | [diff] [blame] | 350 | /* |
| 351 | * Ignore masked IRQs and ack if we need to; we ack early so |
| 352 | * there is no race between handling and acknowleding the |
| 353 | * interrupt. We assume that typically few of the interrupts |
| 354 | * will fire simultaneously so don't worry about overhead from |
| 355 | * doing a write per register. |
| 356 | */ |
| 357 | for (i = 0; i < data->chip->num_regs; i++) { |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 358 | data->status_buf[i] &= ~data->mask_buf[i]; |
| 359 | |
Alexander Shiyan | d323343 | 2013-12-15 13:36:51 +0400 | [diff] [blame] | 360 | if (data->status_buf[i] && (chip->ack_base || chip->use_ack)) { |
Stephen Warren | 1603262 | 2012-07-27 13:01:54 -0600 | [diff] [blame] | 361 | reg = chip->ack_base + |
| 362 | (i * map->reg_stride * data->irq_reg_stride); |
| 363 | ret = regmap_write(map, reg, data->status_buf[i]); |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 364 | if (ret != 0) |
| 365 | dev_err(map->dev, "Failed to ack 0x%x: %d\n", |
Stephen Warren | 1603262 | 2012-07-27 13:01:54 -0600 | [diff] [blame] | 366 | reg, ret); |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | |
| 370 | for (i = 0; i < chip->num_irqs; i++) { |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 371 | if (data->status_buf[chip->irqs[i].reg_offset / |
| 372 | map->reg_stride] & chip->irqs[i].mask) { |
Mark Brown | 4af8be6 | 2012-05-13 10:59:56 +0100 | [diff] [blame] | 373 | handle_nested_irq(irq_find_mapping(data->domain, i)); |
Mark Brown | d23511f | 2011-11-28 18:50:39 +0000 | [diff] [blame] | 374 | handled = true; |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 375 | } |
| 376 | } |
| 377 | |
Mark Brown | 0c00c50 | 2012-07-24 15:41:19 +0100 | [diff] [blame] | 378 | if (chip->runtime_pm) |
| 379 | pm_runtime_put(map->dev); |
| 380 | |
Laxman Dewangan | ccc1256 | 2016-05-20 20:40:26 +0530 | [diff] [blame] | 381 | exit: |
| 382 | if (chip->handle_post_irq) |
| 383 | chip->handle_post_irq(chip->irq_drv_data); |
| 384 | |
Mark Brown | d23511f | 2011-11-28 18:50:39 +0000 | [diff] [blame] | 385 | if (handled) |
| 386 | return IRQ_HANDLED; |
| 387 | else |
| 388 | return IRQ_NONE; |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 389 | } |
| 390 | |
Mark Brown | 4af8be6 | 2012-05-13 10:59:56 +0100 | [diff] [blame] | 391 | static int regmap_irq_map(struct irq_domain *h, unsigned int virq, |
| 392 | irq_hw_number_t hw) |
| 393 | { |
| 394 | struct regmap_irq_chip_data *data = h->host_data; |
| 395 | |
| 396 | irq_set_chip_data(virq, data); |
Yunfan Zhang | 8138073 | 2012-09-08 03:53:25 -0700 | [diff] [blame] | 397 | irq_set_chip(virq, &data->irq_chip); |
Mark Brown | 4af8be6 | 2012-05-13 10:59:56 +0100 | [diff] [blame] | 398 | irq_set_nested_thread(virq, 1); |
Grygorii Strashko | 58a5336 | 2016-02-26 17:53:57 +0200 | [diff] [blame] | 399 | irq_set_parent(virq, data->irq); |
Mark Brown | 4af8be6 | 2012-05-13 10:59:56 +0100 | [diff] [blame] | 400 | irq_set_noprobe(virq); |
Mark Brown | 4af8be6 | 2012-05-13 10:59:56 +0100 | [diff] [blame] | 401 | |
| 402 | return 0; |
| 403 | } |
| 404 | |
Krzysztof Kozlowski | 77f5f3e | 2015-04-27 21:52:10 +0900 | [diff] [blame] | 405 | static const struct irq_domain_ops regmap_domain_ops = { |
Mark Brown | 4af8be6 | 2012-05-13 10:59:56 +0100 | [diff] [blame] | 406 | .map = regmap_irq_map, |
Vladimir Zapolskiy | 63c73b0 | 2017-06-09 12:05:16 +0300 | [diff] [blame] | 407 | .xlate = irq_domain_xlate_onetwocell, |
Mark Brown | 4af8be6 | 2012-05-13 10:59:56 +0100 | [diff] [blame] | 408 | }; |
| 409 | |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 410 | /** |
Charles Keepax | 2cf8e2d | 2017-01-12 11:17:39 +0000 | [diff] [blame] | 411 | * regmap_add_irq_chip() - Use standard regmap IRQ controller handling |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 412 | * |
Charles Keepax | 2cf8e2d | 2017-01-12 11:17:39 +0000 | [diff] [blame] | 413 | * @map: The regmap for the device. |
| 414 | * @irq: The IRQ the device uses to signal interrupts. |
| 415 | * @irq_flags: The IRQF_ flags to use for the primary interrupt. |
| 416 | * @irq_base: Allocate at specific IRQ number if irq_base > 0. |
| 417 | * @chip: Configuration for the interrupt controller. |
| 418 | * @data: Runtime data structure for the controller, allocated on success. |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 419 | * |
| 420 | * Returns 0 on success or an errno on failure. |
| 421 | * |
| 422 | * In order for this to be efficient the chip really should use a |
| 423 | * register cache. The chip driver is responsible for restoring the |
| 424 | * register values used by the IRQ controller over suspend and resume. |
| 425 | */ |
| 426 | 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] | 427 | int irq_base, const struct regmap_irq_chip *chip, |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 428 | struct regmap_irq_chip_data **data) |
| 429 | { |
| 430 | struct regmap_irq_chip_data *d; |
Mark Brown | 4af8be6 | 2012-05-13 10:59:56 +0100 | [diff] [blame] | 431 | int i; |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 432 | int ret = -ENOMEM; |
Stephen Warren | 1603262 | 2012-07-27 13:01:54 -0600 | [diff] [blame] | 433 | u32 reg; |
Guo Zeng | 7b7d196 | 2015-09-17 05:23:20 +0000 | [diff] [blame] | 434 | u32 unmask_offset; |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 435 | |
Xiubo Li | e128920 | 2014-05-19 15:13:45 +0800 | [diff] [blame] | 436 | if (chip->num_regs <= 0) |
| 437 | return -EINVAL; |
| 438 | |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 439 | for (i = 0; i < chip->num_irqs; i++) { |
| 440 | if (chip->irqs[i].reg_offset % map->reg_stride) |
| 441 | return -EINVAL; |
| 442 | if (chip->irqs[i].reg_offset / map->reg_stride >= |
| 443 | chip->num_regs) |
| 444 | return -EINVAL; |
| 445 | } |
| 446 | |
Mark Brown | 4af8be6 | 2012-05-13 10:59:56 +0100 | [diff] [blame] | 447 | if (irq_base) { |
| 448 | irq_base = irq_alloc_descs(irq_base, 0, chip->num_irqs, 0); |
| 449 | if (irq_base < 0) { |
| 450 | dev_warn(map->dev, "Failed to allocate IRQs: %d\n", |
| 451 | irq_base); |
| 452 | return irq_base; |
| 453 | } |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | d = kzalloc(sizeof(*d), GFP_KERNEL); |
| 457 | if (!d) |
| 458 | return -ENOMEM; |
| 459 | |
lixiubo | eeda1bd | 2015-11-20 18:06:29 +0800 | [diff] [blame] | 460 | d->status_buf = kcalloc(chip->num_regs, sizeof(unsigned int), |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 461 | GFP_KERNEL); |
| 462 | if (!d->status_buf) |
| 463 | goto err_alloc; |
| 464 | |
lixiubo | eeda1bd | 2015-11-20 18:06:29 +0800 | [diff] [blame] | 465 | d->mask_buf = kcalloc(chip->num_regs, sizeof(unsigned int), |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 466 | GFP_KERNEL); |
| 467 | if (!d->mask_buf) |
| 468 | goto err_alloc; |
| 469 | |
lixiubo | eeda1bd | 2015-11-20 18:06:29 +0800 | [diff] [blame] | 470 | d->mask_buf_def = kcalloc(chip->num_regs, sizeof(unsigned int), |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 471 | GFP_KERNEL); |
| 472 | if (!d->mask_buf_def) |
| 473 | goto err_alloc; |
| 474 | |
Mark Brown | a43fd50 | 2012-06-05 14:34:03 +0100 | [diff] [blame] | 475 | if (chip->wake_base) { |
lixiubo | eeda1bd | 2015-11-20 18:06:29 +0800 | [diff] [blame] | 476 | d->wake_buf = kcalloc(chip->num_regs, sizeof(unsigned int), |
Mark Brown | a43fd50 | 2012-06-05 14:34:03 +0100 | [diff] [blame] | 477 | GFP_KERNEL); |
| 478 | if (!d->wake_buf) |
| 479 | goto err_alloc; |
| 480 | } |
| 481 | |
Laxman Dewangan | 7a78479f | 2015-12-22 18:25:26 +0530 | [diff] [blame] | 482 | if (chip->num_type_reg) { |
| 483 | d->type_buf_def = kcalloc(chip->num_type_reg, |
| 484 | sizeof(unsigned int), GFP_KERNEL); |
| 485 | if (!d->type_buf_def) |
| 486 | goto err_alloc; |
| 487 | |
| 488 | d->type_buf = kcalloc(chip->num_type_reg, sizeof(unsigned int), |
| 489 | GFP_KERNEL); |
| 490 | if (!d->type_buf) |
| 491 | goto err_alloc; |
| 492 | } |
| 493 | |
Stephen Warren | 7ac140e | 2012-08-01 11:40:47 -0600 | [diff] [blame] | 494 | d->irq_chip = regmap_irq_chip; |
Stephen Warren | ca14275 | 2012-08-01 11:40:48 -0600 | [diff] [blame] | 495 | d->irq_chip.name = chip->name; |
Mark Brown | a43fd50 | 2012-06-05 14:34:03 +0100 | [diff] [blame] | 496 | d->irq = irq; |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 497 | d->map = map; |
| 498 | d->chip = chip; |
| 499 | d->irq_base = irq_base; |
Graeme Gregory | 022f926a | 2012-05-14 22:40:43 +0900 | [diff] [blame] | 500 | |
| 501 | if (chip->irq_reg_stride) |
| 502 | d->irq_reg_stride = chip->irq_reg_stride; |
| 503 | else |
| 504 | d->irq_reg_stride = 1; |
| 505 | |
Laxman Dewangan | 7a78479f | 2015-12-22 18:25:26 +0530 | [diff] [blame] | 506 | if (chip->type_reg_stride) |
| 507 | d->type_reg_stride = chip->type_reg_stride; |
| 508 | else |
| 509 | d->type_reg_stride = 1; |
| 510 | |
Markus Pargmann | 67921a1 | 2015-08-21 10:26:42 +0200 | [diff] [blame] | 511 | if (!map->use_single_read && map->reg_stride == 1 && |
Mark Brown | a7440ea | 2013-01-03 14:27:15 +0000 | [diff] [blame] | 512 | d->irq_reg_stride == 1) { |
lixiubo | 549e08a | 2015-11-20 18:06:30 +0800 | [diff] [blame] | 513 | d->status_reg_buf = kmalloc_array(chip->num_regs, |
| 514 | map->format.val_bytes, |
| 515 | GFP_KERNEL); |
Mark Brown | a7440ea | 2013-01-03 14:27:15 +0000 | [diff] [blame] | 516 | if (!d->status_reg_buf) |
| 517 | goto err_alloc; |
| 518 | } |
| 519 | |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 520 | mutex_init(&d->lock); |
| 521 | |
| 522 | for (i = 0; i < chip->num_irqs; i++) |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 523 | d->mask_buf_def[chip->irqs[i].reg_offset / map->reg_stride] |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 524 | |= chip->irqs[i].mask; |
| 525 | |
| 526 | /* Mask all the interrupts by default */ |
| 527 | for (i = 0; i < chip->num_regs; i++) { |
| 528 | d->mask_buf[i] = d->mask_buf_def[i]; |
Stephen Warren | 1603262 | 2012-07-27 13:01:54 -0600 | [diff] [blame] | 529 | reg = chip->mask_base + |
| 530 | (i * map->reg_stride * d->irq_reg_stride); |
Xiaofan Tian | 36ac914 | 2012-08-30 17:03:35 +0800 | [diff] [blame] | 531 | if (chip->mask_invert) |
Michael Grzeschik | a71411d | 2017-06-23 14:35:09 +0200 | [diff] [blame] | 532 | ret = regmap_irq_update_bits(d, reg, |
Xiaofan Tian | 36ac914 | 2012-08-30 17:03:35 +0800 | [diff] [blame] | 533 | d->mask_buf[i], ~d->mask_buf[i]); |
Guo Zeng | 7b7d196 | 2015-09-17 05:23:20 +0000 | [diff] [blame] | 534 | else if (d->chip->unmask_base) { |
| 535 | unmask_offset = d->chip->unmask_base - |
| 536 | d->chip->mask_base; |
Michael Grzeschik | a71411d | 2017-06-23 14:35:09 +0200 | [diff] [blame] | 537 | ret = regmap_irq_update_bits(d, |
Guo Zeng | 7b7d196 | 2015-09-17 05:23:20 +0000 | [diff] [blame] | 538 | reg + unmask_offset, |
| 539 | d->mask_buf[i], |
| 540 | d->mask_buf[i]); |
| 541 | } else |
Michael Grzeschik | a71411d | 2017-06-23 14:35:09 +0200 | [diff] [blame] | 542 | ret = regmap_irq_update_bits(d, reg, |
Mark Brown | 0eb46ad | 2012-08-01 20:29:14 +0100 | [diff] [blame] | 543 | d->mask_buf[i], d->mask_buf[i]); |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 544 | if (ret != 0) { |
| 545 | 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] | 546 | reg, ret); |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 547 | goto err_alloc; |
| 548 | } |
Philipp Zabel | 2753e6f | 2013-07-22 17:15:52 +0200 | [diff] [blame] | 549 | |
| 550 | if (!chip->init_ack_masked) |
| 551 | continue; |
| 552 | |
| 553 | /* Ack masked but set interrupts */ |
| 554 | reg = chip->status_base + |
| 555 | (i * map->reg_stride * d->irq_reg_stride); |
| 556 | ret = regmap_read(map, reg, &d->status_buf[i]); |
| 557 | if (ret != 0) { |
| 558 | dev_err(map->dev, "Failed to read IRQ status: %d\n", |
| 559 | ret); |
| 560 | goto err_alloc; |
| 561 | } |
| 562 | |
Alexander Shiyan | d323343 | 2013-12-15 13:36:51 +0400 | [diff] [blame] | 563 | if (d->status_buf[i] && (chip->ack_base || chip->use_ack)) { |
Philipp Zabel | 2753e6f | 2013-07-22 17:15:52 +0200 | [diff] [blame] | 564 | reg = chip->ack_base + |
| 565 | (i * map->reg_stride * d->irq_reg_stride); |
Guo Zeng | a650fdd | 2015-09-17 05:23:21 +0000 | [diff] [blame] | 566 | if (chip->ack_invert) |
| 567 | ret = regmap_write(map, reg, |
| 568 | ~(d->status_buf[i] & d->mask_buf[i])); |
| 569 | else |
| 570 | ret = regmap_write(map, reg, |
Philipp Zabel | 2753e6f | 2013-07-22 17:15:52 +0200 | [diff] [blame] | 571 | d->status_buf[i] & d->mask_buf[i]); |
| 572 | if (ret != 0) { |
| 573 | dev_err(map->dev, "Failed to ack 0x%x: %d\n", |
| 574 | reg, ret); |
| 575 | goto err_alloc; |
| 576 | } |
| 577 | } |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 578 | } |
| 579 | |
Stephen Warren | 40052ca | 2012-08-01 13:57:24 -0600 | [diff] [blame] | 580 | /* Wake is disabled by default */ |
| 581 | if (d->wake_buf) { |
| 582 | for (i = 0; i < chip->num_regs; i++) { |
| 583 | d->wake_buf[i] = d->mask_buf_def[i]; |
| 584 | reg = chip->wake_base + |
| 585 | (i * map->reg_stride * d->irq_reg_stride); |
Mark Brown | 9442490 | 2013-01-04 16:35:07 +0000 | [diff] [blame] | 586 | |
| 587 | if (chip->wake_invert) |
Michael Grzeschik | a71411d | 2017-06-23 14:35:09 +0200 | [diff] [blame] | 588 | ret = regmap_irq_update_bits(d, reg, |
Mark Brown | 9442490 | 2013-01-04 16:35:07 +0000 | [diff] [blame] | 589 | d->mask_buf_def[i], |
| 590 | 0); |
| 591 | else |
Michael Grzeschik | a71411d | 2017-06-23 14:35:09 +0200 | [diff] [blame] | 592 | ret = regmap_irq_update_bits(d, reg, |
Mark Brown | 9442490 | 2013-01-04 16:35:07 +0000 | [diff] [blame] | 593 | d->mask_buf_def[i], |
| 594 | d->wake_buf[i]); |
Stephen Warren | 40052ca | 2012-08-01 13:57:24 -0600 | [diff] [blame] | 595 | if (ret != 0) { |
| 596 | dev_err(map->dev, "Failed to set masks in 0x%x: %d\n", |
| 597 | reg, ret); |
| 598 | goto err_alloc; |
| 599 | } |
| 600 | } |
| 601 | } |
| 602 | |
Laxman Dewangan | 7a78479f | 2015-12-22 18:25:26 +0530 | [diff] [blame] | 603 | if (chip->num_type_reg) { |
| 604 | for (i = 0; i < chip->num_irqs; i++) { |
| 605 | reg = chip->irqs[i].type_reg_offset / map->reg_stride; |
| 606 | d->type_buf_def[reg] |= chip->irqs[i].type_rising_mask | |
| 607 | chip->irqs[i].type_falling_mask; |
| 608 | } |
| 609 | for (i = 0; i < chip->num_type_reg; ++i) { |
| 610 | if (!d->type_buf_def[i]) |
| 611 | continue; |
| 612 | |
| 613 | reg = chip->type_base + |
| 614 | (i * map->reg_stride * d->type_reg_stride); |
| 615 | if (chip->type_invert) |
Michael Grzeschik | a71411d | 2017-06-23 14:35:09 +0200 | [diff] [blame] | 616 | ret = regmap_irq_update_bits(d, reg, |
Laxman Dewangan | 7a78479f | 2015-12-22 18:25:26 +0530 | [diff] [blame] | 617 | d->type_buf_def[i], 0xFF); |
| 618 | else |
Michael Grzeschik | a71411d | 2017-06-23 14:35:09 +0200 | [diff] [blame] | 619 | ret = regmap_irq_update_bits(d, reg, |
Laxman Dewangan | 7a78479f | 2015-12-22 18:25:26 +0530 | [diff] [blame] | 620 | d->type_buf_def[i], 0x0); |
| 621 | if (ret != 0) { |
| 622 | dev_err(map->dev, |
| 623 | "Failed to set type in 0x%x: %x\n", |
| 624 | reg, ret); |
| 625 | goto err_alloc; |
| 626 | } |
| 627 | } |
| 628 | } |
| 629 | |
Mark Brown | 4af8be6 | 2012-05-13 10:59:56 +0100 | [diff] [blame] | 630 | if (irq_base) |
| 631 | d->domain = irq_domain_add_legacy(map->dev->of_node, |
| 632 | chip->num_irqs, irq_base, 0, |
| 633 | ®map_domain_ops, d); |
| 634 | else |
| 635 | d->domain = irq_domain_add_linear(map->dev->of_node, |
| 636 | chip->num_irqs, |
| 637 | ®map_domain_ops, d); |
| 638 | if (!d->domain) { |
| 639 | dev_err(map->dev, "Failed to create IRQ domain\n"); |
| 640 | ret = -ENOMEM; |
| 641 | goto err_alloc; |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 642 | } |
| 643 | |
Valentin Rothberg | 09cadf6 | 2015-02-11 16:37:57 +0100 | [diff] [blame] | 644 | ret = request_threaded_irq(irq, NULL, regmap_irq_thread, |
| 645 | irq_flags | IRQF_ONESHOT, |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 646 | chip->name, d); |
| 647 | if (ret != 0) { |
Mark Brown | eed456f | 2013-03-19 10:45:04 +0000 | [diff] [blame] | 648 | dev_err(map->dev, "Failed to request IRQ %d for %s: %d\n", |
| 649 | irq, chip->name, ret); |
Mark Brown | 4af8be6 | 2012-05-13 10:59:56 +0100 | [diff] [blame] | 650 | goto err_domain; |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 651 | } |
| 652 | |
Krzysztof Kozlowski | 72a6a5d | 2014-03-13 09:06:01 +0100 | [diff] [blame] | 653 | *data = d; |
| 654 | |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 655 | return 0; |
| 656 | |
Mark Brown | 4af8be6 | 2012-05-13 10:59:56 +0100 | [diff] [blame] | 657 | err_domain: |
| 658 | /* Should really dispose of the domain but... */ |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 659 | err_alloc: |
Laxman Dewangan | 7a78479f | 2015-12-22 18:25:26 +0530 | [diff] [blame] | 660 | kfree(d->type_buf); |
| 661 | kfree(d->type_buf_def); |
Mark Brown | a43fd50 | 2012-06-05 14:34:03 +0100 | [diff] [blame] | 662 | kfree(d->wake_buf); |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 663 | kfree(d->mask_buf_def); |
| 664 | kfree(d->mask_buf); |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 665 | kfree(d->status_buf); |
Mark Brown | a7440ea | 2013-01-03 14:27:15 +0000 | [diff] [blame] | 666 | kfree(d->status_reg_buf); |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 667 | kfree(d); |
| 668 | return ret; |
| 669 | } |
| 670 | EXPORT_SYMBOL_GPL(regmap_add_irq_chip); |
| 671 | |
| 672 | /** |
Charles Keepax | 2cf8e2d | 2017-01-12 11:17:39 +0000 | [diff] [blame] | 673 | * regmap_del_irq_chip() - Stop interrupt handling for a regmap IRQ chip |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 674 | * |
| 675 | * @irq: Primary IRQ for the device |
Charles Keepax | 2cf8e2d | 2017-01-12 11:17:39 +0000 | [diff] [blame] | 676 | * @d: ®map_irq_chip_data allocated by regmap_add_irq_chip() |
Laxman Dewangan | 4618951 | 2016-02-09 17:58:22 +0530 | [diff] [blame] | 677 | * |
Charles Keepax | 2cf8e2d | 2017-01-12 11:17:39 +0000 | [diff] [blame] | 678 | * This function also disposes of all mapped IRQs on the chip. |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 679 | */ |
| 680 | void regmap_del_irq_chip(int irq, struct regmap_irq_chip_data *d) |
| 681 | { |
Laxman Dewangan | 4618951 | 2016-02-09 17:58:22 +0530 | [diff] [blame] | 682 | unsigned int virq; |
| 683 | int hwirq; |
| 684 | |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 685 | if (!d) |
| 686 | return; |
| 687 | |
| 688 | free_irq(irq, d); |
Laxman Dewangan | 4618951 | 2016-02-09 17:58:22 +0530 | [diff] [blame] | 689 | |
| 690 | /* Dispose all virtual irq from irq domain before removing it */ |
| 691 | for (hwirq = 0; hwirq < d->chip->num_irqs; hwirq++) { |
| 692 | /* Ignore hwirq if holes in the IRQ list */ |
| 693 | if (!d->chip->irqs[hwirq].mask) |
| 694 | continue; |
| 695 | |
| 696 | /* |
| 697 | * Find the virtual irq of hwirq on chip and if it is |
| 698 | * there then dispose it |
| 699 | */ |
| 700 | virq = irq_find_mapping(d->domain, hwirq); |
| 701 | if (virq) |
| 702 | irq_dispose_mapping(virq); |
| 703 | } |
| 704 | |
Mark Brown | b5ab3e5 | 2014-01-22 20:25:48 +0000 | [diff] [blame] | 705 | irq_domain_remove(d->domain); |
Laxman Dewangan | 7a78479f | 2015-12-22 18:25:26 +0530 | [diff] [blame] | 706 | kfree(d->type_buf); |
| 707 | kfree(d->type_buf_def); |
Mark Brown | a43fd50 | 2012-06-05 14:34:03 +0100 | [diff] [blame] | 708 | kfree(d->wake_buf); |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 709 | kfree(d->mask_buf_def); |
| 710 | kfree(d->mask_buf); |
Mark Brown | a7440ea | 2013-01-03 14:27:15 +0000 | [diff] [blame] | 711 | kfree(d->status_reg_buf); |
Mark Brown | f8beab2 | 2011-10-28 23:50:49 +0200 | [diff] [blame] | 712 | kfree(d->status_buf); |
| 713 | kfree(d); |
| 714 | } |
| 715 | EXPORT_SYMBOL_GPL(regmap_del_irq_chip); |
Mark Brown | 209a600 | 2011-12-05 16:10:15 +0000 | [diff] [blame] | 716 | |
Laxman Dewangan | 800c3a0 | 2016-02-10 14:29:50 +0530 | [diff] [blame] | 717 | static void devm_regmap_irq_chip_release(struct device *dev, void *res) |
| 718 | { |
| 719 | struct regmap_irq_chip_data *d = *(struct regmap_irq_chip_data **)res; |
| 720 | |
| 721 | regmap_del_irq_chip(d->irq, d); |
| 722 | } |
| 723 | |
| 724 | static int devm_regmap_irq_chip_match(struct device *dev, void *res, void *data) |
| 725 | |
| 726 | { |
| 727 | struct regmap_irq_chip_data **r = res; |
| 728 | |
| 729 | if (!r || !*r) { |
| 730 | WARN_ON(!r || !*r); |
| 731 | return 0; |
| 732 | } |
| 733 | return *r == data; |
| 734 | } |
| 735 | |
| 736 | /** |
Charles Keepax | 2cf8e2d | 2017-01-12 11:17:39 +0000 | [diff] [blame] | 737 | * devm_regmap_add_irq_chip() - Resource manager regmap_add_irq_chip() |
Laxman Dewangan | 800c3a0 | 2016-02-10 14:29:50 +0530 | [diff] [blame] | 738 | * |
Charles Keepax | 2cf8e2d | 2017-01-12 11:17:39 +0000 | [diff] [blame] | 739 | * @dev: The device pointer on which irq_chip belongs to. |
| 740 | * @map: The regmap for the device. |
| 741 | * @irq: The IRQ the device uses to signal interrupts |
Laxman Dewangan | 800c3a0 | 2016-02-10 14:29:50 +0530 | [diff] [blame] | 742 | * @irq_flags: The IRQF_ flags to use for the primary interrupt. |
Charles Keepax | 2cf8e2d | 2017-01-12 11:17:39 +0000 | [diff] [blame] | 743 | * @irq_base: Allocate at specific IRQ number if irq_base > 0. |
| 744 | * @chip: Configuration for the interrupt controller. |
| 745 | * @data: Runtime data structure for the controller, allocated on success |
Laxman Dewangan | 800c3a0 | 2016-02-10 14:29:50 +0530 | [diff] [blame] | 746 | * |
| 747 | * Returns 0 on success or an errno on failure. |
| 748 | * |
Charles Keepax | 2cf8e2d | 2017-01-12 11:17:39 +0000 | [diff] [blame] | 749 | * The ®map_irq_chip_data will be automatically released when the device is |
Laxman Dewangan | 800c3a0 | 2016-02-10 14:29:50 +0530 | [diff] [blame] | 750 | * unbound. |
| 751 | */ |
| 752 | int devm_regmap_add_irq_chip(struct device *dev, struct regmap *map, int irq, |
| 753 | int irq_flags, int irq_base, |
| 754 | const struct regmap_irq_chip *chip, |
| 755 | struct regmap_irq_chip_data **data) |
| 756 | { |
| 757 | struct regmap_irq_chip_data **ptr, *d; |
| 758 | int ret; |
| 759 | |
| 760 | ptr = devres_alloc(devm_regmap_irq_chip_release, sizeof(*ptr), |
| 761 | GFP_KERNEL); |
| 762 | if (!ptr) |
| 763 | return -ENOMEM; |
| 764 | |
| 765 | ret = regmap_add_irq_chip(map, irq, irq_flags, irq_base, |
| 766 | chip, &d); |
| 767 | if (ret < 0) { |
| 768 | devres_free(ptr); |
| 769 | return ret; |
| 770 | } |
| 771 | |
| 772 | *ptr = d; |
| 773 | devres_add(dev, ptr); |
| 774 | *data = d; |
| 775 | return 0; |
| 776 | } |
| 777 | EXPORT_SYMBOL_GPL(devm_regmap_add_irq_chip); |
| 778 | |
| 779 | /** |
Charles Keepax | 2cf8e2d | 2017-01-12 11:17:39 +0000 | [diff] [blame] | 780 | * devm_regmap_del_irq_chip() - Resource managed regmap_del_irq_chip() |
Laxman Dewangan | 800c3a0 | 2016-02-10 14:29:50 +0530 | [diff] [blame] | 781 | * |
| 782 | * @dev: Device for which which resource was allocated. |
Charles Keepax | 2cf8e2d | 2017-01-12 11:17:39 +0000 | [diff] [blame] | 783 | * @irq: Primary IRQ for the device. |
| 784 | * @data: ®map_irq_chip_data allocated by regmap_add_irq_chip(). |
| 785 | * |
| 786 | * A resource managed version of regmap_del_irq_chip(). |
Laxman Dewangan | 800c3a0 | 2016-02-10 14:29:50 +0530 | [diff] [blame] | 787 | */ |
| 788 | void devm_regmap_del_irq_chip(struct device *dev, int irq, |
| 789 | struct regmap_irq_chip_data *data) |
| 790 | { |
| 791 | int rc; |
| 792 | |
| 793 | WARN_ON(irq != data->irq); |
| 794 | rc = devres_release(dev, devm_regmap_irq_chip_release, |
| 795 | devm_regmap_irq_chip_match, data); |
| 796 | |
| 797 | if (rc != 0) |
| 798 | WARN_ON(rc); |
| 799 | } |
| 800 | EXPORT_SYMBOL_GPL(devm_regmap_del_irq_chip); |
| 801 | |
Mark Brown | 209a600 | 2011-12-05 16:10:15 +0000 | [diff] [blame] | 802 | /** |
Charles Keepax | 2cf8e2d | 2017-01-12 11:17:39 +0000 | [diff] [blame] | 803 | * regmap_irq_chip_get_base() - Retrieve interrupt base for a regmap IRQ chip |
| 804 | * |
| 805 | * @data: regmap irq controller to operate on. |
Mark Brown | 209a600 | 2011-12-05 16:10:15 +0000 | [diff] [blame] | 806 | * |
| 807 | * Useful for drivers to request their own IRQs. |
Mark Brown | 209a600 | 2011-12-05 16:10:15 +0000 | [diff] [blame] | 808 | */ |
| 809 | int regmap_irq_chip_get_base(struct regmap_irq_chip_data *data) |
| 810 | { |
Mark Brown | 4af8be6 | 2012-05-13 10:59:56 +0100 | [diff] [blame] | 811 | WARN_ON(!data->irq_base); |
Mark Brown | 209a600 | 2011-12-05 16:10:15 +0000 | [diff] [blame] | 812 | return data->irq_base; |
| 813 | } |
| 814 | EXPORT_SYMBOL_GPL(regmap_irq_chip_get_base); |
Mark Brown | 4af8be6 | 2012-05-13 10:59:56 +0100 | [diff] [blame] | 815 | |
| 816 | /** |
Charles Keepax | 2cf8e2d | 2017-01-12 11:17:39 +0000 | [diff] [blame] | 817 | * regmap_irq_get_virq() - Map an interrupt on a chip to a virtual IRQ |
| 818 | * |
| 819 | * @data: regmap irq controller to operate on. |
| 820 | * @irq: index of the interrupt requested in the chip IRQs. |
Mark Brown | 4af8be6 | 2012-05-13 10:59:56 +0100 | [diff] [blame] | 821 | * |
| 822 | * Useful for drivers to request their own IRQs. |
Mark Brown | 4af8be6 | 2012-05-13 10:59:56 +0100 | [diff] [blame] | 823 | */ |
| 824 | int regmap_irq_get_virq(struct regmap_irq_chip_data *data, int irq) |
| 825 | { |
Mark Brown | bfd6185d | 2012-06-05 14:29:36 +0100 | [diff] [blame] | 826 | /* Handle holes in the IRQ list */ |
| 827 | if (!data->chip->irqs[irq].mask) |
| 828 | return -EINVAL; |
| 829 | |
Mark Brown | 4af8be6 | 2012-05-13 10:59:56 +0100 | [diff] [blame] | 830 | return irq_create_mapping(data->domain, irq); |
| 831 | } |
| 832 | EXPORT_SYMBOL_GPL(regmap_irq_get_virq); |
Mark Brown | 90f790d | 2012-08-20 21:45:05 +0100 | [diff] [blame] | 833 | |
| 834 | /** |
Charles Keepax | 2cf8e2d | 2017-01-12 11:17:39 +0000 | [diff] [blame] | 835 | * regmap_irq_get_domain() - Retrieve the irq_domain for the chip |
| 836 | * |
| 837 | * @data: regmap_irq controller to operate on. |
Mark Brown | 90f790d | 2012-08-20 21:45:05 +0100 | [diff] [blame] | 838 | * |
| 839 | * Useful for drivers to request their own IRQs and for integration |
| 840 | * with subsystems. For ease of integration NULL is accepted as a |
| 841 | * domain, allowing devices to just call this even if no domain is |
| 842 | * allocated. |
Mark Brown | 90f790d | 2012-08-20 21:45:05 +0100 | [diff] [blame] | 843 | */ |
| 844 | struct irq_domain *regmap_irq_get_domain(struct regmap_irq_chip_data *data) |
| 845 | { |
| 846 | if (data) |
| 847 | return data->domain; |
| 848 | else |
| 849 | return NULL; |
| 850 | } |
| 851 | EXPORT_SYMBOL_GPL(regmap_irq_get_domain); |