xref: /linux/sound/soc/sof/control.c (revision 3092b9e481fedc413bf7acda9edc04b0bd8cb28a)
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) 2018 Intel Corporation
7 //
8 // Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
9 //
10 
11 /* Mixer Controls */
12 
13 #include <linux/pm_runtime.h>
14 #include <linux/leds.h>
15 #include "sof-priv.h"
16 #include "sof-audio.h"
17 
18 int snd_sof_volume_get(struct snd_kcontrol *kcontrol,
19 		       struct snd_ctl_elem_value *ucontrol)
20 {
21 	struct soc_mixer_control *sm = (struct soc_mixer_control *)kcontrol->private_value;
22 	struct snd_sof_control *scontrol = sm->dobj.private;
23 	struct snd_soc_component *scomp = scontrol->scomp;
24 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
25 	const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
26 
27 	if (tplg_ops && tplg_ops->control && tplg_ops->control->volume_get)
28 		return tplg_ops->control->volume_get(scontrol, ucontrol);
29 
30 	return 0;
31 }
32 
33 int snd_sof_volume_put(struct snd_kcontrol *kcontrol,
34 		       struct snd_ctl_elem_value *ucontrol)
35 {
36 	struct soc_mixer_control *sm = (struct soc_mixer_control *)kcontrol->private_value;
37 	struct snd_sof_control *scontrol = sm->dobj.private;
38 	struct snd_soc_component *scomp = scontrol->scomp;
39 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
40 	const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
41 
42 	if (tplg_ops && tplg_ops->control && tplg_ops->control->volume_put)
43 		return tplg_ops->control->volume_put(scontrol, ucontrol);
44 
45 	return false;
46 }
47 
48 int snd_sof_volume_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
49 {
50 	struct soc_mixer_control *sm = (struct soc_mixer_control *)kcontrol->private_value;
51 	struct snd_sof_control *scontrol = sm->dobj.private;
52 	unsigned int channels = scontrol->num_channels;
53 	int platform_max;
54 
55 	if (!sm->platform_max)
56 		sm->platform_max = sm->max;
57 	platform_max = sm->platform_max;
58 
59 	if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
60 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
61 	else
62 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
63 
64 	uinfo->count = channels;
65 	uinfo->value.integer.min = 0;
66 	uinfo->value.integer.max = platform_max - sm->min;
67 	return 0;
68 }
69 
70 int snd_sof_switch_get(struct snd_kcontrol *kcontrol,
71 		       struct snd_ctl_elem_value *ucontrol)
72 {
73 	struct soc_mixer_control *sm = (struct soc_mixer_control *)kcontrol->private_value;
74 	struct snd_sof_control *scontrol = sm->dobj.private;
75 	struct snd_soc_component *scomp = scontrol->scomp;
76 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
77 	const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
78 
79 	if (tplg_ops && tplg_ops->control && tplg_ops->control->switch_get)
80 		return tplg_ops->control->switch_get(scontrol, ucontrol);
81 
82 	return 0;
83 }
84 
85 int snd_sof_switch_put(struct snd_kcontrol *kcontrol,
86 		       struct snd_ctl_elem_value *ucontrol)
87 {
88 	struct soc_mixer_control *sm = (struct soc_mixer_control *)kcontrol->private_value;
89 	struct snd_sof_control *scontrol = sm->dobj.private;
90 	struct snd_soc_component *scomp = scontrol->scomp;
91 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
92 	const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
93 
94 	if (tplg_ops && tplg_ops->control && tplg_ops->control->switch_put)
95 		return tplg_ops->control->switch_put(scontrol, ucontrol);
96 
97 	return false;
98 }
99 
100 int snd_sof_enum_get(struct snd_kcontrol *kcontrol,
101 		     struct snd_ctl_elem_value *ucontrol)
102 {
103 	struct soc_enum *se = (struct soc_enum *)kcontrol->private_value;
104 	struct snd_sof_control *scontrol = se->dobj.private;
105 	struct snd_soc_component *scomp = scontrol->scomp;
106 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
107 	const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
108 
109 	if (tplg_ops && tplg_ops->control && tplg_ops->control->enum_get)
110 		return tplg_ops->control->enum_get(scontrol, ucontrol);
111 
112 	return 0;
113 }
114 
115 int snd_sof_enum_put(struct snd_kcontrol *kcontrol,
116 		     struct snd_ctl_elem_value *ucontrol)
117 {
118 	struct soc_enum *se = (struct soc_enum *)kcontrol->private_value;
119 	struct snd_sof_control *scontrol = se->dobj.private;
120 	struct snd_soc_component *scomp = scontrol->scomp;
121 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
122 	const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
123 	unsigned int i;
124 
125 	for (i = 0; i < scontrol->num_channels; i++)
126 		if (ucontrol->value.enumerated.item[i] >= se->items)
127 			return -EINVAL;
128 
129 	if (tplg_ops && tplg_ops->control && tplg_ops->control->enum_put)
130 		return tplg_ops->control->enum_put(scontrol, ucontrol);
131 
132 	return false;
133 }
134 
135 int snd_sof_bytes_get(struct snd_kcontrol *kcontrol,
136 		      struct snd_ctl_elem_value *ucontrol)
137 {
138 	struct soc_bytes_ext *be = (struct soc_bytes_ext *)kcontrol->private_value;
139 	struct snd_sof_control *scontrol = be->dobj.private;
140 	struct snd_soc_component *scomp = scontrol->scomp;
141 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
142 	const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
143 
144 	if (tplg_ops && tplg_ops->control && tplg_ops->control->bytes_get)
145 		return tplg_ops->control->bytes_get(scontrol, ucontrol);
146 
147 	return 0;
148 }
149 
150 int snd_sof_bytes_put(struct snd_kcontrol *kcontrol,
151 		      struct snd_ctl_elem_value *ucontrol)
152 {
153 	struct soc_bytes_ext *be = (struct soc_bytes_ext *)kcontrol->private_value;
154 	struct snd_sof_control *scontrol = be->dobj.private;
155 	struct snd_soc_component *scomp = scontrol->scomp;
156 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
157 	const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
158 
159 	if (tplg_ops && tplg_ops->control && tplg_ops->control->bytes_put)
160 		return tplg_ops->control->bytes_put(scontrol, ucontrol);
161 
162 	return 0;
163 }
164 
165 int snd_sof_bytes_ext_put(struct snd_kcontrol *kcontrol,
166 			  const unsigned int __user *binary_data,
167 			  unsigned int size)
168 {
169 	struct soc_bytes_ext *be = (struct soc_bytes_ext *)kcontrol->private_value;
170 	struct snd_sof_control *scontrol = be->dobj.private;
171 	struct snd_soc_component *scomp = scontrol->scomp;
172 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
173 	const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
174 
175 	/* make sure we have at least a header */
176 	if (size < sizeof(struct snd_ctl_tlv))
177 		return -EINVAL;
178 
179 	if (tplg_ops && tplg_ops->control && tplg_ops->control->bytes_ext_put)
180 		return tplg_ops->control->bytes_ext_put(scontrol, binary_data, size);
181 
182 	return 0;
183 }
184 
185 int snd_sof_bytes_ext_volatile_get(struct snd_kcontrol *kcontrol, unsigned int __user *binary_data,
186 				   unsigned int size)
187 {
188 	struct soc_bytes_ext *be = (struct soc_bytes_ext *)kcontrol->private_value;
189 	struct snd_sof_control *scontrol = be->dobj.private;
190 	struct snd_soc_component *scomp = scontrol->scomp;
191 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
192 	const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
193 	int ret, err;
194 
195 	/* ignore the ext_volatile_get call if the callbacks are not provided */
196 	if (!tplg_ops || !tplg_ops->control ||
197 	    !tplg_ops->control->bytes_ext_volatile_get)
198 		return 0;
199 
200 	ret = pm_runtime_resume_and_get(scomp->dev);
201 	if (ret < 0 && ret != -EACCES) {
202 		dev_err_ratelimited(scomp->dev, "%s: failed to resume %d\n", __func__, ret);
203 		return ret;
204 	}
205 
206 	/* Make sure the DSP/firmware is booted up */
207 	ret = snd_sof_boot_dsp_firmware(sdev);
208 	if (!ret)
209 		ret = tplg_ops->control->bytes_ext_volatile_get(scontrol,
210 								binary_data,
211 								size);
212 
213 	err = pm_runtime_put_autosuspend(scomp->dev);
214 	if (err < 0)
215 		dev_err_ratelimited(scomp->dev, "%s: failed to idle %d\n", __func__, err);
216 
217 	return ret;
218 }
219 
220 int snd_sof_bytes_ext_get(struct snd_kcontrol *kcontrol,
221 			  unsigned int __user *binary_data,
222 			  unsigned int size)
223 {
224 	struct soc_bytes_ext *be = (struct soc_bytes_ext *)kcontrol->private_value;
225 	struct snd_sof_control *scontrol = be->dobj.private;
226 	struct snd_soc_component *scomp = scontrol->scomp;
227 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
228 	const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
229 
230 	if (tplg_ops && tplg_ops->control && tplg_ops->control->bytes_ext_get)
231 		return tplg_ops->control->bytes_ext_get(scontrol, binary_data, size);
232 
233 	return 0;
234 }
235