Linux Audio

Check our new training course

Loading...
v6.13.7
   1// SPDX-License-Identifier: GPL-2.0
   2//
   3// mt6358.c  --  mt6358 ALSA SoC audio codec driver
   4//
   5// Copyright (c) 2018 MediaTek Inc.
   6// Author: KaiChieh Chuang <kaichieh.chuang@mediatek.com>
   7
   8#include <linux/platform_device.h>
   9#include <linux/mod_devicetable.h>
  10#include <linux/module.h>
 
  11#include <linux/delay.h>
  12#include <linux/kthread.h>
  13#include <linux/sched.h>
  14#include <linux/mfd/mt6397/core.h>
  15#include <linux/regulator/consumer.h>
  16
  17#include <sound/soc.h>
  18#include <sound/tlv.h>
  19
  20#include "mt6358.h"
  21
  22enum {
  23	AUDIO_ANALOG_VOLUME_HSOUTL,
  24	AUDIO_ANALOG_VOLUME_HSOUTR,
  25	AUDIO_ANALOG_VOLUME_HPOUTL,
  26	AUDIO_ANALOG_VOLUME_HPOUTR,
  27	AUDIO_ANALOG_VOLUME_LINEOUTL,
  28	AUDIO_ANALOG_VOLUME_LINEOUTR,
  29	AUDIO_ANALOG_VOLUME_MICAMP1,
  30	AUDIO_ANALOG_VOLUME_MICAMP2,
  31	AUDIO_ANALOG_VOLUME_TYPE_MAX
  32};
  33
  34enum {
  35	MUX_ADC_L,
  36	MUX_ADC_R,
  37	MUX_PGA_L,
  38	MUX_PGA_R,
  39	MUX_MIC_TYPE,
  40	MUX_HP_L,
  41	MUX_HP_R,
  42	MUX_NUM,
  43};
  44
  45enum {
  46	DEVICE_HP,
  47	DEVICE_LO,
  48	DEVICE_RCV,
  49	DEVICE_MIC1,
  50	DEVICE_MIC2,
  51	DEVICE_NUM
  52};
  53
  54/* Supply widget subseq */
  55enum {
  56	/* common */
  57	SUPPLY_SEQ_CLK_BUF,
  58	SUPPLY_SEQ_AUD_GLB,
  59	SUPPLY_SEQ_CLKSQ,
  60	SUPPLY_SEQ_VOW_AUD_LPW,
  61	SUPPLY_SEQ_AUD_VOW,
  62	SUPPLY_SEQ_VOW_CLK,
  63	SUPPLY_SEQ_VOW_LDO,
  64	SUPPLY_SEQ_TOP_CK,
  65	SUPPLY_SEQ_TOP_CK_LAST,
  66	SUPPLY_SEQ_AUD_TOP,
  67	SUPPLY_SEQ_AUD_TOP_LAST,
  68	SUPPLY_SEQ_AFE,
  69	/* capture */
  70	SUPPLY_SEQ_ADC_SUPPLY,
  71};
  72
  73enum {
  74	CH_L = 0,
  75	CH_R,
  76	NUM_CH,
  77};
  78
  79#define REG_STRIDE 2
  80
  81struct mt6358_priv {
  82	struct device *dev;
  83	struct regmap *regmap;
  84
  85	unsigned int dl_rate;
  86	unsigned int ul_rate;
  87
  88	int ana_gain[AUDIO_ANALOG_VOLUME_TYPE_MAX];
  89	unsigned int mux_select[MUX_NUM];
  90
  91	int dev_counter[DEVICE_NUM];
  92
  93	int mtkaif_protocol;
  94
  95	struct regulator *avdd_reg;
  96
  97	int wov_enabled;
  98
  99	int dmic_one_wire_mode;
 100};
 101
 102int mt6358_set_mtkaif_protocol(struct snd_soc_component *cmpnt,
 103			       int mtkaif_protocol)
 104{
 105	struct mt6358_priv *priv = snd_soc_component_get_drvdata(cmpnt);
 106
 107	priv->mtkaif_protocol = mtkaif_protocol;
 108	return 0;
 109}
 110EXPORT_SYMBOL_GPL(mt6358_set_mtkaif_protocol);
 111
 112static void playback_gpio_set(struct mt6358_priv *priv)
 113{
 114	/* set gpio mosi mode */
 115	regmap_update_bits(priv->regmap, MT6358_GPIO_MODE2_CLR,
 116			   0x01f8, 0x01f8);
 117	regmap_update_bits(priv->regmap, MT6358_GPIO_MODE2_SET,
 118			   0xffff, 0x0249);
 119	regmap_update_bits(priv->regmap, MT6358_GPIO_MODE2,
 120			   0xffff, 0x0249);
 121}
 122
 123static void playback_gpio_reset(struct mt6358_priv *priv)
 124{
 125	/* set pad_aud_*_mosi to GPIO mode and dir input
 126	 * reason:
 127	 * pad_aud_dat_mosi*, because the pin is used as boot strap
 128	 * don't clean clk/sync, for mtkaif protocol 2
 129	 */
 130	regmap_update_bits(priv->regmap, MT6358_GPIO_MODE2_CLR,
 131			   0x01f8, 0x01f8);
 132	regmap_update_bits(priv->regmap, MT6358_GPIO_MODE2,
 133			   0x01f8, 0x0000);
 134	regmap_update_bits(priv->regmap, MT6358_GPIO_DIR0,
 135			   0xf << 8, 0x0);
 136}
 137
 138static void capture_gpio_set(struct mt6358_priv *priv)
 139{
 140	/* set gpio miso mode */
 141	regmap_update_bits(priv->regmap, MT6358_GPIO_MODE3_CLR,
 142			   0xffff, 0xffff);
 143	regmap_update_bits(priv->regmap, MT6358_GPIO_MODE3_SET,
 144			   0xffff, 0x0249);
 145	regmap_update_bits(priv->regmap, MT6358_GPIO_MODE3,
 146			   0xffff, 0x0249);
 147}
 148
 149static void capture_gpio_reset(struct mt6358_priv *priv)
 150{
 151	/* set pad_aud_*_miso to GPIO mode and dir input
 152	 * reason:
 153	 * pad_aud_clk_miso, because when playback only the miso_clk
 154	 * will also have 26m, so will have power leak
 155	 * pad_aud_dat_miso*, because the pin is used as boot strap
 156	 */
 157	regmap_update_bits(priv->regmap, MT6358_GPIO_MODE3_CLR,
 158			   0xffff, 0xffff);
 159	regmap_update_bits(priv->regmap, MT6358_GPIO_MODE3,
 160			   0xffff, 0x0000);
 161	regmap_update_bits(priv->regmap, MT6358_GPIO_DIR0,
 162			   0xf << 12, 0x0);
 163}
 164
 165/* use only when not govern by DAPM */
 166static int mt6358_set_dcxo(struct mt6358_priv *priv, bool enable)
 167{
 168	regmap_update_bits(priv->regmap, MT6358_DCXO_CW14,
 169			   0x1 << RG_XO_AUDIO_EN_M_SFT,
 170			   (enable ? 1 : 0) << RG_XO_AUDIO_EN_M_SFT);
 171	return 0;
 172}
 173
 174/* use only when not govern by DAPM */
 175static int mt6358_set_clksq(struct mt6358_priv *priv, bool enable)
 176{
 177	/* audio clk source from internal dcxo */
 178	regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON6,
 179			   RG_CLKSQ_IN_SEL_TEST_MASK_SFT,
 180			   0x0);
 181
 182	/* Enable/disable CLKSQ 26MHz */
 183	regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON6,
 184			   RG_CLKSQ_EN_MASK_SFT,
 185			   (enable ? 1 : 0) << RG_CLKSQ_EN_SFT);
 186	return 0;
 187}
 188
 189/* use only when not govern by DAPM */
 190static int mt6358_set_aud_global_bias(struct mt6358_priv *priv, bool enable)
 191{
 192	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON13,
 193			   RG_AUDGLB_PWRDN_VA28_MASK_SFT,
 194			   (enable ? 0 : 1) << RG_AUDGLB_PWRDN_VA28_SFT);
 195	return 0;
 196}
 197
 198/* use only when not govern by DAPM */
 199static int mt6358_set_topck(struct mt6358_priv *priv, bool enable)
 200{
 201	regmap_update_bits(priv->regmap, MT6358_AUD_TOP_CKPDN_CON0,
 202			   0x0066, enable ? 0x0 : 0x66);
 203	return 0;
 204}
 205
 206static int mt6358_mtkaif_tx_enable(struct mt6358_priv *priv)
 207{
 208	switch (priv->mtkaif_protocol) {
 209	case MT6358_MTKAIF_PROTOCOL_2_CLK_P2:
 210		/* MTKAIF TX format setting */
 211		regmap_update_bits(priv->regmap,
 212				   MT6358_AFE_ADDA_MTKAIF_CFG0,
 213				   0xffff, 0x0010);
 214		/* enable aud_pad TX fifos */
 215		regmap_update_bits(priv->regmap,
 216				   MT6358_AFE_AUD_PAD_TOP,
 217				   0xff00, 0x3800);
 218		regmap_update_bits(priv->regmap,
 219				   MT6358_AFE_AUD_PAD_TOP,
 220				   0xff00, 0x3900);
 221		break;
 222	case MT6358_MTKAIF_PROTOCOL_2:
 223		/* MTKAIF TX format setting */
 224		regmap_update_bits(priv->regmap,
 225				   MT6358_AFE_ADDA_MTKAIF_CFG0,
 226				   0xffff, 0x0010);
 227		/* enable aud_pad TX fifos */
 228		regmap_update_bits(priv->regmap,
 229				   MT6358_AFE_AUD_PAD_TOP,
 230				   0xff00, 0x3100);
 231		break;
 232	case MT6358_MTKAIF_PROTOCOL_1:
 233	default:
 234		/* MTKAIF TX format setting */
 235		regmap_update_bits(priv->regmap,
 236				   MT6358_AFE_ADDA_MTKAIF_CFG0,
 237				   0xffff, 0x0000);
 238		/* enable aud_pad TX fifos */
 239		regmap_update_bits(priv->regmap,
 240				   MT6358_AFE_AUD_PAD_TOP,
 241				   0xff00, 0x3100);
 242		break;
 243	}
 244	return 0;
 245}
 246
 247static int mt6358_mtkaif_tx_disable(struct mt6358_priv *priv)
 248{
 249	/* disable aud_pad TX fifos */
 250	regmap_update_bits(priv->regmap, MT6358_AFE_AUD_PAD_TOP,
 251			   0xff00, 0x3000);
 252	return 0;
 253}
 254
 255int mt6358_mtkaif_calibration_enable(struct snd_soc_component *cmpnt)
 256{
 257	struct mt6358_priv *priv = snd_soc_component_get_drvdata(cmpnt);
 258
 259	playback_gpio_set(priv);
 260	capture_gpio_set(priv);
 261	mt6358_mtkaif_tx_enable(priv);
 262
 263	mt6358_set_dcxo(priv, true);
 264	mt6358_set_aud_global_bias(priv, true);
 265	mt6358_set_clksq(priv, true);
 266	mt6358_set_topck(priv, true);
 267
 268	/* set dat_miso_loopback on */
 269	regmap_update_bits(priv->regmap, MT6358_AUDIO_DIG_CFG,
 270			   RG_AUD_PAD_TOP_DAT_MISO2_LOOPBACK_MASK_SFT,
 271			   1 << RG_AUD_PAD_TOP_DAT_MISO2_LOOPBACK_SFT);
 272	regmap_update_bits(priv->regmap, MT6358_AUDIO_DIG_CFG,
 273			   RG_AUD_PAD_TOP_DAT_MISO_LOOPBACK_MASK_SFT,
 274			   1 << RG_AUD_PAD_TOP_DAT_MISO_LOOPBACK_SFT);
 275	return 0;
 276}
 277EXPORT_SYMBOL_GPL(mt6358_mtkaif_calibration_enable);
 278
 279int mt6358_mtkaif_calibration_disable(struct snd_soc_component *cmpnt)
 280{
 281	struct mt6358_priv *priv = snd_soc_component_get_drvdata(cmpnt);
 282
 283	/* set dat_miso_loopback off */
 284	regmap_update_bits(priv->regmap, MT6358_AUDIO_DIG_CFG,
 285			   RG_AUD_PAD_TOP_DAT_MISO2_LOOPBACK_MASK_SFT,
 286			   0 << RG_AUD_PAD_TOP_DAT_MISO2_LOOPBACK_SFT);
 287	regmap_update_bits(priv->regmap, MT6358_AUDIO_DIG_CFG,
 288			   RG_AUD_PAD_TOP_DAT_MISO_LOOPBACK_MASK_SFT,
 289			   0 << RG_AUD_PAD_TOP_DAT_MISO_LOOPBACK_SFT);
 290
 291	mt6358_set_topck(priv, false);
 292	mt6358_set_clksq(priv, false);
 293	mt6358_set_aud_global_bias(priv, false);
 294	mt6358_set_dcxo(priv, false);
 295
 296	mt6358_mtkaif_tx_disable(priv);
 297	playback_gpio_reset(priv);
 298	capture_gpio_reset(priv);
 299	return 0;
 300}
 301EXPORT_SYMBOL_GPL(mt6358_mtkaif_calibration_disable);
 302
 303int mt6358_set_mtkaif_calibration_phase(struct snd_soc_component *cmpnt,
 304					int phase_1, int phase_2)
 305{
 306	struct mt6358_priv *priv = snd_soc_component_get_drvdata(cmpnt);
 307
 308	regmap_update_bits(priv->regmap, MT6358_AUDIO_DIG_CFG,
 309			   RG_AUD_PAD_TOP_PHASE_MODE_MASK_SFT,
 310			   phase_1 << RG_AUD_PAD_TOP_PHASE_MODE_SFT);
 311	regmap_update_bits(priv->regmap, MT6358_AUDIO_DIG_CFG,
 312			   RG_AUD_PAD_TOP_PHASE_MODE2_MASK_SFT,
 313			   phase_2 << RG_AUD_PAD_TOP_PHASE_MODE2_SFT);
 314	return 0;
 315}
 316EXPORT_SYMBOL_GPL(mt6358_set_mtkaif_calibration_phase);
 317
 318/* dl pga gain */
 319enum {
 320	DL_GAIN_8DB = 0,
 321	DL_GAIN_0DB = 8,
 322	DL_GAIN_N_1DB = 9,
 323	DL_GAIN_N_10DB = 18,
 324	DL_GAIN_N_40DB = 0x1f,
 325};
 326
 327#define DL_GAIN_N_10DB_REG (DL_GAIN_N_10DB << 7 | DL_GAIN_N_10DB)
 328#define DL_GAIN_N_40DB_REG (DL_GAIN_N_40DB << 7 | DL_GAIN_N_40DB)
 329#define DL_GAIN_REG_MASK 0x0f9f
 330
 331static void hp_zcd_disable(struct mt6358_priv *priv)
 332{
 333	regmap_write(priv->regmap, MT6358_ZCD_CON0, 0x0000);
 334}
 335
 336static void hp_main_output_ramp(struct mt6358_priv *priv, bool up)
 337{
 338	int i, stage;
 339	int target = 7;
 340
 341	/* Enable/Reduce HPL/R main output stage step by step */
 342	for (i = 0; i <= target; i++) {
 343		stage = up ? i : target - i;
 344		regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON1,
 345				   0x7 << 8, stage << 8);
 346		regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON1,
 347				   0x7 << 11, stage << 11);
 348		usleep_range(100, 150);
 349	}
 350}
 351
 352static void hp_aux_feedback_loop_gain_ramp(struct mt6358_priv *priv, bool up)
 353{
 354	int i, stage;
 355
 356	/* Reduce HP aux feedback loop gain step by step */
 357	for (i = 0; i <= 0xf; i++) {
 358		stage = up ? i : 0xf - i;
 359		regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON9,
 360				   0xf << 12, stage << 12);
 361		usleep_range(100, 150);
 362	}
 363}
 364
 365static void hp_pull_down(struct mt6358_priv *priv, bool enable)
 366{
 367	int i;
 368
 369	if (enable) {
 370		for (i = 0x0; i <= 0x6; i++) {
 371			regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON4,
 372					   0x7, i);
 373			usleep_range(600, 700);
 374		}
 375	} else {
 376		for (i = 0x6; i >= 0x1; i--) {
 377			regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON4,
 378					   0x7, i);
 379			usleep_range(600, 700);
 380		}
 381	}
 382}
 383
 384static bool is_valid_hp_pga_idx(int reg_idx)
 385{
 386	return (reg_idx >= DL_GAIN_8DB && reg_idx <= DL_GAIN_N_10DB) ||
 387	       reg_idx == DL_GAIN_N_40DB;
 388}
 389
 390static void headset_volume_ramp(struct mt6358_priv *priv, int from, int to)
 391{
 392	int offset = 0, count = 0, reg_idx;
 393
 394	if (!is_valid_hp_pga_idx(from) || !is_valid_hp_pga_idx(to))
 395		dev_warn(priv->dev, "%s(), volume index is not valid, from %d, to %d\n",
 396			 __func__, from, to);
 397
 398	dev_info(priv->dev, "%s(), from %d, to %d\n",
 399		 __func__, from, to);
 400
 401	if (to > from)
 402		offset = to - from;
 403	else
 404		offset = from - to;
 405
 406	while (offset >= 0) {
 407		if (to > from)
 408			reg_idx = from + count;
 409		else
 410			reg_idx = from - count;
 411
 412		if (is_valid_hp_pga_idx(reg_idx)) {
 413			regmap_update_bits(priv->regmap,
 414					   MT6358_ZCD_CON2,
 415					   DL_GAIN_REG_MASK,
 416					   (reg_idx << 7) | reg_idx);
 417			usleep_range(200, 300);
 418		}
 419		offset--;
 420		count++;
 421	}
 422}
 423
 424static int mt6358_put_volsw(struct snd_kcontrol *kcontrol,
 425			    struct snd_ctl_elem_value *ucontrol)
 426{
 427	struct snd_soc_component *component =
 428			snd_soc_kcontrol_component(kcontrol);
 429	struct mt6358_priv *priv = snd_soc_component_get_drvdata(component);
 430	struct soc_mixer_control *mc =
 431			(struct soc_mixer_control *)kcontrol->private_value;
 432	unsigned int reg = 0;
 433	int ret;
 434
 435	ret = snd_soc_put_volsw(kcontrol, ucontrol);
 436	if (ret < 0)
 437		return ret;
 438
 439	switch (mc->reg) {
 440	case MT6358_ZCD_CON2:
 441		regmap_read(priv->regmap, MT6358_ZCD_CON2, &reg);
 442		priv->ana_gain[AUDIO_ANALOG_VOLUME_HPOUTL] =
 443			(reg >> RG_AUDHPLGAIN_SFT) & RG_AUDHPLGAIN_MASK;
 444		priv->ana_gain[AUDIO_ANALOG_VOLUME_HPOUTR] =
 445			(reg >> RG_AUDHPRGAIN_SFT) & RG_AUDHPRGAIN_MASK;
 446		break;
 447	case MT6358_ZCD_CON1:
 448		regmap_read(priv->regmap, MT6358_ZCD_CON1, &reg);
 449		priv->ana_gain[AUDIO_ANALOG_VOLUME_LINEOUTL] =
 450			(reg >> RG_AUDLOLGAIN_SFT) & RG_AUDLOLGAIN_MASK;
 451		priv->ana_gain[AUDIO_ANALOG_VOLUME_LINEOUTR] =
 452			(reg >> RG_AUDLORGAIN_SFT) & RG_AUDLORGAIN_MASK;
 453		break;
 454	case MT6358_ZCD_CON3:
 455		regmap_read(priv->regmap, MT6358_ZCD_CON3, &reg);
 456		priv->ana_gain[AUDIO_ANALOG_VOLUME_HSOUTL] =
 457			(reg >> RG_AUDHSGAIN_SFT) & RG_AUDHSGAIN_MASK;
 458		priv->ana_gain[AUDIO_ANALOG_VOLUME_HSOUTR] =
 459			(reg >> RG_AUDHSGAIN_SFT) & RG_AUDHSGAIN_MASK;
 460		break;
 461	case MT6358_AUDENC_ANA_CON0:
 462	case MT6358_AUDENC_ANA_CON1:
 463		regmap_read(priv->regmap, MT6358_AUDENC_ANA_CON0, &reg);
 464		priv->ana_gain[AUDIO_ANALOG_VOLUME_MICAMP1] =
 465			(reg >> RG_AUDPREAMPLGAIN_SFT) & RG_AUDPREAMPLGAIN_MASK;
 466		regmap_read(priv->regmap, MT6358_AUDENC_ANA_CON1, &reg);
 467		priv->ana_gain[AUDIO_ANALOG_VOLUME_MICAMP2] =
 468			(reg >> RG_AUDPREAMPRGAIN_SFT) & RG_AUDPREAMPRGAIN_MASK;
 469		break;
 470	}
 471
 472	return ret;
 473}
 474
 475static void mt6358_restore_pga(struct mt6358_priv *priv);
 476
 477static int mt6358_enable_wov_phase2(struct mt6358_priv *priv)
 478{
 479	/* analog */
 480	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON13,
 481			   0xffff, 0x0000);
 482	regmap_update_bits(priv->regmap, MT6358_DCXO_CW14, 0xffff, 0xa2b5);
 483	regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON1,
 484			   0xffff, 0x0800);
 485	mt6358_restore_pga(priv);
 486
 487	regmap_update_bits(priv->regmap, MT6358_DCXO_CW13, 0xffff, 0x9929);
 488	regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON9,
 489			   0xffff, 0x0025);
 490	regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON8,
 491			   0xffff, 0x0005);
 492
 493	/* digital */
 494	regmap_update_bits(priv->regmap, MT6358_AUD_TOP_CKPDN_CON0,
 495			   0xffff, 0x0000);
 496	regmap_update_bits(priv->regmap, MT6358_GPIO_MODE3, 0xffff, 0x0120);
 497	regmap_update_bits(priv->regmap, MT6358_AFE_VOW_CFG0, 0xffff, 0xffff);
 498	regmap_update_bits(priv->regmap, MT6358_AFE_VOW_CFG1, 0xffff, 0x0200);
 499	regmap_update_bits(priv->regmap, MT6358_AFE_VOW_CFG2, 0xffff, 0x2424);
 500	regmap_update_bits(priv->regmap, MT6358_AFE_VOW_CFG3, 0xffff, 0xdbac);
 501	regmap_update_bits(priv->regmap, MT6358_AFE_VOW_CFG4, 0xffff, 0x029e);
 502	regmap_update_bits(priv->regmap, MT6358_AFE_VOW_CFG5, 0xffff, 0x0000);
 503	regmap_update_bits(priv->regmap, MT6358_AFE_VOW_POSDIV_CFG0,
 504			   0xffff, 0x0000);
 505	regmap_update_bits(priv->regmap, MT6358_AFE_VOW_HPF_CFG0,
 506			   0xffff, 0x0451);
 507	regmap_update_bits(priv->regmap, MT6358_AFE_VOW_TOP, 0xffff, 0x68d1);
 508
 509	return 0;
 510}
 511
 512static int mt6358_disable_wov_phase2(struct mt6358_priv *priv)
 513{
 514	/* digital */
 515	regmap_update_bits(priv->regmap, MT6358_AFE_VOW_TOP, 0xffff, 0xc000);
 516	regmap_update_bits(priv->regmap, MT6358_AFE_VOW_HPF_CFG0,
 517			   0xffff, 0x0450);
 518	regmap_update_bits(priv->regmap, MT6358_AFE_VOW_POSDIV_CFG0,
 519			   0xffff, 0x0c00);
 520	regmap_update_bits(priv->regmap, MT6358_AFE_VOW_CFG5, 0xffff, 0x0100);
 521	regmap_update_bits(priv->regmap, MT6358_AFE_VOW_CFG4, 0xffff, 0x006c);
 522	regmap_update_bits(priv->regmap, MT6358_AFE_VOW_CFG3, 0xffff, 0xa879);
 523	regmap_update_bits(priv->regmap, MT6358_AFE_VOW_CFG2, 0xffff, 0x2323);
 524	regmap_update_bits(priv->regmap, MT6358_AFE_VOW_CFG1, 0xffff, 0x0400);
 525	regmap_update_bits(priv->regmap, MT6358_AFE_VOW_CFG0, 0xffff, 0x0000);
 526	regmap_update_bits(priv->regmap, MT6358_GPIO_MODE3, 0xffff, 0x02d8);
 527	regmap_update_bits(priv->regmap, MT6358_AUD_TOP_CKPDN_CON0,
 528			   0xffff, 0x0000);
 529
 530	/* analog */
 531	regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON8,
 532			   0xffff, 0x0004);
 533	regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON9,
 534			   0xffff, 0x0000);
 535	regmap_update_bits(priv->regmap, MT6358_DCXO_CW13, 0xffff, 0x9829);
 536	regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON1,
 537			   0xffff, 0x0000);
 538	mt6358_restore_pga(priv);
 539	regmap_update_bits(priv->regmap, MT6358_DCXO_CW14, 0xffff, 0xa2b5);
 540	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON13,
 541			   0xffff, 0x0010);
 542
 543	return 0;
 544}
 545
 546static int mt6358_get_wov(struct snd_kcontrol *kcontrol,
 547			  struct snd_ctl_elem_value *ucontrol)
 548{
 549	struct snd_soc_component *c = snd_soc_kcontrol_component(kcontrol);
 550	struct mt6358_priv *priv = snd_soc_component_get_drvdata(c);
 551
 552	ucontrol->value.integer.value[0] = priv->wov_enabled;
 553	return 0;
 554}
 555
 556static int mt6358_put_wov(struct snd_kcontrol *kcontrol,
 557			  struct snd_ctl_elem_value *ucontrol)
 558{
 559	struct snd_soc_component *c = snd_soc_kcontrol_component(kcontrol);
 560	struct mt6358_priv *priv = snd_soc_component_get_drvdata(c);
 561	int enabled = ucontrol->value.integer.value[0];
 562
 563	if (enabled < 0 || enabled > 1)
 564		return -EINVAL;
 565
 566	if (priv->wov_enabled != enabled) {
 567		if (enabled)
 568			mt6358_enable_wov_phase2(priv);
 569		else
 570			mt6358_disable_wov_phase2(priv);
 571
 572		priv->wov_enabled = enabled;
 573
 574		return 1;
 575	}
 576
 577	return 0;
 578}
 579
 580static int mt6358_dmic_mode_get(struct snd_kcontrol *kcontrol,
 581				struct snd_ctl_elem_value *ucontrol)
 582{
 583	struct snd_soc_component *c = snd_soc_kcontrol_component(kcontrol);
 584	struct mt6358_priv *priv = snd_soc_component_get_drvdata(c);
 585
 586	ucontrol->value.integer.value[0] = priv->dmic_one_wire_mode;
 587	dev_dbg(priv->dev, "%s() dmic_mode = %d", __func__, priv->dmic_one_wire_mode);
 588
 589	return 0;
 590}
 591
 592static int mt6358_dmic_mode_set(struct snd_kcontrol *kcontrol,
 593				struct snd_ctl_elem_value *ucontrol)
 594{
 595	struct snd_soc_component *c = snd_soc_kcontrol_component(kcontrol);
 596	struct mt6358_priv *priv = snd_soc_component_get_drvdata(c);
 597	int enabled = ucontrol->value.integer.value[0];
 598
 599	if (enabled < 0 || enabled > 1)
 600		return -EINVAL;
 601
 602	if (priv->dmic_one_wire_mode != enabled) {
 603		priv->dmic_one_wire_mode = enabled;
 604		dev_dbg(priv->dev, "%s() dmic_mode = %d", __func__, priv->dmic_one_wire_mode);
 605
 606		return 1;
 607	}
 608	dev_dbg(priv->dev, "%s() dmic_mode = %d", __func__, priv->dmic_one_wire_mode);
 609
 610	return 0;
 611}
 612
 613static const DECLARE_TLV_DB_SCALE(playback_tlv, -1000, 100, 0);
 614static const DECLARE_TLV_DB_SCALE(pga_tlv, 0, 600, 0);
 615
 616static const struct snd_kcontrol_new mt6358_snd_controls[] = {
 617	/* dl pga gain */
 618	SOC_DOUBLE_EXT_TLV("Headphone Volume",
 619			   MT6358_ZCD_CON2, 0, 7, 0x12, 1,
 620			   snd_soc_get_volsw, mt6358_put_volsw, playback_tlv),
 621	SOC_DOUBLE_EXT_TLV("Lineout Volume",
 622			   MT6358_ZCD_CON1, 0, 7, 0x12, 1,
 623			   snd_soc_get_volsw, mt6358_put_volsw, playback_tlv),
 624	SOC_SINGLE_EXT_TLV("Handset Volume",
 625			   MT6358_ZCD_CON3, 0, 0x12, 1,
 626			   snd_soc_get_volsw, mt6358_put_volsw, playback_tlv),
 627	/* ul pga gain */
 628	SOC_DOUBLE_R_EXT_TLV("PGA Volume",
 629			     MT6358_AUDENC_ANA_CON0, MT6358_AUDENC_ANA_CON1,
 630			     8, 4, 0,
 631			     snd_soc_get_volsw, mt6358_put_volsw, pga_tlv),
 632
 633	SOC_SINGLE_BOOL_EXT("Wake-on-Voice Phase2 Switch", 0,
 634			    mt6358_get_wov, mt6358_put_wov),
 635
 636	SOC_SINGLE_BOOL_EXT("Dmic Mode Switch", 0,
 637			    mt6358_dmic_mode_get, mt6358_dmic_mode_set),
 638};
 639
 640/* MUX */
 641/* LOL MUX */
 642static const char * const lo_in_mux_map[] = {
 643	"Open", "Mute", "Playback", "Test Mode"
 644};
 645
 646static int lo_in_mux_map_value[] = {
 647	0x0, 0x1, 0x2, 0x3,
 648};
 649
 650static SOC_VALUE_ENUM_SINGLE_DECL(lo_in_mux_map_enum,
 651				  MT6358_AUDDEC_ANA_CON7,
 652				  RG_AUDLOLMUXINPUTSEL_VAUDP15_SFT,
 653				  RG_AUDLOLMUXINPUTSEL_VAUDP15_MASK,
 654				  lo_in_mux_map,
 655				  lo_in_mux_map_value);
 656
 657static const struct snd_kcontrol_new lo_in_mux_control =
 658	SOC_DAPM_ENUM("In Select", lo_in_mux_map_enum);
 659
 660/*HP MUX */
 661enum {
 662	HP_MUX_OPEN = 0,
 663	HP_MUX_HPSPK,
 664	HP_MUX_HP,
 665	HP_MUX_TEST_MODE,
 666	HP_MUX_HP_IMPEDANCE,
 667	HP_MUX_MASK = 0x7,
 668};
 669
 670static const char * const hp_in_mux_map[] = {
 671	"Open",
 672	"LoudSPK Playback",
 673	"Audio Playback",
 674	"Test Mode",
 675	"HP Impedance",
 
 
 
 676};
 677
 678static int hp_in_mux_map_value[] = {
 679	HP_MUX_OPEN,
 680	HP_MUX_HPSPK,
 681	HP_MUX_HP,
 682	HP_MUX_TEST_MODE,
 683	HP_MUX_HP_IMPEDANCE,
 
 
 
 684};
 685
 686static SOC_VALUE_ENUM_SINGLE_DECL(hpl_in_mux_map_enum,
 687				  SND_SOC_NOPM,
 688				  0,
 689				  HP_MUX_MASK,
 690				  hp_in_mux_map,
 691				  hp_in_mux_map_value);
 692
 693static const struct snd_kcontrol_new hpl_in_mux_control =
 694	SOC_DAPM_ENUM("HPL Select", hpl_in_mux_map_enum);
 695
 696static SOC_VALUE_ENUM_SINGLE_DECL(hpr_in_mux_map_enum,
 697				  SND_SOC_NOPM,
 698				  0,
 699				  HP_MUX_MASK,
 700				  hp_in_mux_map,
 701				  hp_in_mux_map_value);
 702
 703static const struct snd_kcontrol_new hpr_in_mux_control =
 704	SOC_DAPM_ENUM("HPR Select", hpr_in_mux_map_enum);
 705
 706/* RCV MUX */
 707enum {
 708	RCV_MUX_OPEN = 0,
 709	RCV_MUX_MUTE,
 710	RCV_MUX_VOICE_PLAYBACK,
 711	RCV_MUX_TEST_MODE,
 712	RCV_MUX_MASK = 0x3,
 713};
 714
 715static const char * const rcv_in_mux_map[] = {
 716	"Open", "Mute", "Voice Playback", "Test Mode"
 717};
 718
 719static int rcv_in_mux_map_value[] = {
 720	RCV_MUX_OPEN,
 721	RCV_MUX_MUTE,
 722	RCV_MUX_VOICE_PLAYBACK,
 723	RCV_MUX_TEST_MODE,
 724};
 725
 726static SOC_VALUE_ENUM_SINGLE_DECL(rcv_in_mux_map_enum,
 727				  SND_SOC_NOPM,
 728				  0,
 729				  RCV_MUX_MASK,
 730				  rcv_in_mux_map,
 731				  rcv_in_mux_map_value);
 732
 733static const struct snd_kcontrol_new rcv_in_mux_control =
 734	SOC_DAPM_ENUM("RCV Select", rcv_in_mux_map_enum);
 735
 736/* DAC In MUX */
 737static const char * const dac_in_mux_map[] = {
 738	"Normal Path", "Sgen"
 739};
 740
 741static int dac_in_mux_map_value[] = {
 742	0x0, 0x1,
 743};
 744
 745static SOC_VALUE_ENUM_SINGLE_DECL(dac_in_mux_map_enum,
 746				  MT6358_AFE_TOP_CON0,
 747				  DL_SINE_ON_SFT,
 748				  DL_SINE_ON_MASK,
 749				  dac_in_mux_map,
 750				  dac_in_mux_map_value);
 751
 752static const struct snd_kcontrol_new dac_in_mux_control =
 753	SOC_DAPM_ENUM("DAC Select", dac_in_mux_map_enum);
 754
 755/* AIF Out MUX */
 756static SOC_VALUE_ENUM_SINGLE_DECL(aif_out_mux_map_enum,
 757				  MT6358_AFE_TOP_CON0,
 758				  UL_SINE_ON_SFT,
 759				  UL_SINE_ON_MASK,
 760				  dac_in_mux_map,
 761				  dac_in_mux_map_value);
 762
 763static const struct snd_kcontrol_new aif_out_mux_control =
 764	SOC_DAPM_ENUM("AIF Out Select", aif_out_mux_map_enum);
 765
 766/* Mic Type MUX */
 767enum {
 768	MIC_TYPE_MUX_IDLE = 0,
 769	MIC_TYPE_MUX_ACC,
 770	MIC_TYPE_MUX_DMIC,
 771	MIC_TYPE_MUX_DCC,
 772	MIC_TYPE_MUX_DCC_ECM_DIFF,
 773	MIC_TYPE_MUX_DCC_ECM_SINGLE,
 774	MIC_TYPE_MUX_MASK = 0x7,
 775};
 776
 777#define IS_DCC_BASE(type) ((type) == MIC_TYPE_MUX_DCC || \
 778			(type) == MIC_TYPE_MUX_DCC_ECM_DIFF || \
 779			(type) == MIC_TYPE_MUX_DCC_ECM_SINGLE)
 780
 781static const char * const mic_type_mux_map[] = {
 782	"Idle",
 783	"ACC",
 784	"DMIC",
 785	"DCC",
 786	"DCC_ECM_DIFF",
 787	"DCC_ECM_SINGLE",
 788};
 789
 790static int mic_type_mux_map_value[] = {
 791	MIC_TYPE_MUX_IDLE,
 792	MIC_TYPE_MUX_ACC,
 793	MIC_TYPE_MUX_DMIC,
 794	MIC_TYPE_MUX_DCC,
 795	MIC_TYPE_MUX_DCC_ECM_DIFF,
 796	MIC_TYPE_MUX_DCC_ECM_SINGLE,
 797};
 798
 799static SOC_VALUE_ENUM_SINGLE_DECL(mic_type_mux_map_enum,
 800				  SND_SOC_NOPM,
 801				  0,
 802				  MIC_TYPE_MUX_MASK,
 803				  mic_type_mux_map,
 804				  mic_type_mux_map_value);
 805
 806static const struct snd_kcontrol_new mic_type_mux_control =
 807	SOC_DAPM_ENUM("Mic Type Select", mic_type_mux_map_enum);
 808
 809/* ADC L MUX */
 810enum {
 811	ADC_MUX_IDLE = 0,
 812	ADC_MUX_AIN0,
 813	ADC_MUX_PREAMPLIFIER,
 814	ADC_MUX_IDLE1,
 815	ADC_MUX_MASK = 0x3,
 816};
 817
 818static const char * const adc_left_mux_map[] = {
 819	"Idle", "AIN0", "Left Preamplifier", "Idle_1"
 820};
 821
 822static int adc_mux_map_value[] = {
 823	ADC_MUX_IDLE,
 824	ADC_MUX_AIN0,
 825	ADC_MUX_PREAMPLIFIER,
 826	ADC_MUX_IDLE1,
 827};
 828
 829static SOC_VALUE_ENUM_SINGLE_DECL(adc_left_mux_map_enum,
 830				  SND_SOC_NOPM,
 831				  0,
 832				  ADC_MUX_MASK,
 833				  adc_left_mux_map,
 834				  adc_mux_map_value);
 835
 836static const struct snd_kcontrol_new adc_left_mux_control =
 837	SOC_DAPM_ENUM("ADC L Select", adc_left_mux_map_enum);
 838
 839/* ADC R MUX */
 840static const char * const adc_right_mux_map[] = {
 841	"Idle", "AIN0", "Right Preamplifier", "Idle_1"
 842};
 843
 844static SOC_VALUE_ENUM_SINGLE_DECL(adc_right_mux_map_enum,
 845				  SND_SOC_NOPM,
 846				  0,
 847				  ADC_MUX_MASK,
 848				  adc_right_mux_map,
 849				  adc_mux_map_value);
 850
 851static const struct snd_kcontrol_new adc_right_mux_control =
 852	SOC_DAPM_ENUM("ADC R Select", adc_right_mux_map_enum);
 853
 854/* PGA L MUX */
 855enum {
 856	PGA_MUX_NONE = 0,
 857	PGA_MUX_AIN0,
 858	PGA_MUX_AIN1,
 859	PGA_MUX_AIN2,
 860	PGA_MUX_MASK = 0x3,
 861};
 862
 863static const char * const pga_mux_map[] = {
 864	"None", "AIN0", "AIN1", "AIN2"
 865};
 866
 867static int pga_mux_map_value[] = {
 868	PGA_MUX_NONE,
 869	PGA_MUX_AIN0,
 870	PGA_MUX_AIN1,
 871	PGA_MUX_AIN2,
 872};
 873
 874static SOC_VALUE_ENUM_SINGLE_DECL(pga_left_mux_map_enum,
 875				  SND_SOC_NOPM,
 876				  0,
 877				  PGA_MUX_MASK,
 878				  pga_mux_map,
 879				  pga_mux_map_value);
 880
 881static const struct snd_kcontrol_new pga_left_mux_control =
 882	SOC_DAPM_ENUM("PGA L Select", pga_left_mux_map_enum);
 883
 884/* PGA R MUX */
 885static SOC_VALUE_ENUM_SINGLE_DECL(pga_right_mux_map_enum,
 886				  SND_SOC_NOPM,
 887				  0,
 888				  PGA_MUX_MASK,
 889				  pga_mux_map,
 890				  pga_mux_map_value);
 891
 892static const struct snd_kcontrol_new pga_right_mux_control =
 893	SOC_DAPM_ENUM("PGA R Select", pga_right_mux_map_enum);
 894
 895static int mt_clksq_event(struct snd_soc_dapm_widget *w,
 896			  struct snd_kcontrol *kcontrol,
 897			  int event)
 898{
 899	struct snd_soc_component *cmpnt = snd_soc_dapm_to_component(w->dapm);
 900	struct mt6358_priv *priv = snd_soc_component_get_drvdata(cmpnt);
 901
 902	dev_dbg(priv->dev, "%s(), event = 0x%x\n", __func__, event);
 903
 904	switch (event) {
 905	case SND_SOC_DAPM_PRE_PMU:
 906		/* audio clk source from internal dcxo */
 907		regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON6,
 908				   RG_CLKSQ_IN_SEL_TEST_MASK_SFT,
 909				   0x0);
 910		break;
 911	default:
 912		break;
 913	}
 914
 915	return 0;
 916}
 917
 918static int mt_sgen_event(struct snd_soc_dapm_widget *w,
 919			 struct snd_kcontrol *kcontrol,
 920			 int event)
 921{
 922	struct snd_soc_component *cmpnt = snd_soc_dapm_to_component(w->dapm);
 923	struct mt6358_priv *priv = snd_soc_component_get_drvdata(cmpnt);
 924
 925	dev_dbg(priv->dev, "%s(), event = 0x%x\n", __func__, event);
 926
 927	switch (event) {
 928	case SND_SOC_DAPM_PRE_PMU:
 929		/* sdm audio fifo clock power on */
 930		regmap_write(priv->regmap, MT6358_AFUNC_AUD_CON2, 0x0006);
 931		/* scrambler clock on enable */
 932		regmap_write(priv->regmap, MT6358_AFUNC_AUD_CON0, 0xCBA1);
 933		/* sdm power on */
 934		regmap_write(priv->regmap, MT6358_AFUNC_AUD_CON2, 0x0003);
 935		/* sdm fifo enable */
 936		regmap_write(priv->regmap, MT6358_AFUNC_AUD_CON2, 0x000B);
 937
 938		regmap_update_bits(priv->regmap, MT6358_AFE_SGEN_CFG0,
 939				   0xff3f,
 940				   0x0000);
 941		regmap_update_bits(priv->regmap, MT6358_AFE_SGEN_CFG1,
 942				   0xffff,
 943				   0x0001);
 944		break;
 945	case SND_SOC_DAPM_POST_PMD:
 946		/* DL scrambler disabling sequence */
 947		regmap_write(priv->regmap, MT6358_AFUNC_AUD_CON2, 0x0000);
 948		regmap_write(priv->regmap, MT6358_AFUNC_AUD_CON0, 0xcba0);
 949		break;
 950	default:
 951		break;
 952	}
 953
 954	return 0;
 955}
 956
 957static int mt_aif_in_event(struct snd_soc_dapm_widget *w,
 958			   struct snd_kcontrol *kcontrol,
 959			   int event)
 960{
 961	struct snd_soc_component *cmpnt = snd_soc_dapm_to_component(w->dapm);
 962	struct mt6358_priv *priv = snd_soc_component_get_drvdata(cmpnt);
 963
 964	dev_info(priv->dev, "%s(), event 0x%x, rate %d\n",
 965		 __func__, event, priv->dl_rate);
 966
 967	switch (event) {
 968	case SND_SOC_DAPM_PRE_PMU:
 969		playback_gpio_set(priv);
 970
 971		/* sdm audio fifo clock power on */
 972		regmap_write(priv->regmap, MT6358_AFUNC_AUD_CON2, 0x0006);
 973		/* scrambler clock on enable */
 974		regmap_write(priv->regmap, MT6358_AFUNC_AUD_CON0, 0xCBA1);
 975		/* sdm power on */
 976		regmap_write(priv->regmap, MT6358_AFUNC_AUD_CON2, 0x0003);
 977		/* sdm fifo enable */
 978		regmap_write(priv->regmap, MT6358_AFUNC_AUD_CON2, 0x000B);
 979		break;
 980	case SND_SOC_DAPM_POST_PMD:
 981		/* DL scrambler disabling sequence */
 982		regmap_write(priv->regmap, MT6358_AFUNC_AUD_CON2, 0x0000);
 983		regmap_write(priv->regmap, MT6358_AFUNC_AUD_CON0, 0xcba0);
 984
 985		playback_gpio_reset(priv);
 986		break;
 987	default:
 988		break;
 989	}
 990
 991	return 0;
 992}
 993
 994static int mtk_hp_enable(struct mt6358_priv *priv)
 995{
 996	/* Pull-down HPL/R to AVSS28_AUD */
 997	hp_pull_down(priv, true);
 998	/* release HP CMFB gate rstb */
 999	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON4,
1000			   0x1 << 6, 0x1 << 6);
1001
1002	/* Reduce ESD resistance of AU_REFN */
1003	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON2, 0x4000);
1004
1005	/* Set HPR/HPL gain as minimum (~ -40dB) */
1006	regmap_write(priv->regmap, MT6358_ZCD_CON2, DL_GAIN_N_40DB_REG);
1007
1008	/* Turn on DA_600K_NCP_VA18 */
1009	regmap_write(priv->regmap, MT6358_AUDNCP_CLKDIV_CON1, 0x0001);
1010	/* Set NCP clock as 604kHz // 26MHz/43 = 604KHz */
1011	regmap_write(priv->regmap, MT6358_AUDNCP_CLKDIV_CON2, 0x002c);
1012	/* Toggle RG_DIVCKS_CHG */
1013	regmap_write(priv->regmap, MT6358_AUDNCP_CLKDIV_CON0, 0x0001);
1014	/* Set NCP soft start mode as default mode: 100us */
1015	regmap_write(priv->regmap, MT6358_AUDNCP_CLKDIV_CON4, 0x0003);
1016	/* Enable NCP */
1017	regmap_write(priv->regmap, MT6358_AUDNCP_CLKDIV_CON3, 0x0000);
1018	usleep_range(250, 270);
1019
1020	/* Enable cap-less LDOs (1.5V) */
1021	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON14,
1022			   0x1055, 0x1055);
1023	/* Enable NV regulator (-1.2V) */
1024	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON15, 0x0001);
1025	usleep_range(100, 120);
1026
1027	/* Disable AUD_ZCD */
1028	hp_zcd_disable(priv);
1029
1030	/* Disable headphone short-circuit protection */
1031	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON0, 0x3000);
1032
1033	/* Enable IBIST */
1034	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON12, 0x0055);
1035
1036	/* Set HP DR bias current optimization, 010: 6uA */
1037	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON11, 0x4900);
1038	/* Set HP & ZCD bias current optimization */
1039	/* 01: ZCD: 4uA, HP/HS/LO: 5uA */
1040	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON12, 0x0055);
1041	/* Set HPP/N STB enhance circuits */
1042	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON2, 0x4033);
1043
1044	/* Enable HP aux output stage */
1045	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON1, 0x000c);
1046	/* Enable HP aux feedback loop */
1047	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON1, 0x003c);
1048	/* Enable HP aux CMFB loop */
1049	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON9, 0x0c00);
1050	/* Enable HP driver bias circuits */
1051	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON0, 0x30c0);
1052	/* Enable HP driver core circuits */
1053	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON0, 0x30f0);
1054	/* Short HP main output to HP aux output stage */
1055	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON1, 0x00fc);
1056
1057	/* Enable HP main CMFB loop */
1058	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON9, 0x0e00);
1059	/* Disable HP aux CMFB loop */
1060	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON9, 0x0200);
1061
1062	/* Select CMFB resistor bulk to AC mode */
1063	/* Selec HS/LO cap size (6.5pF default) */
1064	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON10, 0x0000);
1065
1066	/* Enable HP main output stage */
1067	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON1, 0x00ff);
1068	/* Enable HPR/L main output stage step by step */
1069	hp_main_output_ramp(priv, true);
1070
1071	/* Reduce HP aux feedback loop gain */
1072	hp_aux_feedback_loop_gain_ramp(priv, true);
1073	/* Disable HP aux feedback loop */
1074	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON1, 0x3fcf);
1075
1076	/* apply volume setting */
1077	headset_volume_ramp(priv,
1078			    DL_GAIN_N_10DB,
1079			    priv->ana_gain[AUDIO_ANALOG_VOLUME_HPOUTL]);
1080
1081	/* Disable HP aux output stage */
1082	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON1, 0x3fc3);
1083	/* Unshort HP main output to HP aux output stage */
1084	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON1, 0x3f03);
1085	usleep_range(100, 120);
1086
1087	/* Enable AUD_CLK */
1088	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON13, 0x1, 0x1);
1089	/* Enable Audio DAC  */
1090	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON0, 0x30ff);
1091	/* Enable low-noise mode of DAC */
1092	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON9, 0xf201);
1093	usleep_range(100, 120);
1094
1095	/* Switch HPL MUX to audio DAC */
1096	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON0, 0x32ff);
1097	/* Switch HPR MUX to audio DAC */
1098	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON0, 0x3aff);
1099
1100	/* Disable Pull-down HPL/R to AVSS28_AUD */
1101	hp_pull_down(priv, false);
1102
1103	return 0;
1104}
1105
1106static int mtk_hp_disable(struct mt6358_priv *priv)
1107{
1108	/* Pull-down HPL/R to AVSS28_AUD */
1109	hp_pull_down(priv, true);
1110
1111	/* HPR/HPL mux to open */
1112	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON0,
1113			   0x0f00, 0x0000);
1114
1115	/* Disable low-noise mode of DAC */
1116	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON9,
1117			   0x0001, 0x0000);
1118
1119	/* Disable Audio DAC */
1120	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON0,
1121			   0x000f, 0x0000);
1122
1123	/* Disable AUD_CLK */
1124	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON13, 0x1, 0x0);
1125
1126	/* Short HP main output to HP aux output stage */
1127	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON1, 0x3fc3);
1128	/* Enable HP aux output stage */
1129	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON1, 0x3fcf);
1130
1131	/* decrease HPL/R gain to normal gain step by step */
1132	headset_volume_ramp(priv,
1133			    priv->ana_gain[AUDIO_ANALOG_VOLUME_HPOUTL],
1134			    DL_GAIN_N_40DB);
1135
1136	/* Enable HP aux feedback loop */
1137	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON1, 0x3fff);
1138
1139	/* Reduce HP aux feedback loop gain */
1140	hp_aux_feedback_loop_gain_ramp(priv, false);
1141
1142	/* decrease HPR/L main output stage step by step */
1143	hp_main_output_ramp(priv, false);
1144
1145	/* Disable HP main output stage */
1146	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON1, 0x3, 0x0);
1147
1148	/* Enable HP aux CMFB loop */
1149	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON9, 0x0e00);
1150
1151	/* Disable HP main CMFB loop */
1152	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON9, 0x0c00);
1153
1154	/* Unshort HP main output to HP aux output stage */
1155	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON1,
1156			   0x3 << 6, 0x0);
1157
1158	/* Disable HP driver core circuits */
1159	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON0,
1160			   0x3 << 4, 0x0);
1161
1162	/* Disable HP driver bias circuits */
1163	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON0,
1164			   0x3 << 6, 0x0);
1165
1166	/* Disable HP aux CMFB loop */
1167	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON9, 0x0000);
1168
1169	/* Disable HP aux feedback loop */
1170	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON1,
1171			   0x3 << 4, 0x0);
1172
1173	/* Disable HP aux output stage */
1174	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON1,
1175			   0x3 << 2, 0x0);
1176
1177	/* Disable IBIST */
1178	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON12,
1179			   0x1 << 8, 0x1 << 8);
1180
1181	/* Disable NV regulator (-1.2V) */
1182	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON15, 0x1, 0x0);
1183	/* Disable cap-less LDOs (1.5V) */
1184	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON14,
1185			   0x1055, 0x0);
1186	/* Disable NCP */
1187	regmap_update_bits(priv->regmap, MT6358_AUDNCP_CLKDIV_CON3,
1188			   0x1, 0x1);
1189
1190	/* Increase ESD resistance of AU_REFN */
1191	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON2,
1192			   0x1 << 14, 0x0);
1193
1194	/* Set HP CMFB gate rstb */
1195	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON4,
1196			   0x1 << 6, 0x0);
1197	/* disable Pull-down HPL/R to AVSS28_AUD */
1198	hp_pull_down(priv, false);
1199
1200	return 0;
1201}
1202
1203static int mtk_hp_spk_enable(struct mt6358_priv *priv)
1204{
1205	/* Pull-down HPL/R to AVSS28_AUD */
1206	hp_pull_down(priv, true);
1207	/* release HP CMFB gate rstb */
1208	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON4,
1209			   0x1 << 6, 0x1 << 6);
1210
1211	/* Reduce ESD resistance of AU_REFN */
1212	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON2, 0x4000);
1213
1214	/* Set HPR/HPL gain to -10dB */
1215	regmap_write(priv->regmap, MT6358_ZCD_CON2, DL_GAIN_N_10DB_REG);
1216
1217	/* Turn on DA_600K_NCP_VA18 */
1218	regmap_write(priv->regmap, MT6358_AUDNCP_CLKDIV_CON1, 0x0001);
1219	/* Set NCP clock as 604kHz // 26MHz/43 = 604KHz */
1220	regmap_write(priv->regmap, MT6358_AUDNCP_CLKDIV_CON2, 0x002c);
1221	/* Toggle RG_DIVCKS_CHG */
1222	regmap_write(priv->regmap, MT6358_AUDNCP_CLKDIV_CON0, 0x0001);
1223	/* Set NCP soft start mode as default mode: 100us */
1224	regmap_write(priv->regmap, MT6358_AUDNCP_CLKDIV_CON4, 0x0003);
1225	/* Enable NCP */
1226	regmap_write(priv->regmap, MT6358_AUDNCP_CLKDIV_CON3, 0x0000);
1227	usleep_range(250, 270);
1228
1229	/* Enable cap-less LDOs (1.5V) */
1230	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON14,
1231			   0x1055, 0x1055);
1232	/* Enable NV regulator (-1.2V) */
1233	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON15, 0x0001);
1234	usleep_range(100, 120);
1235
1236	/* Disable AUD_ZCD */
1237	hp_zcd_disable(priv);
1238
1239	/* Disable headphone short-circuit protection */
1240	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON0, 0x3000);
1241
1242	/* Enable IBIST */
1243	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON12, 0x0055);
1244
1245	/* Set HP DR bias current optimization, 010: 6uA */
1246	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON11, 0x4900);
1247	/* Set HP & ZCD bias current optimization */
1248	/* 01: ZCD: 4uA, HP/HS/LO: 5uA */
1249	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON12, 0x0055);
1250	/* Set HPP/N STB enhance circuits */
1251	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON2, 0x4033);
1252
1253	/* Disable Pull-down HPL/R to AVSS28_AUD */
1254	hp_pull_down(priv, false);
1255
1256	/* Enable HP driver bias circuits */
1257	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON0, 0x30c0);
1258	/* Enable HP driver core circuits */
1259	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON0, 0x30f0);
1260	/* Enable HP main CMFB loop */
1261	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON9, 0x0200);
1262
1263	/* Select CMFB resistor bulk to AC mode */
1264	/* Selec HS/LO cap size (6.5pF default) */
1265	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON10, 0x0000);
1266
1267	/* Enable HP main output stage */
1268	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON1, 0x0003);
1269	/* Enable HPR/L main output stage step by step */
1270	hp_main_output_ramp(priv, true);
1271
1272	/* Set LO gain as minimum (~ -40dB) */
1273	regmap_write(priv->regmap, MT6358_ZCD_CON1, DL_GAIN_N_40DB_REG);
1274	/* apply volume setting */
1275	headset_volume_ramp(priv,
1276			    DL_GAIN_N_10DB,
1277			    priv->ana_gain[AUDIO_ANALOG_VOLUME_HPOUTL]);
1278
1279	/* Set LO STB enhance circuits */
1280	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON7, 0x0110);
1281	/* Enable LO driver bias circuits */
1282	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON7, 0x0112);
1283	/* Enable LO driver core circuits */
1284	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON7, 0x0113);
1285
1286	/* Set LOL gain to normal gain step by step */
1287	regmap_update_bits(priv->regmap, MT6358_ZCD_CON1,
1288			   RG_AUDLOLGAIN_MASK_SFT,
1289			   priv->ana_gain[AUDIO_ANALOG_VOLUME_LINEOUTL] <<
1290			   RG_AUDLOLGAIN_SFT);
1291	regmap_update_bits(priv->regmap, MT6358_ZCD_CON1,
1292			   RG_AUDLORGAIN_MASK_SFT,
1293			   priv->ana_gain[AUDIO_ANALOG_VOLUME_LINEOUTR] <<
1294			   RG_AUDLORGAIN_SFT);
1295
1296	/* Enable AUD_CLK */
1297	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON13, 0x1, 0x1);
1298	/* Enable Audio DAC  */
1299	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON0, 0x30f9);
1300	/* Enable low-noise mode of DAC */
1301	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON9, 0x0201);
1302	/* Switch LOL MUX to audio DAC */
1303	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON7, 0x011b);
1304	/* Switch HPL/R MUX to Line-out */
1305	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON0, 0x35f9);
1306
1307	return 0;
1308}
1309
1310static int mtk_hp_spk_disable(struct mt6358_priv *priv)
1311{
1312	/* HPR/HPL mux to open */
1313	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON0,
1314			   0x0f00, 0x0000);
1315	/* LOL mux to open */
1316	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON7,
1317			   0x3 << 2, 0x0000);
1318
1319	/* Disable Audio DAC */
1320	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON0,
1321			   0x000f, 0x0000);
1322
1323	/* Disable AUD_CLK */
1324	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON13, 0x1, 0x0);
1325
1326	/* decrease HPL/R gain to normal gain step by step */
1327	headset_volume_ramp(priv,
1328			    priv->ana_gain[AUDIO_ANALOG_VOLUME_HPOUTL],
1329			    DL_GAIN_N_40DB);
1330
1331	/* decrease LOL gain to minimum gain step by step */
1332	regmap_update_bits(priv->regmap, MT6358_ZCD_CON1,
1333			   DL_GAIN_REG_MASK, DL_GAIN_N_40DB_REG);
1334
1335	/* decrease HPR/L main output stage step by step */
1336	hp_main_output_ramp(priv, false);
1337
1338	/* Disable HP main output stage */
1339	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON1, 0x3, 0x0);
1340
1341	/* Short HP main output to HP aux output stage */
1342	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON1, 0x3fc3);
1343	/* Enable HP aux output stage */
1344	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON1, 0x3fcf);
1345
1346	/* Enable HP aux feedback loop */
1347	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON1, 0x3fff);
1348
1349	/* Reduce HP aux feedback loop gain */
1350	hp_aux_feedback_loop_gain_ramp(priv, false);
1351
1352	/* Disable HP driver core circuits */
1353	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON0,
1354			   0x3 << 4, 0x0);
1355	/* Disable LO driver core circuits */
1356	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON7,
1357			   0x1, 0x0);
1358
1359	/* Disable HP driver bias circuits */
1360	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON0,
1361			   0x3 << 6, 0x0);
1362	/* Disable LO driver bias circuits */
1363	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON7,
1364			   0x1 << 1, 0x0);
1365
1366	/* Disable HP aux CMFB loop */
1367	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON9,
1368			   0xff << 8, 0x0000);
1369
1370	/* Disable IBIST */
1371	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON12,
1372			   0x1 << 8, 0x1 << 8);
1373	/* Disable NV regulator (-1.2V) */
1374	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON15, 0x1, 0x0);
1375	/* Disable cap-less LDOs (1.5V) */
1376	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON14, 0x1055, 0x0);
1377	/* Disable NCP */
1378	regmap_update_bits(priv->regmap, MT6358_AUDNCP_CLKDIV_CON3, 0x1, 0x1);
1379
1380	/* Set HP CMFB gate rstb */
1381	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON4,
1382			   0x1 << 6, 0x0);
1383	/* disable Pull-down HPL/R to AVSS28_AUD */
1384	hp_pull_down(priv, false);
1385
1386	return 0;
1387}
1388
1389static int mt_hp_event(struct snd_soc_dapm_widget *w,
1390		       struct snd_kcontrol *kcontrol,
1391		       int event)
1392{
1393	struct snd_soc_component *cmpnt = snd_soc_dapm_to_component(w->dapm);
1394	struct mt6358_priv *priv = snd_soc_component_get_drvdata(cmpnt);
1395	unsigned int mux = dapm_kcontrol_get_value(w->kcontrols[0]);
1396	int device = DEVICE_HP;
1397
1398	dev_info(priv->dev, "%s(), event 0x%x, dev_counter[DEV_HP] %d, mux %u\n",
1399		 __func__,
1400		 event,
1401		 priv->dev_counter[device],
1402		 mux);
1403
1404	switch (event) {
1405	case SND_SOC_DAPM_PRE_PMU:
1406		priv->dev_counter[device]++;
1407		if (priv->dev_counter[device] > 1)
1408			break;	/* already enabled, do nothing */
1409		else if (priv->dev_counter[device] <= 0)
1410			dev_warn(priv->dev, "%s(), dev_counter[DEV_HP] %d <= 0\n",
1411				 __func__,
1412				 priv->dev_counter[device]);
1413
1414		priv->mux_select[MUX_HP_L] = mux;
1415
1416		if (mux == HP_MUX_HP)
1417			mtk_hp_enable(priv);
1418		else if (mux == HP_MUX_HPSPK)
1419			mtk_hp_spk_enable(priv);
1420		break;
1421	case SND_SOC_DAPM_PRE_PMD:
1422		priv->dev_counter[device]--;
1423		if (priv->dev_counter[device] > 0) {
1424			break;	/* still being used, don't close */
1425		} else if (priv->dev_counter[device] < 0) {
1426			dev_warn(priv->dev, "%s(), dev_counter[DEV_HP] %d < 0\n",
1427				 __func__,
1428				 priv->dev_counter[device]);
1429			priv->dev_counter[device] = 0;
1430			break;
1431		}
1432
1433		if (priv->mux_select[MUX_HP_L] == HP_MUX_HP)
1434			mtk_hp_disable(priv);
1435		else if (priv->mux_select[MUX_HP_L] == HP_MUX_HPSPK)
1436			mtk_hp_spk_disable(priv);
1437
1438		priv->mux_select[MUX_HP_L] = mux;
1439		break;
1440	default:
1441		break;
1442	}
1443
1444	return 0;
1445}
1446
1447static int mt_rcv_event(struct snd_soc_dapm_widget *w,
1448			struct snd_kcontrol *kcontrol,
1449			int event)
1450{
1451	struct snd_soc_component *cmpnt = snd_soc_dapm_to_component(w->dapm);
1452	struct mt6358_priv *priv = snd_soc_component_get_drvdata(cmpnt);
1453
1454	dev_info(priv->dev, "%s(), event 0x%x, mux %u\n",
1455		 __func__,
1456		 event,
1457		 dapm_kcontrol_get_value(w->kcontrols[0]));
1458
1459	switch (event) {
1460	case SND_SOC_DAPM_PRE_PMU:
1461		/* Reduce ESD resistance of AU_REFN */
1462		regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON2, 0x4000);
1463
1464		/* Turn on DA_600K_NCP_VA18 */
1465		regmap_write(priv->regmap, MT6358_AUDNCP_CLKDIV_CON1, 0x0001);
1466		/* Set NCP clock as 604kHz // 26MHz/43 = 604KHz */
1467		regmap_write(priv->regmap, MT6358_AUDNCP_CLKDIV_CON2, 0x002c);
1468		/* Toggle RG_DIVCKS_CHG */
1469		regmap_write(priv->regmap, MT6358_AUDNCP_CLKDIV_CON0, 0x0001);
1470		/* Set NCP soft start mode as default mode: 100us */
1471		regmap_write(priv->regmap, MT6358_AUDNCP_CLKDIV_CON4, 0x0003);
1472		/* Enable NCP */
1473		regmap_write(priv->regmap, MT6358_AUDNCP_CLKDIV_CON3, 0x0000);
1474		usleep_range(250, 270);
1475
1476		/* Enable cap-less LDOs (1.5V) */
1477		regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON14,
1478				   0x1055, 0x1055);
1479		/* Enable NV regulator (-1.2V) */
1480		regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON15, 0x0001);
1481		usleep_range(100, 120);
1482
1483		/* Disable AUD_ZCD */
1484		hp_zcd_disable(priv);
1485
1486		/* Disable handset short-circuit protection */
1487		regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON6, 0x0010);
1488
1489		/* Enable IBIST */
1490		regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON12, 0x0055);
1491		/* Set HP DR bias current optimization, 010: 6uA */
1492		regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON11, 0x4900);
1493		/* Set HP & ZCD bias current optimization */
1494		/* 01: ZCD: 4uA, HP/HS/LO: 5uA */
1495		regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON12, 0x0055);
1496		/* Set HS STB enhance circuits */
1497		regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON6, 0x0090);
1498
1499		/* Disable HP main CMFB loop */
1500		regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON9, 0x0000);
1501		/* Select CMFB resistor bulk to AC mode */
1502		/* Selec HS/LO cap size (6.5pF default) */
1503		regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON10, 0x0000);
1504
1505		/* Enable HS driver bias circuits */
1506		regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON6, 0x0092);
1507		/* Enable HS driver core circuits */
1508		regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON6, 0x0093);
1509
1510		/* Enable AUD_CLK */
1511		regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON13,
1512				   0x1, 0x1);
1513
1514		/* Enable Audio DAC  */
1515		regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON0, 0x0009);
1516		/* Enable low-noise mode of DAC */
1517		regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON9, 0x0001);
1518		/* Switch HS MUX to audio DAC */
1519		regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON6, 0x009b);
1520		break;
1521	case SND_SOC_DAPM_PRE_PMD:
1522		/* HS mux to open */
1523		regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON6,
1524				   RG_AUDHSMUXINPUTSEL_VAUDP15_MASK_SFT,
1525				   RCV_MUX_OPEN);
1526
1527		/* Disable Audio DAC */
1528		regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON0,
1529				   0x000f, 0x0000);
1530
1531		/* Disable AUD_CLK */
1532		regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON13,
1533				   0x1, 0x0);
1534
1535		/* decrease HS gain to minimum gain step by step */
1536		regmap_write(priv->regmap, MT6358_ZCD_CON3, DL_GAIN_N_40DB);
1537
1538		/* Disable HS driver core circuits */
1539		regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON6,
1540				   0x1, 0x0);
1541
1542		/* Disable HS driver bias circuits */
1543		regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON6,
1544				   0x1 << 1, 0x0000);
1545
1546		/* Disable HP aux CMFB loop */
1547		regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON9,
1548				   0xff << 8, 0x0);
1549
1550		/* Enable HP main CMFB Switch */
1551		regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON9,
1552				   0xff << 8, 0x2 << 8);
1553
1554		/* Disable IBIST */
1555		regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON12,
1556				   0x1 << 8, 0x1 << 8);
1557
1558		/* Disable NV regulator (-1.2V) */
1559		regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON15,
1560				   0x1, 0x0);
1561		/* Disable cap-less LDOs (1.5V) */
1562		regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON14,
1563				   0x1055, 0x0);
1564		/* Disable NCP */
1565		regmap_update_bits(priv->regmap, MT6358_AUDNCP_CLKDIV_CON3,
1566				   0x1, 0x1);
1567		break;
1568	default:
1569		break;
1570	}
1571
1572	return 0;
1573}
1574
1575static int mt_aif_out_event(struct snd_soc_dapm_widget *w,
1576			    struct snd_kcontrol *kcontrol,
1577			    int event)
1578{
1579	struct snd_soc_component *cmpnt = snd_soc_dapm_to_component(w->dapm);
1580	struct mt6358_priv *priv = snd_soc_component_get_drvdata(cmpnt);
1581
1582	dev_dbg(priv->dev, "%s(), event 0x%x, rate %d\n",
1583		__func__, event, priv->ul_rate);
1584
1585	switch (event) {
1586	case SND_SOC_DAPM_PRE_PMU:
1587		capture_gpio_set(priv);
1588		break;
1589	case SND_SOC_DAPM_POST_PMD:
1590		capture_gpio_reset(priv);
1591		break;
1592	default:
1593		break;
1594	}
1595
1596	return 0;
1597}
1598
1599static int mt_adc_supply_event(struct snd_soc_dapm_widget *w,
1600			       struct snd_kcontrol *kcontrol,
1601			       int event)
1602{
1603	struct snd_soc_component *cmpnt = snd_soc_dapm_to_component(w->dapm);
1604	struct mt6358_priv *priv = snd_soc_component_get_drvdata(cmpnt);
1605
1606	dev_dbg(priv->dev, "%s(), event 0x%x\n",
1607		__func__, event);
1608
1609	switch (event) {
1610	case SND_SOC_DAPM_PRE_PMU:
1611		/* Enable audio ADC CLKGEN  */
1612		regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON13,
1613				   0x1 << 5, 0x1 << 5);
1614		/* ADC CLK from CLKGEN (13MHz) */
1615		regmap_write(priv->regmap, MT6358_AUDENC_ANA_CON3,
1616			     0x0000);
1617		/* Enable  LCLDO_ENC 1P8V */
1618		regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON14,
1619				   0x2500, 0x0100);
1620		/* LCLDO_ENC remote sense */
1621		regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON14,
1622				   0x2500, 0x2500);
1623		break;
1624	case SND_SOC_DAPM_POST_PMD:
1625		/* LCLDO_ENC remote sense off */
1626		regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON14,
1627				   0x2500, 0x0100);
1628		/* disable LCLDO_ENC 1P8V */
1629		regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON14,
1630				   0x2500, 0x0000);
1631
1632		/* ADC CLK from CLKGEN (13MHz) */
1633		regmap_write(priv->regmap, MT6358_AUDENC_ANA_CON3, 0x0000);
1634		/* disable audio ADC CLKGEN  */
1635		regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON13,
1636				   0x1 << 5, 0x0 << 5);
1637		break;
1638	default:
1639		break;
1640	}
1641
1642	return 0;
1643}
1644
1645static int mt6358_amic_enable(struct mt6358_priv *priv)
1646{
1647	unsigned int mic_type = priv->mux_select[MUX_MIC_TYPE];
1648	unsigned int mux_pga_l = priv->mux_select[MUX_PGA_L];
1649	unsigned int mux_pga_r = priv->mux_select[MUX_PGA_R];
1650
1651	dev_info(priv->dev, "%s(), mux, mic %u, pga l %u, pga r %u\n",
1652		 __func__, mic_type, mux_pga_l, mux_pga_r);
1653
1654	if (IS_DCC_BASE(mic_type)) {
1655		/* DCC 50k CLK (from 26M) */
1656		regmap_write(priv->regmap, MT6358_AFE_DCCLK_CFG0, 0x2062);
1657		regmap_write(priv->regmap, MT6358_AFE_DCCLK_CFG0, 0x2062);
1658		regmap_write(priv->regmap, MT6358_AFE_DCCLK_CFG0, 0x2060);
1659		regmap_write(priv->regmap, MT6358_AFE_DCCLK_CFG0, 0x2061);
1660		regmap_write(priv->regmap, MT6358_AFE_DCCLK_CFG1, 0x0100);
1661	}
1662
1663	/* mic bias 0 */
1664	if (mux_pga_l == PGA_MUX_AIN0 || mux_pga_l == PGA_MUX_AIN2 ||
1665	    mux_pga_r == PGA_MUX_AIN0 || mux_pga_r == PGA_MUX_AIN2) {
1666		switch (mic_type) {
1667		case MIC_TYPE_MUX_DCC_ECM_DIFF:
1668			regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON9,
1669					   0xff00, 0x7700);
1670			break;
1671		case MIC_TYPE_MUX_DCC_ECM_SINGLE:
1672			regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON9,
1673					   0xff00, 0x1100);
1674			break;
1675		default:
1676			regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON9,
1677					   0xff00, 0x0000);
1678			break;
1679		}
1680		/* Enable MICBIAS0, MISBIAS0 = 1P9V */
1681		regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON9,
1682				   0xff, 0x21);
1683	}
1684
1685	/* mic bias 1 */
1686	if (mux_pga_l == PGA_MUX_AIN1 || mux_pga_r == PGA_MUX_AIN1) {
1687		/* Enable MICBIAS1, MISBIAS1 = 2P6V */
1688		if (mic_type == MIC_TYPE_MUX_DCC_ECM_SINGLE)
1689			regmap_write(priv->regmap,
1690				     MT6358_AUDENC_ANA_CON10, 0x0161);
1691		else
1692			regmap_write(priv->regmap,
1693				     MT6358_AUDENC_ANA_CON10, 0x0061);
1694	}
1695
1696	if (IS_DCC_BASE(mic_type)) {
1697		/* Audio L/R preamplifier DCC precharge */
1698		regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON0,
1699				   0xf8ff, 0x0004);
1700		regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON1,
1701				   0xf8ff, 0x0004);
1702	} else {
1703		/* reset reg */
1704		regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON0,
1705				   0xf8ff, 0x0000);
1706		regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON1,
1707				   0xf8ff, 0x0000);
1708	}
1709
1710	if (mux_pga_l != PGA_MUX_NONE) {
1711		/* L preamplifier input sel */
1712		regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON0,
1713				   RG_AUDPREAMPLINPUTSEL_MASK_SFT,
1714				   mux_pga_l << RG_AUDPREAMPLINPUTSEL_SFT);
1715
1716		/* L preamplifier enable */
1717		regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON0,
1718				   RG_AUDPREAMPLON_MASK_SFT,
1719				   0x1 << RG_AUDPREAMPLON_SFT);
1720
1721		if (IS_DCC_BASE(mic_type)) {
1722			/* L preamplifier DCCEN */
1723			regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON0,
1724					   RG_AUDPREAMPLDCCEN_MASK_SFT,
1725					   0x1 << RG_AUDPREAMPLDCCEN_SFT);
1726		}
1727
1728		/* L ADC input sel : L PGA. Enable audio L ADC */
1729		regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON0,
1730				   RG_AUDADCLINPUTSEL_MASK_SFT,
1731				   ADC_MUX_PREAMPLIFIER <<
1732				   RG_AUDADCLINPUTSEL_SFT);
1733		regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON0,
1734				   RG_AUDADCLPWRUP_MASK_SFT,
1735				   0x1 << RG_AUDADCLPWRUP_SFT);
1736	}
1737
1738	if (mux_pga_r != PGA_MUX_NONE) {
1739		/* R preamplifier input sel */
1740		regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON1,
1741				   RG_AUDPREAMPRINPUTSEL_MASK_SFT,
1742				   mux_pga_r << RG_AUDPREAMPRINPUTSEL_SFT);
1743
1744		/* R preamplifier enable */
1745		regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON1,
1746				   RG_AUDPREAMPRON_MASK_SFT,
1747				   0x1 << RG_AUDPREAMPRON_SFT);
1748
1749		if (IS_DCC_BASE(mic_type)) {
1750			/* R preamplifier DCCEN */
1751			regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON1,
1752					   RG_AUDPREAMPRDCCEN_MASK_SFT,
1753					   0x1 << RG_AUDPREAMPRDCCEN_SFT);
1754		}
1755
1756		/* R ADC input sel : R PGA. Enable audio R ADC */
1757		regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON1,
1758				   RG_AUDADCRINPUTSEL_MASK_SFT,
1759				   ADC_MUX_PREAMPLIFIER <<
1760				   RG_AUDADCRINPUTSEL_SFT);
1761		regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON1,
1762				   RG_AUDADCRPWRUP_MASK_SFT,
1763				   0x1 << RG_AUDADCRPWRUP_SFT);
1764	}
1765
1766	if (IS_DCC_BASE(mic_type)) {
1767		usleep_range(100, 150);
1768		/* Audio L preamplifier DCC precharge off */
1769		regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON0,
1770				   RG_AUDPREAMPLDCPRECHARGE_MASK_SFT, 0x0);
1771		/* Audio R preamplifier DCC precharge off */
1772		regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON1,
1773				   RG_AUDPREAMPRDCPRECHARGE_MASK_SFT, 0x0);
1774
1775		/* Short body to ground in PGA */
1776		regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON3,
1777				   0x1 << 12, 0x0);
1778	}
1779
1780	/* here to set digital part */
1781	mt6358_mtkaif_tx_enable(priv);
1782
1783	/* UL dmic setting off */
1784	regmap_write(priv->regmap, MT6358_AFE_UL_SRC_CON0_H, 0x0000);
1785
1786	/* UL turn on */
1787	regmap_write(priv->regmap, MT6358_AFE_UL_SRC_CON0_L, 0x0001);
1788
1789	return 0;
1790}
1791
1792static void mt6358_amic_disable(struct mt6358_priv *priv)
1793{
1794	unsigned int mic_type = priv->mux_select[MUX_MIC_TYPE];
1795	unsigned int mux_pga_l = priv->mux_select[MUX_PGA_L];
1796	unsigned int mux_pga_r = priv->mux_select[MUX_PGA_R];
1797
1798	dev_info(priv->dev, "%s(), mux, mic %u, pga l %u, pga r %u\n",
1799		 __func__, mic_type, mux_pga_l, mux_pga_r);
1800
1801	/* UL turn off */
1802	regmap_update_bits(priv->regmap, MT6358_AFE_UL_SRC_CON0_L,
1803			   0x0001, 0x0000);
1804
1805	/* disable aud_pad TX fifos */
1806	mt6358_mtkaif_tx_disable(priv);
1807
1808	/* L ADC input sel : off, disable L ADC */
1809	regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON0,
1810			   0xf000, 0x0000);
1811	/* L preamplifier DCCEN */
1812	regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON0,
1813			   0x1 << 1, 0x0);
1814	/* L preamplifier input sel : off, L PGA 0 dB gain */
1815	regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON0,
1816			   0xfffb, 0x0000);
1817
1818	/* disable L preamplifier DCC precharge */
1819	regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON0,
1820			   0x1 << 2, 0x0);
1821
1822	/* R ADC input sel : off, disable R ADC */
1823	regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON1,
1824			   0xf000, 0x0000);
1825	/* R preamplifier DCCEN */
1826	regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON1,
1827			   0x1 << 1, 0x0);
1828	/* R preamplifier input sel : off, R PGA 0 dB gain */
1829	regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON1,
1830			   0x0ffb, 0x0000);
1831
1832	/* disable R preamplifier DCC precharge */
1833	regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON1,
1834			   0x1 << 2, 0x0);
1835
1836	/* mic bias */
1837	/* Disable MICBIAS0, MISBIAS0 = 1P7V */
1838	regmap_write(priv->regmap, MT6358_AUDENC_ANA_CON9, 0x0000);
1839
1840	/* Disable MICBIAS1 */
1841	regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON10,
1842			   0x0001, 0x0000);
1843
1844	if (IS_DCC_BASE(mic_type)) {
1845		/* dcclk_gen_on=1'b0 */
1846		regmap_write(priv->regmap, MT6358_AFE_DCCLK_CFG0, 0x2060);
1847		/* dcclk_pdn=1'b1 */
1848		regmap_write(priv->regmap, MT6358_AFE_DCCLK_CFG0, 0x2062);
1849		/* dcclk_ref_ck_sel=2'b00 */
1850		regmap_write(priv->regmap, MT6358_AFE_DCCLK_CFG0, 0x2062);
1851		/* dcclk_div=11'b00100000011 */
1852		regmap_write(priv->regmap, MT6358_AFE_DCCLK_CFG0, 0x2062);
1853	}
1854}
1855
1856static int mt6358_dmic_enable(struct mt6358_priv *priv)
1857{
1858	dev_info(priv->dev, "%s()\n", __func__);
1859
1860	/* mic bias */
1861	/* Enable MICBIAS0, MISBIAS0 = 1P9V */
1862	regmap_write(priv->regmap, MT6358_AUDENC_ANA_CON9, 0x0021);
1863
1864	/* RG_BANDGAPGEN=1'b0 */
1865	regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON10,
1866			   0x1 << 12, 0x0);
1867
1868	/* DMIC enable */
1869	regmap_write(priv->regmap, MT6358_AUDENC_ANA_CON8, 0x0005);
1870
1871	/* here to set digital part */
1872	mt6358_mtkaif_tx_enable(priv);
1873
1874	/* UL dmic setting */
1875	if (priv->dmic_one_wire_mode)
1876		regmap_write(priv->regmap, MT6358_AFE_UL_SRC_CON0_H, 0x0400);
1877	else
1878		regmap_write(priv->regmap, MT6358_AFE_UL_SRC_CON0_H, 0x0080);
1879
1880	/* UL turn on */
1881	regmap_write(priv->regmap, MT6358_AFE_UL_SRC_CON0_L, 0x0003);
1882
1883	/* Prevent pop noise form dmic hw */
1884	msleep(100);
1885
1886	return 0;
1887}
1888
1889static void mt6358_dmic_disable(struct mt6358_priv *priv)
1890{
1891	dev_info(priv->dev, "%s()\n", __func__);
1892
1893	/* UL turn off */
1894	regmap_update_bits(priv->regmap, MT6358_AFE_UL_SRC_CON0_L,
1895			   0x0003, 0x0000);
1896
1897	/* disable aud_pad TX fifos */
1898	mt6358_mtkaif_tx_disable(priv);
1899
1900	/* DMIC disable */
1901	regmap_write(priv->regmap, MT6358_AUDENC_ANA_CON8, 0x0000);
1902
1903	/* mic bias */
1904	/* MISBIAS0 = 1P7V */
1905	regmap_write(priv->regmap, MT6358_AUDENC_ANA_CON9, 0x0001);
1906
1907	/* RG_BANDGAPGEN=1'b0 */
1908	regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON10,
1909			   0x1 << 12, 0x0);
1910
1911	/* MICBIA0 disable */
1912	regmap_write(priv->regmap, MT6358_AUDENC_ANA_CON9, 0x0000);
1913}
1914
1915static void mt6358_restore_pga(struct mt6358_priv *priv)
1916{
1917	unsigned int gain_l, gain_r;
1918
1919	gain_l = priv->ana_gain[AUDIO_ANALOG_VOLUME_MICAMP1];
1920	gain_r = priv->ana_gain[AUDIO_ANALOG_VOLUME_MICAMP2];
1921
1922	regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON0,
1923			   RG_AUDPREAMPLGAIN_MASK_SFT,
1924			   gain_l << RG_AUDPREAMPLGAIN_SFT);
1925	regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON1,
1926			   RG_AUDPREAMPRGAIN_MASK_SFT,
1927			   gain_r << RG_AUDPREAMPRGAIN_SFT);
1928}
1929
1930static int mt_mic_type_event(struct snd_soc_dapm_widget *w,
1931			     struct snd_kcontrol *kcontrol,
1932			     int event)
1933{
1934	struct snd_soc_component *cmpnt = snd_soc_dapm_to_component(w->dapm);
1935	struct mt6358_priv *priv = snd_soc_component_get_drvdata(cmpnt);
1936	unsigned int mux = dapm_kcontrol_get_value(w->kcontrols[0]);
1937
1938	dev_dbg(priv->dev, "%s(), event 0x%x, mux %u\n",
1939		__func__, event, mux);
1940
1941	switch (event) {
1942	case SND_SOC_DAPM_WILL_PMU:
1943		priv->mux_select[MUX_MIC_TYPE] = mux;
1944		break;
1945	case SND_SOC_DAPM_PRE_PMU:
1946		switch (mux) {
1947		case MIC_TYPE_MUX_DMIC:
1948			mt6358_dmic_enable(priv);
1949			break;
1950		default:
1951			mt6358_amic_enable(priv);
1952			break;
1953		}
1954		mt6358_restore_pga(priv);
1955
1956		break;
1957	case SND_SOC_DAPM_POST_PMD:
1958		switch (priv->mux_select[MUX_MIC_TYPE]) {
1959		case MIC_TYPE_MUX_DMIC:
1960			mt6358_dmic_disable(priv);
1961			break;
1962		default:
1963			mt6358_amic_disable(priv);
1964			break;
1965		}
1966
1967		priv->mux_select[MUX_MIC_TYPE] = mux;
1968		break;
1969	default:
1970		break;
1971	}
1972
1973	return 0;
1974}
1975
1976static int mt_adc_l_event(struct snd_soc_dapm_widget *w,
1977			  struct snd_kcontrol *kcontrol,
1978			  int event)
1979{
1980	struct snd_soc_component *cmpnt = snd_soc_dapm_to_component(w->dapm);
1981	struct mt6358_priv *priv = snd_soc_component_get_drvdata(cmpnt);
1982	unsigned int mux = dapm_kcontrol_get_value(w->kcontrols[0]);
1983
1984	dev_dbg(priv->dev, "%s(), event = 0x%x, mux %u\n",
1985		__func__, event, mux);
1986
1987	priv->mux_select[MUX_ADC_L] = mux;
1988
1989	return 0;
1990}
1991
1992static int mt_adc_r_event(struct snd_soc_dapm_widget *w,
1993			  struct snd_kcontrol *kcontrol,
1994			  int event)
1995{
1996	struct snd_soc_component *cmpnt = snd_soc_dapm_to_component(w->dapm);
1997	struct mt6358_priv *priv = snd_soc_component_get_drvdata(cmpnt);
1998	unsigned int mux = dapm_kcontrol_get_value(w->kcontrols[0]);
1999
2000	dev_dbg(priv->dev, "%s(), event = 0x%x, mux %u\n",
2001		__func__, event, mux);
2002
2003	priv->mux_select[MUX_ADC_R] = mux;
2004
2005	return 0;
2006}
2007
2008static int mt_pga_left_event(struct snd_soc_dapm_widget *w,
2009			     struct snd_kcontrol *kcontrol,
2010			     int event)
2011{
2012	struct snd_soc_component *cmpnt = snd_soc_dapm_to_component(w->dapm);
2013	struct mt6358_priv *priv = snd_soc_component_get_drvdata(cmpnt);
2014	unsigned int mux = dapm_kcontrol_get_value(w->kcontrols[0]);
2015
2016	dev_dbg(priv->dev, "%s(), event = 0x%x, mux %u\n",
2017		__func__, event, mux);
2018
2019	priv->mux_select[MUX_PGA_L] = mux;
2020
2021	return 0;
2022}
2023
2024static int mt_pga_right_event(struct snd_soc_dapm_widget *w,
2025			      struct snd_kcontrol *kcontrol,
2026			      int event)
2027{
2028	struct snd_soc_component *cmpnt = snd_soc_dapm_to_component(w->dapm);
2029	struct mt6358_priv *priv = snd_soc_component_get_drvdata(cmpnt);
2030	unsigned int mux = dapm_kcontrol_get_value(w->kcontrols[0]);
2031
2032	dev_dbg(priv->dev, "%s(), event = 0x%x, mux %u\n",
2033		__func__, event, mux);
2034
2035	priv->mux_select[MUX_PGA_R] = mux;
2036
2037	return 0;
2038}
2039
2040static int mt_delay_250_event(struct snd_soc_dapm_widget *w,
2041			      struct snd_kcontrol *kcontrol,
2042			      int event)
2043{
2044	switch (event) {
2045	case SND_SOC_DAPM_POST_PMU:
2046		usleep_range(250, 270);
2047		break;
2048	case SND_SOC_DAPM_PRE_PMD:
2049		usleep_range(250, 270);
2050		break;
2051	default:
2052		break;
2053	}
2054
2055	return 0;
2056}
2057
2058/* DAPM Widgets */
2059static const struct snd_soc_dapm_widget mt6358_dapm_widgets[] = {
2060	/* Global Supply*/
2061	SND_SOC_DAPM_SUPPLY_S("CLK_BUF", SUPPLY_SEQ_CLK_BUF,
2062			      MT6358_DCXO_CW14,
2063			      RG_XO_AUDIO_EN_M_SFT, 0, NULL, 0),
2064	SND_SOC_DAPM_SUPPLY_S("AUDGLB", SUPPLY_SEQ_AUD_GLB,
2065			      MT6358_AUDDEC_ANA_CON13,
2066			      RG_AUDGLB_PWRDN_VA28_SFT, 1, NULL, 0),
2067	SND_SOC_DAPM_SUPPLY_S("CLKSQ Audio", SUPPLY_SEQ_CLKSQ,
2068			      MT6358_AUDENC_ANA_CON6,
2069			      RG_CLKSQ_EN_SFT, 0,
2070			      mt_clksq_event,
2071			      SND_SOC_DAPM_PRE_PMU),
2072	SND_SOC_DAPM_SUPPLY_S("AUDNCP_CK", SUPPLY_SEQ_TOP_CK,
2073			      MT6358_AUD_TOP_CKPDN_CON0,
2074			      RG_AUDNCP_CK_PDN_SFT, 1, NULL, 0),
2075	SND_SOC_DAPM_SUPPLY_S("ZCD13M_CK", SUPPLY_SEQ_TOP_CK,
2076			      MT6358_AUD_TOP_CKPDN_CON0,
2077			      RG_ZCD13M_CK_PDN_SFT, 1, NULL, 0),
2078	SND_SOC_DAPM_SUPPLY_S("AUD_CK", SUPPLY_SEQ_TOP_CK_LAST,
2079			      MT6358_AUD_TOP_CKPDN_CON0,
2080			      RG_AUD_CK_PDN_SFT, 1,
2081			      mt_delay_250_event,
2082			      SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
2083	SND_SOC_DAPM_SUPPLY_S("AUDIF_CK", SUPPLY_SEQ_TOP_CK,
2084			      MT6358_AUD_TOP_CKPDN_CON0,
2085			      RG_AUDIF_CK_PDN_SFT, 1, NULL, 0),
2086
2087	/* Digital Clock */
2088	SND_SOC_DAPM_SUPPLY_S("AUDIO_TOP_AFE_CTL", SUPPLY_SEQ_AUD_TOP_LAST,
2089			      MT6358_AUDIO_TOP_CON0,
2090			      PDN_AFE_CTL_SFT, 1,
2091			      mt_delay_250_event,
2092			      SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
2093	SND_SOC_DAPM_SUPPLY_S("AUDIO_TOP_DAC_CTL", SUPPLY_SEQ_AUD_TOP,
2094			      MT6358_AUDIO_TOP_CON0,
2095			      PDN_DAC_CTL_SFT, 1, NULL, 0),
2096	SND_SOC_DAPM_SUPPLY_S("AUDIO_TOP_ADC_CTL", SUPPLY_SEQ_AUD_TOP,
2097			      MT6358_AUDIO_TOP_CON0,
2098			      PDN_ADC_CTL_SFT, 1, NULL, 0),
2099	SND_SOC_DAPM_SUPPLY_S("AUDIO_TOP_I2S_DL", SUPPLY_SEQ_AUD_TOP,
2100			      MT6358_AUDIO_TOP_CON0,
2101			      PDN_I2S_DL_CTL_SFT, 1, NULL, 0),
2102	SND_SOC_DAPM_SUPPLY_S("AUDIO_TOP_PWR_CLK", SUPPLY_SEQ_AUD_TOP,
2103			      MT6358_AUDIO_TOP_CON0,
2104			      PWR_CLK_DIS_CTL_SFT, 1, NULL, 0),
2105	SND_SOC_DAPM_SUPPLY_S("AUDIO_TOP_PDN_AFE_TESTMODEL", SUPPLY_SEQ_AUD_TOP,
2106			      MT6358_AUDIO_TOP_CON0,
2107			      PDN_AFE_TESTMODEL_CTL_SFT, 1, NULL, 0),
2108	SND_SOC_DAPM_SUPPLY_S("AUDIO_TOP_PDN_RESERVED", SUPPLY_SEQ_AUD_TOP,
2109			      MT6358_AUDIO_TOP_CON0,
2110			      PDN_RESERVED_SFT, 1, NULL, 0),
2111
2112	SND_SOC_DAPM_SUPPLY("DL Digital Clock", SND_SOC_NOPM,
2113			    0, 0, NULL, 0),
2114
2115	/* AFE ON */
2116	SND_SOC_DAPM_SUPPLY_S("AFE_ON", SUPPLY_SEQ_AFE,
2117			      MT6358_AFE_UL_DL_CON0, AFE_ON_SFT, 0,
2118			      NULL, 0),
2119
2120	/* AIF Rx*/
2121	SND_SOC_DAPM_AIF_IN_E("AIF_RX", "AIF1 Playback", 0,
2122			      MT6358_AFE_DL_SRC2_CON0_L,
2123			      DL_2_SRC_ON_TMP_CTL_PRE_SFT, 0,
2124			      mt_aif_in_event,
2125			      SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
2126
2127	/* DL Supply */
2128	SND_SOC_DAPM_SUPPLY("DL Power Supply", SND_SOC_NOPM,
2129			    0, 0, NULL, 0),
2130
2131	/* DAC */
2132	SND_SOC_DAPM_MUX("DAC In Mux", SND_SOC_NOPM, 0, 0, &dac_in_mux_control),
2133
2134	SND_SOC_DAPM_DAC("DACL", NULL, SND_SOC_NOPM, 0, 0),
2135
2136	SND_SOC_DAPM_DAC("DACR", NULL, SND_SOC_NOPM, 0, 0),
2137
2138	/* LOL */
2139	SND_SOC_DAPM_MUX("LOL Mux", SND_SOC_NOPM, 0, 0, &lo_in_mux_control),
2140
2141	SND_SOC_DAPM_SUPPLY("LO Stability Enh", MT6358_AUDDEC_ANA_CON7,
2142			    RG_LOOUTPUTSTBENH_VAUDP15_SFT, 0, NULL, 0),
2143
2144	SND_SOC_DAPM_OUT_DRV("LOL Buffer", MT6358_AUDDEC_ANA_CON7,
2145			     RG_AUDLOLPWRUP_VAUDP15_SFT, 0, NULL, 0),
2146
2147	/* Headphone */
2148	SND_SOC_DAPM_MUX_E("HPL Mux", SND_SOC_NOPM, 0, 0,
2149			   &hpl_in_mux_control,
2150			   mt_hp_event,
2151			   SND_SOC_DAPM_PRE_PMU |
2152			   SND_SOC_DAPM_PRE_PMD),
2153
2154	SND_SOC_DAPM_MUX_E("HPR Mux", SND_SOC_NOPM, 0, 0,
2155			   &hpr_in_mux_control,
2156			   mt_hp_event,
2157			   SND_SOC_DAPM_PRE_PMU |
2158			   SND_SOC_DAPM_PRE_PMD),
2159
2160	/* Receiver */
2161	SND_SOC_DAPM_MUX_E("RCV Mux", SND_SOC_NOPM, 0, 0,
2162			   &rcv_in_mux_control,
2163			   mt_rcv_event,
2164			   SND_SOC_DAPM_PRE_PMU |
2165			   SND_SOC_DAPM_PRE_PMD),
2166
2167	/* Outputs */
2168	SND_SOC_DAPM_OUTPUT("Receiver"),
2169	SND_SOC_DAPM_OUTPUT("Headphone L"),
2170	SND_SOC_DAPM_OUTPUT("Headphone R"),
2171	SND_SOC_DAPM_OUTPUT("Headphone L Ext Spk Amp"),
2172	SND_SOC_DAPM_OUTPUT("Headphone R Ext Spk Amp"),
2173	SND_SOC_DAPM_OUTPUT("LINEOUT L"),
2174	SND_SOC_DAPM_OUTPUT("LINEOUT L HSSPK"),
2175
2176	/* SGEN */
2177	SND_SOC_DAPM_SUPPLY("SGEN DL Enable", MT6358_AFE_SGEN_CFG0,
2178			    SGEN_DAC_EN_CTL_SFT, 0, NULL, 0),
2179	SND_SOC_DAPM_SUPPLY("SGEN MUTE", MT6358_AFE_SGEN_CFG0,
2180			    SGEN_MUTE_SW_CTL_SFT, 1,
2181			    mt_sgen_event,
2182			    SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
2183	SND_SOC_DAPM_SUPPLY("SGEN DL SRC", MT6358_AFE_DL_SRC2_CON0_L,
2184			    DL_2_SRC_ON_TMP_CTL_PRE_SFT, 0, NULL, 0),
2185
2186	SND_SOC_DAPM_INPUT("SGEN DL"),
2187
2188	/* Uplinks */
2189	SND_SOC_DAPM_AIF_OUT_E("AIF1TX", "AIF1 Capture", 0,
2190			       SND_SOC_NOPM, 0, 0,
2191			       mt_aif_out_event,
2192			       SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
2193
2194	SND_SOC_DAPM_SUPPLY_S("ADC Supply", SUPPLY_SEQ_ADC_SUPPLY,
2195			      SND_SOC_NOPM, 0, 0,
2196			      mt_adc_supply_event,
2197			      SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
2198
2199	/* Uplinks MUX */
2200	SND_SOC_DAPM_MUX("AIF Out Mux", SND_SOC_NOPM, 0, 0,
2201			 &aif_out_mux_control),
2202
2203	SND_SOC_DAPM_MUX_E("Mic Type Mux", SND_SOC_NOPM, 0, 0,
2204			   &mic_type_mux_control,
2205			   mt_mic_type_event,
2206			   SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD |
2207			   SND_SOC_DAPM_WILL_PMU),
2208
2209	SND_SOC_DAPM_MUX_E("ADC L Mux", SND_SOC_NOPM, 0, 0,
2210			   &adc_left_mux_control,
2211			   mt_adc_l_event,
2212			   SND_SOC_DAPM_WILL_PMU),
2213	SND_SOC_DAPM_MUX_E("ADC R Mux", SND_SOC_NOPM, 0, 0,
2214			   &adc_right_mux_control,
2215			   mt_adc_r_event,
2216			   SND_SOC_DAPM_WILL_PMU),
2217
2218	SND_SOC_DAPM_ADC("ADC L", NULL, SND_SOC_NOPM, 0, 0),
2219	SND_SOC_DAPM_ADC("ADC R", NULL, SND_SOC_NOPM, 0, 0),
2220
2221	SND_SOC_DAPM_MUX_E("PGA L Mux", SND_SOC_NOPM, 0, 0,
2222			   &pga_left_mux_control,
2223			   mt_pga_left_event,
2224			   SND_SOC_DAPM_WILL_PMU),
2225	SND_SOC_DAPM_MUX_E("PGA R Mux", SND_SOC_NOPM, 0, 0,
2226			   &pga_right_mux_control,
2227			   mt_pga_right_event,
2228			   SND_SOC_DAPM_WILL_PMU),
2229
2230	SND_SOC_DAPM_PGA("PGA L", SND_SOC_NOPM, 0, 0, NULL, 0),
2231	SND_SOC_DAPM_PGA("PGA R", SND_SOC_NOPM, 0, 0, NULL, 0),
2232
2233	/* UL input */
2234	SND_SOC_DAPM_INPUT("AIN0"),
2235	SND_SOC_DAPM_INPUT("AIN1"),
2236	SND_SOC_DAPM_INPUT("AIN2"),
2237};
2238
2239static const struct snd_soc_dapm_route mt6358_dapm_routes[] = {
2240	/* Capture */
2241	{"AIF1TX", NULL, "AIF Out Mux"},
2242	{"AIF1TX", NULL, "CLK_BUF"},
2243	{"AIF1TX", NULL, "AUDGLB"},
2244	{"AIF1TX", NULL, "CLKSQ Audio"},
2245
2246	{"AIF1TX", NULL, "AUD_CK"},
2247	{"AIF1TX", NULL, "AUDIF_CK"},
2248
2249	{"AIF1TX", NULL, "AUDIO_TOP_AFE_CTL"},
2250	{"AIF1TX", NULL, "AUDIO_TOP_ADC_CTL"},
2251	{"AIF1TX", NULL, "AUDIO_TOP_PWR_CLK"},
2252	{"AIF1TX", NULL, "AUDIO_TOP_PDN_RESERVED"},
2253	{"AIF1TX", NULL, "AUDIO_TOP_I2S_DL"},
2254
2255	{"AIF1TX", NULL, "AFE_ON"},
2256
2257	{"AIF Out Mux", NULL, "Mic Type Mux"},
2258
2259	{"Mic Type Mux", "ACC", "ADC L"},
2260	{"Mic Type Mux", "ACC", "ADC R"},
2261	{"Mic Type Mux", "DCC", "ADC L"},
2262	{"Mic Type Mux", "DCC", "ADC R"},
2263	{"Mic Type Mux", "DCC_ECM_DIFF", "ADC L"},
2264	{"Mic Type Mux", "DCC_ECM_DIFF", "ADC R"},
2265	{"Mic Type Mux", "DCC_ECM_SINGLE", "ADC L"},
2266	{"Mic Type Mux", "DCC_ECM_SINGLE", "ADC R"},
2267	{"Mic Type Mux", "DMIC", "AIN0"},
2268	{"Mic Type Mux", "DMIC", "AIN2"},
2269
2270	{"ADC L", NULL, "ADC L Mux"},
2271	{"ADC L", NULL, "ADC Supply"},
2272	{"ADC R", NULL, "ADC R Mux"},
2273	{"ADC R", NULL, "ADC Supply"},
2274
2275	{"ADC L Mux", "Left Preamplifier", "PGA L"},
2276
2277	{"ADC R Mux", "Right Preamplifier", "PGA R"},
2278
2279	{"PGA L", NULL, "PGA L Mux"},
2280	{"PGA R", NULL, "PGA R Mux"},
2281
2282	{"PGA L Mux", "AIN0", "AIN0"},
2283	{"PGA L Mux", "AIN1", "AIN1"},
2284	{"PGA L Mux", "AIN2", "AIN2"},
2285
2286	{"PGA R Mux", "AIN0", "AIN0"},
2287	{"PGA R Mux", "AIN1", "AIN1"},
2288	{"PGA R Mux", "AIN2", "AIN2"},
2289
2290	/* DL Supply */
2291	{"DL Power Supply", NULL, "CLK_BUF"},
2292	{"DL Power Supply", NULL, "AUDGLB"},
2293	{"DL Power Supply", NULL, "CLKSQ Audio"},
2294
2295	{"DL Power Supply", NULL, "AUDNCP_CK"},
2296	{"DL Power Supply", NULL, "ZCD13M_CK"},
2297	{"DL Power Supply", NULL, "AUD_CK"},
2298	{"DL Power Supply", NULL, "AUDIF_CK"},
2299
2300	/* DL Digital Supply */
2301	{"DL Digital Clock", NULL, "AUDIO_TOP_AFE_CTL"},
2302	{"DL Digital Clock", NULL, "AUDIO_TOP_DAC_CTL"},
2303	{"DL Digital Clock", NULL, "AUDIO_TOP_PWR_CLK"},
2304
2305	{"DL Digital Clock", NULL, "AFE_ON"},
2306
2307	{"AIF_RX", NULL, "DL Digital Clock"},
2308
2309	/* DL Path */
2310	{"DAC In Mux", "Normal Path", "AIF_RX"},
2311
2312	{"DAC In Mux", "Sgen", "SGEN DL"},
2313	{"SGEN DL", NULL, "SGEN DL SRC"},
2314	{"SGEN DL", NULL, "SGEN MUTE"},
2315	{"SGEN DL", NULL, "SGEN DL Enable"},
2316	{"SGEN DL", NULL, "DL Digital Clock"},
2317	{"SGEN DL", NULL, "AUDIO_TOP_PDN_AFE_TESTMODEL"},
2318
2319	{"DACL", NULL, "DAC In Mux"},
2320	{"DACL", NULL, "DL Power Supply"},
2321
2322	{"DACR", NULL, "DAC In Mux"},
2323	{"DACR", NULL, "DL Power Supply"},
2324
2325	/* Lineout Path */
2326	{"LOL Mux", "Playback", "DACL"},
2327
2328	{"LOL Buffer", NULL, "LOL Mux"},
2329	{"LOL Buffer", NULL, "LO Stability Enh"},
2330
2331	{"LINEOUT L", NULL, "LOL Buffer"},
2332
2333	/* Headphone Path */
2334	{"HPL Mux", "Audio Playback", "DACL"},
2335	{"HPR Mux", "Audio Playback", "DACR"},
2336	{"HPL Mux", "HP Impedance", "DACL"},
2337	{"HPR Mux", "HP Impedance", "DACR"},
2338	{"HPL Mux", "LoudSPK Playback", "DACL"},
2339	{"HPR Mux", "LoudSPK Playback", "DACR"},
2340
2341	{"Headphone L", NULL, "HPL Mux"},
2342	{"Headphone R", NULL, "HPR Mux"},
2343	{"Headphone L Ext Spk Amp", NULL, "HPL Mux"},
2344	{"Headphone R Ext Spk Amp", NULL, "HPR Mux"},
2345	{"LINEOUT L HSSPK", NULL, "HPL Mux"},
2346
2347	/* Receiver Path */
2348	{"RCV Mux", "Voice Playback", "DACL"},
2349	{"Receiver", NULL, "RCV Mux"},
2350};
2351
2352static int mt6358_codec_dai_hw_params(struct snd_pcm_substream *substream,
2353				      struct snd_pcm_hw_params *params,
2354				      struct snd_soc_dai *dai)
2355{
2356	struct snd_soc_component *cmpnt = dai->component;
2357	struct mt6358_priv *priv = snd_soc_component_get_drvdata(cmpnt);
2358	unsigned int rate = params_rate(params);
2359
2360	dev_info(priv->dev, "%s(), substream->stream %d, rate %d, number %d\n",
2361		 __func__,
2362		 substream->stream,
2363		 rate,
2364		 substream->number);
2365
2366	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2367		priv->dl_rate = rate;
2368	else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2369		priv->ul_rate = rate;
2370
2371	return 0;
2372}
2373
2374static const struct snd_soc_dai_ops mt6358_codec_dai_ops = {
2375	.hw_params = mt6358_codec_dai_hw_params,
2376};
2377
2378#define MT6358_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE |\
2379			SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_U24_LE |\
2380			SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_U32_LE)
 
 
 
2381
2382static struct snd_soc_dai_driver mt6358_dai_driver[] = {
2383	{
2384		.name = "mt6358-snd-codec-aif1",
2385		.playback = {
2386			.stream_name = "AIF1 Playback",
2387			.channels_min = 1,
2388			.channels_max = 2,
2389			.rates = SNDRV_PCM_RATE_8000_48000 |
2390				 SNDRV_PCM_RATE_96000 |
2391				 SNDRV_PCM_RATE_192000,
2392			.formats = MT6358_FORMATS,
2393		},
2394		.capture = {
2395			.stream_name = "AIF1 Capture",
2396			.channels_min = 1,
2397			.channels_max = 2,
2398			.rates = SNDRV_PCM_RATE_8000 |
2399				 SNDRV_PCM_RATE_16000 |
2400				 SNDRV_PCM_RATE_32000 |
2401				 SNDRV_PCM_RATE_48000,
2402			.formats = MT6358_FORMATS,
2403		},
2404		.ops = &mt6358_codec_dai_ops,
2405	},
2406};
2407
2408static void mt6358_codec_init_reg(struct mt6358_priv *priv)
2409{
2410	/* Disable HeadphoneL/HeadphoneR short circuit protection */
2411	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON0,
2412			   RG_AUDHPLSCDISABLE_VAUDP15_MASK_SFT,
2413			   0x1 << RG_AUDHPLSCDISABLE_VAUDP15_SFT);
2414	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON0,
2415			   RG_AUDHPRSCDISABLE_VAUDP15_MASK_SFT,
2416			   0x1 << RG_AUDHPRSCDISABLE_VAUDP15_SFT);
2417	/* Disable voice short circuit protection */
2418	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON6,
2419			   RG_AUDHSSCDISABLE_VAUDP15_MASK_SFT,
2420			   0x1 << RG_AUDHSSCDISABLE_VAUDP15_SFT);
2421	/* disable LO buffer left short circuit protection */
2422	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON7,
2423			   RG_AUDLOLSCDISABLE_VAUDP15_MASK_SFT,
2424			   0x1 << RG_AUDLOLSCDISABLE_VAUDP15_SFT);
2425
2426	/* accdet s/w enable */
2427	regmap_update_bits(priv->regmap, MT6358_ACCDET_CON13,
2428			   0xFFFF, 0x700E);
2429
2430	/* gpio miso driving set to 4mA */
2431	regmap_write(priv->regmap, MT6358_DRV_CON3, 0x8888);
2432
2433	/* set gpio */
2434	playback_gpio_reset(priv);
2435	capture_gpio_reset(priv);
2436}
2437
2438static int mt6358_codec_probe(struct snd_soc_component *cmpnt)
2439{
2440	struct mt6358_priv *priv = snd_soc_component_get_drvdata(cmpnt);
2441	int ret;
2442
2443	snd_soc_component_init_regmap(cmpnt, priv->regmap);
2444
2445	mt6358_codec_init_reg(priv);
2446
2447	priv->avdd_reg = devm_regulator_get(priv->dev, "Avdd");
2448	if (IS_ERR(priv->avdd_reg)) {
2449		dev_err(priv->dev, "%s() have no Avdd supply", __func__);
2450		return PTR_ERR(priv->avdd_reg);
2451	}
2452
2453	ret = regulator_enable(priv->avdd_reg);
2454	if (ret)
2455		return  ret;
2456
2457	return 0;
2458}
2459
2460static const struct snd_soc_component_driver mt6358_soc_component_driver = {
2461	.probe = mt6358_codec_probe,
2462	.controls = mt6358_snd_controls,
2463	.num_controls = ARRAY_SIZE(mt6358_snd_controls),
2464	.dapm_widgets = mt6358_dapm_widgets,
2465	.num_dapm_widgets = ARRAY_SIZE(mt6358_dapm_widgets),
2466	.dapm_routes = mt6358_dapm_routes,
2467	.num_dapm_routes = ARRAY_SIZE(mt6358_dapm_routes),
2468	.endianness = 1,
2469};
2470
2471static void mt6358_parse_dt(struct mt6358_priv *priv)
2472{
2473	int ret;
2474	struct device *dev = priv->dev;
2475
2476	ret = of_property_read_u32(dev->of_node, "mediatek,dmic-mode",
2477				   &priv->dmic_one_wire_mode);
2478	if (ret) {
2479		dev_warn(priv->dev, "%s() failed to read dmic-mode\n",
2480			 __func__);
2481		priv->dmic_one_wire_mode = 0;
2482	}
2483}
2484
2485static int mt6358_platform_driver_probe(struct platform_device *pdev)
2486{
2487	struct mt6358_priv *priv;
2488	struct mt6397_chip *mt6397 = dev_get_drvdata(pdev->dev.parent);
2489
2490	priv = devm_kzalloc(&pdev->dev,
2491			    sizeof(struct mt6358_priv),
2492			    GFP_KERNEL);
2493	if (!priv)
2494		return -ENOMEM;
2495
2496	dev_set_drvdata(&pdev->dev, priv);
2497
2498	priv->dev = &pdev->dev;
2499
2500	priv->regmap = mt6397->regmap;
2501	if (IS_ERR(priv->regmap))
2502		return PTR_ERR(priv->regmap);
2503
2504	mt6358_parse_dt(priv);
2505
2506	dev_info(priv->dev, "%s(), dev name %s\n",
2507		 __func__, dev_name(&pdev->dev));
2508
2509	return devm_snd_soc_register_component(&pdev->dev,
2510				      &mt6358_soc_component_driver,
2511				      mt6358_dai_driver,
2512				      ARRAY_SIZE(mt6358_dai_driver));
2513}
2514
2515static const struct of_device_id mt6358_of_match[] = {
2516	{.compatible = "mediatek,mt6358-sound",},
2517	{.compatible = "mediatek,mt6366-sound",},
2518	{}
2519};
2520MODULE_DEVICE_TABLE(of, mt6358_of_match);
2521
2522static struct platform_driver mt6358_platform_driver = {
2523	.driver = {
2524		.name = "mt6358-sound",
2525		.of_match_table = mt6358_of_match,
2526	},
2527	.probe = mt6358_platform_driver_probe,
2528};
2529
2530module_platform_driver(mt6358_platform_driver)
2531
2532/* Module information */
2533MODULE_DESCRIPTION("MT6358 ALSA SoC codec driver");
2534MODULE_AUTHOR("KaiChieh Chuang <kaichieh.chuang@mediatek.com>");
2535MODULE_LICENSE("GPL v2");
v5.14.15
   1// SPDX-License-Identifier: GPL-2.0
   2//
   3// mt6358.c  --  mt6358 ALSA SoC audio codec driver
   4//
   5// Copyright (c) 2018 MediaTek Inc.
   6// Author: KaiChieh Chuang <kaichieh.chuang@mediatek.com>
   7
   8#include <linux/platform_device.h>
 
   9#include <linux/module.h>
  10#include <linux/of_device.h>
  11#include <linux/delay.h>
  12#include <linux/kthread.h>
  13#include <linux/sched.h>
  14#include <linux/mfd/mt6397/core.h>
  15#include <linux/regulator/consumer.h>
  16
  17#include <sound/soc.h>
  18#include <sound/tlv.h>
  19
  20#include "mt6358.h"
  21
  22enum {
  23	AUDIO_ANALOG_VOLUME_HSOUTL,
  24	AUDIO_ANALOG_VOLUME_HSOUTR,
  25	AUDIO_ANALOG_VOLUME_HPOUTL,
  26	AUDIO_ANALOG_VOLUME_HPOUTR,
  27	AUDIO_ANALOG_VOLUME_LINEOUTL,
  28	AUDIO_ANALOG_VOLUME_LINEOUTR,
  29	AUDIO_ANALOG_VOLUME_MICAMP1,
  30	AUDIO_ANALOG_VOLUME_MICAMP2,
  31	AUDIO_ANALOG_VOLUME_TYPE_MAX
  32};
  33
  34enum {
  35	MUX_ADC_L,
  36	MUX_ADC_R,
  37	MUX_PGA_L,
  38	MUX_PGA_R,
  39	MUX_MIC_TYPE,
  40	MUX_HP_L,
  41	MUX_HP_R,
  42	MUX_NUM,
  43};
  44
  45enum {
  46	DEVICE_HP,
  47	DEVICE_LO,
  48	DEVICE_RCV,
  49	DEVICE_MIC1,
  50	DEVICE_MIC2,
  51	DEVICE_NUM
  52};
  53
  54/* Supply widget subseq */
  55enum {
  56	/* common */
  57	SUPPLY_SEQ_CLK_BUF,
  58	SUPPLY_SEQ_AUD_GLB,
  59	SUPPLY_SEQ_CLKSQ,
  60	SUPPLY_SEQ_VOW_AUD_LPW,
  61	SUPPLY_SEQ_AUD_VOW,
  62	SUPPLY_SEQ_VOW_CLK,
  63	SUPPLY_SEQ_VOW_LDO,
  64	SUPPLY_SEQ_TOP_CK,
  65	SUPPLY_SEQ_TOP_CK_LAST,
  66	SUPPLY_SEQ_AUD_TOP,
  67	SUPPLY_SEQ_AUD_TOP_LAST,
  68	SUPPLY_SEQ_AFE,
  69	/* capture */
  70	SUPPLY_SEQ_ADC_SUPPLY,
  71};
  72
  73enum {
  74	CH_L = 0,
  75	CH_R,
  76	NUM_CH,
  77};
  78
  79#define REG_STRIDE 2
  80
  81struct mt6358_priv {
  82	struct device *dev;
  83	struct regmap *regmap;
  84
  85	unsigned int dl_rate;
  86	unsigned int ul_rate;
  87
  88	int ana_gain[AUDIO_ANALOG_VOLUME_TYPE_MAX];
  89	unsigned int mux_select[MUX_NUM];
  90
  91	int dev_counter[DEVICE_NUM];
  92
  93	int mtkaif_protocol;
  94
  95	struct regulator *avdd_reg;
  96
  97	int wov_enabled;
  98
  99	unsigned int dmic_one_wire_mode;
 100};
 101
 102int mt6358_set_mtkaif_protocol(struct snd_soc_component *cmpnt,
 103			       int mtkaif_protocol)
 104{
 105	struct mt6358_priv *priv = snd_soc_component_get_drvdata(cmpnt);
 106
 107	priv->mtkaif_protocol = mtkaif_protocol;
 108	return 0;
 109}
 
 110
 111static void playback_gpio_set(struct mt6358_priv *priv)
 112{
 113	/* set gpio mosi mode */
 114	regmap_update_bits(priv->regmap, MT6358_GPIO_MODE2_CLR,
 115			   0x01f8, 0x01f8);
 116	regmap_update_bits(priv->regmap, MT6358_GPIO_MODE2_SET,
 117			   0xffff, 0x0249);
 118	regmap_update_bits(priv->regmap, MT6358_GPIO_MODE2,
 119			   0xffff, 0x0249);
 120}
 121
 122static void playback_gpio_reset(struct mt6358_priv *priv)
 123{
 124	/* set pad_aud_*_mosi to GPIO mode and dir input
 125	 * reason:
 126	 * pad_aud_dat_mosi*, because the pin is used as boot strap
 127	 * don't clean clk/sync, for mtkaif protocol 2
 128	 */
 129	regmap_update_bits(priv->regmap, MT6358_GPIO_MODE2_CLR,
 130			   0x01f8, 0x01f8);
 131	regmap_update_bits(priv->regmap, MT6358_GPIO_MODE2,
 132			   0x01f8, 0x0000);
 133	regmap_update_bits(priv->regmap, MT6358_GPIO_DIR0,
 134			   0xf << 8, 0x0);
 135}
 136
 137static void capture_gpio_set(struct mt6358_priv *priv)
 138{
 139	/* set gpio miso mode */
 140	regmap_update_bits(priv->regmap, MT6358_GPIO_MODE3_CLR,
 141			   0xffff, 0xffff);
 142	regmap_update_bits(priv->regmap, MT6358_GPIO_MODE3_SET,
 143			   0xffff, 0x0249);
 144	regmap_update_bits(priv->regmap, MT6358_GPIO_MODE3,
 145			   0xffff, 0x0249);
 146}
 147
 148static void capture_gpio_reset(struct mt6358_priv *priv)
 149{
 150	/* set pad_aud_*_miso to GPIO mode and dir input
 151	 * reason:
 152	 * pad_aud_clk_miso, because when playback only the miso_clk
 153	 * will also have 26m, so will have power leak
 154	 * pad_aud_dat_miso*, because the pin is used as boot strap
 155	 */
 156	regmap_update_bits(priv->regmap, MT6358_GPIO_MODE3_CLR,
 157			   0xffff, 0xffff);
 158	regmap_update_bits(priv->regmap, MT6358_GPIO_MODE3,
 159			   0xffff, 0x0000);
 160	regmap_update_bits(priv->regmap, MT6358_GPIO_DIR0,
 161			   0xf << 12, 0x0);
 162}
 163
 164/* use only when not govern by DAPM */
 165static int mt6358_set_dcxo(struct mt6358_priv *priv, bool enable)
 166{
 167	regmap_update_bits(priv->regmap, MT6358_DCXO_CW14,
 168			   0x1 << RG_XO_AUDIO_EN_M_SFT,
 169			   (enable ? 1 : 0) << RG_XO_AUDIO_EN_M_SFT);
 170	return 0;
 171}
 172
 173/* use only when not govern by DAPM */
 174static int mt6358_set_clksq(struct mt6358_priv *priv, bool enable)
 175{
 176	/* audio clk source from internal dcxo */
 177	regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON6,
 178			   RG_CLKSQ_IN_SEL_TEST_MASK_SFT,
 179			   0x0);
 180
 181	/* Enable/disable CLKSQ 26MHz */
 182	regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON6,
 183			   RG_CLKSQ_EN_MASK_SFT,
 184			   (enable ? 1 : 0) << RG_CLKSQ_EN_SFT);
 185	return 0;
 186}
 187
 188/* use only when not govern by DAPM */
 189static int mt6358_set_aud_global_bias(struct mt6358_priv *priv, bool enable)
 190{
 191	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON13,
 192			   RG_AUDGLB_PWRDN_VA28_MASK_SFT,
 193			   (enable ? 0 : 1) << RG_AUDGLB_PWRDN_VA28_SFT);
 194	return 0;
 195}
 196
 197/* use only when not govern by DAPM */
 198static int mt6358_set_topck(struct mt6358_priv *priv, bool enable)
 199{
 200	regmap_update_bits(priv->regmap, MT6358_AUD_TOP_CKPDN_CON0,
 201			   0x0066, enable ? 0x0 : 0x66);
 202	return 0;
 203}
 204
 205static int mt6358_mtkaif_tx_enable(struct mt6358_priv *priv)
 206{
 207	switch (priv->mtkaif_protocol) {
 208	case MT6358_MTKAIF_PROTOCOL_2_CLK_P2:
 209		/* MTKAIF TX format setting */
 210		regmap_update_bits(priv->regmap,
 211				   MT6358_AFE_ADDA_MTKAIF_CFG0,
 212				   0xffff, 0x0010);
 213		/* enable aud_pad TX fifos */
 214		regmap_update_bits(priv->regmap,
 215				   MT6358_AFE_AUD_PAD_TOP,
 216				   0xff00, 0x3800);
 217		regmap_update_bits(priv->regmap,
 218				   MT6358_AFE_AUD_PAD_TOP,
 219				   0xff00, 0x3900);
 220		break;
 221	case MT6358_MTKAIF_PROTOCOL_2:
 222		/* MTKAIF TX format setting */
 223		regmap_update_bits(priv->regmap,
 224				   MT6358_AFE_ADDA_MTKAIF_CFG0,
 225				   0xffff, 0x0010);
 226		/* enable aud_pad TX fifos */
 227		regmap_update_bits(priv->regmap,
 228				   MT6358_AFE_AUD_PAD_TOP,
 229				   0xff00, 0x3100);
 230		break;
 231	case MT6358_MTKAIF_PROTOCOL_1:
 232	default:
 233		/* MTKAIF TX format setting */
 234		regmap_update_bits(priv->regmap,
 235				   MT6358_AFE_ADDA_MTKAIF_CFG0,
 236				   0xffff, 0x0000);
 237		/* enable aud_pad TX fifos */
 238		regmap_update_bits(priv->regmap,
 239				   MT6358_AFE_AUD_PAD_TOP,
 240				   0xff00, 0x3100);
 241		break;
 242	}
 243	return 0;
 244}
 245
 246static int mt6358_mtkaif_tx_disable(struct mt6358_priv *priv)
 247{
 248	/* disable aud_pad TX fifos */
 249	regmap_update_bits(priv->regmap, MT6358_AFE_AUD_PAD_TOP,
 250			   0xff00, 0x3000);
 251	return 0;
 252}
 253
 254int mt6358_mtkaif_calibration_enable(struct snd_soc_component *cmpnt)
 255{
 256	struct mt6358_priv *priv = snd_soc_component_get_drvdata(cmpnt);
 257
 258	playback_gpio_set(priv);
 259	capture_gpio_set(priv);
 260	mt6358_mtkaif_tx_enable(priv);
 261
 262	mt6358_set_dcxo(priv, true);
 263	mt6358_set_aud_global_bias(priv, true);
 264	mt6358_set_clksq(priv, true);
 265	mt6358_set_topck(priv, true);
 266
 267	/* set dat_miso_loopback on */
 268	regmap_update_bits(priv->regmap, MT6358_AUDIO_DIG_CFG,
 269			   RG_AUD_PAD_TOP_DAT_MISO2_LOOPBACK_MASK_SFT,
 270			   1 << RG_AUD_PAD_TOP_DAT_MISO2_LOOPBACK_SFT);
 271	regmap_update_bits(priv->regmap, MT6358_AUDIO_DIG_CFG,
 272			   RG_AUD_PAD_TOP_DAT_MISO_LOOPBACK_MASK_SFT,
 273			   1 << RG_AUD_PAD_TOP_DAT_MISO_LOOPBACK_SFT);
 274	return 0;
 275}
 
 276
 277int mt6358_mtkaif_calibration_disable(struct snd_soc_component *cmpnt)
 278{
 279	struct mt6358_priv *priv = snd_soc_component_get_drvdata(cmpnt);
 280
 281	/* set dat_miso_loopback off */
 282	regmap_update_bits(priv->regmap, MT6358_AUDIO_DIG_CFG,
 283			   RG_AUD_PAD_TOP_DAT_MISO2_LOOPBACK_MASK_SFT,
 284			   0 << RG_AUD_PAD_TOP_DAT_MISO2_LOOPBACK_SFT);
 285	regmap_update_bits(priv->regmap, MT6358_AUDIO_DIG_CFG,
 286			   RG_AUD_PAD_TOP_DAT_MISO_LOOPBACK_MASK_SFT,
 287			   0 << RG_AUD_PAD_TOP_DAT_MISO_LOOPBACK_SFT);
 288
 289	mt6358_set_topck(priv, false);
 290	mt6358_set_clksq(priv, false);
 291	mt6358_set_aud_global_bias(priv, false);
 292	mt6358_set_dcxo(priv, false);
 293
 294	mt6358_mtkaif_tx_disable(priv);
 295	playback_gpio_reset(priv);
 296	capture_gpio_reset(priv);
 297	return 0;
 298}
 
 299
 300int mt6358_set_mtkaif_calibration_phase(struct snd_soc_component *cmpnt,
 301					int phase_1, int phase_2)
 302{
 303	struct mt6358_priv *priv = snd_soc_component_get_drvdata(cmpnt);
 304
 305	regmap_update_bits(priv->regmap, MT6358_AUDIO_DIG_CFG,
 306			   RG_AUD_PAD_TOP_PHASE_MODE_MASK_SFT,
 307			   phase_1 << RG_AUD_PAD_TOP_PHASE_MODE_SFT);
 308	regmap_update_bits(priv->regmap, MT6358_AUDIO_DIG_CFG,
 309			   RG_AUD_PAD_TOP_PHASE_MODE2_MASK_SFT,
 310			   phase_2 << RG_AUD_PAD_TOP_PHASE_MODE2_SFT);
 311	return 0;
 312}
 
 313
 314/* dl pga gain */
 315enum {
 316	DL_GAIN_8DB = 0,
 317	DL_GAIN_0DB = 8,
 318	DL_GAIN_N_1DB = 9,
 319	DL_GAIN_N_10DB = 18,
 320	DL_GAIN_N_40DB = 0x1f,
 321};
 322
 323#define DL_GAIN_N_10DB_REG (DL_GAIN_N_10DB << 7 | DL_GAIN_N_10DB)
 324#define DL_GAIN_N_40DB_REG (DL_GAIN_N_40DB << 7 | DL_GAIN_N_40DB)
 325#define DL_GAIN_REG_MASK 0x0f9f
 326
 327static void hp_zcd_disable(struct mt6358_priv *priv)
 328{
 329	regmap_write(priv->regmap, MT6358_ZCD_CON0, 0x0000);
 330}
 331
 332static void hp_main_output_ramp(struct mt6358_priv *priv, bool up)
 333{
 334	int i, stage;
 335	int target = 7;
 336
 337	/* Enable/Reduce HPL/R main output stage step by step */
 338	for (i = 0; i <= target; i++) {
 339		stage = up ? i : target - i;
 340		regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON1,
 341				   0x7 << 8, stage << 8);
 342		regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON1,
 343				   0x7 << 11, stage << 11);
 344		usleep_range(100, 150);
 345	}
 346}
 347
 348static void hp_aux_feedback_loop_gain_ramp(struct mt6358_priv *priv, bool up)
 349{
 350	int i, stage;
 351
 352	/* Reduce HP aux feedback loop gain step by step */
 353	for (i = 0; i <= 0xf; i++) {
 354		stage = up ? i : 0xf - i;
 355		regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON9,
 356				   0xf << 12, stage << 12);
 357		usleep_range(100, 150);
 358	}
 359}
 360
 361static void hp_pull_down(struct mt6358_priv *priv, bool enable)
 362{
 363	int i;
 364
 365	if (enable) {
 366		for (i = 0x0; i <= 0x6; i++) {
 367			regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON4,
 368					   0x7, i);
 369			usleep_range(600, 700);
 370		}
 371	} else {
 372		for (i = 0x6; i >= 0x1; i--) {
 373			regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON4,
 374					   0x7, i);
 375			usleep_range(600, 700);
 376		}
 377	}
 378}
 379
 380static bool is_valid_hp_pga_idx(int reg_idx)
 381{
 382	return (reg_idx >= DL_GAIN_8DB && reg_idx <= DL_GAIN_N_10DB) ||
 383	       reg_idx == DL_GAIN_N_40DB;
 384}
 385
 386static void headset_volume_ramp(struct mt6358_priv *priv, int from, int to)
 387{
 388	int offset = 0, count = 0, reg_idx;
 389
 390	if (!is_valid_hp_pga_idx(from) || !is_valid_hp_pga_idx(to))
 391		dev_warn(priv->dev, "%s(), volume index is not valid, from %d, to %d\n",
 392			 __func__, from, to);
 393
 394	dev_info(priv->dev, "%s(), from %d, to %d\n",
 395		 __func__, from, to);
 396
 397	if (to > from)
 398		offset = to - from;
 399	else
 400		offset = from - to;
 401
 402	while (offset >= 0) {
 403		if (to > from)
 404			reg_idx = from + count;
 405		else
 406			reg_idx = from - count;
 407
 408		if (is_valid_hp_pga_idx(reg_idx)) {
 409			regmap_update_bits(priv->regmap,
 410					   MT6358_ZCD_CON2,
 411					   DL_GAIN_REG_MASK,
 412					   (reg_idx << 7) | reg_idx);
 413			usleep_range(200, 300);
 414		}
 415		offset--;
 416		count++;
 417	}
 418}
 419
 420static int mt6358_put_volsw(struct snd_kcontrol *kcontrol,
 421			    struct snd_ctl_elem_value *ucontrol)
 422{
 423	struct snd_soc_component *component =
 424			snd_soc_kcontrol_component(kcontrol);
 425	struct mt6358_priv *priv = snd_soc_component_get_drvdata(component);
 426	struct soc_mixer_control *mc =
 427			(struct soc_mixer_control *)kcontrol->private_value;
 428	unsigned int reg;
 429	int ret;
 430
 431	ret = snd_soc_put_volsw(kcontrol, ucontrol);
 432	if (ret < 0)
 433		return ret;
 434
 435	switch (mc->reg) {
 436	case MT6358_ZCD_CON2:
 437		regmap_read(priv->regmap, MT6358_ZCD_CON2, &reg);
 438		priv->ana_gain[AUDIO_ANALOG_VOLUME_HPOUTL] =
 439			(reg >> RG_AUDHPLGAIN_SFT) & RG_AUDHPLGAIN_MASK;
 440		priv->ana_gain[AUDIO_ANALOG_VOLUME_HPOUTR] =
 441			(reg >> RG_AUDHPRGAIN_SFT) & RG_AUDHPRGAIN_MASK;
 442		break;
 443	case MT6358_ZCD_CON1:
 444		regmap_read(priv->regmap, MT6358_ZCD_CON1, &reg);
 445		priv->ana_gain[AUDIO_ANALOG_VOLUME_LINEOUTL] =
 446			(reg >> RG_AUDLOLGAIN_SFT) & RG_AUDLOLGAIN_MASK;
 447		priv->ana_gain[AUDIO_ANALOG_VOLUME_LINEOUTR] =
 448			(reg >> RG_AUDLORGAIN_SFT) & RG_AUDLORGAIN_MASK;
 449		break;
 450	case MT6358_ZCD_CON3:
 451		regmap_read(priv->regmap, MT6358_ZCD_CON3, &reg);
 452		priv->ana_gain[AUDIO_ANALOG_VOLUME_HSOUTL] =
 453			(reg >> RG_AUDHSGAIN_SFT) & RG_AUDHSGAIN_MASK;
 454		priv->ana_gain[AUDIO_ANALOG_VOLUME_HSOUTR] =
 455			(reg >> RG_AUDHSGAIN_SFT) & RG_AUDHSGAIN_MASK;
 456		break;
 457	case MT6358_AUDENC_ANA_CON0:
 458	case MT6358_AUDENC_ANA_CON1:
 459		regmap_read(priv->regmap, MT6358_AUDENC_ANA_CON0, &reg);
 460		priv->ana_gain[AUDIO_ANALOG_VOLUME_MICAMP1] =
 461			(reg >> RG_AUDPREAMPLGAIN_SFT) & RG_AUDPREAMPLGAIN_MASK;
 462		regmap_read(priv->regmap, MT6358_AUDENC_ANA_CON1, &reg);
 463		priv->ana_gain[AUDIO_ANALOG_VOLUME_MICAMP2] =
 464			(reg >> RG_AUDPREAMPRGAIN_SFT) & RG_AUDPREAMPRGAIN_MASK;
 465		break;
 466	}
 467
 468	return ret;
 469}
 470
 471static void mt6358_restore_pga(struct mt6358_priv *priv);
 472
 473static int mt6358_enable_wov_phase2(struct mt6358_priv *priv)
 474{
 475	/* analog */
 476	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON13,
 477			   0xffff, 0x0000);
 478	regmap_update_bits(priv->regmap, MT6358_DCXO_CW14, 0xffff, 0xa2b5);
 479	regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON1,
 480			   0xffff, 0x0800);
 481	mt6358_restore_pga(priv);
 482
 483	regmap_update_bits(priv->regmap, MT6358_DCXO_CW13, 0xffff, 0x9929);
 484	regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON9,
 485			   0xffff, 0x0025);
 486	regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON8,
 487			   0xffff, 0x0005);
 488
 489	/* digital */
 490	regmap_update_bits(priv->regmap, MT6358_AUD_TOP_CKPDN_CON0,
 491			   0xffff, 0x0000);
 492	regmap_update_bits(priv->regmap, MT6358_GPIO_MODE3, 0xffff, 0x0120);
 493	regmap_update_bits(priv->regmap, MT6358_AFE_VOW_CFG0, 0xffff, 0xffff);
 494	regmap_update_bits(priv->regmap, MT6358_AFE_VOW_CFG1, 0xffff, 0x0200);
 495	regmap_update_bits(priv->regmap, MT6358_AFE_VOW_CFG2, 0xffff, 0x2424);
 496	regmap_update_bits(priv->regmap, MT6358_AFE_VOW_CFG3, 0xffff, 0xdbac);
 497	regmap_update_bits(priv->regmap, MT6358_AFE_VOW_CFG4, 0xffff, 0x029e);
 498	regmap_update_bits(priv->regmap, MT6358_AFE_VOW_CFG5, 0xffff, 0x0000);
 499	regmap_update_bits(priv->regmap, MT6358_AFE_VOW_POSDIV_CFG0,
 500			   0xffff, 0x0000);
 501	regmap_update_bits(priv->regmap, MT6358_AFE_VOW_HPF_CFG0,
 502			   0xffff, 0x0451);
 503	regmap_update_bits(priv->regmap, MT6358_AFE_VOW_TOP, 0xffff, 0x68d1);
 504
 505	return 0;
 506}
 507
 508static int mt6358_disable_wov_phase2(struct mt6358_priv *priv)
 509{
 510	/* digital */
 511	regmap_update_bits(priv->regmap, MT6358_AFE_VOW_TOP, 0xffff, 0xc000);
 512	regmap_update_bits(priv->regmap, MT6358_AFE_VOW_HPF_CFG0,
 513			   0xffff, 0x0450);
 514	regmap_update_bits(priv->regmap, MT6358_AFE_VOW_POSDIV_CFG0,
 515			   0xffff, 0x0c00);
 516	regmap_update_bits(priv->regmap, MT6358_AFE_VOW_CFG5, 0xffff, 0x0100);
 517	regmap_update_bits(priv->regmap, MT6358_AFE_VOW_CFG4, 0xffff, 0x006c);
 518	regmap_update_bits(priv->regmap, MT6358_AFE_VOW_CFG3, 0xffff, 0xa879);
 519	regmap_update_bits(priv->regmap, MT6358_AFE_VOW_CFG2, 0xffff, 0x2323);
 520	regmap_update_bits(priv->regmap, MT6358_AFE_VOW_CFG1, 0xffff, 0x0400);
 521	regmap_update_bits(priv->regmap, MT6358_AFE_VOW_CFG0, 0xffff, 0x0000);
 522	regmap_update_bits(priv->regmap, MT6358_GPIO_MODE3, 0xffff, 0x02d8);
 523	regmap_update_bits(priv->regmap, MT6358_AUD_TOP_CKPDN_CON0,
 524			   0xffff, 0x0000);
 525
 526	/* analog */
 527	regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON8,
 528			   0xffff, 0x0004);
 529	regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON9,
 530			   0xffff, 0x0000);
 531	regmap_update_bits(priv->regmap, MT6358_DCXO_CW13, 0xffff, 0x9829);
 532	regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON1,
 533			   0xffff, 0x0000);
 534	mt6358_restore_pga(priv);
 535	regmap_update_bits(priv->regmap, MT6358_DCXO_CW14, 0xffff, 0xa2b5);
 536	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON13,
 537			   0xffff, 0x0010);
 538
 539	return 0;
 540}
 541
 542static int mt6358_get_wov(struct snd_kcontrol *kcontrol,
 543			  struct snd_ctl_elem_value *ucontrol)
 544{
 545	struct snd_soc_component *c = snd_soc_kcontrol_component(kcontrol);
 546	struct mt6358_priv *priv = snd_soc_component_get_drvdata(c);
 547
 548	ucontrol->value.integer.value[0] = priv->wov_enabled;
 549	return 0;
 550}
 551
 552static int mt6358_put_wov(struct snd_kcontrol *kcontrol,
 553			  struct snd_ctl_elem_value *ucontrol)
 554{
 555	struct snd_soc_component *c = snd_soc_kcontrol_component(kcontrol);
 556	struct mt6358_priv *priv = snd_soc_component_get_drvdata(c);
 557	int enabled = ucontrol->value.integer.value[0];
 558
 
 
 
 559	if (priv->wov_enabled != enabled) {
 560		if (enabled)
 561			mt6358_enable_wov_phase2(priv);
 562		else
 563			mt6358_disable_wov_phase2(priv);
 564
 565		priv->wov_enabled = enabled;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 566	}
 
 567
 568	return 0;
 569}
 570
 571static const DECLARE_TLV_DB_SCALE(playback_tlv, -1000, 100, 0);
 572static const DECLARE_TLV_DB_SCALE(pga_tlv, 0, 600, 0);
 573
 574static const struct snd_kcontrol_new mt6358_snd_controls[] = {
 575	/* dl pga gain */
 576	SOC_DOUBLE_EXT_TLV("Headphone Volume",
 577			   MT6358_ZCD_CON2, 0, 7, 0x12, 1,
 578			   snd_soc_get_volsw, mt6358_put_volsw, playback_tlv),
 579	SOC_DOUBLE_EXT_TLV("Lineout Volume",
 580			   MT6358_ZCD_CON1, 0, 7, 0x12, 1,
 581			   snd_soc_get_volsw, mt6358_put_volsw, playback_tlv),
 582	SOC_SINGLE_EXT_TLV("Handset Volume",
 583			   MT6358_ZCD_CON3, 0, 0x12, 1,
 584			   snd_soc_get_volsw, mt6358_put_volsw, playback_tlv),
 585	/* ul pga gain */
 586	SOC_DOUBLE_R_EXT_TLV("PGA Volume",
 587			     MT6358_AUDENC_ANA_CON0, MT6358_AUDENC_ANA_CON1,
 588			     8, 4, 0,
 589			     snd_soc_get_volsw, mt6358_put_volsw, pga_tlv),
 590
 591	SOC_SINGLE_BOOL_EXT("Wake-on-Voice Phase2 Switch", 0,
 592			    mt6358_get_wov, mt6358_put_wov),
 
 
 
 593};
 594
 595/* MUX */
 596/* LOL MUX */
 597static const char * const lo_in_mux_map[] = {
 598	"Open", "Mute", "Playback", "Test Mode"
 599};
 600
 601static int lo_in_mux_map_value[] = {
 602	0x0, 0x1, 0x2, 0x3,
 603};
 604
 605static SOC_VALUE_ENUM_SINGLE_DECL(lo_in_mux_map_enum,
 606				  MT6358_AUDDEC_ANA_CON7,
 607				  RG_AUDLOLMUXINPUTSEL_VAUDP15_SFT,
 608				  RG_AUDLOLMUXINPUTSEL_VAUDP15_MASK,
 609				  lo_in_mux_map,
 610				  lo_in_mux_map_value);
 611
 612static const struct snd_kcontrol_new lo_in_mux_control =
 613	SOC_DAPM_ENUM("In Select", lo_in_mux_map_enum);
 614
 615/*HP MUX */
 616enum {
 617	HP_MUX_OPEN = 0,
 618	HP_MUX_HPSPK,
 619	HP_MUX_HP,
 620	HP_MUX_TEST_MODE,
 621	HP_MUX_HP_IMPEDANCE,
 622	HP_MUX_MASK = 0x7,
 623};
 624
 625static const char * const hp_in_mux_map[] = {
 626	"Open",
 627	"LoudSPK Playback",
 628	"Audio Playback",
 629	"Test Mode",
 630	"HP Impedance",
 631	"undefined1",
 632	"undefined2",
 633	"undefined3",
 634};
 635
 636static int hp_in_mux_map_value[] = {
 637	HP_MUX_OPEN,
 638	HP_MUX_HPSPK,
 639	HP_MUX_HP,
 640	HP_MUX_TEST_MODE,
 641	HP_MUX_HP_IMPEDANCE,
 642	HP_MUX_OPEN,
 643	HP_MUX_OPEN,
 644	HP_MUX_OPEN,
 645};
 646
 647static SOC_VALUE_ENUM_SINGLE_DECL(hpl_in_mux_map_enum,
 648				  SND_SOC_NOPM,
 649				  0,
 650				  HP_MUX_MASK,
 651				  hp_in_mux_map,
 652				  hp_in_mux_map_value);
 653
 654static const struct snd_kcontrol_new hpl_in_mux_control =
 655	SOC_DAPM_ENUM("HPL Select", hpl_in_mux_map_enum);
 656
 657static SOC_VALUE_ENUM_SINGLE_DECL(hpr_in_mux_map_enum,
 658				  SND_SOC_NOPM,
 659				  0,
 660				  HP_MUX_MASK,
 661				  hp_in_mux_map,
 662				  hp_in_mux_map_value);
 663
 664static const struct snd_kcontrol_new hpr_in_mux_control =
 665	SOC_DAPM_ENUM("HPR Select", hpr_in_mux_map_enum);
 666
 667/* RCV MUX */
 668enum {
 669	RCV_MUX_OPEN = 0,
 670	RCV_MUX_MUTE,
 671	RCV_MUX_VOICE_PLAYBACK,
 672	RCV_MUX_TEST_MODE,
 673	RCV_MUX_MASK = 0x3,
 674};
 675
 676static const char * const rcv_in_mux_map[] = {
 677	"Open", "Mute", "Voice Playback", "Test Mode"
 678};
 679
 680static int rcv_in_mux_map_value[] = {
 681	RCV_MUX_OPEN,
 682	RCV_MUX_MUTE,
 683	RCV_MUX_VOICE_PLAYBACK,
 684	RCV_MUX_TEST_MODE,
 685};
 686
 687static SOC_VALUE_ENUM_SINGLE_DECL(rcv_in_mux_map_enum,
 688				  SND_SOC_NOPM,
 689				  0,
 690				  RCV_MUX_MASK,
 691				  rcv_in_mux_map,
 692				  rcv_in_mux_map_value);
 693
 694static const struct snd_kcontrol_new rcv_in_mux_control =
 695	SOC_DAPM_ENUM("RCV Select", rcv_in_mux_map_enum);
 696
 697/* DAC In MUX */
 698static const char * const dac_in_mux_map[] = {
 699	"Normal Path", "Sgen"
 700};
 701
 702static int dac_in_mux_map_value[] = {
 703	0x0, 0x1,
 704};
 705
 706static SOC_VALUE_ENUM_SINGLE_DECL(dac_in_mux_map_enum,
 707				  MT6358_AFE_TOP_CON0,
 708				  DL_SINE_ON_SFT,
 709				  DL_SINE_ON_MASK,
 710				  dac_in_mux_map,
 711				  dac_in_mux_map_value);
 712
 713static const struct snd_kcontrol_new dac_in_mux_control =
 714	SOC_DAPM_ENUM("DAC Select", dac_in_mux_map_enum);
 715
 716/* AIF Out MUX */
 717static SOC_VALUE_ENUM_SINGLE_DECL(aif_out_mux_map_enum,
 718				  MT6358_AFE_TOP_CON0,
 719				  UL_SINE_ON_SFT,
 720				  UL_SINE_ON_MASK,
 721				  dac_in_mux_map,
 722				  dac_in_mux_map_value);
 723
 724static const struct snd_kcontrol_new aif_out_mux_control =
 725	SOC_DAPM_ENUM("AIF Out Select", aif_out_mux_map_enum);
 726
 727/* Mic Type MUX */
 728enum {
 729	MIC_TYPE_MUX_IDLE = 0,
 730	MIC_TYPE_MUX_ACC,
 731	MIC_TYPE_MUX_DMIC,
 732	MIC_TYPE_MUX_DCC,
 733	MIC_TYPE_MUX_DCC_ECM_DIFF,
 734	MIC_TYPE_MUX_DCC_ECM_SINGLE,
 735	MIC_TYPE_MUX_MASK = 0x7,
 736};
 737
 738#define IS_DCC_BASE(type) ((type) == MIC_TYPE_MUX_DCC || \
 739			(type) == MIC_TYPE_MUX_DCC_ECM_DIFF || \
 740			(type) == MIC_TYPE_MUX_DCC_ECM_SINGLE)
 741
 742static const char * const mic_type_mux_map[] = {
 743	"Idle",
 744	"ACC",
 745	"DMIC",
 746	"DCC",
 747	"DCC_ECM_DIFF",
 748	"DCC_ECM_SINGLE",
 749};
 750
 751static int mic_type_mux_map_value[] = {
 752	MIC_TYPE_MUX_IDLE,
 753	MIC_TYPE_MUX_ACC,
 754	MIC_TYPE_MUX_DMIC,
 755	MIC_TYPE_MUX_DCC,
 756	MIC_TYPE_MUX_DCC_ECM_DIFF,
 757	MIC_TYPE_MUX_DCC_ECM_SINGLE,
 758};
 759
 760static SOC_VALUE_ENUM_SINGLE_DECL(mic_type_mux_map_enum,
 761				  SND_SOC_NOPM,
 762				  0,
 763				  MIC_TYPE_MUX_MASK,
 764				  mic_type_mux_map,
 765				  mic_type_mux_map_value);
 766
 767static const struct snd_kcontrol_new mic_type_mux_control =
 768	SOC_DAPM_ENUM("Mic Type Select", mic_type_mux_map_enum);
 769
 770/* ADC L MUX */
 771enum {
 772	ADC_MUX_IDLE = 0,
 773	ADC_MUX_AIN0,
 774	ADC_MUX_PREAMPLIFIER,
 775	ADC_MUX_IDLE1,
 776	ADC_MUX_MASK = 0x3,
 777};
 778
 779static const char * const adc_left_mux_map[] = {
 780	"Idle", "AIN0", "Left Preamplifier", "Idle_1"
 781};
 782
 783static int adc_mux_map_value[] = {
 784	ADC_MUX_IDLE,
 785	ADC_MUX_AIN0,
 786	ADC_MUX_PREAMPLIFIER,
 787	ADC_MUX_IDLE1,
 788};
 789
 790static SOC_VALUE_ENUM_SINGLE_DECL(adc_left_mux_map_enum,
 791				  SND_SOC_NOPM,
 792				  0,
 793				  ADC_MUX_MASK,
 794				  adc_left_mux_map,
 795				  adc_mux_map_value);
 796
 797static const struct snd_kcontrol_new adc_left_mux_control =
 798	SOC_DAPM_ENUM("ADC L Select", adc_left_mux_map_enum);
 799
 800/* ADC R MUX */
 801static const char * const adc_right_mux_map[] = {
 802	"Idle", "AIN0", "Right Preamplifier", "Idle_1"
 803};
 804
 805static SOC_VALUE_ENUM_SINGLE_DECL(adc_right_mux_map_enum,
 806				  SND_SOC_NOPM,
 807				  0,
 808				  ADC_MUX_MASK,
 809				  adc_right_mux_map,
 810				  adc_mux_map_value);
 811
 812static const struct snd_kcontrol_new adc_right_mux_control =
 813	SOC_DAPM_ENUM("ADC R Select", adc_right_mux_map_enum);
 814
 815/* PGA L MUX */
 816enum {
 817	PGA_MUX_NONE = 0,
 818	PGA_MUX_AIN0,
 819	PGA_MUX_AIN1,
 820	PGA_MUX_AIN2,
 821	PGA_MUX_MASK = 0x3,
 822};
 823
 824static const char * const pga_mux_map[] = {
 825	"None", "AIN0", "AIN1", "AIN2"
 826};
 827
 828static int pga_mux_map_value[] = {
 829	PGA_MUX_NONE,
 830	PGA_MUX_AIN0,
 831	PGA_MUX_AIN1,
 832	PGA_MUX_AIN2,
 833};
 834
 835static SOC_VALUE_ENUM_SINGLE_DECL(pga_left_mux_map_enum,
 836				  SND_SOC_NOPM,
 837				  0,
 838				  PGA_MUX_MASK,
 839				  pga_mux_map,
 840				  pga_mux_map_value);
 841
 842static const struct snd_kcontrol_new pga_left_mux_control =
 843	SOC_DAPM_ENUM("PGA L Select", pga_left_mux_map_enum);
 844
 845/* PGA R MUX */
 846static SOC_VALUE_ENUM_SINGLE_DECL(pga_right_mux_map_enum,
 847				  SND_SOC_NOPM,
 848				  0,
 849				  PGA_MUX_MASK,
 850				  pga_mux_map,
 851				  pga_mux_map_value);
 852
 853static const struct snd_kcontrol_new pga_right_mux_control =
 854	SOC_DAPM_ENUM("PGA R Select", pga_right_mux_map_enum);
 855
 856static int mt_clksq_event(struct snd_soc_dapm_widget *w,
 857			  struct snd_kcontrol *kcontrol,
 858			  int event)
 859{
 860	struct snd_soc_component *cmpnt = snd_soc_dapm_to_component(w->dapm);
 861	struct mt6358_priv *priv = snd_soc_component_get_drvdata(cmpnt);
 862
 863	dev_dbg(priv->dev, "%s(), event = 0x%x\n", __func__, event);
 864
 865	switch (event) {
 866	case SND_SOC_DAPM_PRE_PMU:
 867		/* audio clk source from internal dcxo */
 868		regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON6,
 869				   RG_CLKSQ_IN_SEL_TEST_MASK_SFT,
 870				   0x0);
 871		break;
 872	default:
 873		break;
 874	}
 875
 876	return 0;
 877}
 878
 879static int mt_sgen_event(struct snd_soc_dapm_widget *w,
 880			 struct snd_kcontrol *kcontrol,
 881			 int event)
 882{
 883	struct snd_soc_component *cmpnt = snd_soc_dapm_to_component(w->dapm);
 884	struct mt6358_priv *priv = snd_soc_component_get_drvdata(cmpnt);
 885
 886	dev_dbg(priv->dev, "%s(), event = 0x%x\n", __func__, event);
 887
 888	switch (event) {
 889	case SND_SOC_DAPM_PRE_PMU:
 890		/* sdm audio fifo clock power on */
 891		regmap_write(priv->regmap, MT6358_AFUNC_AUD_CON2, 0x0006);
 892		/* scrambler clock on enable */
 893		regmap_write(priv->regmap, MT6358_AFUNC_AUD_CON0, 0xCBA1);
 894		/* sdm power on */
 895		regmap_write(priv->regmap, MT6358_AFUNC_AUD_CON2, 0x0003);
 896		/* sdm fifo enable */
 897		regmap_write(priv->regmap, MT6358_AFUNC_AUD_CON2, 0x000B);
 898
 899		regmap_update_bits(priv->regmap, MT6358_AFE_SGEN_CFG0,
 900				   0xff3f,
 901				   0x0000);
 902		regmap_update_bits(priv->regmap, MT6358_AFE_SGEN_CFG1,
 903				   0xffff,
 904				   0x0001);
 905		break;
 906	case SND_SOC_DAPM_POST_PMD:
 907		/* DL scrambler disabling sequence */
 908		regmap_write(priv->regmap, MT6358_AFUNC_AUD_CON2, 0x0000);
 909		regmap_write(priv->regmap, MT6358_AFUNC_AUD_CON0, 0xcba0);
 910		break;
 911	default:
 912		break;
 913	}
 914
 915	return 0;
 916}
 917
 918static int mt_aif_in_event(struct snd_soc_dapm_widget *w,
 919			   struct snd_kcontrol *kcontrol,
 920			   int event)
 921{
 922	struct snd_soc_component *cmpnt = snd_soc_dapm_to_component(w->dapm);
 923	struct mt6358_priv *priv = snd_soc_component_get_drvdata(cmpnt);
 924
 925	dev_info(priv->dev, "%s(), event 0x%x, rate %d\n",
 926		 __func__, event, priv->dl_rate);
 927
 928	switch (event) {
 929	case SND_SOC_DAPM_PRE_PMU:
 930		playback_gpio_set(priv);
 931
 932		/* sdm audio fifo clock power on */
 933		regmap_write(priv->regmap, MT6358_AFUNC_AUD_CON2, 0x0006);
 934		/* scrambler clock on enable */
 935		regmap_write(priv->regmap, MT6358_AFUNC_AUD_CON0, 0xCBA1);
 936		/* sdm power on */
 937		regmap_write(priv->regmap, MT6358_AFUNC_AUD_CON2, 0x0003);
 938		/* sdm fifo enable */
 939		regmap_write(priv->regmap, MT6358_AFUNC_AUD_CON2, 0x000B);
 940		break;
 941	case SND_SOC_DAPM_POST_PMD:
 942		/* DL scrambler disabling sequence */
 943		regmap_write(priv->regmap, MT6358_AFUNC_AUD_CON2, 0x0000);
 944		regmap_write(priv->regmap, MT6358_AFUNC_AUD_CON0, 0xcba0);
 945
 946		playback_gpio_reset(priv);
 947		break;
 948	default:
 949		break;
 950	}
 951
 952	return 0;
 953}
 954
 955static int mtk_hp_enable(struct mt6358_priv *priv)
 956{
 957	/* Pull-down HPL/R to AVSS28_AUD */
 958	hp_pull_down(priv, true);
 959	/* release HP CMFB gate rstb */
 960	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON4,
 961			   0x1 << 6, 0x1 << 6);
 962
 963	/* Reduce ESD resistance of AU_REFN */
 964	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON2, 0x4000);
 965
 966	/* Set HPR/HPL gain as minimum (~ -40dB) */
 967	regmap_write(priv->regmap, MT6358_ZCD_CON2, DL_GAIN_N_40DB_REG);
 968
 969	/* Turn on DA_600K_NCP_VA18 */
 970	regmap_write(priv->regmap, MT6358_AUDNCP_CLKDIV_CON1, 0x0001);
 971	/* Set NCP clock as 604kHz // 26MHz/43 = 604KHz */
 972	regmap_write(priv->regmap, MT6358_AUDNCP_CLKDIV_CON2, 0x002c);
 973	/* Toggle RG_DIVCKS_CHG */
 974	regmap_write(priv->regmap, MT6358_AUDNCP_CLKDIV_CON0, 0x0001);
 975	/* Set NCP soft start mode as default mode: 100us */
 976	regmap_write(priv->regmap, MT6358_AUDNCP_CLKDIV_CON4, 0x0003);
 977	/* Enable NCP */
 978	regmap_write(priv->regmap, MT6358_AUDNCP_CLKDIV_CON3, 0x0000);
 979	usleep_range(250, 270);
 980
 981	/* Enable cap-less LDOs (1.5V) */
 982	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON14,
 983			   0x1055, 0x1055);
 984	/* Enable NV regulator (-1.2V) */
 985	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON15, 0x0001);
 986	usleep_range(100, 120);
 987
 988	/* Disable AUD_ZCD */
 989	hp_zcd_disable(priv);
 990
 991	/* Disable headphone short-circuit protection */
 992	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON0, 0x3000);
 993
 994	/* Enable IBIST */
 995	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON12, 0x0055);
 996
 997	/* Set HP DR bias current optimization, 010: 6uA */
 998	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON11, 0x4900);
 999	/* Set HP & ZCD bias current optimization */
1000	/* 01: ZCD: 4uA, HP/HS/LO: 5uA */
1001	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON12, 0x0055);
1002	/* Set HPP/N STB enhance circuits */
1003	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON2, 0x4033);
1004
1005	/* Enable HP aux output stage */
1006	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON1, 0x000c);
1007	/* Enable HP aux feedback loop */
1008	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON1, 0x003c);
1009	/* Enable HP aux CMFB loop */
1010	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON9, 0x0c00);
1011	/* Enable HP driver bias circuits */
1012	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON0, 0x30c0);
1013	/* Enable HP driver core circuits */
1014	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON0, 0x30f0);
1015	/* Short HP main output to HP aux output stage */
1016	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON1, 0x00fc);
1017
1018	/* Enable HP main CMFB loop */
1019	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON9, 0x0e00);
1020	/* Disable HP aux CMFB loop */
1021	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON9, 0x0200);
1022
1023	/* Select CMFB resistor bulk to AC mode */
1024	/* Selec HS/LO cap size (6.5pF default) */
1025	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON10, 0x0000);
1026
1027	/* Enable HP main output stage */
1028	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON1, 0x00ff);
1029	/* Enable HPR/L main output stage step by step */
1030	hp_main_output_ramp(priv, true);
1031
1032	/* Reduce HP aux feedback loop gain */
1033	hp_aux_feedback_loop_gain_ramp(priv, true);
1034	/* Disable HP aux feedback loop */
1035	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON1, 0x3fcf);
1036
1037	/* apply volume setting */
1038	headset_volume_ramp(priv,
1039			    DL_GAIN_N_10DB,
1040			    priv->ana_gain[AUDIO_ANALOG_VOLUME_HPOUTL]);
1041
1042	/* Disable HP aux output stage */
1043	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON1, 0x3fc3);
1044	/* Unshort HP main output to HP aux output stage */
1045	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON1, 0x3f03);
1046	usleep_range(100, 120);
1047
1048	/* Enable AUD_CLK */
1049	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON13, 0x1, 0x1);
1050	/* Enable Audio DAC  */
1051	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON0, 0x30ff);
1052	/* Enable low-noise mode of DAC */
1053	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON9, 0xf201);
1054	usleep_range(100, 120);
1055
1056	/* Switch HPL MUX to audio DAC */
1057	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON0, 0x32ff);
1058	/* Switch HPR MUX to audio DAC */
1059	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON0, 0x3aff);
1060
1061	/* Disable Pull-down HPL/R to AVSS28_AUD */
1062	hp_pull_down(priv, false);
1063
1064	return 0;
1065}
1066
1067static int mtk_hp_disable(struct mt6358_priv *priv)
1068{
1069	/* Pull-down HPL/R to AVSS28_AUD */
1070	hp_pull_down(priv, true);
1071
1072	/* HPR/HPL mux to open */
1073	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON0,
1074			   0x0f00, 0x0000);
1075
1076	/* Disable low-noise mode of DAC */
1077	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON9,
1078			   0x0001, 0x0000);
1079
1080	/* Disable Audio DAC */
1081	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON0,
1082			   0x000f, 0x0000);
1083
1084	/* Disable AUD_CLK */
1085	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON13, 0x1, 0x0);
1086
1087	/* Short HP main output to HP aux output stage */
1088	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON1, 0x3fc3);
1089	/* Enable HP aux output stage */
1090	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON1, 0x3fcf);
1091
1092	/* decrease HPL/R gain to normal gain step by step */
1093	headset_volume_ramp(priv,
1094			    priv->ana_gain[AUDIO_ANALOG_VOLUME_HPOUTL],
1095			    DL_GAIN_N_40DB);
1096
1097	/* Enable HP aux feedback loop */
1098	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON1, 0x3fff);
1099
1100	/* Reduce HP aux feedback loop gain */
1101	hp_aux_feedback_loop_gain_ramp(priv, false);
1102
1103	/* decrease HPR/L main output stage step by step */
1104	hp_main_output_ramp(priv, false);
1105
1106	/* Disable HP main output stage */
1107	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON1, 0x3, 0x0);
1108
1109	/* Enable HP aux CMFB loop */
1110	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON9, 0x0e00);
1111
1112	/* Disable HP main CMFB loop */
1113	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON9, 0x0c00);
1114
1115	/* Unshort HP main output to HP aux output stage */
1116	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON1,
1117			   0x3 << 6, 0x0);
1118
1119	/* Disable HP driver core circuits */
1120	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON0,
1121			   0x3 << 4, 0x0);
1122
1123	/* Disable HP driver bias circuits */
1124	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON0,
1125			   0x3 << 6, 0x0);
1126
1127	/* Disable HP aux CMFB loop */
1128	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON9, 0x0000);
1129
1130	/* Disable HP aux feedback loop */
1131	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON1,
1132			   0x3 << 4, 0x0);
1133
1134	/* Disable HP aux output stage */
1135	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON1,
1136			   0x3 << 2, 0x0);
1137
1138	/* Disable IBIST */
1139	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON12,
1140			   0x1 << 8, 0x1 << 8);
1141
1142	/* Disable NV regulator (-1.2V) */
1143	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON15, 0x1, 0x0);
1144	/* Disable cap-less LDOs (1.5V) */
1145	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON14,
1146			   0x1055, 0x0);
1147	/* Disable NCP */
1148	regmap_update_bits(priv->regmap, MT6358_AUDNCP_CLKDIV_CON3,
1149			   0x1, 0x1);
1150
1151	/* Increase ESD resistance of AU_REFN */
1152	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON2,
1153			   0x1 << 14, 0x0);
1154
1155	/* Set HP CMFB gate rstb */
1156	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON4,
1157			   0x1 << 6, 0x0);
1158	/* disable Pull-down HPL/R to AVSS28_AUD */
1159	hp_pull_down(priv, false);
1160
1161	return 0;
1162}
1163
1164static int mtk_hp_spk_enable(struct mt6358_priv *priv)
1165{
1166	/* Pull-down HPL/R to AVSS28_AUD */
1167	hp_pull_down(priv, true);
1168	/* release HP CMFB gate rstb */
1169	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON4,
1170			   0x1 << 6, 0x1 << 6);
1171
1172	/* Reduce ESD resistance of AU_REFN */
1173	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON2, 0x4000);
1174
1175	/* Set HPR/HPL gain to -10dB */
1176	regmap_write(priv->regmap, MT6358_ZCD_CON2, DL_GAIN_N_10DB_REG);
1177
1178	/* Turn on DA_600K_NCP_VA18 */
1179	regmap_write(priv->regmap, MT6358_AUDNCP_CLKDIV_CON1, 0x0001);
1180	/* Set NCP clock as 604kHz // 26MHz/43 = 604KHz */
1181	regmap_write(priv->regmap, MT6358_AUDNCP_CLKDIV_CON2, 0x002c);
1182	/* Toggle RG_DIVCKS_CHG */
1183	regmap_write(priv->regmap, MT6358_AUDNCP_CLKDIV_CON0, 0x0001);
1184	/* Set NCP soft start mode as default mode: 100us */
1185	regmap_write(priv->regmap, MT6358_AUDNCP_CLKDIV_CON4, 0x0003);
1186	/* Enable NCP */
1187	regmap_write(priv->regmap, MT6358_AUDNCP_CLKDIV_CON3, 0x0000);
1188	usleep_range(250, 270);
1189
1190	/* Enable cap-less LDOs (1.5V) */
1191	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON14,
1192			   0x1055, 0x1055);
1193	/* Enable NV regulator (-1.2V) */
1194	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON15, 0x0001);
1195	usleep_range(100, 120);
1196
1197	/* Disable AUD_ZCD */
1198	hp_zcd_disable(priv);
1199
1200	/* Disable headphone short-circuit protection */
1201	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON0, 0x3000);
1202
1203	/* Enable IBIST */
1204	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON12, 0x0055);
1205
1206	/* Set HP DR bias current optimization, 010: 6uA */
1207	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON11, 0x4900);
1208	/* Set HP & ZCD bias current optimization */
1209	/* 01: ZCD: 4uA, HP/HS/LO: 5uA */
1210	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON12, 0x0055);
1211	/* Set HPP/N STB enhance circuits */
1212	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON2, 0x4033);
1213
1214	/* Disable Pull-down HPL/R to AVSS28_AUD */
1215	hp_pull_down(priv, false);
1216
1217	/* Enable HP driver bias circuits */
1218	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON0, 0x30c0);
1219	/* Enable HP driver core circuits */
1220	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON0, 0x30f0);
1221	/* Enable HP main CMFB loop */
1222	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON9, 0x0200);
1223
1224	/* Select CMFB resistor bulk to AC mode */
1225	/* Selec HS/LO cap size (6.5pF default) */
1226	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON10, 0x0000);
1227
1228	/* Enable HP main output stage */
1229	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON1, 0x0003);
1230	/* Enable HPR/L main output stage step by step */
1231	hp_main_output_ramp(priv, true);
1232
1233	/* Set LO gain as minimum (~ -40dB) */
1234	regmap_write(priv->regmap, MT6358_ZCD_CON1, DL_GAIN_N_40DB_REG);
1235	/* apply volume setting */
1236	headset_volume_ramp(priv,
1237			    DL_GAIN_N_10DB,
1238			    priv->ana_gain[AUDIO_ANALOG_VOLUME_HPOUTL]);
1239
1240	/* Set LO STB enhance circuits */
1241	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON7, 0x0110);
1242	/* Enable LO driver bias circuits */
1243	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON7, 0x0112);
1244	/* Enable LO driver core circuits */
1245	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON7, 0x0113);
1246
1247	/* Set LOL gain to normal gain step by step */
1248	regmap_update_bits(priv->regmap, MT6358_ZCD_CON1,
1249			   RG_AUDLOLGAIN_MASK_SFT,
1250			   priv->ana_gain[AUDIO_ANALOG_VOLUME_LINEOUTL] <<
1251			   RG_AUDLOLGAIN_SFT);
1252	regmap_update_bits(priv->regmap, MT6358_ZCD_CON1,
1253			   RG_AUDLORGAIN_MASK_SFT,
1254			   priv->ana_gain[AUDIO_ANALOG_VOLUME_LINEOUTR] <<
1255			   RG_AUDLORGAIN_SFT);
1256
1257	/* Enable AUD_CLK */
1258	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON13, 0x1, 0x1);
1259	/* Enable Audio DAC  */
1260	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON0, 0x30f9);
1261	/* Enable low-noise mode of DAC */
1262	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON9, 0x0201);
1263	/* Switch LOL MUX to audio DAC */
1264	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON7, 0x011b);
1265	/* Switch HPL/R MUX to Line-out */
1266	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON0, 0x35f9);
1267
1268	return 0;
1269}
1270
1271static int mtk_hp_spk_disable(struct mt6358_priv *priv)
1272{
1273	/* HPR/HPL mux to open */
1274	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON0,
1275			   0x0f00, 0x0000);
1276	/* LOL mux to open */
1277	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON7,
1278			   0x3 << 2, 0x0000);
1279
1280	/* Disable Audio DAC */
1281	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON0,
1282			   0x000f, 0x0000);
1283
1284	/* Disable AUD_CLK */
1285	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON13, 0x1, 0x0);
1286
1287	/* decrease HPL/R gain to normal gain step by step */
1288	headset_volume_ramp(priv,
1289			    priv->ana_gain[AUDIO_ANALOG_VOLUME_HPOUTL],
1290			    DL_GAIN_N_40DB);
1291
1292	/* decrease LOL gain to minimum gain step by step */
1293	regmap_update_bits(priv->regmap, MT6358_ZCD_CON1,
1294			   DL_GAIN_REG_MASK, DL_GAIN_N_40DB_REG);
1295
1296	/* decrease HPR/L main output stage step by step */
1297	hp_main_output_ramp(priv, false);
1298
1299	/* Disable HP main output stage */
1300	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON1, 0x3, 0x0);
1301
1302	/* Short HP main output to HP aux output stage */
1303	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON1, 0x3fc3);
1304	/* Enable HP aux output stage */
1305	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON1, 0x3fcf);
1306
1307	/* Enable HP aux feedback loop */
1308	regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON1, 0x3fff);
1309
1310	/* Reduce HP aux feedback loop gain */
1311	hp_aux_feedback_loop_gain_ramp(priv, false);
1312
1313	/* Disable HP driver core circuits */
1314	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON0,
1315			   0x3 << 4, 0x0);
1316	/* Disable LO driver core circuits */
1317	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON7,
1318			   0x1, 0x0);
1319
1320	/* Disable HP driver bias circuits */
1321	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON0,
1322			   0x3 << 6, 0x0);
1323	/* Disable LO driver bias circuits */
1324	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON7,
1325			   0x1 << 1, 0x0);
1326
1327	/* Disable HP aux CMFB loop */
1328	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON9,
1329			   0xff << 8, 0x0000);
1330
1331	/* Disable IBIST */
1332	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON12,
1333			   0x1 << 8, 0x1 << 8);
1334	/* Disable NV regulator (-1.2V) */
1335	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON15, 0x1, 0x0);
1336	/* Disable cap-less LDOs (1.5V) */
1337	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON14, 0x1055, 0x0);
1338	/* Disable NCP */
1339	regmap_update_bits(priv->regmap, MT6358_AUDNCP_CLKDIV_CON3, 0x1, 0x1);
1340
1341	/* Set HP CMFB gate rstb */
1342	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON4,
1343			   0x1 << 6, 0x0);
1344	/* disable Pull-down HPL/R to AVSS28_AUD */
1345	hp_pull_down(priv, false);
1346
1347	return 0;
1348}
1349
1350static int mt_hp_event(struct snd_soc_dapm_widget *w,
1351		       struct snd_kcontrol *kcontrol,
1352		       int event)
1353{
1354	struct snd_soc_component *cmpnt = snd_soc_dapm_to_component(w->dapm);
1355	struct mt6358_priv *priv = snd_soc_component_get_drvdata(cmpnt);
1356	unsigned int mux = dapm_kcontrol_get_value(w->kcontrols[0]);
1357	int device = DEVICE_HP;
1358
1359	dev_info(priv->dev, "%s(), event 0x%x, dev_counter[DEV_HP] %d, mux %u\n",
1360		 __func__,
1361		 event,
1362		 priv->dev_counter[device],
1363		 mux);
1364
1365	switch (event) {
1366	case SND_SOC_DAPM_PRE_PMU:
1367		priv->dev_counter[device]++;
1368		if (priv->dev_counter[device] > 1)
1369			break;	/* already enabled, do nothing */
1370		else if (priv->dev_counter[device] <= 0)
1371			dev_warn(priv->dev, "%s(), dev_counter[DEV_HP] %d <= 0\n",
1372				 __func__,
1373				 priv->dev_counter[device]);
1374
1375		priv->mux_select[MUX_HP_L] = mux;
1376
1377		if (mux == HP_MUX_HP)
1378			mtk_hp_enable(priv);
1379		else if (mux == HP_MUX_HPSPK)
1380			mtk_hp_spk_enable(priv);
1381		break;
1382	case SND_SOC_DAPM_PRE_PMD:
1383		priv->dev_counter[device]--;
1384		if (priv->dev_counter[device] > 0) {
1385			break;	/* still being used, don't close */
1386		} else if (priv->dev_counter[device] < 0) {
1387			dev_warn(priv->dev, "%s(), dev_counter[DEV_HP] %d < 0\n",
1388				 __func__,
1389				 priv->dev_counter[device]);
1390			priv->dev_counter[device] = 0;
1391			break;
1392		}
1393
1394		if (priv->mux_select[MUX_HP_L] == HP_MUX_HP)
1395			mtk_hp_disable(priv);
1396		else if (priv->mux_select[MUX_HP_L] == HP_MUX_HPSPK)
1397			mtk_hp_spk_disable(priv);
1398
1399		priv->mux_select[MUX_HP_L] = mux;
1400		break;
1401	default:
1402		break;
1403	}
1404
1405	return 0;
1406}
1407
1408static int mt_rcv_event(struct snd_soc_dapm_widget *w,
1409			struct snd_kcontrol *kcontrol,
1410			int event)
1411{
1412	struct snd_soc_component *cmpnt = snd_soc_dapm_to_component(w->dapm);
1413	struct mt6358_priv *priv = snd_soc_component_get_drvdata(cmpnt);
1414
1415	dev_info(priv->dev, "%s(), event 0x%x, mux %u\n",
1416		 __func__,
1417		 event,
1418		 dapm_kcontrol_get_value(w->kcontrols[0]));
1419
1420	switch (event) {
1421	case SND_SOC_DAPM_PRE_PMU:
1422		/* Reduce ESD resistance of AU_REFN */
1423		regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON2, 0x4000);
1424
1425		/* Turn on DA_600K_NCP_VA18 */
1426		regmap_write(priv->regmap, MT6358_AUDNCP_CLKDIV_CON1, 0x0001);
1427		/* Set NCP clock as 604kHz // 26MHz/43 = 604KHz */
1428		regmap_write(priv->regmap, MT6358_AUDNCP_CLKDIV_CON2, 0x002c);
1429		/* Toggle RG_DIVCKS_CHG */
1430		regmap_write(priv->regmap, MT6358_AUDNCP_CLKDIV_CON0, 0x0001);
1431		/* Set NCP soft start mode as default mode: 100us */
1432		regmap_write(priv->regmap, MT6358_AUDNCP_CLKDIV_CON4, 0x0003);
1433		/* Enable NCP */
1434		regmap_write(priv->regmap, MT6358_AUDNCP_CLKDIV_CON3, 0x0000);
1435		usleep_range(250, 270);
1436
1437		/* Enable cap-less LDOs (1.5V) */
1438		regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON14,
1439				   0x1055, 0x1055);
1440		/* Enable NV regulator (-1.2V) */
1441		regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON15, 0x0001);
1442		usleep_range(100, 120);
1443
1444		/* Disable AUD_ZCD */
1445		hp_zcd_disable(priv);
1446
1447		/* Disable handset short-circuit protection */
1448		regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON6, 0x0010);
1449
1450		/* Enable IBIST */
1451		regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON12, 0x0055);
1452		/* Set HP DR bias current optimization, 010: 6uA */
1453		regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON11, 0x4900);
1454		/* Set HP & ZCD bias current optimization */
1455		/* 01: ZCD: 4uA, HP/HS/LO: 5uA */
1456		regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON12, 0x0055);
1457		/* Set HS STB enhance circuits */
1458		regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON6, 0x0090);
1459
1460		/* Disable HP main CMFB loop */
1461		regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON9, 0x0000);
1462		/* Select CMFB resistor bulk to AC mode */
1463		/* Selec HS/LO cap size (6.5pF default) */
1464		regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON10, 0x0000);
1465
1466		/* Enable HS driver bias circuits */
1467		regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON6, 0x0092);
1468		/* Enable HS driver core circuits */
1469		regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON6, 0x0093);
1470
1471		/* Enable AUD_CLK */
1472		regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON13,
1473				   0x1, 0x1);
1474
1475		/* Enable Audio DAC  */
1476		regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON0, 0x0009);
1477		/* Enable low-noise mode of DAC */
1478		regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON9, 0x0001);
1479		/* Switch HS MUX to audio DAC */
1480		regmap_write(priv->regmap, MT6358_AUDDEC_ANA_CON6, 0x009b);
1481		break;
1482	case SND_SOC_DAPM_PRE_PMD:
1483		/* HS mux to open */
1484		regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON6,
1485				   RG_AUDHSMUXINPUTSEL_VAUDP15_MASK_SFT,
1486				   RCV_MUX_OPEN);
1487
1488		/* Disable Audio DAC */
1489		regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON0,
1490				   0x000f, 0x0000);
1491
1492		/* Disable AUD_CLK */
1493		regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON13,
1494				   0x1, 0x0);
1495
1496		/* decrease HS gain to minimum gain step by step */
1497		regmap_write(priv->regmap, MT6358_ZCD_CON3, DL_GAIN_N_40DB);
1498
1499		/* Disable HS driver core circuits */
1500		regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON6,
1501				   0x1, 0x0);
1502
1503		/* Disable HS driver bias circuits */
1504		regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON6,
1505				   0x1 << 1, 0x0000);
1506
1507		/* Disable HP aux CMFB loop */
1508		regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON9,
1509				   0xff << 8, 0x0);
1510
1511		/* Enable HP main CMFB Switch */
1512		regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON9,
1513				   0xff << 8, 0x2 << 8);
1514
1515		/* Disable IBIST */
1516		regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON12,
1517				   0x1 << 8, 0x1 << 8);
1518
1519		/* Disable NV regulator (-1.2V) */
1520		regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON15,
1521				   0x1, 0x0);
1522		/* Disable cap-less LDOs (1.5V) */
1523		regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON14,
1524				   0x1055, 0x0);
1525		/* Disable NCP */
1526		regmap_update_bits(priv->regmap, MT6358_AUDNCP_CLKDIV_CON3,
1527				   0x1, 0x1);
1528		break;
1529	default:
1530		break;
1531	}
1532
1533	return 0;
1534}
1535
1536static int mt_aif_out_event(struct snd_soc_dapm_widget *w,
1537			    struct snd_kcontrol *kcontrol,
1538			    int event)
1539{
1540	struct snd_soc_component *cmpnt = snd_soc_dapm_to_component(w->dapm);
1541	struct mt6358_priv *priv = snd_soc_component_get_drvdata(cmpnt);
1542
1543	dev_dbg(priv->dev, "%s(), event 0x%x, rate %d\n",
1544		__func__, event, priv->ul_rate);
1545
1546	switch (event) {
1547	case SND_SOC_DAPM_PRE_PMU:
1548		capture_gpio_set(priv);
1549		break;
1550	case SND_SOC_DAPM_POST_PMD:
1551		capture_gpio_reset(priv);
1552		break;
1553	default:
1554		break;
1555	}
1556
1557	return 0;
1558}
1559
1560static int mt_adc_supply_event(struct snd_soc_dapm_widget *w,
1561			       struct snd_kcontrol *kcontrol,
1562			       int event)
1563{
1564	struct snd_soc_component *cmpnt = snd_soc_dapm_to_component(w->dapm);
1565	struct mt6358_priv *priv = snd_soc_component_get_drvdata(cmpnt);
1566
1567	dev_dbg(priv->dev, "%s(), event 0x%x\n",
1568		__func__, event);
1569
1570	switch (event) {
1571	case SND_SOC_DAPM_PRE_PMU:
1572		/* Enable audio ADC CLKGEN  */
1573		regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON13,
1574				   0x1 << 5, 0x1 << 5);
1575		/* ADC CLK from CLKGEN (13MHz) */
1576		regmap_write(priv->regmap, MT6358_AUDENC_ANA_CON3,
1577			     0x0000);
1578		/* Enable  LCLDO_ENC 1P8V */
1579		regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON14,
1580				   0x2500, 0x0100);
1581		/* LCLDO_ENC remote sense */
1582		regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON14,
1583				   0x2500, 0x2500);
1584		break;
1585	case SND_SOC_DAPM_POST_PMD:
1586		/* LCLDO_ENC remote sense off */
1587		regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON14,
1588				   0x2500, 0x0100);
1589		/* disable LCLDO_ENC 1P8V */
1590		regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON14,
1591				   0x2500, 0x0000);
1592
1593		/* ADC CLK from CLKGEN (13MHz) */
1594		regmap_write(priv->regmap, MT6358_AUDENC_ANA_CON3, 0x0000);
1595		/* disable audio ADC CLKGEN  */
1596		regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON13,
1597				   0x1 << 5, 0x0 << 5);
1598		break;
1599	default:
1600		break;
1601	}
1602
1603	return 0;
1604}
1605
1606static int mt6358_amic_enable(struct mt6358_priv *priv)
1607{
1608	unsigned int mic_type = priv->mux_select[MUX_MIC_TYPE];
1609	unsigned int mux_pga_l = priv->mux_select[MUX_PGA_L];
1610	unsigned int mux_pga_r = priv->mux_select[MUX_PGA_R];
1611
1612	dev_info(priv->dev, "%s(), mux, mic %u, pga l %u, pga r %u\n",
1613		 __func__, mic_type, mux_pga_l, mux_pga_r);
1614
1615	if (IS_DCC_BASE(mic_type)) {
1616		/* DCC 50k CLK (from 26M) */
1617		regmap_write(priv->regmap, MT6358_AFE_DCCLK_CFG0, 0x2062);
1618		regmap_write(priv->regmap, MT6358_AFE_DCCLK_CFG0, 0x2062);
1619		regmap_write(priv->regmap, MT6358_AFE_DCCLK_CFG0, 0x2060);
1620		regmap_write(priv->regmap, MT6358_AFE_DCCLK_CFG0, 0x2061);
1621		regmap_write(priv->regmap, MT6358_AFE_DCCLK_CFG1, 0x0100);
1622	}
1623
1624	/* mic bias 0 */
1625	if (mux_pga_l == PGA_MUX_AIN0 || mux_pga_l == PGA_MUX_AIN2 ||
1626	    mux_pga_r == PGA_MUX_AIN0 || mux_pga_r == PGA_MUX_AIN2) {
1627		switch (mic_type) {
1628		case MIC_TYPE_MUX_DCC_ECM_DIFF:
1629			regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON9,
1630					   0xff00, 0x7700);
1631			break;
1632		case MIC_TYPE_MUX_DCC_ECM_SINGLE:
1633			regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON9,
1634					   0xff00, 0x1100);
1635			break;
1636		default:
1637			regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON9,
1638					   0xff00, 0x0000);
1639			break;
1640		}
1641		/* Enable MICBIAS0, MISBIAS0 = 1P9V */
1642		regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON9,
1643				   0xff, 0x21);
1644	}
1645
1646	/* mic bias 1 */
1647	if (mux_pga_l == PGA_MUX_AIN1 || mux_pga_r == PGA_MUX_AIN1) {
1648		/* Enable MICBIAS1, MISBIAS1 = 2P6V */
1649		if (mic_type == MIC_TYPE_MUX_DCC_ECM_SINGLE)
1650			regmap_write(priv->regmap,
1651				     MT6358_AUDENC_ANA_CON10, 0x0161);
1652		else
1653			regmap_write(priv->regmap,
1654				     MT6358_AUDENC_ANA_CON10, 0x0061);
1655	}
1656
1657	if (IS_DCC_BASE(mic_type)) {
1658		/* Audio L/R preamplifier DCC precharge */
1659		regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON0,
1660				   0xf8ff, 0x0004);
1661		regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON1,
1662				   0xf8ff, 0x0004);
1663	} else {
1664		/* reset reg */
1665		regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON0,
1666				   0xf8ff, 0x0000);
1667		regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON1,
1668				   0xf8ff, 0x0000);
1669	}
1670
1671	if (mux_pga_l != PGA_MUX_NONE) {
1672		/* L preamplifier input sel */
1673		regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON0,
1674				   RG_AUDPREAMPLINPUTSEL_MASK_SFT,
1675				   mux_pga_l << RG_AUDPREAMPLINPUTSEL_SFT);
1676
1677		/* L preamplifier enable */
1678		regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON0,
1679				   RG_AUDPREAMPLON_MASK_SFT,
1680				   0x1 << RG_AUDPREAMPLON_SFT);
1681
1682		if (IS_DCC_BASE(mic_type)) {
1683			/* L preamplifier DCCEN */
1684			regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON0,
1685					   RG_AUDPREAMPLDCCEN_MASK_SFT,
1686					   0x1 << RG_AUDPREAMPLDCCEN_SFT);
1687		}
1688
1689		/* L ADC input sel : L PGA. Enable audio L ADC */
1690		regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON0,
1691				   RG_AUDADCLINPUTSEL_MASK_SFT,
1692				   ADC_MUX_PREAMPLIFIER <<
1693				   RG_AUDADCLINPUTSEL_SFT);
1694		regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON0,
1695				   RG_AUDADCLPWRUP_MASK_SFT,
1696				   0x1 << RG_AUDADCLPWRUP_SFT);
1697	}
1698
1699	if (mux_pga_r != PGA_MUX_NONE) {
1700		/* R preamplifier input sel */
1701		regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON1,
1702				   RG_AUDPREAMPRINPUTSEL_MASK_SFT,
1703				   mux_pga_r << RG_AUDPREAMPRINPUTSEL_SFT);
1704
1705		/* R preamplifier enable */
1706		regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON1,
1707				   RG_AUDPREAMPRON_MASK_SFT,
1708				   0x1 << RG_AUDPREAMPRON_SFT);
1709
1710		if (IS_DCC_BASE(mic_type)) {
1711			/* R preamplifier DCCEN */
1712			regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON1,
1713					   RG_AUDPREAMPRDCCEN_MASK_SFT,
1714					   0x1 << RG_AUDPREAMPRDCCEN_SFT);
1715		}
1716
1717		/* R ADC input sel : R PGA. Enable audio R ADC */
1718		regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON1,
1719				   RG_AUDADCRINPUTSEL_MASK_SFT,
1720				   ADC_MUX_PREAMPLIFIER <<
1721				   RG_AUDADCRINPUTSEL_SFT);
1722		regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON1,
1723				   RG_AUDADCRPWRUP_MASK_SFT,
1724				   0x1 << RG_AUDADCRPWRUP_SFT);
1725	}
1726
1727	if (IS_DCC_BASE(mic_type)) {
1728		usleep_range(100, 150);
1729		/* Audio L preamplifier DCC precharge off */
1730		regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON0,
1731				   RG_AUDPREAMPLDCPRECHARGE_MASK_SFT, 0x0);
1732		/* Audio R preamplifier DCC precharge off */
1733		regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON1,
1734				   RG_AUDPREAMPRDCPRECHARGE_MASK_SFT, 0x0);
1735
1736		/* Short body to ground in PGA */
1737		regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON3,
1738				   0x1 << 12, 0x0);
1739	}
1740
1741	/* here to set digital part */
1742	mt6358_mtkaif_tx_enable(priv);
1743
1744	/* UL dmic setting off */
1745	regmap_write(priv->regmap, MT6358_AFE_UL_SRC_CON0_H, 0x0000);
1746
1747	/* UL turn on */
1748	regmap_write(priv->regmap, MT6358_AFE_UL_SRC_CON0_L, 0x0001);
1749
1750	return 0;
1751}
1752
1753static void mt6358_amic_disable(struct mt6358_priv *priv)
1754{
1755	unsigned int mic_type = priv->mux_select[MUX_MIC_TYPE];
1756	unsigned int mux_pga_l = priv->mux_select[MUX_PGA_L];
1757	unsigned int mux_pga_r = priv->mux_select[MUX_PGA_R];
1758
1759	dev_info(priv->dev, "%s(), mux, mic %u, pga l %u, pga r %u\n",
1760		 __func__, mic_type, mux_pga_l, mux_pga_r);
1761
1762	/* UL turn off */
1763	regmap_update_bits(priv->regmap, MT6358_AFE_UL_SRC_CON0_L,
1764			   0x0001, 0x0000);
1765
1766	/* disable aud_pad TX fifos */
1767	mt6358_mtkaif_tx_disable(priv);
1768
1769	/* L ADC input sel : off, disable L ADC */
1770	regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON0,
1771			   0xf000, 0x0000);
1772	/* L preamplifier DCCEN */
1773	regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON0,
1774			   0x1 << 1, 0x0);
1775	/* L preamplifier input sel : off, L PGA 0 dB gain */
1776	regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON0,
1777			   0xfffb, 0x0000);
1778
1779	/* disable L preamplifier DCC precharge */
1780	regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON0,
1781			   0x1 << 2, 0x0);
1782
1783	/* R ADC input sel : off, disable R ADC */
1784	regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON1,
1785			   0xf000, 0x0000);
1786	/* R preamplifier DCCEN */
1787	regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON1,
1788			   0x1 << 1, 0x0);
1789	/* R preamplifier input sel : off, R PGA 0 dB gain */
1790	regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON1,
1791			   0x0ffb, 0x0000);
1792
1793	/* disable R preamplifier DCC precharge */
1794	regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON1,
1795			   0x1 << 2, 0x0);
1796
1797	/* mic bias */
1798	/* Disable MICBIAS0, MISBIAS0 = 1P7V */
1799	regmap_write(priv->regmap, MT6358_AUDENC_ANA_CON9, 0x0000);
1800
1801	/* Disable MICBIAS1 */
1802	regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON10,
1803			   0x0001, 0x0000);
1804
1805	if (IS_DCC_BASE(mic_type)) {
1806		/* dcclk_gen_on=1'b0 */
1807		regmap_write(priv->regmap, MT6358_AFE_DCCLK_CFG0, 0x2060);
1808		/* dcclk_pdn=1'b1 */
1809		regmap_write(priv->regmap, MT6358_AFE_DCCLK_CFG0, 0x2062);
1810		/* dcclk_ref_ck_sel=2'b00 */
1811		regmap_write(priv->regmap, MT6358_AFE_DCCLK_CFG0, 0x2062);
1812		/* dcclk_div=11'b00100000011 */
1813		regmap_write(priv->regmap, MT6358_AFE_DCCLK_CFG0, 0x2062);
1814	}
1815}
1816
1817static int mt6358_dmic_enable(struct mt6358_priv *priv)
1818{
1819	dev_info(priv->dev, "%s()\n", __func__);
1820
1821	/* mic bias */
1822	/* Enable MICBIAS0, MISBIAS0 = 1P9V */
1823	regmap_write(priv->regmap, MT6358_AUDENC_ANA_CON9, 0x0021);
1824
1825	/* RG_BANDGAPGEN=1'b0 */
1826	regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON10,
1827			   0x1 << 12, 0x0);
1828
1829	/* DMIC enable */
1830	regmap_write(priv->regmap, MT6358_AUDENC_ANA_CON8, 0x0005);
1831
1832	/* here to set digital part */
1833	mt6358_mtkaif_tx_enable(priv);
1834
1835	/* UL dmic setting */
1836	if (priv->dmic_one_wire_mode)
1837		regmap_write(priv->regmap, MT6358_AFE_UL_SRC_CON0_H, 0x0400);
1838	else
1839		regmap_write(priv->regmap, MT6358_AFE_UL_SRC_CON0_H, 0x0080);
1840
1841	/* UL turn on */
1842	regmap_write(priv->regmap, MT6358_AFE_UL_SRC_CON0_L, 0x0003);
1843
1844	/* Prevent pop noise form dmic hw */
1845	msleep(100);
1846
1847	return 0;
1848}
1849
1850static void mt6358_dmic_disable(struct mt6358_priv *priv)
1851{
1852	dev_info(priv->dev, "%s()\n", __func__);
1853
1854	/* UL turn off */
1855	regmap_update_bits(priv->regmap, MT6358_AFE_UL_SRC_CON0_L,
1856			   0x0003, 0x0000);
1857
1858	/* disable aud_pad TX fifos */
1859	mt6358_mtkaif_tx_disable(priv);
1860
1861	/* DMIC disable */
1862	regmap_write(priv->regmap, MT6358_AUDENC_ANA_CON8, 0x0000);
1863
1864	/* mic bias */
1865	/* MISBIAS0 = 1P7V */
1866	regmap_write(priv->regmap, MT6358_AUDENC_ANA_CON9, 0x0001);
1867
1868	/* RG_BANDGAPGEN=1'b0 */
1869	regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON10,
1870			   0x1 << 12, 0x0);
1871
1872	/* MICBIA0 disable */
1873	regmap_write(priv->regmap, MT6358_AUDENC_ANA_CON9, 0x0000);
1874}
1875
1876static void mt6358_restore_pga(struct mt6358_priv *priv)
1877{
1878	unsigned int gain_l, gain_r;
1879
1880	gain_l = priv->ana_gain[AUDIO_ANALOG_VOLUME_MICAMP1];
1881	gain_r = priv->ana_gain[AUDIO_ANALOG_VOLUME_MICAMP2];
1882
1883	regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON0,
1884			   RG_AUDPREAMPLGAIN_MASK_SFT,
1885			   gain_l << RG_AUDPREAMPLGAIN_SFT);
1886	regmap_update_bits(priv->regmap, MT6358_AUDENC_ANA_CON1,
1887			   RG_AUDPREAMPRGAIN_MASK_SFT,
1888			   gain_r << RG_AUDPREAMPRGAIN_SFT);
1889}
1890
1891static int mt_mic_type_event(struct snd_soc_dapm_widget *w,
1892			     struct snd_kcontrol *kcontrol,
1893			     int event)
1894{
1895	struct snd_soc_component *cmpnt = snd_soc_dapm_to_component(w->dapm);
1896	struct mt6358_priv *priv = snd_soc_component_get_drvdata(cmpnt);
1897	unsigned int mux = dapm_kcontrol_get_value(w->kcontrols[0]);
1898
1899	dev_dbg(priv->dev, "%s(), event 0x%x, mux %u\n",
1900		__func__, event, mux);
1901
1902	switch (event) {
1903	case SND_SOC_DAPM_WILL_PMU:
1904		priv->mux_select[MUX_MIC_TYPE] = mux;
1905		break;
1906	case SND_SOC_DAPM_PRE_PMU:
1907		switch (mux) {
1908		case MIC_TYPE_MUX_DMIC:
1909			mt6358_dmic_enable(priv);
1910			break;
1911		default:
1912			mt6358_amic_enable(priv);
1913			break;
1914		}
1915		mt6358_restore_pga(priv);
1916
1917		break;
1918	case SND_SOC_DAPM_POST_PMD:
1919		switch (priv->mux_select[MUX_MIC_TYPE]) {
1920		case MIC_TYPE_MUX_DMIC:
1921			mt6358_dmic_disable(priv);
1922			break;
1923		default:
1924			mt6358_amic_disable(priv);
1925			break;
1926		}
1927
1928		priv->mux_select[MUX_MIC_TYPE] = mux;
1929		break;
1930	default:
1931		break;
1932	}
1933
1934	return 0;
1935}
1936
1937static int mt_adc_l_event(struct snd_soc_dapm_widget *w,
1938			  struct snd_kcontrol *kcontrol,
1939			  int event)
1940{
1941	struct snd_soc_component *cmpnt = snd_soc_dapm_to_component(w->dapm);
1942	struct mt6358_priv *priv = snd_soc_component_get_drvdata(cmpnt);
1943	unsigned int mux = dapm_kcontrol_get_value(w->kcontrols[0]);
1944
1945	dev_dbg(priv->dev, "%s(), event = 0x%x, mux %u\n",
1946		__func__, event, mux);
1947
1948	priv->mux_select[MUX_ADC_L] = mux;
1949
1950	return 0;
1951}
1952
1953static int mt_adc_r_event(struct snd_soc_dapm_widget *w,
1954			  struct snd_kcontrol *kcontrol,
1955			  int event)
1956{
1957	struct snd_soc_component *cmpnt = snd_soc_dapm_to_component(w->dapm);
1958	struct mt6358_priv *priv = snd_soc_component_get_drvdata(cmpnt);
1959	unsigned int mux = dapm_kcontrol_get_value(w->kcontrols[0]);
1960
1961	dev_dbg(priv->dev, "%s(), event = 0x%x, mux %u\n",
1962		__func__, event, mux);
1963
1964	priv->mux_select[MUX_ADC_R] = mux;
1965
1966	return 0;
1967}
1968
1969static int mt_pga_left_event(struct snd_soc_dapm_widget *w,
1970			     struct snd_kcontrol *kcontrol,
1971			     int event)
1972{
1973	struct snd_soc_component *cmpnt = snd_soc_dapm_to_component(w->dapm);
1974	struct mt6358_priv *priv = snd_soc_component_get_drvdata(cmpnt);
1975	unsigned int mux = dapm_kcontrol_get_value(w->kcontrols[0]);
1976
1977	dev_dbg(priv->dev, "%s(), event = 0x%x, mux %u\n",
1978		__func__, event, mux);
1979
1980	priv->mux_select[MUX_PGA_L] = mux;
1981
1982	return 0;
1983}
1984
1985static int mt_pga_right_event(struct snd_soc_dapm_widget *w,
1986			      struct snd_kcontrol *kcontrol,
1987			      int event)
1988{
1989	struct snd_soc_component *cmpnt = snd_soc_dapm_to_component(w->dapm);
1990	struct mt6358_priv *priv = snd_soc_component_get_drvdata(cmpnt);
1991	unsigned int mux = dapm_kcontrol_get_value(w->kcontrols[0]);
1992
1993	dev_dbg(priv->dev, "%s(), event = 0x%x, mux %u\n",
1994		__func__, event, mux);
1995
1996	priv->mux_select[MUX_PGA_R] = mux;
1997
1998	return 0;
1999}
2000
2001static int mt_delay_250_event(struct snd_soc_dapm_widget *w,
2002			      struct snd_kcontrol *kcontrol,
2003			      int event)
2004{
2005	switch (event) {
2006	case SND_SOC_DAPM_POST_PMU:
2007		usleep_range(250, 270);
2008		break;
2009	case SND_SOC_DAPM_PRE_PMD:
2010		usleep_range(250, 270);
2011		break;
2012	default:
2013		break;
2014	}
2015
2016	return 0;
2017}
2018
2019/* DAPM Widgets */
2020static const struct snd_soc_dapm_widget mt6358_dapm_widgets[] = {
2021	/* Global Supply*/
2022	SND_SOC_DAPM_SUPPLY_S("CLK_BUF", SUPPLY_SEQ_CLK_BUF,
2023			      MT6358_DCXO_CW14,
2024			      RG_XO_AUDIO_EN_M_SFT, 0, NULL, 0),
2025	SND_SOC_DAPM_SUPPLY_S("AUDGLB", SUPPLY_SEQ_AUD_GLB,
2026			      MT6358_AUDDEC_ANA_CON13,
2027			      RG_AUDGLB_PWRDN_VA28_SFT, 1, NULL, 0),
2028	SND_SOC_DAPM_SUPPLY_S("CLKSQ Audio", SUPPLY_SEQ_CLKSQ,
2029			      MT6358_AUDENC_ANA_CON6,
2030			      RG_CLKSQ_EN_SFT, 0,
2031			      mt_clksq_event,
2032			      SND_SOC_DAPM_PRE_PMU),
2033	SND_SOC_DAPM_SUPPLY_S("AUDNCP_CK", SUPPLY_SEQ_TOP_CK,
2034			      MT6358_AUD_TOP_CKPDN_CON0,
2035			      RG_AUDNCP_CK_PDN_SFT, 1, NULL, 0),
2036	SND_SOC_DAPM_SUPPLY_S("ZCD13M_CK", SUPPLY_SEQ_TOP_CK,
2037			      MT6358_AUD_TOP_CKPDN_CON0,
2038			      RG_ZCD13M_CK_PDN_SFT, 1, NULL, 0),
2039	SND_SOC_DAPM_SUPPLY_S("AUD_CK", SUPPLY_SEQ_TOP_CK_LAST,
2040			      MT6358_AUD_TOP_CKPDN_CON0,
2041			      RG_AUD_CK_PDN_SFT, 1,
2042			      mt_delay_250_event,
2043			      SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
2044	SND_SOC_DAPM_SUPPLY_S("AUDIF_CK", SUPPLY_SEQ_TOP_CK,
2045			      MT6358_AUD_TOP_CKPDN_CON0,
2046			      RG_AUDIF_CK_PDN_SFT, 1, NULL, 0),
2047
2048	/* Digital Clock */
2049	SND_SOC_DAPM_SUPPLY_S("AUDIO_TOP_AFE_CTL", SUPPLY_SEQ_AUD_TOP_LAST,
2050			      MT6358_AUDIO_TOP_CON0,
2051			      PDN_AFE_CTL_SFT, 1,
2052			      mt_delay_250_event,
2053			      SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
2054	SND_SOC_DAPM_SUPPLY_S("AUDIO_TOP_DAC_CTL", SUPPLY_SEQ_AUD_TOP,
2055			      MT6358_AUDIO_TOP_CON0,
2056			      PDN_DAC_CTL_SFT, 1, NULL, 0),
2057	SND_SOC_DAPM_SUPPLY_S("AUDIO_TOP_ADC_CTL", SUPPLY_SEQ_AUD_TOP,
2058			      MT6358_AUDIO_TOP_CON0,
2059			      PDN_ADC_CTL_SFT, 1, NULL, 0),
2060	SND_SOC_DAPM_SUPPLY_S("AUDIO_TOP_I2S_DL", SUPPLY_SEQ_AUD_TOP,
2061			      MT6358_AUDIO_TOP_CON0,
2062			      PDN_I2S_DL_CTL_SFT, 1, NULL, 0),
2063	SND_SOC_DAPM_SUPPLY_S("AUDIO_TOP_PWR_CLK", SUPPLY_SEQ_AUD_TOP,
2064			      MT6358_AUDIO_TOP_CON0,
2065			      PWR_CLK_DIS_CTL_SFT, 1, NULL, 0),
2066	SND_SOC_DAPM_SUPPLY_S("AUDIO_TOP_PDN_AFE_TESTMODEL", SUPPLY_SEQ_AUD_TOP,
2067			      MT6358_AUDIO_TOP_CON0,
2068			      PDN_AFE_TESTMODEL_CTL_SFT, 1, NULL, 0),
2069	SND_SOC_DAPM_SUPPLY_S("AUDIO_TOP_PDN_RESERVED", SUPPLY_SEQ_AUD_TOP,
2070			      MT6358_AUDIO_TOP_CON0,
2071			      PDN_RESERVED_SFT, 1, NULL, 0),
2072
2073	SND_SOC_DAPM_SUPPLY("DL Digital Clock", SND_SOC_NOPM,
2074			    0, 0, NULL, 0),
2075
2076	/* AFE ON */
2077	SND_SOC_DAPM_SUPPLY_S("AFE_ON", SUPPLY_SEQ_AFE,
2078			      MT6358_AFE_UL_DL_CON0, AFE_ON_SFT, 0,
2079			      NULL, 0),
2080
2081	/* AIF Rx*/
2082	SND_SOC_DAPM_AIF_IN_E("AIF_RX", "AIF1 Playback", 0,
2083			      MT6358_AFE_DL_SRC2_CON0_L,
2084			      DL_2_SRC_ON_TMP_CTL_PRE_SFT, 0,
2085			      mt_aif_in_event,
2086			      SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
2087
2088	/* DL Supply */
2089	SND_SOC_DAPM_SUPPLY("DL Power Supply", SND_SOC_NOPM,
2090			    0, 0, NULL, 0),
2091
2092	/* DAC */
2093	SND_SOC_DAPM_MUX("DAC In Mux", SND_SOC_NOPM, 0, 0, &dac_in_mux_control),
2094
2095	SND_SOC_DAPM_DAC("DACL", NULL, SND_SOC_NOPM, 0, 0),
2096
2097	SND_SOC_DAPM_DAC("DACR", NULL, SND_SOC_NOPM, 0, 0),
2098
2099	/* LOL */
2100	SND_SOC_DAPM_MUX("LOL Mux", SND_SOC_NOPM, 0, 0, &lo_in_mux_control),
2101
2102	SND_SOC_DAPM_SUPPLY("LO Stability Enh", MT6358_AUDDEC_ANA_CON7,
2103			    RG_LOOUTPUTSTBENH_VAUDP15_SFT, 0, NULL, 0),
2104
2105	SND_SOC_DAPM_OUT_DRV("LOL Buffer", MT6358_AUDDEC_ANA_CON7,
2106			     RG_AUDLOLPWRUP_VAUDP15_SFT, 0, NULL, 0),
2107
2108	/* Headphone */
2109	SND_SOC_DAPM_MUX_E("HPL Mux", SND_SOC_NOPM, 0, 0,
2110			   &hpl_in_mux_control,
2111			   mt_hp_event,
2112			   SND_SOC_DAPM_PRE_PMU |
2113			   SND_SOC_DAPM_PRE_PMD),
2114
2115	SND_SOC_DAPM_MUX_E("HPR Mux", SND_SOC_NOPM, 0, 0,
2116			   &hpr_in_mux_control,
2117			   mt_hp_event,
2118			   SND_SOC_DAPM_PRE_PMU |
2119			   SND_SOC_DAPM_PRE_PMD),
2120
2121	/* Receiver */
2122	SND_SOC_DAPM_MUX_E("RCV Mux", SND_SOC_NOPM, 0, 0,
2123			   &rcv_in_mux_control,
2124			   mt_rcv_event,
2125			   SND_SOC_DAPM_PRE_PMU |
2126			   SND_SOC_DAPM_PRE_PMD),
2127
2128	/* Outputs */
2129	SND_SOC_DAPM_OUTPUT("Receiver"),
2130	SND_SOC_DAPM_OUTPUT("Headphone L"),
2131	SND_SOC_DAPM_OUTPUT("Headphone R"),
2132	SND_SOC_DAPM_OUTPUT("Headphone L Ext Spk Amp"),
2133	SND_SOC_DAPM_OUTPUT("Headphone R Ext Spk Amp"),
2134	SND_SOC_DAPM_OUTPUT("LINEOUT L"),
2135	SND_SOC_DAPM_OUTPUT("LINEOUT L HSSPK"),
2136
2137	/* SGEN */
2138	SND_SOC_DAPM_SUPPLY("SGEN DL Enable", MT6358_AFE_SGEN_CFG0,
2139			    SGEN_DAC_EN_CTL_SFT, 0, NULL, 0),
2140	SND_SOC_DAPM_SUPPLY("SGEN MUTE", MT6358_AFE_SGEN_CFG0,
2141			    SGEN_MUTE_SW_CTL_SFT, 1,
2142			    mt_sgen_event,
2143			    SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
2144	SND_SOC_DAPM_SUPPLY("SGEN DL SRC", MT6358_AFE_DL_SRC2_CON0_L,
2145			    DL_2_SRC_ON_TMP_CTL_PRE_SFT, 0, NULL, 0),
2146
2147	SND_SOC_DAPM_INPUT("SGEN DL"),
2148
2149	/* Uplinks */
2150	SND_SOC_DAPM_AIF_OUT_E("AIF1TX", "AIF1 Capture", 0,
2151			       SND_SOC_NOPM, 0, 0,
2152			       mt_aif_out_event,
2153			       SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
2154
2155	SND_SOC_DAPM_SUPPLY_S("ADC Supply", SUPPLY_SEQ_ADC_SUPPLY,
2156			      SND_SOC_NOPM, 0, 0,
2157			      mt_adc_supply_event,
2158			      SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
2159
2160	/* Uplinks MUX */
2161	SND_SOC_DAPM_MUX("AIF Out Mux", SND_SOC_NOPM, 0, 0,
2162			 &aif_out_mux_control),
2163
2164	SND_SOC_DAPM_MUX_E("Mic Type Mux", SND_SOC_NOPM, 0, 0,
2165			   &mic_type_mux_control,
2166			   mt_mic_type_event,
2167			   SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD |
2168			   SND_SOC_DAPM_WILL_PMU),
2169
2170	SND_SOC_DAPM_MUX_E("ADC L Mux", SND_SOC_NOPM, 0, 0,
2171			   &adc_left_mux_control,
2172			   mt_adc_l_event,
2173			   SND_SOC_DAPM_WILL_PMU),
2174	SND_SOC_DAPM_MUX_E("ADC R Mux", SND_SOC_NOPM, 0, 0,
2175			   &adc_right_mux_control,
2176			   mt_adc_r_event,
2177			   SND_SOC_DAPM_WILL_PMU),
2178
2179	SND_SOC_DAPM_ADC("ADC L", NULL, SND_SOC_NOPM, 0, 0),
2180	SND_SOC_DAPM_ADC("ADC R", NULL, SND_SOC_NOPM, 0, 0),
2181
2182	SND_SOC_DAPM_MUX_E("PGA L Mux", SND_SOC_NOPM, 0, 0,
2183			   &pga_left_mux_control,
2184			   mt_pga_left_event,
2185			   SND_SOC_DAPM_WILL_PMU),
2186	SND_SOC_DAPM_MUX_E("PGA R Mux", SND_SOC_NOPM, 0, 0,
2187			   &pga_right_mux_control,
2188			   mt_pga_right_event,
2189			   SND_SOC_DAPM_WILL_PMU),
2190
2191	SND_SOC_DAPM_PGA("PGA L", SND_SOC_NOPM, 0, 0, NULL, 0),
2192	SND_SOC_DAPM_PGA("PGA R", SND_SOC_NOPM, 0, 0, NULL, 0),
2193
2194	/* UL input */
2195	SND_SOC_DAPM_INPUT("AIN0"),
2196	SND_SOC_DAPM_INPUT("AIN1"),
2197	SND_SOC_DAPM_INPUT("AIN2"),
2198};
2199
2200static const struct snd_soc_dapm_route mt6358_dapm_routes[] = {
2201	/* Capture */
2202	{"AIF1TX", NULL, "AIF Out Mux"},
2203	{"AIF1TX", NULL, "CLK_BUF"},
2204	{"AIF1TX", NULL, "AUDGLB"},
2205	{"AIF1TX", NULL, "CLKSQ Audio"},
2206
2207	{"AIF1TX", NULL, "AUD_CK"},
2208	{"AIF1TX", NULL, "AUDIF_CK"},
2209
2210	{"AIF1TX", NULL, "AUDIO_TOP_AFE_CTL"},
2211	{"AIF1TX", NULL, "AUDIO_TOP_ADC_CTL"},
2212	{"AIF1TX", NULL, "AUDIO_TOP_PWR_CLK"},
2213	{"AIF1TX", NULL, "AUDIO_TOP_PDN_RESERVED"},
2214	{"AIF1TX", NULL, "AUDIO_TOP_I2S_DL"},
2215
2216	{"AIF1TX", NULL, "AFE_ON"},
2217
2218	{"AIF Out Mux", NULL, "Mic Type Mux"},
2219
2220	{"Mic Type Mux", "ACC", "ADC L"},
2221	{"Mic Type Mux", "ACC", "ADC R"},
2222	{"Mic Type Mux", "DCC", "ADC L"},
2223	{"Mic Type Mux", "DCC", "ADC R"},
2224	{"Mic Type Mux", "DCC_ECM_DIFF", "ADC L"},
2225	{"Mic Type Mux", "DCC_ECM_DIFF", "ADC R"},
2226	{"Mic Type Mux", "DCC_ECM_SINGLE", "ADC L"},
2227	{"Mic Type Mux", "DCC_ECM_SINGLE", "ADC R"},
2228	{"Mic Type Mux", "DMIC", "AIN0"},
2229	{"Mic Type Mux", "DMIC", "AIN2"},
2230
2231	{"ADC L", NULL, "ADC L Mux"},
2232	{"ADC L", NULL, "ADC Supply"},
2233	{"ADC R", NULL, "ADC R Mux"},
2234	{"ADC R", NULL, "ADC Supply"},
2235
2236	{"ADC L Mux", "Left Preamplifier", "PGA L"},
2237
2238	{"ADC R Mux", "Right Preamplifier", "PGA R"},
2239
2240	{"PGA L", NULL, "PGA L Mux"},
2241	{"PGA R", NULL, "PGA R Mux"},
2242
2243	{"PGA L Mux", "AIN0", "AIN0"},
2244	{"PGA L Mux", "AIN1", "AIN1"},
2245	{"PGA L Mux", "AIN2", "AIN2"},
2246
2247	{"PGA R Mux", "AIN0", "AIN0"},
2248	{"PGA R Mux", "AIN1", "AIN1"},
2249	{"PGA R Mux", "AIN2", "AIN2"},
2250
2251	/* DL Supply */
2252	{"DL Power Supply", NULL, "CLK_BUF"},
2253	{"DL Power Supply", NULL, "AUDGLB"},
2254	{"DL Power Supply", NULL, "CLKSQ Audio"},
2255
2256	{"DL Power Supply", NULL, "AUDNCP_CK"},
2257	{"DL Power Supply", NULL, "ZCD13M_CK"},
2258	{"DL Power Supply", NULL, "AUD_CK"},
2259	{"DL Power Supply", NULL, "AUDIF_CK"},
2260
2261	/* DL Digital Supply */
2262	{"DL Digital Clock", NULL, "AUDIO_TOP_AFE_CTL"},
2263	{"DL Digital Clock", NULL, "AUDIO_TOP_DAC_CTL"},
2264	{"DL Digital Clock", NULL, "AUDIO_TOP_PWR_CLK"},
2265
2266	{"DL Digital Clock", NULL, "AFE_ON"},
2267
2268	{"AIF_RX", NULL, "DL Digital Clock"},
2269
2270	/* DL Path */
2271	{"DAC In Mux", "Normal Path", "AIF_RX"},
2272
2273	{"DAC In Mux", "Sgen", "SGEN DL"},
2274	{"SGEN DL", NULL, "SGEN DL SRC"},
2275	{"SGEN DL", NULL, "SGEN MUTE"},
2276	{"SGEN DL", NULL, "SGEN DL Enable"},
2277	{"SGEN DL", NULL, "DL Digital Clock"},
2278	{"SGEN DL", NULL, "AUDIO_TOP_PDN_AFE_TESTMODEL"},
2279
2280	{"DACL", NULL, "DAC In Mux"},
2281	{"DACL", NULL, "DL Power Supply"},
2282
2283	{"DACR", NULL, "DAC In Mux"},
2284	{"DACR", NULL, "DL Power Supply"},
2285
2286	/* Lineout Path */
2287	{"LOL Mux", "Playback", "DACL"},
2288
2289	{"LOL Buffer", NULL, "LOL Mux"},
2290	{"LOL Buffer", NULL, "LO Stability Enh"},
2291
2292	{"LINEOUT L", NULL, "LOL Buffer"},
2293
2294	/* Headphone Path */
2295	{"HPL Mux", "Audio Playback", "DACL"},
2296	{"HPR Mux", "Audio Playback", "DACR"},
2297	{"HPL Mux", "HP Impedance", "DACL"},
2298	{"HPR Mux", "HP Impedance", "DACR"},
2299	{"HPL Mux", "LoudSPK Playback", "DACL"},
2300	{"HPR Mux", "LoudSPK Playback", "DACR"},
2301
2302	{"Headphone L", NULL, "HPL Mux"},
2303	{"Headphone R", NULL, "HPR Mux"},
2304	{"Headphone L Ext Spk Amp", NULL, "HPL Mux"},
2305	{"Headphone R Ext Spk Amp", NULL, "HPR Mux"},
2306	{"LINEOUT L HSSPK", NULL, "HPL Mux"},
2307
2308	/* Receiver Path */
2309	{"RCV Mux", "Voice Playback", "DACL"},
2310	{"Receiver", NULL, "RCV Mux"},
2311};
2312
2313static int mt6358_codec_dai_hw_params(struct snd_pcm_substream *substream,
2314				      struct snd_pcm_hw_params *params,
2315				      struct snd_soc_dai *dai)
2316{
2317	struct snd_soc_component *cmpnt = dai->component;
2318	struct mt6358_priv *priv = snd_soc_component_get_drvdata(cmpnt);
2319	unsigned int rate = params_rate(params);
2320
2321	dev_info(priv->dev, "%s(), substream->stream %d, rate %d, number %d\n",
2322		 __func__,
2323		 substream->stream,
2324		 rate,
2325		 substream->number);
2326
2327	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2328		priv->dl_rate = rate;
2329	else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2330		priv->ul_rate = rate;
2331
2332	return 0;
2333}
2334
2335static const struct snd_soc_dai_ops mt6358_codec_dai_ops = {
2336	.hw_params = mt6358_codec_dai_hw_params,
2337};
2338
2339#define MT6358_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |\
2340			SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE |\
2341			SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE |\
2342			SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE |\
2343			SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE |\
2344			SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE)
2345
2346static struct snd_soc_dai_driver mt6358_dai_driver[] = {
2347	{
2348		.name = "mt6358-snd-codec-aif1",
2349		.playback = {
2350			.stream_name = "AIF1 Playback",
2351			.channels_min = 1,
2352			.channels_max = 2,
2353			.rates = SNDRV_PCM_RATE_8000_48000 |
2354				 SNDRV_PCM_RATE_96000 |
2355				 SNDRV_PCM_RATE_192000,
2356			.formats = MT6358_FORMATS,
2357		},
2358		.capture = {
2359			.stream_name = "AIF1 Capture",
2360			.channels_min = 1,
2361			.channels_max = 2,
2362			.rates = SNDRV_PCM_RATE_8000 |
2363				 SNDRV_PCM_RATE_16000 |
2364				 SNDRV_PCM_RATE_32000 |
2365				 SNDRV_PCM_RATE_48000,
2366			.formats = MT6358_FORMATS,
2367		},
2368		.ops = &mt6358_codec_dai_ops,
2369	},
2370};
2371
2372static void mt6358_codec_init_reg(struct mt6358_priv *priv)
2373{
2374	/* Disable HeadphoneL/HeadphoneR short circuit protection */
2375	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON0,
2376			   RG_AUDHPLSCDISABLE_VAUDP15_MASK_SFT,
2377			   0x1 << RG_AUDHPLSCDISABLE_VAUDP15_SFT);
2378	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON0,
2379			   RG_AUDHPRSCDISABLE_VAUDP15_MASK_SFT,
2380			   0x1 << RG_AUDHPRSCDISABLE_VAUDP15_SFT);
2381	/* Disable voice short circuit protection */
2382	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON6,
2383			   RG_AUDHSSCDISABLE_VAUDP15_MASK_SFT,
2384			   0x1 << RG_AUDHSSCDISABLE_VAUDP15_SFT);
2385	/* disable LO buffer left short circuit protection */
2386	regmap_update_bits(priv->regmap, MT6358_AUDDEC_ANA_CON7,
2387			   RG_AUDLOLSCDISABLE_VAUDP15_MASK_SFT,
2388			   0x1 << RG_AUDLOLSCDISABLE_VAUDP15_SFT);
2389
2390	/* accdet s/w enable */
2391	regmap_update_bits(priv->regmap, MT6358_ACCDET_CON13,
2392			   0xFFFF, 0x700E);
2393
2394	/* gpio miso driving set to 4mA */
2395	regmap_write(priv->regmap, MT6358_DRV_CON3, 0x8888);
2396
2397	/* set gpio */
2398	playback_gpio_reset(priv);
2399	capture_gpio_reset(priv);
2400}
2401
2402static int mt6358_codec_probe(struct snd_soc_component *cmpnt)
2403{
2404	struct mt6358_priv *priv = snd_soc_component_get_drvdata(cmpnt);
2405	int ret;
2406
2407	snd_soc_component_init_regmap(cmpnt, priv->regmap);
2408
2409	mt6358_codec_init_reg(priv);
2410
2411	priv->avdd_reg = devm_regulator_get(priv->dev, "Avdd");
2412	if (IS_ERR(priv->avdd_reg)) {
2413		dev_err(priv->dev, "%s() have no Avdd supply", __func__);
2414		return PTR_ERR(priv->avdd_reg);
2415	}
2416
2417	ret = regulator_enable(priv->avdd_reg);
2418	if (ret)
2419		return  ret;
2420
2421	return 0;
2422}
2423
2424static const struct snd_soc_component_driver mt6358_soc_component_driver = {
2425	.probe = mt6358_codec_probe,
2426	.controls = mt6358_snd_controls,
2427	.num_controls = ARRAY_SIZE(mt6358_snd_controls),
2428	.dapm_widgets = mt6358_dapm_widgets,
2429	.num_dapm_widgets = ARRAY_SIZE(mt6358_dapm_widgets),
2430	.dapm_routes = mt6358_dapm_routes,
2431	.num_dapm_routes = ARRAY_SIZE(mt6358_dapm_routes),
 
2432};
2433
2434static void mt6358_parse_dt(struct mt6358_priv *priv)
2435{
2436	int ret;
2437	struct device *dev = priv->dev;
2438
2439	ret = of_property_read_u32(dev->of_node, "mediatek,dmic-mode",
2440				   &priv->dmic_one_wire_mode);
2441	if (ret) {
2442		dev_warn(priv->dev, "%s() failed to read dmic-mode\n",
2443			 __func__);
2444		priv->dmic_one_wire_mode = 0;
2445	}
2446}
2447
2448static int mt6358_platform_driver_probe(struct platform_device *pdev)
2449{
2450	struct mt6358_priv *priv;
2451	struct mt6397_chip *mt6397 = dev_get_drvdata(pdev->dev.parent);
2452
2453	priv = devm_kzalloc(&pdev->dev,
2454			    sizeof(struct mt6358_priv),
2455			    GFP_KERNEL);
2456	if (!priv)
2457		return -ENOMEM;
2458
2459	dev_set_drvdata(&pdev->dev, priv);
2460
2461	priv->dev = &pdev->dev;
2462
2463	priv->regmap = mt6397->regmap;
2464	if (IS_ERR(priv->regmap))
2465		return PTR_ERR(priv->regmap);
2466
2467	mt6358_parse_dt(priv);
2468
2469	dev_info(priv->dev, "%s(), dev name %s\n",
2470		 __func__, dev_name(&pdev->dev));
2471
2472	return devm_snd_soc_register_component(&pdev->dev,
2473				      &mt6358_soc_component_driver,
2474				      mt6358_dai_driver,
2475				      ARRAY_SIZE(mt6358_dai_driver));
2476}
2477
2478static const struct of_device_id mt6358_of_match[] = {
2479	{.compatible = "mediatek,mt6358-sound",},
 
2480	{}
2481};
2482MODULE_DEVICE_TABLE(of, mt6358_of_match);
2483
2484static struct platform_driver mt6358_platform_driver = {
2485	.driver = {
2486		.name = "mt6358-sound",
2487		.of_match_table = mt6358_of_match,
2488	},
2489	.probe = mt6358_platform_driver_probe,
2490};
2491
2492module_platform_driver(mt6358_platform_driver)
2493
2494/* Module information */
2495MODULE_DESCRIPTION("MT6358 ALSA SoC codec driver");
2496MODULE_AUTHOR("KaiChieh Chuang <kaichieh.chuang@mediatek.com>");
2497MODULE_LICENSE("GPL v2");