xref: /linux/sound/soc/intel/avs/topology.c (revision 666f12ae9f504625e5a93581d8fbcba3dd60c294)
1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // Copyright(c) 2021 Intel Corporation
4 //
5 // Authors: Cezary Rojewski <cezary.rojewski@intel.com>
6 //          Amadeusz Slawinski <amadeuszx.slawinski@linux.intel.com>
7 //
8 
9 #include <linux/firmware.h>
10 #include <linux/uuid.h>
11 #include <sound/soc.h>
12 #include <sound/soc-acpi.h>
13 #include <sound/soc-topology.h>
14 #include <uapi/sound/intel/avs/tokens.h>
15 #include "avs.h"
16 #include "control.h"
17 #include "topology.h"
18 #include "utils.h"
19 
20 /* Get pointer to vendor array at the specified offset. */
21 #define avs_tplg_vendor_array_at(array, offset) \
22 	((struct snd_soc_tplg_vendor_array *)((u8 *)array + offset))
23 
24 /* Get pointer to vendor array that is next in line. */
25 #define avs_tplg_vendor_array_next(array) \
26 	(avs_tplg_vendor_array_at(array, le32_to_cpu((array)->size)))
27 
28 /*
29  * Scan provided block of tuples for the specified token. If found,
30  * @offset is updated with position at which first matching token is
31  * located.
32  *
33  * Returns 0 on success, -ENOENT if not found and error code otherwise.
34  */
35 static int
avs_tplg_vendor_array_lookup(struct snd_soc_tplg_vendor_array * tuples,u32 block_size,u32 token,u32 * offset)36 avs_tplg_vendor_array_lookup(struct snd_soc_tplg_vendor_array *tuples,
37 			     u32 block_size, u32 token, u32 *offset)
38 {
39 	u32 pos = 0;
40 
41 	while (block_size > 0) {
42 		struct snd_soc_tplg_vendor_value_elem *tuple;
43 		u32 tuples_size = le32_to_cpu(tuples->size);
44 
45 		if (tuples_size > block_size)
46 			return -EINVAL;
47 
48 		tuple = tuples->value;
49 		if (le32_to_cpu(tuple->token) == token) {
50 			*offset = pos;
51 			return 0;
52 		}
53 
54 		block_size -= tuples_size;
55 		pos += tuples_size;
56 		tuples = avs_tplg_vendor_array_next(tuples);
57 	}
58 
59 	return -ENOENT;
60 }
61 
62 /*
63  * See avs_tplg_vendor_array_lookup() for description.
64  *
65  * Behaves exactly like avs_tplg_vendor_lookup() but starts from the
66  * next vendor array in line. Useful when searching for the finish line
67  * of an arbitrary entry in a list of entries where each is composed of
68  * several vendor tuples and a specific token marks the beginning of
69  * a new entry block.
70  */
71 static int
avs_tplg_vendor_array_lookup_next(struct snd_soc_tplg_vendor_array * tuples,u32 block_size,u32 token,u32 * offset)72 avs_tplg_vendor_array_lookup_next(struct snd_soc_tplg_vendor_array *tuples,
73 				  u32 block_size, u32 token, u32 *offset)
74 {
75 	u32 tuples_size = le32_to_cpu(tuples->size);
76 	int ret;
77 
78 	if (tuples_size > block_size)
79 		return -EINVAL;
80 
81 	tuples = avs_tplg_vendor_array_next(tuples);
82 	block_size -= tuples_size;
83 
84 	ret = avs_tplg_vendor_array_lookup(tuples, block_size, token, offset);
85 	if (!ret)
86 		*offset += tuples_size;
87 	return ret;
88 }
89 
90 /*
91  * Scan provided block of tuples for the specified token which marks
92  * the border of an entry block. Behavior is similar to
93  * avs_tplg_vendor_array_lookup() except 0 is also returned if no
94  * matching token has been found. In such case, returned @size is
95  * assigned to @block_size as the entire block belongs to the current
96  * entry.
97  *
98  * Returns 0 on success, error code otherwise.
99  */
100 static int
avs_tplg_vendor_entry_size(struct snd_soc_tplg_vendor_array * tuples,u32 block_size,u32 entry_id_token,u32 * size)101 avs_tplg_vendor_entry_size(struct snd_soc_tplg_vendor_array *tuples,
102 			   u32 block_size, u32 entry_id_token, u32 *size)
103 {
104 	int ret;
105 
106 	ret = avs_tplg_vendor_array_lookup_next(tuples, block_size, entry_id_token, size);
107 	if (ret == -ENOENT) {
108 		*size = block_size;
109 		ret = 0;
110 	}
111 
112 	return ret;
113 }
114 
115 /*
116  * Vendor tuple parsing descriptor.
117  *
118  * @token: vendor specific token that identifies tuple
119  * @type: tuple type, one of SND_SOC_TPLG_TUPLE_TYPE_XXX
120  * @offset: offset of a struct's field to initialize
121  * @parse: parsing function, extracts and assigns value to object's field
122  */
123 struct avs_tplg_token_parser {
124 	enum avs_tplg_token token;
125 	u32 type;
126 	u32 offset;
127 	int (*parse)(struct snd_soc_component *comp, void *elem, void *object, u32 offset);
128 };
129 
130 static int
avs_parse_uuid_token(struct snd_soc_component * comp,void * elem,void * object,u32 offset)131 avs_parse_uuid_token(struct snd_soc_component *comp, void *elem, void *object, u32 offset)
132 {
133 	struct snd_soc_tplg_vendor_uuid_elem *tuple = elem;
134 	guid_t *val = (guid_t *)((u8 *)object + offset);
135 
136 	guid_copy((guid_t *)val, (const guid_t *)&tuple->uuid);
137 
138 	return 0;
139 }
140 
141 static int
avs_parse_bool_token(struct snd_soc_component * comp,void * elem,void * object,u32 offset)142 avs_parse_bool_token(struct snd_soc_component *comp, void *elem, void *object, u32 offset)
143 {
144 	struct snd_soc_tplg_vendor_value_elem *tuple = elem;
145 	bool *val = (bool *)((u8 *)object + offset);
146 
147 	*val = le32_to_cpu(tuple->value);
148 
149 	return 0;
150 }
151 
152 static int
avs_parse_byte_token(struct snd_soc_component * comp,void * elem,void * object,u32 offset)153 avs_parse_byte_token(struct snd_soc_component *comp, void *elem, void *object, u32 offset)
154 {
155 	struct snd_soc_tplg_vendor_value_elem *tuple = elem;
156 	u8 *val = ((u8 *)object + offset);
157 
158 	*val = le32_to_cpu(tuple->value);
159 
160 	return 0;
161 }
162 
163 static int
avs_parse_short_token(struct snd_soc_component * comp,void * elem,void * object,u32 offset)164 avs_parse_short_token(struct snd_soc_component *comp, void *elem, void *object, u32 offset)
165 {
166 	struct snd_soc_tplg_vendor_value_elem *tuple = elem;
167 	u16 *val = (u16 *)((u8 *)object + offset);
168 
169 	*val = le32_to_cpu(tuple->value);
170 
171 	return 0;
172 }
173 
174 static int
avs_parse_word_token(struct snd_soc_component * comp,void * elem,void * object,u32 offset)175 avs_parse_word_token(struct snd_soc_component *comp, void *elem, void *object, u32 offset)
176 {
177 	struct snd_soc_tplg_vendor_value_elem *tuple = elem;
178 	u32 *val = (u32 *)((u8 *)object + offset);
179 
180 	*val = le32_to_cpu(tuple->value);
181 
182 	return 0;
183 }
184 
185 static int
avs_parse_string_token(struct snd_soc_component * comp,void * elem,void * object,u32 offset)186 avs_parse_string_token(struct snd_soc_component *comp, void *elem, void *object, u32 offset)
187 {
188 	struct snd_soc_tplg_vendor_string_elem *tuple = elem;
189 	char *val = (char *)((u8 *)object + offset);
190 
191 	snprintf(val, SNDRV_CTL_ELEM_ID_NAME_MAXLEN, "%s", tuple->string);
192 
193 	return 0;
194 }
195 
avs_parse_uuid_tokens(struct snd_soc_component * comp,void * object,const struct avs_tplg_token_parser * parsers,int count,struct snd_soc_tplg_vendor_array * tuples)196 static int avs_parse_uuid_tokens(struct snd_soc_component *comp, void *object,
197 				 const struct avs_tplg_token_parser *parsers, int count,
198 				 struct snd_soc_tplg_vendor_array *tuples)
199 {
200 	struct snd_soc_tplg_vendor_uuid_elem *tuple;
201 	int ret, i, j;
202 
203 	/* Parse element by element. */
204 	for (i = 0; i < le32_to_cpu(tuples->num_elems); i++) {
205 		tuple = &tuples->uuid[i];
206 
207 		for (j = 0; j < count; j++) {
208 			/* Ignore non-UUID tokens. */
209 			if (parsers[j].type != SND_SOC_TPLG_TUPLE_TYPE_UUID ||
210 			    parsers[j].token != le32_to_cpu(tuple->token))
211 				continue;
212 
213 			ret = parsers[j].parse(comp, tuple, object, parsers[j].offset);
214 			if (ret)
215 				return ret;
216 		}
217 	}
218 
219 	return 0;
220 }
221 
avs_parse_string_tokens(struct snd_soc_component * comp,void * object,const struct avs_tplg_token_parser * parsers,int count,struct snd_soc_tplg_vendor_array * tuples)222 static int avs_parse_string_tokens(struct snd_soc_component *comp, void *object,
223 				   const struct avs_tplg_token_parser *parsers, int count,
224 				   struct snd_soc_tplg_vendor_array *tuples)
225 {
226 	struct snd_soc_tplg_vendor_string_elem *tuple;
227 	int ret, i, j;
228 
229 	/* Parse element by element. */
230 	for (i = 0; i < le32_to_cpu(tuples->num_elems); i++) {
231 		tuple = &tuples->string[i];
232 
233 		for (j = 0; j < count; j++) {
234 			/* Ignore non-string tokens. */
235 			if (parsers[j].type != SND_SOC_TPLG_TUPLE_TYPE_STRING ||
236 			    parsers[j].token != le32_to_cpu(tuple->token))
237 				continue;
238 
239 			ret = parsers[j].parse(comp, tuple, object, parsers[j].offset);
240 			if (ret)
241 				return ret;
242 		}
243 	}
244 
245 	return 0;
246 }
247 
avs_parse_word_tokens(struct snd_soc_component * comp,void * object,const struct avs_tplg_token_parser * parsers,int count,struct snd_soc_tplg_vendor_array * tuples)248 static int avs_parse_word_tokens(struct snd_soc_component *comp, void *object,
249 				 const struct avs_tplg_token_parser *parsers, int count,
250 				 struct snd_soc_tplg_vendor_array *tuples)
251 {
252 	struct snd_soc_tplg_vendor_value_elem *tuple;
253 	int ret, i, j;
254 
255 	/* Parse element by element. */
256 	for (i = 0; i < le32_to_cpu(tuples->num_elems); i++) {
257 		tuple = &tuples->value[i];
258 
259 		for (j = 0; j < count; j++) {
260 			/* Ignore non-integer tokens. */
261 			if (!(parsers[j].type == SND_SOC_TPLG_TUPLE_TYPE_WORD ||
262 			      parsers[j].type == SND_SOC_TPLG_TUPLE_TYPE_SHORT ||
263 			      parsers[j].type == SND_SOC_TPLG_TUPLE_TYPE_BYTE ||
264 			      parsers[j].type == SND_SOC_TPLG_TUPLE_TYPE_BOOL))
265 				continue;
266 
267 			if (parsers[j].token != le32_to_cpu(tuple->token))
268 				continue;
269 
270 			ret = parsers[j].parse(comp, tuple, object, parsers[j].offset);
271 			if (ret)
272 				return ret;
273 		}
274 	}
275 
276 	return 0;
277 }
278 
avs_parse_tokens(struct snd_soc_component * comp,void * object,const struct avs_tplg_token_parser * parsers,size_t count,struct snd_soc_tplg_vendor_array * tuples,int priv_size)279 static int avs_parse_tokens(struct snd_soc_component *comp, void *object,
280 			    const struct avs_tplg_token_parser *parsers, size_t count,
281 			    struct snd_soc_tplg_vendor_array *tuples, int priv_size)
282 {
283 	int array_size, ret;
284 
285 	while (priv_size > 0) {
286 		array_size = le32_to_cpu(tuples->size);
287 
288 		if (array_size <= 0) {
289 			dev_err(comp->dev, "invalid array size 0x%x\n", array_size);
290 			return -EINVAL;
291 		}
292 
293 		/* Make sure there is enough data before parsing. */
294 		priv_size -= array_size;
295 		if (priv_size < 0) {
296 			dev_err(comp->dev, "invalid array size 0x%x\n", array_size);
297 			return -EINVAL;
298 		}
299 
300 		switch (le32_to_cpu(tuples->type)) {
301 		case SND_SOC_TPLG_TUPLE_TYPE_UUID:
302 			ret = avs_parse_uuid_tokens(comp, object, parsers, count, tuples);
303 			break;
304 		case SND_SOC_TPLG_TUPLE_TYPE_STRING:
305 			ret = avs_parse_string_tokens(comp, object, parsers, count, tuples);
306 			break;
307 		case SND_SOC_TPLG_TUPLE_TYPE_BOOL:
308 		case SND_SOC_TPLG_TUPLE_TYPE_BYTE:
309 		case SND_SOC_TPLG_TUPLE_TYPE_SHORT:
310 		case SND_SOC_TPLG_TUPLE_TYPE_WORD:
311 			ret = avs_parse_word_tokens(comp, object, parsers, count, tuples);
312 			break;
313 		default:
314 			dev_err(comp->dev, "unknown token type %d\n", tuples->type);
315 			ret = -EINVAL;
316 		}
317 
318 		if (ret) {
319 			dev_err(comp->dev, "parsing %zu tokens of %d type failed: %d\n",
320 				count, tuples->type, ret);
321 			return ret;
322 		}
323 
324 		tuples = avs_tplg_vendor_array_next(tuples);
325 	}
326 
327 	return 0;
328 }
329 
330 #define AVS_DEFINE_PTR_PARSER(name, type, member) \
331 static int \
332 avs_parse_##name##_ptr(struct snd_soc_component *comp, void *elem, void *object, u32 offset) \
333 { \
334 	struct snd_soc_tplg_vendor_value_elem *tuple = elem;		\
335 	struct avs_soc_component *acomp = to_avs_soc_component(comp);	\
336 	type **val = (type **)(object + offset);			\
337 	u32 idx;							\
338 									\
339 	idx = le32_to_cpu(tuple->value);				\
340 	if (idx >= acomp->tplg->num_##member)				\
341 		return -EINVAL;						\
342 									\
343 	*val = &acomp->tplg->member[idx];				\
344 									\
345 	return 0;							\
346 }
347 
348 AVS_DEFINE_PTR_PARSER(audio_format, struct avs_audio_format, fmts);
349 AVS_DEFINE_PTR_PARSER(modcfg_base, struct avs_tplg_modcfg_base, modcfgs_base);
350 AVS_DEFINE_PTR_PARSER(modcfg_ext, struct avs_tplg_modcfg_ext, modcfgs_ext);
351 AVS_DEFINE_PTR_PARSER(pplcfg, struct avs_tplg_pplcfg, pplcfgs);
352 AVS_DEFINE_PTR_PARSER(binding, struct avs_tplg_binding, bindings);
353 AVS_DEFINE_PTR_PARSER(init_config, struct avs_tplg_init_config, init_configs);
354 AVS_DEFINE_PTR_PARSER(nhlt_config, struct avs_tplg_nhlt_config, nhlt_configs);
355 
356 static int
parse_audio_format_bitfield(struct snd_soc_component * comp,void * elem,void * object,u32 offset)357 parse_audio_format_bitfield(struct snd_soc_component *comp, void *elem, void *object, u32 offset)
358 {
359 	struct snd_soc_tplg_vendor_value_elem *velem = elem;
360 	struct avs_audio_format *audio_format = object;
361 
362 	switch (offset) {
363 	case AVS_TKN_AFMT_NUM_CHANNELS_U32:
364 		audio_format->num_channels = le32_to_cpu(velem->value);
365 		break;
366 	case AVS_TKN_AFMT_VALID_BIT_DEPTH_U32:
367 		audio_format->valid_bit_depth = le32_to_cpu(velem->value);
368 		break;
369 	case AVS_TKN_AFMT_SAMPLE_TYPE_U32:
370 		audio_format->sample_type = le32_to_cpu(velem->value);
371 		break;
372 	}
373 
374 	return 0;
375 }
376 
avs_ssp_sprint(char * buf,size_t size,const char * fmt,int port,int tdm)377 static int avs_ssp_sprint(char *buf, size_t size, const char *fmt, int port, int tdm)
378 {
379 	char *needle = strstr(fmt, "%d");
380 	int retsize;
381 
382 	/*
383 	 * If there is %d present in fmt string it should be replaced by either
384 	 * SSP or SSP:TDM, where SSP and TDM are numbers, all other formatting
385 	 * will be ignored.
386 	 */
387 	if (needle) {
388 		retsize = scnprintf(buf, min_t(size_t, size, needle - fmt + 1), "%s", fmt);
389 		retsize += scnprintf(buf + retsize, size - retsize, "%d", port);
390 		if (tdm)
391 			retsize += scnprintf(buf + retsize, size - retsize, ":%d", tdm);
392 		retsize += scnprintf(buf + retsize, size - retsize, "%s", needle + 2);
393 		return retsize;
394 	}
395 
396 	return snprintf(buf, size, "%s", fmt);
397 }
398 
parse_link_formatted_string(struct snd_soc_component * comp,void * elem,void * object,u32 offset)399 static int parse_link_formatted_string(struct snd_soc_component *comp, void *elem,
400 				       void *object, u32 offset)
401 {
402 	struct snd_soc_tplg_vendor_string_elem *tuple = elem;
403 	struct snd_soc_acpi_mach *mach = dev_get_platdata(comp->card->dev);
404 	char *val = (char *)((u8 *)object + offset);
405 	int ssp_port, tdm_slot;
406 
407 	/*
408 	 * Dynamic naming - string formats, e.g.: ssp%d - supported only for
409 	 * topologies describing single device e.g.: an I2S codec on SSP0.
410 	 */
411 	if (!avs_mach_singular_ssp(mach))
412 		return avs_parse_string_token(comp, elem, object, offset);
413 
414 	ssp_port = avs_mach_ssp_port(mach);
415 	if (!avs_mach_singular_tdm(mach, ssp_port))
416 		return avs_parse_string_token(comp, elem, object, offset);
417 
418 	tdm_slot = avs_mach_ssp_tdm(mach, ssp_port);
419 
420 	avs_ssp_sprint(val, SNDRV_CTL_ELEM_ID_NAME_MAXLEN, tuple->string, ssp_port, tdm_slot);
421 
422 	return 0;
423 }
424 
avs_parse_nhlt_config_size(struct snd_soc_component * comp,void * elem,void * object,u32 offset)425 static int avs_parse_nhlt_config_size(struct snd_soc_component *comp, void *elem, void *object,
426 				      u32 offset)
427 {
428 	struct snd_soc_tplg_vendor_value_elem *tuple = elem;
429 	struct acpi_nhlt_config **blob = (struct acpi_nhlt_config **)((u8 *)object + offset);
430 	u32 size;
431 
432 	size = le32_to_cpu(tuple->value);
433 	*blob = devm_kzalloc(comp->card->dev, struct_size(*blob, capabilities, size), GFP_KERNEL);
434 	if (!*blob)
435 		return -ENOMEM;
436 
437 	(*blob)->capabilities_size = size;
438 	return 0;
439 }
440 
441 static int
parse_dictionary_header(struct snd_soc_component * comp,struct snd_soc_tplg_vendor_array * tuples,void ** dict,u32 * num_entries,size_t entry_size,u32 num_entries_token)442 parse_dictionary_header(struct snd_soc_component *comp,
443 			struct snd_soc_tplg_vendor_array *tuples,
444 			void **dict, u32 *num_entries, size_t entry_size,
445 			u32 num_entries_token)
446 {
447 	struct snd_soc_tplg_vendor_value_elem *tuple;
448 
449 	/* Dictionary header consists of single tuple - entry count. */
450 	tuple = tuples->value;
451 	if (le32_to_cpu(tuple->token) != num_entries_token) {
452 		dev_err(comp->dev, "invalid dictionary header, expected: %d\n",
453 			num_entries_token);
454 		return -EINVAL;
455 	}
456 
457 	*num_entries = le32_to_cpu(tuple->value);
458 	*dict = devm_kcalloc(comp->card->dev, *num_entries, entry_size, GFP_KERNEL);
459 	if (!*dict)
460 		return -ENOMEM;
461 
462 	return 0;
463 }
464 
465 static int
parse_dictionary_entries(struct snd_soc_component * comp,struct snd_soc_tplg_vendor_array * tuples,u32 block_size,void * dict,u32 num_entries,size_t entry_size,u32 entry_id_token,const struct avs_tplg_token_parser * parsers,size_t num_parsers)466 parse_dictionary_entries(struct snd_soc_component *comp,
467 			 struct snd_soc_tplg_vendor_array *tuples, u32 block_size,
468 			 void *dict, u32 num_entries, size_t entry_size,
469 			 u32 entry_id_token,
470 			 const struct avs_tplg_token_parser *parsers, size_t num_parsers)
471 {
472 	void *pos = dict;
473 	int i;
474 
475 	for (i = 0; i < num_entries; i++) {
476 		u32 esize;
477 		int ret;
478 
479 		ret = avs_tplg_vendor_entry_size(tuples, block_size,
480 						 entry_id_token, &esize);
481 		if (ret)
482 			return ret;
483 
484 		ret = avs_parse_tokens(comp, pos, parsers, num_parsers, tuples, esize);
485 		if (ret < 0) {
486 			dev_err(comp->dev, "parse entry: %d of type: %d failed: %d\n",
487 				i, entry_id_token, ret);
488 			return ret;
489 		}
490 
491 		pos += entry_size;
492 		block_size -= esize;
493 		tuples = avs_tplg_vendor_array_at(tuples, esize);
494 	}
495 
496 	return 0;
497 }
498 
parse_dictionary(struct snd_soc_component * comp,struct snd_soc_tplg_vendor_array * tuples,u32 block_size,void ** dict,u32 * num_entries,size_t entry_size,u32 num_entries_token,u32 entry_id_token,const struct avs_tplg_token_parser * parsers,size_t num_parsers)499 static int parse_dictionary(struct snd_soc_component *comp,
500 			    struct snd_soc_tplg_vendor_array *tuples, u32 block_size,
501 			    void **dict, u32 *num_entries, size_t entry_size,
502 			    u32 num_entries_token, u32 entry_id_token,
503 			    const struct avs_tplg_token_parser *parsers, size_t num_parsers)
504 {
505 	int ret;
506 
507 	ret = parse_dictionary_header(comp, tuples, dict, num_entries,
508 				      entry_size, num_entries_token);
509 	if (ret)
510 		return ret;
511 
512 	block_size -= le32_to_cpu(tuples->size);
513 	/* With header parsed, move on to parsing entries. */
514 	tuples = avs_tplg_vendor_array_next(tuples);
515 
516 	return parse_dictionary_entries(comp, tuples, block_size, *dict,
517 					*num_entries, entry_size,
518 					entry_id_token, parsers, num_parsers);
519 }
520 
521 static const struct avs_tplg_token_parser library_parsers[] = {
522 	{
523 		.token = AVS_TKN_LIBRARY_NAME_STRING,
524 		.type = SND_SOC_TPLG_TUPLE_TYPE_STRING,
525 		.offset = offsetof(struct avs_tplg_library, name),
526 		.parse = avs_parse_string_token,
527 	},
528 };
529 
avs_tplg_parse_libraries(struct snd_soc_component * comp,struct snd_soc_tplg_vendor_array * tuples,u32 block_size)530 static int avs_tplg_parse_libraries(struct snd_soc_component *comp,
531 				    struct snd_soc_tplg_vendor_array *tuples, u32 block_size)
532 {
533 	struct avs_soc_component *acomp = to_avs_soc_component(comp);
534 	struct avs_tplg *tplg = acomp->tplg;
535 
536 	return parse_dictionary(comp, tuples, block_size, (void **)&tplg->libs,
537 				&tplg->num_libs, sizeof(*tplg->libs),
538 				AVS_TKN_MANIFEST_NUM_LIBRARIES_U32,
539 				AVS_TKN_LIBRARY_ID_U32,
540 				library_parsers, ARRAY_SIZE(library_parsers));
541 }
542 
543 static const struct avs_tplg_token_parser audio_format_parsers[] = {
544 	{
545 		.token = AVS_TKN_AFMT_SAMPLE_RATE_U32,
546 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
547 		.offset = offsetof(struct avs_audio_format, sampling_freq),
548 		.parse = avs_parse_word_token,
549 	},
550 	{
551 		.token = AVS_TKN_AFMT_BIT_DEPTH_U32,
552 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
553 		.offset = offsetof(struct avs_audio_format, bit_depth),
554 		.parse = avs_parse_word_token,
555 	},
556 	{
557 		.token = AVS_TKN_AFMT_CHANNEL_MAP_U32,
558 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
559 		.offset = offsetof(struct avs_audio_format, channel_map),
560 		.parse = avs_parse_word_token,
561 	},
562 	{
563 		.token = AVS_TKN_AFMT_CHANNEL_CFG_U32,
564 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
565 		.offset = offsetof(struct avs_audio_format, channel_config),
566 		.parse = avs_parse_word_token,
567 	},
568 	{
569 		.token = AVS_TKN_AFMT_INTERLEAVING_U32,
570 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
571 		.offset = offsetof(struct avs_audio_format, interleaving),
572 		.parse = avs_parse_word_token,
573 	},
574 	{
575 		.token = AVS_TKN_AFMT_NUM_CHANNELS_U32,
576 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
577 		.offset = AVS_TKN_AFMT_NUM_CHANNELS_U32,
578 		.parse = parse_audio_format_bitfield,
579 	},
580 	{
581 		.token = AVS_TKN_AFMT_VALID_BIT_DEPTH_U32,
582 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
583 		.offset = AVS_TKN_AFMT_VALID_BIT_DEPTH_U32,
584 		.parse = parse_audio_format_bitfield,
585 	},
586 	{
587 		.token = AVS_TKN_AFMT_SAMPLE_TYPE_U32,
588 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
589 		.offset = AVS_TKN_AFMT_SAMPLE_TYPE_U32,
590 		.parse = parse_audio_format_bitfield,
591 	},
592 };
593 
avs_tplg_parse_audio_formats(struct snd_soc_component * comp,struct snd_soc_tplg_vendor_array * tuples,u32 block_size)594 static int avs_tplg_parse_audio_formats(struct snd_soc_component *comp,
595 					struct snd_soc_tplg_vendor_array *tuples,
596 					u32 block_size)
597 {
598 	struct avs_soc_component *acomp = to_avs_soc_component(comp);
599 	struct avs_tplg *tplg = acomp->tplg;
600 
601 	return parse_dictionary(comp, tuples, block_size, (void **)&tplg->fmts,
602 				&tplg->num_fmts, sizeof(*tplg->fmts),
603 				AVS_TKN_MANIFEST_NUM_AFMTS_U32,
604 				AVS_TKN_AFMT_ID_U32,
605 				audio_format_parsers, ARRAY_SIZE(audio_format_parsers));
606 }
607 
608 static const struct avs_tplg_token_parser modcfg_base_parsers[] = {
609 	{
610 		.token = AVS_TKN_MODCFG_BASE_CPC_U32,
611 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
612 		.offset = offsetof(struct avs_tplg_modcfg_base, cpc),
613 		.parse = avs_parse_word_token,
614 	},
615 	{
616 		.token = AVS_TKN_MODCFG_BASE_IBS_U32,
617 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
618 		.offset = offsetof(struct avs_tplg_modcfg_base, ibs),
619 		.parse = avs_parse_word_token,
620 	},
621 	{
622 		.token = AVS_TKN_MODCFG_BASE_OBS_U32,
623 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
624 		.offset = offsetof(struct avs_tplg_modcfg_base, obs),
625 		.parse = avs_parse_word_token,
626 	},
627 	{
628 		.token = AVS_TKN_MODCFG_BASE_PAGES_U32,
629 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
630 		.offset = offsetof(struct avs_tplg_modcfg_base, is_pages),
631 		.parse = avs_parse_word_token,
632 	},
633 };
634 
avs_tplg_parse_modcfgs_base(struct snd_soc_component * comp,struct snd_soc_tplg_vendor_array * tuples,u32 block_size)635 static int avs_tplg_parse_modcfgs_base(struct snd_soc_component *comp,
636 				       struct snd_soc_tplg_vendor_array *tuples,
637 				       u32 block_size)
638 {
639 	struct avs_soc_component *acomp = to_avs_soc_component(comp);
640 	struct avs_tplg *tplg = acomp->tplg;
641 
642 	return parse_dictionary(comp, tuples, block_size, (void **)&tplg->modcfgs_base,
643 				&tplg->num_modcfgs_base, sizeof(*tplg->modcfgs_base),
644 				AVS_TKN_MANIFEST_NUM_MODCFGS_BASE_U32,
645 				AVS_TKN_MODCFG_BASE_ID_U32,
646 				modcfg_base_parsers, ARRAY_SIZE(modcfg_base_parsers));
647 }
648 
649 static const struct avs_tplg_token_parser modcfg_ext_parsers[] = {
650 	{
651 		.token = AVS_TKN_MODCFG_EXT_TYPE_UUID,
652 		.type = SND_SOC_TPLG_TUPLE_TYPE_UUID,
653 		.offset = offsetof(struct avs_tplg_modcfg_ext, type),
654 		.parse = avs_parse_uuid_token,
655 	},
656 	{
657 		.token = AVS_TKN_MODCFG_CPR_OUT_AFMT_ID_U32,
658 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
659 		.offset = offsetof(struct avs_tplg_modcfg_ext, copier.out_fmt),
660 		.parse = avs_parse_audio_format_ptr,
661 	},
662 	{
663 		.token = AVS_TKN_MODCFG_CPR_FEATURE_MASK_U32,
664 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
665 		.offset = offsetof(struct avs_tplg_modcfg_ext, copier.feature_mask),
666 		.parse = avs_parse_word_token,
667 	},
668 	{
669 		.token = AVS_TKN_MODCFG_CPR_VINDEX_U8,
670 		.type = SND_SOC_TPLG_TUPLE_TYPE_BYTE,
671 		.offset = offsetof(struct avs_tplg_modcfg_ext, copier.vindex),
672 		.parse = avs_parse_byte_token,
673 	},
674 	{
675 		.token = AVS_TKN_MODCFG_CPR_DMA_TYPE_U32,
676 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
677 		.offset = offsetof(struct avs_tplg_modcfg_ext, copier.dma_type),
678 		.parse = avs_parse_word_token,
679 	},
680 	{
681 		.token = AVS_TKN_MODCFG_CPR_DMABUFF_SIZE_U32,
682 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
683 		.offset = offsetof(struct avs_tplg_modcfg_ext, copier.dma_buffer_size),
684 		.parse = avs_parse_word_token,
685 	},
686 	{
687 		.token = AVS_TKN_MODCFG_CPR_BLOB_FMT_ID_U32,
688 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
689 		.offset = offsetof(struct avs_tplg_modcfg_ext, copier.blob_fmt),
690 		.parse = avs_parse_audio_format_ptr,
691 	},
692 	{
693 		.token = AVS_TKN_MODCFG_MICSEL_OUT_AFMT_ID_U32,
694 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
695 		.offset = offsetof(struct avs_tplg_modcfg_ext, micsel.out_fmt),
696 		.parse = avs_parse_audio_format_ptr,
697 	},
698 	{
699 		.token = AVS_TKN_MODCFG_INTELWOV_CPC_LP_MODE_U32,
700 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
701 		.offset = offsetof(struct avs_tplg_modcfg_ext, wov.cpc_lp_mode),
702 		.parse = avs_parse_word_token,
703 	},
704 	{
705 		.token = AVS_TKN_MODCFG_SRC_OUT_FREQ_U32,
706 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
707 		.offset = offsetof(struct avs_tplg_modcfg_ext, src.out_freq),
708 		.parse = avs_parse_word_token,
709 	},
710 	{
711 		.token = AVS_TKN_MODCFG_MUX_REF_AFMT_ID_U32,
712 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
713 		.offset = offsetof(struct avs_tplg_modcfg_ext, mux.ref_fmt),
714 		.parse = avs_parse_audio_format_ptr,
715 	},
716 	{
717 		.token = AVS_TKN_MODCFG_MUX_OUT_AFMT_ID_U32,
718 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
719 		.offset = offsetof(struct avs_tplg_modcfg_ext, mux.out_fmt),
720 		.parse = avs_parse_audio_format_ptr,
721 	},
722 	{
723 		.token = AVS_TKN_MODCFG_AEC_REF_AFMT_ID_U32,
724 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
725 		.offset = offsetof(struct avs_tplg_modcfg_ext, aec.ref_fmt),
726 		.parse = avs_parse_audio_format_ptr,
727 	},
728 	{
729 		.token = AVS_TKN_MODCFG_AEC_OUT_AFMT_ID_U32,
730 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
731 		.offset = offsetof(struct avs_tplg_modcfg_ext, aec.out_fmt),
732 		.parse = avs_parse_audio_format_ptr,
733 	},
734 	{
735 		.token = AVS_TKN_MODCFG_AEC_CPC_LP_MODE_U32,
736 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
737 		.offset = offsetof(struct avs_tplg_modcfg_ext, aec.cpc_lp_mode),
738 		.parse = avs_parse_word_token,
739 	},
740 	{
741 		.token = AVS_TKN_MODCFG_ASRC_OUT_FREQ_U32,
742 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
743 		.offset = offsetof(struct avs_tplg_modcfg_ext, asrc.out_freq),
744 		.parse = avs_parse_word_token,
745 	},
746 	{
747 		.token = AVS_TKN_MODCFG_ASRC_MODE_U8,
748 		.type = SND_SOC_TPLG_TUPLE_TYPE_BYTE,
749 		.offset = offsetof(struct avs_tplg_modcfg_ext, asrc.mode),
750 		.parse = avs_parse_byte_token,
751 	},
752 	{
753 		.token = AVS_TKN_MODCFG_ASRC_DISABLE_JITTER_U8,
754 		.type = SND_SOC_TPLG_TUPLE_TYPE_BYTE,
755 		.offset = offsetof(struct avs_tplg_modcfg_ext, asrc.disable_jitter_buffer),
756 		.parse = avs_parse_byte_token,
757 	},
758 	{
759 		.token = AVS_TKN_MODCFG_UPDOWN_MIX_OUT_CHAN_CFG_U32,
760 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
761 		.offset = offsetof(struct avs_tplg_modcfg_ext, updown_mix.out_channel_config),
762 		.parse = avs_parse_word_token,
763 	},
764 	{
765 		.token = AVS_TKN_MODCFG_UPDOWN_MIX_COEFF_SELECT_U32,
766 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
767 		.offset = offsetof(struct avs_tplg_modcfg_ext, updown_mix.coefficients_select),
768 		.parse = avs_parse_word_token,
769 	},
770 	{
771 		.token = AVS_TKN_MODCFG_UPDOWN_MIX_COEFF_0_S32,
772 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
773 		.offset = offsetof(struct avs_tplg_modcfg_ext, updown_mix.coefficients[0]),
774 		.parse = avs_parse_word_token,
775 	},
776 	{
777 		.token = AVS_TKN_MODCFG_UPDOWN_MIX_COEFF_1_S32,
778 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
779 		.offset = offsetof(struct avs_tplg_modcfg_ext, updown_mix.coefficients[1]),
780 		.parse = avs_parse_word_token,
781 	},
782 	{
783 		.token = AVS_TKN_MODCFG_UPDOWN_MIX_COEFF_2_S32,
784 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
785 		.offset = offsetof(struct avs_tplg_modcfg_ext, updown_mix.coefficients[2]),
786 		.parse = avs_parse_word_token,
787 	},
788 	{
789 		.token = AVS_TKN_MODCFG_UPDOWN_MIX_COEFF_3_S32,
790 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
791 		.offset = offsetof(struct avs_tplg_modcfg_ext, updown_mix.coefficients[3]),
792 		.parse = avs_parse_word_token,
793 	},
794 	{
795 		.token = AVS_TKN_MODCFG_UPDOWN_MIX_COEFF_4_S32,
796 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
797 		.offset = offsetof(struct avs_tplg_modcfg_ext, updown_mix.coefficients[4]),
798 		.parse = avs_parse_word_token,
799 	},
800 	{
801 		.token = AVS_TKN_MODCFG_UPDOWN_MIX_COEFF_5_S32,
802 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
803 		.offset = offsetof(struct avs_tplg_modcfg_ext, updown_mix.coefficients[5]),
804 		.parse = avs_parse_word_token,
805 	},
806 	{
807 		.token = AVS_TKN_MODCFG_UPDOWN_MIX_COEFF_6_S32,
808 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
809 		.offset = offsetof(struct avs_tplg_modcfg_ext, updown_mix.coefficients[6]),
810 		.parse = avs_parse_word_token,
811 	},
812 	{
813 		.token = AVS_TKN_MODCFG_UPDOWN_MIX_COEFF_7_S32,
814 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
815 		.offset = offsetof(struct avs_tplg_modcfg_ext, updown_mix.coefficients[7]),
816 		.parse = avs_parse_word_token,
817 	},
818 	{
819 		.token = AVS_TKN_MODCFG_UPDOWN_MIX_CHAN_MAP_U32,
820 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
821 		.offset = offsetof(struct avs_tplg_modcfg_ext, updown_mix.channel_map),
822 		.parse = avs_parse_word_token,
823 	},
824 	{
825 		.token = AVS_TKN_MODCFG_EXT_NUM_INPUT_PINS_U16,
826 		.type = SND_SOC_TPLG_TUPLE_TYPE_SHORT,
827 		.offset = offsetof(struct avs_tplg_modcfg_ext, generic.num_input_pins),
828 		.parse = avs_parse_short_token,
829 	},
830 	{
831 		.token = AVS_TKN_MODCFG_EXT_NUM_OUTPUT_PINS_U16,
832 		.type = SND_SOC_TPLG_TUPLE_TYPE_SHORT,
833 		.offset = offsetof(struct avs_tplg_modcfg_ext, generic.num_output_pins),
834 		.parse = avs_parse_short_token,
835 	},
836 	{
837 		.token = AVS_TKN_MODCFG_WHM_REF_AFMT_ID_U32,
838 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
839 		.offset = offsetof(struct avs_tplg_modcfg_ext, whm.ref_fmt),
840 		.parse = avs_parse_audio_format_ptr,
841 	},
842 	{
843 		.token = AVS_TKN_MODCFG_WHM_OUT_AFMT_ID_U32,
844 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
845 		.offset = offsetof(struct avs_tplg_modcfg_ext, whm.out_fmt),
846 		.parse = avs_parse_audio_format_ptr,
847 	},
848 	{
849 		.token = AVS_TKN_MODCFG_WHM_WAKE_TICK_PERIOD_U32,
850 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
851 		.offset = offsetof(struct avs_tplg_modcfg_ext, whm.wake_tick_period),
852 		.parse = avs_parse_word_token,
853 	},
854 	{
855 		.token = AVS_TKN_MODCFG_WHM_VINDEX_U8,
856 		.type = SND_SOC_TPLG_TUPLE_TYPE_BYTE,
857 		.offset = offsetof(struct avs_tplg_modcfg_ext, whm.vindex),
858 		.parse = avs_parse_byte_token,
859 	},
860 	{
861 		.token = AVS_TKN_MODCFG_WHM_DMA_TYPE_U32,
862 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
863 		.offset = offsetof(struct avs_tplg_modcfg_ext, whm.dma_type),
864 		.parse = avs_parse_word_token,
865 	},
866 	{
867 		.token = AVS_TKN_MODCFG_WHM_DMABUFF_SIZE_U32,
868 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
869 		.offset = offsetof(struct avs_tplg_modcfg_ext, whm.dma_buffer_size),
870 		.parse = avs_parse_word_token,
871 	},
872 	{
873 		.token = AVS_TKN_MODCFG_WHM_BLOB_AFMT_ID_U32,
874 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
875 		.offset = offsetof(struct avs_tplg_modcfg_ext, whm.blob_fmt),
876 		.parse = avs_parse_audio_format_ptr,
877 	},
878 	{
879 		.token = AVS_TKN_MODCFG_PEAKVOL_VOLUME_U32,
880 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
881 		.offset = offsetof(struct avs_tplg_modcfg_ext, peakvol.target_volume),
882 		.parse = avs_parse_word_token,
883 	},
884 	{
885 		.token = AVS_TKN_MODCFG_PEAKVOL_CURVE_TYPE_U32,
886 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
887 		.offset = offsetof(struct avs_tplg_modcfg_ext, peakvol.curve_type),
888 		.parse = avs_parse_word_token,
889 	},
890 	{
891 		.token = AVS_TKN_MODCFG_PEAKVOL_CURVE_DURATION_U32,
892 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
893 		.offset = offsetof(struct avs_tplg_modcfg_ext, peakvol.curve_duration),
894 		.parse = avs_parse_word_token,
895 	},
896 };
897 
898 static const struct avs_tplg_token_parser pin_format_parsers[] = {
899 	{
900 		.token = AVS_TKN_PIN_FMT_INDEX_U32,
901 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
902 		.offset = offsetof(struct avs_tplg_pin_format, pin_index),
903 		.parse = avs_parse_word_token,
904 	},
905 	{
906 		.token = AVS_TKN_PIN_FMT_IOBS_U32,
907 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
908 		.offset = offsetof(struct avs_tplg_pin_format, iobs),
909 		.parse = avs_parse_word_token,
910 	},
911 	{
912 		.token = AVS_TKN_PIN_FMT_AFMT_ID_U32,
913 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
914 		.offset = offsetof(struct avs_tplg_pin_format, fmt),
915 		.parse = avs_parse_audio_format_ptr,
916 	},
917 };
918 
919 static void
assign_copier_gtw_instance(struct snd_soc_component * comp,struct avs_tplg_modcfg_ext * cfg)920 assign_copier_gtw_instance(struct snd_soc_component *comp, struct avs_tplg_modcfg_ext *cfg)
921 {
922 	struct snd_soc_acpi_mach *mach;
923 	int ssp_port, tdm_slot;
924 
925 	if (!guid_equal(&cfg->type, &AVS_COPIER_MOD_UUID))
926 		return;
927 
928 	/* Only I2S boards assign port instance in ->i2s_link_mask. */
929 	switch (cfg->copier.dma_type) {
930 	case AVS_DMA_I2S_LINK_OUTPUT:
931 	case AVS_DMA_I2S_LINK_INPUT:
932 		break;
933 	default:
934 		return;
935 	}
936 
937 	/* If topology sets value don't overwrite it */
938 	if (cfg->copier.vindex.val)
939 		return;
940 
941 	mach = dev_get_platdata(comp->card->dev);
942 
943 	if (!avs_mach_singular_ssp(mach))
944 		return;
945 	ssp_port = avs_mach_ssp_port(mach);
946 
947 	if (!avs_mach_singular_tdm(mach, ssp_port))
948 		return;
949 	tdm_slot = avs_mach_ssp_tdm(mach, ssp_port);
950 
951 	cfg->copier.vindex.i2s.instance = ssp_port;
952 	cfg->copier.vindex.i2s.time_slot = tdm_slot;
953 }
954 
avs_tplg_parse_modcfg_ext(struct snd_soc_component * comp,struct avs_tplg_modcfg_ext * cfg,struct snd_soc_tplg_vendor_array * tuples,u32 block_size)955 static int avs_tplg_parse_modcfg_ext(struct snd_soc_component *comp,
956 				     struct avs_tplg_modcfg_ext *cfg,
957 				     struct snd_soc_tplg_vendor_array *tuples,
958 				     u32 block_size)
959 {
960 	u32 esize;
961 	int ret;
962 
963 	/* See where pin block starts. */
964 	ret = avs_tplg_vendor_entry_size(tuples, block_size,
965 					 AVS_TKN_PIN_FMT_INDEX_U32, &esize);
966 	if (ret)
967 		return ret;
968 
969 	ret = avs_parse_tokens(comp, cfg, modcfg_ext_parsers,
970 			       ARRAY_SIZE(modcfg_ext_parsers), tuples, esize);
971 	if (ret)
972 		return ret;
973 
974 	/* Update copier gateway based on board's i2s_link_mask. */
975 	assign_copier_gtw_instance(comp, cfg);
976 
977 	block_size -= esize;
978 	/* Parse trailing in/out pin formats if any. */
979 	if (block_size) {
980 		struct avs_tplg_pin_format *pins;
981 		u32 num_pins;
982 
983 		num_pins = cfg->generic.num_input_pins + cfg->generic.num_output_pins;
984 		if (!num_pins)
985 			return -EINVAL;
986 
987 		pins = devm_kcalloc(comp->card->dev, num_pins, sizeof(*pins), GFP_KERNEL);
988 		if (!pins)
989 			return -ENOMEM;
990 
991 		tuples = avs_tplg_vendor_array_at(tuples, esize);
992 		ret = parse_dictionary_entries(comp, tuples, block_size,
993 					       pins, num_pins, sizeof(*pins),
994 					       AVS_TKN_PIN_FMT_INDEX_U32,
995 					       pin_format_parsers,
996 					       ARRAY_SIZE(pin_format_parsers));
997 		if (ret)
998 			return ret;
999 		cfg->generic.pin_fmts = pins;
1000 	}
1001 
1002 	return 0;
1003 }
1004 
avs_tplg_parse_modcfgs_ext(struct snd_soc_component * comp,struct snd_soc_tplg_vendor_array * tuples,u32 block_size)1005 static int avs_tplg_parse_modcfgs_ext(struct snd_soc_component *comp,
1006 				      struct snd_soc_tplg_vendor_array *tuples,
1007 				      u32 block_size)
1008 {
1009 	struct avs_soc_component *acomp = to_avs_soc_component(comp);
1010 	struct avs_tplg *tplg = acomp->tplg;
1011 	int ret, i;
1012 
1013 	ret = parse_dictionary_header(comp, tuples, (void **)&tplg->modcfgs_ext,
1014 				      &tplg->num_modcfgs_ext,
1015 				      sizeof(*tplg->modcfgs_ext),
1016 				      AVS_TKN_MANIFEST_NUM_MODCFGS_EXT_U32);
1017 	if (ret)
1018 		return ret;
1019 
1020 	block_size -= le32_to_cpu(tuples->size);
1021 	/* With header parsed, move on to parsing entries. */
1022 	tuples = avs_tplg_vendor_array_next(tuples);
1023 
1024 	for (i = 0; i < tplg->num_modcfgs_ext; i++) {
1025 		struct avs_tplg_modcfg_ext *cfg = &tplg->modcfgs_ext[i];
1026 		u32 esize;
1027 
1028 		ret = avs_tplg_vendor_entry_size(tuples, block_size,
1029 						 AVS_TKN_MODCFG_EXT_ID_U32, &esize);
1030 		if (ret)
1031 			return ret;
1032 
1033 		ret = avs_tplg_parse_modcfg_ext(comp, cfg, tuples, esize);
1034 		if (ret)
1035 			return ret;
1036 
1037 		block_size -= esize;
1038 		tuples = avs_tplg_vendor_array_at(tuples, esize);
1039 	}
1040 
1041 	return 0;
1042 }
1043 
1044 static const struct avs_tplg_token_parser pplcfg_parsers[] = {
1045 	{
1046 		.token = AVS_TKN_PPLCFG_REQ_SIZE_U16,
1047 		.type = SND_SOC_TPLG_TUPLE_TYPE_SHORT,
1048 		.offset = offsetof(struct avs_tplg_pplcfg, req_size),
1049 		.parse = avs_parse_short_token,
1050 	},
1051 	{
1052 		.token = AVS_TKN_PPLCFG_PRIORITY_U8,
1053 		.type = SND_SOC_TPLG_TUPLE_TYPE_BYTE,
1054 		.offset = offsetof(struct avs_tplg_pplcfg, priority),
1055 		.parse = avs_parse_byte_token,
1056 	},
1057 	{
1058 		.token = AVS_TKN_PPLCFG_LOW_POWER_BOOL,
1059 		.type = SND_SOC_TPLG_TUPLE_TYPE_BOOL,
1060 		.offset = offsetof(struct avs_tplg_pplcfg, lp),
1061 		.parse = avs_parse_bool_token,
1062 	},
1063 	{
1064 		.token = AVS_TKN_PPLCFG_ATTRIBUTES_U16,
1065 		.type = SND_SOC_TPLG_TUPLE_TYPE_SHORT,
1066 		.offset = offsetof(struct avs_tplg_pplcfg, attributes),
1067 		.parse = avs_parse_short_token,
1068 	},
1069 	{
1070 		.token = AVS_TKN_PPLCFG_TRIGGER_U32,
1071 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
1072 		.offset = offsetof(struct avs_tplg_pplcfg, trigger),
1073 		.parse = avs_parse_word_token,
1074 	},
1075 };
1076 
avs_tplg_parse_pplcfgs(struct snd_soc_component * comp,struct snd_soc_tplg_vendor_array * tuples,u32 block_size)1077 static int avs_tplg_parse_pplcfgs(struct snd_soc_component *comp,
1078 				  struct snd_soc_tplg_vendor_array *tuples,
1079 				  u32 block_size)
1080 {
1081 	struct avs_soc_component *acomp = to_avs_soc_component(comp);
1082 	struct avs_tplg *tplg = acomp->tplg;
1083 
1084 	return parse_dictionary(comp, tuples, block_size, (void **)&tplg->pplcfgs,
1085 				&tplg->num_pplcfgs, sizeof(*tplg->pplcfgs),
1086 				AVS_TKN_MANIFEST_NUM_PPLCFGS_U32,
1087 				AVS_TKN_PPLCFG_ID_U32,
1088 				pplcfg_parsers, ARRAY_SIZE(pplcfg_parsers));
1089 }
1090 
1091 static const struct avs_tplg_token_parser binding_parsers[] = {
1092 	{
1093 		.token = AVS_TKN_BINDING_TARGET_TPLG_NAME_STRING,
1094 		.type = SND_SOC_TPLG_TUPLE_TYPE_STRING,
1095 		.offset = offsetof(struct avs_tplg_binding, target_tplg_name),
1096 		.parse = parse_link_formatted_string,
1097 	},
1098 	{
1099 		.token = AVS_TKN_BINDING_TARGET_PATH_TMPL_ID_U32,
1100 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
1101 		.offset = offsetof(struct avs_tplg_binding, target_path_tmpl_id),
1102 		.parse = avs_parse_word_token,
1103 	},
1104 	{
1105 		.token = AVS_TKN_BINDING_TARGET_PPL_ID_U32,
1106 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
1107 		.offset = offsetof(struct avs_tplg_binding, target_ppl_id),
1108 		.parse = avs_parse_word_token,
1109 	},
1110 	{
1111 		.token = AVS_TKN_BINDING_TARGET_MOD_ID_U32,
1112 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
1113 		.offset = offsetof(struct avs_tplg_binding, target_mod_id),
1114 		.parse = avs_parse_word_token,
1115 	},
1116 	{
1117 		.token = AVS_TKN_BINDING_TARGET_MOD_PIN_U8,
1118 		.type = SND_SOC_TPLG_TUPLE_TYPE_BYTE,
1119 		.offset = offsetof(struct avs_tplg_binding, target_mod_pin),
1120 		.parse = avs_parse_byte_token,
1121 	},
1122 	{
1123 		.token = AVS_TKN_BINDING_MOD_ID_U32,
1124 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
1125 		.offset = offsetof(struct avs_tplg_binding, mod_id),
1126 		.parse = avs_parse_word_token,
1127 	},
1128 	{
1129 		.token = AVS_TKN_BINDING_MOD_PIN_U8,
1130 		.type = SND_SOC_TPLG_TUPLE_TYPE_BYTE,
1131 		.offset = offsetof(struct avs_tplg_binding, mod_pin),
1132 		.parse = avs_parse_byte_token,
1133 	},
1134 	{
1135 		.token = AVS_TKN_BINDING_IS_SINK_U8,
1136 		.type = SND_SOC_TPLG_TUPLE_TYPE_BYTE,
1137 		.offset = offsetof(struct avs_tplg_binding, is_sink),
1138 		.parse = avs_parse_byte_token,
1139 	},
1140 };
1141 
avs_tplg_parse_bindings(struct snd_soc_component * comp,struct snd_soc_tplg_vendor_array * tuples,u32 block_size)1142 static int avs_tplg_parse_bindings(struct snd_soc_component *comp,
1143 				   struct snd_soc_tplg_vendor_array *tuples,
1144 				   u32 block_size)
1145 {
1146 	struct avs_soc_component *acomp = to_avs_soc_component(comp);
1147 	struct avs_tplg *tplg = acomp->tplg;
1148 
1149 	return parse_dictionary(comp, tuples, block_size, (void **)&tplg->bindings,
1150 				&tplg->num_bindings, sizeof(*tplg->bindings),
1151 				AVS_TKN_MANIFEST_NUM_BINDINGS_U32,
1152 				AVS_TKN_BINDING_ID_U32,
1153 				binding_parsers, ARRAY_SIZE(binding_parsers));
1154 }
1155 
1156 static const struct avs_tplg_token_parser module_parsers[] = {
1157 	{
1158 		.token = AVS_TKN_MOD_ID_U32,
1159 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
1160 		.offset = offsetof(struct avs_tplg_module, id),
1161 		.parse = avs_parse_word_token,
1162 	},
1163 	{
1164 		.token = AVS_TKN_MOD_MODCFG_BASE_ID_U32,
1165 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
1166 		.offset = offsetof(struct avs_tplg_module, cfg_base),
1167 		.parse = avs_parse_modcfg_base_ptr,
1168 	},
1169 	{
1170 		.token = AVS_TKN_MOD_IN_AFMT_ID_U32,
1171 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
1172 		.offset = offsetof(struct avs_tplg_module, in_fmt),
1173 		.parse = avs_parse_audio_format_ptr,
1174 	},
1175 	{
1176 		.token = AVS_TKN_MOD_CORE_ID_U8,
1177 		.type = SND_SOC_TPLG_TUPLE_TYPE_BYTE,
1178 		.offset = offsetof(struct avs_tplg_module, core_id),
1179 		.parse = avs_parse_byte_token,
1180 	},
1181 	{
1182 		.token = AVS_TKN_MOD_PROC_DOMAIN_U8,
1183 		.type = SND_SOC_TPLG_TUPLE_TYPE_BYTE,
1184 		.offset = offsetof(struct avs_tplg_module, domain),
1185 		.parse = avs_parse_byte_token,
1186 	},
1187 	{
1188 		.token = AVS_TKN_MOD_MODCFG_EXT_ID_U32,
1189 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
1190 		.offset = offsetof(struct avs_tplg_module, cfg_ext),
1191 		.parse = avs_parse_modcfg_ext_ptr,
1192 	},
1193 	{
1194 		.token = AVS_TKN_MOD_KCONTROL_ID_U32,
1195 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
1196 		.offset = offsetof(struct avs_tplg_module, ctl_id),
1197 		.parse = avs_parse_byte_token,
1198 	},
1199 	{
1200 		.token = AVS_TKN_MOD_INIT_CONFIG_NUM_IDS_U32,
1201 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
1202 		.offset = offsetof(struct avs_tplg_module, num_init_configs),
1203 		.parse = avs_parse_byte_token,
1204 	},
1205 	{
1206 		.token = AVS_TKN_MOD_NHLT_CONFIG_ID_U32,
1207 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
1208 		.offset = offsetof(struct avs_tplg_module, nhlt_config),
1209 		.parse = avs_parse_nhlt_config_ptr,
1210 	},
1211 };
1212 
1213 static const struct avs_tplg_token_parser init_config_parsers[] = {
1214 	{
1215 		.token = AVS_TKN_MOD_INIT_CONFIG_ID_U32,
1216 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
1217 		.offset = 0,
1218 		.parse = avs_parse_init_config_ptr,
1219 	},
1220 };
1221 
avs_tplg_module_init_configs(struct snd_soc_component * comp,struct avs_tplg_module * module,struct snd_soc_tplg_vendor_array * tuples,u32 block_size)1222 static int avs_tplg_module_init_configs(struct snd_soc_component *comp,
1223 					struct avs_tplg_module *module,
1224 					struct snd_soc_tplg_vendor_array *tuples, u32 block_size)
1225 {
1226 	struct avs_tplg_init_config **cfgs;
1227 	int ret;
1228 
1229 	if (!module->num_init_configs)
1230 		return -EINVAL;
1231 
1232 	cfgs = devm_kcalloc(comp->card->dev, module->num_init_configs, sizeof(*cfgs), GFP_KERNEL);
1233 	if (!cfgs)
1234 		return -ENOMEM;
1235 
1236 	ret = parse_dictionary_entries(comp, tuples, block_size, cfgs, module->num_init_configs,
1237 				       sizeof(*cfgs), AVS_TKN_MOD_INIT_CONFIG_ID_U32,
1238 				       init_config_parsers, ARRAY_SIZE(init_config_parsers));
1239 	if (!ret)
1240 		module->init_configs = cfgs;
1241 	return ret;
1242 }
1243 
1244 static struct avs_tplg_module *
avs_tplg_module_create(struct snd_soc_component * comp,struct avs_tplg_pipeline * owner,struct snd_soc_tplg_vendor_array * tuples,u32 block_size)1245 avs_tplg_module_create(struct snd_soc_component *comp, struct avs_tplg_pipeline *owner,
1246 		       struct snd_soc_tplg_vendor_array *tuples, u32 block_size)
1247 {
1248 	struct avs_tplg_module *module;
1249 	u32 esize;
1250 	int ret;
1251 
1252 	/* See where config id block starts. */
1253 	ret = avs_tplg_vendor_entry_size(tuples, block_size,
1254 					 AVS_TKN_MOD_INIT_CONFIG_ID_U32, &esize);
1255 	if (ret)
1256 		return ERR_PTR(ret);
1257 
1258 	module = devm_kzalloc(comp->card->dev, sizeof(*module), GFP_KERNEL);
1259 	if (!module)
1260 		return ERR_PTR(-ENOMEM);
1261 
1262 	ret = avs_parse_tokens(comp, module, module_parsers,
1263 			       ARRAY_SIZE(module_parsers), tuples, esize);
1264 	if (ret < 0)
1265 		return ERR_PTR(ret);
1266 
1267 	block_size -= esize;
1268 	/* Parse trailing config ids if any. */
1269 	if (block_size) {
1270 		tuples = avs_tplg_vendor_array_at(tuples, esize);
1271 
1272 		ret = avs_tplg_module_init_configs(comp, module, tuples, block_size);
1273 		if (ret)
1274 			return ERR_PTR(ret);
1275 	}
1276 
1277 	module->owner = owner;
1278 	INIT_LIST_HEAD(&module->node);
1279 
1280 	return module;
1281 }
1282 
1283 static const struct avs_tplg_token_parser pipeline_parsers[] = {
1284 	{
1285 		.token = AVS_TKN_PPL_ID_U32,
1286 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
1287 		.offset = offsetof(struct avs_tplg_pipeline, id),
1288 		.parse = avs_parse_word_token,
1289 	},
1290 	{
1291 		.token = AVS_TKN_PPL_PPLCFG_ID_U32,
1292 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
1293 		.offset = offsetof(struct avs_tplg_pipeline, cfg),
1294 		.parse = avs_parse_pplcfg_ptr,
1295 	},
1296 	{
1297 		.token = AVS_TKN_PPL_NUM_BINDING_IDS_U32,
1298 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
1299 		.offset = offsetof(struct avs_tplg_pipeline, num_bindings),
1300 		.parse = avs_parse_word_token,
1301 	},
1302 };
1303 
1304 static const struct avs_tplg_token_parser bindings_parsers[] = {
1305 	{
1306 		.token = AVS_TKN_PPL_BINDING_ID_U32,
1307 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
1308 		.offset = 0, /* to treat pipeline->bindings as dictionary */
1309 		.parse = avs_parse_binding_ptr,
1310 	},
1311 };
1312 
1313 static struct avs_tplg_pipeline *
avs_tplg_pipeline_create(struct snd_soc_component * comp,struct avs_tplg_path * owner,struct snd_soc_tplg_vendor_array * tuples,u32 block_size)1314 avs_tplg_pipeline_create(struct snd_soc_component *comp, struct avs_tplg_path *owner,
1315 			 struct snd_soc_tplg_vendor_array *tuples, u32 block_size)
1316 {
1317 	struct avs_tplg_pipeline *pipeline;
1318 	u32 modblk_size, offset;
1319 	int ret;
1320 
1321 	pipeline = devm_kzalloc(comp->card->dev, sizeof(*pipeline), GFP_KERNEL);
1322 	if (!pipeline)
1323 		return ERR_PTR(-ENOMEM);
1324 
1325 	pipeline->owner = owner;
1326 	INIT_LIST_HEAD(&pipeline->mod_list);
1327 
1328 	/* Pipeline header MUST be followed by at least one module. */
1329 	ret = avs_tplg_vendor_array_lookup(tuples, block_size,
1330 					   AVS_TKN_MOD_ID_U32, &offset);
1331 	if (!ret && !offset)
1332 		ret = -EINVAL;
1333 	if (ret)
1334 		return ERR_PTR(ret);
1335 
1336 	/* Process header which precedes module sections. */
1337 	ret = avs_parse_tokens(comp, pipeline, pipeline_parsers,
1338 			       ARRAY_SIZE(pipeline_parsers), tuples, offset);
1339 	if (ret < 0)
1340 		return ERR_PTR(ret);
1341 
1342 	block_size -= offset;
1343 	tuples = avs_tplg_vendor_array_at(tuples, offset);
1344 
1345 	/* Optionally, binding sections follow module ones. */
1346 	ret = avs_tplg_vendor_array_lookup_next(tuples, block_size,
1347 						AVS_TKN_PPL_BINDING_ID_U32, &offset);
1348 	if (ret) {
1349 		if (ret != -ENOENT)
1350 			return ERR_PTR(ret);
1351 
1352 		/* Does header information match actual block layout? */
1353 		if (pipeline->num_bindings)
1354 			return ERR_PTR(-EINVAL);
1355 
1356 		modblk_size = block_size;
1357 	} else {
1358 		pipeline->bindings = devm_kcalloc(comp->card->dev, pipeline->num_bindings,
1359 						  sizeof(*pipeline->bindings), GFP_KERNEL);
1360 		if (!pipeline->bindings)
1361 			return ERR_PTR(-ENOMEM);
1362 
1363 		modblk_size = offset;
1364 	}
1365 
1366 	block_size -= modblk_size;
1367 	do {
1368 		struct avs_tplg_module *module;
1369 		u32 esize;
1370 
1371 		ret = avs_tplg_vendor_entry_size(tuples, modblk_size,
1372 						 AVS_TKN_MOD_ID_U32, &esize);
1373 		if (ret)
1374 			return ERR_PTR(ret);
1375 
1376 		module = avs_tplg_module_create(comp, pipeline, tuples, esize);
1377 		if (IS_ERR(module)) {
1378 			dev_err(comp->dev, "parse module failed: %ld\n",
1379 				PTR_ERR(module));
1380 			return ERR_CAST(module);
1381 		}
1382 
1383 		list_add_tail(&module->node, &pipeline->mod_list);
1384 		modblk_size -= esize;
1385 		tuples = avs_tplg_vendor_array_at(tuples, esize);
1386 	} while (modblk_size > 0);
1387 
1388 	/* What's left is optional range of bindings. */
1389 	ret = parse_dictionary_entries(comp, tuples, block_size, pipeline->bindings,
1390 				       pipeline->num_bindings, sizeof(*pipeline->bindings),
1391 				       AVS_TKN_PPL_BINDING_ID_U32,
1392 				       bindings_parsers, ARRAY_SIZE(bindings_parsers));
1393 	if (ret)
1394 		return ERR_PTR(ret);
1395 
1396 	return pipeline;
1397 }
1398 
1399 static const struct avs_tplg_token_parser path_parsers[] = {
1400 	{
1401 		.token = AVS_TKN_PATH_ID_U32,
1402 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
1403 		.offset = offsetof(struct avs_tplg_path, id),
1404 		.parse = avs_parse_word_token,
1405 	},
1406 	{
1407 		.token = AVS_TKN_PATH_FE_FMT_ID_U32,
1408 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
1409 		.offset = offsetof(struct avs_tplg_path, fe_fmt),
1410 		.parse = avs_parse_audio_format_ptr,
1411 	},
1412 	{
1413 		.token = AVS_TKN_PATH_BE_FMT_ID_U32,
1414 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
1415 		.offset = offsetof(struct avs_tplg_path, be_fmt),
1416 		.parse = avs_parse_audio_format_ptr,
1417 	},
1418 };
1419 
1420 static const struct avs_tplg_token_parser condpath_parsers[] = {
1421 	{
1422 		.token = AVS_TKN_CONDPATH_ID_U32,
1423 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
1424 		.offset = offsetof(struct avs_tplg_path, id),
1425 		.parse = avs_parse_word_token,
1426 	},
1427 	{
1428 		.token = AVS_TKN_CONDPATH_SOURCE_PATH_ID_U32,
1429 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
1430 		.offset = offsetof(struct avs_tplg_path, source_path_id),
1431 		.parse = avs_parse_word_token,
1432 	},
1433 	{
1434 		.token = AVS_TKN_CONDPATH_SINK_PATH_ID_U32,
1435 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
1436 		.offset = offsetof(struct avs_tplg_path, sink_path_id),
1437 		.parse = avs_parse_word_token,
1438 	},
1439 };
1440 
1441 static struct avs_tplg_path *
avs_tplg_path_create(struct snd_soc_component * comp,struct avs_tplg_path_template * owner,struct snd_soc_tplg_vendor_array * tuples,u32 block_size,const struct avs_tplg_token_parser * parsers,u32 num_parsers)1442 avs_tplg_path_create(struct snd_soc_component *comp, struct avs_tplg_path_template *owner,
1443 		     struct snd_soc_tplg_vendor_array *tuples, u32 block_size,
1444 		     const struct avs_tplg_token_parser *parsers, u32 num_parsers)
1445 {
1446 	struct avs_tplg_pipeline *pipeline;
1447 	struct avs_tplg_path *path;
1448 	u32 offset;
1449 	int ret;
1450 
1451 	path = devm_kzalloc(comp->card->dev, sizeof(*path), GFP_KERNEL);
1452 	if (!path)
1453 		return ERR_PTR(-ENOMEM);
1454 
1455 	path->owner = owner;
1456 	INIT_LIST_HEAD(&path->ppl_list);
1457 	INIT_LIST_HEAD(&path->node);
1458 
1459 	/* Path header MAY be followed by one or more pipelines. */
1460 	ret = avs_tplg_vendor_array_lookup(tuples, block_size,
1461 					   AVS_TKN_PPL_ID_U32, &offset);
1462 	if (ret == -ENOENT)
1463 		offset = block_size;
1464 	else if (ret)
1465 		return ERR_PTR(ret);
1466 	else if (!offset)
1467 		return ERR_PTR(-EINVAL);
1468 
1469 	/* Process header which precedes pipeline sections. */
1470 	ret = avs_parse_tokens(comp, path, parsers, num_parsers, tuples, offset);
1471 	if (ret < 0)
1472 		return ERR_PTR(ret);
1473 
1474 	block_size -= offset;
1475 	tuples = avs_tplg_vendor_array_at(tuples, offset);
1476 	while (block_size > 0) {
1477 		u32 esize;
1478 
1479 		ret = avs_tplg_vendor_entry_size(tuples, block_size,
1480 						 AVS_TKN_PPL_ID_U32, &esize);
1481 		if (ret)
1482 			return ERR_PTR(ret);
1483 
1484 		pipeline = avs_tplg_pipeline_create(comp, path, tuples, esize);
1485 		if (IS_ERR(pipeline)) {
1486 			dev_err(comp->dev, "parse pipeline failed: %ld\n",
1487 				PTR_ERR(pipeline));
1488 			return ERR_CAST(pipeline);
1489 		}
1490 
1491 		list_add_tail(&pipeline->node, &path->ppl_list);
1492 		block_size -= esize;
1493 		tuples = avs_tplg_vendor_array_at(tuples, esize);
1494 	}
1495 
1496 	return path;
1497 }
1498 
1499 static const struct avs_tplg_token_parser path_tmpl_parsers[] = {
1500 	{
1501 		.token = AVS_TKN_PATH_TMPL_ID_U32,
1502 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
1503 		.offset = offsetof(struct avs_tplg_path_template, id),
1504 		.parse = avs_parse_word_token,
1505 	},
1506 };
1507 
1508 static const struct avs_tplg_token_parser condpath_tmpl_parsers[] = {
1509 	{
1510 		.token = AVS_TKN_CONDPATH_TMPL_ID_U32,
1511 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
1512 		.offset = offsetof(struct avs_tplg_path_template, id),
1513 		.parse = avs_parse_word_token,
1514 	},
1515 	{
1516 		.token = AVS_TKN_CONDPATH_TMPL_SOURCE_TPLG_NAME_STRING,
1517 		.type = SND_SOC_TPLG_TUPLE_TYPE_STRING,
1518 		.offset = offsetof(struct avs_tplg_path_template, source.tplg_name),
1519 		.parse = avs_parse_string_token,
1520 	},
1521 	{
1522 		.token = AVS_TKN_CONDPATH_TMPL_SOURCE_PATH_TMPL_ID_U32,
1523 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
1524 		.offset = offsetof(struct avs_tplg_path_template, source.id),
1525 		.parse = avs_parse_word_token,
1526 	},
1527 	{
1528 		.token = AVS_TKN_CONDPATH_TMPL_SINK_TPLG_NAME_STRING,
1529 		.type = SND_SOC_TPLG_TUPLE_TYPE_STRING,
1530 		.offset = offsetof(struct avs_tplg_path_template, sink.tplg_name),
1531 		.parse = avs_parse_string_token,
1532 	},
1533 	{
1534 		.token = AVS_TKN_CONDPATH_TMPL_SINK_PATH_TMPL_ID_U32,
1535 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
1536 		.offset = offsetof(struct avs_tplg_path_template, sink.id),
1537 		.parse = avs_parse_word_token,
1538 	},
1539 };
1540 
parse_path_template(struct snd_soc_component * comp,struct snd_soc_tplg_vendor_array * tuples,u32 block_size,struct avs_tplg_path_template * template,const struct avs_tplg_token_parser * tmpl_tokens,u32 num_tmpl_tokens,const struct avs_tplg_token_parser * path_tokens,u32 num_path_tokens)1541 static int parse_path_template(struct snd_soc_component *comp,
1542 			       struct snd_soc_tplg_vendor_array *tuples, u32 block_size,
1543 			       struct avs_tplg_path_template *template,
1544 			       const struct avs_tplg_token_parser *tmpl_tokens, u32 num_tmpl_tokens,
1545 			       const struct avs_tplg_token_parser *path_tokens, u32 num_path_tokens)
1546 {
1547 	struct avs_tplg_path *path;
1548 	u32 offset;
1549 	int ret;
1550 
1551 	/* Path template header MUST be followed by at least one path variant. */
1552 	ret = avs_tplg_vendor_array_lookup(tuples, block_size,
1553 					   AVS_TKN_PATH_ID_U32, &offset);
1554 	if (ret)
1555 		return ret;
1556 
1557 	/* Process header which precedes path variants sections. */
1558 	ret = avs_parse_tokens(comp, template, tmpl_tokens, num_tmpl_tokens, tuples, offset);
1559 	if (ret < 0)
1560 		return ret;
1561 
1562 	block_size -= offset;
1563 	tuples = avs_tplg_vendor_array_at(tuples, offset);
1564 	do {
1565 		u32 esize;
1566 
1567 		ret = avs_tplg_vendor_entry_size(tuples, block_size,
1568 						 AVS_TKN_PATH_ID_U32, &esize);
1569 		if (ret)
1570 			return ret;
1571 
1572 		path = avs_tplg_path_create(comp, template, tuples, esize, path_tokens,
1573 					    num_path_tokens);
1574 		if (IS_ERR(path)) {
1575 			dev_err(comp->dev, "parse path failed: %ld\n", PTR_ERR(path));
1576 			return PTR_ERR(path);
1577 		}
1578 
1579 		list_add_tail(&path->node, &template->path_list);
1580 		block_size -= esize;
1581 		tuples = avs_tplg_vendor_array_at(tuples, esize);
1582 	} while (block_size > 0);
1583 
1584 	return 0;
1585 }
1586 
1587 static struct avs_tplg_path_template *
avs_tplg_path_template_create(struct snd_soc_component * comp,struct avs_tplg * owner,struct snd_soc_tplg_vendor_array * tuples,u32 block_size)1588 avs_tplg_path_template_create(struct snd_soc_component *comp, struct avs_tplg *owner,
1589 			      struct snd_soc_tplg_vendor_array *tuples, u32 block_size)
1590 {
1591 	struct avs_tplg_path_template *template;
1592 	int ret;
1593 
1594 	template = devm_kzalloc(comp->card->dev, sizeof(*template), GFP_KERNEL);
1595 	if (!template)
1596 		return ERR_PTR(-ENOMEM);
1597 
1598 	template->owner = owner; /* Used to access component tplg is assigned to. */
1599 	INIT_LIST_HEAD(&template->path_list);
1600 	INIT_LIST_HEAD(&template->node);
1601 
1602 	ret = parse_path_template(comp, tuples, block_size, template, path_tmpl_parsers,
1603 				  ARRAY_SIZE(path_tmpl_parsers), path_parsers,
1604 				  ARRAY_SIZE(path_parsers));
1605 	if (ret)
1606 		return ERR_PTR(ret);
1607 
1608 	return template;
1609 }
1610 
avs_tplg_parse_condpath_templates(struct snd_soc_component * comp,struct snd_soc_tplg_vendor_array * tuples,u32 block_size)1611 static int avs_tplg_parse_condpath_templates(struct snd_soc_component *comp,
1612 					     struct snd_soc_tplg_vendor_array *tuples,
1613 					     u32 block_size)
1614 {
1615 	struct avs_soc_component *acomp = to_avs_soc_component(comp);
1616 	struct avs_tplg *tplg = acomp->tplg;
1617 	int ret, i;
1618 
1619 	ret = parse_dictionary_header(comp, tuples, (void **)&tplg->condpath_tmpls,
1620 				      &tplg->num_condpath_tmpls,
1621 				      sizeof(*tplg->condpath_tmpls),
1622 				      AVS_TKN_MANIFEST_NUM_CONDPATH_TMPLS_U32);
1623 	if (ret)
1624 		return ret;
1625 
1626 	block_size -= le32_to_cpu(tuples->size);
1627 	/* With header parsed, move on to parsing entries. */
1628 	tuples = avs_tplg_vendor_array_next(tuples);
1629 
1630 	for (i = 0; i < tplg->num_condpath_tmpls; i++) {
1631 		struct avs_tplg_path_template *template;
1632 		u32 esize;
1633 
1634 		template = &tplg->condpath_tmpls[i];
1635 		template->owner = tplg; /* Used when building sysfs hierarchy. */
1636 		INIT_LIST_HEAD(&template->path_list);
1637 		INIT_LIST_HEAD(&template->node);
1638 
1639 		ret = avs_tplg_vendor_entry_size(tuples, block_size,
1640 						 AVS_TKN_CONDPATH_TMPL_ID_U32, &esize);
1641 		if (ret)
1642 			return ret;
1643 
1644 		ret = parse_path_template(comp, tuples, esize, template,
1645 					  condpath_tmpl_parsers,
1646 					  ARRAY_SIZE(condpath_tmpl_parsers),
1647 					  condpath_parsers,
1648 					  ARRAY_SIZE(condpath_parsers));
1649 		if (ret < 0) {
1650 			dev_err(comp->dev, "parse condpath_tmpl: %d failed: %d\n", i, ret);
1651 			return ret;
1652 		}
1653 
1654 		block_size -= esize;
1655 		tuples = avs_tplg_vendor_array_at(tuples, esize);
1656 	}
1657 
1658 	return 0;
1659 }
1660 
1661 static const struct avs_tplg_token_parser mod_init_config_parsers[] = {
1662 	{
1663 		.token = AVS_TKN_INIT_CONFIG_ID_U32,
1664 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
1665 		.offset = offsetof(struct avs_tplg_init_config, id),
1666 		.parse = avs_parse_word_token,
1667 	},
1668 	{
1669 		.token = AVS_TKN_INIT_CONFIG_PARAM_U8,
1670 		.type = SND_SOC_TPLG_TUPLE_TYPE_BYTE,
1671 		.offset = offsetof(struct avs_tplg_init_config, param),
1672 		.parse = avs_parse_byte_token,
1673 	},
1674 	{
1675 		.token = AVS_TKN_INIT_CONFIG_LENGTH_U32,
1676 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
1677 		.offset = offsetof(struct avs_tplg_init_config, length),
1678 		.parse = avs_parse_word_token,
1679 	},
1680 };
1681 
avs_tplg_parse_initial_configs(struct snd_soc_component * comp,struct snd_soc_tplg_vendor_array * tuples,u32 block_size,u32 * offset)1682 static int avs_tplg_parse_initial_configs(struct snd_soc_component *comp,
1683 					   struct snd_soc_tplg_vendor_array *tuples,
1684 					   u32 block_size, u32 *offset)
1685 {
1686 	struct avs_soc_component *acomp = to_avs_soc_component(comp);
1687 	struct avs_tplg *tplg = acomp->tplg;
1688 	int ret, i;
1689 
1690 	*offset = 0;
1691 
1692 	/* Parse tuple section telling how many init configs there are. */
1693 	ret = parse_dictionary_header(comp, tuples, (void **)&tplg->init_configs,
1694 				      &tplg->num_init_configs,
1695 				      sizeof(*tplg->init_configs),
1696 				      AVS_TKN_MANIFEST_NUM_INIT_CONFIGS_U32);
1697 	if (ret)
1698 		return ret;
1699 
1700 	block_size -= le32_to_cpu(tuples->size);
1701 	*offset += le32_to_cpu(tuples->size);
1702 	/* With header parsed, move on to parsing entries. */
1703 	tuples = avs_tplg_vendor_array_next(tuples);
1704 
1705 	for (i = 0; i < tplg->num_init_configs && block_size > 0; i++) {
1706 		struct avs_tplg_init_config *config = &tplg->init_configs[i];
1707 		struct snd_soc_tplg_vendor_array *tmp;
1708 		void *init_config_data;
1709 		u32 esize;
1710 
1711 		/*
1712 		 * Usually to get section length we search for first token of next group of data,
1713 		 * but in this case we can't as tuples are followed by raw data.
1714 		 */
1715 		tmp = avs_tplg_vendor_array_next(tuples);
1716 		esize = le32_to_cpu(tuples->size) + le32_to_cpu(tmp->size);
1717 		*offset += esize;
1718 
1719 		ret = parse_dictionary_entries(comp, tuples, esize, config, 1, sizeof(*config),
1720 					       AVS_TKN_INIT_CONFIG_ID_U32,
1721 					       mod_init_config_parsers,
1722 					       ARRAY_SIZE(mod_init_config_parsers));
1723 
1724 		block_size -= esize;
1725 
1726 		/* handle raw data section */
1727 		init_config_data = (void *)tuples + esize;
1728 		esize = config->length;
1729 		*offset += esize;
1730 
1731 		config->data = devm_kmemdup(comp->card->dev, init_config_data, esize, GFP_KERNEL);
1732 		if (!config->data)
1733 			return -ENOMEM;
1734 
1735 		tuples = init_config_data + esize;
1736 		block_size -= esize;
1737 	}
1738 
1739 	return 0;
1740 }
1741 
1742 static const struct avs_tplg_token_parser mod_nhlt_config_parsers[] = {
1743 	{
1744 		.token = AVS_TKN_NHLT_CONFIG_ID_U32,
1745 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
1746 		.offset = offsetof(struct avs_tplg_nhlt_config, id),
1747 		.parse = avs_parse_word_token,
1748 	},
1749 	{
1750 		.token = AVS_TKN_NHLT_CONFIG_SIZE_U32,
1751 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
1752 		.offset = offsetof(struct avs_tplg_nhlt_config, blob),
1753 		.parse = avs_parse_nhlt_config_size,
1754 	},
1755 };
1756 
avs_tplg_parse_nhlt_configs(struct snd_soc_component * comp,struct snd_soc_tplg_vendor_array * tuples,u32 block_size)1757 static int avs_tplg_parse_nhlt_configs(struct snd_soc_component *comp,
1758 				       struct snd_soc_tplg_vendor_array *tuples,
1759 				       u32 block_size)
1760 {
1761 	struct avs_soc_component *acomp = to_avs_soc_component(comp);
1762 	struct avs_tplg *tplg = acomp->tplg;
1763 	int ret, i;
1764 
1765 	/* Parse the header section to know how many entries there are. */
1766 	ret = parse_dictionary_header(comp, tuples, (void **)&tplg->nhlt_configs,
1767 				      &tplg->num_nhlt_configs,
1768 				      sizeof(*tplg->nhlt_configs),
1769 				      AVS_TKN_MANIFEST_NUM_NHLT_CONFIGS_U32);
1770 	if (ret)
1771 		return ret;
1772 
1773 	block_size -= le32_to_cpu(tuples->size);
1774 	/* With the header parsed, move on to parsing entries. */
1775 	tuples = avs_tplg_vendor_array_next(tuples);
1776 
1777 	for (i = 0; i < tplg->num_nhlt_configs && block_size > 0; i++) {
1778 		struct avs_tplg_nhlt_config *config;
1779 		u32 esize;
1780 
1781 		config = &tplg->nhlt_configs[i];
1782 		esize = le32_to_cpu(tuples->size);
1783 
1784 		ret = parse_dictionary_entries(comp, tuples, esize, config, 1, sizeof(*config),
1785 					       AVS_TKN_NHLT_CONFIG_ID_U32,
1786 					       mod_nhlt_config_parsers,
1787 					       ARRAY_SIZE(mod_nhlt_config_parsers));
1788 		if (ret)
1789 			return ret;
1790 		/* With tuples parsed, the blob shall be allocated. */
1791 		if (!config->blob)
1792 			return -EINVAL;
1793 
1794 		/* Consume the raw data and move to the next entry. */
1795 		memcpy(config->blob->capabilities, (u8 *)tuples + esize,
1796 		       config->blob->capabilities_size);
1797 		esize += config->blob->capabilities_size;
1798 
1799 		block_size -= esize;
1800 		tuples = avs_tplg_vendor_array_at(tuples, esize);
1801 	}
1802 
1803 	return 0;
1804 }
1805 
avs_route_load(struct snd_soc_component * comp,int index,struct snd_soc_dapm_route * route)1806 static int avs_route_load(struct snd_soc_component *comp, int index,
1807 			  struct snd_soc_dapm_route *route)
1808 {
1809 	struct snd_soc_acpi_mach *mach = dev_get_platdata(comp->card->dev);
1810 	size_t len = SNDRV_CTL_ELEM_ID_NAME_MAXLEN;
1811 	int ssp_port, tdm_slot;
1812 	char *buf;
1813 
1814 	/* See parse_link_formatted_string() for dynamic naming when(s). */
1815 	if (!avs_mach_singular_ssp(mach))
1816 		return 0;
1817 	ssp_port = avs_mach_ssp_port(mach);
1818 
1819 	if (!avs_mach_singular_tdm(mach, ssp_port))
1820 		return 0;
1821 	tdm_slot = avs_mach_ssp_tdm(mach, ssp_port);
1822 
1823 	buf = devm_kzalloc(comp->card->dev, len, GFP_KERNEL);
1824 	if (!buf)
1825 		return -ENOMEM;
1826 	avs_ssp_sprint(buf, len, route->source, ssp_port, tdm_slot);
1827 	route->source = buf;
1828 
1829 	buf = devm_kzalloc(comp->card->dev, len, GFP_KERNEL);
1830 	if (!buf)
1831 		return -ENOMEM;
1832 	avs_ssp_sprint(buf, len, route->sink, ssp_port, tdm_slot);
1833 	route->sink = buf;
1834 
1835 	if (route->control) {
1836 		buf = devm_kzalloc(comp->card->dev, len, GFP_KERNEL);
1837 		if (!buf)
1838 			return -ENOMEM;
1839 		avs_ssp_sprint(buf, len, route->control, ssp_port, tdm_slot);
1840 		route->control = buf;
1841 	}
1842 
1843 	return 0;
1844 }
1845 
avs_widget_load(struct snd_soc_component * comp,int index,struct snd_soc_dapm_widget * w,struct snd_soc_tplg_dapm_widget * dw)1846 static int avs_widget_load(struct snd_soc_component *comp, int index,
1847 			   struct snd_soc_dapm_widget *w,
1848 			   struct snd_soc_tplg_dapm_widget *dw)
1849 {
1850 	struct snd_soc_acpi_mach *mach;
1851 	struct avs_tplg_path_template *template;
1852 	struct avs_soc_component *acomp = to_avs_soc_component(comp);
1853 	struct avs_tplg *tplg;
1854 	int ssp_port, tdm_slot;
1855 
1856 	if (!le32_to_cpu(dw->priv.size))
1857 		return 0;
1858 
1859 	w->no_wname_in_kcontrol_name = true;
1860 
1861 	if (w->ignore_suspend && !AVS_S0IX_SUPPORTED) {
1862 		dev_info_once(comp->dev, "Device does not support S0IX, check BIOS settings\n");
1863 		w->ignore_suspend = false;
1864 	}
1865 
1866 	tplg = acomp->tplg;
1867 	mach = dev_get_platdata(comp->card->dev);
1868 	if (!avs_mach_singular_ssp(mach))
1869 		goto static_name;
1870 	ssp_port = avs_mach_ssp_port(mach);
1871 
1872 	/* See parse_link_formatted_string() for dynamic naming when(s). */
1873 	if (avs_mach_singular_tdm(mach, ssp_port)) {
1874 		/* size is based on possible %d -> SSP:TDM, where SSP and TDM < 16 + '\0' */
1875 		size_t size = strlen(dw->name) + 3;
1876 		char *buf;
1877 
1878 		tdm_slot = avs_mach_ssp_tdm(mach, ssp_port);
1879 
1880 		buf = kmalloc(size, GFP_KERNEL);
1881 		if (!buf)
1882 			return -ENOMEM;
1883 		avs_ssp_sprint(buf, size, dw->name, ssp_port, tdm_slot);
1884 		kfree(w->name);
1885 		/* w->name is freed later by soc_tplg_dapm_widget_create() */
1886 		w->name = buf;
1887 	}
1888 
1889 static_name:
1890 	template = avs_tplg_path_template_create(comp, tplg, dw->priv.array,
1891 						 le32_to_cpu(dw->priv.size));
1892 	if (IS_ERR(template)) {
1893 		dev_err(comp->dev, "widget %s load failed: %ld\n", dw->name,
1894 			PTR_ERR(template));
1895 		return PTR_ERR(template);
1896 	}
1897 
1898 	w->priv = template; /* link path information to widget */
1899 	list_add_tail(&template->node, &tplg->path_tmpl_list);
1900 	return 0;
1901 }
1902 
avs_widget_ready(struct snd_soc_component * comp,int index,struct snd_soc_dapm_widget * w,struct snd_soc_tplg_dapm_widget * dw)1903 static int avs_widget_ready(struct snd_soc_component *comp, int index,
1904 			    struct snd_soc_dapm_widget *w,
1905 			    struct snd_soc_tplg_dapm_widget *dw)
1906 {
1907 	struct avs_tplg_path_template *template = w->priv;
1908 
1909 	template->w = w;
1910 	return 0;
1911 }
1912 
avs_dai_load(struct snd_soc_component * comp,int index,struct snd_soc_dai_driver * dai_drv,struct snd_soc_tplg_pcm * pcm,struct snd_soc_dai * dai)1913 static int avs_dai_load(struct snd_soc_component *comp, int index,
1914 			struct snd_soc_dai_driver *dai_drv, struct snd_soc_tplg_pcm *pcm,
1915 			struct snd_soc_dai *dai)
1916 {
1917 	u32 fe_subformats = SNDRV_PCM_SUBFMTBIT_MSBITS_20 |
1918 			    SNDRV_PCM_SUBFMTBIT_MSBITS_24 |
1919 			    SNDRV_PCM_SUBFMTBIT_MSBITS_MAX;
1920 
1921 	if (pcm) {
1922 		dai_drv->ops = &avs_dai_fe_ops;
1923 		dai_drv->capture.subformats = fe_subformats;
1924 		dai_drv->playback.subformats = fe_subformats;
1925 	}
1926 
1927 	return 0;
1928 }
1929 
avs_link_load(struct snd_soc_component * comp,int index,struct snd_soc_dai_link * link,struct snd_soc_tplg_link_config * cfg)1930 static int avs_link_load(struct snd_soc_component *comp, int index, struct snd_soc_dai_link *link,
1931 			 struct snd_soc_tplg_link_config *cfg)
1932 {
1933 	if (link->ignore_suspend && !AVS_S0IX_SUPPORTED) {
1934 		dev_info_once(comp->dev, "Device does not support S0IX, check BIOS settings\n");
1935 		link->ignore_suspend = false;
1936 	}
1937 
1938 	if (!link->no_pcm) {
1939 		/* Stream control handled by IPCs. */
1940 		link->nonatomic = true;
1941 
1942 		/* Open LINK (BE) pipes last and close them first to prevent xruns. */
1943 		link->trigger[0] = SND_SOC_DPCM_TRIGGER_PRE;
1944 		link->trigger[1] = SND_SOC_DPCM_TRIGGER_PRE;
1945 	} else {
1946 		/* Do not ignore codec capabilities. */
1947 		link->dpcm_merged_format = 1;
1948 	}
1949 
1950 	return 0;
1951 }
1952 
1953 static const struct avs_tplg_token_parser manifest_parsers[] = {
1954 	{
1955 		.token = AVS_TKN_MANIFEST_NAME_STRING,
1956 		.type = SND_SOC_TPLG_TUPLE_TYPE_STRING,
1957 		.offset = offsetof(struct avs_tplg, name),
1958 		.parse = parse_link_formatted_string,
1959 	},
1960 	{
1961 		.token = AVS_TKN_MANIFEST_VERSION_U32,
1962 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
1963 		.offset = offsetof(struct avs_tplg, version),
1964 		.parse = avs_parse_word_token,
1965 	},
1966 };
1967 
avs_manifest(struct snd_soc_component * comp,int index,struct snd_soc_tplg_manifest * manifest)1968 static int avs_manifest(struct snd_soc_component *comp, int index,
1969 			struct snd_soc_tplg_manifest *manifest)
1970 {
1971 	struct snd_soc_tplg_vendor_array *tuples = manifest->priv.array;
1972 	struct avs_soc_component *acomp = to_avs_soc_component(comp);
1973 	size_t remaining = le32_to_cpu(manifest->priv.size);
1974 	bool has_init_config = true;
1975 	u32 offset;
1976 	int ret;
1977 
1978 	ret = avs_tplg_vendor_array_lookup(tuples, remaining,
1979 					   AVS_TKN_MANIFEST_NUM_LIBRARIES_U32, &offset);
1980 	/* Manifest MUST begin with a header. */
1981 	if (!ret && !offset)
1982 		ret = -EINVAL;
1983 	if (ret) {
1984 		dev_err(comp->dev, "incorrect manifest format: %d\n", ret);
1985 		return ret;
1986 	}
1987 
1988 	/* Process header which precedes any of the dictionaries. */
1989 	ret = avs_parse_tokens(comp, acomp->tplg, manifest_parsers,
1990 			       ARRAY_SIZE(manifest_parsers), tuples, offset);
1991 	if (ret < 0)
1992 		return ret;
1993 
1994 	remaining -= offset;
1995 	tuples = avs_tplg_vendor_array_at(tuples, offset);
1996 
1997 	ret = avs_tplg_vendor_array_lookup(tuples, remaining,
1998 					   AVS_TKN_MANIFEST_NUM_AFMTS_U32, &offset);
1999 	if (ret) {
2000 		dev_err(comp->dev, "audio formats lookup failed: %d\n", ret);
2001 		return ret;
2002 	}
2003 
2004 	/* Libraries dictionary. */
2005 	ret = avs_tplg_parse_libraries(comp, tuples, offset);
2006 	if (ret < 0)
2007 		return ret;
2008 
2009 	remaining -= offset;
2010 	tuples = avs_tplg_vendor_array_at(tuples, offset);
2011 
2012 	ret = avs_tplg_vendor_array_lookup(tuples, remaining,
2013 					   AVS_TKN_MANIFEST_NUM_MODCFGS_BASE_U32, &offset);
2014 	if (ret) {
2015 		dev_err(comp->dev, "modcfgs_base lookup failed: %d\n", ret);
2016 		return ret;
2017 	}
2018 
2019 	/* Audio formats dictionary. */
2020 	ret = avs_tplg_parse_audio_formats(comp, tuples, offset);
2021 	if (ret < 0)
2022 		return ret;
2023 
2024 	remaining -= offset;
2025 	tuples = avs_tplg_vendor_array_at(tuples, offset);
2026 
2027 	ret = avs_tplg_vendor_array_lookup(tuples, remaining,
2028 					   AVS_TKN_MANIFEST_NUM_MODCFGS_EXT_U32, &offset);
2029 	if (ret) {
2030 		dev_err(comp->dev, "modcfgs_ext lookup failed: %d\n", ret);
2031 		return ret;
2032 	}
2033 
2034 	/* Module configs-base dictionary. */
2035 	ret = avs_tplg_parse_modcfgs_base(comp, tuples, offset);
2036 	if (ret < 0)
2037 		return ret;
2038 
2039 	remaining -= offset;
2040 	tuples = avs_tplg_vendor_array_at(tuples, offset);
2041 
2042 	ret = avs_tplg_vendor_array_lookup(tuples, remaining,
2043 					   AVS_TKN_MANIFEST_NUM_PPLCFGS_U32, &offset);
2044 	if (ret) {
2045 		dev_err(comp->dev, "pplcfgs lookup failed: %d\n", ret);
2046 		return ret;
2047 	}
2048 
2049 	/* Module configs-ext dictionary. */
2050 	ret = avs_tplg_parse_modcfgs_ext(comp, tuples, offset);
2051 	if (ret < 0)
2052 		return ret;
2053 
2054 	remaining -= offset;
2055 	tuples = avs_tplg_vendor_array_at(tuples, offset);
2056 
2057 	ret = avs_tplg_vendor_array_lookup(tuples, remaining,
2058 					   AVS_TKN_MANIFEST_NUM_BINDINGS_U32, &offset);
2059 	if (ret) {
2060 		dev_err(comp->dev, "bindings lookup failed: %d\n", ret);
2061 		return ret;
2062 	}
2063 
2064 	/* Pipeline configs dictionary. */
2065 	ret = avs_tplg_parse_pplcfgs(comp, tuples, offset);
2066 	if (ret < 0)
2067 		return ret;
2068 
2069 	remaining -= offset;
2070 	tuples = avs_tplg_vendor_array_at(tuples, offset);
2071 
2072 	ret = avs_tplg_vendor_array_lookup(tuples, remaining,
2073 					   AVS_TKN_MANIFEST_NUM_CONDPATH_TMPLS_U32, &offset);
2074 	if (ret) {
2075 		dev_err(comp->dev, "condpath lookup failed: %d\n", ret);
2076 		return ret;
2077 	}
2078 
2079 	/* Bindings dictionary. */
2080 	ret = avs_tplg_parse_bindings(comp, tuples, offset);
2081 	if (ret < 0)
2082 		return ret;
2083 
2084 	remaining -= offset;
2085 	tuples = avs_tplg_vendor_array_at(tuples, offset);
2086 
2087 	ret = avs_tplg_vendor_array_lookup(tuples, remaining,
2088 					   AVS_TKN_MANIFEST_NUM_INIT_CONFIGS_U32, &offset);
2089 	if (ret == -ENOENT) {
2090 		dev_dbg(comp->dev, "init config lookup failed: %d\n", ret);
2091 		has_init_config = false;
2092 	} else if (ret) {
2093 		dev_err(comp->dev, "init config lookup failed: %d\n", ret);
2094 		return ret;
2095 	}
2096 
2097 	/* Condpaths dictionary. */
2098 	ret = avs_tplg_parse_condpath_templates(comp, tuples,
2099 						has_init_config ? offset : remaining);
2100 	if (ret < 0)
2101 		return ret;
2102 
2103 	if (!has_init_config)
2104 		return 0;
2105 
2106 	remaining -= offset;
2107 	tuples = avs_tplg_vendor_array_at(tuples, offset);
2108 
2109 	/* Initial configs dictionary. */
2110 	ret = avs_tplg_parse_initial_configs(comp, tuples, remaining, &offset);
2111 	if (ret < 0)
2112 		return ret;
2113 
2114 	remaining -= offset;
2115 	tuples = avs_tplg_vendor_array_at(tuples, offset);
2116 
2117 	ret = avs_tplg_vendor_array_lookup(tuples, remaining,
2118 					   AVS_TKN_MANIFEST_NUM_NHLT_CONFIGS_U32, &offset);
2119 	if (ret == -ENOENT)
2120 		return 0;
2121 	if (ret) {
2122 		dev_err(comp->dev, "NHLT config lookup failed: %d\n", ret);
2123 		return ret;
2124 	}
2125 
2126 	tuples = avs_tplg_vendor_array_at(tuples, offset);
2127 
2128 	/* NHLT configs dictionary. */
2129 	return avs_tplg_parse_nhlt_configs(comp, tuples, remaining);
2130 }
2131 
2132 enum {
2133 	AVS_CONTROL_OPS_VOLUME = 257,
2134 	AVS_CONTROL_OPS_MUTE,
2135 };
2136 
2137 static const struct snd_soc_tplg_kcontrol_ops avs_control_ops[] = {
2138 	{
2139 		.id = AVS_CONTROL_OPS_VOLUME,
2140 		.get = avs_control_volume_get,
2141 		.put = avs_control_volume_put,
2142 		.info = avs_control_volume_info,
2143 	},
2144 	{
2145 		.id = AVS_CONTROL_OPS_MUTE,
2146 		.get = avs_control_mute_get,
2147 		.put = avs_control_mute_put,
2148 		.info = avs_control_mute_info,
2149 	},
2150 };
2151 
2152 static const struct avs_tplg_token_parser control_parsers[] = {
2153 	{
2154 		.token = AVS_TKN_KCONTROL_ID_U32,
2155 		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
2156 		.offset = offsetof(struct avs_control_data, id),
2157 		.parse = avs_parse_word_token,
2158 	},
2159 };
2160 
2161 static int
avs_control_load(struct snd_soc_component * comp,int index,struct snd_kcontrol_new * ctmpl,struct snd_soc_tplg_ctl_hdr * hdr)2162 avs_control_load(struct snd_soc_component *comp, int index, struct snd_kcontrol_new *ctmpl,
2163 		 struct snd_soc_tplg_ctl_hdr *hdr)
2164 {
2165 	struct snd_soc_tplg_vendor_array *tuples;
2166 	struct snd_soc_tplg_mixer_control *tmc;
2167 	struct avs_control_data *ctl_data;
2168 	struct soc_mixer_control *mc;
2169 	size_t block_size;
2170 	int ret, i;
2171 
2172 	switch (le32_to_cpu(hdr->type)) {
2173 	case SND_SOC_TPLG_TYPE_MIXER:
2174 		break;
2175 	default:
2176 		return -EINVAL;
2177 	}
2178 
2179 	mc = (struct soc_mixer_control *)ctmpl->private_value;
2180 	tmc = container_of(hdr, typeof(*tmc), hdr);
2181 	tuples = tmc->priv.array;
2182 	block_size = le32_to_cpu(tmc->priv.size);
2183 
2184 	ctl_data = devm_kzalloc(comp->card->dev, sizeof(*ctl_data), GFP_KERNEL);
2185 	if (!ctl_data)
2186 		return -ENOMEM;
2187 
2188 	ret = parse_dictionary_entries(comp, tuples, block_size, ctl_data, 1, sizeof(*ctl_data),
2189 				       AVS_TKN_KCONTROL_ID_U32, control_parsers,
2190 				       ARRAY_SIZE(control_parsers));
2191 	if (ret)
2192 		return ret;
2193 
2194 	mc->dobj.private = ctl_data;
2195 	if (tmc->invert) {
2196 		ctl_data->values[0] = mc->max;
2197 		for (i = 1; i < mc->num_channels; i++)
2198 			ctl_data->values[i] = mc->max;
2199 	}
2200 
2201 	return 0;
2202 }
2203 
2204 const struct snd_soc_tplg_ops avs_tplg_ops = {
2205 	.io_ops			= avs_control_ops,
2206 	.io_ops_count		= ARRAY_SIZE(avs_control_ops),
2207 	.control_load		= avs_control_load,
2208 	.dapm_route_load	= avs_route_load,
2209 	.widget_load		= avs_widget_load,
2210 	.widget_ready		= avs_widget_ready,
2211 	.dai_load		= avs_dai_load,
2212 	.link_load		= avs_link_load,
2213 	.manifest		= avs_manifest,
2214 };
2215 
avs_tplg_new(struct snd_soc_component * comp)2216 struct avs_tplg *avs_tplg_new(struct snd_soc_component *comp)
2217 {
2218 	struct avs_tplg *tplg;
2219 
2220 	tplg = devm_kzalloc(comp->card->dev, sizeof(*tplg), GFP_KERNEL);
2221 	if (!tplg)
2222 		return NULL;
2223 
2224 	tplg->comp = comp;
2225 	INIT_LIST_HEAD(&tplg->path_tmpl_list);
2226 
2227 	return tplg;
2228 }
2229 
avs_load_topology(struct snd_soc_component * comp,const char * filename)2230 int avs_load_topology(struct snd_soc_component *comp, const char *filename)
2231 {
2232 	const struct firmware *fw __free(firmware) = NULL;
2233 	int ret;
2234 
2235 	ret = request_firmware(&fw, filename, comp->dev);
2236 	if (ret < 0) {
2237 		dev_err(comp->dev, "request topology \"%s\" failed: %d\n", filename, ret);
2238 		return ret;
2239 	}
2240 
2241 	ret = snd_soc_tplg_component_load(comp, &avs_tplg_ops, fw);
2242 	if (ret < 0)
2243 		dev_err(comp->dev, "load topology \"%s\" failed: %d\n", filename, ret);
2244 
2245 	return ret;
2246 }
2247 
avs_remove_topology(struct snd_soc_component * comp)2248 int avs_remove_topology(struct snd_soc_component *comp)
2249 {
2250 	snd_soc_tplg_component_remove(comp);
2251 
2252 	return 0;
2253 }
2254