1 // SPDX-License-Identifier: BSD-3-Clause
2 //
3 // Copyright(c) 2021 Intel Corporation. All rights reserved.
4 //
5 // Author: Jaska Uimonen <jaska.uimonen@linux.intel.com>
6
7 #include "aconfig.h"
8 #include "intel-nhlt.h"
9
get_int_val(snd_config_t *input, long *int_val, snd_config_t *top)10 static int get_int_val(snd_config_t *input, long *int_val, snd_config_t *top)
11 {
12 char tplg_define[128] = "Define.";
13 snd_config_t *n;
14 const char *s;
15 int ret;
16
17 if (snd_config_get_string(input, &s) < 0)
18 return snd_config_get_integer(input, int_val);
19
20 if (*s != '$')
21 return 0;
22
23 strcat(tplg_define, s + 1);
24
25 ret = snd_config_search(top, tplg_define, &n);
26 if (ret < 0)
27 return ret;
28
29 return snd_config_get_integer(n, int_val);
30 }
31
get_string_val(snd_config_t *input, const char **string_val, snd_config_t *top)32 static int get_string_val(snd_config_t *input, const char **string_val, snd_config_t *top)
33 {
34 char tplg_define[128] = "Define.";
35 snd_config_t *n;
36 int ret;
37
38 if (snd_config_get_string(input, string_val) < 0)
39 return -EINVAL;
40
41 if (**string_val != '$')
42 return 0;
43
44 strcat(tplg_define, *string_val + 1);
45
46 ret = snd_config_search(top, tplg_define, &n);
47 if (ret < 0)
48 return ret;
49
50 return snd_config_get_string(n, string_val);
51 }
52
53 #ifdef NHLT_DEBUG
print_array_values(struct dai_values *values, int size)54 static void print_array_values(struct dai_values *values, int size)
55 {
56 int i;
57
58 fprintf(stdout, "print parsed array:\n");
59 for (i = 0; i < size; i++, values++) {
60 if (values->type == SND_CONFIG_TYPE_INTEGER)
61 fprintf(stdout, "%s %ld\n", values->name, *values->int_val);
62 else
63 fprintf(stdout, "%s %s\n", values->name, *values->string_val);
64 }
65 fprintf(stdout, "\n");
66 }
67 #endif
68
find_set_values(struct dai_values *values, int size, snd_config_t *dai_cfg, snd_config_t *top, const char *class_name)69 int find_set_values(struct dai_values *values, int size, snd_config_t *dai_cfg,
70 snd_config_t *top, const char *class_name)
71 {
72 snd_config_iterator_t i, next;
73 struct dai_values *temp_val;
74 snd_config_t *class_cfg;
75 snd_config_t *n;
76 const char *id;
77 int ret;
78 int j;
79
80 /* get default values from class definition */
81 ret = snd_config_search(top, class_name, &class_cfg);
82 if (ret < 0)
83 return ret;
84
85 snd_config_for_each(i, next, class_cfg) {
86 n = snd_config_iterator_entry(i);
87
88 if (snd_config_get_id(n, &id) < 0)
89 continue;
90
91 for (j = 0, temp_val = values; j < size; j++, temp_val++) {
92 if (!strcmp(id, temp_val->name)) {
93 temp_val->data = n;
94 break;
95 }
96 }
97 }
98
99 /* set instance specific values */
100 snd_config_for_each(i, next, dai_cfg) {
101 n = snd_config_iterator_entry(i);
102
103 if (snd_config_get_id(n, &id) < 0)
104 continue;
105
106 for (j = 0, temp_val = values; j < size; j++, temp_val++) {
107 if (!strcmp(id, temp_val->name)) {
108 temp_val->data = n;
109 break;
110 }
111 }
112 }
113
114 for (j = 0, temp_val = values; j < size; j++, temp_val++) {
115 if (!temp_val->data)
116 continue;
117 if (temp_val->type == SND_CONFIG_TYPE_INTEGER)
118 get_int_val(temp_val->data, temp_val->int_val, top);
119 else
120 get_string_val(temp_val->data, temp_val->string_val, top);
121 }
122
123 #ifdef NHLT_DEBUG
124 print_array_values(values, size);
125 #endif
126 return 0;
127 }
128