xref: /openbmc/linux/sound/pci/hda/hda_cs_dsp_ctl.c (revision 93696d8f)
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // HDA DSP ALSA Control Driver
4 //
5 // Copyright 2022 Cirrus Logic, Inc.
6 //
7 // Author: Stefan Binding <sbinding@opensource.cirrus.com>
8 
9 #include <linux/module.h>
10 #include <sound/soc.h>
11 #include <linux/cleanup.h>
12 #include <linux/firmware/cirrus/cs_dsp.h>
13 #include <linux/firmware/cirrus/wmfw.h>
14 #include "hda_cs_dsp_ctl.h"
15 
16 #define ADSP_MAX_STD_CTRL_SIZE               512
17 
18 struct hda_cs_dsp_coeff_ctl {
19 	struct cs_dsp_coeff_ctl *cs_ctl;
20 	struct snd_card *card;
21 	struct snd_kcontrol *kctl;
22 };
23 
24 static const char * const hda_cs_dsp_fw_text[HDA_CS_DSP_NUM_FW] = {
25 	[HDA_CS_DSP_FW_SPK_PROT] = "Prot",
26 	[HDA_CS_DSP_FW_SPK_CALI] = "Cali",
27 	[HDA_CS_DSP_FW_SPK_DIAG] = "Diag",
28 	[HDA_CS_DSP_FW_MISC] =     "Misc",
29 };
30 
31 const char * const hda_cs_dsp_fw_ids[HDA_CS_DSP_NUM_FW] = {
32 	[HDA_CS_DSP_FW_SPK_PROT] = "spk-prot",
33 	[HDA_CS_DSP_FW_SPK_CALI] = "spk-cali",
34 	[HDA_CS_DSP_FW_SPK_DIAG] = "spk-diag",
35 	[HDA_CS_DSP_FW_MISC] =     "misc",
36 };
37 EXPORT_SYMBOL_NS_GPL(hda_cs_dsp_fw_ids, SND_HDA_CS_DSP_CONTROLS);
38 
39 static int hda_cs_dsp_coeff_info(struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
40 {
41 	struct hda_cs_dsp_coeff_ctl *ctl = (struct hda_cs_dsp_coeff_ctl *)snd_kcontrol_chip(kctl);
42 	struct cs_dsp_coeff_ctl *cs_ctl = ctl->cs_ctl;
43 
44 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
45 	uinfo->count = cs_ctl->len;
46 
47 	return 0;
48 }
49 
50 static int hda_cs_dsp_coeff_put(struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
51 {
52 	struct hda_cs_dsp_coeff_ctl *ctl = (struct hda_cs_dsp_coeff_ctl *)snd_kcontrol_chip(kctl);
53 	struct cs_dsp_coeff_ctl *cs_ctl = ctl->cs_ctl;
54 	char *p = ucontrol->value.bytes.data;
55 	int ret = 0;
56 
57 	mutex_lock(&cs_ctl->dsp->pwr_lock);
58 	ret = cs_dsp_coeff_write_ctrl(cs_ctl, 0, p, cs_ctl->len);
59 	mutex_unlock(&cs_ctl->dsp->pwr_lock);
60 
61 	return ret;
62 }
63 
64 static int hda_cs_dsp_coeff_get(struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
65 {
66 	struct hda_cs_dsp_coeff_ctl *ctl = (struct hda_cs_dsp_coeff_ctl *)snd_kcontrol_chip(kctl);
67 	struct cs_dsp_coeff_ctl *cs_ctl = ctl->cs_ctl;
68 	char *p = ucontrol->value.bytes.data;
69 	int ret;
70 
71 	mutex_lock(&cs_ctl->dsp->pwr_lock);
72 	ret = cs_dsp_coeff_read_ctrl(cs_ctl, 0, p, cs_ctl->len);
73 	mutex_unlock(&cs_ctl->dsp->pwr_lock);
74 
75 	return ret;
76 }
77 
78 static unsigned int wmfw_convert_flags(unsigned int in)
79 {
80 	unsigned int out, rd, wr, vol;
81 
82 	rd = SNDRV_CTL_ELEM_ACCESS_READ;
83 	wr = SNDRV_CTL_ELEM_ACCESS_WRITE;
84 	vol = SNDRV_CTL_ELEM_ACCESS_VOLATILE;
85 
86 	out = 0;
87 
88 	if (in) {
89 		out |= rd;
90 		if (in & WMFW_CTL_FLAG_WRITEABLE)
91 			out |= wr;
92 		if (in & WMFW_CTL_FLAG_VOLATILE)
93 			out |= vol;
94 	} else {
95 		out |= rd | wr | vol;
96 	}
97 
98 	return out;
99 }
100 
101 static void hda_cs_dsp_free_kcontrol(struct snd_kcontrol *kctl)
102 {
103 	struct hda_cs_dsp_coeff_ctl *ctl = (struct hda_cs_dsp_coeff_ctl *)snd_kcontrol_chip(kctl);
104 	struct cs_dsp_coeff_ctl *cs_ctl = ctl->cs_ctl;
105 
106 	/* NULL priv to prevent a double-free in hda_cs_dsp_control_remove() */
107 	cs_ctl->priv = NULL;
108 	kfree(ctl);
109 }
110 
111 static void hda_cs_dsp_add_kcontrol(struct cs_dsp_coeff_ctl *cs_ctl,
112 				    const struct hda_cs_dsp_ctl_info *info,
113 				    const char *name)
114 {
115 	struct snd_kcontrol_new kcontrol = {0};
116 	struct snd_kcontrol *kctl;
117 	struct hda_cs_dsp_coeff_ctl *ctl __free(kfree) = NULL;
118 	int ret = 0;
119 
120 	if (cs_ctl->len > ADSP_MAX_STD_CTRL_SIZE) {
121 		dev_err(cs_ctl->dsp->dev, "KControl %s: length %zu exceeds maximum %d\n", name,
122 			cs_ctl->len, ADSP_MAX_STD_CTRL_SIZE);
123 		return;
124 	}
125 
126 	ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
127 	if (!ctl)
128 		return;
129 
130 	ctl->cs_ctl = cs_ctl;
131 	ctl->card = info->card;
132 
133 	kcontrol.name = name;
134 	kcontrol.info = hda_cs_dsp_coeff_info;
135 	kcontrol.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
136 	kcontrol.access = wmfw_convert_flags(cs_ctl->flags);
137 	kcontrol.get = hda_cs_dsp_coeff_get;
138 	kcontrol.put = hda_cs_dsp_coeff_put;
139 
140 	kctl = snd_ctl_new1(&kcontrol, (void *)ctl);
141 	if (!kctl)
142 		return;
143 
144 	kctl->private_free = hda_cs_dsp_free_kcontrol;
145 	ctl->kctl = kctl;
146 
147 	/* snd_ctl_add() calls our private_free on error, which will kfree(ctl) */
148 	cs_ctl->priv = no_free_ptr(ctl);
149 	ret = snd_ctl_add(info->card, kctl);
150 	if (ret) {
151 		dev_err(cs_ctl->dsp->dev, "Failed to add KControl %s = %d\n", kcontrol.name, ret);
152 		return;
153 	}
154 
155 	dev_dbg(cs_ctl->dsp->dev, "Added KControl: %s\n", kcontrol.name);
156 }
157 
158 static void hda_cs_dsp_control_add(struct cs_dsp_coeff_ctl *cs_ctl,
159 				   const struct hda_cs_dsp_ctl_info *info)
160 {
161 	struct cs_dsp *cs_dsp = cs_ctl->dsp;
162 	char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
163 	const char *region_name;
164 	int ret;
165 
166 	region_name = cs_dsp_mem_region_name(cs_ctl->alg_region.type);
167 	if (!region_name) {
168 		dev_warn(cs_dsp->dev, "Unknown region type: %d\n", cs_ctl->alg_region.type);
169 		return;
170 	}
171 
172 	ret = scnprintf(name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN, "%s %s %.12s %x", info->device_name,
173 			cs_dsp->name, hda_cs_dsp_fw_text[info->fw_type], cs_ctl->alg_region.alg);
174 
175 	if (cs_ctl->subname) {
176 		int avail = SNDRV_CTL_ELEM_ID_NAME_MAXLEN - ret - 2;
177 		int skip = 0;
178 
179 		/* Truncate the subname from the start if it is too long */
180 		if (cs_ctl->subname_len > avail)
181 			skip = cs_ctl->subname_len - avail;
182 
183 		snprintf(name + ret, SNDRV_CTL_ELEM_ID_NAME_MAXLEN - ret,
184 			 " %.*s", cs_ctl->subname_len - skip, cs_ctl->subname + skip);
185 	}
186 
187 	hda_cs_dsp_add_kcontrol(cs_ctl, info, name);
188 }
189 
190 void hda_cs_dsp_add_controls(struct cs_dsp *dsp, const struct hda_cs_dsp_ctl_info *info)
191 {
192 	struct cs_dsp_coeff_ctl *cs_ctl;
193 
194 	/*
195 	 * pwr_lock would cause mutex inversion with ALSA control lock compared
196 	 * to the get/put functions.
197 	 * It is safe to walk the list without holding a mutex because entries
198 	 * are persistent and only cs_dsp_power_up() or cs_dsp_remove() can
199 	 * change the list.
200 	 */
201 	lockdep_assert_not_held(&dsp->pwr_lock);
202 
203 	list_for_each_entry(cs_ctl, &dsp->ctl_list, list) {
204 		if (cs_ctl->flags & WMFW_CTL_FLAG_SYS)
205 			continue;
206 
207 		if (cs_ctl->priv)
208 			continue;
209 
210 		hda_cs_dsp_control_add(cs_ctl, info);
211 	}
212 }
213 EXPORT_SYMBOL_NS_GPL(hda_cs_dsp_add_controls, SND_HDA_CS_DSP_CONTROLS);
214 
215 void hda_cs_dsp_control_remove(struct cs_dsp_coeff_ctl *cs_ctl)
216 {
217 	struct hda_cs_dsp_coeff_ctl *ctl = cs_ctl->priv;
218 
219 	/* ctl and kctl may already have been removed by ALSA private_free */
220 	if (ctl && ctl->kctl)
221 		snd_ctl_remove(ctl->card, ctl->kctl);
222 }
223 EXPORT_SYMBOL_NS_GPL(hda_cs_dsp_control_remove, SND_HDA_CS_DSP_CONTROLS);
224 
225 int hda_cs_dsp_write_ctl(struct cs_dsp *dsp, const char *name, int type,
226 			 unsigned int alg, const void *buf, size_t len)
227 {
228 	struct cs_dsp_coeff_ctl *cs_ctl;
229 	struct hda_cs_dsp_coeff_ctl *ctl;
230 	int ret;
231 
232 	mutex_lock(&dsp->pwr_lock);
233 	cs_ctl = cs_dsp_get_ctl(dsp, name, type, alg);
234 	ret = cs_dsp_coeff_write_ctrl(cs_ctl, 0, buf, len);
235 	mutex_unlock(&dsp->pwr_lock);
236 	if (ret < 0)
237 		return ret;
238 
239 	if (ret == 0 || (cs_ctl->flags & WMFW_CTL_FLAG_SYS))
240 		return 0;
241 
242 	ctl = cs_ctl->priv;
243 
244 	snd_ctl_notify(ctl->card, SNDRV_CTL_EVENT_MASK_VALUE, &ctl->kctl->id);
245 
246 	return 0;
247 }
248 EXPORT_SYMBOL_NS_GPL(hda_cs_dsp_write_ctl, SND_HDA_CS_DSP_CONTROLS);
249 
250 int hda_cs_dsp_read_ctl(struct cs_dsp *dsp, const char *name, int type,
251 			unsigned int alg, void *buf, size_t len)
252 {
253 	int ret;
254 
255 	mutex_lock(&dsp->pwr_lock);
256 	ret = cs_dsp_coeff_read_ctrl(cs_dsp_get_ctl(dsp, name, type, alg), 0, buf, len);
257 	mutex_unlock(&dsp->pwr_lock);
258 
259 	return ret;
260 
261 }
262 EXPORT_SYMBOL_NS_GPL(hda_cs_dsp_read_ctl, SND_HDA_CS_DSP_CONTROLS);
263 
264 MODULE_DESCRIPTION("CS_DSP ALSA Control HDA Library");
265 MODULE_AUTHOR("Stefan Binding, <sbinding@opensource.cirrus.com>");
266 MODULE_LICENSE("GPL");
267 MODULE_IMPORT_NS(FW_CS_DSP);
268