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