xref: /linux/sound/soc/codecs/cs35l56.c (revision c22407252a2421286998323831095e6b8a1d9532)
1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // Driver for Cirrus Logic CS35L56 smart amp
4 //
5 // Copyright (C) 2023 Cirrus Logic, Inc. and
6 //                    Cirrus Logic International Semiconductor Ltd.
7 
8 #include <kunit/static_stub.h>
9 #include <kunit/visibility.h>
10 #include <linux/acpi.h>
11 #include <linux/array_size.h>
12 #include <linux/completion.h>
13 #include <linux/debugfs.h>
14 #include <linux/delay.h>
15 #include <linux/device.h>
16 #include <linux/err.h>
17 #include <linux/gpio/consumer.h>
18 #include <linux/interrupt.h>
19 #include <linux/math.h>
20 #include <linux/module.h>
21 #include <linux/pm.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/property.h>
24 #include <linux/regmap.h>
25 #include <linux/regulator/consumer.h>
26 #include <linux/slab.h>
27 #include <linux/soundwire/sdw.h>
28 #include <linux/types.h>
29 #include <linux/workqueue.h>
30 #include <sound/cs-amp-lib.h>
31 #include <sound/pcm.h>
32 #include <sound/pcm_params.h>
33 #include <sound/soc.h>
34 #include <sound/soc-dapm.h>
35 #include <sound/tlv.h>
36 
37 #include "wm_adsp.h"
38 #include "cs35l56.h"
39 
40 static int cs35l56_dsp_event(struct snd_soc_dapm_widget *w,
41 			     struct snd_kcontrol *kcontrol, int event);
42 
43 static void cs35l56_wait_dsp_ready(struct cs35l56_private *cs35l56)
44 {
45 	/* Wait for patching to complete */
46 	flush_work(&cs35l56->dsp_work);
47 }
48 
49 static int cs35l56_dspwait_get_volsw(struct snd_kcontrol *kcontrol,
50 				     struct snd_ctl_elem_value *ucontrol)
51 {
52 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
53 	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
54 
55 	cs35l56_wait_dsp_ready(cs35l56);
56 	return snd_soc_get_volsw(kcontrol, ucontrol);
57 }
58 
59 static int cs35l56_dspwait_put_volsw(struct snd_kcontrol *kcontrol,
60 				     struct snd_ctl_elem_value *ucontrol)
61 {
62 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
63 	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
64 
65 	cs35l56_wait_dsp_ready(cs35l56);
66 	return snd_soc_put_volsw(kcontrol, ucontrol);
67 }
68 
69 static DECLARE_TLV_DB_SCALE(vol_tlv, -10000, 25, 0);
70 
71 static SOC_ENUM_SINGLE_DECL(cs35l56_cal_set_status_enum, SND_SOC_NOPM, 0,
72 			    cs35l56_cal_set_status_text);
73 
74 static int cs35l56_cal_set_status_ctl_get(struct snd_kcontrol *kcontrol,
75 					  struct snd_ctl_elem_value *ucontrol)
76 {
77 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
78 	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
79 
80 	return cs35l56_cal_set_status_get(&cs35l56->base, ucontrol);
81 }
82 
83 static const struct snd_kcontrol_new cs35l56_controls[] = {
84 	SOC_SINGLE_EXT("Speaker Switch",
85 		       CS35L56_MAIN_RENDER_USER_MUTE, 0, 1, 1,
86 		       cs35l56_dspwait_get_volsw, cs35l56_dspwait_put_volsw),
87 	SOC_SINGLE_S_EXT_TLV("Speaker Volume",
88 			     CS35L56_MAIN_RENDER_USER_VOLUME,
89 			     CS35L56_MAIN_RENDER_USER_VOLUME_SHIFT,
90 			     CS35L56_MAIN_RENDER_USER_VOLUME_MIN,
91 			     CS35L56_MAIN_RENDER_USER_VOLUME_MAX,
92 			     CS35L56_MAIN_RENDER_USER_VOLUME_SIGNBIT,
93 			     0,
94 			     cs35l56_dspwait_get_volsw,
95 			     cs35l56_dspwait_put_volsw,
96 			     vol_tlv),
97 	SOC_SINGLE_EXT("Posture Number", CS35L56_MAIN_POSTURE_NUMBER,
98 		       0, 255, 0,
99 		       cs35l56_dspwait_get_volsw, cs35l56_dspwait_put_volsw),
100 	SOC_ENUM_EXT_ACC("CAL_SET_STATUS", cs35l56_cal_set_status_enum,
101 			 cs35l56_cal_set_status_ctl_get, NULL,
102 			 SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE),
103 };
104 
105 static const struct snd_kcontrol_new cs35l63_controls[] = {
106 	SOC_SINGLE_EXT("Speaker Switch",
107 		       CS35L63_MAIN_RENDER_USER_MUTE, 0, 1, 1,
108 		       cs35l56_dspwait_get_volsw, cs35l56_dspwait_put_volsw),
109 	SOC_SINGLE_S_EXT_TLV("Speaker Volume",
110 			     CS35L63_MAIN_RENDER_USER_VOLUME,
111 			     CS35L56_MAIN_RENDER_USER_VOLUME_SHIFT,
112 			     CS35L56_MAIN_RENDER_USER_VOLUME_MIN,
113 			     CS35L56_MAIN_RENDER_USER_VOLUME_MAX,
114 			     CS35L56_MAIN_RENDER_USER_VOLUME_SIGNBIT,
115 			     0,
116 			     cs35l56_dspwait_get_volsw,
117 			     cs35l56_dspwait_put_volsw,
118 			     vol_tlv),
119 	SOC_SINGLE_EXT("Posture Number", CS35L63_MAIN_POSTURE_NUMBER,
120 		       0, 255, 0,
121 		       cs35l56_dspwait_get_volsw, cs35l56_dspwait_put_volsw),
122 	SOC_ENUM_EXT_ACC("CAL_SET_STATUS", cs35l56_cal_set_status_enum,
123 			 cs35l56_cal_set_status_ctl_get, NULL,
124 			 SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE),
125 };
126 
127 static SOC_VALUE_ENUM_SINGLE_DECL(cs35l56_asp1tx1_enum,
128 				  CS35L56_ASP1TX1_INPUT,
129 				  0, CS35L56_ASP_TXn_SRC_MASK,
130 				  cs35l56_tx_input_texts,
131 				  cs35l56_tx_input_values);
132 
133 static const struct snd_kcontrol_new asp1_tx1_mux =
134 	SOC_DAPM_ENUM("ASP1TX1 SRC", cs35l56_asp1tx1_enum);
135 
136 static SOC_VALUE_ENUM_SINGLE_DECL(cs35l56_asp1tx2_enum,
137 				  CS35L56_ASP1TX2_INPUT,
138 				  0, CS35L56_ASP_TXn_SRC_MASK,
139 				  cs35l56_tx_input_texts,
140 				  cs35l56_tx_input_values);
141 
142 static const struct snd_kcontrol_new asp1_tx2_mux =
143 	SOC_DAPM_ENUM("ASP1TX2 SRC", cs35l56_asp1tx2_enum);
144 
145 static SOC_VALUE_ENUM_SINGLE_DECL(cs35l56_asp1tx3_enum,
146 				  CS35L56_ASP1TX3_INPUT,
147 				  0, CS35L56_ASP_TXn_SRC_MASK,
148 				  cs35l56_tx_input_texts,
149 				  cs35l56_tx_input_values);
150 
151 static const struct snd_kcontrol_new asp1_tx3_mux =
152 	SOC_DAPM_ENUM("ASP1TX3 SRC", cs35l56_asp1tx3_enum);
153 
154 static SOC_VALUE_ENUM_SINGLE_DECL(cs35l56_asp1tx4_enum,
155 				  CS35L56_ASP1TX4_INPUT,
156 				  0, CS35L56_ASP_TXn_SRC_MASK,
157 				  cs35l56_tx_input_texts,
158 				  cs35l56_tx_input_values);
159 
160 static const struct snd_kcontrol_new asp1_tx4_mux =
161 	SOC_DAPM_ENUM("ASP1TX4 SRC", cs35l56_asp1tx4_enum);
162 
163 static SOC_VALUE_ENUM_SINGLE_DECL(cs35l56_sdw1tx1_enum,
164 				CS35L56_SWIRE_DP3_CH1_INPUT,
165 				0, CS35L56_SWIRETXn_SRC_MASK,
166 				cs35l56_tx_input_texts,
167 				cs35l56_tx_input_values);
168 
169 static const struct snd_kcontrol_new sdw1_tx1_mux =
170 	SOC_DAPM_ENUM("SDW1TX1 SRC", cs35l56_sdw1tx1_enum);
171 
172 static SOC_VALUE_ENUM_SINGLE_DECL(cs35l56_sdw1tx2_enum,
173 				CS35L56_SWIRE_DP3_CH2_INPUT,
174 				0, CS35L56_SWIRETXn_SRC_MASK,
175 				cs35l56_tx_input_texts,
176 				cs35l56_tx_input_values);
177 
178 static const struct snd_kcontrol_new sdw1_tx2_mux =
179 	SOC_DAPM_ENUM("SDW1TX2 SRC", cs35l56_sdw1tx2_enum);
180 
181 static SOC_VALUE_ENUM_SINGLE_DECL(cs35l56_sdw1tx3_enum,
182 				CS35L56_SWIRE_DP3_CH3_INPUT,
183 				0, CS35L56_SWIRETXn_SRC_MASK,
184 				cs35l56_tx_input_texts,
185 				cs35l56_tx_input_values);
186 
187 static const struct snd_kcontrol_new sdw1_tx3_mux =
188 	SOC_DAPM_ENUM("SDW1TX3 SRC", cs35l56_sdw1tx3_enum);
189 
190 static SOC_VALUE_ENUM_SINGLE_DECL(cs35l56_sdw1tx4_enum,
191 				CS35L56_SWIRE_DP3_CH4_INPUT,
192 				0, CS35L56_SWIRETXn_SRC_MASK,
193 				cs35l56_tx_input_texts,
194 				cs35l56_tx_input_values);
195 
196 static const struct snd_kcontrol_new sdw1_tx4_mux =
197 	SOC_DAPM_ENUM("SDW1TX4 SRC", cs35l56_sdw1tx4_enum);
198 
199 static int cs35l56_play_event(struct snd_soc_dapm_widget *w,
200 			      struct snd_kcontrol *kcontrol, int event)
201 {
202 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
203 	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
204 	unsigned int val;
205 	int ret;
206 
207 	dev_dbg(cs35l56->base.dev, "play: %d\n", event);
208 
209 	switch (event) {
210 	case SND_SOC_DAPM_PRE_PMU:
211 		/* Don't wait for ACK, we check in POST_PMU that it completed */
212 		return regmap_write(cs35l56->base.regmap, CS35L56_DSP_VIRTUAL1_MBOX_1,
213 				    CS35L56_MBOX_CMD_AUDIO_PLAY);
214 	case SND_SOC_DAPM_POST_PMU:
215 		/* Wait for firmware to enter PS0 power state */
216 		ret = regmap_read_poll_timeout(cs35l56->base.regmap,
217 					       cs35l56->base.fw_reg->transducer_actual_ps,
218 					       val, (val == CS35L56_PS0),
219 					       CS35L56_PS0_POLL_US,
220 					       CS35L56_PS0_TIMEOUT_US);
221 		if (ret)
222 			dev_err(cs35l56->base.dev, "PS0 wait failed: %d\n", ret);
223 		return ret;
224 	case SND_SOC_DAPM_POST_PMD:
225 		return cs35l56_mbox_send(&cs35l56->base, CS35L56_MBOX_CMD_AUDIO_PAUSE);
226 	default:
227 		return 0;
228 	}
229 }
230 
231 static const struct snd_soc_dapm_widget cs35l56_dapm_widgets[] = {
232 	SND_SOC_DAPM_REGULATOR_SUPPLY("VDD_B", 0, 0),
233 	SND_SOC_DAPM_REGULATOR_SUPPLY("VDD_AMP", 0, 0),
234 
235 	SND_SOC_DAPM_SUPPLY("PLAY", SND_SOC_NOPM, 0, 0, cs35l56_play_event,
236 			    SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
237 
238 	SND_SOC_DAPM_OUT_DRV("AMP", SND_SOC_NOPM, 0, 0, NULL, 0),
239 	SND_SOC_DAPM_OUTPUT("SPK"),
240 
241 	SND_SOC_DAPM_PGA_E("DSP1", SND_SOC_NOPM, 0, 0, NULL, 0, cs35l56_dsp_event,
242 			   SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
243 
244 	SND_SOC_DAPM_AIF_IN("ASP1RX1", NULL, 0, CS35L56_ASP1_ENABLES1,
245 			    CS35L56_ASP_RX1_EN_SHIFT, 0),
246 	SND_SOC_DAPM_AIF_IN("ASP1RX2", NULL, 1, CS35L56_ASP1_ENABLES1,
247 			    CS35L56_ASP_RX2_EN_SHIFT, 0),
248 	SND_SOC_DAPM_AIF_OUT("ASP1TX1", NULL, 0, CS35L56_ASP1_ENABLES1,
249 			     CS35L56_ASP_TX1_EN_SHIFT, 0),
250 	SND_SOC_DAPM_AIF_OUT("ASP1TX2", NULL, 1, CS35L56_ASP1_ENABLES1,
251 			     CS35L56_ASP_TX2_EN_SHIFT, 0),
252 	SND_SOC_DAPM_AIF_OUT("ASP1TX3", NULL, 2, CS35L56_ASP1_ENABLES1,
253 			     CS35L56_ASP_TX3_EN_SHIFT, 0),
254 	SND_SOC_DAPM_AIF_OUT("ASP1TX4", NULL, 3, CS35L56_ASP1_ENABLES1,
255 			     CS35L56_ASP_TX4_EN_SHIFT, 0),
256 
257 	SND_SOC_DAPM_MUX("ASP1 TX1 Source", SND_SOC_NOPM, 0, 0, &asp1_tx1_mux),
258 	SND_SOC_DAPM_MUX("ASP1 TX2 Source", SND_SOC_NOPM, 0, 0, &asp1_tx2_mux),
259 	SND_SOC_DAPM_MUX("ASP1 TX3 Source", SND_SOC_NOPM, 0, 0, &asp1_tx3_mux),
260 	SND_SOC_DAPM_MUX("ASP1 TX4 Source", SND_SOC_NOPM, 0, 0, &asp1_tx4_mux),
261 
262 	SND_SOC_DAPM_MUX("SDW1 TX1 Source", SND_SOC_NOPM, 0, 0, &sdw1_tx1_mux),
263 	SND_SOC_DAPM_MUX("SDW1 TX2 Source", SND_SOC_NOPM, 0, 0, &sdw1_tx2_mux),
264 	SND_SOC_DAPM_MUX("SDW1 TX3 Source", SND_SOC_NOPM, 0, 0, &sdw1_tx3_mux),
265 	SND_SOC_DAPM_MUX("SDW1 TX4 Source", SND_SOC_NOPM, 0, 0, &sdw1_tx4_mux),
266 
267 	SND_SOC_DAPM_SIGGEN("VMON ADC"),
268 	SND_SOC_DAPM_SIGGEN("IMON ADC"),
269 	SND_SOC_DAPM_SIGGEN("ERRVOL ADC"),
270 	SND_SOC_DAPM_SIGGEN("CLASSH ADC"),
271 	SND_SOC_DAPM_SIGGEN("VDDBMON ADC"),
272 	SND_SOC_DAPM_SIGGEN("VBSTMON ADC"),
273 	SND_SOC_DAPM_SIGGEN("TEMPMON ADC"),
274 
275 	SND_SOC_DAPM_INPUT("Calibrate"),
276 };
277 
278 #define CS35L56_SRC_ROUTE(name) \
279 	{ name" Source", "ASP1RX1", "ASP1RX1" }, \
280 	{ name" Source", "ASP1RX2", "ASP1RX2" }, \
281 	{ name" Source", "VMON", "VMON ADC" }, \
282 	{ name" Source", "IMON", "IMON ADC" }, \
283 	{ name" Source", "ERRVOL", "ERRVOL ADC" },   \
284 	{ name" Source", "CLASSH", "CLASSH ADC" },   \
285 	{ name" Source", "VDDBMON", "VDDBMON ADC" }, \
286 	{ name" Source", "VBSTMON", "VBSTMON ADC" }, \
287 	{ name" Source", "DSP1TX1", "DSP1" }, \
288 	{ name" Source", "DSP1TX2", "DSP1" }, \
289 	{ name" Source", "DSP1TX3", "DSP1" }, \
290 	{ name" Source", "DSP1TX4", "DSP1" }, \
291 	{ name" Source", "DSP1TX5", "DSP1" }, \
292 	{ name" Source", "DSP1TX6", "DSP1" }, \
293 	{ name" Source", "DSP1TX7", "DSP1" }, \
294 	{ name" Source", "DSP1TX8", "DSP1" }, \
295 	{ name" Source", "TEMPMON", "TEMPMON ADC" }, \
296 	{ name" Source", "INTERPOLATOR", "AMP" }, \
297 	{ name" Source", "SDW1RX1", "SDW1 Playback" }, \
298 	{ name" Source", "SDW1RX2", "SDW1 Playback" },
299 
300 static const struct snd_soc_dapm_route cs35l56_audio_map[] = {
301 	{ "AMP", NULL, "VDD_B" },
302 	{ "AMP", NULL, "VDD_AMP" },
303 
304 	{ "ASP1 Playback", NULL, "PLAY" },
305 	{ "SDW1 Playback", NULL, "PLAY" },
306 
307 	{ "ASP1RX1", NULL, "ASP1 Playback" },
308 	{ "ASP1RX2", NULL, "ASP1 Playback" },
309 	{ "DSP1", NULL, "ASP1RX1" },
310 	{ "DSP1", NULL, "ASP1RX2" },
311 	{ "DSP1", NULL, "SDW1 Playback" },
312 	{ "DSP1", NULL, "Calibrate" },
313 	{ "AMP", NULL, "DSP1" },
314 	{ "SPK", NULL, "AMP" },
315 
316 	CS35L56_SRC_ROUTE("ASP1 TX1")
317 	CS35L56_SRC_ROUTE("ASP1 TX2")
318 	CS35L56_SRC_ROUTE("ASP1 TX3")
319 	CS35L56_SRC_ROUTE("ASP1 TX4")
320 
321 	{ "ASP1TX1", NULL, "ASP1 TX1 Source" },
322 	{ "ASP1TX2", NULL, "ASP1 TX2 Source" },
323 	{ "ASP1TX3", NULL, "ASP1 TX3 Source" },
324 	{ "ASP1TX4", NULL, "ASP1 TX4 Source" },
325 	{ "ASP1 Capture", NULL, "ASP1TX1" },
326 	{ "ASP1 Capture", NULL, "ASP1TX2" },
327 	{ "ASP1 Capture", NULL, "ASP1TX3" },
328 	{ "ASP1 Capture", NULL, "ASP1TX4" },
329 
330 	CS35L56_SRC_ROUTE("SDW1 TX1")
331 	CS35L56_SRC_ROUTE("SDW1 TX2")
332 	CS35L56_SRC_ROUTE("SDW1 TX3")
333 	CS35L56_SRC_ROUTE("SDW1 TX4")
334 	{ "SDW1 Capture", NULL, "SDW1 TX1 Source" },
335 	{ "SDW1 Capture", NULL, "SDW1 TX2 Source" },
336 	{ "SDW1 Capture", NULL, "SDW1 TX3 Source" },
337 	{ "SDW1 Capture", NULL, "SDW1 TX4 Source" },
338 };
339 
340 static int cs35l56_dsp_event(struct snd_soc_dapm_widget *w,
341 			     struct snd_kcontrol *kcontrol, int event)
342 {
343 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
344 	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
345 
346 	dev_dbg(cs35l56->base.dev, "%s: %d\n", __func__, event);
347 
348 	return wm_adsp_event(w, kcontrol, event);
349 }
350 
351 static int cs35l56_asp_dai_probe(struct snd_soc_dai *codec_dai)
352 {
353 	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(codec_dai->component);
354 
355 	return cs35l56_set_asp_patch(&cs35l56->base);
356 }
357 
358 static int cs35l56_asp_dai_set_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
359 {
360 	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(codec_dai->component);
361 	unsigned int val;
362 
363 	dev_dbg(cs35l56->base.dev, "%s: %#x\n", __func__, fmt);
364 
365 	switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
366 	case SND_SOC_DAIFMT_CBC_CFC:
367 		break;
368 	default:
369 		dev_err(cs35l56->base.dev, "Unsupported clock source mode\n");
370 		return -EINVAL;
371 	}
372 
373 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
374 	case SND_SOC_DAIFMT_DSP_A:
375 		val = CS35L56_ASP_FMT_DSP_A << CS35L56_ASP_FMT_SHIFT;
376 		cs35l56->tdm_mode = true;
377 		break;
378 	case SND_SOC_DAIFMT_I2S:
379 		val = CS35L56_ASP_FMT_I2S << CS35L56_ASP_FMT_SHIFT;
380 		cs35l56->tdm_mode = false;
381 		break;
382 	default:
383 		dev_err(cs35l56->base.dev, "Unsupported DAI format\n");
384 		return -EINVAL;
385 	}
386 
387 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
388 	case SND_SOC_DAIFMT_NB_IF:
389 		val |= CS35L56_ASP_FSYNC_INV_MASK;
390 		break;
391 	case SND_SOC_DAIFMT_IB_NF:
392 		val |= CS35L56_ASP_BCLK_INV_MASK;
393 		break;
394 	case SND_SOC_DAIFMT_IB_IF:
395 		val |= CS35L56_ASP_BCLK_INV_MASK | CS35L56_ASP_FSYNC_INV_MASK;
396 		break;
397 	case SND_SOC_DAIFMT_NB_NF:
398 		break;
399 	default:
400 		dev_err(cs35l56->base.dev, "Invalid clock invert\n");
401 		return -EINVAL;
402 	}
403 
404 	regmap_update_bits(cs35l56->base.regmap,
405 			   CS35L56_ASP1_CONTROL2,
406 			   CS35L56_ASP_FMT_MASK |
407 			   CS35L56_ASP_BCLK_INV_MASK | CS35L56_ASP_FSYNC_INV_MASK,
408 			   val);
409 
410 	/* Hi-Z DOUT in unused slots and when all TX are disabled */
411 	regmap_update_bits(cs35l56->base.regmap, CS35L56_ASP1_CONTROL3,
412 			   CS35L56_ASP1_DOUT_HIZ_CTRL_MASK,
413 			   CS35L56_ASP_UNUSED_HIZ_OFF_HIZ);
414 
415 	return 0;
416 }
417 
418 static unsigned int cs35l56_make_tdm_config_word(unsigned int reg_val, unsigned long mask)
419 {
420 	unsigned int channel_shift;
421 	int bit_num;
422 
423 	/* Enable consecutive TX1..TXn for each of the slots set in mask */
424 	channel_shift = 0;
425 	for_each_set_bit(bit_num, &mask, 32) {
426 		reg_val &= ~(0x3f << channel_shift);
427 		reg_val |= bit_num << channel_shift;
428 		channel_shift += 8;
429 	}
430 
431 	return reg_val;
432 }
433 
434 static int cs35l56_asp_dai_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
435 					unsigned int rx_mask, int slots, int slot_width)
436 {
437 	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(dai->component);
438 
439 	if ((slots == 0) || (slot_width == 0)) {
440 		dev_dbg(cs35l56->base.dev, "tdm config cleared\n");
441 		cs35l56->asp_slot_width = 0;
442 		cs35l56->asp_slot_count = 0;
443 		return 0;
444 	}
445 
446 	if (slot_width > (CS35L56_ASP_RX_WIDTH_MASK >> CS35L56_ASP_RX_WIDTH_SHIFT)) {
447 		dev_err(cs35l56->base.dev, "tdm invalid slot width %d\n", slot_width);
448 		return -EINVAL;
449 	}
450 
451 	/* More than 32 slots would give an unsupportable BCLK frequency */
452 	if (slots > 32) {
453 		dev_err(cs35l56->base.dev, "tdm invalid slot count %d\n", slots);
454 		return -EINVAL;
455 	}
456 
457 	cs35l56->asp_slot_width = (u8)slot_width;
458 	cs35l56->asp_slot_count = (u8)slots;
459 
460 	// Note: rx/tx is from point of view of the CPU end
461 	if (tx_mask == 0)
462 		tx_mask = 0x3;	// ASPRX1/RX2 in slots 0 and 1
463 
464 	if (rx_mask == 0)
465 		rx_mask = 0xf;	// ASPTX1..TX4 in slots 0..3
466 
467 	/* Default unused slots to 63 */
468 	regmap_write(cs35l56->base.regmap, CS35L56_ASP1_FRAME_CONTROL1,
469 		     cs35l56_make_tdm_config_word(0x3f3f3f3f, rx_mask));
470 	regmap_write(cs35l56->base.regmap, CS35L56_ASP1_FRAME_CONTROL5,
471 		     cs35l56_make_tdm_config_word(0x3f3f3f, tx_mask));
472 
473 	dev_dbg(cs35l56->base.dev, "tdm slot width: %u count: %u tx_mask: %#x rx_mask: %#x\n",
474 		cs35l56->asp_slot_width, cs35l56->asp_slot_count, tx_mask, rx_mask);
475 
476 	return 0;
477 }
478 
479 static int cs35l56_asp_dai_hw_params(struct snd_pcm_substream *substream,
480 				     struct snd_pcm_hw_params *params,
481 				     struct snd_soc_dai *dai)
482 {
483 	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(dai->component);
484 	unsigned int rate = params_rate(params);
485 	u8 asp_width, asp_wl;
486 
487 	asp_wl = params_width(params);
488 	if (cs35l56->asp_slot_width)
489 		asp_width = cs35l56->asp_slot_width;
490 	else
491 		asp_width = asp_wl;
492 
493 	dev_dbg(cs35l56->base.dev, "%s: wl=%d, width=%d, rate=%d",
494 		__func__, asp_wl, asp_width, rate);
495 
496 	if (!cs35l56->sysclk_set) {
497 		unsigned int slots = cs35l56->asp_slot_count;
498 		unsigned int bclk_freq;
499 		int freq_id;
500 
501 		if (slots == 0) {
502 			slots = params_channels(params);
503 
504 			/* I2S always has an even number of slots */
505 			if (!cs35l56->tdm_mode)
506 				slots = round_up(slots, 2);
507 		}
508 
509 		bclk_freq = asp_width * slots * rate;
510 		freq_id = cs35l56_get_bclk_freq_id(bclk_freq);
511 		if (freq_id < 0) {
512 			dev_err(cs35l56->base.dev, "%s: Invalid BCLK %u\n", __func__, bclk_freq);
513 			return -EINVAL;
514 		}
515 
516 		regmap_update_bits(cs35l56->base.regmap, CS35L56_ASP1_CONTROL1,
517 				   CS35L56_ASP_BCLK_FREQ_MASK,
518 				   freq_id << CS35L56_ASP_BCLK_FREQ_SHIFT);
519 	}
520 
521 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
522 		regmap_update_bits(cs35l56->base.regmap, CS35L56_ASP1_CONTROL2,
523 				   CS35L56_ASP_RX_WIDTH_MASK, asp_width <<
524 				   CS35L56_ASP_RX_WIDTH_SHIFT);
525 		regmap_update_bits(cs35l56->base.regmap, CS35L56_ASP1_DATA_CONTROL5,
526 				   CS35L56_ASP_RX_WL_MASK, asp_wl);
527 	} else {
528 		regmap_update_bits(cs35l56->base.regmap, CS35L56_ASP1_CONTROL2,
529 				   CS35L56_ASP_TX_WIDTH_MASK, asp_width <<
530 				   CS35L56_ASP_TX_WIDTH_SHIFT);
531 		regmap_update_bits(cs35l56->base.regmap, CS35L56_ASP1_DATA_CONTROL1,
532 				   CS35L56_ASP_TX_WL_MASK, asp_wl);
533 	}
534 
535 	return 0;
536 }
537 
538 static int cs35l56_asp_dai_set_sysclk(struct snd_soc_dai *dai,
539 				      int clk_id, unsigned int freq, int dir)
540 {
541 	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(dai->component);
542 	int freq_id;
543 
544 	if (freq == 0) {
545 		cs35l56->sysclk_set = false;
546 		return 0;
547 	}
548 
549 	freq_id = cs35l56_get_bclk_freq_id(freq);
550 	if (freq_id < 0)
551 		return freq_id;
552 
553 	regmap_update_bits(cs35l56->base.regmap, CS35L56_ASP1_CONTROL1,
554 			   CS35L56_ASP_BCLK_FREQ_MASK,
555 			   freq_id << CS35L56_ASP_BCLK_FREQ_SHIFT);
556 	cs35l56->sysclk_set = true;
557 
558 	return 0;
559 }
560 
561 static const struct snd_soc_dai_ops cs35l56_ops = {
562 	.probe = cs35l56_asp_dai_probe,
563 	.set_fmt = cs35l56_asp_dai_set_fmt,
564 	.set_tdm_slot = cs35l56_asp_dai_set_tdm_slot,
565 	.hw_params = cs35l56_asp_dai_hw_params,
566 	.set_sysclk = cs35l56_asp_dai_set_sysclk,
567 };
568 
569 static void cs35l56_sdw_dai_shutdown(struct snd_pcm_substream *substream,
570 				     struct snd_soc_dai *dai)
571 {
572 	snd_soc_dai_set_dma_data(dai, substream, NULL);
573 }
574 
575 static int cs35l56_sdw_dai_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
576 					unsigned int rx_mask, int slots, int slot_width)
577 {
578 	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(dai->component);
579 
580 	/* rx/tx are from point of view of the CPU end so opposite to our rx/tx */
581 	cs35l56->rx_mask = tx_mask;
582 	cs35l56->tx_mask = rx_mask;
583 
584 	return 0;
585 }
586 
587 static int cs35l56_sdw_dai_hw_params(struct snd_pcm_substream *substream,
588 				     struct snd_pcm_hw_params *params,
589 				     struct snd_soc_dai *dai)
590 {
591 	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(dai->component);
592 	struct sdw_stream_runtime *sdw_stream = snd_soc_dai_get_dma_data(dai, substream);
593 	struct sdw_stream_config sconfig;
594 	struct sdw_port_config pconfig;
595 	int ret;
596 
597 	dev_dbg(cs35l56->base.dev, "%s: rate %d\n", __func__, params_rate(params));
598 
599 	if (!cs35l56->base.init_done)
600 		return -ENODEV;
601 
602 	if (!sdw_stream)
603 		return -EINVAL;
604 
605 	memset(&sconfig, 0, sizeof(sconfig));
606 	memset(&pconfig, 0, sizeof(pconfig));
607 
608 	sconfig.frame_rate = params_rate(params);
609 	sconfig.bps = snd_pcm_format_width(params_format(params));
610 
611 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
612 		sconfig.direction = SDW_DATA_DIR_RX;
613 		pconfig.num = CS35L56_SDW1_PLAYBACK_PORT;
614 		pconfig.ch_mask = cs35l56->rx_mask;
615 	} else {
616 		sconfig.direction = SDW_DATA_DIR_TX;
617 		pconfig.num = CS35L56_SDW1_CAPTURE_PORT;
618 		pconfig.ch_mask = cs35l56->tx_mask;
619 	}
620 
621 	if (pconfig.ch_mask == 0) {
622 		sconfig.ch_count = params_channels(params);
623 		pconfig.ch_mask = GENMASK(sconfig.ch_count - 1, 0);
624 	} else {
625 		sconfig.ch_count = hweight32(pconfig.ch_mask);
626 	}
627 
628 	ret = sdw_stream_add_slave(cs35l56->sdw_peripheral, &sconfig, &pconfig,
629 				   1, sdw_stream);
630 	if (ret) {
631 		dev_err(dai->dev, "Failed to add sdw stream: %d\n", ret);
632 		return ret;
633 	}
634 
635 	return 0;
636 }
637 
638 static int cs35l56_sdw_dai_hw_free(struct snd_pcm_substream *substream,
639 				   struct snd_soc_dai *dai)
640 {
641 	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(dai->component);
642 	struct sdw_stream_runtime *sdw_stream = snd_soc_dai_get_dma_data(dai, substream);
643 
644 	if (!cs35l56->sdw_peripheral)
645 		return -EINVAL;
646 
647 	sdw_stream_remove_slave(cs35l56->sdw_peripheral, sdw_stream);
648 
649 	return 0;
650 }
651 
652 static int cs35l56_sdw_dai_set_stream(struct snd_soc_dai *dai,
653 				      void *sdw_stream, int direction)
654 {
655 	snd_soc_dai_dma_data_set(dai, direction, sdw_stream);
656 
657 	return 0;
658 }
659 
660 static const struct snd_soc_dai_ops cs35l56_sdw_dai_ops = {
661 	.set_tdm_slot = cs35l56_sdw_dai_set_tdm_slot,
662 	.shutdown = cs35l56_sdw_dai_shutdown,
663 	.hw_params = cs35l56_sdw_dai_hw_params,
664 	.hw_free = cs35l56_sdw_dai_hw_free,
665 	.set_stream = cs35l56_sdw_dai_set_stream,
666 };
667 
668 static struct snd_soc_dai_driver cs35l56_dai[] = {
669 	{
670 		.name = "cs35l56-asp1",
671 		.id = 0,
672 		.playback = {
673 			.stream_name = "ASP1 Playback",
674 			.channels_min = 1,
675 			.channels_max = 2,
676 			.rates = CS35L56_RATES,
677 			.formats = CS35L56_RX_FORMATS,
678 		},
679 		.capture = {
680 			.stream_name = "ASP1 Capture",
681 			.channels_min = 1,
682 			.channels_max = 4,
683 			.rates = CS35L56_RATES,
684 			.formats = CS35L56_TX_FORMATS,
685 		},
686 		.ops = &cs35l56_ops,
687 		.symmetric_rate = 1,
688 		.symmetric_sample_bits = 1,
689 	},
690 	{
691 		.name = "cs35l56-sdw1",
692 		.id = 1,
693 		.playback = {
694 			.stream_name = "SDW1 Playback",
695 			.channels_min = 1,
696 			.channels_max = 2,
697 			.rates = CS35L56_RATES,
698 			.formats = CS35L56_RX_FORMATS,
699 		},
700 		.symmetric_rate = 1,
701 		.ops = &cs35l56_sdw_dai_ops,
702 	},
703 	{
704 		.name = "cs35l56-sdw1c",
705 		.id = 2,
706 		.capture = {
707 			.stream_name = "SDW1 Capture",
708 			.channels_min = 1,
709 			.channels_max = 4,
710 			.rates = CS35L56_RATES,
711 			.formats = CS35L56_TX_FORMATS,
712 		},
713 		.symmetric_rate = 1,
714 		.ops = &cs35l56_sdw_dai_ops,
715 	},
716 };
717 
718 static int cs35l56_write_cal(struct cs35l56_private *cs35l56)
719 {
720 	int ret;
721 
722 	if (cs35l56->base.secured || !cs35l56->base.cal_data_valid)
723 		return -ENODATA;
724 
725 	ret = wm_adsp_run(&cs35l56->dsp);
726 	if (ret)
727 		return ret;
728 
729 	ret = cs_amp_write_cal_coeffs(&cs35l56->dsp.cs_dsp,
730 				      cs35l56->base.calibration_controls,
731 				      &cs35l56->base.cal_data);
732 
733 	wm_adsp_stop(&cs35l56->dsp);
734 
735 	if (ret == 0)
736 		dev_info(cs35l56->base.dev, "Calibration applied\n");
737 
738 	return ret;
739 }
740 
741 static int cs35l56_dsp_download_and_power_up(struct cs35l56_private *cs35l56,
742 					     bool load_firmware)
743 {
744 	int ret;
745 
746 	/*
747 	 * Abort the first load if it didn't find the suffixed bins and
748 	 * we have an alternate fallback suffix.
749 	 */
750 	cs35l56->dsp.bin_mandatory = (load_firmware && cs35l56->fallback_fw_suffix);
751 
752 	ret = wm_adsp_power_up(&cs35l56->dsp, load_firmware);
753 	if ((ret == -ENOENT) && cs35l56->dsp.bin_mandatory) {
754 		cs35l56->dsp.fwf_suffix = cs35l56->fallback_fw_suffix;
755 		cs35l56->fallback_fw_suffix = NULL;
756 		cs35l56->dsp.bin_mandatory = false;
757 		ret = wm_adsp_power_up(&cs35l56->dsp, load_firmware);
758 	}
759 
760 	if (ret) {
761 		dev_dbg(cs35l56->base.dev, "wm_adsp_power_up ret %d\n", ret);
762 		return ret;
763 	}
764 
765 	return 0;
766 }
767 
768 static void cs35l56_reinit_patch(struct cs35l56_private *cs35l56)
769 {
770 	int ret;
771 
772 	ret = cs35l56_dsp_download_and_power_up(cs35l56, true);
773 	if (ret)
774 		return;
775 
776 	cs35l56_write_cal(cs35l56);
777 
778 	/* Always REINIT after applying patch or coefficients */
779 	cs35l56_mbox_send(&cs35l56->base, CS35L56_MBOX_CMD_AUDIO_REINIT);
780 }
781 
782 static void cs35l56_patch(struct cs35l56_private *cs35l56, bool firmware_missing)
783 {
784 	int ret;
785 
786 	/*
787 	 * Disable SoundWire interrupts to prevent race with IRQ work.
788 	 * Setting sdw_irq_no_unmask prevents the handler re-enabling
789 	 * the SoundWire interrupt.
790 	 */
791 	if (cs35l56->sdw_peripheral) {
792 		cs35l56->sdw_irq_no_unmask = true;
793 		flush_work(&cs35l56->sdw_irq_work);
794 		sdw_write_no_pm(cs35l56->sdw_peripheral, CS35L56_SDW_GEN_INT_MASK_1, 0);
795 		sdw_read_no_pm(cs35l56->sdw_peripheral, CS35L56_SDW_GEN_INT_STAT_1);
796 		sdw_write_no_pm(cs35l56->sdw_peripheral, CS35L56_SDW_GEN_INT_STAT_1, 0xFF);
797 		flush_work(&cs35l56->sdw_irq_work);
798 	}
799 
800 	ret = cs35l56_firmware_shutdown(&cs35l56->base);
801 	if (ret)
802 		goto err;
803 
804 	/*
805 	 * Use wm_adsp to load and apply the firmware patch and coefficient files,
806 	 * but only if firmware is missing. If firmware is already patched just
807 	 * power-up wm_adsp without downloading firmware.
808 	 */
809 	ret = cs35l56_dsp_download_and_power_up(cs35l56, firmware_missing);
810 	if (ret)
811 		goto err;
812 
813 	mutex_lock(&cs35l56->base.irq_lock);
814 
815 	reinit_completion(&cs35l56->init_completion);
816 
817 	cs35l56->soft_resetting = true;
818 	cs35l56_system_reset(&cs35l56->base, !!cs35l56->sdw_peripheral);
819 
820 	if (cs35l56->sdw_peripheral) {
821 		/*
822 		 * The system-reset causes the CS35L56 to detach from the bus.
823 		 * Wait for the manager to re-enumerate the CS35L56 and
824 		 * cs35l56_init() to run again.
825 		 */
826 		if (!wait_for_completion_timeout(&cs35l56->init_completion,
827 						 msecs_to_jiffies(5000))) {
828 			dev_err(cs35l56->base.dev, "%s: init_completion timed out (SDW)\n",
829 				__func__);
830 			goto err_unlock;
831 		}
832 	} else if (cs35l56_init(cs35l56)) {
833 		goto err_unlock;
834 	}
835 
836 	/* Check if the firmware is still reported missing */
837 	cs35l56_warn_if_firmware_missing(&cs35l56->base);
838 
839 	regmap_clear_bits(cs35l56->base.regmap,
840 			  cs35l56->base.fw_reg->prot_sts,
841 			  CS35L56_FIRMWARE_MISSING);
842 	cs35l56->base.fw_patched = true;
843 
844 	if (cs35l56_write_cal(cs35l56) == 0)
845 		cs35l56_mbox_send(&cs35l56->base, CS35L56_MBOX_CMD_AUDIO_REINIT);
846 
847 err_unlock:
848 	mutex_unlock(&cs35l56->base.irq_lock);
849 err:
850 	/* Re-enable SoundWire interrupts */
851 	if (cs35l56->sdw_peripheral) {
852 		cs35l56->sdw_irq_no_unmask = false;
853 		sdw_write_no_pm(cs35l56->sdw_peripheral, CS35L56_SDW_GEN_INT_MASK_1,
854 				CS35L56_SDW_INT_MASK_CODEC_IRQ);
855 	}
856 }
857 
858 static void cs35l56_dsp_work(struct work_struct *work)
859 {
860 	struct cs35l56_private *cs35l56 = container_of(work,
861 						       struct cs35l56_private,
862 						       dsp_work);
863 	unsigned int firmware_version;
864 	bool firmware_missing;
865 	int ret;
866 
867 	if (!cs35l56->base.init_done)
868 		return;
869 
870 	PM_RUNTIME_ACQUIRE(cs35l56->base.dev, pm);
871 	ret = PM_RUNTIME_ACQUIRE_ERR(&pm);
872 	if (ret) {
873 		dev_err(cs35l56->base.dev, "dsp_work failed to runtime-resume: %d\n", ret);
874 		return;
875 	}
876 
877 	ret = cs35l56_read_prot_status(&cs35l56->base, &firmware_missing, &firmware_version);
878 	if (ret)
879 		return;
880 
881 	/* Populate fw file qualifier with the revision and security state */
882 	kfree(cs35l56->dsp.fwf_name);
883 	if (firmware_missing) {
884 		cs35l56->dsp.fwf_name = kasprintf(GFP_KERNEL, "%02x-dsp1", cs35l56->base.rev);
885 	} else {
886 		/* Firmware files must match the running firmware version */
887 		cs35l56->dsp.fwf_name = kasprintf(GFP_KERNEL,
888 						  "%02x%s-%06x-dsp1",
889 						  cs35l56->base.rev,
890 						  cs35l56->base.secured ? "-s" : "",
891 						  firmware_version);
892 	}
893 
894 	if (!cs35l56->dsp.fwf_name)
895 		return;
896 
897 	dev_dbg(cs35l56->base.dev, "DSP fwf name: '%s' system name: '%s'\n",
898 		cs35l56->dsp.fwf_name, cs35l56->dsp.system_name);
899 
900 	/*
901 	 * The firmware cannot be patched if it is already running from
902 	 * patch RAM. In this case the firmware files are versioned to
903 	 * match the running firmware version and will only contain
904 	 * tunings. We do not need to shutdown the firmware to apply
905 	 * tunings so can use the lower cost reinit sequence instead.
906 	 */
907 	if (!firmware_missing)
908 		cs35l56_reinit_patch(cs35l56);
909 	else
910 		cs35l56_patch(cs35l56, firmware_missing);
911 
912 	cs35l56_log_tuning(&cs35l56->base, &cs35l56->dsp.cs_dsp);
913 }
914 
915 static struct snd_soc_dapm_context *cs35l56_power_up_for_cal(struct cs35l56_private *cs35l56)
916 {
917 	struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(cs35l56->component);
918 	int ret;
919 
920 	ret = snd_soc_dapm_enable_pin(dapm, "Calibrate");
921 	if (ret)
922 		return ERR_PTR(ret);
923 
924 	snd_soc_dapm_sync(dapm);
925 
926 	return dapm;
927 }
928 
929 static void cs35l56_power_down_after_cal(struct cs35l56_private *cs35l56)
930 {
931 	struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(cs35l56->component);
932 
933 	snd_soc_dapm_disable_pin(dapm, "Calibrate");
934 	snd_soc_dapm_sync(dapm);
935 }
936 
937 static ssize_t cs35l56_debugfs_calibrate_write(struct file *file,
938 					       const char __user *from,
939 					       size_t count, loff_t *ppos)
940 {
941 	struct cs35l56_base *cs35l56_base = file->private_data;
942 	struct cs35l56_private *cs35l56 = cs35l56_private_from_base(cs35l56_base);
943 	struct snd_soc_dapm_context *dapm;
944 	ssize_t ret;
945 
946 	dapm = cs35l56_power_up_for_cal(cs35l56);
947 	if (IS_ERR(dapm))
948 		return PTR_ERR(dapm);
949 
950 	snd_soc_dapm_mutex_lock(dapm);
951 	ret = cs35l56_calibrate_debugfs_write(&cs35l56->base, from, count, ppos);
952 	snd_soc_dapm_mutex_unlock(dapm);
953 
954 	cs35l56_power_down_after_cal(cs35l56);
955 
956 	return ret;
957 }
958 
959 static ssize_t cs35l56_debugfs_cal_temperature_write(struct file *file,
960 						     const char __user *from,
961 						     size_t count, loff_t *ppos)
962 {
963 	struct cs35l56_base *cs35l56_base = file->private_data;
964 	struct cs35l56_private *cs35l56 = cs35l56_private_from_base(cs35l56_base);
965 	struct snd_soc_dapm_context *dapm;
966 	ssize_t ret;
967 
968 	dapm = cs35l56_power_up_for_cal(cs35l56);
969 	if (IS_ERR(dapm))
970 		return PTR_ERR(dapm);
971 
972 	ret = cs35l56_cal_ambient_debugfs_write(&cs35l56->base, from, count, ppos);
973 	cs35l56_power_down_after_cal(cs35l56);
974 
975 	return ret;
976 }
977 
978 static ssize_t cs35l56_debugfs_cal_data_read(struct file *file,
979 					     char __user *to,
980 					     size_t count, loff_t *ppos)
981 {
982 	struct cs35l56_base *cs35l56_base = file->private_data;
983 	struct cs35l56_private *cs35l56 = cs35l56_private_from_base(cs35l56_base);
984 	struct snd_soc_dapm_context *dapm;
985 	ssize_t ret;
986 
987 	dapm = cs35l56_power_up_for_cal(cs35l56);
988 	if (IS_ERR(dapm))
989 		return PTR_ERR(dapm);
990 
991 	ret = cs35l56_cal_data_debugfs_read(&cs35l56->base, to, count, ppos);
992 	cs35l56_power_down_after_cal(cs35l56);
993 
994 	return ret;
995 }
996 
997 static int cs35l56_new_cal_data_apply(struct cs35l56_private *cs35l56)
998 {
999 	struct snd_soc_dapm_context *dapm;
1000 	int ret;
1001 
1002 	if (!cs35l56->base.cal_data_valid)
1003 		return -ENXIO;
1004 
1005 	if (cs35l56->base.secured)
1006 		return -EACCES;
1007 
1008 	dapm = cs35l56_power_up_for_cal(cs35l56);
1009 	if (IS_ERR(dapm))
1010 		return PTR_ERR(dapm);
1011 
1012 	snd_soc_dapm_mutex_lock(dapm);
1013 	ret = cs_amp_write_cal_coeffs(&cs35l56->dsp.cs_dsp,
1014 				      cs35l56->base.calibration_controls,
1015 				      &cs35l56->base.cal_data);
1016 	if (ret == 0)
1017 		cs35l56_mbox_send(&cs35l56->base, CS35L56_MBOX_CMD_AUDIO_REINIT);
1018 	else
1019 		ret = -EIO;
1020 
1021 	snd_soc_dapm_mutex_unlock(dapm);
1022 	cs35l56_power_down_after_cal(cs35l56);
1023 
1024 	return ret;
1025 }
1026 
1027 static ssize_t cs35l56_debugfs_cal_data_write(struct file *file,
1028 					      const char __user *from,
1029 					      size_t count, loff_t *ppos)
1030 {
1031 	struct cs35l56_base *cs35l56_base = file->private_data;
1032 	struct cs35l56_private *cs35l56 = cs35l56_private_from_base(cs35l56_base);
1033 	int ret;
1034 
1035 	ret = cs35l56_cal_data_debugfs_write(&cs35l56->base, from, count, ppos);
1036 	if (ret == -ENODATA)
1037 		return count;	/* Ignore writes of empty cal blobs */
1038 	else if (ret < 0)
1039 		return -EIO;
1040 
1041 	ret = cs35l56_new_cal_data_apply(cs35l56);
1042 	if (ret)
1043 		return ret;
1044 
1045 	return count;
1046 }
1047 
1048 static const struct cs35l56_cal_debugfs_fops cs35l56_cal_debugfs_fops = {
1049 	.calibrate = {
1050 		.write = cs35l56_debugfs_calibrate_write,
1051 	},
1052 	.cal_temperature = {
1053 		.write = cs35l56_debugfs_cal_temperature_write,
1054 	},
1055 	.cal_data = {
1056 		.read = cs35l56_debugfs_cal_data_read,
1057 		.write = cs35l56_debugfs_cal_data_write,
1058 	},
1059 };
1060 
1061 static int cs35l56_cal_data_rb_ctl_get(struct snd_kcontrol *kcontrol,
1062 				    struct snd_ctl_elem_value *ucontrol)
1063 {
1064 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
1065 	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
1066 
1067 	if (!cs35l56->base.cal_data_valid)
1068 		return -ENODATA;
1069 
1070 	memcpy(ucontrol->value.bytes.data, &cs35l56->base.cal_data,
1071 	       sizeof(cs35l56->base.cal_data));
1072 
1073 	return 0;
1074 }
1075 
1076 static int cs35l56_cal_data_ctl_get(struct snd_kcontrol *kcontrol,
1077 				    struct snd_ctl_elem_value *ucontrol)
1078 {
1079 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
1080 	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
1081 
1082 	/*
1083 	 * This control is write-only but mixer libraries often try to read
1084 	 * a control before writing it. So we have to implement read.
1085 	 * Return zeros so a write of valid data will always be a change
1086 	 * from its "current value".
1087 	 */
1088 	memset(ucontrol->value.bytes.data, 0, sizeof(cs35l56->base.cal_data));
1089 
1090 	return 0;
1091 }
1092 
1093 static int cs35l56_cal_data_ctl_set(struct snd_kcontrol *kcontrol,
1094 				    struct snd_ctl_elem_value *ucontrol)
1095 {
1096 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
1097 	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
1098 	const struct cirrus_amp_cal_data *cal_data = (const void *)ucontrol->value.bytes.data;
1099 	int ret;
1100 
1101 	if (cs35l56->base.cal_data_valid)
1102 		return -EACCES;
1103 
1104 	ret = cs35l56_stash_calibration(&cs35l56->base, cal_data);
1105 	if (ret)
1106 		return ret;
1107 
1108 	ret = cs35l56_new_cal_data_apply(cs35l56);
1109 	if (ret < 0)
1110 		return ret;
1111 
1112 	return 1;
1113 }
1114 
1115 static int cs35l56_cal_ambient_ctl_get(struct snd_kcontrol *kcontrol,
1116 				       struct snd_ctl_elem_value *ucontrol)
1117 {
1118 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
1119 	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
1120 
1121 	ucontrol->value.integer.value[0] = cs35l56->ambient_ctl_value;
1122 
1123 	return 0;
1124 }
1125 
1126 static int cs35l56_cal_ambient_ctl_set(struct snd_kcontrol *kcontrol,
1127 				       struct snd_ctl_elem_value *ucontrol)
1128 {
1129 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
1130 	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
1131 	struct snd_soc_dapm_context *dapm;
1132 	int temperature = ucontrol->value.integer.value[0];
1133 	int ret;
1134 
1135 	if (temperature == cs35l56->ambient_ctl_value)
1136 		return 0;
1137 
1138 	if ((temperature < 0) || (temperature > 40))
1139 		return -EINVAL;
1140 
1141 	dapm = cs35l56_power_up_for_cal(cs35l56);
1142 	if (IS_ERR(dapm))
1143 		return PTR_ERR(dapm);
1144 
1145 	ret = cs_amp_write_ambient_temp(&cs35l56->dsp.cs_dsp,
1146 					cs35l56->base.calibration_controls,
1147 					temperature);
1148 	cs35l56_power_down_after_cal(cs35l56);
1149 
1150 	if (ret)
1151 		return ret;
1152 
1153 	cs35l56->ambient_ctl_value = temperature;
1154 
1155 	return 1;
1156 }
1157 
1158 static int cs35l56_calibrate_ctl_get(struct snd_kcontrol *kcontrol,
1159 				     struct snd_ctl_elem_value *ucontrol)
1160 {
1161 	/*
1162 	 * Allow reading because of user-side libraries that assume all
1163 	 * controls are readable. But always return false to prevent dumb
1164 	 * save-restore tools like alsactl accidentically triggering a
1165 	 * factory calibration when they restore.
1166 	 */
1167 	ucontrol->value.integer.value[0] = 0;
1168 
1169 	return 0;
1170 }
1171 
1172 static int cs35l56_calibrate_ctl_set(struct snd_kcontrol *kcontrol,
1173 				     struct snd_ctl_elem_value *ucontrol)
1174 {
1175 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
1176 	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
1177 	struct snd_soc_dapm_context *dapm;
1178 	int ret;
1179 
1180 	if (ucontrol->value.integer.value[0] == 0)
1181 		return 0;
1182 
1183 	dapm = cs35l56_power_up_for_cal(cs35l56);
1184 	if (IS_ERR(dapm))
1185 		return PTR_ERR(dapm);
1186 
1187 	snd_soc_dapm_mutex_lock(dapm);
1188 	ret = cs35l56_factory_calibrate(&cs35l56->base);
1189 	snd_soc_dapm_mutex_unlock(dapm);
1190 	cs35l56_power_down_after_cal(cs35l56);
1191 	if (ret < 0)
1192 		return ret;
1193 
1194 	return 1;
1195 }
1196 
1197 static const struct snd_kcontrol_new cs35l56_cal_data_restore_controls[] = {
1198 	SND_SOC_BYTES_E("CAL_DATA", 0, sizeof(struct cirrus_amp_cal_data) / sizeof(u32),
1199 			cs35l56_cal_data_ctl_get, cs35l56_cal_data_ctl_set),
1200 	SND_SOC_BYTES_E_ACC("CAL_DATA_RB", 0, sizeof(struct cirrus_amp_cal_data) / sizeof(u32),
1201 			cs35l56_cal_data_rb_ctl_get, NULL,
1202 			SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE),
1203 };
1204 
1205 static const struct snd_kcontrol_new cs35l56_cal_perform_controls[] = {
1206 	SOC_SINGLE_EXT("CAL_AMBIENT", SND_SOC_NOPM, 0, 40, 0,
1207 		       cs35l56_cal_ambient_ctl_get, cs35l56_cal_ambient_ctl_set),
1208 	SOC_SINGLE_BOOL_EXT_ACC("Calibrate Switch", 0,
1209 				cs35l56_calibrate_ctl_get, cs35l56_calibrate_ctl_set,
1210 				SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_VOLATILE),
1211 };
1212 
1213 VISIBLE_IF_KUNIT int cs35l56_set_fw_suffix(struct cs35l56_private *cs35l56)
1214 {
1215 	unsigned short vendor, device;
1216 	const char *vendor_id;
1217 	int ret;
1218 
1219 	if (cs35l56->dsp.fwf_suffix)
1220 		return 0;
1221 
1222 	if (cs35l56->sdw_peripheral) {
1223 		cs35l56->dsp.fwf_suffix = devm_kasprintf(cs35l56->base.dev, GFP_KERNEL,
1224 							 "l%uu%u",
1225 							 cs35l56->sdw_link_num,
1226 							 cs35l56->sdw_unique_id);
1227 		if (!cs35l56->dsp.fwf_suffix)
1228 			return -ENOMEM;
1229 
1230 		/*
1231 		 * There are published firmware files for L56 B0 silicon using
1232 		 * the ALSA prefix as the filename suffix. Default to trying these
1233 		 * first, with the new SoundWire suffix as a fallback.
1234 		 * None of these older systems use a vendor-specific ID.
1235 		 */
1236 		if ((cs35l56->base.type == 0x56) && (cs35l56->base.rev == 0xb0)) {
1237 			cs35l56->fallback_fw_suffix = cs35l56->dsp.fwf_suffix;
1238 			cs35l56->dsp.fwf_suffix = cs35l56->component->name_prefix;
1239 
1240 			return 0;
1241 		}
1242 	}
1243 
1244 	/*
1245 	 * Some manufacturers use the same SSID on multiple products and have
1246 	 * a vendor-specific qualifier to distinguish different models.
1247 	 * Models with the same SSID but different qualifier might require
1248 	 * different audio firmware, or they might all have the same audio
1249 	 * firmware.
1250 	 * Try searching for a firmware with this qualifier first, else
1251 	 * fallback to standard naming.
1252 	 */
1253 	if (snd_soc_card_get_pci_ssid(cs35l56->component->card, &vendor, &device) < 0) {
1254 		vendor_id = cs_amp_devm_get_vendor_specific_variant_id(cs35l56->base.dev, -1, -1);
1255 	} else {
1256 		vendor_id = cs_amp_devm_get_vendor_specific_variant_id(cs35l56->base.dev,
1257 								       vendor, device);
1258 	}
1259 	ret = PTR_ERR_OR_ZERO(vendor_id);
1260 	if (ret == -ENOENT)
1261 		return 0;
1262 	else if (ret)
1263 		return ret;
1264 
1265 	if (vendor_id) {
1266 		if (cs35l56->dsp.fwf_suffix)
1267 			cs35l56->fallback_fw_suffix = cs35l56->dsp.fwf_suffix;
1268 		else
1269 			cs35l56->fallback_fw_suffix = cs35l56->component->name_prefix;
1270 
1271 		cs35l56->dsp.fwf_suffix = devm_kasprintf(cs35l56->base.dev, GFP_KERNEL,
1272 							 "%s-%s",
1273 							 vendor_id,
1274 							 cs35l56->fallback_fw_suffix);
1275 		if (!cs35l56->dsp.fwf_suffix)
1276 			return -ENOMEM;
1277 	}
1278 
1279 	return 0;
1280 }
1281 EXPORT_SYMBOL_IF_KUNIT(cs35l56_set_fw_suffix);
1282 
1283 VISIBLE_IF_KUNIT int cs35l56_set_fw_name(struct snd_soc_component *component)
1284 {
1285 	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
1286 	unsigned short vendor, device;
1287 	int ret;
1288 
1289 	if ((cs35l56->speaker_id < 0) && cs35l56->base.num_onchip_spkid_gpios) {
1290 		PM_RUNTIME_ACQUIRE(cs35l56->base.dev, pm);
1291 		ret = PM_RUNTIME_ACQUIRE_ERR(&pm);
1292 		if (ret)
1293 			return ret;
1294 
1295 		ret = cs35l56_configure_onchip_spkid_pads(&cs35l56->base);
1296 		if (ret)
1297 			return ret;
1298 
1299 		ret = cs35l56_read_onchip_spkid(&cs35l56->base);
1300 		if (ret < 0)
1301 			return ret;
1302 
1303 		cs35l56->speaker_id = ret;
1304 	}
1305 
1306 	if (!cs35l56->dsp.system_name &&
1307 	    (snd_soc_card_get_pci_ssid(component->card, &vendor, &device) == 0)) {
1308 		/* Append a speaker qualifier if there is a speaker ID */
1309 		if (cs35l56->speaker_id >= 0) {
1310 			cs35l56->dsp.system_name = devm_kasprintf(cs35l56->base.dev,
1311 								  GFP_KERNEL,
1312 								  "%04x%04x-spkid%d",
1313 								  vendor, device,
1314 								  cs35l56->speaker_id);
1315 		} else {
1316 			cs35l56->dsp.system_name = devm_kasprintf(cs35l56->base.dev,
1317 								  GFP_KERNEL,
1318 								  "%04x%04x",
1319 								  vendor, device);
1320 		}
1321 		if (!cs35l56->dsp.system_name)
1322 			return -ENOMEM;
1323 	}
1324 
1325 	return 0;
1326 }
1327 EXPORT_SYMBOL_IF_KUNIT(cs35l56_set_fw_name);
1328 
1329 static int cs35l56_component_probe(struct snd_soc_component *component)
1330 {
1331 	struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(component);
1332 	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
1333 	struct dentry *debugfs_root = component->debugfs_root;
1334 	int ret;
1335 
1336 	BUILD_BUG_ON(ARRAY_SIZE(cs35l56_tx_input_texts) != ARRAY_SIZE(cs35l56_tx_input_values));
1337 
1338 	if (!wait_for_completion_timeout(&cs35l56->init_completion,
1339 					 msecs_to_jiffies(5000))) {
1340 		dev_err(cs35l56->base.dev, "%s: init_completion timed out\n", __func__);
1341 		return -ENODEV;
1342 	}
1343 
1344 	cs35l56->dsp.part = kasprintf(GFP_KERNEL, "cs35l%02x", cs35l56->base.type);
1345 	if (!cs35l56->dsp.part)
1346 		return -ENOMEM;
1347 
1348 	cs35l56->component = component;
1349 	ret = cs35l56_set_fw_name(component);
1350 	if (ret)
1351 		return ret;
1352 
1353 	ret = cs35l56_set_fw_suffix(cs35l56);
1354 	if (ret)
1355 		return ret;
1356 
1357 	wm_adsp2_component_probe(&cs35l56->dsp, component);
1358 
1359 	debugfs_create_bool("init_done", 0444, debugfs_root, &cs35l56->base.init_done);
1360 	debugfs_create_bool("can_hibernate", 0444, debugfs_root, &cs35l56->base.can_hibernate);
1361 	debugfs_create_bool("fw_patched", 0444, debugfs_root, &cs35l56->base.fw_patched);
1362 
1363 
1364 	switch (cs35l56->base.type) {
1365 	case 0x54:
1366 	case 0x56:
1367 	case 0x57:
1368 		ret = snd_soc_add_component_controls(component, cs35l56_controls,
1369 						     ARRAY_SIZE(cs35l56_controls));
1370 		break;
1371 	case 0x63:
1372 		ret = snd_soc_add_component_controls(component, cs35l63_controls,
1373 						     ARRAY_SIZE(cs35l63_controls));
1374 		break;
1375 	default:
1376 		ret = -ENODEV;
1377 		break;
1378 	}
1379 
1380 	if (!ret && IS_ENABLED(CONFIG_SND_SOC_CS35L56_CAL_SET_CTRL)) {
1381 		ret = snd_soc_add_component_controls(component,
1382 						     cs35l56_cal_data_restore_controls,
1383 						     ARRAY_SIZE(cs35l56_cal_data_restore_controls));
1384 	}
1385 
1386 	if (!ret && IS_ENABLED(CONFIG_SND_SOC_CS35L56_CAL_PERFORM_CTRL)) {
1387 		ret = snd_soc_add_component_controls(component,
1388 						     cs35l56_cal_perform_controls,
1389 						     ARRAY_SIZE(cs35l56_cal_perform_controls));
1390 	}
1391 
1392 	if (ret)
1393 		return dev_err_probe(cs35l56->base.dev, ret, "unable to add controls\n");
1394 
1395 	ret = snd_soc_dapm_disable_pin(dapm, "Calibrate");
1396 	if (ret)
1397 		return ret;
1398 
1399 	if (IS_ENABLED(CONFIG_SND_SOC_CS35L56_CAL_DEBUGFS))
1400 		cs35l56_create_cal_debugfs(&cs35l56->base, &cs35l56_cal_debugfs_fops);
1401 
1402 	queue_work(cs35l56->dsp_wq, &cs35l56->dsp_work);
1403 
1404 	return 0;
1405 }
1406 
1407 static void cs35l56_component_remove(struct snd_soc_component *component)
1408 {
1409 	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
1410 
1411 	cancel_work_sync(&cs35l56->dsp_work);
1412 
1413 	cs35l56_remove_cal_debugfs(&cs35l56->base);
1414 
1415 	if (cs35l56->dsp.cs_dsp.booted)
1416 		wm_adsp_power_down(&cs35l56->dsp);
1417 
1418 	wm_adsp2_component_remove(&cs35l56->dsp, component);
1419 
1420 	kfree(cs35l56->dsp.part);
1421 	cs35l56->dsp.part = NULL;
1422 
1423 	kfree(cs35l56->dsp.fwf_name);
1424 	cs35l56->dsp.fwf_name = NULL;
1425 
1426 	cs35l56->component = NULL;
1427 }
1428 
1429 static int cs35l56_set_bias_level(struct snd_soc_component *component,
1430 				  enum snd_soc_bias_level level)
1431 {
1432 	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
1433 	struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(component);
1434 
1435 	switch (level) {
1436 	case SND_SOC_BIAS_STANDBY:
1437 		/*
1438 		 * Wait for patching to complete when transitioning from
1439 		 * BIAS_OFF to BIAS_STANDBY
1440 		 */
1441 		if (snd_soc_dapm_get_bias_level(dapm) == SND_SOC_BIAS_OFF)
1442 			cs35l56_wait_dsp_ready(cs35l56);
1443 
1444 		break;
1445 	default:
1446 		break;
1447 	}
1448 
1449 	return 0;
1450 }
1451 
1452 static const struct snd_soc_component_driver soc_component_dev_cs35l56 = {
1453 	.probe = cs35l56_component_probe,
1454 	.remove = cs35l56_component_remove,
1455 
1456 	.dapm_widgets = cs35l56_dapm_widgets,
1457 	.num_dapm_widgets = ARRAY_SIZE(cs35l56_dapm_widgets),
1458 	.dapm_routes = cs35l56_audio_map,
1459 	.num_dapm_routes = ARRAY_SIZE(cs35l56_audio_map),
1460 
1461 	.set_bias_level = cs35l56_set_bias_level,
1462 
1463 	.suspend_bias_off = 1, /* see cs35l56_system_resume() */
1464 };
1465 
1466 static int __maybe_unused cs35l56_runtime_suspend_i2c_spi(struct device *dev)
1467 {
1468 	struct cs35l56_private *cs35l56 = dev_get_drvdata(dev);
1469 
1470 	return cs35l56_runtime_suspend_common(&cs35l56->base);
1471 }
1472 
1473 static int __maybe_unused cs35l56_runtime_resume_i2c_spi(struct device *dev)
1474 {
1475 	struct cs35l56_private *cs35l56 = dev_get_drvdata(dev);
1476 
1477 	return cs35l56_runtime_resume_common(&cs35l56->base, false);
1478 }
1479 
1480 int cs35l56_system_suspend(struct device *dev)
1481 {
1482 	struct cs35l56_private *cs35l56 = dev_get_drvdata(dev);
1483 
1484 	dev_dbg(dev, "system_suspend\n");
1485 
1486 	if (cs35l56->component)
1487 		flush_work(&cs35l56->dsp_work);
1488 
1489 	/*
1490 	 * The interrupt line is normally shared, but after we start suspending
1491 	 * we can't check if our device is the source of an interrupt, and can't
1492 	 * clear it. Prevent this race by temporarily disabling the parent irq
1493 	 * until we reach _no_irq.
1494 	 */
1495 	if (cs35l56->base.irq)
1496 		disable_irq(cs35l56->base.irq);
1497 
1498 	return pm_runtime_force_suspend(dev);
1499 }
1500 EXPORT_SYMBOL_GPL(cs35l56_system_suspend);
1501 
1502 int cs35l56_system_suspend_late(struct device *dev)
1503 {
1504 	struct cs35l56_private *cs35l56 = dev_get_drvdata(dev);
1505 
1506 	dev_dbg(dev, "system_suspend_late\n");
1507 
1508 	/*
1509 	 * Assert RESET before removing supplies.
1510 	 * RESET is usually shared by all amps so it must not be asserted until
1511 	 * all driver instances have done their suspend() stage.
1512 	 */
1513 	if (cs35l56->base.reset_gpio) {
1514 		gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 0);
1515 		cs35l56_wait_min_reset_pulse();
1516 	}
1517 
1518 	regulator_bulk_disable(ARRAY_SIZE(cs35l56->supplies), cs35l56->supplies);
1519 
1520 	return 0;
1521 }
1522 EXPORT_SYMBOL_GPL(cs35l56_system_suspend_late);
1523 
1524 int cs35l56_system_suspend_no_irq(struct device *dev)
1525 {
1526 	struct cs35l56_private *cs35l56 = dev_get_drvdata(dev);
1527 
1528 	dev_dbg(dev, "system_suspend_no_irq\n");
1529 
1530 	/* Handlers are now disabled so the parent IRQ can safely be re-enabled. */
1531 	if (cs35l56->base.irq)
1532 		enable_irq(cs35l56->base.irq);
1533 
1534 	return 0;
1535 }
1536 EXPORT_SYMBOL_GPL(cs35l56_system_suspend_no_irq);
1537 
1538 int cs35l56_system_resume_no_irq(struct device *dev)
1539 {
1540 	struct cs35l56_private *cs35l56 = dev_get_drvdata(dev);
1541 
1542 	dev_dbg(dev, "system_resume_no_irq\n");
1543 
1544 	/*
1545 	 * WAKE interrupts unmask if the CS35L56 hibernates, which can cause
1546 	 * spurious interrupts, and the interrupt line is normally shared.
1547 	 * We can't check if our device is the source of an interrupt, and can't
1548 	 * clear it, until it has fully resumed. Prevent this race by temporarily
1549 	 * disabling the parent irq until we complete resume().
1550 	 */
1551 	if (cs35l56->base.irq)
1552 		disable_irq(cs35l56->base.irq);
1553 
1554 	return 0;
1555 }
1556 EXPORT_SYMBOL_GPL(cs35l56_system_resume_no_irq);
1557 
1558 int cs35l56_system_resume_early(struct device *dev)
1559 {
1560 	struct cs35l56_private *cs35l56 = dev_get_drvdata(dev);
1561 	int ret;
1562 
1563 	dev_dbg(dev, "system_resume_early\n");
1564 
1565 	/* Ensure a spec-compliant RESET pulse. */
1566 	if (cs35l56->base.reset_gpio) {
1567 		gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 0);
1568 		cs35l56_wait_min_reset_pulse();
1569 	}
1570 
1571 	/* Enable supplies before releasing RESET. */
1572 	ret = regulator_bulk_enable(ARRAY_SIZE(cs35l56->supplies), cs35l56->supplies);
1573 	if (ret) {
1574 		dev_err(dev, "system_resume_early failed to enable supplies: %d\n", ret);
1575 		return ret;
1576 	}
1577 
1578 	/* Release shared RESET before drivers start resume(). */
1579 	gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 1);
1580 
1581 	return 0;
1582 }
1583 EXPORT_SYMBOL_GPL(cs35l56_system_resume_early);
1584 
1585 int cs35l56_system_resume(struct device *dev)
1586 {
1587 	struct cs35l56_private *cs35l56 = dev_get_drvdata(dev);
1588 	int ret;
1589 
1590 	dev_dbg(dev, "system_resume\n");
1591 
1592 	/*
1593 	 * We might have done a hard reset or the CS35L56 was power-cycled
1594 	 * so wait for control port to be ready.
1595 	 */
1596 	cs35l56_wait_control_port_ready();
1597 
1598 	/* Undo pm_runtime_force_suspend() before re-enabling the irq */
1599 	ret = pm_runtime_force_resume(dev);
1600 	if (cs35l56->base.irq)
1601 		enable_irq(cs35l56->base.irq);
1602 
1603 	if (ret)
1604 		return ret;
1605 
1606 	/* Firmware won't have been loaded if the component hasn't probed */
1607 	if (!cs35l56->component)
1608 		return 0;
1609 
1610 	ret = cs35l56_is_fw_reload_needed(&cs35l56->base);
1611 	dev_dbg(cs35l56->base.dev, "fw_reload_needed: %d\n", ret);
1612 	if (ret < 1)
1613 		return ret;
1614 
1615 	cs35l56->base.fw_patched = false;
1616 	wm_adsp_power_down(&cs35l56->dsp);
1617 	queue_work(cs35l56->dsp_wq, &cs35l56->dsp_work);
1618 
1619 	/*
1620 	 * suspend_bias_off ensures we are now in BIAS_OFF so there will be
1621 	 * a BIAS_OFF->BIAS_STANDBY transition to complete dsp patching.
1622 	 */
1623 
1624 	return 0;
1625 }
1626 EXPORT_SYMBOL_GPL(cs35l56_system_resume);
1627 
1628 static int cs35l56_control_add_nop(struct wm_adsp *dsp, struct cs_dsp_coeff_ctl *cs_ctl)
1629 {
1630 	return 0;
1631 }
1632 
1633 static int cs35l56_dsp_init(struct cs35l56_private *cs35l56)
1634 {
1635 	struct wm_adsp *dsp;
1636 	int ret;
1637 
1638 	cs35l56->dsp_wq = create_singlethread_workqueue("cs35l56-dsp");
1639 	if (!cs35l56->dsp_wq)
1640 		return -ENOMEM;
1641 
1642 	INIT_WORK(&cs35l56->dsp_work, cs35l56_dsp_work);
1643 
1644 	dsp = &cs35l56->dsp;
1645 	cs35l56_init_cs_dsp(&cs35l56->base, &dsp->cs_dsp);
1646 
1647 	/*
1648 	 * dsp->part is filled in later as it is based on the DEVID. In a
1649 	 * SoundWire system that cannot be read until enumeration has occurred
1650 	 * and the device has attached.
1651 	 */
1652 	dsp->fw = 12;
1653 	dsp->wmfw_optional = true;
1654 
1655 	/*
1656 	 * None of the firmware controls need to be exported so add a no-op
1657 	 * callback that suppresses creating an ALSA control.
1658 	 */
1659 	dsp->control_add = &cs35l56_control_add_nop;
1660 
1661 	dev_dbg(cs35l56->base.dev, "DSP system name: '%s'\n", dsp->system_name);
1662 
1663 	ret = wm_halo_init(dsp);
1664 	if (ret != 0) {
1665 		dev_err(cs35l56->base.dev, "wm_halo_init failed\n");
1666 		return ret;
1667 	}
1668 
1669 	return 0;
1670 }
1671 
1672 static int cs35l56_read_fwnode_u32_array(struct device *dev,
1673 					struct fwnode_handle *parent_node,
1674 					const char *prop_name,
1675 					int max_count,
1676 					u32 *dest)
1677 {
1678 	int count, ret;
1679 
1680 	count = fwnode_property_count_u32(parent_node, prop_name);
1681 	if ((count == 0) || (count == -EINVAL) || (count == -ENODATA)) {
1682 		dev_dbg(dev, "%s not found in %s\n", prop_name, fwnode_get_name(parent_node));
1683 		return 0;
1684 	}
1685 
1686 	if (count < 0) {
1687 		dev_err(dev, "Get %s error:%d\n", prop_name, count);
1688 		return count;
1689 	}
1690 
1691 	if (count > max_count) {
1692 		dev_err(dev, "%s too many entries (%d)\n", prop_name, count);
1693 		return -EOVERFLOW;
1694 	}
1695 
1696 	ret = fwnode_property_read_u32_array(parent_node, prop_name, dest, count);
1697 	if (ret) {
1698 		dev_err(dev, "Error reading %s: %d\n", prop_name, ret);
1699 		return ret;
1700 	}
1701 
1702 	return count;
1703 }
1704 
1705 static int cs35l56_process_xu_onchip_speaker_id(struct cs35l56_private *cs35l56,
1706 						struct fwnode_handle *ext_node)
1707 {
1708 	static const char * const gpio_name = "01fa-spk-id-gpios-onchip";
1709 	static const char * const pull_name = "01fa-spk-id-gpios-onchip-pull";
1710 	u32 gpios[5], pulls[5];
1711 	int num_gpios, num_pulls;
1712 	int ret;
1713 
1714 	static_assert(ARRAY_SIZE(gpios) == ARRAY_SIZE(cs35l56->base.onchip_spkid_gpios));
1715 	static_assert(ARRAY_SIZE(pulls) == ARRAY_SIZE(cs35l56->base.onchip_spkid_pulls));
1716 
1717 	num_gpios = cs35l56_read_fwnode_u32_array(cs35l56->base.dev, ext_node, gpio_name,
1718 						  ARRAY_SIZE(gpios), gpios);
1719 	if (num_gpios < 1)
1720 		return num_gpios;
1721 
1722 	num_pulls = cs35l56_read_fwnode_u32_array(cs35l56->base.dev, ext_node, pull_name,
1723 						  ARRAY_SIZE(pulls), pulls);
1724 	if (num_pulls < 0)
1725 		return num_pulls;
1726 
1727 	if (num_pulls && (num_pulls != num_gpios)) {
1728 		dev_warn(cs35l56->base.dev, "%s count(%d) != %s count(%d)\n",
1729 			 pull_name, num_pulls, gpio_name, num_gpios);
1730 	}
1731 
1732 	ret = cs35l56_check_and_save_onchip_spkid_gpios(&cs35l56->base,
1733 							gpios, num_gpios,
1734 							pulls, num_pulls);
1735 	if (ret) {
1736 		return dev_err_probe(cs35l56->base.dev, ret, "Error in %s/%s\n",
1737 				     gpio_name, pull_name);
1738 	}
1739 
1740 	return 0;
1741 }
1742 
1743 VISIBLE_IF_KUNIT int cs35l56_process_xu_properties(struct cs35l56_private *cs35l56)
1744 {
1745 	struct fwnode_handle *ext_node = NULL;
1746 	struct fwnode_handle *link;
1747 	int ret;
1748 
1749 	if (!cs35l56->sdw_peripheral)
1750 		return 0;
1751 
1752 	fwnode_for_each_child_node(dev_fwnode(cs35l56->base.dev), link) {
1753 		ext_node = fwnode_get_named_child_node(link,
1754 						       "mipi-sdca-function-expansion-subproperties");
1755 		if (ext_node) {
1756 			fwnode_handle_put(link);
1757 			break;
1758 		}
1759 	}
1760 
1761 	if (!ext_node)
1762 		return 0;
1763 
1764 	ret = cs35l56_process_xu_onchip_speaker_id(cs35l56, ext_node);
1765 	fwnode_handle_put(ext_node);
1766 
1767 	return ret;
1768 }
1769 EXPORT_SYMBOL_IF_KUNIT(cs35l56_process_xu_properties);
1770 
1771 VISIBLE_IF_KUNIT int cs35l56_get_firmware_uid(struct cs35l56_private *cs35l56)
1772 {
1773 	struct device *dev = cs35l56->base.dev;
1774 	const char *prop;
1775 	int ret;
1776 
1777 	ret = device_property_read_string(dev, "cirrus,firmware-uid", &prop);
1778 	/* If bad sw node property, return 0 and fallback to legacy firmware path */
1779 	if (ret < 0)
1780 		return 0;
1781 
1782 	/* Append a speaker qualifier if there is a speaker ID */
1783 	if (cs35l56->speaker_id >= 0)
1784 		cs35l56->dsp.system_name = devm_kasprintf(dev, GFP_KERNEL, "%s-spkid%d",
1785 							  prop, cs35l56->speaker_id);
1786 	else
1787 		cs35l56->dsp.system_name = devm_kstrdup(dev, prop, GFP_KERNEL);
1788 
1789 	if (cs35l56->dsp.system_name == NULL)
1790 		return -ENOMEM;
1791 
1792 	dev_dbg(dev, "Firmware UID: %s\n", cs35l56->dsp.system_name);
1793 
1794 	return 0;
1795 }
1796 EXPORT_SYMBOL_IF_KUNIT(cs35l56_get_firmware_uid);
1797 
1798 /*
1799  * Some SoundWire laptops have a spk-id-gpios property but it points to
1800  * the wrong ACPI Device node so can't be used to get the GPIO. Try to
1801  * find the SDCA node containing the GpioIo resource and add a GPIO
1802  * mapping to it.
1803  */
1804 static const struct acpi_gpio_params cs35l56_af01_first_gpio = { 0, 0, false };
1805 static const struct acpi_gpio_mapping cs35l56_af01_spkid_gpios_mapping[] = {
1806 	{ "spk-id-gpios", &cs35l56_af01_first_gpio, 1 },
1807 	{ }
1808 };
1809 
1810 static void cs35l56_acpi_dev_release_driver_gpios(void *adev)
1811 {
1812 	acpi_dev_remove_driver_gpios(adev);
1813 }
1814 
1815 static int cs35l56_try_get_broken_sdca_spkid_gpio(struct cs35l56_private *cs35l56)
1816 {
1817 	struct fwnode_handle *af01_fwnode;
1818 	const union acpi_object *obj;
1819 	struct gpio_desc *desc;
1820 	int ret;
1821 
1822 	/* Find the SDCA node containing the GpioIo */
1823 	af01_fwnode = device_get_named_child_node(cs35l56->base.dev, "AF01");
1824 	if (!af01_fwnode) {
1825 		dev_dbg(cs35l56->base.dev, "No AF01 node\n");
1826 		return -ENOENT;
1827 	}
1828 
1829 	ret = acpi_dev_get_property(ACPI_COMPANION(cs35l56->base.dev),
1830 				    "spk-id-gpios", ACPI_TYPE_PACKAGE, &obj);
1831 	if (ret) {
1832 		dev_dbg(cs35l56->base.dev, "Could not get spk-id-gpios package: %d\n", ret);
1833 		fwnode_handle_put(af01_fwnode);
1834 		return -ENOENT;
1835 	}
1836 
1837 	/* The broken properties we can handle are a 4-element package (one GPIO) */
1838 	if (obj->package.count != 4) {
1839 		dev_warn(cs35l56->base.dev, "Unexpected spk-id element count %d\n",
1840 			 obj->package.count);
1841 		fwnode_handle_put(af01_fwnode);
1842 		return -ENOENT;
1843 	}
1844 
1845 	/* Add a GPIO mapping if it doesn't already have one */
1846 	if (!fwnode_property_present(af01_fwnode, "spk-id-gpios")) {
1847 		struct acpi_device *adev = to_acpi_device_node(af01_fwnode);
1848 
1849 		/*
1850 		 * Can't use devm_acpi_dev_add_driver_gpios() because the
1851 		 * mapping isn't being added to the node pointed to by
1852 		 * ACPI_COMPANION().
1853 		 */
1854 		ret = acpi_dev_add_driver_gpios(adev, cs35l56_af01_spkid_gpios_mapping);
1855 		if (ret) {
1856 			fwnode_handle_put(af01_fwnode);
1857 			return dev_err_probe(cs35l56->base.dev, ret,
1858 					     "Failed to add gpio mapping to AF01\n");
1859 		}
1860 
1861 		ret = devm_add_action_or_reset(cs35l56->base.dev,
1862 					       cs35l56_acpi_dev_release_driver_gpios,
1863 					       adev);
1864 		if (ret) {
1865 			fwnode_handle_put(af01_fwnode);
1866 			return ret;
1867 		}
1868 
1869 		dev_dbg(cs35l56->base.dev, "Added spk-id-gpios mapping to AF01\n");
1870 	}
1871 
1872 	desc = fwnode_gpiod_get_index(af01_fwnode, "spk-id", 0, GPIOD_IN, NULL);
1873 	if (IS_ERR(desc)) {
1874 		fwnode_handle_put(af01_fwnode);
1875 		ret = PTR_ERR(desc);
1876 		return dev_err_probe(cs35l56->base.dev, ret, "Get GPIO from AF01 failed\n");
1877 	}
1878 
1879 	ret = gpiod_get_value_cansleep(desc);
1880 	gpiod_put(desc);
1881 
1882 	if (ret < 0) {
1883 		fwnode_handle_put(af01_fwnode);
1884 		dev_err_probe(cs35l56->base.dev, ret, "Error reading spk-id GPIO\n");
1885 		return ret;
1886 	}
1887 
1888 	fwnode_handle_put(af01_fwnode);
1889 
1890 	dev_info(cs35l56->base.dev, "Got spk-id from AF01\n");
1891 
1892 	return ret;
1893 }
1894 
1895 int cs35l56_common_probe(struct cs35l56_private *cs35l56)
1896 {
1897 	int ret;
1898 
1899 	init_completion(&cs35l56->init_completion);
1900 	mutex_init(&cs35l56->base.irq_lock);
1901 	cs35l56->base.cal_index = -1;
1902 	cs35l56->speaker_id = -ENOENT;
1903 
1904 	dev_set_drvdata(cs35l56->base.dev, cs35l56);
1905 
1906 	cs35l56_fill_supply_names(cs35l56->supplies);
1907 	ret = devm_regulator_bulk_get(cs35l56->base.dev, ARRAY_SIZE(cs35l56->supplies),
1908 				      cs35l56->supplies);
1909 	if (ret != 0)
1910 		return dev_err_probe(cs35l56->base.dev, ret, "Failed to request supplies\n");
1911 
1912 	/* Reset could be controlled by the BIOS or shared by multiple amps */
1913 	cs35l56->base.reset_gpio = devm_gpiod_get_optional(cs35l56->base.dev, "reset",
1914 							   GPIOD_OUT_LOW);
1915 	if (IS_ERR(cs35l56->base.reset_gpio)) {
1916 		ret = PTR_ERR(cs35l56->base.reset_gpio);
1917 		/*
1918 		 * If RESET is shared the first amp to probe will grab the reset
1919 		 * line and reset all the amps
1920 		 */
1921 		if (ret != -EBUSY)
1922 			return dev_err_probe(cs35l56->base.dev, ret, "Failed to get reset GPIO\n");
1923 
1924 		dev_info(cs35l56->base.dev, "Reset GPIO busy, assume shared reset\n");
1925 		cs35l56->base.reset_gpio = NULL;
1926 	}
1927 
1928 	ret = regulator_bulk_enable(ARRAY_SIZE(cs35l56->supplies), cs35l56->supplies);
1929 	if (ret != 0)
1930 		return dev_err_probe(cs35l56->base.dev, ret, "Failed to enable supplies\n");
1931 
1932 	if (cs35l56->base.reset_gpio) {
1933 		/* ACPI can override GPIOD_OUT_LOW flag so force it to start low */
1934 		gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 0);
1935 		cs35l56_wait_min_reset_pulse();
1936 		gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 1);
1937 	}
1938 
1939 	ret = cs35l56_get_speaker_id(&cs35l56->base);
1940 	if (ACPI_COMPANION(cs35l56->base.dev) && cs35l56->sdw_peripheral && (ret == -ENOENT))
1941 		ret = cs35l56_try_get_broken_sdca_spkid_gpio(cs35l56);
1942 
1943 	if ((ret < 0) && (ret != -ENOENT))
1944 		goto err;
1945 
1946 	cs35l56->speaker_id = ret;
1947 
1948 	ret = cs35l56_get_firmware_uid(cs35l56);
1949 	if (ret != 0)
1950 		goto err;
1951 
1952 	ret = cs35l56_process_xu_properties(cs35l56);
1953 	if (ret)
1954 		goto err;
1955 
1956 	ret = cs35l56_dsp_init(cs35l56);
1957 	if (ret < 0) {
1958 		dev_err_probe(cs35l56->base.dev, ret, "DSP init failed\n");
1959 		goto err;
1960 	}
1961 
1962 	ret = snd_soc_register_component(cs35l56->base.dev,
1963 					 &soc_component_dev_cs35l56,
1964 					 cs35l56_dai, ARRAY_SIZE(cs35l56_dai));
1965 	if (ret < 0) {
1966 		dev_err_probe(cs35l56->base.dev, ret, "Register codec failed\n");
1967 		goto err;
1968 	}
1969 
1970 	return 0;
1971 
1972 err:
1973 	gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 0);
1974 	regulator_bulk_disable(ARRAY_SIZE(cs35l56->supplies), cs35l56->supplies);
1975 
1976 	if (cs35l56->dsp_wq)
1977 		destroy_workqueue(cs35l56->dsp_wq);
1978 
1979 	return ret;
1980 }
1981 EXPORT_SYMBOL_NS_GPL(cs35l56_common_probe, "SND_SOC_CS35L56_CORE");
1982 
1983 int cs35l56_init(struct cs35l56_private *cs35l56)
1984 {
1985 	int ret;
1986 
1987 	/*
1988 	 * Check whether the actions associated with soft reset or one time
1989 	 * init need to be performed.
1990 	 */
1991 	if (cs35l56->soft_resetting)
1992 		goto post_soft_reset;
1993 
1994 	if (cs35l56->base.init_done)
1995 		return 0;
1996 
1997 	pm_runtime_set_autosuspend_delay(cs35l56->base.dev, 100);
1998 	pm_runtime_use_autosuspend(cs35l56->base.dev);
1999 	pm_runtime_set_active(cs35l56->base.dev);
2000 	pm_runtime_enable(cs35l56->base.dev);
2001 
2002 	ret = cs35l56_hw_init(&cs35l56->base);
2003 	if (ret < 0)
2004 		return ret;
2005 
2006 	ret = cs35l56_set_patch(&cs35l56->base);
2007 	if (ret)
2008 		return ret;
2009 
2010 	ret = cs35l56_get_calibration(&cs35l56->base);
2011 	if (ret)
2012 		return ret;
2013 
2014 	if (!cs35l56->base.reset_gpio) {
2015 		dev_dbg(cs35l56->base.dev, "No reset gpio: using soft reset\n");
2016 		cs35l56->soft_resetting = true;
2017 		cs35l56_system_reset(&cs35l56->base, !!cs35l56->sdw_peripheral);
2018 		if (cs35l56->sdw_peripheral) {
2019 			/* Keep alive while we wait for re-enumeration */
2020 			pm_runtime_get_noresume(cs35l56->base.dev);
2021 			return 0;
2022 		}
2023 	}
2024 
2025 post_soft_reset:
2026 	if (cs35l56->soft_resetting) {
2027 		cs35l56->soft_resetting = false;
2028 
2029 		/* Done re-enumerating after one-time init so release the keep-alive */
2030 		if (cs35l56->sdw_peripheral && !cs35l56->base.init_done)
2031 			pm_runtime_put_noidle(cs35l56->base.dev);
2032 
2033 		regcache_mark_dirty(cs35l56->base.regmap);
2034 		ret = cs35l56_wait_for_firmware_boot(&cs35l56->base);
2035 		if (ret)
2036 			return ret;
2037 
2038 		dev_dbg(cs35l56->base.dev, "Firmware rebooted after soft reset\n");
2039 
2040 		regcache_cache_only(cs35l56->base.regmap, false);
2041 	}
2042 
2043 	/* Disable auto-hibernate so that runtime_pm has control */
2044 	ret = cs35l56_mbox_send(&cs35l56->base, CS35L56_MBOX_CMD_PREVENT_AUTO_HIBERNATE);
2045 	if (ret)
2046 		return ret;
2047 
2048 	/* Registers could be dirty after soft reset or SoundWire enumeration */
2049 	regcache_sync(cs35l56->base.regmap);
2050 
2051 	/* Set ASP1 DOUT to high-impedance when it is not transmitting audio data. */
2052 	ret = regmap_set_bits(cs35l56->base.regmap, CS35L56_ASP1_CONTROL3,
2053 			      CS35L56_ASP1_DOUT_HIZ_CTRL_MASK);
2054 	if (ret)
2055 		return dev_err_probe(cs35l56->base.dev, ret, "Failed to write ASP1_CONTROL3\n");
2056 
2057 	cs35l56->base.init_done = true;
2058 	complete(&cs35l56->init_completion);
2059 
2060 	return 0;
2061 }
2062 EXPORT_SYMBOL_NS_GPL(cs35l56_init, "SND_SOC_CS35L56_CORE");
2063 
2064 void cs35l56_remove(struct cs35l56_private *cs35l56)
2065 {
2066 	snd_soc_unregister_component(cs35l56->base.dev);
2067 
2068 	cs35l56->base.init_done = false;
2069 
2070 	/*
2071 	 * WAKE IRQs unmask if CS35L56 hibernates so free the handler to
2072 	 * prevent it racing with remove().
2073 	 */
2074 	if (cs35l56->base.irq)
2075 		devm_free_irq(cs35l56->base.dev, cs35l56->base.irq, &cs35l56->base);
2076 
2077 	destroy_workqueue(cs35l56->dsp_wq);
2078 
2079 	pm_runtime_dont_use_autosuspend(cs35l56->base.dev);
2080 	pm_runtime_suspend(cs35l56->base.dev);
2081 	pm_runtime_disable(cs35l56->base.dev);
2082 
2083 	regcache_cache_only(cs35l56->base.regmap, true);
2084 
2085 	gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 0);
2086 	regulator_bulk_disable(ARRAY_SIZE(cs35l56->supplies), cs35l56->supplies);
2087 }
2088 EXPORT_SYMBOL_NS_GPL(cs35l56_remove, "SND_SOC_CS35L56_CORE");
2089 
2090 #if IS_ENABLED(CONFIG_SND_SOC_CS35L56_I2C) || IS_ENABLED(CONFIG_SND_SOC_CS35L56_SPI)
2091 EXPORT_NS_GPL_DEV_PM_OPS(cs35l56_pm_ops_i2c_spi, SND_SOC_CS35L56_CORE) = {
2092 	SET_RUNTIME_PM_OPS(cs35l56_runtime_suspend_i2c_spi, cs35l56_runtime_resume_i2c_spi, NULL)
2093 	SYSTEM_SLEEP_PM_OPS(cs35l56_system_suspend, cs35l56_system_resume)
2094 	LATE_SYSTEM_SLEEP_PM_OPS(cs35l56_system_suspend_late, cs35l56_system_resume_early)
2095 	NOIRQ_SYSTEM_SLEEP_PM_OPS(cs35l56_system_suspend_no_irq, cs35l56_system_resume_no_irq)
2096 };
2097 #endif
2098 
2099 MODULE_DESCRIPTION("ASoC CS35L56 driver");
2100 MODULE_IMPORT_NS("SND_SOC_CS35L56_SHARED");
2101 MODULE_IMPORT_NS("SND_SOC_CS_AMP_LIB");
2102 MODULE_AUTHOR("Richard Fitzgerald <rf@opensource.cirrus.com>");
2103 MODULE_AUTHOR("Simon Trimmer <simont@opensource.cirrus.com>");
2104 MODULE_LICENSE("GPL");
2105