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