ASoC: wm9705: Fix build due to removal of 'runtime' definition
[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/spi/spi.h>
21 #include <linux/regmap.h>
22 #include <linux/slab.h>
23 #include <linux/module.h>
24 #include <sound/pcm.h>
25 #include <sound/pcm_params.h>
26 #include <sound/soc.h>
27 #include <sound/initval.h>
28 #include <sound/tlv.h>
29
30 /* DA7210 register space */
31 #define DA7210_PAGE_CONTROL             0x00
32 #define DA7210_CONTROL                  0x01
33 #define DA7210_STATUS                   0x02
34 #define DA7210_STARTUP1                 0x03
35 #define DA7210_STARTUP2                 0x04
36 #define DA7210_STARTUP3                 0x05
37 #define DA7210_MIC_L                    0x07
38 #define DA7210_MIC_R                    0x08
39 #define DA7210_AUX1_L                   0x09
40 #define DA7210_AUX1_R                   0x0A
41 #define DA7210_AUX2                     0x0B
42 #define DA7210_IN_GAIN                  0x0C
43 #define DA7210_INMIX_L                  0x0D
44 #define DA7210_INMIX_R                  0x0E
45 #define DA7210_ADC_HPF                  0x0F
46 #define DA7210_ADC                      0x10
47 #define DA7210_ADC_EQ1_2                0X11
48 #define DA7210_ADC_EQ3_4                0x12
49 #define DA7210_ADC_EQ5                  0x13
50 #define DA7210_DAC_HPF                  0x14
51 #define DA7210_DAC_L                    0x15
52 #define DA7210_DAC_R                    0x16
53 #define DA7210_DAC_SEL                  0x17
54 #define DA7210_SOFTMUTE                 0x18
55 #define DA7210_DAC_EQ1_2                0x19
56 #define DA7210_DAC_EQ3_4                0x1A
57 #define DA7210_DAC_EQ5                  0x1B
58 #define DA7210_OUTMIX_L                 0x1C
59 #define DA7210_OUTMIX_R                 0x1D
60 #define DA7210_OUT1_L                   0x1E
61 #define DA7210_OUT1_R                   0x1F
62 #define DA7210_OUT2                     0x20
63 #define DA7210_HP_L_VOL                 0x21
64 #define DA7210_HP_R_VOL                 0x22
65 #define DA7210_HP_CFG                   0x23
66 #define DA7210_ZERO_CROSS               0x24
67 #define DA7210_DAI_SRC_SEL              0x25
68 #define DA7210_DAI_CFG1                 0x26
69 #define DA7210_DAI_CFG3                 0x28
70 #define DA7210_PLL_DIV1                 0x29
71 #define DA7210_PLL_DIV2                 0x2A
72 #define DA7210_PLL_DIV3                 0x2B
73 #define DA7210_PLL                      0x2C
74 #define DA7210_ALC_MAX                  0x83
75 #define DA7210_ALC_MIN                  0x84
76 #define DA7210_ALC_NOIS                 0x85
77 #define DA7210_ALC_ATT                  0x86
78 #define DA7210_ALC_REL                  0x87
79 #define DA7210_ALC_DEL                  0x88
80 #define DA7210_A_HID_UNLOCK             0x8A
81 #define DA7210_A_TEST_UNLOCK            0x8B
82 #define DA7210_A_PLL1                   0x90
83 #define DA7210_A_CP_MODE                0xA7
84
85 /* STARTUP1 bit fields */
86 #define DA7210_SC_MST_EN                (1 << 0)
87
88 /* MIC_L bit fields */
89 #define DA7210_MICBIAS_EN               (1 << 6)
90 #define DA7210_MIC_L_EN                 (1 << 7)
91
92 /* MIC_R bit fields */
93 #define DA7210_MIC_R_EN                 (1 << 7)
94
95 /* INMIX_L bit fields */
96 #define DA7210_IN_L_EN                  (1 << 7)
97
98 /* INMIX_R bit fields */
99 #define DA7210_IN_R_EN                  (1 << 7)
100
101 /* ADC bit fields */
102 #define DA7210_ADC_ALC_EN               (1 << 0)
103 #define DA7210_ADC_L_EN                 (1 << 3)
104 #define DA7210_ADC_R_EN                 (1 << 7)
105
106 /* DAC/ADC HPF fields */
107 #define DA7210_VOICE_F0_MASK            (0x7 << 4)
108 #define DA7210_VOICE_F0_25              (1 << 4)
109 #define DA7210_VOICE_EN                 (1 << 7)
110
111 /* DAC_SEL bit fields */
112 #define DA7210_DAC_L_SRC_DAI_L          (4 << 0)
113 #define DA7210_DAC_L_EN                 (1 << 3)
114 #define DA7210_DAC_R_SRC_DAI_R          (5 << 4)
115 #define DA7210_DAC_R_EN                 (1 << 7)
116
117 /* OUTMIX_L bit fields */
118 #define DA7210_OUT_L_EN                 (1 << 7)
119
120 /* OUTMIX_R bit fields */
121 #define DA7210_OUT_R_EN                 (1 << 7)
122
123 /* HP_CFG bit fields */
124 #define DA7210_HP_2CAP_MODE             (1 << 1)
125 #define DA7210_HP_SENSE_EN              (1 << 2)
126 #define DA7210_HP_L_EN                  (1 << 3)
127 #define DA7210_HP_MODE                  (1 << 6)
128 #define DA7210_HP_R_EN                  (1 << 7)
129
130 /* DAI_SRC_SEL bit fields */
131 #define DA7210_DAI_OUT_L_SRC            (6 << 0)
132 #define DA7210_DAI_OUT_R_SRC            (7 << 4)
133
134 /* DAI_CFG1 bit fields */
135 #define DA7210_DAI_WORD_S16_LE          (0 << 0)
136 #define DA7210_DAI_WORD_S20_3LE         (1 << 0)
137 #define DA7210_DAI_WORD_S24_LE          (2 << 0)
138 #define DA7210_DAI_WORD_S32_LE          (3 << 0)
139 #define DA7210_DAI_FLEN_64BIT           (1 << 2)
140 #define DA7210_DAI_MODE_SLAVE           (0 << 7)
141 #define DA7210_DAI_MODE_MASTER          (1 << 7)
142
143 /* DAI_CFG3 bit fields */
144 #define DA7210_DAI_FORMAT_I2SMODE       (0 << 0)
145 #define DA7210_DAI_FORMAT_LEFT_J        (1 << 0)
146 #define DA7210_DAI_FORMAT_RIGHT_J       (2 << 0)
147 #define DA7210_DAI_OE                   (1 << 3)
148 #define DA7210_DAI_EN                   (1 << 7)
149
150 /*PLL_DIV3 bit fields */
151 #define DA7210_MCLK_RANGE_10_20_MHZ     (1 << 4)
152 #define DA7210_PLL_BYP                  (1 << 6)
153
154 /* PLL bit fields */
155 #define DA7210_PLL_FS_MASK              (0xF << 0)
156 #define DA7210_PLL_FS_8000              (0x1 << 0)
157 #define DA7210_PLL_FS_11025             (0x2 << 0)
158 #define DA7210_PLL_FS_12000             (0x3 << 0)
159 #define DA7210_PLL_FS_16000             (0x5 << 0)
160 #define DA7210_PLL_FS_22050             (0x6 << 0)
161 #define DA7210_PLL_FS_24000             (0x7 << 0)
162 #define DA7210_PLL_FS_32000             (0x9 << 0)
163 #define DA7210_PLL_FS_44100             (0xA << 0)
164 #define DA7210_PLL_FS_48000             (0xB << 0)
165 #define DA7210_PLL_FS_88200             (0xE << 0)
166 #define DA7210_PLL_FS_96000             (0xF << 0)
167 #define DA7210_PLL_EN                   (0x1 << 7)
168
169 /* SOFTMUTE bit fields */
170 #define DA7210_RAMP_EN                  (1 << 6)
171
172 /* CONTROL bit fields */
173 #define DA7210_NOISE_SUP_EN             (1 << 3)
174
175 /* IN_GAIN bit fields */
176 #define DA7210_INPGA_L_VOL              (0x0F << 0)
177 #define DA7210_INPGA_R_VOL              (0xF0 << 0)
178
179 /* ZERO_CROSS bit fields */
180 #define DA7210_AUX1_L_ZC                (1 << 0)
181 #define DA7210_AUX1_R_ZC                (1 << 1)
182 #define DA7210_HP_L_ZC                  (1 << 6)
183 #define DA7210_HP_R_ZC                  (1 << 7)
184
185 /* AUX1_L bit fields */
186 #define DA7210_AUX1_L_VOL               (0x3F << 0)
187 #define DA7210_AUX1_L_EN                (1 << 7)
188
189 /* AUX1_R bit fields */
190 #define DA7210_AUX1_R_VOL               (0x3F << 0)
191 #define DA7210_AUX1_R_EN                (1 << 7)
192
193 /* AUX2 bit fields */
194 #define DA7210_AUX2_EN                  (1 << 3)
195
196 /* Minimum INPGA and AUX1 volume to enable noise suppression */
197 #define DA7210_INPGA_MIN_VOL_NS         0x0A  /* 10.5dB */
198 #define DA7210_AUX1_MIN_VOL_NS          0x35  /* 6dB */
199
200 /* OUT1_L bit fields */
201 #define DA7210_OUT1_L_EN                (1 << 7)
202
203 /* OUT1_R bit fields */
204 #define DA7210_OUT1_R_EN                (1 << 7)
205
206 /* OUT2 bit fields */
207 #define DA7210_OUT2_OUTMIX_R            (1 << 5)
208 #define DA7210_OUT2_OUTMIX_L            (1 << 6)
209 #define DA7210_OUT2_EN                  (1 << 7)
210
211 #define DA7210_VERSION "0.0.1"
212
213 /*
214  * Playback Volume
215  *
216  * max          : 0x3F (+15.0 dB)
217  *                 (1.5 dB step)
218  * min          : 0x11 (-54.0 dB)
219  * mute         : 0x10
220  * reserved     : 0x00 - 0x0F
221  *
222  * Reserved area are considered as "mute".
223  */
224 static const unsigned int hp_out_tlv[] = {
225         TLV_DB_RANGE_HEAD(2),
226         0x0, 0x10, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE, 0, 1),
227         /* -54 dB to +15 dB */
228         0x11, 0x3f, TLV_DB_SCALE_ITEM(-5400, 150, 0),
229 };
230
231 static const unsigned int lineout_vol_tlv[] = {
232         TLV_DB_RANGE_HEAD(2),
233         0x0, 0x10, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE, 0, 1),
234         /* -54dB to 15dB */
235         0x11, 0x3f, TLV_DB_SCALE_ITEM(-5400, 150, 0)
236 };
237
238 static const unsigned int mono_vol_tlv[] = {
239         TLV_DB_RANGE_HEAD(2),
240         0x0, 0x2, TLV_DB_SCALE_ITEM(-1800, 0, 1),
241         /* -18dB to 6dB */
242         0x3, 0x7, TLV_DB_SCALE_ITEM(-1800, 600, 0)
243 };
244
245 static const unsigned int aux1_vol_tlv[] = {
246         TLV_DB_RANGE_HEAD(2),
247         0x0, 0x10, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE, 0, 1),
248         /* -48dB to 21dB */
249         0x11, 0x3f, TLV_DB_SCALE_ITEM(-4800, 150, 0)
250 };
251
252 static const DECLARE_TLV_DB_SCALE(eq_gain_tlv, -1050, 150, 0);
253 static const DECLARE_TLV_DB_SCALE(adc_eq_master_gain_tlv, -1800, 600, 1);
254 static const DECLARE_TLV_DB_SCALE(dac_gain_tlv, -7725, 75, 0);
255 static const DECLARE_TLV_DB_SCALE(mic_vol_tlv, -600, 600, 0);
256 static const DECLARE_TLV_DB_SCALE(aux2_vol_tlv, -600, 600, 0);
257 static const DECLARE_TLV_DB_SCALE(inpga_gain_tlv, -450, 150, 0);
258
259 /* ADC and DAC high pass filter f0 value */
260 static const char * const da7210_hpf_cutoff_txt[] = {
261         "Fs/8192*pi", "Fs/4096*pi", "Fs/2048*pi", "Fs/1024*pi"
262 };
263
264 static const struct soc_enum da7210_dac_hpf_cutoff =
265         SOC_ENUM_SINGLE(DA7210_DAC_HPF, 0, 4, da7210_hpf_cutoff_txt);
266
267 static const struct soc_enum da7210_adc_hpf_cutoff =
268         SOC_ENUM_SINGLE(DA7210_ADC_HPF, 0, 4, da7210_hpf_cutoff_txt);
269
270 /* ADC and DAC voice (8kHz) high pass cutoff value */
271 static const char * const da7210_vf_cutoff_txt[] = {
272         "2.5Hz", "25Hz", "50Hz", "100Hz", "150Hz", "200Hz", "300Hz", "400Hz"
273 };
274
275 static const struct soc_enum da7210_dac_vf_cutoff =
276         SOC_ENUM_SINGLE(DA7210_DAC_HPF, 4, 8, da7210_vf_cutoff_txt);
277
278 static const struct soc_enum da7210_adc_vf_cutoff =
279         SOC_ENUM_SINGLE(DA7210_ADC_HPF, 4, 8, da7210_vf_cutoff_txt);
280
281 static const char *da7210_hp_mode_txt[] = {
282         "Class H", "Class G"
283 };
284
285 static const struct soc_enum da7210_hp_mode_sel =
286         SOC_ENUM_SINGLE(DA7210_HP_CFG, 0, 2, da7210_hp_mode_txt);
287
288 /* ALC can be enabled only if noise suppression is disabled */
289 static int da7210_put_alc_sw(struct snd_kcontrol *kcontrol,
290                              struct snd_ctl_elem_value *ucontrol)
291 {
292         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
293
294         if (ucontrol->value.integer.value[0]) {
295                 /* Check if noise suppression is enabled */
296                 if (snd_soc_read(codec, DA7210_CONTROL) & DA7210_NOISE_SUP_EN) {
297                         dev_dbg(codec->dev,
298                                 "Disable noise suppression to enable ALC\n");
299                         return -EINVAL;
300                 }
301         }
302         /* If all conditions are met or we are actually disabling ALC */
303         return snd_soc_put_volsw(kcontrol, ucontrol);
304 }
305
306 /* Noise suppression can be enabled only if following conditions are met
307  *  ALC disabled
308  *  ZC enabled for HP and AUX1 PGA
309  *  INPGA_L_VOL and INPGA_R_VOL >= 10.5 dB
310  *  AUX1_L_VOL and AUX1_R_VOL >= 6 dB
311  */
312 static int da7210_put_noise_sup_sw(struct snd_kcontrol *kcontrol,
313                                    struct snd_ctl_elem_value *ucontrol)
314 {
315         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
316         u8 val;
317
318         if (ucontrol->value.integer.value[0]) {
319                 /* Check if ALC is enabled */
320                 if (snd_soc_read(codec, DA7210_ADC) & DA7210_ADC_ALC_EN)
321                         goto err;
322
323                 /* Check ZC for HP and AUX1 PGA */
324                 if ((snd_soc_read(codec, DA7210_ZERO_CROSS) &
325                         (DA7210_AUX1_L_ZC | DA7210_AUX1_R_ZC | DA7210_HP_L_ZC |
326                         DA7210_HP_R_ZC)) != (DA7210_AUX1_L_ZC |
327                         DA7210_AUX1_R_ZC | DA7210_HP_L_ZC | DA7210_HP_R_ZC))
328                         goto err;
329
330                 /* Check INPGA_L_VOL and INPGA_R_VOL */
331                 val = snd_soc_read(codec, DA7210_IN_GAIN);
332                 if (((val & DA7210_INPGA_L_VOL) < DA7210_INPGA_MIN_VOL_NS) ||
333                         (((val & DA7210_INPGA_R_VOL) >> 4) <
334                         DA7210_INPGA_MIN_VOL_NS))
335                         goto err;
336
337                 /* Check AUX1_L_VOL and AUX1_R_VOL */
338                 if (((snd_soc_read(codec, DA7210_AUX1_L) & DA7210_AUX1_L_VOL) <
339                     DA7210_AUX1_MIN_VOL_NS) ||
340                     ((snd_soc_read(codec, DA7210_AUX1_R) & DA7210_AUX1_R_VOL) <
341                     DA7210_AUX1_MIN_VOL_NS))
342                         goto err;
343         }
344         /* If all conditions are met or we are actually disabling Noise sup */
345         return snd_soc_put_volsw(kcontrol, ucontrol);
346
347 err:
348         return -EINVAL;
349 }
350
351 static const struct snd_kcontrol_new da7210_snd_controls[] = {
352
353         SOC_DOUBLE_R_TLV("HeadPhone Playback Volume",
354                          DA7210_HP_L_VOL, DA7210_HP_R_VOL,
355                          0, 0x3F, 0, hp_out_tlv),
356         SOC_DOUBLE_R_TLV("Digital Playback Volume",
357                          DA7210_DAC_L, DA7210_DAC_R,
358                          0, 0x77, 1, dac_gain_tlv),
359         SOC_DOUBLE_R_TLV("Lineout Playback Volume",
360                          DA7210_OUT1_L, DA7210_OUT1_R,
361                          0, 0x3f, 0, lineout_vol_tlv),
362         SOC_SINGLE_TLV("Mono Playback Volume", DA7210_OUT2, 0, 0x7, 0,
363                        mono_vol_tlv),
364
365         SOC_DOUBLE_R_TLV("Mic Capture Volume",
366                          DA7210_MIC_L, DA7210_MIC_R,
367                          0, 0x5, 0, mic_vol_tlv),
368         SOC_DOUBLE_R_TLV("Aux1 Capture Volume",
369                          DA7210_AUX1_L, DA7210_AUX1_R,
370                          0, 0x3f, 0, aux1_vol_tlv),
371         SOC_SINGLE_TLV("Aux2 Capture Volume", DA7210_AUX2, 0, 0x3, 0,
372                        aux2_vol_tlv),
373         SOC_DOUBLE_TLV("In PGA Capture Volume", DA7210_IN_GAIN, 0, 4, 0xF, 0,
374                        inpga_gain_tlv),
375
376         /* DAC Equalizer  controls */
377         SOC_SINGLE("DAC EQ Switch", DA7210_DAC_EQ5, 7, 1, 0),
378         SOC_SINGLE_TLV("DAC EQ1 Volume", DA7210_DAC_EQ1_2, 0, 0xf, 1,
379                        eq_gain_tlv),
380         SOC_SINGLE_TLV("DAC EQ2 Volume", DA7210_DAC_EQ1_2, 4, 0xf, 1,
381                        eq_gain_tlv),
382         SOC_SINGLE_TLV("DAC EQ3 Volume", DA7210_DAC_EQ3_4, 0, 0xf, 1,
383                        eq_gain_tlv),
384         SOC_SINGLE_TLV("DAC EQ4 Volume", DA7210_DAC_EQ3_4, 4, 0xf, 1,
385                        eq_gain_tlv),
386         SOC_SINGLE_TLV("DAC EQ5 Volume", DA7210_DAC_EQ5, 0, 0xf, 1,
387                        eq_gain_tlv),
388
389         /* ADC Equalizer  controls */
390         SOC_SINGLE("ADC EQ Switch", DA7210_ADC_EQ5, 7, 1, 0),
391         SOC_SINGLE_TLV("ADC EQ Master Volume", DA7210_ADC_EQ5, 4, 0x3,
392                        1, adc_eq_master_gain_tlv),
393         SOC_SINGLE_TLV("ADC EQ1 Volume", DA7210_ADC_EQ1_2, 0, 0xf, 1,
394                        eq_gain_tlv),
395         SOC_SINGLE_TLV("ADC EQ2 Volume", DA7210_ADC_EQ1_2, 4, 0xf, 1,
396                        eq_gain_tlv),
397         SOC_SINGLE_TLV("ADC EQ3 Volume", DA7210_ADC_EQ3_4, 0, 0xf, 1,
398                        eq_gain_tlv),
399         SOC_SINGLE_TLV("ADC EQ4 Volume", DA7210_ADC_EQ3_4, 4, 0xf, 1,
400                        eq_gain_tlv),
401         SOC_SINGLE_TLV("ADC EQ5 Volume", DA7210_ADC_EQ5, 0, 0xf, 1,
402                        eq_gain_tlv),
403
404         SOC_SINGLE("DAC HPF Switch", DA7210_DAC_HPF, 3, 1, 0),
405         SOC_ENUM("DAC HPF Cutoff", da7210_dac_hpf_cutoff),
406         SOC_SINGLE("DAC Voice Mode Switch", DA7210_DAC_HPF, 7, 1, 0),
407         SOC_ENUM("DAC Voice Cutoff", da7210_dac_vf_cutoff),
408
409         SOC_SINGLE("ADC HPF Switch", DA7210_ADC_HPF, 3, 1, 0),
410         SOC_ENUM("ADC HPF Cutoff", da7210_adc_hpf_cutoff),
411         SOC_SINGLE("ADC Voice Mode Switch", DA7210_ADC_HPF, 7, 1, 0),
412         SOC_ENUM("ADC Voice Cutoff", da7210_adc_vf_cutoff),
413
414         /* Mute controls */
415         SOC_DOUBLE_R("Mic Capture Switch", DA7210_MIC_L, DA7210_MIC_R, 3, 1, 0),
416         SOC_SINGLE("Aux2 Capture Switch", DA7210_AUX2, 2, 1, 0),
417         SOC_DOUBLE("ADC Capture Switch", DA7210_ADC, 2, 6, 1, 0),
418         SOC_SINGLE("Digital Soft Mute Switch", DA7210_SOFTMUTE, 7, 1, 0),
419         SOC_SINGLE("Digital Soft Mute Rate", DA7210_SOFTMUTE, 0, 0x7, 0),
420
421         /* Zero cross controls */
422         SOC_DOUBLE("Aux1 ZC Switch", DA7210_ZERO_CROSS, 0, 1, 1, 0),
423         SOC_DOUBLE("In PGA ZC Switch", DA7210_ZERO_CROSS, 2, 3, 1, 0),
424         SOC_DOUBLE("Lineout ZC Switch", DA7210_ZERO_CROSS, 4, 5, 1, 0),
425         SOC_DOUBLE("Headphone ZC Switch", DA7210_ZERO_CROSS, 6, 7, 1, 0),
426
427         SOC_ENUM("Headphone Class", da7210_hp_mode_sel),
428
429         /* ALC controls */
430         SOC_SINGLE_EXT("ALC Enable Switch", DA7210_ADC, 0, 1, 0,
431                        snd_soc_get_volsw, da7210_put_alc_sw),
432         SOC_SINGLE("ALC Capture Max Volume", DA7210_ALC_MAX, 0, 0x3F, 0),
433         SOC_SINGLE("ALC Capture Min Volume", DA7210_ALC_MIN, 0, 0x3F, 0),
434         SOC_SINGLE("ALC Capture Noise Volume", DA7210_ALC_NOIS, 0, 0x3F, 0),
435         SOC_SINGLE("ALC Capture Attack Rate", DA7210_ALC_ATT, 0, 0xFF, 0),
436         SOC_SINGLE("ALC Capture Release Rate", DA7210_ALC_REL, 0, 0xFF, 0),
437         SOC_SINGLE("ALC Capture Release Delay", DA7210_ALC_DEL, 0, 0xFF, 0),
438
439         SOC_SINGLE_EXT("Noise Suppression Enable Switch", DA7210_CONTROL, 3, 1,
440                        0, snd_soc_get_volsw, da7210_put_noise_sup_sw),
441 };
442
443 /*
444  * DAPM Controls
445  *
446  * Current DAPM implementation covers almost all codec components e.g. IOs,
447  * mixers, PGAs,ADC and DAC.
448  */
449 /* In Mixer Left */
450 static const struct snd_kcontrol_new da7210_dapm_inmixl_controls[] = {
451         SOC_DAPM_SINGLE("Mic Left Switch", DA7210_INMIX_L, 0, 1, 0),
452         SOC_DAPM_SINGLE("Mic Right Switch", DA7210_INMIX_L, 1, 1, 0),
453         SOC_DAPM_SINGLE("Aux1 Left Switch", DA7210_INMIX_L, 2, 1, 0),
454         SOC_DAPM_SINGLE("Aux2 Switch", DA7210_INMIX_L, 3, 1, 0),
455         SOC_DAPM_SINGLE("Outmix Left Switch", DA7210_INMIX_L, 4, 1, 0),
456 };
457
458 /* In Mixer Right */
459 static const struct snd_kcontrol_new da7210_dapm_inmixr_controls[] = {
460         SOC_DAPM_SINGLE("Mic Right Switch", DA7210_INMIX_R, 0, 1, 0),
461         SOC_DAPM_SINGLE("Mic Left Switch", DA7210_INMIX_R, 1, 1, 0),
462         SOC_DAPM_SINGLE("Aux1 Right Switch", DA7210_INMIX_R, 2, 1, 0),
463         SOC_DAPM_SINGLE("Aux2 Switch", DA7210_INMIX_R, 3, 1, 0),
464         SOC_DAPM_SINGLE("Outmix Right Switch", DA7210_INMIX_R, 4, 1, 0),
465 };
466
467 /* Out Mixer Left */
468 static const struct snd_kcontrol_new da7210_dapm_outmixl_controls[] = {
469         SOC_DAPM_SINGLE("Aux1 Left Switch", DA7210_OUTMIX_L, 0, 1, 0),
470         SOC_DAPM_SINGLE("Aux2 Switch", DA7210_OUTMIX_L, 1, 1, 0),
471         SOC_DAPM_SINGLE("INPGA Left Switch", DA7210_OUTMIX_L, 2, 1, 0),
472         SOC_DAPM_SINGLE("INPGA Right Switch", DA7210_OUTMIX_L, 3, 1, 0),
473         SOC_DAPM_SINGLE("DAC Left Switch", DA7210_OUTMIX_L, 4, 1, 0),
474 };
475
476 /* Out Mixer Right */
477 static const struct snd_kcontrol_new da7210_dapm_outmixr_controls[] = {
478         SOC_DAPM_SINGLE("Aux1 Right Switch", DA7210_OUTMIX_R, 0, 1, 0),
479         SOC_DAPM_SINGLE("Aux2 Switch", DA7210_OUTMIX_R, 1, 1, 0),
480         SOC_DAPM_SINGLE("INPGA Left Switch", DA7210_OUTMIX_R, 2, 1, 0),
481         SOC_DAPM_SINGLE("INPGA Right Switch", DA7210_OUTMIX_R, 3, 1, 0),
482         SOC_DAPM_SINGLE("DAC Right Switch", DA7210_OUTMIX_R, 4, 1, 0),
483 };
484
485 /* Mono Mixer */
486 static const struct snd_kcontrol_new da7210_dapm_monomix_controls[] = {
487         SOC_DAPM_SINGLE("INPGA Right Switch", DA7210_OUT2, 3, 1, 0),
488         SOC_DAPM_SINGLE("INPGA Left Switch", DA7210_OUT2, 4, 1, 0),
489         SOC_DAPM_SINGLE("Outmix Right Switch", DA7210_OUT2, 5, 1, 0),
490         SOC_DAPM_SINGLE("Outmix Left Switch", DA7210_OUT2, 6, 1, 0),
491 };
492
493 /* DAPM widgets */
494 static const struct snd_soc_dapm_widget da7210_dapm_widgets[] = {
495         /* Input Side */
496         /* Input Lines */
497         SND_SOC_DAPM_INPUT("MICL"),
498         SND_SOC_DAPM_INPUT("MICR"),
499         SND_SOC_DAPM_INPUT("AUX1L"),
500         SND_SOC_DAPM_INPUT("AUX1R"),
501         SND_SOC_DAPM_INPUT("AUX2"),
502
503         /* Input PGAs */
504         SND_SOC_DAPM_PGA("Mic Left", DA7210_STARTUP3, 0, 1, NULL, 0),
505         SND_SOC_DAPM_PGA("Mic Right", DA7210_STARTUP3, 1, 1, NULL, 0),
506         SND_SOC_DAPM_PGA("Aux1 Left", DA7210_STARTUP3, 2, 1, NULL, 0),
507         SND_SOC_DAPM_PGA("Aux1 Right", DA7210_STARTUP3, 3, 1, NULL, 0),
508         SND_SOC_DAPM_PGA("Aux2 Mono", DA7210_STARTUP3, 4, 1, NULL, 0),
509
510         SND_SOC_DAPM_PGA("INPGA Left", DA7210_INMIX_L, 7, 0, NULL, 0),
511         SND_SOC_DAPM_PGA("INPGA Right", DA7210_INMIX_R, 7, 0, NULL, 0),
512
513         /* MICBIAS */
514         SND_SOC_DAPM_SUPPLY("Mic Bias", DA7210_MIC_L, 6, 0, NULL, 0),
515
516         /* Input Mixers */
517         SND_SOC_DAPM_MIXER("In Mixer Left", SND_SOC_NOPM, 0, 0,
518                 &da7210_dapm_inmixl_controls[0],
519                 ARRAY_SIZE(da7210_dapm_inmixl_controls)),
520
521         SND_SOC_DAPM_MIXER("In Mixer Right", SND_SOC_NOPM, 0, 0,
522                 &da7210_dapm_inmixr_controls[0],
523                 ARRAY_SIZE(da7210_dapm_inmixr_controls)),
524
525         /* ADCs */
526         SND_SOC_DAPM_ADC("ADC Left", "Capture", DA7210_STARTUP3, 5, 1),
527         SND_SOC_DAPM_ADC("ADC Right", "Capture", DA7210_STARTUP3, 6, 1),
528
529         /* Output Side */
530         /* DACs */
531         SND_SOC_DAPM_DAC("DAC Left", "Playback", DA7210_STARTUP2, 5, 1),
532         SND_SOC_DAPM_DAC("DAC Right", "Playback", DA7210_STARTUP2, 6, 1),
533
534         /* Output Mixers */
535         SND_SOC_DAPM_MIXER("Out Mixer Left", SND_SOC_NOPM, 0, 0,
536                 &da7210_dapm_outmixl_controls[0],
537                 ARRAY_SIZE(da7210_dapm_outmixl_controls)),
538
539         SND_SOC_DAPM_MIXER("Out Mixer Right", SND_SOC_NOPM, 0, 0,
540                 &da7210_dapm_outmixr_controls[0],
541                 ARRAY_SIZE(da7210_dapm_outmixr_controls)),
542
543         SND_SOC_DAPM_MIXER("Mono Mixer", SND_SOC_NOPM, 0, 0,
544                 &da7210_dapm_monomix_controls[0],
545                 ARRAY_SIZE(da7210_dapm_monomix_controls)),
546
547         /* Output PGAs */
548         SND_SOC_DAPM_PGA("OUTPGA Left Enable", DA7210_OUTMIX_L, 7, 0, NULL, 0),
549         SND_SOC_DAPM_PGA("OUTPGA Right Enable", DA7210_OUTMIX_R, 7, 0, NULL, 0),
550
551         SND_SOC_DAPM_PGA("Out1 Left", DA7210_STARTUP2, 0, 1, NULL, 0),
552         SND_SOC_DAPM_PGA("Out1 Right", DA7210_STARTUP2, 1, 1, NULL, 0),
553         SND_SOC_DAPM_PGA("Out2 Mono", DA7210_STARTUP2, 2, 1, NULL, 0),
554         SND_SOC_DAPM_PGA("Headphone Left", DA7210_STARTUP2, 3, 1, NULL, 0),
555         SND_SOC_DAPM_PGA("Headphone Right", DA7210_STARTUP2, 4, 1, NULL, 0),
556
557         /* Output Lines */
558         SND_SOC_DAPM_OUTPUT("OUT1L"),
559         SND_SOC_DAPM_OUTPUT("OUT1R"),
560         SND_SOC_DAPM_OUTPUT("HPL"),
561         SND_SOC_DAPM_OUTPUT("HPR"),
562         SND_SOC_DAPM_OUTPUT("OUT2"),
563 };
564
565 /* DAPM audio route definition */
566 static const struct snd_soc_dapm_route da7210_audio_map[] = {
567         /* Dest       Connecting Widget    source */
568         /* Input path */
569         {"Mic Left", NULL, "MICL"},
570         {"Mic Right", NULL, "MICR"},
571         {"Aux1 Left", NULL, "AUX1L"},
572         {"Aux1 Right", NULL, "AUX1R"},
573         {"Aux2 Mono", NULL, "AUX2"},
574
575         {"In Mixer Left", "Mic Left Switch", "Mic Left"},
576         {"In Mixer Left", "Mic Right Switch", "Mic Right"},
577         {"In Mixer Left", "Aux1 Left Switch", "Aux1 Left"},
578         {"In Mixer Left", "Aux2 Switch", "Aux2 Mono"},
579         {"In Mixer Left", "Outmix Left Switch", "Out Mixer Left"},
580
581         {"In Mixer Right", "Mic Right Switch", "Mic Right"},
582         {"In Mixer Right", "Mic Left Switch", "Mic Left"},
583         {"In Mixer Right", "Aux1 Right Switch", "Aux1 Right"},
584         {"In Mixer Right", "Aux2 Switch", "Aux2 Mono"},
585         {"In Mixer Right", "Outmix Right Switch", "Out Mixer Right"},
586
587         {"INPGA Left", NULL, "In Mixer Left"},
588         {"ADC Left", NULL, "INPGA Left"},
589
590         {"INPGA Right", NULL, "In Mixer Right"},
591         {"ADC Right", NULL, "INPGA Right"},
592
593         /* Output path */
594         {"Out Mixer Left", "Aux1 Left Switch", "Aux1 Left"},
595         {"Out Mixer Left", "Aux2 Switch", "Aux2 Mono"},
596         {"Out Mixer Left", "INPGA Left Switch", "INPGA Left"},
597         {"Out Mixer Left", "INPGA Right Switch", "INPGA Right"},
598         {"Out Mixer Left", "DAC Left Switch", "DAC Left"},
599
600         {"Out Mixer Right", "Aux1 Right Switch", "Aux1 Right"},
601         {"Out Mixer Right", "Aux2 Switch", "Aux2 Mono"},
602         {"Out Mixer Right", "INPGA Right Switch", "INPGA Right"},
603         {"Out Mixer Right", "INPGA Left Switch", "INPGA Left"},
604         {"Out Mixer Right", "DAC Right Switch", "DAC Right"},
605
606         {"Mono Mixer", "INPGA Right Switch", "INPGA Right"},
607         {"Mono Mixer", "INPGA Left Switch", "INPGA Left"},
608         {"Mono Mixer", "Outmix Right Switch", "Out Mixer Right"},
609         {"Mono Mixer", "Outmix Left Switch", "Out Mixer Left"},
610
611         {"OUTPGA Left Enable", NULL, "Out Mixer Left"},
612         {"OUTPGA Right Enable", NULL, "Out Mixer Right"},
613
614         {"Out1 Left", NULL, "OUTPGA Left Enable"},
615         {"OUT1L", NULL, "Out1 Left"},
616
617         {"Out1 Right", NULL, "OUTPGA Right Enable"},
618         {"OUT1R", NULL, "Out1 Right"},
619
620         {"Headphone Left", NULL, "OUTPGA Left Enable"},
621         {"HPL", NULL, "Headphone Left"},
622
623         {"Headphone Right", NULL, "OUTPGA Right Enable"},
624         {"HPR", NULL, "Headphone Right"},
625
626         {"Out2 Mono", NULL, "Mono Mixer"},
627         {"OUT2", NULL, "Out2 Mono"},
628 };
629
630 /* Codec private data */
631 struct da7210_priv {
632         struct regmap *regmap;
633 };
634
635 static struct reg_default da7210_reg_defaults[] = {
636         { 0x00, 0x00 },
637         { 0x01, 0x11 },
638         { 0x03, 0x00 },
639         { 0x04, 0x00 },
640         { 0x05, 0x00 },
641         { 0x06, 0x00 },
642         { 0x07, 0x00 },
643         { 0x08, 0x00 },
644         { 0x09, 0x00 },
645         { 0x0a, 0x00 },
646         { 0x0b, 0x00 },
647         { 0x0c, 0x00 },
648         { 0x0d, 0x00 },
649         { 0x0e, 0x00 },
650         { 0x0f, 0x08 },
651         { 0x10, 0x00 },
652         { 0x11, 0x00 },
653         { 0x12, 0x00 },
654         { 0x13, 0x00 },
655         { 0x14, 0x08 },
656         { 0x15, 0x10 },
657         { 0x16, 0x10 },
658         { 0x17, 0x54 },
659         { 0x18, 0x40 },
660         { 0x19, 0x00 },
661         { 0x1a, 0x00 },
662         { 0x1b, 0x00 },
663         { 0x1c, 0x00 },
664         { 0x1d, 0x00 },
665         { 0x1e, 0x00 },
666         { 0x1f, 0x00 },
667         { 0x20, 0x00 },
668         { 0x21, 0x00 },
669         { 0x22, 0x00 },
670         { 0x23, 0x02 },
671         { 0x24, 0x00 },
672         { 0x25, 0x76 },
673         { 0x26, 0x00 },
674         { 0x27, 0x00 },
675         { 0x28, 0x04 },
676         { 0x29, 0x00 },
677         { 0x2a, 0x00 },
678         { 0x2b, 0x30 },
679         { 0x2c, 0x2A },
680         { 0x83, 0x00 },
681         { 0x84, 0x00 },
682         { 0x85, 0x00 },
683         { 0x86, 0x00 },
684         { 0x87, 0x00 },
685         { 0x88, 0x00 },
686 };
687
688 static bool da7210_readable_register(struct device *dev, unsigned int reg)
689 {
690         switch (reg) {
691         case DA7210_A_HID_UNLOCK:
692         case DA7210_A_TEST_UNLOCK:
693         case DA7210_A_PLL1:
694         case DA7210_A_CP_MODE:
695                 return false;
696         default:
697                 return true;
698         }
699 }
700
701 static bool da7210_volatile_register(struct device *dev,
702                                     unsigned int reg)
703 {
704         switch (reg) {
705         case DA7210_STATUS:
706                 return true;
707         default:
708                 return false;
709         }
710 }
711
712 /*
713  * Set PCM DAI word length.
714  */
715 static int da7210_hw_params(struct snd_pcm_substream *substream,
716                             struct snd_pcm_hw_params *params,
717                             struct snd_soc_dai *dai)
718 {
719         struct snd_soc_codec *codec = dai->codec;
720         u32 dai_cfg1;
721         u32 fs, bypass;
722
723         /* set DAI source to Left and Right ADC */
724         snd_soc_write(codec, DA7210_DAI_SRC_SEL,
725                      DA7210_DAI_OUT_R_SRC | DA7210_DAI_OUT_L_SRC);
726
727         /* Enable DAI */
728         snd_soc_write(codec, DA7210_DAI_CFG3, DA7210_DAI_OE | DA7210_DAI_EN);
729
730         dai_cfg1 = 0xFC & snd_soc_read(codec, DA7210_DAI_CFG1);
731
732         switch (params_format(params)) {
733         case SNDRV_PCM_FORMAT_S16_LE:
734                 dai_cfg1 |= DA7210_DAI_WORD_S16_LE;
735                 break;
736         case SNDRV_PCM_FORMAT_S20_3LE:
737                 dai_cfg1 |= DA7210_DAI_WORD_S20_3LE;
738                 break;
739         case SNDRV_PCM_FORMAT_S24_LE:
740                 dai_cfg1 |= DA7210_DAI_WORD_S24_LE;
741                 break;
742         case SNDRV_PCM_FORMAT_S32_LE:
743                 dai_cfg1 |= DA7210_DAI_WORD_S32_LE;
744                 break;
745         default:
746                 return -EINVAL;
747         }
748
749         snd_soc_write(codec, DA7210_DAI_CFG1, dai_cfg1);
750
751         switch (params_rate(params)) {
752         case 8000:
753                 fs              = DA7210_PLL_FS_8000;
754                 bypass          = DA7210_PLL_BYP;
755                 break;
756         case 11025:
757                 fs              = DA7210_PLL_FS_11025;
758                 bypass          = 0;
759                 break;
760         case 12000:
761                 fs              = DA7210_PLL_FS_12000;
762                 bypass          = DA7210_PLL_BYP;
763                 break;
764         case 16000:
765                 fs              = DA7210_PLL_FS_16000;
766                 bypass          = DA7210_PLL_BYP;
767                 break;
768         case 22050:
769                 fs              = DA7210_PLL_FS_22050;
770                 bypass          = 0;
771                 break;
772         case 32000:
773                 fs              = DA7210_PLL_FS_32000;
774                 bypass          = DA7210_PLL_BYP;
775                 break;
776         case 44100:
777                 fs              = DA7210_PLL_FS_44100;
778                 bypass          = 0;
779                 break;
780         case 48000:
781                 fs              = DA7210_PLL_FS_48000;
782                 bypass          = DA7210_PLL_BYP;
783                 break;
784         case 88200:
785                 fs              = DA7210_PLL_FS_88200;
786                 bypass          = 0;
787                 break;
788         case 96000:
789                 fs              = DA7210_PLL_FS_96000;
790                 bypass          = DA7210_PLL_BYP;
791                 break;
792         default:
793                 return -EINVAL;
794         }
795
796         /* Disable active mode */
797         snd_soc_update_bits(codec, DA7210_STARTUP1, DA7210_SC_MST_EN, 0);
798
799         snd_soc_update_bits(codec, DA7210_PLL, DA7210_PLL_FS_MASK, fs);
800         snd_soc_update_bits(codec, DA7210_PLL_DIV3, DA7210_PLL_BYP, bypass);
801
802         /* Enable active mode */
803         snd_soc_update_bits(codec, DA7210_STARTUP1,
804                             DA7210_SC_MST_EN, DA7210_SC_MST_EN);
805
806         return 0;
807 }
808
809 /*
810  * Set DAI mode and Format
811  */
812 static int da7210_set_dai_fmt(struct snd_soc_dai *codec_dai, u32 fmt)
813 {
814         struct snd_soc_codec *codec = codec_dai->codec;
815         u32 dai_cfg1;
816         u32 dai_cfg3;
817
818         dai_cfg1 = 0x7f & snd_soc_read(codec, DA7210_DAI_CFG1);
819         dai_cfg3 = 0xfc & snd_soc_read(codec, DA7210_DAI_CFG3);
820
821         switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
822         case SND_SOC_DAIFMT_CBM_CFM:
823                 dai_cfg1 |= DA7210_DAI_MODE_MASTER;
824                 break;
825         case SND_SOC_DAIFMT_CBS_CFS:
826                 dai_cfg1 |= DA7210_DAI_MODE_SLAVE;
827                 break;
828         default:
829                 return -EINVAL;
830         }
831
832         /* FIXME
833          *
834          * It support I2S only now
835          */
836         switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
837         case SND_SOC_DAIFMT_I2S:
838                 dai_cfg3 |= DA7210_DAI_FORMAT_I2SMODE;
839                 break;
840         case SND_SOC_DAIFMT_LEFT_J:
841                 dai_cfg3 |= DA7210_DAI_FORMAT_LEFT_J;
842                 break;
843         case SND_SOC_DAIFMT_RIGHT_J:
844                 dai_cfg3 |= DA7210_DAI_FORMAT_RIGHT_J;
845                 break;
846         default:
847                 return -EINVAL;
848         }
849
850         /* FIXME
851          *
852          * It support 64bit data transmission only now
853          */
854         dai_cfg1 |= DA7210_DAI_FLEN_64BIT;
855
856         snd_soc_write(codec, DA7210_DAI_CFG1, dai_cfg1);
857         snd_soc_write(codec, DA7210_DAI_CFG3, dai_cfg3);
858
859         return 0;
860 }
861
862 static int da7210_mute(struct snd_soc_dai *dai, int mute)
863 {
864         struct snd_soc_codec *codec = dai->codec;
865         u8 mute_reg = snd_soc_read(codec, DA7210_DAC_HPF) & 0xFB;
866
867         if (mute)
868                 snd_soc_write(codec, DA7210_DAC_HPF, mute_reg | 0x4);
869         else
870                 snd_soc_write(codec, DA7210_DAC_HPF, mute_reg);
871         return 0;
872 }
873
874 #define DA7210_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
875                         SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
876
877 /* DAI operations */
878 static const struct snd_soc_dai_ops da7210_dai_ops = {
879         .hw_params      = da7210_hw_params,
880         .set_fmt        = da7210_set_dai_fmt,
881         .digital_mute   = da7210_mute,
882 };
883
884 static struct snd_soc_dai_driver da7210_dai = {
885         .name = "da7210-hifi",
886         /* playback capabilities */
887         .playback = {
888                 .stream_name = "Playback",
889                 .channels_min = 1,
890                 .channels_max = 2,
891                 .rates = SNDRV_PCM_RATE_8000_96000,
892                 .formats = DA7210_FORMATS,
893         },
894         /* capture capabilities */
895         .capture = {
896                 .stream_name = "Capture",
897                 .channels_min = 1,
898                 .channels_max = 2,
899                 .rates = SNDRV_PCM_RATE_8000_96000,
900                 .formats = DA7210_FORMATS,
901         },
902         .ops = &da7210_dai_ops,
903         .symmetric_rates = 1,
904 };
905
906 static int da7210_probe(struct snd_soc_codec *codec)
907 {
908         struct da7210_priv *da7210 = snd_soc_codec_get_drvdata(codec);
909         int ret;
910
911         dev_info(codec->dev, "DA7210 Audio Codec %s\n", DA7210_VERSION);
912
913         codec->control_data = da7210->regmap;
914         ret = snd_soc_codec_set_cache_io(codec, 8, 8, SND_SOC_REGMAP);
915         if (ret < 0) {
916                 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
917                 return ret;
918         }
919
920         /* FIXME
921          *
922          * This driver use fixed value here
923          * And below settings expects MCLK = 12.288MHz
924          *
925          * When you select different MCLK, please check...
926          *      DA7210_PLL_DIV1 val
927          *      DA7210_PLL_DIV2 val
928          *      DA7210_PLL_DIV3 val
929          *      DA7210_PLL_DIV3 :: DA7210_MCLK_RANGExxx
930          */
931
932         /*
933          * ADC settings
934          */
935
936         /* Enable Left & Right MIC PGA and Mic Bias */
937         snd_soc_write(codec, DA7210_MIC_L, DA7210_MIC_L_EN | DA7210_MICBIAS_EN);
938         snd_soc_write(codec, DA7210_MIC_R, DA7210_MIC_R_EN);
939
940         /* Enable Left and Right input PGA */
941         snd_soc_write(codec, DA7210_INMIX_L, DA7210_IN_L_EN);
942         snd_soc_write(codec, DA7210_INMIX_R, DA7210_IN_R_EN);
943
944         /* Enable Left and Right ADC */
945         snd_soc_write(codec, DA7210_ADC, DA7210_ADC_L_EN | DA7210_ADC_R_EN);
946
947         /*
948          * DAC settings
949          */
950
951         /* Enable Left and Right DAC */
952         snd_soc_write(codec, DA7210_DAC_SEL,
953                      DA7210_DAC_L_SRC_DAI_L | DA7210_DAC_L_EN |
954                      DA7210_DAC_R_SRC_DAI_R | DA7210_DAC_R_EN);
955
956         /* Enable Left and Right out PGA */
957         snd_soc_write(codec, DA7210_OUTMIX_L, DA7210_OUT_L_EN);
958         snd_soc_write(codec, DA7210_OUTMIX_R, DA7210_OUT_R_EN);
959
960         /* Enable Left and Right HeadPhone PGA */
961         snd_soc_write(codec, DA7210_HP_CFG,
962                      DA7210_HP_2CAP_MODE | DA7210_HP_SENSE_EN |
963                      DA7210_HP_L_EN | DA7210_HP_MODE | DA7210_HP_R_EN);
964
965         /* Enable ramp mode for DAC gain update */
966         snd_soc_write(codec, DA7210_SOFTMUTE, DA7210_RAMP_EN);
967
968         /*
969          * For DA7210 codec, there are two ways to enable/disable analog IOs
970          * and ADC/DAC,
971          * (1) Using "Enable Bit" of register associated with that IO
972          * (or ADC/DAC)
973          *      e.g. Mic Left can be enabled using bit 7 of MIC_L(0x7) reg
974          *
975          * (2) Using "Standby Bit" of STARTUP2 or STARTUP3 register
976          *      e.g. Mic left can be put to STANDBY using bit 0 of STARTUP3(0x5)
977          *
978          * Out of these two methods, the one using STANDBY bits is preferred
979          * way to enable/disable individual blocks. This is because STANDBY
980          * registers are part of system controller which allows system power
981          * up/down in a controlled, pop-free manner. Also, as per application
982          * note of DA7210, STANDBY register bits are only effective if a
983          * particular IO (or ADC/DAC) is already enabled using enable/disable
984          * register bits. Keeping these things in mind, current DAPM
985          * implementation manipulates only STANDBY bits.
986          *
987          * Overall implementation can be outlined as below,
988          *
989          * - "Enable bit" of an IO or ADC/DAC is used to enable it in probe()
990          * - "STANDBY bit" is controlled by DAPM
991          */
992
993         /* Enable Line out amplifiers */
994         snd_soc_write(codec, DA7210_OUT1_L, DA7210_OUT1_L_EN);
995         snd_soc_write(codec, DA7210_OUT1_R, DA7210_OUT1_R_EN);
996         snd_soc_write(codec, DA7210_OUT2, DA7210_OUT2_EN |
997                      DA7210_OUT2_OUTMIX_L | DA7210_OUT2_OUTMIX_R);
998
999         /* Enable Aux1 */
1000         snd_soc_write(codec, DA7210_AUX1_L, DA7210_AUX1_L_EN);
1001         snd_soc_write(codec, DA7210_AUX1_R, DA7210_AUX1_R_EN);
1002         /* Enable Aux2 */
1003         snd_soc_write(codec, DA7210_AUX2, DA7210_AUX2_EN);
1004
1005         /* Diable PLL and bypass it */
1006         snd_soc_write(codec, DA7210_PLL, DA7210_PLL_FS_48000);
1007
1008         /*
1009          * If 48kHz sound came, it use bypass mode,
1010          * and when it is 44.1kHz, it use PLL.
1011          *
1012          * This time, this driver sets PLL always ON
1013          * and controls bypass/PLL mode by switching
1014          * DA7210_PLL_DIV3 :: DA7210_PLL_BYP bit.
1015          *   see da7210_hw_params
1016          */
1017         snd_soc_write(codec, DA7210_PLL_DIV1, 0xE5); /* MCLK = 12.288MHz */
1018         snd_soc_write(codec, DA7210_PLL_DIV2, 0x99);
1019         snd_soc_write(codec, DA7210_PLL_DIV3, 0x0A |
1020                      DA7210_MCLK_RANGE_10_20_MHZ | DA7210_PLL_BYP);
1021         snd_soc_update_bits(codec, DA7210_PLL, DA7210_PLL_EN, DA7210_PLL_EN);
1022
1023         /* Activate all enabled subsystem */
1024         snd_soc_write(codec, DA7210_STARTUP1, DA7210_SC_MST_EN);
1025
1026         dev_info(codec->dev, "DA7210 Audio Codec %s\n", DA7210_VERSION);
1027
1028         return 0;
1029 }
1030
1031 static struct snd_soc_codec_driver soc_codec_dev_da7210 = {
1032         .probe                  = da7210_probe,
1033
1034         .controls               = da7210_snd_controls,
1035         .num_controls           = ARRAY_SIZE(da7210_snd_controls),
1036
1037         .dapm_widgets           = da7210_dapm_widgets,
1038         .num_dapm_widgets       = ARRAY_SIZE(da7210_dapm_widgets),
1039         .dapm_routes            = da7210_audio_map,
1040         .num_dapm_routes        = ARRAY_SIZE(da7210_audio_map),
1041 };
1042
1043 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
1044
1045 static struct reg_default da7210_regmap_i2c_patch[] = {
1046
1047         /* System controller master disable */
1048         { DA7210_STARTUP1, 0x00 },
1049         /* make sure that DA7210 use bypass mode before start up */
1050         { DA7210_PLL_DIV3, DA7210_MCLK_RANGE_10_20_MHZ | DA7210_PLL_BYP },
1051
1052         /* to unlock */
1053         { DA7210_A_HID_UNLOCK, 0x8B},
1054         { DA7210_A_TEST_UNLOCK, 0xB4},
1055         { DA7210_A_PLL1, 0x01},
1056         { DA7210_A_CP_MODE, 0x7C},
1057         /* to re-lock */
1058         { DA7210_A_HID_UNLOCK, 0x00},
1059         { DA7210_A_TEST_UNLOCK, 0x00},
1060 };
1061
1062 static const struct regmap_config da7210_regmap_config_i2c = {
1063         .reg_bits = 8,
1064         .val_bits = 8,
1065
1066         .reg_defaults = da7210_reg_defaults,
1067         .num_reg_defaults = ARRAY_SIZE(da7210_reg_defaults),
1068         .volatile_reg = da7210_volatile_register,
1069         .readable_reg = da7210_readable_register,
1070         .cache_type = REGCACHE_RBTREE,
1071 };
1072
1073 static int __devinit da7210_i2c_probe(struct i2c_client *i2c,
1074                                       const struct i2c_device_id *id)
1075 {
1076         struct da7210_priv *da7210;
1077         int ret;
1078
1079         da7210 = devm_kzalloc(&i2c->dev, sizeof(struct da7210_priv),
1080                               GFP_KERNEL);
1081         if (!da7210)
1082                 return -ENOMEM;
1083
1084         i2c_set_clientdata(i2c, da7210);
1085
1086         da7210->regmap = regmap_init_i2c(i2c, &da7210_regmap_config_i2c);
1087         if (IS_ERR(da7210->regmap)) {
1088                 ret = PTR_ERR(da7210->regmap);
1089                 dev_err(&i2c->dev, "regmap_init() failed: %d\n", ret);
1090                 return ret;
1091         }
1092
1093         ret = regmap_register_patch(da7210->regmap, da7210_regmap_i2c_patch,
1094                                     ARRAY_SIZE(da7210_regmap_i2c_patch));
1095         if (ret != 0)
1096                 dev_warn(&i2c->dev, "Failed to apply regmap patch: %d\n", ret);
1097
1098         ret =  snd_soc_register_codec(&i2c->dev,
1099                         &soc_codec_dev_da7210, &da7210_dai, 1);
1100         if (ret < 0) {
1101                 dev_err(&i2c->dev, "Failed to register codec: %d\n", ret);
1102                 goto err_regmap;
1103         }
1104         return ret;
1105
1106 err_regmap:
1107         regmap_exit(da7210->regmap);
1108
1109         return ret;
1110 }
1111
1112 static int __devexit da7210_i2c_remove(struct i2c_client *client)
1113 {
1114         struct da7210_priv *da7210 = i2c_get_clientdata(client);
1115
1116         snd_soc_unregister_codec(&client->dev);
1117         regmap_exit(da7210->regmap);
1118         return 0;
1119 }
1120
1121 static const struct i2c_device_id da7210_i2c_id[] = {
1122         { "da7210", 0 },
1123         { }
1124 };
1125 MODULE_DEVICE_TABLE(i2c, da7210_i2c_id);
1126
1127 /* I2C codec control layer */
1128 static struct i2c_driver da7210_i2c_driver = {
1129         .driver = {
1130                 .name = "da7210",
1131                 .owner = THIS_MODULE,
1132         },
1133         .probe          = da7210_i2c_probe,
1134         .remove         = __devexit_p(da7210_i2c_remove),
1135         .id_table       = da7210_i2c_id,
1136 };
1137 #endif
1138
1139 #if defined(CONFIG_SPI_MASTER)
1140
1141 static struct reg_default da7210_regmap_spi_patch[] = {
1142         /* Dummy read to give two pulses over nCS for SPI */
1143         { DA7210_AUX2, 0x00 },
1144         { DA7210_AUX2, 0x00 },
1145
1146         /* System controller master disable */
1147         { DA7210_STARTUP1, 0x00 },
1148         /* make sure that DA7210 use bypass mode before start up */
1149         { DA7210_PLL_DIV3, DA7210_MCLK_RANGE_10_20_MHZ | DA7210_PLL_BYP },
1150
1151         /* to set PAGE1 of SPI register space */
1152         { DA7210_PAGE_CONTROL, 0x80 },
1153         /* to unlock */
1154         { DA7210_A_HID_UNLOCK, 0x8B},
1155         { DA7210_A_TEST_UNLOCK, 0xB4},
1156         { DA7210_A_PLL1, 0x01},
1157         { DA7210_A_CP_MODE, 0x7C},
1158         /* to re-lock */
1159         { DA7210_A_HID_UNLOCK, 0x00},
1160         { DA7210_A_TEST_UNLOCK, 0x00},
1161         /* to set back PAGE0 of SPI register space */
1162         { DA7210_PAGE_CONTROL, 0x00 },
1163 };
1164
1165 static const struct regmap_config da7210_regmap_config_spi = {
1166         .reg_bits = 8,
1167         .val_bits = 8,
1168         .read_flag_mask = 0x01,
1169         .write_flag_mask = 0x00,
1170
1171         .reg_defaults = da7210_reg_defaults,
1172         .num_reg_defaults = ARRAY_SIZE(da7210_reg_defaults),
1173         .volatile_reg = da7210_volatile_register,
1174         .readable_reg = da7210_readable_register,
1175         .cache_type = REGCACHE_RBTREE,
1176 };
1177
1178 static int __devinit da7210_spi_probe(struct spi_device *spi)
1179 {
1180         struct da7210_priv *da7210;
1181         int ret;
1182
1183         da7210 = devm_kzalloc(&spi->dev, sizeof(struct da7210_priv),
1184                               GFP_KERNEL);
1185         if (!da7210)
1186                 return -ENOMEM;
1187
1188         spi_set_drvdata(spi, da7210);
1189         da7210->regmap = devm_regmap_init_spi(spi, &da7210_regmap_config_spi);
1190         if (IS_ERR(da7210->regmap)) {
1191                 ret = PTR_ERR(da7210->regmap);
1192                 dev_err(&spi->dev, "Failed to register regmap: %d\n", ret);
1193                 return ret;
1194         }
1195
1196         ret = regmap_register_patch(da7210->regmap, da7210_regmap_spi_patch,
1197                                     ARRAY_SIZE(da7210_regmap_spi_patch));
1198         if (ret != 0)
1199                 dev_warn(&spi->dev, "Failed to apply regmap patch: %d\n", ret);
1200
1201         ret =  snd_soc_register_codec(&spi->dev,
1202                         &soc_codec_dev_da7210, &da7210_dai, 1);
1203         if (ret < 0)
1204                 goto err_regmap;
1205
1206         return ret;
1207
1208 err_regmap:
1209         regmap_exit(da7210->regmap);
1210
1211         return ret;
1212 }
1213
1214 static int __devexit da7210_spi_remove(struct spi_device *spi)
1215 {
1216         struct da7210_priv *da7210 = spi_get_drvdata(spi);
1217         snd_soc_unregister_codec(&spi->dev);
1218         regmap_exit(da7210->regmap);
1219         return 0;
1220 }
1221
1222 static struct spi_driver da7210_spi_driver = {
1223         .driver = {
1224                 .name = "da7210",
1225                 .owner = THIS_MODULE,
1226         },
1227         .probe = da7210_spi_probe,
1228         .remove = __devexit_p(da7210_spi_remove)
1229 };
1230 #endif
1231
1232 static int __init da7210_modinit(void)
1233 {
1234         int ret = 0;
1235 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
1236         ret = i2c_add_driver(&da7210_i2c_driver);
1237 #endif
1238 #if defined(CONFIG_SPI_MASTER)
1239         ret = spi_register_driver(&da7210_spi_driver);
1240         if (ret) {
1241                 printk(KERN_ERR "Failed to register da7210 SPI driver: %d\n",
1242                        ret);
1243         }
1244 #endif
1245         return ret;
1246 }
1247 module_init(da7210_modinit);
1248
1249 static void __exit da7210_exit(void)
1250 {
1251 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
1252         i2c_del_driver(&da7210_i2c_driver);
1253 #endif
1254 #if defined(CONFIG_SPI_MASTER)
1255         spi_unregister_driver(&da7210_spi_driver);
1256 #endif
1257 }
1258 module_exit(da7210_exit);
1259
1260 MODULE_DESCRIPTION("ASoC DA7210 driver");
1261 MODULE_AUTHOR("David Chen, Kuninori Morimoto");
1262 MODULE_LICENSE("GPL");