blob: 2610fc5cff686e413ea9aac87519a02e6e97d5c4 [file] [log] [blame]
Adam Thomson6d817c02015-09-29 16:44:01 +01001/*
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
Mark Brownb58cea72016-05-06 18:13:17 +010014#include <linux/acpi.h>
Adam Thomson6d817c02015-09-29 16:44:01 +010015#include <linux/clk.h>
16#include <linux/i2c.h>
17#include <linux/of_device.h>
Adam Thomsona01b8932016-06-21 18:50:21 +010018#include <linux/property.h>
Adam Thomson6d817c02015-09-29 16:44:01 +010019#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 */
43static const DECLARE_TLV_DB_SCALE(da7219_mic_gain_tlv, -600, 600, 0);
44static const DECLARE_TLV_DB_SCALE(da7219_mixin_gain_tlv, -450, 150, 0);
45static const DECLARE_TLV_DB_SCALE(da7219_adc_dig_gain_tlv, -8325, 75, 0);
46static const DECLARE_TLV_DB_SCALE(da7219_alc_threshold_tlv, -9450, 150, 0);
47static const DECLARE_TLV_DB_SCALE(da7219_alc_gain_tlv, 0, 600, 0);
48static const DECLARE_TLV_DB_SCALE(da7219_alc_ana_gain_tlv, 0, 600, 0);
49static const DECLARE_TLV_DB_SCALE(da7219_sidetone_gain_tlv, -4200, 300, 0);
50static const DECLARE_TLV_DB_SCALE(da7219_tonegen_gain_tlv, -4500, 300, 0);
51
52/* Output TLVs */
53static const DECLARE_TLV_DB_SCALE(da7219_dac_eq_band_tlv, -1050, 150, 0);
54
55static 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
61static const DECLARE_TLV_DB_SCALE(da7219_dac_ng_threshold_tlv, -10200, 600, 0);
62static const DECLARE_TLV_DB_SCALE(da7219_hp_gain_tlv, -5700, 100, 0);
63
64/* Input Enums */
65static 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
71static 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
75static 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
80static 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
84static 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
90static 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
94static const char * const da7219_alc_env_rate_txt[] = {
95 "1/4", "1/16", "1/256", "1/65536"
96};
97
98static 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
102static 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
106static const char * const da7219_alc_anticlip_step_txt[] = {
107 "0.034dB/fs", "0.068dB/fs", "0.136dB/fs", "0.272dB/fs"
108};
109
110static 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 */
117static const char * const da7219_gain_ramp_rate_txt[] = {
118 "Nominal Rate * 8", "Nominal Rate", "Nominal Rate / 8",
119 "Nominal Rate / 16"
120};
121
122static 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
126static const char * const da7219_hpf_mode_txt[] = {
127 "Disabled", "Audio", "Voice"
128};
129
130static const unsigned int da7219_hpf_mode_val[] = {
131 DA7219_HPF_DISABLED, DA7219_HPF_AUDIO_EN, DA7219_HPF_VOICE_EN,
132};
133
134static 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
139static 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
144static const char * const da7219_audio_hpf_corner_txt[] = {
145 "2Hz", "4Hz", "8Hz", "16Hz"
146};
147
148static 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
154static 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
160static const char * const da7219_voice_hpf_corner_txt[] = {
161 "2.5Hz", "25Hz", "50Hz", "100Hz", "150Hz", "200Hz", "300Hz", "400Hz"
162};
163
164static 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
170static 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
176static 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
181static 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
185static const char * const da7219_tonegen_swg_sel_txt[] = {
186 "Sum", "SWG1", "SWG2", "SWG1_1-Cos"
187};
188
189static 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 */
194static 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
199static 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
204static const char * const da7219_dac_ng_setup_time_txt[] = {
205 "256 Samples", "512 Samples", "1024 Samples", "2048 Samples"
206};
207
208static 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
214static const char * const da7219_dac_ng_rampup_txt[] = {
215 "0.22ms/dB", "0.0138ms/dB"
216};
217
218static 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
224static const char * const da7219_dac_ng_rampdown_txt[] = {
225 "0.88ms/dB", "14.08ms/dB"
226};
227
228static 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
235static const char * const da7219_cp_track_mode_txt[] = {
236 "Largest Volume", "DAC Volume", "Signal Magnitude"
237};
238
239static 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
244static 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 */
256static 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
270static 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
284static 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
298static 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 */
313static 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
378static 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
397static 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);
Axel Linc5cff892015-10-21 08:34:37 +0800407 da7219->alc_en = true;
Adam Thomson6d817c02015-09-29 16:44:01 +0100408 } else {
409 da7219->alc_en = false;
410 }
411
412 return snd_soc_put_volsw(kcontrol, ucontrol);
413}
414
415/* ToneGen */
416static 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
443static 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
473static 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
682static const char * const da7219_out_sel_txt[] = {
683 "ADC", "Tone Generator", "DAIL", "DAIR"
684};
685
686static 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
692static const struct snd_kcontrol_new da7219_out_dail_sel_mux =
693 SOC_DAPM_ENUM("Out DAIL Mux", da7219_out_dail_sel);
694
695static 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
701static const struct snd_kcontrol_new da7219_out_dair_sel_mux =
702 SOC_DAPM_ENUM("Out DAIR Mux", da7219_out_dair_sel);
703
704static 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
710static const struct snd_kcontrol_new da7219_out_dacl_sel_mux =
711 SOC_DAPM_ENUM("Out DACL Mux", da7219_out_dacl_sel);
712
713static 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
719static 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
727static 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
733static 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
739static 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
756static const struct snd_kcontrol_new da7219_st_out_filtl_mix_controls[] = {
757 DA7219_DMIX_ST_CTRLS(DA7219_DROUTING_ST_OUTFILT_1L),
758};
759
760static 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
769static 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 }
Takashi Sakamotoa56a5692016-09-27 07:45:07 +0900804 } while ((i < DA7219_SRM_CHECK_RETRIES) && (!srm_lock));
Adam Thomson6d817c02015-09-29 16:44:01 +0100805
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
Adam Thomson86834512016-10-03 11:21:01 +0100826static int da7219_settling_event(struct snd_soc_dapm_widget *w,
827 struct snd_kcontrol *kcontrol, int event)
828{
829 switch (event) {
830 case SND_SOC_DAPM_POST_PMU:
831 case SND_SOC_DAPM_POST_PMD:
832 msleep(DA7219_SETTLING_DELAY);
833 break;
834 default:
835 break;
836 }
837
838 return 0;
839}
840
841static int da7219_mixout_event(struct snd_soc_dapm_widget *w,
842 struct snd_kcontrol *kcontrol, int event)
843{
844 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
845 u8 hp_ctrl, min_gain_mask;
846
847 switch (w->reg) {
848 case DA7219_MIXOUT_L_CTRL:
849 hp_ctrl = DA7219_HP_L_CTRL;
850 min_gain_mask = DA7219_HP_L_AMP_MIN_GAIN_EN_MASK;
851 break;
852 case DA7219_MIXOUT_R_CTRL:
853 hp_ctrl = DA7219_HP_R_CTRL;
854 min_gain_mask = DA7219_HP_R_AMP_MIN_GAIN_EN_MASK;
855 break;
856 default:
857 return -EINVAL;
858 }
859
860 switch (event) {
861 case SND_SOC_DAPM_PRE_PMD:
862 /* Enable minimum gain on HP to avoid pops */
863 snd_soc_update_bits(codec, hp_ctrl, min_gain_mask,
864 min_gain_mask);
865
866 msleep(DA7219_MIN_GAIN_DELAY);
867
868 break;
869 case SND_SOC_DAPM_POST_PMU:
870 /* Remove minimum gain on HP */
871 snd_soc_update_bits(codec, hp_ctrl, min_gain_mask, 0);
872
873 break;
874 }
875
876 return 0;
877}
878
879static int da7219_gain_ramp_event(struct snd_soc_dapm_widget *w,
880 struct snd_kcontrol *kcontrol, int event)
881{
882 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
883 struct da7219_priv *da7219 = snd_soc_codec_get_drvdata(codec);
884
885 switch (event) {
886 case SND_SOC_DAPM_PRE_PMU:
887 case SND_SOC_DAPM_PRE_PMD:
888 /* Ensure nominal gain ramping for DAPM sequence */
889 da7219->gain_ramp_ctrl =
890 snd_soc_read(codec, DA7219_GAIN_RAMP_CTRL);
891 snd_soc_write(codec, DA7219_GAIN_RAMP_CTRL,
892 DA7219_GAIN_RAMP_RATE_NOMINAL);
893 break;
894 case SND_SOC_DAPM_POST_PMU:
895 case SND_SOC_DAPM_POST_PMD:
896 /* Restore previous gain ramp settings */
897 snd_soc_write(codec, DA7219_GAIN_RAMP_CTRL,
898 da7219->gain_ramp_ctrl);
899 break;
900 }
901
902 return 0;
903}
904
Adam Thomson6d817c02015-09-29 16:44:01 +0100905
906/*
907 * DAPM Widgets
908 */
909
910static const struct snd_soc_dapm_widget da7219_dapm_widgets[] = {
911 /* Input Supplies */
912 SND_SOC_DAPM_SUPPLY("Mic Bias", DA7219_MICBIAS_CTRL,
913 DA7219_MICBIAS1_EN_SHIFT, DA7219_NO_INVERT,
914 NULL, 0),
915
916 /* Inputs */
917 SND_SOC_DAPM_INPUT("MIC"),
918
919 /* Input PGAs */
920 SND_SOC_DAPM_PGA("Mic PGA", DA7219_MIC_1_CTRL,
921 DA7219_MIC_1_AMP_EN_SHIFT, DA7219_NO_INVERT,
922 NULL, 0),
923 SND_SOC_DAPM_PGA("Mixin PGA", DA7219_MIXIN_L_CTRL,
924 DA7219_MIXIN_L_AMP_EN_SHIFT, DA7219_NO_INVERT,
925 NULL, 0),
926
927 /* Input Filters */
928 SND_SOC_DAPM_ADC("ADC", NULL, DA7219_ADC_L_CTRL, DA7219_ADC_L_EN_SHIFT,
929 DA7219_NO_INVERT),
930
931 /* Tone Generator */
932 SND_SOC_DAPM_SIGGEN("TONE"),
933 SND_SOC_DAPM_PGA("Tone Generator", DA7219_TONE_GEN_CFG1,
934 DA7219_START_STOPN_SHIFT, DA7219_NO_INVERT, NULL, 0),
935
936 /* Sidetone Input */
937 SND_SOC_DAPM_ADC("Sidetone Filter", NULL, DA7219_SIDETONE_CTRL,
938 DA7219_SIDETONE_EN_SHIFT, DA7219_NO_INVERT),
939
940 /* Input Mixer Supply */
941 SND_SOC_DAPM_SUPPLY("Mixer In Supply", DA7219_MIXIN_L_CTRL,
942 DA7219_MIXIN_L_MIX_EN_SHIFT, DA7219_NO_INVERT,
943 NULL, 0),
944
945 /* Input Mixer */
946 SND_SOC_DAPM_MIXER("Mixer In", SND_SOC_NOPM, 0, 0,
947 da7219_mixin_controls,
948 ARRAY_SIZE(da7219_mixin_controls)),
949
950 /* Input Muxes */
951 SND_SOC_DAPM_MUX("Out DAIL Mux", SND_SOC_NOPM, 0, 0,
952 &da7219_out_dail_sel_mux),
953 SND_SOC_DAPM_MUX("Out DAIR Mux", SND_SOC_NOPM, 0, 0,
954 &da7219_out_dair_sel_mux),
955
956 /* DAI Supply */
957 SND_SOC_DAPM_SUPPLY("DAI", DA7219_DAI_CTRL, DA7219_DAI_EN_SHIFT,
958 DA7219_NO_INVERT, da7219_dai_event,
959 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
960
961 /* DAI */
962 SND_SOC_DAPM_AIF_OUT("DAIOUT", "Capture", 0, SND_SOC_NOPM, 0, 0),
963 SND_SOC_DAPM_AIF_IN("DAIIN", "Playback", 0, SND_SOC_NOPM, 0, 0),
964
965 /* Output Muxes */
966 SND_SOC_DAPM_MUX("Out DACL Mux", SND_SOC_NOPM, 0, 0,
967 &da7219_out_dacl_sel_mux),
968 SND_SOC_DAPM_MUX("Out DACR Mux", SND_SOC_NOPM, 0, 0,
969 &da7219_out_dacr_sel_mux),
970
971 /* Output Mixers */
972 SND_SOC_DAPM_MIXER("Mixer Out FilterL", SND_SOC_NOPM, 0, 0,
973 da7219_mixout_l_controls,
974 ARRAY_SIZE(da7219_mixout_l_controls)),
975 SND_SOC_DAPM_MIXER("Mixer Out FilterR", SND_SOC_NOPM, 0, 0,
976 da7219_mixout_r_controls,
977 ARRAY_SIZE(da7219_mixout_r_controls)),
978
979 /* Sidetone Mixers */
980 SND_SOC_DAPM_MIXER("ST Mixer Out FilterL", SND_SOC_NOPM, 0, 0,
981 da7219_st_out_filtl_mix_controls,
982 ARRAY_SIZE(da7219_st_out_filtl_mix_controls)),
983 SND_SOC_DAPM_MIXER("ST Mixer Out FilterR", SND_SOC_NOPM, 0,
984 0, da7219_st_out_filtr_mix_controls,
985 ARRAY_SIZE(da7219_st_out_filtr_mix_controls)),
986
987 /* DACs */
Adam Thomson86834512016-10-03 11:21:01 +0100988 SND_SOC_DAPM_DAC_E("DACL", NULL, DA7219_DAC_L_CTRL,
989 DA7219_DAC_L_EN_SHIFT, DA7219_NO_INVERT,
990 da7219_settling_event,
991 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
992 SND_SOC_DAPM_DAC_E("DACR", NULL, DA7219_DAC_R_CTRL,
993 DA7219_DAC_R_EN_SHIFT, DA7219_NO_INVERT,
994 da7219_settling_event,
995 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
Adam Thomson6d817c02015-09-29 16:44:01 +0100996
997 /* Output PGAs */
Adam Thomson86834512016-10-03 11:21:01 +0100998 SND_SOC_DAPM_PGA_E("Mixout Left PGA", DA7219_MIXOUT_L_CTRL,
999 DA7219_MIXOUT_L_AMP_EN_SHIFT, DA7219_NO_INVERT,
1000 NULL, 0, da7219_mixout_event,
1001 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1002 SND_SOC_DAPM_PGA_E("Mixout Right PGA", DA7219_MIXOUT_R_CTRL,
1003 DA7219_MIXOUT_R_AMP_EN_SHIFT, DA7219_NO_INVERT,
1004 NULL, 0, da7219_mixout_event,
1005 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1006 SND_SOC_DAPM_SUPPLY_S("Headphone Left PGA", 1, DA7219_HP_L_CTRL,
1007 DA7219_HP_L_AMP_EN_SHIFT, DA7219_NO_INVERT,
1008 da7219_settling_event,
1009 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1010 SND_SOC_DAPM_SUPPLY_S("Headphone Right PGA", 1, DA7219_HP_R_CTRL,
1011 DA7219_HP_R_AMP_EN_SHIFT, DA7219_NO_INVERT,
1012 da7219_settling_event,
1013 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
Adam Thomson6d817c02015-09-29 16:44:01 +01001014
1015 /* Output Supplies */
Adam Thomson86834512016-10-03 11:21:01 +01001016 SND_SOC_DAPM_SUPPLY_S("Charge Pump", 0, DA7219_CP_CTRL,
1017 DA7219_CP_EN_SHIFT, DA7219_NO_INVERT,
1018 da7219_settling_event,
1019 SND_SOC_DAPM_POST_PMU),
Adam Thomson6d817c02015-09-29 16:44:01 +01001020
1021 /* Outputs */
1022 SND_SOC_DAPM_OUTPUT("HPL"),
1023 SND_SOC_DAPM_OUTPUT("HPR"),
Adam Thomson86834512016-10-03 11:21:01 +01001024
1025 /* Pre/Post Power */
1026 SND_SOC_DAPM_PRE("Pre Power Gain Ramp", da7219_gain_ramp_event),
1027 SND_SOC_DAPM_POST("Post Power Gain Ramp", da7219_gain_ramp_event),
Adam Thomson6d817c02015-09-29 16:44:01 +01001028};
1029
1030
1031/*
1032 * DAPM Mux Routes
1033 */
1034
1035#define DA7219_OUT_DAI_MUX_ROUTES(name) \
1036 {name, "ADC", "Mixer In"}, \
1037 {name, "Tone Generator", "Tone Generator"}, \
1038 {name, "DAIL", "DAIOUT"}, \
1039 {name, "DAIR", "DAIOUT"}
1040
1041#define DA7219_OUT_DAC_MUX_ROUTES(name) \
1042 {name, "ADC", "Mixer In"}, \
1043 {name, "Tone Generator", "Tone Generator"}, \
1044 {name, "DAIL", "DAIIN"}, \
1045 {name, "DAIR", "DAIIN"}
1046
1047/*
1048 * DAPM Mixer Routes
1049 */
1050
1051#define DA7219_DMIX_ST_ROUTES(name) \
1052 {name, "Out FilterL Switch", "Mixer Out FilterL"}, \
1053 {name, "Out FilterR Switch", "Mixer Out FilterR"}, \
1054 {name, "Sidetone Switch", "Sidetone Filter"}
1055
1056
1057/*
1058 * DAPM audio route definition
1059 */
1060
1061static const struct snd_soc_dapm_route da7219_audio_map[] = {
1062 /* Input paths */
1063 {"MIC", NULL, "Mic Bias"},
1064 {"Mic PGA", NULL, "MIC"},
1065 {"Mixin PGA", NULL, "Mic PGA"},
1066 {"ADC", NULL, "Mixin PGA"},
1067
Adam Thomson6d817c02015-09-29 16:44:01 +01001068 {"Mixer In", NULL, "Mixer In Supply"},
1069 {"Mixer In", "Mic Switch", "ADC"},
1070
Adam Thomsonfdd50a82015-12-22 18:27:52 +00001071 {"Sidetone Filter", NULL, "Mixer In"},
1072
Adam Thomson6d817c02015-09-29 16:44:01 +01001073 {"Tone Generator", NULL, "TONE"},
1074
1075 DA7219_OUT_DAI_MUX_ROUTES("Out DAIL Mux"),
1076 DA7219_OUT_DAI_MUX_ROUTES("Out DAIR Mux"),
1077
1078 {"DAIOUT", NULL, "Out DAIL Mux"},
1079 {"DAIOUT", NULL, "Out DAIR Mux"},
1080 {"DAIOUT", NULL, "DAI"},
1081
1082 /* Output paths */
1083 {"DAIIN", NULL, "DAI"},
1084
1085 DA7219_OUT_DAC_MUX_ROUTES("Out DACL Mux"),
1086 DA7219_OUT_DAC_MUX_ROUTES("Out DACR Mux"),
1087
1088 {"Mixer Out FilterL", "DACL Switch", "Out DACL Mux"},
1089 {"Mixer Out FilterR", "DACR Switch", "Out DACR Mux"},
1090
1091 DA7219_DMIX_ST_ROUTES("ST Mixer Out FilterL"),
1092 DA7219_DMIX_ST_ROUTES("ST Mixer Out FilterR"),
1093
1094 {"DACL", NULL, "ST Mixer Out FilterL"},
1095 {"DACR", NULL, "ST Mixer Out FilterR"},
1096
1097 {"Mixout Left PGA", NULL, "DACL"},
1098 {"Mixout Right PGA", NULL, "DACR"},
1099
Adam Thomson86834512016-10-03 11:21:01 +01001100 {"HPL", NULL, "Mixout Left PGA"},
1101 {"HPR", NULL, "Mixout Right PGA"},
Adam Thomson6d817c02015-09-29 16:44:01 +01001102
1103 {"HPL", NULL, "Headphone Left PGA"},
1104 {"HPR", NULL, "Headphone Right PGA"},
1105
1106 {"HPL", NULL, "Charge Pump"},
1107 {"HPR", NULL, "Charge Pump"},
1108};
1109
1110
1111/*
1112 * DAI operations
1113 */
1114
1115static int da7219_set_dai_sysclk(struct snd_soc_dai *codec_dai,
1116 int clk_id, unsigned int freq, int dir)
1117{
1118 struct snd_soc_codec *codec = codec_dai->codec;
1119 struct da7219_priv *da7219 = snd_soc_codec_get_drvdata(codec);
1120 int ret = 0;
1121
1122 if ((da7219->clk_src == clk_id) && (da7219->mclk_rate == freq))
1123 return 0;
1124
Adam Thomsonfb137ba2016-04-19 15:19:03 +01001125 if ((freq < 2000000) || (freq > 54000000)) {
Adam Thomson6d817c02015-09-29 16:44:01 +01001126 dev_err(codec_dai->dev, "Unsupported MCLK value %d\n",
1127 freq);
1128 return -EINVAL;
1129 }
1130
1131 switch (clk_id) {
1132 case DA7219_CLKSRC_MCLK_SQR:
1133 snd_soc_update_bits(codec, DA7219_PLL_CTRL,
1134 DA7219_PLL_MCLK_SQR_EN_MASK,
1135 DA7219_PLL_MCLK_SQR_EN_MASK);
1136 break;
1137 case DA7219_CLKSRC_MCLK:
1138 snd_soc_update_bits(codec, DA7219_PLL_CTRL,
1139 DA7219_PLL_MCLK_SQR_EN_MASK, 0);
1140 break;
1141 default:
1142 dev_err(codec_dai->dev, "Unknown clock source %d\n", clk_id);
1143 return -EINVAL;
1144 }
1145
1146 da7219->clk_src = clk_id;
1147
1148 if (da7219->mclk) {
1149 freq = clk_round_rate(da7219->mclk, freq);
1150 ret = clk_set_rate(da7219->mclk, freq);
1151 if (ret) {
1152 dev_err(codec_dai->dev, "Failed to set clock rate %d\n",
1153 freq);
1154 return ret;
1155 }
1156 }
1157
1158 da7219->mclk_rate = freq;
1159
1160 return 0;
1161}
1162
1163static int da7219_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id,
1164 int source, unsigned int fref, unsigned int fout)
1165{
1166 struct snd_soc_codec *codec = codec_dai->codec;
1167 struct da7219_priv *da7219 = snd_soc_codec_get_drvdata(codec);
1168
1169 u8 pll_ctrl, indiv_bits, indiv;
1170 u8 pll_frac_top, pll_frac_bot, pll_integer;
1171 u32 freq_ref;
1172 u64 frac_div;
1173
Adam Thomson501f72e2015-12-22 18:27:56 +00001174 /* Verify 2MHz - 54MHz MCLK provided, and set input divider */
1175 if (da7219->mclk_rate < 2000000) {
Adam Thomson6d817c02015-09-29 16:44:01 +01001176 dev_err(codec->dev, "PLL input clock %d below valid range\n",
1177 da7219->mclk_rate);
1178 return -EINVAL;
Adam Thomson63a450a2016-04-19 15:19:02 +01001179 } else if (da7219->mclk_rate <= 4500000) {
1180 indiv_bits = DA7219_PLL_INDIV_2_TO_4_5_MHZ;
1181 indiv = DA7219_PLL_INDIV_2_TO_4_5_MHZ_VAL;
1182 } else if (da7219->mclk_rate <= 9000000) {
1183 indiv_bits = DA7219_PLL_INDIV_4_5_TO_9_MHZ;
1184 indiv = DA7219_PLL_INDIV_4_5_TO_9_MHZ_VAL;
1185 } else if (da7219->mclk_rate <= 18000000) {
1186 indiv_bits = DA7219_PLL_INDIV_9_TO_18_MHZ;
1187 indiv = DA7219_PLL_INDIV_9_TO_18_MHZ_VAL;
1188 } else if (da7219->mclk_rate <= 36000000) {
1189 indiv_bits = DA7219_PLL_INDIV_18_TO_36_MHZ;
1190 indiv = DA7219_PLL_INDIV_18_TO_36_MHZ_VAL;
Adam Thomson6d817c02015-09-29 16:44:01 +01001191 } else if (da7219->mclk_rate <= 54000000) {
Adam Thomson63a450a2016-04-19 15:19:02 +01001192 indiv_bits = DA7219_PLL_INDIV_36_TO_54_MHZ;
1193 indiv = DA7219_PLL_INDIV_36_TO_54_MHZ_VAL;
Adam Thomson6d817c02015-09-29 16:44:01 +01001194 } else {
1195 dev_err(codec->dev, "PLL input clock %d above valid range\n",
1196 da7219->mclk_rate);
1197 return -EINVAL;
1198 }
1199 freq_ref = (da7219->mclk_rate / indiv);
1200 pll_ctrl = indiv_bits;
1201
1202 /* Configure PLL */
1203 switch (source) {
1204 case DA7219_SYSCLK_MCLK:
1205 pll_ctrl |= DA7219_PLL_MODE_BYPASS;
1206 snd_soc_update_bits(codec, DA7219_PLL_CTRL,
1207 DA7219_PLL_INDIV_MASK |
1208 DA7219_PLL_MODE_MASK, pll_ctrl);
1209 return 0;
1210 case DA7219_SYSCLK_PLL:
1211 pll_ctrl |= DA7219_PLL_MODE_NORMAL;
1212 break;
1213 case DA7219_SYSCLK_PLL_SRM:
1214 pll_ctrl |= DA7219_PLL_MODE_SRM;
1215 break;
Adam Thomson6d817c02015-09-29 16:44:01 +01001216 default:
1217 dev_err(codec->dev, "Invalid PLL config\n");
1218 return -EINVAL;
1219 }
1220
1221 /* Calculate dividers for PLL */
1222 pll_integer = fout / freq_ref;
1223 frac_div = (u64)(fout % freq_ref) * 8192ULL;
1224 do_div(frac_div, freq_ref);
1225 pll_frac_top = (frac_div >> DA7219_BYTE_SHIFT) & DA7219_BYTE_MASK;
1226 pll_frac_bot = (frac_div) & DA7219_BYTE_MASK;
1227
1228 /* Write PLL config & dividers */
1229 snd_soc_write(codec, DA7219_PLL_FRAC_TOP, pll_frac_top);
1230 snd_soc_write(codec, DA7219_PLL_FRAC_BOT, pll_frac_bot);
1231 snd_soc_write(codec, DA7219_PLL_INTEGER, pll_integer);
1232 snd_soc_update_bits(codec, DA7219_PLL_CTRL,
1233 DA7219_PLL_INDIV_MASK | DA7219_PLL_MODE_MASK,
1234 pll_ctrl);
1235
1236 return 0;
1237}
1238
1239static int da7219_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
1240{
1241 struct snd_soc_codec *codec = codec_dai->codec;
1242 struct da7219_priv *da7219 = snd_soc_codec_get_drvdata(codec);
1243 u8 dai_clk_mode = 0, dai_ctrl = 0;
1244
1245 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1246 case SND_SOC_DAIFMT_CBM_CFM:
1247 da7219->master = true;
1248 break;
1249 case SND_SOC_DAIFMT_CBS_CFS:
1250 da7219->master = false;
1251 break;
1252 default:
1253 return -EINVAL;
1254 }
1255
Adam Thomson4acfa362016-01-05 15:05:36 +00001256 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1257 case SND_SOC_DAIFMT_I2S:
1258 case SND_SOC_DAIFMT_LEFT_J:
1259 case SND_SOC_DAIFMT_RIGHT_J:
1260 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1261 case SND_SOC_DAIFMT_NB_NF:
1262 break;
1263 case SND_SOC_DAIFMT_NB_IF:
1264 dai_clk_mode |= DA7219_DAI_WCLK_POL_INV;
1265 break;
1266 case SND_SOC_DAIFMT_IB_NF:
1267 dai_clk_mode |= DA7219_DAI_CLK_POL_INV;
1268 break;
1269 case SND_SOC_DAIFMT_IB_IF:
1270 dai_clk_mode |= DA7219_DAI_WCLK_POL_INV |
1271 DA7219_DAI_CLK_POL_INV;
1272 break;
1273 default:
1274 return -EINVAL;
1275 }
Adam Thomson6d817c02015-09-29 16:44:01 +01001276 break;
Adam Thomson4acfa362016-01-05 15:05:36 +00001277 case SND_SOC_DAIFMT_DSP_B:
1278 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1279 case SND_SOC_DAIFMT_NB_NF:
1280 dai_clk_mode |= DA7219_DAI_CLK_POL_INV;
1281 break;
1282 case SND_SOC_DAIFMT_NB_IF:
1283 dai_clk_mode |= DA7219_DAI_WCLK_POL_INV |
1284 DA7219_DAI_CLK_POL_INV;
1285 break;
1286 case SND_SOC_DAIFMT_IB_NF:
1287 break;
1288 case SND_SOC_DAIFMT_IB_IF:
1289 dai_clk_mode |= DA7219_DAI_WCLK_POL_INV;
1290 break;
1291 default:
1292 return -EINVAL;
1293 }
Adam Thomson6d817c02015-09-29 16:44:01 +01001294 break;
1295 default:
1296 return -EINVAL;
1297 }
1298
1299 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1300 case SND_SOC_DAIFMT_I2S:
1301 dai_ctrl |= DA7219_DAI_FORMAT_I2S;
1302 break;
1303 case SND_SOC_DAIFMT_LEFT_J:
1304 dai_ctrl |= DA7219_DAI_FORMAT_LEFT_J;
1305 break;
1306 case SND_SOC_DAIFMT_RIGHT_J:
1307 dai_ctrl |= DA7219_DAI_FORMAT_RIGHT_J;
1308 break;
1309 case SND_SOC_DAIFMT_DSP_B:
1310 dai_ctrl |= DA7219_DAI_FORMAT_DSP;
1311 break;
1312 default:
1313 return -EINVAL;
1314 }
1315
1316 /* By default 64 BCLKs per WCLK is supported */
1317 dai_clk_mode |= DA7219_DAI_BCLKS_PER_WCLK_64;
1318
1319 snd_soc_update_bits(codec, DA7219_DAI_CLK_MODE,
1320 DA7219_DAI_BCLKS_PER_WCLK_MASK |
1321 DA7219_DAI_CLK_POL_MASK | DA7219_DAI_WCLK_POL_MASK,
1322 dai_clk_mode);
1323 snd_soc_update_bits(codec, DA7219_DAI_CTRL, DA7219_DAI_FORMAT_MASK,
1324 dai_ctrl);
1325
1326 return 0;
1327}
1328
1329static int da7219_set_dai_tdm_slot(struct snd_soc_dai *dai,
1330 unsigned int tx_mask, unsigned int rx_mask,
1331 int slots, int slot_width)
1332{
1333 struct snd_soc_codec *codec = dai->codec;
1334 struct da7219_priv *da7219 = snd_soc_codec_get_drvdata(codec);
1335 u8 dai_bclks_per_wclk;
1336 u16 offset;
1337 u32 frame_size;
1338
1339 /* No channels enabled so disable TDM, revert to 64-bit frames */
1340 if (!tx_mask) {
1341 snd_soc_update_bits(codec, DA7219_DAI_TDM_CTRL,
1342 DA7219_DAI_TDM_CH_EN_MASK |
1343 DA7219_DAI_TDM_MODE_EN_MASK, 0);
1344 snd_soc_update_bits(codec, DA7219_DAI_CLK_MODE,
1345 DA7219_DAI_BCLKS_PER_WCLK_MASK,
1346 DA7219_DAI_BCLKS_PER_WCLK_64);
1347 return 0;
1348 }
1349
1350 /* Check we have valid slots */
1351 if (fls(tx_mask) > DA7219_DAI_TDM_MAX_SLOTS) {
1352 dev_err(codec->dev, "Invalid number of slots, max = %d\n",
1353 DA7219_DAI_TDM_MAX_SLOTS);
1354 return -EINVAL;
1355 }
1356
1357 /* Check we have a valid offset given */
1358 if (rx_mask > DA7219_DAI_OFFSET_MAX) {
1359 dev_err(codec->dev, "Invalid slot offset, max = %d\n",
1360 DA7219_DAI_OFFSET_MAX);
1361 return -EINVAL;
1362 }
1363
1364 /* Calculate & validate frame size based on slot info provided. */
1365 frame_size = slots * slot_width;
1366 switch (frame_size) {
1367 case 32:
1368 dai_bclks_per_wclk = DA7219_DAI_BCLKS_PER_WCLK_32;
1369 break;
1370 case 64:
1371 dai_bclks_per_wclk = DA7219_DAI_BCLKS_PER_WCLK_64;
1372 break;
1373 case 128:
1374 dai_bclks_per_wclk = DA7219_DAI_BCLKS_PER_WCLK_128;
1375 break;
1376 case 256:
1377 dai_bclks_per_wclk = DA7219_DAI_BCLKS_PER_WCLK_256;
1378 break;
1379 default:
1380 dev_err(codec->dev, "Invalid frame size %d\n", frame_size);
1381 return -EINVAL;
1382 }
1383
1384 snd_soc_update_bits(codec, DA7219_DAI_CLK_MODE,
1385 DA7219_DAI_BCLKS_PER_WCLK_MASK,
1386 dai_bclks_per_wclk);
1387
1388 offset = cpu_to_le16(rx_mask);
1389 regmap_bulk_write(da7219->regmap, DA7219_DAI_OFFSET_LOWER,
1390 &offset, sizeof(offset));
1391
1392 snd_soc_update_bits(codec, DA7219_DAI_TDM_CTRL,
1393 DA7219_DAI_TDM_CH_EN_MASK |
1394 DA7219_DAI_TDM_MODE_EN_MASK,
1395 (tx_mask << DA7219_DAI_TDM_CH_EN_SHIFT) |
1396 DA7219_DAI_TDM_MODE_EN_MASK);
1397
1398 return 0;
1399}
1400
1401static int da7219_hw_params(struct snd_pcm_substream *substream,
1402 struct snd_pcm_hw_params *params,
1403 struct snd_soc_dai *dai)
1404{
1405 struct snd_soc_codec *codec = dai->codec;
1406 u8 dai_ctrl = 0, fs;
1407 unsigned int channels;
1408
1409 switch (params_width(params)) {
1410 case 16:
1411 dai_ctrl |= DA7219_DAI_WORD_LENGTH_S16_LE;
1412 break;
1413 case 20:
1414 dai_ctrl |= DA7219_DAI_WORD_LENGTH_S20_LE;
1415 break;
1416 case 24:
1417 dai_ctrl |= DA7219_DAI_WORD_LENGTH_S24_LE;
1418 break;
1419 case 32:
1420 dai_ctrl |= DA7219_DAI_WORD_LENGTH_S32_LE;
1421 break;
1422 default:
1423 return -EINVAL;
1424 }
1425
1426 channels = params_channels(params);
Axel Lina7374472015-10-24 14:28:33 +08001427 if ((channels < 1) || (channels > DA7219_DAI_CH_NUM_MAX)) {
Adam Thomson6d817c02015-09-29 16:44:01 +01001428 dev_err(codec->dev,
1429 "Invalid number of channels, only 1 to %d supported\n",
1430 DA7219_DAI_CH_NUM_MAX);
1431 return -EINVAL;
1432 }
1433 dai_ctrl |= channels << DA7219_DAI_CH_NUM_SHIFT;
1434
1435 switch (params_rate(params)) {
1436 case 8000:
1437 fs = DA7219_SR_8000;
1438 break;
1439 case 11025:
1440 fs = DA7219_SR_11025;
1441 break;
1442 case 12000:
1443 fs = DA7219_SR_12000;
1444 break;
1445 case 16000:
1446 fs = DA7219_SR_16000;
1447 break;
1448 case 22050:
1449 fs = DA7219_SR_22050;
1450 break;
1451 case 24000:
1452 fs = DA7219_SR_24000;
1453 break;
1454 case 32000:
1455 fs = DA7219_SR_32000;
1456 break;
1457 case 44100:
1458 fs = DA7219_SR_44100;
1459 break;
1460 case 48000:
1461 fs = DA7219_SR_48000;
1462 break;
1463 case 88200:
1464 fs = DA7219_SR_88200;
1465 break;
1466 case 96000:
1467 fs = DA7219_SR_96000;
1468 break;
1469 default:
1470 return -EINVAL;
1471 }
1472
1473 snd_soc_update_bits(codec, DA7219_DAI_CTRL,
1474 DA7219_DAI_WORD_LENGTH_MASK |
1475 DA7219_DAI_CH_NUM_MASK,
1476 dai_ctrl);
1477 snd_soc_write(codec, DA7219_SR, fs);
1478
1479 return 0;
1480}
1481
1482static const struct snd_soc_dai_ops da7219_dai_ops = {
1483 .hw_params = da7219_hw_params,
1484 .set_sysclk = da7219_set_dai_sysclk,
1485 .set_pll = da7219_set_dai_pll,
1486 .set_fmt = da7219_set_dai_fmt,
1487 .set_tdm_slot = da7219_set_dai_tdm_slot,
1488};
1489
1490#define DA7219_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
1491 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
1492
1493static struct snd_soc_dai_driver da7219_dai = {
1494 .name = "da7219-hifi",
1495 .playback = {
1496 .stream_name = "Playback",
1497 .channels_min = 1,
1498 .channels_max = DA7219_DAI_CH_NUM_MAX,
1499 .rates = SNDRV_PCM_RATE_8000_96000,
1500 .formats = DA7219_FORMATS,
1501 },
1502 .capture = {
1503 .stream_name = "Capture",
1504 .channels_min = 1,
1505 .channels_max = DA7219_DAI_CH_NUM_MAX,
1506 .rates = SNDRV_PCM_RATE_8000_96000,
1507 .formats = DA7219_FORMATS,
1508 },
1509 .ops = &da7219_dai_ops,
1510 .symmetric_rates = 1,
1511 .symmetric_channels = 1,
1512 .symmetric_samplebits = 1,
1513};
1514
1515
1516/*
Adam Thomsona01b8932016-06-21 18:50:21 +01001517 * DT/ACPI
Adam Thomson6d817c02015-09-29 16:44:01 +01001518 */
1519
1520static const struct of_device_id da7219_of_match[] = {
1521 { .compatible = "dlg,da7219", },
1522 { }
1523};
1524MODULE_DEVICE_TABLE(of, da7219_of_match);
1525
Adam Thomson51813652016-05-05 11:53:06 +01001526static const struct acpi_device_id da7219_acpi_match[] = {
1527 { .id = "DLGS7219", },
1528 { }
1529};
1530MODULE_DEVICE_TABLE(acpi, da7219_acpi_match);
1531
Adam Thomson6d817c02015-09-29 16:44:01 +01001532static enum da7219_micbias_voltage
Adam Thomsona01b8932016-06-21 18:50:21 +01001533 da7219_fw_micbias_lvl(struct device *dev, u32 val)
Adam Thomson6d817c02015-09-29 16:44:01 +01001534{
1535 switch (val) {
Adam Thomson0aed64c2015-12-22 18:27:55 +00001536 case 1600:
1537 return DA7219_MICBIAS_1_6V;
Adam Thomson6d817c02015-09-29 16:44:01 +01001538 case 1800:
1539 return DA7219_MICBIAS_1_8V;
1540 case 2000:
1541 return DA7219_MICBIAS_2_0V;
1542 case 2200:
1543 return DA7219_MICBIAS_2_2V;
1544 case 2400:
1545 return DA7219_MICBIAS_2_4V;
1546 case 2600:
1547 return DA7219_MICBIAS_2_6V;
1548 default:
Adam Thomsona01b8932016-06-21 18:50:21 +01001549 dev_warn(dev, "Invalid micbias level");
Adam Thomson6d817c02015-09-29 16:44:01 +01001550 return DA7219_MICBIAS_2_2V;
1551 }
1552}
1553
1554static enum da7219_mic_amp_in_sel
Adam Thomsona01b8932016-06-21 18:50:21 +01001555 da7219_fw_mic_amp_in_sel(struct device *dev, const char *str)
Adam Thomson6d817c02015-09-29 16:44:01 +01001556{
1557 if (!strcmp(str, "diff")) {
1558 return DA7219_MIC_AMP_IN_SEL_DIFF;
1559 } else if (!strcmp(str, "se_p")) {
1560 return DA7219_MIC_AMP_IN_SEL_SE_P;
1561 } else if (!strcmp(str, "se_n")) {
1562 return DA7219_MIC_AMP_IN_SEL_SE_N;
1563 } else {
Adam Thomsona01b8932016-06-21 18:50:21 +01001564 dev_warn(dev, "Invalid mic input type selection");
Adam Thomson6d817c02015-09-29 16:44:01 +01001565 return DA7219_MIC_AMP_IN_SEL_DIFF;
1566 }
1567}
1568
Adam Thomsona01b8932016-06-21 18:50:21 +01001569static struct da7219_pdata *da7219_fw_to_pdata(struct snd_soc_codec *codec)
Adam Thomson6d817c02015-09-29 16:44:01 +01001570{
Adam Thomsona01b8932016-06-21 18:50:21 +01001571 struct device *dev = codec->dev;
Adam Thomson6d817c02015-09-29 16:44:01 +01001572 struct da7219_pdata *pdata;
1573 const char *of_str;
1574 u32 of_val32;
1575
Adam Thomsona01b8932016-06-21 18:50:21 +01001576 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
Adam Thomson6d817c02015-09-29 16:44:01 +01001577 if (!pdata)
1578 return NULL;
1579
Adam Thomsonbb0c35f2016-09-26 14:29:21 +01001580 pdata->wakeup_source = device_property_read_bool(dev, "wakeup-source");
1581
Adam Thomsona01b8932016-06-21 18:50:21 +01001582 if (device_property_read_u32(dev, "dlg,micbias-lvl", &of_val32) >= 0)
1583 pdata->micbias_lvl = da7219_fw_micbias_lvl(dev, of_val32);
Adam Thomson6d817c02015-09-29 16:44:01 +01001584 else
1585 pdata->micbias_lvl = DA7219_MICBIAS_2_2V;
1586
Adam Thomsona01b8932016-06-21 18:50:21 +01001587 if (!device_property_read_string(dev, "dlg,mic-amp-in-sel", &of_str))
1588 pdata->mic_amp_in_sel = da7219_fw_mic_amp_in_sel(dev, of_str);
Adam Thomson6d817c02015-09-29 16:44:01 +01001589 else
1590 pdata->mic_amp_in_sel = DA7219_MIC_AMP_IN_SEL_DIFF;
1591
1592 return pdata;
1593}
1594
1595
1596/*
1597 * Codec driver functions
1598 */
1599
1600static int da7219_set_bias_level(struct snd_soc_codec *codec,
1601 enum snd_soc_bias_level level)
1602{
1603 struct da7219_priv *da7219 = snd_soc_codec_get_drvdata(codec);
Adam Thomsonba856fb2015-10-07 11:57:14 +01001604 int ret;
Adam Thomson6d817c02015-09-29 16:44:01 +01001605
1606 switch (level) {
1607 case SND_SOC_BIAS_ON:
Adam Thomson6d817c02015-09-29 16:44:01 +01001608 break;
Adam Thomson40585392016-07-29 14:46:54 +01001609 case SND_SOC_BIAS_PREPARE:
1610 /* Enable MCLK for transition to ON state */
1611 if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_STANDBY) {
Adam Thomsonba856fb2015-10-07 11:57:14 +01001612 if (da7219->mclk) {
1613 ret = clk_prepare_enable(da7219->mclk);
1614 if (ret) {
1615 dev_err(codec->dev,
1616 "Failed to enable mclk\n");
1617 return ret;
1618 }
1619 }
Adam Thomson40585392016-07-29 14:46:54 +01001620 }
Adam Thomson6d817c02015-09-29 16:44:01 +01001621
Adam Thomson40585392016-07-29 14:46:54 +01001622 break;
1623 case SND_SOC_BIAS_STANDBY:
Adam Thomsonbb0c35f2016-09-26 14:29:21 +01001624 if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_OFF)
Adam Thomson6d817c02015-09-29 16:44:01 +01001625 /* Master bias */
1626 snd_soc_update_bits(codec, DA7219_REFERENCES,
1627 DA7219_BIAS_EN_MASK,
1628 DA7219_BIAS_EN_MASK);
Adam Thomsonbb0c35f2016-09-26 14:29:21 +01001629
1630 if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_PREPARE) {
Adam Thomson40585392016-07-29 14:46:54 +01001631 /* Remove MCLK */
1632 if (da7219->mclk)
1633 clk_disable_unprepare(da7219->mclk);
Adam Thomson6d817c02015-09-29 16:44:01 +01001634 }
1635 break;
1636 case SND_SOC_BIAS_OFF:
Adam Thomsonbb0c35f2016-09-26 14:29:21 +01001637 /* Only disable master bias if we're not a wake-up source */
1638 if (!da7219->wakeup_source)
Adam Thomson6d817c02015-09-29 16:44:01 +01001639 snd_soc_update_bits(codec, DA7219_REFERENCES,
1640 DA7219_BIAS_EN_MASK, 0);
Adam Thomson6d817c02015-09-29 16:44:01 +01001641
Adam Thomson6d817c02015-09-29 16:44:01 +01001642 break;
1643 }
1644
1645 return 0;
1646}
1647
1648static const char *da7219_supply_names[DA7219_NUM_SUPPLIES] = {
1649 [DA7219_SUPPLY_VDD] = "VDD",
1650 [DA7219_SUPPLY_VDDMIC] = "VDDMIC",
1651 [DA7219_SUPPLY_VDDIO] = "VDDIO",
1652};
1653
1654static int da7219_handle_supplies(struct snd_soc_codec *codec)
1655{
1656 struct da7219_priv *da7219 = snd_soc_codec_get_drvdata(codec);
1657 struct regulator *vddio;
1658 u8 io_voltage_lvl = DA7219_IO_VOLTAGE_LEVEL_2_5V_3_6V;
1659 int i, ret;
1660
1661 /* Get required supplies */
1662 for (i = 0; i < DA7219_NUM_SUPPLIES; ++i)
1663 da7219->supplies[i].supply = da7219_supply_names[i];
1664
1665 ret = devm_regulator_bulk_get(codec->dev, DA7219_NUM_SUPPLIES,
1666 da7219->supplies);
Adam Thomson4e929132015-10-05 15:10:04 +01001667 if (ret) {
1668 dev_err(codec->dev, "Failed to get supplies");
Adam Thomson6d817c02015-09-29 16:44:01 +01001669 return ret;
Adam Thomson4e929132015-10-05 15:10:04 +01001670 }
Adam Thomson6d817c02015-09-29 16:44:01 +01001671
1672 /* Determine VDDIO voltage provided */
1673 vddio = da7219->supplies[DA7219_SUPPLY_VDDIO].consumer;
1674 ret = regulator_get_voltage(vddio);
1675 if (ret < 1200000)
1676 dev_warn(codec->dev, "Invalid VDDIO voltage\n");
1677 else if (ret < 2800000)
1678 io_voltage_lvl = DA7219_IO_VOLTAGE_LEVEL_1_2V_2_8V;
1679
1680 /* Enable main supplies */
1681 ret = regulator_bulk_enable(DA7219_NUM_SUPPLIES, da7219->supplies);
Adam Thomson4e929132015-10-05 15:10:04 +01001682 if (ret) {
1683 dev_err(codec->dev, "Failed to enable supplies");
1684 return ret;
1685 }
Adam Thomson6d817c02015-09-29 16:44:01 +01001686
1687 /* Ensure device in active mode */
1688 snd_soc_write(codec, DA7219_SYSTEM_ACTIVE, DA7219_SYSTEM_ACTIVE_MASK);
1689
1690 /* Update IO voltage level range */
1691 snd_soc_write(codec, DA7219_IO_CTRL, io_voltage_lvl);
1692
Adam Thomson4e929132015-10-05 15:10:04 +01001693 return 0;
Adam Thomson6d817c02015-09-29 16:44:01 +01001694}
1695
1696static void da7219_handle_pdata(struct snd_soc_codec *codec)
1697{
1698 struct da7219_priv *da7219 = snd_soc_codec_get_drvdata(codec);
1699 struct da7219_pdata *pdata = da7219->pdata;
1700
1701 if (pdata) {
1702 u8 micbias_lvl = 0;
1703
Adam Thomsonbb0c35f2016-09-26 14:29:21 +01001704 da7219->wakeup_source = pdata->wakeup_source;
1705
Adam Thomson6d817c02015-09-29 16:44:01 +01001706 /* Mic Bias voltages */
1707 switch (pdata->micbias_lvl) {
Adam Thomson0aed64c2015-12-22 18:27:55 +00001708 case DA7219_MICBIAS_1_6V:
Adam Thomson6d817c02015-09-29 16:44:01 +01001709 case DA7219_MICBIAS_1_8V:
1710 case DA7219_MICBIAS_2_0V:
1711 case DA7219_MICBIAS_2_2V:
1712 case DA7219_MICBIAS_2_4V:
1713 case DA7219_MICBIAS_2_6V:
1714 micbias_lvl |= (pdata->micbias_lvl <<
1715 DA7219_MICBIAS1_LEVEL_SHIFT);
1716 break;
1717 }
1718
1719 snd_soc_write(codec, DA7219_MICBIAS_CTRL, micbias_lvl);
1720
1721 /* Mic */
1722 switch (pdata->mic_amp_in_sel) {
1723 case DA7219_MIC_AMP_IN_SEL_DIFF:
1724 case DA7219_MIC_AMP_IN_SEL_SE_P:
1725 case DA7219_MIC_AMP_IN_SEL_SE_N:
1726 snd_soc_write(codec, DA7219_MIC_1_SELECT,
1727 pdata->mic_amp_in_sel);
1728 break;
1729 }
1730 }
1731}
1732
Adam Thomsonabd7c892015-12-23 13:50:04 +00001733static struct reg_sequence da7219_rev_aa_patch[] = {
1734 { DA7219_REFERENCES, 0x08 },
1735};
1736
Adam Thomson6d817c02015-09-29 16:44:01 +01001737static int da7219_probe(struct snd_soc_codec *codec)
1738{
1739 struct da7219_priv *da7219 = snd_soc_codec_get_drvdata(codec);
Adam Thomsonabd7c892015-12-23 13:50:04 +00001740 unsigned int rev;
Adam Thomson6d817c02015-09-29 16:44:01 +01001741 int ret;
1742
1743 mutex_init(&da7219->lock);
1744
1745 /* Regulator configuration */
1746 ret = da7219_handle_supplies(codec);
1747 if (ret)
1748 return ret;
1749
Adam Thomsonabd7c892015-12-23 13:50:04 +00001750 ret = regmap_read(da7219->regmap, DA7219_CHIP_REVISION, &rev);
1751 if (ret) {
1752 dev_err(codec->dev, "Failed to read chip revision: %d\n", ret);
1753 goto err_disable_reg;
1754 }
1755
1756 switch (rev & DA7219_CHIP_MINOR_MASK) {
1757 case 0:
1758 ret = regmap_register_patch(da7219->regmap, da7219_rev_aa_patch,
1759 ARRAY_SIZE(da7219_rev_aa_patch));
1760 if (ret) {
1761 dev_err(codec->dev, "Failed to register AA patch: %d\n",
1762 ret);
1763 goto err_disable_reg;
1764 }
1765 break;
1766 default:
1767 break;
1768 }
1769
Adam Thomsona01b8932016-06-21 18:50:21 +01001770 /* Handle DT/ACPI/Platform data */
1771 da7219->pdata = dev_get_platdata(codec->dev);
1772 if (!da7219->pdata)
1773 da7219->pdata = da7219_fw_to_pdata(codec);
Adam Thomson6d817c02015-09-29 16:44:01 +01001774
1775 da7219_handle_pdata(codec);
1776
1777 /* Check if MCLK provided */
1778 da7219->mclk = devm_clk_get(codec->dev, "mclk");
1779 if (IS_ERR(da7219->mclk)) {
Adam Thomson9069bf92015-12-22 18:27:51 +00001780 if (PTR_ERR(da7219->mclk) != -ENOENT) {
1781 ret = PTR_ERR(da7219->mclk);
1782 goto err_disable_reg;
1783 } else {
Adam Thomson6d817c02015-09-29 16:44:01 +01001784 da7219->mclk = NULL;
Adam Thomson9069bf92015-12-22 18:27:51 +00001785 }
Adam Thomson6d817c02015-09-29 16:44:01 +01001786 }
1787
1788 /* Default PC counter to free-running */
1789 snd_soc_update_bits(codec, DA7219_PC_COUNT, DA7219_PC_FREERUN_MASK,
1790 DA7219_PC_FREERUN_MASK);
1791
1792 /* Default gain ramping */
1793 snd_soc_update_bits(codec, DA7219_MIXIN_L_CTRL,
1794 DA7219_MIXIN_L_AMP_RAMP_EN_MASK,
1795 DA7219_MIXIN_L_AMP_RAMP_EN_MASK);
1796 snd_soc_update_bits(codec, DA7219_ADC_L_CTRL, DA7219_ADC_L_RAMP_EN_MASK,
1797 DA7219_ADC_L_RAMP_EN_MASK);
1798 snd_soc_update_bits(codec, DA7219_DAC_L_CTRL, DA7219_DAC_L_RAMP_EN_MASK,
1799 DA7219_DAC_L_RAMP_EN_MASK);
1800 snd_soc_update_bits(codec, DA7219_DAC_R_CTRL, DA7219_DAC_R_RAMP_EN_MASK,
1801 DA7219_DAC_R_RAMP_EN_MASK);
1802 snd_soc_update_bits(codec, DA7219_HP_L_CTRL,
1803 DA7219_HP_L_AMP_RAMP_EN_MASK,
1804 DA7219_HP_L_AMP_RAMP_EN_MASK);
1805 snd_soc_update_bits(codec, DA7219_HP_R_CTRL,
1806 DA7219_HP_R_AMP_RAMP_EN_MASK,
1807 DA7219_HP_R_AMP_RAMP_EN_MASK);
1808
Adam Thomson86834512016-10-03 11:21:01 +01001809 /* Default minimum gain on HP to avoid pops during DAPM sequencing */
1810 snd_soc_update_bits(codec, DA7219_HP_L_CTRL,
1811 DA7219_HP_L_AMP_MIN_GAIN_EN_MASK,
1812 DA7219_HP_L_AMP_MIN_GAIN_EN_MASK);
1813 snd_soc_update_bits(codec, DA7219_HP_R_CTRL,
1814 DA7219_HP_R_AMP_MIN_GAIN_EN_MASK,
1815 DA7219_HP_R_AMP_MIN_GAIN_EN_MASK);
1816
Adam Thomson6d817c02015-09-29 16:44:01 +01001817 /* Default infinite tone gen, start/stop by Kcontrol */
1818 snd_soc_write(codec, DA7219_TONE_GEN_CYCLES, DA7219_BEEP_CYCLES_MASK);
1819
1820 /* Initialise AAD block */
Adam Thomson9069bf92015-12-22 18:27:51 +00001821 ret = da7219_aad_init(codec);
1822 if (ret)
1823 goto err_disable_reg;
1824
1825 return 0;
1826
1827err_disable_reg:
1828 regulator_bulk_disable(DA7219_NUM_SUPPLIES, da7219->supplies);
1829
1830 return ret;
Adam Thomson6d817c02015-09-29 16:44:01 +01001831}
1832
1833static int da7219_remove(struct snd_soc_codec *codec)
1834{
1835 struct da7219_priv *da7219 = snd_soc_codec_get_drvdata(codec);
1836
1837 da7219_aad_exit(codec);
1838
1839 /* Supplies */
1840 return regulator_bulk_disable(DA7219_NUM_SUPPLIES, da7219->supplies);
1841}
1842
1843#ifdef CONFIG_PM
1844static int da7219_suspend(struct snd_soc_codec *codec)
1845{
1846 struct da7219_priv *da7219 = snd_soc_codec_get_drvdata(codec);
1847
Adam Thomsonbb0c35f2016-09-26 14:29:21 +01001848 /* Suspend AAD if we're not a wake-up source */
1849 if (!da7219->wakeup_source)
1850 da7219_aad_suspend(codec);
Adam Thomson6d817c02015-09-29 16:44:01 +01001851
Adam Thomsonbb0c35f2016-09-26 14:29:21 +01001852 snd_soc_codec_force_bias_level(codec, SND_SOC_BIAS_OFF);
Adam Thomson6d817c02015-09-29 16:44:01 +01001853
1854 return 0;
1855}
1856
1857static int da7219_resume(struct snd_soc_codec *codec)
1858{
1859 struct da7219_priv *da7219 = snd_soc_codec_get_drvdata(codec);
1860
Adam Thomson6d817c02015-09-29 16:44:01 +01001861 snd_soc_codec_force_bias_level(codec, SND_SOC_BIAS_STANDBY);
1862
Adam Thomsonbb0c35f2016-09-26 14:29:21 +01001863 /* Resume AAD if previously suspended */
1864 if (!da7219->wakeup_source)
1865 da7219_aad_resume(codec);
1866
Adam Thomson6d817c02015-09-29 16:44:01 +01001867 return 0;
1868}
1869#else
1870#define da7219_suspend NULL
1871#define da7219_resume NULL
1872#endif
1873
1874static struct snd_soc_codec_driver soc_codec_dev_da7219 = {
1875 .probe = da7219_probe,
1876 .remove = da7219_remove,
1877 .suspend = da7219_suspend,
1878 .resume = da7219_resume,
1879 .set_bias_level = da7219_set_bias_level,
1880
Kuninori Morimotob0d9df42016-08-08 09:15:27 +00001881 .component_driver = {
1882 .controls = da7219_snd_controls,
1883 .num_controls = ARRAY_SIZE(da7219_snd_controls),
1884 .dapm_widgets = da7219_dapm_widgets,
1885 .num_dapm_widgets = ARRAY_SIZE(da7219_dapm_widgets),
1886 .dapm_routes = da7219_audio_map,
1887 .num_dapm_routes = ARRAY_SIZE(da7219_audio_map),
1888 },
Adam Thomson6d817c02015-09-29 16:44:01 +01001889};
1890
1891
1892/*
1893 * Regmap configs
1894 */
1895
1896static struct reg_default da7219_reg_defaults[] = {
1897 { DA7219_MIC_1_SELECT, 0x00 },
1898 { DA7219_CIF_TIMEOUT_CTRL, 0x01 },
1899 { DA7219_SR_24_48, 0x00 },
1900 { DA7219_SR, 0x0A },
1901 { DA7219_CIF_I2C_ADDR_CFG, 0x02 },
1902 { DA7219_PLL_CTRL, 0x10 },
1903 { DA7219_PLL_FRAC_TOP, 0x00 },
1904 { DA7219_PLL_FRAC_BOT, 0x00 },
1905 { DA7219_PLL_INTEGER, 0x20 },
1906 { DA7219_DIG_ROUTING_DAI, 0x10 },
1907 { DA7219_DAI_CLK_MODE, 0x01 },
1908 { DA7219_DAI_CTRL, 0x28 },
1909 { DA7219_DAI_TDM_CTRL, 0x40 },
1910 { DA7219_DIG_ROUTING_DAC, 0x32 },
1911 { DA7219_DAI_OFFSET_LOWER, 0x00 },
1912 { DA7219_DAI_OFFSET_UPPER, 0x00 },
Adam Thomson9ff09972015-12-22 18:27:53 +00001913 { DA7219_REFERENCES, 0x08 },
Adam Thomson6d817c02015-09-29 16:44:01 +01001914 { DA7219_MIXIN_L_SELECT, 0x00 },
1915 { DA7219_MIXIN_L_GAIN, 0x03 },
1916 { DA7219_ADC_L_GAIN, 0x6F },
1917 { DA7219_ADC_FILTERS1, 0x80 },
1918 { DA7219_MIC_1_GAIN, 0x01 },
1919 { DA7219_SIDETONE_CTRL, 0x40 },
1920 { DA7219_SIDETONE_GAIN, 0x0E },
1921 { DA7219_DROUTING_ST_OUTFILT_1L, 0x01 },
1922 { DA7219_DROUTING_ST_OUTFILT_1R, 0x02 },
1923 { DA7219_DAC_FILTERS5, 0x00 },
1924 { DA7219_DAC_FILTERS2, 0x88 },
1925 { DA7219_DAC_FILTERS3, 0x88 },
1926 { DA7219_DAC_FILTERS4, 0x08 },
1927 { DA7219_DAC_FILTERS1, 0x80 },
1928 { DA7219_DAC_L_GAIN, 0x6F },
1929 { DA7219_DAC_R_GAIN, 0x6F },
1930 { DA7219_CP_CTRL, 0x20 },
1931 { DA7219_HP_L_GAIN, 0x39 },
1932 { DA7219_HP_R_GAIN, 0x39 },
1933 { DA7219_MIXOUT_L_SELECT, 0x00 },
1934 { DA7219_MIXOUT_R_SELECT, 0x00 },
1935 { DA7219_MICBIAS_CTRL, 0x03 },
1936 { DA7219_MIC_1_CTRL, 0x40 },
1937 { DA7219_MIXIN_L_CTRL, 0x40 },
1938 { DA7219_ADC_L_CTRL, 0x40 },
1939 { DA7219_DAC_L_CTRL, 0x40 },
1940 { DA7219_DAC_R_CTRL, 0x40 },
1941 { DA7219_HP_L_CTRL, 0x40 },
1942 { DA7219_HP_R_CTRL, 0x40 },
1943 { DA7219_MIXOUT_L_CTRL, 0x10 },
1944 { DA7219_MIXOUT_R_CTRL, 0x10 },
1945 { DA7219_CHIP_ID1, 0x23 },
1946 { DA7219_CHIP_ID2, 0x93 },
Adam Thomson6d817c02015-09-29 16:44:01 +01001947 { DA7219_IO_CTRL, 0x00 },
1948 { DA7219_GAIN_RAMP_CTRL, 0x00 },
1949 { DA7219_PC_COUNT, 0x02 },
1950 { DA7219_CP_VOL_THRESHOLD1, 0x0E },
1951 { DA7219_DIG_CTRL, 0x00 },
1952 { DA7219_ALC_CTRL2, 0x00 },
1953 { DA7219_ALC_CTRL3, 0x00 },
1954 { DA7219_ALC_NOISE, 0x3F },
1955 { DA7219_ALC_TARGET_MIN, 0x3F },
1956 { DA7219_ALC_TARGET_MAX, 0x00 },
1957 { DA7219_ALC_GAIN_LIMITS, 0xFF },
1958 { DA7219_ALC_ANA_GAIN_LIMITS, 0x71 },
1959 { DA7219_ALC_ANTICLIP_CTRL, 0x00 },
1960 { DA7219_ALC_ANTICLIP_LEVEL, 0x00 },
1961 { DA7219_DAC_NG_SETUP_TIME, 0x00 },
1962 { DA7219_DAC_NG_OFF_THRESH, 0x00 },
1963 { DA7219_DAC_NG_ON_THRESH, 0x00 },
1964 { DA7219_DAC_NG_CTRL, 0x00 },
1965 { DA7219_TONE_GEN_CFG1, 0x00 },
1966 { DA7219_TONE_GEN_CFG2, 0x00 },
1967 { DA7219_TONE_GEN_CYCLES, 0x00 },
1968 { DA7219_TONE_GEN_FREQ1_L, 0x55 },
1969 { DA7219_TONE_GEN_FREQ1_U, 0x15 },
1970 { DA7219_TONE_GEN_FREQ2_L, 0x00 },
1971 { DA7219_TONE_GEN_FREQ2_U, 0x40 },
1972 { DA7219_TONE_GEN_ON_PER, 0x02 },
1973 { DA7219_TONE_GEN_OFF_PER, 0x01 },
1974 { DA7219_ACCDET_IRQ_MASK_A, 0x00 },
1975 { DA7219_ACCDET_IRQ_MASK_B, 0x00 },
1976 { DA7219_ACCDET_CONFIG_1, 0xD6 },
1977 { DA7219_ACCDET_CONFIG_2, 0x34 },
1978 { DA7219_ACCDET_CONFIG_3, 0x0A },
1979 { DA7219_ACCDET_CONFIG_4, 0x16 },
1980 { DA7219_ACCDET_CONFIG_5, 0x21 },
1981 { DA7219_ACCDET_CONFIG_6, 0x3E },
1982 { DA7219_ACCDET_CONFIG_7, 0x01 },
1983 { DA7219_SYSTEM_ACTIVE, 0x00 },
1984};
1985
1986static bool da7219_volatile_register(struct device *dev, unsigned int reg)
1987{
1988 switch (reg) {
1989 case DA7219_MIC_1_GAIN_STATUS:
1990 case DA7219_MIXIN_L_GAIN_STATUS:
1991 case DA7219_ADC_L_GAIN_STATUS:
1992 case DA7219_DAC_L_GAIN_STATUS:
1993 case DA7219_DAC_R_GAIN_STATUS:
1994 case DA7219_HP_L_GAIN_STATUS:
1995 case DA7219_HP_R_GAIN_STATUS:
1996 case DA7219_CIF_CTRL:
1997 case DA7219_PLL_SRM_STS:
1998 case DA7219_ALC_CTRL1:
1999 case DA7219_SYSTEM_MODES_INPUT:
2000 case DA7219_SYSTEM_MODES_OUTPUT:
2001 case DA7219_ALC_OFFSET_AUTO_M_L:
2002 case DA7219_ALC_OFFSET_AUTO_U_L:
2003 case DA7219_TONE_GEN_CFG1:
2004 case DA7219_ACCDET_STATUS_A:
2005 case DA7219_ACCDET_STATUS_B:
2006 case DA7219_ACCDET_IRQ_EVENT_A:
2007 case DA7219_ACCDET_IRQ_EVENT_B:
2008 case DA7219_ACCDET_CONFIG_8:
2009 case DA7219_SYSTEM_STATUS:
2010 return 1;
2011 default:
2012 return 0;
2013 }
2014}
2015
2016static const struct regmap_config da7219_regmap_config = {
2017 .reg_bits = 8,
2018 .val_bits = 8,
2019
2020 .max_register = DA7219_SYSTEM_ACTIVE,
2021 .reg_defaults = da7219_reg_defaults,
2022 .num_reg_defaults = ARRAY_SIZE(da7219_reg_defaults),
2023 .volatile_reg = da7219_volatile_register,
2024 .cache_type = REGCACHE_RBTREE,
2025};
2026
2027
2028/*
2029 * I2C layer
2030 */
2031
2032static int da7219_i2c_probe(struct i2c_client *i2c,
2033 const struct i2c_device_id *id)
2034{
2035 struct da7219_priv *da7219;
Adam Thomsona7f16ea2016-09-26 14:29:20 +01002036 unsigned int system_active, system_status;
2037 int i, ret;
Adam Thomson6d817c02015-09-29 16:44:01 +01002038
2039 da7219 = devm_kzalloc(&i2c->dev, sizeof(struct da7219_priv),
2040 GFP_KERNEL);
2041 if (!da7219)
2042 return -ENOMEM;
2043
2044 i2c_set_clientdata(i2c, da7219);
2045
2046 da7219->regmap = devm_regmap_init_i2c(i2c, &da7219_regmap_config);
2047 if (IS_ERR(da7219->regmap)) {
2048 ret = PTR_ERR(da7219->regmap);
2049 dev_err(&i2c->dev, "regmap_init() failed: %d\n", ret);
2050 return ret;
2051 }
2052
Adam Thomsona7f16ea2016-09-26 14:29:20 +01002053 regcache_cache_bypass(da7219->regmap, true);
2054
2055 /* Disable audio paths if still active from previous start */
2056 regmap_read(da7219->regmap, DA7219_SYSTEM_ACTIVE, &system_active);
2057 if (system_active) {
2058 regmap_write(da7219->regmap, DA7219_GAIN_RAMP_CTRL,
2059 DA7219_GAIN_RAMP_RATE_NOMINAL);
2060 regmap_write(da7219->regmap, DA7219_SYSTEM_MODES_INPUT, 0x00);
2061 regmap_write(da7219->regmap, DA7219_SYSTEM_MODES_OUTPUT, 0x01);
2062
2063 for (i = 0; i < DA7219_SYS_STAT_CHECK_RETRIES; ++i) {
2064 regmap_read(da7219->regmap, DA7219_SYSTEM_STATUS,
2065 &system_status);
2066 if (!system_status)
2067 break;
2068
2069 msleep(DA7219_SYS_STAT_CHECK_DELAY);
2070 }
2071 }
2072
2073 /* Soft reset codec */
Hsin-Yu Chao96fc2942016-09-14 22:25:40 +08002074 regmap_write_bits(da7219->regmap, DA7219_ACCDET_CONFIG_1,
2075 DA7219_ACCDET_EN_MASK, 0);
2076 regmap_write_bits(da7219->regmap, DA7219_CIF_CTRL,
Adam Thomsona7f16ea2016-09-26 14:29:20 +01002077 DA7219_CIF_REG_SOFT_RESET_MASK,
2078 DA7219_CIF_REG_SOFT_RESET_MASK);
Hsin-Yu Chao96fc2942016-09-14 22:25:40 +08002079 regmap_write_bits(da7219->regmap, DA7219_SYSTEM_ACTIVE,
2080 DA7219_SYSTEM_ACTIVE_MASK, 0);
2081
Adam Thomsona7f16ea2016-09-26 14:29:20 +01002082 regcache_cache_bypass(da7219->regmap, false);
2083
Adam Thomson6d817c02015-09-29 16:44:01 +01002084 ret = snd_soc_register_codec(&i2c->dev, &soc_codec_dev_da7219,
2085 &da7219_dai, 1);
2086 if (ret < 0) {
2087 dev_err(&i2c->dev, "Failed to register da7219 codec: %d\n",
2088 ret);
2089 }
2090 return ret;
2091}
2092
2093static int da7219_i2c_remove(struct i2c_client *client)
2094{
2095 snd_soc_unregister_codec(&client->dev);
2096 return 0;
2097}
2098
2099static const struct i2c_device_id da7219_i2c_id[] = {
2100 { "da7219", },
2101 { }
2102};
2103MODULE_DEVICE_TABLE(i2c, da7219_i2c_id);
2104
2105static struct i2c_driver da7219_i2c_driver = {
2106 .driver = {
2107 .name = "da7219",
Adam Thomsonb7ebd782015-10-07 11:57:12 +01002108 .of_match_table = of_match_ptr(da7219_of_match),
Adam Thomson51813652016-05-05 11:53:06 +01002109 .acpi_match_table = ACPI_PTR(da7219_acpi_match),
Adam Thomson6d817c02015-09-29 16:44:01 +01002110 },
2111 .probe = da7219_i2c_probe,
2112 .remove = da7219_i2c_remove,
2113 .id_table = da7219_i2c_id,
2114};
2115
2116module_i2c_driver(da7219_i2c_driver);
2117
2118MODULE_DESCRIPTION("ASoC DA7219 Codec Driver");
2119MODULE_AUTHOR("Adam Thomson <Adam.Thomson.Opensource@diasemi.com>");
2120MODULE_LICENSE("GPL");