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