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