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