1 // SPDX-License-Identifier: GPL-2.0-only
3 // rt1318.c -- RT1318 ALSA SoC audio amplifier driver
4 // Author: Jack Yu <jack.yu@realtek.com>
6 // Copyright(c) 2024 Realtek Semiconductor Corp.
10 #include <linux/acpi.h>
12 #include <linux/module.h>
13 #include <linux/moduleparam.h>
14 #include <linux/init.h>
15 #include <linux/delay.h>
17 #include <linux/regmap.h>
18 #include <linux/i2c.h>
19 #include <linux/platform_device.h>
20 #include <linux/firmware.h>
21 #include <linux/gpio.h>
22 #include <sound/core.h>
23 #include <sound/pcm.h>
24 #include <sound/pcm_params.h>
25 #include <sound/soc.h>
26 #include <sound/soc-dapm.h>
27 #include <sound/initval.h>
28 #include <sound/tlv.h>
29 #include <sound/rt1318.h>
33 static const struct reg_sequence init_list
[] = {
258 static const struct reg_default rt1318_reg
[] = {
403 static bool rt1318_volatile_register(struct device
*dev
, unsigned int reg
)
409 case 0xc430 ... 0xc431:
412 case 0xdb1d ... 0xdb1f:
415 case 0xdb8a ... 0xdb92:
416 case 0xdbc5 ... 0xdbc8:
417 case 0xdc2b ... 0xdc49:
420 case 0xdd1d ... 0xdd1f:
422 case 0xdd8a ... 0xdd92:
423 case 0xddc5 ... 0xddc8:
424 case 0xde2b ... 0xde44:
425 case 0xdf4a ... 0xdf55:
426 case 0xe224 ... 0xe23b:
430 case 0xebcb ... 0xebcc:
431 case 0xed03 ... 0xed06:
432 case 0xf010 ... 0xf014:
440 static bool rt1318_readable_register(struct device
*dev
, unsigned int reg
)
443 case 0xc000 ... 0xc00f:
444 case 0xc120 ... 0xc130:
445 case 0xc200 ... 0xc20e:
446 case 0xc300 ... 0xc303:
447 case 0xc320 ... 0xc322:
448 case 0xc400 ... 0xc408:
449 case 0xc430 ... 0xc431:
450 case 0xca00 ... 0xca02:
451 case 0xca10 ... 0xca12:
452 case 0xcb00 ... 0xcb0b:
453 case 0xcc00 ... 0xcce5:
454 case 0xcd00 ... 0xcde5:
455 case 0xce00 ... 0xce6a:
456 case 0xcf00 ... 0xcf53:
457 case 0xd000 ... 0xd0cc:
458 case 0xd100 ... 0xd1b9:
459 case 0xdb00 ... 0xdc53:
460 case 0xdd00 ... 0xde53:
461 case 0xdf00 ... 0xdf6b:
465 case 0xe706 ... 0xe707:
466 case 0xe806 ... 0xe807:
468 case 0xeb00 ... 0xebcc:
469 case 0xec00 ... 0xecb9:
470 case 0xed00 ... 0xed06:
471 case 0xf010 ... 0xf014:
472 case 0xf102 ... 0xf10b:
474 case 0xf20d ... 0xf242:
475 case 0xf800 ... 0xf807:
482 static int rt1318_dac_event(struct snd_soc_dapm_widget
*w
,
483 struct snd_kcontrol
*kcontrol
, int event
)
485 struct snd_soc_component
*component
= snd_soc_dapm_to_component(w
->dapm
);
486 struct rt1318_priv
*rt1318
= snd_soc_component_get_drvdata(component
);
489 case SND_SOC_DAPM_PRE_PMU
:
490 regmap_update_bits(rt1318
->regmap
, RT1318_PWR_STA1
,
491 RT1318_PDB_CTRL_MASK
, RT1318_PDB_CTRL_HIGH
);
494 case SND_SOC_DAPM_POST_PMD
:
495 regmap_update_bits(rt1318
->regmap
, RT1318_PWR_STA1
,
496 RT1318_PDB_CTRL_MASK
, RT1318_PDB_CTRL_LOW
);
505 static int rt1318_dvol_put(struct snd_kcontrol
*kcontrol
,
506 struct snd_ctl_elem_value
*ucontrol
)
508 struct snd_soc_component
*component
= snd_soc_kcontrol_component(kcontrol
);
509 struct rt1318_priv
*rt1318
= snd_soc_component_get_drvdata(component
);
511 rt1318
->rt1318_dvol
= ucontrol
->value
.integer
.value
[0];
513 if (rt1318
->rt1318_dvol
<= RT1318_DVOL_STEP
&& rt1318
->rt1318_dvol
>= 0) {
514 regmap_write(rt1318
->regmap
, RT1318_DA_VOL_L_8
,
515 rt1318
->rt1318_dvol
>> 8);
516 regmap_write(rt1318
->regmap
, RT1318_DA_VOL_L_1_7
,
517 rt1318
->rt1318_dvol
& 0xff);
518 regmap_write(rt1318
->regmap
, RT1318_DA_VOL_R_8
,
519 rt1318
->rt1318_dvol
>> 8);
520 regmap_write(rt1318
->regmap
, RT1318_DA_VOL_R_1_7
,
521 rt1318
->rt1318_dvol
& 0xff);
528 static int rt1318_dvol_get(struct snd_kcontrol
*kcontrol
,
529 struct snd_ctl_elem_value
*ucontrol
)
531 struct snd_soc_component
*component
= snd_soc_kcontrol_component(kcontrol
);
532 struct rt1318_priv
*rt1318
= snd_soc_component_get_drvdata(component
);
534 ucontrol
->value
.integer
.value
[0] = rt1318
->rt1318_dvol
;
539 static const struct snd_kcontrol_new rt1318_snd_controls
[] = {
540 SOC_SINGLE_EXT("Amp Playback Volume", SND_SOC_NOPM
, 0, 383, 0,
541 rt1318_dvol_get
, rt1318_dvol_put
),
544 static const struct snd_soc_dapm_widget rt1318_dapm_widgets
[] = {
545 /* Audio Interface */
546 SND_SOC_DAPM_AIF_IN("AIF1RX", "AIF1 Playback", 0, SND_SOC_NOPM
, 0, 0),
548 SND_SOC_DAPM_DAC_E("DAC", NULL
, SND_SOC_NOPM
, 0, 0,
549 rt1318_dac_event
, SND_SOC_DAPM_PRE_PMU
|
550 SND_SOC_DAPM_POST_PMD
),
552 SND_SOC_DAPM_OUTPUT("Amp"),
555 static const struct snd_soc_dapm_route rt1318_dapm_routes
[] = {
556 {"DAC", NULL
, "AIF1RX"},
557 {"Amp", NULL
, "DAC"},
560 static int rt1318_get_clk_info(int sclk
, int rate
)
562 int i
, pd
[] = {1, 2, 4, 8, 16, 24};
564 if (sclk
<= 0 || rate
<= 0)
568 for (i
= 0; i
< ARRAY_SIZE(pd
); i
++)
569 if (sclk
== rate
* pd
[i
])
575 static int rt1318_clk_ip_info(struct snd_soc_component
*component
, int lrclk
)
577 struct rt1318_priv
*rt1318
= snd_soc_component_get_drvdata(component
);
580 case RT1318_LRCLK_48000
:
581 case RT1318_LRCLK_44100
:
582 case RT1318_LRCLK_16000
:
583 regmap_update_bits(rt1318
->regmap
, RT1318_SRC_TCON
,
584 RT1318_SRCIN_F12288_MASK
| RT1318_SRCIN_DACLK_MASK
,
585 RT1318_SRCIN_TCON4
| RT1318_DACLK_TCON4
);
587 case RT1318_LRCLK_96000
:
588 regmap_update_bits(rt1318
->regmap
, RT1318_SRC_TCON
,
589 RT1318_SRCIN_F12288_MASK
| RT1318_SRCIN_DACLK_MASK
,
590 RT1318_SRCIN_TCON4
| RT1318_DACLK_TCON2
);
592 case RT1318_LRCLK_192000
:
593 regmap_update_bits(rt1318
->regmap
, RT1318_SRC_TCON
,
594 RT1318_SRCIN_F12288_MASK
| RT1318_SRCIN_DACLK_MASK
,
595 RT1318_SRCIN_TCON4
| RT1318_DACLK_TCON1
);
598 dev_err(component
->dev
, "Unsupported clock rate.\n");
605 static int rt1318_hw_params(struct snd_pcm_substream
*substream
,
606 struct snd_pcm_hw_params
*params
, struct snd_soc_dai
*dai
)
608 struct snd_soc_component
*component
= dai
->component
;
609 struct rt1318_priv
*rt1318
= snd_soc_component_get_drvdata(component
);
610 int data_len
= 0, ch_len
= 0;
613 rt1318
->lrck
= params_rate(params
);
614 pre_div
= rt1318_get_clk_info(rt1318
->sysclk
, rt1318
->lrck
);
616 dev_err(component
->dev
, "Unsupported clock setting\n");
619 ret
= rt1318_clk_ip_info(component
, rt1318
->lrck
);
621 dev_err(component
->dev
, "Unsupported clock setting\n");
625 switch (params_width(params
)) {
629 data_len
= RT1318_I2S_DL_20
;
630 ch_len
= RT1318_I2S_DL_20
;
633 data_len
= RT1318_I2S_DL_24
;
634 ch_len
= RT1318_I2S_DL_24
;
637 data_len
= RT1318_I2S_DL_32
;
638 ch_len
= RT1318_I2S_DL_32
;
641 data_len
= RT1318_I2S_DL_8
;
642 ch_len
= RT1318_I2S_DL_8
;
648 regmap_update_bits(rt1318
->regmap
, RT1318_CLK2
,
649 RT1318_DIV_AP_MASK
| RT1318_DIV_DAMOD_MASK
,
650 pre_div
<< RT1318_DIV_AP_SFT
|
651 pre_div
<< RT1318_DIV_DAMOD_SFT
);
652 regmap_update_bits(rt1318
->regmap
, RT1318_CLK3
,
653 RT1318_AD_STO1_MASK
| RT1318_AD_STO2_MASK
,
654 pre_div
<< RT1318_AD_STO1_SFT
|
655 pre_div
<< RT1318_AD_STO2_SFT
);
656 regmap_update_bits(rt1318
->regmap
, RT1318_CLK4
,
657 RT1318_AD_ANA_STO1_MASK
| RT1318_AD_ANA_STO2_MASK
,
658 pre_div
<< RT1318_AD_ANA_STO1_SFT
|
659 pre_div
<< RT1318_AD_ANA_STO2_SFT
);
660 regmap_update_bits(rt1318
->regmap
, RT1318_CLK5
,
661 RT1318_DIV_FIFO_IN_MASK
| RT1318_DIV_FIFO_OUT_MASK
,
662 pre_div
<< RT1318_DIV_FIFO_IN_SFT
|
663 pre_div
<< RT1318_DIV_FIFO_OUT_SFT
);
664 regmap_update_bits(rt1318
->regmap
, RT1318_CLK6
,
665 RT1318_DIV_NLMS_MASK
| RT1318_DIV_AD_MONO_MASK
|
666 RT1318_DIV_POST_G_MASK
, pre_div
<< RT1318_DIV_NLMS_SFT
|
667 pre_div
<< RT1318_DIV_AD_MONO_SFT
|
668 pre_div
<< RT1318_DIV_POST_G_SFT
);
670 regmap_update_bits(rt1318
->regmap
, RT1318_TDM_CTRL2
,
671 RT1318_I2S_DL_MASK
, data_len
<< RT1318_I2S_DL_SFT
);
672 regmap_update_bits(rt1318
->regmap
, RT1318_TDM_CTRL3
,
673 RT1318_I2S_TX_CHL_MASK
| RT1318_I2S_RX_CHL_MASK
,
674 ch_len
<< RT1318_I2S_TX_CHL_SFT
|
675 ch_len
<< RT1318_I2S_RX_CHL_SFT
);
680 static int rt1318_set_dai_fmt(struct snd_soc_dai
*dai
, unsigned int fmt
)
682 struct snd_soc_component
*component
= dai
->component
;
683 struct rt1318_priv
*rt1318
= snd_soc_component_get_drvdata(component
);
684 unsigned int reg_val
= 0, reg_val2
= 0;
686 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
687 case SND_SOC_DAIFMT_NB_NF
:
689 case SND_SOC_DAIFMT_IB_NF
:
690 reg_val2
|= RT1318_TDM_BCLK_INV
;
696 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
697 case SND_SOC_DAIFMT_I2S
:
700 case SND_SOC_DAIFMT_LEFT_J
:
701 reg_val
|= RT1318_FMT_LEFT_J
;
704 case SND_SOC_DAIFMT_DSP_A
:
705 reg_val
|= RT1318_FMT_PCM_A_R
;
708 case SND_SOC_DAIFMT_DSP_B
:
709 reg_val
|= RT1318_FMT_PCM_B_R
;
716 regmap_update_bits(rt1318
->regmap
, RT1318_TDM_CTRL1
,
717 RT1318_I2S_FMT_MASK
, reg_val
);
718 regmap_update_bits(rt1318
->regmap
, RT1318_TDM_CTRL1
,
719 RT1318_TDM_BCLK_MASK
, reg_val2
);
724 static int rt1318_set_dai_sysclk(struct snd_soc_dai
*dai
,
725 int clk_id
, unsigned int freq
, int dir
)
727 struct snd_soc_component
*component
= dai
->component
;
728 struct rt1318_priv
*rt1318
= snd_soc_component_get_drvdata(component
);
731 if (freq
== rt1318
->sysclk
&& clk_id
== rt1318
->sysclk_src
)
735 case RT1318_SCLK_S_BCLK
:
736 reg_val
|= RT1318_SYSCLK_BCLK
;
738 case RT1318_SCLK_S_SDW
:
739 reg_val
|= RT1318_SYSCLK_SDW
;
741 case RT1318_SCLK_S_PLL2F
:
742 reg_val
|= RT1318_SYSCLK_PLL2F
;
744 case RT1318_SCLK_S_PLL2B
:
745 reg_val
|= RT1318_SYSCLK_PLL2B
;
747 case RT1318_SCLK_S_MCLK
:
748 reg_val
|= RT1318_SYSCLK_MCLK
;
750 case RT1318_SCLK_S_RC0
:
751 reg_val
|= RT1318_SYSCLK_RC1
;
753 case RT1318_SCLK_S_RC1
:
754 reg_val
|= RT1318_SYSCLK_RC2
;
756 case RT1318_SCLK_S_RC2
:
757 reg_val
|= RT1318_SYSCLK_RC3
;
760 dev_err(component
->dev
, "Invalid clock id (%d)\n", clk_id
);
764 rt1318
->sysclk
= freq
;
765 rt1318
->sysclk_src
= clk_id
;
766 dev_dbg(dai
->dev
, "Sysclk is %dHz and clock id is %d\n", freq
, clk_id
);
767 regmap_update_bits(rt1318
->regmap
, RT1318_CLK1
,
768 RT1318_SYSCLK_SEL_MASK
, reg_val
);
773 static const struct pll_calc_map pll_preset_table
[] = {
774 {512000, 4096000, 22, 190, 0, true, false},
775 {1024000, 4096000, 22, 94, 0, true, false},
776 {1024000, 16384000, 4, 190, 0, true, false},
777 {1411200, 11289600, 6, 62, 0, true, false},
778 {1536000, 12288000, 6, 62, 0, true, false},
779 {2822400, 11289600, 6, 62, 0, true, false},
780 {2822400, 45158400, 0, 62, 0, true, false},
781 {2822400, 49152000, 0, 62, 0, true, false},
782 {3072000, 12288000, 6, 62, 0, true, false},
783 {3072000, 24576000, 2, 62, 0, true, false},
784 {3072000, 49152000, 0, 62, 0, true, false},
785 {6144000, 24576000, 2, 94, 4, false, false},
786 {6144000, 49152000, 0, 30, 0, true, false},
787 {6144000, 98304000, 0, 94, 4, false, true},
788 {12288000, 49152000, 0, 62, 6, false, false},
791 static int rt1318_pll_calc(const unsigned int freq_in
,
792 const unsigned int freq_out
, struct rt1318_pll_code
*pll_code
)
794 int max_n
= RT1318_PLL_N_MAX
, max_m
= RT1318_PLL_M_MAX
;
795 int i
, k
, red
, n_t
, pll_out
, in_t
, out_t
;
796 int n
= 0, m
= 0, m_t
= 0;
797 int red_t
= abs(freq_out
- freq_in
);
798 bool m_bypass
= false, k_bypass
= false;
800 if (RT1318_PLL_INP_MAX
< freq_in
|| RT1318_PLL_INP_MIN
> freq_in
)
803 for (i
= 0; i
< ARRAY_SIZE(pll_preset_table
); i
++) {
804 if (freq_in
== pll_preset_table
[i
].pll_in
&&
805 freq_out
== pll_preset_table
[i
].pll_out
) {
806 k
= pll_preset_table
[i
].k
;
807 m
= pll_preset_table
[i
].m
;
808 n
= pll_preset_table
[i
].n
;
809 m_bypass
= pll_preset_table
[i
].m_bp
;
810 k_bypass
= pll_preset_table
[i
].k_bp
;
815 k
= 100000000 / freq_out
- 2;
816 if (k
> RT1318_PLL_K_MAX
)
817 k
= RT1318_PLL_K_MAX
;
822 for (n_t
= 0; n_t
<= max_n
; n_t
++) {
823 in_t
= freq_in
/ (k_bypass
? 1 : (k
+ 2));
824 pll_out
= freq_out
/ (n_t
+ 2);
827 if (in_t
== pll_out
) {
832 red
= abs(in_t
- pll_out
);
841 for (m_t
= 0; m_t
<= max_m
; m_t
++) {
842 out_t
= in_t
/ (m_t
+ 2);
843 red
= abs(out_t
- pll_out
);
854 pr_debug("Only get approximation about PLL\n");
858 pll_code
->m_bp
= m_bypass
;
859 pll_code
->k_bp
= k_bypass
;
860 pll_code
->m_code
= m
;
861 pll_code
->n_code
= n
;
862 pll_code
->k_code
= k
;
866 static int rt1318_set_dai_pll(struct snd_soc_dai
*dai
, int pll_id
, int source
,
867 unsigned int freq_in
, unsigned int freq_out
)
869 struct snd_soc_component
*component
= dai
->component
;
870 struct rt1318_priv
*rt1318
= snd_soc_component_get_drvdata(component
);
871 struct rt1318_pll_code pll_code
;
874 if (!freq_in
|| !freq_out
) {
875 dev_dbg(component
->dev
, "PLL disabled\n");
881 if (source
== rt1318
->pll_src
&& freq_in
== rt1318
->pll_in
&&
882 freq_out
== rt1318
->pll_out
)
886 case RT1318_PLL_S_BCLK0
:
887 regmap_update_bits(rt1318
->regmap
, RT1318_CLK1
,
888 RT1318_PLLIN_MASK
, RT1318_PLLIN_BCLK0
);
890 case RT1318_PLL_S_BCLK1
:
891 regmap_update_bits(rt1318
->regmap
, RT1318_CLK1
,
892 RT1318_PLLIN_MASK
, RT1318_PLLIN_BCLK1
);
894 case RT1318_PLL_S_RC
:
895 regmap_update_bits(rt1318
->regmap
, RT1318_CLK1
,
896 RT1318_PLLIN_MASK
, RT1318_PLLIN_RC
);
898 case RT1318_PLL_S_MCLK
:
899 regmap_update_bits(rt1318
->regmap
, RT1318_CLK1
,
900 RT1318_PLLIN_MASK
, RT1318_PLLIN_MCLK
);
902 case RT1318_PLL_S_SDW_IN_PLL
:
903 regmap_update_bits(rt1318
->regmap
, RT1318_CLK1
,
904 RT1318_PLLIN_MASK
, RT1318_PLLIN_SDW1
);
906 case RT1318_PLL_S_SDW_0
:
907 regmap_update_bits(rt1318
->regmap
, RT1318_CLK1
,
908 RT1318_PLLIN_MASK
, RT1318_PLLIN_SDW2
);
910 case RT1318_PLL_S_SDW_1
:
911 regmap_update_bits(rt1318
->regmap
, RT1318_CLK1
,
912 RT1318_PLLIN_MASK
, RT1318_PLLIN_SDW3
);
914 case RT1318_PLL_S_SDW_2
:
915 regmap_update_bits(rt1318
->regmap
, RT1318_CLK1
,
916 RT1318_PLLIN_MASK
, RT1318_PLLIN_SDW4
);
919 dev_err(component
->dev
, "Unknown PLL source %d\n", source
);
923 ret
= rt1318_pll_calc(freq_in
, freq_out
, &pll_code
);
925 dev_err(component
->dev
, "Unsupport input clock %d\n", freq_in
);
929 dev_dbg(component
->dev
, "bypass=%d m=%d n=%d k=%d\n",
930 pll_code
.m_bp
, (pll_code
.m_bp
? 0 : pll_code
.m_code
),
931 pll_code
.n_code
, pll_code
.k_code
);
933 regmap_update_bits(rt1318
->regmap
, RT1318_PLL1_K
,
934 RT1318_K_PLL1_MASK
, pll_code
.k_code
);
935 regmap_update_bits(rt1318
->regmap
, RT1318_PLL1_M
,
936 RT1318_M_PLL1_MASK
, (pll_code
.m_bp
? 0 : pll_code
.m_code
));
937 regmap_update_bits(rt1318
->regmap
, RT1318_PLL1_N_8
,
938 RT1318_N_8_PLL1_MASK
, pll_code
.n_code
>> 8);
939 regmap_update_bits(rt1318
->regmap
, RT1318_PLL1_N_7_0
,
940 RT1318_N_7_0_PLL1_MASK
, pll_code
.n_code
);
942 rt1318
->pll_in
= freq_in
;
943 rt1318
->pll_out
= freq_out
;
944 rt1318
->pll_src
= source
;
949 static int rt1318_set_tdm_slot(struct snd_soc_dai
*dai
, unsigned int tx_mask
,
950 unsigned int rx_mask
, int slots
, int slot_width
)
952 struct snd_soc_component
*component
= dai
->component
;
953 struct rt1318_priv
*rt1318
= snd_soc_component_get_drvdata(component
);
954 unsigned int cn
= 0, cl
= 0, rx_slotnum
;
955 int ret
= 0, first_bit
;
959 cn
|= RT1318_I2S_CH_TX_4CH
;
960 cn
|= RT1318_I2S_CH_RX_4CH
;
963 cn
|= RT1318_I2S_CH_TX_6CH
;
964 cn
|= RT1318_I2S_CH_RX_6CH
;
967 cn
|= RT1318_I2S_CH_TX_8CH
;
968 cn
|= RT1318_I2S_CH_RX_8CH
;
976 switch (slot_width
) {
978 cl
|= RT1318_I2S_TX_CHL_20
;
979 cl
|= RT1318_I2S_RX_CHL_20
;
982 cl
|= RT1318_I2S_TX_CHL_24
;
983 cl
|= RT1318_I2S_RX_CHL_24
;
986 cl
|= RT1318_I2S_TX_CHL_32
;
987 cl
|= RT1318_I2S_RX_CHL_32
;
990 cl
|= RT1318_I2S_TX_CHL_8
;
991 cl
|= RT1318_I2S_RX_CHL_8
;
999 /* Rx slot configuration */
1000 rx_slotnum
= hweight_long(rx_mask
);
1001 if (rx_slotnum
!= 1) {
1003 dev_err(component
->dev
, "too many rx slots or zero slot\n");
1007 first_bit
= __ffs(rx_mask
);
1008 switch (first_bit
) {
1013 regmap_update_bits(rt1318
->regmap
,
1015 RT1318_TDM_I2S_TX_L_DAC1_1_MASK
|
1016 RT1318_TDM_I2S_TX_R_DAC1_1_MASK
,
1017 (first_bit
<< RT1318_TDM_I2S_TX_L_DAC1_1_SFT
) |
1018 ((first_bit
+ 1) << RT1318_TDM_I2S_TX_R_DAC1_1_SFT
));
1024 regmap_update_bits(rt1318
->regmap
,
1026 RT1318_TDM_I2S_TX_L_DAC1_1_MASK
|
1027 RT1318_TDM_I2S_TX_R_DAC1_1_MASK
,
1028 ((first_bit
- 1) << RT1318_TDM_I2S_TX_L_DAC1_1_SFT
) |
1029 (first_bit
<< RT1318_TDM_I2S_TX_R_DAC1_1_SFT
));
1036 regmap_update_bits(rt1318
->regmap
, RT1318_TDM_CTRL2
,
1037 RT1318_I2S_CH_TX_MASK
| RT1318_I2S_CH_RX_MASK
, cn
);
1038 regmap_update_bits(rt1318
->regmap
, RT1318_TDM_CTRL3
,
1039 RT1318_I2S_TX_CHL_MASK
| RT1318_I2S_RX_CHL_MASK
, cl
);
1045 static int rt1318_probe(struct snd_soc_component
*component
)
1047 struct rt1318_priv
*rt1318
= snd_soc_component_get_drvdata(component
);
1049 rt1318
->component
= component
;
1051 schedule_work(&rt1318
->cali_work
);
1052 rt1318
->rt1318_dvol
= RT1318_DVOL_STEP
;
1057 static void rt1318_remove(struct snd_soc_component
*component
)
1059 struct rt1318_priv
*rt1318
= snd_soc_component_get_drvdata(component
);
1061 cancel_work_sync(&rt1318
->cali_work
);
1065 static int rt1318_suspend(struct snd_soc_component
*component
)
1067 struct rt1318_priv
*rt1318
= snd_soc_component_get_drvdata(component
);
1069 regcache_cache_only(rt1318
->regmap
, true);
1070 regcache_mark_dirty(rt1318
->regmap
);
1074 static int rt1318_resume(struct snd_soc_component
*component
)
1076 struct rt1318_priv
*rt1318
= snd_soc_component_get_drvdata(component
);
1078 regcache_cache_only(rt1318
->regmap
, false);
1079 regcache_sync(rt1318
->regmap
);
1083 #define rt1318_suspend NULL
1084 #define rt1318_resume NULL
1087 #define RT1318_STEREO_RATES SNDRV_PCM_RATE_8000_192000
1088 #define RT1318_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
1089 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S8)
1091 static const struct snd_soc_dai_ops rt1318_aif_dai_ops
= {
1092 .hw_params
= rt1318_hw_params
,
1093 .set_fmt
= rt1318_set_dai_fmt
,
1094 .set_sysclk
= rt1318_set_dai_sysclk
,
1095 .set_pll
= rt1318_set_dai_pll
,
1096 .set_tdm_slot
= rt1318_set_tdm_slot
,
1099 static struct snd_soc_dai_driver rt1318_dai
[] = {
1101 .name
= "rt1318-aif",
1104 .stream_name
= "AIF1 Playback",
1107 .rates
= RT1318_STEREO_RATES
,
1108 .formats
= RT1318_FORMATS
,
1110 .ops
= &rt1318_aif_dai_ops
,
1114 static const struct snd_soc_component_driver soc_component_dev_rt1318
= {
1115 .probe
= rt1318_probe
,
1116 .remove
= rt1318_remove
,
1117 .suspend
= rt1318_suspend
,
1118 .resume
= rt1318_resume
,
1119 .controls
= rt1318_snd_controls
,
1120 .num_controls
= ARRAY_SIZE(rt1318_snd_controls
),
1121 .dapm_widgets
= rt1318_dapm_widgets
,
1122 .num_dapm_widgets
= ARRAY_SIZE(rt1318_dapm_widgets
),
1123 .dapm_routes
= rt1318_dapm_routes
,
1124 .num_dapm_routes
= ARRAY_SIZE(rt1318_dapm_routes
),
1125 .use_pmdown_time
= 1,
1129 static const struct regmap_config rt1318_regmap
= {
1132 .readable_reg
= rt1318_readable_register
,
1133 .volatile_reg
= rt1318_volatile_register
,
1134 .max_register
= 0x41001888,
1135 .reg_defaults
= rt1318_reg
,
1136 .num_reg_defaults
= ARRAY_SIZE(rt1318_reg
),
1137 .cache_type
= REGCACHE_RBTREE
,
1138 .use_single_read
= true,
1139 .use_single_write
= true,
1142 static const struct i2c_device_id rt1318_i2c_id
[] = {
1146 MODULE_DEVICE_TABLE(i2c
, rt1318_i2c_id
);
1148 static const struct of_device_id rt1318_of_match
[] = {
1149 { .compatible
= "realtek,rt1318", },
1152 MODULE_DEVICE_TABLE(of
, rt1318_of_match
);
1155 static const struct acpi_device_id rt1318_acpi_match
[] = {
1159 MODULE_DEVICE_TABLE(acpi
, rt1318_acpi_match
);
1162 static int rt1318_parse_dt(struct rt1318_priv
*rt1318
, struct device
*dev
)
1164 device_property_read_u32(dev
, "realtek,r0_l",
1165 &rt1318
->pdata
.init_r0_l
);
1166 device_property_read_u32(dev
, "realtek,r0_r",
1167 &rt1318
->pdata
.init_r0_r
);
1172 static void rt1318_calibration_sequence(struct rt1318_priv
*rt1318
)
1174 regmap_write(rt1318
->regmap
, RT1318_CLK1
, 0x22);
1175 regmap_write(rt1318
->regmap
, RT1318_PLL1_N_7_0
, 0x06);
1176 regmap_write(rt1318
->regmap
, RT1318_STP_TEMP_L
, 0xCC);
1177 regmap_write(rt1318
->regmap
, RT1318_STP_SEL_L
, 0x40);
1178 regmap_write(rt1318
->regmap
, RT1318_STP_SEL_R
, 0x40);
1179 regmap_write(rt1318
->regmap
, RT1318_SINE_GEN0
, 0x20);
1180 regmap_write(rt1318
->regmap
, RT1318_SPK_VOL_TH
, 0x00);
1181 regmap_write(rt1318
->regmap
, RT1318_FEEDBACK_PATH
, 0x0B);
1182 regmap_write(rt1318
->regmap
, RT1318_TCON
, 0x1C);
1183 regmap_write(rt1318
->regmap
, RT1318_TCON_RELATE
, 0x58);
1184 regmap_write(rt1318
->regmap
, RT1318_TCON_RELATE
, 0x78);
1185 regmap_write(rt1318
->regmap
, RT1318_STP_R0_EN_L
, 0xC2);
1188 static void rt1318_r0_calculate(struct rt1318_priv
*rt1318
)
1190 unsigned int r0_l
, r0_l_byte0
, r0_l_byte1
, r0_l_byte2
, r0_l_byte3
;
1191 unsigned int r0_r
, r0_r_byte0
, r0_r_byte1
, r0_r_byte2
, r0_r_byte3
;
1192 unsigned int r0_l_integer
, r0_l_factor
, r0_r_integer
, r0_r_factor
;
1193 unsigned int format
= 16777216; /* 2^24 */
1195 regmap_read(rt1318
->regmap
, RT1318_R0_L_24
, &r0_l_byte0
);
1196 regmap_read(rt1318
->regmap
, RT1318_R0_L_23_16
, &r0_l_byte1
);
1197 regmap_read(rt1318
->regmap
, RT1318_R0_L_15_8
, &r0_l_byte2
);
1198 regmap_read(rt1318
->regmap
, RT1318_R0_L_7_0
, &r0_l_byte3
);
1199 r0_l
= r0_l_byte0
<< 24 | r0_l_byte1
<< 16 | r0_l_byte2
<< 8 | r0_l_byte3
;
1200 r0_l_integer
= format
/ r0_l
;
1201 r0_l_factor
= (format
* 10) / r0_l
- r0_l_integer
* 10;
1203 regmap_read(rt1318
->regmap
, RT1318_R0_R_24
, &r0_r_byte0
);
1204 regmap_read(rt1318
->regmap
, RT1318_R0_R_23_16
, &r0_r_byte1
);
1205 regmap_read(rt1318
->regmap
, RT1318_R0_R_15_8
, &r0_r_byte2
);
1206 regmap_read(rt1318
->regmap
, RT1318_R0_R_7_0
, &r0_r_byte3
);
1207 r0_r
= r0_r_byte0
<< 24 | r0_r_byte1
<< 16 | r0_r_byte2
<< 8 | r0_r_byte3
;
1208 r0_r_integer
= format
/ r0_r
;
1209 r0_r_factor
= (format
* 10) / r0_r
- r0_r_integer
* 10;
1211 dev_dbg(rt1318
->component
->dev
, "r0_l_ch:%d.%d ohm\n", r0_l_integer
, r0_l_factor
);
1212 dev_dbg(rt1318
->component
->dev
, "r0_r_ch:%d.%d ohm\n", r0_r_integer
, r0_r_factor
);
1215 static void rt1318_r0_restore(struct rt1318_priv
*rt1318
)
1217 regmap_write(rt1318
->regmap
, RT1318_PRE_R0_L_24
,
1218 (rt1318
->pdata
.init_r0_l
>> 24) & 0xff);
1219 regmap_write(rt1318
->regmap
, RT1318_PRE_R0_L_23_16
,
1220 (rt1318
->pdata
.init_r0_l
>> 16) & 0xff);
1221 regmap_write(rt1318
->regmap
, RT1318_PRE_R0_L_15_8
,
1222 (rt1318
->pdata
.init_r0_l
>> 8) & 0xff);
1223 regmap_write(rt1318
->regmap
, RT1318_PRE_R0_L_7_0
,
1224 (rt1318
->pdata
.init_r0_l
>> 0) & 0xff);
1225 regmap_write(rt1318
->regmap
, RT1318_PRE_R0_R_24
,
1226 (rt1318
->pdata
.init_r0_r
>> 24) & 0xff);
1227 regmap_write(rt1318
->regmap
, RT1318_PRE_R0_R_23_16
,
1228 (rt1318
->pdata
.init_r0_r
>> 16) & 0xff);
1229 regmap_write(rt1318
->regmap
, RT1318_PRE_R0_R_15_8
,
1230 (rt1318
->pdata
.init_r0_r
>> 8) & 0xff);
1231 regmap_write(rt1318
->regmap
, RT1318_PRE_R0_R_7_0
,
1232 (rt1318
->pdata
.init_r0_r
>> 0) & 0xff);
1233 regmap_write(rt1318
->regmap
, RT1318_STP_SEL_L
, 0x80);
1234 regmap_write(rt1318
->regmap
, RT1318_STP_SEL_R
, 0x80);
1235 regmap_write(rt1318
->regmap
, RT1318_R0_CMP_L_FLAG
, 0xc0);
1236 regmap_write(rt1318
->regmap
, RT1318_R0_CMP_R_FLAG
, 0xc0);
1237 regmap_write(rt1318
->regmap
, RT1318_STP_R0_EN_L
, 0xc0);
1238 regmap_write(rt1318
->regmap
, RT1318_STP_R0_EN_R
, 0xc0);
1239 regmap_write(rt1318
->regmap
, RT1318_STP_TEMP_L
, 0xcc);
1240 regmap_write(rt1318
->regmap
, RT1318_TCON
, 0x9c);
1243 static int rt1318_calibrate(struct rt1318_priv
*rt1318
)
1245 int chk_cnt
= 30, count
= 0;
1248 regmap_write(rt1318
->regmap
, RT1318_PWR_STA1
, 0x1);
1249 usleep_range(0, 10000);
1250 rt1318_calibration_sequence(rt1318
);
1252 while (count
< chk_cnt
) {
1254 regmap_read(rt1318
->regmap
, RT1318_R0_CMP_L_FLAG
, &val
);
1255 regmap_read(rt1318
->regmap
, RT1318_R0_CMP_R_FLAG
, &val2
);
1256 val
= (val
>> 1) & 0x1;
1257 val2
= (val2
>> 1) & 0x1;
1259 dev_dbg(rt1318
->component
->dev
, "Calibration done.\n");
1263 if (count
== chk_cnt
) {
1264 regmap_write(rt1318
->regmap
, RT1318_PWR_STA1
, 0x0);
1265 return RT1318_R0_CALIB_NOT_DONE
;
1268 regmap_write(rt1318
->regmap
, RT1318_PWR_STA1
, 0x0);
1269 regmap_read(rt1318
->regmap
, RT1318_R0_CMP_L_FLAG
, &val
);
1270 regmap_read(rt1318
->regmap
, RT1318_R0_CMP_R_FLAG
, &val2
);
1271 if ((val
& 0x1) & (val2
& 0x1))
1272 return RT1318_R0_IN_RANGE
;
1274 return RT1318_R0_OUT_OF_RANGE
;
1277 static void rt1318_calibration_work(struct work_struct
*work
)
1279 struct rt1318_priv
*rt1318
=
1280 container_of(work
, struct rt1318_priv
, cali_work
);
1283 if (rt1318
->pdata
.init_r0_l
&& rt1318
->pdata
.init_r0_r
)
1284 rt1318_r0_restore(rt1318
);
1286 ret
= rt1318_calibrate(rt1318
);
1287 if (ret
== RT1318_R0_IN_RANGE
)
1288 rt1318_r0_calculate(rt1318
);
1289 dev_dbg(rt1318
->component
->dev
, "Calibrate R0 result:%d\n", ret
);
1293 static int rt1318_i2c_probe(struct i2c_client
*i2c
)
1295 struct rt1318_platform_data
*pdata
= dev_get_platdata(&i2c
->dev
);
1296 struct rt1318_priv
*rt1318
;
1297 int ret
, val
, val2
, dev_id
;
1299 rt1318
= devm_kzalloc(&i2c
->dev
, sizeof(struct rt1318_priv
),
1304 i2c_set_clientdata(i2c
, rt1318
);
1307 rt1318
->pdata
= *pdata
;
1309 rt1318_parse_dt(rt1318
, &i2c
->dev
);
1311 rt1318
->regmap
= devm_regmap_init_i2c(i2c
, &rt1318_regmap
);
1312 if (IS_ERR(rt1318
->regmap
)) {
1313 ret
= PTR_ERR(rt1318
->regmap
);
1314 dev_err(&i2c
->dev
, "Failed to allocate register map: %d\n",
1319 regmap_read(rt1318
->regmap
, RT1318_DEV_ID1
, &val
);
1320 regmap_read(rt1318
->regmap
, RT1318_DEV_ID2
, &val2
);
1321 dev_id
= (val
<< 8) | val2
;
1322 if (dev_id
!= 0x6821) {
1324 "Device with ID register %#x is not rt1318\n",
1329 ret
= regmap_register_patch(rt1318
->regmap
, init_list
,
1330 ARRAY_SIZE(init_list
));
1332 dev_warn(&i2c
->dev
, "Failed to apply regmap patch: %d\n", ret
);
1334 INIT_WORK(&rt1318
->cali_work
, rt1318_calibration_work
);
1336 return devm_snd_soc_register_component(&i2c
->dev
,
1337 &soc_component_dev_rt1318
, rt1318_dai
, ARRAY_SIZE(rt1318_dai
));
1340 static struct i2c_driver rt1318_i2c_driver
= {
1343 .of_match_table
= of_match_ptr(rt1318_of_match
),
1344 .acpi_match_table
= ACPI_PTR(rt1318_acpi_match
),
1346 .probe
= rt1318_i2c_probe
,
1347 .id_table
= rt1318_i2c_id
,
1349 module_i2c_driver(rt1318_i2c_driver
);
1351 MODULE_DESCRIPTION("ASoC RT1318 driver");
1352 MODULE_AUTHOR("Jack Yu <jack.yu@realtek.com>");
1353 MODULE_LICENSE("GPL");