xref: /linux/sound/soc/codecs/alc5623.c (revision b43ab901d671e3e3cad425ea5e9a3c74e266dcdd)
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_pcm_runtime *rtd = substream->private_data;
709 	struct snd_soc_codec *codec = rtd->codec;
710 	struct alc5623_priv *alc5623 = snd_soc_codec_get_drvdata(codec);
711 	int coeff, rate;
712 	u16 iface;
713 
714 	iface = snd_soc_read(codec, ALC5623_DAI_CONTROL);
715 	iface &= ~ALC5623_DAI_I2S_DL_MASK;
716 
717 	/* bit size */
718 	switch (params_format(params)) {
719 	case SNDRV_PCM_FORMAT_S16_LE:
720 		iface |= ALC5623_DAI_I2S_DL_16;
721 		break;
722 	case SNDRV_PCM_FORMAT_S20_3LE:
723 		iface |= ALC5623_DAI_I2S_DL_20;
724 		break;
725 	case SNDRV_PCM_FORMAT_S24_LE:
726 		iface |= ALC5623_DAI_I2S_DL_24;
727 		break;
728 	case SNDRV_PCM_FORMAT_S32_LE:
729 		iface |= ALC5623_DAI_I2S_DL_32;
730 		break;
731 	default:
732 		return -EINVAL;
733 	}
734 
735 	/* set iface & srate */
736 	snd_soc_write(codec, ALC5623_DAI_CONTROL, iface);
737 	rate = params_rate(params);
738 	coeff = get_coeff(codec, rate);
739 	if (coeff < 0)
740 		return -EINVAL;
741 
742 	coeff = coeff_div[coeff].regvalue;
743 	dev_dbg(codec->dev, "%s: sysclk=%d,rate=%d,coeff=0x%04x\n",
744 		__func__, alc5623->sysclk, rate, coeff);
745 	snd_soc_write(codec, ALC5623_STEREO_AD_DA_CLK_CTRL, coeff);
746 
747 	return 0;
748 }
749 
750 static int alc5623_mute(struct snd_soc_dai *dai, int mute)
751 {
752 	struct snd_soc_codec *codec = dai->codec;
753 	u16 hp_mute = ALC5623_MISC_M_DAC_L_INPUT | ALC5623_MISC_M_DAC_R_INPUT;
754 	u16 mute_reg = snd_soc_read(codec, ALC5623_MISC_CTRL) & ~hp_mute;
755 
756 	if (mute)
757 		mute_reg |= hp_mute;
758 
759 	return snd_soc_write(codec, ALC5623_MISC_CTRL, mute_reg);
760 }
761 
762 #define ALC5623_ADD2_POWER_EN (ALC5623_PWR_ADD2_VREF \
763 	| ALC5623_PWR_ADD2_DAC_REF_CIR)
764 
765 #define ALC5623_ADD3_POWER_EN (ALC5623_PWR_ADD3_MAIN_BIAS \
766 	| ALC5623_PWR_ADD3_MIC1_BOOST_AD)
767 
768 #define ALC5623_ADD1_POWER_EN \
769 	(ALC5623_PWR_ADD1_SHORT_CURR_DET_EN | ALC5623_PWR_ADD1_SOFTGEN_EN \
770 	| ALC5623_PWR_ADD1_DEPOP_BUF_HP | ALC5623_PWR_ADD1_HP_OUT_AMP \
771 	| ALC5623_PWR_ADD1_HP_OUT_ENH_AMP)
772 
773 #define ALC5623_ADD1_POWER_EN_5622 \
774 	(ALC5623_PWR_ADD1_SHORT_CURR_DET_EN \
775 	| ALC5623_PWR_ADD1_HP_OUT_AMP)
776 
777 static void enable_power_depop(struct snd_soc_codec *codec)
778 {
779 	struct alc5623_priv *alc5623 = snd_soc_codec_get_drvdata(codec);
780 
781 	snd_soc_update_bits(codec, ALC5623_PWR_MANAG_ADD1,
782 				ALC5623_PWR_ADD1_SOFTGEN_EN,
783 				ALC5623_PWR_ADD1_SOFTGEN_EN);
784 
785 	snd_soc_write(codec, ALC5623_PWR_MANAG_ADD3, ALC5623_ADD3_POWER_EN);
786 
787 	snd_soc_update_bits(codec, ALC5623_MISC_CTRL,
788 				ALC5623_MISC_HP_DEPOP_MODE2_EN,
789 				ALC5623_MISC_HP_DEPOP_MODE2_EN);
790 
791 	msleep(500);
792 
793 	snd_soc_write(codec, ALC5623_PWR_MANAG_ADD2, ALC5623_ADD2_POWER_EN);
794 
795 	/* avoid writing '1' into 5622 reserved bits */
796 	if (alc5623->id == 0x22)
797 		snd_soc_write(codec, ALC5623_PWR_MANAG_ADD1,
798 			ALC5623_ADD1_POWER_EN_5622);
799 	else
800 		snd_soc_write(codec, ALC5623_PWR_MANAG_ADD1,
801 			ALC5623_ADD1_POWER_EN);
802 
803 	/* disable HP Depop2 */
804 	snd_soc_update_bits(codec, ALC5623_MISC_CTRL,
805 				ALC5623_MISC_HP_DEPOP_MODE2_EN,
806 				0);
807 
808 }
809 
810 static int alc5623_set_bias_level(struct snd_soc_codec *codec,
811 				      enum snd_soc_bias_level level)
812 {
813 	switch (level) {
814 	case SND_SOC_BIAS_ON:
815 		enable_power_depop(codec);
816 		break;
817 	case SND_SOC_BIAS_PREPARE:
818 		break;
819 	case SND_SOC_BIAS_STANDBY:
820 		/* everything off except vref/vmid, */
821 		snd_soc_write(codec, ALC5623_PWR_MANAG_ADD2,
822 				ALC5623_PWR_ADD2_VREF);
823 		snd_soc_write(codec, ALC5623_PWR_MANAG_ADD3,
824 				ALC5623_PWR_ADD3_MAIN_BIAS);
825 		break;
826 	case SND_SOC_BIAS_OFF:
827 		/* everything off, dac mute, inactive */
828 		snd_soc_write(codec, ALC5623_PWR_MANAG_ADD2, 0);
829 		snd_soc_write(codec, ALC5623_PWR_MANAG_ADD3, 0);
830 		snd_soc_write(codec, ALC5623_PWR_MANAG_ADD1, 0);
831 		break;
832 	}
833 	codec->dapm.bias_level = level;
834 	return 0;
835 }
836 
837 #define ALC5623_FORMATS	(SNDRV_PCM_FMTBIT_S16_LE \
838 			| SNDRV_PCM_FMTBIT_S24_LE \
839 			| SNDRV_PCM_FMTBIT_S32_LE)
840 
841 static const struct snd_soc_dai_ops alc5623_dai_ops = {
842 		.hw_params = alc5623_pcm_hw_params,
843 		.digital_mute = alc5623_mute,
844 		.set_fmt = alc5623_set_dai_fmt,
845 		.set_sysclk = alc5623_set_dai_sysclk,
846 		.set_pll = alc5623_set_dai_pll,
847 };
848 
849 static struct snd_soc_dai_driver alc5623_dai = {
850 	.name = "alc5623-hifi",
851 	.playback = {
852 		.stream_name = "Playback",
853 		.channels_min = 1,
854 		.channels_max = 2,
855 		.rate_min =	8000,
856 		.rate_max =	48000,
857 		.rates = SNDRV_PCM_RATE_8000_48000,
858 		.formats = ALC5623_FORMATS,},
859 	.capture = {
860 		.stream_name = "Capture",
861 		.channels_min = 1,
862 		.channels_max = 2,
863 		.rate_min =	8000,
864 		.rate_max =	48000,
865 		.rates = SNDRV_PCM_RATE_8000_48000,
866 		.formats = ALC5623_FORMATS,},
867 
868 	.ops = &alc5623_dai_ops,
869 };
870 
871 static int alc5623_suspend(struct snd_soc_codec *codec)
872 {
873 	alc5623_set_bias_level(codec, SND_SOC_BIAS_OFF);
874 	return 0;
875 }
876 
877 static int alc5623_resume(struct snd_soc_codec *codec)
878 {
879 	int i, step = codec->driver->reg_cache_step;
880 	u16 *cache = codec->reg_cache;
881 
882 	/* Sync reg_cache with the hardware */
883 	for (i = 2 ; i < codec->driver->reg_cache_size ; i += step)
884 		snd_soc_write(codec, i, cache[i]);
885 
886 	alc5623_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
887 
888 	/* charge alc5623 caps */
889 	if (codec->dapm.suspend_bias_level == SND_SOC_BIAS_ON) {
890 		alc5623_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
891 		codec->dapm.bias_level = SND_SOC_BIAS_ON;
892 		alc5623_set_bias_level(codec, codec->dapm.bias_level);
893 	}
894 
895 	return 0;
896 }
897 
898 static int alc5623_probe(struct snd_soc_codec *codec)
899 {
900 	struct alc5623_priv *alc5623 = snd_soc_codec_get_drvdata(codec);
901 	struct snd_soc_dapm_context *dapm = &codec->dapm;
902 	int ret;
903 
904 	ret = snd_soc_codec_set_cache_io(codec, 8, 16, alc5623->control_type);
905 	if (ret < 0) {
906 		dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
907 		return ret;
908 	}
909 
910 	alc5623_reset(codec);
911 	alc5623_fill_cache(codec);
912 
913 	/* power on device */
914 	alc5623_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
915 
916 	if (alc5623->add_ctrl) {
917 		snd_soc_write(codec, ALC5623_ADD_CTRL_REG,
918 				alc5623->add_ctrl);
919 	}
920 
921 	if (alc5623->jack_det_ctrl) {
922 		snd_soc_write(codec, ALC5623_JACK_DET_CTRL,
923 				alc5623->jack_det_ctrl);
924 	}
925 
926 	switch (alc5623->id) {
927 	case 0x21:
928 		snd_soc_add_controls(codec, alc5621_vol_snd_controls,
929 			ARRAY_SIZE(alc5621_vol_snd_controls));
930 		break;
931 	case 0x22:
932 		snd_soc_add_controls(codec, alc5622_vol_snd_controls,
933 			ARRAY_SIZE(alc5622_vol_snd_controls));
934 		break;
935 	case 0x23:
936 		snd_soc_add_controls(codec, alc5623_vol_snd_controls,
937 			ARRAY_SIZE(alc5623_vol_snd_controls));
938 		break;
939 	default:
940 		return -EINVAL;
941 	}
942 
943 	snd_soc_add_controls(codec, alc5623_snd_controls,
944 			ARRAY_SIZE(alc5623_snd_controls));
945 
946 	snd_soc_dapm_new_controls(dapm, alc5623_dapm_widgets,
947 					ARRAY_SIZE(alc5623_dapm_widgets));
948 
949 	/* set up audio path interconnects */
950 	snd_soc_dapm_add_routes(dapm, intercon, ARRAY_SIZE(intercon));
951 
952 	switch (alc5623->id) {
953 	case 0x21:
954 	case 0x22:
955 		snd_soc_dapm_new_controls(dapm, alc5623_dapm_amp_widgets,
956 					ARRAY_SIZE(alc5623_dapm_amp_widgets));
957 		snd_soc_dapm_add_routes(dapm, intercon_amp_spk,
958 					ARRAY_SIZE(intercon_amp_spk));
959 		break;
960 	case 0x23:
961 		snd_soc_dapm_add_routes(dapm, intercon_spk,
962 					ARRAY_SIZE(intercon_spk));
963 		break;
964 	default:
965 		return -EINVAL;
966 	}
967 
968 	return ret;
969 }
970 
971 /* power down chip */
972 static int alc5623_remove(struct snd_soc_codec *codec)
973 {
974 	alc5623_set_bias_level(codec, SND_SOC_BIAS_OFF);
975 	return 0;
976 }
977 
978 static struct snd_soc_codec_driver soc_codec_device_alc5623 = {
979 	.probe = alc5623_probe,
980 	.remove = alc5623_remove,
981 	.suspend = alc5623_suspend,
982 	.resume = alc5623_resume,
983 	.set_bias_level = alc5623_set_bias_level,
984 	.reg_cache_size = ALC5623_VENDOR_ID2+2,
985 	.reg_word_size = sizeof(u16),
986 	.reg_cache_step = 2,
987 };
988 
989 /*
990  * ALC5623 2 wire address is determined by A1 pin
991  * state during powerup.
992  *    low  = 0x1a
993  *    high = 0x1b
994  */
995 static int alc5623_i2c_probe(struct i2c_client *client,
996 				const struct i2c_device_id *id)
997 {
998 	struct alc5623_platform_data *pdata;
999 	struct alc5623_priv *alc5623;
1000 	int ret, vid1, vid2;
1001 
1002 	vid1 = i2c_smbus_read_word_data(client, ALC5623_VENDOR_ID1);
1003 	if (vid1 < 0) {
1004 		dev_err(&client->dev, "failed to read I2C\n");
1005 		return -EIO;
1006 	}
1007 	vid1 = ((vid1 & 0xff) << 8) | (vid1 >> 8);
1008 
1009 	vid2 = i2c_smbus_read_byte_data(client, ALC5623_VENDOR_ID2);
1010 	if (vid2 < 0) {
1011 		dev_err(&client->dev, "failed to read I2C\n");
1012 		return -EIO;
1013 	}
1014 
1015 	if ((vid1 != 0x10ec) || (vid2 != id->driver_data)) {
1016 		dev_err(&client->dev, "unknown or wrong codec\n");
1017 		dev_err(&client->dev, "Expected %x:%lx, got %x:%x\n",
1018 				0x10ec, id->driver_data,
1019 				vid1, vid2);
1020 		return -ENODEV;
1021 	}
1022 
1023 	dev_dbg(&client->dev, "Found codec id : alc56%02x\n", vid2);
1024 
1025 	alc5623 = devm_kzalloc(&client->dev, sizeof(struct alc5623_priv),
1026 			       GFP_KERNEL);
1027 	if (alc5623 == NULL)
1028 		return -ENOMEM;
1029 
1030 	pdata = client->dev.platform_data;
1031 	if (pdata) {
1032 		alc5623->add_ctrl = pdata->add_ctrl;
1033 		alc5623->jack_det_ctrl = pdata->jack_det_ctrl;
1034 	}
1035 
1036 	alc5623->id = vid2;
1037 	switch (alc5623->id) {
1038 	case 0x21:
1039 		alc5623_dai.name = "alc5621-hifi";
1040 		break;
1041 	case 0x22:
1042 		alc5623_dai.name = "alc5622-hifi";
1043 		break;
1044 	case 0x23:
1045 		alc5623_dai.name = "alc5623-hifi";
1046 		break;
1047 	default:
1048 		return -EINVAL;
1049 	}
1050 
1051 	i2c_set_clientdata(client, alc5623);
1052 	alc5623->control_type = SND_SOC_I2C;
1053 
1054 	ret =  snd_soc_register_codec(&client->dev,
1055 		&soc_codec_device_alc5623, &alc5623_dai, 1);
1056 	if (ret != 0)
1057 		dev_err(&client->dev, "Failed to register codec: %d\n", ret);
1058 
1059 	return ret;
1060 }
1061 
1062 static int alc5623_i2c_remove(struct i2c_client *client)
1063 {
1064 	snd_soc_unregister_codec(&client->dev);
1065 	return 0;
1066 }
1067 
1068 static const struct i2c_device_id alc5623_i2c_table[] = {
1069 	{"alc5621", 0x21},
1070 	{"alc5622", 0x22},
1071 	{"alc5623", 0x23},
1072 	{}
1073 };
1074 MODULE_DEVICE_TABLE(i2c, alc5623_i2c_table);
1075 
1076 /*  i2c codec control layer */
1077 static struct i2c_driver alc5623_i2c_driver = {
1078 	.driver = {
1079 		.name = "alc562x-codec",
1080 		.owner = THIS_MODULE,
1081 	},
1082 	.probe = alc5623_i2c_probe,
1083 	.remove =  __devexit_p(alc5623_i2c_remove),
1084 	.id_table = alc5623_i2c_table,
1085 };
1086 
1087 static int __init alc5623_modinit(void)
1088 {
1089 	int ret;
1090 
1091 	ret = i2c_add_driver(&alc5623_i2c_driver);
1092 	if (ret != 0) {
1093 		printk(KERN_ERR "%s: can't add i2c driver", __func__);
1094 		return ret;
1095 	}
1096 
1097 	return ret;
1098 }
1099 module_init(alc5623_modinit);
1100 
1101 static void __exit alc5623_modexit(void)
1102 {
1103 	i2c_del_driver(&alc5623_i2c_driver);
1104 }
1105 module_exit(alc5623_modexit);
1106 
1107 MODULE_DESCRIPTION("ASoC alc5621/2/3 driver");
1108 MODULE_AUTHOR("Arnaud Patard <arnaud.patard@rtp-net.org>");
1109 MODULE_LICENSE("GPL");
1110