1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun //
3*4882a593Smuzhiyun // regmap based irq_chip
4*4882a593Smuzhiyun //
5*4882a593Smuzhiyun // Copyright 2011 Wolfson Microelectronics plc
6*4882a593Smuzhiyun //
7*4882a593Smuzhiyun // Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun #include <linux/device.h>
10*4882a593Smuzhiyun #include <linux/export.h>
11*4882a593Smuzhiyun #include <linux/interrupt.h>
12*4882a593Smuzhiyun #include <linux/irq.h>
13*4882a593Smuzhiyun #include <linux/irqdomain.h>
14*4882a593Smuzhiyun #include <linux/pm_runtime.h>
15*4882a593Smuzhiyun #include <linux/regmap.h>
16*4882a593Smuzhiyun #include <linux/slab.h>
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun #include "internal.h"
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun struct regmap_irq_chip_data {
21*4882a593Smuzhiyun struct mutex lock;
22*4882a593Smuzhiyun struct irq_chip irq_chip;
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun struct regmap *map;
25*4882a593Smuzhiyun const struct regmap_irq_chip *chip;
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun int irq_base;
28*4882a593Smuzhiyun struct irq_domain *domain;
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun int irq;
31*4882a593Smuzhiyun int wake_count;
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun void *status_reg_buf;
34*4882a593Smuzhiyun unsigned int *main_status_buf;
35*4882a593Smuzhiyun unsigned int *status_buf;
36*4882a593Smuzhiyun unsigned int *mask_buf;
37*4882a593Smuzhiyun unsigned int *mask_buf_def;
38*4882a593Smuzhiyun unsigned int *wake_buf;
39*4882a593Smuzhiyun unsigned int *type_buf;
40*4882a593Smuzhiyun unsigned int *type_buf_def;
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun unsigned int irq_reg_stride;
43*4882a593Smuzhiyun unsigned int type_reg_stride;
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun bool clear_status:1;
46*4882a593Smuzhiyun };
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun static inline const
irq_to_regmap_irq(struct regmap_irq_chip_data * data,int irq)49*4882a593Smuzhiyun struct regmap_irq *irq_to_regmap_irq(struct regmap_irq_chip_data *data,
50*4882a593Smuzhiyun int irq)
51*4882a593Smuzhiyun {
52*4882a593Smuzhiyun return &data->chip->irqs[irq];
53*4882a593Smuzhiyun }
54*4882a593Smuzhiyun
regmap_irq_lock(struct irq_data * data)55*4882a593Smuzhiyun static void regmap_irq_lock(struct irq_data *data)
56*4882a593Smuzhiyun {
57*4882a593Smuzhiyun struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data);
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun mutex_lock(&d->lock);
60*4882a593Smuzhiyun }
61*4882a593Smuzhiyun
regmap_irq_update_bits(struct regmap_irq_chip_data * d,unsigned int reg,unsigned int mask,unsigned int val)62*4882a593Smuzhiyun static int regmap_irq_update_bits(struct regmap_irq_chip_data *d,
63*4882a593Smuzhiyun unsigned int reg, unsigned int mask,
64*4882a593Smuzhiyun unsigned int val)
65*4882a593Smuzhiyun {
66*4882a593Smuzhiyun if (d->chip->mask_writeonly)
67*4882a593Smuzhiyun return regmap_write_bits(d->map, reg, mask, val);
68*4882a593Smuzhiyun else
69*4882a593Smuzhiyun return regmap_update_bits(d->map, reg, mask, val);
70*4882a593Smuzhiyun }
71*4882a593Smuzhiyun
regmap_irq_sync_unlock(struct irq_data * data)72*4882a593Smuzhiyun static void regmap_irq_sync_unlock(struct irq_data *data)
73*4882a593Smuzhiyun {
74*4882a593Smuzhiyun struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data);
75*4882a593Smuzhiyun struct regmap *map = d->map;
76*4882a593Smuzhiyun int i, ret;
77*4882a593Smuzhiyun u32 reg;
78*4882a593Smuzhiyun u32 unmask_offset;
79*4882a593Smuzhiyun u32 val;
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun if (d->chip->runtime_pm) {
82*4882a593Smuzhiyun ret = pm_runtime_get_sync(map->dev);
83*4882a593Smuzhiyun if (ret < 0)
84*4882a593Smuzhiyun dev_err(map->dev, "IRQ sync failed to resume: %d\n",
85*4882a593Smuzhiyun ret);
86*4882a593Smuzhiyun }
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun if (d->clear_status) {
89*4882a593Smuzhiyun for (i = 0; i < d->chip->num_regs; i++) {
90*4882a593Smuzhiyun reg = d->chip->status_base +
91*4882a593Smuzhiyun (i * map->reg_stride * d->irq_reg_stride);
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun ret = regmap_read(map, reg, &val);
94*4882a593Smuzhiyun if (ret)
95*4882a593Smuzhiyun dev_err(d->map->dev,
96*4882a593Smuzhiyun "Failed to clear the interrupt status bits\n");
97*4882a593Smuzhiyun }
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun d->clear_status = false;
100*4882a593Smuzhiyun }
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun /*
103*4882a593Smuzhiyun * If there's been a change in the mask write it back to the
104*4882a593Smuzhiyun * hardware. We rely on the use of the regmap core cache to
105*4882a593Smuzhiyun * suppress pointless writes.
106*4882a593Smuzhiyun */
107*4882a593Smuzhiyun for (i = 0; i < d->chip->num_regs; i++) {
108*4882a593Smuzhiyun if (!d->chip->mask_base)
109*4882a593Smuzhiyun continue;
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun reg = d->chip->mask_base +
112*4882a593Smuzhiyun (i * map->reg_stride * d->irq_reg_stride);
113*4882a593Smuzhiyun if (d->chip->mask_invert) {
114*4882a593Smuzhiyun ret = regmap_irq_update_bits(d, reg,
115*4882a593Smuzhiyun d->mask_buf_def[i], ~d->mask_buf[i]);
116*4882a593Smuzhiyun } else if (d->chip->unmask_base) {
117*4882a593Smuzhiyun /* set mask with mask_base register */
118*4882a593Smuzhiyun ret = regmap_irq_update_bits(d, reg,
119*4882a593Smuzhiyun d->mask_buf_def[i], ~d->mask_buf[i]);
120*4882a593Smuzhiyun if (ret < 0)
121*4882a593Smuzhiyun dev_err(d->map->dev,
122*4882a593Smuzhiyun "Failed to sync unmasks in %x\n",
123*4882a593Smuzhiyun reg);
124*4882a593Smuzhiyun unmask_offset = d->chip->unmask_base -
125*4882a593Smuzhiyun d->chip->mask_base;
126*4882a593Smuzhiyun /* clear mask with unmask_base register */
127*4882a593Smuzhiyun ret = regmap_irq_update_bits(d,
128*4882a593Smuzhiyun reg + unmask_offset,
129*4882a593Smuzhiyun d->mask_buf_def[i],
130*4882a593Smuzhiyun d->mask_buf[i]);
131*4882a593Smuzhiyun } else {
132*4882a593Smuzhiyun ret = regmap_irq_update_bits(d, reg,
133*4882a593Smuzhiyun d->mask_buf_def[i], d->mask_buf[i]);
134*4882a593Smuzhiyun }
135*4882a593Smuzhiyun if (ret != 0)
136*4882a593Smuzhiyun dev_err(d->map->dev, "Failed to sync masks in %x\n",
137*4882a593Smuzhiyun reg);
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun reg = d->chip->wake_base +
140*4882a593Smuzhiyun (i * map->reg_stride * d->irq_reg_stride);
141*4882a593Smuzhiyun if (d->wake_buf) {
142*4882a593Smuzhiyun if (d->chip->wake_invert)
143*4882a593Smuzhiyun ret = regmap_irq_update_bits(d, reg,
144*4882a593Smuzhiyun d->mask_buf_def[i],
145*4882a593Smuzhiyun ~d->wake_buf[i]);
146*4882a593Smuzhiyun else
147*4882a593Smuzhiyun ret = regmap_irq_update_bits(d, reg,
148*4882a593Smuzhiyun d->mask_buf_def[i],
149*4882a593Smuzhiyun d->wake_buf[i]);
150*4882a593Smuzhiyun if (ret != 0)
151*4882a593Smuzhiyun dev_err(d->map->dev,
152*4882a593Smuzhiyun "Failed to sync wakes in %x: %d\n",
153*4882a593Smuzhiyun reg, ret);
154*4882a593Smuzhiyun }
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun if (!d->chip->init_ack_masked)
157*4882a593Smuzhiyun continue;
158*4882a593Smuzhiyun /*
159*4882a593Smuzhiyun * Ack all the masked interrupts unconditionally,
160*4882a593Smuzhiyun * OR if there is masked interrupt which hasn't been Acked,
161*4882a593Smuzhiyun * it'll be ignored in irq handler, then may introduce irq storm
162*4882a593Smuzhiyun */
163*4882a593Smuzhiyun if (d->mask_buf[i] && (d->chip->ack_base || d->chip->use_ack)) {
164*4882a593Smuzhiyun reg = d->chip->ack_base +
165*4882a593Smuzhiyun (i * map->reg_stride * d->irq_reg_stride);
166*4882a593Smuzhiyun /* some chips ack by write 0 */
167*4882a593Smuzhiyun if (d->chip->ack_invert)
168*4882a593Smuzhiyun ret = regmap_write(map, reg, ~d->mask_buf[i]);
169*4882a593Smuzhiyun else
170*4882a593Smuzhiyun ret = regmap_write(map, reg, d->mask_buf[i]);
171*4882a593Smuzhiyun if (d->chip->clear_ack) {
172*4882a593Smuzhiyun if (d->chip->ack_invert && !ret)
173*4882a593Smuzhiyun ret = regmap_write(map, reg, UINT_MAX);
174*4882a593Smuzhiyun else if (!ret)
175*4882a593Smuzhiyun ret = regmap_write(map, reg, 0);
176*4882a593Smuzhiyun }
177*4882a593Smuzhiyun if (ret != 0)
178*4882a593Smuzhiyun dev_err(d->map->dev, "Failed to ack 0x%x: %d\n",
179*4882a593Smuzhiyun reg, ret);
180*4882a593Smuzhiyun }
181*4882a593Smuzhiyun }
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun /* Don't update the type bits if we're using mask bits for irq type. */
184*4882a593Smuzhiyun if (!d->chip->type_in_mask) {
185*4882a593Smuzhiyun for (i = 0; i < d->chip->num_type_reg; i++) {
186*4882a593Smuzhiyun if (!d->type_buf_def[i])
187*4882a593Smuzhiyun continue;
188*4882a593Smuzhiyun reg = d->chip->type_base +
189*4882a593Smuzhiyun (i * map->reg_stride * d->type_reg_stride);
190*4882a593Smuzhiyun if (d->chip->type_invert)
191*4882a593Smuzhiyun ret = regmap_irq_update_bits(d, reg,
192*4882a593Smuzhiyun d->type_buf_def[i], ~d->type_buf[i]);
193*4882a593Smuzhiyun else
194*4882a593Smuzhiyun ret = regmap_irq_update_bits(d, reg,
195*4882a593Smuzhiyun d->type_buf_def[i], d->type_buf[i]);
196*4882a593Smuzhiyun if (ret != 0)
197*4882a593Smuzhiyun dev_err(d->map->dev, "Failed to sync type in %x\n",
198*4882a593Smuzhiyun reg);
199*4882a593Smuzhiyun }
200*4882a593Smuzhiyun }
201*4882a593Smuzhiyun
202*4882a593Smuzhiyun if (d->chip->runtime_pm)
203*4882a593Smuzhiyun pm_runtime_put(map->dev);
204*4882a593Smuzhiyun
205*4882a593Smuzhiyun /* If we've changed our wakeup count propagate it to the parent */
206*4882a593Smuzhiyun if (d->wake_count < 0)
207*4882a593Smuzhiyun for (i = d->wake_count; i < 0; i++)
208*4882a593Smuzhiyun irq_set_irq_wake(d->irq, 0);
209*4882a593Smuzhiyun else if (d->wake_count > 0)
210*4882a593Smuzhiyun for (i = 0; i < d->wake_count; i++)
211*4882a593Smuzhiyun irq_set_irq_wake(d->irq, 1);
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun d->wake_count = 0;
214*4882a593Smuzhiyun
215*4882a593Smuzhiyun mutex_unlock(&d->lock);
216*4882a593Smuzhiyun }
217*4882a593Smuzhiyun
regmap_irq_enable(struct irq_data * data)218*4882a593Smuzhiyun static void regmap_irq_enable(struct irq_data *data)
219*4882a593Smuzhiyun {
220*4882a593Smuzhiyun struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data);
221*4882a593Smuzhiyun struct regmap *map = d->map;
222*4882a593Smuzhiyun const struct regmap_irq *irq_data = irq_to_regmap_irq(d, data->hwirq);
223*4882a593Smuzhiyun unsigned int reg = irq_data->reg_offset / map->reg_stride;
224*4882a593Smuzhiyun unsigned int mask, type;
225*4882a593Smuzhiyun
226*4882a593Smuzhiyun type = irq_data->type.type_falling_val | irq_data->type.type_rising_val;
227*4882a593Smuzhiyun
228*4882a593Smuzhiyun /*
229*4882a593Smuzhiyun * The type_in_mask flag means that the underlying hardware uses
230*4882a593Smuzhiyun * separate mask bits for rising and falling edge interrupts, but
231*4882a593Smuzhiyun * we want to make them into a single virtual interrupt with
232*4882a593Smuzhiyun * configurable edge.
233*4882a593Smuzhiyun *
234*4882a593Smuzhiyun * If the interrupt we're enabling defines the falling or rising
235*4882a593Smuzhiyun * masks then instead of using the regular mask bits for this
236*4882a593Smuzhiyun * interrupt, use the value previously written to the type buffer
237*4882a593Smuzhiyun * at the corresponding offset in regmap_irq_set_type().
238*4882a593Smuzhiyun */
239*4882a593Smuzhiyun if (d->chip->type_in_mask && type)
240*4882a593Smuzhiyun mask = d->type_buf[reg] & irq_data->mask;
241*4882a593Smuzhiyun else
242*4882a593Smuzhiyun mask = irq_data->mask;
243*4882a593Smuzhiyun
244*4882a593Smuzhiyun if (d->chip->clear_on_unmask)
245*4882a593Smuzhiyun d->clear_status = true;
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun d->mask_buf[reg] &= ~mask;
248*4882a593Smuzhiyun }
249*4882a593Smuzhiyun
regmap_irq_disable(struct irq_data * data)250*4882a593Smuzhiyun static void regmap_irq_disable(struct irq_data *data)
251*4882a593Smuzhiyun {
252*4882a593Smuzhiyun struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data);
253*4882a593Smuzhiyun struct regmap *map = d->map;
254*4882a593Smuzhiyun const struct regmap_irq *irq_data = irq_to_regmap_irq(d, data->hwirq);
255*4882a593Smuzhiyun
256*4882a593Smuzhiyun d->mask_buf[irq_data->reg_offset / map->reg_stride] |= irq_data->mask;
257*4882a593Smuzhiyun }
258*4882a593Smuzhiyun
regmap_irq_set_type(struct irq_data * data,unsigned int type)259*4882a593Smuzhiyun static int regmap_irq_set_type(struct irq_data *data, unsigned int type)
260*4882a593Smuzhiyun {
261*4882a593Smuzhiyun struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data);
262*4882a593Smuzhiyun struct regmap *map = d->map;
263*4882a593Smuzhiyun const struct regmap_irq *irq_data = irq_to_regmap_irq(d, data->hwirq);
264*4882a593Smuzhiyun int reg;
265*4882a593Smuzhiyun const struct regmap_irq_type *t = &irq_data->type;
266*4882a593Smuzhiyun
267*4882a593Smuzhiyun if ((t->types_supported & type) != type)
268*4882a593Smuzhiyun return 0;
269*4882a593Smuzhiyun
270*4882a593Smuzhiyun reg = t->type_reg_offset / map->reg_stride;
271*4882a593Smuzhiyun
272*4882a593Smuzhiyun if (t->type_reg_mask)
273*4882a593Smuzhiyun d->type_buf[reg] &= ~t->type_reg_mask;
274*4882a593Smuzhiyun else
275*4882a593Smuzhiyun d->type_buf[reg] &= ~(t->type_falling_val |
276*4882a593Smuzhiyun t->type_rising_val |
277*4882a593Smuzhiyun t->type_level_low_val |
278*4882a593Smuzhiyun t->type_level_high_val);
279*4882a593Smuzhiyun switch (type) {
280*4882a593Smuzhiyun case IRQ_TYPE_EDGE_FALLING:
281*4882a593Smuzhiyun d->type_buf[reg] |= t->type_falling_val;
282*4882a593Smuzhiyun break;
283*4882a593Smuzhiyun
284*4882a593Smuzhiyun case IRQ_TYPE_EDGE_RISING:
285*4882a593Smuzhiyun d->type_buf[reg] |= t->type_rising_val;
286*4882a593Smuzhiyun break;
287*4882a593Smuzhiyun
288*4882a593Smuzhiyun case IRQ_TYPE_EDGE_BOTH:
289*4882a593Smuzhiyun d->type_buf[reg] |= (t->type_falling_val |
290*4882a593Smuzhiyun t->type_rising_val);
291*4882a593Smuzhiyun break;
292*4882a593Smuzhiyun
293*4882a593Smuzhiyun case IRQ_TYPE_LEVEL_HIGH:
294*4882a593Smuzhiyun d->type_buf[reg] |= t->type_level_high_val;
295*4882a593Smuzhiyun break;
296*4882a593Smuzhiyun
297*4882a593Smuzhiyun case IRQ_TYPE_LEVEL_LOW:
298*4882a593Smuzhiyun d->type_buf[reg] |= t->type_level_low_val;
299*4882a593Smuzhiyun break;
300*4882a593Smuzhiyun default:
301*4882a593Smuzhiyun return -EINVAL;
302*4882a593Smuzhiyun }
303*4882a593Smuzhiyun return 0;
304*4882a593Smuzhiyun }
305*4882a593Smuzhiyun
regmap_irq_set_wake(struct irq_data * data,unsigned int on)306*4882a593Smuzhiyun static int regmap_irq_set_wake(struct irq_data *data, unsigned int on)
307*4882a593Smuzhiyun {
308*4882a593Smuzhiyun struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data);
309*4882a593Smuzhiyun struct regmap *map = d->map;
310*4882a593Smuzhiyun const struct regmap_irq *irq_data = irq_to_regmap_irq(d, data->hwirq);
311*4882a593Smuzhiyun
312*4882a593Smuzhiyun if (on) {
313*4882a593Smuzhiyun if (d->wake_buf)
314*4882a593Smuzhiyun d->wake_buf[irq_data->reg_offset / map->reg_stride]
315*4882a593Smuzhiyun &= ~irq_data->mask;
316*4882a593Smuzhiyun d->wake_count++;
317*4882a593Smuzhiyun } else {
318*4882a593Smuzhiyun if (d->wake_buf)
319*4882a593Smuzhiyun d->wake_buf[irq_data->reg_offset / map->reg_stride]
320*4882a593Smuzhiyun |= irq_data->mask;
321*4882a593Smuzhiyun d->wake_count--;
322*4882a593Smuzhiyun }
323*4882a593Smuzhiyun
324*4882a593Smuzhiyun return 0;
325*4882a593Smuzhiyun }
326*4882a593Smuzhiyun
327*4882a593Smuzhiyun static const struct irq_chip regmap_irq_chip = {
328*4882a593Smuzhiyun .irq_bus_lock = regmap_irq_lock,
329*4882a593Smuzhiyun .irq_bus_sync_unlock = regmap_irq_sync_unlock,
330*4882a593Smuzhiyun .irq_disable = regmap_irq_disable,
331*4882a593Smuzhiyun .irq_enable = regmap_irq_enable,
332*4882a593Smuzhiyun .irq_set_type = regmap_irq_set_type,
333*4882a593Smuzhiyun .irq_set_wake = regmap_irq_set_wake,
334*4882a593Smuzhiyun };
335*4882a593Smuzhiyun
read_sub_irq_data(struct regmap_irq_chip_data * data,unsigned int b)336*4882a593Smuzhiyun static inline int read_sub_irq_data(struct regmap_irq_chip_data *data,
337*4882a593Smuzhiyun unsigned int b)
338*4882a593Smuzhiyun {
339*4882a593Smuzhiyun const struct regmap_irq_chip *chip = data->chip;
340*4882a593Smuzhiyun struct regmap *map = data->map;
341*4882a593Smuzhiyun struct regmap_irq_sub_irq_map *subreg;
342*4882a593Smuzhiyun int i, ret = 0;
343*4882a593Smuzhiyun
344*4882a593Smuzhiyun if (!chip->sub_reg_offsets) {
345*4882a593Smuzhiyun /* Assume linear mapping */
346*4882a593Smuzhiyun ret = regmap_read(map, chip->status_base +
347*4882a593Smuzhiyun (b * map->reg_stride * data->irq_reg_stride),
348*4882a593Smuzhiyun &data->status_buf[b]);
349*4882a593Smuzhiyun } else {
350*4882a593Smuzhiyun subreg = &chip->sub_reg_offsets[b];
351*4882a593Smuzhiyun for (i = 0; i < subreg->num_regs; i++) {
352*4882a593Smuzhiyun unsigned int offset = subreg->offset[i];
353*4882a593Smuzhiyun
354*4882a593Smuzhiyun ret = regmap_read(map, chip->status_base + offset,
355*4882a593Smuzhiyun &data->status_buf[offset]);
356*4882a593Smuzhiyun if (ret)
357*4882a593Smuzhiyun break;
358*4882a593Smuzhiyun }
359*4882a593Smuzhiyun }
360*4882a593Smuzhiyun return ret;
361*4882a593Smuzhiyun }
362*4882a593Smuzhiyun
regmap_irq_thread(int irq,void * d)363*4882a593Smuzhiyun static irqreturn_t regmap_irq_thread(int irq, void *d)
364*4882a593Smuzhiyun {
365*4882a593Smuzhiyun struct regmap_irq_chip_data *data = d;
366*4882a593Smuzhiyun const struct regmap_irq_chip *chip = data->chip;
367*4882a593Smuzhiyun struct regmap *map = data->map;
368*4882a593Smuzhiyun int ret, i;
369*4882a593Smuzhiyun bool handled = false;
370*4882a593Smuzhiyun u32 reg;
371*4882a593Smuzhiyun
372*4882a593Smuzhiyun if (chip->handle_pre_irq)
373*4882a593Smuzhiyun chip->handle_pre_irq(chip->irq_drv_data);
374*4882a593Smuzhiyun
375*4882a593Smuzhiyun if (chip->runtime_pm) {
376*4882a593Smuzhiyun ret = pm_runtime_get_sync(map->dev);
377*4882a593Smuzhiyun if (ret < 0) {
378*4882a593Smuzhiyun dev_err(map->dev, "IRQ thread failed to resume: %d\n",
379*4882a593Smuzhiyun ret);
380*4882a593Smuzhiyun goto exit;
381*4882a593Smuzhiyun }
382*4882a593Smuzhiyun }
383*4882a593Smuzhiyun
384*4882a593Smuzhiyun /*
385*4882a593Smuzhiyun * Read only registers with active IRQs if the chip has 'main status
386*4882a593Smuzhiyun * register'. Else read in the statuses, using a single bulk read if
387*4882a593Smuzhiyun * possible in order to reduce the I/O overheads.
388*4882a593Smuzhiyun */
389*4882a593Smuzhiyun
390*4882a593Smuzhiyun if (chip->num_main_regs) {
391*4882a593Smuzhiyun unsigned int max_main_bits;
392*4882a593Smuzhiyun unsigned long size;
393*4882a593Smuzhiyun
394*4882a593Smuzhiyun size = chip->num_regs * sizeof(unsigned int);
395*4882a593Smuzhiyun
396*4882a593Smuzhiyun max_main_bits = (chip->num_main_status_bits) ?
397*4882a593Smuzhiyun chip->num_main_status_bits : chip->num_regs;
398*4882a593Smuzhiyun /* Clear the status buf as we don't read all status regs */
399*4882a593Smuzhiyun memset(data->status_buf, 0, size);
400*4882a593Smuzhiyun
401*4882a593Smuzhiyun /* We could support bulk read for main status registers
402*4882a593Smuzhiyun * but I don't expect to see devices with really many main
403*4882a593Smuzhiyun * status registers so let's only support single reads for the
404*4882a593Smuzhiyun * sake of simplicity. and add bulk reads only if needed
405*4882a593Smuzhiyun */
406*4882a593Smuzhiyun for (i = 0; i < chip->num_main_regs; i++) {
407*4882a593Smuzhiyun ret = regmap_read(map, chip->main_status +
408*4882a593Smuzhiyun (i * map->reg_stride
409*4882a593Smuzhiyun * data->irq_reg_stride),
410*4882a593Smuzhiyun &data->main_status_buf[i]);
411*4882a593Smuzhiyun if (ret) {
412*4882a593Smuzhiyun dev_err(map->dev,
413*4882a593Smuzhiyun "Failed to read IRQ status %d\n",
414*4882a593Smuzhiyun ret);
415*4882a593Smuzhiyun goto exit;
416*4882a593Smuzhiyun }
417*4882a593Smuzhiyun }
418*4882a593Smuzhiyun
419*4882a593Smuzhiyun /* Read sub registers with active IRQs */
420*4882a593Smuzhiyun for (i = 0; i < chip->num_main_regs; i++) {
421*4882a593Smuzhiyun unsigned int b;
422*4882a593Smuzhiyun const unsigned long mreg = data->main_status_buf[i];
423*4882a593Smuzhiyun
424*4882a593Smuzhiyun for_each_set_bit(b, &mreg, map->format.val_bytes * 8) {
425*4882a593Smuzhiyun if (i * map->format.val_bytes * 8 + b >
426*4882a593Smuzhiyun max_main_bits)
427*4882a593Smuzhiyun break;
428*4882a593Smuzhiyun ret = read_sub_irq_data(data, b);
429*4882a593Smuzhiyun
430*4882a593Smuzhiyun if (ret != 0) {
431*4882a593Smuzhiyun dev_err(map->dev,
432*4882a593Smuzhiyun "Failed to read IRQ status %d\n",
433*4882a593Smuzhiyun ret);
434*4882a593Smuzhiyun goto exit;
435*4882a593Smuzhiyun }
436*4882a593Smuzhiyun }
437*4882a593Smuzhiyun
438*4882a593Smuzhiyun }
439*4882a593Smuzhiyun } else if (!map->use_single_read && map->reg_stride == 1 &&
440*4882a593Smuzhiyun data->irq_reg_stride == 1) {
441*4882a593Smuzhiyun
442*4882a593Smuzhiyun u8 *buf8 = data->status_reg_buf;
443*4882a593Smuzhiyun u16 *buf16 = data->status_reg_buf;
444*4882a593Smuzhiyun u32 *buf32 = data->status_reg_buf;
445*4882a593Smuzhiyun
446*4882a593Smuzhiyun BUG_ON(!data->status_reg_buf);
447*4882a593Smuzhiyun
448*4882a593Smuzhiyun ret = regmap_bulk_read(map, chip->status_base,
449*4882a593Smuzhiyun data->status_reg_buf,
450*4882a593Smuzhiyun chip->num_regs);
451*4882a593Smuzhiyun if (ret != 0) {
452*4882a593Smuzhiyun dev_err(map->dev, "Failed to read IRQ status: %d\n",
453*4882a593Smuzhiyun ret);
454*4882a593Smuzhiyun goto exit;
455*4882a593Smuzhiyun }
456*4882a593Smuzhiyun
457*4882a593Smuzhiyun for (i = 0; i < data->chip->num_regs; i++) {
458*4882a593Smuzhiyun switch (map->format.val_bytes) {
459*4882a593Smuzhiyun case 1:
460*4882a593Smuzhiyun data->status_buf[i] = buf8[i];
461*4882a593Smuzhiyun break;
462*4882a593Smuzhiyun case 2:
463*4882a593Smuzhiyun data->status_buf[i] = buf16[i];
464*4882a593Smuzhiyun break;
465*4882a593Smuzhiyun case 4:
466*4882a593Smuzhiyun data->status_buf[i] = buf32[i];
467*4882a593Smuzhiyun break;
468*4882a593Smuzhiyun default:
469*4882a593Smuzhiyun BUG();
470*4882a593Smuzhiyun goto exit;
471*4882a593Smuzhiyun }
472*4882a593Smuzhiyun }
473*4882a593Smuzhiyun
474*4882a593Smuzhiyun } else {
475*4882a593Smuzhiyun for (i = 0; i < data->chip->num_regs; i++) {
476*4882a593Smuzhiyun ret = regmap_read(map, chip->status_base +
477*4882a593Smuzhiyun (i * map->reg_stride
478*4882a593Smuzhiyun * data->irq_reg_stride),
479*4882a593Smuzhiyun &data->status_buf[i]);
480*4882a593Smuzhiyun
481*4882a593Smuzhiyun if (ret != 0) {
482*4882a593Smuzhiyun dev_err(map->dev,
483*4882a593Smuzhiyun "Failed to read IRQ status: %d\n",
484*4882a593Smuzhiyun ret);
485*4882a593Smuzhiyun goto exit;
486*4882a593Smuzhiyun }
487*4882a593Smuzhiyun }
488*4882a593Smuzhiyun }
489*4882a593Smuzhiyun
490*4882a593Smuzhiyun /*
491*4882a593Smuzhiyun * Ignore masked IRQs and ack if we need to; we ack early so
492*4882a593Smuzhiyun * there is no race between handling and acknowleding the
493*4882a593Smuzhiyun * interrupt. We assume that typically few of the interrupts
494*4882a593Smuzhiyun * will fire simultaneously so don't worry about overhead from
495*4882a593Smuzhiyun * doing a write per register.
496*4882a593Smuzhiyun */
497*4882a593Smuzhiyun for (i = 0; i < data->chip->num_regs; i++) {
498*4882a593Smuzhiyun data->status_buf[i] &= ~data->mask_buf[i];
499*4882a593Smuzhiyun
500*4882a593Smuzhiyun if (data->status_buf[i] && (chip->ack_base || chip->use_ack)) {
501*4882a593Smuzhiyun reg = chip->ack_base +
502*4882a593Smuzhiyun (i * map->reg_stride * data->irq_reg_stride);
503*4882a593Smuzhiyun if (chip->ack_invert)
504*4882a593Smuzhiyun ret = regmap_write(map, reg,
505*4882a593Smuzhiyun ~data->status_buf[i]);
506*4882a593Smuzhiyun else
507*4882a593Smuzhiyun ret = regmap_write(map, reg,
508*4882a593Smuzhiyun data->status_buf[i]);
509*4882a593Smuzhiyun if (chip->clear_ack) {
510*4882a593Smuzhiyun if (chip->ack_invert && !ret)
511*4882a593Smuzhiyun ret = regmap_write(map, reg, UINT_MAX);
512*4882a593Smuzhiyun else if (!ret)
513*4882a593Smuzhiyun ret = regmap_write(map, reg, 0);
514*4882a593Smuzhiyun }
515*4882a593Smuzhiyun if (ret != 0)
516*4882a593Smuzhiyun dev_err(map->dev, "Failed to ack 0x%x: %d\n",
517*4882a593Smuzhiyun reg, ret);
518*4882a593Smuzhiyun }
519*4882a593Smuzhiyun }
520*4882a593Smuzhiyun
521*4882a593Smuzhiyun for (i = 0; i < chip->num_irqs; i++) {
522*4882a593Smuzhiyun if (data->status_buf[chip->irqs[i].reg_offset /
523*4882a593Smuzhiyun map->reg_stride] & chip->irqs[i].mask) {
524*4882a593Smuzhiyun handle_nested_irq(irq_find_mapping(data->domain, i));
525*4882a593Smuzhiyun handled = true;
526*4882a593Smuzhiyun }
527*4882a593Smuzhiyun }
528*4882a593Smuzhiyun
529*4882a593Smuzhiyun exit:
530*4882a593Smuzhiyun if (chip->runtime_pm)
531*4882a593Smuzhiyun pm_runtime_put(map->dev);
532*4882a593Smuzhiyun
533*4882a593Smuzhiyun if (chip->handle_post_irq)
534*4882a593Smuzhiyun chip->handle_post_irq(chip->irq_drv_data);
535*4882a593Smuzhiyun
536*4882a593Smuzhiyun if (handled)
537*4882a593Smuzhiyun return IRQ_HANDLED;
538*4882a593Smuzhiyun else
539*4882a593Smuzhiyun return IRQ_NONE;
540*4882a593Smuzhiyun }
541*4882a593Smuzhiyun
regmap_irq_map(struct irq_domain * h,unsigned int virq,irq_hw_number_t hw)542*4882a593Smuzhiyun static int regmap_irq_map(struct irq_domain *h, unsigned int virq,
543*4882a593Smuzhiyun irq_hw_number_t hw)
544*4882a593Smuzhiyun {
545*4882a593Smuzhiyun struct regmap_irq_chip_data *data = h->host_data;
546*4882a593Smuzhiyun
547*4882a593Smuzhiyun irq_set_chip_data(virq, data);
548*4882a593Smuzhiyun irq_set_chip(virq, &data->irq_chip);
549*4882a593Smuzhiyun irq_set_nested_thread(virq, 1);
550*4882a593Smuzhiyun irq_set_parent(virq, data->irq);
551*4882a593Smuzhiyun irq_set_noprobe(virq);
552*4882a593Smuzhiyun
553*4882a593Smuzhiyun return 0;
554*4882a593Smuzhiyun }
555*4882a593Smuzhiyun
556*4882a593Smuzhiyun static const struct irq_domain_ops regmap_domain_ops = {
557*4882a593Smuzhiyun .map = regmap_irq_map,
558*4882a593Smuzhiyun .xlate = irq_domain_xlate_onetwocell,
559*4882a593Smuzhiyun };
560*4882a593Smuzhiyun
561*4882a593Smuzhiyun /**
562*4882a593Smuzhiyun * regmap_add_irq_chip_fwnode() - Use standard regmap IRQ controller handling
563*4882a593Smuzhiyun *
564*4882a593Smuzhiyun * @fwnode: The firmware node where the IRQ domain should be added to.
565*4882a593Smuzhiyun * @map: The regmap for the device.
566*4882a593Smuzhiyun * @irq: The IRQ the device uses to signal interrupts.
567*4882a593Smuzhiyun * @irq_flags: The IRQF_ flags to use for the primary interrupt.
568*4882a593Smuzhiyun * @irq_base: Allocate at specific IRQ number if irq_base > 0.
569*4882a593Smuzhiyun * @chip: Configuration for the interrupt controller.
570*4882a593Smuzhiyun * @data: Runtime data structure for the controller, allocated on success.
571*4882a593Smuzhiyun *
572*4882a593Smuzhiyun * Returns 0 on success or an errno on failure.
573*4882a593Smuzhiyun *
574*4882a593Smuzhiyun * In order for this to be efficient the chip really should use a
575*4882a593Smuzhiyun * register cache. The chip driver is responsible for restoring the
576*4882a593Smuzhiyun * register values used by the IRQ controller over suspend and resume.
577*4882a593Smuzhiyun */
regmap_add_irq_chip_fwnode(struct fwnode_handle * fwnode,struct regmap * map,int irq,int irq_flags,int irq_base,const struct regmap_irq_chip * chip,struct regmap_irq_chip_data ** data)578*4882a593Smuzhiyun int regmap_add_irq_chip_fwnode(struct fwnode_handle *fwnode,
579*4882a593Smuzhiyun struct regmap *map, int irq,
580*4882a593Smuzhiyun int irq_flags, int irq_base,
581*4882a593Smuzhiyun const struct regmap_irq_chip *chip,
582*4882a593Smuzhiyun struct regmap_irq_chip_data **data)
583*4882a593Smuzhiyun {
584*4882a593Smuzhiyun struct regmap_irq_chip_data *d;
585*4882a593Smuzhiyun int i;
586*4882a593Smuzhiyun int ret = -ENOMEM;
587*4882a593Smuzhiyun int num_type_reg;
588*4882a593Smuzhiyun u32 reg;
589*4882a593Smuzhiyun u32 unmask_offset;
590*4882a593Smuzhiyun
591*4882a593Smuzhiyun if (chip->num_regs <= 0)
592*4882a593Smuzhiyun return -EINVAL;
593*4882a593Smuzhiyun
594*4882a593Smuzhiyun if (chip->clear_on_unmask && (chip->ack_base || chip->use_ack))
595*4882a593Smuzhiyun return -EINVAL;
596*4882a593Smuzhiyun
597*4882a593Smuzhiyun for (i = 0; i < chip->num_irqs; i++) {
598*4882a593Smuzhiyun if (chip->irqs[i].reg_offset % map->reg_stride)
599*4882a593Smuzhiyun return -EINVAL;
600*4882a593Smuzhiyun if (chip->irqs[i].reg_offset / map->reg_stride >=
601*4882a593Smuzhiyun chip->num_regs)
602*4882a593Smuzhiyun return -EINVAL;
603*4882a593Smuzhiyun }
604*4882a593Smuzhiyun
605*4882a593Smuzhiyun if (irq_base) {
606*4882a593Smuzhiyun irq_base = irq_alloc_descs(irq_base, 0, chip->num_irqs, 0);
607*4882a593Smuzhiyun if (irq_base < 0) {
608*4882a593Smuzhiyun dev_warn(map->dev, "Failed to allocate IRQs: %d\n",
609*4882a593Smuzhiyun irq_base);
610*4882a593Smuzhiyun return irq_base;
611*4882a593Smuzhiyun }
612*4882a593Smuzhiyun }
613*4882a593Smuzhiyun
614*4882a593Smuzhiyun d = kzalloc(sizeof(*d), GFP_KERNEL);
615*4882a593Smuzhiyun if (!d)
616*4882a593Smuzhiyun return -ENOMEM;
617*4882a593Smuzhiyun
618*4882a593Smuzhiyun if (chip->num_main_regs) {
619*4882a593Smuzhiyun d->main_status_buf = kcalloc(chip->num_main_regs,
620*4882a593Smuzhiyun sizeof(unsigned int),
621*4882a593Smuzhiyun GFP_KERNEL);
622*4882a593Smuzhiyun
623*4882a593Smuzhiyun if (!d->main_status_buf)
624*4882a593Smuzhiyun goto err_alloc;
625*4882a593Smuzhiyun }
626*4882a593Smuzhiyun
627*4882a593Smuzhiyun d->status_buf = kcalloc(chip->num_regs, sizeof(unsigned int),
628*4882a593Smuzhiyun GFP_KERNEL);
629*4882a593Smuzhiyun if (!d->status_buf)
630*4882a593Smuzhiyun goto err_alloc;
631*4882a593Smuzhiyun
632*4882a593Smuzhiyun d->mask_buf = kcalloc(chip->num_regs, sizeof(unsigned int),
633*4882a593Smuzhiyun GFP_KERNEL);
634*4882a593Smuzhiyun if (!d->mask_buf)
635*4882a593Smuzhiyun goto err_alloc;
636*4882a593Smuzhiyun
637*4882a593Smuzhiyun d->mask_buf_def = kcalloc(chip->num_regs, sizeof(unsigned int),
638*4882a593Smuzhiyun GFP_KERNEL);
639*4882a593Smuzhiyun if (!d->mask_buf_def)
640*4882a593Smuzhiyun goto err_alloc;
641*4882a593Smuzhiyun
642*4882a593Smuzhiyun if (chip->wake_base) {
643*4882a593Smuzhiyun d->wake_buf = kcalloc(chip->num_regs, sizeof(unsigned int),
644*4882a593Smuzhiyun GFP_KERNEL);
645*4882a593Smuzhiyun if (!d->wake_buf)
646*4882a593Smuzhiyun goto err_alloc;
647*4882a593Smuzhiyun }
648*4882a593Smuzhiyun
649*4882a593Smuzhiyun num_type_reg = chip->type_in_mask ? chip->num_regs : chip->num_type_reg;
650*4882a593Smuzhiyun if (num_type_reg) {
651*4882a593Smuzhiyun d->type_buf_def = kcalloc(num_type_reg,
652*4882a593Smuzhiyun sizeof(unsigned int), GFP_KERNEL);
653*4882a593Smuzhiyun if (!d->type_buf_def)
654*4882a593Smuzhiyun goto err_alloc;
655*4882a593Smuzhiyun
656*4882a593Smuzhiyun d->type_buf = kcalloc(num_type_reg, sizeof(unsigned int),
657*4882a593Smuzhiyun GFP_KERNEL);
658*4882a593Smuzhiyun if (!d->type_buf)
659*4882a593Smuzhiyun goto err_alloc;
660*4882a593Smuzhiyun }
661*4882a593Smuzhiyun
662*4882a593Smuzhiyun d->irq_chip = regmap_irq_chip;
663*4882a593Smuzhiyun d->irq_chip.name = chip->name;
664*4882a593Smuzhiyun d->irq = irq;
665*4882a593Smuzhiyun d->map = map;
666*4882a593Smuzhiyun d->chip = chip;
667*4882a593Smuzhiyun d->irq_base = irq_base;
668*4882a593Smuzhiyun
669*4882a593Smuzhiyun if (chip->irq_reg_stride)
670*4882a593Smuzhiyun d->irq_reg_stride = chip->irq_reg_stride;
671*4882a593Smuzhiyun else
672*4882a593Smuzhiyun d->irq_reg_stride = 1;
673*4882a593Smuzhiyun
674*4882a593Smuzhiyun if (chip->type_reg_stride)
675*4882a593Smuzhiyun d->type_reg_stride = chip->type_reg_stride;
676*4882a593Smuzhiyun else
677*4882a593Smuzhiyun d->type_reg_stride = 1;
678*4882a593Smuzhiyun
679*4882a593Smuzhiyun if (!map->use_single_read && map->reg_stride == 1 &&
680*4882a593Smuzhiyun d->irq_reg_stride == 1) {
681*4882a593Smuzhiyun d->status_reg_buf = kmalloc_array(chip->num_regs,
682*4882a593Smuzhiyun map->format.val_bytes,
683*4882a593Smuzhiyun GFP_KERNEL);
684*4882a593Smuzhiyun if (!d->status_reg_buf)
685*4882a593Smuzhiyun goto err_alloc;
686*4882a593Smuzhiyun }
687*4882a593Smuzhiyun
688*4882a593Smuzhiyun mutex_init(&d->lock);
689*4882a593Smuzhiyun
690*4882a593Smuzhiyun for (i = 0; i < chip->num_irqs; i++)
691*4882a593Smuzhiyun d->mask_buf_def[chip->irqs[i].reg_offset / map->reg_stride]
692*4882a593Smuzhiyun |= chip->irqs[i].mask;
693*4882a593Smuzhiyun
694*4882a593Smuzhiyun /* Mask all the interrupts by default */
695*4882a593Smuzhiyun for (i = 0; i < chip->num_regs; i++) {
696*4882a593Smuzhiyun d->mask_buf[i] = d->mask_buf_def[i];
697*4882a593Smuzhiyun if (!chip->mask_base)
698*4882a593Smuzhiyun continue;
699*4882a593Smuzhiyun
700*4882a593Smuzhiyun reg = chip->mask_base +
701*4882a593Smuzhiyun (i * map->reg_stride * d->irq_reg_stride);
702*4882a593Smuzhiyun if (chip->mask_invert)
703*4882a593Smuzhiyun ret = regmap_irq_update_bits(d, reg,
704*4882a593Smuzhiyun d->mask_buf[i], ~d->mask_buf[i]);
705*4882a593Smuzhiyun else if (d->chip->unmask_base) {
706*4882a593Smuzhiyun unmask_offset = d->chip->unmask_base -
707*4882a593Smuzhiyun d->chip->mask_base;
708*4882a593Smuzhiyun ret = regmap_irq_update_bits(d,
709*4882a593Smuzhiyun reg + unmask_offset,
710*4882a593Smuzhiyun d->mask_buf[i],
711*4882a593Smuzhiyun d->mask_buf[i]);
712*4882a593Smuzhiyun } else
713*4882a593Smuzhiyun ret = regmap_irq_update_bits(d, reg,
714*4882a593Smuzhiyun d->mask_buf[i], d->mask_buf[i]);
715*4882a593Smuzhiyun if (ret != 0) {
716*4882a593Smuzhiyun dev_err(map->dev, "Failed to set masks in 0x%x: %d\n",
717*4882a593Smuzhiyun reg, ret);
718*4882a593Smuzhiyun goto err_alloc;
719*4882a593Smuzhiyun }
720*4882a593Smuzhiyun
721*4882a593Smuzhiyun if (!chip->init_ack_masked)
722*4882a593Smuzhiyun continue;
723*4882a593Smuzhiyun
724*4882a593Smuzhiyun /* Ack masked but set interrupts */
725*4882a593Smuzhiyun reg = chip->status_base +
726*4882a593Smuzhiyun (i * map->reg_stride * d->irq_reg_stride);
727*4882a593Smuzhiyun ret = regmap_read(map, reg, &d->status_buf[i]);
728*4882a593Smuzhiyun if (ret != 0) {
729*4882a593Smuzhiyun dev_err(map->dev, "Failed to read IRQ status: %d\n",
730*4882a593Smuzhiyun ret);
731*4882a593Smuzhiyun goto err_alloc;
732*4882a593Smuzhiyun }
733*4882a593Smuzhiyun
734*4882a593Smuzhiyun if (d->status_buf[i] && (chip->ack_base || chip->use_ack)) {
735*4882a593Smuzhiyun reg = chip->ack_base +
736*4882a593Smuzhiyun (i * map->reg_stride * d->irq_reg_stride);
737*4882a593Smuzhiyun if (chip->ack_invert)
738*4882a593Smuzhiyun ret = regmap_write(map, reg,
739*4882a593Smuzhiyun ~(d->status_buf[i] & d->mask_buf[i]));
740*4882a593Smuzhiyun else
741*4882a593Smuzhiyun ret = regmap_write(map, reg,
742*4882a593Smuzhiyun d->status_buf[i] & d->mask_buf[i]);
743*4882a593Smuzhiyun if (chip->clear_ack) {
744*4882a593Smuzhiyun if (chip->ack_invert && !ret)
745*4882a593Smuzhiyun ret = regmap_write(map, reg, UINT_MAX);
746*4882a593Smuzhiyun else if (!ret)
747*4882a593Smuzhiyun ret = regmap_write(map, reg, 0);
748*4882a593Smuzhiyun }
749*4882a593Smuzhiyun if (ret != 0) {
750*4882a593Smuzhiyun dev_err(map->dev, "Failed to ack 0x%x: %d\n",
751*4882a593Smuzhiyun reg, ret);
752*4882a593Smuzhiyun goto err_alloc;
753*4882a593Smuzhiyun }
754*4882a593Smuzhiyun }
755*4882a593Smuzhiyun }
756*4882a593Smuzhiyun
757*4882a593Smuzhiyun /* Wake is disabled by default */
758*4882a593Smuzhiyun if (d->wake_buf) {
759*4882a593Smuzhiyun for (i = 0; i < chip->num_regs; i++) {
760*4882a593Smuzhiyun d->wake_buf[i] = d->mask_buf_def[i];
761*4882a593Smuzhiyun reg = chip->wake_base +
762*4882a593Smuzhiyun (i * map->reg_stride * d->irq_reg_stride);
763*4882a593Smuzhiyun
764*4882a593Smuzhiyun if (chip->wake_invert)
765*4882a593Smuzhiyun ret = regmap_irq_update_bits(d, reg,
766*4882a593Smuzhiyun d->mask_buf_def[i],
767*4882a593Smuzhiyun 0);
768*4882a593Smuzhiyun else
769*4882a593Smuzhiyun ret = regmap_irq_update_bits(d, reg,
770*4882a593Smuzhiyun d->mask_buf_def[i],
771*4882a593Smuzhiyun d->wake_buf[i]);
772*4882a593Smuzhiyun if (ret != 0) {
773*4882a593Smuzhiyun dev_err(map->dev, "Failed to set masks in 0x%x: %d\n",
774*4882a593Smuzhiyun reg, ret);
775*4882a593Smuzhiyun goto err_alloc;
776*4882a593Smuzhiyun }
777*4882a593Smuzhiyun }
778*4882a593Smuzhiyun }
779*4882a593Smuzhiyun
780*4882a593Smuzhiyun if (chip->num_type_reg && !chip->type_in_mask) {
781*4882a593Smuzhiyun for (i = 0; i < chip->num_type_reg; ++i) {
782*4882a593Smuzhiyun reg = chip->type_base +
783*4882a593Smuzhiyun (i * map->reg_stride * d->type_reg_stride);
784*4882a593Smuzhiyun
785*4882a593Smuzhiyun ret = regmap_read(map, reg, &d->type_buf_def[i]);
786*4882a593Smuzhiyun
787*4882a593Smuzhiyun if (d->chip->type_invert)
788*4882a593Smuzhiyun d->type_buf_def[i] = ~d->type_buf_def[i];
789*4882a593Smuzhiyun
790*4882a593Smuzhiyun if (ret) {
791*4882a593Smuzhiyun dev_err(map->dev, "Failed to get type defaults at 0x%x: %d\n",
792*4882a593Smuzhiyun reg, ret);
793*4882a593Smuzhiyun goto err_alloc;
794*4882a593Smuzhiyun }
795*4882a593Smuzhiyun }
796*4882a593Smuzhiyun }
797*4882a593Smuzhiyun
798*4882a593Smuzhiyun if (irq_base)
799*4882a593Smuzhiyun d->domain = irq_domain_add_legacy(to_of_node(fwnode),
800*4882a593Smuzhiyun chip->num_irqs, irq_base,
801*4882a593Smuzhiyun 0, ®map_domain_ops, d);
802*4882a593Smuzhiyun else
803*4882a593Smuzhiyun d->domain = irq_domain_add_linear(to_of_node(fwnode),
804*4882a593Smuzhiyun chip->num_irqs,
805*4882a593Smuzhiyun ®map_domain_ops, d);
806*4882a593Smuzhiyun if (!d->domain) {
807*4882a593Smuzhiyun dev_err(map->dev, "Failed to create IRQ domain\n");
808*4882a593Smuzhiyun ret = -ENOMEM;
809*4882a593Smuzhiyun goto err_alloc;
810*4882a593Smuzhiyun }
811*4882a593Smuzhiyun
812*4882a593Smuzhiyun ret = request_threaded_irq(irq, NULL, regmap_irq_thread,
813*4882a593Smuzhiyun irq_flags | IRQF_ONESHOT,
814*4882a593Smuzhiyun chip->name, d);
815*4882a593Smuzhiyun if (ret != 0) {
816*4882a593Smuzhiyun dev_err(map->dev, "Failed to request IRQ %d for %s: %d\n",
817*4882a593Smuzhiyun irq, chip->name, ret);
818*4882a593Smuzhiyun goto err_domain;
819*4882a593Smuzhiyun }
820*4882a593Smuzhiyun
821*4882a593Smuzhiyun *data = d;
822*4882a593Smuzhiyun
823*4882a593Smuzhiyun return 0;
824*4882a593Smuzhiyun
825*4882a593Smuzhiyun err_domain:
826*4882a593Smuzhiyun /* Should really dispose of the domain but... */
827*4882a593Smuzhiyun err_alloc:
828*4882a593Smuzhiyun kfree(d->type_buf);
829*4882a593Smuzhiyun kfree(d->type_buf_def);
830*4882a593Smuzhiyun kfree(d->wake_buf);
831*4882a593Smuzhiyun kfree(d->mask_buf_def);
832*4882a593Smuzhiyun kfree(d->mask_buf);
833*4882a593Smuzhiyun kfree(d->status_buf);
834*4882a593Smuzhiyun kfree(d->status_reg_buf);
835*4882a593Smuzhiyun kfree(d);
836*4882a593Smuzhiyun return ret;
837*4882a593Smuzhiyun }
838*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regmap_add_irq_chip_fwnode);
839*4882a593Smuzhiyun
840*4882a593Smuzhiyun /**
841*4882a593Smuzhiyun * regmap_add_irq_chip() - Use standard regmap IRQ controller handling
842*4882a593Smuzhiyun *
843*4882a593Smuzhiyun * @map: The regmap for the device.
844*4882a593Smuzhiyun * @irq: The IRQ the device uses to signal interrupts.
845*4882a593Smuzhiyun * @irq_flags: The IRQF_ flags to use for the primary interrupt.
846*4882a593Smuzhiyun * @irq_base: Allocate at specific IRQ number if irq_base > 0.
847*4882a593Smuzhiyun * @chip: Configuration for the interrupt controller.
848*4882a593Smuzhiyun * @data: Runtime data structure for the controller, allocated on success.
849*4882a593Smuzhiyun *
850*4882a593Smuzhiyun * Returns 0 on success or an errno on failure.
851*4882a593Smuzhiyun *
852*4882a593Smuzhiyun * This is the same as regmap_add_irq_chip_fwnode, except that the firmware
853*4882a593Smuzhiyun * node of the regmap is used.
854*4882a593Smuzhiyun */
regmap_add_irq_chip(struct regmap * map,int irq,int irq_flags,int irq_base,const struct regmap_irq_chip * chip,struct regmap_irq_chip_data ** data)855*4882a593Smuzhiyun int regmap_add_irq_chip(struct regmap *map, int irq, int irq_flags,
856*4882a593Smuzhiyun int irq_base, const struct regmap_irq_chip *chip,
857*4882a593Smuzhiyun struct regmap_irq_chip_data **data)
858*4882a593Smuzhiyun {
859*4882a593Smuzhiyun return regmap_add_irq_chip_fwnode(dev_fwnode(map->dev), map, irq,
860*4882a593Smuzhiyun irq_flags, irq_base, chip, data);
861*4882a593Smuzhiyun }
862*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regmap_add_irq_chip);
863*4882a593Smuzhiyun
864*4882a593Smuzhiyun /**
865*4882a593Smuzhiyun * regmap_del_irq_chip() - Stop interrupt handling for a regmap IRQ chip
866*4882a593Smuzhiyun *
867*4882a593Smuzhiyun * @irq: Primary IRQ for the device
868*4882a593Smuzhiyun * @d: ®map_irq_chip_data allocated by regmap_add_irq_chip()
869*4882a593Smuzhiyun *
870*4882a593Smuzhiyun * This function also disposes of all mapped IRQs on the chip.
871*4882a593Smuzhiyun */
regmap_del_irq_chip(int irq,struct regmap_irq_chip_data * d)872*4882a593Smuzhiyun void regmap_del_irq_chip(int irq, struct regmap_irq_chip_data *d)
873*4882a593Smuzhiyun {
874*4882a593Smuzhiyun unsigned int virq;
875*4882a593Smuzhiyun int hwirq;
876*4882a593Smuzhiyun
877*4882a593Smuzhiyun if (!d)
878*4882a593Smuzhiyun return;
879*4882a593Smuzhiyun
880*4882a593Smuzhiyun free_irq(irq, d);
881*4882a593Smuzhiyun
882*4882a593Smuzhiyun /* Dispose all virtual irq from irq domain before removing it */
883*4882a593Smuzhiyun for (hwirq = 0; hwirq < d->chip->num_irqs; hwirq++) {
884*4882a593Smuzhiyun /* Ignore hwirq if holes in the IRQ list */
885*4882a593Smuzhiyun if (!d->chip->irqs[hwirq].mask)
886*4882a593Smuzhiyun continue;
887*4882a593Smuzhiyun
888*4882a593Smuzhiyun /*
889*4882a593Smuzhiyun * Find the virtual irq of hwirq on chip and if it is
890*4882a593Smuzhiyun * there then dispose it
891*4882a593Smuzhiyun */
892*4882a593Smuzhiyun virq = irq_find_mapping(d->domain, hwirq);
893*4882a593Smuzhiyun if (virq)
894*4882a593Smuzhiyun irq_dispose_mapping(virq);
895*4882a593Smuzhiyun }
896*4882a593Smuzhiyun
897*4882a593Smuzhiyun irq_domain_remove(d->domain);
898*4882a593Smuzhiyun kfree(d->type_buf);
899*4882a593Smuzhiyun kfree(d->type_buf_def);
900*4882a593Smuzhiyun kfree(d->wake_buf);
901*4882a593Smuzhiyun kfree(d->mask_buf_def);
902*4882a593Smuzhiyun kfree(d->mask_buf);
903*4882a593Smuzhiyun kfree(d->status_reg_buf);
904*4882a593Smuzhiyun kfree(d->status_buf);
905*4882a593Smuzhiyun kfree(d);
906*4882a593Smuzhiyun }
907*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regmap_del_irq_chip);
908*4882a593Smuzhiyun
devm_regmap_irq_chip_release(struct device * dev,void * res)909*4882a593Smuzhiyun static void devm_regmap_irq_chip_release(struct device *dev, void *res)
910*4882a593Smuzhiyun {
911*4882a593Smuzhiyun struct regmap_irq_chip_data *d = *(struct regmap_irq_chip_data **)res;
912*4882a593Smuzhiyun
913*4882a593Smuzhiyun regmap_del_irq_chip(d->irq, d);
914*4882a593Smuzhiyun }
915*4882a593Smuzhiyun
devm_regmap_irq_chip_match(struct device * dev,void * res,void * data)916*4882a593Smuzhiyun static int devm_regmap_irq_chip_match(struct device *dev, void *res, void *data)
917*4882a593Smuzhiyun
918*4882a593Smuzhiyun {
919*4882a593Smuzhiyun struct regmap_irq_chip_data **r = res;
920*4882a593Smuzhiyun
921*4882a593Smuzhiyun if (!r || !*r) {
922*4882a593Smuzhiyun WARN_ON(!r || !*r);
923*4882a593Smuzhiyun return 0;
924*4882a593Smuzhiyun }
925*4882a593Smuzhiyun return *r == data;
926*4882a593Smuzhiyun }
927*4882a593Smuzhiyun
928*4882a593Smuzhiyun /**
929*4882a593Smuzhiyun * devm_regmap_add_irq_chip_fwnode() - Resource managed regmap_add_irq_chip_fwnode()
930*4882a593Smuzhiyun *
931*4882a593Smuzhiyun * @dev: The device pointer on which irq_chip belongs to.
932*4882a593Smuzhiyun * @fwnode: The firmware node where the IRQ domain should be added to.
933*4882a593Smuzhiyun * @map: The regmap for the device.
934*4882a593Smuzhiyun * @irq: The IRQ the device uses to signal interrupts
935*4882a593Smuzhiyun * @irq_flags: The IRQF_ flags to use for the primary interrupt.
936*4882a593Smuzhiyun * @irq_base: Allocate at specific IRQ number if irq_base > 0.
937*4882a593Smuzhiyun * @chip: Configuration for the interrupt controller.
938*4882a593Smuzhiyun * @data: Runtime data structure for the controller, allocated on success
939*4882a593Smuzhiyun *
940*4882a593Smuzhiyun * Returns 0 on success or an errno on failure.
941*4882a593Smuzhiyun *
942*4882a593Smuzhiyun * The ®map_irq_chip_data will be automatically released when the device is
943*4882a593Smuzhiyun * unbound.
944*4882a593Smuzhiyun */
devm_regmap_add_irq_chip_fwnode(struct device * dev,struct fwnode_handle * fwnode,struct regmap * map,int irq,int irq_flags,int irq_base,const struct regmap_irq_chip * chip,struct regmap_irq_chip_data ** data)945*4882a593Smuzhiyun int devm_regmap_add_irq_chip_fwnode(struct device *dev,
946*4882a593Smuzhiyun struct fwnode_handle *fwnode,
947*4882a593Smuzhiyun struct regmap *map, int irq,
948*4882a593Smuzhiyun int irq_flags, int irq_base,
949*4882a593Smuzhiyun const struct regmap_irq_chip *chip,
950*4882a593Smuzhiyun struct regmap_irq_chip_data **data)
951*4882a593Smuzhiyun {
952*4882a593Smuzhiyun struct regmap_irq_chip_data **ptr, *d;
953*4882a593Smuzhiyun int ret;
954*4882a593Smuzhiyun
955*4882a593Smuzhiyun ptr = devres_alloc(devm_regmap_irq_chip_release, sizeof(*ptr),
956*4882a593Smuzhiyun GFP_KERNEL);
957*4882a593Smuzhiyun if (!ptr)
958*4882a593Smuzhiyun return -ENOMEM;
959*4882a593Smuzhiyun
960*4882a593Smuzhiyun ret = regmap_add_irq_chip_fwnode(fwnode, map, irq, irq_flags, irq_base,
961*4882a593Smuzhiyun chip, &d);
962*4882a593Smuzhiyun if (ret < 0) {
963*4882a593Smuzhiyun devres_free(ptr);
964*4882a593Smuzhiyun return ret;
965*4882a593Smuzhiyun }
966*4882a593Smuzhiyun
967*4882a593Smuzhiyun *ptr = d;
968*4882a593Smuzhiyun devres_add(dev, ptr);
969*4882a593Smuzhiyun *data = d;
970*4882a593Smuzhiyun return 0;
971*4882a593Smuzhiyun }
972*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(devm_regmap_add_irq_chip_fwnode);
973*4882a593Smuzhiyun
974*4882a593Smuzhiyun /**
975*4882a593Smuzhiyun * devm_regmap_add_irq_chip() - Resource manager regmap_add_irq_chip()
976*4882a593Smuzhiyun *
977*4882a593Smuzhiyun * @dev: The device pointer on which irq_chip belongs to.
978*4882a593Smuzhiyun * @map: The regmap for the device.
979*4882a593Smuzhiyun * @irq: The IRQ the device uses to signal interrupts
980*4882a593Smuzhiyun * @irq_flags: The IRQF_ flags to use for the primary interrupt.
981*4882a593Smuzhiyun * @irq_base: Allocate at specific IRQ number if irq_base > 0.
982*4882a593Smuzhiyun * @chip: Configuration for the interrupt controller.
983*4882a593Smuzhiyun * @data: Runtime data structure for the controller, allocated on success
984*4882a593Smuzhiyun *
985*4882a593Smuzhiyun * Returns 0 on success or an errno on failure.
986*4882a593Smuzhiyun *
987*4882a593Smuzhiyun * The ®map_irq_chip_data will be automatically released when the device is
988*4882a593Smuzhiyun * unbound.
989*4882a593Smuzhiyun */
devm_regmap_add_irq_chip(struct device * dev,struct regmap * map,int irq,int irq_flags,int irq_base,const struct regmap_irq_chip * chip,struct regmap_irq_chip_data ** data)990*4882a593Smuzhiyun int devm_regmap_add_irq_chip(struct device *dev, struct regmap *map, int irq,
991*4882a593Smuzhiyun int irq_flags, int irq_base,
992*4882a593Smuzhiyun const struct regmap_irq_chip *chip,
993*4882a593Smuzhiyun struct regmap_irq_chip_data **data)
994*4882a593Smuzhiyun {
995*4882a593Smuzhiyun return devm_regmap_add_irq_chip_fwnode(dev, dev_fwnode(map->dev), map,
996*4882a593Smuzhiyun irq, irq_flags, irq_base, chip,
997*4882a593Smuzhiyun data);
998*4882a593Smuzhiyun }
999*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(devm_regmap_add_irq_chip);
1000*4882a593Smuzhiyun
1001*4882a593Smuzhiyun /**
1002*4882a593Smuzhiyun * devm_regmap_del_irq_chip() - Resource managed regmap_del_irq_chip()
1003*4882a593Smuzhiyun *
1004*4882a593Smuzhiyun * @dev: Device for which which resource was allocated.
1005*4882a593Smuzhiyun * @irq: Primary IRQ for the device.
1006*4882a593Smuzhiyun * @data: ®map_irq_chip_data allocated by regmap_add_irq_chip().
1007*4882a593Smuzhiyun *
1008*4882a593Smuzhiyun * A resource managed version of regmap_del_irq_chip().
1009*4882a593Smuzhiyun */
devm_regmap_del_irq_chip(struct device * dev,int irq,struct regmap_irq_chip_data * data)1010*4882a593Smuzhiyun void devm_regmap_del_irq_chip(struct device *dev, int irq,
1011*4882a593Smuzhiyun struct regmap_irq_chip_data *data)
1012*4882a593Smuzhiyun {
1013*4882a593Smuzhiyun int rc;
1014*4882a593Smuzhiyun
1015*4882a593Smuzhiyun WARN_ON(irq != data->irq);
1016*4882a593Smuzhiyun rc = devres_release(dev, devm_regmap_irq_chip_release,
1017*4882a593Smuzhiyun devm_regmap_irq_chip_match, data);
1018*4882a593Smuzhiyun
1019*4882a593Smuzhiyun if (rc != 0)
1020*4882a593Smuzhiyun WARN_ON(rc);
1021*4882a593Smuzhiyun }
1022*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(devm_regmap_del_irq_chip);
1023*4882a593Smuzhiyun
1024*4882a593Smuzhiyun /**
1025*4882a593Smuzhiyun * regmap_irq_chip_get_base() - Retrieve interrupt base for a regmap IRQ chip
1026*4882a593Smuzhiyun *
1027*4882a593Smuzhiyun * @data: regmap irq controller to operate on.
1028*4882a593Smuzhiyun *
1029*4882a593Smuzhiyun * Useful for drivers to request their own IRQs.
1030*4882a593Smuzhiyun */
regmap_irq_chip_get_base(struct regmap_irq_chip_data * data)1031*4882a593Smuzhiyun int regmap_irq_chip_get_base(struct regmap_irq_chip_data *data)
1032*4882a593Smuzhiyun {
1033*4882a593Smuzhiyun WARN_ON(!data->irq_base);
1034*4882a593Smuzhiyun return data->irq_base;
1035*4882a593Smuzhiyun }
1036*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regmap_irq_chip_get_base);
1037*4882a593Smuzhiyun
1038*4882a593Smuzhiyun /**
1039*4882a593Smuzhiyun * regmap_irq_get_virq() - Map an interrupt on a chip to a virtual IRQ
1040*4882a593Smuzhiyun *
1041*4882a593Smuzhiyun * @data: regmap irq controller to operate on.
1042*4882a593Smuzhiyun * @irq: index of the interrupt requested in the chip IRQs.
1043*4882a593Smuzhiyun *
1044*4882a593Smuzhiyun * Useful for drivers to request their own IRQs.
1045*4882a593Smuzhiyun */
regmap_irq_get_virq(struct regmap_irq_chip_data * data,int irq)1046*4882a593Smuzhiyun int regmap_irq_get_virq(struct regmap_irq_chip_data *data, int irq)
1047*4882a593Smuzhiyun {
1048*4882a593Smuzhiyun /* Handle holes in the IRQ list */
1049*4882a593Smuzhiyun if (!data->chip->irqs[irq].mask)
1050*4882a593Smuzhiyun return -EINVAL;
1051*4882a593Smuzhiyun
1052*4882a593Smuzhiyun return irq_create_mapping(data->domain, irq);
1053*4882a593Smuzhiyun }
1054*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regmap_irq_get_virq);
1055*4882a593Smuzhiyun
1056*4882a593Smuzhiyun /**
1057*4882a593Smuzhiyun * regmap_irq_get_domain() - Retrieve the irq_domain for the chip
1058*4882a593Smuzhiyun *
1059*4882a593Smuzhiyun * @data: regmap_irq controller to operate on.
1060*4882a593Smuzhiyun *
1061*4882a593Smuzhiyun * Useful for drivers to request their own IRQs and for integration
1062*4882a593Smuzhiyun * with subsystems. For ease of integration NULL is accepted as a
1063*4882a593Smuzhiyun * domain, allowing devices to just call this even if no domain is
1064*4882a593Smuzhiyun * allocated.
1065*4882a593Smuzhiyun */
regmap_irq_get_domain(struct regmap_irq_chip_data * data)1066*4882a593Smuzhiyun struct irq_domain *regmap_irq_get_domain(struct regmap_irq_chip_data *data)
1067*4882a593Smuzhiyun {
1068*4882a593Smuzhiyun if (data)
1069*4882a593Smuzhiyun return data->domain;
1070*4882a593Smuzhiyun else
1071*4882a593Smuzhiyun return NULL;
1072*4882a593Smuzhiyun }
1073*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(regmap_irq_get_domain);
1074