xref: /openbmc/linux/sound/soc/intel/avs/control.c (revision 2f164822)
1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // Copyright(c) 2021-2022 Intel Corporation. All rights reserved.
4 //
5 // Authors: Amadeusz Slawinski <amadeuszx.slawinski@linux.intel.com>
6 //          Cezary Rojewski <cezary.rojewski@intel.com>
7 //
8 
9 #include <sound/soc.h>
10 #include "avs.h"
11 #include "control.h"
12 #include "messages.h"
13 #include "path.h"
14 
15 static struct avs_dev *avs_get_kcontrol_adev(struct snd_kcontrol *kcontrol)
16 {
17 	struct snd_soc_dapm_widget *w;
18 
19 	w = snd_soc_dapm_kcontrol_widget(kcontrol);
20 
21 	return to_avs_dev(w->dapm->component->dev);
22 }
23 
24 static struct avs_path_module *avs_get_kcontrol_module(struct avs_dev *adev, u32 id)
25 {
26 	struct avs_path *path;
27 	struct avs_path_pipeline *ppl;
28 	struct avs_path_module *mod;
29 
30 	list_for_each_entry(path, &adev->path_list, node)
31 		list_for_each_entry(ppl, &path->ppl_list, node)
32 			list_for_each_entry(mod, &ppl->mod_list, node)
33 				if (mod->template->ctl_id && mod->template->ctl_id == id)
34 					return mod;
35 
36 	return NULL;
37 }
38 
39 int avs_control_volume_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
40 {
41 	struct soc_mixer_control *mc = (struct soc_mixer_control *)kcontrol->private_value;
42 	struct avs_control_data *ctl_data = (struct avs_control_data *)mc->dobj.private;
43 	struct avs_dev *adev = avs_get_kcontrol_adev(kcontrol);
44 	struct avs_volume_cfg *dspvols = NULL;
45 	struct avs_path_module *active_module;
46 	size_t num_dspvols;
47 	int ret = 0;
48 
49 	/* prevent access to modules while path is being constructed */
50 	mutex_lock(&adev->path_mutex);
51 
52 	active_module = avs_get_kcontrol_module(adev, ctl_data->id);
53 	if (active_module) {
54 		ret = avs_ipc_peakvol_get_volume(adev, active_module->module_id,
55 						 active_module->instance_id, &dspvols,
56 						 &num_dspvols);
57 		if (!ret)
58 			ucontrol->value.integer.value[0] = dspvols[0].target_volume;
59 
60 		ret = AVS_IPC_RET(ret);
61 		kfree(dspvols);
62 	} else {
63 		ucontrol->value.integer.value[0] = ctl_data->volume;
64 	}
65 
66 	mutex_unlock(&adev->path_mutex);
67 	return ret;
68 }
69 
70 int avs_control_volume_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
71 {
72 	struct soc_mixer_control *mc = (struct soc_mixer_control *)kcontrol->private_value;
73 	struct avs_control_data *ctl_data = (struct avs_control_data *)mc->dobj.private;
74 	struct avs_dev *adev = avs_get_kcontrol_adev(kcontrol);
75 	long *volume = &ctl_data->volume;
76 	struct avs_path_module *active_module;
77 	struct avs_volume_cfg dspvol = {0};
78 	long ctlvol = ucontrol->value.integer.value[0];
79 	int ret = 0, changed = 0;
80 
81 	if (ctlvol < 0 || ctlvol > mc->max)
82 		return -EINVAL;
83 
84 	/* prevent access to modules while path is being constructed */
85 	mutex_lock(&adev->path_mutex);
86 
87 	if (*volume != ctlvol) {
88 		*volume = ctlvol;
89 		changed = 1;
90 	}
91 
92 	active_module = avs_get_kcontrol_module(adev, ctl_data->id);
93 	if (active_module) {
94 		dspvol.channel_id = AVS_ALL_CHANNELS_MASK;
95 		dspvol.target_volume = *volume;
96 
97 		ret = avs_ipc_peakvol_set_volume(adev, active_module->module_id,
98 						 active_module->instance_id, &dspvol);
99 		ret = AVS_IPC_RET(ret);
100 	}
101 
102 	mutex_unlock(&adev->path_mutex);
103 
104 	return ret ? ret : changed;
105 }
106