xref: /linux/sound/soc/codecs/cpcap.c (revision 5b4288792ff246cf2bda0c81cebcc02d1f631ca3)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * ALSA SoC CPCAP codec driver
4  *
5  * Copyright (C) 2017 - 2018 Sebastian Reichel <sre@kernel.org>
6  *
7  * Very loosely based on original driver from Motorola:
8  * Copyright (C) 2007 - 2009 Motorola, Inc.
9  */
10 
11 #include <linux/module.h>
12 #include <linux/regmap.h>
13 #include <linux/platform_device.h>
14 #include <linux/regulator/consumer.h>
15 #include <linux/mfd/motorola-cpcap.h>
16 #include <sound/core.h>
17 #include <sound/soc.h>
18 #include <sound/tlv.h>
19 
20 /* Register 512 CPCAP_REG_VAUDIOC --- Audio Regulator and Bias Voltage */
21 #define CPCAP_BIT_AUDIO_LOW_PWR           6
22 #define CPCAP_BIT_AUD_LOWPWR_SPEED        5
23 #define CPCAP_BIT_VAUDIOPRISTBY           4
24 #define CPCAP_BIT_VAUDIO_MODE1            2
25 #define CPCAP_BIT_VAUDIO_MODE0            1
26 #define CPCAP_BIT_V_AUDIO_EN              0
27 
28 /* Register 513 CPCAP_REG_CC     --- CODEC */
29 #define CPCAP_BIT_CDC_CLK2                15
30 #define CPCAP_BIT_CDC_CLK1                14
31 #define CPCAP_BIT_CDC_CLK0                13
32 #define CPCAP_BIT_CDC_SR3                 12
33 #define CPCAP_BIT_CDC_SR2                 11
34 #define CPCAP_BIT_CDC_SR1                 10
35 #define CPCAP_BIT_CDC_SR0                 9
36 #define CPCAP_BIT_CDC_CLOCK_TREE_RESET    8
37 #define CPCAP_BIT_MIC2_CDC_EN             7
38 #define CPCAP_BIT_CDC_EN_RX               6
39 #define CPCAP_BIT_DF_RESET                5
40 #define CPCAP_BIT_MIC1_CDC_EN             4
41 #define CPCAP_BIT_AUDOHPF_1		  3
42 #define CPCAP_BIT_AUDOHPF_0		  2
43 #define CPCAP_BIT_AUDIHPF_1		  1
44 #define CPCAP_BIT_AUDIHPF_0		  0
45 
46 /* Register 514 CPCAP_REG_CDI    --- CODEC Digital Audio Interface */
47 #define CPCAP_BIT_CDC_PLL_SEL             15
48 #define CPCAP_BIT_CLK_IN_SEL              13
49 #define CPCAP_BIT_DIG_AUD_IN              12
50 #define CPCAP_BIT_CDC_CLK_EN              11
51 #define CPCAP_BIT_CDC_DIG_AUD_FS1         10
52 #define CPCAP_BIT_CDC_DIG_AUD_FS0         9
53 #define CPCAP_BIT_MIC2_TIMESLOT2          8
54 #define CPCAP_BIT_MIC2_TIMESLOT1          7
55 #define CPCAP_BIT_MIC2_TIMESLOT0          6
56 #define CPCAP_BIT_MIC1_RX_TIMESLOT2       5
57 #define CPCAP_BIT_MIC1_RX_TIMESLOT1       4
58 #define CPCAP_BIT_MIC1_RX_TIMESLOT0       3
59 #define CPCAP_BIT_FS_INV                  2
60 #define CPCAP_BIT_CLK_INV                 1
61 #define CPCAP_BIT_SMB_CDC                 0
62 
63 /* Register 515 CPCAP_REG_SDAC   --- Stereo DAC */
64 #define CPCAP_BIT_FSYNC_CLK_IN_COMMON     11
65 #define CPCAP_BIT_SLAVE_PLL_CLK_INPUT     10
66 #define CPCAP_BIT_ST_CLOCK_TREE_RESET     9
67 #define CPCAP_BIT_DF_RESET_ST_DAC         8
68 #define CPCAP_BIT_ST_SR3                  7
69 #define CPCAP_BIT_ST_SR2                  6
70 #define CPCAP_BIT_ST_SR1                  5
71 #define CPCAP_BIT_ST_SR0                  4
72 #define CPCAP_BIT_ST_DAC_CLK2             3
73 #define CPCAP_BIT_ST_DAC_CLK1             2
74 #define CPCAP_BIT_ST_DAC_CLK0             1
75 #define CPCAP_BIT_ST_DAC_EN               0
76 
77 /* Register 516 CPCAP_REG_SDACDI --- Stereo DAC Digital Audio Interface */
78 #define CPCAP_BIT_ST_L_TIMESLOT2          13
79 #define CPCAP_BIT_ST_L_TIMESLOT1          12
80 #define CPCAP_BIT_ST_L_TIMESLOT0          11
81 #define CPCAP_BIT_ST_R_TIMESLOT2          10
82 #define CPCAP_BIT_ST_R_TIMESLOT1          9
83 #define CPCAP_BIT_ST_R_TIMESLOT0          8
84 #define CPCAP_BIT_ST_DAC_CLK_IN_SEL       7
85 #define CPCAP_BIT_ST_FS_INV               6
86 #define CPCAP_BIT_ST_CLK_INV              5
87 #define CPCAP_BIT_ST_DIG_AUD_FS1          4
88 #define CPCAP_BIT_ST_DIG_AUD_FS0          3
89 #define CPCAP_BIT_DIG_AUD_IN_ST_DAC       2
90 #define CPCAP_BIT_ST_CLK_EN               1
91 #define CPCAP_BIT_SMB_ST_DAC              0
92 
93 /* Register 517 CPCAP_REG_TXI    --- TX Interface */
94 #define CPCAP_BIT_PTT_TH		15
95 #define CPCAP_BIT_PTT_CMP_EN		14
96 #define CPCAP_BIT_HS_ID_TX		13
97 #define CPCAP_BIT_MB_ON2		12
98 #define CPCAP_BIT_MB_ON1L		11
99 #define CPCAP_BIT_MB_ON1R		10
100 #define CPCAP_BIT_RX_L_ENCODE		9
101 #define CPCAP_BIT_RX_R_ENCODE		8
102 #define CPCAP_BIT_MIC2_MUX		7
103 #define CPCAP_BIT_MIC2_PGA_EN		6
104 #define CPCAP_BIT_CDET_DIS		5
105 #define CPCAP_BIT_EMU_MIC_MUX		4
106 #define CPCAP_BIT_HS_MIC_MUX		3
107 #define CPCAP_BIT_MIC1_MUX		2
108 #define CPCAP_BIT_MIC1_PGA_EN		1
109 #define CPCAP_BIT_DLM			0
110 
111 /* Register 518 CPCAP_REG_TXMP   --- Mic Gain */
112 #define CPCAP_BIT_MB_BIAS_R1              11
113 #define CPCAP_BIT_MB_BIAS_R0              10
114 #define CPCAP_BIT_MIC2_GAIN_4             9
115 #define CPCAP_BIT_MIC2_GAIN_3             8
116 #define CPCAP_BIT_MIC2_GAIN_2             7
117 #define CPCAP_BIT_MIC2_GAIN_1             6
118 #define CPCAP_BIT_MIC2_GAIN_0             5
119 #define CPCAP_BIT_MIC1_GAIN_4             4
120 #define CPCAP_BIT_MIC1_GAIN_3             3
121 #define CPCAP_BIT_MIC1_GAIN_2             2
122 #define CPCAP_BIT_MIC1_GAIN_1             1
123 #define CPCAP_BIT_MIC1_GAIN_0             0
124 
125 /* Register 519 CPCAP_REG_RXOA   --- RX Output Amplifier */
126 #define CPCAP_BIT_UNUSED_519_15		15
127 #define CPCAP_BIT_UNUSED_519_14		14
128 #define CPCAP_BIT_UNUSED_519_13		13
129 #define CPCAP_BIT_STDAC_LOW_PWR_DISABLE	12
130 #define CPCAP_BIT_HS_LOW_PWR		11
131 #define CPCAP_BIT_HS_ID_RX		10
132 #define CPCAP_BIT_ST_HS_CP_EN		9
133 #define CPCAP_BIT_EMU_SPKR_R_EN		8
134 #define CPCAP_BIT_EMU_SPKR_L_EN		7
135 #define CPCAP_BIT_HS_L_EN		6
136 #define CPCAP_BIT_HS_R_EN		5
137 #define CPCAP_BIT_A4_LINEOUT_L_EN	4
138 #define CPCAP_BIT_A4_LINEOUT_R_EN	3
139 #define CPCAP_BIT_A2_LDSP_L_EN		2
140 #define CPCAP_BIT_A2_LDSP_R_EN		1
141 #define CPCAP_BIT_A1_EAR_EN		0
142 
143 /* Register 520 CPCAP_REG_RXVC   --- RX Volume Control */
144 #define CPCAP_BIT_VOL_EXT3                15
145 #define CPCAP_BIT_VOL_EXT2                14
146 #define CPCAP_BIT_VOL_EXT1                13
147 #define CPCAP_BIT_VOL_EXT0                12
148 #define CPCAP_BIT_VOL_DAC3                11
149 #define CPCAP_BIT_VOL_DAC2                10
150 #define CPCAP_BIT_VOL_DAC1                9
151 #define CPCAP_BIT_VOL_DAC0                8
152 #define CPCAP_BIT_VOL_DAC_LSB_1dB1        7
153 #define CPCAP_BIT_VOL_DAC_LSB_1dB0        6
154 #define CPCAP_BIT_VOL_CDC3                5
155 #define CPCAP_BIT_VOL_CDC2                4
156 #define CPCAP_BIT_VOL_CDC1                3
157 #define CPCAP_BIT_VOL_CDC0                2
158 #define CPCAP_BIT_VOL_CDC_LSB_1dB1        1
159 #define CPCAP_BIT_VOL_CDC_LSB_1dB0        0
160 
161 /* Register 521 CPCAP_REG_RXCOA  --- Codec to Output Amp Switches */
162 #define CPCAP_BIT_PGA_CDC_EN              10
163 #define CPCAP_BIT_CDC_SW                  9
164 #define CPCAP_BIT_PGA_OUTR_USBDP_CDC_SW   8
165 #define CPCAP_BIT_PGA_OUTL_USBDN_CDC_SW   7
166 #define CPCAP_BIT_ALEFT_HS_CDC_SW         6
167 #define CPCAP_BIT_ARIGHT_HS_CDC_SW        5
168 #define CPCAP_BIT_A4_LINEOUT_L_CDC_SW     4
169 #define CPCAP_BIT_A4_LINEOUT_R_CDC_SW     3
170 #define CPCAP_BIT_A2_LDSP_L_CDC_SW        2
171 #define CPCAP_BIT_A2_LDSP_R_CDC_SW        1
172 #define CPCAP_BIT_A1_EAR_CDC_SW           0
173 
174 /* Register 522 CPCAP_REG_RXSDOA --- RX Stereo DAC to Output Amp Switches */
175 #define CPCAP_BIT_PGA_DAC_EN              12
176 #define CPCAP_BIT_ST_DAC_SW               11
177 #define CPCAP_BIT_MONO_DAC1               10
178 #define CPCAP_BIT_MONO_DAC0               9
179 #define CPCAP_BIT_PGA_OUTR_USBDP_DAC_SW   8
180 #define CPCAP_BIT_PGA_OUTL_USBDN_DAC_SW   7
181 #define CPCAP_BIT_ALEFT_HS_DAC_SW         6
182 #define CPCAP_BIT_ARIGHT_HS_DAC_SW        5
183 #define CPCAP_BIT_A4_LINEOUT_L_DAC_SW     4
184 #define CPCAP_BIT_A4_LINEOUT_R_DAC_SW     3
185 #define CPCAP_BIT_A2_LDSP_L_DAC_SW        2
186 #define CPCAP_BIT_A2_LDSP_R_DAC_SW        1
187 #define CPCAP_BIT_A1_EAR_DAC_SW           0
188 
189 /* Register 523 CPCAP_REG_RXEPOA --- RX External PGA to Output Amp Switches */
190 #define CPCAP_BIT_PGA_EXT_L_EN            14
191 #define CPCAP_BIT_PGA_EXT_R_EN            13
192 #define CPCAP_BIT_PGA_IN_L_SW             12
193 #define CPCAP_BIT_PGA_IN_R_SW             11
194 #define CPCAP_BIT_MONO_EXT1               10
195 #define CPCAP_BIT_MONO_EXT0               9
196 #define CPCAP_BIT_PGA_OUTR_USBDP_EXT_SW   8
197 #define CPCAP_BIT_PGA_OUTL_USBDN_EXT_SW   7
198 #define CPCAP_BIT_ALEFT_HS_EXT_SW         6
199 #define CPCAP_BIT_ARIGHT_HS_EXT_SW        5
200 #define CPCAP_BIT_A4_LINEOUT_L_EXT_SW     4
201 #define CPCAP_BIT_A4_LINEOUT_R_EXT_SW     3
202 #define CPCAP_BIT_A2_LDSP_L_EXT_SW        2
203 #define CPCAP_BIT_A2_LDSP_R_EXT_SW        1
204 #define CPCAP_BIT_A1_EAR_EXT_SW           0
205 
206 /* Register 525 CPCAP_REG_A2LA --- SPK Amplifier and Clock Config for Headset */
207 #define CPCAP_BIT_NCP_CLK_SYNC            7
208 #define CPCAP_BIT_A2_CLK_SYNC             6
209 #define CPCAP_BIT_A2_FREE_RUN             5
210 #define CPCAP_BIT_A2_CLK2                 4
211 #define CPCAP_BIT_A2_CLK1                 3
212 #define CPCAP_BIT_A2_CLK0                 2
213 #define CPCAP_BIT_A2_CLK_IN               1
214 #define CPCAP_BIT_A2_CONFIG               0
215 
216 #define SLEEP_ACTIVATE_POWER 2
217 #define CLOCK_TREE_RESET_TIME 1
218 
219 /* constants for ST delay workaround */
220 #define STM_STDAC_ACTIVATE_RAMP_TIME   1
221 #define STM_STDAC_EN_TEST_PRE          0x090C
222 #define STM_STDAC_EN_TEST_POST         0x0000
223 #define STM_STDAC_EN_ST_TEST1_PRE      0x2400
224 #define STM_STDAC_EN_ST_TEST1_POST     0x0400
225 
226 struct cpcap_reg_info {
227 	u16 reg;
228 	u16 mask;
229 	u16 val;
230 };
231 
232 static const struct cpcap_reg_info cpcap_default_regs[] = {
233 	{ CPCAP_REG_VAUDIOC, 0x003F, 0x0000 },
234 	{ CPCAP_REG_CC, 0xFFFF, 0x0000 },
235 	{ CPCAP_REG_CC, 0xFFFF, 0x0000 },
236 	{ CPCAP_REG_CDI, 0xBFFF, 0x0000 },
237 	{ CPCAP_REG_SDAC, 0x0FFF, 0x0000 },
238 	{ CPCAP_REG_SDACDI, 0x3FFF, 0x0000 },
239 	{ CPCAP_REG_TXI, 0x0FDF, 0x0000 },
240 	{ CPCAP_REG_TXMP, 0x0FFF, 0x0400 },
241 	{ CPCAP_REG_RXOA, 0x01FF, 0x0000 },
242 	{ CPCAP_REG_RXVC, 0xFF3C, 0x0000 },
243 	{ CPCAP_REG_RXCOA, 0x07FF, 0x0000 },
244 	{ CPCAP_REG_RXSDOA, 0x1FFF, 0x0000 },
245 	{ CPCAP_REG_RXEPOA, 0x7FFF, 0x0000 },
246 	{ CPCAP_REG_A2LA, BIT(CPCAP_BIT_A2_FREE_RUN),
247 	  BIT(CPCAP_BIT_A2_FREE_RUN) },
248 };
249 
250 enum cpcap_dai {
251 	CPCAP_DAI_HIFI,
252 	CPCAP_DAI_VOICE,
253 };
254 
255 struct cpcap_audio {
256 	struct snd_soc_component *component;
257 	struct regmap *regmap;
258 
259 	u16 vendor;
260 
261 	int codec_clk_id;
262 	int codec_freq;
263 	int codec_format;
264 	struct regulator *vaudio;
265 };
266 
267 static int cpcap_st_workaround(struct snd_soc_dapm_widget *w,
268 			       struct snd_kcontrol *kcontrol, int event)
269 {
270 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
271 	struct cpcap_audio *cpcap = snd_soc_component_get_drvdata(component);
272 	int err = 0;
273 
274 	/* Only CPCAP from ST requires workaround */
275 	if (cpcap->vendor != CPCAP_VENDOR_ST)
276 		return 0;
277 
278 	switch (event) {
279 	case SND_SOC_DAPM_PRE_PMU:
280 		err = regmap_write(cpcap->regmap, CPCAP_REG_TEST,
281 				   STM_STDAC_EN_TEST_PRE);
282 		if (err)
283 			return err;
284 		err = regmap_write(cpcap->regmap, CPCAP_REG_ST_TEST1,
285 				   STM_STDAC_EN_ST_TEST1_PRE);
286 		break;
287 	case SND_SOC_DAPM_POST_PMU:
288 		msleep(STM_STDAC_ACTIVATE_RAMP_TIME);
289 
290 		err = regmap_write(cpcap->regmap, CPCAP_REG_ST_TEST1,
291 				   STM_STDAC_EN_ST_TEST1_POST);
292 		if (err)
293 			return err;
294 		err = regmap_write(cpcap->regmap, CPCAP_REG_TEST,
295 				   STM_STDAC_EN_TEST_POST);
296 		break;
297 	default:
298 		break;
299 	}
300 
301 	return err;
302 }
303 
304 /* Capture Gain Control: 0dB to 31dB in 1dB steps */
305 static const DECLARE_TLV_DB_SCALE(mic_gain_tlv, 0, 100, 0);
306 
307 /* Playback Gain Control: -33dB to 12dB in 3dB steps */
308 static const DECLARE_TLV_DB_SCALE(vol_tlv, -3300, 300, 0);
309 
310 static const struct snd_kcontrol_new cpcap_snd_controls[] = {
311 	/* Playback Gain */
312 	SOC_SINGLE_TLV("HiFi Playback Volume",
313 		CPCAP_REG_RXVC, CPCAP_BIT_VOL_DAC0, 0xF, 0, vol_tlv),
314 	SOC_SINGLE_TLV("Voice Playback Volume",
315 		CPCAP_REG_RXVC, CPCAP_BIT_VOL_CDC0, 0xF, 0, vol_tlv),
316 	SOC_SINGLE_TLV("Ext Playback Volume",
317 		CPCAP_REG_RXVC, CPCAP_BIT_VOL_EXT0, 0xF, 0, vol_tlv),
318 
319 	/* Capture Gain */
320 	SOC_SINGLE_TLV("Mic1 Capture Volume",
321 		CPCAP_REG_TXMP, CPCAP_BIT_MIC1_GAIN_0, 0x1F, 0, mic_gain_tlv),
322 	SOC_SINGLE_TLV("Mic2 Capture Volume",
323 		CPCAP_REG_TXMP, CPCAP_BIT_MIC2_GAIN_0, 0x1F, 0, mic_gain_tlv),
324 
325 	/* Phase Invert */
326 	SOC_SINGLE("Hifi Left Phase Invert Switch",
327 		CPCAP_REG_RXSDOA, CPCAP_BIT_MONO_DAC0, 1, 0),
328 	SOC_SINGLE("Ext Left Phase Invert Switch",
329 		CPCAP_REG_RXEPOA, CPCAP_BIT_MONO_EXT0, 1, 0),
330 };
331 
332 static const char * const cpcap_out_mux_texts[] = {
333 	"Off", "Voice", "HiFi", "Ext"
334 };
335 
336 static const char * const cpcap_in_right_mux_texts[] = {
337 	"Off", "Mic 1", "Headset Mic", "EMU Mic", "Ext Right"
338 };
339 
340 static const char * const cpcap_in_left_mux_texts[] = {
341 	"Off", "Mic 2", "Ext Left"
342 };
343 
344 /*
345  * input muxes use unusual register layout, so that we need to use custom
346  * getter/setter methods
347  */
348 static SOC_ENUM_SINGLE_EXT_DECL(cpcap_input_left_mux_enum,
349 				cpcap_in_left_mux_texts);
350 static SOC_ENUM_SINGLE_EXT_DECL(cpcap_input_right_mux_enum,
351 				cpcap_in_right_mux_texts);
352 
353 /*
354  * mux uses same bit in CPCAP_REG_RXCOA, CPCAP_REG_RXSDOA & CPCAP_REG_RXEPOA;
355  * even though the register layout makes it look like a mixer, this is a mux.
356  * Enabling multiple inputs will result in no audio being forwarded.
357  */
358 static SOC_ENUM_SINGLE_DECL(cpcap_earpiece_mux_enum, 0, 0, cpcap_out_mux_texts);
359 static SOC_ENUM_SINGLE_DECL(cpcap_spkr_r_mux_enum, 0, 1, cpcap_out_mux_texts);
360 static SOC_ENUM_SINGLE_DECL(cpcap_spkr_l_mux_enum, 0, 2, cpcap_out_mux_texts);
361 static SOC_ENUM_SINGLE_DECL(cpcap_line_r_mux_enum, 0, 3, cpcap_out_mux_texts);
362 static SOC_ENUM_SINGLE_DECL(cpcap_line_l_mux_enum, 0, 4, cpcap_out_mux_texts);
363 static SOC_ENUM_SINGLE_DECL(cpcap_hs_r_mux_enum, 0, 5, cpcap_out_mux_texts);
364 static SOC_ENUM_SINGLE_DECL(cpcap_hs_l_mux_enum, 0, 6, cpcap_out_mux_texts);
365 static SOC_ENUM_SINGLE_DECL(cpcap_emu_l_mux_enum, 0, 7, cpcap_out_mux_texts);
366 static SOC_ENUM_SINGLE_DECL(cpcap_emu_r_mux_enum, 0, 8, cpcap_out_mux_texts);
367 
368 static int cpcap_output_mux_get_enum(struct snd_kcontrol *kcontrol,
369 				     struct snd_ctl_elem_value *ucontrol)
370 {
371 	struct snd_soc_component *component = snd_soc_dapm_kcontrol_component(kcontrol);
372 	struct cpcap_audio *cpcap = snd_soc_component_get_drvdata(component);
373 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
374 	unsigned int shift = e->shift_l;
375 	int reg_voice, reg_hifi, reg_ext, status;
376 	int err;
377 
378 	err = regmap_read(cpcap->regmap, CPCAP_REG_RXCOA, &reg_voice);
379 	if (err)
380 		return err;
381 	err = regmap_read(cpcap->regmap, CPCAP_REG_RXSDOA, &reg_hifi);
382 	if (err)
383 		return err;
384 	err = regmap_read(cpcap->regmap, CPCAP_REG_RXEPOA, &reg_ext);
385 	if (err)
386 		return err;
387 
388 	reg_voice = (reg_voice >> shift) & 1;
389 	reg_hifi = (reg_hifi >> shift) & 1;
390 	reg_ext = (reg_ext >> shift) & 1;
391 	status = reg_ext << 2 | reg_hifi << 1 | reg_voice;
392 
393 	switch (status) {
394 	case 0x04:
395 		ucontrol->value.enumerated.item[0] = 3;
396 		break;
397 	case 0x02:
398 		ucontrol->value.enumerated.item[0] = 2;
399 		break;
400 	case 0x01:
401 		ucontrol->value.enumerated.item[0] = 1;
402 		break;
403 	default:
404 		ucontrol->value.enumerated.item[0] = 0;
405 		break;
406 	}
407 
408 	return 0;
409 }
410 
411 static int cpcap_output_mux_put_enum(struct snd_kcontrol *kcontrol,
412 				     struct snd_ctl_elem_value *ucontrol)
413 {
414 	struct snd_soc_component *component = snd_soc_dapm_kcontrol_component(kcontrol);
415 	struct cpcap_audio *cpcap = snd_soc_component_get_drvdata(component);
416 	struct snd_soc_dapm_context *dapm =
417 		snd_soc_dapm_kcontrol_dapm(kcontrol);
418 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
419 	unsigned int muxval = ucontrol->value.enumerated.item[0];
420 	unsigned int mask = BIT(e->shift_l);
421 	u16 reg_voice = 0x00, reg_hifi = 0x00, reg_ext = 0x00;
422 	int err;
423 
424 	switch (muxval) {
425 	case 1:
426 		reg_voice = mask;
427 		break;
428 	case 2:
429 		reg_hifi = mask;
430 		break;
431 	case 3:
432 		reg_ext = mask;
433 		break;
434 	default:
435 		break;
436 	}
437 
438 	err = regmap_update_bits(cpcap->regmap, CPCAP_REG_RXCOA,
439 				 mask, reg_voice);
440 	if (err)
441 		return err;
442 	err = regmap_update_bits(cpcap->regmap, CPCAP_REG_RXSDOA,
443 				 mask, reg_hifi);
444 	if (err)
445 		return err;
446 	err = regmap_update_bits(cpcap->regmap, CPCAP_REG_RXEPOA,
447 				 mask, reg_ext);
448 	if (err)
449 		return err;
450 
451 	snd_soc_dapm_mux_update_power(dapm, kcontrol, muxval, e, NULL);
452 
453 	return 0;
454 }
455 
456 static int cpcap_input_right_mux_get_enum(struct snd_kcontrol *kcontrol,
457 					  struct snd_ctl_elem_value *ucontrol)
458 {
459 	struct snd_soc_component *component = snd_soc_dapm_kcontrol_component(kcontrol);
460 	struct cpcap_audio *cpcap = snd_soc_component_get_drvdata(component);
461 	int regval, mask;
462 	int err;
463 
464 	err = regmap_read(cpcap->regmap, CPCAP_REG_TXI, &regval);
465 	if (err)
466 		return err;
467 
468 	mask = 0;
469 	mask |= BIT(CPCAP_BIT_MIC1_MUX);
470 	mask |= BIT(CPCAP_BIT_HS_MIC_MUX);
471 	mask |= BIT(CPCAP_BIT_EMU_MIC_MUX);
472 	mask |= BIT(CPCAP_BIT_RX_R_ENCODE);
473 
474 	switch (regval & mask) {
475 	case BIT(CPCAP_BIT_RX_R_ENCODE):
476 		ucontrol->value.enumerated.item[0] = 4;
477 		break;
478 	case BIT(CPCAP_BIT_EMU_MIC_MUX):
479 		ucontrol->value.enumerated.item[0] = 3;
480 		break;
481 	case BIT(CPCAP_BIT_HS_MIC_MUX):
482 		ucontrol->value.enumerated.item[0] = 2;
483 		break;
484 	case BIT(CPCAP_BIT_MIC1_MUX):
485 		ucontrol->value.enumerated.item[0] = 1;
486 		break;
487 	default:
488 		ucontrol->value.enumerated.item[0] = 0;
489 		break;
490 	}
491 
492 	return 0;
493 }
494 
495 static int cpcap_input_right_mux_put_enum(struct snd_kcontrol *kcontrol,
496 					  struct snd_ctl_elem_value *ucontrol)
497 {
498 	struct snd_soc_component *component = snd_soc_dapm_kcontrol_component(kcontrol);
499 	struct cpcap_audio *cpcap = snd_soc_component_get_drvdata(component);
500 	struct snd_soc_dapm_context *dapm =
501 		snd_soc_dapm_kcontrol_dapm(kcontrol);
502 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
503 	unsigned int muxval = ucontrol->value.enumerated.item[0];
504 	int regval = 0, mask;
505 	int err;
506 
507 	mask = 0;
508 	mask |= BIT(CPCAP_BIT_MIC1_MUX);
509 	mask |= BIT(CPCAP_BIT_HS_MIC_MUX);
510 	mask |= BIT(CPCAP_BIT_EMU_MIC_MUX);
511 	mask |= BIT(CPCAP_BIT_RX_R_ENCODE);
512 
513 	switch (muxval) {
514 	case 1:
515 		regval = BIT(CPCAP_BIT_MIC1_MUX);
516 		break;
517 	case 2:
518 		regval = BIT(CPCAP_BIT_HS_MIC_MUX);
519 		break;
520 	case 3:
521 		regval = BIT(CPCAP_BIT_EMU_MIC_MUX);
522 		break;
523 	case 4:
524 		regval = BIT(CPCAP_BIT_RX_R_ENCODE);
525 		break;
526 	default:
527 		break;
528 	}
529 
530 	err = regmap_update_bits(cpcap->regmap, CPCAP_REG_TXI,
531 				 mask, regval);
532 	if (err)
533 		return err;
534 
535 	snd_soc_dapm_mux_update_power(dapm, kcontrol, muxval, e, NULL);
536 
537 	return 0;
538 }
539 
540 static int cpcap_input_left_mux_get_enum(struct snd_kcontrol *kcontrol,
541 					 struct snd_ctl_elem_value *ucontrol)
542 {
543 	struct snd_soc_component *component = snd_soc_dapm_kcontrol_component(kcontrol);
544 	struct cpcap_audio *cpcap = snd_soc_component_get_drvdata(component);
545 	int regval, mask;
546 	int err;
547 
548 	err = regmap_read(cpcap->regmap, CPCAP_REG_TXI, &regval);
549 	if (err)
550 		return err;
551 
552 	mask = 0;
553 	mask |= BIT(CPCAP_BIT_MIC2_MUX);
554 	mask |= BIT(CPCAP_BIT_RX_L_ENCODE);
555 
556 	switch (regval & mask) {
557 	case BIT(CPCAP_BIT_RX_L_ENCODE):
558 		ucontrol->value.enumerated.item[0] = 2;
559 		break;
560 	case BIT(CPCAP_BIT_MIC2_MUX):
561 		ucontrol->value.enumerated.item[0] = 1;
562 		break;
563 	default:
564 		ucontrol->value.enumerated.item[0] = 0;
565 		break;
566 	}
567 
568 	return 0;
569 }
570 
571 static int cpcap_input_left_mux_put_enum(struct snd_kcontrol *kcontrol,
572 					 struct snd_ctl_elem_value *ucontrol)
573 {
574 	struct snd_soc_component *component = snd_soc_dapm_kcontrol_component(kcontrol);
575 	struct cpcap_audio *cpcap = snd_soc_component_get_drvdata(component);
576 	struct snd_soc_dapm_context *dapm =
577 		snd_soc_dapm_kcontrol_dapm(kcontrol);
578 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
579 	unsigned int muxval = ucontrol->value.enumerated.item[0];
580 	int regval = 0, mask;
581 	int err;
582 
583 	mask = 0;
584 	mask |= BIT(CPCAP_BIT_MIC2_MUX);
585 	mask |= BIT(CPCAP_BIT_RX_L_ENCODE);
586 
587 	switch (muxval) {
588 	case 1:
589 		regval = BIT(CPCAP_BIT_MIC2_MUX);
590 		break;
591 	case 2:
592 		regval = BIT(CPCAP_BIT_RX_L_ENCODE);
593 		break;
594 	default:
595 		break;
596 	}
597 
598 	err = regmap_update_bits(cpcap->regmap, CPCAP_REG_TXI,
599 				 mask, regval);
600 	if (err)
601 		return err;
602 
603 	snd_soc_dapm_mux_update_power(dapm, kcontrol, muxval, e, NULL);
604 
605 	return 0;
606 }
607 
608 static const struct snd_kcontrol_new cpcap_input_left_mux =
609 	SOC_DAPM_ENUM_EXT("Input Left", cpcap_input_left_mux_enum,
610 			  cpcap_input_left_mux_get_enum,
611 			  cpcap_input_left_mux_put_enum);
612 static const struct snd_kcontrol_new cpcap_input_right_mux =
613 	SOC_DAPM_ENUM_EXT("Input Right", cpcap_input_right_mux_enum,
614 			  cpcap_input_right_mux_get_enum,
615 			  cpcap_input_right_mux_put_enum);
616 static const struct snd_kcontrol_new cpcap_emu_left_mux =
617 	SOC_DAPM_ENUM_EXT("EMU Left", cpcap_emu_l_mux_enum,
618 			  cpcap_output_mux_get_enum, cpcap_output_mux_put_enum);
619 static const struct snd_kcontrol_new cpcap_emu_right_mux =
620 	SOC_DAPM_ENUM_EXT("EMU Right", cpcap_emu_r_mux_enum,
621 			  cpcap_output_mux_get_enum, cpcap_output_mux_put_enum);
622 static const struct snd_kcontrol_new cpcap_hs_left_mux =
623 	SOC_DAPM_ENUM_EXT("Headset Left", cpcap_hs_l_mux_enum,
624 			  cpcap_output_mux_get_enum, cpcap_output_mux_put_enum);
625 static const struct snd_kcontrol_new cpcap_hs_right_mux =
626 	SOC_DAPM_ENUM_EXT("Headset Right", cpcap_hs_r_mux_enum,
627 			  cpcap_output_mux_get_enum, cpcap_output_mux_put_enum);
628 static const struct snd_kcontrol_new cpcap_line_left_mux =
629 	SOC_DAPM_ENUM_EXT("Line Left", cpcap_line_l_mux_enum,
630 			  cpcap_output_mux_get_enum, cpcap_output_mux_put_enum);
631 static const struct snd_kcontrol_new cpcap_line_right_mux =
632 	SOC_DAPM_ENUM_EXT("Line Right", cpcap_line_r_mux_enum,
633 			  cpcap_output_mux_get_enum, cpcap_output_mux_put_enum);
634 static const struct snd_kcontrol_new cpcap_speaker_left_mux =
635 	SOC_DAPM_ENUM_EXT("Speaker Left", cpcap_spkr_l_mux_enum,
636 			  cpcap_output_mux_get_enum, cpcap_output_mux_put_enum);
637 static const struct snd_kcontrol_new cpcap_speaker_right_mux =
638 	SOC_DAPM_ENUM_EXT("Speaker Right", cpcap_spkr_r_mux_enum,
639 			  cpcap_output_mux_get_enum, cpcap_output_mux_put_enum);
640 static const struct snd_kcontrol_new cpcap_earpiece_mux =
641 	SOC_DAPM_ENUM_EXT("Earpiece", cpcap_earpiece_mux_enum,
642 			  cpcap_output_mux_get_enum, cpcap_output_mux_put_enum);
643 
644 static const struct snd_kcontrol_new cpcap_hifi_mono_mixer_controls[] = {
645 	SOC_DAPM_SINGLE("HiFi Mono Playback Switch",
646 		CPCAP_REG_RXSDOA, CPCAP_BIT_MONO_DAC1, 1, 0),
647 };
648 static const struct snd_kcontrol_new cpcap_ext_mono_mixer_controls[] = {
649 	SOC_DAPM_SINGLE("Ext Mono Playback Switch",
650 		CPCAP_REG_RXEPOA, CPCAP_BIT_MONO_EXT0, 1, 0),
651 };
652 
653 static const struct snd_kcontrol_new cpcap_extr_mute_control =
654 	SOC_DAPM_SINGLE("Switch",
655 		CPCAP_REG_RXEPOA, CPCAP_BIT_PGA_IN_R_SW, 1, 0);
656 static const struct snd_kcontrol_new cpcap_extl_mute_control =
657 	SOC_DAPM_SINGLE("Switch",
658 		CPCAP_REG_RXEPOA, CPCAP_BIT_PGA_IN_L_SW, 1, 0);
659 
660 static const struct snd_kcontrol_new cpcap_voice_loopback =
661 	SOC_DAPM_SINGLE("Switch",
662 		CPCAP_REG_TXI, CPCAP_BIT_DLM, 1, 0);
663 
664 static const struct snd_soc_dapm_widget cpcap_dapm_widgets[] = {
665 	/* DAIs */
666 	SND_SOC_DAPM_AIF_IN("HiFi RX", NULL, 0, SND_SOC_NOPM, 0, 0),
667 	SND_SOC_DAPM_AIF_IN("Voice RX", NULL, 0, SND_SOC_NOPM, 0, 0),
668 	SND_SOC_DAPM_AIF_OUT("Voice TX", NULL, 0, SND_SOC_NOPM, 0, 0),
669 
670 	/* Power Supply */
671 	SND_SOC_DAPM_REGULATOR_SUPPLY("VAUDIO", SLEEP_ACTIVATE_POWER, 0),
672 
673 	/* Highpass Filters */
674 	SND_SOC_DAPM_REG(snd_soc_dapm_pga, "Highpass Filter RX",
675 		CPCAP_REG_CC, CPCAP_BIT_AUDIHPF_0, 0x3, 0x3, 0x0),
676 	SND_SOC_DAPM_REG(snd_soc_dapm_pga, "Highpass Filter TX",
677 		CPCAP_REG_CC, CPCAP_BIT_AUDOHPF_0, 0x3, 0x3, 0x0),
678 
679 	/* Clocks */
680 	SND_SOC_DAPM_SUPPLY("HiFi DAI Clock",
681 		CPCAP_REG_SDACDI, CPCAP_BIT_ST_CLK_EN, 0, NULL, 0),
682 	SND_SOC_DAPM_SUPPLY("Voice DAI Clock",
683 		CPCAP_REG_CDI, CPCAP_BIT_CDC_CLK_EN, 0, NULL, 0),
684 
685 	/* Microphone Bias */
686 	SND_SOC_DAPM_SUPPLY("MIC1R Bias",
687 		CPCAP_REG_TXI, CPCAP_BIT_MB_ON1R, 0, NULL, 0),
688 	SND_SOC_DAPM_SUPPLY("MIC1L Bias",
689 		CPCAP_REG_TXI, CPCAP_BIT_MB_ON1L, 0, NULL, 0),
690 	SND_SOC_DAPM_SUPPLY("MIC2 Bias",
691 		CPCAP_REG_TXI, CPCAP_BIT_MB_ON2, 0, NULL, 0),
692 
693 	/* Inputs */
694 	SND_SOC_DAPM_INPUT("MICR"),
695 	SND_SOC_DAPM_INPUT("HSMIC"),
696 	SND_SOC_DAPM_INPUT("EMUMIC"),
697 	SND_SOC_DAPM_INPUT("MICL"),
698 	SND_SOC_DAPM_INPUT("EXTR"),
699 	SND_SOC_DAPM_INPUT("EXTL"),
700 
701 	/* Capture Route */
702 	SND_SOC_DAPM_MUX("Right Capture Route",
703 		SND_SOC_NOPM, 0, 0, &cpcap_input_right_mux),
704 	SND_SOC_DAPM_MUX("Left Capture Route",
705 		SND_SOC_NOPM, 0, 0, &cpcap_input_left_mux),
706 
707 	/* Capture PGAs */
708 	SND_SOC_DAPM_PGA("Microphone 1 PGA",
709 		CPCAP_REG_TXI, CPCAP_BIT_MIC1_PGA_EN, 0, NULL, 0),
710 	SND_SOC_DAPM_PGA("Microphone 2 PGA",
711 		CPCAP_REG_TXI, CPCAP_BIT_MIC2_PGA_EN, 0, NULL, 0),
712 
713 	/* ADC */
714 	SND_SOC_DAPM_ADC("ADC Right", NULL,
715 		CPCAP_REG_CC, CPCAP_BIT_MIC1_CDC_EN, 0),
716 	SND_SOC_DAPM_ADC("ADC Left", NULL,
717 		CPCAP_REG_CC, CPCAP_BIT_MIC2_CDC_EN, 0),
718 
719 	/* DAC */
720 	SND_SOC_DAPM_DAC_E("DAC HiFi", NULL,
721 		CPCAP_REG_SDAC, CPCAP_BIT_ST_DAC_EN, 0,
722 		cpcap_st_workaround,
723 		SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU),
724 	SND_SOC_DAPM_DAC_E("DAC Voice", NULL,
725 		CPCAP_REG_CC, CPCAP_BIT_CDC_EN_RX, 0,
726 		cpcap_st_workaround,
727 		SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU),
728 
729 	/* Playback PGA */
730 	SND_SOC_DAPM_PGA("HiFi PGA",
731 		CPCAP_REG_RXSDOA, CPCAP_BIT_PGA_DAC_EN, 0, NULL, 0),
732 	SND_SOC_DAPM_PGA("Voice PGA",
733 		CPCAP_REG_RXCOA, CPCAP_BIT_PGA_CDC_EN, 0, NULL, 0),
734 	SND_SOC_DAPM_PGA_E("Ext Right PGA",
735 		CPCAP_REG_RXEPOA, CPCAP_BIT_PGA_EXT_R_EN, 0,
736 		NULL, 0,
737 		cpcap_st_workaround,
738 		SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU),
739 	SND_SOC_DAPM_PGA_E("Ext Left PGA",
740 		CPCAP_REG_RXEPOA, CPCAP_BIT_PGA_EXT_L_EN, 0,
741 		NULL, 0,
742 		cpcap_st_workaround,
743 		SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU),
744 
745 	/* Playback Switch */
746 	SND_SOC_DAPM_SWITCH("Ext Right Enable", SND_SOC_NOPM, 0, 0,
747 		&cpcap_extr_mute_control),
748 	SND_SOC_DAPM_SWITCH("Ext Left Enable", SND_SOC_NOPM, 0, 0,
749 		&cpcap_extl_mute_control),
750 
751 	/* Loopback Switch */
752 	SND_SOC_DAPM_SWITCH("Voice Loopback", SND_SOC_NOPM, 0, 0,
753 		&cpcap_voice_loopback),
754 
755 	/* Mono Mixer */
756 	SOC_MIXER_ARRAY("HiFi Mono Left Mixer", SND_SOC_NOPM, 0, 0,
757 		cpcap_hifi_mono_mixer_controls),
758 	SOC_MIXER_ARRAY("HiFi Mono Right Mixer", SND_SOC_NOPM, 0, 0,
759 		cpcap_hifi_mono_mixer_controls),
760 	SOC_MIXER_ARRAY("Ext Mono Left Mixer", SND_SOC_NOPM, 0, 0,
761 		cpcap_ext_mono_mixer_controls),
762 	SOC_MIXER_ARRAY("Ext Mono Right Mixer", SND_SOC_NOPM, 0, 0,
763 		cpcap_ext_mono_mixer_controls),
764 
765 	/* Output Routes */
766 	SND_SOC_DAPM_MUX("Earpiece Playback Route", SND_SOC_NOPM, 0, 0,
767 		&cpcap_earpiece_mux),
768 	SND_SOC_DAPM_MUX("Speaker Right Playback Route", SND_SOC_NOPM, 0, 0,
769 		&cpcap_speaker_right_mux),
770 	SND_SOC_DAPM_MUX("Speaker Left Playback Route", SND_SOC_NOPM, 0, 0,
771 		&cpcap_speaker_left_mux),
772 	SND_SOC_DAPM_MUX("Lineout Right Playback Route", SND_SOC_NOPM, 0, 0,
773 		&cpcap_line_right_mux),
774 	SND_SOC_DAPM_MUX("Lineout Left Playback Route", SND_SOC_NOPM, 0, 0,
775 		&cpcap_line_left_mux),
776 	SND_SOC_DAPM_MUX("Headset Right Playback Route", SND_SOC_NOPM, 0, 0,
777 		&cpcap_hs_right_mux),
778 	SND_SOC_DAPM_MUX("Headset Left Playback Route", SND_SOC_NOPM, 0, 0,
779 		&cpcap_hs_left_mux),
780 	SND_SOC_DAPM_MUX("EMU Right Playback Route", SND_SOC_NOPM, 0, 0,
781 		&cpcap_emu_right_mux),
782 	SND_SOC_DAPM_MUX("EMU Left Playback Route", SND_SOC_NOPM, 0, 0,
783 		&cpcap_emu_left_mux),
784 
785 	/* Output Amplifier */
786 	SND_SOC_DAPM_PGA("Earpiece PGA",
787 		CPCAP_REG_RXOA, CPCAP_BIT_A1_EAR_EN, 0, NULL, 0),
788 	SND_SOC_DAPM_PGA("Speaker Right PGA",
789 		CPCAP_REG_RXOA, CPCAP_BIT_A2_LDSP_R_EN, 0, NULL, 0),
790 	SND_SOC_DAPM_PGA("Speaker Left PGA",
791 		CPCAP_REG_RXOA, CPCAP_BIT_A2_LDSP_L_EN, 0, NULL, 0),
792 	SND_SOC_DAPM_PGA("Lineout Right PGA",
793 		CPCAP_REG_RXOA, CPCAP_BIT_A4_LINEOUT_R_EN, 0, NULL, 0),
794 	SND_SOC_DAPM_PGA("Lineout Left PGA",
795 		CPCAP_REG_RXOA, CPCAP_BIT_A4_LINEOUT_L_EN, 0, NULL, 0),
796 	SND_SOC_DAPM_PGA("Headset Right PGA",
797 		CPCAP_REG_RXOA, CPCAP_BIT_HS_R_EN, 0, NULL, 0),
798 	SND_SOC_DAPM_PGA("Headset Left PGA",
799 		CPCAP_REG_RXOA, CPCAP_BIT_HS_L_EN, 0, NULL, 0),
800 	SND_SOC_DAPM_PGA("EMU Right PGA",
801 		CPCAP_REG_RXOA, CPCAP_BIT_EMU_SPKR_R_EN, 0, NULL, 0),
802 	SND_SOC_DAPM_PGA("EMU Left PGA",
803 		CPCAP_REG_RXOA, CPCAP_BIT_EMU_SPKR_L_EN, 0, NULL, 0),
804 
805 	/* Headet Charge Pump */
806 	SND_SOC_DAPM_SUPPLY("Headset Charge Pump",
807 		CPCAP_REG_RXOA, CPCAP_BIT_ST_HS_CP_EN, 0, NULL, 0),
808 
809 	/* Outputs */
810 	SND_SOC_DAPM_OUTPUT("EP"),
811 	SND_SOC_DAPM_OUTPUT("SPKR"),
812 	SND_SOC_DAPM_OUTPUT("SPKL"),
813 	SND_SOC_DAPM_OUTPUT("LINER"),
814 	SND_SOC_DAPM_OUTPUT("LINEL"),
815 	SND_SOC_DAPM_OUTPUT("HSR"),
816 	SND_SOC_DAPM_OUTPUT("HSL"),
817 	SND_SOC_DAPM_OUTPUT("EMUR"),
818 	SND_SOC_DAPM_OUTPUT("EMUL"),
819 };
820 
821 static const struct snd_soc_dapm_route intercon[] = {
822 	/* Power Supply */
823 	{"HiFi PGA", NULL, "VAUDIO"},
824 	{"Voice PGA", NULL, "VAUDIO"},
825 	{"Ext Right PGA", NULL, "VAUDIO"},
826 	{"Ext Left PGA", NULL, "VAUDIO"},
827 	{"Microphone 1 PGA", NULL, "VAUDIO"},
828 	{"Microphone 2 PGA", NULL, "VAUDIO"},
829 
830 	/* Stream -> AIF */
831 	{"HiFi RX", NULL, "HiFi Playback"},
832 	{"Voice RX", NULL, "Voice Playback"},
833 	{"Voice Capture", NULL, "Voice TX"},
834 
835 	/* AIF clocks */
836 	{"HiFi RX", NULL, "HiFi DAI Clock"},
837 	{"Voice RX", NULL, "Voice DAI Clock"},
838 	{"Voice TX", NULL, "Voice DAI Clock"},
839 
840 	/* Digital Loopback */
841 	{"Voice Loopback", "Switch", "Voice TX"},
842 	{"Voice RX", NULL, "Voice Loopback"},
843 
844 	/* Highpass Filters */
845 	{"Highpass Filter RX", NULL, "Voice RX"},
846 	{"Voice TX", NULL, "Highpass Filter TX"},
847 
848 	/* AIF -> DAC mapping */
849 	{"DAC HiFi", NULL, "HiFi RX"},
850 	{"DAC Voice", NULL, "Highpass Filter RX"},
851 
852 	/* DAC -> PGA */
853 	{"HiFi PGA", NULL, "DAC HiFi"},
854 	{"Voice PGA", NULL, "DAC Voice"},
855 
856 	/* Ext Input -> PGA */
857 	{"Ext Right PGA", NULL, "EXTR"},
858 	{"Ext Left PGA", NULL, "EXTL"},
859 
860 	/* Ext PGA -> Ext Playback Switch */
861 	{"Ext Right Enable", "Switch", "Ext Right PGA"},
862 	{"Ext Left Enable", "Switch", "Ext Left PGA"},
863 
864 	/* HiFi PGA -> Mono Mixer */
865 	{"HiFi Mono Left Mixer", NULL, "HiFi PGA"},
866 	{"HiFi Mono Left Mixer", "HiFi Mono Playback Switch", "HiFi PGA"},
867 	{"HiFi Mono Right Mixer", NULL, "HiFi PGA"},
868 	{"HiFi Mono Right Mixer", "HiFi Mono Playback Switch", "HiFi PGA"},
869 
870 	/* Ext Playback Switch -> Ext Mono Mixer */
871 	{"Ext Mono Right Mixer", NULL, "Ext Right Enable"},
872 	{"Ext Mono Right Mixer", "Ext Mono Playback Switch", "Ext Left Enable"},
873 	{"Ext Mono Left Mixer", NULL, "Ext Left Enable"},
874 	{"Ext Mono Left Mixer", "Ext Mono Playback Switch", "Ext Right Enable"},
875 
876 	/* HiFi Mono Mixer -> Output Route */
877 	{"Earpiece Playback Route", "HiFi", "HiFi Mono Right Mixer"},
878 	{"Speaker Right Playback Route", "HiFi", "HiFi Mono Right Mixer"},
879 	{"Speaker Left Playback Route", "HiFi", "HiFi Mono Left Mixer"},
880 	{"Lineout Right Playback Route", "HiFi", "HiFi Mono Right Mixer"},
881 	{"Lineout Left Playback Route", "HiFi", "HiFi Mono Left Mixer"},
882 	{"Headset Right Playback Route", "HiFi", "HiFi Mono Right Mixer"},
883 	{"Headset Left Playback Route", "HiFi", "HiFi Mono Left Mixer"},
884 	{"EMU Right Playback Route", "HiFi", "HiFi Mono Right Mixer"},
885 	{"EMU Left Playback Route", "HiFi", "HiFi Mono Left Mixer"},
886 
887 	/* Voice PGA -> Output Route */
888 	{"Earpiece Playback Route", "Voice", "Voice PGA"},
889 	{"Speaker Right Playback Route", "Voice", "Voice PGA"},
890 	{"Speaker Left Playback Route", "Voice", "Voice PGA"},
891 	{"Lineout Right Playback Route", "Voice", "Voice PGA"},
892 	{"Lineout Left Playback Route", "Voice", "Voice PGA"},
893 	{"Headset Right Playback Route", "Voice", "Voice PGA"},
894 	{"Headset Left Playback Route", "Voice", "Voice PGA"},
895 	{"EMU Right Playback Route", "Voice", "Voice PGA"},
896 	{"EMU Left Playback Route", "Voice", "Voice PGA"},
897 
898 	/* Ext Mono Mixer -> Output Route */
899 	{"Earpiece Playback Route", "Ext", "Ext Mono Right Mixer"},
900 	{"Speaker Right Playback Route", "Ext", "Ext Mono Right Mixer"},
901 	{"Speaker Left Playback Route", "Ext", "Ext Mono Left Mixer"},
902 	{"Lineout Right Playback Route", "Ext", "Ext Mono Right Mixer"},
903 	{"Lineout Left Playback Route", "Ext", "Ext Mono Left Mixer"},
904 	{"Headset Right Playback Route", "Ext", "Ext Mono Right Mixer"},
905 	{"Headset Left Playback Route", "Ext", "Ext Mono Left Mixer"},
906 	{"EMU Right Playback Route", "Ext", "Ext Mono Right Mixer"},
907 	{"EMU Left Playback Route", "Ext", "Ext Mono Left Mixer"},
908 
909 	/* Output Route -> Output Amplifier */
910 	{"Earpiece PGA", NULL, "Earpiece Playback Route"},
911 	{"Speaker Right PGA", NULL, "Speaker Right Playback Route"},
912 	{"Speaker Left PGA", NULL, "Speaker Left Playback Route"},
913 	{"Lineout Right PGA", NULL, "Lineout Right Playback Route"},
914 	{"Lineout Left PGA", NULL, "Lineout Left Playback Route"},
915 	{"Headset Right PGA", NULL, "Headset Right Playback Route"},
916 	{"Headset Left PGA", NULL, "Headset Left Playback Route"},
917 	{"EMU Right PGA", NULL, "EMU Right Playback Route"},
918 	{"EMU Left PGA", NULL, "EMU Left Playback Route"},
919 
920 	/* Output Amplifier -> Output */
921 	{"EP", NULL, "Earpiece PGA"},
922 	{"SPKR", NULL, "Speaker Right PGA"},
923 	{"SPKL", NULL, "Speaker Left PGA"},
924 	{"LINER", NULL, "Lineout Right PGA"},
925 	{"LINEL", NULL, "Lineout Left PGA"},
926 	{"HSR", NULL, "Headset Right PGA"},
927 	{"HSL", NULL, "Headset Left PGA"},
928 	{"EMUR", NULL, "EMU Right PGA"},
929 	{"EMUL", NULL, "EMU Left PGA"},
930 
931 	/* Headset Charge Pump -> Headset */
932 	{"HSR", NULL, "Headset Charge Pump"},
933 	{"HSL", NULL, "Headset Charge Pump"},
934 
935 	/* Mic -> Mic Route */
936 	{"Right Capture Route", "Mic 1", "MICR"},
937 	{"Right Capture Route", "Headset Mic", "HSMIC"},
938 	{"Right Capture Route", "EMU Mic", "EMUMIC"},
939 	{"Right Capture Route", "Ext Right", "EXTR"},
940 	{"Left Capture Route", "Mic 2", "MICL"},
941 	{"Left Capture Route", "Ext Left", "EXTL"},
942 
943 	/* Input Route -> Microphone PGA */
944 	{"Microphone 1 PGA", NULL, "Right Capture Route"},
945 	{"Microphone 2 PGA", NULL, "Left Capture Route"},
946 
947 	/* Microphone PGA -> ADC */
948 	{"ADC Right", NULL, "Microphone 1 PGA"},
949 	{"ADC Left", NULL, "Microphone 2 PGA"},
950 
951 	/* ADC -> Stream */
952 	{"Highpass Filter TX", NULL, "ADC Right"},
953 	{"Highpass Filter TX", NULL, "ADC Left"},
954 
955 	/* Mic Bias */
956 	{"MICL", NULL, "MIC1L Bias"},
957 	{"MICR", NULL, "MIC1R Bias"},
958 };
959 
960 static int cpcap_set_sysclk(struct cpcap_audio *cpcap, enum cpcap_dai dai,
961 			    int clk_id, int freq)
962 {
963 	u16 clkfreqreg, clkfreqshift;
964 	u16 clkfreqmask, clkfreqval;
965 	u16 clkidreg, clkidshift;
966 	u16 mask, val;
967 	int err;
968 
969 	switch (dai) {
970 	case CPCAP_DAI_HIFI:
971 		clkfreqreg = CPCAP_REG_SDAC;
972 		clkfreqshift = CPCAP_BIT_ST_DAC_CLK0;
973 		clkidreg = CPCAP_REG_SDACDI;
974 		clkidshift = CPCAP_BIT_ST_DAC_CLK_IN_SEL;
975 		break;
976 	case CPCAP_DAI_VOICE:
977 		clkfreqreg = CPCAP_REG_CC;
978 		clkfreqshift = CPCAP_BIT_CDC_CLK0;
979 		clkidreg = CPCAP_REG_CDI;
980 		clkidshift = CPCAP_BIT_CLK_IN_SEL;
981 		break;
982 	default:
983 		dev_err(cpcap->component->dev, "invalid DAI: %d", dai);
984 		return -EINVAL;
985 	}
986 
987 	/* setup clk id */
988 	if (clk_id < 0 || clk_id > 1) {
989 		dev_err(cpcap->component->dev, "invalid clk id %d", clk_id);
990 		return -EINVAL;
991 	}
992 	err = regmap_update_bits(cpcap->regmap, clkidreg, BIT(clkidshift),
993 				 clk_id ? BIT(clkidshift) : 0);
994 	if (err)
995 		return err;
996 
997 	/* enable PLL for Voice DAI */
998 	if (dai == CPCAP_DAI_VOICE) {
999 		mask = BIT(CPCAP_BIT_CDC_PLL_SEL);
1000 		val = BIT(CPCAP_BIT_CDC_PLL_SEL);
1001 		err = regmap_update_bits(cpcap->regmap, CPCAP_REG_CDI,
1002 					 mask, val);
1003 		if (err)
1004 			return err;
1005 	}
1006 
1007 	/* setup frequency */
1008 	clkfreqmask = 0x7 << clkfreqshift;
1009 	switch (freq) {
1010 	case 15360000:
1011 		clkfreqval = 0x01 << clkfreqshift;
1012 		break;
1013 	case 16800000:
1014 		clkfreqval = 0x02 << clkfreqshift;
1015 		break;
1016 	case 19200000:
1017 		clkfreqval = 0x03 << clkfreqshift;
1018 		break;
1019 	case 26000000:
1020 		clkfreqval = 0x04 << clkfreqshift;
1021 		break;
1022 	case 33600000:
1023 		clkfreqval = 0x05 << clkfreqshift;
1024 		break;
1025 	case 38400000:
1026 		clkfreqval = 0x06 << clkfreqshift;
1027 		break;
1028 	default:
1029 		dev_err(cpcap->component->dev, "unsupported freq %u", freq);
1030 		return -EINVAL;
1031 	}
1032 
1033 	err = regmap_update_bits(cpcap->regmap, clkfreqreg,
1034 				 clkfreqmask, clkfreqval);
1035 	if (err)
1036 		return err;
1037 
1038 	if (dai == CPCAP_DAI_VOICE) {
1039 		cpcap->codec_clk_id = clk_id;
1040 		cpcap->codec_freq = freq;
1041 	}
1042 
1043 	return 0;
1044 }
1045 
1046 static int cpcap_set_samprate(struct cpcap_audio *cpcap, enum cpcap_dai dai,
1047 			      int samplerate)
1048 {
1049 	struct snd_soc_component *component = cpcap->component;
1050 	u16 sampreg, sampmask, sampshift, sampval, sampreset;
1051 	int err, sampreadval;
1052 
1053 	switch (dai) {
1054 	case CPCAP_DAI_HIFI:
1055 		sampreg = CPCAP_REG_SDAC;
1056 		sampshift = CPCAP_BIT_ST_SR0;
1057 		sampreset = BIT(CPCAP_BIT_DF_RESET_ST_DAC) |
1058 			    BIT(CPCAP_BIT_ST_CLOCK_TREE_RESET);
1059 		break;
1060 	case CPCAP_DAI_VOICE:
1061 		sampreg = CPCAP_REG_CC;
1062 		sampshift = CPCAP_BIT_CDC_SR0;
1063 		sampreset = BIT(CPCAP_BIT_DF_RESET) |
1064 			    BIT(CPCAP_BIT_CDC_CLOCK_TREE_RESET);
1065 		break;
1066 	default:
1067 		dev_err(component->dev, "invalid DAI: %d", dai);
1068 		return -EINVAL;
1069 	}
1070 
1071 	sampmask = 0xF << sampshift | sampreset;
1072 	switch (samplerate) {
1073 	case 48000:
1074 		sampval = 0x8 << sampshift;
1075 		break;
1076 	case 44100:
1077 		sampval = 0x7 << sampshift;
1078 		break;
1079 	case 32000:
1080 		sampval = 0x6 << sampshift;
1081 		break;
1082 	case 24000:
1083 		sampval = 0x5 << sampshift;
1084 		break;
1085 	case 22050:
1086 		sampval = 0x4 << sampshift;
1087 		break;
1088 	case 16000:
1089 		sampval = 0x3 << sampshift;
1090 		break;
1091 	case 12000:
1092 		sampval = 0x2 << sampshift;
1093 		break;
1094 	case 11025:
1095 		sampval = 0x1 << sampshift;
1096 		break;
1097 	case 8000:
1098 		sampval = 0x0 << sampshift;
1099 		break;
1100 	default:
1101 		dev_err(component->dev, "unsupported samplerate %d", samplerate);
1102 		return -EINVAL;
1103 	}
1104 	err = regmap_update_bits(cpcap->regmap, sampreg,
1105 				 sampmask, sampval | sampreset);
1106 	if (err)
1107 		return err;
1108 
1109 	/* Wait for clock tree reset to complete */
1110 	mdelay(CLOCK_TREE_RESET_TIME);
1111 
1112 	err = regmap_read(cpcap->regmap, sampreg, &sampreadval);
1113 	if (err)
1114 		return err;
1115 
1116 	if (sampreadval & sampreset) {
1117 		dev_err(component->dev, "reset self-clear failed: %04x",
1118 			sampreadval);
1119 		return -EIO;
1120 	}
1121 
1122 	return 0;
1123 }
1124 
1125 static int cpcap_hifi_hw_params(struct snd_pcm_substream *substream,
1126 				struct snd_pcm_hw_params *params,
1127 				struct snd_soc_dai *dai)
1128 {
1129 	struct snd_soc_component *component = dai->component;
1130 	struct cpcap_audio *cpcap = snd_soc_component_get_drvdata(component);
1131 	int rate = params_rate(params);
1132 
1133 	dev_dbg(component->dev, "HiFi setup HW params: rate=%d", rate);
1134 	return cpcap_set_samprate(cpcap, CPCAP_DAI_HIFI, rate);
1135 }
1136 
1137 static int cpcap_hifi_set_dai_sysclk(struct snd_soc_dai *codec_dai, int clk_id,
1138 				     unsigned int freq, int dir)
1139 {
1140 	struct snd_soc_component *component = codec_dai->component;
1141 	struct cpcap_audio *cpcap = snd_soc_component_get_drvdata(component);
1142 	struct device *dev = component->dev;
1143 
1144 	dev_dbg(dev, "HiFi setup sysclk: clk_id=%u, freq=%u", clk_id, freq);
1145 	return cpcap_set_sysclk(cpcap, CPCAP_DAI_HIFI, clk_id, freq);
1146 }
1147 
1148 static int cpcap_hifi_set_dai_fmt(struct snd_soc_dai *codec_dai,
1149 				  unsigned int fmt)
1150 {
1151 	struct snd_soc_component *component = codec_dai->component;
1152 	struct cpcap_audio *cpcap = snd_soc_component_get_drvdata(component);
1153 	struct device *dev = component->dev;
1154 	static const u16 reg = CPCAP_REG_SDACDI;
1155 	static const u16 mask =
1156 		BIT(CPCAP_BIT_SMB_ST_DAC) |
1157 		BIT(CPCAP_BIT_ST_CLK_INV) |
1158 		BIT(CPCAP_BIT_ST_FS_INV) |
1159 		BIT(CPCAP_BIT_ST_DIG_AUD_FS0) |
1160 		BIT(CPCAP_BIT_ST_DIG_AUD_FS1) |
1161 		BIT(CPCAP_BIT_ST_L_TIMESLOT0) |
1162 		BIT(CPCAP_BIT_ST_L_TIMESLOT1) |
1163 		BIT(CPCAP_BIT_ST_L_TIMESLOT2) |
1164 		BIT(CPCAP_BIT_ST_R_TIMESLOT0) |
1165 		BIT(CPCAP_BIT_ST_R_TIMESLOT1) |
1166 		BIT(CPCAP_BIT_ST_R_TIMESLOT2);
1167 	u16 val = 0x0000;
1168 
1169 	dev_dbg(dev, "HiFi setup dai format (%08x)", fmt);
1170 
1171 	/*
1172 	 * "HiFi Playback" should always be configured as
1173 	 * SND_SOC_DAIFMT_CBP_CFP - codec clk & frm provider
1174 	 * SND_SOC_DAIFMT_I2S - I2S mode
1175 	 */
1176 	switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
1177 	case SND_SOC_DAIFMT_CBP_CFP:
1178 		val &= ~BIT(CPCAP_BIT_SMB_ST_DAC);
1179 		break;
1180 	default:
1181 		dev_err(dev, "HiFi dai fmt failed: CPCAP should be provider");
1182 		return -EINVAL;
1183 	}
1184 
1185 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1186 	case SND_SOC_DAIFMT_IB_IF:
1187 		val |= BIT(CPCAP_BIT_ST_FS_INV);
1188 		val |= BIT(CPCAP_BIT_ST_CLK_INV);
1189 		break;
1190 	case SND_SOC_DAIFMT_IB_NF:
1191 		val &= ~BIT(CPCAP_BIT_ST_FS_INV);
1192 		val |= BIT(CPCAP_BIT_ST_CLK_INV);
1193 		break;
1194 	case SND_SOC_DAIFMT_NB_IF:
1195 		val |= BIT(CPCAP_BIT_ST_FS_INV);
1196 		val &= ~BIT(CPCAP_BIT_ST_CLK_INV);
1197 		break;
1198 	case SND_SOC_DAIFMT_NB_NF:
1199 		val &= ~BIT(CPCAP_BIT_ST_FS_INV);
1200 		val &= ~BIT(CPCAP_BIT_ST_CLK_INV);
1201 		break;
1202 	default:
1203 		dev_err(dev, "HiFi dai fmt failed: unsupported clock invert mode");
1204 		return -EINVAL;
1205 	}
1206 
1207 	if (val & BIT(CPCAP_BIT_ST_CLK_INV))
1208 		val &= ~BIT(CPCAP_BIT_ST_CLK_INV);
1209 	else
1210 		val |= BIT(CPCAP_BIT_ST_CLK_INV);
1211 
1212 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1213 	case SND_SOC_DAIFMT_I2S:
1214 		val |= BIT(CPCAP_BIT_ST_DIG_AUD_FS0);
1215 		val |= BIT(CPCAP_BIT_ST_DIG_AUD_FS1);
1216 		break;
1217 	default:
1218 		/* 01 - 4 slots network mode */
1219 		val |= BIT(CPCAP_BIT_ST_DIG_AUD_FS0);
1220 		val &= ~BIT(CPCAP_BIT_ST_DIG_AUD_FS1);
1221 		/* L on slot 1 */
1222 		val |= BIT(CPCAP_BIT_ST_L_TIMESLOT0);
1223 		break;
1224 	}
1225 
1226 	dev_dbg(dev, "HiFi dai format: val=%04x", val);
1227 	return regmap_update_bits(cpcap->regmap, reg, mask, val);
1228 }
1229 
1230 static int cpcap_hifi_set_mute(struct snd_soc_dai *dai, int mute, int direction)
1231 {
1232 	struct snd_soc_component *component = dai->component;
1233 	struct cpcap_audio *cpcap = snd_soc_component_get_drvdata(component);
1234 	static const u16 reg = CPCAP_REG_RXSDOA;
1235 	static const u16 mask = BIT(CPCAP_BIT_ST_DAC_SW);
1236 	u16 val;
1237 
1238 	if (mute)
1239 		val = 0;
1240 	else
1241 		val = BIT(CPCAP_BIT_ST_DAC_SW);
1242 
1243 	dev_dbg(component->dev, "HiFi mute: %d", mute);
1244 	return regmap_update_bits(cpcap->regmap, reg, mask, val);
1245 }
1246 
1247 static const struct snd_soc_dai_ops cpcap_dai_hifi_ops = {
1248 	.hw_params	= cpcap_hifi_hw_params,
1249 	.set_sysclk	= cpcap_hifi_set_dai_sysclk,
1250 	.set_fmt	= cpcap_hifi_set_dai_fmt,
1251 	.mute_stream	= cpcap_hifi_set_mute,
1252 	.no_capture_mute = 1,
1253 };
1254 
1255 static int cpcap_voice_hw_params(struct snd_pcm_substream *substream,
1256 				 struct snd_pcm_hw_params *params,
1257 				 struct snd_soc_dai *dai)
1258 {
1259 	struct snd_soc_component *component = dai->component;
1260 	struct device *dev = component->dev;
1261 	struct cpcap_audio *cpcap = snd_soc_component_get_drvdata(component);
1262 	static const u16 reg_cdi = CPCAP_REG_CDI;
1263 	int rate = params_rate(params);
1264 	int channels = params_channels(params);
1265 	int direction = substream->stream;
1266 	u16 val, mask;
1267 	int err;
1268 
1269 	dev_dbg(dev, "Voice setup HW params: rate=%d, direction=%d, chan=%d",
1270 		rate, direction, channels);
1271 
1272 	err = cpcap_set_samprate(cpcap, CPCAP_DAI_VOICE, rate);
1273 	if (err)
1274 		return err;
1275 
1276 	if (direction == SNDRV_PCM_STREAM_CAPTURE) {
1277 		mask = 0x0000;
1278 		mask |= BIT(CPCAP_BIT_MIC1_RX_TIMESLOT0);
1279 		mask |= BIT(CPCAP_BIT_MIC1_RX_TIMESLOT1);
1280 		mask |= BIT(CPCAP_BIT_MIC1_RX_TIMESLOT2);
1281 		mask |= BIT(CPCAP_BIT_MIC2_TIMESLOT0);
1282 		mask |= BIT(CPCAP_BIT_MIC2_TIMESLOT1);
1283 		mask |= BIT(CPCAP_BIT_MIC2_TIMESLOT2);
1284 		val = 0x0000;
1285 		if (channels >= 2)
1286 			val = BIT(CPCAP_BIT_MIC1_RX_TIMESLOT0);
1287 		err = regmap_update_bits(cpcap->regmap, reg_cdi, mask, val);
1288 		if (err)
1289 			return err;
1290 	}
1291 
1292 	return 0;
1293 }
1294 
1295 static int cpcap_voice_set_dai_sysclk(struct snd_soc_dai *codec_dai, int clk_id,
1296 				      unsigned int freq, int dir)
1297 {
1298 	struct snd_soc_component *component = codec_dai->component;
1299 	struct cpcap_audio *cpcap = snd_soc_component_get_drvdata(component);
1300 
1301 	dev_dbg(component->dev, "Voice setup sysclk: clk_id=%u, freq=%u",
1302 		clk_id, freq);
1303 	return cpcap_set_sysclk(cpcap, CPCAP_DAI_VOICE, clk_id, freq);
1304 }
1305 
1306 static int cpcap_voice_set_dai_fmt(struct snd_soc_dai *codec_dai,
1307 				   unsigned int fmt)
1308 {
1309 	struct snd_soc_component *component = codec_dai->component;
1310 	struct cpcap_audio *cpcap = snd_soc_component_get_drvdata(component);
1311 	static const u16 mask = BIT(CPCAP_BIT_SMB_CDC) |
1312 				BIT(CPCAP_BIT_CLK_INV) |
1313 				BIT(CPCAP_BIT_FS_INV) |
1314 				BIT(CPCAP_BIT_CDC_DIG_AUD_FS0) |
1315 				BIT(CPCAP_BIT_CDC_DIG_AUD_FS1);
1316 	u16 val = 0x0000;
1317 	int err;
1318 
1319 	dev_dbg(component->dev, "Voice setup dai format (%08x)", fmt);
1320 
1321 	/*
1322 	 * "Voice Playback" and "Voice Capture" should always be
1323 	 * configured as SND_SOC_DAIFMT_CBP_CFP - codec clk & frm
1324 	 * provider
1325 	 */
1326 	switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
1327 	case SND_SOC_DAIFMT_CBP_CFP:
1328 		val &= ~BIT(CPCAP_BIT_SMB_CDC);
1329 		break;
1330 	default:
1331 		dev_err(component->dev, "Voice dai fmt failed: CPCAP should be the provider");
1332 		val &= ~BIT(CPCAP_BIT_SMB_CDC);
1333 		break;
1334 	}
1335 
1336 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1337 	case SND_SOC_DAIFMT_IB_IF:
1338 		val |= BIT(CPCAP_BIT_CLK_INV);
1339 		val |= BIT(CPCAP_BIT_FS_INV);
1340 		break;
1341 	case SND_SOC_DAIFMT_IB_NF:
1342 		val |= BIT(CPCAP_BIT_CLK_INV);
1343 		val &= ~BIT(CPCAP_BIT_FS_INV);
1344 		break;
1345 	case SND_SOC_DAIFMT_NB_IF:
1346 		val &= ~BIT(CPCAP_BIT_CLK_INV);
1347 		val |= BIT(CPCAP_BIT_FS_INV);
1348 		break;
1349 	case SND_SOC_DAIFMT_NB_NF:
1350 		val &= ~BIT(CPCAP_BIT_CLK_INV);
1351 		val &= ~BIT(CPCAP_BIT_FS_INV);
1352 		break;
1353 	default:
1354 		dev_err(component->dev, "Voice dai fmt failed: unsupported clock invert mode");
1355 		break;
1356 	}
1357 
1358 	if (val & BIT(CPCAP_BIT_CLK_INV))
1359 		val &= ~BIT(CPCAP_BIT_CLK_INV);
1360 	else
1361 		val |= BIT(CPCAP_BIT_CLK_INV);
1362 
1363 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1364 	case SND_SOC_DAIFMT_I2S:
1365 		/* 11 - true I2S mode */
1366 		val |= BIT(CPCAP_BIT_CDC_DIG_AUD_FS0);
1367 		val |= BIT(CPCAP_BIT_CDC_DIG_AUD_FS1);
1368 		break;
1369 	default:
1370 		/* 4 timeslots network mode */
1371 		val |= BIT(CPCAP_BIT_CDC_DIG_AUD_FS0);
1372 		val &= ~BIT(CPCAP_BIT_CDC_DIG_AUD_FS1);
1373 		break;
1374 	}
1375 
1376 	dev_dbg(component->dev, "Voice dai format: val=%04x", val);
1377 	err = regmap_update_bits(cpcap->regmap, CPCAP_REG_CDI, mask, val);
1378 	if (err)
1379 		return err;
1380 
1381 	cpcap->codec_format = val;
1382 	return 0;
1383 }
1384 
1385 
1386 /*
1387  * Configure codec for voice call if requested.
1388  *
1389  * We can configure most with snd_soc_dai_set_sysclk(), snd_soc_dai_set_fmt()
1390  * and snd_soc_dai_set_tdm_slot(). This function configures the rest of the
1391  * cpcap related hardware as CPU is not involved in the voice call.
1392  */
1393 static int cpcap_voice_call(struct cpcap_audio *cpcap, struct snd_soc_dai *dai,
1394 			    bool voice_call)
1395 {
1396 	int mask, err;
1397 
1398 	/* Modem to codec VAUDIO_MODE1 */
1399 	mask = BIT(CPCAP_BIT_VAUDIO_MODE1);
1400 	err = regmap_update_bits(cpcap->regmap, CPCAP_REG_VAUDIOC,
1401 				 mask, voice_call ? mask : 0);
1402 	if (err)
1403 		return err;
1404 
1405 	/* Clear MIC1_MUX for call */
1406 	mask = BIT(CPCAP_BIT_MIC1_MUX);
1407 	err = regmap_update_bits(cpcap->regmap, CPCAP_REG_TXI,
1408 				 mask, voice_call ? 0 : mask);
1409 	if (err)
1410 		return err;
1411 
1412 	/* Set MIC2_MUX for call */
1413 	mask = BIT(CPCAP_BIT_MB_ON1L) | BIT(CPCAP_BIT_MB_ON1R) |
1414 		BIT(CPCAP_BIT_MIC2_MUX) | BIT(CPCAP_BIT_MIC2_PGA_EN);
1415 	err = regmap_update_bits(cpcap->regmap, CPCAP_REG_TXI,
1416 				 mask, voice_call ? mask : 0);
1417 	if (err)
1418 		return err;
1419 
1420 	/* Enable LDSP for call */
1421 	mask = BIT(CPCAP_BIT_A2_LDSP_L_EN) | BIT(CPCAP_BIT_A2_LDSP_R_EN);
1422 	err = regmap_update_bits(cpcap->regmap, CPCAP_REG_RXOA,
1423 				 mask, voice_call ? mask : 0);
1424 	if (err)
1425 		return err;
1426 
1427 	/* Enable CPCAP_BIT_PGA_CDC_EN for call */
1428 	mask = BIT(CPCAP_BIT_PGA_CDC_EN);
1429 	err = regmap_update_bits(cpcap->regmap, CPCAP_REG_RXCOA,
1430 				 mask, voice_call ? mask : 0);
1431 	if (err)
1432 		return err;
1433 
1434 	/* Unmute voice for call */
1435 	if (dai) {
1436 		err = snd_soc_dai_digital_mute(dai, !voice_call,
1437 					       SNDRV_PCM_STREAM_PLAYBACK);
1438 		if (err)
1439 			return err;
1440 	}
1441 
1442 	/* Set modem to codec mic CDC and HPF for call */
1443 	mask = BIT(CPCAP_BIT_MIC2_CDC_EN) | BIT(CPCAP_BIT_CDC_EN_RX) |
1444 	       BIT(CPCAP_BIT_AUDOHPF_1) | BIT(CPCAP_BIT_AUDOHPF_0) |
1445 	       BIT(CPCAP_BIT_AUDIHPF_1) | BIT(CPCAP_BIT_AUDIHPF_0);
1446 	err = regmap_update_bits(cpcap->regmap, CPCAP_REG_CC,
1447 				 mask, voice_call ? mask : 0);
1448 	if (err)
1449 		return err;
1450 
1451 	/* Enable modem to codec CDC for call*/
1452 	mask = BIT(CPCAP_BIT_CDC_CLK_EN);
1453 	err = regmap_update_bits(cpcap->regmap, CPCAP_REG_CDI,
1454 				 mask, voice_call ? mask : 0);
1455 
1456 	return err;
1457 }
1458 
1459 static int cpcap_voice_set_tdm_slot(struct snd_soc_dai *dai,
1460 				    unsigned int tx_mask, unsigned int rx_mask,
1461 				    int slots, int slot_width)
1462 {
1463 	struct snd_soc_component *component = dai->component;
1464 	struct cpcap_audio *cpcap = snd_soc_component_get_drvdata(component);
1465 	int err, ts_mask, mask;
1466 	bool voice_call;
1467 
1468 	/*
1469 	 * Primitive test for voice call, probably needs more checks
1470 	 * later on for 16-bit calls detected, Bluetooth headset etc.
1471 	 */
1472 	if (tx_mask == 0 && rx_mask == 1 && slot_width == 8)
1473 		voice_call = true;
1474 	else
1475 		voice_call = false;
1476 
1477 	ts_mask = 0x7 << CPCAP_BIT_MIC2_TIMESLOT0;
1478 	ts_mask |= 0x7 << CPCAP_BIT_MIC1_RX_TIMESLOT0;
1479 
1480 	mask = (tx_mask & 0x7) << CPCAP_BIT_MIC2_TIMESLOT0;
1481 	mask |= (rx_mask & 0x7) << CPCAP_BIT_MIC1_RX_TIMESLOT0;
1482 
1483 	err = regmap_update_bits(cpcap->regmap, CPCAP_REG_CDI,
1484 				 ts_mask, mask);
1485 	if (err)
1486 		return err;
1487 
1488 	err = cpcap_set_samprate(cpcap, CPCAP_DAI_VOICE, slot_width * 1000);
1489 	if (err)
1490 		return err;
1491 
1492 	err = cpcap_voice_call(cpcap, dai, voice_call);
1493 	if (err)
1494 		return err;
1495 
1496 	return 0;
1497 }
1498 
1499 static int cpcap_voice_set_mute(struct snd_soc_dai *dai, int mute, int direction)
1500 {
1501 	struct snd_soc_component *component = dai->component;
1502 	struct cpcap_audio *cpcap = snd_soc_component_get_drvdata(component);
1503 	static const u16 reg = CPCAP_REG_RXCOA;
1504 	static const u16 mask = BIT(CPCAP_BIT_CDC_SW);
1505 	u16 val;
1506 
1507 	if (mute)
1508 		val = 0;
1509 	else
1510 		val = BIT(CPCAP_BIT_CDC_SW);
1511 
1512 	dev_dbg(component->dev, "Voice mute: %d", mute);
1513 	return regmap_update_bits(cpcap->regmap, reg, mask, val);
1514 };
1515 
1516 static const struct snd_soc_dai_ops cpcap_dai_voice_ops = {
1517 	.hw_params	= cpcap_voice_hw_params,
1518 	.set_sysclk	= cpcap_voice_set_dai_sysclk,
1519 	.set_fmt	= cpcap_voice_set_dai_fmt,
1520 	.set_tdm_slot	= cpcap_voice_set_tdm_slot,
1521 	.mute_stream	= cpcap_voice_set_mute,
1522 	.no_capture_mute = 1,
1523 };
1524 
1525 static struct snd_soc_dai_driver cpcap_dai[] = {
1526 {
1527 	.id = 0,
1528 	.name = "cpcap-hifi",
1529 	.playback = {
1530 		.stream_name = "HiFi Playback",
1531 		.channels_min = 2,
1532 		.channels_max = 2,
1533 		.rates = SNDRV_PCM_RATE_8000_48000,
1534 		.formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FORMAT_S24_LE,
1535 	},
1536 	.ops = &cpcap_dai_hifi_ops,
1537 },
1538 {
1539 	.id = 1,
1540 	.name = "cpcap-voice",
1541 	.playback = {
1542 		.stream_name = "Voice Playback",
1543 		.channels_min = 1,
1544 		.channels_max = 1,
1545 		.rates = SNDRV_PCM_RATE_8000_48000,
1546 		.formats = SNDRV_PCM_FMTBIT_S16_LE,
1547 	},
1548 	.capture = {
1549 		.stream_name = "Voice Capture",
1550 		.channels_min = 1,
1551 		.channels_max = 2,
1552 		.rates = SNDRV_PCM_RATE_8000_48000,
1553 		.formats = SNDRV_PCM_FMTBIT_S16_LE,
1554 	},
1555 	.ops = &cpcap_dai_voice_ops,
1556 },
1557 };
1558 
1559 static int cpcap_dai_mux(struct cpcap_audio *cpcap, bool swap_dai_configuration)
1560 {
1561 	u16 hifi_val, voice_val;
1562 	u16 hifi_mask = BIT(CPCAP_BIT_DIG_AUD_IN_ST_DAC);
1563 	u16 voice_mask = BIT(CPCAP_BIT_DIG_AUD_IN);
1564 	int err;
1565 
1566 
1567 
1568 	if (!swap_dai_configuration) {
1569 		/* Codec on DAI0, HiFi on DAI1 */
1570 		voice_val = 0;
1571 		hifi_val = hifi_mask;
1572 	} else {
1573 		/* Codec on DAI1, HiFi on DAI0 */
1574 		voice_val = voice_mask;
1575 		hifi_val = 0;
1576 	}
1577 
1578 	err = regmap_update_bits(cpcap->regmap, CPCAP_REG_CDI,
1579 				 voice_mask, voice_val);
1580 	if (err)
1581 		return err;
1582 
1583 	err = regmap_update_bits(cpcap->regmap, CPCAP_REG_SDACDI,
1584 				 hifi_mask, hifi_val);
1585 	if (err)
1586 		return err;
1587 
1588 	return 0;
1589 }
1590 
1591 static int cpcap_audio_reset(struct snd_soc_component *component,
1592 			     bool swap_dai_configuration)
1593 {
1594 	struct cpcap_audio *cpcap = snd_soc_component_get_drvdata(component);
1595 	int i, err = 0;
1596 
1597 	dev_dbg(component->dev, "init audio codec");
1598 
1599 	for (i = 0; i < ARRAY_SIZE(cpcap_default_regs); i++) {
1600 		err = regmap_update_bits(cpcap->regmap,
1601 					 cpcap_default_regs[i].reg,
1602 					 cpcap_default_regs[i].mask,
1603 					 cpcap_default_regs[i].val);
1604 		if (err)
1605 			return err;
1606 	}
1607 
1608 	/* setup default settings */
1609 	err = cpcap_dai_mux(cpcap, swap_dai_configuration);
1610 	if (err)
1611 		return err;
1612 
1613 	err = cpcap_set_sysclk(cpcap, CPCAP_DAI_HIFI, 0, 26000000);
1614 	if (err)
1615 		return err;
1616 	err = cpcap_set_sysclk(cpcap, CPCAP_DAI_VOICE, 0, 26000000);
1617 	if (err)
1618 		return err;
1619 
1620 	err = cpcap_set_samprate(cpcap, CPCAP_DAI_HIFI, 48000);
1621 	if (err)
1622 		return err;
1623 
1624 	err = cpcap_set_samprate(cpcap, CPCAP_DAI_VOICE, 48000);
1625 	if (err)
1626 		return err;
1627 
1628 	return 0;
1629 }
1630 
1631 static int cpcap_soc_probe(struct snd_soc_component *component)
1632 {
1633 	struct cpcap_audio *cpcap;
1634 	int err;
1635 
1636 	cpcap = devm_kzalloc(component->dev, sizeof(*cpcap), GFP_KERNEL);
1637 	if (!cpcap)
1638 		return -ENOMEM;
1639 	snd_soc_component_set_drvdata(component, cpcap);
1640 	cpcap->component = component;
1641 
1642 	cpcap->vaudio = devm_regulator_get(component->dev, "VAUDIO");
1643 	if (IS_ERR(cpcap->vaudio))
1644 		return dev_err_probe(component->dev, PTR_ERR(cpcap->vaudio),
1645 				     "Cannot get VAUDIO regulator\n");
1646 
1647 	cpcap->regmap = dev_get_regmap(component->dev->parent, NULL);
1648 	if (!cpcap->regmap)
1649 		return -ENODEV;
1650 	snd_soc_component_init_regmap(component, cpcap->regmap);
1651 
1652 	err = cpcap_get_vendor(component->dev, cpcap->regmap, &cpcap->vendor);
1653 	if (err)
1654 		return err;
1655 
1656 	return cpcap_audio_reset(component, false);
1657 }
1658 
1659 static int cpcap_set_bias_level(struct snd_soc_component *component,
1660 		enum snd_soc_bias_level level)
1661 {
1662 	struct cpcap_audio *cpcap = snd_soc_component_get_drvdata(component);
1663 
1664 	switch (level) {
1665 	case SND_SOC_BIAS_OFF:
1666 		break;
1667 	case SND_SOC_BIAS_PREPARE:
1668 		regulator_set_mode(cpcap->vaudio, REGULATOR_MODE_NORMAL);
1669 		break;
1670 	case SND_SOC_BIAS_STANDBY:
1671 		regulator_set_mode(cpcap->vaudio, REGULATOR_MODE_STANDBY);
1672 		break;
1673 	case SND_SOC_BIAS_ON:
1674 		break;
1675 	}
1676 
1677 	return 0;
1678 }
1679 
1680 static const struct snd_soc_component_driver soc_codec_dev_cpcap = {
1681 	.probe			= cpcap_soc_probe,
1682 	.controls		= cpcap_snd_controls,
1683 	.num_controls		= ARRAY_SIZE(cpcap_snd_controls),
1684 	.dapm_widgets		= cpcap_dapm_widgets,
1685 	.num_dapm_widgets	= ARRAY_SIZE(cpcap_dapm_widgets),
1686 	.dapm_routes		= intercon,
1687 	.num_dapm_routes	= ARRAY_SIZE(intercon),
1688 	.set_bias_level		= cpcap_set_bias_level,
1689 	.idle_bias_on		= 1,
1690 	.use_pmdown_time	= 1,
1691 	.endianness		= 1,
1692 };
1693 
1694 static int cpcap_codec_probe(struct platform_device *pdev)
1695 {
1696 	struct device_node *codec_node =
1697 		of_get_child_by_name(pdev->dev.parent->of_node, "audio-codec");
1698 	if (!codec_node)
1699 		return -ENODEV;
1700 
1701 	pdev->dev.of_node = codec_node;
1702 
1703 	return devm_snd_soc_register_component(&pdev->dev, &soc_codec_dev_cpcap,
1704 				      cpcap_dai, ARRAY_SIZE(cpcap_dai));
1705 }
1706 
1707 static struct platform_driver cpcap_codec_driver = {
1708 	.probe		= cpcap_codec_probe,
1709 	.driver		= {
1710 		.name	= "cpcap-codec",
1711 	},
1712 };
1713 module_platform_driver(cpcap_codec_driver);
1714 
1715 MODULE_ALIAS("platform:cpcap-codec");
1716 MODULE_DESCRIPTION("ASoC CPCAP codec driver");
1717 MODULE_AUTHOR("Sebastian Reichel");
1718 MODULE_LICENSE("GPL v2");
1719