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