]> git.karo-electronics.de Git - mv-sheeva.git/blob - sound/soc/codecs/da7210.c
ASoC: da7210: Add support for ALC and Noise suppression
[mv-sheeva.git] / sound / soc / codecs / da7210.c
1 /*
2  * DA7210 ALSA Soc codec driver
3  *
4  * Copyright (c) 2009 Dialog Semiconductor
5  * Written by David Chen <Dajun.chen@diasemi.com>
6  *
7  * Copyright (C) 2009 Renesas Solutions Corp.
8  * Cleanups by Kuninori Morimoto <morimoto.kuninori@renesas.com>
9  *
10  * Tested on SuperH Ecovec24 board with S16/S24 LE in 48KHz using I2S
11  *
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.
16  */
17
18 #include <linux/delay.h>
19 #include <linux/i2c.h>
20 #include <linux/platform_device.h>
21 #include <linux/slab.h>
22 #include <sound/pcm.h>
23 #include <sound/pcm_params.h>
24 #include <sound/soc.h>
25 #include <sound/initval.h>
26 #include <sound/tlv.h>
27
28 /* DA7210 register space */
29 #define DA7210_CONTROL                  0x01
30 #define DA7210_STATUS                   0x02
31 #define DA7210_STARTUP1                 0x03
32 #define DA7210_MIC_L                    0x07
33 #define DA7210_MIC_R                    0x08
34 #define DA7210_AUX1_L                   0x09
35 #define DA7210_AUX1_R                   0x0A
36 #define DA7210_AUX2                     0x0B
37 #define DA7210_IN_GAIN                  0x0C
38 #define DA7210_INMIX_L                  0x0D
39 #define DA7210_INMIX_R                  0x0E
40 #define DA7210_ADC_HPF                  0x0F
41 #define DA7210_ADC                      0x10
42 #define DA7210_ADC_EQ1_2                0X11
43 #define DA7210_ADC_EQ3_4                0x12
44 #define DA7210_ADC_EQ5                  0x13
45 #define DA7210_DAC_HPF                  0x14
46 #define DA7210_DAC_L                    0x15
47 #define DA7210_DAC_R                    0x16
48 #define DA7210_DAC_SEL                  0x17
49 #define DA7210_SOFTMUTE                 0x18
50 #define DA7210_DAC_EQ1_2                0x19
51 #define DA7210_DAC_EQ3_4                0x1A
52 #define DA7210_DAC_EQ5                  0x1B
53 #define DA7210_OUTMIX_L                 0x1C
54 #define DA7210_OUTMIX_R                 0x1D
55 #define DA7210_HP_L_VOL                 0x21
56 #define DA7210_HP_R_VOL                 0x22
57 #define DA7210_HP_CFG                   0x23
58 #define DA7210_ZERO_CROSS               0x24
59 #define DA7210_DAI_SRC_SEL              0x25
60 #define DA7210_DAI_CFG1                 0x26
61 #define DA7210_DAI_CFG3                 0x28
62 #define DA7210_PLL_DIV1                 0x29
63 #define DA7210_PLL_DIV2                 0x2A
64 #define DA7210_PLL_DIV3                 0x2B
65 #define DA7210_PLL                      0x2C
66 #define DA7210_ALC_MAX                  0x83
67 #define DA7210_ALC_MIN                  0x84
68 #define DA7210_ALC_NOIS                 0x85
69 #define DA7210_ALC_ATT                  0x86
70 #define DA7210_ALC_REL                  0x87
71 #define DA7210_ALC_DEL                  0x88
72 #define DA7210_A_HID_UNLOCK             0x8A
73 #define DA7210_A_TEST_UNLOCK            0x8B
74 #define DA7210_A_PLL1                   0x90
75 #define DA7210_A_CP_MODE                0xA7
76
77 /* STARTUP1 bit fields */
78 #define DA7210_SC_MST_EN                (1 << 0)
79
80 /* MIC_L bit fields */
81 #define DA7210_MICBIAS_EN               (1 << 6)
82 #define DA7210_MIC_L_EN                 (1 << 7)
83
84 /* MIC_R bit fields */
85 #define DA7210_MIC_R_EN                 (1 << 7)
86
87 /* INMIX_L bit fields */
88 #define DA7210_IN_L_EN                  (1 << 7)
89
90 /* INMIX_R bit fields */
91 #define DA7210_IN_R_EN                  (1 << 7)
92
93 /* ADC bit fields */
94 #define DA7210_ADC_ALC_EN               (1 << 0)
95 #define DA7210_ADC_L_EN                 (1 << 3)
96 #define DA7210_ADC_R_EN                 (1 << 7)
97
98 /* DAC/ADC HPF fields */
99 #define DA7210_VOICE_F0_MASK            (0x7 << 4)
100 #define DA7210_VOICE_F0_25              (1 << 4)
101 #define DA7210_VOICE_EN                 (1 << 7)
102
103 /* DAC_SEL bit fields */
104 #define DA7210_DAC_L_SRC_DAI_L          (4 << 0)
105 #define DA7210_DAC_L_EN                 (1 << 3)
106 #define DA7210_DAC_R_SRC_DAI_R          (5 << 4)
107 #define DA7210_DAC_R_EN                 (1 << 7)
108
109 /* OUTMIX_L bit fields */
110 #define DA7210_OUT_L_EN                 (1 << 7)
111
112 /* OUTMIX_R bit fields */
113 #define DA7210_OUT_R_EN                 (1 << 7)
114
115 /* HP_CFG bit fields */
116 #define DA7210_HP_2CAP_MODE             (1 << 1)
117 #define DA7210_HP_SENSE_EN              (1 << 2)
118 #define DA7210_HP_L_EN                  (1 << 3)
119 #define DA7210_HP_MODE                  (1 << 6)
120 #define DA7210_HP_R_EN                  (1 << 7)
121
122 /* DAI_SRC_SEL bit fields */
123 #define DA7210_DAI_OUT_L_SRC            (6 << 0)
124 #define DA7210_DAI_OUT_R_SRC            (7 << 4)
125
126 /* DAI_CFG1 bit fields */
127 #define DA7210_DAI_WORD_S16_LE          (0 << 0)
128 #define DA7210_DAI_WORD_S20_3LE         (1 << 0)
129 #define DA7210_DAI_WORD_S24_LE          (2 << 0)
130 #define DA7210_DAI_WORD_S32_LE          (3 << 0)
131 #define DA7210_DAI_FLEN_64BIT           (1 << 2)
132 #define DA7210_DAI_MODE_SLAVE           (0 << 7)
133 #define DA7210_DAI_MODE_MASTER          (1 << 7)
134
135 /* DAI_CFG3 bit fields */
136 #define DA7210_DAI_FORMAT_I2SMODE       (0 << 0)
137 #define DA7210_DAI_FORMAT_LEFT_J        (1 << 0)
138 #define DA7210_DAI_FORMAT_RIGHT_J       (2 << 0)
139 #define DA7210_DAI_OE                   (1 << 3)
140 #define DA7210_DAI_EN                   (1 << 7)
141
142 /*PLL_DIV3 bit fields */
143 #define DA7210_MCLK_RANGE_10_20_MHZ     (1 << 4)
144 #define DA7210_PLL_BYP                  (1 << 6)
145
146 /* PLL bit fields */
147 #define DA7210_PLL_FS_MASK              (0xF << 0)
148 #define DA7210_PLL_FS_8000              (0x1 << 0)
149 #define DA7210_PLL_FS_11025             (0x2 << 0)
150 #define DA7210_PLL_FS_12000             (0x3 << 0)
151 #define DA7210_PLL_FS_16000             (0x5 << 0)
152 #define DA7210_PLL_FS_22050             (0x6 << 0)
153 #define DA7210_PLL_FS_24000             (0x7 << 0)
154 #define DA7210_PLL_FS_32000             (0x9 << 0)
155 #define DA7210_PLL_FS_44100             (0xA << 0)
156 #define DA7210_PLL_FS_48000             (0xB << 0)
157 #define DA7210_PLL_FS_88200             (0xE << 0)
158 #define DA7210_PLL_FS_96000             (0xF << 0)
159 #define DA7210_PLL_EN                   (0x1 << 7)
160
161 /* SOFTMUTE bit fields */
162 #define DA7210_RAMP_EN                  (1 << 6)
163
164 /* CONTROL bit fields */
165 #define DA7210_NOISE_SUP_EN             (1 << 3)
166
167 /* IN_GAIN bit fields */
168 #define DA7210_INPGA_L_VOL              (0x0F << 0)
169 #define DA7210_INPGA_R_VOL              (0xF0 << 0)
170
171 /* ZERO_CROSS bit fields */
172 #define DA7210_AUX1_L_ZC                (1 << 0)
173 #define DA7210_AUX1_R_ZC                (1 << 1)
174 #define DA7210_HP_L_ZC                  (1 << 6)
175 #define DA7210_HP_R_ZC                  (1 << 7)
176
177 /* AUX1_L bit fields */
178 #define DA7210_AUX1_L_VOL               (0x3F << 0)
179
180 /* AUX1_R bit fields */
181 #define DA7210_AUX1_R_VOL               (0x3F << 0)
182
183 /* Minimum INPGA and AUX1 volume to enable noise suppression */
184 #define DA7210_INPGA_MIN_VOL_NS         0x0A  /* 10.5dB */
185 #define DA7210_AUX1_MIN_VOL_NS          0x35  /* 6dB */
186
187 #define DA7210_VERSION "0.0.1"
188
189 /*
190  * Playback Volume
191  *
192  * max          : 0x3F (+15.0 dB)
193  *                 (1.5 dB step)
194  * min          : 0x11 (-54.0 dB)
195  * mute         : 0x10
196  * reserved     : 0x00 - 0x0F
197  *
198  * Reserved area are considered as "mute".
199  */
200 static const unsigned int hp_out_tlv[] = {
201         TLV_DB_RANGE_HEAD(2),
202         0x0, 0x10, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE, 0, 1),
203         /* -54 dB to +15 dB */
204         0x11, 0x3f, TLV_DB_SCALE_ITEM(-5400, 150, 0),
205 };
206
207 static const DECLARE_TLV_DB_SCALE(eq_gain_tlv, -1050, 150, 0);
208 static const DECLARE_TLV_DB_SCALE(adc_eq_master_gain_tlv, -1800, 600, 1);
209
210 /* ADC and DAC high pass filter f0 value */
211 static const char const *da7210_hpf_cutoff_txt[] = {
212         "Fs/8192*pi", "Fs/4096*pi", "Fs/2048*pi", "Fs/1024*pi"
213 };
214
215 static const struct soc_enum da7210_dac_hpf_cutoff =
216         SOC_ENUM_SINGLE(DA7210_DAC_HPF, 0, 4, da7210_hpf_cutoff_txt);
217
218 static const struct soc_enum da7210_adc_hpf_cutoff =
219         SOC_ENUM_SINGLE(DA7210_ADC_HPF, 0, 4, da7210_hpf_cutoff_txt);
220
221 /* ADC and DAC voice (8kHz) high pass cutoff value */
222 static const char const *da7210_vf_cutoff_txt[] = {
223         "2.5Hz", "25Hz", "50Hz", "100Hz", "150Hz", "200Hz", "300Hz", "400Hz"
224 };
225
226 static const struct soc_enum da7210_dac_vf_cutoff =
227         SOC_ENUM_SINGLE(DA7210_DAC_HPF, 4, 8, da7210_vf_cutoff_txt);
228
229 static const struct soc_enum da7210_adc_vf_cutoff =
230         SOC_ENUM_SINGLE(DA7210_ADC_HPF, 4, 8, da7210_vf_cutoff_txt);
231
232 static const char *da7210_hp_mode_txt[] = {
233         "Class H", "Class G"
234 };
235
236 static const struct soc_enum da7210_hp_mode_sel =
237         SOC_ENUM_SINGLE(DA7210_HP_CFG, 0, 2, da7210_hp_mode_txt);
238
239 /* ALC can be enabled only if noise suppression is disabled */
240 static int da7210_put_alc_sw(struct snd_kcontrol *kcontrol,
241                              struct snd_ctl_elem_value *ucontrol)
242 {
243         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
244
245         if (ucontrol->value.integer.value[0]) {
246                 /* Check if noise suppression is enabled */
247                 if (snd_soc_read(codec, DA7210_CONTROL) & DA7210_NOISE_SUP_EN) {
248                         dev_dbg(codec->dev,
249                                 "Disable noise suppression to enable ALC\n");
250                         return -EINVAL;
251                 }
252         }
253         /* If all conditions are met or we are actually disabling ALC */
254         return snd_soc_put_volsw(kcontrol, ucontrol);
255 }
256
257 /* Noise suppression can be enabled only if following conditions are met
258  *  ALC disabled
259  *  ZC enabled for HP and AUX1 PGA
260  *  INPGA_L_VOL and INPGA_R_VOL >= 10.5 dB
261  *  AUX1_L_VOL and AUX1_R_VOL >= 6 dB
262  */
263 static int da7210_put_noise_sup_sw(struct snd_kcontrol *kcontrol,
264                                    struct snd_ctl_elem_value *ucontrol)
265 {
266         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
267         u8 val;
268
269         if (ucontrol->value.integer.value[0]) {
270                 /* Check if ALC is enabled */
271                 if (snd_soc_read(codec, DA7210_ADC) & DA7210_ADC_ALC_EN)
272                         goto err;
273
274                 /* Check ZC for HP and AUX1 PGA */
275                 if ((snd_soc_read(codec, DA7210_ZERO_CROSS) &
276                         (DA7210_AUX1_L_ZC | DA7210_AUX1_R_ZC | DA7210_HP_L_ZC |
277                         DA7210_HP_R_ZC)) != (DA7210_AUX1_L_ZC |
278                         DA7210_AUX1_R_ZC | DA7210_HP_L_ZC | DA7210_HP_R_ZC))
279                         goto err;
280
281                 /* Check INPGA_L_VOL and INPGA_R_VOL */
282                 val = snd_soc_read(codec, DA7210_IN_GAIN);
283                 if (((val & DA7210_INPGA_L_VOL) < DA7210_INPGA_MIN_VOL_NS) ||
284                         (((val & DA7210_INPGA_R_VOL) >> 4) <
285                         DA7210_INPGA_MIN_VOL_NS))
286                         goto err;
287
288                 /* Check AUX1_L_VOL and AUX1_R_VOL */
289                 if (((snd_soc_read(codec, DA7210_AUX1_L) & DA7210_AUX1_L_VOL) <
290                     DA7210_AUX1_MIN_VOL_NS) ||
291                     ((snd_soc_read(codec, DA7210_AUX1_R) & DA7210_AUX1_R_VOL) <
292                     DA7210_AUX1_MIN_VOL_NS))
293                         goto err;
294         }
295         /* If all conditions are met or we are actually disabling Noise sup */
296         return snd_soc_put_volsw(kcontrol, ucontrol);
297
298 err:
299         return -EINVAL;
300 }
301
302 static const struct snd_kcontrol_new da7210_snd_controls[] = {
303
304         SOC_DOUBLE_R_TLV("HeadPhone Playback Volume",
305                          DA7210_HP_L_VOL, DA7210_HP_R_VOL,
306                          0, 0x3F, 0, hp_out_tlv),
307
308         /* DAC Equalizer  controls */
309         SOC_SINGLE("DAC EQ Switch", DA7210_DAC_EQ5, 7, 1, 0),
310         SOC_SINGLE_TLV("DAC EQ1 Volume", DA7210_DAC_EQ1_2, 0, 0xf, 1,
311                        eq_gain_tlv),
312         SOC_SINGLE_TLV("DAC EQ2 Volume", DA7210_DAC_EQ1_2, 4, 0xf, 1,
313                        eq_gain_tlv),
314         SOC_SINGLE_TLV("DAC EQ3 Volume", DA7210_DAC_EQ3_4, 0, 0xf, 1,
315                        eq_gain_tlv),
316         SOC_SINGLE_TLV("DAC EQ4 Volume", DA7210_DAC_EQ3_4, 4, 0xf, 1,
317                        eq_gain_tlv),
318         SOC_SINGLE_TLV("DAC EQ5 Volume", DA7210_DAC_EQ5, 0, 0xf, 1,
319                        eq_gain_tlv),
320
321         /* ADC Equalizer  controls */
322         SOC_SINGLE("ADC EQ Switch", DA7210_ADC_EQ5, 7, 1, 0),
323         SOC_SINGLE_TLV("ADC EQ Master Volume", DA7210_ADC_EQ5, 4, 0x3,
324                        1, adc_eq_master_gain_tlv),
325         SOC_SINGLE_TLV("ADC EQ1 Volume", DA7210_ADC_EQ1_2, 0, 0xf, 1,
326                        eq_gain_tlv),
327         SOC_SINGLE_TLV("ADC EQ2 Volume", DA7210_ADC_EQ1_2, 4, 0xf, 1,
328                        eq_gain_tlv),
329         SOC_SINGLE_TLV("ADC EQ3 Volume", DA7210_ADC_EQ3_4, 0, 0xf, 1,
330                        eq_gain_tlv),
331         SOC_SINGLE_TLV("ADC EQ4 Volume", DA7210_ADC_EQ3_4, 4, 0xf, 1,
332                        eq_gain_tlv),
333         SOC_SINGLE_TLV("ADC EQ5 Volume", DA7210_ADC_EQ5, 0, 0xf, 1,
334                        eq_gain_tlv),
335
336         SOC_SINGLE("DAC HPF Switch", DA7210_DAC_HPF, 3, 1, 0),
337         SOC_ENUM("DAC HPF Cutoff", da7210_dac_hpf_cutoff),
338         SOC_SINGLE("DAC Voice Mode Switch", DA7210_DAC_HPF, 7, 1, 0),
339         SOC_ENUM("DAC Voice Cutoff", da7210_dac_vf_cutoff),
340
341         SOC_SINGLE("ADC HPF Switch", DA7210_ADC_HPF, 3, 1, 0),
342         SOC_ENUM("ADC HPF Cutoff", da7210_adc_hpf_cutoff),
343         SOC_SINGLE("ADC Voice Mode Switch", DA7210_ADC_HPF, 7, 1, 0),
344         SOC_ENUM("ADC Voice Cutoff", da7210_adc_vf_cutoff),
345
346         /* Mute controls */
347         SOC_DOUBLE_R("Mic Capture Switch", DA7210_MIC_L, DA7210_MIC_R, 3, 1, 0),
348         SOC_SINGLE("Aux2 Capture Switch", DA7210_AUX2, 2, 1, 0),
349         SOC_DOUBLE("ADC Capture Switch", DA7210_ADC, 2, 6, 1, 0),
350         SOC_SINGLE("Digital Soft Mute Switch", DA7210_SOFTMUTE, 7, 1, 0),
351         SOC_SINGLE("Digital Soft Mute Rate", DA7210_SOFTMUTE, 0, 0x7, 0),
352
353         /* Zero cross controls */
354         SOC_DOUBLE("Aux1 ZC Switch", DA7210_ZERO_CROSS, 0, 1, 1, 0),
355         SOC_DOUBLE("In PGA ZC Switch", DA7210_ZERO_CROSS, 2, 3, 1, 0),
356         SOC_DOUBLE("Lineout ZC Switch", DA7210_ZERO_CROSS, 4, 5, 1, 0),
357         SOC_DOUBLE("Headphone ZC Switch", DA7210_ZERO_CROSS, 6, 7, 1, 0),
358
359         SOC_ENUM("Headphone Class", da7210_hp_mode_sel),
360
361         /* ALC controls */
362         SOC_SINGLE_EXT("ALC Enable Switch", DA7210_ADC, 0, 1, 0,
363                        snd_soc_get_volsw, da7210_put_alc_sw),
364         SOC_SINGLE("ALC Capture Max Volume", DA7210_ALC_MAX, 0, 0x3F, 0),
365         SOC_SINGLE("ALC Capture Min Volume", DA7210_ALC_MIN, 0, 0x3F, 0),
366         SOC_SINGLE("ALC Capture Noise Volume", DA7210_ALC_NOIS, 0, 0x3F, 0),
367         SOC_SINGLE("ALC Capture Attack Rate", DA7210_ALC_ATT, 0, 0xFF, 0),
368         SOC_SINGLE("ALC Capture Release Rate", DA7210_ALC_REL, 0, 0xFF, 0),
369         SOC_SINGLE("ALC Capture Release Delay", DA7210_ALC_DEL, 0, 0xFF, 0),
370
371         SOC_SINGLE_EXT("Noise Suppression Enable Switch", DA7210_CONTROL, 3, 1,
372                        0, snd_soc_get_volsw, da7210_put_noise_sup_sw),
373 };
374
375 /* Codec private data */
376 struct da7210_priv {
377         enum snd_soc_control_type control_type;
378 };
379
380 /*
381  * Register cache
382  */
383 static const u8 da7210_reg[] = {
384         0x00, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R0  - R7  */
385         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, /* R8  - RF  */
386         0x00, 0x00, 0x00, 0x00, 0x08, 0x10, 0x10, 0x54, /* R10 - R17 */
387         0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R18 - R1F */
388         0x00, 0x00, 0x00, 0x02, 0x00, 0x76, 0x00, 0x00, /* R20 - R27 */
389         0x04, 0x00, 0x00, 0x30, 0x2A, 0x00, 0x40, 0x00, /* R28 - R2F */
390         0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, /* R30 - R37 */
391         0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x00, 0x00, /* R38 - R3F */
392         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R40 - R4F */
393         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R48 - R4F */
394         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R50 - R57 */
395         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R58 - R5F */
396         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R60 - R67 */
397         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R68 - R6F */
398         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R70 - R77 */
399         0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x54, 0x00, /* R78 - R7F */
400         0x00, 0x00, 0x2C, 0x00, 0x00, 0x00, 0x00, 0x00, /* R80 - R87 */
401         0x00,                                           /* R88       */
402 };
403
404 static int da7210_volatile_register(struct snd_soc_codec *codec,
405                                     unsigned int reg)
406 {
407         switch (reg) {
408         case DA7210_STATUS:
409                 return 1;
410         default:
411                 return 0;
412         }
413 }
414 static int da7210_startup(struct snd_pcm_substream *substream,
415                           struct snd_soc_dai *dai)
416 {
417         int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
418         struct snd_soc_codec *codec = dai->codec;
419
420         if (is_play) {
421                 /* Enable Out */
422                 snd_soc_update_bits(codec, DA7210_OUTMIX_L, 0x1F, 0x10);
423                 snd_soc_update_bits(codec, DA7210_OUTMIX_R, 0x1F, 0x10);
424
425         } else {
426                 /* Volume 7 */
427                 snd_soc_update_bits(codec, DA7210_MIC_L, 0x7, 0x7);
428                 snd_soc_update_bits(codec, DA7210_MIC_R, 0x7, 0x7);
429
430                 /* Enable Mic */
431                 snd_soc_update_bits(codec, DA7210_INMIX_L, 0x1F, 0x1);
432                 snd_soc_update_bits(codec, DA7210_INMIX_R, 0x1F, 0x1);
433         }
434
435         return 0;
436 }
437
438 /*
439  * Set PCM DAI word length.
440  */
441 static int da7210_hw_params(struct snd_pcm_substream *substream,
442                             struct snd_pcm_hw_params *params,
443                             struct snd_soc_dai *dai)
444 {
445         struct snd_soc_pcm_runtime *rtd = substream->private_data;
446         struct snd_soc_codec *codec = rtd->codec;
447         u32 dai_cfg1;
448         u32 fs, bypass;
449
450         /* set DAI source to Left and Right ADC */
451         snd_soc_write(codec, DA7210_DAI_SRC_SEL,
452                      DA7210_DAI_OUT_R_SRC | DA7210_DAI_OUT_L_SRC);
453
454         /* Enable DAI */
455         snd_soc_write(codec, DA7210_DAI_CFG3, DA7210_DAI_OE | DA7210_DAI_EN);
456
457         dai_cfg1 = 0xFC & snd_soc_read(codec, DA7210_DAI_CFG1);
458
459         switch (params_format(params)) {
460         case SNDRV_PCM_FORMAT_S16_LE:
461                 dai_cfg1 |= DA7210_DAI_WORD_S16_LE;
462                 break;
463         case SNDRV_PCM_FORMAT_S20_3LE:
464                 dai_cfg1 |= DA7210_DAI_WORD_S20_3LE;
465                 break;
466         case SNDRV_PCM_FORMAT_S24_LE:
467                 dai_cfg1 |= DA7210_DAI_WORD_S24_LE;
468                 break;
469         case SNDRV_PCM_FORMAT_S32_LE:
470                 dai_cfg1 |= DA7210_DAI_WORD_S32_LE;
471                 break;
472         default:
473                 return -EINVAL;
474         }
475
476         snd_soc_write(codec, DA7210_DAI_CFG1, dai_cfg1);
477
478         switch (params_rate(params)) {
479         case 8000:
480                 fs              = DA7210_PLL_FS_8000;
481                 bypass          = DA7210_PLL_BYP;
482                 break;
483         case 11025:
484                 fs              = DA7210_PLL_FS_11025;
485                 bypass          = 0;
486                 break;
487         case 12000:
488                 fs              = DA7210_PLL_FS_12000;
489                 bypass          = DA7210_PLL_BYP;
490                 break;
491         case 16000:
492                 fs              = DA7210_PLL_FS_16000;
493                 bypass          = DA7210_PLL_BYP;
494                 break;
495         case 22050:
496                 fs              = DA7210_PLL_FS_22050;
497                 bypass          = 0;
498                 break;
499         case 32000:
500                 fs              = DA7210_PLL_FS_32000;
501                 bypass          = DA7210_PLL_BYP;
502                 break;
503         case 44100:
504                 fs              = DA7210_PLL_FS_44100;
505                 bypass          = 0;
506                 break;
507         case 48000:
508                 fs              = DA7210_PLL_FS_48000;
509                 bypass          = DA7210_PLL_BYP;
510                 break;
511         case 88200:
512                 fs              = DA7210_PLL_FS_88200;
513                 bypass          = 0;
514                 break;
515         case 96000:
516                 fs              = DA7210_PLL_FS_96000;
517                 bypass          = DA7210_PLL_BYP;
518                 break;
519         default:
520                 return -EINVAL;
521         }
522
523         /* Disable active mode */
524         snd_soc_update_bits(codec, DA7210_STARTUP1, DA7210_SC_MST_EN, 0);
525
526         snd_soc_update_bits(codec, DA7210_PLL, DA7210_PLL_FS_MASK, fs);
527         snd_soc_update_bits(codec, DA7210_PLL_DIV3, DA7210_PLL_BYP, bypass);
528
529         /* Enable active mode */
530         snd_soc_update_bits(codec, DA7210_STARTUP1,
531                             DA7210_SC_MST_EN, DA7210_SC_MST_EN);
532
533         return 0;
534 }
535
536 /*
537  * Set DAI mode and Format
538  */
539 static int da7210_set_dai_fmt(struct snd_soc_dai *codec_dai, u32 fmt)
540 {
541         struct snd_soc_codec *codec = codec_dai->codec;
542         u32 dai_cfg1;
543         u32 dai_cfg3;
544
545         dai_cfg1 = 0x7f & snd_soc_read(codec, DA7210_DAI_CFG1);
546         dai_cfg3 = 0xfc & snd_soc_read(codec, DA7210_DAI_CFG3);
547
548         switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
549         case SND_SOC_DAIFMT_CBM_CFM:
550                 dai_cfg1 |= DA7210_DAI_MODE_MASTER;
551                 break;
552         case SND_SOC_DAIFMT_CBS_CFS:
553                 dai_cfg1 |= DA7210_DAI_MODE_SLAVE;
554                 break;
555         default:
556                 return -EINVAL;
557         }
558
559         /* FIXME
560          *
561          * It support I2S only now
562          */
563         switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
564         case SND_SOC_DAIFMT_I2S:
565                 dai_cfg3 |= DA7210_DAI_FORMAT_I2SMODE;
566                 break;
567         case SND_SOC_DAIFMT_LEFT_J:
568                 dai_cfg3 |= DA7210_DAI_FORMAT_LEFT_J;
569                 break;
570         case SND_SOC_DAIFMT_RIGHT_J:
571                 dai_cfg3 |= DA7210_DAI_FORMAT_RIGHT_J;
572                 break;
573         default:
574                 return -EINVAL;
575         }
576
577         /* FIXME
578          *
579          * It support 64bit data transmission only now
580          */
581         dai_cfg1 |= DA7210_DAI_FLEN_64BIT;
582
583         snd_soc_write(codec, DA7210_DAI_CFG1, dai_cfg1);
584         snd_soc_write(codec, DA7210_DAI_CFG3, dai_cfg3);
585
586         return 0;
587 }
588
589 static int da7210_mute(struct snd_soc_dai *dai, int mute)
590 {
591         struct snd_soc_codec *codec = dai->codec;
592         u8 mute_reg = snd_soc_read(codec, DA7210_DAC_HPF) & 0xFB;
593
594         if (mute)
595                 snd_soc_write(codec, DA7210_DAC_HPF, mute_reg | 0x4);
596         else
597                 snd_soc_write(codec, DA7210_DAC_HPF, mute_reg);
598         return 0;
599 }
600
601 #define DA7210_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
602                         SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
603
604 /* DAI operations */
605 static struct snd_soc_dai_ops da7210_dai_ops = {
606         .startup        = da7210_startup,
607         .hw_params      = da7210_hw_params,
608         .set_fmt        = da7210_set_dai_fmt,
609         .digital_mute   = da7210_mute,
610 };
611
612 static struct snd_soc_dai_driver da7210_dai = {
613         .name = "da7210-hifi",
614         /* playback capabilities */
615         .playback = {
616                 .stream_name = "Playback",
617                 .channels_min = 1,
618                 .channels_max = 2,
619                 .rates = SNDRV_PCM_RATE_8000_96000,
620                 .formats = DA7210_FORMATS,
621         },
622         /* capture capabilities */
623         .capture = {
624                 .stream_name = "Capture",
625                 .channels_min = 1,
626                 .channels_max = 2,
627                 .rates = SNDRV_PCM_RATE_8000_96000,
628                 .formats = DA7210_FORMATS,
629         },
630         .ops = &da7210_dai_ops,
631         .symmetric_rates = 1,
632 };
633
634 static int da7210_probe(struct snd_soc_codec *codec)
635 {
636         struct da7210_priv *da7210 = snd_soc_codec_get_drvdata(codec);
637         int ret;
638
639         dev_info(codec->dev, "DA7210 Audio Codec %s\n", DA7210_VERSION);
640
641         ret = snd_soc_codec_set_cache_io(codec, 8, 8, da7210->control_type);
642         if (ret < 0) {
643                 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
644                 return ret;
645         }
646
647         /* FIXME
648          *
649          * This driver use fixed value here
650          * And below settings expects MCLK = 12.288MHz
651          *
652          * When you select different MCLK, please check...
653          *      DA7210_PLL_DIV1 val
654          *      DA7210_PLL_DIV2 val
655          *      DA7210_PLL_DIV3 val
656          *      DA7210_PLL_DIV3 :: DA7210_MCLK_RANGExxx
657          */
658
659         /*
660          * make sure that DA7210 use bypass mode before start up
661          */
662         snd_soc_write(codec, DA7210_STARTUP1, 0);
663         snd_soc_write(codec, DA7210_PLL_DIV3,
664                      DA7210_MCLK_RANGE_10_20_MHZ | DA7210_PLL_BYP);
665
666         /*
667          * ADC settings
668          */
669
670         /* Enable Left & Right MIC PGA and Mic Bias */
671         snd_soc_write(codec, DA7210_MIC_L, DA7210_MIC_L_EN | DA7210_MICBIAS_EN);
672         snd_soc_write(codec, DA7210_MIC_R, DA7210_MIC_R_EN);
673
674         /* Enable Left and Right input PGA */
675         snd_soc_write(codec, DA7210_INMIX_L, DA7210_IN_L_EN);
676         snd_soc_write(codec, DA7210_INMIX_R, DA7210_IN_R_EN);
677
678         /* Enable Left and Right ADC */
679         snd_soc_write(codec, DA7210_ADC, DA7210_ADC_L_EN | DA7210_ADC_R_EN);
680
681         /*
682          * DAC settings
683          */
684
685         /* Enable Left and Right DAC */
686         snd_soc_write(codec, DA7210_DAC_SEL,
687                      DA7210_DAC_L_SRC_DAI_L | DA7210_DAC_L_EN |
688                      DA7210_DAC_R_SRC_DAI_R | DA7210_DAC_R_EN);
689
690         /* Enable Left and Right out PGA */
691         snd_soc_write(codec, DA7210_OUTMIX_L, DA7210_OUT_L_EN);
692         snd_soc_write(codec, DA7210_OUTMIX_R, DA7210_OUT_R_EN);
693
694         /* Enable Left and Right HeadPhone PGA */
695         snd_soc_write(codec, DA7210_HP_CFG,
696                      DA7210_HP_2CAP_MODE | DA7210_HP_SENSE_EN |
697                      DA7210_HP_L_EN | DA7210_HP_MODE | DA7210_HP_R_EN);
698
699         /* Enable ramp mode for DAC gain update */
700         snd_soc_write(codec, DA7210_SOFTMUTE, DA7210_RAMP_EN);
701
702         /* Diable PLL and bypass it */
703         snd_soc_write(codec, DA7210_PLL, DA7210_PLL_FS_48000);
704
705         /*
706          * If 48kHz sound came, it use bypass mode,
707          * and when it is 44.1kHz, it use PLL.
708          *
709          * This time, this driver sets PLL always ON
710          * and controls bypass/PLL mode by switching
711          * DA7210_PLL_DIV3 :: DA7210_PLL_BYP bit.
712          *   see da7210_hw_params
713          */
714         snd_soc_write(codec, DA7210_PLL_DIV1, 0xE5); /* MCLK = 12.288MHz */
715         snd_soc_write(codec, DA7210_PLL_DIV2, 0x99);
716         snd_soc_write(codec, DA7210_PLL_DIV3, 0x0A |
717                      DA7210_MCLK_RANGE_10_20_MHZ | DA7210_PLL_BYP);
718         snd_soc_update_bits(codec, DA7210_PLL, DA7210_PLL_EN, DA7210_PLL_EN);
719
720         /* As suggested by Dialog */
721         snd_soc_write(codec, DA7210_A_HID_UNLOCK,       0x8B); /* unlock */
722         snd_soc_write(codec, DA7210_A_TEST_UNLOCK,      0xB4);
723         snd_soc_write(codec, DA7210_A_PLL1,             0x01);
724         snd_soc_write(codec, DA7210_A_CP_MODE,          0x7C);
725         snd_soc_write(codec, DA7210_A_HID_UNLOCK,       0x00); /* re-lock */
726         snd_soc_write(codec, DA7210_A_TEST_UNLOCK,      0x00);
727
728         /* Activate all enabled subsystem */
729         snd_soc_write(codec, DA7210_STARTUP1, DA7210_SC_MST_EN);
730
731         dev_info(codec->dev, "DA7210 Audio Codec %s\n", DA7210_VERSION);
732
733         return 0;
734 }
735
736 static struct snd_soc_codec_driver soc_codec_dev_da7210 = {
737         .probe                  = da7210_probe,
738         .reg_cache_size         = ARRAY_SIZE(da7210_reg),
739         .reg_word_size          = sizeof(u8),
740         .reg_cache_default      = da7210_reg,
741         .volatile_register      = da7210_volatile_register,
742
743         .controls               = da7210_snd_controls,
744         .num_controls           = ARRAY_SIZE(da7210_snd_controls),
745 };
746
747 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
748 static int __devinit da7210_i2c_probe(struct i2c_client *i2c,
749                                       const struct i2c_device_id *id)
750 {
751         struct da7210_priv *da7210;
752         int ret;
753
754         da7210 = kzalloc(sizeof(struct da7210_priv), GFP_KERNEL);
755         if (!da7210)
756                 return -ENOMEM;
757
758         i2c_set_clientdata(i2c, da7210);
759         da7210->control_type = SND_SOC_I2C;
760
761         ret =  snd_soc_register_codec(&i2c->dev,
762                         &soc_codec_dev_da7210, &da7210_dai, 1);
763         if (ret < 0)
764                 kfree(da7210);
765
766         return ret;
767 }
768
769 static int __devexit da7210_i2c_remove(struct i2c_client *client)
770 {
771         snd_soc_unregister_codec(&client->dev);
772         kfree(i2c_get_clientdata(client));
773         return 0;
774 }
775
776 static const struct i2c_device_id da7210_i2c_id[] = {
777         { "da7210", 0 },
778         { }
779 };
780 MODULE_DEVICE_TABLE(i2c, da7210_i2c_id);
781
782 /* I2C codec control layer */
783 static struct i2c_driver da7210_i2c_driver = {
784         .driver = {
785                 .name = "da7210-codec",
786                 .owner = THIS_MODULE,
787         },
788         .probe          = da7210_i2c_probe,
789         .remove         = __devexit_p(da7210_i2c_remove),
790         .id_table       = da7210_i2c_id,
791 };
792 #endif
793
794 static int __init da7210_modinit(void)
795 {
796         int ret = 0;
797 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
798         ret = i2c_add_driver(&da7210_i2c_driver);
799 #endif
800         return ret;
801 }
802 module_init(da7210_modinit);
803
804 static void __exit da7210_exit(void)
805 {
806 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
807         i2c_del_driver(&da7210_i2c_driver);
808 #endif
809 }
810 module_exit(da7210_exit);
811
812 MODULE_DESCRIPTION("ASoC DA7210 driver");
813 MODULE_AUTHOR("David Chen, Kuninori Morimoto");
814 MODULE_LICENSE("GPL");