xref: /linux/sound/soc/qcom/qdsp6/topology.c (revision 562b2cc2aa88e039790248b5f1014c4850a7e528)
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2020, Linaro Limited
3 
4 #include <linux/cleanup.h>
5 #include <sound/soc.h>
6 #include <sound/soc-dapm.h>
7 #include <sound/pcm.h>
8 #include <sound/control.h>
9 #include <sound/asound.h>
10 #include <linux/firmware.h>
11 #include <sound/soc-topology.h>
12 #include <sound/soc-dpcm.h>
13 #include <uapi/sound/snd_ar_tokens.h>
14 #include <linux/kernel.h>
15 #include <linux/wait.h>
16 #include "q6apm.h"
17 #include "audioreach.h"
18 
19 struct snd_ar_control {
20 	u32 graph_id; /* Graph ID */
21 	u32 sgid; /* Sub Graph ID */
22 	u32 module_instance_id; /* Connected Module Instance ID */
23 	struct snd_soc_dapm_widget *w;
24 	struct list_head node;
25 	struct snd_soc_component *scomp;
26 };
27 
28 static struct audioreach_graph_info *audioreach_tplg_alloc_graph_info(struct q6apm *apm,
29 								      uint32_t graph_id,
30 								      bool *found)
31 {
32 	struct audioreach_graph_info *info;
33 	int ret;
34 
35 	mutex_lock(&apm->lock);
36 	info = idr_find(&apm->graph_info_idr, graph_id);
37 	mutex_unlock(&apm->lock);
38 
39 	if (info) {
40 		*found = true;
41 		return info;
42 	}
43 
44 	*found = false;
45 	info = kzalloc_obj(*info);
46 	if (!info)
47 		return ERR_PTR(-ENOMEM);
48 
49 	INIT_LIST_HEAD(&info->sg_list);
50 
51 	mutex_lock(&apm->lock);
52 	ret = idr_alloc_u32(&apm->graph_info_idr, info, &graph_id, graph_id, GFP_KERNEL);
53 	mutex_unlock(&apm->lock);
54 
55 	if (ret < 0) {
56 		dev_err(apm->dev, "Failed to allocate Graph ID (%x)\n", graph_id);
57 		kfree(info);
58 		return ERR_PTR(ret);
59 	}
60 
61 	info->id = graph_id;
62 
63 	return info;
64 }
65 
66 static void audioreach_tplg_add_sub_graph(struct audioreach_sub_graph *sg,
67 					  struct audioreach_graph_info *info)
68 {
69 	list_add_tail(&sg->node, &info->sg_list);
70 	sg->info = info;
71 	info->num_sub_graphs++;
72 }
73 
74 static struct audioreach_sub_graph *audioreach_tplg_alloc_sub_graph(struct q6apm *apm,
75 								    uint32_t sub_graph_id,
76 								    bool *found)
77 {
78 	struct audioreach_sub_graph *sg;
79 	int ret;
80 
81 	if (!sub_graph_id)
82 		return ERR_PTR(-EINVAL);
83 
84 	/* Find if there is already a matching sub-graph */
85 	mutex_lock(&apm->lock);
86 	sg = idr_find(&apm->sub_graphs_idr, sub_graph_id);
87 	mutex_unlock(&apm->lock);
88 
89 	if (sg) {
90 		*found = true;
91 		return sg;
92 	}
93 
94 	*found = false;
95 	sg = kzalloc_obj(*sg);
96 	if (!sg)
97 		return ERR_PTR(-ENOMEM);
98 
99 	INIT_LIST_HEAD(&sg->container_list);
100 
101 	mutex_lock(&apm->lock);
102 	ret = idr_alloc_u32(&apm->sub_graphs_idr, sg, &sub_graph_id, sub_graph_id, GFP_KERNEL);
103 	mutex_unlock(&apm->lock);
104 
105 	if (ret < 0) {
106 		dev_err(apm->dev, "Failed to allocate Sub-Graph Instance ID (%x)\n", sub_graph_id);
107 		kfree(sg);
108 		return ERR_PTR(ret);
109 	}
110 
111 	sg->sub_graph_id = sub_graph_id;
112 
113 	return sg;
114 }
115 
116 static struct audioreach_container *audioreach_tplg_alloc_container(struct q6apm *apm,
117 							    struct audioreach_sub_graph *sg,
118 							    uint32_t container_id,
119 							    bool *found)
120 {
121 	struct audioreach_container *cont;
122 	int ret;
123 
124 	if (!container_id)
125 		return ERR_PTR(-EINVAL);
126 
127 	mutex_lock(&apm->lock);
128 	cont = idr_find(&apm->containers_idr, container_id);
129 	mutex_unlock(&apm->lock);
130 
131 	if (cont) {
132 		*found = true;
133 		return cont;
134 	}
135 	*found = false;
136 
137 	cont = kzalloc_obj(*cont);
138 	if (!cont)
139 		return ERR_PTR(-ENOMEM);
140 
141 	INIT_LIST_HEAD(&cont->modules_list);
142 
143 	mutex_lock(&apm->lock);
144 	ret = idr_alloc_u32(&apm->containers_idr, cont, &container_id, container_id, GFP_KERNEL);
145 	mutex_unlock(&apm->lock);
146 
147 	if (ret < 0) {
148 		dev_err(apm->dev, "Failed to allocate Container Instance ID (%x)\n", container_id);
149 		kfree(cont);
150 		return ERR_PTR(ret);
151 	}
152 
153 	cont->container_id = container_id;
154 	cont->sub_graph = sg;
155 	/* add to container list */
156 	list_add_tail(&cont->node, &sg->container_list);
157 	sg->num_containers++;
158 
159 	return cont;
160 }
161 
162 static struct audioreach_module *audioreach_tplg_alloc_module(struct q6apm *apm,
163 							      struct audioreach_container *cont,
164 							      struct snd_soc_dapm_widget *w,
165 							      uint32_t module_id, bool *found)
166 {
167 	struct audioreach_module *mod;
168 	int ret;
169 
170 	mutex_lock(&apm->lock);
171 	mod = idr_find(&apm->modules_idr, module_id);
172 	mutex_unlock(&apm->lock);
173 
174 	if (mod) {
175 		*found = true;
176 		return mod;
177 	}
178 	*found = false;
179 	mod = kzalloc_obj(*mod);
180 	if (!mod)
181 		return ERR_PTR(-ENOMEM);
182 
183 	mutex_lock(&apm->lock);
184 	if (!module_id) { /* alloc module id dynamically */
185 		ret = idr_alloc_cyclic(&apm->modules_idr, mod,
186 				       AR_MODULE_DYNAMIC_INSTANCE_ID_START,
187 				       AR_MODULE_DYNAMIC_INSTANCE_ID_END, GFP_KERNEL);
188 	} else {
189 		ret = idr_alloc_u32(&apm->modules_idr, mod, &module_id, module_id, GFP_KERNEL);
190 	}
191 	mutex_unlock(&apm->lock);
192 
193 	if (ret < 0) {
194 		dev_err(apm->dev, "Failed to allocate Module Instance ID (%x)\n", module_id);
195 		kfree(mod);
196 		return ERR_PTR(ret);
197 	}
198 
199 	mod->instance_id = module_id;
200 	/* add to module list */
201 	list_add_tail(&mod->node, &cont->modules_list);
202 	mod->container = cont;
203 	mod->widget = w;
204 	cont->num_modules++;
205 
206 	return mod;
207 }
208 
209 static const struct snd_soc_tplg_vendor_array *
210 audioreach_get_sg_array(const struct snd_soc_tplg_private *private)
211 {
212 	const struct snd_soc_tplg_vendor_array *sg_array = NULL;
213 	bool found = false;
214 	int sz;
215 
216 	for (sz = 0; !found && (sz < le32_to_cpu(private->size)); ) {
217 		const struct snd_soc_tplg_vendor_value_elem *sg_elem;
218 		int tkn_count = 0;
219 
220 		sg_array = (struct snd_soc_tplg_vendor_array *)((u8 *)private->array + sz);
221 		sg_elem = sg_array->value;
222 		sz = sz + le32_to_cpu(sg_array->size);
223 		while (!found && tkn_count <= (le32_to_cpu(sg_array->num_elems) - 1)) {
224 			switch (le32_to_cpu(sg_elem->token)) {
225 			case AR_TKN_U32_SUB_GRAPH_INSTANCE_ID:
226 				found = true;
227 				break;
228 			default:
229 				break;
230 			}
231 			tkn_count++;
232 			sg_elem++;
233 		}
234 	}
235 
236 	if (found)
237 		return sg_array;
238 
239 	return NULL;
240 }
241 
242 static const struct snd_soc_tplg_vendor_array *
243 audioreach_get_cont_array(const struct snd_soc_tplg_private *private)
244 {
245 	const struct snd_soc_tplg_vendor_array *cont_array = NULL;
246 	bool found = false;
247 	int sz;
248 
249 	for (sz = 0; !found && (sz < le32_to_cpu(private->size)); ) {
250 		const struct snd_soc_tplg_vendor_value_elem *cont_elem;
251 		int tkn_count = 0;
252 
253 		cont_array = (struct snd_soc_tplg_vendor_array *)((u8 *)private->array + sz);
254 		cont_elem = cont_array->value;
255 		sz = sz + le32_to_cpu(cont_array->size);
256 		while (!found && tkn_count <= (le32_to_cpu(cont_array->num_elems) - 1)) {
257 			switch (le32_to_cpu(cont_elem->token)) {
258 			case AR_TKN_U32_CONTAINER_INSTANCE_ID:
259 				found = true;
260 				break;
261 			default:
262 				break;
263 			}
264 			tkn_count++;
265 			cont_elem++;
266 		}
267 	}
268 
269 	if (found)
270 		return cont_array;
271 
272 	return NULL;
273 }
274 
275 static const struct snd_soc_tplg_vendor_array *
276 audioreach_get_module_array(const struct snd_soc_tplg_private *private)
277 {
278 	const struct snd_soc_tplg_vendor_array *mod_array = NULL;
279 	bool found = false;
280 	int sz = 0;
281 
282 	for (sz = 0; !found && (sz < le32_to_cpu(private->size)); ) {
283 		const struct snd_soc_tplg_vendor_value_elem *mod_elem;
284 		int tkn_count = 0;
285 
286 		mod_array = (struct snd_soc_tplg_vendor_array *)((u8 *)private->array + sz);
287 		mod_elem = mod_array->value;
288 		sz = sz + le32_to_cpu(mod_array->size);
289 		while (!found && tkn_count <= (le32_to_cpu(mod_array->num_elems) - 1)) {
290 			switch (le32_to_cpu(mod_elem->token)) {
291 			case AR_TKN_U32_MODULE_INSTANCE_ID:
292 				found = true;
293 				break;
294 			default:
295 				break;
296 			}
297 			tkn_count++;
298 			mod_elem++;
299 		}
300 	}
301 
302 	if (found)
303 		return mod_array;
304 
305 	return NULL;
306 }
307 
308 static struct audioreach_module_priv_data *
309 audioreach_get_module_priv_data(const struct snd_soc_tplg_private *private)
310 {
311 	int sz;
312 
313 	for (sz = 0; sz < le32_to_cpu(private->size); ) {
314 		const struct snd_soc_tplg_vendor_array *mod_array;
315 		u32 array_size;
316 
317 		mod_array = (struct snd_soc_tplg_vendor_array *)((u8 *)private->array + sz);
318 		array_size = le32_to_cpu(mod_array->size);
319 
320 		if (le32_to_cpu(mod_array->type) == SND_SOC_AR_TPLG_MODULE_CFG_TYPE) {
321 			struct audioreach_module_priv_data *pdata;
322 			u32 nwords;
323 
324 			/* size is the byte count of the __le32 data[] */
325 			nwords = array_size / sizeof(__le32);
326 
327 			/* the blob must lie within the private data */
328 			if (sizeof(*pdata) + (u64)array_size >
329 			    le32_to_cpu(private->size) - sz)
330 				return ERR_PTR(-EINVAL);
331 
332 			pdata = kzalloc_flex(*pdata, data, nwords);
333 			if (!pdata)
334 				return ERR_PTR(-ENOMEM);
335 
336 			memcpy(pdata, ((u8 *)private->data + sz),
337 			       struct_size(pdata, data, nwords));
338 			return pdata;
339 		}
340 
341 		/* a zero size would never advance the walk */
342 		if (!array_size)
343 			return ERR_PTR(-EINVAL);
344 
345 		sz = sz + array_size;
346 	}
347 
348 	return NULL;
349 }
350 
351 static struct audioreach_sub_graph *audioreach_parse_sg_tokens(struct q6apm *apm,
352 							       const struct snd_soc_tplg_private *private)
353 {
354 	const struct snd_soc_tplg_vendor_value_elem *sg_elem;
355 	const struct snd_soc_tplg_vendor_array *sg_array;
356 	struct audioreach_graph_info *info = NULL;
357 	int graph_id, sub_graph_id, tkn_count = 0;
358 	struct audioreach_sub_graph *sg;
359 	bool found;
360 
361 	sg_array = audioreach_get_sg_array(private);
362 	sg_elem = sg_array->value;
363 
364 	while (tkn_count <= (le32_to_cpu(sg_array->num_elems) - 1)) {
365 		switch (le32_to_cpu(sg_elem->token)) {
366 		case AR_TKN_U32_SUB_GRAPH_INSTANCE_ID:
367 			sub_graph_id = le32_to_cpu(sg_elem->value);
368 			sg = audioreach_tplg_alloc_sub_graph(apm, sub_graph_id, &found);
369 			if (IS_ERR(sg)) {
370 				return sg;
371 			} else if (found) {
372 				/* Already parsed data for this sub-graph */
373 				return sg;
374 			}
375 			break;
376 		case AR_TKN_DAI_INDEX:
377 			/* Sub graph is associated with predefined graph */
378 			graph_id = le32_to_cpu(sg_elem->value);
379 			info = audioreach_tplg_alloc_graph_info(apm, graph_id, &found);
380 			if (IS_ERR(info))
381 				return ERR_CAST(info);
382 			break;
383 		case AR_TKN_U32_SUB_GRAPH_PERF_MODE:
384 			sg->perf_mode = le32_to_cpu(sg_elem->value);
385 			break;
386 		case AR_TKN_U32_SUB_GRAPH_DIRECTION:
387 			sg->direction = le32_to_cpu(sg_elem->value);
388 			break;
389 		case AR_TKN_U32_SUB_GRAPH_SCENARIO_ID:
390 			sg->scenario_id = le32_to_cpu(sg_elem->value);
391 			break;
392 		default:
393 			dev_err(apm->dev, "Not a valid token %d for graph\n", sg_elem->token);
394 			break;
395 
396 		}
397 		tkn_count++;
398 		sg_elem++;
399 	}
400 
401 	/* Sub graph is associated with predefined graph */
402 	if (info)
403 		audioreach_tplg_add_sub_graph(sg, info);
404 
405 	return sg;
406 }
407 
408 static struct audioreach_container *audioreach_parse_cont_tokens(struct q6apm *apm,
409 							 struct audioreach_sub_graph *sg,
410 							 const struct snd_soc_tplg_private *private)
411 {
412 	const struct snd_soc_tplg_vendor_value_elem *cont_elem;
413 	const struct snd_soc_tplg_vendor_array *cont_array;
414 	struct audioreach_container *cont;
415 	int container_id, tkn_count = 0;
416 	bool found = false;
417 
418 	cont_array = audioreach_get_cont_array(private);
419 	cont_elem = cont_array->value;
420 
421 	while (tkn_count <= (le32_to_cpu(cont_array->num_elems) - 1)) {
422 		switch (le32_to_cpu(cont_elem->token)) {
423 		case AR_TKN_U32_CONTAINER_INSTANCE_ID:
424 			container_id = le32_to_cpu(cont_elem->value);
425 			cont = audioreach_tplg_alloc_container(apm, sg, container_id, &found);
426 			if (IS_ERR(cont) || found)/* Error or Already parsed container data */
427 				return cont;
428 			break;
429 		case AR_TKN_U32_CONTAINER_CAPABILITY_ID:
430 			cont->capability_id = le32_to_cpu(cont_elem->value);
431 			break;
432 		case AR_TKN_U32_CONTAINER_STACK_SIZE:
433 			cont->stack_size = le32_to_cpu(cont_elem->value);
434 			break;
435 		case AR_TKN_U32_CONTAINER_GRAPH_POS:
436 			cont->graph_pos = le32_to_cpu(cont_elem->value);
437 			break;
438 		case AR_TKN_U32_CONTAINER_PROC_DOMAIN:
439 			cont->proc_domain = le32_to_cpu(cont_elem->value);
440 			break;
441 		default:
442 			dev_err(apm->dev, "Not a valid token %d for graph\n", cont_elem->token);
443 			break;
444 
445 		}
446 		tkn_count++;
447 		cont_elem++;
448 	}
449 
450 	return cont;
451 }
452 
453 static struct audioreach_module *audioreach_parse_common_tokens(struct q6apm *apm,
454 							struct audioreach_container *cont,
455 							const struct snd_soc_tplg_private *private,
456 							struct snd_soc_dapm_widget *w)
457 {
458 	uint32_t max_ip_port = 0, max_op_port = 0;
459 	uint32_t src_mod_op_port_id[AR_MAX_MOD_LINKS] = { 0, };
460 	uint32_t dst_mod_inst_id[AR_MAX_MOD_LINKS] = { 0, };
461 	uint32_t dst_mod_ip_port_id[AR_MAX_MOD_LINKS] = { 0, };
462 	uint32_t src_mod_inst_id = 0;
463 
464 	int module_id = 0, instance_id = 0, tkn_count = 0;
465 	const struct snd_soc_tplg_vendor_value_elem *mod_elem;
466 	const struct snd_soc_tplg_vendor_array *mod_array;
467 	struct audioreach_module *mod = NULL;
468 	uint32_t token;
469 	bool found;
470 	int max_tokens;
471 
472 	mod_array = audioreach_get_module_array(private);
473 	mod_elem = mod_array->value;
474 	max_tokens = le32_to_cpu(mod_array->num_elems);
475 	while (tkn_count <= (max_tokens - 1)) {
476 		token = le32_to_cpu(mod_elem->token);
477 		switch (token) {
478 		/* common module info */
479 		case AR_TKN_U32_MODULE_ID:
480 			module_id = le32_to_cpu(mod_elem->value);
481 			break;
482 		case AR_TKN_U32_MODULE_INSTANCE_ID:
483 			instance_id = le32_to_cpu(mod_elem->value);
484 			mod = audioreach_tplg_alloc_module(apm, cont, w,
485 							   instance_id, &found);
486 			if (IS_ERR(mod)) {
487 				return mod;
488 			} else if (found) {
489 				dev_err(apm->dev, "Duplicate Module Instance ID 0x%08x found\n",
490 					instance_id);
491 				return ERR_PTR(-EINVAL);
492 			}
493 
494 			break;
495 		case AR_TKN_U32_MODULE_MAX_IP_PORTS:
496 			max_ip_port = le32_to_cpu(mod_elem->value);
497 			break;
498 		case AR_TKN_U32_MODULE_MAX_OP_PORTS:
499 			max_op_port = le32_to_cpu(mod_elem->value);
500 			break;
501 		case AR_TKN_U32_MODULE_SRC_INSTANCE_ID:
502 			src_mod_inst_id = le32_to_cpu(mod_elem->value);
503 			break;
504 		case AR_TKN_U32_MODULE_SRC_OP_PORT_ID:
505 			src_mod_op_port_id[0] = le32_to_cpu(mod_elem->value);
506 			break;
507 		case AR_TKN_U32_MODULE_SRC_OP_PORT_ID1:
508 			src_mod_op_port_id[1] = le32_to_cpu(mod_elem->value);
509 			break;
510 		case AR_TKN_U32_MODULE_SRC_OP_PORT_ID2:
511 			src_mod_op_port_id[2] = le32_to_cpu(mod_elem->value);
512 			break;
513 		case AR_TKN_U32_MODULE_SRC_OP_PORT_ID3:
514 			src_mod_op_port_id[3] = le32_to_cpu(mod_elem->value);
515 			break;
516 		case AR_TKN_U32_MODULE_SRC_OP_PORT_ID4:
517 			src_mod_op_port_id[4] = le32_to_cpu(mod_elem->value);
518 			break;
519 		case AR_TKN_U32_MODULE_SRC_OP_PORT_ID5:
520 			src_mod_op_port_id[5] = le32_to_cpu(mod_elem->value);
521 			break;
522 		case AR_TKN_U32_MODULE_SRC_OP_PORT_ID6:
523 			src_mod_op_port_id[6] = le32_to_cpu(mod_elem->value);
524 			break;
525 		case AR_TKN_U32_MODULE_SRC_OP_PORT_ID7:
526 			src_mod_op_port_id[7] = le32_to_cpu(mod_elem->value);
527 			break;
528 		case AR_TKN_U32_MODULE_DST_INSTANCE_ID:
529 			dst_mod_inst_id[0] = le32_to_cpu(mod_elem->value);
530 			break;
531 		case AR_TKN_U32_MODULE_DST_INSTANCE_ID1:
532 			dst_mod_inst_id[1] = le32_to_cpu(mod_elem->value);
533 			break;
534 		case AR_TKN_U32_MODULE_DST_INSTANCE_ID2:
535 			dst_mod_inst_id[2] = le32_to_cpu(mod_elem->value);
536 			break;
537 		case AR_TKN_U32_MODULE_DST_INSTANCE_ID3:
538 			dst_mod_inst_id[3] = le32_to_cpu(mod_elem->value);
539 			break;
540 		case AR_TKN_U32_MODULE_DST_INSTANCE_ID4:
541 			dst_mod_inst_id[4] = le32_to_cpu(mod_elem->value);
542 			break;
543 		case AR_TKN_U32_MODULE_DST_INSTANCE_ID5:
544 			dst_mod_inst_id[5] = le32_to_cpu(mod_elem->value);
545 			break;
546 		case AR_TKN_U32_MODULE_DST_INSTANCE_ID6:
547 			dst_mod_inst_id[6] = le32_to_cpu(mod_elem->value);
548 			break;
549 		case AR_TKN_U32_MODULE_DST_INSTANCE_ID7:
550 			dst_mod_inst_id[7] = le32_to_cpu(mod_elem->value);
551 			break;
552 		case AR_TKN_U32_MODULE_DST_IN_PORT_ID:
553 			dst_mod_ip_port_id[0] = le32_to_cpu(mod_elem->value);
554 			break;
555 		case AR_TKN_U32_MODULE_DST_IN_PORT_ID1:
556 			dst_mod_ip_port_id[1] = le32_to_cpu(mod_elem->value);
557 			break;
558 		case AR_TKN_U32_MODULE_DST_IN_PORT_ID2:
559 			dst_mod_ip_port_id[2] = le32_to_cpu(mod_elem->value);
560 			break;
561 		case AR_TKN_U32_MODULE_DST_IN_PORT_ID3:
562 			dst_mod_ip_port_id[3] = le32_to_cpu(mod_elem->value);
563 			break;
564 		case AR_TKN_U32_MODULE_DST_IN_PORT_ID4:
565 			dst_mod_ip_port_id[4] = le32_to_cpu(mod_elem->value);
566 			break;
567 		case AR_TKN_U32_MODULE_DST_IN_PORT_ID5:
568 			dst_mod_ip_port_id[5] = le32_to_cpu(mod_elem->value);
569 			break;
570 		case AR_TKN_U32_MODULE_DST_IN_PORT_ID6:
571 			dst_mod_ip_port_id[6] = le32_to_cpu(mod_elem->value);
572 			break;
573 		case AR_TKN_U32_MODULE_DST_IN_PORT_ID7:
574 			dst_mod_ip_port_id[7] = le32_to_cpu(mod_elem->value);
575 			break;
576 		default:
577 			break;
578 
579 		}
580 		tkn_count++;
581 		mod_elem++;
582 	}
583 
584 	if (mod) {
585 		int pn, id = 0;
586 
587 		mod->module_id = module_id;
588 		mod->max_ip_port = max_ip_port;
589 		mod->max_op_port = max_op_port;
590 		mod->src_mod_inst_id = src_mod_inst_id;
591 		for (pn = 0; pn < mod->max_op_port; pn++) {
592 			if (src_mod_op_port_id[pn] && dst_mod_inst_id[pn] &&
593 			    dst_mod_ip_port_id[pn]) {
594 				mod->src_mod_op_port_id[id] = src_mod_op_port_id[pn];
595 				mod->dst_mod_inst_id[id] = dst_mod_inst_id[pn];
596 				mod->dst_mod_ip_port_id[id] = dst_mod_ip_port_id[pn];
597 				id++;
598 				mod->num_connections = id;
599 			}
600 		}
601 	}
602 
603 	return mod;
604 }
605 
606 static int audioreach_widget_load_module_common(struct snd_soc_component *component,
607 						int index, struct snd_soc_dapm_widget *w,
608 						const struct snd_soc_tplg_dapm_widget *tplg_w)
609 {
610 	struct q6apm *apm = dev_get_drvdata(component->dev);
611 	struct audioreach_container *cont;
612 	struct audioreach_sub_graph *sg;
613 	struct audioreach_module *mod;
614 	struct snd_soc_dobj *dobj;
615 
616 	sg = audioreach_parse_sg_tokens(apm, &tplg_w->priv);
617 	if (IS_ERR(sg))
618 		return PTR_ERR(sg);
619 
620 	cont = audioreach_parse_cont_tokens(apm, sg, &tplg_w->priv);
621 	if (IS_ERR(cont))
622 		return PTR_ERR(cont);
623 
624 	mod = audioreach_parse_common_tokens(apm, cont, &tplg_w->priv, w);
625 	if (IS_ERR_OR_NULL(mod))
626 		return mod ? PTR_ERR(mod) : -ENODEV;
627 
628 	mod->data = audioreach_get_module_priv_data(&tplg_w->priv);
629 	if (IS_ERR(mod->data)) {
630 		int rc = PTR_ERR(mod->data);
631 
632 		mod->data = NULL;
633 		return rc;
634 	}
635 
636 	dobj = &w->dobj;
637 	dobj->private = mod;
638 
639 	return 0;
640 }
641 
642 static int audioreach_widget_load_enc_dec_cnv(struct snd_soc_component *component,
643 					      int index, struct snd_soc_dapm_widget *w,
644 					      const struct snd_soc_tplg_dapm_widget *tplg_w)
645 {
646 	const struct snd_soc_tplg_vendor_value_elem *mod_elem;
647 	const struct snd_soc_tplg_vendor_array *mod_array;
648 	struct audioreach_module *mod;
649 	struct snd_soc_dobj *dobj;
650 	int tkn_count = 0;
651 	int ret;
652 
653 	ret = audioreach_widget_load_module_common(component, index, w, tplg_w);
654 	if (ret)
655 		return ret;
656 
657 	dobj = &w->dobj;
658 	mod = dobj->private;
659 	mod_array = audioreach_get_module_array(&tplg_w->priv);
660 	mod_elem = mod_array->value;
661 
662 	while (tkn_count <= (le32_to_cpu(mod_array->num_elems) - 1)) {
663 		switch (le32_to_cpu(mod_elem->token)) {
664 		case AR_TKN_U32_MODULE_FMT_INTERLEAVE:
665 			mod->interleave_type = le32_to_cpu(mod_elem->value);
666 			break;
667 		case AR_TKN_U32_MODULE_FMT_SAMPLE_RATE:
668 			mod->rate = le32_to_cpu(mod_elem->value);
669 			break;
670 		case AR_TKN_U32_MODULE_FMT_BIT_DEPTH:
671 			mod->bit_depth = le32_to_cpu(mod_elem->value);
672 			break;
673 		default:
674 			break;
675 		}
676 		tkn_count++;
677 		mod_elem++;
678 	}
679 
680 	return 0;
681 }
682 
683 static int audioreach_widget_log_module_load(struct audioreach_module *mod,
684 					     const struct snd_soc_tplg_vendor_array *mod_array)
685 {
686 	const struct snd_soc_tplg_vendor_value_elem *mod_elem;
687 	int tkn_count = 0;
688 
689 	mod_elem = mod_array->value;
690 
691 	while (tkn_count <= (le32_to_cpu(mod_array->num_elems) - 1)) {
692 		switch (le32_to_cpu(mod_elem->token)) {
693 
694 		case AR_TKN_U32_MODULE_LOG_CODE:
695 			mod->log_code = le32_to_cpu(mod_elem->value);
696 			break;
697 		case AR_TKN_U32_MODULE_LOG_TAP_POINT_ID:
698 			mod->log_tap_point_id = le32_to_cpu(mod_elem->value);
699 			break;
700 		case AR_TKN_U32_MODULE_LOG_MODE:
701 			mod->log_mode = le32_to_cpu(mod_elem->value);
702 			break;
703 		default:
704 			break;
705 		}
706 		tkn_count++;
707 		mod_elem++;
708 	}
709 
710 	return 0;
711 }
712 
713 static int audioreach_widget_dma_module_load(struct audioreach_module *mod,
714 					     const struct snd_soc_tplg_vendor_array *mod_array)
715 {
716 	const struct snd_soc_tplg_vendor_value_elem *mod_elem;
717 	int tkn_count = 0;
718 
719 	mod_elem = mod_array->value;
720 
721 	while (tkn_count <= (le32_to_cpu(mod_array->num_elems) - 1)) {
722 		switch (le32_to_cpu(mod_elem->token)) {
723 		case AR_TKN_U32_MODULE_HW_IF_IDX:
724 			mod->hw_interface_idx = le32_to_cpu(mod_elem->value);
725 			break;
726 		case AR_TKN_U32_MODULE_FMT_DATA:
727 			mod->data_format = le32_to_cpu(mod_elem->value);
728 			break;
729 		case AR_TKN_U32_MODULE_HW_IF_TYPE:
730 			mod->hw_interface_type = le32_to_cpu(mod_elem->value);
731 			break;
732 		default:
733 			break;
734 		}
735 		tkn_count++;
736 		mod_elem++;
737 	}
738 
739 	return 0;
740 }
741 
742 static int audioreach_widget_i2s_module_load(struct audioreach_module *mod,
743 					     const struct snd_soc_tplg_vendor_array *mod_array)
744 {
745 	const struct snd_soc_tplg_vendor_value_elem *mod_elem;
746 	int tkn_count = 0;
747 
748 	mod_elem = mod_array->value;
749 
750 	while (tkn_count <= (le32_to_cpu(mod_array->num_elems) - 1)) {
751 		switch (le32_to_cpu(mod_elem->token)) {
752 		case AR_TKN_U32_MODULE_HW_IF_IDX:
753 			mod->hw_interface_idx = le32_to_cpu(mod_elem->value);
754 			break;
755 		case AR_TKN_U32_MODULE_FMT_DATA:
756 			mod->data_format = le32_to_cpu(mod_elem->value);
757 			break;
758 		case AR_TKN_U32_MODULE_HW_IF_TYPE:
759 			mod->hw_interface_type = le32_to_cpu(mod_elem->value);
760 			break;
761 		case AR_TKN_U32_MODULE_SD_LINE_IDX:
762 			mod->sd_line_idx = le32_to_cpu(mod_elem->value);
763 			break;
764 		case AR_TKN_U32_MODULE_WS_SRC:
765 			mod->ws_src = le32_to_cpu(mod_elem->value);
766 			break;
767 		default:
768 			break;
769 		}
770 		tkn_count++;
771 		mod_elem++;
772 	}
773 
774 	return 0;
775 }
776 
777 static int audioreach_widget_audio_if_module_load(struct audioreach_module *mod,
778 						  const struct snd_soc_tplg_vendor_array *mod_array)
779 {
780 	const struct snd_soc_tplg_vendor_value_elem *mod_elem;
781 	int tkn_count = 0;
782 	u32 val;
783 
784 	mod_elem = mod_array->value;
785 
786 	while (tkn_count < le32_to_cpu(mod_array->num_elems)) {
787 		val = le32_to_cpu(mod_elem->value);
788 		switch (le32_to_cpu(mod_elem->token)) {
789 		case AR_TKN_U32_MODULE_HW_IF_IDX:
790 			mod->hw_interface_idx = val;
791 			break;
792 		case AR_TKN_U32_MODULE_FMT_DATA:
793 			mod->data_format = val;
794 			break;
795 		case AR_TKN_U16_MODULE_SYNC_SRC:
796 			if (val > U16_MAX)
797 				return -EINVAL;
798 			mod->sync_src = (u16)val;
799 			break;
800 		case AR_TKN_U16_MODULE_CTRL_DATA_OUT_ENABLE:
801 			if (val > U16_MAX)
802 				return -EINVAL;
803 			mod->ctrl_data_out_enable = (u16)val;
804 			break;
805 		case AR_TKN_U32_MODULE_SLOT_MASK:
806 			mod->slot_mask = val;
807 			break;
808 		case AR_TKN_U16_MODULE_NSLOTS_PER_FRAME:
809 			if (val > U16_MAX)
810 				return -EINVAL;
811 			mod->nslots_per_frame = (u16)val;
812 			break;
813 		case AR_TKN_U16_MODULE_SLOT_WIDTH:
814 			if (val > U16_MAX)
815 				return -EINVAL;
816 			mod->slot_width = (u16)val;
817 			break;
818 		case AR_TKN_U16_MODULE_INTF_MODE:
819 			if (val > U16_MAX)
820 				return -EINVAL;
821 			mod->intf_mode = (u16)val;
822 			break;
823 		case AR_TKN_U16_MODULE_SYNC_MODE:
824 			if (val > U16_MAX)
825 				return -EINVAL;
826 			mod->sync_mode = (u16)val;
827 			break;
828 		case AR_TKN_U16_MODULE_CTRL_INVERT_SYNC_PULSE:
829 			if (val > U16_MAX)
830 				return -EINVAL;
831 			mod->ctrl_invert_sync_pulse = (u16)val;
832 			break;
833 		case AR_TKN_U16_MODULE_CTRL_SYNC_DATA_DELAY:
834 			if (val > U16_MAX)
835 				return -EINVAL;
836 			mod->ctrl_sync_data_delay = (u16)val;
837 			break;
838 		case AR_TKN_U16_MODULE_QAIF_TYPE:
839 			if (val > U16_MAX)
840 				return -EINVAL;
841 			mod->qaif_type = (u16)val;
842 			break;
843 		case AR_TKN_U32_MODULE_ACTIVE_LANE_MASK:
844 			mod->active_lane_mask = val;
845 			break;
846 		case AR_TKN_U32_MODULE_FRAME_SYNC_RATE:
847 			mod->frame_sync_rate = val;
848 			break;
849 		case AR_TKN_U16_MODULE_BIT_CLK_TYPE:
850 			if (val > U16_MAX)
851 				return -EINVAL;
852 			mod->bit_clk_type = (u16)val;
853 			break;
854 		case AR_TKN_U8_MODULE_INV_INT_BIT_CLK:
855 			if (val > U8_MAX)
856 				return -EINVAL;
857 			mod->inv_int_bit_clk = (u8)val;
858 			break;
859 		case AR_TKN_U8_MODULE_INV_EXT_BIT_CLK:
860 			if (val > U8_MAX)
861 				return -EINVAL;
862 			mod->inv_ext_bit_clk = (u8)val;
863 			break;
864 		default:
865 			break;
866 		}
867 		tkn_count++;
868 		mod_elem++;
869 	}
870 
871 	return 0;
872 }
873 
874 static int audioreach_widget_dp_module_load(struct audioreach_module *mod,
875 					    const struct snd_soc_tplg_vendor_array *mod_array)
876 {
877 	const struct snd_soc_tplg_vendor_value_elem *mod_elem;
878 	int tkn_count = 0;
879 
880 	mod_elem = mod_array->value;
881 
882 	while (tkn_count <= (le32_to_cpu(mod_array->num_elems) - 1)) {
883 		switch (le32_to_cpu(mod_elem->token)) {
884 		case AR_TKN_U32_MODULE_FMT_DATA:
885 			mod->data_format = le32_to_cpu(mod_elem->value);
886 			break;
887 		default:
888 			break;
889 		}
890 		tkn_count++;
891 		mod_elem++;
892 	}
893 
894 	return 0;
895 }
896 
897 static int audioreach_widget_load_buffer(struct snd_soc_component *component,
898 					 int index, struct snd_soc_dapm_widget *w,
899 					 const struct snd_soc_tplg_dapm_widget *tplg_w)
900 {
901 	const struct snd_soc_tplg_vendor_array *mod_array;
902 	struct audioreach_module *mod;
903 	struct snd_soc_dobj *dobj;
904 	int ret;
905 
906 	ret = audioreach_widget_load_module_common(component, index, w, tplg_w);
907 	if (ret)
908 		return ret;
909 
910 	dobj = &w->dobj;
911 	mod = dobj->private;
912 
913 	mod_array = audioreach_get_module_array(&tplg_w->priv);
914 
915 	switch (mod->module_id) {
916 	case MODULE_ID_CODEC_DMA_SINK:
917 	case MODULE_ID_CODEC_DMA_SOURCE:
918 		audioreach_widget_dma_module_load(mod, mod_array);
919 		break;
920 	case MODULE_ID_DATA_LOGGING:
921 		audioreach_widget_log_module_load(mod, mod_array);
922 		break;
923 	case MODULE_ID_I2S_SINK:
924 	case MODULE_ID_I2S_SOURCE:
925 		audioreach_widget_i2s_module_load(mod, mod_array);
926 		break;
927 	case MODULE_ID_AUDIO_IF_SINK:
928 	case MODULE_ID_AUDIO_IF_SOURCE:
929 		ret = audioreach_widget_audio_if_module_load(mod, mod_array);
930 		if (ret)
931 			return ret;
932 		break;
933 	case MODULE_ID_DISPLAY_PORT_SINK:
934 		audioreach_widget_dp_module_load(mod, mod_array);
935 		break;
936 	default:
937 		return -EINVAL;
938 	}
939 
940 	return 0;
941 }
942 
943 static int audioreach_widget_load_mixer(struct snd_soc_component *component,
944 					int index, struct snd_soc_dapm_widget *w,
945 					const struct snd_soc_tplg_dapm_widget *tplg_w)
946 {
947 	const struct snd_soc_tplg_vendor_value_elem *w_elem;
948 	const struct snd_soc_tplg_vendor_array *w_array;
949 	struct snd_ar_control *scontrol;
950 	struct q6apm *data = dev_get_drvdata(component->dev);
951 	struct snd_soc_dobj *dobj;
952 	int tkn_count = 0;
953 
954 	w_array = &tplg_w->priv.array[0];
955 
956 	scontrol = kzalloc_obj(*scontrol);
957 	if (!scontrol)
958 		return -ENOMEM;
959 
960 	scontrol->scomp = component;
961 	dobj = &w->dobj;
962 	dobj->private = scontrol;
963 
964 	w_elem = w_array->value;
965 	while (tkn_count <= (le32_to_cpu(w_array->num_elems) - 1)) {
966 		switch (le32_to_cpu(w_elem->token)) {
967 		case AR_TKN_U32_SUB_GRAPH_INSTANCE_ID:
968 			scontrol->sgid = le32_to_cpu(w_elem->value);
969 			break;
970 		case AR_TKN_DAI_INDEX:
971 			scontrol->graph_id = le32_to_cpu(w_elem->value);
972 			break;
973 		default: /* ignore other tokens */
974 			break;
975 		}
976 		tkn_count++;
977 		w_elem++;
978 	}
979 
980 	scontrol->w = w;
981 	list_add_tail(&scontrol->node, &data->widget_list);
982 
983 	return 0;
984 }
985 
986 static int audioreach_pga_event(struct snd_soc_dapm_widget *w,
987 				struct snd_kcontrol *kcontrol, int event)
988 
989 {
990 	struct snd_soc_dapm_context *dapm = w->dapm;
991 	struct snd_soc_component *c = snd_soc_dapm_to_component(dapm);
992 	struct audioreach_module *mod = w->dobj.private;
993 	struct q6apm *apm = dev_get_drvdata(c->dev);
994 
995 	switch (event) {
996 	case SND_SOC_DAPM_POST_PMU:
997 		/* apply gain after power up of widget */
998 		audioreach_gain_set_vol_ctrl(apm, mod, mod->gain);
999 		break;
1000 	default:
1001 		break;
1002 	}
1003 
1004 	return 0;
1005 }
1006 
1007 static const struct snd_soc_tplg_widget_events audioreach_widget_ops[] = {
1008 	{ AR_PGA_DAPM_EVENT, audioreach_pga_event },
1009 };
1010 
1011 static int audioreach_widget_load_pga(struct snd_soc_component *component,
1012 				      int index, struct snd_soc_dapm_widget *w,
1013 				      const struct snd_soc_tplg_dapm_widget *tplg_w)
1014 {
1015 	struct audioreach_module *mod;
1016 	struct snd_soc_dobj *dobj;
1017 	int ret;
1018 
1019 	ret = audioreach_widget_load_module_common(component, index, w, tplg_w);
1020 	if (ret)
1021 		return ret;
1022 
1023 	dobj = &w->dobj;
1024 	mod = dobj->private;
1025 	mod->gain = VOL_CTRL_DEFAULT_GAIN;
1026 
1027 	ret = snd_soc_tplg_widget_bind_event(w, audioreach_widget_ops,
1028 					     ARRAY_SIZE(audioreach_widget_ops),
1029 					     le16_to_cpu(tplg_w->event_type));
1030 	if (ret) {
1031 		dev_err(component->dev, "matching event handlers NOT found for %d\n",
1032 			le16_to_cpu(tplg_w->event_type));
1033 		return -EINVAL;
1034 	}
1035 
1036 	return 0;
1037 }
1038 
1039 static int audioreach_widget_ready(struct snd_soc_component *component,
1040 				   int index, struct snd_soc_dapm_widget *w,
1041 				   struct snd_soc_tplg_dapm_widget *tplg_w)
1042 {
1043 	switch (w->id) {
1044 	case snd_soc_dapm_aif_in:
1045 	case snd_soc_dapm_aif_out:
1046 		audioreach_widget_load_buffer(component, index, w, tplg_w);
1047 		break;
1048 	case snd_soc_dapm_decoder:
1049 	case snd_soc_dapm_encoder:
1050 	case snd_soc_dapm_src:
1051 		audioreach_widget_load_enc_dec_cnv(component, index, w, tplg_w);
1052 		break;
1053 	case snd_soc_dapm_buffer:
1054 		audioreach_widget_load_buffer(component, index, w, tplg_w);
1055 		break;
1056 	case snd_soc_dapm_mixer:
1057 		return audioreach_widget_load_mixer(component, index, w, tplg_w);
1058 	case snd_soc_dapm_pga:
1059 		return audioreach_widget_load_pga(component, index, w, tplg_w);
1060 	case snd_soc_dapm_dai_link:
1061 	case snd_soc_dapm_scheduler:
1062 	case snd_soc_dapm_out_drv:
1063 	default:
1064 		dev_err(component->dev, "Widget type (0x%x) not yet supported\n", w->id);
1065 		break;
1066 	}
1067 
1068 	return 0;
1069 }
1070 
1071 static int audioreach_widget_unload(struct snd_soc_component *scomp,
1072 				    struct snd_soc_dobj *dobj)
1073 {
1074 	const struct snd_soc_dapm_widget *w = container_of(dobj, struct snd_soc_dapm_widget, dobj);
1075 	struct q6apm *apm = dev_get_drvdata(scomp->dev);
1076 	struct audioreach_container *cont;
1077 	struct audioreach_module *mod;
1078 
1079 	if (w->id == snd_soc_dapm_mixer) {
1080 		/* virtual widget */
1081 		struct snd_ar_control *scontrol = dobj->private;
1082 
1083 		list_del(&scontrol->node);
1084 		kfree(scontrol);
1085 		return 0;
1086 	}
1087 	mod = dobj->private;
1088 	if (!mod)
1089 		return 0;
1090 
1091 	cont = mod->container;
1092 
1093 	mutex_lock(&apm->lock);
1094 	idr_remove(&apm->modules_idr, mod->instance_id);
1095 	cont->num_modules--;
1096 
1097 	list_del(&mod->node);
1098 	kfree(mod->data);
1099 	kfree(mod);
1100 	/* Graph Info has N sub-graphs, sub-graph has N containers, Container has N Modules */
1101 	if (list_empty(&cont->modules_list)) { /* if no modules in the container then remove it */
1102 		struct audioreach_sub_graph *sg = cont->sub_graph;
1103 
1104 		idr_remove(&apm->containers_idr, cont->container_id);
1105 		list_del(&cont->node);
1106 		sg->num_containers--;
1107 		kfree(cont);
1108 		/* check if there are no more containers in the sub graph and remove it */
1109 		if (list_empty(&sg->container_list)) {
1110 			struct audioreach_graph_info *info = sg->info;
1111 
1112 			idr_remove(&apm->sub_graphs_idr, sg->sub_graph_id);
1113 			list_del(&sg->node);
1114 			info->num_sub_graphs--;
1115 			kfree(sg);
1116 			/* Check if there are no more sub-graphs left then remove graph info */
1117 			if (list_empty(&info->sg_list)) {
1118 				idr_remove(&apm->graph_info_idr, info->id);
1119 				kfree(info);
1120 			}
1121 		}
1122 	}
1123 
1124 	mutex_unlock(&apm->lock);
1125 
1126 	return 0;
1127 }
1128 
1129 static struct snd_ar_control *audioreach_find_widget(struct snd_soc_component *comp,
1130 						     const char *name)
1131 {
1132 	struct q6apm *apm = dev_get_drvdata(comp->dev);
1133 	struct snd_ar_control *control;
1134 
1135 	list_for_each_entry(control, &apm->widget_list, node) {
1136 		if (control->w && !strcmp(name, control->w->name))
1137 			return control;
1138 	}
1139 
1140 	return NULL;
1141 }
1142 
1143 static struct audioreach_module *audioreach_find_module(struct snd_soc_component *comp,
1144 							const char *name)
1145 {
1146 	struct q6apm *apm = dev_get_drvdata(comp->dev);
1147 	struct audioreach_module *module;
1148 	int id;
1149 
1150 	idr_for_each_entry(&apm->modules_idr, module, id) {
1151 		if (!strcmp(name, module->widget->name))
1152 			return module;
1153 	}
1154 
1155 	return NULL;
1156 }
1157 
1158 static int audioreach_route_load(struct snd_soc_component *scomp, int index,
1159 				 struct snd_soc_dapm_route *route)
1160 {
1161 	const struct audioreach_module *src_module, *sink_module;
1162 	struct snd_ar_control *control;
1163 	struct snd_soc_dapm_widget *w;
1164 	int i;
1165 
1166 	/* check if these are actual modules */
1167 	src_module = audioreach_find_module(scomp, route->source);
1168 	sink_module = audioreach_find_module(scomp, route->sink);
1169 
1170 	if (sink_module && !src_module) {
1171 		control = audioreach_find_widget(scomp, route->source);
1172 		if (control)
1173 			control->module_instance_id = sink_module->instance_id;
1174 
1175 	} else if (!sink_module && src_module && route->control) {
1176 		/* check if this is a virtual mixer */
1177 		control = audioreach_find_widget(scomp, route->sink);
1178 		if (!control || !control->w)
1179 			return 0;
1180 
1181 		w = control->w;
1182 
1183 		for (i = 0; i < w->num_kcontrols; i++) {
1184 			if (!strcmp(route->control, w->kcontrol_news[i].name)) {
1185 				struct soc_mixer_control *sm;
1186 				struct snd_soc_dobj *dobj;
1187 				struct snd_ar_control *scontrol;
1188 
1189 				sm = (struct soc_mixer_control *)w->kcontrol_news[i].private_value;
1190 				dobj = &sm->dobj;
1191 				scontrol = dobj->private;
1192 				scontrol->module_instance_id = src_module->instance_id;
1193 			}
1194 		}
1195 
1196 	}
1197 
1198 	return 0;
1199 }
1200 
1201 static int audioreach_route_unload(struct snd_soc_component *scomp,
1202 				   struct snd_soc_dobj *dobj)
1203 {
1204 	return 0;
1205 }
1206 
1207 static int audioreach_tplg_complete(struct snd_soc_component *component)
1208 {
1209 	/* TBD */
1210 	return 0;
1211 }
1212 
1213 /* DAI link - used for any driver specific init */
1214 static int audioreach_link_load(struct snd_soc_component *component, int index,
1215 				struct snd_soc_dai_link *link,
1216 				struct snd_soc_tplg_link_config *cfg)
1217 {
1218 	link->nonatomic = true;
1219 	link->dynamic = true;
1220 	link->platforms->name = NULL;
1221 	link->platforms->of_node = of_get_compatible_child(component->dev->of_node,
1222 							   "qcom,q6apm-dais");
1223 	return 0;
1224 }
1225 
1226 static void audioreach_connect_sub_graphs(struct q6apm *apm,
1227 					  const struct snd_ar_control *m1,
1228 					  const struct snd_ar_control *m2,
1229 					  bool connect)
1230 {
1231 	struct audioreach_graph_info *info;
1232 
1233 	mutex_lock(&apm->lock);
1234 	info = idr_find(&apm->graph_info_idr, m2->graph_id);
1235 	mutex_unlock(&apm->lock);
1236 
1237 	if (connect) {
1238 		info->src_mod_inst_id = m1->module_instance_id;
1239 		info->src_mod_op_port_id = 1;
1240 		info->dst_mod_inst_id = m2->module_instance_id;
1241 		info->dst_mod_ip_port_id = 2;
1242 
1243 	} else if (info->src_mod_inst_id == m1->module_instance_id &&
1244 		   info->dst_mod_inst_id == m2->module_instance_id) {
1245 		/* only drop the link this control made, not another FE's */
1246 		info->src_mod_inst_id = 0;
1247 		info->src_mod_op_port_id = 0;
1248 		info->dst_mod_inst_id = 0;
1249 		info->dst_mod_ip_port_id = 0;
1250 	}
1251 }
1252 
1253 static bool audioreach_is_vmixer_connected(struct q6apm *apm,
1254 					   const struct snd_ar_control *m1,
1255 					   const struct snd_ar_control *m2)
1256 {
1257 	const struct audioreach_graph_info *info;
1258 
1259 	mutex_lock(&apm->lock);
1260 	info = idr_find(&apm->graph_info_idr, m2->graph_id);
1261 	mutex_unlock(&apm->lock);
1262 
1263 	if (info->dst_mod_inst_id == m2->module_instance_id &&
1264 	    info->src_mod_inst_id == m1->module_instance_id)
1265 		return true;
1266 
1267 	return false;
1268 }
1269 
1270 static int audioreach_get_audio_mixer(struct snd_kcontrol *kcontrol,
1271 				      struct snd_ctl_elem_value *ucontrol)
1272 {
1273 	struct soc_mixer_control *mc = (struct soc_mixer_control *)kcontrol->private_value;
1274 	struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_to_dapm(kcontrol);
1275 	const struct snd_soc_dapm_widget *dw = snd_soc_dapm_kcontrol_to_widget(kcontrol);
1276 	struct snd_soc_component *c = snd_soc_dapm_to_component(dapm);
1277 	const struct snd_ar_control *dapm_scontrol = dw->dobj.private;
1278 	const struct snd_ar_control *scontrol = mc->dobj.private;
1279 	struct q6apm *data = dev_get_drvdata(c->dev);
1280 	bool connected;
1281 
1282 	connected = audioreach_is_vmixer_connected(data, scontrol, dapm_scontrol);
1283 	if (connected)
1284 		ucontrol->value.integer.value[0] = 1;
1285 	else
1286 		ucontrol->value.integer.value[0] = 0;
1287 
1288 	return 0;
1289 }
1290 
1291 static int audioreach_put_audio_mixer(struct snd_kcontrol *kcontrol,
1292 				      struct snd_ctl_elem_value *ucontrol)
1293 {
1294 	struct soc_mixer_control *mc = (struct soc_mixer_control *)kcontrol->private_value;
1295 	struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_to_dapm(kcontrol);
1296 	struct snd_soc_dapm_widget *dw = snd_soc_dapm_kcontrol_to_widget(kcontrol);
1297 	struct snd_soc_component *c = snd_soc_dapm_to_component(dapm);
1298 	const struct snd_ar_control *dapm_scontrol = dw->dobj.private;
1299 	const struct snd_ar_control *scontrol = mc->dobj.private;
1300 	struct q6apm *data = dev_get_drvdata(c->dev);
1301 
1302 	if (ucontrol->value.integer.value[0]) {
1303 		audioreach_connect_sub_graphs(data, scontrol, dapm_scontrol, true);
1304 		snd_soc_dapm_mixer_update_power(dapm, kcontrol, 1, NULL);
1305 	} else {
1306 		audioreach_connect_sub_graphs(data, scontrol, dapm_scontrol, false);
1307 		snd_soc_dapm_mixer_update_power(dapm, kcontrol, 0, NULL);
1308 	}
1309 	return 0;
1310 }
1311 
1312 static int audioreach_get_vol_ctrl_audio_mixer(struct snd_kcontrol *kcontrol,
1313 					       struct snd_ctl_elem_value *ucontrol)
1314 {
1315 	struct snd_soc_dapm_widget *dw = snd_soc_dapm_kcontrol_to_widget(kcontrol);
1316 	struct audioreach_module *mod = dw->dobj.private;
1317 
1318 	ucontrol->value.integer.value[0] = mod->gain;
1319 
1320 	return 0;
1321 }
1322 
1323 static int audioreach_put_vol_ctrl_audio_mixer(struct snd_kcontrol *kcontrol,
1324 					       struct snd_ctl_elem_value *ucontrol)
1325 {
1326 	struct snd_soc_dapm_widget *dw = snd_soc_dapm_kcontrol_to_widget(kcontrol);
1327 	struct audioreach_module *mod = dw->dobj.private;
1328 
1329 	mod->gain = ucontrol->value.integer.value[0];
1330 
1331 	return 1;
1332 }
1333 
1334 static int audioreach_control_load_mix(struct snd_soc_component *scomp,
1335 				       struct snd_ar_control *scontrol,
1336 				       struct snd_kcontrol_new *kc,
1337 				       const struct snd_soc_tplg_ctl_hdr *hdr)
1338 {
1339 	const struct snd_soc_tplg_vendor_value_elem *c_elem;
1340 	const struct snd_soc_tplg_vendor_array *c_array;
1341 	const struct snd_soc_tplg_mixer_control *mc;
1342 	int tkn_count = 0;
1343 
1344 	mc = container_of_const(hdr, struct snd_soc_tplg_mixer_control, hdr);
1345 	c_array = (struct snd_soc_tplg_vendor_array *)mc->priv.data;
1346 
1347 	c_elem = c_array->value;
1348 
1349 	while (tkn_count <= (le32_to_cpu(c_array->num_elems) - 1)) {
1350 		switch (le32_to_cpu(c_elem->token)) {
1351 		case AR_TKN_U32_SUB_GRAPH_INSTANCE_ID:
1352 			scontrol->sgid = le32_to_cpu(c_elem->value);
1353 			break;
1354 		case AR_TKN_DAI_INDEX:
1355 			scontrol->graph_id = le32_to_cpu(c_elem->value);
1356 			break;
1357 		default:
1358 			/* Ignore other tokens */
1359 			break;
1360 		}
1361 		c_elem++;
1362 		tkn_count++;
1363 	}
1364 
1365 	return 0;
1366 }
1367 
1368 static int audioreach_control_load(struct snd_soc_component *scomp, int index,
1369 				   struct snd_kcontrol_new *kc,
1370 				   struct snd_soc_tplg_ctl_hdr *hdr)
1371 {
1372 	struct snd_ar_control *scontrol;
1373 	struct soc_mixer_control *sm;
1374 	struct snd_soc_dobj *dobj;
1375 	int ret = 0;
1376 
1377 	scontrol = kzalloc_obj(*scontrol);
1378 	if (!scontrol)
1379 		return -ENOMEM;
1380 
1381 	scontrol->scomp = scomp;
1382 
1383 	switch (le32_to_cpu(hdr->ops.get)) {
1384 	case SND_SOC_AR_TPLG_FE_BE_GRAPH_CTL_MIX:
1385 		sm = (struct soc_mixer_control *)kc->private_value;
1386 		dobj = &sm->dobj;
1387 		ret = audioreach_control_load_mix(scomp, scontrol, kc, hdr);
1388 		break;
1389 	case SND_SOC_AR_TPLG_VOL_CTL:
1390 		sm = (struct soc_mixer_control *)kc->private_value;
1391 		dobj = &sm->dobj;
1392 		break;
1393 	default:
1394 		dev_warn(scomp->dev, "control type not supported %d:%d:%d\n",
1395 			 hdr->ops.get, hdr->ops.put, hdr->ops.info);
1396 		kfree(scontrol);
1397 		return -EINVAL;
1398 	}
1399 
1400 	dobj->private = scontrol;
1401 	return ret;
1402 }
1403 
1404 static int audioreach_control_unload(struct snd_soc_component *scomp,
1405 				     struct snd_soc_dobj *dobj)
1406 {
1407 	struct snd_ar_control *scontrol = dobj->private;
1408 
1409 	kfree(scontrol);
1410 
1411 	return 0;
1412 }
1413 
1414 static const struct snd_soc_tplg_kcontrol_ops audioreach_io_ops[] = {
1415 	{SND_SOC_AR_TPLG_FE_BE_GRAPH_CTL_MIX, audioreach_get_audio_mixer,
1416 		audioreach_put_audio_mixer, snd_soc_info_volsw},
1417 	{SND_SOC_AR_TPLG_VOL_CTL, audioreach_get_vol_ctrl_audio_mixer,
1418 		audioreach_put_vol_ctrl_audio_mixer, snd_soc_info_volsw},
1419 };
1420 
1421 static const struct snd_soc_tplg_ops audioreach_tplg_ops = {
1422 	.io_ops = audioreach_io_ops,
1423 	.io_ops_count = ARRAY_SIZE(audioreach_io_ops),
1424 
1425 	.control_load	= audioreach_control_load,
1426 	.control_unload	= audioreach_control_unload,
1427 
1428 	.widget_ready = audioreach_widget_ready,
1429 	.widget_unload = audioreach_widget_unload,
1430 
1431 	.complete = audioreach_tplg_complete,
1432 	.link_load = audioreach_link_load,
1433 
1434 	.dapm_route_load	= audioreach_route_load,
1435 	.dapm_route_unload	= audioreach_route_unload,
1436 };
1437 
1438 int audioreach_tplg_init(struct snd_soc_component *component)
1439 {
1440 	struct snd_soc_card *card = component->card;
1441 	struct device *dev = component->dev;
1442 	int ret;
1443 
1444 	/* Inline with Qualcomm UCM configs and linux-firmware path */
1445 	char *tplg_fw_name __free(kfree) = kasprintf(GFP_KERNEL, "qcom/%s/%s-tplg.bin",
1446 						     card->driver_name,
1447 						     card->name);
1448 	if (!tplg_fw_name)
1449 		return -ENOMEM;
1450 
1451 	const struct firmware *fw __free(firmware) = NULL;
1452 	ret = request_firmware(&fw, tplg_fw_name, dev);
1453 	if (ret < 0) {
1454 		dev_err(dev, "tplg firmware loading %s failed %d\n", tplg_fw_name, ret);
1455 		return ret;
1456 	}
1457 
1458 	ret = snd_soc_tplg_component_load(component, &audioreach_tplg_ops, fw);
1459 	if (ret < 0) {
1460 		if (ret != -EPROBE_DEFER)
1461 			dev_err(dev, "tplg component load failed: %d\n", ret);
1462 	}
1463 
1464 	return ret;
1465 }
1466 EXPORT_SYMBOL_GPL(audioreach_tplg_init);
1467