cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

rt711.c (35838B)


      1// SPDX-License-Identifier: GPL-2.0
      2//
      3// rt711.c -- rt711 ALSA SoC audio driver
      4//
      5// Copyright(c) 2019 Realtek Semiconductor Corp.
      6//
      7//
      8
      9#include <linux/module.h>
     10#include <linux/moduleparam.h>
     11#include <linux/kernel.h>
     12#include <linux/init.h>
     13#include <linux/delay.h>
     14#include <linux/pm_runtime.h>
     15#include <linux/pm.h>
     16#include <linux/soundwire/sdw.h>
     17#include <linux/regmap.h>
     18#include <linux/slab.h>
     19#include <sound/core.h>
     20#include <sound/pcm.h>
     21#include <sound/pcm_params.h>
     22#include <sound/soc.h>
     23#include <sound/soc-dapm.h>
     24#include <sound/initval.h>
     25#include <sound/tlv.h>
     26#include <sound/hda_verbs.h>
     27#include <sound/jack.h>
     28
     29#include "rt711.h"
     30
     31static int rt711_index_write(struct regmap *regmap,
     32		unsigned int nid, unsigned int reg, unsigned int value)
     33{
     34	int ret;
     35	unsigned int addr = ((RT711_PRIV_INDEX_W_H | nid) << 8) | reg;
     36
     37	ret = regmap_write(regmap, addr, value);
     38	if (ret < 0)
     39		pr_err("Failed to set private value: %06x <= %04x ret=%d\n",
     40			addr, value, ret);
     41
     42	return ret;
     43}
     44
     45static int rt711_index_read(struct regmap *regmap,
     46		unsigned int nid, unsigned int reg, unsigned int *value)
     47{
     48	int ret;
     49	unsigned int addr = ((RT711_PRIV_INDEX_W_H | nid) << 8) | reg;
     50
     51	*value = 0;
     52	ret = regmap_read(regmap, addr, value);
     53	if (ret < 0)
     54		pr_err("Failed to get private value: %06x => %04x ret=%d\n",
     55			addr, *value, ret);
     56
     57	return ret;
     58}
     59
     60static int rt711_index_update_bits(struct regmap *regmap, unsigned int nid,
     61			unsigned int reg, unsigned int mask, unsigned int val)
     62{
     63	unsigned int tmp, orig;
     64	int ret;
     65
     66	ret = rt711_index_read(regmap, nid, reg, &orig);
     67	if (ret < 0)
     68		return ret;
     69
     70	tmp = orig & ~mask;
     71	tmp |= val & mask;
     72
     73	return rt711_index_write(regmap, nid, reg, tmp);
     74}
     75
     76static void rt711_reset(struct regmap *regmap)
     77{
     78	regmap_write(regmap, RT711_FUNC_RESET, 0);
     79	rt711_index_update_bits(regmap, RT711_VENDOR_REG,
     80		RT711_PARA_VERB_CTL, RT711_HIDDEN_REG_SW_RESET,
     81		RT711_HIDDEN_REG_SW_RESET);
     82}
     83
     84static int rt711_calibration(struct rt711_priv *rt711)
     85{
     86	unsigned int val, loop = 0;
     87	struct device *dev;
     88	struct regmap *regmap = rt711->regmap;
     89	int ret = 0;
     90
     91	mutex_lock(&rt711->calibrate_mutex);
     92	regmap_write(rt711->regmap,
     93		RT711_SET_AUDIO_POWER_STATE, AC_PWRST_D0);
     94
     95	dev = regmap_get_device(regmap);
     96
     97	/* Calibration manual mode */
     98	rt711_index_update_bits(regmap, RT711_VENDOR_REG, RT711_FSM_CTL,
     99		0xf, 0x0);
    100
    101	/* trigger */
    102	rt711_index_update_bits(regmap, RT711_VENDOR_CALI,
    103		RT711_DAC_DC_CALI_CTL1, RT711_DAC_DC_CALI_TRIGGER,
    104		RT711_DAC_DC_CALI_TRIGGER);
    105
    106	/* wait for calibration process */
    107	rt711_index_read(regmap, RT711_VENDOR_CALI,
    108		RT711_DAC_DC_CALI_CTL1, &val);
    109
    110	while (val & RT711_DAC_DC_CALI_TRIGGER) {
    111		if (loop >= 500) {
    112			pr_err("%s, calibration time-out!\n",
    113							__func__);
    114			ret = -ETIMEDOUT;
    115			break;
    116		}
    117		loop++;
    118
    119		usleep_range(10000, 11000);
    120		rt711_index_read(regmap, RT711_VENDOR_CALI,
    121			RT711_DAC_DC_CALI_CTL1, &val);
    122	}
    123
    124	/* depop mode */
    125	rt711_index_update_bits(regmap, RT711_VENDOR_REG,
    126		RT711_FSM_CTL, 0xf, RT711_DEPOP_CTL);
    127
    128	regmap_write(rt711->regmap,
    129		RT711_SET_AUDIO_POWER_STATE, AC_PWRST_D3);
    130	mutex_unlock(&rt711->calibrate_mutex);
    131
    132	dev_dbg(dev, "%s calibration complete, ret=%d\n", __func__, ret);
    133	return ret;
    134}
    135
    136static unsigned int rt711_button_detect(struct rt711_priv *rt711)
    137{
    138	unsigned int btn_type = 0, val80, val81;
    139	int ret;
    140
    141	ret = rt711_index_read(rt711->regmap, RT711_VENDOR_REG,
    142				RT711_IRQ_FLAG_TABLE1, &val80);
    143	if (ret < 0)
    144		goto read_error;
    145	ret = rt711_index_read(rt711->regmap, RT711_VENDOR_REG,
    146					RT711_IRQ_FLAG_TABLE2, &val81);
    147	if (ret < 0)
    148		goto read_error;
    149
    150	val80 &= 0x0381;
    151	val81 &= 0xff00;
    152
    153	switch (val80) {
    154	case 0x0200:
    155	case 0x0100:
    156	case 0x0080:
    157		btn_type |= SND_JACK_BTN_0;
    158		break;
    159	case 0x0001:
    160		btn_type |= SND_JACK_BTN_3;
    161		break;
    162	}
    163	switch (val81) {
    164	case 0x8000:
    165	case 0x4000:
    166	case 0x2000:
    167		btn_type |= SND_JACK_BTN_1;
    168		break;
    169	case 0x1000:
    170	case 0x0800:
    171	case 0x0400:
    172		btn_type |= SND_JACK_BTN_2;
    173		break;
    174	case 0x0200:
    175	case 0x0100:
    176		btn_type |= SND_JACK_BTN_3;
    177		break;
    178	}
    179read_error:
    180	return btn_type;
    181}
    182
    183static int rt711_headset_detect(struct rt711_priv *rt711)
    184{
    185	unsigned int buf, loop = 0;
    186	int ret;
    187	unsigned int jack_status = 0, reg;
    188
    189	ret = rt711_index_read(rt711->regmap, RT711_VENDOR_REG,
    190				RT711_COMBO_JACK_AUTO_CTL2, &buf);
    191	if (ret < 0)
    192		goto io_error;
    193
    194	while (loop < 500 &&
    195		(buf & RT711_COMBOJACK_AUTO_DET_STATUS) == 0) {
    196		loop++;
    197
    198		usleep_range(9000, 10000);
    199		ret = rt711_index_read(rt711->regmap, RT711_VENDOR_REG,
    200					RT711_COMBO_JACK_AUTO_CTL2, &buf);
    201		if (ret < 0)
    202			goto io_error;
    203
    204		reg = RT711_VERB_GET_PIN_SENSE | RT711_HP_OUT;
    205		ret = regmap_read(rt711->regmap, reg, &jack_status);
    206		if (ret < 0)
    207			goto io_error;
    208		if ((jack_status & (1 << 31)) == 0)
    209			goto remove_error;
    210	}
    211
    212	if (loop >= 500)
    213		goto to_error;
    214
    215	if (buf & RT711_COMBOJACK_AUTO_DET_TRS)
    216		rt711->jack_type = SND_JACK_HEADPHONE;
    217	else if ((buf & RT711_COMBOJACK_AUTO_DET_CTIA) ||
    218		(buf & RT711_COMBOJACK_AUTO_DET_OMTP))
    219		rt711->jack_type = SND_JACK_HEADSET;
    220
    221	return 0;
    222
    223to_error:
    224	ret = -ETIMEDOUT;
    225	pr_err_ratelimited("Time-out error in %s\n", __func__);
    226	return ret;
    227io_error:
    228	pr_err_ratelimited("IO error in %s, ret %d\n", __func__, ret);
    229	return ret;
    230remove_error:
    231	pr_err_ratelimited("Jack removal in %s\n", __func__);
    232	return -ENODEV;
    233}
    234
    235static void rt711_jack_detect_handler(struct work_struct *work)
    236{
    237	struct rt711_priv *rt711 =
    238		container_of(work, struct rt711_priv, jack_detect_work.work);
    239	int btn_type = 0, ret;
    240	unsigned int jack_status = 0, reg;
    241
    242	if (!rt711->hs_jack)
    243		return;
    244
    245	if (!rt711->component->card || !rt711->component->card->instantiated)
    246		return;
    247
    248	if (pm_runtime_status_suspended(rt711->slave->dev.parent)) {
    249		dev_dbg(&rt711->slave->dev,
    250			"%s: parent device is pm_runtime_status_suspended, skipping jack detection\n",
    251			__func__);
    252		return;
    253	}
    254
    255	reg = RT711_VERB_GET_PIN_SENSE | RT711_HP_OUT;
    256	ret = regmap_read(rt711->regmap, reg, &jack_status);
    257	if (ret < 0)
    258		goto io_error;
    259
    260	/* pin attached */
    261	if (jack_status & (1 << 31)) {
    262		/* jack in */
    263		if (rt711->jack_type == 0) {
    264			ret = rt711_headset_detect(rt711);
    265			if (ret < 0)
    266				return;
    267			if (rt711->jack_type == SND_JACK_HEADSET)
    268				btn_type = rt711_button_detect(rt711);
    269		} else if (rt711->jack_type == SND_JACK_HEADSET) {
    270			/* jack is already in, report button event */
    271			btn_type = rt711_button_detect(rt711);
    272		}
    273	} else {
    274		/* jack out */
    275		rt711->jack_type = 0;
    276	}
    277
    278	dev_dbg(&rt711->slave->dev,
    279		"in %s, jack_type=0x%x\n", __func__, rt711->jack_type);
    280	dev_dbg(&rt711->slave->dev,
    281		"in %s, btn_type=0x%x\n", __func__, btn_type);
    282
    283	snd_soc_jack_report(rt711->hs_jack, rt711->jack_type | btn_type,
    284			SND_JACK_HEADSET |
    285			SND_JACK_BTN_0 | SND_JACK_BTN_1 |
    286			SND_JACK_BTN_2 | SND_JACK_BTN_3);
    287
    288	if (btn_type) {
    289		/* button released */
    290		snd_soc_jack_report(rt711->hs_jack, rt711->jack_type,
    291			SND_JACK_HEADSET |
    292			SND_JACK_BTN_0 | SND_JACK_BTN_1 |
    293			SND_JACK_BTN_2 | SND_JACK_BTN_3);
    294
    295		mod_delayed_work(system_power_efficient_wq,
    296			&rt711->jack_btn_check_work, msecs_to_jiffies(200));
    297	}
    298
    299	return;
    300
    301io_error:
    302	pr_err_ratelimited("IO error in %s, ret %d\n", __func__, ret);
    303}
    304
    305static void rt711_btn_check_handler(struct work_struct *work)
    306{
    307	struct rt711_priv *rt711 = container_of(work, struct rt711_priv,
    308		jack_btn_check_work.work);
    309	int btn_type = 0, ret;
    310	unsigned int jack_status = 0, reg;
    311
    312	reg = RT711_VERB_GET_PIN_SENSE | RT711_HP_OUT;
    313	ret = regmap_read(rt711->regmap, reg, &jack_status);
    314	if (ret < 0)
    315		goto io_error;
    316
    317	/* pin attached */
    318	if (jack_status & (1 << 31)) {
    319		if (rt711->jack_type == SND_JACK_HEADSET) {
    320			/* jack is already in, report button event */
    321			btn_type = rt711_button_detect(rt711);
    322		}
    323	} else {
    324		rt711->jack_type = 0;
    325	}
    326
    327	/* cbj comparator */
    328	ret = rt711_index_read(rt711->regmap, RT711_VENDOR_REG,
    329		RT711_COMBO_JACK_AUTO_CTL2, &reg);
    330	if (ret < 0)
    331		goto io_error;
    332
    333	if ((reg & 0xf0) == 0xf0)
    334		btn_type = 0;
    335
    336	dev_dbg(&rt711->slave->dev,
    337		"%s, btn_type=0x%x\n",	__func__, btn_type);
    338	snd_soc_jack_report(rt711->hs_jack, rt711->jack_type | btn_type,
    339			SND_JACK_HEADSET |
    340			SND_JACK_BTN_0 | SND_JACK_BTN_1 |
    341			SND_JACK_BTN_2 | SND_JACK_BTN_3);
    342
    343	if (btn_type) {
    344		/* button released */
    345		snd_soc_jack_report(rt711->hs_jack, rt711->jack_type,
    346			SND_JACK_HEADSET |
    347			SND_JACK_BTN_0 | SND_JACK_BTN_1 |
    348			SND_JACK_BTN_2 | SND_JACK_BTN_3);
    349
    350		mod_delayed_work(system_power_efficient_wq,
    351			&rt711->jack_btn_check_work, msecs_to_jiffies(200));
    352	}
    353
    354	return;
    355
    356io_error:
    357	pr_err_ratelimited("IO error in %s, ret %d\n", __func__, ret);
    358}
    359
    360static void rt711_jack_init(struct rt711_priv *rt711)
    361{
    362	struct snd_soc_dapm_context *dapm =
    363		snd_soc_component_get_dapm(rt711->component);
    364
    365	mutex_lock(&rt711->calibrate_mutex);
    366	/* power on */
    367	if (dapm->bias_level <= SND_SOC_BIAS_STANDBY)
    368		regmap_write(rt711->regmap,
    369			RT711_SET_AUDIO_POWER_STATE, AC_PWRST_D0);
    370
    371	if (rt711->hs_jack) {
    372		/* unsolicited response & IRQ control */
    373		regmap_write(rt711->regmap,
    374			RT711_SET_MIC2_UNSOLICITED_ENABLE, 0x82);
    375		regmap_write(rt711->regmap,
    376			RT711_SET_HP_UNSOLICITED_ENABLE, 0x81);
    377		regmap_write(rt711->regmap,
    378			RT711_SET_INLINE_UNSOLICITED_ENABLE, 0x83);
    379		rt711_index_write(rt711->regmap, RT711_VENDOR_REG,
    380			0x10, 0x2420);
    381		rt711_index_write(rt711->regmap, RT711_VENDOR_REG,
    382			0x19, 0x2e11);
    383
    384		switch (rt711->jd_src) {
    385		case RT711_JD1:
    386			/* default settings was already for JD1 */
    387			break;
    388		case RT711_JD2:
    389			rt711_index_update_bits(rt711->regmap, RT711_VENDOR_REG,
    390				RT711_JD_CTL2, RT711_JD2_2PORT_200K_DECODE_HP |
    391				RT711_HP_JD_SEL_JD2,
    392				RT711_JD2_2PORT_200K_DECODE_HP |
    393				RT711_HP_JD_SEL_JD2);
    394			rt711_index_update_bits(rt711->regmap, RT711_VENDOR_REG,
    395				RT711_CC_DET1,
    396				RT711_HP_JD_FINAL_RESULT_CTL_JD12,
    397				RT711_HP_JD_FINAL_RESULT_CTL_JD12);
    398			break;
    399		case RT711_JD2_100K:
    400			rt711_index_update_bits(rt711->regmap, RT711_VENDOR_REG,
    401				RT711_JD_CTL2, RT711_JD2_2PORT_100K_DECODE | RT711_JD2_1PORT_TYPE_DECODE |
    402				RT711_HP_JD_SEL_JD2 | RT711_JD1_2PORT_TYPE_100K_DECODE,
    403				RT711_JD2_2PORT_100K_DECODE_HP | RT711_JD2_1PORT_JD_HP |
    404				RT711_HP_JD_SEL_JD2 | RT711_JD1_2PORT_JD_RESERVED);
    405			rt711_index_update_bits(rt711->regmap, RT711_VENDOR_REG,
    406				RT711_CC_DET1,
    407				RT711_HP_JD_FINAL_RESULT_CTL_JD12,
    408				RT711_HP_JD_FINAL_RESULT_CTL_JD12);
    409			break;
    410		case RT711_JD2_1P8V_1PORT:
    411			rt711_index_update_bits(rt711->regmap, RT711_VENDOR_REG,
    412				RT711_JD_CTL1, RT711_JD2_DIGITAL_JD_MODE_SEL,
    413				RT711_JD2_1_JD_MODE);
    414			rt711_index_update_bits(rt711->regmap, RT711_VENDOR_REG,
    415				RT711_JD_CTL2, RT711_JD2_1PORT_TYPE_DECODE |
    416				RT711_HP_JD_SEL_JD2,
    417				RT711_JD2_1PORT_JD_HP |
    418				RT711_HP_JD_SEL_JD2);
    419			rt711_index_update_bits(rt711->regmap, RT711_VENDOR_REG,
    420				RT711_JD_CTL4, RT711_JD2_PAD_PULL_UP_MASK |
    421				RT711_JD2_MODE_SEL_MASK,
    422				RT711_JD2_PAD_PULL_UP |
    423				RT711_JD2_MODE2_1P8V_1PORT);
    424			rt711_index_update_bits(rt711->regmap, RT711_VENDOR_REG,
    425				RT711_CC_DET1,
    426				RT711_HP_JD_FINAL_RESULT_CTL_JD12,
    427				RT711_HP_JD_FINAL_RESULT_CTL_JD12);
    428			break;
    429		default:
    430			dev_warn(rt711->component->dev, "Wrong JD source\n");
    431			break;
    432		}
    433
    434		dev_dbg(&rt711->slave->dev, "in %s enable\n", __func__);
    435
    436		mod_delayed_work(system_power_efficient_wq,
    437			&rt711->jack_detect_work, msecs_to_jiffies(250));
    438	} else {
    439		regmap_write(rt711->regmap,
    440			RT711_SET_MIC2_UNSOLICITED_ENABLE, 0x00);
    441		regmap_write(rt711->regmap,
    442			RT711_SET_HP_UNSOLICITED_ENABLE, 0x00);
    443		regmap_write(rt711->regmap,
    444			RT711_SET_INLINE_UNSOLICITED_ENABLE, 0x00);
    445
    446		dev_dbg(&rt711->slave->dev, "in %s disable\n", __func__);
    447	}
    448
    449	/* power off */
    450	if (dapm->bias_level <= SND_SOC_BIAS_STANDBY)
    451		regmap_write(rt711->regmap,
    452			RT711_SET_AUDIO_POWER_STATE, AC_PWRST_D3);
    453	mutex_unlock(&rt711->calibrate_mutex);
    454}
    455
    456static int rt711_set_jack_detect(struct snd_soc_component *component,
    457	struct snd_soc_jack *hs_jack, void *data)
    458{
    459	struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component);
    460	int ret;
    461
    462	rt711->hs_jack = hs_jack;
    463
    464	ret = pm_runtime_resume_and_get(component->dev);
    465	if (ret < 0) {
    466		if (ret != -EACCES) {
    467			dev_err(component->dev, "%s: failed to resume %d\n", __func__, ret);
    468			return ret;
    469		}
    470
    471		/* pm_runtime not enabled yet */
    472		dev_dbg(component->dev,	"%s: skipping jack init for now\n", __func__);
    473		return 0;
    474	}
    475
    476	rt711_jack_init(rt711);
    477
    478	pm_runtime_mark_last_busy(component->dev);
    479	pm_runtime_put_autosuspend(component->dev);
    480
    481	return 0;
    482}
    483
    484static void rt711_get_gain(struct rt711_priv *rt711, unsigned int addr_h,
    485				unsigned int addr_l, unsigned int val_h,
    486				unsigned int *r_val, unsigned int *l_val)
    487{
    488	/* R Channel */
    489	*r_val = (val_h << 8);
    490	regmap_read(rt711->regmap, addr_l, r_val);
    491
    492	/* L Channel */
    493	val_h |= 0x20;
    494	*l_val = (val_h << 8);
    495	regmap_read(rt711->regmap, addr_h, l_val);
    496}
    497
    498/* For Verb-Set Amplifier Gain (Verb ID = 3h) */
    499static int rt711_set_amp_gain_put(struct snd_kcontrol *kcontrol,
    500		struct snd_ctl_elem_value *ucontrol)
    501{
    502	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
    503	struct snd_soc_dapm_context *dapm =
    504		snd_soc_component_get_dapm(component);
    505	struct soc_mixer_control *mc =
    506		(struct soc_mixer_control *)kcontrol->private_value;
    507	struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component);
    508	unsigned int addr_h, addr_l, val_h, val_ll, val_lr;
    509	unsigned int read_ll, read_rl;
    510	int i;
    511
    512	mutex_lock(&rt711->calibrate_mutex);
    513
    514	/* Can't use update bit function, so read the original value first */
    515	addr_h = mc->reg;
    516	addr_l = mc->rreg;
    517	if (mc->shift == RT711_DIR_OUT_SFT) /* output */
    518		val_h = 0x80;
    519	else /* input */
    520		val_h = 0x0;
    521
    522	rt711_get_gain(rt711, addr_h, addr_l, val_h, &read_rl, &read_ll);
    523
    524	/* L Channel */
    525	if (mc->invert) {
    526		/* for mute/unmute */
    527		val_ll = (mc->max - ucontrol->value.integer.value[0])
    528					<< RT711_MUTE_SFT;
    529		/* keep gain */
    530		read_ll = read_ll & 0x7f;
    531		val_ll |= read_ll;
    532	} else {
    533		/* for gain */
    534		val_ll = ((ucontrol->value.integer.value[0]) & 0x7f);
    535		if (val_ll > mc->max)
    536			val_ll = mc->max;
    537		/* keep mute status */
    538		read_ll = read_ll & (1 << RT711_MUTE_SFT);
    539		val_ll |= read_ll;
    540	}
    541
    542	if (dapm->bias_level <= SND_SOC_BIAS_STANDBY)
    543		regmap_write(rt711->regmap,
    544				RT711_SET_AUDIO_POWER_STATE, AC_PWRST_D0);
    545
    546	/* R Channel */
    547	if (mc->invert) {
    548		/* for mute/unmute */
    549		val_lr = (mc->max - ucontrol->value.integer.value[1])
    550					<< RT711_MUTE_SFT;
    551		/* keep gain */
    552		read_rl = read_rl & 0x7f;
    553		val_lr |= read_rl;
    554	} else {
    555		/* for gain */
    556		val_lr = ((ucontrol->value.integer.value[1]) & 0x7f);
    557		if (val_lr > mc->max)
    558			val_lr = mc->max;
    559		/* keep mute status */
    560		read_rl = read_rl & (1 << RT711_MUTE_SFT);
    561		val_lr |= read_rl;
    562	}
    563
    564	for (i = 0; i < 3; i++) { /* retry 3 times at most */
    565
    566		if (val_ll == val_lr) {
    567			/* Set both L/R channels at the same time */
    568			val_h = (1 << mc->shift) | (3 << 4);
    569			regmap_write(rt711->regmap,
    570				addr_h, (val_h << 8 | val_ll));
    571			regmap_write(rt711->regmap,
    572				addr_l, (val_h << 8 | val_ll));
    573		} else {
    574			/* Lch*/
    575			val_h = (1 << mc->shift) | (1 << 5);
    576			regmap_write(rt711->regmap,
    577				addr_h, (val_h << 8 | val_ll));
    578
    579			/* Rch */
    580			val_h = (1 << mc->shift) | (1 << 4);
    581			regmap_write(rt711->regmap,
    582				addr_l, (val_h << 8 | val_lr));
    583		}
    584		/* check result */
    585		if (mc->shift == RT711_DIR_OUT_SFT) /* output */
    586			val_h = 0x80;
    587		else /* input */
    588			val_h = 0x0;
    589
    590		rt711_get_gain(rt711, addr_h, addr_l, val_h,
    591					&read_rl, &read_ll);
    592		if (read_rl == val_lr && read_ll == val_ll)
    593			break;
    594	}
    595
    596	if (dapm->bias_level <= SND_SOC_BIAS_STANDBY)
    597		regmap_write(rt711->regmap,
    598				RT711_SET_AUDIO_POWER_STATE, AC_PWRST_D3);
    599
    600	mutex_unlock(&rt711->calibrate_mutex);
    601	return 0;
    602}
    603
    604static int rt711_set_amp_gain_get(struct snd_kcontrol *kcontrol,
    605		struct snd_ctl_elem_value *ucontrol)
    606{
    607	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
    608	struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component);
    609	struct soc_mixer_control *mc =
    610		(struct soc_mixer_control *)kcontrol->private_value;
    611	unsigned int addr_h, addr_l, val_h;
    612	unsigned int read_ll, read_rl;
    613
    614	/* switch to get command */
    615	addr_h = mc->reg;
    616	addr_l = mc->rreg;
    617	if (mc->shift == RT711_DIR_OUT_SFT) /* output */
    618		val_h = 0x80;
    619	else /* input */
    620		val_h = 0x0;
    621
    622	rt711_get_gain(rt711, addr_h, addr_l, val_h, &read_rl, &read_ll);
    623
    624	if (mc->invert) {
    625		/* mute/unmute for switch controls */
    626		read_ll = !((read_ll & 0x80) >> RT711_MUTE_SFT);
    627		read_rl = !((read_rl & 0x80) >> RT711_MUTE_SFT);
    628	} else {
    629		/* for gain volume controls */
    630		read_ll = read_ll & 0x7f;
    631		read_rl = read_rl & 0x7f;
    632	}
    633	ucontrol->value.integer.value[0] = read_ll;
    634	ucontrol->value.integer.value[1] = read_rl;
    635
    636	return 0;
    637}
    638
    639static const DECLARE_TLV_DB_SCALE(out_vol_tlv, -6525, 75, 0);
    640static const DECLARE_TLV_DB_SCALE(in_vol_tlv, -1725, 75, 0);
    641static const DECLARE_TLV_DB_SCALE(mic_vol_tlv, 0, 1000, 0);
    642
    643static const struct snd_kcontrol_new rt711_snd_controls[] = {
    644	SOC_DOUBLE_R_EXT_TLV("DAC Surr Playback Volume",
    645		RT711_SET_GAIN_DAC2_H, RT711_SET_GAIN_DAC2_L,
    646		RT711_DIR_OUT_SFT, 0x57, 0,
    647		rt711_set_amp_gain_get, rt711_set_amp_gain_put, out_vol_tlv),
    648	SOC_DOUBLE_R_EXT("ADC 08 Capture Switch",
    649		RT711_SET_GAIN_ADC2_H, RT711_SET_GAIN_ADC2_L,
    650		RT711_DIR_IN_SFT, 1, 1,
    651		rt711_set_amp_gain_get, rt711_set_amp_gain_put),
    652	SOC_DOUBLE_R_EXT("ADC 09 Capture Switch",
    653		RT711_SET_GAIN_ADC1_H, RT711_SET_GAIN_ADC1_L,
    654		RT711_DIR_IN_SFT, 1, 1,
    655		rt711_set_amp_gain_get, rt711_set_amp_gain_put),
    656	SOC_DOUBLE_R_EXT_TLV("ADC 08 Capture Volume",
    657		RT711_SET_GAIN_ADC2_H, RT711_SET_GAIN_ADC2_L,
    658		RT711_DIR_IN_SFT, 0x3f, 0,
    659		rt711_set_amp_gain_get, rt711_set_amp_gain_put, in_vol_tlv),
    660	SOC_DOUBLE_R_EXT_TLV("ADC 09 Capture Volume",
    661		RT711_SET_GAIN_ADC1_H, RT711_SET_GAIN_ADC1_L,
    662		RT711_DIR_IN_SFT, 0x3f, 0,
    663		rt711_set_amp_gain_get, rt711_set_amp_gain_put, in_vol_tlv),
    664	SOC_DOUBLE_R_EXT_TLV("AMIC Volume",
    665		RT711_SET_GAIN_AMIC_H, RT711_SET_GAIN_AMIC_L,
    666		RT711_DIR_IN_SFT, 3, 0,
    667		rt711_set_amp_gain_get, rt711_set_amp_gain_put, mic_vol_tlv),
    668	SOC_DOUBLE_R_EXT_TLV("DMIC1 Volume",
    669		RT711_SET_GAIN_DMIC1_H, RT711_SET_GAIN_DMIC1_L,
    670		RT711_DIR_IN_SFT, 3, 0,
    671		rt711_set_amp_gain_get, rt711_set_amp_gain_put, mic_vol_tlv),
    672	SOC_DOUBLE_R_EXT_TLV("DMIC2 Volume",
    673		RT711_SET_GAIN_DMIC2_H, RT711_SET_GAIN_DMIC2_L,
    674		RT711_DIR_IN_SFT, 3, 0,
    675		rt711_set_amp_gain_get, rt711_set_amp_gain_put, mic_vol_tlv),
    676};
    677
    678static int rt711_mux_get(struct snd_kcontrol *kcontrol,
    679			struct snd_ctl_elem_value *ucontrol)
    680{
    681	struct snd_soc_component *component =
    682		snd_soc_dapm_kcontrol_component(kcontrol);
    683	struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component);
    684	unsigned int reg, val = 0, nid;
    685	int ret;
    686
    687	if (strstr(ucontrol->id.name, "ADC 22 Mux"))
    688		nid = RT711_MIXER_IN1;
    689	else if (strstr(ucontrol->id.name, "ADC 23 Mux"))
    690		nid = RT711_MIXER_IN2;
    691	else
    692		return -EINVAL;
    693
    694	/* vid = 0xf01 */
    695	reg = RT711_VERB_SET_CONNECT_SEL | nid;
    696	ret = regmap_read(rt711->regmap, reg, &val);
    697	if (ret < 0) {
    698		dev_err(component->dev, "%s: sdw read failed: %d\n",
    699			__func__, ret);
    700		return ret;
    701	}
    702
    703	ucontrol->value.enumerated.item[0] = val;
    704
    705	return 0;
    706}
    707
    708static int rt711_mux_put(struct snd_kcontrol *kcontrol,
    709			struct snd_ctl_elem_value *ucontrol)
    710{
    711	struct snd_soc_component *component =
    712		snd_soc_dapm_kcontrol_component(kcontrol);
    713	struct snd_soc_dapm_context *dapm =
    714		snd_soc_dapm_kcontrol_dapm(kcontrol);
    715	struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component);
    716	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
    717	unsigned int *item = ucontrol->value.enumerated.item;
    718	unsigned int val, val2 = 0, change, reg, nid;
    719	int ret;
    720
    721	if (item[0] >= e->items)
    722		return -EINVAL;
    723
    724	if (strstr(ucontrol->id.name, "ADC 22 Mux"))
    725		nid = RT711_MIXER_IN1;
    726	else if (strstr(ucontrol->id.name, "ADC 23 Mux"))
    727		nid = RT711_MIXER_IN2;
    728	else
    729		return -EINVAL;
    730
    731	/* Verb ID = 0x701h */
    732	val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
    733
    734	reg = RT711_VERB_SET_CONNECT_SEL | nid;
    735	ret = regmap_read(rt711->regmap, reg, &val2);
    736	if (ret < 0) {
    737		dev_err(component->dev, "%s: sdw read failed: %d\n",
    738			__func__, ret);
    739		return ret;
    740	}
    741
    742	if (val == val2)
    743		change = 0;
    744	else
    745		change = 1;
    746
    747	if (change) {
    748		reg = RT711_VERB_SET_CONNECT_SEL | nid;
    749		regmap_write(rt711->regmap, reg, val);
    750	}
    751
    752	snd_soc_dapm_mux_update_power(dapm, kcontrol,
    753						item[0], e, NULL);
    754
    755	return change;
    756}
    757
    758static const char * const adc_mux_text[] = {
    759	"MIC2",
    760	"LINE1",
    761	"LINE2",
    762	"DMIC",
    763};
    764
    765static SOC_ENUM_SINGLE_DECL(
    766	rt711_adc22_enum, SND_SOC_NOPM, 0, adc_mux_text);
    767
    768static SOC_ENUM_SINGLE_DECL(
    769	rt711_adc23_enum, SND_SOC_NOPM, 0, adc_mux_text);
    770
    771static const struct snd_kcontrol_new rt711_adc22_mux =
    772	SOC_DAPM_ENUM_EXT("ADC 22 Mux", rt711_adc22_enum,
    773			rt711_mux_get, rt711_mux_put);
    774
    775static const struct snd_kcontrol_new rt711_adc23_mux =
    776	SOC_DAPM_ENUM_EXT("ADC 23 Mux", rt711_adc23_enum,
    777			rt711_mux_get, rt711_mux_put);
    778
    779static int rt711_dac_surround_event(struct snd_soc_dapm_widget *w,
    780	struct snd_kcontrol *kcontrol, int event)
    781{
    782	struct snd_soc_component *component =
    783		snd_soc_dapm_to_component(w->dapm);
    784	struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component);
    785	unsigned int val_h = (1 << RT711_DIR_OUT_SFT) | (0x3 << 4);
    786	unsigned int val_l;
    787
    788	switch (event) {
    789	case SND_SOC_DAPM_POST_PMU:
    790		regmap_write(rt711->regmap,
    791			RT711_SET_STREAMID_DAC2, 0x10);
    792
    793		val_l = 0x00;
    794		regmap_write(rt711->regmap,
    795			RT711_SET_GAIN_HP_H, (val_h << 8 | val_l));
    796		break;
    797	case SND_SOC_DAPM_PRE_PMD:
    798		val_l = (1 << RT711_MUTE_SFT);
    799		regmap_write(rt711->regmap,
    800			RT711_SET_GAIN_HP_H, (val_h << 8 | val_l));
    801		usleep_range(50000, 55000);
    802
    803		regmap_write(rt711->regmap,
    804			RT711_SET_STREAMID_DAC2, 0x00);
    805		break;
    806	}
    807	return 0;
    808}
    809
    810static int rt711_adc_09_event(struct snd_soc_dapm_widget *w,
    811	struct snd_kcontrol *kcontrol, int event)
    812{
    813	struct snd_soc_component *component =
    814		snd_soc_dapm_to_component(w->dapm);
    815	struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component);
    816
    817	switch (event) {
    818	case SND_SOC_DAPM_POST_PMU:
    819		regmap_write(rt711->regmap,
    820			RT711_SET_STREAMID_ADC1, 0x10);
    821		break;
    822	case SND_SOC_DAPM_PRE_PMD:
    823		regmap_write(rt711->regmap,
    824			RT711_SET_STREAMID_ADC1, 0x00);
    825		break;
    826	}
    827	return 0;
    828}
    829
    830static int rt711_adc_08_event(struct snd_soc_dapm_widget *w,
    831	struct snd_kcontrol *kcontrol, int event)
    832{
    833	struct snd_soc_component *component =
    834		snd_soc_dapm_to_component(w->dapm);
    835	struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component);
    836
    837	switch (event) {
    838	case SND_SOC_DAPM_POST_PMU:
    839		regmap_write(rt711->regmap,
    840			RT711_SET_STREAMID_ADC2, 0x10);
    841		break;
    842	case SND_SOC_DAPM_PRE_PMD:
    843		regmap_write(rt711->regmap,
    844			RT711_SET_STREAMID_ADC2, 0x00);
    845		break;
    846	}
    847	return 0;
    848}
    849
    850static const struct snd_soc_dapm_widget rt711_dapm_widgets[] = {
    851	SND_SOC_DAPM_OUTPUT("HP"),
    852	SND_SOC_DAPM_INPUT("MIC2"),
    853	SND_SOC_DAPM_INPUT("DMIC1"),
    854	SND_SOC_DAPM_INPUT("DMIC2"),
    855	SND_SOC_DAPM_INPUT("LINE1"),
    856	SND_SOC_DAPM_INPUT("LINE2"),
    857
    858	SND_SOC_DAPM_DAC_E("DAC Surround", NULL, SND_SOC_NOPM, 0, 0,
    859		rt711_dac_surround_event,
    860		SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
    861	SND_SOC_DAPM_ADC_E("ADC 09", NULL, SND_SOC_NOPM, 0, 0,
    862		rt711_adc_09_event,
    863		SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
    864	SND_SOC_DAPM_ADC_E("ADC 08", NULL, SND_SOC_NOPM, 0, 0,
    865		rt711_adc_08_event,
    866		SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
    867	SND_SOC_DAPM_MUX("ADC 22 Mux", SND_SOC_NOPM, 0, 0,
    868		&rt711_adc22_mux),
    869	SND_SOC_DAPM_MUX("ADC 23 Mux", SND_SOC_NOPM, 0, 0,
    870		&rt711_adc23_mux),
    871
    872	SND_SOC_DAPM_AIF_IN("DP3RX", "DP3 Playback", 0, SND_SOC_NOPM, 0, 0),
    873	SND_SOC_DAPM_AIF_OUT("DP2TX", "DP2 Capture", 0, SND_SOC_NOPM, 0, 0),
    874	SND_SOC_DAPM_AIF_OUT("DP4TX", "DP4 Capture", 0, SND_SOC_NOPM, 0, 0),
    875};
    876
    877static const struct snd_soc_dapm_route rt711_audio_map[] = {
    878	{"DAC Surround", NULL, "DP3RX"},
    879	{"DP2TX", NULL, "ADC 09"},
    880	{"DP4TX", NULL, "ADC 08"},
    881
    882	{"ADC 09", NULL, "ADC 22 Mux"},
    883	{"ADC 08", NULL, "ADC 23 Mux"},
    884	{"ADC 22 Mux", "DMIC", "DMIC1"},
    885	{"ADC 22 Mux", "LINE1", "LINE1"},
    886	{"ADC 22 Mux", "LINE2", "LINE2"},
    887	{"ADC 22 Mux", "MIC2", "MIC2"},
    888	{"ADC 23 Mux", "DMIC", "DMIC2"},
    889	{"ADC 23 Mux", "LINE1", "LINE1"},
    890	{"ADC 23 Mux", "LINE2", "LINE2"},
    891	{"ADC 23 Mux", "MIC2", "MIC2"},
    892
    893	{"HP", NULL, "DAC Surround"},
    894};
    895
    896static int rt711_set_bias_level(struct snd_soc_component *component,
    897				enum snd_soc_bias_level level)
    898{
    899	struct snd_soc_dapm_context *dapm =
    900		snd_soc_component_get_dapm(component);
    901	struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component);
    902
    903	switch (level) {
    904	case SND_SOC_BIAS_PREPARE:
    905		if (dapm->bias_level == SND_SOC_BIAS_STANDBY) {
    906			regmap_write(rt711->regmap,
    907				RT711_SET_AUDIO_POWER_STATE,
    908				AC_PWRST_D0);
    909		}
    910		break;
    911
    912	case SND_SOC_BIAS_STANDBY:
    913		mutex_lock(&rt711->calibrate_mutex);
    914		regmap_write(rt711->regmap,
    915			RT711_SET_AUDIO_POWER_STATE,
    916			AC_PWRST_D3);
    917		mutex_unlock(&rt711->calibrate_mutex);
    918		break;
    919
    920	default:
    921		break;
    922	}
    923
    924	return 0;
    925}
    926
    927static int rt711_parse_dt(struct rt711_priv *rt711, struct device *dev)
    928{
    929	device_property_read_u32(dev, "realtek,jd-src",
    930		&rt711->jd_src);
    931
    932	return 0;
    933}
    934
    935static int rt711_probe(struct snd_soc_component *component)
    936{
    937	struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component);
    938
    939	rt711_parse_dt(rt711, &rt711->slave->dev);
    940	rt711->component = component;
    941
    942	return 0;
    943}
    944
    945static const struct snd_soc_component_driver soc_codec_dev_rt711 = {
    946	.probe = rt711_probe,
    947	.set_bias_level = rt711_set_bias_level,
    948	.controls = rt711_snd_controls,
    949	.num_controls = ARRAY_SIZE(rt711_snd_controls),
    950	.dapm_widgets = rt711_dapm_widgets,
    951	.num_dapm_widgets = ARRAY_SIZE(rt711_dapm_widgets),
    952	.dapm_routes = rt711_audio_map,
    953	.num_dapm_routes = ARRAY_SIZE(rt711_audio_map),
    954	.set_jack = rt711_set_jack_detect,
    955	.endianness = 1,
    956};
    957
    958static int rt711_set_sdw_stream(struct snd_soc_dai *dai, void *sdw_stream,
    959				int direction)
    960{
    961	struct sdw_stream_data *stream;
    962
    963	if (!sdw_stream)
    964		return 0;
    965
    966	stream = kzalloc(sizeof(*stream), GFP_KERNEL);
    967	if (!stream)
    968		return -ENOMEM;
    969
    970	stream->sdw_stream = sdw_stream;
    971
    972	/* Use tx_mask or rx_mask to configure stream tag and set dma_data */
    973	if (direction == SNDRV_PCM_STREAM_PLAYBACK)
    974		dai->playback_dma_data = stream;
    975	else
    976		dai->capture_dma_data = stream;
    977
    978	return 0;
    979}
    980
    981static void rt711_shutdown(struct snd_pcm_substream *substream,
    982				struct snd_soc_dai *dai)
    983{
    984	struct sdw_stream_data *stream;
    985
    986	stream = snd_soc_dai_get_dma_data(dai, substream);
    987	snd_soc_dai_set_dma_data(dai, substream, NULL);
    988	kfree(stream);
    989}
    990
    991static int rt711_pcm_hw_params(struct snd_pcm_substream *substream,
    992				struct snd_pcm_hw_params *params,
    993				struct snd_soc_dai *dai)
    994{
    995	struct snd_soc_component *component = dai->component;
    996	struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component);
    997	struct sdw_stream_config stream_config;
    998	struct sdw_port_config port_config;
    999	enum sdw_data_direction direction;
   1000	struct sdw_stream_data *stream;
   1001	int retval, port, num_channels;
   1002	unsigned int val = 0;
   1003
   1004	dev_dbg(dai->dev, "%s %s", __func__, dai->name);
   1005	stream = snd_soc_dai_get_dma_data(dai, substream);
   1006
   1007	if (!stream)
   1008		return -EINVAL;
   1009
   1010	if (!rt711->slave)
   1011		return -EINVAL;
   1012
   1013	/* SoundWire specific configuration */
   1014	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
   1015		direction = SDW_DATA_DIR_RX;
   1016		port = 3;
   1017	} else {
   1018		direction = SDW_DATA_DIR_TX;
   1019		if (dai->id == RT711_AIF1)
   1020			port = 4;
   1021		else if (dai->id == RT711_AIF2)
   1022			port = 2;
   1023		else
   1024			return -EINVAL;
   1025	}
   1026
   1027	stream_config.frame_rate = params_rate(params);
   1028	stream_config.ch_count = params_channels(params);
   1029	stream_config.bps = snd_pcm_format_width(params_format(params));
   1030	stream_config.direction = direction;
   1031
   1032	num_channels = params_channels(params);
   1033	port_config.ch_mask = (1 << (num_channels)) - 1;
   1034	port_config.num = port;
   1035
   1036	retval = sdw_stream_add_slave(rt711->slave, &stream_config,
   1037					&port_config, 1, stream->sdw_stream);
   1038	if (retval) {
   1039		dev_err(dai->dev, "Unable to configure port\n");
   1040		return retval;
   1041	}
   1042
   1043	if (params_channels(params) <= 16) {
   1044		/* bit 3:0 Number of Channel */
   1045		val |= (params_channels(params) - 1);
   1046	} else {
   1047		dev_err(component->dev, "Unsupported channels %d\n",
   1048			params_channels(params));
   1049		return -EINVAL;
   1050	}
   1051
   1052	switch (params_width(params)) {
   1053	/* bit 6:4 Bits per Sample */
   1054	case 8:
   1055		break;
   1056	case 16:
   1057		val |= (0x1 << 4);
   1058		break;
   1059	case 20:
   1060		val |= (0x2 << 4);
   1061		break;
   1062	case 24:
   1063		val |= (0x3 << 4);
   1064		break;
   1065	case 32:
   1066		val |= (0x4 << 4);
   1067		break;
   1068	default:
   1069		return -EINVAL;
   1070	}
   1071
   1072	/* 48Khz */
   1073	regmap_write(rt711->regmap, RT711_DAC_FORMAT_H, val);
   1074	regmap_write(rt711->regmap, RT711_ADC1_FORMAT_H, val);
   1075	regmap_write(rt711->regmap, RT711_ADC2_FORMAT_H, val);
   1076
   1077	return retval;
   1078}
   1079
   1080static int rt711_pcm_hw_free(struct snd_pcm_substream *substream,
   1081				struct snd_soc_dai *dai)
   1082{
   1083	struct snd_soc_component *component = dai->component;
   1084	struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component);
   1085	struct sdw_stream_data *stream =
   1086		snd_soc_dai_get_dma_data(dai, substream);
   1087
   1088	if (!rt711->slave)
   1089		return -EINVAL;
   1090
   1091	sdw_stream_remove_slave(rt711->slave, stream->sdw_stream);
   1092	return 0;
   1093}
   1094
   1095#define RT711_STEREO_RATES (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000)
   1096#define RT711_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
   1097			SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S8)
   1098
   1099static const struct snd_soc_dai_ops rt711_ops = {
   1100	.hw_params	= rt711_pcm_hw_params,
   1101	.hw_free	= rt711_pcm_hw_free,
   1102	.set_stream	= rt711_set_sdw_stream,
   1103	.shutdown	= rt711_shutdown,
   1104};
   1105
   1106static struct snd_soc_dai_driver rt711_dai[] = {
   1107	{
   1108		.name = "rt711-aif1",
   1109		.id = RT711_AIF1,
   1110		.playback = {
   1111			.stream_name = "DP3 Playback",
   1112			.channels_min = 1,
   1113			.channels_max = 2,
   1114			.rates = RT711_STEREO_RATES,
   1115			.formats = RT711_FORMATS,
   1116		},
   1117		.capture = {
   1118			.stream_name = "DP4 Capture",
   1119			.channels_min = 1,
   1120			.channels_max = 2,
   1121			.rates = RT711_STEREO_RATES,
   1122			.formats = RT711_FORMATS,
   1123		},
   1124		.ops = &rt711_ops,
   1125	},
   1126	{
   1127		.name = "rt711-aif2",
   1128		.id = RT711_AIF2,
   1129		.capture = {
   1130			.stream_name = "DP2 Capture",
   1131			.channels_min = 1,
   1132			.channels_max = 2,
   1133			.rates = RT711_STEREO_RATES,
   1134			.formats = RT711_FORMATS,
   1135		},
   1136		.ops = &rt711_ops,
   1137	}
   1138};
   1139
   1140/* Bus clock frequency */
   1141#define RT711_CLK_FREQ_9600000HZ 9600000
   1142#define RT711_CLK_FREQ_12000000HZ 12000000
   1143#define RT711_CLK_FREQ_6000000HZ 6000000
   1144#define RT711_CLK_FREQ_4800000HZ 4800000
   1145#define RT711_CLK_FREQ_2400000HZ 2400000
   1146#define RT711_CLK_FREQ_12288000HZ 12288000
   1147
   1148int rt711_clock_config(struct device *dev)
   1149{
   1150	struct rt711_priv *rt711 = dev_get_drvdata(dev);
   1151	unsigned int clk_freq, value;
   1152
   1153	clk_freq = (rt711->params.curr_dr_freq >> 1);
   1154
   1155	switch (clk_freq) {
   1156	case RT711_CLK_FREQ_12000000HZ:
   1157		value = 0x0;
   1158		break;
   1159	case RT711_CLK_FREQ_6000000HZ:
   1160		value = 0x1;
   1161		break;
   1162	case RT711_CLK_FREQ_9600000HZ:
   1163		value = 0x2;
   1164		break;
   1165	case RT711_CLK_FREQ_4800000HZ:
   1166		value = 0x3;
   1167		break;
   1168	case RT711_CLK_FREQ_2400000HZ:
   1169		value = 0x4;
   1170		break;
   1171	case RT711_CLK_FREQ_12288000HZ:
   1172		value = 0x5;
   1173		break;
   1174	default:
   1175		return -EINVAL;
   1176	}
   1177
   1178	regmap_write(rt711->regmap, 0xe0, value);
   1179	regmap_write(rt711->regmap, 0xf0, value);
   1180
   1181	dev_dbg(dev, "%s complete, clk_freq=%d\n", __func__, clk_freq);
   1182
   1183	return 0;
   1184}
   1185
   1186static void rt711_calibration_work(struct work_struct *work)
   1187{
   1188	struct rt711_priv *rt711 =
   1189		container_of(work, struct rt711_priv, calibration_work);
   1190
   1191	rt711_calibration(rt711);
   1192}
   1193
   1194int rt711_init(struct device *dev, struct regmap *sdw_regmap,
   1195			struct regmap *regmap, struct sdw_slave *slave)
   1196{
   1197	struct rt711_priv *rt711;
   1198	int ret;
   1199
   1200	rt711 = devm_kzalloc(dev, sizeof(*rt711), GFP_KERNEL);
   1201	if (!rt711)
   1202		return -ENOMEM;
   1203
   1204	dev_set_drvdata(dev, rt711);
   1205	rt711->slave = slave;
   1206	rt711->sdw_regmap = sdw_regmap;
   1207	rt711->regmap = regmap;
   1208
   1209	mutex_init(&rt711->calibrate_mutex);
   1210	mutex_init(&rt711->disable_irq_lock);
   1211
   1212	INIT_DELAYED_WORK(&rt711->jack_detect_work, rt711_jack_detect_handler);
   1213	INIT_DELAYED_WORK(&rt711->jack_btn_check_work, rt711_btn_check_handler);
   1214	INIT_WORK(&rt711->calibration_work, rt711_calibration_work);
   1215
   1216	/*
   1217	 * Mark hw_init to false
   1218	 * HW init will be performed when device reports present
   1219	 */
   1220	rt711->hw_init = false;
   1221	rt711->first_hw_init = false;
   1222
   1223	/* JD source uses JD2 in default */
   1224	rt711->jd_src = RT711_JD2;
   1225
   1226	ret =  devm_snd_soc_register_component(dev,
   1227				&soc_codec_dev_rt711,
   1228				rt711_dai,
   1229				ARRAY_SIZE(rt711_dai));
   1230
   1231	dev_dbg(&slave->dev, "%s\n", __func__);
   1232
   1233	return ret;
   1234}
   1235
   1236int rt711_io_init(struct device *dev, struct sdw_slave *slave)
   1237{
   1238	struct rt711_priv *rt711 = dev_get_drvdata(dev);
   1239
   1240	rt711->disable_irq = false;
   1241
   1242	if (rt711->hw_init)
   1243		return 0;
   1244
   1245	if (rt711->first_hw_init) {
   1246		regcache_cache_only(rt711->regmap, false);
   1247		regcache_cache_bypass(rt711->regmap, true);
   1248	}
   1249
   1250	/*
   1251	 * PM runtime is only enabled when a Slave reports as Attached
   1252	 */
   1253	if (!rt711->first_hw_init) {
   1254		/* set autosuspend parameters */
   1255		pm_runtime_set_autosuspend_delay(&slave->dev, 3000);
   1256		pm_runtime_use_autosuspend(&slave->dev);
   1257
   1258		/* update count of parent 'active' children */
   1259		pm_runtime_set_active(&slave->dev);
   1260
   1261		/* make sure the device does not suspend immediately */
   1262		pm_runtime_mark_last_busy(&slave->dev);
   1263
   1264		pm_runtime_enable(&slave->dev);
   1265	}
   1266
   1267	pm_runtime_get_noresume(&slave->dev);
   1268
   1269	rt711_reset(rt711->regmap);
   1270
   1271	/* power on */
   1272	regmap_write(rt711->regmap, RT711_SET_AUDIO_POWER_STATE, AC_PWRST_D0);
   1273
   1274	/* Set Pin Widget */
   1275	regmap_write(rt711->regmap, RT711_SET_PIN_MIC2, 0x25);
   1276	regmap_write(rt711->regmap, RT711_SET_PIN_HP, 0xc0);
   1277	regmap_write(rt711->regmap, RT711_SET_PIN_DMIC1, 0x20);
   1278	regmap_write(rt711->regmap, RT711_SET_PIN_DMIC2, 0x20);
   1279	regmap_write(rt711->regmap, RT711_SET_PIN_LINE1, 0x20);
   1280	regmap_write(rt711->regmap, RT711_SET_PIN_LINE2, 0x20);
   1281
   1282	/* Mute HP/ADC1/ADC2 */
   1283	regmap_write(rt711->regmap, RT711_SET_GAIN_HP_H, 0xa080);
   1284	regmap_write(rt711->regmap, RT711_SET_GAIN_HP_H, 0x9080);
   1285	regmap_write(rt711->regmap, RT711_SET_GAIN_ADC2_H, 0x6080);
   1286	regmap_write(rt711->regmap, RT711_SET_GAIN_ADC2_H, 0x5080);
   1287	regmap_write(rt711->regmap, RT711_SET_GAIN_ADC1_H, 0x6080);
   1288	regmap_write(rt711->regmap, RT711_SET_GAIN_ADC1_H, 0x5080);
   1289
   1290	/* Set Configuration Default */
   1291	regmap_write(rt711->regmap, 0x4f12, 0x91);
   1292	regmap_write(rt711->regmap, 0x4e12, 0xd6);
   1293	regmap_write(rt711->regmap, 0x4d12, 0x11);
   1294	regmap_write(rt711->regmap, 0x4c12, 0x20);
   1295	regmap_write(rt711->regmap, 0x4f13, 0x91);
   1296	regmap_write(rt711->regmap, 0x4e13, 0xd6);
   1297	regmap_write(rt711->regmap, 0x4d13, 0x11);
   1298	regmap_write(rt711->regmap, 0x4c13, 0x21);
   1299	regmap_write(rt711->regmap, 0x4c21, 0xf0);
   1300	regmap_write(rt711->regmap, 0x4d21, 0x11);
   1301	regmap_write(rt711->regmap, 0x4e21, 0x11);
   1302	regmap_write(rt711->regmap, 0x4f21, 0x01);
   1303
   1304	/* Data port arrangement */
   1305	rt711_index_write(rt711->regmap, RT711_VENDOR_REG,
   1306		RT711_TX_RX_MUX_CTL, 0x0154);
   1307
   1308	/* Set index */
   1309	rt711_index_write(rt711->regmap, RT711_VENDOR_REG,
   1310		RT711_DIGITAL_MISC_CTRL4, 0x201b);
   1311	rt711_index_write(rt711->regmap, RT711_VENDOR_REG,
   1312		RT711_COMBO_JACK_AUTO_CTL1, 0x5089);
   1313	rt711_index_write(rt711->regmap, RT711_VENDOR_REG,
   1314		RT711_VREFOUT_CTL, 0x5064);
   1315	rt711_index_write(rt711->regmap, RT711_VENDOR_REG,
   1316		RT711_INLINE_CMD_CTL, 0xd249);
   1317
   1318	/* Finish Initial Settings, set power to D3 */
   1319	regmap_write(rt711->regmap, RT711_SET_AUDIO_POWER_STATE, AC_PWRST_D3);
   1320
   1321	if (rt711->first_hw_init)
   1322		rt711_calibration(rt711);
   1323	else
   1324		schedule_work(&rt711->calibration_work);
   1325
   1326	/*
   1327	 * if set_jack callback occurred early than io_init,
   1328	 * we set up the jack detection function now
   1329	 */
   1330	if (rt711->hs_jack)
   1331		rt711_jack_init(rt711);
   1332
   1333	if (rt711->first_hw_init) {
   1334		regcache_cache_bypass(rt711->regmap, false);
   1335		regcache_mark_dirty(rt711->regmap);
   1336	} else
   1337		rt711->first_hw_init = true;
   1338
   1339	/* Mark Slave initialization complete */
   1340	rt711->hw_init = true;
   1341
   1342	pm_runtime_mark_last_busy(&slave->dev);
   1343	pm_runtime_put_autosuspend(&slave->dev);
   1344
   1345	dev_dbg(&slave->dev, "%s hw_init complete\n", __func__);
   1346	return 0;
   1347}
   1348
   1349MODULE_DESCRIPTION("ASoC RT711 SDW driver");
   1350MODULE_AUTHOR("Shuming Fan <shumingf@realtek.com>");
   1351MODULE_LICENSE("GPL");