blob: 429ca8ed7e518087bc1ddaebd9f4c8393eb5179e [file] [log] [blame]
Mark Brownf8beab22011-10-28 23:50:49 +02001/*
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 Gortmaker51990e82012-01-22 11:23:42 -050013#include <linux/device.h>
Xiubo Lie1289202014-05-19 15:13:45 +080014#include <linux/export.h>
Mark Brownf8beab22011-10-28 23:50:49 +020015#include <linux/interrupt.h>
Xiubo Lie1289202014-05-19 15:13:45 +080016#include <linux/irq.h>
Mark Brown4af8be62012-05-13 10:59:56 +010017#include <linux/irqdomain.h>
Mark Brown0c00c502012-07-24 15:41:19 +010018#include <linux/pm_runtime.h>
Xiubo Lie1289202014-05-19 15:13:45 +080019#include <linux/regmap.h>
Mark Brownf8beab22011-10-28 23:50:49 +020020#include <linux/slab.h>
21
22#include "internal.h"
23
24struct regmap_irq_chip_data {
25 struct mutex lock;
Stephen Warren7ac140e2012-08-01 11:40:47 -060026 struct irq_chip irq_chip;
Mark Brownf8beab22011-10-28 23:50:49 +020027
28 struct regmap *map;
Mark Brownb026ddb2012-05-31 21:01:46 +010029 const struct regmap_irq_chip *chip;
Mark Brownf8beab22011-10-28 23:50:49 +020030
31 int irq_base;
Mark Brown4af8be62012-05-13 10:59:56 +010032 struct irq_domain *domain;
Mark Brownf8beab22011-10-28 23:50:49 +020033
Mark Browna43fd502012-06-05 14:34:03 +010034 int irq;
35 int wake_count;
36
Mark Browna7440ea2013-01-03 14:27:15 +000037 void *status_reg_buf;
Mark Brownf8beab22011-10-28 23:50:49 +020038 unsigned int *status_buf;
39 unsigned int *mask_buf;
40 unsigned int *mask_buf_def;
Mark Browna43fd502012-06-05 14:34:03 +010041 unsigned int *wake_buf;
Laxman Dewangan7a78479f2015-12-22 18:25:26 +053042 unsigned int *type_buf;
43 unsigned int *type_buf_def;
Graeme Gregory022f926a2012-05-14 22:40:43 +090044
45 unsigned int irq_reg_stride;
Laxman Dewangan7a78479f2015-12-22 18:25:26 +053046 unsigned int type_reg_stride;
Mark Brownf8beab22011-10-28 23:50:49 +020047};
48
49static inline const
50struct regmap_irq *irq_to_regmap_irq(struct regmap_irq_chip_data *data,
51 int irq)
52{
Mark Brown4af8be62012-05-13 10:59:56 +010053 return &data->chip->irqs[irq];
Mark Brownf8beab22011-10-28 23:50:49 +020054}
55
56static 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 Grzeschika71411d2017-06-23 14:35:09 +020063static 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 Brownf8beab22011-10-28 23:50:49 +020073static 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 Warren56806552012-04-10 23:37:22 -060076 struct regmap *map = d->map;
Mark Brownf8beab22011-10-28 23:50:49 +020077 int i, ret;
Stephen Warren16032622012-07-27 13:01:54 -060078 u32 reg;
Guo Zeng7b7d1962015-09-17 05:23:20 +000079 u32 unmask_offset;
Mark Brownf8beab22011-10-28 23:50:49 +020080
Mark Brown0c00c502012-07-24 15:41:19 +010081 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 Brownf8beab22011-10-28 23:50:49 +020088 /*
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 Warren16032622012-07-27 13:01:54 -060094 reg = d->chip->mask_base +
95 (i * map->reg_stride * d->irq_reg_stride);
Guo Zeng7b7d1962015-09-17 05:23:20 +000096 if (d->chip->mask_invert) {
Michael Grzeschika71411d2017-06-23 14:35:09 +020097 ret = regmap_irq_update_bits(d, reg,
Xiaofan Tian36ac9142012-08-30 17:03:35 +080098 d->mask_buf_def[i], ~d->mask_buf[i]);
Guo Zeng7b7d1962015-09-17 05:23:20 +000099 } else if (d->chip->unmask_base) {
100 /* set mask with mask_base register */
Michael Grzeschika71411d2017-06-23 14:35:09 +0200101 ret = regmap_irq_update_bits(d, reg,
Guo Zeng7b7d1962015-09-17 05:23:20 +0000102 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 Grzeschika71411d2017-06-23 14:35:09 +0200110 ret = regmap_irq_update_bits(d,
Guo Zeng7b7d1962015-09-17 05:23:20 +0000111 reg + unmask_offset,
112 d->mask_buf_def[i],
113 d->mask_buf[i]);
114 } else {
Michael Grzeschika71411d2017-06-23 14:35:09 +0200115 ret = regmap_irq_update_bits(d, reg,
Mark Brownf8beab22011-10-28 23:50:49 +0200116 d->mask_buf_def[i], d->mask_buf[i]);
Guo Zeng7b7d1962015-09-17 05:23:20 +0000117 }
Mark Brownf8beab22011-10-28 23:50:49 +0200118 if (ret != 0)
119 dev_err(d->map->dev, "Failed to sync masks in %x\n",
Stephen Warren16032622012-07-27 13:01:54 -0600120 reg);
Mark Brown33be4932013-01-04 16:32:54 +0000121
122 reg = d->chip->wake_base +
123 (i * map->reg_stride * d->irq_reg_stride);
124 if (d->wake_buf) {
Mark Brown94424902013-01-04 16:35:07 +0000125 if (d->chip->wake_invert)
Michael Grzeschika71411d2017-06-23 14:35:09 +0200126 ret = regmap_irq_update_bits(d, reg,
Mark Brown94424902013-01-04 16:35:07 +0000127 d->mask_buf_def[i],
128 ~d->wake_buf[i]);
129 else
Michael Grzeschika71411d2017-06-23 14:35:09 +0200130 ret = regmap_irq_update_bits(d, reg,
Mark Brown94424902013-01-04 16:35:07 +0000131 d->mask_buf_def[i],
132 d->wake_buf[i]);
Mark Brown33be4932013-01-04 16:32:54 +0000133 if (ret != 0)
134 dev_err(d->map->dev,
135 "Failed to sync wakes in %x: %d\n",
136 reg, ret);
137 }
Yi Zhang4bd71452013-10-22 18:44:32 +0800138
139 if (!d->chip->init_ack_masked)
140 continue;
141 /*
dashsriram7043f5f2015-05-27 00:55:13 +0530142 * Ack all the masked interrupts unconditionally,
Yi Zhang4bd71452013-10-22 18:44:32 +0800143 * 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 Shiyand3233432013-12-15 13:36:51 +0400146 if (d->mask_buf[i] && (d->chip->ack_base || d->chip->use_ack)) {
Yi Zhang4bd71452013-10-22 18:44:32 +0800147 reg = d->chip->ack_base +
148 (i * map->reg_stride * d->irq_reg_stride);
Guo Zenga650fdd2015-09-17 05:23:21 +0000149 /* 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 Zhang4bd71452013-10-22 18:44:32 +0800154 if (ret != 0)
155 dev_err(d->map->dev, "Failed to ack 0x%x: %d\n",
156 reg, ret);
157 }
Mark Brownf8beab22011-10-28 23:50:49 +0200158 }
159
Laxman Dewangan7a78479f2015-12-22 18:25:26 +0530160 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 Grzeschika71411d2017-06-23 14:35:09 +0200166 ret = regmap_irq_update_bits(d, reg,
Laxman Dewangan7a78479f2015-12-22 18:25:26 +0530167 d->type_buf_def[i], ~d->type_buf[i]);
168 else
Michael Grzeschika71411d2017-06-23 14:35:09 +0200169 ret = regmap_irq_update_bits(d, reg,
Laxman Dewangan7a78479f2015-12-22 18:25:26 +0530170 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 Brown0c00c502012-07-24 15:41:19 +0100176 if (d->chip->runtime_pm)
177 pm_runtime_put(map->dev);
178
Mark Browna43fd502012-06-05 14:34:03 +0100179 /* 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 Brownf8beab22011-10-28 23:50:49 +0200189 mutex_unlock(&d->lock);
190}
191
192static void regmap_irq_enable(struct irq_data *data)
193{
194 struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data);
Stephen Warren56806552012-04-10 23:37:22 -0600195 struct regmap *map = d->map;
Mark Brown4af8be62012-05-13 10:59:56 +0100196 const struct regmap_irq *irq_data = irq_to_regmap_irq(d, data->hwirq);
Mark Brownf8beab22011-10-28 23:50:49 +0200197
Stephen Warrenf01ee602012-04-09 13:40:24 -0600198 d->mask_buf[irq_data->reg_offset / map->reg_stride] &= ~irq_data->mask;
Mark Brownf8beab22011-10-28 23:50:49 +0200199}
200
201static void regmap_irq_disable(struct irq_data *data)
202{
203 struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data);
Stephen Warren56806552012-04-10 23:37:22 -0600204 struct regmap *map = d->map;
Mark Brown4af8be62012-05-13 10:59:56 +0100205 const struct regmap_irq *irq_data = irq_to_regmap_irq(d, data->hwirq);
Mark Brownf8beab22011-10-28 23:50:49 +0200206
Stephen Warrenf01ee602012-04-09 13:40:24 -0600207 d->mask_buf[irq_data->reg_offset / map->reg_stride] |= irq_data->mask;
Mark Brownf8beab22011-10-28 23:50:49 +0200208}
209
Laxman Dewangan7a78479f2015-12-22 18:25:26 +0530210static 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 Browna43fd502012-06-05 14:34:03 +0100242static 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 Browna43fd502012-06-05 14:34:03 +0100248 if (on) {
Laxman Dewangan55ac85e2012-12-19 19:42:28 +0530249 if (d->wake_buf)
250 d->wake_buf[irq_data->reg_offset / map->reg_stride]
251 &= ~irq_data->mask;
Mark Browna43fd502012-06-05 14:34:03 +0100252 d->wake_count++;
253 } else {
Laxman Dewangan55ac85e2012-12-19 19:42:28 +0530254 if (d->wake_buf)
255 d->wake_buf[irq_data->reg_offset / map->reg_stride]
256 |= irq_data->mask;
Mark Browna43fd502012-06-05 14:34:03 +0100257 d->wake_count--;
258 }
259
260 return 0;
261}
262
Stephen Warren7ac140e2012-08-01 11:40:47 -0600263static const struct irq_chip regmap_irq_chip = {
Mark Brownf8beab22011-10-28 23:50:49 +0200264 .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 Dewangan7a78479f2015-12-22 18:25:26 +0530268 .irq_set_type = regmap_irq_set_type,
Mark Browna43fd502012-06-05 14:34:03 +0100269 .irq_set_wake = regmap_irq_set_wake,
Mark Brownf8beab22011-10-28 23:50:49 +0200270};
271
272static irqreturn_t regmap_irq_thread(int irq, void *d)
273{
274 struct regmap_irq_chip_data *data = d;
Mark Brownb026ddb2012-05-31 21:01:46 +0100275 const struct regmap_irq_chip *chip = data->chip;
Mark Brownf8beab22011-10-28 23:50:49 +0200276 struct regmap *map = data->map;
277 int ret, i;
Mark Brownd23511f2011-11-28 18:50:39 +0000278 bool handled = false;
Stephen Warren16032622012-07-27 13:01:54 -0600279 u32 reg;
Mark Brownf8beab22011-10-28 23:50:49 +0200280
Laxman Dewanganccc12562016-05-20 20:40:26 +0530281 if (chip->handle_pre_irq)
282 chip->handle_pre_irq(chip->irq_drv_data);
283
Mark Brown0c00c502012-07-24 15:41:19 +0100284 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 Fei283189d2013-02-28 15:37:11 +0800289 pm_runtime_put(map->dev);
Laxman Dewanganccc12562016-05-20 20:40:26 +0530290 goto exit;
Mark Brown0c00c502012-07-24 15:41:19 +0100291 }
292 }
293
Mark Browna7440ea2013-01-03 14:27:15 +0000294 /*
295 * Read in the statuses, using a single bulk read if possible
296 * in order to reduce the I/O overheads.
297 */
Markus Pargmann67921a12015-08-21 10:26:42 +0200298 if (!map->use_single_read && map->reg_stride == 1 &&
Mark Browna7440ea2013-01-03 14:27:15 +0000299 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 Gregory022f926a2012-05-14 22:40:43 +0900303
Mark Browna7440ea2013-01-03 14:27:15 +0000304 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 Gregory022f926a2012-05-14 22:40:43 +0900309 if (ret != 0) {
310 dev_err(map->dev, "Failed to read IRQ status: %d\n",
Mark Browna7440ea2013-01-03 14:27:15 +0000311 ret);
Laxman Dewanganccc12562016-05-20 20:40:26 +0530312 goto exit;
Mark Brownf8beab22011-10-28 23:50:49 +0200313 }
Mark Browna7440ea2013-01-03 14:27:15 +0000314
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 Dewanganccc12562016-05-20 20:40:26 +0530328 goto exit;
Mark Browna7440ea2013-01-03 14:27:15 +0000329 }
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 Dewanganccc12562016-05-20 20:40:26 +0530345 goto exit;
Mark Browna7440ea2013-01-03 14:27:15 +0000346 }
347 }
Mark Brownbbae92c2013-01-03 13:58:33 +0000348 }
Mark Brownf8beab22011-10-28 23:50:49 +0200349
Mark Brownbbae92c2013-01-03 13:58:33 +0000350 /*
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 Brownf8beab22011-10-28 23:50:49 +0200358 data->status_buf[i] &= ~data->mask_buf[i];
359
Alexander Shiyand3233432013-12-15 13:36:51 +0400360 if (data->status_buf[i] && (chip->ack_base || chip->use_ack)) {
Stephen Warren16032622012-07-27 13:01:54 -0600361 reg = chip->ack_base +
362 (i * map->reg_stride * data->irq_reg_stride);
363 ret = regmap_write(map, reg, data->status_buf[i]);
Mark Brownf8beab22011-10-28 23:50:49 +0200364 if (ret != 0)
365 dev_err(map->dev, "Failed to ack 0x%x: %d\n",
Stephen Warren16032622012-07-27 13:01:54 -0600366 reg, ret);
Mark Brownf8beab22011-10-28 23:50:49 +0200367 }
368 }
369
370 for (i = 0; i < chip->num_irqs; i++) {
Stephen Warrenf01ee602012-04-09 13:40:24 -0600371 if (data->status_buf[chip->irqs[i].reg_offset /
372 map->reg_stride] & chip->irqs[i].mask) {
Mark Brown4af8be62012-05-13 10:59:56 +0100373 handle_nested_irq(irq_find_mapping(data->domain, i));
Mark Brownd23511f2011-11-28 18:50:39 +0000374 handled = true;
Mark Brownf8beab22011-10-28 23:50:49 +0200375 }
376 }
377
Mark Brown0c00c502012-07-24 15:41:19 +0100378 if (chip->runtime_pm)
379 pm_runtime_put(map->dev);
380
Laxman Dewanganccc12562016-05-20 20:40:26 +0530381exit:
382 if (chip->handle_post_irq)
383 chip->handle_post_irq(chip->irq_drv_data);
384
Mark Brownd23511f2011-11-28 18:50:39 +0000385 if (handled)
386 return IRQ_HANDLED;
387 else
388 return IRQ_NONE;
Mark Brownf8beab22011-10-28 23:50:49 +0200389}
390
Mark Brown4af8be62012-05-13 10:59:56 +0100391static 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 Zhang81380732012-09-08 03:53:25 -0700397 irq_set_chip(virq, &data->irq_chip);
Mark Brown4af8be62012-05-13 10:59:56 +0100398 irq_set_nested_thread(virq, 1);
Grygorii Strashko58a53362016-02-26 17:53:57 +0200399 irq_set_parent(virq, data->irq);
Mark Brown4af8be62012-05-13 10:59:56 +0100400 irq_set_noprobe(virq);
Mark Brown4af8be62012-05-13 10:59:56 +0100401
402 return 0;
403}
404
Krzysztof Kozlowski77f5f3e2015-04-27 21:52:10 +0900405static const struct irq_domain_ops regmap_domain_ops = {
Mark Brown4af8be62012-05-13 10:59:56 +0100406 .map = regmap_irq_map,
Vladimir Zapolskiy63c73b02017-06-09 12:05:16 +0300407 .xlate = irq_domain_xlate_onetwocell,
Mark Brown4af8be62012-05-13 10:59:56 +0100408};
409
Mark Brownf8beab22011-10-28 23:50:49 +0200410/**
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000411 * regmap_add_irq_chip() - Use standard regmap IRQ controller handling
Mark Brownf8beab22011-10-28 23:50:49 +0200412 *
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000413 * @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 Brownf8beab22011-10-28 23:50:49 +0200419 *
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 */
426int regmap_add_irq_chip(struct regmap *map, int irq, int irq_flags,
Mark Brownb026ddb2012-05-31 21:01:46 +0100427 int irq_base, const struct regmap_irq_chip *chip,
Mark Brownf8beab22011-10-28 23:50:49 +0200428 struct regmap_irq_chip_data **data)
429{
430 struct regmap_irq_chip_data *d;
Mark Brown4af8be62012-05-13 10:59:56 +0100431 int i;
Mark Brownf8beab22011-10-28 23:50:49 +0200432 int ret = -ENOMEM;
Stephen Warren16032622012-07-27 13:01:54 -0600433 u32 reg;
Guo Zeng7b7d1962015-09-17 05:23:20 +0000434 u32 unmask_offset;
Mark Brownf8beab22011-10-28 23:50:49 +0200435
Xiubo Lie1289202014-05-19 15:13:45 +0800436 if (chip->num_regs <= 0)
437 return -EINVAL;
438
Stephen Warrenf01ee602012-04-09 13:40:24 -0600439 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 Brown4af8be62012-05-13 10:59:56 +0100447 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 Brownf8beab22011-10-28 23:50:49 +0200454 }
455
456 d = kzalloc(sizeof(*d), GFP_KERNEL);
457 if (!d)
458 return -ENOMEM;
459
lixiuboeeda1bd2015-11-20 18:06:29 +0800460 d->status_buf = kcalloc(chip->num_regs, sizeof(unsigned int),
Mark Brownf8beab22011-10-28 23:50:49 +0200461 GFP_KERNEL);
462 if (!d->status_buf)
463 goto err_alloc;
464
lixiuboeeda1bd2015-11-20 18:06:29 +0800465 d->mask_buf = kcalloc(chip->num_regs, sizeof(unsigned int),
Mark Brownf8beab22011-10-28 23:50:49 +0200466 GFP_KERNEL);
467 if (!d->mask_buf)
468 goto err_alloc;
469
lixiuboeeda1bd2015-11-20 18:06:29 +0800470 d->mask_buf_def = kcalloc(chip->num_regs, sizeof(unsigned int),
Mark Brownf8beab22011-10-28 23:50:49 +0200471 GFP_KERNEL);
472 if (!d->mask_buf_def)
473 goto err_alloc;
474
Mark Browna43fd502012-06-05 14:34:03 +0100475 if (chip->wake_base) {
lixiuboeeda1bd2015-11-20 18:06:29 +0800476 d->wake_buf = kcalloc(chip->num_regs, sizeof(unsigned int),
Mark Browna43fd502012-06-05 14:34:03 +0100477 GFP_KERNEL);
478 if (!d->wake_buf)
479 goto err_alloc;
480 }
481
Laxman Dewangan7a78479f2015-12-22 18:25:26 +0530482 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 Warren7ac140e2012-08-01 11:40:47 -0600494 d->irq_chip = regmap_irq_chip;
Stephen Warrenca142752012-08-01 11:40:48 -0600495 d->irq_chip.name = chip->name;
Mark Browna43fd502012-06-05 14:34:03 +0100496 d->irq = irq;
Mark Brownf8beab22011-10-28 23:50:49 +0200497 d->map = map;
498 d->chip = chip;
499 d->irq_base = irq_base;
Graeme Gregory022f926a2012-05-14 22:40:43 +0900500
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 Dewangan7a78479f2015-12-22 18:25:26 +0530506 if (chip->type_reg_stride)
507 d->type_reg_stride = chip->type_reg_stride;
508 else
509 d->type_reg_stride = 1;
510
Markus Pargmann67921a12015-08-21 10:26:42 +0200511 if (!map->use_single_read && map->reg_stride == 1 &&
Mark Browna7440ea2013-01-03 14:27:15 +0000512 d->irq_reg_stride == 1) {
lixiubo549e08a2015-11-20 18:06:30 +0800513 d->status_reg_buf = kmalloc_array(chip->num_regs,
514 map->format.val_bytes,
515 GFP_KERNEL);
Mark Browna7440ea2013-01-03 14:27:15 +0000516 if (!d->status_reg_buf)
517 goto err_alloc;
518 }
519
Mark Brownf8beab22011-10-28 23:50:49 +0200520 mutex_init(&d->lock);
521
522 for (i = 0; i < chip->num_irqs; i++)
Stephen Warrenf01ee602012-04-09 13:40:24 -0600523 d->mask_buf_def[chip->irqs[i].reg_offset / map->reg_stride]
Mark Brownf8beab22011-10-28 23:50:49 +0200524 |= 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 Warren16032622012-07-27 13:01:54 -0600529 reg = chip->mask_base +
530 (i * map->reg_stride * d->irq_reg_stride);
Xiaofan Tian36ac9142012-08-30 17:03:35 +0800531 if (chip->mask_invert)
Michael Grzeschika71411d2017-06-23 14:35:09 +0200532 ret = regmap_irq_update_bits(d, reg,
Xiaofan Tian36ac9142012-08-30 17:03:35 +0800533 d->mask_buf[i], ~d->mask_buf[i]);
Guo Zeng7b7d1962015-09-17 05:23:20 +0000534 else if (d->chip->unmask_base) {
535 unmask_offset = d->chip->unmask_base -
536 d->chip->mask_base;
Michael Grzeschika71411d2017-06-23 14:35:09 +0200537 ret = regmap_irq_update_bits(d,
Guo Zeng7b7d1962015-09-17 05:23:20 +0000538 reg + unmask_offset,
539 d->mask_buf[i],
540 d->mask_buf[i]);
541 } else
Michael Grzeschika71411d2017-06-23 14:35:09 +0200542 ret = regmap_irq_update_bits(d, reg,
Mark Brown0eb46ad2012-08-01 20:29:14 +0100543 d->mask_buf[i], d->mask_buf[i]);
Mark Brownf8beab22011-10-28 23:50:49 +0200544 if (ret != 0) {
545 dev_err(map->dev, "Failed to set masks in 0x%x: %d\n",
Stephen Warren16032622012-07-27 13:01:54 -0600546 reg, ret);
Mark Brownf8beab22011-10-28 23:50:49 +0200547 goto err_alloc;
548 }
Philipp Zabel2753e6f2013-07-22 17:15:52 +0200549
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 Shiyand3233432013-12-15 13:36:51 +0400563 if (d->status_buf[i] && (chip->ack_base || chip->use_ack)) {
Philipp Zabel2753e6f2013-07-22 17:15:52 +0200564 reg = chip->ack_base +
565 (i * map->reg_stride * d->irq_reg_stride);
Guo Zenga650fdd2015-09-17 05:23:21 +0000566 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 Zabel2753e6f2013-07-22 17:15:52 +0200571 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 Brownf8beab22011-10-28 23:50:49 +0200578 }
579
Stephen Warren40052ca2012-08-01 13:57:24 -0600580 /* 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 Brown94424902013-01-04 16:35:07 +0000586
587 if (chip->wake_invert)
Michael Grzeschika71411d2017-06-23 14:35:09 +0200588 ret = regmap_irq_update_bits(d, reg,
Mark Brown94424902013-01-04 16:35:07 +0000589 d->mask_buf_def[i],
590 0);
591 else
Michael Grzeschika71411d2017-06-23 14:35:09 +0200592 ret = regmap_irq_update_bits(d, reg,
Mark Brown94424902013-01-04 16:35:07 +0000593 d->mask_buf_def[i],
594 d->wake_buf[i]);
Stephen Warren40052ca2012-08-01 13:57:24 -0600595 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 Dewangan7a78479f2015-12-22 18:25:26 +0530603 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 Grzeschika71411d2017-06-23 14:35:09 +0200616 ret = regmap_irq_update_bits(d, reg,
Laxman Dewangan7a78479f2015-12-22 18:25:26 +0530617 d->type_buf_def[i], 0xFF);
618 else
Michael Grzeschika71411d2017-06-23 14:35:09 +0200619 ret = regmap_irq_update_bits(d, reg,
Laxman Dewangan7a78479f2015-12-22 18:25:26 +0530620 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 Brown4af8be62012-05-13 10:59:56 +0100630 if (irq_base)
631 d->domain = irq_domain_add_legacy(map->dev->of_node,
632 chip->num_irqs, irq_base, 0,
633 &regmap_domain_ops, d);
634 else
635 d->domain = irq_domain_add_linear(map->dev->of_node,
636 chip->num_irqs,
637 &regmap_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 Brownf8beab22011-10-28 23:50:49 +0200642 }
643
Valentin Rothberg09cadf62015-02-11 16:37:57 +0100644 ret = request_threaded_irq(irq, NULL, regmap_irq_thread,
645 irq_flags | IRQF_ONESHOT,
Mark Brownf8beab22011-10-28 23:50:49 +0200646 chip->name, d);
647 if (ret != 0) {
Mark Browneed456f2013-03-19 10:45:04 +0000648 dev_err(map->dev, "Failed to request IRQ %d for %s: %d\n",
649 irq, chip->name, ret);
Mark Brown4af8be62012-05-13 10:59:56 +0100650 goto err_domain;
Mark Brownf8beab22011-10-28 23:50:49 +0200651 }
652
Krzysztof Kozlowski72a6a5d2014-03-13 09:06:01 +0100653 *data = d;
654
Mark Brownf8beab22011-10-28 23:50:49 +0200655 return 0;
656
Mark Brown4af8be62012-05-13 10:59:56 +0100657err_domain:
658 /* Should really dispose of the domain but... */
Mark Brownf8beab22011-10-28 23:50:49 +0200659err_alloc:
Laxman Dewangan7a78479f2015-12-22 18:25:26 +0530660 kfree(d->type_buf);
661 kfree(d->type_buf_def);
Mark Browna43fd502012-06-05 14:34:03 +0100662 kfree(d->wake_buf);
Mark Brownf8beab22011-10-28 23:50:49 +0200663 kfree(d->mask_buf_def);
664 kfree(d->mask_buf);
Mark Brownf8beab22011-10-28 23:50:49 +0200665 kfree(d->status_buf);
Mark Browna7440ea2013-01-03 14:27:15 +0000666 kfree(d->status_reg_buf);
Mark Brownf8beab22011-10-28 23:50:49 +0200667 kfree(d);
668 return ret;
669}
670EXPORT_SYMBOL_GPL(regmap_add_irq_chip);
671
672/**
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000673 * regmap_del_irq_chip() - Stop interrupt handling for a regmap IRQ chip
Mark Brownf8beab22011-10-28 23:50:49 +0200674 *
675 * @irq: Primary IRQ for the device
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000676 * @d: &regmap_irq_chip_data allocated by regmap_add_irq_chip()
Laxman Dewangan46189512016-02-09 17:58:22 +0530677 *
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000678 * This function also disposes of all mapped IRQs on the chip.
Mark Brownf8beab22011-10-28 23:50:49 +0200679 */
680void regmap_del_irq_chip(int irq, struct regmap_irq_chip_data *d)
681{
Laxman Dewangan46189512016-02-09 17:58:22 +0530682 unsigned int virq;
683 int hwirq;
684
Mark Brownf8beab22011-10-28 23:50:49 +0200685 if (!d)
686 return;
687
688 free_irq(irq, d);
Laxman Dewangan46189512016-02-09 17:58:22 +0530689
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 Brownb5ab3e52014-01-22 20:25:48 +0000705 irq_domain_remove(d->domain);
Laxman Dewangan7a78479f2015-12-22 18:25:26 +0530706 kfree(d->type_buf);
707 kfree(d->type_buf_def);
Mark Browna43fd502012-06-05 14:34:03 +0100708 kfree(d->wake_buf);
Mark Brownf8beab22011-10-28 23:50:49 +0200709 kfree(d->mask_buf_def);
710 kfree(d->mask_buf);
Mark Browna7440ea2013-01-03 14:27:15 +0000711 kfree(d->status_reg_buf);
Mark Brownf8beab22011-10-28 23:50:49 +0200712 kfree(d->status_buf);
713 kfree(d);
714}
715EXPORT_SYMBOL_GPL(regmap_del_irq_chip);
Mark Brown209a6002011-12-05 16:10:15 +0000716
Laxman Dewangan800c3a02016-02-10 14:29:50 +0530717static 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
724static 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 Keepax2cf8e2d2017-01-12 11:17:39 +0000737 * devm_regmap_add_irq_chip() - Resource manager regmap_add_irq_chip()
Laxman Dewangan800c3a02016-02-10 14:29:50 +0530738 *
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000739 * @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 Dewangan800c3a02016-02-10 14:29:50 +0530742 * @irq_flags: The IRQF_ flags to use for the primary interrupt.
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000743 * @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 Dewangan800c3a02016-02-10 14:29:50 +0530746 *
747 * Returns 0 on success or an errno on failure.
748 *
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000749 * The &regmap_irq_chip_data will be automatically released when the device is
Laxman Dewangan800c3a02016-02-10 14:29:50 +0530750 * unbound.
751 */
752int 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}
777EXPORT_SYMBOL_GPL(devm_regmap_add_irq_chip);
778
779/**
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000780 * devm_regmap_del_irq_chip() - Resource managed regmap_del_irq_chip()
Laxman Dewangan800c3a02016-02-10 14:29:50 +0530781 *
782 * @dev: Device for which which resource was allocated.
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000783 * @irq: Primary IRQ for the device.
784 * @data: &regmap_irq_chip_data allocated by regmap_add_irq_chip().
785 *
786 * A resource managed version of regmap_del_irq_chip().
Laxman Dewangan800c3a02016-02-10 14:29:50 +0530787 */
788void 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}
800EXPORT_SYMBOL_GPL(devm_regmap_del_irq_chip);
801
Mark Brown209a6002011-12-05 16:10:15 +0000802/**
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000803 * regmap_irq_chip_get_base() - Retrieve interrupt base for a regmap IRQ chip
804 *
805 * @data: regmap irq controller to operate on.
Mark Brown209a6002011-12-05 16:10:15 +0000806 *
807 * Useful for drivers to request their own IRQs.
Mark Brown209a6002011-12-05 16:10:15 +0000808 */
809int regmap_irq_chip_get_base(struct regmap_irq_chip_data *data)
810{
Mark Brown4af8be62012-05-13 10:59:56 +0100811 WARN_ON(!data->irq_base);
Mark Brown209a6002011-12-05 16:10:15 +0000812 return data->irq_base;
813}
814EXPORT_SYMBOL_GPL(regmap_irq_chip_get_base);
Mark Brown4af8be62012-05-13 10:59:56 +0100815
816/**
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000817 * 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 Brown4af8be62012-05-13 10:59:56 +0100821 *
822 * Useful for drivers to request their own IRQs.
Mark Brown4af8be62012-05-13 10:59:56 +0100823 */
824int regmap_irq_get_virq(struct regmap_irq_chip_data *data, int irq)
825{
Mark Brownbfd6185d2012-06-05 14:29:36 +0100826 /* Handle holes in the IRQ list */
827 if (!data->chip->irqs[irq].mask)
828 return -EINVAL;
829
Mark Brown4af8be62012-05-13 10:59:56 +0100830 return irq_create_mapping(data->domain, irq);
831}
832EXPORT_SYMBOL_GPL(regmap_irq_get_virq);
Mark Brown90f790d2012-08-20 21:45:05 +0100833
834/**
Charles Keepax2cf8e2d2017-01-12 11:17:39 +0000835 * regmap_irq_get_domain() - Retrieve the irq_domain for the chip
836 *
837 * @data: regmap_irq controller to operate on.
Mark Brown90f790d2012-08-20 21:45:05 +0100838 *
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 Brown90f790d2012-08-20 21:45:05 +0100843 */
844struct 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}
851EXPORT_SYMBOL_GPL(regmap_irq_get_domain);