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