1 // SPDX-License-Identifier: GPL-2.0-only
3 // Copyright(c) 2021-2022 Intel Corporation. All rights reserved.
5 // Authors: Amadeusz Slawinski <amadeuszx.slawinski@linux.intel.com>
6 // Cezary Rojewski <cezary.rojewski@intel.com>
15 static struct avs_dev *avs_get_kcontrol_adev(struct snd_kcontrol *kcontrol)
17 struct snd_soc_dapm_widget *w;
19 w = snd_soc_dapm_kcontrol_widget(kcontrol);
21 return to_avs_dev(w->dapm->component->dev);
24 static struct avs_path_module *avs_get_volume_module(struct avs_dev *adev, u32 id)
26 struct avs_path *path;
27 struct avs_path_pipeline *ppl;
28 struct avs_path_module *mod;
30 spin_lock(&adev->path_list_lock);
31 list_for_each_entry(path, &adev->path_list, node) {
32 list_for_each_entry(ppl, &path->ppl_list, node) {
33 list_for_each_entry(mod, &ppl->mod_list, node) {
34 if (guid_equal(&mod->template->cfg_ext->type, &AVS_PEAKVOL_MOD_UUID)
35 && mod->template->ctl_id == id) {
36 spin_unlock(&adev->path_list_lock);
42 spin_unlock(&adev->path_list_lock);
47 int avs_control_volume_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
49 struct soc_mixer_control *mc = (struct soc_mixer_control *)kcontrol->private_value;
50 struct avs_control_data *ctl_data = (struct avs_control_data *)mc->dobj.private;
51 struct avs_dev *adev = avs_get_kcontrol_adev(kcontrol);
52 struct avs_volume_cfg *dspvols = NULL;
53 struct avs_path_module *active_module;
57 /* prevent access to modules while path is being constructed */
58 mutex_lock(&adev->path_mutex);
60 active_module = avs_get_volume_module(adev, ctl_data->id);
62 ret = avs_ipc_peakvol_get_volume(adev, active_module->module_id,
63 active_module->instance_id, &dspvols,
66 ucontrol->value.integer.value[0] = dspvols[0].target_volume;
68 ret = AVS_IPC_RET(ret);
71 ucontrol->value.integer.value[0] = ctl_data->volume;
74 mutex_unlock(&adev->path_mutex);
78 int avs_control_volume_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
80 struct soc_mixer_control *mc = (struct soc_mixer_control *)kcontrol->private_value;
81 struct avs_control_data *ctl_data = (struct avs_control_data *)mc->dobj.private;
82 struct avs_dev *adev = avs_get_kcontrol_adev(kcontrol);
83 long *volume = &ctl_data->volume;
84 struct avs_path_module *active_module;
85 struct avs_volume_cfg dspvol = {0};
86 long ctlvol = ucontrol->value.integer.value[0];
87 int ret = 0, changed = 0;
89 if (ctlvol < 0 || ctlvol > mc->max)
92 /* prevent access to modules while path is being constructed */
93 mutex_lock(&adev->path_mutex);
95 if (*volume != ctlvol) {
100 active_module = avs_get_volume_module(adev, ctl_data->id);
102 dspvol.channel_id = AVS_ALL_CHANNELS_MASK;
103 dspvol.target_volume = *volume;
105 ret = avs_ipc_peakvol_set_volume(adev, active_module->module_id,
106 active_module->instance_id, &dspvol);
107 ret = AVS_IPC_RET(ret);
110 mutex_unlock(&adev->path_mutex);
112 return ret ? ret : changed;