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 <linux/acpi.h>
9#include <linux/completion.h>
10#include <linux/debugfs.h>
11#include <linux/delay.h>
12#include <linux/err.h>
13#include <linux/gpio/consumer.h>
14#include <linux/interrupt.h>
15#include <linux/math.h>
16#include <linux/module.h>
17#include <linux/pm.h>
18#include <linux/pm_runtime.h>
19#include <linux/property.h>
20#include <linux/regmap.h>
21#include <linux/regulator/consumer.h>
22#include <linux/slab.h>
23#include <linux/soundwire/sdw.h>
24#include <linux/types.h>
25#include <linux/workqueue.h>
26#include <sound/cs-amp-lib.h>
27#include <sound/pcm.h>
28#include <sound/pcm_params.h>
29#include <sound/soc.h>
30#include <sound/soc-dapm.h>
31#include <sound/tlv.h>
32
33#include "wm_adsp.h"
34#include "cs35l56.h"
35
36static int cs35l56_dsp_event(struct snd_soc_dapm_widget *w,
37			     struct snd_kcontrol *kcontrol, int event);
38
39static void cs35l56_wait_dsp_ready(struct cs35l56_private *cs35l56)
40{
41	/* Wait for patching to complete */
42	flush_work(&cs35l56->dsp_work);
43}
44
45static int cs35l56_dspwait_get_volsw(struct snd_kcontrol *kcontrol,
46				     struct snd_ctl_elem_value *ucontrol)
47{
48	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
49	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
50
51	cs35l56_wait_dsp_ready(cs35l56);
52	return snd_soc_get_volsw(kcontrol, ucontrol);
53}
54
55static int cs35l56_dspwait_put_volsw(struct snd_kcontrol *kcontrol,
56				     struct snd_ctl_elem_value *ucontrol)
57{
58	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
59	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
60
61	cs35l56_wait_dsp_ready(cs35l56);
62	return snd_soc_put_volsw(kcontrol, ucontrol);
63}
64
65static const unsigned short cs35l56_asp1_mixer_regs[] = {
66	CS35L56_ASP1TX1_INPUT, CS35L56_ASP1TX2_INPUT,
67	CS35L56_ASP1TX3_INPUT, CS35L56_ASP1TX4_INPUT,
68};
69
70static const char * const cs35l56_asp1_mux_control_names[] = {
71	"ASP1 TX1 Source", "ASP1 TX2 Source", "ASP1 TX3 Source", "ASP1 TX4 Source"
72};
73
74static int cs35l56_sync_asp1_mixer_widgets_with_firmware(struct cs35l56_private *cs35l56)
75{
76	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cs35l56->component);
77	const char *prefix = cs35l56->component->name_prefix;
78	char full_name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
79	const char *name;
80	struct snd_kcontrol *kcontrol;
81	struct soc_enum *e;
82	unsigned int val[4];
83	int i, item, ret;
84
85	if (cs35l56->asp1_mixer_widgets_initialized)
86		return 0;
87
88	/*
89	 * Resume so we can read the registers from silicon if the regmap
90	 * cache has not yet been populated.
91	 */
92	ret = pm_runtime_resume_and_get(cs35l56->base.dev);
93	if (ret < 0)
94		return ret;
95
96	/* Wait for firmware download and reboot */
97	cs35l56_wait_dsp_ready(cs35l56);
98
99	ret = regmap_bulk_read(cs35l56->base.regmap, CS35L56_ASP1TX1_INPUT,
100			       val, ARRAY_SIZE(val));
101
102	pm_runtime_mark_last_busy(cs35l56->base.dev);
103	pm_runtime_put_autosuspend(cs35l56->base.dev);
104
105	if (ret) {
106		dev_err(cs35l56->base.dev, "Failed to read ASP1 mixer regs: %d\n", ret);
107		return ret;
108	}
109
110	for (i = 0; i < ARRAY_SIZE(cs35l56_asp1_mux_control_names); ++i) {
111		name = cs35l56_asp1_mux_control_names[i];
112
113		if (prefix) {
114			snprintf(full_name, sizeof(full_name), "%s %s", prefix, name);
115			name = full_name;
116		}
117
118		kcontrol = snd_soc_card_get_kcontrol_locked(dapm->card, name);
119		if (!kcontrol) {
120			dev_warn(cs35l56->base.dev, "Could not find control %s\n", name);
121			continue;
122		}
123
124		e = (struct soc_enum *)kcontrol->private_value;
125		item = snd_soc_enum_val_to_item(e, val[i] & CS35L56_ASP_TXn_SRC_MASK);
126		snd_soc_dapm_mux_update_power(dapm, kcontrol, item, e, NULL);
127	}
128
129	cs35l56->asp1_mixer_widgets_initialized = true;
130
131	return 0;
132}
133
134static int cs35l56_dspwait_asp1tx_get(struct snd_kcontrol *kcontrol,
135				      struct snd_ctl_elem_value *ucontrol)
136{
137	struct snd_soc_component *component = snd_soc_dapm_kcontrol_component(kcontrol);
138	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
139	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
140	int index = e->shift_l;
141	unsigned int addr, val;
142	int ret;
143
144	ret = cs35l56_sync_asp1_mixer_widgets_with_firmware(cs35l56);
145	if (ret)
146		return ret;
147
148	addr = cs35l56_asp1_mixer_regs[index];
149	ret = regmap_read(cs35l56->base.regmap, addr, &val);
150	if (ret)
151		return ret;
152
153	val &= CS35L56_ASP_TXn_SRC_MASK;
154	ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
155
156	return 0;
157}
158
159static int cs35l56_dspwait_asp1tx_put(struct snd_kcontrol *kcontrol,
160				      struct snd_ctl_elem_value *ucontrol)
161{
162	struct snd_soc_component *component = snd_soc_dapm_kcontrol_component(kcontrol);
163	struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
164	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
165	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
166	int item = ucontrol->value.enumerated.item[0];
167	int index = e->shift_l;
168	unsigned int addr, val;
169	bool changed;
170	int ret;
171
172	ret = cs35l56_sync_asp1_mixer_widgets_with_firmware(cs35l56);
173	if (ret)
174		return ret;
175
176	addr = cs35l56_asp1_mixer_regs[index];
177	val = snd_soc_enum_item_to_val(e, item);
178
179	ret = regmap_update_bits_check(cs35l56->base.regmap, addr,
180				       CS35L56_ASP_TXn_SRC_MASK, val, &changed);
181	if (ret)
182		return ret;
183
184	if (changed)
185		snd_soc_dapm_mux_update_power(dapm, kcontrol, item, e, NULL);
186
187	return changed;
188}
189
190static DECLARE_TLV_DB_SCALE(vol_tlv, -10000, 25, 0);
191
192static const struct snd_kcontrol_new cs35l56_controls[] = {
193	SOC_SINGLE_EXT("Speaker Switch",
194		       CS35L56_MAIN_RENDER_USER_MUTE, 0, 1, 1,
195		       cs35l56_dspwait_get_volsw, cs35l56_dspwait_put_volsw),
196	SOC_SINGLE_S_EXT_TLV("Speaker Volume",
197			     CS35L56_MAIN_RENDER_USER_VOLUME,
198			     6, -400, 400, 9, 0,
199			     cs35l56_dspwait_get_volsw,
200			     cs35l56_dspwait_put_volsw,
201			     vol_tlv),
202	SOC_SINGLE_EXT("Posture Number", CS35L56_MAIN_POSTURE_NUMBER,
203		       0, 255, 0,
204		       cs35l56_dspwait_get_volsw, cs35l56_dspwait_put_volsw),
205};
206
207static SOC_VALUE_ENUM_SINGLE_DECL(cs35l56_asp1tx1_enum,
208				  SND_SOC_NOPM,
209				  0, 0,
210				  cs35l56_tx_input_texts,
211				  cs35l56_tx_input_values);
212
213static const struct snd_kcontrol_new asp1_tx1_mux =
214	SOC_DAPM_ENUM_EXT("ASP1TX1 SRC", cs35l56_asp1tx1_enum,
215			  cs35l56_dspwait_asp1tx_get, cs35l56_dspwait_asp1tx_put);
216
217static SOC_VALUE_ENUM_SINGLE_DECL(cs35l56_asp1tx2_enum,
218				  SND_SOC_NOPM,
219				  1, 0,
220				  cs35l56_tx_input_texts,
221				  cs35l56_tx_input_values);
222
223static const struct snd_kcontrol_new asp1_tx2_mux =
224	SOC_DAPM_ENUM_EXT("ASP1TX2 SRC", cs35l56_asp1tx2_enum,
225			  cs35l56_dspwait_asp1tx_get, cs35l56_dspwait_asp1tx_put);
226
227static SOC_VALUE_ENUM_SINGLE_DECL(cs35l56_asp1tx3_enum,
228				  SND_SOC_NOPM,
229				  2, 0,
230				  cs35l56_tx_input_texts,
231				  cs35l56_tx_input_values);
232
233static const struct snd_kcontrol_new asp1_tx3_mux =
234	SOC_DAPM_ENUM_EXT("ASP1TX3 SRC", cs35l56_asp1tx3_enum,
235			  cs35l56_dspwait_asp1tx_get, cs35l56_dspwait_asp1tx_put);
236
237static SOC_VALUE_ENUM_SINGLE_DECL(cs35l56_asp1tx4_enum,
238				  SND_SOC_NOPM,
239				  3, 0,
240				  cs35l56_tx_input_texts,
241				  cs35l56_tx_input_values);
242
243static const struct snd_kcontrol_new asp1_tx4_mux =
244	SOC_DAPM_ENUM_EXT("ASP1TX4 SRC", cs35l56_asp1tx4_enum,
245			  cs35l56_dspwait_asp1tx_get, cs35l56_dspwait_asp1tx_put);
246
247static SOC_VALUE_ENUM_SINGLE_DECL(cs35l56_sdw1tx1_enum,
248				CS35L56_SWIRE_DP3_CH1_INPUT,
249				0, CS35L56_SWIRETXn_SRC_MASK,
250				cs35l56_tx_input_texts,
251				cs35l56_tx_input_values);
252
253static const struct snd_kcontrol_new sdw1_tx1_mux =
254	SOC_DAPM_ENUM("SDW1TX1 SRC", cs35l56_sdw1tx1_enum);
255
256static SOC_VALUE_ENUM_SINGLE_DECL(cs35l56_sdw1tx2_enum,
257				CS35L56_SWIRE_DP3_CH2_INPUT,
258				0, CS35L56_SWIRETXn_SRC_MASK,
259				cs35l56_tx_input_texts,
260				cs35l56_tx_input_values);
261
262static const struct snd_kcontrol_new sdw1_tx2_mux =
263	SOC_DAPM_ENUM("SDW1TX2 SRC", cs35l56_sdw1tx2_enum);
264
265static SOC_VALUE_ENUM_SINGLE_DECL(cs35l56_sdw1tx3_enum,
266				CS35L56_SWIRE_DP3_CH3_INPUT,
267				0, CS35L56_SWIRETXn_SRC_MASK,
268				cs35l56_tx_input_texts,
269				cs35l56_tx_input_values);
270
271static const struct snd_kcontrol_new sdw1_tx3_mux =
272	SOC_DAPM_ENUM("SDW1TX3 SRC", cs35l56_sdw1tx3_enum);
273
274static SOC_VALUE_ENUM_SINGLE_DECL(cs35l56_sdw1tx4_enum,
275				CS35L56_SWIRE_DP3_CH4_INPUT,
276				0, CS35L56_SWIRETXn_SRC_MASK,
277				cs35l56_tx_input_texts,
278				cs35l56_tx_input_values);
279
280static const struct snd_kcontrol_new sdw1_tx4_mux =
281	SOC_DAPM_ENUM("SDW1TX4 SRC", cs35l56_sdw1tx4_enum);
282
283static int cs35l56_asp1_cfg_event(struct snd_soc_dapm_widget *w,
284				  struct snd_kcontrol *kcontrol, int event)
285{
286	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
287	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
288
289	switch (event) {
290	case SND_SOC_DAPM_PRE_PMU:
291		/* Override register values set by firmware boot */
292		return cs35l56_force_sync_asp1_registers_from_cache(&cs35l56->base);
293	default:
294		return 0;
295	}
296}
297
298static int cs35l56_play_event(struct snd_soc_dapm_widget *w,
299			      struct snd_kcontrol *kcontrol, int event)
300{
301	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
302	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
303	unsigned int val;
304	int ret;
305
306	dev_dbg(cs35l56->base.dev, "play: %d\n", event);
307
308	switch (event) {
309	case SND_SOC_DAPM_PRE_PMU:
310		/* Don't wait for ACK, we check in POST_PMU that it completed */
311		return regmap_write(cs35l56->base.regmap, CS35L56_DSP_VIRTUAL1_MBOX_1,
312				    CS35L56_MBOX_CMD_AUDIO_PLAY);
313	case SND_SOC_DAPM_POST_PMU:
314		/* Wait for firmware to enter PS0 power state */
315		ret = regmap_read_poll_timeout(cs35l56->base.regmap,
316					       CS35L56_TRANSDUCER_ACTUAL_PS,
317					       val, (val == CS35L56_PS0),
318					       CS35L56_PS0_POLL_US,
319					       CS35L56_PS0_TIMEOUT_US);
320		if (ret)
321			dev_err(cs35l56->base.dev, "PS0 wait failed: %d\n", ret);
322		return ret;
323	case SND_SOC_DAPM_POST_PMD:
324		return cs35l56_mbox_send(&cs35l56->base, CS35L56_MBOX_CMD_AUDIO_PAUSE);
325	default:
326		return 0;
327	}
328}
329
330static const struct snd_soc_dapm_widget cs35l56_dapm_widgets[] = {
331	SND_SOC_DAPM_REGULATOR_SUPPLY("VDD_B", 0, 0),
332	SND_SOC_DAPM_REGULATOR_SUPPLY("VDD_AMP", 0, 0),
333
334	SND_SOC_DAPM_SUPPLY("ASP1 CFG", SND_SOC_NOPM, 0, 0, cs35l56_asp1_cfg_event,
335			    SND_SOC_DAPM_PRE_PMU),
336
337	SND_SOC_DAPM_SUPPLY("PLAY", SND_SOC_NOPM, 0, 0, cs35l56_play_event,
338			    SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
339
340	SND_SOC_DAPM_OUT_DRV("AMP", SND_SOC_NOPM, 0, 0, NULL, 0),
341	SND_SOC_DAPM_OUTPUT("SPK"),
342
343	SND_SOC_DAPM_PGA_E("DSP1", SND_SOC_NOPM, 0, 0, NULL, 0, cs35l56_dsp_event,
344			   SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
345
346	SND_SOC_DAPM_AIF_IN("ASP1RX1", NULL, 0, CS35L56_ASP1_ENABLES1,
347			    CS35L56_ASP_RX1_EN_SHIFT, 0),
348	SND_SOC_DAPM_AIF_IN("ASP1RX2", NULL, 1, CS35L56_ASP1_ENABLES1,
349			    CS35L56_ASP_RX2_EN_SHIFT, 0),
350	SND_SOC_DAPM_AIF_OUT("ASP1TX1", NULL, 0, CS35L56_ASP1_ENABLES1,
351			     CS35L56_ASP_TX1_EN_SHIFT, 0),
352	SND_SOC_DAPM_AIF_OUT("ASP1TX2", NULL, 1, CS35L56_ASP1_ENABLES1,
353			     CS35L56_ASP_TX2_EN_SHIFT, 0),
354	SND_SOC_DAPM_AIF_OUT("ASP1TX3", NULL, 2, CS35L56_ASP1_ENABLES1,
355			     CS35L56_ASP_TX3_EN_SHIFT, 0),
356	SND_SOC_DAPM_AIF_OUT("ASP1TX4", NULL, 3, CS35L56_ASP1_ENABLES1,
357			     CS35L56_ASP_TX4_EN_SHIFT, 0),
358
359	SND_SOC_DAPM_MUX("ASP1 TX1 Source", SND_SOC_NOPM, 0, 0, &asp1_tx1_mux),
360	SND_SOC_DAPM_MUX("ASP1 TX2 Source", SND_SOC_NOPM, 0, 0, &asp1_tx2_mux),
361	SND_SOC_DAPM_MUX("ASP1 TX3 Source", SND_SOC_NOPM, 0, 0, &asp1_tx3_mux),
362	SND_SOC_DAPM_MUX("ASP1 TX4 Source", SND_SOC_NOPM, 0, 0, &asp1_tx4_mux),
363
364	SND_SOC_DAPM_MUX("SDW1 TX1 Source", SND_SOC_NOPM, 0, 0, &sdw1_tx1_mux),
365	SND_SOC_DAPM_MUX("SDW1 TX2 Source", SND_SOC_NOPM, 0, 0, &sdw1_tx2_mux),
366	SND_SOC_DAPM_MUX("SDW1 TX3 Source", SND_SOC_NOPM, 0, 0, &sdw1_tx3_mux),
367	SND_SOC_DAPM_MUX("SDW1 TX4 Source", SND_SOC_NOPM, 0, 0, &sdw1_tx4_mux),
368
369	SND_SOC_DAPM_SIGGEN("VMON ADC"),
370	SND_SOC_DAPM_SIGGEN("IMON ADC"),
371	SND_SOC_DAPM_SIGGEN("ERRVOL ADC"),
372	SND_SOC_DAPM_SIGGEN("CLASSH ADC"),
373	SND_SOC_DAPM_SIGGEN("VDDBMON ADC"),
374	SND_SOC_DAPM_SIGGEN("VBSTMON ADC"),
375	SND_SOC_DAPM_SIGGEN("TEMPMON ADC"),
376};
377
378#define CS35L56_SRC_ROUTE(name) \
379	{ name" Source", "ASP1RX1", "ASP1RX1" }, \
380	{ name" Source", "ASP1RX2", "ASP1RX2" }, \
381	{ name" Source", "VMON", "VMON ADC" }, \
382	{ name" Source", "IMON", "IMON ADC" }, \
383	{ name" Source", "ERRVOL", "ERRVOL ADC" },   \
384	{ name" Source", "CLASSH", "CLASSH ADC" },   \
385	{ name" Source", "VDDBMON", "VDDBMON ADC" }, \
386	{ name" Source", "VBSTMON", "VBSTMON ADC" }, \
387	{ name" Source", "DSP1TX1", "DSP1" }, \
388	{ name" Source", "DSP1TX2", "DSP1" }, \
389	{ name" Source", "DSP1TX3", "DSP1" }, \
390	{ name" Source", "DSP1TX4", "DSP1" }, \
391	{ name" Source", "DSP1TX5", "DSP1" }, \
392	{ name" Source", "DSP1TX6", "DSP1" }, \
393	{ name" Source", "DSP1TX7", "DSP1" }, \
394	{ name" Source", "DSP1TX8", "DSP1" }, \
395	{ name" Source", "TEMPMON", "TEMPMON ADC" }, \
396	{ name" Source", "INTERPOLATOR", "AMP" }, \
397	{ name" Source", "SDW1RX1", "SDW1 Playback" }, \
398	{ name" Source", "SDW1RX2", "SDW1 Playback" },
399
400static const struct snd_soc_dapm_route cs35l56_audio_map[] = {
401	{ "AMP", NULL, "VDD_B" },
402	{ "AMP", NULL, "VDD_AMP" },
403
404	{ "ASP1 Playback", NULL, "ASP1 CFG" },
405	{ "ASP1 Capture", NULL, "ASP1 CFG" },
406
407	{ "ASP1 Playback", NULL, "PLAY" },
408	{ "SDW1 Playback", NULL, "PLAY" },
409
410	{ "ASP1RX1", NULL, "ASP1 Playback" },
411	{ "ASP1RX2", NULL, "ASP1 Playback" },
412	{ "DSP1", NULL, "ASP1RX1" },
413	{ "DSP1", NULL, "ASP1RX2" },
414	{ "DSP1", NULL, "SDW1 Playback" },
415	{ "AMP", NULL, "DSP1" },
416	{ "SPK", NULL, "AMP" },
417
418	CS35L56_SRC_ROUTE("ASP1 TX1")
419	CS35L56_SRC_ROUTE("ASP1 TX2")
420	CS35L56_SRC_ROUTE("ASP1 TX3")
421	CS35L56_SRC_ROUTE("ASP1 TX4")
422
423	{ "ASP1TX1", NULL, "ASP1 TX1 Source" },
424	{ "ASP1TX2", NULL, "ASP1 TX2 Source" },
425	{ "ASP1TX3", NULL, "ASP1 TX3 Source" },
426	{ "ASP1TX4", NULL, "ASP1 TX4 Source" },
427	{ "ASP1 Capture", NULL, "ASP1TX1" },
428	{ "ASP1 Capture", NULL, "ASP1TX2" },
429	{ "ASP1 Capture", NULL, "ASP1TX3" },
430	{ "ASP1 Capture", NULL, "ASP1TX4" },
431
432	CS35L56_SRC_ROUTE("SDW1 TX1")
433	CS35L56_SRC_ROUTE("SDW1 TX2")
434	CS35L56_SRC_ROUTE("SDW1 TX3")
435	CS35L56_SRC_ROUTE("SDW1 TX4")
436	{ "SDW1 Capture", NULL, "SDW1 TX1 Source" },
437	{ "SDW1 Capture", NULL, "SDW1 TX2 Source" },
438	{ "SDW1 Capture", NULL, "SDW1 TX3 Source" },
439	{ "SDW1 Capture", NULL, "SDW1 TX4 Source" },
440};
441
442static int cs35l56_dsp_event(struct snd_soc_dapm_widget *w,
443			     struct snd_kcontrol *kcontrol, int event)
444{
445	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
446	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
447
448	dev_dbg(cs35l56->base.dev, "%s: %d\n", __func__, event);
449
450	return wm_adsp_event(w, kcontrol, event);
451}
452
453static int cs35l56_asp_dai_set_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
454{
455	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(codec_dai->component);
456	unsigned int val;
457	int ret;
458
459	dev_dbg(cs35l56->base.dev, "%s: %#x\n", __func__, fmt);
460
461	ret = cs35l56_init_asp1_regs_for_driver_control(&cs35l56->base);
462	if (ret)
463		return ret;
464
465	switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
466	case SND_SOC_DAIFMT_CBC_CFC:
467		break;
468	default:
469		dev_err(cs35l56->base.dev, "Unsupported clock source mode\n");
470		return -EINVAL;
471	}
472
473	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
474	case SND_SOC_DAIFMT_DSP_A:
475		val = CS35L56_ASP_FMT_DSP_A << CS35L56_ASP_FMT_SHIFT;
476		cs35l56->tdm_mode = true;
477		break;
478	case SND_SOC_DAIFMT_I2S:
479		val = CS35L56_ASP_FMT_I2S << CS35L56_ASP_FMT_SHIFT;
480		cs35l56->tdm_mode = false;
481		break;
482	default:
483		dev_err(cs35l56->base.dev, "Unsupported DAI format\n");
484		return -EINVAL;
485	}
486
487	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
488	case SND_SOC_DAIFMT_NB_IF:
489		val |= CS35L56_ASP_FSYNC_INV_MASK;
490		break;
491	case SND_SOC_DAIFMT_IB_NF:
492		val |= CS35L56_ASP_BCLK_INV_MASK;
493		break;
494	case SND_SOC_DAIFMT_IB_IF:
495		val |= CS35L56_ASP_BCLK_INV_MASK | CS35L56_ASP_FSYNC_INV_MASK;
496		break;
497	case SND_SOC_DAIFMT_NB_NF:
498		break;
499	default:
500		dev_err(cs35l56->base.dev, "Invalid clock invert\n");
501		return -EINVAL;
502	}
503
504	regmap_update_bits(cs35l56->base.regmap,
505			   CS35L56_ASP1_CONTROL2,
506			   CS35L56_ASP_FMT_MASK |
507			   CS35L56_ASP_BCLK_INV_MASK | CS35L56_ASP_FSYNC_INV_MASK,
508			   val);
509
510	/* Hi-Z DOUT in unused slots and when all TX are disabled */
511	regmap_update_bits(cs35l56->base.regmap, CS35L56_ASP1_CONTROL3,
512			   CS35L56_ASP1_DOUT_HIZ_CTRL_MASK,
513			   CS35L56_ASP_UNUSED_HIZ_OFF_HIZ);
514
515	return 0;
516}
517
518static unsigned int cs35l56_make_tdm_config_word(unsigned int reg_val, unsigned long mask)
519{
520	unsigned int channel_shift;
521	int bit_num;
522
523	/* Enable consecutive TX1..TXn for each of the slots set in mask */
524	channel_shift = 0;
525	for_each_set_bit(bit_num, &mask, 32) {
526		reg_val &= ~(0x3f << channel_shift);
527		reg_val |= bit_num << channel_shift;
528		channel_shift += 8;
529	}
530
531	return reg_val;
532}
533
534static int cs35l56_asp_dai_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
535					unsigned int rx_mask, int slots, int slot_width)
536{
537	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(dai->component);
538	int ret;
539
540	ret = cs35l56_init_asp1_regs_for_driver_control(&cs35l56->base);
541	if (ret)
542		return ret;
543
544	if ((slots == 0) || (slot_width == 0)) {
545		dev_dbg(cs35l56->base.dev, "tdm config cleared\n");
546		cs35l56->asp_slot_width = 0;
547		cs35l56->asp_slot_count = 0;
548		return 0;
549	}
550
551	if (slot_width > (CS35L56_ASP_RX_WIDTH_MASK >> CS35L56_ASP_RX_WIDTH_SHIFT)) {
552		dev_err(cs35l56->base.dev, "tdm invalid slot width %d\n", slot_width);
553		return -EINVAL;
554	}
555
556	/* More than 32 slots would give an unsupportable BCLK frequency */
557	if (slots > 32) {
558		dev_err(cs35l56->base.dev, "tdm invalid slot count %d\n", slots);
559		return -EINVAL;
560	}
561
562	cs35l56->asp_slot_width = (u8)slot_width;
563	cs35l56->asp_slot_count = (u8)slots;
564
565	// Note: rx/tx is from point of view of the CPU end
566	if (tx_mask == 0)
567		tx_mask = 0x3;	// ASPRX1/RX2 in slots 0 and 1
568
569	if (rx_mask == 0)
570		rx_mask = 0xf;	// ASPTX1..TX4 in slots 0..3
571
572	/* Default unused slots to 63 */
573	regmap_write(cs35l56->base.regmap, CS35L56_ASP1_FRAME_CONTROL1,
574		     cs35l56_make_tdm_config_word(0x3f3f3f3f, rx_mask));
575	regmap_write(cs35l56->base.regmap, CS35L56_ASP1_FRAME_CONTROL5,
576		     cs35l56_make_tdm_config_word(0x3f3f3f, tx_mask));
577
578	dev_dbg(cs35l56->base.dev, "tdm slot width: %u count: %u tx_mask: %#x rx_mask: %#x\n",
579		cs35l56->asp_slot_width, cs35l56->asp_slot_count, tx_mask, rx_mask);
580
581	return 0;
582}
583
584static int cs35l56_asp_dai_hw_params(struct snd_pcm_substream *substream,
585				     struct snd_pcm_hw_params *params,
586				     struct snd_soc_dai *dai)
587{
588	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(dai->component);
589	unsigned int rate = params_rate(params);
590	u8 asp_width, asp_wl;
591	int ret;
592
593	ret = cs35l56_init_asp1_regs_for_driver_control(&cs35l56->base);
594	if (ret)
595		return ret;
596
597	asp_wl = params_width(params);
598	if (cs35l56->asp_slot_width)
599		asp_width = cs35l56->asp_slot_width;
600	else
601		asp_width = asp_wl;
602
603	dev_dbg(cs35l56->base.dev, "%s: wl=%d, width=%d, rate=%d",
604		__func__, asp_wl, asp_width, rate);
605
606	if (!cs35l56->sysclk_set) {
607		unsigned int slots = cs35l56->asp_slot_count;
608		unsigned int bclk_freq;
609		int freq_id;
610
611		if (slots == 0) {
612			slots = params_channels(params);
613
614			/* I2S always has an even number of slots */
615			if (!cs35l56->tdm_mode)
616				slots = round_up(slots, 2);
617		}
618
619		bclk_freq = asp_width * slots * rate;
620		freq_id = cs35l56_get_bclk_freq_id(bclk_freq);
621		if (freq_id < 0) {
622			dev_err(cs35l56->base.dev, "%s: Invalid BCLK %u\n", __func__, bclk_freq);
623			return -EINVAL;
624		}
625
626		regmap_update_bits(cs35l56->base.regmap, CS35L56_ASP1_CONTROL1,
627				   CS35L56_ASP_BCLK_FREQ_MASK,
628				   freq_id << CS35L56_ASP_BCLK_FREQ_SHIFT);
629	}
630
631	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
632		regmap_update_bits(cs35l56->base.regmap, CS35L56_ASP1_CONTROL2,
633				   CS35L56_ASP_RX_WIDTH_MASK, asp_width <<
634				   CS35L56_ASP_RX_WIDTH_SHIFT);
635		regmap_update_bits(cs35l56->base.regmap, CS35L56_ASP1_DATA_CONTROL5,
636				   CS35L56_ASP_RX_WL_MASK, asp_wl);
637	} else {
638		regmap_update_bits(cs35l56->base.regmap, CS35L56_ASP1_CONTROL2,
639				   CS35L56_ASP_TX_WIDTH_MASK, asp_width <<
640				   CS35L56_ASP_TX_WIDTH_SHIFT);
641		regmap_update_bits(cs35l56->base.regmap, CS35L56_ASP1_DATA_CONTROL1,
642				   CS35L56_ASP_TX_WL_MASK, asp_wl);
643	}
644
645	return 0;
646}
647
648static int cs35l56_asp_dai_set_sysclk(struct snd_soc_dai *dai,
649				      int clk_id, unsigned int freq, int dir)
650{
651	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(dai->component);
652	int freq_id, ret;
653
654	ret = cs35l56_init_asp1_regs_for_driver_control(&cs35l56->base);
655	if (ret)
656		return ret;
657
658	if (freq == 0) {
659		cs35l56->sysclk_set = false;
660		return 0;
661	}
662
663	freq_id = cs35l56_get_bclk_freq_id(freq);
664	if (freq_id < 0)
665		return freq_id;
666
667	regmap_update_bits(cs35l56->base.regmap, CS35L56_ASP1_CONTROL1,
668			   CS35L56_ASP_BCLK_FREQ_MASK,
669			   freq_id << CS35L56_ASP_BCLK_FREQ_SHIFT);
670	cs35l56->sysclk_set = true;
671
672	return 0;
673}
674
675static const struct snd_soc_dai_ops cs35l56_ops = {
676	.set_fmt = cs35l56_asp_dai_set_fmt,
677	.set_tdm_slot = cs35l56_asp_dai_set_tdm_slot,
678	.hw_params = cs35l56_asp_dai_hw_params,
679	.set_sysclk = cs35l56_asp_dai_set_sysclk,
680};
681
682static void cs35l56_sdw_dai_shutdown(struct snd_pcm_substream *substream,
683				     struct snd_soc_dai *dai)
684{
685	snd_soc_dai_set_dma_data(dai, substream, NULL);
686}
687
688static int cs35l56_sdw_dai_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
689					unsigned int rx_mask, int slots, int slot_width)
690{
691	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(dai->component);
692
693	/* rx/tx are from point of view of the CPU end so opposite to our rx/tx */
694	cs35l56->rx_mask = tx_mask;
695	cs35l56->tx_mask = rx_mask;
696
697	return 0;
698}
699
700static int cs35l56_sdw_dai_hw_params(struct snd_pcm_substream *substream,
701				     struct snd_pcm_hw_params *params,
702				     struct snd_soc_dai *dai)
703{
704	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(dai->component);
705	struct sdw_stream_runtime *sdw_stream = snd_soc_dai_get_dma_data(dai, substream);
706	struct sdw_stream_config sconfig;
707	struct sdw_port_config pconfig;
708	int ret;
709
710	dev_dbg(cs35l56->base.dev, "%s: rate %d\n", __func__, params_rate(params));
711
712	if (!cs35l56->base.init_done)
713		return -ENODEV;
714
715	if (!sdw_stream)
716		return -EINVAL;
717
718	memset(&sconfig, 0, sizeof(sconfig));
719	memset(&pconfig, 0, sizeof(pconfig));
720
721	sconfig.frame_rate = params_rate(params);
722	sconfig.bps = snd_pcm_format_width(params_format(params));
723
724	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
725		sconfig.direction = SDW_DATA_DIR_RX;
726		pconfig.num = CS35L56_SDW1_PLAYBACK_PORT;
727		pconfig.ch_mask = cs35l56->rx_mask;
728	} else {
729		sconfig.direction = SDW_DATA_DIR_TX;
730		pconfig.num = CS35L56_SDW1_CAPTURE_PORT;
731		pconfig.ch_mask = cs35l56->tx_mask;
732	}
733
734	if (pconfig.ch_mask == 0) {
735		sconfig.ch_count = params_channels(params);
736		pconfig.ch_mask = GENMASK(sconfig.ch_count - 1, 0);
737	} else {
738		sconfig.ch_count = hweight32(pconfig.ch_mask);
739	}
740
741	ret = sdw_stream_add_slave(cs35l56->sdw_peripheral, &sconfig, &pconfig,
742				   1, sdw_stream);
743	if (ret) {
744		dev_err(dai->dev, "Failed to add sdw stream: %d\n", ret);
745		return ret;
746	}
747
748	return 0;
749}
750
751static int cs35l56_sdw_dai_hw_free(struct snd_pcm_substream *substream,
752				   struct snd_soc_dai *dai)
753{
754	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(dai->component);
755	struct sdw_stream_runtime *sdw_stream = snd_soc_dai_get_dma_data(dai, substream);
756
757	if (!cs35l56->sdw_peripheral)
758		return -EINVAL;
759
760	sdw_stream_remove_slave(cs35l56->sdw_peripheral, sdw_stream);
761
762	return 0;
763}
764
765static int cs35l56_sdw_dai_set_stream(struct snd_soc_dai *dai,
766				      void *sdw_stream, int direction)
767{
768	snd_soc_dai_dma_data_set(dai, direction, sdw_stream);
769
770	return 0;
771}
772
773static const struct snd_soc_dai_ops cs35l56_sdw_dai_ops = {
774	.set_tdm_slot = cs35l56_sdw_dai_set_tdm_slot,
775	.shutdown = cs35l56_sdw_dai_shutdown,
776	.hw_params = cs35l56_sdw_dai_hw_params,
777	.hw_free = cs35l56_sdw_dai_hw_free,
778	.set_stream = cs35l56_sdw_dai_set_stream,
779};
780
781static struct snd_soc_dai_driver cs35l56_dai[] = {
782	{
783		.name = "cs35l56-asp1",
784		.id = 0,
785		.playback = {
786			.stream_name = "ASP1 Playback",
787			.channels_min = 1,
788			.channels_max = 2,
789			.rates = CS35L56_RATES,
790			.formats = CS35L56_RX_FORMATS,
791		},
792		.capture = {
793			.stream_name = "ASP1 Capture",
794			.channels_min = 1,
795			.channels_max = 4,
796			.rates = CS35L56_RATES,
797			.formats = CS35L56_TX_FORMATS,
798		},
799		.ops = &cs35l56_ops,
800		.symmetric_rate = 1,
801		.symmetric_sample_bits = 1,
802	},
803	{
804		.name = "cs35l56-sdw1",
805		.id = 1,
806		.playback = {
807			.stream_name = "SDW1 Playback",
808			.channels_min = 1,
809			.channels_max = 2,
810			.rates = CS35L56_RATES,
811			.formats = CS35L56_RX_FORMATS,
812		},
813		.capture = {
814			.stream_name = "SDW1 Capture",
815			.channels_min = 1,
816			.channels_max = 4,
817			.rates = CS35L56_RATES,
818			.formats = CS35L56_TX_FORMATS,
819		},
820		.symmetric_rate = 1,
821		.ops = &cs35l56_sdw_dai_ops,
822	}
823};
824
825static int cs35l56_write_cal(struct cs35l56_private *cs35l56)
826{
827	int ret;
828
829	if (cs35l56->base.secured || !cs35l56->base.cal_data_valid)
830		return -ENODATA;
831
832	ret = wm_adsp_run(&cs35l56->dsp);
833	if (ret)
834		return ret;
835
836	ret = cs_amp_write_cal_coeffs(&cs35l56->dsp.cs_dsp,
837				      &cs35l56_calibration_controls,
838				      &cs35l56->base.cal_data);
839
840	wm_adsp_stop(&cs35l56->dsp);
841
842	if (ret == 0)
843		dev_info(cs35l56->base.dev, "Calibration applied\n");
844
845	return ret;
846}
847
848static void cs35l56_reinit_patch(struct cs35l56_private *cs35l56)
849{
850	int ret;
851
852	/* Use wm_adsp to load and apply the firmware patch and coefficient files */
853	ret = wm_adsp_power_up(&cs35l56->dsp, true);
854	if (ret) {
855		dev_dbg(cs35l56->base.dev, "%s: wm_adsp_power_up ret %d\n", __func__, ret);
856		return;
857	}
858
859	cs35l56_write_cal(cs35l56);
860
861	/* Always REINIT after applying patch or coefficients */
862	cs35l56_mbox_send(&cs35l56->base, CS35L56_MBOX_CMD_AUDIO_REINIT);
863}
864
865static void cs35l56_patch(struct cs35l56_private *cs35l56, bool firmware_missing)
866{
867	int ret;
868
869	/*
870	 * Disable SoundWire interrupts to prevent race with IRQ work.
871	 * Setting sdw_irq_no_unmask prevents the handler re-enabling
872	 * the SoundWire interrupt.
873	 */
874	if (cs35l56->sdw_peripheral) {
875		cs35l56->sdw_irq_no_unmask = true;
876		flush_work(&cs35l56->sdw_irq_work);
877		sdw_write_no_pm(cs35l56->sdw_peripheral, CS35L56_SDW_GEN_INT_MASK_1, 0);
878		sdw_read_no_pm(cs35l56->sdw_peripheral, CS35L56_SDW_GEN_INT_STAT_1);
879		sdw_write_no_pm(cs35l56->sdw_peripheral, CS35L56_SDW_GEN_INT_STAT_1, 0xFF);
880		flush_work(&cs35l56->sdw_irq_work);
881	}
882
883	ret = cs35l56_firmware_shutdown(&cs35l56->base);
884	if (ret)
885		goto err;
886
887	/*
888	 * Use wm_adsp to load and apply the firmware patch and coefficient files,
889	 * but only if firmware is missing. If firmware is already patched just
890	 * power-up wm_adsp without downloading firmware.
891	 */
892	ret = wm_adsp_power_up(&cs35l56->dsp, !!firmware_missing);
893	if (ret) {
894		dev_dbg(cs35l56->base.dev, "%s: wm_adsp_power_up ret %d\n", __func__, ret);
895		goto err;
896	}
897
898	mutex_lock(&cs35l56->base.irq_lock);
899
900	reinit_completion(&cs35l56->init_completion);
901
902	cs35l56->soft_resetting = true;
903	cs35l56_system_reset(&cs35l56->base, !!cs35l56->sdw_peripheral);
904
905	if (cs35l56->sdw_peripheral) {
906		/*
907		 * The system-reset causes the CS35L56 to detach from the bus.
908		 * Wait for the manager to re-enumerate the CS35L56 and
909		 * cs35l56_init() to run again.
910		 */
911		if (!wait_for_completion_timeout(&cs35l56->init_completion,
912						 msecs_to_jiffies(5000))) {
913			dev_err(cs35l56->base.dev, "%s: init_completion timed out (SDW)\n",
914				__func__);
915			goto err_unlock;
916		}
917	} else if (cs35l56_init(cs35l56)) {
918		goto err_unlock;
919	}
920
921	regmap_clear_bits(cs35l56->base.regmap, CS35L56_PROTECTION_STATUS,
922			  CS35L56_FIRMWARE_MISSING);
923	cs35l56->base.fw_patched = true;
924
925	if (cs35l56_write_cal(cs35l56) == 0)
926		cs35l56_mbox_send(&cs35l56->base, CS35L56_MBOX_CMD_AUDIO_REINIT);
927
928err_unlock:
929	mutex_unlock(&cs35l56->base.irq_lock);
930err:
931	/* Re-enable SoundWire interrupts */
932	if (cs35l56->sdw_peripheral) {
933		cs35l56->sdw_irq_no_unmask = false;
934		sdw_write_no_pm(cs35l56->sdw_peripheral, CS35L56_SDW_GEN_INT_MASK_1,
935				CS35L56_SDW_INT_MASK_CODEC_IRQ);
936	}
937}
938
939static void cs35l56_dsp_work(struct work_struct *work)
940{
941	struct cs35l56_private *cs35l56 = container_of(work,
942						       struct cs35l56_private,
943						       dsp_work);
944	unsigned int firmware_version;
945	bool firmware_missing;
946	int ret;
947
948	if (!cs35l56->base.init_done)
949		return;
950
951	pm_runtime_get_sync(cs35l56->base.dev);
952
953	ret = cs35l56_read_prot_status(&cs35l56->base, &firmware_missing, &firmware_version);
954	if (ret)
955		goto err;
956
957	/* Populate fw file qualifier with the revision and security state */
958	kfree(cs35l56->dsp.fwf_name);
959	if (firmware_missing) {
960		cs35l56->dsp.fwf_name = kasprintf(GFP_KERNEL, "%02x-dsp1", cs35l56->base.rev);
961	} else {
962		/* Firmware files must match the running firmware version */
963		cs35l56->dsp.fwf_name = kasprintf(GFP_KERNEL,
964						  "%02x%s-%06x-dsp1",
965						  cs35l56->base.rev,
966						  cs35l56->base.secured ? "-s" : "",
967						  firmware_version);
968	}
969
970	if (!cs35l56->dsp.fwf_name)
971		goto err;
972
973	dev_dbg(cs35l56->base.dev, "DSP fwf name: '%s' system name: '%s'\n",
974		cs35l56->dsp.fwf_name, cs35l56->dsp.system_name);
975
976	/*
977	 * The firmware cannot be patched if it is already running from
978	 * patch RAM. In this case the firmware files are versioned to
979	 * match the running firmware version and will only contain
980	 * tunings. We do not need to shutdown the firmware to apply
981	 * tunings so can use the lower cost reinit sequence instead.
982	 */
983	if (!firmware_missing)
984		cs35l56_reinit_patch(cs35l56);
985	else
986		cs35l56_patch(cs35l56, firmware_missing);
987
988err:
989	pm_runtime_mark_last_busy(cs35l56->base.dev);
990	pm_runtime_put_autosuspend(cs35l56->base.dev);
991}
992
993static int cs35l56_component_probe(struct snd_soc_component *component)
994{
995	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
996	struct dentry *debugfs_root = component->debugfs_root;
997	unsigned short vendor, device;
998
999	BUILD_BUG_ON(ARRAY_SIZE(cs35l56_tx_input_texts) != ARRAY_SIZE(cs35l56_tx_input_values));
1000
1001	if (!cs35l56->dsp.system_name &&
1002	    (snd_soc_card_get_pci_ssid(component->card, &vendor, &device) == 0)) {
1003		/* Append a speaker qualifier if there is a speaker ID */
1004		if (cs35l56->speaker_id >= 0) {
1005			cs35l56->dsp.system_name = devm_kasprintf(cs35l56->base.dev,
1006								  GFP_KERNEL,
1007								  "%04x%04x-spkid%d",
1008								  vendor, device,
1009								  cs35l56->speaker_id);
1010		} else {
1011			cs35l56->dsp.system_name = devm_kasprintf(cs35l56->base.dev,
1012								  GFP_KERNEL,
1013								  "%04x%04x",
1014								  vendor, device);
1015		}
1016		if (!cs35l56->dsp.system_name)
1017			return -ENOMEM;
1018	}
1019
1020	if (!wait_for_completion_timeout(&cs35l56->init_completion,
1021					 msecs_to_jiffies(5000))) {
1022		dev_err(cs35l56->base.dev, "%s: init_completion timed out\n", __func__);
1023		return -ENODEV;
1024	}
1025
1026	cs35l56->dsp.part = kasprintf(GFP_KERNEL, "cs35l%02x", cs35l56->base.type);
1027	if (!cs35l56->dsp.part)
1028		return -ENOMEM;
1029
1030	cs35l56->component = component;
1031	wm_adsp2_component_probe(&cs35l56->dsp, component);
1032
1033	debugfs_create_bool("init_done", 0444, debugfs_root, &cs35l56->base.init_done);
1034	debugfs_create_bool("can_hibernate", 0444, debugfs_root, &cs35l56->base.can_hibernate);
1035	debugfs_create_bool("fw_patched", 0444, debugfs_root, &cs35l56->base.fw_patched);
1036
1037	/*
1038	 * The widgets for the ASP1TX mixer can't be initialized
1039	 * until the firmware has been downloaded and rebooted.
1040	 */
1041	regcache_drop_region(cs35l56->base.regmap, CS35L56_ASP1TX1_INPUT, CS35L56_ASP1TX4_INPUT);
1042	cs35l56->asp1_mixer_widgets_initialized = false;
1043
1044	queue_work(cs35l56->dsp_wq, &cs35l56->dsp_work);
1045
1046	return 0;
1047}
1048
1049static void cs35l56_component_remove(struct snd_soc_component *component)
1050{
1051	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
1052
1053	cancel_work_sync(&cs35l56->dsp_work);
1054
1055	if (cs35l56->dsp.cs_dsp.booted)
1056		wm_adsp_power_down(&cs35l56->dsp);
1057
1058	wm_adsp2_component_remove(&cs35l56->dsp, component);
1059
1060	kfree(cs35l56->dsp.part);
1061	cs35l56->dsp.part = NULL;
1062
1063	kfree(cs35l56->dsp.fwf_name);
1064	cs35l56->dsp.fwf_name = NULL;
1065
1066	cs35l56->component = NULL;
1067}
1068
1069static int cs35l56_set_bias_level(struct snd_soc_component *component,
1070				  enum snd_soc_bias_level level)
1071{
1072	struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
1073
1074	switch (level) {
1075	case SND_SOC_BIAS_STANDBY:
1076		/*
1077		 * Wait for patching to complete when transitioning from
1078		 * BIAS_OFF to BIAS_STANDBY
1079		 */
1080		if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF)
1081			cs35l56_wait_dsp_ready(cs35l56);
1082
1083		break;
1084	default:
1085		break;
1086	}
1087
1088	return 0;
1089}
1090
1091static const struct snd_soc_component_driver soc_component_dev_cs35l56 = {
1092	.probe = cs35l56_component_probe,
1093	.remove = cs35l56_component_remove,
1094
1095	.dapm_widgets = cs35l56_dapm_widgets,
1096	.num_dapm_widgets = ARRAY_SIZE(cs35l56_dapm_widgets),
1097	.dapm_routes = cs35l56_audio_map,
1098	.num_dapm_routes = ARRAY_SIZE(cs35l56_audio_map),
1099	.controls = cs35l56_controls,
1100	.num_controls = ARRAY_SIZE(cs35l56_controls),
1101
1102	.set_bias_level = cs35l56_set_bias_level,
1103
1104	.suspend_bias_off = 1, /* see cs35l56_system_resume() */
1105};
1106
1107static int __maybe_unused cs35l56_runtime_suspend_i2c_spi(struct device *dev)
1108{
1109	struct cs35l56_private *cs35l56 = dev_get_drvdata(dev);
1110
1111	return cs35l56_runtime_suspend_common(&cs35l56->base);
1112}
1113
1114static int __maybe_unused cs35l56_runtime_resume_i2c_spi(struct device *dev)
1115{
1116	struct cs35l56_private *cs35l56 = dev_get_drvdata(dev);
1117
1118	return cs35l56_runtime_resume_common(&cs35l56->base, false);
1119}
1120
1121int cs35l56_system_suspend(struct device *dev)
1122{
1123	struct cs35l56_private *cs35l56 = dev_get_drvdata(dev);
1124
1125	dev_dbg(dev, "system_suspend\n");
1126
1127	if (cs35l56->component)
1128		flush_work(&cs35l56->dsp_work);
1129
1130	/*
1131	 * The interrupt line is normally shared, but after we start suspending
1132	 * we can't check if our device is the source of an interrupt, and can't
1133	 * clear it. Prevent this race by temporarily disabling the parent irq
1134	 * until we reach _no_irq.
1135	 */
1136	if (cs35l56->base.irq)
1137		disable_irq(cs35l56->base.irq);
1138
1139	return pm_runtime_force_suspend(dev);
1140}
1141EXPORT_SYMBOL_GPL(cs35l56_system_suspend);
1142
1143int cs35l56_system_suspend_late(struct device *dev)
1144{
1145	struct cs35l56_private *cs35l56 = dev_get_drvdata(dev);
1146
1147	dev_dbg(dev, "system_suspend_late\n");
1148
1149	/*
1150	 * Assert RESET before removing supplies.
1151	 * RESET is usually shared by all amps so it must not be asserted until
1152	 * all driver instances have done their suspend() stage.
1153	 */
1154	if (cs35l56->base.reset_gpio) {
1155		gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 0);
1156		cs35l56_wait_min_reset_pulse();
1157	}
1158
1159	regulator_bulk_disable(ARRAY_SIZE(cs35l56->supplies), cs35l56->supplies);
1160
1161	return 0;
1162}
1163EXPORT_SYMBOL_GPL(cs35l56_system_suspend_late);
1164
1165int cs35l56_system_suspend_no_irq(struct device *dev)
1166{
1167	struct cs35l56_private *cs35l56 = dev_get_drvdata(dev);
1168
1169	dev_dbg(dev, "system_suspend_no_irq\n");
1170
1171	/* Handlers are now disabled so the parent IRQ can safely be re-enabled. */
1172	if (cs35l56->base.irq)
1173		enable_irq(cs35l56->base.irq);
1174
1175	return 0;
1176}
1177EXPORT_SYMBOL_GPL(cs35l56_system_suspend_no_irq);
1178
1179int cs35l56_system_resume_no_irq(struct device *dev)
1180{
1181	struct cs35l56_private *cs35l56 = dev_get_drvdata(dev);
1182
1183	dev_dbg(dev, "system_resume_no_irq\n");
1184
1185	/*
1186	 * WAKE interrupts unmask if the CS35L56 hibernates, which can cause
1187	 * spurious interrupts, and the interrupt line is normally shared.
1188	 * We can't check if our device is the source of an interrupt, and can't
1189	 * clear it, until it has fully resumed. Prevent this race by temporarily
1190	 * disabling the parent irq until we complete resume().
1191	 */
1192	if (cs35l56->base.irq)
1193		disable_irq(cs35l56->base.irq);
1194
1195	return 0;
1196}
1197EXPORT_SYMBOL_GPL(cs35l56_system_resume_no_irq);
1198
1199int cs35l56_system_resume_early(struct device *dev)
1200{
1201	struct cs35l56_private *cs35l56 = dev_get_drvdata(dev);
1202	int ret;
1203
1204	dev_dbg(dev, "system_resume_early\n");
1205
1206	/* Ensure a spec-compliant RESET pulse. */
1207	if (cs35l56->base.reset_gpio) {
1208		gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 0);
1209		cs35l56_wait_min_reset_pulse();
1210	}
1211
1212	/* Enable supplies before releasing RESET. */
1213	ret = regulator_bulk_enable(ARRAY_SIZE(cs35l56->supplies), cs35l56->supplies);
1214	if (ret) {
1215		dev_err(dev, "system_resume_early failed to enable supplies: %d\n", ret);
1216		return ret;
1217	}
1218
1219	/* Release shared RESET before drivers start resume(). */
1220	gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 1);
1221
1222	return 0;
1223}
1224EXPORT_SYMBOL_GPL(cs35l56_system_resume_early);
1225
1226int cs35l56_system_resume(struct device *dev)
1227{
1228	struct cs35l56_private *cs35l56 = dev_get_drvdata(dev);
1229	int ret;
1230
1231	dev_dbg(dev, "system_resume\n");
1232
1233	/*
1234	 * We might have done a hard reset or the CS35L56 was power-cycled
1235	 * so wait for control port to be ready.
1236	 */
1237	cs35l56_wait_control_port_ready();
1238
1239	/* Undo pm_runtime_force_suspend() before re-enabling the irq */
1240	ret = pm_runtime_force_resume(dev);
1241	if (cs35l56->base.irq)
1242		enable_irq(cs35l56->base.irq);
1243
1244	if (ret)
1245		return ret;
1246
1247	/* Firmware won't have been loaded if the component hasn't probed */
1248	if (!cs35l56->component)
1249		return 0;
1250
1251	ret = cs35l56_is_fw_reload_needed(&cs35l56->base);
1252	dev_dbg(cs35l56->base.dev, "fw_reload_needed: %d\n", ret);
1253	if (ret < 1)
1254		return ret;
1255
1256	cs35l56->base.fw_patched = false;
1257	wm_adsp_power_down(&cs35l56->dsp);
1258	queue_work(cs35l56->dsp_wq, &cs35l56->dsp_work);
1259
1260	/*
1261	 * suspend_bias_off ensures we are now in BIAS_OFF so there will be
1262	 * a BIAS_OFF->BIAS_STANDBY transition to complete dsp patching.
1263	 */
1264
1265	return 0;
1266}
1267EXPORT_SYMBOL_GPL(cs35l56_system_resume);
1268
1269static int cs35l56_dsp_init(struct cs35l56_private *cs35l56)
1270{
1271	struct wm_adsp *dsp;
1272	int ret;
1273
1274	cs35l56->dsp_wq = create_singlethread_workqueue("cs35l56-dsp");
1275	if (!cs35l56->dsp_wq)
1276		return -ENOMEM;
1277
1278	INIT_WORK(&cs35l56->dsp_work, cs35l56_dsp_work);
1279
1280	dsp = &cs35l56->dsp;
1281	cs35l56_init_cs_dsp(&cs35l56->base, &dsp->cs_dsp);
1282
1283	/*
1284	 * dsp->part is filled in later as it is based on the DEVID. In a
1285	 * SoundWire system that cannot be read until enumeration has occurred
1286	 * and the device has attached.
1287	 */
1288	dsp->fw = 12;
1289	dsp->wmfw_optional = true;
1290
1291	dev_dbg(cs35l56->base.dev, "DSP system name: '%s'\n", dsp->system_name);
1292
1293	ret = wm_halo_init(dsp);
1294	if (ret != 0) {
1295		dev_err(cs35l56->base.dev, "wm_halo_init failed\n");
1296		return ret;
1297	}
1298
1299	return 0;
1300}
1301
1302static int cs35l56_get_firmware_uid(struct cs35l56_private *cs35l56)
1303{
1304	struct device *dev = cs35l56->base.dev;
1305	const char *prop;
1306	int ret;
1307
1308	ret = device_property_read_string(dev, "cirrus,firmware-uid", &prop);
1309	/* If bad sw node property, return 0 and fallback to legacy firmware path */
1310	if (ret < 0)
1311		return 0;
1312
1313	/* Append a speaker qualifier if there is a speaker ID */
1314	if (cs35l56->speaker_id >= 0)
1315		cs35l56->dsp.system_name = devm_kasprintf(dev, GFP_KERNEL, "%s-spkid%d",
1316							  prop, cs35l56->speaker_id);
1317	else
1318		cs35l56->dsp.system_name = devm_kstrdup(dev, prop, GFP_KERNEL);
1319
1320	if (cs35l56->dsp.system_name == NULL)
1321		return -ENOMEM;
1322
1323	dev_dbg(dev, "Firmware UID: %s\n", cs35l56->dsp.system_name);
1324
1325	return 0;
1326}
1327
1328/*
1329 * Some SoundWire laptops have a spk-id-gpios property but it points to
1330 * the wrong ACPI Device node so can't be used to get the GPIO. Try to
1331 * find the SDCA node containing the GpioIo resource and add a GPIO
1332 * mapping to it.
1333 */
1334static const struct acpi_gpio_params cs35l56_af01_first_gpio = { 0, 0, false };
1335static const struct acpi_gpio_mapping cs35l56_af01_spkid_gpios_mapping[] = {
1336	{ "spk-id-gpios", &cs35l56_af01_first_gpio, 1 },
1337	{ }
1338};
1339
1340static void cs35l56_acpi_dev_release_driver_gpios(void *adev)
1341{
1342	acpi_dev_remove_driver_gpios(adev);
1343}
1344
1345static int cs35l56_try_get_broken_sdca_spkid_gpio(struct cs35l56_private *cs35l56)
1346{
1347	struct fwnode_handle *af01_fwnode;
1348	const union acpi_object *obj;
1349	struct gpio_desc *desc;
1350	int ret;
1351
1352	/* Find the SDCA node containing the GpioIo */
1353	af01_fwnode = device_get_named_child_node(cs35l56->base.dev, "AF01");
1354	if (!af01_fwnode) {
1355		dev_dbg(cs35l56->base.dev, "No AF01 node\n");
1356		return -ENOENT;
1357	}
1358
1359	ret = acpi_dev_get_property(ACPI_COMPANION(cs35l56->base.dev),
1360				    "spk-id-gpios", ACPI_TYPE_PACKAGE, &obj);
1361	if (ret) {
1362		dev_dbg(cs35l56->base.dev, "Could not get spk-id-gpios package: %d\n", ret);
1363		fwnode_handle_put(af01_fwnode);
1364		return -ENOENT;
1365	}
1366
1367	/* The broken properties we can handle are a 4-element package (one GPIO) */
1368	if (obj->package.count != 4) {
1369		dev_warn(cs35l56->base.dev, "Unexpected spk-id element count %d\n",
1370			 obj->package.count);
1371		fwnode_handle_put(af01_fwnode);
1372		return -ENOENT;
1373	}
1374
1375	/* Add a GPIO mapping if it doesn't already have one */
1376	if (!fwnode_property_present(af01_fwnode, "spk-id-gpios")) {
1377		struct acpi_device *adev = to_acpi_device_node(af01_fwnode);
1378
1379		/*
1380		 * Can't use devm_acpi_dev_add_driver_gpios() because the
1381		 * mapping isn't being added to the node pointed to by
1382		 * ACPI_COMPANION().
1383		 */
1384		ret = acpi_dev_add_driver_gpios(adev, cs35l56_af01_spkid_gpios_mapping);
1385		if (ret) {
1386			fwnode_handle_put(af01_fwnode);
1387			return dev_err_probe(cs35l56->base.dev, ret,
1388					     "Failed to add gpio mapping to AF01\n");
1389		}
1390
1391		ret = devm_add_action_or_reset(cs35l56->base.dev,
1392					       cs35l56_acpi_dev_release_driver_gpios,
1393					       adev);
1394		if (ret) {
1395			fwnode_handle_put(af01_fwnode);
1396			return ret;
1397		}
1398
1399		dev_dbg(cs35l56->base.dev, "Added spk-id-gpios mapping to AF01\n");
1400	}
1401
1402	desc = fwnode_gpiod_get_index(af01_fwnode, "spk-id", 0, GPIOD_IN, NULL);
1403	if (IS_ERR(desc)) {
1404		fwnode_handle_put(af01_fwnode);
1405		ret = PTR_ERR(desc);
1406		return dev_err_probe(cs35l56->base.dev, ret, "Get GPIO from AF01 failed\n");
1407	}
1408
1409	ret = gpiod_get_value_cansleep(desc);
1410	gpiod_put(desc);
1411
1412	if (ret < 0) {
1413		fwnode_handle_put(af01_fwnode);
1414		dev_err_probe(cs35l56->base.dev, ret, "Error reading spk-id GPIO\n");
1415		return ret;
1416	}
1417
1418	fwnode_handle_put(af01_fwnode);
1419
1420	dev_info(cs35l56->base.dev, "Got spk-id from AF01\n");
1421
1422	return ret;
1423}
1424
1425int cs35l56_common_probe(struct cs35l56_private *cs35l56)
1426{
1427	int ret;
1428
1429	init_completion(&cs35l56->init_completion);
1430	mutex_init(&cs35l56->base.irq_lock);
1431	cs35l56->base.cal_index = -1;
1432	cs35l56->speaker_id = -ENOENT;
1433
1434	/* Assume that the firmware owns ASP1 until we know different */
1435	cs35l56->base.fw_owns_asp1 = true;
1436
1437	dev_set_drvdata(cs35l56->base.dev, cs35l56);
1438
1439	cs35l56_fill_supply_names(cs35l56->supplies);
1440	ret = devm_regulator_bulk_get(cs35l56->base.dev, ARRAY_SIZE(cs35l56->supplies),
1441				      cs35l56->supplies);
1442	if (ret != 0)
1443		return dev_err_probe(cs35l56->base.dev, ret, "Failed to request supplies\n");
1444
1445	/* Reset could be controlled by the BIOS or shared by multiple amps */
1446	cs35l56->base.reset_gpio = devm_gpiod_get_optional(cs35l56->base.dev, "reset",
1447							   GPIOD_OUT_LOW);
1448	if (IS_ERR(cs35l56->base.reset_gpio)) {
1449		ret = PTR_ERR(cs35l56->base.reset_gpio);
1450		/*
1451		 * If RESET is shared the first amp to probe will grab the reset
1452		 * line and reset all the amps
1453		 */
1454		if (ret != -EBUSY)
1455			return dev_err_probe(cs35l56->base.dev, ret, "Failed to get reset GPIO\n");
1456
1457		dev_info(cs35l56->base.dev, "Reset GPIO busy, assume shared reset\n");
1458		cs35l56->base.reset_gpio = NULL;
1459	}
1460
1461	ret = regulator_bulk_enable(ARRAY_SIZE(cs35l56->supplies), cs35l56->supplies);
1462	if (ret != 0)
1463		return dev_err_probe(cs35l56->base.dev, ret, "Failed to enable supplies\n");
1464
1465	if (cs35l56->base.reset_gpio) {
1466		/* ACPI can override GPIOD_OUT_LOW flag so force it to start low */
1467		gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 0);
1468		cs35l56_wait_min_reset_pulse();
1469		gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 1);
1470	}
1471
1472	ret = cs35l56_get_speaker_id(&cs35l56->base);
1473	if (ACPI_COMPANION(cs35l56->base.dev) && cs35l56->sdw_peripheral && (ret == -ENOENT))
1474		ret = cs35l56_try_get_broken_sdca_spkid_gpio(cs35l56);
1475
1476	if ((ret < 0) && (ret != -ENOENT))
1477		goto err;
1478
1479	cs35l56->speaker_id = ret;
1480
1481	ret = cs35l56_get_firmware_uid(cs35l56);
1482	if (ret != 0)
1483		goto err;
1484
1485	ret = cs35l56_dsp_init(cs35l56);
1486	if (ret < 0) {
1487		dev_err_probe(cs35l56->base.dev, ret, "DSP init failed\n");
1488		goto err;
1489	}
1490
1491	ret = devm_snd_soc_register_component(cs35l56->base.dev,
1492					      &soc_component_dev_cs35l56,
1493					      cs35l56_dai, ARRAY_SIZE(cs35l56_dai));
1494	if (ret < 0) {
1495		dev_err_probe(cs35l56->base.dev, ret, "Register codec failed\n");
1496		goto err;
1497	}
1498
1499	return 0;
1500
1501err:
1502	gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 0);
1503	regulator_bulk_disable(ARRAY_SIZE(cs35l56->supplies), cs35l56->supplies);
1504
1505	return ret;
1506}
1507EXPORT_SYMBOL_NS_GPL(cs35l56_common_probe, SND_SOC_CS35L56_CORE);
1508
1509int cs35l56_init(struct cs35l56_private *cs35l56)
1510{
1511	int ret;
1512
1513	/*
1514	 * Check whether the actions associated with soft reset or one time
1515	 * init need to be performed.
1516	 */
1517	if (cs35l56->soft_resetting)
1518		goto post_soft_reset;
1519
1520	if (cs35l56->base.init_done)
1521		return 0;
1522
1523	pm_runtime_set_autosuspend_delay(cs35l56->base.dev, 100);
1524	pm_runtime_use_autosuspend(cs35l56->base.dev);
1525	pm_runtime_set_active(cs35l56->base.dev);
1526	pm_runtime_enable(cs35l56->base.dev);
1527
1528	ret = cs35l56_hw_init(&cs35l56->base);
1529	if (ret < 0)
1530		return ret;
1531
1532	ret = cs35l56_set_patch(&cs35l56->base);
1533	if (ret)
1534		return ret;
1535
1536	ret = cs35l56_get_calibration(&cs35l56->base);
1537	if (ret)
1538		return ret;
1539
1540	if (!cs35l56->base.reset_gpio) {
1541		dev_dbg(cs35l56->base.dev, "No reset gpio: using soft reset\n");
1542		cs35l56->soft_resetting = true;
1543		cs35l56_system_reset(&cs35l56->base, !!cs35l56->sdw_peripheral);
1544		if (cs35l56->sdw_peripheral) {
1545			/* Keep alive while we wait for re-enumeration */
1546			pm_runtime_get_noresume(cs35l56->base.dev);
1547			return 0;
1548		}
1549	}
1550
1551post_soft_reset:
1552	if (cs35l56->soft_resetting) {
1553		cs35l56->soft_resetting = false;
1554
1555		/* Done re-enumerating after one-time init so release the keep-alive */
1556		if (cs35l56->sdw_peripheral && !cs35l56->base.init_done)
1557			pm_runtime_put_noidle(cs35l56->base.dev);
1558
1559		regcache_mark_dirty(cs35l56->base.regmap);
1560		ret = cs35l56_wait_for_firmware_boot(&cs35l56->base);
1561		if (ret)
1562			return ret;
1563
1564		dev_dbg(cs35l56->base.dev, "Firmware rebooted after soft reset\n");
1565
1566		regcache_cache_only(cs35l56->base.regmap, false);
1567	}
1568
1569	/* Disable auto-hibernate so that runtime_pm has control */
1570	ret = cs35l56_mbox_send(&cs35l56->base, CS35L56_MBOX_CMD_PREVENT_AUTO_HIBERNATE);
1571	if (ret)
1572		return ret;
1573
1574	/* Registers could be dirty after soft reset or SoundWire enumeration */
1575	regcache_sync(cs35l56->base.regmap);
1576
1577	/* Set ASP1 DOUT to high-impedance when it is not transmitting audio data. */
1578	ret = regmap_set_bits(cs35l56->base.regmap, CS35L56_ASP1_CONTROL3,
1579			      CS35L56_ASP1_DOUT_HIZ_CTRL_MASK);
1580	if (ret)
1581		return dev_err_probe(cs35l56->base.dev, ret, "Failed to write ASP1_CONTROL3\n");
1582
1583	cs35l56->base.init_done = true;
1584	complete(&cs35l56->init_completion);
1585
1586	return 0;
1587}
1588EXPORT_SYMBOL_NS_GPL(cs35l56_init, SND_SOC_CS35L56_CORE);
1589
1590void cs35l56_remove(struct cs35l56_private *cs35l56)
1591{
1592	cs35l56->base.init_done = false;
1593
1594	/*
1595	 * WAKE IRQs unmask if CS35L56 hibernates so free the handler to
1596	 * prevent it racing with remove().
1597	 */
1598	if (cs35l56->base.irq)
1599		devm_free_irq(cs35l56->base.dev, cs35l56->base.irq, &cs35l56->base);
1600
1601	flush_workqueue(cs35l56->dsp_wq);
1602	destroy_workqueue(cs35l56->dsp_wq);
1603
1604	pm_runtime_dont_use_autosuspend(cs35l56->base.dev);
1605	pm_runtime_suspend(cs35l56->base.dev);
1606	pm_runtime_disable(cs35l56->base.dev);
1607
1608	regcache_cache_only(cs35l56->base.regmap, true);
1609
1610	gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 0);
1611	regulator_bulk_disable(ARRAY_SIZE(cs35l56->supplies), cs35l56->supplies);
1612}
1613EXPORT_SYMBOL_NS_GPL(cs35l56_remove, SND_SOC_CS35L56_CORE);
1614
1615#if IS_ENABLED(CONFIG_SND_SOC_CS35L56_I2C) || IS_ENABLED(CONFIG_SND_SOC_CS35L56_SPI)
1616EXPORT_NS_GPL_DEV_PM_OPS(cs35l56_pm_ops_i2c_spi, SND_SOC_CS35L56_CORE) = {
1617	SET_RUNTIME_PM_OPS(cs35l56_runtime_suspend_i2c_spi, cs35l56_runtime_resume_i2c_spi, NULL)
1618	SYSTEM_SLEEP_PM_OPS(cs35l56_system_suspend, cs35l56_system_resume)
1619	LATE_SYSTEM_SLEEP_PM_OPS(cs35l56_system_suspend_late, cs35l56_system_resume_early)
1620	NOIRQ_SYSTEM_SLEEP_PM_OPS(cs35l56_system_suspend_no_irq, cs35l56_system_resume_no_irq)
1621};
1622#endif
1623
1624MODULE_DESCRIPTION("ASoC CS35L56 driver");
1625MODULE_IMPORT_NS(SND_SOC_CS35L56_SHARED);
1626MODULE_IMPORT_NS(SND_SOC_CS_AMP_LIB);
1627MODULE_AUTHOR("Richard Fitzgerald <rf@opensource.cirrus.com>");
1628MODULE_AUTHOR("Simon Trimmer <simont@opensource.cirrus.com>");
1629MODULE_LICENSE("GPL");
1630