xref: /openbmc/linux/sound/soc/codecs/da7219.c (revision eb7ebfa3)
1 /*
2  * da7219.c - DA7219 ALSA SoC Codec Driver
3  *
4  * Copyright (c) 2015 Dialog Semiconductor
5  *
6  * Author: Adam Thomson <Adam.Thomson.Opensource@diasemi.com>
7  *
8  * This program is free software; you can redistribute  it and/or modify it
9  * under  the terms of  the GNU General  Public License as published by the
10  * Free Software Foundation;  either version 2 of the  License, or (at your
11  * option) any later version.
12  */
13 
14 #include <linux/acpi.h>
15 #include <linux/clk.h>
16 #include <linux/clkdev.h>
17 #include <linux/clk-provider.h>
18 #include <linux/i2c.h>
19 #include <linux/of_device.h>
20 #include <linux/property.h>
21 #include <linux/regmap.h>
22 #include <linux/slab.h>
23 #include <linux/pm.h>
24 #include <linux/module.h>
25 #include <linux/delay.h>
26 #include <linux/regulator/consumer.h>
27 #include <sound/pcm.h>
28 #include <sound/pcm_params.h>
29 #include <sound/soc.h>
30 #include <sound/soc-dapm.h>
31 #include <sound/initval.h>
32 #include <sound/tlv.h>
33 #include <asm/div64.h>
34 
35 #include <sound/da7219.h>
36 #include "da7219.h"
37 #include "da7219-aad.h"
38 
39 
40 /*
41  * TLVs and Enums
42  */
43 
44 /* Input TLVs */
45 static const DECLARE_TLV_DB_SCALE(da7219_mic_gain_tlv, -600, 600, 0);
46 static const DECLARE_TLV_DB_SCALE(da7219_mixin_gain_tlv, -450, 150, 0);
47 static const DECLARE_TLV_DB_SCALE(da7219_adc_dig_gain_tlv, -8325, 75, 0);
48 static const DECLARE_TLV_DB_SCALE(da7219_alc_threshold_tlv, -9450, 150, 0);
49 static const DECLARE_TLV_DB_SCALE(da7219_alc_gain_tlv, 0, 600, 0);
50 static const DECLARE_TLV_DB_SCALE(da7219_alc_ana_gain_tlv, 0, 600, 0);
51 static const DECLARE_TLV_DB_SCALE(da7219_sidetone_gain_tlv, -4200, 300, 0);
52 static const DECLARE_TLV_DB_SCALE(da7219_tonegen_gain_tlv, -4500, 300, 0);
53 
54 /* Output TLVs */
55 static const DECLARE_TLV_DB_SCALE(da7219_dac_eq_band_tlv, -1050, 150, 0);
56 
57 static const DECLARE_TLV_DB_RANGE(da7219_dac_dig_gain_tlv,
58 	0x0, 0x07, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE, 0, 1),
59 	/* -77.25dB to 12dB */
60 	0x08, 0x7f, TLV_DB_SCALE_ITEM(-7725, 75, 0)
61 );
62 
63 static const DECLARE_TLV_DB_SCALE(da7219_dac_ng_threshold_tlv, -10200, 600, 0);
64 static const DECLARE_TLV_DB_SCALE(da7219_hp_gain_tlv, -5700, 100, 0);
65 
66 /* Input Enums */
67 static const char * const da7219_alc_attack_rate_txt[] = {
68 	"7.33/fs", "14.66/fs", "29.32/fs", "58.64/fs", "117.3/fs", "234.6/fs",
69 	"469.1/fs", "938.2/fs", "1876/fs", "3753/fs", "7506/fs", "15012/fs",
70 	"30024/fs"
71 };
72 
73 static const struct soc_enum da7219_alc_attack_rate =
74 	SOC_ENUM_SINGLE(DA7219_ALC_CTRL2, DA7219_ALC_ATTACK_SHIFT,
75 			DA7219_ALC_ATTACK_MAX, da7219_alc_attack_rate_txt);
76 
77 static const char * const da7219_alc_release_rate_txt[] = {
78 	"28.66/fs", "57.33/fs", "114.6/fs", "229.3/fs", "458.6/fs", "917.1/fs",
79 	"1834/fs", "3668/fs", "7337/fs", "14674/fs", "29348/fs"
80 };
81 
82 static const struct soc_enum da7219_alc_release_rate =
83 	SOC_ENUM_SINGLE(DA7219_ALC_CTRL2, DA7219_ALC_RELEASE_SHIFT,
84 			DA7219_ALC_RELEASE_MAX, da7219_alc_release_rate_txt);
85 
86 static const char * const da7219_alc_hold_time_txt[] = {
87 	"62/fs", "124/fs", "248/fs", "496/fs", "992/fs", "1984/fs", "3968/fs",
88 	"7936/fs", "15872/fs", "31744/fs", "63488/fs", "126976/fs",
89 	"253952/fs", "507904/fs", "1015808/fs", "2031616/fs"
90 };
91 
92 static const struct soc_enum da7219_alc_hold_time =
93 	SOC_ENUM_SINGLE(DA7219_ALC_CTRL3, DA7219_ALC_HOLD_SHIFT,
94 			DA7219_ALC_HOLD_MAX, da7219_alc_hold_time_txt);
95 
96 static const char * const da7219_alc_env_rate_txt[] = {
97 	"1/4", "1/16", "1/256", "1/65536"
98 };
99 
100 static const struct soc_enum da7219_alc_env_attack_rate =
101 	SOC_ENUM_SINGLE(DA7219_ALC_CTRL3, DA7219_ALC_INTEG_ATTACK_SHIFT,
102 			DA7219_ALC_INTEG_MAX, da7219_alc_env_rate_txt);
103 
104 static const struct soc_enum da7219_alc_env_release_rate =
105 	SOC_ENUM_SINGLE(DA7219_ALC_CTRL3, DA7219_ALC_INTEG_RELEASE_SHIFT,
106 			DA7219_ALC_INTEG_MAX, da7219_alc_env_rate_txt);
107 
108 static const char * const da7219_alc_anticlip_step_txt[] = {
109 	"0.034dB/fs", "0.068dB/fs", "0.136dB/fs", "0.272dB/fs"
110 };
111 
112 static const struct soc_enum da7219_alc_anticlip_step =
113 	SOC_ENUM_SINGLE(DA7219_ALC_ANTICLIP_CTRL,
114 			DA7219_ALC_ANTICLIP_STEP_SHIFT,
115 			DA7219_ALC_ANTICLIP_STEP_MAX,
116 			da7219_alc_anticlip_step_txt);
117 
118 /* Input/Output Enums */
119 static const char * const da7219_gain_ramp_rate_txt[] = {
120 	"Nominal Rate * 8", "Nominal Rate", "Nominal Rate / 8",
121 	"Nominal Rate / 16"
122 };
123 
124 static const struct soc_enum da7219_gain_ramp_rate =
125 	SOC_ENUM_SINGLE(DA7219_GAIN_RAMP_CTRL, DA7219_GAIN_RAMP_RATE_SHIFT,
126 			DA7219_GAIN_RAMP_RATE_MAX, da7219_gain_ramp_rate_txt);
127 
128 static const char * const da7219_hpf_mode_txt[] = {
129 	"Disabled", "Audio", "Voice"
130 };
131 
132 static const unsigned int da7219_hpf_mode_val[] = {
133 	DA7219_HPF_DISABLED, DA7219_HPF_AUDIO_EN, DA7219_HPF_VOICE_EN,
134 };
135 
136 static const struct soc_enum da7219_adc_hpf_mode =
137 	SOC_VALUE_ENUM_SINGLE(DA7219_ADC_FILTERS1, DA7219_HPF_MODE_SHIFT,
138 			      DA7219_HPF_MODE_MASK, DA7219_HPF_MODE_MAX,
139 			      da7219_hpf_mode_txt, da7219_hpf_mode_val);
140 
141 static const struct soc_enum da7219_dac_hpf_mode =
142 	SOC_VALUE_ENUM_SINGLE(DA7219_DAC_FILTERS1, DA7219_HPF_MODE_SHIFT,
143 			      DA7219_HPF_MODE_MASK, DA7219_HPF_MODE_MAX,
144 			      da7219_hpf_mode_txt, da7219_hpf_mode_val);
145 
146 static const char * const da7219_audio_hpf_corner_txt[] = {
147 	"2Hz", "4Hz", "8Hz", "16Hz"
148 };
149 
150 static const struct soc_enum da7219_adc_audio_hpf_corner =
151 	SOC_ENUM_SINGLE(DA7219_ADC_FILTERS1,
152 			DA7219_ADC_AUDIO_HPF_CORNER_SHIFT,
153 			DA7219_AUDIO_HPF_CORNER_MAX,
154 			da7219_audio_hpf_corner_txt);
155 
156 static const struct soc_enum da7219_dac_audio_hpf_corner =
157 	SOC_ENUM_SINGLE(DA7219_DAC_FILTERS1,
158 			DA7219_DAC_AUDIO_HPF_CORNER_SHIFT,
159 			DA7219_AUDIO_HPF_CORNER_MAX,
160 			da7219_audio_hpf_corner_txt);
161 
162 static const char * const da7219_voice_hpf_corner_txt[] = {
163 	"2.5Hz", "25Hz", "50Hz", "100Hz", "150Hz", "200Hz", "300Hz", "400Hz"
164 };
165 
166 static const struct soc_enum da7219_adc_voice_hpf_corner =
167 	SOC_ENUM_SINGLE(DA7219_ADC_FILTERS1,
168 			DA7219_ADC_VOICE_HPF_CORNER_SHIFT,
169 			DA7219_VOICE_HPF_CORNER_MAX,
170 			da7219_voice_hpf_corner_txt);
171 
172 static const struct soc_enum da7219_dac_voice_hpf_corner =
173 	SOC_ENUM_SINGLE(DA7219_DAC_FILTERS1,
174 			DA7219_DAC_VOICE_HPF_CORNER_SHIFT,
175 			DA7219_VOICE_HPF_CORNER_MAX,
176 			da7219_voice_hpf_corner_txt);
177 
178 static const char * const da7219_tonegen_dtmf_key_txt[] = {
179 	"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D",
180 	"*", "#"
181 };
182 
183 static const struct soc_enum da7219_tonegen_dtmf_key =
184 	SOC_ENUM_SINGLE(DA7219_TONE_GEN_CFG1, DA7219_DTMF_REG_SHIFT,
185 			DA7219_DTMF_REG_MAX, da7219_tonegen_dtmf_key_txt);
186 
187 static const char * const da7219_tonegen_swg_sel_txt[] = {
188 	"Sum", "SWG1", "SWG2", "SWG1_1-Cos"
189 };
190 
191 static const struct soc_enum da7219_tonegen_swg_sel =
192 	SOC_ENUM_SINGLE(DA7219_TONE_GEN_CFG2, DA7219_SWG_SEL_SHIFT,
193 			DA7219_SWG_SEL_MAX, da7219_tonegen_swg_sel_txt);
194 
195 /* Output Enums */
196 static const char * const da7219_dac_softmute_rate_txt[] = {
197 	"1 Sample", "2 Samples", "4 Samples", "8 Samples", "16 Samples",
198 	"32 Samples", "64 Samples"
199 };
200 
201 static const struct soc_enum da7219_dac_softmute_rate =
202 	SOC_ENUM_SINGLE(DA7219_DAC_FILTERS5, DA7219_DAC_SOFTMUTE_RATE_SHIFT,
203 			DA7219_DAC_SOFTMUTE_RATE_MAX,
204 			da7219_dac_softmute_rate_txt);
205 
206 static const char * const da7219_dac_ng_setup_time_txt[] = {
207 	"256 Samples", "512 Samples", "1024 Samples", "2048 Samples"
208 };
209 
210 static const struct soc_enum da7219_dac_ng_setup_time =
211 	SOC_ENUM_SINGLE(DA7219_DAC_NG_SETUP_TIME,
212 			DA7219_DAC_NG_SETUP_TIME_SHIFT,
213 			DA7219_DAC_NG_SETUP_TIME_MAX,
214 			da7219_dac_ng_setup_time_txt);
215 
216 static const char * const da7219_dac_ng_rampup_txt[] = {
217 	"0.22ms/dB", "0.0138ms/dB"
218 };
219 
220 static const struct soc_enum da7219_dac_ng_rampup_rate =
221 	SOC_ENUM_SINGLE(DA7219_DAC_NG_SETUP_TIME,
222 			DA7219_DAC_NG_RAMPUP_RATE_SHIFT,
223 			DA7219_DAC_NG_RAMP_RATE_MAX,
224 			da7219_dac_ng_rampup_txt);
225 
226 static const char * const da7219_dac_ng_rampdown_txt[] = {
227 	"0.88ms/dB", "14.08ms/dB"
228 };
229 
230 static const struct soc_enum da7219_dac_ng_rampdown_rate =
231 	SOC_ENUM_SINGLE(DA7219_DAC_NG_SETUP_TIME,
232 			DA7219_DAC_NG_RAMPDN_RATE_SHIFT,
233 			DA7219_DAC_NG_RAMP_RATE_MAX,
234 			da7219_dac_ng_rampdown_txt);
235 
236 
237 static const char * const da7219_cp_track_mode_txt[] = {
238 	"Largest Volume", "DAC Volume", "Signal Magnitude"
239 };
240 
241 static const unsigned int da7219_cp_track_mode_val[] = {
242 	DA7219_CP_MCHANGE_LARGEST_VOL, DA7219_CP_MCHANGE_DAC_VOL,
243 	DA7219_CP_MCHANGE_SIG_MAG
244 };
245 
246 static const struct soc_enum da7219_cp_track_mode =
247 	SOC_VALUE_ENUM_SINGLE(DA7219_CP_CTRL, DA7219_CP_MCHANGE_SHIFT,
248 			      DA7219_CP_MCHANGE_REL_MASK, DA7219_CP_MCHANGE_MAX,
249 			      da7219_cp_track_mode_txt,
250 			      da7219_cp_track_mode_val);
251 
252 
253 /*
254  * Control Functions
255  */
256 
257 /* Locked Kcontrol calls */
258 static int da7219_volsw_locked_get(struct snd_kcontrol *kcontrol,
259 				   struct snd_ctl_elem_value *ucontrol)
260 {
261 	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
262 	struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
263 	int ret;
264 
265 	mutex_lock(&da7219->ctrl_lock);
266 	ret = snd_soc_get_volsw(kcontrol, ucontrol);
267 	mutex_unlock(&da7219->ctrl_lock);
268 
269 	return ret;
270 }
271 
272 static int da7219_volsw_locked_put(struct snd_kcontrol *kcontrol,
273 				   struct snd_ctl_elem_value *ucontrol)
274 {
275 	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
276 	struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
277 	int ret;
278 
279 	mutex_lock(&da7219->ctrl_lock);
280 	ret = snd_soc_put_volsw(kcontrol, ucontrol);
281 	mutex_unlock(&da7219->ctrl_lock);
282 
283 	return ret;
284 }
285 
286 static int da7219_enum_locked_get(struct snd_kcontrol *kcontrol,
287 				struct snd_ctl_elem_value *ucontrol)
288 {
289 	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
290 	struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
291 	int ret;
292 
293 	mutex_lock(&da7219->ctrl_lock);
294 	ret = snd_soc_get_enum_double(kcontrol, ucontrol);
295 	mutex_unlock(&da7219->ctrl_lock);
296 
297 	return ret;
298 }
299 
300 static int da7219_enum_locked_put(struct snd_kcontrol *kcontrol,
301 				struct snd_ctl_elem_value *ucontrol)
302 {
303 	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
304 	struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
305 	int ret;
306 
307 	mutex_lock(&da7219->ctrl_lock);
308 	ret = snd_soc_put_enum_double(kcontrol, ucontrol);
309 	mutex_unlock(&da7219->ctrl_lock);
310 
311 	return ret;
312 }
313 
314 /* ALC */
315 static void da7219_alc_calib(struct snd_soc_component *component)
316 {
317 	u8 mic_ctrl, mixin_ctrl, adc_ctrl, calib_ctrl;
318 
319 	/* Save current state of mic control register */
320 	mic_ctrl = snd_soc_component_read32(component, DA7219_MIC_1_CTRL);
321 
322 	/* Save current state of input mixer control register */
323 	mixin_ctrl = snd_soc_component_read32(component, DA7219_MIXIN_L_CTRL);
324 
325 	/* Save current state of input ADC control register */
326 	adc_ctrl = snd_soc_component_read32(component, DA7219_ADC_L_CTRL);
327 
328 	/* Enable then Mute MIC PGAs */
329 	snd_soc_component_update_bits(component, DA7219_MIC_1_CTRL, DA7219_MIC_1_AMP_EN_MASK,
330 			    DA7219_MIC_1_AMP_EN_MASK);
331 	snd_soc_component_update_bits(component, DA7219_MIC_1_CTRL,
332 			    DA7219_MIC_1_AMP_MUTE_EN_MASK,
333 			    DA7219_MIC_1_AMP_MUTE_EN_MASK);
334 
335 	/* Enable input mixers unmuted */
336 	snd_soc_component_update_bits(component, DA7219_MIXIN_L_CTRL,
337 			    DA7219_MIXIN_L_AMP_EN_MASK |
338 			    DA7219_MIXIN_L_AMP_MUTE_EN_MASK,
339 			    DA7219_MIXIN_L_AMP_EN_MASK);
340 
341 	/* Enable input filters unmuted */
342 	snd_soc_component_update_bits(component, DA7219_ADC_L_CTRL,
343 			    DA7219_ADC_L_MUTE_EN_MASK | DA7219_ADC_L_EN_MASK,
344 			    DA7219_ADC_L_EN_MASK);
345 
346 	/* Perform auto calibration */
347 	snd_soc_component_update_bits(component, DA7219_ALC_CTRL1,
348 			    DA7219_ALC_AUTO_CALIB_EN_MASK,
349 			    DA7219_ALC_AUTO_CALIB_EN_MASK);
350 	do {
351 		calib_ctrl = snd_soc_component_read32(component, DA7219_ALC_CTRL1);
352 	} while (calib_ctrl & DA7219_ALC_AUTO_CALIB_EN_MASK);
353 
354 	/* If auto calibration fails, disable DC offset, hybrid ALC */
355 	if (calib_ctrl & DA7219_ALC_CALIB_OVERFLOW_MASK) {
356 		dev_warn(component->dev,
357 			 "ALC auto calibration failed with overflow\n");
358 		snd_soc_component_update_bits(component, DA7219_ALC_CTRL1,
359 				    DA7219_ALC_OFFSET_EN_MASK |
360 				    DA7219_ALC_SYNC_MODE_MASK, 0);
361 	} else {
362 		/* Enable DC offset cancellation, hybrid mode */
363 		snd_soc_component_update_bits(component, DA7219_ALC_CTRL1,
364 				    DA7219_ALC_OFFSET_EN_MASK |
365 				    DA7219_ALC_SYNC_MODE_MASK,
366 				    DA7219_ALC_OFFSET_EN_MASK |
367 				    DA7219_ALC_SYNC_MODE_MASK);
368 	}
369 
370 	/* Restore input filter control register to original state */
371 	snd_soc_component_write(component, DA7219_ADC_L_CTRL, adc_ctrl);
372 
373 	/* Restore input mixer control registers to original state */
374 	snd_soc_component_write(component, DA7219_MIXIN_L_CTRL, mixin_ctrl);
375 
376 	/* Restore MIC control registers to original states */
377 	snd_soc_component_write(component, DA7219_MIC_1_CTRL, mic_ctrl);
378 }
379 
380 static int da7219_mixin_gain_put(struct snd_kcontrol *kcontrol,
381 				 struct snd_ctl_elem_value *ucontrol)
382 {
383 	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
384 	struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
385 	int ret;
386 
387 	ret = snd_soc_put_volsw(kcontrol, ucontrol);
388 
389 	/*
390 	 * If ALC in operation and value of control has been updated,
391 	 * make sure calibrated offsets are updated.
392 	 */
393 	if ((ret == 1) && (da7219->alc_en))
394 		da7219_alc_calib(component);
395 
396 	return ret;
397 }
398 
399 static int da7219_alc_sw_put(struct snd_kcontrol *kcontrol,
400 			     struct snd_ctl_elem_value *ucontrol)
401 {
402 	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
403 	struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
404 
405 
406 	/* Force ALC offset calibration if enabling ALC */
407 	if ((ucontrol->value.integer.value[0]) && (!da7219->alc_en)) {
408 		da7219_alc_calib(component);
409 		da7219->alc_en = true;
410 	} else {
411 		da7219->alc_en = false;
412 	}
413 
414 	return snd_soc_put_volsw(kcontrol, ucontrol);
415 }
416 
417 /* ToneGen */
418 static int da7219_tonegen_freq_get(struct snd_kcontrol *kcontrol,
419 				   struct snd_ctl_elem_value *ucontrol)
420 {
421 	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
422 	struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
423 	struct soc_mixer_control *mixer_ctrl =
424 		(struct soc_mixer_control *) kcontrol->private_value;
425 	unsigned int reg = mixer_ctrl->reg;
426 	u16 val;
427 	int ret;
428 
429 	mutex_lock(&da7219->ctrl_lock);
430 	ret = regmap_raw_read(da7219->regmap, reg, &val, sizeof(val));
431 	mutex_unlock(&da7219->ctrl_lock);
432 
433 	if (ret)
434 		return ret;
435 
436 	/*
437 	 * Frequency value spans two 8-bit registers, lower then upper byte.
438 	 * Therefore we need to convert to host endianness here.
439 	 */
440 	ucontrol->value.integer.value[0] = le16_to_cpu(val);
441 
442 	return 0;
443 }
444 
445 static int da7219_tonegen_freq_put(struct snd_kcontrol *kcontrol,
446 				   struct snd_ctl_elem_value *ucontrol)
447 {
448 	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
449 	struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
450 	struct soc_mixer_control *mixer_ctrl =
451 		(struct soc_mixer_control *) kcontrol->private_value;
452 	unsigned int reg = mixer_ctrl->reg;
453 	u16 val;
454 	int ret;
455 
456 	/*
457 	 * Frequency value spans two 8-bit registers, lower then upper byte.
458 	 * Therefore we need to convert to little endian here to align with
459 	 * HW registers.
460 	 */
461 	val = cpu_to_le16(ucontrol->value.integer.value[0]);
462 
463 	mutex_lock(&da7219->ctrl_lock);
464 	ret = regmap_raw_write(da7219->regmap, reg, &val, sizeof(val));
465 	mutex_unlock(&da7219->ctrl_lock);
466 
467 	return ret;
468 }
469 
470 
471 /*
472  * KControls
473  */
474 
475 static const struct snd_kcontrol_new da7219_snd_controls[] = {
476 	/* Mics */
477 	SOC_SINGLE_TLV("Mic Volume", DA7219_MIC_1_GAIN,
478 		       DA7219_MIC_1_AMP_GAIN_SHIFT, DA7219_MIC_1_AMP_GAIN_MAX,
479 		       DA7219_NO_INVERT, da7219_mic_gain_tlv),
480 	SOC_SINGLE("Mic Switch", DA7219_MIC_1_CTRL,
481 		   DA7219_MIC_1_AMP_MUTE_EN_SHIFT, DA7219_SWITCH_EN_MAX,
482 		   DA7219_INVERT),
483 
484 	/* Mixer Input */
485 	SOC_SINGLE_EXT_TLV("Mixin Volume", DA7219_MIXIN_L_GAIN,
486 			   DA7219_MIXIN_L_AMP_GAIN_SHIFT,
487 			   DA7219_MIXIN_L_AMP_GAIN_MAX, DA7219_NO_INVERT,
488 			   snd_soc_get_volsw, da7219_mixin_gain_put,
489 			   da7219_mixin_gain_tlv),
490 	SOC_SINGLE("Mixin Switch", DA7219_MIXIN_L_CTRL,
491 		   DA7219_MIXIN_L_AMP_MUTE_EN_SHIFT, DA7219_SWITCH_EN_MAX,
492 		   DA7219_INVERT),
493 	SOC_SINGLE("Mixin Gain Ramp Switch", DA7219_MIXIN_L_CTRL,
494 		   DA7219_MIXIN_L_AMP_RAMP_EN_SHIFT, DA7219_SWITCH_EN_MAX,
495 		   DA7219_NO_INVERT),
496 	SOC_SINGLE("Mixin ZC Gain Switch", DA7219_MIXIN_L_CTRL,
497 		   DA7219_MIXIN_L_AMP_ZC_EN_SHIFT, DA7219_SWITCH_EN_MAX,
498 		   DA7219_NO_INVERT),
499 
500 	/* ADC */
501 	SOC_SINGLE_TLV("Capture Digital Volume", DA7219_ADC_L_GAIN,
502 		       DA7219_ADC_L_DIGITAL_GAIN_SHIFT,
503 		       DA7219_ADC_L_DIGITAL_GAIN_MAX, DA7219_NO_INVERT,
504 		       da7219_adc_dig_gain_tlv),
505 	SOC_SINGLE("Capture Digital Switch", DA7219_ADC_L_CTRL,
506 		   DA7219_ADC_L_MUTE_EN_SHIFT, DA7219_SWITCH_EN_MAX,
507 		   DA7219_INVERT),
508 	SOC_SINGLE("Capture Digital Gain Ramp Switch", DA7219_ADC_L_CTRL,
509 		   DA7219_ADC_L_RAMP_EN_SHIFT, DA7219_SWITCH_EN_MAX,
510 		   DA7219_NO_INVERT),
511 
512 	/* ALC */
513 	SOC_ENUM("ALC Attack Rate", da7219_alc_attack_rate),
514 	SOC_ENUM("ALC Release Rate", da7219_alc_release_rate),
515 	SOC_ENUM("ALC Hold Time", da7219_alc_hold_time),
516 	SOC_ENUM("ALC Envelope Attack Rate", da7219_alc_env_attack_rate),
517 	SOC_ENUM("ALC Envelope Release Rate", da7219_alc_env_release_rate),
518 	SOC_SINGLE_TLV("ALC Noise Threshold", DA7219_ALC_NOISE,
519 		       DA7219_ALC_NOISE_SHIFT, DA7219_ALC_THRESHOLD_MAX,
520 		       DA7219_INVERT, da7219_alc_threshold_tlv),
521 	SOC_SINGLE_TLV("ALC Min Threshold", DA7219_ALC_TARGET_MIN,
522 		       DA7219_ALC_THRESHOLD_MIN_SHIFT, DA7219_ALC_THRESHOLD_MAX,
523 		       DA7219_INVERT, da7219_alc_threshold_tlv),
524 	SOC_SINGLE_TLV("ALC Max Threshold", DA7219_ALC_TARGET_MAX,
525 		       DA7219_ALC_THRESHOLD_MAX_SHIFT, DA7219_ALC_THRESHOLD_MAX,
526 		       DA7219_INVERT, da7219_alc_threshold_tlv),
527 	SOC_SINGLE_TLV("ALC Max Attenuation", DA7219_ALC_GAIN_LIMITS,
528 		       DA7219_ALC_ATTEN_MAX_SHIFT, DA7219_ALC_ATTEN_GAIN_MAX,
529 		       DA7219_NO_INVERT, da7219_alc_gain_tlv),
530 	SOC_SINGLE_TLV("ALC Max Volume", DA7219_ALC_GAIN_LIMITS,
531 		       DA7219_ALC_GAIN_MAX_SHIFT, DA7219_ALC_ATTEN_GAIN_MAX,
532 		       DA7219_NO_INVERT, da7219_alc_gain_tlv),
533 	SOC_SINGLE_RANGE_TLV("ALC Min Analog Volume", DA7219_ALC_ANA_GAIN_LIMITS,
534 			     DA7219_ALC_ANA_GAIN_MIN_SHIFT,
535 			     DA7219_ALC_ANA_GAIN_MIN, DA7219_ALC_ANA_GAIN_MAX,
536 			     DA7219_NO_INVERT, da7219_alc_ana_gain_tlv),
537 	SOC_SINGLE_RANGE_TLV("ALC Max Analog Volume", DA7219_ALC_ANA_GAIN_LIMITS,
538 			     DA7219_ALC_ANA_GAIN_MAX_SHIFT,
539 			     DA7219_ALC_ANA_GAIN_MIN, DA7219_ALC_ANA_GAIN_MAX,
540 			     DA7219_NO_INVERT, da7219_alc_ana_gain_tlv),
541 	SOC_ENUM("ALC Anticlip Step", da7219_alc_anticlip_step),
542 	SOC_SINGLE("ALC Anticlip Switch", DA7219_ALC_ANTICLIP_CTRL,
543 		   DA7219_ALC_ANTIPCLIP_EN_SHIFT, DA7219_SWITCH_EN_MAX,
544 		   DA7219_NO_INVERT),
545 	SOC_SINGLE_EXT("ALC Switch", DA7219_ALC_CTRL1, DA7219_ALC_EN_SHIFT,
546 		       DA7219_SWITCH_EN_MAX, DA7219_NO_INVERT,
547 		       snd_soc_get_volsw, da7219_alc_sw_put),
548 
549 	/* Input High-Pass Filters */
550 	SOC_ENUM("ADC HPF Mode", da7219_adc_hpf_mode),
551 	SOC_ENUM("ADC HPF Corner Audio", da7219_adc_audio_hpf_corner),
552 	SOC_ENUM("ADC HPF Corner Voice", da7219_adc_voice_hpf_corner),
553 
554 	/* Sidetone Filter */
555 	SOC_SINGLE_TLV("Sidetone Volume", DA7219_SIDETONE_GAIN,
556 		       DA7219_SIDETONE_GAIN_SHIFT, DA7219_SIDETONE_GAIN_MAX,
557 		       DA7219_NO_INVERT, da7219_sidetone_gain_tlv),
558 	SOC_SINGLE("Sidetone Switch", DA7219_SIDETONE_CTRL,
559 		   DA7219_SIDETONE_MUTE_EN_SHIFT, DA7219_SWITCH_EN_MAX,
560 		   DA7219_INVERT),
561 
562 	/* Tone Generator */
563 	SOC_SINGLE_EXT_TLV("ToneGen Volume", DA7219_TONE_GEN_CFG2,
564 			   DA7219_TONE_GEN_GAIN_SHIFT, DA7219_TONE_GEN_GAIN_MAX,
565 			   DA7219_NO_INVERT, da7219_volsw_locked_get,
566 			   da7219_volsw_locked_put, da7219_tonegen_gain_tlv),
567 	SOC_ENUM_EXT("ToneGen DTMF Key", da7219_tonegen_dtmf_key,
568 		     da7219_enum_locked_get, da7219_enum_locked_put),
569 	SOC_SINGLE_EXT("ToneGen DTMF Switch", DA7219_TONE_GEN_CFG1,
570 		       DA7219_DTMF_EN_SHIFT, DA7219_SWITCH_EN_MAX,
571 		       DA7219_NO_INVERT, da7219_volsw_locked_get,
572 		       da7219_volsw_locked_put),
573 	SOC_ENUM_EXT("ToneGen Sinewave Gen Type", da7219_tonegen_swg_sel,
574 		     da7219_enum_locked_get, da7219_enum_locked_put),
575 	SOC_SINGLE_EXT("ToneGen Sinewave1 Freq", DA7219_TONE_GEN_FREQ1_L,
576 		       DA7219_FREQ1_L_SHIFT, DA7219_FREQ_MAX, DA7219_NO_INVERT,
577 		       da7219_tonegen_freq_get, da7219_tonegen_freq_put),
578 	SOC_SINGLE_EXT("ToneGen Sinewave2 Freq", DA7219_TONE_GEN_FREQ2_L,
579 		       DA7219_FREQ2_L_SHIFT, DA7219_FREQ_MAX, DA7219_NO_INVERT,
580 		       da7219_tonegen_freq_get, da7219_tonegen_freq_put),
581 	SOC_SINGLE_EXT("ToneGen On Time", DA7219_TONE_GEN_ON_PER,
582 		       DA7219_BEEP_ON_PER_SHIFT, DA7219_BEEP_ON_OFF_MAX,
583 		       DA7219_NO_INVERT, da7219_volsw_locked_get,
584 		       da7219_volsw_locked_put),
585 	SOC_SINGLE("ToneGen Off Time", DA7219_TONE_GEN_OFF_PER,
586 		   DA7219_BEEP_OFF_PER_SHIFT, DA7219_BEEP_ON_OFF_MAX,
587 		   DA7219_NO_INVERT),
588 
589 	/* Gain ramping */
590 	SOC_ENUM("Gain Ramp Rate", da7219_gain_ramp_rate),
591 
592 	/* DAC High-Pass Filter */
593 	SOC_ENUM_EXT("DAC HPF Mode", da7219_dac_hpf_mode,
594 		     da7219_enum_locked_get, da7219_enum_locked_put),
595 	SOC_ENUM("DAC HPF Corner Audio", da7219_dac_audio_hpf_corner),
596 	SOC_ENUM("DAC HPF Corner Voice", da7219_dac_voice_hpf_corner),
597 
598 	/* DAC 5-Band Equaliser */
599 	SOC_SINGLE_TLV("DAC EQ Band1 Volume", DA7219_DAC_FILTERS2,
600 		       DA7219_DAC_EQ_BAND1_SHIFT, DA7219_DAC_EQ_BAND_MAX,
601 		       DA7219_NO_INVERT, da7219_dac_eq_band_tlv),
602 	SOC_SINGLE_TLV("DAC EQ Band2 Volume", DA7219_DAC_FILTERS2,
603 		       DA7219_DAC_EQ_BAND2_SHIFT, DA7219_DAC_EQ_BAND_MAX,
604 		       DA7219_NO_INVERT, da7219_dac_eq_band_tlv),
605 	SOC_SINGLE_TLV("DAC EQ Band3 Volume", DA7219_DAC_FILTERS3,
606 		       DA7219_DAC_EQ_BAND3_SHIFT, DA7219_DAC_EQ_BAND_MAX,
607 		       DA7219_NO_INVERT, da7219_dac_eq_band_tlv),
608 	SOC_SINGLE_TLV("DAC EQ Band4 Volume", DA7219_DAC_FILTERS3,
609 		       DA7219_DAC_EQ_BAND4_SHIFT, DA7219_DAC_EQ_BAND_MAX,
610 		       DA7219_NO_INVERT, da7219_dac_eq_band_tlv),
611 	SOC_SINGLE_TLV("DAC EQ Band5 Volume", DA7219_DAC_FILTERS4,
612 		       DA7219_DAC_EQ_BAND5_SHIFT, DA7219_DAC_EQ_BAND_MAX,
613 		       DA7219_NO_INVERT, da7219_dac_eq_band_tlv),
614 	SOC_SINGLE_EXT("DAC EQ Switch", DA7219_DAC_FILTERS4,
615 		       DA7219_DAC_EQ_EN_SHIFT, DA7219_SWITCH_EN_MAX,
616 		       DA7219_NO_INVERT, da7219_volsw_locked_get,
617 		       da7219_volsw_locked_put),
618 
619 	/* DAC Softmute */
620 	SOC_ENUM("DAC Soft Mute Rate", da7219_dac_softmute_rate),
621 	SOC_SINGLE_EXT("DAC Soft Mute Switch", DA7219_DAC_FILTERS5,
622 		       DA7219_DAC_SOFTMUTE_EN_SHIFT, DA7219_SWITCH_EN_MAX,
623 		       DA7219_NO_INVERT, da7219_volsw_locked_get,
624 		       da7219_volsw_locked_put),
625 
626 	/* DAC Noise Gate */
627 	SOC_ENUM("DAC NG Setup Time", da7219_dac_ng_setup_time),
628 	SOC_ENUM("DAC NG Rampup Rate", da7219_dac_ng_rampup_rate),
629 	SOC_ENUM("DAC NG Rampdown Rate", da7219_dac_ng_rampdown_rate),
630 	SOC_SINGLE_TLV("DAC NG Off Threshold", DA7219_DAC_NG_OFF_THRESH,
631 		       DA7219_DAC_NG_OFF_THRESHOLD_SHIFT,
632 		       DA7219_DAC_NG_THRESHOLD_MAX, DA7219_NO_INVERT,
633 		       da7219_dac_ng_threshold_tlv),
634 	SOC_SINGLE_TLV("DAC NG On Threshold", DA7219_DAC_NG_ON_THRESH,
635 		       DA7219_DAC_NG_ON_THRESHOLD_SHIFT,
636 		       DA7219_DAC_NG_THRESHOLD_MAX, DA7219_NO_INVERT,
637 		       da7219_dac_ng_threshold_tlv),
638 	SOC_SINGLE("DAC NG Switch", DA7219_DAC_NG_CTRL, DA7219_DAC_NG_EN_SHIFT,
639 		   DA7219_SWITCH_EN_MAX, DA7219_NO_INVERT),
640 
641 	/* DACs */
642 	SOC_DOUBLE_R_EXT_TLV("Playback Digital Volume", DA7219_DAC_L_GAIN,
643 			     DA7219_DAC_R_GAIN, DA7219_DAC_L_DIGITAL_GAIN_SHIFT,
644 			     DA7219_DAC_DIGITAL_GAIN_MAX, DA7219_NO_INVERT,
645 			     da7219_volsw_locked_get, da7219_volsw_locked_put,
646 			     da7219_dac_dig_gain_tlv),
647 	SOC_DOUBLE_R_EXT("Playback Digital Switch", DA7219_DAC_L_CTRL,
648 			 DA7219_DAC_R_CTRL, DA7219_DAC_L_MUTE_EN_SHIFT,
649 			 DA7219_SWITCH_EN_MAX, DA7219_INVERT,
650 			 da7219_volsw_locked_get, da7219_volsw_locked_put),
651 	SOC_DOUBLE_R("Playback Digital Gain Ramp Switch", DA7219_DAC_L_CTRL,
652 		     DA7219_DAC_R_CTRL, DA7219_DAC_L_RAMP_EN_SHIFT,
653 		     DA7219_SWITCH_EN_MAX, DA7219_NO_INVERT),
654 
655 	/* CP */
656 	SOC_ENUM("Charge Pump Track Mode", da7219_cp_track_mode),
657 	SOC_SINGLE("Charge Pump Threshold", DA7219_CP_VOL_THRESHOLD1,
658 		   DA7219_CP_THRESH_VDD2_SHIFT, DA7219_CP_THRESH_VDD2_MAX,
659 		   DA7219_NO_INVERT),
660 
661 	/* Headphones */
662 	SOC_DOUBLE_R_EXT_TLV("Headphone Volume", DA7219_HP_L_GAIN,
663 			     DA7219_HP_R_GAIN, DA7219_HP_L_AMP_GAIN_SHIFT,
664 			     DA7219_HP_AMP_GAIN_MAX, DA7219_NO_INVERT,
665 			     da7219_volsw_locked_get, da7219_volsw_locked_put,
666 			     da7219_hp_gain_tlv),
667 	SOC_DOUBLE_R_EXT("Headphone Switch", DA7219_HP_L_CTRL, DA7219_HP_R_CTRL,
668 			 DA7219_HP_L_AMP_MUTE_EN_SHIFT, DA7219_SWITCH_EN_MAX,
669 			 DA7219_INVERT, da7219_volsw_locked_get,
670 			 da7219_volsw_locked_put),
671 	SOC_DOUBLE_R("Headphone Gain Ramp Switch", DA7219_HP_L_CTRL,
672 		     DA7219_HP_R_CTRL, DA7219_HP_L_AMP_RAMP_EN_SHIFT,
673 		     DA7219_SWITCH_EN_MAX, DA7219_NO_INVERT),
674 	SOC_DOUBLE_R("Headphone ZC Gain Switch", DA7219_HP_L_CTRL,
675 		     DA7219_HP_R_CTRL, DA7219_HP_L_AMP_ZC_EN_SHIFT,
676 		     DA7219_SWITCH_EN_MAX, DA7219_NO_INVERT),
677 };
678 
679 
680 /*
681  * DAPM Mux Controls
682  */
683 
684 static const char * const da7219_out_sel_txt[] = {
685 	"ADC", "Tone Generator", "DAIL", "DAIR"
686 };
687 
688 static const struct soc_enum da7219_out_dail_sel =
689 	SOC_ENUM_SINGLE(DA7219_DIG_ROUTING_DAI,
690 			DA7219_DAI_L_SRC_SHIFT,
691 			DA7219_OUT_SRC_MAX,
692 			da7219_out_sel_txt);
693 
694 static const struct snd_kcontrol_new da7219_out_dail_sel_mux =
695 	SOC_DAPM_ENUM("Out DAIL Mux", da7219_out_dail_sel);
696 
697 static const struct soc_enum da7219_out_dair_sel =
698 	SOC_ENUM_SINGLE(DA7219_DIG_ROUTING_DAI,
699 			DA7219_DAI_R_SRC_SHIFT,
700 			DA7219_OUT_SRC_MAX,
701 			da7219_out_sel_txt);
702 
703 static const struct snd_kcontrol_new da7219_out_dair_sel_mux =
704 	SOC_DAPM_ENUM("Out DAIR Mux", da7219_out_dair_sel);
705 
706 static const struct soc_enum da7219_out_dacl_sel =
707 	SOC_ENUM_SINGLE(DA7219_DIG_ROUTING_DAC,
708 			DA7219_DAC_L_SRC_SHIFT,
709 			DA7219_OUT_SRC_MAX,
710 			da7219_out_sel_txt);
711 
712 static const struct snd_kcontrol_new da7219_out_dacl_sel_mux =
713 	SOC_DAPM_ENUM("Out DACL Mux", da7219_out_dacl_sel);
714 
715 static const struct soc_enum da7219_out_dacr_sel =
716 	SOC_ENUM_SINGLE(DA7219_DIG_ROUTING_DAC,
717 			DA7219_DAC_R_SRC_SHIFT,
718 			DA7219_OUT_SRC_MAX,
719 			da7219_out_sel_txt);
720 
721 static const struct snd_kcontrol_new da7219_out_dacr_sel_mux =
722 	SOC_DAPM_ENUM("Out DACR Mux", da7219_out_dacr_sel);
723 
724 
725 /*
726  * DAPM Mixer Controls
727  */
728 
729 static const struct snd_kcontrol_new da7219_mixin_controls[] = {
730 	SOC_DAPM_SINGLE("Mic Switch", DA7219_MIXIN_L_SELECT,
731 			DA7219_MIXIN_L_MIX_SELECT_SHIFT,
732 			DA7219_SWITCH_EN_MAX, DA7219_NO_INVERT),
733 };
734 
735 static const struct snd_kcontrol_new da7219_mixout_l_controls[] = {
736 	SOC_DAPM_SINGLE("DACL Switch", DA7219_MIXOUT_L_SELECT,
737 			DA7219_MIXOUT_L_MIX_SELECT_SHIFT,
738 			DA7219_SWITCH_EN_MAX, DA7219_NO_INVERT),
739 };
740 
741 static const struct snd_kcontrol_new da7219_mixout_r_controls[] = {
742 	SOC_DAPM_SINGLE("DACR Switch", DA7219_MIXOUT_R_SELECT,
743 			DA7219_MIXOUT_R_MIX_SELECT_SHIFT,
744 			DA7219_SWITCH_EN_MAX, DA7219_NO_INVERT),
745 };
746 
747 #define DA7219_DMIX_ST_CTRLS(reg)					\
748 	SOC_DAPM_SINGLE("Out FilterL Switch", reg,			\
749 			DA7219_DMIX_ST_SRC_OUTFILT1L_SHIFT,		\
750 			DA7219_SWITCH_EN_MAX, DA7219_NO_INVERT),	\
751 	SOC_DAPM_SINGLE("Out FilterR Switch", reg,			\
752 			DA7219_DMIX_ST_SRC_OUTFILT1R_SHIFT,		\
753 			DA7219_SWITCH_EN_MAX, DA7219_NO_INVERT),	\
754 	SOC_DAPM_SINGLE("Sidetone Switch", reg,				\
755 			DA7219_DMIX_ST_SRC_SIDETONE_SHIFT,		\
756 			DA7219_SWITCH_EN_MAX, DA7219_NO_INVERT)		\
757 
758 static const struct snd_kcontrol_new da7219_st_out_filtl_mix_controls[] = {
759 	DA7219_DMIX_ST_CTRLS(DA7219_DROUTING_ST_OUTFILT_1L),
760 };
761 
762 static const struct snd_kcontrol_new da7219_st_out_filtr_mix_controls[] = {
763 	DA7219_DMIX_ST_CTRLS(DA7219_DROUTING_ST_OUTFILT_1R),
764 };
765 
766 
767 /*
768  * DAPM Events
769  */
770 
771 static int da7219_mic_pga_event(struct snd_soc_dapm_widget *w,
772 				struct snd_kcontrol *kcontrol, int event)
773 {
774 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
775 	struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
776 
777 	switch (event) {
778 	case SND_SOC_DAPM_POST_PMU:
779 		if (da7219->micbias_on_event) {
780 			/*
781 			 * Delay only for first capture after bias enabled to
782 			 * avoid possible DC offset related noise.
783 			 */
784 			da7219->micbias_on_event = false;
785 			msleep(da7219->mic_pga_delay);
786 		}
787 		break;
788 	default:
789 		break;
790 	}
791 
792 	return 0;
793 }
794 
795 static int da7219_dai_event(struct snd_soc_dapm_widget *w,
796 			    struct snd_kcontrol *kcontrol, int event)
797 {
798 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
799 	struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
800 	u8 pll_ctrl, pll_status;
801 	int i = 0, ret;
802 	bool srm_lock = false;
803 
804 	switch (event) {
805 	case SND_SOC_DAPM_PRE_PMU:
806 		if (da7219->master) {
807 			/* Enable DAI clks for master mode */
808 			if (da7219->dai_clks) {
809 				ret = clk_prepare_enable(da7219->dai_clks);
810 				if (ret) {
811 					dev_err(component->dev,
812 						"Failed to enable dai_clks\n");
813 					return ret;
814 				}
815 			} else {
816 				snd_soc_component_update_bits(component,
817 							      DA7219_DAI_CLK_MODE,
818 							      DA7219_DAI_CLK_EN_MASK,
819 							      DA7219_DAI_CLK_EN_MASK);
820 			}
821 		}
822 
823 		/* PC synchronised to DAI */
824 		snd_soc_component_update_bits(component, DA7219_PC_COUNT,
825 				    DA7219_PC_FREERUN_MASK, 0);
826 
827 		/* Slave mode, if SRM not enabled no need for status checks */
828 		pll_ctrl = snd_soc_component_read32(component, DA7219_PLL_CTRL);
829 		if ((pll_ctrl & DA7219_PLL_MODE_MASK) != DA7219_PLL_MODE_SRM)
830 			return 0;
831 
832 		/* Check SRM has locked */
833 		do {
834 			pll_status = snd_soc_component_read32(component, DA7219_PLL_SRM_STS);
835 			if (pll_status & DA7219_PLL_SRM_STS_SRM_LOCK) {
836 				srm_lock = true;
837 			} else {
838 				++i;
839 				msleep(50);
840 			}
841 		} while ((i < DA7219_SRM_CHECK_RETRIES) & (!srm_lock));
842 
843 		if (!srm_lock)
844 			dev_warn(component->dev, "SRM failed to lock\n");
845 
846 		return 0;
847 	case SND_SOC_DAPM_POST_PMD:
848 		/* PC free-running */
849 		snd_soc_component_update_bits(component, DA7219_PC_COUNT,
850 				    DA7219_PC_FREERUN_MASK,
851 				    DA7219_PC_FREERUN_MASK);
852 
853 		/* Disable DAI clks if in master mode */
854 		if (da7219->master) {
855 			if (da7219->dai_clks)
856 				clk_disable_unprepare(da7219->dai_clks);
857 			else
858 				snd_soc_component_update_bits(component,
859 							      DA7219_DAI_CLK_MODE,
860 							      DA7219_DAI_CLK_EN_MASK,
861 							      0);
862 		}
863 
864 		return 0;
865 	default:
866 		return -EINVAL;
867 	}
868 }
869 
870 static int da7219_settling_event(struct snd_soc_dapm_widget *w,
871 				 struct snd_kcontrol *kcontrol, int event)
872 {
873 	switch (event) {
874 	case SND_SOC_DAPM_POST_PMU:
875 	case SND_SOC_DAPM_POST_PMD:
876 		msleep(DA7219_SETTLING_DELAY);
877 		break;
878 	default:
879 		break;
880 	}
881 
882 	return 0;
883 }
884 
885 static int da7219_mixout_event(struct snd_soc_dapm_widget *w,
886 			       struct snd_kcontrol *kcontrol, int event)
887 {
888 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
889 	u8 hp_ctrl, min_gain_mask;
890 
891 	switch (w->reg) {
892 	case DA7219_MIXOUT_L_CTRL:
893 		hp_ctrl = DA7219_HP_L_CTRL;
894 		min_gain_mask = DA7219_HP_L_AMP_MIN_GAIN_EN_MASK;
895 		break;
896 	case DA7219_MIXOUT_R_CTRL:
897 		hp_ctrl = DA7219_HP_R_CTRL;
898 		min_gain_mask = DA7219_HP_R_AMP_MIN_GAIN_EN_MASK;
899 		break;
900 	default:
901 		return -EINVAL;
902 	}
903 
904 	switch (event) {
905 	case SND_SOC_DAPM_PRE_PMD:
906 		/* Enable minimum gain on HP to avoid pops */
907 		snd_soc_component_update_bits(component, hp_ctrl, min_gain_mask,
908 				    min_gain_mask);
909 
910 		msleep(DA7219_MIN_GAIN_DELAY);
911 
912 		break;
913 	case SND_SOC_DAPM_POST_PMU:
914 		/* Remove minimum gain on HP */
915 		snd_soc_component_update_bits(component, hp_ctrl, min_gain_mask, 0);
916 
917 		break;
918 	}
919 
920 	return 0;
921 }
922 
923 static int da7219_gain_ramp_event(struct snd_soc_dapm_widget *w,
924 				  struct snd_kcontrol *kcontrol, int event)
925 {
926 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
927 	struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
928 
929 	switch (event) {
930 	case SND_SOC_DAPM_PRE_PMU:
931 	case SND_SOC_DAPM_PRE_PMD:
932 		/* Ensure nominal gain ramping for DAPM sequence */
933 		da7219->gain_ramp_ctrl =
934 			snd_soc_component_read32(component, DA7219_GAIN_RAMP_CTRL);
935 		snd_soc_component_write(component, DA7219_GAIN_RAMP_CTRL,
936 			      DA7219_GAIN_RAMP_RATE_NOMINAL);
937 		break;
938 	case SND_SOC_DAPM_POST_PMU:
939 	case SND_SOC_DAPM_POST_PMD:
940 		/* Restore previous gain ramp settings */
941 		snd_soc_component_write(component, DA7219_GAIN_RAMP_CTRL,
942 			      da7219->gain_ramp_ctrl);
943 		break;
944 	}
945 
946 	return 0;
947 }
948 
949 
950 /*
951  * DAPM Widgets
952  */
953 
954 static const struct snd_soc_dapm_widget da7219_dapm_widgets[] = {
955 	/* Input Supplies */
956 	SND_SOC_DAPM_SUPPLY("Mic Bias", DA7219_MICBIAS_CTRL,
957 			    DA7219_MICBIAS1_EN_SHIFT, DA7219_NO_INVERT,
958 			    NULL, 0),
959 
960 	/* Inputs */
961 	SND_SOC_DAPM_INPUT("MIC"),
962 
963 	/* Input PGAs */
964 	SND_SOC_DAPM_PGA_E("Mic PGA", DA7219_MIC_1_CTRL,
965 			   DA7219_MIC_1_AMP_EN_SHIFT, DA7219_NO_INVERT,
966 			   NULL, 0, da7219_mic_pga_event, SND_SOC_DAPM_POST_PMU),
967 	SND_SOC_DAPM_PGA_E("Mixin PGA", DA7219_MIXIN_L_CTRL,
968 			   DA7219_MIXIN_L_AMP_EN_SHIFT, DA7219_NO_INVERT,
969 			   NULL, 0, da7219_settling_event, SND_SOC_DAPM_POST_PMU),
970 
971 	/* Input Filters */
972 	SND_SOC_DAPM_ADC("ADC", NULL, DA7219_ADC_L_CTRL, DA7219_ADC_L_EN_SHIFT,
973 			 DA7219_NO_INVERT),
974 
975 	/* Tone Generator */
976 	SND_SOC_DAPM_SIGGEN("TONE"),
977 	SND_SOC_DAPM_PGA("Tone Generator", DA7219_TONE_GEN_CFG1,
978 			 DA7219_START_STOPN_SHIFT, DA7219_NO_INVERT, NULL, 0),
979 
980 	/* Sidetone Input */
981 	SND_SOC_DAPM_ADC("Sidetone Filter", NULL, DA7219_SIDETONE_CTRL,
982 			 DA7219_SIDETONE_EN_SHIFT, DA7219_NO_INVERT),
983 
984 	/* Input Mixer Supply */
985 	SND_SOC_DAPM_SUPPLY("Mixer In Supply", DA7219_MIXIN_L_CTRL,
986 			    DA7219_MIXIN_L_MIX_EN_SHIFT, DA7219_NO_INVERT,
987 			    NULL, 0),
988 
989 	/* Input Mixer */
990 	SND_SOC_DAPM_MIXER("Mixer In", SND_SOC_NOPM, 0, 0,
991 			   da7219_mixin_controls,
992 			   ARRAY_SIZE(da7219_mixin_controls)),
993 
994 	/* Input Muxes */
995 	SND_SOC_DAPM_MUX("Out DAIL Mux", SND_SOC_NOPM, 0, 0,
996 			 &da7219_out_dail_sel_mux),
997 	SND_SOC_DAPM_MUX("Out DAIR Mux", SND_SOC_NOPM, 0, 0,
998 			 &da7219_out_dair_sel_mux),
999 
1000 	/* DAI Supply */
1001 	SND_SOC_DAPM_SUPPLY("DAI", DA7219_DAI_CTRL, DA7219_DAI_EN_SHIFT,
1002 			    DA7219_NO_INVERT, da7219_dai_event,
1003 			    SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
1004 
1005 	/* DAI */
1006 	SND_SOC_DAPM_AIF_OUT("DAIOUT", "Capture", 0, DA7219_DAI_TDM_CTRL,
1007 			     DA7219_DAI_OE_SHIFT, DA7219_NO_INVERT),
1008 	SND_SOC_DAPM_AIF_IN("DAIIN", "Playback", 0, SND_SOC_NOPM, 0, 0),
1009 
1010 	/* Output Muxes */
1011 	SND_SOC_DAPM_MUX("Out DACL Mux", SND_SOC_NOPM, 0, 0,
1012 			 &da7219_out_dacl_sel_mux),
1013 	SND_SOC_DAPM_MUX("Out DACR Mux", SND_SOC_NOPM, 0, 0,
1014 			 &da7219_out_dacr_sel_mux),
1015 
1016 	/* Output Mixers */
1017 	SND_SOC_DAPM_MIXER("Mixer Out FilterL", SND_SOC_NOPM, 0, 0,
1018 			   da7219_mixout_l_controls,
1019 			   ARRAY_SIZE(da7219_mixout_l_controls)),
1020 	SND_SOC_DAPM_MIXER("Mixer Out FilterR", SND_SOC_NOPM, 0, 0,
1021 			   da7219_mixout_r_controls,
1022 			   ARRAY_SIZE(da7219_mixout_r_controls)),
1023 
1024 	/* Sidetone Mixers */
1025 	SND_SOC_DAPM_MIXER("ST Mixer Out FilterL", SND_SOC_NOPM, 0, 0,
1026 			   da7219_st_out_filtl_mix_controls,
1027 			   ARRAY_SIZE(da7219_st_out_filtl_mix_controls)),
1028 	SND_SOC_DAPM_MIXER("ST Mixer Out FilterR", SND_SOC_NOPM, 0,
1029 			   0, da7219_st_out_filtr_mix_controls,
1030 			   ARRAY_SIZE(da7219_st_out_filtr_mix_controls)),
1031 
1032 	/* DACs */
1033 	SND_SOC_DAPM_DAC_E("DACL", NULL, DA7219_DAC_L_CTRL,
1034 			   DA7219_DAC_L_EN_SHIFT, DA7219_NO_INVERT,
1035 			   da7219_settling_event,
1036 			   SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1037 	SND_SOC_DAPM_DAC_E("DACR", NULL, DA7219_DAC_R_CTRL,
1038 			   DA7219_DAC_R_EN_SHIFT, DA7219_NO_INVERT,
1039 			   da7219_settling_event,
1040 			   SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1041 
1042 	/* Output PGAs */
1043 	SND_SOC_DAPM_PGA_E("Mixout Left PGA", DA7219_MIXOUT_L_CTRL,
1044 			   DA7219_MIXOUT_L_AMP_EN_SHIFT, DA7219_NO_INVERT,
1045 			   NULL, 0, da7219_mixout_event,
1046 			   SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1047 	SND_SOC_DAPM_PGA_E("Mixout Right PGA", DA7219_MIXOUT_R_CTRL,
1048 			   DA7219_MIXOUT_R_AMP_EN_SHIFT, DA7219_NO_INVERT,
1049 			   NULL, 0, da7219_mixout_event,
1050 			   SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1051 	SND_SOC_DAPM_SUPPLY_S("Headphone Left PGA", 1, DA7219_HP_L_CTRL,
1052 			      DA7219_HP_L_AMP_EN_SHIFT, DA7219_NO_INVERT,
1053 			      da7219_settling_event,
1054 			      SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1055 	SND_SOC_DAPM_SUPPLY_S("Headphone Right PGA", 1, DA7219_HP_R_CTRL,
1056 			      DA7219_HP_R_AMP_EN_SHIFT, DA7219_NO_INVERT,
1057 			      da7219_settling_event,
1058 			      SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1059 
1060 	/* Output Supplies */
1061 	SND_SOC_DAPM_SUPPLY_S("Charge Pump", 0, DA7219_CP_CTRL,
1062 			      DA7219_CP_EN_SHIFT, DA7219_NO_INVERT,
1063 			      da7219_settling_event,
1064 			      SND_SOC_DAPM_POST_PMU),
1065 
1066 	/* Outputs */
1067 	SND_SOC_DAPM_OUTPUT("HPL"),
1068 	SND_SOC_DAPM_OUTPUT("HPR"),
1069 
1070 	/* Pre/Post Power */
1071 	SND_SOC_DAPM_PRE("Pre Power Gain Ramp", da7219_gain_ramp_event),
1072 	SND_SOC_DAPM_POST("Post Power Gain Ramp", da7219_gain_ramp_event),
1073 };
1074 
1075 
1076 /*
1077  * DAPM Mux Routes
1078  */
1079 
1080 #define DA7219_OUT_DAI_MUX_ROUTES(name)			\
1081 	{name, "ADC", "Mixer In"},			\
1082 	{name, "Tone Generator", "Tone Generator"},	\
1083 	{name, "DAIL", "DAIOUT"},			\
1084 	{name, "DAIR", "DAIOUT"}
1085 
1086 #define DA7219_OUT_DAC_MUX_ROUTES(name)			\
1087 	{name, "ADC", "Mixer In"},			\
1088 	{name, "Tone Generator", "Tone Generator"},		\
1089 	{name, "DAIL", "DAIIN"},			\
1090 	{name, "DAIR", "DAIIN"}
1091 
1092 /*
1093  * DAPM Mixer Routes
1094  */
1095 
1096 #define DA7219_DMIX_ST_ROUTES(name)				\
1097 	{name, "Out FilterL Switch", "Mixer Out FilterL"},	\
1098 	{name, "Out FilterR Switch", "Mixer Out FilterR"},	\
1099 	{name, "Sidetone Switch", "Sidetone Filter"}
1100 
1101 
1102 /*
1103  * DAPM audio route definition
1104  */
1105 
1106 static const struct snd_soc_dapm_route da7219_audio_map[] = {
1107 	/* Input paths */
1108 	{"MIC", NULL, "Mic Bias"},
1109 	{"Mic PGA", NULL, "MIC"},
1110 	{"Mixin PGA", NULL, "Mic PGA"},
1111 	{"ADC", NULL, "Mixin PGA"},
1112 
1113 	{"Mixer In", NULL, "Mixer In Supply"},
1114 	{"Mixer In", "Mic Switch", "ADC"},
1115 
1116 	{"Sidetone Filter", NULL, "Mixer In"},
1117 
1118 	{"Tone Generator", NULL, "TONE"},
1119 
1120 	DA7219_OUT_DAI_MUX_ROUTES("Out DAIL Mux"),
1121 	DA7219_OUT_DAI_MUX_ROUTES("Out DAIR Mux"),
1122 
1123 	{"DAIOUT", NULL, "Out DAIL Mux"},
1124 	{"DAIOUT", NULL, "Out DAIR Mux"},
1125 	{"DAIOUT", NULL, "DAI"},
1126 
1127 	/* Output paths */
1128 	{"DAIIN", NULL, "DAI"},
1129 
1130 	DA7219_OUT_DAC_MUX_ROUTES("Out DACL Mux"),
1131 	DA7219_OUT_DAC_MUX_ROUTES("Out DACR Mux"),
1132 
1133 	{"Mixer Out FilterL", "DACL Switch", "Out DACL Mux"},
1134 	{"Mixer Out FilterR", "DACR Switch", "Out DACR Mux"},
1135 
1136 	DA7219_DMIX_ST_ROUTES("ST Mixer Out FilterL"),
1137 	DA7219_DMIX_ST_ROUTES("ST Mixer Out FilterR"),
1138 
1139 	{"DACL", NULL, "ST Mixer Out FilterL"},
1140 	{"DACR", NULL, "ST Mixer Out FilterR"},
1141 
1142 	{"Mixout Left PGA", NULL, "DACL"},
1143 	{"Mixout Right PGA", NULL, "DACR"},
1144 
1145 	{"HPL", NULL, "Mixout Left PGA"},
1146 	{"HPR", NULL, "Mixout Right PGA"},
1147 
1148 	{"HPL", NULL, "Headphone Left PGA"},
1149 	{"HPR", NULL, "Headphone Right PGA"},
1150 
1151 	{"HPL", NULL, "Charge Pump"},
1152 	{"HPR", NULL, "Charge Pump"},
1153 };
1154 
1155 
1156 /*
1157  * DAI operations
1158  */
1159 
1160 static int da7219_set_dai_sysclk(struct snd_soc_dai *codec_dai,
1161 				 int clk_id, unsigned int freq, int dir)
1162 {
1163 	struct snd_soc_component *component = codec_dai->component;
1164 	struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
1165 	int ret = 0;
1166 
1167 	if ((da7219->clk_src == clk_id) && (da7219->mclk_rate == freq))
1168 		return 0;
1169 
1170 	if ((freq < 2000000) || (freq > 54000000)) {
1171 		dev_err(codec_dai->dev, "Unsupported MCLK value %d\n",
1172 			freq);
1173 		return -EINVAL;
1174 	}
1175 
1176 	mutex_lock(&da7219->pll_lock);
1177 
1178 	switch (clk_id) {
1179 	case DA7219_CLKSRC_MCLK_SQR:
1180 		snd_soc_component_update_bits(component, DA7219_PLL_CTRL,
1181 				    DA7219_PLL_MCLK_SQR_EN_MASK,
1182 				    DA7219_PLL_MCLK_SQR_EN_MASK);
1183 		break;
1184 	case DA7219_CLKSRC_MCLK:
1185 		snd_soc_component_update_bits(component, DA7219_PLL_CTRL,
1186 				    DA7219_PLL_MCLK_SQR_EN_MASK, 0);
1187 		break;
1188 	default:
1189 		dev_err(codec_dai->dev, "Unknown clock source %d\n", clk_id);
1190 		mutex_unlock(&da7219->pll_lock);
1191 		return -EINVAL;
1192 	}
1193 
1194 	da7219->clk_src = clk_id;
1195 
1196 	if (da7219->mclk) {
1197 		freq = clk_round_rate(da7219->mclk, freq);
1198 		ret = clk_set_rate(da7219->mclk, freq);
1199 		if (ret) {
1200 			dev_err(codec_dai->dev, "Failed to set clock rate %d\n",
1201 				freq);
1202 			mutex_unlock(&da7219->pll_lock);
1203 			return ret;
1204 		}
1205 	}
1206 
1207 	da7219->mclk_rate = freq;
1208 
1209 	mutex_unlock(&da7219->pll_lock);
1210 
1211 	return 0;
1212 }
1213 
1214 int da7219_set_pll(struct snd_soc_component *component, int source, unsigned int fout)
1215 {
1216 	struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
1217 
1218 	u8 pll_ctrl, indiv_bits, indiv;
1219 	u8 pll_frac_top, pll_frac_bot, pll_integer;
1220 	u32 freq_ref;
1221 	u64 frac_div;
1222 
1223 	/* Verify 2MHz - 54MHz MCLK provided, and set input divider */
1224 	if (da7219->mclk_rate < 2000000) {
1225 		dev_err(component->dev, "PLL input clock %d below valid range\n",
1226 			da7219->mclk_rate);
1227 		return -EINVAL;
1228 	} else if (da7219->mclk_rate <= 4500000) {
1229 		indiv_bits = DA7219_PLL_INDIV_2_TO_4_5_MHZ;
1230 		indiv = DA7219_PLL_INDIV_2_TO_4_5_MHZ_VAL;
1231 	} else if (da7219->mclk_rate <= 9000000) {
1232 		indiv_bits = DA7219_PLL_INDIV_4_5_TO_9_MHZ;
1233 		indiv = DA7219_PLL_INDIV_4_5_TO_9_MHZ_VAL;
1234 	} else if (da7219->mclk_rate <= 18000000) {
1235 		indiv_bits = DA7219_PLL_INDIV_9_TO_18_MHZ;
1236 		indiv = DA7219_PLL_INDIV_9_TO_18_MHZ_VAL;
1237 	} else if (da7219->mclk_rate <= 36000000) {
1238 		indiv_bits = DA7219_PLL_INDIV_18_TO_36_MHZ;
1239 		indiv = DA7219_PLL_INDIV_18_TO_36_MHZ_VAL;
1240 	} else if (da7219->mclk_rate <= 54000000) {
1241 		indiv_bits = DA7219_PLL_INDIV_36_TO_54_MHZ;
1242 		indiv = DA7219_PLL_INDIV_36_TO_54_MHZ_VAL;
1243 	} else {
1244 		dev_err(component->dev, "PLL input clock %d above valid range\n",
1245 			da7219->mclk_rate);
1246 		return -EINVAL;
1247 	}
1248 	freq_ref = (da7219->mclk_rate / indiv);
1249 	pll_ctrl = indiv_bits;
1250 
1251 	/* Configure PLL */
1252 	switch (source) {
1253 	case DA7219_SYSCLK_MCLK:
1254 		pll_ctrl |= DA7219_PLL_MODE_BYPASS;
1255 		snd_soc_component_update_bits(component, DA7219_PLL_CTRL,
1256 				    DA7219_PLL_INDIV_MASK |
1257 				    DA7219_PLL_MODE_MASK, pll_ctrl);
1258 		return 0;
1259 	case DA7219_SYSCLK_PLL:
1260 		pll_ctrl |= DA7219_PLL_MODE_NORMAL;
1261 		break;
1262 	case DA7219_SYSCLK_PLL_SRM:
1263 		pll_ctrl |= DA7219_PLL_MODE_SRM;
1264 		break;
1265 	default:
1266 		dev_err(component->dev, "Invalid PLL config\n");
1267 		return -EINVAL;
1268 	}
1269 
1270 	/* Calculate dividers for PLL */
1271 	pll_integer = fout / freq_ref;
1272 	frac_div = (u64)(fout % freq_ref) * 8192ULL;
1273 	do_div(frac_div, freq_ref);
1274 	pll_frac_top = (frac_div >> DA7219_BYTE_SHIFT) & DA7219_BYTE_MASK;
1275 	pll_frac_bot = (frac_div) & DA7219_BYTE_MASK;
1276 
1277 	/* Write PLL config & dividers */
1278 	snd_soc_component_write(component, DA7219_PLL_FRAC_TOP, pll_frac_top);
1279 	snd_soc_component_write(component, DA7219_PLL_FRAC_BOT, pll_frac_bot);
1280 	snd_soc_component_write(component, DA7219_PLL_INTEGER, pll_integer);
1281 	snd_soc_component_update_bits(component, DA7219_PLL_CTRL,
1282 			    DA7219_PLL_INDIV_MASK | DA7219_PLL_MODE_MASK,
1283 			    pll_ctrl);
1284 
1285 	return 0;
1286 }
1287 
1288 static int da7219_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id,
1289 			      int source, unsigned int fref, unsigned int fout)
1290 {
1291 	struct snd_soc_component *component = codec_dai->component;
1292 	struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
1293 	int ret;
1294 
1295 	mutex_lock(&da7219->pll_lock);
1296 	ret = da7219_set_pll(component, source, fout);
1297 	mutex_unlock(&da7219->pll_lock);
1298 
1299 	return ret;
1300 }
1301 
1302 static int da7219_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
1303 {
1304 	struct snd_soc_component *component = codec_dai->component;
1305 	struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
1306 	u8 dai_clk_mode = 0, dai_ctrl = 0;
1307 
1308 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1309 	case SND_SOC_DAIFMT_CBM_CFM:
1310 		da7219->master = true;
1311 		break;
1312 	case SND_SOC_DAIFMT_CBS_CFS:
1313 		da7219->master = false;
1314 		break;
1315 	default:
1316 		return -EINVAL;
1317 	}
1318 
1319 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1320 	case SND_SOC_DAIFMT_I2S:
1321 	case SND_SOC_DAIFMT_LEFT_J:
1322 	case SND_SOC_DAIFMT_RIGHT_J:
1323 		switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1324 		case SND_SOC_DAIFMT_NB_NF:
1325 			break;
1326 		case SND_SOC_DAIFMT_NB_IF:
1327 			dai_clk_mode |= DA7219_DAI_WCLK_POL_INV;
1328 			break;
1329 		case SND_SOC_DAIFMT_IB_NF:
1330 			dai_clk_mode |= DA7219_DAI_CLK_POL_INV;
1331 			break;
1332 		case SND_SOC_DAIFMT_IB_IF:
1333 			dai_clk_mode |= DA7219_DAI_WCLK_POL_INV |
1334 					DA7219_DAI_CLK_POL_INV;
1335 			break;
1336 		default:
1337 			return -EINVAL;
1338 		}
1339 		break;
1340 	case SND_SOC_DAIFMT_DSP_B:
1341 		switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1342 		case SND_SOC_DAIFMT_NB_NF:
1343 			dai_clk_mode |= DA7219_DAI_CLK_POL_INV;
1344 			break;
1345 		case SND_SOC_DAIFMT_NB_IF:
1346 			dai_clk_mode |= DA7219_DAI_WCLK_POL_INV |
1347 					DA7219_DAI_CLK_POL_INV;
1348 			break;
1349 		case SND_SOC_DAIFMT_IB_NF:
1350 			break;
1351 		case SND_SOC_DAIFMT_IB_IF:
1352 			dai_clk_mode |= DA7219_DAI_WCLK_POL_INV;
1353 			break;
1354 		default:
1355 			return -EINVAL;
1356 		}
1357 		break;
1358 	default:
1359 		return -EINVAL;
1360 	}
1361 
1362 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1363 	case SND_SOC_DAIFMT_I2S:
1364 		dai_ctrl |= DA7219_DAI_FORMAT_I2S;
1365 		break;
1366 	case SND_SOC_DAIFMT_LEFT_J:
1367 		dai_ctrl |= DA7219_DAI_FORMAT_LEFT_J;
1368 		break;
1369 	case SND_SOC_DAIFMT_RIGHT_J:
1370 		dai_ctrl |= DA7219_DAI_FORMAT_RIGHT_J;
1371 		break;
1372 	case SND_SOC_DAIFMT_DSP_B:
1373 		dai_ctrl |= DA7219_DAI_FORMAT_DSP;
1374 		break;
1375 	default:
1376 		return -EINVAL;
1377 	}
1378 
1379 	/* By default 64 BCLKs per WCLK is supported */
1380 	dai_clk_mode |= DA7219_DAI_BCLKS_PER_WCLK_64;
1381 
1382 	snd_soc_component_update_bits(component, DA7219_DAI_CLK_MODE,
1383 			    DA7219_DAI_BCLKS_PER_WCLK_MASK |
1384 			    DA7219_DAI_CLK_POL_MASK | DA7219_DAI_WCLK_POL_MASK,
1385 			    dai_clk_mode);
1386 	snd_soc_component_update_bits(component, DA7219_DAI_CTRL, DA7219_DAI_FORMAT_MASK,
1387 			    dai_ctrl);
1388 
1389 	return 0;
1390 }
1391 
1392 static int da7219_set_dai_tdm_slot(struct snd_soc_dai *dai,
1393 				   unsigned int tx_mask, unsigned int rx_mask,
1394 				   int slots, int slot_width)
1395 {
1396 	struct snd_soc_component *component = dai->component;
1397 	struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
1398 	u8 dai_bclks_per_wclk;
1399 	u16 offset;
1400 	u32 frame_size;
1401 
1402 	/* No channels enabled so disable TDM, revert to 64-bit frames */
1403 	if (!tx_mask) {
1404 		snd_soc_component_update_bits(component, DA7219_DAI_TDM_CTRL,
1405 				    DA7219_DAI_TDM_CH_EN_MASK |
1406 				    DA7219_DAI_TDM_MODE_EN_MASK, 0);
1407 		snd_soc_component_update_bits(component, DA7219_DAI_CLK_MODE,
1408 				    DA7219_DAI_BCLKS_PER_WCLK_MASK,
1409 				    DA7219_DAI_BCLKS_PER_WCLK_64);
1410 		return 0;
1411 	}
1412 
1413 	/* Check we have valid slots */
1414 	if (fls(tx_mask) > DA7219_DAI_TDM_MAX_SLOTS) {
1415 		dev_err(component->dev, "Invalid number of slots, max = %d\n",
1416 			DA7219_DAI_TDM_MAX_SLOTS);
1417 		return -EINVAL;
1418 	}
1419 
1420 	/* Check we have a valid offset given */
1421 	if (rx_mask > DA7219_DAI_OFFSET_MAX) {
1422 		dev_err(component->dev, "Invalid slot offset, max = %d\n",
1423 			DA7219_DAI_OFFSET_MAX);
1424 		return -EINVAL;
1425 	}
1426 
1427 	/* Calculate & validate frame size based on slot info provided. */
1428 	frame_size = slots * slot_width;
1429 	switch (frame_size) {
1430 	case 32:
1431 		dai_bclks_per_wclk = DA7219_DAI_BCLKS_PER_WCLK_32;
1432 		break;
1433 	case 64:
1434 		dai_bclks_per_wclk = DA7219_DAI_BCLKS_PER_WCLK_64;
1435 		break;
1436 	case 128:
1437 		dai_bclks_per_wclk = DA7219_DAI_BCLKS_PER_WCLK_128;
1438 		break;
1439 	case 256:
1440 		dai_bclks_per_wclk = DA7219_DAI_BCLKS_PER_WCLK_256;
1441 		break;
1442 	default:
1443 		dev_err(component->dev, "Invalid frame size %d\n", frame_size);
1444 		return -EINVAL;
1445 	}
1446 
1447 	snd_soc_component_update_bits(component, DA7219_DAI_CLK_MODE,
1448 			    DA7219_DAI_BCLKS_PER_WCLK_MASK,
1449 			    dai_bclks_per_wclk);
1450 
1451 	offset = cpu_to_le16(rx_mask);
1452 	regmap_bulk_write(da7219->regmap, DA7219_DAI_OFFSET_LOWER,
1453 			  &offset, sizeof(offset));
1454 
1455 	snd_soc_component_update_bits(component, DA7219_DAI_TDM_CTRL,
1456 			    DA7219_DAI_TDM_CH_EN_MASK |
1457 			    DA7219_DAI_TDM_MODE_EN_MASK,
1458 			    (tx_mask << DA7219_DAI_TDM_CH_EN_SHIFT) |
1459 			    DA7219_DAI_TDM_MODE_EN_MASK);
1460 
1461 	return 0;
1462 }
1463 
1464 static int da7219_hw_params(struct snd_pcm_substream *substream,
1465 			    struct snd_pcm_hw_params *params,
1466 			    struct snd_soc_dai *dai)
1467 {
1468 	struct snd_soc_component *component = dai->component;
1469 	u8 dai_ctrl = 0, fs;
1470 	unsigned int channels;
1471 
1472 	switch (params_width(params)) {
1473 	case 16:
1474 		dai_ctrl |= DA7219_DAI_WORD_LENGTH_S16_LE;
1475 		break;
1476 	case 20:
1477 		dai_ctrl |= DA7219_DAI_WORD_LENGTH_S20_LE;
1478 		break;
1479 	case 24:
1480 		dai_ctrl |= DA7219_DAI_WORD_LENGTH_S24_LE;
1481 		break;
1482 	case 32:
1483 		dai_ctrl |= DA7219_DAI_WORD_LENGTH_S32_LE;
1484 		break;
1485 	default:
1486 		return -EINVAL;
1487 	}
1488 
1489 	channels = params_channels(params);
1490 	if ((channels < 1) || (channels > DA7219_DAI_CH_NUM_MAX)) {
1491 		dev_err(component->dev,
1492 			"Invalid number of channels, only 1 to %d supported\n",
1493 			DA7219_DAI_CH_NUM_MAX);
1494 		return -EINVAL;
1495 	}
1496 	dai_ctrl |= channels << DA7219_DAI_CH_NUM_SHIFT;
1497 
1498 	switch (params_rate(params)) {
1499 	case 8000:
1500 		fs = DA7219_SR_8000;
1501 		break;
1502 	case 11025:
1503 		fs = DA7219_SR_11025;
1504 		break;
1505 	case 12000:
1506 		fs = DA7219_SR_12000;
1507 		break;
1508 	case 16000:
1509 		fs = DA7219_SR_16000;
1510 		break;
1511 	case 22050:
1512 		fs = DA7219_SR_22050;
1513 		break;
1514 	case 24000:
1515 		fs = DA7219_SR_24000;
1516 		break;
1517 	case 32000:
1518 		fs = DA7219_SR_32000;
1519 		break;
1520 	case 44100:
1521 		fs = DA7219_SR_44100;
1522 		break;
1523 	case 48000:
1524 		fs = DA7219_SR_48000;
1525 		break;
1526 	case 88200:
1527 		fs = DA7219_SR_88200;
1528 		break;
1529 	case 96000:
1530 		fs = DA7219_SR_96000;
1531 		break;
1532 	default:
1533 		return -EINVAL;
1534 	}
1535 
1536 	snd_soc_component_update_bits(component, DA7219_DAI_CTRL,
1537 			    DA7219_DAI_WORD_LENGTH_MASK |
1538 			    DA7219_DAI_CH_NUM_MASK,
1539 			    dai_ctrl);
1540 	snd_soc_component_write(component, DA7219_SR, fs);
1541 
1542 	return 0;
1543 }
1544 
1545 static const struct snd_soc_dai_ops da7219_dai_ops = {
1546 	.hw_params	= da7219_hw_params,
1547 	.set_sysclk	= da7219_set_dai_sysclk,
1548 	.set_pll	= da7219_set_dai_pll,
1549 	.set_fmt	= da7219_set_dai_fmt,
1550 	.set_tdm_slot	= da7219_set_dai_tdm_slot,
1551 };
1552 
1553 #define DA7219_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
1554 			SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
1555 
1556 static struct snd_soc_dai_driver da7219_dai = {
1557 	.name = "da7219-hifi",
1558 	.playback = {
1559 		.stream_name = "Playback",
1560 		.channels_min = 1,
1561 		.channels_max = DA7219_DAI_CH_NUM_MAX,
1562 		.rates = SNDRV_PCM_RATE_8000_96000,
1563 		.formats = DA7219_FORMATS,
1564 	},
1565 	.capture = {
1566 		.stream_name = "Capture",
1567 		.channels_min = 1,
1568 		.channels_max = DA7219_DAI_CH_NUM_MAX,
1569 		.rates = SNDRV_PCM_RATE_8000_96000,
1570 		.formats = DA7219_FORMATS,
1571 	},
1572 	.ops = &da7219_dai_ops,
1573 	.symmetric_rates = 1,
1574 	.symmetric_channels = 1,
1575 	.symmetric_samplebits = 1,
1576 };
1577 
1578 
1579 /*
1580  * DT/ACPI
1581  */
1582 
1583 static const struct of_device_id da7219_of_match[] = {
1584 	{ .compatible = "dlg,da7219", },
1585 	{ }
1586 };
1587 MODULE_DEVICE_TABLE(of, da7219_of_match);
1588 
1589 static const struct acpi_device_id da7219_acpi_match[] = {
1590 	{ .id = "DLGS7219", },
1591 	{ }
1592 };
1593 MODULE_DEVICE_TABLE(acpi, da7219_acpi_match);
1594 
1595 static enum da7219_micbias_voltage
1596 	da7219_fw_micbias_lvl(struct device *dev, u32 val)
1597 {
1598 	switch (val) {
1599 	case 1600:
1600 		return DA7219_MICBIAS_1_6V;
1601 	case 1800:
1602 		return DA7219_MICBIAS_1_8V;
1603 	case 2000:
1604 		return DA7219_MICBIAS_2_0V;
1605 	case 2200:
1606 		return DA7219_MICBIAS_2_2V;
1607 	case 2400:
1608 		return DA7219_MICBIAS_2_4V;
1609 	case 2600:
1610 		return DA7219_MICBIAS_2_6V;
1611 	default:
1612 		dev_warn(dev, "Invalid micbias level");
1613 		return DA7219_MICBIAS_2_2V;
1614 	}
1615 }
1616 
1617 static enum da7219_mic_amp_in_sel
1618 	da7219_fw_mic_amp_in_sel(struct device *dev, const char *str)
1619 {
1620 	if (!strcmp(str, "diff")) {
1621 		return DA7219_MIC_AMP_IN_SEL_DIFF;
1622 	} else if (!strcmp(str, "se_p")) {
1623 		return DA7219_MIC_AMP_IN_SEL_SE_P;
1624 	} else if (!strcmp(str, "se_n")) {
1625 		return DA7219_MIC_AMP_IN_SEL_SE_N;
1626 	} else {
1627 		dev_warn(dev, "Invalid mic input type selection");
1628 		return DA7219_MIC_AMP_IN_SEL_DIFF;
1629 	}
1630 }
1631 
1632 static struct da7219_pdata *da7219_fw_to_pdata(struct snd_soc_component *component)
1633 {
1634 	struct device *dev = component->dev;
1635 	struct da7219_pdata *pdata;
1636 	const char *of_str;
1637 	u32 of_val32;
1638 
1639 	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1640 	if (!pdata)
1641 		return NULL;
1642 
1643 	pdata->wakeup_source = device_property_read_bool(dev, "wakeup-source");
1644 
1645 	pdata->dai_clks_name = "da7219-dai-clks";
1646 	if (device_property_read_string(dev, "clock-output-names",
1647 					&pdata->dai_clks_name))
1648 		dev_warn(dev, "Using default clk name: %s\n",
1649 			 pdata->dai_clks_name);
1650 
1651 	if (device_property_read_u32(dev, "dlg,micbias-lvl", &of_val32) >= 0)
1652 		pdata->micbias_lvl = da7219_fw_micbias_lvl(dev, of_val32);
1653 	else
1654 		pdata->micbias_lvl = DA7219_MICBIAS_2_2V;
1655 
1656 	if (!device_property_read_string(dev, "dlg,mic-amp-in-sel", &of_str))
1657 		pdata->mic_amp_in_sel = da7219_fw_mic_amp_in_sel(dev, of_str);
1658 	else
1659 		pdata->mic_amp_in_sel = DA7219_MIC_AMP_IN_SEL_DIFF;
1660 
1661 	return pdata;
1662 }
1663 
1664 
1665 /*
1666  * Codec driver functions
1667  */
1668 
1669 static int da7219_set_bias_level(struct snd_soc_component *component,
1670 				 enum snd_soc_bias_level level)
1671 {
1672 	struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
1673 	int ret;
1674 
1675 	switch (level) {
1676 	case SND_SOC_BIAS_ON:
1677 		break;
1678 	case SND_SOC_BIAS_PREPARE:
1679 		/* Enable MCLK for transition to ON state */
1680 		if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_STANDBY) {
1681 			if (da7219->mclk) {
1682 				ret = clk_prepare_enable(da7219->mclk);
1683 				if (ret) {
1684 					dev_err(component->dev,
1685 						"Failed to enable mclk\n");
1686 					return ret;
1687 				}
1688 			}
1689 		}
1690 
1691 		break;
1692 	case SND_SOC_BIAS_STANDBY:
1693 		if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF)
1694 			/* Master bias */
1695 			snd_soc_component_update_bits(component, DA7219_REFERENCES,
1696 					    DA7219_BIAS_EN_MASK,
1697 					    DA7219_BIAS_EN_MASK);
1698 
1699 		if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_PREPARE) {
1700 			/* Remove MCLK */
1701 			if (da7219->mclk)
1702 				clk_disable_unprepare(da7219->mclk);
1703 		}
1704 		break;
1705 	case SND_SOC_BIAS_OFF:
1706 		/* Only disable master bias if we're not a wake-up source */
1707 		if (!da7219->wakeup_source)
1708 			snd_soc_component_update_bits(component, DA7219_REFERENCES,
1709 					    DA7219_BIAS_EN_MASK, 0);
1710 
1711 		break;
1712 	}
1713 
1714 	return 0;
1715 }
1716 
1717 static const char *da7219_supply_names[DA7219_NUM_SUPPLIES] = {
1718 	[DA7219_SUPPLY_VDD] = "VDD",
1719 	[DA7219_SUPPLY_VDDMIC] = "VDDMIC",
1720 	[DA7219_SUPPLY_VDDIO] = "VDDIO",
1721 };
1722 
1723 static int da7219_handle_supplies(struct snd_soc_component *component)
1724 {
1725 	struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
1726 	struct regulator *vddio;
1727 	u8 io_voltage_lvl = DA7219_IO_VOLTAGE_LEVEL_2_5V_3_6V;
1728 	int i, ret;
1729 
1730 	/* Get required supplies */
1731 	for (i = 0; i < DA7219_NUM_SUPPLIES; ++i)
1732 		da7219->supplies[i].supply = da7219_supply_names[i];
1733 
1734 	ret = devm_regulator_bulk_get(component->dev, DA7219_NUM_SUPPLIES,
1735 				      da7219->supplies);
1736 	if (ret) {
1737 		dev_err(component->dev, "Failed to get supplies");
1738 		return ret;
1739 	}
1740 
1741 	/* Determine VDDIO voltage provided */
1742 	vddio = da7219->supplies[DA7219_SUPPLY_VDDIO].consumer;
1743 	ret = regulator_get_voltage(vddio);
1744 	if (ret < 1200000)
1745 		dev_warn(component->dev, "Invalid VDDIO voltage\n");
1746 	else if (ret < 2800000)
1747 		io_voltage_lvl = DA7219_IO_VOLTAGE_LEVEL_1_2V_2_8V;
1748 
1749 	/* Enable main supplies */
1750 	ret = regulator_bulk_enable(DA7219_NUM_SUPPLIES, da7219->supplies);
1751 	if (ret) {
1752 		dev_err(component->dev, "Failed to enable supplies");
1753 		return ret;
1754 	}
1755 
1756 	/* Ensure device in active mode */
1757 	snd_soc_component_write(component, DA7219_SYSTEM_ACTIVE, DA7219_SYSTEM_ACTIVE_MASK);
1758 
1759 	/* Update IO voltage level range */
1760 	snd_soc_component_write(component, DA7219_IO_CTRL, io_voltage_lvl);
1761 
1762 	return 0;
1763 }
1764 
1765 #ifdef CONFIG_COMMON_CLK
1766 static int da7219_dai_clks_prepare(struct clk_hw *hw)
1767 {
1768 	struct da7219_priv *da7219 =
1769 		container_of(hw, struct da7219_priv, dai_clks_hw);
1770 	struct snd_soc_component *component = da7219->aad->component;
1771 
1772 	snd_soc_component_update_bits(component, DA7219_DAI_CLK_MODE,
1773 				      DA7219_DAI_CLK_EN_MASK,
1774 				      DA7219_DAI_CLK_EN_MASK);
1775 
1776 	return 0;
1777 }
1778 
1779 static void da7219_dai_clks_unprepare(struct clk_hw *hw)
1780 {
1781 	struct da7219_priv *da7219 =
1782 		container_of(hw, struct da7219_priv, dai_clks_hw);
1783 	struct snd_soc_component *component = da7219->aad->component;
1784 
1785 	snd_soc_component_update_bits(component, DA7219_DAI_CLK_MODE,
1786 				      DA7219_DAI_CLK_EN_MASK, 0);
1787 }
1788 
1789 static int da7219_dai_clks_is_prepared(struct clk_hw *hw)
1790 {
1791 	struct da7219_priv *da7219 =
1792 		container_of(hw, struct da7219_priv, dai_clks_hw);
1793 	struct snd_soc_component *component = da7219->aad->component;
1794 	u8 clk_reg;
1795 
1796 	clk_reg = snd_soc_component_read32(component, DA7219_DAI_CLK_MODE);
1797 
1798 	return !!(clk_reg & DA7219_DAI_CLK_EN_MASK);
1799 }
1800 
1801 static const struct clk_ops da7219_dai_clks_ops = {
1802 	.prepare = da7219_dai_clks_prepare,
1803 	.unprepare = da7219_dai_clks_unprepare,
1804 	.is_prepared = da7219_dai_clks_is_prepared,
1805 };
1806 
1807 static void da7219_register_dai_clks(struct snd_soc_component *component)
1808 {
1809 	struct device *dev = component->dev;
1810 	struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
1811 	struct da7219_pdata *pdata = da7219->pdata;
1812 	struct clk_init_data init = {};
1813 	struct clk *dai_clks;
1814 	struct clk_lookup *dai_clks_lookup;
1815 
1816 	init.parent_names = NULL;
1817 	init.num_parents = 0;
1818 	init.name = pdata->dai_clks_name;
1819 	init.ops = &da7219_dai_clks_ops;
1820 	da7219->dai_clks_hw.init = &init;
1821 
1822 	dai_clks = devm_clk_register(dev, &da7219->dai_clks_hw);
1823 	if (IS_ERR(dai_clks)) {
1824 		dev_warn(dev, "Failed to register DAI clocks: %ld\n",
1825 			 PTR_ERR(dai_clks));
1826 		return;
1827 	}
1828 	da7219->dai_clks = dai_clks;
1829 
1830 	/* If we're using DT, then register as provider accordingly */
1831 	if (dev->of_node) {
1832 		devm_of_clk_add_hw_provider(dev, of_clk_hw_simple_get,
1833 					    &da7219->dai_clks_hw);
1834 	} else {
1835 		dai_clks_lookup = clkdev_create(dai_clks, pdata->dai_clks_name,
1836 						"%s", dev_name(dev));
1837 		if (!dai_clks_lookup)
1838 			dev_warn(dev, "Failed to create DAI clkdev");
1839 		else
1840 			da7219->dai_clks_lookup = dai_clks_lookup;
1841 	}
1842 }
1843 #else
1844 static inline void da7219_register_dai_clks(struct snd_soc_component *component) {}
1845 #endif /* CONFIG_COMMON_CLK */
1846 
1847 static void da7219_handle_pdata(struct snd_soc_component *component)
1848 {
1849 	struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
1850 	struct da7219_pdata *pdata = da7219->pdata;
1851 
1852 	if (pdata) {
1853 		u8 micbias_lvl = 0;
1854 
1855 		da7219->wakeup_source = pdata->wakeup_source;
1856 
1857 		da7219_register_dai_clks(component);
1858 
1859 		/* Mic Bias voltages */
1860 		switch (pdata->micbias_lvl) {
1861 		case DA7219_MICBIAS_1_6V:
1862 		case DA7219_MICBIAS_1_8V:
1863 		case DA7219_MICBIAS_2_0V:
1864 		case DA7219_MICBIAS_2_2V:
1865 		case DA7219_MICBIAS_2_4V:
1866 		case DA7219_MICBIAS_2_6V:
1867 			micbias_lvl |= (pdata->micbias_lvl <<
1868 					DA7219_MICBIAS1_LEVEL_SHIFT);
1869 			break;
1870 		}
1871 
1872 		snd_soc_component_write(component, DA7219_MICBIAS_CTRL, micbias_lvl);
1873 
1874 		/*
1875 		 * Calculate delay required to compensate for DC offset in
1876 		 * Mic PGA, based on Mic Bias voltage.
1877 		 */
1878 		da7219->mic_pga_delay =  DA7219_MIC_PGA_BASE_DELAY +
1879 					(pdata->micbias_lvl *
1880 					 DA7219_MIC_PGA_OFFSET_DELAY);
1881 
1882 		/* Mic */
1883 		switch (pdata->mic_amp_in_sel) {
1884 		case DA7219_MIC_AMP_IN_SEL_DIFF:
1885 		case DA7219_MIC_AMP_IN_SEL_SE_P:
1886 		case DA7219_MIC_AMP_IN_SEL_SE_N:
1887 			snd_soc_component_write(component, DA7219_MIC_1_SELECT,
1888 				      pdata->mic_amp_in_sel);
1889 			break;
1890 		}
1891 	}
1892 }
1893 
1894 static struct reg_sequence da7219_rev_aa_patch[] = {
1895 	{ DA7219_REFERENCES, 0x08 },
1896 };
1897 
1898 static int da7219_probe(struct snd_soc_component *component)
1899 {
1900 	struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
1901 	unsigned int rev;
1902 	int ret;
1903 
1904 	mutex_init(&da7219->ctrl_lock);
1905 	mutex_init(&da7219->pll_lock);
1906 
1907 	/* Regulator configuration */
1908 	ret = da7219_handle_supplies(component);
1909 	if (ret)
1910 		return ret;
1911 
1912 	ret = regmap_read(da7219->regmap, DA7219_CHIP_REVISION, &rev);
1913 	if (ret) {
1914 		dev_err(component->dev, "Failed to read chip revision: %d\n", ret);
1915 		goto err_disable_reg;
1916 	}
1917 
1918 	switch (rev & DA7219_CHIP_MINOR_MASK) {
1919 	case 0:
1920 		ret = regmap_register_patch(da7219->regmap, da7219_rev_aa_patch,
1921 					    ARRAY_SIZE(da7219_rev_aa_patch));
1922 		if (ret) {
1923 			dev_err(component->dev, "Failed to register AA patch: %d\n",
1924 				ret);
1925 			goto err_disable_reg;
1926 		}
1927 		break;
1928 	default:
1929 		break;
1930 	}
1931 
1932 	/* Handle DT/ACPI/Platform data */
1933 	da7219->pdata = dev_get_platdata(component->dev);
1934 	if (!da7219->pdata)
1935 		da7219->pdata = da7219_fw_to_pdata(component);
1936 
1937 	da7219_handle_pdata(component);
1938 
1939 	/* Check if MCLK provided */
1940 	da7219->mclk = devm_clk_get(component->dev, "mclk");
1941 	if (IS_ERR(da7219->mclk)) {
1942 		if (PTR_ERR(da7219->mclk) != -ENOENT) {
1943 			ret = PTR_ERR(da7219->mclk);
1944 			goto err_disable_reg;
1945 		} else {
1946 			da7219->mclk = NULL;
1947 		}
1948 	}
1949 
1950 	/* Default PC counter to free-running */
1951 	snd_soc_component_update_bits(component, DA7219_PC_COUNT, DA7219_PC_FREERUN_MASK,
1952 			    DA7219_PC_FREERUN_MASK);
1953 
1954 	/* Default gain ramping */
1955 	snd_soc_component_update_bits(component, DA7219_MIXIN_L_CTRL,
1956 			    DA7219_MIXIN_L_AMP_RAMP_EN_MASK,
1957 			    DA7219_MIXIN_L_AMP_RAMP_EN_MASK);
1958 	snd_soc_component_update_bits(component, DA7219_ADC_L_CTRL, DA7219_ADC_L_RAMP_EN_MASK,
1959 			    DA7219_ADC_L_RAMP_EN_MASK);
1960 	snd_soc_component_update_bits(component, DA7219_DAC_L_CTRL, DA7219_DAC_L_RAMP_EN_MASK,
1961 			    DA7219_DAC_L_RAMP_EN_MASK);
1962 	snd_soc_component_update_bits(component, DA7219_DAC_R_CTRL, DA7219_DAC_R_RAMP_EN_MASK,
1963 			    DA7219_DAC_R_RAMP_EN_MASK);
1964 	snd_soc_component_update_bits(component, DA7219_HP_L_CTRL,
1965 			    DA7219_HP_L_AMP_RAMP_EN_MASK,
1966 			    DA7219_HP_L_AMP_RAMP_EN_MASK);
1967 	snd_soc_component_update_bits(component, DA7219_HP_R_CTRL,
1968 			    DA7219_HP_R_AMP_RAMP_EN_MASK,
1969 			    DA7219_HP_R_AMP_RAMP_EN_MASK);
1970 
1971 	/* Default minimum gain on HP to avoid pops during DAPM sequencing */
1972 	snd_soc_component_update_bits(component, DA7219_HP_L_CTRL,
1973 			    DA7219_HP_L_AMP_MIN_GAIN_EN_MASK,
1974 			    DA7219_HP_L_AMP_MIN_GAIN_EN_MASK);
1975 	snd_soc_component_update_bits(component, DA7219_HP_R_CTRL,
1976 			    DA7219_HP_R_AMP_MIN_GAIN_EN_MASK,
1977 			    DA7219_HP_R_AMP_MIN_GAIN_EN_MASK);
1978 
1979 	/* Default infinite tone gen, start/stop by Kcontrol */
1980 	snd_soc_component_write(component, DA7219_TONE_GEN_CYCLES, DA7219_BEEP_CYCLES_MASK);
1981 
1982 	/* Initialise AAD block */
1983 	ret = da7219_aad_init(component);
1984 	if (ret)
1985 		goto err_disable_reg;
1986 
1987 	return 0;
1988 
1989 err_disable_reg:
1990 	regulator_bulk_disable(DA7219_NUM_SUPPLIES, da7219->supplies);
1991 
1992 	return ret;
1993 }
1994 
1995 static void da7219_remove(struct snd_soc_component *component)
1996 {
1997 	struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
1998 
1999 	da7219_aad_exit(component);
2000 
2001 #ifdef CONFIG_COMMON_CLK
2002 	if (da7219->dai_clks_lookup)
2003 		clkdev_drop(da7219->dai_clks_lookup);
2004 #endif
2005 
2006 	/* Supplies */
2007 	regulator_bulk_disable(DA7219_NUM_SUPPLIES, da7219->supplies);
2008 }
2009 
2010 #ifdef CONFIG_PM
2011 static int da7219_suspend(struct snd_soc_component *component)
2012 {
2013 	struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
2014 
2015 	/* Suspend AAD if we're not a wake-up source */
2016 	if (!da7219->wakeup_source)
2017 		da7219_aad_suspend(component);
2018 
2019 	snd_soc_component_force_bias_level(component, SND_SOC_BIAS_OFF);
2020 
2021 	return 0;
2022 }
2023 
2024 static int da7219_resume(struct snd_soc_component *component)
2025 {
2026 	struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
2027 
2028 	snd_soc_component_force_bias_level(component, SND_SOC_BIAS_STANDBY);
2029 
2030 	/* Resume AAD if previously suspended */
2031 	if (!da7219->wakeup_source)
2032 		da7219_aad_resume(component);
2033 
2034 	return 0;
2035 }
2036 #else
2037 #define da7219_suspend NULL
2038 #define da7219_resume NULL
2039 #endif
2040 
2041 static const struct snd_soc_component_driver soc_component_dev_da7219 = {
2042 	.probe			= da7219_probe,
2043 	.remove			= da7219_remove,
2044 	.suspend		= da7219_suspend,
2045 	.resume			= da7219_resume,
2046 	.set_bias_level		= da7219_set_bias_level,
2047 	.controls		= da7219_snd_controls,
2048 	.num_controls		= ARRAY_SIZE(da7219_snd_controls),
2049 	.dapm_widgets		= da7219_dapm_widgets,
2050 	.num_dapm_widgets	= ARRAY_SIZE(da7219_dapm_widgets),
2051 	.dapm_routes		= da7219_audio_map,
2052 	.num_dapm_routes	= ARRAY_SIZE(da7219_audio_map),
2053 	.idle_bias_on		= 1,
2054 	.use_pmdown_time	= 1,
2055 	.endianness		= 1,
2056 	.non_legacy_dai_naming	= 1,
2057 };
2058 
2059 
2060 /*
2061  * Regmap configs
2062  */
2063 
2064 static struct reg_default da7219_reg_defaults[] = {
2065 	{ DA7219_MIC_1_SELECT, 0x00 },
2066 	{ DA7219_CIF_TIMEOUT_CTRL, 0x01 },
2067 	{ DA7219_SR_24_48, 0x00 },
2068 	{ DA7219_SR, 0x0A },
2069 	{ DA7219_CIF_I2C_ADDR_CFG, 0x02 },
2070 	{ DA7219_PLL_CTRL, 0x10 },
2071 	{ DA7219_PLL_FRAC_TOP, 0x00 },
2072 	{ DA7219_PLL_FRAC_BOT, 0x00 },
2073 	{ DA7219_PLL_INTEGER, 0x20 },
2074 	{ DA7219_DIG_ROUTING_DAI, 0x10 },
2075 	{ DA7219_DAI_CLK_MODE, 0x01 },
2076 	{ DA7219_DAI_CTRL, 0x28 },
2077 	{ DA7219_DAI_TDM_CTRL, 0x40 },
2078 	{ DA7219_DIG_ROUTING_DAC, 0x32 },
2079 	{ DA7219_DAI_OFFSET_LOWER, 0x00 },
2080 	{ DA7219_DAI_OFFSET_UPPER, 0x00 },
2081 	{ DA7219_REFERENCES, 0x08 },
2082 	{ DA7219_MIXIN_L_SELECT, 0x00 },
2083 	{ DA7219_MIXIN_L_GAIN, 0x03 },
2084 	{ DA7219_ADC_L_GAIN, 0x6F },
2085 	{ DA7219_ADC_FILTERS1, 0x80 },
2086 	{ DA7219_MIC_1_GAIN, 0x01 },
2087 	{ DA7219_SIDETONE_CTRL, 0x40 },
2088 	{ DA7219_SIDETONE_GAIN, 0x0E },
2089 	{ DA7219_DROUTING_ST_OUTFILT_1L, 0x01 },
2090 	{ DA7219_DROUTING_ST_OUTFILT_1R, 0x02 },
2091 	{ DA7219_DAC_FILTERS5, 0x00 },
2092 	{ DA7219_DAC_FILTERS2, 0x88 },
2093 	{ DA7219_DAC_FILTERS3, 0x88 },
2094 	{ DA7219_DAC_FILTERS4, 0x08 },
2095 	{ DA7219_DAC_FILTERS1, 0x80 },
2096 	{ DA7219_DAC_L_GAIN, 0x6F },
2097 	{ DA7219_DAC_R_GAIN, 0x6F },
2098 	{ DA7219_CP_CTRL, 0x20 },
2099 	{ DA7219_HP_L_GAIN, 0x39 },
2100 	{ DA7219_HP_R_GAIN, 0x39 },
2101 	{ DA7219_MIXOUT_L_SELECT, 0x00 },
2102 	{ DA7219_MIXOUT_R_SELECT, 0x00 },
2103 	{ DA7219_MICBIAS_CTRL, 0x03 },
2104 	{ DA7219_MIC_1_CTRL, 0x40 },
2105 	{ DA7219_MIXIN_L_CTRL, 0x40 },
2106 	{ DA7219_ADC_L_CTRL, 0x40 },
2107 	{ DA7219_DAC_L_CTRL, 0x40 },
2108 	{ DA7219_DAC_R_CTRL, 0x40 },
2109 	{ DA7219_HP_L_CTRL, 0x40 },
2110 	{ DA7219_HP_R_CTRL, 0x40 },
2111 	{ DA7219_MIXOUT_L_CTRL, 0x10 },
2112 	{ DA7219_MIXOUT_R_CTRL, 0x10 },
2113 	{ DA7219_CHIP_ID1, 0x23 },
2114 	{ DA7219_CHIP_ID2, 0x93 },
2115 	{ DA7219_IO_CTRL, 0x00 },
2116 	{ DA7219_GAIN_RAMP_CTRL, 0x00 },
2117 	{ DA7219_PC_COUNT, 0x02 },
2118 	{ DA7219_CP_VOL_THRESHOLD1, 0x0E },
2119 	{ DA7219_DIG_CTRL, 0x00 },
2120 	{ DA7219_ALC_CTRL2, 0x00 },
2121 	{ DA7219_ALC_CTRL3, 0x00 },
2122 	{ DA7219_ALC_NOISE, 0x3F },
2123 	{ DA7219_ALC_TARGET_MIN, 0x3F },
2124 	{ DA7219_ALC_TARGET_MAX, 0x00 },
2125 	{ DA7219_ALC_GAIN_LIMITS, 0xFF },
2126 	{ DA7219_ALC_ANA_GAIN_LIMITS, 0x71 },
2127 	{ DA7219_ALC_ANTICLIP_CTRL, 0x00 },
2128 	{ DA7219_ALC_ANTICLIP_LEVEL, 0x00 },
2129 	{ DA7219_DAC_NG_SETUP_TIME, 0x00 },
2130 	{ DA7219_DAC_NG_OFF_THRESH, 0x00 },
2131 	{ DA7219_DAC_NG_ON_THRESH, 0x00 },
2132 	{ DA7219_DAC_NG_CTRL, 0x00 },
2133 	{ DA7219_TONE_GEN_CFG1, 0x00 },
2134 	{ DA7219_TONE_GEN_CFG2, 0x00 },
2135 	{ DA7219_TONE_GEN_CYCLES, 0x00 },
2136 	{ DA7219_TONE_GEN_FREQ1_L, 0x55 },
2137 	{ DA7219_TONE_GEN_FREQ1_U, 0x15 },
2138 	{ DA7219_TONE_GEN_FREQ2_L, 0x00 },
2139 	{ DA7219_TONE_GEN_FREQ2_U, 0x40 },
2140 	{ DA7219_TONE_GEN_ON_PER, 0x02 },
2141 	{ DA7219_TONE_GEN_OFF_PER, 0x01 },
2142 	{ DA7219_ACCDET_IRQ_MASK_A, 0x00 },
2143 	{ DA7219_ACCDET_IRQ_MASK_B, 0x00 },
2144 	{ DA7219_ACCDET_CONFIG_1, 0xD6 },
2145 	{ DA7219_ACCDET_CONFIG_2, 0x34 },
2146 	{ DA7219_ACCDET_CONFIG_3, 0x0A },
2147 	{ DA7219_ACCDET_CONFIG_4, 0x16 },
2148 	{ DA7219_ACCDET_CONFIG_5, 0x21 },
2149 	{ DA7219_ACCDET_CONFIG_6, 0x3E },
2150 	{ DA7219_ACCDET_CONFIG_7, 0x01 },
2151 	{ DA7219_SYSTEM_ACTIVE, 0x00 },
2152 };
2153 
2154 static bool da7219_volatile_register(struct device *dev, unsigned int reg)
2155 {
2156 	switch (reg) {
2157 	case DA7219_MIC_1_GAIN_STATUS:
2158 	case DA7219_MIXIN_L_GAIN_STATUS:
2159 	case DA7219_ADC_L_GAIN_STATUS:
2160 	case DA7219_DAC_L_GAIN_STATUS:
2161 	case DA7219_DAC_R_GAIN_STATUS:
2162 	case DA7219_HP_L_GAIN_STATUS:
2163 	case DA7219_HP_R_GAIN_STATUS:
2164 	case DA7219_CIF_CTRL:
2165 	case DA7219_PLL_SRM_STS:
2166 	case DA7219_ALC_CTRL1:
2167 	case DA7219_SYSTEM_MODES_INPUT:
2168 	case DA7219_SYSTEM_MODES_OUTPUT:
2169 	case DA7219_ALC_OFFSET_AUTO_M_L:
2170 	case DA7219_ALC_OFFSET_AUTO_U_L:
2171 	case DA7219_TONE_GEN_CFG1:
2172 	case DA7219_ACCDET_STATUS_A:
2173 	case DA7219_ACCDET_STATUS_B:
2174 	case DA7219_ACCDET_IRQ_EVENT_A:
2175 	case DA7219_ACCDET_IRQ_EVENT_B:
2176 	case DA7219_ACCDET_CONFIG_8:
2177 	case DA7219_SYSTEM_STATUS:
2178 		return true;
2179 	default:
2180 		return false;
2181 	}
2182 }
2183 
2184 static const struct regmap_config da7219_regmap_config = {
2185 	.reg_bits = 8,
2186 	.val_bits = 8,
2187 
2188 	.max_register = DA7219_SYSTEM_ACTIVE,
2189 	.reg_defaults = da7219_reg_defaults,
2190 	.num_reg_defaults = ARRAY_SIZE(da7219_reg_defaults),
2191 	.volatile_reg = da7219_volatile_register,
2192 	.cache_type = REGCACHE_RBTREE,
2193 };
2194 
2195 
2196 /*
2197  * I2C layer
2198  */
2199 
2200 static int da7219_i2c_probe(struct i2c_client *i2c,
2201 			    const struct i2c_device_id *id)
2202 {
2203 	struct da7219_priv *da7219;
2204 	unsigned int system_active, system_status;
2205 	int i, ret;
2206 
2207 	da7219 = devm_kzalloc(&i2c->dev, sizeof(struct da7219_priv),
2208 			      GFP_KERNEL);
2209 	if (!da7219)
2210 		return -ENOMEM;
2211 
2212 	i2c_set_clientdata(i2c, da7219);
2213 
2214 	da7219->regmap = devm_regmap_init_i2c(i2c, &da7219_regmap_config);
2215 	if (IS_ERR(da7219->regmap)) {
2216 		ret = PTR_ERR(da7219->regmap);
2217 		dev_err(&i2c->dev, "regmap_init() failed: %d\n", ret);
2218 		return ret;
2219 	}
2220 
2221 	regcache_cache_bypass(da7219->regmap, true);
2222 
2223 	/* Disable audio paths if still active from previous start */
2224 	regmap_read(da7219->regmap, DA7219_SYSTEM_ACTIVE, &system_active);
2225 	if (system_active) {
2226 		regmap_write(da7219->regmap, DA7219_GAIN_RAMP_CTRL,
2227 			     DA7219_GAIN_RAMP_RATE_NOMINAL);
2228 		regmap_write(da7219->regmap, DA7219_SYSTEM_MODES_INPUT, 0x00);
2229 		regmap_write(da7219->regmap, DA7219_SYSTEM_MODES_OUTPUT, 0x01);
2230 
2231 		for (i = 0; i < DA7219_SYS_STAT_CHECK_RETRIES; ++i) {
2232 			regmap_read(da7219->regmap, DA7219_SYSTEM_STATUS,
2233 				    &system_status);
2234 			if (!system_status)
2235 				break;
2236 
2237 			msleep(DA7219_SYS_STAT_CHECK_DELAY);
2238 		}
2239 	}
2240 
2241 	/* Soft reset component */
2242 	regmap_write_bits(da7219->regmap, DA7219_ACCDET_CONFIG_1,
2243 			  DA7219_ACCDET_EN_MASK, 0);
2244 	regmap_write_bits(da7219->regmap, DA7219_CIF_CTRL,
2245 			  DA7219_CIF_REG_SOFT_RESET_MASK,
2246 			  DA7219_CIF_REG_SOFT_RESET_MASK);
2247 	regmap_write_bits(da7219->regmap, DA7219_SYSTEM_ACTIVE,
2248 			  DA7219_SYSTEM_ACTIVE_MASK, 0);
2249 
2250 	regcache_cache_bypass(da7219->regmap, false);
2251 
2252 	ret = devm_snd_soc_register_component(&i2c->dev,
2253 				     &soc_component_dev_da7219,
2254 				     &da7219_dai, 1);
2255 	if (ret < 0) {
2256 		dev_err(&i2c->dev, "Failed to register da7219 component: %d\n",
2257 			ret);
2258 	}
2259 	return ret;
2260 }
2261 
2262 static int da7219_i2c_remove(struct i2c_client *client)
2263 {
2264 	return 0;
2265 }
2266 
2267 static const struct i2c_device_id da7219_i2c_id[] = {
2268 	{ "da7219", },
2269 	{ }
2270 };
2271 MODULE_DEVICE_TABLE(i2c, da7219_i2c_id);
2272 
2273 static struct i2c_driver da7219_i2c_driver = {
2274 	.driver = {
2275 		.name = "da7219",
2276 		.of_match_table = of_match_ptr(da7219_of_match),
2277 		.acpi_match_table = ACPI_PTR(da7219_acpi_match),
2278 	},
2279 	.probe		= da7219_i2c_probe,
2280 	.remove		= da7219_i2c_remove,
2281 	.id_table	= da7219_i2c_id,
2282 };
2283 
2284 module_i2c_driver(da7219_i2c_driver);
2285 
2286 MODULE_DESCRIPTION("ASoC DA7219 Codec Driver");
2287 MODULE_AUTHOR("Adam Thomson <Adam.Thomson.Opensource@diasemi.com>");
2288 MODULE_LICENSE("GPL");
2289