summaryrefslogtreecommitdiff
path: root/sound/soc/qcom/qdsp6/topology.c
blob: bd649c232a0611817406fda015ade9e16b14f6d0 (plain)
1
2
3
4
5
6
7
8
9
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
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
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
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
949
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
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
// SPDX-License-Identifier: GPL-2.0
// Copyright (c) 2020, Linaro Limited

#include <sound/soc.h>
#include <sound/soc-dapm.h>
#include <sound/pcm.h>
#include <sound/control.h>
#include <sound/asound.h>
#include <linux/firmware.h>
#include <sound/soc-topology.h>
#include <sound/soc-dpcm.h>
#include <uapi/sound/snd_ar_tokens.h>
#include <linux/kernel.h>
#include <linux/wait.h>
#include "q6apm.h"
#include "audioreach.h"

struct snd_ar_control {
	u32 sgid; /* Sub Graph ID */
	struct snd_soc_component *scomp;
};

static struct audioreach_graph_info *audioreach_tplg_alloc_graph_info(struct q6apm *apm,
								      uint32_t graph_id,
								      bool *found)
{
	struct audioreach_graph_info *info;
	int ret;

	mutex_lock(&apm->lock);
	info = idr_find(&apm->graph_info_idr, graph_id);
	mutex_unlock(&apm->lock);

	if (info) {
		*found = true;
		return info;
	}

	*found = false;
	info = kzalloc(sizeof(*info), GFP_KERNEL);
	if (!info)
		return ERR_PTR(-ENOMEM);

	INIT_LIST_HEAD(&info->sg_list);

	mutex_lock(&apm->lock);
	ret = idr_alloc(&apm->graph_info_idr, info, graph_id, graph_id + 1, GFP_KERNEL);
	mutex_unlock(&apm->lock);

	if (ret < 0) {
		dev_err(apm->dev, "Failed to allocate Graph ID (%x)\n", graph_id);
		kfree(info);
		return ERR_PTR(ret);
	}

	info->id = ret;

	return info;
}

static void audioreach_tplg_add_sub_graph(struct audioreach_sub_graph *sg,
					  struct audioreach_graph_info *info)
{
	list_add_tail(&sg->node, &info->sg_list);
	sg->info = info;
	info->num_sub_graphs++;
}

static struct audioreach_sub_graph *audioreach_tplg_alloc_sub_graph(struct q6apm *apm,
								    uint32_t sub_graph_id,
								    bool *found)
{
	struct audioreach_sub_graph *sg;
	int ret;

	if (!sub_graph_id)
		return ERR_PTR(-EINVAL);

	/* Find if there is already a matching sub-graph */
	mutex_lock(&apm->lock);
	sg = idr_find(&apm->sub_graphs_idr, sub_graph_id);
	mutex_unlock(&apm->lock);

	if (sg) {
		*found = true;
		return sg;
	}

	*found = false;
	sg = kzalloc(sizeof(*sg), GFP_KERNEL);
	if (!sg)
		return ERR_PTR(-ENOMEM);

	INIT_LIST_HEAD(&sg->container_list);

	mutex_lock(&apm->lock);
	ret = idr_alloc(&apm->sub_graphs_idr, sg, sub_graph_id, sub_graph_id + 1, GFP_KERNEL);
	mutex_unlock(&apm->lock);

	if (ret < 0) {
		dev_err(apm->dev, "Failed to allocate Sub-Graph Instance ID (%x)\n", sub_graph_id);
		kfree(sg);
		return ERR_PTR(ret);
	}

	sg->sub_graph_id = ret;

	return sg;
}

static struct audioreach_container *audioreach_tplg_alloc_container(struct q6apm *apm,
							    struct audioreach_sub_graph *sg,
							    uint32_t container_id,
							    bool *found)
{
	struct audioreach_container *cont;
	int ret;

	if (!container_id)
		return ERR_PTR(-EINVAL);

	mutex_lock(&apm->lock);
	cont = idr_find(&apm->containers_idr, container_id);
	mutex_unlock(&apm->lock);

	if (cont) {
		*found = true;
		return cont;
	}
	*found = false;

	cont = kzalloc(sizeof(*cont), GFP_KERNEL);
	if (!cont)
		return ERR_PTR(-ENOMEM);

	INIT_LIST_HEAD(&cont->modules_list);

	mutex_lock(&apm->lock);
	ret = idr_alloc(&apm->containers_idr, cont, container_id, container_id + 1, GFP_KERNEL);
	mutex_unlock(&apm->lock);

	if (ret < 0) {
		dev_err(apm->dev, "Failed to allocate Container Instance ID (%x)\n", container_id);
		kfree(cont);
		return ERR_PTR(ret);
	}

	cont->container_id = ret;
	cont->sub_graph = sg;
	/* add to container list */
	list_add_tail(&cont->node, &sg->container_list);
	sg->num_containers++;

	return cont;
}

static struct audioreach_module *audioreach_tplg_alloc_module(struct q6apm *apm,
							      struct audioreach_container *cont,
							      struct snd_soc_dapm_widget *w,
							      uint32_t module_id, bool *found)
{
	struct audioreach_module *mod;
	int ret;

	mutex_lock(&apm->lock);
	mod = idr_find(&apm->modules_idr, module_id);
	mutex_unlock(&apm->lock);

	if (mod) {
		*found = true;
		return mod;
	}
	*found = false;
	mod = kzalloc(sizeof(*mod), GFP_KERNEL);
	if (!mod)
		return ERR_PTR(-ENOMEM);

	mutex_lock(&apm->lock);
	if (!module_id) { /* alloc module id dynamically */
		ret = idr_alloc_cyclic(&apm->modules_idr, mod,
				       AR_MODULE_DYNAMIC_INSTANCE_ID_START,
				       AR_MODULE_DYNAMIC_INSTANCE_ID_END, GFP_KERNEL);
	} else {
		ret = idr_alloc(&apm->modules_idr, mod, module_id, module_id + 1, GFP_KERNEL);
	}
	mutex_unlock(&apm->lock);

	if (ret < 0) {
		dev_err(apm->dev, "Failed to allocate Module Instance ID (%x)\n", module_id);
		kfree(mod);
		return ERR_PTR(ret);
	}

	mod->instance_id = ret;
	/* add to module list */
	list_add_tail(&mod->node, &cont->modules_list);
	mod->container = cont;
	mod->widget = w;
	cont->num_modules++;

	return mod;
}

static struct snd_soc_tplg_vendor_array *audioreach_get_sg_array(
							struct snd_soc_tplg_private *private)
{
	struct snd_soc_tplg_vendor_array *sg_array = NULL;
	bool found = false;
	int sz;

	for (sz = 0; !found && (sz < le32_to_cpu(private->size)); ) {
		struct snd_soc_tplg_vendor_value_elem *sg_elem;
		int tkn_count = 0;

		sg_array = (struct snd_soc_tplg_vendor_array *)((u8 *)private->array + sz);
		sg_elem = sg_array->value;
		sz = sz + le32_to_cpu(sg_array->size);
		while (!found && tkn_count <= (le32_to_cpu(sg_array->num_elems) - 1)) {
			switch (le32_to_cpu(sg_elem->token)) {
			case AR_TKN_U32_SUB_GRAPH_INSTANCE_ID:
				found = true;
				break;
			default:
				break;
			}
			tkn_count++;
			sg_elem++;
		}
	}

	if (found)
		return sg_array;

	return NULL;
}

static struct snd_soc_tplg_vendor_array *audioreach_get_cont_array(
							struct snd_soc_tplg_private *private)
{
	struct snd_soc_tplg_vendor_array *cont_array = NULL;
	bool found = false;
	int sz;

	for (sz = 0; !found && (sz < le32_to_cpu(private->size)); ) {
		struct snd_soc_tplg_vendor_value_elem *cont_elem;
		int tkn_count = 0;

		cont_array = (struct snd_soc_tplg_vendor_array *)((u8 *)private->array + sz);
		cont_elem = cont_array->value;
		sz = sz + le32_to_cpu(cont_array->size);
		while (!found && tkn_count <= (le32_to_cpu(cont_array->num_elems) - 1)) {
			switch (le32_to_cpu(cont_elem->token)) {
			case AR_TKN_U32_CONTAINER_INSTANCE_ID:
				found = true;
				break;
			default:
				break;
			}
			tkn_count++;
			cont_elem++;
		}
	}

	if (found)
		return cont_array;

	return NULL;
}

static struct snd_soc_tplg_vendor_array *audioreach_get_module_array(
							     struct snd_soc_tplg_private *private)
{
	struct snd_soc_tplg_vendor_array *mod_array = NULL;
	bool found = false;
	int sz = 0;

	for (sz = 0; !found && (sz < le32_to_cpu(private->size)); ) {
		struct snd_soc_tplg_vendor_value_elem *mod_elem;
		int tkn_count = 0;

		mod_array = (struct snd_soc_tplg_vendor_array *)((u8 *)private->array + sz);
		mod_elem = mod_array->value;
		sz = sz + le32_to_cpu(mod_array->size);
		while (!found && tkn_count <= (le32_to_cpu(mod_array->num_elems) - 1)) {
			switch (le32_to_cpu(mod_elem->token)) {
			case AR_TKN_U32_MODULE_INSTANCE_ID:
				found = true;
				break;
			default:
				break;
			}
			tkn_count++;
			mod_elem++;
		}
	}

	if (found)
		return mod_array;

	return NULL;
}

static struct audioreach_sub_graph *audioreach_parse_sg_tokens(struct q6apm *apm,
						       struct snd_soc_tplg_private *private)
{
	struct snd_soc_tplg_vendor_value_elem *sg_elem;
	struct snd_soc_tplg_vendor_array *sg_array;
	struct audioreach_graph_info *info = NULL;
	int graph_id, sub_graph_id, tkn_count = 0;
	struct audioreach_sub_graph *sg;
	bool found;

	sg_array = audioreach_get_sg_array(private);
	sg_elem = sg_array->value;

	while (tkn_count <= (le32_to_cpu(sg_array->num_elems) - 1)) {
		switch (le32_to_cpu(sg_elem->token)) {
		case AR_TKN_U32_SUB_GRAPH_INSTANCE_ID:
			sub_graph_id = le32_to_cpu(sg_elem->value);
			sg = audioreach_tplg_alloc_sub_graph(apm, sub_graph_id, &found);
			if (IS_ERR(sg)) {
				return sg;
			} else if (found) {
				/* Already parsed data for this sub-graph */
				return sg;
			}
			break;
		case AR_TKN_DAI_INDEX:
			/* Sub graph is associated with predefined graph */
			graph_id = le32_to_cpu(sg_elem->value);
			info = audioreach_tplg_alloc_graph_info(apm, graph_id, &found);
			if (IS_ERR(info))
				return ERR_CAST(info);
			break;
		case AR_TKN_U32_SUB_GRAPH_PERF_MODE:
			sg->perf_mode = le32_to_cpu(sg_elem->value);
			break;
		case AR_TKN_U32_SUB_GRAPH_DIRECTION:
			sg->direction = le32_to_cpu(sg_elem->value);
			break;
		case AR_TKN_U32_SUB_GRAPH_SCENARIO_ID:
			sg->scenario_id = le32_to_cpu(sg_elem->value);
			break;
		default:
			dev_err(apm->dev, "Not a valid token %d for graph\n", sg_elem->token);
			break;

		}
		tkn_count++;
		sg_elem++;
	}

	/* Sub graph is associated with predefined graph */
	if (info)
		audioreach_tplg_add_sub_graph(sg, info);

	return sg;
}

static struct audioreach_container *audioreach_parse_cont_tokens(struct q6apm *apm,
							 struct audioreach_sub_graph *sg,
							 struct snd_soc_tplg_private *private)
{
	struct snd_soc_tplg_vendor_value_elem *cont_elem;
	struct snd_soc_tplg_vendor_array *cont_array;
	struct audioreach_container *cont;
	int container_id, tkn_count = 0;
	bool found = false;

	cont_array = audioreach_get_cont_array(private);
	cont_elem = cont_array->value;

	while (tkn_count <= (le32_to_cpu(cont_array->num_elems) - 1)) {
		switch (le32_to_cpu(cont_elem->token)) {
		case AR_TKN_U32_CONTAINER_INSTANCE_ID:
			container_id = le32_to_cpu(cont_elem->value);
			cont = audioreach_tplg_alloc_container(apm, sg, container_id, &found);
			if (IS_ERR(cont) || found)/* Error or Already parsed container data */
				return cont;
			break;
		case AR_TKN_U32_CONTAINER_CAPABILITY_ID:
			cont->capability_id = le32_to_cpu(cont_elem->value);
			break;
		case AR_TKN_U32_CONTAINER_STACK_SIZE:
			cont->stack_size = le32_to_cpu(cont_elem->value);
			break;
		case AR_TKN_U32_CONTAINER_GRAPH_POS:
			cont->graph_pos = le32_to_cpu(cont_elem->value);
			break;
		case AR_TKN_U32_CONTAINER_PROC_DOMAIN:
			cont->proc_domain = le32_to_cpu(cont_elem->value);
			break;
		default:
			dev_err(apm->dev, "Not a valid token %d for graph\n", cont_elem->token);
			break;

		}
		tkn_count++;
		cont_elem++;
	}

	return cont;
}

static struct audioreach_module *audioreach_parse_common_tokens(struct q6apm *apm,
							struct audioreach_container *cont,
							struct snd_soc_tplg_private *private,
							struct snd_soc_dapm_widget *w)
{
	uint32_t max_ip_port = 0, max_op_port = 0, in_port = 0, out_port = 0;
	uint32_t src_mod_inst_id = 0, src_mod_op_port_id = 0;
	uint32_t dst_mod_inst_id = 0, dst_mod_ip_port_id = 0;
	int module_id = 0, instance_id = 0, tkn_count = 0;
	struct snd_soc_tplg_vendor_value_elem *mod_elem;
	struct snd_soc_tplg_vendor_array *mod_array;
	struct audioreach_module *mod = NULL;
	bool found;

	mod_array = audioreach_get_module_array(private);
	mod_elem = mod_array->value;

	while (tkn_count <= (le32_to_cpu(mod_array->num_elems) - 1)) {
		switch (le32_to_cpu(mod_elem->token)) {
		/* common module info */
		case AR_TKN_U32_MODULE_ID:
			module_id = le32_to_cpu(mod_elem->value);
			break;
		case AR_TKN_U32_MODULE_INSTANCE_ID:
			instance_id = le32_to_cpu(mod_elem->value);
			mod = audioreach_tplg_alloc_module(apm, cont, w,
							   instance_id, &found);
			if (IS_ERR(mod)) {
				return mod;
			} else if (found) {
				dev_err(apm->dev, "Duplicate Module Instance ID 0x%08x found\n",
					instance_id);
				return ERR_PTR(-EINVAL);
			}

			break;
		case AR_TKN_U32_MODULE_MAX_IP_PORTS:
			max_ip_port = le32_to_cpu(mod_elem->value);
			break;
		case AR_TKN_U32_MODULE_MAX_OP_PORTS:
			max_op_port = le32_to_cpu(mod_elem->value);
			break;
		case AR_TKN_U32_MODULE_IN_PORTS:
			in_port = le32_to_cpu(mod_elem->value);
			break;
		case AR_TKN_U32_MODULE_OUT_PORTS:
			out_port = le32_to_cpu(mod_elem->value);
			break;
		case AR_TKN_U32_MODULE_SRC_OP_PORT_ID:
			src_mod_op_port_id = le32_to_cpu(mod_elem->value);
			break;
		case AR_TKN_U32_MODULE_SRC_INSTANCE_ID:
			src_mod_inst_id = le32_to_cpu(mod_elem->value);
			break;
		case AR_TKN_U32_MODULE_DST_INSTANCE_ID:
			dst_mod_inst_id = le32_to_cpu(mod_elem->value);
			break;
		case AR_TKN_U32_MODULE_DST_IN_PORT_ID:
			dst_mod_ip_port_id = le32_to_cpu(mod_elem->value);
			break;
		default:
			break;

		}
		tkn_count++;
		mod_elem++;
	}

	if (mod) {
		mod->module_id = module_id;
		mod->max_ip_port = max_ip_port;
		mod->max_op_port = max_op_port;
		mod->in_port = in_port;
		mod->out_port = out_port;
		mod->src_mod_inst_id = src_mod_inst_id;
		mod->src_mod_op_port_id = src_mod_op_port_id;
		mod->dst_mod_inst_id = dst_mod_inst_id;
		mod->dst_mod_ip_port_id = dst_mod_ip_port_id;
	}

	return mod;
}

static int audioreach_widget_load_module_common(struct snd_soc_component *component,
						int index, struct snd_soc_dapm_widget *w,
						struct snd_soc_tplg_dapm_widget *tplg_w)
{
	struct q6apm *apm = dev_get_drvdata(component->dev);
	struct audioreach_container *cont;
	struct audioreach_sub_graph *sg;
	struct audioreach_module *mod;
	struct snd_soc_dobj *dobj;

	sg = audioreach_parse_sg_tokens(apm, &tplg_w->priv);
	if (IS_ERR(sg))
		return PTR_ERR(sg);

	cont = audioreach_parse_cont_tokens(apm, sg, &tplg_w->priv);
	if (IS_ERR(cont))
		return PTR_ERR(cont);

	mod = audioreach_parse_common_tokens(apm, cont, &tplg_w->priv, w);
	if (IS_ERR(mod))
		return PTR_ERR(mod);

	dobj = &w->dobj;
	dobj->private = mod;

	return 0;
}

static int audioreach_widget_load_enc_dec_cnv(struct snd_soc_component *component,
					      int index, struct snd_soc_dapm_widget *w,
					      struct snd_soc_tplg_dapm_widget *tplg_w)
{
	struct snd_soc_tplg_vendor_value_elem *mod_elem;
	struct snd_soc_tplg_vendor_array *mod_array;
	struct audioreach_module *mod;
	struct snd_soc_dobj *dobj;
	int tkn_count = 0;
	int ret;

	ret = audioreach_widget_load_module_common(component, index, w, tplg_w);
	if (ret)
		return ret;

	dobj = &w->dobj;
	mod = dobj->private;
	mod_array = audioreach_get_module_array(&tplg_w->priv);
	mod_elem = mod_array->value;

	while (tkn_count <= (le32_to_cpu(mod_array->num_elems) - 1)) {
		switch (le32_to_cpu(mod_elem->token)) {
		case AR_TKN_U32_MODULE_FMT_INTERLEAVE:
			mod->interleave_type = le32_to_cpu(mod_elem->value);
			break;
		case AR_TKN_U32_MODULE_FMT_SAMPLE_RATE:
			mod->rate = le32_to_cpu(mod_elem->value);
			break;
		case AR_TKN_U32_MODULE_FMT_BIT_DEPTH:
			mod->bit_depth = le32_to_cpu(mod_elem->value);
			break;
		default:
			break;
		}
		tkn_count++;
		mod_elem++;
	}

	return 0;
}

static int audioreach_widget_log_module_load(struct audioreach_module *mod,
					     struct snd_soc_tplg_vendor_array *mod_array)
{
	struct snd_soc_tplg_vendor_value_elem *mod_elem;
	int tkn_count = 0;

	mod_elem = mod_array->value;

	while (tkn_count <= (le32_to_cpu(mod_array->num_elems) - 1)) {
		switch (le32_to_cpu(mod_elem->token)) {

		case AR_TKN_U32_MODULE_LOG_CODE:
			mod->log_code = le32_to_cpu(mod_elem->value);
			break;
		case AR_TKN_U32_MODULE_LOG_TAP_POINT_ID:
			mod->log_tap_point_id = le32_to_cpu(mod_elem->value);
			break;
		case AR_TKN_U32_MODULE_LOG_MODE:
			mod->log_mode = le32_to_cpu(mod_elem->value);
			break;
		default:
			break;
		}
		tkn_count++;
		mod_elem++;
	}

	return 0;
}

static int audioreach_widget_dma_module_load(struct audioreach_module *mod,
					     struct snd_soc_tplg_vendor_array *mod_array)
{
	struct snd_soc_tplg_vendor_value_elem *mod_elem;
	int tkn_count = 0;

	mod_elem = mod_array->value;

	while (tkn_count <= (le32_to_cpu(mod_array->num_elems) - 1)) {
		switch (le32_to_cpu(mod_elem->token)) {
		case AR_TKN_U32_MODULE_HW_IF_IDX:
			mod->hw_interface_idx = le32_to_cpu(mod_elem->value);
			break;
		case AR_TKN_U32_MODULE_FMT_DATA:
			mod->data_format = le32_to_cpu(mod_elem->value);
			break;
		case AR_TKN_U32_MODULE_HW_IF_TYPE:
			mod->hw_interface_type = le32_to_cpu(mod_elem->value);
			break;
		default:
			break;
		}
		tkn_count++;
		mod_elem++;
	}

	return 0;
}

static int audioreach_widget_i2s_module_load(struct audioreach_module *mod,
					     struct snd_soc_tplg_vendor_array *mod_array)
{
	struct snd_soc_tplg_vendor_value_elem *mod_elem;
	int tkn_count = 0;

	mod_elem = mod_array->value;

	while (tkn_count <= (le32_to_cpu(mod_array->num_elems) - 1)) {
		switch (le32_to_cpu(mod_elem->token)) {
		case AR_TKN_U32_MODULE_HW_IF_IDX:
			mod->hw_interface_idx = le32_to_cpu(mod_elem->value);
			break;
		case AR_TKN_U32_MODULE_FMT_DATA:
			mod->data_format = le32_to_cpu(mod_elem->value);
			break;
		case AR_TKN_U32_MODULE_HW_IF_TYPE:
			mod->hw_interface_type = le32_to_cpu(mod_elem->value);
			break;
		case AR_TKN_U32_MODULE_SD_LINE_IDX:
			mod->sd_line_idx = le32_to_cpu(mod_elem->value);
			break;
		case AR_TKN_U32_MODULE_WS_SRC:
			mod->ws_src = le32_to_cpu(mod_elem->value);
			break;
		default:
			break;
		}
		tkn_count++;
		mod_elem++;
	}

	return 0;
}

static int audioreach_widget_load_buffer(struct snd_soc_component *component,
					 int index, struct snd_soc_dapm_widget *w,
					 struct snd_soc_tplg_dapm_widget *tplg_w)
{
	struct snd_soc_tplg_vendor_array *mod_array;
	struct audioreach_module *mod;
	struct snd_soc_dobj *dobj;
	int ret;

	ret = audioreach_widget_load_module_common(component, index, w, tplg_w);
	if (ret)
		return ret;

	dobj = &w->dobj;
	mod = dobj->private;

	mod_array = audioreach_get_module_array(&tplg_w->priv);

	switch (mod->module_id) {
	case MODULE_ID_CODEC_DMA_SINK:
	case MODULE_ID_CODEC_DMA_SOURCE:
		audioreach_widget_dma_module_load(mod, mod_array);
		break;
	case MODULE_ID_DATA_LOGGING:
		audioreach_widget_log_module_load(mod, mod_array);
		break;
	case MODULE_ID_I2S_SINK:
	case MODULE_ID_I2S_SOURCE:
		audioreach_widget_i2s_module_load(mod, mod_array);
		break;
	default:
		return -EINVAL;
	}

	return 0;
}

static int audioreach_widget_load_mixer(struct snd_soc_component *component,
					int index, struct snd_soc_dapm_widget *w,
					struct snd_soc_tplg_dapm_widget *tplg_w)
{
	struct snd_soc_tplg_vendor_value_elem *w_elem;
	struct snd_soc_tplg_vendor_array *w_array;
	struct snd_ar_control *scontrol;
	struct snd_soc_dobj *dobj;
	int tkn_count = 0;

	w_array = &tplg_w->priv.array[0];

	scontrol = kzalloc(sizeof(*scontrol), GFP_KERNEL);
	if (!scontrol)
		return -ENOMEM;

	scontrol->scomp = component;
	dobj = &w->dobj;
	dobj->private = scontrol;

	w_elem = w_array->value;
	while (tkn_count <= (le32_to_cpu(w_array->num_elems) - 1)) {
		switch (le32_to_cpu(w_elem->token)) {
		case AR_TKN_U32_SUB_GRAPH_INSTANCE_ID:
			scontrol->sgid = le32_to_cpu(w_elem->value);
			break;
		default: /* ignore other tokens */
			break;
		}
		tkn_count++;
		w_elem++;
	}

	return 0;
}

static int audioreach_pga_event(struct snd_soc_dapm_widget *w,
				struct snd_kcontrol *kcontrol, int event)

{
	struct snd_soc_dapm_context *dapm = w->dapm;
	struct snd_soc_component *c = snd_soc_dapm_to_component(dapm);
	struct audioreach_module *mod = w->dobj.private;
	struct q6apm *apm = dev_get_drvdata(c->dev);

	switch (event) {
	case SND_SOC_DAPM_POST_PMU:
		/* apply gain after power up of widget */
		audioreach_gain_set_vol_ctrl(apm, mod, mod->gain);
		break;
	default:
		break;
	}

	return 0;
}

static const struct snd_soc_tplg_widget_events audioreach_widget_ops[] = {
	{ AR_PGA_DAPM_EVENT, audioreach_pga_event },
};

static int audioreach_widget_load_pga(struct snd_soc_component *component,
				      int index, struct snd_soc_dapm_widget *w,
				      struct snd_soc_tplg_dapm_widget *tplg_w)
{
	struct audioreach_module *mod;
	struct snd_soc_dobj *dobj;
	int ret;

	ret = audioreach_widget_load_module_common(component, index, w, tplg_w);
	if (ret)
		return ret;

	dobj = &w->dobj;
	mod = dobj->private;
	mod->gain = VOL_CTRL_DEFAULT_GAIN;

	ret = snd_soc_tplg_widget_bind_event(w, audioreach_widget_ops,
					     ARRAY_SIZE(audioreach_widget_ops),
					     le16_to_cpu(tplg_w->event_type));
	if (ret) {
		dev_err(component->dev, "matching event handlers NOT found for %d\n",
			le16_to_cpu(tplg_w->event_type));
		return -EINVAL;
	}

	return 0;
}

static int audioreach_widget_ready(struct snd_soc_component *component,
				   int index, struct snd_soc_dapm_widget *w,
				   struct snd_soc_tplg_dapm_widget *tplg_w)
{
	switch (w->id) {
	case snd_soc_dapm_aif_in:
	case snd_soc_dapm_aif_out:
		audioreach_widget_load_buffer(component, index, w, tplg_w);
		break;
	case snd_soc_dapm_decoder:
	case snd_soc_dapm_encoder:
	case snd_soc_dapm_src:
		audioreach_widget_load_enc_dec_cnv(component, index, w, tplg_w);
		break;
	case snd_soc_dapm_buffer:
		audioreach_widget_load_buffer(component, index, w, tplg_w);
		break;
	case snd_soc_dapm_mixer:
		return audioreach_widget_load_mixer(component, index, w, tplg_w);
	case snd_soc_dapm_pga:
		return audioreach_widget_load_pga(component, index, w, tplg_w);
	case snd_soc_dapm_dai_link:
	case snd_soc_dapm_scheduler:
	case snd_soc_dapm_out_drv:
	default:
		dev_err(component->dev, "Widget type (0x%x) not yet supported\n", w->id);
		break;
	}

	return 0;
}

static int audioreach_widget_unload(struct snd_soc_component *scomp,
				    struct snd_soc_dobj *dobj)
{
	struct snd_soc_dapm_widget *w = container_of(dobj, struct snd_soc_dapm_widget, dobj);
	struct q6apm *apm = dev_get_drvdata(scomp->dev);
	struct audioreach_container *cont;
	struct audioreach_module *mod;

	mod = dobj->private;
	cont = mod->container;

	if (w->id == snd_soc_dapm_mixer) {
		/* virtual widget */
		kfree(dobj->private);
		return 0;
	}

	mutex_lock(&apm->lock);
	idr_remove(&apm->modules_idr, mod->instance_id);
	cont->num_modules--;

	list_del(&mod->node);
	kfree(mod);
	/* Graph Info has N sub-graphs, sub-graph has N containers, Container has N Modules */
	if (list_empty(&cont->modules_list)) { /* if no modules in the container then remove it */
		struct audioreach_sub_graph *sg = cont->sub_graph;

		idr_remove(&apm->containers_idr, cont->container_id);
		list_del(&cont->node);
		sg->num_containers--;
		kfree(cont);
		/* check if there are no more containers in the sub graph and remove it */
		if (list_empty(&sg->container_list)) {
			struct audioreach_graph_info *info = sg->info;

			idr_remove(&apm->sub_graphs_idr, sg->sub_graph_id);
			list_del(&sg->node);
			info->num_sub_graphs--;
			kfree(sg);
			/* Check if there are no more sub-graphs left then remove graph info */
			if (list_empty(&info->sg_list)) {
				idr_remove(&apm->graph_info_idr, info->id);
				kfree(info);
			}
		}
	}

	mutex_unlock(&apm->lock);

	return 0;
}

static struct audioreach_module *audioreach_find_widget(struct snd_soc_component *comp,
							const char *name)
{
	struct q6apm *apm = dev_get_drvdata(comp->dev);
	struct audioreach_module *module;
	int id;

	idr_for_each_entry(&apm->modules_idr, module, id) {
		if (!strcmp(name, module->widget->name))
			return module;
	}

	return NULL;
}

static int audioreach_route_load(struct snd_soc_component *scomp, int index,
				 struct snd_soc_dapm_route *route)
{
	struct audioreach_module *src, *sink;

	src = audioreach_find_widget(scomp, route->source);
	sink = audioreach_find_widget(scomp, route->sink);

	if (src && sink) {
		src->dst_mod_inst_id = sink->instance_id;
		sink->src_mod_inst_id = src->instance_id;
	}

	return 0;
}

static int audioreach_route_unload(struct snd_soc_component *scomp,
				   struct snd_soc_dobj *dobj)
{
	return 0;
}

static int audioreach_tplg_complete(struct snd_soc_component *component)
{
	/* TBD */
	return 0;
}

/* DAI link - used for any driver specific init */
static int audioreach_link_load(struct snd_soc_component *component, int index,
				struct snd_soc_dai_link *link,
				struct snd_soc_tplg_link_config *cfg)
{
	link->nonatomic = true;
	link->dynamic = true;
	link->platforms->name = NULL;
	link->platforms->of_node = of_get_compatible_child(component->dev->of_node,
							   "qcom,q6apm-dais");
	return 0;
}

static int audioreach_get_audio_mixer(struct snd_kcontrol *kcontrol,
				      struct snd_ctl_elem_value *ucontrol)
{
	struct soc_mixer_control *mc = (struct soc_mixer_control *)kcontrol->private_value;
	struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
	struct snd_soc_dapm_widget *dw = snd_soc_dapm_kcontrol_widget(kcontrol);
	struct snd_soc_component *c = snd_soc_dapm_to_component(dapm);
	struct snd_ar_control *dapm_scontrol = dw->dobj.private;
	struct snd_ar_control *scontrol = mc->dobj.private;
	struct q6apm *data = dev_get_drvdata(c->dev);
	bool connected;

	connected = q6apm_is_sub_graphs_connected(data, scontrol->sgid, dapm_scontrol->sgid);
	if (connected)
		ucontrol->value.integer.value[0] = 1;
	else
		ucontrol->value.integer.value[0] = 0;

	return 0;
}

static int audioreach_put_audio_mixer(struct snd_kcontrol *kcontrol,
				      struct snd_ctl_elem_value *ucontrol)
{
	struct soc_mixer_control *mc = (struct soc_mixer_control *)kcontrol->private_value;
	struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
	struct snd_soc_dapm_widget *dw = snd_soc_dapm_kcontrol_widget(kcontrol);
	struct snd_soc_component *c = snd_soc_dapm_to_component(dapm);
	struct snd_ar_control *dapm_scontrol = dw->dobj.private;
	struct snd_ar_control *scontrol = mc->dobj.private;
	struct q6apm *data = dev_get_drvdata(c->dev);

	if (ucontrol->value.integer.value[0]) {
		q6apm_connect_sub_graphs(data, scontrol->sgid, dapm_scontrol->sgid, true);
		snd_soc_dapm_mixer_update_power(dapm, kcontrol, 1, NULL);
	} else {
		q6apm_connect_sub_graphs(data, scontrol->sgid, dapm_scontrol->sgid, false);
		snd_soc_dapm_mixer_update_power(dapm, kcontrol, 0, NULL);
	}
	return 0;
}

static int audioreach_get_vol_ctrl_audio_mixer(struct snd_kcontrol *kcontrol,
					       struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_dapm_widget *dw = snd_soc_dapm_kcontrol_widget(kcontrol);
	struct audioreach_module *mod = dw->dobj.private;

	ucontrol->value.integer.value[0] = mod->gain;

	return 0;
}

static int audioreach_put_vol_ctrl_audio_mixer(struct snd_kcontrol *kcontrol,
					       struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_dapm_widget *dw = snd_soc_dapm_kcontrol_widget(kcontrol);
	struct audioreach_module *mod = dw->dobj.private;

	mod->gain = ucontrol->value.integer.value[0];

	return 1;
}

static int audioreach_control_load_mix(struct snd_soc_component *scomp,
				       struct snd_ar_control *scontrol,
				       struct snd_kcontrol_new *kc,
				       struct snd_soc_tplg_ctl_hdr *hdr)
{
	struct snd_soc_tplg_vendor_value_elem *c_elem;
	struct snd_soc_tplg_vendor_array *c_array;
	struct snd_soc_tplg_mixer_control *mc;
	int tkn_count = 0;

	mc = container_of(hdr, struct snd_soc_tplg_mixer_control, hdr);
	c_array = (struct snd_soc_tplg_vendor_array *)mc->priv.data;

	c_elem = c_array->value;

	while (tkn_count <= (le32_to_cpu(c_array->num_elems) - 1)) {
		switch (le32_to_cpu(c_elem->token)) {
		case AR_TKN_U32_SUB_GRAPH_INSTANCE_ID:
			scontrol->sgid = le32_to_cpu(c_elem->value);
			break;
		default:
			/* Ignore other tokens */
			break;
		}
		c_elem++;
		tkn_count++;
	}

	return 0;
}

static int audioreach_control_load(struct snd_soc_component *scomp, int index,
				   struct snd_kcontrol_new *kc,
				   struct snd_soc_tplg_ctl_hdr *hdr)
{
	struct snd_ar_control *scontrol;
	struct soc_mixer_control *sm;
	struct snd_soc_dobj *dobj;
	int ret = 0;

	scontrol = kzalloc(sizeof(*scontrol), GFP_KERNEL);
	if (!scontrol)
		return -ENOMEM;

	scontrol->scomp = scomp;

	switch (le32_to_cpu(hdr->ops.get)) {
	case SND_SOC_AR_TPLG_FE_BE_GRAPH_CTL_MIX:
		sm = (struct soc_mixer_control *)kc->private_value;
		dobj = &sm->dobj;
		ret = audioreach_control_load_mix(scomp, scontrol, kc, hdr);
		break;
	case SND_SOC_AR_TPLG_VOL_CTL:
		sm = (struct soc_mixer_control *)kc->private_value;
		dobj = &sm->dobj;
		break;
	default:
		dev_warn(scomp->dev, "control type not supported %d:%d:%d\n",
			 hdr->ops.get, hdr->ops.put, hdr->ops.info);
		kfree(scontrol);
		return -EINVAL;
	}

	dobj->private = scontrol;
	return ret;
}

static int audioreach_control_unload(struct snd_soc_component *scomp,
				     struct snd_soc_dobj *dobj)
{
	struct snd_ar_control *scontrol = dobj->private;

	kfree(scontrol);

	return 0;
}

static const struct snd_soc_tplg_kcontrol_ops audioreach_io_ops[] = {
	{SND_SOC_AR_TPLG_FE_BE_GRAPH_CTL_MIX, audioreach_get_audio_mixer,
		audioreach_put_audio_mixer, snd_soc_info_volsw},
	{SND_SOC_AR_TPLG_VOL_CTL, audioreach_get_vol_ctrl_audio_mixer,
		audioreach_put_vol_ctrl_audio_mixer, snd_soc_info_volsw},
};

static struct snd_soc_tplg_ops audioreach_tplg_ops  = {
	.io_ops = audioreach_io_ops,
	.io_ops_count = ARRAY_SIZE(audioreach_io_ops),

	.control_load	= audioreach_control_load,
	.control_unload	= audioreach_control_unload,

	.widget_ready = audioreach_widget_ready,
	.widget_unload = audioreach_widget_unload,

	.complete = audioreach_tplg_complete,
	.link_load = audioreach_link_load,

	.dapm_route_load	= audioreach_route_load,
	.dapm_route_unload	= audioreach_route_unload,
};

int audioreach_tplg_init(struct snd_soc_component *component)
{
	struct snd_soc_card *card = component->card;
	struct device *dev = component->dev;
	const struct firmware *fw;
	char *tplg_fw_name;
	int ret;

	/* Inline with Qualcomm UCM configs and linux-firmware path */
	tplg_fw_name = kasprintf(GFP_KERNEL, "qcom/%s/%s-tplg.bin", card->driver_name, card->name);
	if (!tplg_fw_name)
		return -ENOMEM;

	ret = request_firmware(&fw, tplg_fw_name, dev);
	if (ret < 0) {
		dev_err(dev, "tplg firmware loading %s failed %d \n", tplg_fw_name, ret);
		goto err;
	}

	ret = snd_soc_tplg_component_load(component, &audioreach_tplg_ops, fw);
	if (ret < 0) {
		dev_err(dev, "tplg component load failed%d\n", ret);
		ret = -EINVAL;
	}

	release_firmware(fw);
err:
	kfree(tplg_fw_name);

	return ret;
}
EXPORT_SYMBOL_GPL(audioreach_tplg_init);