topology.c 45.1 KB
Newer Older
1 2 3 4 5 6 7 8
// SPDX-License-Identifier: GPL-2.0-only
//
// Copyright(c) 2021 Intel Corporation. All rights reserved.
//
// Authors: Cezary Rojewski <cezary.rojewski@intel.com>
//          Amadeusz Slawinski <amadeuszx.slawinski@linux.intel.com>
//

9
#include <linux/firmware.h>
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
#include <linux/uuid.h>
#include <sound/soc.h>
#include <sound/soc-acpi.h>
#include <sound/soc-topology.h>
#include <uapi/sound/intel/avs/tokens.h>
#include "avs.h"
#include "topology.h"

/* Get pointer to vendor array at the specified offset. */
#define avs_tplg_vendor_array_at(array, offset) \
	((struct snd_soc_tplg_vendor_array *)((u8 *)array + offset))

/* Get pointer to vendor array that is next in line. */
#define avs_tplg_vendor_array_next(array) \
	(avs_tplg_vendor_array_at(array, le32_to_cpu((array)->size)))

/*
 * Scan provided block of tuples for the specified token. If found,
 * @offset is updated with position at which first matching token is
 * located.
 *
 * Returns 0 on success, -ENOENT if not found and error code otherwise.
 */
static int
avs_tplg_vendor_array_lookup(struct snd_soc_tplg_vendor_array *tuples,
			     u32 block_size, u32 token, u32 *offset)
{
	u32 pos = 0;

	while (block_size > 0) {
		struct snd_soc_tplg_vendor_value_elem *tuple;
		u32 tuples_size = le32_to_cpu(tuples->size);

		if (tuples_size > block_size)
			return -EINVAL;

		tuple = tuples->value;
		if (le32_to_cpu(tuple->token) == token) {
			*offset = pos;
			return 0;
		}

		block_size -= tuples_size;
		pos += tuples_size;
		tuples = avs_tplg_vendor_array_next(tuples);
	}

	return -ENOENT;
}

/*
 * See avs_tplg_vendor_array_lookup() for description.
 *
 * Behaves exactly like avs_tplg_vendor_lookup() but starts from the
 * next vendor array in line. Useful when searching for the finish line
 * of an arbitrary entry in a list of entries where each is composed of
 * several vendor tuples and a specific token marks the beginning of
 * a new entry block.
 */
static int
avs_tplg_vendor_array_lookup_next(struct snd_soc_tplg_vendor_array *tuples,
				  u32 block_size, u32 token, u32 *offset)
{
	u32 tuples_size = le32_to_cpu(tuples->size);
	int ret;

	if (tuples_size > block_size)
		return -EINVAL;

	tuples = avs_tplg_vendor_array_next(tuples);
	block_size -= tuples_size;

	ret = avs_tplg_vendor_array_lookup(tuples, block_size, token, offset);
	if (!ret)
		*offset += tuples_size;
	return ret;
}

/*
 * Scan provided block of tuples for the specified token which marks
 * the border of an entry block. Behavior is similar to
 * avs_tplg_vendor_array_lookup() except 0 is also returned if no
 * matching token has been found. In such case, returned @size is
 * assigned to @block_size as the entire block belongs to the current
 * entry.
 *
 * Returns 0 on success, error code otherwise.
 */
static int
avs_tplg_vendor_entry_size(struct snd_soc_tplg_vendor_array *tuples,
			   u32 block_size, u32 entry_id_token, u32 *size)
{
	int ret;

	ret = avs_tplg_vendor_array_lookup_next(tuples, block_size, entry_id_token, size);
	if (ret == -ENOENT) {
		*size = block_size;
		ret = 0;
	}

	return ret;
}

/*
 * Vendor tuple parsing descriptor.
 *
 * @token: vendor specific token that identifies tuple
 * @type: tuple type, one of SND_SOC_TPLG_TUPLE_TYPE_XXX
 * @offset: offset of a struct's field to initialize
 * @parse: parsing function, extracts and assigns value to object's field
 */
struct avs_tplg_token_parser {
	enum avs_tplg_token token;
	u32 type;
	u32 offset;
	int (*parse)(struct snd_soc_component *comp, void *elem, void *object, u32 offset);
};

static int
avs_parse_uuid_token(struct snd_soc_component *comp, void *elem, void *object, u32 offset)
{
	struct snd_soc_tplg_vendor_value_elem *tuple = elem;
	guid_t *val = (guid_t *)((u8 *)object + offset);

	guid_copy((guid_t *)val, (const guid_t *)&tuple->value);

	return 0;
}

static int
avs_parse_bool_token(struct snd_soc_component *comp, void *elem, void *object, u32 offset)
{
	struct snd_soc_tplg_vendor_value_elem *tuple = elem;
	bool *val = (bool *)((u8 *)object + offset);

	*val = le32_to_cpu(tuple->value);

	return 0;
}

static int
avs_parse_byte_token(struct snd_soc_component *comp, void *elem, void *object, u32 offset)
{
	struct snd_soc_tplg_vendor_value_elem *tuple = elem;
	u8 *val = ((u8 *)object + offset);

	*val = le32_to_cpu(tuple->value);

	return 0;
}

static int
avs_parse_short_token(struct snd_soc_component *comp, void *elem, void *object, u32 offset)
{
	struct snd_soc_tplg_vendor_value_elem *tuple = elem;
	u16 *val = (u16 *)((u8 *)object + offset);

	*val = le32_to_cpu(tuple->value);

	return 0;
}

static int
avs_parse_word_token(struct snd_soc_component *comp, void *elem, void *object, u32 offset)
{
	struct snd_soc_tplg_vendor_value_elem *tuple = elem;
	u32 *val = (u32 *)((u8 *)object + offset);

	*val = le32_to_cpu(tuple->value);

	return 0;
}

static int
avs_parse_string_token(struct snd_soc_component *comp, void *elem, void *object, u32 offset)
{
	struct snd_soc_tplg_vendor_string_elem *tuple = elem;
	char *val = (char *)((u8 *)object + offset);

	snprintf(val, SNDRV_CTL_ELEM_ID_NAME_MAXLEN, "%s", tuple->string);

	return 0;
}

static int 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)
{
	struct snd_soc_tplg_vendor_uuid_elem *tuple;
	int ret, i, j;

	/* Parse element by element. */
	for (i = 0; i < le32_to_cpu(tuples->num_elems); i++) {
		tuple = &tuples->uuid[i];

		for (j = 0; j < count; j++) {
			/* Ignore non-UUID tokens. */
			if (parsers[j].type != SND_SOC_TPLG_TUPLE_TYPE_UUID ||
			    parsers[j].token != le32_to_cpu(tuple->token))
				continue;

			ret = parsers[j].parse(comp, tuple, object, parsers[j].offset);
			if (ret)
				return ret;
		}
	}

	return 0;
}

static int 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)
{
	struct snd_soc_tplg_vendor_string_elem *tuple;
	int ret, i, j;

	/* Parse element by element. */
	for (i = 0; i < le32_to_cpu(tuples->num_elems); i++) {
		tuple = &tuples->string[i];

		for (j = 0; j < count; j++) {
			/* Ignore non-string tokens. */
			if (parsers[j].type != SND_SOC_TPLG_TUPLE_TYPE_STRING ||
			    parsers[j].token != le32_to_cpu(tuple->token))
				continue;

			ret = parsers[j].parse(comp, tuple, object, parsers[j].offset);
			if (ret)
				return ret;
		}
	}

	return 0;
}

static int 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)
{
	struct snd_soc_tplg_vendor_value_elem *tuple;
	int ret, i, j;

	/* Parse element by element. */
	for (i = 0; i < le32_to_cpu(tuples->num_elems); i++) {
		tuple = &tuples->value[i];

		for (j = 0; j < count; j++) {
			/* Ignore non-integer tokens. */
			if (!(parsers[j].type == SND_SOC_TPLG_TUPLE_TYPE_WORD ||
			      parsers[j].type == SND_SOC_TPLG_TUPLE_TYPE_SHORT ||
			      parsers[j].type == SND_SOC_TPLG_TUPLE_TYPE_BYTE ||
			      parsers[j].type == SND_SOC_TPLG_TUPLE_TYPE_BOOL))
				continue;

			if (parsers[j].token != le32_to_cpu(tuple->token))
				continue;

			ret = parsers[j].parse(comp, tuple, object, parsers[j].offset);
			if (ret)
				return ret;
		}
	}

	return 0;
}

static int 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)
{
	int array_size, ret;

	while (priv_size > 0) {
		array_size = le32_to_cpu(tuples->size);

		if (array_size <= 0) {
			dev_err(comp->dev, "invalid array size 0x%x\n", array_size);
			return -EINVAL;
		}

		/* Make sure there is enough data before parsing. */
		priv_size -= array_size;
		if (priv_size < 0) {
			dev_err(comp->dev, "invalid array size 0x%x\n", array_size);
			return -EINVAL;
		}

		switch (le32_to_cpu(tuples->type)) {
		case SND_SOC_TPLG_TUPLE_TYPE_UUID:
			ret = avs_parse_uuid_tokens(comp, object, parsers, count, tuples);
			break;
		case SND_SOC_TPLG_TUPLE_TYPE_STRING:
			ret = avs_parse_string_tokens(comp, object, parsers, count, tuples);
			break;
		case SND_SOC_TPLG_TUPLE_TYPE_BOOL:
		case SND_SOC_TPLG_TUPLE_TYPE_BYTE:
		case SND_SOC_TPLG_TUPLE_TYPE_SHORT:
		case SND_SOC_TPLG_TUPLE_TYPE_WORD:
			ret = avs_parse_word_tokens(comp, object, parsers, count, tuples);
			break;
		default:
			dev_err(comp->dev, "unknown token type %d\n", tuples->type);
			ret = -EINVAL;
		}

		if (ret) {
			dev_err(comp->dev, "parsing %zu tokens of %d type failed: %d\n",
				count, tuples->type, ret);
			return ret;
		}

		tuples = avs_tplg_vendor_array_next(tuples);
	}

	return 0;
}

#define AVS_DEFINE_PTR_PARSER(name, type, member) \
static int \
avs_parse_##name##_ptr(struct snd_soc_component *comp, void *elem, void *object, u32 offset) \
{ \
	struct snd_soc_tplg_vendor_value_elem *tuple = elem;		\
	struct avs_soc_component *acomp = to_avs_soc_component(comp);	\
	type **val = (type **)(object + offset);			\
	u32 idx;							\
									\
	idx = le32_to_cpu(tuple->value);				\
	if (idx >= acomp->tplg->num_##member)				\
		return -EINVAL;						\
									\
	*val = &acomp->tplg->member[idx];				\
									\
	return 0;							\
}

AVS_DEFINE_PTR_PARSER(audio_format, struct avs_audio_format, fmts);
AVS_DEFINE_PTR_PARSER(modcfg_base, struct avs_tplg_modcfg_base, modcfgs_base);
348
AVS_DEFINE_PTR_PARSER(modcfg_ext, struct avs_tplg_modcfg_ext, modcfgs_ext);
349 350
AVS_DEFINE_PTR_PARSER(pplcfg, struct avs_tplg_pplcfg, pplcfgs);
AVS_DEFINE_PTR_PARSER(binding, struct avs_tplg_binding, bindings);
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372

static int
parse_audio_format_bitfield(struct snd_soc_component *comp, void *elem, void *object, u32 offset)
{
	struct snd_soc_tplg_vendor_value_elem *velem = elem;
	struct avs_audio_format *audio_format = object;

	switch (offset) {
	case AVS_TKN_AFMT_NUM_CHANNELS_U32:
		audio_format->num_channels = le32_to_cpu(velem->value);
		break;
	case AVS_TKN_AFMT_VALID_BIT_DEPTH_U32:
		audio_format->valid_bit_depth = le32_to_cpu(velem->value);
		break;
	case AVS_TKN_AFMT_SAMPLE_TYPE_U32:
		audio_format->sample_type = le32_to_cpu(velem->value);
		break;
	}

	return 0;
}

373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
static int parse_link_formatted_string(struct snd_soc_component *comp, void *elem,
				       void *object, u32 offset)
{
	struct snd_soc_tplg_vendor_string_elem *tuple = elem;
	struct snd_soc_acpi_mach *mach = dev_get_platdata(comp->card->dev);
	char *val = (char *)((u8 *)object + offset);

	/*
	 * Dynamic naming - string formats, e.g.: ssp%d - supported only for
	 * topologies describing single device e.g.: an I2S codec on SSP0.
	 */
	if (hweight_long(mach->link_mask) != 1)
		return avs_parse_string_token(comp, elem, object, offset);

	snprintf(val, SNDRV_CTL_ELEM_ID_NAME_MAXLEN, tuple->string,
		 __ffs(mach->link_mask));

	return 0;
}

393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
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)
{
	struct snd_soc_tplg_vendor_value_elem *tuple;

	/* Dictionary header consists of single tuple - entry count. */
	tuple = tuples->value;
	if (le32_to_cpu(tuple->token) != num_entries_token) {
		dev_err(comp->dev, "invalid dictionary header, expected: %d\n",
			num_entries_token);
		return -EINVAL;
	}

	*num_entries = le32_to_cpu(tuple->value);
	*dict = devm_kcalloc(comp->card->dev, *num_entries, entry_size, GFP_KERNEL);
	if (!*dict)
		return -ENOMEM;

	return 0;
}

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)
{
	void *pos = dict;
	int i;

	for (i = 0; i < num_entries; i++) {
		u32 esize;
		int ret;

		ret = avs_tplg_vendor_entry_size(tuples, block_size,
						 entry_id_token, &esize);
		if (ret)
			return ret;

		ret = avs_parse_tokens(comp, pos, parsers, num_parsers, tuples, esize);
		if (ret < 0) {
			dev_err(comp->dev, "parse entry: %d of type: %d failed: %d\n",
				i, entry_id_token, ret);
			return ret;
		}

		pos += entry_size;
		block_size -= esize;
		tuples = avs_tplg_vendor_array_at(tuples, esize);
	}

	return 0;
}

static int 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)
{
	int ret;

	ret = parse_dictionary_header(comp, tuples, dict, num_entries,
				      entry_size, num_entries_token);
	if (ret)
		return ret;

	block_size -= le32_to_cpu(tuples->size);
	/* With header parsed, move on to parsing entries. */
	tuples = avs_tplg_vendor_array_next(tuples);

	return parse_dictionary_entries(comp, tuples, block_size, *dict,
					*num_entries, entry_size,
					entry_id_token, parsers, num_parsers);
}

static const struct avs_tplg_token_parser library_parsers[] = {
	{
		.token = AVS_TKN_LIBRARY_NAME_STRING,
		.type = SND_SOC_TPLG_TUPLE_TYPE_STRING,
		.offset = offsetof(struct avs_tplg_library, name),
		.parse = avs_parse_string_token,
	},
};

static int avs_tplg_parse_libraries(struct snd_soc_component *comp,
				    struct snd_soc_tplg_vendor_array *tuples, u32 block_size)
{
	struct avs_soc_component *acomp = to_avs_soc_component(comp);
	struct avs_tplg *tplg = acomp->tplg;

	return parse_dictionary(comp, tuples, block_size, (void **)&tplg->libs,
				&tplg->num_libs, sizeof(*tplg->libs),
				AVS_TKN_MANIFEST_NUM_LIBRARIES_U32,
				AVS_TKN_LIBRARY_ID_U32,
				library_parsers, ARRAY_SIZE(library_parsers));
}

static const struct avs_tplg_token_parser audio_format_parsers[] = {
	{
		.token = AVS_TKN_AFMT_SAMPLE_RATE_U32,
		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
		.offset = offsetof(struct avs_audio_format, sampling_freq),
		.parse = avs_parse_word_token,
	},
	{
		.token = AVS_TKN_AFMT_BIT_DEPTH_U32,
		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
		.offset = offsetof(struct avs_audio_format, bit_depth),
		.parse = avs_parse_word_token,
	},
	{
		.token = AVS_TKN_AFMT_CHANNEL_MAP_U32,
		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
		.offset = offsetof(struct avs_audio_format, channel_map),
		.parse = avs_parse_word_token,
	},
	{
		.token = AVS_TKN_AFMT_CHANNEL_CFG_U32,
		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
		.offset = offsetof(struct avs_audio_format, channel_config),
		.parse = avs_parse_word_token,
	},
	{
		.token = AVS_TKN_AFMT_INTERLEAVING_U32,
		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
		.offset = offsetof(struct avs_audio_format, interleaving),
		.parse = avs_parse_word_token,
	},
	{
		.token = AVS_TKN_AFMT_NUM_CHANNELS_U32,
		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
		.offset = AVS_TKN_AFMT_NUM_CHANNELS_U32,
		.parse = parse_audio_format_bitfield,
	},
	{
		.token = AVS_TKN_AFMT_VALID_BIT_DEPTH_U32,
		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
		.offset = AVS_TKN_AFMT_VALID_BIT_DEPTH_U32,
		.parse = parse_audio_format_bitfield,
	},
	{
		.token = AVS_TKN_AFMT_SAMPLE_TYPE_U32,
		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
		.offset = AVS_TKN_AFMT_SAMPLE_TYPE_U32,
		.parse = parse_audio_format_bitfield,
	},
};

static int avs_tplg_parse_audio_formats(struct snd_soc_component *comp,
					struct snd_soc_tplg_vendor_array *tuples,
					u32 block_size)
{
	struct avs_soc_component *acomp = to_avs_soc_component(comp);
	struct avs_tplg *tplg = acomp->tplg;

	return parse_dictionary(comp, tuples, block_size, (void **)&tplg->fmts,
				&tplg->num_fmts, sizeof(*tplg->fmts),
				AVS_TKN_MANIFEST_NUM_AFMTS_U32,
				AVS_TKN_AFMT_ID_U32,
				audio_format_parsers, ARRAY_SIZE(audio_format_parsers));
}

static const struct avs_tplg_token_parser modcfg_base_parsers[] = {
	{
		.token = AVS_TKN_MODCFG_BASE_CPC_U32,
		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
		.offset = offsetof(struct avs_tplg_modcfg_base, cpc),
		.parse = avs_parse_word_token,
	},
	{
		.token = AVS_TKN_MODCFG_BASE_IBS_U32,
		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
		.offset = offsetof(struct avs_tplg_modcfg_base, ibs),
		.parse = avs_parse_word_token,
	},
	{
		.token = AVS_TKN_MODCFG_BASE_OBS_U32,
		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
		.offset = offsetof(struct avs_tplg_modcfg_base, obs),
		.parse = avs_parse_word_token,
	},
	{
		.token = AVS_TKN_MODCFG_BASE_PAGES_U32,
		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
		.offset = offsetof(struct avs_tplg_modcfg_base, is_pages),
		.parse = avs_parse_word_token,
	},
};

static int avs_tplg_parse_modcfgs_base(struct snd_soc_component *comp,
				       struct snd_soc_tplg_vendor_array *tuples,
				       u32 block_size)
{
	struct avs_soc_component *acomp = to_avs_soc_component(comp);
	struct avs_tplg *tplg = acomp->tplg;

	return parse_dictionary(comp, tuples, block_size, (void **)&tplg->modcfgs_base,
				&tplg->num_modcfgs_base, sizeof(*tplg->modcfgs_base),
				AVS_TKN_MANIFEST_NUM_MODCFGS_BASE_U32,
				AVS_TKN_MODCFG_BASE_ID_U32,
				modcfg_base_parsers, ARRAY_SIZE(modcfg_base_parsers));
}
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895

static const struct avs_tplg_token_parser modcfg_ext_parsers[] = {
	{
		.token = AVS_TKN_MODCFG_EXT_TYPE_UUID,
		.type = SND_SOC_TPLG_TUPLE_TYPE_UUID,
		.offset = offsetof(struct avs_tplg_modcfg_ext, type),
		.parse = avs_parse_uuid_token,
	},
	{
		.token = AVS_TKN_MODCFG_CPR_OUT_AFMT_ID_U32,
		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
		.offset = offsetof(struct avs_tplg_modcfg_ext, copier.out_fmt),
		.parse = avs_parse_audio_format_ptr,
	},
	{
		.token = AVS_TKN_MODCFG_CPR_FEATURE_MASK_U32,
		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
		.offset = offsetof(struct avs_tplg_modcfg_ext, copier.feature_mask),
		.parse = avs_parse_word_token,
	},
	{
		.token = AVS_TKN_MODCFG_CPR_VINDEX_U8,
		.type = SND_SOC_TPLG_TUPLE_TYPE_BYTE,
		.offset = offsetof(struct avs_tplg_modcfg_ext, copier.vindex),
		.parse = avs_parse_byte_token,
	},
	{
		.token = AVS_TKN_MODCFG_CPR_DMA_TYPE_U32,
		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
		.offset = offsetof(struct avs_tplg_modcfg_ext, copier.dma_type),
		.parse = avs_parse_word_token,
	},
	{
		.token = AVS_TKN_MODCFG_CPR_DMABUFF_SIZE_U32,
		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
		.offset = offsetof(struct avs_tplg_modcfg_ext, copier.dma_buffer_size),
		.parse = avs_parse_word_token,
	},
	{
		.token = AVS_TKN_MODCFG_CPR_BLOB_FMT_ID_U32,
		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
		.offset = offsetof(struct avs_tplg_modcfg_ext, copier.blob_fmt),
		.parse = avs_parse_audio_format_ptr,
	},
	{
		.token = AVS_TKN_MODCFG_MICSEL_OUT_AFMT_ID_U32,
		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
		.offset = offsetof(struct avs_tplg_modcfg_ext, micsel.out_fmt),
		.parse = avs_parse_audio_format_ptr,
	},
	{
		.token = AVS_TKN_MODCFG_INTELWOV_CPC_LP_MODE_U32,
		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
		.offset = offsetof(struct avs_tplg_modcfg_ext, wov.cpc_lp_mode),
		.parse = avs_parse_word_token,
	},
	{
		.token = AVS_TKN_MODCFG_SRC_OUT_FREQ_U32,
		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
		.offset = offsetof(struct avs_tplg_modcfg_ext, src.out_freq),
		.parse = avs_parse_word_token,
	},
	{
		.token = AVS_TKN_MODCFG_MUX_REF_AFMT_ID_U32,
		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
		.offset = offsetof(struct avs_tplg_modcfg_ext, mux.ref_fmt),
		.parse = avs_parse_audio_format_ptr,
	},
	{
		.token = AVS_TKN_MODCFG_MUX_OUT_AFMT_ID_U32,
		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
		.offset = offsetof(struct avs_tplg_modcfg_ext, mux.out_fmt),
		.parse = avs_parse_audio_format_ptr,
	},
	{
		.token = AVS_TKN_MODCFG_AEC_REF_AFMT_ID_U32,
		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
		.offset = offsetof(struct avs_tplg_modcfg_ext, aec.ref_fmt),
		.parse = avs_parse_audio_format_ptr,
	},
	{
		.token = AVS_TKN_MODCFG_AEC_OUT_AFMT_ID_U32,
		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
		.offset = offsetof(struct avs_tplg_modcfg_ext, aec.out_fmt),
		.parse = avs_parse_audio_format_ptr,
	},
	{
		.token = AVS_TKN_MODCFG_AEC_CPC_LP_MODE_U32,
		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
		.offset = offsetof(struct avs_tplg_modcfg_ext, aec.cpc_lp_mode),
		.parse = avs_parse_word_token,
	},
	{
		.token = AVS_TKN_MODCFG_ASRC_OUT_FREQ_U32,
		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
		.offset = offsetof(struct avs_tplg_modcfg_ext, asrc.out_freq),
		.parse = avs_parse_word_token,
	},
	{
		.token = AVS_TKN_MODCFG_ASRC_MODE_U8,
		.type = SND_SOC_TPLG_TUPLE_TYPE_BYTE,
		.offset = offsetof(struct avs_tplg_modcfg_ext, asrc.mode),
		.parse = avs_parse_byte_token,
	},
	{
		.token = AVS_TKN_MODCFG_ASRC_DISABLE_JITTER_U8,
		.type = SND_SOC_TPLG_TUPLE_TYPE_BYTE,
		.offset = offsetof(struct avs_tplg_modcfg_ext, asrc.disable_jitter_buffer),
		.parse = avs_parse_byte_token,
	},
	{
		.token = AVS_TKN_MODCFG_UPDOWN_MIX_OUT_CHAN_CFG_U32,
		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
		.offset = offsetof(struct avs_tplg_modcfg_ext, updown_mix.out_channel_config),
		.parse = avs_parse_word_token,
	},
	{
		.token = AVS_TKN_MODCFG_UPDOWN_MIX_COEFF_SELECT_U32,
		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
		.offset = offsetof(struct avs_tplg_modcfg_ext, updown_mix.coefficients_select),
		.parse = avs_parse_word_token,
	},
	{
		.token = AVS_TKN_MODCFG_UPDOWN_MIX_COEFF_0_S32,
		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
		.offset = offsetof(struct avs_tplg_modcfg_ext, updown_mix.coefficients[0]),
		.parse = avs_parse_word_token,
	},
	{
		.token = AVS_TKN_MODCFG_UPDOWN_MIX_COEFF_1_S32,
		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
		.offset = offsetof(struct avs_tplg_modcfg_ext, updown_mix.coefficients[1]),
		.parse = avs_parse_word_token,
	},
	{
		.token = AVS_TKN_MODCFG_UPDOWN_MIX_COEFF_2_S32,
		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
		.offset = offsetof(struct avs_tplg_modcfg_ext, updown_mix.coefficients[2]),
		.parse = avs_parse_word_token,
	},
	{
		.token = AVS_TKN_MODCFG_UPDOWN_MIX_COEFF_3_S32,
		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
		.offset = offsetof(struct avs_tplg_modcfg_ext, updown_mix.coefficients[3]),
		.parse = avs_parse_word_token,
	},
	{
		.token = AVS_TKN_MODCFG_UPDOWN_MIX_COEFF_4_S32,
		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
		.offset = offsetof(struct avs_tplg_modcfg_ext, updown_mix.coefficients[4]),
		.parse = avs_parse_word_token,
	},
	{
		.token = AVS_TKN_MODCFG_UPDOWN_MIX_COEFF_5_S32,
		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
		.offset = offsetof(struct avs_tplg_modcfg_ext, updown_mix.coefficients[5]),
		.parse = avs_parse_word_token,
	},
	{
		.token = AVS_TKN_MODCFG_UPDOWN_MIX_COEFF_6_S32,
		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
		.offset = offsetof(struct avs_tplg_modcfg_ext, updown_mix.coefficients[6]),
		.parse = avs_parse_word_token,
	},
	{
		.token = AVS_TKN_MODCFG_UPDOWN_MIX_COEFF_7_S32,
		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
		.offset = offsetof(struct avs_tplg_modcfg_ext, updown_mix.coefficients[7]),
		.parse = avs_parse_word_token,
	},
	{
		.token = AVS_TKN_MODCFG_UPDOWN_MIX_CHAN_MAP_U32,
		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
		.offset = offsetof(struct avs_tplg_modcfg_ext, updown_mix.channel_map),
		.parse = avs_parse_word_token,
	},
	{
		.token = AVS_TKN_MODCFG_EXT_NUM_INPUT_PINS_U16,
		.type = SND_SOC_TPLG_TUPLE_TYPE_SHORT,
		.offset = offsetof(struct avs_tplg_modcfg_ext, generic.num_input_pins),
		.parse = avs_parse_short_token,
	},
	{
		.token = AVS_TKN_MODCFG_EXT_NUM_OUTPUT_PINS_U16,
		.type = SND_SOC_TPLG_TUPLE_TYPE_SHORT,
		.offset = offsetof(struct avs_tplg_modcfg_ext, generic.num_output_pins),
		.parse = avs_parse_short_token,
	},
};

static const struct avs_tplg_token_parser pin_format_parsers[] = {
	{
		.token = AVS_TKN_PIN_FMT_INDEX_U32,
		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
		.offset = offsetof(struct avs_tplg_pin_format, pin_index),
		.parse = avs_parse_word_token,
	},
	{
		.token = AVS_TKN_PIN_FMT_IOBS_U32,
		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
		.offset = offsetof(struct avs_tplg_pin_format, iobs),
		.parse = avs_parse_word_token,
	},
	{
		.token = AVS_TKN_PIN_FMT_AFMT_ID_U32,
		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
		.offset = offsetof(struct avs_tplg_pin_format, fmt),
		.parse = avs_parse_audio_format_ptr,
	},
};

static int 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)
{
	u32 esize;
	int ret;

	/* See where pin block starts. */
	ret = avs_tplg_vendor_entry_size(tuples, block_size,
					 AVS_TKN_PIN_FMT_INDEX_U32, &esize);
	if (ret)
		return ret;

	ret = avs_parse_tokens(comp, cfg, modcfg_ext_parsers,
			       ARRAY_SIZE(modcfg_ext_parsers), tuples, esize);
	if (ret)
		return ret;

	block_size -= esize;
	/* Parse trailing in/out pin formats if any. */
	if (block_size) {
		struct avs_tplg_pin_format *pins;
		u32 num_pins;

		num_pins = cfg->generic.num_input_pins + cfg->generic.num_output_pins;
		if (!num_pins)
			return -EINVAL;

		pins = devm_kcalloc(comp->card->dev, num_pins, sizeof(*pins), GFP_KERNEL);
		if (!pins)
			return -ENOMEM;

		tuples = avs_tplg_vendor_array_at(tuples, esize);
		ret = parse_dictionary_entries(comp, tuples, block_size,
					       pins, num_pins, sizeof(*pins),
					       AVS_TKN_PIN_FMT_INDEX_U32,
					       pin_format_parsers,
					       ARRAY_SIZE(pin_format_parsers));
		if (ret)
			return ret;
		cfg->generic.pin_fmts = pins;
	}

	return 0;
}

static int avs_tplg_parse_modcfgs_ext(struct snd_soc_component *comp,
				      struct snd_soc_tplg_vendor_array *tuples,
				      u32 block_size)
{
	struct avs_soc_component *acomp = to_avs_soc_component(comp);
	struct avs_tplg *tplg = acomp->tplg;
	int ret, i;

	ret = parse_dictionary_header(comp, tuples, (void **)&tplg->modcfgs_ext,
				      &tplg->num_modcfgs_ext,
				      sizeof(*tplg->modcfgs_ext),
				      AVS_TKN_MANIFEST_NUM_MODCFGS_EXT_U32);
	if (ret)
		return ret;

	block_size -= le32_to_cpu(tuples->size);
	/* With header parsed, move on to parsing entries. */
	tuples = avs_tplg_vendor_array_next(tuples);

	for (i = 0; i < tplg->num_modcfgs_ext; i++) {
		struct avs_tplg_modcfg_ext *cfg = &tplg->modcfgs_ext[i];
		u32 esize;

		ret = avs_tplg_vendor_entry_size(tuples, block_size,
						 AVS_TKN_MODCFG_EXT_ID_U32, &esize);
		if (ret)
			return ret;

		ret = avs_tplg_parse_modcfg_ext(comp, cfg, tuples, esize);
		if (ret)
			return ret;

		block_size -= esize;
		tuples = avs_tplg_vendor_array_at(tuples, esize);
	}

	return 0;
}
896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948

static const struct avs_tplg_token_parser pplcfg_parsers[] = {
	{
		.token = AVS_TKN_PPLCFG_REQ_SIZE_U16,
		.type = SND_SOC_TPLG_TUPLE_TYPE_SHORT,
		.offset = offsetof(struct avs_tplg_pplcfg, req_size),
		.parse = avs_parse_short_token,
	},
	{
		.token = AVS_TKN_PPLCFG_PRIORITY_U8,
		.type = SND_SOC_TPLG_TUPLE_TYPE_BYTE,
		.offset = offsetof(struct avs_tplg_pplcfg, priority),
		.parse = avs_parse_byte_token,
	},
	{
		.token = AVS_TKN_PPLCFG_LOW_POWER_BOOL,
		.type = SND_SOC_TPLG_TUPLE_TYPE_BOOL,
		.offset = offsetof(struct avs_tplg_pplcfg, lp),
		.parse = avs_parse_bool_token,
	},
	{
		.token = AVS_TKN_PPLCFG_ATTRIBUTES_U16,
		.type = SND_SOC_TPLG_TUPLE_TYPE_SHORT,
		.offset = offsetof(struct avs_tplg_pplcfg, attributes),
		.parse = avs_parse_short_token,
	},
	{
		.token = AVS_TKN_PPLCFG_TRIGGER_U32,
		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
		.offset = offsetof(struct avs_tplg_pplcfg, trigger),
		.parse = avs_parse_word_token,
	},
};

static int avs_tplg_parse_pplcfgs(struct snd_soc_component *comp,
				  struct snd_soc_tplg_vendor_array *tuples,
				  u32 block_size)
{
	struct avs_soc_component *acomp = to_avs_soc_component(comp);
	struct avs_tplg *tplg = acomp->tplg;

	return parse_dictionary(comp, tuples, block_size, (void **)&tplg->pplcfgs,
				&tplg->num_pplcfgs, sizeof(*tplg->pplcfgs),
				AVS_TKN_MANIFEST_NUM_PPLCFGS_U32,
				AVS_TKN_PPLCFG_ID_U32,
				pplcfg_parsers, ARRAY_SIZE(pplcfg_parsers));
}

static const struct avs_tplg_token_parser binding_parsers[] = {
	{
		.token = AVS_TKN_BINDING_TARGET_TPLG_NAME_STRING,
		.type = SND_SOC_TPLG_TUPLE_TYPE_STRING,
		.offset = offsetof(struct avs_tplg_binding, target_tplg_name),
949
		.parse = parse_link_formatted_string,
950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
	},
	{
		.token = AVS_TKN_BINDING_TARGET_PATH_TMPL_ID_U32,
		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
		.offset = offsetof(struct avs_tplg_binding, target_path_tmpl_id),
		.parse = avs_parse_word_token,
	},
	{
		.token = AVS_TKN_BINDING_TARGET_PPL_ID_U32,
		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
		.offset = offsetof(struct avs_tplg_binding, target_ppl_id),
		.parse = avs_parse_word_token,
	},
	{
		.token = AVS_TKN_BINDING_TARGET_MOD_ID_U32,
		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
		.offset = offsetof(struct avs_tplg_binding, target_mod_id),
		.parse = avs_parse_word_token,
	},
	{
		.token = AVS_TKN_BINDING_TARGET_MOD_PIN_U8,
		.type = SND_SOC_TPLG_TUPLE_TYPE_BYTE,
		.offset = offsetof(struct avs_tplg_binding, target_mod_pin),
		.parse = avs_parse_byte_token,
	},
	{
		.token = AVS_TKN_BINDING_MOD_ID_U32,
		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
		.offset = offsetof(struct avs_tplg_binding, mod_id),
		.parse = avs_parse_word_token,
	},
	{
		.token = AVS_TKN_BINDING_MOD_PIN_U8,
		.type = SND_SOC_TPLG_TUPLE_TYPE_BYTE,
		.offset = offsetof(struct avs_tplg_binding, mod_pin),
		.parse = avs_parse_byte_token,
	},
	{
		.token = AVS_TKN_BINDING_IS_SINK_U8,
		.type = SND_SOC_TPLG_TUPLE_TYPE_BYTE,
		.offset = offsetof(struct avs_tplg_binding, is_sink),
		.parse = avs_parse_byte_token,
	},
};

static int avs_tplg_parse_bindings(struct snd_soc_component *comp,
				   struct snd_soc_tplg_vendor_array *tuples,
				   u32 block_size)
{
	struct avs_soc_component *acomp = to_avs_soc_component(comp);
	struct avs_tplg *tplg = acomp->tplg;

	return parse_dictionary(comp, tuples, block_size, (void **)&tplg->bindings,
				&tplg->num_bindings, sizeof(*tplg->bindings),
				AVS_TKN_MANIFEST_NUM_BINDINGS_U32,
				AVS_TKN_BINDING_ID_U32,
				binding_parsers, ARRAY_SIZE(binding_parsers));
}
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184

static const struct avs_tplg_token_parser module_parsers[] = {
	{
		.token = AVS_TKN_MOD_ID_U32,
		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
		.offset = offsetof(struct avs_tplg_module, id),
		.parse = avs_parse_word_token,
	},
	{
		.token = AVS_TKN_MOD_MODCFG_BASE_ID_U32,
		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
		.offset = offsetof(struct avs_tplg_module, cfg_base),
		.parse = avs_parse_modcfg_base_ptr,
	},
	{
		.token = AVS_TKN_MOD_IN_AFMT_ID_U32,
		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
		.offset = offsetof(struct avs_tplg_module, in_fmt),
		.parse = avs_parse_audio_format_ptr,
	},
	{
		.token = AVS_TKN_MOD_CORE_ID_U8,
		.type = SND_SOC_TPLG_TUPLE_TYPE_BYTE,
		.offset = offsetof(struct avs_tplg_module, core_id),
		.parse = avs_parse_byte_token,
	},
	{
		.token = AVS_TKN_MOD_PROC_DOMAIN_U8,
		.type = SND_SOC_TPLG_TUPLE_TYPE_BYTE,
		.offset = offsetof(struct avs_tplg_module, domain),
		.parse = avs_parse_byte_token,
	},
	{
		.token = AVS_TKN_MOD_MODCFG_EXT_ID_U32,
		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
		.offset = offsetof(struct avs_tplg_module, cfg_ext),
		.parse = avs_parse_modcfg_ext_ptr,
	},
};

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)
{
	struct avs_tplg_module *module;
	int ret;

	module = devm_kzalloc(comp->card->dev, sizeof(*module), GFP_KERNEL);
	if (!module)
		return ERR_PTR(-ENOMEM);

	ret = avs_parse_tokens(comp, module, module_parsers,
			       ARRAY_SIZE(module_parsers), tuples, block_size);
	if (ret < 0)
		return ERR_PTR(ret);

	module->owner = owner;
	INIT_LIST_HEAD(&module->node);

	return module;
}

static const struct avs_tplg_token_parser pipeline_parsers[] = {
	{
		.token = AVS_TKN_PPL_ID_U32,
		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
		.offset = offsetof(struct avs_tplg_pipeline, id),
		.parse = avs_parse_word_token,
	},
	{
		.token = AVS_TKN_PPL_PPLCFG_ID_U32,
		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
		.offset = offsetof(struct avs_tplg_pipeline, cfg),
		.parse = avs_parse_pplcfg_ptr,
	},
	{
		.token = AVS_TKN_PPL_NUM_BINDING_IDS_U32,
		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
		.offset = offsetof(struct avs_tplg_pipeline, num_bindings),
		.parse = avs_parse_word_token,
	},
};

static const struct avs_tplg_token_parser bindings_parsers[] = {
	{
		.token = AVS_TKN_PPL_BINDING_ID_U32,
		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
		.offset = 0, /* to treat pipeline->bindings as dictionary */
		.parse = avs_parse_binding_ptr,
	},
};

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)
{
	struct avs_tplg_pipeline *pipeline;
	u32 modblk_size, offset;
	int ret;

	pipeline = devm_kzalloc(comp->card->dev, sizeof(*pipeline), GFP_KERNEL);
	if (!pipeline)
		return ERR_PTR(-ENOMEM);

	pipeline->owner = owner;
	INIT_LIST_HEAD(&pipeline->mod_list);

	/* Pipeline header MUST be followed by at least one module. */
	ret = avs_tplg_vendor_array_lookup(tuples, block_size,
					   AVS_TKN_MOD_ID_U32, &offset);
	if (!ret && !offset)
		ret = -EINVAL;
	if (ret)
		return ERR_PTR(ret);

	/* Process header which precedes module sections. */
	ret = avs_parse_tokens(comp, pipeline, pipeline_parsers,
			       ARRAY_SIZE(pipeline_parsers), tuples, offset);
	if (ret < 0)
		return ERR_PTR(ret);

	block_size -= offset;
	tuples = avs_tplg_vendor_array_at(tuples, offset);

	/* Optionally, binding sections follow module ones. */
	ret = avs_tplg_vendor_array_lookup_next(tuples, block_size,
						AVS_TKN_PPL_BINDING_ID_U32, &offset);
	if (ret) {
		if (ret != -ENOENT)
			return ERR_PTR(ret);

		/* Does header information match actual block layout? */
		if (pipeline->num_bindings)
			return ERR_PTR(-EINVAL);

		modblk_size = block_size;
	} else {
		pipeline->bindings = devm_kcalloc(comp->card->dev, pipeline->num_bindings,
						  sizeof(*pipeline->bindings), GFP_KERNEL);
		if (!pipeline->bindings)
			return ERR_PTR(-ENOMEM);

		modblk_size = offset;
	}

	block_size -= modblk_size;
	do {
		struct avs_tplg_module *module;
		u32 esize;

		ret = avs_tplg_vendor_entry_size(tuples, modblk_size,
						 AVS_TKN_MOD_ID_U32, &esize);
		if (ret)
			return ERR_PTR(ret);

		module = avs_tplg_module_create(comp, pipeline, tuples, esize);
		if (IS_ERR(module)) {
			dev_err(comp->dev, "parse module failed: %ld\n",
				PTR_ERR(module));
			return ERR_CAST(module);
		}

		list_add_tail(&module->node, &pipeline->mod_list);
		modblk_size -= esize;
		tuples = avs_tplg_vendor_array_at(tuples, esize);
	} while (modblk_size > 0);

	/* What's left is optional range of bindings. */
	ret = parse_dictionary_entries(comp, tuples, block_size, pipeline->bindings,
				       pipeline->num_bindings, sizeof(*pipeline->bindings),
				       AVS_TKN_PPL_BINDING_ID_U32,
				       bindings_parsers, ARRAY_SIZE(bindings_parsers));
	if (ret)
		return ERR_PTR(ret);

	return pipeline;
}
1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342

static const struct avs_tplg_token_parser path_parsers[] = {
	{
		.token = AVS_TKN_PATH_ID_U32,
		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
		.offset = offsetof(struct avs_tplg_path, id),
		.parse = avs_parse_word_token,
	},
	{
		.token = AVS_TKN_PATH_FE_FMT_ID_U32,
		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
		.offset = offsetof(struct avs_tplg_path, fe_fmt),
		.parse = avs_parse_audio_format_ptr,
	},
	{
		.token = AVS_TKN_PATH_BE_FMT_ID_U32,
		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
		.offset = offsetof(struct avs_tplg_path, be_fmt),
		.parse = avs_parse_audio_format_ptr,
	},
};

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)
{
	struct avs_tplg_pipeline *pipeline;
	struct avs_tplg_path *path;
	u32 offset;
	int ret;

	path = devm_kzalloc(comp->card->dev, sizeof(*path), GFP_KERNEL);
	if (!path)
		return ERR_PTR(-ENOMEM);

	path->owner = owner;
	INIT_LIST_HEAD(&path->ppl_list);
	INIT_LIST_HEAD(&path->node);

	/* Path header MAY be followed by one or more pipelines. */
	ret = avs_tplg_vendor_array_lookup(tuples, block_size,
					   AVS_TKN_PPL_ID_U32, &offset);
	if (ret == -ENOENT)
		offset = block_size;
	else if (ret)
		return ERR_PTR(ret);
	else if (!offset)
		return ERR_PTR(-EINVAL);

	/* Process header which precedes pipeline sections. */
	ret = avs_parse_tokens(comp, path, parsers, num_parsers, tuples, offset);
	if (ret < 0)
		return ERR_PTR(ret);

	block_size -= offset;
	tuples = avs_tplg_vendor_array_at(tuples, offset);
	while (block_size > 0) {
		u32 esize;

		ret = avs_tplg_vendor_entry_size(tuples, block_size,
						 AVS_TKN_PPL_ID_U32, &esize);
		if (ret)
			return ERR_PTR(ret);

		pipeline = avs_tplg_pipeline_create(comp, path, tuples, esize);
		if (IS_ERR(pipeline)) {
			dev_err(comp->dev, "parse pipeline failed: %ld\n",
				PTR_ERR(pipeline));
			return ERR_CAST(pipeline);
		}

		list_add_tail(&pipeline->node, &path->ppl_list);
		block_size -= esize;
		tuples = avs_tplg_vendor_array_at(tuples, esize);
	}

	return path;
}

static const struct avs_tplg_token_parser path_tmpl_parsers[] = {
	{
		.token = AVS_TKN_PATH_TMPL_ID_U32,
		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
		.offset = offsetof(struct avs_tplg_path_template, id),
		.parse = avs_parse_word_token,
	},
};

static int 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)
{
	struct avs_tplg_path *path;
	u32 offset;
	int ret;

	/* Path template header MUST be followed by at least one path variant. */
	ret = avs_tplg_vendor_array_lookup(tuples, block_size,
					   AVS_TKN_PATH_ID_U32, &offset);
	if (ret)
		return ret;

	/* Process header which precedes path variants sections. */
	ret = avs_parse_tokens(comp, template, tmpl_tokens, num_tmpl_tokens, tuples, offset);
	if (ret < 0)
		return ret;

	block_size -= offset;
	tuples = avs_tplg_vendor_array_at(tuples, offset);
	do {
		u32 esize;

		ret = avs_tplg_vendor_entry_size(tuples, block_size,
						 AVS_TKN_PATH_ID_U32, &esize);
		if (ret)
			return ret;

		path = avs_tplg_path_create(comp, template, tuples, esize, path_tokens,
					    num_path_tokens);
		if (IS_ERR(path)) {
			dev_err(comp->dev, "parse path failed: %ld\n", PTR_ERR(path));
			return PTR_ERR(path);
		}

		list_add_tail(&path->node, &template->path_list);
		block_size -= esize;
		tuples = avs_tplg_vendor_array_at(tuples, esize);
	} while (block_size > 0);

	return 0;
}

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)
{
	struct avs_tplg_path_template *template;
	int ret;

	template = devm_kzalloc(comp->card->dev, sizeof(*template), GFP_KERNEL);
	if (!template)
		return ERR_PTR(-ENOMEM);

	template->owner = owner; /* Used to access component tplg is assigned to. */
	INIT_LIST_HEAD(&template->path_list);
	INIT_LIST_HEAD(&template->node);

	ret = parse_path_template(comp, tuples, block_size, template, path_tmpl_parsers,
				  ARRAY_SIZE(path_tmpl_parsers), path_parsers,
				  ARRAY_SIZE(path_parsers));
	if (ret)
		return ERR_PTR(ret);

	return template;
}
1343

1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368
static int avs_route_load(struct snd_soc_component *comp, int index,
			  struct snd_soc_dapm_route *route)
{
	struct snd_soc_acpi_mach *mach = dev_get_platdata(comp->card->dev);
	size_t len = SNDRV_CTL_ELEM_ID_NAME_MAXLEN;
	char buf[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
	u32 port;

	/* See parse_link_formatted_string() for dynamic naming when(s). */
	if (hweight_long(mach->link_mask) == 1) {
		port = __ffs(mach->link_mask);

		snprintf(buf, len, route->source, port);
		strncpy((char *)route->source, buf, len);
		snprintf(buf, len, route->sink, port);
		strncpy((char *)route->sink, buf, len);
		if (route->control) {
			snprintf(buf, len, route->control, port);
			strncpy((char *)route->control, buf, len);
		}
	}

	return 0;
}

1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383
static int avs_widget_load(struct snd_soc_component *comp, int index,
			   struct snd_soc_dapm_widget *w,
			   struct snd_soc_tplg_dapm_widget *dw)
{
	struct snd_soc_acpi_mach *mach;
	struct avs_tplg_path_template *template;
	struct avs_soc_component *acomp = to_avs_soc_component(comp);
	struct avs_tplg *tplg;

	if (!le32_to_cpu(dw->priv.size))
		return 0;

	tplg = acomp->tplg;
	mach = dev_get_platdata(comp->card->dev);

1384 1385 1386 1387 1388 1389 1390 1391 1392
	/* See parse_link_formatted_string() for dynamic naming when(s). */
	if (hweight_long(mach->link_mask) == 1) {
		kfree(w->name);
		/* w->name is freed later by soc_tplg_dapm_widget_create() */
		w->name = kasprintf(GFP_KERNEL, dw->name, __ffs(mach->link_mask));
		if (!w->name)
			return -ENOMEM;
	}

1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434
	template = avs_tplg_path_template_create(comp, tplg, dw->priv.array,
						 le32_to_cpu(dw->priv.size));
	if (IS_ERR(template)) {
		dev_err(comp->dev, "widget %s load failed: %ld\n", dw->name,
			PTR_ERR(template));
		return PTR_ERR(template);
	}

	w->priv = template; /* link path information to widget */
	list_add_tail(&template->node, &tplg->path_tmpl_list);
	return 0;
}

static int 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)
{
	if (pcm)
		dai_drv->ops = &avs_dai_fe_ops;
	return 0;
}

static int avs_link_load(struct snd_soc_component *comp, int index, struct snd_soc_dai_link *link,
			 struct snd_soc_tplg_link_config *cfg)
{
	if (!link->no_pcm) {
		/* Stream control handled by IPCs. */
		link->nonatomic = true;

		/* Open LINK (BE) pipes last and close them first to prevent xruns. */
		link->trigger[0] = SND_SOC_DPCM_TRIGGER_PRE;
		link->trigger[1] = SND_SOC_DPCM_TRIGGER_PRE;
	}

	return 0;
}

static const struct avs_tplg_token_parser manifest_parsers[] = {
	{
		.token = AVS_TKN_MANIFEST_NAME_STRING,
		.type = SND_SOC_TPLG_TUPLE_TYPE_STRING,
		.offset = offsetof(struct avs_tplg, name),
1435
		.parse = parse_link_formatted_string,
1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552
	},
	{
		.token = AVS_TKN_MANIFEST_VERSION_U32,
		.type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
		.offset = offsetof(struct avs_tplg, version),
		.parse = avs_parse_word_token,
	},
};

static int avs_manifest(struct snd_soc_component *comp, int index,
			struct snd_soc_tplg_manifest *manifest)
{
	struct snd_soc_tplg_vendor_array *tuples = manifest->priv.array;
	struct avs_soc_component *acomp = to_avs_soc_component(comp);
	size_t remaining = le32_to_cpu(manifest->priv.size);
	u32 offset;
	int ret;

	ret = avs_tplg_vendor_array_lookup(tuples, remaining,
					   AVS_TKN_MANIFEST_NUM_LIBRARIES_U32, &offset);
	/* Manifest MUST begin with a header. */
	if (!ret && !offset)
		ret = -EINVAL;
	if (ret) {
		dev_err(comp->dev, "incorrect manifest format: %d\n", ret);
		return ret;
	}

	/* Process header which precedes any of the dictionaries. */
	ret = avs_parse_tokens(comp, acomp->tplg, manifest_parsers,
			       ARRAY_SIZE(manifest_parsers), tuples, offset);
	if (ret < 0)
		return ret;

	remaining -= offset;
	tuples = avs_tplg_vendor_array_at(tuples, offset);

	ret = avs_tplg_vendor_array_lookup(tuples, remaining,
					   AVS_TKN_MANIFEST_NUM_AFMTS_U32, &offset);
	if (ret) {
		dev_err(comp->dev, "audio formats lookup failed: %d\n", ret);
		return ret;
	}

	/* Libraries dictionary. */
	ret = avs_tplg_parse_libraries(comp, tuples, offset);
	if (ret < 0)
		return ret;

	remaining -= offset;
	tuples = avs_tplg_vendor_array_at(tuples, offset);

	ret = avs_tplg_vendor_array_lookup(tuples, remaining,
					   AVS_TKN_MANIFEST_NUM_MODCFGS_BASE_U32, &offset);
	if (ret) {
		dev_err(comp->dev, "modcfgs_base lookup failed: %d\n", ret);
		return ret;
	}

	/* Audio formats dictionary. */
	ret = avs_tplg_parse_audio_formats(comp, tuples, offset);
	if (ret < 0)
		return ret;

	remaining -= offset;
	tuples = avs_tplg_vendor_array_at(tuples, offset);

	ret = avs_tplg_vendor_array_lookup(tuples, remaining,
					   AVS_TKN_MANIFEST_NUM_MODCFGS_EXT_U32, &offset);
	if (ret) {
		dev_err(comp->dev, "modcfgs_ext lookup failed: %d\n", ret);
		return ret;
	}

	/* Module configs-base dictionary. */
	ret = avs_tplg_parse_modcfgs_base(comp, tuples, offset);
	if (ret < 0)
		return ret;

	remaining -= offset;
	tuples = avs_tplg_vendor_array_at(tuples, offset);

	ret = avs_tplg_vendor_array_lookup(tuples, remaining,
					   AVS_TKN_MANIFEST_NUM_PPLCFGS_U32, &offset);
	if (ret) {
		dev_err(comp->dev, "pplcfgs lookup failed: %d\n", ret);
		return ret;
	}

	/* Module configs-ext dictionary. */
	ret = avs_tplg_parse_modcfgs_ext(comp, tuples, offset);
	if (ret < 0)
		return ret;

	remaining -= offset;
	tuples = avs_tplg_vendor_array_at(tuples, offset);

	ret = avs_tplg_vendor_array_lookup(tuples, remaining,
					   AVS_TKN_MANIFEST_NUM_BINDINGS_U32, &offset);
	if (ret) {
		dev_err(comp->dev, "bindings lookup failed: %d\n", ret);
		return ret;
	}

	/* Pipeline configs dictionary. */
	ret = avs_tplg_parse_pplcfgs(comp, tuples, offset);
	if (ret < 0)
		return ret;

	remaining -= offset;
	tuples = avs_tplg_vendor_array_at(tuples, offset);

	/* Bindings dictionary. */
	return avs_tplg_parse_bindings(comp, tuples, remaining);
}

static struct snd_soc_tplg_ops avs_tplg_ops = {
1553
	.dapm_route_load	= avs_route_load,
1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598
	.widget_load		= avs_widget_load,
	.dai_load		= avs_dai_load,
	.link_load		= avs_link_load,
	.manifest		= avs_manifest,
};

struct avs_tplg *avs_tplg_new(struct snd_soc_component *comp)
{
	struct avs_tplg *tplg;

	tplg = devm_kzalloc(comp->card->dev, sizeof(*tplg), GFP_KERNEL);
	if (!tplg)
		return NULL;

	tplg->comp = comp;
	INIT_LIST_HEAD(&tplg->path_tmpl_list);

	return tplg;
}

int avs_load_topology(struct snd_soc_component *comp, const char *filename)
{
	const struct firmware *fw;
	int ret;

	ret = request_firmware(&fw, filename, comp->dev);
	if (ret < 0) {
		dev_err(comp->dev, "request topology \"%s\" failed: %d\n", filename, ret);
		return ret;
	}

	ret = snd_soc_tplg_component_load(comp, &avs_tplg_ops, fw);
	if (ret < 0)
		dev_err(comp->dev, "load topology \"%s\" failed: %d\n", filename, ret);

	release_firmware(fw);
	return ret;
}

int avs_remove_topology(struct snd_soc_component *comp)
{
	snd_soc_tplg_component_remove(comp);

	return 0;
}