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