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/completion.h>
9 #include <linux/debugfs.h>
10 #include <linux/delay.h>
11 #include <linux/err.h>
12 #include <linux/gpio/consumer.h>
13 #include <linux/interrupt.h>
14 #include <linux/math.h>
15 #include <linux/module.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/regmap.h>
19 #include <linux/regulator/consumer.h>
20 #include <linux/slab.h>
21 #include <linux/soundwire/sdw.h>
22 #include <linux/types.h>
23 #include <linux/workqueue.h>
24 #include <sound/pcm.h>
25 #include <sound/pcm_params.h>
26 #include <sound/soc.h>
27 #include <sound/soc-dapm.h>
28 #include <sound/tlv.h>
33 static int cs35l56_dsp_event(struct snd_soc_dapm_widget *w,
34 struct snd_kcontrol *kcontrol, int event);
36 static int cs35l56_mbox_send(struct cs35l56_private *cs35l56, unsigned int command)
41 regmap_write(cs35l56->regmap, CS35L56_DSP_VIRTUAL1_MBOX_1, command);
42 ret = regmap_read_poll_timeout(cs35l56->regmap, CS35L56_DSP_VIRTUAL1_MBOX_1,
44 CS35L56_MBOX_POLL_US, CS35L56_MBOX_TIMEOUT_US);
46 dev_warn(cs35l56->dev, "MBOX command %#x failed: %d\n", command, ret);
53 static void cs35l56_wait_dsp_ready(struct cs35l56_private *cs35l56)
55 /* Wait for patching to complete */
56 flush_work(&cs35l56->dsp_work);
59 static int cs35l56_dspwait_get_volsw(struct snd_kcontrol *kcontrol,
60 struct snd_ctl_elem_value *ucontrol)
62 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
63 struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
65 cs35l56_wait_dsp_ready(cs35l56);
66 return snd_soc_get_volsw(kcontrol, ucontrol);
69 static int cs35l56_dspwait_put_volsw(struct snd_kcontrol *kcontrol,
70 struct snd_ctl_elem_value *ucontrol)
72 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
73 struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
75 cs35l56_wait_dsp_ready(cs35l56);
76 return snd_soc_put_volsw(kcontrol, ucontrol);
79 static DECLARE_TLV_DB_SCALE(vol_tlv, -10000, 25, 0);
81 static const struct snd_kcontrol_new cs35l56_controls[] = {
82 SOC_SINGLE_EXT("Speaker Switch",
83 CS35L56_MAIN_RENDER_USER_MUTE, 0, 1, 1,
84 cs35l56_dspwait_get_volsw, cs35l56_dspwait_put_volsw),
85 SOC_SINGLE_S_EXT_TLV("Speaker Volume",
86 CS35L56_MAIN_RENDER_USER_VOLUME,
88 cs35l56_dspwait_get_volsw,
89 cs35l56_dspwait_put_volsw,
91 SOC_SINGLE_EXT("Posture Number", CS35L56_MAIN_POSTURE_NUMBER,
93 cs35l56_dspwait_get_volsw, cs35l56_dspwait_put_volsw),
96 static SOC_VALUE_ENUM_SINGLE_DECL(cs35l56_asp1tx1_enum,
97 CS35L56_ASP1TX1_INPUT,
98 0, CS35L56_ASP_TXn_SRC_MASK,
99 cs35l56_tx_input_texts,
100 cs35l56_tx_input_values);
102 static const struct snd_kcontrol_new asp1_tx1_mux =
103 SOC_DAPM_ENUM("ASP1TX1 SRC", cs35l56_asp1tx1_enum);
105 static SOC_VALUE_ENUM_SINGLE_DECL(cs35l56_asp1tx2_enum,
106 CS35L56_ASP1TX2_INPUT,
107 0, CS35L56_ASP_TXn_SRC_MASK,
108 cs35l56_tx_input_texts,
109 cs35l56_tx_input_values);
111 static const struct snd_kcontrol_new asp1_tx2_mux =
112 SOC_DAPM_ENUM("ASP1TX2 SRC", cs35l56_asp1tx2_enum);
114 static SOC_VALUE_ENUM_SINGLE_DECL(cs35l56_asp1tx3_enum,
115 CS35L56_ASP1TX3_INPUT,
116 0, CS35L56_ASP_TXn_SRC_MASK,
117 cs35l56_tx_input_texts,
118 cs35l56_tx_input_values);
120 static const struct snd_kcontrol_new asp1_tx3_mux =
121 SOC_DAPM_ENUM("ASP1TX3 SRC", cs35l56_asp1tx3_enum);
123 static SOC_VALUE_ENUM_SINGLE_DECL(cs35l56_asp1tx4_enum,
124 CS35L56_ASP1TX4_INPUT,
125 0, CS35L56_ASP_TXn_SRC_MASK,
126 cs35l56_tx_input_texts,
127 cs35l56_tx_input_values);
129 static const struct snd_kcontrol_new asp1_tx4_mux =
130 SOC_DAPM_ENUM("ASP1TX4 SRC", cs35l56_asp1tx4_enum);
132 static SOC_VALUE_ENUM_SINGLE_DECL(cs35l56_sdw1tx1_enum,
133 CS35L56_SWIRE_DP3_CH1_INPUT,
134 0, CS35L56_SWIRETXn_SRC_MASK,
135 cs35l56_tx_input_texts,
136 cs35l56_tx_input_values);
138 static const struct snd_kcontrol_new sdw1_tx1_mux =
139 SOC_DAPM_ENUM("SDW1TX1 SRC", cs35l56_sdw1tx1_enum);
141 static SOC_VALUE_ENUM_SINGLE_DECL(cs35l56_sdw1tx2_enum,
142 CS35L56_SWIRE_DP3_CH2_INPUT,
143 0, CS35L56_SWIRETXn_SRC_MASK,
144 cs35l56_tx_input_texts,
145 cs35l56_tx_input_values);
147 static const struct snd_kcontrol_new sdw1_tx2_mux =
148 SOC_DAPM_ENUM("SDW1TX2 SRC", cs35l56_sdw1tx2_enum);
150 static SOC_VALUE_ENUM_SINGLE_DECL(cs35l56_sdw1tx3_enum,
151 CS35L56_SWIRE_DP3_CH3_INPUT,
152 0, CS35L56_SWIRETXn_SRC_MASK,
153 cs35l56_tx_input_texts,
154 cs35l56_tx_input_values);
156 static const struct snd_kcontrol_new sdw1_tx3_mux =
157 SOC_DAPM_ENUM("SDW1TX3 SRC", cs35l56_sdw1tx3_enum);
159 static SOC_VALUE_ENUM_SINGLE_DECL(cs35l56_sdw1tx4_enum,
160 CS35L56_SWIRE_DP3_CH4_INPUT,
161 0, CS35L56_SWIRETXn_SRC_MASK,
162 cs35l56_tx_input_texts,
163 cs35l56_tx_input_values);
165 static const struct snd_kcontrol_new sdw1_tx4_mux =
166 SOC_DAPM_ENUM("SDW1TX4 SRC", cs35l56_sdw1tx4_enum);
168 static int cs35l56_play_event(struct snd_soc_dapm_widget *w,
169 struct snd_kcontrol *kcontrol, int event)
171 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
172 struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
176 dev_dbg(cs35l56->dev, "play: %d\n", event);
179 case SND_SOC_DAPM_PRE_PMU:
180 /* Don't wait for ACK, we check in POST_PMU that it completed */
181 return regmap_write(cs35l56->regmap, CS35L56_DSP_VIRTUAL1_MBOX_1,
182 CS35L56_MBOX_CMD_AUDIO_PLAY);
183 case SND_SOC_DAPM_POST_PMU:
184 /* Wait for firmware to enter PS0 power state */
185 ret = regmap_read_poll_timeout(cs35l56->regmap,
186 CS35L56_TRANSDUCER_ACTUAL_PS,
187 val, (val == CS35L56_PS0),
189 CS35L56_PS0_TIMEOUT_US);
191 dev_err(cs35l56->dev, "PS0 wait failed: %d\n", ret);
193 case SND_SOC_DAPM_POST_PMD:
194 return cs35l56_mbox_send(cs35l56, CS35L56_MBOX_CMD_AUDIO_PAUSE);
200 static const struct snd_soc_dapm_widget cs35l56_dapm_widgets[] = {
201 SND_SOC_DAPM_REGULATOR_SUPPLY("VDD_B", 0, 0),
202 SND_SOC_DAPM_REGULATOR_SUPPLY("VDD_AMP", 0, 0),
204 SND_SOC_DAPM_SUPPLY("PLAY", SND_SOC_NOPM, 0, 0, cs35l56_play_event,
205 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
207 SND_SOC_DAPM_OUT_DRV("AMP", SND_SOC_NOPM, 0, 0, NULL, 0),
208 SND_SOC_DAPM_OUTPUT("SPK"),
210 SND_SOC_DAPM_PGA_E("DSP1", SND_SOC_NOPM, 0, 0, NULL, 0, cs35l56_dsp_event,
211 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
213 SND_SOC_DAPM_AIF_IN("ASP1RX1", NULL, 0, CS35L56_ASP1_ENABLES1,
214 CS35L56_ASP_RX1_EN_SHIFT, 0),
215 SND_SOC_DAPM_AIF_IN("ASP1RX2", NULL, 1, CS35L56_ASP1_ENABLES1,
216 CS35L56_ASP_RX2_EN_SHIFT, 0),
217 SND_SOC_DAPM_AIF_OUT("ASP1TX1", NULL, 0, CS35L56_ASP1_ENABLES1,
218 CS35L56_ASP_TX1_EN_SHIFT, 0),
219 SND_SOC_DAPM_AIF_OUT("ASP1TX2", NULL, 1, CS35L56_ASP1_ENABLES1,
220 CS35L56_ASP_TX2_EN_SHIFT, 0),
221 SND_SOC_DAPM_AIF_OUT("ASP1TX3", NULL, 2, CS35L56_ASP1_ENABLES1,
222 CS35L56_ASP_TX3_EN_SHIFT, 0),
223 SND_SOC_DAPM_AIF_OUT("ASP1TX4", NULL, 3, CS35L56_ASP1_ENABLES1,
224 CS35L56_ASP_TX4_EN_SHIFT, 0),
226 SND_SOC_DAPM_MUX("ASP1 TX1 Source", SND_SOC_NOPM, 0, 0, &asp1_tx1_mux),
227 SND_SOC_DAPM_MUX("ASP1 TX2 Source", SND_SOC_NOPM, 0, 0, &asp1_tx2_mux),
228 SND_SOC_DAPM_MUX("ASP1 TX3 Source", SND_SOC_NOPM, 0, 0, &asp1_tx3_mux),
229 SND_SOC_DAPM_MUX("ASP1 TX4 Source", SND_SOC_NOPM, 0, 0, &asp1_tx4_mux),
231 SND_SOC_DAPM_MUX("SDW1 TX1 Source", SND_SOC_NOPM, 0, 0, &sdw1_tx1_mux),
232 SND_SOC_DAPM_MUX("SDW1 TX2 Source", SND_SOC_NOPM, 0, 0, &sdw1_tx2_mux),
233 SND_SOC_DAPM_MUX("SDW1 TX3 Source", SND_SOC_NOPM, 0, 0, &sdw1_tx3_mux),
234 SND_SOC_DAPM_MUX("SDW1 TX4 Source", SND_SOC_NOPM, 0, 0, &sdw1_tx4_mux),
236 SND_SOC_DAPM_SIGGEN("VMON ADC"),
237 SND_SOC_DAPM_SIGGEN("IMON ADC"),
238 SND_SOC_DAPM_SIGGEN("ERRVOL ADC"),
239 SND_SOC_DAPM_SIGGEN("CLASSH ADC"),
240 SND_SOC_DAPM_SIGGEN("VDDBMON ADC"),
241 SND_SOC_DAPM_SIGGEN("VBSTMON ADC"),
242 SND_SOC_DAPM_SIGGEN("TEMPMON ADC"),
245 #define CS35L56_SRC_ROUTE(name) \
246 { name" Source", "ASP1RX1", "ASP1RX1" }, \
247 { name" Source", "ASP1RX2", "ASP1RX2" }, \
248 { name" Source", "VMON", "VMON ADC" }, \
249 { name" Source", "IMON", "IMON ADC" }, \
250 { name" Source", "ERRVOL", "ERRVOL ADC" }, \
251 { name" Source", "CLASSH", "CLASSH ADC" }, \
252 { name" Source", "VDDBMON", "VDDBMON ADC" }, \
253 { name" Source", "VBSTMON", "VBSTMON ADC" }, \
254 { name" Source", "DSP1TX1", "DSP1" }, \
255 { name" Source", "DSP1TX2", "DSP1" }, \
256 { name" Source", "DSP1TX3", "DSP1" }, \
257 { name" Source", "DSP1TX4", "DSP1" }, \
258 { name" Source", "DSP1TX5", "DSP1" }, \
259 { name" Source", "DSP1TX6", "DSP1" }, \
260 { name" Source", "DSP1TX7", "DSP1" }, \
261 { name" Source", "DSP1TX8", "DSP1" }, \
262 { name" Source", "TEMPMON", "TEMPMON ADC" }, \
263 { name" Source", "INTERPOLATOR", "AMP" }, \
264 { name" Source", "SDW1RX1", "SDW1 Playback" }, \
265 { name" Source", "SDW1RX2", "SDW1 Playback" },
267 static const struct snd_soc_dapm_route cs35l56_audio_map[] = {
268 { "AMP", NULL, "VDD_B" },
269 { "AMP", NULL, "VDD_AMP" },
271 { "ASP1 Playback", NULL, "PLAY" },
272 { "SDW1 Playback", NULL, "PLAY" },
274 { "ASP1RX1", NULL, "ASP1 Playback" },
275 { "ASP1RX2", NULL, "ASP1 Playback" },
276 { "DSP1", NULL, "ASP1RX1" },
277 { "DSP1", NULL, "ASP1RX2" },
278 { "DSP1", NULL, "SDW1 Playback" },
279 { "AMP", NULL, "DSP1" },
280 { "SPK", NULL, "AMP" },
282 CS35L56_SRC_ROUTE("ASP1 TX1")
283 CS35L56_SRC_ROUTE("ASP1 TX2")
284 CS35L56_SRC_ROUTE("ASP1 TX3")
285 CS35L56_SRC_ROUTE("ASP1 TX4")
287 { "ASP1TX1", NULL, "ASP1 TX1 Source" },
288 { "ASP1TX2", NULL, "ASP1 TX2 Source" },
289 { "ASP1TX3", NULL, "ASP1 TX3 Source" },
290 { "ASP1TX4", NULL, "ASP1 TX4 Source" },
291 { "ASP1 Capture", NULL, "ASP1TX1" },
292 { "ASP1 Capture", NULL, "ASP1TX2" },
293 { "ASP1 Capture", NULL, "ASP1TX3" },
294 { "ASP1 Capture", NULL, "ASP1TX4" },
296 CS35L56_SRC_ROUTE("SDW1 TX1")
297 CS35L56_SRC_ROUTE("SDW1 TX2")
298 CS35L56_SRC_ROUTE("SDW1 TX3")
299 CS35L56_SRC_ROUTE("SDW1 TX4")
300 { "SDW1 Capture", NULL, "SDW1 TX1 Source" },
301 { "SDW1 Capture", NULL, "SDW1 TX2 Source" },
302 { "SDW1 Capture", NULL, "SDW1 TX3 Source" },
303 { "SDW1 Capture", NULL, "SDW1 TX4 Source" },
306 static int cs35l56_dsp_event(struct snd_soc_dapm_widget *w,
307 struct snd_kcontrol *kcontrol, int event)
309 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
310 struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
312 dev_dbg(cs35l56->dev, "%s: %d\n", __func__, event);
314 return wm_adsp_event(w, kcontrol, event);
317 irqreturn_t cs35l56_irq(int irq, void *data)
319 struct cs35l56_private *cs35l56 = data;
320 unsigned int status1 = 0, status8 = 0, status20 = 0;
321 unsigned int mask1, mask8, mask20;
325 irqreturn_t ret = IRQ_NONE;
327 if (!cs35l56->init_done)
330 mutex_lock(&cs35l56->irq_lock);
332 rv = pm_runtime_resume_and_get(cs35l56->dev);
334 dev_err(cs35l56->dev, "irq: failed to get pm_runtime: %d\n", rv);
338 regmap_read(cs35l56->regmap, CS35L56_IRQ1_STATUS, &val);
339 if ((val & CS35L56_IRQ1_STS_MASK) == 0) {
340 dev_dbg(cs35l56->dev, "Spurious IRQ: no pending interrupt\n");
345 regmap_read(cs35l56->regmap, CS35L56_IRQ1_EINT_1, &status1);
346 regmap_read(cs35l56->regmap, CS35L56_IRQ1_MASK_1, &mask1);
348 regmap_write(cs35l56->regmap, CS35L56_IRQ1_EINT_1, status1);
350 regmap_read(cs35l56->regmap, CS35L56_IRQ1_EINT_8, &status8);
351 regmap_read(cs35l56->regmap, CS35L56_IRQ1_MASK_8, &mask8);
353 regmap_write(cs35l56->regmap, CS35L56_IRQ1_EINT_8, status8);
355 regmap_read(cs35l56->regmap, CS35L56_IRQ1_EINT_20, &status20);
356 regmap_read(cs35l56->regmap, CS35L56_IRQ1_MASK_20, &mask20);
358 /* We don't want EINT20 but they default to unmasked: force mask */
359 regmap_write(cs35l56->regmap, CS35L56_IRQ1_MASK_20, 0xffffffff);
361 dev_dbg(cs35l56->dev, "%s: %#x %#x\n", __func__, status1, status8);
363 /* Check to see if unmasked bits are active */
364 if (!status1 && !status8 && !status20)
367 if (status1 & CS35L56_AMP_SHORT_ERR_EINT1_MASK)
368 dev_crit(cs35l56->dev, "Amp short error\n");
370 if (status8 & CS35L56_TEMP_ERR_EINT1_MASK)
371 dev_crit(cs35l56->dev, "Overtemp error\n");
376 pm_runtime_put(cs35l56->dev);
378 mutex_unlock(&cs35l56->irq_lock);
382 EXPORT_SYMBOL_NS_GPL(cs35l56_irq, SND_SOC_CS35L56_CORE);
384 int cs35l56_irq_request(struct cs35l56_private *cs35l56, int irq)
391 ret = devm_request_threaded_irq(cs35l56->dev, irq, NULL, cs35l56_irq,
392 IRQF_ONESHOT | IRQF_SHARED | IRQF_TRIGGER_LOW,
397 dev_err(cs35l56->dev, "Failed to get IRQ: %d\n", ret);
401 EXPORT_SYMBOL_NS_GPL(cs35l56_irq_request, SND_SOC_CS35L56_CORE);
403 static int cs35l56_asp_dai_set_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
405 struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(codec_dai->component);
408 dev_dbg(cs35l56->dev, "%s: %#x\n", __func__, fmt);
410 switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
411 case SND_SOC_DAIFMT_CBC_CFC:
414 dev_err(cs35l56->dev, "Unsupported clock source mode\n");
418 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
419 case SND_SOC_DAIFMT_DSP_A:
420 val = CS35L56_ASP_FMT_DSP_A << CS35L56_ASP_FMT_SHIFT;
421 cs35l56->tdm_mode = true;
423 case SND_SOC_DAIFMT_I2S:
424 val = CS35L56_ASP_FMT_I2S << CS35L56_ASP_FMT_SHIFT;
425 cs35l56->tdm_mode = false;
428 dev_err(cs35l56->dev, "Unsupported DAI format\n");
432 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
433 case SND_SOC_DAIFMT_NB_IF:
434 val |= CS35L56_ASP_FSYNC_INV_MASK;
436 case SND_SOC_DAIFMT_IB_NF:
437 val |= CS35L56_ASP_BCLK_INV_MASK;
439 case SND_SOC_DAIFMT_IB_IF:
440 val |= CS35L56_ASP_BCLK_INV_MASK | CS35L56_ASP_FSYNC_INV_MASK;
442 case SND_SOC_DAIFMT_NB_NF:
445 dev_err(cs35l56->dev, "Invalid clock invert\n");
449 regmap_update_bits(cs35l56->regmap,
450 CS35L56_ASP1_CONTROL2,
451 CS35L56_ASP_FMT_MASK |
452 CS35L56_ASP_BCLK_INV_MASK | CS35L56_ASP_FSYNC_INV_MASK,
455 /* Hi-Z DOUT in unused slots and when all TX are disabled */
456 regmap_update_bits(cs35l56->regmap, CS35L56_ASP1_CONTROL3,
457 CS35L56_ASP1_DOUT_HIZ_CTRL_MASK,
458 CS35L56_ASP_UNUSED_HIZ_OFF_HIZ);
463 static void cs35l56_set_asp_slot_positions(struct cs35l56_private *cs35l56,
464 unsigned int reg, unsigned long mask)
466 unsigned int reg_val, channel_shift;
469 /* Init all slots to 63 */
471 case CS35L56_ASP1_FRAME_CONTROL1:
472 reg_val = 0x3f3f3f3f;
474 case CS35L56_ASP1_FRAME_CONTROL5:
479 /* Enable consecutive TX1..TXn for each of the slots set in mask */
481 for_each_set_bit(bit_num, &mask, 32) {
482 reg_val &= ~(0x3f << channel_shift);
483 reg_val |= bit_num << channel_shift;
487 regmap_write(cs35l56->regmap, reg, reg_val);
490 static int cs35l56_asp_dai_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
491 unsigned int rx_mask, int slots, int slot_width)
493 struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(dai->component);
495 if ((slots == 0) || (slot_width == 0)) {
496 dev_dbg(cs35l56->dev, "tdm config cleared\n");
497 cs35l56->asp_slot_width = 0;
498 cs35l56->asp_slot_count = 0;
502 if (slot_width > (CS35L56_ASP_RX_WIDTH_MASK >> CS35L56_ASP_RX_WIDTH_SHIFT)) {
503 dev_err(cs35l56->dev, "tdm invalid slot width %d\n", slot_width);
507 /* More than 32 slots would give an unsupportable BCLK frequency */
509 dev_err(cs35l56->dev, "tdm invalid slot count %d\n", slots);
513 cs35l56->asp_slot_width = (u8)slot_width;
514 cs35l56->asp_slot_count = (u8)slots;
516 // Note: rx/tx is from point of view of the CPU end
518 tx_mask = 0x3; // ASPRX1/RX2 in slots 0 and 1
521 rx_mask = 0xf; // ASPTX1..TX4 in slots 0..3
523 cs35l56_set_asp_slot_positions(cs35l56, CS35L56_ASP1_FRAME_CONTROL1, rx_mask);
524 cs35l56_set_asp_slot_positions(cs35l56, CS35L56_ASP1_FRAME_CONTROL5, tx_mask);
526 dev_dbg(cs35l56->dev, "tdm slot width: %u count: %u tx_mask: %#x rx_mask: %#x\n",
527 cs35l56->asp_slot_width, cs35l56->asp_slot_count, tx_mask, rx_mask);
532 static int cs35l56_asp_dai_hw_params(struct snd_pcm_substream *substream,
533 struct snd_pcm_hw_params *params,
534 struct snd_soc_dai *dai)
536 struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(dai->component);
537 unsigned int rate = params_rate(params);
538 u8 asp_width, asp_wl;
540 asp_wl = params_width(params);
541 if (cs35l56->asp_slot_width)
542 asp_width = cs35l56->asp_slot_width;
546 dev_dbg(cs35l56->dev, "%s: wl=%d, width=%d, rate=%d", __func__, asp_wl, asp_width, rate);
548 if (!cs35l56->sysclk_set) {
549 unsigned int slots = cs35l56->asp_slot_count;
550 unsigned int bclk_freq;
554 slots = params_channels(params);
556 /* I2S always has an even number of slots */
557 if (!cs35l56->tdm_mode)
558 slots = round_up(slots, 2);
561 bclk_freq = asp_width * slots * rate;
562 freq_id = cs35l56_get_bclk_freq_id(bclk_freq);
564 dev_err(cs35l56->dev, "%s: Invalid BCLK %u\n", __func__, bclk_freq);
568 regmap_update_bits(cs35l56->regmap, CS35L56_ASP1_CONTROL1,
569 CS35L56_ASP_BCLK_FREQ_MASK,
570 freq_id << CS35L56_ASP_BCLK_FREQ_SHIFT);
573 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
574 regmap_update_bits(cs35l56->regmap, CS35L56_ASP1_CONTROL2,
575 CS35L56_ASP_RX_WIDTH_MASK, asp_width <<
576 CS35L56_ASP_RX_WIDTH_SHIFT);
577 regmap_update_bits(cs35l56->regmap, CS35L56_ASP1_DATA_CONTROL5,
578 CS35L56_ASP_RX_WL_MASK, asp_wl);
580 regmap_update_bits(cs35l56->regmap, CS35L56_ASP1_CONTROL2,
581 CS35L56_ASP_TX_WIDTH_MASK, asp_width <<
582 CS35L56_ASP_TX_WIDTH_SHIFT);
583 regmap_update_bits(cs35l56->regmap, CS35L56_ASP1_DATA_CONTROL1,
584 CS35L56_ASP_TX_WL_MASK, asp_wl);
590 static int cs35l56_asp_dai_set_sysclk(struct snd_soc_dai *dai,
591 int clk_id, unsigned int freq, int dir)
593 struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(dai->component);
597 cs35l56->sysclk_set = false;
601 freq_id = cs35l56_get_bclk_freq_id(freq);
605 regmap_update_bits(cs35l56->regmap, CS35L56_ASP1_CONTROL1,
606 CS35L56_ASP_BCLK_FREQ_MASK,
607 freq_id << CS35L56_ASP_BCLK_FREQ_SHIFT);
608 cs35l56->sysclk_set = true;
613 static const struct snd_soc_dai_ops cs35l56_ops = {
614 .set_fmt = cs35l56_asp_dai_set_fmt,
615 .set_tdm_slot = cs35l56_asp_dai_set_tdm_slot,
616 .hw_params = cs35l56_asp_dai_hw_params,
617 .set_sysclk = cs35l56_asp_dai_set_sysclk,
620 static void cs35l56_sdw_dai_shutdown(struct snd_pcm_substream *substream,
621 struct snd_soc_dai *dai)
623 snd_soc_dai_set_dma_data(dai, substream, NULL);
626 static int cs35l56_sdw_dai_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
627 unsigned int rx_mask, int slots, int slot_width)
629 struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(dai->component);
631 /* rx/tx are from point of view of the CPU end so opposite to our rx/tx */
632 cs35l56->rx_mask = tx_mask;
633 cs35l56->tx_mask = rx_mask;
638 static int cs35l56_sdw_dai_hw_params(struct snd_pcm_substream *substream,
639 struct snd_pcm_hw_params *params,
640 struct snd_soc_dai *dai)
642 struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(dai->component);
643 struct sdw_stream_runtime *sdw_stream = snd_soc_dai_get_dma_data(dai, substream);
644 struct sdw_stream_config sconfig;
645 struct sdw_port_config pconfig;
648 dev_dbg(cs35l56->dev, "%s: rate %d\n", __func__, params_rate(params));
650 if (!cs35l56->init_done)
656 memset(&sconfig, 0, sizeof(sconfig));
657 memset(&pconfig, 0, sizeof(pconfig));
659 sconfig.frame_rate = params_rate(params);
660 sconfig.bps = snd_pcm_format_width(params_format(params));
662 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
663 sconfig.direction = SDW_DATA_DIR_RX;
664 pconfig.num = CS35L56_SDW1_PLAYBACK_PORT;
665 pconfig.ch_mask = cs35l56->rx_mask;
667 sconfig.direction = SDW_DATA_DIR_TX;
668 pconfig.num = CS35L56_SDW1_CAPTURE_PORT;
669 pconfig.ch_mask = cs35l56->tx_mask;
672 if (pconfig.ch_mask == 0) {
673 sconfig.ch_count = params_channels(params);
674 pconfig.ch_mask = GENMASK(sconfig.ch_count - 1, 0);
676 sconfig.ch_count = hweight32(pconfig.ch_mask);
679 ret = sdw_stream_add_slave(cs35l56->sdw_peripheral, &sconfig, &pconfig,
682 dev_err(dai->dev, "Failed to add sdw stream: %d\n", ret);
689 static int cs35l56_sdw_dai_hw_free(struct snd_pcm_substream *substream,
690 struct snd_soc_dai *dai)
692 struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(dai->component);
693 struct sdw_stream_runtime *sdw_stream = snd_soc_dai_get_dma_data(dai, substream);
695 if (!cs35l56->sdw_peripheral)
698 sdw_stream_remove_slave(cs35l56->sdw_peripheral, sdw_stream);
703 static int cs35l56_sdw_dai_set_stream(struct snd_soc_dai *dai,
704 void *sdw_stream, int direction)
706 snd_soc_dai_dma_data_set(dai, direction, sdw_stream);
711 static const struct snd_soc_dai_ops cs35l56_sdw_dai_ops = {
712 .set_tdm_slot = cs35l56_sdw_dai_set_tdm_slot,
713 .shutdown = cs35l56_sdw_dai_shutdown,
714 .hw_params = cs35l56_sdw_dai_hw_params,
715 .hw_free = cs35l56_sdw_dai_hw_free,
716 .set_stream = cs35l56_sdw_dai_set_stream,
719 static struct snd_soc_dai_driver cs35l56_dai[] = {
721 .name = "cs35l56-asp1",
724 .stream_name = "ASP1 Playback",
727 .rates = CS35L56_RATES,
728 .formats = CS35L56_RX_FORMATS,
731 .stream_name = "ASP1 Capture",
734 .rates = CS35L56_RATES,
735 .formats = CS35L56_TX_FORMATS,
739 .symmetric_sample_bits = 1,
742 .name = "cs35l56-sdw1",
745 .stream_name = "SDW1 Playback",
748 .rates = CS35L56_RATES,
749 .formats = CS35L56_RX_FORMATS,
752 .stream_name = "SDW1 Capture",
755 .rates = CS35L56_RATES,
756 .formats = CS35L56_TX_FORMATS,
759 .ops = &cs35l56_sdw_dai_ops,
763 static int cs35l56_wait_for_firmware_boot(struct cs35l56_private *cs35l56)
769 if (cs35l56->rev < CS35L56_REVID_B0)
770 reg = CS35L56_DSP1_HALO_STATE_A1;
772 reg = CS35L56_DSP1_HALO_STATE;
774 ret = regmap_read_poll_timeout(cs35l56->regmap, reg,
776 (val < 0xFFFF) && (val >= CS35L56_HALO_STATE_BOOT_DONE),
777 CS35L56_HALO_STATE_POLL_US,
778 CS35L56_HALO_STATE_TIMEOUT_US);
780 if ((ret < 0) && (ret != -ETIMEDOUT)) {
781 dev_err(cs35l56->dev, "Failed to read HALO_STATE: %d\n", ret);
785 if ((ret == -ETIMEDOUT) || (val != CS35L56_HALO_STATE_BOOT_DONE)) {
786 dev_err(cs35l56->dev, "Firmware boot fail: HALO_STATE=%#x\n", val);
793 static inline void cs35l56_wait_min_reset_pulse(void)
795 /* Satisfy minimum reset pulse width spec */
796 usleep_range(CS35L56_RESET_PULSE_MIN_US, 2 * CS35L56_RESET_PULSE_MIN_US);
799 static const struct reg_sequence cs35l56_system_reset_seq[] = {
800 REG_SEQ0(CS35L56_DSP_VIRTUAL1_MBOX_1, CS35L56_MBOX_CMD_SYSTEM_RESET),
803 static void cs35l56_system_reset(struct cs35l56_private *cs35l56)
805 cs35l56->soft_resetting = true;
808 * Must enter cache-only first so there can't be any more register
809 * accesses other than the controlled system reset sequence below.
811 regcache_cache_only(cs35l56->regmap, true);
812 regmap_multi_reg_write_bypassed(cs35l56->regmap,
813 cs35l56_system_reset_seq,
814 ARRAY_SIZE(cs35l56_system_reset_seq));
816 /* On SoundWire the registers won't be accessible until it re-enumerates. */
817 if (cs35l56->sdw_peripheral)
820 usleep_range(CS35L56_CONTROL_PORT_READY_US, CS35L56_CONTROL_PORT_READY_US + 400);
821 regcache_cache_only(cs35l56->regmap, false);
824 static void cs35l56_secure_patch(struct cs35l56_private *cs35l56)
828 /* Use wm_adsp to load and apply the firmware patch and coefficient files */
829 ret = wm_adsp_power_up(&cs35l56->dsp);
831 dev_dbg(cs35l56->dev, "%s: wm_adsp_power_up ret %d\n", __func__, ret);
833 cs35l56_mbox_send(cs35l56, CS35L56_MBOX_CMD_AUDIO_REINIT);
836 static void cs35l56_patch(struct cs35l56_private *cs35l56)
843 * Disable SoundWire interrupts to prevent race with IRQ work.
844 * Setting sdw_irq_no_unmask prevents the handler re-enabling
845 * the SoundWire interrupt.
847 if (cs35l56->sdw_peripheral) {
848 cs35l56->sdw_irq_no_unmask = true;
849 flush_work(&cs35l56->sdw_irq_work);
850 sdw_write_no_pm(cs35l56->sdw_peripheral, CS35L56_SDW_GEN_INT_MASK_1, 0);
851 sdw_read_no_pm(cs35l56->sdw_peripheral, CS35L56_SDW_GEN_INT_STAT_1);
852 sdw_write_no_pm(cs35l56->sdw_peripheral, CS35L56_SDW_GEN_INT_STAT_1, 0xFF);
853 flush_work(&cs35l56->sdw_irq_work);
856 ret = cs35l56_mbox_send(cs35l56, CS35L56_MBOX_CMD_SHUTDOWN);
860 if (cs35l56->rev < CS35L56_REVID_B0)
861 reg = CS35L56_DSP1_PM_CUR_STATE_A1;
863 reg = CS35L56_DSP1_PM_CUR_STATE;
865 ret = regmap_read_poll_timeout(cs35l56->regmap, reg,
866 val, (val == CS35L56_HALO_STATE_SHUTDOWN),
867 CS35L56_HALO_STATE_POLL_US,
868 CS35L56_HALO_STATE_TIMEOUT_US);
870 dev_err(cs35l56->dev, "Failed to poll PM_CUR_STATE to 1 is %d (ret %d)\n",
873 /* Use wm_adsp to load and apply the firmware patch and coefficient files */
874 ret = wm_adsp_power_up(&cs35l56->dsp);
876 dev_dbg(cs35l56->dev, "%s: wm_adsp_power_up ret %d\n", __func__, ret);
880 mutex_lock(&cs35l56->irq_lock);
882 init_completion(&cs35l56->init_completion);
884 cs35l56_system_reset(cs35l56);
886 if (cs35l56->sdw_peripheral) {
888 * The system-reset causes the CS35L56 to detach from the bus.
889 * Wait for the manager to re-enumerate the CS35L56 and
890 * cs35l56_init() to run again.
892 if (!wait_for_completion_timeout(&cs35l56->init_completion,
893 msecs_to_jiffies(5000))) {
894 dev_err(cs35l56->dev, "%s: init_completion timed out (SDW)\n", __func__);
897 } else if (cs35l56_init(cs35l56)) {
901 regmap_clear_bits(cs35l56->regmap, CS35L56_PROTECTION_STATUS, CS35L56_FIRMWARE_MISSING);
902 cs35l56->fw_patched = true;
905 mutex_unlock(&cs35l56->irq_lock);
907 /* Re-enable SoundWire interrupts */
908 if (cs35l56->sdw_peripheral) {
909 cs35l56->sdw_irq_no_unmask = false;
910 sdw_write_no_pm(cs35l56->sdw_peripheral, CS35L56_SDW_GEN_INT_MASK_1,
911 CS35L56_SDW_INT_MASK_CODEC_IRQ);
915 static void cs35l56_dsp_work(struct work_struct *work)
917 struct cs35l56_private *cs35l56 = container_of(work,
918 struct cs35l56_private,
921 if (!cs35l56->init_done)
924 pm_runtime_get_sync(cs35l56->dev);
927 * When the device is running in secure mode the firmware files can
928 * only contain insecure tunings and therefore we do not need to
929 * shutdown the firmware to apply them and can use the lower cost
930 * reinit sequence instead.
932 if (cs35l56->secured)
933 cs35l56_secure_patch(cs35l56);
935 cs35l56_patch(cs35l56);
937 pm_runtime_mark_last_busy(cs35l56->dev);
938 pm_runtime_put_autosuspend(cs35l56->dev);
941 static int cs35l56_component_probe(struct snd_soc_component *component)
943 struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
944 struct dentry *debugfs_root = component->debugfs_root;
946 BUILD_BUG_ON(ARRAY_SIZE(cs35l56_tx_input_texts) != ARRAY_SIZE(cs35l56_tx_input_values));
948 if (!wait_for_completion_timeout(&cs35l56->init_completion,
949 msecs_to_jiffies(5000))) {
950 dev_err(cs35l56->dev, "%s: init_completion timed out\n", __func__);
954 cs35l56->component = component;
955 wm_adsp2_component_probe(&cs35l56->dsp, component);
957 debugfs_create_bool("init_done", 0444, debugfs_root, &cs35l56->init_done);
958 debugfs_create_bool("can_hibernate", 0444, debugfs_root, &cs35l56->can_hibernate);
959 debugfs_create_bool("fw_patched", 0444, debugfs_root, &cs35l56->fw_patched);
961 queue_work(cs35l56->dsp_wq, &cs35l56->dsp_work);
966 static void cs35l56_component_remove(struct snd_soc_component *component)
968 struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
970 cancel_work_sync(&cs35l56->dsp_work);
973 static int cs35l56_set_bias_level(struct snd_soc_component *component,
974 enum snd_soc_bias_level level)
976 struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(component);
979 case SND_SOC_BIAS_STANDBY:
981 * Wait for patching to complete when transitioning from
982 * BIAS_OFF to BIAS_STANDBY
984 if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF)
985 cs35l56_wait_dsp_ready(cs35l56);
995 static const struct snd_soc_component_driver soc_component_dev_cs35l56 = {
996 .probe = cs35l56_component_probe,
997 .remove = cs35l56_component_remove,
999 .dapm_widgets = cs35l56_dapm_widgets,
1000 .num_dapm_widgets = ARRAY_SIZE(cs35l56_dapm_widgets),
1001 .dapm_routes = cs35l56_audio_map,
1002 .num_dapm_routes = ARRAY_SIZE(cs35l56_audio_map),
1003 .controls = cs35l56_controls,
1004 .num_controls = ARRAY_SIZE(cs35l56_controls),
1006 .set_bias_level = cs35l56_set_bias_level,
1008 .suspend_bias_off = 1, /* see cs35l56_system_resume() */
1011 static const struct reg_sequence cs35l56_hibernate_seq[] = {
1012 /* This must be the last register access */
1013 REG_SEQ0(CS35L56_DSP_VIRTUAL1_MBOX_1, CS35L56_MBOX_CMD_HIBERNATE_NOW),
1016 static const struct reg_sequence cs35l56_hibernate_wake_seq[] = {
1017 REG_SEQ0(CS35L56_DSP_VIRTUAL1_MBOX_1, CS35L56_MBOX_CMD_WAKEUP),
1020 int cs35l56_runtime_suspend(struct device *dev)
1022 struct cs35l56_private *cs35l56 = dev_get_drvdata(dev);
1026 if (!cs35l56->init_done)
1029 /* Firmware must have entered a power-save state */
1030 ret = regmap_read_poll_timeout(cs35l56->regmap,
1031 CS35L56_TRANSDUCER_ACTUAL_PS,
1032 val, (val >= CS35L56_PS3),
1033 CS35L56_PS3_POLL_US,
1034 CS35L56_PS3_TIMEOUT_US);
1036 dev_warn(cs35l56->dev, "PS3 wait failed: %d\n", ret);
1038 /* Clear BOOT_DONE so it can be used to detect a reboot */
1039 regmap_write(cs35l56->regmap, CS35L56_IRQ1_EINT_4, CS35L56_OTP_BOOT_DONE_MASK);
1041 if (!cs35l56->can_hibernate) {
1042 regcache_cache_only(cs35l56->regmap, true);
1043 dev_dbg(dev, "Suspended: no hibernate");
1049 * Enable auto-hibernate. If it is woken by some other wake source
1050 * it will automatically return to hibernate.
1052 cs35l56_mbox_send(cs35l56, CS35L56_MBOX_CMD_ALLOW_AUTO_HIBERNATE);
1055 * Must enter cache-only first so there can't be any more register
1056 * accesses other than the controlled hibernate sequence below.
1058 regcache_cache_only(cs35l56->regmap, true);
1060 regmap_multi_reg_write_bypassed(cs35l56->regmap,
1061 cs35l56_hibernate_seq,
1062 ARRAY_SIZE(cs35l56_hibernate_seq));
1064 dev_dbg(dev, "Suspended: hibernate");
1068 EXPORT_SYMBOL_NS_GPL(cs35l56_runtime_suspend, SND_SOC_CS35L56_CORE);
1070 static int __maybe_unused cs35l56_runtime_resume_i2c_spi(struct device *dev)
1072 struct cs35l56_private *cs35l56 = dev_get_drvdata(dev);
1074 if (!cs35l56->init_done)
1077 return cs35l56_runtime_resume_common(cs35l56);
1080 int cs35l56_runtime_resume_common(struct cs35l56_private *cs35l56)
1085 if (!cs35l56->can_hibernate)
1088 if (!cs35l56->sdw_peripheral) {
1090 * Dummy transaction to trigger I2C/SPI auto-wake. This will NAK on I2C.
1091 * Must be done before releasing cache-only.
1093 regmap_multi_reg_write_bypassed(cs35l56->regmap,
1094 cs35l56_hibernate_wake_seq,
1095 ARRAY_SIZE(cs35l56_hibernate_wake_seq));
1097 usleep_range(CS35L56_CONTROL_PORT_READY_US,
1098 CS35L56_CONTROL_PORT_READY_US + 400);
1102 regcache_cache_only(cs35l56->regmap, false);
1104 ret = cs35l56_wait_for_firmware_boot(cs35l56);
1106 dev_err(cs35l56->dev, "Hibernate wake failed: %d\n", ret);
1110 ret = cs35l56_mbox_send(cs35l56, CS35L56_MBOX_CMD_PREVENT_AUTO_HIBERNATE);
1114 /* BOOT_DONE will be 1 if the amp reset */
1115 regmap_read(cs35l56->regmap, CS35L56_IRQ1_EINT_4, &val);
1116 if (val & CS35L56_OTP_BOOT_DONE_MASK) {
1117 dev_dbg(cs35l56->dev, "Registers reset in suspend\n");
1118 regcache_mark_dirty(cs35l56->regmap);
1121 regcache_sync(cs35l56->regmap);
1123 dev_dbg(cs35l56->dev, "Resumed");
1128 regmap_write(cs35l56->regmap, CS35L56_DSP_VIRTUAL1_MBOX_1,
1129 CS35L56_MBOX_CMD_HIBERNATE_NOW);
1131 regcache_cache_only(cs35l56->regmap, true);
1135 EXPORT_SYMBOL_NS_GPL(cs35l56_runtime_resume_common, SND_SOC_CS35L56_CORE);
1137 static int cs35l56_is_fw_reload_needed(struct cs35l56_private *cs35l56)
1142 /* Nothing to re-patch if we haven't done any patching yet. */
1143 if (!cs35l56->fw_patched)
1147 * If we have control of RESET we will have asserted it so the firmware
1148 * will need re-patching.
1150 if (cs35l56->reset_gpio)
1154 * In secure mode FIRMWARE_MISSING is cleared by the BIOS loader so
1155 * can't be used here to test for memory retention.
1156 * Assume that tuning must be re-loaded.
1158 if (cs35l56->secured)
1161 ret = pm_runtime_resume_and_get(cs35l56->dev);
1163 dev_err(cs35l56->dev, "Failed to runtime_get: %d\n", ret);
1167 ret = regmap_read(cs35l56->regmap, CS35L56_PROTECTION_STATUS, &val);
1169 dev_err(cs35l56->dev, "Failed to read PROTECTION_STATUS: %d\n", ret);
1171 ret = !!(val & CS35L56_FIRMWARE_MISSING);
1173 pm_runtime_put_autosuspend(cs35l56->dev);
1178 int cs35l56_system_suspend(struct device *dev)
1180 struct cs35l56_private *cs35l56 = dev_get_drvdata(dev);
1182 dev_dbg(dev, "system_suspend\n");
1184 if (cs35l56->component)
1185 flush_work(&cs35l56->dsp_work);
1188 * The interrupt line is normally shared, but after we start suspending
1189 * we can't check if our device is the source of an interrupt, and can't
1190 * clear it. Prevent this race by temporarily disabling the parent irq
1191 * until we reach _no_irq.
1194 disable_irq(cs35l56->irq);
1196 return pm_runtime_force_suspend(dev);
1198 EXPORT_SYMBOL_GPL(cs35l56_system_suspend);
1200 int cs35l56_system_suspend_late(struct device *dev)
1202 struct cs35l56_private *cs35l56 = dev_get_drvdata(dev);
1204 dev_dbg(dev, "system_suspend_late\n");
1207 * Assert RESET before removing supplies.
1208 * RESET is usually shared by all amps so it must not be asserted until
1209 * all driver instances have done their suspend() stage.
1211 if (cs35l56->reset_gpio) {
1212 gpiod_set_value_cansleep(cs35l56->reset_gpio, 0);
1213 cs35l56_wait_min_reset_pulse();
1216 regulator_bulk_disable(ARRAY_SIZE(cs35l56->supplies), cs35l56->supplies);
1220 EXPORT_SYMBOL_GPL(cs35l56_system_suspend_late);
1222 int cs35l56_system_suspend_no_irq(struct device *dev)
1224 struct cs35l56_private *cs35l56 = dev_get_drvdata(dev);
1226 dev_dbg(dev, "system_suspend_no_irq\n");
1228 /* Handlers are now disabled so the parent IRQ can safely be re-enabled. */
1230 enable_irq(cs35l56->irq);
1234 EXPORT_SYMBOL_GPL(cs35l56_system_suspend_no_irq);
1236 int cs35l56_system_resume_no_irq(struct device *dev)
1238 struct cs35l56_private *cs35l56 = dev_get_drvdata(dev);
1240 dev_dbg(dev, "system_resume_no_irq\n");
1243 * WAKE interrupts unmask if the CS35L56 hibernates, which can cause
1244 * spurious interrupts, and the interrupt line is normally shared.
1245 * We can't check if our device is the source of an interrupt, and can't
1246 * clear it, until it has fully resumed. Prevent this race by temporarily
1247 * disabling the parent irq until we complete resume().
1250 disable_irq(cs35l56->irq);
1254 EXPORT_SYMBOL_GPL(cs35l56_system_resume_no_irq);
1256 int cs35l56_system_resume_early(struct device *dev)
1258 struct cs35l56_private *cs35l56 = dev_get_drvdata(dev);
1261 dev_dbg(dev, "system_resume_early\n");
1263 /* Ensure a spec-compliant RESET pulse. */
1264 if (cs35l56->reset_gpio) {
1265 gpiod_set_value_cansleep(cs35l56->reset_gpio, 0);
1266 cs35l56_wait_min_reset_pulse();
1269 /* Enable supplies before releasing RESET. */
1270 ret = regulator_bulk_enable(ARRAY_SIZE(cs35l56->supplies), cs35l56->supplies);
1272 dev_err(dev, "system_resume_early failed to enable supplies: %d\n", ret);
1276 /* Release shared RESET before drivers start resume(). */
1277 gpiod_set_value_cansleep(cs35l56->reset_gpio, 1);
1281 EXPORT_SYMBOL_GPL(cs35l56_system_resume_early);
1283 int cs35l56_system_resume(struct device *dev)
1285 struct cs35l56_private *cs35l56 = dev_get_drvdata(dev);
1288 dev_dbg(dev, "system_resume\n");
1290 /* Undo pm_runtime_force_suspend() before re-enabling the irq */
1291 ret = pm_runtime_force_resume(dev);
1293 enable_irq(cs35l56->irq);
1298 /* Firmware won't have been loaded if the component hasn't probed */
1299 if (!cs35l56->component)
1302 ret = cs35l56_is_fw_reload_needed(cs35l56);
1303 dev_dbg(cs35l56->dev, "fw_reload_needed: %d\n", ret);
1307 cs35l56->fw_patched = false;
1308 queue_work(cs35l56->dsp_wq, &cs35l56->dsp_work);
1311 * suspend_bias_off ensures we are now in BIAS_OFF so there will be
1312 * a BIAS_OFF->BIAS_STANDBY transition to complete dsp patching.
1317 EXPORT_SYMBOL_GPL(cs35l56_system_resume);
1319 static int cs35l56_dsp_init(struct cs35l56_private *cs35l56)
1321 struct wm_adsp *dsp;
1324 cs35l56->dsp_wq = create_singlethread_workqueue("cs35l56-dsp");
1325 if (!cs35l56->dsp_wq)
1328 INIT_WORK(&cs35l56->dsp_work, cs35l56_dsp_work);
1330 dsp = &cs35l56->dsp;
1331 dsp->part = "cs35l56";
1332 dsp->cs_dsp.num = 1;
1333 dsp->cs_dsp.type = WMFW_HALO;
1334 dsp->cs_dsp.rev = 0;
1336 dsp->cs_dsp.dev = cs35l56->dev;
1337 dsp->cs_dsp.regmap = cs35l56->regmap;
1338 dsp->cs_dsp.base = CS35L56_DSP1_CORE_BASE;
1339 dsp->cs_dsp.base_sysinfo = CS35L56_DSP1_SYS_INFO_ID;
1340 dsp->cs_dsp.mem = cs35l56_dsp1_regions;
1341 dsp->cs_dsp.num_mems = ARRAY_SIZE(cs35l56_dsp1_regions);
1342 dsp->cs_dsp.no_core_startstop = true;
1343 dsp->wmfw_optional = true;
1345 dev_dbg(cs35l56->dev, "DSP system name: '%s'\n", dsp->system_name);
1347 ret = wm_halo_init(dsp);
1349 dev_err(cs35l56->dev, "wm_halo_init failed\n");
1356 static int cs35l56_get_firmware_uid(struct cs35l56_private *cs35l56)
1358 struct device *dev = cs35l56->dev;
1362 ret = device_property_read_string(dev, "cirrus,firmware-uid", &prop);
1363 /* If bad sw node property, return 0 and fallback to legacy firmware path */
1367 cs35l56->dsp.system_name = devm_kstrdup(dev, prop, GFP_KERNEL);
1368 if (cs35l56->dsp.system_name == NULL)
1371 dev_dbg(dev, "Firmware UID: %s\n", cs35l56->dsp.system_name);
1376 int cs35l56_common_probe(struct cs35l56_private *cs35l56)
1380 init_completion(&cs35l56->init_completion);
1381 mutex_init(&cs35l56->irq_lock);
1383 dev_set_drvdata(cs35l56->dev, cs35l56);
1385 cs35l56_fill_supply_names(cs35l56->supplies);
1386 ret = devm_regulator_bulk_get(cs35l56->dev, ARRAY_SIZE(cs35l56->supplies),
1389 return dev_err_probe(cs35l56->dev, ret, "Failed to request supplies\n");
1391 /* Reset could be controlled by the BIOS or shared by multiple amps */
1392 cs35l56->reset_gpio = devm_gpiod_get_optional(cs35l56->dev, "reset", GPIOD_OUT_LOW);
1393 if (IS_ERR(cs35l56->reset_gpio)) {
1394 ret = PTR_ERR(cs35l56->reset_gpio);
1396 * If RESET is shared the first amp to probe will grab the reset
1397 * line and reset all the amps
1400 return dev_err_probe(cs35l56->dev, ret, "Failed to get reset GPIO\n");
1402 dev_info(cs35l56->dev, "Reset GPIO busy, assume shared reset\n");
1403 cs35l56->reset_gpio = NULL;
1406 ret = regulator_bulk_enable(ARRAY_SIZE(cs35l56->supplies), cs35l56->supplies);
1408 return dev_err_probe(cs35l56->dev, ret, "Failed to enable supplies\n");
1410 if (cs35l56->reset_gpio) {
1411 cs35l56_wait_min_reset_pulse();
1412 gpiod_set_value_cansleep(cs35l56->reset_gpio, 1);
1415 ret = cs35l56_get_firmware_uid(cs35l56);
1419 ret = cs35l56_dsp_init(cs35l56);
1421 dev_err_probe(cs35l56->dev, ret, "DSP init failed\n");
1425 ret = devm_snd_soc_register_component(cs35l56->dev,
1426 &soc_component_dev_cs35l56,
1427 cs35l56_dai, ARRAY_SIZE(cs35l56_dai));
1429 dev_err_probe(cs35l56->dev, ret, "Register codec failed\n");
1436 gpiod_set_value_cansleep(cs35l56->reset_gpio, 0);
1437 regulator_bulk_disable(ARRAY_SIZE(cs35l56->supplies), cs35l56->supplies);
1441 EXPORT_SYMBOL_NS_GPL(cs35l56_common_probe, SND_SOC_CS35L56_CORE);
1443 int cs35l56_init(struct cs35l56_private *cs35l56)
1446 unsigned int devid, revid, otpid, secured;
1449 * Check whether the actions associated with soft reset or one time
1450 * init need to be performed.
1452 if (cs35l56->soft_resetting)
1453 goto post_soft_reset;
1455 if (cs35l56->init_done)
1458 pm_runtime_set_autosuspend_delay(cs35l56->dev, 100);
1459 pm_runtime_use_autosuspend(cs35l56->dev);
1460 pm_runtime_set_active(cs35l56->dev);
1461 pm_runtime_enable(cs35l56->dev);
1464 * If the system is not using a reset_gpio then issue a
1465 * dummy read to force a wakeup.
1467 if (!cs35l56->reset_gpio)
1468 regmap_read(cs35l56->regmap, CS35L56_DSP_VIRTUAL1_MBOX_1, &devid);
1470 /* Wait for control port to be ready (datasheet tIRS). */
1471 usleep_range(CS35L56_CONTROL_PORT_READY_US,
1472 CS35L56_CONTROL_PORT_READY_US + 400);
1475 * The HALO_STATE register is in different locations on Ax and B0
1476 * devices so the REVID needs to be determined before waiting for the
1479 ret = regmap_read(cs35l56->regmap, CS35L56_REVID, &revid);
1481 dev_err(cs35l56->dev, "Get Revision ID failed\n");
1484 cs35l56->rev = revid & (CS35L56_AREVID_MASK | CS35L56_MTLREVID_MASK);
1486 ret = cs35l56_wait_for_firmware_boot(cs35l56);
1490 ret = regmap_read(cs35l56->regmap, CS35L56_DEVID, &devid);
1492 dev_err(cs35l56->dev, "Get Device ID failed\n");
1495 devid &= CS35L56_DEVID_MASK;
1501 dev_err(cs35l56->dev, "Unknown device %x\n", devid);
1505 ret = regmap_read(cs35l56->regmap, CS35L56_DSP_RESTRICT_STS1, &secured);
1507 dev_err(cs35l56->dev, "Get Secure status failed\n");
1511 /* When any bus is restricted treat the device as secured */
1512 if (secured & CS35L56_RESTRICTED_MASK)
1513 cs35l56->secured = true;
1515 ret = regmap_read(cs35l56->regmap, CS35L56_OTPID, &otpid);
1517 dev_err(cs35l56->dev, "Get OTP ID failed\n");
1521 dev_info(cs35l56->dev, "Cirrus Logic CS35L56%s Rev %02X OTP%d\n",
1522 cs35l56->secured ? "s" : "", cs35l56->rev, otpid);
1524 /* Populate the DSP information with the revision and security state */
1525 cs35l56->dsp.part = devm_kasprintf(cs35l56->dev, GFP_KERNEL, "cs35l56%s-%02x",
1526 cs35l56->secured ? "s" : "", cs35l56->rev);
1527 if (!cs35l56->dsp.part)
1530 /* Wake source and *_BLOCKED interrupts default to unmasked, so mask them */
1531 regmap_write(cs35l56->regmap, CS35L56_IRQ1_MASK_20, 0xffffffff);
1532 regmap_update_bits(cs35l56->regmap, CS35L56_IRQ1_MASK_1,
1533 CS35L56_AMP_SHORT_ERR_EINT1_MASK,
1535 regmap_update_bits(cs35l56->regmap, CS35L56_IRQ1_MASK_8,
1536 CS35L56_TEMP_ERR_EINT1_MASK,
1539 if (!cs35l56->reset_gpio) {
1540 dev_dbg(cs35l56->dev, "No reset gpio: using soft reset\n");
1541 cs35l56_system_reset(cs35l56);
1542 if (cs35l56->sdw_peripheral) {
1543 /* Keep alive while we wait for re-enumeration */
1544 pm_runtime_get_noresume(cs35l56->dev);
1550 if (cs35l56->soft_resetting) {
1551 cs35l56->soft_resetting = false;
1553 /* Done re-enumerating after one-time init so release the keep-alive */
1554 if (cs35l56->sdw_peripheral && !cs35l56->init_done)
1555 pm_runtime_put_noidle(cs35l56->dev);
1557 regcache_mark_dirty(cs35l56->regmap);
1558 ret = cs35l56_wait_for_firmware_boot(cs35l56);
1562 dev_dbg(cs35l56->dev, "Firmware rebooted after soft reset\n");
1565 /* Disable auto-hibernate so that runtime_pm has control */
1566 ret = cs35l56_mbox_send(cs35l56, CS35L56_MBOX_CMD_PREVENT_AUTO_HIBERNATE);
1570 /* Populate soft registers in the regmap cache */
1571 cs35l56_reread_firmware_registers(cs35l56->dev, cs35l56->regmap);
1573 /* Registers could be dirty after soft reset or SoundWire enumeration */
1574 regcache_sync(cs35l56->regmap);
1576 cs35l56->init_done = true;
1577 complete(&cs35l56->init_completion);
1581 EXPORT_SYMBOL_NS_GPL(cs35l56_init, SND_SOC_CS35L56_CORE);
1583 void cs35l56_remove(struct cs35l56_private *cs35l56)
1585 cs35l56->init_done = false;
1588 * WAKE IRQs unmask if CS35L56 hibernates so free the handler to
1589 * prevent it racing with remove().
1592 devm_free_irq(cs35l56->dev, cs35l56->irq, cs35l56);
1594 flush_workqueue(cs35l56->dsp_wq);
1595 destroy_workqueue(cs35l56->dsp_wq);
1597 pm_runtime_suspend(cs35l56->dev);
1598 pm_runtime_disable(cs35l56->dev);
1600 regcache_cache_only(cs35l56->regmap, true);
1602 gpiod_set_value_cansleep(cs35l56->reset_gpio, 0);
1603 regulator_bulk_disable(ARRAY_SIZE(cs35l56->supplies), cs35l56->supplies);
1605 EXPORT_SYMBOL_NS_GPL(cs35l56_remove, SND_SOC_CS35L56_CORE);
1607 const struct dev_pm_ops cs35l56_pm_ops_i2c_spi = {
1608 SET_RUNTIME_PM_OPS(cs35l56_runtime_suspend, cs35l56_runtime_resume_i2c_spi, NULL)
1609 SYSTEM_SLEEP_PM_OPS(cs35l56_system_suspend, cs35l56_system_resume)
1610 LATE_SYSTEM_SLEEP_PM_OPS(cs35l56_system_suspend_late, cs35l56_system_resume_early)
1611 NOIRQ_SYSTEM_SLEEP_PM_OPS(cs35l56_system_suspend_no_irq, cs35l56_system_resume_no_irq)
1613 EXPORT_SYMBOL_NS_GPL(cs35l56_pm_ops_i2c_spi, SND_SOC_CS35L56_CORE);
1615 MODULE_DESCRIPTION("ASoC CS35L56 driver");
1616 MODULE_IMPORT_NS(SND_SOC_CS35L56_SHARED);
1617 MODULE_AUTHOR("Richard Fitzgerald <rf@opensource.cirrus.com>");
1618 MODULE_AUTHOR("Simon Trimmer <simont@opensource.cirrus.com>");
1619 MODULE_LICENSE("GPL");