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