xref: /linux/sound/soc/sdca/sdca_interrupts.c (revision 22c55fb9eb92395d999b8404d73e58540d11bdd8)
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2025 Cirrus Logic, Inc. and
3 //                    Cirrus Logic International Semiconductor Ltd.
4 
5 /*
6  * The MIPI SDCA specification is available for public downloads at
7  * https://www.mipi.org/mipi-sdca-v1-0-download
8  */
9 
10 #include <linux/bitmap.h>
11 #include <linux/bits.h>
12 #include <linux/cleanup.h>
13 #include <linux/device.h>
14 #include <linux/interrupt.h>
15 #include <linux/regmap.h>
16 #include <linux/soundwire/sdw.h>
17 #include <linux/soundwire/sdw_registers.h>
18 #include <sound/sdca.h>
19 #include <sound/sdca_function.h>
20 #include <sound/sdca_interrupts.h>
21 #include <sound/soc-component.h>
22 #include <sound/soc.h>
23 
24 #define IRQ_SDCA(number) REGMAP_IRQ_REG(number, ((number) / BITS_PER_BYTE), \
25 					SDW_SCP_SDCA_INTMASK_SDCA_##number)
26 
27 static const struct regmap_irq regmap_irqs[SDCA_MAX_INTERRUPTS] = {
28 	IRQ_SDCA(0),
29 	IRQ_SDCA(1),
30 	IRQ_SDCA(2),
31 	IRQ_SDCA(3),
32 	IRQ_SDCA(4),
33 	IRQ_SDCA(5),
34 	IRQ_SDCA(6),
35 	IRQ_SDCA(7),
36 	IRQ_SDCA(8),
37 	IRQ_SDCA(9),
38 	IRQ_SDCA(10),
39 	IRQ_SDCA(11),
40 	IRQ_SDCA(12),
41 	IRQ_SDCA(13),
42 	IRQ_SDCA(14),
43 	IRQ_SDCA(15),
44 	IRQ_SDCA(16),
45 	IRQ_SDCA(17),
46 	IRQ_SDCA(18),
47 	IRQ_SDCA(19),
48 	IRQ_SDCA(20),
49 	IRQ_SDCA(21),
50 	IRQ_SDCA(22),
51 	IRQ_SDCA(23),
52 	IRQ_SDCA(24),
53 	IRQ_SDCA(25),
54 	IRQ_SDCA(26),
55 	IRQ_SDCA(27),
56 	IRQ_SDCA(28),
57 	IRQ_SDCA(29),
58 	IRQ_SDCA(30),
59 };
60 
61 static const struct regmap_irq_chip sdca_irq_chip = {
62 	.name = "sdca_irq",
63 
64 	.status_base = SDW_SCP_SDCA_INT1,
65 	.unmask_base = SDW_SCP_SDCA_INTMASK1,
66 	.ack_base = SDW_SCP_SDCA_INT1,
67 	.num_regs = 4,
68 
69 	.irqs = regmap_irqs,
70 	.num_irqs = SDCA_MAX_INTERRUPTS,
71 
72 	.runtime_pm = true,
73 };
74 
75 static irqreturn_t base_handler(int irq, void *data)
76 {
77 	struct sdca_interrupt *interrupt = data;
78 	struct device *dev = interrupt->component->dev;
79 
80 	dev_info(dev, "%s irq without full handling\n", interrupt->name);
81 
82 	return IRQ_HANDLED;
83 }
84 
85 static irqreturn_t function_status_handler(int irq, void *data)
86 {
87 	struct sdca_interrupt *interrupt = data;
88 	struct device *dev = interrupt->component->dev;
89 	unsigned int reg, val;
90 	unsigned long status;
91 	unsigned int mask;
92 	int ret;
93 
94 	reg = SDW_SDCA_CTL(interrupt->function->desc->adr, interrupt->entity->id,
95 			   interrupt->control->sel, 0);
96 
97 	ret = regmap_read(interrupt->component->regmap, reg, &val);
98 	if (ret < 0) {
99 		dev_err(dev, "failed to read function status: %d\n", ret);
100 		return IRQ_NONE;
101 	}
102 
103 	dev_dbg(dev, "function status: %#x\n", val);
104 
105 	status = val;
106 	for_each_set_bit(mask, &status, BITS_PER_BYTE) {
107 		mask = 1 << mask;
108 
109 		switch (mask) {
110 		case SDCA_CTL_ENTITY_0_FUNCTION_NEEDS_INITIALIZATION:
111 			//FIXME: Add init writes
112 			break;
113 		case SDCA_CTL_ENTITY_0_FUNCTION_FAULT:
114 			dev_err(dev, "function fault\n");
115 			break;
116 		case SDCA_CTL_ENTITY_0_UMP_SEQUENCE_FAULT:
117 			dev_err(dev, "ump sequence fault\n");
118 			break;
119 		case SDCA_CTL_ENTITY_0_FUNCTION_BUSY:
120 			dev_info(dev, "unexpected function busy\n");
121 			break;
122 		case SDCA_CTL_ENTITY_0_DEVICE_NEWLY_ATTACHED:
123 		case SDCA_CTL_ENTITY_0_INTS_DISABLED_ABNORMALLY:
124 		case SDCA_CTL_ENTITY_0_STREAMING_STOPPED_ABNORMALLY:
125 		case SDCA_CTL_ENTITY_0_FUNCTION_HAS_BEEN_RESET:
126 			break;
127 		}
128 	}
129 
130 	ret = regmap_write(interrupt->component->regmap, reg, val);
131 	if (ret < 0) {
132 		dev_err(dev, "failed to clear function status: %d\n", ret);
133 		return IRQ_NONE;
134 	}
135 
136 	return IRQ_HANDLED;
137 }
138 
139 static irqreturn_t detected_mode_handler(int irq, void *data)
140 {
141 	struct sdca_interrupt *interrupt = data;
142 	struct snd_soc_component *component = interrupt->component;
143 	struct device *dev = component->dev;
144 	struct snd_soc_card *card = component->card;
145 	struct rw_semaphore *rwsem = &card->snd_card->controls_rwsem;
146 	struct snd_kcontrol *kctl = interrupt->priv;
147 	struct snd_ctl_elem_value *ucontrol __free(kfree) = NULL;
148 	struct soc_enum *soc_enum;
149 	unsigned int reg, val;
150 	int ret;
151 
152 	if (!kctl) {
153 		const char *name __free(kfree) = kasprintf(GFP_KERNEL, "%s %s",
154 							   interrupt->entity->label,
155 							   SDCA_CTL_SELECTED_MODE_NAME);
156 
157 		if (!name)
158 			return -ENOMEM;
159 
160 		kctl = snd_soc_component_get_kcontrol(component, name);
161 		if (!kctl) {
162 			dev_dbg(dev, "control not found: %s\n", name);
163 			return IRQ_NONE;
164 		}
165 
166 		interrupt->priv = kctl;
167 	}
168 
169 	soc_enum = (struct soc_enum *)kctl->private_value;
170 
171 	reg = SDW_SDCA_CTL(interrupt->function->desc->adr, interrupt->entity->id,
172 			   interrupt->control->sel, 0);
173 
174 	ret = regmap_read(component->regmap, reg, &val);
175 	if (ret < 0) {
176 		dev_err(dev, "failed to read detected mode: %d\n", ret);
177 		return IRQ_NONE;
178 	}
179 
180 	switch (val) {
181 	case SDCA_DETECTED_MODE_DETECTION_IN_PROGRESS:
182 	case SDCA_DETECTED_MODE_JACK_UNKNOWN:
183 		reg = SDW_SDCA_CTL(interrupt->function->desc->adr,
184 				   interrupt->entity->id,
185 				   SDCA_CTL_GE_SELECTED_MODE, 0);
186 
187 		/*
188 		 * Selected mode is not normally marked as volatile register
189 		 * (RW), but here force a read from the hardware. If the
190 		 * detected mode is unknown we need to see what the device
191 		 * selected as a "safe" option.
192 		 */
193 		regcache_drop_region(component->regmap, reg, reg);
194 
195 		ret = regmap_read(component->regmap, reg, &val);
196 		if (ret) {
197 			dev_err(dev, "failed to re-check selected mode: %d\n", ret);
198 			return IRQ_NONE;
199 		}
200 		break;
201 	default:
202 		break;
203 	}
204 
205 	dev_dbg(dev, "%s: %#x\n", interrupt->name, val);
206 
207 	ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
208 	if (!ucontrol)
209 		return IRQ_NONE;
210 
211 	ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(soc_enum, val);
212 
213 	down_write(rwsem);
214 	ret = kctl->put(kctl, ucontrol);
215 	up_write(rwsem);
216 	if (ret < 0) {
217 		dev_err(dev, "failed to update selected mode: %d\n", ret);
218 		return IRQ_NONE;
219 	}
220 
221 	snd_ctl_notify(card->snd_card, SNDRV_CTL_EVENT_MASK_VALUE, &kctl->id);
222 
223 	return IRQ_HANDLED;
224 }
225 
226 static int sdca_irq_request_locked(struct device *dev,
227 				   struct sdca_interrupt_info *info,
228 				   int sdca_irq, const char *name,
229 				   irq_handler_t handler, void *data)
230 {
231 	int irq;
232 	int ret;
233 
234 	irq = regmap_irq_get_virq(info->irq_data, sdca_irq);
235 	if (irq < 0)
236 		return irq;
237 
238 	ret = devm_request_threaded_irq(dev, irq, NULL, handler,
239 					IRQF_ONESHOT, name, data);
240 	if (ret)
241 		return ret;
242 
243 	dev_dbg(dev, "requested irq %d for %s\n", irq, name);
244 
245 	return 0;
246 }
247 
248 /**
249  * sdca_request_irq - request an individual SDCA interrupt
250  * @dev: Pointer to the struct device against which things should be allocated.
251  * @interrupt_info: Pointer to the interrupt information structure.
252  * @sdca_irq: SDCA interrupt position.
253  * @name: Name to be given to the IRQ.
254  * @handler: A callback thread function to be called for the IRQ.
255  * @data: Private data pointer that will be passed to the handler.
256  *
257  * Typically this is handled internally by sdca_irq_populate, however if
258  * a device requires custom IRQ handling this can be called manually before
259  * calling sdca_irq_populate, which will then skip that IRQ whilst processing.
260  *
261  * Return: Zero on success, and a negative error code on failure.
262  */
263 int sdca_irq_request(struct device *dev, struct sdca_interrupt_info *info,
264 		     int sdca_irq, const char *name, irq_handler_t handler,
265 		     void *data)
266 {
267 	int ret;
268 
269 	if (sdca_irq < 0 || sdca_irq >= SDCA_MAX_INTERRUPTS) {
270 		dev_err(dev, "bad irq request: %d\n", sdca_irq);
271 		return -EINVAL;
272 	}
273 
274 	guard(mutex)(&info->irq_lock);
275 
276 	ret = sdca_irq_request_locked(dev, info, sdca_irq, name, handler, data);
277 	if (ret) {
278 		dev_err(dev, "failed to request irq %s: %d\n", name, ret);
279 		return ret;
280 	}
281 
282 	info->irqs[sdca_irq].externally_requested = true;
283 
284 	return 0;
285 }
286 EXPORT_SYMBOL_NS_GPL(sdca_irq_request, "SND_SOC_SDCA");
287 
288 /**
289  * sdca_irq_data_populate - Populate common interrupt data
290  * @component: Pointer to the ASoC component for the Function.
291  * @function: Pointer to the SDCA Function.
292  * @entity: Pointer to the SDCA Entity.
293  * @control: Pointer to the SDCA Control.
294  * @interrupt: Pointer to the SDCA interrupt for this IRQ.
295  *
296  * Return: Zero on success, and a negative error code on failure.
297  */
298 int sdca_irq_data_populate(struct snd_soc_component *component,
299 			   struct sdca_function_data *function,
300 			   struct sdca_entity *entity,
301 			   struct sdca_control *control,
302 			   struct sdca_interrupt *interrupt)
303 {
304 	struct device *dev = component->dev;
305 	const char *name;
306 
307 	name = devm_kasprintf(dev, GFP_KERNEL, "%s %s %s", function->desc->name,
308 			      entity->label, control->label);
309 	if (!name)
310 		return -ENOMEM;
311 
312 	interrupt->name = name;
313 	interrupt->component = component;
314 	interrupt->function = function;
315 	interrupt->entity = entity;
316 	interrupt->control = control;
317 
318 	return 0;
319 }
320 EXPORT_SYMBOL_NS_GPL(sdca_irq_data_populate, "SND_SOC_SDCA");
321 
322 /**
323  * sdca_irq_populate - Request all the individual IRQs for an SDCA Function
324  * @function: Pointer to the SDCA Function.
325  * @component: Pointer to the ASoC component for the Function.
326  * @info: Pointer to the SDCA interrupt info for this device.
327  *
328  * Typically this would be called from the driver for a single SDCA Function.
329  *
330  * Return: Zero on success, and a negative error code on failure.
331  */
332 int sdca_irq_populate(struct sdca_function_data *function,
333 		      struct snd_soc_component *component,
334 		      struct sdca_interrupt_info *info)
335 {
336 	struct device *dev = component->dev;
337 	int i, j;
338 
339 	guard(mutex)(&info->irq_lock);
340 
341 	for (i = 0; i < function->num_entities; i++) {
342 		struct sdca_entity *entity = &function->entities[i];
343 
344 		for (j = 0; j < entity->num_controls; j++) {
345 			struct sdca_control *control = &entity->controls[j];
346 			int irq = control->interrupt_position;
347 			struct sdca_interrupt *interrupt;
348 			irq_handler_t handler;
349 			int ret;
350 
351 			if (irq == SDCA_NO_INTERRUPT) {
352 				continue;
353 			} else if (irq < 0 || irq >= SDCA_MAX_INTERRUPTS) {
354 				dev_err(dev, "bad irq position: %d\n", irq);
355 				return -EINVAL;
356 			}
357 
358 			interrupt = &info->irqs[irq];
359 
360 			if (interrupt->externally_requested) {
361 				dev_dbg(dev,
362 					"skipping irq %d, externally requested\n",
363 					irq);
364 				continue;
365 			}
366 
367 			ret = sdca_irq_data_populate(component, function, entity,
368 						     control, interrupt);
369 			if (ret)
370 				return ret;
371 
372 			handler = base_handler;
373 
374 			switch (entity->type) {
375 			case SDCA_ENTITY_TYPE_ENTITY_0:
376 				if (control->sel == SDCA_CTL_ENTITY_0_FUNCTION_STATUS)
377 					handler = function_status_handler;
378 				break;
379 			case SDCA_ENTITY_TYPE_GE:
380 				if (control->sel == SDCA_CTL_GE_DETECTED_MODE)
381 					handler = detected_mode_handler;
382 				break;
383 			default:
384 				break;
385 			}
386 
387 			ret = sdca_irq_request_locked(dev, info, irq, interrupt->name,
388 						      handler, interrupt);
389 			if (ret) {
390 				dev_err(dev, "failed to request irq %s: %d\n",
391 					interrupt->name, ret);
392 				return ret;
393 			}
394 		}
395 	}
396 
397 	return 0;
398 }
399 EXPORT_SYMBOL_NS_GPL(sdca_irq_populate, "SND_SOC_SDCA");
400 
401 /**
402  * sdca_irq_allocate - allocate an SDCA interrupt structure for a device
403  * @dev: Device pointer against which things should be allocated.
404  * @regmap: regmap to be used for accessing the SDCA IRQ registers.
405  * @irq: The interrupt number.
406  *
407  * Typically this would be called from the top level driver for the whole
408  * SDCA device, as only a single instance is required across all Functions
409  * on the device.
410  *
411  * Return: A pointer to the allocated sdca_interrupt_info struct, or an
412  * error code.
413  */
414 struct sdca_interrupt_info *sdca_irq_allocate(struct device *dev,
415 					      struct regmap *regmap, int irq)
416 {
417 	struct sdca_interrupt_info *info;
418 	int ret;
419 
420 	info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
421 	if (!info)
422 		return ERR_PTR(-ENOMEM);
423 
424 	info->irq_chip = sdca_irq_chip;
425 
426 	ret = devm_mutex_init(dev, &info->irq_lock);
427 	if (ret)
428 		return ERR_PTR(ret);
429 
430 	ret = devm_regmap_add_irq_chip(dev, regmap, irq, IRQF_ONESHOT, 0,
431 				       &info->irq_chip, &info->irq_data);
432 	if (ret) {
433 		dev_err(dev, "failed to register irq chip: %d\n", ret);
434 		return ERR_PTR(ret);
435 	}
436 
437 	dev_dbg(dev, "registered on irq %d\n", irq);
438 
439 	return info;
440 }
441 EXPORT_SYMBOL_NS_GPL(sdca_irq_allocate, "SND_SOC_SDCA");
442 
443 MODULE_LICENSE("GPL");
444 MODULE_DESCRIPTION("SDCA IRQ library");
445