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