1 // SPDX-License-Identifier: GPL-2.0
3 // HDA DSP ALSA Control Driver
5 // Copyright 2022 Cirrus Logic, Inc.
7 // Author: Stefan Binding <sbinding@opensource.cirrus.com>
9 #include <linux/module.h>
10 #include <sound/soc.h>
11 #include <linux/firmware/cirrus/cs_dsp.h>
12 #include <linux/firmware/cirrus/wmfw.h>
13 #include "hda_cs_dsp_ctl.h"
15 #define ADSP_MAX_STD_CTRL_SIZE 512
17 struct hda_cs_dsp_coeff_ctl {
18 struct cs_dsp_coeff_ctl *cs_ctl;
19 struct snd_card *card;
20 struct snd_kcontrol *kctl;
23 static const char * const hda_cs_dsp_fw_text[HDA_CS_DSP_NUM_FW] = {
24 [HDA_CS_DSP_FW_SPK_PROT] = "Prot",
25 [HDA_CS_DSP_FW_SPK_CALI] = "Cali",
26 [HDA_CS_DSP_FW_SPK_DIAG] = "Diag",
27 [HDA_CS_DSP_FW_MISC] = "Misc",
30 const char * const hda_cs_dsp_fw_ids[HDA_CS_DSP_NUM_FW] = {
31 [HDA_CS_DSP_FW_SPK_PROT] = "spk-prot",
32 [HDA_CS_DSP_FW_SPK_CALI] = "spk-cali",
33 [HDA_CS_DSP_FW_SPK_DIAG] = "spk-diag",
34 [HDA_CS_DSP_FW_MISC] = "misc",
36 EXPORT_SYMBOL_NS_GPL(hda_cs_dsp_fw_ids, SND_HDA_CS_DSP_CONTROLS);
38 static int hda_cs_dsp_coeff_info(struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
40 struct hda_cs_dsp_coeff_ctl *ctl = (struct hda_cs_dsp_coeff_ctl *)snd_kcontrol_chip(kctl);
41 struct cs_dsp_coeff_ctl *cs_ctl = ctl->cs_ctl;
43 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
44 uinfo->count = cs_ctl->len;
49 static int hda_cs_dsp_coeff_put(struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
51 struct hda_cs_dsp_coeff_ctl *ctl = (struct hda_cs_dsp_coeff_ctl *)snd_kcontrol_chip(kctl);
52 struct cs_dsp_coeff_ctl *cs_ctl = ctl->cs_ctl;
53 char *p = ucontrol->value.bytes.data;
56 mutex_lock(&cs_ctl->dsp->pwr_lock);
57 ret = cs_dsp_coeff_write_ctrl(cs_ctl, 0, p, cs_ctl->len);
58 mutex_unlock(&cs_ctl->dsp->pwr_lock);
63 static int hda_cs_dsp_coeff_get(struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
65 struct hda_cs_dsp_coeff_ctl *ctl = (struct hda_cs_dsp_coeff_ctl *)snd_kcontrol_chip(kctl);
66 struct cs_dsp_coeff_ctl *cs_ctl = ctl->cs_ctl;
67 char *p = ucontrol->value.bytes.data;
70 mutex_lock(&cs_ctl->dsp->pwr_lock);
71 ret = cs_dsp_coeff_read_ctrl(cs_ctl, 0, p, cs_ctl->len);
72 mutex_unlock(&cs_ctl->dsp->pwr_lock);
77 static unsigned int wmfw_convert_flags(unsigned int in)
79 unsigned int out, rd, wr, vol;
81 rd = SNDRV_CTL_ELEM_ACCESS_READ;
82 wr = SNDRV_CTL_ELEM_ACCESS_WRITE;
83 vol = SNDRV_CTL_ELEM_ACCESS_VOLATILE;
89 if (in & WMFW_CTL_FLAG_WRITEABLE)
91 if (in & WMFW_CTL_FLAG_VOLATILE)
100 static int hda_cs_dsp_add_kcontrol(struct hda_cs_dsp_coeff_ctl *ctl, const char *name)
102 struct cs_dsp_coeff_ctl *cs_ctl = ctl->cs_ctl;
103 struct snd_kcontrol_new kcontrol = {0};
104 struct snd_kcontrol *kctl;
107 if (cs_ctl->len > ADSP_MAX_STD_CTRL_SIZE) {
108 dev_err(cs_ctl->dsp->dev, "KControl %s: length %zu exceeds maximum %d\n", name,
109 cs_ctl->len, ADSP_MAX_STD_CTRL_SIZE);
113 kcontrol.name = name;
114 kcontrol.info = hda_cs_dsp_coeff_info;
115 kcontrol.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
116 kcontrol.access = wmfw_convert_flags(cs_ctl->flags);
117 kcontrol.get = hda_cs_dsp_coeff_get;
118 kcontrol.put = hda_cs_dsp_coeff_put;
120 /* Save ctl inside private_data, ctl is owned by cs_dsp,
121 * and will be freed when cs_dsp removes the control */
122 kctl = snd_ctl_new1(&kcontrol, (void *)ctl);
128 ret = snd_ctl_add(ctl->card, kctl);
130 dev_err(cs_ctl->dsp->dev, "Failed to add KControl %s = %d\n", kcontrol.name, ret);
134 dev_dbg(cs_ctl->dsp->dev, "Added KControl: %s\n", kcontrol.name);
140 int hda_cs_dsp_control_add(struct cs_dsp_coeff_ctl *cs_ctl, struct hda_cs_dsp_ctl_info *info)
142 struct cs_dsp *cs_dsp = cs_ctl->dsp;
143 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
144 struct hda_cs_dsp_coeff_ctl *ctl;
145 const char *region_name;
148 if (cs_ctl->flags & WMFW_CTL_FLAG_SYS)
151 region_name = cs_dsp_mem_region_name(cs_ctl->alg_region.type);
153 dev_err(cs_dsp->dev, "Unknown region type: %d\n", cs_ctl->alg_region.type);
157 ret = scnprintf(name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN, "%s %s %.12s %x", info->device_name,
158 cs_dsp->name, hda_cs_dsp_fw_text[info->fw_type], cs_ctl->alg_region.alg);
160 if (cs_ctl->subname) {
161 int avail = SNDRV_CTL_ELEM_ID_NAME_MAXLEN - ret - 2;
164 /* Truncate the subname from the start if it is too long */
165 if (cs_ctl->subname_len > avail)
166 skip = cs_ctl->subname_len - avail;
168 snprintf(name + ret, SNDRV_CTL_ELEM_ID_NAME_MAXLEN - ret,
169 " %.*s", cs_ctl->subname_len - skip, cs_ctl->subname + skip);
172 ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
176 ctl->cs_ctl = cs_ctl;
177 ctl->card = info->card;
180 ret = hda_cs_dsp_add_kcontrol(ctl, name);
182 dev_err(cs_dsp->dev, "Error (%d) adding control %s\n", ret, name);
189 EXPORT_SYMBOL_NS_GPL(hda_cs_dsp_control_add, SND_HDA_CS_DSP_CONTROLS);
191 void hda_cs_dsp_control_remove(struct cs_dsp_coeff_ctl *cs_ctl)
193 struct hda_cs_dsp_coeff_ctl *ctl = cs_ctl->priv;
197 EXPORT_SYMBOL_NS_GPL(hda_cs_dsp_control_remove, SND_HDA_CS_DSP_CONTROLS);
199 int hda_cs_dsp_write_ctl(struct cs_dsp *dsp, const char *name, int type,
200 unsigned int alg, const void *buf, size_t len)
202 struct cs_dsp_coeff_ctl *cs_ctl;
203 struct hda_cs_dsp_coeff_ctl *ctl;
206 cs_ctl = cs_dsp_get_ctl(dsp, name, type, alg);
212 ret = cs_dsp_coeff_write_ctrl(cs_ctl, 0, buf, len);
216 if (cs_ctl->flags & WMFW_CTL_FLAG_SYS)
219 snd_ctl_notify(ctl->card, SNDRV_CTL_EVENT_MASK_VALUE, &ctl->kctl->id);
223 EXPORT_SYMBOL_NS_GPL(hda_cs_dsp_write_ctl, SND_HDA_CS_DSP_CONTROLS);
225 int hda_cs_dsp_read_ctl(struct cs_dsp *dsp, const char *name, int type,
226 unsigned int alg, void *buf, size_t len)
228 struct cs_dsp_coeff_ctl *cs_ctl;
230 cs_ctl = cs_dsp_get_ctl(dsp, name, type, alg);
234 return cs_dsp_coeff_read_ctrl(cs_ctl, 0, buf, len);
236 EXPORT_SYMBOL_NS_GPL(hda_cs_dsp_read_ctl, SND_HDA_CS_DSP_CONTROLS);
238 MODULE_DESCRIPTION("CS_DSP ALSA Control HDA Library");
239 MODULE_AUTHOR("Stefan Binding, <sbinding@opensource.cirrus.com>");
240 MODULE_LICENSE("GPL");