xref: /openbmc/linux/drivers/base/regmap/regmap-irq.c (revision bc33f5e5)
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 			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 
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 
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 
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 
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 
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 
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 
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  */
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  */
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  */
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 	u32 reg;
726 
727 	if (chip->num_regs <= 0)
728 		return -EINVAL;
729 
730 	if (chip->clear_on_unmask && (chip->ack_base || chip->use_ack))
731 		return -EINVAL;
732 
733 	for (i = 0; i < chip->num_irqs; i++) {
734 		if (chip->irqs[i].reg_offset % map->reg_stride)
735 			return -EINVAL;
736 		if (chip->irqs[i].reg_offset / map->reg_stride >=
737 		    chip->num_regs)
738 			return -EINVAL;
739 	}
740 
741 	if (chip->not_fixed_stride) {
742 		dev_warn(map->dev, "not_fixed_stride is deprecated; use ->get_irq_reg() instead");
743 
744 		for (i = 0; i < chip->num_regs; i++)
745 			if (chip->sub_reg_offsets[i].num_regs != 1)
746 				return -EINVAL;
747 	}
748 
749 	if (chip->num_type_reg)
750 		dev_warn(map->dev, "type registers are deprecated; use config registers instead");
751 
752 	if (chip->num_virt_regs || chip->virt_reg_base || chip->set_type_virt)
753 		dev_warn(map->dev, "virtual registers are deprecated; use config registers instead");
754 
755 	if (irq_base) {
756 		irq_base = irq_alloc_descs(irq_base, 0, chip->num_irqs, 0);
757 		if (irq_base < 0) {
758 			dev_warn(map->dev, "Failed to allocate IRQs: %d\n",
759 				 irq_base);
760 			return irq_base;
761 		}
762 	}
763 
764 	d = kzalloc(sizeof(*d), GFP_KERNEL);
765 	if (!d)
766 		return -ENOMEM;
767 
768 	if (chip->num_main_regs) {
769 		d->main_status_buf = kcalloc(chip->num_main_regs,
770 					     sizeof(*d->main_status_buf),
771 					     GFP_KERNEL);
772 
773 		if (!d->main_status_buf)
774 			goto err_alloc;
775 	}
776 
777 	d->status_buf = kcalloc(chip->num_regs, sizeof(*d->status_buf),
778 				GFP_KERNEL);
779 	if (!d->status_buf)
780 		goto err_alloc;
781 
782 	d->mask_buf = kcalloc(chip->num_regs, sizeof(*d->mask_buf),
783 			      GFP_KERNEL);
784 	if (!d->mask_buf)
785 		goto err_alloc;
786 
787 	d->mask_buf_def = kcalloc(chip->num_regs, sizeof(*d->mask_buf_def),
788 				  GFP_KERNEL);
789 	if (!d->mask_buf_def)
790 		goto err_alloc;
791 
792 	if (chip->wake_base) {
793 		d->wake_buf = kcalloc(chip->num_regs, sizeof(*d->wake_buf),
794 				      GFP_KERNEL);
795 		if (!d->wake_buf)
796 			goto err_alloc;
797 	}
798 
799 	num_type_reg = chip->type_in_mask ? chip->num_regs : chip->num_type_reg;
800 	if (num_type_reg) {
801 		d->type_buf_def = kcalloc(num_type_reg,
802 					  sizeof(*d->type_buf_def), GFP_KERNEL);
803 		if (!d->type_buf_def)
804 			goto err_alloc;
805 
806 		d->type_buf = kcalloc(num_type_reg, sizeof(*d->type_buf),
807 				      GFP_KERNEL);
808 		if (!d->type_buf)
809 			goto err_alloc;
810 	}
811 
812 	if (chip->num_virt_regs) {
813 		/*
814 		 * Create virt_buf[chip->num_extra_config_regs][chip->num_regs]
815 		 */
816 		d->virt_buf = kcalloc(chip->num_virt_regs, sizeof(*d->virt_buf),
817 				      GFP_KERNEL);
818 		if (!d->virt_buf)
819 			goto err_alloc;
820 
821 		for (i = 0; i < chip->num_virt_regs; i++) {
822 			d->virt_buf[i] = kcalloc(chip->num_regs,
823 						 sizeof(**d->virt_buf),
824 						 GFP_KERNEL);
825 			if (!d->virt_buf[i])
826 				goto err_alloc;
827 		}
828 	}
829 
830 	if (chip->num_config_bases && chip->num_config_regs) {
831 		/*
832 		 * Create config_buf[num_config_bases][num_config_regs]
833 		 */
834 		d->config_buf = kcalloc(chip->num_config_bases,
835 					sizeof(*d->config_buf), GFP_KERNEL);
836 		if (!d->config_buf)
837 			goto err_alloc;
838 
839 		for (i = 0; i < chip->num_config_regs; i++) {
840 			d->config_buf[i] = kcalloc(chip->num_config_regs,
841 						   sizeof(**d->config_buf),
842 						   GFP_KERNEL);
843 			if (!d->config_buf[i])
844 				goto err_alloc;
845 		}
846 	}
847 
848 	d->irq_chip = regmap_irq_chip;
849 	d->irq_chip.name = chip->name;
850 	d->irq = irq;
851 	d->map = map;
852 	d->chip = chip;
853 	d->irq_base = irq_base;
854 
855 	if (chip->mask_base && chip->unmask_base &&
856 	    !chip->mask_unmask_non_inverted) {
857 		/*
858 		 * Chips that specify both mask_base and unmask_base used to
859 		 * get inverted mask behavior by default, with no way to ask
860 		 * for the normal, non-inverted behavior. This "inverted by
861 		 * default" behavior is deprecated, but we have to support it
862 		 * until existing drivers have been fixed.
863 		 *
864 		 * Existing drivers should be updated by swapping mask_base
865 		 * and unmask_base and setting mask_unmask_non_inverted=true.
866 		 * New drivers should always set the flag.
867 		 */
868 		dev_warn(map->dev, "mask_base and unmask_base are inverted, please fix it");
869 
870 		/* Might as well warn about mask_invert while we're at it... */
871 		if (chip->mask_invert)
872 			dev_warn(map->dev, "mask_invert=true ignored");
873 
874 		d->mask_base = chip->unmask_base;
875 		d->unmask_base = chip->mask_base;
876 	} else if (chip->mask_invert) {
877 		/*
878 		 * Swap the roles of mask_base and unmask_base if the bits are
879 		 * inverted. This is deprecated, drivers should use unmask_base
880 		 * directly.
881 		 */
882 		dev_warn(map->dev, "mask_invert=true is deprecated; please switch to unmask_base");
883 
884 		d->mask_base = chip->unmask_base;
885 		d->unmask_base = chip->mask_base;
886 	} else {
887 		d->mask_base = chip->mask_base;
888 		d->unmask_base = chip->unmask_base;
889 	}
890 
891 	if (chip->irq_reg_stride)
892 		d->irq_reg_stride = chip->irq_reg_stride;
893 	else
894 		d->irq_reg_stride = 1;
895 
896 	if (chip->get_irq_reg)
897 		d->get_irq_reg = chip->get_irq_reg;
898 	else
899 		d->get_irq_reg = regmap_irq_get_irq_reg_linear;
900 
901 	if (regmap_irq_can_bulk_read_status(d)) {
902 		d->status_reg_buf = kmalloc_array(chip->num_regs,
903 						  map->format.val_bytes,
904 						  GFP_KERNEL);
905 		if (!d->status_reg_buf)
906 			goto err_alloc;
907 	}
908 
909 	mutex_init(&d->lock);
910 
911 	for (i = 0; i < chip->num_irqs; i++)
912 		d->mask_buf_def[chip->irqs[i].reg_offset / map->reg_stride]
913 			|= chip->irqs[i].mask;
914 
915 	/* Mask all the interrupts by default */
916 	for (i = 0; i < chip->num_regs; i++) {
917 		d->mask_buf[i] = d->mask_buf_def[i];
918 
919 		if (d->mask_base) {
920 			reg = d->get_irq_reg(d, d->mask_base, i);
921 			ret = regmap_update_bits(d->map, reg,
922 					d->mask_buf_def[i], d->mask_buf[i]);
923 			if (ret) {
924 				dev_err(map->dev, "Failed to set masks in 0x%x: %d\n",
925 					reg, ret);
926 				goto err_alloc;
927 			}
928 		}
929 
930 		if (d->unmask_base) {
931 			reg = d->get_irq_reg(d, d->unmask_base, i);
932 			ret = regmap_update_bits(d->map, reg,
933 					d->mask_buf_def[i], ~d->mask_buf[i]);
934 			if (ret) {
935 				dev_err(map->dev, "Failed to set masks in 0x%x: %d\n",
936 					reg, ret);
937 				goto err_alloc;
938 			}
939 		}
940 
941 		if (!chip->init_ack_masked)
942 			continue;
943 
944 		/* Ack masked but set interrupts */
945 		reg = d->get_irq_reg(d, d->chip->status_base, i);
946 		ret = regmap_read(map, reg, &d->status_buf[i]);
947 		if (ret != 0) {
948 			dev_err(map->dev, "Failed to read IRQ status: %d\n",
949 				ret);
950 			goto err_alloc;
951 		}
952 
953 		if (chip->status_invert)
954 			d->status_buf[i] = ~d->status_buf[i];
955 
956 		if (d->status_buf[i] && (chip->ack_base || chip->use_ack)) {
957 			reg = d->get_irq_reg(d, d->chip->ack_base, i);
958 			if (chip->ack_invert)
959 				ret = regmap_write(map, reg,
960 					~(d->status_buf[i] & d->mask_buf[i]));
961 			else
962 				ret = regmap_write(map, reg,
963 					d->status_buf[i] & d->mask_buf[i]);
964 			if (chip->clear_ack) {
965 				if (chip->ack_invert && !ret)
966 					ret = regmap_write(map, reg, UINT_MAX);
967 				else if (!ret)
968 					ret = regmap_write(map, reg, 0);
969 			}
970 			if (ret != 0) {
971 				dev_err(map->dev, "Failed to ack 0x%x: %d\n",
972 					reg, ret);
973 				goto err_alloc;
974 			}
975 		}
976 	}
977 
978 	/* Wake is disabled by default */
979 	if (d->wake_buf) {
980 		for (i = 0; i < chip->num_regs; i++) {
981 			d->wake_buf[i] = d->mask_buf_def[i];
982 			reg = d->get_irq_reg(d, d->chip->wake_base, i);
983 
984 			if (chip->wake_invert)
985 				ret = regmap_update_bits(d->map, reg,
986 							 d->mask_buf_def[i],
987 							 0);
988 			else
989 				ret = regmap_update_bits(d->map, reg,
990 							 d->mask_buf_def[i],
991 							 d->wake_buf[i]);
992 			if (ret != 0) {
993 				dev_err(map->dev, "Failed to set masks in 0x%x: %d\n",
994 					reg, ret);
995 				goto err_alloc;
996 			}
997 		}
998 	}
999 
1000 	if (chip->num_type_reg && !chip->type_in_mask) {
1001 		for (i = 0; i < chip->num_type_reg; ++i) {
1002 			reg = d->get_irq_reg(d, d->chip->type_base, i);
1003 
1004 			ret = regmap_read(map, reg, &d->type_buf_def[i]);
1005 
1006 			if (d->chip->type_invert)
1007 				d->type_buf_def[i] = ~d->type_buf_def[i];
1008 
1009 			if (ret) {
1010 				dev_err(map->dev, "Failed to get type defaults at 0x%x: %d\n",
1011 					reg, ret);
1012 				goto err_alloc;
1013 			}
1014 		}
1015 	}
1016 
1017 	if (irq_base)
1018 		d->domain = irq_domain_create_legacy(fwnode, chip->num_irqs,
1019 						     irq_base, 0,
1020 						     &regmap_domain_ops, d);
1021 	else
1022 		d->domain = irq_domain_create_linear(fwnode, chip->num_irqs,
1023 						     &regmap_domain_ops, d);
1024 	if (!d->domain) {
1025 		dev_err(map->dev, "Failed to create IRQ domain\n");
1026 		ret = -ENOMEM;
1027 		goto err_alloc;
1028 	}
1029 
1030 	ret = request_threaded_irq(irq, NULL, regmap_irq_thread,
1031 				   irq_flags | IRQF_ONESHOT,
1032 				   chip->name, d);
1033 	if (ret != 0) {
1034 		dev_err(map->dev, "Failed to request IRQ %d for %s: %d\n",
1035 			irq, chip->name, ret);
1036 		goto err_domain;
1037 	}
1038 
1039 	*data = d;
1040 
1041 	return 0;
1042 
1043 err_domain:
1044 	/* Should really dispose of the domain but... */
1045 err_alloc:
1046 	kfree(d->type_buf);
1047 	kfree(d->type_buf_def);
1048 	kfree(d->wake_buf);
1049 	kfree(d->mask_buf_def);
1050 	kfree(d->mask_buf);
1051 	kfree(d->status_buf);
1052 	kfree(d->status_reg_buf);
1053 	if (d->virt_buf) {
1054 		for (i = 0; i < chip->num_virt_regs; i++)
1055 			kfree(d->virt_buf[i]);
1056 		kfree(d->virt_buf);
1057 	}
1058 	if (d->config_buf) {
1059 		for (i = 0; i < chip->num_config_bases; i++)
1060 			kfree(d->config_buf[i]);
1061 		kfree(d->config_buf);
1062 	}
1063 	kfree(d);
1064 	return ret;
1065 }
1066 EXPORT_SYMBOL_GPL(regmap_add_irq_chip_fwnode);
1067 
1068 /**
1069  * regmap_add_irq_chip() - Use standard regmap IRQ controller handling
1070  *
1071  * @map: The regmap for the device.
1072  * @irq: The IRQ the device uses to signal interrupts.
1073  * @irq_flags: The IRQF_ flags to use for the primary interrupt.
1074  * @irq_base: Allocate at specific IRQ number if irq_base > 0.
1075  * @chip: Configuration for the interrupt controller.
1076  * @data: Runtime data structure for the controller, allocated on success.
1077  *
1078  * Returns 0 on success or an errno on failure.
1079  *
1080  * This is the same as regmap_add_irq_chip_fwnode, except that the firmware
1081  * node of the regmap is used.
1082  */
1083 int regmap_add_irq_chip(struct regmap *map, int irq, int irq_flags,
1084 			int irq_base, const struct regmap_irq_chip *chip,
1085 			struct regmap_irq_chip_data **data)
1086 {
1087 	return regmap_add_irq_chip_fwnode(dev_fwnode(map->dev), map, irq,
1088 					  irq_flags, irq_base, chip, data);
1089 }
1090 EXPORT_SYMBOL_GPL(regmap_add_irq_chip);
1091 
1092 /**
1093  * regmap_del_irq_chip() - Stop interrupt handling for a regmap IRQ chip
1094  *
1095  * @irq: Primary IRQ for the device
1096  * @d: &regmap_irq_chip_data allocated by regmap_add_irq_chip()
1097  *
1098  * This function also disposes of all mapped IRQs on the chip.
1099  */
1100 void regmap_del_irq_chip(int irq, struct regmap_irq_chip_data *d)
1101 {
1102 	unsigned int virq;
1103 	int i, hwirq;
1104 
1105 	if (!d)
1106 		return;
1107 
1108 	free_irq(irq, d);
1109 
1110 	/* Dispose all virtual irq from irq domain before removing it */
1111 	for (hwirq = 0; hwirq < d->chip->num_irqs; hwirq++) {
1112 		/* Ignore hwirq if holes in the IRQ list */
1113 		if (!d->chip->irqs[hwirq].mask)
1114 			continue;
1115 
1116 		/*
1117 		 * Find the virtual irq of hwirq on chip and if it is
1118 		 * there then dispose it
1119 		 */
1120 		virq = irq_find_mapping(d->domain, hwirq);
1121 		if (virq)
1122 			irq_dispose_mapping(virq);
1123 	}
1124 
1125 	irq_domain_remove(d->domain);
1126 	kfree(d->type_buf);
1127 	kfree(d->type_buf_def);
1128 	kfree(d->wake_buf);
1129 	kfree(d->mask_buf_def);
1130 	kfree(d->mask_buf);
1131 	kfree(d->status_reg_buf);
1132 	kfree(d->status_buf);
1133 	if (d->config_buf) {
1134 		for (i = 0; i < d->chip->num_config_bases; i++)
1135 			kfree(d->config_buf[i]);
1136 		kfree(d->config_buf);
1137 	}
1138 	kfree(d);
1139 }
1140 EXPORT_SYMBOL_GPL(regmap_del_irq_chip);
1141 
1142 static void devm_regmap_irq_chip_release(struct device *dev, void *res)
1143 {
1144 	struct regmap_irq_chip_data *d = *(struct regmap_irq_chip_data **)res;
1145 
1146 	regmap_del_irq_chip(d->irq, d);
1147 }
1148 
1149 static int devm_regmap_irq_chip_match(struct device *dev, void *res, void *data)
1150 
1151 {
1152 	struct regmap_irq_chip_data **r = res;
1153 
1154 	if (!r || !*r) {
1155 		WARN_ON(!r || !*r);
1156 		return 0;
1157 	}
1158 	return *r == data;
1159 }
1160 
1161 /**
1162  * devm_regmap_add_irq_chip_fwnode() - Resource managed regmap_add_irq_chip_fwnode()
1163  *
1164  * @dev: The device pointer on which irq_chip belongs to.
1165  * @fwnode: The firmware node where the IRQ domain should be added to.
1166  * @map: The regmap for the device.
1167  * @irq: The IRQ the device uses to signal interrupts
1168  * @irq_flags: The IRQF_ flags to use for the primary interrupt.
1169  * @irq_base: Allocate at specific IRQ number if irq_base > 0.
1170  * @chip: Configuration for the interrupt controller.
1171  * @data: Runtime data structure for the controller, allocated on success
1172  *
1173  * Returns 0 on success or an errno on failure.
1174  *
1175  * The &regmap_irq_chip_data will be automatically released when the device is
1176  * unbound.
1177  */
1178 int devm_regmap_add_irq_chip_fwnode(struct device *dev,
1179 				    struct fwnode_handle *fwnode,
1180 				    struct regmap *map, int irq,
1181 				    int irq_flags, int irq_base,
1182 				    const struct regmap_irq_chip *chip,
1183 				    struct regmap_irq_chip_data **data)
1184 {
1185 	struct regmap_irq_chip_data **ptr, *d;
1186 	int ret;
1187 
1188 	ptr = devres_alloc(devm_regmap_irq_chip_release, sizeof(*ptr),
1189 			   GFP_KERNEL);
1190 	if (!ptr)
1191 		return -ENOMEM;
1192 
1193 	ret = regmap_add_irq_chip_fwnode(fwnode, map, irq, irq_flags, irq_base,
1194 					 chip, &d);
1195 	if (ret < 0) {
1196 		devres_free(ptr);
1197 		return ret;
1198 	}
1199 
1200 	*ptr = d;
1201 	devres_add(dev, ptr);
1202 	*data = d;
1203 	return 0;
1204 }
1205 EXPORT_SYMBOL_GPL(devm_regmap_add_irq_chip_fwnode);
1206 
1207 /**
1208  * devm_regmap_add_irq_chip() - Resource managed regmap_add_irq_chip()
1209  *
1210  * @dev: The device pointer on which irq_chip belongs to.
1211  * @map: The regmap for the device.
1212  * @irq: The IRQ the device uses to signal interrupts
1213  * @irq_flags: The IRQF_ flags to use for the primary interrupt.
1214  * @irq_base: Allocate at specific IRQ number if irq_base > 0.
1215  * @chip: Configuration for the interrupt controller.
1216  * @data: Runtime data structure for the controller, allocated on success
1217  *
1218  * Returns 0 on success or an errno on failure.
1219  *
1220  * The &regmap_irq_chip_data will be automatically released when the device is
1221  * unbound.
1222  */
1223 int devm_regmap_add_irq_chip(struct device *dev, struct regmap *map, int irq,
1224 			     int irq_flags, int irq_base,
1225 			     const struct regmap_irq_chip *chip,
1226 			     struct regmap_irq_chip_data **data)
1227 {
1228 	return devm_regmap_add_irq_chip_fwnode(dev, dev_fwnode(map->dev), map,
1229 					       irq, irq_flags, irq_base, chip,
1230 					       data);
1231 }
1232 EXPORT_SYMBOL_GPL(devm_regmap_add_irq_chip);
1233 
1234 /**
1235  * devm_regmap_del_irq_chip() - Resource managed regmap_del_irq_chip()
1236  *
1237  * @dev: Device for which the resource was allocated.
1238  * @irq: Primary IRQ for the device.
1239  * @data: &regmap_irq_chip_data allocated by regmap_add_irq_chip().
1240  *
1241  * A resource managed version of regmap_del_irq_chip().
1242  */
1243 void devm_regmap_del_irq_chip(struct device *dev, int irq,
1244 			      struct regmap_irq_chip_data *data)
1245 {
1246 	int rc;
1247 
1248 	WARN_ON(irq != data->irq);
1249 	rc = devres_release(dev, devm_regmap_irq_chip_release,
1250 			    devm_regmap_irq_chip_match, data);
1251 
1252 	if (rc != 0)
1253 		WARN_ON(rc);
1254 }
1255 EXPORT_SYMBOL_GPL(devm_regmap_del_irq_chip);
1256 
1257 /**
1258  * regmap_irq_chip_get_base() - Retrieve interrupt base for a regmap IRQ chip
1259  *
1260  * @data: regmap irq controller to operate on.
1261  *
1262  * Useful for drivers to request their own IRQs.
1263  */
1264 int regmap_irq_chip_get_base(struct regmap_irq_chip_data *data)
1265 {
1266 	WARN_ON(!data->irq_base);
1267 	return data->irq_base;
1268 }
1269 EXPORT_SYMBOL_GPL(regmap_irq_chip_get_base);
1270 
1271 /**
1272  * regmap_irq_get_virq() - Map an interrupt on a chip to a virtual IRQ
1273  *
1274  * @data: regmap irq controller to operate on.
1275  * @irq: index of the interrupt requested in the chip IRQs.
1276  *
1277  * Useful for drivers to request their own IRQs.
1278  */
1279 int regmap_irq_get_virq(struct regmap_irq_chip_data *data, int irq)
1280 {
1281 	/* Handle holes in the IRQ list */
1282 	if (!data->chip->irqs[irq].mask)
1283 		return -EINVAL;
1284 
1285 	return irq_create_mapping(data->domain, irq);
1286 }
1287 EXPORT_SYMBOL_GPL(regmap_irq_get_virq);
1288 
1289 /**
1290  * regmap_irq_get_domain() - Retrieve the irq_domain for the chip
1291  *
1292  * @data: regmap_irq controller to operate on.
1293  *
1294  * Useful for drivers to request their own IRQs and for integration
1295  * with subsystems.  For ease of integration NULL is accepted as a
1296  * domain, allowing devices to just call this even if no domain is
1297  * allocated.
1298  */
1299 struct irq_domain *regmap_irq_get_domain(struct regmap_irq_chip_data *data)
1300 {
1301 	if (data)
1302 		return data->domain;
1303 	else
1304 		return NULL;
1305 }
1306 EXPORT_SYMBOL_GPL(regmap_irq_get_domain);
1307