2 * DA7210 ALSA Soc codec driver
4 * Copyright (c) 2009 Dialog Semiconductor
5 * Written by David Chen <Dajun.chen@diasemi.com>
7 * Copyright (C) 2009 Renesas Solutions Corp.
8 * Cleanups by Kuninori Morimoto <morimoto.kuninori@renesas.com>
10 * Tested on SuperH Ecovec24 board with S16/S24 LE in 48KHz using I2S
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
18 #include <linux/delay.h>
19 #include <linux/i2c.h>
20 #include <linux/regmap.h>
21 #include <linux/slab.h>
22 #include <linux/module.h>
23 #include <sound/pcm.h>
24 #include <sound/pcm_params.h>
25 #include <sound/soc.h>
26 #include <sound/initval.h>
27 #include <sound/tlv.h>
29 /* DA7210 register space */
30 #define DA7210_CONTROL 0x01
31 #define DA7210_STATUS 0x02
32 #define DA7210_STARTUP1 0x03
33 #define DA7210_STARTUP2 0x04
34 #define DA7210_STARTUP3 0x05
35 #define DA7210_MIC_L 0x07
36 #define DA7210_MIC_R 0x08
37 #define DA7210_AUX1_L 0x09
38 #define DA7210_AUX1_R 0x0A
39 #define DA7210_AUX2 0x0B
40 #define DA7210_IN_GAIN 0x0C
41 #define DA7210_INMIX_L 0x0D
42 #define DA7210_INMIX_R 0x0E
43 #define DA7210_ADC_HPF 0x0F
44 #define DA7210_ADC 0x10
45 #define DA7210_ADC_EQ1_2 0X11
46 #define DA7210_ADC_EQ3_4 0x12
47 #define DA7210_ADC_EQ5 0x13
48 #define DA7210_DAC_HPF 0x14
49 #define DA7210_DAC_L 0x15
50 #define DA7210_DAC_R 0x16
51 #define DA7210_DAC_SEL 0x17
52 #define DA7210_SOFTMUTE 0x18
53 #define DA7210_DAC_EQ1_2 0x19
54 #define DA7210_DAC_EQ3_4 0x1A
55 #define DA7210_DAC_EQ5 0x1B
56 #define DA7210_OUTMIX_L 0x1C
57 #define DA7210_OUTMIX_R 0x1D
58 #define DA7210_OUT1_L 0x1E
59 #define DA7210_OUT1_R 0x1F
60 #define DA7210_OUT2 0x20
61 #define DA7210_HP_L_VOL 0x21
62 #define DA7210_HP_R_VOL 0x22
63 #define DA7210_HP_CFG 0x23
64 #define DA7210_ZERO_CROSS 0x24
65 #define DA7210_DAI_SRC_SEL 0x25
66 #define DA7210_DAI_CFG1 0x26
67 #define DA7210_DAI_CFG3 0x28
68 #define DA7210_PLL_DIV1 0x29
69 #define DA7210_PLL_DIV2 0x2A
70 #define DA7210_PLL_DIV3 0x2B
71 #define DA7210_PLL 0x2C
72 #define DA7210_ALC_MAX 0x83
73 #define DA7210_ALC_MIN 0x84
74 #define DA7210_ALC_NOIS 0x85
75 #define DA7210_ALC_ATT 0x86
76 #define DA7210_ALC_REL 0x87
77 #define DA7210_ALC_DEL 0x88
78 #define DA7210_A_HID_UNLOCK 0x8A
79 #define DA7210_A_TEST_UNLOCK 0x8B
80 #define DA7210_A_PLL1 0x90
81 #define DA7210_A_CP_MODE 0xA7
83 /* STARTUP1 bit fields */
84 #define DA7210_SC_MST_EN (1 << 0)
86 /* MIC_L bit fields */
87 #define DA7210_MICBIAS_EN (1 << 6)
88 #define DA7210_MIC_L_EN (1 << 7)
90 /* MIC_R bit fields */
91 #define DA7210_MIC_R_EN (1 << 7)
93 /* INMIX_L bit fields */
94 #define DA7210_IN_L_EN (1 << 7)
96 /* INMIX_R bit fields */
97 #define DA7210_IN_R_EN (1 << 7)
100 #define DA7210_ADC_ALC_EN (1 << 0)
101 #define DA7210_ADC_L_EN (1 << 3)
102 #define DA7210_ADC_R_EN (1 << 7)
104 /* DAC/ADC HPF fields */
105 #define DA7210_VOICE_F0_MASK (0x7 << 4)
106 #define DA7210_VOICE_F0_25 (1 << 4)
107 #define DA7210_VOICE_EN (1 << 7)
109 /* DAC_SEL bit fields */
110 #define DA7210_DAC_L_SRC_DAI_L (4 << 0)
111 #define DA7210_DAC_L_EN (1 << 3)
112 #define DA7210_DAC_R_SRC_DAI_R (5 << 4)
113 #define DA7210_DAC_R_EN (1 << 7)
115 /* OUTMIX_L bit fields */
116 #define DA7210_OUT_L_EN (1 << 7)
118 /* OUTMIX_R bit fields */
119 #define DA7210_OUT_R_EN (1 << 7)
121 /* HP_CFG bit fields */
122 #define DA7210_HP_2CAP_MODE (1 << 1)
123 #define DA7210_HP_SENSE_EN (1 << 2)
124 #define DA7210_HP_L_EN (1 << 3)
125 #define DA7210_HP_MODE (1 << 6)
126 #define DA7210_HP_R_EN (1 << 7)
128 /* DAI_SRC_SEL bit fields */
129 #define DA7210_DAI_OUT_L_SRC (6 << 0)
130 #define DA7210_DAI_OUT_R_SRC (7 << 4)
132 /* DAI_CFG1 bit fields */
133 #define DA7210_DAI_WORD_S16_LE (0 << 0)
134 #define DA7210_DAI_WORD_S20_3LE (1 << 0)
135 #define DA7210_DAI_WORD_S24_LE (2 << 0)
136 #define DA7210_DAI_WORD_S32_LE (3 << 0)
137 #define DA7210_DAI_FLEN_64BIT (1 << 2)
138 #define DA7210_DAI_MODE_SLAVE (0 << 7)
139 #define DA7210_DAI_MODE_MASTER (1 << 7)
141 /* DAI_CFG3 bit fields */
142 #define DA7210_DAI_FORMAT_I2SMODE (0 << 0)
143 #define DA7210_DAI_FORMAT_LEFT_J (1 << 0)
144 #define DA7210_DAI_FORMAT_RIGHT_J (2 << 0)
145 #define DA7210_DAI_OE (1 << 3)
146 #define DA7210_DAI_EN (1 << 7)
148 /*PLL_DIV3 bit fields */
149 #define DA7210_MCLK_RANGE_10_20_MHZ (1 << 4)
150 #define DA7210_PLL_BYP (1 << 6)
153 #define DA7210_PLL_FS_MASK (0xF << 0)
154 #define DA7210_PLL_FS_8000 (0x1 << 0)
155 #define DA7210_PLL_FS_11025 (0x2 << 0)
156 #define DA7210_PLL_FS_12000 (0x3 << 0)
157 #define DA7210_PLL_FS_16000 (0x5 << 0)
158 #define DA7210_PLL_FS_22050 (0x6 << 0)
159 #define DA7210_PLL_FS_24000 (0x7 << 0)
160 #define DA7210_PLL_FS_32000 (0x9 << 0)
161 #define DA7210_PLL_FS_44100 (0xA << 0)
162 #define DA7210_PLL_FS_48000 (0xB << 0)
163 #define DA7210_PLL_FS_88200 (0xE << 0)
164 #define DA7210_PLL_FS_96000 (0xF << 0)
165 #define DA7210_PLL_EN (0x1 << 7)
167 /* SOFTMUTE bit fields */
168 #define DA7210_RAMP_EN (1 << 6)
170 /* CONTROL bit fields */
171 #define DA7210_NOISE_SUP_EN (1 << 3)
173 /* IN_GAIN bit fields */
174 #define DA7210_INPGA_L_VOL (0x0F << 0)
175 #define DA7210_INPGA_R_VOL (0xF0 << 0)
177 /* ZERO_CROSS bit fields */
178 #define DA7210_AUX1_L_ZC (1 << 0)
179 #define DA7210_AUX1_R_ZC (1 << 1)
180 #define DA7210_HP_L_ZC (1 << 6)
181 #define DA7210_HP_R_ZC (1 << 7)
183 /* AUX1_L bit fields */
184 #define DA7210_AUX1_L_VOL (0x3F << 0)
185 #define DA7210_AUX1_L_EN (1 << 7)
187 /* AUX1_R bit fields */
188 #define DA7210_AUX1_R_VOL (0x3F << 0)
189 #define DA7210_AUX1_R_EN (1 << 7)
191 /* AUX2 bit fields */
192 #define DA7210_AUX2_EN (1 << 3)
194 /* Minimum INPGA and AUX1 volume to enable noise suppression */
195 #define DA7210_INPGA_MIN_VOL_NS 0x0A /* 10.5dB */
196 #define DA7210_AUX1_MIN_VOL_NS 0x35 /* 6dB */
198 /* OUT1_L bit fields */
199 #define DA7210_OUT1_L_EN (1 << 7)
201 /* OUT1_R bit fields */
202 #define DA7210_OUT1_R_EN (1 << 7)
204 /* OUT2 bit fields */
205 #define DA7210_OUT2_OUTMIX_R (1 << 5)
206 #define DA7210_OUT2_OUTMIX_L (1 << 6)
207 #define DA7210_OUT2_EN (1 << 7)
209 #define DA7210_VERSION "0.0.1"
214 * max : 0x3F (+15.0 dB)
216 * min : 0x11 (-54.0 dB)
218 * reserved : 0x00 - 0x0F
220 * Reserved area are considered as "mute".
222 static const unsigned int hp_out_tlv[] = {
223 TLV_DB_RANGE_HEAD(2),
224 0x0, 0x10, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE, 0, 1),
225 /* -54 dB to +15 dB */
226 0x11, 0x3f, TLV_DB_SCALE_ITEM(-5400, 150, 0),
229 static const unsigned int lineout_vol_tlv[] = {
230 TLV_DB_RANGE_HEAD(2),
231 0x0, 0x10, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE, 0, 1),
233 0x11, 0x3f, TLV_DB_SCALE_ITEM(-5400, 150, 0)
236 static const unsigned int mono_vol_tlv[] = {
237 TLV_DB_RANGE_HEAD(2),
238 0x0, 0x2, TLV_DB_SCALE_ITEM(-1800, 0, 1),
240 0x3, 0x7, TLV_DB_SCALE_ITEM(-1800, 600, 0)
243 static const unsigned int aux1_vol_tlv[] = {
244 TLV_DB_RANGE_HEAD(2),
245 0x0, 0x10, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE, 0, 1),
247 0x11, 0x3f, TLV_DB_SCALE_ITEM(-4800, 150, 0)
250 static const DECLARE_TLV_DB_SCALE(eq_gain_tlv, -1050, 150, 0);
251 static const DECLARE_TLV_DB_SCALE(adc_eq_master_gain_tlv, -1800, 600, 1);
252 static const DECLARE_TLV_DB_SCALE(dac_gain_tlv, -7725, 75, 0);
253 static const DECLARE_TLV_DB_SCALE(mic_vol_tlv, -600, 600, 0);
254 static const DECLARE_TLV_DB_SCALE(aux2_vol_tlv, -600, 600, 0);
255 static const DECLARE_TLV_DB_SCALE(inpga_gain_tlv, -450, 150, 0);
257 /* ADC and DAC high pass filter f0 value */
258 static const char * const da7210_hpf_cutoff_txt[] = {
259 "Fs/8192*pi", "Fs/4096*pi", "Fs/2048*pi", "Fs/1024*pi"
262 static const struct soc_enum da7210_dac_hpf_cutoff =
263 SOC_ENUM_SINGLE(DA7210_DAC_HPF, 0, 4, da7210_hpf_cutoff_txt);
265 static const struct soc_enum da7210_adc_hpf_cutoff =
266 SOC_ENUM_SINGLE(DA7210_ADC_HPF, 0, 4, da7210_hpf_cutoff_txt);
268 /* ADC and DAC voice (8kHz) high pass cutoff value */
269 static const char * const da7210_vf_cutoff_txt[] = {
270 "2.5Hz", "25Hz", "50Hz", "100Hz", "150Hz", "200Hz", "300Hz", "400Hz"
273 static const struct soc_enum da7210_dac_vf_cutoff =
274 SOC_ENUM_SINGLE(DA7210_DAC_HPF, 4, 8, da7210_vf_cutoff_txt);
276 static const struct soc_enum da7210_adc_vf_cutoff =
277 SOC_ENUM_SINGLE(DA7210_ADC_HPF, 4, 8, da7210_vf_cutoff_txt);
279 static const char *da7210_hp_mode_txt[] = {
283 static const struct soc_enum da7210_hp_mode_sel =
284 SOC_ENUM_SINGLE(DA7210_HP_CFG, 0, 2, da7210_hp_mode_txt);
286 /* ALC can be enabled only if noise suppression is disabled */
287 static int da7210_put_alc_sw(struct snd_kcontrol *kcontrol,
288 struct snd_ctl_elem_value *ucontrol)
290 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
292 if (ucontrol->value.integer.value[0]) {
293 /* Check if noise suppression is enabled */
294 if (snd_soc_read(codec, DA7210_CONTROL) & DA7210_NOISE_SUP_EN) {
296 "Disable noise suppression to enable ALC\n");
300 /* If all conditions are met or we are actually disabling ALC */
301 return snd_soc_put_volsw(kcontrol, ucontrol);
304 /* Noise suppression can be enabled only if following conditions are met
306 * ZC enabled for HP and AUX1 PGA
307 * INPGA_L_VOL and INPGA_R_VOL >= 10.5 dB
308 * AUX1_L_VOL and AUX1_R_VOL >= 6 dB
310 static int da7210_put_noise_sup_sw(struct snd_kcontrol *kcontrol,
311 struct snd_ctl_elem_value *ucontrol)
313 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
316 if (ucontrol->value.integer.value[0]) {
317 /* Check if ALC is enabled */
318 if (snd_soc_read(codec, DA7210_ADC) & DA7210_ADC_ALC_EN)
321 /* Check ZC for HP and AUX1 PGA */
322 if ((snd_soc_read(codec, DA7210_ZERO_CROSS) &
323 (DA7210_AUX1_L_ZC | DA7210_AUX1_R_ZC | DA7210_HP_L_ZC |
324 DA7210_HP_R_ZC)) != (DA7210_AUX1_L_ZC |
325 DA7210_AUX1_R_ZC | DA7210_HP_L_ZC | DA7210_HP_R_ZC))
328 /* Check INPGA_L_VOL and INPGA_R_VOL */
329 val = snd_soc_read(codec, DA7210_IN_GAIN);
330 if (((val & DA7210_INPGA_L_VOL) < DA7210_INPGA_MIN_VOL_NS) ||
331 (((val & DA7210_INPGA_R_VOL) >> 4) <
332 DA7210_INPGA_MIN_VOL_NS))
335 /* Check AUX1_L_VOL and AUX1_R_VOL */
336 if (((snd_soc_read(codec, DA7210_AUX1_L) & DA7210_AUX1_L_VOL) <
337 DA7210_AUX1_MIN_VOL_NS) ||
338 ((snd_soc_read(codec, DA7210_AUX1_R) & DA7210_AUX1_R_VOL) <
339 DA7210_AUX1_MIN_VOL_NS))
342 /* If all conditions are met or we are actually disabling Noise sup */
343 return snd_soc_put_volsw(kcontrol, ucontrol);
349 static const struct snd_kcontrol_new da7210_snd_controls[] = {
351 SOC_DOUBLE_R_TLV("HeadPhone Playback Volume",
352 DA7210_HP_L_VOL, DA7210_HP_R_VOL,
353 0, 0x3F, 0, hp_out_tlv),
354 SOC_DOUBLE_R_TLV("Digital Playback Volume",
355 DA7210_DAC_L, DA7210_DAC_R,
356 0, 0x77, 1, dac_gain_tlv),
357 SOC_DOUBLE_R_TLV("Lineout Playback Volume",
358 DA7210_OUT1_L, DA7210_OUT1_R,
359 0, 0x3f, 0, lineout_vol_tlv),
360 SOC_SINGLE_TLV("Mono Playback Volume", DA7210_OUT2, 0, 0x7, 0,
363 SOC_DOUBLE_R_TLV("Mic Capture Volume",
364 DA7210_MIC_L, DA7210_MIC_R,
365 0, 0x5, 0, mic_vol_tlv),
366 SOC_DOUBLE_R_TLV("Aux1 Capture Volume",
367 DA7210_AUX1_L, DA7210_AUX1_R,
368 0, 0x3f, 0, aux1_vol_tlv),
369 SOC_SINGLE_TLV("Aux2 Capture Volume", DA7210_AUX2, 0, 0x3, 0,
371 SOC_DOUBLE_TLV("In PGA Capture Volume", DA7210_IN_GAIN, 0, 4, 0xF, 0,
374 /* DAC Equalizer controls */
375 SOC_SINGLE("DAC EQ Switch", DA7210_DAC_EQ5, 7, 1, 0),
376 SOC_SINGLE_TLV("DAC EQ1 Volume", DA7210_DAC_EQ1_2, 0, 0xf, 1,
378 SOC_SINGLE_TLV("DAC EQ2 Volume", DA7210_DAC_EQ1_2, 4, 0xf, 1,
380 SOC_SINGLE_TLV("DAC EQ3 Volume", DA7210_DAC_EQ3_4, 0, 0xf, 1,
382 SOC_SINGLE_TLV("DAC EQ4 Volume", DA7210_DAC_EQ3_4, 4, 0xf, 1,
384 SOC_SINGLE_TLV("DAC EQ5 Volume", DA7210_DAC_EQ5, 0, 0xf, 1,
387 /* ADC Equalizer controls */
388 SOC_SINGLE("ADC EQ Switch", DA7210_ADC_EQ5, 7, 1, 0),
389 SOC_SINGLE_TLV("ADC EQ Master Volume", DA7210_ADC_EQ5, 4, 0x3,
390 1, adc_eq_master_gain_tlv),
391 SOC_SINGLE_TLV("ADC EQ1 Volume", DA7210_ADC_EQ1_2, 0, 0xf, 1,
393 SOC_SINGLE_TLV("ADC EQ2 Volume", DA7210_ADC_EQ1_2, 4, 0xf, 1,
395 SOC_SINGLE_TLV("ADC EQ3 Volume", DA7210_ADC_EQ3_4, 0, 0xf, 1,
397 SOC_SINGLE_TLV("ADC EQ4 Volume", DA7210_ADC_EQ3_4, 4, 0xf, 1,
399 SOC_SINGLE_TLV("ADC EQ5 Volume", DA7210_ADC_EQ5, 0, 0xf, 1,
402 SOC_SINGLE("DAC HPF Switch", DA7210_DAC_HPF, 3, 1, 0),
403 SOC_ENUM("DAC HPF Cutoff", da7210_dac_hpf_cutoff),
404 SOC_SINGLE("DAC Voice Mode Switch", DA7210_DAC_HPF, 7, 1, 0),
405 SOC_ENUM("DAC Voice Cutoff", da7210_dac_vf_cutoff),
407 SOC_SINGLE("ADC HPF Switch", DA7210_ADC_HPF, 3, 1, 0),
408 SOC_ENUM("ADC HPF Cutoff", da7210_adc_hpf_cutoff),
409 SOC_SINGLE("ADC Voice Mode Switch", DA7210_ADC_HPF, 7, 1, 0),
410 SOC_ENUM("ADC Voice Cutoff", da7210_adc_vf_cutoff),
413 SOC_DOUBLE_R("Mic Capture Switch", DA7210_MIC_L, DA7210_MIC_R, 3, 1, 0),
414 SOC_SINGLE("Aux2 Capture Switch", DA7210_AUX2, 2, 1, 0),
415 SOC_DOUBLE("ADC Capture Switch", DA7210_ADC, 2, 6, 1, 0),
416 SOC_SINGLE("Digital Soft Mute Switch", DA7210_SOFTMUTE, 7, 1, 0),
417 SOC_SINGLE("Digital Soft Mute Rate", DA7210_SOFTMUTE, 0, 0x7, 0),
419 /* Zero cross controls */
420 SOC_DOUBLE("Aux1 ZC Switch", DA7210_ZERO_CROSS, 0, 1, 1, 0),
421 SOC_DOUBLE("In PGA ZC Switch", DA7210_ZERO_CROSS, 2, 3, 1, 0),
422 SOC_DOUBLE("Lineout ZC Switch", DA7210_ZERO_CROSS, 4, 5, 1, 0),
423 SOC_DOUBLE("Headphone ZC Switch", DA7210_ZERO_CROSS, 6, 7, 1, 0),
425 SOC_ENUM("Headphone Class", da7210_hp_mode_sel),
428 SOC_SINGLE_EXT("ALC Enable Switch", DA7210_ADC, 0, 1, 0,
429 snd_soc_get_volsw, da7210_put_alc_sw),
430 SOC_SINGLE("ALC Capture Max Volume", DA7210_ALC_MAX, 0, 0x3F, 0),
431 SOC_SINGLE("ALC Capture Min Volume", DA7210_ALC_MIN, 0, 0x3F, 0),
432 SOC_SINGLE("ALC Capture Noise Volume", DA7210_ALC_NOIS, 0, 0x3F, 0),
433 SOC_SINGLE("ALC Capture Attack Rate", DA7210_ALC_ATT, 0, 0xFF, 0),
434 SOC_SINGLE("ALC Capture Release Rate", DA7210_ALC_REL, 0, 0xFF, 0),
435 SOC_SINGLE("ALC Capture Release Delay", DA7210_ALC_DEL, 0, 0xFF, 0),
437 SOC_SINGLE_EXT("Noise Suppression Enable Switch", DA7210_CONTROL, 3, 1,
438 0, snd_soc_get_volsw, da7210_put_noise_sup_sw),
444 * Current DAPM implementation covers almost all codec components e.g. IOs,
445 * mixers, PGAs,ADC and DAC.
448 static const struct snd_kcontrol_new da7210_dapm_inmixl_controls[] = {
449 SOC_DAPM_SINGLE("Mic Left Switch", DA7210_INMIX_L, 0, 1, 0),
450 SOC_DAPM_SINGLE("Mic Right Switch", DA7210_INMIX_L, 1, 1, 0),
451 SOC_DAPM_SINGLE("Aux1 Left Switch", DA7210_INMIX_L, 2, 1, 0),
452 SOC_DAPM_SINGLE("Aux2 Switch", DA7210_INMIX_L, 3, 1, 0),
453 SOC_DAPM_SINGLE("Outmix Left Switch", DA7210_INMIX_L, 4, 1, 0),
457 static const struct snd_kcontrol_new da7210_dapm_inmixr_controls[] = {
458 SOC_DAPM_SINGLE("Mic Right Switch", DA7210_INMIX_R, 0, 1, 0),
459 SOC_DAPM_SINGLE("Mic Left Switch", DA7210_INMIX_R, 1, 1, 0),
460 SOC_DAPM_SINGLE("Aux1 Right Switch", DA7210_INMIX_R, 2, 1, 0),
461 SOC_DAPM_SINGLE("Aux2 Switch", DA7210_INMIX_R, 3, 1, 0),
462 SOC_DAPM_SINGLE("Outmix Right Switch", DA7210_INMIX_R, 4, 1, 0),
466 static const struct snd_kcontrol_new da7210_dapm_outmixl_controls[] = {
467 SOC_DAPM_SINGLE("Aux1 Left Switch", DA7210_OUTMIX_L, 0, 1, 0),
468 SOC_DAPM_SINGLE("Aux2 Switch", DA7210_OUTMIX_L, 1, 1, 0),
469 SOC_DAPM_SINGLE("INPGA Left Switch", DA7210_OUTMIX_L, 2, 1, 0),
470 SOC_DAPM_SINGLE("INPGA Right Switch", DA7210_OUTMIX_L, 3, 1, 0),
471 SOC_DAPM_SINGLE("DAC Left Switch", DA7210_OUTMIX_L, 4, 1, 0),
474 /* Out Mixer Right */
475 static const struct snd_kcontrol_new da7210_dapm_outmixr_controls[] = {
476 SOC_DAPM_SINGLE("Aux1 Right Switch", DA7210_OUTMIX_R, 0, 1, 0),
477 SOC_DAPM_SINGLE("Aux2 Switch", DA7210_OUTMIX_R, 1, 1, 0),
478 SOC_DAPM_SINGLE("INPGA Left Switch", DA7210_OUTMIX_R, 2, 1, 0),
479 SOC_DAPM_SINGLE("INPGA Right Switch", DA7210_OUTMIX_R, 3, 1, 0),
480 SOC_DAPM_SINGLE("DAC Right Switch", DA7210_OUTMIX_R, 4, 1, 0),
484 static const struct snd_kcontrol_new da7210_dapm_monomix_controls[] = {
485 SOC_DAPM_SINGLE("INPGA Right Switch", DA7210_OUT2, 3, 1, 0),
486 SOC_DAPM_SINGLE("INPGA Left Switch", DA7210_OUT2, 4, 1, 0),
487 SOC_DAPM_SINGLE("Outmix Right Switch", DA7210_OUT2, 5, 1, 0),
488 SOC_DAPM_SINGLE("Outmix Left Switch", DA7210_OUT2, 6, 1, 0),
492 static const struct snd_soc_dapm_widget da7210_dapm_widgets[] = {
495 SND_SOC_DAPM_INPUT("MICL"),
496 SND_SOC_DAPM_INPUT("MICR"),
497 SND_SOC_DAPM_INPUT("AUX1L"),
498 SND_SOC_DAPM_INPUT("AUX1R"),
499 SND_SOC_DAPM_INPUT("AUX2"),
502 SND_SOC_DAPM_PGA("Mic Left", DA7210_STARTUP3, 0, 1, NULL, 0),
503 SND_SOC_DAPM_PGA("Mic Right", DA7210_STARTUP3, 1, 1, NULL, 0),
504 SND_SOC_DAPM_PGA("Aux1 Left", DA7210_STARTUP3, 2, 1, NULL, 0),
505 SND_SOC_DAPM_PGA("Aux1 Right", DA7210_STARTUP3, 3, 1, NULL, 0),
506 SND_SOC_DAPM_PGA("Aux2 Mono", DA7210_STARTUP3, 4, 1, NULL, 0),
508 SND_SOC_DAPM_PGA("INPGA Left", DA7210_INMIX_L, 7, 0, NULL, 0),
509 SND_SOC_DAPM_PGA("INPGA Right", DA7210_INMIX_R, 7, 0, NULL, 0),
512 SND_SOC_DAPM_SUPPLY("Mic Bias", DA7210_MIC_L, 6, 0, NULL, 0),
515 SND_SOC_DAPM_MIXER("In Mixer Left", SND_SOC_NOPM, 0, 0,
516 &da7210_dapm_inmixl_controls[0],
517 ARRAY_SIZE(da7210_dapm_inmixl_controls)),
519 SND_SOC_DAPM_MIXER("In Mixer Right", SND_SOC_NOPM, 0, 0,
520 &da7210_dapm_inmixr_controls[0],
521 ARRAY_SIZE(da7210_dapm_inmixr_controls)),
524 SND_SOC_DAPM_ADC("ADC Left", "Capture", DA7210_STARTUP3, 5, 1),
525 SND_SOC_DAPM_ADC("ADC Right", "Capture", DA7210_STARTUP3, 6, 1),
529 SND_SOC_DAPM_DAC("DAC Left", "Playback", DA7210_STARTUP2, 5, 1),
530 SND_SOC_DAPM_DAC("DAC Right", "Playback", DA7210_STARTUP2, 6, 1),
533 SND_SOC_DAPM_MIXER("Out Mixer Left", SND_SOC_NOPM, 0, 0,
534 &da7210_dapm_outmixl_controls[0],
535 ARRAY_SIZE(da7210_dapm_outmixl_controls)),
537 SND_SOC_DAPM_MIXER("Out Mixer Right", SND_SOC_NOPM, 0, 0,
538 &da7210_dapm_outmixr_controls[0],
539 ARRAY_SIZE(da7210_dapm_outmixr_controls)),
541 SND_SOC_DAPM_MIXER("Mono Mixer", SND_SOC_NOPM, 0, 0,
542 &da7210_dapm_monomix_controls[0],
543 ARRAY_SIZE(da7210_dapm_monomix_controls)),
546 SND_SOC_DAPM_PGA("OUTPGA Left Enable", DA7210_OUTMIX_L, 7, 0, NULL, 0),
547 SND_SOC_DAPM_PGA("OUTPGA Right Enable", DA7210_OUTMIX_R, 7, 0, NULL, 0),
549 SND_SOC_DAPM_PGA("Out1 Left", DA7210_STARTUP2, 0, 1, NULL, 0),
550 SND_SOC_DAPM_PGA("Out1 Right", DA7210_STARTUP2, 1, 1, NULL, 0),
551 SND_SOC_DAPM_PGA("Out2 Mono", DA7210_STARTUP2, 2, 1, NULL, 0),
552 SND_SOC_DAPM_PGA("Headphone Left", DA7210_STARTUP2, 3, 1, NULL, 0),
553 SND_SOC_DAPM_PGA("Headphone Right", DA7210_STARTUP2, 4, 1, NULL, 0),
556 SND_SOC_DAPM_OUTPUT("OUT1L"),
557 SND_SOC_DAPM_OUTPUT("OUT1R"),
558 SND_SOC_DAPM_OUTPUT("HPL"),
559 SND_SOC_DAPM_OUTPUT("HPR"),
560 SND_SOC_DAPM_OUTPUT("OUT2"),
563 /* DAPM audio route definition */
564 static const struct snd_soc_dapm_route da7210_audio_map[] = {
565 /* Dest Connecting Widget source */
567 {"Mic Left", NULL, "MICL"},
568 {"Mic Right", NULL, "MICR"},
569 {"Aux1 Left", NULL, "AUX1L"},
570 {"Aux1 Right", NULL, "AUX1R"},
571 {"Aux2 Mono", NULL, "AUX2"},
573 {"In Mixer Left", "Mic Left Switch", "Mic Left"},
574 {"In Mixer Left", "Mic Right Switch", "Mic Right"},
575 {"In Mixer Left", "Aux1 Left Switch", "Aux1 Left"},
576 {"In Mixer Left", "Aux2 Switch", "Aux2 Mono"},
577 {"In Mixer Left", "Outmix Left Switch", "Out Mixer Left"},
579 {"In Mixer Right", "Mic Right Switch", "Mic Right"},
580 {"In Mixer Right", "Mic Left Switch", "Mic Left"},
581 {"In Mixer Right", "Aux1 Right Switch", "Aux1 Right"},
582 {"In Mixer Right", "Aux2 Switch", "Aux2 Mono"},
583 {"In Mixer Right", "Outmix Right Switch", "Out Mixer Right"},
585 {"INPGA Left", NULL, "In Mixer Left"},
586 {"ADC Left", NULL, "INPGA Left"},
588 {"INPGA Right", NULL, "In Mixer Right"},
589 {"ADC Right", NULL, "INPGA Right"},
592 {"Out Mixer Left", "Aux1 Left Switch", "Aux1 Left"},
593 {"Out Mixer Left", "Aux2 Switch", "Aux2 Mono"},
594 {"Out Mixer Left", "INPGA Left Switch", "INPGA Left"},
595 {"Out Mixer Left", "INPGA Right Switch", "INPGA Right"},
596 {"Out Mixer Left", "DAC Left Switch", "DAC Left"},
598 {"Out Mixer Right", "Aux1 Right Switch", "Aux1 Right"},
599 {"Out Mixer Right", "Aux2 Switch", "Aux2 Mono"},
600 {"Out Mixer Right", "INPGA Right Switch", "INPGA Right"},
601 {"Out Mixer Right", "INPGA Left Switch", "INPGA Left"},
602 {"Out Mixer Right", "DAC Right Switch", "DAC Right"},
604 {"Mono Mixer", "INPGA Right Switch", "INPGA Right"},
605 {"Mono Mixer", "INPGA Left Switch", "INPGA Left"},
606 {"Mono Mixer", "Outmix Right Switch", "Out Mixer Right"},
607 {"Mono Mixer", "Outmix Left Switch", "Out Mixer Left"},
609 {"OUTPGA Left Enable", NULL, "Out Mixer Left"},
610 {"OUTPGA Right Enable", NULL, "Out Mixer Right"},
612 {"Out1 Left", NULL, "OUTPGA Left Enable"},
613 {"OUT1L", NULL, "Out1 Left"},
615 {"Out1 Right", NULL, "OUTPGA Right Enable"},
616 {"OUT1R", NULL, "Out1 Right"},
618 {"Headphone Left", NULL, "OUTPGA Left Enable"},
619 {"HPL", NULL, "Headphone Left"},
621 {"Headphone Right", NULL, "OUTPGA Right Enable"},
622 {"HPR", NULL, "Headphone Right"},
624 {"Out2 Mono", NULL, "Mono Mixer"},
625 {"OUT2", NULL, "Out2 Mono"},
628 /* Codec private data */
630 struct regmap *regmap;
633 static struct reg_default da7210_reg_defaults[] = {
685 static bool da7210_readable_register(struct device *dev, unsigned int reg)
688 case DA7210_A_HID_UNLOCK:
689 case DA7210_A_TEST_UNLOCK:
691 case DA7210_A_CP_MODE:
698 static bool da7210_volatile_register(struct device *dev,
710 * Set PCM DAI word length.
712 static int da7210_hw_params(struct snd_pcm_substream *substream,
713 struct snd_pcm_hw_params *params,
714 struct snd_soc_dai *dai)
716 struct snd_soc_pcm_runtime *rtd = substream->private_data;
717 struct snd_soc_codec *codec = rtd->codec;
721 /* set DAI source to Left and Right ADC */
722 snd_soc_write(codec, DA7210_DAI_SRC_SEL,
723 DA7210_DAI_OUT_R_SRC | DA7210_DAI_OUT_L_SRC);
726 snd_soc_write(codec, DA7210_DAI_CFG3, DA7210_DAI_OE | DA7210_DAI_EN);
728 dai_cfg1 = 0xFC & snd_soc_read(codec, DA7210_DAI_CFG1);
730 switch (params_format(params)) {
731 case SNDRV_PCM_FORMAT_S16_LE:
732 dai_cfg1 |= DA7210_DAI_WORD_S16_LE;
734 case SNDRV_PCM_FORMAT_S20_3LE:
735 dai_cfg1 |= DA7210_DAI_WORD_S20_3LE;
737 case SNDRV_PCM_FORMAT_S24_LE:
738 dai_cfg1 |= DA7210_DAI_WORD_S24_LE;
740 case SNDRV_PCM_FORMAT_S32_LE:
741 dai_cfg1 |= DA7210_DAI_WORD_S32_LE;
747 snd_soc_write(codec, DA7210_DAI_CFG1, dai_cfg1);
749 switch (params_rate(params)) {
751 fs = DA7210_PLL_FS_8000;
752 bypass = DA7210_PLL_BYP;
755 fs = DA7210_PLL_FS_11025;
759 fs = DA7210_PLL_FS_12000;
760 bypass = DA7210_PLL_BYP;
763 fs = DA7210_PLL_FS_16000;
764 bypass = DA7210_PLL_BYP;
767 fs = DA7210_PLL_FS_22050;
771 fs = DA7210_PLL_FS_32000;
772 bypass = DA7210_PLL_BYP;
775 fs = DA7210_PLL_FS_44100;
779 fs = DA7210_PLL_FS_48000;
780 bypass = DA7210_PLL_BYP;
783 fs = DA7210_PLL_FS_88200;
787 fs = DA7210_PLL_FS_96000;
788 bypass = DA7210_PLL_BYP;
794 /* Disable active mode */
795 snd_soc_update_bits(codec, DA7210_STARTUP1, DA7210_SC_MST_EN, 0);
797 snd_soc_update_bits(codec, DA7210_PLL, DA7210_PLL_FS_MASK, fs);
798 snd_soc_update_bits(codec, DA7210_PLL_DIV3, DA7210_PLL_BYP, bypass);
800 /* Enable active mode */
801 snd_soc_update_bits(codec, DA7210_STARTUP1,
802 DA7210_SC_MST_EN, DA7210_SC_MST_EN);
808 * Set DAI mode and Format
810 static int da7210_set_dai_fmt(struct snd_soc_dai *codec_dai, u32 fmt)
812 struct snd_soc_codec *codec = codec_dai->codec;
816 dai_cfg1 = 0x7f & snd_soc_read(codec, DA7210_DAI_CFG1);
817 dai_cfg3 = 0xfc & snd_soc_read(codec, DA7210_DAI_CFG3);
819 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
820 case SND_SOC_DAIFMT_CBM_CFM:
821 dai_cfg1 |= DA7210_DAI_MODE_MASTER;
823 case SND_SOC_DAIFMT_CBS_CFS:
824 dai_cfg1 |= DA7210_DAI_MODE_SLAVE;
832 * It support I2S only now
834 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
835 case SND_SOC_DAIFMT_I2S:
836 dai_cfg3 |= DA7210_DAI_FORMAT_I2SMODE;
838 case SND_SOC_DAIFMT_LEFT_J:
839 dai_cfg3 |= DA7210_DAI_FORMAT_LEFT_J;
841 case SND_SOC_DAIFMT_RIGHT_J:
842 dai_cfg3 |= DA7210_DAI_FORMAT_RIGHT_J;
850 * It support 64bit data transmission only now
852 dai_cfg1 |= DA7210_DAI_FLEN_64BIT;
854 snd_soc_write(codec, DA7210_DAI_CFG1, dai_cfg1);
855 snd_soc_write(codec, DA7210_DAI_CFG3, dai_cfg3);
860 static int da7210_mute(struct snd_soc_dai *dai, int mute)
862 struct snd_soc_codec *codec = dai->codec;
863 u8 mute_reg = snd_soc_read(codec, DA7210_DAC_HPF) & 0xFB;
866 snd_soc_write(codec, DA7210_DAC_HPF, mute_reg | 0x4);
868 snd_soc_write(codec, DA7210_DAC_HPF, mute_reg);
872 #define DA7210_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
873 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
876 static const struct snd_soc_dai_ops da7210_dai_ops = {
877 .hw_params = da7210_hw_params,
878 .set_fmt = da7210_set_dai_fmt,
879 .digital_mute = da7210_mute,
882 static struct snd_soc_dai_driver da7210_dai = {
883 .name = "da7210-hifi",
884 /* playback capabilities */
886 .stream_name = "Playback",
889 .rates = SNDRV_PCM_RATE_8000_96000,
890 .formats = DA7210_FORMATS,
892 /* capture capabilities */
894 .stream_name = "Capture",
897 .rates = SNDRV_PCM_RATE_8000_96000,
898 .formats = DA7210_FORMATS,
900 .ops = &da7210_dai_ops,
901 .symmetric_rates = 1,
904 static int da7210_probe(struct snd_soc_codec *codec)
906 struct da7210_priv *da7210 = snd_soc_codec_get_drvdata(codec);
909 dev_info(codec->dev, "DA7210 Audio Codec %s\n", DA7210_VERSION);
911 codec->control_data = da7210->regmap;
912 ret = snd_soc_codec_set_cache_io(codec, 8, 8, SND_SOC_REGMAP);
914 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
920 * This driver use fixed value here
921 * And below settings expects MCLK = 12.288MHz
923 * When you select different MCLK, please check...
924 * DA7210_PLL_DIV1 val
925 * DA7210_PLL_DIV2 val
926 * DA7210_PLL_DIV3 val
927 * DA7210_PLL_DIV3 :: DA7210_MCLK_RANGExxx
931 * make sure that DA7210 use bypass mode before start up
933 snd_soc_write(codec, DA7210_STARTUP1, 0);
934 snd_soc_write(codec, DA7210_PLL_DIV3,
935 DA7210_MCLK_RANGE_10_20_MHZ | DA7210_PLL_BYP);
941 /* Enable Left & Right MIC PGA and Mic Bias */
942 snd_soc_write(codec, DA7210_MIC_L, DA7210_MIC_L_EN | DA7210_MICBIAS_EN);
943 snd_soc_write(codec, DA7210_MIC_R, DA7210_MIC_R_EN);
945 /* Enable Left and Right input PGA */
946 snd_soc_write(codec, DA7210_INMIX_L, DA7210_IN_L_EN);
947 snd_soc_write(codec, DA7210_INMIX_R, DA7210_IN_R_EN);
949 /* Enable Left and Right ADC */
950 snd_soc_write(codec, DA7210_ADC, DA7210_ADC_L_EN | DA7210_ADC_R_EN);
956 /* Enable Left and Right DAC */
957 snd_soc_write(codec, DA7210_DAC_SEL,
958 DA7210_DAC_L_SRC_DAI_L | DA7210_DAC_L_EN |
959 DA7210_DAC_R_SRC_DAI_R | DA7210_DAC_R_EN);
961 /* Enable Left and Right out PGA */
962 snd_soc_write(codec, DA7210_OUTMIX_L, DA7210_OUT_L_EN);
963 snd_soc_write(codec, DA7210_OUTMIX_R, DA7210_OUT_R_EN);
965 /* Enable Left and Right HeadPhone PGA */
966 snd_soc_write(codec, DA7210_HP_CFG,
967 DA7210_HP_2CAP_MODE | DA7210_HP_SENSE_EN |
968 DA7210_HP_L_EN | DA7210_HP_MODE | DA7210_HP_R_EN);
970 /* Enable ramp mode for DAC gain update */
971 snd_soc_write(codec, DA7210_SOFTMUTE, DA7210_RAMP_EN);
974 * For DA7210 codec, there are two ways to enable/disable analog IOs
976 * (1) Using "Enable Bit" of register associated with that IO
978 * e.g. Mic Left can be enabled using bit 7 of MIC_L(0x7) reg
980 * (2) Using "Standby Bit" of STARTUP2 or STARTUP3 register
981 * e.g. Mic left can be put to STANDBY using bit 0 of STARTUP3(0x5)
983 * Out of these two methods, the one using STANDBY bits is preferred
984 * way to enable/disable individual blocks. This is because STANDBY
985 * registers are part of system controller which allows system power
986 * up/down in a controlled, pop-free manner. Also, as per application
987 * note of DA7210, STANDBY register bits are only effective if a
988 * particular IO (or ADC/DAC) is already enabled using enable/disable
989 * register bits. Keeping these things in mind, current DAPM
990 * implementation manipulates only STANDBY bits.
992 * Overall implementation can be outlined as below,
994 * - "Enable bit" of an IO or ADC/DAC is used to enable it in probe()
995 * - "STANDBY bit" is controlled by DAPM
998 /* Enable Line out amplifiers */
999 snd_soc_write(codec, DA7210_OUT1_L, DA7210_OUT1_L_EN);
1000 snd_soc_write(codec, DA7210_OUT1_R, DA7210_OUT1_R_EN);
1001 snd_soc_write(codec, DA7210_OUT2, DA7210_OUT2_EN |
1002 DA7210_OUT2_OUTMIX_L | DA7210_OUT2_OUTMIX_R);
1005 snd_soc_write(codec, DA7210_AUX1_L, DA7210_AUX1_L_EN);
1006 snd_soc_write(codec, DA7210_AUX1_R, DA7210_AUX1_R_EN);
1008 snd_soc_write(codec, DA7210_AUX2, DA7210_AUX2_EN);
1010 /* Diable PLL and bypass it */
1011 snd_soc_write(codec, DA7210_PLL, DA7210_PLL_FS_48000);
1014 * If 48kHz sound came, it use bypass mode,
1015 * and when it is 44.1kHz, it use PLL.
1017 * This time, this driver sets PLL always ON
1018 * and controls bypass/PLL mode by switching
1019 * DA7210_PLL_DIV3 :: DA7210_PLL_BYP bit.
1020 * see da7210_hw_params
1022 snd_soc_write(codec, DA7210_PLL_DIV1, 0xE5); /* MCLK = 12.288MHz */
1023 snd_soc_write(codec, DA7210_PLL_DIV2, 0x99);
1024 snd_soc_write(codec, DA7210_PLL_DIV3, 0x0A |
1025 DA7210_MCLK_RANGE_10_20_MHZ | DA7210_PLL_BYP);
1026 snd_soc_update_bits(codec, DA7210_PLL, DA7210_PLL_EN, DA7210_PLL_EN);
1028 /* As suggested by Dialog */
1030 regmap_write(da7210->regmap, DA7210_A_HID_UNLOCK, 0x8B);
1031 regmap_write(da7210->regmap, DA7210_A_TEST_UNLOCK, 0xB4);
1032 regmap_write(da7210->regmap, DA7210_A_PLL1, 0x01);
1033 regmap_write(da7210->regmap, DA7210_A_CP_MODE, 0x7C);
1035 regmap_write(da7210->regmap, DA7210_A_HID_UNLOCK, 0x00);
1036 regmap_write(da7210->regmap, DA7210_A_TEST_UNLOCK, 0x00);
1038 /* Activate all enabled subsystem */
1039 snd_soc_write(codec, DA7210_STARTUP1, DA7210_SC_MST_EN);
1041 dev_info(codec->dev, "DA7210 Audio Codec %s\n", DA7210_VERSION);
1046 static struct snd_soc_codec_driver soc_codec_dev_da7210 = {
1047 .probe = da7210_probe,
1049 .controls = da7210_snd_controls,
1050 .num_controls = ARRAY_SIZE(da7210_snd_controls),
1052 .dapm_widgets = da7210_dapm_widgets,
1053 .num_dapm_widgets = ARRAY_SIZE(da7210_dapm_widgets),
1054 .dapm_routes = da7210_audio_map,
1055 .num_dapm_routes = ARRAY_SIZE(da7210_audio_map),
1058 static struct regmap_config da7210_regmap = {
1062 .reg_defaults = da7210_reg_defaults,
1063 .num_reg_defaults = ARRAY_SIZE(da7210_reg_defaults),
1064 .volatile_reg = da7210_volatile_register,
1065 .readable_reg = da7210_readable_register,
1066 .cache_type = REGCACHE_RBTREE,
1069 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
1070 static int __devinit da7210_i2c_probe(struct i2c_client *i2c,
1071 const struct i2c_device_id *id)
1073 struct da7210_priv *da7210;
1076 da7210 = devm_kzalloc(&i2c->dev, sizeof(struct da7210_priv),
1081 i2c_set_clientdata(i2c, da7210);
1083 da7210->regmap = regmap_init_i2c(i2c, &da7210_regmap);
1084 if (IS_ERR(da7210->regmap)) {
1085 ret = PTR_ERR(da7210->regmap);
1086 dev_err(&i2c->dev, "regmap_init() failed: %d\n", ret);
1090 ret = snd_soc_register_codec(&i2c->dev,
1091 &soc_codec_dev_da7210, &da7210_dai, 1);
1093 dev_err(&i2c->dev, "Failed to register codec: %d\n", ret);
1099 regmap_exit(da7210->regmap);
1104 static int __devexit da7210_i2c_remove(struct i2c_client *client)
1106 struct da7210_priv *da7210 = i2c_get_clientdata(client);
1108 snd_soc_unregister_codec(&client->dev);
1109 regmap_exit(da7210->regmap);
1113 static const struct i2c_device_id da7210_i2c_id[] = {
1117 MODULE_DEVICE_TABLE(i2c, da7210_i2c_id);
1119 /* I2C codec control layer */
1120 static struct i2c_driver da7210_i2c_driver = {
1122 .name = "da7210-codec",
1123 .owner = THIS_MODULE,
1125 .probe = da7210_i2c_probe,
1126 .remove = __devexit_p(da7210_i2c_remove),
1127 .id_table = da7210_i2c_id,
1131 static int __init da7210_modinit(void)
1134 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
1135 ret = i2c_add_driver(&da7210_i2c_driver);
1139 module_init(da7210_modinit);
1141 static void __exit da7210_exit(void)
1143 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
1144 i2c_del_driver(&da7210_i2c_driver);
1147 module_exit(da7210_exit);
1149 MODULE_DESCRIPTION("ASoC DA7210 driver");
1150 MODULE_AUTHOR("David Chen, Kuninori Morimoto");
1151 MODULE_LICENSE("GPL");