1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
2 //
3 // This file is provided under a dual BSD/GPLv2 license. When using or
4 // redistributing this file, you may do so under either license.
5 //
6 // Copyright(c) 2022 Intel Corporation. All rights reserved.
7 //
8 //
9
10 #include "sof-priv.h"
11 #include "sof-audio.h"
12 #include "ipc4-priv.h"
13 #include "ipc4-topology.h"
14
sof_ipc4_set_get_kcontrol_data(struct snd_sof_control * scontrol,bool set)15 static int sof_ipc4_set_get_kcontrol_data(struct snd_sof_control *scontrol, bool set)
16 {
17 struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
18 struct snd_soc_component *scomp = scontrol->scomp;
19 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
20 const struct sof_ipc_ops *iops = sdev->ipc->ops;
21 struct sof_ipc4_msg *msg = &cdata->msg;
22 struct snd_sof_widget *swidget;
23 bool widget_found = false;
24
25 /* find widget associated with the control */
26 list_for_each_entry(swidget, &sdev->widget_list, list) {
27 if (swidget->comp_id == scontrol->comp_id) {
28 widget_found = true;
29 break;
30 }
31 }
32
33 if (!widget_found) {
34 dev_err(scomp->dev, "Failed to find widget for kcontrol %s\n", scontrol->name);
35 return -ENOENT;
36 }
37
38 /*
39 * Volatile controls should always be part of static pipelines and the widget use_count
40 * would always be > 0 in this case. For the others, just return the cached value if the
41 * widget is not set up.
42 */
43 if (!swidget->use_count)
44 return 0;
45
46 msg->primary &= ~SOF_IPC4_MOD_INSTANCE_MASK;
47 msg->primary |= SOF_IPC4_MOD_INSTANCE(swidget->instance_id);
48
49 return iops->set_get_data(sdev, msg, msg->data_size, set);
50 }
51
52 static int
sof_ipc4_set_volume_data(struct snd_sof_dev * sdev,struct snd_sof_widget * swidget,struct snd_sof_control * scontrol)53 sof_ipc4_set_volume_data(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget,
54 struct snd_sof_control *scontrol)
55 {
56 struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
57 struct sof_ipc4_gain *gain = swidget->private;
58 struct sof_ipc4_msg *msg = &cdata->msg;
59 struct sof_ipc4_gain_data data;
60 bool all_channels_equal = true;
61 u32 value;
62 int ret, i;
63
64 /* check if all channel values are equal */
65 value = cdata->chanv[0].value;
66 for (i = 1; i < scontrol->num_channels; i++) {
67 if (cdata->chanv[i].value != value) {
68 all_channels_equal = false;
69 break;
70 }
71 }
72
73 /*
74 * notify DSP with a single IPC message if all channel values are equal. Otherwise send
75 * a separate IPC for each channel.
76 */
77 for (i = 0; i < scontrol->num_channels; i++) {
78 if (all_channels_equal) {
79 data.channels = SOF_IPC4_GAIN_ALL_CHANNELS_MASK;
80 data.init_val = cdata->chanv[0].value;
81 } else {
82 data.channels = cdata->chanv[i].channel;
83 data.init_val = cdata->chanv[i].value;
84 }
85
86 /* set curve type and duration from topology */
87 data.curve_duration_l = gain->data.curve_duration_l;
88 data.curve_duration_h = gain->data.curve_duration_h;
89 data.curve_type = gain->data.curve_type;
90
91 msg->data_ptr = &data;
92 msg->data_size = sizeof(data);
93
94 ret = sof_ipc4_set_get_kcontrol_data(scontrol, true);
95 msg->data_ptr = NULL;
96 msg->data_size = 0;
97 if (ret < 0) {
98 dev_err(sdev->dev, "Failed to set volume update for %s\n",
99 scontrol->name);
100 return ret;
101 }
102
103 if (all_channels_equal)
104 break;
105 }
106
107 return 0;
108 }
109
sof_ipc4_volume_put(struct snd_sof_control * scontrol,struct snd_ctl_elem_value * ucontrol)110 static bool sof_ipc4_volume_put(struct snd_sof_control *scontrol,
111 struct snd_ctl_elem_value *ucontrol)
112 {
113 struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
114 struct snd_soc_component *scomp = scontrol->scomp;
115 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
116 unsigned int channels = scontrol->num_channels;
117 struct snd_sof_widget *swidget;
118 bool widget_found = false;
119 bool change = false;
120 unsigned int i;
121 int ret;
122
123 /* update each channel */
124 for (i = 0; i < channels; i++) {
125 u32 value = mixer_to_ipc(ucontrol->value.integer.value[i],
126 scontrol->volume_table, scontrol->max + 1);
127
128 change = change || (value != cdata->chanv[i].value);
129 cdata->chanv[i].channel = i;
130 cdata->chanv[i].value = value;
131 }
132
133 if (!pm_runtime_active(scomp->dev))
134 return change;
135
136 /* find widget associated with the control */
137 list_for_each_entry(swidget, &sdev->widget_list, list) {
138 if (swidget->comp_id == scontrol->comp_id) {
139 widget_found = true;
140 break;
141 }
142 }
143
144 if (!widget_found) {
145 dev_err(scomp->dev, "Failed to find widget for kcontrol %s\n", scontrol->name);
146 return false;
147 }
148
149 ret = sof_ipc4_set_volume_data(sdev, swidget, scontrol);
150 if (ret < 0)
151 return false;
152
153 return change;
154 }
155
sof_ipc4_volume_get(struct snd_sof_control * scontrol,struct snd_ctl_elem_value * ucontrol)156 static int sof_ipc4_volume_get(struct snd_sof_control *scontrol,
157 struct snd_ctl_elem_value *ucontrol)
158 {
159 struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
160 unsigned int channels = scontrol->num_channels;
161 unsigned int i;
162
163 for (i = 0; i < channels; i++)
164 ucontrol->value.integer.value[i] = ipc_to_mixer(cdata->chanv[i].value,
165 scontrol->volume_table,
166 scontrol->max + 1);
167
168 return 0;
169 }
170
171 /* set up all controls for the widget */
sof_ipc4_widget_kcontrol_setup(struct snd_sof_dev * sdev,struct snd_sof_widget * swidget)172 static int sof_ipc4_widget_kcontrol_setup(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget)
173 {
174 struct snd_sof_control *scontrol;
175 int ret;
176
177 list_for_each_entry(scontrol, &sdev->kcontrol_list, list)
178 if (scontrol->comp_id == swidget->comp_id) {
179 ret = sof_ipc4_set_volume_data(sdev, swidget, scontrol);
180 if (ret < 0) {
181 dev_err(sdev->dev, "%s: kcontrol %d set up failed for widget %s\n",
182 __func__, scontrol->comp_id, swidget->widget->name);
183 return ret;
184 }
185 }
186
187 return 0;
188 }
189
190 static int
sof_ipc4_set_up_volume_table(struct snd_sof_control * scontrol,int tlv[SOF_TLV_ITEMS],int size)191 sof_ipc4_set_up_volume_table(struct snd_sof_control *scontrol, int tlv[SOF_TLV_ITEMS], int size)
192 {
193 int i;
194
195 /* init the volume table */
196 scontrol->volume_table = kcalloc(size, sizeof(u32), GFP_KERNEL);
197 if (!scontrol->volume_table)
198 return -ENOMEM;
199
200 /* populate the volume table */
201 for (i = 0; i < size ; i++) {
202 u32 val = vol_compute_gain(i, tlv);
203 u64 q31val = ((u64)val) << 15; /* Can be over Q1.31, need to saturate */
204
205 scontrol->volume_table[i] = q31val > SOF_IPC4_VOL_ZERO_DB ?
206 SOF_IPC4_VOL_ZERO_DB : q31val;
207 }
208
209 return 0;
210 }
211
212 const struct sof_ipc_tplg_control_ops tplg_ipc4_control_ops = {
213 .volume_put = sof_ipc4_volume_put,
214 .volume_get = sof_ipc4_volume_get,
215 .widget_kcontrol_setup = sof_ipc4_widget_kcontrol_setup,
216 .set_up_volume_table = sof_ipc4_set_up_volume_table,
217 };
218