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