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