1 // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
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.
6 // Copyright(c) 2018 Intel Corporation. All rights reserved.
8 // Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
13 #include <linux/pm_runtime.h>
16 static inline u32 mixer_to_ipc(unsigned int value, u32 *volume_map, int size)
19 return volume_map[size - 1];
21 return volume_map[value];
24 static inline u32 ipc_to_mixer(u32 value, u32 *volume_map, int size)
28 for (i = 0; i < size; i++) {
29 if (volume_map[i] >= value)
36 int snd_sof_volume_get(struct snd_kcontrol *kcontrol,
37 struct snd_ctl_elem_value *ucontrol)
39 struct soc_mixer_control *sm =
40 (struct soc_mixer_control *)kcontrol->private_value;
41 struct snd_sof_control *scontrol = sm->dobj.private;
42 struct snd_sof_dev *sdev = scontrol->sdev;
43 struct sof_ipc_ctrl_data *cdata = scontrol->control_data;
44 unsigned int i, channels = scontrol->num_channels;
47 ret = pm_runtime_get_sync(sdev->dev);
49 dev_err_ratelimited(sdev->dev,
50 "error: volume get failed to resume %d\n",
52 pm_runtime_put_noidle(sdev->dev);
56 /* get all the mixer data from DSP */
57 snd_sof_ipc_set_get_comp_data(sdev->ipc, scontrol,
58 SOF_IPC_COMP_GET_VALUE,
59 SOF_CTRL_TYPE_VALUE_CHAN_GET,
63 /* read back each channel */
64 for (i = 0; i < channels; i++)
65 ucontrol->value.integer.value[i] =
66 ipc_to_mixer(cdata->chanv[i].value,
67 scontrol->volume_table, sm->max + 1);
69 pm_runtime_mark_last_busy(sdev->dev);
70 err = pm_runtime_put_autosuspend(sdev->dev);
72 dev_err_ratelimited(sdev->dev,
73 "error: volume get failed to idle %d\n",
78 int snd_sof_volume_put(struct snd_kcontrol *kcontrol,
79 struct snd_ctl_elem_value *ucontrol)
81 struct soc_mixer_control *sm =
82 (struct soc_mixer_control *)kcontrol->private_value;
83 struct snd_sof_control *scontrol = sm->dobj.private;
84 struct snd_sof_dev *sdev = scontrol->sdev;
85 struct sof_ipc_ctrl_data *cdata = scontrol->control_data;
86 unsigned int i, channels = scontrol->num_channels;
89 ret = pm_runtime_get_sync(sdev->dev);
91 dev_err_ratelimited(sdev->dev,
92 "error: volume put failed to resume %d\n",
94 pm_runtime_put_noidle(sdev->dev);
98 /* update each channel */
99 for (i = 0; i < channels; i++) {
100 cdata->chanv[i].value =
101 mixer_to_ipc(ucontrol->value.integer.value[i],
102 scontrol->volume_table, sm->max + 1);
103 cdata->chanv[i].channel = i;
106 /* notify DSP of mixer updates */
107 snd_sof_ipc_set_get_comp_data(sdev->ipc, scontrol,
108 SOF_IPC_COMP_SET_VALUE,
109 SOF_CTRL_TYPE_VALUE_CHAN_GET,
113 pm_runtime_mark_last_busy(sdev->dev);
114 err = pm_runtime_put_autosuspend(sdev->dev);
116 dev_err_ratelimited(sdev->dev,
117 "error: volume put failed to idle %d\n",
122 int snd_sof_switch_get(struct snd_kcontrol *kcontrol,
123 struct snd_ctl_elem_value *ucontrol)
125 struct soc_mixer_control *sm =
126 (struct soc_mixer_control *)kcontrol->private_value;
127 struct snd_sof_control *scontrol = sm->dobj.private;
128 struct snd_sof_dev *sdev = scontrol->sdev;
129 struct sof_ipc_ctrl_data *cdata = scontrol->control_data;
130 unsigned int i, channels = scontrol->num_channels;
133 ret = pm_runtime_get_sync(sdev->dev);
135 dev_err_ratelimited(sdev->dev,
136 "error: switch get failed to resume %d\n",
138 pm_runtime_put_noidle(sdev->dev);
142 /* get all the mixer data from DSP */
143 snd_sof_ipc_set_get_comp_data(sdev->ipc, scontrol,
144 SOF_IPC_COMP_GET_VALUE,
145 SOF_CTRL_TYPE_VALUE_CHAN_GET,
149 /* read back each channel */
150 for (i = 0; i < channels; i++)
151 ucontrol->value.integer.value[i] = cdata->chanv[i].value;
153 pm_runtime_mark_last_busy(sdev->dev);
154 err = pm_runtime_put_autosuspend(sdev->dev);
156 dev_err_ratelimited(sdev->dev,
157 "error: switch get failed to idle %d\n",
162 int snd_sof_switch_put(struct snd_kcontrol *kcontrol,
163 struct snd_ctl_elem_value *ucontrol)
165 struct soc_mixer_control *sm =
166 (struct soc_mixer_control *)kcontrol->private_value;
167 struct snd_sof_control *scontrol = sm->dobj.private;
168 struct snd_sof_dev *sdev = scontrol->sdev;
169 struct sof_ipc_ctrl_data *cdata = scontrol->control_data;
170 unsigned int i, channels = scontrol->num_channels;
173 ret = pm_runtime_get_sync(sdev->dev);
175 dev_err_ratelimited(sdev->dev,
176 "error: switch put failed to resume %d\n",
178 pm_runtime_put_noidle(sdev->dev);
182 /* update each channel */
183 for (i = 0; i < channels; i++) {
184 cdata->chanv[i].value = ucontrol->value.integer.value[i];
185 cdata->chanv[i].channel = i;
188 /* notify DSP of mixer updates */
189 snd_sof_ipc_set_get_comp_data(sdev->ipc, scontrol,
190 SOF_IPC_COMP_SET_VALUE,
191 SOF_CTRL_TYPE_VALUE_CHAN_GET,
195 pm_runtime_mark_last_busy(sdev->dev);
196 err = pm_runtime_put_autosuspend(sdev->dev);
198 dev_err_ratelimited(sdev->dev,
199 "error: switch put failed to idle %d\n",
204 int snd_sof_enum_get(struct snd_kcontrol *kcontrol,
205 struct snd_ctl_elem_value *ucontrol)
207 struct soc_enum *se =
208 (struct soc_enum *)kcontrol->private_value;
209 struct snd_sof_control *scontrol = se->dobj.private;
210 struct snd_sof_dev *sdev = scontrol->sdev;
211 struct sof_ipc_ctrl_data *cdata = scontrol->control_data;
212 unsigned int i, channels = scontrol->num_channels;
215 ret = pm_runtime_get_sync(sdev->dev);
217 dev_err_ratelimited(sdev->dev,
218 "error: enum get failed to resume %d\n",
220 pm_runtime_put_noidle(sdev->dev);
224 /* get all the enum data from DSP */
225 snd_sof_ipc_set_get_comp_data(sdev->ipc, scontrol,
226 SOF_IPC_COMP_GET_VALUE,
227 SOF_CTRL_TYPE_VALUE_CHAN_GET,
231 /* read back each channel */
232 for (i = 0; i < channels; i++)
233 ucontrol->value.enumerated.item[i] = cdata->chanv[i].value;
235 pm_runtime_mark_last_busy(sdev->dev);
236 err = pm_runtime_put_autosuspend(sdev->dev);
238 dev_err_ratelimited(sdev->dev,
239 "error: enum get failed to idle %d\n",
244 int snd_sof_enum_put(struct snd_kcontrol *kcontrol,
245 struct snd_ctl_elem_value *ucontrol)
247 struct soc_enum *se =
248 (struct soc_enum *)kcontrol->private_value;
249 struct snd_sof_control *scontrol = se->dobj.private;
250 struct snd_sof_dev *sdev = scontrol->sdev;
251 struct sof_ipc_ctrl_data *cdata = scontrol->control_data;
252 unsigned int i, channels = scontrol->num_channels;
255 ret = pm_runtime_get_sync(sdev->dev);
257 dev_err_ratelimited(sdev->dev,
258 "error: enum put failed to resume %d\n",
260 pm_runtime_put_noidle(sdev->dev);
264 /* update each channel */
265 for (i = 0; i < channels; i++) {
266 cdata->chanv[i].value = ucontrol->value.enumerated.item[i];
267 cdata->chanv[i].channel = i;
270 /* notify DSP of enum updates */
271 snd_sof_ipc_set_get_comp_data(sdev->ipc, scontrol,
272 SOF_IPC_COMP_SET_VALUE,
273 SOF_CTRL_TYPE_VALUE_CHAN_GET,
277 pm_runtime_mark_last_busy(sdev->dev);
278 err = pm_runtime_put_autosuspend(sdev->dev);
280 dev_err_ratelimited(sdev->dev,
281 "error: enum put failed to idle %d\n",
286 int snd_sof_bytes_get(struct snd_kcontrol *kcontrol,
287 struct snd_ctl_elem_value *ucontrol)
289 struct soc_bytes_ext *be =
290 (struct soc_bytes_ext *)kcontrol->private_value;
291 struct snd_sof_control *scontrol = be->dobj.private;
292 struct snd_sof_dev *sdev = scontrol->sdev;
293 struct sof_ipc_ctrl_data *cdata = scontrol->control_data;
294 struct sof_abi_hdr *data = cdata->data;
298 if (be->max > sizeof(ucontrol->value.bytes.data)) {
299 dev_err_ratelimited(sdev->dev,
300 "error: data max %d exceeds ucontrol data array size\n",
305 ret = pm_runtime_get_sync(sdev->dev);
307 dev_err_ratelimited(sdev->dev,
308 "error: bytes get failed to resume %d\n",
310 pm_runtime_put_noidle(sdev->dev);
314 /* get all the binary data from DSP */
315 snd_sof_ipc_set_get_comp_data(sdev->ipc, scontrol,
316 SOF_IPC_COMP_GET_DATA,
317 SOF_CTRL_TYPE_DATA_GET,
321 size = data->size + sizeof(*data);
322 if (size > be->max) {
323 dev_err_ratelimited(sdev->dev,
324 "error: DSP sent %zu bytes max is %d\n",
330 /* copy back to kcontrol */
331 memcpy(ucontrol->value.bytes.data, data, size);
334 pm_runtime_mark_last_busy(sdev->dev);
335 err = pm_runtime_put_autosuspend(sdev->dev);
337 dev_err_ratelimited(sdev->dev,
338 "error: bytes get failed to idle %d\n",
343 int snd_sof_bytes_put(struct snd_kcontrol *kcontrol,
344 struct snd_ctl_elem_value *ucontrol)
346 struct soc_bytes_ext *be =
347 (struct soc_bytes_ext *)kcontrol->private_value;
348 struct snd_sof_control *scontrol = be->dobj.private;
349 struct snd_sof_dev *sdev = scontrol->sdev;
350 struct sof_ipc_ctrl_data *cdata = scontrol->control_data;
351 struct sof_abi_hdr *data = cdata->data;
352 size_t size = data->size + sizeof(*data);
355 if (be->max > sizeof(ucontrol->value.bytes.data)) {
356 dev_err_ratelimited(sdev->dev,
357 "error: data max %d exceeds ucontrol data array size\n",
362 if (size > be->max) {
363 dev_err_ratelimited(sdev->dev,
364 "error: size too big %zu bytes max is %d\n",
369 ret = pm_runtime_get_sync(sdev->dev);
371 dev_err_ratelimited(sdev->dev,
372 "error: bytes put failed to resume %d\n",
374 pm_runtime_put_noidle(sdev->dev);
378 /* copy from kcontrol */
379 memcpy(data, ucontrol->value.bytes.data, size);
381 /* notify DSP of byte control updates */
382 snd_sof_ipc_set_get_comp_data(sdev->ipc, scontrol,
383 SOF_IPC_COMP_SET_DATA,
384 SOF_CTRL_TYPE_DATA_SET,
388 pm_runtime_mark_last_busy(sdev->dev);
389 err = pm_runtime_put_autosuspend(sdev->dev);
391 dev_err_ratelimited(sdev->dev,
392 "error: bytes put failed to idle %d\n",
397 int snd_sof_bytes_ext_put(struct snd_kcontrol *kcontrol,
398 const unsigned int __user *binary_data,
401 struct soc_bytes_ext *be =
402 (struct soc_bytes_ext *)kcontrol->private_value;
403 struct snd_sof_control *scontrol = be->dobj.private;
404 struct snd_sof_dev *sdev = scontrol->sdev;
405 struct sof_ipc_ctrl_data *cdata = scontrol->control_data;
406 struct snd_ctl_tlv header;
407 const struct snd_ctl_tlv __user *tlvd =
408 (const struct snd_ctl_tlv __user *)binary_data;
413 * The beginning of bytes data contains a header from where
414 * the length (as bytes) is needed to know the correct copy
415 * length of data from tlvd->tlv.
417 if (copy_from_user(&header, tlvd, sizeof(const struct snd_ctl_tlv)))
420 /* be->max is coming from topology */
421 if (header.length > be->max) {
422 dev_err_ratelimited(sdev->dev, "error: Bytes data size %d exceeds max %d.\n",
423 header.length, be->max);
427 /* Check that header id matches the command */
428 if (header.numid != scontrol->cmd) {
429 dev_err_ratelimited(sdev->dev,
430 "error: incorrect numid %d\n",
435 if (copy_from_user(cdata->data, tlvd->tlv, header.length))
438 if (cdata->data->magic != SOF_ABI_MAGIC) {
439 dev_err_ratelimited(sdev->dev,
440 "error: Wrong ABI magic 0x%08x.\n",
445 if (SOF_ABI_VERSION_INCOMPATIBLE(SOF_ABI_VERSION, cdata->data->abi)) {
446 dev_err_ratelimited(sdev->dev, "error: Incompatible ABI version 0x%08x.\n",
451 if (cdata->data->size + sizeof(const struct sof_abi_hdr) > be->max) {
452 dev_err_ratelimited(sdev->dev, "error: Mismatch in ABI data size (truncated?).\n");
456 ret = pm_runtime_get_sync(sdev->dev);
458 dev_err_ratelimited(sdev->dev,
459 "error: bytes_ext put failed to resume %d\n",
461 pm_runtime_put_noidle(sdev->dev);
465 /* notify DSP of byte control updates */
466 snd_sof_ipc_set_get_comp_data(sdev->ipc, scontrol,
467 SOF_IPC_COMP_SET_DATA,
468 SOF_CTRL_TYPE_DATA_SET,
472 pm_runtime_mark_last_busy(sdev->dev);
473 err = pm_runtime_put_autosuspend(sdev->dev);
475 dev_err_ratelimited(sdev->dev,
476 "error: bytes_ext put failed to idle %d\n",
482 int snd_sof_bytes_ext_get(struct snd_kcontrol *kcontrol,
483 unsigned int __user *binary_data,
486 struct soc_bytes_ext *be =
487 (struct soc_bytes_ext *)kcontrol->private_value;
488 struct snd_sof_control *scontrol = be->dobj.private;
489 struct snd_sof_dev *sdev = scontrol->sdev;
490 struct sof_ipc_ctrl_data *cdata = scontrol->control_data;
491 struct snd_ctl_tlv header;
492 struct snd_ctl_tlv __user *tlvd =
493 (struct snd_ctl_tlv __user *)binary_data;
498 ret = pm_runtime_get_sync(sdev->dev);
500 dev_err_ratelimited(sdev->dev,
501 "error: bytes_ext get failed to resume %d\n",
503 pm_runtime_put_noidle(sdev->dev);
508 * Decrement the limit by ext bytes header size to
509 * ensure the user space buffer is not exceeded.
511 size -= sizeof(const struct snd_ctl_tlv);
513 /* set the ABI header values */
514 cdata->data->magic = SOF_ABI_MAGIC;
515 cdata->data->abi = SOF_ABI_VERSION;
517 /* get all the component data from DSP */
518 ret = snd_sof_ipc_set_get_comp_data(sdev->ipc, scontrol,
519 SOF_IPC_COMP_GET_DATA,
520 SOF_CTRL_TYPE_DATA_GET,
524 /* Prevent read of other kernel data or possibly corrupt response */
525 data_size = cdata->data->size + sizeof(const struct sof_abi_hdr);
527 /* check data size doesn't exceed max coming from topology */
528 if (data_size > be->max) {
529 dev_err_ratelimited(sdev->dev, "error: user data size %d exceeds max size %d.\n",
535 header.numid = scontrol->cmd;
536 header.length = data_size;
537 if (copy_to_user(tlvd, &header, sizeof(const struct snd_ctl_tlv))) {
542 if (copy_to_user(tlvd->tlv, cdata->data, data_size))
546 pm_runtime_mark_last_busy(sdev->dev);
547 err = pm_runtime_put_autosuspend(sdev->dev);
549 dev_err_ratelimited(sdev->dev,
550 "error: bytes_ext get failed to idle %d\n",