13233b978SStefan Binding // SPDX-License-Identifier: GPL-2.0
23233b978SStefan Binding //
33233b978SStefan Binding // HDA DSP ALSA Control Driver
43233b978SStefan Binding //
53233b978SStefan Binding // Copyright 2022 Cirrus Logic, Inc.
63233b978SStefan Binding //
73233b978SStefan Binding // Author: Stefan Binding <sbinding@opensource.cirrus.com>
83233b978SStefan Binding
93233b978SStefan Binding #include <linux/module.h>
103233b978SStefan Binding #include <sound/soc.h>
11*6e359be4SRichard Fitzgerald #include <linux/cleanup.h>
123233b978SStefan Binding #include <linux/firmware/cirrus/cs_dsp.h>
133233b978SStefan Binding #include <linux/firmware/cirrus/wmfw.h>
143233b978SStefan Binding #include "hda_cs_dsp_ctl.h"
153233b978SStefan Binding
163233b978SStefan Binding #define ADSP_MAX_STD_CTRL_SIZE 512
173233b978SStefan Binding
183233b978SStefan Binding struct hda_cs_dsp_coeff_ctl {
193233b978SStefan Binding struct cs_dsp_coeff_ctl *cs_ctl;
203233b978SStefan Binding struct snd_card *card;
213233b978SStefan Binding struct snd_kcontrol *kctl;
223233b978SStefan Binding };
233233b978SStefan Binding
243233b978SStefan Binding static const char * const hda_cs_dsp_fw_text[HDA_CS_DSP_NUM_FW] = {
253233b978SStefan Binding [HDA_CS_DSP_FW_SPK_PROT] = "Prot",
263233b978SStefan Binding [HDA_CS_DSP_FW_SPK_CALI] = "Cali",
273233b978SStefan Binding [HDA_CS_DSP_FW_SPK_DIAG] = "Diag",
283233b978SStefan Binding [HDA_CS_DSP_FW_MISC] = "Misc",
293233b978SStefan Binding };
303233b978SStefan Binding
31291e7c22SStefan Binding const char * const hda_cs_dsp_fw_ids[HDA_CS_DSP_NUM_FW] = {
32291e7c22SStefan Binding [HDA_CS_DSP_FW_SPK_PROT] = "spk-prot",
33291e7c22SStefan Binding [HDA_CS_DSP_FW_SPK_CALI] = "spk-cali",
34291e7c22SStefan Binding [HDA_CS_DSP_FW_SPK_DIAG] = "spk-diag",
35291e7c22SStefan Binding [HDA_CS_DSP_FW_MISC] = "misc",
36291e7c22SStefan Binding };
37291e7c22SStefan Binding EXPORT_SYMBOL_NS_GPL(hda_cs_dsp_fw_ids, SND_HDA_CS_DSP_CONTROLS);
38291e7c22SStefan Binding
hda_cs_dsp_coeff_info(struct snd_kcontrol * kctl,struct snd_ctl_elem_info * uinfo)393233b978SStefan Binding static int hda_cs_dsp_coeff_info(struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
403233b978SStefan Binding {
413233b978SStefan Binding struct hda_cs_dsp_coeff_ctl *ctl = (struct hda_cs_dsp_coeff_ctl *)snd_kcontrol_chip(kctl);
423233b978SStefan Binding struct cs_dsp_coeff_ctl *cs_ctl = ctl->cs_ctl;
433233b978SStefan Binding
443233b978SStefan Binding uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
453233b978SStefan Binding uinfo->count = cs_ctl->len;
463233b978SStefan Binding
473233b978SStefan Binding return 0;
483233b978SStefan Binding }
493233b978SStefan Binding
hda_cs_dsp_coeff_put(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)503233b978SStefan Binding static int hda_cs_dsp_coeff_put(struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
513233b978SStefan Binding {
523233b978SStefan Binding struct hda_cs_dsp_coeff_ctl *ctl = (struct hda_cs_dsp_coeff_ctl *)snd_kcontrol_chip(kctl);
533233b978SStefan Binding struct cs_dsp_coeff_ctl *cs_ctl = ctl->cs_ctl;
543233b978SStefan Binding char *p = ucontrol->value.bytes.data;
553233b978SStefan Binding int ret = 0;
563233b978SStefan Binding
573233b978SStefan Binding mutex_lock(&cs_ctl->dsp->pwr_lock);
583233b978SStefan Binding ret = cs_dsp_coeff_write_ctrl(cs_ctl, 0, p, cs_ctl->len);
593233b978SStefan Binding mutex_unlock(&cs_ctl->dsp->pwr_lock);
603233b978SStefan Binding
613233b978SStefan Binding return ret;
623233b978SStefan Binding }
633233b978SStefan Binding
hda_cs_dsp_coeff_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)643233b978SStefan Binding static int hda_cs_dsp_coeff_get(struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
653233b978SStefan Binding {
663233b978SStefan Binding struct hda_cs_dsp_coeff_ctl *ctl = (struct hda_cs_dsp_coeff_ctl *)snd_kcontrol_chip(kctl);
673233b978SStefan Binding struct cs_dsp_coeff_ctl *cs_ctl = ctl->cs_ctl;
683233b978SStefan Binding char *p = ucontrol->value.bytes.data;
693233b978SStefan Binding int ret;
703233b978SStefan Binding
713233b978SStefan Binding mutex_lock(&cs_ctl->dsp->pwr_lock);
723233b978SStefan Binding ret = cs_dsp_coeff_read_ctrl(cs_ctl, 0, p, cs_ctl->len);
733233b978SStefan Binding mutex_unlock(&cs_ctl->dsp->pwr_lock);
743233b978SStefan Binding
753233b978SStefan Binding return ret;
763233b978SStefan Binding }
773233b978SStefan Binding
wmfw_convert_flags(unsigned int in)783233b978SStefan Binding static unsigned int wmfw_convert_flags(unsigned int in)
793233b978SStefan Binding {
803233b978SStefan Binding unsigned int out, rd, wr, vol;
813233b978SStefan Binding
823233b978SStefan Binding rd = SNDRV_CTL_ELEM_ACCESS_READ;
833233b978SStefan Binding wr = SNDRV_CTL_ELEM_ACCESS_WRITE;
843233b978SStefan Binding vol = SNDRV_CTL_ELEM_ACCESS_VOLATILE;
853233b978SStefan Binding
863233b978SStefan Binding out = 0;
873233b978SStefan Binding
883233b978SStefan Binding if (in) {
893233b978SStefan Binding out |= rd;
903233b978SStefan Binding if (in & WMFW_CTL_FLAG_WRITEABLE)
913233b978SStefan Binding out |= wr;
923233b978SStefan Binding if (in & WMFW_CTL_FLAG_VOLATILE)
933233b978SStefan Binding out |= vol;
943233b978SStefan Binding } else {
953233b978SStefan Binding out |= rd | wr | vol;
963233b978SStefan Binding }
973233b978SStefan Binding
983233b978SStefan Binding return out;
993233b978SStefan Binding }
1003233b978SStefan Binding
hda_cs_dsp_free_kcontrol(struct snd_kcontrol * kctl)101*6e359be4SRichard Fitzgerald static void hda_cs_dsp_free_kcontrol(struct snd_kcontrol *kctl)
1023233b978SStefan Binding {
103*6e359be4SRichard Fitzgerald struct hda_cs_dsp_coeff_ctl *ctl = (struct hda_cs_dsp_coeff_ctl *)snd_kcontrol_chip(kctl);
1043233b978SStefan Binding struct cs_dsp_coeff_ctl *cs_ctl = ctl->cs_ctl;
105*6e359be4SRichard Fitzgerald
106*6e359be4SRichard Fitzgerald /* NULL priv to prevent a double-free in hda_cs_dsp_control_remove() */
107*6e359be4SRichard Fitzgerald cs_ctl->priv = NULL;
108*6e359be4SRichard Fitzgerald kfree(ctl);
109*6e359be4SRichard Fitzgerald }
110*6e359be4SRichard Fitzgerald
hda_cs_dsp_add_kcontrol(struct cs_dsp_coeff_ctl * cs_ctl,const struct hda_cs_dsp_ctl_info * info,const char * name)111*6e359be4SRichard Fitzgerald static void hda_cs_dsp_add_kcontrol(struct cs_dsp_coeff_ctl *cs_ctl,
112*6e359be4SRichard Fitzgerald const struct hda_cs_dsp_ctl_info *info,
113*6e359be4SRichard Fitzgerald const char *name)
114*6e359be4SRichard Fitzgerald {
1153233b978SStefan Binding struct snd_kcontrol_new kcontrol = {0};
1163233b978SStefan Binding struct snd_kcontrol *kctl;
117*6e359be4SRichard Fitzgerald struct hda_cs_dsp_coeff_ctl *ctl __free(kfree) = NULL;
1183233b978SStefan Binding int ret = 0;
1193233b978SStefan Binding
1203233b978SStefan Binding if (cs_ctl->len > ADSP_MAX_STD_CTRL_SIZE) {
1213233b978SStefan Binding dev_err(cs_ctl->dsp->dev, "KControl %s: length %zu exceeds maximum %d\n", name,
1223233b978SStefan Binding cs_ctl->len, ADSP_MAX_STD_CTRL_SIZE);
1232176c6b5SRichard Fitzgerald return;
1243233b978SStefan Binding }
1253233b978SStefan Binding
126*6e359be4SRichard Fitzgerald ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
127*6e359be4SRichard Fitzgerald if (!ctl)
128*6e359be4SRichard Fitzgerald return;
129*6e359be4SRichard Fitzgerald
130*6e359be4SRichard Fitzgerald ctl->cs_ctl = cs_ctl;
131*6e359be4SRichard Fitzgerald ctl->card = info->card;
132*6e359be4SRichard Fitzgerald
1333233b978SStefan Binding kcontrol.name = name;
1343233b978SStefan Binding kcontrol.info = hda_cs_dsp_coeff_info;
1353233b978SStefan Binding kcontrol.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1363233b978SStefan Binding kcontrol.access = wmfw_convert_flags(cs_ctl->flags);
1373233b978SStefan Binding kcontrol.get = hda_cs_dsp_coeff_get;
1383233b978SStefan Binding kcontrol.put = hda_cs_dsp_coeff_put;
1393233b978SStefan Binding
1403233b978SStefan Binding kctl = snd_ctl_new1(&kcontrol, (void *)ctl);
1412176c6b5SRichard Fitzgerald if (!kctl)
1422176c6b5SRichard Fitzgerald return;
1433233b978SStefan Binding
144*6e359be4SRichard Fitzgerald kctl->private_free = hda_cs_dsp_free_kcontrol;
145*6e359be4SRichard Fitzgerald ctl->kctl = kctl;
146*6e359be4SRichard Fitzgerald
147*6e359be4SRichard Fitzgerald /* snd_ctl_add() calls our private_free on error, which will kfree(ctl) */
148*6e359be4SRichard Fitzgerald cs_ctl->priv = no_free_ptr(ctl);
149*6e359be4SRichard Fitzgerald ret = snd_ctl_add(info->card, kctl);
1503233b978SStefan Binding if (ret) {
1513233b978SStefan Binding dev_err(cs_ctl->dsp->dev, "Failed to add KControl %s = %d\n", kcontrol.name, ret);
1522176c6b5SRichard Fitzgerald return;
1533233b978SStefan Binding }
1543233b978SStefan Binding
1553233b978SStefan Binding dev_dbg(cs_ctl->dsp->dev, "Added KControl: %s\n", kcontrol.name);
1563233b978SStefan Binding }
1573233b978SStefan Binding
hda_cs_dsp_control_add(struct cs_dsp_coeff_ctl * cs_ctl,const struct hda_cs_dsp_ctl_info * info)1582176c6b5SRichard Fitzgerald static void hda_cs_dsp_control_add(struct cs_dsp_coeff_ctl *cs_ctl,
1592176c6b5SRichard Fitzgerald const struct hda_cs_dsp_ctl_info *info)
1603233b978SStefan Binding {
1613233b978SStefan Binding struct cs_dsp *cs_dsp = cs_ctl->dsp;
1623233b978SStefan Binding char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1633233b978SStefan Binding const char *region_name;
1643233b978SStefan Binding int ret;
1653233b978SStefan Binding
1663233b978SStefan Binding region_name = cs_dsp_mem_region_name(cs_ctl->alg_region.type);
1673233b978SStefan Binding if (!region_name) {
1682176c6b5SRichard Fitzgerald dev_warn(cs_dsp->dev, "Unknown region type: %d\n", cs_ctl->alg_region.type);
1692176c6b5SRichard Fitzgerald return;
1703233b978SStefan Binding }
1713233b978SStefan Binding
1723233b978SStefan Binding ret = scnprintf(name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN, "%s %s %.12s %x", info->device_name,
1733233b978SStefan Binding cs_dsp->name, hda_cs_dsp_fw_text[info->fw_type], cs_ctl->alg_region.alg);
1743233b978SStefan Binding
1753233b978SStefan Binding if (cs_ctl->subname) {
1763233b978SStefan Binding int avail = SNDRV_CTL_ELEM_ID_NAME_MAXLEN - ret - 2;
1773233b978SStefan Binding int skip = 0;
1783233b978SStefan Binding
1793233b978SStefan Binding /* Truncate the subname from the start if it is too long */
1803233b978SStefan Binding if (cs_ctl->subname_len > avail)
1813233b978SStefan Binding skip = cs_ctl->subname_len - avail;
1823233b978SStefan Binding
1833233b978SStefan Binding snprintf(name + ret, SNDRV_CTL_ELEM_ID_NAME_MAXLEN - ret,
1843233b978SStefan Binding " %.*s", cs_ctl->subname_len - skip, cs_ctl->subname + skip);
1853233b978SStefan Binding }
1863233b978SStefan Binding
187*6e359be4SRichard Fitzgerald hda_cs_dsp_add_kcontrol(cs_ctl, info, name);
1883233b978SStefan Binding }
1893233b978SStefan Binding
hda_cs_dsp_add_controls(struct cs_dsp * dsp,const struct hda_cs_dsp_ctl_info * info)1902176c6b5SRichard Fitzgerald void hda_cs_dsp_add_controls(struct cs_dsp *dsp, const struct hda_cs_dsp_ctl_info *info)
1912176c6b5SRichard Fitzgerald {
1922176c6b5SRichard Fitzgerald struct cs_dsp_coeff_ctl *cs_ctl;
1932176c6b5SRichard Fitzgerald
1942176c6b5SRichard Fitzgerald /*
1952176c6b5SRichard Fitzgerald * pwr_lock would cause mutex inversion with ALSA control lock compared
1962176c6b5SRichard Fitzgerald * to the get/put functions.
1972176c6b5SRichard Fitzgerald * It is safe to walk the list without holding a mutex because entries
1982176c6b5SRichard Fitzgerald * are persistent and only cs_dsp_power_up() or cs_dsp_remove() can
1992176c6b5SRichard Fitzgerald * change the list.
2002176c6b5SRichard Fitzgerald */
2012176c6b5SRichard Fitzgerald lockdep_assert_not_held(&dsp->pwr_lock);
2022176c6b5SRichard Fitzgerald
2032176c6b5SRichard Fitzgerald list_for_each_entry(cs_ctl, &dsp->ctl_list, list) {
2042176c6b5SRichard Fitzgerald if (cs_ctl->flags & WMFW_CTL_FLAG_SYS)
2052176c6b5SRichard Fitzgerald continue;
2062176c6b5SRichard Fitzgerald
2072176c6b5SRichard Fitzgerald if (cs_ctl->priv)
2082176c6b5SRichard Fitzgerald continue;
2092176c6b5SRichard Fitzgerald
2102176c6b5SRichard Fitzgerald hda_cs_dsp_control_add(cs_ctl, info);
2113233b978SStefan Binding }
2122176c6b5SRichard Fitzgerald }
2132176c6b5SRichard Fitzgerald EXPORT_SYMBOL_NS_GPL(hda_cs_dsp_add_controls, SND_HDA_CS_DSP_CONTROLS);
2143233b978SStefan Binding
hda_cs_dsp_control_remove(struct cs_dsp_coeff_ctl * cs_ctl)2153233b978SStefan Binding void hda_cs_dsp_control_remove(struct cs_dsp_coeff_ctl *cs_ctl)
2163233b978SStefan Binding {
2173233b978SStefan Binding struct hda_cs_dsp_coeff_ctl *ctl = cs_ctl->priv;
2183233b978SStefan Binding
219*6e359be4SRichard Fitzgerald /* ctl and kctl may already have been removed by ALSA private_free */
220*6e359be4SRichard Fitzgerald if (ctl && ctl->kctl)
221*6e359be4SRichard Fitzgerald snd_ctl_remove(ctl->card, ctl->kctl);
2223233b978SStefan Binding }
2233233b978SStefan Binding EXPORT_SYMBOL_NS_GPL(hda_cs_dsp_control_remove, SND_HDA_CS_DSP_CONTROLS);
2243233b978SStefan Binding
hda_cs_dsp_write_ctl(struct cs_dsp * dsp,const char * name,int type,unsigned int alg,const void * buf,size_t len)225e414b05eSStefan Binding int hda_cs_dsp_write_ctl(struct cs_dsp *dsp, const char *name, int type,
226e414b05eSStefan Binding unsigned int alg, const void *buf, size_t len)
227e414b05eSStefan Binding {
22806f3a0a7SStefan Binding struct cs_dsp_coeff_ctl *cs_ctl;
229e414b05eSStefan Binding struct hda_cs_dsp_coeff_ctl *ctl;
230e414b05eSStefan Binding int ret;
231e414b05eSStefan Binding
23206f3a0a7SStefan Binding mutex_lock(&dsp->pwr_lock);
23306f3a0a7SStefan Binding cs_ctl = cs_dsp_get_ctl(dsp, name, type, alg);
234e414b05eSStefan Binding ret = cs_dsp_coeff_write_ctrl(cs_ctl, 0, buf, len);
23506f3a0a7SStefan Binding mutex_unlock(&dsp->pwr_lock);
236943f4e64SVitaly Rodionov if (ret < 0)
237e414b05eSStefan Binding return ret;
238e414b05eSStefan Binding
239943f4e64SVitaly Rodionov if (ret == 0 || (cs_ctl->flags & WMFW_CTL_FLAG_SYS))
240e414b05eSStefan Binding return 0;
241e414b05eSStefan Binding
24249b0dea1SStefan Binding ctl = cs_ctl->priv;
24349b0dea1SStefan Binding
244e414b05eSStefan Binding snd_ctl_notify(ctl->card, SNDRV_CTL_EVENT_MASK_VALUE, &ctl->kctl->id);
245e414b05eSStefan Binding
246e414b05eSStefan Binding return 0;
247e414b05eSStefan Binding }
248e414b05eSStefan Binding EXPORT_SYMBOL_NS_GPL(hda_cs_dsp_write_ctl, SND_HDA_CS_DSP_CONTROLS);
249e414b05eSStefan Binding
hda_cs_dsp_read_ctl(struct cs_dsp * dsp,const char * name,int type,unsigned int alg,void * buf,size_t len)250e414b05eSStefan Binding int hda_cs_dsp_read_ctl(struct cs_dsp *dsp, const char *name, int type,
251e414b05eSStefan Binding unsigned int alg, void *buf, size_t len)
252e414b05eSStefan Binding {
25306f3a0a7SStefan Binding int ret;
25406f3a0a7SStefan Binding
25506f3a0a7SStefan Binding mutex_lock(&dsp->pwr_lock);
25606f3a0a7SStefan Binding ret = cs_dsp_coeff_read_ctrl(cs_dsp_get_ctl(dsp, name, type, alg), 0, buf, len);
25706f3a0a7SStefan Binding mutex_unlock(&dsp->pwr_lock);
25806f3a0a7SStefan Binding
25906f3a0a7SStefan Binding return ret;
260e414b05eSStefan Binding
261e414b05eSStefan Binding }
262e414b05eSStefan Binding EXPORT_SYMBOL_NS_GPL(hda_cs_dsp_read_ctl, SND_HDA_CS_DSP_CONTROLS);
263e414b05eSStefan Binding
2643233b978SStefan Binding MODULE_DESCRIPTION("CS_DSP ALSA Control HDA Library");
2653233b978SStefan Binding MODULE_AUTHOR("Stefan Binding, <sbinding@opensource.cirrus.com>");
2663233b978SStefan Binding MODULE_LICENSE("GPL");
267e57d904aSRichard Fitzgerald MODULE_IMPORT_NS(FW_CS_DSP);
268