OSDN Git Service

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