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