xref: /openbmc/linux/sound/soc/codecs/alc5623.c (revision 95e9fd10)
1 /*
2  * alc5623.c  --  alc562[123] ALSA Soc Audio driver
3  *
4  * Copyright 2008 Realtek Microelectronics
5  * Author: flove <flove@realtek.com> Ethan <eku@marvell.com>
6  *
7  * Copyright 2010 Arnaud Patard <arnaud.patard@rtp-net.org>
8  *
9  *
10  * Based on WM8753.c
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License version 2 as
14  * published by the Free Software Foundation.
15  *
16  */
17 
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/init.h>
21 #include <linux/delay.h>
22 #include <linux/pm.h>
23 #include <linux/i2c.h>
24 #include <linux/slab.h>
25 #include <sound/core.h>
26 #include <sound/pcm.h>
27 #include <sound/pcm_params.h>
28 #include <sound/tlv.h>
29 #include <sound/soc.h>
30 #include <sound/initval.h>
31 #include <sound/alc5623.h>
32 
33 #include "alc5623.h"
34 
35 static int caps_charge = 2000;
36 module_param(caps_charge, int, 0);
37 MODULE_PARM_DESC(caps_charge, "ALC5623 cap charge time (msecs)");
38 
39 /* codec private data */
40 struct alc5623_priv {
41 	enum snd_soc_control_type control_type;
42 	u8 id;
43 	unsigned int sysclk;
44 	u16 reg_cache[ALC5623_VENDOR_ID2+2];
45 	unsigned int add_ctrl;
46 	unsigned int jack_det_ctrl;
47 };
48 
49 static void alc5623_fill_cache(struct snd_soc_codec *codec)
50 {
51 	int i, step = codec->driver->reg_cache_step;
52 	u16 *cache = codec->reg_cache;
53 
54 	/* not really efficient ... */
55 	codec->cache_bypass = 1;
56 	for (i = 0 ; i < codec->driver->reg_cache_size ; i += step)
57 		cache[i] = snd_soc_read(codec, i);
58 	codec->cache_bypass = 0;
59 }
60 
61 static inline int alc5623_reset(struct snd_soc_codec *codec)
62 {
63 	return snd_soc_write(codec, ALC5623_RESET, 0);
64 }
65 
66 static int amp_mixer_event(struct snd_soc_dapm_widget *w,
67 	struct snd_kcontrol *kcontrol, int event)
68 {
69 	/* to power-on/off class-d amp generators/speaker */
70 	/* need to write to 'index-46h' register :        */
71 	/* so write index num (here 0x46) to reg 0x6a     */
72 	/* and then 0xffff/0 to reg 0x6c                  */
73 	snd_soc_write(w->codec, ALC5623_HID_CTRL_INDEX, 0x46);
74 
75 	switch (event) {
76 	case SND_SOC_DAPM_PRE_PMU:
77 		snd_soc_write(w->codec, ALC5623_HID_CTRL_DATA, 0xFFFF);
78 		break;
79 	case SND_SOC_DAPM_POST_PMD:
80 		snd_soc_write(w->codec, ALC5623_HID_CTRL_DATA, 0);
81 		break;
82 	}
83 
84 	return 0;
85 }
86 
87 /*
88  * ALC5623 Controls
89  */
90 
91 static const DECLARE_TLV_DB_SCALE(vol_tlv, -3450, 150, 0);
92 static const DECLARE_TLV_DB_SCALE(hp_tlv, -4650, 150, 0);
93 static const DECLARE_TLV_DB_SCALE(adc_rec_tlv, -1650, 150, 0);
94 static const unsigned int boost_tlv[] = {
95 	TLV_DB_RANGE_HEAD(3),
96 	0, 0, TLV_DB_SCALE_ITEM(0, 0, 0),
97 	1, 1, TLV_DB_SCALE_ITEM(2000, 0, 0),
98 	2, 2, TLV_DB_SCALE_ITEM(3000, 0, 0),
99 };
100 static const DECLARE_TLV_DB_SCALE(dig_tlv, 0, 600, 0);
101 
102 static const struct snd_kcontrol_new alc5621_vol_snd_controls[] = {
103 	SOC_DOUBLE_TLV("Speaker Playback Volume",
104 			ALC5623_SPK_OUT_VOL, 8, 0, 31, 1, hp_tlv),
105 	SOC_DOUBLE("Speaker Playback Switch",
106 			ALC5623_SPK_OUT_VOL, 15, 7, 1, 1),
107 	SOC_DOUBLE_TLV("Headphone Playback Volume",
108 			ALC5623_HP_OUT_VOL, 8, 0, 31, 1, hp_tlv),
109 	SOC_DOUBLE("Headphone Playback Switch",
110 			ALC5623_HP_OUT_VOL, 15, 7, 1, 1),
111 };
112 
113 static const struct snd_kcontrol_new alc5622_vol_snd_controls[] = {
114 	SOC_DOUBLE_TLV("Speaker Playback Volume",
115 			ALC5623_SPK_OUT_VOL, 8, 0, 31, 1, hp_tlv),
116 	SOC_DOUBLE("Speaker Playback Switch",
117 			ALC5623_SPK_OUT_VOL, 15, 7, 1, 1),
118 	SOC_DOUBLE_TLV("Line Playback Volume",
119 			ALC5623_HP_OUT_VOL, 8, 0, 31, 1, hp_tlv),
120 	SOC_DOUBLE("Line Playback Switch",
121 			ALC5623_HP_OUT_VOL, 15, 7, 1, 1),
122 };
123 
124 static const struct snd_kcontrol_new alc5623_vol_snd_controls[] = {
125 	SOC_DOUBLE_TLV("Line Playback Volume",
126 			ALC5623_SPK_OUT_VOL, 8, 0, 31, 1, hp_tlv),
127 	SOC_DOUBLE("Line Playback Switch",
128 			ALC5623_SPK_OUT_VOL, 15, 7, 1, 1),
129 	SOC_DOUBLE_TLV("Headphone Playback Volume",
130 			ALC5623_HP_OUT_VOL, 8, 0, 31, 1, hp_tlv),
131 	SOC_DOUBLE("Headphone Playback Switch",
132 			ALC5623_HP_OUT_VOL, 15, 7, 1, 1),
133 };
134 
135 static const struct snd_kcontrol_new alc5623_snd_controls[] = {
136 	SOC_DOUBLE_TLV("Auxout Playback Volume",
137 			ALC5623_MONO_AUX_OUT_VOL, 8, 0, 31, 1, hp_tlv),
138 	SOC_DOUBLE("Auxout Playback Switch",
139 			ALC5623_MONO_AUX_OUT_VOL, 15, 7, 1, 1),
140 	SOC_DOUBLE_TLV("PCM Playback Volume",
141 			ALC5623_STEREO_DAC_VOL, 8, 0, 31, 1, vol_tlv),
142 	SOC_DOUBLE_TLV("AuxI Capture Volume",
143 			ALC5623_AUXIN_VOL, 8, 0, 31, 1, vol_tlv),
144 	SOC_DOUBLE_TLV("LineIn Capture Volume",
145 			ALC5623_LINE_IN_VOL, 8, 0, 31, 1, vol_tlv),
146 	SOC_SINGLE_TLV("Mic1 Capture Volume",
147 			ALC5623_MIC_VOL, 8, 31, 1, vol_tlv),
148 	SOC_SINGLE_TLV("Mic2 Capture Volume",
149 			ALC5623_MIC_VOL, 0, 31, 1, vol_tlv),
150 	SOC_DOUBLE_TLV("Rec Capture Volume",
151 			ALC5623_ADC_REC_GAIN, 7, 0, 31, 0, adc_rec_tlv),
152 	SOC_SINGLE_TLV("Mic 1 Boost Volume",
153 			ALC5623_MIC_CTRL, 10, 2, 0, boost_tlv),
154 	SOC_SINGLE_TLV("Mic 2 Boost Volume",
155 			ALC5623_MIC_CTRL, 8, 2, 0, boost_tlv),
156 	SOC_SINGLE_TLV("Digital Boost Volume",
157 			ALC5623_ADD_CTRL_REG, 4, 3, 0, dig_tlv),
158 };
159 
160 /*
161  * DAPM Controls
162  */
163 static const struct snd_kcontrol_new alc5623_hp_mixer_controls[] = {
164 SOC_DAPM_SINGLE("LI2HP Playback Switch", ALC5623_LINE_IN_VOL, 15, 1, 1),
165 SOC_DAPM_SINGLE("AUXI2HP Playback Switch", ALC5623_AUXIN_VOL, 15, 1, 1),
166 SOC_DAPM_SINGLE("MIC12HP Playback Switch", ALC5623_MIC_ROUTING_CTRL, 15, 1, 1),
167 SOC_DAPM_SINGLE("MIC22HP Playback Switch", ALC5623_MIC_ROUTING_CTRL, 7, 1, 1),
168 SOC_DAPM_SINGLE("DAC2HP Playback Switch", ALC5623_STEREO_DAC_VOL, 15, 1, 1),
169 };
170 
171 static const struct snd_kcontrol_new alc5623_hpl_mixer_controls[] = {
172 SOC_DAPM_SINGLE("ADC2HP_L Playback Switch", ALC5623_ADC_REC_GAIN, 15, 1, 1),
173 };
174 
175 static const struct snd_kcontrol_new alc5623_hpr_mixer_controls[] = {
176 SOC_DAPM_SINGLE("ADC2HP_R Playback Switch", ALC5623_ADC_REC_GAIN, 14, 1, 1),
177 };
178 
179 static const struct snd_kcontrol_new alc5623_mono_mixer_controls[] = {
180 SOC_DAPM_SINGLE("ADC2MONO_L Playback Switch", ALC5623_ADC_REC_GAIN, 13, 1, 1),
181 SOC_DAPM_SINGLE("ADC2MONO_R Playback Switch", ALC5623_ADC_REC_GAIN, 12, 1, 1),
182 SOC_DAPM_SINGLE("LI2MONO Playback Switch", ALC5623_LINE_IN_VOL, 13, 1, 1),
183 SOC_DAPM_SINGLE("AUXI2MONO Playback Switch", ALC5623_AUXIN_VOL, 13, 1, 1),
184 SOC_DAPM_SINGLE("MIC12MONO Playback Switch", ALC5623_MIC_ROUTING_CTRL, 13, 1, 1),
185 SOC_DAPM_SINGLE("MIC22MONO Playback Switch", ALC5623_MIC_ROUTING_CTRL, 5, 1, 1),
186 SOC_DAPM_SINGLE("DAC2MONO Playback Switch", ALC5623_STEREO_DAC_VOL, 13, 1, 1),
187 };
188 
189 static const struct snd_kcontrol_new alc5623_speaker_mixer_controls[] = {
190 SOC_DAPM_SINGLE("LI2SPK Playback Switch", ALC5623_LINE_IN_VOL, 14, 1, 1),
191 SOC_DAPM_SINGLE("AUXI2SPK Playback Switch", ALC5623_AUXIN_VOL, 14, 1, 1),
192 SOC_DAPM_SINGLE("MIC12SPK Playback Switch", ALC5623_MIC_ROUTING_CTRL, 14, 1, 1),
193 SOC_DAPM_SINGLE("MIC22SPK Playback Switch", ALC5623_MIC_ROUTING_CTRL, 6, 1, 1),
194 SOC_DAPM_SINGLE("DAC2SPK Playback Switch", ALC5623_STEREO_DAC_VOL, 14, 1, 1),
195 };
196 
197 /* Left Record Mixer */
198 static const struct snd_kcontrol_new alc5623_captureL_mixer_controls[] = {
199 SOC_DAPM_SINGLE("Mic1 Capture Switch", ALC5623_ADC_REC_MIXER, 14, 1, 1),
200 SOC_DAPM_SINGLE("Mic2 Capture Switch", ALC5623_ADC_REC_MIXER, 13, 1, 1),
201 SOC_DAPM_SINGLE("LineInL Capture Switch", ALC5623_ADC_REC_MIXER, 12, 1, 1),
202 SOC_DAPM_SINGLE("Left AuxI Capture Switch", ALC5623_ADC_REC_MIXER, 11, 1, 1),
203 SOC_DAPM_SINGLE("HPMixerL Capture Switch", ALC5623_ADC_REC_MIXER, 10, 1, 1),
204 SOC_DAPM_SINGLE("SPKMixer Capture Switch", ALC5623_ADC_REC_MIXER, 9, 1, 1),
205 SOC_DAPM_SINGLE("MonoMixer Capture Switch", ALC5623_ADC_REC_MIXER, 8, 1, 1),
206 };
207 
208 /* Right Record Mixer */
209 static const struct snd_kcontrol_new alc5623_captureR_mixer_controls[] = {
210 SOC_DAPM_SINGLE("Mic1 Capture Switch", ALC5623_ADC_REC_MIXER, 6, 1, 1),
211 SOC_DAPM_SINGLE("Mic2 Capture Switch", ALC5623_ADC_REC_MIXER, 5, 1, 1),
212 SOC_DAPM_SINGLE("LineInR Capture Switch", ALC5623_ADC_REC_MIXER, 4, 1, 1),
213 SOC_DAPM_SINGLE("Right AuxI Capture Switch", ALC5623_ADC_REC_MIXER, 3, 1, 1),
214 SOC_DAPM_SINGLE("HPMixerR Capture Switch", ALC5623_ADC_REC_MIXER, 2, 1, 1),
215 SOC_DAPM_SINGLE("SPKMixer Capture Switch", ALC5623_ADC_REC_MIXER, 1, 1, 1),
216 SOC_DAPM_SINGLE("MonoMixer Capture Switch", ALC5623_ADC_REC_MIXER, 0, 1, 1),
217 };
218 
219 static const char *alc5623_spk_n_sour_sel[] = {
220 		"RN/-R", "RP/+R", "LN/-R", "Vmid" };
221 static const char *alc5623_hpl_out_input_sel[] = {
222 		"Vmid", "HP Left Mix"};
223 static const char *alc5623_hpr_out_input_sel[] = {
224 		"Vmid", "HP Right Mix"};
225 static const char *alc5623_spkout_input_sel[] = {
226 		"Vmid", "HPOut Mix", "Speaker Mix", "Mono Mix"};
227 static const char *alc5623_aux_out_input_sel[] = {
228 		"Vmid", "HPOut Mix", "Speaker Mix", "Mono Mix"};
229 
230 /* auxout output mux */
231 static const struct soc_enum alc5623_aux_out_input_enum =
232 SOC_ENUM_SINGLE(ALC5623_OUTPUT_MIXER_CTRL, 6, 4, alc5623_aux_out_input_sel);
233 static const struct snd_kcontrol_new alc5623_auxout_mux_controls =
234 SOC_DAPM_ENUM("Route", alc5623_aux_out_input_enum);
235 
236 /* speaker output mux */
237 static const struct soc_enum alc5623_spkout_input_enum =
238 SOC_ENUM_SINGLE(ALC5623_OUTPUT_MIXER_CTRL, 10, 4, alc5623_spkout_input_sel);
239 static const struct snd_kcontrol_new alc5623_spkout_mux_controls =
240 SOC_DAPM_ENUM("Route", alc5623_spkout_input_enum);
241 
242 /* headphone left output mux */
243 static const struct soc_enum alc5623_hpl_out_input_enum =
244 SOC_ENUM_SINGLE(ALC5623_OUTPUT_MIXER_CTRL, 9, 2, alc5623_hpl_out_input_sel);
245 static const struct snd_kcontrol_new alc5623_hpl_out_mux_controls =
246 SOC_DAPM_ENUM("Route", alc5623_hpl_out_input_enum);
247 
248 /* headphone right output mux */
249 static const struct soc_enum alc5623_hpr_out_input_enum =
250 SOC_ENUM_SINGLE(ALC5623_OUTPUT_MIXER_CTRL, 8, 2, alc5623_hpr_out_input_sel);
251 static const struct snd_kcontrol_new alc5623_hpr_out_mux_controls =
252 SOC_DAPM_ENUM("Route", alc5623_hpr_out_input_enum);
253 
254 /* speaker output N select */
255 static const struct soc_enum alc5623_spk_n_sour_enum =
256 SOC_ENUM_SINGLE(ALC5623_OUTPUT_MIXER_CTRL, 14, 4, alc5623_spk_n_sour_sel);
257 static const struct snd_kcontrol_new alc5623_spkoutn_mux_controls =
258 SOC_DAPM_ENUM("Route", alc5623_spk_n_sour_enum);
259 
260 static const struct snd_soc_dapm_widget alc5623_dapm_widgets[] = {
261 /* Muxes */
262 SND_SOC_DAPM_MUX("AuxOut Mux", SND_SOC_NOPM, 0, 0,
263 	&alc5623_auxout_mux_controls),
264 SND_SOC_DAPM_MUX("SpeakerOut Mux", SND_SOC_NOPM, 0, 0,
265 	&alc5623_spkout_mux_controls),
266 SND_SOC_DAPM_MUX("Left Headphone Mux", SND_SOC_NOPM, 0, 0,
267 	&alc5623_hpl_out_mux_controls),
268 SND_SOC_DAPM_MUX("Right Headphone Mux", SND_SOC_NOPM, 0, 0,
269 	&alc5623_hpr_out_mux_controls),
270 SND_SOC_DAPM_MUX("SpeakerOut N Mux", SND_SOC_NOPM, 0, 0,
271 	&alc5623_spkoutn_mux_controls),
272 
273 /* output mixers */
274 SND_SOC_DAPM_MIXER("HP Mix", SND_SOC_NOPM, 0, 0,
275 	&alc5623_hp_mixer_controls[0],
276 	ARRAY_SIZE(alc5623_hp_mixer_controls)),
277 SND_SOC_DAPM_MIXER("HPR Mix", ALC5623_PWR_MANAG_ADD2, 4, 0,
278 	&alc5623_hpr_mixer_controls[0],
279 	ARRAY_SIZE(alc5623_hpr_mixer_controls)),
280 SND_SOC_DAPM_MIXER("HPL Mix", ALC5623_PWR_MANAG_ADD2, 5, 0,
281 	&alc5623_hpl_mixer_controls[0],
282 	ARRAY_SIZE(alc5623_hpl_mixer_controls)),
283 SND_SOC_DAPM_MIXER("HPOut Mix", SND_SOC_NOPM, 0, 0, NULL, 0),
284 SND_SOC_DAPM_MIXER("Mono Mix", ALC5623_PWR_MANAG_ADD2, 2, 0,
285 	&alc5623_mono_mixer_controls[0],
286 	ARRAY_SIZE(alc5623_mono_mixer_controls)),
287 SND_SOC_DAPM_MIXER("Speaker Mix", ALC5623_PWR_MANAG_ADD2, 3, 0,
288 	&alc5623_speaker_mixer_controls[0],
289 	ARRAY_SIZE(alc5623_speaker_mixer_controls)),
290 
291 /* input mixers */
292 SND_SOC_DAPM_MIXER("Left Capture Mix", ALC5623_PWR_MANAG_ADD2, 1, 0,
293 	&alc5623_captureL_mixer_controls[0],
294 	ARRAY_SIZE(alc5623_captureL_mixer_controls)),
295 SND_SOC_DAPM_MIXER("Right Capture Mix", ALC5623_PWR_MANAG_ADD2, 0, 0,
296 	&alc5623_captureR_mixer_controls[0],
297 	ARRAY_SIZE(alc5623_captureR_mixer_controls)),
298 
299 SND_SOC_DAPM_DAC("Left DAC", "Left HiFi Playback",
300 	ALC5623_PWR_MANAG_ADD2, 9, 0),
301 SND_SOC_DAPM_DAC("Right DAC", "Right HiFi Playback",
302 	ALC5623_PWR_MANAG_ADD2, 8, 0),
303 SND_SOC_DAPM_MIXER("I2S Mix", ALC5623_PWR_MANAG_ADD1, 15, 0, NULL, 0),
304 SND_SOC_DAPM_MIXER("AuxI Mix", SND_SOC_NOPM, 0, 0, NULL, 0),
305 SND_SOC_DAPM_MIXER("Line Mix", SND_SOC_NOPM, 0, 0, NULL, 0),
306 SND_SOC_DAPM_ADC("Left ADC", "Left HiFi Capture",
307 	ALC5623_PWR_MANAG_ADD2, 7, 0),
308 SND_SOC_DAPM_ADC("Right ADC", "Right HiFi Capture",
309 	ALC5623_PWR_MANAG_ADD2, 6, 0),
310 SND_SOC_DAPM_PGA("Left Headphone", ALC5623_PWR_MANAG_ADD3, 10, 0, NULL, 0),
311 SND_SOC_DAPM_PGA("Right Headphone", ALC5623_PWR_MANAG_ADD3, 9, 0, NULL, 0),
312 SND_SOC_DAPM_PGA("SpeakerOut", ALC5623_PWR_MANAG_ADD3, 12, 0, NULL, 0),
313 SND_SOC_DAPM_PGA("Left AuxOut", ALC5623_PWR_MANAG_ADD3, 14, 0, NULL, 0),
314 SND_SOC_DAPM_PGA("Right AuxOut", ALC5623_PWR_MANAG_ADD3, 13, 0, NULL, 0),
315 SND_SOC_DAPM_PGA("Left LineIn", ALC5623_PWR_MANAG_ADD3, 7, 0, NULL, 0),
316 SND_SOC_DAPM_PGA("Right LineIn", ALC5623_PWR_MANAG_ADD3, 6, 0, NULL, 0),
317 SND_SOC_DAPM_PGA("Left AuxI", ALC5623_PWR_MANAG_ADD3, 5, 0, NULL, 0),
318 SND_SOC_DAPM_PGA("Right AuxI", ALC5623_PWR_MANAG_ADD3, 4, 0, NULL, 0),
319 SND_SOC_DAPM_PGA("MIC1 PGA", ALC5623_PWR_MANAG_ADD3, 3, 0, NULL, 0),
320 SND_SOC_DAPM_PGA("MIC2 PGA", ALC5623_PWR_MANAG_ADD3, 2, 0, NULL, 0),
321 SND_SOC_DAPM_PGA("MIC1 Pre Amp", ALC5623_PWR_MANAG_ADD3, 1, 0, NULL, 0),
322 SND_SOC_DAPM_PGA("MIC2 Pre Amp", ALC5623_PWR_MANAG_ADD3, 0, 0, NULL, 0),
323 SND_SOC_DAPM_MICBIAS("Mic Bias1", ALC5623_PWR_MANAG_ADD1, 11, 0),
324 
325 SND_SOC_DAPM_OUTPUT("AUXOUTL"),
326 SND_SOC_DAPM_OUTPUT("AUXOUTR"),
327 SND_SOC_DAPM_OUTPUT("HPL"),
328 SND_SOC_DAPM_OUTPUT("HPR"),
329 SND_SOC_DAPM_OUTPUT("SPKOUT"),
330 SND_SOC_DAPM_OUTPUT("SPKOUTN"),
331 SND_SOC_DAPM_INPUT("LINEINL"),
332 SND_SOC_DAPM_INPUT("LINEINR"),
333 SND_SOC_DAPM_INPUT("AUXINL"),
334 SND_SOC_DAPM_INPUT("AUXINR"),
335 SND_SOC_DAPM_INPUT("MIC1"),
336 SND_SOC_DAPM_INPUT("MIC2"),
337 SND_SOC_DAPM_VMID("Vmid"),
338 };
339 
340 static const char *alc5623_amp_names[] = {"AB Amp", "D Amp"};
341 static const struct soc_enum alc5623_amp_enum =
342 	SOC_ENUM_SINGLE(ALC5623_OUTPUT_MIXER_CTRL, 13, 2, alc5623_amp_names);
343 static const struct snd_kcontrol_new alc5623_amp_mux_controls =
344 	SOC_DAPM_ENUM("Route", alc5623_amp_enum);
345 
346 static const struct snd_soc_dapm_widget alc5623_dapm_amp_widgets[] = {
347 SND_SOC_DAPM_PGA_E("D Amp", ALC5623_PWR_MANAG_ADD2, 14, 0, NULL, 0,
348 	amp_mixer_event, SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
349 SND_SOC_DAPM_PGA("AB Amp", ALC5623_PWR_MANAG_ADD2, 15, 0, NULL, 0),
350 SND_SOC_DAPM_MUX("AB-D Amp Mux", SND_SOC_NOPM, 0, 0,
351 	&alc5623_amp_mux_controls),
352 };
353 
354 static const struct snd_soc_dapm_route intercon[] = {
355 	/* virtual mixer - mixes left & right channels */
356 	{"I2S Mix", NULL,				"Left DAC"},
357 	{"I2S Mix", NULL,				"Right DAC"},
358 	{"Line Mix", NULL,				"Right LineIn"},
359 	{"Line Mix", NULL,				"Left LineIn"},
360 	{"AuxI Mix", NULL,				"Left AuxI"},
361 	{"AuxI Mix", NULL,				"Right AuxI"},
362 	{"AUXOUTL", NULL,				"Left AuxOut"},
363 	{"AUXOUTR", NULL,				"Right AuxOut"},
364 
365 	/* HP mixer */
366 	{"HPL Mix", "ADC2HP_L Playback Switch",		"Left Capture Mix"},
367 	{"HPL Mix", NULL,				"HP Mix"},
368 	{"HPR Mix", "ADC2HP_R Playback Switch",		"Right Capture Mix"},
369 	{"HPR Mix", NULL,				"HP Mix"},
370 	{"HP Mix", "LI2HP Playback Switch",		"Line Mix"},
371 	{"HP Mix", "AUXI2HP Playback Switch",		"AuxI Mix"},
372 	{"HP Mix", "MIC12HP Playback Switch",		"MIC1 PGA"},
373 	{"HP Mix", "MIC22HP Playback Switch",		"MIC2 PGA"},
374 	{"HP Mix", "DAC2HP Playback Switch",		"I2S Mix"},
375 
376 	/* speaker mixer */
377 	{"Speaker Mix", "LI2SPK Playback Switch",	"Line Mix"},
378 	{"Speaker Mix", "AUXI2SPK Playback Switch",	"AuxI Mix"},
379 	{"Speaker Mix", "MIC12SPK Playback Switch",	"MIC1 PGA"},
380 	{"Speaker Mix", "MIC22SPK Playback Switch",	"MIC2 PGA"},
381 	{"Speaker Mix", "DAC2SPK Playback Switch",	"I2S Mix"},
382 
383 	/* mono mixer */
384 	{"Mono Mix", "ADC2MONO_L Playback Switch",	"Left Capture Mix"},
385 	{"Mono Mix", "ADC2MONO_R Playback Switch",	"Right Capture Mix"},
386 	{"Mono Mix", "LI2MONO Playback Switch",		"Line Mix"},
387 	{"Mono Mix", "AUXI2MONO Playback Switch",	"AuxI Mix"},
388 	{"Mono Mix", "MIC12MONO Playback Switch",	"MIC1 PGA"},
389 	{"Mono Mix", "MIC22MONO Playback Switch",	"MIC2 PGA"},
390 	{"Mono Mix", "DAC2MONO Playback Switch",	"I2S Mix"},
391 
392 	/* Left record mixer */
393 	{"Left Capture Mix", "LineInL Capture Switch",	"LINEINL"},
394 	{"Left Capture Mix", "Left AuxI Capture Switch", "AUXINL"},
395 	{"Left Capture Mix", "Mic1 Capture Switch",	"MIC1 Pre Amp"},
396 	{"Left Capture Mix", "Mic2 Capture Switch",	"MIC2 Pre Amp"},
397 	{"Left Capture Mix", "HPMixerL Capture Switch", "HPL Mix"},
398 	{"Left Capture Mix", "SPKMixer Capture Switch", "Speaker Mix"},
399 	{"Left Capture Mix", "MonoMixer Capture Switch", "Mono Mix"},
400 
401 	/*Right record mixer */
402 	{"Right Capture Mix", "LineInR Capture Switch",	"LINEINR"},
403 	{"Right Capture Mix", "Right AuxI Capture Switch",	"AUXINR"},
404 	{"Right Capture Mix", "Mic1 Capture Switch",	"MIC1 Pre Amp"},
405 	{"Right Capture Mix", "Mic2 Capture Switch",	"MIC2 Pre Amp"},
406 	{"Right Capture Mix", "HPMixerR Capture Switch", "HPR Mix"},
407 	{"Right Capture Mix", "SPKMixer Capture Switch", "Speaker Mix"},
408 	{"Right Capture Mix", "MonoMixer Capture Switch", "Mono Mix"},
409 
410 	/* headphone left mux */
411 	{"Left Headphone Mux", "HP Left Mix",		"HPL Mix"},
412 	{"Left Headphone Mux", "Vmid",			"Vmid"},
413 
414 	/* headphone right mux */
415 	{"Right Headphone Mux", "HP Right Mix",		"HPR Mix"},
416 	{"Right Headphone Mux", "Vmid",			"Vmid"},
417 
418 	/* speaker out mux */
419 	{"SpeakerOut Mux", "Vmid",			"Vmid"},
420 	{"SpeakerOut Mux", "HPOut Mix",			"HPOut Mix"},
421 	{"SpeakerOut Mux", "Speaker Mix",		"Speaker Mix"},
422 	{"SpeakerOut Mux", "Mono Mix",			"Mono Mix"},
423 
424 	/* Mono/Aux Out mux */
425 	{"AuxOut Mux", "Vmid",				"Vmid"},
426 	{"AuxOut Mux", "HPOut Mix",			"HPOut Mix"},
427 	{"AuxOut Mux", "Speaker Mix",			"Speaker Mix"},
428 	{"AuxOut Mux", "Mono Mix",			"Mono Mix"},
429 
430 	/* output pga */
431 	{"HPL", NULL,					"Left Headphone"},
432 	{"Left Headphone", NULL,			"Left Headphone Mux"},
433 	{"HPR", NULL,					"Right Headphone"},
434 	{"Right Headphone", NULL,			"Right Headphone Mux"},
435 	{"Left AuxOut", NULL,				"AuxOut Mux"},
436 	{"Right AuxOut", NULL,				"AuxOut Mux"},
437 
438 	/* input pga */
439 	{"Left LineIn", NULL,				"LINEINL"},
440 	{"Right LineIn", NULL,				"LINEINR"},
441 	{"Left AuxI", NULL,				"AUXINL"},
442 	{"Right AuxI", NULL,				"AUXINR"},
443 	{"MIC1 Pre Amp", NULL,				"MIC1"},
444 	{"MIC2 Pre Amp", NULL,				"MIC2"},
445 	{"MIC1 PGA", NULL,				"MIC1 Pre Amp"},
446 	{"MIC2 PGA", NULL,				"MIC2 Pre Amp"},
447 
448 	/* left ADC */
449 	{"Left ADC", NULL,				"Left Capture Mix"},
450 
451 	/* right ADC */
452 	{"Right ADC", NULL,				"Right Capture Mix"},
453 
454 	{"SpeakerOut N Mux", "RN/-R",			"SpeakerOut"},
455 	{"SpeakerOut N Mux", "RP/+R",			"SpeakerOut"},
456 	{"SpeakerOut N Mux", "LN/-R",			"SpeakerOut"},
457 	{"SpeakerOut N Mux", "Vmid",			"Vmid"},
458 
459 	{"SPKOUT", NULL,				"SpeakerOut"},
460 	{"SPKOUTN", NULL,				"SpeakerOut N Mux"},
461 };
462 
463 static const struct snd_soc_dapm_route intercon_spk[] = {
464 	{"SpeakerOut", NULL,				"SpeakerOut Mux"},
465 };
466 
467 static const struct snd_soc_dapm_route intercon_amp_spk[] = {
468 	{"AB Amp", NULL,				"SpeakerOut Mux"},
469 	{"D Amp", NULL,					"SpeakerOut Mux"},
470 	{"AB-D Amp Mux", "AB Amp",			"AB Amp"},
471 	{"AB-D Amp Mux", "D Amp",			"D Amp"},
472 	{"SpeakerOut", NULL,				"AB-D Amp Mux"},
473 };
474 
475 /* PLL divisors */
476 struct _pll_div {
477 	u32 pll_in;
478 	u32 pll_out;
479 	u16 regvalue;
480 };
481 
482 /* Note : pll code from original alc5623 driver. Not sure of how good it is */
483 /* useful only for master mode */
484 static const struct _pll_div codec_master_pll_div[] = {
485 
486 	{  2048000,  8192000,	0x0ea0},
487 	{  3686400,  8192000,	0x4e27},
488 	{ 12000000,  8192000,	0x456b},
489 	{ 13000000,  8192000,	0x495f},
490 	{ 13100000,  8192000,	0x0320},
491 	{  2048000,  11289600,	0xf637},
492 	{  3686400,  11289600,	0x2f22},
493 	{ 12000000,  11289600,	0x3e2f},
494 	{ 13000000,  11289600,	0x4d5b},
495 	{ 13100000,  11289600,	0x363b},
496 	{  2048000,  16384000,	0x1ea0},
497 	{  3686400,  16384000,	0x9e27},
498 	{ 12000000,  16384000,	0x452b},
499 	{ 13000000,  16384000,	0x542f},
500 	{ 13100000,  16384000,	0x03a0},
501 	{  2048000,  16934400,	0xe625},
502 	{  3686400,  16934400,	0x9126},
503 	{ 12000000,  16934400,	0x4d2c},
504 	{ 13000000,  16934400,	0x742f},
505 	{ 13100000,  16934400,	0x3c27},
506 	{  2048000,  22579200,	0x2aa0},
507 	{  3686400,  22579200,	0x2f20},
508 	{ 12000000,  22579200,	0x7e2f},
509 	{ 13000000,  22579200,	0x742f},
510 	{ 13100000,  22579200,	0x3c27},
511 	{  2048000,  24576000,	0x2ea0},
512 	{  3686400,  24576000,	0xee27},
513 	{ 12000000,  24576000,	0x2915},
514 	{ 13000000,  24576000,	0x772e},
515 	{ 13100000,  24576000,	0x0d20},
516 };
517 
518 static const struct _pll_div codec_slave_pll_div[] = {
519 
520 	{  1024000,  16384000,  0x3ea0},
521 	{  1411200,  22579200,	0x3ea0},
522 	{  1536000,  24576000,	0x3ea0},
523 	{  2048000,  16384000,  0x1ea0},
524 	{  2822400,  22579200,	0x1ea0},
525 	{  3072000,  24576000,	0x1ea0},
526 
527 };
528 
529 static int alc5623_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id,
530 		int source, unsigned int freq_in, unsigned int freq_out)
531 {
532 	int i;
533 	struct snd_soc_codec *codec = codec_dai->codec;
534 	int gbl_clk = 0, pll_div = 0;
535 	u16 reg;
536 
537 	if (pll_id < ALC5623_PLL_FR_MCLK || pll_id > ALC5623_PLL_FR_BCK)
538 		return -ENODEV;
539 
540 	/* Disable PLL power */
541 	snd_soc_update_bits(codec, ALC5623_PWR_MANAG_ADD2,
542 				ALC5623_PWR_ADD2_PLL,
543 				0);
544 
545 	/* pll is not used in slave mode */
546 	reg = snd_soc_read(codec, ALC5623_DAI_CONTROL);
547 	if (reg & ALC5623_DAI_SDP_SLAVE_MODE)
548 		return 0;
549 
550 	if (!freq_in || !freq_out)
551 		return 0;
552 
553 	switch (pll_id) {
554 	case ALC5623_PLL_FR_MCLK:
555 		for (i = 0; i < ARRAY_SIZE(codec_master_pll_div); i++) {
556 			if (codec_master_pll_div[i].pll_in == freq_in
557 			   && codec_master_pll_div[i].pll_out == freq_out) {
558 				/* PLL source from MCLK */
559 				pll_div  = codec_master_pll_div[i].regvalue;
560 				break;
561 			}
562 		}
563 		break;
564 	case ALC5623_PLL_FR_BCK:
565 		for (i = 0; i < ARRAY_SIZE(codec_slave_pll_div); i++) {
566 			if (codec_slave_pll_div[i].pll_in == freq_in
567 			   && codec_slave_pll_div[i].pll_out == freq_out) {
568 				/* PLL source from Bitclk */
569 				gbl_clk = ALC5623_GBL_CLK_PLL_SOUR_SEL_BITCLK;
570 				pll_div = codec_slave_pll_div[i].regvalue;
571 				break;
572 			}
573 		}
574 		break;
575 	default:
576 		return -EINVAL;
577 	}
578 
579 	if (!pll_div)
580 		return -EINVAL;
581 
582 	snd_soc_write(codec, ALC5623_GLOBAL_CLK_CTRL_REG, gbl_clk);
583 	snd_soc_write(codec, ALC5623_PLL_CTRL, pll_div);
584 	snd_soc_update_bits(codec, ALC5623_PWR_MANAG_ADD2,
585 				ALC5623_PWR_ADD2_PLL,
586 				ALC5623_PWR_ADD2_PLL);
587 	gbl_clk |= ALC5623_GBL_CLK_SYS_SOUR_SEL_PLL;
588 	snd_soc_write(codec, ALC5623_GLOBAL_CLK_CTRL_REG, gbl_clk);
589 
590 	return 0;
591 }
592 
593 struct _coeff_div {
594 	u16 fs;
595 	u16 regvalue;
596 };
597 
598 /* codec hifi mclk (after PLL) clock divider coefficients */
599 /* values inspired from column BCLK=32Fs of Appendix A table */
600 static const struct _coeff_div coeff_div[] = {
601 	{256*8, 0x3a69},
602 	{384*8, 0x3c6b},
603 	{256*4, 0x2a69},
604 	{384*4, 0x2c6b},
605 	{256*2, 0x1a69},
606 	{384*2, 0x1c6b},
607 	{256*1, 0x0a69},
608 	{384*1, 0x0c6b},
609 };
610 
611 static int get_coeff(struct snd_soc_codec *codec, int rate)
612 {
613 	struct alc5623_priv *alc5623 = snd_soc_codec_get_drvdata(codec);
614 	int i;
615 
616 	for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
617 		if (coeff_div[i].fs * rate == alc5623->sysclk)
618 			return i;
619 	}
620 	return -EINVAL;
621 }
622 
623 /*
624  * Clock after PLL and dividers
625  */
626 static int alc5623_set_dai_sysclk(struct snd_soc_dai *codec_dai,
627 		int clk_id, unsigned int freq, int dir)
628 {
629 	struct snd_soc_codec *codec = codec_dai->codec;
630 	struct alc5623_priv *alc5623 = snd_soc_codec_get_drvdata(codec);
631 
632 	switch (freq) {
633 	case  8192000:
634 	case 11289600:
635 	case 12288000:
636 	case 16384000:
637 	case 16934400:
638 	case 18432000:
639 	case 22579200:
640 	case 24576000:
641 		alc5623->sysclk = freq;
642 		return 0;
643 	}
644 	return -EINVAL;
645 }
646 
647 static int alc5623_set_dai_fmt(struct snd_soc_dai *codec_dai,
648 		unsigned int fmt)
649 {
650 	struct snd_soc_codec *codec = codec_dai->codec;
651 	u16 iface = 0;
652 
653 	/* set master/slave audio interface */
654 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
655 	case SND_SOC_DAIFMT_CBM_CFM:
656 		iface = ALC5623_DAI_SDP_MASTER_MODE;
657 		break;
658 	case SND_SOC_DAIFMT_CBS_CFS:
659 		iface = ALC5623_DAI_SDP_SLAVE_MODE;
660 		break;
661 	default:
662 		return -EINVAL;
663 	}
664 
665 	/* interface format */
666 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
667 	case SND_SOC_DAIFMT_I2S:
668 		iface |= ALC5623_DAI_I2S_DF_I2S;
669 		break;
670 	case SND_SOC_DAIFMT_RIGHT_J:
671 		iface |= ALC5623_DAI_I2S_DF_RIGHT;
672 		break;
673 	case SND_SOC_DAIFMT_LEFT_J:
674 		iface |= ALC5623_DAI_I2S_DF_LEFT;
675 		break;
676 	case SND_SOC_DAIFMT_DSP_A:
677 		iface |= ALC5623_DAI_I2S_DF_PCM;
678 		break;
679 	case SND_SOC_DAIFMT_DSP_B:
680 		iface |= ALC5623_DAI_I2S_DF_PCM | ALC5623_DAI_I2S_PCM_MODE;
681 		break;
682 	default:
683 		return -EINVAL;
684 	}
685 
686 	/* clock inversion */
687 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
688 	case SND_SOC_DAIFMT_NB_NF:
689 		break;
690 	case SND_SOC_DAIFMT_IB_IF:
691 		iface |= ALC5623_DAI_MAIN_I2S_BCLK_POL_CTRL;
692 		break;
693 	case SND_SOC_DAIFMT_IB_NF:
694 		iface |= ALC5623_DAI_MAIN_I2S_BCLK_POL_CTRL;
695 		break;
696 	case SND_SOC_DAIFMT_NB_IF:
697 		break;
698 	default:
699 		return -EINVAL;
700 	}
701 
702 	return snd_soc_write(codec, ALC5623_DAI_CONTROL, iface);
703 }
704 
705 static int alc5623_pcm_hw_params(struct snd_pcm_substream *substream,
706 		struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
707 {
708 	struct snd_soc_codec *codec = dai->codec;
709 	struct alc5623_priv *alc5623 = snd_soc_codec_get_drvdata(codec);
710 	int coeff, rate;
711 	u16 iface;
712 
713 	iface = snd_soc_read(codec, ALC5623_DAI_CONTROL);
714 	iface &= ~ALC5623_DAI_I2S_DL_MASK;
715 
716 	/* bit size */
717 	switch (params_format(params)) {
718 	case SNDRV_PCM_FORMAT_S16_LE:
719 		iface |= ALC5623_DAI_I2S_DL_16;
720 		break;
721 	case SNDRV_PCM_FORMAT_S20_3LE:
722 		iface |= ALC5623_DAI_I2S_DL_20;
723 		break;
724 	case SNDRV_PCM_FORMAT_S24_LE:
725 		iface |= ALC5623_DAI_I2S_DL_24;
726 		break;
727 	case SNDRV_PCM_FORMAT_S32_LE:
728 		iface |= ALC5623_DAI_I2S_DL_32;
729 		break;
730 	default:
731 		return -EINVAL;
732 	}
733 
734 	/* set iface & srate */
735 	snd_soc_write(codec, ALC5623_DAI_CONTROL, iface);
736 	rate = params_rate(params);
737 	coeff = get_coeff(codec, rate);
738 	if (coeff < 0)
739 		return -EINVAL;
740 
741 	coeff = coeff_div[coeff].regvalue;
742 	dev_dbg(codec->dev, "%s: sysclk=%d,rate=%d,coeff=0x%04x\n",
743 		__func__, alc5623->sysclk, rate, coeff);
744 	snd_soc_write(codec, ALC5623_STEREO_AD_DA_CLK_CTRL, coeff);
745 
746 	return 0;
747 }
748 
749 static int alc5623_mute(struct snd_soc_dai *dai, int mute)
750 {
751 	struct snd_soc_codec *codec = dai->codec;
752 	u16 hp_mute = ALC5623_MISC_M_DAC_L_INPUT | ALC5623_MISC_M_DAC_R_INPUT;
753 	u16 mute_reg = snd_soc_read(codec, ALC5623_MISC_CTRL) & ~hp_mute;
754 
755 	if (mute)
756 		mute_reg |= hp_mute;
757 
758 	return snd_soc_write(codec, ALC5623_MISC_CTRL, mute_reg);
759 }
760 
761 #define ALC5623_ADD2_POWER_EN (ALC5623_PWR_ADD2_VREF \
762 	| ALC5623_PWR_ADD2_DAC_REF_CIR)
763 
764 #define ALC5623_ADD3_POWER_EN (ALC5623_PWR_ADD3_MAIN_BIAS \
765 	| ALC5623_PWR_ADD3_MIC1_BOOST_AD)
766 
767 #define ALC5623_ADD1_POWER_EN \
768 	(ALC5623_PWR_ADD1_SHORT_CURR_DET_EN | ALC5623_PWR_ADD1_SOFTGEN_EN \
769 	| ALC5623_PWR_ADD1_DEPOP_BUF_HP | ALC5623_PWR_ADD1_HP_OUT_AMP \
770 	| ALC5623_PWR_ADD1_HP_OUT_ENH_AMP)
771 
772 #define ALC5623_ADD1_POWER_EN_5622 \
773 	(ALC5623_PWR_ADD1_SHORT_CURR_DET_EN \
774 	| ALC5623_PWR_ADD1_HP_OUT_AMP)
775 
776 static void enable_power_depop(struct snd_soc_codec *codec)
777 {
778 	struct alc5623_priv *alc5623 = snd_soc_codec_get_drvdata(codec);
779 
780 	snd_soc_update_bits(codec, ALC5623_PWR_MANAG_ADD1,
781 				ALC5623_PWR_ADD1_SOFTGEN_EN,
782 				ALC5623_PWR_ADD1_SOFTGEN_EN);
783 
784 	snd_soc_write(codec, ALC5623_PWR_MANAG_ADD3, ALC5623_ADD3_POWER_EN);
785 
786 	snd_soc_update_bits(codec, ALC5623_MISC_CTRL,
787 				ALC5623_MISC_HP_DEPOP_MODE2_EN,
788 				ALC5623_MISC_HP_DEPOP_MODE2_EN);
789 
790 	msleep(500);
791 
792 	snd_soc_write(codec, ALC5623_PWR_MANAG_ADD2, ALC5623_ADD2_POWER_EN);
793 
794 	/* avoid writing '1' into 5622 reserved bits */
795 	if (alc5623->id == 0x22)
796 		snd_soc_write(codec, ALC5623_PWR_MANAG_ADD1,
797 			ALC5623_ADD1_POWER_EN_5622);
798 	else
799 		snd_soc_write(codec, ALC5623_PWR_MANAG_ADD1,
800 			ALC5623_ADD1_POWER_EN);
801 
802 	/* disable HP Depop2 */
803 	snd_soc_update_bits(codec, ALC5623_MISC_CTRL,
804 				ALC5623_MISC_HP_DEPOP_MODE2_EN,
805 				0);
806 
807 }
808 
809 static int alc5623_set_bias_level(struct snd_soc_codec *codec,
810 				      enum snd_soc_bias_level level)
811 {
812 	switch (level) {
813 	case SND_SOC_BIAS_ON:
814 		enable_power_depop(codec);
815 		break;
816 	case SND_SOC_BIAS_PREPARE:
817 		break;
818 	case SND_SOC_BIAS_STANDBY:
819 		/* everything off except vref/vmid, */
820 		snd_soc_write(codec, ALC5623_PWR_MANAG_ADD2,
821 				ALC5623_PWR_ADD2_VREF);
822 		snd_soc_write(codec, ALC5623_PWR_MANAG_ADD3,
823 				ALC5623_PWR_ADD3_MAIN_BIAS);
824 		break;
825 	case SND_SOC_BIAS_OFF:
826 		/* everything off, dac mute, inactive */
827 		snd_soc_write(codec, ALC5623_PWR_MANAG_ADD2, 0);
828 		snd_soc_write(codec, ALC5623_PWR_MANAG_ADD3, 0);
829 		snd_soc_write(codec, ALC5623_PWR_MANAG_ADD1, 0);
830 		break;
831 	}
832 	codec->dapm.bias_level = level;
833 	return 0;
834 }
835 
836 #define ALC5623_FORMATS	(SNDRV_PCM_FMTBIT_S16_LE \
837 			| SNDRV_PCM_FMTBIT_S24_LE \
838 			| SNDRV_PCM_FMTBIT_S32_LE)
839 
840 static const struct snd_soc_dai_ops alc5623_dai_ops = {
841 		.hw_params = alc5623_pcm_hw_params,
842 		.digital_mute = alc5623_mute,
843 		.set_fmt = alc5623_set_dai_fmt,
844 		.set_sysclk = alc5623_set_dai_sysclk,
845 		.set_pll = alc5623_set_dai_pll,
846 };
847 
848 static struct snd_soc_dai_driver alc5623_dai = {
849 	.name = "alc5623-hifi",
850 	.playback = {
851 		.stream_name = "Playback",
852 		.channels_min = 1,
853 		.channels_max = 2,
854 		.rate_min =	8000,
855 		.rate_max =	48000,
856 		.rates = SNDRV_PCM_RATE_8000_48000,
857 		.formats = ALC5623_FORMATS,},
858 	.capture = {
859 		.stream_name = "Capture",
860 		.channels_min = 1,
861 		.channels_max = 2,
862 		.rate_min =	8000,
863 		.rate_max =	48000,
864 		.rates = SNDRV_PCM_RATE_8000_48000,
865 		.formats = ALC5623_FORMATS,},
866 
867 	.ops = &alc5623_dai_ops,
868 };
869 
870 static int alc5623_suspend(struct snd_soc_codec *codec)
871 {
872 	alc5623_set_bias_level(codec, SND_SOC_BIAS_OFF);
873 	return 0;
874 }
875 
876 static int alc5623_resume(struct snd_soc_codec *codec)
877 {
878 	int i, step = codec->driver->reg_cache_step;
879 	u16 *cache = codec->reg_cache;
880 
881 	/* Sync reg_cache with the hardware */
882 	for (i = 2 ; i < codec->driver->reg_cache_size ; i += step)
883 		snd_soc_write(codec, i, cache[i]);
884 
885 	alc5623_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
886 
887 	/* charge alc5623 caps */
888 	if (codec->dapm.suspend_bias_level == SND_SOC_BIAS_ON) {
889 		alc5623_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
890 		codec->dapm.bias_level = SND_SOC_BIAS_ON;
891 		alc5623_set_bias_level(codec, codec->dapm.bias_level);
892 	}
893 
894 	return 0;
895 }
896 
897 static int alc5623_probe(struct snd_soc_codec *codec)
898 {
899 	struct alc5623_priv *alc5623 = snd_soc_codec_get_drvdata(codec);
900 	struct snd_soc_dapm_context *dapm = &codec->dapm;
901 	int ret;
902 
903 	ret = snd_soc_codec_set_cache_io(codec, 8, 16, alc5623->control_type);
904 	if (ret < 0) {
905 		dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
906 		return ret;
907 	}
908 
909 	alc5623_reset(codec);
910 	alc5623_fill_cache(codec);
911 
912 	/* power on device */
913 	alc5623_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
914 
915 	if (alc5623->add_ctrl) {
916 		snd_soc_write(codec, ALC5623_ADD_CTRL_REG,
917 				alc5623->add_ctrl);
918 	}
919 
920 	if (alc5623->jack_det_ctrl) {
921 		snd_soc_write(codec, ALC5623_JACK_DET_CTRL,
922 				alc5623->jack_det_ctrl);
923 	}
924 
925 	switch (alc5623->id) {
926 	case 0x21:
927 		snd_soc_add_codec_controls(codec, alc5621_vol_snd_controls,
928 			ARRAY_SIZE(alc5621_vol_snd_controls));
929 		break;
930 	case 0x22:
931 		snd_soc_add_codec_controls(codec, alc5622_vol_snd_controls,
932 			ARRAY_SIZE(alc5622_vol_snd_controls));
933 		break;
934 	case 0x23:
935 		snd_soc_add_codec_controls(codec, alc5623_vol_snd_controls,
936 			ARRAY_SIZE(alc5623_vol_snd_controls));
937 		break;
938 	default:
939 		return -EINVAL;
940 	}
941 
942 	snd_soc_add_codec_controls(codec, alc5623_snd_controls,
943 			ARRAY_SIZE(alc5623_snd_controls));
944 
945 	snd_soc_dapm_new_controls(dapm, alc5623_dapm_widgets,
946 					ARRAY_SIZE(alc5623_dapm_widgets));
947 
948 	/* set up audio path interconnects */
949 	snd_soc_dapm_add_routes(dapm, intercon, ARRAY_SIZE(intercon));
950 
951 	switch (alc5623->id) {
952 	case 0x21:
953 	case 0x22:
954 		snd_soc_dapm_new_controls(dapm, alc5623_dapm_amp_widgets,
955 					ARRAY_SIZE(alc5623_dapm_amp_widgets));
956 		snd_soc_dapm_add_routes(dapm, intercon_amp_spk,
957 					ARRAY_SIZE(intercon_amp_spk));
958 		break;
959 	case 0x23:
960 		snd_soc_dapm_add_routes(dapm, intercon_spk,
961 					ARRAY_SIZE(intercon_spk));
962 		break;
963 	default:
964 		return -EINVAL;
965 	}
966 
967 	return ret;
968 }
969 
970 /* power down chip */
971 static int alc5623_remove(struct snd_soc_codec *codec)
972 {
973 	alc5623_set_bias_level(codec, SND_SOC_BIAS_OFF);
974 	return 0;
975 }
976 
977 static struct snd_soc_codec_driver soc_codec_device_alc5623 = {
978 	.probe = alc5623_probe,
979 	.remove = alc5623_remove,
980 	.suspend = alc5623_suspend,
981 	.resume = alc5623_resume,
982 	.set_bias_level = alc5623_set_bias_level,
983 	.reg_cache_size = ALC5623_VENDOR_ID2+2,
984 	.reg_word_size = sizeof(u16),
985 	.reg_cache_step = 2,
986 };
987 
988 /*
989  * ALC5623 2 wire address is determined by A1 pin
990  * state during powerup.
991  *    low  = 0x1a
992  *    high = 0x1b
993  */
994 static __devinit int alc5623_i2c_probe(struct i2c_client *client,
995 				const struct i2c_device_id *id)
996 {
997 	struct alc5623_platform_data *pdata;
998 	struct alc5623_priv *alc5623;
999 	int ret, vid1, vid2;
1000 
1001 	vid1 = i2c_smbus_read_word_data(client, ALC5623_VENDOR_ID1);
1002 	if (vid1 < 0) {
1003 		dev_err(&client->dev, "failed to read I2C\n");
1004 		return -EIO;
1005 	}
1006 	vid1 = ((vid1 & 0xff) << 8) | (vid1 >> 8);
1007 
1008 	vid2 = i2c_smbus_read_byte_data(client, ALC5623_VENDOR_ID2);
1009 	if (vid2 < 0) {
1010 		dev_err(&client->dev, "failed to read I2C\n");
1011 		return -EIO;
1012 	}
1013 
1014 	if ((vid1 != 0x10ec) || (vid2 != id->driver_data)) {
1015 		dev_err(&client->dev, "unknown or wrong codec\n");
1016 		dev_err(&client->dev, "Expected %x:%lx, got %x:%x\n",
1017 				0x10ec, id->driver_data,
1018 				vid1, vid2);
1019 		return -ENODEV;
1020 	}
1021 
1022 	dev_dbg(&client->dev, "Found codec id : alc56%02x\n", vid2);
1023 
1024 	alc5623 = devm_kzalloc(&client->dev, sizeof(struct alc5623_priv),
1025 			       GFP_KERNEL);
1026 	if (alc5623 == NULL)
1027 		return -ENOMEM;
1028 
1029 	pdata = client->dev.platform_data;
1030 	if (pdata) {
1031 		alc5623->add_ctrl = pdata->add_ctrl;
1032 		alc5623->jack_det_ctrl = pdata->jack_det_ctrl;
1033 	}
1034 
1035 	alc5623->id = vid2;
1036 	switch (alc5623->id) {
1037 	case 0x21:
1038 		alc5623_dai.name = "alc5621-hifi";
1039 		break;
1040 	case 0x22:
1041 		alc5623_dai.name = "alc5622-hifi";
1042 		break;
1043 	case 0x23:
1044 		alc5623_dai.name = "alc5623-hifi";
1045 		break;
1046 	default:
1047 		return -EINVAL;
1048 	}
1049 
1050 	i2c_set_clientdata(client, alc5623);
1051 	alc5623->control_type = SND_SOC_I2C;
1052 
1053 	ret =  snd_soc_register_codec(&client->dev,
1054 		&soc_codec_device_alc5623, &alc5623_dai, 1);
1055 	if (ret != 0)
1056 		dev_err(&client->dev, "Failed to register codec: %d\n", ret);
1057 
1058 	return ret;
1059 }
1060 
1061 static __devexit int alc5623_i2c_remove(struct i2c_client *client)
1062 {
1063 	snd_soc_unregister_codec(&client->dev);
1064 	return 0;
1065 }
1066 
1067 static const struct i2c_device_id alc5623_i2c_table[] = {
1068 	{"alc5621", 0x21},
1069 	{"alc5622", 0x22},
1070 	{"alc5623", 0x23},
1071 	{}
1072 };
1073 MODULE_DEVICE_TABLE(i2c, alc5623_i2c_table);
1074 
1075 /*  i2c codec control layer */
1076 static struct i2c_driver alc5623_i2c_driver = {
1077 	.driver = {
1078 		.name = "alc562x-codec",
1079 		.owner = THIS_MODULE,
1080 	},
1081 	.probe = alc5623_i2c_probe,
1082 	.remove =  __devexit_p(alc5623_i2c_remove),
1083 	.id_table = alc5623_i2c_table,
1084 };
1085 
1086 module_i2c_driver(alc5623_i2c_driver);
1087 
1088 MODULE_DESCRIPTION("ASoC alc5621/2/3 driver");
1089 MODULE_AUTHOR("Arnaud Patard <arnaud.patard@rtp-net.org>");
1090 MODULE_LICENSE("GPL");
1091