瀏覽代碼

Merge 53937508d2869b15165eb2981b0f765ea49eabd8 on remote branch

Change-Id: I6e1f4b4eec8d89b47175973db5d8df22816a3f1c
Linux Build Service Account 2 年之前
父節點
當前提交
bb2700a141

+ 38 - 7
asoc/codecs/lpass-cdc/lpass-cdc-wsa-macro.c

@@ -314,10 +314,12 @@ struct lpass_cdc_wsa_macro_priv {
 	u32 wsa_sys_gain[2 * (LPASS_CDC_WSA_MACRO_RX1 + 1)];
 	u32 wsa_bat_cfg[LPASS_CDC_WSA_MACRO_RX1 + 1];
 	u32 wsa_rload[LPASS_CDC_WSA_MACRO_RX1 + 1];
+	u32 wsa_fs_ctl_reg;
 	u8 idle_detect_en;
 	int noise_gate_mode;
 	bool pre_dev_up;
 	int pbr_clk_users;
+	char __iomem *wsa_fs_reg_base;
 };
 
 static struct snd_soc_dai_driver lpass_cdc_wsa_macro_dai[];
@@ -590,7 +592,7 @@ static int lpass_cdc_wsa_macro_set_prim_interpolator_rate(struct snd_soc_dai *da
 			 LPASS_CDC_WSA_MACRO_RX_MAX) {
 		int_1_mix1_inp = port;
 		if ((int_1_mix1_inp < LPASS_CDC_WSA_MACRO_RX0) ||
-			(int_1_mix1_inp > LPASS_CDC_WSA_MACRO_RX_MIX1)) {
+			(int_1_mix1_inp > LPASS_CDC_WSA_MACRO_RX_MAX)) {
 			dev_err_ratelimited(wsa_dev,
 				"%s: Invalid RX port, Dai ID is %d\n",
 				__func__, dai->id);
@@ -900,6 +902,7 @@ static int lpass_cdc_wsa_macro_mute_stream(struct snd_soc_dai *dai, int mute, in
 	struct device *wsa_dev = NULL;
 	struct lpass_cdc_wsa_macro_priv *wsa_priv = NULL;
 	bool adie_lb = false;
+	uint32_t temp;
 
 	if (mute)
 		return 0;
@@ -912,10 +915,23 @@ static int lpass_cdc_wsa_macro_mute_stream(struct snd_soc_dai *dai, int mute, in
 		lpass_cdc_wsa_pa_on(wsa_dev, adie_lb);
 		lpass_cdc_wsa_unmute_interpolator(dai);
 		lpass_cdc_wsa_macro_enable_vi_decimator(component);
-	break;
+		break;
 	default:
-	break;
+		break;
+	}
+	if ((test_bit(LPASS_CDC_WSA_MACRO_RX4,
+			&wsa_priv->active_ch_mask[dai->id]) ||
+			test_bit(LPASS_CDC_WSA_MACRO_RX5,
+			&wsa_priv->active_ch_mask[dai->id])) &&
+			wsa_priv->wsa_fs_reg_base) {
+		temp = ioread32(wsa_priv->wsa_fs_reg_base);
+		if (temp != 0) {
+			temp = 0;
+			iowrite32(temp, wsa_priv->wsa_fs_reg_base);
+		}
+		dev_dbg(wsa_dev, "%s: LPASS_WSA_FS_CTL : %d", __func__, temp);
 	}
+
 	return 0;
 }
 
@@ -1374,6 +1390,7 @@ static int lpass_cdc_wsa_macro_config_compander(struct snd_soc_component *compon
 	struct lpass_cdc_wsa_macro_priv *wsa_priv = NULL;
 	struct lpass_cdc_comp_setting *comp_settings = NULL;
 	u16 mode = 0;
+	u16 index = 0;
 	int sys_gain, bat_cfg, sys_gain_int, upper_gain, lower_gain;
 
 	if (!lpass_cdc_wsa_macro_get_data(component, &wsa_dev, &wsa_priv, __func__))
@@ -1404,7 +1421,13 @@ static int lpass_cdc_wsa_macro_config_compander(struct snd_soc_component *compon
 
 	/* If System has battery configuration */
 	if (wsa_priv->wsa_bat_cfg[comp]) {
-		sys_gain = wsa_priv->wsa_sys_gain[comp * 2 + wsa_priv->wsa_spkrrecv];
+		index = (comp * 2) + wsa_priv->wsa_spkrrecv;
+		if (index >= (2 * (LPASS_CDC_WSA_MACRO_RX1 + 1))) {
+			dev_err(component->dev, "%s: Invalid index: %d\n",
+					__func__, index);
+			return -EINVAL;
+		}
+		sys_gain = wsa_priv->wsa_sys_gain[index];
 		bat_cfg = wsa_priv->wsa_bat_cfg[comp];
 		/* Convert enum to value and
 		 * multiply all values by 10 to avoid float
@@ -2914,9 +2937,6 @@ static const struct snd_soc_dapm_widget lpass_cdc_wsa_macro_dapm_widgets[] = {
 	SND_SOC_DAPM_AIF_OUT("WSA AIF_CPS", "WSA_AIF_CPS Capture", 0,
 		SND_SOC_NOPM, 0, 0),
 
-	SND_SOC_DAPM_AIF_OUT("WSA AIF_CPS", "WSA_AIF_CPS Capture", 0,
-		SND_SOC_NOPM, 0, 0),
-
 	SND_SOC_DAPM_MIXER("WSA_AIF_VI Mixer", SND_SOC_NOPM, LPASS_CDC_WSA_MACRO_AIF_VI,
 		0, aif_vi_mixer, ARRAY_SIZE(aif_vi_mixer)),
 	SND_SOC_DAPM_MIXER("WSA_AIF_CPS Mixer", SND_SOC_NOPM, LPASS_CDC_WSA_MACRO_AIF_CPS,
@@ -3847,6 +3867,17 @@ static int lpass_cdc_wsa_macro_probe(struct platform_device *pdev)
 			__func__, "reg");
 		return ret;
 	}
+	ret = of_property_read_u32(pdev->dev.of_node, "wsa_data_fs_ctl_reg",
+					&wsa_priv->wsa_fs_ctl_reg);
+	if (ret) {
+		dev_dbg(&pdev->dev, "%s: error finding %s entry in dt\n",
+			__func__, "wsa_data_fs_ctl_reg");
+	}
+
+	if (!wsa_priv->wsa_fs_reg_base && wsa_priv->wsa_fs_ctl_reg)
+		wsa_priv->wsa_fs_reg_base = devm_ioremap(&pdev->dev,
+			wsa_priv->wsa_fs_ctl_reg, LPASS_CDC_WSA_MACRO_MAX_OFFSET);
+
 	if (of_find_property(pdev->dev.of_node, is_used_wsa_swr_gpio_dt,
 			     NULL)) {
 		ret = of_property_read_u32(pdev->dev.of_node,

+ 80 - 13
asoc/codecs/lpass-cdc/lpass-cdc-wsa2-macro.c

@@ -314,10 +314,13 @@ struct lpass_cdc_wsa2_macro_priv {
 	u32 wsa2_sys_gain[2 * (LPASS_CDC_WSA2_MACRO_RX1 + 1)];
 	u32 wsa2_bat_cfg[LPASS_CDC_WSA2_MACRO_RX1 + 1];
 	u32 wsa2_rload[LPASS_CDC_WSA2_MACRO_RX1 + 1];
+	u32 wsa2_fs_ctl_reg;
 	u8 idle_detect_en;
 	int noise_gate_mode;
 	bool pre_dev_up;
 	int pbr_clk_users;
+	char __iomem *wsa2_fs_reg_base;
+	bool wsa2_2ch_dma_enable;
 };
 
 static struct snd_soc_dai_driver lpass_cdc_wsa2_macro_dai[];
@@ -590,7 +593,7 @@ static int lpass_cdc_wsa2_macro_set_prim_interpolator_rate(struct snd_soc_dai *d
 			 LPASS_CDC_WSA2_MACRO_RX_MAX) {
 		int_1_mix1_inp = port;
 		if ((int_1_mix1_inp < LPASS_CDC_WSA2_MACRO_RX0) ||
-			(int_1_mix1_inp > LPASS_CDC_WSA2_MACRO_RX_MIX1)) {
+			(int_1_mix1_inp >= LPASS_CDC_WSA2_MACRO_RX_MAX)) {
 			dev_err_ratelimited(wsa2_dev,
 				"%s: Invalid RX port, Dai ID is %d\n",
 				__func__, dai->id);
@@ -831,11 +834,13 @@ static int lpass_cdc_wsa2_macro_get_channel_map(struct snd_soc_dai *dai,
 			if (++cnt == LPASS_CDC_WSA2_MACRO_MAX_DMA_CH_PER_PORT)
 				break;
 		}
-		if (mask & 0x30)
-			mask = mask >> 0x4;
-		if (mask & 0x03)
-			mask = mask << 0x2;
-
+		/* consider WSA2 Backend is used when 2ch_dma is enabled
+		 * and doesn't require channel mask shift
+		 */
+		if (!wsa2_priv->wsa2_2ch_dma_enable) {
+			if (mask & 0x03)
+				mask = mask << 0x2;
+		}
 		*tx_slot = mask;
 		*tx_num = cnt;
 		break;
@@ -853,7 +858,7 @@ static int lpass_cdc_wsa2_macro_get_channel_map(struct snd_soc_dai *dai,
 		}
 		if (mask & 0x30)
 			mask = mask >> 0x4;
-		if (mask & 0x03)
+		else
 			mask = mask << 0x2;
 		*rx_slot = mask;
 		*rx_num = cnt;
@@ -873,7 +878,7 @@ static int lpass_cdc_wsa2_macro_get_channel_map(struct snd_soc_dai *dai,
 		*tx_num = cnt;
 		break;
 	default:
-		dev_err_ratelimited(wsa2_dev, "%s: Invalid AIF\n", __func__);
+		dev_err(wsa2_dev, "%s: Invalid AIF\n", __func__);
 		break;
 	}
 	return 0;
@@ -904,6 +909,8 @@ static int lpass_cdc_wsa2_macro_mute_stream(struct snd_soc_dai *dai, int mute, i
 	struct snd_soc_component *component = dai->component;
 	struct device *wsa2_dev = NULL;
 	struct lpass_cdc_wsa2_macro_priv *wsa2_priv = NULL;
+	uint32_t temp;
+
 	bool adie_lb = false;
 
 	if (mute)
@@ -921,6 +928,20 @@ static int lpass_cdc_wsa2_macro_mute_stream(struct snd_soc_dai *dai, int mute, i
 	default:
 		break;
 	}
+
+	if ((test_bit(LPASS_CDC_WSA2_MACRO_RX4,
+			&wsa2_priv->active_ch_mask[dai->id]) ||
+			test_bit(LPASS_CDC_WSA2_MACRO_RX5,
+			&wsa2_priv->active_ch_mask[dai->id])) &&
+			wsa2_priv->wsa2_fs_reg_base) {
+		temp = ioread32(wsa2_priv->wsa2_fs_reg_base);
+		if (temp != 1) {
+			temp = 1;
+			iowrite32(temp, wsa2_priv->wsa2_fs_reg_base);
+		}
+		dev_dbg(wsa2_dev, "%s: LPASS_WSA_FS_CTL : %d", __func__, temp);
+	}
+
 	return 0;
 }
 
@@ -1379,6 +1400,7 @@ static int lpass_cdc_wsa2_macro_config_compander(struct snd_soc_component *compo
 	struct lpass_cdc_wsa2_macro_priv *wsa2_priv = NULL;
 	struct lpass_cdc_comp_setting *comp_settings = NULL;
 	u16 mode = 0;
+	u16 index = 0;
 	int sys_gain, bat_cfg, sys_gain_int, upper_gain, lower_gain;
 
 	if (!lpass_cdc_wsa2_macro_get_data(component, &wsa2_dev, &wsa2_priv, __func__))
@@ -1409,7 +1431,13 @@ static int lpass_cdc_wsa2_macro_config_compander(struct snd_soc_component *compo
 
 	/* If System has battery configuration */
 	if (wsa2_priv->wsa2_bat_cfg[comp]) {
-		sys_gain = wsa2_priv->wsa2_sys_gain[comp * 2 + wsa2_priv->wsa2_spkrrecv];
+		index = (comp * 2) + wsa2_priv->wsa2_spkrrecv;
+		if (index >= (2 * (LPASS_CDC_WSA2_MACRO_RX1 + 1))) {
+			dev_err(component->dev, "%s: Invalid index: %d\n",
+					__func__, index);
+			return -EINVAL;
+		}
+		sys_gain = wsa2_priv->wsa2_sys_gain[index];
 		bat_cfg = wsa2_priv->wsa2_bat_cfg[comp];
 		/* Convert enum to value and
 		 * multiply all values by 10 to avoid float
@@ -2650,6 +2678,35 @@ static int lpass_cdc_wsa2_macro_pbr_enable_put(struct snd_kcontrol *kcontrol,
 
 }
 
+static int lpass_cdc_wsa2_macro_2ch_dma_enable_get(struct snd_kcontrol *kcontrol,
+				struct snd_ctl_elem_value *ucontrol)
+{
+	struct snd_soc_component *component =
+		snd_soc_kcontrol_component(kcontrol);
+	struct device *wsa2_dev = NULL;
+	struct lpass_cdc_wsa2_macro_priv *wsa2_priv = NULL;
+
+	if (!lpass_cdc_wsa2_macro_get_data(component, &wsa2_dev, &wsa2_priv, __func__))
+		return -EINVAL;
+
+	ucontrol->value.integer.value[0] = wsa2_priv->wsa2_2ch_dma_enable;
+	return 0;
+}
+
+static int lpass_cdc_wsa2_macro_2ch_dma_enable_put(struct snd_kcontrol *kcontrol,
+				struct snd_ctl_elem_value *ucontrol)
+{
+	struct snd_soc_component *component =
+		snd_soc_kcontrol_component(kcontrol);
+	struct device *wsa2_dev = NULL;
+	struct lpass_cdc_wsa2_macro_priv *wsa2_priv = NULL;
+
+	if (!lpass_cdc_wsa2_macro_get_data(component, &wsa2_dev, &wsa2_priv, __func__))
+		return -EINVAL;
+
+	wsa2_priv->wsa2_2ch_dma_enable = ucontrol->value.integer.value[0];
+	return 0;
+}
 
 static const struct snd_kcontrol_new lpass_cdc_wsa2_macro_snd_controls[] = {
 	SOC_ENUM_EXT("WSA2_GSM mode Enable", lpass_cdc_wsa2_macro_vbat_bcl_gsm_mode_enum,
@@ -2704,6 +2761,9 @@ static const struct snd_kcontrol_new lpass_cdc_wsa2_macro_snd_controls[] = {
 	SOC_SINGLE_EXT("WSA2 PBR Enable", SND_SOC_NOPM, 0, 1,
 			0, lpass_cdc_wsa2_macro_pbr_enable_get,
 			lpass_cdc_wsa2_macro_pbr_enable_put),
+	SOC_SINGLE_EXT("WSA2 2CH_DMA ENABLE", SND_SOC_NOPM, 0, 1,
+			0, lpass_cdc_wsa2_macro_2ch_dma_enable_get,
+			lpass_cdc_wsa2_macro_2ch_dma_enable_put),
 };
 
 static const struct soc_enum rx_mux_enum =
@@ -2918,10 +2978,6 @@ static const struct snd_soc_dapm_widget lpass_cdc_wsa2_macro_dapm_widgets[] = {
 	SND_SOC_DAPM_AIF_OUT("WSA2 AIF_CPS", "WSA2_AIF_CPS Capture", 0,
 		SND_SOC_NOPM, 0, 0),
 
-	SND_SOC_DAPM_AIF_OUT("WSA2 AIF_CPS", "WSA2_AIF_CPS Capture", 0,
-		SND_SOC_NOPM, 0, 0),
-
-
 	SND_SOC_DAPM_MIXER("WSA2_AIF_VI Mixer", SND_SOC_NOPM, LPASS_CDC_WSA2_MACRO_AIF_VI,
 		0, aif_vi_mixer, ARRAY_SIZE(aif_vi_mixer)),
 	SND_SOC_DAPM_MIXER("WSA2_AIF_CPS Mixer", SND_SOC_NOPM, LPASS_CDC_WSA2_MACRO_AIF_CPS,
@@ -3851,6 +3907,17 @@ static int lpass_cdc_wsa2_macro_probe(struct platform_device *pdev)
 			__func__, "reg");
 		return ret;
 	}
+	ret = of_property_read_u32(pdev->dev.of_node, "wsa_data_fs_ctl_reg",
+						&wsa2_priv->wsa2_fs_ctl_reg);
+	if (ret) {
+		dev_dbg(&pdev->dev, "%s: error finding %s entry in dt\n",
+			__func__, "wsa_data_fs_ctl_reg");
+	}
+
+	if (!wsa2_priv->wsa2_fs_reg_base && wsa2_priv->wsa2_fs_ctl_reg)
+		wsa2_priv->wsa2_fs_reg_base = devm_ioremap(&pdev->dev,
+			wsa2_priv->wsa2_fs_ctl_reg, LPASS_CDC_WSA2_MACRO_MAX_OFFSET);
+
 	if (of_find_property(pdev->dev.of_node, is_used_wsa2_swr_gpio_dt,
 			     NULL)) {
 		ret = of_property_read_u32(pdev->dev.of_node,

+ 4 - 1
asoc/codecs/wcd-mbhc-v2.c

@@ -1733,7 +1733,7 @@ static int wcd_mbhc_usbc_ana_event_handler(struct notifier_block *nb,
 			mbhc->mbhc_cb->lock_sleep(mbhc, false);
 		}
 #endif
-	} else {
+	} else if (mode < TYPEC_MAX_ACCESSORY) {
 #if IS_ENABLED(CONFIG_QCOM_WCD_USBSS_I2C)
 		WCD_MBHC_REG_READ(WCD_MBHC_L_DET_EN, l_det_en);
 		WCD_MBHC_REG_READ(WCD_MBHC_MECH_DETECTION_TYPE, detection_type);
@@ -1748,6 +1748,9 @@ static int wcd_mbhc_usbc_ana_event_handler(struct notifier_block *nb,
 			dev_dbg(mbhc->component->dev, "leave, %s: mode = %lu\n", __func__, mode);
 		}
 #endif
+	} else if (mode == TYPEC_MAX_ACCESSORY) {
+		if (mbhc->mbhc_cb->surge_reset_routine)
+			mbhc->mbhc_cb->surge_reset_routine(mbhc);
 	}
 	return 0;
 }

+ 82 - 11
asoc/codecs/wcd939x/internal.h

@@ -14,9 +14,14 @@
 #include "wcd939x-mbhc.h"
 #include "wcd939x.h"
 
-#define SWR_SCP_CONTROL    0x44
+#define SWR_SCP_CONTROL 0x44
 #define SWR_SCP_HOST_CLK_DIV2_CTL_BANK 0xE0
 #define WCD939X_MAX_MICBIAS 4
+#define MAX_XTALK_SCALE 31
+#define MIN_XTALK_ALPHA 0
+#define MIN_K_TIMES_100 -90
+#define MAX_K_TIMES_100 10000
+#define MAX_USBCSS_HS_IMPEDANCE_MOHMS 20000
 
 /* Convert from vout ctl to micbias voltage in mV */
 #define  WCD_VOUT_CTL_TO_MICB(v)  (1000 + v * 50)
@@ -157,27 +162,93 @@ struct wcd939x_micbias_setting {
 	u8 bias1_cfilt_sel;
 };
 
-struct wcd939x_xtalk_params {
-	u32 r_gnd_int_fet_mohms;
+struct wcd939x_usbcss_hs_params {
+	/* Resistance of ground-side internal FET for SBU1 */
+	u32 r_gnd_sbu1_int_fet_mohms;
+	/* Resistance of ground-side internal FET for SBU2 */
+	u32 r_gnd_sbu2_int_fet_mohms;
+	/* Customer-characterized resistance for the ground-side external FET */
+	u32 r_gnd_ext_fet_customer_mohms;
+	/* SW-computed resistance for the ground-side external FET */
+	u32 r_gnd_ext_fet_mohms;
+	/* Total ground-side parasitics between the WCD and external FET */
 	u32 r_gnd_par_route1_mohms;
+	/* Total ground-side parasitics between the external FET and connector */
 	u32 r_gnd_par_route2_mohms;
-	u32 r_gnd_ext_fet_mohms;
-	u32 r_conn_par_load_neg_mohms;
+	/* Total ground-side parasitics between the WCD and connector; sum of route1 and route2 */
+	u32 r_gnd_par_tot_mohms;
+	/* Total ground-side resistance for SBU1 */
+	u32 r_gnd_sbu1_res_tot_mohms;
+	/* Total ground-side resistance for SBU2 */
+	u32 r_gnd_sbu2_res_tot_mohms;
+	/* Customer-characterized positive parasitics introduced from the connector */
+	u32 r_conn_par_load_pos_mohms;
+	/* Resistance of left audio-side internal FET */
 	u32 r_aud_int_fet_l_mohms;
+	/* Resistance of right audio-side internal FET */
 	u32 r_aud_int_fet_r_mohms;
+	/* Resistance of left audio-side external FET */
 	u32 r_aud_ext_fet_l_mohms;
+	/* Resistance of right audio-side external FET */
 	u32 r_aud_ext_fet_r_mohms;
-	u32 r_conn_par_load_pos_l_mohms;
-	u32 r_conn_par_load_pos_r_mohms;
-	u32 r_gnd_res_tot_mohms;
+	/* Total left audio-side resistance */
 	u32 r_aud_res_tot_l_mohms;
+	/* Total right audio-side resistance */
 	u32 r_aud_res_tot_r_mohms;
-	u32 zl;
-	u32 zr;
+	/* Surge switch resistance */
+	u32 r_surge_mohms;
+	/* Sum of left audio-side parasitics and the left side of the load */
+	u32 r_load_eff_l_mohms;
+	/* Sum of right audio-side parasitics and the right side of the load */
+	u32 r_load_eff_r_mohms;
+	/* Customer-characterized audio-side parasitics between the WCD and external FET,
+	 * in milliohms
+	 */
+	u32 r3;
+	/* Customer-characterized ground-side parasitics between the external FET and connector,
+	 * in milliohms
+	 */
+	u32 r4;
+	/* For digital crosstalk with remote sensed analog crosstalk mode, customer-characterized
+	 * ground path parasitic resistance between the WCD SBU pin and the external MOSFET,
+	 * in milliohms
+	 */
+	u32 r5;
+	/* For digital crosstalk with local sensed analog crosstalk mode, customer-characterized
+	 * ground path parasitic resistance between the WCD GSBU tap point and the external MOSFET,
+	 * in milliohms
+	 */
+	u32 r6;
+	/* For digital crosstalk with local sensed analog crosstalk mode, customer-characterized
+	 * ground path parasitic resistance between the WCD GSBU tap point and the WCD SBU pin,
+	 * in milliohms
+	 */
+	u32 r7;
+	/* Tap out linearizer constant for the audio path, multiplied by 100 from the original
+	 * constants to support decimal values up to the hundredth place
+	 */
+	s32 k_aud_times_100;
+	/* Tap out linearizer constant for the ground path, multiplied by 100 from the original
+	 * constants to support decimal values up to the hundredth place
+	 */
+	s32 k_gnd_times_100;
+	/* Fixed offset to be applied to audio taps */
+	s32 aud_tap_offset;
+	/* Fixed offset to be applied to ground taps */
+	s32 gnd_tap_offset;
+	/* Computed optimal d-xtalk left-side scale value */
 	u8 scale_l;
+	/* Computed optimal d-xtalk left-side alpha value */
 	u8 alpha_l;
+	/* Computed optimal d-xtalk right-side scale value */
 	u8 scale_r;
+	/* Computed optimal d-xtalk right-side alpha value */
 	u8 alpha_r;
+	/* Customer-tuned configuration for d-xtalk:
+	 * 0 for digital crosstalk disabled,
+	 * 1 for digital crosstalk with local sensed a-xtalk enabled, and
+	 * 2 for digital crosstalk with remote sensed a-xtalk enabled.
+	 */
 	enum xtalk_mode xtalk_config;
 };
 
@@ -186,7 +257,7 @@ struct wcd939x_pdata {
 	struct device_node *rx_slave;
 	struct device_node *tx_slave;
 	struct wcd939x_micbias_setting micbias;
-	struct wcd939x_xtalk_params xtalk;
+	struct wcd939x_usbcss_hs_params usbcss_hs;
 
 	struct cdc_regulator *regulator;
 	int num_supplies;

+ 598 - 78
asoc/codecs/wcd939x/wcd939x-mbhc.c

@@ -1,7 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0-only
 /*
  * Copyright (c) 2018-2019, The Linux Foundation. All rights reserved.
- * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
+ * Copyright (c) 2022-2023, Qualcomm Innovation Center, Inc. All rights reserved.
  */
 #include <linux/module.h>
 #include <linux/init.h>
@@ -21,6 +21,9 @@
 #include <asoc/wcd-mbhc-v2-api.h>
 #include "wcd939x-registers.h"
 #include "internal.h"
+#if IS_ENABLED(CONFIG_QCOM_WCD_USBSS_I2C)
+#include <linux/soc/qcom/wcd939x-i2c.h>
+#endif
 
 #define WCD939X_ZDET_SUPPORTED          true
 /* Z value defined in milliohm */
@@ -36,9 +39,22 @@
 #define WCD939X_MBHC_GET_X1(x)          (x & 0x3FFF)
 /* Z value compared in milliOhm */
 #define WCD939X_MBHC_IS_SECOND_RAMP_REQUIRED(z) false
-#define WCD939X_MBHC_ZDET_CONST         (1018 * 1024)
+#define WCD939X_MBHC_ZDET_CONST         (1071 * 1024)
 #define WCD939X_MBHC_MOISTURE_RREF      R_24_KOHM
 
+#define OHMS_TO_MILLIOHMS 1000
+#define FLOAT_TO_FIXED_XTALK (1UL << 16)
+#define MAX_XTALK_ALPHA 255
+#define MIN_RL_EFF_MOHMS 1
+#define MAX_RL_EFF_MOHMS 900000
+#define HD2_CODE_BASE_VALUE 0x1D
+#define HD2_CODE_INV_RESOLUTION 4201025
+#define FLOAT_TO_FIXED_LINEARIZER (1UL << 12)
+#define MIN_TAP_OFFSET -1023
+#define MAX_TAP_OFFSET 1023
+#define MIN_TAP 0
+#define MAX_TAP 1023
+
 static struct wcd_mbhc_register
 	wcd_mbhc_registers[WCD_MBHC_REG_FUNC_MAX] = {
 	WCD_MBHC_REGISTER("WCD_MBHC_L_DET_EN",
@@ -439,8 +455,8 @@ static void wcd939x_mbhc_zdet_ramp(struct snd_soc_component *component,
 	struct wcd939x_priv *wcd939x = dev_get_drvdata(component->dev);
 	int32_t zdet = 0;
 
-	snd_soc_component_update_bits(component, WCD939X_ZDET_ANA_CTL,
-				0x70, zdet_param->ldo_ctl << 4);
+	snd_soc_component_update_bits(component, WCD939X_ZDET_ANA_CTL, 0xF0,
+				      0x80 | (zdet_param->ldo_ctl << 4));
 	snd_soc_component_update_bits(component, WCD939X_MBHC_BTN5, 0xFC,
 			    zdet_param->btn5);
 	snd_soc_component_update_bits(component, WCD939X_MBHC_BTN6, 0xFC,
@@ -499,38 +515,470 @@ static inline void wcd939x_wcd_mbhc_qfuse_cal(
 		*z_val = ((*z_val) * 10000) / q1_cal;
 }
 
-static void wcd939x_wcd_mbhc_calc_impedance(struct wcd_mbhc *mbhc, uint32_t *zl,
-					  uint32_t *zr)
+static void update_hd2_codes(struct regmap *regmap, u32 r_gnd_res_tot_mohms, u32 r_load_eff)
+{
+	u64 hd2_delta = 0;
+
+	if (!regmap)
+		return;
+	hd2_delta = (HD2_CODE_INV_RESOLUTION * (u64) r_gnd_res_tot_mohms +
+		    FLOAT_TO_FIXED_XTALK * (u64) ((r_gnd_res_tot_mohms + r_load_eff) / 2)) /
+		    (FLOAT_TO_FIXED_XTALK * (u64) (r_gnd_res_tot_mohms + r_load_eff));
+	if (hd2_delta >= HD2_CODE_BASE_VALUE) {
+		regmap_update_bits(regmap, WCD939X_RDAC_HD2_CTL_L, 0x1F, 0x00);
+		regmap_update_bits(regmap, WCD939X_RDAC_HD2_CTL_R, 0x1F, 0x00);
+	} else {
+		regmap_update_bits(regmap, WCD939X_RDAC_HD2_CTL_L, 0x1F,
+				   HD2_CODE_BASE_VALUE - hd2_delta);
+		regmap_update_bits(regmap, WCD939X_RDAC_HD2_CTL_R, 0x1F,
+				   HD2_CODE_BASE_VALUE - hd2_delta);
+	}
+}
+
+static u8 get_xtalk_scale(u32 gain)
+{
+	u8 i;
+	int target, residue;
+
+	if (gain == 0)
+		return MAX_XTALK_SCALE;
+
+	target = FLOAT_TO_FIXED_XTALK / ((int) gain);
+	residue = target;
+
+	for (i = 0; i <= MAX_XTALK_SCALE; i++) {
+		residue = target - (1 << ((int)((u32) i)));
+		if (residue < 0)
+			return i;
+	}
+	return MAX_XTALK_SCALE;
+}
+
+static u8 get_xtalk_alpha(u32 gain, u8 scale)
+{
+	u32 two_exp_scale, round_offset, alpha;
+
+	if (gain == 0)
+		return MIN_XTALK_ALPHA;
+
+	two_exp_scale = 1 << ((u32) scale);
+	round_offset = FLOAT_TO_FIXED_XTALK / 2;
+	alpha = (((gain * two_exp_scale - FLOAT_TO_FIXED_XTALK) * 255) + round_offset)
+		    / FLOAT_TO_FIXED_XTALK;
+	return (alpha <= MAX_XTALK_ALPHA) ? ((u8) alpha) : MAX_XTALK_ALPHA;
+}
+
+static u32 get_v_common_gnd_factor(u32 r_gnd_res_tot_mohms, u32 r_load_eff_mohms,
+				   u32 r_aud_res_tot_mohms)
+{
+	/* Proof 1: The numerator does not overflow.
+	 * r_gnd_res_tot_mohms = r_gnd_int_fet_mohms + r_gnd_ext_fet_mohms + r_gnd_par_tot_mohms =
+	 * r_gnd_int_fet_mohms + r_gnd_ext_fet_mohms + r_gnd_par_route1_mohms +
+	 * r_gnd_par_route2_mohms
+	 *
+	 * r_gnd_int_fet_mohms, r_gnd_ext_fet_mohms, r_gnd_par_route{1,2}_mohms are all less
+	 * than MAX_USBCSS_HS_IMPEDANCE_MOHMS
+	 * -->
+	 * FLOAT_TO_FIXED_XTALK * r_gnd_res_tot_mohms <=
+	 * FLOAT_TO_FIXED_XTALK * 4 * MAX_USBCSS_HS_IMPEDANCE_MOHMS =
+	 * (1 << 16) * 4 * 20,000 = 65,536 * 80,000 = 3,932,160,000 <= 2^32 - 1 =
+	 * 4,294,967,295 = U32_MAX
+	 *
+	 * Proof 2: The denominator is greater than 0.
+	 * r_load_eff_mohms >= MIN_RL_EFF_MOHMS = 1 > 0
+	 * -->
+	 * r_load_eff_mohms + r_aud_res_tot_mohms + r_gnd_res_tot_mohms > 0
+	 *
+	 * Proof 3: The deonominator does not overflow.
+	 * r_load_eff_mohms <= MAX_RL_EFF_MOHMS
+	 * r_aud_res_tot_mohms and r_gnd_res_tot_mohms <= MAX_USBCSS_HS_IMPEDANCE_MOHMS
+	 * -->
+	 * r_load_eff_mohms + r_aud_res_tot_mohms + r_gnd_res_tot_mohms <=
+	 * MAX_RL_EFF_MOHMS + 2 * MAX_USBCSS_HS_IMPEDANCE_MOHMS = 900,000 + 2 * 20,000 = 940,000
+	 * <= U32_MAX = 2^32 - 1 = 4,294,967,295
+	 */
+	return FLOAT_TO_FIXED_XTALK * r_gnd_res_tot_mohms /
+	       (r_load_eff_mohms + r_aud_res_tot_mohms + r_gnd_res_tot_mohms);
+}
+
+static u32 get_v_feedback_tap_factor_digital(u32 r_gnd_int_fet_mohms, u32 r_gnd_par_route1_mohms,
+					    u32 r_load_eff_mohms, u32 r_gnd_res_tot_mohms,
+					    u32 r_aud_res_tot_mohms)
+{
+	/* Proof 4: The numerator does not overflow.
+	 * r_gnd_int_fet_mohms and r_gnd_par_route1_mohms <= MAX_USBCSS_HS_IMPEDANCE_MOHMS
+	 * -->
+	 * FLOAT_TO_FIXED_XTALK * (r_gnd_int_fet_mohms + r_gnd_par_route1_mohms) <=
+	 * FLOAT_TO_FIXED_XTALK * 2 * MAX_USBCSS_HS_IMPEDANCE_MOHMS =
+	 * (1 << 16) * 2 * 20,000 = 65,536 * 40,000 = 2,621,440,000 <= 2^32 - 1 =
+	 * 4,294,967,295 = U32_MAX
+	 *
+	 * The denominator is greater than 0: See Proof 2
+	 * The deonominator does not overflow: See Proof 3
+	 */
+	return FLOAT_TO_FIXED_XTALK * (r_gnd_int_fet_mohms + r_gnd_par_route1_mohms) /
+	       (r_load_eff_mohms + r_gnd_res_tot_mohms + r_aud_res_tot_mohms);
+}
+
+static u32 get_v_feedback_tap_factor_analog(u32 r_gnd_par_route2_mohms, u32 r_load_eff_mohms,
+					    u32 r_gnd_res_tot_mohms, u32 r_aud_res_tot_mohms)
+{
+	/* Proof 5: The numerator does not overflow.
+	 * r_gnd_res_tot_mohms = r_gnd_int_fet_mohms + r_gnd_ext_fet_mohms + r_gnd_par_tot_mohms =
+	 * r_gnd_int_fet_mohms + r_gnd_ext_fet_mohms + r_gnd_par_route1_mohms +
+	 * r_gnd_par_route2_mohms
+	 *
+	 *  r_gnd_res_tot_mohms - r_gnd_par_route2_mohms =
+	 *  r_gnd_int_fet_mohms + r_gnd_ext_fet_mohms + r_gnd_par_route1_mohms
+	 *
+	 * r_gnd_int_fet_mohms, r_gnd_ext_fet_mohms, r_gnd_par_route1_mohms
+	 * <= MAX_USBCSS_HS_IMPEDANCE_MOHMS = 20,000
+	 * -->
+	 * FLOAT_TO_FIXED_XTALK * (r_gnd_int_fet_mohms + r_gnd_ext_fet_mohms +
+	 *			   r_gnd_par_route1_mohms)
+	 * <= FLOAT_TO_FIXED_XTALK * 3 * MAX_USBCSS_HS_IMPEDANCE_MOHMS =
+	 * (1 << 16) * 3 * 20,000 = 65,536 * 60,000 = 3,932,160,000 <= 2^32 - 1 =
+	 * 4,294,967,295 = U32_MAX
+	 *
+	 *  The denominator is greater than 0: See Proof 2
+	 *  The deonominator does not overflow: See Proof 3
+	 */
+	return FLOAT_TO_FIXED_XTALK * (r_gnd_res_tot_mohms - r_gnd_par_route2_mohms) /
+	       (r_load_eff_mohms + r_gnd_res_tot_mohms + r_aud_res_tot_mohms);
+}
+
+static u32 get_xtalk_gain(u32 v_common_gnd_factor, u32 v_feedback_tap_factor)
+{
+	return v_common_gnd_factor - v_feedback_tap_factor;
+}
+
+static void update_xtalk_scale_and_alpha(struct wcd939x_pdata *pdata, struct regmap *regmap)
+{
+	u32 r_gnd_res_tot_mohms = 0, r_gnd_int_fet_mohms = 0, v_common_gnd_factor = 0;
+	u32 v_feedback_tap_factor = 0, xtalk_gain = 0;
+
+	if (!pdata || pdata->usbcss_hs.xtalk_config == XTALK_NONE)
+		return;
+
+	/* Orientation-dependent ground impedance parameters */
+#if IS_ENABLED(CONFIG_QCOM_WCD_USBSS_I2C)
+	if (wcd_usbss_get_sbu_switch_orientation() == GND_SBU2_ORIENTATION_A) {
+		r_gnd_res_tot_mohms = pdata->usbcss_hs.r_gnd_sbu2_res_tot_mohms;
+		r_gnd_int_fet_mohms = pdata->usbcss_hs.r_gnd_sbu2_int_fet_mohms;
+	} else if (wcd_usbss_get_sbu_switch_orientation() == GND_SBU1_ORIENTATION_B) {
+		r_gnd_res_tot_mohms = pdata->usbcss_hs.r_gnd_sbu1_res_tot_mohms;
+		r_gnd_int_fet_mohms = pdata->usbcss_hs.r_gnd_sbu1_int_fet_mohms;
+	} else {
+		pdata->usbcss_hs.scale_l = MAX_XTALK_SCALE;
+		pdata->usbcss_hs.alpha_l = MIN_XTALK_ALPHA;
+		pdata->usbcss_hs.scale_r = MAX_XTALK_SCALE;
+		pdata->usbcss_hs.alpha_r = MIN_XTALK_ALPHA;
+		return;
+	}
+#endif
+
+	/* Recall assumptions about L and R channel impedance parameters being equivalent */
+	/* Xtalk gain calculation */
+	v_common_gnd_factor = get_v_common_gnd_factor(r_gnd_res_tot_mohms,
+						      pdata->usbcss_hs.r_load_eff_l_mohms,
+						      pdata->usbcss_hs.r_aud_res_tot_l_mohms);
+	if (pdata->usbcss_hs.xtalk_config == XTALK_ANALOG) {
+		v_feedback_tap_factor = get_v_feedback_tap_factor_analog(
+						pdata->usbcss_hs.r_gnd_par_route2_mohms,
+						pdata->usbcss_hs.r_load_eff_l_mohms,
+						r_gnd_res_tot_mohms,
+						pdata->usbcss_hs.r_aud_res_tot_l_mohms);
+		/* Update HD2 codes for analog xtalk */
+		update_hd2_codes(regmap, r_gnd_res_tot_mohms, pdata->usbcss_hs.r_load_eff_l_mohms);
+	} else {
+		v_feedback_tap_factor = get_v_feedback_tap_factor_digital(
+						r_gnd_int_fet_mohms,
+						pdata->usbcss_hs.r_gnd_par_route1_mohms,
+						pdata->usbcss_hs.r_load_eff_l_mohms,
+						r_gnd_res_tot_mohms,
+						pdata->usbcss_hs.r_aud_res_tot_l_mohms);
+	}
+	xtalk_gain = get_xtalk_gain(v_common_gnd_factor, v_feedback_tap_factor);
+	/* Store scale and alpha values */
+	pdata->usbcss_hs.scale_l = get_xtalk_scale(xtalk_gain);
+	pdata->usbcss_hs.alpha_l = get_xtalk_alpha(xtalk_gain, pdata->usbcss_hs.scale_l);
+	pdata->usbcss_hs.scale_r = pdata->usbcss_hs.scale_l;
+	pdata->usbcss_hs.alpha_r = pdata->usbcss_hs.alpha_l;
+}
+
+static void update_ext_fet_res(struct wcd939x_pdata *pdata, u32 r_gnd_ext_fet_mohms)
+{
+	if (!pdata)
+		return;
+
+	pdata->usbcss_hs.r_gnd_ext_fet_mohms = (r_gnd_ext_fet_mohms > MAX_USBCSS_HS_IMPEDANCE_MOHMS)
+					       ? MAX_USBCSS_HS_IMPEDANCE_MOHMS
+					       : r_gnd_ext_fet_mohms;
+	pdata->usbcss_hs.r_aud_ext_fet_l_mohms = pdata->usbcss_hs.r_gnd_ext_fet_mohms;
+	pdata->usbcss_hs.r_aud_ext_fet_r_mohms = pdata->usbcss_hs.r_gnd_ext_fet_mohms;
+	pdata->usbcss_hs.r_gnd_sbu1_res_tot_mohms = get_r_gnd_res_tot_mohms(
+							pdata->usbcss_hs.r_gnd_sbu1_int_fet_mohms,
+							pdata->usbcss_hs.r_gnd_ext_fet_mohms,
+							pdata->usbcss_hs.r_gnd_par_tot_mohms);
+	pdata->usbcss_hs.r_gnd_sbu2_res_tot_mohms = get_r_gnd_res_tot_mohms(
+							pdata->usbcss_hs.r_gnd_sbu2_int_fet_mohms,
+							pdata->usbcss_hs.r_gnd_ext_fet_mohms,
+							pdata->usbcss_hs.r_gnd_par_tot_mohms);
+	pdata->usbcss_hs.r_aud_res_tot_l_mohms = get_r_aud_res_tot_mohms(
+							pdata->usbcss_hs.r_aud_int_fet_l_mohms,
+							pdata->usbcss_hs.r_aud_ext_fet_l_mohms);
+	pdata->usbcss_hs.r_aud_res_tot_r_mohms = get_r_aud_res_tot_mohms(
+							pdata->usbcss_hs.r_aud_int_fet_r_mohms,
+							pdata->usbcss_hs.r_aud_ext_fet_r_mohms);
+}
+
+static void get_linearizer_taps(struct wcd939x_pdata *pdata, u32 *aud_tap, u32 *gnd_tap)
+{
+	u32 r_gnd_res_tot_mohms = 0, r_gnd_int_fet_mohms = 0, v_aud1 = 0, v_aud2 = 0;
+	u32 v_gnd_denom = 0, v_gnd1 = 0, v_gnd2 = 0, aud_denom = 0, gnd_denom = 0;
+
+	if (!pdata)
+		goto err_data;
+
+#if IS_ENABLED(CONFIG_QCOM_WCD_USBSS_I2C)
+	/* Orientation-dependent ground impedance parameters */
+	if (wcd_usbss_get_sbu_switch_orientation() == GND_SBU2_ORIENTATION_A) {
+		r_gnd_res_tot_mohms = pdata->usbcss_hs.r_gnd_sbu2_res_tot_mohms;
+		r_gnd_int_fet_mohms = pdata->usbcss_hs.r_gnd_sbu2_int_fet_mohms;
+	} else if (wcd_usbss_get_sbu_switch_orientation() == GND_SBU1_ORIENTATION_B) {
+		r_gnd_res_tot_mohms = pdata->usbcss_hs.r_gnd_sbu1_res_tot_mohms;
+		r_gnd_int_fet_mohms = pdata->usbcss_hs.r_gnd_sbu1_int_fet_mohms;
+	} else {
+		goto err_data;
+	}
+#endif
+
+	/* Proof 6: Neither aud_denom nor gnd_denom is 0 and neither overflows.
+	 * MIN_K_TIMES_100 = -50 <= MAX_K_TIMES_100 <= 10,000 = k_aud_times_100
+	 * -->
+	 * 0 < 410 = 0.1 * 4,096 = 0.1 * FLOAT_TO_FIXED_LINEARIZER < {aud,gnd}_denom <
+	 * 101 * FLOAT_TO_FIXED_LINEARIZER =
+	 * 101 * (1 << 12) < 413,696 <= 4,294,967,295 = U32_MAX
+	 */
+	aud_denom = (u32) (FLOAT_TO_FIXED_LINEARIZER +
+			   (FLOAT_TO_FIXED_LINEARIZER * pdata->usbcss_hs.k_aud_times_100 / 100));
+	gnd_denom = (u32) (FLOAT_TO_FIXED_LINEARIZER +
+			   (FLOAT_TO_FIXED_LINEARIZER * pdata->usbcss_hs.k_gnd_times_100 / 100));
+
+	/* Proof 7: v_aud2 does not overflow.
+	 * MIN_RL_EFF_MOHMS = 1 = <= pdata->usbcss_hs.r_load_eff_l_mohms <= MAX_RL_EFF_MOHMS =
+	 * 900,000
+	 *
+	 * pdata->usbcss_hs.r_gnd_par_tot_mohms = r_gnd_par_route1_mohms + r_gnd_par_route2_mohms
+	 * <= 2 * MAX_USBCSS_HS_IMPEDANCE_MOHMS = 4,0000
+	 *
+	 * r_gnd_int_fet_mohms, pdata->usbcss_hs.r_gnd_ext_fet_mohms, r_gnd_par_route1_mohms,
+	 * r_gnd_par_route2_mohms <= MAX_USBCSS_HS_IMPEDANCE_MOHMS = 20,000
+	 * -->
+	 * 1 <= v_aud2 <= MAX_RL_EFF_MOHMS + 4 * MAX_USBCSS_HS_IMPEDANCE_MOHMS =
+	 * 900,000 + 4 * 20,000 = 980,000 <= 4,294,967,295 = U32_MAX
+	 */
+	v_aud2 = pdata->usbcss_hs.r_load_eff_l_mohms - pdata->usbcss_hs.r3 + r_gnd_int_fet_mohms +
+		 pdata->usbcss_hs.r_gnd_ext_fet_mohms + pdata->usbcss_hs.r_gnd_par_tot_mohms;
+
+	/* Proof 8: v_aud1 does not overflow.
+	 * pdata->usbcss_hs.r_aud_ext_fet_l_mohms <= MAX_USBCSS_HS_IMPEDANCE_MOHMS = 20,000
+	 * From Proof 7,
+	 * 1 <= v_aud2 <= MAX_RL_EFF_MOHMS + 4 * MAX_USBCSS_HS_IMPEDANCE_MOHMS <= S32_MAX
+	 * -->
+	 * 1 <= v_aud1 <= MAX_RL_EFF_MOHMS + 5 * MAX_USBCSS_HS_IMPEDANCE_MOHMS =
+	 * 900,000 + 5 * 20,000 = 1,000,000 <= 2,147,483,647 = S32_MAX
+	 */
+	v_aud1 = v_aud2 + pdata->usbcss_hs.r_aud_ext_fet_l_mohms;
+
+	/* Proof 9: The numerator of v_aud1 does not overflow.
+	 * From Proof 8, v_aud1 was less than or equal to 1,000,000
+	 * Thus, the new v_aud1 numerator is less than or equal to
+	 * FLOAT_TO_FIXED_LINEARIZER * 1,000,000 =
+	 * 4,096 * 1,000,000 = 4,096,000,000 <= 4,294,967,295 = U32_MAX
+	 *
+	 * Proof 10: The denominator of v_aud1 is not 0.
+	 * From Proof 8, v_aud1 was greater than or equal to 1 > 0
+	 *
+	 * Proof 11: The denominator does not overflow.
+	 * From Proof 8, v_aud1 was less than or equal to 1,000,000
+	 * Thus, the new v_aud1 denominator is less than or equal to
+	 * 1,000,000 + pdata->usbcss_hs.r_aud_int_fet_l_mohms = 1,000,000 + 20,000 = 1,020,000 <=
+	 * 4,294,967,295 = U32_MAX
+	 */
+	v_aud1 = FLOAT_TO_FIXED_LINEARIZER * v_aud1 /
+		 (v_aud1 + pdata->usbcss_hs.r_aud_int_fet_l_mohms);
+
+	/* Proof 12: The numerator of v_aud2 does not overflow.
+	 * From Proof 7, v_aud2 was less than or equal to 980,000
+	 * Thus, the new v_aud2 numerator is less than or equal to
+	 * FLOAT_TO_FIXED_LINEARIZER * 980,000 =
+	 * 4,096 * 980,000 = 4,014,080,000 <= 4,294,967,295 = U32_MAX
+	 *
+	 * Proof 13: The denominator of v_aud2 is not 0.
+	 * From Proof 7, v_aud2 was greater than or equal to 1 > 0
+	 *
+	 * Proof 14: The denominator does not overflow.
+	 * From Proof 7, v_aud2 was less than or equal to 980,000
+	 * Thus, the new v_aud2 denominator is less than or equal to
+	 * 980,000 + pdata->usbcss_hs.r_aud_int_fet_l_mohms pdata->usbcss_hs.r_aud_int_fet_l_mohms =
+	 * 980,000 + 20,000 + + 20,000 = 1,020,000 <= 4,294,967,295 = U32_MAX
+	 */
+	v_aud2 = FLOAT_TO_FIXED_LINEARIZER * v_aud2 /
+		 (v_aud2 + pdata->usbcss_hs.r_aud_ext_fet_l_mohms +
+		  pdata->usbcss_hs.r_aud_int_fet_l_mohms);
+
+	/* Proof 15: The numerator of aud_tap does not overflow.
+	 * Looking at the formula for v_aud1 from Proofs 9 to 11, the greatest value of v_aud1 is
+	 * FLOAT_TO_FIXED_LINEARIZER = 4,096
+	 * Looking at the formula for v_aud2 from Proofs 12 to 14, the greatest value of v_aud2 is
+	 * FLOAT_TO_FIXED_LINEARIZER = 4,096
+	 * From Proof 6, aud_denom <= 413,696
+	 * Thus, the numerator <= 1,000 * 4,096 + 10 * 10,000 * 4,096 + 413,696 / 2 =
+	 * 4,096,000 + 409,600,000 + 206,848 = 413,902,848 <= 4,294,967,295 = U32_MAX
+	 *
+	 * Proof 16: The denominator of aud_tap is not 0.
+	 * From Proof 6, aud_denom > 410 > 0
+	 *
+	 * Proof 17: The denominator of aud_tap does not overflow
+	 * From Proof 6, aud_denom <= 413,696 <= 4,294,967,295 = U32_MAX
+	 *
+	 * Proof 18: The result of aud_tap does not overflow.
+	 * From Proof 15, the numerator <= 413,902,848 and from Proof 16, the denominator > 410
+	 * Thus, the divsion will be at most 1,009,519.
+	 * pdata->usbcss_hs.aud_tap_offset <= MAX_TAP_OFFSET = 1,023
+	 * The sum will thus be bounded by 1,009,519 + 1,023 = 1,010,542 <= 2,147,483,647 = S32_MAX
+	 * Note: aud_tap won't underflow either since pdata->usbcss_hs.aud_tap_offset >= -1,023
+	 */
+	*aud_tap = (u32) ((s32) ((1000 * v_aud1 + 10 * pdata->usbcss_hs.k_aud_times_100 * v_aud2
+				  + aud_denom / 2) / aud_denom) + pdata->usbcss_hs.aud_tap_offset);
+	if (*aud_tap > MAX_TAP)
+		*aud_tap = MAX_TAP;
+	else if (*aud_tap < MIN_TAP)
+		*aud_tap = MIN_TAP;
+
+	/* Proof 19: v_gnd_denom does not overflow.
+	 * r_gnd_res_tot_mohms = r_gnd_int_fet_mohms + r_gnd_ext_fet_mohms + r_gnd_par_tot_mohms
+	 *
+	 * r_gnd_int_fet_mohms, r_gnd_ext_fet_mohms, r_gnd_par_tot_mohms,
+	 * pdata->usbcss_hs.r_aud_ext_fet_l_mohms, pdata->usbcss_hs.r_aud_int_fet_l_mohms are all
+	 * <= MAX_USBCSS_HS_IMPEDANCE_MOHMS = 20,000
+	 *
+	 * pdata->usbcss_hs.r_load_eff_l_mohms <= MAX_RL_EFF_MOHMS = 900,000
+	 *
+	 * --> v_gnd_denom <= 3 * 20,000 + 900,000 + 2 * 20,000 = 60,000 + 900,000 + 40,000 =
+	 * 1,000,000 <= 4,294,967,295 = U32_MAX
+	 *
+	 * Proof 20: v_gnd_denom is not 0.
+	 * pdata->usbcss_hs.r_load_eff_l_mohms >= MIN_RL_EFF_MOHMS = 1
+	 * -->  v_gnd_denom >= 1 > 0
+	 */
+
+	v_gnd_denom = (r_gnd_res_tot_mohms + pdata->usbcss_hs.r_load_eff_l_mohms -
+		       pdata->usbcss_hs.r3 + pdata->usbcss_hs.r_aud_ext_fet_l_mohms +
+		       pdata->usbcss_hs.r_aud_int_fet_l_mohms);
+
+	/* Proof 21: v_gnd1 numerator does not overflow.
+	 * r_gnd_int_fet_mohms <= MAX_USBCSS_HS_IMPEDANCE_MOHMS = 20,000
+	 * --> v_gnd1 numerator <= 4,096 * 20,000 = 81,920,000 <= 4,294,967,295 = U32_MAX
+	 *
+	 * v_gnd1 denominator is not 0: See Proof 20
+	 * v_gnd1 denominator does not overflow: See Proof 19
+	 */
+	v_gnd1 = FLOAT_TO_FIXED_LINEARIZER * r_gnd_int_fet_mohms / v_gnd_denom;
+
+	/* Proof 22: v_gnd2 numerator does not overflow.
+	 * r_gnd_int_fet_mohms <= MAX_USBCSS_HS_IMPEDANCE_MOHMS = 20,000
+	 * pdata->usbcss_hs.r_load_eff_l_mohms <= MAX_RL_EFF_MOHMS = 900,000
+	 * --> v_gnd2 numerator <= 4,096 * (20,000 + 900,000) = 4,096 * 920,000 = 3,768,320,000
+	 * <= 4,294,967,295 = U32_MAX
+	 *
+	 * v_gnd2 denominator is not 0: See Proof 20
+	 * v_gnd2 denominator does not overflow: See Proof 19
+	 */
+	v_gnd2 = FLOAT_TO_FIXED_LINEARIZER * (r_gnd_int_fet_mohms +
+					      pdata->usbcss_hs.r_gnd_ext_fet_mohms) / v_gnd_denom;
+
+	/* Proof 23: The numerator of gnd_tap does not overflow.
+	 * Looking at the formula for v_gnd1 from Proof 21, and considering that
+	 * r_gnd_res_tot_mohms = r_gnd_int_fet_mohms + r_gnd_ext_fet_mohms + r_gnd_par_tot_mohms,
+	 * the greatest value of v_gnd1 is FLOAT_TO_FIXED_LINEARIZER = 4,096.
+	 * Looking at the formula for v_aud2 from Proof 22 and again at the definintion of
+	 * r_gnd_res_tot_mohms, the greatest value of v_gnd2 is FLOAT_TO_FIXED_LINEARIZER = 4,096
+	 * From Proof 6, gnd_denom <= 413,696
+	 * Thus, the numerator <= 1,000 * 4,096 + 10 * 10,000 * 4,096 + 413,696 / 2 =
+	 * 4,096,000 + 409,600,000 + 206,848 = 413,902,848 <= 4,294,967,295 = U32_MAX
+	 *
+	 * Proof 24: The denominator of gnd_tap is not 0.
+	 * From Proof 6, gnd_denom > 410 > 0
+	 *
+	 * Proof 25: The denominator of gnd_tap does not overflow
+	 * From Proof 6, gnd_denom <= 413,696 <= 4,294,967,295 = U32_MAX
+	 *
+	 * Proof 26: The result of aud_tap does not overflow.
+	 * From Proof 15, the numerator <= 413,902,848 and from Proof 16, the denominator > 410
+	 * Thus, the divsion will be at most 1,009,519.
+	 * pdata->usbcss_hs.aud_tap_offset <= MAX_TAP_OFFSET = 1,023
+	 * The sum will thus be bounded by 1,009,519 + 1,023 = 1,010,542 <= 2,147,483,647 = S32_MAX
+	 * Note: gnd_tap won't underflow either since pdata->usbcss_hs.aud_tap_offset >= -1,023
+	 */
+	*gnd_tap = (u32) ((s32) ((1000 * v_gnd1 + 10 * pdata->usbcss_hs.k_gnd_times_100 * v_gnd2
+				  + gnd_denom / 2) / gnd_denom) + pdata->usbcss_hs.gnd_tap_offset);
+	if (*gnd_tap > MAX_TAP)
+		*gnd_tap = MAX_TAP;
+	else if (*gnd_tap < MIN_TAP)
+		*gnd_tap = MIN_TAP;
+	return;
+
+err_data:
+	*aud_tap = 0;
+	*gnd_tap = 0;
+}
+
+static void wcd939x_wcd_mbhc_calc_impedance(struct wcd_mbhc *mbhc, uint32_t *zl, uint32_t *zr)
 {
 	struct snd_soc_component *component = mbhc->component;
 	struct wcd939x_priv *wcd939x = dev_get_drvdata(component->dev);
+	struct wcd939x_pdata *pdata = dev_get_platdata(wcd939x->dev);
 	s16 reg0, reg1, reg2, reg3, reg4;
-	int32_t z1L, z1R, z1Ls;
+	uint32_t zdiff_val = 0, r_gnd_int_fet_mohms = 0, rl_eff_mohms = 0, r_gnd_ext_fet_mohms = 0;
+	uint32_t aud_tap = 0, gnd_tap = 0;
+	uint32_t *zdiff = &zdiff_val;
+	int32_t z1L, z1R, z1Ls, z1Diff;
 	int zMono, z_diff1, z_diff2;
 	bool is_fsm_disable = false;
-	struct wcd939x_mbhc_zdet_param zdet_param[] = {
-		{4, 0, 4, 0x08, 0x14, 0x18}, /* < 32ohm */
-		{4, 0, 6, 0x18, 0x60, 0x78}, /* 32ohm < Z < 400ohm */
-		{1, 4, 5, 0x18, 0x7C, 0x90}, /* 400ohm < Z < 1200ohm */
-		{1, 6, 7, 0x18, 0x7C, 0x90}, /* >1200ohm */
-	};
-	struct wcd939x_mbhc_zdet_param *zdet_param_ptr = NULL;
-	s16 d1_a[][4] = {
-		{0, 30, 90, 30},
-		{0, 30, 30, 6},
-		{0, 30, 30, 5},
-		{0, 30, 30, 5},
-	};
-	s16 *d1 = NULL;
+	struct wcd939x_mbhc_zdet_param zdet_param = {4, 0, 6, 0x18, 0x60, 0x78};
+	struct wcd939x_mbhc_zdet_param *zdet_param_ptr = &zdet_param;
+	s16 d1[] = {0, 30, 30, 6};
 
 	WCD_MBHC_RSC_ASSERT_LOCKED(mbhc);
 
+	/* Turn on RX supplies */
+	if (wcd939x->version == WCD939X_VERSION_2_0) {
+		/* Start up Buck/Flyback, Enable RX bias, Use MBHC RCO for MBHC Zdet, Enable Vneg */
+		regmap_update_bits(wcd939x->regmap, WCD939X_ZDET_VNEG_CTL, 0x4C, 0x4C);
+		/* Wait 100us for settling */
+		usleep_range(100, 110);
+		/* Enable VNEGDAC_LDO */
+		regmap_update_bits(wcd939x->regmap, WCD939X_ZDET_VNEG_CTL, 0x10, 0x10);
+		/* Wait 100us for settling */
+		usleep_range(100, 110);
+		/* Keep PA left/right channels disabled */
+		regmap_update_bits(wcd939x->regmap, WCD939X_ZDET_VNEG_CTL, 0x01, 0x01);
+		/* Enable VPOS */
+		regmap_update_bits(wcd939x->regmap, WCD939X_ZDET_VNEG_CTL, 0x20, 0x20);
+		/* Wait 500us for settling */
+		usleep_range(500, 510);
+	}
+
+	/* Store register values */
 	reg0 = snd_soc_component_read(component, WCD939X_MBHC_BTN5);
 	reg1 = snd_soc_component_read(component, WCD939X_MBHC_BTN6);
 	reg2 = snd_soc_component_read(component, WCD939X_MBHC_BTN7);
 	reg3 = snd_soc_component_read(component, WCD939X_CTL_CLK);
 	reg4 = snd_soc_component_read(component, WCD939X_ZDET_ANA_CTL);
 
+	/* Disable the detection FSM */
 	if (snd_soc_component_read(component, WCD939X_MBHC_ELECT) & 0x80) {
 		is_fsm_disable = true;
 		regmap_update_bits(wcd939x->regmap,
@@ -554,76 +1002,113 @@ static void wcd939x_wcd_mbhc_calc_impedance(struct wcd_mbhc *mbhc, uint32_t *zl,
 	/* 1ms delay needed after disable surge protection */
 	usleep_range(1000, 1010);
 
-	/* First get impedance on Left */
-	d1 = d1_a[1];
-	zdet_param_ptr = &zdet_param[1];
-	wcd939x_mbhc_zdet_ramp(component, zdet_param_ptr, &z1L, NULL, d1);
-
-	if (!WCD939X_MBHC_IS_SECOND_RAMP_REQUIRED(z1L))
-		goto left_ch_impedance;
-
-	/* Second ramp for left ch */
-	if (z1L < WCD939X_ZDET_VAL_32) {
-		zdet_param_ptr = &zdet_param[0];
-		d1 = d1_a[0];
-	} else if ((z1L > WCD939X_ZDET_VAL_400) &&
-		  (z1L <= WCD939X_ZDET_VAL_1200)) {
-		zdet_param_ptr = &zdet_param[2];
-		d1 = d1_a[2];
-	} else if (z1L > WCD939X_ZDET_VAL_1200) {
-		zdet_param_ptr = &zdet_param[3];
-		d1 = d1_a[3];
+#if IS_ENABLED(CONFIG_QCOM_WCD_USBSS_I2C)
+	/* Disable sense switch and MIC for USB-C analog platforms */
+	if (mbhc->mbhc_cfg->enable_usbc_analog) {
+		wcd_usbss_set_switch_settings_enable(SENSE_SWITCHES, USBSS_SWITCH_DISABLE);
+		wcd_usbss_set_switch_settings_enable(MIC_SWITCHES, USBSS_SWITCH_DISABLE);
 	}
-	wcd939x_mbhc_zdet_ramp(component, zdet_param_ptr, &z1L, NULL, d1);
+#endif
 
-left_ch_impedance:
-	if ((z1L == WCD939X_ZDET_FLOATING_IMPEDANCE) ||
-		(z1L > WCD939X_ZDET_VAL_100K)) {
+	/* L-channel impedance */
+	wcd939x_mbhc_zdet_ramp(component, zdet_param_ptr, &z1L, NULL, d1);
+	if ((z1L == WCD939X_ZDET_FLOATING_IMPEDANCE) || (z1L > WCD939X_ZDET_VAL_100K)) {
 		*zl = WCD939X_ZDET_FLOATING_IMPEDANCE;
-		zdet_param_ptr = &zdet_param[1];
-		d1 = d1_a[1];
 	} else {
-		*zl = z1L/1000;
+		*zl = z1L;
 		wcd939x_wcd_mbhc_qfuse_cal(component, zl, 0);
 	}
-	dev_dbg(component->dev, "%s: impedance on HPH_L = %d(ohms)\n",
+	/* Differential measurement for USB-C analog platforms */
+	if (mbhc->mbhc_cfg->enable_usbc_analog) {
+		dev_dbg(component->dev, "%s: effective impedance on HPH_L = %d(mohms)\n",
+			__func__, *zl);
+		goto diff_impedance;
+	}
+	dev_dbg(component->dev, "%s: impedance on HPH_L = %d(mohms)\n",
 		__func__, *zl);
 
-	/* Start of right impedance ramp and calculation */
+	/* R-channel impedance */
 	wcd939x_mbhc_zdet_ramp(component, zdet_param_ptr, NULL, &z1R, d1);
-	if (WCD939X_MBHC_IS_SECOND_RAMP_REQUIRED(z1R)) {
-		if (((z1R > WCD939X_ZDET_VAL_1200) &&
-			(zdet_param_ptr->noff == 0x6)) ||
-			((*zl) != WCD939X_ZDET_FLOATING_IMPEDANCE))
-			goto right_ch_impedance;
-		/* Second ramp for right ch */
-		if (z1R < WCD939X_ZDET_VAL_32) {
-			zdet_param_ptr = &zdet_param[0];
-			d1 = d1_a[0];
-		} else if ((z1R > WCD939X_ZDET_VAL_400) &&
-			(z1R <= WCD939X_ZDET_VAL_1200)) {
-			zdet_param_ptr = &zdet_param[2];
-			d1 = d1_a[2];
-		} else if (z1R > WCD939X_ZDET_VAL_1200) {
-			zdet_param_ptr = &zdet_param[3];
-			d1 = d1_a[3];
-		}
-		wcd939x_mbhc_zdet_ramp(component, zdet_param_ptr, NULL, &z1R, d1);
-	}
-right_ch_impedance:
-	if ((z1R == WCD939X_ZDET_FLOATING_IMPEDANCE) ||
-		(z1R > WCD939X_ZDET_VAL_100K)) {
+	if ((z1R == WCD939X_ZDET_FLOATING_IMPEDANCE) || (z1R > WCD939X_ZDET_VAL_100K)) {
 		*zr = WCD939X_ZDET_FLOATING_IMPEDANCE;
 	} else {
-		*zr = z1R/1000;
+		*zr = z1R;
 		wcd939x_wcd_mbhc_qfuse_cal(component, zr, 1);
 	}
-	dev_dbg(component->dev, "%s: impedance on HPH_R = %d(ohms)\n",
+	dev_dbg(component->dev, "%s: impedance on HPH_R = %d(mohms)\n",
 		__func__, *zr);
-
+		/* Convert from mohms to ohms (rounded) */
+	*zl = (*zl + OHMS_TO_MILLIOHMS / 2) / OHMS_TO_MILLIOHMS;
+	*zr = (*zr + OHMS_TO_MILLIOHMS / 2) / OHMS_TO_MILLIOHMS;
+	goto mono_stereo_detection;
+
+diff_impedance:
+#if IS_ENABLED(CONFIG_QCOM_WCD_USBSS_I2C)
+	/* Disable AGND switch */
+	wcd_usbss_set_switch_settings_enable(AGND_SWITCHES, USBSS_SWITCH_DISABLE);
+#endif
+	/* Enable HPHR NCLAMP */
+	regmap_update_bits(wcd939x->regmap, WCD939X_HPHLR_SURGE_MISC1, 0x08, 0x08);
+
+	/* Diffrential impedance */
+	wcd939x_mbhc_zdet_ramp(component, zdet_param_ptr, &z1Diff, NULL, d1);
+	if ((z1Diff == WCD939X_ZDET_FLOATING_IMPEDANCE) || (z1Diff > WCD939X_ZDET_VAL_100K)) {
+		*zdiff = WCD939X_ZDET_FLOATING_IMPEDANCE;
+	} else {
+		*zdiff = z1Diff;
+		wcd939x_wcd_mbhc_qfuse_cal(component, zdiff, 0);
+	}
+	dev_dbg(component->dev, "%s: effective impedance on HPH_diff after calib = %d(mohms)\n",
+		__func__, *zdiff);
+	/* Disable HPHR NCLAMP */
+	regmap_update_bits(wcd939x->regmap, WCD939X_HPHLR_SURGE_MISC1, 0x08, 0x00);
+#if IS_ENABLED(CONFIG_QCOM_WCD_USBSS_I2C)
+	/* Enable AGND switch */
+	wcd_usbss_set_switch_settings_enable(AGND_SWITCHES, USBSS_SWITCH_ENABLE);
+	/* Get ground internal resistance based on orientation */
+	if (wcd_usbss_get_sbu_switch_orientation() == GND_SBU2_ORIENTATION_A) {
+		r_gnd_int_fet_mohms = pdata->usbcss_hs.r_gnd_sbu2_int_fet_mohms;
+	} else if (wcd_usbss_get_sbu_switch_orientation() == GND_SBU1_ORIENTATION_B) {
+		r_gnd_int_fet_mohms = pdata->usbcss_hs.r_gnd_sbu1_int_fet_mohms;
+	} else {
+		*zl = 0;
+		*zr = 0;
+		dev_dbg(component->dev, "%s: Invalid SBU switch orientation\n", __func__);
+		goto zdet_complete;
+	}
+#endif
+	/* Compute external fet and effective load impedance */
+	r_gnd_ext_fet_mohms = *zl - *zdiff / 2 + pdata->usbcss_hs.r_surge_mohms / 2 -
+			      pdata->usbcss_hs.r_gnd_par_tot_mohms - r_gnd_int_fet_mohms;
+	rl_eff_mohms = *zdiff / 2 - pdata->usbcss_hs.r_aud_int_fet_r_mohms -
+		       pdata->usbcss_hs.r_gnd_ext_fet_mohms - pdata->usbcss_hs.r_surge_mohms / 2 -
+		       pdata->usbcss_hs.r_gnd_par_tot_mohms;
+	/* Store values */
+	*zl = (rl_eff_mohms - pdata->usbcss_hs.r_conn_par_load_pos_mohms - pdata->usbcss_hs.r3 +
+		   OHMS_TO_MILLIOHMS / 2) / OHMS_TO_MILLIOHMS;
+	*zr = *zl;
+
+	/* Update USBC-SS HS params */
+	if (rl_eff_mohms > MAX_RL_EFF_MOHMS)
+		rl_eff_mohms = MAX_RL_EFF_MOHMS;
+	else if (rl_eff_mohms == 0)
+		rl_eff_mohms = MIN_RL_EFF_MOHMS;
+	pdata->usbcss_hs.r_load_eff_l_mohms = rl_eff_mohms;
+	pdata->usbcss_hs.r_load_eff_r_mohms = rl_eff_mohms;
+	update_ext_fet_res(pdata, r_gnd_ext_fet_mohms);
+	update_xtalk_scale_and_alpha(pdata, wcd939x->regmap);
+	dev_dbg(component->dev, "%s: Xtalk scale is 0x%x and alpha is 0x%x\n",
+		__func__, pdata->usbcss_hs.scale_l, pdata->usbcss_hs.alpha_l);
+	get_linearizer_taps(pdata, &aud_tap, &gnd_tap);
+#if IS_ENABLED(CONFIG_QCOM_WCD_USBSS_I2C)
+	wcd_usbss_set_linearizer_sw_tap(aud_tap, gnd_tap);
+#endif
+	dev_dbg(component->dev, "%s: Linearizer aud_tap is 0x%x and gnd_tap is 0x%x\n",
+		__func__, aud_tap, gnd_tap);
+
+mono_stereo_detection:
 	/* Mono/stereo detection */
-	if ((*zl == WCD939X_ZDET_FLOATING_IMPEDANCE) &&
-		(*zr == WCD939X_ZDET_FLOATING_IMPEDANCE)) {
+	if ((*zl == WCD939X_ZDET_FLOATING_IMPEDANCE) && (*zr == WCD939X_ZDET_FLOATING_IMPEDANCE)) {
 		dev_dbg(component->dev,
 			"%s: plug type is invalid or extension cable\n",
 			__func__);
@@ -641,7 +1126,7 @@ right_ch_impedance:
 	}
 	snd_soc_component_update_bits(component, WCD939X_R_ATEST, 0x02, 0x02);
 	snd_soc_component_update_bits(component, WCD939X_PA_CTL2, 0x40, 0x01);
-	wcd939x_mbhc_zdet_ramp(component, &zdet_param[1], &z1Ls, NULL, d1);
+	wcd939x_mbhc_zdet_ramp(component, zdet_param_ptr, &z1Ls, NULL, d1);
 	snd_soc_component_update_bits(component, WCD939X_PA_CTL2, 0x40, 0x00);
 	snd_soc_component_update_bits(component, WCD939X_R_ATEST, 0x02, 0x00);
 	z1Ls /= 1000;
@@ -660,10 +1145,18 @@ right_ch_impedance:
 		mbhc->hph_type = WCD_MBHC_HPH_MONO;
 	}
 
+zdet_complete:
+#if IS_ENABLED(CONFIG_QCOM_WCD_USBSS_I2C)
+	/* Enable sense switch and MIC for USB-C analog platforms */
+	if (mbhc->mbhc_cfg->enable_usbc_analog) {
+		wcd_usbss_set_switch_settings_enable(SENSE_SWITCHES, USBSS_SWITCH_ENABLE);
+		wcd_usbss_set_switch_settings_enable(MIC_SWITCHES, USBSS_SWITCH_ENABLE);
+	}
+#endif
 	/* Enable surge protection again after impedance detection */
 	regmap_update_bits(wcd939x->regmap,
 			   WCD939X_HPHLR_SURGE_EN, 0xC0, 0xC0);
-zdet_complete:
+
 	snd_soc_component_write(component, WCD939X_MBHC_BTN5, reg0);
 	snd_soc_component_write(component, WCD939X_MBHC_BTN6, reg1);
 	snd_soc_component_write(component, WCD939X_MBHC_BTN7, reg2);
@@ -681,6 +1174,24 @@ zdet_complete:
 	if (is_fsm_disable)
 		regmap_update_bits(wcd939x->regmap,
 				   WCD939X_MBHC_ELECT, 0x80, 0x80);
+
+	/* Turn off RX supplies */
+	if (wcd939x->version == WCD939X_VERSION_2_0) {
+		/* Set VPOS to be controlled by RX */
+		regmap_update_bits(wcd939x->regmap, WCD939X_ZDET_VNEG_CTL, 0x20, 0x00);
+		/* Wait 500us for settling */
+		usleep_range(500, 510);
+		/* Set PA Left/Right channels and VNEGDAC_LDO to be controlled by RX */
+		regmap_update_bits(wcd939x->regmap, WCD939X_ZDET_VNEG_CTL, 0x11, 0x00);
+		/* Wait 100us for settling */
+		usleep_range(100, 110);
+		/* Set Vneg mode and enable to be controlled by RX */
+		regmap_update_bits(wcd939x->regmap, WCD939X_ZDET_VNEG_CTL, 0x06, 0x00);
+		/* Wait 100us for settling */
+		usleep_range(100, 110);
+		/* Set RX bias to be controlled by RX and set Buck/Flyback back to SWR Rx clock */
+		regmap_update_bits(wcd939x->regmap, WCD939X_ZDET_VNEG_CTL, 0x48, 0x00);
+	}
 }
 
 static void wcd939x_mbhc_gnd_det_ctrl(struct snd_soc_component *component,
@@ -808,6 +1319,14 @@ static void wcd939x_mbhc_bcs_enable(struct wcd_mbhc *mbhc,
 		wcd939x_disable_bcs_before_slow_insert(mbhc->component, true);
 }
 
+static void wcd939x_surge_reset_routine(struct wcd_mbhc *mbhc)
+{
+	struct wcd939x_priv *wcd939x = snd_soc_component_get_drvdata(mbhc->component);
+
+	regcache_mark_dirty(wcd939x->regmap);
+	regcache_sync(wcd939x->regmap);
+}
+
 static const struct wcd_mbhc_cb mbhc_cb = {
 	.request_irq = wcd939x_mbhc_request_irq,
 	.irq_control = wcd939x_mbhc_irq_control,
@@ -833,6 +1352,7 @@ static const struct wcd_mbhc_cb mbhc_cb = {
 	.mbhc_moisture_polling_ctrl = wcd939x_mbhc_moisture_polling_ctrl,
 	.mbhc_moisture_detect_en = wcd939x_mbhc_moisture_detect_en,
 	.bcs_enable = wcd939x_mbhc_bcs_enable,
+	.surge_reset_routine = wcd939x_surge_reset_routine,
 };
 
 static int wcd939x_get_hph_type(struct snd_kcontrol *kcontrol,

+ 12 - 1
asoc/codecs/wcd939x/wcd939x-mbhc.h

@@ -1,7 +1,7 @@
 /* SPDX-License-Identifier: GPL-2.0-only */
 /*
  * Copyright (c) 2018-2019, The Linux Foundation. All rights reserved.
- * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
+ * Copyright (c) 2022-2023, Qualcomm Innovation Center, Inc. All rights reserved.
  */
 #ifndef __WCD939X_MBHC_H__
 #define __WCD939X_MBHC_H__
@@ -13,6 +13,17 @@ struct wcd939x_mbhc {
 	struct fw_info *fw_data;
 };
 
+static inline u32 get_r_gnd_res_tot_mohms(u32 r_gnd_int_fet_mohms, u32 r_gnd_ext_fet_mohms,
+					  u32 r_gnd_par_tot_mohms)
+{
+	return r_gnd_int_fet_mohms + r_gnd_ext_fet_mohms + r_gnd_par_tot_mohms;
+}
+
+static inline u32 get_r_aud_res_tot_mohms(u32 r_aud_int_fet_mohms, u32 r_aud_ext_fet_mohms)
+{
+	return r_aud_int_fet_mohms + r_aud_ext_fet_mohms;
+}
+
 #if IS_ENABLED(CONFIG_SND_SOC_WCD939X)
 extern int wcd939x_mbhc_init(struct wcd939x_mbhc **mbhc,
 			   struct snd_soc_component *component,

+ 229 - 297
asoc/codecs/wcd939x/wcd939x.c

@@ -47,21 +47,14 @@
 #define ADC_MODE_VAL_ULP1     0x09
 #define ADC_MODE_VAL_ULP2     0x0B
 
-#define HPH_IMPEDANCE_2VPK_MODE_OHMS 300
+#define HPH_IMPEDANCE_2VPK_MODE_OHMS 260
+#define XTALK_L_CH_NUM 0
+#define XTALK_R_CH_NUM 1
 
 #define NUM_ATTEMPTS 5
 #define COMP_MAX_COEFF 25
 #define HPH_MODE_MAX 4
 
-#define FLOAT_TO_FIXED (1 << 12)
-#define MAX_XTALK_SCALE 31
-#define MAX_XTALK_ALPHA 255
-#define MAX_RLOAD_OHMS 1000
-#define MAX_IMPEDANCE_MOHMS 20000
-#define OHMS_TO_MILLIOHMS 1000
-#define XTALK_L_CH_NUM 0
-#define XTALK_R_CH_NUM 1
-
 #define DAPM_MICBIAS1_STANDALONE "MIC BIAS1 Standalone"
 #define DAPM_MICBIAS2_STANDALONE "MIC BIAS2 Standalone"
 #define DAPM_MICBIAS3_STANDALONE "MIC BIAS3 Standalone"
@@ -120,6 +113,12 @@ enum {
 	ADC_MODE_ULP2,
 };
 
+enum {
+	SUPPLY_LEVEL_2VPK,
+	REGULATOR_MODE_2VPK,
+	SET_HPH_GAIN_2VPK,
+};
+
 static u8 tx_mode_bit[] = {
 	[ADC_MODE_INVALID] = 0x00,
 	[ADC_MODE_HIFI] = 0x01,
@@ -131,7 +130,7 @@ static u8 tx_mode_bit[] = {
 };
 
 extern const u8 wcd939x_reg_access[WCD939X_NUM_REGISTERS];
-static const DECLARE_TLV_DB_SCALE(line_gain, 0, 7, 1);
+static const SNDRV_CTL_TLVD_DECLARE_DB_MINMAX(hph_analog_gain, 600, -3000);
 static const DECLARE_TLV_DB_SCALE(analog_gain, 0, 25, 1);
 
 /* Will be set by reading the registers during bind()*/
@@ -141,6 +140,8 @@ static int wcd939x_handle_post_irq(void *data);
 static int wcd939x_reset(struct device *dev);
 static int wcd939x_reset_low(struct device *dev);
 static int wcd939x_get_adc_mode(int val);
+static void wcd939x_config_2Vpk_mode(struct snd_soc_component *component,
+			struct wcd939x_priv *wcd939x, int mode_2vpk);
 
 static const struct regmap_irq wcd939x_irqs[WCD939X_NUM_IRQS] = {
 	REGMAP_IRQ_REG(WCD939X_IRQ_MBHC_BUTTON_PRESS_DET, 0, 0x01),
@@ -972,17 +973,15 @@ static int wcd939x_config_compander(struct snd_soc_component *component,
 			gain_source_sel = 0x01;
 		else
 			gain_source_sel = 0x00;
+
 		if (compander_indx == WCD939X_HPHL) {
 			snd_soc_component_update_bits(component,
 				REG_FIELD_VALUE(L_EN, GAIN_SOURCE_SEL, gain_source_sel));
-			snd_soc_component_update_bits(component,
-				REG_FIELD_VALUE(PA_GAIN_CTL_L, PA_GAIN_L, 0x04));
 		} else if (compander_indx == WCD939X_HPHR) {
 			snd_soc_component_update_bits(component,
 				REG_FIELD_VALUE(R_EN, GAIN_SOURCE_SEL, gain_source_sel));
-			snd_soc_component_update_bits(component,
-				REG_FIELD_VALUE(PA_GAIN_CTL_R, PA_GAIN_R, 0x04));
 		}
+		wcd939x_config_2Vpk_mode(component, wcd939x, SET_HPH_GAIN_2VPK);
 		return 0;
 	}
 
@@ -1031,180 +1030,12 @@ static int wcd939x_config_compander(struct snd_soc_component *component,
 	return 0;
 }
 
-static u8 get_xtalk_scale(u32 gain)
-{
-	u8 i;
-	int target = FLOAT_TO_FIXED / ((int) gain);
-	int residue = target;
-
-	for (i = 0; i <= MAX_XTALK_SCALE; i++) {
-		residue = target - (1 << ((int)((u32) i)));
-		if (residue <= 0)
-			return i;
-	}
-	return MAX_XTALK_SCALE;
-}
-
-static u8 get_xtalk_alpha(u32 gain, u8 scale)
-{
-	u32 two_exp_scale = 1 << ((u32) scale);
-	u32 round_offset = FLOAT_TO_FIXED / 2;
-	u32 alpha = (((gain * two_exp_scale - FLOAT_TO_FIXED) * 255) + round_offset)
-		    / FLOAT_TO_FIXED;
-	return (alpha <= MAX_XTALK_ALPHA) ? ((u8) alpha) : MAX_XTALK_ALPHA;
-}
-
-static u32 get_r_gnd_res_tot_mohms(u32 r_gnd_int_fet_mohms, u32 r_gnd_par_route1_mohms,
-				   u32 r_gnd_par_route2_mohms, u32 r_gnd_ext_fet_mohms,
-				   u32 r_conn_par_load_neg_mohms)
-{
-	return r_gnd_int_fet_mohms + r_gnd_par_route1_mohms + r_gnd_par_route2_mohms +
-	       r_gnd_ext_fet_mohms + r_conn_par_load_neg_mohms;
-}
-
-static u32 get_r_aud_res_tot_mohms(u32 r_aud_int_fet_mohms, u32 r_aud_ext_fet_mohms,
-				   u32 r_conn_par_load_pos_mohms)
-{
-	return r_aud_int_fet_mohms + r_aud_ext_fet_mohms + r_conn_par_load_pos_mohms;
-}
-
-static u32 get_v_common_gnd_factor(u32 r_gnd_res_tot_mohms, u32 r_load_mohms,
-				   u32 r_aud_res_tot_mohms)
-{
-	return FLOAT_TO_FIXED * r_gnd_res_tot_mohms /
-	       (r_load_mohms + r_aud_res_tot_mohms + r_gnd_res_tot_mohms);
-}
-
-static u32 get_v_feedback_tap_factor(u32 r_gnd_int_fet_mohms, u32 r_gnd_par_route1_mohms,
-				     u32 r_load_mohms, u32 r_gnd_res_tot_mohms,
-				     u32 r_aud_res_tot_mohms)
-{
-	return FLOAT_TO_FIXED * (r_gnd_int_fet_mohms + r_gnd_par_route1_mohms) /
-	       (r_load_mohms + r_gnd_res_tot_mohms + r_aud_res_tot_mohms);
-}
-
-static u32 get_v_feedback_tap_factor_analog(u32 r_conn_par_load_neg_mohms, u32 r_load_mohms,
-					    u32 r_gnd_res_tot_mohms, u32 r_aud_res_tot_mohms)
-{
-	return FLOAT_TO_FIXED * (r_gnd_res_tot_mohms - r_conn_par_load_neg_mohms) /
-	       (r_load_mohms + r_gnd_res_tot_mohms + r_aud_res_tot_mohms);
-}
-
-static u32 get_xtalk_gain(u32 v_common_gnd_factor, u32 v_feedback_tap_factor)
-{
-	return v_common_gnd_factor - v_feedback_tap_factor;
-}
-
-static void get_xtalk_scale_and_alpha(struct snd_soc_component *component, int xtalk_indx,
-				      u8 *scale, u8 *alpha)
-{
-	u32 r_aud_int_fet_mohms = 0, r_aud_ext_fet_mohms = 0, r_conn_par_load_pos_mohms = 0,
-	    r_load_mohms = 32360, r_aud_res_tot_mohms = 0, v_common_gnd_factor = 0,
-	    v_feedback_tap_factor = 0, xtalk_gain = 0, zl = 0, zr = 0;
-	struct wcd939x_priv *wcd939x = NULL;
-	struct wcd939x_pdata *pdata = NULL;
-
-	if ((xtalk_indx != XTALK_L_CH_NUM) && (xtalk_indx != XTALK_R_CH_NUM))
-		goto err_data;
-	wcd939x = snd_soc_component_get_drvdata(component);
-	if (!wcd939x->dev)
-		goto err_data;
-	pdata = dev_get_platdata(wcd939x->dev);
-	if (pdata->xtalk.xtalk_config == XTALK_NONE)
-		goto err_data;
-
-	/* Get headphone impedance for r_load */
-	wcd939x_mbhc_get_impedance(wcd939x->mbhc, &zl, &zr);
-	if (xtalk_indx == XTALK_L_CH_NUM) {
-		if (zl > MAX_RLOAD_OHMS || zl == 0) {
-			pdata->xtalk.scale_l = MAX_XTALK_SCALE;
-			pdata->xtalk.alpha_l = MAX_XTALK_ALPHA;
-			pdata->xtalk.zl = 0;
-			goto err_data;
-		}
-		/* Use cached alpha and scale for the same headphone load */
-		if (zl == pdata->xtalk.zl) {
-			*alpha = pdata->xtalk.alpha_l;
-			*scale = pdata->xtalk.scale_l;
-			return;
-		}
-		pdata->xtalk.zl = zl;
-	} else {
-		if (zr > MAX_RLOAD_OHMS || zr == 0) {
-			pdata->xtalk.scale_r = MAX_XTALK_SCALE;
-			pdata->xtalk.alpha_r = MAX_XTALK_ALPHA;
-			pdata->xtalk.zr = 0;
-			goto err_data;
-		}
-		/* Use cached alpha and scale for the same headphone load */
-		if (zr == pdata->xtalk.zr) {
-			*alpha = pdata->xtalk.alpha_r;
-			*scale = pdata->xtalk.scale_r;
-			return;
-		}
-		pdata->xtalk.zr = zr;
-	}
-
-	/* Channel-dependent impedance parameters */
-	if (xtalk_indx == XTALK_L_CH_NUM) {
-		r_aud_int_fet_mohms = pdata->xtalk.r_aud_int_fet_l_mohms;
-		r_aud_ext_fet_mohms = pdata->xtalk.r_aud_ext_fet_l_mohms;
-		r_conn_par_load_pos_mohms = pdata->xtalk.r_conn_par_load_pos_l_mohms;
-		r_aud_res_tot_mohms = pdata->xtalk.r_aud_res_tot_l_mohms;
-		r_load_mohms = pdata->xtalk.zl * OHMS_TO_MILLIOHMS;
-	} else {
-		r_aud_int_fet_mohms = pdata->xtalk.r_aud_int_fet_r_mohms;
-		r_aud_ext_fet_mohms = pdata->xtalk.r_aud_ext_fet_r_mohms;
-		r_conn_par_load_pos_mohms = pdata->xtalk.r_conn_par_load_pos_r_mohms;
-		r_aud_res_tot_mohms = pdata->xtalk.r_aud_res_tot_r_mohms;
-		r_load_mohms = pdata->xtalk.zr * OHMS_TO_MILLIOHMS;
-	}
-
-	/* Xtalk gain calculation */
-	v_common_gnd_factor = get_v_common_gnd_factor(pdata->xtalk.r_gnd_res_tot_mohms,
-						      r_load_mohms,
-						      r_aud_res_tot_mohms);
-	if (pdata->xtalk.xtalk_config == XTALK_ANALOG) {
-		v_feedback_tap_factor = get_v_feedback_tap_factor_analog(
-						pdata->xtalk.r_conn_par_load_neg_mohms,
-						r_load_mohms,
-						pdata->xtalk.r_gnd_res_tot_mohms,
-						r_aud_res_tot_mohms);
-	} else {
-		v_feedback_tap_factor = get_v_feedback_tap_factor(
-						pdata->xtalk.r_gnd_int_fet_mohms,
-						pdata->xtalk.r_gnd_par_route1_mohms,
-						r_load_mohms,
-						pdata->xtalk.r_gnd_res_tot_mohms,
-						r_aud_res_tot_mohms);
-	}
-	xtalk_gain = get_xtalk_gain(v_common_gnd_factor, v_feedback_tap_factor);
-
-	/* Store scale and alpha values */
-	*scale = get_xtalk_scale(xtalk_gain);
-	*alpha = get_xtalk_alpha(xtalk_gain, *scale);
-	if (xtalk_indx == XTALK_L_CH_NUM) {
-		pdata->xtalk.scale_l = *scale;
-		pdata->xtalk.alpha_l = *alpha;
-	} else {
-		pdata->xtalk.scale_r = *scale;
-		pdata->xtalk.alpha_r = *alpha;
-	}
-
-	return;
-
-err_data:
-	*scale = MAX_XTALK_SCALE;
-	*alpha = MAX_XTALK_ALPHA;
-}
-
 static int wcd939x_config_xtalk(struct snd_soc_component *component,
 					int event, int xtalk_indx)
 {
-	u8 scale = MAX_XTALK_SCALE, alpha = MAX_XTALK_ALPHA;
 	u16 xtalk_sec0 = 0, xtalk_sec1 = 0, xtalk_sec2 = 0, xtalk_sec3 = 0;
 	struct wcd939x_priv *wcd939x = NULL;
-
+	struct wcd939x_pdata *pdata = NULL;
 	if (!component) {
 		pr_err_ratelimited("%s: Invalid params, NULL component\n", __func__);
 		return -EINVAL;
@@ -1215,6 +1046,8 @@ static int wcd939x_config_xtalk(struct snd_soc_component *component,
 	if (!wcd939x->xtalk_enabled[xtalk_indx])
 		return 0;
 
+	pdata = dev_get_platdata(wcd939x->dev);
+
 	dev_dbg(component->dev, "%s xtalk_indx = %d event = %d\n",
 					__func__, xtalk_indx, event);
 
@@ -1226,10 +1059,25 @@ static int wcd939x_config_xtalk(struct snd_soc_component *component,
 		xtalk_sec2 = WCD939X_HPHL_RX_PATH_SEC2 + (xtalk_indx * WCD939X_XTALK_OFFSET);
 		xtalk_sec3 = WCD939X_HPHL_RX_PATH_SEC3 + (xtalk_indx * WCD939X_XTALK_OFFSET);
 
-		/* Determine scale and alpha */
-		get_xtalk_scale_and_alpha(component, xtalk_indx, &scale, &alpha);
-		snd_soc_component_update_bits(component, xtalk_sec1, 0xFF, alpha);
-		snd_soc_component_update_bits(component, xtalk_sec0, 0x1F, scale);
+		/* Write scale and alpha based on channel */
+		if (xtalk_indx == XTALK_L_CH_NUM) {
+			snd_soc_component_update_bits(component, xtalk_sec1, 0xFF,
+						      pdata->usbcss_hs.alpha_l);
+			snd_soc_component_update_bits(component, xtalk_sec0, 0x1F,
+						      pdata->usbcss_hs.scale_l);
+		} else if (xtalk_indx == XTALK_R_CH_NUM) {
+			snd_soc_component_update_bits(component, xtalk_sec1, 0xFF,
+						      pdata->usbcss_hs.alpha_r);
+			snd_soc_component_update_bits(component, xtalk_sec0, 0x1F,
+						      pdata->usbcss_hs.scale_r);
+		} else {
+			snd_soc_component_update_bits(component, xtalk_sec1, 0xFF, MIN_XTALK_ALPHA);
+			snd_soc_component_update_bits(component, xtalk_sec0, 0x1F, MAX_XTALK_SCALE);
+		}
+
+		dev_dbg(component->dev, "%s Scale = 0x%x, Alpha = 0x%x\n", __func__,
+			snd_soc_component_read(component, xtalk_sec0),
+			snd_soc_component_read(component, xtalk_sec1));
 
 		snd_soc_component_update_bits(component, xtalk_sec3, 0xFF, 0x4F);
 		snd_soc_component_update_bits(component, xtalk_sec2, 0x1F, 0x11);
@@ -1246,7 +1094,6 @@ static int wcd939x_config_xtalk(struct snd_soc_component *component,
 				0x00, 0x00);
 		break;
 	}
-
 	return 0;
 }
 
@@ -1320,25 +1167,51 @@ static int wcd939x_rx_mux(struct snd_soc_dapm_widget *w,
 }
 
 static void wcd939x_config_2Vpk_mode(struct snd_soc_component *component,
-			struct wcd939x_priv *wcd939x)
+			struct wcd939x_priv *wcd939x, int mode_2vpk)
 {
 	uint32_t zl = 0, zr = 0;
-	int rc = wcd_mbhc_get_impedance(&wcd939x->mbhc->wcd_mbhc, &zl, &zr);
+	int rc;
+
+	if (!wcd939x->in_2Vpk_mode)
+		return;
 
+	rc = wcd_mbhc_get_impedance(&wcd939x->mbhc->wcd_mbhc, &zl, &zr);
 	if (rc) {
 		dev_err_ratelimited(component->dev, "%s: Unable to get impedance for 2Vpk mode", __func__);
 		return;
 	}
 
-	snd_soc_component_update_bits(component,
-		REG_FIELD_VALUE(PA_GAIN_CTL_L, RX_SUPPLY_LEVEL, 0x01));
-
-	if (zl < HPH_IMPEDANCE_2VPK_MODE_OHMS)
-		snd_soc_component_update_bits(component,
-			REG_FIELD_VALUE(PA_GAIN_CTL_L, EN_HPHPA_2VPK, 0x00));
-	else
+	switch (mode_2vpk) {
+	case SUPPLY_LEVEL_2VPK:
 		snd_soc_component_update_bits(component,
-			REG_FIELD_VALUE(PA_GAIN_CTL_L, EN_HPHPA_2VPK, 0x01));
+				REG_FIELD_VALUE(PA_GAIN_CTL_L, RX_SUPPLY_LEVEL, 0x01));
+
+		if (zl < HPH_IMPEDANCE_2VPK_MODE_OHMS)
+			snd_soc_component_update_bits(component,
+					REG_FIELD_VALUE(PA_GAIN_CTL_L, EN_HPHPA_2VPK, 0x00));
+		else
+			snd_soc_component_update_bits(component,
+					REG_FIELD_VALUE(PA_GAIN_CTL_L, EN_HPHPA_2VPK, 0x01));
+		break;
+	case REGULATOR_MODE_2VPK:
+		if (zl >= HPH_IMPEDANCE_2VPK_MODE_OHMS) {
+			snd_soc_component_update_bits(component,
+				REG_FIELD_VALUE(RX_SUPPLIES, REGULATOR_MODE, 0x01));
+			snd_soc_component_update_bits(component, WCD939X_FLYBACK_TEST_CTL,
+					0x0F, 0x02);
+		} else {
+			snd_soc_component_update_bits(component, WCD939X_FLYBACK_TEST_CTL,
+					0x0F, 0x0D);
+		}
+		break;
+	case SET_HPH_GAIN_2VPK:
+		if (zl >= HPH_IMPEDANCE_2VPK_MODE_OHMS) {
+			snd_soc_component_update_bits(component, WCD939X_PA_GAIN_CTL_L, 0x1F, 0x02);
+			snd_soc_component_update_bits(component, WCD939X_PA_GAIN_CTL_R, 0x1F, 0x02);
+		}
+
+		break;
+	}
 }
 
 static int wcd939x_codec_hphl_dac_event(struct snd_soc_dapm_widget *w,
@@ -1356,8 +1229,7 @@ static int wcd939x_codec_hphl_dac_event(struct snd_soc_dapm_widget *w,
 		if (!wcd939x->hph_pcm_enabled)
 			snd_soc_component_update_bits(component,
 				REG_FIELD_VALUE(RDAC_CLK_CTL1, OPAMP_CHOP_CLK_EN, 0x00));
-		if (wcd939x->in_2Vpk_mode)
-			wcd939x_config_2Vpk_mode(component, wcd939x);
+		wcd939x_config_2Vpk_mode(component, wcd939x, SUPPLY_LEVEL_2VPK);
 
 		snd_soc_component_update_bits(component,
 				REG_FIELD_VALUE(CDC_HPH_GAIN_CTL, HPHL_RX_EN, 0x01));
@@ -1421,8 +1293,7 @@ static int wcd939x_codec_hphr_dac_event(struct snd_soc_dapm_widget *w,
 		if (!wcd939x->hph_pcm_enabled)
 			snd_soc_component_update_bits(component,
 				REG_FIELD_VALUE(RDAC_CLK_CTL1, OPAMP_CHOP_CLK_EN, 0x00));
-		if (wcd939x->in_2Vpk_mode)
-			wcd939x_config_2Vpk_mode(component, wcd939x);
+		wcd939x_config_2Vpk_mode(component, wcd939x, SUPPLY_LEVEL_2VPK);
 
 		snd_soc_component_update_bits(component,
 				REG_FIELD_VALUE(CDC_HPH_GAIN_CTL, HPHR_RX_EN, 0x01));
@@ -1532,6 +1403,7 @@ static int wcd939x_codec_enable_hphr_pa(struct snd_soc_dapm_widget *w,
 			     WCD_CLSH_EVENT_PRE_DAC,
 			     WCD_CLSH_STATE_HPHR,
 			     hph_mode);
+		wcd939x_config_2Vpk_mode(component, wcd939x, REGULATOR_MODE_2VPK);
 		if (hph_mode == CLS_H_LP || hph_mode == CLS_H_LOHIFI ||
 		    hph_mode == CLS_H_ULP) {
 			if (!wcd939x->hph_pcm_enabled)
@@ -1684,6 +1556,7 @@ static int wcd939x_codec_enable_hphl_pa(struct snd_soc_dapm_widget *w,
 			     WCD_CLSH_EVENT_PRE_DAC,
 			     WCD_CLSH_STATE_HPHL,
 			     hph_mode);
+		wcd939x_config_2Vpk_mode(component, wcd939x, REGULATOR_MODE_2VPK);
 		if (hph_mode == CLS_H_LP || hph_mode == CLS_H_LOHIFI ||
 		    hph_mode == CLS_H_ULP) {
 			if (!wcd939x->hph_pcm_enabled)
@@ -3731,8 +3604,8 @@ static const struct snd_kcontrol_new wcd939x_snd_controls[] = {
 	SOC_SINGLE_EXT("ADC2_BCS Disable", SND_SOC_NOPM, 0, 1, 0,
 		wcd939x_bcs_get, wcd939x_bcs_put),
 
-	SOC_SINGLE_TLV("HPHL Volume", WCD939X_L_EN, 0, 20, 1, line_gain),
-	SOC_SINGLE_TLV("HPHR Volume", WCD939X_R_EN, 0, 20, 1, line_gain),
+	SOC_SINGLE_TLV("HPHL Volume", WCD939X_PA_GAIN_CTL_L, 0, 0x18, 0, hph_analog_gain),
+	SOC_SINGLE_TLV("HPHR Volume", WCD939X_PA_GAIN_CTL_R, 0, 0x18, 0, hph_analog_gain),
 	SOC_SINGLE_TLV("ADC1 Volume", WCD939X_TX_CH1, 0, 20, 0,
 			analog_gain),
 	SOC_SINGLE_TLV("ADC2 Volume", WCD939X_TX_CH2, 0, 20, 0,
@@ -4817,6 +4690,19 @@ static int wcd939x_read_of_property_u32(struct device *dev, const char *name,
 	return rc;
 }
 
+static int wcd939x_read_of_property_s32(struct device *dev, const char *name,
+					s32 *val)
+{
+	int rc = 0;
+
+	rc = of_property_read_s32(dev->of_node, name, val);
+	if (rc)
+		dev_err(dev, "%s: Looking up %s property in node %s failed\n",
+			__func__, name, dev->of_node->full_name);
+
+	return rc;
+}
+
 static void wcd939x_dt_parse_micbias_info(struct device *dev,
 					  struct wcd939x_micbias_setting *mb)
 {
@@ -4876,26 +4762,41 @@ static void wcd939x_dt_parse_micbias_info(struct device *dev,
 	}
 }
 
-static void init_xtalk_params(struct wcd939x_xtalk_params *xtalk)
+static void init_usbcss_hs_params(struct wcd939x_usbcss_hs_params *usbcss_hs)
 {
-	xtalk->r_gnd_int_fet_mohms = 200;
-	xtalk->r_gnd_par_route1_mohms = 50;
-	xtalk->r_gnd_par_route2_mohms = 50;
-	xtalk->r_gnd_ext_fet_mohms = 650;
-	xtalk->r_conn_par_load_neg_mohms = 125;
-	xtalk->r_aud_int_fet_l_mohms = 200;
-	xtalk->r_aud_int_fet_r_mohms = 200;
-	xtalk->r_aud_ext_fet_l_mohms = 650;
-	xtalk->r_aud_ext_fet_r_mohms = 650;
-	xtalk->r_conn_par_load_pos_l_mohms = 7550;
-	xtalk->r_conn_par_load_pos_r_mohms = 7550;
-	xtalk->zl = 0;
-	xtalk->zr = 0;
-	xtalk->scale_l = MAX_XTALK_SCALE;
-	xtalk->alpha_l = MAX_XTALK_ALPHA;
-	xtalk->scale_r = MAX_XTALK_SCALE;
-	xtalk->alpha_r = MAX_XTALK_ALPHA;
-	xtalk->xtalk_config = XTALK_ANALOG;
+	usbcss_hs->r_gnd_sbu1_int_fet_mohms = 145;
+	usbcss_hs->r_gnd_sbu2_int_fet_mohms = 185;
+	usbcss_hs->r_gnd_ext_fet_customer_mohms = 0;
+	usbcss_hs->r_gnd_ext_fet_mohms = 0;  /* to be computed during MBHC zdet */
+	usbcss_hs->r_gnd_par_route1_mohms = 5;
+	usbcss_hs->r_gnd_par_route2_mohms = 330;
+	usbcss_hs->r_gnd_par_tot_mohms = 0;
+	usbcss_hs->r_gnd_sbu1_res_tot_mohms = 0;
+	usbcss_hs->r_gnd_sbu2_res_tot_mohms = 0;
+	usbcss_hs->r_conn_par_load_pos_mohms = 7550;
+	usbcss_hs->r_aud_int_fet_l_mohms = 303;
+	usbcss_hs->r_aud_int_fet_r_mohms = 275;
+	usbcss_hs->r_aud_ext_fet_l_mohms = 0; /* to be computed during MBHC zdet */
+	usbcss_hs->r_aud_ext_fet_r_mohms = 0; /* to be computed during MBHC zdet */
+	usbcss_hs->r_aud_res_tot_l_mohms = 0;
+	usbcss_hs->r_aud_res_tot_r_mohms = 0;
+	usbcss_hs->r_surge_mohms = 272;
+	usbcss_hs->r_load_eff_l_mohms = 0; /* to be computed during MBHC zdet */
+	usbcss_hs->r_load_eff_r_mohms = 0; /* to be computed during MBHC zdet */
+	usbcss_hs->r3 = 1;
+	usbcss_hs->r4 = 330;
+	usbcss_hs->r5 = 5;
+	usbcss_hs->r6 = 1;
+	usbcss_hs->r7 = 5;
+	usbcss_hs->k_aud_times_100 = 13;
+	usbcss_hs->k_gnd_times_100 = 13;
+	usbcss_hs->aud_tap_offset = 0;
+	usbcss_hs->gnd_tap_offset = 0;
+	usbcss_hs->scale_l = MAX_XTALK_SCALE;
+	usbcss_hs->alpha_l = MIN_XTALK_ALPHA;
+	usbcss_hs->scale_r = MAX_XTALK_SCALE;
+	usbcss_hs->alpha_r = MIN_XTALK_ALPHA;
+	usbcss_hs->xtalk_config = XTALK_NONE;
 }
 
 static void parse_xtalk_param(struct device *dev, u32 default_val, u32 *prop_val_p,
@@ -4905,7 +4806,7 @@ static void parse_xtalk_param(struct device *dev, u32 default_val, u32 *prop_val
 
 	if (of_find_property(dev->of_node, prop, NULL)) {
 		rc = wcd939x_read_of_property_u32(dev, prop, prop_val_p);
-		if ((!rc) && (*prop_val_p <= MAX_IMPEDANCE_MOHMS) && (*prop_val_p > 0))
+		if ((!rc) && (*prop_val_p <= MAX_USBCSS_HS_IMPEDANCE_MOHMS) && (*prop_val_p > 0))
 			return;
 		*prop_val_p = default_val;
 		dev_dbg(dev, "%s: %s OOB. Default value of %d will be used.\n", __func__, prop,
@@ -4917,86 +4818,112 @@ static void parse_xtalk_param(struct device *dev, u32 default_val, u32 *prop_val
 	}
 }
 
-static void wcd939x_dt_parse_xtalk_info(struct device *dev, struct wcd939x_xtalk_params *xtalk)
+static void wcd939x_dt_parse_usbcss_hs_info(struct device *dev,
+					    struct wcd939x_usbcss_hs_params *usbcss_hs)
 {
 	u32 prop_val = 0;
+	s32 prop_val_signed = 0;
 	int rc = 0;
 
-	init_xtalk_params(xtalk);
+	/* Default values for parameters */
+	init_usbcss_hs_params(usbcss_hs);
 
 	/* xtalk_config: Determine type of crosstalk: none (0), digital (1), or analog (2) */
-	if (of_find_property(dev->of_node, "qcom,xtalk-config", NULL)) {
-		rc = wcd939x_read_of_property_u32(dev, "qcom,xtalk-config", &prop_val);
+	if (of_find_property(dev->of_node, "qcom,usbcss-hs-xtalk-config", NULL)) {
+		rc = wcd939x_read_of_property_u32(dev, "qcom,usbcss-hs-xtalk-config", &prop_val);
 		if ((!rc) && (prop_val == XTALK_NONE || prop_val == XTALK_DIGITAL
 			      || prop_val == XTALK_ANALOG)) {
-			xtalk->xtalk_config = (enum xtalk_mode) prop_val;
+			usbcss_hs->xtalk_config = (enum xtalk_mode) prop_val;
 		} else
-			dev_dbg(dev, "%s: qcom,xtalk-config OOB. Default value of %s used.\n",
-				__func__, "XTALK_NONE");
+			dev_dbg(dev, "%s: %s OOB. Default value of %s used.\n",
+				__func__, "qcom,usbcss-hs-xtalk-config", "XTALK_NONE");
 	} else
-		dev_dbg(dev,
-			"%s: qcom,xtalk-config property not found. Default value of %s used.\n",
-			__func__, "XTALK_NONE");
-	if (xtalk->xtalk_config == XTALK_NONE)
-		goto post_get_params;
-
-	/* r_gnd_int_fet_mohms */
-	parse_xtalk_param(dev, xtalk->r_gnd_int_fet_mohms, &prop_val,
-			  "qcom,xtalk-r-gnd-int-fet-mohms");
-	xtalk->r_gnd_int_fet_mohms = prop_val;
-	/* r_gnd_par_route1_mohms */
-	parse_xtalk_param(dev, xtalk->r_gnd_par_route1_mohms, &prop_val,
-			  "qcom,xtalk-r-gnd-par-route1-mohms");
-	xtalk->r_gnd_par_route1_mohms = prop_val;
-	/* r_gnd_par_route2_mohms */
-	parse_xtalk_param(dev, xtalk->r_gnd_par_route2_mohms, &prop_val,
-			  "qcom,xtalk-r-gnd-par-route2-mohms");
-	xtalk->r_gnd_par_route2_mohms = prop_val;
-	/* r_gnd_ext_fet_mohms */
-	parse_xtalk_param(dev, xtalk->r_gnd_ext_fet_mohms, &prop_val,
-			  "qcom,xtalk-r-gnd-ext-fet-mohms");
-	xtalk->r_gnd_ext_fet_mohms = prop_val;
-	/* r_conn_par_load_neg_mohms */
-	parse_xtalk_param(dev, xtalk->r_conn_par_load_neg_mohms, &prop_val,
-			  "qcom,xtalk-r-conn-par-load-neg-mohms");
-	xtalk->r_conn_par_load_neg_mohms = prop_val;
-	/* r_aud_int_fet_l_mohms */
-	parse_xtalk_param(dev, xtalk->r_aud_int_fet_l_mohms, &prop_val,
-			  "qcom,xtalk-r-aud-int-fet-l-mohms");
-	xtalk->r_aud_int_fet_l_mohms = prop_val;
-	/* r_aud_int_fet_r_mohms */
-	parse_xtalk_param(dev, xtalk->r_aud_int_fet_r_mohms, &prop_val,
-			  "qcom,xtalk-r-aud-int-fet-r-mohms");
-	xtalk->r_aud_int_fet_r_mohms = prop_val;
-	/* r_aud_ext_fet_l_mohms */
-	parse_xtalk_param(dev, xtalk->r_aud_ext_fet_l_mohms, &prop_val,
-			  "qcom,xtalk-r-aud-ext-fet-l-mohms");
-	xtalk->r_aud_ext_fet_l_mohms = prop_val;
-	/* r_aud_ext_fet_r_mohms */
-	parse_xtalk_param(dev, xtalk->r_aud_ext_fet_r_mohms, &prop_val,
-			  "qcom,xtalk-r-aud-ext-fet-r-mohms");
-	xtalk->r_aud_ext_fet_r_mohms = prop_val;
-	/* r_conn_par_load_pos_l_mohms */
-	parse_xtalk_param(dev, xtalk->r_conn_par_load_pos_l_mohms, &prop_val,
-			  "qcom,xtalk-r-conn-par-load-pos-l-mohms");
-	xtalk->r_conn_par_load_pos_l_mohms = prop_val;
-	/* r_conn_par_load_pos_r_mohms */
-	parse_xtalk_param(dev, xtalk->r_conn_par_load_pos_r_mohms, &prop_val,
-			  "qcom,xtalk-r-conn-par-load-pos-r-mohms");
-	xtalk->r_conn_par_load_pos_r_mohms = prop_val;
-
-post_get_params:
-	xtalk->r_gnd_res_tot_mohms = get_r_gnd_res_tot_mohms(xtalk->r_gnd_int_fet_mohms,
-							     xtalk->r_gnd_par_route1_mohms,
-							     xtalk->r_gnd_par_route2_mohms,
-							     xtalk->r_gnd_ext_fet_mohms,
-							     xtalk->r_conn_par_load_neg_mohms);
-	xtalk->r_aud_res_tot_l_mohms = get_r_aud_res_tot_mohms(xtalk->r_aud_int_fet_l_mohms,
-							       xtalk->r_aud_ext_fet_l_mohms,
-							       xtalk->r_conn_par_load_pos_l_mohms);
-	xtalk->r_aud_res_tot_r_mohms = get_r_aud_res_tot_mohms(xtalk->r_aud_int_fet_r_mohms,
-							       xtalk->r_aud_ext_fet_r_mohms,
-							       xtalk->r_conn_par_load_pos_r_mohms);
+		dev_dbg(dev, "%s: %s property not found. Default value of %s used.\n",
+			__func__, "qcom,usbcss-hs-xtalk-config", "XTALK_NONE");
+
+	/* k values for linearizer */
+	if (of_find_property(dev->of_node, "qcom,usbcss-hs-lin-k-aud", NULL)) {
+		rc = wcd939x_read_of_property_s32(dev, "qcom,usbcss-hs-lin-k-aud",
+						  &prop_val);
+		if ((!rc) && (prop_val <= MAX_K_TIMES_100) && (prop_val >= MIN_K_TIMES_100))
+			usbcss_hs->k_aud_times_100 = prop_val;
+		dev_dbg(dev, "%s: %s OOB. Default value of %d will be used.\n",
+			__func__, "qcom,usbcss-hs-lin-k-aud",
+			usbcss_hs->k_aud_times_100);
+	} else {
+		dev_dbg(dev, "%s: %s property not found. Default value of %d will be used.\n",
+			__func__, "qcom,usbcss-hs-lin-k-aud",
+			usbcss_hs->k_aud_times_100);
+	}
+	if (of_find_property(dev->of_node, "qcom,usbcss-hs-lin-k-gnd", NULL)) {
+		rc = wcd939x_read_of_property_s32(dev, "qcom,usbcss-hs-lin-k-gnd",
+						  &prop_val_signed);
+		if ((!rc) && (prop_val_signed <= MAX_K_TIMES_100) &&
+		    (prop_val_signed >= MIN_K_TIMES_100))
+			usbcss_hs->k_gnd_times_100 = prop_val_signed;
+		dev_dbg(dev, "%s: %s OOB. Default value of %d will be used.\n",
+			__func__, "qcom,usbcss-hs-lin-k-gnd",
+			usbcss_hs->k_gnd_times_100);
+	} else {
+		dev_dbg(dev, "%s: %s property not found. Default value of %d will be used.\n",
+			__func__, "qcom,usbcss-hs-lin-k-gnd",
+			usbcss_hs->k_gnd_times_100);
+	}
+
+	/* r_gnd_ext_fet_customer_mohms */
+	parse_xtalk_param(dev, usbcss_hs->r_gnd_ext_fet_customer_mohms, &prop_val,
+			  "qcom,usbcss-hs-rdson");
+	usbcss_hs->r_gnd_ext_fet_customer_mohms = prop_val;
+	/* r_conn_par_load_pos_mohm */
+	parse_xtalk_param(dev, usbcss_hs->r_conn_par_load_pos_mohms, &prop_val,
+			  "qcom,usbcss-hs-r2");
+	usbcss_hs->r_conn_par_load_pos_mohms = prop_val;
+	/* r3 */
+	parse_xtalk_param(dev, usbcss_hs->r3, &prop_val,
+			  "qcom,usbcss-hs-r3");
+	usbcss_hs->r3 = prop_val;
+	/* r4 */
+	parse_xtalk_param(dev, usbcss_hs->r4, &prop_val,
+			  "qcom,usbcss-hs-r4");
+	usbcss_hs->r4 = prop_val;
+	/* r_gnd_par_route1_mohms and r_gnd_par_route2_mohms */
+	if (usbcss_hs->xtalk_config == XTALK_ANALOG) {
+		parse_xtalk_param(dev, usbcss_hs->r5, &prop_val,
+				  "qcom,usbcss-hs-r5");
+		usbcss_hs->r5 = prop_val;
+		usbcss_hs->r_gnd_par_route1_mohms = usbcss_hs->r5 + usbcss_hs->r4;
+		usbcss_hs->r_gnd_par_route2_mohms = 125;
+	} else if (usbcss_hs->xtalk_config == XTALK_DIGITAL) {
+		parse_xtalk_param(dev, usbcss_hs->r6, &prop_val,
+				  "qcom,usbcss-hs-r6");
+		usbcss_hs->r6 = prop_val;
+		usbcss_hs->r_gnd_par_route2_mohms = usbcss_hs->r6 + usbcss_hs->r4;
+		parse_xtalk_param(dev, usbcss_hs->r_gnd_par_route1_mohms, &prop_val,
+				  "qcom,usbcss-hs-r7");
+		usbcss_hs->r7 = prop_val;
+		usbcss_hs->r_gnd_par_route1_mohms = prop_val;
+	}
+
+	/* Compute total resistances */
+	usbcss_hs->r_gnd_par_tot_mohms = usbcss_hs->r_gnd_par_route1_mohms +
+					 usbcss_hs->r_gnd_par_route2_mohms;
+	usbcss_hs->r_gnd_sbu1_res_tot_mohms = get_r_gnd_res_tot_mohms(
+								usbcss_hs->r_gnd_sbu1_int_fet_mohms,
+								usbcss_hs->r_gnd_ext_fet_mohms,
+								usbcss_hs->r_gnd_par_tot_mohms);
+	usbcss_hs->r_gnd_sbu2_res_tot_mohms = get_r_gnd_res_tot_mohms(
+								usbcss_hs->r_gnd_sbu2_int_fet_mohms,
+								usbcss_hs->r_gnd_ext_fet_mohms,
+								usbcss_hs->r_gnd_par_tot_mohms);
+	usbcss_hs->r_aud_res_tot_l_mohms = get_r_aud_res_tot_mohms(
+								usbcss_hs->r_aud_int_fet_l_mohms,
+								usbcss_hs->r_aud_ext_fet_l_mohms);
+	usbcss_hs->r_aud_res_tot_r_mohms = get_r_aud_res_tot_mohms(
+								usbcss_hs->r_aud_int_fet_r_mohms,
+								usbcss_hs->r_aud_ext_fet_r_mohms);
+
+	/* Set linearizer calibration codes to be sourced from SW */
+	wcd_usbss_linearizer_rdac_cal_code_select(LINEARIZER_SOURCE_SW);
 }
 
 static int wcd939x_reset_low(struct device *dev)
@@ -5061,7 +4988,7 @@ struct wcd939x_pdata *wcd939x_populate_dt_data(struct device *dev)
 	pdata->tx_slave = of_parse_phandle(dev->of_node, "qcom,tx-slave", 0);
 
 	wcd939x_dt_parse_micbias_info(dev, &pdata->micbias);
-	wcd939x_dt_parse_xtalk_info(dev, &pdata->xtalk);
+	wcd939x_dt_parse_usbcss_hs_info(dev, &pdata->usbcss_hs);
 
 	return pdata;
 }
@@ -5141,7 +5068,7 @@ static void wcd939x_update_regmap_cache(struct wcd939x_priv *wcd939x)
 
 static int wcd939x_bind(struct device *dev)
 {
-	int ret = 0, i = 0;
+	int ret = 0, i = 0, val = 0;
 	struct wcd939x_pdata *pdata = dev_get_platdata(dev);
 	struct wcd939x_priv *wcd939x = dev_get_drvdata(dev);
 	u8 id1 = 0, status1 = 0;
@@ -5198,6 +5125,11 @@ static int wcd939x_bind(struct device *dev)
 				__func__);
 		goto err;
 	}
+#if IS_ENABLED(CONFIG_QCOM_WCD_USBSS_I2C)
+	regmap_read(wcd939x->regmap, WCD939X_EFUSE_REG_17, &val);
+	if (wcd939x_version == WCD939X_VERSION_2_0 && val < 3)
+		wcd_usbss_update_default_trim();
+#endif
 	wcd939x_update_regmap_cache(wcd939x);
 
 	/* Set all interupts as edge triggered */

+ 2 - 0
asoc/msm-audio-defs.h

@@ -113,7 +113,9 @@
 #define LPASS_BE_RT_PROXY_PCM_RX "PCM_RT_PROXY-RX-1"
 #define LPASS_BE_RT_PROXY_PCM_TX "PCM_RT_PROXY-TX-1"
 
+#define LPASS_BE_PCM_DUMMY_RX_0 "PCM_DUMMY-RX-0"
 #define LPASS_BE_PCM_DUMMY_TX_0 "PCM_DUMMY-TX-0"
+#define LPASS_BE_PCM_DUMMY_TX_1 "PCM_DUMMY-TX-1"
 
 #define LPASS_BE_BTFM_PROXY_RX_0 "BTFM_PROXY-RX-0"
 #define LPASS_BE_BTFM_PROXY_TX_0 "BTFM_PROXY-TX-0"

+ 15 - 2
asoc/msm_common.c

@@ -48,6 +48,8 @@ struct snd_card_pdata {
 #define SAMPLING_RATE_176P4KHZ  176400
 #define SAMPLING_RATE_352P8KHZ  352800
 
+struct mutex vote_against_sleep_lock;
+
 static struct attribute device_state_attr = {
 	.name = "state",
 	.mode = 0660,
@@ -173,8 +175,13 @@ int snd_card_notify_user(snd_card_status_t card_status)
 {
 	snd_card_pdata->card_status = card_status;
 	sysfs_notify(&snd_card_pdata->snd_card_kobj, NULL, "card_state");
-	if (card_status == 0)
+	if (card_status == 0) {
+		mutex_lock(&vote_against_sleep_lock);
 		vote_against_sleep_cnt = 0;
+		pr_debug("%s: SSR/PDR triggered reset vote_against_sleep_cnt = %d\n",
+					__func__, vote_against_sleep_cnt);
+		mutex_unlock(&vote_against_sleep_lock);
+	}
 	return 0;
 }
 
@@ -809,6 +816,8 @@ exit:
 		devm_kfree(&pdev->dev, core_val_array);
 	}
 
+	mutex_init(&vote_against_sleep_lock);
+
 	return 0;
 };
 
@@ -819,6 +828,7 @@ void msm_common_snd_deinit(struct msm_common_pdata *common_pdata)
 	if (!common_pdata)
 		return;
 
+	mutex_destroy(&vote_against_sleep_lock);
 	msm_audio_remove_qos_request();
 
 	mutex_destroy(&common_pdata->aud_dev_lock);
@@ -1098,6 +1108,7 @@ static int msm_vote_against_sleep_ctl_put(struct snd_kcontrol *kcontrol,
 {
 	int ret = 0;
 
+	mutex_lock(&vote_against_sleep_lock);
 	vote_against_sleep_enable = ucontrol->value.integer.value[0];
 	pr_debug("%s: vote against sleep enable: %d sleep cnt: %d", __func__,
 			vote_against_sleep_enable, vote_against_sleep_cnt);
@@ -1107,7 +1118,8 @@ static int msm_vote_against_sleep_ctl_put(struct snd_kcontrol *kcontrol,
 		if (vote_against_sleep_cnt ==  1) {
 			ret = audio_prm_set_vote_against_sleep(1);
 			if (ret < 0) {
-				--vote_against_sleep_cnt;
+				if (vote_against_sleep_cnt > 0)
+					--vote_against_sleep_cnt;
 				pr_err("%s: failed to vote against sleep ret: %d\n", __func__, ret);
 			}
 		}
@@ -1119,6 +1131,7 @@ static int msm_vote_against_sleep_ctl_put(struct snd_kcontrol *kcontrol,
 	}
 
 	pr_debug("%s: vote against sleep vote ret: %d\n", __func__, ret);
+	mutex_unlock(&vote_against_sleep_lock);
 	return ret;
 }
 

+ 9 - 0
asoc/msm_dailink.h

@@ -400,3 +400,12 @@ SND_SOC_DAILINK_DEFS(tavil_i2s_tx1,
 	DAILINK_COMP_ARRAY(COMP_CODEC("tavil_codec", "tavil_i2s_tx1")),
 	DAILINK_COMP_ARRAY(COMP_PLATFORM("snd-soc-dummy")));
 
+SND_SOC_DAILINK_DEFS(pcm_dummy_rx0,
+	DAILINK_COMP_ARRAY(COMP_CPU("snd-soc-dummy-dai")),
+	DAILINK_COMP_ARRAY(COMP_DUMMY()),
+	DAILINK_COMP_ARRAY(COMP_PLATFORM("snd-soc-dummy")));
+
+SND_SOC_DAILINK_DEFS(pcm_dummy_tx1,
+	DAILINK_COMP_ARRAY(COMP_CPU("snd-soc-dummy-dai")),
+	DAILINK_COMP_ARRAY(COMP_DUMMY()),
+	DAILINK_COMP_ARRAY(COMP_PLATFORM("snd-soc-dummy")));

+ 231 - 60
asoc/pineapple.c

@@ -81,6 +81,7 @@ struct msm_asoc_mach_data {
 	int (*get_dev_num)(struct snd_soc_component *);
 	int backend_used;
 	struct prm_earpa_hw_intf_config upd_config;
+	bool dedicated_wsa2; /* used to define how wsa2 slave devices are used */
 };
 
 static bool is_initial_boot;
@@ -97,6 +98,9 @@ static int msm_rx_tx_codec_init(struct snd_soc_pcm_runtime*);
 static int msm_int_wsa_init(struct snd_soc_pcm_runtime*);
 static int msm_int_wsa884x_init(struct snd_soc_pcm_runtime*);
 static int msm_int_wsa883x_init(struct snd_soc_pcm_runtime*);
+static int msm_int_wsa2_init(struct snd_soc_pcm_runtime *);
+static int msm_int_wsa884x_2_init(struct snd_soc_pcm_runtime *);
+static int msm_int_wsa883x_2_init(struct snd_soc_pcm_runtime *);
 
 /*
  * Need to report LINEIN
@@ -136,10 +140,12 @@ static bool msm_usbc_swap_gnd_mic(struct snd_soc_component *component, bool acti
 		return false;
 
 #if IS_ENABLED(CONFIG_QCOM_WCD_USBSS_I2C)
-	ret = wcd_usbss_switch_update(WCD_USBSS_GND_MIC_SWAP_AATC,
-								WCD_USBSS_CABLE_CONNECT);
-	if (ret == 0)
-		return true;
+	if (wcd_mbhc_cfg.usbss_hsj_connect_enable)
+		ret = wcd_usbss_switch_update(WCD_USBSS_GND_MIC_SWAP_HSJ,
+							WCD_USBSS_CABLE_CONNECT);
+	else if (wcd_mbhc_cfg.enable_usbc_analog)
+		ret = wcd_usbss_switch_update(WCD_USBSS_GND_MIC_SWAP_AATC,
+							WCD_USBSS_CABLE_CONNECT);
 #endif
 	return ret;
 }
@@ -516,6 +522,29 @@ static struct snd_soc_dai_link msm_common_be_dai_links[] = {
 		.ops = &msm_common_be_ops,
 		SND_SOC_DAILINK_REG(pcm_dummy_tx0),
 	},
+	/* Dummy Dev Rx BACK END DAI Link */
+	{
+		.name = LPASS_BE_PCM_DUMMY_RX_0,
+		.stream_name = LPASS_BE_PCM_DUMMY_RX_0,
+		.playback_only = 1,
+		.trigger = {SND_SOC_DPCM_TRIGGER_POST,
+			SND_SOC_DPCM_TRIGGER_POST},
+		.ignore_pmdown_time = 1,
+		.ignore_suspend = 1,
+		.ops = &msm_common_be_ops,
+		SND_SOC_DAILINK_REG(pcm_dummy_rx0),
+	},
+	/* Dummy Dev Tx BACK END DAI Link */
+	{
+		.name = LPASS_BE_PCM_DUMMY_TX_1,
+		.stream_name = LPASS_BE_PCM_DUMMY_TX_1,
+		.capture_only = 1,
+		.trigger = {SND_SOC_DPCM_TRIGGER_POST,
+			SND_SOC_DPCM_TRIGGER_POST},
+		.ignore_suspend = 1,
+		.ops = &msm_common_be_ops,
+		SND_SOC_DAILINK_REG(pcm_dummy_tx1),
+	},
 };
 
 #ifndef CONFIG_AUDIO_BTFM_PROXY
@@ -663,6 +692,7 @@ static struct snd_soc_dai_link msm_wsa2_cdc_dma_be_dai_links[] = {
 		.ignore_suspend = 1,
 		.ops = &msm_common_be_ops,
 		SND_SOC_DAILINK_REG(wsa2_dma_rx0),
+		.init = &msm_int_wsa2_init,
 	},
 	{
 		.name = LPASS_BE_WSA2_CDC_DMA_RX_1,
@@ -1395,6 +1425,12 @@ static int msm_snd_card_late_probe(struct snd_soc_card *card)
 		return -ENOMEM;
 	wcd_mbhc_cfg.calibration = mbhc_calibration;
 
+#if IS_ENABLED(CONFIG_QCOM_WCD_USBSS_I2C)
+	if (of_find_property(card->dev->of_node,
+				"qcom,usbss-hsj-connect-enabled", NULL))
+		wcd_mbhc_cfg.usbss_hsj_connect_enable = true;
+#endif
+
 	ret = wcd939x_mbhc_hs_detect(component, &wcd_mbhc_cfg);
 	if (ret) {
 		dev_err(component->dev, "%s: mbhc hs detect failed, err:%d\n",
@@ -1443,15 +1479,28 @@ static struct snd_soc_card *populate_snd_card_dailinks(struct device *dev, int w
 			total_links += ARRAY_SIZE(msm_wsa_cdc_dma_be_dai_links);
 			break;
 		case QUAD_SPEAKER:
-			memcpy(msm_pineapple_dai_links + total_links,
-			       msm_wsa2_cdc_dma_be_dai_links,
-			       sizeof(msm_wsa2_cdc_dma_be_dai_links));
-			total_links += ARRAY_SIZE(msm_wsa2_cdc_dma_be_dai_links);
-
-			memcpy(msm_pineapple_dai_links + total_links,
-			       msm_wsa_wsa2_cdc_dma_be_dai_links,
-			       sizeof(msm_wsa_wsa2_cdc_dma_be_dai_links));
-			total_links += ARRAY_SIZE(msm_wsa_wsa2_cdc_dma_be_dai_links);
+			if (of_find_property(dev->of_node,
+					"qcom,dedicated-wsa2", NULL)) {
+				memcpy(msm_pineapple_dai_links + total_links,
+					msm_wsa_cdc_dma_be_dai_links,
+					sizeof(msm_wsa_cdc_dma_be_dai_links));
+				total_links += ARRAY_SIZE(msm_wsa_cdc_dma_be_dai_links);
+
+				memcpy(msm_pineapple_dai_links + total_links,
+					msm_wsa2_cdc_dma_be_dai_links,
+					sizeof(msm_wsa2_cdc_dma_be_dai_links));
+				total_links += ARRAY_SIZE(msm_wsa2_cdc_dma_be_dai_links);
+			} else {
+				memcpy(msm_pineapple_dai_links + total_links,
+					msm_wsa2_cdc_dma_be_dai_links,
+					sizeof(msm_wsa2_cdc_dma_be_dai_links));
+				total_links += ARRAY_SIZE(msm_wsa2_cdc_dma_be_dai_links);
+
+				memcpy(msm_pineapple_dai_links + total_links,
+					msm_wsa_wsa2_cdc_dma_be_dai_links,
+					sizeof(msm_wsa_wsa2_cdc_dma_be_dai_links));
+				total_links += ARRAY_SIZE(msm_wsa_wsa2_cdc_dma_be_dai_links);
+			}
 			break;
 		default:
 			dev_dbg(dev,
@@ -1577,36 +1626,37 @@ static int msm_int_wsa883x_init(struct snd_soc_pcm_runtime *rtd)
 				component);
 	}
 
-	if (pdata->wsa_max_devs > 2) {
-		component = snd_soc_rtdcom_lookup(rtd, "wsa-codec.3");
-		if (!component) {
-			pr_err("%s: wsa-codec.3 component is NULL\n", __func__);
-			return -EINVAL;
-		}
-
-		wsa883x_set_channel_map(component, &spkleft_ports[0],
-				WSA883X_MAX_SWR_PORTS, &ch_mask[0],
-				&ch_rate[0], &spkleft_port_types[0]);
+	if (!pdata->dedicated_wsa2) {
+		if (pdata->wsa_max_devs > 2) {
+			component = snd_soc_rtdcom_lookup(rtd, "wsa-codec.3");
+			if (!component) {
+				pr_err("%s: wsa-codec.3 component is NULL\n", __func__);
+				return -EINVAL;
+			}
 
-		wsa883x_codec_info_create_codec_entry(pdata->codec_root,
-				component);
-	}
+			wsa883x_set_channel_map(component, &spkleft_ports[0],
+					WSA883X_MAX_SWR_PORTS, &ch_mask[0],
+					&ch_rate[0], &spkleft_port_types[0]);
 
-	if (pdata->wsa_max_devs > 3) {
-		component = snd_soc_rtdcom_lookup(rtd, "wsa-codec.4");
-		if (!component) {
-			pr_err("%s: wsa-codec.4 component is NULL\n", __func__);
-			return -EINVAL;
+			wsa883x_codec_info_create_codec_entry(pdata->codec_root,
+					component);
 		}
 
-		wsa883x_set_channel_map(component, &spkright_ports[0],
-				WSA883X_MAX_SWR_PORTS, &ch_mask[0],
-				&ch_rate[0], &spkright_port_types[0]);
+		if (pdata->wsa_max_devs > 3) {
+			component = snd_soc_rtdcom_lookup(rtd, "wsa-codec.4");
+			if (!component) {
+				pr_err("%s: wsa-codec.4 component is NULL\n", __func__);
+				return -EINVAL;
+			}
 
-		wsa883x_codec_info_create_codec_entry(pdata->codec_root,
-				component);
-	}
+			wsa883x_set_channel_map(component, &spkright_ports[0],
+					WSA883X_MAX_SWR_PORTS, &ch_mask[0],
+					&ch_rate[0], &spkright_port_types[0]);
 
+			wsa883x_codec_info_create_codec_entry(pdata->codec_root,
+					component);
+		}
+	}
 	msm_common_dai_link_init(rtd);
 
 	return 0;
@@ -1661,34 +1711,36 @@ static int msm_int_wsa884x_init(struct snd_soc_pcm_runtime *rtd)
 			component);
 	}
 
-	if (pdata->wsa_max_devs > 2) {
-		component = snd_soc_rtdcom_lookup(rtd, "wsa-codec.3");
-		if (!component) {
-			pr_err("%s: wsa-codec.3 component is NULL\n", __func__);
-			return -EINVAL;
-		}
-
-		wsa884x_set_channel_map(component, &spkleft_ports[0],
-			WSA884X_MAX_SWR_PORTS, &ch_mask[0],
-			&ch_rate[0], &spkleft_port_types[0]);
+	if (!pdata->dedicated_wsa2) {
+		if (pdata->wsa_max_devs > 2) {
+			component = snd_soc_rtdcom_lookup(rtd, "wsa-codec.3");
+			if (!component) {
+				pr_err("%s: wsa-codec.3 component is NULL\n", __func__);
+				return -EINVAL;
+			}
 
-		wsa884x_codec_info_create_codec_entry(pdata->codec_root,
-			component);
-	}
+			wsa884x_set_channel_map(component, &spkleft_ports[0],
+				WSA884X_MAX_SWR_PORTS, &ch_mask[0],
+				&ch_rate[0], &spkleft_port_types[0]);
 
-	if (pdata->wsa_max_devs > 3) {
-		component = snd_soc_rtdcom_lookup(rtd, "wsa-codec.4");
-		if (!component) {
-			pr_err("%s: wsa-codec.4 component is NULL\n", __func__);
-			return -EINVAL;
+			wsa884x_codec_info_create_codec_entry(pdata->codec_root,
+				component);
 		}
 
-		wsa884x_set_channel_map(component, &spkright_ports[0],
-			WSA884X_MAX_SWR_PORTS, &ch_mask[0],
-			&ch_rate[0], &spkright_port_types[0]);
+		if (pdata->wsa_max_devs > 3) {
+			component = snd_soc_rtdcom_lookup(rtd, "wsa-codec.4");
+			if (!component) {
+				pr_err("%s: wsa-codec.4 component is NULL\n", __func__);
+				return -EINVAL;
+			}
 
-		wsa884x_codec_info_create_codec_entry(pdata->codec_root,
-			component);
+			wsa884x_set_channel_map(component, &spkright_ports[0],
+				WSA884X_MAX_SWR_PORTS, &ch_mask[0],
+				&ch_rate[0], &spkright_port_types[0]);
+
+			wsa884x_codec_info_create_codec_entry(pdata->codec_root,
+				component);
+		}
 	}
 
 	msm_common_dai_link_init(rtd);
@@ -1704,6 +1756,121 @@ static int msm_int_wsa_init(struct snd_soc_pcm_runtime *rtd)
 	return msm_int_wsa884x_init(rtd);
 }
 
+static int msm_int_wsa883x_2_init(struct snd_soc_pcm_runtime *rtd)
+{
+	u8 spkleft_ports[WSA883X_MAX_SWR_PORTS] = {0, 1, 2, 3};
+	u8 spkright_ports[WSA883X_MAX_SWR_PORTS] = {0, 1, 2, 3};
+	u8 spkleft_port_types[WSA883X_MAX_SWR_PORTS] = {SPKR_L, SPKR_L_COMP,
+							SPKR_L_BOOST, SPKR_L_VI};
+	u8 spkright_port_types[WSA883X_MAX_SWR_PORTS] = {SPKR_R, SPKR_R_COMP,
+							SPKR_R_BOOST, SPKR_R_VI};
+	unsigned int ch_rate[WSA883X_MAX_SWR_PORTS] = {SWR_CLK_RATE_2P4MHZ, SWR_CLK_RATE_0P6MHZ,
+						SWR_CLK_RATE_0P3MHZ, SWR_CLK_RATE_1P2MHZ};
+	unsigned int ch_mask[WSA883X_MAX_SWR_PORTS] = {0x1, 0xF, 0x3, 0x3};
+	struct snd_soc_component *component = NULL;
+	struct msm_asoc_mach_data *pdata =
+			snd_soc_card_get_drvdata(rtd->card);
+
+	if (pdata->dedicated_wsa2) {
+		if (pdata->wsa_max_devs > 2) {
+			component = snd_soc_rtdcom_lookup(rtd, "wsa-codec.3");
+			if (!component) {
+				pr_err("%s: wsa-codec.3 component is NULL\n", __func__);
+				return -EINVAL;
+			}
+
+			wsa883x_set_channel_map(component, &spkleft_ports[0],
+					WSA883X_MAX_SWR_PORTS, &ch_mask[0],
+					&ch_rate[0], &spkleft_port_types[0]);
+
+			wsa883x_codec_info_create_codec_entry(pdata->codec_root,
+					component);
+		}
+
+		if (pdata->wsa_max_devs > 3) {
+			component = snd_soc_rtdcom_lookup(rtd, "wsa-codec.4");
+			if (!component) {
+				pr_err("%s: wsa-codec.4 component is NULL\n", __func__);
+				return -EINVAL;
+			}
+
+			wsa883x_set_channel_map(component, &spkright_ports[0],
+					WSA883X_MAX_SWR_PORTS, &ch_mask[0],
+					&ch_rate[0], &spkright_port_types[0]);
+
+			wsa883x_codec_info_create_codec_entry(pdata->codec_root,
+					component);
+		}
+
+		msm_common_dai_link_init(rtd);
+	}
+
+	return 0;
+}
+
+static int msm_int_wsa884x_2_init(struct snd_soc_pcm_runtime *rtd)
+{
+	u8 spkleft_ports[WSA884X_MAX_SWR_PORTS] = {0, 1, 2, 3, 4, 5};
+	u8 spkright_ports[WSA884X_MAX_SWR_PORTS] = {0, 1, 2, 3, 4, 5};
+	u8 spkleft_port_types[WSA884X_MAX_SWR_PORTS] = {SPKR_L, SPKR_L_COMP,
+						SPKR_L_BOOST, PBR, SPKR_L_VI, CPS};
+	u8 spkright_port_types[WSA884X_MAX_SWR_PORTS] = {SPKR_R, SPKR_R_COMP,
+						SPKR_R_BOOST, PBR, SPKR_R_VI, CPS};
+	unsigned int ch_rate[WSA884X_MAX_SWR_PORTS] = {SWR_CLK_RATE_2P4MHZ, SWR_CLK_RATE_0P6MHZ,
+							SWR_CLK_RATE_0P3MHZ, SWR_CLK_RATE_48KHZ,
+							SWR_CLK_RATE_1P2MHZ, SWR_CLK_RATE_24KHZ};
+	unsigned int ch_mask[WSA884X_MAX_SWR_PORTS] = {0x1, 0xF, 0x3, 0x1, 0x3, 0x3};
+
+
+	struct snd_soc_component *component = NULL;
+	struct msm_asoc_mach_data *pdata =
+				snd_soc_card_get_drvdata(rtd->card);
+
+	if (pdata->dedicated_wsa2) {
+		if (pdata->wsa_max_devs > 2) {
+			component = snd_soc_rtdcom_lookup(rtd, "wsa-codec.3");
+			if (!component) {
+				pr_err("%s: wsa-codec.3 component is NULL\n", __func__);
+				return -EINVAL;
+			}
+
+			wsa884x_set_channel_map(component, &spkleft_ports[0],
+				WSA884X_MAX_SWR_PORTS, &ch_mask[0],
+				&ch_rate[0], &spkleft_port_types[0]);
+
+			wsa884x_codec_info_create_codec_entry(pdata->codec_root,
+				component);
+		}
+
+		if (pdata->wsa_max_devs > 3) {
+			component = snd_soc_rtdcom_lookup(rtd, "wsa-codec.4");
+			if (!component) {
+				pr_err("%s: wsa-codec.4 component is NULL\n", __func__);
+				return -EINVAL;
+			}
+
+			wsa884x_set_channel_map(component, &spkright_ports[0],
+				WSA884X_MAX_SWR_PORTS, &ch_mask[0],
+				&ch_rate[0], &spkright_port_types[0]);
+
+			wsa884x_codec_info_create_codec_entry(pdata->codec_root,
+				component);
+		}
+
+		msm_common_dai_link_init(rtd);
+	}
+
+	return 0;
+}
+
+static int msm_int_wsa2_init(struct snd_soc_pcm_runtime *rtd)
+{
+	if (strstr(rtd->card->name, "wsa883x"))
+		return msm_int_wsa883x_2_init(rtd);
+
+	return msm_int_wsa884x_2_init(rtd);
+}
+
 static int msm_rx_tx_codec_init(struct snd_soc_pcm_runtime *rtd)
 {
 	int codec_variant = -1;
@@ -1992,6 +2159,10 @@ static int msm_asoc_machine_probe(struct platform_device *pdev)
 		pdata->wsa_max_devs = 0;
 	}
 
+	/* Check if WSA2 Backends were used for dedicated usecase */
+	pdata->dedicated_wsa2 = of_find_property(pdev->dev.of_node,
+				"qcom,dedicated-wsa2", NULL);
+
 	card = populate_snd_card_dailinks(&pdev->dev, pdata->wsa_max_devs);
 	if (!card) {
 		dev_err(&pdev->dev, "%s: Card uninitialized\n", __func__);

+ 1 - 1
audio_modules.bzl

@@ -265,7 +265,7 @@ audio_modules.register(
     path = ASOC_CODECS_PATH,
     config_option = "CONFIG_SND_SOC_MSM_HDMI_CODEC_RX",
     srcs = ["msm_hdmi_codec_rx.c"],
-    deps = ["//vendor/qcom/opensource/mm-drivers:%b_mm_drivers"]
+    deps = ["//vendor/qcom/opensource/mm-drivers/msm_ext_display:%b_msm_ext_display"]
 )
 # >>>> ASOC/CODECS/LPASS-CDC MODULES <<<<
 audio_modules.register(

+ 3 - 1
include/asoc/wcd-mbhc-v2.h

@@ -1,6 +1,6 @@
 /* SPDX-License-Identifier: GPL-2.0-only */
 /* Copyright (c) 2014-2021, The Linux Foundation. All rights reserved.
- * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
+ * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
  */
 #ifndef __WCD_MBHC_V2_H__
 #define __WCD_MBHC_V2_H__
@@ -440,6 +440,7 @@ struct wcd_mbhc_config {
 	bool enable_anc_mic_detect;
 	u32 enable_usbc_analog;
 	bool moisture_duty_cycle_en;
+	bool usbss_hsj_connect_enable;
 };
 
 struct wcd_mbhc_intr {
@@ -529,6 +530,7 @@ struct wcd_mbhc_cb {
 	bool (*mbhc_get_moisture_status)(struct wcd_mbhc *mbhc);
 	void (*mbhc_moisture_polling_ctrl)(struct wcd_mbhc *mbhc, bool enable);
 	void (*mbhc_moisture_detect_en)(struct wcd_mbhc *mbhc, bool enable);
+	void (*surge_reset_routine)(struct wcd_mbhc *mbhc);
 };
 
 struct wcd_mbhc_fn {

+ 14 - 1
include/uapi/audio/linux/msm_audio.h

@@ -19,6 +19,19 @@
 
 #define IOCTL_MAP_HYP_ASSIGN _IOW(AUDIO_IOCTL_MAGIC, 109, int)
 #define IOCTL_UNMAP_HYP_ASSIGN _IOW(AUDIO_IOCTL_MAGIC, 110, int)
-#define	AUDIO_MAX_COMMON_IOCTL_NUM	111
+#define IOCTL_MAP_HYP_ASSIGN_V2 _IOW(AUDIO_IOCTL_MAGIC, 111, struct msm_mdf_data)
+#define IOCTL_UNMAP_HYP_ASSIGN_V2 _IOW(AUDIO_IOCTL_MAGIC, 112, struct msm_mdf_data)
+
+#define AUDIO_MAX_COMMON_IOCTL_NUM 113
+
+/* ss_masks is generated from sys_ids
+ * for id in sys_ids
+ * ss_masks |= 1 << (id - 1)
+ */
+struct msm_mdf_data {
+	__u64 ss_masks;
+	__u32 mem_fd;
+	__u32 reserved;
+};
 
 #endif

+ 14 - 0
soc/pinctrl-lpi.c

@@ -67,6 +67,15 @@
 #define LPI_GPIO_FUNC_FUNC4              "func4"
 #define LPI_GPIO_FUNC_FUNC5              "func5"
 
+#define LPI_GPIO_DRV_2_MA      2
+#define LPI_GPIO_DRV_4_MA      4
+#define LPI_GPIO_DRV_6_MA      6
+#define LPI_GPIO_DRV_8_MA      8
+#define LPI_GPIO_DRV_10_MA      10
+#define LPI_GPIO_DRV_12_MA      12
+#define LPI_GPIO_DRV_14_MA      14
+#define LPI_GPIO_DRV_16_MA      16
+
 static bool lpi_dev_up;
 static struct device *lpi_dev;
 static bool initial_boot = false;
@@ -338,6 +347,11 @@ static int lpi_config_get(struct pinctrl_dev *pctldev,
 
 static unsigned int lpi_drive_to_regval(u32 arg)
 {
+	if (arg < LPI_GPIO_DRV_2_MA)
+		arg = LPI_GPIO_DRV_2_MA;
+	if (arg > LPI_GPIO_DRV_16_MA)
+		arg = LPI_GPIO_DRV_16_MA;
+
 	return (arg/2 - 1);
 }