Skip to content

Commit 07f7d6e

Browse files
rfvirgilbroonie
authored andcommitted
ASoC: cs35l56: Fix for initializing ASP1 mixer registers
Defer initializing the state of the ASP1 mixer registers until the firmware has been downloaded and rebooted. On a SoundWire system the ASP is free for use as a chip-to-chip interconnect. This can be either for the firmware on multiple CS35L56 to share reference audio; or as a bridge to another device. If it is a firmware interconnect it is owned by the firmware and the Linux driver should avoid writing the registers. However, if it is a bridge then Linux may take over and handle it as a normal codec-to-codec link. Even if the ASP is used as a firmware-firmware interconnect it is useful to have ALSA controls for the ASP mixer. They are at least useful for debugging. CS35L56 is designed for SDCA and a generic SDCA driver would know nothing about these chip-specific registers. So if the ASP is being used on a SoundWire system the firmware sets up the ASP mixer registers. This means that we can't assume the default state of these registers. But we don't know the initial state that the firmware set them to until after the firmware has been downloaded and booted, which can take several seconds when downloading multiple amps. DAPM normally reads the initial state of mux registers during probe() but this would mean blocking probe() for several seconds until the firmware has initialized them. To avoid this, the mixer muxes are set SND_SOC_NOPM to prevent DAPM trying to read the register state. Custom get/set callbacks are implemented for ALSA control access, and these can safely block waiting for the firmware download. After the firmware download has completed, the state of the mux registers is known so a work job is queued to call snd_soc_dapm_mux_update_power() on each of the mux widgets. Backport note: This won't apply cleanly to kernels older than v6.6. Signed-off-by: Richard Fitzgerald <[email protected]> Fixes: e496112 ("ASoC: cs35l56: Add driver for Cirrus Logic CS35L56") Link: https://msgid.link/r/[email protected] Signed-off-by: Mark Brown <[email protected]>
1 parent 856ce89 commit 07f7d6e

File tree

3 files changed

+163
-17
lines changed

3 files changed

+163
-17
lines changed

sound/soc/codecs/cs35l56-shared.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,9 @@ static const struct reg_default cs35l56_reg_defaults[] = {
4343
{ CS35L56_ASP1_FRAME_CONTROL5, 0x00020100 },
4444
{ CS35L56_ASP1_DATA_CONTROL1, 0x00000018 },
4545
{ CS35L56_ASP1_DATA_CONTROL5, 0x00000018 },
46-
{ CS35L56_ASP1TX1_INPUT, 0x00000018 },
47-
{ CS35L56_ASP1TX2_INPUT, 0x00000019 },
48-
{ CS35L56_ASP1TX3_INPUT, 0x00000020 },
49-
{ CS35L56_ASP1TX4_INPUT, 0x00000028 },
46+
47+
/* no defaults for ASP1TX mixer */
48+
5049
{ CS35L56_SWIRE_DP3_CH1_INPUT, 0x00000018 },
5150
{ CS35L56_SWIRE_DP3_CH2_INPUT, 0x00000019 },
5251
{ CS35L56_SWIRE_DP3_CH3_INPUT, 0x00000029 },

sound/soc/codecs/cs35l56.c

Lines changed: 159 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,135 @@ static int cs35l56_dspwait_put_volsw(struct snd_kcontrol *kcontrol,
5959
return snd_soc_put_volsw(kcontrol, ucontrol);
6060
}
6161

62+
static const unsigned short cs35l56_asp1_mixer_regs[] = {
63+
CS35L56_ASP1TX1_INPUT, CS35L56_ASP1TX2_INPUT,
64+
CS35L56_ASP1TX3_INPUT, CS35L56_ASP1TX4_INPUT,
65+
};
66+
67+
static const char * const cs35l56_asp1_mux_control_names[] = {
68+
"ASP1 TX1 Source", "ASP1 TX2 Source", "ASP1 TX3 Source", "ASP1 TX4 Source"
69+
};
70+
71+
static int cs35l56_dspwait_asp1tx_get(struct snd_kcontrol *kcontrol,
72+
struct snd_ctl_elem_value *ucontrol)
73+
{
74+
struct snd_soc_component *component = snd_soc_dapm_kcontrol_component(kcontrol);
75+
struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
76+
struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
77+
int index = e->shift_l;
78+
unsigned int addr, val;
79+
int ret;
80+
81+
/* Wait for mux to be initialized */
82+
cs35l56_wait_dsp_ready(cs35l56);
83+
flush_work(&cs35l56->mux_init_work);
84+
85+
addr = cs35l56_asp1_mixer_regs[index];
86+
ret = regmap_read(cs35l56->base.regmap, addr, &val);
87+
if (ret)
88+
return ret;
89+
90+
val &= CS35L56_ASP_TXn_SRC_MASK;
91+
ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
92+
93+
return 0;
94+
}
95+
96+
static int cs35l56_dspwait_asp1tx_put(struct snd_kcontrol *kcontrol,
97+
struct snd_ctl_elem_value *ucontrol)
98+
{
99+
struct snd_soc_component *component = snd_soc_dapm_kcontrol_component(kcontrol);
100+
struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
101+
struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
102+
struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
103+
int item = ucontrol->value.enumerated.item[0];
104+
int index = e->shift_l;
105+
unsigned int addr, val;
106+
bool changed;
107+
int ret;
108+
109+
/* Wait for mux to be initialized */
110+
cs35l56_wait_dsp_ready(cs35l56);
111+
flush_work(&cs35l56->mux_init_work);
112+
113+
addr = cs35l56_asp1_mixer_regs[index];
114+
val = snd_soc_enum_item_to_val(e, item);
115+
116+
ret = regmap_update_bits_check(cs35l56->base.regmap, addr,
117+
CS35L56_ASP_TXn_SRC_MASK, val, &changed);
118+
if (!ret)
119+
return ret;
120+
121+
if (changed)
122+
snd_soc_dapm_mux_update_power(dapm, kcontrol, item, e, NULL);
123+
124+
return changed;
125+
}
126+
127+
static void cs35l56_mark_asp1_mixer_widgets_dirty(struct cs35l56_private *cs35l56)
128+
{
129+
struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cs35l56->component);
130+
const char *prefix = cs35l56->component->name_prefix;
131+
char full_name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
132+
const char *name;
133+
struct snd_kcontrol *kcontrol;
134+
struct soc_enum *e;
135+
unsigned int val[4];
136+
int i, item, ret;
137+
138+
/*
139+
* Resume so we can read the registers from silicon if the regmap
140+
* cache has not yet been populated.
141+
*/
142+
ret = pm_runtime_resume_and_get(cs35l56->base.dev);
143+
if (ret < 0)
144+
return;
145+
146+
ret = regmap_bulk_read(cs35l56->base.regmap, CS35L56_ASP1TX1_INPUT,
147+
val, ARRAY_SIZE(val));
148+
149+
pm_runtime_mark_last_busy(cs35l56->base.dev);
150+
pm_runtime_put_autosuspend(cs35l56->base.dev);
151+
152+
if (ret) {
153+
dev_err(cs35l56->base.dev, "Failed to read ASP1 mixer regs: %d\n", ret);
154+
return;
155+
}
156+
157+
snd_soc_card_mutex_lock(dapm->card);
158+
WARN_ON(!dapm->card->instantiated);
159+
160+
for (i = 0; i < ARRAY_SIZE(cs35l56_asp1_mux_control_names); ++i) {
161+
name = cs35l56_asp1_mux_control_names[i];
162+
163+
if (prefix) {
164+
snprintf(full_name, sizeof(full_name), "%s %s", prefix, name);
165+
name = full_name;
166+
}
167+
168+
kcontrol = snd_soc_card_get_kcontrol(dapm->card, name);
169+
if (!kcontrol) {
170+
dev_warn(cs35l56->base.dev, "Could not find control %s\n", name);
171+
continue;
172+
}
173+
174+
e = (struct soc_enum *)kcontrol->private_value;
175+
item = snd_soc_enum_val_to_item(e, val[i] & CS35L56_ASP_TXn_SRC_MASK);
176+
snd_soc_dapm_mux_update_power(dapm, kcontrol, item, e, NULL);
177+
}
178+
179+
snd_soc_card_mutex_unlock(dapm->card);
180+
}
181+
182+
static void cs35l56_mux_init_work(struct work_struct *work)
183+
{
184+
struct cs35l56_private *cs35l56 = container_of(work,
185+
struct cs35l56_private,
186+
mux_init_work);
187+
188+
cs35l56_mark_asp1_mixer_widgets_dirty(cs35l56);
189+
}
190+
62191
static DECLARE_TLV_DB_SCALE(vol_tlv, -10000, 25, 0);
63192

64193
static const struct snd_kcontrol_new cs35l56_controls[] = {
@@ -77,40 +206,44 @@ static const struct snd_kcontrol_new cs35l56_controls[] = {
77206
};
78207

79208
static SOC_VALUE_ENUM_SINGLE_DECL(cs35l56_asp1tx1_enum,
80-
CS35L56_ASP1TX1_INPUT,
81-
0, CS35L56_ASP_TXn_SRC_MASK,
209+
SND_SOC_NOPM,
210+
0, 0,
82211
cs35l56_tx_input_texts,
83212
cs35l56_tx_input_values);
84213

85214
static const struct snd_kcontrol_new asp1_tx1_mux =
86-
SOC_DAPM_ENUM("ASP1TX1 SRC", cs35l56_asp1tx1_enum);
215+
SOC_DAPM_ENUM_EXT("ASP1TX1 SRC", cs35l56_asp1tx1_enum,
216+
cs35l56_dspwait_asp1tx_get, cs35l56_dspwait_asp1tx_put);
87217

88218
static SOC_VALUE_ENUM_SINGLE_DECL(cs35l56_asp1tx2_enum,
89-
CS35L56_ASP1TX2_INPUT,
90-
0, CS35L56_ASP_TXn_SRC_MASK,
219+
SND_SOC_NOPM,
220+
1, 0,
91221
cs35l56_tx_input_texts,
92222
cs35l56_tx_input_values);
93223

94224
static const struct snd_kcontrol_new asp1_tx2_mux =
95-
SOC_DAPM_ENUM("ASP1TX2 SRC", cs35l56_asp1tx2_enum);
225+
SOC_DAPM_ENUM_EXT("ASP1TX2 SRC", cs35l56_asp1tx2_enum,
226+
cs35l56_dspwait_asp1tx_get, cs35l56_dspwait_asp1tx_put);
96227

97228
static SOC_VALUE_ENUM_SINGLE_DECL(cs35l56_asp1tx3_enum,
98-
CS35L56_ASP1TX3_INPUT,
99-
0, CS35L56_ASP_TXn_SRC_MASK,
229+
SND_SOC_NOPM,
230+
2, 0,
100231
cs35l56_tx_input_texts,
101232
cs35l56_tx_input_values);
102233

103234
static const struct snd_kcontrol_new asp1_tx3_mux =
104-
SOC_DAPM_ENUM("ASP1TX3 SRC", cs35l56_asp1tx3_enum);
235+
SOC_DAPM_ENUM_EXT("ASP1TX3 SRC", cs35l56_asp1tx3_enum,
236+
cs35l56_dspwait_asp1tx_get, cs35l56_dspwait_asp1tx_put);
105237

106238
static SOC_VALUE_ENUM_SINGLE_DECL(cs35l56_asp1tx4_enum,
107-
CS35L56_ASP1TX4_INPUT,
108-
0, CS35L56_ASP_TXn_SRC_MASK,
239+
SND_SOC_NOPM,
240+
3, 0,
109241
cs35l56_tx_input_texts,
110242
cs35l56_tx_input_values);
111243

112244
static const struct snd_kcontrol_new asp1_tx4_mux =
113-
SOC_DAPM_ENUM("ASP1TX4 SRC", cs35l56_asp1tx4_enum);
245+
SOC_DAPM_ENUM_EXT("ASP1TX4 SRC", cs35l56_asp1tx4_enum,
246+
cs35l56_dspwait_asp1tx_get, cs35l56_dspwait_asp1tx_put);
114247

115248
static SOC_VALUE_ENUM_SINGLE_DECL(cs35l56_sdw1tx1_enum,
116249
CS35L56_SWIRE_DP3_CH1_INPUT,
@@ -785,6 +918,15 @@ static void cs35l56_dsp_work(struct work_struct *work)
785918
else
786919
cs35l56_patch(cs35l56);
787920

921+
922+
/*
923+
* Set starting value of ASP1 mux widgets. Updating a mux takes
924+
* the DAPM mutex. Post this to a separate job so that DAPM
925+
* power-up can wait for dsp_work to complete without deadlocking
926+
* on the DAPM mutex.
927+
*/
928+
queue_work(cs35l56->dsp_wq, &cs35l56->mux_init_work);
929+
788930
pm_runtime_mark_last_busy(cs35l56->base.dev);
789931
pm_runtime_put_autosuspend(cs35l56->base.dev);
790932
}
@@ -830,6 +972,7 @@ static void cs35l56_component_remove(struct snd_soc_component *component)
830972
struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
831973

832974
cancel_work_sync(&cs35l56->dsp_work);
975+
cancel_work_sync(&cs35l56->mux_init_work);
833976

834977
if (cs35l56->dsp.cs_dsp.booted)
835978
wm_adsp_power_down(&cs35l56->dsp);
@@ -897,8 +1040,10 @@ int cs35l56_system_suspend(struct device *dev)
8971040

8981041
dev_dbg(dev, "system_suspend\n");
8991042

900-
if (cs35l56->component)
1043+
if (cs35l56->component) {
9011044
flush_work(&cs35l56->dsp_work);
1045+
cancel_work_sync(&cs35l56->mux_init_work);
1046+
}
9021047

9031048
/*
9041049
* The interrupt line is normally shared, but after we start suspending
@@ -1049,6 +1194,7 @@ static int cs35l56_dsp_init(struct cs35l56_private *cs35l56)
10491194
return -ENOMEM;
10501195

10511196
INIT_WORK(&cs35l56->dsp_work, cs35l56_dsp_work);
1197+
INIT_WORK(&cs35l56->mux_init_work, cs35l56_mux_init_work);
10521198

10531199
dsp = &cs35l56->dsp;
10541200
cs35l56_init_cs_dsp(&cs35l56->base, &dsp->cs_dsp);

sound/soc/codecs/cs35l56.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ struct cs35l56_private {
3434
struct wm_adsp dsp; /* must be first member */
3535
struct cs35l56_base base;
3636
struct work_struct dsp_work;
37+
struct work_struct mux_init_work;
3738
struct workqueue_struct *dsp_wq;
3839
struct snd_soc_component *component;
3940
struct regulator_bulk_data supplies[CS35L56_NUM_BULK_SUPPLIES];

0 commit comments

Comments
 (0)