1 // SPDX-License-Identifier: GPL-2.0
3 // rt1015.c -- RT1015 ALSA SoC audio amplifier driver
5 // Copyright 2019 Realtek Semiconductor Corp.
11 #include <linux/acpi.h>
12 #include <linux/delay.h>
13 #include <linux/firmware.h>
15 #include <linux/i2c.h>
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/moduleparam.h>
19 #include <linux/platform_device.h>
21 #include <linux/regmap.h>
22 #include <sound/core.h>
23 #include <sound/initval.h>
24 #include <sound/pcm.h>
25 #include <sound/pcm_params.h>
26 #include <sound/rt1015.h>
27 #include <sound/soc-dapm.h>
28 #include <sound/soc.h>
29 #include <sound/tlv.h>
34 static const struct rt1015_platform_data i2s_default_platform_data = {
35 .power_up_delay_ms = 50,
38 static const struct reg_default rt1015_reg[] = {
201 static bool rt1015_volatile_register(struct device *dev, unsigned int reg)
208 case RT1015_VENDOR_ID:
209 case RT1015_DEVICE_ID:
213 case RT1015_VBAT_TEST_OUT1:
214 case RT1015_VBAT_TEST_OUT2:
215 case RT1015_VBAT_PROT_ATT:
216 case RT1015_VBAT_DET_CODE:
217 case RT1015_SMART_BST_CTRL1:
218 case RT1015_SPK_DC_DETECT1:
219 case RT1015_SPK_DC_DETECT4:
220 case RT1015_SPK_DC_DETECT5:
221 case RT1015_DC_CALIB_CLSD1:
222 case RT1015_DC_CALIB_CLSD5:
223 case RT1015_DC_CALIB_CLSD6:
224 case RT1015_DC_CALIB_CLSD7:
225 case RT1015_DC_CALIB_CLSD8:
226 case RT1015_S_BST_TIMING_INTER1:
227 case RT1015_OSCK_STA:
228 case RT1015_MONO_DYNA_CTRL1:
229 case RT1015_MONO_DYNA_CTRL5:
237 static bool rt1015_readable_register(struct device *dev, unsigned int reg)
253 case RT1015_CUSTOMER_ID:
254 case RT1015_PCODE_FWVER:
256 case RT1015_VENDOR_ID:
257 case RT1015_DEVICE_ID:
258 case RT1015_PAD_DRV1:
259 case RT1015_PAD_DRV2:
260 case RT1015_GAT_BOOST:
262 case RT1015_OSCK_STA:
269 case RT1015_TDM_MASTER:
270 case RT1015_TDM_TCON:
278 case RT1015_ANA_PROTECT1:
279 case RT1015_ANA_CTRL_SEQ1:
280 case RT1015_ANA_CTRL_SEQ2:
281 case RT1015_VBAT_DET_DEB:
282 case RT1015_VBAT_VOLT_DET1:
283 case RT1015_VBAT_VOLT_DET2:
284 case RT1015_VBAT_TEST_OUT1:
285 case RT1015_VBAT_TEST_OUT2:
286 case RT1015_VBAT_PROT_ATT:
287 case RT1015_VBAT_DET_CODE:
295 case RT1015_CLASSD_SEQ:
296 case RT1015_SMART_BST_CTRL1:
297 case RT1015_SMART_BST_CTRL2:
298 case RT1015_ANA_CTRL1:
299 case RT1015_ANA_CTRL2:
300 case RT1015_PWR_STATE_CTRL:
301 case RT1015_MONO_DYNA_CTRL:
302 case RT1015_MONO_DYNA_CTRL1:
303 case RT1015_MONO_DYNA_CTRL2:
304 case RT1015_MONO_DYNA_CTRL3:
305 case RT1015_MONO_DYNA_CTRL4:
306 case RT1015_MONO_DYNA_CTRL5:
308 case RT1015_SHORT_DETTOP1:
309 case RT1015_SHORT_DETTOP2:
310 case RT1015_SPK_DC_DETECT1:
311 case RT1015_SPK_DC_DETECT2:
312 case RT1015_SPK_DC_DETECT3:
313 case RT1015_SPK_DC_DETECT4:
314 case RT1015_SPK_DC_DETECT5:
315 case RT1015_BAT_RPO_STEP1:
316 case RT1015_BAT_RPO_STEP2:
317 case RT1015_BAT_RPO_STEP3:
318 case RT1015_BAT_RPO_STEP4:
319 case RT1015_BAT_RPO_STEP5:
320 case RT1015_BAT_RPO_STEP6:
321 case RT1015_BAT_RPO_STEP7:
322 case RT1015_BAT_RPO_STEP8:
323 case RT1015_BAT_RPO_STEP9:
324 case RT1015_BAT_RPO_STEP10:
325 case RT1015_BAT_RPO_STEP11:
326 case RT1015_BAT_RPO_STEP12:
327 case RT1015_SPREAD_SPEC1:
328 case RT1015_SPREAD_SPEC2:
329 case RT1015_PAD_STATUS:
330 case RT1015_PADS_PULLING_CTRL1:
331 case RT1015_PADS_DRIVING:
332 case RT1015_SYS_RST1:
333 case RT1015_SYS_RST2:
334 case RT1015_SYS_GATING1:
335 case RT1015_TEST_MODE1:
336 case RT1015_TEST_MODE2:
337 case RT1015_TIMING_CTRL1:
339 case RT1015_TEST_OUT1:
340 case RT1015_DC_CALIB_CLSD1:
341 case RT1015_DC_CALIB_CLSD2:
342 case RT1015_DC_CALIB_CLSD3:
343 case RT1015_DC_CALIB_CLSD4:
344 case RT1015_DC_CALIB_CLSD5:
345 case RT1015_DC_CALIB_CLSD6:
346 case RT1015_DC_CALIB_CLSD7:
347 case RT1015_DC_CALIB_CLSD8:
348 case RT1015_DC_CALIB_CLSD9:
349 case RT1015_DC_CALIB_CLSD10:
350 case RT1015_CLSD_INTERNAL1:
351 case RT1015_CLSD_INTERNAL2:
352 case RT1015_CLSD_INTERNAL3:
353 case RT1015_CLSD_INTERNAL4:
354 case RT1015_CLSD_INTERNAL5:
355 case RT1015_CLSD_INTERNAL6:
356 case RT1015_CLSD_INTERNAL7:
357 case RT1015_CLSD_INTERNAL8:
358 case RT1015_CLSD_INTERNAL9:
359 case RT1015_CLSD_OCP_CTRL:
365 case RT1015_VREF_LV1:
366 case RT1015_S_BST_TIMING_INTER1:
367 case RT1015_S_BST_TIMING_INTER2:
368 case RT1015_S_BST_TIMING_INTER3:
369 case RT1015_S_BST_TIMING_INTER4:
370 case RT1015_S_BST_TIMING_INTER5:
371 case RT1015_S_BST_TIMING_INTER6:
372 case RT1015_S_BST_TIMING_INTER7:
373 case RT1015_S_BST_TIMING_INTER8:
374 case RT1015_S_BST_TIMING_INTER9:
375 case RT1015_S_BST_TIMING_INTER10:
376 case RT1015_S_BST_TIMING_INTER11:
377 case RT1015_S_BST_TIMING_INTER12:
378 case RT1015_S_BST_TIMING_INTER13:
379 case RT1015_S_BST_TIMING_INTER14:
380 case RT1015_S_BST_TIMING_INTER15:
381 case RT1015_S_BST_TIMING_INTER16:
382 case RT1015_S_BST_TIMING_INTER17:
383 case RT1015_S_BST_TIMING_INTER18:
384 case RT1015_S_BST_TIMING_INTER19:
385 case RT1015_S_BST_TIMING_INTER20:
386 case RT1015_S_BST_TIMING_INTER21:
387 case RT1015_S_BST_TIMING_INTER22:
388 case RT1015_S_BST_TIMING_INTER23:
389 case RT1015_S_BST_TIMING_INTER24:
390 case RT1015_S_BST_TIMING_INTER25:
391 case RT1015_S_BST_TIMING_INTER26:
392 case RT1015_S_BST_TIMING_INTER27:
393 case RT1015_S_BST_TIMING_INTER28:
394 case RT1015_S_BST_TIMING_INTER29:
395 case RT1015_S_BST_TIMING_INTER30:
396 case RT1015_S_BST_TIMING_INTER31:
397 case RT1015_S_BST_TIMING_INTER32:
398 case RT1015_S_BST_TIMING_INTER33:
399 case RT1015_S_BST_TIMING_INTER34:
400 case RT1015_S_BST_TIMING_INTER35:
401 case RT1015_S_BST_TIMING_INTER36:
409 static const DECLARE_TLV_DB_SCALE(dac_vol_tlv, -9525, 75, 0);
411 static const char * const rt1015_din_source_select[] = {
414 "Left + Right average",
417 static SOC_ENUM_SINGLE_DECL(rt1015_mono_lr_sel, RT1015_PAD_DRV2, 4,
418 rt1015_din_source_select);
420 static const char * const rt1015_boost_mode[] = {
421 "Bypass", "Adaptive", "Fixed Adaptive"
424 static SOC_ENUM_SINGLE_DECL(rt1015_boost_mode_enum, 0, 0,
427 static int rt1015_boost_mode_get(struct snd_kcontrol *kcontrol,
428 struct snd_ctl_elem_value *ucontrol)
430 struct snd_soc_component *component =
431 snd_soc_kcontrol_component(kcontrol);
432 struct rt1015_priv *rt1015 =
433 snd_soc_component_get_drvdata(component);
435 ucontrol->value.integer.value[0] = rt1015->boost_mode;
440 static int rt1015_boost_mode_put(struct snd_kcontrol *kcontrol,
441 struct snd_ctl_elem_value *ucontrol)
443 struct snd_soc_component *component =
444 snd_soc_kcontrol_component(kcontrol);
445 struct rt1015_priv *rt1015 =
446 snd_soc_component_get_drvdata(component);
447 int boost_mode = ucontrol->value.integer.value[0];
449 switch (boost_mode) {
451 snd_soc_component_update_bits(component,
452 RT1015_SMART_BST_CTRL1, RT1015_ABST_AUTO_EN_MASK |
453 RT1015_ABST_FIX_TGT_MASK | RT1015_BYPASS_SWR_REG_MASK,
454 RT1015_ABST_REG_MODE | RT1015_ABST_FIX_TGT_DIS |
455 RT1015_BYPASS_SWRREG_BYPASS);
458 snd_soc_component_update_bits(component,
459 RT1015_SMART_BST_CTRL1, RT1015_ABST_AUTO_EN_MASK |
460 RT1015_ABST_FIX_TGT_MASK | RT1015_BYPASS_SWR_REG_MASK,
461 RT1015_ABST_AUTO_MODE | RT1015_ABST_FIX_TGT_DIS |
462 RT1015_BYPASS_SWRREG_PASS);
465 snd_soc_component_update_bits(component,
466 RT1015_SMART_BST_CTRL1, RT1015_ABST_AUTO_EN_MASK |
467 RT1015_ABST_FIX_TGT_MASK | RT1015_BYPASS_SWR_REG_MASK,
468 RT1015_ABST_AUTO_MODE | RT1015_ABST_FIX_TGT_EN |
469 RT1015_BYPASS_SWRREG_PASS);
472 dev_err(component->dev, "Unknown boost control.\n");
476 rt1015->boost_mode = boost_mode;
481 static int rt1015_bypass_boost_get(struct snd_kcontrol *kcontrol,
482 struct snd_ctl_elem_value *ucontrol)
484 struct snd_soc_component *component =
485 snd_soc_kcontrol_component(kcontrol);
486 struct rt1015_priv *rt1015 =
487 snd_soc_component_get_drvdata(component);
489 ucontrol->value.integer.value[0] = rt1015->bypass_boost;
494 static void rt1015_calibrate(struct rt1015_priv *rt1015)
496 struct snd_soc_component *component = rt1015->component;
497 struct regmap *regmap = rt1015->regmap;
499 snd_soc_dapm_mutex_lock(&component->dapm);
500 regcache_cache_bypass(regmap, true);
502 regmap_write(regmap, RT1015_CLK_DET, 0x0000);
503 regmap_write(regmap, RT1015_PWR4, 0x00B2);
504 regmap_write(regmap, RT1015_PWR_STATE_CTRL, 0x0009);
506 regmap_write(regmap, RT1015_PWR_STATE_CTRL, 0x000A);
508 regmap_write(regmap, RT1015_PWR_STATE_CTRL, 0x000C);
510 regmap_write(regmap, RT1015_CLSD_INTERNAL8, 0x2028);
511 regmap_write(regmap, RT1015_CLSD_INTERNAL9, 0x0140);
512 regmap_write(regmap, RT1015_PWR_STATE_CTRL, 0x000D);
514 regmap_write(regmap, RT1015_PWR_STATE_CTRL, 0x0008);
515 regmap_write(regmap, RT1015_SYS_RST1, 0x05F5);
516 regmap_write(regmap, RT1015_CLK_DET, 0x8000);
518 regcache_cache_bypass(regmap, false);
519 regcache_mark_dirty(regmap);
520 regcache_sync(regmap);
521 snd_soc_dapm_mutex_unlock(&component->dapm);
524 static int rt1015_bypass_boost_put(struct snd_kcontrol *kcontrol,
525 struct snd_ctl_elem_value *ucontrol)
527 struct snd_soc_component *component =
528 snd_soc_kcontrol_component(kcontrol);
529 struct rt1015_priv *rt1015 =
530 snd_soc_component_get_drvdata(component);
532 if (rt1015->dac_is_used) {
533 dev_err(component->dev, "DAC is being used!\n");
537 rt1015->bypass_boost = ucontrol->value.integer.value[0];
538 if (rt1015->bypass_boost == RT1015_Bypass_Boost &&
539 !rt1015->cali_done) {
540 rt1015_calibrate(rt1015);
541 rt1015->cali_done = 1;
543 regmap_write(rt1015->regmap, RT1015_MONO_DYNA_CTRL, 0x0010);
549 static const char * const rt1015_dac_output_vol_select[] = {
551 "zero detection + immediate change",
552 "zero detection + inc/dec change",
553 "zero detection + soft inc/dec change",
556 static SOC_ENUM_SINGLE_DECL(rt1015_dac_vol_ctl_enum,
557 RT1015_DAC3, 2, rt1015_dac_output_vol_select);
559 static const struct snd_kcontrol_new rt1015_snd_controls[] = {
560 SOC_SINGLE_TLV("DAC Playback Volume", RT1015_DAC1, RT1015_DAC_VOL_SFT,
561 127, 0, dac_vol_tlv),
562 SOC_DOUBLE("DAC Playback Switch", RT1015_DAC3,
563 RT1015_DA_MUTE_SFT, RT1015_DVOL_MUTE_FLAG_SFT, 1, 1),
564 SOC_ENUM_EXT("Boost Mode", rt1015_boost_mode_enum,
565 rt1015_boost_mode_get, rt1015_boost_mode_put),
566 SOC_ENUM("Mono LR Select", rt1015_mono_lr_sel),
567 SOC_SINGLE_EXT("Bypass Boost", SND_SOC_NOPM, 0, 1, 0,
568 rt1015_bypass_boost_get, rt1015_bypass_boost_put),
570 /* DAC Output Volume Control */
571 SOC_ENUM("DAC Output Control", rt1015_dac_vol_ctl_enum),
574 static int rt1015_is_sys_clk_from_pll(struct snd_soc_dapm_widget *source,
575 struct snd_soc_dapm_widget *sink)
577 struct snd_soc_component *component =
578 snd_soc_dapm_to_component(source->dapm);
579 struct rt1015_priv *rt1015 = snd_soc_component_get_drvdata(component);
581 if (rt1015->sysclk_src == RT1015_SCLK_S_PLL)
587 static int r1015_dac_event(struct snd_soc_dapm_widget *w,
588 struct snd_kcontrol *kcontrol, int event)
590 struct snd_soc_component *component =
591 snd_soc_dapm_to_component(w->dapm);
592 struct rt1015_priv *rt1015 = snd_soc_component_get_drvdata(component);
595 case SND_SOC_DAPM_PRE_PMU:
596 rt1015->dac_is_used = 1;
597 if (rt1015->bypass_boost == RT1015_Enable_Boost) {
598 snd_soc_component_write(component,
599 RT1015_SYS_RST1, 0x05f7);
600 snd_soc_component_write(component,
601 RT1015_SYS_RST2, 0x0b0a);
602 snd_soc_component_write(component,
603 RT1015_GAT_BOOST, 0xacfe);
604 snd_soc_component_write(component,
605 RT1015_PWR9, 0xaa00);
606 snd_soc_component_write(component,
607 RT1015_GAT_BOOST, 0xecfe);
609 snd_soc_component_write(component,
611 snd_soc_component_write(component,
612 RT1015_SYS_RST1, 0x05f7);
613 snd_soc_component_write(component,
614 RT1015_SYS_RST2, 0x0b0a);
615 snd_soc_component_write(component,
616 RT1015_PWR_STATE_CTRL, 0x008e);
620 case SND_SOC_DAPM_POST_PMD:
621 if (rt1015->bypass_boost == RT1015_Enable_Boost) {
622 snd_soc_component_write(component,
623 RT1015_PWR9, 0xa800);
624 snd_soc_component_write(component,
625 RT1015_SYS_RST1, 0x05f5);
626 snd_soc_component_write(component,
627 RT1015_SYS_RST2, 0x0b9a);
629 snd_soc_component_write(component,
631 snd_soc_component_write(component,
632 RT1015_PWR_STATE_CTRL, 0x0088);
633 snd_soc_component_write(component,
634 RT1015_SYS_RST1, 0x05f5);
635 snd_soc_component_write(component,
636 RT1015_SYS_RST2, 0x0b9a);
638 rt1015->dac_is_used = 0;
647 static int rt1015_amp_drv_event(struct snd_soc_dapm_widget *w,
648 struct snd_kcontrol *kcontrol, int event)
650 struct snd_soc_component *component =
651 snd_soc_dapm_to_component(w->dapm);
652 struct rt1015_priv *rt1015 = snd_soc_component_get_drvdata(component);
653 unsigned int ret, ret2;
656 case SND_SOC_DAPM_PRE_PMU:
657 ret = snd_soc_component_read(component, RT1015_CLK_DET);
658 ret2 = snd_soc_component_read(component, RT1015_SPK_DC_DETECT1);
659 if (!((ret >> 15) & 0x1)) {
660 snd_soc_component_update_bits(component, RT1015_CLK_DET,
661 RT1015_EN_BCLK_DET_MASK, RT1015_EN_BCLK_DET);
662 dev_dbg(component->dev, "BCLK Detection Enabled.\n");
664 if (!((ret2 >> 12) & 0x1)) {
665 snd_soc_component_update_bits(component, RT1015_SPK_DC_DETECT1,
666 RT1015_EN_CLA_D_DC_DET_MASK, RT1015_EN_CLA_D_DC_DET);
667 dev_dbg(component->dev, "Class-D DC Detection Enabled.\n");
670 case SND_SOC_DAPM_POST_PMU:
671 msleep(rt1015->pdata.power_up_delay_ms);
679 static const struct snd_soc_dapm_widget rt1015_dapm_widgets[] = {
680 SND_SOC_DAPM_SUPPLY("PLL", RT1015_PWR1, RT1015_PWR_PLL_BIT, 0,
682 SND_SOC_DAPM_AIF_IN("AIFRX", "AIF Playback", 0, SND_SOC_NOPM, 0, 0),
683 SND_SOC_DAPM_DAC_E("DAC", NULL, SND_SOC_NOPM, 0, 0,
684 r1015_dac_event, SND_SOC_DAPM_PRE_PMU |
685 SND_SOC_DAPM_POST_PMD),
686 SND_SOC_DAPM_OUT_DRV_E("Amp Drv", SND_SOC_NOPM, 0, 0, NULL, 0,
687 rt1015_amp_drv_event, SND_SOC_DAPM_PRE_PMU |
688 SND_SOC_DAPM_POST_PMU),
689 SND_SOC_DAPM_OUTPUT("SPO"),
692 static const struct snd_soc_dapm_route rt1015_dapm_routes[] = {
693 { "DAC", NULL, "AIFRX" },
694 { "DAC", NULL, "PLL", rt1015_is_sys_clk_from_pll},
695 { "Amp Drv", NULL, "DAC" },
696 { "SPO", NULL, "Amp Drv" },
699 static int rt1015_hw_params(struct snd_pcm_substream *substream,
700 struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
702 struct snd_soc_component *component = dai->component;
703 struct rt1015_priv *rt1015 = snd_soc_component_get_drvdata(component);
704 int pre_div, frame_size, lrck;
705 unsigned int val_len = 0;
707 lrck = params_rate(params);
708 pre_div = rl6231_get_clk_info(rt1015->sysclk, lrck);
710 dev_err(component->dev, "Unsupported clock rate\n");
714 frame_size = snd_soc_params_to_frame_size(params);
715 if (frame_size < 0) {
716 dev_err(component->dev, "Unsupported frame size: %d\n",
721 dev_dbg(component->dev, "pre_div is %d for iis %d\n", pre_div, dai->id);
723 dev_dbg(component->dev, "lrck is %dHz and pre_div is %d for iis %d\n",
724 lrck, pre_div, dai->id);
726 switch (params_width(params)) {
730 val_len = RT1015_I2S_DL_20;
733 val_len = RT1015_I2S_DL_24;
736 val_len = RT1015_I2S_DL_8;
742 snd_soc_component_update_bits(component, RT1015_TDM_MASTER,
743 RT1015_I2S_DL_MASK, val_len);
744 snd_soc_component_update_bits(component, RT1015_CLK2,
745 RT1015_FS_PD_MASK, pre_div << RT1015_FS_PD_SFT);
750 static int rt1015_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
752 struct snd_soc_component *component = dai->component;
753 unsigned int reg_val = 0, reg_val2 = 0;
755 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
756 case SND_SOC_DAIFMT_CBM_CFM:
757 reg_val |= RT1015_TCON_TDM_MS_M;
759 case SND_SOC_DAIFMT_CBS_CFS:
760 reg_val |= RT1015_TCON_TDM_MS_S;
766 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
767 case SND_SOC_DAIFMT_NB_NF:
769 case SND_SOC_DAIFMT_IB_NF:
770 reg_val2 |= RT1015_TDM_INV_BCLK;
776 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
777 case SND_SOC_DAIFMT_I2S:
780 case SND_SOC_DAIFMT_LEFT_J:
781 reg_val |= RT1015_I2S_M_DF_LEFT;
784 case SND_SOC_DAIFMT_DSP_A:
785 reg_val |= RT1015_I2S_M_DF_PCM_A;
788 case SND_SOC_DAIFMT_DSP_B:
789 reg_val |= RT1015_I2S_M_DF_PCM_B;
796 snd_soc_component_update_bits(component, RT1015_TDM_MASTER,
797 RT1015_TCON_TDM_MS_MASK | RT1015_I2S_M_DF_MASK,
799 snd_soc_component_update_bits(component, RT1015_TDM1_1,
800 RT1015_TDM_INV_BCLK_MASK, reg_val2);
805 static int rt1015_set_component_sysclk(struct snd_soc_component *component,
806 int clk_id, int source, unsigned int freq, int dir)
808 struct rt1015_priv *rt1015 = snd_soc_component_get_drvdata(component);
809 unsigned int reg_val = 0;
811 if (freq == rt1015->sysclk && clk_id == rt1015->sysclk_src)
815 case RT1015_SCLK_S_MCLK:
816 reg_val |= RT1015_CLK_SYS_PRE_SEL_MCLK;
819 case RT1015_SCLK_S_PLL:
820 reg_val |= RT1015_CLK_SYS_PRE_SEL_PLL;
824 dev_err(component->dev, "Invalid clock id (%d)\n", clk_id);
828 rt1015->sysclk = freq;
829 rt1015->sysclk_src = clk_id;
831 dev_dbg(component->dev, "Sysclk is %dHz and clock id is %d\n",
834 snd_soc_component_update_bits(component, RT1015_CLK2,
835 RT1015_CLK_SYS_PRE_SEL_MASK, reg_val);
840 static int rt1015_set_component_pll(struct snd_soc_component *component,
841 int pll_id, int source, unsigned int freq_in,
842 unsigned int freq_out)
844 struct rt1015_priv *rt1015 = snd_soc_component_get_drvdata(component);
845 struct rl6231_pll_code pll_code;
848 if (!freq_in || !freq_out) {
849 dev_dbg(component->dev, "PLL disabled\n");
857 if (source == rt1015->pll_src && freq_in == rt1015->pll_in &&
858 freq_out == rt1015->pll_out)
862 case RT1015_PLL_S_MCLK:
863 snd_soc_component_update_bits(component, RT1015_CLK2,
864 RT1015_PLL_SEL_MASK, RT1015_PLL_SEL_PLL_SRC2);
867 case RT1015_PLL_S_BCLK:
868 snd_soc_component_update_bits(component, RT1015_CLK2,
869 RT1015_PLL_SEL_MASK, RT1015_PLL_SEL_BCLK);
873 dev_err(component->dev, "Unknown PLL Source %d\n", source);
877 ret = rl6231_pll_calc(freq_in, freq_out, &pll_code);
879 dev_err(component->dev, "Unsupported input clock %d\n", freq_in);
883 dev_dbg(component->dev, "bypass=%d m=%d n=%d k=%d\n",
884 pll_code.m_bp, (pll_code.m_bp ? 0 : pll_code.m_code),
885 pll_code.n_code, pll_code.k_code);
887 snd_soc_component_write(component, RT1015_PLL1,
888 ((pll_code.m_bp ? 0 : pll_code.m_code) << RT1015_PLL_M_SFT) |
889 (pll_code.m_bp << RT1015_PLL_M_BP_SFT) |
891 snd_soc_component_write(component, RT1015_PLL2,
894 rt1015->pll_in = freq_in;
895 rt1015->pll_out = freq_out;
896 rt1015->pll_src = source;
901 static int rt1015_set_tdm_slot(struct snd_soc_dai *dai,
902 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
904 struct snd_soc_component *component = dai->component;
905 unsigned int val = 0, rx_slotnum, tx_slotnum;
906 int ret = 0, first_bit;
910 val |= RT1015_I2S_TX_2CH;
913 val |= RT1015_I2S_TX_4CH;
916 val |= RT1015_I2S_TX_6CH;
919 val |= RT1015_I2S_TX_8CH;
926 switch (slot_width) {
928 val |= RT1015_I2S_CH_TX_LEN_16B;
931 val |= RT1015_I2S_CH_TX_LEN_20B;
934 val |= RT1015_I2S_CH_TX_LEN_24B;
937 val |= RT1015_I2S_CH_TX_LEN_32B;
944 /* Rx slot configuration */
945 rx_slotnum = hweight_long(rx_mask);
946 if (rx_slotnum != 1) {
948 dev_err(component->dev, "too many rx slots or zero slot\n");
952 /* This is an assumption that the system sends stereo audio to the amplifier typically.
953 * And the stereo audio is placed in slot 0/2/4/6 as the starting slot.
954 * The users could select the channel from L/R/L+R by "Mono LR Select" control.
956 first_bit = __ffs(rx_mask);
962 snd_soc_component_update_bits(component,
964 RT1015_TDM_I2S_TX_L_DAC1_1_MASK |
965 RT1015_TDM_I2S_TX_R_DAC1_1_MASK,
966 (first_bit << RT1015_TDM_I2S_TX_L_DAC1_1_SFT) |
967 ((first_bit+1) << RT1015_TDM_I2S_TX_R_DAC1_1_SFT));
973 snd_soc_component_update_bits(component,
975 RT1015_TDM_I2S_TX_L_DAC1_1_MASK |
976 RT1015_TDM_I2S_TX_R_DAC1_1_MASK,
977 ((first_bit-1) << RT1015_TDM_I2S_TX_L_DAC1_1_SFT) |
978 (first_bit << RT1015_TDM_I2S_TX_R_DAC1_1_SFT));
985 /* Tx slot configuration */
986 tx_slotnum = hweight_long(tx_mask);
989 dev_err(component->dev, "doesn't need to support tx slots\n");
993 snd_soc_component_update_bits(component, RT1015_TDM1_1,
994 RT1015_I2S_CH_TX_MASK | RT1015_I2S_CH_RX_MASK |
995 RT1015_I2S_CH_TX_LEN_MASK | RT1015_I2S_CH_RX_LEN_MASK, val);
1001 static int rt1015_probe(struct snd_soc_component *component)
1003 struct rt1015_priv *rt1015 =
1004 snd_soc_component_get_drvdata(component);
1006 rt1015->component = component;
1011 static void rt1015_remove(struct snd_soc_component *component)
1013 struct rt1015_priv *rt1015 = snd_soc_component_get_drvdata(component);
1015 regmap_write(rt1015->regmap, RT1015_RESET, 0);
1018 #define RT1015_STEREO_RATES SNDRV_PCM_RATE_8000_192000
1019 #define RT1015_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
1020 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S8)
1022 static const struct snd_soc_dai_ops rt1015_aif_dai_ops = {
1023 .hw_params = rt1015_hw_params,
1024 .set_fmt = rt1015_set_dai_fmt,
1025 .set_tdm_slot = rt1015_set_tdm_slot,
1028 static struct snd_soc_dai_driver rt1015_dai[] = {
1030 .name = "rt1015-aif",
1033 .stream_name = "AIF Playback",
1036 .rates = RT1015_STEREO_RATES,
1037 .formats = RT1015_FORMATS,
1039 .ops = &rt1015_aif_dai_ops,
1044 static int rt1015_suspend(struct snd_soc_component *component)
1046 struct rt1015_priv *rt1015 = snd_soc_component_get_drvdata(component);
1048 regcache_cache_only(rt1015->regmap, true);
1049 regcache_mark_dirty(rt1015->regmap);
1054 static int rt1015_resume(struct snd_soc_component *component)
1056 struct rt1015_priv *rt1015 = snd_soc_component_get_drvdata(component);
1058 regcache_cache_only(rt1015->regmap, false);
1059 regcache_sync(rt1015->regmap);
1061 if (rt1015->cali_done)
1062 rt1015_calibrate(rt1015);
1067 #define rt1015_suspend NULL
1068 #define rt1015_resume NULL
1071 static const struct snd_soc_component_driver soc_component_dev_rt1015 = {
1072 .probe = rt1015_probe,
1073 .remove = rt1015_remove,
1074 .suspend = rt1015_suspend,
1075 .resume = rt1015_resume,
1076 .controls = rt1015_snd_controls,
1077 .num_controls = ARRAY_SIZE(rt1015_snd_controls),
1078 .dapm_widgets = rt1015_dapm_widgets,
1079 .num_dapm_widgets = ARRAY_SIZE(rt1015_dapm_widgets),
1080 .dapm_routes = rt1015_dapm_routes,
1081 .num_dapm_routes = ARRAY_SIZE(rt1015_dapm_routes),
1082 .set_sysclk = rt1015_set_component_sysclk,
1083 .set_pll = rt1015_set_component_pll,
1084 .use_pmdown_time = 1,
1088 static const struct regmap_config rt1015_regmap = {
1091 .max_register = RT1015_S_BST_TIMING_INTER36,
1092 .volatile_reg = rt1015_volatile_register,
1093 .readable_reg = rt1015_readable_register,
1094 .cache_type = REGCACHE_RBTREE,
1095 .reg_defaults = rt1015_reg,
1096 .num_reg_defaults = ARRAY_SIZE(rt1015_reg),
1099 static const struct i2c_device_id rt1015_i2c_id[] = {
1103 MODULE_DEVICE_TABLE(i2c, rt1015_i2c_id);
1105 #if defined(CONFIG_OF)
1106 static const struct of_device_id rt1015_of_match[] = {
1107 { .compatible = "realtek,rt1015", },
1110 MODULE_DEVICE_TABLE(of, rt1015_of_match);
1114 static const struct acpi_device_id rt1015_acpi_match[] = {
1118 MODULE_DEVICE_TABLE(acpi, rt1015_acpi_match);
1121 static void rt1015_parse_dt(struct rt1015_priv *rt1015, struct device *dev)
1123 device_property_read_u32(dev, "realtek,power-up-delay-ms",
1124 &rt1015->pdata.power_up_delay_ms);
1127 static int rt1015_i2c_probe(struct i2c_client *i2c)
1129 struct rt1015_platform_data *pdata = dev_get_platdata(&i2c->dev);
1130 struct rt1015_priv *rt1015;
1134 rt1015 = devm_kzalloc(&i2c->dev, sizeof(*rt1015), GFP_KERNEL);
1138 i2c_set_clientdata(i2c, rt1015);
1140 rt1015->pdata = i2s_default_platform_data;
1143 rt1015->pdata = *pdata;
1145 rt1015_parse_dt(rt1015, &i2c->dev);
1147 rt1015->regmap = devm_regmap_init_i2c(i2c, &rt1015_regmap);
1148 if (IS_ERR(rt1015->regmap)) {
1149 ret = PTR_ERR(rt1015->regmap);
1150 dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
1155 ret = regmap_read(rt1015->regmap, RT1015_DEVICE_ID, &val);
1158 "Failed to read device register: %d\n", ret);
1160 } else if ((val != RT1015_DEVICE_ID_VAL) &&
1161 (val != RT1015_DEVICE_ID_VAL2)) {
1163 "Device with ID register %x is not rt1015\n", val);
1167 return devm_snd_soc_register_component(&i2c->dev,
1168 &soc_component_dev_rt1015,
1169 rt1015_dai, ARRAY_SIZE(rt1015_dai));
1172 static void rt1015_i2c_shutdown(struct i2c_client *client)
1174 struct rt1015_priv *rt1015 = i2c_get_clientdata(client);
1176 regmap_write(rt1015->regmap, RT1015_RESET, 0);
1179 static struct i2c_driver rt1015_i2c_driver = {
1182 .of_match_table = of_match_ptr(rt1015_of_match),
1183 .acpi_match_table = ACPI_PTR(rt1015_acpi_match),
1185 .probe = rt1015_i2c_probe,
1186 .shutdown = rt1015_i2c_shutdown,
1187 .id_table = rt1015_i2c_id,
1189 module_i2c_driver(rt1015_i2c_driver);
1191 MODULE_DESCRIPTION("ASoC RT1015 driver");
1193 MODULE_LICENSE("GPL v2");