cgroup: superblock can't be released with active dentries
[firefly-linux-kernel-4.4.55.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/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>
28
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
82
83 /* STARTUP1 bit fields */
84 #define DA7210_SC_MST_EN                (1 << 0)
85
86 /* MIC_L bit fields */
87 #define DA7210_MICBIAS_EN               (1 << 6)
88 #define DA7210_MIC_L_EN                 (1 << 7)
89
90 /* MIC_R bit fields */
91 #define DA7210_MIC_R_EN                 (1 << 7)
92
93 /* INMIX_L bit fields */
94 #define DA7210_IN_L_EN                  (1 << 7)
95
96 /* INMIX_R bit fields */
97 #define DA7210_IN_R_EN                  (1 << 7)
98
99 /* ADC bit fields */
100 #define DA7210_ADC_ALC_EN               (1 << 0)
101 #define DA7210_ADC_L_EN                 (1 << 3)
102 #define DA7210_ADC_R_EN                 (1 << 7)
103
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)
108
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)
114
115 /* OUTMIX_L bit fields */
116 #define DA7210_OUT_L_EN                 (1 << 7)
117
118 /* OUTMIX_R bit fields */
119 #define DA7210_OUT_R_EN                 (1 << 7)
120
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)
127
128 /* DAI_SRC_SEL bit fields */
129 #define DA7210_DAI_OUT_L_SRC            (6 << 0)
130 #define DA7210_DAI_OUT_R_SRC            (7 << 4)
131
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)
140
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)
147
148 /*PLL_DIV3 bit fields */
149 #define DA7210_MCLK_RANGE_10_20_MHZ     (1 << 4)
150 #define DA7210_PLL_BYP                  (1 << 6)
151
152 /* PLL bit fields */
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)
166
167 /* SOFTMUTE bit fields */
168 #define DA7210_RAMP_EN                  (1 << 6)
169
170 /* CONTROL bit fields */
171 #define DA7210_NOISE_SUP_EN             (1 << 3)
172
173 /* IN_GAIN bit fields */
174 #define DA7210_INPGA_L_VOL              (0x0F << 0)
175 #define DA7210_INPGA_R_VOL              (0xF0 << 0)
176
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)
182
183 /* AUX1_L bit fields */
184 #define DA7210_AUX1_L_VOL               (0x3F << 0)
185 #define DA7210_AUX1_L_EN                (1 << 7)
186
187 /* AUX1_R bit fields */
188 #define DA7210_AUX1_R_VOL               (0x3F << 0)
189 #define DA7210_AUX1_R_EN                (1 << 7)
190
191 /* AUX2 bit fields */
192 #define DA7210_AUX2_EN                  (1 << 3)
193
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 */
197
198 /* OUT1_L bit fields */
199 #define DA7210_OUT1_L_EN                (1 << 7)
200
201 /* OUT1_R bit fields */
202 #define DA7210_OUT1_R_EN                (1 << 7)
203
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)
208
209 #define DA7210_VERSION "0.0.1"
210
211 /*
212  * Playback Volume
213  *
214  * max          : 0x3F (+15.0 dB)
215  *                 (1.5 dB step)
216  * min          : 0x11 (-54.0 dB)
217  * mute         : 0x10
218  * reserved     : 0x00 - 0x0F
219  *
220  * Reserved area are considered as "mute".
221  */
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),
227 };
228
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),
232         /* -54dB to 15dB */
233         0x11, 0x3f, TLV_DB_SCALE_ITEM(-5400, 150, 0)
234 };
235
236 static const unsigned int mono_vol_tlv[] = {
237         TLV_DB_RANGE_HEAD(2),
238         0x0, 0x2, TLV_DB_SCALE_ITEM(-1800, 0, 1),
239         /* -18dB to 6dB */
240         0x3, 0x7, TLV_DB_SCALE_ITEM(-1800, 600, 0)
241 };
242
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),
246         /* -48dB to 21dB */
247         0x11, 0x3f, TLV_DB_SCALE_ITEM(-4800, 150, 0)
248 };
249
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);
256
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"
260 };
261
262 static const struct soc_enum da7210_dac_hpf_cutoff =
263         SOC_ENUM_SINGLE(DA7210_DAC_HPF, 0, 4, da7210_hpf_cutoff_txt);
264
265 static const struct soc_enum da7210_adc_hpf_cutoff =
266         SOC_ENUM_SINGLE(DA7210_ADC_HPF, 0, 4, da7210_hpf_cutoff_txt);
267
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"
271 };
272
273 static const struct soc_enum da7210_dac_vf_cutoff =
274         SOC_ENUM_SINGLE(DA7210_DAC_HPF, 4, 8, da7210_vf_cutoff_txt);
275
276 static const struct soc_enum da7210_adc_vf_cutoff =
277         SOC_ENUM_SINGLE(DA7210_ADC_HPF, 4, 8, da7210_vf_cutoff_txt);
278
279 static const char *da7210_hp_mode_txt[] = {
280         "Class H", "Class G"
281 };
282
283 static const struct soc_enum da7210_hp_mode_sel =
284         SOC_ENUM_SINGLE(DA7210_HP_CFG, 0, 2, da7210_hp_mode_txt);
285
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)
289 {
290         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
291
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) {
295                         dev_dbg(codec->dev,
296                                 "Disable noise suppression to enable ALC\n");
297                         return -EINVAL;
298                 }
299         }
300         /* If all conditions are met or we are actually disabling ALC */
301         return snd_soc_put_volsw(kcontrol, ucontrol);
302 }
303
304 /* Noise suppression can be enabled only if following conditions are met
305  *  ALC disabled
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
309  */
310 static int da7210_put_noise_sup_sw(struct snd_kcontrol *kcontrol,
311                                    struct snd_ctl_elem_value *ucontrol)
312 {
313         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
314         u8 val;
315
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)
319                         goto err;
320
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))
326                         goto err;
327
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))
333                         goto err;
334
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))
340                         goto err;
341         }
342         /* If all conditions are met or we are actually disabling Noise sup */
343         return snd_soc_put_volsw(kcontrol, ucontrol);
344
345 err:
346         return -EINVAL;
347 }
348
349 static const struct snd_kcontrol_new da7210_snd_controls[] = {
350
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,
361                        mono_vol_tlv),
362
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,
370                        aux2_vol_tlv),
371         SOC_DOUBLE_TLV("In PGA Capture Volume", DA7210_IN_GAIN, 0, 4, 0xF, 0,
372                        inpga_gain_tlv),
373
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,
377                        eq_gain_tlv),
378         SOC_SINGLE_TLV("DAC EQ2 Volume", DA7210_DAC_EQ1_2, 4, 0xf, 1,
379                        eq_gain_tlv),
380         SOC_SINGLE_TLV("DAC EQ3 Volume", DA7210_DAC_EQ3_4, 0, 0xf, 1,
381                        eq_gain_tlv),
382         SOC_SINGLE_TLV("DAC EQ4 Volume", DA7210_DAC_EQ3_4, 4, 0xf, 1,
383                        eq_gain_tlv),
384         SOC_SINGLE_TLV("DAC EQ5 Volume", DA7210_DAC_EQ5, 0, 0xf, 1,
385                        eq_gain_tlv),
386
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,
392                        eq_gain_tlv),
393         SOC_SINGLE_TLV("ADC EQ2 Volume", DA7210_ADC_EQ1_2, 4, 0xf, 1,
394                        eq_gain_tlv),
395         SOC_SINGLE_TLV("ADC EQ3 Volume", DA7210_ADC_EQ3_4, 0, 0xf, 1,
396                        eq_gain_tlv),
397         SOC_SINGLE_TLV("ADC EQ4 Volume", DA7210_ADC_EQ3_4, 4, 0xf, 1,
398                        eq_gain_tlv),
399         SOC_SINGLE_TLV("ADC EQ5 Volume", DA7210_ADC_EQ5, 0, 0xf, 1,
400                        eq_gain_tlv),
401
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),
406
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),
411
412         /* Mute controls */
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),
418
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),
424
425         SOC_ENUM("Headphone Class", da7210_hp_mode_sel),
426
427         /* ALC controls */
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),
436
437         SOC_SINGLE_EXT("Noise Suppression Enable Switch", DA7210_CONTROL, 3, 1,
438                        0, snd_soc_get_volsw, da7210_put_noise_sup_sw),
439 };
440
441 /*
442  * DAPM Controls
443  *
444  * Current DAPM implementation covers almost all codec components e.g. IOs,
445  * mixers, PGAs,ADC and DAC.
446  */
447 /* In Mixer Left */
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),
454 };
455
456 /* In Mixer Right */
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),
463 };
464
465 /* Out Mixer Left */
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),
472 };
473
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),
481 };
482
483 /* Mono Mixer */
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),
489 };
490
491 /* DAPM widgets */
492 static const struct snd_soc_dapm_widget da7210_dapm_widgets[] = {
493         /* Input Side */
494         /* Input Lines */
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"),
500
501         /* Input PGAs */
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),
507
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),
510
511         /* MICBIAS */
512         SND_SOC_DAPM_SUPPLY("Mic Bias", DA7210_MIC_L, 6, 0, NULL, 0),
513
514         /* Input Mixers */
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)),
518
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)),
522
523         /* ADCs */
524         SND_SOC_DAPM_ADC("ADC Left", "Capture", DA7210_STARTUP3, 5, 1),
525         SND_SOC_DAPM_ADC("ADC Right", "Capture", DA7210_STARTUP3, 6, 1),
526
527         /* Output Side */
528         /* DACs */
529         SND_SOC_DAPM_DAC("DAC Left", "Playback", DA7210_STARTUP2, 5, 1),
530         SND_SOC_DAPM_DAC("DAC Right", "Playback", DA7210_STARTUP2, 6, 1),
531
532         /* Output Mixers */
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)),
536
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)),
540
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)),
544
545         /* Output PGAs */
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),
548
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),
554
555         /* Output Lines */
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"),
561 };
562
563 /* DAPM audio route definition */
564 static const struct snd_soc_dapm_route da7210_audio_map[] = {
565         /* Dest       Connecting Widget    source */
566         /* Input path */
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"},
572
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"},
578
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"},
584
585         {"INPGA Left", NULL, "In Mixer Left"},
586         {"ADC Left", NULL, "INPGA Left"},
587
588         {"INPGA Right", NULL, "In Mixer Right"},
589         {"ADC Right", NULL, "INPGA Right"},
590
591         /* Output path */
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"},
597
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"},
603
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"},
608
609         {"OUTPGA Left Enable", NULL, "Out Mixer Left"},
610         {"OUTPGA Right Enable", NULL, "Out Mixer Right"},
611
612         {"Out1 Left", NULL, "OUTPGA Left Enable"},
613         {"OUT1L", NULL, "Out1 Left"},
614
615         {"Out1 Right", NULL, "OUTPGA Right Enable"},
616         {"OUT1R", NULL, "Out1 Right"},
617
618         {"Headphone Left", NULL, "OUTPGA Left Enable"},
619         {"HPL", NULL, "Headphone Left"},
620
621         {"Headphone Right", NULL, "OUTPGA Right Enable"},
622         {"HPR", NULL, "Headphone Right"},
623
624         {"Out2 Mono", NULL, "Mono Mixer"},
625         {"OUT2", NULL, "Out2 Mono"},
626 };
627
628 /* Codec private data */
629 struct da7210_priv {
630         struct regmap *regmap;
631 };
632
633 static struct reg_default da7210_reg_defaults[] = {
634         { 0x01, 0x11 },
635         { 0x03, 0x00 },
636         { 0x04, 0x00 },
637         { 0x05, 0x00 },
638         { 0x06, 0x00 },
639         { 0x07, 0x00 },
640         { 0x08, 0x00 },
641         { 0x09, 0x00 },
642         { 0x0a, 0x00 },
643         { 0x0b, 0x00 },
644         { 0x0c, 0x00 },
645         { 0x0d, 0x00 },
646         { 0x0e, 0x00 },
647         { 0x0f, 0x08 },
648         { 0x10, 0x00 },
649         { 0x11, 0x00 },
650         { 0x12, 0x00 },
651         { 0x13, 0x00 },
652         { 0x14, 0x08 },
653         { 0x15, 0x10 },
654         { 0x16, 0x10 },
655         { 0x17, 0x54 },
656         { 0x18, 0x40 },
657         { 0x19, 0x00 },
658         { 0x1a, 0x00 },
659         { 0x1b, 0x00 },
660         { 0x1c, 0x00 },
661         { 0x1d, 0x00 },
662         { 0x1e, 0x00 },
663         { 0x1f, 0x00 },
664         { 0x20, 0x00 },
665         { 0x21, 0x00 },
666         { 0x22, 0x00 },
667         { 0x23, 0x02 },
668         { 0x24, 0x00 },
669         { 0x25, 0x76 },
670         { 0x26, 0x00 },
671         { 0x27, 0x00 },
672         { 0x28, 0x04 },
673         { 0x29, 0x00 },
674         { 0x2a, 0x00 },
675         { 0x2b, 0x30 },
676         { 0x2c, 0x2A },
677         { 0x83, 0x00 },
678         { 0x84, 0x00 },
679         { 0x85, 0x00 },
680         { 0x86, 0x00 },
681         { 0x87, 0x00 },
682         { 0x88, 0x00 },
683 };
684
685 static bool da7210_readable_register(struct device *dev, unsigned int reg)
686 {
687         switch (reg) {
688         case DA7210_A_HID_UNLOCK:
689         case DA7210_A_TEST_UNLOCK:
690         case DA7210_A_PLL1:
691         case DA7210_A_CP_MODE:
692                 return false;
693         default:
694                 return true;
695         }
696 }
697
698 static bool da7210_volatile_register(struct device *dev,
699                                     unsigned int reg)
700 {
701         switch (reg) {
702         case DA7210_STATUS:
703                 return true;
704         default:
705                 return false;
706         }
707 }
708
709 /*
710  * Set PCM DAI word length.
711  */
712 static int da7210_hw_params(struct snd_pcm_substream *substream,
713                             struct snd_pcm_hw_params *params,
714                             struct snd_soc_dai *dai)
715 {
716         struct snd_soc_pcm_runtime *rtd = substream->private_data;
717         struct snd_soc_codec *codec = rtd->codec;
718         u32 dai_cfg1;
719         u32 fs, bypass;
720
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);
724
725         /* Enable DAI */
726         snd_soc_write(codec, DA7210_DAI_CFG3, DA7210_DAI_OE | DA7210_DAI_EN);
727
728         dai_cfg1 = 0xFC & snd_soc_read(codec, DA7210_DAI_CFG1);
729
730         switch (params_format(params)) {
731         case SNDRV_PCM_FORMAT_S16_LE:
732                 dai_cfg1 |= DA7210_DAI_WORD_S16_LE;
733                 break;
734         case SNDRV_PCM_FORMAT_S20_3LE:
735                 dai_cfg1 |= DA7210_DAI_WORD_S20_3LE;
736                 break;
737         case SNDRV_PCM_FORMAT_S24_LE:
738                 dai_cfg1 |= DA7210_DAI_WORD_S24_LE;
739                 break;
740         case SNDRV_PCM_FORMAT_S32_LE:
741                 dai_cfg1 |= DA7210_DAI_WORD_S32_LE;
742                 break;
743         default:
744                 return -EINVAL;
745         }
746
747         snd_soc_write(codec, DA7210_DAI_CFG1, dai_cfg1);
748
749         switch (params_rate(params)) {
750         case 8000:
751                 fs              = DA7210_PLL_FS_8000;
752                 bypass          = DA7210_PLL_BYP;
753                 break;
754         case 11025:
755                 fs              = DA7210_PLL_FS_11025;
756                 bypass          = 0;
757                 break;
758         case 12000:
759                 fs              = DA7210_PLL_FS_12000;
760                 bypass          = DA7210_PLL_BYP;
761                 break;
762         case 16000:
763                 fs              = DA7210_PLL_FS_16000;
764                 bypass          = DA7210_PLL_BYP;
765                 break;
766         case 22050:
767                 fs              = DA7210_PLL_FS_22050;
768                 bypass          = 0;
769                 break;
770         case 32000:
771                 fs              = DA7210_PLL_FS_32000;
772                 bypass          = DA7210_PLL_BYP;
773                 break;
774         case 44100:
775                 fs              = DA7210_PLL_FS_44100;
776                 bypass          = 0;
777                 break;
778         case 48000:
779                 fs              = DA7210_PLL_FS_48000;
780                 bypass          = DA7210_PLL_BYP;
781                 break;
782         case 88200:
783                 fs              = DA7210_PLL_FS_88200;
784                 bypass          = 0;
785                 break;
786         case 96000:
787                 fs              = DA7210_PLL_FS_96000;
788                 bypass          = DA7210_PLL_BYP;
789                 break;
790         default:
791                 return -EINVAL;
792         }
793
794         /* Disable active mode */
795         snd_soc_update_bits(codec, DA7210_STARTUP1, DA7210_SC_MST_EN, 0);
796
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);
799
800         /* Enable active mode */
801         snd_soc_update_bits(codec, DA7210_STARTUP1,
802                             DA7210_SC_MST_EN, DA7210_SC_MST_EN);
803
804         return 0;
805 }
806
807 /*
808  * Set DAI mode and Format
809  */
810 static int da7210_set_dai_fmt(struct snd_soc_dai *codec_dai, u32 fmt)
811 {
812         struct snd_soc_codec *codec = codec_dai->codec;
813         u32 dai_cfg1;
814         u32 dai_cfg3;
815
816         dai_cfg1 = 0x7f & snd_soc_read(codec, DA7210_DAI_CFG1);
817         dai_cfg3 = 0xfc & snd_soc_read(codec, DA7210_DAI_CFG3);
818
819         switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
820         case SND_SOC_DAIFMT_CBM_CFM:
821                 dai_cfg1 |= DA7210_DAI_MODE_MASTER;
822                 break;
823         case SND_SOC_DAIFMT_CBS_CFS:
824                 dai_cfg1 |= DA7210_DAI_MODE_SLAVE;
825                 break;
826         default:
827                 return -EINVAL;
828         }
829
830         /* FIXME
831          *
832          * It support I2S only now
833          */
834         switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
835         case SND_SOC_DAIFMT_I2S:
836                 dai_cfg3 |= DA7210_DAI_FORMAT_I2SMODE;
837                 break;
838         case SND_SOC_DAIFMT_LEFT_J:
839                 dai_cfg3 |= DA7210_DAI_FORMAT_LEFT_J;
840                 break;
841         case SND_SOC_DAIFMT_RIGHT_J:
842                 dai_cfg3 |= DA7210_DAI_FORMAT_RIGHT_J;
843                 break;
844         default:
845                 return -EINVAL;
846         }
847
848         /* FIXME
849          *
850          * It support 64bit data transmission only now
851          */
852         dai_cfg1 |= DA7210_DAI_FLEN_64BIT;
853
854         snd_soc_write(codec, DA7210_DAI_CFG1, dai_cfg1);
855         snd_soc_write(codec, DA7210_DAI_CFG3, dai_cfg3);
856
857         return 0;
858 }
859
860 static int da7210_mute(struct snd_soc_dai *dai, int mute)
861 {
862         struct snd_soc_codec *codec = dai->codec;
863         u8 mute_reg = snd_soc_read(codec, DA7210_DAC_HPF) & 0xFB;
864
865         if (mute)
866                 snd_soc_write(codec, DA7210_DAC_HPF, mute_reg | 0x4);
867         else
868                 snd_soc_write(codec, DA7210_DAC_HPF, mute_reg);
869         return 0;
870 }
871
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)
874
875 /* DAI operations */
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,
880 };
881
882 static struct snd_soc_dai_driver da7210_dai = {
883         .name = "da7210-hifi",
884         /* playback capabilities */
885         .playback = {
886                 .stream_name = "Playback",
887                 .channels_min = 1,
888                 .channels_max = 2,
889                 .rates = SNDRV_PCM_RATE_8000_96000,
890                 .formats = DA7210_FORMATS,
891         },
892         /* capture capabilities */
893         .capture = {
894                 .stream_name = "Capture",
895                 .channels_min = 1,
896                 .channels_max = 2,
897                 .rates = SNDRV_PCM_RATE_8000_96000,
898                 .formats = DA7210_FORMATS,
899         },
900         .ops = &da7210_dai_ops,
901         .symmetric_rates = 1,
902 };
903
904 static int da7210_probe(struct snd_soc_codec *codec)
905 {
906         struct da7210_priv *da7210 = snd_soc_codec_get_drvdata(codec);
907         int ret;
908
909         dev_info(codec->dev, "DA7210 Audio Codec %s\n", DA7210_VERSION);
910
911         codec->control_data = da7210->regmap;
912         ret = snd_soc_codec_set_cache_io(codec, 8, 8, SND_SOC_REGMAP);
913         if (ret < 0) {
914                 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
915                 return ret;
916         }
917
918         /* FIXME
919          *
920          * This driver use fixed value here
921          * And below settings expects MCLK = 12.288MHz
922          *
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
928          */
929
930         /*
931          * make sure that DA7210 use bypass mode before start up
932          */
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);
936
937         /*
938          * ADC settings
939          */
940
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);
944
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);
948
949         /* Enable Left and Right ADC */
950         snd_soc_write(codec, DA7210_ADC, DA7210_ADC_L_EN | DA7210_ADC_R_EN);
951
952         /*
953          * DAC settings
954          */
955
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);
960
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);
964
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);
969
970         /* Enable ramp mode for DAC gain update */
971         snd_soc_write(codec, DA7210_SOFTMUTE, DA7210_RAMP_EN);
972
973         /*
974          * For DA7210 codec, there are two ways to enable/disable analog IOs
975          * and ADC/DAC,
976          * (1) Using "Enable Bit" of register associated with that IO
977          * (or ADC/DAC)
978          *      e.g. Mic Left can be enabled using bit 7 of MIC_L(0x7) reg
979          *
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)
982          *
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.
991          *
992          * Overall implementation can be outlined as below,
993          *
994          * - "Enable bit" of an IO or ADC/DAC is used to enable it in probe()
995          * - "STANDBY bit" is controlled by DAPM
996          */
997
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);
1003
1004         /* Enable Aux1 */
1005         snd_soc_write(codec, DA7210_AUX1_L, DA7210_AUX1_L_EN);
1006         snd_soc_write(codec, DA7210_AUX1_R, DA7210_AUX1_R_EN);
1007         /* Enable Aux2 */
1008         snd_soc_write(codec, DA7210_AUX2, DA7210_AUX2_EN);
1009
1010         /* Diable PLL and bypass it */
1011         snd_soc_write(codec, DA7210_PLL, DA7210_PLL_FS_48000);
1012
1013         /*
1014          * If 48kHz sound came, it use bypass mode,
1015          * and when it is 44.1kHz, it use PLL.
1016          *
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
1021          */
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);
1027
1028         /* As suggested by Dialog */
1029         /* unlock */
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);
1034         /* re-lock */
1035         regmap_write(da7210->regmap, DA7210_A_HID_UNLOCK,       0x00);
1036         regmap_write(da7210->regmap, DA7210_A_TEST_UNLOCK,      0x00);
1037
1038         /* Activate all enabled subsystem */
1039         snd_soc_write(codec, DA7210_STARTUP1, DA7210_SC_MST_EN);
1040
1041         dev_info(codec->dev, "DA7210 Audio Codec %s\n", DA7210_VERSION);
1042
1043         return 0;
1044 }
1045
1046 static struct snd_soc_codec_driver soc_codec_dev_da7210 = {
1047         .probe                  = da7210_probe,
1048
1049         .controls               = da7210_snd_controls,
1050         .num_controls           = ARRAY_SIZE(da7210_snd_controls),
1051
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),
1056 };
1057
1058 static struct regmap_config da7210_regmap = {
1059         .reg_bits = 8,
1060         .val_bits = 8,
1061
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,
1067 };
1068
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)
1072 {
1073         struct da7210_priv *da7210;
1074         int ret;
1075
1076         da7210 = devm_kzalloc(&i2c->dev, sizeof(struct da7210_priv),
1077                               GFP_KERNEL);
1078         if (!da7210)
1079                 return -ENOMEM;
1080
1081         i2c_set_clientdata(i2c, da7210);
1082
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);
1087                 return ret;
1088         }
1089
1090         ret =  snd_soc_register_codec(&i2c->dev,
1091                         &soc_codec_dev_da7210, &da7210_dai, 1);
1092         if (ret < 0) {
1093                 dev_err(&i2c->dev, "Failed to register codec: %d\n", ret);
1094                 goto err_regmap;
1095         }
1096         return ret;
1097
1098 err_regmap:
1099         regmap_exit(da7210->regmap);
1100
1101         return ret;
1102 }
1103
1104 static int __devexit da7210_i2c_remove(struct i2c_client *client)
1105 {
1106         struct da7210_priv *da7210 = i2c_get_clientdata(client);
1107
1108         snd_soc_unregister_codec(&client->dev);
1109         regmap_exit(da7210->regmap);
1110         return 0;
1111 }
1112
1113 static const struct i2c_device_id da7210_i2c_id[] = {
1114         { "da7210", 0 },
1115         { }
1116 };
1117 MODULE_DEVICE_TABLE(i2c, da7210_i2c_id);
1118
1119 /* I2C codec control layer */
1120 static struct i2c_driver da7210_i2c_driver = {
1121         .driver = {
1122                 .name = "da7210-codec",
1123                 .owner = THIS_MODULE,
1124         },
1125         .probe          = da7210_i2c_probe,
1126         .remove         = __devexit_p(da7210_i2c_remove),
1127         .id_table       = da7210_i2c_id,
1128 };
1129 #endif
1130
1131 static int __init da7210_modinit(void)
1132 {
1133         int ret = 0;
1134 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
1135         ret = i2c_add_driver(&da7210_i2c_driver);
1136 #endif
1137         return ret;
1138 }
1139 module_init(da7210_modinit);
1140
1141 static void __exit da7210_exit(void)
1142 {
1143 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
1144         i2c_del_driver(&da7210_i2c_driver);
1145 #endif
1146 }
1147 module_exit(da7210_exit);
1148
1149 MODULE_DESCRIPTION("ASoC DA7210 driver");
1150 MODULE_AUTHOR("David Chen, Kuninori Morimoto");
1151 MODULE_LICENSE("GPL");