xref: /openbmc/linux/sound/soc/codecs/rt286.c (revision 2612e3bbc0386368a850140a6c9b990cd496a5ec)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
207cf7cbaSBard Liao /*
307cf7cbaSBard Liao  * rt286.c  --  RT286 ALSA SoC audio codec driver
407cf7cbaSBard Liao  *
507cf7cbaSBard Liao  * Copyright 2013 Realtek Semiconductor Corp.
607cf7cbaSBard Liao  * Author: Bard Liao <bardliao@realtek.com>
707cf7cbaSBard Liao  */
807cf7cbaSBard Liao 
907cf7cbaSBard Liao #include <linux/module.h>
1007cf7cbaSBard Liao #include <linux/moduleparam.h>
1107cf7cbaSBard Liao #include <linux/init.h>
1207cf7cbaSBard Liao #include <linux/delay.h>
1307cf7cbaSBard Liao #include <linux/pm.h>
1407cf7cbaSBard Liao #include <linux/i2c.h>
1507cf7cbaSBard Liao #include <linux/platform_device.h>
1607cf7cbaSBard Liao #include <linux/spi/spi.h>
176c67cde2SBard Liao #include <linux/dmi.h>
1807cf7cbaSBard Liao #include <linux/acpi.h>
1907cf7cbaSBard Liao #include <sound/core.h>
2007cf7cbaSBard Liao #include <sound/pcm.h>
2107cf7cbaSBard Liao #include <sound/pcm_params.h>
2207cf7cbaSBard Liao #include <sound/soc.h>
2307cf7cbaSBard Liao #include <sound/soc-dapm.h>
2407cf7cbaSBard Liao #include <sound/initval.h>
2507cf7cbaSBard Liao #include <sound/tlv.h>
2607cf7cbaSBard Liao #include <sound/jack.h>
2707cf7cbaSBard Liao #include <linux/workqueue.h>
2807cf7cbaSBard Liao #include <sound/rt286.h>
2907cf7cbaSBard Liao 
30bc08f96bSOder Chiou #include "rl6347a.h"
3107cf7cbaSBard Liao #include "rt286.h"
3207cf7cbaSBard Liao 
3307cf7cbaSBard Liao #define RT286_VENDOR_ID 0x10ec0286
343ab888dbSBard Liao #define RT288_VENDOR_ID 0x10ec0288
3507cf7cbaSBard Liao 
3607cf7cbaSBard Liao struct rt286_priv {
37dc6d84c6SAxel Lin 	struct reg_default *index_cache;
38bc08f96bSOder Chiou 	int index_cache_size;
3907cf7cbaSBard Liao 	struct regmap *regmap;
4045101122SKuninori Morimoto 	struct snd_soc_component *component;
4107cf7cbaSBard Liao 	struct rt286_platform_data pdata;
4207cf7cbaSBard Liao 	struct i2c_client *i2c;
4307cf7cbaSBard Liao 	struct snd_soc_jack *jack;
4407cf7cbaSBard Liao 	struct delayed_work jack_detect_work;
4507cf7cbaSBard Liao 	int sys_clk;
466879db76SBard Liao 	int clk_id;
4707cf7cbaSBard Liao };
4807cf7cbaSBard Liao 
49c418a84aSAxel Lin static const struct reg_default rt286_index_def[] = {
5007cf7cbaSBard Liao 	{ 0x01, 0xaaaa },
5107cf7cbaSBard Liao 	{ 0x02, 0x8aaa },
5207cf7cbaSBard Liao 	{ 0x03, 0x0002 },
5307cf7cbaSBard Liao 	{ 0x04, 0xaf01 },
5407cf7cbaSBard Liao 	{ 0x08, 0x000d },
5507cf7cbaSBard Liao 	{ 0x09, 0xd810 },
56b7a29767SBard Liao 	{ 0x0a, 0x0120 },
5707cf7cbaSBard Liao 	{ 0x0b, 0x0000 },
58bc6c4e45SBard Liao 	{ 0x0d, 0x2800 },
5907cf7cbaSBard Liao 	{ 0x0f, 0x0000 },
6007cf7cbaSBard Liao 	{ 0x19, 0x0a17 },
6107cf7cbaSBard Liao 	{ 0x20, 0x0020 },
6207cf7cbaSBard Liao 	{ 0x33, 0x0208 },
6307cf7cbaSBard Liao 	{ 0x49, 0x0004 },
6407cf7cbaSBard Liao 	{ 0x4f, 0x50e9 },
65b7a29767SBard Liao 	{ 0x50, 0x2000 },
6607cf7cbaSBard Liao 	{ 0x63, 0x2902 },
67bc6c4e45SBard Liao 	{ 0x67, 0x1111 },
68bc6c4e45SBard Liao 	{ 0x68, 0x1016 },
69bc6c4e45SBard Liao 	{ 0x69, 0x273f },
7007cf7cbaSBard Liao };
7107cf7cbaSBard Liao #define INDEX_CACHE_SIZE ARRAY_SIZE(rt286_index_def)
7207cf7cbaSBard Liao 
7307cf7cbaSBard Liao static const struct reg_default rt286_reg[] = {
7407cf7cbaSBard Liao 	{ 0x00170500, 0x00000400 },
7507cf7cbaSBard Liao 	{ 0x00220000, 0x00000031 },
7607cf7cbaSBard Liao 	{ 0x00239000, 0x0000007f },
7707cf7cbaSBard Liao 	{ 0x0023a000, 0x0000007f },
7807cf7cbaSBard Liao 	{ 0x00270500, 0x00000400 },
7907cf7cbaSBard Liao 	{ 0x00370500, 0x00000400 },
8007cf7cbaSBard Liao 	{ 0x00870500, 0x00000400 },
8107cf7cbaSBard Liao 	{ 0x00920000, 0x00000031 },
8207cf7cbaSBard Liao 	{ 0x00935000, 0x000000c3 },
8307cf7cbaSBard Liao 	{ 0x00936000, 0x000000c3 },
8407cf7cbaSBard Liao 	{ 0x00970500, 0x00000400 },
8507cf7cbaSBard Liao 	{ 0x00b37000, 0x00000097 },
8607cf7cbaSBard Liao 	{ 0x00b37200, 0x00000097 },
8707cf7cbaSBard Liao 	{ 0x00b37300, 0x00000097 },
8807cf7cbaSBard Liao 	{ 0x00c37000, 0x00000000 },
8907cf7cbaSBard Liao 	{ 0x00c37100, 0x00000080 },
9007cf7cbaSBard Liao 	{ 0x01270500, 0x00000400 },
9107cf7cbaSBard Liao 	{ 0x01370500, 0x00000400 },
9207cf7cbaSBard Liao 	{ 0x01371f00, 0x411111f0 },
9307cf7cbaSBard Liao 	{ 0x01439000, 0x00000080 },
9407cf7cbaSBard Liao 	{ 0x0143a000, 0x00000080 },
9507cf7cbaSBard Liao 	{ 0x01470700, 0x00000000 },
9607cf7cbaSBard Liao 	{ 0x01470500, 0x00000400 },
9707cf7cbaSBard Liao 	{ 0x01470c00, 0x00000000 },
9807cf7cbaSBard Liao 	{ 0x01470100, 0x00000000 },
9907cf7cbaSBard Liao 	{ 0x01837000, 0x00000000 },
10007cf7cbaSBard Liao 	{ 0x01870500, 0x00000400 },
10107cf7cbaSBard Liao 	{ 0x02050000, 0x00000000 },
10207cf7cbaSBard Liao 	{ 0x02139000, 0x00000080 },
10307cf7cbaSBard Liao 	{ 0x0213a000, 0x00000080 },
10407cf7cbaSBard Liao 	{ 0x02170100, 0x00000000 },
10507cf7cbaSBard Liao 	{ 0x02170500, 0x00000400 },
10607cf7cbaSBard Liao 	{ 0x02170700, 0x00000000 },
10707cf7cbaSBard Liao 	{ 0x02270100, 0x00000000 },
10807cf7cbaSBard Liao 	{ 0x02370100, 0x00000000 },
10907cf7cbaSBard Liao 	{ 0x01870700, 0x00000020 },
11007cf7cbaSBard Liao 	{ 0x00830000, 0x000000c3 },
11107cf7cbaSBard Liao 	{ 0x00930000, 0x000000c3 },
11207cf7cbaSBard Liao 	{ 0x01270700, 0x00000000 },
11307cf7cbaSBard Liao };
11407cf7cbaSBard Liao 
rt286_volatile_register(struct device * dev,unsigned int reg)11507cf7cbaSBard Liao static bool rt286_volatile_register(struct device *dev, unsigned int reg)
11607cf7cbaSBard Liao {
11707cf7cbaSBard Liao 	switch (reg) {
11807cf7cbaSBard Liao 	case 0 ... 0xff:
11907cf7cbaSBard Liao 	case RT286_GET_PARAM(AC_NODE_ROOT, AC_PAR_VENDOR_ID):
12007cf7cbaSBard Liao 	case RT286_GET_HP_SENSE:
12107cf7cbaSBard Liao 	case RT286_GET_MIC1_SENSE:
12207cf7cbaSBard Liao 	case RT286_PROC_COEF:
12307cf7cbaSBard Liao 		return true;
12407cf7cbaSBard Liao 	default:
12507cf7cbaSBard Liao 		return false;
12607cf7cbaSBard Liao 	}
12707cf7cbaSBard Liao 
12807cf7cbaSBard Liao 
12907cf7cbaSBard Liao }
13007cf7cbaSBard Liao 
rt286_readable_register(struct device * dev,unsigned int reg)13107cf7cbaSBard Liao static bool rt286_readable_register(struct device *dev, unsigned int reg)
13207cf7cbaSBard Liao {
13307cf7cbaSBard Liao 	switch (reg) {
13407cf7cbaSBard Liao 	case 0 ... 0xff:
13507cf7cbaSBard Liao 	case RT286_GET_PARAM(AC_NODE_ROOT, AC_PAR_VENDOR_ID):
13607cf7cbaSBard Liao 	case RT286_GET_HP_SENSE:
13707cf7cbaSBard Liao 	case RT286_GET_MIC1_SENSE:
13807cf7cbaSBard Liao 	case RT286_SET_AUDIO_POWER:
13907cf7cbaSBard Liao 	case RT286_SET_HPO_POWER:
14007cf7cbaSBard Liao 	case RT286_SET_SPK_POWER:
14107cf7cbaSBard Liao 	case RT286_SET_DMIC1_POWER:
14207cf7cbaSBard Liao 	case RT286_SPK_MUX:
14307cf7cbaSBard Liao 	case RT286_HPO_MUX:
14407cf7cbaSBard Liao 	case RT286_ADC0_MUX:
14507cf7cbaSBard Liao 	case RT286_ADC1_MUX:
14607cf7cbaSBard Liao 	case RT286_SET_MIC1:
14707cf7cbaSBard Liao 	case RT286_SET_PIN_HPO:
14807cf7cbaSBard Liao 	case RT286_SET_PIN_SPK:
14907cf7cbaSBard Liao 	case RT286_SET_PIN_DMIC1:
15007cf7cbaSBard Liao 	case RT286_SPK_EAPD:
15107cf7cbaSBard Liao 	case RT286_SET_AMP_GAIN_HPO:
15207cf7cbaSBard Liao 	case RT286_SET_DMIC2_DEFAULT:
15307cf7cbaSBard Liao 	case RT286_DACL_GAIN:
15407cf7cbaSBard Liao 	case RT286_DACR_GAIN:
15507cf7cbaSBard Liao 	case RT286_ADCL_GAIN:
15607cf7cbaSBard Liao 	case RT286_ADCR_GAIN:
15707cf7cbaSBard Liao 	case RT286_MIC_GAIN:
15807cf7cbaSBard Liao 	case RT286_SPOL_GAIN:
15907cf7cbaSBard Liao 	case RT286_SPOR_GAIN:
16007cf7cbaSBard Liao 	case RT286_HPOL_GAIN:
16107cf7cbaSBard Liao 	case RT286_HPOR_GAIN:
16207cf7cbaSBard Liao 	case RT286_F_DAC_SWITCH:
16307cf7cbaSBard Liao 	case RT286_F_RECMIX_SWITCH:
16407cf7cbaSBard Liao 	case RT286_REC_MIC_SWITCH:
16507cf7cbaSBard Liao 	case RT286_REC_I2S_SWITCH:
16607cf7cbaSBard Liao 	case RT286_REC_LINE_SWITCH:
16707cf7cbaSBard Liao 	case RT286_REC_BEEP_SWITCH:
16807cf7cbaSBard Liao 	case RT286_DAC_FORMAT:
16907cf7cbaSBard Liao 	case RT286_ADC_FORMAT:
17007cf7cbaSBard Liao 	case RT286_COEF_INDEX:
17107cf7cbaSBard Liao 	case RT286_PROC_COEF:
17207cf7cbaSBard Liao 	case RT286_SET_AMP_GAIN_ADC_IN1:
17307cf7cbaSBard Liao 	case RT286_SET_AMP_GAIN_ADC_IN2:
174cd8499d5SDavid Ward 	case RT286_SET_GPIO_MASK:
175cd8499d5SDavid Ward 	case RT286_SET_GPIO_DIRECTION:
176cd8499d5SDavid Ward 	case RT286_SET_GPIO_DATA:
17707cf7cbaSBard Liao 	case RT286_SET_POWER(RT286_DAC_OUT1):
17807cf7cbaSBard Liao 	case RT286_SET_POWER(RT286_DAC_OUT2):
17907cf7cbaSBard Liao 	case RT286_SET_POWER(RT286_ADC_IN1):
18007cf7cbaSBard Liao 	case RT286_SET_POWER(RT286_ADC_IN2):
18107cf7cbaSBard Liao 	case RT286_SET_POWER(RT286_DMIC2):
18207cf7cbaSBard Liao 	case RT286_SET_POWER(RT286_MIC1):
18307cf7cbaSBard Liao 		return true;
18407cf7cbaSBard Liao 	default:
18507cf7cbaSBard Liao 		return false;
18607cf7cbaSBard Liao 	}
18707cf7cbaSBard Liao }
18807cf7cbaSBard Liao 
18981f3dfe1SThierry Reding #ifdef CONFIG_PM
rt286_index_sync(struct snd_soc_component * component)19045101122SKuninori Morimoto static void rt286_index_sync(struct snd_soc_component *component)
19107cf7cbaSBard Liao {
19245101122SKuninori Morimoto 	struct rt286_priv *rt286 = snd_soc_component_get_drvdata(component);
19307cf7cbaSBard Liao 	int i;
19407cf7cbaSBard Liao 
19507cf7cbaSBard Liao 	for (i = 0; i < INDEX_CACHE_SIZE; i++) {
19645101122SKuninori Morimoto 		snd_soc_component_write(component, rt286->index_cache[i].reg,
19707cf7cbaSBard Liao 				  rt286->index_cache[i].def);
19807cf7cbaSBard Liao 	}
19907cf7cbaSBard Liao }
20081f3dfe1SThierry Reding #endif
20107cf7cbaSBard Liao 
20207cf7cbaSBard Liao static int rt286_support_power_controls[] = {
20307cf7cbaSBard Liao 	RT286_DAC_OUT1,
20407cf7cbaSBard Liao 	RT286_DAC_OUT2,
20507cf7cbaSBard Liao 	RT286_ADC_IN1,
20607cf7cbaSBard Liao 	RT286_ADC_IN2,
20707cf7cbaSBard Liao 	RT286_MIC1,
20807cf7cbaSBard Liao 	RT286_DMIC1,
20907cf7cbaSBard Liao 	RT286_DMIC2,
21007cf7cbaSBard Liao 	RT286_SPK_OUT,
21107cf7cbaSBard Liao 	RT286_HP_OUT,
21207cf7cbaSBard Liao };
21307cf7cbaSBard Liao #define RT286_POWER_REG_LEN ARRAY_SIZE(rt286_support_power_controls)
21407cf7cbaSBard Liao 
rt286_jack_detect(struct rt286_priv * rt286,bool * hp,bool * mic)21590f601efSBard Liao static int rt286_jack_detect(struct rt286_priv *rt286, bool *hp, bool *mic)
21607cf7cbaSBard Liao {
217b1cd8457SLars-Peter Clausen 	struct snd_soc_dapm_context *dapm;
21807cf7cbaSBard Liao 	unsigned int val, buf;
21907cf7cbaSBard Liao 
22007cf7cbaSBard Liao 	*hp = false;
22107cf7cbaSBard Liao 	*mic = false;
22207cf7cbaSBard Liao 
22345101122SKuninori Morimoto 	if (!rt286->component)
22428d1ad09SBard Liao 		return -EINVAL;
225b1cd8457SLars-Peter Clausen 
22645101122SKuninori Morimoto 	dapm = snd_soc_component_get_dapm(rt286->component);
227b1cd8457SLars-Peter Clausen 
22807cf7cbaSBard Liao 	if (rt286->pdata.cbj_en) {
22990f601efSBard Liao 		regmap_read(rt286->regmap, RT286_GET_HP_SENSE, &buf);
23007cf7cbaSBard Liao 		*hp = buf & 0x80000000;
23107cf7cbaSBard Liao 		if (*hp) {
23207cf7cbaSBard Liao 			/* power on HV,VERF */
23390f601efSBard Liao 			regmap_update_bits(rt286->regmap,
2346879db76SBard Liao 				RT286_DC_GAIN, 0x200, 0x200);
2356879db76SBard Liao 
236b1cd8457SLars-Peter Clausen 			snd_soc_dapm_force_enable_pin(dapm, "HV");
237b1cd8457SLars-Peter Clausen 			snd_soc_dapm_force_enable_pin(dapm, "VREF");
23807cf7cbaSBard Liao 			/* power LDO1 */
239b1cd8457SLars-Peter Clausen 			snd_soc_dapm_force_enable_pin(dapm, "LDO1");
240b1cd8457SLars-Peter Clausen 			snd_soc_dapm_sync(dapm);
2416879db76SBard Liao 
24290f601efSBard Liao 			regmap_write(rt286->regmap, RT286_SET_MIC1, 0x24);
2436879db76SBard Liao 			msleep(50);
2446879db76SBard Liao 
2456879db76SBard Liao 			regmap_update_bits(rt286->regmap,
2466879db76SBard Liao 				RT286_CBJ_CTRL1, 0xfcc0, 0xd400);
2476879db76SBard Liao 			msleep(300);
24890f601efSBard Liao 			regmap_read(rt286->regmap, RT286_CBJ_CTRL2, &val);
24907cf7cbaSBard Liao 
2506879db76SBard Liao 			if (0x0070 == (val & 0x0070)) {
2516879db76SBard Liao 				*mic = true;
2526879db76SBard Liao 			} else {
2536879db76SBard Liao 				regmap_update_bits(rt286->regmap,
2546879db76SBard Liao 					RT286_CBJ_CTRL1, 0xfcc0, 0xe400);
2556879db76SBard Liao 				msleep(300);
25690f601efSBard Liao 				regmap_read(rt286->regmap,
25790f601efSBard Liao 					RT286_CBJ_CTRL2, &val);
258b15c3ea7SDavid Ward 				if (0x0070 == (val & 0x0070)) {
25907cf7cbaSBard Liao 					*mic = true;
260b15c3ea7SDavid Ward 				} else {
26107cf7cbaSBard Liao 					*mic = false;
262b15c3ea7SDavid Ward 					regmap_update_bits(rt286->regmap,
263b15c3ea7SDavid Ward 						RT286_CBJ_CTRL1,
264b15c3ea7SDavid Ward 						0xfcc0, 0xc400);
26507cf7cbaSBard Liao 				}
266b15c3ea7SDavid Ward 			}
267b15c3ea7SDavid Ward 
26890f601efSBard Liao 			regmap_update_bits(rt286->regmap,
2696879db76SBard Liao 				RT286_DC_GAIN, 0x200, 0x0);
2706879db76SBard Liao 
27107cf7cbaSBard Liao 		} else {
27207cf7cbaSBard Liao 			*mic = false;
2736879db76SBard Liao 			regmap_write(rt286->regmap, RT286_SET_MIC1, 0x20);
2746d514c72SBard Liao 			regmap_update_bits(rt286->regmap,
2756d514c72SBard Liao 				RT286_CBJ_CTRL1, 0x0400, 0x0000);
27607cf7cbaSBard Liao 		}
27707cf7cbaSBard Liao 	} else {
27890f601efSBard Liao 		regmap_read(rt286->regmap, RT286_GET_HP_SENSE, &buf);
27907cf7cbaSBard Liao 		*hp = buf & 0x80000000;
28090f601efSBard Liao 		regmap_read(rt286->regmap, RT286_GET_MIC1_SENSE, &buf);
28107cf7cbaSBard Liao 		*mic = buf & 0x80000000;
28207cf7cbaSBard Liao 	}
28312eb3ad0SShuming Fan 
28412eb3ad0SShuming Fan 	if (!*hp) {
285b1cd8457SLars-Peter Clausen 		snd_soc_dapm_disable_pin(dapm, "HV");
286b1cd8457SLars-Peter Clausen 		snd_soc_dapm_disable_pin(dapm, "VREF");
287b1cd8457SLars-Peter Clausen 		snd_soc_dapm_disable_pin(dapm, "LDO1");
288b1cd8457SLars-Peter Clausen 		snd_soc_dapm_sync(dapm);
28912eb3ad0SShuming Fan 	}
2906879db76SBard Liao 
29107cf7cbaSBard Liao 	return 0;
29207cf7cbaSBard Liao }
29307cf7cbaSBard Liao 
rt286_jack_detect_work(struct work_struct * work)29407cf7cbaSBard Liao static void rt286_jack_detect_work(struct work_struct *work)
29507cf7cbaSBard Liao {
29607cf7cbaSBard Liao 	struct rt286_priv *rt286 =
29707cf7cbaSBard Liao 		container_of(work, struct rt286_priv, jack_detect_work.work);
29807cf7cbaSBard Liao 	int status = 0;
29907cf7cbaSBard Liao 	bool hp = false;
30007cf7cbaSBard Liao 	bool mic = false;
30107cf7cbaSBard Liao 
30290f601efSBard Liao 	rt286_jack_detect(rt286, &hp, &mic);
30307cf7cbaSBard Liao 
304af3b2b54SPierre-Louis Bossart 	if (hp)
30507cf7cbaSBard Liao 		status |= SND_JACK_HEADPHONE;
30607cf7cbaSBard Liao 
307af3b2b54SPierre-Louis Bossart 	if (mic)
30807cf7cbaSBard Liao 		status |= SND_JACK_MICROPHONE;
30907cf7cbaSBard Liao 
31007cf7cbaSBard Liao 	snd_soc_jack_report(rt286->jack, status,
31107cf7cbaSBard Liao 		SND_JACK_MICROPHONE | SND_JACK_HEADPHONE);
31207cf7cbaSBard Liao }
31307cf7cbaSBard Liao 
rt286_mic_detect(struct snd_soc_component * component,struct snd_soc_jack * jack,void * data)3143082afe0SCezary Rojewski static int rt286_mic_detect(struct snd_soc_component *component,
3153082afe0SCezary Rojewski 			    struct snd_soc_jack *jack, void *data)
31607cf7cbaSBard Liao {
31745101122SKuninori Morimoto 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
31845101122SKuninori Morimoto 	struct rt286_priv *rt286 = snd_soc_component_get_drvdata(component);
31907cf7cbaSBard Liao 
32007cf7cbaSBard Liao 	rt286->jack = jack;
32107cf7cbaSBard Liao 
322e2cef68dSJie Yang 	if (jack) {
323e2cef68dSJie Yang 		/* enable IRQ */
3245af76d5cSJie Yang 		if (rt286->jack->status & SND_JACK_HEADPHONE)
325b1cd8457SLars-Peter Clausen 			snd_soc_dapm_force_enable_pin(dapm, "LDO1");
326e2cef68dSJie Yang 		regmap_update_bits(rt286->regmap, RT286_IRQ_CTRL, 0x2, 0x2);
32707cf7cbaSBard Liao 		/* Send an initial empty report */
328e2cef68dSJie Yang 		snd_soc_jack_report(rt286->jack, rt286->jack->status,
32907cf7cbaSBard Liao 			SND_JACK_MICROPHONE | SND_JACK_HEADPHONE);
330e2cef68dSJie Yang 	} else {
331e2cef68dSJie Yang 		/* disable IRQ */
332e2cef68dSJie Yang 		regmap_update_bits(rt286->regmap, RT286_IRQ_CTRL, 0x2, 0x0);
333b1cd8457SLars-Peter Clausen 		snd_soc_dapm_disable_pin(dapm, "LDO1");
334e2cef68dSJie Yang 	}
335b1cd8457SLars-Peter Clausen 	snd_soc_dapm_sync(dapm);
33607cf7cbaSBard Liao 
33707cf7cbaSBard Liao 	return 0;
33807cf7cbaSBard Liao }
33907cf7cbaSBard Liao 
is_mclk_mode(struct snd_soc_dapm_widget * source,struct snd_soc_dapm_widget * sink)3406879db76SBard Liao static int is_mclk_mode(struct snd_soc_dapm_widget *source,
3416879db76SBard Liao 			 struct snd_soc_dapm_widget *sink)
3426879db76SBard Liao {
34345101122SKuninori Morimoto 	struct snd_soc_component *component = snd_soc_dapm_to_component(source->dapm);
34445101122SKuninori Morimoto 	struct rt286_priv *rt286 = snd_soc_component_get_drvdata(component);
3456879db76SBard Liao 
3466879db76SBard Liao 	if (rt286->clk_id == RT286_SCLK_S_MCLK)
3476879db76SBard Liao 		return 1;
3486879db76SBard Liao 	else
3496879db76SBard Liao 		return 0;
3506879db76SBard Liao }
3516879db76SBard Liao 
35207cf7cbaSBard Liao static const DECLARE_TLV_DB_SCALE(out_vol_tlv, -6350, 50, 0);
35307cf7cbaSBard Liao static const DECLARE_TLV_DB_SCALE(mic_vol_tlv, 0, 1000, 0);
35407cf7cbaSBard Liao 
35507cf7cbaSBard Liao static const struct snd_kcontrol_new rt286_snd_controls[] = {
35607cf7cbaSBard Liao 	SOC_DOUBLE_R_TLV("DAC0 Playback Volume", RT286_DACL_GAIN,
35707cf7cbaSBard Liao 			    RT286_DACR_GAIN, 0, 0x7f, 0, out_vol_tlv),
35854d96a40SBard Liao 	SOC_DOUBLE_R("ADC0 Capture Switch", RT286_ADCL_GAIN,
35954d96a40SBard Liao 			    RT286_ADCR_GAIN, 7, 1, 1),
36007cf7cbaSBard Liao 	SOC_DOUBLE_R_TLV("ADC0 Capture Volume", RT286_ADCL_GAIN,
36107cf7cbaSBard Liao 			    RT286_ADCR_GAIN, 0, 0x7f, 0, out_vol_tlv),
36207cf7cbaSBard Liao 	SOC_SINGLE_TLV("AMIC Volume", RT286_MIC_GAIN,
36307cf7cbaSBard Liao 			    0, 0x3, 0, mic_vol_tlv),
36407cf7cbaSBard Liao 	SOC_DOUBLE_R("Speaker Playback Switch", RT286_SPOL_GAIN,
36507cf7cbaSBard Liao 			    RT286_SPOR_GAIN, RT286_MUTE_SFT, 1, 1),
36607cf7cbaSBard Liao };
36707cf7cbaSBard Liao 
36807cf7cbaSBard Liao /* Digital Mixer */
36907cf7cbaSBard Liao static const struct snd_kcontrol_new rt286_front_mix[] = {
37007cf7cbaSBard Liao 	SOC_DAPM_SINGLE("DAC Switch",  RT286_F_DAC_SWITCH,
37107cf7cbaSBard Liao 			RT286_MUTE_SFT, 1, 1),
37207cf7cbaSBard Liao 	SOC_DAPM_SINGLE("RECMIX Switch", RT286_F_RECMIX_SWITCH,
37307cf7cbaSBard Liao 			RT286_MUTE_SFT, 1, 1),
37407cf7cbaSBard Liao };
37507cf7cbaSBard Liao 
37607cf7cbaSBard Liao /* Analog Input Mixer */
37707cf7cbaSBard Liao static const struct snd_kcontrol_new rt286_rec_mix[] = {
37807cf7cbaSBard Liao 	SOC_DAPM_SINGLE("Mic1 Switch", RT286_REC_MIC_SWITCH,
37907cf7cbaSBard Liao 			RT286_MUTE_SFT, 1, 1),
38007cf7cbaSBard Liao 	SOC_DAPM_SINGLE("I2S Switch", RT286_REC_I2S_SWITCH,
38107cf7cbaSBard Liao 			RT286_MUTE_SFT, 1, 1),
38207cf7cbaSBard Liao 	SOC_DAPM_SINGLE("Line1 Switch", RT286_REC_LINE_SWITCH,
38307cf7cbaSBard Liao 			RT286_MUTE_SFT, 1, 1),
38407cf7cbaSBard Liao 	SOC_DAPM_SINGLE("Beep Switch", RT286_REC_BEEP_SWITCH,
38507cf7cbaSBard Liao 			RT286_MUTE_SFT, 1, 1),
38607cf7cbaSBard Liao };
38707cf7cbaSBard Liao 
38807cf7cbaSBard Liao static const struct snd_kcontrol_new spo_enable_control =
38907cf7cbaSBard Liao 	SOC_DAPM_SINGLE("Switch", RT286_SET_PIN_SPK,
39007cf7cbaSBard Liao 			RT286_SET_PIN_SFT, 1, 0);
39107cf7cbaSBard Liao 
39207cf7cbaSBard Liao static const struct snd_kcontrol_new hpol_enable_control =
39307cf7cbaSBard Liao 	SOC_DAPM_SINGLE_AUTODISABLE("Switch", RT286_HPOL_GAIN,
39407cf7cbaSBard Liao 			RT286_MUTE_SFT, 1, 1);
39507cf7cbaSBard Liao 
39607cf7cbaSBard Liao static const struct snd_kcontrol_new hpor_enable_control =
39707cf7cbaSBard Liao 	SOC_DAPM_SINGLE_AUTODISABLE("Switch", RT286_HPOR_GAIN,
39807cf7cbaSBard Liao 			RT286_MUTE_SFT, 1, 1);
39907cf7cbaSBard Liao 
40007cf7cbaSBard Liao /* ADC0 source */
40107cf7cbaSBard Liao static const char * const rt286_adc_src[] = {
40207cf7cbaSBard Liao 	"Mic", "RECMIX", "Dmic"
40307cf7cbaSBard Liao };
40407cf7cbaSBard Liao 
40507cf7cbaSBard Liao static const int rt286_adc_values[] = {
40607cf7cbaSBard Liao 	0, 4, 5,
40707cf7cbaSBard Liao };
40807cf7cbaSBard Liao 
40907cf7cbaSBard Liao static SOC_VALUE_ENUM_SINGLE_DECL(
41007cf7cbaSBard Liao 	rt286_adc0_enum, RT286_ADC0_MUX, RT286_ADC_SEL_SFT,
41107cf7cbaSBard Liao 	RT286_ADC_SEL_MASK, rt286_adc_src, rt286_adc_values);
41207cf7cbaSBard Liao 
41307cf7cbaSBard Liao static const struct snd_kcontrol_new rt286_adc0_mux =
41407cf7cbaSBard Liao 	SOC_DAPM_ENUM("ADC 0 source", rt286_adc0_enum);
41507cf7cbaSBard Liao 
41607cf7cbaSBard Liao static SOC_VALUE_ENUM_SINGLE_DECL(
41707cf7cbaSBard Liao 	rt286_adc1_enum, RT286_ADC1_MUX, RT286_ADC_SEL_SFT,
41807cf7cbaSBard Liao 	RT286_ADC_SEL_MASK, rt286_adc_src, rt286_adc_values);
41907cf7cbaSBard Liao 
42007cf7cbaSBard Liao static const struct snd_kcontrol_new rt286_adc1_mux =
42107cf7cbaSBard Liao 	SOC_DAPM_ENUM("ADC 1 source", rt286_adc1_enum);
42207cf7cbaSBard Liao 
42307cf7cbaSBard Liao static const char * const rt286_dac_src[] = {
42407cf7cbaSBard Liao 	"Front", "Surround"
42507cf7cbaSBard Liao };
42607cf7cbaSBard Liao /* HP-OUT source */
42707cf7cbaSBard Liao static SOC_ENUM_SINGLE_DECL(rt286_hpo_enum, RT286_HPO_MUX,
42807cf7cbaSBard Liao 				0, rt286_dac_src);
42907cf7cbaSBard Liao 
43007cf7cbaSBard Liao static const struct snd_kcontrol_new rt286_hpo_mux =
43107cf7cbaSBard Liao SOC_DAPM_ENUM("HPO source", rt286_hpo_enum);
43207cf7cbaSBard Liao 
43307cf7cbaSBard Liao /* SPK-OUT source */
43407cf7cbaSBard Liao static SOC_ENUM_SINGLE_DECL(rt286_spo_enum, RT286_SPK_MUX,
43507cf7cbaSBard Liao 				0, rt286_dac_src);
43607cf7cbaSBard Liao 
43707cf7cbaSBard Liao static const struct snd_kcontrol_new rt286_spo_mux =
43807cf7cbaSBard Liao SOC_DAPM_ENUM("SPO source", rt286_spo_enum);
43907cf7cbaSBard Liao 
rt286_spk_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kcontrol,int event)44007cf7cbaSBard Liao static int rt286_spk_event(struct snd_soc_dapm_widget *w,
44107cf7cbaSBard Liao 			    struct snd_kcontrol *kcontrol, int event)
44207cf7cbaSBard Liao {
44345101122SKuninori Morimoto 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
44407cf7cbaSBard Liao 
44507cf7cbaSBard Liao 	switch (event) {
44607cf7cbaSBard Liao 	case SND_SOC_DAPM_POST_PMU:
44745101122SKuninori Morimoto 		snd_soc_component_write(component,
44807cf7cbaSBard Liao 			RT286_SPK_EAPD, RT286_SET_EAPD_HIGH);
44907cf7cbaSBard Liao 		break;
45007cf7cbaSBard Liao 	case SND_SOC_DAPM_PRE_PMD:
45145101122SKuninori Morimoto 		snd_soc_component_write(component,
45207cf7cbaSBard Liao 			RT286_SPK_EAPD, RT286_SET_EAPD_LOW);
45307cf7cbaSBard Liao 		break;
45407cf7cbaSBard Liao 
45507cf7cbaSBard Liao 	default:
45607cf7cbaSBard Liao 		return 0;
45707cf7cbaSBard Liao 	}
45807cf7cbaSBard Liao 
45907cf7cbaSBard Liao 	return 0;
46007cf7cbaSBard Liao }
46107cf7cbaSBard Liao 
rt286_set_dmic1_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kcontrol,int event)46207cf7cbaSBard Liao static int rt286_set_dmic1_event(struct snd_soc_dapm_widget *w,
46307cf7cbaSBard Liao 				  struct snd_kcontrol *kcontrol, int event)
46407cf7cbaSBard Liao {
46545101122SKuninori Morimoto 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
46607cf7cbaSBard Liao 
46707cf7cbaSBard Liao 	switch (event) {
46807cf7cbaSBard Liao 	case SND_SOC_DAPM_POST_PMU:
46945101122SKuninori Morimoto 		snd_soc_component_write(component, RT286_SET_PIN_DMIC1, 0x20);
47007cf7cbaSBard Liao 		break;
47107cf7cbaSBard Liao 	case SND_SOC_DAPM_PRE_PMD:
47245101122SKuninori Morimoto 		snd_soc_component_write(component, RT286_SET_PIN_DMIC1, 0);
47307cf7cbaSBard Liao 		break;
47407cf7cbaSBard Liao 	default:
47507cf7cbaSBard Liao 		return 0;
47607cf7cbaSBard Liao 	}
47707cf7cbaSBard Liao 
47807cf7cbaSBard Liao 	return 0;
47907cf7cbaSBard Liao }
48007cf7cbaSBard Liao 
rt286_ldo2_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kcontrol,int event)4816879db76SBard Liao static int rt286_ldo2_event(struct snd_soc_dapm_widget *w,
4826879db76SBard Liao 			     struct snd_kcontrol *kcontrol, int event)
4836879db76SBard Liao {
48445101122SKuninori Morimoto 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
4856879db76SBard Liao 
4866879db76SBard Liao 	switch (event) {
4876879db76SBard Liao 	case SND_SOC_DAPM_POST_PMU:
48845101122SKuninori Morimoto 		snd_soc_component_update_bits(component, RT286_POWER_CTRL2, 0x38, 0x08);
4896879db76SBard Liao 		break;
4906879db76SBard Liao 	case SND_SOC_DAPM_PRE_PMD:
49145101122SKuninori Morimoto 		snd_soc_component_update_bits(component, RT286_POWER_CTRL2, 0x38, 0x30);
4926879db76SBard Liao 		break;
4936879db76SBard Liao 	default:
4946879db76SBard Liao 		return 0;
4956879db76SBard Liao 	}
4966879db76SBard Liao 
4976879db76SBard Liao 	return 0;
4986879db76SBard Liao }
4996879db76SBard Liao 
rt286_mic1_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kcontrol,int event)5006879db76SBard Liao static int rt286_mic1_event(struct snd_soc_dapm_widget *w,
5016879db76SBard Liao 			     struct snd_kcontrol *kcontrol, int event)
5026879db76SBard Liao {
50345101122SKuninori Morimoto 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
5046879db76SBard Liao 
5056879db76SBard Liao 	switch (event) {
5066879db76SBard Liao 	case SND_SOC_DAPM_PRE_PMU:
50745101122SKuninori Morimoto 		snd_soc_component_update_bits(component,
5086879db76SBard Liao 			RT286_A_BIAS_CTRL3, 0xc000, 0x8000);
50945101122SKuninori Morimoto 		snd_soc_component_update_bits(component,
5106879db76SBard Liao 			RT286_A_BIAS_CTRL2, 0xc000, 0x8000);
5116879db76SBard Liao 		break;
5126879db76SBard Liao 	case SND_SOC_DAPM_POST_PMD:
51345101122SKuninori Morimoto 		snd_soc_component_update_bits(component,
5146879db76SBard Liao 			RT286_A_BIAS_CTRL3, 0xc000, 0x0000);
51545101122SKuninori Morimoto 		snd_soc_component_update_bits(component,
5166879db76SBard Liao 			RT286_A_BIAS_CTRL2, 0xc000, 0x0000);
5176879db76SBard Liao 		break;
5186879db76SBard Liao 	default:
5196879db76SBard Liao 		return 0;
5206879db76SBard Liao 	}
5216879db76SBard Liao 
5226879db76SBard Liao 	return 0;
5236879db76SBard Liao }
5246879db76SBard Liao 
52507cf7cbaSBard Liao static const struct snd_soc_dapm_widget rt286_dapm_widgets[] = {
5266879db76SBard Liao 	SND_SOC_DAPM_SUPPLY_S("HV", 1, RT286_POWER_CTRL1,
5276879db76SBard Liao 		12, 1, NULL, 0),
5286879db76SBard Liao 	SND_SOC_DAPM_SUPPLY("VREF", RT286_POWER_CTRL1,
5296d514c72SBard Liao 		0, 1, NULL, 0),
5306879db76SBard Liao 	SND_SOC_DAPM_SUPPLY_S("LDO1", 1, RT286_POWER_CTRL2,
5316879db76SBard Liao 		2, 0, NULL, 0),
5326879db76SBard Liao 	SND_SOC_DAPM_SUPPLY_S("LDO2", 2, RT286_POWER_CTRL1,
5336879db76SBard Liao 		13, 1, rt286_ldo2_event, SND_SOC_DAPM_PRE_PMD |
5346879db76SBard Liao 		SND_SOC_DAPM_POST_PMU),
5356879db76SBard Liao 	SND_SOC_DAPM_SUPPLY("MCLK MODE", RT286_PLL_CTRL1,
5366879db76SBard Liao 		5, 0, NULL, 0),
5376879db76SBard Liao 	SND_SOC_DAPM_SUPPLY("MIC1 Input Buffer", SND_SOC_NOPM,
5386879db76SBard Liao 		0, 0, rt286_mic1_event, SND_SOC_DAPM_PRE_PMU |
5396879db76SBard Liao 		SND_SOC_DAPM_POST_PMD),
5406879db76SBard Liao 
54107cf7cbaSBard Liao 	/* Input Lines */
54207cf7cbaSBard Liao 	SND_SOC_DAPM_INPUT("DMIC1 Pin"),
54307cf7cbaSBard Liao 	SND_SOC_DAPM_INPUT("DMIC2 Pin"),
54407cf7cbaSBard Liao 	SND_SOC_DAPM_INPUT("MIC1"),
54507cf7cbaSBard Liao 	SND_SOC_DAPM_INPUT("LINE1"),
54607cf7cbaSBard Liao 	SND_SOC_DAPM_INPUT("Beep"),
54707cf7cbaSBard Liao 
54807cf7cbaSBard Liao 	/* DMIC */
54907cf7cbaSBard Liao 	SND_SOC_DAPM_PGA_E("DMIC1", RT286_SET_POWER(RT286_DMIC1), 0, 1,
55007cf7cbaSBard Liao 		NULL, 0, rt286_set_dmic1_event,
55107cf7cbaSBard Liao 		SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
55207cf7cbaSBard Liao 	SND_SOC_DAPM_PGA("DMIC2", RT286_SET_POWER(RT286_DMIC2), 0, 1,
55307cf7cbaSBard Liao 		NULL, 0),
55407cf7cbaSBard Liao 	SND_SOC_DAPM_SUPPLY("DMIC Receiver", SND_SOC_NOPM,
55507cf7cbaSBard Liao 		0, 0, NULL, 0),
55607cf7cbaSBard Liao 
55707cf7cbaSBard Liao 	/* REC Mixer */
55807cf7cbaSBard Liao 	SND_SOC_DAPM_MIXER("RECMIX", SND_SOC_NOPM, 0, 0,
55907cf7cbaSBard Liao 		rt286_rec_mix, ARRAY_SIZE(rt286_rec_mix)),
56007cf7cbaSBard Liao 
56107cf7cbaSBard Liao 	/* ADCs */
56207cf7cbaSBard Liao 	SND_SOC_DAPM_ADC("ADC 0", NULL, SND_SOC_NOPM, 0, 0),
56307cf7cbaSBard Liao 	SND_SOC_DAPM_ADC("ADC 1", NULL, SND_SOC_NOPM, 0, 0),
56407cf7cbaSBard Liao 
56507cf7cbaSBard Liao 	/* ADC Mux */
56654d96a40SBard Liao 	SND_SOC_DAPM_MUX("ADC 0 Mux", RT286_SET_POWER(RT286_ADC_IN1), 0, 1,
56754d96a40SBard Liao 		&rt286_adc0_mux),
56854d96a40SBard Liao 	SND_SOC_DAPM_MUX("ADC 1 Mux", RT286_SET_POWER(RT286_ADC_IN2), 0, 1,
56954d96a40SBard Liao 		&rt286_adc1_mux),
57007cf7cbaSBard Liao 
57107cf7cbaSBard Liao 	/* Audio Interface */
57207cf7cbaSBard Liao 	SND_SOC_DAPM_AIF_IN("AIF1RX", "AIF1 Playback", 0, SND_SOC_NOPM, 0, 0),
57307cf7cbaSBard Liao 	SND_SOC_DAPM_AIF_OUT("AIF1TX", "AIF1 Capture", 0, SND_SOC_NOPM, 0, 0),
57407cf7cbaSBard Liao 	SND_SOC_DAPM_AIF_IN("AIF2RX", "AIF2 Playback", 0, SND_SOC_NOPM, 0, 0),
57507cf7cbaSBard Liao 	SND_SOC_DAPM_AIF_OUT("AIF2TX", "AIF2 Capture", 0, SND_SOC_NOPM, 0, 0),
57607cf7cbaSBard Liao 
57707cf7cbaSBard Liao 	/* Output Side */
57807cf7cbaSBard Liao 	/* DACs */
57907cf7cbaSBard Liao 	SND_SOC_DAPM_DAC("DAC 0", NULL, SND_SOC_NOPM, 0, 0),
58007cf7cbaSBard Liao 	SND_SOC_DAPM_DAC("DAC 1", NULL, SND_SOC_NOPM, 0, 0),
58107cf7cbaSBard Liao 
58207cf7cbaSBard Liao 	/* Output Mux */
58307cf7cbaSBard Liao 	SND_SOC_DAPM_MUX("SPK Mux", SND_SOC_NOPM, 0, 0, &rt286_spo_mux),
58407cf7cbaSBard Liao 	SND_SOC_DAPM_MUX("HPO Mux", SND_SOC_NOPM, 0, 0, &rt286_hpo_mux),
58507cf7cbaSBard Liao 
58607cf7cbaSBard Liao 	SND_SOC_DAPM_SUPPLY("HP Power", RT286_SET_PIN_HPO,
58707cf7cbaSBard Liao 		RT286_SET_PIN_SFT, 0, NULL, 0),
58807cf7cbaSBard Liao 
58907cf7cbaSBard Liao 	/* Output Mixer */
59007cf7cbaSBard Liao 	SND_SOC_DAPM_MIXER("Front", RT286_SET_POWER(RT286_DAC_OUT1), 0, 1,
59107cf7cbaSBard Liao 			rt286_front_mix, ARRAY_SIZE(rt286_front_mix)),
59207cf7cbaSBard Liao 	SND_SOC_DAPM_PGA("Surround", RT286_SET_POWER(RT286_DAC_OUT2), 0, 1,
59307cf7cbaSBard Liao 			NULL, 0),
59407cf7cbaSBard Liao 
59507cf7cbaSBard Liao 	/* Output Pga */
59607cf7cbaSBard Liao 	SND_SOC_DAPM_SWITCH_E("SPO", SND_SOC_NOPM, 0, 0,
59707cf7cbaSBard Liao 		&spo_enable_control, rt286_spk_event,
59807cf7cbaSBard Liao 		SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
59907cf7cbaSBard Liao 	SND_SOC_DAPM_SWITCH("HPO L", SND_SOC_NOPM, 0, 0,
60007cf7cbaSBard Liao 		&hpol_enable_control),
60107cf7cbaSBard Liao 	SND_SOC_DAPM_SWITCH("HPO R", SND_SOC_NOPM, 0, 0,
60207cf7cbaSBard Liao 		&hpor_enable_control),
60307cf7cbaSBard Liao 
60407cf7cbaSBard Liao 	/* Output Lines */
60507cf7cbaSBard Liao 	SND_SOC_DAPM_OUTPUT("SPOL"),
60607cf7cbaSBard Liao 	SND_SOC_DAPM_OUTPUT("SPOR"),
60707cf7cbaSBard Liao 	SND_SOC_DAPM_OUTPUT("HPO Pin"),
60807cf7cbaSBard Liao 	SND_SOC_DAPM_OUTPUT("SPDIF"),
60907cf7cbaSBard Liao };
61007cf7cbaSBard Liao 
61107cf7cbaSBard Liao static const struct snd_soc_dapm_route rt286_dapm_routes[] = {
6126879db76SBard Liao 	{"ADC 0", NULL, "MCLK MODE", is_mclk_mode},
6136879db76SBard Liao 	{"ADC 1", NULL, "MCLK MODE", is_mclk_mode},
6146879db76SBard Liao 	{"Front", NULL, "MCLK MODE", is_mclk_mode},
6156879db76SBard Liao 	{"Surround", NULL, "MCLK MODE", is_mclk_mode},
6166879db76SBard Liao 
6176879db76SBard Liao 	{"HP Power", NULL, "LDO1"},
6186879db76SBard Liao 	{"HP Power", NULL, "LDO2"},
6196879db76SBard Liao 
6206879db76SBard Liao 	{"MIC1", NULL, "LDO1"},
6216879db76SBard Liao 	{"MIC1", NULL, "LDO2"},
6226879db76SBard Liao 	{"MIC1", NULL, "HV"},
6236879db76SBard Liao 	{"MIC1", NULL, "VREF"},
6246879db76SBard Liao 	{"MIC1", NULL, "MIC1 Input Buffer"},
6256879db76SBard Liao 
6266879db76SBard Liao 	{"SPO", NULL, "LDO1"},
6276879db76SBard Liao 	{"SPO", NULL, "LDO2"},
6286879db76SBard Liao 	{"SPO", NULL, "HV"},
6296879db76SBard Liao 	{"SPO", NULL, "VREF"},
6306879db76SBard Liao 
63107cf7cbaSBard Liao 	{"DMIC1", NULL, "DMIC1 Pin"},
63207cf7cbaSBard Liao 	{"DMIC2", NULL, "DMIC2 Pin"},
63307cf7cbaSBard Liao 	{"DMIC1", NULL, "DMIC Receiver"},
63407cf7cbaSBard Liao 	{"DMIC2", NULL, "DMIC Receiver"},
63507cf7cbaSBard Liao 
63607cf7cbaSBard Liao 	{"RECMIX", "Beep Switch", "Beep"},
63707cf7cbaSBard Liao 	{"RECMIX", "Line1 Switch", "LINE1"},
63807cf7cbaSBard Liao 	{"RECMIX", "Mic1 Switch", "MIC1"},
63907cf7cbaSBard Liao 
64007cf7cbaSBard Liao 	{"ADC 0 Mux", "Dmic", "DMIC1"},
64107cf7cbaSBard Liao 	{"ADC 0 Mux", "RECMIX", "RECMIX"},
64207cf7cbaSBard Liao 	{"ADC 0 Mux", "Mic", "MIC1"},
64307cf7cbaSBard Liao 	{"ADC 1 Mux", "Dmic", "DMIC2"},
64407cf7cbaSBard Liao 	{"ADC 1 Mux", "RECMIX", "RECMIX"},
64507cf7cbaSBard Liao 	{"ADC 1 Mux", "Mic", "MIC1"},
64607cf7cbaSBard Liao 
64707cf7cbaSBard Liao 	{"ADC 0", NULL, "ADC 0 Mux"},
64807cf7cbaSBard Liao 	{"ADC 1", NULL, "ADC 1 Mux"},
64907cf7cbaSBard Liao 
65007cf7cbaSBard Liao 	{"AIF1TX", NULL, "ADC 0"},
65107cf7cbaSBard Liao 	{"AIF2TX", NULL, "ADC 1"},
65207cf7cbaSBard Liao 
65307cf7cbaSBard Liao 	{"DAC 0", NULL, "AIF1RX"},
65407cf7cbaSBard Liao 	{"DAC 1", NULL, "AIF2RX"},
65507cf7cbaSBard Liao 
65607cf7cbaSBard Liao 	{"Front", "DAC Switch", "DAC 0"},
65707cf7cbaSBard Liao 	{"Front", "RECMIX Switch", "RECMIX"},
65807cf7cbaSBard Liao 
65907cf7cbaSBard Liao 	{"Surround", NULL, "DAC 1"},
66007cf7cbaSBard Liao 
66107cf7cbaSBard Liao 	{"SPK Mux", "Front", "Front"},
66207cf7cbaSBard Liao 	{"SPK Mux", "Surround", "Surround"},
66307cf7cbaSBard Liao 
66407cf7cbaSBard Liao 	{"HPO Mux", "Front", "Front"},
66507cf7cbaSBard Liao 	{"HPO Mux", "Surround", "Surround"},
66607cf7cbaSBard Liao 
66707cf7cbaSBard Liao 	{"SPO", "Switch", "SPK Mux"},
66807cf7cbaSBard Liao 	{"HPO L", "Switch", "HPO Mux"},
66907cf7cbaSBard Liao 	{"HPO R", "Switch", "HPO Mux"},
67007cf7cbaSBard Liao 	{"HPO L", NULL, "HP Power"},
67107cf7cbaSBard Liao 	{"HPO R", NULL, "HP Power"},
67207cf7cbaSBard Liao 
67307cf7cbaSBard Liao 	{"SPOL", NULL, "SPO"},
67407cf7cbaSBard Liao 	{"SPOR", NULL, "SPO"},
67507cf7cbaSBard Liao 	{"HPO Pin", NULL, "HPO L"},
67607cf7cbaSBard Liao 	{"HPO Pin", NULL, "HPO R"},
67707cf7cbaSBard Liao };
67807cf7cbaSBard Liao 
rt286_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,struct snd_soc_dai * dai)67907cf7cbaSBard Liao static int rt286_hw_params(struct snd_pcm_substream *substream,
68007cf7cbaSBard Liao 			    struct snd_pcm_hw_params *params,
68107cf7cbaSBard Liao 			    struct snd_soc_dai *dai)
68207cf7cbaSBard Liao {
68345101122SKuninori Morimoto 	struct snd_soc_component *component = dai->component;
68445101122SKuninori Morimoto 	struct rt286_priv *rt286 = snd_soc_component_get_drvdata(component);
68507cf7cbaSBard Liao 	unsigned int val = 0;
68607cf7cbaSBard Liao 	int d_len_code;
68707cf7cbaSBard Liao 
68807cf7cbaSBard Liao 	switch (params_rate(params)) {
68907cf7cbaSBard Liao 	/* bit 14 0:48K 1:44.1K */
69007cf7cbaSBard Liao 	case 44100:
69107cf7cbaSBard Liao 		val |= 0x4000;
69207cf7cbaSBard Liao 		break;
69307cf7cbaSBard Liao 	case 48000:
69407cf7cbaSBard Liao 		break;
69507cf7cbaSBard Liao 	default:
69645101122SKuninori Morimoto 		dev_err(component->dev, "Unsupported sample rate %d\n",
69707cf7cbaSBard Liao 					params_rate(params));
69807cf7cbaSBard Liao 		return -EINVAL;
69907cf7cbaSBard Liao 	}
70007cf7cbaSBard Liao 	switch (rt286->sys_clk) {
70107cf7cbaSBard Liao 	case 12288000:
70207cf7cbaSBard Liao 	case 24576000:
70307cf7cbaSBard Liao 		if (params_rate(params) != 48000) {
70445101122SKuninori Morimoto 			dev_err(component->dev, "Sys_clk is not matched (%d %d)\n",
70507cf7cbaSBard Liao 					params_rate(params), rt286->sys_clk);
70607cf7cbaSBard Liao 			return -EINVAL;
70707cf7cbaSBard Liao 		}
70807cf7cbaSBard Liao 		break;
70907cf7cbaSBard Liao 	case 11289600:
71007cf7cbaSBard Liao 	case 22579200:
71107cf7cbaSBard Liao 		if (params_rate(params) != 44100) {
71245101122SKuninori Morimoto 			dev_err(component->dev, "Sys_clk is not matched (%d %d)\n",
71307cf7cbaSBard Liao 					params_rate(params), rt286->sys_clk);
71407cf7cbaSBard Liao 			return -EINVAL;
71507cf7cbaSBard Liao 		}
71607cf7cbaSBard Liao 		break;
71707cf7cbaSBard Liao 	}
71807cf7cbaSBard Liao 
71907cf7cbaSBard Liao 	if (params_channels(params) <= 16) {
72007cf7cbaSBard Liao 		/* bit 3:0 Number of Channel */
72107cf7cbaSBard Liao 		val |= (params_channels(params) - 1);
72207cf7cbaSBard Liao 	} else {
72345101122SKuninori Morimoto 		dev_err(component->dev, "Unsupported channels %d\n",
72407cf7cbaSBard Liao 					params_channels(params));
72507cf7cbaSBard Liao 		return -EINVAL;
72607cf7cbaSBard Liao 	}
72707cf7cbaSBard Liao 
72807cf7cbaSBard Liao 	switch (params_width(params)) {
72907cf7cbaSBard Liao 	/* bit 6:4 Bits per Sample */
73007cf7cbaSBard Liao 	case 16:
73107cf7cbaSBard Liao 		d_len_code = 0;
73207cf7cbaSBard Liao 		val |= (0x1 << 4);
73307cf7cbaSBard Liao 		break;
73407cf7cbaSBard Liao 	case 32:
73507cf7cbaSBard Liao 		d_len_code = 2;
73607cf7cbaSBard Liao 		val |= (0x4 << 4);
73707cf7cbaSBard Liao 		break;
73807cf7cbaSBard Liao 	case 20:
73907cf7cbaSBard Liao 		d_len_code = 1;
74007cf7cbaSBard Liao 		val |= (0x2 << 4);
74107cf7cbaSBard Liao 		break;
74207cf7cbaSBard Liao 	case 24:
74307cf7cbaSBard Liao 		d_len_code = 2;
74407cf7cbaSBard Liao 		val |= (0x3 << 4);
74507cf7cbaSBard Liao 		break;
74607cf7cbaSBard Liao 	case 8:
74707cf7cbaSBard Liao 		d_len_code = 3;
74807cf7cbaSBard Liao 		break;
74907cf7cbaSBard Liao 	default:
75007cf7cbaSBard Liao 		return -EINVAL;
75107cf7cbaSBard Liao 	}
75207cf7cbaSBard Liao 
75345101122SKuninori Morimoto 	snd_soc_component_update_bits(component,
75407cf7cbaSBard Liao 		RT286_I2S_CTRL1, 0x0018, d_len_code << 3);
75545101122SKuninori Morimoto 	dev_dbg(component->dev, "format val = 0x%x\n", val);
75607cf7cbaSBard Liao 
75745101122SKuninori Morimoto 	snd_soc_component_update_bits(component, RT286_DAC_FORMAT, 0x407f, val);
75845101122SKuninori Morimoto 	snd_soc_component_update_bits(component, RT286_ADC_FORMAT, 0x407f, val);
75907cf7cbaSBard Liao 
76007cf7cbaSBard Liao 	return 0;
76107cf7cbaSBard Liao }
76207cf7cbaSBard Liao 
rt286_set_dai_fmt(struct snd_soc_dai * dai,unsigned int fmt)76307cf7cbaSBard Liao static int rt286_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
76407cf7cbaSBard Liao {
76545101122SKuninori Morimoto 	struct snd_soc_component *component = dai->component;
76607cf7cbaSBard Liao 
76707cf7cbaSBard Liao 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
76807cf7cbaSBard Liao 	case SND_SOC_DAIFMT_CBM_CFM:
76945101122SKuninori Morimoto 		snd_soc_component_update_bits(component,
77007cf7cbaSBard Liao 			RT286_I2S_CTRL1, 0x800, 0x800);
77107cf7cbaSBard Liao 		break;
77207cf7cbaSBard Liao 	case SND_SOC_DAIFMT_CBS_CFS:
77345101122SKuninori Morimoto 		snd_soc_component_update_bits(component,
77407cf7cbaSBard Liao 			RT286_I2S_CTRL1, 0x800, 0x0);
77507cf7cbaSBard Liao 		break;
77607cf7cbaSBard Liao 	default:
77707cf7cbaSBard Liao 		return -EINVAL;
77807cf7cbaSBard Liao 	}
77907cf7cbaSBard Liao 
78007cf7cbaSBard Liao 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
78107cf7cbaSBard Liao 	case SND_SOC_DAIFMT_I2S:
78245101122SKuninori Morimoto 		snd_soc_component_update_bits(component,
78307cf7cbaSBard Liao 			RT286_I2S_CTRL1, 0x300, 0x0);
78407cf7cbaSBard Liao 		break;
78507cf7cbaSBard Liao 	case SND_SOC_DAIFMT_LEFT_J:
78645101122SKuninori Morimoto 		snd_soc_component_update_bits(component,
78707cf7cbaSBard Liao 			RT286_I2S_CTRL1, 0x300, 0x1 << 8);
78807cf7cbaSBard Liao 		break;
78907cf7cbaSBard Liao 	case SND_SOC_DAIFMT_DSP_A:
79045101122SKuninori Morimoto 		snd_soc_component_update_bits(component,
79107cf7cbaSBard Liao 			RT286_I2S_CTRL1, 0x300, 0x2 << 8);
79207cf7cbaSBard Liao 		break;
79307cf7cbaSBard Liao 	case SND_SOC_DAIFMT_DSP_B:
79445101122SKuninori Morimoto 		snd_soc_component_update_bits(component,
79507cf7cbaSBard Liao 			RT286_I2S_CTRL1, 0x300, 0x3 << 8);
79607cf7cbaSBard Liao 		break;
79707cf7cbaSBard Liao 	default:
79807cf7cbaSBard Liao 		return -EINVAL;
79907cf7cbaSBard Liao 	}
80007cf7cbaSBard Liao 	/* bit 15 Stream Type 0:PCM 1:Non-PCM */
80145101122SKuninori Morimoto 	snd_soc_component_update_bits(component, RT286_DAC_FORMAT, 0x8000, 0);
80245101122SKuninori Morimoto 	snd_soc_component_update_bits(component, RT286_ADC_FORMAT, 0x8000, 0);
80307cf7cbaSBard Liao 
80407cf7cbaSBard Liao 	return 0;
80507cf7cbaSBard Liao }
80607cf7cbaSBard Liao 
rt286_set_dai_sysclk(struct snd_soc_dai * dai,int clk_id,unsigned int freq,int dir)80707cf7cbaSBard Liao static int rt286_set_dai_sysclk(struct snd_soc_dai *dai,
80807cf7cbaSBard Liao 				int clk_id, unsigned int freq, int dir)
80907cf7cbaSBard Liao {
81045101122SKuninori Morimoto 	struct snd_soc_component *component = dai->component;
81145101122SKuninori Morimoto 	struct rt286_priv *rt286 = snd_soc_component_get_drvdata(component);
81207cf7cbaSBard Liao 
81345101122SKuninori Morimoto 	dev_dbg(component->dev, "%s freq=%d\n", __func__, freq);
81407cf7cbaSBard Liao 
81507cf7cbaSBard Liao 	if (RT286_SCLK_S_MCLK == clk_id) {
81645101122SKuninori Morimoto 		snd_soc_component_update_bits(component,
81707cf7cbaSBard Liao 			RT286_I2S_CTRL2, 0x0100, 0x0);
81845101122SKuninori Morimoto 		snd_soc_component_update_bits(component,
81907cf7cbaSBard Liao 			RT286_PLL_CTRL1, 0x20, 0x20);
82007cf7cbaSBard Liao 	} else {
82145101122SKuninori Morimoto 		snd_soc_component_update_bits(component,
82207cf7cbaSBard Liao 			RT286_I2S_CTRL2, 0x0100, 0x0100);
82345101122SKuninori Morimoto 		snd_soc_component_update_bits(component,
82407cf7cbaSBard Liao 			RT286_PLL_CTRL, 0x4, 0x4);
82545101122SKuninori Morimoto 		snd_soc_component_update_bits(component,
82607cf7cbaSBard Liao 			RT286_PLL_CTRL1, 0x20, 0x0);
82707cf7cbaSBard Liao 	}
82807cf7cbaSBard Liao 
82907cf7cbaSBard Liao 	switch (freq) {
83007cf7cbaSBard Liao 	case 19200000:
83107cf7cbaSBard Liao 		if (RT286_SCLK_S_MCLK == clk_id) {
83245101122SKuninori Morimoto 			dev_err(component->dev, "Should not use MCLK\n");
83307cf7cbaSBard Liao 			return -EINVAL;
83407cf7cbaSBard Liao 		}
83545101122SKuninori Morimoto 		snd_soc_component_update_bits(component,
83607cf7cbaSBard Liao 			RT286_I2S_CTRL2, 0x40, 0x40);
83707cf7cbaSBard Liao 		break;
83807cf7cbaSBard Liao 	case 24000000:
83907cf7cbaSBard Liao 		if (RT286_SCLK_S_MCLK == clk_id) {
84045101122SKuninori Morimoto 			dev_err(component->dev, "Should not use MCLK\n");
84107cf7cbaSBard Liao 			return -EINVAL;
84207cf7cbaSBard Liao 		}
84345101122SKuninori Morimoto 		snd_soc_component_update_bits(component,
84407cf7cbaSBard Liao 			RT286_I2S_CTRL2, 0x40, 0x0);
84507cf7cbaSBard Liao 		break;
84607cf7cbaSBard Liao 	case 12288000:
84707cf7cbaSBard Liao 	case 11289600:
84845101122SKuninori Morimoto 		snd_soc_component_update_bits(component,
84907cf7cbaSBard Liao 			RT286_I2S_CTRL2, 0x8, 0x0);
85045101122SKuninori Morimoto 		snd_soc_component_update_bits(component,
85107cf7cbaSBard Liao 			RT286_CLK_DIV, 0xfc1e, 0x0004);
85207cf7cbaSBard Liao 		break;
85307cf7cbaSBard Liao 	case 24576000:
85407cf7cbaSBard Liao 	case 22579200:
85545101122SKuninori Morimoto 		snd_soc_component_update_bits(component,
85607cf7cbaSBard Liao 			RT286_I2S_CTRL2, 0x8, 0x8);
85745101122SKuninori Morimoto 		snd_soc_component_update_bits(component,
85807cf7cbaSBard Liao 			RT286_CLK_DIV, 0xfc1e, 0x5406);
85907cf7cbaSBard Liao 		break;
86007cf7cbaSBard Liao 	default:
86145101122SKuninori Morimoto 		dev_err(component->dev, "Unsupported system clock\n");
86207cf7cbaSBard Liao 		return -EINVAL;
86307cf7cbaSBard Liao 	}
86407cf7cbaSBard Liao 
86507cf7cbaSBard Liao 	rt286->sys_clk = freq;
8666879db76SBard Liao 	rt286->clk_id = clk_id;
86707cf7cbaSBard Liao 
86807cf7cbaSBard Liao 	return 0;
86907cf7cbaSBard Liao }
87007cf7cbaSBard Liao 
rt286_set_bclk_ratio(struct snd_soc_dai * dai,unsigned int ratio)87107cf7cbaSBard Liao static int rt286_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
87207cf7cbaSBard Liao {
87345101122SKuninori Morimoto 	struct snd_soc_component *component = dai->component;
87407cf7cbaSBard Liao 
87545101122SKuninori Morimoto 	dev_dbg(component->dev, "%s ratio=%d\n", __func__, ratio);
87607cf7cbaSBard Liao 	if (50 == ratio)
87745101122SKuninori Morimoto 		snd_soc_component_update_bits(component,
87807cf7cbaSBard Liao 			RT286_I2S_CTRL1, 0x1000, 0x1000);
87907cf7cbaSBard Liao 	else
88045101122SKuninori Morimoto 		snd_soc_component_update_bits(component,
88107cf7cbaSBard Liao 			RT286_I2S_CTRL1, 0x1000, 0x0);
88207cf7cbaSBard Liao 
88307cf7cbaSBard Liao 
88407cf7cbaSBard Liao 	return 0;
88507cf7cbaSBard Liao }
88607cf7cbaSBard Liao 
rt286_set_bias_level(struct snd_soc_component * component,enum snd_soc_bias_level level)88745101122SKuninori Morimoto static int rt286_set_bias_level(struct snd_soc_component *component,
88807cf7cbaSBard Liao 				 enum snd_soc_bias_level level)
88907cf7cbaSBard Liao {
89007cf7cbaSBard Liao 	switch (level) {
89107cf7cbaSBard Liao 	case SND_SOC_BIAS_PREPARE:
89245101122SKuninori Morimoto 		if (SND_SOC_BIAS_STANDBY == snd_soc_component_get_bias_level(component)) {
89345101122SKuninori Morimoto 			snd_soc_component_write(component,
89407cf7cbaSBard Liao 				RT286_SET_AUDIO_POWER, AC_PWRST_D0);
89545101122SKuninori Morimoto 			snd_soc_component_update_bits(component,
896bc6c4e45SBard Liao 				RT286_DC_GAIN, 0x200, 0x200);
897bc6c4e45SBard Liao 		}
898bc6c4e45SBard Liao 		break;
899bc6c4e45SBard Liao 
900bc6c4e45SBard Liao 	case SND_SOC_BIAS_ON:
901bc6c4e45SBard Liao 		mdelay(10);
90245101122SKuninori Morimoto 		snd_soc_component_update_bits(component,
9036879db76SBard Liao 			RT286_DC_GAIN, 0x200, 0x0);
9046879db76SBard Liao 
90507cf7cbaSBard Liao 		break;
90607cf7cbaSBard Liao 
90707cf7cbaSBard Liao 	case SND_SOC_BIAS_STANDBY:
90845101122SKuninori Morimoto 		snd_soc_component_write(component,
90907cf7cbaSBard Liao 			RT286_SET_AUDIO_POWER, AC_PWRST_D3);
91007cf7cbaSBard Liao 		break;
91107cf7cbaSBard Liao 
91207cf7cbaSBard Liao 	default:
91307cf7cbaSBard Liao 		break;
91407cf7cbaSBard Liao 	}
91507cf7cbaSBard Liao 
91607cf7cbaSBard Liao 	return 0;
91707cf7cbaSBard Liao }
91807cf7cbaSBard Liao 
rt286_irq(int irq,void * data)91907cf7cbaSBard Liao static irqreturn_t rt286_irq(int irq, void *data)
92007cf7cbaSBard Liao {
92107cf7cbaSBard Liao 	struct rt286_priv *rt286 = data;
92207cf7cbaSBard Liao 	bool hp = false;
92307cf7cbaSBard Liao 	bool mic = false;
92407cf7cbaSBard Liao 	int status = 0;
92507cf7cbaSBard Liao 
92690f601efSBard Liao 	rt286_jack_detect(rt286, &hp, &mic);
92707cf7cbaSBard Liao 
92807cf7cbaSBard Liao 	/* Clear IRQ */
92990f601efSBard Liao 	regmap_update_bits(rt286->regmap, RT286_IRQ_CTRL, 0x1, 0x1);
93007cf7cbaSBard Liao 
931af3b2b54SPierre-Louis Bossart 	if (hp)
93207cf7cbaSBard Liao 		status |= SND_JACK_HEADPHONE;
93307cf7cbaSBard Liao 
934af3b2b54SPierre-Louis Bossart 	if (mic)
93507cf7cbaSBard Liao 		status |= SND_JACK_MICROPHONE;
93607cf7cbaSBard Liao 
93707cf7cbaSBard Liao 	snd_soc_jack_report(rt286->jack, status,
93807cf7cbaSBard Liao 		SND_JACK_MICROPHONE | SND_JACK_HEADPHONE);
93907cf7cbaSBard Liao 
94007cf7cbaSBard Liao 	pm_wakeup_event(&rt286->i2c->dev, 300);
94107cf7cbaSBard Liao 
94207cf7cbaSBard Liao 	return IRQ_HANDLED;
94307cf7cbaSBard Liao }
94407cf7cbaSBard Liao 
rt286_probe(struct snd_soc_component * component)94545101122SKuninori Morimoto static int rt286_probe(struct snd_soc_component *component)
94607cf7cbaSBard Liao {
94745101122SKuninori Morimoto 	struct rt286_priv *rt286 = snd_soc_component_get_drvdata(component);
94807cf7cbaSBard Liao 
94945101122SKuninori Morimoto 	rt286->component = component;
9503082afe0SCezary Rojewski 	INIT_DELAYED_WORK(&rt286->jack_detect_work, rt286_jack_detect_work);
95190f601efSBard Liao 
9523082afe0SCezary Rojewski 	if (rt286->i2c->irq)
95390f601efSBard Liao 		schedule_delayed_work(&rt286->jack_detect_work,
9543082afe0SCezary Rojewski 				      msecs_to_jiffies(50));
95507cf7cbaSBard Liao 	return 0;
95607cf7cbaSBard Liao }
95707cf7cbaSBard Liao 
rt286_remove(struct snd_soc_component * component)95845101122SKuninori Morimoto static void rt286_remove(struct snd_soc_component *component)
95907cf7cbaSBard Liao {
96045101122SKuninori Morimoto 	struct rt286_priv *rt286 = snd_soc_component_get_drvdata(component);
96107cf7cbaSBard Liao 
96207cf7cbaSBard Liao 	cancel_delayed_work_sync(&rt286->jack_detect_work);
963c1d7ebdaSAmadeusz Sławiński 	rt286->component = NULL;
96407cf7cbaSBard Liao }
96507cf7cbaSBard Liao 
96607cf7cbaSBard Liao #ifdef CONFIG_PM
rt286_suspend(struct snd_soc_component * component)96745101122SKuninori Morimoto static int rt286_suspend(struct snd_soc_component *component)
96807cf7cbaSBard Liao {
96945101122SKuninori Morimoto 	struct rt286_priv *rt286 = snd_soc_component_get_drvdata(component);
97007cf7cbaSBard Liao 
97107cf7cbaSBard Liao 	regcache_cache_only(rt286->regmap, true);
97207cf7cbaSBard Liao 	regcache_mark_dirty(rt286->regmap);
97307cf7cbaSBard Liao 
97407cf7cbaSBard Liao 	return 0;
97507cf7cbaSBard Liao }
97607cf7cbaSBard Liao 
rt286_resume(struct snd_soc_component * component)97745101122SKuninori Morimoto static int rt286_resume(struct snd_soc_component *component)
97807cf7cbaSBard Liao {
97945101122SKuninori Morimoto 	struct rt286_priv *rt286 = snd_soc_component_get_drvdata(component);
98007cf7cbaSBard Liao 
98107cf7cbaSBard Liao 	regcache_cache_only(rt286->regmap, false);
98245101122SKuninori Morimoto 	rt286_index_sync(component);
98307cf7cbaSBard Liao 	regcache_sync(rt286->regmap);
98407cf7cbaSBard Liao 
98507cf7cbaSBard Liao 	return 0;
98607cf7cbaSBard Liao }
98707cf7cbaSBard Liao #else
98807cf7cbaSBard Liao #define rt286_suspend NULL
98907cf7cbaSBard Liao #define rt286_resume NULL
99007cf7cbaSBard Liao #endif
99107cf7cbaSBard Liao 
99207cf7cbaSBard Liao #define RT286_STEREO_RATES (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000)
99307cf7cbaSBard Liao #define RT286_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
99407cf7cbaSBard Liao 			SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S8)
99507cf7cbaSBard Liao 
99607cf7cbaSBard Liao static const struct snd_soc_dai_ops rt286_aif_dai_ops = {
99707cf7cbaSBard Liao 	.hw_params = rt286_hw_params,
99807cf7cbaSBard Liao 	.set_fmt = rt286_set_dai_fmt,
99907cf7cbaSBard Liao 	.set_sysclk = rt286_set_dai_sysclk,
100007cf7cbaSBard Liao 	.set_bclk_ratio = rt286_set_bclk_ratio,
100107cf7cbaSBard Liao };
100207cf7cbaSBard Liao 
100307cf7cbaSBard Liao static struct snd_soc_dai_driver rt286_dai[] = {
100407cf7cbaSBard Liao 	{
100507cf7cbaSBard Liao 		.name = "rt286-aif1",
100607cf7cbaSBard Liao 		.id = RT286_AIF1,
100707cf7cbaSBard Liao 		.playback = {
100807cf7cbaSBard Liao 			.stream_name = "AIF1 Playback",
100907cf7cbaSBard Liao 			.channels_min = 1,
101007cf7cbaSBard Liao 			.channels_max = 2,
101107cf7cbaSBard Liao 			.rates = RT286_STEREO_RATES,
101207cf7cbaSBard Liao 			.formats = RT286_FORMATS,
101307cf7cbaSBard Liao 		},
101407cf7cbaSBard Liao 		.capture = {
101507cf7cbaSBard Liao 			.stream_name = "AIF1 Capture",
101607cf7cbaSBard Liao 			.channels_min = 1,
101707cf7cbaSBard Liao 			.channels_max = 2,
101807cf7cbaSBard Liao 			.rates = RT286_STEREO_RATES,
101907cf7cbaSBard Liao 			.formats = RT286_FORMATS,
102007cf7cbaSBard Liao 		},
102107cf7cbaSBard Liao 		.ops = &rt286_aif_dai_ops,
1022bc03b391SKuninori Morimoto 		.symmetric_rate = 1,
102307cf7cbaSBard Liao 	},
102407cf7cbaSBard Liao 	{
102507cf7cbaSBard Liao 		.name = "rt286-aif2",
102607cf7cbaSBard Liao 		.id = RT286_AIF2,
102707cf7cbaSBard Liao 		.playback = {
102807cf7cbaSBard Liao 			.stream_name = "AIF2 Playback",
102907cf7cbaSBard Liao 			.channels_min = 1,
103007cf7cbaSBard Liao 			.channels_max = 2,
103107cf7cbaSBard Liao 			.rates = RT286_STEREO_RATES,
103207cf7cbaSBard Liao 			.formats = RT286_FORMATS,
103307cf7cbaSBard Liao 		},
103407cf7cbaSBard Liao 		.capture = {
103507cf7cbaSBard Liao 			.stream_name = "AIF2 Capture",
103607cf7cbaSBard Liao 			.channels_min = 1,
103707cf7cbaSBard Liao 			.channels_max = 2,
103807cf7cbaSBard Liao 			.rates = RT286_STEREO_RATES,
103907cf7cbaSBard Liao 			.formats = RT286_FORMATS,
104007cf7cbaSBard Liao 		},
104107cf7cbaSBard Liao 		.ops = &rt286_aif_dai_ops,
1042bc03b391SKuninori Morimoto 		.symmetric_rate = 1,
104307cf7cbaSBard Liao 	},
104407cf7cbaSBard Liao 
104507cf7cbaSBard Liao };
104607cf7cbaSBard Liao 
104745101122SKuninori Morimoto static const struct snd_soc_component_driver soc_component_dev_rt286 = {
104807cf7cbaSBard Liao 	.probe			= rt286_probe,
104907cf7cbaSBard Liao 	.remove			= rt286_remove,
105007cf7cbaSBard Liao 	.suspend		= rt286_suspend,
105107cf7cbaSBard Liao 	.resume			= rt286_resume,
105207cf7cbaSBard Liao 	.set_bias_level		= rt286_set_bias_level,
10533082afe0SCezary Rojewski 	.set_jack		= rt286_mic_detect,
105407cf7cbaSBard Liao 	.controls		= rt286_snd_controls,
105507cf7cbaSBard Liao 	.num_controls		= ARRAY_SIZE(rt286_snd_controls),
105607cf7cbaSBard Liao 	.dapm_widgets		= rt286_dapm_widgets,
105707cf7cbaSBard Liao 	.num_dapm_widgets	= ARRAY_SIZE(rt286_dapm_widgets),
105807cf7cbaSBard Liao 	.dapm_routes		= rt286_dapm_routes,
105907cf7cbaSBard Liao 	.num_dapm_routes	= ARRAY_SIZE(rt286_dapm_routes),
106045101122SKuninori Morimoto 	.use_pmdown_time	= 1,
106145101122SKuninori Morimoto 	.endianness		= 1,
106207cf7cbaSBard Liao };
106307cf7cbaSBard Liao 
106407cf7cbaSBard Liao static const struct regmap_config rt286_regmap = {
106507cf7cbaSBard Liao 	.reg_bits = 32,
106607cf7cbaSBard Liao 	.val_bits = 32,
106707cf7cbaSBard Liao 	.max_register = 0x02370100,
106807cf7cbaSBard Liao 	.volatile_reg = rt286_volatile_register,
106907cf7cbaSBard Liao 	.readable_reg = rt286_readable_register,
1070bc08f96bSOder Chiou 	.reg_write = rl6347a_hw_write,
1071bc08f96bSOder Chiou 	.reg_read = rl6347a_hw_read,
107207cf7cbaSBard Liao 	.cache_type = REGCACHE_RBTREE,
107307cf7cbaSBard Liao 	.reg_defaults = rt286_reg,
107407cf7cbaSBard Liao 	.num_reg_defaults = ARRAY_SIZE(rt286_reg),
107507cf7cbaSBard Liao };
107607cf7cbaSBard Liao 
107707cf7cbaSBard Liao static const struct i2c_device_id rt286_i2c_id[] = {
107807cf7cbaSBard Liao 	{"rt286", 0},
10793ab888dbSBard Liao 	{"rt288", 0},
108007cf7cbaSBard Liao 	{}
108107cf7cbaSBard Liao };
108207cf7cbaSBard Liao MODULE_DEVICE_TABLE(i2c, rt286_i2c_id);
108307cf7cbaSBard Liao 
108456bbfbfdSPierre-Louis Bossart #ifdef CONFIG_ACPI
108507cf7cbaSBard Liao static const struct acpi_device_id rt286_acpi_match[] = {
108607cf7cbaSBard Liao 	{ "INT343A", 0 },
108707cf7cbaSBard Liao 	{},
108807cf7cbaSBard Liao };
108907cf7cbaSBard Liao MODULE_DEVICE_TABLE(acpi, rt286_acpi_match);
109056bbfbfdSPierre-Louis Bossart #endif
109107cf7cbaSBard Liao 
1092a5afdc5bSAxel Lin static const struct dmi_system_id force_combo_jack_table[] = {
10936c67cde2SBard Liao 	{
10946c67cde2SBard Liao 		.ident = "Intel Wilson Beach",
10956c67cde2SBard Liao 		.matches = {
10966c67cde2SBard Liao 			DMI_MATCH(DMI_BOARD_NAME, "Wilson Beach SDS")
10976c67cde2SBard Liao 		}
10986c67cde2SBard Liao 	},
1099166765eaSVinod Koul 	{
1100166765eaSVinod Koul 		.ident = "Intel Skylake RVP",
1101166765eaSVinod Koul 		.matches = {
1102166765eaSVinod Koul 			DMI_MATCH(DMI_PRODUCT_NAME, "Skylake Client platform")
1103166765eaSVinod Koul 		}
1104166765eaSVinod Koul 	},
1105d06de6d9SVinod Koul 	{
1106d06de6d9SVinod Koul 		.ident = "Intel Kabylake RVP",
1107d06de6d9SVinod Koul 		.matches = {
1108d06de6d9SVinod Koul 			DMI_MATCH(DMI_PRODUCT_NAME, "Kabylake Client platform")
1109d06de6d9SVinod Koul 		}
1110d06de6d9SVinod Koul 	},
1111fe0dfd63SYifeng Li 	{
1112fe0dfd63SYifeng Li 		.ident = "Thinkpad Helix 2nd",
1113fe0dfd63SYifeng Li 		.matches = {
1114fe0dfd63SYifeng Li 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1115fe0dfd63SYifeng Li 			DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad Helix 2nd")
1116fe0dfd63SYifeng Li 		}
1117fe0dfd63SYifeng Li 	},
1118d06de6d9SVinod Koul 
11196c67cde2SBard Liao 	{ }
11206c67cde2SBard Liao };
11216c67cde2SBard Liao 
1122aa2f9c12SDavid Ward static const struct dmi_system_id dmi_dell[] = {
11232cc3f234SBard Liao 	{
1124aa2f9c12SDavid Ward 		.ident = "Dell",
11252cc3f234SBard Liao 		.matches = {
11262cc3f234SBard Liao 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
11272cc3f234SBard Liao 		}
11282cc3f234SBard Liao 	},
11292cc3f234SBard Liao 	{ }
11302cc3f234SBard Liao };
11312cc3f234SBard Liao 
rt286_i2c_probe(struct i2c_client * i2c)113235b88858SStephen Kitt static int rt286_i2c_probe(struct i2c_client *i2c)
113307cf7cbaSBard Liao {
113407cf7cbaSBard Liao 	struct rt286_platform_data *pdata = dev_get_platdata(&i2c->dev);
113507cf7cbaSBard Liao 	struct rt286_priv *rt286;
1136aa2f9c12SDavid Ward 	int i, ret, vendor_id;
113707cf7cbaSBard Liao 
113807cf7cbaSBard Liao 	rt286 = devm_kzalloc(&i2c->dev,	sizeof(*rt286),
113907cf7cbaSBard Liao 				GFP_KERNEL);
114007cf7cbaSBard Liao 	if (NULL == rt286)
114107cf7cbaSBard Liao 		return -ENOMEM;
114207cf7cbaSBard Liao 
114307cf7cbaSBard Liao 	rt286->regmap = devm_regmap_init(&i2c->dev, NULL, i2c, &rt286_regmap);
114407cf7cbaSBard Liao 	if (IS_ERR(rt286->regmap)) {
114507cf7cbaSBard Liao 		ret = PTR_ERR(rt286->regmap);
114607cf7cbaSBard Liao 		dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
114707cf7cbaSBard Liao 			ret);
114807cf7cbaSBard Liao 		return ret;
114907cf7cbaSBard Liao 	}
115007cf7cbaSBard Liao 
1151143526eeSBard Liao 	ret = regmap_read(rt286->regmap,
1152aa2f9c12SDavid Ward 		RT286_GET_PARAM(AC_NODE_ROOT, AC_PAR_VENDOR_ID), &vendor_id);
1153143526eeSBard Liao 	if (ret != 0) {
1154143526eeSBard Liao 		dev_err(&i2c->dev, "I2C error %d\n", ret);
1155143526eeSBard Liao 		return ret;
1156143526eeSBard Liao 	}
1157aa2f9c12SDavid Ward 	if (vendor_id != RT286_VENDOR_ID && vendor_id != RT288_VENDOR_ID) {
11584b21768aSBard Liao 		dev_err(&i2c->dev,
1159aa2f9c12SDavid Ward 			"Device with ID register %#x is not rt286\n",
1160aa2f9c12SDavid Ward 			vendor_id);
11614b21768aSBard Liao 		return -ENODEV;
11624b21768aSBard Liao 	}
11634b21768aSBard Liao 
1164dc6d84c6SAxel Lin 	rt286->index_cache = devm_kmemdup(&i2c->dev, rt286_index_def,
1165dc6d84c6SAxel Lin 					  sizeof(rt286_index_def), GFP_KERNEL);
1166dc6d84c6SAxel Lin 	if (!rt286->index_cache)
1167dc6d84c6SAxel Lin 		return -ENOMEM;
1168dc6d84c6SAxel Lin 
1169bc08f96bSOder Chiou 	rt286->index_cache_size = INDEX_CACHE_SIZE;
117007cf7cbaSBard Liao 	rt286->i2c = i2c;
117107cf7cbaSBard Liao 	i2c_set_clientdata(i2c, rt286);
117207cf7cbaSBard Liao 
1173d53d59ecSBard Liao 	/* restore codec default */
1174d53d59ecSBard Liao 	for (i = 0; i < INDEX_CACHE_SIZE; i++)
1175d53d59ecSBard Liao 		regmap_write(rt286->regmap, rt286->index_cache[i].reg,
1176d53d59ecSBard Liao 				rt286->index_cache[i].def);
1177d53d59ecSBard Liao 	for (i = 0; i < ARRAY_SIZE(rt286_reg); i++)
1178d53d59ecSBard Liao 		regmap_write(rt286->regmap, rt286_reg[i].reg,
1179d53d59ecSBard Liao 				rt286_reg[i].def);
1180d53d59ecSBard Liao 
118107cf7cbaSBard Liao 	if (pdata)
118207cf7cbaSBard Liao 		rt286->pdata = *pdata;
118307cf7cbaSBard Liao 
1184aa2f9c12SDavid Ward 	if ((vendor_id == RT288_VENDOR_ID && dmi_check_system(dmi_dell)) ||
1185aa2f9c12SDavid Ward 		dmi_check_system(force_combo_jack_table))
11866c67cde2SBard Liao 		rt286->pdata.cbj_en = true;
11876c67cde2SBard Liao 
118861a414c4SBard Liao 	regmap_write(rt286->regmap, RT286_SET_AUDIO_POWER, AC_PWRST_D3);
118961a414c4SBard Liao 
119061a414c4SBard Liao 	for (i = 0; i < RT286_POWER_REG_LEN; i++)
119161a414c4SBard Liao 		regmap_write(rt286->regmap,
119261a414c4SBard Liao 			RT286_SET_POWER(rt286_support_power_controls[i]),
119361a414c4SBard Liao 			AC_PWRST_D1);
119461a414c4SBard Liao 
119561a414c4SBard Liao 	if (!rt286->pdata.cbj_en) {
119661a414c4SBard Liao 		regmap_write(rt286->regmap, RT286_CBJ_CTRL2, 0x0000);
119761a414c4SBard Liao 		regmap_write(rt286->regmap, RT286_MIC1_DET_CTRL, 0x0816);
119861a414c4SBard Liao 		regmap_update_bits(rt286->regmap,
119961a414c4SBard Liao 					RT286_CBJ_CTRL1, 0xf000, 0xb000);
120061a414c4SBard Liao 	} else {
120161a414c4SBard Liao 		regmap_update_bits(rt286->regmap,
120261a414c4SBard Liao 					RT286_CBJ_CTRL1, 0xf000, 0x5000);
120361a414c4SBard Liao 	}
120461a414c4SBard Liao 
120561a414c4SBard Liao 	mdelay(10);
120661a414c4SBard Liao 
120761a414c4SBard Liao 	if (!rt286->pdata.gpio2_en)
1208dfa7b01dSDavid Ward 		regmap_write(rt286->regmap, RT286_SET_DMIC2_DEFAULT, 0x40);
120961a414c4SBard Liao 	else
121061a414c4SBard Liao 		regmap_write(rt286->regmap, RT286_SET_DMIC2_DEFAULT, 0);
121161a414c4SBard Liao 
121261a414c4SBard Liao 	mdelay(10);
121361a414c4SBard Liao 
12146879db76SBard Liao 	regmap_write(rt286->regmap, RT286_MISC_CTRL1, 0x0000);
12156879db76SBard Liao 	/* Power down LDO, VREF */
12166879db76SBard Liao 	regmap_update_bits(rt286->regmap, RT286_POWER_CTRL2, 0xc, 0x0);
12176879db76SBard Liao 	regmap_update_bits(rt286->regmap, RT286_POWER_CTRL1, 0x1001, 0x1001);
121861a414c4SBard Liao 
1219bc6c4e45SBard Liao 	/* Set depop parameter */
1220bc6c4e45SBard Liao 	regmap_update_bits(rt286->regmap, RT286_DEPOP_CTRL2, 0x403a, 0x401a);
1221bc6c4e45SBard Liao 	regmap_update_bits(rt286->regmap, RT286_DEPOP_CTRL3, 0xf777, 0x4737);
1222bc6c4e45SBard Liao 	regmap_update_bits(rt286->regmap, RT286_DEPOP_CTRL4, 0x00ff, 0x003f);
1223bc6c4e45SBard Liao 
1224aa2f9c12SDavid Ward 	if (vendor_id == RT288_VENDOR_ID && dmi_check_system(dmi_dell)) {
12252cc3f234SBard Liao 		regmap_update_bits(rt286->regmap,
12262cc3f234SBard Liao 			RT286_SET_GPIO_MASK, 0x40, 0x40);
12272cc3f234SBard Liao 		regmap_update_bits(rt286->regmap,
12282cc3f234SBard Liao 			RT286_SET_GPIO_DIRECTION, 0x40, 0x40);
12292cc3f234SBard Liao 		regmap_update_bits(rt286->regmap,
12302cc3f234SBard Liao 			RT286_SET_GPIO_DATA, 0x40, 0x40);
12312cc3f234SBard Liao 		regmap_update_bits(rt286->regmap,
12322cc3f234SBard Liao 			RT286_GPIO_CTRL, 0xc, 0x8);
12332cc3f234SBard Liao 	}
12342cc3f234SBard Liao 
123561a414c4SBard Liao 	if (rt286->i2c->irq) {
123661a414c4SBard Liao 		ret = request_threaded_irq(rt286->i2c->irq, NULL, rt286_irq,
123761a414c4SBard Liao 			IRQF_TRIGGER_HIGH | IRQF_ONESHOT, "rt286", rt286);
123861a414c4SBard Liao 		if (ret != 0) {
123961a414c4SBard Liao 			dev_err(&i2c->dev,
124061a414c4SBard Liao 				"Failed to reguest IRQ: %d\n", ret);
124161a414c4SBard Liao 			return ret;
124261a414c4SBard Liao 		}
124361a414c4SBard Liao 	}
124461a414c4SBard Liao 
124545101122SKuninori Morimoto 	ret = devm_snd_soc_register_component(&i2c->dev,
124645101122SKuninori Morimoto 				     &soc_component_dev_rt286,
124707cf7cbaSBard Liao 				     rt286_dai, ARRAY_SIZE(rt286_dai));
124807cf7cbaSBard Liao 
124907cf7cbaSBard Liao 	return ret;
125007cf7cbaSBard Liao }
125107cf7cbaSBard Liao 
rt286_i2c_remove(struct i2c_client * i2c)1252ed5c2f5fSUwe Kleine-König static void rt286_i2c_remove(struct i2c_client *i2c)
125307cf7cbaSBard Liao {
125407cf7cbaSBard Liao 	struct rt286_priv *rt286 = i2c_get_clientdata(i2c);
125507cf7cbaSBard Liao 
125607cf7cbaSBard Liao 	if (i2c->irq)
125707cf7cbaSBard Liao 		free_irq(i2c->irq, rt286);
125807cf7cbaSBard Liao }
125907cf7cbaSBard Liao 
126007cf7cbaSBard Liao 
126123c4fd5cSBard Liao static struct i2c_driver rt286_i2c_driver = {
126207cf7cbaSBard Liao 	.driver = {
126307cf7cbaSBard Liao 		   .name = "rt286",
126407cf7cbaSBard Liao 		   .acpi_match_table = ACPI_PTR(rt286_acpi_match),
126507cf7cbaSBard Liao 		   },
1266*9abcd240SUwe Kleine-König 	.probe = rt286_i2c_probe,
126707cf7cbaSBard Liao 	.remove = rt286_i2c_remove,
126807cf7cbaSBard Liao 	.id_table = rt286_i2c_id,
126907cf7cbaSBard Liao };
127007cf7cbaSBard Liao 
127107cf7cbaSBard Liao module_i2c_driver(rt286_i2c_driver);
127207cf7cbaSBard Liao 
127307cf7cbaSBard Liao MODULE_DESCRIPTION("ASoC RT286 driver");
127407cf7cbaSBard Liao MODULE_AUTHOR("Bard Liao <bardliao@realtek.com>");
127507cf7cbaSBard Liao MODULE_LICENSE("GPL");
1276