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