1 // SPDX-License-Identifier: GPL-2.0-only
3 // Driver for Cirrus Logic CS35L56 smart amp
5 // Copyright (C) 2023 Cirrus Logic, Inc. and
6 // Cirrus Logic International Semiconductor Ltd.
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>
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/pcm.h>
27 #include <sound/pcm_params.h>
28 #include <sound/soc.h>
29 #include <sound/soc-dapm.h>
30 #include <sound/tlv.h>
35 static int cs35l56_dsp_event(struct snd_soc_dapm_widget *w,
36 struct snd_kcontrol *kcontrol, int event);
38 static void cs35l56_wait_dsp_ready(struct cs35l56_private *cs35l56)
40 /* Wait for patching to complete */
41 flush_work(&cs35l56->dsp_work);
44 static int cs35l56_dspwait_get_volsw(struct snd_kcontrol *kcontrol,
45 struct snd_ctl_elem_value *ucontrol)
47 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
48 struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
50 cs35l56_wait_dsp_ready(cs35l56);
51 return snd_soc_get_volsw(kcontrol, ucontrol);
54 static int cs35l56_dspwait_put_volsw(struct snd_kcontrol *kcontrol,
55 struct snd_ctl_elem_value *ucontrol)
57 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
58 struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
60 cs35l56_wait_dsp_ready(cs35l56);
61 return snd_soc_put_volsw(kcontrol, ucontrol);
64 static const unsigned short cs35l56_asp1_mixer_regs[] = {
65 CS35L56_ASP1TX1_INPUT, CS35L56_ASP1TX2_INPUT,
66 CS35L56_ASP1TX3_INPUT, CS35L56_ASP1TX4_INPUT,
69 static const char * const cs35l56_asp1_mux_control_names[] = {
70 "ASP1 TX1 Source", "ASP1 TX2 Source", "ASP1 TX3 Source", "ASP1 TX4 Source"
73 static int cs35l56_sync_asp1_mixer_widgets_with_firmware(struct cs35l56_private *cs35l56)
75 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cs35l56->component);
76 const char *prefix = cs35l56->component->name_prefix;
77 char full_name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
79 struct snd_kcontrol *kcontrol;
84 if (cs35l56->asp1_mixer_widgets_initialized)
88 * Resume so we can read the registers from silicon if the regmap
89 * cache has not yet been populated.
91 ret = pm_runtime_resume_and_get(cs35l56->base.dev);
95 /* Wait for firmware download and reboot */
96 cs35l56_wait_dsp_ready(cs35l56);
98 ret = regmap_bulk_read(cs35l56->base.regmap, CS35L56_ASP1TX1_INPUT,
99 val, ARRAY_SIZE(val));
101 pm_runtime_mark_last_busy(cs35l56->base.dev);
102 pm_runtime_put_autosuspend(cs35l56->base.dev);
105 dev_err(cs35l56->base.dev, "Failed to read ASP1 mixer regs: %d\n", ret);
109 for (i = 0; i < ARRAY_SIZE(cs35l56_asp1_mux_control_names); ++i) {
110 name = cs35l56_asp1_mux_control_names[i];
113 snprintf(full_name, sizeof(full_name), "%s %s", prefix, name);
117 kcontrol = snd_soc_card_get_kcontrol_locked(dapm->card, name);
119 dev_warn(cs35l56->base.dev, "Could not find control %s\n", name);
123 e = (struct soc_enum *)kcontrol->private_value;
124 item = snd_soc_enum_val_to_item(e, val[i] & CS35L56_ASP_TXn_SRC_MASK);
125 snd_soc_dapm_mux_update_power(dapm, kcontrol, item, e, NULL);
128 cs35l56->asp1_mixer_widgets_initialized = true;
133 static int cs35l56_dspwait_asp1tx_get(struct snd_kcontrol *kcontrol,
134 struct snd_ctl_elem_value *ucontrol)
136 struct snd_soc_component *component = snd_soc_dapm_kcontrol_component(kcontrol);
137 struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
138 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
139 int index = e->shift_l;
140 unsigned int addr, val;
143 ret = cs35l56_sync_asp1_mixer_widgets_with_firmware(cs35l56);
147 addr = cs35l56_asp1_mixer_regs[index];
148 ret = regmap_read(cs35l56->base.regmap, addr, &val);
152 val &= CS35L56_ASP_TXn_SRC_MASK;
153 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
158 static int cs35l56_dspwait_asp1tx_put(struct snd_kcontrol *kcontrol,
159 struct snd_ctl_elem_value *ucontrol)
161 struct snd_soc_component *component = snd_soc_dapm_kcontrol_component(kcontrol);
162 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
163 struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
164 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
165 int item = ucontrol->value.enumerated.item[0];
166 int index = e->shift_l;
167 unsigned int addr, val;
171 ret = cs35l56_sync_asp1_mixer_widgets_with_firmware(cs35l56);
175 addr = cs35l56_asp1_mixer_regs[index];
176 val = snd_soc_enum_item_to_val(e, item);
178 ret = regmap_update_bits_check(cs35l56->base.regmap, addr,
179 CS35L56_ASP_TXn_SRC_MASK, val, &changed);
184 snd_soc_dapm_mux_update_power(dapm, kcontrol, item, e, NULL);
189 static DECLARE_TLV_DB_SCALE(vol_tlv, -10000, 25, 0);
191 static const struct snd_kcontrol_new cs35l56_controls[] = {
192 SOC_SINGLE_EXT("Speaker Switch",
193 CS35L56_MAIN_RENDER_USER_MUTE, 0, 1, 1,
194 cs35l56_dspwait_get_volsw, cs35l56_dspwait_put_volsw),
195 SOC_SINGLE_S_EXT_TLV("Speaker Volume",
196 CS35L56_MAIN_RENDER_USER_VOLUME,
198 cs35l56_dspwait_get_volsw,
199 cs35l56_dspwait_put_volsw,
201 SOC_SINGLE_EXT("Posture Number", CS35L56_MAIN_POSTURE_NUMBER,
203 cs35l56_dspwait_get_volsw, cs35l56_dspwait_put_volsw),
206 static SOC_VALUE_ENUM_SINGLE_DECL(cs35l56_asp1tx1_enum,
209 cs35l56_tx_input_texts,
210 cs35l56_tx_input_values);
212 static const struct snd_kcontrol_new asp1_tx1_mux =
213 SOC_DAPM_ENUM_EXT("ASP1TX1 SRC", cs35l56_asp1tx1_enum,
214 cs35l56_dspwait_asp1tx_get, cs35l56_dspwait_asp1tx_put);
216 static SOC_VALUE_ENUM_SINGLE_DECL(cs35l56_asp1tx2_enum,
219 cs35l56_tx_input_texts,
220 cs35l56_tx_input_values);
222 static const struct snd_kcontrol_new asp1_tx2_mux =
223 SOC_DAPM_ENUM_EXT("ASP1TX2 SRC", cs35l56_asp1tx2_enum,
224 cs35l56_dspwait_asp1tx_get, cs35l56_dspwait_asp1tx_put);
226 static SOC_VALUE_ENUM_SINGLE_DECL(cs35l56_asp1tx3_enum,
229 cs35l56_tx_input_texts,
230 cs35l56_tx_input_values);
232 static const struct snd_kcontrol_new asp1_tx3_mux =
233 SOC_DAPM_ENUM_EXT("ASP1TX3 SRC", cs35l56_asp1tx3_enum,
234 cs35l56_dspwait_asp1tx_get, cs35l56_dspwait_asp1tx_put);
236 static SOC_VALUE_ENUM_SINGLE_DECL(cs35l56_asp1tx4_enum,
239 cs35l56_tx_input_texts,
240 cs35l56_tx_input_values);
242 static const struct snd_kcontrol_new asp1_tx4_mux =
243 SOC_DAPM_ENUM_EXT("ASP1TX4 SRC", cs35l56_asp1tx4_enum,
244 cs35l56_dspwait_asp1tx_get, cs35l56_dspwait_asp1tx_put);
246 static SOC_VALUE_ENUM_SINGLE_DECL(cs35l56_sdw1tx1_enum,
247 CS35L56_SWIRE_DP3_CH1_INPUT,
248 0, CS35L56_SWIRETXn_SRC_MASK,
249 cs35l56_tx_input_texts,
250 cs35l56_tx_input_values);
252 static const struct snd_kcontrol_new sdw1_tx1_mux =
253 SOC_DAPM_ENUM("SDW1TX1 SRC", cs35l56_sdw1tx1_enum);
255 static SOC_VALUE_ENUM_SINGLE_DECL(cs35l56_sdw1tx2_enum,
256 CS35L56_SWIRE_DP3_CH2_INPUT,
257 0, CS35L56_SWIRETXn_SRC_MASK,
258 cs35l56_tx_input_texts,
259 cs35l56_tx_input_values);
261 static const struct snd_kcontrol_new sdw1_tx2_mux =
262 SOC_DAPM_ENUM("SDW1TX2 SRC", cs35l56_sdw1tx2_enum);
264 static SOC_VALUE_ENUM_SINGLE_DECL(cs35l56_sdw1tx3_enum,
265 CS35L56_SWIRE_DP3_CH3_INPUT,
266 0, CS35L56_SWIRETXn_SRC_MASK,
267 cs35l56_tx_input_texts,
268 cs35l56_tx_input_values);
270 static const struct snd_kcontrol_new sdw1_tx3_mux =
271 SOC_DAPM_ENUM("SDW1TX3 SRC", cs35l56_sdw1tx3_enum);
273 static SOC_VALUE_ENUM_SINGLE_DECL(cs35l56_sdw1tx4_enum,
274 CS35L56_SWIRE_DP3_CH4_INPUT,
275 0, CS35L56_SWIRETXn_SRC_MASK,
276 cs35l56_tx_input_texts,
277 cs35l56_tx_input_values);
279 static const struct snd_kcontrol_new sdw1_tx4_mux =
280 SOC_DAPM_ENUM("SDW1TX4 SRC", cs35l56_sdw1tx4_enum);
282 static int cs35l56_asp1_cfg_event(struct snd_soc_dapm_widget *w,
283 struct snd_kcontrol *kcontrol, int event)
285 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
286 struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
289 case SND_SOC_DAPM_PRE_PMU:
290 /* Override register values set by firmware boot */
291 return cs35l56_force_sync_asp1_registers_from_cache(&cs35l56->base);
297 static int cs35l56_play_event(struct snd_soc_dapm_widget *w,
298 struct snd_kcontrol *kcontrol, int event)
300 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
301 struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
305 dev_dbg(cs35l56->base.dev, "play: %d\n", event);
308 case SND_SOC_DAPM_PRE_PMU:
309 /* Don't wait for ACK, we check in POST_PMU that it completed */
310 return regmap_write(cs35l56->base.regmap, CS35L56_DSP_VIRTUAL1_MBOX_1,
311 CS35L56_MBOX_CMD_AUDIO_PLAY);
312 case SND_SOC_DAPM_POST_PMU:
313 /* Wait for firmware to enter PS0 power state */
314 ret = regmap_read_poll_timeout(cs35l56->base.regmap,
315 CS35L56_TRANSDUCER_ACTUAL_PS,
316 val, (val == CS35L56_PS0),
318 CS35L56_PS0_TIMEOUT_US);
320 dev_err(cs35l56->base.dev, "PS0 wait failed: %d\n", ret);
322 case SND_SOC_DAPM_POST_PMD:
323 return cs35l56_mbox_send(&cs35l56->base, CS35L56_MBOX_CMD_AUDIO_PAUSE);
329 static const struct snd_soc_dapm_widget cs35l56_dapm_widgets[] = {
330 SND_SOC_DAPM_REGULATOR_SUPPLY("VDD_B", 0, 0),
331 SND_SOC_DAPM_REGULATOR_SUPPLY("VDD_AMP", 0, 0),
333 SND_SOC_DAPM_SUPPLY("ASP1 CFG", SND_SOC_NOPM, 0, 0, cs35l56_asp1_cfg_event,
334 SND_SOC_DAPM_PRE_PMU),
336 SND_SOC_DAPM_SUPPLY("PLAY", SND_SOC_NOPM, 0, 0, cs35l56_play_event,
337 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
339 SND_SOC_DAPM_OUT_DRV("AMP", SND_SOC_NOPM, 0, 0, NULL, 0),
340 SND_SOC_DAPM_OUTPUT("SPK"),
342 SND_SOC_DAPM_PGA_E("DSP1", SND_SOC_NOPM, 0, 0, NULL, 0, cs35l56_dsp_event,
343 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
345 SND_SOC_DAPM_AIF_IN("ASP1RX1", NULL, 0, CS35L56_ASP1_ENABLES1,
346 CS35L56_ASP_RX1_EN_SHIFT, 0),
347 SND_SOC_DAPM_AIF_IN("ASP1RX2", NULL, 1, CS35L56_ASP1_ENABLES1,
348 CS35L56_ASP_RX2_EN_SHIFT, 0),
349 SND_SOC_DAPM_AIF_OUT("ASP1TX1", NULL, 0, CS35L56_ASP1_ENABLES1,
350 CS35L56_ASP_TX1_EN_SHIFT, 0),
351 SND_SOC_DAPM_AIF_OUT("ASP1TX2", NULL, 1, CS35L56_ASP1_ENABLES1,
352 CS35L56_ASP_TX2_EN_SHIFT, 0),
353 SND_SOC_DAPM_AIF_OUT("ASP1TX3", NULL, 2, CS35L56_ASP1_ENABLES1,
354 CS35L56_ASP_TX3_EN_SHIFT, 0),
355 SND_SOC_DAPM_AIF_OUT("ASP1TX4", NULL, 3, CS35L56_ASP1_ENABLES1,
356 CS35L56_ASP_TX4_EN_SHIFT, 0),
358 SND_SOC_DAPM_MUX("ASP1 TX1 Source", SND_SOC_NOPM, 0, 0, &asp1_tx1_mux),
359 SND_SOC_DAPM_MUX("ASP1 TX2 Source", SND_SOC_NOPM, 0, 0, &asp1_tx2_mux),
360 SND_SOC_DAPM_MUX("ASP1 TX3 Source", SND_SOC_NOPM, 0, 0, &asp1_tx3_mux),
361 SND_SOC_DAPM_MUX("ASP1 TX4 Source", SND_SOC_NOPM, 0, 0, &asp1_tx4_mux),
363 SND_SOC_DAPM_MUX("SDW1 TX1 Source", SND_SOC_NOPM, 0, 0, &sdw1_tx1_mux),
364 SND_SOC_DAPM_MUX("SDW1 TX2 Source", SND_SOC_NOPM, 0, 0, &sdw1_tx2_mux),
365 SND_SOC_DAPM_MUX("SDW1 TX3 Source", SND_SOC_NOPM, 0, 0, &sdw1_tx3_mux),
366 SND_SOC_DAPM_MUX("SDW1 TX4 Source", SND_SOC_NOPM, 0, 0, &sdw1_tx4_mux),
368 SND_SOC_DAPM_SIGGEN("VMON ADC"),
369 SND_SOC_DAPM_SIGGEN("IMON ADC"),
370 SND_SOC_DAPM_SIGGEN("ERRVOL ADC"),
371 SND_SOC_DAPM_SIGGEN("CLASSH ADC"),
372 SND_SOC_DAPM_SIGGEN("VDDBMON ADC"),
373 SND_SOC_DAPM_SIGGEN("VBSTMON ADC"),
374 SND_SOC_DAPM_SIGGEN("TEMPMON ADC"),
377 #define CS35L56_SRC_ROUTE(name) \
378 { name" Source", "ASP1RX1", "ASP1RX1" }, \
379 { name" Source", "ASP1RX2", "ASP1RX2" }, \
380 { name" Source", "VMON", "VMON ADC" }, \
381 { name" Source", "IMON", "IMON ADC" }, \
382 { name" Source", "ERRVOL", "ERRVOL ADC" }, \
383 { name" Source", "CLASSH", "CLASSH ADC" }, \
384 { name" Source", "VDDBMON", "VDDBMON ADC" }, \
385 { name" Source", "VBSTMON", "VBSTMON ADC" }, \
386 { name" Source", "DSP1TX1", "DSP1" }, \
387 { name" Source", "DSP1TX2", "DSP1" }, \
388 { name" Source", "DSP1TX3", "DSP1" }, \
389 { name" Source", "DSP1TX4", "DSP1" }, \
390 { name" Source", "DSP1TX5", "DSP1" }, \
391 { name" Source", "DSP1TX6", "DSP1" }, \
392 { name" Source", "DSP1TX7", "DSP1" }, \
393 { name" Source", "DSP1TX8", "DSP1" }, \
394 { name" Source", "TEMPMON", "TEMPMON ADC" }, \
395 { name" Source", "INTERPOLATOR", "AMP" }, \
396 { name" Source", "SDW1RX1", "SDW1 Playback" }, \
397 { name" Source", "SDW1RX2", "SDW1 Playback" },
399 static const struct snd_soc_dapm_route cs35l56_audio_map[] = {
400 { "AMP", NULL, "VDD_B" },
401 { "AMP", NULL, "VDD_AMP" },
403 { "ASP1 Playback", NULL, "ASP1 CFG" },
404 { "ASP1 Capture", NULL, "ASP1 CFG" },
406 { "ASP1 Playback", NULL, "PLAY" },
407 { "SDW1 Playback", NULL, "PLAY" },
409 { "ASP1RX1", NULL, "ASP1 Playback" },
410 { "ASP1RX2", NULL, "ASP1 Playback" },
411 { "DSP1", NULL, "ASP1RX1" },
412 { "DSP1", NULL, "ASP1RX2" },
413 { "DSP1", NULL, "SDW1 Playback" },
414 { "AMP", NULL, "DSP1" },
415 { "SPK", NULL, "AMP" },
417 CS35L56_SRC_ROUTE("ASP1 TX1")
418 CS35L56_SRC_ROUTE("ASP1 TX2")
419 CS35L56_SRC_ROUTE("ASP1 TX3")
420 CS35L56_SRC_ROUTE("ASP1 TX4")
422 { "ASP1TX1", NULL, "ASP1 TX1 Source" },
423 { "ASP1TX2", NULL, "ASP1 TX2 Source" },
424 { "ASP1TX3", NULL, "ASP1 TX3 Source" },
425 { "ASP1TX4", NULL, "ASP1 TX4 Source" },
426 { "ASP1 Capture", NULL, "ASP1TX1" },
427 { "ASP1 Capture", NULL, "ASP1TX2" },
428 { "ASP1 Capture", NULL, "ASP1TX3" },
429 { "ASP1 Capture", NULL, "ASP1TX4" },
431 CS35L56_SRC_ROUTE("SDW1 TX1")
432 CS35L56_SRC_ROUTE("SDW1 TX2")
433 CS35L56_SRC_ROUTE("SDW1 TX3")
434 CS35L56_SRC_ROUTE("SDW1 TX4")
435 { "SDW1 Capture", NULL, "SDW1 TX1 Source" },
436 { "SDW1 Capture", NULL, "SDW1 TX2 Source" },
437 { "SDW1 Capture", NULL, "SDW1 TX3 Source" },
438 { "SDW1 Capture", NULL, "SDW1 TX4 Source" },
441 static int cs35l56_dsp_event(struct snd_soc_dapm_widget *w,
442 struct snd_kcontrol *kcontrol, int event)
444 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
445 struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
447 dev_dbg(cs35l56->base.dev, "%s: %d\n", __func__, event);
449 return wm_adsp_event(w, kcontrol, event);
452 static int cs35l56_asp_dai_set_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
454 struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(codec_dai->component);
457 dev_dbg(cs35l56->base.dev, "%s: %#x\n", __func__, fmt);
459 switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
460 case SND_SOC_DAIFMT_CBC_CFC:
463 dev_err(cs35l56->base.dev, "Unsupported clock source mode\n");
467 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
468 case SND_SOC_DAIFMT_DSP_A:
469 val = CS35L56_ASP_FMT_DSP_A << CS35L56_ASP_FMT_SHIFT;
470 cs35l56->tdm_mode = true;
472 case SND_SOC_DAIFMT_I2S:
473 val = CS35L56_ASP_FMT_I2S << CS35L56_ASP_FMT_SHIFT;
474 cs35l56->tdm_mode = false;
477 dev_err(cs35l56->base.dev, "Unsupported DAI format\n");
481 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
482 case SND_SOC_DAIFMT_NB_IF:
483 val |= CS35L56_ASP_FSYNC_INV_MASK;
485 case SND_SOC_DAIFMT_IB_NF:
486 val |= CS35L56_ASP_BCLK_INV_MASK;
488 case SND_SOC_DAIFMT_IB_IF:
489 val |= CS35L56_ASP_BCLK_INV_MASK | CS35L56_ASP_FSYNC_INV_MASK;
491 case SND_SOC_DAIFMT_NB_NF:
494 dev_err(cs35l56->base.dev, "Invalid clock invert\n");
498 regmap_update_bits(cs35l56->base.regmap,
499 CS35L56_ASP1_CONTROL2,
500 CS35L56_ASP_FMT_MASK |
501 CS35L56_ASP_BCLK_INV_MASK | CS35L56_ASP_FSYNC_INV_MASK,
504 /* Hi-Z DOUT in unused slots and when all TX are disabled */
505 regmap_update_bits(cs35l56->base.regmap, CS35L56_ASP1_CONTROL3,
506 CS35L56_ASP1_DOUT_HIZ_CTRL_MASK,
507 CS35L56_ASP_UNUSED_HIZ_OFF_HIZ);
512 static unsigned int cs35l56_make_tdm_config_word(unsigned int reg_val, unsigned long mask)
514 unsigned int channel_shift;
517 /* Enable consecutive TX1..TXn for each of the slots set in mask */
519 for_each_set_bit(bit_num, &mask, 32) {
520 reg_val &= ~(0x3f << channel_shift);
521 reg_val |= bit_num << channel_shift;
528 static int cs35l56_asp_dai_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
529 unsigned int rx_mask, int slots, int slot_width)
531 struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(dai->component);
533 if ((slots == 0) || (slot_width == 0)) {
534 dev_dbg(cs35l56->base.dev, "tdm config cleared\n");
535 cs35l56->asp_slot_width = 0;
536 cs35l56->asp_slot_count = 0;
540 if (slot_width > (CS35L56_ASP_RX_WIDTH_MASK >> CS35L56_ASP_RX_WIDTH_SHIFT)) {
541 dev_err(cs35l56->base.dev, "tdm invalid slot width %d\n", slot_width);
545 /* More than 32 slots would give an unsupportable BCLK frequency */
547 dev_err(cs35l56->base.dev, "tdm invalid slot count %d\n", slots);
551 cs35l56->asp_slot_width = (u8)slot_width;
552 cs35l56->asp_slot_count = (u8)slots;
554 // Note: rx/tx is from point of view of the CPU end
556 tx_mask = 0x3; // ASPRX1/RX2 in slots 0 and 1
559 rx_mask = 0xf; // ASPTX1..TX4 in slots 0..3
561 /* Default unused slots to 63 */
562 regmap_write(cs35l56->base.regmap, CS35L56_ASP1_FRAME_CONTROL1,
563 cs35l56_make_tdm_config_word(0x3f3f3f3f, rx_mask));
564 regmap_write(cs35l56->base.regmap, CS35L56_ASP1_FRAME_CONTROL5,
565 cs35l56_make_tdm_config_word(0x3f3f3f, tx_mask));
567 dev_dbg(cs35l56->base.dev, "tdm slot width: %u count: %u tx_mask: %#x rx_mask: %#x\n",
568 cs35l56->asp_slot_width, cs35l56->asp_slot_count, tx_mask, rx_mask);
573 static int cs35l56_asp_dai_hw_params(struct snd_pcm_substream *substream,
574 struct snd_pcm_hw_params *params,
575 struct snd_soc_dai *dai)
577 struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(dai->component);
578 unsigned int rate = params_rate(params);
579 u8 asp_width, asp_wl;
581 asp_wl = params_width(params);
582 if (cs35l56->asp_slot_width)
583 asp_width = cs35l56->asp_slot_width;
587 dev_dbg(cs35l56->base.dev, "%s: wl=%d, width=%d, rate=%d",
588 __func__, asp_wl, asp_width, rate);
590 if (!cs35l56->sysclk_set) {
591 unsigned int slots = cs35l56->asp_slot_count;
592 unsigned int bclk_freq;
596 slots = params_channels(params);
598 /* I2S always has an even number of slots */
599 if (!cs35l56->tdm_mode)
600 slots = round_up(slots, 2);
603 bclk_freq = asp_width * slots * rate;
604 freq_id = cs35l56_get_bclk_freq_id(bclk_freq);
606 dev_err(cs35l56->base.dev, "%s: Invalid BCLK %u\n", __func__, bclk_freq);
610 regmap_update_bits(cs35l56->base.regmap, CS35L56_ASP1_CONTROL1,
611 CS35L56_ASP_BCLK_FREQ_MASK,
612 freq_id << CS35L56_ASP_BCLK_FREQ_SHIFT);
615 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
616 regmap_update_bits(cs35l56->base.regmap, CS35L56_ASP1_CONTROL2,
617 CS35L56_ASP_RX_WIDTH_MASK, asp_width <<
618 CS35L56_ASP_RX_WIDTH_SHIFT);
619 regmap_update_bits(cs35l56->base.regmap, CS35L56_ASP1_DATA_CONTROL5,
620 CS35L56_ASP_RX_WL_MASK, asp_wl);
622 regmap_update_bits(cs35l56->base.regmap, CS35L56_ASP1_CONTROL2,
623 CS35L56_ASP_TX_WIDTH_MASK, asp_width <<
624 CS35L56_ASP_TX_WIDTH_SHIFT);
625 regmap_update_bits(cs35l56->base.regmap, CS35L56_ASP1_DATA_CONTROL1,
626 CS35L56_ASP_TX_WL_MASK, asp_wl);
632 static int cs35l56_asp_dai_set_sysclk(struct snd_soc_dai *dai,
633 int clk_id, unsigned int freq, int dir)
635 struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(dai->component);
639 cs35l56->sysclk_set = false;
643 freq_id = cs35l56_get_bclk_freq_id(freq);
647 regmap_update_bits(cs35l56->base.regmap, CS35L56_ASP1_CONTROL1,
648 CS35L56_ASP_BCLK_FREQ_MASK,
649 freq_id << CS35L56_ASP_BCLK_FREQ_SHIFT);
650 cs35l56->sysclk_set = true;
655 static const struct snd_soc_dai_ops cs35l56_ops = {
656 .set_fmt = cs35l56_asp_dai_set_fmt,
657 .set_tdm_slot = cs35l56_asp_dai_set_tdm_slot,
658 .hw_params = cs35l56_asp_dai_hw_params,
659 .set_sysclk = cs35l56_asp_dai_set_sysclk,
662 static void cs35l56_sdw_dai_shutdown(struct snd_pcm_substream *substream,
663 struct snd_soc_dai *dai)
665 snd_soc_dai_set_dma_data(dai, substream, NULL);
668 static int cs35l56_sdw_dai_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
669 unsigned int rx_mask, int slots, int slot_width)
671 struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(dai->component);
673 /* rx/tx are from point of view of the CPU end so opposite to our rx/tx */
674 cs35l56->rx_mask = tx_mask;
675 cs35l56->tx_mask = rx_mask;
680 static int cs35l56_sdw_dai_hw_params(struct snd_pcm_substream *substream,
681 struct snd_pcm_hw_params *params,
682 struct snd_soc_dai *dai)
684 struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(dai->component);
685 struct sdw_stream_runtime *sdw_stream = snd_soc_dai_get_dma_data(dai, substream);
686 struct sdw_stream_config sconfig;
687 struct sdw_port_config pconfig;
690 dev_dbg(cs35l56->base.dev, "%s: rate %d\n", __func__, params_rate(params));
692 if (!cs35l56->base.init_done)
698 memset(&sconfig, 0, sizeof(sconfig));
699 memset(&pconfig, 0, sizeof(pconfig));
701 sconfig.frame_rate = params_rate(params);
702 sconfig.bps = snd_pcm_format_width(params_format(params));
704 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
705 sconfig.direction = SDW_DATA_DIR_RX;
706 pconfig.num = CS35L56_SDW1_PLAYBACK_PORT;
707 pconfig.ch_mask = cs35l56->rx_mask;
709 sconfig.direction = SDW_DATA_DIR_TX;
710 pconfig.num = CS35L56_SDW1_CAPTURE_PORT;
711 pconfig.ch_mask = cs35l56->tx_mask;
714 if (pconfig.ch_mask == 0) {
715 sconfig.ch_count = params_channels(params);
716 pconfig.ch_mask = GENMASK(sconfig.ch_count - 1, 0);
718 sconfig.ch_count = hweight32(pconfig.ch_mask);
721 ret = sdw_stream_add_slave(cs35l56->sdw_peripheral, &sconfig, &pconfig,
724 dev_err(dai->dev, "Failed to add sdw stream: %d\n", ret);
731 static int cs35l56_sdw_dai_hw_free(struct snd_pcm_substream *substream,
732 struct snd_soc_dai *dai)
734 struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(dai->component);
735 struct sdw_stream_runtime *sdw_stream = snd_soc_dai_get_dma_data(dai, substream);
737 if (!cs35l56->sdw_peripheral)
740 sdw_stream_remove_slave(cs35l56->sdw_peripheral, sdw_stream);
745 static int cs35l56_sdw_dai_set_stream(struct snd_soc_dai *dai,
746 void *sdw_stream, int direction)
748 snd_soc_dai_dma_data_set(dai, direction, sdw_stream);
753 static const struct snd_soc_dai_ops cs35l56_sdw_dai_ops = {
754 .set_tdm_slot = cs35l56_sdw_dai_set_tdm_slot,
755 .shutdown = cs35l56_sdw_dai_shutdown,
756 .hw_params = cs35l56_sdw_dai_hw_params,
757 .hw_free = cs35l56_sdw_dai_hw_free,
758 .set_stream = cs35l56_sdw_dai_set_stream,
761 static struct snd_soc_dai_driver cs35l56_dai[] = {
763 .name = "cs35l56-asp1",
766 .stream_name = "ASP1 Playback",
769 .rates = CS35L56_RATES,
770 .formats = CS35L56_RX_FORMATS,
773 .stream_name = "ASP1 Capture",
776 .rates = CS35L56_RATES,
777 .formats = CS35L56_TX_FORMATS,
781 .symmetric_sample_bits = 1,
784 .name = "cs35l56-sdw1",
787 .stream_name = "SDW1 Playback",
790 .rates = CS35L56_RATES,
791 .formats = CS35L56_RX_FORMATS,
794 .stream_name = "SDW1 Capture",
797 .rates = CS35L56_RATES,
798 .formats = CS35L56_TX_FORMATS,
801 .ops = &cs35l56_sdw_dai_ops,
805 static void cs35l56_reinit_patch(struct cs35l56_private *cs35l56)
809 /* Use wm_adsp to load and apply the firmware patch and coefficient files */
810 ret = wm_adsp_power_up(&cs35l56->dsp, true);
812 dev_dbg(cs35l56->base.dev, "%s: wm_adsp_power_up ret %d\n", __func__, ret);
814 cs35l56_mbox_send(&cs35l56->base, CS35L56_MBOX_CMD_AUDIO_REINIT);
817 static void cs35l56_patch(struct cs35l56_private *cs35l56, bool firmware_missing)
822 * Disable SoundWire interrupts to prevent race with IRQ work.
823 * Setting sdw_irq_no_unmask prevents the handler re-enabling
824 * the SoundWire interrupt.
826 if (cs35l56->sdw_peripheral) {
827 cs35l56->sdw_irq_no_unmask = true;
828 flush_work(&cs35l56->sdw_irq_work);
829 sdw_write_no_pm(cs35l56->sdw_peripheral, CS35L56_SDW_GEN_INT_MASK_1, 0);
830 sdw_read_no_pm(cs35l56->sdw_peripheral, CS35L56_SDW_GEN_INT_STAT_1);
831 sdw_write_no_pm(cs35l56->sdw_peripheral, CS35L56_SDW_GEN_INT_STAT_1, 0xFF);
832 flush_work(&cs35l56->sdw_irq_work);
835 ret = cs35l56_firmware_shutdown(&cs35l56->base);
840 * Use wm_adsp to load and apply the firmware patch and coefficient files,
841 * but only if firmware is missing. If firmware is already patched just
842 * power-up wm_adsp without downloading firmware.
844 ret = wm_adsp_power_up(&cs35l56->dsp, !!firmware_missing);
846 dev_dbg(cs35l56->base.dev, "%s: wm_adsp_power_up ret %d\n", __func__, ret);
850 mutex_lock(&cs35l56->base.irq_lock);
852 reinit_completion(&cs35l56->init_completion);
854 cs35l56->soft_resetting = true;
855 cs35l56_system_reset(&cs35l56->base, !!cs35l56->sdw_peripheral);
857 if (cs35l56->sdw_peripheral) {
859 * The system-reset causes the CS35L56 to detach from the bus.
860 * Wait for the manager to re-enumerate the CS35L56 and
861 * cs35l56_init() to run again.
863 if (!wait_for_completion_timeout(&cs35l56->init_completion,
864 msecs_to_jiffies(5000))) {
865 dev_err(cs35l56->base.dev, "%s: init_completion timed out (SDW)\n",
869 } else if (cs35l56_init(cs35l56)) {
873 regmap_clear_bits(cs35l56->base.regmap, CS35L56_PROTECTION_STATUS,
874 CS35L56_FIRMWARE_MISSING);
875 cs35l56->base.fw_patched = true;
878 mutex_unlock(&cs35l56->base.irq_lock);
880 /* Re-enable SoundWire interrupts */
881 if (cs35l56->sdw_peripheral) {
882 cs35l56->sdw_irq_no_unmask = false;
883 sdw_write_no_pm(cs35l56->sdw_peripheral, CS35L56_SDW_GEN_INT_MASK_1,
884 CS35L56_SDW_INT_MASK_CODEC_IRQ);
888 static void cs35l56_dsp_work(struct work_struct *work)
890 struct cs35l56_private *cs35l56 = container_of(work,
891 struct cs35l56_private,
893 unsigned int firmware_version;
894 bool firmware_missing;
897 if (!cs35l56->base.init_done)
900 pm_runtime_get_sync(cs35l56->base.dev);
902 ret = cs35l56_read_prot_status(&cs35l56->base, &firmware_missing, &firmware_version);
906 /* Populate fw file qualifier with the revision and security state */
907 kfree(cs35l56->dsp.fwf_name);
908 if (firmware_missing) {
909 cs35l56->dsp.fwf_name = kasprintf(GFP_KERNEL, "%02x-dsp1", cs35l56->base.rev);
911 /* Firmware files must match the running firmware version */
912 cs35l56->dsp.fwf_name = kasprintf(GFP_KERNEL,
915 cs35l56->base.secured ? "-s" : "",
919 if (!cs35l56->dsp.fwf_name)
922 dev_dbg(cs35l56->base.dev, "DSP fwf name: '%s' system name: '%s'\n",
923 cs35l56->dsp.fwf_name, cs35l56->dsp.system_name);
926 * The firmware cannot be patched if it is already running from
927 * patch RAM. In this case the firmware files are versioned to
928 * match the running firmware version and will only contain
929 * tunings. We do not need to shutdown the firmware to apply
930 * tunings so can use the lower cost reinit sequence instead.
932 if (!firmware_missing)
933 cs35l56_reinit_patch(cs35l56);
935 cs35l56_patch(cs35l56, firmware_missing);
938 pm_runtime_mark_last_busy(cs35l56->base.dev);
939 pm_runtime_put_autosuspend(cs35l56->base.dev);
942 static int cs35l56_component_probe(struct snd_soc_component *component)
944 struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
945 struct dentry *debugfs_root = component->debugfs_root;
946 unsigned short vendor, device;
948 BUILD_BUG_ON(ARRAY_SIZE(cs35l56_tx_input_texts) != ARRAY_SIZE(cs35l56_tx_input_values));
950 if (!cs35l56->dsp.system_name &&
951 (snd_soc_card_get_pci_ssid(component->card, &vendor, &device) == 0)) {
952 /* Append a speaker qualifier if there is a speaker ID */
953 if (cs35l56->speaker_id >= 0) {
954 cs35l56->dsp.system_name = devm_kasprintf(cs35l56->base.dev,
958 cs35l56->speaker_id);
960 cs35l56->dsp.system_name = devm_kasprintf(cs35l56->base.dev,
965 if (!cs35l56->dsp.system_name)
969 if (!wait_for_completion_timeout(&cs35l56->init_completion,
970 msecs_to_jiffies(5000))) {
971 dev_err(cs35l56->base.dev, "%s: init_completion timed out\n", __func__);
975 cs35l56->component = component;
976 wm_adsp2_component_probe(&cs35l56->dsp, component);
978 debugfs_create_bool("init_done", 0444, debugfs_root, &cs35l56->base.init_done);
979 debugfs_create_bool("can_hibernate", 0444, debugfs_root, &cs35l56->base.can_hibernate);
980 debugfs_create_bool("fw_patched", 0444, debugfs_root, &cs35l56->base.fw_patched);
983 * The widgets for the ASP1TX mixer can't be initialized
984 * until the firmware has been downloaded and rebooted.
986 regcache_drop_region(cs35l56->base.regmap, CS35L56_ASP1TX1_INPUT, CS35L56_ASP1TX4_INPUT);
987 cs35l56->asp1_mixer_widgets_initialized = false;
989 queue_work(cs35l56->dsp_wq, &cs35l56->dsp_work);
994 static void cs35l56_component_remove(struct snd_soc_component *component)
996 struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
998 cancel_work_sync(&cs35l56->dsp_work);
1000 if (cs35l56->dsp.cs_dsp.booted)
1001 wm_adsp_power_down(&cs35l56->dsp);
1003 wm_adsp2_component_remove(&cs35l56->dsp, component);
1005 kfree(cs35l56->dsp.fwf_name);
1006 cs35l56->dsp.fwf_name = NULL;
1008 cs35l56->component = NULL;
1011 static int cs35l56_set_bias_level(struct snd_soc_component *component,
1012 enum snd_soc_bias_level level)
1014 struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
1017 case SND_SOC_BIAS_STANDBY:
1019 * Wait for patching to complete when transitioning from
1020 * BIAS_OFF to BIAS_STANDBY
1022 if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF)
1023 cs35l56_wait_dsp_ready(cs35l56);
1033 static const struct snd_soc_component_driver soc_component_dev_cs35l56 = {
1034 .probe = cs35l56_component_probe,
1035 .remove = cs35l56_component_remove,
1037 .dapm_widgets = cs35l56_dapm_widgets,
1038 .num_dapm_widgets = ARRAY_SIZE(cs35l56_dapm_widgets),
1039 .dapm_routes = cs35l56_audio_map,
1040 .num_dapm_routes = ARRAY_SIZE(cs35l56_audio_map),
1041 .controls = cs35l56_controls,
1042 .num_controls = ARRAY_SIZE(cs35l56_controls),
1044 .set_bias_level = cs35l56_set_bias_level,
1046 .suspend_bias_off = 1, /* see cs35l56_system_resume() */
1049 static int __maybe_unused cs35l56_runtime_suspend_i2c_spi(struct device *dev)
1051 struct cs35l56_private *cs35l56 = dev_get_drvdata(dev);
1053 return cs35l56_runtime_suspend_common(&cs35l56->base);
1056 static int __maybe_unused cs35l56_runtime_resume_i2c_spi(struct device *dev)
1058 struct cs35l56_private *cs35l56 = dev_get_drvdata(dev);
1060 return cs35l56_runtime_resume_common(&cs35l56->base, false);
1063 int cs35l56_system_suspend(struct device *dev)
1065 struct cs35l56_private *cs35l56 = dev_get_drvdata(dev);
1067 dev_dbg(dev, "system_suspend\n");
1069 if (cs35l56->component)
1070 flush_work(&cs35l56->dsp_work);
1073 * The interrupt line is normally shared, but after we start suspending
1074 * we can't check if our device is the source of an interrupt, and can't
1075 * clear it. Prevent this race by temporarily disabling the parent irq
1076 * until we reach _no_irq.
1078 if (cs35l56->base.irq)
1079 disable_irq(cs35l56->base.irq);
1081 return pm_runtime_force_suspend(dev);
1083 EXPORT_SYMBOL_GPL(cs35l56_system_suspend);
1085 int cs35l56_system_suspend_late(struct device *dev)
1087 struct cs35l56_private *cs35l56 = dev_get_drvdata(dev);
1089 dev_dbg(dev, "system_suspend_late\n");
1092 * Assert RESET before removing supplies.
1093 * RESET is usually shared by all amps so it must not be asserted until
1094 * all driver instances have done their suspend() stage.
1096 if (cs35l56->base.reset_gpio) {
1097 gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 0);
1098 cs35l56_wait_min_reset_pulse();
1101 regulator_bulk_disable(ARRAY_SIZE(cs35l56->supplies), cs35l56->supplies);
1105 EXPORT_SYMBOL_GPL(cs35l56_system_suspend_late);
1107 int cs35l56_system_suspend_no_irq(struct device *dev)
1109 struct cs35l56_private *cs35l56 = dev_get_drvdata(dev);
1111 dev_dbg(dev, "system_suspend_no_irq\n");
1113 /* Handlers are now disabled so the parent IRQ can safely be re-enabled. */
1114 if (cs35l56->base.irq)
1115 enable_irq(cs35l56->base.irq);
1119 EXPORT_SYMBOL_GPL(cs35l56_system_suspend_no_irq);
1121 int cs35l56_system_resume_no_irq(struct device *dev)
1123 struct cs35l56_private *cs35l56 = dev_get_drvdata(dev);
1125 dev_dbg(dev, "system_resume_no_irq\n");
1128 * WAKE interrupts unmask if the CS35L56 hibernates, which can cause
1129 * spurious interrupts, and the interrupt line is normally shared.
1130 * We can't check if our device is the source of an interrupt, and can't
1131 * clear it, until it has fully resumed. Prevent this race by temporarily
1132 * disabling the parent irq until we complete resume().
1134 if (cs35l56->base.irq)
1135 disable_irq(cs35l56->base.irq);
1139 EXPORT_SYMBOL_GPL(cs35l56_system_resume_no_irq);
1141 int cs35l56_system_resume_early(struct device *dev)
1143 struct cs35l56_private *cs35l56 = dev_get_drvdata(dev);
1146 dev_dbg(dev, "system_resume_early\n");
1148 /* Ensure a spec-compliant RESET pulse. */
1149 if (cs35l56->base.reset_gpio) {
1150 gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 0);
1151 cs35l56_wait_min_reset_pulse();
1154 /* Enable supplies before releasing RESET. */
1155 ret = regulator_bulk_enable(ARRAY_SIZE(cs35l56->supplies), cs35l56->supplies);
1157 dev_err(dev, "system_resume_early failed to enable supplies: %d\n", ret);
1161 /* Release shared RESET before drivers start resume(). */
1162 gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 1);
1166 EXPORT_SYMBOL_GPL(cs35l56_system_resume_early);
1168 int cs35l56_system_resume(struct device *dev)
1170 struct cs35l56_private *cs35l56 = dev_get_drvdata(dev);
1173 dev_dbg(dev, "system_resume\n");
1176 * We might have done a hard reset or the CS35L56 was power-cycled
1177 * so wait for control port to be ready.
1179 cs35l56_wait_control_port_ready();
1181 /* Undo pm_runtime_force_suspend() before re-enabling the irq */
1182 ret = pm_runtime_force_resume(dev);
1183 if (cs35l56->base.irq)
1184 enable_irq(cs35l56->base.irq);
1189 /* Firmware won't have been loaded if the component hasn't probed */
1190 if (!cs35l56->component)
1193 ret = cs35l56_is_fw_reload_needed(&cs35l56->base);
1194 dev_dbg(cs35l56->base.dev, "fw_reload_needed: %d\n", ret);
1198 cs35l56->base.fw_patched = false;
1199 wm_adsp_power_down(&cs35l56->dsp);
1200 queue_work(cs35l56->dsp_wq, &cs35l56->dsp_work);
1203 * suspend_bias_off ensures we are now in BIAS_OFF so there will be
1204 * a BIAS_OFF->BIAS_STANDBY transition to complete dsp patching.
1209 EXPORT_SYMBOL_GPL(cs35l56_system_resume);
1211 static int cs35l56_dsp_init(struct cs35l56_private *cs35l56)
1213 struct wm_adsp *dsp;
1216 cs35l56->dsp_wq = create_singlethread_workqueue("cs35l56-dsp");
1217 if (!cs35l56->dsp_wq)
1220 INIT_WORK(&cs35l56->dsp_work, cs35l56_dsp_work);
1222 dsp = &cs35l56->dsp;
1223 cs35l56_init_cs_dsp(&cs35l56->base, &dsp->cs_dsp);
1224 dsp->part = "cs35l56";
1226 dsp->wmfw_optional = true;
1228 dev_dbg(cs35l56->base.dev, "DSP system name: '%s'\n", dsp->system_name);
1230 ret = wm_halo_init(dsp);
1232 dev_err(cs35l56->base.dev, "wm_halo_init failed\n");
1239 static int cs35l56_get_firmware_uid(struct cs35l56_private *cs35l56)
1241 struct device *dev = cs35l56->base.dev;
1245 ret = device_property_read_string(dev, "cirrus,firmware-uid", &prop);
1246 /* If bad sw node property, return 0 and fallback to legacy firmware path */
1250 /* Append a speaker qualifier if there is a speaker ID */
1251 if (cs35l56->speaker_id >= 0)
1252 cs35l56->dsp.system_name = devm_kasprintf(dev, GFP_KERNEL, "%s-spkid%d",
1253 prop, cs35l56->speaker_id);
1255 cs35l56->dsp.system_name = devm_kstrdup(dev, prop, GFP_KERNEL);
1257 if (cs35l56->dsp.system_name == NULL)
1260 dev_dbg(dev, "Firmware UID: %s\n", cs35l56->dsp.system_name);
1266 * Some SoundWire laptops have a spk-id-gpios property but it points to
1267 * the wrong ACPI Device node so can't be used to get the GPIO. Try to
1268 * find the SDCA node containing the GpioIo resource and add a GPIO
1271 static const struct acpi_gpio_params cs35l56_af01_first_gpio = { 0, 0, false };
1272 static const struct acpi_gpio_mapping cs35l56_af01_spkid_gpios_mapping[] = {
1273 { "spk-id-gpios", &cs35l56_af01_first_gpio, 1 },
1277 static void cs35l56_acpi_dev_release_driver_gpios(void *adev)
1279 acpi_dev_remove_driver_gpios(adev);
1282 static int cs35l56_try_get_broken_sdca_spkid_gpio(struct cs35l56_private *cs35l56)
1284 struct fwnode_handle *af01_fwnode;
1285 const union acpi_object *obj;
1286 struct gpio_desc *desc;
1289 /* Find the SDCA node containing the GpioIo */
1290 af01_fwnode = device_get_named_child_node(cs35l56->base.dev, "AF01");
1292 dev_dbg(cs35l56->base.dev, "No AF01 node\n");
1296 ret = acpi_dev_get_property(ACPI_COMPANION(cs35l56->base.dev),
1297 "spk-id-gpios", ACPI_TYPE_PACKAGE, &obj);
1299 dev_dbg(cs35l56->base.dev, "Could not get spk-id-gpios package: %d\n", ret);
1303 /* The broken properties we can handle are a 4-element package (one GPIO) */
1304 if (obj->package.count != 4) {
1305 dev_warn(cs35l56->base.dev, "Unexpected spk-id element count %d\n",
1306 obj->package.count);
1310 /* Add a GPIO mapping if it doesn't already have one */
1311 if (!fwnode_property_present(af01_fwnode, "spk-id-gpios")) {
1312 struct acpi_device *adev = to_acpi_device_node(af01_fwnode);
1315 * Can't use devm_acpi_dev_add_driver_gpios() because the
1316 * mapping isn't being added to the node pointed to by
1319 ret = acpi_dev_add_driver_gpios(adev, cs35l56_af01_spkid_gpios_mapping);
1321 return dev_err_probe(cs35l56->base.dev, ret,
1322 "Failed to add gpio mapping to AF01\n");
1325 ret = devm_add_action_or_reset(cs35l56->base.dev,
1326 cs35l56_acpi_dev_release_driver_gpios,
1331 dev_dbg(cs35l56->base.dev, "Added spk-id-gpios mapping to AF01\n");
1334 desc = fwnode_gpiod_get_index(af01_fwnode, "spk-id", 0, GPIOD_IN, NULL);
1336 ret = PTR_ERR(desc);
1337 return dev_err_probe(cs35l56->base.dev, ret, "Get GPIO from AF01 failed\n");
1340 ret = gpiod_get_value_cansleep(desc);
1344 dev_err_probe(cs35l56->base.dev, ret, "Error reading spk-id GPIO\n");
1348 dev_info(cs35l56->base.dev, "Got spk-id from AF01\n");
1353 int cs35l56_common_probe(struct cs35l56_private *cs35l56)
1357 init_completion(&cs35l56->init_completion);
1358 mutex_init(&cs35l56->base.irq_lock);
1359 cs35l56->speaker_id = -ENOENT;
1361 dev_set_drvdata(cs35l56->base.dev, cs35l56);
1363 cs35l56_fill_supply_names(cs35l56->supplies);
1364 ret = devm_regulator_bulk_get(cs35l56->base.dev, ARRAY_SIZE(cs35l56->supplies),
1367 return dev_err_probe(cs35l56->base.dev, ret, "Failed to request supplies\n");
1369 /* Reset could be controlled by the BIOS or shared by multiple amps */
1370 cs35l56->base.reset_gpio = devm_gpiod_get_optional(cs35l56->base.dev, "reset",
1372 if (IS_ERR(cs35l56->base.reset_gpio)) {
1373 ret = PTR_ERR(cs35l56->base.reset_gpio);
1375 * If RESET is shared the first amp to probe will grab the reset
1376 * line and reset all the amps
1379 return dev_err_probe(cs35l56->base.dev, ret, "Failed to get reset GPIO\n");
1381 dev_info(cs35l56->base.dev, "Reset GPIO busy, assume shared reset\n");
1382 cs35l56->base.reset_gpio = NULL;
1385 ret = regulator_bulk_enable(ARRAY_SIZE(cs35l56->supplies), cs35l56->supplies);
1387 return dev_err_probe(cs35l56->base.dev, ret, "Failed to enable supplies\n");
1389 if (cs35l56->base.reset_gpio) {
1390 /* ACPI can override GPIOD_OUT_LOW flag so force it to start low */
1391 gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 0);
1392 cs35l56_wait_min_reset_pulse();
1393 gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 1);
1396 ret = cs35l56_get_speaker_id(&cs35l56->base);
1397 if (ACPI_COMPANION(cs35l56->base.dev) && cs35l56->sdw_peripheral && (ret == -ENOENT))
1398 ret = cs35l56_try_get_broken_sdca_spkid_gpio(cs35l56);
1400 if ((ret < 0) && (ret != -ENOENT))
1403 cs35l56->speaker_id = ret;
1405 ret = cs35l56_get_firmware_uid(cs35l56);
1409 ret = cs35l56_dsp_init(cs35l56);
1411 dev_err_probe(cs35l56->base.dev, ret, "DSP init failed\n");
1415 ret = devm_snd_soc_register_component(cs35l56->base.dev,
1416 &soc_component_dev_cs35l56,
1417 cs35l56_dai, ARRAY_SIZE(cs35l56_dai));
1419 dev_err_probe(cs35l56->base.dev, ret, "Register codec failed\n");
1426 gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 0);
1427 regulator_bulk_disable(ARRAY_SIZE(cs35l56->supplies), cs35l56->supplies);
1431 EXPORT_SYMBOL_NS_GPL(cs35l56_common_probe, SND_SOC_CS35L56_CORE);
1433 int cs35l56_init(struct cs35l56_private *cs35l56)
1438 * Check whether the actions associated with soft reset or one time
1439 * init need to be performed.
1441 if (cs35l56->soft_resetting)
1442 goto post_soft_reset;
1444 if (cs35l56->base.init_done)
1447 pm_runtime_set_autosuspend_delay(cs35l56->base.dev, 100);
1448 pm_runtime_use_autosuspend(cs35l56->base.dev);
1449 pm_runtime_set_active(cs35l56->base.dev);
1450 pm_runtime_enable(cs35l56->base.dev);
1452 ret = cs35l56_hw_init(&cs35l56->base);
1456 ret = cs35l56_set_patch(&cs35l56->base);
1460 if (!cs35l56->base.reset_gpio) {
1461 dev_dbg(cs35l56->base.dev, "No reset gpio: using soft reset\n");
1462 cs35l56->soft_resetting = true;
1463 cs35l56_system_reset(&cs35l56->base, !!cs35l56->sdw_peripheral);
1464 if (cs35l56->sdw_peripheral) {
1465 /* Keep alive while we wait for re-enumeration */
1466 pm_runtime_get_noresume(cs35l56->base.dev);
1472 if (cs35l56->soft_resetting) {
1473 cs35l56->soft_resetting = false;
1475 /* Done re-enumerating after one-time init so release the keep-alive */
1476 if (cs35l56->sdw_peripheral && !cs35l56->base.init_done)
1477 pm_runtime_put_noidle(cs35l56->base.dev);
1479 regcache_mark_dirty(cs35l56->base.regmap);
1480 ret = cs35l56_wait_for_firmware_boot(&cs35l56->base);
1484 dev_dbg(cs35l56->base.dev, "Firmware rebooted after soft reset\n");
1487 /* Disable auto-hibernate so that runtime_pm has control */
1488 ret = cs35l56_mbox_send(&cs35l56->base, CS35L56_MBOX_CMD_PREVENT_AUTO_HIBERNATE);
1492 /* Registers could be dirty after soft reset or SoundWire enumeration */
1493 regcache_sync(cs35l56->base.regmap);
1495 /* Set ASP1 DOUT to high-impedance when it is not transmitting audio data. */
1496 ret = regmap_set_bits(cs35l56->base.regmap, CS35L56_ASP1_CONTROL3,
1497 CS35L56_ASP1_DOUT_HIZ_CTRL_MASK);
1499 return dev_err_probe(cs35l56->base.dev, ret, "Failed to write ASP1_CONTROL3\n");
1501 cs35l56->base.init_done = true;
1502 complete(&cs35l56->init_completion);
1506 EXPORT_SYMBOL_NS_GPL(cs35l56_init, SND_SOC_CS35L56_CORE);
1508 void cs35l56_remove(struct cs35l56_private *cs35l56)
1510 cs35l56->base.init_done = false;
1513 * WAKE IRQs unmask if CS35L56 hibernates so free the handler to
1514 * prevent it racing with remove().
1516 if (cs35l56->base.irq)
1517 devm_free_irq(cs35l56->base.dev, cs35l56->base.irq, &cs35l56->base);
1519 flush_workqueue(cs35l56->dsp_wq);
1520 destroy_workqueue(cs35l56->dsp_wq);
1522 pm_runtime_dont_use_autosuspend(cs35l56->base.dev);
1523 pm_runtime_suspend(cs35l56->base.dev);
1524 pm_runtime_disable(cs35l56->base.dev);
1526 regcache_cache_only(cs35l56->base.regmap, true);
1528 gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 0);
1529 regulator_bulk_disable(ARRAY_SIZE(cs35l56->supplies), cs35l56->supplies);
1531 EXPORT_SYMBOL_NS_GPL(cs35l56_remove, SND_SOC_CS35L56_CORE);
1533 #if IS_ENABLED(CONFIG_SND_SOC_CS35L56_I2C) || IS_ENABLED(CONFIG_SND_SOC_CS35L56_SPI)
1534 EXPORT_NS_GPL_DEV_PM_OPS(cs35l56_pm_ops_i2c_spi, SND_SOC_CS35L56_CORE) = {
1535 SET_RUNTIME_PM_OPS(cs35l56_runtime_suspend_i2c_spi, cs35l56_runtime_resume_i2c_spi, NULL)
1536 SYSTEM_SLEEP_PM_OPS(cs35l56_system_suspend, cs35l56_system_resume)
1537 LATE_SYSTEM_SLEEP_PM_OPS(cs35l56_system_suspend_late, cs35l56_system_resume_early)
1538 NOIRQ_SYSTEM_SLEEP_PM_OPS(cs35l56_system_suspend_no_irq, cs35l56_system_resume_no_irq)
1542 MODULE_DESCRIPTION("ASoC CS35L56 driver");
1543 MODULE_IMPORT_NS(SND_SOC_CS35L56_SHARED);
1544 MODULE_AUTHOR("Richard Fitzgerald <rf@opensource.cirrus.com>");
1545 MODULE_AUTHOR("Simon Trimmer <simont@opensource.cirrus.com>");
1546 MODULE_LICENSE("GPL");