1 // SPDX-License-Identifier: GPL-2.0
3 // cs35l41.c -- CS35l41 ALSA SoC audio driver
5 // Copyright 2017-2021 Cirrus Logic, Inc.
7 // Author: David Rhodes <david.rhodes@cirrus.com>
9 #include <linux/acpi.h>
10 #include <linux/delay.h>
11 #include <linux/err.h>
12 #include <linux/init.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/of_device.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/property.h>
19 #include <sound/initval.h>
20 #include <sound/pcm.h>
21 #include <sound/pcm_params.h>
22 #include <sound/soc.h>
23 #include <sound/soc-dapm.h>
24 #include <sound/tlv.h>
28 static const char * const cs35l41_supplies[CS35L41_NUM_SUPPLIES] = {
33 struct cs35l41_pll_sysclk_config {
38 static const struct cs35l41_pll_sysclk_config cs35l41_pll_sysclk[] = {
105 struct cs35l41_fs_mon_config {
111 static const struct cs35l41_fs_mon_config cs35l41_fs_mon[] = {
112 { 32768, 2254, 3754 },
113 { 8000, 9220, 15364 },
114 { 11025, 6148, 10244 },
115 { 12000, 6148, 10244 },
116 { 16000, 4612, 7684 },
117 { 22050, 3076, 5124 },
118 { 24000, 3076, 5124 },
119 { 32000, 2308, 3844 },
120 { 44100, 1540, 2564 },
121 { 48000, 1540, 2564 },
122 { 88200, 772, 1284 },
123 { 96000, 772, 1284 },
124 { 128000, 580, 964 },
125 { 176400, 388, 644 },
126 { 192000, 388, 644 },
127 { 256000, 292, 484 },
128 { 352800, 196, 324 },
129 { 384000, 196, 324 },
130 { 512000, 148, 244 },
131 { 705600, 100, 164 },
132 { 750000, 100, 164 },
133 { 768000, 100, 164 },
134 { 1000000, 76, 124 },
135 { 1024000, 76, 124 },
136 { 1200000, 64, 104 },
156 static int cs35l41_get_fs_mon_config_index(int freq)
160 for (i = 0; i < ARRAY_SIZE(cs35l41_fs_mon); i++) {
161 if (cs35l41_fs_mon[i].freq == freq)
168 static const DECLARE_TLV_DB_RANGE(dig_vol_tlv,
169 0, 0, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE, 0, 1),
170 1, 913, TLV_DB_MINMAX_ITEM(-10200, 1200));
171 static DECLARE_TLV_DB_SCALE(amp_gain_tlv, 50, 100, 0);
173 static const struct snd_kcontrol_new dre_ctrl =
174 SOC_DAPM_SINGLE("Switch", CS35L41_PWR_CTRL3, 20, 1, 0);
176 static const char * const cs35l41_pcm_sftramp_text[] = {
177 "Off", ".5ms", "1ms", "2ms", "4ms", "8ms", "15ms", "30ms"
180 static SOC_ENUM_SINGLE_DECL(pcm_sft_ramp,
181 CS35L41_AMP_DIG_VOL_CTRL, 0,
182 cs35l41_pcm_sftramp_text);
184 static int cs35l41_dsp_preload_ev(struct snd_soc_dapm_widget *w,
185 struct snd_kcontrol *kcontrol, int event)
187 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
188 struct cs35l41_private *cs35l41 = snd_soc_component_get_drvdata(component);
192 case SND_SOC_DAPM_PRE_PMU:
193 if (cs35l41->dsp.cs_dsp.booted)
196 return wm_adsp_early_event(w, kcontrol, event);
197 case SND_SOC_DAPM_PRE_PMD:
198 if (cs35l41->dsp.preloaded)
201 if (cs35l41->dsp.cs_dsp.running) {
202 ret = wm_adsp_event(w, kcontrol, event);
207 return wm_adsp_early_event(w, kcontrol, event);
213 static int cs35l41_dsp_audio_ev(struct snd_soc_dapm_widget *w,
214 struct snd_kcontrol *kcontrol, int event)
216 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
217 struct cs35l41_private *cs35l41 = snd_soc_component_get_drvdata(component);
218 unsigned int fw_status;
222 case SND_SOC_DAPM_POST_PMU:
223 if (!cs35l41->dsp.cs_dsp.running)
224 return wm_adsp_event(w, kcontrol, event);
226 ret = regmap_read(cs35l41->regmap, CS35L41_DSP_MBOX_2, &fw_status);
228 dev_err(cs35l41->dev,
229 "Failed to read firmware status: %d\n", ret);
234 case CSPL_MBOX_STS_RUNNING:
235 case CSPL_MBOX_STS_PAUSED:
238 dev_err(cs35l41->dev, "Firmware status is invalid: %u\n",
243 return cs35l41_set_cspl_mbox_cmd(cs35l41->dev, cs35l41->regmap,
244 CSPL_MBOX_CMD_RESUME);
245 case SND_SOC_DAPM_PRE_PMD:
246 return cs35l41_set_cspl_mbox_cmd(cs35l41->dev, cs35l41->regmap,
247 CSPL_MBOX_CMD_PAUSE);
253 static const char * const cs35l41_pcm_source_texts[] = {"ASP", "DSP"};
254 static const unsigned int cs35l41_pcm_source_values[] = {0x08, 0x32};
255 static SOC_VALUE_ENUM_SINGLE_DECL(cs35l41_pcm_source_enum,
256 CS35L41_DAC_PCM1_SRC,
257 0, CS35L41_ASP_SOURCE_MASK,
258 cs35l41_pcm_source_texts,
259 cs35l41_pcm_source_values);
261 static const struct snd_kcontrol_new pcm_source_mux =
262 SOC_DAPM_ENUM("PCM Source", cs35l41_pcm_source_enum);
264 static const char * const cs35l41_tx_input_texts[] = {
265 "Zero", "ASPRX1", "ASPRX2", "VMON", "IMON",
266 "VPMON", "VBSTMON", "DSPTX1", "DSPTX2"
269 static const unsigned int cs35l41_tx_input_values[] = {
270 0x00, CS35L41_INPUT_SRC_ASPRX1, CS35L41_INPUT_SRC_ASPRX2,
271 CS35L41_INPUT_SRC_VMON, CS35L41_INPUT_SRC_IMON, CS35L41_INPUT_SRC_VPMON,
272 CS35L41_INPUT_SRC_VBSTMON, CS35L41_INPUT_DSP_TX1, CS35L41_INPUT_DSP_TX2
275 static SOC_VALUE_ENUM_SINGLE_DECL(cs35l41_asptx1_enum,
277 0, CS35L41_ASP_SOURCE_MASK,
278 cs35l41_tx_input_texts,
279 cs35l41_tx_input_values);
281 static const struct snd_kcontrol_new asp_tx1_mux =
282 SOC_DAPM_ENUM("ASPTX1 SRC", cs35l41_asptx1_enum);
284 static SOC_VALUE_ENUM_SINGLE_DECL(cs35l41_asptx2_enum,
286 0, CS35L41_ASP_SOURCE_MASK,
287 cs35l41_tx_input_texts,
288 cs35l41_tx_input_values);
290 static const struct snd_kcontrol_new asp_tx2_mux =
291 SOC_DAPM_ENUM("ASPTX2 SRC", cs35l41_asptx2_enum);
293 static SOC_VALUE_ENUM_SINGLE_DECL(cs35l41_asptx3_enum,
295 0, CS35L41_ASP_SOURCE_MASK,
296 cs35l41_tx_input_texts,
297 cs35l41_tx_input_values);
299 static const struct snd_kcontrol_new asp_tx3_mux =
300 SOC_DAPM_ENUM("ASPTX3 SRC", cs35l41_asptx3_enum);
302 static SOC_VALUE_ENUM_SINGLE_DECL(cs35l41_asptx4_enum,
304 0, CS35L41_ASP_SOURCE_MASK,
305 cs35l41_tx_input_texts,
306 cs35l41_tx_input_values);
308 static const struct snd_kcontrol_new asp_tx4_mux =
309 SOC_DAPM_ENUM("ASPTX4 SRC", cs35l41_asptx4_enum);
311 static SOC_VALUE_ENUM_SINGLE_DECL(cs35l41_dsprx1_enum,
312 CS35L41_DSP1_RX1_SRC,
313 0, CS35L41_ASP_SOURCE_MASK,
314 cs35l41_tx_input_texts,
315 cs35l41_tx_input_values);
317 static const struct snd_kcontrol_new dsp_rx1_mux =
318 SOC_DAPM_ENUM("DSPRX1 SRC", cs35l41_dsprx1_enum);
320 static SOC_VALUE_ENUM_SINGLE_DECL(cs35l41_dsprx2_enum,
321 CS35L41_DSP1_RX2_SRC,
322 0, CS35L41_ASP_SOURCE_MASK,
323 cs35l41_tx_input_texts,
324 cs35l41_tx_input_values);
326 static const struct snd_kcontrol_new dsp_rx2_mux =
327 SOC_DAPM_ENUM("DSPRX2 SRC", cs35l41_dsprx2_enum);
329 static const struct snd_kcontrol_new cs35l41_aud_controls[] = {
330 SOC_SINGLE_SX_TLV("Digital PCM Volume", CS35L41_AMP_DIG_VOL_CTRL,
331 3, 0x4CF, 0x391, dig_vol_tlv),
332 SOC_SINGLE_TLV("Analog PCM Volume", CS35L41_AMP_GAIN_CTRL, 5, 0x14, 0,
334 SOC_ENUM("PCM Soft Ramp", pcm_sft_ramp),
335 SOC_SINGLE("HW Noise Gate Enable", CS35L41_NG_CFG, 8, 63, 0),
336 SOC_SINGLE("HW Noise Gate Delay", CS35L41_NG_CFG, 4, 7, 0),
337 SOC_SINGLE("HW Noise Gate Threshold", CS35L41_NG_CFG, 0, 7, 0),
338 SOC_SINGLE("Aux Noise Gate CH1 Switch",
339 CS35L41_MIXER_NGATE_CH1_CFG, 16, 1, 0),
340 SOC_SINGLE("Aux Noise Gate CH1 Entry Delay",
341 CS35L41_MIXER_NGATE_CH1_CFG, 8, 15, 0),
342 SOC_SINGLE("Aux Noise Gate CH1 Threshold",
343 CS35L41_MIXER_NGATE_CH1_CFG, 0, 7, 0),
344 SOC_SINGLE("Aux Noise Gate CH2 Entry Delay",
345 CS35L41_MIXER_NGATE_CH2_CFG, 8, 15, 0),
346 SOC_SINGLE("Aux Noise Gate CH2 Switch",
347 CS35L41_MIXER_NGATE_CH2_CFG, 16, 1, 0),
348 SOC_SINGLE("Aux Noise Gate CH2 Threshold",
349 CS35L41_MIXER_NGATE_CH2_CFG, 0, 7, 0),
350 SOC_SINGLE("SCLK Force Switch", CS35L41_SP_FORMAT, CS35L41_SCLK_FRC_SHIFT, 1, 0),
351 SOC_SINGLE("LRCLK Force Switch", CS35L41_SP_FORMAT, CS35L41_LRCLK_FRC_SHIFT, 1, 0),
352 SOC_SINGLE("Invert Class D Switch", CS35L41_AMP_DIG_VOL_CTRL,
353 CS35L41_AMP_INV_PCM_SHIFT, 1, 0),
354 SOC_SINGLE("Amp Gain ZC Switch", CS35L41_AMP_GAIN_CTRL,
355 CS35L41_AMP_GAIN_ZC_SHIFT, 1, 0),
356 WM_ADSP2_PRELOAD_SWITCH("DSP1", 1),
357 WM_ADSP_FW_CONTROL("DSP1", 0),
360 static void cs35l41_boost_enable(struct cs35l41_private *cs35l41, unsigned int enable)
362 switch (cs35l41->hw_cfg.bst_type) {
363 case CS35L41_INT_BOOST:
364 case CS35L41_SHD_BOOST_ACTV:
365 enable = enable ? CS35L41_BST_EN_DEFAULT : CS35L41_BST_DIS_FET_OFF;
366 regmap_update_bits(cs35l41->regmap, CS35L41_PWR_CTRL2, CS35L41_BST_EN_MASK,
367 enable << CS35L41_BST_EN_SHIFT);
375 static void cs35l41_error_release(struct cs35l41_private *cs35l41, unsigned int irq_err_bit,
376 unsigned int rel_err_bit)
378 regmap_write(cs35l41->regmap, CS35L41_IRQ1_STATUS1, irq_err_bit);
379 regmap_write(cs35l41->regmap, CS35L41_PROTECT_REL_ERR_IGN, 0);
380 regmap_update_bits(cs35l41->regmap, CS35L41_PROTECT_REL_ERR_IGN, rel_err_bit, rel_err_bit);
381 regmap_update_bits(cs35l41->regmap, CS35L41_PROTECT_REL_ERR_IGN, rel_err_bit, 0);
384 static irqreturn_t cs35l41_irq(int irq, void *data)
386 struct cs35l41_private *cs35l41 = data;
387 unsigned int status[4] = { 0, 0, 0, 0 };
388 unsigned int masks[4] = { 0, 0, 0, 0 };
392 pm_runtime_get_sync(cs35l41->dev);
394 for (i = 0; i < ARRAY_SIZE(status); i++) {
395 regmap_read(cs35l41->regmap,
396 CS35L41_IRQ1_STATUS1 + (i * CS35L41_REGSTRIDE),
398 regmap_read(cs35l41->regmap,
399 CS35L41_IRQ1_MASK1 + (i * CS35L41_REGSTRIDE),
403 /* Check to see if unmasked bits are active */
404 if (!(status[0] & ~masks[0]) && !(status[1] & ~masks[1]) &&
405 !(status[2] & ~masks[2]) && !(status[3] & ~masks[3]))
408 if (status[3] & CS35L41_OTP_BOOT_DONE) {
409 regmap_update_bits(cs35l41->regmap, CS35L41_IRQ1_MASK4,
410 CS35L41_OTP_BOOT_DONE, CS35L41_OTP_BOOT_DONE);
414 * The following interrupts require a
415 * protection release cycle to get the
416 * speaker out of Safe-Mode.
418 if (status[0] & CS35L41_AMP_SHORT_ERR) {
419 dev_crit_ratelimited(cs35l41->dev, "Amp short error\n");
420 cs35l41_error_release(cs35l41, CS35L41_AMP_SHORT_ERR, CS35L41_AMP_SHORT_ERR_RLS);
424 if (status[0] & CS35L41_TEMP_WARN) {
425 dev_crit_ratelimited(cs35l41->dev, "Over temperature warning\n");
426 cs35l41_error_release(cs35l41, CS35L41_TEMP_WARN, CS35L41_TEMP_WARN_ERR_RLS);
430 if (status[0] & CS35L41_TEMP_ERR) {
431 dev_crit_ratelimited(cs35l41->dev, "Over temperature error\n");
432 cs35l41_error_release(cs35l41, CS35L41_TEMP_ERR, CS35L41_TEMP_ERR_RLS);
436 if (status[0] & CS35L41_BST_OVP_ERR) {
437 dev_crit_ratelimited(cs35l41->dev, "VBST Over Voltage error\n");
438 cs35l41_boost_enable(cs35l41, 0);
439 cs35l41_error_release(cs35l41, CS35L41_BST_OVP_ERR, CS35L41_BST_OVP_ERR_RLS);
440 cs35l41_boost_enable(cs35l41, 1);
444 if (status[0] & CS35L41_BST_DCM_UVP_ERR) {
445 dev_crit_ratelimited(cs35l41->dev, "DCM VBST Under Voltage Error\n");
446 cs35l41_boost_enable(cs35l41, 0);
447 cs35l41_error_release(cs35l41, CS35L41_BST_DCM_UVP_ERR, CS35L41_BST_UVP_ERR_RLS);
448 cs35l41_boost_enable(cs35l41, 1);
452 if (status[0] & CS35L41_BST_SHORT_ERR) {
453 dev_crit_ratelimited(cs35l41->dev, "LBST error: powering off!\n");
454 cs35l41_boost_enable(cs35l41, 0);
455 cs35l41_error_release(cs35l41, CS35L41_BST_SHORT_ERR, CS35L41_BST_SHORT_ERR_RLS);
456 cs35l41_boost_enable(cs35l41, 1);
460 if (status[2] & CS35L41_PLL_LOCK) {
461 regmap_write(cs35l41->regmap, CS35L41_IRQ1_STATUS3, CS35L41_PLL_LOCK);
462 complete(&cs35l41->pll_lock);
467 pm_runtime_mark_last_busy(cs35l41->dev);
468 pm_runtime_put_autosuspend(cs35l41->dev);
473 static const struct reg_sequence cs35l41_pup_patch[] = {
474 { CS35L41_TEST_KEY_CTL, 0x00000055 },
475 { CS35L41_TEST_KEY_CTL, 0x000000AA },
476 { 0x00002084, 0x002F1AA0 },
477 { CS35L41_TEST_KEY_CTL, 0x000000CC },
478 { CS35L41_TEST_KEY_CTL, 0x00000033 },
481 static const struct reg_sequence cs35l41_pdn_patch[] = {
482 { CS35L41_TEST_KEY_CTL, 0x00000055 },
483 { CS35L41_TEST_KEY_CTL, 0x000000AA },
484 { 0x00002084, 0x002F1AA3 },
485 { CS35L41_TEST_KEY_CTL, 0x000000CC },
486 { CS35L41_TEST_KEY_CTL, 0x00000033 },
489 static int cs35l41_main_amp_event(struct snd_soc_dapm_widget *w,
490 struct snd_kcontrol *kcontrol, int event)
492 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
493 struct cs35l41_private *cs35l41 = snd_soc_component_get_drvdata(component);
497 case SND_SOC_DAPM_PRE_PMU:
498 regmap_multi_reg_write_bypassed(cs35l41->regmap,
500 ARRAY_SIZE(cs35l41_pup_patch));
502 ret = cs35l41_global_enable(cs35l41->dev, cs35l41->regmap, cs35l41->hw_cfg.bst_type,
503 1, &cs35l41->pll_lock, cs35l41->dsp.cs_dsp.running);
505 case SND_SOC_DAPM_POST_PMD:
506 ret = cs35l41_global_enable(cs35l41->dev, cs35l41->regmap, cs35l41->hw_cfg.bst_type,
507 0, &cs35l41->pll_lock, cs35l41->dsp.cs_dsp.running);
509 regmap_multi_reg_write_bypassed(cs35l41->regmap,
511 ARRAY_SIZE(cs35l41_pdn_patch));
514 dev_err(cs35l41->dev, "Invalid event = 0x%x\n", event);
521 static const struct snd_soc_dapm_widget cs35l41_dapm_widgets[] = {
522 SND_SOC_DAPM_SPK("DSP1 Preload", NULL),
523 SND_SOC_DAPM_SUPPLY_S("DSP1 Preloader", 100, SND_SOC_NOPM, 0, 0,
524 cs35l41_dsp_preload_ev,
525 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_PRE_PMD),
526 SND_SOC_DAPM_OUT_DRV_E("DSP1", SND_SOC_NOPM, 0, 0, NULL, 0,
527 cs35l41_dsp_audio_ev,
528 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
530 SND_SOC_DAPM_OUTPUT("SPK"),
532 SND_SOC_DAPM_AIF_IN("ASPRX1", NULL, 0, CS35L41_SP_ENABLES, 16, 0),
533 SND_SOC_DAPM_AIF_IN("ASPRX2", NULL, 0, CS35L41_SP_ENABLES, 17, 0),
534 SND_SOC_DAPM_AIF_OUT("ASPTX1", NULL, 0, CS35L41_SP_ENABLES, 0, 0),
535 SND_SOC_DAPM_AIF_OUT("ASPTX2", NULL, 0, CS35L41_SP_ENABLES, 1, 0),
536 SND_SOC_DAPM_AIF_OUT("ASPTX3", NULL, 0, CS35L41_SP_ENABLES, 2, 0),
537 SND_SOC_DAPM_AIF_OUT("ASPTX4", NULL, 0, CS35L41_SP_ENABLES, 3, 0),
539 SND_SOC_DAPM_SIGGEN("VSENSE"),
540 SND_SOC_DAPM_SIGGEN("ISENSE"),
541 SND_SOC_DAPM_SIGGEN("VP"),
542 SND_SOC_DAPM_SIGGEN("VBST"),
543 SND_SOC_DAPM_SIGGEN("TEMP"),
545 SND_SOC_DAPM_SUPPLY("VMON", CS35L41_PWR_CTRL2, 12, 0, NULL, 0),
546 SND_SOC_DAPM_SUPPLY("IMON", CS35L41_PWR_CTRL2, 13, 0, NULL, 0),
547 SND_SOC_DAPM_SUPPLY("VPMON", CS35L41_PWR_CTRL2, 8, 0, NULL, 0),
548 SND_SOC_DAPM_SUPPLY("VBSTMON", CS35L41_PWR_CTRL2, 9, 0, NULL, 0),
549 SND_SOC_DAPM_SUPPLY("TEMPMON", CS35L41_PWR_CTRL2, 10, 0, NULL, 0),
551 SND_SOC_DAPM_ADC("VMON ADC", NULL, SND_SOC_NOPM, 0, 0),
552 SND_SOC_DAPM_ADC("IMON ADC", NULL, SND_SOC_NOPM, 0, 0),
553 SND_SOC_DAPM_ADC("VPMON ADC", NULL, SND_SOC_NOPM, 0, 0),
554 SND_SOC_DAPM_ADC("VBSTMON ADC", NULL, SND_SOC_NOPM, 0, 0),
555 SND_SOC_DAPM_ADC("TEMPMON ADC", NULL, SND_SOC_NOPM, 0, 0),
557 SND_SOC_DAPM_ADC("CLASS H", NULL, CS35L41_PWR_CTRL3, 4, 0),
559 SND_SOC_DAPM_OUT_DRV_E("Main AMP", CS35L41_PWR_CTRL2, 0, 0, NULL, 0,
560 cs35l41_main_amp_event,
561 SND_SOC_DAPM_POST_PMD | SND_SOC_DAPM_PRE_PMU),
563 SND_SOC_DAPM_MUX("ASP TX1 Source", SND_SOC_NOPM, 0, 0, &asp_tx1_mux),
564 SND_SOC_DAPM_MUX("ASP TX2 Source", SND_SOC_NOPM, 0, 0, &asp_tx2_mux),
565 SND_SOC_DAPM_MUX("ASP TX3 Source", SND_SOC_NOPM, 0, 0, &asp_tx3_mux),
566 SND_SOC_DAPM_MUX("ASP TX4 Source", SND_SOC_NOPM, 0, 0, &asp_tx4_mux),
567 SND_SOC_DAPM_MUX("DSP RX1 Source", SND_SOC_NOPM, 0, 0, &dsp_rx1_mux),
568 SND_SOC_DAPM_MUX("DSP RX2 Source", SND_SOC_NOPM, 0, 0, &dsp_rx2_mux),
569 SND_SOC_DAPM_MUX("PCM Source", SND_SOC_NOPM, 0, 0, &pcm_source_mux),
570 SND_SOC_DAPM_SWITCH("DRE", SND_SOC_NOPM, 0, 0, &dre_ctrl),
573 static const struct snd_soc_dapm_route cs35l41_audio_map[] = {
574 {"DSP RX1 Source", "ASPRX1", "ASPRX1"},
575 {"DSP RX1 Source", "ASPRX2", "ASPRX2"},
576 {"DSP RX2 Source", "ASPRX1", "ASPRX1"},
577 {"DSP RX2 Source", "ASPRX2", "ASPRX2"},
579 {"DSP1", NULL, "DSP RX1 Source"},
580 {"DSP1", NULL, "DSP RX2 Source"},
582 {"ASP TX1 Source", "VMON", "VMON ADC"},
583 {"ASP TX1 Source", "IMON", "IMON ADC"},
584 {"ASP TX1 Source", "VPMON", "VPMON ADC"},
585 {"ASP TX1 Source", "VBSTMON", "VBSTMON ADC"},
586 {"ASP TX1 Source", "DSPTX1", "DSP1"},
587 {"ASP TX1 Source", "DSPTX2", "DSP1"},
588 {"ASP TX1 Source", "ASPRX1", "ASPRX1" },
589 {"ASP TX1 Source", "ASPRX2", "ASPRX2" },
590 {"ASP TX2 Source", "VMON", "VMON ADC"},
591 {"ASP TX2 Source", "IMON", "IMON ADC"},
592 {"ASP TX2 Source", "VPMON", "VPMON ADC"},
593 {"ASP TX2 Source", "VBSTMON", "VBSTMON ADC"},
594 {"ASP TX2 Source", "DSPTX1", "DSP1"},
595 {"ASP TX2 Source", "DSPTX2", "DSP1"},
596 {"ASP TX2 Source", "ASPRX1", "ASPRX1" },
597 {"ASP TX2 Source", "ASPRX2", "ASPRX2" },
598 {"ASP TX3 Source", "VMON", "VMON ADC"},
599 {"ASP TX3 Source", "IMON", "IMON ADC"},
600 {"ASP TX3 Source", "VPMON", "VPMON ADC"},
601 {"ASP TX3 Source", "VBSTMON", "VBSTMON ADC"},
602 {"ASP TX3 Source", "DSPTX1", "DSP1"},
603 {"ASP TX3 Source", "DSPTX2", "DSP1"},
604 {"ASP TX3 Source", "ASPRX1", "ASPRX1" },
605 {"ASP TX3 Source", "ASPRX2", "ASPRX2" },
606 {"ASP TX4 Source", "VMON", "VMON ADC"},
607 {"ASP TX4 Source", "IMON", "IMON ADC"},
608 {"ASP TX4 Source", "VPMON", "VPMON ADC"},
609 {"ASP TX4 Source", "VBSTMON", "VBSTMON ADC"},
610 {"ASP TX4 Source", "DSPTX1", "DSP1"},
611 {"ASP TX4 Source", "DSPTX2", "DSP1"},
612 {"ASP TX4 Source", "ASPRX1", "ASPRX1" },
613 {"ASP TX4 Source", "ASPRX2", "ASPRX2" },
614 {"ASPTX1", NULL, "ASP TX1 Source"},
615 {"ASPTX2", NULL, "ASP TX2 Source"},
616 {"ASPTX3", NULL, "ASP TX3 Source"},
617 {"ASPTX4", NULL, "ASP TX4 Source"},
618 {"AMP Capture", NULL, "ASPTX1"},
619 {"AMP Capture", NULL, "ASPTX2"},
620 {"AMP Capture", NULL, "ASPTX3"},
621 {"AMP Capture", NULL, "ASPTX4"},
623 {"DSP1", NULL, "VMON"},
624 {"DSP1", NULL, "IMON"},
625 {"DSP1", NULL, "VPMON"},
626 {"DSP1", NULL, "VBSTMON"},
627 {"DSP1", NULL, "TEMPMON"},
629 {"VMON ADC", NULL, "VMON"},
630 {"IMON ADC", NULL, "IMON"},
631 {"VPMON ADC", NULL, "VPMON"},
632 {"VBSTMON ADC", NULL, "VBSTMON"},
633 {"TEMPMON ADC", NULL, "TEMPMON"},
635 {"VMON ADC", NULL, "VSENSE"},
636 {"IMON ADC", NULL, "ISENSE"},
637 {"VPMON ADC", NULL, "VP"},
638 {"VBSTMON ADC", NULL, "VBST"},
639 {"TEMPMON ADC", NULL, "TEMP"},
641 {"DSP1 Preload", NULL, "DSP1 Preloader"},
642 {"DSP1", NULL, "DSP1 Preloader"},
644 {"ASPRX1", NULL, "AMP Playback"},
645 {"ASPRX2", NULL, "AMP Playback"},
646 {"DRE", "Switch", "CLASS H"},
647 {"Main AMP", NULL, "CLASS H"},
648 {"Main AMP", NULL, "DRE"},
649 {"SPK", NULL, "Main AMP"},
651 {"PCM Source", "ASP", "ASPRX1"},
652 {"PCM Source", "DSP", "DSP1"},
653 {"CLASS H", NULL, "PCM Source"},
656 static int cs35l41_set_channel_map(struct snd_soc_dai *dai, unsigned int tx_n,
657 unsigned int *tx_slot, unsigned int rx_n, unsigned int *rx_slot)
659 struct cs35l41_private *cs35l41 = snd_soc_component_get_drvdata(dai->component);
661 return cs35l41_set_channels(cs35l41->dev, cs35l41->regmap, tx_n, tx_slot, rx_n, rx_slot);
664 static int cs35l41_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
666 struct cs35l41_private *cs35l41 = snd_soc_component_get_drvdata(dai->component);
667 unsigned int daifmt = 0;
669 switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
670 case SND_SOC_DAIFMT_CBP_CFP:
671 daifmt |= CS35L41_SCLK_MSTR_MASK | CS35L41_LRCLK_MSTR_MASK;
673 case SND_SOC_DAIFMT_CBC_CFC:
676 dev_warn(cs35l41->dev, "Mixed provider/consumer mode unsupported\n");
680 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
681 case SND_SOC_DAIFMT_DSP_A:
683 case SND_SOC_DAIFMT_I2S:
684 daifmt |= 2 << CS35L41_ASP_FMT_SHIFT;
687 dev_warn(cs35l41->dev, "Invalid or unsupported DAI format\n");
691 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
692 case SND_SOC_DAIFMT_NB_IF:
693 daifmt |= CS35L41_LRCLK_INV_MASK;
695 case SND_SOC_DAIFMT_IB_NF:
696 daifmt |= CS35L41_SCLK_INV_MASK;
698 case SND_SOC_DAIFMT_IB_IF:
699 daifmt |= CS35L41_LRCLK_INV_MASK | CS35L41_SCLK_INV_MASK;
701 case SND_SOC_DAIFMT_NB_NF:
704 dev_warn(cs35l41->dev, "Invalid DAI clock INV\n");
708 return regmap_update_bits(cs35l41->regmap, CS35L41_SP_FORMAT,
709 CS35L41_SCLK_MSTR_MASK | CS35L41_LRCLK_MSTR_MASK |
710 CS35L41_ASP_FMT_MASK | CS35L41_LRCLK_INV_MASK |
711 CS35L41_SCLK_INV_MASK, daifmt);
714 struct cs35l41_global_fs_config {
719 static const struct cs35l41_global_fs_config cs35l41_fs_rates[] = {
735 static int cs35l41_pcm_hw_params(struct snd_pcm_substream *substream,
736 struct snd_pcm_hw_params *params,
737 struct snd_soc_dai *dai)
739 struct cs35l41_private *cs35l41 = snd_soc_component_get_drvdata(dai->component);
740 unsigned int rate = params_rate(params);
744 for (i = 0; i < ARRAY_SIZE(cs35l41_fs_rates); i++) {
745 if (rate == cs35l41_fs_rates[i].rate)
749 if (i >= ARRAY_SIZE(cs35l41_fs_rates)) {
750 dev_err(cs35l41->dev, "Unsupported rate: %u\n", rate);
754 asp_wl = params_width(params);
756 if (i < ARRAY_SIZE(cs35l41_fs_rates))
757 regmap_update_bits(cs35l41->regmap, CS35L41_GLOBAL_CLK_CTRL,
758 CS35L41_GLOBAL_FS_MASK,
759 cs35l41_fs_rates[i].fs_cfg << CS35L41_GLOBAL_FS_SHIFT);
761 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
762 regmap_update_bits(cs35l41->regmap, CS35L41_SP_FORMAT,
763 CS35L41_ASP_WIDTH_RX_MASK,
764 asp_wl << CS35L41_ASP_WIDTH_RX_SHIFT);
765 regmap_update_bits(cs35l41->regmap, CS35L41_SP_RX_WL,
766 CS35L41_ASP_RX_WL_MASK,
767 asp_wl << CS35L41_ASP_RX_WL_SHIFT);
769 regmap_update_bits(cs35l41->regmap, CS35L41_SP_FORMAT,
770 CS35L41_ASP_WIDTH_TX_MASK,
771 asp_wl << CS35L41_ASP_WIDTH_TX_SHIFT);
772 regmap_update_bits(cs35l41->regmap, CS35L41_SP_TX_WL,
773 CS35L41_ASP_TX_WL_MASK,
774 asp_wl << CS35L41_ASP_TX_WL_SHIFT);
780 static int cs35l41_get_clk_config(int freq)
784 for (i = 0; i < ARRAY_SIZE(cs35l41_pll_sysclk); i++) {
785 if (cs35l41_pll_sysclk[i].freq == freq)
786 return cs35l41_pll_sysclk[i].clk_cfg;
792 static const unsigned int cs35l41_src_rates[] = {
793 8000, 12000, 11025, 16000, 22050, 24000, 32000,
794 44100, 48000, 88200, 96000, 176400, 192000
797 static const struct snd_pcm_hw_constraint_list cs35l41_constraints = {
798 .count = ARRAY_SIZE(cs35l41_src_rates),
799 .list = cs35l41_src_rates,
802 static int cs35l41_pcm_startup(struct snd_pcm_substream *substream,
803 struct snd_soc_dai *dai)
805 struct cs35l41_private *cs35l41 = snd_soc_component_get_drvdata(dai->component);
807 reinit_completion(&cs35l41->pll_lock);
809 if (substream->runtime)
810 return snd_pcm_hw_constraint_list(substream->runtime, 0,
811 SNDRV_PCM_HW_PARAM_RATE,
812 &cs35l41_constraints);
816 static int cs35l41_component_set_sysclk(struct snd_soc_component *component,
817 int clk_id, int source,
818 unsigned int freq, int dir)
820 struct cs35l41_private *cs35l41 = snd_soc_component_get_drvdata(component);
821 int extclk_cfg, clksrc;
824 case CS35L41_CLKID_SCLK:
825 clksrc = CS35L41_PLLSRC_SCLK;
827 case CS35L41_CLKID_LRCLK:
828 clksrc = CS35L41_PLLSRC_LRCLK;
830 case CS35L41_CLKID_MCLK:
831 clksrc = CS35L41_PLLSRC_MCLK;
834 dev_err(cs35l41->dev, "Invalid CLK Config\n");
838 extclk_cfg = cs35l41_get_clk_config(freq);
840 if (extclk_cfg < 0) {
841 dev_err(cs35l41->dev, "Invalid CLK Config: %d, freq: %u\n",
846 regmap_update_bits(cs35l41->regmap, CS35L41_PLL_CLK_CTRL,
847 CS35L41_PLL_OPENLOOP_MASK,
848 1 << CS35L41_PLL_OPENLOOP_SHIFT);
849 regmap_update_bits(cs35l41->regmap, CS35L41_PLL_CLK_CTRL,
850 CS35L41_REFCLK_FREQ_MASK,
851 extclk_cfg << CS35L41_REFCLK_FREQ_SHIFT);
852 regmap_update_bits(cs35l41->regmap, CS35L41_PLL_CLK_CTRL,
853 CS35L41_PLL_CLK_EN_MASK,
854 0 << CS35L41_PLL_CLK_EN_SHIFT);
855 regmap_update_bits(cs35l41->regmap, CS35L41_PLL_CLK_CTRL,
856 CS35L41_PLL_CLK_SEL_MASK, clksrc);
857 regmap_update_bits(cs35l41->regmap, CS35L41_PLL_CLK_CTRL,
858 CS35L41_PLL_OPENLOOP_MASK,
859 0 << CS35L41_PLL_OPENLOOP_SHIFT);
860 regmap_update_bits(cs35l41->regmap, CS35L41_PLL_CLK_CTRL,
861 CS35L41_PLL_CLK_EN_MASK,
862 1 << CS35L41_PLL_CLK_EN_SHIFT);
867 static int cs35l41_dai_set_sysclk(struct snd_soc_dai *dai,
868 int clk_id, unsigned int freq, int dir)
870 struct cs35l41_private *cs35l41 = snd_soc_component_get_drvdata(dai->component);
871 unsigned int fs1_val;
872 unsigned int fs2_val;
876 fsindex = cs35l41_get_fs_mon_config_index(freq);
878 dev_err(cs35l41->dev, "Invalid CLK Config freq: %u\n", freq);
882 dev_dbg(cs35l41->dev, "Set DAI sysclk %d\n", freq);
884 if (freq <= 6144000) {
885 /* Use the lookup table */
886 fs1_val = cs35l41_fs_mon[fsindex].fs1;
887 fs2_val = cs35l41_fs_mon[fsindex].fs2;
889 /* Use hard-coded values */
895 val |= (fs2_val << CS35L41_FS2_WINDOW_SHIFT) & CS35L41_FS2_WINDOW_MASK;
896 regmap_write(cs35l41->regmap, CS35L41_TST_FS_MON0, val);
901 static int cs35l41_set_pdata(struct cs35l41_private *cs35l41)
903 struct cs35l41_hw_cfg *hw_cfg = &cs35l41->hw_cfg;
909 if (hw_cfg->bst_type == CS35L41_EXT_BOOST_NO_VSPK_SWITCH)
913 ret = cs35l41_init_boost(cs35l41->dev, cs35l41->regmap, hw_cfg);
918 if (hw_cfg->dout_hiz <= CS35L41_ASP_DOUT_HIZ_MASK && hw_cfg->dout_hiz >= 0)
919 regmap_update_bits(cs35l41->regmap, CS35L41_SP_HIZ_CTRL, CS35L41_ASP_DOUT_HIZ_MASK,
925 static const struct snd_soc_dapm_route cs35l41_ext_bst_routes[] = {
926 {"Main AMP", NULL, "VSPK"},
929 static const struct snd_soc_dapm_widget cs35l41_ext_bst_widget[] = {
930 SND_SOC_DAPM_SUPPLY("VSPK", CS35L41_GPIO1_CTRL1, CS35L41_GPIO_LVL_SHIFT, 0, NULL, 0),
933 static int cs35l41_component_probe(struct snd_soc_component *component)
935 struct cs35l41_private *cs35l41 = snd_soc_component_get_drvdata(component);
936 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
939 if (cs35l41->hw_cfg.bst_type == CS35L41_EXT_BOOST) {
940 ret = snd_soc_dapm_new_controls(dapm, cs35l41_ext_bst_widget,
941 ARRAY_SIZE(cs35l41_ext_bst_widget));
945 ret = snd_soc_dapm_add_routes(dapm, cs35l41_ext_bst_routes,
946 ARRAY_SIZE(cs35l41_ext_bst_routes));
951 return wm_adsp2_component_probe(&cs35l41->dsp, component);
954 static void cs35l41_component_remove(struct snd_soc_component *component)
956 struct cs35l41_private *cs35l41 = snd_soc_component_get_drvdata(component);
958 wm_adsp2_component_remove(&cs35l41->dsp, component);
961 static const struct snd_soc_dai_ops cs35l41_ops = {
962 .startup = cs35l41_pcm_startup,
963 .set_fmt = cs35l41_set_dai_fmt,
964 .hw_params = cs35l41_pcm_hw_params,
965 .set_sysclk = cs35l41_dai_set_sysclk,
966 .set_channel_map = cs35l41_set_channel_map,
969 static struct snd_soc_dai_driver cs35l41_dai[] = {
971 .name = "cs35l41-pcm",
974 .stream_name = "AMP Playback",
977 .rates = SNDRV_PCM_RATE_KNOT,
978 .formats = CS35L41_RX_FORMATS,
981 .stream_name = "AMP Capture",
984 .rates = SNDRV_PCM_RATE_KNOT,
985 .formats = CS35L41_TX_FORMATS,
992 static const struct snd_soc_component_driver soc_component_dev_cs35l41 = {
993 .name = "cs35l41-codec",
994 .probe = cs35l41_component_probe,
995 .remove = cs35l41_component_remove,
997 .dapm_widgets = cs35l41_dapm_widgets,
998 .num_dapm_widgets = ARRAY_SIZE(cs35l41_dapm_widgets),
999 .dapm_routes = cs35l41_audio_map,
1000 .num_dapm_routes = ARRAY_SIZE(cs35l41_audio_map),
1002 .controls = cs35l41_aud_controls,
1003 .num_controls = ARRAY_SIZE(cs35l41_aud_controls),
1004 .set_sysclk = cs35l41_component_set_sysclk,
1009 static int cs35l41_handle_pdata(struct device *dev, struct cs35l41_hw_cfg *hw_cfg)
1011 struct cs35l41_gpio_cfg *gpio1 = &hw_cfg->gpio1;
1012 struct cs35l41_gpio_cfg *gpio2 = &hw_cfg->gpio2;
1016 /* Some ACPI systems received the Shared Boost feature before the upstream driver,
1017 * leaving those systems with deprecated _DSD properties.
1018 * To correctly configure those systems add shared-boost-active and shared-boost-passive
1019 * properties mapped to the correct value in boost-type.
1020 * These two are not DT properties and should not be used in new systems designs.
1022 if (device_property_read_bool(dev, "cirrus,shared-boost-active")) {
1023 hw_cfg->bst_type = CS35L41_SHD_BOOST_ACTV;
1024 } else if (device_property_read_bool(dev, "cirrus,shared-boost-passive")) {
1025 hw_cfg->bst_type = CS35L41_SHD_BOOST_PASS;
1027 ret = device_property_read_u32(dev, "cirrus,boost-type", &val);
1029 hw_cfg->bst_type = val;
1032 ret = device_property_read_u32(dev, "cirrus,boost-peak-milliamp", &val);
1034 hw_cfg->bst_ipk = val;
1036 hw_cfg->bst_ipk = -1;
1038 ret = device_property_read_u32(dev, "cirrus,boost-ind-nanohenry", &val);
1040 hw_cfg->bst_ind = val;
1042 hw_cfg->bst_ind = -1;
1044 ret = device_property_read_u32(dev, "cirrus,boost-cap-microfarad", &val);
1046 hw_cfg->bst_cap = val;
1048 hw_cfg->bst_cap = -1;
1050 ret = device_property_read_u32(dev, "cirrus,asp-sdout-hiz", &val);
1052 hw_cfg->dout_hiz = val;
1054 hw_cfg->dout_hiz = -1;
1056 /* GPIO1 Pin Config */
1057 gpio1->pol_inv = device_property_read_bool(dev, "cirrus,gpio1-polarity-invert");
1058 gpio1->out_en = device_property_read_bool(dev, "cirrus,gpio1-output-enable");
1059 ret = device_property_read_u32(dev, "cirrus,gpio1-src-select", &val);
1062 gpio1->valid = true;
1065 /* GPIO2 Pin Config */
1066 gpio2->pol_inv = device_property_read_bool(dev, "cirrus,gpio2-polarity-invert");
1067 gpio2->out_en = device_property_read_bool(dev, "cirrus,gpio2-output-enable");
1068 ret = device_property_read_u32(dev, "cirrus,gpio2-src-select", &val);
1071 gpio2->valid = true;
1074 hw_cfg->valid = true;
1079 static int cs35l41_dsp_init(struct cs35l41_private *cs35l41)
1081 struct wm_adsp *dsp;
1084 dsp = &cs35l41->dsp;
1085 dsp->part = "cs35l41";
1086 dsp->fw = 9; /* 9 is WM_ADSP_FW_SPK_PROT in wm_adsp.c */
1087 dsp->toggle_preload = true;
1089 cs35l41_configure_cs_dsp(cs35l41->dev, cs35l41->regmap, &dsp->cs_dsp);
1091 ret = cs35l41_write_fs_errata(cs35l41->dev, cs35l41->regmap);
1095 ret = wm_halo_init(dsp);
1097 dev_err(cs35l41->dev, "wm_halo_init failed: %d\n", ret);
1101 ret = regmap_write(cs35l41->regmap, CS35L41_DSP1_RX5_SRC,
1102 CS35L41_INPUT_SRC_VPMON);
1104 dev_err(cs35l41->dev, "Write INPUT_SRC_VPMON failed: %d\n", ret);
1107 ret = regmap_write(cs35l41->regmap, CS35L41_DSP1_RX6_SRC,
1108 CS35L41_INPUT_SRC_CLASSH);
1110 dev_err(cs35l41->dev, "Write INPUT_SRC_CLASSH failed: %d\n", ret);
1113 ret = regmap_write(cs35l41->regmap, CS35L41_DSP1_RX7_SRC,
1114 CS35L41_INPUT_SRC_TEMPMON);
1116 dev_err(cs35l41->dev, "Write INPUT_SRC_TEMPMON failed: %d\n", ret);
1119 ret = regmap_write(cs35l41->regmap, CS35L41_DSP1_RX8_SRC,
1120 CS35L41_INPUT_SRC_RSVD);
1122 dev_err(cs35l41->dev, "Write INPUT_SRC_RSVD failed: %d\n", ret);
1129 wm_adsp2_remove(dsp);
1134 static int cs35l41_acpi_get_name(struct cs35l41_private *cs35l41)
1136 acpi_handle handle = ACPI_HANDLE(cs35l41->dev);
1139 /* If there is no ACPI_HANDLE, there is no ACPI for this system, return 0 */
1143 sub = acpi_get_subsystem_id(handle);
1145 /* If bad ACPI, return 0 and fallback to legacy firmware path, otherwise fail */
1146 if (PTR_ERR(sub) == -ENODATA)
1149 return PTR_ERR(sub);
1152 cs35l41->dsp.system_name = sub;
1153 dev_dbg(cs35l41->dev, "Subsystem ID: %s\n", cs35l41->dsp.system_name);
1158 int cs35l41_probe(struct cs35l41_private *cs35l41, const struct cs35l41_hw_cfg *hw_cfg)
1160 u32 regid, reg_revid, i, mtl_revid, int_status, chipid_match;
1165 cs35l41->hw_cfg = *hw_cfg;
1167 ret = cs35l41_handle_pdata(cs35l41->dev, &cs35l41->hw_cfg);
1172 for (i = 0; i < CS35L41_NUM_SUPPLIES; i++)
1173 cs35l41->supplies[i].supply = cs35l41_supplies[i];
1175 ret = devm_regulator_bulk_get(cs35l41->dev, CS35L41_NUM_SUPPLIES,
1178 dev_err(cs35l41->dev, "Failed to request core supplies: %d\n", ret);
1182 ret = regulator_bulk_enable(CS35L41_NUM_SUPPLIES, cs35l41->supplies);
1184 dev_err(cs35l41->dev, "Failed to enable core supplies: %d\n", ret);
1188 /* returning NULL can be an option if in stereo mode */
1189 cs35l41->reset_gpio = devm_gpiod_get_optional(cs35l41->dev, "reset",
1191 if (IS_ERR(cs35l41->reset_gpio)) {
1192 ret = PTR_ERR(cs35l41->reset_gpio);
1193 cs35l41->reset_gpio = NULL;
1194 if (ret == -EBUSY) {
1195 dev_info(cs35l41->dev,
1196 "Reset line busy, assuming shared reset\n");
1198 dev_err(cs35l41->dev,
1199 "Failed to get reset GPIO: %d\n", ret);
1203 if (cs35l41->reset_gpio) {
1204 /* satisfy minimum reset pulse width spec */
1205 usleep_range(2000, 2100);
1206 gpiod_set_value_cansleep(cs35l41->reset_gpio, 1);
1209 usleep_range(2000, 2100);
1211 ret = regmap_read_poll_timeout(cs35l41->regmap, CS35L41_IRQ1_STATUS4,
1212 int_status, int_status & CS35L41_OTP_BOOT_DONE,
1215 dev_err(cs35l41->dev,
1216 "Failed waiting for OTP_BOOT_DONE: %d\n", ret);
1220 regmap_read(cs35l41->regmap, CS35L41_IRQ1_STATUS3, &int_status);
1221 if (int_status & CS35L41_OTP_BOOT_ERR) {
1222 dev_err(cs35l41->dev, "OTP Boot error\n");
1227 ret = regmap_read(cs35l41->regmap, CS35L41_DEVID, ®id);
1229 dev_err(cs35l41->dev, "Get Device ID failed: %d\n", ret);
1233 ret = regmap_read(cs35l41->regmap, CS35L41_REVID, ®_revid);
1235 dev_err(cs35l41->dev, "Get Revision ID failed: %d\n", ret);
1239 mtl_revid = reg_revid & CS35L41_MTLREVID_MASK;
1241 /* CS35L41 will have even MTLREVID
1242 * CS35L41R will have odd MTLREVID
1244 chipid_match = (mtl_revid % 2) ? CS35L41R_CHIP_ID : CS35L41_CHIP_ID;
1245 if (regid != chipid_match) {
1246 dev_err(cs35l41->dev, "CS35L41 Device ID (%X). Expected ID %X\n",
1247 regid, chipid_match);
1252 cs35l41_test_key_unlock(cs35l41->dev, cs35l41->regmap);
1254 ret = cs35l41_register_errata_patch(cs35l41->dev, cs35l41->regmap, reg_revid);
1258 ret = cs35l41_otp_unpack(cs35l41->dev, cs35l41->regmap);
1260 dev_err(cs35l41->dev, "OTP Unpack failed: %d\n", ret);
1264 cs35l41_test_key_lock(cs35l41->dev, cs35l41->regmap);
1266 irq_pol = cs35l41_gpio_config(cs35l41->regmap, &cs35l41->hw_cfg);
1268 /* Set interrupt masks for critical errors */
1269 regmap_write(cs35l41->regmap, CS35L41_IRQ1_MASK1,
1270 CS35L41_INT1_MASK_DEFAULT);
1271 if (cs35l41->hw_cfg.bst_type == CS35L41_SHD_BOOST_PASS ||
1272 cs35l41->hw_cfg.bst_type == CS35L41_SHD_BOOST_ACTV)
1273 regmap_update_bits(cs35l41->regmap, CS35L41_IRQ1_MASK3, CS35L41_INT3_PLL_LOCK_MASK,
1274 0 << CS35L41_INT3_PLL_LOCK_SHIFT);
1276 ret = devm_request_threaded_irq(cs35l41->dev, cs35l41->irq, NULL, cs35l41_irq,
1277 IRQF_ONESHOT | IRQF_SHARED | irq_pol,
1278 "cs35l41", cs35l41);
1280 dev_err(cs35l41->dev, "Failed to request IRQ: %d\n", ret);
1284 ret = cs35l41_set_pdata(cs35l41);
1286 dev_err(cs35l41->dev, "Set pdata failed: %d\n", ret);
1290 ret = cs35l41_acpi_get_name(cs35l41);
1294 ret = cs35l41_dsp_init(cs35l41);
1298 init_completion(&cs35l41->pll_lock);
1300 pm_runtime_set_autosuspend_delay(cs35l41->dev, 3000);
1301 pm_runtime_use_autosuspend(cs35l41->dev);
1302 pm_runtime_mark_last_busy(cs35l41->dev);
1303 pm_runtime_set_active(cs35l41->dev);
1304 pm_runtime_get_noresume(cs35l41->dev);
1305 pm_runtime_enable(cs35l41->dev);
1307 ret = devm_snd_soc_register_component(cs35l41->dev,
1308 &soc_component_dev_cs35l41,
1309 cs35l41_dai, ARRAY_SIZE(cs35l41_dai));
1311 dev_err(cs35l41->dev, "Register codec failed: %d\n", ret);
1315 pm_runtime_put_autosuspend(cs35l41->dev);
1317 dev_info(cs35l41->dev, "Cirrus Logic CS35L41 (%x), Revision: %02X\n",
1323 pm_runtime_disable(cs35l41->dev);
1324 pm_runtime_put_noidle(cs35l41->dev);
1326 wm_adsp2_remove(&cs35l41->dsp);
1328 cs35l41_safe_reset(cs35l41->regmap, cs35l41->hw_cfg.bst_type);
1329 regulator_bulk_disable(CS35L41_NUM_SUPPLIES, cs35l41->supplies);
1330 gpiod_set_value_cansleep(cs35l41->reset_gpio, 0);
1334 EXPORT_SYMBOL_GPL(cs35l41_probe);
1336 void cs35l41_remove(struct cs35l41_private *cs35l41)
1338 pm_runtime_get_sync(cs35l41->dev);
1339 pm_runtime_disable(cs35l41->dev);
1341 regmap_write(cs35l41->regmap, CS35L41_IRQ1_MASK1, 0xFFFFFFFF);
1342 if (cs35l41->hw_cfg.bst_type == CS35L41_SHD_BOOST_PASS ||
1343 cs35l41->hw_cfg.bst_type == CS35L41_SHD_BOOST_ACTV)
1344 regmap_update_bits(cs35l41->regmap, CS35L41_IRQ1_MASK3, CS35L41_INT3_PLL_LOCK_MASK,
1345 1 << CS35L41_INT3_PLL_LOCK_SHIFT);
1346 kfree(cs35l41->dsp.system_name);
1347 wm_adsp2_remove(&cs35l41->dsp);
1348 cs35l41_safe_reset(cs35l41->regmap, cs35l41->hw_cfg.bst_type);
1350 pm_runtime_put_noidle(cs35l41->dev);
1352 regulator_bulk_disable(CS35L41_NUM_SUPPLIES, cs35l41->supplies);
1353 gpiod_set_value_cansleep(cs35l41->reset_gpio, 0);
1355 EXPORT_SYMBOL_GPL(cs35l41_remove);
1357 static int __maybe_unused cs35l41_runtime_suspend(struct device *dev)
1359 struct cs35l41_private *cs35l41 = dev_get_drvdata(dev);
1361 dev_dbg(cs35l41->dev, "Runtime suspend\n");
1363 if (!cs35l41->dsp.preloaded || !cs35l41->dsp.cs_dsp.running)
1366 cs35l41_enter_hibernate(dev, cs35l41->regmap, cs35l41->hw_cfg.bst_type);
1368 regcache_cache_only(cs35l41->regmap, true);
1369 regcache_mark_dirty(cs35l41->regmap);
1374 static int __maybe_unused cs35l41_runtime_resume(struct device *dev)
1376 struct cs35l41_private *cs35l41 = dev_get_drvdata(dev);
1379 dev_dbg(cs35l41->dev, "Runtime resume\n");
1381 if (!cs35l41->dsp.preloaded || !cs35l41->dsp.cs_dsp.running)
1384 regcache_cache_only(cs35l41->regmap, false);
1386 ret = cs35l41_exit_hibernate(cs35l41->dev, cs35l41->regmap);
1390 /* Test key needs to be unlocked to allow the OTP settings to re-apply */
1391 cs35l41_test_key_unlock(cs35l41->dev, cs35l41->regmap);
1392 ret = regcache_sync(cs35l41->regmap);
1393 cs35l41_test_key_lock(cs35l41->dev, cs35l41->regmap);
1395 dev_err(cs35l41->dev, "Failed to restore register cache: %d\n", ret);
1398 cs35l41_init_boost(cs35l41->dev, cs35l41->regmap, &cs35l41->hw_cfg);
1403 static int __maybe_unused cs35l41_sys_suspend(struct device *dev)
1405 struct cs35l41_private *cs35l41 = dev_get_drvdata(dev);
1407 dev_dbg(cs35l41->dev, "System suspend, disabling IRQ\n");
1408 disable_irq(cs35l41->irq);
1413 static int __maybe_unused cs35l41_sys_suspend_noirq(struct device *dev)
1415 struct cs35l41_private *cs35l41 = dev_get_drvdata(dev);
1417 dev_dbg(cs35l41->dev, "Late system suspend, reenabling IRQ\n");
1418 enable_irq(cs35l41->irq);
1423 static int __maybe_unused cs35l41_sys_resume_noirq(struct device *dev)
1425 struct cs35l41_private *cs35l41 = dev_get_drvdata(dev);
1427 dev_dbg(cs35l41->dev, "Early system resume, disabling IRQ\n");
1428 disable_irq(cs35l41->irq);
1433 static int __maybe_unused cs35l41_sys_resume(struct device *dev)
1435 struct cs35l41_private *cs35l41 = dev_get_drvdata(dev);
1437 dev_dbg(cs35l41->dev, "System resume, reenabling IRQ\n");
1438 enable_irq(cs35l41->irq);
1443 const struct dev_pm_ops cs35l41_pm_ops = {
1444 SET_RUNTIME_PM_OPS(cs35l41_runtime_suspend, cs35l41_runtime_resume, NULL)
1446 SET_SYSTEM_SLEEP_PM_OPS(cs35l41_sys_suspend, cs35l41_sys_resume)
1447 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(cs35l41_sys_suspend_noirq, cs35l41_sys_resume_noirq)
1449 EXPORT_SYMBOL_GPL(cs35l41_pm_ops);
1451 MODULE_DESCRIPTION("ASoC CS35L41 driver");
1452 MODULE_AUTHOR("David Rhodes, Cirrus Logic Inc, <david.rhodes@cirrus.com>");
1453 MODULE_LICENSE("GPL");