xref: /linux/sound/soc/codecs/rt5651.c (revision d306873589c5a4c13df7176cd73d66ebfa690064)
140bc18a2SBard Liao /*
240bc18a2SBard Liao  * rt5651.c  --  RT5651 ALSA SoC audio codec driver
340bc18a2SBard Liao  *
440bc18a2SBard Liao  * Copyright 2014 Realtek Semiconductor Corp.
540bc18a2SBard Liao  * Author: Bard Liao <bardliao@realtek.com>
640bc18a2SBard Liao  *
740bc18a2SBard Liao  * This program is free software; you can redistribute it and/or modify
840bc18a2SBard Liao  * it under the terms of the GNU General Public License version 2 as
940bc18a2SBard Liao  * published by the Free Software Foundation.
1040bc18a2SBard Liao  */
1140bc18a2SBard Liao 
1240bc18a2SBard Liao #include <linux/module.h>
1340bc18a2SBard Liao #include <linux/init.h>
1440bc18a2SBard Liao #include <linux/delay.h>
1540bc18a2SBard Liao #include <linux/pm.h>
1640bc18a2SBard Liao #include <linux/i2c.h>
1740bc18a2SBard Liao #include <linux/regmap.h>
1840bc18a2SBard Liao #include <linux/platform_device.h>
1940bc18a2SBard Liao #include <linux/spi/spi.h>
203ae08dc0SBard Liao #include <linux/acpi.h>
2140bc18a2SBard Liao #include <sound/core.h>
2240bc18a2SBard Liao #include <sound/pcm.h>
2340bc18a2SBard Liao #include <sound/pcm_params.h>
2440bc18a2SBard Liao #include <sound/soc.h>
2540bc18a2SBard Liao #include <sound/soc-dapm.h>
2640bc18a2SBard Liao #include <sound/initval.h>
2740bc18a2SBard Liao #include <sound/tlv.h>
2880bbe4a3SCarlo Caione #include <sound/jack.h>
2940bc18a2SBard Liao 
3049ef7925SOder Chiou #include "rl6231.h"
3140bc18a2SBard Liao #include "rt5651.h"
3240bc18a2SBard Liao 
3340bc18a2SBard Liao #define RT5651_DEVICE_ID_VALUE 0x6281
3440bc18a2SBard Liao 
3540bc18a2SBard Liao #define RT5651_PR_RANGE_BASE (0xff + 1)
3640bc18a2SBard Liao #define RT5651_PR_SPACING 0x100
3740bc18a2SBard Liao 
3840bc18a2SBard Liao #define RT5651_PR_BASE (RT5651_PR_RANGE_BASE + (0 * RT5651_PR_SPACING))
3940bc18a2SBard Liao 
4040bc18a2SBard Liao static const struct regmap_range_cfg rt5651_ranges[] = {
4140bc18a2SBard Liao 	{ .name = "PR", .range_min = RT5651_PR_BASE,
4240bc18a2SBard Liao 	  .range_max = RT5651_PR_BASE + 0xb4,
4340bc18a2SBard Liao 	  .selector_reg = RT5651_PRIV_INDEX,
4440bc18a2SBard Liao 	  .selector_mask = 0xff,
4540bc18a2SBard Liao 	  .selector_shift = 0x0,
4640bc18a2SBard Liao 	  .window_start = RT5651_PRIV_DATA,
4740bc18a2SBard Liao 	  .window_len = 0x1, },
4840bc18a2SBard Liao };
4940bc18a2SBard Liao 
5041a5fefeSMark Brown static const struct reg_sequence init_list[] = {
5140bc18a2SBard Liao 	{RT5651_PR_BASE + 0x3d,	0x3e00},
5240bc18a2SBard Liao };
5340bc18a2SBard Liao 
5440bc18a2SBard Liao static const struct reg_default rt5651_reg[] = {
5540bc18a2SBard Liao 	{ 0x00, 0x0000 },
5640bc18a2SBard Liao 	{ 0x02, 0xc8c8 },
5740bc18a2SBard Liao 	{ 0x03, 0xc8c8 },
5840bc18a2SBard Liao 	{ 0x05, 0x0000 },
5940bc18a2SBard Liao 	{ 0x0d, 0x0000 },
6040bc18a2SBard Liao 	{ 0x0e, 0x0000 },
6140bc18a2SBard Liao 	{ 0x0f, 0x0808 },
6240bc18a2SBard Liao 	{ 0x10, 0x0808 },
6340bc18a2SBard Liao 	{ 0x19, 0xafaf },
6440bc18a2SBard Liao 	{ 0x1a, 0xafaf },
6540bc18a2SBard Liao 	{ 0x1b, 0x0c00 },
6640bc18a2SBard Liao 	{ 0x1c, 0x2f2f },
6740bc18a2SBard Liao 	{ 0x1d, 0x2f2f },
6840bc18a2SBard Liao 	{ 0x1e, 0x0000 },
6940bc18a2SBard Liao 	{ 0x27, 0x7860 },
7040bc18a2SBard Liao 	{ 0x28, 0x7070 },
7140bc18a2SBard Liao 	{ 0x29, 0x8080 },
7240bc18a2SBard Liao 	{ 0x2a, 0x5252 },
7340bc18a2SBard Liao 	{ 0x2b, 0x5454 },
7440bc18a2SBard Liao 	{ 0x2f, 0x0000 },
7540bc18a2SBard Liao 	{ 0x30, 0x5000 },
7640bc18a2SBard Liao 	{ 0x3b, 0x0000 },
7740bc18a2SBard Liao 	{ 0x3c, 0x006f },
7840bc18a2SBard Liao 	{ 0x3d, 0x0000 },
7940bc18a2SBard Liao 	{ 0x3e, 0x006f },
8040bc18a2SBard Liao 	{ 0x45, 0x6000 },
8140bc18a2SBard Liao 	{ 0x4d, 0x0000 },
8240bc18a2SBard Liao 	{ 0x4e, 0x0000 },
8340bc18a2SBard Liao 	{ 0x4f, 0x0279 },
8440bc18a2SBard Liao 	{ 0x50, 0x0000 },
8540bc18a2SBard Liao 	{ 0x51, 0x0000 },
8640bc18a2SBard Liao 	{ 0x52, 0x0279 },
8740bc18a2SBard Liao 	{ 0x53, 0xf000 },
8840bc18a2SBard Liao 	{ 0x61, 0x0000 },
8940bc18a2SBard Liao 	{ 0x62, 0x0000 },
9040bc18a2SBard Liao 	{ 0x63, 0x00c0 },
9140bc18a2SBard Liao 	{ 0x64, 0x0000 },
9240bc18a2SBard Liao 	{ 0x65, 0x0000 },
9340bc18a2SBard Liao 	{ 0x66, 0x0000 },
9440bc18a2SBard Liao 	{ 0x70, 0x8000 },
9540bc18a2SBard Liao 	{ 0x71, 0x8000 },
9640bc18a2SBard Liao 	{ 0x73, 0x1104 },
9740bc18a2SBard Liao 	{ 0x74, 0x0c00 },
9840bc18a2SBard Liao 	{ 0x75, 0x1400 },
9940bc18a2SBard Liao 	{ 0x77, 0x0c00 },
10040bc18a2SBard Liao 	{ 0x78, 0x4000 },
10140bc18a2SBard Liao 	{ 0x79, 0x0123 },
10240bc18a2SBard Liao 	{ 0x80, 0x0000 },
10340bc18a2SBard Liao 	{ 0x81, 0x0000 },
10440bc18a2SBard Liao 	{ 0x82, 0x0000 },
10540bc18a2SBard Liao 	{ 0x83, 0x0800 },
10640bc18a2SBard Liao 	{ 0x84, 0x0000 },
10740bc18a2SBard Liao 	{ 0x85, 0x0008 },
10840bc18a2SBard Liao 	{ 0x89, 0x0000 },
10940bc18a2SBard Liao 	{ 0x8e, 0x0004 },
11040bc18a2SBard Liao 	{ 0x8f, 0x1100 },
11140bc18a2SBard Liao 	{ 0x90, 0x0000 },
11240bc18a2SBard Liao 	{ 0x93, 0x2000 },
11340bc18a2SBard Liao 	{ 0x94, 0x0200 },
11440bc18a2SBard Liao 	{ 0xb0, 0x2080 },
11540bc18a2SBard Liao 	{ 0xb1, 0x0000 },
11640bc18a2SBard Liao 	{ 0xb4, 0x2206 },
11740bc18a2SBard Liao 	{ 0xb5, 0x1f00 },
11840bc18a2SBard Liao 	{ 0xb6, 0x0000 },
11940bc18a2SBard Liao 	{ 0xbb, 0x0000 },
12040bc18a2SBard Liao 	{ 0xbc, 0x0000 },
12140bc18a2SBard Liao 	{ 0xbd, 0x0000 },
12240bc18a2SBard Liao 	{ 0xbe, 0x0000 },
12340bc18a2SBard Liao 	{ 0xbf, 0x0000 },
12440bc18a2SBard Liao 	{ 0xc0, 0x0400 },
12540bc18a2SBard Liao 	{ 0xc1, 0x0000 },
12640bc18a2SBard Liao 	{ 0xc2, 0x0000 },
12740bc18a2SBard Liao 	{ 0xcf, 0x0013 },
12840bc18a2SBard Liao 	{ 0xd0, 0x0680 },
12940bc18a2SBard Liao 	{ 0xd1, 0x1c17 },
13040bc18a2SBard Liao 	{ 0xd3, 0xb320 },
13140bc18a2SBard Liao 	{ 0xd9, 0x0809 },
13240bc18a2SBard Liao 	{ 0xfa, 0x0010 },
13340bc18a2SBard Liao 	{ 0xfe, 0x10ec },
13440bc18a2SBard Liao 	{ 0xff, 0x6281 },
13540bc18a2SBard Liao };
13640bc18a2SBard Liao 
13740bc18a2SBard Liao static bool rt5651_volatile_register(struct device *dev,  unsigned int reg)
13840bc18a2SBard Liao {
13940bc18a2SBard Liao 	int i;
14040bc18a2SBard Liao 
14140bc18a2SBard Liao 	for (i = 0; i < ARRAY_SIZE(rt5651_ranges); i++) {
14240bc18a2SBard Liao 		if ((reg >= rt5651_ranges[i].window_start &&
14340bc18a2SBard Liao 		     reg <= rt5651_ranges[i].window_start +
14440bc18a2SBard Liao 		     rt5651_ranges[i].window_len) ||
14540bc18a2SBard Liao 		    (reg >= rt5651_ranges[i].range_min &&
14640bc18a2SBard Liao 		     reg <= rt5651_ranges[i].range_max)) {
14740bc18a2SBard Liao 			return true;
14840bc18a2SBard Liao 		}
14940bc18a2SBard Liao 	}
15040bc18a2SBard Liao 
15140bc18a2SBard Liao 	switch (reg) {
15240bc18a2SBard Liao 	case RT5651_RESET:
15340bc18a2SBard Liao 	case RT5651_PRIV_DATA:
15440bc18a2SBard Liao 	case RT5651_EQ_CTRL1:
15540bc18a2SBard Liao 	case RT5651_ALC_1:
15640bc18a2SBard Liao 	case RT5651_IRQ_CTRL2:
15740bc18a2SBard Liao 	case RT5651_INT_IRQ_ST:
15840bc18a2SBard Liao 	case RT5651_PGM_REG_ARR1:
15940bc18a2SBard Liao 	case RT5651_PGM_REG_ARR3:
16040bc18a2SBard Liao 	case RT5651_VENDOR_ID:
16140bc18a2SBard Liao 	case RT5651_DEVICE_ID:
16240bc18a2SBard Liao 		return true;
16340bc18a2SBard Liao 	default:
16440bc18a2SBard Liao 		return false;
16540bc18a2SBard Liao 	}
16640bc18a2SBard Liao }
16740bc18a2SBard Liao 
16840bc18a2SBard Liao static bool rt5651_readable_register(struct device *dev, unsigned int reg)
16940bc18a2SBard Liao {
17040bc18a2SBard Liao 	int i;
17140bc18a2SBard Liao 
17240bc18a2SBard Liao 	for (i = 0; i < ARRAY_SIZE(rt5651_ranges); i++) {
17340bc18a2SBard Liao 		if ((reg >= rt5651_ranges[i].window_start &&
17440bc18a2SBard Liao 		     reg <= rt5651_ranges[i].window_start +
17540bc18a2SBard Liao 		     rt5651_ranges[i].window_len) ||
17640bc18a2SBard Liao 		    (reg >= rt5651_ranges[i].range_min &&
17740bc18a2SBard Liao 		     reg <= rt5651_ranges[i].range_max)) {
17840bc18a2SBard Liao 			return true;
17940bc18a2SBard Liao 		}
18040bc18a2SBard Liao 	}
18140bc18a2SBard Liao 
18240bc18a2SBard Liao 	switch (reg) {
18340bc18a2SBard Liao 	case RT5651_RESET:
18440bc18a2SBard Liao 	case RT5651_VERSION_ID:
18540bc18a2SBard Liao 	case RT5651_VENDOR_ID:
18640bc18a2SBard Liao 	case RT5651_DEVICE_ID:
18740bc18a2SBard Liao 	case RT5651_HP_VOL:
18840bc18a2SBard Liao 	case RT5651_LOUT_CTRL1:
18940bc18a2SBard Liao 	case RT5651_LOUT_CTRL2:
19040bc18a2SBard Liao 	case RT5651_IN1_IN2:
19140bc18a2SBard Liao 	case RT5651_IN3:
19240bc18a2SBard Liao 	case RT5651_INL1_INR1_VOL:
19340bc18a2SBard Liao 	case RT5651_INL2_INR2_VOL:
19440bc18a2SBard Liao 	case RT5651_DAC1_DIG_VOL:
19540bc18a2SBard Liao 	case RT5651_DAC2_DIG_VOL:
19640bc18a2SBard Liao 	case RT5651_DAC2_CTRL:
19740bc18a2SBard Liao 	case RT5651_ADC_DIG_VOL:
19840bc18a2SBard Liao 	case RT5651_ADC_DATA:
19940bc18a2SBard Liao 	case RT5651_ADC_BST_VOL:
20040bc18a2SBard Liao 	case RT5651_STO1_ADC_MIXER:
20140bc18a2SBard Liao 	case RT5651_STO2_ADC_MIXER:
20240bc18a2SBard Liao 	case RT5651_AD_DA_MIXER:
20340bc18a2SBard Liao 	case RT5651_STO_DAC_MIXER:
20440bc18a2SBard Liao 	case RT5651_DD_MIXER:
20540bc18a2SBard Liao 	case RT5651_DIG_INF_DATA:
20640bc18a2SBard Liao 	case RT5651_PDM_CTL:
20740bc18a2SBard Liao 	case RT5651_REC_L1_MIXER:
20840bc18a2SBard Liao 	case RT5651_REC_L2_MIXER:
20940bc18a2SBard Liao 	case RT5651_REC_R1_MIXER:
21040bc18a2SBard Liao 	case RT5651_REC_R2_MIXER:
21140bc18a2SBard Liao 	case RT5651_HPO_MIXER:
21240bc18a2SBard Liao 	case RT5651_OUT_L1_MIXER:
21340bc18a2SBard Liao 	case RT5651_OUT_L2_MIXER:
21440bc18a2SBard Liao 	case RT5651_OUT_L3_MIXER:
21540bc18a2SBard Liao 	case RT5651_OUT_R1_MIXER:
21640bc18a2SBard Liao 	case RT5651_OUT_R2_MIXER:
21740bc18a2SBard Liao 	case RT5651_OUT_R3_MIXER:
21840bc18a2SBard Liao 	case RT5651_LOUT_MIXER:
21940bc18a2SBard Liao 	case RT5651_PWR_DIG1:
22040bc18a2SBard Liao 	case RT5651_PWR_DIG2:
22140bc18a2SBard Liao 	case RT5651_PWR_ANLG1:
22240bc18a2SBard Liao 	case RT5651_PWR_ANLG2:
22340bc18a2SBard Liao 	case RT5651_PWR_MIXER:
22440bc18a2SBard Liao 	case RT5651_PWR_VOL:
22540bc18a2SBard Liao 	case RT5651_PRIV_INDEX:
22640bc18a2SBard Liao 	case RT5651_PRIV_DATA:
22740bc18a2SBard Liao 	case RT5651_I2S1_SDP:
22840bc18a2SBard Liao 	case RT5651_I2S2_SDP:
22940bc18a2SBard Liao 	case RT5651_ADDA_CLK1:
23040bc18a2SBard Liao 	case RT5651_ADDA_CLK2:
23140bc18a2SBard Liao 	case RT5651_DMIC:
23240bc18a2SBard Liao 	case RT5651_TDM_CTL_1:
23340bc18a2SBard Liao 	case RT5651_TDM_CTL_2:
23440bc18a2SBard Liao 	case RT5651_TDM_CTL_3:
23540bc18a2SBard Liao 	case RT5651_GLB_CLK:
23640bc18a2SBard Liao 	case RT5651_PLL_CTRL1:
23740bc18a2SBard Liao 	case RT5651_PLL_CTRL2:
23840bc18a2SBard Liao 	case RT5651_PLL_MODE_1:
23940bc18a2SBard Liao 	case RT5651_PLL_MODE_2:
24040bc18a2SBard Liao 	case RT5651_PLL_MODE_3:
24140bc18a2SBard Liao 	case RT5651_PLL_MODE_4:
24240bc18a2SBard Liao 	case RT5651_PLL_MODE_5:
24340bc18a2SBard Liao 	case RT5651_PLL_MODE_6:
24440bc18a2SBard Liao 	case RT5651_PLL_MODE_7:
24540bc18a2SBard Liao 	case RT5651_DEPOP_M1:
24640bc18a2SBard Liao 	case RT5651_DEPOP_M2:
24740bc18a2SBard Liao 	case RT5651_DEPOP_M3:
24840bc18a2SBard Liao 	case RT5651_CHARGE_PUMP:
24940bc18a2SBard Liao 	case RT5651_MICBIAS:
25040bc18a2SBard Liao 	case RT5651_A_JD_CTL1:
25140bc18a2SBard Liao 	case RT5651_EQ_CTRL1:
25240bc18a2SBard Liao 	case RT5651_EQ_CTRL2:
25340bc18a2SBard Liao 	case RT5651_ALC_1:
25440bc18a2SBard Liao 	case RT5651_ALC_2:
25540bc18a2SBard Liao 	case RT5651_ALC_3:
25640bc18a2SBard Liao 	case RT5651_JD_CTRL1:
25740bc18a2SBard Liao 	case RT5651_JD_CTRL2:
25840bc18a2SBard Liao 	case RT5651_IRQ_CTRL1:
25940bc18a2SBard Liao 	case RT5651_IRQ_CTRL2:
26040bc18a2SBard Liao 	case RT5651_INT_IRQ_ST:
26140bc18a2SBard Liao 	case RT5651_GPIO_CTRL1:
26240bc18a2SBard Liao 	case RT5651_GPIO_CTRL2:
26340bc18a2SBard Liao 	case RT5651_GPIO_CTRL3:
26440bc18a2SBard Liao 	case RT5651_PGM_REG_ARR1:
26540bc18a2SBard Liao 	case RT5651_PGM_REG_ARR2:
26640bc18a2SBard Liao 	case RT5651_PGM_REG_ARR3:
26740bc18a2SBard Liao 	case RT5651_PGM_REG_ARR4:
26840bc18a2SBard Liao 	case RT5651_PGM_REG_ARR5:
26940bc18a2SBard Liao 	case RT5651_SCB_FUNC:
27040bc18a2SBard Liao 	case RT5651_SCB_CTRL:
27140bc18a2SBard Liao 	case RT5651_BASE_BACK:
27240bc18a2SBard Liao 	case RT5651_MP3_PLUS1:
27340bc18a2SBard Liao 	case RT5651_MP3_PLUS2:
27440bc18a2SBard Liao 	case RT5651_ADJ_HPF_CTRL1:
27540bc18a2SBard Liao 	case RT5651_ADJ_HPF_CTRL2:
27640bc18a2SBard Liao 	case RT5651_HP_CALIB_AMP_DET:
27740bc18a2SBard Liao 	case RT5651_HP_CALIB2:
27840bc18a2SBard Liao 	case RT5651_SV_ZCD1:
27940bc18a2SBard Liao 	case RT5651_SV_ZCD2:
28040bc18a2SBard Liao 	case RT5651_D_MISC:
28140bc18a2SBard Liao 	case RT5651_DUMMY2:
28240bc18a2SBard Liao 	case RT5651_DUMMY3:
28340bc18a2SBard Liao 		return true;
28440bc18a2SBard Liao 	default:
28540bc18a2SBard Liao 		return false;
28640bc18a2SBard Liao 	}
28740bc18a2SBard Liao }
28840bc18a2SBard Liao 
28940bc18a2SBard Liao static const DECLARE_TLV_DB_SCALE(out_vol_tlv, -4650, 150, 0);
29040bc18a2SBard Liao static const DECLARE_TLV_DB_SCALE(dac_vol_tlv, -65625, 375, 0);
29140bc18a2SBard Liao static const DECLARE_TLV_DB_SCALE(in_vol_tlv, -3450, 150, 0);
29240bc18a2SBard Liao static const DECLARE_TLV_DB_SCALE(adc_vol_tlv, -17625, 375, 0);
29340bc18a2SBard Liao static const DECLARE_TLV_DB_SCALE(adc_bst_tlv, 0, 1200, 0);
29440bc18a2SBard Liao 
29540bc18a2SBard Liao /* {0, +20, +24, +30, +35, +40, +44, +50, +52} dB */
2968e3648e1SLars-Peter Clausen static const DECLARE_TLV_DB_RANGE(bst_tlv,
29740bc18a2SBard Liao 	0, 0, TLV_DB_SCALE_ITEM(0, 0, 0),
29840bc18a2SBard Liao 	1, 1, TLV_DB_SCALE_ITEM(2000, 0, 0),
29940bc18a2SBard Liao 	2, 2, TLV_DB_SCALE_ITEM(2400, 0, 0),
30040bc18a2SBard Liao 	3, 5, TLV_DB_SCALE_ITEM(3000, 500, 0),
30140bc18a2SBard Liao 	6, 6, TLV_DB_SCALE_ITEM(4400, 0, 0),
30240bc18a2SBard Liao 	7, 7, TLV_DB_SCALE_ITEM(5000, 0, 0),
3038e3648e1SLars-Peter Clausen 	8, 8, TLV_DB_SCALE_ITEM(5200, 0, 0)
3048e3648e1SLars-Peter Clausen );
30540bc18a2SBard Liao 
30640bc18a2SBard Liao /* Interface data select */
30740bc18a2SBard Liao static const char * const rt5651_data_select[] = {
30840bc18a2SBard Liao 	"Normal", "Swap", "left copy to right", "right copy to left"};
30940bc18a2SBard Liao 
31040bc18a2SBard Liao static SOC_ENUM_SINGLE_DECL(rt5651_if2_dac_enum, RT5651_DIG_INF_DATA,
31140bc18a2SBard Liao 				RT5651_IF2_DAC_SEL_SFT, rt5651_data_select);
31240bc18a2SBard Liao 
31340bc18a2SBard Liao static SOC_ENUM_SINGLE_DECL(rt5651_if2_adc_enum, RT5651_DIG_INF_DATA,
31440bc18a2SBard Liao 				RT5651_IF2_ADC_SEL_SFT, rt5651_data_select);
31540bc18a2SBard Liao 
31640bc18a2SBard Liao static const struct snd_kcontrol_new rt5651_snd_controls[] = {
31740bc18a2SBard Liao 	/* Headphone Output Volume */
31840bc18a2SBard Liao 	SOC_DOUBLE_TLV("HP Playback Volume", RT5651_HP_VOL,
31940bc18a2SBard Liao 		RT5651_L_VOL_SFT, RT5651_R_VOL_SFT, 39, 1, out_vol_tlv),
32040bc18a2SBard Liao 	/* OUTPUT Control */
32140bc18a2SBard Liao 	SOC_DOUBLE_TLV("OUT Playback Volume", RT5651_LOUT_CTRL1,
32240bc18a2SBard Liao 		RT5651_L_VOL_SFT, RT5651_R_VOL_SFT, 39, 1, out_vol_tlv),
32340bc18a2SBard Liao 
32440bc18a2SBard Liao 	/* DAC Digital Volume */
32540bc18a2SBard Liao 	SOC_DOUBLE("DAC2 Playback Switch", RT5651_DAC2_CTRL,
32640bc18a2SBard Liao 		RT5651_M_DAC_L2_VOL_SFT, RT5651_M_DAC_R2_VOL_SFT, 1, 1),
32740bc18a2SBard Liao 	SOC_DOUBLE_TLV("DAC1 Playback Volume", RT5651_DAC1_DIG_VOL,
32840bc18a2SBard Liao 			RT5651_L_VOL_SFT, RT5651_R_VOL_SFT,
32940bc18a2SBard Liao 			175, 0, dac_vol_tlv),
33040bc18a2SBard Liao 	SOC_DOUBLE_TLV("Mono DAC Playback Volume", RT5651_DAC2_DIG_VOL,
33140bc18a2SBard Liao 			RT5651_L_VOL_SFT, RT5651_R_VOL_SFT,
33240bc18a2SBard Liao 			175, 0, dac_vol_tlv),
333eea16625SHans de Goede 	/* IN1/IN2/IN3 Control */
33440bc18a2SBard Liao 	SOC_SINGLE_TLV("IN1 Boost", RT5651_IN1_IN2,
33540bc18a2SBard Liao 		RT5651_BST_SFT1, 8, 0, bst_tlv),
33640bc18a2SBard Liao 	SOC_SINGLE_TLV("IN2 Boost", RT5651_IN1_IN2,
33740bc18a2SBard Liao 		RT5651_BST_SFT2, 8, 0, bst_tlv),
338eea16625SHans de Goede 	SOC_SINGLE_TLV("IN3 Boost", RT5651_IN3,
339eea16625SHans de Goede 		RT5651_BST_SFT1, 8, 0, bst_tlv),
34040bc18a2SBard Liao 	/* INL/INR Volume Control */
34140bc18a2SBard Liao 	SOC_DOUBLE_TLV("IN Capture Volume", RT5651_INL1_INR1_VOL,
34240bc18a2SBard Liao 			RT5651_INL_VOL_SFT, RT5651_INR_VOL_SFT,
34340bc18a2SBard Liao 			31, 1, in_vol_tlv),
34440bc18a2SBard Liao 	/* ADC Digital Volume Control */
34540bc18a2SBard Liao 	SOC_DOUBLE("ADC Capture Switch", RT5651_ADC_DIG_VOL,
34640bc18a2SBard Liao 		RT5651_L_MUTE_SFT, RT5651_R_MUTE_SFT, 1, 1),
34740bc18a2SBard Liao 	SOC_DOUBLE_TLV("ADC Capture Volume", RT5651_ADC_DIG_VOL,
34840bc18a2SBard Liao 			RT5651_L_VOL_SFT, RT5651_R_VOL_SFT,
34940bc18a2SBard Liao 			127, 0, adc_vol_tlv),
35040bc18a2SBard Liao 	SOC_DOUBLE_TLV("Mono ADC Capture Volume", RT5651_ADC_DATA,
35140bc18a2SBard Liao 			RT5651_L_VOL_SFT, RT5651_R_VOL_SFT,
35240bc18a2SBard Liao 			127, 0, adc_vol_tlv),
35340bc18a2SBard Liao 	/* ADC Boost Volume Control */
35440bc18a2SBard Liao 	SOC_DOUBLE_TLV("ADC Boost Gain", RT5651_ADC_BST_VOL,
35540bc18a2SBard Liao 			RT5651_ADC_L_BST_SFT, RT5651_ADC_R_BST_SFT,
35640bc18a2SBard Liao 			3, 0, adc_bst_tlv),
35740bc18a2SBard Liao 
35840bc18a2SBard Liao 	/* ASRC */
35940bc18a2SBard Liao 	SOC_SINGLE("IF1 ASRC Switch", RT5651_PLL_MODE_1,
36040bc18a2SBard Liao 		RT5651_STO1_T_SFT, 1, 0),
36140bc18a2SBard Liao 	SOC_SINGLE("IF2 ASRC Switch", RT5651_PLL_MODE_1,
36240bc18a2SBard Liao 		RT5651_STO2_T_SFT, 1, 0),
36340bc18a2SBard Liao 	SOC_SINGLE("DMIC ASRC Switch", RT5651_PLL_MODE_1,
36440bc18a2SBard Liao 		RT5651_DMIC_1_M_SFT, 1, 0),
36540bc18a2SBard Liao 
36640bc18a2SBard Liao 	SOC_ENUM("ADC IF2 Data Switch", rt5651_if2_adc_enum),
36740bc18a2SBard Liao 	SOC_ENUM("DAC IF2 Data Switch", rt5651_if2_dac_enum),
36840bc18a2SBard Liao };
36940bc18a2SBard Liao 
37040bc18a2SBard Liao /**
37140bc18a2SBard Liao  * set_dmic_clk - Set parameter of dmic.
37240bc18a2SBard Liao  *
37340bc18a2SBard Liao  * @w: DAPM widget.
37440bc18a2SBard Liao  * @kcontrol: The kcontrol of this widget.
37540bc18a2SBard Liao  * @event: Event id.
37640bc18a2SBard Liao  *
37740bc18a2SBard Liao  */
37840bc18a2SBard Liao static int set_dmic_clk(struct snd_soc_dapm_widget *w,
37940bc18a2SBard Liao 	struct snd_kcontrol *kcontrol, int event)
38040bc18a2SBard Liao {
38117b52010SKuninori Morimoto 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
38217b52010SKuninori Morimoto 	struct rt5651_priv *rt5651 = snd_soc_component_get_drvdata(component);
38300a6d6e5SOder Chiou 	int idx, rate;
38440bc18a2SBard Liao 
38500a6d6e5SOder Chiou 	rate = rt5651->sysclk / rl6231_get_pre_div(rt5651->regmap,
38600a6d6e5SOder Chiou 		RT5651_ADDA_CLK1, RT5651_I2S_PD1_SFT);
38700a6d6e5SOder Chiou 	idx = rl6231_calc_dmic_clk(rate);
38840bc18a2SBard Liao 	if (idx < 0)
38917b52010SKuninori Morimoto 		dev_err(component->dev, "Failed to set DMIC clock\n");
39040bc18a2SBard Liao 	else
39117b52010SKuninori Morimoto 		snd_soc_component_update_bits(component, RT5651_DMIC, RT5651_DMIC_CLK_MASK,
39240bc18a2SBard Liao 					idx << RT5651_DMIC_CLK_SFT);
39340bc18a2SBard Liao 
39440bc18a2SBard Liao 	return idx;
39540bc18a2SBard Liao }
39640bc18a2SBard Liao 
39740bc18a2SBard Liao /* Digital Mixer */
39840bc18a2SBard Liao static const struct snd_kcontrol_new rt5651_sto1_adc_l_mix[] = {
39940bc18a2SBard Liao 	SOC_DAPM_SINGLE("ADC1 Switch", RT5651_STO1_ADC_MIXER,
40040bc18a2SBard Liao 			RT5651_M_STO1_ADC_L1_SFT, 1, 1),
40140bc18a2SBard Liao 	SOC_DAPM_SINGLE("ADC2 Switch", RT5651_STO1_ADC_MIXER,
40240bc18a2SBard Liao 			RT5651_M_STO1_ADC_L2_SFT, 1, 1),
40340bc18a2SBard Liao };
40440bc18a2SBard Liao 
40540bc18a2SBard Liao static const struct snd_kcontrol_new rt5651_sto1_adc_r_mix[] = {
40640bc18a2SBard Liao 	SOC_DAPM_SINGLE("ADC1 Switch", RT5651_STO1_ADC_MIXER,
40740bc18a2SBard Liao 			RT5651_M_STO1_ADC_R1_SFT, 1, 1),
40840bc18a2SBard Liao 	SOC_DAPM_SINGLE("ADC2 Switch", RT5651_STO1_ADC_MIXER,
40940bc18a2SBard Liao 			RT5651_M_STO1_ADC_R2_SFT, 1, 1),
41040bc18a2SBard Liao };
41140bc18a2SBard Liao 
41240bc18a2SBard Liao static const struct snd_kcontrol_new rt5651_sto2_adc_l_mix[] = {
41340bc18a2SBard Liao 	SOC_DAPM_SINGLE("ADC1 Switch", RT5651_STO2_ADC_MIXER,
41440bc18a2SBard Liao 			RT5651_M_STO2_ADC_L1_SFT, 1, 1),
41540bc18a2SBard Liao 	SOC_DAPM_SINGLE("ADC2 Switch", RT5651_STO2_ADC_MIXER,
41640bc18a2SBard Liao 			RT5651_M_STO2_ADC_L2_SFT, 1, 1),
41740bc18a2SBard Liao };
41840bc18a2SBard Liao 
41940bc18a2SBard Liao static const struct snd_kcontrol_new rt5651_sto2_adc_r_mix[] = {
42040bc18a2SBard Liao 	SOC_DAPM_SINGLE("ADC1 Switch", RT5651_STO2_ADC_MIXER,
42140bc18a2SBard Liao 			RT5651_M_STO2_ADC_R1_SFT, 1, 1),
42240bc18a2SBard Liao 	SOC_DAPM_SINGLE("ADC2 Switch", RT5651_STO2_ADC_MIXER,
42340bc18a2SBard Liao 			RT5651_M_STO2_ADC_R2_SFT, 1, 1),
42440bc18a2SBard Liao };
42540bc18a2SBard Liao 
42640bc18a2SBard Liao static const struct snd_kcontrol_new rt5651_dac_l_mix[] = {
42740bc18a2SBard Liao 	SOC_DAPM_SINGLE("Stereo ADC Switch", RT5651_AD_DA_MIXER,
42840bc18a2SBard Liao 			RT5651_M_ADCMIX_L_SFT, 1, 1),
42940bc18a2SBard Liao 	SOC_DAPM_SINGLE("INF1 Switch", RT5651_AD_DA_MIXER,
43040bc18a2SBard Liao 			RT5651_M_IF1_DAC_L_SFT, 1, 1),
43140bc18a2SBard Liao };
43240bc18a2SBard Liao 
43340bc18a2SBard Liao static const struct snd_kcontrol_new rt5651_dac_r_mix[] = {
43440bc18a2SBard Liao 	SOC_DAPM_SINGLE("Stereo ADC Switch", RT5651_AD_DA_MIXER,
43540bc18a2SBard Liao 			RT5651_M_ADCMIX_R_SFT, 1, 1),
43640bc18a2SBard Liao 	SOC_DAPM_SINGLE("INF1 Switch", RT5651_AD_DA_MIXER,
43740bc18a2SBard Liao 			RT5651_M_IF1_DAC_R_SFT, 1, 1),
43840bc18a2SBard Liao };
43940bc18a2SBard Liao 
44040bc18a2SBard Liao static const struct snd_kcontrol_new rt5651_sto_dac_l_mix[] = {
44140bc18a2SBard Liao 	SOC_DAPM_SINGLE("DAC L1 Switch", RT5651_STO_DAC_MIXER,
44240bc18a2SBard Liao 			RT5651_M_DAC_L1_MIXL_SFT, 1, 1),
44340bc18a2SBard Liao 	SOC_DAPM_SINGLE("DAC L2 Switch", RT5651_STO_DAC_MIXER,
44440bc18a2SBard Liao 			RT5651_M_DAC_L2_MIXL_SFT, 1, 1),
44540bc18a2SBard Liao 	SOC_DAPM_SINGLE("DAC R1 Switch", RT5651_STO_DAC_MIXER,
44640bc18a2SBard Liao 			RT5651_M_DAC_R1_MIXL_SFT, 1, 1),
44740bc18a2SBard Liao };
44840bc18a2SBard Liao 
44940bc18a2SBard Liao static const struct snd_kcontrol_new rt5651_sto_dac_r_mix[] = {
45040bc18a2SBard Liao 	SOC_DAPM_SINGLE("DAC R1 Switch", RT5651_STO_DAC_MIXER,
45140bc18a2SBard Liao 			RT5651_M_DAC_R1_MIXR_SFT, 1, 1),
45240bc18a2SBard Liao 	SOC_DAPM_SINGLE("DAC R2 Switch", RT5651_STO_DAC_MIXER,
45340bc18a2SBard Liao 			RT5651_M_DAC_R2_MIXR_SFT, 1, 1),
45440bc18a2SBard Liao 	SOC_DAPM_SINGLE("DAC L1 Switch", RT5651_STO_DAC_MIXER,
45540bc18a2SBard Liao 			RT5651_M_DAC_L1_MIXR_SFT, 1, 1),
45640bc18a2SBard Liao };
45740bc18a2SBard Liao 
45840bc18a2SBard Liao static const struct snd_kcontrol_new rt5651_dd_dac_l_mix[] = {
45940bc18a2SBard Liao 	SOC_DAPM_SINGLE("DAC L1 Switch", RT5651_DD_MIXER,
46040bc18a2SBard Liao 			RT5651_M_STO_DD_L1_SFT, 1, 1),
46140bc18a2SBard Liao 	SOC_DAPM_SINGLE("DAC L2 Switch", RT5651_DD_MIXER,
46240bc18a2SBard Liao 			RT5651_M_STO_DD_L2_SFT, 1, 1),
46340bc18a2SBard Liao 	SOC_DAPM_SINGLE("DAC R2 Switch", RT5651_DD_MIXER,
46440bc18a2SBard Liao 			RT5651_M_STO_DD_R2_L_SFT, 1, 1),
46540bc18a2SBard Liao };
46640bc18a2SBard Liao 
46740bc18a2SBard Liao static const struct snd_kcontrol_new rt5651_dd_dac_r_mix[] = {
46840bc18a2SBard Liao 	SOC_DAPM_SINGLE("DAC R1 Switch", RT5651_DD_MIXER,
46940bc18a2SBard Liao 			RT5651_M_STO_DD_R1_SFT, 1, 1),
47040bc18a2SBard Liao 	SOC_DAPM_SINGLE("DAC R2 Switch", RT5651_DD_MIXER,
47140bc18a2SBard Liao 			RT5651_M_STO_DD_R2_SFT, 1, 1),
47240bc18a2SBard Liao 	SOC_DAPM_SINGLE("DAC L2 Switch", RT5651_DD_MIXER,
47340bc18a2SBard Liao 			RT5651_M_STO_DD_L2_R_SFT, 1, 1),
47440bc18a2SBard Liao };
47540bc18a2SBard Liao 
47640bc18a2SBard Liao /* Analog Input Mixer */
47740bc18a2SBard Liao static const struct snd_kcontrol_new rt5651_rec_l_mix[] = {
47840bc18a2SBard Liao 	SOC_DAPM_SINGLE("INL1 Switch", RT5651_REC_L2_MIXER,
47940bc18a2SBard Liao 			RT5651_M_IN1_L_RM_L_SFT, 1, 1),
48040bc18a2SBard Liao 	SOC_DAPM_SINGLE("BST3 Switch", RT5651_REC_L2_MIXER,
48140bc18a2SBard Liao 			RT5651_M_BST3_RM_L_SFT, 1, 1),
48240bc18a2SBard Liao 	SOC_DAPM_SINGLE("BST2 Switch", RT5651_REC_L2_MIXER,
48340bc18a2SBard Liao 			RT5651_M_BST2_RM_L_SFT, 1, 1),
48440bc18a2SBard Liao 	SOC_DAPM_SINGLE("BST1 Switch", RT5651_REC_L2_MIXER,
48540bc18a2SBard Liao 			RT5651_M_BST1_RM_L_SFT, 1, 1),
48640bc18a2SBard Liao };
48740bc18a2SBard Liao 
48840bc18a2SBard Liao static const struct snd_kcontrol_new rt5651_rec_r_mix[] = {
48940bc18a2SBard Liao 	SOC_DAPM_SINGLE("INR1 Switch", RT5651_REC_R2_MIXER,
49040bc18a2SBard Liao 			RT5651_M_IN1_R_RM_R_SFT, 1, 1),
49140bc18a2SBard Liao 	SOC_DAPM_SINGLE("BST3 Switch", RT5651_REC_R2_MIXER,
49240bc18a2SBard Liao 			RT5651_M_BST3_RM_R_SFT, 1, 1),
49340bc18a2SBard Liao 	SOC_DAPM_SINGLE("BST2 Switch", RT5651_REC_R2_MIXER,
49440bc18a2SBard Liao 			RT5651_M_BST2_RM_R_SFT, 1, 1),
49540bc18a2SBard Liao 	SOC_DAPM_SINGLE("BST1 Switch", RT5651_REC_R2_MIXER,
49640bc18a2SBard Liao 			RT5651_M_BST1_RM_R_SFT, 1, 1),
49740bc18a2SBard Liao };
49840bc18a2SBard Liao 
49940bc18a2SBard Liao /* Analog Output Mixer */
50040bc18a2SBard Liao 
50140bc18a2SBard Liao static const struct snd_kcontrol_new rt5651_out_l_mix[] = {
50240bc18a2SBard Liao 	SOC_DAPM_SINGLE("BST1 Switch", RT5651_OUT_L3_MIXER,
50340bc18a2SBard Liao 			RT5651_M_BST1_OM_L_SFT, 1, 1),
50440bc18a2SBard Liao 	SOC_DAPM_SINGLE("BST2 Switch", RT5651_OUT_L3_MIXER,
50540bc18a2SBard Liao 			RT5651_M_BST2_OM_L_SFT, 1, 1),
50640bc18a2SBard Liao 	SOC_DAPM_SINGLE("INL1 Switch", RT5651_OUT_L3_MIXER,
50740bc18a2SBard Liao 			RT5651_M_IN1_L_OM_L_SFT, 1, 1),
50840bc18a2SBard Liao 	SOC_DAPM_SINGLE("REC MIXL Switch", RT5651_OUT_L3_MIXER,
50940bc18a2SBard Liao 			RT5651_M_RM_L_OM_L_SFT, 1, 1),
51040bc18a2SBard Liao 	SOC_DAPM_SINGLE("DAC L1 Switch", RT5651_OUT_L3_MIXER,
51140bc18a2SBard Liao 			RT5651_M_DAC_L1_OM_L_SFT, 1, 1),
51240bc18a2SBard Liao };
51340bc18a2SBard Liao 
51440bc18a2SBard Liao static const struct snd_kcontrol_new rt5651_out_r_mix[] = {
51540bc18a2SBard Liao 	SOC_DAPM_SINGLE("BST2 Switch", RT5651_OUT_R3_MIXER,
51640bc18a2SBard Liao 			RT5651_M_BST2_OM_R_SFT, 1, 1),
51740bc18a2SBard Liao 	SOC_DAPM_SINGLE("BST1 Switch", RT5651_OUT_R3_MIXER,
51840bc18a2SBard Liao 			RT5651_M_BST1_OM_R_SFT, 1, 1),
51940bc18a2SBard Liao 	SOC_DAPM_SINGLE("INR1 Switch", RT5651_OUT_R3_MIXER,
52040bc18a2SBard Liao 			RT5651_M_IN1_R_OM_R_SFT, 1, 1),
52140bc18a2SBard Liao 	SOC_DAPM_SINGLE("REC MIXR Switch", RT5651_OUT_R3_MIXER,
52240bc18a2SBard Liao 			RT5651_M_RM_R_OM_R_SFT, 1, 1),
52340bc18a2SBard Liao 	SOC_DAPM_SINGLE("DAC R1 Switch", RT5651_OUT_R3_MIXER,
52440bc18a2SBard Liao 			RT5651_M_DAC_R1_OM_R_SFT, 1, 1),
52540bc18a2SBard Liao };
52640bc18a2SBard Liao 
52740bc18a2SBard Liao static const struct snd_kcontrol_new rt5651_hpo_mix[] = {
52840bc18a2SBard Liao 	SOC_DAPM_SINGLE("HPO MIX DAC1 Switch", RT5651_HPO_MIXER,
52940bc18a2SBard Liao 			RT5651_M_DAC1_HM_SFT, 1, 1),
53040bc18a2SBard Liao 	SOC_DAPM_SINGLE("HPO MIX HPVOL Switch", RT5651_HPO_MIXER,
53140bc18a2SBard Liao 			RT5651_M_HPVOL_HM_SFT, 1, 1),
53240bc18a2SBard Liao };
53340bc18a2SBard Liao 
53440bc18a2SBard Liao static const struct snd_kcontrol_new rt5651_lout_mix[] = {
53540bc18a2SBard Liao 	SOC_DAPM_SINGLE("DAC L1 Switch", RT5651_LOUT_MIXER,
53640bc18a2SBard Liao 			RT5651_M_DAC_L1_LM_SFT, 1, 1),
53740bc18a2SBard Liao 	SOC_DAPM_SINGLE("DAC R1 Switch", RT5651_LOUT_MIXER,
53840bc18a2SBard Liao 			RT5651_M_DAC_R1_LM_SFT, 1, 1),
53940bc18a2SBard Liao 	SOC_DAPM_SINGLE("OUTVOL L Switch", RT5651_LOUT_MIXER,
54040bc18a2SBard Liao 			RT5651_M_OV_L_LM_SFT, 1, 1),
54140bc18a2SBard Liao 	SOC_DAPM_SINGLE("OUTVOL R Switch", RT5651_LOUT_MIXER,
54240bc18a2SBard Liao 			RT5651_M_OV_R_LM_SFT, 1, 1),
54340bc18a2SBard Liao };
54440bc18a2SBard Liao 
54540bc18a2SBard Liao static const struct snd_kcontrol_new outvol_l_control =
54640bc18a2SBard Liao 	SOC_DAPM_SINGLE("Switch", RT5651_LOUT_CTRL1,
54740bc18a2SBard Liao 			RT5651_VOL_L_SFT, 1, 1);
54840bc18a2SBard Liao 
54940bc18a2SBard Liao static const struct snd_kcontrol_new outvol_r_control =
55040bc18a2SBard Liao 	SOC_DAPM_SINGLE("Switch", RT5651_LOUT_CTRL1,
55140bc18a2SBard Liao 			RT5651_VOL_R_SFT, 1, 1);
55240bc18a2SBard Liao 
55340bc18a2SBard Liao static const struct snd_kcontrol_new lout_l_mute_control =
55440bc18a2SBard Liao 	SOC_DAPM_SINGLE_AUTODISABLE("Switch", RT5651_LOUT_CTRL1,
55540bc18a2SBard Liao 				    RT5651_L_MUTE_SFT, 1, 1);
55640bc18a2SBard Liao 
55740bc18a2SBard Liao static const struct snd_kcontrol_new lout_r_mute_control =
55840bc18a2SBard Liao 	SOC_DAPM_SINGLE_AUTODISABLE("Switch", RT5651_LOUT_CTRL1,
55940bc18a2SBard Liao 				    RT5651_R_MUTE_SFT, 1, 1);
56040bc18a2SBard Liao 
56140bc18a2SBard Liao static const struct snd_kcontrol_new hpovol_l_control =
56240bc18a2SBard Liao 	SOC_DAPM_SINGLE("Switch", RT5651_HP_VOL,
56340bc18a2SBard Liao 			RT5651_VOL_L_SFT, 1, 1);
56440bc18a2SBard Liao 
56540bc18a2SBard Liao static const struct snd_kcontrol_new hpovol_r_control =
56640bc18a2SBard Liao 	SOC_DAPM_SINGLE("Switch", RT5651_HP_VOL,
56740bc18a2SBard Liao 			RT5651_VOL_R_SFT, 1, 1);
56840bc18a2SBard Liao 
56940bc18a2SBard Liao static const struct snd_kcontrol_new hpo_l_mute_control =
57040bc18a2SBard Liao 	SOC_DAPM_SINGLE_AUTODISABLE("Switch", RT5651_HP_VOL,
57140bc18a2SBard Liao 				    RT5651_L_MUTE_SFT, 1, 1);
57240bc18a2SBard Liao 
57340bc18a2SBard Liao static const struct snd_kcontrol_new hpo_r_mute_control =
57440bc18a2SBard Liao 	SOC_DAPM_SINGLE_AUTODISABLE("Switch", RT5651_HP_VOL,
57540bc18a2SBard Liao 				    RT5651_R_MUTE_SFT, 1, 1);
57640bc18a2SBard Liao 
57740bc18a2SBard Liao /* Stereo ADC source */
57840bc18a2SBard Liao static const char * const rt5651_stereo1_adc1_src[] = {"DD MIX", "ADC"};
57940bc18a2SBard Liao 
58040bc18a2SBard Liao static SOC_ENUM_SINGLE_DECL(
58140bc18a2SBard Liao 	rt5651_stereo1_adc1_enum, RT5651_STO1_ADC_MIXER,
58240bc18a2SBard Liao 	RT5651_STO1_ADC_1_SRC_SFT, rt5651_stereo1_adc1_src);
58340bc18a2SBard Liao 
58440bc18a2SBard Liao static const struct snd_kcontrol_new rt5651_sto1_adc_l1_mux =
58540bc18a2SBard Liao 	SOC_DAPM_ENUM("Stereo1 ADC L1 source", rt5651_stereo1_adc1_enum);
58640bc18a2SBard Liao 
58740bc18a2SBard Liao static const struct snd_kcontrol_new rt5651_sto1_adc_r1_mux =
58840bc18a2SBard Liao 	SOC_DAPM_ENUM("Stereo1 ADC R1 source", rt5651_stereo1_adc1_enum);
58940bc18a2SBard Liao 
59040bc18a2SBard Liao static const char * const rt5651_stereo1_adc2_src[] = {"DMIC", "DD MIX"};
59140bc18a2SBard Liao 
59240bc18a2SBard Liao static SOC_ENUM_SINGLE_DECL(
59340bc18a2SBard Liao 	rt5651_stereo1_adc2_enum, RT5651_STO1_ADC_MIXER,
59440bc18a2SBard Liao 	RT5651_STO1_ADC_2_SRC_SFT, rt5651_stereo1_adc2_src);
59540bc18a2SBard Liao 
59640bc18a2SBard Liao static const struct snd_kcontrol_new rt5651_sto1_adc_l2_mux =
59740bc18a2SBard Liao 	SOC_DAPM_ENUM("Stereo1 ADC L2 source", rt5651_stereo1_adc2_enum);
59840bc18a2SBard Liao 
59940bc18a2SBard Liao static const struct snd_kcontrol_new rt5651_sto1_adc_r2_mux =
60040bc18a2SBard Liao 	SOC_DAPM_ENUM("Stereo1 ADC R2 source", rt5651_stereo1_adc2_enum);
60140bc18a2SBard Liao 
60240bc18a2SBard Liao /* Mono ADC source */
60340bc18a2SBard Liao static const char * const rt5651_sto2_adc_l1_src[] = {"DD MIXL", "ADCL"};
60440bc18a2SBard Liao 
60540bc18a2SBard Liao static SOC_ENUM_SINGLE_DECL(
60640bc18a2SBard Liao 	rt5651_sto2_adc_l1_enum, RT5651_STO1_ADC_MIXER,
60740bc18a2SBard Liao 	RT5651_STO2_ADC_L1_SRC_SFT, rt5651_sto2_adc_l1_src);
60840bc18a2SBard Liao 
60940bc18a2SBard Liao static const struct snd_kcontrol_new rt5651_sto2_adc_l1_mux =
61040bc18a2SBard Liao 	SOC_DAPM_ENUM("Stereo2 ADC1 left source", rt5651_sto2_adc_l1_enum);
61140bc18a2SBard Liao 
61240bc18a2SBard Liao static const char * const rt5651_sto2_adc_l2_src[] = {"DMIC L", "DD MIXL"};
61340bc18a2SBard Liao 
61440bc18a2SBard Liao static SOC_ENUM_SINGLE_DECL(
61540bc18a2SBard Liao 	rt5651_sto2_adc_l2_enum, RT5651_STO1_ADC_MIXER,
61640bc18a2SBard Liao 	RT5651_STO2_ADC_L2_SRC_SFT, rt5651_sto2_adc_l2_src);
61740bc18a2SBard Liao 
61840bc18a2SBard Liao static const struct snd_kcontrol_new rt5651_sto2_adc_l2_mux =
61940bc18a2SBard Liao 	SOC_DAPM_ENUM("Stereo2 ADC2 left source", rt5651_sto2_adc_l2_enum);
62040bc18a2SBard Liao 
62140bc18a2SBard Liao static const char * const rt5651_sto2_adc_r1_src[] = {"DD MIXR", "ADCR"};
62240bc18a2SBard Liao 
62340bc18a2SBard Liao static SOC_ENUM_SINGLE_DECL(
62440bc18a2SBard Liao 	rt5651_sto2_adc_r1_enum, RT5651_STO1_ADC_MIXER,
62540bc18a2SBard Liao 	RT5651_STO2_ADC_R1_SRC_SFT, rt5651_sto2_adc_r1_src);
62640bc18a2SBard Liao 
62740bc18a2SBard Liao static const struct snd_kcontrol_new rt5651_sto2_adc_r1_mux =
62840bc18a2SBard Liao 	SOC_DAPM_ENUM("Stereo2 ADC1 right source", rt5651_sto2_adc_r1_enum);
62940bc18a2SBard Liao 
63040bc18a2SBard Liao static const char * const rt5651_sto2_adc_r2_src[] = {"DMIC R", "DD MIXR"};
63140bc18a2SBard Liao 
63240bc18a2SBard Liao static SOC_ENUM_SINGLE_DECL(
63340bc18a2SBard Liao 	rt5651_sto2_adc_r2_enum, RT5651_STO1_ADC_MIXER,
63440bc18a2SBard Liao 	RT5651_STO2_ADC_R2_SRC_SFT, rt5651_sto2_adc_r2_src);
63540bc18a2SBard Liao 
63640bc18a2SBard Liao static const struct snd_kcontrol_new rt5651_sto2_adc_r2_mux =
63740bc18a2SBard Liao 	SOC_DAPM_ENUM("Stereo2 ADC2 right source", rt5651_sto2_adc_r2_enum);
63840bc18a2SBard Liao 
63940bc18a2SBard Liao /* DAC2 channel source */
64040bc18a2SBard Liao 
64140bc18a2SBard Liao static const char * const rt5651_dac_src[] = {"IF1", "IF2"};
64240bc18a2SBard Liao 
64340bc18a2SBard Liao static SOC_ENUM_SINGLE_DECL(rt5651_dac_l2_enum, RT5651_DAC2_CTRL,
64440bc18a2SBard Liao 				RT5651_SEL_DAC_L2_SFT, rt5651_dac_src);
64540bc18a2SBard Liao 
64640bc18a2SBard Liao static const struct snd_kcontrol_new rt5651_dac_l2_mux =
64740bc18a2SBard Liao 	SOC_DAPM_ENUM("DAC2 left channel source", rt5651_dac_l2_enum);
64840bc18a2SBard Liao 
64940bc18a2SBard Liao static SOC_ENUM_SINGLE_DECL(
65040bc18a2SBard Liao 	rt5651_dac_r2_enum, RT5651_DAC2_CTRL,
65140bc18a2SBard Liao 	RT5651_SEL_DAC_R2_SFT, rt5651_dac_src);
65240bc18a2SBard Liao 
65340bc18a2SBard Liao static const struct snd_kcontrol_new rt5651_dac_r2_mux =
65440bc18a2SBard Liao 	SOC_DAPM_ENUM("DAC2 right channel source", rt5651_dac_r2_enum);
65540bc18a2SBard Liao 
65640bc18a2SBard Liao /* IF2_ADC channel source */
65740bc18a2SBard Liao 
65840bc18a2SBard Liao static const char * const rt5651_adc_src[] = {"IF1 ADC1", "IF1 ADC2"};
65940bc18a2SBard Liao 
66040bc18a2SBard Liao static SOC_ENUM_SINGLE_DECL(rt5651_if2_adc_src_enum, RT5651_DIG_INF_DATA,
66140bc18a2SBard Liao 				RT5651_IF2_ADC_SRC_SFT, rt5651_adc_src);
66240bc18a2SBard Liao 
66340bc18a2SBard Liao static const struct snd_kcontrol_new rt5651_if2_adc_src_mux =
66440bc18a2SBard Liao 	SOC_DAPM_ENUM("IF2 ADC channel source", rt5651_if2_adc_src_enum);
66540bc18a2SBard Liao 
66640bc18a2SBard Liao /* PDM select */
66740bc18a2SBard Liao static const char * const rt5651_pdm_sel[] = {"DD MIX", "Stereo DAC MIX"};
66840bc18a2SBard Liao 
66940bc18a2SBard Liao static SOC_ENUM_SINGLE_DECL(
67040bc18a2SBard Liao 	rt5651_pdm_l_sel_enum, RT5651_PDM_CTL,
67140bc18a2SBard Liao 	RT5651_PDM_L_SEL_SFT, rt5651_pdm_sel);
67240bc18a2SBard Liao 
67340bc18a2SBard Liao static SOC_ENUM_SINGLE_DECL(
67440bc18a2SBard Liao 	rt5651_pdm_r_sel_enum, RT5651_PDM_CTL,
67540bc18a2SBard Liao 	RT5651_PDM_R_SEL_SFT, rt5651_pdm_sel);
67640bc18a2SBard Liao 
67740bc18a2SBard Liao static const struct snd_kcontrol_new rt5651_pdm_l_mux =
67840bc18a2SBard Liao 	SOC_DAPM_ENUM("PDM L select", rt5651_pdm_l_sel_enum);
67940bc18a2SBard Liao 
68040bc18a2SBard Liao static const struct snd_kcontrol_new rt5651_pdm_r_mux =
68140bc18a2SBard Liao 	SOC_DAPM_ENUM("PDM R select", rt5651_pdm_r_sel_enum);
68240bc18a2SBard Liao 
68340bc18a2SBard Liao static int rt5651_amp_power_event(struct snd_soc_dapm_widget *w,
68440bc18a2SBard Liao 	struct snd_kcontrol *kcontrol, int event)
68540bc18a2SBard Liao {
68617b52010SKuninori Morimoto 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
68717b52010SKuninori Morimoto 	struct rt5651_priv *rt5651 = snd_soc_component_get_drvdata(component);
68840bc18a2SBard Liao 
68940bc18a2SBard Liao 	switch (event) {
69040bc18a2SBard Liao 	case SND_SOC_DAPM_POST_PMU:
69140bc18a2SBard Liao 		/* depop parameters */
69240bc18a2SBard Liao 		regmap_update_bits(rt5651->regmap, RT5651_PR_BASE +
69340bc18a2SBard Liao 			RT5651_CHPUMP_INT_REG1, 0x0700, 0x0200);
69440bc18a2SBard Liao 		regmap_update_bits(rt5651->regmap, RT5651_DEPOP_M2,
69540bc18a2SBard Liao 			RT5651_DEPOP_MASK, RT5651_DEPOP_MAN);
69640bc18a2SBard Liao 		regmap_update_bits(rt5651->regmap, RT5651_DEPOP_M1,
69740bc18a2SBard Liao 			RT5651_HP_CP_MASK | RT5651_HP_SG_MASK |
69840bc18a2SBard Liao 			RT5651_HP_CB_MASK, RT5651_HP_CP_PU |
69940bc18a2SBard Liao 			RT5651_HP_SG_DIS | RT5651_HP_CB_PU);
70040bc18a2SBard Liao 		regmap_write(rt5651->regmap, RT5651_PR_BASE +
70140bc18a2SBard Liao 				RT5651_HP_DCC_INT1, 0x9f00);
70240bc18a2SBard Liao 		/* headphone amp power on */
70340bc18a2SBard Liao 		regmap_update_bits(rt5651->regmap, RT5651_PWR_ANLG1,
70440bc18a2SBard Liao 			RT5651_PWR_FV1 | RT5651_PWR_FV2, 0);
70540bc18a2SBard Liao 		regmap_update_bits(rt5651->regmap, RT5651_PWR_ANLG1,
70640bc18a2SBard Liao 			RT5651_PWR_HA,
70740bc18a2SBard Liao 			RT5651_PWR_HA);
70840bc18a2SBard Liao 		usleep_range(10000, 15000);
70940bc18a2SBard Liao 		regmap_update_bits(rt5651->regmap, RT5651_PWR_ANLG1,
71040bc18a2SBard Liao 			RT5651_PWR_FV1 | RT5651_PWR_FV2 ,
71140bc18a2SBard Liao 			RT5651_PWR_FV1 | RT5651_PWR_FV2);
71240bc18a2SBard Liao 		break;
71340bc18a2SBard Liao 
71440bc18a2SBard Liao 	default:
71540bc18a2SBard Liao 		return 0;
71640bc18a2SBard Liao 	}
71740bc18a2SBard Liao 
71840bc18a2SBard Liao 	return 0;
71940bc18a2SBard Liao }
72040bc18a2SBard Liao 
72140bc18a2SBard Liao static int rt5651_hp_event(struct snd_soc_dapm_widget *w,
72240bc18a2SBard Liao 	struct snd_kcontrol *kcontrol, int event)
72340bc18a2SBard Liao {
72417b52010SKuninori Morimoto 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
72517b52010SKuninori Morimoto 	struct rt5651_priv *rt5651 = snd_soc_component_get_drvdata(component);
72640bc18a2SBard Liao 
72740bc18a2SBard Liao 	switch (event) {
72840bc18a2SBard Liao 	case SND_SOC_DAPM_POST_PMU:
72940bc18a2SBard Liao 		/* headphone unmute sequence */
73040bc18a2SBard Liao 		regmap_update_bits(rt5651->regmap, RT5651_DEPOP_M2,
73140bc18a2SBard Liao 			RT5651_DEPOP_MASK | RT5651_DIG_DP_MASK,
73240bc18a2SBard Liao 			RT5651_DEPOP_AUTO | RT5651_DIG_DP_EN);
73340bc18a2SBard Liao 		regmap_update_bits(rt5651->regmap, RT5651_CHARGE_PUMP,
73440bc18a2SBard Liao 			RT5651_PM_HP_MASK, RT5651_PM_HP_HV);
73540bc18a2SBard Liao 
73640bc18a2SBard Liao 		regmap_update_bits(rt5651->regmap, RT5651_DEPOP_M3,
73740bc18a2SBard Liao 			RT5651_CP_FQ1_MASK | RT5651_CP_FQ2_MASK |
73840bc18a2SBard Liao 			RT5651_CP_FQ3_MASK,
73940bc18a2SBard Liao 			(RT5651_CP_FQ_192_KHZ << RT5651_CP_FQ1_SFT) |
74040bc18a2SBard Liao 			(RT5651_CP_FQ_12_KHZ << RT5651_CP_FQ2_SFT) |
74140bc18a2SBard Liao 			(RT5651_CP_FQ_192_KHZ << RT5651_CP_FQ3_SFT));
74240bc18a2SBard Liao 
74340bc18a2SBard Liao 		regmap_write(rt5651->regmap, RT5651_PR_BASE +
74440bc18a2SBard Liao 			RT5651_MAMP_INT_REG2, 0x1c00);
74540bc18a2SBard Liao 		regmap_update_bits(rt5651->regmap, RT5651_DEPOP_M1,
74640bc18a2SBard Liao 			RT5651_HP_CP_MASK | RT5651_HP_SG_MASK,
74740bc18a2SBard Liao 			RT5651_HP_CP_PD | RT5651_HP_SG_EN);
74840bc18a2SBard Liao 		regmap_update_bits(rt5651->regmap, RT5651_PR_BASE +
74940bc18a2SBard Liao 			RT5651_CHPUMP_INT_REG1, 0x0700, 0x0400);
750577dc32fSPierre-Louis Bossart 		rt5651->hp_mute = false;
75140bc18a2SBard Liao 		break;
75240bc18a2SBard Liao 
75340bc18a2SBard Liao 	case SND_SOC_DAPM_PRE_PMD:
754577dc32fSPierre-Louis Bossart 		rt5651->hp_mute = true;
75540bc18a2SBard Liao 		usleep_range(70000, 75000);
75640bc18a2SBard Liao 		break;
75740bc18a2SBard Liao 
75840bc18a2SBard Liao 	default:
75940bc18a2SBard Liao 		return 0;
76040bc18a2SBard Liao 	}
76140bc18a2SBard Liao 
76240bc18a2SBard Liao 	return 0;
76340bc18a2SBard Liao }
76440bc18a2SBard Liao 
76540bc18a2SBard Liao static int rt5651_hp_post_event(struct snd_soc_dapm_widget *w,
76640bc18a2SBard Liao 			   struct snd_kcontrol *kcontrol, int event)
76740bc18a2SBard Liao {
76830c173edSLars-Peter Clausen 
76917b52010SKuninori Morimoto 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
77017b52010SKuninori Morimoto 	struct rt5651_priv *rt5651 = snd_soc_component_get_drvdata(component);
77140bc18a2SBard Liao 
77240bc18a2SBard Liao 	switch (event) {
77340bc18a2SBard Liao 	case SND_SOC_DAPM_POST_PMU:
77440bc18a2SBard Liao 		if (!rt5651->hp_mute)
77540bc18a2SBard Liao 			usleep_range(80000, 85000);
77640bc18a2SBard Liao 
77740bc18a2SBard Liao 		break;
77840bc18a2SBard Liao 
77940bc18a2SBard Liao 	default:
78040bc18a2SBard Liao 		return 0;
78140bc18a2SBard Liao 	}
78240bc18a2SBard Liao 
78340bc18a2SBard Liao 	return 0;
78440bc18a2SBard Liao }
78540bc18a2SBard Liao 
78640bc18a2SBard Liao static int rt5651_bst1_event(struct snd_soc_dapm_widget *w,
78740bc18a2SBard Liao 	struct snd_kcontrol *kcontrol, int event)
78840bc18a2SBard Liao {
78917b52010SKuninori Morimoto 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
79040bc18a2SBard Liao 
79140bc18a2SBard Liao 	switch (event) {
79240bc18a2SBard Liao 	case SND_SOC_DAPM_POST_PMU:
79317b52010SKuninori Morimoto 		snd_soc_component_update_bits(component, RT5651_PWR_ANLG2,
79440bc18a2SBard Liao 			RT5651_PWR_BST1_OP2, RT5651_PWR_BST1_OP2);
79540bc18a2SBard Liao 		break;
79640bc18a2SBard Liao 
79740bc18a2SBard Liao 	case SND_SOC_DAPM_PRE_PMD:
79817b52010SKuninori Morimoto 		snd_soc_component_update_bits(component, RT5651_PWR_ANLG2,
79940bc18a2SBard Liao 			RT5651_PWR_BST1_OP2, 0);
80040bc18a2SBard Liao 		break;
80140bc18a2SBard Liao 
80240bc18a2SBard Liao 	default:
80340bc18a2SBard Liao 		return 0;
80440bc18a2SBard Liao 	}
80540bc18a2SBard Liao 
80640bc18a2SBard Liao 	return 0;
80740bc18a2SBard Liao }
80840bc18a2SBard Liao 
80940bc18a2SBard Liao static int rt5651_bst2_event(struct snd_soc_dapm_widget *w,
81040bc18a2SBard Liao 	struct snd_kcontrol *kcontrol, int event)
81140bc18a2SBard Liao {
81217b52010SKuninori Morimoto 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
81340bc18a2SBard Liao 
81440bc18a2SBard Liao 	switch (event) {
81540bc18a2SBard Liao 	case SND_SOC_DAPM_POST_PMU:
81617b52010SKuninori Morimoto 		snd_soc_component_update_bits(component, RT5651_PWR_ANLG2,
81740bc18a2SBard Liao 			RT5651_PWR_BST2_OP2, RT5651_PWR_BST2_OP2);
81840bc18a2SBard Liao 		break;
81940bc18a2SBard Liao 
82040bc18a2SBard Liao 	case SND_SOC_DAPM_PRE_PMD:
82117b52010SKuninori Morimoto 		snd_soc_component_update_bits(component, RT5651_PWR_ANLG2,
82240bc18a2SBard Liao 			RT5651_PWR_BST2_OP2, 0);
82340bc18a2SBard Liao 		break;
82440bc18a2SBard Liao 
82540bc18a2SBard Liao 	default:
82640bc18a2SBard Liao 		return 0;
82740bc18a2SBard Liao 	}
82840bc18a2SBard Liao 
82940bc18a2SBard Liao 	return 0;
83040bc18a2SBard Liao }
83140bc18a2SBard Liao 
83240bc18a2SBard Liao static int rt5651_bst3_event(struct snd_soc_dapm_widget *w,
83340bc18a2SBard Liao 	struct snd_kcontrol *kcontrol, int event)
83440bc18a2SBard Liao {
83517b52010SKuninori Morimoto 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
83640bc18a2SBard Liao 
83740bc18a2SBard Liao 	switch (event) {
83840bc18a2SBard Liao 	case SND_SOC_DAPM_POST_PMU:
83917b52010SKuninori Morimoto 		snd_soc_component_update_bits(component, RT5651_PWR_ANLG2,
84040bc18a2SBard Liao 			RT5651_PWR_BST3_OP2, RT5651_PWR_BST3_OP2);
84140bc18a2SBard Liao 		break;
84240bc18a2SBard Liao 
84340bc18a2SBard Liao 	case SND_SOC_DAPM_PRE_PMD:
84417b52010SKuninori Morimoto 		snd_soc_component_update_bits(component, RT5651_PWR_ANLG2,
84540bc18a2SBard Liao 			RT5651_PWR_BST3_OP2, 0);
84640bc18a2SBard Liao 		break;
84740bc18a2SBard Liao 
84840bc18a2SBard Liao 	default:
84940bc18a2SBard Liao 		return 0;
85040bc18a2SBard Liao 	}
85140bc18a2SBard Liao 
85240bc18a2SBard Liao 	return 0;
85340bc18a2SBard Liao }
85440bc18a2SBard Liao 
85540bc18a2SBard Liao static const struct snd_soc_dapm_widget rt5651_dapm_widgets[] = {
85640bc18a2SBard Liao 	/* ASRC */
85740bc18a2SBard Liao 	SND_SOC_DAPM_SUPPLY_S("I2S1 ASRC", 1, RT5651_PLL_MODE_2,
85840bc18a2SBard Liao 			      15, 0, NULL, 0),
85940bc18a2SBard Liao 	SND_SOC_DAPM_SUPPLY_S("I2S2 ASRC", 1, RT5651_PLL_MODE_2,
86040bc18a2SBard Liao 			      14, 0, NULL, 0),
86140bc18a2SBard Liao 	SND_SOC_DAPM_SUPPLY_S("STO1 DAC ASRC", 1, RT5651_PLL_MODE_2,
86240bc18a2SBard Liao 			      13, 0, NULL, 0),
86340bc18a2SBard Liao 	SND_SOC_DAPM_SUPPLY_S("STO2 DAC ASRC", 1, RT5651_PLL_MODE_2,
86440bc18a2SBard Liao 			      12, 0, NULL, 0),
86540bc18a2SBard Liao 	SND_SOC_DAPM_SUPPLY_S("ADC ASRC", 1, RT5651_PLL_MODE_2,
86640bc18a2SBard Liao 			      11, 0, NULL, 0),
86740bc18a2SBard Liao 
86840bc18a2SBard Liao 	/* micbias */
86940bc18a2SBard Liao 	SND_SOC_DAPM_SUPPLY("LDO", RT5651_PWR_ANLG1,
87040bc18a2SBard Liao 			RT5651_PWR_LDO_BIT, 0, NULL, 0),
871be96fc54SCarlo Caione 	SND_SOC_DAPM_SUPPLY("micbias1", RT5651_PWR_ANLG2,
872be96fc54SCarlo Caione 			RT5651_PWR_MB1_BIT, 0, NULL, 0),
87340bc18a2SBard Liao 	/* Input Lines */
87440bc18a2SBard Liao 	SND_SOC_DAPM_INPUT("MIC1"),
87540bc18a2SBard Liao 	SND_SOC_DAPM_INPUT("MIC2"),
87640bc18a2SBard Liao 	SND_SOC_DAPM_INPUT("MIC3"),
87740bc18a2SBard Liao 
87840bc18a2SBard Liao 	SND_SOC_DAPM_INPUT("IN1P"),
87940bc18a2SBard Liao 	SND_SOC_DAPM_INPUT("IN2P"),
88040bc18a2SBard Liao 	SND_SOC_DAPM_INPUT("IN2N"),
88140bc18a2SBard Liao 	SND_SOC_DAPM_INPUT("IN3P"),
88240bc18a2SBard Liao 	SND_SOC_DAPM_INPUT("DMIC L1"),
88340bc18a2SBard Liao 	SND_SOC_DAPM_INPUT("DMIC R1"),
88440bc18a2SBard Liao 	SND_SOC_DAPM_SUPPLY("DMIC CLK", RT5651_DMIC, RT5651_DMIC_1_EN_SFT,
88540bc18a2SBard Liao 			    0, set_dmic_clk, SND_SOC_DAPM_PRE_PMU),
88640bc18a2SBard Liao 	/* Boost */
88740bc18a2SBard Liao 	SND_SOC_DAPM_PGA_E("BST1", RT5651_PWR_ANLG2,
88840bc18a2SBard Liao 		RT5651_PWR_BST1_BIT, 0, NULL, 0, rt5651_bst1_event,
88940bc18a2SBard Liao 		SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
89040bc18a2SBard Liao 	SND_SOC_DAPM_PGA_E("BST2", RT5651_PWR_ANLG2,
89140bc18a2SBard Liao 		RT5651_PWR_BST2_BIT, 0, NULL, 0, rt5651_bst2_event,
89240bc18a2SBard Liao 		SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
89340bc18a2SBard Liao 	SND_SOC_DAPM_PGA_E("BST3", RT5651_PWR_ANLG2,
89440bc18a2SBard Liao 		RT5651_PWR_BST3_BIT, 0, NULL, 0, rt5651_bst3_event,
89540bc18a2SBard Liao 		SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
89640bc18a2SBard Liao 	/* Input Volume */
89740bc18a2SBard Liao 	SND_SOC_DAPM_PGA("INL1 VOL", RT5651_PWR_VOL,
89840bc18a2SBard Liao 			 RT5651_PWR_IN1_L_BIT, 0, NULL, 0),
89940bc18a2SBard Liao 	SND_SOC_DAPM_PGA("INR1 VOL", RT5651_PWR_VOL,
90040bc18a2SBard Liao 			 RT5651_PWR_IN1_R_BIT, 0, NULL, 0),
90140bc18a2SBard Liao 	SND_SOC_DAPM_PGA("INL2 VOL", RT5651_PWR_VOL,
90240bc18a2SBard Liao 			 RT5651_PWR_IN2_L_BIT, 0, NULL, 0),
90340bc18a2SBard Liao 	SND_SOC_DAPM_PGA("INR2 VOL", RT5651_PWR_VOL,
90440bc18a2SBard Liao 			 RT5651_PWR_IN2_R_BIT, 0, NULL, 0),
9055800b697SBard Liao 
90640bc18a2SBard Liao 	/* REC Mixer */
90740bc18a2SBard Liao 	SND_SOC_DAPM_MIXER("RECMIXL", RT5651_PWR_MIXER, RT5651_PWR_RM_L_BIT, 0,
90840bc18a2SBard Liao 			   rt5651_rec_l_mix, ARRAY_SIZE(rt5651_rec_l_mix)),
90940bc18a2SBard Liao 	SND_SOC_DAPM_MIXER("RECMIXR", RT5651_PWR_MIXER, RT5651_PWR_RM_R_BIT, 0,
91040bc18a2SBard Liao 			   rt5651_rec_r_mix, ARRAY_SIZE(rt5651_rec_r_mix)),
91140bc18a2SBard Liao 	/* ADCs */
91240bc18a2SBard Liao 	SND_SOC_DAPM_ADC("ADC L", NULL, SND_SOC_NOPM, 0, 0),
91340bc18a2SBard Liao 	SND_SOC_DAPM_ADC("ADC R", NULL, SND_SOC_NOPM, 0, 0),
91440bc18a2SBard Liao 	SND_SOC_DAPM_SUPPLY("ADC L Power", RT5651_PWR_DIG1,
91540bc18a2SBard Liao 			    RT5651_PWR_ADC_L_BIT, 0, NULL, 0),
91640bc18a2SBard Liao 	SND_SOC_DAPM_SUPPLY("ADC R Power", RT5651_PWR_DIG1,
91740bc18a2SBard Liao 			    RT5651_PWR_ADC_R_BIT, 0, NULL, 0),
91840bc18a2SBard Liao 	/* ADC Mux */
91940bc18a2SBard Liao 	SND_SOC_DAPM_MUX("Stereo1 ADC L2 Mux", SND_SOC_NOPM, 0, 0,
92040bc18a2SBard Liao 			 &rt5651_sto1_adc_l2_mux),
92140bc18a2SBard Liao 	SND_SOC_DAPM_MUX("Stereo1 ADC R2 Mux", SND_SOC_NOPM, 0, 0,
92240bc18a2SBard Liao 			 &rt5651_sto1_adc_r2_mux),
92340bc18a2SBard Liao 	SND_SOC_DAPM_MUX("Stereo1 ADC L1 Mux", SND_SOC_NOPM, 0, 0,
92440bc18a2SBard Liao 			 &rt5651_sto1_adc_l1_mux),
92540bc18a2SBard Liao 	SND_SOC_DAPM_MUX("Stereo1 ADC R1 Mux", SND_SOC_NOPM, 0, 0,
92640bc18a2SBard Liao 			 &rt5651_sto1_adc_r1_mux),
92740bc18a2SBard Liao 	SND_SOC_DAPM_MUX("Stereo2 ADC L2 Mux", SND_SOC_NOPM, 0, 0,
92840bc18a2SBard Liao 			 &rt5651_sto2_adc_l2_mux),
92940bc18a2SBard Liao 	SND_SOC_DAPM_MUX("Stereo2 ADC L1 Mux", SND_SOC_NOPM, 0, 0,
93040bc18a2SBard Liao 			 &rt5651_sto2_adc_l1_mux),
93140bc18a2SBard Liao 	SND_SOC_DAPM_MUX("Stereo2 ADC R1 Mux", SND_SOC_NOPM, 0, 0,
93240bc18a2SBard Liao 			 &rt5651_sto2_adc_r1_mux),
93340bc18a2SBard Liao 	SND_SOC_DAPM_MUX("Stereo2 ADC R2 Mux", SND_SOC_NOPM, 0, 0,
93440bc18a2SBard Liao 			 &rt5651_sto2_adc_r2_mux),
93540bc18a2SBard Liao 	/* ADC Mixer */
93640bc18a2SBard Liao 	SND_SOC_DAPM_SUPPLY("Stereo1 Filter", RT5651_PWR_DIG2,
93740bc18a2SBard Liao 			    RT5651_PWR_ADC_STO1_F_BIT, 0, NULL, 0),
93840bc18a2SBard Liao 	SND_SOC_DAPM_SUPPLY("Stereo2 Filter", RT5651_PWR_DIG2,
93940bc18a2SBard Liao 			    RT5651_PWR_ADC_STO2_F_BIT, 0, NULL, 0),
94040bc18a2SBard Liao 	SND_SOC_DAPM_MIXER("Stereo1 ADC MIXL", SND_SOC_NOPM, 0, 0,
94140bc18a2SBard Liao 			   rt5651_sto1_adc_l_mix,
94240bc18a2SBard Liao 			   ARRAY_SIZE(rt5651_sto1_adc_l_mix)),
94340bc18a2SBard Liao 	SND_SOC_DAPM_MIXER("Stereo1 ADC MIXR", SND_SOC_NOPM, 0, 0,
94440bc18a2SBard Liao 			   rt5651_sto1_adc_r_mix,
94540bc18a2SBard Liao 			   ARRAY_SIZE(rt5651_sto1_adc_r_mix)),
94640bc18a2SBard Liao 	SND_SOC_DAPM_MIXER("Stereo2 ADC MIXL", SND_SOC_NOPM, 0, 0,
94740bc18a2SBard Liao 			   rt5651_sto2_adc_l_mix,
94840bc18a2SBard Liao 			   ARRAY_SIZE(rt5651_sto2_adc_l_mix)),
94940bc18a2SBard Liao 	SND_SOC_DAPM_MIXER("Stereo2 ADC MIXR", SND_SOC_NOPM, 0, 0,
95040bc18a2SBard Liao 			   rt5651_sto2_adc_r_mix,
95140bc18a2SBard Liao 			   ARRAY_SIZE(rt5651_sto2_adc_r_mix)),
95240bc18a2SBard Liao 
95340bc18a2SBard Liao 	/* Digital Interface */
95440bc18a2SBard Liao 	SND_SOC_DAPM_SUPPLY("I2S1", RT5651_PWR_DIG1,
95540bc18a2SBard Liao 			    RT5651_PWR_I2S1_BIT, 0, NULL, 0),
95640bc18a2SBard Liao 	SND_SOC_DAPM_PGA("IF1 DAC", SND_SOC_NOPM, 0, 0, NULL, 0),
95740bc18a2SBard Liao 	SND_SOC_DAPM_PGA("IF1 DAC1 L", SND_SOC_NOPM, 0, 0, NULL, 0),
95840bc18a2SBard Liao 	SND_SOC_DAPM_PGA("IF1 DAC1 R", SND_SOC_NOPM, 0, 0, NULL, 0),
95940bc18a2SBard Liao 	SND_SOC_DAPM_PGA("IF1 ADC1", SND_SOC_NOPM, 0, 0, NULL, 0),
96040bc18a2SBard Liao 	SND_SOC_DAPM_PGA("IF1 DAC2 L", SND_SOC_NOPM, 0, 0, NULL, 0),
96140bc18a2SBard Liao 	SND_SOC_DAPM_PGA("IF1 DAC2 R", SND_SOC_NOPM, 0, 0, NULL, 0),
96240bc18a2SBard Liao 	SND_SOC_DAPM_PGA("IF1 ADC2", SND_SOC_NOPM, 0, 0, NULL, 0),
96340bc18a2SBard Liao 	SND_SOC_DAPM_SUPPLY("I2S2", RT5651_PWR_DIG1,
96440bc18a2SBard Liao 			    RT5651_PWR_I2S2_BIT, 0, NULL, 0),
96540bc18a2SBard Liao 	SND_SOC_DAPM_PGA("IF2 DAC", SND_SOC_NOPM, 0, 0, NULL, 0),
96640bc18a2SBard Liao 	SND_SOC_DAPM_PGA("IF2 DAC L", SND_SOC_NOPM, 0, 0, NULL, 0),
96740bc18a2SBard Liao 	SND_SOC_DAPM_PGA("IF2 DAC R", SND_SOC_NOPM, 0, 0, NULL, 0),
96840bc18a2SBard Liao 	SND_SOC_DAPM_MUX("IF2 ADC", SND_SOC_NOPM, 0, 0,
96940bc18a2SBard Liao 			 &rt5651_if2_adc_src_mux),
97040bc18a2SBard Liao 
97140bc18a2SBard Liao 	/* Digital Interface Select */
97240bc18a2SBard Liao 
97340bc18a2SBard Liao 	SND_SOC_DAPM_MUX("PDM L Mux", RT5651_PDM_CTL,
97440bc18a2SBard Liao 			 RT5651_M_PDM_L_SFT, 1, &rt5651_pdm_l_mux),
97540bc18a2SBard Liao 	SND_SOC_DAPM_MUX("PDM R Mux", RT5651_PDM_CTL,
97640bc18a2SBard Liao 			 RT5651_M_PDM_R_SFT, 1, &rt5651_pdm_r_mux),
97740bc18a2SBard Liao 	/* Audio Interface */
97840bc18a2SBard Liao 	SND_SOC_DAPM_AIF_IN("AIF1RX", "AIF1 Playback", 0, SND_SOC_NOPM, 0, 0),
97940bc18a2SBard Liao 	SND_SOC_DAPM_AIF_OUT("AIF1TX", "AIF1 Capture", 0, SND_SOC_NOPM, 0, 0),
98040bc18a2SBard Liao 	SND_SOC_DAPM_AIF_IN("AIF2RX", "AIF2 Playback", 0, SND_SOC_NOPM, 0, 0),
98140bc18a2SBard Liao 	SND_SOC_DAPM_AIF_OUT("AIF2TX", "AIF2 Capture", 0, SND_SOC_NOPM, 0, 0),
98240bc18a2SBard Liao 
98340bc18a2SBard Liao 	/* Audio DSP */
98440bc18a2SBard Liao 	SND_SOC_DAPM_PGA("Audio DSP", SND_SOC_NOPM, 0, 0, NULL, 0),
98540bc18a2SBard Liao 
98640bc18a2SBard Liao 	/* Output Side */
98740bc18a2SBard Liao 	/* DAC mixer before sound effect  */
98840bc18a2SBard Liao 	SND_SOC_DAPM_MIXER("DAC MIXL", SND_SOC_NOPM, 0, 0,
98940bc18a2SBard Liao 			   rt5651_dac_l_mix, ARRAY_SIZE(rt5651_dac_l_mix)),
99040bc18a2SBard Liao 	SND_SOC_DAPM_MIXER("DAC MIXR", SND_SOC_NOPM, 0, 0,
99140bc18a2SBard Liao 			   rt5651_dac_r_mix, ARRAY_SIZE(rt5651_dac_r_mix)),
99240bc18a2SBard Liao 
99340bc18a2SBard Liao 	/* DAC2 channel Mux */
99440bc18a2SBard Liao 	SND_SOC_DAPM_MUX("DAC L2 Mux", SND_SOC_NOPM, 0, 0, &rt5651_dac_l2_mux),
99540bc18a2SBard Liao 	SND_SOC_DAPM_MUX("DAC R2 Mux", SND_SOC_NOPM, 0, 0, &rt5651_dac_r2_mux),
99640bc18a2SBard Liao 	SND_SOC_DAPM_PGA("DAC L2 Volume", SND_SOC_NOPM, 0, 0, NULL, 0),
99740bc18a2SBard Liao 	SND_SOC_DAPM_PGA("DAC R2 Volume", SND_SOC_NOPM, 0, 0, NULL, 0),
99840bc18a2SBard Liao 
99940bc18a2SBard Liao 	SND_SOC_DAPM_SUPPLY("Stero1 DAC Power", RT5651_PWR_DIG2,
100040bc18a2SBard Liao 			    RT5651_PWR_DAC_STO1_F_BIT, 0, NULL, 0),
100140bc18a2SBard Liao 	SND_SOC_DAPM_SUPPLY("Stero2 DAC Power", RT5651_PWR_DIG2,
100240bc18a2SBard Liao 			    RT5651_PWR_DAC_STO2_F_BIT, 0, NULL, 0),
100340bc18a2SBard Liao 	/* DAC Mixer */
100440bc18a2SBard Liao 	SND_SOC_DAPM_MIXER("Stereo DAC MIXL", SND_SOC_NOPM, 0, 0,
100540bc18a2SBard Liao 			   rt5651_sto_dac_l_mix,
100640bc18a2SBard Liao 			   ARRAY_SIZE(rt5651_sto_dac_l_mix)),
100740bc18a2SBard Liao 	SND_SOC_DAPM_MIXER("Stereo DAC MIXR", SND_SOC_NOPM, 0, 0,
100840bc18a2SBard Liao 			   rt5651_sto_dac_r_mix,
100940bc18a2SBard Liao 			   ARRAY_SIZE(rt5651_sto_dac_r_mix)),
101040bc18a2SBard Liao 	SND_SOC_DAPM_MIXER("DD MIXL", SND_SOC_NOPM, 0, 0,
101140bc18a2SBard Liao 			   rt5651_dd_dac_l_mix,
101240bc18a2SBard Liao 			   ARRAY_SIZE(rt5651_dd_dac_l_mix)),
101340bc18a2SBard Liao 	SND_SOC_DAPM_MIXER("DD MIXR", SND_SOC_NOPM, 0, 0,
101440bc18a2SBard Liao 			   rt5651_dd_dac_r_mix,
101540bc18a2SBard Liao 			   ARRAY_SIZE(rt5651_dd_dac_r_mix)),
101640bc18a2SBard Liao 
101740bc18a2SBard Liao 	/* DACs */
101840bc18a2SBard Liao 	SND_SOC_DAPM_DAC("DAC L1", NULL, SND_SOC_NOPM, 0, 0),
101940bc18a2SBard Liao 	SND_SOC_DAPM_DAC("DAC R1", NULL, SND_SOC_NOPM, 0, 0),
102040bc18a2SBard Liao 	SND_SOC_DAPM_SUPPLY("DAC L1 Power", RT5651_PWR_DIG1,
102140bc18a2SBard Liao 			    RT5651_PWR_DAC_L1_BIT, 0, NULL, 0),
102240bc18a2SBard Liao 	SND_SOC_DAPM_SUPPLY("DAC R1 Power", RT5651_PWR_DIG1,
102340bc18a2SBard Liao 			    RT5651_PWR_DAC_R1_BIT, 0, NULL, 0),
102440bc18a2SBard Liao 	/* OUT Mixer */
102540bc18a2SBard Liao 	SND_SOC_DAPM_MIXER("OUT MIXL", RT5651_PWR_MIXER, RT5651_PWR_OM_L_BIT,
102640bc18a2SBard Liao 			   0, rt5651_out_l_mix, ARRAY_SIZE(rt5651_out_l_mix)),
102740bc18a2SBard Liao 	SND_SOC_DAPM_MIXER("OUT MIXR", RT5651_PWR_MIXER, RT5651_PWR_OM_R_BIT,
102840bc18a2SBard Liao 			   0, rt5651_out_r_mix, ARRAY_SIZE(rt5651_out_r_mix)),
102940bc18a2SBard Liao 	/* Ouput Volume */
103040bc18a2SBard Liao 	SND_SOC_DAPM_SWITCH("OUTVOL L", RT5651_PWR_VOL,
103140bc18a2SBard Liao 			    RT5651_PWR_OV_L_BIT, 0, &outvol_l_control),
103240bc18a2SBard Liao 	SND_SOC_DAPM_SWITCH("OUTVOL R", RT5651_PWR_VOL,
103340bc18a2SBard Liao 			    RT5651_PWR_OV_R_BIT, 0, &outvol_r_control),
103440bc18a2SBard Liao 	SND_SOC_DAPM_SWITCH("HPOVOL L", RT5651_PWR_VOL,
103540bc18a2SBard Liao 			    RT5651_PWR_HV_L_BIT, 0, &hpovol_l_control),
103640bc18a2SBard Liao 	SND_SOC_DAPM_SWITCH("HPOVOL R", RT5651_PWR_VOL,
103740bc18a2SBard Liao 			    RT5651_PWR_HV_R_BIT, 0, &hpovol_r_control),
103840bc18a2SBard Liao 	SND_SOC_DAPM_PGA("INL1", RT5651_PWR_VOL,
103940bc18a2SBard Liao 			 RT5651_PWR_IN1_L_BIT, 0, NULL, 0),
104040bc18a2SBard Liao 	SND_SOC_DAPM_PGA("INR1", RT5651_PWR_VOL,
104140bc18a2SBard Liao 			 RT5651_PWR_IN1_R_BIT, 0, NULL, 0),
104240bc18a2SBard Liao 	SND_SOC_DAPM_PGA("INL2", RT5651_PWR_VOL,
104340bc18a2SBard Liao 			 RT5651_PWR_IN2_L_BIT, 0, NULL, 0),
104440bc18a2SBard Liao 	SND_SOC_DAPM_PGA("INR2", RT5651_PWR_VOL,
104540bc18a2SBard Liao 			 RT5651_PWR_IN2_R_BIT, 0, NULL, 0),
104640bc18a2SBard Liao 	/* HPO/LOUT/Mono Mixer */
104740bc18a2SBard Liao 	SND_SOC_DAPM_MIXER("HPOL MIX", SND_SOC_NOPM, 0, 0,
104840bc18a2SBard Liao 			   rt5651_hpo_mix, ARRAY_SIZE(rt5651_hpo_mix)),
104940bc18a2SBard Liao 	SND_SOC_DAPM_MIXER("HPOR MIX", SND_SOC_NOPM, 0, 0,
105040bc18a2SBard Liao 			   rt5651_hpo_mix, ARRAY_SIZE(rt5651_hpo_mix)),
105140bc18a2SBard Liao 	SND_SOC_DAPM_SUPPLY("HP L Amp", RT5651_PWR_ANLG1,
105240bc18a2SBard Liao 			    RT5651_PWR_HP_L_BIT, 0, NULL, 0),
105340bc18a2SBard Liao 	SND_SOC_DAPM_SUPPLY("HP R Amp", RT5651_PWR_ANLG1,
105440bc18a2SBard Liao 			    RT5651_PWR_HP_R_BIT, 0, NULL, 0),
105540bc18a2SBard Liao 	SND_SOC_DAPM_MIXER("LOUT MIX", RT5651_PWR_ANLG1, RT5651_PWR_LM_BIT, 0,
105640bc18a2SBard Liao 			   rt5651_lout_mix, ARRAY_SIZE(rt5651_lout_mix)),
105740bc18a2SBard Liao 
105840bc18a2SBard Liao 	SND_SOC_DAPM_SUPPLY("Amp Power", RT5651_PWR_ANLG1,
105940bc18a2SBard Liao 			    RT5651_PWR_HA_BIT, 0, rt5651_amp_power_event,
106040bc18a2SBard Liao 			    SND_SOC_DAPM_POST_PMU),
106140bc18a2SBard Liao 	SND_SOC_DAPM_PGA_S("HP Amp", 1, SND_SOC_NOPM, 0, 0, rt5651_hp_event,
106240bc18a2SBard Liao 			   SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
106340bc18a2SBard Liao 	SND_SOC_DAPM_SWITCH("HPO L Playback", SND_SOC_NOPM, 0, 0,
106440bc18a2SBard Liao 			    &hpo_l_mute_control),
106540bc18a2SBard Liao 	SND_SOC_DAPM_SWITCH("HPO R Playback", SND_SOC_NOPM, 0, 0,
106640bc18a2SBard Liao 			    &hpo_r_mute_control),
106740bc18a2SBard Liao 	SND_SOC_DAPM_SWITCH("LOUT L Playback", SND_SOC_NOPM, 0, 0,
106840bc18a2SBard Liao 			    &lout_l_mute_control),
106940bc18a2SBard Liao 	SND_SOC_DAPM_SWITCH("LOUT R Playback", SND_SOC_NOPM, 0, 0,
107040bc18a2SBard Liao 			    &lout_r_mute_control),
107140bc18a2SBard Liao 	SND_SOC_DAPM_POST("HP Post", rt5651_hp_post_event),
107240bc18a2SBard Liao 
107340bc18a2SBard Liao 	/* Output Lines */
107440bc18a2SBard Liao 	SND_SOC_DAPM_OUTPUT("HPOL"),
107540bc18a2SBard Liao 	SND_SOC_DAPM_OUTPUT("HPOR"),
107640bc18a2SBard Liao 	SND_SOC_DAPM_OUTPUT("LOUTL"),
107740bc18a2SBard Liao 	SND_SOC_DAPM_OUTPUT("LOUTR"),
107840bc18a2SBard Liao 	SND_SOC_DAPM_OUTPUT("PDML"),
107940bc18a2SBard Liao 	SND_SOC_DAPM_OUTPUT("PDMR"),
108040bc18a2SBard Liao };
108140bc18a2SBard Liao 
108240bc18a2SBard Liao static const struct snd_soc_dapm_route rt5651_dapm_routes[] = {
108340bc18a2SBard Liao 	{"Stero1 DAC Power", NULL, "STO1 DAC ASRC"},
108440bc18a2SBard Liao 	{"Stero2 DAC Power", NULL, "STO2 DAC ASRC"},
108540bc18a2SBard Liao 	{"I2S1", NULL, "I2S1 ASRC"},
108640bc18a2SBard Liao 	{"I2S2", NULL, "I2S2 ASRC"},
108740bc18a2SBard Liao 
108840bc18a2SBard Liao 	{"IN1P", NULL, "LDO"},
108940bc18a2SBard Liao 	{"IN2P", NULL, "LDO"},
109040bc18a2SBard Liao 	{"IN3P", NULL, "LDO"},
109140bc18a2SBard Liao 
109240bc18a2SBard Liao 	{"IN1P", NULL, "MIC1"},
109340bc18a2SBard Liao 	{"IN2P", NULL, "MIC2"},
109440bc18a2SBard Liao 	{"IN2N", NULL, "MIC2"},
109540bc18a2SBard Liao 	{"IN3P", NULL, "MIC3"},
109640bc18a2SBard Liao 
109740bc18a2SBard Liao 	{"BST1", NULL, "IN1P"},
109840bc18a2SBard Liao 	{"BST2", NULL, "IN2P"},
109940bc18a2SBard Liao 	{"BST2", NULL, "IN2N"},
110040bc18a2SBard Liao 	{"BST3", NULL, "IN3P"},
110140bc18a2SBard Liao 
110240bc18a2SBard Liao 	{"INL1 VOL", NULL, "IN2P"},
110340bc18a2SBard Liao 	{"INR1 VOL", NULL, "IN2N"},
110440bc18a2SBard Liao 
110540bc18a2SBard Liao 	{"RECMIXL", "INL1 Switch", "INL1 VOL"},
110640bc18a2SBard Liao 	{"RECMIXL", "BST3 Switch", "BST3"},
110740bc18a2SBard Liao 	{"RECMIXL", "BST2 Switch", "BST2"},
110840bc18a2SBard Liao 	{"RECMIXL", "BST1 Switch", "BST1"},
110940bc18a2SBard Liao 
111040bc18a2SBard Liao 	{"RECMIXR", "INR1 Switch", "INR1 VOL"},
111140bc18a2SBard Liao 	{"RECMIXR", "BST3 Switch", "BST3"},
111240bc18a2SBard Liao 	{"RECMIXR", "BST2 Switch", "BST2"},
111340bc18a2SBard Liao 	{"RECMIXR", "BST1 Switch", "BST1"},
111440bc18a2SBard Liao 
111540bc18a2SBard Liao 	{"ADC L", NULL, "RECMIXL"},
111640bc18a2SBard Liao 	{"ADC L", NULL, "ADC L Power"},
111740bc18a2SBard Liao 	{"ADC R", NULL, "RECMIXR"},
111840bc18a2SBard Liao 	{"ADC R", NULL, "ADC R Power"},
111940bc18a2SBard Liao 
112040bc18a2SBard Liao 	{"DMIC L1", NULL, "DMIC CLK"},
112140bc18a2SBard Liao 	{"DMIC R1", NULL, "DMIC CLK"},
112240bc18a2SBard Liao 
112340bc18a2SBard Liao 	{"Stereo1 ADC L2 Mux", "DMIC", "DMIC L1"},
112440bc18a2SBard Liao 	{"Stereo1 ADC L2 Mux", "DD MIX", "DD MIXL"},
112540bc18a2SBard Liao 	{"Stereo1 ADC L1 Mux", "ADC", "ADC L"},
112640bc18a2SBard Liao 	{"Stereo1 ADC L1 Mux", "DD MIX", "DD MIXL"},
112740bc18a2SBard Liao 
112840bc18a2SBard Liao 	{"Stereo1 ADC R1 Mux", "ADC", "ADC R"},
112940bc18a2SBard Liao 	{"Stereo1 ADC R1 Mux", "DD MIX", "DD MIXR"},
113040bc18a2SBard Liao 	{"Stereo1 ADC R2 Mux", "DMIC", "DMIC R1"},
113140bc18a2SBard Liao 	{"Stereo1 ADC R2 Mux", "DD MIX", "DD MIXR"},
113240bc18a2SBard Liao 
113340bc18a2SBard Liao 	{"Stereo2 ADC L2 Mux", "DMIC L", "DMIC L1"},
113440bc18a2SBard Liao 	{"Stereo2 ADC L2 Mux", "DD MIXL", "DD MIXL"},
113540bc18a2SBard Liao 	{"Stereo2 ADC L1 Mux", "DD MIXL", "DD MIXL"},
113640bc18a2SBard Liao 	{"Stereo2 ADC L1 Mux", "ADCL", "ADC L"},
113740bc18a2SBard Liao 
113840bc18a2SBard Liao 	{"Stereo2 ADC R1 Mux", "DD MIXR", "DD MIXR"},
113940bc18a2SBard Liao 	{"Stereo2 ADC R1 Mux", "ADCR", "ADC R"},
114040bc18a2SBard Liao 	{"Stereo2 ADC R2 Mux", "DMIC R", "DMIC R1"},
114140bc18a2SBard Liao 	{"Stereo2 ADC R2 Mux", "DD MIXR", "DD MIXR"},
114240bc18a2SBard Liao 
114340bc18a2SBard Liao 	{"Stereo1 ADC MIXL", "ADC1 Switch", "Stereo1 ADC L1 Mux"},
114440bc18a2SBard Liao 	{"Stereo1 ADC MIXL", "ADC2 Switch", "Stereo1 ADC L2 Mux"},
114540bc18a2SBard Liao 	{"Stereo1 ADC MIXL", NULL, "Stereo1 Filter"},
114640bc18a2SBard Liao 	{"Stereo1 Filter", NULL, "ADC ASRC"},
114740bc18a2SBard Liao 
114840bc18a2SBard Liao 	{"Stereo1 ADC MIXR", "ADC1 Switch", "Stereo1 ADC R1 Mux"},
114940bc18a2SBard Liao 	{"Stereo1 ADC MIXR", "ADC2 Switch", "Stereo1 ADC R2 Mux"},
115040bc18a2SBard Liao 	{"Stereo1 ADC MIXR", NULL, "Stereo1 Filter"},
115140bc18a2SBard Liao 
115240bc18a2SBard Liao 	{"Stereo2 ADC MIXL", "ADC1 Switch", "Stereo2 ADC L1 Mux"},
115340bc18a2SBard Liao 	{"Stereo2 ADC MIXL", "ADC2 Switch", "Stereo2 ADC L2 Mux"},
115440bc18a2SBard Liao 	{"Stereo2 ADC MIXL", NULL, "Stereo2 Filter"},
115540bc18a2SBard Liao 	{"Stereo2 Filter", NULL, "ADC ASRC"},
115640bc18a2SBard Liao 
115740bc18a2SBard Liao 	{"Stereo2 ADC MIXR", "ADC1 Switch", "Stereo2 ADC R1 Mux"},
115840bc18a2SBard Liao 	{"Stereo2 ADC MIXR", "ADC2 Switch", "Stereo2 ADC R2 Mux"},
115940bc18a2SBard Liao 	{"Stereo2 ADC MIXR", NULL, "Stereo2 Filter"},
116040bc18a2SBard Liao 
116140bc18a2SBard Liao 	{"IF1 ADC2", NULL, "Stereo2 ADC MIXL"},
116240bc18a2SBard Liao 	{"IF1 ADC2", NULL, "Stereo2 ADC MIXR"},
116340bc18a2SBard Liao 	{"IF1 ADC1", NULL, "Stereo1 ADC MIXL"},
116440bc18a2SBard Liao 	{"IF1 ADC1", NULL, "Stereo1 ADC MIXR"},
116540bc18a2SBard Liao 
116640bc18a2SBard Liao 	{"IF1 ADC1", NULL, "I2S1"},
116740bc18a2SBard Liao 
116840bc18a2SBard Liao 	{"IF2 ADC", "IF1 ADC1", "IF1 ADC1"},
116940bc18a2SBard Liao 	{"IF2 ADC", "IF1 ADC2", "IF1 ADC2"},
117040bc18a2SBard Liao 	{"IF2 ADC", NULL, "I2S2"},
117140bc18a2SBard Liao 
117240bc18a2SBard Liao 	{"AIF1TX", NULL, "IF1 ADC1"},
117340bc18a2SBard Liao 	{"AIF1TX", NULL, "IF1 ADC2"},
117440bc18a2SBard Liao 	{"AIF2TX", NULL, "IF2 ADC"},
117540bc18a2SBard Liao 
117640bc18a2SBard Liao 	{"IF1 DAC", NULL, "AIF1RX"},
117740bc18a2SBard Liao 	{"IF1 DAC", NULL, "I2S1"},
117840bc18a2SBard Liao 	{"IF2 DAC", NULL, "AIF2RX"},
117940bc18a2SBard Liao 	{"IF2 DAC", NULL, "I2S2"},
118040bc18a2SBard Liao 
118140bc18a2SBard Liao 	{"IF1 DAC1 L", NULL, "IF1 DAC"},
118240bc18a2SBard Liao 	{"IF1 DAC1 R", NULL, "IF1 DAC"},
118340bc18a2SBard Liao 	{"IF1 DAC2 L", NULL, "IF1 DAC"},
118440bc18a2SBard Liao 	{"IF1 DAC2 R", NULL, "IF1 DAC"},
118540bc18a2SBard Liao 	{"IF2 DAC L", NULL, "IF2 DAC"},
118640bc18a2SBard Liao 	{"IF2 DAC R", NULL, "IF2 DAC"},
118740bc18a2SBard Liao 
118840bc18a2SBard Liao 	{"DAC MIXL", "Stereo ADC Switch", "Stereo1 ADC MIXL"},
118940bc18a2SBard Liao 	{"DAC MIXL", "INF1 Switch", "IF1 DAC1 L"},
119040bc18a2SBard Liao 	{"DAC MIXR", "Stereo ADC Switch", "Stereo1 ADC MIXR"},
119140bc18a2SBard Liao 	{"DAC MIXR", "INF1 Switch", "IF1 DAC1 R"},
119240bc18a2SBard Liao 
119340bc18a2SBard Liao 	{"Audio DSP", NULL, "DAC MIXL"},
119440bc18a2SBard Liao 	{"Audio DSP", NULL, "DAC MIXR"},
119540bc18a2SBard Liao 
119640bc18a2SBard Liao 	{"DAC L2 Mux", "IF1", "IF1 DAC2 L"},
119740bc18a2SBard Liao 	{"DAC L2 Mux", "IF2", "IF2 DAC L"},
119840bc18a2SBard Liao 	{"DAC L2 Volume", NULL, "DAC L2 Mux"},
119940bc18a2SBard Liao 
120040bc18a2SBard Liao 	{"DAC R2 Mux", "IF1", "IF1 DAC2 R"},
120140bc18a2SBard Liao 	{"DAC R2 Mux", "IF2", "IF2 DAC R"},
120240bc18a2SBard Liao 	{"DAC R2 Volume", NULL, "DAC R2 Mux"},
120340bc18a2SBard Liao 
120440bc18a2SBard Liao 	{"Stereo DAC MIXL", "DAC L1 Switch", "Audio DSP"},
120540bc18a2SBard Liao 	{"Stereo DAC MIXL", "DAC L2 Switch", "DAC L2 Volume"},
120640bc18a2SBard Liao 	{"Stereo DAC MIXL", "DAC R1 Switch", "DAC MIXR"},
120740bc18a2SBard Liao 	{"Stereo DAC MIXL", NULL, "Stero1 DAC Power"},
120840bc18a2SBard Liao 	{"Stereo DAC MIXL", NULL, "Stero2 DAC Power"},
120940bc18a2SBard Liao 	{"Stereo DAC MIXR", "DAC R1 Switch", "Audio DSP"},
121040bc18a2SBard Liao 	{"Stereo DAC MIXR", "DAC R2 Switch", "DAC R2 Volume"},
121140bc18a2SBard Liao 	{"Stereo DAC MIXR", "DAC L1 Switch", "DAC MIXL"},
121240bc18a2SBard Liao 	{"Stereo DAC MIXR", NULL, "Stero1 DAC Power"},
121340bc18a2SBard Liao 	{"Stereo DAC MIXR", NULL, "Stero2 DAC Power"},
121440bc18a2SBard Liao 
121540bc18a2SBard Liao 	{"PDM L Mux", "Stereo DAC MIX", "Stereo DAC MIXL"},
121640bc18a2SBard Liao 	{"PDM L Mux", "DD MIX", "DAC MIXL"},
121740bc18a2SBard Liao 	{"PDM R Mux", "Stereo DAC MIX", "Stereo DAC MIXR"},
121840bc18a2SBard Liao 	{"PDM R Mux", "DD MIX", "DAC MIXR"},
121940bc18a2SBard Liao 
122040bc18a2SBard Liao 	{"DAC L1", NULL, "Stereo DAC MIXL"},
122140bc18a2SBard Liao 	{"DAC L1", NULL, "DAC L1 Power"},
122240bc18a2SBard Liao 	{"DAC R1", NULL, "Stereo DAC MIXR"},
122340bc18a2SBard Liao 	{"DAC R1", NULL, "DAC R1 Power"},
122440bc18a2SBard Liao 
122540bc18a2SBard Liao 	{"DD MIXL", "DAC L1 Switch", "DAC MIXL"},
122640bc18a2SBard Liao 	{"DD MIXL", "DAC L2 Switch", "DAC L2 Volume"},
122740bc18a2SBard Liao 	{"DD MIXL", "DAC R2 Switch", "DAC R2 Volume"},
122840bc18a2SBard Liao 	{"DD MIXL", NULL, "Stero2 DAC Power"},
122940bc18a2SBard Liao 
123040bc18a2SBard Liao 	{"DD MIXR", "DAC R1 Switch", "DAC MIXR"},
123140bc18a2SBard Liao 	{"DD MIXR", "DAC R2 Switch", "DAC R2 Volume"},
123240bc18a2SBard Liao 	{"DD MIXR", "DAC L2 Switch", "DAC L2 Volume"},
123340bc18a2SBard Liao 	{"DD MIXR", NULL, "Stero2 DAC Power"},
123440bc18a2SBard Liao 
123540bc18a2SBard Liao 	{"OUT MIXL", "BST1 Switch", "BST1"},
123640bc18a2SBard Liao 	{"OUT MIXL", "BST2 Switch", "BST2"},
123740bc18a2SBard Liao 	{"OUT MIXL", "INL1 Switch", "INL1 VOL"},
123840bc18a2SBard Liao 	{"OUT MIXL", "REC MIXL Switch", "RECMIXL"},
123940bc18a2SBard Liao 	{"OUT MIXL", "DAC L1 Switch", "DAC L1"},
124040bc18a2SBard Liao 
124140bc18a2SBard Liao 	{"OUT MIXR", "BST2 Switch", "BST2"},
124240bc18a2SBard Liao 	{"OUT MIXR", "BST1 Switch", "BST1"},
124340bc18a2SBard Liao 	{"OUT MIXR", "INR1 Switch", "INR1 VOL"},
124440bc18a2SBard Liao 	{"OUT MIXR", "REC MIXR Switch", "RECMIXR"},
124540bc18a2SBard Liao 	{"OUT MIXR", "DAC R1 Switch", "DAC R1"},
124640bc18a2SBard Liao 
124740bc18a2SBard Liao 	{"HPOVOL L", "Switch", "OUT MIXL"},
124840bc18a2SBard Liao 	{"HPOVOL R", "Switch", "OUT MIXR"},
124940bc18a2SBard Liao 	{"OUTVOL L", "Switch", "OUT MIXL"},
125040bc18a2SBard Liao 	{"OUTVOL R", "Switch", "OUT MIXR"},
125140bc18a2SBard Liao 
125240bc18a2SBard Liao 	{"HPOL MIX", "HPO MIX DAC1 Switch", "DAC L1"},
125340bc18a2SBard Liao 	{"HPOL MIX", "HPO MIX HPVOL Switch", "HPOVOL L"},
125440bc18a2SBard Liao 	{"HPOL MIX", NULL, "HP L Amp"},
125540bc18a2SBard Liao 	{"HPOR MIX", "HPO MIX DAC1 Switch", "DAC R1"},
125640bc18a2SBard Liao 	{"HPOR MIX", "HPO MIX HPVOL Switch", "HPOVOL R"},
125740bc18a2SBard Liao 	{"HPOR MIX", NULL, "HP R Amp"},
125840bc18a2SBard Liao 
125940bc18a2SBard Liao 	{"LOUT MIX", "DAC L1 Switch", "DAC L1"},
126040bc18a2SBard Liao 	{"LOUT MIX", "DAC R1 Switch", "DAC R1"},
126140bc18a2SBard Liao 	{"LOUT MIX", "OUTVOL L Switch", "OUTVOL L"},
126240bc18a2SBard Liao 	{"LOUT MIX", "OUTVOL R Switch", "OUTVOL R"},
126340bc18a2SBard Liao 
126440bc18a2SBard Liao 	{"HP Amp", NULL, "HPOL MIX"},
126540bc18a2SBard Liao 	{"HP Amp", NULL, "HPOR MIX"},
126640bc18a2SBard Liao 	{"HP Amp", NULL, "Amp Power"},
126740bc18a2SBard Liao 	{"HPO L Playback", "Switch", "HP Amp"},
126840bc18a2SBard Liao 	{"HPO R Playback", "Switch", "HP Amp"},
126940bc18a2SBard Liao 	{"HPOL", NULL, "HPO L Playback"},
127040bc18a2SBard Liao 	{"HPOR", NULL, "HPO R Playback"},
127140bc18a2SBard Liao 
127240bc18a2SBard Liao 	{"LOUT L Playback", "Switch", "LOUT MIX"},
127340bc18a2SBard Liao 	{"LOUT R Playback", "Switch", "LOUT MIX"},
127440bc18a2SBard Liao 	{"LOUTL", NULL, "LOUT L Playback"},
127540bc18a2SBard Liao 	{"LOUTL", NULL, "Amp Power"},
127640bc18a2SBard Liao 	{"LOUTR", NULL, "LOUT R Playback"},
127740bc18a2SBard Liao 	{"LOUTR", NULL, "Amp Power"},
127840bc18a2SBard Liao 
127940bc18a2SBard Liao 	{"PDML", NULL, "PDM L Mux"},
128040bc18a2SBard Liao 	{"PDMR", NULL, "PDM R Mux"},
128140bc18a2SBard Liao };
128240bc18a2SBard Liao 
128340bc18a2SBard Liao static int rt5651_hw_params(struct snd_pcm_substream *substream,
128440bc18a2SBard Liao 	struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
128540bc18a2SBard Liao {
128617b52010SKuninori Morimoto 	struct snd_soc_component *component = dai->component;
128717b52010SKuninori Morimoto 	struct rt5651_priv *rt5651 = snd_soc_component_get_drvdata(component);
128840bc18a2SBard Liao 	unsigned int val_len = 0, val_clk, mask_clk;
128940bc18a2SBard Liao 	int pre_div, bclk_ms, frame_size;
129040bc18a2SBard Liao 
129140bc18a2SBard Liao 	rt5651->lrck[dai->id] = params_rate(params);
1292d92950e7SOder Chiou 	pre_div = rl6231_get_clk_info(rt5651->sysclk, rt5651->lrck[dai->id]);
129340bc18a2SBard Liao 
129440bc18a2SBard Liao 	if (pre_div < 0) {
129517b52010SKuninori Morimoto 		dev_err(component->dev, "Unsupported clock setting\n");
129640bc18a2SBard Liao 		return -EINVAL;
129740bc18a2SBard Liao 	}
129840bc18a2SBard Liao 	frame_size = snd_soc_params_to_frame_size(params);
129940bc18a2SBard Liao 	if (frame_size < 0) {
130017b52010SKuninori Morimoto 		dev_err(component->dev, "Unsupported frame size: %d\n", frame_size);
130140bc18a2SBard Liao 		return -EINVAL;
130240bc18a2SBard Liao 	}
130340bc18a2SBard Liao 	bclk_ms = frame_size > 32 ? 1 : 0;
130440bc18a2SBard Liao 	rt5651->bclk[dai->id] = rt5651->lrck[dai->id] * (32 << bclk_ms);
130540bc18a2SBard Liao 
130640bc18a2SBard Liao 	dev_dbg(dai->dev, "bclk is %dHz and lrck is %dHz\n",
130740bc18a2SBard Liao 		rt5651->bclk[dai->id], rt5651->lrck[dai->id]);
130840bc18a2SBard Liao 	dev_dbg(dai->dev, "bclk_ms is %d and pre_div is %d for iis %d\n",
130940bc18a2SBard Liao 				bclk_ms, pre_div, dai->id);
131040bc18a2SBard Liao 
1311794f33d2SMark Brown 	switch (params_width(params)) {
1312794f33d2SMark Brown 	case 16:
131340bc18a2SBard Liao 		break;
1314794f33d2SMark Brown 	case 20:
131540bc18a2SBard Liao 		val_len |= RT5651_I2S_DL_20;
131640bc18a2SBard Liao 		break;
1317794f33d2SMark Brown 	case 24:
131840bc18a2SBard Liao 		val_len |= RT5651_I2S_DL_24;
131940bc18a2SBard Liao 		break;
1320794f33d2SMark Brown 	case 8:
132140bc18a2SBard Liao 		val_len |= RT5651_I2S_DL_8;
132240bc18a2SBard Liao 		break;
132340bc18a2SBard Liao 	default:
132440bc18a2SBard Liao 		return -EINVAL;
132540bc18a2SBard Liao 	}
132640bc18a2SBard Liao 
132740bc18a2SBard Liao 	switch (dai->id) {
132840bc18a2SBard Liao 	case RT5651_AIF1:
132940bc18a2SBard Liao 		mask_clk = RT5651_I2S_PD1_MASK;
133040bc18a2SBard Liao 		val_clk = pre_div << RT5651_I2S_PD1_SFT;
133117b52010SKuninori Morimoto 		snd_soc_component_update_bits(component, RT5651_I2S1_SDP,
133240bc18a2SBard Liao 			RT5651_I2S_DL_MASK, val_len);
133317b52010SKuninori Morimoto 		snd_soc_component_update_bits(component, RT5651_ADDA_CLK1, mask_clk, val_clk);
133440bc18a2SBard Liao 		break;
133540bc18a2SBard Liao 	case RT5651_AIF2:
133640bc18a2SBard Liao 		mask_clk = RT5651_I2S_BCLK_MS2_MASK | RT5651_I2S_PD2_MASK;
133740bc18a2SBard Liao 		val_clk = pre_div << RT5651_I2S_PD2_SFT;
133817b52010SKuninori Morimoto 		snd_soc_component_update_bits(component, RT5651_I2S2_SDP,
133940bc18a2SBard Liao 			RT5651_I2S_DL_MASK, val_len);
134017b52010SKuninori Morimoto 		snd_soc_component_update_bits(component, RT5651_ADDA_CLK1, mask_clk, val_clk);
134140bc18a2SBard Liao 		break;
134240bc18a2SBard Liao 	default:
134317b52010SKuninori Morimoto 		dev_err(component->dev, "Wrong dai->id: %d\n", dai->id);
134440bc18a2SBard Liao 		return -EINVAL;
134540bc18a2SBard Liao 	}
134640bc18a2SBard Liao 
134740bc18a2SBard Liao 	return 0;
134840bc18a2SBard Liao }
134940bc18a2SBard Liao 
135040bc18a2SBard Liao static int rt5651_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
135140bc18a2SBard Liao {
135217b52010SKuninori Morimoto 	struct snd_soc_component *component = dai->component;
135317b52010SKuninori Morimoto 	struct rt5651_priv *rt5651 = snd_soc_component_get_drvdata(component);
135440bc18a2SBard Liao 	unsigned int reg_val = 0;
135540bc18a2SBard Liao 
135640bc18a2SBard Liao 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
135740bc18a2SBard Liao 	case SND_SOC_DAIFMT_CBM_CFM:
135840bc18a2SBard Liao 		rt5651->master[dai->id] = 1;
135940bc18a2SBard Liao 		break;
136040bc18a2SBard Liao 	case SND_SOC_DAIFMT_CBS_CFS:
136140bc18a2SBard Liao 		reg_val |= RT5651_I2S_MS_S;
136240bc18a2SBard Liao 		rt5651->master[dai->id] = 0;
136340bc18a2SBard Liao 		break;
136440bc18a2SBard Liao 	default:
136540bc18a2SBard Liao 		return -EINVAL;
136640bc18a2SBard Liao 	}
136740bc18a2SBard Liao 
136840bc18a2SBard Liao 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
136940bc18a2SBard Liao 	case SND_SOC_DAIFMT_NB_NF:
137040bc18a2SBard Liao 		break;
137140bc18a2SBard Liao 	case SND_SOC_DAIFMT_IB_NF:
137240bc18a2SBard Liao 		reg_val |= RT5651_I2S_BP_INV;
137340bc18a2SBard Liao 		break;
137440bc18a2SBard Liao 	default:
137540bc18a2SBard Liao 		return -EINVAL;
137640bc18a2SBard Liao 	}
137740bc18a2SBard Liao 
137840bc18a2SBard Liao 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
137940bc18a2SBard Liao 	case SND_SOC_DAIFMT_I2S:
138040bc18a2SBard Liao 		break;
138140bc18a2SBard Liao 	case SND_SOC_DAIFMT_LEFT_J:
138240bc18a2SBard Liao 		reg_val |= RT5651_I2S_DF_LEFT;
138340bc18a2SBard Liao 		break;
138440bc18a2SBard Liao 	case SND_SOC_DAIFMT_DSP_A:
138540bc18a2SBard Liao 		reg_val |= RT5651_I2S_DF_PCM_A;
138640bc18a2SBard Liao 		break;
138740bc18a2SBard Liao 	case SND_SOC_DAIFMT_DSP_B:
138840bc18a2SBard Liao 		reg_val |= RT5651_I2S_DF_PCM_B;
138940bc18a2SBard Liao 		break;
139040bc18a2SBard Liao 	default:
139140bc18a2SBard Liao 		return -EINVAL;
139240bc18a2SBard Liao 	}
139340bc18a2SBard Liao 
139440bc18a2SBard Liao 	switch (dai->id) {
139540bc18a2SBard Liao 	case RT5651_AIF1:
139617b52010SKuninori Morimoto 		snd_soc_component_update_bits(component, RT5651_I2S1_SDP,
139740bc18a2SBard Liao 			RT5651_I2S_MS_MASK | RT5651_I2S_BP_MASK |
139840bc18a2SBard Liao 			RT5651_I2S_DF_MASK, reg_val);
139940bc18a2SBard Liao 		break;
140040bc18a2SBard Liao 	case RT5651_AIF2:
140117b52010SKuninori Morimoto 		snd_soc_component_update_bits(component, RT5651_I2S2_SDP,
140240bc18a2SBard Liao 			RT5651_I2S_MS_MASK | RT5651_I2S_BP_MASK |
140340bc18a2SBard Liao 			RT5651_I2S_DF_MASK, reg_val);
140440bc18a2SBard Liao 		break;
140540bc18a2SBard Liao 	default:
140617b52010SKuninori Morimoto 		dev_err(component->dev, "Wrong dai->id: %d\n", dai->id);
140740bc18a2SBard Liao 		return -EINVAL;
140840bc18a2SBard Liao 	}
140940bc18a2SBard Liao 	return 0;
141040bc18a2SBard Liao }
141140bc18a2SBard Liao 
141240bc18a2SBard Liao static int rt5651_set_dai_sysclk(struct snd_soc_dai *dai,
141340bc18a2SBard Liao 		int clk_id, unsigned int freq, int dir)
141440bc18a2SBard Liao {
141517b52010SKuninori Morimoto 	struct snd_soc_component *component = dai->component;
141617b52010SKuninori Morimoto 	struct rt5651_priv *rt5651 = snd_soc_component_get_drvdata(component);
141740bc18a2SBard Liao 	unsigned int reg_val = 0;
1418d082174cSHans de Goede 	unsigned int pll_bit = 0;
141940bc18a2SBard Liao 
142040bc18a2SBard Liao 	if (freq == rt5651->sysclk && clk_id == rt5651->sysclk_src)
142140bc18a2SBard Liao 		return 0;
142240bc18a2SBard Liao 
142340bc18a2SBard Liao 	switch (clk_id) {
142440bc18a2SBard Liao 	case RT5651_SCLK_S_MCLK:
142540bc18a2SBard Liao 		reg_val |= RT5651_SCLK_SRC_MCLK;
142640bc18a2SBard Liao 		break;
142740bc18a2SBard Liao 	case RT5651_SCLK_S_PLL1:
142840bc18a2SBard Liao 		reg_val |= RT5651_SCLK_SRC_PLL1;
1429d082174cSHans de Goede 		pll_bit |= RT5651_PWR_PLL;
143040bc18a2SBard Liao 		break;
143140bc18a2SBard Liao 	case RT5651_SCLK_S_RCCLK:
143240bc18a2SBard Liao 		reg_val |= RT5651_SCLK_SRC_RCCLK;
143340bc18a2SBard Liao 		break;
143440bc18a2SBard Liao 	default:
143517b52010SKuninori Morimoto 		dev_err(component->dev, "Invalid clock id (%d)\n", clk_id);
143640bc18a2SBard Liao 		return -EINVAL;
143740bc18a2SBard Liao 	}
1438d082174cSHans de Goede 	snd_soc_component_update_bits(component, RT5651_PWR_ANLG2,
1439d082174cSHans de Goede 		RT5651_PWR_PLL, pll_bit);
144017b52010SKuninori Morimoto 	snd_soc_component_update_bits(component, RT5651_GLB_CLK,
144140bc18a2SBard Liao 		RT5651_SCLK_SRC_MASK, reg_val);
144240bc18a2SBard Liao 	rt5651->sysclk = freq;
144340bc18a2SBard Liao 	rt5651->sysclk_src = clk_id;
144440bc18a2SBard Liao 
144540bc18a2SBard Liao 	dev_dbg(dai->dev, "Sysclk is %dHz and clock id is %d\n", freq, clk_id);
144640bc18a2SBard Liao 
144740bc18a2SBard Liao 	return 0;
144840bc18a2SBard Liao }
144940bc18a2SBard Liao 
145040bc18a2SBard Liao static int rt5651_set_dai_pll(struct snd_soc_dai *dai, int pll_id, int source,
145140bc18a2SBard Liao 			unsigned int freq_in, unsigned int freq_out)
145240bc18a2SBard Liao {
145317b52010SKuninori Morimoto 	struct snd_soc_component *component = dai->component;
145417b52010SKuninori Morimoto 	struct rt5651_priv *rt5651 = snd_soc_component_get_drvdata(component);
145571c7a2d6SOder Chiou 	struct rl6231_pll_code pll_code;
145640bc18a2SBard Liao 	int ret;
145740bc18a2SBard Liao 
145840bc18a2SBard Liao 	if (source == rt5651->pll_src && freq_in == rt5651->pll_in &&
145940bc18a2SBard Liao 	    freq_out == rt5651->pll_out)
146040bc18a2SBard Liao 		return 0;
146140bc18a2SBard Liao 
146240bc18a2SBard Liao 	if (!freq_in || !freq_out) {
146317b52010SKuninori Morimoto 		dev_dbg(component->dev, "PLL disabled\n");
146440bc18a2SBard Liao 
146540bc18a2SBard Liao 		rt5651->pll_in = 0;
146640bc18a2SBard Liao 		rt5651->pll_out = 0;
146717b52010SKuninori Morimoto 		snd_soc_component_update_bits(component, RT5651_GLB_CLK,
146840bc18a2SBard Liao 			RT5651_SCLK_SRC_MASK, RT5651_SCLK_SRC_MCLK);
146940bc18a2SBard Liao 		return 0;
147040bc18a2SBard Liao 	}
147140bc18a2SBard Liao 
147240bc18a2SBard Liao 	switch (source) {
147340bc18a2SBard Liao 	case RT5651_PLL1_S_MCLK:
147417b52010SKuninori Morimoto 		snd_soc_component_update_bits(component, RT5651_GLB_CLK,
147540bc18a2SBard Liao 			RT5651_PLL1_SRC_MASK, RT5651_PLL1_SRC_MCLK);
147640bc18a2SBard Liao 		break;
147740bc18a2SBard Liao 	case RT5651_PLL1_S_BCLK1:
147817b52010SKuninori Morimoto 		snd_soc_component_update_bits(component, RT5651_GLB_CLK,
147940bc18a2SBard Liao 				RT5651_PLL1_SRC_MASK, RT5651_PLL1_SRC_BCLK1);
148040bc18a2SBard Liao 		break;
148140bc18a2SBard Liao 	case RT5651_PLL1_S_BCLK2:
148217b52010SKuninori Morimoto 			snd_soc_component_update_bits(component, RT5651_GLB_CLK,
148340bc18a2SBard Liao 				RT5651_PLL1_SRC_MASK, RT5651_PLL1_SRC_BCLK2);
148440bc18a2SBard Liao 		break;
148540bc18a2SBard Liao 	default:
148617b52010SKuninori Morimoto 		dev_err(component->dev, "Unknown PLL source %d\n", source);
148740bc18a2SBard Liao 		return -EINVAL;
148840bc18a2SBard Liao 	}
148940bc18a2SBard Liao 
149071c7a2d6SOder Chiou 	ret = rl6231_pll_calc(freq_in, freq_out, &pll_code);
149140bc18a2SBard Liao 	if (ret < 0) {
149217b52010SKuninori Morimoto 		dev_err(component->dev, "Unsupport input clock %d\n", freq_in);
149340bc18a2SBard Liao 		return ret;
149440bc18a2SBard Liao 	}
149540bc18a2SBard Liao 
149617b52010SKuninori Morimoto 	dev_dbg(component->dev, "bypass=%d m=%d n=%d k=%d\n",
149771c7a2d6SOder Chiou 		pll_code.m_bp, (pll_code.m_bp ? 0 : pll_code.m_code),
149871c7a2d6SOder Chiou 		pll_code.n_code, pll_code.k_code);
149940bc18a2SBard Liao 
150017b52010SKuninori Morimoto 	snd_soc_component_write(component, RT5651_PLL_CTRL1,
150171c7a2d6SOder Chiou 		pll_code.n_code << RT5651_PLL_N_SFT | pll_code.k_code);
150217b52010SKuninori Morimoto 	snd_soc_component_write(component, RT5651_PLL_CTRL2,
150371c7a2d6SOder Chiou 		(pll_code.m_bp ? 0 : pll_code.m_code) << RT5651_PLL_M_SFT |
150471c7a2d6SOder Chiou 		pll_code.m_bp << RT5651_PLL_M_BP_SFT);
150540bc18a2SBard Liao 
150640bc18a2SBard Liao 	rt5651->pll_in = freq_in;
150740bc18a2SBard Liao 	rt5651->pll_out = freq_out;
150840bc18a2SBard Liao 	rt5651->pll_src = source;
150940bc18a2SBard Liao 
151040bc18a2SBard Liao 	return 0;
151140bc18a2SBard Liao }
151240bc18a2SBard Liao 
151317b52010SKuninori Morimoto static int rt5651_set_bias_level(struct snd_soc_component *component,
151440bc18a2SBard Liao 			enum snd_soc_bias_level level)
151540bc18a2SBard Liao {
151640bc18a2SBard Liao 	switch (level) {
151740bc18a2SBard Liao 	case SND_SOC_BIAS_PREPARE:
151817b52010SKuninori Morimoto 		if (SND_SOC_BIAS_STANDBY == snd_soc_component_get_bias_level(component)) {
1519984c803fSHans de Goede 			if (snd_soc_component_read32(component, RT5651_PLL_MODE_1) & 0x9200)
1520984c803fSHans de Goede 				snd_soc_component_update_bits(component, RT5651_D_MISC,
1521984c803fSHans de Goede 						    0xc00, 0xc00);
1522984c803fSHans de Goede 		}
1523984c803fSHans de Goede 		break;
1524984c803fSHans de Goede 	case SND_SOC_BIAS_STANDBY:
1525984c803fSHans de Goede 		if (SND_SOC_BIAS_OFF == snd_soc_component_get_bias_level(component)) {
152617b52010SKuninori Morimoto 			snd_soc_component_update_bits(component, RT5651_PWR_ANLG1,
152740bc18a2SBard Liao 				RT5651_PWR_VREF1 | RT5651_PWR_MB |
152840bc18a2SBard Liao 				RT5651_PWR_BG | RT5651_PWR_VREF2,
152940bc18a2SBard Liao 				RT5651_PWR_VREF1 | RT5651_PWR_MB |
153040bc18a2SBard Liao 				RT5651_PWR_BG | RT5651_PWR_VREF2);
153140bc18a2SBard Liao 			usleep_range(10000, 15000);
153217b52010SKuninori Morimoto 			snd_soc_component_update_bits(component, RT5651_PWR_ANLG1,
153340bc18a2SBard Liao 				RT5651_PWR_FV1 | RT5651_PWR_FV2,
153440bc18a2SBard Liao 				RT5651_PWR_FV1 | RT5651_PWR_FV2);
153517b52010SKuninori Morimoto 			snd_soc_component_update_bits(component, RT5651_D_MISC, 0x1, 0x1);
153640bc18a2SBard Liao 		}
153740bc18a2SBard Liao 		break;
153840bc18a2SBard Liao 
1539984c803fSHans de Goede 	case SND_SOC_BIAS_OFF:
154017b52010SKuninori Morimoto 		snd_soc_component_write(component, RT5651_D_MISC, 0x0010);
154117b52010SKuninori Morimoto 		snd_soc_component_write(component, RT5651_PWR_DIG1, 0x0000);
154217b52010SKuninori Morimoto 		snd_soc_component_write(component, RT5651_PWR_DIG2, 0x0000);
154317b52010SKuninori Morimoto 		snd_soc_component_write(component, RT5651_PWR_VOL, 0x0000);
154417b52010SKuninori Morimoto 		snd_soc_component_write(component, RT5651_PWR_MIXER, 0x0000);
1545bba4e685SHans de Goede 		/* Do not touch the LDO voltage select bits on bias-off */
1546bba4e685SHans de Goede 		snd_soc_component_update_bits(component, RT5651_PWR_ANLG1,
1547bba4e685SHans de Goede 			~RT5651_PWR_LDO_DVO_MASK, 0);
1548887fcc6fSHans de Goede 		/* Leave PLL1 and jack-detect power as is, all others off */
1549887fcc6fSHans de Goede 		snd_soc_component_update_bits(component, RT5651_PWR_ANLG2,
1550887fcc6fSHans de Goede 				    ~(RT5651_PWR_PLL | RT5651_PWR_JD_M), 0);
155140bc18a2SBard Liao 		break;
155240bc18a2SBard Liao 
155340bc18a2SBard Liao 	default:
155440bc18a2SBard Liao 		break;
155540bc18a2SBard Liao 	}
155640bc18a2SBard Liao 
155740bc18a2SBard Liao 	return 0;
155840bc18a2SBard Liao }
155940bc18a2SBard Liao 
15601310e737SHans de Goede static void rt5651_enable_micbias1_for_ovcd(struct snd_soc_component *component)
156140bc18a2SBard Liao {
15621310e737SHans de Goede 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
156340bc18a2SBard Liao 
15641310e737SHans de Goede 	snd_soc_dapm_mutex_lock(dapm);
15651310e737SHans de Goede 	snd_soc_dapm_force_enable_pin_unlocked(dapm, "LDO");
15661310e737SHans de Goede 	snd_soc_dapm_force_enable_pin_unlocked(dapm, "micbias1");
15674b4a373cSHans de Goede 	/* OVCD is unreliable when used with RCCLK as sysclk-source */
15684b4a373cSHans de Goede 	snd_soc_dapm_force_enable_pin_unlocked(dapm, "Platform Clock");
15691310e737SHans de Goede 	snd_soc_dapm_sync_unlocked(dapm);
15701310e737SHans de Goede 	snd_soc_dapm_mutex_unlock(dapm);
157180bbe4a3SCarlo Caione }
157280bbe4a3SCarlo Caione 
15731310e737SHans de Goede static void rt5651_disable_micbias1_for_ovcd(struct snd_soc_component *component)
15741310e737SHans de Goede {
15751310e737SHans de Goede 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
15761310e737SHans de Goede 
15771310e737SHans de Goede 	snd_soc_dapm_mutex_lock(dapm);
15784b4a373cSHans de Goede 	snd_soc_dapm_disable_pin_unlocked(dapm, "Platform Clock");
15791310e737SHans de Goede 	snd_soc_dapm_disable_pin_unlocked(dapm, "micbias1");
15801310e737SHans de Goede 	snd_soc_dapm_disable_pin_unlocked(dapm, "LDO");
15811310e737SHans de Goede 	snd_soc_dapm_sync_unlocked(dapm);
15821310e737SHans de Goede 	snd_soc_dapm_mutex_unlock(dapm);
15831310e737SHans de Goede }
15841310e737SHans de Goede 
1585df1569f2SHans de Goede static void rt5651_enable_micbias1_ovcd_irq(struct snd_soc_component *component)
1586df1569f2SHans de Goede {
1587df1569f2SHans de Goede 	struct rt5651_priv *rt5651 = snd_soc_component_get_drvdata(component);
1588df1569f2SHans de Goede 
1589df1569f2SHans de Goede 	snd_soc_component_update_bits(component, RT5651_IRQ_CTRL2,
1590df1569f2SHans de Goede 		RT5651_IRQ_MB1_OC_MASK, RT5651_IRQ_MB1_OC_NOR);
1591df1569f2SHans de Goede 	rt5651->ovcd_irq_enabled = true;
1592df1569f2SHans de Goede }
1593df1569f2SHans de Goede 
1594df1569f2SHans de Goede static void rt5651_disable_micbias1_ovcd_irq(struct snd_soc_component *component)
1595df1569f2SHans de Goede {
1596df1569f2SHans de Goede 	struct rt5651_priv *rt5651 = snd_soc_component_get_drvdata(component);
1597df1569f2SHans de Goede 
1598df1569f2SHans de Goede 	snd_soc_component_update_bits(component, RT5651_IRQ_CTRL2,
1599df1569f2SHans de Goede 		RT5651_IRQ_MB1_OC_MASK, RT5651_IRQ_MB1_OC_BP);
1600df1569f2SHans de Goede 	rt5651->ovcd_irq_enabled = false;
1601df1569f2SHans de Goede }
1602df1569f2SHans de Goede 
16031b1ad835SHans de Goede static void rt5651_clear_micbias1_ovcd(struct snd_soc_component *component)
16041b1ad835SHans de Goede {
16051b1ad835SHans de Goede 	snd_soc_component_update_bits(component, RT5651_IRQ_CTRL2,
16061b1ad835SHans de Goede 		RT5651_MB1_OC_CLR, 0);
16071b1ad835SHans de Goede }
16081b1ad835SHans de Goede 
16091b1ad835SHans de Goede static bool rt5651_micbias1_ovcd(struct snd_soc_component *component)
16101b1ad835SHans de Goede {
16111b1ad835SHans de Goede 	int val;
16121b1ad835SHans de Goede 
16131b1ad835SHans de Goede 	val = snd_soc_component_read32(component, RT5651_IRQ_CTRL2);
16141b1ad835SHans de Goede 	dev_dbg(component->dev, "irq ctrl2 %#04x\n", val);
16151b1ad835SHans de Goede 
16161b1ad835SHans de Goede 	return (val & RT5651_MB1_OC_CLR);
16171b1ad835SHans de Goede }
16181b1ad835SHans de Goede 
16190fe94745SHans de Goede static bool rt5651_jack_inserted(struct snd_soc_component *component)
16200fe94745SHans de Goede {
16210fe94745SHans de Goede 	struct rt5651_priv *rt5651 = snd_soc_component_get_drvdata(component);
16220fe94745SHans de Goede 	int val;
16230fe94745SHans de Goede 
16240fe94745SHans de Goede 	val = snd_soc_component_read32(component, RT5651_INT_IRQ_ST);
16250fe94745SHans de Goede 	dev_dbg(component->dev, "irq status %#04x\n", val);
16260fe94745SHans de Goede 
16270fe94745SHans de Goede 	switch (rt5651->jd_src) {
16280fe94745SHans de Goede 	case RT5651_JD1_1:
16290fe94745SHans de Goede 		val &= 0x1000;
16300fe94745SHans de Goede 		break;
16310fe94745SHans de Goede 	case RT5651_JD1_2:
16320fe94745SHans de Goede 		val &= 0x2000;
16330fe94745SHans de Goede 		break;
16340fe94745SHans de Goede 	case RT5651_JD2:
16350fe94745SHans de Goede 		val &= 0x4000;
16360fe94745SHans de Goede 		break;
16370fe94745SHans de Goede 	default:
16380fe94745SHans de Goede 		break;
16390fe94745SHans de Goede 	}
16400fe94745SHans de Goede 
16410fe94745SHans de Goede 	return val == 0;
16420fe94745SHans de Goede }
16430fe94745SHans de Goede 
1644df1569f2SHans de Goede /* Jack detect and button-press timings */
1645ee680968SHans de Goede #define JACK_SETTLE_TIME	100 /* milli seconds */
1646ee680968SHans de Goede #define JACK_DETECT_COUNT	5
1647ee680968SHans de Goede #define JACK_DETECT_MAXCOUNT	20  /* Aprox. 2 seconds worth of tries */
1648df1569f2SHans de Goede #define JACK_UNPLUG_TIME	80  /* milli seconds */
1649df1569f2SHans de Goede #define BP_POLL_TIME		10  /* milli seconds */
1650df1569f2SHans de Goede #define BP_POLL_MAXCOUNT	200 /* assume something is wrong after this */
1651df1569f2SHans de Goede #define BP_THRESHOLD		3
1652df1569f2SHans de Goede 
1653df1569f2SHans de Goede static void rt5651_start_button_press_work(struct snd_soc_component *component)
1654df1569f2SHans de Goede {
1655df1569f2SHans de Goede 	struct rt5651_priv *rt5651 = snd_soc_component_get_drvdata(component);
1656df1569f2SHans de Goede 
1657df1569f2SHans de Goede 	rt5651->poll_count = 0;
1658df1569f2SHans de Goede 	rt5651->press_count = 0;
1659df1569f2SHans de Goede 	rt5651->release_count = 0;
1660df1569f2SHans de Goede 	rt5651->pressed = false;
1661df1569f2SHans de Goede 	rt5651->press_reported = false;
1662df1569f2SHans de Goede 	rt5651_clear_micbias1_ovcd(component);
1663df1569f2SHans de Goede 	schedule_delayed_work(&rt5651->bp_work, msecs_to_jiffies(BP_POLL_TIME));
1664df1569f2SHans de Goede }
1665df1569f2SHans de Goede 
1666df1569f2SHans de Goede static void rt5651_button_press_work(struct work_struct *work)
1667df1569f2SHans de Goede {
1668df1569f2SHans de Goede 	struct rt5651_priv *rt5651 =
1669df1569f2SHans de Goede 		container_of(work, struct rt5651_priv, bp_work.work);
1670df1569f2SHans de Goede 	struct snd_soc_component *component = rt5651->component;
1671df1569f2SHans de Goede 
1672df1569f2SHans de Goede 	/* Check the jack was not removed underneath us */
1673df1569f2SHans de Goede 	if (!rt5651_jack_inserted(component))
1674df1569f2SHans de Goede 		return;
1675df1569f2SHans de Goede 
1676df1569f2SHans de Goede 	if (rt5651_micbias1_ovcd(component)) {
1677df1569f2SHans de Goede 		rt5651->release_count = 0;
1678df1569f2SHans de Goede 		rt5651->press_count++;
1679df1569f2SHans de Goede 		/* Remember till after JACK_UNPLUG_TIME wait */
1680df1569f2SHans de Goede 		if (rt5651->press_count >= BP_THRESHOLD)
1681df1569f2SHans de Goede 			rt5651->pressed = true;
1682df1569f2SHans de Goede 		rt5651_clear_micbias1_ovcd(component);
1683df1569f2SHans de Goede 	} else {
1684df1569f2SHans de Goede 		rt5651->press_count = 0;
1685df1569f2SHans de Goede 		rt5651->release_count++;
1686df1569f2SHans de Goede 	}
1687df1569f2SHans de Goede 
1688df1569f2SHans de Goede 	/*
1689df1569f2SHans de Goede 	 * The pins get temporarily shorted on jack unplug, so we poll for
1690df1569f2SHans de Goede 	 * at least JACK_UNPLUG_TIME milli-seconds before reporting a press.
1691df1569f2SHans de Goede 	 */
1692df1569f2SHans de Goede 	rt5651->poll_count++;
1693df1569f2SHans de Goede 	if (rt5651->poll_count < (JACK_UNPLUG_TIME / BP_POLL_TIME)) {
1694df1569f2SHans de Goede 		schedule_delayed_work(&rt5651->bp_work,
1695df1569f2SHans de Goede 				      msecs_to_jiffies(BP_POLL_TIME));
1696df1569f2SHans de Goede 		return;
1697df1569f2SHans de Goede 	}
1698df1569f2SHans de Goede 
1699df1569f2SHans de Goede 	if (rt5651->pressed && !rt5651->press_reported) {
1700df1569f2SHans de Goede 		dev_dbg(component->dev, "headset button press\n");
1701df1569f2SHans de Goede 		snd_soc_jack_report(rt5651->hp_jack, SND_JACK_BTN_0,
1702df1569f2SHans de Goede 				    SND_JACK_BTN_0);
1703df1569f2SHans de Goede 		rt5651->press_reported = true;
1704df1569f2SHans de Goede 	}
1705df1569f2SHans de Goede 
1706df1569f2SHans de Goede 	if (rt5651->release_count >= BP_THRESHOLD) {
1707df1569f2SHans de Goede 		if (rt5651->press_reported) {
1708df1569f2SHans de Goede 			dev_dbg(component->dev, "headset button release\n");
1709df1569f2SHans de Goede 			snd_soc_jack_report(rt5651->hp_jack, 0, SND_JACK_BTN_0);
1710df1569f2SHans de Goede 		}
1711df1569f2SHans de Goede 		/* Re-enable OVCD IRQ to detect next press */
1712df1569f2SHans de Goede 		rt5651_enable_micbias1_ovcd_irq(component);
1713df1569f2SHans de Goede 		return; /* Stop polling */
1714df1569f2SHans de Goede 	}
1715df1569f2SHans de Goede 
1716df1569f2SHans de Goede 	schedule_delayed_work(&rt5651->bp_work, msecs_to_jiffies(BP_POLL_TIME));
1717df1569f2SHans de Goede }
1718ee680968SHans de Goede 
1719ee680968SHans de Goede static int rt5651_detect_headset(struct snd_soc_component *component)
1720ee680968SHans de Goede {
1721ee680968SHans de Goede 	int i, headset_count = 0, headphone_count = 0;
1722ee680968SHans de Goede 
1723ee680968SHans de Goede 	/*
1724ee680968SHans de Goede 	 * We get the insertion event before the jack is fully inserted at which
1725ee680968SHans de Goede 	 * point the second ring on a TRRS connector may short the 2nd ring and
1726ee680968SHans de Goede 	 * sleeve contacts, also the overcurrent detection is not entirely
1727ee680968SHans de Goede 	 * reliable. So we try several times with a wait in between until we
1728ee680968SHans de Goede 	 * detect the same type JACK_DETECT_COUNT times in a row.
1729ee680968SHans de Goede 	 */
1730ee680968SHans de Goede 	for (i = 0; i < JACK_DETECT_MAXCOUNT; i++) {
1731ee680968SHans de Goede 		/* Clear any previous over-current status flag */
1732ee680968SHans de Goede 		rt5651_clear_micbias1_ovcd(component);
1733ee680968SHans de Goede 
1734ee680968SHans de Goede 		msleep(JACK_SETTLE_TIME);
1735ee680968SHans de Goede 
1736ee680968SHans de Goede 		/* Check the jack is still connected before checking ovcd */
1737ee680968SHans de Goede 		if (!rt5651_jack_inserted(component))
1738ee680968SHans de Goede 			return 0;
1739ee680968SHans de Goede 
1740ee680968SHans de Goede 		if (rt5651_micbias1_ovcd(component)) {
1741ee680968SHans de Goede 			/*
1742ee680968SHans de Goede 			 * Over current detected, there is a short between the
1743ee680968SHans de Goede 			 * 2nd ring contact and the ground, so a TRS connector
1744ee680968SHans de Goede 			 * without a mic contact and thus plain headphones.
1745ee680968SHans de Goede 			 */
1746ee680968SHans de Goede 			dev_dbg(component->dev, "mic-gnd shorted\n");
1747ee680968SHans de Goede 			headset_count = 0;
1748ee680968SHans de Goede 			headphone_count++;
1749ee680968SHans de Goede 			if (headphone_count == JACK_DETECT_COUNT)
1750ee680968SHans de Goede 				return SND_JACK_HEADPHONE;
1751ee680968SHans de Goede 		} else {
1752ee680968SHans de Goede 			dev_dbg(component->dev, "mic-gnd open\n");
1753ee680968SHans de Goede 			headphone_count = 0;
1754ee680968SHans de Goede 			headset_count++;
1755ee680968SHans de Goede 			if (headset_count == JACK_DETECT_COUNT)
1756ee680968SHans de Goede 				return SND_JACK_HEADSET;
1757ee680968SHans de Goede 		}
1758ee680968SHans de Goede 	}
1759ee680968SHans de Goede 
1760ee680968SHans de Goede 	dev_err(component->dev, "Error detecting headset vs headphones, bad contact?, assuming headphones\n");
1761ee680968SHans de Goede 	return SND_JACK_HEADPHONE;
1762ee680968SHans de Goede }
1763ee680968SHans de Goede 
1764ee680968SHans de Goede static void rt5651_jack_detect_work(struct work_struct *work)
1765ee680968SHans de Goede {
1766ee680968SHans de Goede 	struct rt5651_priv *rt5651 =
1767ee680968SHans de Goede 		container_of(work, struct rt5651_priv, jack_detect_work);
1768df1569f2SHans de Goede 	struct snd_soc_component *component = rt5651->component;
1769ee680968SHans de Goede 	int report = 0;
1770ee680968SHans de Goede 
1771df1569f2SHans de Goede 	if (!rt5651_jack_inserted(component)) {
1772df1569f2SHans de Goede 		/* Jack removed, or spurious IRQ? */
1773df1569f2SHans de Goede 		if (rt5651->hp_jack->status & SND_JACK_HEADPHONE) {
1774df1569f2SHans de Goede 			if (rt5651->hp_jack->status & SND_JACK_MICROPHONE) {
1775df1569f2SHans de Goede 				cancel_delayed_work_sync(&rt5651->bp_work);
1776df1569f2SHans de Goede 				rt5651_disable_micbias1_ovcd_irq(component);
1777df1569f2SHans de Goede 				rt5651_disable_micbias1_for_ovcd(component);
1778ee680968SHans de Goede 			}
1779df1569f2SHans de Goede 			snd_soc_jack_report(rt5651->hp_jack, 0,
1780df1569f2SHans de Goede 					    SND_JACK_HEADSET | SND_JACK_BTN_0);
1781df1569f2SHans de Goede 			dev_dbg(component->dev, "jack unplugged\n");
1782df1569f2SHans de Goede 		}
1783df1569f2SHans de Goede 	} else if (!(rt5651->hp_jack->status & SND_JACK_HEADPHONE)) {
1784df1569f2SHans de Goede 		/* Jack inserted */
1785df1569f2SHans de Goede 		WARN_ON(rt5651->ovcd_irq_enabled);
1786df1569f2SHans de Goede 		rt5651_enable_micbias1_for_ovcd(component);
1787df1569f2SHans de Goede 		report = rt5651_detect_headset(component);
1788df1569f2SHans de Goede 		if (report == SND_JACK_HEADSET) {
1789df1569f2SHans de Goede 			/* Enable ovcd IRQ for button press detect. */
1790df1569f2SHans de Goede 			rt5651_enable_micbias1_ovcd_irq(component);
1791df1569f2SHans de Goede 		} else {
1792df1569f2SHans de Goede 			/* No more need for overcurrent detect. */
1793df1569f2SHans de Goede 			rt5651_disable_micbias1_for_ovcd(component);
1794df1569f2SHans de Goede 		}
1795df1569f2SHans de Goede 		dev_dbg(component->dev, "detect report %#02x\n", report);
1796ee680968SHans de Goede 		snd_soc_jack_report(rt5651->hp_jack, report, SND_JACK_HEADSET);
1797df1569f2SHans de Goede 	} else if (rt5651->ovcd_irq_enabled && rt5651_micbias1_ovcd(component)) {
1798df1569f2SHans de Goede 		dev_dbg(component->dev, "OVCD IRQ\n");
1799df1569f2SHans de Goede 
1800df1569f2SHans de Goede 		/*
1801df1569f2SHans de Goede 		 * The ovcd IRQ keeps firing while the button is pressed, so
1802df1569f2SHans de Goede 		 * we disable it and start polling the button until released.
1803df1569f2SHans de Goede 		 *
1804df1569f2SHans de Goede 		 * The disable will make the IRQ pin 0 again and since we get
1805df1569f2SHans de Goede 		 * IRQs on both edges (so as to detect both jack plugin and
1806df1569f2SHans de Goede 		 * unplug) this means we will immediately get another IRQ.
1807df1569f2SHans de Goede 		 * The ovcd_irq_enabled check above makes the 2ND IRQ a NOP.
1808df1569f2SHans de Goede 		 */
1809df1569f2SHans de Goede 		rt5651_disable_micbias1_ovcd_irq(component);
1810df1569f2SHans de Goede 		rt5651_start_button_press_work(component);
1811df1569f2SHans de Goede 
1812df1569f2SHans de Goede 		/*
1813df1569f2SHans de Goede 		 * If the jack-detect IRQ flag goes high (unplug) after our
1814df1569f2SHans de Goede 		 * above rt5651_jack_inserted() check and before we have
1815df1569f2SHans de Goede 		 * disabled the OVCD IRQ, the IRQ pin will stay high and as
1816df1569f2SHans de Goede 		 * we react to edges, we miss the unplug event -> recheck.
1817df1569f2SHans de Goede 		 */
1818df1569f2SHans de Goede 		queue_work(system_long_wq, &rt5651->jack_detect_work);
1819df1569f2SHans de Goede 	}
1820ee680968SHans de Goede }
1821ee680968SHans de Goede 
1822d8b8c878SHans de Goede static irqreturn_t rt5651_irq(int irq, void *data)
1823d8b8c878SHans de Goede {
1824d8b8c878SHans de Goede 	struct rt5651_priv *rt5651 = data;
1825d8b8c878SHans de Goede 
1826ee680968SHans de Goede 	queue_work(system_power_efficient_wq, &rt5651->jack_detect_work);
1827d8b8c878SHans de Goede 
1828d8b8c878SHans de Goede 	return IRQ_HANDLED;
1829d8b8c878SHans de Goede }
1830d8b8c878SHans de Goede 
18318d2d7bcdSHans de Goede static void rt5651_cancel_work(void *data)
18328d2d7bcdSHans de Goede {
18338d2d7bcdSHans de Goede 	struct rt5651_priv *rt5651 = data;
18348d2d7bcdSHans de Goede 
18358d2d7bcdSHans de Goede 	cancel_work_sync(&rt5651->jack_detect_work);
1836df1569f2SHans de Goede 	cancel_delayed_work_sync(&rt5651->bp_work);
18378d2d7bcdSHans de Goede }
18388d2d7bcdSHans de Goede 
183934c906ddSHans de Goede static void rt5651_enable_jack_detect(struct snd_soc_component *component,
184034c906ddSHans de Goede 				      struct snd_soc_jack *hp_jack)
1841d8b8c878SHans de Goede {
1842d8b8c878SHans de Goede 	struct rt5651_priv *rt5651 = snd_soc_component_get_drvdata(component);
1843d8b8c878SHans de Goede 
1844d8b8c878SHans de Goede 	/* IRQ output on GPIO1 */
1845d8b8c878SHans de Goede 	snd_soc_component_update_bits(component, RT5651_GPIO_CTRL1,
1846d8b8c878SHans de Goede 		RT5651_GP1_PIN_MASK, RT5651_GP1_PIN_IRQ);
1847d8b8c878SHans de Goede 
1848d8b8c878SHans de Goede 	/* Select jack detect source */
1849d8b8c878SHans de Goede 	switch (rt5651->jd_src) {
1850d8b8c878SHans de Goede 	case RT5651_JD1_1:
1851d8b8c878SHans de Goede 		snd_soc_component_update_bits(component, RT5651_JD_CTRL2,
1852d8b8c878SHans de Goede 			RT5651_JD_TRG_SEL_MASK, RT5651_JD_TRG_SEL_JD1_1);
1853d8b8c878SHans de Goede 		snd_soc_component_update_bits(component, RT5651_IRQ_CTRL1,
1854d8b8c878SHans de Goede 			RT5651_JD1_1_IRQ_EN, RT5651_JD1_1_IRQ_EN);
1855d8b8c878SHans de Goede 		break;
1856d8b8c878SHans de Goede 	case RT5651_JD1_2:
1857d8b8c878SHans de Goede 		snd_soc_component_update_bits(component, RT5651_JD_CTRL2,
1858d8b8c878SHans de Goede 			RT5651_JD_TRG_SEL_MASK, RT5651_JD_TRG_SEL_JD1_2);
1859d8b8c878SHans de Goede 		snd_soc_component_update_bits(component, RT5651_IRQ_CTRL1,
1860d8b8c878SHans de Goede 			RT5651_JD1_2_IRQ_EN, RT5651_JD1_2_IRQ_EN);
1861d8b8c878SHans de Goede 		break;
1862d8b8c878SHans de Goede 	case RT5651_JD2:
1863d8b8c878SHans de Goede 		snd_soc_component_update_bits(component, RT5651_JD_CTRL2,
1864d8b8c878SHans de Goede 			RT5651_JD_TRG_SEL_MASK, RT5651_JD_TRG_SEL_JD2);
1865d8b8c878SHans de Goede 		snd_soc_component_update_bits(component, RT5651_IRQ_CTRL1,
1866d8b8c878SHans de Goede 			RT5651_JD2_IRQ_EN, RT5651_JD2_IRQ_EN);
1867d8b8c878SHans de Goede 		break;
1868d8b8c878SHans de Goede 	case RT5651_JD_NULL:
186934c906ddSHans de Goede 		return;
1870d8b8c878SHans de Goede 	default:
1871d8b8c878SHans de Goede 		dev_err(component->dev, "Currently only JD1_1 / JD1_2 / JD2 are supported\n");
187234c906ddSHans de Goede 		return;
1873d8b8c878SHans de Goede 	}
1874d8b8c878SHans de Goede 
187557d9d7c3SHans de Goede 	/* Enable jack detect power */
187657d9d7c3SHans de Goede 	snd_soc_component_update_bits(component, RT5651_PWR_ANLG2,
187757d9d7c3SHans de Goede 		RT5651_PWR_JD_M, RT5651_PWR_JD_M);
187857d9d7c3SHans de Goede 
1879e6eb0207SHans de Goede 	/* Set OVCD threshold current and scale-factor */
1880e6eb0207SHans de Goede 	snd_soc_component_write(component, RT5651_PR_BASE + RT5651_BIAS_CUR4,
1881e6eb0207SHans de Goede 				0xa800 | rt5651->ovcd_sf);
1882e6eb0207SHans de Goede 
18839e179592SHans de Goede 	snd_soc_component_update_bits(component, RT5651_MICBIAS,
18849e179592SHans de Goede 				      RT5651_MIC1_OVCD_MASK |
18859e179592SHans de Goede 				      RT5651_MIC1_OVTH_MASK |
18869e179592SHans de Goede 				      RT5651_PWR_CLK12M_MASK |
18879e179592SHans de Goede 				      RT5651_PWR_MB_MASK,
1888f1088d4bSHans de Goede 				      RT5651_MIC1_OVCD_EN |
1889583a9debSHans de Goede 				      rt5651->ovcd_th |
18909e179592SHans de Goede 				      RT5651_PWR_MB_PU |
18919e179592SHans de Goede 				      RT5651_PWR_CLK12M_PU);
1892d8b8c878SHans de Goede 
18931b1ad835SHans de Goede 	/*
18941b1ad835SHans de Goede 	 * The over-current-detect is only reliable in detecting the absence
18951b1ad835SHans de Goede 	 * of over-current, when the mic-contact in the jack is short-circuited,
18961b1ad835SHans de Goede 	 * the hardware periodically retries if it can apply the bias-current
18971b1ad835SHans de Goede 	 * leading to the ovcd status flip-flopping 1-0-1 with it being 0 about
18981b1ad835SHans de Goede 	 * 10% of the time, as we poll the ovcd status bit we might hit that
18991b1ad835SHans de Goede 	 * 10%, so we enable sticky mode and when checking OVCD we clear the
19001b1ad835SHans de Goede 	 * status, msleep() a bit and then check to get a reliable reading.
19011b1ad835SHans de Goede 	 */
19021b1ad835SHans de Goede 	snd_soc_component_update_bits(component, RT5651_IRQ_CTRL2,
19031b1ad835SHans de Goede 		RT5651_MB1_OC_STKY_MASK, RT5651_MB1_OC_STKY_EN);
19041b1ad835SHans de Goede 
1905d8b8c878SHans de Goede 	rt5651->hp_jack = hp_jack;
1906df1569f2SHans de Goede 	if (rt5651->hp_jack->status & SND_JACK_MICROPHONE) {
1907df1569f2SHans de Goede 		rt5651_enable_micbias1_for_ovcd(component);
1908df1569f2SHans de Goede 		rt5651_enable_micbias1_ovcd_irq(component);
1909df1569f2SHans de Goede 	}
1910df1569f2SHans de Goede 
191134c906ddSHans de Goede 	enable_irq(rt5651->irq);
1912d8b8c878SHans de Goede 	/* sync initial jack state */
1913ee680968SHans de Goede 	queue_work(system_power_efficient_wq, &rt5651->jack_detect_work);
191434c906ddSHans de Goede }
191534c906ddSHans de Goede 
191634c906ddSHans de Goede static void rt5651_disable_jack_detect(struct snd_soc_component *component)
191734c906ddSHans de Goede {
191834c906ddSHans de Goede 	struct rt5651_priv *rt5651 = snd_soc_component_get_drvdata(component);
191934c906ddSHans de Goede 
192034c906ddSHans de Goede 	disable_irq(rt5651->irq);
192134c906ddSHans de Goede 	rt5651_cancel_work(rt5651);
192234c906ddSHans de Goede 
1923df1569f2SHans de Goede 	if (rt5651->hp_jack->status & SND_JACK_MICROPHONE) {
1924df1569f2SHans de Goede 		rt5651_disable_micbias1_ovcd_irq(component);
1925df1569f2SHans de Goede 		rt5651_disable_micbias1_for_ovcd(component);
1926df1569f2SHans de Goede 		snd_soc_jack_report(rt5651->hp_jack, 0, SND_JACK_BTN_0);
1927df1569f2SHans de Goede 	}
1928df1569f2SHans de Goede 
192934c906ddSHans de Goede 	rt5651->hp_jack = NULL;
193034c906ddSHans de Goede }
193134c906ddSHans de Goede 
193234c906ddSHans de Goede static int rt5651_set_jack(struct snd_soc_component *component,
193334c906ddSHans de Goede 			   struct snd_soc_jack *jack, void *data)
193434c906ddSHans de Goede {
193534c906ddSHans de Goede 	if (jack)
193634c906ddSHans de Goede 		rt5651_enable_jack_detect(component, jack);
193734c906ddSHans de Goede 	else
193834c906ddSHans de Goede 		rt5651_disable_jack_detect(component);
1939d8b8c878SHans de Goede 
1940d8b8c878SHans de Goede 	return 0;
1941d8b8c878SHans de Goede }
1942d8b8c878SHans de Goede 
19431cf5b504SHans de Goede /*
19441cf5b504SHans de Goede  * Note on some platforms the platform code may need to add device-properties,
19451cf5b504SHans de Goede  * rather then relying only on properties set by the firmware. Therefor the
19461cf5b504SHans de Goede  * property parsing MUST be done from the component driver's probe function,
19471cf5b504SHans de Goede  * rather then from the i2c driver's probe function, so that the platform-code
19481cf5b504SHans de Goede  * can attach extra properties before calling snd_soc_register_card().
19491cf5b504SHans de Goede  */
19501cf5b504SHans de Goede static void rt5651_apply_properties(struct snd_soc_component *component)
19515f293d43SHans de Goede {
1952f0c2a330SHans de Goede 	struct rt5651_priv *rt5651 = snd_soc_component_get_drvdata(component);
1953f0c2a330SHans de Goede 	u32 val;
1954f0c2a330SHans de Goede 
19555f293d43SHans de Goede 	if (device_property_read_bool(component->dev, "realtek,in2-differential"))
19565f293d43SHans de Goede 		snd_soc_component_update_bits(component, RT5651_IN1_IN2,
19575f293d43SHans de Goede 				RT5651_IN_DF2, RT5651_IN_DF2);
19585f293d43SHans de Goede 
19595f293d43SHans de Goede 	if (device_property_read_bool(component->dev, "realtek,dmic-en"))
19605f293d43SHans de Goede 		snd_soc_component_update_bits(component, RT5651_GPIO_CTRL1,
19615f293d43SHans de Goede 				RT5651_GP2_PIN_MASK, RT5651_GP2_PIN_DMIC1_SCL);
1962f0c2a330SHans de Goede 
1963f0c2a330SHans de Goede 	if (device_property_read_u32(component->dev,
1964f0c2a330SHans de Goede 				     "realtek,jack-detect-source", &val) == 0)
1965f0c2a330SHans de Goede 		rt5651->jd_src = val;
1966583a9debSHans de Goede 
1967e6eb0207SHans de Goede 	/*
1968e6eb0207SHans de Goede 	 * Testing on various boards has shown that good defaults for the OVCD
1969e6eb0207SHans de Goede 	 * threshold and scale-factor are 2000µA and 0.75. For an effective
1970e6eb0207SHans de Goede 	 * limit of 1500µA, this seems to be more reliable then 1500µA and 1.0.
1971e6eb0207SHans de Goede 	 */
1972583a9debSHans de Goede 	rt5651->ovcd_th = RT5651_MIC1_OVTH_2000UA;
1973e6eb0207SHans de Goede 	rt5651->ovcd_sf = RT5651_MIC_OVCD_SF_0P75;
1974583a9debSHans de Goede 
1975583a9debSHans de Goede 	if (device_property_read_u32(component->dev,
1976583a9debSHans de Goede 			"realtek,over-current-threshold-microamp", &val) == 0) {
1977583a9debSHans de Goede 		switch (val) {
1978583a9debSHans de Goede 		case 600:
1979583a9debSHans de Goede 			rt5651->ovcd_th = RT5651_MIC1_OVTH_600UA;
1980583a9debSHans de Goede 			break;
1981583a9debSHans de Goede 		case 1500:
1982583a9debSHans de Goede 			rt5651->ovcd_th = RT5651_MIC1_OVTH_1500UA;
1983583a9debSHans de Goede 			break;
1984583a9debSHans de Goede 		case 2000:
1985583a9debSHans de Goede 			rt5651->ovcd_th = RT5651_MIC1_OVTH_2000UA;
1986583a9debSHans de Goede 			break;
1987583a9debSHans de Goede 		default:
1988583a9debSHans de Goede 			dev_warn(component->dev, "Warning: Invalid over-current-threshold-microamp value: %d, defaulting to 2000uA\n",
1989583a9debSHans de Goede 				 val);
1990583a9debSHans de Goede 		}
1991583a9debSHans de Goede 	}
1992e6eb0207SHans de Goede 
1993e6eb0207SHans de Goede 	if (device_property_read_u32(component->dev,
1994e6eb0207SHans de Goede 			"realtek,over-current-scale-factor", &val) == 0) {
1995e6eb0207SHans de Goede 		if (val <= RT5651_OVCD_SF_1P5)
1996e6eb0207SHans de Goede 			rt5651->ovcd_sf = val << RT5651_MIC_OVCD_SF_SFT;
1997e6eb0207SHans de Goede 		else
1998e6eb0207SHans de Goede 			dev_warn(component->dev, "Warning: Invalid over-current-scale-factor value: %d, defaulting to 0.75\n",
1999e6eb0207SHans de Goede 				 val);
2000e6eb0207SHans de Goede 	}
20015f293d43SHans de Goede }
20025f293d43SHans de Goede 
200317b52010SKuninori Morimoto static int rt5651_probe(struct snd_soc_component *component)
200440bc18a2SBard Liao {
200517b52010SKuninori Morimoto 	struct rt5651_priv *rt5651 = snd_soc_component_get_drvdata(component);
200640bc18a2SBard Liao 
200717b52010SKuninori Morimoto 	rt5651->component = component;
200840bc18a2SBard Liao 
20093d7719d3SHans de Goede 	snd_soc_component_update_bits(component, RT5651_PWR_ANLG1,
20103d7719d3SHans de Goede 		RT5651_PWR_LDO_DVO_MASK, RT5651_PWR_LDO_DVO_1_2V);
20113d7719d3SHans de Goede 
201217b52010SKuninori Morimoto 	snd_soc_component_force_bias_level(component, SND_SOC_BIAS_OFF);
201340bc18a2SBard Liao 
20145f293d43SHans de Goede 	rt5651_apply_properties(component);
20155f293d43SHans de Goede 
201640bc18a2SBard Liao 	return 0;
201740bc18a2SBard Liao }
201840bc18a2SBard Liao 
201940bc18a2SBard Liao #ifdef CONFIG_PM
202017b52010SKuninori Morimoto static int rt5651_suspend(struct snd_soc_component *component)
202140bc18a2SBard Liao {
202217b52010SKuninori Morimoto 	struct rt5651_priv *rt5651 = snd_soc_component_get_drvdata(component);
202340bc18a2SBard Liao 
202440bc18a2SBard Liao 	regcache_cache_only(rt5651->regmap, true);
202540bc18a2SBard Liao 	regcache_mark_dirty(rt5651->regmap);
202640bc18a2SBard Liao 	return 0;
202740bc18a2SBard Liao }
202840bc18a2SBard Liao 
202917b52010SKuninori Morimoto static int rt5651_resume(struct snd_soc_component *component)
203040bc18a2SBard Liao {
203117b52010SKuninori Morimoto 	struct rt5651_priv *rt5651 = snd_soc_component_get_drvdata(component);
203240bc18a2SBard Liao 
203340bc18a2SBard Liao 	regcache_cache_only(rt5651->regmap, false);
203417b52010SKuninori Morimoto 	snd_soc_component_cache_sync(component);
203540bc18a2SBard Liao 
203640bc18a2SBard Liao 	return 0;
203740bc18a2SBard Liao }
203840bc18a2SBard Liao #else
203940bc18a2SBard Liao #define rt5651_suspend NULL
204040bc18a2SBard Liao #define rt5651_resume NULL
204140bc18a2SBard Liao #endif
204240bc18a2SBard Liao 
204340bc18a2SBard Liao #define RT5651_STEREO_RATES SNDRV_PCM_RATE_8000_96000
204440bc18a2SBard Liao #define RT5651_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
204540bc18a2SBard Liao 			SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S8)
204640bc18a2SBard Liao 
2047871c131dSMark Brown static const struct snd_soc_dai_ops rt5651_aif_dai_ops = {
204840bc18a2SBard Liao 	.hw_params = rt5651_hw_params,
204940bc18a2SBard Liao 	.set_fmt = rt5651_set_dai_fmt,
205040bc18a2SBard Liao 	.set_sysclk = rt5651_set_dai_sysclk,
205140bc18a2SBard Liao 	.set_pll = rt5651_set_dai_pll,
205240bc18a2SBard Liao };
205340bc18a2SBard Liao 
2054871c131dSMark Brown static struct snd_soc_dai_driver rt5651_dai[] = {
205540bc18a2SBard Liao 	{
205640bc18a2SBard Liao 		.name = "rt5651-aif1",
205740bc18a2SBard Liao 		.id = RT5651_AIF1,
205840bc18a2SBard Liao 		.playback = {
205940bc18a2SBard Liao 			.stream_name = "AIF1 Playback",
206040bc18a2SBard Liao 			.channels_min = 1,
206140bc18a2SBard Liao 			.channels_max = 2,
206240bc18a2SBard Liao 			.rates = RT5651_STEREO_RATES,
206340bc18a2SBard Liao 			.formats = RT5651_FORMATS,
206440bc18a2SBard Liao 		},
206540bc18a2SBard Liao 		.capture = {
206640bc18a2SBard Liao 			.stream_name = "AIF1 Capture",
206740bc18a2SBard Liao 			.channels_min = 1,
206840bc18a2SBard Liao 			.channels_max = 2,
206940bc18a2SBard Liao 			.rates = RT5651_STEREO_RATES,
207040bc18a2SBard Liao 			.formats = RT5651_FORMATS,
207140bc18a2SBard Liao 		},
207240bc18a2SBard Liao 		.ops = &rt5651_aif_dai_ops,
207340bc18a2SBard Liao 	},
207440bc18a2SBard Liao 	{
207540bc18a2SBard Liao 		.name = "rt5651-aif2",
207640bc18a2SBard Liao 		.id = RT5651_AIF2,
207740bc18a2SBard Liao 		.playback = {
207840bc18a2SBard Liao 			.stream_name = "AIF2 Playback",
207940bc18a2SBard Liao 			.channels_min = 1,
208040bc18a2SBard Liao 			.channels_max = 2,
208140bc18a2SBard Liao 			.rates = RT5651_STEREO_RATES,
208240bc18a2SBard Liao 			.formats = RT5651_FORMATS,
208340bc18a2SBard Liao 		},
208440bc18a2SBard Liao 		.capture = {
208540bc18a2SBard Liao 			.stream_name = "AIF2 Capture",
208640bc18a2SBard Liao 			.channels_min = 1,
208740bc18a2SBard Liao 			.channels_max = 2,
208840bc18a2SBard Liao 			.rates = RT5651_STEREO_RATES,
208940bc18a2SBard Liao 			.formats = RT5651_FORMATS,
209040bc18a2SBard Liao 		},
209140bc18a2SBard Liao 		.ops = &rt5651_aif_dai_ops,
209240bc18a2SBard Liao 	},
209340bc18a2SBard Liao };
209440bc18a2SBard Liao 
209517b52010SKuninori Morimoto static const struct snd_soc_component_driver soc_component_dev_rt5651 = {
209640bc18a2SBard Liao 	.probe			= rt5651_probe,
209740bc18a2SBard Liao 	.suspend		= rt5651_suspend,
209840bc18a2SBard Liao 	.resume			= rt5651_resume,
209940bc18a2SBard Liao 	.set_bias_level		= rt5651_set_bias_level,
21006f0b819aSHans de Goede 	.set_jack		= rt5651_set_jack,
210140bc18a2SBard Liao 	.controls		= rt5651_snd_controls,
210240bc18a2SBard Liao 	.num_controls		= ARRAY_SIZE(rt5651_snd_controls),
210340bc18a2SBard Liao 	.dapm_widgets		= rt5651_dapm_widgets,
210440bc18a2SBard Liao 	.num_dapm_widgets	= ARRAY_SIZE(rt5651_dapm_widgets),
210540bc18a2SBard Liao 	.dapm_routes		= rt5651_dapm_routes,
210640bc18a2SBard Liao 	.num_dapm_routes	= ARRAY_SIZE(rt5651_dapm_routes),
210717b52010SKuninori Morimoto 	.use_pmdown_time	= 1,
210817b52010SKuninori Morimoto 	.endianness		= 1,
210917b52010SKuninori Morimoto 	.non_legacy_dai_naming	= 1,
211040bc18a2SBard Liao };
211140bc18a2SBard Liao 
211240bc18a2SBard Liao static const struct regmap_config rt5651_regmap = {
211340bc18a2SBard Liao 	.reg_bits = 8,
211440bc18a2SBard Liao 	.val_bits = 16,
211540bc18a2SBard Liao 
211640bc18a2SBard Liao 	.max_register = RT5651_DEVICE_ID + 1 + (ARRAY_SIZE(rt5651_ranges) *
211740bc18a2SBard Liao 					       RT5651_PR_SPACING),
211840bc18a2SBard Liao 	.volatile_reg = rt5651_volatile_register,
211940bc18a2SBard Liao 	.readable_reg = rt5651_readable_register,
212040bc18a2SBard Liao 
212140bc18a2SBard Liao 	.cache_type = REGCACHE_RBTREE,
212240bc18a2SBard Liao 	.reg_defaults = rt5651_reg,
212340bc18a2SBard Liao 	.num_reg_defaults = ARRAY_SIZE(rt5651_reg),
212440bc18a2SBard Liao 	.ranges = rt5651_ranges,
212540bc18a2SBard Liao 	.num_ranges = ARRAY_SIZE(rt5651_ranges),
21261c96a2f6SDavid Frey 	.use_single_read = true,
21271c96a2f6SDavid Frey 	.use_single_write = true,
212840bc18a2SBard Liao };
212940bc18a2SBard Liao 
21303ae08dc0SBard Liao #if defined(CONFIG_OF)
21313ae08dc0SBard Liao static const struct of_device_id rt5651_of_match[] = {
21323ae08dc0SBard Liao 	{ .compatible = "realtek,rt5651", },
21333ae08dc0SBard Liao 	{},
21343ae08dc0SBard Liao };
21353ae08dc0SBard Liao MODULE_DEVICE_TABLE(of, rt5651_of_match);
21363ae08dc0SBard Liao #endif
21373ae08dc0SBard Liao 
21383ae08dc0SBard Liao #ifdef CONFIG_ACPI
21393ae08dc0SBard Liao static const struct acpi_device_id rt5651_acpi_match[] = {
21403ae08dc0SBard Liao 	{ "10EC5651", 0 },
2141*d3068735SHans de Goede 	{ "10EC5640", 0 },
21423ae08dc0SBard Liao 	{ },
21433ae08dc0SBard Liao };
21443ae08dc0SBard Liao MODULE_DEVICE_TABLE(acpi, rt5651_acpi_match);
21453ae08dc0SBard Liao #endif
21463ae08dc0SBard Liao 
214740bc18a2SBard Liao static const struct i2c_device_id rt5651_i2c_id[] = {
214840bc18a2SBard Liao 	{ "rt5651", 0 },
214940bc18a2SBard Liao 	{ }
215040bc18a2SBard Liao };
215140bc18a2SBard Liao MODULE_DEVICE_TABLE(i2c, rt5651_i2c_id);
215240bc18a2SBard Liao 
21531cf5b504SHans de Goede /*
21541cf5b504SHans de Goede  * Note this function MUST not look at device-properties, see the comment
21551cf5b504SHans de Goede  * above rt5651_apply_properties().
21561cf5b504SHans de Goede  */
215740bc18a2SBard Liao static int rt5651_i2c_probe(struct i2c_client *i2c,
215840bc18a2SBard Liao 		    const struct i2c_device_id *id)
215940bc18a2SBard Liao {
216040bc18a2SBard Liao 	struct rt5651_priv *rt5651;
216140bc18a2SBard Liao 	int ret;
216240bc18a2SBard Liao 
216340bc18a2SBard Liao 	rt5651 = devm_kzalloc(&i2c->dev, sizeof(*rt5651),
216440bc18a2SBard Liao 				GFP_KERNEL);
216540bc18a2SBard Liao 	if (NULL == rt5651)
216640bc18a2SBard Liao 		return -ENOMEM;
216740bc18a2SBard Liao 
216840bc18a2SBard Liao 	i2c_set_clientdata(i2c, rt5651);
216940bc18a2SBard Liao 
217040bc18a2SBard Liao 	rt5651->regmap = devm_regmap_init_i2c(i2c, &rt5651_regmap);
217140bc18a2SBard Liao 	if (IS_ERR(rt5651->regmap)) {
217240bc18a2SBard Liao 		ret = PTR_ERR(rt5651->regmap);
217340bc18a2SBard Liao 		dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
217440bc18a2SBard Liao 			ret);
217540bc18a2SBard Liao 		return ret;
217640bc18a2SBard Liao 	}
217740bc18a2SBard Liao 
217840bc18a2SBard Liao 	regmap_read(rt5651->regmap, RT5651_DEVICE_ID, &ret);
217940bc18a2SBard Liao 	if (ret != RT5651_DEVICE_ID_VALUE) {
218040bc18a2SBard Liao 		dev_err(&i2c->dev,
2181469444fbSJarkko Nikula 			"Device with ID register %#x is not rt5651\n", ret);
218240bc18a2SBard Liao 		return -ENODEV;
218340bc18a2SBard Liao 	}
218440bc18a2SBard Liao 
218540bc18a2SBard Liao 	regmap_write(rt5651->regmap, RT5651_RESET, 0);
218640bc18a2SBard Liao 
218740bc18a2SBard Liao 	ret = regmap_register_patch(rt5651->regmap, init_list,
218840bc18a2SBard Liao 				    ARRAY_SIZE(init_list));
218940bc18a2SBard Liao 	if (ret != 0)
219040bc18a2SBard Liao 		dev_warn(&i2c->dev, "Failed to apply regmap patch: %d\n", ret);
219140bc18a2SBard Liao 
2192f06da4fdSHans de Goede 	rt5651->irq = i2c->irq;
2193577dc32fSPierre-Louis Bossart 	rt5651->hp_mute = true;
219440bc18a2SBard Liao 
2195df1569f2SHans de Goede 	INIT_DELAYED_WORK(&rt5651->bp_work, rt5651_button_press_work);
2196ee680968SHans de Goede 	INIT_WORK(&rt5651->jack_detect_work, rt5651_jack_detect_work);
219780bbe4a3SCarlo Caione 
21988d2d7bcdSHans de Goede 	/* Make sure work is stopped on probe-error / remove */
21998d2d7bcdSHans de Goede 	ret = devm_add_action_or_reset(&i2c->dev, rt5651_cancel_work, rt5651);
22008d2d7bcdSHans de Goede 	if (ret)
22018d2d7bcdSHans de Goede 		return ret;
22028d2d7bcdSHans de Goede 
220334c906ddSHans de Goede 	ret = devm_request_irq(&i2c->dev, rt5651->irq, rt5651_irq,
220434c906ddSHans de Goede 			       IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING
220534c906ddSHans de Goede 			       | IRQF_ONESHOT, "rt5651", rt5651);
220634c906ddSHans de Goede 	if (ret == 0) {
220734c906ddSHans de Goede 		/* Gets re-enabled by rt5651_set_jack() */
220834c906ddSHans de Goede 		disable_irq(rt5651->irq);
220934c906ddSHans de Goede 	} else {
221034c906ddSHans de Goede 		dev_warn(&i2c->dev, "Failed to reguest IRQ %d: %d\n",
221134c906ddSHans de Goede 			 rt5651->irq, ret);
221234c906ddSHans de Goede 		rt5651->irq = -ENXIO;
221334c906ddSHans de Goede 	}
221434c906ddSHans de Goede 
221517b52010SKuninori Morimoto 	ret = devm_snd_soc_register_component(&i2c->dev,
221617b52010SKuninori Morimoto 				&soc_component_dev_rt5651,
221740bc18a2SBard Liao 				rt5651_dai, ARRAY_SIZE(rt5651_dai));
221840bc18a2SBard Liao 
221940bc18a2SBard Liao 	return ret;
222040bc18a2SBard Liao }
222140bc18a2SBard Liao 
2222871c131dSMark Brown static struct i2c_driver rt5651_i2c_driver = {
222340bc18a2SBard Liao 	.driver = {
222440bc18a2SBard Liao 		.name = "rt5651",
22253ae08dc0SBard Liao 		.acpi_match_table = ACPI_PTR(rt5651_acpi_match),
22263ae08dc0SBard Liao 		.of_match_table = of_match_ptr(rt5651_of_match),
222740bc18a2SBard Liao 	},
222840bc18a2SBard Liao 	.probe = rt5651_i2c_probe,
222940bc18a2SBard Liao 	.id_table = rt5651_i2c_id,
223040bc18a2SBard Liao };
223140bc18a2SBard Liao module_i2c_driver(rt5651_i2c_driver);
223240bc18a2SBard Liao 
223340bc18a2SBard Liao MODULE_DESCRIPTION("ASoC RT5651 driver");
223440bc18a2SBard Liao MODULE_AUTHOR("Bard Liao <bardliao@realtek.com>");
223540bc18a2SBard Liao MODULE_LICENSE("GPL v2");
2236