2 * DA7213 ALSA SoC Codec Driver
4 * Copyright (c) 2013 Dialog Semiconductor
6 * Author: Adam Thomson <Adam.Thomson.Opensource@diasemi.com>
7 * Based on DA9055 ALSA SoC codec driver.
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
15 #include <linux/acpi.h>
16 #include <linux/of_device.h>
17 #include <linux/property.h>
18 #include <linux/clk.h>
19 #include <linux/delay.h>
20 #include <linux/i2c.h>
21 #include <linux/regmap.h>
22 #include <linux/slab.h>
23 #include <linux/module.h>
24 #include <sound/pcm.h>
25 #include <sound/pcm_params.h>
26 #include <sound/soc.h>
27 #include <sound/initval.h>
28 #include <sound/tlv.h>
30 #include <sound/da7213.h>
35 static const DECLARE_TLV_DB_RANGE(aux_vol_tlv
,
37 0x0, 0x11, TLV_DB_SCALE_ITEM(-5400, 0, 0),
39 0x12, 0x3f, TLV_DB_SCALE_ITEM(-5250, 150, 0)
42 static const DECLARE_TLV_DB_RANGE(digital_gain_tlv
,
43 0x0, 0x07, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE
, 0, 1),
45 0x08, 0x7f, TLV_DB_SCALE_ITEM(-7800, 75, 0)
48 static const DECLARE_TLV_DB_RANGE(alc_analog_gain_tlv
,
49 0x0, 0x0, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE
, 0, 1),
51 0x01, 0x07, TLV_DB_SCALE_ITEM(0, 600, 0)
54 static const DECLARE_TLV_DB_SCALE(mic_vol_tlv
, -600, 600, 0);
55 static const DECLARE_TLV_DB_SCALE(mixin_gain_tlv
, -450, 150, 0);
56 static const DECLARE_TLV_DB_SCALE(eq_gain_tlv
, -1050, 150, 0);
57 static const DECLARE_TLV_DB_SCALE(hp_vol_tlv
, -5700, 100, 0);
58 static const DECLARE_TLV_DB_SCALE(lineout_vol_tlv
, -4800, 100, 0);
59 static const DECLARE_TLV_DB_SCALE(alc_threshold_tlv
, -9450, 150, 0);
60 static const DECLARE_TLV_DB_SCALE(alc_gain_tlv
, 0, 600, 0);
62 /* ADC and DAC voice mode (8kHz) high pass cutoff value */
63 static const char * const da7213_voice_hpf_corner_txt
[] = {
64 "2.5Hz", "25Hz", "50Hz", "100Hz", "150Hz", "200Hz", "300Hz", "400Hz"
67 static SOC_ENUM_SINGLE_DECL(da7213_dac_voice_hpf_corner
,
69 DA7213_VOICE_HPF_CORNER_SHIFT
,
70 da7213_voice_hpf_corner_txt
);
72 static SOC_ENUM_SINGLE_DECL(da7213_adc_voice_hpf_corner
,
74 DA7213_VOICE_HPF_CORNER_SHIFT
,
75 da7213_voice_hpf_corner_txt
);
77 /* ADC and DAC high pass filter cutoff value */
78 static const char * const da7213_audio_hpf_corner_txt
[] = {
79 "Fs/24000", "Fs/12000", "Fs/6000", "Fs/3000"
82 static SOC_ENUM_SINGLE_DECL(da7213_dac_audio_hpf_corner
,
84 , DA7213_AUDIO_HPF_CORNER_SHIFT
,
85 da7213_audio_hpf_corner_txt
);
87 static SOC_ENUM_SINGLE_DECL(da7213_adc_audio_hpf_corner
,
89 DA7213_AUDIO_HPF_CORNER_SHIFT
,
90 da7213_audio_hpf_corner_txt
);
92 /* Gain ramping rate value */
93 static const char * const da7213_gain_ramp_rate_txt
[] = {
94 "nominal rate * 8", "nominal rate * 16", "nominal rate / 16",
98 static SOC_ENUM_SINGLE_DECL(da7213_gain_ramp_rate
,
99 DA7213_GAIN_RAMP_CTRL
,
100 DA7213_GAIN_RAMP_RATE_SHIFT
,
101 da7213_gain_ramp_rate_txt
);
103 /* DAC noise gate setup time value */
104 static const char * const da7213_dac_ng_setup_time_txt
[] = {
105 "256 samples", "512 samples", "1024 samples", "2048 samples"
108 static SOC_ENUM_SINGLE_DECL(da7213_dac_ng_setup_time
,
109 DA7213_DAC_NG_SETUP_TIME
,
110 DA7213_DAC_NG_SETUP_TIME_SHIFT
,
111 da7213_dac_ng_setup_time_txt
);
113 /* DAC noise gate rampup rate value */
114 static const char * const da7213_dac_ng_rampup_txt
[] = {
115 "0.02 ms/dB", "0.16 ms/dB"
118 static SOC_ENUM_SINGLE_DECL(da7213_dac_ng_rampup_rate
,
119 DA7213_DAC_NG_SETUP_TIME
,
120 DA7213_DAC_NG_RAMPUP_RATE_SHIFT
,
121 da7213_dac_ng_rampup_txt
);
123 /* DAC noise gate rampdown rate value */
124 static const char * const da7213_dac_ng_rampdown_txt
[] = {
125 "0.64 ms/dB", "20.48 ms/dB"
128 static SOC_ENUM_SINGLE_DECL(da7213_dac_ng_rampdown_rate
,
129 DA7213_DAC_NG_SETUP_TIME
,
130 DA7213_DAC_NG_RAMPDN_RATE_SHIFT
,
131 da7213_dac_ng_rampdown_txt
);
133 /* DAC soft mute rate value */
134 static const char * const da7213_dac_soft_mute_rate_txt
[] = {
135 "1", "2", "4", "8", "16", "32", "64"
138 static SOC_ENUM_SINGLE_DECL(da7213_dac_soft_mute_rate
,
140 DA7213_DAC_SOFTMUTE_RATE_SHIFT
,
141 da7213_dac_soft_mute_rate_txt
);
143 /* ALC Attack Rate select */
144 static const char * const da7213_alc_attack_rate_txt
[] = {
145 "44/fs", "88/fs", "176/fs", "352/fs", "704/fs", "1408/fs", "2816/fs",
146 "5632/fs", "11264/fs", "22528/fs", "45056/fs", "90112/fs", "180224/fs"
149 static SOC_ENUM_SINGLE_DECL(da7213_alc_attack_rate
,
151 DA7213_ALC_ATTACK_SHIFT
,
152 da7213_alc_attack_rate_txt
);
154 /* ALC Release Rate select */
155 static const char * const da7213_alc_release_rate_txt
[] = {
156 "176/fs", "352/fs", "704/fs", "1408/fs", "2816/fs", "5632/fs",
157 "11264/fs", "22528/fs", "45056/fs", "90112/fs", "180224/fs"
160 static SOC_ENUM_SINGLE_DECL(da7213_alc_release_rate
,
162 DA7213_ALC_RELEASE_SHIFT
,
163 da7213_alc_release_rate_txt
);
165 /* ALC Hold Time select */
166 static const char * const da7213_alc_hold_time_txt
[] = {
167 "62/fs", "124/fs", "248/fs", "496/fs", "992/fs", "1984/fs", "3968/fs",
168 "7936/fs", "15872/fs", "31744/fs", "63488/fs", "126976/fs",
169 "253952/fs", "507904/fs", "1015808/fs", "2031616/fs"
172 static SOC_ENUM_SINGLE_DECL(da7213_alc_hold_time
,
174 DA7213_ALC_HOLD_SHIFT
,
175 da7213_alc_hold_time_txt
);
177 /* ALC Input Signal Tracking rate select */
178 static const char * const da7213_alc_integ_rate_txt
[] = {
179 "1/4", "1/16", "1/256", "1/65536"
182 static SOC_ENUM_SINGLE_DECL(da7213_alc_integ_attack_rate
,
184 DA7213_ALC_INTEG_ATTACK_SHIFT
,
185 da7213_alc_integ_rate_txt
);
187 static SOC_ENUM_SINGLE_DECL(da7213_alc_integ_release_rate
,
189 DA7213_ALC_INTEG_RELEASE_SHIFT
,
190 da7213_alc_integ_rate_txt
);
197 static int da7213_get_alc_data(struct snd_soc_codec
*codec
, u8 reg_val
)
199 int mid_data
, top_data
;
203 for (iteration
= 0; iteration
< DA7213_ALC_AVG_ITERATIONS
;
205 /* Select the left or right channel and capture data */
206 snd_soc_write(codec
, DA7213_ALC_CIC_OP_LVL_CTRL
, reg_val
);
208 /* Select middle 8 bits for read back from data register */
209 snd_soc_write(codec
, DA7213_ALC_CIC_OP_LVL_CTRL
,
210 reg_val
| DA7213_ALC_DATA_MIDDLE
);
211 mid_data
= snd_soc_read(codec
, DA7213_ALC_CIC_OP_LVL_DATA
);
213 /* Select top 8 bits for read back from data register */
214 snd_soc_write(codec
, DA7213_ALC_CIC_OP_LVL_CTRL
,
215 reg_val
| DA7213_ALC_DATA_TOP
);
216 top_data
= snd_soc_read(codec
, DA7213_ALC_CIC_OP_LVL_DATA
);
218 sum
+= ((mid_data
<< 8) | (top_data
<< 16));
221 return sum
/ DA7213_ALC_AVG_ITERATIONS
;
224 static void da7213_alc_calib_man(struct snd_soc_codec
*codec
)
227 int avg_left_data
, avg_right_data
, offset_l
, offset_r
;
229 /* Calculate average for Left and Right data */
231 avg_left_data
= da7213_get_alc_data(codec
,
232 DA7213_ALC_CIC_OP_CHANNEL_LEFT
);
234 avg_right_data
= da7213_get_alc_data(codec
,
235 DA7213_ALC_CIC_OP_CHANNEL_RIGHT
);
237 /* Calculate DC offset */
238 offset_l
= -avg_left_data
;
239 offset_r
= -avg_right_data
;
241 reg_val
= (offset_l
& DA7213_ALC_OFFSET_15_8
) >> 8;
242 snd_soc_write(codec
, DA7213_ALC_OFFSET_MAN_M_L
, reg_val
);
243 reg_val
= (offset_l
& DA7213_ALC_OFFSET_19_16
) >> 16;
244 snd_soc_write(codec
, DA7213_ALC_OFFSET_MAN_U_L
, reg_val
);
246 reg_val
= (offset_r
& DA7213_ALC_OFFSET_15_8
) >> 8;
247 snd_soc_write(codec
, DA7213_ALC_OFFSET_MAN_M_R
, reg_val
);
248 reg_val
= (offset_r
& DA7213_ALC_OFFSET_19_16
) >> 16;
249 snd_soc_write(codec
, DA7213_ALC_OFFSET_MAN_U_R
, reg_val
);
251 /* Enable analog/digital gain mode & offset cancellation */
252 snd_soc_update_bits(codec
, DA7213_ALC_CTRL1
,
253 DA7213_ALC_OFFSET_EN
| DA7213_ALC_SYNC_MODE
,
254 DA7213_ALC_OFFSET_EN
| DA7213_ALC_SYNC_MODE
);
257 static void da7213_alc_calib_auto(struct snd_soc_codec
*codec
)
261 /* Begin auto calibration and wait for completion */
262 snd_soc_update_bits(codec
, DA7213_ALC_CTRL1
, DA7213_ALC_AUTO_CALIB_EN
,
263 DA7213_ALC_AUTO_CALIB_EN
);
265 alc_ctrl1
= snd_soc_read(codec
, DA7213_ALC_CTRL1
);
266 } while (alc_ctrl1
& DA7213_ALC_AUTO_CALIB_EN
);
268 /* If auto calibration fails, fall back to digital gain only mode */
269 if (alc_ctrl1
& DA7213_ALC_CALIB_OVERFLOW
) {
271 "ALC auto calibration failed with overflow\n");
272 snd_soc_update_bits(codec
, DA7213_ALC_CTRL1
,
273 DA7213_ALC_OFFSET_EN
| DA7213_ALC_SYNC_MODE
,
276 /* Enable analog/digital gain mode & offset cancellation */
277 snd_soc_update_bits(codec
, DA7213_ALC_CTRL1
,
278 DA7213_ALC_OFFSET_EN
| DA7213_ALC_SYNC_MODE
,
279 DA7213_ALC_OFFSET_EN
| DA7213_ALC_SYNC_MODE
);
284 static void da7213_alc_calib(struct snd_soc_codec
*codec
)
286 struct da7213_priv
*da7213
= snd_soc_codec_get_drvdata(codec
);
287 u8 adc_l_ctrl
, adc_r_ctrl
;
288 u8 mixin_l_sel
, mixin_r_sel
;
289 u8 mic_1_ctrl
, mic_2_ctrl
;
291 /* Save current values from ADC control registers */
292 adc_l_ctrl
= snd_soc_read(codec
, DA7213_ADC_L_CTRL
);
293 adc_r_ctrl
= snd_soc_read(codec
, DA7213_ADC_R_CTRL
);
295 /* Save current values from MIXIN_L/R_SELECT registers */
296 mixin_l_sel
= snd_soc_read(codec
, DA7213_MIXIN_L_SELECT
);
297 mixin_r_sel
= snd_soc_read(codec
, DA7213_MIXIN_R_SELECT
);
299 /* Save current values from MIC control registers */
300 mic_1_ctrl
= snd_soc_read(codec
, DA7213_MIC_1_CTRL
);
301 mic_2_ctrl
= snd_soc_read(codec
, DA7213_MIC_2_CTRL
);
303 /* Enable ADC Left and Right */
304 snd_soc_update_bits(codec
, DA7213_ADC_L_CTRL
, DA7213_ADC_EN
,
306 snd_soc_update_bits(codec
, DA7213_ADC_R_CTRL
, DA7213_ADC_EN
,
309 /* Enable MIC paths */
310 snd_soc_update_bits(codec
, DA7213_MIXIN_L_SELECT
,
311 DA7213_MIXIN_L_MIX_SELECT_MIC_1
|
312 DA7213_MIXIN_L_MIX_SELECT_MIC_2
,
313 DA7213_MIXIN_L_MIX_SELECT_MIC_1
|
314 DA7213_MIXIN_L_MIX_SELECT_MIC_2
);
315 snd_soc_update_bits(codec
, DA7213_MIXIN_R_SELECT
,
316 DA7213_MIXIN_R_MIX_SELECT_MIC_2
|
317 DA7213_MIXIN_R_MIX_SELECT_MIC_1
,
318 DA7213_MIXIN_R_MIX_SELECT_MIC_2
|
319 DA7213_MIXIN_R_MIX_SELECT_MIC_1
);
322 snd_soc_update_bits(codec
, DA7213_MIC_1_CTRL
, DA7213_MUTE_EN
,
324 snd_soc_update_bits(codec
, DA7213_MIC_2_CTRL
, DA7213_MUTE_EN
,
327 /* Perform calibration */
328 if (da7213
->alc_calib_auto
)
329 da7213_alc_calib_auto(codec
);
331 da7213_alc_calib_man(codec
);
333 /* Restore MIXIN_L/R_SELECT registers to their original states */
334 snd_soc_write(codec
, DA7213_MIXIN_L_SELECT
, mixin_l_sel
);
335 snd_soc_write(codec
, DA7213_MIXIN_R_SELECT
, mixin_r_sel
);
337 /* Restore ADC control registers to their original states */
338 snd_soc_write(codec
, DA7213_ADC_L_CTRL
, adc_l_ctrl
);
339 snd_soc_write(codec
, DA7213_ADC_R_CTRL
, adc_r_ctrl
);
341 /* Restore original values of MIC control registers */
342 snd_soc_write(codec
, DA7213_MIC_1_CTRL
, mic_1_ctrl
);
343 snd_soc_write(codec
, DA7213_MIC_2_CTRL
, mic_2_ctrl
);
346 static int da7213_put_mixin_gain(struct snd_kcontrol
*kcontrol
,
347 struct snd_ctl_elem_value
*ucontrol
)
349 struct snd_soc_codec
*codec
= snd_soc_kcontrol_codec(kcontrol
);
350 struct da7213_priv
*da7213
= snd_soc_codec_get_drvdata(codec
);
353 ret
= snd_soc_put_volsw_2r(kcontrol
, ucontrol
);
355 /* If ALC in operation, make sure calibrated offsets are updated */
356 if ((!ret
) && (da7213
->alc_en
))
357 da7213_alc_calib(codec
);
362 static int da7213_put_alc_sw(struct snd_kcontrol
*kcontrol
,
363 struct snd_ctl_elem_value
*ucontrol
)
365 struct snd_soc_codec
*codec
= snd_soc_kcontrol_codec(kcontrol
);
366 struct da7213_priv
*da7213
= snd_soc_codec_get_drvdata(codec
);
368 /* Force ALC offset calibration if enabling ALC */
369 if (ucontrol
->value
.integer
.value
[0] ||
370 ucontrol
->value
.integer
.value
[1]) {
371 if (!da7213
->alc_en
) {
372 da7213_alc_calib(codec
);
373 da7213
->alc_en
= true;
376 da7213
->alc_en
= false;
379 return snd_soc_put_volsw(kcontrol
, ucontrol
);
387 static const struct snd_kcontrol_new da7213_snd_controls
[] = {
389 /* Volume controls */
390 SOC_SINGLE_TLV("Mic 1 Volume", DA7213_MIC_1_GAIN
,
391 DA7213_MIC_AMP_GAIN_SHIFT
, DA7213_MIC_AMP_GAIN_MAX
,
392 DA7213_NO_INVERT
, mic_vol_tlv
),
393 SOC_SINGLE_TLV("Mic 2 Volume", DA7213_MIC_2_GAIN
,
394 DA7213_MIC_AMP_GAIN_SHIFT
, DA7213_MIC_AMP_GAIN_MAX
,
395 DA7213_NO_INVERT
, mic_vol_tlv
),
396 SOC_DOUBLE_R_TLV("Aux Volume", DA7213_AUX_L_GAIN
, DA7213_AUX_R_GAIN
,
397 DA7213_AUX_AMP_GAIN_SHIFT
, DA7213_AUX_AMP_GAIN_MAX
,
398 DA7213_NO_INVERT
, aux_vol_tlv
),
399 SOC_DOUBLE_R_EXT_TLV("Mixin PGA Volume", DA7213_MIXIN_L_GAIN
,
400 DA7213_MIXIN_R_GAIN
, DA7213_MIXIN_AMP_GAIN_SHIFT
,
401 DA7213_MIXIN_AMP_GAIN_MAX
, DA7213_NO_INVERT
,
402 snd_soc_get_volsw_2r
, da7213_put_mixin_gain
,
404 SOC_DOUBLE_R_TLV("ADC Volume", DA7213_ADC_L_GAIN
, DA7213_ADC_R_GAIN
,
405 DA7213_ADC_AMP_GAIN_SHIFT
, DA7213_ADC_AMP_GAIN_MAX
,
406 DA7213_NO_INVERT
, digital_gain_tlv
),
407 SOC_DOUBLE_R_TLV("DAC Volume", DA7213_DAC_L_GAIN
, DA7213_DAC_R_GAIN
,
408 DA7213_DAC_AMP_GAIN_SHIFT
, DA7213_DAC_AMP_GAIN_MAX
,
409 DA7213_NO_INVERT
, digital_gain_tlv
),
410 SOC_DOUBLE_R_TLV("Headphone Volume", DA7213_HP_L_GAIN
, DA7213_HP_R_GAIN
,
411 DA7213_HP_AMP_GAIN_SHIFT
, DA7213_HP_AMP_GAIN_MAX
,
412 DA7213_NO_INVERT
, hp_vol_tlv
),
413 SOC_SINGLE_TLV("Lineout Volume", DA7213_LINE_GAIN
,
414 DA7213_LINE_AMP_GAIN_SHIFT
, DA7213_LINE_AMP_GAIN_MAX
,
415 DA7213_NO_INVERT
, lineout_vol_tlv
),
417 /* DAC Equalizer controls */
418 SOC_SINGLE("DAC EQ Switch", DA7213_DAC_FILTERS4
, DA7213_DAC_EQ_EN_SHIFT
,
419 DA7213_DAC_EQ_EN_MAX
, DA7213_NO_INVERT
),
420 SOC_SINGLE_TLV("DAC EQ1 Volume", DA7213_DAC_FILTERS2
,
421 DA7213_DAC_EQ_BAND1_SHIFT
, DA7213_DAC_EQ_BAND_MAX
,
422 DA7213_NO_INVERT
, eq_gain_tlv
),
423 SOC_SINGLE_TLV("DAC EQ2 Volume", DA7213_DAC_FILTERS2
,
424 DA7213_DAC_EQ_BAND2_SHIFT
, DA7213_DAC_EQ_BAND_MAX
,
425 DA7213_NO_INVERT
, eq_gain_tlv
),
426 SOC_SINGLE_TLV("DAC EQ3 Volume", DA7213_DAC_FILTERS3
,
427 DA7213_DAC_EQ_BAND3_SHIFT
, DA7213_DAC_EQ_BAND_MAX
,
428 DA7213_NO_INVERT
, eq_gain_tlv
),
429 SOC_SINGLE_TLV("DAC EQ4 Volume", DA7213_DAC_FILTERS3
,
430 DA7213_DAC_EQ_BAND4_SHIFT
, DA7213_DAC_EQ_BAND_MAX
,
431 DA7213_NO_INVERT
, eq_gain_tlv
),
432 SOC_SINGLE_TLV("DAC EQ5 Volume", DA7213_DAC_FILTERS4
,
433 DA7213_DAC_EQ_BAND5_SHIFT
, DA7213_DAC_EQ_BAND_MAX
,
434 DA7213_NO_INVERT
, eq_gain_tlv
),
436 /* High Pass Filter and Voice Mode controls */
437 SOC_SINGLE("ADC HPF Switch", DA7213_ADC_FILTERS1
, DA7213_HPF_EN_SHIFT
,
438 DA7213_HPF_EN_MAX
, DA7213_NO_INVERT
),
439 SOC_ENUM("ADC HPF Cutoff", da7213_adc_audio_hpf_corner
),
440 SOC_SINGLE("ADC Voice Mode Switch", DA7213_ADC_FILTERS1
,
441 DA7213_VOICE_EN_SHIFT
, DA7213_VOICE_EN_MAX
,
443 SOC_ENUM("ADC Voice Cutoff", da7213_adc_voice_hpf_corner
),
445 SOC_SINGLE("DAC HPF Switch", DA7213_DAC_FILTERS1
, DA7213_HPF_EN_SHIFT
,
446 DA7213_HPF_EN_MAX
, DA7213_NO_INVERT
),
447 SOC_ENUM("DAC HPF Cutoff", da7213_dac_audio_hpf_corner
),
448 SOC_SINGLE("DAC Voice Mode Switch", DA7213_DAC_FILTERS1
,
449 DA7213_VOICE_EN_SHIFT
, DA7213_VOICE_EN_MAX
,
451 SOC_ENUM("DAC Voice Cutoff", da7213_dac_voice_hpf_corner
),
454 SOC_SINGLE("Mic 1 Switch", DA7213_MIC_1_CTRL
, DA7213_MUTE_EN_SHIFT
,
455 DA7213_MUTE_EN_MAX
, DA7213_INVERT
),
456 SOC_SINGLE("Mic 2 Switch", DA7213_MIC_2_CTRL
, DA7213_MUTE_EN_SHIFT
,
457 DA7213_MUTE_EN_MAX
, DA7213_INVERT
),
458 SOC_DOUBLE_R("Aux Switch", DA7213_AUX_L_CTRL
, DA7213_AUX_R_CTRL
,
459 DA7213_MUTE_EN_SHIFT
, DA7213_MUTE_EN_MAX
, DA7213_INVERT
),
460 SOC_DOUBLE_R("Mixin PGA Switch", DA7213_MIXIN_L_CTRL
,
461 DA7213_MIXIN_R_CTRL
, DA7213_MUTE_EN_SHIFT
,
462 DA7213_MUTE_EN_MAX
, DA7213_INVERT
),
463 SOC_DOUBLE_R("ADC Switch", DA7213_ADC_L_CTRL
, DA7213_ADC_R_CTRL
,
464 DA7213_MUTE_EN_SHIFT
, DA7213_MUTE_EN_MAX
, DA7213_INVERT
),
465 SOC_DOUBLE_R("Headphone Switch", DA7213_HP_L_CTRL
, DA7213_HP_R_CTRL
,
466 DA7213_MUTE_EN_SHIFT
, DA7213_MUTE_EN_MAX
, DA7213_INVERT
),
467 SOC_SINGLE("Lineout Switch", DA7213_LINE_CTRL
, DA7213_MUTE_EN_SHIFT
,
468 DA7213_MUTE_EN_MAX
, DA7213_INVERT
),
469 SOC_SINGLE("DAC Soft Mute Switch", DA7213_DAC_FILTERS5
,
470 DA7213_DAC_SOFTMUTE_EN_SHIFT
, DA7213_DAC_SOFTMUTE_EN_MAX
,
472 SOC_ENUM("DAC Soft Mute Rate", da7213_dac_soft_mute_rate
),
474 /* Zero Cross controls */
475 SOC_DOUBLE_R("Aux ZC Switch", DA7213_AUX_L_CTRL
, DA7213_AUX_R_CTRL
,
476 DA7213_ZC_EN_SHIFT
, DA7213_ZC_EN_MAX
, DA7213_NO_INVERT
),
477 SOC_DOUBLE_R("Mixin PGA ZC Switch", DA7213_MIXIN_L_CTRL
,
478 DA7213_MIXIN_R_CTRL
, DA7213_ZC_EN_SHIFT
, DA7213_ZC_EN_MAX
,
480 SOC_DOUBLE_R("Headphone ZC Switch", DA7213_HP_L_CTRL
, DA7213_HP_R_CTRL
,
481 DA7213_ZC_EN_SHIFT
, DA7213_ZC_EN_MAX
, DA7213_NO_INVERT
),
483 /* Gain Ramping controls */
484 SOC_DOUBLE_R("Aux Gain Ramping Switch", DA7213_AUX_L_CTRL
,
485 DA7213_AUX_R_CTRL
, DA7213_GAIN_RAMP_EN_SHIFT
,
486 DA7213_GAIN_RAMP_EN_MAX
, DA7213_NO_INVERT
),
487 SOC_DOUBLE_R("Mixin Gain Ramping Switch", DA7213_MIXIN_L_CTRL
,
488 DA7213_MIXIN_R_CTRL
, DA7213_GAIN_RAMP_EN_SHIFT
,
489 DA7213_GAIN_RAMP_EN_MAX
, DA7213_NO_INVERT
),
490 SOC_DOUBLE_R("ADC Gain Ramping Switch", DA7213_ADC_L_CTRL
,
491 DA7213_ADC_R_CTRL
, DA7213_GAIN_RAMP_EN_SHIFT
,
492 DA7213_GAIN_RAMP_EN_MAX
, DA7213_NO_INVERT
),
493 SOC_DOUBLE_R("DAC Gain Ramping Switch", DA7213_DAC_L_CTRL
,
494 DA7213_DAC_R_CTRL
, DA7213_GAIN_RAMP_EN_SHIFT
,
495 DA7213_GAIN_RAMP_EN_MAX
, DA7213_NO_INVERT
),
496 SOC_DOUBLE_R("Headphone Gain Ramping Switch", DA7213_HP_L_CTRL
,
497 DA7213_HP_R_CTRL
, DA7213_GAIN_RAMP_EN_SHIFT
,
498 DA7213_GAIN_RAMP_EN_MAX
, DA7213_NO_INVERT
),
499 SOC_SINGLE("Lineout Gain Ramping Switch", DA7213_LINE_CTRL
,
500 DA7213_GAIN_RAMP_EN_SHIFT
, DA7213_GAIN_RAMP_EN_MAX
,
502 SOC_ENUM("Gain Ramping Rate", da7213_gain_ramp_rate
),
504 /* DAC Noise Gate controls */
505 SOC_SINGLE("DAC NG Switch", DA7213_DAC_NG_CTRL
, DA7213_DAC_NG_EN_SHIFT
,
506 DA7213_DAC_NG_EN_MAX
, DA7213_NO_INVERT
),
507 SOC_ENUM("DAC NG Setup Time", da7213_dac_ng_setup_time
),
508 SOC_ENUM("DAC NG Rampup Rate", da7213_dac_ng_rampup_rate
),
509 SOC_ENUM("DAC NG Rampdown Rate", da7213_dac_ng_rampdown_rate
),
510 SOC_SINGLE("DAC NG OFF Threshold", DA7213_DAC_NG_OFF_THRESHOLD
,
511 DA7213_DAC_NG_THRESHOLD_SHIFT
, DA7213_DAC_NG_THRESHOLD_MAX
,
513 SOC_SINGLE("DAC NG ON Threshold", DA7213_DAC_NG_ON_THRESHOLD
,
514 DA7213_DAC_NG_THRESHOLD_SHIFT
, DA7213_DAC_NG_THRESHOLD_MAX
,
517 /* DAC Routing & Inversion */
518 SOC_DOUBLE("DAC Mono Switch", DA7213_DIG_ROUTING_DAC
,
519 DA7213_DAC_L_MONO_SHIFT
, DA7213_DAC_R_MONO_SHIFT
,
520 DA7213_DAC_MONO_MAX
, DA7213_NO_INVERT
),
521 SOC_DOUBLE("DAC Invert Switch", DA7213_DIG_CTRL
, DA7213_DAC_L_INV_SHIFT
,
522 DA7213_DAC_R_INV_SHIFT
, DA7213_DAC_INV_MAX
,
526 SOC_DOUBLE_R("DMIC Switch", DA7213_MIXIN_L_SELECT
,
527 DA7213_MIXIN_R_SELECT
, DA7213_DMIC_EN_SHIFT
,
528 DA7213_DMIC_EN_MAX
, DA7213_NO_INVERT
),
531 SOC_DOUBLE_EXT("ALC Switch", DA7213_ALC_CTRL1
, DA7213_ALC_L_EN_SHIFT
,
532 DA7213_ALC_R_EN_SHIFT
, DA7213_ALC_EN_MAX
,
533 DA7213_NO_INVERT
, snd_soc_get_volsw
, da7213_put_alc_sw
),
534 SOC_ENUM("ALC Attack Rate", da7213_alc_attack_rate
),
535 SOC_ENUM("ALC Release Rate", da7213_alc_release_rate
),
536 SOC_ENUM("ALC Hold Time", da7213_alc_hold_time
),
538 * Rate at which input signal envelope is tracked as the signal gets
541 SOC_ENUM("ALC Integ Attack Rate", da7213_alc_integ_attack_rate
),
543 * Rate at which input signal envelope is tracked as the signal gets
546 SOC_ENUM("ALC Integ Release Rate", da7213_alc_integ_release_rate
),
547 SOC_SINGLE_TLV("ALC Noise Threshold Volume", DA7213_ALC_NOISE
,
548 DA7213_ALC_THRESHOLD_SHIFT
, DA7213_ALC_THRESHOLD_MAX
,
549 DA7213_INVERT
, alc_threshold_tlv
),
550 SOC_SINGLE_TLV("ALC Min Threshold Volume", DA7213_ALC_TARGET_MIN
,
551 DA7213_ALC_THRESHOLD_SHIFT
, DA7213_ALC_THRESHOLD_MAX
,
552 DA7213_INVERT
, alc_threshold_tlv
),
553 SOC_SINGLE_TLV("ALC Max Threshold Volume", DA7213_ALC_TARGET_MAX
,
554 DA7213_ALC_THRESHOLD_SHIFT
, DA7213_ALC_THRESHOLD_MAX
,
555 DA7213_INVERT
, alc_threshold_tlv
),
556 SOC_SINGLE_TLV("ALC Max Attenuation Volume", DA7213_ALC_GAIN_LIMITS
,
557 DA7213_ALC_ATTEN_MAX_SHIFT
,
558 DA7213_ALC_ATTEN_GAIN_MAX_MAX
, DA7213_NO_INVERT
,
560 SOC_SINGLE_TLV("ALC Max Gain Volume", DA7213_ALC_GAIN_LIMITS
,
561 DA7213_ALC_GAIN_MAX_SHIFT
, DA7213_ALC_ATTEN_GAIN_MAX_MAX
,
562 DA7213_NO_INVERT
, alc_gain_tlv
),
563 SOC_SINGLE_TLV("ALC Min Analog Gain Volume", DA7213_ALC_ANA_GAIN_LIMITS
,
564 DA7213_ALC_ANA_GAIN_MIN_SHIFT
, DA7213_ALC_ANA_GAIN_MAX
,
565 DA7213_NO_INVERT
, alc_analog_gain_tlv
),
566 SOC_SINGLE_TLV("ALC Max Analog Gain Volume", DA7213_ALC_ANA_GAIN_LIMITS
,
567 DA7213_ALC_ANA_GAIN_MAX_SHIFT
, DA7213_ALC_ANA_GAIN_MAX
,
568 DA7213_NO_INVERT
, alc_analog_gain_tlv
),
569 SOC_SINGLE("ALC Anticlip Mode Switch", DA7213_ALC_ANTICLIP_CTRL
,
570 DA7213_ALC_ANTICLIP_EN_SHIFT
, DA7213_ALC_ANTICLIP_EN_MAX
,
572 SOC_SINGLE("ALC Anticlip Level", DA7213_ALC_ANTICLIP_LEVEL
,
573 DA7213_ALC_ANTICLIP_LEVEL_SHIFT
,
574 DA7213_ALC_ANTICLIP_LEVEL_MAX
, DA7213_NO_INVERT
),
586 /* MIC PGA source select */
587 static const char * const da7213_mic_amp_in_sel_txt
[] = {
588 "Differential", "MIC_P", "MIC_N"
591 static SOC_ENUM_SINGLE_DECL(da7213_mic_1_amp_in_sel
,
593 DA7213_MIC_AMP_IN_SEL_SHIFT
,
594 da7213_mic_amp_in_sel_txt
);
595 static const struct snd_kcontrol_new da7213_mic_1_amp_in_sel_mux
=
596 SOC_DAPM_ENUM("Mic 1 Amp Source MUX", da7213_mic_1_amp_in_sel
);
598 static SOC_ENUM_SINGLE_DECL(da7213_mic_2_amp_in_sel
,
600 DA7213_MIC_AMP_IN_SEL_SHIFT
,
601 da7213_mic_amp_in_sel_txt
);
602 static const struct snd_kcontrol_new da7213_mic_2_amp_in_sel_mux
=
603 SOC_DAPM_ENUM("Mic 2 Amp Source MUX", da7213_mic_2_amp_in_sel
);
605 /* DAI routing select */
606 static const char * const da7213_dai_src_txt
[] = {
607 "ADC Left", "ADC Right", "DAI Input Left", "DAI Input Right"
610 static SOC_ENUM_SINGLE_DECL(da7213_dai_l_src
,
611 DA7213_DIG_ROUTING_DAI
,
612 DA7213_DAI_L_SRC_SHIFT
,
614 static const struct snd_kcontrol_new da7213_dai_l_src_mux
=
615 SOC_DAPM_ENUM("DAI Left Source MUX", da7213_dai_l_src
);
617 static SOC_ENUM_SINGLE_DECL(da7213_dai_r_src
,
618 DA7213_DIG_ROUTING_DAI
,
619 DA7213_DAI_R_SRC_SHIFT
,
621 static const struct snd_kcontrol_new da7213_dai_r_src_mux
=
622 SOC_DAPM_ENUM("DAI Right Source MUX", da7213_dai_r_src
);
624 /* DAC routing select */
625 static const char * const da7213_dac_src_txt
[] = {
626 "ADC Output Left", "ADC Output Right", "DAI Input Left",
630 static SOC_ENUM_SINGLE_DECL(da7213_dac_l_src
,
631 DA7213_DIG_ROUTING_DAC
,
632 DA7213_DAC_L_SRC_SHIFT
,
634 static const struct snd_kcontrol_new da7213_dac_l_src_mux
=
635 SOC_DAPM_ENUM("DAC Left Source MUX", da7213_dac_l_src
);
637 static SOC_ENUM_SINGLE_DECL(da7213_dac_r_src
,
638 DA7213_DIG_ROUTING_DAC
,
639 DA7213_DAC_R_SRC_SHIFT
,
641 static const struct snd_kcontrol_new da7213_dac_r_src_mux
=
642 SOC_DAPM_ENUM("DAC Right Source MUX", da7213_dac_r_src
);
649 static const struct snd_kcontrol_new da7213_dapm_mixinl_controls
[] = {
650 SOC_DAPM_SINGLE("Aux Left Switch", DA7213_MIXIN_L_SELECT
,
651 DA7213_MIXIN_L_MIX_SELECT_AUX_L_SHIFT
,
652 DA7213_MIXIN_L_MIX_SELECT_MAX
, DA7213_NO_INVERT
),
653 SOC_DAPM_SINGLE("Mic 1 Switch", DA7213_MIXIN_L_SELECT
,
654 DA7213_MIXIN_L_MIX_SELECT_MIC_1_SHIFT
,
655 DA7213_MIXIN_L_MIX_SELECT_MAX
, DA7213_NO_INVERT
),
656 SOC_DAPM_SINGLE("Mic 2 Switch", DA7213_MIXIN_L_SELECT
,
657 DA7213_MIXIN_L_MIX_SELECT_MIC_2_SHIFT
,
658 DA7213_MIXIN_L_MIX_SELECT_MAX
, DA7213_NO_INVERT
),
659 SOC_DAPM_SINGLE("Mixin Right Switch", DA7213_MIXIN_L_SELECT
,
660 DA7213_MIXIN_L_MIX_SELECT_MIXIN_R_SHIFT
,
661 DA7213_MIXIN_L_MIX_SELECT_MAX
, DA7213_NO_INVERT
),
665 static const struct snd_kcontrol_new da7213_dapm_mixinr_controls
[] = {
666 SOC_DAPM_SINGLE("Aux Right Switch", DA7213_MIXIN_R_SELECT
,
667 DA7213_MIXIN_R_MIX_SELECT_AUX_R_SHIFT
,
668 DA7213_MIXIN_R_MIX_SELECT_MAX
, DA7213_NO_INVERT
),
669 SOC_DAPM_SINGLE("Mic 2 Switch", DA7213_MIXIN_R_SELECT
,
670 DA7213_MIXIN_R_MIX_SELECT_MIC_2_SHIFT
,
671 DA7213_MIXIN_R_MIX_SELECT_MAX
, DA7213_NO_INVERT
),
672 SOC_DAPM_SINGLE("Mic 1 Switch", DA7213_MIXIN_R_SELECT
,
673 DA7213_MIXIN_R_MIX_SELECT_MIC_1_SHIFT
,
674 DA7213_MIXIN_R_MIX_SELECT_MAX
, DA7213_NO_INVERT
),
675 SOC_DAPM_SINGLE("Mixin Left Switch", DA7213_MIXIN_R_SELECT
,
676 DA7213_MIXIN_R_MIX_SELECT_MIXIN_L_SHIFT
,
677 DA7213_MIXIN_R_MIX_SELECT_MAX
, DA7213_NO_INVERT
),
681 static const struct snd_kcontrol_new da7213_dapm_mixoutl_controls
[] = {
682 SOC_DAPM_SINGLE("Aux Left Switch", DA7213_MIXOUT_L_SELECT
,
683 DA7213_MIXOUT_L_MIX_SELECT_AUX_L_SHIFT
,
684 DA7213_MIXOUT_L_MIX_SELECT_MAX
, DA7213_NO_INVERT
),
685 SOC_DAPM_SINGLE("Mixin Left Switch", DA7213_MIXOUT_L_SELECT
,
686 DA7213_MIXOUT_L_MIX_SELECT_MIXIN_L_SHIFT
,
687 DA7213_MIXOUT_L_MIX_SELECT_MAX
, DA7213_NO_INVERT
),
688 SOC_DAPM_SINGLE("Mixin Right Switch", DA7213_MIXOUT_L_SELECT
,
689 DA7213_MIXOUT_L_MIX_SELECT_MIXIN_R_SHIFT
,
690 DA7213_MIXOUT_L_MIX_SELECT_MAX
, DA7213_NO_INVERT
),
691 SOC_DAPM_SINGLE("DAC Left Switch", DA7213_MIXOUT_L_SELECT
,
692 DA7213_MIXOUT_L_MIX_SELECT_DAC_L_SHIFT
,
693 DA7213_MIXOUT_L_MIX_SELECT_MAX
, DA7213_NO_INVERT
),
694 SOC_DAPM_SINGLE("Aux Left Invert Switch", DA7213_MIXOUT_L_SELECT
,
695 DA7213_MIXOUT_L_MIX_SELECT_AUX_L_INVERTED_SHIFT
,
696 DA7213_MIXOUT_L_MIX_SELECT_MAX
, DA7213_NO_INVERT
),
697 SOC_DAPM_SINGLE("Mixin Left Invert Switch", DA7213_MIXOUT_L_SELECT
,
698 DA7213_MIXOUT_L_MIX_SELECT_MIXIN_L_INVERTED_SHIFT
,
699 DA7213_MIXOUT_L_MIX_SELECT_MAX
, DA7213_NO_INVERT
),
700 SOC_DAPM_SINGLE("Mixin Right Invert Switch", DA7213_MIXOUT_L_SELECT
,
701 DA7213_MIXOUT_L_MIX_SELECT_MIXIN_R_INVERTED_SHIFT
,
702 DA7213_MIXOUT_L_MIX_SELECT_MAX
, DA7213_NO_INVERT
),
706 static const struct snd_kcontrol_new da7213_dapm_mixoutr_controls
[] = {
707 SOC_DAPM_SINGLE("Aux Right Switch", DA7213_MIXOUT_R_SELECT
,
708 DA7213_MIXOUT_R_MIX_SELECT_AUX_R_SHIFT
,
709 DA7213_MIXOUT_R_MIX_SELECT_MAX
, DA7213_NO_INVERT
),
710 SOC_DAPM_SINGLE("Mixin Right Switch", DA7213_MIXOUT_R_SELECT
,
711 DA7213_MIXOUT_R_MIX_SELECT_MIXIN_R_SHIFT
,
712 DA7213_MIXOUT_R_MIX_SELECT_MAX
, DA7213_NO_INVERT
),
713 SOC_DAPM_SINGLE("Mixin Left Switch", DA7213_MIXOUT_R_SELECT
,
714 DA7213_MIXOUT_R_MIX_SELECT_MIXIN_L_SHIFT
,
715 DA7213_MIXOUT_R_MIX_SELECT_MAX
, DA7213_NO_INVERT
),
716 SOC_DAPM_SINGLE("DAC Right Switch", DA7213_MIXOUT_R_SELECT
,
717 DA7213_MIXOUT_R_MIX_SELECT_DAC_R_SHIFT
,
718 DA7213_MIXOUT_R_MIX_SELECT_MAX
, DA7213_NO_INVERT
),
719 SOC_DAPM_SINGLE("Aux Right Invert Switch", DA7213_MIXOUT_R_SELECT
,
720 DA7213_MIXOUT_R_MIX_SELECT_AUX_R_INVERTED_SHIFT
,
721 DA7213_MIXOUT_R_MIX_SELECT_MAX
, DA7213_NO_INVERT
),
722 SOC_DAPM_SINGLE("Mixin Right Invert Switch", DA7213_MIXOUT_R_SELECT
,
723 DA7213_MIXOUT_R_MIX_SELECT_MIXIN_R_INVERTED_SHIFT
,
724 DA7213_MIXOUT_R_MIX_SELECT_MAX
, DA7213_NO_INVERT
),
725 SOC_DAPM_SINGLE("Mixin Left Invert Switch", DA7213_MIXOUT_R_SELECT
,
726 DA7213_MIXOUT_R_MIX_SELECT_MIXIN_L_INVERTED_SHIFT
,
727 DA7213_MIXOUT_R_MIX_SELECT_MAX
, DA7213_NO_INVERT
),
735 static int da7213_dai_event(struct snd_soc_dapm_widget
*w
,
736 struct snd_kcontrol
*kcontrol
, int event
)
738 struct snd_soc_codec
*codec
= snd_soc_dapm_to_codec(w
->dapm
);
739 struct da7213_priv
*da7213
= snd_soc_codec_get_drvdata(codec
);
740 u8 pll_ctrl
, pll_status
;
742 bool srm_lock
= false;
745 case SND_SOC_DAPM_PRE_PMU
:
746 /* Enable DAI clks for master mode */
748 snd_soc_update_bits(codec
, DA7213_DAI_CLK_MODE
,
749 DA7213_DAI_CLK_EN_MASK
,
750 DA7213_DAI_CLK_EN_MASK
);
752 /* PC synchronised to DAI */
753 snd_soc_update_bits(codec
, DA7213_PC_COUNT
,
754 DA7213_PC_FREERUN_MASK
, 0);
756 /* If SRM not enabled then nothing more to do */
757 pll_ctrl
= snd_soc_read(codec
, DA7213_PLL_CTRL
);
758 if (!(pll_ctrl
& DA7213_PLL_SRM_EN
))
761 /* Assist 32KHz mode PLL lock */
762 if (pll_ctrl
& DA7213_PLL_32K_MODE
) {
763 snd_soc_write(codec
, 0xF0, 0x8B);
764 snd_soc_write(codec
, 0xF2, 0x03);
765 snd_soc_write(codec
, 0xF0, 0x00);
768 /* Check SRM has locked */
770 pll_status
= snd_soc_read(codec
, DA7213_PLL_STATUS
);
771 if (pll_status
& DA7219_PLL_SRM_LOCK
) {
777 } while ((i
< DA7213_SRM_CHECK_RETRIES
) && (!srm_lock
));
780 dev_warn(codec
->dev
, "SRM failed to lock\n");
783 case SND_SOC_DAPM_POST_PMD
:
784 /* Revert 32KHz PLL lock udpates if applied previously */
785 pll_ctrl
= snd_soc_read(codec
, DA7213_PLL_CTRL
);
786 if (pll_ctrl
& DA7213_PLL_32K_MODE
) {
787 snd_soc_write(codec
, 0xF0, 0x8B);
788 snd_soc_write(codec
, 0xF2, 0x01);
789 snd_soc_write(codec
, 0xF0, 0x00);
792 /* PC free-running */
793 snd_soc_update_bits(codec
, DA7213_PC_COUNT
,
794 DA7213_PC_FREERUN_MASK
,
795 DA7213_PC_FREERUN_MASK
);
797 /* Disable DAI clks if in master mode */
799 snd_soc_update_bits(codec
, DA7213_DAI_CLK_MODE
,
800 DA7213_DAI_CLK_EN_MASK
, 0);
812 static const struct snd_soc_dapm_widget da7213_dapm_widgets
[] = {
817 /* Use a supply here as this controls both input & output DAIs */
818 SND_SOC_DAPM_SUPPLY("DAI", DA7213_DAI_CTRL
, DA7213_DAI_EN_SHIFT
,
819 DA7213_NO_INVERT
, da7213_dai_event
,
820 SND_SOC_DAPM_PRE_PMU
| SND_SOC_DAPM_POST_PMD
),
827 SND_SOC_DAPM_INPUT("MIC1"),
828 SND_SOC_DAPM_INPUT("MIC2"),
829 SND_SOC_DAPM_INPUT("AUXL"),
830 SND_SOC_DAPM_INPUT("AUXR"),
832 /* MUXs for Mic PGA source selection */
833 SND_SOC_DAPM_MUX("Mic 1 Amp Source MUX", SND_SOC_NOPM
, 0, 0,
834 &da7213_mic_1_amp_in_sel_mux
),
835 SND_SOC_DAPM_MUX("Mic 2 Amp Source MUX", SND_SOC_NOPM
, 0, 0,
836 &da7213_mic_2_amp_in_sel_mux
),
839 SND_SOC_DAPM_PGA("Mic 1 PGA", DA7213_MIC_1_CTRL
, DA7213_AMP_EN_SHIFT
,
840 DA7213_NO_INVERT
, NULL
, 0),
841 SND_SOC_DAPM_PGA("Mic 2 PGA", DA7213_MIC_2_CTRL
, DA7213_AMP_EN_SHIFT
,
842 DA7213_NO_INVERT
, NULL
, 0),
843 SND_SOC_DAPM_PGA("Aux Left PGA", DA7213_AUX_L_CTRL
, DA7213_AMP_EN_SHIFT
,
844 DA7213_NO_INVERT
, NULL
, 0),
845 SND_SOC_DAPM_PGA("Aux Right PGA", DA7213_AUX_R_CTRL
,
846 DA7213_AMP_EN_SHIFT
, DA7213_NO_INVERT
, NULL
, 0),
847 SND_SOC_DAPM_PGA("Mixin Left PGA", DA7213_MIXIN_L_CTRL
,
848 DA7213_AMP_EN_SHIFT
, DA7213_NO_INVERT
, NULL
, 0),
849 SND_SOC_DAPM_PGA("Mixin Right PGA", DA7213_MIXIN_R_CTRL
,
850 DA7213_AMP_EN_SHIFT
, DA7213_NO_INVERT
, NULL
, 0),
853 SND_SOC_DAPM_SUPPLY("Mic Bias 1", DA7213_MICBIAS_CTRL
,
854 DA7213_MICBIAS1_EN_SHIFT
, DA7213_NO_INVERT
,
856 SND_SOC_DAPM_SUPPLY("Mic Bias 2", DA7213_MICBIAS_CTRL
,
857 DA7213_MICBIAS2_EN_SHIFT
, DA7213_NO_INVERT
,
861 SND_SOC_DAPM_MIXER("Mixin Left", SND_SOC_NOPM
, 0, 0,
862 &da7213_dapm_mixinl_controls
[0],
863 ARRAY_SIZE(da7213_dapm_mixinl_controls
)),
864 SND_SOC_DAPM_MIXER("Mixin Right", SND_SOC_NOPM
, 0, 0,
865 &da7213_dapm_mixinr_controls
[0],
866 ARRAY_SIZE(da7213_dapm_mixinr_controls
)),
869 SND_SOC_DAPM_ADC("ADC Left", NULL
, DA7213_ADC_L_CTRL
,
870 DA7213_ADC_EN_SHIFT
, DA7213_NO_INVERT
),
871 SND_SOC_DAPM_ADC("ADC Right", NULL
, DA7213_ADC_R_CTRL
,
872 DA7213_ADC_EN_SHIFT
, DA7213_NO_INVERT
),
875 SND_SOC_DAPM_MUX("DAI Left Source MUX", SND_SOC_NOPM
, 0, 0,
876 &da7213_dai_l_src_mux
),
877 SND_SOC_DAPM_MUX("DAI Right Source MUX", SND_SOC_NOPM
, 0, 0,
878 &da7213_dai_r_src_mux
),
879 SND_SOC_DAPM_AIF_OUT("DAIOUTL", "Capture", 0, SND_SOC_NOPM
, 0, 0),
880 SND_SOC_DAPM_AIF_OUT("DAIOUTR", "Capture", 1, SND_SOC_NOPM
, 0, 0),
887 SND_SOC_DAPM_AIF_IN("DAIINL", "Playback", 0, SND_SOC_NOPM
, 0, 0),
888 SND_SOC_DAPM_AIF_IN("DAIINR", "Playback", 1, SND_SOC_NOPM
, 0, 0),
889 SND_SOC_DAPM_MUX("DAC Left Source MUX", SND_SOC_NOPM
, 0, 0,
890 &da7213_dac_l_src_mux
),
891 SND_SOC_DAPM_MUX("DAC Right Source MUX", SND_SOC_NOPM
, 0, 0,
892 &da7213_dac_r_src_mux
),
895 SND_SOC_DAPM_DAC("DAC Left", NULL
, DA7213_DAC_L_CTRL
,
896 DA7213_DAC_EN_SHIFT
, DA7213_NO_INVERT
),
897 SND_SOC_DAPM_DAC("DAC Right", NULL
, DA7213_DAC_R_CTRL
,
898 DA7213_DAC_EN_SHIFT
, DA7213_NO_INVERT
),
901 SND_SOC_DAPM_MIXER("Mixout Left", SND_SOC_NOPM
, 0, 0,
902 &da7213_dapm_mixoutl_controls
[0],
903 ARRAY_SIZE(da7213_dapm_mixoutl_controls
)),
904 SND_SOC_DAPM_MIXER("Mixout Right", SND_SOC_NOPM
, 0, 0,
905 &da7213_dapm_mixoutr_controls
[0],
906 ARRAY_SIZE(da7213_dapm_mixoutr_controls
)),
909 SND_SOC_DAPM_PGA("Mixout Left PGA", DA7213_MIXOUT_L_CTRL
,
910 DA7213_AMP_EN_SHIFT
, DA7213_NO_INVERT
, NULL
, 0),
911 SND_SOC_DAPM_PGA("Mixout Right PGA", DA7213_MIXOUT_R_CTRL
,
912 DA7213_AMP_EN_SHIFT
, DA7213_NO_INVERT
, NULL
, 0),
913 SND_SOC_DAPM_PGA("Lineout PGA", DA7213_LINE_CTRL
, DA7213_AMP_EN_SHIFT
,
914 DA7213_NO_INVERT
, NULL
, 0),
915 SND_SOC_DAPM_PGA("Headphone Left PGA", DA7213_HP_L_CTRL
,
916 DA7213_AMP_EN_SHIFT
, DA7213_NO_INVERT
, NULL
, 0),
917 SND_SOC_DAPM_PGA("Headphone Right PGA", DA7213_HP_R_CTRL
,
918 DA7213_AMP_EN_SHIFT
, DA7213_NO_INVERT
, NULL
, 0),
921 SND_SOC_DAPM_SUPPLY("Charge Pump", DA7213_CP_CTRL
, DA7213_CP_EN_SHIFT
,
922 DA7213_NO_INVERT
, NULL
, 0),
925 SND_SOC_DAPM_OUTPUT("HPL"),
926 SND_SOC_DAPM_OUTPUT("HPR"),
927 SND_SOC_DAPM_OUTPUT("LINE"),
932 * DAPM audio route definition
935 static const struct snd_soc_dapm_route da7213_audio_map
[] = {
936 /* Dest Connecting Widget source */
939 {"MIC1", NULL
, "Mic Bias 1"},
940 {"MIC2", NULL
, "Mic Bias 2"},
942 {"Mic 1 Amp Source MUX", "Differential", "MIC1"},
943 {"Mic 1 Amp Source MUX", "MIC_P", "MIC1"},
944 {"Mic 1 Amp Source MUX", "MIC_N", "MIC1"},
946 {"Mic 2 Amp Source MUX", "Differential", "MIC2"},
947 {"Mic 2 Amp Source MUX", "MIC_P", "MIC2"},
948 {"Mic 2 Amp Source MUX", "MIC_N", "MIC2"},
950 {"Mic 1 PGA", NULL
, "Mic 1 Amp Source MUX"},
951 {"Mic 2 PGA", NULL
, "Mic 2 Amp Source MUX"},
953 {"Aux Left PGA", NULL
, "AUXL"},
954 {"Aux Right PGA", NULL
, "AUXR"},
956 {"Mixin Left", "Aux Left Switch", "Aux Left PGA"},
957 {"Mixin Left", "Mic 1 Switch", "Mic 1 PGA"},
958 {"Mixin Left", "Mic 2 Switch", "Mic 2 PGA"},
959 {"Mixin Left", "Mixin Right Switch", "Mixin Right PGA"},
961 {"Mixin Right", "Aux Right Switch", "Aux Right PGA"},
962 {"Mixin Right", "Mic 2 Switch", "Mic 2 PGA"},
963 {"Mixin Right", "Mic 1 Switch", "Mic 1 PGA"},
964 {"Mixin Right", "Mixin Left Switch", "Mixin Left PGA"},
966 {"Mixin Left PGA", NULL
, "Mixin Left"},
967 {"ADC Left", NULL
, "Mixin Left PGA"},
969 {"Mixin Right PGA", NULL
, "Mixin Right"},
970 {"ADC Right", NULL
, "Mixin Right PGA"},
972 {"DAI Left Source MUX", "ADC Left", "ADC Left"},
973 {"DAI Left Source MUX", "ADC Right", "ADC Right"},
974 {"DAI Left Source MUX", "DAI Input Left", "DAIINL"},
975 {"DAI Left Source MUX", "DAI Input Right", "DAIINR"},
977 {"DAI Right Source MUX", "ADC Left", "ADC Left"},
978 {"DAI Right Source MUX", "ADC Right", "ADC Right"},
979 {"DAI Right Source MUX", "DAI Input Left", "DAIINL"},
980 {"DAI Right Source MUX", "DAI Input Right", "DAIINR"},
982 {"DAIOUTL", NULL
, "DAI Left Source MUX"},
983 {"DAIOUTR", NULL
, "DAI Right Source MUX"},
985 {"DAIOUTL", NULL
, "DAI"},
986 {"DAIOUTR", NULL
, "DAI"},
989 {"DAIINL", NULL
, "DAI"},
990 {"DAIINR", NULL
, "DAI"},
992 {"DAC Left Source MUX", "ADC Output Left", "ADC Left"},
993 {"DAC Left Source MUX", "ADC Output Right", "ADC Right"},
994 {"DAC Left Source MUX", "DAI Input Left", "DAIINL"},
995 {"DAC Left Source MUX", "DAI Input Right", "DAIINR"},
997 {"DAC Right Source MUX", "ADC Output Left", "ADC Left"},
998 {"DAC Right Source MUX", "ADC Output Right", "ADC Right"},
999 {"DAC Right Source MUX", "DAI Input Left", "DAIINL"},
1000 {"DAC Right Source MUX", "DAI Input Right", "DAIINR"},
1002 {"DAC Left", NULL
, "DAC Left Source MUX"},
1003 {"DAC Right", NULL
, "DAC Right Source MUX"},
1005 {"Mixout Left", "Aux Left Switch", "Aux Left PGA"},
1006 {"Mixout Left", "Mixin Left Switch", "Mixin Left PGA"},
1007 {"Mixout Left", "Mixin Right Switch", "Mixin Right PGA"},
1008 {"Mixout Left", "DAC Left Switch", "DAC Left"},
1009 {"Mixout Left", "Aux Left Invert Switch", "Aux Left PGA"},
1010 {"Mixout Left", "Mixin Left Invert Switch", "Mixin Left PGA"},
1011 {"Mixout Left", "Mixin Right Invert Switch", "Mixin Right PGA"},
1013 {"Mixout Right", "Aux Right Switch", "Aux Right PGA"},
1014 {"Mixout Right", "Mixin Right Switch", "Mixin Right PGA"},
1015 {"Mixout Right", "Mixin Left Switch", "Mixin Left PGA"},
1016 {"Mixout Right", "DAC Right Switch", "DAC Right"},
1017 {"Mixout Right", "Aux Right Invert Switch", "Aux Right PGA"},
1018 {"Mixout Right", "Mixin Right Invert Switch", "Mixin Right PGA"},
1019 {"Mixout Right", "Mixin Left Invert Switch", "Mixin Left PGA"},
1021 {"Mixout Left PGA", NULL
, "Mixout Left"},
1022 {"Mixout Right PGA", NULL
, "Mixout Right"},
1024 {"Headphone Left PGA", NULL
, "Mixout Left PGA"},
1025 {"Headphone Left PGA", NULL
, "Charge Pump"},
1026 {"HPL", NULL
, "Headphone Left PGA"},
1028 {"Headphone Right PGA", NULL
, "Mixout Right PGA"},
1029 {"Headphone Right PGA", NULL
, "Charge Pump"},
1030 {"HPR", NULL
, "Headphone Right PGA"},
1032 {"Lineout PGA", NULL
, "Mixout Right PGA"},
1033 {"LINE", NULL
, "Lineout PGA"},
1036 static const struct reg_default da7213_reg_defaults
[] = {
1037 { DA7213_DIG_ROUTING_DAI
, 0x10 },
1038 { DA7213_SR
, 0x0A },
1039 { DA7213_REFERENCES
, 0x80 },
1040 { DA7213_PLL_FRAC_TOP
, 0x00 },
1041 { DA7213_PLL_FRAC_BOT
, 0x00 },
1042 { DA7213_PLL_INTEGER
, 0x20 },
1043 { DA7213_PLL_CTRL
, 0x0C },
1044 { DA7213_DAI_CLK_MODE
, 0x01 },
1045 { DA7213_DAI_CTRL
, 0x08 },
1046 { DA7213_DIG_ROUTING_DAC
, 0x32 },
1047 { DA7213_AUX_L_GAIN
, 0x35 },
1048 { DA7213_AUX_R_GAIN
, 0x35 },
1049 { DA7213_MIXIN_L_SELECT
, 0x00 },
1050 { DA7213_MIXIN_R_SELECT
, 0x00 },
1051 { DA7213_MIXIN_L_GAIN
, 0x03 },
1052 { DA7213_MIXIN_R_GAIN
, 0x03 },
1053 { DA7213_ADC_L_GAIN
, 0x6F },
1054 { DA7213_ADC_R_GAIN
, 0x6F },
1055 { DA7213_ADC_FILTERS1
, 0x80 },
1056 { DA7213_MIC_1_GAIN
, 0x01 },
1057 { DA7213_MIC_2_GAIN
, 0x01 },
1058 { DA7213_DAC_FILTERS5
, 0x00 },
1059 { DA7213_DAC_FILTERS2
, 0x88 },
1060 { DA7213_DAC_FILTERS3
, 0x88 },
1061 { DA7213_DAC_FILTERS4
, 0x08 },
1062 { DA7213_DAC_FILTERS1
, 0x80 },
1063 { DA7213_DAC_L_GAIN
, 0x6F },
1064 { DA7213_DAC_R_GAIN
, 0x6F },
1065 { DA7213_CP_CTRL
, 0x61 },
1066 { DA7213_HP_L_GAIN
, 0x39 },
1067 { DA7213_HP_R_GAIN
, 0x39 },
1068 { DA7213_LINE_GAIN
, 0x30 },
1069 { DA7213_MIXOUT_L_SELECT
, 0x00 },
1070 { DA7213_MIXOUT_R_SELECT
, 0x00 },
1071 { DA7213_SYSTEM_MODES_INPUT
, 0x00 },
1072 { DA7213_SYSTEM_MODES_OUTPUT
, 0x00 },
1073 { DA7213_AUX_L_CTRL
, 0x44 },
1074 { DA7213_AUX_R_CTRL
, 0x44 },
1075 { DA7213_MICBIAS_CTRL
, 0x11 },
1076 { DA7213_MIC_1_CTRL
, 0x40 },
1077 { DA7213_MIC_2_CTRL
, 0x40 },
1078 { DA7213_MIXIN_L_CTRL
, 0x40 },
1079 { DA7213_MIXIN_R_CTRL
, 0x40 },
1080 { DA7213_ADC_L_CTRL
, 0x40 },
1081 { DA7213_ADC_R_CTRL
, 0x40 },
1082 { DA7213_DAC_L_CTRL
, 0x48 },
1083 { DA7213_DAC_R_CTRL
, 0x40 },
1084 { DA7213_HP_L_CTRL
, 0x41 },
1085 { DA7213_HP_R_CTRL
, 0x40 },
1086 { DA7213_LINE_CTRL
, 0x40 },
1087 { DA7213_MIXOUT_L_CTRL
, 0x10 },
1088 { DA7213_MIXOUT_R_CTRL
, 0x10 },
1089 { DA7213_LDO_CTRL
, 0x00 },
1090 { DA7213_IO_CTRL
, 0x00 },
1091 { DA7213_GAIN_RAMP_CTRL
, 0x00},
1092 { DA7213_MIC_CONFIG
, 0x00 },
1093 { DA7213_PC_COUNT
, 0x00 },
1094 { DA7213_CP_VOL_THRESHOLD1
, 0x32 },
1095 { DA7213_CP_DELAY
, 0x95 },
1096 { DA7213_CP_DETECTOR
, 0x00 },
1097 { DA7213_DAI_OFFSET
, 0x00 },
1098 { DA7213_DIG_CTRL
, 0x00 },
1099 { DA7213_ALC_CTRL2
, 0x00 },
1100 { DA7213_ALC_CTRL3
, 0x00 },
1101 { DA7213_ALC_NOISE
, 0x3F },
1102 { DA7213_ALC_TARGET_MIN
, 0x3F },
1103 { DA7213_ALC_TARGET_MAX
, 0x00 },
1104 { DA7213_ALC_GAIN_LIMITS
, 0xFF },
1105 { DA7213_ALC_ANA_GAIN_LIMITS
, 0x71 },
1106 { DA7213_ALC_ANTICLIP_CTRL
, 0x00 },
1107 { DA7213_ALC_ANTICLIP_LEVEL
, 0x00 },
1108 { DA7213_ALC_OFFSET_MAN_M_L
, 0x00 },
1109 { DA7213_ALC_OFFSET_MAN_U_L
, 0x00 },
1110 { DA7213_ALC_OFFSET_MAN_M_R
, 0x00 },
1111 { DA7213_ALC_OFFSET_MAN_U_R
, 0x00 },
1112 { DA7213_ALC_CIC_OP_LVL_CTRL
, 0x00 },
1113 { DA7213_DAC_NG_SETUP_TIME
, 0x00 },
1114 { DA7213_DAC_NG_OFF_THRESHOLD
, 0x00 },
1115 { DA7213_DAC_NG_ON_THRESHOLD
, 0x00 },
1116 { DA7213_DAC_NG_CTRL
, 0x00 },
1119 static bool da7213_volatile_register(struct device
*dev
, unsigned int reg
)
1122 case DA7213_STATUS1
:
1123 case DA7213_PLL_STATUS
:
1124 case DA7213_AUX_L_GAIN_STATUS
:
1125 case DA7213_AUX_R_GAIN_STATUS
:
1126 case DA7213_MIC_1_GAIN_STATUS
:
1127 case DA7213_MIC_2_GAIN_STATUS
:
1128 case DA7213_MIXIN_L_GAIN_STATUS
:
1129 case DA7213_MIXIN_R_GAIN_STATUS
:
1130 case DA7213_ADC_L_GAIN_STATUS
:
1131 case DA7213_ADC_R_GAIN_STATUS
:
1132 case DA7213_DAC_L_GAIN_STATUS
:
1133 case DA7213_DAC_R_GAIN_STATUS
:
1134 case DA7213_HP_L_GAIN_STATUS
:
1135 case DA7213_HP_R_GAIN_STATUS
:
1136 case DA7213_LINE_GAIN_STATUS
:
1137 case DA7213_ALC_CTRL1
:
1138 case DA7213_ALC_OFFSET_AUTO_M_L
:
1139 case DA7213_ALC_OFFSET_AUTO_U_L
:
1140 case DA7213_ALC_OFFSET_AUTO_M_R
:
1141 case DA7213_ALC_OFFSET_AUTO_U_R
:
1142 case DA7213_ALC_CIC_OP_LVL_DATA
:
1149 static int da7213_hw_params(struct snd_pcm_substream
*substream
,
1150 struct snd_pcm_hw_params
*params
,
1151 struct snd_soc_dai
*dai
)
1153 struct snd_soc_codec
*codec
= dai
->codec
;
1157 /* Set DAI format */
1158 switch (params_width(params
)) {
1160 dai_ctrl
|= DA7213_DAI_WORD_LENGTH_S16_LE
;
1163 dai_ctrl
|= DA7213_DAI_WORD_LENGTH_S20_LE
;
1166 dai_ctrl
|= DA7213_DAI_WORD_LENGTH_S24_LE
;
1169 dai_ctrl
|= DA7213_DAI_WORD_LENGTH_S32_LE
;
1175 /* Set sampling rate */
1176 switch (params_rate(params
)) {
1178 fs
= DA7213_SR_8000
;
1181 fs
= DA7213_SR_11025
;
1184 fs
= DA7213_SR_12000
;
1187 fs
= DA7213_SR_16000
;
1190 fs
= DA7213_SR_22050
;
1193 fs
= DA7213_SR_32000
;
1196 fs
= DA7213_SR_44100
;
1199 fs
= DA7213_SR_48000
;
1202 fs
= DA7213_SR_88200
;
1205 fs
= DA7213_SR_96000
;
1211 snd_soc_update_bits(codec
, DA7213_DAI_CTRL
, DA7213_DAI_WORD_LENGTH_MASK
,
1213 snd_soc_write(codec
, DA7213_SR
, fs
);
1218 static int da7213_set_dai_fmt(struct snd_soc_dai
*codec_dai
, unsigned int fmt
)
1220 struct snd_soc_codec
*codec
= codec_dai
->codec
;
1221 struct da7213_priv
*da7213
= snd_soc_codec_get_drvdata(codec
);
1222 u8 dai_clk_mode
= 0, dai_ctrl
= 0;
1224 /* Set master/slave mode */
1225 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
1226 case SND_SOC_DAIFMT_CBM_CFM
:
1227 da7213
->master
= true;
1229 case SND_SOC_DAIFMT_CBS_CFS
:
1230 da7213
->master
= false;
1236 /* Set clock normal/inverted */
1237 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
1238 case SND_SOC_DAIFMT_NB_NF
:
1240 case SND_SOC_DAIFMT_NB_IF
:
1241 dai_clk_mode
|= DA7213_DAI_WCLK_POL_INV
;
1243 case SND_SOC_DAIFMT_IB_NF
:
1244 dai_clk_mode
|= DA7213_DAI_CLK_POL_INV
;
1246 case SND_SOC_DAIFMT_IB_IF
:
1247 dai_clk_mode
|= DA7213_DAI_WCLK_POL_INV
| DA7213_DAI_CLK_POL_INV
;
1253 /* Only I2S is supported */
1254 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
1255 case SND_SOC_DAIFMT_I2S
:
1256 dai_ctrl
|= DA7213_DAI_FORMAT_I2S_MODE
;
1258 case SND_SOC_DAIFMT_LEFT_J
:
1259 dai_ctrl
|= DA7213_DAI_FORMAT_LEFT_J
;
1261 case SND_SOC_DAIFMT_RIGHT_J
:
1262 dai_ctrl
|= DA7213_DAI_FORMAT_RIGHT_J
;
1268 /* By default only 64 BCLK per WCLK is supported */
1269 dai_clk_mode
|= DA7213_DAI_BCLKS_PER_WCLK_64
;
1271 snd_soc_write(codec
, DA7213_DAI_CLK_MODE
, dai_clk_mode
);
1272 snd_soc_update_bits(codec
, DA7213_DAI_CTRL
, DA7213_DAI_FORMAT_MASK
,
1278 static int da7213_mute(struct snd_soc_dai
*dai
, int mute
)
1280 struct snd_soc_codec
*codec
= dai
->codec
;
1283 snd_soc_update_bits(codec
, DA7213_DAC_L_CTRL
,
1284 DA7213_MUTE_EN
, DA7213_MUTE_EN
);
1285 snd_soc_update_bits(codec
, DA7213_DAC_R_CTRL
,
1286 DA7213_MUTE_EN
, DA7213_MUTE_EN
);
1288 snd_soc_update_bits(codec
, DA7213_DAC_L_CTRL
,
1290 snd_soc_update_bits(codec
, DA7213_DAC_R_CTRL
,
1297 #define DA7213_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
1298 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
1300 static int da7213_set_dai_sysclk(struct snd_soc_dai
*codec_dai
,
1301 int clk_id
, unsigned int freq
, int dir
)
1303 struct snd_soc_codec
*codec
= codec_dai
->codec
;
1304 struct da7213_priv
*da7213
= snd_soc_codec_get_drvdata(codec
);
1307 if ((da7213
->clk_src
== clk_id
) && (da7213
->mclk_rate
== freq
))
1310 if (((freq
< 5000000) && (freq
!= 32768)) || (freq
> 54000000)) {
1311 dev_err(codec_dai
->dev
, "Unsupported MCLK value %d\n",
1317 case DA7213_CLKSRC_MCLK
:
1318 snd_soc_update_bits(codec
, DA7213_PLL_CTRL
,
1319 DA7213_PLL_MCLK_SQR_EN
, 0);
1321 case DA7213_CLKSRC_MCLK_SQR
:
1322 snd_soc_update_bits(codec
, DA7213_PLL_CTRL
,
1323 DA7213_PLL_MCLK_SQR_EN
,
1324 DA7213_PLL_MCLK_SQR_EN
);
1327 dev_err(codec_dai
->dev
, "Unknown clock source %d\n", clk_id
);
1331 da7213
->clk_src
= clk_id
;
1334 freq
= clk_round_rate(da7213
->mclk
, freq
);
1335 ret
= clk_set_rate(da7213
->mclk
, freq
);
1337 dev_err(codec_dai
->dev
, "Failed to set clock rate %d\n",
1343 da7213
->mclk_rate
= freq
;
1348 /* Supported PLL input frequencies are 32KHz, 5MHz - 54MHz. */
1349 static int da7213_set_dai_pll(struct snd_soc_dai
*codec_dai
, int pll_id
,
1350 int source
, unsigned int fref
, unsigned int fout
)
1352 struct snd_soc_codec
*codec
= codec_dai
->codec
;
1353 struct da7213_priv
*da7213
= snd_soc_codec_get_drvdata(codec
);
1355 u8 pll_ctrl
, indiv_bits
, indiv
;
1356 u8 pll_frac_top
, pll_frac_bot
, pll_integer
;
1360 /* Workout input divider based on MCLK rate */
1361 if (da7213
->mclk_rate
== 32768) {
1362 if (!da7213
->master
) {
1364 "32KHz only valid if codec is clock master\n");
1368 /* 32KHz PLL Mode */
1369 indiv_bits
= DA7213_PLL_INDIV_9_TO_18_MHZ
;
1370 indiv
= DA7213_PLL_INDIV_9_TO_18_MHZ_VAL
;
1371 source
= DA7213_SYSCLK_PLL_32KHZ
;
1375 if (da7213
->mclk_rate
< 5000000) {
1377 "PLL input clock %d below valid range\n",
1380 } else if (da7213
->mclk_rate
<= 9000000) {
1381 indiv_bits
= DA7213_PLL_INDIV_5_TO_9_MHZ
;
1382 indiv
= DA7213_PLL_INDIV_5_TO_9_MHZ_VAL
;
1383 } else if (da7213
->mclk_rate
<= 18000000) {
1384 indiv_bits
= DA7213_PLL_INDIV_9_TO_18_MHZ
;
1385 indiv
= DA7213_PLL_INDIV_9_TO_18_MHZ_VAL
;
1386 } else if (da7213
->mclk_rate
<= 36000000) {
1387 indiv_bits
= DA7213_PLL_INDIV_18_TO_36_MHZ
;
1388 indiv
= DA7213_PLL_INDIV_18_TO_36_MHZ_VAL
;
1389 } else if (da7213
->mclk_rate
<= 54000000) {
1390 indiv_bits
= DA7213_PLL_INDIV_36_TO_54_MHZ
;
1391 indiv
= DA7213_PLL_INDIV_36_TO_54_MHZ_VAL
;
1394 "PLL input clock %d above valid range\n",
1398 freq_ref
= (da7213
->mclk_rate
/ indiv
);
1401 pll_ctrl
= indiv_bits
;
1405 case DA7213_SYSCLK_MCLK
:
1406 snd_soc_update_bits(codec
, DA7213_PLL_CTRL
,
1407 DA7213_PLL_INDIV_MASK
|
1408 DA7213_PLL_MODE_MASK
, pll_ctrl
);
1410 case DA7213_SYSCLK_PLL
:
1412 case DA7213_SYSCLK_PLL_SRM
:
1413 pll_ctrl
|= DA7213_PLL_SRM_EN
;
1414 fout
= DA7213_PLL_FREQ_OUT_94310400
;
1416 case DA7213_SYSCLK_PLL_32KHZ
:
1417 if (da7213
->mclk_rate
!= 32768) {
1419 "32KHz mode only valid with 32KHz MCLK\n");
1423 pll_ctrl
|= DA7213_PLL_32K_MODE
| DA7213_PLL_SRM_EN
;
1424 fout
= DA7213_PLL_FREQ_OUT_94310400
;
1427 dev_err(codec
->dev
, "Invalid PLL config\n");
1431 /* Calculate dividers for PLL */
1432 pll_integer
= fout
/ freq_ref
;
1433 frac_div
= (u64
)(fout
% freq_ref
) * 8192ULL;
1434 do_div(frac_div
, freq_ref
);
1435 pll_frac_top
= (frac_div
>> DA7213_BYTE_SHIFT
) & DA7213_BYTE_MASK
;
1436 pll_frac_bot
= (frac_div
) & DA7213_BYTE_MASK
;
1438 /* Write PLL dividers */
1439 snd_soc_write(codec
, DA7213_PLL_FRAC_TOP
, pll_frac_top
);
1440 snd_soc_write(codec
, DA7213_PLL_FRAC_BOT
, pll_frac_bot
);
1441 snd_soc_write(codec
, DA7213_PLL_INTEGER
, pll_integer
);
1444 pll_ctrl
|= DA7213_PLL_EN
;
1445 snd_soc_update_bits(codec
, DA7213_PLL_CTRL
,
1446 DA7213_PLL_INDIV_MASK
| DA7213_PLL_MODE_MASK
,
1449 /* Assist 32KHz mode PLL lock */
1450 if (source
== DA7213_SYSCLK_PLL_32KHZ
) {
1451 snd_soc_write(codec
, 0xF0, 0x8B);
1452 snd_soc_write(codec
, 0xF1, 0x03);
1453 snd_soc_write(codec
, 0xF1, 0x01);
1454 snd_soc_write(codec
, 0xF0, 0x00);
1460 /* DAI operations */
1461 static const struct snd_soc_dai_ops da7213_dai_ops
= {
1462 .hw_params
= da7213_hw_params
,
1463 .set_fmt
= da7213_set_dai_fmt
,
1464 .set_sysclk
= da7213_set_dai_sysclk
,
1465 .set_pll
= da7213_set_dai_pll
,
1466 .digital_mute
= da7213_mute
,
1469 static struct snd_soc_dai_driver da7213_dai
= {
1470 .name
= "da7213-hifi",
1471 /* Playback Capabilities */
1473 .stream_name
= "Playback",
1476 .rates
= SNDRV_PCM_RATE_8000_96000
,
1477 .formats
= DA7213_FORMATS
,
1479 /* Capture Capabilities */
1481 .stream_name
= "Capture",
1484 .rates
= SNDRV_PCM_RATE_8000_96000
,
1485 .formats
= DA7213_FORMATS
,
1487 .ops
= &da7213_dai_ops
,
1488 .symmetric_rates
= 1,
1491 static int da7213_set_bias_level(struct snd_soc_codec
*codec
,
1492 enum snd_soc_bias_level level
)
1494 struct da7213_priv
*da7213
= snd_soc_codec_get_drvdata(codec
);
1498 case SND_SOC_BIAS_ON
:
1500 case SND_SOC_BIAS_PREPARE
:
1501 /* Enable MCLK for transition to ON state */
1502 if (snd_soc_codec_get_bias_level(codec
) == SND_SOC_BIAS_STANDBY
) {
1504 ret
= clk_prepare_enable(da7213
->mclk
);
1507 "Failed to enable mclk\n");
1513 case SND_SOC_BIAS_STANDBY
:
1514 if (snd_soc_codec_get_bias_level(codec
) == SND_SOC_BIAS_OFF
) {
1515 /* Enable VMID reference & master bias */
1516 snd_soc_update_bits(codec
, DA7213_REFERENCES
,
1517 DA7213_VMID_EN
| DA7213_BIAS_EN
,
1518 DA7213_VMID_EN
| DA7213_BIAS_EN
);
1522 clk_disable_unprepare(da7213
->mclk
);
1525 case SND_SOC_BIAS_OFF
:
1526 /* Disable VMID reference & master bias */
1527 snd_soc_update_bits(codec
, DA7213_REFERENCES
,
1528 DA7213_VMID_EN
| DA7213_BIAS_EN
, 0);
1534 #if defined(CONFIG_OF)
1536 static const struct of_device_id da7213_of_match
[] = {
1537 { .compatible
= "dlg,da7213", },
1540 MODULE_DEVICE_TABLE(of
, da7213_of_match
);
1544 static const struct acpi_device_id da7213_acpi_match
[] = {
1549 MODULE_DEVICE_TABLE(acpi
, da7213_acpi_match
);
1552 static enum da7213_micbias_voltage
1553 da7213_of_micbias_lvl(struct snd_soc_codec
*codec
, u32 val
)
1557 return DA7213_MICBIAS_1_6V
;
1559 return DA7213_MICBIAS_2_2V
;
1561 return DA7213_MICBIAS_2_5V
;
1563 return DA7213_MICBIAS_3_0V
;
1565 dev_warn(codec
->dev
, "Invalid micbias level\n");
1566 return DA7213_MICBIAS_2_2V
;
1570 static enum da7213_dmic_data_sel
1571 da7213_of_dmic_data_sel(struct snd_soc_codec
*codec
, const char *str
)
1573 if (!strcmp(str
, "lrise_rfall")) {
1574 return DA7213_DMIC_DATA_LRISE_RFALL
;
1575 } else if (!strcmp(str
, "lfall_rrise")) {
1576 return DA7213_DMIC_DATA_LFALL_RRISE
;
1578 dev_warn(codec
->dev
, "Invalid DMIC data select type\n");
1579 return DA7213_DMIC_DATA_LRISE_RFALL
;
1583 static enum da7213_dmic_samplephase
1584 da7213_of_dmic_samplephase(struct snd_soc_codec
*codec
, const char *str
)
1586 if (!strcmp(str
, "on_clkedge")) {
1587 return DA7213_DMIC_SAMPLE_ON_CLKEDGE
;
1588 } else if (!strcmp(str
, "between_clkedge")) {
1589 return DA7213_DMIC_SAMPLE_BETWEEN_CLKEDGE
;
1591 dev_warn(codec
->dev
, "Invalid DMIC sample phase\n");
1592 return DA7213_DMIC_SAMPLE_ON_CLKEDGE
;
1596 static enum da7213_dmic_clk_rate
1597 da7213_of_dmic_clkrate(struct snd_soc_codec
*codec
, u32 val
)
1601 return DA7213_DMIC_CLK_1_5MHZ
;
1603 return DA7213_DMIC_CLK_3_0MHZ
;
1605 dev_warn(codec
->dev
, "Invalid DMIC clock rate\n");
1606 return DA7213_DMIC_CLK_1_5MHZ
;
1610 static struct da7213_platform_data
1611 *da7213_fw_to_pdata(struct snd_soc_codec
*codec
)
1613 struct device
*dev
= codec
->dev
;
1614 struct da7213_platform_data
*pdata
;
1618 pdata
= devm_kzalloc(codec
->dev
, sizeof(*pdata
), GFP_KERNEL
);
1620 dev_warn(codec
->dev
, "Failed to allocate memory for pdata\n");
1624 if (device_property_read_u32(dev
, "dlg,micbias1-lvl", &fw_val32
) >= 0)
1625 pdata
->micbias1_lvl
= da7213_of_micbias_lvl(codec
, fw_val32
);
1627 pdata
->micbias1_lvl
= DA7213_MICBIAS_2_2V
;
1629 if (device_property_read_u32(dev
, "dlg,micbias2-lvl", &fw_val32
) >= 0)
1630 pdata
->micbias2_lvl
= da7213_of_micbias_lvl(codec
, fw_val32
);
1632 pdata
->micbias2_lvl
= DA7213_MICBIAS_2_2V
;
1634 if (!device_property_read_string(dev
, "dlg,dmic-data-sel", &fw_str
))
1635 pdata
->dmic_data_sel
= da7213_of_dmic_data_sel(codec
, fw_str
);
1637 pdata
->dmic_data_sel
= DA7213_DMIC_DATA_LRISE_RFALL
;
1639 if (!device_property_read_string(dev
, "dlg,dmic-samplephase", &fw_str
))
1640 pdata
->dmic_samplephase
=
1641 da7213_of_dmic_samplephase(codec
, fw_str
);
1643 pdata
->dmic_samplephase
= DA7213_DMIC_SAMPLE_ON_CLKEDGE
;
1645 if (device_property_read_u32(dev
, "dlg,dmic-clkrate", &fw_val32
) >= 0)
1646 pdata
->dmic_clk_rate
= da7213_of_dmic_clkrate(codec
, fw_val32
);
1648 pdata
->dmic_clk_rate
= DA7213_DMIC_CLK_3_0MHZ
;
1654 static int da7213_probe(struct snd_soc_codec
*codec
)
1656 struct da7213_priv
*da7213
= snd_soc_codec_get_drvdata(codec
);
1658 /* Default to using ALC auto offset calibration mode. */
1659 snd_soc_update_bits(codec
, DA7213_ALC_CTRL1
,
1660 DA7213_ALC_CALIB_MODE_MAN
, 0);
1661 da7213
->alc_calib_auto
= true;
1663 /* Default PC counter to free-running */
1664 snd_soc_update_bits(codec
, DA7213_PC_COUNT
, DA7213_PC_FREERUN_MASK
,
1665 DA7213_PC_FREERUN_MASK
);
1667 /* Enable all Gain Ramps */
1668 snd_soc_update_bits(codec
, DA7213_AUX_L_CTRL
,
1669 DA7213_GAIN_RAMP_EN
, DA7213_GAIN_RAMP_EN
);
1670 snd_soc_update_bits(codec
, DA7213_AUX_R_CTRL
,
1671 DA7213_GAIN_RAMP_EN
, DA7213_GAIN_RAMP_EN
);
1672 snd_soc_update_bits(codec
, DA7213_MIXIN_L_CTRL
,
1673 DA7213_GAIN_RAMP_EN
, DA7213_GAIN_RAMP_EN
);
1674 snd_soc_update_bits(codec
, DA7213_MIXIN_R_CTRL
,
1675 DA7213_GAIN_RAMP_EN
, DA7213_GAIN_RAMP_EN
);
1676 snd_soc_update_bits(codec
, DA7213_ADC_L_CTRL
,
1677 DA7213_GAIN_RAMP_EN
, DA7213_GAIN_RAMP_EN
);
1678 snd_soc_update_bits(codec
, DA7213_ADC_R_CTRL
,
1679 DA7213_GAIN_RAMP_EN
, DA7213_GAIN_RAMP_EN
);
1680 snd_soc_update_bits(codec
, DA7213_DAC_L_CTRL
,
1681 DA7213_GAIN_RAMP_EN
, DA7213_GAIN_RAMP_EN
);
1682 snd_soc_update_bits(codec
, DA7213_DAC_R_CTRL
,
1683 DA7213_GAIN_RAMP_EN
, DA7213_GAIN_RAMP_EN
);
1684 snd_soc_update_bits(codec
, DA7213_HP_L_CTRL
,
1685 DA7213_GAIN_RAMP_EN
, DA7213_GAIN_RAMP_EN
);
1686 snd_soc_update_bits(codec
, DA7213_HP_R_CTRL
,
1687 DA7213_GAIN_RAMP_EN
, DA7213_GAIN_RAMP_EN
);
1688 snd_soc_update_bits(codec
, DA7213_LINE_CTRL
,
1689 DA7213_GAIN_RAMP_EN
, DA7213_GAIN_RAMP_EN
);
1692 * There are two separate control bits for input and output mixers as
1693 * well as headphone and line outs.
1694 * One to enable corresponding amplifier and other to enable its
1695 * output. As amplifier bits are related to power control, they are
1696 * being managed by DAPM while other (non power related) bits are
1699 snd_soc_update_bits(codec
, DA7213_MIXIN_L_CTRL
,
1700 DA7213_MIXIN_MIX_EN
, DA7213_MIXIN_MIX_EN
);
1701 snd_soc_update_bits(codec
, DA7213_MIXIN_R_CTRL
,
1702 DA7213_MIXIN_MIX_EN
, DA7213_MIXIN_MIX_EN
);
1704 snd_soc_update_bits(codec
, DA7213_MIXOUT_L_CTRL
,
1705 DA7213_MIXOUT_MIX_EN
, DA7213_MIXOUT_MIX_EN
);
1706 snd_soc_update_bits(codec
, DA7213_MIXOUT_R_CTRL
,
1707 DA7213_MIXOUT_MIX_EN
, DA7213_MIXOUT_MIX_EN
);
1709 snd_soc_update_bits(codec
, DA7213_HP_L_CTRL
,
1710 DA7213_HP_AMP_OE
, DA7213_HP_AMP_OE
);
1711 snd_soc_update_bits(codec
, DA7213_HP_R_CTRL
,
1712 DA7213_HP_AMP_OE
, DA7213_HP_AMP_OE
);
1714 snd_soc_update_bits(codec
, DA7213_LINE_CTRL
,
1715 DA7213_LINE_AMP_OE
, DA7213_LINE_AMP_OE
);
1717 /* Handle DT/Platform data */
1718 da7213
->pdata
= dev_get_platdata(codec
->dev
);
1720 da7213
->pdata
= da7213_fw_to_pdata(codec
);
1722 /* Set platform data values */
1723 if (da7213
->pdata
) {
1724 struct da7213_platform_data
*pdata
= da7213
->pdata
;
1725 u8 micbias_lvl
= 0, dmic_cfg
= 0;
1727 /* Set Mic Bias voltages */
1728 switch (pdata
->micbias1_lvl
) {
1729 case DA7213_MICBIAS_1_6V
:
1730 case DA7213_MICBIAS_2_2V
:
1731 case DA7213_MICBIAS_2_5V
:
1732 case DA7213_MICBIAS_3_0V
:
1733 micbias_lvl
|= (pdata
->micbias1_lvl
<<
1734 DA7213_MICBIAS1_LEVEL_SHIFT
);
1737 switch (pdata
->micbias2_lvl
) {
1738 case DA7213_MICBIAS_1_6V
:
1739 case DA7213_MICBIAS_2_2V
:
1740 case DA7213_MICBIAS_2_5V
:
1741 case DA7213_MICBIAS_3_0V
:
1742 micbias_lvl
|= (pdata
->micbias2_lvl
<<
1743 DA7213_MICBIAS2_LEVEL_SHIFT
);
1746 snd_soc_update_bits(codec
, DA7213_MICBIAS_CTRL
,
1747 DA7213_MICBIAS1_LEVEL_MASK
|
1748 DA7213_MICBIAS2_LEVEL_MASK
, micbias_lvl
);
1750 /* Set DMIC configuration */
1751 switch (pdata
->dmic_data_sel
) {
1752 case DA7213_DMIC_DATA_LFALL_RRISE
:
1753 case DA7213_DMIC_DATA_LRISE_RFALL
:
1754 dmic_cfg
|= (pdata
->dmic_data_sel
<<
1755 DA7213_DMIC_DATA_SEL_SHIFT
);
1758 switch (pdata
->dmic_samplephase
) {
1759 case DA7213_DMIC_SAMPLE_ON_CLKEDGE
:
1760 case DA7213_DMIC_SAMPLE_BETWEEN_CLKEDGE
:
1761 dmic_cfg
|= (pdata
->dmic_samplephase
<<
1762 DA7213_DMIC_SAMPLEPHASE_SHIFT
);
1765 switch (pdata
->dmic_clk_rate
) {
1766 case DA7213_DMIC_CLK_3_0MHZ
:
1767 case DA7213_DMIC_CLK_1_5MHZ
:
1768 dmic_cfg
|= (pdata
->dmic_clk_rate
<<
1769 DA7213_DMIC_CLK_RATE_SHIFT
);
1772 snd_soc_update_bits(codec
, DA7213_MIC_CONFIG
,
1773 DA7213_DMIC_DATA_SEL_MASK
|
1774 DA7213_DMIC_SAMPLEPHASE_MASK
|
1775 DA7213_DMIC_CLK_RATE_MASK
, dmic_cfg
);
1778 /* Check if MCLK provided */
1779 da7213
->mclk
= devm_clk_get(codec
->dev
, "mclk");
1780 if (IS_ERR(da7213
->mclk
)) {
1781 if (PTR_ERR(da7213
->mclk
) != -ENOENT
)
1782 return PTR_ERR(da7213
->mclk
);
1784 da7213
->mclk
= NULL
;
1790 static const struct snd_soc_codec_driver soc_codec_dev_da7213
= {
1791 .probe
= da7213_probe
,
1792 .set_bias_level
= da7213_set_bias_level
,
1794 .component_driver
= {
1795 .controls
= da7213_snd_controls
,
1796 .num_controls
= ARRAY_SIZE(da7213_snd_controls
),
1797 .dapm_widgets
= da7213_dapm_widgets
,
1798 .num_dapm_widgets
= ARRAY_SIZE(da7213_dapm_widgets
),
1799 .dapm_routes
= da7213_audio_map
,
1800 .num_dapm_routes
= ARRAY_SIZE(da7213_audio_map
),
1804 static const struct regmap_config da7213_regmap_config
= {
1808 .reg_defaults
= da7213_reg_defaults
,
1809 .num_reg_defaults
= ARRAY_SIZE(da7213_reg_defaults
),
1810 .volatile_reg
= da7213_volatile_register
,
1811 .cache_type
= REGCACHE_RBTREE
,
1814 static int da7213_i2c_probe(struct i2c_client
*i2c
,
1815 const struct i2c_device_id
*id
)
1817 struct da7213_priv
*da7213
;
1820 da7213
= devm_kzalloc(&i2c
->dev
, sizeof(struct da7213_priv
),
1825 i2c_set_clientdata(i2c
, da7213
);
1827 da7213
->regmap
= devm_regmap_init_i2c(i2c
, &da7213_regmap_config
);
1828 if (IS_ERR(da7213
->regmap
)) {
1829 ret
= PTR_ERR(da7213
->regmap
);
1830 dev_err(&i2c
->dev
, "regmap_init() failed: %d\n", ret
);
1834 ret
= snd_soc_register_codec(&i2c
->dev
,
1835 &soc_codec_dev_da7213
, &da7213_dai
, 1);
1837 dev_err(&i2c
->dev
, "Failed to register da7213 codec: %d\n",
1843 static int da7213_remove(struct i2c_client
*client
)
1845 snd_soc_unregister_codec(&client
->dev
);
1849 static const struct i2c_device_id da7213_i2c_id
[] = {
1853 MODULE_DEVICE_TABLE(i2c
, da7213_i2c_id
);
1855 /* I2C codec control layer */
1856 static struct i2c_driver da7213_i2c_driver
= {
1859 .of_match_table
= of_match_ptr(da7213_of_match
),
1860 .acpi_match_table
= ACPI_PTR(da7213_acpi_match
),
1862 .probe
= da7213_i2c_probe
,
1863 .remove
= da7213_remove
,
1864 .id_table
= da7213_i2c_id
,
1867 module_i2c_driver(da7213_i2c_driver
);
1869 MODULE_DESCRIPTION("ASoC DA7213 Codec driver");
1870 MODULE_AUTHOR("Adam Thomson <Adam.Thomson.Opensource@diasemi.com>");
1871 MODULE_LICENSE("GPL");