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