Debian Codegraph @

sound/soc/sdca/sdca_asoc.c

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