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