1 // SPDX-License-Identifier: GPL-2.0
3 * rt1011.c -- rt1011 ALSA SoC amplifier component driver
5 * Copyright(c) 2019 Realtek Semiconductor Corp.
7 * Author: Shuming Fan <shumingf@realtek.com>
11 #include <linux/module.h>
12 #include <linux/moduleparam.h>
13 #include <linux/init.h>
14 #include <linux/delay.h>
16 #include <linux/gpio.h>
17 #include <linux/i2c.h>
18 #include <linux/acpi.h>
19 #include <linux/regmap.h>
20 #include <linux/of_gpio.h>
21 #include <linux/platform_device.h>
22 #include <linux/firmware.h>
23 #include <sound/core.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/initval.h>
29 #include <sound/tlv.h>
34 static int rt1011_calibrate(struct rt1011_priv
*rt1011
,
35 unsigned char cali_flag
);
37 static const struct reg_sequence init_list
[] = {
39 { RT1011_POWER_9
, 0xa840 },
41 { RT1011_ADC_SET_5
, 0x0a20 },
42 { RT1011_DAC_SET_2
, 0xa032 },
44 { RT1011_SPK_PRO_DC_DET_1
, 0xb00c },
45 { RT1011_SPK_PRO_DC_DET_2
, 0xcccc },
47 { RT1011_A_TIMING_1
, 0x6054 },
49 { RT1011_POWER_7
, 0x3e55 },
50 { RT1011_POWER_8
, 0x0520 },
51 { RT1011_BOOST_CON_1
, 0xe188 },
52 { RT1011_POWER_4
, 0x16f2 },
54 { RT1011_CROSS_BQ_SET_1
, 0x0004 },
55 { RT1011_SIL_DET
, 0xc313 },
56 { RT1011_SINE_GEN_REG_1
, 0x0707 },
58 { RT1011_DC_CALIB_CLASSD_3
, 0xcb00 },
60 { RT1011_DAC_SET_1
, 0xe702 },
61 { RT1011_DAC_SET_3
, 0x2004 },
64 static const struct reg_default rt1011_reg
[] = {
681 static int rt1011_reg_init(struct snd_soc_component
*component
)
683 struct rt1011_priv
*rt1011
= snd_soc_component_get_drvdata(component
);
685 regmap_multi_reg_write(rt1011
->regmap
,
686 init_list
, ARRAY_SIZE(init_list
));
690 static bool rt1011_volatile_register(struct device
*dev
, unsigned int reg
)
697 case RT1011_VERSION_ID
:
698 case RT1011_VENDOR_ID
:
699 case RT1011_DEVICE_ID
:
701 case RT1011_DAC_SET_3
:
703 case RT1011_SPK_VOL_TEST_OUT
:
704 case RT1011_VBAT_VOL_DET_1
:
705 case RT1011_VBAT_TEST_OUT_1
:
706 case RT1011_VBAT_TEST_OUT_2
:
707 case RT1011_VBAT_PROTECTION
:
708 case RT1011_VBAT_DET
:
709 case RT1011_BOOST_CON_1
:
710 case RT1011_SHORT_CIRCUIT_DET_1
:
711 case RT1011_SPK_TEMP_PROTECT_3
:
712 case RT1011_SPK_TEMP_PROTECT_6
:
713 case RT1011_SPK_PRO_DC_DET_3
:
714 case RT1011_SPK_PRO_DC_DET_7
:
715 case RT1011_SPK_PRO_DC_DET_8
:
718 case RT1011_EXCUR_PROTECT_1
:
719 case RT1011_CROSS_BQ_SET_1
:
720 case RT1011_CROSS_BQ_SET_2
:
721 case RT1011_BQ_SET_0
:
722 case RT1011_BQ_SET_1
:
723 case RT1011_BQ_SET_2
:
724 case RT1011_TEST_PAD_STATUS
:
725 case RT1011_DC_CALIB_CLASSD_1
:
726 case RT1011_DC_CALIB_CLASSD_5
:
727 case RT1011_DC_CALIB_CLASSD_6
:
728 case RT1011_DC_CALIB_CLASSD_7
:
729 case RT1011_DC_CALIB_CLASSD_8
:
730 case RT1011_SINE_GEN_REG_2
:
731 case RT1011_STP_CALIB_RS_TEMP
:
732 case RT1011_SPK_RESISTANCE_1
:
733 case RT1011_SPK_RESISTANCE_2
:
734 case RT1011_SPK_THERMAL
:
735 case RT1011_ALC_BK_GAIN_O
:
736 case RT1011_ALC_BK_GAIN_O_PRE
:
737 case RT1011_SPK_DC_O_23_16
:
738 case RT1011_SPK_DC_O_15_0
:
739 case RT1011_INIT_RECIPROCAL_SYN_24_16
:
740 case RT1011_INIT_RECIPROCAL_SYN_15_0
:
741 case RT1011_SPK_EXCURSION_23_16
:
742 case RT1011_SPK_EXCURSION_15_0
:
743 case RT1011_SEP_MAIN_OUT_23_16
:
744 case RT1011_SEP_MAIN_OUT_15_0
:
745 case RT1011_ALC_DRC_HB_INTERNAL_5
:
746 case RT1011_ALC_DRC_HB_INTERNAL_6
:
747 case RT1011_ALC_DRC_HB_INTERNAL_7
:
748 case RT1011_ALC_DRC_BB_INTERNAL_5
:
749 case RT1011_ALC_DRC_BB_INTERNAL_6
:
750 case RT1011_ALC_DRC_BB_INTERNAL_7
:
751 case RT1011_ALC_DRC_POS_INTERNAL_5
:
752 case RT1011_ALC_DRC_POS_INTERNAL_6
:
753 case RT1011_ALC_DRC_POS_INTERNAL_7
:
754 case RT1011_ALC_DRC_POS_INTERNAL_8
:
755 case RT1011_ALC_DRC_POS_INTERNAL_9
:
756 case RT1011_ALC_DRC_POS_INTERNAL_10
:
757 case RT1011_ALC_DRC_POS_INTERNAL_11
:
759 case RT1011_EFUSE_CONTROL_1
:
760 case RT1011_EFUSE_CONTROL_2
:
761 case RT1011_EFUSE_MATCH_DONE
... RT1011_EFUSE_READ_R0_3_15_0
:
769 static bool rt1011_readable_register(struct device
*dev
, unsigned int reg
)
784 case RT1011_PRIV_INDEX
:
785 case RT1011_PRIV_DATA
:
786 case RT1011_CUSTOMER_ID
:
788 case RT1011_VERSION_ID
:
789 case RT1011_VENDOR_ID
:
790 case RT1011_DEVICE_ID
:
791 case RT1011_DUM_RW_0
:
793 case RT1011_DUM_RW_1
:
795 case RT1011_MAN_I2C_DEV
:
796 case RT1011_DAC_SET_1
:
797 case RT1011_DAC_SET_2
:
798 case RT1011_DAC_SET_3
:
800 case RT1011_ADC_SET_1
:
801 case RT1011_ADC_SET_2
:
802 case RT1011_ADC_SET_3
:
803 case RT1011_ADC_SET_4
:
804 case RT1011_ADC_SET_5
:
805 case RT1011_TDM_TOTAL_SET
:
806 case RT1011_TDM1_SET_TCON
:
807 case RT1011_TDM1_SET_1
:
808 case RT1011_TDM1_SET_2
:
809 case RT1011_TDM1_SET_3
:
810 case RT1011_TDM1_SET_4
:
811 case RT1011_TDM1_SET_5
:
812 case RT1011_TDM2_SET_1
:
813 case RT1011_TDM2_SET_2
:
814 case RT1011_TDM2_SET_3
:
815 case RT1011_TDM2_SET_4
:
816 case RT1011_TDM2_SET_5
:
820 case RT1011_ADRC_LIMIT
:
822 case RT1011_A_TIMING_1
:
823 case RT1011_A_TIMING_2
:
824 case RT1011_A_TEMP_SEN
:
825 case RT1011_SPK_VOL_DET_1
:
826 case RT1011_SPK_VOL_DET_2
:
827 case RT1011_SPK_VOL_TEST_OUT
:
828 case RT1011_VBAT_VOL_DET_1
:
829 case RT1011_VBAT_VOL_DET_2
:
830 case RT1011_VBAT_TEST_OUT_1
:
831 case RT1011_VBAT_TEST_OUT_2
:
832 case RT1011_VBAT_PROTECTION
:
833 case RT1011_VBAT_DET
:
843 case RT1011_CLASS_D_POS
:
844 case RT1011_BOOST_CON_1
:
845 case RT1011_BOOST_CON_2
:
846 case RT1011_ANALOG_CTRL
:
847 case RT1011_POWER_SEQ
:
848 case RT1011_SHORT_CIRCUIT_DET_1
:
849 case RT1011_SHORT_CIRCUIT_DET_2
:
850 case RT1011_SPK_TEMP_PROTECT_0
:
851 case RT1011_SPK_TEMP_PROTECT_1
:
852 case RT1011_SPK_TEMP_PROTECT_2
:
853 case RT1011_SPK_TEMP_PROTECT_3
:
854 case RT1011_SPK_TEMP_PROTECT_4
:
855 case RT1011_SPK_TEMP_PROTECT_5
:
856 case RT1011_SPK_TEMP_PROTECT_6
:
857 case RT1011_SPK_TEMP_PROTECT_7
:
858 case RT1011_SPK_TEMP_PROTECT_8
:
859 case RT1011_SPK_TEMP_PROTECT_9
:
860 case RT1011_SPK_PRO_DC_DET_1
:
861 case RT1011_SPK_PRO_DC_DET_2
:
862 case RT1011_SPK_PRO_DC_DET_3
:
863 case RT1011_SPK_PRO_DC_DET_4
:
864 case RT1011_SPK_PRO_DC_DET_5
:
865 case RT1011_SPK_PRO_DC_DET_6
:
866 case RT1011_SPK_PRO_DC_DET_7
:
867 case RT1011_SPK_PRO_DC_DET_8
:
872 case RT1011_THER_FOLD_BACK_1
:
873 case RT1011_THER_FOLD_BACK_2
:
874 case RT1011_EXCUR_PROTECT_1
:
875 case RT1011_EXCUR_PROTECT_2
:
876 case RT1011_EXCUR_PROTECT_3
:
877 case RT1011_EXCUR_PROTECT_4
:
878 case RT1011_BAT_GAIN_1
:
879 case RT1011_BAT_GAIN_2
:
880 case RT1011_BAT_GAIN_3
:
881 case RT1011_BAT_GAIN_4
:
882 case RT1011_BAT_GAIN_5
:
883 case RT1011_BAT_GAIN_6
:
884 case RT1011_BAT_GAIN_7
:
885 case RT1011_BAT_GAIN_8
:
886 case RT1011_BAT_GAIN_9
:
887 case RT1011_BAT_GAIN_10
:
888 case RT1011_BAT_GAIN_11
:
889 case RT1011_BAT_RT_THMAX_1
:
890 case RT1011_BAT_RT_THMAX_2
:
891 case RT1011_BAT_RT_THMAX_3
:
892 case RT1011_BAT_RT_THMAX_4
:
893 case RT1011_BAT_RT_THMAX_5
:
894 case RT1011_BAT_RT_THMAX_6
:
895 case RT1011_BAT_RT_THMAX_7
:
896 case RT1011_BAT_RT_THMAX_8
:
897 case RT1011_BAT_RT_THMAX_9
:
898 case RT1011_BAT_RT_THMAX_10
:
899 case RT1011_BAT_RT_THMAX_11
:
900 case RT1011_BAT_RT_THMAX_12
:
901 case RT1011_SPREAD_SPECTURM
:
902 case RT1011_PRO_GAIN_MODE
:
903 case RT1011_RT_DRC_CROSS
:
904 case RT1011_RT_DRC_HB_1
:
905 case RT1011_RT_DRC_HB_2
:
906 case RT1011_RT_DRC_HB_3
:
907 case RT1011_RT_DRC_HB_4
:
908 case RT1011_RT_DRC_HB_5
:
909 case RT1011_RT_DRC_HB_6
:
910 case RT1011_RT_DRC_HB_7
:
911 case RT1011_RT_DRC_HB_8
:
912 case RT1011_RT_DRC_BB_1
:
913 case RT1011_RT_DRC_BB_2
:
914 case RT1011_RT_DRC_BB_3
:
915 case RT1011_RT_DRC_BB_4
:
916 case RT1011_RT_DRC_BB_5
:
917 case RT1011_RT_DRC_BB_6
:
918 case RT1011_RT_DRC_BB_7
:
919 case RT1011_RT_DRC_BB_8
:
920 case RT1011_RT_DRC_POS_1
:
921 case RT1011_RT_DRC_POS_2
:
922 case RT1011_RT_DRC_POS_3
:
923 case RT1011_RT_DRC_POS_4
:
924 case RT1011_RT_DRC_POS_5
:
925 case RT1011_RT_DRC_POS_6
:
926 case RT1011_RT_DRC_POS_7
:
927 case RT1011_RT_DRC_POS_8
:
928 case RT1011_CROSS_BQ_SET_1
:
929 case RT1011_CROSS_BQ_SET_2
:
930 case RT1011_BQ_SET_0
:
931 case RT1011_BQ_SET_1
:
932 case RT1011_BQ_SET_2
:
933 case RT1011_BQ_PRE_GAIN_28_16
:
934 case RT1011_BQ_PRE_GAIN_15_0
:
935 case RT1011_BQ_POST_GAIN_28_16
:
936 case RT1011_BQ_POST_GAIN_15_0
:
937 case RT1011_BQ_H0_28_16
... RT1011_BQ_A2_15_0
:
938 case RT1011_BQ_1_H0_28_16
... RT1011_BQ_1_A2_15_0
:
939 case RT1011_BQ_2_H0_28_16
... RT1011_BQ_2_A2_15_0
:
940 case RT1011_BQ_3_H0_28_16
... RT1011_BQ_3_A2_15_0
:
941 case RT1011_BQ_4_H0_28_16
... RT1011_BQ_4_A2_15_0
:
942 case RT1011_BQ_5_H0_28_16
... RT1011_BQ_5_A2_15_0
:
943 case RT1011_BQ_6_H0_28_16
... RT1011_BQ_6_A2_15_0
:
944 case RT1011_BQ_7_H0_28_16
... RT1011_BQ_7_A2_15_0
:
945 case RT1011_BQ_8_H0_28_16
... RT1011_BQ_8_A2_15_0
:
946 case RT1011_BQ_9_H0_28_16
... RT1011_BQ_9_A2_15_0
:
947 case RT1011_BQ_10_H0_28_16
... RT1011_BQ_10_A2_15_0
:
948 case RT1011_TEST_PAD_STATUS
... RT1011_PLL_INTERNAL_SET
:
949 case RT1011_TEST_OUT_1
... RT1011_TEST_OUT_3
:
950 case RT1011_DC_CALIB_CLASSD_1
... RT1011_DC_CALIB_CLASSD_10
:
951 case RT1011_CLASSD_INTERNAL_SET_1
... RT1011_VREF_LV_1
:
952 case RT1011_SMART_BOOST_TIMING_1
... RT1011_SMART_BOOST_TIMING_36
:
953 case RT1011_SINE_GEN_REG_1
... RT1011_SINE_GEN_REG_3
:
954 case RT1011_STP_INITIAL_RS_TEMP
... RT1011_SPK_THERMAL
:
955 case RT1011_STP_OTP_TH
... RT1011_INIT_RECIPROCAL_SYN_15_0
:
956 case RT1011_STP_BQ_1_A1_L_28_16
... RT1011_STP_BQ_1_H0_R_15_0
:
957 case RT1011_STP_BQ_2_A1_L_28_16
... RT1011_SEP_RE_REG_15_0
:
958 case RT1011_DRC_CF_PARAMS_1
... RT1011_DRC_CF_PARAMS_12
:
959 case RT1011_ALC_DRC_HB_INTERNAL_1
... RT1011_ALC_DRC_HB_INTERNAL_7
:
960 case RT1011_ALC_DRC_BB_INTERNAL_1
... RT1011_ALC_DRC_BB_INTERNAL_7
:
961 case RT1011_ALC_DRC_POS_INTERNAL_1
... RT1011_ALC_DRC_POS_INTERNAL_8
:
962 case RT1011_ALC_DRC_POS_INTERNAL_9
... RT1011_BQ_1_PARAMS_CHECK_5
:
963 case RT1011_BQ_2_PARAMS_CHECK_1
... RT1011_BQ_2_PARAMS_CHECK_5
:
964 case RT1011_BQ_3_PARAMS_CHECK_1
... RT1011_BQ_3_PARAMS_CHECK_5
:
965 case RT1011_BQ_4_PARAMS_CHECK_1
... RT1011_BQ_4_PARAMS_CHECK_5
:
966 case RT1011_BQ_5_PARAMS_CHECK_1
... RT1011_BQ_5_PARAMS_CHECK_5
:
967 case RT1011_BQ_6_PARAMS_CHECK_1
... RT1011_BQ_6_PARAMS_CHECK_5
:
968 case RT1011_BQ_7_PARAMS_CHECK_1
... RT1011_BQ_7_PARAMS_CHECK_5
:
969 case RT1011_BQ_8_PARAMS_CHECK_1
... RT1011_BQ_8_PARAMS_CHECK_5
:
970 case RT1011_BQ_9_PARAMS_CHECK_1
... RT1011_BQ_9_PARAMS_CHECK_5
:
971 case RT1011_BQ_10_PARAMS_CHECK_1
... RT1011_BQ_10_PARAMS_CHECK_5
:
972 case RT1011_IRQ_1
... RT1011_PART_NUMBER_EFUSE
:
973 case RT1011_EFUSE_CONTROL_1
... RT1011_EFUSE_READ_R0_3_15_0
:
980 static const char * const rt1011_din_source_select
[] = {
983 "Left + Right average",
986 static SOC_ENUM_SINGLE_DECL(rt1011_din_source_enum
, RT1011_CROSS_BQ_SET_1
, 5,
987 rt1011_din_source_select
);
989 static const char * const rt1011_tdm_data_out_select
[] = {
990 "TDM_O_LR", "BQ1", "DVOL", "BQ10", "ALC", "DMIX", "ADC_SRC_LR",
991 "ADC_O_LR", "ADC_MONO", "RSPK_BPF_LR", "DMIX_ADD", "ENVELOPE_FS",
992 "SEP_O_GAIN", "ALC_BK_GAIN", "STP_V_C", "DMIX_ABST"
995 static const char * const rt1011_tdm_l_ch_data_select
[] = {
996 "Slot0", "Slot1", "Slot2", "Slot3", "Slot4", "Slot5", "Slot6", "Slot7"
998 static SOC_ENUM_SINGLE_DECL(rt1011_tdm1_l_dac1_enum
, RT1011_TDM1_SET_4
, 12,
999 rt1011_tdm_l_ch_data_select
);
1000 static SOC_ENUM_SINGLE_DECL(rt1011_tdm2_l_dac1_enum
, RT1011_TDM2_SET_4
, 12,
1001 rt1011_tdm_l_ch_data_select
);
1003 static SOC_ENUM_SINGLE_DECL(rt1011_tdm1_adc1_dat_enum
,
1004 RT1011_ADCDAT_OUT_SOURCE
, 0, rt1011_tdm_data_out_select
);
1005 static SOC_ENUM_SINGLE_DECL(rt1011_tdm1_adc1_loc_enum
, RT1011_TDM1_SET_2
, 0,
1006 rt1011_tdm_l_ch_data_select
);
1008 static const char * const rt1011_adc_data_mode_select
[] = {
1011 static SOC_ENUM_SINGLE_DECL(rt1011_adc_dout_mode_enum
, RT1011_TDM1_SET_1
, 12,
1012 rt1011_adc_data_mode_select
);
1014 static const char * const rt1011_tdm_adc_data_len_control
[] = {
1015 "1CH", "2CH", "3CH", "4CH", "5CH", "6CH", "7CH", "8CH"
1017 static SOC_ENUM_SINGLE_DECL(rt1011_tdm1_dout_len_enum
, RT1011_TDM1_SET_2
, 13,
1018 rt1011_tdm_adc_data_len_control
);
1019 static SOC_ENUM_SINGLE_DECL(rt1011_tdm2_dout_len_enum
, RT1011_TDM2_SET_2
, 13,
1020 rt1011_tdm_adc_data_len_control
);
1022 static const char * const rt1011_tdm_adc_swap_select
[] = {
1023 "L/R", "R/L", "L/L", "R/R"
1026 static SOC_ENUM_SINGLE_DECL(rt1011_tdm_adc1_1_enum
, RT1011_TDM1_SET_3
, 6,
1027 rt1011_tdm_adc_swap_select
);
1028 static SOC_ENUM_SINGLE_DECL(rt1011_tdm_adc2_1_enum
, RT1011_TDM1_SET_3
, 4,
1029 rt1011_tdm_adc_swap_select
);
1031 static void rt1011_reset(struct regmap
*regmap
)
1033 regmap_write(regmap
, RT1011_RESET
, 0);
1036 static int rt1011_recv_spk_mode_get(struct snd_kcontrol
*kcontrol
,
1037 struct snd_ctl_elem_value
*ucontrol
)
1039 struct snd_soc_component
*component
=
1040 snd_soc_kcontrol_component(kcontrol
);
1041 struct rt1011_priv
*rt1011
=
1042 snd_soc_component_get_drvdata(component
);
1044 ucontrol
->value
.integer
.value
[0] = rt1011
->recv_spk_mode
;
1049 static int rt1011_recv_spk_mode_put(struct snd_kcontrol
*kcontrol
,
1050 struct snd_ctl_elem_value
*ucontrol
)
1052 struct snd_soc_component
*component
=
1053 snd_soc_kcontrol_component(kcontrol
);
1054 struct rt1011_priv
*rt1011
=
1055 snd_soc_component_get_drvdata(component
);
1057 if (ucontrol
->value
.integer
.value
[0] == rt1011
->recv_spk_mode
)
1060 if (snd_soc_component_get_bias_level(component
) == SND_SOC_BIAS_OFF
) {
1061 rt1011
->recv_spk_mode
= ucontrol
->value
.integer
.value
[0];
1063 if (rt1011
->recv_spk_mode
) {
1065 /* 1: recevier mode on */
1066 snd_soc_component_update_bits(component
,
1067 RT1011_CLASSD_INTERNAL_SET_3
,
1068 RT1011_REG_GAIN_CLASSD_RI_SPK_MASK
,
1069 RT1011_REG_GAIN_CLASSD_RI_410K
);
1070 snd_soc_component_update_bits(component
,
1071 RT1011_CLASSD_INTERNAL_SET_1
,
1072 RT1011_RECV_MODE_SPK_MASK
,
1075 /* 0: speaker mode on */
1076 snd_soc_component_update_bits(component
,
1077 RT1011_CLASSD_INTERNAL_SET_3
,
1078 RT1011_REG_GAIN_CLASSD_RI_SPK_MASK
,
1079 RT1011_REG_GAIN_CLASSD_RI_72P5K
);
1080 snd_soc_component_update_bits(component
,
1081 RT1011_CLASSD_INTERNAL_SET_1
,
1082 RT1011_RECV_MODE_SPK_MASK
,
1090 static bool rt1011_validate_bq_drc_coeff(unsigned short reg
)
1092 if ((reg
== RT1011_DAC_SET_1
) |
1093 (reg
>= RT1011_ADC_SET
&& reg
<= RT1011_ADC_SET_1
) |
1094 (reg
== RT1011_ADC_SET_4
) | (reg
== RT1011_ADC_SET_5
) |
1095 (reg
== RT1011_MIXER_1
) |
1096 (reg
== RT1011_A_TIMING_1
) | (reg
>= RT1011_POWER_7
&&
1097 reg
<= RT1011_POWER_8
) |
1098 (reg
== RT1011_CLASS_D_POS
) | (reg
== RT1011_ANALOG_CTRL
) |
1099 (reg
>= RT1011_SPK_TEMP_PROTECT_0
&&
1100 reg
<= RT1011_SPK_TEMP_PROTECT_6
) |
1101 (reg
>= RT1011_SPK_PRO_DC_DET_5
&& reg
<= RT1011_BAT_GAIN_1
) |
1102 (reg
>= RT1011_RT_DRC_CROSS
&& reg
<= RT1011_RT_DRC_POS_8
) |
1103 (reg
>= RT1011_CROSS_BQ_SET_1
&& reg
<= RT1011_BQ_10_A2_15_0
) |
1104 (reg
>= RT1011_SMART_BOOST_TIMING_1
&&
1105 reg
<= RT1011_SMART_BOOST_TIMING_36
) |
1106 (reg
== RT1011_SINE_GEN_REG_1
) |
1107 (reg
>= RT1011_STP_ALPHA_RECIPROCAL_MSB
&&
1108 reg
<= RT1011_BQ_6_PARAMS_CHECK_5
) |
1109 (reg
>= RT1011_BQ_7_PARAMS_CHECK_1
&&
1110 reg
<= RT1011_BQ_10_PARAMS_CHECK_5
))
1116 static int rt1011_bq_drc_coeff_get(struct snd_kcontrol
*kcontrol
,
1117 struct snd_ctl_elem_value
*ucontrol
)
1119 struct snd_soc_component
*component
=
1120 snd_soc_kcontrol_component(kcontrol
);
1121 struct rt1011_priv
*rt1011
=
1122 snd_soc_component_get_drvdata(component
);
1123 struct rt1011_bq_drc_params
*bq_drc_info
;
1124 struct rt1011_bq_drc_params
*params
=
1125 (struct rt1011_bq_drc_params
*)ucontrol
->value
.integer
.value
;
1126 unsigned int i
, mode_idx
= 0;
1128 if (strstr(ucontrol
->id
.name
, "AdvanceMode Initial Set"))
1129 mode_idx
= RT1011_ADVMODE_INITIAL_SET
;
1130 else if (strstr(ucontrol
->id
.name
, "AdvanceMode SEP BQ Coeff"))
1131 mode_idx
= RT1011_ADVMODE_SEP_BQ_COEFF
;
1132 else if (strstr(ucontrol
->id
.name
, "AdvanceMode EQ BQ Coeff"))
1133 mode_idx
= RT1011_ADVMODE_EQ_BQ_COEFF
;
1134 else if (strstr(ucontrol
->id
.name
, "AdvanceMode BQ UI Coeff"))
1135 mode_idx
= RT1011_ADVMODE_BQ_UI_COEFF
;
1136 else if (strstr(ucontrol
->id
.name
, "AdvanceMode SmartBoost Coeff"))
1137 mode_idx
= RT1011_ADVMODE_SMARTBOOST_COEFF
;
1141 pr_info("%s, id.name=%s, mode_idx=%d\n", __func__
,
1142 ucontrol
->id
.name
, mode_idx
);
1143 bq_drc_info
= rt1011
->bq_drc_params
[mode_idx
];
1145 for (i
= 0; i
< RT1011_BQ_DRC_NUM
; i
++) {
1146 params
[i
].reg
= bq_drc_info
[i
].reg
;
1147 params
[i
].val
= bq_drc_info
[i
].val
;
1153 static int rt1011_bq_drc_coeff_put(struct snd_kcontrol
*kcontrol
,
1154 struct snd_ctl_elem_value
*ucontrol
)
1156 struct snd_soc_component
*component
=
1157 snd_soc_kcontrol_component(kcontrol
);
1158 struct rt1011_priv
*rt1011
=
1159 snd_soc_component_get_drvdata(component
);
1160 struct rt1011_bq_drc_params
*bq_drc_info
;
1161 struct rt1011_bq_drc_params
*params
=
1162 (struct rt1011_bq_drc_params
*)ucontrol
->value
.integer
.value
;
1163 unsigned int i
, mode_idx
= 0;
1165 if (strstr(ucontrol
->id
.name
, "AdvanceMode Initial Set"))
1166 mode_idx
= RT1011_ADVMODE_INITIAL_SET
;
1167 else if (strstr(ucontrol
->id
.name
, "AdvanceMode SEP BQ Coeff"))
1168 mode_idx
= RT1011_ADVMODE_SEP_BQ_COEFF
;
1169 else if (strstr(ucontrol
->id
.name
, "AdvanceMode EQ BQ Coeff"))
1170 mode_idx
= RT1011_ADVMODE_EQ_BQ_COEFF
;
1171 else if (strstr(ucontrol
->id
.name
, "AdvanceMode BQ UI Coeff"))
1172 mode_idx
= RT1011_ADVMODE_BQ_UI_COEFF
;
1173 else if (strstr(ucontrol
->id
.name
, "AdvanceMode SmartBoost Coeff"))
1174 mode_idx
= RT1011_ADVMODE_SMARTBOOST_COEFF
;
1178 bq_drc_info
= rt1011
->bq_drc_params
[mode_idx
];
1179 memset(bq_drc_info
, 0,
1180 sizeof(struct rt1011_bq_drc_params
) * RT1011_BQ_DRC_NUM
);
1182 pr_info("%s, id.name=%s, mode_idx=%d\n", __func__
,
1183 ucontrol
->id
.name
, mode_idx
);
1184 for (i
= 0; i
< RT1011_BQ_DRC_NUM
; i
++) {
1185 bq_drc_info
[i
].reg
= params
[i
].reg
;
1186 bq_drc_info
[i
].val
= params
[i
].val
;
1189 for (i
= 0; i
< RT1011_BQ_DRC_NUM
; i
++) {
1190 if (bq_drc_info
[i
].reg
== 0)
1192 else if (rt1011_validate_bq_drc_coeff(bq_drc_info
[i
].reg
)) {
1193 snd_soc_component_write(component
, bq_drc_info
[i
].reg
,
1194 bq_drc_info
[i
].val
);
1201 static int rt1011_bq_drc_info(struct snd_kcontrol
*kcontrol
,
1202 struct snd_ctl_elem_info
*uinfo
)
1204 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
1206 uinfo
->value
.integer
.max
= 0x17ffffff;
1211 #define RT1011_BQ_DRC(xname) \
1212 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
1213 .info = rt1011_bq_drc_info, \
1214 .get = rt1011_bq_drc_coeff_get, \
1215 .put = rt1011_bq_drc_coeff_put \
1218 static int rt1011_r0_cali_get(struct snd_kcontrol
*kcontrol
,
1219 struct snd_ctl_elem_value
*ucontrol
)
1221 struct snd_soc_component
*component
= snd_kcontrol_chip(kcontrol
);
1222 struct rt1011_priv
*rt1011
= snd_soc_component_get_drvdata(component
);
1224 ucontrol
->value
.integer
.value
[0] = rt1011
->cali_done
;
1229 static int rt1011_r0_cali_put(struct snd_kcontrol
*kcontrol
,
1230 struct snd_ctl_elem_value
*ucontrol
)
1232 struct snd_soc_component
*component
= snd_kcontrol_chip(kcontrol
);
1233 struct rt1011_priv
*rt1011
= snd_soc_component_get_drvdata(component
);
1235 rt1011
->cali_done
= 0;
1236 if (snd_soc_component_get_bias_level(component
) == SND_SOC_BIAS_OFF
&&
1237 ucontrol
->value
.integer
.value
[0])
1238 rt1011_calibrate(rt1011
, 1);
1243 static int rt1011_r0_load(struct rt1011_priv
*rt1011
)
1245 if (!rt1011
->r0_reg
)
1248 /* write R0 to register */
1249 regmap_write(rt1011
->regmap
, RT1011_INIT_RECIPROCAL_REG_24_16
,
1250 ((rt1011
->r0_reg
>>16) & 0x1ff));
1251 regmap_write(rt1011
->regmap
, RT1011_INIT_RECIPROCAL_REG_15_0
,
1252 (rt1011
->r0_reg
& 0xffff));
1253 regmap_write(rt1011
->regmap
, RT1011_SPK_TEMP_PROTECT_4
, 0x4080);
1258 static int rt1011_r0_load_mode_get(struct snd_kcontrol
*kcontrol
,
1259 struct snd_ctl_elem_value
*ucontrol
)
1261 struct snd_soc_component
*component
= snd_kcontrol_chip(kcontrol
);
1262 struct rt1011_priv
*rt1011
= snd_soc_component_get_drvdata(component
);
1264 ucontrol
->value
.integer
.value
[0] = rt1011
->r0_reg
;
1269 static int rt1011_r0_load_mode_put(struct snd_kcontrol
*kcontrol
,
1270 struct snd_ctl_elem_value
*ucontrol
)
1272 struct snd_soc_component
*component
= snd_kcontrol_chip(kcontrol
);
1273 struct rt1011_priv
*rt1011
= snd_soc_component_get_drvdata(component
);
1275 unsigned int r0_integer
, r0_factor
, format
;
1277 if (ucontrol
->value
.integer
.value
[0] == rt1011
->r0_reg
)
1280 if (ucontrol
->value
.integer
.value
[0] == 0)
1283 dev
= regmap_get_device(rt1011
->regmap
);
1284 if (snd_soc_component_get_bias_level(component
) == SND_SOC_BIAS_OFF
) {
1285 rt1011
->r0_reg
= ucontrol
->value
.integer
.value
[0];
1287 format
= 2147483648U; /* 2^24 * 128 */
1288 r0_integer
= format
/ rt1011
->r0_reg
/ 128;
1289 r0_factor
= ((format
/ rt1011
->r0_reg
* 100) / 128)
1290 - (r0_integer
* 100);
1291 dev_info(dev
, "New r0 resistance about %d.%02d ohm, reg=0x%X\n",
1292 r0_integer
, r0_factor
, rt1011
->r0_reg
);
1295 rt1011_r0_load(rt1011
);
1301 static int rt1011_r0_load_info(struct snd_kcontrol
*kcontrol
,
1302 struct snd_ctl_elem_info
*uinfo
)
1304 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
1306 uinfo
->value
.integer
.max
= 0x1ffffff;
1311 #define RT1011_R0_LOAD(xname) \
1312 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
1313 .info = rt1011_r0_load_info, \
1314 .get = rt1011_r0_load_mode_get, \
1315 .put = rt1011_r0_load_mode_put \
1318 static const struct snd_kcontrol_new rt1011_snd_controls
[] = {
1319 /* I2S Data In Selection */
1320 SOC_ENUM("DIN Source", rt1011_din_source_enum
),
1322 /* TDM Data In Selection */
1323 SOC_ENUM("TDM1 DIN Source", rt1011_tdm1_l_dac1_enum
),
1324 SOC_ENUM("TDM2 DIN Source", rt1011_tdm2_l_dac1_enum
),
1326 /* TDM1 Data Out Selection */
1327 SOC_ENUM("TDM1 DOUT Source", rt1011_tdm1_adc1_dat_enum
),
1328 SOC_ENUM("TDM1 DOUT Location", rt1011_tdm1_adc1_loc_enum
),
1329 SOC_ENUM("TDM1 ADC1DAT Swap Select", rt1011_tdm_adc1_1_enum
),
1330 SOC_ENUM("TDM1 ADC2DAT Swap Select", rt1011_tdm_adc2_1_enum
),
1333 SOC_ENUM("I2S ADC DOUT Mode", rt1011_adc_dout_mode_enum
),
1334 SOC_ENUM("TDM1 DOUT Length", rt1011_tdm1_dout_len_enum
),
1335 SOC_ENUM("TDM2 DOUT Length", rt1011_tdm2_dout_len_enum
),
1337 /* Speaker/Receiver Mode */
1338 SOC_SINGLE_EXT("RECV SPK Mode", SND_SOC_NOPM
, 0, 1, 0,
1339 rt1011_recv_spk_mode_get
, rt1011_recv_spk_mode_put
),
1341 /* BiQuad/DRC/SmartBoost Settings */
1342 RT1011_BQ_DRC("AdvanceMode Initial Set"),
1343 RT1011_BQ_DRC("AdvanceMode SEP BQ Coeff"),
1344 RT1011_BQ_DRC("AdvanceMode EQ BQ Coeff"),
1345 RT1011_BQ_DRC("AdvanceMode BQ UI Coeff"),
1346 RT1011_BQ_DRC("AdvanceMode SmartBoost Coeff"),
1349 SOC_SINGLE_EXT("R0 Calibration", SND_SOC_NOPM
, 0, 1, 0,
1350 rt1011_r0_cali_get
, rt1011_r0_cali_put
),
1351 RT1011_R0_LOAD("R0 Load Mode"),
1353 /* R0 temperature */
1354 SOC_SINGLE("R0 Temperature", RT1011_STP_INITIAL_RESISTANCE_TEMP
,
1358 static int rt1011_is_sys_clk_from_pll(struct snd_soc_dapm_widget
*source
,
1359 struct snd_soc_dapm_widget
*sink
)
1361 struct snd_soc_component
*component
=
1362 snd_soc_dapm_to_component(source
->dapm
);
1363 struct rt1011_priv
*rt1011
= snd_soc_component_get_drvdata(component
);
1365 if (rt1011
->sysclk_src
== RT1011_FS_SYS_PRE_S_PLL1
)
1371 static int rt1011_dac_event(struct snd_soc_dapm_widget
*w
,
1372 struct snd_kcontrol
*kcontrol
, int event
)
1374 struct snd_soc_component
*component
=
1375 snd_soc_dapm_to_component(w
->dapm
);
1378 case SND_SOC_DAPM_POST_PMU
:
1379 snd_soc_component_update_bits(component
,
1380 RT1011_SPK_TEMP_PROTECT_0
,
1381 RT1011_STP_EN_MASK
| RT1011_STP_RS_CLB_EN_MASK
,
1382 RT1011_STP_EN
| RT1011_STP_RS_CLB_EN
);
1383 snd_soc_component_update_bits(component
, RT1011_POWER_9
,
1384 RT1011_POW_MNL_SDB_MASK
, RT1011_POW_MNL_SDB
);
1386 snd_soc_component_update_bits(component
,
1387 RT1011_CLASSD_INTERNAL_SET_1
,
1388 RT1011_DRIVER_READY_SPK
, RT1011_DRIVER_READY_SPK
);
1390 case SND_SOC_DAPM_PRE_PMD
:
1391 snd_soc_component_update_bits(component
, RT1011_POWER_9
,
1392 RT1011_POW_MNL_SDB_MASK
, 0);
1393 snd_soc_component_update_bits(component
,
1394 RT1011_SPK_TEMP_PROTECT_0
,
1395 RT1011_STP_EN_MASK
| RT1011_STP_RS_CLB_EN_MASK
, 0);
1397 snd_soc_component_update_bits(component
,
1398 RT1011_CLASSD_INTERNAL_SET_1
,
1399 RT1011_DRIVER_READY_SPK
, 0);
1410 static const struct snd_soc_dapm_widget rt1011_dapm_widgets
[] = {
1411 SND_SOC_DAPM_SUPPLY("LDO2", RT1011_POWER_1
,
1412 RT1011_POW_LDO2_BIT
, 0, NULL
, 0),
1413 SND_SOC_DAPM_SUPPLY("ISENSE SPK", RT1011_POWER_1
,
1414 RT1011_POW_ISENSE_SPK_BIT
, 0, NULL
, 0),
1415 SND_SOC_DAPM_SUPPLY("VSENSE SPK", RT1011_POWER_1
,
1416 RT1011_POW_VSENSE_SPK_BIT
, 0, NULL
, 0),
1418 SND_SOC_DAPM_SUPPLY("PLL", RT1011_POWER_2
,
1419 RT1011_PLLEN_BIT
, 0, NULL
, 0),
1420 SND_SOC_DAPM_SUPPLY("BG", RT1011_POWER_2
,
1421 RT1011_POW_BG_BIT
, 0, NULL
, 0),
1422 SND_SOC_DAPM_SUPPLY("BG MBIAS", RT1011_POWER_2
,
1423 RT1011_POW_BG_MBIAS_LV_BIT
, 0, NULL
, 0),
1425 SND_SOC_DAPM_SUPPLY("DET VBAT", RT1011_POWER_3
,
1426 RT1011_POW_DET_VBAT_BIT
, 0, NULL
, 0),
1427 SND_SOC_DAPM_SUPPLY("MBIAS", RT1011_POWER_3
,
1428 RT1011_POW_MBIAS_LV_BIT
, 0, NULL
, 0),
1429 SND_SOC_DAPM_SUPPLY("ADC I", RT1011_POWER_3
,
1430 RT1011_POW_ADC_I_BIT
, 0, NULL
, 0),
1431 SND_SOC_DAPM_SUPPLY("ADC V", RT1011_POWER_3
,
1432 RT1011_POW_ADC_V_BIT
, 0, NULL
, 0),
1433 SND_SOC_DAPM_SUPPLY("ADC T", RT1011_POWER_3
,
1434 RT1011_POW_ADC_T_BIT
, 0, NULL
, 0),
1435 SND_SOC_DAPM_SUPPLY("DITHER ADC T", RT1011_POWER_3
,
1436 RT1011_POWD_ADC_T_BIT
, 0, NULL
, 0),
1437 SND_SOC_DAPM_SUPPLY("MIX I", RT1011_POWER_3
,
1438 RT1011_POW_MIX_I_BIT
, 0, NULL
, 0),
1439 SND_SOC_DAPM_SUPPLY("MIX V", RT1011_POWER_3
,
1440 RT1011_POW_MIX_V_BIT
, 0, NULL
, 0),
1441 SND_SOC_DAPM_SUPPLY("SUM I", RT1011_POWER_3
,
1442 RT1011_POW_SUM_I_BIT
, 0, NULL
, 0),
1443 SND_SOC_DAPM_SUPPLY("SUM V", RT1011_POWER_3
,
1444 RT1011_POW_SUM_V_BIT
, 0, NULL
, 0),
1445 SND_SOC_DAPM_SUPPLY("MIX T", RT1011_POWER_3
,
1446 RT1011_POW_MIX_T_BIT
, 0, NULL
, 0),
1447 SND_SOC_DAPM_SUPPLY("VREF", RT1011_POWER_3
,
1448 RT1011_POW_VREF_LV_BIT
, 0, NULL
, 0),
1450 SND_SOC_DAPM_SUPPLY("BOOST SWR", RT1011_POWER_4
,
1451 RT1011_POW_EN_SWR_BIT
, 0, NULL
, 0),
1452 SND_SOC_DAPM_SUPPLY("BGOK SWR", RT1011_POWER_4
,
1453 RT1011_POW_EN_PASS_BGOK_SWR_BIT
, 0, NULL
, 0),
1454 SND_SOC_DAPM_SUPPLY("VPOK SWR", RT1011_POWER_4
,
1455 RT1011_POW_EN_PASS_VPOK_SWR_BIT
, 0, NULL
, 0),
1457 SND_SOC_DAPM_SUPPLY("TEMP REG", RT1011_A_TEMP_SEN
,
1458 RT1011_POW_TEMP_REG_BIT
, 0, NULL
, 0),
1460 /* Audio Interface */
1461 SND_SOC_DAPM_AIF_IN("AIF1RX", "AIF1 Playback", 0, SND_SOC_NOPM
, 0, 0),
1462 /* Digital Interface */
1463 SND_SOC_DAPM_SUPPLY("DAC Power", RT1011_POWER_1
,
1464 RT1011_POW_DAC_BIT
, 0, NULL
, 0),
1465 SND_SOC_DAPM_SUPPLY("CLK12M", RT1011_POWER_1
,
1466 RT1011_POW_CLK12M_BIT
, 0, NULL
, 0),
1467 SND_SOC_DAPM_DAC_E("DAC", NULL
, RT1011_DAC_SET_3
,
1468 RT1011_DA_MUTE_EN_SFT
, 1, rt1011_dac_event
,
1469 SND_SOC_DAPM_PRE_PMD
| SND_SOC_DAPM_POST_PMU
),
1472 SND_SOC_DAPM_OUTPUT("SPO"),
1475 static const struct snd_soc_dapm_route rt1011_dapm_routes
[] = {
1477 { "DAC", NULL
, "AIF1RX" },
1478 { "DAC", NULL
, "DAC Power" },
1479 { "DAC", NULL
, "LDO2" },
1480 { "DAC", NULL
, "ISENSE SPK" },
1481 { "DAC", NULL
, "VSENSE SPK" },
1482 { "DAC", NULL
, "CLK12M" },
1484 { "DAC", NULL
, "PLL", rt1011_is_sys_clk_from_pll
},
1485 { "DAC", NULL
, "BG" },
1486 { "DAC", NULL
, "BG MBIAS" },
1488 { "DAC", NULL
, "BOOST SWR" },
1489 { "DAC", NULL
, "BGOK SWR" },
1490 { "DAC", NULL
, "VPOK SWR" },
1492 { "DAC", NULL
, "DET VBAT" },
1493 { "DAC", NULL
, "MBIAS" },
1494 { "DAC", NULL
, "VREF" },
1495 { "DAC", NULL
, "ADC I" },
1496 { "DAC", NULL
, "ADC V" },
1497 { "DAC", NULL
, "ADC T" },
1498 { "DAC", NULL
, "DITHER ADC T" },
1499 { "DAC", NULL
, "MIX I" },
1500 { "DAC", NULL
, "MIX V" },
1501 { "DAC", NULL
, "SUM I" },
1502 { "DAC", NULL
, "SUM V" },
1503 { "DAC", NULL
, "MIX T" },
1505 { "DAC", NULL
, "TEMP REG" },
1507 { "SPO", NULL
, "DAC" },
1510 static int rt1011_get_clk_info(int sclk
, int rate
)
1513 static const int pd
[] = {1, 2, 3, 4, 6, 8, 12, 16};
1515 if (sclk
<= 0 || rate
<= 0)
1519 for (i
= 0; i
< ARRAY_SIZE(pd
); i
++)
1520 if (sclk
== rate
* pd
[i
])
1526 static int rt1011_hw_params(struct snd_pcm_substream
*substream
,
1527 struct snd_pcm_hw_params
*params
, struct snd_soc_dai
*dai
)
1529 struct snd_soc_component
*component
= dai
->component
;
1530 struct rt1011_priv
*rt1011
= snd_soc_component_get_drvdata(component
);
1531 unsigned int val_len
= 0, ch_len
= 0, val_clk
, mask_clk
;
1532 int pre_div
, bclk_ms
, frame_size
;
1534 rt1011
->lrck
= params_rate(params
);
1535 pre_div
= rt1011_get_clk_info(rt1011
->sysclk
, rt1011
->lrck
);
1537 dev_warn(component
->dev
, "Force using PLL ");
1538 snd_soc_dai_set_pll(dai
, 0, RT1011_PLL1_S_BCLK
,
1539 rt1011
->lrck
* 64, rt1011
->lrck
* 256);
1540 snd_soc_dai_set_sysclk(dai
, RT1011_FS_SYS_PRE_S_PLL1
,
1541 rt1011
->lrck
* 256, SND_SOC_CLOCK_IN
);
1544 frame_size
= snd_soc_params_to_frame_size(params
);
1545 if (frame_size
< 0) {
1546 dev_err(component
->dev
, "Unsupported frame size: %d\n",
1551 bclk_ms
= frame_size
> 32;
1552 rt1011
->bclk
= rt1011
->lrck
* (32 << bclk_ms
);
1554 dev_dbg(component
->dev
, "bclk_ms is %d and pre_div is %d for iis %d\n",
1555 bclk_ms
, pre_div
, dai
->id
);
1557 dev_dbg(component
->dev
, "lrck is %dHz and pre_div is %d for iis %d\n",
1558 rt1011
->lrck
, pre_div
, dai
->id
);
1560 switch (params_width(params
)) {
1562 val_len
|= RT1011_I2S_TX_DL_16B
;
1563 val_len
|= RT1011_I2S_RX_DL_16B
;
1564 ch_len
|= RT1011_I2S_CH_TX_LEN_16B
;
1565 ch_len
|= RT1011_I2S_CH_RX_LEN_16B
;
1568 val_len
|= RT1011_I2S_TX_DL_20B
;
1569 val_len
|= RT1011_I2S_RX_DL_20B
;
1570 ch_len
|= RT1011_I2S_CH_TX_LEN_20B
;
1571 ch_len
|= RT1011_I2S_CH_RX_LEN_20B
;
1574 val_len
|= RT1011_I2S_TX_DL_24B
;
1575 val_len
|= RT1011_I2S_RX_DL_24B
;
1576 ch_len
|= RT1011_I2S_CH_TX_LEN_24B
;
1577 ch_len
|= RT1011_I2S_CH_RX_LEN_24B
;
1580 val_len
|= RT1011_I2S_TX_DL_32B
;
1581 val_len
|= RT1011_I2S_RX_DL_32B
;
1582 ch_len
|= RT1011_I2S_CH_TX_LEN_32B
;
1583 ch_len
|= RT1011_I2S_CH_RX_LEN_32B
;
1586 val_len
|= RT1011_I2S_TX_DL_8B
;
1587 val_len
|= RT1011_I2S_RX_DL_8B
;
1588 ch_len
|= RT1011_I2S_CH_TX_LEN_8B
;
1589 ch_len
|= RT1011_I2S_CH_RX_LEN_8B
;
1597 mask_clk
= RT1011_FS_SYS_DIV_MASK
;
1598 val_clk
= pre_div
<< RT1011_FS_SYS_DIV_SFT
;
1599 snd_soc_component_update_bits(component
, RT1011_TDM_TOTAL_SET
,
1600 RT1011_I2S_TX_DL_MASK
| RT1011_I2S_RX_DL_MASK
,
1602 snd_soc_component_update_bits(component
, RT1011_TDM1_SET_1
,
1603 RT1011_I2S_CH_TX_LEN_MASK
|
1604 RT1011_I2S_CH_RX_LEN_MASK
,
1608 dev_err(component
->dev
, "Invalid dai->id: %d\n", dai
->id
);
1612 snd_soc_component_update_bits(component
,
1613 RT1011_CLK_2
, mask_clk
, val_clk
);
1618 static int rt1011_set_dai_fmt(struct snd_soc_dai
*dai
, unsigned int fmt
)
1620 struct snd_soc_component
*component
= dai
->component
;
1621 struct snd_soc_dapm_context
*dapm
=
1622 snd_soc_component_get_dapm(component
);
1623 unsigned int reg_val
= 0, reg_bclk_inv
= 0;
1626 snd_soc_dapm_mutex_lock(dapm
);
1627 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
1628 case SND_SOC_DAIFMT_CBS_CFS
:
1629 reg_val
|= RT1011_I2S_TDM_MS_S
;
1636 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
1637 case SND_SOC_DAIFMT_NB_NF
:
1639 case SND_SOC_DAIFMT_IB_NF
:
1640 reg_bclk_inv
|= RT1011_TDM_INV_BCLK
;
1647 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
1648 case SND_SOC_DAIFMT_I2S
:
1650 case SND_SOC_DAIFMT_LEFT_J
:
1651 reg_val
|= RT1011_I2S_TDM_DF_LEFT
;
1653 case SND_SOC_DAIFMT_DSP_A
:
1654 reg_val
|= RT1011_I2S_TDM_DF_PCM_A
;
1656 case SND_SOC_DAIFMT_DSP_B
:
1657 reg_val
|= RT1011_I2S_TDM_DF_PCM_B
;
1666 snd_soc_component_update_bits(component
, RT1011_TDM_TOTAL_SET
,
1667 RT1011_I2S_TDM_MS_MASK
| RT1011_I2S_TDM_DF_MASK
,
1669 snd_soc_component_update_bits(component
, RT1011_TDM1_SET_1
,
1670 RT1011_TDM_INV_BCLK_MASK
, reg_bclk_inv
);
1671 snd_soc_component_update_bits(component
, RT1011_TDM2_SET_1
,
1672 RT1011_TDM_INV_BCLK_MASK
, reg_bclk_inv
);
1675 dev_err(component
->dev
, "Invalid dai->id: %d\n", dai
->id
);
1680 snd_soc_dapm_mutex_unlock(dapm
);
1684 static int rt1011_set_component_sysclk(struct snd_soc_component
*component
,
1685 int clk_id
, int source
, unsigned int freq
, int dir
)
1687 struct rt1011_priv
*rt1011
= snd_soc_component_get_drvdata(component
);
1688 unsigned int reg_val
= 0;
1690 if (freq
== rt1011
->sysclk
&& clk_id
== rt1011
->sysclk_src
)
1693 /* disable MCLK detect in default */
1694 snd_soc_component_update_bits(component
, RT1011_CLK_DET
,
1695 RT1011_EN_MCLK_DET_MASK
, 0);
1698 case RT1011_FS_SYS_PRE_S_MCLK
:
1699 reg_val
|= RT1011_FS_SYS_PRE_MCLK
;
1700 snd_soc_component_update_bits(component
, RT1011_CLK_DET
,
1701 RT1011_EN_MCLK_DET_MASK
, RT1011_EN_MCLK_DET
);
1703 case RT1011_FS_SYS_PRE_S_BCLK
:
1704 reg_val
|= RT1011_FS_SYS_PRE_BCLK
;
1706 case RT1011_FS_SYS_PRE_S_PLL1
:
1707 reg_val
|= RT1011_FS_SYS_PRE_PLL1
;
1709 case RT1011_FS_SYS_PRE_S_RCCLK
:
1710 reg_val
|= RT1011_FS_SYS_PRE_RCCLK
;
1713 dev_err(component
->dev
, "Invalid clock id (%d)\n", clk_id
);
1716 snd_soc_component_update_bits(component
, RT1011_CLK_2
,
1717 RT1011_FS_SYS_PRE_MASK
, reg_val
);
1718 rt1011
->sysclk
= freq
;
1719 rt1011
->sysclk_src
= clk_id
;
1721 dev_dbg(component
->dev
, "Sysclk is %dHz and clock id is %d\n",
1727 static int rt1011_set_component_pll(struct snd_soc_component
*component
,
1728 int pll_id
, int source
, unsigned int freq_in
,
1729 unsigned int freq_out
)
1731 struct rt1011_priv
*rt1011
= snd_soc_component_get_drvdata(component
);
1732 struct rl6231_pll_code pll_code
;
1735 if (source
== rt1011
->pll_src
&& freq_in
== rt1011
->pll_in
&&
1736 freq_out
== rt1011
->pll_out
)
1739 if (!freq_in
|| !freq_out
) {
1740 dev_dbg(component
->dev
, "PLL disabled\n");
1743 rt1011
->pll_out
= 0;
1744 snd_soc_component_update_bits(component
, RT1011_CLK_2
,
1745 RT1011_FS_SYS_PRE_MASK
, RT1011_FS_SYS_PRE_BCLK
);
1750 case RT1011_PLL2_S_MCLK
:
1751 snd_soc_component_update_bits(component
, RT1011_CLK_2
,
1752 RT1011_PLL2_SRC_MASK
, RT1011_PLL2_SRC_MCLK
);
1753 snd_soc_component_update_bits(component
, RT1011_CLK_2
,
1754 RT1011_PLL1_SRC_MASK
, RT1011_PLL1_SRC_PLL2
);
1755 snd_soc_component_update_bits(component
, RT1011_CLK_DET
,
1756 RT1011_EN_MCLK_DET_MASK
, RT1011_EN_MCLK_DET
);
1758 case RT1011_PLL1_S_BCLK
:
1759 snd_soc_component_update_bits(component
, RT1011_CLK_2
,
1760 RT1011_PLL1_SRC_MASK
, RT1011_PLL1_SRC_BCLK
);
1762 case RT1011_PLL2_S_RCCLK
:
1763 snd_soc_component_update_bits(component
, RT1011_CLK_2
,
1764 RT1011_PLL2_SRC_MASK
, RT1011_PLL2_SRC_RCCLK
);
1765 snd_soc_component_update_bits(component
, RT1011_CLK_2
,
1766 RT1011_PLL1_SRC_MASK
, RT1011_PLL1_SRC_PLL2
);
1769 dev_err(component
->dev
, "Unknown PLL Source %d\n", source
);
1773 ret
= rl6231_pll_calc(freq_in
, freq_out
, &pll_code
);
1775 dev_err(component
->dev
, "Unsupported input clock %d\n",
1780 dev_dbg(component
->dev
, "bypass=%d m=%d n=%d k=%d\n",
1781 pll_code
.m_bp
, (pll_code
.m_bp
? 0 : pll_code
.m_code
),
1782 pll_code
.n_code
, pll_code
.k_code
);
1784 snd_soc_component_write(component
, RT1011_PLL_1
,
1785 (pll_code
.m_bp
? 0 : pll_code
.m_code
) << RT1011_PLL1_QM_SFT
|
1786 pll_code
.m_bp
<< RT1011_PLL1_BPM_SFT
| pll_code
.n_code
);
1787 snd_soc_component_write(component
, RT1011_PLL_2
,
1790 rt1011
->pll_in
= freq_in
;
1791 rt1011
->pll_out
= freq_out
;
1792 rt1011
->pll_src
= source
;
1797 static int rt1011_set_tdm_slot(struct snd_soc_dai
*dai
,
1798 unsigned int tx_mask
, unsigned int rx_mask
, int slots
, int slot_width
)
1800 struct snd_soc_component
*component
= dai
->component
;
1801 struct snd_soc_dapm_context
*dapm
=
1802 snd_soc_component_get_dapm(component
);
1803 unsigned int val
= 0, tdm_en
= 0, rx_slotnum
, tx_slotnum
;
1804 int ret
= 0, first_bit
, last_bit
;
1806 snd_soc_dapm_mutex_lock(dapm
);
1807 if (rx_mask
|| tx_mask
)
1808 tdm_en
= RT1011_TDM_I2S_DOCK_EN_1
;
1812 val
|= RT1011_I2S_TX_4CH
;
1813 val
|= RT1011_I2S_RX_4CH
;
1816 val
|= RT1011_I2S_TX_6CH
;
1817 val
|= RT1011_I2S_RX_6CH
;
1820 val
|= RT1011_I2S_TX_8CH
;
1821 val
|= RT1011_I2S_RX_8CH
;
1830 switch (slot_width
) {
1832 val
|= RT1011_I2S_CH_TX_LEN_20B
;
1833 val
|= RT1011_I2S_CH_RX_LEN_20B
;
1836 val
|= RT1011_I2S_CH_TX_LEN_24B
;
1837 val
|= RT1011_I2S_CH_RX_LEN_24B
;
1840 val
|= RT1011_I2S_CH_TX_LEN_32B
;
1841 val
|= RT1011_I2S_CH_RX_LEN_32B
;
1850 /* Rx slot configuration */
1851 rx_slotnum
= hweight_long(rx_mask
);
1852 first_bit
= find_next_bit((unsigned long *)&rx_mask
, 32, 0);
1853 if (rx_slotnum
> 1 || rx_slotnum
== 0) {
1855 dev_dbg(component
->dev
, "too many rx slots or zero slot\n");
1859 switch (first_bit
) {
1864 snd_soc_component_update_bits(component
,
1865 RT1011_CROSS_BQ_SET_1
, RT1011_MONO_LR_SEL_MASK
,
1866 RT1011_MONO_L_CHANNEL
);
1867 snd_soc_component_update_bits(component
,
1869 RT1011_TDM_I2S_TX_L_DAC1_1_MASK
|
1870 RT1011_TDM_I2S_TX_R_DAC1_1_MASK
,
1871 (first_bit
<< RT1011_TDM_I2S_TX_L_DAC1_1_SFT
) |
1872 ((first_bit
+1) << RT1011_TDM_I2S_TX_R_DAC1_1_SFT
));
1878 snd_soc_component_update_bits(component
,
1879 RT1011_CROSS_BQ_SET_1
, RT1011_MONO_LR_SEL_MASK
,
1880 RT1011_MONO_R_CHANNEL
);
1881 snd_soc_component_update_bits(component
,
1883 RT1011_TDM_I2S_TX_L_DAC1_1_MASK
|
1884 RT1011_TDM_I2S_TX_R_DAC1_1_MASK
,
1885 ((first_bit
-1) << RT1011_TDM_I2S_TX_L_DAC1_1_SFT
) |
1886 (first_bit
<< RT1011_TDM_I2S_TX_R_DAC1_1_SFT
));
1893 /* Tx slot configuration */
1894 tx_slotnum
= hweight_long(tx_mask
);
1895 first_bit
= find_next_bit((unsigned long *)&tx_mask
, 32, 0);
1896 last_bit
= find_last_bit((unsigned long *)&tx_mask
, 32);
1897 if (tx_slotnum
> 2 || (last_bit
-first_bit
) > 1) {
1899 dev_dbg(component
->dev
, "too many tx slots or tx slot location error\n");
1903 if (tx_slotnum
== 1) {
1904 snd_soc_component_update_bits(component
, RT1011_TDM1_SET_2
,
1905 RT1011_TDM_I2S_DOCK_ADCDAT_LEN_1_MASK
|
1906 RT1011_TDM_ADCDAT1_DATA_LOCATION
, first_bit
);
1907 switch (first_bit
) {
1909 snd_soc_component_update_bits(component
,
1911 RT1011_TDM_I2S_RX_ADC1_1_MASK
,
1912 RT1011_TDM_I2S_RX_ADC1_1_LL
);
1915 snd_soc_component_update_bits(component
,
1917 RT1011_TDM_I2S_RX_ADC2_1_MASK
,
1918 RT1011_TDM_I2S_RX_ADC2_1_LL
);
1921 snd_soc_component_update_bits(component
,
1923 RT1011_TDM_I2S_RX_ADC3_1_MASK
,
1924 RT1011_TDM_I2S_RX_ADC3_1_LL
);
1927 snd_soc_component_update_bits(component
,
1929 RT1011_TDM_I2S_RX_ADC4_1_MASK
,
1930 RT1011_TDM_I2S_RX_ADC4_1_LL
);
1933 snd_soc_component_update_bits(component
,
1935 RT1011_TDM_I2S_RX_ADC1_1_MASK
, 0);
1938 snd_soc_component_update_bits(component
,
1940 RT1011_TDM_I2S_RX_ADC2_1_MASK
, 0);
1943 snd_soc_component_update_bits(component
,
1945 RT1011_TDM_I2S_RX_ADC3_1_MASK
, 0);
1948 snd_soc_component_update_bits(component
,
1950 RT1011_TDM_I2S_RX_ADC4_1_MASK
, 0);
1954 dev_dbg(component
->dev
,
1955 "tx slot location error\n");
1958 } else if (tx_slotnum
== 2) {
1959 switch (first_bit
) {
1964 snd_soc_component_update_bits(component
,
1966 RT1011_TDM_I2S_DOCK_ADCDAT_LEN_1_MASK
|
1967 RT1011_TDM_ADCDAT1_DATA_LOCATION
,
1968 RT1011_TDM_I2S_DOCK_ADCDAT_2CH
| first_bit
);
1972 dev_dbg(component
->dev
,
1973 "tx slot location should be paired and start from slot0/2/4/6\n");
1978 snd_soc_component_update_bits(component
, RT1011_TDM1_SET_1
,
1979 RT1011_I2S_CH_TX_MASK
| RT1011_I2S_CH_RX_MASK
|
1980 RT1011_I2S_CH_TX_LEN_MASK
| RT1011_I2S_CH_RX_LEN_MASK
, val
);
1981 snd_soc_component_update_bits(component
, RT1011_TDM2_SET_1
,
1982 RT1011_I2S_CH_TX_MASK
| RT1011_I2S_CH_RX_MASK
|
1983 RT1011_I2S_CH_TX_LEN_MASK
| RT1011_I2S_CH_RX_LEN_MASK
, val
);
1984 snd_soc_component_update_bits(component
, RT1011_TDM1_SET_2
,
1985 RT1011_TDM_I2S_DOCK_EN_1_MASK
, tdm_en
);
1986 snd_soc_component_update_bits(component
, RT1011_TDM2_SET_2
,
1987 RT1011_TDM_I2S_DOCK_EN_2_MASK
, tdm_en
);
1989 snd_soc_component_update_bits(component
, RT1011_TDM_TOTAL_SET
,
1990 RT1011_ADCDAT1_PIN_CONFIG
| RT1011_ADCDAT2_PIN_CONFIG
,
1991 RT1011_ADCDAT1_OUTPUT
| RT1011_ADCDAT2_OUTPUT
);
1994 snd_soc_dapm_mutex_unlock(dapm
);
1998 static int rt1011_probe(struct snd_soc_component
*component
)
2000 struct rt1011_priv
*rt1011
= snd_soc_component_get_drvdata(component
);
2003 rt1011
->component
= component
;
2005 schedule_work(&rt1011
->cali_work
);
2007 rt1011
->bq_drc_params
= devm_kcalloc(component
->dev
,
2008 RT1011_ADVMODE_NUM
, sizeof(struct rt1011_bq_drc_params
*),
2010 if (!rt1011
->bq_drc_params
)
2013 for (i
= 0; i
< RT1011_ADVMODE_NUM
; i
++) {
2014 rt1011
->bq_drc_params
[i
] = devm_kcalloc(component
->dev
,
2015 RT1011_BQ_DRC_NUM
, sizeof(struct rt1011_bq_drc_params
),
2017 if (!rt1011
->bq_drc_params
[i
])
2024 static void rt1011_remove(struct snd_soc_component
*component
)
2026 struct rt1011_priv
*rt1011
= snd_soc_component_get_drvdata(component
);
2028 cancel_work_sync(&rt1011
->cali_work
);
2029 rt1011_reset(rt1011
->regmap
);
2033 static int rt1011_suspend(struct snd_soc_component
*component
)
2035 struct rt1011_priv
*rt1011
= snd_soc_component_get_drvdata(component
);
2037 regcache_cache_only(rt1011
->regmap
, true);
2038 regcache_mark_dirty(rt1011
->regmap
);
2043 static int rt1011_resume(struct snd_soc_component
*component
)
2045 struct rt1011_priv
*rt1011
= snd_soc_component_get_drvdata(component
);
2047 regcache_cache_only(rt1011
->regmap
, false);
2048 regcache_sync(rt1011
->regmap
);
2053 #define rt1011_suspend NULL
2054 #define rt1011_resume NULL
2057 static int rt1011_set_bias_level(struct snd_soc_component
*component
,
2058 enum snd_soc_bias_level level
)
2061 case SND_SOC_BIAS_OFF
:
2062 snd_soc_component_write(component
,
2063 RT1011_SYSTEM_RESET_1
, 0x0000);
2064 snd_soc_component_write(component
,
2065 RT1011_SYSTEM_RESET_2
, 0x0000);
2066 snd_soc_component_write(component
,
2067 RT1011_SYSTEM_RESET_3
, 0x0001);
2068 snd_soc_component_write(component
,
2069 RT1011_SYSTEM_RESET_1
, 0x003f);
2070 snd_soc_component_write(component
,
2071 RT1011_SYSTEM_RESET_2
, 0x7fd7);
2072 snd_soc_component_write(component
,
2073 RT1011_SYSTEM_RESET_3
, 0x770f);
2082 #define RT1011_STEREO_RATES SNDRV_PCM_RATE_8000_192000
2083 #define RT1011_FORMATS (SNDRV_PCM_FMTBIT_S8 | \
2084 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S16_LE | \
2085 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
2087 static const struct snd_soc_dai_ops rt1011_aif_dai_ops
= {
2088 .hw_params
= rt1011_hw_params
,
2089 .set_fmt
= rt1011_set_dai_fmt
,
2090 .set_tdm_slot
= rt1011_set_tdm_slot
,
2093 static struct snd_soc_dai_driver rt1011_dai
[] = {
2095 .name
= "rt1011-aif",
2097 .stream_name
= "AIF1 Playback",
2100 .rates
= RT1011_STEREO_RATES
,
2101 .formats
= RT1011_FORMATS
,
2103 .ops
= &rt1011_aif_dai_ops
,
2107 static const struct snd_soc_component_driver soc_component_dev_rt1011
= {
2108 .probe
= rt1011_probe
,
2109 .remove
= rt1011_remove
,
2110 .suspend
= rt1011_suspend
,
2111 .resume
= rt1011_resume
,
2112 .set_bias_level
= rt1011_set_bias_level
,
2113 .controls
= rt1011_snd_controls
,
2114 .num_controls
= ARRAY_SIZE(rt1011_snd_controls
),
2115 .dapm_widgets
= rt1011_dapm_widgets
,
2116 .num_dapm_widgets
= ARRAY_SIZE(rt1011_dapm_widgets
),
2117 .dapm_routes
= rt1011_dapm_routes
,
2118 .num_dapm_routes
= ARRAY_SIZE(rt1011_dapm_routes
),
2119 .set_sysclk
= rt1011_set_component_sysclk
,
2120 .set_pll
= rt1011_set_component_pll
,
2121 .use_pmdown_time
= 1,
2123 .non_legacy_dai_naming
= 1,
2126 static const struct regmap_config rt1011_regmap
= {
2129 .max_register
= RT1011_MAX_REG
+ 1,
2130 .volatile_reg
= rt1011_volatile_register
,
2131 .readable_reg
= rt1011_readable_register
,
2132 .cache_type
= REGCACHE_RBTREE
,
2133 .reg_defaults
= rt1011_reg
,
2134 .num_reg_defaults
= ARRAY_SIZE(rt1011_reg
),
2135 .use_single_read
= true,
2136 .use_single_write
= true,
2139 #if defined(CONFIG_OF)
2140 static const struct of_device_id rt1011_of_match
[] = {
2141 { .compatible
= "realtek,rt1011", },
2144 MODULE_DEVICE_TABLE(of
, rt1011_of_match
);
2148 static struct acpi_device_id rt1011_acpi_match
[] = {
2152 MODULE_DEVICE_TABLE(acpi
, rt1011_acpi_match
);
2155 static const struct i2c_device_id rt1011_i2c_id
[] = {
2159 MODULE_DEVICE_TABLE(i2c
, rt1011_i2c_id
);
2161 static int rt1011_calibrate(struct rt1011_priv
*rt1011
, unsigned char cali_flag
)
2163 unsigned int value
, count
= 0, r0
[3];
2164 unsigned int chk_cnt
= 50; /* DONT change this */
2165 unsigned int dc_offset
;
2166 unsigned int r0_integer
, r0_factor
, format
;
2167 struct device
*dev
= regmap_get_device(rt1011
->regmap
);
2168 struct snd_soc_dapm_context
*dapm
=
2169 snd_soc_component_get_dapm(rt1011
->component
);
2172 snd_soc_dapm_mutex_lock(dapm
);
2173 regcache_cache_bypass(rt1011
->regmap
, true);
2175 regmap_write(rt1011
->regmap
, RT1011_RESET
, 0x0000);
2176 regmap_write(rt1011
->regmap
, RT1011_SYSTEM_RESET_3
, 0x740f);
2177 regmap_write(rt1011
->regmap
, RT1011_SYSTEM_RESET_3
, 0x770f);
2180 regmap_write(rt1011
->regmap
, RT1011_CLK_2
, 0x9400);
2181 regmap_write(rt1011
->regmap
, RT1011_PLL_1
, 0x0800);
2182 regmap_write(rt1011
->regmap
, RT1011_PLL_2
, 0x0020);
2183 regmap_write(rt1011
->regmap
, RT1011_CLK_DET
, 0x0800);
2185 /* ADC/DAC setting */
2186 regmap_write(rt1011
->regmap
, RT1011_ADC_SET_5
, 0x0a20);
2187 regmap_write(rt1011
->regmap
, RT1011_DAC_SET_2
, 0xe232);
2188 regmap_write(rt1011
->regmap
, RT1011_ADC_SET_4
, 0xc000);
2191 regmap_write(rt1011
->regmap
, RT1011_SPK_PRO_DC_DET_1
, 0xb00c);
2192 regmap_write(rt1011
->regmap
, RT1011_SPK_PRO_DC_DET_2
, 0xcccc);
2195 regmap_write(rt1011
->regmap
, RT1011_POWER_1
, 0xe0e0);
2196 regmap_write(rt1011
->regmap
, RT1011_POWER_3
, 0x5003);
2197 regmap_write(rt1011
->regmap
, RT1011_POWER_9
, 0xa860);
2198 regmap_write(rt1011
->regmap
, RT1011_DAC_SET_2
, 0xa032);
2200 /* POW_PLL / POW_BG / POW_BG_MBIAS_LV / POW_V/I */
2201 regmap_write(rt1011
->regmap
, RT1011_POWER_2
, 0x0007);
2202 regmap_write(rt1011
->regmap
, RT1011_POWER_3
, 0x5ff7);
2203 regmap_write(rt1011
->regmap
, RT1011_A_TEMP_SEN
, 0x7f44);
2204 regmap_write(rt1011
->regmap
, RT1011_A_TIMING_1
, 0x4054);
2205 regmap_write(rt1011
->regmap
, RT1011_BAT_GAIN_1
, 0x309c);
2207 /* DC offset from EFUSE */
2208 regmap_write(rt1011
->regmap
, RT1011_DC_CALIB_CLASSD_3
, 0xcb00);
2209 regmap_write(rt1011
->regmap
, RT1011_BOOST_CON_1
, 0xe080);
2210 regmap_write(rt1011
->regmap
, RT1011_POWER_4
, 0x16f2);
2211 regmap_write(rt1011
->regmap
, RT1011_POWER_6
, 0x36ad);
2214 regmap_write(rt1011
->regmap
, RT1011_MIXER_1
, 0x3f1d);
2217 regmap_write(rt1011
->regmap
, RT1011_EFUSE_CONTROL_1
, 0x0d0a);
2220 regmap_read(rt1011
->regmap
, RT1011_EFUSE_ADC_OFFSET_18_16
, &value
);
2221 dc_offset
= value
<< 16;
2222 regmap_read(rt1011
->regmap
, RT1011_EFUSE_ADC_OFFSET_15_0
, &value
);
2223 dc_offset
|= (value
& 0xffff);
2224 dev_info(dev
, "ADC offset=0x%x\n", dc_offset
);
2225 regmap_read(rt1011
->regmap
, RT1011_EFUSE_DAC_OFFSET_G0_20_16
, &value
);
2226 dc_offset
= value
<< 16;
2227 regmap_read(rt1011
->regmap
, RT1011_EFUSE_DAC_OFFSET_G0_15_0
, &value
);
2228 dc_offset
|= (value
& 0xffff);
2229 dev_info(dev
, "Gain0 offset=0x%x\n", dc_offset
);
2230 regmap_read(rt1011
->regmap
, RT1011_EFUSE_DAC_OFFSET_G1_20_16
, &value
);
2231 dc_offset
= value
<< 16;
2232 regmap_read(rt1011
->regmap
, RT1011_EFUSE_DAC_OFFSET_G1_15_0
, &value
);
2233 dc_offset
|= (value
& 0xffff);
2234 dev_info(dev
, "Gain1 offset=0x%x\n", dc_offset
);
2236 /* check the package info. */
2237 regmap_read(rt1011
->regmap
, RT1011_EFUSE_MATCH_DONE
, &value
);
2239 rt1011
->pack_id
= 1;
2243 if (rt1011
->pack_id
)
2244 regmap_write(rt1011
->regmap
, RT1011_ADC_SET_1
, 0x292c);
2246 regmap_write(rt1011
->regmap
, RT1011_ADC_SET_1
, 0x2925);
2249 regmap_write(rt1011
->regmap
, RT1011_CLASS_D_POS
, 0x010e);
2250 regmap_write(rt1011
->regmap
,
2251 RT1011_CLASSD_INTERNAL_SET_1
, 0x1701);
2254 regmap_write(rt1011
->regmap
, RT1011_SPK_TEMP_PROTECT_0
, 0x8000);
2255 regmap_write(rt1011
->regmap
, RT1011_SPK_TEMP_PROTECT_7
, 0xf000);
2256 regmap_write(rt1011
->regmap
, RT1011_SPK_TEMP_PROTECT_4
, 0x4040);
2257 regmap_write(rt1011
->regmap
, RT1011_SPK_TEMP_PROTECT_0
, 0xc000);
2258 regmap_write(rt1011
->regmap
, RT1011_SPK_TEMP_PROTECT_6
, 0x07c2);
2260 r0
[0] = r0
[1] = r0
[2] = count
= 0;
2261 while (count
< chk_cnt
) {
2263 regmap_read(rt1011
->regmap
,
2264 RT1011_INIT_RECIPROCAL_SYN_24_16
, &value
);
2265 r0
[count
%3] = value
<< 16;
2266 regmap_read(rt1011
->regmap
,
2267 RT1011_INIT_RECIPROCAL_SYN_15_0
, &value
);
2268 r0
[count
%3] |= value
;
2270 if (r0
[count
%3] == 0)
2275 if (r0
[0] == r0
[1] && r0
[1] == r0
[2])
2278 if (count
> chk_cnt
) {
2279 dev_err(dev
, "Calibrate R0 Failure\n");
2282 format
= 2147483648U; /* 2^24 * 128 */
2283 r0_integer
= format
/ r0
[0] / 128;
2284 r0_factor
= ((format
/ r0
[0] * 100) / 128)
2285 - (r0_integer
* 100);
2286 rt1011
->r0_reg
= r0
[0];
2287 rt1011
->cali_done
= 1;
2288 dev_info(dev
, "r0 resistance about %d.%02d ohm, reg=0x%X\n",
2289 r0_integer
, r0_factor
, r0
[0]);
2294 regmap_write(rt1011
->regmap
, RT1011_SPK_TEMP_PROTECT_0
, 0x0000);
2296 regmap_write(rt1011
->regmap
, RT1011_POWER_9
, 0xa840);
2297 regmap_write(rt1011
->regmap
, RT1011_SPK_TEMP_PROTECT_6
, 0x0702);
2298 regmap_write(rt1011
->regmap
, RT1011_MIXER_1
, 0xffdd);
2299 regmap_write(rt1011
->regmap
, RT1011_CLASSD_INTERNAL_SET_1
, 0x0701);
2300 regmap_write(rt1011
->regmap
, RT1011_DAC_SET_3
, 0xe004);
2301 regmap_write(rt1011
->regmap
, RT1011_A_TEMP_SEN
, 0x7f40);
2302 regmap_write(rt1011
->regmap
, RT1011_POWER_1
, 0x0000);
2303 regmap_write(rt1011
->regmap
, RT1011_POWER_2
, 0x0000);
2304 regmap_write(rt1011
->regmap
, RT1011_POWER_3
, 0x0002);
2305 regmap_write(rt1011
->regmap
, RT1011_POWER_4
, 0x00f2);
2307 regmap_write(rt1011
->regmap
, RT1011_RESET
, 0x0000);
2310 if (count
<= chk_cnt
) {
2311 regmap_write(rt1011
->regmap
,
2312 RT1011_INIT_RECIPROCAL_REG_24_16
,
2313 ((r0
[0]>>16) & 0x1ff));
2314 regmap_write(rt1011
->regmap
,
2315 RT1011_INIT_RECIPROCAL_REG_15_0
,
2317 regmap_write(rt1011
->regmap
,
2318 RT1011_SPK_TEMP_PROTECT_4
, 0x4080);
2322 regcache_cache_bypass(rt1011
->regmap
, false);
2323 regcache_mark_dirty(rt1011
->regmap
);
2324 regcache_sync(rt1011
->regmap
);
2325 snd_soc_dapm_mutex_unlock(dapm
);
2330 static void rt1011_calibration_work(struct work_struct
*work
)
2332 struct rt1011_priv
*rt1011
=
2333 container_of(work
, struct rt1011_priv
, cali_work
);
2334 struct snd_soc_component
*component
= rt1011
->component
;
2335 unsigned int r0_integer
, r0_factor
, format
;
2337 if (rt1011
->r0_calib
)
2338 rt1011_calibrate(rt1011
, 0);
2340 rt1011_calibrate(rt1011
, 1);
2343 * This flag should reset after booting.
2344 * The factory test will do calibration again and use this flag to check
2345 * whether the calibration completed
2347 rt1011
->cali_done
= 0;
2350 rt1011_reg_init(component
);
2352 /* Apply temperature and calibration data from device property */
2353 if (rt1011
->temperature_calib
<= 0xff &&
2354 rt1011
->temperature_calib
> 0) {
2355 snd_soc_component_update_bits(component
,
2356 RT1011_STP_INITIAL_RESISTANCE_TEMP
, 0x3ff,
2357 (rt1011
->temperature_calib
<< 2));
2360 if (rt1011
->r0_calib
) {
2361 rt1011
->r0_reg
= rt1011
->r0_calib
;
2363 format
= 2147483648U; /* 2^24 * 128 */
2364 r0_integer
= format
/ rt1011
->r0_reg
/ 128;
2365 r0_factor
= ((format
/ rt1011
->r0_reg
* 100) / 128)
2366 - (r0_integer
* 100);
2367 dev_info(component
->dev
, "DP r0 resistance about %d.%02d ohm, reg=0x%X\n",
2368 r0_integer
, r0_factor
, rt1011
->r0_reg
);
2370 rt1011_r0_load(rt1011
);
2373 if (rt1011
->pack_id
)
2374 snd_soc_component_write(component
, RT1011_ADC_SET_1
, 0x292c);
2376 snd_soc_component_write(component
, RT1011_ADC_SET_1
, 0x2925);
2379 static int rt1011_parse_dp(struct rt1011_priv
*rt1011
, struct device
*dev
)
2381 device_property_read_u32(dev
, "realtek,temperature_calib",
2382 &rt1011
->temperature_calib
);
2383 device_property_read_u32(dev
, "realtek,r0_calib",
2386 dev_dbg(dev
, "%s: r0_calib: 0x%x, temperature_calib: 0x%x",
2387 __func__
, rt1011
->r0_calib
, rt1011
->temperature_calib
);
2392 static int rt1011_i2c_probe(struct i2c_client
*i2c
,
2393 const struct i2c_device_id
*id
)
2395 struct rt1011_priv
*rt1011
;
2399 rt1011
= devm_kzalloc(&i2c
->dev
, sizeof(struct rt1011_priv
),
2404 i2c_set_clientdata(i2c
, rt1011
);
2406 rt1011_parse_dp(rt1011
, &i2c
->dev
);
2408 rt1011
->regmap
= devm_regmap_init_i2c(i2c
, &rt1011_regmap
);
2409 if (IS_ERR(rt1011
->regmap
)) {
2410 ret
= PTR_ERR(rt1011
->regmap
);
2411 dev_err(&i2c
->dev
, "Failed to allocate register map: %d\n",
2416 regmap_read(rt1011
->regmap
, RT1011_DEVICE_ID
, &val
);
2417 if (val
!= RT1011_DEVICE_ID_NUM
) {
2419 "Device with ID register %x is not rt1011\n", val
);
2423 INIT_WORK(&rt1011
->cali_work
, rt1011_calibration_work
);
2425 return devm_snd_soc_register_component(&i2c
->dev
,
2426 &soc_component_dev_rt1011
,
2427 rt1011_dai
, ARRAY_SIZE(rt1011_dai
));
2431 static void rt1011_i2c_shutdown(struct i2c_client
*client
)
2433 struct rt1011_priv
*rt1011
= i2c_get_clientdata(client
);
2435 rt1011_reset(rt1011
->regmap
);
2438 static struct i2c_driver rt1011_i2c_driver
= {
2441 .of_match_table
= of_match_ptr(rt1011_of_match
),
2442 .acpi_match_table
= ACPI_PTR(rt1011_acpi_match
)
2444 .probe
= rt1011_i2c_probe
,
2445 .shutdown
= rt1011_i2c_shutdown
,
2446 .id_table
= rt1011_i2c_id
,
2448 module_i2c_driver(rt1011_i2c_driver
);
2450 MODULE_DESCRIPTION("ASoC RT1011 amplifier driver");
2451 MODULE_AUTHOR("Shuming Fan <shumingf@realtek.com>");
2452 MODULE_LICENSE("GPL");