xref: /openbmc/linux/sound/soc/codecs/da7219.c (revision d2ba09c1)
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_dai_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 	u8 pll_ctrl, pll_status;
777 	int i = 0, ret;
778 	bool srm_lock = false;
779 
780 	switch (event) {
781 	case SND_SOC_DAPM_PRE_PMU:
782 		if (da7219->master) {
783 			/* Enable DAI clks for master mode */
784 			if (da7219->dai_clks) {
785 				ret = clk_prepare_enable(da7219->dai_clks);
786 				if (ret) {
787 					dev_err(component->dev,
788 						"Failed to enable dai_clks\n");
789 					return ret;
790 				}
791 			} else {
792 				snd_soc_component_update_bits(component,
793 							      DA7219_DAI_CLK_MODE,
794 							      DA7219_DAI_CLK_EN_MASK,
795 							      DA7219_DAI_CLK_EN_MASK);
796 			}
797 		}
798 
799 		/* PC synchronised to DAI */
800 		snd_soc_component_update_bits(component, DA7219_PC_COUNT,
801 				    DA7219_PC_FREERUN_MASK, 0);
802 
803 		/* Slave mode, if SRM not enabled no need for status checks */
804 		pll_ctrl = snd_soc_component_read32(component, DA7219_PLL_CTRL);
805 		if ((pll_ctrl & DA7219_PLL_MODE_MASK) != DA7219_PLL_MODE_SRM)
806 			return 0;
807 
808 		/* Check SRM has locked */
809 		do {
810 			pll_status = snd_soc_component_read32(component, DA7219_PLL_SRM_STS);
811 			if (pll_status & DA7219_PLL_SRM_STS_SRM_LOCK) {
812 				srm_lock = true;
813 			} else {
814 				++i;
815 				msleep(50);
816 			}
817 		} while ((i < DA7219_SRM_CHECK_RETRIES) & (!srm_lock));
818 
819 		if (!srm_lock)
820 			dev_warn(component->dev, "SRM failed to lock\n");
821 
822 		return 0;
823 	case SND_SOC_DAPM_POST_PMD:
824 		/* PC free-running */
825 		snd_soc_component_update_bits(component, DA7219_PC_COUNT,
826 				    DA7219_PC_FREERUN_MASK,
827 				    DA7219_PC_FREERUN_MASK);
828 
829 		/* Disable DAI clks if in master mode */
830 		if (da7219->master) {
831 			if (da7219->dai_clks)
832 				clk_disable_unprepare(da7219->dai_clks);
833 			else
834 				snd_soc_component_update_bits(component,
835 							      DA7219_DAI_CLK_MODE,
836 							      DA7219_DAI_CLK_EN_MASK,
837 							      0);
838 		}
839 
840 		return 0;
841 	default:
842 		return -EINVAL;
843 	}
844 }
845 
846 static int da7219_settling_event(struct snd_soc_dapm_widget *w,
847 				 struct snd_kcontrol *kcontrol, int event)
848 {
849 	switch (event) {
850 	case SND_SOC_DAPM_POST_PMU:
851 	case SND_SOC_DAPM_POST_PMD:
852 		msleep(DA7219_SETTLING_DELAY);
853 		break;
854 	default:
855 		break;
856 	}
857 
858 	return 0;
859 }
860 
861 static int da7219_mixout_event(struct snd_soc_dapm_widget *w,
862 			       struct snd_kcontrol *kcontrol, int event)
863 {
864 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
865 	u8 hp_ctrl, min_gain_mask;
866 
867 	switch (w->reg) {
868 	case DA7219_MIXOUT_L_CTRL:
869 		hp_ctrl = DA7219_HP_L_CTRL;
870 		min_gain_mask = DA7219_HP_L_AMP_MIN_GAIN_EN_MASK;
871 		break;
872 	case DA7219_MIXOUT_R_CTRL:
873 		hp_ctrl = DA7219_HP_R_CTRL;
874 		min_gain_mask = DA7219_HP_R_AMP_MIN_GAIN_EN_MASK;
875 		break;
876 	default:
877 		return -EINVAL;
878 	}
879 
880 	switch (event) {
881 	case SND_SOC_DAPM_PRE_PMD:
882 		/* Enable minimum gain on HP to avoid pops */
883 		snd_soc_component_update_bits(component, hp_ctrl, min_gain_mask,
884 				    min_gain_mask);
885 
886 		msleep(DA7219_MIN_GAIN_DELAY);
887 
888 		break;
889 	case SND_SOC_DAPM_POST_PMU:
890 		/* Remove minimum gain on HP */
891 		snd_soc_component_update_bits(component, hp_ctrl, min_gain_mask, 0);
892 
893 		break;
894 	}
895 
896 	return 0;
897 }
898 
899 static int da7219_gain_ramp_event(struct snd_soc_dapm_widget *w,
900 				  struct snd_kcontrol *kcontrol, int event)
901 {
902 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
903 	struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
904 
905 	switch (event) {
906 	case SND_SOC_DAPM_PRE_PMU:
907 	case SND_SOC_DAPM_PRE_PMD:
908 		/* Ensure nominal gain ramping for DAPM sequence */
909 		da7219->gain_ramp_ctrl =
910 			snd_soc_component_read32(component, DA7219_GAIN_RAMP_CTRL);
911 		snd_soc_component_write(component, DA7219_GAIN_RAMP_CTRL,
912 			      DA7219_GAIN_RAMP_RATE_NOMINAL);
913 		break;
914 	case SND_SOC_DAPM_POST_PMU:
915 	case SND_SOC_DAPM_POST_PMD:
916 		/* Restore previous gain ramp settings */
917 		snd_soc_component_write(component, DA7219_GAIN_RAMP_CTRL,
918 			      da7219->gain_ramp_ctrl);
919 		break;
920 	}
921 
922 	return 0;
923 }
924 
925 
926 /*
927  * DAPM Widgets
928  */
929 
930 static const struct snd_soc_dapm_widget da7219_dapm_widgets[] = {
931 	/* Input Supplies */
932 	SND_SOC_DAPM_SUPPLY("Mic Bias", DA7219_MICBIAS_CTRL,
933 			    DA7219_MICBIAS1_EN_SHIFT, DA7219_NO_INVERT,
934 			    NULL, 0),
935 
936 	/* Inputs */
937 	SND_SOC_DAPM_INPUT("MIC"),
938 
939 	/* Input PGAs */
940 	SND_SOC_DAPM_PGA("Mic PGA", DA7219_MIC_1_CTRL,
941 			 DA7219_MIC_1_AMP_EN_SHIFT, DA7219_NO_INVERT,
942 			 NULL, 0),
943 	SND_SOC_DAPM_PGA("Mixin PGA", DA7219_MIXIN_L_CTRL,
944 			 DA7219_MIXIN_L_AMP_EN_SHIFT, DA7219_NO_INVERT,
945 			 NULL, 0),
946 
947 	/* Input Filters */
948 	SND_SOC_DAPM_ADC("ADC", NULL, DA7219_ADC_L_CTRL, DA7219_ADC_L_EN_SHIFT,
949 			 DA7219_NO_INVERT),
950 
951 	/* Tone Generator */
952 	SND_SOC_DAPM_SIGGEN("TONE"),
953 	SND_SOC_DAPM_PGA("Tone Generator", DA7219_TONE_GEN_CFG1,
954 			 DA7219_START_STOPN_SHIFT, DA7219_NO_INVERT, NULL, 0),
955 
956 	/* Sidetone Input */
957 	SND_SOC_DAPM_ADC("Sidetone Filter", NULL, DA7219_SIDETONE_CTRL,
958 			 DA7219_SIDETONE_EN_SHIFT, DA7219_NO_INVERT),
959 
960 	/* Input Mixer Supply */
961 	SND_SOC_DAPM_SUPPLY("Mixer In Supply", DA7219_MIXIN_L_CTRL,
962 			    DA7219_MIXIN_L_MIX_EN_SHIFT, DA7219_NO_INVERT,
963 			    NULL, 0),
964 
965 	/* Input Mixer */
966 	SND_SOC_DAPM_MIXER("Mixer In", SND_SOC_NOPM, 0, 0,
967 			   da7219_mixin_controls,
968 			   ARRAY_SIZE(da7219_mixin_controls)),
969 
970 	/* Input Muxes */
971 	SND_SOC_DAPM_MUX("Out DAIL Mux", SND_SOC_NOPM, 0, 0,
972 			 &da7219_out_dail_sel_mux),
973 	SND_SOC_DAPM_MUX("Out DAIR Mux", SND_SOC_NOPM, 0, 0,
974 			 &da7219_out_dair_sel_mux),
975 
976 	/* DAI Supply */
977 	SND_SOC_DAPM_SUPPLY("DAI", DA7219_DAI_CTRL, DA7219_DAI_EN_SHIFT,
978 			    DA7219_NO_INVERT, da7219_dai_event,
979 			    SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
980 
981 	/* DAI */
982 	SND_SOC_DAPM_AIF_OUT("DAIOUT", "Capture", 0, DA7219_DAI_TDM_CTRL,
983 			     DA7219_DAI_OE_SHIFT, DA7219_NO_INVERT),
984 	SND_SOC_DAPM_AIF_IN("DAIIN", "Playback", 0, SND_SOC_NOPM, 0, 0),
985 
986 	/* Output Muxes */
987 	SND_SOC_DAPM_MUX("Out DACL Mux", SND_SOC_NOPM, 0, 0,
988 			 &da7219_out_dacl_sel_mux),
989 	SND_SOC_DAPM_MUX("Out DACR Mux", SND_SOC_NOPM, 0, 0,
990 			 &da7219_out_dacr_sel_mux),
991 
992 	/* Output Mixers */
993 	SND_SOC_DAPM_MIXER("Mixer Out FilterL", SND_SOC_NOPM, 0, 0,
994 			   da7219_mixout_l_controls,
995 			   ARRAY_SIZE(da7219_mixout_l_controls)),
996 	SND_SOC_DAPM_MIXER("Mixer Out FilterR", SND_SOC_NOPM, 0, 0,
997 			   da7219_mixout_r_controls,
998 			   ARRAY_SIZE(da7219_mixout_r_controls)),
999 
1000 	/* Sidetone Mixers */
1001 	SND_SOC_DAPM_MIXER("ST Mixer Out FilterL", SND_SOC_NOPM, 0, 0,
1002 			   da7219_st_out_filtl_mix_controls,
1003 			   ARRAY_SIZE(da7219_st_out_filtl_mix_controls)),
1004 	SND_SOC_DAPM_MIXER("ST Mixer Out FilterR", SND_SOC_NOPM, 0,
1005 			   0, da7219_st_out_filtr_mix_controls,
1006 			   ARRAY_SIZE(da7219_st_out_filtr_mix_controls)),
1007 
1008 	/* DACs */
1009 	SND_SOC_DAPM_DAC_E("DACL", NULL, DA7219_DAC_L_CTRL,
1010 			   DA7219_DAC_L_EN_SHIFT, DA7219_NO_INVERT,
1011 			   da7219_settling_event,
1012 			   SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1013 	SND_SOC_DAPM_DAC_E("DACR", NULL, DA7219_DAC_R_CTRL,
1014 			   DA7219_DAC_R_EN_SHIFT, DA7219_NO_INVERT,
1015 			   da7219_settling_event,
1016 			   SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1017 
1018 	/* Output PGAs */
1019 	SND_SOC_DAPM_PGA_E("Mixout Left PGA", DA7219_MIXOUT_L_CTRL,
1020 			   DA7219_MIXOUT_L_AMP_EN_SHIFT, DA7219_NO_INVERT,
1021 			   NULL, 0, da7219_mixout_event,
1022 			   SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1023 	SND_SOC_DAPM_PGA_E("Mixout Right PGA", DA7219_MIXOUT_R_CTRL,
1024 			   DA7219_MIXOUT_R_AMP_EN_SHIFT, DA7219_NO_INVERT,
1025 			   NULL, 0, da7219_mixout_event,
1026 			   SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1027 	SND_SOC_DAPM_SUPPLY_S("Headphone Left PGA", 1, DA7219_HP_L_CTRL,
1028 			      DA7219_HP_L_AMP_EN_SHIFT, DA7219_NO_INVERT,
1029 			      da7219_settling_event,
1030 			      SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1031 	SND_SOC_DAPM_SUPPLY_S("Headphone Right PGA", 1, DA7219_HP_R_CTRL,
1032 			      DA7219_HP_R_AMP_EN_SHIFT, DA7219_NO_INVERT,
1033 			      da7219_settling_event,
1034 			      SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1035 
1036 	/* Output Supplies */
1037 	SND_SOC_DAPM_SUPPLY_S("Charge Pump", 0, DA7219_CP_CTRL,
1038 			      DA7219_CP_EN_SHIFT, DA7219_NO_INVERT,
1039 			      da7219_settling_event,
1040 			      SND_SOC_DAPM_POST_PMU),
1041 
1042 	/* Outputs */
1043 	SND_SOC_DAPM_OUTPUT("HPL"),
1044 	SND_SOC_DAPM_OUTPUT("HPR"),
1045 
1046 	/* Pre/Post Power */
1047 	SND_SOC_DAPM_PRE("Pre Power Gain Ramp", da7219_gain_ramp_event),
1048 	SND_SOC_DAPM_POST("Post Power Gain Ramp", da7219_gain_ramp_event),
1049 };
1050 
1051 
1052 /*
1053  * DAPM Mux Routes
1054  */
1055 
1056 #define DA7219_OUT_DAI_MUX_ROUTES(name)			\
1057 	{name, "ADC", "Mixer In"},			\
1058 	{name, "Tone Generator", "Tone Generator"},	\
1059 	{name, "DAIL", "DAIOUT"},			\
1060 	{name, "DAIR", "DAIOUT"}
1061 
1062 #define DA7219_OUT_DAC_MUX_ROUTES(name)			\
1063 	{name, "ADC", "Mixer In"},			\
1064 	{name, "Tone Generator", "Tone Generator"},		\
1065 	{name, "DAIL", "DAIIN"},			\
1066 	{name, "DAIR", "DAIIN"}
1067 
1068 /*
1069  * DAPM Mixer Routes
1070  */
1071 
1072 #define DA7219_DMIX_ST_ROUTES(name)				\
1073 	{name, "Out FilterL Switch", "Mixer Out FilterL"},	\
1074 	{name, "Out FilterR Switch", "Mixer Out FilterR"},	\
1075 	{name, "Sidetone Switch", "Sidetone Filter"}
1076 
1077 
1078 /*
1079  * DAPM audio route definition
1080  */
1081 
1082 static const struct snd_soc_dapm_route da7219_audio_map[] = {
1083 	/* Input paths */
1084 	{"MIC", NULL, "Mic Bias"},
1085 	{"Mic PGA", NULL, "MIC"},
1086 	{"Mixin PGA", NULL, "Mic PGA"},
1087 	{"ADC", NULL, "Mixin PGA"},
1088 
1089 	{"Mixer In", NULL, "Mixer In Supply"},
1090 	{"Mixer In", "Mic Switch", "ADC"},
1091 
1092 	{"Sidetone Filter", NULL, "Mixer In"},
1093 
1094 	{"Tone Generator", NULL, "TONE"},
1095 
1096 	DA7219_OUT_DAI_MUX_ROUTES("Out DAIL Mux"),
1097 	DA7219_OUT_DAI_MUX_ROUTES("Out DAIR Mux"),
1098 
1099 	{"DAIOUT", NULL, "Out DAIL Mux"},
1100 	{"DAIOUT", NULL, "Out DAIR Mux"},
1101 	{"DAIOUT", NULL, "DAI"},
1102 
1103 	/* Output paths */
1104 	{"DAIIN", NULL, "DAI"},
1105 
1106 	DA7219_OUT_DAC_MUX_ROUTES("Out DACL Mux"),
1107 	DA7219_OUT_DAC_MUX_ROUTES("Out DACR Mux"),
1108 
1109 	{"Mixer Out FilterL", "DACL Switch", "Out DACL Mux"},
1110 	{"Mixer Out FilterR", "DACR Switch", "Out DACR Mux"},
1111 
1112 	DA7219_DMIX_ST_ROUTES("ST Mixer Out FilterL"),
1113 	DA7219_DMIX_ST_ROUTES("ST Mixer Out FilterR"),
1114 
1115 	{"DACL", NULL, "ST Mixer Out FilterL"},
1116 	{"DACR", NULL, "ST Mixer Out FilterR"},
1117 
1118 	{"Mixout Left PGA", NULL, "DACL"},
1119 	{"Mixout Right PGA", NULL, "DACR"},
1120 
1121 	{"HPL", NULL, "Mixout Left PGA"},
1122 	{"HPR", NULL, "Mixout Right PGA"},
1123 
1124 	{"HPL", NULL, "Headphone Left PGA"},
1125 	{"HPR", NULL, "Headphone Right PGA"},
1126 
1127 	{"HPL", NULL, "Charge Pump"},
1128 	{"HPR", NULL, "Charge Pump"},
1129 };
1130 
1131 
1132 /*
1133  * DAI operations
1134  */
1135 
1136 static int da7219_set_dai_sysclk(struct snd_soc_dai *codec_dai,
1137 				 int clk_id, unsigned int freq, int dir)
1138 {
1139 	struct snd_soc_component *component = codec_dai->component;
1140 	struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
1141 	int ret = 0;
1142 
1143 	if ((da7219->clk_src == clk_id) && (da7219->mclk_rate == freq))
1144 		return 0;
1145 
1146 	if ((freq < 2000000) || (freq > 54000000)) {
1147 		dev_err(codec_dai->dev, "Unsupported MCLK value %d\n",
1148 			freq);
1149 		return -EINVAL;
1150 	}
1151 
1152 	mutex_lock(&da7219->pll_lock);
1153 
1154 	switch (clk_id) {
1155 	case DA7219_CLKSRC_MCLK_SQR:
1156 		snd_soc_component_update_bits(component, DA7219_PLL_CTRL,
1157 				    DA7219_PLL_MCLK_SQR_EN_MASK,
1158 				    DA7219_PLL_MCLK_SQR_EN_MASK);
1159 		break;
1160 	case DA7219_CLKSRC_MCLK:
1161 		snd_soc_component_update_bits(component, DA7219_PLL_CTRL,
1162 				    DA7219_PLL_MCLK_SQR_EN_MASK, 0);
1163 		break;
1164 	default:
1165 		dev_err(codec_dai->dev, "Unknown clock source %d\n", clk_id);
1166 		mutex_unlock(&da7219->pll_lock);
1167 		return -EINVAL;
1168 	}
1169 
1170 	da7219->clk_src = clk_id;
1171 
1172 	if (da7219->mclk) {
1173 		freq = clk_round_rate(da7219->mclk, freq);
1174 		ret = clk_set_rate(da7219->mclk, freq);
1175 		if (ret) {
1176 			dev_err(codec_dai->dev, "Failed to set clock rate %d\n",
1177 				freq);
1178 			mutex_unlock(&da7219->pll_lock);
1179 			return ret;
1180 		}
1181 	}
1182 
1183 	da7219->mclk_rate = freq;
1184 
1185 	mutex_unlock(&da7219->pll_lock);
1186 
1187 	return 0;
1188 }
1189 
1190 int da7219_set_pll(struct snd_soc_component *component, int source, unsigned int fout)
1191 {
1192 	struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
1193 
1194 	u8 pll_ctrl, indiv_bits, indiv;
1195 	u8 pll_frac_top, pll_frac_bot, pll_integer;
1196 	u32 freq_ref;
1197 	u64 frac_div;
1198 
1199 	/* Verify 2MHz - 54MHz MCLK provided, and set input divider */
1200 	if (da7219->mclk_rate < 2000000) {
1201 		dev_err(component->dev, "PLL input clock %d below valid range\n",
1202 			da7219->mclk_rate);
1203 		return -EINVAL;
1204 	} else if (da7219->mclk_rate <= 4500000) {
1205 		indiv_bits = DA7219_PLL_INDIV_2_TO_4_5_MHZ;
1206 		indiv = DA7219_PLL_INDIV_2_TO_4_5_MHZ_VAL;
1207 	} else if (da7219->mclk_rate <= 9000000) {
1208 		indiv_bits = DA7219_PLL_INDIV_4_5_TO_9_MHZ;
1209 		indiv = DA7219_PLL_INDIV_4_5_TO_9_MHZ_VAL;
1210 	} else if (da7219->mclk_rate <= 18000000) {
1211 		indiv_bits = DA7219_PLL_INDIV_9_TO_18_MHZ;
1212 		indiv = DA7219_PLL_INDIV_9_TO_18_MHZ_VAL;
1213 	} else if (da7219->mclk_rate <= 36000000) {
1214 		indiv_bits = DA7219_PLL_INDIV_18_TO_36_MHZ;
1215 		indiv = DA7219_PLL_INDIV_18_TO_36_MHZ_VAL;
1216 	} else if (da7219->mclk_rate <= 54000000) {
1217 		indiv_bits = DA7219_PLL_INDIV_36_TO_54_MHZ;
1218 		indiv = DA7219_PLL_INDIV_36_TO_54_MHZ_VAL;
1219 	} else {
1220 		dev_err(component->dev, "PLL input clock %d above valid range\n",
1221 			da7219->mclk_rate);
1222 		return -EINVAL;
1223 	}
1224 	freq_ref = (da7219->mclk_rate / indiv);
1225 	pll_ctrl = indiv_bits;
1226 
1227 	/* Configure PLL */
1228 	switch (source) {
1229 	case DA7219_SYSCLK_MCLK:
1230 		pll_ctrl |= DA7219_PLL_MODE_BYPASS;
1231 		snd_soc_component_update_bits(component, DA7219_PLL_CTRL,
1232 				    DA7219_PLL_INDIV_MASK |
1233 				    DA7219_PLL_MODE_MASK, pll_ctrl);
1234 		return 0;
1235 	case DA7219_SYSCLK_PLL:
1236 		pll_ctrl |= DA7219_PLL_MODE_NORMAL;
1237 		break;
1238 	case DA7219_SYSCLK_PLL_SRM:
1239 		pll_ctrl |= DA7219_PLL_MODE_SRM;
1240 		break;
1241 	default:
1242 		dev_err(component->dev, "Invalid PLL config\n");
1243 		return -EINVAL;
1244 	}
1245 
1246 	/* Calculate dividers for PLL */
1247 	pll_integer = fout / freq_ref;
1248 	frac_div = (u64)(fout % freq_ref) * 8192ULL;
1249 	do_div(frac_div, freq_ref);
1250 	pll_frac_top = (frac_div >> DA7219_BYTE_SHIFT) & DA7219_BYTE_MASK;
1251 	pll_frac_bot = (frac_div) & DA7219_BYTE_MASK;
1252 
1253 	/* Write PLL config & dividers */
1254 	snd_soc_component_write(component, DA7219_PLL_FRAC_TOP, pll_frac_top);
1255 	snd_soc_component_write(component, DA7219_PLL_FRAC_BOT, pll_frac_bot);
1256 	snd_soc_component_write(component, DA7219_PLL_INTEGER, pll_integer);
1257 	snd_soc_component_update_bits(component, DA7219_PLL_CTRL,
1258 			    DA7219_PLL_INDIV_MASK | DA7219_PLL_MODE_MASK,
1259 			    pll_ctrl);
1260 
1261 	return 0;
1262 }
1263 
1264 static int da7219_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id,
1265 			      int source, unsigned int fref, unsigned int fout)
1266 {
1267 	struct snd_soc_component *component = codec_dai->component;
1268 	struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
1269 	int ret;
1270 
1271 	mutex_lock(&da7219->pll_lock);
1272 	ret = da7219_set_pll(component, source, fout);
1273 	mutex_unlock(&da7219->pll_lock);
1274 
1275 	return ret;
1276 }
1277 
1278 static int da7219_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
1279 {
1280 	struct snd_soc_component *component = codec_dai->component;
1281 	struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
1282 	u8 dai_clk_mode = 0, dai_ctrl = 0;
1283 
1284 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1285 	case SND_SOC_DAIFMT_CBM_CFM:
1286 		da7219->master = true;
1287 		break;
1288 	case SND_SOC_DAIFMT_CBS_CFS:
1289 		da7219->master = false;
1290 		break;
1291 	default:
1292 		return -EINVAL;
1293 	}
1294 
1295 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1296 	case SND_SOC_DAIFMT_I2S:
1297 	case SND_SOC_DAIFMT_LEFT_J:
1298 	case SND_SOC_DAIFMT_RIGHT_J:
1299 		switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1300 		case SND_SOC_DAIFMT_NB_NF:
1301 			break;
1302 		case SND_SOC_DAIFMT_NB_IF:
1303 			dai_clk_mode |= DA7219_DAI_WCLK_POL_INV;
1304 			break;
1305 		case SND_SOC_DAIFMT_IB_NF:
1306 			dai_clk_mode |= DA7219_DAI_CLK_POL_INV;
1307 			break;
1308 		case SND_SOC_DAIFMT_IB_IF:
1309 			dai_clk_mode |= DA7219_DAI_WCLK_POL_INV |
1310 					DA7219_DAI_CLK_POL_INV;
1311 			break;
1312 		default:
1313 			return -EINVAL;
1314 		}
1315 		break;
1316 	case SND_SOC_DAIFMT_DSP_B:
1317 		switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1318 		case SND_SOC_DAIFMT_NB_NF:
1319 			dai_clk_mode |= DA7219_DAI_CLK_POL_INV;
1320 			break;
1321 		case SND_SOC_DAIFMT_NB_IF:
1322 			dai_clk_mode |= DA7219_DAI_WCLK_POL_INV |
1323 					DA7219_DAI_CLK_POL_INV;
1324 			break;
1325 		case SND_SOC_DAIFMT_IB_NF:
1326 			break;
1327 		case SND_SOC_DAIFMT_IB_IF:
1328 			dai_clk_mode |= DA7219_DAI_WCLK_POL_INV;
1329 			break;
1330 		default:
1331 			return -EINVAL;
1332 		}
1333 		break;
1334 	default:
1335 		return -EINVAL;
1336 	}
1337 
1338 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1339 	case SND_SOC_DAIFMT_I2S:
1340 		dai_ctrl |= DA7219_DAI_FORMAT_I2S;
1341 		break;
1342 	case SND_SOC_DAIFMT_LEFT_J:
1343 		dai_ctrl |= DA7219_DAI_FORMAT_LEFT_J;
1344 		break;
1345 	case SND_SOC_DAIFMT_RIGHT_J:
1346 		dai_ctrl |= DA7219_DAI_FORMAT_RIGHT_J;
1347 		break;
1348 	case SND_SOC_DAIFMT_DSP_B:
1349 		dai_ctrl |= DA7219_DAI_FORMAT_DSP;
1350 		break;
1351 	default:
1352 		return -EINVAL;
1353 	}
1354 
1355 	/* By default 64 BCLKs per WCLK is supported */
1356 	dai_clk_mode |= DA7219_DAI_BCLKS_PER_WCLK_64;
1357 
1358 	snd_soc_component_update_bits(component, DA7219_DAI_CLK_MODE,
1359 			    DA7219_DAI_BCLKS_PER_WCLK_MASK |
1360 			    DA7219_DAI_CLK_POL_MASK | DA7219_DAI_WCLK_POL_MASK,
1361 			    dai_clk_mode);
1362 	snd_soc_component_update_bits(component, DA7219_DAI_CTRL, DA7219_DAI_FORMAT_MASK,
1363 			    dai_ctrl);
1364 
1365 	return 0;
1366 }
1367 
1368 static int da7219_set_dai_tdm_slot(struct snd_soc_dai *dai,
1369 				   unsigned int tx_mask, unsigned int rx_mask,
1370 				   int slots, int slot_width)
1371 {
1372 	struct snd_soc_component *component = dai->component;
1373 	struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
1374 	u8 dai_bclks_per_wclk;
1375 	u16 offset;
1376 	u32 frame_size;
1377 
1378 	/* No channels enabled so disable TDM, revert to 64-bit frames */
1379 	if (!tx_mask) {
1380 		snd_soc_component_update_bits(component, DA7219_DAI_TDM_CTRL,
1381 				    DA7219_DAI_TDM_CH_EN_MASK |
1382 				    DA7219_DAI_TDM_MODE_EN_MASK, 0);
1383 		snd_soc_component_update_bits(component, DA7219_DAI_CLK_MODE,
1384 				    DA7219_DAI_BCLKS_PER_WCLK_MASK,
1385 				    DA7219_DAI_BCLKS_PER_WCLK_64);
1386 		return 0;
1387 	}
1388 
1389 	/* Check we have valid slots */
1390 	if (fls(tx_mask) > DA7219_DAI_TDM_MAX_SLOTS) {
1391 		dev_err(component->dev, "Invalid number of slots, max = %d\n",
1392 			DA7219_DAI_TDM_MAX_SLOTS);
1393 		return -EINVAL;
1394 	}
1395 
1396 	/* Check we have a valid offset given */
1397 	if (rx_mask > DA7219_DAI_OFFSET_MAX) {
1398 		dev_err(component->dev, "Invalid slot offset, max = %d\n",
1399 			DA7219_DAI_OFFSET_MAX);
1400 		return -EINVAL;
1401 	}
1402 
1403 	/* Calculate & validate frame size based on slot info provided. */
1404 	frame_size = slots * slot_width;
1405 	switch (frame_size) {
1406 	case 32:
1407 		dai_bclks_per_wclk = DA7219_DAI_BCLKS_PER_WCLK_32;
1408 		break;
1409 	case 64:
1410 		dai_bclks_per_wclk = DA7219_DAI_BCLKS_PER_WCLK_64;
1411 		break;
1412 	case 128:
1413 		dai_bclks_per_wclk = DA7219_DAI_BCLKS_PER_WCLK_128;
1414 		break;
1415 	case 256:
1416 		dai_bclks_per_wclk = DA7219_DAI_BCLKS_PER_WCLK_256;
1417 		break;
1418 	default:
1419 		dev_err(component->dev, "Invalid frame size %d\n", frame_size);
1420 		return -EINVAL;
1421 	}
1422 
1423 	snd_soc_component_update_bits(component, DA7219_DAI_CLK_MODE,
1424 			    DA7219_DAI_BCLKS_PER_WCLK_MASK,
1425 			    dai_bclks_per_wclk);
1426 
1427 	offset = cpu_to_le16(rx_mask);
1428 	regmap_bulk_write(da7219->regmap, DA7219_DAI_OFFSET_LOWER,
1429 			  &offset, sizeof(offset));
1430 
1431 	snd_soc_component_update_bits(component, DA7219_DAI_TDM_CTRL,
1432 			    DA7219_DAI_TDM_CH_EN_MASK |
1433 			    DA7219_DAI_TDM_MODE_EN_MASK,
1434 			    (tx_mask << DA7219_DAI_TDM_CH_EN_SHIFT) |
1435 			    DA7219_DAI_TDM_MODE_EN_MASK);
1436 
1437 	return 0;
1438 }
1439 
1440 static int da7219_hw_params(struct snd_pcm_substream *substream,
1441 			    struct snd_pcm_hw_params *params,
1442 			    struct snd_soc_dai *dai)
1443 {
1444 	struct snd_soc_component *component = dai->component;
1445 	u8 dai_ctrl = 0, fs;
1446 	unsigned int channels;
1447 
1448 	switch (params_width(params)) {
1449 	case 16:
1450 		dai_ctrl |= DA7219_DAI_WORD_LENGTH_S16_LE;
1451 		break;
1452 	case 20:
1453 		dai_ctrl |= DA7219_DAI_WORD_LENGTH_S20_LE;
1454 		break;
1455 	case 24:
1456 		dai_ctrl |= DA7219_DAI_WORD_LENGTH_S24_LE;
1457 		break;
1458 	case 32:
1459 		dai_ctrl |= DA7219_DAI_WORD_LENGTH_S32_LE;
1460 		break;
1461 	default:
1462 		return -EINVAL;
1463 	}
1464 
1465 	channels = params_channels(params);
1466 	if ((channels < 1) || (channels > DA7219_DAI_CH_NUM_MAX)) {
1467 		dev_err(component->dev,
1468 			"Invalid number of channels, only 1 to %d supported\n",
1469 			DA7219_DAI_CH_NUM_MAX);
1470 		return -EINVAL;
1471 	}
1472 	dai_ctrl |= channels << DA7219_DAI_CH_NUM_SHIFT;
1473 
1474 	switch (params_rate(params)) {
1475 	case 8000:
1476 		fs = DA7219_SR_8000;
1477 		break;
1478 	case 11025:
1479 		fs = DA7219_SR_11025;
1480 		break;
1481 	case 12000:
1482 		fs = DA7219_SR_12000;
1483 		break;
1484 	case 16000:
1485 		fs = DA7219_SR_16000;
1486 		break;
1487 	case 22050:
1488 		fs = DA7219_SR_22050;
1489 		break;
1490 	case 24000:
1491 		fs = DA7219_SR_24000;
1492 		break;
1493 	case 32000:
1494 		fs = DA7219_SR_32000;
1495 		break;
1496 	case 44100:
1497 		fs = DA7219_SR_44100;
1498 		break;
1499 	case 48000:
1500 		fs = DA7219_SR_48000;
1501 		break;
1502 	case 88200:
1503 		fs = DA7219_SR_88200;
1504 		break;
1505 	case 96000:
1506 		fs = DA7219_SR_96000;
1507 		break;
1508 	default:
1509 		return -EINVAL;
1510 	}
1511 
1512 	snd_soc_component_update_bits(component, DA7219_DAI_CTRL,
1513 			    DA7219_DAI_WORD_LENGTH_MASK |
1514 			    DA7219_DAI_CH_NUM_MASK,
1515 			    dai_ctrl);
1516 	snd_soc_component_write(component, DA7219_SR, fs);
1517 
1518 	return 0;
1519 }
1520 
1521 static const struct snd_soc_dai_ops da7219_dai_ops = {
1522 	.hw_params	= da7219_hw_params,
1523 	.set_sysclk	= da7219_set_dai_sysclk,
1524 	.set_pll	= da7219_set_dai_pll,
1525 	.set_fmt	= da7219_set_dai_fmt,
1526 	.set_tdm_slot	= da7219_set_dai_tdm_slot,
1527 };
1528 
1529 #define DA7219_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
1530 			SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
1531 
1532 static struct snd_soc_dai_driver da7219_dai = {
1533 	.name = "da7219-hifi",
1534 	.playback = {
1535 		.stream_name = "Playback",
1536 		.channels_min = 1,
1537 		.channels_max = DA7219_DAI_CH_NUM_MAX,
1538 		.rates = SNDRV_PCM_RATE_8000_96000,
1539 		.formats = DA7219_FORMATS,
1540 	},
1541 	.capture = {
1542 		.stream_name = "Capture",
1543 		.channels_min = 1,
1544 		.channels_max = DA7219_DAI_CH_NUM_MAX,
1545 		.rates = SNDRV_PCM_RATE_8000_96000,
1546 		.formats = DA7219_FORMATS,
1547 	},
1548 	.ops = &da7219_dai_ops,
1549 	.symmetric_rates = 1,
1550 	.symmetric_channels = 1,
1551 	.symmetric_samplebits = 1,
1552 };
1553 
1554 
1555 /*
1556  * DT/ACPI
1557  */
1558 
1559 static const struct of_device_id da7219_of_match[] = {
1560 	{ .compatible = "dlg,da7219", },
1561 	{ }
1562 };
1563 MODULE_DEVICE_TABLE(of, da7219_of_match);
1564 
1565 static const struct acpi_device_id da7219_acpi_match[] = {
1566 	{ .id = "DLGS7219", },
1567 	{ }
1568 };
1569 MODULE_DEVICE_TABLE(acpi, da7219_acpi_match);
1570 
1571 static enum da7219_micbias_voltage
1572 	da7219_fw_micbias_lvl(struct device *dev, u32 val)
1573 {
1574 	switch (val) {
1575 	case 1600:
1576 		return DA7219_MICBIAS_1_6V;
1577 	case 1800:
1578 		return DA7219_MICBIAS_1_8V;
1579 	case 2000:
1580 		return DA7219_MICBIAS_2_0V;
1581 	case 2200:
1582 		return DA7219_MICBIAS_2_2V;
1583 	case 2400:
1584 		return DA7219_MICBIAS_2_4V;
1585 	case 2600:
1586 		return DA7219_MICBIAS_2_6V;
1587 	default:
1588 		dev_warn(dev, "Invalid micbias level");
1589 		return DA7219_MICBIAS_2_2V;
1590 	}
1591 }
1592 
1593 static enum da7219_mic_amp_in_sel
1594 	da7219_fw_mic_amp_in_sel(struct device *dev, const char *str)
1595 {
1596 	if (!strcmp(str, "diff")) {
1597 		return DA7219_MIC_AMP_IN_SEL_DIFF;
1598 	} else if (!strcmp(str, "se_p")) {
1599 		return DA7219_MIC_AMP_IN_SEL_SE_P;
1600 	} else if (!strcmp(str, "se_n")) {
1601 		return DA7219_MIC_AMP_IN_SEL_SE_N;
1602 	} else {
1603 		dev_warn(dev, "Invalid mic input type selection");
1604 		return DA7219_MIC_AMP_IN_SEL_DIFF;
1605 	}
1606 }
1607 
1608 static struct da7219_pdata *da7219_fw_to_pdata(struct snd_soc_component *component)
1609 {
1610 	struct device *dev = component->dev;
1611 	struct da7219_pdata *pdata;
1612 	const char *of_str;
1613 	u32 of_val32;
1614 
1615 	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1616 	if (!pdata)
1617 		return NULL;
1618 
1619 	pdata->wakeup_source = device_property_read_bool(dev, "wakeup-source");
1620 
1621 	pdata->dai_clks_name = "da7219-dai-clks";
1622 	if (device_property_read_string(dev, "clock-output-names",
1623 					&pdata->dai_clks_name))
1624 		dev_warn(dev, "Using default clk name: %s\n",
1625 			 pdata->dai_clks_name);
1626 
1627 	if (device_property_read_u32(dev, "dlg,micbias-lvl", &of_val32) >= 0)
1628 		pdata->micbias_lvl = da7219_fw_micbias_lvl(dev, of_val32);
1629 	else
1630 		pdata->micbias_lvl = DA7219_MICBIAS_2_2V;
1631 
1632 	if (!device_property_read_string(dev, "dlg,mic-amp-in-sel", &of_str))
1633 		pdata->mic_amp_in_sel = da7219_fw_mic_amp_in_sel(dev, of_str);
1634 	else
1635 		pdata->mic_amp_in_sel = DA7219_MIC_AMP_IN_SEL_DIFF;
1636 
1637 	return pdata;
1638 }
1639 
1640 
1641 /*
1642  * Codec driver functions
1643  */
1644 
1645 static int da7219_set_bias_level(struct snd_soc_component *component,
1646 				 enum snd_soc_bias_level level)
1647 {
1648 	struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
1649 	int ret;
1650 
1651 	switch (level) {
1652 	case SND_SOC_BIAS_ON:
1653 		break;
1654 	case SND_SOC_BIAS_PREPARE:
1655 		/* Enable MCLK for transition to ON state */
1656 		if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_STANDBY) {
1657 			if (da7219->mclk) {
1658 				ret = clk_prepare_enable(da7219->mclk);
1659 				if (ret) {
1660 					dev_err(component->dev,
1661 						"Failed to enable mclk\n");
1662 					return ret;
1663 				}
1664 			}
1665 		}
1666 
1667 		break;
1668 	case SND_SOC_BIAS_STANDBY:
1669 		if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF)
1670 			/* Master bias */
1671 			snd_soc_component_update_bits(component, DA7219_REFERENCES,
1672 					    DA7219_BIAS_EN_MASK,
1673 					    DA7219_BIAS_EN_MASK);
1674 
1675 		if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_PREPARE) {
1676 			/* Remove MCLK */
1677 			if (da7219->mclk)
1678 				clk_disable_unprepare(da7219->mclk);
1679 		}
1680 		break;
1681 	case SND_SOC_BIAS_OFF:
1682 		/* Only disable master bias if we're not a wake-up source */
1683 		if (!da7219->wakeup_source)
1684 			snd_soc_component_update_bits(component, DA7219_REFERENCES,
1685 					    DA7219_BIAS_EN_MASK, 0);
1686 
1687 		break;
1688 	}
1689 
1690 	return 0;
1691 }
1692 
1693 static const char *da7219_supply_names[DA7219_NUM_SUPPLIES] = {
1694 	[DA7219_SUPPLY_VDD] = "VDD",
1695 	[DA7219_SUPPLY_VDDMIC] = "VDDMIC",
1696 	[DA7219_SUPPLY_VDDIO] = "VDDIO",
1697 };
1698 
1699 static int da7219_handle_supplies(struct snd_soc_component *component)
1700 {
1701 	struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
1702 	struct regulator *vddio;
1703 	u8 io_voltage_lvl = DA7219_IO_VOLTAGE_LEVEL_2_5V_3_6V;
1704 	int i, ret;
1705 
1706 	/* Get required supplies */
1707 	for (i = 0; i < DA7219_NUM_SUPPLIES; ++i)
1708 		da7219->supplies[i].supply = da7219_supply_names[i];
1709 
1710 	ret = devm_regulator_bulk_get(component->dev, DA7219_NUM_SUPPLIES,
1711 				      da7219->supplies);
1712 	if (ret) {
1713 		dev_err(component->dev, "Failed to get supplies");
1714 		return ret;
1715 	}
1716 
1717 	/* Determine VDDIO voltage provided */
1718 	vddio = da7219->supplies[DA7219_SUPPLY_VDDIO].consumer;
1719 	ret = regulator_get_voltage(vddio);
1720 	if (ret < 1200000)
1721 		dev_warn(component->dev, "Invalid VDDIO voltage\n");
1722 	else if (ret < 2800000)
1723 		io_voltage_lvl = DA7219_IO_VOLTAGE_LEVEL_1_2V_2_8V;
1724 
1725 	/* Enable main supplies */
1726 	ret = regulator_bulk_enable(DA7219_NUM_SUPPLIES, da7219->supplies);
1727 	if (ret) {
1728 		dev_err(component->dev, "Failed to enable supplies");
1729 		return ret;
1730 	}
1731 
1732 	/* Ensure device in active mode */
1733 	snd_soc_component_write(component, DA7219_SYSTEM_ACTIVE, DA7219_SYSTEM_ACTIVE_MASK);
1734 
1735 	/* Update IO voltage level range */
1736 	snd_soc_component_write(component, DA7219_IO_CTRL, io_voltage_lvl);
1737 
1738 	return 0;
1739 }
1740 
1741 #ifdef CONFIG_COMMON_CLK
1742 static int da7219_dai_clks_prepare(struct clk_hw *hw)
1743 {
1744 	struct da7219_priv *da7219 =
1745 		container_of(hw, struct da7219_priv, dai_clks_hw);
1746 	struct snd_soc_component *component = da7219->aad->component;
1747 
1748 	snd_soc_component_update_bits(component, DA7219_DAI_CLK_MODE,
1749 				      DA7219_DAI_CLK_EN_MASK,
1750 				      DA7219_DAI_CLK_EN_MASK);
1751 
1752 	return 0;
1753 }
1754 
1755 static void da7219_dai_clks_unprepare(struct clk_hw *hw)
1756 {
1757 	struct da7219_priv *da7219 =
1758 		container_of(hw, struct da7219_priv, dai_clks_hw);
1759 	struct snd_soc_component *component = da7219->aad->component;
1760 
1761 	snd_soc_component_update_bits(component, DA7219_DAI_CLK_MODE,
1762 				      DA7219_DAI_CLK_EN_MASK, 0);
1763 }
1764 
1765 static int da7219_dai_clks_is_prepared(struct clk_hw *hw)
1766 {
1767 	struct da7219_priv *da7219 =
1768 		container_of(hw, struct da7219_priv, dai_clks_hw);
1769 	struct snd_soc_component *component = da7219->aad->component;
1770 	u8 clk_reg;
1771 
1772 	clk_reg = snd_soc_component_read32(component, DA7219_DAI_CLK_MODE);
1773 
1774 	return !!(clk_reg & DA7219_DAI_CLK_EN_MASK);
1775 }
1776 
1777 static const struct clk_ops da7219_dai_clks_ops = {
1778 	.prepare = da7219_dai_clks_prepare,
1779 	.unprepare = da7219_dai_clks_unprepare,
1780 	.is_prepared = da7219_dai_clks_is_prepared,
1781 };
1782 
1783 static void da7219_register_dai_clks(struct snd_soc_component *component)
1784 {
1785 	struct device *dev = component->dev;
1786 	struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
1787 	struct da7219_pdata *pdata = da7219->pdata;
1788 	struct clk_init_data init = {};
1789 	struct clk *dai_clks;
1790 	struct clk_lookup *dai_clks_lookup;
1791 
1792 	init.parent_names = NULL;
1793 	init.num_parents = 0;
1794 	init.name = pdata->dai_clks_name;
1795 	init.ops = &da7219_dai_clks_ops;
1796 	da7219->dai_clks_hw.init = &init;
1797 
1798 	dai_clks = devm_clk_register(dev, &da7219->dai_clks_hw);
1799 	if (IS_ERR(dai_clks)) {
1800 		dev_warn(dev, "Failed to register DAI clocks: %ld\n",
1801 			 PTR_ERR(dai_clks));
1802 		return;
1803 	}
1804 	da7219->dai_clks = dai_clks;
1805 
1806 	/* If we're using DT, then register as provider accordingly */
1807 	if (dev->of_node) {
1808 		devm_of_clk_add_hw_provider(dev, of_clk_hw_simple_get,
1809 					    &da7219->dai_clks_hw);
1810 	} else {
1811 		dai_clks_lookup = clkdev_create(dai_clks, pdata->dai_clks_name,
1812 						"%s", dev_name(dev));
1813 		if (!dai_clks_lookup)
1814 			dev_warn(dev, "Failed to create DAI clkdev");
1815 		else
1816 			da7219->dai_clks_lookup = dai_clks_lookup;
1817 	}
1818 }
1819 #else
1820 static inline void da7219_register_dai_clks(struct snd_soc_component *component) {}
1821 #endif /* CONFIG_COMMON_CLK */
1822 
1823 static void da7219_handle_pdata(struct snd_soc_component *component)
1824 {
1825 	struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
1826 	struct da7219_pdata *pdata = da7219->pdata;
1827 
1828 	if (pdata) {
1829 		u8 micbias_lvl = 0;
1830 
1831 		da7219->wakeup_source = pdata->wakeup_source;
1832 
1833 		da7219_register_dai_clks(component);
1834 
1835 		/* Mic Bias voltages */
1836 		switch (pdata->micbias_lvl) {
1837 		case DA7219_MICBIAS_1_6V:
1838 		case DA7219_MICBIAS_1_8V:
1839 		case DA7219_MICBIAS_2_0V:
1840 		case DA7219_MICBIAS_2_2V:
1841 		case DA7219_MICBIAS_2_4V:
1842 		case DA7219_MICBIAS_2_6V:
1843 			micbias_lvl |= (pdata->micbias_lvl <<
1844 					DA7219_MICBIAS1_LEVEL_SHIFT);
1845 			break;
1846 		}
1847 
1848 		snd_soc_component_write(component, DA7219_MICBIAS_CTRL, micbias_lvl);
1849 
1850 		/* Mic */
1851 		switch (pdata->mic_amp_in_sel) {
1852 		case DA7219_MIC_AMP_IN_SEL_DIFF:
1853 		case DA7219_MIC_AMP_IN_SEL_SE_P:
1854 		case DA7219_MIC_AMP_IN_SEL_SE_N:
1855 			snd_soc_component_write(component, DA7219_MIC_1_SELECT,
1856 				      pdata->mic_amp_in_sel);
1857 			break;
1858 		}
1859 	}
1860 }
1861 
1862 static struct reg_sequence da7219_rev_aa_patch[] = {
1863 	{ DA7219_REFERENCES, 0x08 },
1864 };
1865 
1866 static int da7219_probe(struct snd_soc_component *component)
1867 {
1868 	struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
1869 	unsigned int rev;
1870 	int ret;
1871 
1872 	mutex_init(&da7219->ctrl_lock);
1873 	mutex_init(&da7219->pll_lock);
1874 
1875 	/* Regulator configuration */
1876 	ret = da7219_handle_supplies(component);
1877 	if (ret)
1878 		return ret;
1879 
1880 	ret = regmap_read(da7219->regmap, DA7219_CHIP_REVISION, &rev);
1881 	if (ret) {
1882 		dev_err(component->dev, "Failed to read chip revision: %d\n", ret);
1883 		goto err_disable_reg;
1884 	}
1885 
1886 	switch (rev & DA7219_CHIP_MINOR_MASK) {
1887 	case 0:
1888 		ret = regmap_register_patch(da7219->regmap, da7219_rev_aa_patch,
1889 					    ARRAY_SIZE(da7219_rev_aa_patch));
1890 		if (ret) {
1891 			dev_err(component->dev, "Failed to register AA patch: %d\n",
1892 				ret);
1893 			goto err_disable_reg;
1894 		}
1895 		break;
1896 	default:
1897 		break;
1898 	}
1899 
1900 	/* Handle DT/ACPI/Platform data */
1901 	da7219->pdata = dev_get_platdata(component->dev);
1902 	if (!da7219->pdata)
1903 		da7219->pdata = da7219_fw_to_pdata(component);
1904 
1905 	da7219_handle_pdata(component);
1906 
1907 	/* Check if MCLK provided */
1908 	da7219->mclk = devm_clk_get(component->dev, "mclk");
1909 	if (IS_ERR(da7219->mclk)) {
1910 		if (PTR_ERR(da7219->mclk) != -ENOENT) {
1911 			ret = PTR_ERR(da7219->mclk);
1912 			goto err_disable_reg;
1913 		} else {
1914 			da7219->mclk = NULL;
1915 		}
1916 	}
1917 
1918 	/* Default PC counter to free-running */
1919 	snd_soc_component_update_bits(component, DA7219_PC_COUNT, DA7219_PC_FREERUN_MASK,
1920 			    DA7219_PC_FREERUN_MASK);
1921 
1922 	/* Default gain ramping */
1923 	snd_soc_component_update_bits(component, DA7219_MIXIN_L_CTRL,
1924 			    DA7219_MIXIN_L_AMP_RAMP_EN_MASK,
1925 			    DA7219_MIXIN_L_AMP_RAMP_EN_MASK);
1926 	snd_soc_component_update_bits(component, DA7219_ADC_L_CTRL, DA7219_ADC_L_RAMP_EN_MASK,
1927 			    DA7219_ADC_L_RAMP_EN_MASK);
1928 	snd_soc_component_update_bits(component, DA7219_DAC_L_CTRL, DA7219_DAC_L_RAMP_EN_MASK,
1929 			    DA7219_DAC_L_RAMP_EN_MASK);
1930 	snd_soc_component_update_bits(component, DA7219_DAC_R_CTRL, DA7219_DAC_R_RAMP_EN_MASK,
1931 			    DA7219_DAC_R_RAMP_EN_MASK);
1932 	snd_soc_component_update_bits(component, DA7219_HP_L_CTRL,
1933 			    DA7219_HP_L_AMP_RAMP_EN_MASK,
1934 			    DA7219_HP_L_AMP_RAMP_EN_MASK);
1935 	snd_soc_component_update_bits(component, DA7219_HP_R_CTRL,
1936 			    DA7219_HP_R_AMP_RAMP_EN_MASK,
1937 			    DA7219_HP_R_AMP_RAMP_EN_MASK);
1938 
1939 	/* Default minimum gain on HP to avoid pops during DAPM sequencing */
1940 	snd_soc_component_update_bits(component, DA7219_HP_L_CTRL,
1941 			    DA7219_HP_L_AMP_MIN_GAIN_EN_MASK,
1942 			    DA7219_HP_L_AMP_MIN_GAIN_EN_MASK);
1943 	snd_soc_component_update_bits(component, DA7219_HP_R_CTRL,
1944 			    DA7219_HP_R_AMP_MIN_GAIN_EN_MASK,
1945 			    DA7219_HP_R_AMP_MIN_GAIN_EN_MASK);
1946 
1947 	/* Default infinite tone gen, start/stop by Kcontrol */
1948 	snd_soc_component_write(component, DA7219_TONE_GEN_CYCLES, DA7219_BEEP_CYCLES_MASK);
1949 
1950 	/* Initialise AAD block */
1951 	ret = da7219_aad_init(component);
1952 	if (ret)
1953 		goto err_disable_reg;
1954 
1955 	return 0;
1956 
1957 err_disable_reg:
1958 	regulator_bulk_disable(DA7219_NUM_SUPPLIES, da7219->supplies);
1959 
1960 	return ret;
1961 }
1962 
1963 static void da7219_remove(struct snd_soc_component *component)
1964 {
1965 	struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
1966 
1967 	da7219_aad_exit(component);
1968 
1969 #ifdef CONFIG_COMMON_CLK
1970 	if (da7219->dai_clks_lookup)
1971 		clkdev_drop(da7219->dai_clks_lookup);
1972 #endif
1973 
1974 	/* Supplies */
1975 	regulator_bulk_disable(DA7219_NUM_SUPPLIES, da7219->supplies);
1976 }
1977 
1978 #ifdef CONFIG_PM
1979 static int da7219_suspend(struct snd_soc_component *component)
1980 {
1981 	struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
1982 
1983 	/* Suspend AAD if we're not a wake-up source */
1984 	if (!da7219->wakeup_source)
1985 		da7219_aad_suspend(component);
1986 
1987 	snd_soc_component_force_bias_level(component, SND_SOC_BIAS_OFF);
1988 
1989 	return 0;
1990 }
1991 
1992 static int da7219_resume(struct snd_soc_component *component)
1993 {
1994 	struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
1995 
1996 	snd_soc_component_force_bias_level(component, SND_SOC_BIAS_STANDBY);
1997 
1998 	/* Resume AAD if previously suspended */
1999 	if (!da7219->wakeup_source)
2000 		da7219_aad_resume(component);
2001 
2002 	return 0;
2003 }
2004 #else
2005 #define da7219_suspend NULL
2006 #define da7219_resume NULL
2007 #endif
2008 
2009 static const struct snd_soc_component_driver soc_component_dev_da7219 = {
2010 	.probe			= da7219_probe,
2011 	.remove			= da7219_remove,
2012 	.suspend		= da7219_suspend,
2013 	.resume			= da7219_resume,
2014 	.set_bias_level		= da7219_set_bias_level,
2015 	.controls		= da7219_snd_controls,
2016 	.num_controls		= ARRAY_SIZE(da7219_snd_controls),
2017 	.dapm_widgets		= da7219_dapm_widgets,
2018 	.num_dapm_widgets	= ARRAY_SIZE(da7219_dapm_widgets),
2019 	.dapm_routes		= da7219_audio_map,
2020 	.num_dapm_routes	= ARRAY_SIZE(da7219_audio_map),
2021 	.idle_bias_on		= 1,
2022 	.use_pmdown_time	= 1,
2023 	.endianness		= 1,
2024 	.non_legacy_dai_naming	= 1,
2025 };
2026 
2027 
2028 /*
2029  * Regmap configs
2030  */
2031 
2032 static struct reg_default da7219_reg_defaults[] = {
2033 	{ DA7219_MIC_1_SELECT, 0x00 },
2034 	{ DA7219_CIF_TIMEOUT_CTRL, 0x01 },
2035 	{ DA7219_SR_24_48, 0x00 },
2036 	{ DA7219_SR, 0x0A },
2037 	{ DA7219_CIF_I2C_ADDR_CFG, 0x02 },
2038 	{ DA7219_PLL_CTRL, 0x10 },
2039 	{ DA7219_PLL_FRAC_TOP, 0x00 },
2040 	{ DA7219_PLL_FRAC_BOT, 0x00 },
2041 	{ DA7219_PLL_INTEGER, 0x20 },
2042 	{ DA7219_DIG_ROUTING_DAI, 0x10 },
2043 	{ DA7219_DAI_CLK_MODE, 0x01 },
2044 	{ DA7219_DAI_CTRL, 0x28 },
2045 	{ DA7219_DAI_TDM_CTRL, 0x40 },
2046 	{ DA7219_DIG_ROUTING_DAC, 0x32 },
2047 	{ DA7219_DAI_OFFSET_LOWER, 0x00 },
2048 	{ DA7219_DAI_OFFSET_UPPER, 0x00 },
2049 	{ DA7219_REFERENCES, 0x08 },
2050 	{ DA7219_MIXIN_L_SELECT, 0x00 },
2051 	{ DA7219_MIXIN_L_GAIN, 0x03 },
2052 	{ DA7219_ADC_L_GAIN, 0x6F },
2053 	{ DA7219_ADC_FILTERS1, 0x80 },
2054 	{ DA7219_MIC_1_GAIN, 0x01 },
2055 	{ DA7219_SIDETONE_CTRL, 0x40 },
2056 	{ DA7219_SIDETONE_GAIN, 0x0E },
2057 	{ DA7219_DROUTING_ST_OUTFILT_1L, 0x01 },
2058 	{ DA7219_DROUTING_ST_OUTFILT_1R, 0x02 },
2059 	{ DA7219_DAC_FILTERS5, 0x00 },
2060 	{ DA7219_DAC_FILTERS2, 0x88 },
2061 	{ DA7219_DAC_FILTERS3, 0x88 },
2062 	{ DA7219_DAC_FILTERS4, 0x08 },
2063 	{ DA7219_DAC_FILTERS1, 0x80 },
2064 	{ DA7219_DAC_L_GAIN, 0x6F },
2065 	{ DA7219_DAC_R_GAIN, 0x6F },
2066 	{ DA7219_CP_CTRL, 0x20 },
2067 	{ DA7219_HP_L_GAIN, 0x39 },
2068 	{ DA7219_HP_R_GAIN, 0x39 },
2069 	{ DA7219_MIXOUT_L_SELECT, 0x00 },
2070 	{ DA7219_MIXOUT_R_SELECT, 0x00 },
2071 	{ DA7219_MICBIAS_CTRL, 0x03 },
2072 	{ DA7219_MIC_1_CTRL, 0x40 },
2073 	{ DA7219_MIXIN_L_CTRL, 0x40 },
2074 	{ DA7219_ADC_L_CTRL, 0x40 },
2075 	{ DA7219_DAC_L_CTRL, 0x40 },
2076 	{ DA7219_DAC_R_CTRL, 0x40 },
2077 	{ DA7219_HP_L_CTRL, 0x40 },
2078 	{ DA7219_HP_R_CTRL, 0x40 },
2079 	{ DA7219_MIXOUT_L_CTRL, 0x10 },
2080 	{ DA7219_MIXOUT_R_CTRL, 0x10 },
2081 	{ DA7219_CHIP_ID1, 0x23 },
2082 	{ DA7219_CHIP_ID2, 0x93 },
2083 	{ DA7219_IO_CTRL, 0x00 },
2084 	{ DA7219_GAIN_RAMP_CTRL, 0x00 },
2085 	{ DA7219_PC_COUNT, 0x02 },
2086 	{ DA7219_CP_VOL_THRESHOLD1, 0x0E },
2087 	{ DA7219_DIG_CTRL, 0x00 },
2088 	{ DA7219_ALC_CTRL2, 0x00 },
2089 	{ DA7219_ALC_CTRL3, 0x00 },
2090 	{ DA7219_ALC_NOISE, 0x3F },
2091 	{ DA7219_ALC_TARGET_MIN, 0x3F },
2092 	{ DA7219_ALC_TARGET_MAX, 0x00 },
2093 	{ DA7219_ALC_GAIN_LIMITS, 0xFF },
2094 	{ DA7219_ALC_ANA_GAIN_LIMITS, 0x71 },
2095 	{ DA7219_ALC_ANTICLIP_CTRL, 0x00 },
2096 	{ DA7219_ALC_ANTICLIP_LEVEL, 0x00 },
2097 	{ DA7219_DAC_NG_SETUP_TIME, 0x00 },
2098 	{ DA7219_DAC_NG_OFF_THRESH, 0x00 },
2099 	{ DA7219_DAC_NG_ON_THRESH, 0x00 },
2100 	{ DA7219_DAC_NG_CTRL, 0x00 },
2101 	{ DA7219_TONE_GEN_CFG1, 0x00 },
2102 	{ DA7219_TONE_GEN_CFG2, 0x00 },
2103 	{ DA7219_TONE_GEN_CYCLES, 0x00 },
2104 	{ DA7219_TONE_GEN_FREQ1_L, 0x55 },
2105 	{ DA7219_TONE_GEN_FREQ1_U, 0x15 },
2106 	{ DA7219_TONE_GEN_FREQ2_L, 0x00 },
2107 	{ DA7219_TONE_GEN_FREQ2_U, 0x40 },
2108 	{ DA7219_TONE_GEN_ON_PER, 0x02 },
2109 	{ DA7219_TONE_GEN_OFF_PER, 0x01 },
2110 	{ DA7219_ACCDET_IRQ_MASK_A, 0x00 },
2111 	{ DA7219_ACCDET_IRQ_MASK_B, 0x00 },
2112 	{ DA7219_ACCDET_CONFIG_1, 0xD6 },
2113 	{ DA7219_ACCDET_CONFIG_2, 0x34 },
2114 	{ DA7219_ACCDET_CONFIG_3, 0x0A },
2115 	{ DA7219_ACCDET_CONFIG_4, 0x16 },
2116 	{ DA7219_ACCDET_CONFIG_5, 0x21 },
2117 	{ DA7219_ACCDET_CONFIG_6, 0x3E },
2118 	{ DA7219_ACCDET_CONFIG_7, 0x01 },
2119 	{ DA7219_SYSTEM_ACTIVE, 0x00 },
2120 };
2121 
2122 static bool da7219_volatile_register(struct device *dev, unsigned int reg)
2123 {
2124 	switch (reg) {
2125 	case DA7219_MIC_1_GAIN_STATUS:
2126 	case DA7219_MIXIN_L_GAIN_STATUS:
2127 	case DA7219_ADC_L_GAIN_STATUS:
2128 	case DA7219_DAC_L_GAIN_STATUS:
2129 	case DA7219_DAC_R_GAIN_STATUS:
2130 	case DA7219_HP_L_GAIN_STATUS:
2131 	case DA7219_HP_R_GAIN_STATUS:
2132 	case DA7219_CIF_CTRL:
2133 	case DA7219_PLL_SRM_STS:
2134 	case DA7219_ALC_CTRL1:
2135 	case DA7219_SYSTEM_MODES_INPUT:
2136 	case DA7219_SYSTEM_MODES_OUTPUT:
2137 	case DA7219_ALC_OFFSET_AUTO_M_L:
2138 	case DA7219_ALC_OFFSET_AUTO_U_L:
2139 	case DA7219_TONE_GEN_CFG1:
2140 	case DA7219_ACCDET_STATUS_A:
2141 	case DA7219_ACCDET_STATUS_B:
2142 	case DA7219_ACCDET_IRQ_EVENT_A:
2143 	case DA7219_ACCDET_IRQ_EVENT_B:
2144 	case DA7219_ACCDET_CONFIG_8:
2145 	case DA7219_SYSTEM_STATUS:
2146 		return 1;
2147 	default:
2148 		return 0;
2149 	}
2150 }
2151 
2152 static const struct regmap_config da7219_regmap_config = {
2153 	.reg_bits = 8,
2154 	.val_bits = 8,
2155 
2156 	.max_register = DA7219_SYSTEM_ACTIVE,
2157 	.reg_defaults = da7219_reg_defaults,
2158 	.num_reg_defaults = ARRAY_SIZE(da7219_reg_defaults),
2159 	.volatile_reg = da7219_volatile_register,
2160 	.cache_type = REGCACHE_RBTREE,
2161 };
2162 
2163 
2164 /*
2165  * I2C layer
2166  */
2167 
2168 static int da7219_i2c_probe(struct i2c_client *i2c,
2169 			    const struct i2c_device_id *id)
2170 {
2171 	struct da7219_priv *da7219;
2172 	unsigned int system_active, system_status;
2173 	int i, ret;
2174 
2175 	da7219 = devm_kzalloc(&i2c->dev, sizeof(struct da7219_priv),
2176 			      GFP_KERNEL);
2177 	if (!da7219)
2178 		return -ENOMEM;
2179 
2180 	i2c_set_clientdata(i2c, da7219);
2181 
2182 	da7219->regmap = devm_regmap_init_i2c(i2c, &da7219_regmap_config);
2183 	if (IS_ERR(da7219->regmap)) {
2184 		ret = PTR_ERR(da7219->regmap);
2185 		dev_err(&i2c->dev, "regmap_init() failed: %d\n", ret);
2186 		return ret;
2187 	}
2188 
2189 	regcache_cache_bypass(da7219->regmap, true);
2190 
2191 	/* Disable audio paths if still active from previous start */
2192 	regmap_read(da7219->regmap, DA7219_SYSTEM_ACTIVE, &system_active);
2193 	if (system_active) {
2194 		regmap_write(da7219->regmap, DA7219_GAIN_RAMP_CTRL,
2195 			     DA7219_GAIN_RAMP_RATE_NOMINAL);
2196 		regmap_write(da7219->regmap, DA7219_SYSTEM_MODES_INPUT, 0x00);
2197 		regmap_write(da7219->regmap, DA7219_SYSTEM_MODES_OUTPUT, 0x01);
2198 
2199 		for (i = 0; i < DA7219_SYS_STAT_CHECK_RETRIES; ++i) {
2200 			regmap_read(da7219->regmap, DA7219_SYSTEM_STATUS,
2201 				    &system_status);
2202 			if (!system_status)
2203 				break;
2204 
2205 			msleep(DA7219_SYS_STAT_CHECK_DELAY);
2206 		}
2207 	}
2208 
2209 	/* Soft reset component */
2210 	regmap_write_bits(da7219->regmap, DA7219_ACCDET_CONFIG_1,
2211 			  DA7219_ACCDET_EN_MASK, 0);
2212 	regmap_write_bits(da7219->regmap, DA7219_CIF_CTRL,
2213 			  DA7219_CIF_REG_SOFT_RESET_MASK,
2214 			  DA7219_CIF_REG_SOFT_RESET_MASK);
2215 	regmap_write_bits(da7219->regmap, DA7219_SYSTEM_ACTIVE,
2216 			  DA7219_SYSTEM_ACTIVE_MASK, 0);
2217 
2218 	regcache_cache_bypass(da7219->regmap, false);
2219 
2220 	ret = devm_snd_soc_register_component(&i2c->dev,
2221 				     &soc_component_dev_da7219,
2222 				     &da7219_dai, 1);
2223 	if (ret < 0) {
2224 		dev_err(&i2c->dev, "Failed to register da7219 component: %d\n",
2225 			ret);
2226 	}
2227 	return ret;
2228 }
2229 
2230 static int da7219_i2c_remove(struct i2c_client *client)
2231 {
2232 	return 0;
2233 }
2234 
2235 static const struct i2c_device_id da7219_i2c_id[] = {
2236 	{ "da7219", },
2237 	{ }
2238 };
2239 MODULE_DEVICE_TABLE(i2c, da7219_i2c_id);
2240 
2241 static struct i2c_driver da7219_i2c_driver = {
2242 	.driver = {
2243 		.name = "da7219",
2244 		.of_match_table = of_match_ptr(da7219_of_match),
2245 		.acpi_match_table = ACPI_PTR(da7219_acpi_match),
2246 	},
2247 	.probe		= da7219_i2c_probe,
2248 	.remove		= da7219_i2c_remove,
2249 	.id_table	= da7219_i2c_id,
2250 };
2251 
2252 module_i2c_driver(da7219_i2c_driver);
2253 
2254 MODULE_DESCRIPTION("ASoC DA7219 Codec Driver");
2255 MODULE_AUTHOR("Adam Thomson <Adam.Thomson.Opensource@diasemi.com>");
2256 MODULE_LICENSE("GPL");
2257