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