xref: /linux/sound/soc/codecs/cs35l56.c (revision c17ee635fd3a482b2ad2bf5e269755c2eae5f25e)
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 VISIBLE_IF_KUNIT int cs35l56_set_fw_name(struct snd_soc_component *component)
1183 {
1184 	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
1185 	unsigned short vendor, device;
1186 	int ret;
1187 
1188 	if ((cs35l56->speaker_id < 0) && cs35l56->base.num_onchip_spkid_gpios) {
1189 		PM_RUNTIME_ACQUIRE(cs35l56->base.dev, pm);
1190 		ret = PM_RUNTIME_ACQUIRE_ERR(&pm);
1191 		if (ret)
1192 			return ret;
1193 
1194 		ret = cs35l56_configure_onchip_spkid_pads(&cs35l56->base);
1195 		if (ret)
1196 			return ret;
1197 
1198 		ret = cs35l56_read_onchip_spkid(&cs35l56->base);
1199 		if (ret < 0)
1200 			return ret;
1201 
1202 		cs35l56->speaker_id = ret;
1203 	}
1204 
1205 	if (!cs35l56->dsp.system_name &&
1206 	    (snd_soc_card_get_pci_ssid(component->card, &vendor, &device) == 0)) {
1207 		/* Append a speaker qualifier if there is a speaker ID */
1208 		if (cs35l56->speaker_id >= 0) {
1209 			cs35l56->dsp.system_name = devm_kasprintf(cs35l56->base.dev,
1210 								  GFP_KERNEL,
1211 								  "%04x%04x-spkid%d",
1212 								  vendor, device,
1213 								  cs35l56->speaker_id);
1214 		} else {
1215 			cs35l56->dsp.system_name = devm_kasprintf(cs35l56->base.dev,
1216 								  GFP_KERNEL,
1217 								  "%04x%04x",
1218 								  vendor, device);
1219 		}
1220 		if (!cs35l56->dsp.system_name)
1221 			return -ENOMEM;
1222 	}
1223 
1224 	return 0;
1225 }
1226 EXPORT_SYMBOL_IF_KUNIT(cs35l56_set_fw_name);
1227 
1228 static int cs35l56_component_probe(struct snd_soc_component *component)
1229 {
1230 	struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(component);
1231 	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
1232 	struct dentry *debugfs_root = component->debugfs_root;
1233 	int ret;
1234 
1235 	BUILD_BUG_ON(ARRAY_SIZE(cs35l56_tx_input_texts) != ARRAY_SIZE(cs35l56_tx_input_values));
1236 
1237 	if (!wait_for_completion_timeout(&cs35l56->init_completion,
1238 					 msecs_to_jiffies(5000))) {
1239 		dev_err(cs35l56->base.dev, "%s: init_completion timed out\n", __func__);
1240 		return -ENODEV;
1241 	}
1242 
1243 	cs35l56->dsp.part = kasprintf(GFP_KERNEL, "cs35l%02x", cs35l56->base.type);
1244 	if (!cs35l56->dsp.part)
1245 		return -ENOMEM;
1246 
1247 	cs35l56->component = component;
1248 	ret = cs35l56_set_fw_name(component);
1249 	if (ret)
1250 		return ret;
1251 
1252 	ret = cs35l56_set_fw_suffix(cs35l56);
1253 	if (ret)
1254 		return ret;
1255 
1256 	wm_adsp2_component_probe(&cs35l56->dsp, component);
1257 
1258 	debugfs_create_bool("init_done", 0444, debugfs_root, &cs35l56->base.init_done);
1259 	debugfs_create_bool("can_hibernate", 0444, debugfs_root, &cs35l56->base.can_hibernate);
1260 	debugfs_create_bool("fw_patched", 0444, debugfs_root, &cs35l56->base.fw_patched);
1261 
1262 
1263 	switch (cs35l56->base.type) {
1264 	case 0x54:
1265 	case 0x56:
1266 	case 0x57:
1267 		ret = snd_soc_add_component_controls(component, cs35l56_controls,
1268 						     ARRAY_SIZE(cs35l56_controls));
1269 		break;
1270 	case 0x63:
1271 		ret = snd_soc_add_component_controls(component, cs35l63_controls,
1272 						     ARRAY_SIZE(cs35l63_controls));
1273 		break;
1274 	default:
1275 		ret = -ENODEV;
1276 		break;
1277 	}
1278 
1279 	if (!ret && IS_ENABLED(CONFIG_SND_SOC_CS35L56_CAL_SET_CTRL)) {
1280 		ret = snd_soc_add_component_controls(component,
1281 						     cs35l56_cal_data_restore_controls,
1282 						     ARRAY_SIZE(cs35l56_cal_data_restore_controls));
1283 	}
1284 
1285 	if (ret)
1286 		return dev_err_probe(cs35l56->base.dev, ret, "unable to add controls\n");
1287 
1288 	ret = snd_soc_dapm_disable_pin(dapm, "Calibrate");
1289 	if (ret)
1290 		return ret;
1291 
1292 	if (IS_ENABLED(CONFIG_SND_SOC_CS35L56_CAL_DEBUGFS))
1293 		cs35l56_create_cal_debugfs(&cs35l56->base, &cs35l56_cal_debugfs_fops);
1294 
1295 	queue_work(cs35l56->dsp_wq, &cs35l56->dsp_work);
1296 
1297 	return 0;
1298 }
1299 
1300 static void cs35l56_component_remove(struct snd_soc_component *component)
1301 {
1302 	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
1303 
1304 	cancel_work_sync(&cs35l56->dsp_work);
1305 
1306 	cs35l56_remove_cal_debugfs(&cs35l56->base);
1307 
1308 	if (cs35l56->dsp.cs_dsp.booted)
1309 		wm_adsp_power_down(&cs35l56->dsp);
1310 
1311 	wm_adsp2_component_remove(&cs35l56->dsp, component);
1312 
1313 	kfree(cs35l56->dsp.part);
1314 	cs35l56->dsp.part = NULL;
1315 
1316 	kfree(cs35l56->dsp.fwf_name);
1317 	cs35l56->dsp.fwf_name = NULL;
1318 
1319 	cs35l56->component = NULL;
1320 }
1321 
1322 static int cs35l56_set_bias_level(struct snd_soc_component *component,
1323 				  enum snd_soc_bias_level level)
1324 {
1325 	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
1326 	struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(component);
1327 
1328 	switch (level) {
1329 	case SND_SOC_BIAS_STANDBY:
1330 		/*
1331 		 * Wait for patching to complete when transitioning from
1332 		 * BIAS_OFF to BIAS_STANDBY
1333 		 */
1334 		if (snd_soc_dapm_get_bias_level(dapm) == SND_SOC_BIAS_OFF)
1335 			cs35l56_wait_dsp_ready(cs35l56);
1336 
1337 		break;
1338 	default:
1339 		break;
1340 	}
1341 
1342 	return 0;
1343 }
1344 
1345 static const struct snd_soc_component_driver soc_component_dev_cs35l56 = {
1346 	.probe = cs35l56_component_probe,
1347 	.remove = cs35l56_component_remove,
1348 
1349 	.dapm_widgets = cs35l56_dapm_widgets,
1350 	.num_dapm_widgets = ARRAY_SIZE(cs35l56_dapm_widgets),
1351 	.dapm_routes = cs35l56_audio_map,
1352 	.num_dapm_routes = ARRAY_SIZE(cs35l56_audio_map),
1353 
1354 	.set_bias_level = cs35l56_set_bias_level,
1355 
1356 	.suspend_bias_off = 1, /* see cs35l56_system_resume() */
1357 };
1358 
1359 static int __maybe_unused cs35l56_runtime_suspend_i2c_spi(struct device *dev)
1360 {
1361 	struct cs35l56_private *cs35l56 = dev_get_drvdata(dev);
1362 
1363 	return cs35l56_runtime_suspend_common(&cs35l56->base);
1364 }
1365 
1366 static int __maybe_unused cs35l56_runtime_resume_i2c_spi(struct device *dev)
1367 {
1368 	struct cs35l56_private *cs35l56 = dev_get_drvdata(dev);
1369 
1370 	return cs35l56_runtime_resume_common(&cs35l56->base, false);
1371 }
1372 
1373 int cs35l56_system_suspend(struct device *dev)
1374 {
1375 	struct cs35l56_private *cs35l56 = dev_get_drvdata(dev);
1376 
1377 	dev_dbg(dev, "system_suspend\n");
1378 
1379 	if (cs35l56->component)
1380 		flush_work(&cs35l56->dsp_work);
1381 
1382 	/*
1383 	 * The interrupt line is normally shared, but after we start suspending
1384 	 * we can't check if our device is the source of an interrupt, and can't
1385 	 * clear it. Prevent this race by temporarily disabling the parent irq
1386 	 * until we reach _no_irq.
1387 	 */
1388 	if (cs35l56->base.irq)
1389 		disable_irq(cs35l56->base.irq);
1390 
1391 	return pm_runtime_force_suspend(dev);
1392 }
1393 EXPORT_SYMBOL_GPL(cs35l56_system_suspend);
1394 
1395 int cs35l56_system_suspend_late(struct device *dev)
1396 {
1397 	struct cs35l56_private *cs35l56 = dev_get_drvdata(dev);
1398 
1399 	dev_dbg(dev, "system_suspend_late\n");
1400 
1401 	/*
1402 	 * Assert RESET before removing supplies.
1403 	 * RESET is usually shared by all amps so it must not be asserted until
1404 	 * all driver instances have done their suspend() stage.
1405 	 */
1406 	if (cs35l56->base.reset_gpio) {
1407 		gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 0);
1408 		cs35l56_wait_min_reset_pulse();
1409 	}
1410 
1411 	regulator_bulk_disable(ARRAY_SIZE(cs35l56->supplies), cs35l56->supplies);
1412 
1413 	return 0;
1414 }
1415 EXPORT_SYMBOL_GPL(cs35l56_system_suspend_late);
1416 
1417 int cs35l56_system_suspend_no_irq(struct device *dev)
1418 {
1419 	struct cs35l56_private *cs35l56 = dev_get_drvdata(dev);
1420 
1421 	dev_dbg(dev, "system_suspend_no_irq\n");
1422 
1423 	/* Handlers are now disabled so the parent IRQ can safely be re-enabled. */
1424 	if (cs35l56->base.irq)
1425 		enable_irq(cs35l56->base.irq);
1426 
1427 	return 0;
1428 }
1429 EXPORT_SYMBOL_GPL(cs35l56_system_suspend_no_irq);
1430 
1431 int cs35l56_system_resume_no_irq(struct device *dev)
1432 {
1433 	struct cs35l56_private *cs35l56 = dev_get_drvdata(dev);
1434 
1435 	dev_dbg(dev, "system_resume_no_irq\n");
1436 
1437 	/*
1438 	 * WAKE interrupts unmask if the CS35L56 hibernates, which can cause
1439 	 * spurious interrupts, and the interrupt line is normally shared.
1440 	 * We can't check if our device is the source of an interrupt, and can't
1441 	 * clear it, until it has fully resumed. Prevent this race by temporarily
1442 	 * disabling the parent irq until we complete resume().
1443 	 */
1444 	if (cs35l56->base.irq)
1445 		disable_irq(cs35l56->base.irq);
1446 
1447 	return 0;
1448 }
1449 EXPORT_SYMBOL_GPL(cs35l56_system_resume_no_irq);
1450 
1451 int cs35l56_system_resume_early(struct device *dev)
1452 {
1453 	struct cs35l56_private *cs35l56 = dev_get_drvdata(dev);
1454 	int ret;
1455 
1456 	dev_dbg(dev, "system_resume_early\n");
1457 
1458 	/* Ensure a spec-compliant RESET pulse. */
1459 	if (cs35l56->base.reset_gpio) {
1460 		gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 0);
1461 		cs35l56_wait_min_reset_pulse();
1462 	}
1463 
1464 	/* Enable supplies before releasing RESET. */
1465 	ret = regulator_bulk_enable(ARRAY_SIZE(cs35l56->supplies), cs35l56->supplies);
1466 	if (ret) {
1467 		dev_err(dev, "system_resume_early failed to enable supplies: %d\n", ret);
1468 		return ret;
1469 	}
1470 
1471 	/* Release shared RESET before drivers start resume(). */
1472 	gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 1);
1473 
1474 	return 0;
1475 }
1476 EXPORT_SYMBOL_GPL(cs35l56_system_resume_early);
1477 
1478 int cs35l56_system_resume(struct device *dev)
1479 {
1480 	struct cs35l56_private *cs35l56 = dev_get_drvdata(dev);
1481 	int ret;
1482 
1483 	dev_dbg(dev, "system_resume\n");
1484 
1485 	/*
1486 	 * We might have done a hard reset or the CS35L56 was power-cycled
1487 	 * so wait for control port to be ready.
1488 	 */
1489 	cs35l56_wait_control_port_ready();
1490 
1491 	/* Undo pm_runtime_force_suspend() before re-enabling the irq */
1492 	ret = pm_runtime_force_resume(dev);
1493 	if (cs35l56->base.irq)
1494 		enable_irq(cs35l56->base.irq);
1495 
1496 	if (ret)
1497 		return ret;
1498 
1499 	/* Firmware won't have been loaded if the component hasn't probed */
1500 	if (!cs35l56->component)
1501 		return 0;
1502 
1503 	ret = cs35l56_is_fw_reload_needed(&cs35l56->base);
1504 	dev_dbg(cs35l56->base.dev, "fw_reload_needed: %d\n", ret);
1505 	if (ret < 1)
1506 		return ret;
1507 
1508 	cs35l56->base.fw_patched = false;
1509 	wm_adsp_power_down(&cs35l56->dsp);
1510 	queue_work(cs35l56->dsp_wq, &cs35l56->dsp_work);
1511 
1512 	/*
1513 	 * suspend_bias_off ensures we are now in BIAS_OFF so there will be
1514 	 * a BIAS_OFF->BIAS_STANDBY transition to complete dsp patching.
1515 	 */
1516 
1517 	return 0;
1518 }
1519 EXPORT_SYMBOL_GPL(cs35l56_system_resume);
1520 
1521 static int cs35l56_control_add_nop(struct wm_adsp *dsp, struct cs_dsp_coeff_ctl *cs_ctl)
1522 {
1523 	return 0;
1524 }
1525 
1526 static int cs35l56_dsp_init(struct cs35l56_private *cs35l56)
1527 {
1528 	struct wm_adsp *dsp;
1529 	int ret;
1530 
1531 	cs35l56->dsp_wq = create_singlethread_workqueue("cs35l56-dsp");
1532 	if (!cs35l56->dsp_wq)
1533 		return -ENOMEM;
1534 
1535 	INIT_WORK(&cs35l56->dsp_work, cs35l56_dsp_work);
1536 
1537 	dsp = &cs35l56->dsp;
1538 	cs35l56_init_cs_dsp(&cs35l56->base, &dsp->cs_dsp);
1539 
1540 	/*
1541 	 * dsp->part is filled in later as it is based on the DEVID. In a
1542 	 * SoundWire system that cannot be read until enumeration has occurred
1543 	 * and the device has attached.
1544 	 */
1545 	dsp->fw = 12;
1546 	dsp->wmfw_optional = true;
1547 
1548 	/*
1549 	 * None of the firmware controls need to be exported so add a no-op
1550 	 * callback that suppresses creating an ALSA control.
1551 	 */
1552 	dsp->control_add = &cs35l56_control_add_nop;
1553 
1554 	dev_dbg(cs35l56->base.dev, "DSP system name: '%s'\n", dsp->system_name);
1555 
1556 	ret = wm_halo_init(dsp);
1557 	if (ret != 0) {
1558 		dev_err(cs35l56->base.dev, "wm_halo_init failed\n");
1559 		return ret;
1560 	}
1561 
1562 	return 0;
1563 }
1564 
1565 static int cs35l56_read_fwnode_u32_array(struct device *dev,
1566 					struct fwnode_handle *parent_node,
1567 					const char *prop_name,
1568 					int max_count,
1569 					u32 *dest)
1570 {
1571 	int count, ret;
1572 
1573 	count = fwnode_property_count_u32(parent_node, prop_name);
1574 	if ((count == 0) || (count == -EINVAL) || (count == -ENODATA)) {
1575 		dev_dbg(dev, "%s not found in %s\n", prop_name, fwnode_get_name(parent_node));
1576 		return 0;
1577 	}
1578 
1579 	if (count < 0) {
1580 		dev_err(dev, "Get %s error:%d\n", prop_name, count);
1581 		return count;
1582 	}
1583 
1584 	if (count > max_count) {
1585 		dev_err(dev, "%s too many entries (%d)\n", prop_name, count);
1586 		return -EOVERFLOW;
1587 	}
1588 
1589 	ret = fwnode_property_read_u32_array(parent_node, prop_name, dest, count);
1590 	if (ret) {
1591 		dev_err(dev, "Error reading %s: %d\n", prop_name, ret);
1592 		return ret;
1593 	}
1594 
1595 	return count;
1596 }
1597 
1598 static int cs35l56_process_xu_onchip_speaker_id(struct cs35l56_private *cs35l56,
1599 						struct fwnode_handle *ext_node)
1600 {
1601 	static const char * const gpio_name = "01fa-spk-id-gpios-onchip";
1602 	static const char * const pull_name = "01fa-spk-id-gpios-onchip-pull";
1603 	u32 gpios[5], pulls[5];
1604 	int num_gpios, num_pulls;
1605 	int ret;
1606 
1607 	static_assert(ARRAY_SIZE(gpios) == ARRAY_SIZE(cs35l56->base.onchip_spkid_gpios));
1608 	static_assert(ARRAY_SIZE(pulls) == ARRAY_SIZE(cs35l56->base.onchip_spkid_pulls));
1609 
1610 	num_gpios = cs35l56_read_fwnode_u32_array(cs35l56->base.dev, ext_node, gpio_name,
1611 						  ARRAY_SIZE(gpios), gpios);
1612 	if (num_gpios < 1)
1613 		return num_gpios;
1614 
1615 	num_pulls = cs35l56_read_fwnode_u32_array(cs35l56->base.dev, ext_node, pull_name,
1616 						  ARRAY_SIZE(pulls), pulls);
1617 	if (num_pulls < 0)
1618 		return num_pulls;
1619 
1620 	if (num_pulls != num_gpios) {
1621 		dev_warn(cs35l56->base.dev, "%s count(%d) != %s count(%d)\n",
1622 			pull_name, num_pulls, gpio_name, num_gpios);
1623 	}
1624 
1625 	ret = cs35l56_check_and_save_onchip_spkid_gpios(&cs35l56->base,
1626 							gpios, num_gpios,
1627 							pulls, num_pulls);
1628 	if (ret) {
1629 		return dev_err_probe(cs35l56->base.dev, ret, "Error in %s/%s\n",
1630 				     gpio_name, pull_name);
1631 	}
1632 
1633 	return 0;
1634 }
1635 
1636 VISIBLE_IF_KUNIT int cs35l56_process_xu_properties(struct cs35l56_private *cs35l56)
1637 {
1638 	struct fwnode_handle *ext_node = NULL;
1639 	struct fwnode_handle *link;
1640 	int ret;
1641 
1642 	if (!cs35l56->sdw_peripheral)
1643 		return 0;
1644 
1645 	fwnode_for_each_child_node(dev_fwnode(cs35l56->base.dev), link) {
1646 		ext_node = fwnode_get_named_child_node(link,
1647 						       "mipi-sdca-function-expansion-subproperties");
1648 		if (ext_node) {
1649 			fwnode_handle_put(link);
1650 			break;
1651 		}
1652 	}
1653 
1654 	if (!ext_node)
1655 		return 0;
1656 
1657 	ret = cs35l56_process_xu_onchip_speaker_id(cs35l56, ext_node);
1658 	fwnode_handle_put(ext_node);
1659 
1660 	return ret;
1661 }
1662 EXPORT_SYMBOL_IF_KUNIT(cs35l56_process_xu_properties);
1663 
1664 static int cs35l56_get_firmware_uid(struct cs35l56_private *cs35l56)
1665 {
1666 	struct device *dev = cs35l56->base.dev;
1667 	const char *prop;
1668 	int ret;
1669 
1670 	ret = device_property_read_string(dev, "cirrus,firmware-uid", &prop);
1671 	/* If bad sw node property, return 0 and fallback to legacy firmware path */
1672 	if (ret < 0)
1673 		return 0;
1674 
1675 	/* Append a speaker qualifier if there is a speaker ID */
1676 	if (cs35l56->speaker_id >= 0)
1677 		cs35l56->dsp.system_name = devm_kasprintf(dev, GFP_KERNEL, "%s-spkid%d",
1678 							  prop, cs35l56->speaker_id);
1679 	else
1680 		cs35l56->dsp.system_name = devm_kstrdup(dev, prop, GFP_KERNEL);
1681 
1682 	if (cs35l56->dsp.system_name == NULL)
1683 		return -ENOMEM;
1684 
1685 	dev_dbg(dev, "Firmware UID: %s\n", cs35l56->dsp.system_name);
1686 
1687 	return 0;
1688 }
1689 
1690 /*
1691  * Some SoundWire laptops have a spk-id-gpios property but it points to
1692  * the wrong ACPI Device node so can't be used to get the GPIO. Try to
1693  * find the SDCA node containing the GpioIo resource and add a GPIO
1694  * mapping to it.
1695  */
1696 static const struct acpi_gpio_params cs35l56_af01_first_gpio = { 0, 0, false };
1697 static const struct acpi_gpio_mapping cs35l56_af01_spkid_gpios_mapping[] = {
1698 	{ "spk-id-gpios", &cs35l56_af01_first_gpio, 1 },
1699 	{ }
1700 };
1701 
1702 static void cs35l56_acpi_dev_release_driver_gpios(void *adev)
1703 {
1704 	acpi_dev_remove_driver_gpios(adev);
1705 }
1706 
1707 static int cs35l56_try_get_broken_sdca_spkid_gpio(struct cs35l56_private *cs35l56)
1708 {
1709 	struct fwnode_handle *af01_fwnode;
1710 	const union acpi_object *obj;
1711 	struct gpio_desc *desc;
1712 	int ret;
1713 
1714 	/* Find the SDCA node containing the GpioIo */
1715 	af01_fwnode = device_get_named_child_node(cs35l56->base.dev, "AF01");
1716 	if (!af01_fwnode) {
1717 		dev_dbg(cs35l56->base.dev, "No AF01 node\n");
1718 		return -ENOENT;
1719 	}
1720 
1721 	ret = acpi_dev_get_property(ACPI_COMPANION(cs35l56->base.dev),
1722 				    "spk-id-gpios", ACPI_TYPE_PACKAGE, &obj);
1723 	if (ret) {
1724 		dev_dbg(cs35l56->base.dev, "Could not get spk-id-gpios package: %d\n", ret);
1725 		fwnode_handle_put(af01_fwnode);
1726 		return -ENOENT;
1727 	}
1728 
1729 	/* The broken properties we can handle are a 4-element package (one GPIO) */
1730 	if (obj->package.count != 4) {
1731 		dev_warn(cs35l56->base.dev, "Unexpected spk-id element count %d\n",
1732 			 obj->package.count);
1733 		fwnode_handle_put(af01_fwnode);
1734 		return -ENOENT;
1735 	}
1736 
1737 	/* Add a GPIO mapping if it doesn't already have one */
1738 	if (!fwnode_property_present(af01_fwnode, "spk-id-gpios")) {
1739 		struct acpi_device *adev = to_acpi_device_node(af01_fwnode);
1740 
1741 		/*
1742 		 * Can't use devm_acpi_dev_add_driver_gpios() because the
1743 		 * mapping isn't being added to the node pointed to by
1744 		 * ACPI_COMPANION().
1745 		 */
1746 		ret = acpi_dev_add_driver_gpios(adev, cs35l56_af01_spkid_gpios_mapping);
1747 		if (ret) {
1748 			fwnode_handle_put(af01_fwnode);
1749 			return dev_err_probe(cs35l56->base.dev, ret,
1750 					     "Failed to add gpio mapping to AF01\n");
1751 		}
1752 
1753 		ret = devm_add_action_or_reset(cs35l56->base.dev,
1754 					       cs35l56_acpi_dev_release_driver_gpios,
1755 					       adev);
1756 		if (ret) {
1757 			fwnode_handle_put(af01_fwnode);
1758 			return ret;
1759 		}
1760 
1761 		dev_dbg(cs35l56->base.dev, "Added spk-id-gpios mapping to AF01\n");
1762 	}
1763 
1764 	desc = fwnode_gpiod_get_index(af01_fwnode, "spk-id", 0, GPIOD_IN, NULL);
1765 	if (IS_ERR(desc)) {
1766 		fwnode_handle_put(af01_fwnode);
1767 		ret = PTR_ERR(desc);
1768 		return dev_err_probe(cs35l56->base.dev, ret, "Get GPIO from AF01 failed\n");
1769 	}
1770 
1771 	ret = gpiod_get_value_cansleep(desc);
1772 	gpiod_put(desc);
1773 
1774 	if (ret < 0) {
1775 		fwnode_handle_put(af01_fwnode);
1776 		dev_err_probe(cs35l56->base.dev, ret, "Error reading spk-id GPIO\n");
1777 		return ret;
1778 	}
1779 
1780 	fwnode_handle_put(af01_fwnode);
1781 
1782 	dev_info(cs35l56->base.dev, "Got spk-id from AF01\n");
1783 
1784 	return ret;
1785 }
1786 
1787 int cs35l56_common_probe(struct cs35l56_private *cs35l56)
1788 {
1789 	int ret;
1790 
1791 	init_completion(&cs35l56->init_completion);
1792 	mutex_init(&cs35l56->base.irq_lock);
1793 	cs35l56->base.cal_index = -1;
1794 	cs35l56->speaker_id = -ENOENT;
1795 
1796 	dev_set_drvdata(cs35l56->base.dev, cs35l56);
1797 
1798 	cs35l56_fill_supply_names(cs35l56->supplies);
1799 	ret = devm_regulator_bulk_get(cs35l56->base.dev, ARRAY_SIZE(cs35l56->supplies),
1800 				      cs35l56->supplies);
1801 	if (ret != 0)
1802 		return dev_err_probe(cs35l56->base.dev, ret, "Failed to request supplies\n");
1803 
1804 	/* Reset could be controlled by the BIOS or shared by multiple amps */
1805 	cs35l56->base.reset_gpio = devm_gpiod_get_optional(cs35l56->base.dev, "reset",
1806 							   GPIOD_OUT_LOW);
1807 	if (IS_ERR(cs35l56->base.reset_gpio)) {
1808 		ret = PTR_ERR(cs35l56->base.reset_gpio);
1809 		/*
1810 		 * If RESET is shared the first amp to probe will grab the reset
1811 		 * line and reset all the amps
1812 		 */
1813 		if (ret != -EBUSY)
1814 			return dev_err_probe(cs35l56->base.dev, ret, "Failed to get reset GPIO\n");
1815 
1816 		dev_info(cs35l56->base.dev, "Reset GPIO busy, assume shared reset\n");
1817 		cs35l56->base.reset_gpio = NULL;
1818 	}
1819 
1820 	ret = regulator_bulk_enable(ARRAY_SIZE(cs35l56->supplies), cs35l56->supplies);
1821 	if (ret != 0)
1822 		return dev_err_probe(cs35l56->base.dev, ret, "Failed to enable supplies\n");
1823 
1824 	if (cs35l56->base.reset_gpio) {
1825 		/* ACPI can override GPIOD_OUT_LOW flag so force it to start low */
1826 		gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 0);
1827 		cs35l56_wait_min_reset_pulse();
1828 		gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 1);
1829 	}
1830 
1831 	ret = cs35l56_get_speaker_id(&cs35l56->base);
1832 	if (ACPI_COMPANION(cs35l56->base.dev) && cs35l56->sdw_peripheral && (ret == -ENOENT))
1833 		ret = cs35l56_try_get_broken_sdca_spkid_gpio(cs35l56);
1834 
1835 	if ((ret < 0) && (ret != -ENOENT))
1836 		goto err;
1837 
1838 	cs35l56->speaker_id = ret;
1839 
1840 	ret = cs35l56_get_firmware_uid(cs35l56);
1841 	if (ret != 0)
1842 		goto err;
1843 
1844 	ret = cs35l56_process_xu_properties(cs35l56);
1845 	if (ret)
1846 		goto err;
1847 
1848 	ret = cs35l56_dsp_init(cs35l56);
1849 	if (ret < 0) {
1850 		dev_err_probe(cs35l56->base.dev, ret, "DSP init failed\n");
1851 		goto err;
1852 	}
1853 
1854 	ret = devm_snd_soc_register_component(cs35l56->base.dev,
1855 					      &soc_component_dev_cs35l56,
1856 					      cs35l56_dai, ARRAY_SIZE(cs35l56_dai));
1857 	if (ret < 0) {
1858 		dev_err_probe(cs35l56->base.dev, ret, "Register codec failed\n");
1859 		goto err;
1860 	}
1861 
1862 	return 0;
1863 
1864 err:
1865 	gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 0);
1866 	regulator_bulk_disable(ARRAY_SIZE(cs35l56->supplies), cs35l56->supplies);
1867 
1868 	return ret;
1869 }
1870 EXPORT_SYMBOL_NS_GPL(cs35l56_common_probe, "SND_SOC_CS35L56_CORE");
1871 
1872 int cs35l56_init(struct cs35l56_private *cs35l56)
1873 {
1874 	int ret;
1875 
1876 	/*
1877 	 * Check whether the actions associated with soft reset or one time
1878 	 * init need to be performed.
1879 	 */
1880 	if (cs35l56->soft_resetting)
1881 		goto post_soft_reset;
1882 
1883 	if (cs35l56->base.init_done)
1884 		return 0;
1885 
1886 	pm_runtime_set_autosuspend_delay(cs35l56->base.dev, 100);
1887 	pm_runtime_use_autosuspend(cs35l56->base.dev);
1888 	pm_runtime_set_active(cs35l56->base.dev);
1889 	pm_runtime_enable(cs35l56->base.dev);
1890 
1891 	ret = cs35l56_hw_init(&cs35l56->base);
1892 	if (ret < 0)
1893 		return ret;
1894 
1895 	ret = cs35l56_set_patch(&cs35l56->base);
1896 	if (ret)
1897 		return ret;
1898 
1899 	ret = cs35l56_get_calibration(&cs35l56->base);
1900 	if (ret)
1901 		return ret;
1902 
1903 	if (!cs35l56->base.reset_gpio) {
1904 		dev_dbg(cs35l56->base.dev, "No reset gpio: using soft reset\n");
1905 		cs35l56->soft_resetting = true;
1906 		cs35l56_system_reset(&cs35l56->base, !!cs35l56->sdw_peripheral);
1907 		if (cs35l56->sdw_peripheral) {
1908 			/* Keep alive while we wait for re-enumeration */
1909 			pm_runtime_get_noresume(cs35l56->base.dev);
1910 			return 0;
1911 		}
1912 	}
1913 
1914 post_soft_reset:
1915 	if (cs35l56->soft_resetting) {
1916 		cs35l56->soft_resetting = false;
1917 
1918 		/* Done re-enumerating after one-time init so release the keep-alive */
1919 		if (cs35l56->sdw_peripheral && !cs35l56->base.init_done)
1920 			pm_runtime_put_noidle(cs35l56->base.dev);
1921 
1922 		regcache_mark_dirty(cs35l56->base.regmap);
1923 		ret = cs35l56_wait_for_firmware_boot(&cs35l56->base);
1924 		if (ret)
1925 			return ret;
1926 
1927 		dev_dbg(cs35l56->base.dev, "Firmware rebooted after soft reset\n");
1928 
1929 		regcache_cache_only(cs35l56->base.regmap, false);
1930 	}
1931 
1932 	/* Disable auto-hibernate so that runtime_pm has control */
1933 	ret = cs35l56_mbox_send(&cs35l56->base, CS35L56_MBOX_CMD_PREVENT_AUTO_HIBERNATE);
1934 	if (ret)
1935 		return ret;
1936 
1937 	/* Registers could be dirty after soft reset or SoundWire enumeration */
1938 	regcache_sync(cs35l56->base.regmap);
1939 
1940 	/* Set ASP1 DOUT to high-impedance when it is not transmitting audio data. */
1941 	ret = regmap_set_bits(cs35l56->base.regmap, CS35L56_ASP1_CONTROL3,
1942 			      CS35L56_ASP1_DOUT_HIZ_CTRL_MASK);
1943 	if (ret)
1944 		return dev_err_probe(cs35l56->base.dev, ret, "Failed to write ASP1_CONTROL3\n");
1945 
1946 	cs35l56->base.init_done = true;
1947 	complete(&cs35l56->init_completion);
1948 
1949 	return 0;
1950 }
1951 EXPORT_SYMBOL_NS_GPL(cs35l56_init, "SND_SOC_CS35L56_CORE");
1952 
1953 void cs35l56_remove(struct cs35l56_private *cs35l56)
1954 {
1955 	cs35l56->base.init_done = false;
1956 
1957 	/*
1958 	 * WAKE IRQs unmask if CS35L56 hibernates so free the handler to
1959 	 * prevent it racing with remove().
1960 	 */
1961 	if (cs35l56->base.irq)
1962 		devm_free_irq(cs35l56->base.dev, cs35l56->base.irq, &cs35l56->base);
1963 
1964 	destroy_workqueue(cs35l56->dsp_wq);
1965 
1966 	pm_runtime_dont_use_autosuspend(cs35l56->base.dev);
1967 	pm_runtime_suspend(cs35l56->base.dev);
1968 	pm_runtime_disable(cs35l56->base.dev);
1969 
1970 	regcache_cache_only(cs35l56->base.regmap, true);
1971 
1972 	gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 0);
1973 	regulator_bulk_disable(ARRAY_SIZE(cs35l56->supplies), cs35l56->supplies);
1974 }
1975 EXPORT_SYMBOL_NS_GPL(cs35l56_remove, "SND_SOC_CS35L56_CORE");
1976 
1977 #if IS_ENABLED(CONFIG_SND_SOC_CS35L56_I2C) || IS_ENABLED(CONFIG_SND_SOC_CS35L56_SPI)
1978 EXPORT_NS_GPL_DEV_PM_OPS(cs35l56_pm_ops_i2c_spi, SND_SOC_CS35L56_CORE) = {
1979 	SET_RUNTIME_PM_OPS(cs35l56_runtime_suspend_i2c_spi, cs35l56_runtime_resume_i2c_spi, NULL)
1980 	SYSTEM_SLEEP_PM_OPS(cs35l56_system_suspend, cs35l56_system_resume)
1981 	LATE_SYSTEM_SLEEP_PM_OPS(cs35l56_system_suspend_late, cs35l56_system_resume_early)
1982 	NOIRQ_SYSTEM_SLEEP_PM_OPS(cs35l56_system_suspend_no_irq, cs35l56_system_resume_no_irq)
1983 };
1984 #endif
1985 
1986 MODULE_DESCRIPTION("ASoC CS35L56 driver");
1987 MODULE_IMPORT_NS("SND_SOC_CS35L56_SHARED");
1988 MODULE_IMPORT_NS("SND_SOC_CS_AMP_LIB");
1989 MODULE_AUTHOR("Richard Fitzgerald <rf@opensource.cirrus.com>");
1990 MODULE_AUTHOR("Simon Trimmer <simont@opensource.cirrus.com>");
1991 MODULE_LICENSE("GPL");
1992