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