1 | // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause) |
2 | // |
3 | // This file is provided under a dual BSD/GPLv2 license. When using or |
4 | // redistributing this file, you may do so under either license. |
5 | // |
6 | // Copyright(c) 2018 Intel Corporation. All rights reserved. |
7 | // |
8 | // Author: Liam Girdwood <liam.r.girdwood@linux.intel.com> |
9 | // |
10 | |
11 | #include <linux/bits.h> |
12 | #include <linux/device.h> |
13 | #include <linux/errno.h> |
14 | #include <linux/firmware.h> |
15 | #include <linux/workqueue.h> |
16 | #include <sound/tlv.h> |
17 | #include <uapi/sound/sof/tokens.h> |
18 | #include "sof-priv.h" |
19 | #include "sof-audio.h" |
20 | #include "ops.h" |
21 | |
22 | #define COMP_ID_UNASSIGNED 0xffffffff |
23 | /* |
24 | * Constants used in the computation of linear volume gain |
25 | * from dB gain 20th root of 10 in Q1.16 fixed-point notation |
26 | */ |
27 | #define VOL_TWENTIETH_ROOT_OF_TEN 73533 |
28 | /* 40th root of 10 in Q1.16 fixed-point notation*/ |
29 | #define VOL_FORTIETH_ROOT_OF_TEN 69419 |
30 | |
31 | /* 0.5 dB step value in topology TLV */ |
32 | #define VOL_HALF_DB_STEP 50 |
33 | |
34 | /* TLV data items */ |
35 | #define TLV_MIN 0 |
36 | #define TLV_STEP 1 |
37 | #define TLV_MUTE 2 |
38 | |
39 | /** |
40 | * sof_update_ipc_object - Parse multiple sets of tokens within the token array associated with the |
41 | * token ID. |
42 | * @scomp: pointer to SOC component |
43 | * @object: target IPC struct to save the parsed values |
44 | * @token_id: token ID for the token array to be searched |
45 | * @tuples: pointer to the tuples array |
46 | * @num_tuples: number of tuples in the tuples array |
47 | * @object_size: size of the object |
48 | * @token_instance_num: number of times the same @token_id needs to be parsed i.e. the function |
49 | * looks for @token_instance_num of each token in the token array associated |
50 | * with the @token_id |
51 | */ |
52 | int sof_update_ipc_object(struct snd_soc_component *scomp, void *object, enum sof_tokens token_id, |
53 | struct snd_sof_tuple *tuples, int num_tuples, |
54 | size_t object_size, int token_instance_num) |
55 | { |
56 | struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(c: scomp); |
57 | const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg); |
58 | const struct sof_token_info *token_list; |
59 | const struct sof_topology_token *tokens; |
60 | int i, j; |
61 | |
62 | token_list = tplg_ops ? tplg_ops->token_list : NULL; |
63 | /* nothing to do if token_list is NULL */ |
64 | if (!token_list) |
65 | return 0; |
66 | |
67 | if (token_list[token_id].count < 0) { |
68 | dev_err(scomp->dev, "Invalid token count for token ID: %d\n" , token_id); |
69 | return -EINVAL; |
70 | } |
71 | |
72 | /* No tokens to match */ |
73 | if (!token_list[token_id].count) |
74 | return 0; |
75 | |
76 | tokens = token_list[token_id].tokens; |
77 | if (!tokens) { |
78 | dev_err(scomp->dev, "Invalid tokens for token id: %d\n" , token_id); |
79 | return -EINVAL; |
80 | } |
81 | |
82 | for (i = 0; i < token_list[token_id].count; i++) { |
83 | int offset = 0; |
84 | int num_tokens_matched = 0; |
85 | |
86 | for (j = 0; j < num_tuples; j++) { |
87 | if (tokens[i].token == tuples[j].token) { |
88 | switch (tokens[i].type) { |
89 | case SND_SOC_TPLG_TUPLE_TYPE_WORD: |
90 | { |
91 | u32 *val = (u32 *)((u8 *)object + tokens[i].offset + |
92 | offset); |
93 | |
94 | *val = tuples[j].value.v; |
95 | break; |
96 | } |
97 | case SND_SOC_TPLG_TUPLE_TYPE_SHORT: |
98 | case SND_SOC_TPLG_TUPLE_TYPE_BOOL: |
99 | { |
100 | u16 *val = (u16 *)((u8 *)object + tokens[i].offset + |
101 | offset); |
102 | |
103 | *val = (u16)tuples[j].value.v; |
104 | break; |
105 | } |
106 | case SND_SOC_TPLG_TUPLE_TYPE_STRING: |
107 | { |
108 | if (!tokens[i].get_token) { |
109 | dev_err(scomp->dev, |
110 | "get_token not defined for token %d in %s\n" , |
111 | tokens[i].token, token_list[token_id].name); |
112 | return -EINVAL; |
113 | } |
114 | |
115 | tokens[i].get_token((void *)tuples[j].value.s, object, |
116 | tokens[i].offset + offset); |
117 | break; |
118 | } |
119 | default: |
120 | break; |
121 | } |
122 | |
123 | num_tokens_matched++; |
124 | |
125 | /* found all required sets of current token. Move to the next one */ |
126 | if (!(num_tokens_matched % token_instance_num)) |
127 | break; |
128 | |
129 | /* move to the next object */ |
130 | offset += object_size; |
131 | } |
132 | } |
133 | } |
134 | |
135 | return 0; |
136 | } |
137 | |
138 | static inline int get_tlv_data(const int *p, int tlv[SOF_TLV_ITEMS]) |
139 | { |
140 | /* we only support dB scale TLV type at the moment */ |
141 | if ((int)p[SNDRV_CTL_TLVO_TYPE] != SNDRV_CTL_TLVT_DB_SCALE) |
142 | return -EINVAL; |
143 | |
144 | /* min value in topology tlv data is multiplied by 100 */ |
145 | tlv[TLV_MIN] = (int)p[SNDRV_CTL_TLVO_DB_SCALE_MIN] / 100; |
146 | |
147 | /* volume steps */ |
148 | tlv[TLV_STEP] = (int)(p[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP] & |
149 | TLV_DB_SCALE_MASK); |
150 | |
151 | /* mute ON/OFF */ |
152 | if ((p[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP] & |
153 | TLV_DB_SCALE_MUTE) == 0) |
154 | tlv[TLV_MUTE] = 0; |
155 | else |
156 | tlv[TLV_MUTE] = 1; |
157 | |
158 | return 0; |
159 | } |
160 | |
161 | /* |
162 | * Function to truncate an unsigned 64-bit number |
163 | * by x bits and return 32-bit unsigned number. This |
164 | * function also takes care of rounding while truncating |
165 | */ |
166 | static inline u32 vol_shift_64(u64 i, u32 x) |
167 | { |
168 | /* do not truncate more than 32 bits */ |
169 | if (x > 32) |
170 | x = 32; |
171 | |
172 | if (x == 0) |
173 | return (u32)i; |
174 | |
175 | return (u32)(((i >> (x - 1)) + 1) >> 1); |
176 | } |
177 | |
178 | /* |
179 | * Function to compute a ^ exp where, |
180 | * a is a fractional number represented by a fixed-point |
181 | * integer with a fractional world length of "fwl" |
182 | * exp is an integer |
183 | * fwl is the fractional word length |
184 | * Return value is a fractional number represented by a |
185 | * fixed-point integer with a fractional word length of "fwl" |
186 | */ |
187 | static u32 vol_pow32(u32 a, int exp, u32 fwl) |
188 | { |
189 | int i, iter; |
190 | u32 power = 1 << fwl; |
191 | u64 numerator; |
192 | |
193 | /* if exponent is 0, return 1 */ |
194 | if (exp == 0) |
195 | return power; |
196 | |
197 | /* determine the number of iterations based on the exponent */ |
198 | if (exp < 0) |
199 | iter = exp * -1; |
200 | else |
201 | iter = exp; |
202 | |
203 | /* mutiply a "iter" times to compute power */ |
204 | for (i = 0; i < iter; i++) { |
205 | /* |
206 | * Product of 2 Qx.fwl fixed-point numbers yields a Q2*x.2*fwl |
207 | * Truncate product back to fwl fractional bits with rounding |
208 | */ |
209 | power = vol_shift_64(i: (u64)power * a, x: fwl); |
210 | } |
211 | |
212 | if (exp > 0) { |
213 | /* if exp is positive, return the result */ |
214 | return power; |
215 | } |
216 | |
217 | /* if exp is negative, return the multiplicative inverse */ |
218 | numerator = (u64)1 << (fwl << 1); |
219 | do_div(numerator, power); |
220 | |
221 | return (u32)numerator; |
222 | } |
223 | |
224 | /* |
225 | * Function to calculate volume gain from TLV data. |
226 | * This function can only handle gain steps that are multiples of 0.5 dB |
227 | */ |
228 | u32 vol_compute_gain(u32 value, int *tlv) |
229 | { |
230 | int dB_gain; |
231 | u32 linear_gain; |
232 | int f_step; |
233 | |
234 | /* mute volume */ |
235 | if (value == 0 && tlv[TLV_MUTE]) |
236 | return 0; |
237 | |
238 | /* |
239 | * compute dB gain from tlv. tlv_step |
240 | * in topology is multiplied by 100 |
241 | */ |
242 | dB_gain = tlv[TLV_MIN] + (value * tlv[TLV_STEP]) / 100; |
243 | |
244 | /* |
245 | * compute linear gain represented by fixed-point |
246 | * int with VOLUME_FWL fractional bits |
247 | */ |
248 | linear_gain = vol_pow32(VOL_TWENTIETH_ROOT_OF_TEN, exp: dB_gain, VOLUME_FWL); |
249 | |
250 | /* extract the fractional part of volume step */ |
251 | f_step = tlv[TLV_STEP] - (tlv[TLV_STEP] / 100); |
252 | |
253 | /* if volume step is an odd multiple of 0.5 dB */ |
254 | if (f_step == VOL_HALF_DB_STEP && (value & 1)) |
255 | linear_gain = vol_shift_64(i: (u64)linear_gain * |
256 | VOL_FORTIETH_ROOT_OF_TEN, |
257 | VOLUME_FWL); |
258 | |
259 | return linear_gain; |
260 | } |
261 | |
262 | /* |
263 | * Set up volume table for kcontrols from tlv data |
264 | * "size" specifies the number of entries in the table |
265 | */ |
266 | static int set_up_volume_table(struct snd_sof_control *scontrol, |
267 | int tlv[SOF_TLV_ITEMS], int size) |
268 | { |
269 | struct snd_soc_component *scomp = scontrol->scomp; |
270 | struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(c: scomp); |
271 | const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg); |
272 | |
273 | if (tplg_ops && tplg_ops->control && tplg_ops->control->set_up_volume_table) |
274 | return tplg_ops->control->set_up_volume_table(scontrol, tlv, size); |
275 | |
276 | dev_err(scomp->dev, "Mandatory op %s not set\n" , __func__); |
277 | return -EINVAL; |
278 | } |
279 | |
280 | struct sof_dai_types { |
281 | const char *name; |
282 | enum sof_ipc_dai_type type; |
283 | }; |
284 | |
285 | static const struct sof_dai_types sof_dais[] = { |
286 | {"SSP" , SOF_DAI_INTEL_SSP}, |
287 | {"HDA" , SOF_DAI_INTEL_HDA}, |
288 | {"DMIC" , SOF_DAI_INTEL_DMIC}, |
289 | {"ALH" , SOF_DAI_INTEL_ALH}, |
290 | {"SAI" , SOF_DAI_IMX_SAI}, |
291 | {"ESAI" , SOF_DAI_IMX_ESAI}, |
292 | {"ACPBT" , SOF_DAI_AMD_BT}, |
293 | {"ACPSP" , SOF_DAI_AMD_SP}, |
294 | {"ACPDMIC" , SOF_DAI_AMD_DMIC}, |
295 | {"ACPHS" , SOF_DAI_AMD_HS}, |
296 | {"AFE" , SOF_DAI_MEDIATEK_AFE}, |
297 | {"ACPSP_VIRTUAL" , SOF_DAI_AMD_SP_VIRTUAL}, |
298 | {"ACPHS_VIRTUAL" , SOF_DAI_AMD_HS_VIRTUAL}, |
299 | {"MICFIL" , SOF_DAI_IMX_MICFIL}, |
300 | {"ACP_SDW" , SOF_DAI_AMD_SDW}, |
301 | |
302 | }; |
303 | |
304 | static enum sof_ipc_dai_type find_dai(const char *name) |
305 | { |
306 | int i; |
307 | |
308 | for (i = 0; i < ARRAY_SIZE(sof_dais); i++) { |
309 | if (strcmp(name, sof_dais[i].name) == 0) |
310 | return sof_dais[i].type; |
311 | } |
312 | |
313 | return SOF_DAI_INTEL_NONE; |
314 | } |
315 | |
316 | /* |
317 | * Supported Frame format types and lookup, add new ones to end of list. |
318 | */ |
319 | |
320 | struct sof_frame_types { |
321 | const char *name; |
322 | enum sof_ipc_frame frame; |
323 | }; |
324 | |
325 | static const struct sof_frame_types sof_frames[] = { |
326 | {"s16le" , SOF_IPC_FRAME_S16_LE}, |
327 | {"s24le" , SOF_IPC_FRAME_S24_4LE}, |
328 | {"s32le" , SOF_IPC_FRAME_S32_LE}, |
329 | {"float" , SOF_IPC_FRAME_FLOAT}, |
330 | }; |
331 | |
332 | static enum sof_ipc_frame find_format(const char *name) |
333 | { |
334 | int i; |
335 | |
336 | for (i = 0; i < ARRAY_SIZE(sof_frames); i++) { |
337 | if (strcmp(name, sof_frames[i].name) == 0) |
338 | return sof_frames[i].frame; |
339 | } |
340 | |
341 | /* use s32le if nothing is specified */ |
342 | return SOF_IPC_FRAME_S32_LE; |
343 | } |
344 | |
345 | int get_token_u32(void *elem, void *object, u32 offset) |
346 | { |
347 | struct snd_soc_tplg_vendor_value_elem *velem = elem; |
348 | u32 *val = (u32 *)((u8 *)object + offset); |
349 | |
350 | *val = le32_to_cpu(velem->value); |
351 | return 0; |
352 | } |
353 | |
354 | int get_token_u16(void *elem, void *object, u32 offset) |
355 | { |
356 | struct snd_soc_tplg_vendor_value_elem *velem = elem; |
357 | u16 *val = (u16 *)((u8 *)object + offset); |
358 | |
359 | *val = (u16)le32_to_cpu(velem->value); |
360 | return 0; |
361 | } |
362 | |
363 | int get_token_uuid(void *elem, void *object, u32 offset) |
364 | { |
365 | struct snd_soc_tplg_vendor_uuid_elem *velem = elem; |
366 | u8 *dst = (u8 *)object + offset; |
367 | |
368 | memcpy(dst, velem->uuid, UUID_SIZE); |
369 | |
370 | return 0; |
371 | } |
372 | |
373 | /* |
374 | * The string gets from topology will be stored in heap, the owner only |
375 | * holds a char* member point to the heap. |
376 | */ |
377 | int get_token_string(void *elem, void *object, u32 offset) |
378 | { |
379 | /* "dst" here points to the char* member of the owner */ |
380 | char **dst = (char **)((u8 *)object + offset); |
381 | |
382 | *dst = kstrdup(s: elem, GFP_KERNEL); |
383 | if (!*dst) |
384 | return -ENOMEM; |
385 | return 0; |
386 | }; |
387 | |
388 | int get_token_comp_format(void *elem, void *object, u32 offset) |
389 | { |
390 | u32 *val = (u32 *)((u8 *)object + offset); |
391 | |
392 | *val = find_format(name: (const char *)elem); |
393 | return 0; |
394 | } |
395 | |
396 | int get_token_dai_type(void *elem, void *object, u32 offset) |
397 | { |
398 | u32 *val = (u32 *)((u8 *)object + offset); |
399 | |
400 | *val = find_dai(name: (const char *)elem); |
401 | return 0; |
402 | } |
403 | |
404 | /* PCM */ |
405 | static const struct sof_topology_token stream_tokens[] = { |
406 | {SOF_TKN_STREAM_PLAYBACK_COMPATIBLE_D0I3, SND_SOC_TPLG_TUPLE_TYPE_BOOL, get_token_u16, |
407 | offsetof(struct snd_sof_pcm, stream[0].d0i3_compatible)}, |
408 | {SOF_TKN_STREAM_CAPTURE_COMPATIBLE_D0I3, SND_SOC_TPLG_TUPLE_TYPE_BOOL, get_token_u16, |
409 | offsetof(struct snd_sof_pcm, stream[1].d0i3_compatible)}, |
410 | }; |
411 | |
412 | /* Leds */ |
413 | static const struct sof_topology_token led_tokens[] = { |
414 | {SOF_TKN_MUTE_LED_USE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, |
415 | offsetof(struct snd_sof_led_control, use_led)}, |
416 | {SOF_TKN_MUTE_LED_DIRECTION, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, |
417 | offsetof(struct snd_sof_led_control, direction)}, |
418 | }; |
419 | |
420 | static const struct sof_topology_token comp_pin_tokens[] = { |
421 | {SOF_TKN_COMP_NUM_INPUT_PINS, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, |
422 | offsetof(struct snd_sof_widget, num_input_pins)}, |
423 | {SOF_TKN_COMP_NUM_OUTPUT_PINS, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, |
424 | offsetof(struct snd_sof_widget, num_output_pins)}, |
425 | }; |
426 | |
427 | static const struct sof_topology_token comp_input_pin_binding_tokens[] = { |
428 | {SOF_TKN_COMP_INPUT_PIN_BINDING_WNAME, SND_SOC_TPLG_TUPLE_TYPE_STRING, |
429 | get_token_string, 0}, |
430 | }; |
431 | |
432 | static const struct sof_topology_token comp_output_pin_binding_tokens[] = { |
433 | {SOF_TKN_COMP_OUTPUT_PIN_BINDING_WNAME, SND_SOC_TPLG_TUPLE_TYPE_STRING, |
434 | get_token_string, 0}, |
435 | }; |
436 | |
437 | /** |
438 | * sof_parse_uuid_tokens - Parse multiple sets of UUID tokens |
439 | * @scomp: pointer to soc component |
440 | * @object: target ipc struct for parsed values |
441 | * @offset: offset within the object pointer |
442 | * @tokens: array of struct sof_topology_token containing the tokens to be matched |
443 | * @num_tokens: number of tokens in tokens array |
444 | * @array: source pointer to consecutive vendor arrays in topology |
445 | * |
446 | * This function parses multiple sets of string type tokens in vendor arrays |
447 | */ |
448 | static int sof_parse_uuid_tokens(struct snd_soc_component *scomp, |
449 | void *object, size_t offset, |
450 | const struct sof_topology_token *tokens, int num_tokens, |
451 | struct snd_soc_tplg_vendor_array *array) |
452 | { |
453 | struct snd_soc_tplg_vendor_uuid_elem *elem; |
454 | int found = 0; |
455 | int i, j; |
456 | |
457 | /* parse element by element */ |
458 | for (i = 0; i < le32_to_cpu(array->num_elems); i++) { |
459 | elem = &array->uuid[i]; |
460 | |
461 | /* search for token */ |
462 | for (j = 0; j < num_tokens; j++) { |
463 | /* match token type */ |
464 | if (tokens[j].type != SND_SOC_TPLG_TUPLE_TYPE_UUID) |
465 | continue; |
466 | |
467 | /* match token id */ |
468 | if (tokens[j].token != le32_to_cpu(elem->token)) |
469 | continue; |
470 | |
471 | /* matched - now load token */ |
472 | tokens[j].get_token(elem, object, |
473 | offset + tokens[j].offset); |
474 | |
475 | found++; |
476 | } |
477 | } |
478 | |
479 | return found; |
480 | } |
481 | |
482 | /** |
483 | * sof_copy_tuples - Parse tokens and copy them to the @tuples array |
484 | * @sdev: pointer to struct snd_sof_dev |
485 | * @array: source pointer to consecutive vendor arrays in topology |
486 | * @array_size: size of @array |
487 | * @token_id: Token ID associated with a token array |
488 | * @token_instance_num: number of times the same @token_id needs to be parsed i.e. the function |
489 | * looks for @token_instance_num of each token in the token array associated |
490 | * with the @token_id |
491 | * @tuples: tuples array to copy the matched tuples to |
492 | * @tuples_size: size of @tuples |
493 | * @num_copied_tuples: pointer to the number of copied tuples in the tuples array |
494 | * |
495 | */ |
496 | static int sof_copy_tuples(struct snd_sof_dev *sdev, struct snd_soc_tplg_vendor_array *array, |
497 | int array_size, u32 token_id, int token_instance_num, |
498 | struct snd_sof_tuple *tuples, int tuples_size, int *num_copied_tuples) |
499 | { |
500 | const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg); |
501 | const struct sof_token_info *token_list; |
502 | const struct sof_topology_token *tokens; |
503 | int found = 0; |
504 | int num_tokens, asize; |
505 | int i, j; |
506 | |
507 | token_list = tplg_ops ? tplg_ops->token_list : NULL; |
508 | /* nothing to do if token_list is NULL */ |
509 | if (!token_list) |
510 | return 0; |
511 | |
512 | if (!tuples || !num_copied_tuples) { |
513 | dev_err(sdev->dev, "Invalid tuples array\n" ); |
514 | return -EINVAL; |
515 | } |
516 | |
517 | tokens = token_list[token_id].tokens; |
518 | num_tokens = token_list[token_id].count; |
519 | |
520 | if (!tokens) { |
521 | dev_err(sdev->dev, "No token array defined for token ID: %d\n" , token_id); |
522 | return -EINVAL; |
523 | } |
524 | |
525 | /* check if there's space in the tuples array for new tokens */ |
526 | if (*num_copied_tuples >= tuples_size) { |
527 | dev_err(sdev->dev, "No space in tuples array for new tokens from %s" , |
528 | token_list[token_id].name); |
529 | return -EINVAL; |
530 | } |
531 | |
532 | while (array_size > 0 && found < num_tokens * token_instance_num) { |
533 | asize = le32_to_cpu(array->size); |
534 | |
535 | /* validate asize */ |
536 | if (asize < 0) { |
537 | dev_err(sdev->dev, "Invalid array size 0x%x\n" , asize); |
538 | return -EINVAL; |
539 | } |
540 | |
541 | /* make sure there is enough data before parsing */ |
542 | array_size -= asize; |
543 | if (array_size < 0) { |
544 | dev_err(sdev->dev, "Invalid array size 0x%x\n" , asize); |
545 | return -EINVAL; |
546 | } |
547 | |
548 | /* parse element by element */ |
549 | for (i = 0; i < le32_to_cpu(array->num_elems); i++) { |
550 | /* search for token */ |
551 | for (j = 0; j < num_tokens; j++) { |
552 | /* match token type */ |
553 | if (!(tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_WORD || |
554 | tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_SHORT || |
555 | tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_BYTE || |
556 | tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_BOOL || |
557 | tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_STRING)) |
558 | continue; |
559 | |
560 | if (tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_STRING) { |
561 | struct snd_soc_tplg_vendor_string_elem *elem; |
562 | |
563 | elem = &array->string[i]; |
564 | |
565 | /* match token id */ |
566 | if (tokens[j].token != le32_to_cpu(elem->token)) |
567 | continue; |
568 | |
569 | tuples[*num_copied_tuples].token = tokens[j].token; |
570 | tuples[*num_copied_tuples].value.s = elem->string; |
571 | } else { |
572 | struct snd_soc_tplg_vendor_value_elem *elem; |
573 | |
574 | elem = &array->value[i]; |
575 | |
576 | /* match token id */ |
577 | if (tokens[j].token != le32_to_cpu(elem->token)) |
578 | continue; |
579 | |
580 | tuples[*num_copied_tuples].token = tokens[j].token; |
581 | tuples[*num_copied_tuples].value.v = |
582 | le32_to_cpu(elem->value); |
583 | } |
584 | found++; |
585 | (*num_copied_tuples)++; |
586 | |
587 | /* stop if there's no space for any more new tuples */ |
588 | if (*num_copied_tuples == tuples_size) |
589 | return 0; |
590 | } |
591 | |
592 | /* stop when we've found the required token instances */ |
593 | if (found == num_tokens * token_instance_num) |
594 | return 0; |
595 | } |
596 | |
597 | /* next array */ |
598 | array = (struct snd_soc_tplg_vendor_array *)((u8 *)array + asize); |
599 | } |
600 | |
601 | return 0; |
602 | } |
603 | |
604 | /** |
605 | * sof_parse_string_tokens - Parse multiple sets of tokens |
606 | * @scomp: pointer to soc component |
607 | * @object: target ipc struct for parsed values |
608 | * @offset: offset within the object pointer |
609 | * @tokens: array of struct sof_topology_token containing the tokens to be matched |
610 | * @num_tokens: number of tokens in tokens array |
611 | * @array: source pointer to consecutive vendor arrays in topology |
612 | * |
613 | * This function parses multiple sets of string type tokens in vendor arrays |
614 | */ |
615 | static int sof_parse_string_tokens(struct snd_soc_component *scomp, |
616 | void *object, int offset, |
617 | const struct sof_topology_token *tokens, int num_tokens, |
618 | struct snd_soc_tplg_vendor_array *array) |
619 | { |
620 | struct snd_soc_tplg_vendor_string_elem *elem; |
621 | int found = 0; |
622 | int i, j, ret; |
623 | |
624 | /* parse element by element */ |
625 | for (i = 0; i < le32_to_cpu(array->num_elems); i++) { |
626 | elem = &array->string[i]; |
627 | |
628 | /* search for token */ |
629 | for (j = 0; j < num_tokens; j++) { |
630 | /* match token type */ |
631 | if (tokens[j].type != SND_SOC_TPLG_TUPLE_TYPE_STRING) |
632 | continue; |
633 | |
634 | /* match token id */ |
635 | if (tokens[j].token != le32_to_cpu(elem->token)) |
636 | continue; |
637 | |
638 | /* matched - now load token */ |
639 | ret = tokens[j].get_token(elem->string, object, offset + tokens[j].offset); |
640 | if (ret < 0) |
641 | return ret; |
642 | |
643 | found++; |
644 | } |
645 | } |
646 | |
647 | return found; |
648 | } |
649 | |
650 | /** |
651 | * sof_parse_word_tokens - Parse multiple sets of tokens |
652 | * @scomp: pointer to soc component |
653 | * @object: target ipc struct for parsed values |
654 | * @offset: offset within the object pointer |
655 | * @tokens: array of struct sof_topology_token containing the tokens to be matched |
656 | * @num_tokens: number of tokens in tokens array |
657 | * @array: source pointer to consecutive vendor arrays in topology |
658 | * |
659 | * This function parses multiple sets of word type tokens in vendor arrays |
660 | */ |
661 | static int sof_parse_word_tokens(struct snd_soc_component *scomp, |
662 | void *object, int offset, |
663 | const struct sof_topology_token *tokens, int num_tokens, |
664 | struct snd_soc_tplg_vendor_array *array) |
665 | { |
666 | struct snd_soc_tplg_vendor_value_elem *elem; |
667 | int found = 0; |
668 | int i, j; |
669 | |
670 | /* parse element by element */ |
671 | for (i = 0; i < le32_to_cpu(array->num_elems); i++) { |
672 | elem = &array->value[i]; |
673 | |
674 | /* search for token */ |
675 | for (j = 0; j < num_tokens; j++) { |
676 | /* match token type */ |
677 | if (!(tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_WORD || |
678 | tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_SHORT || |
679 | tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_BYTE || |
680 | tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_BOOL)) |
681 | continue; |
682 | |
683 | /* match token id */ |
684 | if (tokens[j].token != le32_to_cpu(elem->token)) |
685 | continue; |
686 | |
687 | /* load token */ |
688 | tokens[j].get_token(elem, object, offset + tokens[j].offset); |
689 | |
690 | found++; |
691 | } |
692 | } |
693 | |
694 | return found; |
695 | } |
696 | |
697 | /** |
698 | * sof_parse_token_sets - Parse multiple sets of tokens |
699 | * @scomp: pointer to soc component |
700 | * @object: target ipc struct for parsed values |
701 | * @tokens: token definition array describing what tokens to parse |
702 | * @count: number of tokens in definition array |
703 | * @array: source pointer to consecutive vendor arrays in topology |
704 | * @array_size: total size of @array |
705 | * @token_instance_num: number of times the same tokens needs to be parsed i.e. the function |
706 | * looks for @token_instance_num of each token in the @tokens |
707 | * @object_size: offset to next target ipc struct with multiple sets |
708 | * |
709 | * This function parses multiple sets of tokens in vendor arrays into |
710 | * consecutive ipc structs. |
711 | */ |
712 | static int sof_parse_token_sets(struct snd_soc_component *scomp, |
713 | void *object, const struct sof_topology_token *tokens, |
714 | int count, struct snd_soc_tplg_vendor_array *array, |
715 | int array_size, int token_instance_num, size_t object_size) |
716 | { |
717 | size_t offset = 0; |
718 | int found = 0; |
719 | int total = 0; |
720 | int asize; |
721 | int ret; |
722 | |
723 | while (array_size > 0 && total < count * token_instance_num) { |
724 | asize = le32_to_cpu(array->size); |
725 | |
726 | /* validate asize */ |
727 | if (asize < 0) { /* FIXME: A zero-size array makes no sense */ |
728 | dev_err(scomp->dev, "error: invalid array size 0x%x\n" , |
729 | asize); |
730 | return -EINVAL; |
731 | } |
732 | |
733 | /* make sure there is enough data before parsing */ |
734 | array_size -= asize; |
735 | if (array_size < 0) { |
736 | dev_err(scomp->dev, "error: invalid array size 0x%x\n" , |
737 | asize); |
738 | return -EINVAL; |
739 | } |
740 | |
741 | /* call correct parser depending on type */ |
742 | switch (le32_to_cpu(array->type)) { |
743 | case SND_SOC_TPLG_TUPLE_TYPE_UUID: |
744 | found += sof_parse_uuid_tokens(scomp, object, offset, tokens, num_tokens: count, |
745 | array); |
746 | break; |
747 | case SND_SOC_TPLG_TUPLE_TYPE_STRING: |
748 | |
749 | ret = sof_parse_string_tokens(scomp, object, offset, tokens, num_tokens: count, |
750 | array); |
751 | if (ret < 0) { |
752 | dev_err(scomp->dev, "error: no memory to copy string token\n" ); |
753 | return ret; |
754 | } |
755 | |
756 | found += ret; |
757 | break; |
758 | case SND_SOC_TPLG_TUPLE_TYPE_BOOL: |
759 | case SND_SOC_TPLG_TUPLE_TYPE_BYTE: |
760 | case SND_SOC_TPLG_TUPLE_TYPE_WORD: |
761 | case SND_SOC_TPLG_TUPLE_TYPE_SHORT: |
762 | found += sof_parse_word_tokens(scomp, object, offset, tokens, num_tokens: count, |
763 | array); |
764 | break; |
765 | default: |
766 | dev_err(scomp->dev, "error: unknown token type %d\n" , |
767 | array->type); |
768 | return -EINVAL; |
769 | } |
770 | |
771 | /* next array */ |
772 | array = (struct snd_soc_tplg_vendor_array *)((u8 *)array |
773 | + asize); |
774 | |
775 | /* move to next target struct */ |
776 | if (found >= count) { |
777 | offset += object_size; |
778 | total += found; |
779 | found = 0; |
780 | } |
781 | } |
782 | |
783 | return 0; |
784 | } |
785 | |
786 | /** |
787 | * sof_parse_tokens - Parse one set of tokens |
788 | * @scomp: pointer to soc component |
789 | * @object: target ipc struct for parsed values |
790 | * @tokens: token definition array describing what tokens to parse |
791 | * @num_tokens: number of tokens in definition array |
792 | * @array: source pointer to consecutive vendor arrays in topology |
793 | * @array_size: total size of @array |
794 | * |
795 | * This function parses a single set of tokens in vendor arrays into |
796 | * consecutive ipc structs. |
797 | */ |
798 | static int sof_parse_tokens(struct snd_soc_component *scomp, void *object, |
799 | const struct sof_topology_token *tokens, int num_tokens, |
800 | struct snd_soc_tplg_vendor_array *array, |
801 | int array_size) |
802 | |
803 | { |
804 | /* |
805 | * sof_parse_tokens is used when topology contains only a single set of |
806 | * identical tuples arrays. So additional parameters to |
807 | * sof_parse_token_sets are sets = 1 (only 1 set) and |
808 | * object_size = 0 (irrelevant). |
809 | */ |
810 | return sof_parse_token_sets(scomp, object, tokens, count: num_tokens, array, |
811 | array_size, token_instance_num: 1, object_size: 0); |
812 | } |
813 | |
814 | /* |
815 | * Standard Kcontrols. |
816 | */ |
817 | |
818 | static int sof_control_load_volume(struct snd_soc_component *scomp, |
819 | struct snd_sof_control *scontrol, |
820 | struct snd_kcontrol_new *kc, |
821 | struct snd_soc_tplg_ctl_hdr *hdr) |
822 | { |
823 | struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(c: scomp); |
824 | struct snd_soc_tplg_mixer_control *mc = |
825 | container_of(hdr, struct snd_soc_tplg_mixer_control, hdr); |
826 | int tlv[SOF_TLV_ITEMS]; |
827 | unsigned int mask; |
828 | int ret; |
829 | |
830 | /* validate topology data */ |
831 | if (le32_to_cpu(mc->num_channels) > SND_SOC_TPLG_MAX_CHAN) |
832 | return -EINVAL; |
833 | |
834 | /* |
835 | * If control has more than 2 channels we need to override the info. This is because even if |
836 | * ASoC layer has defined topology's max channel count to SND_SOC_TPLG_MAX_CHAN = 8, the |
837 | * pre-defined dapm control types (and related functions) creating the actual control |
838 | * restrict the channels only to mono or stereo. |
839 | */ |
840 | if (le32_to_cpu(mc->num_channels) > 2) |
841 | kc->info = snd_sof_volume_info; |
842 | |
843 | scontrol->comp_id = sdev->next_comp_id; |
844 | scontrol->min_volume_step = le32_to_cpu(mc->min); |
845 | scontrol->max_volume_step = le32_to_cpu(mc->max); |
846 | scontrol->num_channels = le32_to_cpu(mc->num_channels); |
847 | |
848 | scontrol->max = le32_to_cpu(mc->max); |
849 | if (le32_to_cpu(mc->max) == 1) |
850 | goto skip; |
851 | |
852 | /* extract tlv data */ |
853 | if (!kc->tlv.p || get_tlv_data(p: kc->tlv.p, tlv) < 0) { |
854 | dev_err(scomp->dev, "error: invalid TLV data\n" ); |
855 | return -EINVAL; |
856 | } |
857 | |
858 | /* set up volume table */ |
859 | ret = set_up_volume_table(scontrol, tlv, le32_to_cpu(mc->max) + 1); |
860 | if (ret < 0) { |
861 | dev_err(scomp->dev, "error: setting up volume table\n" ); |
862 | return ret; |
863 | } |
864 | |
865 | skip: |
866 | /* set up possible led control from mixer private data */ |
867 | ret = sof_parse_tokens(scomp, object: &scontrol->led_ctl, tokens: led_tokens, |
868 | ARRAY_SIZE(led_tokens), array: mc->priv.array, |
869 | le32_to_cpu(mc->priv.size)); |
870 | if (ret != 0) { |
871 | dev_err(scomp->dev, "error: parse led tokens failed %d\n" , |
872 | le32_to_cpu(mc->priv.size)); |
873 | goto err; |
874 | } |
875 | |
876 | if (scontrol->led_ctl.use_led) { |
877 | mask = scontrol->led_ctl.direction ? SNDRV_CTL_ELEM_ACCESS_MIC_LED : |
878 | SNDRV_CTL_ELEM_ACCESS_SPK_LED; |
879 | scontrol->access &= ~SNDRV_CTL_ELEM_ACCESS_LED_MASK; |
880 | scontrol->access |= mask; |
881 | kc->access &= ~SNDRV_CTL_ELEM_ACCESS_LED_MASK; |
882 | kc->access |= mask; |
883 | sdev->led_present = true; |
884 | } |
885 | |
886 | dev_dbg(scomp->dev, "tplg: load kcontrol index %d chans %d\n" , |
887 | scontrol->comp_id, scontrol->num_channels); |
888 | |
889 | return 0; |
890 | |
891 | err: |
892 | if (le32_to_cpu(mc->max) > 1) |
893 | kfree(objp: scontrol->volume_table); |
894 | |
895 | return ret; |
896 | } |
897 | |
898 | static int sof_control_load_enum(struct snd_soc_component *scomp, |
899 | struct snd_sof_control *scontrol, |
900 | struct snd_kcontrol_new *kc, |
901 | struct snd_soc_tplg_ctl_hdr *hdr) |
902 | { |
903 | struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(c: scomp); |
904 | struct snd_soc_tplg_enum_control *ec = |
905 | container_of(hdr, struct snd_soc_tplg_enum_control, hdr); |
906 | |
907 | /* validate topology data */ |
908 | if (le32_to_cpu(ec->num_channels) > SND_SOC_TPLG_MAX_CHAN) |
909 | return -EINVAL; |
910 | |
911 | scontrol->comp_id = sdev->next_comp_id; |
912 | scontrol->num_channels = le32_to_cpu(ec->num_channels); |
913 | |
914 | dev_dbg(scomp->dev, "tplg: load kcontrol index %d chans %d comp_id %d\n" , |
915 | scontrol->comp_id, scontrol->num_channels, scontrol->comp_id); |
916 | |
917 | return 0; |
918 | } |
919 | |
920 | static int sof_control_load_bytes(struct snd_soc_component *scomp, |
921 | struct snd_sof_control *scontrol, |
922 | struct snd_kcontrol_new *kc, |
923 | struct snd_soc_tplg_ctl_hdr *hdr) |
924 | { |
925 | struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(c: scomp); |
926 | struct snd_soc_tplg_bytes_control *control = |
927 | container_of(hdr, struct snd_soc_tplg_bytes_control, hdr); |
928 | struct soc_bytes_ext *sbe = (struct soc_bytes_ext *)kc->private_value; |
929 | size_t priv_size = le32_to_cpu(control->priv.size); |
930 | |
931 | scontrol->max_size = sbe->max; |
932 | scontrol->comp_id = sdev->next_comp_id; |
933 | |
934 | dev_dbg(scomp->dev, "tplg: load kcontrol index %d\n" , scontrol->comp_id); |
935 | |
936 | /* copy the private data */ |
937 | if (priv_size > 0) { |
938 | scontrol->priv = kmemdup(p: control->priv.data, size: priv_size, GFP_KERNEL); |
939 | if (!scontrol->priv) |
940 | return -ENOMEM; |
941 | |
942 | scontrol->priv_size = priv_size; |
943 | } |
944 | |
945 | return 0; |
946 | } |
947 | |
948 | /* external kcontrol init - used for any driver specific init */ |
949 | static int sof_control_load(struct snd_soc_component *scomp, int index, |
950 | struct snd_kcontrol_new *kc, |
951 | struct snd_soc_tplg_ctl_hdr *hdr) |
952 | { |
953 | struct soc_mixer_control *sm; |
954 | struct soc_bytes_ext *sbe; |
955 | struct soc_enum *se; |
956 | struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(c: scomp); |
957 | struct snd_soc_dobj *dobj; |
958 | struct snd_sof_control *scontrol; |
959 | int ret; |
960 | |
961 | dev_dbg(scomp->dev, "tplg: load control type %d name : %s\n" , |
962 | hdr->type, hdr->name); |
963 | |
964 | scontrol = kzalloc(size: sizeof(*scontrol), GFP_KERNEL); |
965 | if (!scontrol) |
966 | return -ENOMEM; |
967 | |
968 | scontrol->name = kstrdup(s: hdr->name, GFP_KERNEL); |
969 | if (!scontrol->name) { |
970 | kfree(objp: scontrol); |
971 | return -ENOMEM; |
972 | } |
973 | |
974 | scontrol->scomp = scomp; |
975 | scontrol->access = kc->access; |
976 | scontrol->info_type = le32_to_cpu(hdr->ops.info); |
977 | scontrol->index = kc->index; |
978 | |
979 | switch (le32_to_cpu(hdr->ops.info)) { |
980 | case SND_SOC_TPLG_CTL_VOLSW: |
981 | case SND_SOC_TPLG_CTL_VOLSW_SX: |
982 | case SND_SOC_TPLG_CTL_VOLSW_XR_SX: |
983 | sm = (struct soc_mixer_control *)kc->private_value; |
984 | dobj = &sm->dobj; |
985 | ret = sof_control_load_volume(scomp, scontrol, kc, hdr); |
986 | break; |
987 | case SND_SOC_TPLG_CTL_BYTES: |
988 | sbe = (struct soc_bytes_ext *)kc->private_value; |
989 | dobj = &sbe->dobj; |
990 | ret = sof_control_load_bytes(scomp, scontrol, kc, hdr); |
991 | break; |
992 | case SND_SOC_TPLG_CTL_ENUM: |
993 | case SND_SOC_TPLG_CTL_ENUM_VALUE: |
994 | se = (struct soc_enum *)kc->private_value; |
995 | dobj = &se->dobj; |
996 | ret = sof_control_load_enum(scomp, scontrol, kc, hdr); |
997 | break; |
998 | case SND_SOC_TPLG_CTL_RANGE: |
999 | case SND_SOC_TPLG_CTL_STROBE: |
1000 | case SND_SOC_TPLG_DAPM_CTL_VOLSW: |
1001 | case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE: |
1002 | case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT: |
1003 | case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE: |
1004 | case SND_SOC_TPLG_DAPM_CTL_PIN: |
1005 | default: |
1006 | dev_warn(scomp->dev, "control type not supported %d:%d:%d\n" , |
1007 | hdr->ops.get, hdr->ops.put, hdr->ops.info); |
1008 | kfree(objp: scontrol->name); |
1009 | kfree(objp: scontrol); |
1010 | return 0; |
1011 | } |
1012 | |
1013 | if (ret < 0) { |
1014 | kfree(objp: scontrol->name); |
1015 | kfree(objp: scontrol); |
1016 | return ret; |
1017 | } |
1018 | |
1019 | scontrol->led_ctl.led_value = -1; |
1020 | |
1021 | dobj->private = scontrol; |
1022 | list_add(new: &scontrol->list, head: &sdev->kcontrol_list); |
1023 | return 0; |
1024 | } |
1025 | |
1026 | static int sof_control_unload(struct snd_soc_component *scomp, |
1027 | struct snd_soc_dobj *dobj) |
1028 | { |
1029 | struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(c: scomp); |
1030 | const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg); |
1031 | struct snd_sof_control *scontrol = dobj->private; |
1032 | int ret = 0; |
1033 | |
1034 | dev_dbg(scomp->dev, "tplg: unload control name : %s\n" , scontrol->name); |
1035 | |
1036 | if (tplg_ops && tplg_ops->control_free) { |
1037 | ret = tplg_ops->control_free(sdev, scontrol); |
1038 | if (ret < 0) |
1039 | dev_err(scomp->dev, "failed to free control: %s\n" , scontrol->name); |
1040 | } |
1041 | |
1042 | /* free all data before returning in case of error too */ |
1043 | kfree(objp: scontrol->ipc_control_data); |
1044 | kfree(objp: scontrol->priv); |
1045 | kfree(objp: scontrol->name); |
1046 | list_del(entry: &scontrol->list); |
1047 | kfree(objp: scontrol); |
1048 | |
1049 | return ret; |
1050 | } |
1051 | |
1052 | /* |
1053 | * DAI Topology |
1054 | */ |
1055 | |
1056 | static int sof_connect_dai_widget(struct snd_soc_component *scomp, |
1057 | struct snd_soc_dapm_widget *w, |
1058 | struct snd_soc_tplg_dapm_widget *tw, |
1059 | struct snd_sof_dai *dai) |
1060 | { |
1061 | struct snd_soc_card *card = scomp->card; |
1062 | struct snd_soc_pcm_runtime *rtd; |
1063 | struct snd_soc_dai *cpu_dai; |
1064 | int stream; |
1065 | int i; |
1066 | |
1067 | if (!w->sname) { |
1068 | dev_err(scomp->dev, "Widget %s does not have stream\n" , w->name); |
1069 | return -EINVAL; |
1070 | } |
1071 | |
1072 | if (w->id == snd_soc_dapm_dai_out) |
1073 | stream = SNDRV_PCM_STREAM_CAPTURE; |
1074 | else if (w->id == snd_soc_dapm_dai_in) |
1075 | stream = SNDRV_PCM_STREAM_PLAYBACK; |
1076 | else |
1077 | goto end; |
1078 | |
1079 | list_for_each_entry(rtd, &card->rtd_list, list) { |
1080 | /* does stream match DAI link ? */ |
1081 | if (!rtd->dai_link->stream_name || |
1082 | !strstr(rtd->dai_link->stream_name, w->sname)) |
1083 | continue; |
1084 | |
1085 | for_each_rtd_cpu_dais(rtd, i, cpu_dai) { |
1086 | /* |
1087 | * Please create DAI widget in the right order |
1088 | * to ensure BE will connect to the right DAI |
1089 | * widget. |
1090 | */ |
1091 | if (!snd_soc_dai_get_widget(dai: cpu_dai, stream)) { |
1092 | snd_soc_dai_set_widget(dai: cpu_dai, stream, widget: w); |
1093 | break; |
1094 | } |
1095 | } |
1096 | if (i == rtd->dai_link->num_cpus) { |
1097 | dev_err(scomp->dev, "error: can't find BE for DAI %s\n" , w->name); |
1098 | |
1099 | return -EINVAL; |
1100 | } |
1101 | |
1102 | dai->name = rtd->dai_link->name; |
1103 | dev_dbg(scomp->dev, "tplg: connected widget %s -> DAI link %s\n" , |
1104 | w->name, rtd->dai_link->name); |
1105 | } |
1106 | end: |
1107 | /* check we have a connection */ |
1108 | if (!dai->name) { |
1109 | dev_err(scomp->dev, "error: can't connect DAI %s stream %s\n" , |
1110 | w->name, w->sname); |
1111 | return -EINVAL; |
1112 | } |
1113 | |
1114 | return 0; |
1115 | } |
1116 | |
1117 | static void sof_disconnect_dai_widget(struct snd_soc_component *scomp, |
1118 | struct snd_soc_dapm_widget *w) |
1119 | { |
1120 | struct snd_soc_card *card = scomp->card; |
1121 | struct snd_soc_pcm_runtime *rtd; |
1122 | const char *sname = w->sname; |
1123 | struct snd_soc_dai *cpu_dai; |
1124 | int i, stream; |
1125 | |
1126 | if (!sname) |
1127 | return; |
1128 | |
1129 | if (w->id == snd_soc_dapm_dai_out) |
1130 | stream = SNDRV_PCM_STREAM_CAPTURE; |
1131 | else if (w->id == snd_soc_dapm_dai_in) |
1132 | stream = SNDRV_PCM_STREAM_PLAYBACK; |
1133 | else |
1134 | return; |
1135 | |
1136 | list_for_each_entry(rtd, &card->rtd_list, list) { |
1137 | /* does stream match DAI link ? */ |
1138 | if (!rtd->dai_link->stream_name || |
1139 | !strstr(rtd->dai_link->stream_name, sname)) |
1140 | continue; |
1141 | |
1142 | for_each_rtd_cpu_dais(rtd, i, cpu_dai) |
1143 | if (snd_soc_dai_get_widget(dai: cpu_dai, stream) == w) { |
1144 | snd_soc_dai_set_widget(dai: cpu_dai, stream, NULL); |
1145 | break; |
1146 | } |
1147 | } |
1148 | } |
1149 | |
1150 | /* bind PCM ID to host component ID */ |
1151 | static int spcm_bind(struct snd_soc_component *scomp, struct snd_sof_pcm *spcm, |
1152 | int dir) |
1153 | { |
1154 | struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(c: scomp); |
1155 | struct snd_sof_widget *host_widget; |
1156 | |
1157 | if (sdev->dspless_mode_selected) |
1158 | return 0; |
1159 | |
1160 | host_widget = snd_sof_find_swidget_sname(scomp, |
1161 | pcm_name: spcm->pcm.caps[dir].name, |
1162 | dir); |
1163 | if (!host_widget) { |
1164 | dev_err(scomp->dev, "can't find host comp to bind pcm\n" ); |
1165 | return -EINVAL; |
1166 | } |
1167 | |
1168 | spcm->stream[dir].comp_id = host_widget->comp_id; |
1169 | |
1170 | return 0; |
1171 | } |
1172 | |
1173 | static int sof_get_token_value(u32 token_id, struct snd_sof_tuple *tuples, int num_tuples) |
1174 | { |
1175 | int i; |
1176 | |
1177 | if (!tuples) |
1178 | return -EINVAL; |
1179 | |
1180 | for (i = 0; i < num_tuples; i++) { |
1181 | if (tuples[i].token == token_id) |
1182 | return tuples[i].value.v; |
1183 | } |
1184 | |
1185 | return -EINVAL; |
1186 | } |
1187 | |
1188 | static int sof_widget_parse_tokens(struct snd_soc_component *scomp, struct snd_sof_widget *swidget, |
1189 | struct snd_soc_tplg_dapm_widget *tw, |
1190 | enum sof_tokens *object_token_list, int count) |
1191 | { |
1192 | struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(c: scomp); |
1193 | const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg); |
1194 | struct snd_soc_tplg_private *private = &tw->priv; |
1195 | const struct sof_token_info *token_list; |
1196 | int num_tuples = 0; |
1197 | int ret, i; |
1198 | |
1199 | token_list = tplg_ops ? tplg_ops->token_list : NULL; |
1200 | /* nothing to do if token_list is NULL */ |
1201 | if (!token_list) |
1202 | return 0; |
1203 | |
1204 | if (count > 0 && !object_token_list) { |
1205 | dev_err(scomp->dev, "No token list for widget %s\n" , swidget->widget->name); |
1206 | return -EINVAL; |
1207 | } |
1208 | |
1209 | /* calculate max size of tuples array */ |
1210 | for (i = 0; i < count; i++) |
1211 | num_tuples += token_list[object_token_list[i]].count; |
1212 | |
1213 | /* allocate memory for tuples array */ |
1214 | swidget->tuples = kcalloc(n: num_tuples, size: sizeof(*swidget->tuples), GFP_KERNEL); |
1215 | if (!swidget->tuples) |
1216 | return -ENOMEM; |
1217 | |
1218 | /* parse token list for widget */ |
1219 | for (i = 0; i < count; i++) { |
1220 | int num_sets = 1; |
1221 | |
1222 | if (object_token_list[i] >= SOF_TOKEN_COUNT) { |
1223 | dev_err(scomp->dev, "Invalid token id %d for widget %s\n" , |
1224 | object_token_list[i], swidget->widget->name); |
1225 | ret = -EINVAL; |
1226 | goto err; |
1227 | } |
1228 | |
1229 | switch (object_token_list[i]) { |
1230 | case SOF_COMP_EXT_TOKENS: |
1231 | /* parse and save UUID in swidget */ |
1232 | ret = sof_parse_tokens(scomp, object: swidget, |
1233 | tokens: token_list[object_token_list[i]].tokens, |
1234 | num_tokens: token_list[object_token_list[i]].count, |
1235 | array: private->array, le32_to_cpu(private->size)); |
1236 | if (ret < 0) { |
1237 | dev_err(scomp->dev, "Failed parsing %s for widget %s\n" , |
1238 | token_list[object_token_list[i]].name, |
1239 | swidget->widget->name); |
1240 | goto err; |
1241 | } |
1242 | |
1243 | continue; |
1244 | case SOF_IN_AUDIO_FORMAT_TOKENS: |
1245 | num_sets = sof_get_token_value(SOF_TKN_COMP_NUM_INPUT_AUDIO_FORMATS, |
1246 | tuples: swidget->tuples, num_tuples: swidget->num_tuples); |
1247 | if (num_sets < 0) { |
1248 | dev_err(sdev->dev, "Invalid input audio format count for %s\n" , |
1249 | swidget->widget->name); |
1250 | ret = num_sets; |
1251 | goto err; |
1252 | } |
1253 | break; |
1254 | case SOF_OUT_AUDIO_FORMAT_TOKENS: |
1255 | num_sets = sof_get_token_value(SOF_TKN_COMP_NUM_OUTPUT_AUDIO_FORMATS, |
1256 | tuples: swidget->tuples, num_tuples: swidget->num_tuples); |
1257 | if (num_sets < 0) { |
1258 | dev_err(sdev->dev, "Invalid output audio format count for %s\n" , |
1259 | swidget->widget->name); |
1260 | ret = num_sets; |
1261 | goto err; |
1262 | } |
1263 | break; |
1264 | default: |
1265 | break; |
1266 | } |
1267 | |
1268 | if (num_sets > 1) { |
1269 | struct snd_sof_tuple *new_tuples; |
1270 | |
1271 | num_tuples += token_list[object_token_list[i]].count * (num_sets - 1); |
1272 | new_tuples = krealloc(objp: swidget->tuples, |
1273 | new_size: sizeof(*new_tuples) * num_tuples, GFP_KERNEL); |
1274 | if (!new_tuples) { |
1275 | ret = -ENOMEM; |
1276 | goto err; |
1277 | } |
1278 | |
1279 | swidget->tuples = new_tuples; |
1280 | } |
1281 | |
1282 | /* copy one set of tuples per token ID into swidget->tuples */ |
1283 | ret = sof_copy_tuples(sdev, array: private->array, le32_to_cpu(private->size), |
1284 | token_id: object_token_list[i], token_instance_num: num_sets, tuples: swidget->tuples, |
1285 | tuples_size: num_tuples, num_copied_tuples: &swidget->num_tuples); |
1286 | if (ret < 0) { |
1287 | dev_err(scomp->dev, "Failed parsing %s for widget %s err: %d\n" , |
1288 | token_list[object_token_list[i]].name, swidget->widget->name, ret); |
1289 | goto err; |
1290 | } |
1291 | } |
1292 | |
1293 | return 0; |
1294 | err: |
1295 | kfree(objp: swidget->tuples); |
1296 | return ret; |
1297 | } |
1298 | |
1299 | static void sof_free_pin_binding(struct snd_sof_widget *swidget, |
1300 | bool pin_type) |
1301 | { |
1302 | char **pin_binding; |
1303 | u32 num_pins; |
1304 | int i; |
1305 | |
1306 | if (pin_type == SOF_PIN_TYPE_INPUT) { |
1307 | pin_binding = swidget->input_pin_binding; |
1308 | num_pins = swidget->num_input_pins; |
1309 | } else { |
1310 | pin_binding = swidget->output_pin_binding; |
1311 | num_pins = swidget->num_output_pins; |
1312 | } |
1313 | |
1314 | if (pin_binding) { |
1315 | for (i = 0; i < num_pins; i++) |
1316 | kfree(objp: pin_binding[i]); |
1317 | } |
1318 | |
1319 | kfree(objp: pin_binding); |
1320 | } |
1321 | |
1322 | static int sof_parse_pin_binding(struct snd_sof_widget *swidget, |
1323 | struct snd_soc_tplg_private *priv, bool pin_type) |
1324 | { |
1325 | const struct sof_topology_token *pin_binding_token; |
1326 | char *pin_binding[SOF_WIDGET_MAX_NUM_PINS]; |
1327 | int token_count; |
1328 | u32 num_pins; |
1329 | char **pb; |
1330 | int ret; |
1331 | int i; |
1332 | |
1333 | if (pin_type == SOF_PIN_TYPE_INPUT) { |
1334 | num_pins = swidget->num_input_pins; |
1335 | pin_binding_token = comp_input_pin_binding_tokens; |
1336 | token_count = ARRAY_SIZE(comp_input_pin_binding_tokens); |
1337 | } else { |
1338 | num_pins = swidget->num_output_pins; |
1339 | pin_binding_token = comp_output_pin_binding_tokens; |
1340 | token_count = ARRAY_SIZE(comp_output_pin_binding_tokens); |
1341 | } |
1342 | |
1343 | memset(pin_binding, 0, SOF_WIDGET_MAX_NUM_PINS * sizeof(char *)); |
1344 | ret = sof_parse_token_sets(scomp: swidget->scomp, object: pin_binding, tokens: pin_binding_token, |
1345 | count: token_count, array: priv->array, le32_to_cpu(priv->size), |
1346 | token_instance_num: num_pins, object_size: sizeof(char *)); |
1347 | if (ret < 0) |
1348 | goto err; |
1349 | |
1350 | /* copy pin binding array to swidget only if it is defined in topology */ |
1351 | if (pin_binding[0]) { |
1352 | pb = kmemdup(p: pin_binding, size: num_pins * sizeof(char *), GFP_KERNEL); |
1353 | if (!pb) { |
1354 | ret = -ENOMEM; |
1355 | goto err; |
1356 | } |
1357 | if (pin_type == SOF_PIN_TYPE_INPUT) |
1358 | swidget->input_pin_binding = pb; |
1359 | else |
1360 | swidget->output_pin_binding = pb; |
1361 | } |
1362 | |
1363 | return 0; |
1364 | |
1365 | err: |
1366 | for (i = 0; i < num_pins; i++) |
1367 | kfree(objp: pin_binding[i]); |
1368 | |
1369 | return ret; |
1370 | } |
1371 | |
1372 | static int get_w_no_wname_in_long_name(void *elem, void *object, u32 offset) |
1373 | { |
1374 | struct snd_soc_tplg_vendor_value_elem *velem = elem; |
1375 | struct snd_soc_dapm_widget *w = object; |
1376 | |
1377 | w->no_wname_in_kcontrol_name = !!le32_to_cpu(velem->value); |
1378 | return 0; |
1379 | } |
1380 | |
1381 | static const struct sof_topology_token dapm_widget_tokens[] = { |
1382 | {SOF_TKN_COMP_NO_WNAME_IN_KCONTROL_NAME, SND_SOC_TPLG_TUPLE_TYPE_BOOL, |
1383 | get_w_no_wname_in_long_name, 0} |
1384 | }; |
1385 | |
1386 | /* external widget init - used for any driver specific init */ |
1387 | static int sof_widget_ready(struct snd_soc_component *scomp, int index, |
1388 | struct snd_soc_dapm_widget *w, |
1389 | struct snd_soc_tplg_dapm_widget *tw) |
1390 | { |
1391 | struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(c: scomp); |
1392 | const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg); |
1393 | const struct sof_ipc_tplg_widget_ops *widget_ops; |
1394 | struct snd_soc_tplg_private *priv = &tw->priv; |
1395 | enum sof_tokens *token_list = NULL; |
1396 | struct snd_sof_widget *swidget; |
1397 | struct snd_sof_dai *dai; |
1398 | int token_list_size = 0; |
1399 | int ret = 0; |
1400 | |
1401 | swidget = kzalloc(size: sizeof(*swidget), GFP_KERNEL); |
1402 | if (!swidget) |
1403 | return -ENOMEM; |
1404 | |
1405 | swidget->scomp = scomp; |
1406 | swidget->widget = w; |
1407 | swidget->comp_id = sdev->next_comp_id++; |
1408 | swidget->id = w->id; |
1409 | swidget->pipeline_id = index; |
1410 | swidget->private = NULL; |
1411 | mutex_init(&swidget->setup_mutex); |
1412 | |
1413 | ida_init(ida: &swidget->output_queue_ida); |
1414 | ida_init(ida: &swidget->input_queue_ida); |
1415 | |
1416 | ret = sof_parse_tokens(scomp, object: w, tokens: dapm_widget_tokens, ARRAY_SIZE(dapm_widget_tokens), |
1417 | array: priv->array, le32_to_cpu(priv->size)); |
1418 | if (ret < 0) { |
1419 | dev_err(scomp->dev, "failed to parse dapm widget tokens for %s\n" , |
1420 | w->name); |
1421 | goto widget_free; |
1422 | } |
1423 | |
1424 | ret = sof_parse_tokens(scomp, object: swidget, tokens: comp_pin_tokens, |
1425 | ARRAY_SIZE(comp_pin_tokens), array: priv->array, |
1426 | le32_to_cpu(priv->size)); |
1427 | if (ret < 0) { |
1428 | dev_err(scomp->dev, "failed to parse component pin tokens for %s\n" , |
1429 | w->name); |
1430 | goto widget_free; |
1431 | } |
1432 | |
1433 | if (swidget->num_input_pins > SOF_WIDGET_MAX_NUM_PINS || |
1434 | swidget->num_output_pins > SOF_WIDGET_MAX_NUM_PINS) { |
1435 | dev_err(scomp->dev, "invalid pins for %s: [input: %d, output: %d]\n" , |
1436 | swidget->widget->name, swidget->num_input_pins, swidget->num_output_pins); |
1437 | ret = -EINVAL; |
1438 | goto widget_free; |
1439 | } |
1440 | |
1441 | if (swidget->num_input_pins > 1) { |
1442 | ret = sof_parse_pin_binding(swidget, priv, SOF_PIN_TYPE_INPUT); |
1443 | /* on parsing error, pin binding is not allocated, nothing to free. */ |
1444 | if (ret < 0) { |
1445 | dev_err(scomp->dev, "failed to parse input pin binding for %s\n" , |
1446 | w->name); |
1447 | goto widget_free; |
1448 | } |
1449 | } |
1450 | |
1451 | if (swidget->num_output_pins > 1) { |
1452 | ret = sof_parse_pin_binding(swidget, priv, SOF_PIN_TYPE_OUTPUT); |
1453 | /* on parsing error, pin binding is not allocated, nothing to free. */ |
1454 | if (ret < 0) { |
1455 | dev_err(scomp->dev, "failed to parse output pin binding for %s\n" , |
1456 | w->name); |
1457 | goto widget_free; |
1458 | } |
1459 | } |
1460 | |
1461 | dev_dbg(scomp->dev, |
1462 | "tplg: widget %d (%s) is ready [type: %d, pipe: %d, pins: %d / %d, stream: %s]\n" , |
1463 | swidget->comp_id, w->name, swidget->id, index, |
1464 | swidget->num_input_pins, swidget->num_output_pins, |
1465 | strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) > 0 ? w->sname : "none" ); |
1466 | |
1467 | widget_ops = tplg_ops ? tplg_ops->widget : NULL; |
1468 | if (widget_ops) { |
1469 | token_list = widget_ops[w->id].token_list; |
1470 | token_list_size = widget_ops[w->id].token_list_size; |
1471 | } |
1472 | |
1473 | /* handle any special case widgets */ |
1474 | switch (w->id) { |
1475 | case snd_soc_dapm_dai_in: |
1476 | case snd_soc_dapm_dai_out: |
1477 | dai = kzalloc(size: sizeof(*dai), GFP_KERNEL); |
1478 | if (!dai) { |
1479 | ret = -ENOMEM; |
1480 | goto widget_free; |
1481 | } |
1482 | |
1483 | ret = sof_widget_parse_tokens(scomp, swidget, tw, object_token_list: token_list, count: token_list_size); |
1484 | if (!ret) |
1485 | ret = sof_connect_dai_widget(scomp, w, tw, dai); |
1486 | if (ret < 0) { |
1487 | kfree(objp: dai); |
1488 | break; |
1489 | } |
1490 | list_add(new: &dai->list, head: &sdev->dai_list); |
1491 | swidget->private = dai; |
1492 | break; |
1493 | case snd_soc_dapm_effect: |
1494 | /* check we have some tokens - we need at least process type */ |
1495 | if (le32_to_cpu(tw->priv.size) == 0) { |
1496 | dev_err(scomp->dev, "error: process tokens not found\n" ); |
1497 | ret = -EINVAL; |
1498 | break; |
1499 | } |
1500 | ret = sof_widget_parse_tokens(scomp, swidget, tw, object_token_list: token_list, count: token_list_size); |
1501 | break; |
1502 | case snd_soc_dapm_pga: |
1503 | if (!le32_to_cpu(tw->num_kcontrols)) { |
1504 | dev_err(scomp->dev, "invalid kcontrol count %d for volume\n" , |
1505 | tw->num_kcontrols); |
1506 | ret = -EINVAL; |
1507 | break; |
1508 | } |
1509 | |
1510 | fallthrough; |
1511 | case snd_soc_dapm_mixer: |
1512 | case snd_soc_dapm_buffer: |
1513 | case snd_soc_dapm_scheduler: |
1514 | case snd_soc_dapm_aif_out: |
1515 | case snd_soc_dapm_aif_in: |
1516 | case snd_soc_dapm_src: |
1517 | case snd_soc_dapm_asrc: |
1518 | case snd_soc_dapm_siggen: |
1519 | case snd_soc_dapm_mux: |
1520 | case snd_soc_dapm_demux: |
1521 | ret = sof_widget_parse_tokens(scomp, swidget, tw, object_token_list: token_list, count: token_list_size); |
1522 | break; |
1523 | case snd_soc_dapm_switch: |
1524 | case snd_soc_dapm_dai_link: |
1525 | case snd_soc_dapm_kcontrol: |
1526 | default: |
1527 | dev_dbg(scomp->dev, "widget type %d name %s not handled\n" , swidget->id, tw->name); |
1528 | break; |
1529 | } |
1530 | |
1531 | /* check token parsing reply */ |
1532 | if (ret < 0) { |
1533 | dev_err(scomp->dev, |
1534 | "error: failed to add widget id %d type %d name : %s stream %s\n" , |
1535 | tw->shift, swidget->id, tw->name, |
1536 | strnlen(tw->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) > 0 |
1537 | ? tw->sname : "none" ); |
1538 | goto widget_free; |
1539 | } |
1540 | |
1541 | if (sof_debug_check_flag(SOF_DBG_DISABLE_MULTICORE)) { |
1542 | swidget->core = SOF_DSP_PRIMARY_CORE; |
1543 | } else { |
1544 | int core = sof_get_token_value(SOF_TKN_COMP_CORE_ID, tuples: swidget->tuples, |
1545 | num_tuples: swidget->num_tuples); |
1546 | |
1547 | if (core >= 0) |
1548 | swidget->core = core; |
1549 | } |
1550 | |
1551 | /* bind widget to external event */ |
1552 | if (tw->event_type) { |
1553 | if (widget_ops && widget_ops[w->id].bind_event) { |
1554 | ret = widget_ops[w->id].bind_event(scomp, swidget, |
1555 | le16_to_cpu(tw->event_type)); |
1556 | if (ret) { |
1557 | dev_err(scomp->dev, "widget event binding failed for %s\n" , |
1558 | swidget->widget->name); |
1559 | goto free; |
1560 | } |
1561 | } |
1562 | } |
1563 | |
1564 | /* create and add pipeline for scheduler type widgets */ |
1565 | if (w->id == snd_soc_dapm_scheduler) { |
1566 | struct snd_sof_pipeline *spipe; |
1567 | |
1568 | spipe = kzalloc(size: sizeof(*spipe), GFP_KERNEL); |
1569 | if (!spipe) { |
1570 | ret = -ENOMEM; |
1571 | goto free; |
1572 | } |
1573 | |
1574 | spipe->pipe_widget = swidget; |
1575 | swidget->spipe = spipe; |
1576 | list_add(new: &spipe->list, head: &sdev->pipeline_list); |
1577 | } |
1578 | |
1579 | w->dobj.private = swidget; |
1580 | list_add(new: &swidget->list, head: &sdev->widget_list); |
1581 | return ret; |
1582 | free: |
1583 | kfree(objp: swidget->private); |
1584 | kfree(objp: swidget->tuples); |
1585 | widget_free: |
1586 | kfree(objp: swidget); |
1587 | return ret; |
1588 | } |
1589 | |
1590 | static int sof_route_unload(struct snd_soc_component *scomp, |
1591 | struct snd_soc_dobj *dobj) |
1592 | { |
1593 | struct snd_sof_route *sroute; |
1594 | |
1595 | sroute = dobj->private; |
1596 | if (!sroute) |
1597 | return 0; |
1598 | |
1599 | /* free sroute and its private data */ |
1600 | kfree(objp: sroute->private); |
1601 | list_del(entry: &sroute->list); |
1602 | kfree(objp: sroute); |
1603 | |
1604 | return 0; |
1605 | } |
1606 | |
1607 | static int sof_widget_unload(struct snd_soc_component *scomp, |
1608 | struct snd_soc_dobj *dobj) |
1609 | { |
1610 | struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(c: scomp); |
1611 | const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg); |
1612 | const struct sof_ipc_tplg_widget_ops *widget_ops; |
1613 | const struct snd_kcontrol_new *kc; |
1614 | struct snd_soc_dapm_widget *widget; |
1615 | struct snd_sof_control *scontrol; |
1616 | struct snd_sof_widget *swidget; |
1617 | struct soc_mixer_control *sm; |
1618 | struct soc_bytes_ext *sbe; |
1619 | struct snd_sof_dai *dai; |
1620 | struct soc_enum *se; |
1621 | int i; |
1622 | |
1623 | swidget = dobj->private; |
1624 | if (!swidget) |
1625 | return 0; |
1626 | |
1627 | widget = swidget->widget; |
1628 | |
1629 | switch (swidget->id) { |
1630 | case snd_soc_dapm_dai_in: |
1631 | case snd_soc_dapm_dai_out: |
1632 | dai = swidget->private; |
1633 | |
1634 | if (dai) |
1635 | list_del(entry: &dai->list); |
1636 | |
1637 | sof_disconnect_dai_widget(scomp, w: widget); |
1638 | |
1639 | break; |
1640 | case snd_soc_dapm_scheduler: |
1641 | { |
1642 | struct snd_sof_pipeline *spipe = swidget->spipe; |
1643 | |
1644 | list_del(entry: &spipe->list); |
1645 | kfree(objp: spipe); |
1646 | swidget->spipe = NULL; |
1647 | break; |
1648 | } |
1649 | default: |
1650 | break; |
1651 | } |
1652 | for (i = 0; i < widget->num_kcontrols; i++) { |
1653 | kc = &widget->kcontrol_news[i]; |
1654 | switch (widget->dobj.widget.kcontrol_type[i]) { |
1655 | case SND_SOC_TPLG_TYPE_MIXER: |
1656 | sm = (struct soc_mixer_control *)kc->private_value; |
1657 | scontrol = sm->dobj.private; |
1658 | if (sm->max > 1) |
1659 | kfree(objp: scontrol->volume_table); |
1660 | break; |
1661 | case SND_SOC_TPLG_TYPE_ENUM: |
1662 | se = (struct soc_enum *)kc->private_value; |
1663 | scontrol = se->dobj.private; |
1664 | break; |
1665 | case SND_SOC_TPLG_TYPE_BYTES: |
1666 | sbe = (struct soc_bytes_ext *)kc->private_value; |
1667 | scontrol = sbe->dobj.private; |
1668 | break; |
1669 | default: |
1670 | dev_warn(scomp->dev, "unsupported kcontrol_type\n" ); |
1671 | goto out; |
1672 | } |
1673 | kfree(objp: scontrol->ipc_control_data); |
1674 | list_del(entry: &scontrol->list); |
1675 | kfree(objp: scontrol->name); |
1676 | kfree(objp: scontrol); |
1677 | } |
1678 | |
1679 | out: |
1680 | /* free IPC related data */ |
1681 | widget_ops = tplg_ops ? tplg_ops->widget : NULL; |
1682 | if (widget_ops && widget_ops[swidget->id].ipc_free) |
1683 | widget_ops[swidget->id].ipc_free(swidget); |
1684 | |
1685 | ida_destroy(ida: &swidget->output_queue_ida); |
1686 | ida_destroy(ida: &swidget->input_queue_ida); |
1687 | |
1688 | sof_free_pin_binding(swidget, SOF_PIN_TYPE_INPUT); |
1689 | sof_free_pin_binding(swidget, SOF_PIN_TYPE_OUTPUT); |
1690 | |
1691 | kfree(objp: swidget->tuples); |
1692 | |
1693 | /* remove and free swidget object */ |
1694 | list_del(entry: &swidget->list); |
1695 | kfree(objp: swidget); |
1696 | |
1697 | return 0; |
1698 | } |
1699 | |
1700 | /* |
1701 | * DAI HW configuration. |
1702 | */ |
1703 | |
1704 | /* FE DAI - used for any driver specific init */ |
1705 | static int sof_dai_load(struct snd_soc_component *scomp, int index, |
1706 | struct snd_soc_dai_driver *dai_drv, |
1707 | struct snd_soc_tplg_pcm *pcm, struct snd_soc_dai *dai) |
1708 | { |
1709 | struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(c: scomp); |
1710 | const struct sof_ipc_pcm_ops *ipc_pcm_ops = sof_ipc_get_ops(sdev, pcm); |
1711 | struct snd_soc_tplg_stream_caps *caps; |
1712 | struct snd_soc_tplg_private *private = &pcm->priv; |
1713 | struct snd_sof_pcm *spcm; |
1714 | int stream; |
1715 | int ret; |
1716 | |
1717 | /* nothing to do for BEs atm */ |
1718 | if (!pcm) |
1719 | return 0; |
1720 | |
1721 | spcm = kzalloc(size: sizeof(*spcm), GFP_KERNEL); |
1722 | if (!spcm) |
1723 | return -ENOMEM; |
1724 | |
1725 | spcm->scomp = scomp; |
1726 | |
1727 | for_each_pcm_streams(stream) { |
1728 | spcm->stream[stream].comp_id = COMP_ID_UNASSIGNED; |
1729 | if (pcm->compress) |
1730 | snd_sof_compr_init_elapsed_work(work: &spcm->stream[stream].period_elapsed_work); |
1731 | else |
1732 | snd_sof_pcm_init_elapsed_work(work: &spcm->stream[stream].period_elapsed_work); |
1733 | } |
1734 | |
1735 | spcm->pcm = *pcm; |
1736 | dev_dbg(scomp->dev, "tplg: load pcm %s\n" , pcm->dai_name); |
1737 | |
1738 | /* perform pcm set op */ |
1739 | if (ipc_pcm_ops && ipc_pcm_ops->pcm_setup) { |
1740 | ret = ipc_pcm_ops->pcm_setup(sdev, spcm); |
1741 | if (ret < 0) { |
1742 | kfree(objp: spcm); |
1743 | return ret; |
1744 | } |
1745 | } |
1746 | |
1747 | dai_drv->dobj.private = spcm; |
1748 | list_add(new: &spcm->list, head: &sdev->pcm_list); |
1749 | |
1750 | ret = sof_parse_tokens(scomp, object: spcm, tokens: stream_tokens, |
1751 | ARRAY_SIZE(stream_tokens), array: private->array, |
1752 | le32_to_cpu(private->size)); |
1753 | if (ret) { |
1754 | dev_err(scomp->dev, "error: parse stream tokens failed %d\n" , |
1755 | le32_to_cpu(private->size)); |
1756 | return ret; |
1757 | } |
1758 | |
1759 | /* do we need to allocate playback PCM DMA pages */ |
1760 | if (!spcm->pcm.playback) |
1761 | goto capture; |
1762 | |
1763 | stream = SNDRV_PCM_STREAM_PLAYBACK; |
1764 | |
1765 | caps = &spcm->pcm.caps[stream]; |
1766 | |
1767 | /* allocate playback page table buffer */ |
1768 | ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, dev: sdev->dev, |
1769 | PAGE_SIZE, dmab: &spcm->stream[stream].page_table); |
1770 | if (ret < 0) { |
1771 | dev_err(scomp->dev, "error: can't alloc page table for %s %d\n" , |
1772 | caps->name, ret); |
1773 | |
1774 | return ret; |
1775 | } |
1776 | |
1777 | /* bind pcm to host comp */ |
1778 | ret = spcm_bind(scomp, spcm, dir: stream); |
1779 | if (ret) { |
1780 | dev_err(scomp->dev, |
1781 | "error: can't bind pcm to host\n" ); |
1782 | goto free_playback_tables; |
1783 | } |
1784 | |
1785 | capture: |
1786 | stream = SNDRV_PCM_STREAM_CAPTURE; |
1787 | |
1788 | /* do we need to allocate capture PCM DMA pages */ |
1789 | if (!spcm->pcm.capture) |
1790 | return ret; |
1791 | |
1792 | caps = &spcm->pcm.caps[stream]; |
1793 | |
1794 | /* allocate capture page table buffer */ |
1795 | ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, dev: sdev->dev, |
1796 | PAGE_SIZE, dmab: &spcm->stream[stream].page_table); |
1797 | if (ret < 0) { |
1798 | dev_err(scomp->dev, "error: can't alloc page table for %s %d\n" , |
1799 | caps->name, ret); |
1800 | goto free_playback_tables; |
1801 | } |
1802 | |
1803 | /* bind pcm to host comp */ |
1804 | ret = spcm_bind(scomp, spcm, dir: stream); |
1805 | if (ret) { |
1806 | dev_err(scomp->dev, |
1807 | "error: can't bind pcm to host\n" ); |
1808 | snd_dma_free_pages(dmab: &spcm->stream[stream].page_table); |
1809 | goto free_playback_tables; |
1810 | } |
1811 | |
1812 | return ret; |
1813 | |
1814 | free_playback_tables: |
1815 | if (spcm->pcm.playback) |
1816 | snd_dma_free_pages(dmab: &spcm->stream[SNDRV_PCM_STREAM_PLAYBACK].page_table); |
1817 | |
1818 | return ret; |
1819 | } |
1820 | |
1821 | static int sof_dai_unload(struct snd_soc_component *scomp, |
1822 | struct snd_soc_dobj *dobj) |
1823 | { |
1824 | struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(c: scomp); |
1825 | const struct sof_ipc_pcm_ops *ipc_pcm_ops = sof_ipc_get_ops(sdev, pcm); |
1826 | struct snd_sof_pcm *spcm = dobj->private; |
1827 | |
1828 | /* free PCM DMA pages */ |
1829 | if (spcm->pcm.playback) |
1830 | snd_dma_free_pages(dmab: &spcm->stream[SNDRV_PCM_STREAM_PLAYBACK].page_table); |
1831 | |
1832 | if (spcm->pcm.capture) |
1833 | snd_dma_free_pages(dmab: &spcm->stream[SNDRV_PCM_STREAM_CAPTURE].page_table); |
1834 | |
1835 | /* perform pcm free op */ |
1836 | if (ipc_pcm_ops && ipc_pcm_ops->pcm_free) |
1837 | ipc_pcm_ops->pcm_free(sdev, spcm); |
1838 | |
1839 | /* remove from list and free spcm */ |
1840 | list_del(entry: &spcm->list); |
1841 | kfree(objp: spcm); |
1842 | |
1843 | return 0; |
1844 | } |
1845 | |
1846 | static const struct sof_topology_token common_dai_link_tokens[] = { |
1847 | {SOF_TKN_DAI_TYPE, SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_dai_type, |
1848 | offsetof(struct snd_sof_dai_link, type)}, |
1849 | }; |
1850 | |
1851 | /* DAI link - used for any driver specific init */ |
1852 | static int sof_link_load(struct snd_soc_component *scomp, int index, struct snd_soc_dai_link *link, |
1853 | struct snd_soc_tplg_link_config *cfg) |
1854 | { |
1855 | struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(c: scomp); |
1856 | const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg); |
1857 | struct snd_soc_tplg_private *private = &cfg->priv; |
1858 | const struct sof_token_info *token_list; |
1859 | struct snd_sof_dai_link *slink; |
1860 | u32 token_id = 0; |
1861 | int num_tuples = 0; |
1862 | int ret, num_sets; |
1863 | |
1864 | if (!link->platforms) { |
1865 | dev_err(scomp->dev, "error: no platforms\n" ); |
1866 | return -EINVAL; |
1867 | } |
1868 | link->platforms->name = dev_name(dev: scomp->dev); |
1869 | |
1870 | if (tplg_ops && tplg_ops->link_setup) { |
1871 | ret = tplg_ops->link_setup(sdev, link); |
1872 | if (ret < 0) |
1873 | return ret; |
1874 | } |
1875 | |
1876 | /* Set nonatomic property for FE dai links as their trigger action involves IPC's */ |
1877 | if (!link->no_pcm) { |
1878 | link->nonatomic = true; |
1879 | return 0; |
1880 | } |
1881 | |
1882 | /* check we have some tokens - we need at least DAI type */ |
1883 | if (le32_to_cpu(private->size) == 0) { |
1884 | dev_err(scomp->dev, "error: expected tokens for DAI, none found\n" ); |
1885 | return -EINVAL; |
1886 | } |
1887 | |
1888 | slink = kzalloc(size: sizeof(*slink), GFP_KERNEL); |
1889 | if (!slink) |
1890 | return -ENOMEM; |
1891 | |
1892 | slink->num_hw_configs = le32_to_cpu(cfg->num_hw_configs); |
1893 | slink->hw_configs = kmemdup(p: cfg->hw_config, |
1894 | size: sizeof(*slink->hw_configs) * slink->num_hw_configs, |
1895 | GFP_KERNEL); |
1896 | if (!slink->hw_configs) { |
1897 | kfree(objp: slink); |
1898 | return -ENOMEM; |
1899 | } |
1900 | |
1901 | slink->default_hw_cfg_id = le32_to_cpu(cfg->default_hw_config_id); |
1902 | slink->link = link; |
1903 | |
1904 | dev_dbg(scomp->dev, "tplg: %d hw_configs found, default id: %d for dai link %s!\n" , |
1905 | slink->num_hw_configs, slink->default_hw_cfg_id, link->name); |
1906 | |
1907 | ret = sof_parse_tokens(scomp, object: slink, tokens: common_dai_link_tokens, |
1908 | ARRAY_SIZE(common_dai_link_tokens), |
1909 | array: private->array, le32_to_cpu(private->size)); |
1910 | if (ret < 0) { |
1911 | dev_err(scomp->dev, "Failed tp parse common DAI link tokens\n" ); |
1912 | kfree(objp: slink->hw_configs); |
1913 | kfree(objp: slink); |
1914 | return ret; |
1915 | } |
1916 | |
1917 | token_list = tplg_ops ? tplg_ops->token_list : NULL; |
1918 | if (!token_list) |
1919 | goto out; |
1920 | |
1921 | /* calculate size of tuples array */ |
1922 | num_tuples += token_list[SOF_DAI_LINK_TOKENS].count; |
1923 | num_sets = slink->num_hw_configs; |
1924 | switch (slink->type) { |
1925 | case SOF_DAI_INTEL_SSP: |
1926 | token_id = SOF_SSP_TOKENS; |
1927 | num_tuples += token_list[SOF_SSP_TOKENS].count * slink->num_hw_configs; |
1928 | break; |
1929 | case SOF_DAI_INTEL_DMIC: |
1930 | token_id = SOF_DMIC_TOKENS; |
1931 | num_tuples += token_list[SOF_DMIC_TOKENS].count; |
1932 | |
1933 | /* Allocate memory for max PDM controllers */ |
1934 | num_tuples += token_list[SOF_DMIC_PDM_TOKENS].count * SOF_DAI_INTEL_DMIC_NUM_CTRL; |
1935 | break; |
1936 | case SOF_DAI_INTEL_HDA: |
1937 | token_id = SOF_HDA_TOKENS; |
1938 | num_tuples += token_list[SOF_HDA_TOKENS].count; |
1939 | break; |
1940 | case SOF_DAI_INTEL_ALH: |
1941 | token_id = SOF_ALH_TOKENS; |
1942 | num_tuples += token_list[SOF_ALH_TOKENS].count; |
1943 | break; |
1944 | case SOF_DAI_IMX_SAI: |
1945 | token_id = SOF_SAI_TOKENS; |
1946 | num_tuples += token_list[SOF_SAI_TOKENS].count; |
1947 | break; |
1948 | case SOF_DAI_IMX_ESAI: |
1949 | token_id = SOF_ESAI_TOKENS; |
1950 | num_tuples += token_list[SOF_ESAI_TOKENS].count; |
1951 | break; |
1952 | case SOF_DAI_MEDIATEK_AFE: |
1953 | token_id = SOF_AFE_TOKENS; |
1954 | num_tuples += token_list[SOF_AFE_TOKENS].count; |
1955 | break; |
1956 | case SOF_DAI_AMD_DMIC: |
1957 | token_id = SOF_ACPDMIC_TOKENS; |
1958 | num_tuples += token_list[SOF_ACPDMIC_TOKENS].count; |
1959 | break; |
1960 | case SOF_DAI_AMD_BT: |
1961 | case SOF_DAI_AMD_SP: |
1962 | case SOF_DAI_AMD_HS: |
1963 | case SOF_DAI_AMD_SP_VIRTUAL: |
1964 | case SOF_DAI_AMD_HS_VIRTUAL: |
1965 | token_id = SOF_ACPI2S_TOKENS; |
1966 | num_tuples += token_list[SOF_ACPI2S_TOKENS].count; |
1967 | break; |
1968 | case SOF_DAI_IMX_MICFIL: |
1969 | token_id = SOF_MICFIL_TOKENS; |
1970 | num_tuples += token_list[SOF_MICFIL_TOKENS].count; |
1971 | break; |
1972 | case SOF_DAI_AMD_SDW: |
1973 | token_id = SOF_ACP_SDW_TOKENS; |
1974 | num_tuples += token_list[SOF_ACP_SDW_TOKENS].count; |
1975 | break; |
1976 | default: |
1977 | break; |
1978 | } |
1979 | |
1980 | /* allocate memory for tuples array */ |
1981 | slink->tuples = kcalloc(n: num_tuples, size: sizeof(*slink->tuples), GFP_KERNEL); |
1982 | if (!slink->tuples) { |
1983 | kfree(objp: slink->hw_configs); |
1984 | kfree(objp: slink); |
1985 | return -ENOMEM; |
1986 | } |
1987 | |
1988 | if (token_list[SOF_DAI_LINK_TOKENS].tokens) { |
1989 | /* parse one set of DAI link tokens */ |
1990 | ret = sof_copy_tuples(sdev, array: private->array, le32_to_cpu(private->size), |
1991 | token_id: SOF_DAI_LINK_TOKENS, token_instance_num: 1, tuples: slink->tuples, |
1992 | tuples_size: num_tuples, num_copied_tuples: &slink->num_tuples); |
1993 | if (ret < 0) { |
1994 | dev_err(scomp->dev, "failed to parse %s for dai link %s\n" , |
1995 | token_list[SOF_DAI_LINK_TOKENS].name, link->name); |
1996 | goto err; |
1997 | } |
1998 | } |
1999 | |
2000 | /* nothing more to do if there are no DAI type-specific tokens defined */ |
2001 | if (!token_id || !token_list[token_id].tokens) |
2002 | goto out; |
2003 | |
2004 | /* parse "num_sets" sets of DAI-specific tokens */ |
2005 | ret = sof_copy_tuples(sdev, array: private->array, le32_to_cpu(private->size), |
2006 | token_id, token_instance_num: num_sets, tuples: slink->tuples, tuples_size: num_tuples, num_copied_tuples: &slink->num_tuples); |
2007 | if (ret < 0) { |
2008 | dev_err(scomp->dev, "failed to parse %s for dai link %s\n" , |
2009 | token_list[token_id].name, link->name); |
2010 | goto err; |
2011 | } |
2012 | |
2013 | /* for DMIC, also parse all sets of DMIC PDM tokens based on active PDM count */ |
2014 | if (token_id == SOF_DMIC_TOKENS) { |
2015 | num_sets = sof_get_token_value(SOF_TKN_INTEL_DMIC_NUM_PDM_ACTIVE, |
2016 | tuples: slink->tuples, num_tuples: slink->num_tuples); |
2017 | |
2018 | if (num_sets < 0) { |
2019 | dev_err(sdev->dev, "Invalid active PDM count for %s\n" , link->name); |
2020 | ret = num_sets; |
2021 | goto err; |
2022 | } |
2023 | |
2024 | ret = sof_copy_tuples(sdev, array: private->array, le32_to_cpu(private->size), |
2025 | token_id: SOF_DMIC_PDM_TOKENS, token_instance_num: num_sets, tuples: slink->tuples, |
2026 | tuples_size: num_tuples, num_copied_tuples: &slink->num_tuples); |
2027 | if (ret < 0) { |
2028 | dev_err(scomp->dev, "failed to parse %s for dai link %s\n" , |
2029 | token_list[SOF_DMIC_PDM_TOKENS].name, link->name); |
2030 | goto err; |
2031 | } |
2032 | } |
2033 | out: |
2034 | link->dobj.private = slink; |
2035 | list_add(new: &slink->list, head: &sdev->dai_link_list); |
2036 | |
2037 | return 0; |
2038 | |
2039 | err: |
2040 | kfree(objp: slink->tuples); |
2041 | kfree(objp: slink->hw_configs); |
2042 | kfree(objp: slink); |
2043 | |
2044 | return ret; |
2045 | } |
2046 | |
2047 | static int sof_link_unload(struct snd_soc_component *scomp, struct snd_soc_dobj *dobj) |
2048 | { |
2049 | struct snd_sof_dai_link *slink = dobj->private; |
2050 | |
2051 | if (!slink) |
2052 | return 0; |
2053 | |
2054 | kfree(objp: slink->tuples); |
2055 | list_del(entry: &slink->list); |
2056 | kfree(objp: slink->hw_configs); |
2057 | kfree(objp: slink); |
2058 | dobj->private = NULL; |
2059 | |
2060 | return 0; |
2061 | } |
2062 | |
2063 | /* DAI link - used for any driver specific init */ |
2064 | static int sof_route_load(struct snd_soc_component *scomp, int index, |
2065 | struct snd_soc_dapm_route *route) |
2066 | { |
2067 | struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(c: scomp); |
2068 | struct snd_sof_widget *source_swidget, *sink_swidget; |
2069 | struct snd_soc_dobj *dobj = &route->dobj; |
2070 | struct snd_sof_route *sroute; |
2071 | int ret = 0; |
2072 | |
2073 | /* allocate memory for sroute and connect */ |
2074 | sroute = kzalloc(size: sizeof(*sroute), GFP_KERNEL); |
2075 | if (!sroute) |
2076 | return -ENOMEM; |
2077 | |
2078 | sroute->scomp = scomp; |
2079 | dev_dbg(scomp->dev, "sink %s control %s source %s\n" , |
2080 | route->sink, route->control ? route->control : "none" , |
2081 | route->source); |
2082 | |
2083 | /* source component */ |
2084 | source_swidget = snd_sof_find_swidget(scomp, name: (char *)route->source); |
2085 | if (!source_swidget) { |
2086 | dev_err(scomp->dev, "error: source %s not found\n" , |
2087 | route->source); |
2088 | ret = -EINVAL; |
2089 | goto err; |
2090 | } |
2091 | |
2092 | /* |
2093 | * Virtual widgets of type output/out_drv may be added in topology |
2094 | * for compatibility. These are not handled by the FW. |
2095 | * So, don't send routes whose source/sink widget is of such types |
2096 | * to the DSP. |
2097 | */ |
2098 | if (source_swidget->id == snd_soc_dapm_out_drv || |
2099 | source_swidget->id == snd_soc_dapm_output) |
2100 | goto err; |
2101 | |
2102 | /* sink component */ |
2103 | sink_swidget = snd_sof_find_swidget(scomp, name: (char *)route->sink); |
2104 | if (!sink_swidget) { |
2105 | dev_err(scomp->dev, "error: sink %s not found\n" , |
2106 | route->sink); |
2107 | ret = -EINVAL; |
2108 | goto err; |
2109 | } |
2110 | |
2111 | /* |
2112 | * Don't send routes whose sink widget is of type |
2113 | * output or out_drv to the DSP |
2114 | */ |
2115 | if (sink_swidget->id == snd_soc_dapm_out_drv || |
2116 | sink_swidget->id == snd_soc_dapm_output) |
2117 | goto err; |
2118 | |
2119 | sroute->route = route; |
2120 | dobj->private = sroute; |
2121 | sroute->src_widget = source_swidget; |
2122 | sroute->sink_widget = sink_swidget; |
2123 | |
2124 | /* add route to route list */ |
2125 | list_add(new: &sroute->list, head: &sdev->route_list); |
2126 | |
2127 | return 0; |
2128 | err: |
2129 | kfree(objp: sroute); |
2130 | return ret; |
2131 | } |
2132 | |
2133 | /** |
2134 | * sof_set_widget_pipeline - Set pipeline for a component |
2135 | * @sdev: pointer to struct snd_sof_dev |
2136 | * @spipe: pointer to struct snd_sof_pipeline |
2137 | * @swidget: pointer to struct snd_sof_widget that has the same pipeline ID as @pipe_widget |
2138 | * |
2139 | * Return: 0 if successful, -EINVAL on error. |
2140 | * The function checks if @swidget is associated with any volatile controls. If so, setting |
2141 | * the dynamic_pipeline_widget is disallowed. |
2142 | */ |
2143 | static int sof_set_widget_pipeline(struct snd_sof_dev *sdev, struct snd_sof_pipeline *spipe, |
2144 | struct snd_sof_widget *swidget) |
2145 | { |
2146 | struct snd_sof_widget *pipe_widget = spipe->pipe_widget; |
2147 | struct snd_sof_control *scontrol; |
2148 | |
2149 | if (pipe_widget->dynamic_pipeline_widget) { |
2150 | /* dynamic widgets cannot have volatile kcontrols */ |
2151 | list_for_each_entry(scontrol, &sdev->kcontrol_list, list) |
2152 | if (scontrol->comp_id == swidget->comp_id && |
2153 | (scontrol->access & SNDRV_CTL_ELEM_ACCESS_VOLATILE)) { |
2154 | dev_err(sdev->dev, |
2155 | "error: volatile control found for dynamic widget %s\n" , |
2156 | swidget->widget->name); |
2157 | return -EINVAL; |
2158 | } |
2159 | } |
2160 | |
2161 | /* set the pipeline and apply the dynamic_pipeline_widget_flag */ |
2162 | swidget->spipe = spipe; |
2163 | swidget->dynamic_pipeline_widget = pipe_widget->dynamic_pipeline_widget; |
2164 | |
2165 | return 0; |
2166 | } |
2167 | |
2168 | /* completion - called at completion of firmware loading */ |
2169 | static int sof_complete(struct snd_soc_component *scomp) |
2170 | { |
2171 | struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(c: scomp); |
2172 | const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg); |
2173 | const struct sof_ipc_tplg_widget_ops *widget_ops; |
2174 | struct snd_sof_control *scontrol; |
2175 | struct snd_sof_pipeline *spipe; |
2176 | int ret; |
2177 | |
2178 | widget_ops = tplg_ops ? tplg_ops->widget : NULL; |
2179 | |
2180 | /* first update all control IPC structures based on the IPC version */ |
2181 | if (tplg_ops && tplg_ops->control_setup) |
2182 | list_for_each_entry(scontrol, &sdev->kcontrol_list, list) { |
2183 | ret = tplg_ops->control_setup(sdev, scontrol); |
2184 | if (ret < 0) { |
2185 | dev_err(sdev->dev, "failed updating IPC struct for control %s\n" , |
2186 | scontrol->name); |
2187 | return ret; |
2188 | } |
2189 | } |
2190 | |
2191 | /* set up the IPC structures for the pipeline widgets */ |
2192 | list_for_each_entry(spipe, &sdev->pipeline_list, list) { |
2193 | struct snd_sof_widget *pipe_widget = spipe->pipe_widget; |
2194 | struct snd_sof_widget *swidget; |
2195 | |
2196 | pipe_widget->instance_id = -EINVAL; |
2197 | |
2198 | /* Update the scheduler widget's IPC structure */ |
2199 | if (widget_ops && widget_ops[pipe_widget->id].ipc_setup) { |
2200 | ret = widget_ops[pipe_widget->id].ipc_setup(pipe_widget); |
2201 | if (ret < 0) { |
2202 | dev_err(sdev->dev, "failed updating IPC struct for %s\n" , |
2203 | pipe_widget->widget->name); |
2204 | return ret; |
2205 | } |
2206 | } |
2207 | |
2208 | /* set the pipeline and update the IPC structure for the non scheduler widgets */ |
2209 | list_for_each_entry(swidget, &sdev->widget_list, list) |
2210 | if (swidget->widget->id != snd_soc_dapm_scheduler && |
2211 | swidget->pipeline_id == pipe_widget->pipeline_id) { |
2212 | ret = sof_set_widget_pipeline(sdev, spipe, swidget); |
2213 | if (ret < 0) |
2214 | return ret; |
2215 | |
2216 | if (widget_ops && widget_ops[swidget->id].ipc_setup) { |
2217 | ret = widget_ops[swidget->id].ipc_setup(swidget); |
2218 | if (ret < 0) { |
2219 | dev_err(sdev->dev, |
2220 | "failed updating IPC struct for %s\n" , |
2221 | swidget->widget->name); |
2222 | return ret; |
2223 | } |
2224 | } |
2225 | } |
2226 | } |
2227 | |
2228 | /* verify topology components loading including dynamic pipelines */ |
2229 | if (sof_debug_check_flag(SOF_DBG_VERIFY_TPLG)) { |
2230 | if (tplg_ops && tplg_ops->set_up_all_pipelines && |
2231 | tplg_ops->tear_down_all_pipelines) { |
2232 | ret = tplg_ops->set_up_all_pipelines(sdev, true); |
2233 | if (ret < 0) { |
2234 | dev_err(sdev->dev, "Failed to set up all topology pipelines: %d\n" , |
2235 | ret); |
2236 | return ret; |
2237 | } |
2238 | |
2239 | ret = tplg_ops->tear_down_all_pipelines(sdev, true); |
2240 | if (ret < 0) { |
2241 | dev_err(sdev->dev, "Failed to tear down topology pipelines: %d\n" , |
2242 | ret); |
2243 | return ret; |
2244 | } |
2245 | } |
2246 | } |
2247 | |
2248 | /* set up static pipelines */ |
2249 | if (tplg_ops && tplg_ops->set_up_all_pipelines) |
2250 | return tplg_ops->set_up_all_pipelines(sdev, false); |
2251 | |
2252 | return 0; |
2253 | } |
2254 | |
2255 | /* manifest - optional to inform component of manifest */ |
2256 | static int sof_manifest(struct snd_soc_component *scomp, int index, |
2257 | struct snd_soc_tplg_manifest *man) |
2258 | { |
2259 | struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(c: scomp); |
2260 | const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg); |
2261 | |
2262 | if (tplg_ops && tplg_ops->parse_manifest) |
2263 | return tplg_ops->parse_manifest(scomp, index, man); |
2264 | |
2265 | return 0; |
2266 | } |
2267 | |
2268 | /* vendor specific kcontrol handlers available for binding */ |
2269 | static const struct snd_soc_tplg_kcontrol_ops sof_io_ops[] = { |
2270 | {SOF_TPLG_KCTL_VOL_ID, snd_sof_volume_get, snd_sof_volume_put}, |
2271 | {SOF_TPLG_KCTL_BYTES_ID, snd_sof_bytes_get, snd_sof_bytes_put}, |
2272 | {SOF_TPLG_KCTL_ENUM_ID, snd_sof_enum_get, snd_sof_enum_put}, |
2273 | {SOF_TPLG_KCTL_SWITCH_ID, snd_sof_switch_get, snd_sof_switch_put}, |
2274 | }; |
2275 | |
2276 | /* vendor specific bytes ext handlers available for binding */ |
2277 | static const struct snd_soc_tplg_bytes_ext_ops sof_bytes_ext_ops[] = { |
2278 | {SOF_TPLG_KCTL_BYTES_ID, snd_sof_bytes_ext_get, snd_sof_bytes_ext_put}, |
2279 | {SOF_TPLG_KCTL_BYTES_VOLATILE_RO, snd_sof_bytes_ext_volatile_get}, |
2280 | }; |
2281 | |
2282 | static struct snd_soc_tplg_ops sof_tplg_ops = { |
2283 | /* external kcontrol init - used for any driver specific init */ |
2284 | .control_load = sof_control_load, |
2285 | .control_unload = sof_control_unload, |
2286 | |
2287 | /* external kcontrol init - used for any driver specific init */ |
2288 | .dapm_route_load = sof_route_load, |
2289 | .dapm_route_unload = sof_route_unload, |
2290 | |
2291 | /* external widget init - used for any driver specific init */ |
2292 | /* .widget_load is not currently used */ |
2293 | .widget_ready = sof_widget_ready, |
2294 | .widget_unload = sof_widget_unload, |
2295 | |
2296 | /* FE DAI - used for any driver specific init */ |
2297 | .dai_load = sof_dai_load, |
2298 | .dai_unload = sof_dai_unload, |
2299 | |
2300 | /* DAI link - used for any driver specific init */ |
2301 | .link_load = sof_link_load, |
2302 | .link_unload = sof_link_unload, |
2303 | |
2304 | /* completion - called at completion of firmware loading */ |
2305 | .complete = sof_complete, |
2306 | |
2307 | /* manifest - optional to inform component of manifest */ |
2308 | .manifest = sof_manifest, |
2309 | |
2310 | /* vendor specific kcontrol handlers available for binding */ |
2311 | .io_ops = sof_io_ops, |
2312 | .io_ops_count = ARRAY_SIZE(sof_io_ops), |
2313 | |
2314 | /* vendor specific bytes ext handlers available for binding */ |
2315 | .bytes_ext_ops = sof_bytes_ext_ops, |
2316 | .bytes_ext_ops_count = ARRAY_SIZE(sof_bytes_ext_ops), |
2317 | }; |
2318 | |
2319 | static int snd_sof_dspless_kcontrol(struct snd_kcontrol *kcontrol, |
2320 | struct snd_ctl_elem_value *ucontrol) |
2321 | { |
2322 | return 0; |
2323 | } |
2324 | |
2325 | static const struct snd_soc_tplg_kcontrol_ops sof_dspless_io_ops[] = { |
2326 | {SOF_TPLG_KCTL_VOL_ID, snd_sof_dspless_kcontrol, snd_sof_dspless_kcontrol}, |
2327 | {SOF_TPLG_KCTL_BYTES_ID, snd_sof_dspless_kcontrol, snd_sof_dspless_kcontrol}, |
2328 | {SOF_TPLG_KCTL_ENUM_ID, snd_sof_dspless_kcontrol, snd_sof_dspless_kcontrol}, |
2329 | {SOF_TPLG_KCTL_SWITCH_ID, snd_sof_dspless_kcontrol, snd_sof_dspless_kcontrol}, |
2330 | }; |
2331 | |
2332 | static int snd_sof_dspless_bytes_ext_get(struct snd_kcontrol *kcontrol, |
2333 | unsigned int __user *binary_data, |
2334 | unsigned int size) |
2335 | { |
2336 | return 0; |
2337 | } |
2338 | |
2339 | static int snd_sof_dspless_bytes_ext_put(struct snd_kcontrol *kcontrol, |
2340 | const unsigned int __user *binary_data, |
2341 | unsigned int size) |
2342 | { |
2343 | return 0; |
2344 | } |
2345 | |
2346 | static const struct snd_soc_tplg_bytes_ext_ops sof_dspless_bytes_ext_ops[] = { |
2347 | {SOF_TPLG_KCTL_BYTES_ID, snd_sof_dspless_bytes_ext_get, snd_sof_dspless_bytes_ext_put}, |
2348 | {SOF_TPLG_KCTL_BYTES_VOLATILE_RO, snd_sof_dspless_bytes_ext_get}, |
2349 | }; |
2350 | |
2351 | /* external widget init - used for any driver specific init */ |
2352 | static int sof_dspless_widget_ready(struct snd_soc_component *scomp, int index, |
2353 | struct snd_soc_dapm_widget *w, |
2354 | struct snd_soc_tplg_dapm_widget *tw) |
2355 | { |
2356 | if (WIDGET_IS_DAI(w->id)) { |
2357 | static const struct sof_topology_token dai_tokens[] = { |
2358 | {SOF_TKN_DAI_TYPE, SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_dai_type, 0}}; |
2359 | struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(c: scomp); |
2360 | struct snd_soc_tplg_private *priv = &tw->priv; |
2361 | struct snd_sof_widget *swidget; |
2362 | struct snd_sof_dai *sdai; |
2363 | int ret; |
2364 | |
2365 | swidget = kzalloc(size: sizeof(*swidget), GFP_KERNEL); |
2366 | if (!swidget) |
2367 | return -ENOMEM; |
2368 | |
2369 | sdai = kzalloc(size: sizeof(*sdai), GFP_KERNEL); |
2370 | if (!sdai) { |
2371 | kfree(objp: swidget); |
2372 | return -ENOMEM; |
2373 | } |
2374 | |
2375 | ret = sof_parse_tokens(scomp, object: &sdai->type, tokens: dai_tokens, ARRAY_SIZE(dai_tokens), |
2376 | array: priv->array, le32_to_cpu(priv->size)); |
2377 | if (ret < 0) { |
2378 | dev_err(scomp->dev, "Failed to parse DAI tokens for %s\n" , tw->name); |
2379 | kfree(objp: swidget); |
2380 | kfree(objp: sdai); |
2381 | return ret; |
2382 | } |
2383 | |
2384 | ret = sof_connect_dai_widget(scomp, w, tw, dai: sdai); |
2385 | if (ret) { |
2386 | kfree(objp: swidget); |
2387 | kfree(objp: sdai); |
2388 | return ret; |
2389 | } |
2390 | |
2391 | swidget->scomp = scomp; |
2392 | swidget->widget = w; |
2393 | swidget->private = sdai; |
2394 | mutex_init(&swidget->setup_mutex); |
2395 | w->dobj.private = swidget; |
2396 | list_add(new: &swidget->list, head: &sdev->widget_list); |
2397 | } |
2398 | |
2399 | return 0; |
2400 | } |
2401 | |
2402 | static int sof_dspless_widget_unload(struct snd_soc_component *scomp, |
2403 | struct snd_soc_dobj *dobj) |
2404 | { |
2405 | struct snd_soc_dapm_widget *w = container_of(dobj, struct snd_soc_dapm_widget, dobj); |
2406 | |
2407 | if (WIDGET_IS_DAI(w->id)) { |
2408 | struct snd_sof_widget *swidget = dobj->private; |
2409 | |
2410 | sof_disconnect_dai_widget(scomp, w); |
2411 | |
2412 | if (!swidget) |
2413 | return 0; |
2414 | |
2415 | /* remove and free swidget object */ |
2416 | list_del(entry: &swidget->list); |
2417 | kfree(objp: swidget->private); |
2418 | kfree(objp: swidget); |
2419 | } |
2420 | |
2421 | return 0; |
2422 | } |
2423 | |
2424 | static int sof_dspless_link_load(struct snd_soc_component *scomp, int index, |
2425 | struct snd_soc_dai_link *link, |
2426 | struct snd_soc_tplg_link_config *cfg) |
2427 | { |
2428 | link->platforms->name = dev_name(dev: scomp->dev); |
2429 | |
2430 | /* Set nonatomic property for FE dai links for FE-BE compatibility */ |
2431 | if (!link->no_pcm) |
2432 | link->nonatomic = true; |
2433 | |
2434 | return 0; |
2435 | } |
2436 | |
2437 | static struct snd_soc_tplg_ops sof_dspless_tplg_ops = { |
2438 | /* external widget init - used for any driver specific init */ |
2439 | .widget_ready = sof_dspless_widget_ready, |
2440 | .widget_unload = sof_dspless_widget_unload, |
2441 | |
2442 | /* FE DAI - used for any driver specific init */ |
2443 | .dai_load = sof_dai_load, |
2444 | .dai_unload = sof_dai_unload, |
2445 | |
2446 | /* DAI link - used for any driver specific init */ |
2447 | .link_load = sof_dspless_link_load, |
2448 | |
2449 | /* vendor specific kcontrol handlers available for binding */ |
2450 | .io_ops = sof_dspless_io_ops, |
2451 | .io_ops_count = ARRAY_SIZE(sof_dspless_io_ops), |
2452 | |
2453 | /* vendor specific bytes ext handlers available for binding */ |
2454 | .bytes_ext_ops = sof_dspless_bytes_ext_ops, |
2455 | .bytes_ext_ops_count = ARRAY_SIZE(sof_dspless_bytes_ext_ops), |
2456 | }; |
2457 | |
2458 | int snd_sof_load_topology(struct snd_soc_component *scomp, const char *file) |
2459 | { |
2460 | struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(c: scomp); |
2461 | const struct firmware *fw; |
2462 | int ret; |
2463 | |
2464 | dev_dbg(scomp->dev, "loading topology:%s\n" , file); |
2465 | |
2466 | ret = request_firmware(fw: &fw, name: file, device: scomp->dev); |
2467 | if (ret < 0) { |
2468 | dev_err(scomp->dev, "error: tplg request firmware %s failed err: %d\n" , |
2469 | file, ret); |
2470 | dev_err(scomp->dev, |
2471 | "you may need to download the firmware from https://github.com/thesofproject/sof-bin/\n" ); |
2472 | return ret; |
2473 | } |
2474 | |
2475 | if (sdev->dspless_mode_selected) |
2476 | ret = snd_soc_tplg_component_load(comp: scomp, ops: &sof_dspless_tplg_ops, fw); |
2477 | else |
2478 | ret = snd_soc_tplg_component_load(comp: scomp, ops: &sof_tplg_ops, fw); |
2479 | |
2480 | if (ret < 0) { |
2481 | dev_err(scomp->dev, "error: tplg component load failed %d\n" , |
2482 | ret); |
2483 | ret = -EINVAL; |
2484 | } |
2485 | |
2486 | release_firmware(fw); |
2487 | |
2488 | if (ret >= 0 && sdev->led_present) |
2489 | ret = snd_ctl_led_request(); |
2490 | |
2491 | return ret; |
2492 | } |
2493 | EXPORT_SYMBOL(snd_sof_load_topology); |
2494 | |