xref: /linux/sound/soc/sdca/sdca_asoc.c (revision cdff2e6ab8908f523474931e6f986022f045fb3f)
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/bits.h>
11 #include <linux/bitmap.h>
12 #include <linux/build_bug.h>
13 #include <linux/delay.h>
14 #include <linux/dev_printk.h>
15 #include <linux/device.h>
16 #include <linux/minmax.h>
17 #include <linux/module.h>
18 #include <linux/overflow.h>
19 #include <linux/pm_runtime.h>
20 #include <linux/regmap.h>
21 #include <linux/soundwire/sdw_registers.h>
22 #include <linux/string_helpers.h>
23 #include <linux/types.h>
24 #include <sound/control.h>
25 #include <sound/pcm.h>
26 #include <sound/pcm_params.h>
27 #include <sound/sdca.h>
28 #include <sound/sdca_asoc.h>
29 #include <sound/sdca_function.h>
30 #include <sound/soc.h>
31 #include <sound/soc-component.h>
32 #include <sound/soc-dai.h>
33 #include <sound/soc-dapm.h>
34 #include <sound/tlv.h>
35 
36 static bool exported_control(struct sdca_entity *entity, struct sdca_control *control)
37 {
38 	switch (SDCA_CTL_TYPE(entity->type, control->sel)) {
39 	case SDCA_CTL_TYPE_S(GE, DETECTED_MODE):
40 		return true;
41 	default:
42 		break;
43 	}
44 
45 	return control->layers & (SDCA_ACCESS_LAYER_USER |
46 				  SDCA_ACCESS_LAYER_APPLICATION);
47 }
48 
49 static bool readonly_control(struct sdca_control *control)
50 {
51 	return control->has_fixed || control->mode == SDCA_ACCESS_MODE_RO;
52 }
53 
54 static int ge_count_routes(struct sdca_entity *entity)
55 {
56 	int count = 0;
57 	int i, j;
58 
59 	for (i = 0; i < entity->ge.num_modes; i++) {
60 		struct sdca_ge_mode *mode = &entity->ge.modes[i];
61 
62 		for (j = 0; j < mode->num_controls; j++) {
63 			struct sdca_ge_control *affected = &mode->controls[j];
64 
65 			if (affected->sel != SDCA_CTL_SU_SELECTOR || affected->val)
66 				count++;
67 		}
68 	}
69 
70 	return count;
71 }
72 
73 /**
74  * sdca_asoc_count_component - count the various component parts
75  * @dev: Pointer to the device against which allocations will be done.
76  * @function: Pointer to the Function information.
77  * @num_widgets: Output integer pointer, will be filled with the
78  * required number of DAPM widgets for the Function.
79  * @num_routes: Output integer pointer, will be filled with the
80  * required number of DAPM routes for the Function.
81  * @num_controls: Output integer pointer, will be filled with the
82  * required number of ALSA controls for the Function.
83  * @num_dais: Output integer pointer, will be filled with the
84  * required number of ASoC DAIs for the Function.
85  *
86  * This function counts various things within the SDCA Function such
87  * that the calling driver can allocate appropriate space before
88  * calling the appropriate population functions.
89  *
90  * Return: Returns zero on success, and a negative error code on failure.
91  */
92 int sdca_asoc_count_component(struct device *dev, struct sdca_function_data *function,
93 			      int *num_widgets, int *num_routes, int *num_controls,
94 			      int *num_dais)
95 {
96 	struct sdca_control *control;
97 	int i, j;
98 
99 	*num_widgets = function->num_entities - 1;
100 	*num_routes = 0;
101 	*num_controls = 0;
102 	*num_dais = 0;
103 
104 	for (i = 0; i < function->num_entities - 1; i++) {
105 		struct sdca_entity *entity = &function->entities[i];
106 		bool skip_primary_routes = false;
107 
108 		/* Add supply/DAI widget connections */
109 		switch (entity->type) {
110 		case SDCA_ENTITY_TYPE_IT:
111 		case SDCA_ENTITY_TYPE_OT:
112 			*num_routes += !!entity->iot.clock;
113 			*num_routes += !!entity->iot.is_dataport;
114 			*num_controls += !entity->iot.is_dataport;
115 			*num_dais += !!entity->iot.is_dataport;
116 			break;
117 		case SDCA_ENTITY_TYPE_PDE:
118 			*num_routes += entity->pde.num_managed;
119 			break;
120 		case SDCA_ENTITY_TYPE_GE:
121 			*num_routes += ge_count_routes(entity);
122 			skip_primary_routes = true;
123 			break;
124 		case SDCA_ENTITY_TYPE_SU:
125 			control = sdca_selector_find_control(dev, entity, SDCA_CTL_SU_SELECTOR);
126 			if (!control)
127 				return -EINVAL;
128 
129 			skip_primary_routes = (control->layers == SDCA_ACCESS_LAYER_DEVICE);
130 			break;
131 		default:
132 			break;
133 		}
134 
135 		if (entity->group)
136 			(*num_routes)++;
137 
138 		/* Add primary entity connections from DisCo */
139 		if (!skip_primary_routes)
140 			*num_routes += entity->num_sources;
141 
142 		for (j = 0; j < entity->num_controls; j++) {
143 			if (exported_control(entity, &entity->controls[j]))
144 				(*num_controls)++;
145 		}
146 	}
147 
148 	return 0;
149 }
150 EXPORT_SYMBOL_NS(sdca_asoc_count_component, "SND_SOC_SDCA");
151 
152 static int ge_put_enum_double(struct snd_kcontrol *kcontrol,
153 			      struct snd_ctl_elem_value *ucontrol)
154 {
155 	struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_to_dapm(kcontrol);
156 	struct snd_soc_component *component = snd_soc_dapm_to_component(dapm);
157 	struct device *dev = component->dev;
158 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
159 	unsigned int *item = ucontrol->value.enumerated.item;
160 	unsigned int reg = e->reg;
161 	int ret;
162 
163 	reg &= ~SDW_SDCA_CTL_CSEL(0x3F);
164 	reg |= SDW_SDCA_CTL_CSEL(SDCA_CTL_GE_DETECTED_MODE);
165 
166 	ret = pm_runtime_resume_and_get(dev);
167 	if (ret < 0) {
168 		dev_err(dev, "failed to resume writing %s: %d\n",
169 			kcontrol->id.name, ret);
170 		return ret;
171 	}
172 
173 	ret = snd_soc_component_read(component, reg);
174 	pm_runtime_put(dev);
175 	if (ret < 0)
176 		return ret;
177 	else if (ret <= SDCA_DETECTED_MODE_DETECTION_IN_PROGRESS)
178 		return -EBUSY;
179 
180 	ret = snd_soc_enum_item_to_val(e, item[0]);
181 	if (ret <= SDCA_DETECTED_MODE_DETECTION_IN_PROGRESS)
182 		return -EINVAL;
183 
184 	return snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
185 }
186 
187 static int entity_early_parse_ge(struct device *dev,
188 				 struct sdca_function_data *function,
189 				 struct sdca_entity *entity)
190 {
191 	struct sdca_control_range *range;
192 	struct sdca_control *control;
193 	struct snd_kcontrol_new *kctl;
194 	struct soc_enum *soc_enum;
195 	const char *control_name;
196 	unsigned int *values;
197 	const char **texts;
198 	int i;
199 
200 	control = sdca_selector_find_control(dev, entity, SDCA_CTL_GE_SELECTED_MODE);
201 	if (!control)
202 		return -EINVAL;
203 
204 	if (control->layers != SDCA_ACCESS_LAYER_CLASS)
205 		dev_warn(dev, "%s: unexpected access layer: %x\n",
206 			 entity->label, control->layers);
207 
208 	range = sdca_control_find_range(dev, entity, control, SDCA_SELECTED_MODE_NCOLS, 0);
209 	if (!range)
210 		return -EINVAL;
211 
212 	control_name = devm_kasprintf(dev, GFP_KERNEL, "%s %s",
213 				      entity->label, control->label);
214 	if (!control_name)
215 		return -ENOMEM;
216 
217 	kctl = devm_kzalloc(dev, sizeof(*kctl), GFP_KERNEL);
218 	if (!kctl)
219 		return -ENOMEM;
220 
221 	soc_enum = devm_kzalloc(dev, sizeof(*soc_enum), GFP_KERNEL);
222 	if (!soc_enum)
223 		return -ENOMEM;
224 
225 	texts = devm_kcalloc(dev, range->rows + 3, sizeof(*texts), GFP_KERNEL);
226 	if (!texts)
227 		return -ENOMEM;
228 
229 	values = devm_kcalloc(dev, range->rows + 3, sizeof(*values), GFP_KERNEL);
230 	if (!values)
231 		return -ENOMEM;
232 
233 	texts[0] = "Jack Unplugged";
234 	texts[1] = "Jack Unknown";
235 	texts[2] = "Detection in Progress";
236 	values[0] = SDCA_DETECTED_MODE_JACK_UNPLUGGED;
237 	values[1] = SDCA_DETECTED_MODE_JACK_UNKNOWN;
238 	values[2] = SDCA_DETECTED_MODE_DETECTION_IN_PROGRESS;
239 	for (i = 0; i < range->rows; i++) {
240 		enum sdca_terminal_type type;
241 
242 		type = sdca_range(range, SDCA_SELECTED_MODE_TERM_TYPE, i);
243 
244 		values[i + 3] = sdca_range(range, SDCA_SELECTED_MODE_INDEX, i);
245 		texts[i + 3] = sdca_find_terminal_name(type);
246 		if (!texts[i + 3]) {
247 			dev_err(dev, "%s: unrecognised terminal type: %#x\n",
248 				entity->label, type);
249 			return -EINVAL;
250 		}
251 	}
252 
253 	soc_enum->reg = SDW_SDCA_CTL(function->desc->adr, entity->id, control->sel, 0);
254 	soc_enum->items = range->rows + 3;
255 	soc_enum->mask = roundup_pow_of_two(soc_enum->items) - 1;
256 	soc_enum->texts = texts;
257 	soc_enum->values = values;
258 
259 	kctl->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
260 	kctl->name = control_name;
261 	kctl->info = snd_soc_info_enum_double;
262 	kctl->get = snd_soc_dapm_get_enum_double;
263 	kctl->put = ge_put_enum_double;
264 	kctl->private_value = (unsigned long)soc_enum;
265 
266 	entity->ge.kctl = kctl;
267 
268 	return 0;
269 }
270 
271 static void add_route(struct snd_soc_dapm_route **route, const char *sink,
272 		      const char *control, const char *source)
273 {
274 	(*route)->sink = sink;
275 	(*route)->control = control;
276 	(*route)->source = source;
277 	(*route)++;
278 }
279 
280 static int entity_parse_simple(struct device *dev,
281 			       struct sdca_function_data *function,
282 			       struct sdca_entity *entity,
283 			       struct snd_soc_dapm_widget **widget,
284 			       struct snd_soc_dapm_route **route,
285 			       enum snd_soc_dapm_type id)
286 {
287 	int i;
288 
289 	(*widget)->id = id;
290 	(*widget)++;
291 
292 	for (i = 0; i < entity->num_sources; i++)
293 		add_route(route, entity->label, NULL, entity->sources[i]->label);
294 
295 	return 0;
296 }
297 
298 static int entity_parse_it(struct device *dev,
299 			   struct sdca_function_data *function,
300 			   struct sdca_entity *entity,
301 			   struct snd_soc_dapm_widget **widget,
302 			   struct snd_soc_dapm_route **route)
303 {
304 	int i;
305 
306 	if (entity->iot.is_dataport) {
307 		const char *aif_name = devm_kasprintf(dev, GFP_KERNEL, "%s %s",
308 						      entity->label, "Playback");
309 		if (!aif_name)
310 			return -ENOMEM;
311 
312 		(*widget)->id = snd_soc_dapm_aif_in;
313 
314 		add_route(route, entity->label, NULL, aif_name);
315 	} else {
316 		(*widget)->id = snd_soc_dapm_mic;
317 	}
318 
319 	if (entity->iot.clock)
320 		add_route(route, entity->label, NULL, entity->iot.clock->label);
321 
322 	for (i = 0; i < entity->num_sources; i++)
323 		add_route(route, entity->label, NULL, entity->sources[i]->label);
324 
325 	(*widget)++;
326 
327 	return 0;
328 }
329 
330 static int entity_parse_ot(struct device *dev,
331 			   struct sdca_function_data *function,
332 			   struct sdca_entity *entity,
333 			   struct snd_soc_dapm_widget **widget,
334 			   struct snd_soc_dapm_route **route)
335 {
336 	int i;
337 
338 	if (entity->iot.is_dataport) {
339 		const char *aif_name = devm_kasprintf(dev, GFP_KERNEL, "%s %s",
340 						      entity->label, "Capture");
341 		if (!aif_name)
342 			return -ENOMEM;
343 
344 		(*widget)->id = snd_soc_dapm_aif_out;
345 
346 		add_route(route, aif_name, NULL, entity->label);
347 	} else {
348 		(*widget)->id = snd_soc_dapm_spk;
349 	}
350 
351 	if (entity->iot.clock)
352 		add_route(route, entity->label, NULL, entity->iot.clock->label);
353 
354 	for (i = 0; i < entity->num_sources; i++)
355 		add_route(route, entity->label, NULL, entity->sources[i]->label);
356 
357 	(*widget)++;
358 
359 	return 0;
360 }
361 
362 /**
363  * sdca_asoc_pde_poll_actual_ps - Verify PDE power state reached target state
364  * @dev: Pointer to the device for error logging.
365  * @regmap: Register map for reading ACTUAL_PS register.
366  * @function_id: SDCA function identifier.
367  * @entity_id: SDCA entity identifier for the power domain.
368  * @from_ps: Source power state (SDCA_PDE_PSn value).
369  * @to_ps: Target power state (SDCA_PDE_PSn value).
370  * @pde_delays: Pointer to array of PDE delay specifications for this device,
371  *              or NULL to use default polling interval.
372  * @num_delays: Number of entries in pde_delays array.
373  *
374  * This function polls the ACTUAL_PS register to verify that a PDE power state
375  * transition has completed. Per SDCA specification, after writing REQUESTED_PS,
376  * the caller must poll ACTUAL_PS until it reflects the requested state.
377  *
378  * This function implements the polling logic but does NOT modify the power state.
379  * The caller is responsible for writing REQUESTED_PS before invoking this function.
380  *
381  * If a delay table is provided, appropriate polling intervals are extracted based
382  * on the from_ps and to_ps transition. If no table is provided or no matching entry
383  * is found, a default polling interval is used.
384  *
385  * Return: Returns zero when ACTUAL_PS reaches the target state, -ETIMEDOUT if the
386  * polling times out before reaching the target state, or a negative error code if
387  * a register read fails.
388  */
389 int sdca_asoc_pde_poll_actual_ps(struct device *dev, struct regmap *regmap,
390 				 int function_id, int entity_id,
391 			    int from_ps, int to_ps,
392 			    const struct sdca_pde_delay *pde_delays,
393 			    int num_delays)
394 {
395 	static const int polls = 100;
396 	static const int default_poll_us = 1000;
397 	unsigned int reg, val;
398 	int i, poll_us = default_poll_us;
399 	int ret;
400 
401 	if (pde_delays && num_delays > 0) {
402 		for (i = 0; i < num_delays; i++) {
403 			if (pde_delays[i].from_ps == from_ps && pde_delays[i].to_ps == to_ps) {
404 				poll_us = pde_delays[i].us / polls;
405 				break;
406 			}
407 		}
408 	}
409 
410 	reg = SDW_SDCA_CTL(function_id, entity_id, SDCA_CTL_PDE_ACTUAL_PS, 0);
411 
412 	for (i = 0; i < polls; i++) {
413 		if (i)
414 			fsleep(poll_us);
415 
416 		ret = regmap_read(regmap, reg, &val);
417 		if (ret)
418 			return ret;
419 		else if (val == to_ps)
420 			return 0;
421 	}
422 
423 	return -ETIMEDOUT;
424 }
425 EXPORT_SYMBOL_NS(sdca_asoc_pde_poll_actual_ps, "SND_SOC_SDCA");
426 
427 static int entity_pde_event(struct snd_soc_dapm_widget *widget,
428 			    struct snd_kcontrol *kctl, int event)
429 {
430 	struct snd_soc_component *component = snd_soc_dapm_to_component(widget->dapm);
431 	struct sdca_entity *entity = widget->priv;
432 	int from, to;
433 	int ret;
434 
435 	if (!component)
436 		return -EIO;
437 
438 	switch (event) {
439 	case SND_SOC_DAPM_POST_PMD:
440 		from = widget->on_val;
441 		to = widget->off_val;
442 		break;
443 	case SND_SOC_DAPM_POST_PMU:
444 		from = widget->off_val;
445 		to = widget->on_val;
446 		break;
447 	default:
448 		return 0;
449 	}
450 
451 	ret = sdca_asoc_pde_poll_actual_ps(component->dev, component->regmap,
452 					   SDW_SDCA_CTL_FUNC(widget->reg),
453 					   SDW_SDCA_CTL_ENT(widget->reg),
454 					   from, to,
455 					   entity->pde.max_delay,
456 					   entity->pde.num_max_delay);
457 	if (ret)
458 		dev_err(component->dev, "%s: PDE transition %x -> %x failed, err=%d\n",
459 			entity->label, from, to, ret);
460 
461 	return ret;
462 }
463 
464 static int entity_parse_pde(struct device *dev,
465 			    struct sdca_function_data *function,
466 			    struct sdca_entity *entity,
467 			    struct snd_soc_dapm_widget **widget,
468 			    struct snd_soc_dapm_route **route)
469 {
470 	unsigned int target = (1 << SDCA_PDE_PS0) | (1 << SDCA_PDE_PS3);
471 	struct sdca_control_range *range;
472 	struct sdca_control *control;
473 	unsigned int mask = 0;
474 	int i;
475 
476 	control = sdca_selector_find_control(dev, entity, SDCA_CTL_PDE_REQUESTED_PS);
477 	if (!control)
478 		return -EINVAL;
479 
480 	/* Power should only be controlled by the driver */
481 	if (control->layers != SDCA_ACCESS_LAYER_CLASS)
482 		dev_warn(dev, "%s: unexpected access layer: %x\n",
483 			 entity->label, control->layers);
484 
485 	range = sdca_control_find_range(dev, entity, control, SDCA_REQUESTED_PS_NCOLS, 0);
486 	if (!range)
487 		return -EINVAL;
488 
489 	for (i = 0; i < range->rows; i++)
490 		mask |= 1 << sdca_range(range, SDCA_REQUESTED_PS_STATE, i);
491 
492 	if ((mask & target) != target) {
493 		dev_err(dev, "%s: power control missing states\n", entity->label);
494 		return -EINVAL;
495 	}
496 
497 	(*widget)->id = snd_soc_dapm_supply;
498 	(*widget)->reg = SDW_SDCA_CTL(function->desc->adr, entity->id, control->sel, 0);
499 	(*widget)->mask = GENMASK(control->nbits - 1, 0);
500 	(*widget)->on_val = SDCA_PDE_PS0;
501 	(*widget)->off_val = SDCA_PDE_PS3;
502 	(*widget)->event_flags = SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD;
503 	(*widget)->event = entity_pde_event;
504 	(*widget)->priv = entity;
505 	(*widget)++;
506 
507 	for (i = 0; i < entity->pde.num_managed; i++)
508 		add_route(route, entity->pde.managed[i]->label, NULL, entity->label);
509 
510 	for (i = 0; i < entity->num_sources; i++)
511 		add_route(route, entity->label, NULL, entity->sources[i]->label);
512 
513 	return 0;
514 }
515 
516 /* Device selector units are controlled through a group entity */
517 static int entity_parse_su_device(struct device *dev,
518 				  struct sdca_function_data *function,
519 				  struct sdca_entity *entity,
520 				  struct snd_soc_dapm_widget **widget,
521 				  struct snd_soc_dapm_route **route)
522 {
523 	struct sdca_control_range *range;
524 	int i, j;
525 
526 	if (!entity->group) {
527 		dev_err(dev, "%s: device selector unit missing group\n", entity->label);
528 		return -EINVAL;
529 	}
530 
531 	range = sdca_selector_find_range(dev, entity->group, SDCA_CTL_GE_SELECTED_MODE,
532 					 SDCA_SELECTED_MODE_NCOLS, 0);
533 	if (!range)
534 		return -EINVAL;
535 
536 	(*widget)->id = snd_soc_dapm_mux_named_ctl;
537 	(*widget)->kcontrol_news = entity->group->ge.kctl;
538 	(*widget)->num_kcontrols = 1;
539 	(*widget)++;
540 
541 	for (i = 0; i < entity->group->ge.num_modes; i++) {
542 		struct sdca_ge_mode *mode = &entity->group->ge.modes[i];
543 
544 		for (j = 0; j < mode->num_controls; j++) {
545 			struct sdca_ge_control *affected = &mode->controls[j];
546 			int term;
547 
548 			if (affected->id != entity->id ||
549 			    affected->sel != SDCA_CTL_SU_SELECTOR ||
550 			    !affected->val)
551 				continue;
552 
553 			if (affected->val - 1 >= entity->num_sources) {
554 				dev_err(dev, "%s: bad control value: %#x\n",
555 					entity->label, affected->val);
556 				return -EINVAL;
557 			}
558 
559 			term = sdca_range_search(range, SDCA_SELECTED_MODE_INDEX,
560 						 mode->val, SDCA_SELECTED_MODE_TERM_TYPE);
561 			if (!term) {
562 				dev_err(dev, "%s: mode not found: %#x\n",
563 					entity->label, mode->val);
564 				return -EINVAL;
565 			}
566 
567 			add_route(route, entity->label, sdca_find_terminal_name(term),
568 				  entity->sources[affected->val - 1]->label);
569 		}
570 	}
571 
572 	return 0;
573 }
574 
575 /* Class selector units will be exported as an ALSA control */
576 static int entity_parse_su_class(struct device *dev,
577 				 struct sdca_function_data *function,
578 				 struct sdca_entity *entity,
579 				 struct sdca_control *control,
580 				 struct snd_soc_dapm_widget **widget,
581 				 struct snd_soc_dapm_route **route)
582 {
583 	struct snd_kcontrol_new *kctl;
584 	struct soc_enum *soc_enum;
585 	const char **texts;
586 	int i;
587 
588 	kctl = devm_kzalloc(dev, sizeof(*kctl), GFP_KERNEL);
589 	if (!kctl)
590 		return -ENOMEM;
591 
592 	soc_enum = devm_kzalloc(dev, sizeof(*soc_enum), GFP_KERNEL);
593 	if (!soc_enum)
594 		return -ENOMEM;
595 
596 	texts = devm_kcalloc(dev, entity->num_sources + 1, sizeof(*texts), GFP_KERNEL);
597 	if (!texts)
598 		return -ENOMEM;
599 
600 	texts[0] = "No Signal";
601 	for (i = 0; i < entity->num_sources; i++)
602 		texts[i + 1] = entity->sources[i]->label;
603 
604 	soc_enum->reg = SDW_SDCA_CTL(function->desc->adr, entity->id, control->sel, 0);
605 	soc_enum->items = entity->num_sources + 1;
606 	soc_enum->mask = roundup_pow_of_two(soc_enum->items) - 1;
607 	soc_enum->texts = texts;
608 
609 	kctl->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
610 	kctl->name = "Route";
611 	kctl->info = snd_soc_info_enum_double;
612 	kctl->get = snd_soc_dapm_get_enum_double;
613 	kctl->put = snd_soc_dapm_put_enum_double;
614 	kctl->private_value = (unsigned long)soc_enum;
615 
616 	(*widget)->id = snd_soc_dapm_mux;
617 	(*widget)->kcontrol_news = kctl;
618 	(*widget)->num_kcontrols = 1;
619 	(*widget)++;
620 
621 	for (i = 0; i < entity->num_sources; i++)
622 		add_route(route, entity->label, texts[i + 1], entity->sources[i]->label);
623 
624 	return 0;
625 }
626 
627 static int entity_parse_su(struct device *dev,
628 			   struct sdca_function_data *function,
629 			   struct sdca_entity *entity,
630 			   struct snd_soc_dapm_widget **widget,
631 			   struct snd_soc_dapm_route **route)
632 {
633 	struct sdca_control *control;
634 
635 	if (!entity->num_sources) {
636 		dev_err(dev, "%s: selector with no inputs\n", entity->label);
637 		return -EINVAL;
638 	}
639 
640 	control = sdca_selector_find_control(dev, entity, SDCA_CTL_SU_SELECTOR);
641 	if (!control)
642 		return -EINVAL;
643 
644 	if (control->layers == SDCA_ACCESS_LAYER_DEVICE)
645 		return entity_parse_su_device(dev, function, entity, widget, route);
646 
647 	if (control->layers != SDCA_ACCESS_LAYER_CLASS)
648 		dev_warn(dev, "%s: unexpected access layer: %x\n",
649 			 entity->label, control->layers);
650 
651 	return entity_parse_su_class(dev, function, entity, control, widget, route);
652 }
653 
654 static int entity_parse_mu(struct device *dev,
655 			   struct sdca_function_data *function,
656 			   struct sdca_entity *entity,
657 			   struct snd_soc_dapm_widget **widget,
658 			   struct snd_soc_dapm_route **route)
659 {
660 	struct sdca_control *control;
661 	struct snd_kcontrol_new *kctl;
662 	int i;
663 
664 	if (!entity->num_sources) {
665 		dev_err(dev, "%s: selector 1 or more inputs\n", entity->label);
666 		return -EINVAL;
667 	}
668 
669 	control = sdca_selector_find_control(dev, entity, SDCA_CTL_MU_MIXER);
670 	if (!control)
671 		return -EINVAL;
672 
673 	/* MU control should be through DAPM */
674 	if (control->layers != SDCA_ACCESS_LAYER_CLASS)
675 		dev_warn(dev, "%s: unexpected access layer: %x\n",
676 			 entity->label, control->layers);
677 
678 	kctl = devm_kcalloc(dev, entity->num_sources, sizeof(*kctl), GFP_KERNEL);
679 	if (!kctl)
680 		return -ENOMEM;
681 
682 	for (i = 0; i < entity->num_sources; i++) {
683 		const char *control_name;
684 		struct soc_mixer_control *mc;
685 
686 		control_name = devm_kasprintf(dev, GFP_KERNEL, "%s %d",
687 					      control->label, i + 1);
688 		if (!control_name)
689 			return -ENOMEM;
690 
691 		mc = devm_kzalloc(dev, sizeof(*mc), GFP_KERNEL);
692 		if (!mc)
693 			return -ENOMEM;
694 
695 		mc->reg = SND_SOC_NOPM;
696 		mc->rreg = SND_SOC_NOPM;
697 		mc->invert = 1; // Ensure default is connected
698 		mc->min = 0;
699 		mc->max = 1;
700 
701 		kctl[i].name = control_name;
702 		kctl[i].private_value = (unsigned long)mc;
703 		kctl[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
704 		kctl[i].info = snd_soc_info_volsw;
705 		kctl[i].get = snd_soc_dapm_get_volsw;
706 		kctl[i].put = snd_soc_dapm_put_volsw;
707 	}
708 
709 	(*widget)->id = snd_soc_dapm_mixer;
710 	(*widget)->kcontrol_news = kctl;
711 	(*widget)->num_kcontrols = entity->num_sources;
712 	(*widget)++;
713 
714 	for (i = 0; i < entity->num_sources; i++)
715 		add_route(route, entity->label, kctl[i].name, entity->sources[i]->label);
716 
717 	return 0;
718 }
719 
720 static int entity_cs_event(struct snd_soc_dapm_widget *widget,
721 			   struct snd_kcontrol *kctl, int event)
722 {
723 	struct snd_soc_component *component = snd_soc_dapm_to_component(widget->dapm);
724 	struct sdca_entity *entity = widget->priv;
725 
726 	if (!component)
727 		return -EIO;
728 
729 	if (entity->cs.max_delay)
730 		fsleep(entity->cs.max_delay);
731 
732 	return 0;
733 }
734 
735 static int entity_parse_cs(struct device *dev,
736 			   struct sdca_function_data *function,
737 			   struct sdca_entity *entity,
738 			   struct snd_soc_dapm_widget **widget,
739 			   struct snd_soc_dapm_route **route)
740 {
741 	int i;
742 
743 	(*widget)->id = snd_soc_dapm_supply;
744 	(*widget)->subseq = 1; /* Ensure these run after PDEs */
745 	(*widget)->event_flags = SND_SOC_DAPM_POST_PMU;
746 	(*widget)->event = entity_cs_event;
747 	(*widget)->priv = entity;
748 	(*widget)++;
749 
750 	for (i = 0; i < entity->num_sources; i++)
751 		add_route(route, entity->label, NULL, entity->sources[i]->label);
752 
753 	return 0;
754 }
755 
756 /**
757  * sdca_asoc_populate_dapm - fill in arrays of DAPM widgets and routes
758  * @dev: Pointer to the device against which allocations will be done.
759  * @function: Pointer to the Function information.
760  * @widget: Array of DAPM widgets to be populated.
761  * @route: Array of DAPM routes to be populated.
762  *
763  * This function populates arrays of DAPM widgets and routes from the
764  * DisCo information for a particular SDCA Function. Typically,
765  * snd_soc_asoc_count_component will be used to allocate appropriately
766  * sized arrays before calling this function.
767  *
768  * Return: Returns zero on success, and a negative error code on failure.
769  */
770 int sdca_asoc_populate_dapm(struct device *dev, struct sdca_function_data *function,
771 			    struct snd_soc_dapm_widget *widget,
772 			    struct snd_soc_dapm_route *route)
773 {
774 	int ret;
775 	int i;
776 
777 	for (i = 0; i < function->num_entities - 1; i++) {
778 		struct sdca_entity *entity = &function->entities[i];
779 
780 		/*
781 		 * Some entities need to add controls "early" as they are
782 		 * referenced by other entities.
783 		 */
784 		switch (entity->type) {
785 		case SDCA_ENTITY_TYPE_GE:
786 			ret = entity_early_parse_ge(dev, function, entity);
787 			if (ret)
788 				return ret;
789 			break;
790 		default:
791 			break;
792 		}
793 	}
794 
795 	for (i = 0; i < function->num_entities - 1; i++) {
796 		struct sdca_entity *entity = &function->entities[i];
797 
798 		widget->name = entity->label;
799 		widget->reg = SND_SOC_NOPM;
800 
801 		switch (entity->type) {
802 		case SDCA_ENTITY_TYPE_IT:
803 			ret = entity_parse_it(dev, function, entity, &widget, &route);
804 			break;
805 		case SDCA_ENTITY_TYPE_OT:
806 			ret = entity_parse_ot(dev, function, entity, &widget, &route);
807 			break;
808 		case SDCA_ENTITY_TYPE_PDE:
809 			ret = entity_parse_pde(dev, function, entity, &widget, &route);
810 			break;
811 		case SDCA_ENTITY_TYPE_SU:
812 			ret = entity_parse_su(dev, function, entity, &widget, &route);
813 			break;
814 		case SDCA_ENTITY_TYPE_MU:
815 			ret = entity_parse_mu(dev, function, entity, &widget, &route);
816 			break;
817 		case SDCA_ENTITY_TYPE_CS:
818 			ret = entity_parse_cs(dev, function, entity, &widget, &route);
819 			break;
820 		case SDCA_ENTITY_TYPE_CX:
821 			/*
822 			 * FIXME: For now we will just treat these as a supply,
823 			 * meaning all options are enabled.
824 			 */
825 			dev_warn(dev, "%s: clock selectors not fully supported yet\n",
826 				 entity->label);
827 			ret = entity_parse_simple(dev, function, entity, &widget,
828 						  &route, snd_soc_dapm_supply);
829 			break;
830 		case SDCA_ENTITY_TYPE_TG:
831 			ret = entity_parse_simple(dev, function, entity, &widget,
832 						  &route, snd_soc_dapm_siggen);
833 			break;
834 		case SDCA_ENTITY_TYPE_GE:
835 			ret = entity_parse_simple(dev, function, entity, &widget,
836 						  &route, snd_soc_dapm_supply);
837 			break;
838 		default:
839 			ret = entity_parse_simple(dev, function, entity, &widget,
840 						  &route, snd_soc_dapm_pga);
841 			break;
842 		}
843 		if (ret)
844 			return ret;
845 
846 		if (entity->group)
847 			add_route(&route, entity->label, NULL, entity->group->label);
848 	}
849 
850 	return 0;
851 }
852 EXPORT_SYMBOL_NS(sdca_asoc_populate_dapm, "SND_SOC_SDCA");
853 
854 static int q78_write(struct snd_soc_component *component,
855 		     struct soc_mixer_control *mc,
856 		     unsigned int reg, const int val)
857 {
858 	unsigned int mask = GENMASK(mc->sign_bit, 0);
859 	unsigned int reg_val;
860 
861 	if (val < 0 || val > mc->max - mc->min)
862 		return -EINVAL;
863 
864 	reg_val = (val + mc->min) * mc->shift;
865 
866 	return snd_soc_component_update_bits(component, reg, mask, reg_val);
867 }
868 
869 int sdca_asoc_q78_put_volsw(struct snd_kcontrol *kcontrol,
870 			    struct snd_ctl_elem_value *ucontrol)
871 {
872 	struct soc_mixer_control *mc = (struct soc_mixer_control *)kcontrol->private_value;
873 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
874 	int ret;
875 
876 	ret = q78_write(component, mc, mc->reg, ucontrol->value.integer.value[0]);
877 	if (ret < 0)
878 		return ret;
879 
880 	if (snd_soc_volsw_is_stereo(mc)) {
881 		int err; /* Don't drop change flag */
882 
883 		err = q78_write(component, mc, mc->rreg, ucontrol->value.integer.value[1]);
884 		if (err)
885 			return err;
886 	}
887 
888 	return ret;
889 }
890 EXPORT_SYMBOL_NS(sdca_asoc_q78_put_volsw, "SND_SOC_SDCA");
891 
892 static int q78_read(struct snd_soc_component *component,
893 		    struct soc_mixer_control *mc, unsigned int reg)
894 {
895 	unsigned int reg_val;
896 	int val;
897 
898 	reg_val = snd_soc_component_read(component, reg);
899 
900 	val = (sign_extend32(reg_val, mc->sign_bit) / (int)mc->shift) - mc->min;
901 
902 	return val & GENMASK(mc->sign_bit, 0);
903 }
904 
905 int sdca_asoc_q78_get_volsw(struct snd_kcontrol *kcontrol,
906 			    struct snd_ctl_elem_value *ucontrol)
907 {
908 	struct soc_mixer_control *mc = (struct soc_mixer_control *)kcontrol->private_value;
909 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
910 
911 	ucontrol->value.integer.value[0] = q78_read(component, mc, mc->reg);
912 
913 	if (snd_soc_volsw_is_stereo(mc))
914 		ucontrol->value.integer.value[1] = q78_read(component, mc, mc->rreg);
915 
916 	return 0;
917 }
918 EXPORT_SYMBOL_NS(sdca_asoc_q78_get_volsw, "SND_SOC_SDCA");
919 
920 static int control_limit_kctl(struct device *dev,
921 			      struct sdca_entity *entity,
922 			      struct sdca_control *control,
923 			      struct snd_kcontrol_new *kctl)
924 {
925 	struct soc_mixer_control *mc = (struct soc_mixer_control *)kctl->private_value;
926 	struct sdca_control_range *range;
927 	int min, max, step;
928 	unsigned int *tlv;
929 
930 	if (control->type != SDCA_CTL_DATATYPE_Q7P8DB)
931 		return 0;
932 
933 	/*
934 	 * FIXME: For now only handle the simple case of a single linear range
935 	 */
936 	range = sdca_control_find_range(dev, entity, control, SDCA_VOLUME_LINEAR_NCOLS, 1);
937 	if (!range)
938 		return -EINVAL;
939 
940 	min = sdca_range(range, SDCA_VOLUME_LINEAR_MIN, 0);
941 	max = sdca_range(range, SDCA_VOLUME_LINEAR_MAX, 0);
942 	step = sdca_range(range, SDCA_VOLUME_LINEAR_STEP, 0);
943 
944 	min = sign_extend32(min, control->nbits - 1);
945 	max = sign_extend32(max, control->nbits - 1);
946 
947 	tlv = devm_kcalloc(dev, 4, sizeof(*tlv), GFP_KERNEL);
948 	if (!tlv)
949 		return -ENOMEM;
950 
951 	tlv[0] = SNDRV_CTL_TLVT_DB_MINMAX;
952 	tlv[1] = 2 * sizeof(*tlv);
953 	tlv[2] = (min * 100) >> 8;
954 	tlv[3] = (max * 100) >> 8;
955 
956 	mc->min = min / step;
957 	mc->max = max / step;
958 	mc->shift = step;
959 	mc->sign_bit = 15;
960 
961 	kctl->tlv.p = tlv;
962 	kctl->access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
963 	kctl->get = sdca_asoc_q78_get_volsw;
964 	kctl->put = sdca_asoc_q78_put_volsw;
965 
966 	return 0;
967 }
968 
969 static int volatile_get_volsw(struct snd_kcontrol *kcontrol,
970 			      struct snd_ctl_elem_value *ucontrol)
971 {
972 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
973 	struct device *dev = component->dev;
974 	int ret;
975 
976 	ret = pm_runtime_resume_and_get(dev);
977 	if (ret < 0) {
978 		dev_err(dev, "failed to resume reading %s: %d\n",
979 			kcontrol->id.name, ret);
980 		return ret;
981 	}
982 
983 	ret = snd_soc_get_volsw(kcontrol, ucontrol);
984 
985 	pm_runtime_put(dev);
986 
987 	return ret;
988 }
989 
990 static int volatile_put_volsw(struct snd_kcontrol *kcontrol,
991 			      struct snd_ctl_elem_value *ucontrol)
992 {
993 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
994 	struct device *dev = component->dev;
995 	int ret;
996 
997 	ret = pm_runtime_resume_and_get(dev);
998 	if (ret < 0) {
999 		dev_err(dev, "failed to resume writing %s: %d\n",
1000 			kcontrol->id.name, ret);
1001 		return ret;
1002 	}
1003 
1004 	ret = snd_soc_put_volsw(kcontrol, ucontrol);
1005 
1006 	pm_runtime_put(dev);
1007 
1008 	return ret;
1009 }
1010 
1011 static int populate_control(struct device *dev,
1012 			    struct sdca_function_data *function,
1013 			    struct sdca_entity *entity,
1014 			    struct sdca_control *control,
1015 			    struct snd_kcontrol_new **kctl)
1016 {
1017 	const char *control_suffix = "";
1018 	const char *control_name;
1019 	struct soc_mixer_control *mc;
1020 	int index = 0;
1021 	int ret;
1022 	int cn;
1023 
1024 	if (!exported_control(entity, control))
1025 		return 0;
1026 
1027 	if (control->type == SDCA_CTL_DATATYPE_ONEBIT)
1028 		control_suffix = " Switch";
1029 
1030 	control_name = devm_kasprintf(dev, GFP_KERNEL, "%s %s%s", entity->label,
1031 				      control->label, control_suffix);
1032 	if (!control_name)
1033 		return -ENOMEM;
1034 
1035 	mc = devm_kzalloc(dev, sizeof(*mc), GFP_KERNEL);
1036 	if (!mc)
1037 		return -ENOMEM;
1038 
1039 	for_each_set_bit(cn, (unsigned long *)&control->cn_list,
1040 			 BITS_PER_TYPE(control->cn_list)) {
1041 		switch (index++) {
1042 		case 0:
1043 			mc->reg = SDW_SDCA_CTL(function->desc->adr, entity->id,
1044 					       control->sel, cn);
1045 			mc->rreg = mc->reg;
1046 			break;
1047 		case 1:
1048 			mc->rreg = SDW_SDCA_CTL(function->desc->adr, entity->id,
1049 						control->sel, cn);
1050 			break;
1051 		default:
1052 			dev_err(dev, "%s: %s: only mono/stereo controls supported\n",
1053 				entity->label, control->label);
1054 			return -EINVAL;
1055 		}
1056 	}
1057 
1058 	mc->min = 0;
1059 	mc->max = clamp((0x1ull << control->nbits) - 1, 0, type_max(mc->max));
1060 
1061 	if (SDCA_CTL_TYPE(entity->type, control->sel) == SDCA_CTL_TYPE_S(FU, MUTE))
1062 		mc->invert = true;
1063 
1064 	(*kctl)->name = control_name;
1065 	(*kctl)->private_value = (unsigned long)mc;
1066 	(*kctl)->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1067 	(*kctl)->info = snd_soc_info_volsw;
1068 	if (control->is_volatile) {
1069 		(*kctl)->get = volatile_get_volsw;
1070 		(*kctl)->put = volatile_put_volsw;
1071 	} else {
1072 		(*kctl)->get = snd_soc_get_volsw;
1073 		(*kctl)->put = snd_soc_put_volsw;
1074 	}
1075 
1076 	if (readonly_control(control))
1077 		(*kctl)->access = SNDRV_CTL_ELEM_ACCESS_READ;
1078 	else
1079 		(*kctl)->access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1080 
1081 	ret = control_limit_kctl(dev, entity, control, *kctl);
1082 	if (ret)
1083 		return ret;
1084 
1085 	(*kctl)++;
1086 
1087 	return 0;
1088 }
1089 
1090 static int populate_pin_switch(struct device *dev,
1091 			       struct sdca_entity *entity,
1092 			       struct snd_kcontrol_new **kctl)
1093 {
1094 	const char *control_name;
1095 
1096 	control_name = devm_kasprintf(dev, GFP_KERNEL, "%s Switch", entity->label);
1097 	if (!control_name)
1098 		return -ENOMEM;
1099 
1100 	(*kctl)->name = control_name;
1101 	(*kctl)->private_value = (unsigned long)entity->label;
1102 	(*kctl)->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1103 	(*kctl)->info = snd_soc_dapm_info_pin_switch;
1104 	(*kctl)->get = snd_soc_dapm_get_component_pin_switch;
1105 	(*kctl)->put = snd_soc_dapm_put_component_pin_switch;
1106 	(*kctl)++;
1107 
1108 	return 0;
1109 }
1110 
1111 /**
1112  * sdca_asoc_populate_controls - fill in an array of ALSA controls for a Function
1113  * @dev: Pointer to the device against which allocations will be done.
1114  * @function: Pointer to the Function information.
1115  * @kctl: Array of ALSA controls to be populated.
1116  *
1117  * This function populates an array of ALSA controls from the DisCo
1118  * information for a particular SDCA Function. Typically,
1119  * snd_soc_asoc_count_component will be used to allocate an
1120  * appropriately sized array before calling this function.
1121  *
1122  * Return: Returns zero on success, and a negative error code on failure.
1123  */
1124 int sdca_asoc_populate_controls(struct device *dev,
1125 				struct sdca_function_data *function,
1126 				struct snd_kcontrol_new *kctl)
1127 {
1128 	int i, j;
1129 	int ret;
1130 
1131 	for (i = 0; i < function->num_entities; i++) {
1132 		struct sdca_entity *entity = &function->entities[i];
1133 
1134 		switch (entity->type) {
1135 		case SDCA_ENTITY_TYPE_IT:
1136 		case SDCA_ENTITY_TYPE_OT:
1137 			if (!entity->iot.is_dataport) {
1138 				ret = populate_pin_switch(dev, entity, &kctl);
1139 				if (ret)
1140 					return ret;
1141 			}
1142 			break;
1143 		default:
1144 			break;
1145 		}
1146 
1147 		for (j = 0; j < entity->num_controls; j++) {
1148 			ret = populate_control(dev, function, entity,
1149 					       &entity->controls[j], &kctl);
1150 			if (ret)
1151 				return ret;
1152 		}
1153 	}
1154 
1155 	return 0;
1156 }
1157 EXPORT_SYMBOL_NS(sdca_asoc_populate_controls, "SND_SOC_SDCA");
1158 
1159 static unsigned int rate_find_mask(unsigned int rate)
1160 {
1161 	switch (rate) {
1162 	case 0:
1163 		return SNDRV_PCM_RATE_8000_768000;
1164 	case 5512:
1165 		return SNDRV_PCM_RATE_5512;
1166 	case 8000:
1167 		return SNDRV_PCM_RATE_8000;
1168 	case 11025:
1169 		return SNDRV_PCM_RATE_11025;
1170 	case 16000:
1171 		return SNDRV_PCM_RATE_16000;
1172 	case 22050:
1173 		return SNDRV_PCM_RATE_22050;
1174 	case 32000:
1175 		return SNDRV_PCM_RATE_32000;
1176 	case 44100:
1177 		return SNDRV_PCM_RATE_44100;
1178 	case 48000:
1179 		return SNDRV_PCM_RATE_48000;
1180 	case 64000:
1181 		return SNDRV_PCM_RATE_64000;
1182 	case 88200:
1183 		return SNDRV_PCM_RATE_88200;
1184 	case 96000:
1185 		return SNDRV_PCM_RATE_96000;
1186 	case 176400:
1187 		return SNDRV_PCM_RATE_176400;
1188 	case 192000:
1189 		return SNDRV_PCM_RATE_192000;
1190 	case 352800:
1191 		return SNDRV_PCM_RATE_352800;
1192 	case 384000:
1193 		return SNDRV_PCM_RATE_384000;
1194 	case 705600:
1195 		return SNDRV_PCM_RATE_705600;
1196 	case 768000:
1197 		return SNDRV_PCM_RATE_768000;
1198 	case 12000:
1199 		return SNDRV_PCM_RATE_12000;
1200 	case 24000:
1201 		return SNDRV_PCM_RATE_24000;
1202 	case 128000:
1203 		return SNDRV_PCM_RATE_128000;
1204 	default:
1205 		return 0;
1206 	}
1207 }
1208 
1209 static u64 width_find_mask(unsigned int bits)
1210 {
1211 	switch (bits) {
1212 	case 0:
1213 		return SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE |
1214 		       SNDRV_PCM_FMTBIT_S20_LE | SNDRV_PCM_FMTBIT_S24_LE |
1215 		       SNDRV_PCM_FMTBIT_S32_LE;
1216 	case 8:
1217 		return SNDRV_PCM_FMTBIT_S8;
1218 	case 16:
1219 		return SNDRV_PCM_FMTBIT_S16_LE;
1220 	case 20:
1221 		return SNDRV_PCM_FMTBIT_S20_LE;
1222 	case 24:
1223 		return SNDRV_PCM_FMTBIT_S24_LE;
1224 	case 32:
1225 		return SNDRV_PCM_FMTBIT_S32_LE;
1226 	default:
1227 		return 0;
1228 	}
1229 }
1230 
1231 static int populate_rate_format(struct device *dev,
1232 				struct sdca_function_data *function,
1233 				struct sdca_entity *entity,
1234 				struct snd_soc_pcm_stream *stream)
1235 {
1236 	struct sdca_control_range *range;
1237 	unsigned int sample_rate, sample_width;
1238 	unsigned int clock_rates = 0;
1239 	unsigned int rates = 0;
1240 	u64 formats = 0;
1241 	int sel, i;
1242 
1243 	switch (entity->type) {
1244 	case SDCA_ENTITY_TYPE_IT:
1245 		sel = SDCA_CTL_IT_USAGE;
1246 		break;
1247 	case SDCA_ENTITY_TYPE_OT:
1248 		sel = SDCA_CTL_OT_USAGE;
1249 		break;
1250 	default:
1251 		dev_err(dev, "%s: entity type has no usage control\n",
1252 			entity->label);
1253 		return -EINVAL;
1254 	}
1255 
1256 	if (entity->iot.clock) {
1257 		range = sdca_selector_find_range(dev, entity->iot.clock,
1258 						 SDCA_CTL_CS_SAMPLERATEINDEX,
1259 						 SDCA_SAMPLERATEINDEX_NCOLS, 0);
1260 		if (!range)
1261 			return -EINVAL;
1262 
1263 		for (i = 0; i < range->rows; i++) {
1264 			sample_rate = sdca_range(range, SDCA_SAMPLERATEINDEX_RATE, i);
1265 			clock_rates |= rate_find_mask(sample_rate);
1266 		}
1267 	} else {
1268 		clock_rates = UINT_MAX;
1269 	}
1270 
1271 	range = sdca_selector_find_range(dev, entity, sel, SDCA_USAGE_NCOLS, 0);
1272 	if (!range)
1273 		return -EINVAL;
1274 
1275 	for (i = 0; i < range->rows; i++) {
1276 		sample_rate = sdca_range(range, SDCA_USAGE_SAMPLE_RATE, i);
1277 		sample_rate = rate_find_mask(sample_rate);
1278 
1279 		if (sample_rate & clock_rates) {
1280 			rates |= sample_rate;
1281 
1282 			sample_width = sdca_range(range, SDCA_USAGE_SAMPLE_WIDTH, i);
1283 			formats |= width_find_mask(sample_width);
1284 		}
1285 	}
1286 
1287 	stream->formats = formats;
1288 	stream->rates = rates;
1289 
1290 	return 0;
1291 }
1292 
1293 /**
1294  * sdca_asoc_populate_dais - fill in an array of DAI drivers for a Function
1295  * @dev: Pointer to the device against which allocations will be done.
1296  * @function: Pointer to the Function information.
1297  * @dais: Array of DAI drivers to be populated.
1298  * @ops: DAI ops to be attached to each of the created DAI drivers.
1299  *
1300  * This function populates an array of ASoC DAI drivers from the DisCo
1301  * information for a particular SDCA Function. Typically,
1302  * snd_soc_asoc_count_component will be used to allocate an
1303  * appropriately sized array before calling this function.
1304  *
1305  * Return: Returns zero on success, and a negative error code on failure.
1306  */
1307 int sdca_asoc_populate_dais(struct device *dev, struct sdca_function_data *function,
1308 			    struct snd_soc_dai_driver *dais,
1309 			    const struct snd_soc_dai_ops *ops)
1310 {
1311 	int i, j;
1312 	int ret;
1313 
1314 	for (i = 0, j = 0; i < function->num_entities - 1; i++) {
1315 		struct sdca_entity *entity = &function->entities[i];
1316 		struct snd_soc_pcm_stream *stream;
1317 		const char *stream_suffix;
1318 
1319 		switch (entity->type) {
1320 		case SDCA_ENTITY_TYPE_IT:
1321 			stream = &dais[j].playback;
1322 			stream_suffix = "Playback";
1323 			break;
1324 		case SDCA_ENTITY_TYPE_OT:
1325 			stream = &dais[j].capture;
1326 			stream_suffix = "Capture";
1327 			break;
1328 		default:
1329 			continue;
1330 		}
1331 
1332 		/* Can't check earlier as only terminals have an iot member. */
1333 		if (!entity->iot.is_dataport)
1334 			continue;
1335 
1336 		stream->stream_name = devm_kasprintf(dev, GFP_KERNEL, "%s %s",
1337 						     entity->label, stream_suffix);
1338 		if (!stream->stream_name)
1339 			return -ENOMEM;
1340 		/* Channels will be further limited by constraints */
1341 		stream->channels_min = 1;
1342 		stream->channels_max = SDCA_MAX_CHANNEL_COUNT;
1343 
1344 		ret = populate_rate_format(dev, function, entity, stream);
1345 		if (ret)
1346 			return ret;
1347 
1348 		dais[j].id = i;
1349 		dais[j].name = entity->label;
1350 		dais[j].ops = ops;
1351 		j++;
1352 	}
1353 
1354 	return 0;
1355 }
1356 EXPORT_SYMBOL_NS(sdca_asoc_populate_dais, "SND_SOC_SDCA");
1357 
1358 /**
1359  * sdca_asoc_populate_component - fill in a component driver for a Function
1360  * @dev: Pointer to the device against which allocations will be done.
1361  * @function: Pointer to the Function information.
1362  * @component_drv: Pointer to the component driver to be populated.
1363  * @dai_drv: Pointer to the DAI driver array to be allocated and populated.
1364  * @num_dai_drv: Pointer to integer that will be populated with the number of
1365  * DAI drivers.
1366  * @ops: DAI ops pointer that will be used for each DAI driver.
1367  *
1368  * This function populates a snd_soc_component_driver structure based
1369  * on the DisCo information for a particular SDCA Function. It does
1370  * all allocation internally.
1371  *
1372  * Return: Returns zero on success, and a negative error code on failure.
1373  */
1374 int sdca_asoc_populate_component(struct device *dev,
1375 				 struct sdca_function_data *function,
1376 				 struct snd_soc_component_driver *component_drv,
1377 				 struct snd_soc_dai_driver **dai_drv, int *num_dai_drv,
1378 				 const struct snd_soc_dai_ops *ops)
1379 {
1380 	struct snd_soc_dapm_widget *widgets;
1381 	struct snd_soc_dapm_route *routes;
1382 	struct snd_kcontrol_new *controls;
1383 	struct snd_soc_dai_driver *dais;
1384 	int num_widgets, num_routes, num_controls, num_dais;
1385 	int ret;
1386 
1387 	ret = sdca_asoc_count_component(dev, function, &num_widgets, &num_routes,
1388 					&num_controls, &num_dais);
1389 	if (ret)
1390 		return ret;
1391 
1392 	widgets = devm_kcalloc(dev, num_widgets, sizeof(*widgets), GFP_KERNEL);
1393 	if (!widgets)
1394 		return -ENOMEM;
1395 
1396 	routes = devm_kcalloc(dev, num_routes, sizeof(*routes), GFP_KERNEL);
1397 	if (!routes)
1398 		return -ENOMEM;
1399 
1400 	controls = devm_kcalloc(dev, num_controls, sizeof(*controls), GFP_KERNEL);
1401 	if (!controls)
1402 		return -ENOMEM;
1403 
1404 	dais = devm_kcalloc(dev, num_dais, sizeof(*dais), GFP_KERNEL);
1405 	if (!dais)
1406 		return -ENOMEM;
1407 
1408 	ret = sdca_asoc_populate_dapm(dev, function, widgets, routes);
1409 	if (ret)
1410 		return ret;
1411 
1412 	ret = sdca_asoc_populate_controls(dev, function, controls);
1413 	if (ret)
1414 		return ret;
1415 
1416 	ret = sdca_asoc_populate_dais(dev, function, dais, ops);
1417 	if (ret)
1418 		return ret;
1419 
1420 	component_drv->dapm_widgets = widgets;
1421 	component_drv->num_dapm_widgets = num_widgets;
1422 	component_drv->dapm_routes = routes;
1423 	component_drv->num_dapm_routes = num_routes;
1424 	component_drv->controls = controls;
1425 	component_drv->num_controls = num_controls;
1426 
1427 	*dai_drv = dais;
1428 	*num_dai_drv = num_dais;
1429 
1430 	return 0;
1431 }
1432 EXPORT_SYMBOL_NS(sdca_asoc_populate_component, "SND_SOC_SDCA");
1433 
1434 /**
1435  * sdca_asoc_set_constraints - constrain channels available on a DAI
1436  * @dev: Pointer to the device, used for error messages.
1437  * @regmap: Pointer to the Function register map.
1438  * @function: Pointer to the Function information.
1439  * @substream: Pointer to the PCM substream.
1440  * @dai: Pointer to the ASoC DAI.
1441  *
1442  * Typically called from startup().
1443  *
1444  * Return: Returns zero on success, and a negative error code on failure.
1445  */
1446 int sdca_asoc_set_constraints(struct device *dev, struct regmap *regmap,
1447 			      struct sdca_function_data *function,
1448 			      struct snd_pcm_substream *substream,
1449 			      struct snd_soc_dai *dai)
1450 {
1451 	static const unsigned int channel_list[] = {
1452 		 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
1453 		17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
1454 	};
1455 	struct sdca_entity *entity = &function->entities[dai->id];
1456 	struct snd_pcm_hw_constraint_list *constraint;
1457 	struct sdca_control_range *range;
1458 	struct sdca_control *control;
1459 	unsigned int channel_mask = 0;
1460 	int i, ret;
1461 
1462 	static_assert(ARRAY_SIZE(channel_list) == SDCA_MAX_CHANNEL_COUNT);
1463 	static_assert(sizeof(channel_mask) * BITS_PER_BYTE >= SDCA_MAX_CHANNEL_COUNT);
1464 
1465 	if (entity->type != SDCA_ENTITY_TYPE_IT)
1466 		return 0;
1467 
1468 	control = sdca_selector_find_control(dev, entity, SDCA_CTL_IT_CLUSTERINDEX);
1469 	if (!control)
1470 		return -EINVAL;
1471 
1472 	range = sdca_control_find_range(dev, entity, control, SDCA_CLUSTER_NCOLS, 0);
1473 	if (!range)
1474 		return -EINVAL;
1475 
1476 	for (i = 0; i < range->rows; i++) {
1477 		int clusterid = sdca_range(range, SDCA_CLUSTER_CLUSTERID, i);
1478 		struct sdca_cluster *cluster;
1479 
1480 		cluster = sdca_id_find_cluster(dev, function, clusterid);
1481 		if (!cluster)
1482 			return -ENODEV;
1483 
1484 		channel_mask |= (1 << (cluster->num_channels - 1));
1485 	}
1486 
1487 	dev_dbg(dev, "%s: set channel constraint mask: %#x\n",
1488 		entity->label, channel_mask);
1489 
1490 	constraint = kzalloc_obj(*constraint);
1491 	if (!constraint)
1492 		return -ENOMEM;
1493 
1494 	constraint->count = ARRAY_SIZE(channel_list);
1495 	constraint->list = channel_list;
1496 	constraint->mask = channel_mask;
1497 
1498 	ret = snd_pcm_hw_constraint_list(substream->runtime, 0,
1499 					 SNDRV_PCM_HW_PARAM_CHANNELS,
1500 					 constraint);
1501 	if (ret) {
1502 		dev_err(dev, "%s: failed to add constraint: %d\n", entity->label, ret);
1503 		kfree(constraint);
1504 		return ret;
1505 	}
1506 
1507 	dai->priv = constraint;
1508 
1509 	return 0;
1510 }
1511 EXPORT_SYMBOL_NS(sdca_asoc_set_constraints, "SND_SOC_SDCA");
1512 
1513 /**
1514  * sdca_asoc_free_constraints - free constraint allocations
1515  * @substream: Pointer to the PCM substream.
1516  * @dai: Pointer to the ASoC DAI.
1517  *
1518  * Typically called from shutdown().
1519  */
1520 void sdca_asoc_free_constraints(struct snd_pcm_substream *substream,
1521 				struct snd_soc_dai *dai)
1522 {
1523 	struct snd_pcm_hw_constraint_list *constraint = dai->priv;
1524 
1525 	kfree(constraint);
1526 }
1527 EXPORT_SYMBOL_NS(sdca_asoc_free_constraints, "SND_SOC_SDCA");
1528 
1529 /**
1530  * sdca_asoc_get_port - return SoundWire port for a DAI
1531  * @dev: Pointer to the device, used for error messages.
1532  * @regmap: Pointer to the Function register map.
1533  * @function: Pointer to the Function information.
1534  * @dai: Pointer to the ASoC DAI.
1535  *
1536  * Typically called from hw_params().
1537  *
1538  * Return: Returns a positive port number on success, and a negative error
1539  * code on failure.
1540  */
1541 int sdca_asoc_get_port(struct device *dev, struct regmap *regmap,
1542 		       struct sdca_function_data *function,
1543 		       struct snd_soc_dai *dai)
1544 {
1545 	struct sdca_entity *entity = &function->entities[dai->id];
1546 	struct sdca_control_range *range;
1547 	unsigned int reg, val;
1548 	int sel = -EINVAL;
1549 	int i, ret;
1550 
1551 	switch (entity->type) {
1552 	case SDCA_ENTITY_TYPE_IT:
1553 		sel = SDCA_CTL_IT_DATAPORT_SELECTOR;
1554 		break;
1555 	case SDCA_ENTITY_TYPE_OT:
1556 		sel = SDCA_CTL_OT_DATAPORT_SELECTOR;
1557 		break;
1558 	default:
1559 		break;
1560 	}
1561 
1562 	if (sel < 0 || !entity->iot.is_dataport) {
1563 		dev_err(dev, "%s: port number only available for dataports\n",
1564 			entity->label);
1565 		return -EINVAL;
1566 	}
1567 
1568 	range = sdca_selector_find_range(dev, entity, sel, SDCA_DATAPORT_SELECTOR_NCOLS,
1569 					 SDCA_DATAPORT_SELECTOR_NROWS);
1570 	if (!range)
1571 		return -EINVAL;
1572 
1573 	reg = SDW_SDCA_CTL(function->desc->adr, entity->id, sel, 0);
1574 
1575 	ret = regmap_read(regmap, reg, &val);
1576 	if (ret) {
1577 		dev_err(dev, "%s: failed to read dataport selector: %d\n",
1578 			entity->label, ret);
1579 		return ret;
1580 	}
1581 
1582 	for (i = 0; i < range->rows; i++) {
1583 		static const u8 port_mask = 0xF;
1584 
1585 		sel = sdca_range(range, val & port_mask, i);
1586 
1587 		/*
1588 		 * FIXME: Currently only a single dataport is supported, so
1589 		 * return the first one found, technically up to 4 dataports
1590 		 * could be linked, but this is not yet supported.
1591 		 */
1592 		if (sel != 0xFF)
1593 			return sel;
1594 
1595 		val >>= hweight8(port_mask);
1596 	}
1597 
1598 	dev_err(dev, "%s: no dataport found\n", entity->label);
1599 	return -ENODEV;
1600 }
1601 EXPORT_SYMBOL_NS(sdca_asoc_get_port, "SND_SOC_SDCA");
1602 
1603 static int set_cluster(struct device *dev, struct regmap *regmap,
1604 		       struct sdca_function_data *function,
1605 		       struct sdca_entity *entity, unsigned int channels)
1606 {
1607 	int sel = SDCA_CTL_IT_CLUSTERINDEX;
1608 	struct sdca_control_range *range;
1609 	int i, ret;
1610 
1611 	range = sdca_selector_find_range(dev, entity, sel, SDCA_CLUSTER_NCOLS, 0);
1612 	if (!range)
1613 		return -EINVAL;
1614 
1615 	for (i = 0; i < range->rows; i++) {
1616 		int cluster_id = sdca_range(range, SDCA_CLUSTER_CLUSTERID, i);
1617 		struct sdca_cluster *cluster;
1618 
1619 		cluster = sdca_id_find_cluster(dev, function, cluster_id);
1620 		if (!cluster)
1621 			return -ENODEV;
1622 
1623 		if (cluster->num_channels == channels) {
1624 			int index = sdca_range(range, SDCA_CLUSTER_BYTEINDEX, i);
1625 			unsigned int reg = SDW_SDCA_CTL(function->desc->adr,
1626 							entity->id, sel, 0);
1627 
1628 			ret = regmap_update_bits(regmap, reg, 0xFF, index);
1629 			if (ret) {
1630 				dev_err(dev, "%s: failed to write cluster index: %d\n",
1631 					entity->label, ret);
1632 				return ret;
1633 			}
1634 
1635 			dev_dbg(dev, "%s: set cluster to %d (%d channels)\n",
1636 				entity->label, index, channels);
1637 
1638 			return 0;
1639 		}
1640 	}
1641 
1642 	dev_err(dev, "%s: no cluster for %d channels\n", entity->label, channels);
1643 	return -EINVAL;
1644 }
1645 
1646 static int set_clock(struct device *dev, struct regmap *regmap,
1647 		     struct sdca_function_data *function,
1648 		     struct sdca_entity *entity, int target_rate)
1649 {
1650 	int sel = SDCA_CTL_CS_SAMPLERATEINDEX;
1651 	struct sdca_control_range *range;
1652 	int i, ret;
1653 
1654 	range = sdca_selector_find_range(dev, entity, sel, SDCA_SAMPLERATEINDEX_NCOLS, 0);
1655 	if (!range)
1656 		return -EINVAL;
1657 
1658 	for (i = 0; i < range->rows; i++) {
1659 		unsigned int rate = sdca_range(range, SDCA_SAMPLERATEINDEX_RATE, i);
1660 
1661 		if (rate == target_rate) {
1662 			unsigned int index = sdca_range(range,
1663 							SDCA_SAMPLERATEINDEX_INDEX,
1664 							i);
1665 			unsigned int reg = SDW_SDCA_CTL(function->desc->adr,
1666 							entity->id, sel, 0);
1667 
1668 			ret = regmap_update_bits(regmap, reg, 0xFF, index);
1669 			if (ret) {
1670 				dev_err(dev, "%s: failed to write clock rate: %d\n",
1671 					entity->label, ret);
1672 				return ret;
1673 			}
1674 
1675 			dev_dbg(dev, "%s: set clock rate to %d (%dHz)\n",
1676 				entity->label, index, rate);
1677 
1678 			return 0;
1679 		}
1680 	}
1681 
1682 	dev_err(dev, "%s: no clock rate for %dHz\n", entity->label, target_rate);
1683 	return -EINVAL;
1684 }
1685 
1686 static int set_usage(struct device *dev, struct regmap *regmap,
1687 		     struct sdca_function_data *function,
1688 		     struct sdca_entity *entity, int sel,
1689 		     int target_rate, int target_width)
1690 {
1691 	struct sdca_control_range *range;
1692 	int i, ret;
1693 
1694 	range = sdca_selector_find_range(dev, entity, sel, SDCA_USAGE_NCOLS, 0);
1695 	if (!range)
1696 		return -EINVAL;
1697 
1698 	for (i = 0; i < range->rows; i++) {
1699 		unsigned int rate = sdca_range(range, SDCA_USAGE_SAMPLE_RATE, i);
1700 		unsigned int width = sdca_range(range, SDCA_USAGE_SAMPLE_WIDTH, i);
1701 
1702 		if ((!rate || rate == target_rate) && (!width || width == target_width)) {
1703 			unsigned int usage = sdca_range(range, SDCA_USAGE_NUMBER, i);
1704 			unsigned int reg = SDW_SDCA_CTL(function->desc->adr,
1705 							entity->id, sel, 0);
1706 
1707 			ret = regmap_update_bits(regmap, reg, 0xFF, usage);
1708 			if (ret) {
1709 				dev_err(dev, "%s: failed to write usage: %d\n",
1710 					entity->label, ret);
1711 				return ret;
1712 			}
1713 
1714 			dev_dbg(dev, "%s: set usage to %#x (%dHz, %d bits)\n",
1715 				entity->label, usage, target_rate, target_width);
1716 
1717 			return 0;
1718 		}
1719 	}
1720 
1721 	dev_err(dev, "%s: no usage for %dHz, %dbits\n",
1722 		entity->label, target_rate, target_width);
1723 	return -EINVAL;
1724 }
1725 
1726 /**
1727  * sdca_asoc_hw_params - set SDCA channels, sample rate and bit depth
1728  * @dev: Pointer to the device, used for error messages.
1729  * @regmap: Pointer to the Function register map.
1730  * @function: Pointer to the Function information.
1731  * @substream: Pointer to the PCM substream.
1732  * @params: Pointer to the hardware parameters.
1733  * @dai: Pointer to the ASoC DAI.
1734  *
1735  * Typically called from hw_params().
1736  *
1737  * Return: Returns zero on success, and a negative error code on failure.
1738  */
1739 int sdca_asoc_hw_params(struct device *dev, struct regmap *regmap,
1740 			struct sdca_function_data *function,
1741 			struct snd_pcm_substream *substream,
1742 			struct snd_pcm_hw_params *params,
1743 			struct snd_soc_dai *dai)
1744 {
1745 	struct sdca_entity *entity = &function->entities[dai->id];
1746 	int channels = params_channels(params);
1747 	int width = params_width(params);
1748 	int rate = params_rate(params);
1749 	int usage_sel;
1750 	int ret;
1751 
1752 	switch (entity->type) {
1753 	case SDCA_ENTITY_TYPE_IT:
1754 		ret = set_cluster(dev, regmap, function, entity, channels);
1755 		if (ret)
1756 			return ret;
1757 
1758 		usage_sel = SDCA_CTL_IT_USAGE;
1759 		break;
1760 	case SDCA_ENTITY_TYPE_OT:
1761 		usage_sel = SDCA_CTL_OT_USAGE;
1762 		break;
1763 	default:
1764 		dev_err(dev, "%s: hw_params on non-terminal entity\n", entity->label);
1765 		return -EINVAL;
1766 	}
1767 
1768 	if (entity->iot.clock) {
1769 		ret = set_clock(dev, regmap, function, entity->iot.clock, rate);
1770 		if (ret)
1771 			return ret;
1772 	}
1773 
1774 	ret = set_usage(dev, regmap, function, entity, usage_sel, rate, width);
1775 	if (ret)
1776 		return ret;
1777 
1778 	return 0;
1779 }
1780 EXPORT_SYMBOL_NS(sdca_asoc_hw_params, "SND_SOC_SDCA");
1781