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