xref: /linux/sound/soc/codecs/da7213.c (revision b85d45947951d23cb22d90caecf4c1eb81342c96)
1 /*
2  * DA7213 ALSA SoC Codec Driver
3  *
4  * Copyright (c) 2013 Dialog Semiconductor
5  *
6  * Author: Adam Thomson <Adam.Thomson.Opensource@diasemi.com>
7  * Based on DA9055 ALSA SoC codec driver.
8  *
9  * This program is free software; you can redistribute  it and/or modify it
10  * under  the terms of  the GNU General  Public License as published by the
11  * Free Software Foundation;  either version 2 of the  License, or (at your
12  * option) any later version.
13  */
14 
15 #include <linux/delay.h>
16 #include <linux/i2c.h>
17 #include <linux/regmap.h>
18 #include <linux/slab.h>
19 #include <linux/module.h>
20 #include <sound/pcm.h>
21 #include <sound/pcm_params.h>
22 #include <sound/soc.h>
23 #include <sound/initval.h>
24 #include <sound/tlv.h>
25 
26 #include <sound/da7213.h>
27 #include "da7213.h"
28 
29 
30 /* Gain and Volume */
31 static const DECLARE_TLV_DB_RANGE(aux_vol_tlv,
32 	/* -54dB */
33 	0x0, 0x11, TLV_DB_SCALE_ITEM(-5400, 0, 0),
34 	/* -52.5dB to 15dB */
35 	0x12, 0x3f, TLV_DB_SCALE_ITEM(-5250, 150, 0)
36 );
37 
38 static const DECLARE_TLV_DB_RANGE(digital_gain_tlv,
39 	0x0, 0x07, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE, 0, 1),
40 	/* -78dB to 12dB */
41 	0x08, 0x7f, TLV_DB_SCALE_ITEM(-7800, 75, 0)
42 );
43 
44 static const DECLARE_TLV_DB_RANGE(alc_analog_gain_tlv,
45 	0x0, 0x0, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE, 0, 1),
46 	/* 0dB to 36dB */
47 	0x01, 0x07, TLV_DB_SCALE_ITEM(0, 600, 0)
48 );
49 
50 static const DECLARE_TLV_DB_SCALE(mic_vol_tlv, -600, 600, 0);
51 static const DECLARE_TLV_DB_SCALE(mixin_gain_tlv, -450, 150, 0);
52 static const DECLARE_TLV_DB_SCALE(eq_gain_tlv, -1050, 150, 0);
53 static const DECLARE_TLV_DB_SCALE(hp_vol_tlv, -5700, 100, 0);
54 static const DECLARE_TLV_DB_SCALE(lineout_vol_tlv, -4800, 100, 0);
55 static const DECLARE_TLV_DB_SCALE(alc_threshold_tlv, -9450, 150, 0);
56 static const DECLARE_TLV_DB_SCALE(alc_gain_tlv, 0, 600, 0);
57 
58 /* ADC and DAC voice mode (8kHz) high pass cutoff value */
59 static const char * const da7213_voice_hpf_corner_txt[] = {
60 	"2.5Hz", "25Hz", "50Hz", "100Hz", "150Hz", "200Hz", "300Hz", "400Hz"
61 };
62 
63 static SOC_ENUM_SINGLE_DECL(da7213_dac_voice_hpf_corner,
64 			    DA7213_DAC_FILTERS1,
65 			    DA7213_VOICE_HPF_CORNER_SHIFT,
66 			    da7213_voice_hpf_corner_txt);
67 
68 static SOC_ENUM_SINGLE_DECL(da7213_adc_voice_hpf_corner,
69 			    DA7213_ADC_FILTERS1,
70 			    DA7213_VOICE_HPF_CORNER_SHIFT,
71 			    da7213_voice_hpf_corner_txt);
72 
73 /* ADC and DAC high pass filter cutoff value */
74 static const char * const da7213_audio_hpf_corner_txt[] = {
75 	"Fs/24000", "Fs/12000", "Fs/6000", "Fs/3000"
76 };
77 
78 static SOC_ENUM_SINGLE_DECL(da7213_dac_audio_hpf_corner,
79 			    DA7213_DAC_FILTERS1
80 			    , DA7213_AUDIO_HPF_CORNER_SHIFT,
81 			    da7213_audio_hpf_corner_txt);
82 
83 static SOC_ENUM_SINGLE_DECL(da7213_adc_audio_hpf_corner,
84 			    DA7213_ADC_FILTERS1,
85 			    DA7213_AUDIO_HPF_CORNER_SHIFT,
86 			    da7213_audio_hpf_corner_txt);
87 
88 /* Gain ramping rate value */
89 static const char * const da7213_gain_ramp_rate_txt[] = {
90 	"nominal rate * 8", "nominal rate * 16", "nominal rate / 16",
91 	"nominal rate / 32"
92 };
93 
94 static SOC_ENUM_SINGLE_DECL(da7213_gain_ramp_rate,
95 			    DA7213_GAIN_RAMP_CTRL,
96 			    DA7213_GAIN_RAMP_RATE_SHIFT,
97 			    da7213_gain_ramp_rate_txt);
98 
99 /* DAC noise gate setup time value */
100 static const char * const da7213_dac_ng_setup_time_txt[] = {
101 	"256 samples", "512 samples", "1024 samples", "2048 samples"
102 };
103 
104 static SOC_ENUM_SINGLE_DECL(da7213_dac_ng_setup_time,
105 			    DA7213_DAC_NG_SETUP_TIME,
106 			    DA7213_DAC_NG_SETUP_TIME_SHIFT,
107 			    da7213_dac_ng_setup_time_txt);
108 
109 /* DAC noise gate rampup rate value */
110 static const char * const da7213_dac_ng_rampup_txt[] = {
111 	"0.02 ms/dB", "0.16 ms/dB"
112 };
113 
114 static SOC_ENUM_SINGLE_DECL(da7213_dac_ng_rampup_rate,
115 			    DA7213_DAC_NG_SETUP_TIME,
116 			    DA7213_DAC_NG_RAMPUP_RATE_SHIFT,
117 			    da7213_dac_ng_rampup_txt);
118 
119 /* DAC noise gate rampdown rate value */
120 static const char * const da7213_dac_ng_rampdown_txt[] = {
121 	"0.64 ms/dB", "20.48 ms/dB"
122 };
123 
124 static SOC_ENUM_SINGLE_DECL(da7213_dac_ng_rampdown_rate,
125 			    DA7213_DAC_NG_SETUP_TIME,
126 			    DA7213_DAC_NG_RAMPDN_RATE_SHIFT,
127 			    da7213_dac_ng_rampdown_txt);
128 
129 /* DAC soft mute rate value */
130 static const char * const da7213_dac_soft_mute_rate_txt[] = {
131 	"1", "2", "4", "8", "16", "32", "64"
132 };
133 
134 static SOC_ENUM_SINGLE_DECL(da7213_dac_soft_mute_rate,
135 			    DA7213_DAC_FILTERS5,
136 			    DA7213_DAC_SOFTMUTE_RATE_SHIFT,
137 			    da7213_dac_soft_mute_rate_txt);
138 
139 /* ALC Attack Rate select */
140 static const char * const da7213_alc_attack_rate_txt[] = {
141 	"44/fs", "88/fs", "176/fs", "352/fs", "704/fs", "1408/fs", "2816/fs",
142 	"5632/fs", "11264/fs", "22528/fs", "45056/fs", "90112/fs", "180224/fs"
143 };
144 
145 static SOC_ENUM_SINGLE_DECL(da7213_alc_attack_rate,
146 			    DA7213_ALC_CTRL2,
147 			    DA7213_ALC_ATTACK_SHIFT,
148 			    da7213_alc_attack_rate_txt);
149 
150 /* ALC Release Rate select */
151 static const char * const da7213_alc_release_rate_txt[] = {
152 	"176/fs", "352/fs", "704/fs", "1408/fs", "2816/fs", "5632/fs",
153 	"11264/fs", "22528/fs", "45056/fs", "90112/fs", "180224/fs"
154 };
155 
156 static SOC_ENUM_SINGLE_DECL(da7213_alc_release_rate,
157 			    DA7213_ALC_CTRL2,
158 			    DA7213_ALC_RELEASE_SHIFT,
159 			    da7213_alc_release_rate_txt);
160 
161 /* ALC Hold Time select */
162 static const char * const da7213_alc_hold_time_txt[] = {
163 	"62/fs", "124/fs", "248/fs", "496/fs", "992/fs", "1984/fs", "3968/fs",
164 	"7936/fs", "15872/fs", "31744/fs", "63488/fs", "126976/fs",
165 	"253952/fs", "507904/fs", "1015808/fs", "2031616/fs"
166 };
167 
168 static SOC_ENUM_SINGLE_DECL(da7213_alc_hold_time,
169 			    DA7213_ALC_CTRL3,
170 			    DA7213_ALC_HOLD_SHIFT,
171 			    da7213_alc_hold_time_txt);
172 
173 /* ALC Input Signal Tracking rate select */
174 static const char * const da7213_alc_integ_rate_txt[] = {
175 	"1/4", "1/16", "1/256", "1/65536"
176 };
177 
178 static SOC_ENUM_SINGLE_DECL(da7213_alc_integ_attack_rate,
179 			    DA7213_ALC_CTRL3,
180 			    DA7213_ALC_INTEG_ATTACK_SHIFT,
181 			    da7213_alc_integ_rate_txt);
182 
183 static SOC_ENUM_SINGLE_DECL(da7213_alc_integ_release_rate,
184 			    DA7213_ALC_CTRL3,
185 			    DA7213_ALC_INTEG_RELEASE_SHIFT,
186 			    da7213_alc_integ_rate_txt);
187 
188 
189 /*
190  * Control Functions
191  */
192 
193 static int da7213_get_alc_data(struct snd_soc_codec *codec, u8 reg_val)
194 {
195 	int mid_data, top_data;
196 	int sum = 0;
197 	u8 iteration;
198 
199 	for (iteration = 0; iteration < DA7213_ALC_AVG_ITERATIONS;
200 	     iteration++) {
201 		/* Select the left or right channel and capture data */
202 		snd_soc_write(codec, DA7213_ALC_CIC_OP_LVL_CTRL, reg_val);
203 
204 		/* Select middle 8 bits for read back from data register */
205 		snd_soc_write(codec, DA7213_ALC_CIC_OP_LVL_CTRL,
206 			      reg_val | DA7213_ALC_DATA_MIDDLE);
207 		mid_data = snd_soc_read(codec, DA7213_ALC_CIC_OP_LVL_DATA);
208 
209 		/* Select top 8 bits for read back from data register */
210 		snd_soc_write(codec, DA7213_ALC_CIC_OP_LVL_CTRL,
211 			      reg_val | DA7213_ALC_DATA_TOP);
212 		top_data = snd_soc_read(codec, DA7213_ALC_CIC_OP_LVL_DATA);
213 
214 		sum += ((mid_data << 8) | (top_data << 16));
215 	}
216 
217 	return sum / DA7213_ALC_AVG_ITERATIONS;
218 }
219 
220 static void da7213_alc_calib_man(struct snd_soc_codec *codec)
221 {
222 	u8 reg_val;
223 	int avg_left_data, avg_right_data, offset_l, offset_r;
224 
225 	/* Calculate average for Left and Right data */
226 	/* Left Data */
227 	avg_left_data = da7213_get_alc_data(codec,
228 			DA7213_ALC_CIC_OP_CHANNEL_LEFT);
229 	/* Right Data */
230 	avg_right_data = da7213_get_alc_data(codec,
231 			 DA7213_ALC_CIC_OP_CHANNEL_RIGHT);
232 
233 	/* Calculate DC offset */
234 	offset_l = -avg_left_data;
235 	offset_r = -avg_right_data;
236 
237 	reg_val = (offset_l & DA7213_ALC_OFFSET_15_8) >> 8;
238 	snd_soc_write(codec, DA7213_ALC_OFFSET_MAN_M_L, reg_val);
239 	reg_val = (offset_l & DA7213_ALC_OFFSET_19_16) >> 16;
240 	snd_soc_write(codec, DA7213_ALC_OFFSET_MAN_U_L, reg_val);
241 
242 	reg_val = (offset_r & DA7213_ALC_OFFSET_15_8) >> 8;
243 	snd_soc_write(codec, DA7213_ALC_OFFSET_MAN_M_R, reg_val);
244 	reg_val = (offset_r & DA7213_ALC_OFFSET_19_16) >> 16;
245 	snd_soc_write(codec, DA7213_ALC_OFFSET_MAN_U_R, reg_val);
246 
247 	/* Enable analog/digital gain mode & offset cancellation */
248 	snd_soc_update_bits(codec, DA7213_ALC_CTRL1,
249 			    DA7213_ALC_OFFSET_EN | DA7213_ALC_SYNC_MODE,
250 			    DA7213_ALC_OFFSET_EN | DA7213_ALC_SYNC_MODE);
251 }
252 
253 static void da7213_alc_calib_auto(struct snd_soc_codec *codec)
254 {
255 	u8 alc_ctrl1;
256 
257 	/* Begin auto calibration and wait for completion */
258 	snd_soc_update_bits(codec, DA7213_ALC_CTRL1, DA7213_ALC_AUTO_CALIB_EN,
259 			    DA7213_ALC_AUTO_CALIB_EN);
260 	do {
261 		alc_ctrl1 = snd_soc_read(codec, DA7213_ALC_CTRL1);
262 	} while (alc_ctrl1 & DA7213_ALC_AUTO_CALIB_EN);
263 
264 	/* If auto calibration fails, fall back to digital gain only mode */
265 	if (alc_ctrl1 & DA7213_ALC_CALIB_OVERFLOW) {
266 		dev_warn(codec->dev,
267 			 "ALC auto calibration failed with overflow\n");
268 		snd_soc_update_bits(codec, DA7213_ALC_CTRL1,
269 				    DA7213_ALC_OFFSET_EN | DA7213_ALC_SYNC_MODE,
270 				    0);
271 	} else {
272 		/* Enable analog/digital gain mode & offset cancellation */
273 		snd_soc_update_bits(codec, DA7213_ALC_CTRL1,
274 				    DA7213_ALC_OFFSET_EN | DA7213_ALC_SYNC_MODE,
275 				    DA7213_ALC_OFFSET_EN | DA7213_ALC_SYNC_MODE);
276 	}
277 
278 }
279 
280 static void da7213_alc_calib(struct snd_soc_codec *codec)
281 {
282 	struct da7213_priv *da7213 = snd_soc_codec_get_drvdata(codec);
283 	u8 adc_l_ctrl, adc_r_ctrl;
284 	u8 mixin_l_sel, mixin_r_sel;
285 	u8 mic_1_ctrl, mic_2_ctrl;
286 
287 	/* Save current values from ADC control registers */
288 	adc_l_ctrl = snd_soc_read(codec, DA7213_ADC_L_CTRL);
289 	adc_r_ctrl = snd_soc_read(codec, DA7213_ADC_R_CTRL);
290 
291 	/* Save current values from MIXIN_L/R_SELECT registers */
292 	mixin_l_sel = snd_soc_read(codec, DA7213_MIXIN_L_SELECT);
293 	mixin_r_sel = snd_soc_read(codec, DA7213_MIXIN_R_SELECT);
294 
295 	/* Save current values from MIC control registers */
296 	mic_1_ctrl = snd_soc_read(codec, DA7213_MIC_1_CTRL);
297 	mic_2_ctrl = snd_soc_read(codec, DA7213_MIC_2_CTRL);
298 
299 	/* Enable ADC Left and Right */
300 	snd_soc_update_bits(codec, DA7213_ADC_L_CTRL, DA7213_ADC_EN,
301 			    DA7213_ADC_EN);
302 	snd_soc_update_bits(codec, DA7213_ADC_R_CTRL, DA7213_ADC_EN,
303 			    DA7213_ADC_EN);
304 
305 	/* Enable MIC paths */
306 	snd_soc_update_bits(codec, DA7213_MIXIN_L_SELECT,
307 			    DA7213_MIXIN_L_MIX_SELECT_MIC_1 |
308 			    DA7213_MIXIN_L_MIX_SELECT_MIC_2,
309 			    DA7213_MIXIN_L_MIX_SELECT_MIC_1 |
310 			    DA7213_MIXIN_L_MIX_SELECT_MIC_2);
311 	snd_soc_update_bits(codec, DA7213_MIXIN_R_SELECT,
312 			    DA7213_MIXIN_R_MIX_SELECT_MIC_2 |
313 			    DA7213_MIXIN_R_MIX_SELECT_MIC_1,
314 			    DA7213_MIXIN_R_MIX_SELECT_MIC_2 |
315 			    DA7213_MIXIN_R_MIX_SELECT_MIC_1);
316 
317 	/* Mute MIC PGAs */
318 	snd_soc_update_bits(codec, DA7213_MIC_1_CTRL, DA7213_MUTE_EN,
319 			    DA7213_MUTE_EN);
320 	snd_soc_update_bits(codec, DA7213_MIC_2_CTRL, DA7213_MUTE_EN,
321 			    DA7213_MUTE_EN);
322 
323 	/* Perform calibration */
324 	if (da7213->alc_calib_auto)
325 		da7213_alc_calib_auto(codec);
326 	else
327 		da7213_alc_calib_man(codec);
328 
329 	/* Restore MIXIN_L/R_SELECT registers to their original states */
330 	snd_soc_write(codec, DA7213_MIXIN_L_SELECT, mixin_l_sel);
331 	snd_soc_write(codec, DA7213_MIXIN_R_SELECT, mixin_r_sel);
332 
333 	/* Restore ADC control registers to their original states */
334 	snd_soc_write(codec, DA7213_ADC_L_CTRL, adc_l_ctrl);
335 	snd_soc_write(codec, DA7213_ADC_R_CTRL, adc_r_ctrl);
336 
337 	/* Restore original values of MIC control registers */
338 	snd_soc_write(codec, DA7213_MIC_1_CTRL, mic_1_ctrl);
339 	snd_soc_write(codec, DA7213_MIC_2_CTRL, mic_2_ctrl);
340 }
341 
342 static int da7213_put_mixin_gain(struct snd_kcontrol *kcontrol,
343 				struct snd_ctl_elem_value *ucontrol)
344 {
345 	struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
346 	struct da7213_priv *da7213 = snd_soc_codec_get_drvdata(codec);
347 	int ret;
348 
349 	ret = snd_soc_put_volsw_2r(kcontrol, ucontrol);
350 
351 	/* If ALC in operation, make sure calibrated offsets are updated */
352 	if ((!ret) && (da7213->alc_en))
353 		da7213_alc_calib(codec);
354 
355 	return ret;
356 }
357 
358 static int da7213_put_alc_sw(struct snd_kcontrol *kcontrol,
359 			    struct snd_ctl_elem_value *ucontrol)
360 {
361 	struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
362 	struct da7213_priv *da7213 = snd_soc_codec_get_drvdata(codec);
363 
364 	/* Force ALC offset calibration if enabling ALC */
365 	if (ucontrol->value.integer.value[0] ||
366 	    ucontrol->value.integer.value[1]) {
367 		if (!da7213->alc_en) {
368 			da7213_alc_calib(codec);
369 			da7213->alc_en = true;
370 		}
371 	} else {
372 		da7213->alc_en = false;
373 	}
374 
375 	return snd_soc_put_volsw(kcontrol, ucontrol);
376 }
377 
378 
379 /*
380  * KControls
381  */
382 
383 static const struct snd_kcontrol_new da7213_snd_controls[] = {
384 
385 	/* Volume controls */
386 	SOC_SINGLE_TLV("Mic 1 Volume", DA7213_MIC_1_GAIN,
387 		       DA7213_MIC_AMP_GAIN_SHIFT, DA7213_MIC_AMP_GAIN_MAX,
388 		       DA7213_NO_INVERT, mic_vol_tlv),
389 	SOC_SINGLE_TLV("Mic 2 Volume", DA7213_MIC_2_GAIN,
390 		       DA7213_MIC_AMP_GAIN_SHIFT, DA7213_MIC_AMP_GAIN_MAX,
391 		       DA7213_NO_INVERT, mic_vol_tlv),
392 	SOC_DOUBLE_R_TLV("Aux Volume", DA7213_AUX_L_GAIN, DA7213_AUX_R_GAIN,
393 			 DA7213_AUX_AMP_GAIN_SHIFT, DA7213_AUX_AMP_GAIN_MAX,
394 			 DA7213_NO_INVERT, aux_vol_tlv),
395 	SOC_DOUBLE_R_EXT_TLV("Mixin PGA Volume", DA7213_MIXIN_L_GAIN,
396 			     DA7213_MIXIN_R_GAIN, DA7213_MIXIN_AMP_GAIN_SHIFT,
397 			     DA7213_MIXIN_AMP_GAIN_MAX, DA7213_NO_INVERT,
398 			     snd_soc_get_volsw_2r, da7213_put_mixin_gain,
399 			     mixin_gain_tlv),
400 	SOC_DOUBLE_R_TLV("ADC Volume", DA7213_ADC_L_GAIN, DA7213_ADC_R_GAIN,
401 			 DA7213_ADC_AMP_GAIN_SHIFT, DA7213_ADC_AMP_GAIN_MAX,
402 			 DA7213_NO_INVERT, digital_gain_tlv),
403 	SOC_DOUBLE_R_TLV("DAC Volume", DA7213_DAC_L_GAIN, DA7213_DAC_R_GAIN,
404 			 DA7213_DAC_AMP_GAIN_SHIFT, DA7213_DAC_AMP_GAIN_MAX,
405 			 DA7213_NO_INVERT, digital_gain_tlv),
406 	SOC_DOUBLE_R_TLV("Headphone Volume", DA7213_HP_L_GAIN, DA7213_HP_R_GAIN,
407 			 DA7213_HP_AMP_GAIN_SHIFT, DA7213_HP_AMP_GAIN_MAX,
408 			 DA7213_NO_INVERT, hp_vol_tlv),
409 	SOC_SINGLE_TLV("Lineout Volume", DA7213_LINE_GAIN,
410 		       DA7213_LINE_AMP_GAIN_SHIFT, DA7213_LINE_AMP_GAIN_MAX,
411 		       DA7213_NO_INVERT, lineout_vol_tlv),
412 
413 	/* DAC Equalizer controls */
414 	SOC_SINGLE("DAC EQ Switch", DA7213_DAC_FILTERS4, DA7213_DAC_EQ_EN_SHIFT,
415 		   DA7213_DAC_EQ_EN_MAX, DA7213_NO_INVERT),
416 	SOC_SINGLE_TLV("DAC EQ1 Volume", DA7213_DAC_FILTERS2,
417 		       DA7213_DAC_EQ_BAND1_SHIFT, DA7213_DAC_EQ_BAND_MAX,
418 		       DA7213_NO_INVERT, eq_gain_tlv),
419 	SOC_SINGLE_TLV("DAC EQ2 Volume", DA7213_DAC_FILTERS2,
420 		       DA7213_DAC_EQ_BAND2_SHIFT, DA7213_DAC_EQ_BAND_MAX,
421 		       DA7213_NO_INVERT, eq_gain_tlv),
422 	SOC_SINGLE_TLV("DAC EQ3 Volume", DA7213_DAC_FILTERS3,
423 		       DA7213_DAC_EQ_BAND3_SHIFT, DA7213_DAC_EQ_BAND_MAX,
424 		       DA7213_NO_INVERT, eq_gain_tlv),
425 	SOC_SINGLE_TLV("DAC EQ4 Volume", DA7213_DAC_FILTERS3,
426 		       DA7213_DAC_EQ_BAND4_SHIFT, DA7213_DAC_EQ_BAND_MAX,
427 		       DA7213_NO_INVERT, eq_gain_tlv),
428 	SOC_SINGLE_TLV("DAC EQ5 Volume", DA7213_DAC_FILTERS4,
429 		       DA7213_DAC_EQ_BAND5_SHIFT, DA7213_DAC_EQ_BAND_MAX,
430 		       DA7213_NO_INVERT, eq_gain_tlv),
431 
432 	/* High Pass Filter and Voice Mode controls */
433 	SOC_SINGLE("ADC HPF Switch", DA7213_ADC_FILTERS1, DA7213_HPF_EN_SHIFT,
434 		   DA7213_HPF_EN_MAX, DA7213_NO_INVERT),
435 	SOC_ENUM("ADC HPF Cutoff", da7213_adc_audio_hpf_corner),
436 	SOC_SINGLE("ADC Voice Mode Switch", DA7213_ADC_FILTERS1,
437 		   DA7213_VOICE_EN_SHIFT, DA7213_VOICE_EN_MAX,
438 		   DA7213_NO_INVERT),
439 	SOC_ENUM("ADC Voice Cutoff", da7213_adc_voice_hpf_corner),
440 
441 	SOC_SINGLE("DAC HPF Switch", DA7213_DAC_FILTERS1, DA7213_HPF_EN_SHIFT,
442 		   DA7213_HPF_EN_MAX, DA7213_NO_INVERT),
443 	SOC_ENUM("DAC HPF Cutoff", da7213_dac_audio_hpf_corner),
444 	SOC_SINGLE("DAC Voice Mode Switch", DA7213_DAC_FILTERS1,
445 		   DA7213_VOICE_EN_SHIFT, DA7213_VOICE_EN_MAX,
446 		   DA7213_NO_INVERT),
447 	SOC_ENUM("DAC Voice Cutoff", da7213_dac_voice_hpf_corner),
448 
449 	/* Mute controls */
450 	SOC_SINGLE("Mic 1 Switch", DA7213_MIC_1_CTRL, DA7213_MUTE_EN_SHIFT,
451 		   DA7213_MUTE_EN_MAX, DA7213_INVERT),
452 	SOC_SINGLE("Mic 2 Switch", DA7213_MIC_2_CTRL, DA7213_MUTE_EN_SHIFT,
453 		   DA7213_MUTE_EN_MAX, DA7213_INVERT),
454 	SOC_DOUBLE_R("Aux Switch", DA7213_AUX_L_CTRL, DA7213_AUX_R_CTRL,
455 		     DA7213_MUTE_EN_SHIFT, DA7213_MUTE_EN_MAX, DA7213_INVERT),
456 	SOC_DOUBLE_R("Mixin PGA Switch", DA7213_MIXIN_L_CTRL,
457 		     DA7213_MIXIN_R_CTRL, DA7213_MUTE_EN_SHIFT,
458 		     DA7213_MUTE_EN_MAX, DA7213_INVERT),
459 	SOC_DOUBLE_R("ADC Switch", DA7213_ADC_L_CTRL, DA7213_ADC_R_CTRL,
460 		     DA7213_MUTE_EN_SHIFT, DA7213_MUTE_EN_MAX, DA7213_INVERT),
461 	SOC_DOUBLE_R("Headphone Switch", DA7213_HP_L_CTRL, DA7213_HP_R_CTRL,
462 		     DA7213_MUTE_EN_SHIFT, DA7213_MUTE_EN_MAX, DA7213_INVERT),
463 	SOC_SINGLE("Lineout Switch", DA7213_LINE_CTRL, DA7213_MUTE_EN_SHIFT,
464 		   DA7213_MUTE_EN_MAX, DA7213_INVERT),
465 	SOC_SINGLE("DAC Soft Mute Switch", DA7213_DAC_FILTERS5,
466 		   DA7213_DAC_SOFTMUTE_EN_SHIFT, DA7213_DAC_SOFTMUTE_EN_MAX,
467 		   DA7213_NO_INVERT),
468 	SOC_ENUM("DAC Soft Mute Rate", da7213_dac_soft_mute_rate),
469 
470 	/* Zero Cross controls */
471 	SOC_DOUBLE_R("Aux ZC Switch", DA7213_AUX_L_CTRL, DA7213_AUX_R_CTRL,
472 		     DA7213_ZC_EN_SHIFT, DA7213_ZC_EN_MAX, DA7213_NO_INVERT),
473 	SOC_DOUBLE_R("Mixin PGA ZC Switch", DA7213_MIXIN_L_CTRL,
474 		     DA7213_MIXIN_R_CTRL, DA7213_ZC_EN_SHIFT, DA7213_ZC_EN_MAX,
475 		     DA7213_NO_INVERT),
476 	SOC_DOUBLE_R("Headphone ZC Switch", DA7213_HP_L_CTRL, DA7213_HP_R_CTRL,
477 		     DA7213_ZC_EN_SHIFT, DA7213_ZC_EN_MAX, DA7213_NO_INVERT),
478 
479 	/* Gain Ramping controls */
480 	SOC_DOUBLE_R("Aux Gain Ramping Switch", DA7213_AUX_L_CTRL,
481 		     DA7213_AUX_R_CTRL, DA7213_GAIN_RAMP_EN_SHIFT,
482 		     DA7213_GAIN_RAMP_EN_MAX, DA7213_NO_INVERT),
483 	SOC_DOUBLE_R("Mixin Gain Ramping Switch", DA7213_MIXIN_L_CTRL,
484 		     DA7213_MIXIN_R_CTRL, DA7213_GAIN_RAMP_EN_SHIFT,
485 		     DA7213_GAIN_RAMP_EN_MAX, DA7213_NO_INVERT),
486 	SOC_DOUBLE_R("ADC Gain Ramping Switch", DA7213_ADC_L_CTRL,
487 		     DA7213_ADC_R_CTRL, DA7213_GAIN_RAMP_EN_SHIFT,
488 		     DA7213_GAIN_RAMP_EN_MAX, DA7213_NO_INVERT),
489 	SOC_DOUBLE_R("DAC Gain Ramping Switch", DA7213_DAC_L_CTRL,
490 		     DA7213_DAC_R_CTRL, DA7213_GAIN_RAMP_EN_SHIFT,
491 		     DA7213_GAIN_RAMP_EN_MAX, DA7213_NO_INVERT),
492 	SOC_DOUBLE_R("Headphone Gain Ramping Switch", DA7213_HP_L_CTRL,
493 		     DA7213_HP_R_CTRL, DA7213_GAIN_RAMP_EN_SHIFT,
494 		     DA7213_GAIN_RAMP_EN_MAX, DA7213_NO_INVERT),
495 	SOC_SINGLE("Lineout Gain Ramping Switch", DA7213_LINE_CTRL,
496 		   DA7213_GAIN_RAMP_EN_SHIFT, DA7213_GAIN_RAMP_EN_MAX,
497 		   DA7213_NO_INVERT),
498 	SOC_ENUM("Gain Ramping Rate", da7213_gain_ramp_rate),
499 
500 	/* DAC Noise Gate controls */
501 	SOC_SINGLE("DAC NG Switch", DA7213_DAC_NG_CTRL, DA7213_DAC_NG_EN_SHIFT,
502 		   DA7213_DAC_NG_EN_MAX, DA7213_NO_INVERT),
503 	SOC_ENUM("DAC NG Setup Time", da7213_dac_ng_setup_time),
504 	SOC_ENUM("DAC NG Rampup Rate", da7213_dac_ng_rampup_rate),
505 	SOC_ENUM("DAC NG Rampdown Rate", da7213_dac_ng_rampdown_rate),
506 	SOC_SINGLE("DAC NG OFF Threshold", DA7213_DAC_NG_OFF_THRESHOLD,
507 		   DA7213_DAC_NG_THRESHOLD_SHIFT, DA7213_DAC_NG_THRESHOLD_MAX,
508 		   DA7213_NO_INVERT),
509 	SOC_SINGLE("DAC NG ON Threshold", DA7213_DAC_NG_ON_THRESHOLD,
510 		   DA7213_DAC_NG_THRESHOLD_SHIFT, DA7213_DAC_NG_THRESHOLD_MAX,
511 		   DA7213_NO_INVERT),
512 
513 	/* DAC Routing & Inversion */
514 	SOC_DOUBLE("DAC Mono Switch", DA7213_DIG_ROUTING_DAC,
515 		   DA7213_DAC_L_MONO_SHIFT, DA7213_DAC_R_MONO_SHIFT,
516 		   DA7213_DAC_MONO_MAX, DA7213_NO_INVERT),
517 	SOC_DOUBLE("DAC Invert Switch", DA7213_DIG_CTRL, DA7213_DAC_L_INV_SHIFT,
518 		   DA7213_DAC_R_INV_SHIFT, DA7213_DAC_INV_MAX,
519 		   DA7213_NO_INVERT),
520 
521 	/* DMIC controls */
522 	SOC_DOUBLE_R("DMIC Switch", DA7213_MIXIN_L_SELECT,
523 		     DA7213_MIXIN_R_SELECT, DA7213_DMIC_EN_SHIFT,
524 		     DA7213_DMIC_EN_MAX, DA7213_NO_INVERT),
525 
526 	/* ALC Controls */
527 	SOC_DOUBLE_EXT("ALC Switch", DA7213_ALC_CTRL1, DA7213_ALC_L_EN_SHIFT,
528 		       DA7213_ALC_R_EN_SHIFT, DA7213_ALC_EN_MAX,
529 		       DA7213_NO_INVERT, snd_soc_get_volsw, da7213_put_alc_sw),
530 	SOC_ENUM("ALC Attack Rate", da7213_alc_attack_rate),
531 	SOC_ENUM("ALC Release Rate", da7213_alc_release_rate),
532 	SOC_ENUM("ALC Hold Time", da7213_alc_hold_time),
533 	/*
534 	 * Rate at which input signal envelope is tracked as the signal gets
535 	 * larger
536 	 */
537 	SOC_ENUM("ALC Integ Attack Rate", da7213_alc_integ_attack_rate),
538 	/*
539 	 * Rate at which input signal envelope is tracked as the signal gets
540 	 * smaller
541 	 */
542 	SOC_ENUM("ALC Integ Release Rate", da7213_alc_integ_release_rate),
543 	SOC_SINGLE_TLV("ALC Noise Threshold Volume", DA7213_ALC_NOISE,
544 		       DA7213_ALC_THRESHOLD_SHIFT, DA7213_ALC_THRESHOLD_MAX,
545 		       DA7213_INVERT, alc_threshold_tlv),
546 	SOC_SINGLE_TLV("ALC Min Threshold Volume", DA7213_ALC_TARGET_MIN,
547 		       DA7213_ALC_THRESHOLD_SHIFT, DA7213_ALC_THRESHOLD_MAX,
548 		       DA7213_INVERT, alc_threshold_tlv),
549 	SOC_SINGLE_TLV("ALC Max Threshold Volume", DA7213_ALC_TARGET_MAX,
550 		       DA7213_ALC_THRESHOLD_SHIFT, DA7213_ALC_THRESHOLD_MAX,
551 		       DA7213_INVERT, alc_threshold_tlv),
552 	SOC_SINGLE_TLV("ALC Max Attenuation Volume", DA7213_ALC_GAIN_LIMITS,
553 		       DA7213_ALC_ATTEN_MAX_SHIFT,
554 		       DA7213_ALC_ATTEN_GAIN_MAX_MAX, DA7213_NO_INVERT,
555 		       alc_gain_tlv),
556 	SOC_SINGLE_TLV("ALC Max Gain Volume", DA7213_ALC_GAIN_LIMITS,
557 		       DA7213_ALC_GAIN_MAX_SHIFT, DA7213_ALC_ATTEN_GAIN_MAX_MAX,
558 		       DA7213_NO_INVERT, alc_gain_tlv),
559 	SOC_SINGLE_TLV("ALC Min Analog Gain Volume", DA7213_ALC_ANA_GAIN_LIMITS,
560 		       DA7213_ALC_ANA_GAIN_MIN_SHIFT, DA7213_ALC_ANA_GAIN_MAX,
561 		       DA7213_NO_INVERT, alc_analog_gain_tlv),
562 	SOC_SINGLE_TLV("ALC Max Analog Gain Volume", DA7213_ALC_ANA_GAIN_LIMITS,
563 		       DA7213_ALC_ANA_GAIN_MAX_SHIFT, DA7213_ALC_ANA_GAIN_MAX,
564 		       DA7213_NO_INVERT, alc_analog_gain_tlv),
565 	SOC_SINGLE("ALC Anticlip Mode Switch", DA7213_ALC_ANTICLIP_CTRL,
566 		   DA7213_ALC_ANTICLIP_EN_SHIFT, DA7213_ALC_ANTICLIP_EN_MAX,
567 		   DA7213_NO_INVERT),
568 	SOC_SINGLE("ALC Anticlip Level", DA7213_ALC_ANTICLIP_LEVEL,
569 		   DA7213_ALC_ANTICLIP_LEVEL_SHIFT,
570 		   DA7213_ALC_ANTICLIP_LEVEL_MAX, DA7213_NO_INVERT),
571 };
572 
573 
574 /*
575  * DAPM
576  */
577 
578 /*
579  * Enums
580  */
581 
582 /* MIC PGA source select */
583 static const char * const da7213_mic_amp_in_sel_txt[] = {
584 	"Differential", "MIC_P", "MIC_N"
585 };
586 
587 static SOC_ENUM_SINGLE_DECL(da7213_mic_1_amp_in_sel,
588 			    DA7213_MIC_1_CTRL,
589 			    DA7213_MIC_AMP_IN_SEL_SHIFT,
590 			    da7213_mic_amp_in_sel_txt);
591 static const struct snd_kcontrol_new da7213_mic_1_amp_in_sel_mux =
592 	SOC_DAPM_ENUM("Mic 1 Amp Source MUX", da7213_mic_1_amp_in_sel);
593 
594 static SOC_ENUM_SINGLE_DECL(da7213_mic_2_amp_in_sel,
595 			    DA7213_MIC_2_CTRL,
596 			    DA7213_MIC_AMP_IN_SEL_SHIFT,
597 			    da7213_mic_amp_in_sel_txt);
598 static const struct snd_kcontrol_new da7213_mic_2_amp_in_sel_mux =
599 	SOC_DAPM_ENUM("Mic 2 Amp Source MUX", da7213_mic_2_amp_in_sel);
600 
601 /* DAI routing select */
602 static const char * const da7213_dai_src_txt[] = {
603 	"ADC Left", "ADC Right", "DAI Input Left", "DAI Input Right"
604 };
605 
606 static SOC_ENUM_SINGLE_DECL(da7213_dai_l_src,
607 			    DA7213_DIG_ROUTING_DAI,
608 			    DA7213_DAI_L_SRC_SHIFT,
609 			    da7213_dai_src_txt);
610 static const struct snd_kcontrol_new da7213_dai_l_src_mux =
611 	SOC_DAPM_ENUM("DAI Left Source MUX", da7213_dai_l_src);
612 
613 static SOC_ENUM_SINGLE_DECL(da7213_dai_r_src,
614 			    DA7213_DIG_ROUTING_DAI,
615 			    DA7213_DAI_R_SRC_SHIFT,
616 			    da7213_dai_src_txt);
617 static const struct snd_kcontrol_new da7213_dai_r_src_mux =
618 	SOC_DAPM_ENUM("DAI Right Source MUX", da7213_dai_r_src);
619 
620 /* DAC routing select */
621 static const char * const da7213_dac_src_txt[] = {
622 	"ADC Output Left", "ADC Output Right", "DAI Input Left",
623 	"DAI Input Right"
624 };
625 
626 static SOC_ENUM_SINGLE_DECL(da7213_dac_l_src,
627 			    DA7213_DIG_ROUTING_DAC,
628 			    DA7213_DAC_L_SRC_SHIFT,
629 			    da7213_dac_src_txt);
630 static const struct snd_kcontrol_new da7213_dac_l_src_mux =
631 	SOC_DAPM_ENUM("DAC Left Source MUX", da7213_dac_l_src);
632 
633 static SOC_ENUM_SINGLE_DECL(da7213_dac_r_src,
634 			    DA7213_DIG_ROUTING_DAC,
635 			    DA7213_DAC_R_SRC_SHIFT,
636 			    da7213_dac_src_txt);
637 static const struct snd_kcontrol_new da7213_dac_r_src_mux =
638 	SOC_DAPM_ENUM("DAC Right Source MUX", da7213_dac_r_src);
639 
640 /*
641  * Mixer Controls
642  */
643 
644 /* Mixin Left */
645 static const struct snd_kcontrol_new da7213_dapm_mixinl_controls[] = {
646 	SOC_DAPM_SINGLE("Aux Left Switch", DA7213_MIXIN_L_SELECT,
647 			DA7213_MIXIN_L_MIX_SELECT_AUX_L_SHIFT,
648 			DA7213_MIXIN_L_MIX_SELECT_MAX, DA7213_NO_INVERT),
649 	SOC_DAPM_SINGLE("Mic 1 Switch", DA7213_MIXIN_L_SELECT,
650 			DA7213_MIXIN_L_MIX_SELECT_MIC_1_SHIFT,
651 			DA7213_MIXIN_L_MIX_SELECT_MAX, DA7213_NO_INVERT),
652 	SOC_DAPM_SINGLE("Mic 2 Switch", DA7213_MIXIN_L_SELECT,
653 			DA7213_MIXIN_L_MIX_SELECT_MIC_2_SHIFT,
654 			DA7213_MIXIN_L_MIX_SELECT_MAX, DA7213_NO_INVERT),
655 	SOC_DAPM_SINGLE("Mixin Right Switch", DA7213_MIXIN_L_SELECT,
656 			DA7213_MIXIN_L_MIX_SELECT_MIXIN_R_SHIFT,
657 			DA7213_MIXIN_L_MIX_SELECT_MAX, DA7213_NO_INVERT),
658 };
659 
660 /* Mixin Right */
661 static const struct snd_kcontrol_new da7213_dapm_mixinr_controls[] = {
662 	SOC_DAPM_SINGLE("Aux Right Switch", DA7213_MIXIN_R_SELECT,
663 			DA7213_MIXIN_R_MIX_SELECT_AUX_R_SHIFT,
664 			DA7213_MIXIN_R_MIX_SELECT_MAX, DA7213_NO_INVERT),
665 	SOC_DAPM_SINGLE("Mic 2 Switch", DA7213_MIXIN_R_SELECT,
666 			DA7213_MIXIN_R_MIX_SELECT_MIC_2_SHIFT,
667 			DA7213_MIXIN_R_MIX_SELECT_MAX, DA7213_NO_INVERT),
668 	SOC_DAPM_SINGLE("Mic 1 Switch", DA7213_MIXIN_R_SELECT,
669 			DA7213_MIXIN_R_MIX_SELECT_MIC_1_SHIFT,
670 			DA7213_MIXIN_R_MIX_SELECT_MAX, DA7213_NO_INVERT),
671 	SOC_DAPM_SINGLE("Mixin Left Switch", DA7213_MIXIN_R_SELECT,
672 			DA7213_MIXIN_R_MIX_SELECT_MIXIN_L_SHIFT,
673 			DA7213_MIXIN_R_MIX_SELECT_MAX, DA7213_NO_INVERT),
674 };
675 
676 /* Mixout Left */
677 static const struct snd_kcontrol_new da7213_dapm_mixoutl_controls[] = {
678 	SOC_DAPM_SINGLE("Aux Left Switch", DA7213_MIXOUT_L_SELECT,
679 			DA7213_MIXOUT_L_MIX_SELECT_AUX_L_SHIFT,
680 			DA7213_MIXOUT_L_MIX_SELECT_MAX, DA7213_NO_INVERT),
681 	SOC_DAPM_SINGLE("Mixin Left Switch", DA7213_MIXOUT_L_SELECT,
682 			DA7213_MIXOUT_L_MIX_SELECT_MIXIN_L_SHIFT,
683 			DA7213_MIXOUT_L_MIX_SELECT_MAX, DA7213_NO_INVERT),
684 	SOC_DAPM_SINGLE("Mixin Right Switch", DA7213_MIXOUT_L_SELECT,
685 			DA7213_MIXOUT_L_MIX_SELECT_MIXIN_R_SHIFT,
686 			DA7213_MIXOUT_L_MIX_SELECT_MAX, DA7213_NO_INVERT),
687 	SOC_DAPM_SINGLE("DAC Left Switch", DA7213_MIXOUT_L_SELECT,
688 			DA7213_MIXOUT_L_MIX_SELECT_DAC_L_SHIFT,
689 			DA7213_MIXOUT_L_MIX_SELECT_MAX, DA7213_NO_INVERT),
690 	SOC_DAPM_SINGLE("Aux Left Invert Switch", DA7213_MIXOUT_L_SELECT,
691 			DA7213_MIXOUT_L_MIX_SELECT_AUX_L_INVERTED_SHIFT,
692 			DA7213_MIXOUT_L_MIX_SELECT_MAX, DA7213_NO_INVERT),
693 	SOC_DAPM_SINGLE("Mixin Left Invert Switch", DA7213_MIXOUT_L_SELECT,
694 			DA7213_MIXOUT_L_MIX_SELECT_MIXIN_L_INVERTED_SHIFT,
695 			DA7213_MIXOUT_L_MIX_SELECT_MAX, DA7213_NO_INVERT),
696 	SOC_DAPM_SINGLE("Mixin Right Invert Switch", DA7213_MIXOUT_L_SELECT,
697 			DA7213_MIXOUT_L_MIX_SELECT_MIXIN_R_INVERTED_SHIFT,
698 			DA7213_MIXOUT_L_MIX_SELECT_MAX, DA7213_NO_INVERT),
699 };
700 
701 /* Mixout Right */
702 static const struct snd_kcontrol_new da7213_dapm_mixoutr_controls[] = {
703 	SOC_DAPM_SINGLE("Aux Right Switch", DA7213_MIXOUT_R_SELECT,
704 			DA7213_MIXOUT_R_MIX_SELECT_AUX_R_SHIFT,
705 			DA7213_MIXOUT_R_MIX_SELECT_MAX, DA7213_NO_INVERT),
706 	SOC_DAPM_SINGLE("Mixin Right Switch", DA7213_MIXOUT_R_SELECT,
707 			DA7213_MIXOUT_R_MIX_SELECT_MIXIN_R_SHIFT,
708 			DA7213_MIXOUT_R_MIX_SELECT_MAX, DA7213_NO_INVERT),
709 	SOC_DAPM_SINGLE("Mixin Left Switch", DA7213_MIXOUT_R_SELECT,
710 			DA7213_MIXOUT_R_MIX_SELECT_MIXIN_L_SHIFT,
711 			DA7213_MIXOUT_R_MIX_SELECT_MAX, DA7213_NO_INVERT),
712 	SOC_DAPM_SINGLE("DAC Right Switch", DA7213_MIXOUT_R_SELECT,
713 			DA7213_MIXOUT_R_MIX_SELECT_DAC_R_SHIFT,
714 			DA7213_MIXOUT_R_MIX_SELECT_MAX, DA7213_NO_INVERT),
715 	SOC_DAPM_SINGLE("Aux Right Invert Switch", DA7213_MIXOUT_R_SELECT,
716 			DA7213_MIXOUT_R_MIX_SELECT_AUX_R_INVERTED_SHIFT,
717 			DA7213_MIXOUT_R_MIX_SELECT_MAX, DA7213_NO_INVERT),
718 	SOC_DAPM_SINGLE("Mixin Right Invert Switch", DA7213_MIXOUT_R_SELECT,
719 			DA7213_MIXOUT_R_MIX_SELECT_MIXIN_R_INVERTED_SHIFT,
720 			DA7213_MIXOUT_R_MIX_SELECT_MAX, DA7213_NO_INVERT),
721 	SOC_DAPM_SINGLE("Mixin Left Invert Switch", DA7213_MIXOUT_R_SELECT,
722 			DA7213_MIXOUT_R_MIX_SELECT_MIXIN_L_INVERTED_SHIFT,
723 			DA7213_MIXOUT_R_MIX_SELECT_MAX, DA7213_NO_INVERT),
724 };
725 
726 
727 /*
728  * DAPM widgets
729  */
730 
731 static const struct snd_soc_dapm_widget da7213_dapm_widgets[] = {
732 	/*
733 	 * Input & Output
734 	 */
735 
736 	/* Use a supply here as this controls both input & output DAIs */
737 	SND_SOC_DAPM_SUPPLY("DAI", DA7213_DAI_CTRL, DA7213_DAI_EN_SHIFT,
738 			    DA7213_NO_INVERT, NULL, 0),
739 
740 	/*
741 	 * Input
742 	 */
743 
744 	/* Input Lines */
745 	SND_SOC_DAPM_INPUT("MIC1"),
746 	SND_SOC_DAPM_INPUT("MIC2"),
747 	SND_SOC_DAPM_INPUT("AUXL"),
748 	SND_SOC_DAPM_INPUT("AUXR"),
749 
750 	/* MUXs for Mic PGA source selection */
751 	SND_SOC_DAPM_MUX("Mic 1 Amp Source MUX", SND_SOC_NOPM, 0, 0,
752 			 &da7213_mic_1_amp_in_sel_mux),
753 	SND_SOC_DAPM_MUX("Mic 2 Amp Source MUX", SND_SOC_NOPM, 0, 0,
754 			 &da7213_mic_2_amp_in_sel_mux),
755 
756 	/* Input PGAs */
757 	SND_SOC_DAPM_PGA("Mic 1 PGA", DA7213_MIC_1_CTRL, DA7213_AMP_EN_SHIFT,
758 			 DA7213_NO_INVERT, NULL, 0),
759 	SND_SOC_DAPM_PGA("Mic 2 PGA", DA7213_MIC_2_CTRL, DA7213_AMP_EN_SHIFT,
760 			 DA7213_NO_INVERT, NULL, 0),
761 	SND_SOC_DAPM_PGA("Aux Left PGA", DA7213_AUX_L_CTRL, DA7213_AMP_EN_SHIFT,
762 			 DA7213_NO_INVERT, NULL, 0),
763 	SND_SOC_DAPM_PGA("Aux Right PGA", DA7213_AUX_R_CTRL,
764 			 DA7213_AMP_EN_SHIFT, DA7213_NO_INVERT, NULL, 0),
765 	SND_SOC_DAPM_PGA("Mixin Left PGA", DA7213_MIXIN_L_CTRL,
766 			 DA7213_AMP_EN_SHIFT, DA7213_NO_INVERT, NULL, 0),
767 	SND_SOC_DAPM_PGA("Mixin Right PGA", DA7213_MIXIN_R_CTRL,
768 			 DA7213_AMP_EN_SHIFT, DA7213_NO_INVERT, NULL, 0),
769 
770 	/* Mic Biases */
771 	SND_SOC_DAPM_SUPPLY("Mic Bias 1", DA7213_MICBIAS_CTRL,
772 			    DA7213_MICBIAS1_EN_SHIFT, DA7213_NO_INVERT,
773 			    NULL, 0),
774 	SND_SOC_DAPM_SUPPLY("Mic Bias 2", DA7213_MICBIAS_CTRL,
775 			    DA7213_MICBIAS2_EN_SHIFT, DA7213_NO_INVERT,
776 			    NULL, 0),
777 
778 	/* Input Mixers */
779 	SND_SOC_DAPM_MIXER("Mixin Left", SND_SOC_NOPM, 0, 0,
780 			   &da7213_dapm_mixinl_controls[0],
781 			   ARRAY_SIZE(da7213_dapm_mixinl_controls)),
782 	SND_SOC_DAPM_MIXER("Mixin Right", SND_SOC_NOPM, 0, 0,
783 			   &da7213_dapm_mixinr_controls[0],
784 			   ARRAY_SIZE(da7213_dapm_mixinr_controls)),
785 
786 	/* ADCs */
787 	SND_SOC_DAPM_ADC("ADC Left", NULL, DA7213_ADC_L_CTRL,
788 			 DA7213_ADC_EN_SHIFT, DA7213_NO_INVERT),
789 	SND_SOC_DAPM_ADC("ADC Right", NULL, DA7213_ADC_R_CTRL,
790 			 DA7213_ADC_EN_SHIFT, DA7213_NO_INVERT),
791 
792 	/* DAI */
793 	SND_SOC_DAPM_MUX("DAI Left Source MUX", SND_SOC_NOPM, 0, 0,
794 			 &da7213_dai_l_src_mux),
795 	SND_SOC_DAPM_MUX("DAI Right Source MUX", SND_SOC_NOPM, 0, 0,
796 			 &da7213_dai_r_src_mux),
797 	SND_SOC_DAPM_AIF_OUT("DAIOUTL", "Capture", 0, SND_SOC_NOPM, 0, 0),
798 	SND_SOC_DAPM_AIF_OUT("DAIOUTR", "Capture", 1, SND_SOC_NOPM, 0, 0),
799 
800 	/*
801 	 * Output
802 	 */
803 
804 	/* DAI */
805 	SND_SOC_DAPM_AIF_IN("DAIINL", "Playback", 0, SND_SOC_NOPM, 0, 0),
806 	SND_SOC_DAPM_AIF_IN("DAIINR", "Playback", 1, SND_SOC_NOPM, 0, 0),
807 	SND_SOC_DAPM_MUX("DAC Left Source MUX", SND_SOC_NOPM, 0, 0,
808 			 &da7213_dac_l_src_mux),
809 	SND_SOC_DAPM_MUX("DAC Right Source MUX", SND_SOC_NOPM, 0, 0,
810 			 &da7213_dac_r_src_mux),
811 
812 	/* DACs */
813 	SND_SOC_DAPM_DAC("DAC Left", NULL, DA7213_DAC_L_CTRL,
814 			 DA7213_DAC_EN_SHIFT, DA7213_NO_INVERT),
815 	SND_SOC_DAPM_DAC("DAC Right", NULL, DA7213_DAC_R_CTRL,
816 			 DA7213_DAC_EN_SHIFT, DA7213_NO_INVERT),
817 
818 	/* Output Mixers */
819 	SND_SOC_DAPM_MIXER("Mixout Left", SND_SOC_NOPM, 0, 0,
820 			   &da7213_dapm_mixoutl_controls[0],
821 			   ARRAY_SIZE(da7213_dapm_mixoutl_controls)),
822 	SND_SOC_DAPM_MIXER("Mixout Right", SND_SOC_NOPM, 0, 0,
823 			   &da7213_dapm_mixoutr_controls[0],
824 			   ARRAY_SIZE(da7213_dapm_mixoutr_controls)),
825 
826 	/* Output PGAs */
827 	SND_SOC_DAPM_PGA("Mixout Left PGA", DA7213_MIXOUT_L_CTRL,
828 			 DA7213_AMP_EN_SHIFT, DA7213_NO_INVERT, NULL, 0),
829 	SND_SOC_DAPM_PGA("Mixout Right PGA", DA7213_MIXOUT_R_CTRL,
830 			 DA7213_AMP_EN_SHIFT, DA7213_NO_INVERT, NULL, 0),
831 	SND_SOC_DAPM_PGA("Lineout PGA", DA7213_LINE_CTRL, DA7213_AMP_EN_SHIFT,
832 			 DA7213_NO_INVERT, NULL, 0),
833 	SND_SOC_DAPM_PGA("Headphone Left PGA", DA7213_HP_L_CTRL,
834 			 DA7213_AMP_EN_SHIFT, DA7213_NO_INVERT, NULL, 0),
835 	SND_SOC_DAPM_PGA("Headphone Right PGA", DA7213_HP_R_CTRL,
836 			 DA7213_AMP_EN_SHIFT, DA7213_NO_INVERT, NULL, 0),
837 
838 	/* Charge Pump */
839 	SND_SOC_DAPM_SUPPLY("Charge Pump", DA7213_CP_CTRL, DA7213_CP_EN_SHIFT,
840 			    DA7213_NO_INVERT, NULL, 0),
841 
842 	/* Output Lines */
843 	SND_SOC_DAPM_OUTPUT("HPL"),
844 	SND_SOC_DAPM_OUTPUT("HPR"),
845 	SND_SOC_DAPM_OUTPUT("LINE"),
846 };
847 
848 
849 /*
850  * DAPM audio route definition
851  */
852 
853 static const struct snd_soc_dapm_route da7213_audio_map[] = {
854 	/* Dest       Connecting Widget    source */
855 
856 	/* Input path */
857 	{"MIC1", NULL, "Mic Bias 1"},
858 	{"MIC2", NULL, "Mic Bias 2"},
859 
860 	{"Mic 1 Amp Source MUX", "Differential", "MIC1"},
861 	{"Mic 1 Amp Source MUX", "MIC_P", "MIC1"},
862 	{"Mic 1 Amp Source MUX", "MIC_N", "MIC1"},
863 
864 	{"Mic 2 Amp Source MUX", "Differential", "MIC2"},
865 	{"Mic 2 Amp Source MUX", "MIC_P", "MIC2"},
866 	{"Mic 2 Amp Source MUX", "MIC_N", "MIC2"},
867 
868 	{"Mic 1 PGA", NULL, "Mic 1 Amp Source MUX"},
869 	{"Mic 2 PGA", NULL, "Mic 2 Amp Source MUX"},
870 
871 	{"Aux Left PGA", NULL, "AUXL"},
872 	{"Aux Right PGA", NULL, "AUXR"},
873 
874 	{"Mixin Left", "Aux Left Switch", "Aux Left PGA"},
875 	{"Mixin Left", "Mic 1 Switch", "Mic 1 PGA"},
876 	{"Mixin Left", "Mic 2 Switch", "Mic 2 PGA"},
877 	{"Mixin Left", "Mixin Right Switch", "Mixin Right PGA"},
878 
879 	{"Mixin Right", "Aux Right Switch", "Aux Right PGA"},
880 	{"Mixin Right", "Mic 2 Switch", "Mic 2 PGA"},
881 	{"Mixin Right", "Mic 1 Switch", "Mic 1 PGA"},
882 	{"Mixin Right", "Mixin Left Switch", "Mixin Left PGA"},
883 
884 	{"Mixin Left PGA", NULL, "Mixin Left"},
885 	{"ADC Left", NULL, "Mixin Left PGA"},
886 
887 	{"Mixin Right PGA", NULL, "Mixin Right"},
888 	{"ADC Right", NULL, "Mixin Right PGA"},
889 
890 	{"DAI Left Source MUX", "ADC Left", "ADC Left"},
891 	{"DAI Left Source MUX", "ADC Right", "ADC Right"},
892 	{"DAI Left Source MUX", "DAI Input Left", "DAIINL"},
893 	{"DAI Left Source MUX", "DAI Input Right", "DAIINR"},
894 
895 	{"DAI Right Source MUX", "ADC Left", "ADC Left"},
896 	{"DAI Right Source MUX", "ADC Right", "ADC Right"},
897 	{"DAI Right Source MUX", "DAI Input Left", "DAIINL"},
898 	{"DAI Right Source MUX", "DAI Input Right", "DAIINR"},
899 
900 	{"DAIOUTL", NULL, "DAI Left Source MUX"},
901 	{"DAIOUTR", NULL, "DAI Right Source MUX"},
902 
903 	{"DAIOUTL", NULL, "DAI"},
904 	{"DAIOUTR", NULL, "DAI"},
905 
906 	/* Output path */
907 	{"DAIINL", NULL, "DAI"},
908 	{"DAIINR", NULL, "DAI"},
909 
910 	{"DAC Left Source MUX", "ADC Output Left", "ADC Left"},
911 	{"DAC Left Source MUX", "ADC Output Right", "ADC Right"},
912 	{"DAC Left Source MUX", "DAI Input Left", "DAIINL"},
913 	{"DAC Left Source MUX", "DAI Input Right", "DAIINR"},
914 
915 	{"DAC Right Source MUX", "ADC Output Left", "ADC Left"},
916 	{"DAC Right Source MUX", "ADC Output Right", "ADC Right"},
917 	{"DAC Right Source MUX", "DAI Input Left", "DAIINL"},
918 	{"DAC Right Source MUX", "DAI Input Right", "DAIINR"},
919 
920 	{"DAC Left", NULL, "DAC Left Source MUX"},
921 	{"DAC Right", NULL, "DAC Right Source MUX"},
922 
923 	{"Mixout Left", "Aux Left Switch", "Aux Left PGA"},
924 	{"Mixout Left", "Mixin Left Switch", "Mixin Left PGA"},
925 	{"Mixout Left", "Mixin Right Switch", "Mixin Right PGA"},
926 	{"Mixout Left", "DAC Left Switch", "DAC Left"},
927 	{"Mixout Left", "Aux Left Invert Switch", "Aux Left PGA"},
928 	{"Mixout Left", "Mixin Left Invert Switch", "Mixin Left PGA"},
929 	{"Mixout Left", "Mixin Right Invert Switch", "Mixin Right PGA"},
930 
931 	{"Mixout Right", "Aux Right Switch", "Aux Right PGA"},
932 	{"Mixout Right", "Mixin Right Switch", "Mixin Right PGA"},
933 	{"Mixout Right", "Mixin Left Switch", "Mixin Left PGA"},
934 	{"Mixout Right", "DAC Right Switch", "DAC Right"},
935 	{"Mixout Right", "Aux Right Invert Switch", "Aux Right PGA"},
936 	{"Mixout Right", "Mixin Right Invert Switch", "Mixin Right PGA"},
937 	{"Mixout Right", "Mixin Left Invert Switch", "Mixin Left PGA"},
938 
939 	{"Mixout Left PGA", NULL, "Mixout Left"},
940 	{"Mixout Right PGA", NULL, "Mixout Right"},
941 
942 	{"Headphone Left PGA", NULL, "Mixout Left PGA"},
943 	{"Headphone Left PGA", NULL, "Charge Pump"},
944 	{"HPL", NULL, "Headphone Left PGA"},
945 
946 	{"Headphone Right PGA", NULL, "Mixout Right PGA"},
947 	{"Headphone Right PGA", NULL, "Charge Pump"},
948 	{"HPR", NULL, "Headphone Right PGA"},
949 
950 	{"Lineout PGA", NULL, "Mixout Right PGA"},
951 	{"LINE", NULL, "Lineout PGA"},
952 };
953 
954 static const struct reg_default da7213_reg_defaults[] = {
955 	{ DA7213_DIG_ROUTING_DAI, 0x10 },
956 	{ DA7213_SR, 0x0A },
957 	{ DA7213_REFERENCES, 0x80 },
958 	{ DA7213_PLL_FRAC_TOP, 0x00 },
959 	{ DA7213_PLL_FRAC_BOT, 0x00 },
960 	{ DA7213_PLL_INTEGER, 0x20 },
961 	{ DA7213_PLL_CTRL, 0x0C },
962 	{ DA7213_DAI_CLK_MODE, 0x01 },
963 	{ DA7213_DAI_CTRL, 0x08 },
964 	{ DA7213_DIG_ROUTING_DAC, 0x32 },
965 	{ DA7213_AUX_L_GAIN, 0x35 },
966 	{ DA7213_AUX_R_GAIN, 0x35 },
967 	{ DA7213_MIXIN_L_SELECT, 0x00 },
968 	{ DA7213_MIXIN_R_SELECT, 0x00 },
969 	{ DA7213_MIXIN_L_GAIN, 0x03 },
970 	{ DA7213_MIXIN_R_GAIN, 0x03 },
971 	{ DA7213_ADC_L_GAIN, 0x6F },
972 	{ DA7213_ADC_R_GAIN, 0x6F },
973 	{ DA7213_ADC_FILTERS1, 0x80 },
974 	{ DA7213_MIC_1_GAIN, 0x01 },
975 	{ DA7213_MIC_2_GAIN, 0x01 },
976 	{ DA7213_DAC_FILTERS5, 0x00 },
977 	{ DA7213_DAC_FILTERS2, 0x88 },
978 	{ DA7213_DAC_FILTERS3, 0x88 },
979 	{ DA7213_DAC_FILTERS4, 0x08 },
980 	{ DA7213_DAC_FILTERS1, 0x80 },
981 	{ DA7213_DAC_L_GAIN, 0x6F },
982 	{ DA7213_DAC_R_GAIN, 0x6F },
983 	{ DA7213_CP_CTRL, 0x61 },
984 	{ DA7213_HP_L_GAIN, 0x39 },
985 	{ DA7213_HP_R_GAIN, 0x39 },
986 	{ DA7213_LINE_GAIN, 0x30 },
987 	{ DA7213_MIXOUT_L_SELECT, 0x00 },
988 	{ DA7213_MIXOUT_R_SELECT, 0x00 },
989 	{ DA7213_SYSTEM_MODES_INPUT, 0x00 },
990 	{ DA7213_SYSTEM_MODES_OUTPUT, 0x00 },
991 	{ DA7213_AUX_L_CTRL, 0x44 },
992 	{ DA7213_AUX_R_CTRL, 0x44 },
993 	{ DA7213_MICBIAS_CTRL, 0x11 },
994 	{ DA7213_MIC_1_CTRL, 0x40 },
995 	{ DA7213_MIC_2_CTRL, 0x40 },
996 	{ DA7213_MIXIN_L_CTRL, 0x40 },
997 	{ DA7213_MIXIN_R_CTRL, 0x40 },
998 	{ DA7213_ADC_L_CTRL, 0x40 },
999 	{ DA7213_ADC_R_CTRL, 0x40 },
1000 	{ DA7213_DAC_L_CTRL, 0x48 },
1001 	{ DA7213_DAC_R_CTRL, 0x40 },
1002 	{ DA7213_HP_L_CTRL, 0x41 },
1003 	{ DA7213_HP_R_CTRL, 0x40 },
1004 	{ DA7213_LINE_CTRL, 0x40 },
1005 	{ DA7213_MIXOUT_L_CTRL, 0x10 },
1006 	{ DA7213_MIXOUT_R_CTRL, 0x10 },
1007 	{ DA7213_LDO_CTRL, 0x00 },
1008 	{ DA7213_IO_CTRL, 0x00 },
1009 	{ DA7213_GAIN_RAMP_CTRL, 0x00},
1010 	{ DA7213_MIC_CONFIG, 0x00 },
1011 	{ DA7213_PC_COUNT, 0x00 },
1012 	{ DA7213_CP_VOL_THRESHOLD1, 0x32 },
1013 	{ DA7213_CP_DELAY, 0x95 },
1014 	{ DA7213_CP_DETECTOR, 0x00 },
1015 	{ DA7213_DAI_OFFSET, 0x00 },
1016 	{ DA7213_DIG_CTRL, 0x00 },
1017 	{ DA7213_ALC_CTRL2, 0x00 },
1018 	{ DA7213_ALC_CTRL3, 0x00 },
1019 	{ DA7213_ALC_NOISE, 0x3F },
1020 	{ DA7213_ALC_TARGET_MIN, 0x3F },
1021 	{ DA7213_ALC_TARGET_MAX, 0x00 },
1022 	{ DA7213_ALC_GAIN_LIMITS, 0xFF },
1023 	{ DA7213_ALC_ANA_GAIN_LIMITS, 0x71 },
1024 	{ DA7213_ALC_ANTICLIP_CTRL, 0x00 },
1025 	{ DA7213_ALC_ANTICLIP_LEVEL, 0x00 },
1026 	{ DA7213_ALC_OFFSET_MAN_M_L, 0x00 },
1027 	{ DA7213_ALC_OFFSET_MAN_U_L, 0x00 },
1028 	{ DA7213_ALC_OFFSET_MAN_M_R, 0x00 },
1029 	{ DA7213_ALC_OFFSET_MAN_U_R, 0x00 },
1030 	{ DA7213_ALC_CIC_OP_LVL_CTRL, 0x00 },
1031 	{ DA7213_DAC_NG_SETUP_TIME, 0x00 },
1032 	{ DA7213_DAC_NG_OFF_THRESHOLD, 0x00 },
1033 	{ DA7213_DAC_NG_ON_THRESHOLD, 0x00 },
1034 	{ DA7213_DAC_NG_CTRL, 0x00 },
1035 };
1036 
1037 static bool da7213_volatile_register(struct device *dev, unsigned int reg)
1038 {
1039 	switch (reg) {
1040 	case DA7213_STATUS1:
1041 	case DA7213_PLL_STATUS:
1042 	case DA7213_AUX_L_GAIN_STATUS:
1043 	case DA7213_AUX_R_GAIN_STATUS:
1044 	case DA7213_MIC_1_GAIN_STATUS:
1045 	case DA7213_MIC_2_GAIN_STATUS:
1046 	case DA7213_MIXIN_L_GAIN_STATUS:
1047 	case DA7213_MIXIN_R_GAIN_STATUS:
1048 	case DA7213_ADC_L_GAIN_STATUS:
1049 	case DA7213_ADC_R_GAIN_STATUS:
1050 	case DA7213_DAC_L_GAIN_STATUS:
1051 	case DA7213_DAC_R_GAIN_STATUS:
1052 	case DA7213_HP_L_GAIN_STATUS:
1053 	case DA7213_HP_R_GAIN_STATUS:
1054 	case DA7213_LINE_GAIN_STATUS:
1055 	case DA7213_ALC_CTRL1:
1056 	case DA7213_ALC_OFFSET_AUTO_M_L:
1057 	case DA7213_ALC_OFFSET_AUTO_U_L:
1058 	case DA7213_ALC_OFFSET_AUTO_M_R:
1059 	case DA7213_ALC_OFFSET_AUTO_U_R:
1060 	case DA7213_ALC_CIC_OP_LVL_DATA:
1061 		return 1;
1062 	default:
1063 		return 0;
1064 	}
1065 }
1066 
1067 static int da7213_hw_params(struct snd_pcm_substream *substream,
1068 			    struct snd_pcm_hw_params *params,
1069 			    struct snd_soc_dai *dai)
1070 {
1071 	struct snd_soc_codec *codec = dai->codec;
1072 	u8 dai_ctrl = 0;
1073 	u8 fs;
1074 
1075 	/* Set DAI format */
1076 	switch (params_width(params)) {
1077 	case 16:
1078 		dai_ctrl |= DA7213_DAI_WORD_LENGTH_S16_LE;
1079 		break;
1080 	case 20:
1081 		dai_ctrl |= DA7213_DAI_WORD_LENGTH_S20_LE;
1082 		break;
1083 	case 24:
1084 		dai_ctrl |= DA7213_DAI_WORD_LENGTH_S24_LE;
1085 		break;
1086 	case 32:
1087 		dai_ctrl |= DA7213_DAI_WORD_LENGTH_S32_LE;
1088 		break;
1089 	default:
1090 		return -EINVAL;
1091 	}
1092 
1093 	/* Set sampling rate */
1094 	switch (params_rate(params)) {
1095 	case 8000:
1096 		fs = DA7213_SR_8000;
1097 		break;
1098 	case 11025:
1099 		fs = DA7213_SR_11025;
1100 		break;
1101 	case 12000:
1102 		fs = DA7213_SR_12000;
1103 		break;
1104 	case 16000:
1105 		fs = DA7213_SR_16000;
1106 		break;
1107 	case 22050:
1108 		fs = DA7213_SR_22050;
1109 		break;
1110 	case 32000:
1111 		fs = DA7213_SR_32000;
1112 		break;
1113 	case 44100:
1114 		fs = DA7213_SR_44100;
1115 		break;
1116 	case 48000:
1117 		fs = DA7213_SR_48000;
1118 		break;
1119 	case 88200:
1120 		fs = DA7213_SR_88200;
1121 		break;
1122 	case 96000:
1123 		fs = DA7213_SR_96000;
1124 		break;
1125 	default:
1126 		return -EINVAL;
1127 	}
1128 
1129 	snd_soc_update_bits(codec, DA7213_DAI_CTRL, DA7213_DAI_WORD_LENGTH_MASK,
1130 			    dai_ctrl);
1131 	snd_soc_write(codec, DA7213_SR, fs);
1132 
1133 	return 0;
1134 }
1135 
1136 static int da7213_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
1137 {
1138 	struct snd_soc_codec *codec = codec_dai->codec;
1139 	struct da7213_priv *da7213 = snd_soc_codec_get_drvdata(codec);
1140 	u8 dai_clk_mode = 0, dai_ctrl = 0;
1141 
1142 	/* Set master/slave mode */
1143 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1144 	case SND_SOC_DAIFMT_CBM_CFM:
1145 		dai_clk_mode |= DA7213_DAI_CLK_EN_MASTER_MODE;
1146 		da7213->master = true;
1147 		break;
1148 	case SND_SOC_DAIFMT_CBS_CFS:
1149 		dai_clk_mode |= DA7213_DAI_CLK_EN_SLAVE_MODE;
1150 		da7213->master = false;
1151 		break;
1152 	default:
1153 		return -EINVAL;
1154 	}
1155 
1156 	/* Set clock normal/inverted */
1157 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1158 	case SND_SOC_DAIFMT_NB_NF:
1159 		break;
1160 	case SND_SOC_DAIFMT_NB_IF:
1161 		dai_clk_mode |= DA7213_DAI_WCLK_POL_INV;
1162 		break;
1163 	case SND_SOC_DAIFMT_IB_NF:
1164 		dai_clk_mode |= DA7213_DAI_CLK_POL_INV;
1165 		break;
1166 	case SND_SOC_DAIFMT_IB_IF:
1167 		dai_clk_mode |= DA7213_DAI_WCLK_POL_INV | DA7213_DAI_CLK_POL_INV;
1168 		break;
1169 	default:
1170 		return -EINVAL;
1171 	}
1172 
1173 	/* Only I2S is supported */
1174 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1175 	case SND_SOC_DAIFMT_I2S:
1176 		dai_ctrl |= DA7213_DAI_FORMAT_I2S_MODE;
1177 		break;
1178 	case SND_SOC_DAIFMT_LEFT_J:
1179 		dai_ctrl |= DA7213_DAI_FORMAT_LEFT_J;
1180 		break;
1181 	case SND_SOC_DAIFMT_RIGHT_J:
1182 		dai_ctrl |= DA7213_DAI_FORMAT_RIGHT_J;
1183 		break;
1184 	default:
1185 		return -EINVAL;
1186 	}
1187 
1188 	/* By default only 32 BCLK per WCLK is supported */
1189 	dai_clk_mode |= DA7213_DAI_BCLKS_PER_WCLK_32;
1190 
1191 	snd_soc_write(codec, DA7213_DAI_CLK_MODE, dai_clk_mode);
1192 	snd_soc_update_bits(codec, DA7213_DAI_CTRL, DA7213_DAI_FORMAT_MASK,
1193 			    dai_ctrl);
1194 
1195 	return 0;
1196 }
1197 
1198 static int da7213_mute(struct snd_soc_dai *dai, int mute)
1199 {
1200 	struct snd_soc_codec *codec = dai->codec;
1201 
1202 	if (mute) {
1203 		snd_soc_update_bits(codec, DA7213_DAC_L_CTRL,
1204 				    DA7213_MUTE_EN, DA7213_MUTE_EN);
1205 		snd_soc_update_bits(codec, DA7213_DAC_R_CTRL,
1206 				    DA7213_MUTE_EN, DA7213_MUTE_EN);
1207 	} else {
1208 		snd_soc_update_bits(codec, DA7213_DAC_L_CTRL,
1209 				    DA7213_MUTE_EN, 0);
1210 		snd_soc_update_bits(codec, DA7213_DAC_R_CTRL,
1211 				    DA7213_MUTE_EN, 0);
1212 	}
1213 
1214 	return 0;
1215 }
1216 
1217 #define DA7213_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
1218 			SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
1219 
1220 static int da7213_set_dai_sysclk(struct snd_soc_dai *codec_dai,
1221 				 int clk_id, unsigned int freq, int dir)
1222 {
1223 	struct snd_soc_codec *codec = codec_dai->codec;
1224 	struct da7213_priv *da7213 = snd_soc_codec_get_drvdata(codec);
1225 
1226 	switch (clk_id) {
1227 	case DA7213_CLKSRC_MCLK:
1228 		if ((freq == 32768) ||
1229 		    ((freq >= 5000000) && (freq <= 54000000))) {
1230 			da7213->mclk_rate = freq;
1231 			return 0;
1232 		} else {
1233 			dev_err(codec_dai->dev, "Unsupported MCLK value %d\n",
1234 				freq);
1235 			return -EINVAL;
1236 		}
1237 		break;
1238 	default:
1239 		dev_err(codec_dai->dev, "Unknown clock source %d\n", clk_id);
1240 		return -EINVAL;
1241 	}
1242 }
1243 
1244 /* Supported PLL input frequencies are 5MHz - 54MHz. */
1245 static int da7213_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id,
1246 			      int source, unsigned int fref, unsigned int fout)
1247 {
1248 	struct snd_soc_codec *codec = codec_dai->codec;
1249 	struct da7213_priv *da7213 = snd_soc_codec_get_drvdata(codec);
1250 
1251 	u8 pll_ctrl, indiv_bits, indiv;
1252 	u8 pll_frac_top, pll_frac_bot, pll_integer;
1253 	u32 freq_ref;
1254 	u64 frac_div;
1255 
1256 	/* Reset PLL configuration */
1257 	snd_soc_write(codec, DA7213_PLL_CTRL, 0);
1258 
1259 	pll_ctrl = 0;
1260 
1261 	/* Workout input divider based on MCLK rate */
1262 	if ((da7213->mclk_rate == 32768) && (source == DA7213_SYSCLK_PLL)) {
1263 		/* 32KHz PLL Mode */
1264 		indiv_bits = DA7213_PLL_INDIV_10_20_MHZ;
1265 		indiv = DA7213_PLL_INDIV_10_20_MHZ_VAL;
1266 		freq_ref = 3750000;
1267 		pll_ctrl |= DA7213_PLL_32K_MODE;
1268 	} else {
1269 		/* 5 - 54MHz MCLK */
1270 		if (da7213->mclk_rate < 5000000) {
1271 			goto pll_err;
1272 		} else if (da7213->mclk_rate <= 10000000) {
1273 			indiv_bits = DA7213_PLL_INDIV_5_10_MHZ;
1274 			indiv = DA7213_PLL_INDIV_5_10_MHZ_VAL;
1275 		} else if (da7213->mclk_rate <= 20000000) {
1276 			indiv_bits = DA7213_PLL_INDIV_10_20_MHZ;
1277 			indiv = DA7213_PLL_INDIV_10_20_MHZ_VAL;
1278 		} else if (da7213->mclk_rate <= 40000000) {
1279 			indiv_bits = DA7213_PLL_INDIV_20_40_MHZ;
1280 			indiv = DA7213_PLL_INDIV_20_40_MHZ_VAL;
1281 		} else if (da7213->mclk_rate <= 54000000) {
1282 			indiv_bits = DA7213_PLL_INDIV_40_54_MHZ;
1283 			indiv = DA7213_PLL_INDIV_40_54_MHZ_VAL;
1284 		} else {
1285 			goto pll_err;
1286 		}
1287 		freq_ref = (da7213->mclk_rate / indiv);
1288 	}
1289 
1290 	pll_ctrl |= indiv_bits;
1291 
1292 	/* PLL Bypass mode */
1293 	if (source == DA7213_SYSCLK_MCLK) {
1294 		snd_soc_write(codec, DA7213_PLL_CTRL, pll_ctrl);
1295 		return 0;
1296 	}
1297 
1298 	/*
1299 	 * If Codec is slave and SRM enabled,
1300 	 * freq_out is (98304000 + 90316800)/2 = 94310400
1301 	 */
1302 	if (!da7213->master && da7213->srm_en) {
1303 		fout = DA7213_PLL_FREQ_OUT_94310400;
1304 		pll_ctrl |= DA7213_PLL_SRM_EN;
1305 	}
1306 
1307 	/* Enable MCLK squarer if required */
1308 	if (da7213->mclk_squarer_en)
1309 		pll_ctrl |= DA7213_PLL_MCLK_SQR_EN;
1310 
1311 	/* Calculate dividers for PLL */
1312 	pll_integer = fout / freq_ref;
1313 	frac_div = (u64)(fout % freq_ref) * 8192ULL;
1314 	do_div(frac_div, freq_ref);
1315 	pll_frac_top = (frac_div >> DA7213_BYTE_SHIFT) & DA7213_BYTE_MASK;
1316 	pll_frac_bot = (frac_div) & DA7213_BYTE_MASK;
1317 
1318 	/* Write PLL dividers */
1319 	snd_soc_write(codec, DA7213_PLL_FRAC_TOP, pll_frac_top);
1320 	snd_soc_write(codec, DA7213_PLL_FRAC_BOT, pll_frac_bot);
1321 	snd_soc_write(codec, DA7213_PLL_INTEGER, pll_integer);
1322 
1323 	/* Enable PLL */
1324 	pll_ctrl |= DA7213_PLL_EN;
1325 	snd_soc_write(codec, DA7213_PLL_CTRL, pll_ctrl);
1326 
1327 	return 0;
1328 
1329 pll_err:
1330 	dev_err(codec_dai->dev, "Unsupported PLL input frequency %d\n",
1331 		da7213->mclk_rate);
1332 	return -EINVAL;
1333 }
1334 
1335 /* DAI operations */
1336 static const struct snd_soc_dai_ops da7213_dai_ops = {
1337 	.hw_params	= da7213_hw_params,
1338 	.set_fmt	= da7213_set_dai_fmt,
1339 	.set_sysclk	= da7213_set_dai_sysclk,
1340 	.set_pll	= da7213_set_dai_pll,
1341 	.digital_mute	= da7213_mute,
1342 };
1343 
1344 static struct snd_soc_dai_driver da7213_dai = {
1345 	.name = "da7213-hifi",
1346 	/* Playback Capabilities */
1347 	.playback = {
1348 		.stream_name = "Playback",
1349 		.channels_min = 1,
1350 		.channels_max = 2,
1351 		.rates = SNDRV_PCM_RATE_8000_96000,
1352 		.formats = DA7213_FORMATS,
1353 	},
1354 	/* Capture Capabilities */
1355 	.capture = {
1356 		.stream_name = "Capture",
1357 		.channels_min = 1,
1358 		.channels_max = 2,
1359 		.rates = SNDRV_PCM_RATE_8000_96000,
1360 		.formats = DA7213_FORMATS,
1361 	},
1362 	.ops = &da7213_dai_ops,
1363 	.symmetric_rates = 1,
1364 };
1365 
1366 static int da7213_set_bias_level(struct snd_soc_codec *codec,
1367 				 enum snd_soc_bias_level level)
1368 {
1369 	switch (level) {
1370 	case SND_SOC_BIAS_ON:
1371 	case SND_SOC_BIAS_PREPARE:
1372 		break;
1373 	case SND_SOC_BIAS_STANDBY:
1374 		if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_OFF) {
1375 			/* Enable VMID reference & master bias */
1376 			snd_soc_update_bits(codec, DA7213_REFERENCES,
1377 					    DA7213_VMID_EN | DA7213_BIAS_EN,
1378 					    DA7213_VMID_EN | DA7213_BIAS_EN);
1379 		}
1380 		break;
1381 	case SND_SOC_BIAS_OFF:
1382 		/* Disable VMID reference & master bias */
1383 		snd_soc_update_bits(codec, DA7213_REFERENCES,
1384 				    DA7213_VMID_EN | DA7213_BIAS_EN, 0);
1385 		break;
1386 	}
1387 	return 0;
1388 }
1389 
1390 static int da7213_probe(struct snd_soc_codec *codec)
1391 {
1392 	struct da7213_priv *da7213 = snd_soc_codec_get_drvdata(codec);
1393 	struct da7213_platform_data *pdata = da7213->pdata;
1394 
1395 	/* Default to using ALC auto offset calibration mode. */
1396 	snd_soc_update_bits(codec, DA7213_ALC_CTRL1,
1397 			    DA7213_ALC_CALIB_MODE_MAN, 0);
1398 	da7213->alc_calib_auto = true;
1399 
1400 	/* Default to using SRM for slave mode */
1401 	da7213->srm_en = true;
1402 
1403 	/* Enable all Gain Ramps */
1404 	snd_soc_update_bits(codec, DA7213_AUX_L_CTRL,
1405 			    DA7213_GAIN_RAMP_EN, DA7213_GAIN_RAMP_EN);
1406 	snd_soc_update_bits(codec, DA7213_AUX_R_CTRL,
1407 			    DA7213_GAIN_RAMP_EN, DA7213_GAIN_RAMP_EN);
1408 	snd_soc_update_bits(codec, DA7213_MIXIN_L_CTRL,
1409 			    DA7213_GAIN_RAMP_EN, DA7213_GAIN_RAMP_EN);
1410 	snd_soc_update_bits(codec, DA7213_MIXIN_R_CTRL,
1411 			    DA7213_GAIN_RAMP_EN, DA7213_GAIN_RAMP_EN);
1412 	snd_soc_update_bits(codec, DA7213_ADC_L_CTRL,
1413 			    DA7213_GAIN_RAMP_EN, DA7213_GAIN_RAMP_EN);
1414 	snd_soc_update_bits(codec, DA7213_ADC_R_CTRL,
1415 			    DA7213_GAIN_RAMP_EN, DA7213_GAIN_RAMP_EN);
1416 	snd_soc_update_bits(codec, DA7213_DAC_L_CTRL,
1417 			    DA7213_GAIN_RAMP_EN, DA7213_GAIN_RAMP_EN);
1418 	snd_soc_update_bits(codec, DA7213_DAC_R_CTRL,
1419 			    DA7213_GAIN_RAMP_EN, DA7213_GAIN_RAMP_EN);
1420 	snd_soc_update_bits(codec, DA7213_HP_L_CTRL,
1421 			    DA7213_GAIN_RAMP_EN, DA7213_GAIN_RAMP_EN);
1422 	snd_soc_update_bits(codec, DA7213_HP_R_CTRL,
1423 			    DA7213_GAIN_RAMP_EN, DA7213_GAIN_RAMP_EN);
1424 	snd_soc_update_bits(codec, DA7213_LINE_CTRL,
1425 			    DA7213_GAIN_RAMP_EN, DA7213_GAIN_RAMP_EN);
1426 
1427 	/*
1428 	 * There are two separate control bits for input and output mixers as
1429 	 * well as headphone and line outs.
1430 	 * One to enable corresponding amplifier and other to enable its
1431 	 * output. As amplifier bits are related to power control, they are
1432 	 * being managed by DAPM while other (non power related) bits are
1433 	 * enabled here
1434 	 */
1435 	snd_soc_update_bits(codec, DA7213_MIXIN_L_CTRL,
1436 			    DA7213_MIXIN_MIX_EN, DA7213_MIXIN_MIX_EN);
1437 	snd_soc_update_bits(codec, DA7213_MIXIN_R_CTRL,
1438 			    DA7213_MIXIN_MIX_EN, DA7213_MIXIN_MIX_EN);
1439 
1440 	snd_soc_update_bits(codec, DA7213_MIXOUT_L_CTRL,
1441 			    DA7213_MIXOUT_MIX_EN, DA7213_MIXOUT_MIX_EN);
1442 	snd_soc_update_bits(codec, DA7213_MIXOUT_R_CTRL,
1443 			    DA7213_MIXOUT_MIX_EN, DA7213_MIXOUT_MIX_EN);
1444 
1445 	snd_soc_update_bits(codec, DA7213_HP_L_CTRL,
1446 			    DA7213_HP_AMP_OE, DA7213_HP_AMP_OE);
1447 	snd_soc_update_bits(codec, DA7213_HP_R_CTRL,
1448 			    DA7213_HP_AMP_OE, DA7213_HP_AMP_OE);
1449 
1450 	snd_soc_update_bits(codec, DA7213_LINE_CTRL,
1451 			    DA7213_LINE_AMP_OE, DA7213_LINE_AMP_OE);
1452 
1453 	/* Set platform data values */
1454 	if (da7213->pdata) {
1455 		u8 micbias_lvl = 0, dmic_cfg = 0;
1456 
1457 		/* Set Mic Bias voltages */
1458 		switch (pdata->micbias1_lvl) {
1459 		case DA7213_MICBIAS_1_6V:
1460 		case DA7213_MICBIAS_2_2V:
1461 		case DA7213_MICBIAS_2_5V:
1462 		case DA7213_MICBIAS_3_0V:
1463 			micbias_lvl |= (pdata->micbias1_lvl <<
1464 					DA7213_MICBIAS1_LEVEL_SHIFT);
1465 			break;
1466 		}
1467 		switch (pdata->micbias2_lvl) {
1468 		case DA7213_MICBIAS_1_6V:
1469 		case DA7213_MICBIAS_2_2V:
1470 		case DA7213_MICBIAS_2_5V:
1471 		case DA7213_MICBIAS_3_0V:
1472 			micbias_lvl |= (pdata->micbias2_lvl <<
1473 					 DA7213_MICBIAS2_LEVEL_SHIFT);
1474 			break;
1475 		}
1476 		snd_soc_update_bits(codec, DA7213_MICBIAS_CTRL,
1477 				    DA7213_MICBIAS1_LEVEL_MASK |
1478 				    DA7213_MICBIAS2_LEVEL_MASK, micbias_lvl);
1479 
1480 		/* Set DMIC configuration */
1481 		switch (pdata->dmic_data_sel) {
1482 		case DA7213_DMIC_DATA_LFALL_RRISE:
1483 		case DA7213_DMIC_DATA_LRISE_RFALL:
1484 			dmic_cfg |= (pdata->dmic_data_sel <<
1485 				     DA7213_DMIC_DATA_SEL_SHIFT);
1486 			break;
1487 		}
1488 		switch (pdata->dmic_samplephase) {
1489 		case DA7213_DMIC_SAMPLE_ON_CLKEDGE:
1490 		case DA7213_DMIC_SAMPLE_BETWEEN_CLKEDGE:
1491 			dmic_cfg |= (pdata->dmic_samplephase <<
1492 				     DA7213_DMIC_SAMPLEPHASE_SHIFT);
1493 			break;
1494 		}
1495 		switch (pdata->dmic_clk_rate) {
1496 		case DA7213_DMIC_CLK_3_0MHZ:
1497 		case DA7213_DMIC_CLK_1_5MHZ:
1498 			dmic_cfg |= (pdata->dmic_clk_rate <<
1499 				     DA7213_DMIC_CLK_RATE_SHIFT);
1500 			break;
1501 		}
1502 		snd_soc_update_bits(codec, DA7213_MIC_CONFIG,
1503 				    DA7213_DMIC_DATA_SEL_MASK |
1504 				    DA7213_DMIC_SAMPLEPHASE_MASK |
1505 				    DA7213_DMIC_CLK_RATE_MASK, dmic_cfg);
1506 
1507 		/* Set MCLK squaring */
1508 		da7213->mclk_squarer_en = pdata->mclk_squaring;
1509 	}
1510 	return 0;
1511 }
1512 
1513 static struct snd_soc_codec_driver soc_codec_dev_da7213 = {
1514 	.probe			= da7213_probe,
1515 	.set_bias_level		= da7213_set_bias_level,
1516 
1517 	.controls		= da7213_snd_controls,
1518 	.num_controls		= ARRAY_SIZE(da7213_snd_controls),
1519 
1520 	.dapm_widgets		= da7213_dapm_widgets,
1521 	.num_dapm_widgets	= ARRAY_SIZE(da7213_dapm_widgets),
1522 	.dapm_routes		= da7213_audio_map,
1523 	.num_dapm_routes	= ARRAY_SIZE(da7213_audio_map),
1524 };
1525 
1526 static const struct regmap_config da7213_regmap_config = {
1527 	.reg_bits = 8,
1528 	.val_bits = 8,
1529 
1530 	.reg_defaults = da7213_reg_defaults,
1531 	.num_reg_defaults = ARRAY_SIZE(da7213_reg_defaults),
1532 	.volatile_reg = da7213_volatile_register,
1533 	.cache_type = REGCACHE_RBTREE,
1534 };
1535 
1536 static int da7213_i2c_probe(struct i2c_client *i2c,
1537 			    const struct i2c_device_id *id)
1538 {
1539 	struct da7213_priv *da7213;
1540 	struct da7213_platform_data *pdata = dev_get_platdata(&i2c->dev);
1541 	int ret;
1542 
1543 	da7213 = devm_kzalloc(&i2c->dev, sizeof(struct da7213_priv),
1544 			      GFP_KERNEL);
1545 	if (!da7213)
1546 		return -ENOMEM;
1547 
1548 	if (pdata)
1549 		da7213->pdata = pdata;
1550 
1551 	i2c_set_clientdata(i2c, da7213);
1552 
1553 	da7213->regmap = devm_regmap_init_i2c(i2c, &da7213_regmap_config);
1554 	if (IS_ERR(da7213->regmap)) {
1555 		ret = PTR_ERR(da7213->regmap);
1556 		dev_err(&i2c->dev, "regmap_init() failed: %d\n", ret);
1557 		return ret;
1558 	}
1559 
1560 	ret = snd_soc_register_codec(&i2c->dev,
1561 			&soc_codec_dev_da7213, &da7213_dai, 1);
1562 	if (ret < 0) {
1563 		dev_err(&i2c->dev, "Failed to register da7213 codec: %d\n",
1564 			ret);
1565 	}
1566 	return ret;
1567 }
1568 
1569 static int da7213_remove(struct i2c_client *client)
1570 {
1571 	snd_soc_unregister_codec(&client->dev);
1572 	return 0;
1573 }
1574 
1575 static const struct i2c_device_id da7213_i2c_id[] = {
1576 	{ "da7213", 0 },
1577 	{ }
1578 };
1579 MODULE_DEVICE_TABLE(i2c, da7213_i2c_id);
1580 
1581 /* I2C codec control layer */
1582 static struct i2c_driver da7213_i2c_driver = {
1583 	.driver = {
1584 		.name = "da7213",
1585 	},
1586 	.probe		= da7213_i2c_probe,
1587 	.remove		= da7213_remove,
1588 	.id_table	= da7213_i2c_id,
1589 };
1590 
1591 module_i2c_driver(da7213_i2c_driver);
1592 
1593 MODULE_DESCRIPTION("ASoC DA7213 Codec driver");
1594 MODULE_AUTHOR("Adam Thomson <Adam.Thomson.Opensource@diasemi.com>");
1595 MODULE_LICENSE("GPL");
1596