xref: /linux/sound/soc/codecs/cs35l56.c (revision 86c8360c97f66fa7830fd930e2dc69374007284b)
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 struct snd_soc_dai_ops cs35l56_ops = {
621 	.probe = cs35l56_asp_dai_probe,
622 	.set_fmt = cs35l56_asp_dai_set_fmt,
623 	.set_tdm_slot = cs35l56_asp_dai_set_tdm_slot,
624 	.hw_params = cs35l56_asp_dai_hw_params,
625 	.set_sysclk = cs35l56_asp_dai_set_sysclk,
626 };
627 
628 static void cs35l56_sdw_dai_shutdown(struct snd_pcm_substream *substream,
629 				     struct snd_soc_dai *dai)
630 {
631 	snd_soc_dai_set_dma_data(dai, substream, NULL);
632 }
633 
634 static int cs35l56_sdw_dai_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
635 					unsigned int rx_mask, int slots, int slot_width)
636 {
637 	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(dai->component);
638 
639 	/* rx/tx are from point of view of the CPU end so opposite to our rx/tx */
640 	cs35l56->rx_mask = tx_mask;
641 	cs35l56->tx_mask = rx_mask;
642 
643 	return 0;
644 }
645 
646 static int cs35l56_sdw_dai_hw_params(struct snd_pcm_substream *substream,
647 				     struct snd_pcm_hw_params *params,
648 				     struct snd_soc_dai *dai)
649 {
650 	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(dai->component);
651 	struct sdw_stream_runtime *sdw_stream = snd_soc_dai_get_dma_data(dai, substream);
652 	struct sdw_stream_config sconfig;
653 	struct sdw_port_config pconfig;
654 	int ret;
655 
656 	dev_dbg(cs35l56->base.dev, "%s: rate %d\n", __func__, params_rate(params));
657 
658 	if (!cs35l56->base.init_done)
659 		return -ENODEV;
660 
661 	if (!sdw_stream)
662 		return -EINVAL;
663 
664 	memset(&sconfig, 0, sizeof(sconfig));
665 	memset(&pconfig, 0, sizeof(pconfig));
666 
667 	sconfig.frame_rate = params_rate(params);
668 	sconfig.bps = snd_pcm_format_width(params_format(params));
669 
670 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
671 		sconfig.direction = SDW_DATA_DIR_RX;
672 		pconfig.num = CS35L56_SDW1_PLAYBACK_PORT;
673 		pconfig.ch_mask = cs35l56->rx_mask;
674 	} else {
675 		sconfig.direction = SDW_DATA_DIR_TX;
676 		pconfig.num = CS35L56_SDW1_CAPTURE_PORT;
677 		pconfig.ch_mask = cs35l56->tx_mask;
678 	}
679 
680 	if (pconfig.ch_mask == 0) {
681 		sconfig.ch_count = params_channels(params);
682 		pconfig.ch_mask = GENMASK(sconfig.ch_count - 1, 0);
683 	} else {
684 		sconfig.ch_count = hweight32(pconfig.ch_mask);
685 	}
686 
687 	ret = sdw_stream_add_slave(cs35l56->sdw_peripheral, &sconfig, &pconfig,
688 				   1, sdw_stream);
689 	if (ret) {
690 		dev_err(dai->dev, "Failed to add sdw stream: %d\n", ret);
691 		return ret;
692 	}
693 
694 	return 0;
695 }
696 
697 static int cs35l56_sdw_dai_hw_free(struct snd_pcm_substream *substream,
698 				   struct snd_soc_dai *dai)
699 {
700 	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(dai->component);
701 	struct sdw_stream_runtime *sdw_stream = snd_soc_dai_get_dma_data(dai, substream);
702 
703 	if (!cs35l56->sdw_peripheral)
704 		return -EINVAL;
705 
706 	sdw_stream_remove_slave(cs35l56->sdw_peripheral, sdw_stream);
707 
708 	return 0;
709 }
710 
711 static int cs35l56_sdw_dai_set_stream(struct snd_soc_dai *dai,
712 				      void *sdw_stream, int direction)
713 {
714 	snd_soc_dai_dma_data_set(dai, direction, sdw_stream);
715 
716 	return 0;
717 }
718 
719 static int cs35l56_ot25_dai_hw_params(struct snd_pcm_substream *substream,
720 				      struct snd_pcm_hw_params *params,
721 				      struct snd_soc_dai *dai)
722 {
723 	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(dai->component);
724 	struct sdw_stream_runtime *sdw_stream = snd_soc_dai_get_dma_data(dai, substream);
725 	struct sdw_stream_config sconfig = { };
726 	struct sdw_port_config pconfig = { };
727 	int ret;
728 
729 	dev_dbg(cs35l56->base.dev, "%s: rate %d\n", __func__, params_rate(params));
730 
731 	if (!cs35l56->base.init_done)
732 		return -ENODEV;
733 
734 	if (!sdw_stream)
735 		return -EINVAL;
736 
737 	snd_sdw_params_to_config(substream, params, &sconfig, &pconfig);
738 	pconfig.num = CS35L56_OT25_CAPTURE_PORT;
739 
740 	ret = sdw_stream_add_slave(cs35l56->sdw_peripheral, &sconfig, &pconfig,
741 				   1, sdw_stream);
742 	if (ret) {
743 		dev_err(dai->dev, "Failed to add OT25 stream: %d\n", ret);
744 		return ret;
745 	}
746 
747 	return 0;
748 }
749 
750 static const struct snd_soc_dai_ops cs35l56_sdw_dai_ops = {
751 	.set_tdm_slot = cs35l56_sdw_dai_set_tdm_slot,
752 	.shutdown = cs35l56_sdw_dai_shutdown,
753 	.hw_params = cs35l56_sdw_dai_hw_params,
754 	.hw_free = cs35l56_sdw_dai_hw_free,
755 	.set_stream = cs35l56_sdw_dai_set_stream,
756 };
757 
758 static const struct snd_soc_dai_ops cs35l56_ot25_dai_ops = {
759 	.shutdown = cs35l56_sdw_dai_shutdown,
760 	.hw_params = cs35l56_ot25_dai_hw_params,
761 	.hw_free = cs35l56_sdw_dai_hw_free,
762 	.set_stream = cs35l56_sdw_dai_set_stream,
763 };
764 
765 static struct snd_soc_dai_driver cs35l56_dai[] = {
766 	{
767 		.name = "cs35l56-asp1",
768 		.id = 0,
769 		.playback = {
770 			.stream_name = "ASP1 Playback",
771 			.channels_min = 1,
772 			.channels_max = 2,
773 			.rates = CS35L56_RATES,
774 			.formats = CS35L56_RX_FORMATS,
775 		},
776 		.capture = {
777 			.stream_name = "ASP1 Capture",
778 			.channels_min = 1,
779 			.channels_max = 4,
780 			.rates = CS35L56_RATES,
781 			.formats = CS35L56_TX_FORMATS,
782 		},
783 		.ops = &cs35l56_ops,
784 		.symmetric_rate = 1,
785 		.symmetric_sample_bits = 1,
786 	},
787 	{
788 		.name = "cs35l56-sdw1",
789 		.id = 1,
790 		.playback = {
791 			.stream_name = "SDW1 Playback",
792 			.channels_min = 1,
793 			.channels_max = 2,
794 			.rates = CS35L56_RATES,
795 			.formats = CS35L56_RX_FORMATS,
796 		},
797 		.symmetric_rate = 1,
798 		.ops = &cs35l56_sdw_dai_ops,
799 	},
800 	{
801 		.name = "cs35l56-sdw1c",
802 		.id = 2,
803 		.capture = {
804 			.stream_name = "SDW1 Capture",
805 			.channels_min = 1,
806 			.channels_max = 4,
807 			.rates = CS35L56_RATES,
808 			.formats = CS35L56_TX_FORMATS,
809 		},
810 		.symmetric_rate = 1,
811 		.ops = &cs35l56_sdw_dai_ops,
812 	},
813 	{
814 		.name = "cs35l56-ot25",
815 		.id = 3,
816 		.capture = {
817 			.stream_name = "OT25",
818 			.channels_min = 1,
819 			.channels_max = 2,
820 			.rates = CS35L56_RATES,
821 			.formats = CS35L56_TX_FORMATS,
822 		},
823 		.ops = &cs35l56_ot25_dai_ops,
824 	},
825 };
826 
827 static int cs35l56_write_cal(struct cs35l56_private *cs35l56)
828 {
829 	int ret;
830 
831 	if (cs35l56->base.secured || !cs35l56->base.cal_data_valid)
832 		return -ENODATA;
833 
834 	ret = wm_adsp_run(&cs35l56->dsp);
835 	if (ret)
836 		return ret;
837 
838 	ret = cs_amp_write_cal_coeffs(&cs35l56->dsp.cs_dsp,
839 				      cs35l56->base.calibration_controls,
840 				      &cs35l56->base.cal_data);
841 
842 	wm_adsp_stop(&cs35l56->dsp);
843 
844 	if (ret == 0)
845 		dev_info(cs35l56->base.dev, "Calibration applied\n");
846 
847 	return ret;
848 }
849 
850 static int cs35l56_dsp_download_and_power_up(struct cs35l56_private *cs35l56,
851 					     bool load_firmware)
852 {
853 	int ret;
854 
855 	/*
856 	 * Abort the first load if it didn't find the suffixed bins and
857 	 * we have an alternate fallback suffix.
858 	 */
859 	cs35l56->dsp.bin_mandatory = (load_firmware && cs35l56->fallback_fw_suffix);
860 
861 	ret = wm_adsp_power_up(&cs35l56->dsp, load_firmware);
862 	if ((ret == -ENOENT) && cs35l56->dsp.bin_mandatory) {
863 		cs35l56->dsp.fwf_suffix = cs35l56->fallback_fw_suffix;
864 		cs35l56->fallback_fw_suffix = NULL;
865 		cs35l56->dsp.bin_mandatory = false;
866 		ret = wm_adsp_power_up(&cs35l56->dsp, load_firmware);
867 	}
868 
869 	if (ret) {
870 		dev_dbg(cs35l56->base.dev, "wm_adsp_power_up ret %d\n", ret);
871 		return ret;
872 	}
873 
874 	return 0;
875 }
876 
877 static void cs35l56_reinit_patch(struct cs35l56_private *cs35l56)
878 {
879 	int ret;
880 
881 	ret = cs35l56_dsp_download_and_power_up(cs35l56, true);
882 	if (ret)
883 		return;
884 
885 	cs35l56_write_cal(cs35l56);
886 
887 	/* Always REINIT after applying patch or coefficients */
888 	cs35l56_mbox_send(&cs35l56->base, CS35L56_MBOX_CMD_AUDIO_REINIT);
889 }
890 
891 static void cs35l56_patch(struct cs35l56_private *cs35l56, bool firmware_missing)
892 {
893 	int ret;
894 
895 	/* Disable SoundWire interrupts to prevent race with IRQ handler thread */
896 	cs35l56_disable_sdw_interrupts(cs35l56);
897 
898 	ret = cs35l56_firmware_shutdown(&cs35l56->base);
899 	if (ret)
900 		goto err;
901 
902 	/*
903 	 * Use wm_adsp to load and apply the firmware patch and coefficient files,
904 	 * but only if firmware is missing. If firmware is already patched just
905 	 * power-up wm_adsp without downloading firmware.
906 	 */
907 	ret = cs35l56_dsp_download_and_power_up(cs35l56, firmware_missing);
908 	if (ret)
909 		goto err;
910 
911 	mutex_lock(&cs35l56->base.irq_lock);
912 
913 	reinit_completion(&cs35l56->init_completion);
914 
915 	cs35l56->soft_resetting = true;
916 	cs35l56_system_reset(&cs35l56->base, !!cs35l56->sdw_peripheral);
917 
918 	if (cs35l56->sdw_peripheral) {
919 		/*
920 		 * The system-reset causes the CS35L56 to detach from the bus.
921 		 * Wait for the manager to re-enumerate the CS35L56 and
922 		 * cs35l56_init() to run again.
923 		 */
924 		if (!wait_for_completion_timeout(&cs35l56->init_completion,
925 						 msecs_to_jiffies(5000))) {
926 			dev_err(cs35l56->base.dev, "%s: init_completion timed out (SDW)\n",
927 				__func__);
928 			goto err_unlock;
929 		}
930 	} else if (cs35l56_init(cs35l56)) {
931 		goto err_unlock;
932 	}
933 
934 	/* Check if the firmware is still reported missing */
935 	cs35l56_warn_if_firmware_missing(&cs35l56->base);
936 
937 	regmap_clear_bits(cs35l56->base.regmap,
938 			  cs35l56->base.fw_reg->prot_sts,
939 			  CS35L56_FIRMWARE_MISSING);
940 	cs35l56->base.fw_patched = true;
941 
942 	if (cs35l56_write_cal(cs35l56) == 0)
943 		cs35l56_mbox_send(&cs35l56->base, CS35L56_MBOX_CMD_AUDIO_REINIT);
944 
945 err_unlock:
946 	mutex_unlock(&cs35l56->base.irq_lock);
947 err:
948 	cs35l56_enable_sdw_interrupts(cs35l56);
949 }
950 
951 static void cs35l56_dsp_work(struct work_struct *work)
952 {
953 	struct cs35l56_private *cs35l56 = container_of(work,
954 						       struct cs35l56_private,
955 						       dsp_work);
956 	unsigned int firmware_version;
957 	bool firmware_missing;
958 	int ret;
959 
960 	if (!cs35l56->base.init_done)
961 		return;
962 
963 	PM_RUNTIME_ACQUIRE(cs35l56->base.dev, pm);
964 	ret = PM_RUNTIME_ACQUIRE_ERR(&pm);
965 	if (ret) {
966 		dev_err(cs35l56->base.dev, "dsp_work failed to runtime-resume: %d\n", ret);
967 		return;
968 	}
969 
970 	ret = cs35l56_read_prot_status(&cs35l56->base, &firmware_missing, &firmware_version);
971 	if (ret)
972 		return;
973 
974 	/* Populate fw file qualifier with the revision and security state */
975 	kfree(cs35l56->dsp.fwf_name);
976 	if (firmware_missing) {
977 		cs35l56->dsp.fwf_name = kasprintf(GFP_KERNEL, "%02x-dsp1", cs35l56->base.rev);
978 	} else {
979 		/* Firmware files must match the running firmware version */
980 		cs35l56->dsp.fwf_name = kasprintf(GFP_KERNEL,
981 						  "%02x%s-%06x-dsp1",
982 						  cs35l56->base.rev,
983 						  cs35l56->base.secured ? "-s" : "",
984 						  firmware_version);
985 	}
986 
987 	if (!cs35l56->dsp.fwf_name)
988 		return;
989 
990 	dev_dbg(cs35l56->base.dev, "DSP fwf name: '%s' system name: '%s'\n",
991 		cs35l56->dsp.fwf_name, cs35l56->dsp.system_name);
992 
993 	/*
994 	 * The firmware cannot be patched if it is already running from
995 	 * patch RAM. In this case the firmware files are versioned to
996 	 * match the running firmware version and will only contain
997 	 * tunings. We do not need to shutdown the firmware to apply
998 	 * tunings so can use the lower cost reinit sequence instead.
999 	 */
1000 	if (!firmware_missing)
1001 		cs35l56_reinit_patch(cs35l56);
1002 	else
1003 		cs35l56_patch(cs35l56, firmware_missing);
1004 
1005 	cs35l56_log_tuning(&cs35l56->base, &cs35l56->dsp.cs_dsp);
1006 }
1007 
1008 static struct snd_soc_dapm_context *cs35l56_power_up_for_cal(struct cs35l56_private *cs35l56)
1009 {
1010 	struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(cs35l56->component);
1011 	int ret;
1012 
1013 	ret = snd_soc_dapm_enable_pin(dapm, "Calibrate");
1014 	if (ret)
1015 		return ERR_PTR(ret);
1016 
1017 	snd_soc_dapm_sync(dapm);
1018 
1019 	return dapm;
1020 }
1021 
1022 static void cs35l56_power_down_after_cal(struct cs35l56_private *cs35l56)
1023 {
1024 	struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(cs35l56->component);
1025 
1026 	snd_soc_dapm_disable_pin(dapm, "Calibrate");
1027 	snd_soc_dapm_sync(dapm);
1028 }
1029 
1030 static ssize_t cs35l56_debugfs_calibrate_write(struct file *file,
1031 					       const char __user *from,
1032 					       size_t count, loff_t *ppos)
1033 {
1034 	struct cs35l56_base *cs35l56_base = file->private_data;
1035 	struct cs35l56_private *cs35l56 = cs35l56_private_from_base(cs35l56_base);
1036 	struct snd_soc_dapm_context *dapm;
1037 	ssize_t ret;
1038 
1039 	dapm = cs35l56_power_up_for_cal(cs35l56);
1040 	if (IS_ERR(dapm))
1041 		return PTR_ERR(dapm);
1042 
1043 	snd_soc_dapm_mutex_lock(dapm);
1044 	ret = cs35l56_calibrate_debugfs_write(&cs35l56->base, from, count, ppos);
1045 	snd_soc_dapm_mutex_unlock(dapm);
1046 
1047 	cs35l56_power_down_after_cal(cs35l56);
1048 
1049 	return ret;
1050 }
1051 
1052 static ssize_t cs35l56_debugfs_cal_temperature_write(struct file *file,
1053 						     const char __user *from,
1054 						     size_t count, loff_t *ppos)
1055 {
1056 	struct cs35l56_base *cs35l56_base = file->private_data;
1057 	struct cs35l56_private *cs35l56 = cs35l56_private_from_base(cs35l56_base);
1058 	struct snd_soc_dapm_context *dapm;
1059 	ssize_t ret;
1060 
1061 	dapm = cs35l56_power_up_for_cal(cs35l56);
1062 	if (IS_ERR(dapm))
1063 		return PTR_ERR(dapm);
1064 
1065 	ret = cs35l56_cal_ambient_debugfs_write(&cs35l56->base, from, count, ppos);
1066 	cs35l56_power_down_after_cal(cs35l56);
1067 
1068 	return ret;
1069 }
1070 
1071 static ssize_t cs35l56_debugfs_cal_data_read(struct file *file,
1072 					     char __user *to,
1073 					     size_t count, loff_t *ppos)
1074 {
1075 	struct cs35l56_base *cs35l56_base = file->private_data;
1076 	struct cs35l56_private *cs35l56 = cs35l56_private_from_base(cs35l56_base);
1077 	struct snd_soc_dapm_context *dapm;
1078 	ssize_t ret;
1079 
1080 	dapm = cs35l56_power_up_for_cal(cs35l56);
1081 	if (IS_ERR(dapm))
1082 		return PTR_ERR(dapm);
1083 
1084 	ret = cs35l56_cal_data_debugfs_read(&cs35l56->base, to, count, ppos);
1085 	cs35l56_power_down_after_cal(cs35l56);
1086 
1087 	return ret;
1088 }
1089 
1090 static int cs35l56_new_cal_data_apply(struct cs35l56_private *cs35l56)
1091 {
1092 	struct snd_soc_dapm_context *dapm;
1093 	int ret;
1094 
1095 	if (!cs35l56->base.cal_data_valid)
1096 		return -ENXIO;
1097 
1098 	if (cs35l56->base.secured)
1099 		return -EACCES;
1100 
1101 	dapm = cs35l56_power_up_for_cal(cs35l56);
1102 	if (IS_ERR(dapm))
1103 		return PTR_ERR(dapm);
1104 
1105 	snd_soc_dapm_mutex_lock(dapm);
1106 	ret = cs_amp_write_cal_coeffs(&cs35l56->dsp.cs_dsp,
1107 				      cs35l56->base.calibration_controls,
1108 				      &cs35l56->base.cal_data);
1109 	if (ret == 0)
1110 		cs35l56_mbox_send(&cs35l56->base, CS35L56_MBOX_CMD_AUDIO_REINIT);
1111 	else
1112 		ret = -EIO;
1113 
1114 	snd_soc_dapm_mutex_unlock(dapm);
1115 	cs35l56_power_down_after_cal(cs35l56);
1116 
1117 	return ret;
1118 }
1119 
1120 static ssize_t cs35l56_debugfs_cal_data_write(struct file *file,
1121 					      const char __user *from,
1122 					      size_t count, loff_t *ppos)
1123 {
1124 	struct cs35l56_base *cs35l56_base = file->private_data;
1125 	struct cs35l56_private *cs35l56 = cs35l56_private_from_base(cs35l56_base);
1126 	int ret;
1127 
1128 	ret = cs35l56_cal_data_debugfs_write(&cs35l56->base, from, count, ppos);
1129 	if (ret == -ENODATA)
1130 		return count;	/* Ignore writes of empty cal blobs */
1131 	else if (ret < 0)
1132 		return -EIO;
1133 
1134 	ret = cs35l56_new_cal_data_apply(cs35l56);
1135 	if (ret)
1136 		return ret;
1137 
1138 	return count;
1139 }
1140 
1141 static const struct cs35l56_cal_debugfs_fops cs35l56_cal_debugfs_fops = {
1142 	.calibrate = {
1143 		.write = cs35l56_debugfs_calibrate_write,
1144 	},
1145 	.cal_temperature = {
1146 		.write = cs35l56_debugfs_cal_temperature_write,
1147 	},
1148 	.cal_data = {
1149 		.read = cs35l56_debugfs_cal_data_read,
1150 		.write = cs35l56_debugfs_cal_data_write,
1151 	},
1152 };
1153 
1154 static int cs35l56_cal_data_rb_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 	if (!cs35l56->base.cal_data_valid)
1161 		return -ENODATA;
1162 
1163 	memcpy(ucontrol->value.bytes.data, &cs35l56->base.cal_data,
1164 	       sizeof(cs35l56->base.cal_data));
1165 
1166 	return 0;
1167 }
1168 
1169 static int cs35l56_cal_data_ctl_get(struct snd_kcontrol *kcontrol,
1170 				    struct snd_ctl_elem_value *ucontrol)
1171 {
1172 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
1173 	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
1174 
1175 	/*
1176 	 * This control is write-only but mixer libraries often try to read
1177 	 * a control before writing it. So we have to implement read.
1178 	 * Return zeros so a write of valid data will always be a change
1179 	 * from its "current value".
1180 	 */
1181 	memset(ucontrol->value.bytes.data, 0, sizeof(cs35l56->base.cal_data));
1182 
1183 	return 0;
1184 }
1185 
1186 static int cs35l56_cal_data_ctl_set(struct snd_kcontrol *kcontrol,
1187 				    struct snd_ctl_elem_value *ucontrol)
1188 {
1189 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
1190 	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
1191 	const struct cirrus_amp_cal_data *cal_data = (const void *)ucontrol->value.bytes.data;
1192 	int ret;
1193 
1194 	if (cs35l56->base.cal_data_valid)
1195 		return -EACCES;
1196 
1197 	ret = cs35l56_stash_calibration(&cs35l56->base, cal_data);
1198 	if (ret)
1199 		return ret;
1200 
1201 	ret = cs35l56_new_cal_data_apply(cs35l56);
1202 	if (ret < 0)
1203 		return ret;
1204 
1205 	return 1;
1206 }
1207 
1208 static int cs35l56_cal_ambient_ctl_get(struct snd_kcontrol *kcontrol,
1209 				       struct snd_ctl_elem_value *ucontrol)
1210 {
1211 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
1212 	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
1213 
1214 	ucontrol->value.integer.value[0] = cs35l56->ambient_ctl_value;
1215 
1216 	return 0;
1217 }
1218 
1219 static int cs35l56_cal_ambient_ctl_set(struct snd_kcontrol *kcontrol,
1220 				       struct snd_ctl_elem_value *ucontrol)
1221 {
1222 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
1223 	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
1224 	struct snd_soc_dapm_context *dapm;
1225 	int temperature = ucontrol->value.integer.value[0];
1226 	int ret;
1227 
1228 	if (temperature == cs35l56->ambient_ctl_value)
1229 		return 0;
1230 
1231 	if ((temperature < 0) || (temperature > 40))
1232 		return -EINVAL;
1233 
1234 	dapm = cs35l56_power_up_for_cal(cs35l56);
1235 	if (IS_ERR(dapm))
1236 		return PTR_ERR(dapm);
1237 
1238 	ret = cs_amp_write_ambient_temp(&cs35l56->dsp.cs_dsp,
1239 					cs35l56->base.calibration_controls,
1240 					temperature);
1241 	cs35l56_power_down_after_cal(cs35l56);
1242 
1243 	if (ret)
1244 		return ret;
1245 
1246 	cs35l56->ambient_ctl_value = temperature;
1247 
1248 	return 1;
1249 }
1250 
1251 static int cs35l56_calibrate_ctl_get(struct snd_kcontrol *kcontrol,
1252 				     struct snd_ctl_elem_value *ucontrol)
1253 {
1254 	/*
1255 	 * Allow reading because of user-side libraries that assume all
1256 	 * controls are readable. But always return false to prevent dumb
1257 	 * save-restore tools like alsactl accidentically triggering a
1258 	 * factory calibration when they restore.
1259 	 */
1260 	ucontrol->value.integer.value[0] = 0;
1261 
1262 	return 0;
1263 }
1264 
1265 static int cs35l56_calibrate_ctl_set(struct snd_kcontrol *kcontrol,
1266 				     struct snd_ctl_elem_value *ucontrol)
1267 {
1268 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
1269 	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
1270 	struct snd_soc_dapm_context *dapm;
1271 	int ret;
1272 
1273 	if (ucontrol->value.integer.value[0] == 0)
1274 		return 0;
1275 
1276 	dapm = cs35l56_power_up_for_cal(cs35l56);
1277 	if (IS_ERR(dapm))
1278 		return PTR_ERR(dapm);
1279 
1280 	snd_soc_dapm_mutex_lock(dapm);
1281 	ret = cs35l56_factory_calibrate(&cs35l56->base);
1282 	snd_soc_dapm_mutex_unlock(dapm);
1283 	cs35l56_power_down_after_cal(cs35l56);
1284 	if (ret < 0)
1285 		return ret;
1286 
1287 	return 1;
1288 }
1289 
1290 static const struct snd_kcontrol_new cs35l56_cal_data_restore_controls[] = {
1291 	SND_SOC_BYTES_E("CAL_DATA", 0, sizeof(struct cirrus_amp_cal_data) / sizeof(u32),
1292 			cs35l56_cal_data_ctl_get, cs35l56_cal_data_ctl_set),
1293 	SND_SOC_BYTES_E_ACC("CAL_DATA_RB", 0, sizeof(struct cirrus_amp_cal_data) / sizeof(u32),
1294 			cs35l56_cal_data_rb_ctl_get, NULL,
1295 			SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE),
1296 };
1297 
1298 static const struct snd_kcontrol_new cs35l56_cal_perform_controls[] = {
1299 	SOC_SINGLE_EXT("CAL_AMBIENT", SND_SOC_NOPM, 0, 40, 0,
1300 		       cs35l56_cal_ambient_ctl_get, cs35l56_cal_ambient_ctl_set),
1301 	SOC_SINGLE_BOOL_EXT_ACC("Calibrate Switch", 0,
1302 				cs35l56_calibrate_ctl_get, cs35l56_calibrate_ctl_set,
1303 				SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_VOLATILE),
1304 };
1305 
1306 VISIBLE_IF_KUNIT int cs35l56_set_fw_suffix(struct cs35l56_private *cs35l56)
1307 {
1308 	unsigned short vendor, device;
1309 	const char *vendor_id;
1310 	int ret;
1311 
1312 	if (cs35l56->dsp.fwf_suffix)
1313 		return 0;
1314 
1315 	if (cs35l56->sdw_peripheral) {
1316 		cs35l56->dsp.fwf_suffix = devm_kasprintf(cs35l56->base.dev, GFP_KERNEL,
1317 							 "l%uu%u",
1318 							 cs35l56->sdw_link_num,
1319 							 cs35l56->sdw_unique_id);
1320 		if (!cs35l56->dsp.fwf_suffix)
1321 			return -ENOMEM;
1322 
1323 		/*
1324 		 * There are published firmware files for L56 B0 silicon using
1325 		 * the ALSA prefix as the filename suffix. Default to trying these
1326 		 * first, with the new SoundWire suffix as a fallback.
1327 		 * None of these older systems use a vendor-specific ID.
1328 		 */
1329 		if ((cs35l56->base.type == 0x56) && (cs35l56->base.rev == 0xb0)) {
1330 			cs35l56->fallback_fw_suffix = cs35l56->dsp.fwf_suffix;
1331 			cs35l56->dsp.fwf_suffix = cs35l56->component->name_prefix;
1332 
1333 			return 0;
1334 		}
1335 	}
1336 
1337 	/*
1338 	 * Some manufacturers use the same SSID on multiple products and have
1339 	 * a vendor-specific qualifier to distinguish different models.
1340 	 * Models with the same SSID but different qualifier might require
1341 	 * different audio firmware, or they might all have the same audio
1342 	 * firmware.
1343 	 * Try searching for a firmware with this qualifier first, else
1344 	 * fallback to standard naming.
1345 	 */
1346 	if (snd_soc_card_get_pci_ssid(cs35l56->component->card, &vendor, &device) < 0) {
1347 		vendor_id = cs_amp_devm_get_vendor_specific_variant_id(cs35l56->base.dev, -1, -1);
1348 	} else {
1349 		vendor_id = cs_amp_devm_get_vendor_specific_variant_id(cs35l56->base.dev,
1350 								       vendor, device);
1351 	}
1352 	ret = PTR_ERR_OR_ZERO(vendor_id);
1353 	if (ret == -ENOENT)
1354 		return 0;
1355 	else if (ret)
1356 		return ret;
1357 
1358 	if (vendor_id) {
1359 		if (cs35l56->dsp.fwf_suffix)
1360 			cs35l56->fallback_fw_suffix = cs35l56->dsp.fwf_suffix;
1361 		else
1362 			cs35l56->fallback_fw_suffix = cs35l56->component->name_prefix;
1363 
1364 		cs35l56->dsp.fwf_suffix = devm_kasprintf(cs35l56->base.dev, GFP_KERNEL,
1365 							 "%s-%s",
1366 							 vendor_id,
1367 							 cs35l56->fallback_fw_suffix);
1368 		if (!cs35l56->dsp.fwf_suffix)
1369 			return -ENOMEM;
1370 	}
1371 
1372 	return 0;
1373 }
1374 EXPORT_SYMBOL_IF_KUNIT(cs35l56_set_fw_suffix);
1375 
1376 VISIBLE_IF_KUNIT int cs35l56_set_fw_name(struct snd_soc_component *component)
1377 {
1378 	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
1379 	unsigned short vendor, device;
1380 	int ret;
1381 
1382 	if ((cs35l56->speaker_id < 0) && cs35l56->base.num_onchip_spkid_gpios) {
1383 		PM_RUNTIME_ACQUIRE(cs35l56->base.dev, pm);
1384 		ret = PM_RUNTIME_ACQUIRE_ERR(&pm);
1385 		if (ret)
1386 			return ret;
1387 
1388 		ret = cs35l56_configure_onchip_spkid_pads(&cs35l56->base);
1389 		if (ret)
1390 			return ret;
1391 
1392 		ret = cs35l56_read_onchip_spkid(&cs35l56->base);
1393 		if (ret < 0)
1394 			return ret;
1395 
1396 		cs35l56->speaker_id = ret;
1397 	}
1398 
1399 	if (!cs35l56->dsp.system_name &&
1400 	    (snd_soc_card_get_pci_ssid(component->card, &vendor, &device) == 0)) {
1401 		/* Append a speaker qualifier if there is a speaker ID */
1402 		if (cs35l56->speaker_id >= 0) {
1403 			cs35l56->dsp.system_name = devm_kasprintf(cs35l56->base.dev,
1404 								  GFP_KERNEL,
1405 								  "%04x%04x-spkid%d",
1406 								  vendor, device,
1407 								  cs35l56->speaker_id);
1408 		} else {
1409 			cs35l56->dsp.system_name = devm_kasprintf(cs35l56->base.dev,
1410 								  GFP_KERNEL,
1411 								  "%04x%04x",
1412 								  vendor, device);
1413 		}
1414 		if (!cs35l56->dsp.system_name)
1415 			return -ENOMEM;
1416 	}
1417 
1418 	return 0;
1419 }
1420 EXPORT_SYMBOL_IF_KUNIT(cs35l56_set_fw_name);
1421 
1422 static int _cs35l56_component_probe(struct snd_soc_component *component)
1423 {
1424 	struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(component);
1425 	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
1426 	struct dentry *debugfs_root = component->debugfs_root;
1427 	int ret;
1428 
1429 	BUILD_BUG_ON(ARRAY_SIZE(cs35l56_tx_input_texts) != ARRAY_SIZE(cs35l56_tx_input_values));
1430 
1431 	cs35l56->dsp.part = kasprintf(GFP_KERNEL, "cs35l%02x", cs35l56->base.type);
1432 	if (!cs35l56->dsp.part)
1433 		return -ENOMEM;
1434 
1435 	cs35l56->component = component;
1436 	ret = cs35l56_set_fw_name(component);
1437 	if (ret)
1438 		return ret;
1439 
1440 	ret = cs35l56_set_fw_suffix(cs35l56);
1441 	if (ret)
1442 		return ret;
1443 
1444 	wm_adsp2_component_probe(&cs35l56->dsp, component);
1445 
1446 	debugfs_create_bool("init_done", 0444, debugfs_root, &cs35l56->base.init_done);
1447 	debugfs_create_bool("can_hibernate", 0444, debugfs_root, &cs35l56->base.can_hibernate);
1448 	debugfs_create_bool("fw_patched", 0444, debugfs_root, &cs35l56->base.fw_patched);
1449 
1450 
1451 	switch (cs35l56->base.type) {
1452 	case 0x54:
1453 	case 0x56:
1454 	case 0x57:
1455 		ret = snd_soc_add_component_controls(component, cs35l56_controls,
1456 						     ARRAY_SIZE(cs35l56_controls));
1457 		break;
1458 	case 0x63:
1459 	case 0x62:
1460 		ret = snd_soc_add_component_controls(component, cs35l63_controls,
1461 						     ARRAY_SIZE(cs35l63_controls));
1462 		break;
1463 	default:
1464 		ret = -ENODEV;
1465 		break;
1466 	}
1467 
1468 	if (!ret && IS_ENABLED(CONFIG_SND_SOC_CS35L56_CAL_SET_CTRL)) {
1469 		ret = snd_soc_add_component_controls(component,
1470 						     cs35l56_cal_data_restore_controls,
1471 						     ARRAY_SIZE(cs35l56_cal_data_restore_controls));
1472 	}
1473 
1474 	if (!ret && IS_ENABLED(CONFIG_SND_SOC_CS35L56_CAL_PERFORM_CTRL)) {
1475 		ret = snd_soc_add_component_controls(component,
1476 						     cs35l56_cal_perform_controls,
1477 						     ARRAY_SIZE(cs35l56_cal_perform_controls));
1478 	}
1479 
1480 	if (ret)
1481 		return dev_err_probe(cs35l56->base.dev, ret, "unable to add controls\n");
1482 
1483 	ret = snd_soc_dapm_disable_pin(dapm, "Calibrate");
1484 	if (ret)
1485 		return ret;
1486 
1487 	if (IS_ENABLED(CONFIG_SND_SOC_CS35L56_CAL_DEBUGFS))
1488 		cs35l56_create_cal_debugfs(&cs35l56->base, &cs35l56_cal_debugfs_fops);
1489 
1490 	queue_work(cs35l56->dsp_wq, &cs35l56->dsp_work);
1491 
1492 	return 0;
1493 }
1494 
1495 static void cs35l56_component_remove(struct snd_soc_component *component)
1496 {
1497 	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
1498 
1499 	cancel_work_sync(&cs35l56->dsp_work);
1500 
1501 	cs35l56_remove_cal_debugfs(&cs35l56->base);
1502 
1503 	if (cs35l56->dsp.cs_dsp.booted)
1504 		wm_adsp_power_down(&cs35l56->dsp);
1505 
1506 	wm_adsp2_component_remove(&cs35l56->dsp, component);
1507 
1508 	kfree(cs35l56->dsp.part);
1509 	cs35l56->dsp.part = NULL;
1510 
1511 	kfree(cs35l56->dsp.fwf_name);
1512 	cs35l56->dsp.fwf_name = NULL;
1513 
1514 	cs35l56->component = NULL;
1515 }
1516 
1517 static int cs35l56_component_probe(struct snd_soc_component *component)
1518 {
1519 	int ret;
1520 
1521 	ret = _cs35l56_component_probe(component);
1522 	if (ret < 0)
1523 		cs35l56_component_remove(component);
1524 
1525 	return ret;
1526 }
1527 
1528 static int cs35l56_set_bias_level(struct snd_soc_component *component,
1529 				  enum snd_soc_bias_level level)
1530 {
1531 	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
1532 	struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(component);
1533 
1534 	switch (level) {
1535 	case SND_SOC_BIAS_STANDBY:
1536 		/*
1537 		 * Wait for patching to complete when transitioning from
1538 		 * BIAS_OFF to BIAS_STANDBY
1539 		 */
1540 		if (snd_soc_dapm_get_bias_level(dapm) == SND_SOC_BIAS_OFF)
1541 			cs35l56_wait_dsp_ready(cs35l56);
1542 
1543 		break;
1544 	default:
1545 		break;
1546 	}
1547 
1548 	return 0;
1549 }
1550 
1551 static const struct snd_soc_component_driver soc_component_dev_cs35l56 = {
1552 	.probe = cs35l56_component_probe,
1553 	.remove = cs35l56_component_remove,
1554 
1555 	.dapm_widgets = cs35l56_dapm_widgets,
1556 	.num_dapm_widgets = ARRAY_SIZE(cs35l56_dapm_widgets),
1557 	.dapm_routes = cs35l56_audio_map,
1558 	.num_dapm_routes = ARRAY_SIZE(cs35l56_audio_map),
1559 
1560 	.set_bias_level = cs35l56_set_bias_level,
1561 
1562 	.suspend_bias_off = 1, /* see cs35l56_system_resume() */
1563 };
1564 
1565 static int __maybe_unused cs35l56_runtime_suspend_i2c_spi(struct device *dev)
1566 {
1567 	struct cs35l56_private *cs35l56 = dev_get_drvdata(dev);
1568 
1569 	return cs35l56_runtime_suspend_common(&cs35l56->base);
1570 }
1571 
1572 static int __maybe_unused cs35l56_runtime_resume_i2c_spi(struct device *dev)
1573 {
1574 	struct cs35l56_private *cs35l56 = dev_get_drvdata(dev);
1575 
1576 	return cs35l56_runtime_resume_common(&cs35l56->base, false);
1577 }
1578 
1579 int cs35l56_system_suspend(struct device *dev)
1580 {
1581 	struct cs35l56_private *cs35l56 = dev_get_drvdata(dev);
1582 	int ret;
1583 
1584 	dev_dbg(dev, "system_suspend\n");
1585 
1586 	if (cs35l56->component)
1587 		flush_work(&cs35l56->dsp_work);
1588 
1589 	/*
1590 	 * The interrupt line is normally shared, but after we start suspending
1591 	 * we can't check if our device is the source of an interrupt, and can't
1592 	 * clear it. Prevent this race by temporarily disabling the parent irq
1593 	 * until we reach _no_irq.
1594 	 */
1595 	if (cs35l56->base.irq)
1596 		disable_irq(cs35l56->base.irq);
1597 
1598 	ret = pm_runtime_force_suspend(dev);
1599 	if ((ret < 0) && cs35l56->base.irq)
1600 		enable_irq(cs35l56->base.irq);
1601 
1602 	return ret;
1603 }
1604 EXPORT_SYMBOL_GPL(cs35l56_system_suspend);
1605 
1606 int cs35l56_system_suspend_late(struct device *dev)
1607 {
1608 	struct cs35l56_private *cs35l56 = dev_get_drvdata(dev);
1609 
1610 	dev_dbg(dev, "system_suspend_late\n");
1611 
1612 	/*
1613 	 * Assert RESET before removing supplies.
1614 	 * RESET is usually shared by all amps so it must not be asserted until
1615 	 * all driver instances have done their suspend() stage.
1616 	 */
1617 	if (cs35l56->base.reset_gpio) {
1618 		gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 0);
1619 		cs35l56_wait_min_reset_pulse();
1620 	}
1621 
1622 	regulator_bulk_disable(ARRAY_SIZE(cs35l56->supplies), cs35l56->supplies);
1623 
1624 	return 0;
1625 }
1626 EXPORT_SYMBOL_GPL(cs35l56_system_suspend_late);
1627 
1628 int cs35l56_system_suspend_no_irq(struct device *dev)
1629 {
1630 	struct cs35l56_private *cs35l56 = dev_get_drvdata(dev);
1631 
1632 	dev_dbg(dev, "system_suspend_no_irq\n");
1633 
1634 	/* Handlers are now disabled so the parent IRQ can safely be re-enabled. */
1635 	if (cs35l56->base.irq)
1636 		enable_irq(cs35l56->base.irq);
1637 
1638 	return 0;
1639 }
1640 EXPORT_SYMBOL_GPL(cs35l56_system_suspend_no_irq);
1641 
1642 int cs35l56_system_resume_no_irq(struct device *dev)
1643 {
1644 	struct cs35l56_private *cs35l56 = dev_get_drvdata(dev);
1645 
1646 	dev_dbg(dev, "system_resume_no_irq\n");
1647 
1648 	/*
1649 	 * WAKE interrupts unmask if the CS35L56 hibernates, which can cause
1650 	 * spurious interrupts, and the interrupt line is normally shared.
1651 	 * We can't check if our device is the source of an interrupt, and can't
1652 	 * clear it, until it has fully resumed. Prevent this race by temporarily
1653 	 * disabling the parent irq until we complete resume().
1654 	 */
1655 	if (cs35l56->base.irq)
1656 		disable_irq(cs35l56->base.irq);
1657 
1658 	return 0;
1659 }
1660 EXPORT_SYMBOL_GPL(cs35l56_system_resume_no_irq);
1661 
1662 int cs35l56_system_resume_early(struct device *dev)
1663 {
1664 	struct cs35l56_private *cs35l56 = dev_get_drvdata(dev);
1665 	int ret;
1666 
1667 	dev_dbg(dev, "system_resume_early\n");
1668 
1669 	/* Ensure a spec-compliant RESET pulse. */
1670 	if (cs35l56->base.reset_gpio) {
1671 		gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 0);
1672 		cs35l56_wait_min_reset_pulse();
1673 	}
1674 
1675 	/* Enable supplies before releasing RESET. */
1676 	ret = regulator_bulk_enable(ARRAY_SIZE(cs35l56->supplies), cs35l56->supplies);
1677 	if (ret) {
1678 		dev_err(dev, "system_resume_early failed to enable supplies: %d\n", ret);
1679 		return ret;
1680 	}
1681 
1682 	/* Release shared RESET before drivers start resume(). */
1683 	gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 1);
1684 
1685 	return 0;
1686 }
1687 EXPORT_SYMBOL_GPL(cs35l56_system_resume_early);
1688 
1689 int cs35l56_system_resume(struct device *dev)
1690 {
1691 	struct cs35l56_private *cs35l56 = dev_get_drvdata(dev);
1692 	int ret;
1693 
1694 	dev_dbg(dev, "system_resume\n");
1695 
1696 	/*
1697 	 * We might have done a hard reset or the CS35L56 was power-cycled
1698 	 * so wait for control port to be ready.
1699 	 */
1700 	cs35l56_wait_control_port_ready();
1701 
1702 	/* Undo pm_runtime_force_suspend() before re-enabling the irq */
1703 	ret = pm_runtime_force_resume(dev);
1704 	if (cs35l56->base.irq)
1705 		enable_irq(cs35l56->base.irq);
1706 
1707 	if (ret)
1708 		return ret;
1709 
1710 	/* Firmware won't have been loaded if the component hasn't probed */
1711 	if (!cs35l56->component)
1712 		return 0;
1713 
1714 	ret = cs35l56_is_fw_reload_needed(&cs35l56->base);
1715 	dev_dbg(cs35l56->base.dev, "fw_reload_needed: %d\n", ret);
1716 	if (ret < 1)
1717 		return ret;
1718 
1719 	cs35l56->base.fw_patched = false;
1720 	wm_adsp_power_down(&cs35l56->dsp);
1721 	queue_work(cs35l56->dsp_wq, &cs35l56->dsp_work);
1722 
1723 	/*
1724 	 * suspend_bias_off ensures we are now in BIAS_OFF so there will be
1725 	 * a BIAS_OFF->BIAS_STANDBY transition to complete dsp patching.
1726 	 */
1727 
1728 	return 0;
1729 }
1730 EXPORT_SYMBOL_GPL(cs35l56_system_resume);
1731 
1732 static int cs35l56_control_add_nop(struct wm_adsp *dsp, struct cs_dsp_coeff_ctl *cs_ctl)
1733 {
1734 	return 0;
1735 }
1736 
1737 static int cs35l56_dsp_init(struct cs35l56_private *cs35l56)
1738 {
1739 	struct wm_adsp *dsp;
1740 	int ret;
1741 
1742 	cs35l56->dsp_wq = create_singlethread_workqueue("cs35l56-dsp");
1743 	if (!cs35l56->dsp_wq)
1744 		return -ENOMEM;
1745 
1746 	INIT_WORK(&cs35l56->dsp_work, cs35l56_dsp_work);
1747 
1748 	dsp = &cs35l56->dsp;
1749 	cs35l56_init_cs_dsp(&cs35l56->base, &dsp->cs_dsp);
1750 
1751 	/*
1752 	 * dsp->part is filled in later as it is based on the DEVID. In a
1753 	 * SoundWire system that cannot be read until enumeration has occurred
1754 	 * and the device has attached.
1755 	 */
1756 	dsp->fw = 12;
1757 	dsp->wmfw_optional = true;
1758 
1759 	/*
1760 	 * None of the firmware controls need to be exported so add a no-op
1761 	 * callback that suppresses creating an ALSA control.
1762 	 */
1763 	dsp->control_add = &cs35l56_control_add_nop;
1764 
1765 	dev_dbg(cs35l56->base.dev, "DSP system name: '%s'\n", dsp->system_name);
1766 
1767 	ret = wm_halo_init(dsp);
1768 	if (ret != 0) {
1769 		dev_err(cs35l56->base.dev, "wm_halo_init failed\n");
1770 		return ret;
1771 	}
1772 
1773 	return 0;
1774 }
1775 
1776 static int cs35l56_read_fwnode_u32_array(struct device *dev,
1777 					struct fwnode_handle *parent_node,
1778 					const char *prop_name,
1779 					int max_count,
1780 					u32 *dest)
1781 {
1782 	int count, ret;
1783 
1784 	count = fwnode_property_count_u32(parent_node, prop_name);
1785 	if ((count == 0) || (count == -EINVAL) || (count == -ENODATA)) {
1786 		dev_dbg(dev, "%s not found in %s\n", prop_name, fwnode_get_name(parent_node));
1787 		return 0;
1788 	}
1789 
1790 	if (count < 0) {
1791 		dev_err(dev, "Get %s error:%d\n", prop_name, count);
1792 		return count;
1793 	}
1794 
1795 	if (count > max_count) {
1796 		dev_err(dev, "%s too many entries (%d)\n", prop_name, count);
1797 		return -EOVERFLOW;
1798 	}
1799 
1800 	ret = fwnode_property_read_u32_array(parent_node, prop_name, dest, count);
1801 	if (ret) {
1802 		dev_err(dev, "Error reading %s: %d\n", prop_name, ret);
1803 		return ret;
1804 	}
1805 
1806 	return count;
1807 }
1808 
1809 static int cs35l56_process_xu_onchip_speaker_id(struct cs35l56_private *cs35l56,
1810 						struct fwnode_handle *ext_node)
1811 {
1812 	static const char * const gpio_name = "01fa-spk-id-gpios-onchip";
1813 	static const char * const pull_name = "01fa-spk-id-gpios-onchip-pull";
1814 	u32 gpios[5], pulls[5];
1815 	int num_gpios, num_pulls;
1816 	int ret;
1817 
1818 	static_assert(ARRAY_SIZE(gpios) == ARRAY_SIZE(cs35l56->base.onchip_spkid_gpios));
1819 	static_assert(ARRAY_SIZE(pulls) == ARRAY_SIZE(cs35l56->base.onchip_spkid_pulls));
1820 
1821 	num_gpios = cs35l56_read_fwnode_u32_array(cs35l56->base.dev, ext_node, gpio_name,
1822 						  ARRAY_SIZE(gpios), gpios);
1823 	if (num_gpios < 1)
1824 		return num_gpios;
1825 
1826 	num_pulls = cs35l56_read_fwnode_u32_array(cs35l56->base.dev, ext_node, pull_name,
1827 						  ARRAY_SIZE(pulls), pulls);
1828 	if (num_pulls < 0)
1829 		return num_pulls;
1830 
1831 	if (num_pulls && (num_pulls != num_gpios)) {
1832 		dev_warn(cs35l56->base.dev, "%s count(%d) != %s count(%d)\n",
1833 			 pull_name, num_pulls, gpio_name, num_gpios);
1834 	}
1835 
1836 	ret = cs35l56_check_and_save_onchip_spkid_gpios(&cs35l56->base,
1837 							gpios, num_gpios,
1838 							pulls, num_pulls);
1839 	if (ret) {
1840 		return dev_err_probe(cs35l56->base.dev, ret, "Error in %s/%s\n",
1841 				     gpio_name, pull_name);
1842 	}
1843 
1844 	return 0;
1845 }
1846 
1847 VISIBLE_IF_KUNIT int cs35l56_process_xu_properties(struct cs35l56_private *cs35l56)
1848 {
1849 	struct fwnode_handle *ext_node = NULL;
1850 	struct fwnode_handle *link;
1851 	int ret;
1852 
1853 	if (!cs35l56->sdw_peripheral)
1854 		return 0;
1855 
1856 	fwnode_for_each_child_node(dev_fwnode(cs35l56->base.dev), link) {
1857 		ext_node = fwnode_get_named_child_node(link,
1858 						       "mipi-sdca-function-expansion-subproperties");
1859 		if (ext_node) {
1860 			fwnode_handle_put(link);
1861 			break;
1862 		}
1863 	}
1864 
1865 	if (!ext_node)
1866 		return 0;
1867 
1868 	ret = cs35l56_process_xu_onchip_speaker_id(cs35l56, ext_node);
1869 	fwnode_handle_put(ext_node);
1870 
1871 	return ret;
1872 }
1873 EXPORT_SYMBOL_IF_KUNIT(cs35l56_process_xu_properties);
1874 
1875 VISIBLE_IF_KUNIT int cs35l56_get_firmware_uid(struct cs35l56_private *cs35l56)
1876 {
1877 	struct device *dev = cs35l56->base.dev;
1878 	const char *prop;
1879 	int ret;
1880 
1881 	ret = device_property_read_string(dev, "cirrus,firmware-uid", &prop);
1882 	/* If bad sw node property, return 0 and fallback to legacy firmware path */
1883 	if (ret < 0)
1884 		return 0;
1885 
1886 	/* Append a speaker qualifier if there is a speaker ID */
1887 	if (cs35l56->speaker_id >= 0)
1888 		cs35l56->dsp.system_name = devm_kasprintf(dev, GFP_KERNEL, "%s-spkid%d",
1889 							  prop, cs35l56->speaker_id);
1890 	else
1891 		cs35l56->dsp.system_name = devm_kstrdup(dev, prop, GFP_KERNEL);
1892 
1893 	if (cs35l56->dsp.system_name == NULL)
1894 		return -ENOMEM;
1895 
1896 	dev_dbg(dev, "Firmware UID: %s\n", cs35l56->dsp.system_name);
1897 
1898 	return 0;
1899 }
1900 EXPORT_SYMBOL_IF_KUNIT(cs35l56_get_firmware_uid);
1901 
1902 /*
1903  * Some SoundWire laptops have a spk-id-gpios property but it points to
1904  * the wrong ACPI Device node so can't be used to get the GPIO. Try to
1905  * find the SDCA node containing the GpioIo resource and add a GPIO
1906  * mapping to it.
1907  */
1908 static const struct acpi_gpio_params cs35l56_af01_first_gpio = { 0, 0, false };
1909 static const struct acpi_gpio_mapping cs35l56_af01_spkid_gpios_mapping[] = {
1910 	{ "spk-id-gpios", &cs35l56_af01_first_gpio, 1 },
1911 	{ }
1912 };
1913 
1914 static void cs35l56_acpi_dev_release_driver_gpios(void *adev)
1915 {
1916 	acpi_dev_remove_driver_gpios(adev);
1917 }
1918 
1919 static int cs35l56_try_get_broken_sdca_spkid_gpio(struct cs35l56_private *cs35l56)
1920 {
1921 	struct fwnode_handle *af01_fwnode;
1922 	const union acpi_object *obj;
1923 	struct gpio_desc *desc;
1924 	int ret;
1925 
1926 	/* Find the SDCA node containing the GpioIo */
1927 	af01_fwnode = device_get_named_child_node(cs35l56->base.dev, "AF01");
1928 	if (!af01_fwnode) {
1929 		dev_dbg(cs35l56->base.dev, "No AF01 node\n");
1930 		return -ENOENT;
1931 	}
1932 
1933 	ret = acpi_dev_get_property(ACPI_COMPANION(cs35l56->base.dev),
1934 				    "spk-id-gpios", ACPI_TYPE_PACKAGE, &obj);
1935 	if (ret) {
1936 		dev_dbg(cs35l56->base.dev, "Could not get spk-id-gpios package: %d\n", ret);
1937 		fwnode_handle_put(af01_fwnode);
1938 		return -ENOENT;
1939 	}
1940 
1941 	/* The broken properties we can handle are a 4-element package (one GPIO) */
1942 	if (obj->package.count != 4) {
1943 		dev_warn(cs35l56->base.dev, "Unexpected spk-id element count %d\n",
1944 			 obj->package.count);
1945 		fwnode_handle_put(af01_fwnode);
1946 		return -ENOENT;
1947 	}
1948 
1949 	/* Add a GPIO mapping if it doesn't already have one */
1950 	if (!fwnode_property_present(af01_fwnode, "spk-id-gpios")) {
1951 		struct acpi_device *adev = to_acpi_device_node(af01_fwnode);
1952 
1953 		/*
1954 		 * Can't use devm_acpi_dev_add_driver_gpios() because the
1955 		 * mapping isn't being added to the node pointed to by
1956 		 * ACPI_COMPANION().
1957 		 */
1958 		ret = acpi_dev_add_driver_gpios(adev, cs35l56_af01_spkid_gpios_mapping);
1959 		if (ret) {
1960 			fwnode_handle_put(af01_fwnode);
1961 			return dev_err_probe(cs35l56->base.dev, ret,
1962 					     "Failed to add gpio mapping to AF01\n");
1963 		}
1964 
1965 		ret = devm_add_action_or_reset(cs35l56->base.dev,
1966 					       cs35l56_acpi_dev_release_driver_gpios,
1967 					       adev);
1968 		if (ret) {
1969 			fwnode_handle_put(af01_fwnode);
1970 			return ret;
1971 		}
1972 
1973 		dev_dbg(cs35l56->base.dev, "Added spk-id-gpios mapping to AF01\n");
1974 	}
1975 
1976 	desc = fwnode_gpiod_get_index(af01_fwnode, "spk-id", 0, GPIOD_IN, NULL);
1977 	if (IS_ERR(desc)) {
1978 		fwnode_handle_put(af01_fwnode);
1979 		ret = PTR_ERR(desc);
1980 		return dev_err_probe(cs35l56->base.dev, ret, "Get GPIO from AF01 failed\n");
1981 	}
1982 
1983 	ret = gpiod_get_value_cansleep(desc);
1984 	gpiod_put(desc);
1985 
1986 	if (ret < 0) {
1987 		fwnode_handle_put(af01_fwnode);
1988 		dev_err_probe(cs35l56->base.dev, ret, "Error reading spk-id GPIO\n");
1989 		return ret;
1990 	}
1991 
1992 	fwnode_handle_put(af01_fwnode);
1993 
1994 	dev_info(cs35l56->base.dev, "Got spk-id from AF01\n");
1995 
1996 	return ret;
1997 }
1998 
1999 static int cs35l56_component_register(struct cs35l56_private *cs35l56)
2000 {
2001 	int ret;
2002 
2003 	ret = snd_soc_register_component(cs35l56->base.dev,
2004 					 &soc_component_dev_cs35l56,
2005 					 cs35l56_dai, ARRAY_SIZE(cs35l56_dai));
2006 	if (ret < 0) {
2007 		dev_err(cs35l56->base.dev, "Register codec failed: %d\n", ret);
2008 		return ret;
2009 	}
2010 
2011 	cs35l56->component_registered = true;
2012 
2013 	return 0;
2014 }
2015 
2016 static void cs35l56_component_register_work(struct work_struct *work)
2017 {
2018 	struct cs35l56_private *cs35l56 = container_of(work,
2019 						       struct cs35l56_private,
2020 						       component_register_work);
2021 	int ret;
2022 
2023 	guard(mutex)(&cs35l56_component_register_lock);
2024 
2025 	if (cs35l56_shutting_down)
2026 		return;
2027 
2028 	PM_RUNTIME_ACQUIRE_AUTOSUSPEND(cs35l56->base.dev, pm_err);
2029 	ret = PM_RUNTIME_ACQUIRE_ERR(&pm_err);
2030 	if (ret) {
2031 		dev_err(cs35l56->base.dev, "register_work failed to get pm_runtime: %d\n", ret);
2032 		return;
2033 	}
2034 
2035 	cs35l56_component_register(cs35l56);
2036 }
2037 
2038 int cs35l56_common_probe(struct cs35l56_private *cs35l56, int irq)
2039 {
2040 	int ret;
2041 
2042 	init_completion(&cs35l56->init_completion);
2043 	mutex_init(&cs35l56->base.irq_lock);
2044 	cs35l56->base.cal_index = -1;
2045 	cs35l56->speaker_id = -ENOENT;
2046 	INIT_WORK(&cs35l56->component_register_work, cs35l56_component_register_work);
2047 
2048 	dev_set_drvdata(cs35l56->base.dev, cs35l56);
2049 
2050 	cs35l56_fill_supply_names(cs35l56->supplies);
2051 	ret = devm_regulator_bulk_get(cs35l56->base.dev, ARRAY_SIZE(cs35l56->supplies),
2052 				      cs35l56->supplies);
2053 	if (ret != 0)
2054 		return dev_err_probe(cs35l56->base.dev, ret, "Failed to request supplies\n");
2055 
2056 	/* Reset could be controlled by the BIOS or shared by multiple amps */
2057 	cs35l56->base.reset_gpio = devm_gpiod_get_optional(cs35l56->base.dev, "reset",
2058 							   GPIOD_OUT_LOW);
2059 	if (IS_ERR(cs35l56->base.reset_gpio)) {
2060 		ret = PTR_ERR(cs35l56->base.reset_gpio);
2061 		/*
2062 		 * If RESET is shared the first amp to probe will grab the reset
2063 		 * line and reset all the amps
2064 		 */
2065 		if (ret != -EBUSY)
2066 			return dev_err_probe(cs35l56->base.dev, ret, "Failed to get reset GPIO\n");
2067 
2068 		dev_info(cs35l56->base.dev, "Reset GPIO busy, assume shared reset\n");
2069 		cs35l56->base.reset_gpio = NULL;
2070 	}
2071 
2072 	ret = regulator_bulk_enable(ARRAY_SIZE(cs35l56->supplies), cs35l56->supplies);
2073 	if (ret != 0)
2074 		return dev_err_probe(cs35l56->base.dev, ret, "Failed to enable supplies\n");
2075 
2076 	if (cs35l56->base.reset_gpio) {
2077 		/* ACPI can override GPIOD_OUT_LOW flag so force it to start low */
2078 		gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 0);
2079 		cs35l56_wait_min_reset_pulse();
2080 		gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 1);
2081 	}
2082 
2083 	ret = cs35l56_get_speaker_id(&cs35l56->base);
2084 	if (ACPI_COMPANION(cs35l56->base.dev) && cs35l56->sdw_peripheral && (ret == -ENOENT))
2085 		ret = cs35l56_try_get_broken_sdca_spkid_gpio(cs35l56);
2086 
2087 	if ((ret < 0) && (ret != -ENOENT))
2088 		goto err;
2089 
2090 	cs35l56->speaker_id = ret;
2091 
2092 	ret = cs35l56_get_firmware_uid(cs35l56);
2093 	if (ret != 0)
2094 		goto err;
2095 
2096 	ret = cs35l56_process_xu_properties(cs35l56);
2097 	if (ret)
2098 		goto err;
2099 
2100 	ret = cs35l56_dsp_init(cs35l56);
2101 	if (ret < 0) {
2102 		dev_err_probe(cs35l56->base.dev, ret, "DSP init failed\n");
2103 		goto err;
2104 	}
2105 
2106 	/*
2107 	 * On SoundWire the cs35l56_init() cannot be run until after the
2108 	 * device has been enumerated by the SoundWire core.
2109 	 */
2110 	if (!cs35l56->sdw_peripheral) {
2111 		ret = cs35l56_init(cs35l56);
2112 		if (ret)
2113 			goto err_remove_wm_adsp;
2114 	}
2115 
2116 	ret = cs35l56_irq_request(&cs35l56->base, irq);
2117 	if (ret)
2118 		goto err_remove_wm_adsp;
2119 
2120 	/*
2121 	 * Defer calling snd_soc_register_component() on SoundWire to prevent
2122 	 * a deadlock where it calls our component_probe(), which requires the
2123 	 * SoundWire enumeration to complete, but because we are still in probe()
2124 	 * the SoundWire core will not call the update_status() callback. At time
2125 	 * of writing snd_soc_register_component() never returns EPROBE_DEFER.
2126 	 */
2127 	if (!cs35l56->sdw_peripheral) {
2128 		ret = cs35l56_component_register(cs35l56);
2129 		if (ret < 0)
2130 			goto err_free_irq;
2131 	}
2132 
2133 	return 0;
2134 
2135 err_free_irq:
2136 	if (cs35l56->base.irq)
2137 		devm_free_irq(cs35l56->base.dev, cs35l56->base.irq, &cs35l56->base);
2138 
2139 err_remove_wm_adsp:
2140 	wm_adsp2_remove(&cs35l56->dsp);
2141 
2142 err:
2143 	if (pm_runtime_enabled(cs35l56->base.dev)) {
2144 		pm_runtime_dont_use_autosuspend(cs35l56->base.dev);
2145 		pm_runtime_disable(cs35l56->base.dev);
2146 	}
2147 
2148 	gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 0);
2149 	regulator_bulk_disable(ARRAY_SIZE(cs35l56->supplies), cs35l56->supplies);
2150 
2151 	if (cs35l56->dsp_wq)
2152 		destroy_workqueue(cs35l56->dsp_wq);
2153 
2154 	return ret;
2155 }
2156 EXPORT_SYMBOL_NS_GPL(cs35l56_common_probe, "SND_SOC_CS35L56_CORE");
2157 
2158 int cs35l56_init(struct cs35l56_private *cs35l56)
2159 {
2160 	bool first_time_init = !cs35l56->base.init_done;
2161 	int ret;
2162 
2163 	/*
2164 	 * Check whether the actions associated with soft reset or one time
2165 	 * init need to be performed.
2166 	 */
2167 	if (cs35l56->soft_resetting)
2168 		goto post_soft_reset;
2169 
2170 	if (cs35l56->base.init_done)
2171 		return 0;
2172 
2173 	pm_runtime_set_autosuspend_delay(cs35l56->base.dev,
2174 					 CS35L56_FW_REQ_ACTIVE_TIMEOUT_MS + 50);
2175 	pm_runtime_use_autosuspend(cs35l56->base.dev);
2176 	pm_runtime_set_active(cs35l56->base.dev);
2177 	pm_runtime_enable(cs35l56->base.dev);
2178 
2179 	ret = cs35l56_hw_init(&cs35l56->base);
2180 	if (ret < 0)
2181 		return ret;
2182 
2183 	ret = cs35l56_set_patch(&cs35l56->base);
2184 	if (ret)
2185 		return ret;
2186 
2187 	ret = cs35l56_get_calibration(&cs35l56->base);
2188 	if (ret)
2189 		return ret;
2190 
2191 	if (!cs35l56->base.reset_gpio) {
2192 		dev_dbg(cs35l56->base.dev, "No reset gpio: using soft reset\n");
2193 		cs35l56->soft_resetting = true;
2194 		cs35l56_system_reset(&cs35l56->base, !!cs35l56->sdw_peripheral);
2195 		if (cs35l56->sdw_peripheral) {
2196 			/* Keep alive while we wait for re-enumeration */
2197 			pm_runtime_get_noresume(cs35l56->base.dev);
2198 			return 0;
2199 		}
2200 	}
2201 
2202 post_soft_reset:
2203 	if (cs35l56->soft_resetting) {
2204 		cs35l56->soft_resetting = false;
2205 
2206 		/* Done re-enumerating after one-time init so release the keep-alive */
2207 		if (cs35l56->sdw_peripheral && !cs35l56->base.init_done)
2208 			pm_runtime_put_noidle(cs35l56->base.dev);
2209 
2210 		regcache_mark_dirty(cs35l56->base.regmap);
2211 		ret = cs35l56_wait_for_firmware_boot(&cs35l56->base);
2212 		if (ret)
2213 			return ret;
2214 
2215 		dev_dbg(cs35l56->base.dev, "Firmware rebooted after soft reset\n");
2216 
2217 		regcache_cache_only(cs35l56->base.regmap, false);
2218 	}
2219 
2220 	/* Disable auto-hibernate so that runtime_pm has control */
2221 	ret = cs35l56_mbox_send(&cs35l56->base, CS35L56_MBOX_CMD_PREVENT_AUTO_HIBERNATE);
2222 	if (ret)
2223 		return ret;
2224 
2225 	/* Registers could be dirty after soft reset or SoundWire enumeration */
2226 	regcache_sync(cs35l56->base.regmap);
2227 
2228 	/* Set ASP1 DOUT to high-impedance when it is not transmitting audio data. */
2229 	ret = regmap_set_bits(cs35l56->base.regmap, CS35L56_ASP1_CONTROL3,
2230 			      CS35L56_ASP1_DOUT_HIZ_CTRL_MASK);
2231 	if (ret)
2232 		return dev_err_probe(cs35l56->base.dev, ret, "Failed to write ASP1_CONTROL3\n");
2233 
2234 	cs35l56->base.init_done = true;
2235 	complete_all(&cs35l56->init_completion);
2236 
2237 	if (cs35l56->sdw_peripheral && first_time_init) {
2238 		/*
2239 		 * Hardware now accessible, queue work to call
2240 		 * snd_soc_register_component().
2241 		 */
2242 		queue_work(system_freezable_wq, &cs35l56->component_register_work);
2243 	}
2244 
2245 	return 0;
2246 }
2247 EXPORT_SYMBOL_NS_GPL(cs35l56_init, "SND_SOC_CS35L56_CORE");
2248 
2249 void cs35l56_remove(struct cs35l56_private *cs35l56)
2250 {
2251 	cancel_work_sync(&cs35l56->component_register_work);
2252 	if (cs35l56->component_registered)
2253 		snd_soc_unregister_component(cs35l56->base.dev);
2254 
2255 	cs35l56->base.init_done = false;
2256 
2257 	/*
2258 	 * WAKE IRQs unmask if CS35L56 hibernates so free the handler to
2259 	 * prevent it racing with remove().
2260 	 */
2261 	if (cs35l56->base.irq)
2262 		devm_free_irq(cs35l56->base.dev, cs35l56->base.irq, &cs35l56->base);
2263 
2264 	destroy_workqueue(cs35l56->dsp_wq);
2265 
2266 	wm_adsp2_remove(&cs35l56->dsp);
2267 
2268 	pm_runtime_dont_use_autosuspend(cs35l56->base.dev);
2269 	pm_runtime_suspend(cs35l56->base.dev);
2270 	pm_runtime_disable(cs35l56->base.dev);
2271 
2272 	regcache_cache_only(cs35l56->base.regmap, true);
2273 
2274 	gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 0);
2275 	regulator_bulk_disable(ARRAY_SIZE(cs35l56->supplies), cs35l56->supplies);
2276 }
2277 EXPORT_SYMBOL_NS_GPL(cs35l56_remove, "SND_SOC_CS35L56_CORE");
2278 
2279 #if IS_ENABLED(CONFIG_SND_SOC_CS35L56_I2C) || IS_ENABLED(CONFIG_SND_SOC_CS35L56_SPI)
2280 EXPORT_NS_GPL_DEV_PM_OPS(cs35l56_pm_ops_i2c_spi, SND_SOC_CS35L56_CORE) = {
2281 	SET_RUNTIME_PM_OPS(cs35l56_runtime_suspend_i2c_spi, cs35l56_runtime_resume_i2c_spi, NULL)
2282 	SYSTEM_SLEEP_PM_OPS(cs35l56_system_suspend, cs35l56_system_resume)
2283 	LATE_SYSTEM_SLEEP_PM_OPS(cs35l56_system_suspend_late, cs35l56_system_resume_early)
2284 	NOIRQ_SYSTEM_SLEEP_PM_OPS(cs35l56_system_suspend_no_irq, cs35l56_system_resume_no_irq)
2285 };
2286 #endif
2287 
2288 static int cs35l56_reboot_notify(struct notifier_block *nb,
2289 				 unsigned long action, void *data)
2290 {
2291 	guard(mutex)(&cs35l56_component_register_lock);
2292 	cs35l56_shutting_down = true;
2293 
2294 	return NOTIFY_DONE;
2295 }
2296 
2297 static struct notifier_block cs35l56_reboot_notifier = {
2298 	.notifier_call = cs35l56_reboot_notify,
2299 };
2300 
2301 static int __init cs35l56_modinit(void)
2302 {
2303 	/*
2304 	 * Use reboot notifier to prevent race between shutdown and
2305 	 * snd_soc_register_component(). Driver shutdown() callback would
2306 	 * run too late, after device_shutdown() is already walking the
2307 	 * device list that component registration can modify.
2308 	 */
2309 	return register_reboot_notifier(&cs35l56_reboot_notifier);
2310 }
2311 module_init(cs35l56_modinit);
2312 
2313 static void __exit cs35l56_modexit(void)
2314 {
2315 	unregister_reboot_notifier(&cs35l56_reboot_notifier);
2316 }
2317 module_exit(cs35l56_modexit);
2318 
2319 MODULE_DESCRIPTION("ASoC CS35L56 driver");
2320 MODULE_IMPORT_NS("SND_SOC_CS35L56_SHARED");
2321 MODULE_IMPORT_NS("SND_SOC_CS_AMP_LIB");
2322 MODULE_AUTHOR("Richard Fitzgerald <rf@opensource.cirrus.com>");
2323 MODULE_AUTHOR("Simon Trimmer <simont@opensource.cirrus.com>");
2324 MODULE_LICENSE("GPL");
2325