xref: /linux/sound/soc/codecs/alc5632.c (revision e6968a1719a88afa4708ff43696d6615f0be90be)
1 /*
2 * alc5632.c  --  ALC5632 ALSA SoC Audio Codec
3 *
4 * Copyright (C) 2011 The AC100 Kernel Team <ac100@lists.lauchpad.net>
5 *
6 * Authors:  Leon Romanovsky <leon@leon.nu>
7 *           Andrey Danin <danindrey@mail.ru>
8 *           Ilya Petrov <ilya.muromec@gmail.com>
9 *           Marc Dietrich <marvin24@gmx.de>
10 *
11 * Based on alc5623.c by Arnaud Patard
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
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 <linux/regmap.h>
26 #include <sound/core.h>
27 #include <sound/pcm.h>
28 #include <sound/pcm_params.h>
29 #include <sound/tlv.h>
30 #include <sound/soc.h>
31 #include <sound/initval.h>
32 
33 #include "alc5632.h"
34 
35 /*
36  * ALC5632 register cache
37  */
38 static struct reg_default  alc5632_reg_defaults[] = {
39 	{   2, 0x8080 },	/* R2   - Speaker Output Volume */
40 	{   4, 0x8080 },	/* R4   - Headphone Output Volume */
41 	{   6, 0x8080 },	/* R6   - AUXOUT Volume */
42 	{   8, 0xC800 },	/* R8   - Phone Input */
43 	{  10, 0xE808 },	/* R10  - LINE_IN Volume */
44 	{  12, 0x1010 },	/* R12  - STEREO DAC Input Volume */
45 	{  14, 0x0808 },	/* R14  - MIC Input Volume */
46 	{  16, 0xEE0F },	/* R16  - Stereo DAC and MIC Routing Control */
47 	{  18, 0xCBCB },	/* R18  - ADC Record Gain */
48 	{  20, 0x7F7F },	/* R20  - ADC Record Mixer Control */
49 	{  24, 0xE010 },	/* R24  - Voice DAC Volume */
50 	{  28, 0x8008 },	/* R28  - Output Mixer Control */
51 	{  34, 0x0000 },	/* R34  - Microphone Control */
52 	{  36, 0x00C0 },    /* R36  - Codec Digital MIC/Digital Boost
53 						   Control */
54 	{  46, 0x0000 },	/* R46  - Stereo DAC/Voice DAC/Stereo ADC
55 						   Function Select */
56 	{  52, 0x8000 },	/* R52  - Main Serial Data Port Control
57 						   (Stereo I2S) */
58 	{  54, 0x0000 },	/* R54  - Extend Serial Data Port Control
59 						   (VoDAC_I2S/PCM) */
60 	{  58, 0x0000 },	/* R58  - Power Management Addition 1 */
61 	{  60, 0x0000 },	/* R60  - Power Management Addition 2 */
62 	{  62, 0x8000 },	/* R62  - Power Management Addition 3 */
63 	{  64, 0x0C0A },	/* R64  - General Purpose Control Register 1 */
64 	{  66, 0x0000 },	/* R66  - General Purpose Control Register 2 */
65 	{  68, 0x0000 },	/* R68  - PLL1 Control */
66 	{  70, 0x0000 },	/* R70  - PLL2 Control */
67 	{  76, 0xBE3E },	/* R76  - GPIO Pin Configuration */
68 	{  78, 0xBE3E },	/* R78  - GPIO Pin Polarity */
69 	{  80, 0x0000 },	/* R80  - GPIO Pin Sticky */
70 	{  82, 0x0000 },	/* R82  - GPIO Pin Wake Up */
71 	{  86, 0x0000 },	/* R86  - Pin Sharing */
72 	{  90, 0x0009 },	/* R90  - Soft Volume Control Setting */
73 	{  92, 0x0000 },	/* R92  - GPIO_Output Pin Control */
74 	{  94, 0x3000 },	/* R94  - MISC Control */
75 	{  96, 0x3075 },	/* R96  - Stereo DAC Clock Control_1 */
76 	{  98, 0x1010 },	/* R98  - Stereo DAC Clock Control_2 */
77 	{ 100, 0x3110 },	/* R100 - VoDAC_PCM Clock Control_1 */
78 	{ 104, 0x0553 },	/* R104 - Pseudo Stereo and Spatial Effect
79 						   Block Control */
80 	{ 106, 0x0000 },	/* R106 - Private Register Address */
81 };
82 
83 /* codec private data */
84 struct alc5632_priv {
85 	struct regmap *regmap;
86 	u8 id;
87 	unsigned int sysclk;
88 };
89 
90 static bool alc5632_volatile_register(struct device *dev,
91 							unsigned int reg)
92 {
93 	switch (reg) {
94 	case ALC5632_RESET:
95 	case ALC5632_PWR_DOWN_CTRL_STATUS:
96 	case ALC5632_GPIO_PIN_STATUS:
97 	case ALC5632_OVER_CURR_STATUS:
98 	case ALC5632_HID_CTRL_DATA:
99 	case ALC5632_EQ_CTRL:
100 	case ALC5632_VENDOR_ID1:
101 	case ALC5632_VENDOR_ID2:
102 		return true;
103 
104 	default:
105 		break;
106 	}
107 
108 	return false;
109 }
110 
111 static inline int alc5632_reset(struct regmap *map)
112 {
113 	return regmap_write(map, ALC5632_RESET, 0x59B4);
114 }
115 
116 static int amp_mixer_event(struct snd_soc_dapm_widget *w,
117 	struct snd_kcontrol *kcontrol, int event)
118 {
119 	/* to power-on/off class-d amp generators/speaker */
120 	/* need to write to 'index-46h' register :        */
121 	/* so write index num (here 0x46) to reg 0x6a     */
122 	/* and then 0xffff/0 to reg 0x6c                  */
123 	snd_soc_write(w->codec, ALC5632_HID_CTRL_INDEX, 0x46);
124 
125 	switch (event) {
126 	case SND_SOC_DAPM_PRE_PMU:
127 		snd_soc_write(w->codec, ALC5632_HID_CTRL_DATA, 0xFFFF);
128 		break;
129 	case SND_SOC_DAPM_POST_PMD:
130 		snd_soc_write(w->codec, ALC5632_HID_CTRL_DATA, 0);
131 		break;
132 	}
133 
134 	return 0;
135 }
136 
137 /*
138  * ALC5632 Controls
139  */
140 
141 /* -34.5db min scale, 1.5db steps, no mute */
142 static const DECLARE_TLV_DB_SCALE(vol_tlv, -3450, 150, 0);
143 /* -46.5db min scale, 1.5db steps, no mute */
144 static const DECLARE_TLV_DB_SCALE(hp_tlv, -4650, 150, 0);
145 /* -16.5db min scale, 1.5db steps, no mute */
146 static const DECLARE_TLV_DB_SCALE(adc_rec_tlv, -1650, 150, 0);
147 static const unsigned int boost_tlv[] = {
148 	TLV_DB_RANGE_HEAD(2),
149 	0, 1, TLV_DB_SCALE_ITEM(0, 2000, 0),
150 	1, 3, TLV_DB_SCALE_ITEM(2000, 1000, 0),
151 };
152 /* 0db min scale, 6 db steps, no mute */
153 static const DECLARE_TLV_DB_SCALE(dig_tlv, 0, 600, 0);
154 /* 0db min scalem 0.75db steps, no mute */
155 static const DECLARE_TLV_DB_SCALE(vdac_tlv, -3525, 75, 0);
156 
157 static const struct snd_kcontrol_new alc5632_vol_snd_controls[] = {
158 	/* left starts at bit 8, right at bit 0 */
159 	/* 31 steps (5 bit), -46.5db scale */
160 	SOC_DOUBLE_TLV("Speaker Playback Volume",
161 			ALC5632_SPK_OUT_VOL, 8, 0, 31, 1, hp_tlv),
162 	/* bit 15 mutes left, bit 7 right */
163 	SOC_DOUBLE("Speaker Playback Switch",
164 			ALC5632_SPK_OUT_VOL, 15, 7, 1, 1),
165 	SOC_DOUBLE_TLV("Headphone Playback Volume",
166 			ALC5632_HP_OUT_VOL, 8, 0, 31, 1, hp_tlv),
167 	SOC_DOUBLE("Headphone Playback Switch",
168 			ALC5632_HP_OUT_VOL, 15, 7, 1, 1),
169 };
170 
171 static const struct snd_kcontrol_new alc5632_snd_controls[] = {
172 	SOC_DOUBLE_TLV("Auxout Playback Volume",
173 			ALC5632_AUX_OUT_VOL, 8, 0, 31, 1, hp_tlv),
174 	SOC_DOUBLE("Auxout Playback Switch",
175 			ALC5632_AUX_OUT_VOL, 15, 7, 1, 1),
176 	SOC_SINGLE_TLV("Voice DAC Playback Volume",
177 			ALC5632_VOICE_DAC_VOL, 0, 63, 0, vdac_tlv),
178 	SOC_SINGLE("Voice DAC Playback Switch",
179 			ALC5632_VOICE_DAC_VOL, 12, 1, 1),
180 	SOC_SINGLE_TLV("Phone Playback Volume",
181 			ALC5632_PHONE_IN_VOL, 8, 31, 1, vol_tlv),
182 	SOC_DOUBLE_TLV("LineIn Playback Volume",
183 			ALC5632_LINE_IN_VOL, 8, 0, 31, 1, vol_tlv),
184 	SOC_DOUBLE_TLV("Master Playback Volume",
185 			ALC5632_STEREO_DAC_IN_VOL, 8, 0, 63, 1, vdac_tlv),
186 	SOC_DOUBLE("Master Playback Switch",
187 			ALC5632_STEREO_DAC_IN_VOL, 15, 7, 1, 1),
188 	SOC_SINGLE_TLV("Mic1 Playback Volume",
189 			ALC5632_MIC_VOL, 8, 31, 1, vol_tlv),
190 	SOC_SINGLE_TLV("Mic2 Playback Volume",
191 			ALC5632_MIC_VOL, 0, 31, 1, vol_tlv),
192 	SOC_DOUBLE_TLV("Rec Capture Volume",
193 			ALC5632_ADC_REC_GAIN, 8, 0, 31, 0, adc_rec_tlv),
194 	SOC_SINGLE_TLV("Mic 1 Boost Volume",
195 			ALC5632_MIC_CTRL, 10, 3, 0, boost_tlv),
196 	SOC_SINGLE_TLV("Mic 2 Boost Volume",
197 			ALC5632_MIC_CTRL, 8, 3, 0, boost_tlv),
198 	SOC_SINGLE_TLV("DMIC Boost Capture Volume",
199 			ALC5632_DIGI_BOOST_CTRL, 0, 7, 0, dig_tlv),
200 	SOC_SINGLE("DMIC En Capture Switch",
201 			ALC5632_DIGI_BOOST_CTRL, 15, 1, 0),
202 	SOC_SINGLE("DMIC PreFilter Capture Switch",
203 			ALC5632_DIGI_BOOST_CTRL, 12, 1, 0),
204 };
205 
206 /*
207  * DAPM Controls
208  */
209 static const struct snd_kcontrol_new alc5632_hp_mixer_controls[] = {
210 SOC_DAPM_SINGLE("LI2HP Playback Switch", ALC5632_LINE_IN_VOL, 15, 1, 1),
211 SOC_DAPM_SINGLE("PHONE2HP Playback Switch", ALC5632_PHONE_IN_VOL, 15, 1, 1),
212 SOC_DAPM_SINGLE("MIC12HP Playback Switch", ALC5632_MIC_ROUTING_CTRL, 15, 1, 1),
213 SOC_DAPM_SINGLE("MIC22HP Playback Switch", ALC5632_MIC_ROUTING_CTRL, 11, 1, 1),
214 SOC_DAPM_SINGLE("VOICE2HP Playback Switch", ALC5632_VOICE_DAC_VOL, 15, 1, 1),
215 };
216 
217 static const struct snd_kcontrol_new alc5632_hpl_mixer_controls[] = {
218 SOC_DAPM_SINGLE("ADC2HP_L Playback Switch", ALC5632_ADC_REC_GAIN, 15, 1, 1),
219 SOC_DAPM_SINGLE("DACL2HP Playback Switch", ALC5632_MIC_ROUTING_CTRL, 3, 1, 1),
220 };
221 
222 static const struct snd_kcontrol_new alc5632_hpr_mixer_controls[] = {
223 SOC_DAPM_SINGLE("ADC2HP_R Playback Switch", ALC5632_ADC_REC_GAIN, 7, 1, 1),
224 SOC_DAPM_SINGLE("DACR2HP Playback Switch", ALC5632_MIC_ROUTING_CTRL, 2, 1, 1),
225 };
226 
227 static const struct snd_kcontrol_new alc5632_mono_mixer_controls[] = {
228 SOC_DAPM_SINGLE("ADC2MONO_L Playback Switch", ALC5632_ADC_REC_GAIN, 14, 1, 1),
229 SOC_DAPM_SINGLE("ADC2MONO_R Playback Switch", ALC5632_ADC_REC_GAIN, 6, 1, 1),
230 SOC_DAPM_SINGLE("LI2MONO Playback Switch", ALC5632_LINE_IN_VOL, 13, 1, 1),
231 SOC_DAPM_SINGLE("MIC12MONO Playback Switch",
232 					ALC5632_MIC_ROUTING_CTRL, 13, 1, 1),
233 SOC_DAPM_SINGLE("MIC22MONO Playback Switch",
234 					ALC5632_MIC_ROUTING_CTRL, 9, 1, 1),
235 SOC_DAPM_SINGLE("DAC2MONO Playback Switch", ALC5632_MIC_ROUTING_CTRL, 0, 1, 1),
236 SOC_DAPM_SINGLE("VOICE2MONO Playback Switch", ALC5632_VOICE_DAC_VOL, 13, 1, 1),
237 };
238 
239 static const struct snd_kcontrol_new alc5632_speaker_mixer_controls[] = {
240 SOC_DAPM_SINGLE("LI2SPK Playback Switch", ALC5632_LINE_IN_VOL, 14, 1, 1),
241 SOC_DAPM_SINGLE("PHONE2SPK Playback Switch", ALC5632_PHONE_IN_VOL, 14, 1, 1),
242 SOC_DAPM_SINGLE("MIC12SPK Playback Switch",
243 					ALC5632_MIC_ROUTING_CTRL, 14, 1, 1),
244 SOC_DAPM_SINGLE("MIC22SPK Playback Switch",
245 					ALC5632_MIC_ROUTING_CTRL, 10, 1, 1),
246 SOC_DAPM_SINGLE("DAC2SPK Playback Switch", ALC5632_MIC_ROUTING_CTRL, 1, 1, 1),
247 SOC_DAPM_SINGLE("VOICE2SPK Playback Switch", ALC5632_VOICE_DAC_VOL, 14, 1, 1),
248 };
249 
250 /* Left Record Mixer */
251 static const struct snd_kcontrol_new alc5632_captureL_mixer_controls[] = {
252 SOC_DAPM_SINGLE("MIC12REC_L Capture Switch", ALC5632_ADC_REC_MIXER, 14, 1, 1),
253 SOC_DAPM_SINGLE("MIC22REC_L Capture Switch", ALC5632_ADC_REC_MIXER, 13, 1, 1),
254 SOC_DAPM_SINGLE("LIL2REC Capture Switch", ALC5632_ADC_REC_MIXER, 12, 1, 1),
255 SOC_DAPM_SINGLE("PH2REC_L Capture Switch", ALC5632_ADC_REC_MIXER, 11, 1, 1),
256 SOC_DAPM_SINGLE("HPL2REC Capture Switch", ALC5632_ADC_REC_MIXER, 10, 1, 1),
257 SOC_DAPM_SINGLE("SPK2REC_L Capture Switch", ALC5632_ADC_REC_MIXER, 9, 1, 1),
258 SOC_DAPM_SINGLE("MONO2REC_L Capture Switch", ALC5632_ADC_REC_MIXER, 8, 1, 1),
259 };
260 
261 /* Right Record Mixer */
262 static const struct snd_kcontrol_new alc5632_captureR_mixer_controls[] = {
263 SOC_DAPM_SINGLE("MIC12REC_R Capture Switch", ALC5632_ADC_REC_MIXER, 6, 1, 1),
264 SOC_DAPM_SINGLE("MIC22REC_R Capture Switch", ALC5632_ADC_REC_MIXER, 5, 1, 1),
265 SOC_DAPM_SINGLE("LIR2REC Capture Switch", ALC5632_ADC_REC_MIXER, 4, 1, 1),
266 SOC_DAPM_SINGLE("PH2REC_R Capture Switch", ALC5632_ADC_REC_MIXER, 3, 1, 1),
267 SOC_DAPM_SINGLE("HPR2REC Capture Switch", ALC5632_ADC_REC_MIXER, 2, 1, 1),
268 SOC_DAPM_SINGLE("SPK2REC_R Capture Switch", ALC5632_ADC_REC_MIXER, 1, 1, 1),
269 SOC_DAPM_SINGLE("MONO2REC_R Capture Switch", ALC5632_ADC_REC_MIXER, 0, 1, 1),
270 };
271 
272 /* Dmic Mixer */
273 static const struct snd_kcontrol_new alc5632_dmicl_mixer_controls[] = {
274 SOC_DAPM_SINGLE("DMICL2ADC Capture Switch", ALC5632_DIGI_BOOST_CTRL, 7, 1, 1),
275 };
276 static const struct snd_kcontrol_new alc5632_dmicr_mixer_controls[] = {
277 SOC_DAPM_SINGLE("DMICR2ADC Capture Switch", ALC5632_DIGI_BOOST_CTRL, 6, 1, 1),
278 };
279 
280 static const char * const alc5632_spk_n_sour_sel[] = {
281 		"RN/-R", "RP/+R", "LN/-R", "Mute"};
282 static const char * const alc5632_hpl_out_input_sel[] = {
283 		"Vmid", "HP Left Mix"};
284 static const char * const alc5632_hpr_out_input_sel[] = {
285 		"Vmid", "HP Right Mix"};
286 static const char * const alc5632_spkout_input_sel[] = {
287 		"Vmid", "HPOut Mix", "Speaker Mix", "Mono Mix"};
288 static const char * const alc5632_aux_out_input_sel[] = {
289 		"Vmid", "HPOut Mix", "Speaker Mix", "Mono Mix"};
290 static const char * const alc5632_adcr_func_sel[] = {
291 		"Stereo ADC", "Voice ADC"};
292 static const char * const alc5632_i2s_out_sel[] = {
293 		"ADC LR", "Voice Stereo Digital"};
294 
295 /* auxout output mux */
296 static const struct soc_enum alc5632_aux_out_input_enum =
297 SOC_ENUM_SINGLE(ALC5632_OUTPUT_MIXER_CTRL, 6, 4, alc5632_aux_out_input_sel);
298 static const struct snd_kcontrol_new alc5632_auxout_mux_controls =
299 SOC_DAPM_ENUM("AuxOut Mux", alc5632_aux_out_input_enum);
300 
301 /* speaker output mux */
302 static const struct soc_enum alc5632_spkout_input_enum =
303 SOC_ENUM_SINGLE(ALC5632_OUTPUT_MIXER_CTRL, 10, 4, alc5632_spkout_input_sel);
304 static const struct snd_kcontrol_new alc5632_spkout_mux_controls =
305 SOC_DAPM_ENUM("SpeakerOut Mux", alc5632_spkout_input_enum);
306 
307 /* headphone left output mux */
308 static const struct soc_enum alc5632_hpl_out_input_enum =
309 SOC_ENUM_SINGLE(ALC5632_OUTPUT_MIXER_CTRL, 9, 2, alc5632_hpl_out_input_sel);
310 static const struct snd_kcontrol_new alc5632_hpl_out_mux_controls =
311 SOC_DAPM_ENUM("Left Headphone Mux", alc5632_hpl_out_input_enum);
312 
313 /* headphone right output mux */
314 static const struct soc_enum alc5632_hpr_out_input_enum =
315 SOC_ENUM_SINGLE(ALC5632_OUTPUT_MIXER_CTRL, 8, 2, alc5632_hpr_out_input_sel);
316 static const struct snd_kcontrol_new alc5632_hpr_out_mux_controls =
317 SOC_DAPM_ENUM("Right Headphone Mux", alc5632_hpr_out_input_enum);
318 
319 /* speaker output N select */
320 static const struct soc_enum alc5632_spk_n_sour_enum =
321 SOC_ENUM_SINGLE(ALC5632_OUTPUT_MIXER_CTRL, 14, 4, alc5632_spk_n_sour_sel);
322 static const struct snd_kcontrol_new alc5632_spkoutn_mux_controls =
323 SOC_DAPM_ENUM("SpeakerOut N Mux", alc5632_spk_n_sour_enum);
324 
325 /* speaker amplifier */
326 static const char *alc5632_amp_names[] = {"AB Amp", "D Amp"};
327 static const struct soc_enum alc5632_amp_enum =
328 	SOC_ENUM_SINGLE(ALC5632_OUTPUT_MIXER_CTRL, 13, 2, alc5632_amp_names);
329 static const struct snd_kcontrol_new alc5632_amp_mux_controls =
330 	SOC_DAPM_ENUM("AB-D Amp Mux", alc5632_amp_enum);
331 
332 /* ADC output select */
333 static const struct soc_enum alc5632_adcr_func_enum =
334 	SOC_ENUM_SINGLE(ALC5632_DAC_FUNC_SELECT, 5, 2, alc5632_adcr_func_sel);
335 static const struct snd_kcontrol_new alc5632_adcr_func_controls =
336 	SOC_DAPM_ENUM("ADCR Mux", alc5632_adcr_func_enum);
337 
338 /* I2S out select */
339 static const struct soc_enum alc5632_i2s_out_enum =
340 	SOC_ENUM_SINGLE(ALC5632_I2S_OUT_CTL, 5, 2, alc5632_i2s_out_sel);
341 static const struct snd_kcontrol_new alc5632_i2s_out_controls =
342 	SOC_DAPM_ENUM("I2SOut Mux", alc5632_i2s_out_enum);
343 
344 static const struct snd_soc_dapm_widget alc5632_dapm_widgets[] = {
345 /* Muxes */
346 SND_SOC_DAPM_MUX("AuxOut Mux", SND_SOC_NOPM, 0, 0,
347 	&alc5632_auxout_mux_controls),
348 SND_SOC_DAPM_MUX("SpeakerOut Mux", SND_SOC_NOPM, 0, 0,
349 	&alc5632_spkout_mux_controls),
350 SND_SOC_DAPM_MUX("Left Headphone Mux", SND_SOC_NOPM, 0, 0,
351 	&alc5632_hpl_out_mux_controls),
352 SND_SOC_DAPM_MUX("Right Headphone Mux", SND_SOC_NOPM, 0, 0,
353 	&alc5632_hpr_out_mux_controls),
354 SND_SOC_DAPM_MUX("SpeakerOut N Mux", SND_SOC_NOPM, 0, 0,
355 	&alc5632_spkoutn_mux_controls),
356 SND_SOC_DAPM_MUX("ADCR Mux", SND_SOC_NOPM, 0, 0,
357 	&alc5632_adcr_func_controls),
358 SND_SOC_DAPM_MUX("I2SOut Mux", ALC5632_PWR_MANAG_ADD1, 11, 0,
359 	&alc5632_i2s_out_controls),
360 
361 /* output mixers */
362 SND_SOC_DAPM_MIXER("HP Mix", SND_SOC_NOPM, 0, 0,
363 	&alc5632_hp_mixer_controls[0],
364 	ARRAY_SIZE(alc5632_hp_mixer_controls)),
365 SND_SOC_DAPM_MIXER("HPR Mix", ALC5632_PWR_MANAG_ADD2, 4, 0,
366 	&alc5632_hpr_mixer_controls[0],
367 	ARRAY_SIZE(alc5632_hpr_mixer_controls)),
368 SND_SOC_DAPM_MIXER("HPL Mix", ALC5632_PWR_MANAG_ADD2, 5, 0,
369 	&alc5632_hpl_mixer_controls[0],
370 	ARRAY_SIZE(alc5632_hpl_mixer_controls)),
371 SND_SOC_DAPM_MIXER("HPOut Mix", SND_SOC_NOPM, 0, 0, NULL, 0),
372 SND_SOC_DAPM_MIXER("Mono Mix", ALC5632_PWR_MANAG_ADD2, 2, 0,
373 	&alc5632_mono_mixer_controls[0],
374 	ARRAY_SIZE(alc5632_mono_mixer_controls)),
375 SND_SOC_DAPM_MIXER("Speaker Mix", ALC5632_PWR_MANAG_ADD2, 3, 0,
376 	&alc5632_speaker_mixer_controls[0],
377 	ARRAY_SIZE(alc5632_speaker_mixer_controls)),
378 SND_SOC_DAPM_MIXER("DMICL Mix", SND_SOC_NOPM, 0, 0,
379 	&alc5632_dmicl_mixer_controls[0],
380 	ARRAY_SIZE(alc5632_dmicl_mixer_controls)),
381 SND_SOC_DAPM_MIXER("DMICR Mix", SND_SOC_NOPM, 0, 0,
382 	&alc5632_dmicr_mixer_controls[0],
383 	ARRAY_SIZE(alc5632_dmicr_mixer_controls)),
384 
385 /* input mixers */
386 SND_SOC_DAPM_MIXER("Left Capture Mix", ALC5632_PWR_MANAG_ADD2, 1, 0,
387 	&alc5632_captureL_mixer_controls[0],
388 	ARRAY_SIZE(alc5632_captureL_mixer_controls)),
389 SND_SOC_DAPM_MIXER("Right Capture Mix", ALC5632_PWR_MANAG_ADD2, 0, 0,
390 	&alc5632_captureR_mixer_controls[0],
391 	ARRAY_SIZE(alc5632_captureR_mixer_controls)),
392 
393 SND_SOC_DAPM_AIF_IN("AIFRXL", "Left HiFi Playback", 0, SND_SOC_NOPM, 0, 0),
394 SND_SOC_DAPM_AIF_IN("AIFRXR", "Right HiFi Playback", 0, SND_SOC_NOPM, 0, 0),
395 SND_SOC_DAPM_AIF_OUT("AIFTXL", "Left HiFi Capture", 0, SND_SOC_NOPM, 0, 0),
396 SND_SOC_DAPM_AIF_OUT("AIFTXR", "Right HiFi Capture", 0, SND_SOC_NOPM, 0, 0),
397 SND_SOC_DAPM_AIF_IN("VAIFRX", "Voice Playback", 0, SND_SOC_NOPM, 0, 0),
398 SND_SOC_DAPM_AIF_OUT("VAIFTX", "Voice Capture", 0, SND_SOC_NOPM, 0, 0),
399 
400 SND_SOC_DAPM_DAC("Voice DAC", NULL, ALC5632_PWR_MANAG_ADD2, 10, 0),
401 SND_SOC_DAPM_DAC("Left DAC", NULL, ALC5632_PWR_MANAG_ADD2, 9, 0),
402 SND_SOC_DAPM_DAC("Right DAC", NULL, ALC5632_PWR_MANAG_ADD2, 8, 0),
403 SND_SOC_DAPM_ADC("Left ADC", NULL, ALC5632_PWR_MANAG_ADD2, 7, 0),
404 SND_SOC_DAPM_ADC("Right ADC", NULL, ALC5632_PWR_MANAG_ADD2, 6, 0),
405 
406 SND_SOC_DAPM_MIXER("DAC Left Channel", ALC5632_PWR_MANAG_ADD1, 15, 0, NULL, 0),
407 SND_SOC_DAPM_MIXER("DAC Right Channel",
408 	ALC5632_PWR_MANAG_ADD1, 14, 0, NULL, 0),
409 SND_SOC_DAPM_MIXER("I2S Mix", ALC5632_PWR_MANAG_ADD1, 11, 0, NULL, 0),
410 SND_SOC_DAPM_MIXER("Phone Mix", SND_SOC_NOPM, 0, 0, NULL, 0),
411 SND_SOC_DAPM_MIXER("Line Mix", SND_SOC_NOPM, 0, 0, NULL, 0),
412 SND_SOC_DAPM_MIXER("Voice Mix", SND_SOC_NOPM, 0, 0, NULL, 0),
413 SND_SOC_DAPM_MIXER("ADCLR", SND_SOC_NOPM, 0, 0, NULL, 0),
414 
415 SND_SOC_DAPM_PGA("Left Headphone", ALC5632_PWR_MANAG_ADD3, 11, 0, NULL, 0),
416 SND_SOC_DAPM_PGA("Right Headphone", ALC5632_PWR_MANAG_ADD3, 10, 0, NULL, 0),
417 SND_SOC_DAPM_PGA("Left Speaker", ALC5632_PWR_MANAG_ADD3, 13, 0, NULL, 0),
418 SND_SOC_DAPM_PGA("Right Speaker", ALC5632_PWR_MANAG_ADD3, 12, 0, NULL, 0),
419 SND_SOC_DAPM_PGA("Aux Out", ALC5632_PWR_MANAG_ADD3, 14, 0, NULL, 0),
420 SND_SOC_DAPM_PGA("Left LineIn", ALC5632_PWR_MANAG_ADD3, 7, 0, NULL, 0),
421 SND_SOC_DAPM_PGA("Right LineIn", ALC5632_PWR_MANAG_ADD3, 6, 0, NULL, 0),
422 SND_SOC_DAPM_PGA("Phone", ALC5632_PWR_MANAG_ADD3, 5, 0, NULL, 0),
423 SND_SOC_DAPM_PGA("Phone ADMix", ALC5632_PWR_MANAG_ADD3, 4, 0, NULL, 0),
424 SND_SOC_DAPM_PGA("MIC1 PGA", ALC5632_PWR_MANAG_ADD3, 3, 0, NULL, 0),
425 SND_SOC_DAPM_PGA("MIC2 PGA", ALC5632_PWR_MANAG_ADD3, 2, 0, NULL, 0),
426 SND_SOC_DAPM_PGA("MIC1 Pre Amp", ALC5632_PWR_MANAG_ADD3, 1, 0, NULL, 0),
427 SND_SOC_DAPM_PGA("MIC2 Pre Amp", ALC5632_PWR_MANAG_ADD3, 0, 0, NULL, 0),
428 SND_SOC_DAPM_SUPPLY("MICBIAS1", ALC5632_PWR_MANAG_ADD1, 3, 0, NULL, 0),
429 SND_SOC_DAPM_SUPPLY("MICBIAS2", ALC5632_PWR_MANAG_ADD1, 2, 0, NULL, 0),
430 
431 SND_SOC_DAPM_PGA_E("D Amp", ALC5632_PWR_MANAG_ADD2, 14, 0, NULL, 0,
432 	amp_mixer_event, SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
433 SND_SOC_DAPM_PGA("AB Amp", ALC5632_PWR_MANAG_ADD2, 15, 0, NULL, 0),
434 SND_SOC_DAPM_MUX("AB-D Amp Mux", ALC5632_PWR_MANAG_ADD1, 10, 0,
435 	&alc5632_amp_mux_controls),
436 
437 SND_SOC_DAPM_OUTPUT("AUXOUT"),
438 SND_SOC_DAPM_OUTPUT("HPL"),
439 SND_SOC_DAPM_OUTPUT("HPR"),
440 SND_SOC_DAPM_OUTPUT("SPKOUT"),
441 SND_SOC_DAPM_OUTPUT("SPKOUTN"),
442 
443 SND_SOC_DAPM_INPUT("LINEINL"),
444 SND_SOC_DAPM_INPUT("LINEINR"),
445 SND_SOC_DAPM_INPUT("PHONEP"),
446 SND_SOC_DAPM_INPUT("PHONEN"),
447 SND_SOC_DAPM_INPUT("DMICDAT"),
448 SND_SOC_DAPM_INPUT("MIC1"),
449 SND_SOC_DAPM_INPUT("MIC2"),
450 SND_SOC_DAPM_VMID("Vmid"),
451 };
452 
453 
454 static const struct snd_soc_dapm_route alc5632_dapm_routes[] = {
455 	/* Playback streams */
456 	{"Left DAC", NULL, "AIFRXL"},
457 	{"Right DAC", NULL, "AIFRXR"},
458 
459 	/* virtual mixer - mixes left & right channels */
460 	{"I2S Mix",	NULL,	"Left DAC"},
461 	{"I2S Mix",	NULL,	"Right DAC"},
462 	{"Line Mix",	NULL,	"Right LineIn"},
463 	{"Line Mix",	NULL,	"Left LineIn"},
464 	{"Phone Mix",	NULL,	"Phone"},
465 	{"Phone Mix",	NULL,	"Phone ADMix"},
466 	{"AUXOUT",		NULL,	"Aux Out"},
467 
468 	/* DAC */
469 	{"DAC Right Channel",	NULL,	"I2S Mix"},
470 	{"DAC Left Channel",	NULL,   "I2S Mix"},
471 
472 	/* HP mixer */
473 	{"HPL Mix",	"ADC2HP_L Playback Switch",	"Left Capture Mix"},
474 	{"HPL Mix", NULL,					"HP Mix"},
475 	{"HPR Mix", "ADC2HP_R Playback Switch",	"Right Capture Mix"},
476 	{"HPR Mix", NULL,					"HP Mix"},
477 	{"HP Mix",	"LI2HP Playback Switch",	"Line Mix"},
478 	{"HP Mix",	"PHONE2HP Playback Switch",	"Phone Mix"},
479 	{"HP Mix",	"MIC12HP Playback Switch",	"MIC1 PGA"},
480 	{"HP Mix",	"MIC22HP Playback Switch",	"MIC2 PGA"},
481 	{"HP Mix", "VOICE2HP Playback Switch",	"Voice Mix"},
482 	{"HPR Mix", "DACR2HP Playback Switch",	"DAC Right Channel"},
483 	{"HPL Mix", "DACL2HP Playback Switch",	"DAC Left Channel"},
484 	{"HPOut Mix", NULL, "HP Mix"},
485 	{"HPOut Mix", NULL, "HPR Mix"},
486 	{"HPOut Mix", NULL, "HPL Mix"},
487 
488 	/* speaker mixer */
489 	{"Speaker Mix", "LI2SPK Playback Switch",	"Line Mix"},
490 	{"Speaker Mix", "PHONE2SPK Playback Switch", "Phone Mix"},
491 	{"Speaker Mix", "MIC12SPK Playback Switch",	"MIC1 PGA"},
492 	{"Speaker Mix", "MIC22SPK Playback Switch",	"MIC2 PGA"},
493 	{"Speaker Mix", "DAC2SPK Playback Switch",	"DAC Left Channel"},
494 	{"Speaker Mix", "VOICE2SPK Playback Switch",	"Voice Mix"},
495 
496 	/* mono mixer */
497 	{"Mono Mix", "ADC2MONO_L Playback Switch",	"Left Capture Mix"},
498 	{"Mono Mix", "ADC2MONO_R Playback Switch",	"Right Capture Mix"},
499 	{"Mono Mix", "LI2MONO Playback Switch",		"Line Mix"},
500 	{"Mono Mix", "MIC12MONO Playback Switch",	"MIC1 PGA"},
501 	{"Mono Mix", "MIC22MONO Playback Switch",	"MIC2 PGA"},
502 	{"Mono Mix", "DAC2MONO Playback Switch",	"DAC Left Channel"},
503 	{"Mono Mix", "VOICE2MONO Playback Switch",	"Voice Mix"},
504 
505 	/* Left record mixer */
506 	{"Left Capture Mix", "LIL2REC Capture Switch", "LINEINL"},
507 	{"Left Capture Mix", "PH2REC_L Capture Switch", "PHONEN"},
508 	{"Left Capture Mix", "MIC12REC_L Capture Switch", "MIC1 Pre Amp"},
509 	{"Left Capture Mix", "MIC22REC_L Capture Switch", "MIC2 Pre Amp"},
510 	{"Left Capture Mix", "HPL2REC Capture Switch", "HPL Mix"},
511 	{"Left Capture Mix", "SPK2REC_L Capture Switch", "Speaker Mix"},
512 	{"Left Capture Mix", "MONO2REC_L Capture Switch", "Mono Mix"},
513 
514 	/*Right record mixer */
515 	{"Right Capture Mix", "LIR2REC Capture Switch", "LINEINR"},
516 	{"Right Capture Mix", "PH2REC_R Capture Switch", "PHONEP"},
517 	{"Right Capture Mix", "MIC12REC_R Capture Switch", "MIC1 Pre Amp"},
518 	{"Right Capture Mix", "MIC22REC_R Capture Switch", "MIC2 Pre Amp"},
519 	{"Right Capture Mix", "HPR2REC Capture Switch", "HPR Mix"},
520 	{"Right Capture Mix", "SPK2REC_R Capture Switch", "Speaker Mix"},
521 	{"Right Capture Mix", "MONO2REC_R Capture Switch", "Mono Mix"},
522 
523 	/* headphone left mux */
524 	{"Left Headphone Mux", "HP Left Mix",		"HPL Mix"},
525 	{"Left Headphone Mux", "Vmid",			"Vmid"},
526 
527 	/* headphone right mux */
528 	{"Right Headphone Mux", "HP Right Mix",		"HPR Mix"},
529 	{"Right Headphone Mux", "Vmid",			"Vmid"},
530 
531 	/* speaker out mux */
532 	{"SpeakerOut Mux", "Vmid",			"Vmid"},
533 	{"SpeakerOut Mux", "HPOut Mix",			"HPOut Mix"},
534 	{"SpeakerOut Mux", "Speaker Mix",		"Speaker Mix"},
535 	{"SpeakerOut Mux", "Mono Mix",			"Mono Mix"},
536 
537 	/* Mono/Aux Out mux */
538 	{"AuxOut Mux", "Vmid",				"Vmid"},
539 	{"AuxOut Mux", "HPOut Mix",			"HPOut Mix"},
540 	{"AuxOut Mux", "Speaker Mix",			"Speaker Mix"},
541 	{"AuxOut Mux", "Mono Mix",			"Mono Mix"},
542 
543 	/* output pga */
544 	{"HPL", NULL,					"Left Headphone"},
545 	{"Left Headphone", NULL,			"Left Headphone Mux"},
546 	{"HPR", NULL,					"Right Headphone"},
547 	{"Right Headphone", NULL,			"Right Headphone Mux"},
548 	{"Aux Out", NULL,				"AuxOut Mux"},
549 
550 	/* input pga */
551 	{"Left LineIn", NULL,				"LINEINL"},
552 	{"Right LineIn", NULL,				"LINEINR"},
553 	{"Phone", NULL,				"PHONEP"},
554 	{"MIC1 Pre Amp", NULL,				"MIC1"},
555 	{"MIC2 Pre Amp", NULL,				"MIC2"},
556 	{"MIC1 PGA", NULL,				"MIC1 Pre Amp"},
557 	{"MIC2 PGA", NULL,				"MIC2 Pre Amp"},
558 
559 	/* left ADC */
560 	{"Left ADC", NULL,				"Left Capture Mix"},
561 	{"DMICL Mix", "DMICL2ADC Capture Switch", "DMICDAT"},
562 	{"Left ADC", NULL,				"DMICL Mix"},
563 	{"ADCLR", NULL,					"Left ADC"},
564 
565 	/* right ADC */
566 	{"Right ADC", NULL, "Right Capture Mix"},
567 	{"DMICR Mix", "DMICR2ADC Capture Switch", "DMICDAT"},
568 	{"Right ADC", NULL, "DMICR Mix"},
569 	{"ADCR Mux", "Stereo ADC", "Right ADC"},
570 	{"ADCR Mux", "Voice ADC", "Right ADC"},
571 	{"ADCLR", NULL, "ADCR Mux"},
572 	{"VAIFTX", NULL, "ADCR Mux"},
573 
574 	/* Digital I2S out */
575 	{"I2SOut Mux", "ADC LR", "ADCLR"},
576 	{"I2SOut Mux", "Voice Stereo Digital", "VAIFRX"},
577 	{"AIFTXL", NULL, "I2SOut Mux"},
578 	{"AIFTXR", NULL, "I2SOut Mux"},
579 
580 	/* Voice Mix */
581 	{"Voice DAC", NULL, "VAIFRX"},
582 	{"Voice Mix", NULL, "Voice DAC"},
583 
584 	/* Speaker Output */
585 	{"SpeakerOut N Mux", "RN/-R",			"Left Speaker"},
586 	{"SpeakerOut N Mux", "RP/+R",			"Left Speaker"},
587 	{"SpeakerOut N Mux", "LN/-R",			"Left Speaker"},
588 	{"SpeakerOut N Mux", "Mute",			"Vmid"},
589 
590 	{"SpeakerOut N Mux", "RN/-R",			"Right Speaker"},
591 	{"SpeakerOut N Mux", "RP/+R",			"Right Speaker"},
592 	{"SpeakerOut N Mux", "LN/-R",			"Right Speaker"},
593 	{"SpeakerOut N Mux", "Mute",			"Vmid"},
594 
595 	{"AB Amp", NULL,				"SpeakerOut Mux"},
596 	{"D Amp", NULL,					"SpeakerOut Mux"},
597 	{"AB-D Amp Mux", "AB Amp",			"AB Amp"},
598 	{"AB-D Amp Mux", "D Amp",			"D Amp"},
599 	{"Left Speaker", NULL,				"AB-D Amp Mux"},
600 	{"Right Speaker", NULL,				"AB-D Amp Mux"},
601 
602 	{"SPKOUT", NULL,				"Left Speaker"},
603 	{"SPKOUT", NULL,				"Right Speaker"},
604 
605 	{"SPKOUTN", NULL,				"SpeakerOut N Mux"},
606 
607 };
608 
609 /* PLL divisors */
610 struct _pll_div {
611 	u32 pll_in;
612 	u32 pll_out;
613 	u16 regvalue;
614 };
615 
616 /* Note : pll code from original alc5632 driver. Not sure of how good it is */
617 /* usefull only for master mode */
618 static const struct _pll_div codec_master_pll_div[] = {
619 
620 	{  2048000,  8192000,	0x0ea0},
621 	{  3686400,  8192000,	0x4e27},
622 	{ 12000000,  8192000,	0x456b},
623 	{ 13000000,  8192000,	0x495f},
624 	{ 13100000,  8192000,	0x0320},
625 	{  2048000,  11289600,	0xf637},
626 	{  3686400,  11289600,	0x2f22},
627 	{ 12000000,  11289600,	0x3e2f},
628 	{ 13000000,  11289600,	0x4d5b},
629 	{ 13100000,  11289600,	0x363b},
630 	{  2048000,  16384000,	0x1ea0},
631 	{  3686400,  16384000,	0x9e27},
632 	{ 12000000,  16384000,	0x452b},
633 	{ 13000000,  16384000,	0x542f},
634 	{ 13100000,  16384000,	0x03a0},
635 	{  2048000,  16934400,	0xe625},
636 	{  3686400,  16934400,	0x9126},
637 	{ 12000000,  16934400,	0x4d2c},
638 	{ 13000000,  16934400,	0x742f},
639 	{ 13100000,  16934400,	0x3c27},
640 	{  2048000,  22579200,	0x2aa0},
641 	{  3686400,  22579200,	0x2f20},
642 	{ 12000000,  22579200,	0x7e2f},
643 	{ 13000000,  22579200,	0x742f},
644 	{ 13100000,  22579200,	0x3c27},
645 	{  2048000,  24576000,	0x2ea0},
646 	{  3686400,  24576000,	0xee27},
647 	{ 12000000,  24576000,	0x2915},
648 	{ 13000000,  24576000,	0x772e},
649 	{ 13100000,  24576000,	0x0d20},
650 };
651 
652 /* FOUT = MCLK*(N+2)/((M+2)*(K+2))
653    N: bit 15:8 (div 2 .. div 257)
654    K: bit  6:4 typical 2
655    M: bit  3:0 (div 2 .. div 17)
656 
657    same as for 5623 - thanks!
658 */
659 
660 static const struct _pll_div codec_slave_pll_div[] = {
661 
662 	{  1024000,  16384000,  0x3ea0},
663 	{  1411200,  22579200,	0x3ea0},
664 	{  1536000,  24576000,	0x3ea0},
665 	{  2048000,  16384000,  0x1ea0},
666 	{  2822400,  22579200,	0x1ea0},
667 	{  3072000,  24576000,	0x1ea0},
668 
669 };
670 
671 static int alc5632_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id,
672 		int source, unsigned int freq_in, unsigned int freq_out)
673 {
674 	int i;
675 	struct snd_soc_codec *codec = codec_dai->codec;
676 	int gbl_clk = 0, pll_div = 0;
677 	u16 reg;
678 
679 	if (pll_id < ALC5632_PLL_FR_MCLK || pll_id > ALC5632_PLL_FR_VBCLK)
680 		return -EINVAL;
681 
682 	/* Disable PLL power */
683 	snd_soc_update_bits(codec, ALC5632_PWR_MANAG_ADD2,
684 				ALC5632_PWR_ADD2_PLL1,
685 				0);
686 	snd_soc_update_bits(codec, ALC5632_PWR_MANAG_ADD2,
687 				ALC5632_PWR_ADD2_PLL2,
688 				0);
689 
690 	/* pll is not used in slave mode */
691 	reg = snd_soc_read(codec, ALC5632_DAI_CONTROL);
692 	if (reg & ALC5632_DAI_SDP_SLAVE_MODE)
693 		return 0;
694 
695 	if (!freq_in || !freq_out)
696 		return 0;
697 
698 	switch (pll_id) {
699 	case ALC5632_PLL_FR_MCLK:
700 		for (i = 0; i < ARRAY_SIZE(codec_master_pll_div); i++) {
701 			if (codec_master_pll_div[i].pll_in == freq_in
702 			   && codec_master_pll_div[i].pll_out == freq_out) {
703 				/* PLL source from MCLK */
704 				pll_div  = codec_master_pll_div[i].regvalue;
705 				break;
706 			}
707 		}
708 		break;
709 	case ALC5632_PLL_FR_BCLK:
710 		for (i = 0; i < ARRAY_SIZE(codec_slave_pll_div); i++) {
711 			if (codec_slave_pll_div[i].pll_in == freq_in
712 			   && codec_slave_pll_div[i].pll_out == freq_out) {
713 				/* PLL source from Bitclk */
714 				gbl_clk = ALC5632_PLL_FR_BCLK;
715 				pll_div = codec_slave_pll_div[i].regvalue;
716 				break;
717 			}
718 		}
719 		break;
720 	case ALC5632_PLL_FR_VBCLK:
721 		for (i = 0; i < ARRAY_SIZE(codec_slave_pll_div); i++) {
722 			if (codec_slave_pll_div[i].pll_in == freq_in
723 			   && codec_slave_pll_div[i].pll_out == freq_out) {
724 				/* PLL source from voice clock */
725 				gbl_clk = ALC5632_PLL_FR_VBCLK;
726 				pll_div = codec_slave_pll_div[i].regvalue;
727 				break;
728 			}
729 		}
730 		break;
731 	default:
732 		return -EINVAL;
733 	}
734 
735 	if (!pll_div)
736 		return -EINVAL;
737 
738 	/* choose MCLK/BCLK/VBCLK */
739 	snd_soc_write(codec, ALC5632_GPCR2, gbl_clk);
740 	/* choose PLL1 clock rate */
741 	snd_soc_write(codec, ALC5632_PLL1_CTRL, pll_div);
742 	/* enable PLL1 */
743 	snd_soc_update_bits(codec, ALC5632_PWR_MANAG_ADD2,
744 				ALC5632_PWR_ADD2_PLL1,
745 				ALC5632_PWR_ADD2_PLL1);
746 	/* enable PLL2 */
747 	snd_soc_update_bits(codec, ALC5632_PWR_MANAG_ADD2,
748 				ALC5632_PWR_ADD2_PLL2,
749 				ALC5632_PWR_ADD2_PLL2);
750 	/* use PLL1 as main SYSCLK */
751 	snd_soc_update_bits(codec, ALC5632_GPCR1,
752 			ALC5632_GPCR1_CLK_SYS_SRC_SEL_PLL1,
753 			ALC5632_GPCR1_CLK_SYS_SRC_SEL_PLL1);
754 
755 	return 0;
756 }
757 
758 struct _coeff_div {
759 	u16 fs;
760 	u16 regvalue;
761 };
762 
763 /* codec hifi mclk (after PLL) clock divider coefficients */
764 /* values inspired from column BCLK=32Fs of Appendix A table */
765 static const struct _coeff_div coeff_div[] = {
766 	{512*1, 0x3075},
767 };
768 
769 static int get_coeff(struct snd_soc_codec *codec, int rate)
770 {
771 	struct alc5632_priv *alc5632 = snd_soc_codec_get_drvdata(codec);
772 	int i;
773 
774 	for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
775 		if (coeff_div[i].fs * rate == alc5632->sysclk)
776 			return i;
777 	}
778 	return -EINVAL;
779 }
780 
781 /*
782  * Clock after PLL and dividers
783  */
784 static int alc5632_set_dai_sysclk(struct snd_soc_dai *codec_dai,
785 		int clk_id, unsigned int freq, int dir)
786 {
787 	struct snd_soc_codec *codec = codec_dai->codec;
788 	struct alc5632_priv *alc5632 = snd_soc_codec_get_drvdata(codec);
789 
790 	switch (freq) {
791 	case  4096000:
792 	case  8192000:
793 	case 11289600:
794 	case 12288000:
795 	case 16384000:
796 	case 16934400:
797 	case 18432000:
798 	case 22579200:
799 	case 24576000:
800 		alc5632->sysclk = freq;
801 		return 0;
802 	}
803 	return -EINVAL;
804 }
805 
806 static int alc5632_set_dai_fmt(struct snd_soc_dai *codec_dai,
807 		unsigned int fmt)
808 {
809 	struct snd_soc_codec *codec = codec_dai->codec;
810 	u16 iface = 0;
811 
812 	/* set master/slave audio interface */
813 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
814 	case SND_SOC_DAIFMT_CBM_CFM:
815 		iface = ALC5632_DAI_SDP_MASTER_MODE;
816 		break;
817 	case SND_SOC_DAIFMT_CBS_CFS:
818 		iface = ALC5632_DAI_SDP_SLAVE_MODE;
819 		break;
820 	default:
821 		return -EINVAL;
822 	}
823 
824 	/* interface format */
825 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
826 	case SND_SOC_DAIFMT_I2S:
827 		iface |= ALC5632_DAI_I2S_DF_I2S;
828 		break;
829 	case SND_SOC_DAIFMT_LEFT_J:
830 		iface |= ALC5632_DAI_I2S_DF_LEFT;
831 		break;
832 	case SND_SOC_DAIFMT_DSP_A:
833 		iface |= ALC5632_DAI_I2S_DF_PCM_A;
834 		break;
835 	case SND_SOC_DAIFMT_DSP_B:
836 		iface |= ALC5632_DAI_I2S_DF_PCM_B;
837 		break;
838 	default:
839 		return -EINVAL;
840 	}
841 
842 	/* clock inversion */
843 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
844 	case SND_SOC_DAIFMT_NB_NF:
845 		break;
846 	case SND_SOC_DAIFMT_IB_IF:
847 		iface |= ALC5632_DAI_MAIN_I2S_BCLK_POL_CTRL;
848 		break;
849 	case SND_SOC_DAIFMT_IB_NF:
850 		iface |= ALC5632_DAI_MAIN_I2S_BCLK_POL_CTRL;
851 		break;
852 	case SND_SOC_DAIFMT_NB_IF:
853 		break;
854 	default:
855 		return -EINVAL;
856 	}
857 
858 	return snd_soc_write(codec, ALC5632_DAI_CONTROL, iface);
859 }
860 
861 static int alc5632_pcm_hw_params(struct snd_pcm_substream *substream,
862 		struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
863 {
864 	struct snd_soc_codec *codec = dai->codec;
865 	int coeff, rate;
866 	u16 iface;
867 
868 	iface = snd_soc_read(codec, ALC5632_DAI_CONTROL);
869 	iface &= ~ALC5632_DAI_I2S_DL_MASK;
870 
871 	/* bit size */
872 	switch (params_format(params)) {
873 	case SNDRV_PCM_FORMAT_S16_LE:
874 		iface |= ALC5632_DAI_I2S_DL_16;
875 		break;
876 	case SNDRV_PCM_FORMAT_S20_3LE:
877 		iface |= ALC5632_DAI_I2S_DL_20;
878 		break;
879 	case SNDRV_PCM_FORMAT_S24_LE:
880 		iface |= ALC5632_DAI_I2S_DL_24;
881 		break;
882 	default:
883 		return -EINVAL;
884 	}
885 
886 	/* set iface & srate */
887 	snd_soc_write(codec, ALC5632_DAI_CONTROL, iface);
888 	rate = params_rate(params);
889 	coeff = get_coeff(codec, rate);
890 	if (coeff < 0)
891 		return -EINVAL;
892 
893 	coeff = coeff_div[coeff].regvalue;
894 	snd_soc_write(codec, ALC5632_DAC_CLK_CTRL1, coeff);
895 
896 	return 0;
897 }
898 
899 static int alc5632_mute(struct snd_soc_dai *dai, int mute)
900 {
901 	struct snd_soc_codec *codec = dai->codec;
902 	u16 hp_mute = ALC5632_MISC_HP_DEPOP_MUTE_L
903 						|ALC5632_MISC_HP_DEPOP_MUTE_R;
904 	u16 mute_reg = snd_soc_read(codec, ALC5632_MISC_CTRL) & ~hp_mute;
905 
906 	if (mute)
907 		mute_reg |= hp_mute;
908 
909 	return snd_soc_write(codec, ALC5632_MISC_CTRL, mute_reg);
910 }
911 
912 #define ALC5632_ADD2_POWER_EN (ALC5632_PWR_ADD2_VREF)
913 
914 #define ALC5632_ADD3_POWER_EN (ALC5632_PWR_ADD3_MIC1_BOOST_AD)
915 
916 #define ALC5632_ADD1_POWER_EN \
917 		(ALC5632_PWR_ADD1_DAC_REF \
918 		| ALC5632_PWR_ADD1_SOFTGEN_EN \
919 		| ALC5632_PWR_ADD1_HP_OUT_AMP \
920 		| ALC5632_PWR_ADD1_HP_OUT_ENH_AMP \
921 		| ALC5632_PWR_ADD1_MAIN_BIAS)
922 
923 static void enable_power_depop(struct snd_soc_codec *codec)
924 {
925 	snd_soc_update_bits(codec, ALC5632_PWR_MANAG_ADD1,
926 				ALC5632_PWR_ADD1_SOFTGEN_EN,
927 				ALC5632_PWR_ADD1_SOFTGEN_EN);
928 
929 	snd_soc_update_bits(codec, ALC5632_PWR_MANAG_ADD3,
930 				ALC5632_ADD3_POWER_EN,
931 				ALC5632_ADD3_POWER_EN);
932 
933 	snd_soc_update_bits(codec, ALC5632_MISC_CTRL,
934 				ALC5632_MISC_HP_DEPOP_MODE2_EN,
935 				ALC5632_MISC_HP_DEPOP_MODE2_EN);
936 
937 	/* "normal" mode: 0 @ 26 */
938 	/* set all PR0-7 mixers to 0 */
939 	snd_soc_update_bits(codec, ALC5632_PWR_DOWN_CTRL_STATUS,
940 				ALC5632_PWR_DOWN_CTRL_STATUS_MASK,
941 				0);
942 
943 	msleep(500);
944 
945 	snd_soc_update_bits(codec, ALC5632_PWR_MANAG_ADD2,
946 				ALC5632_ADD2_POWER_EN,
947 				ALC5632_ADD2_POWER_EN);
948 
949 	snd_soc_update_bits(codec, ALC5632_PWR_MANAG_ADD1,
950 				ALC5632_ADD1_POWER_EN,
951 				ALC5632_ADD1_POWER_EN);
952 
953 	/* disable HP Depop2 */
954 	snd_soc_update_bits(codec, ALC5632_MISC_CTRL,
955 				ALC5632_MISC_HP_DEPOP_MODE2_EN,
956 				0);
957 
958 }
959 
960 static int alc5632_set_bias_level(struct snd_soc_codec *codec,
961 				      enum snd_soc_bias_level level)
962 {
963 	switch (level) {
964 	case SND_SOC_BIAS_ON:
965 		enable_power_depop(codec);
966 		break;
967 	case SND_SOC_BIAS_PREPARE:
968 		break;
969 	case SND_SOC_BIAS_STANDBY:
970 		/* everything off except vref/vmid, */
971 		snd_soc_update_bits(codec, ALC5632_PWR_MANAG_ADD1,
972 				ALC5632_PWR_MANAG_ADD1_MASK,
973 				ALC5632_PWR_ADD1_MAIN_BIAS);
974 		snd_soc_update_bits(codec, ALC5632_PWR_MANAG_ADD2,
975 				ALC5632_PWR_MANAG_ADD2_MASK,
976 				ALC5632_PWR_ADD2_VREF);
977 		/* "normal" mode: 0 @ 26 */
978 		snd_soc_update_bits(codec, ALC5632_PWR_DOWN_CTRL_STATUS,
979 				ALC5632_PWR_DOWN_CTRL_STATUS_MASK,
980 				0xffff ^ (ALC5632_PWR_VREF_PR3
981 				| ALC5632_PWR_VREF_PR2));
982 		break;
983 	case SND_SOC_BIAS_OFF:
984 		/* everything off, dac mute, inactive */
985 		snd_soc_update_bits(codec, ALC5632_PWR_MANAG_ADD2,
986 				ALC5632_PWR_MANAG_ADD2_MASK, 0);
987 		snd_soc_update_bits(codec, ALC5632_PWR_MANAG_ADD3,
988 				ALC5632_PWR_MANAG_ADD3_MASK, 0);
989 		snd_soc_update_bits(codec, ALC5632_PWR_MANAG_ADD1,
990 				ALC5632_PWR_MANAG_ADD1_MASK, 0);
991 		break;
992 	}
993 	codec->dapm.bias_level = level;
994 	return 0;
995 }
996 
997 #define ALC5632_FORMATS	(SNDRV_PCM_FMTBIT_S16_LE \
998 			| SNDRV_PCM_FMTBIT_S24_LE \
999 			| SNDRV_PCM_FMTBIT_S32_LE)
1000 
1001 static const struct snd_soc_dai_ops alc5632_dai_ops = {
1002 		.hw_params = alc5632_pcm_hw_params,
1003 		.digital_mute = alc5632_mute,
1004 		.set_fmt = alc5632_set_dai_fmt,
1005 		.set_sysclk = alc5632_set_dai_sysclk,
1006 		.set_pll = alc5632_set_dai_pll,
1007 };
1008 
1009 static struct snd_soc_dai_driver alc5632_dai = {
1010 	.name = "alc5632-hifi",
1011 	.playback = {
1012 		.stream_name = "HiFi Playback",
1013 		.channels_min = 1,
1014 		.channels_max = 2,
1015 		.rate_min =	8000,
1016 		.rate_max =	48000,
1017 		.rates = SNDRV_PCM_RATE_8000_48000,
1018 		.formats = ALC5632_FORMATS,},
1019 	.capture = {
1020 		.stream_name = "HiFi Capture",
1021 		.channels_min = 1,
1022 		.channels_max = 2,
1023 		.rate_min =	8000,
1024 		.rate_max =	48000,
1025 		.rates = SNDRV_PCM_RATE_8000_48000,
1026 		.formats = ALC5632_FORMATS,},
1027 
1028 	.ops = &alc5632_dai_ops,
1029 	.symmetric_rates = 1,
1030 };
1031 
1032 #ifdef CONFIG_PM
1033 static int alc5632_suspend(struct snd_soc_codec *codec)
1034 {
1035 	alc5632_set_bias_level(codec, SND_SOC_BIAS_OFF);
1036 	return 0;
1037 }
1038 
1039 static int alc5632_resume(struct snd_soc_codec *codec)
1040 {
1041 	struct alc5632_priv *alc5632 = snd_soc_codec_get_drvdata(codec);
1042 
1043 	regcache_sync(alc5632->regmap);
1044 
1045 	alc5632_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1046 	return 0;
1047 }
1048 #else
1049 #define	alc5632_suspend	NULL
1050 #define	alc5632_resume	NULL
1051 #endif
1052 
1053 static int alc5632_probe(struct snd_soc_codec *codec)
1054 {
1055 	struct alc5632_priv *alc5632 = snd_soc_codec_get_drvdata(codec);
1056 	int ret;
1057 
1058 	codec->control_data = alc5632->regmap;
1059 
1060 	ret = snd_soc_codec_set_cache_io(codec, 8, 16, SND_SOC_REGMAP);
1061 	if (ret != 0) {
1062 		dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
1063 		return ret;
1064 	}
1065 
1066 	/* power on device  */
1067 	alc5632_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1068 
1069 	switch (alc5632->id) {
1070 	case 0x5c:
1071 		snd_soc_add_codec_controls(codec, alc5632_vol_snd_controls,
1072 			ARRAY_SIZE(alc5632_vol_snd_controls));
1073 		break;
1074 	default:
1075 		return -EINVAL;
1076 	}
1077 
1078 	return ret;
1079 }
1080 
1081 /* power down chip */
1082 static int alc5632_remove(struct snd_soc_codec *codec)
1083 {
1084 	alc5632_set_bias_level(codec, SND_SOC_BIAS_OFF);
1085 	return 0;
1086 }
1087 
1088 static struct snd_soc_codec_driver soc_codec_device_alc5632 = {
1089 	.probe = alc5632_probe,
1090 	.remove = alc5632_remove,
1091 	.suspend = alc5632_suspend,
1092 	.resume = alc5632_resume,
1093 	.set_bias_level = alc5632_set_bias_level,
1094 	.controls = alc5632_snd_controls,
1095 	.num_controls = ARRAY_SIZE(alc5632_snd_controls),
1096 	.dapm_widgets = alc5632_dapm_widgets,
1097 	.num_dapm_widgets = ARRAY_SIZE(alc5632_dapm_widgets),
1098 	.dapm_routes = alc5632_dapm_routes,
1099 	.num_dapm_routes = ARRAY_SIZE(alc5632_dapm_routes),
1100 };
1101 
1102 static struct regmap_config alc5632_regmap = {
1103 	.reg_bits = 8,
1104 	.val_bits = 16,
1105 
1106 	.max_register = ALC5632_MAX_REGISTER,
1107 	.reg_defaults = alc5632_reg_defaults,
1108 	.num_reg_defaults = ARRAY_SIZE(alc5632_reg_defaults),
1109 	.volatile_reg = alc5632_volatile_register,
1110 	.cache_type = REGCACHE_RBTREE,
1111 };
1112 
1113 /*
1114  * alc5632 2 wire address is determined by A1 pin
1115  * state during powerup.
1116  *    low  = 0x1a
1117  *    high = 0x1b
1118  */
1119 static __devinit int alc5632_i2c_probe(struct i2c_client *client,
1120 				const struct i2c_device_id *id)
1121 {
1122 	struct alc5632_priv *alc5632;
1123 	int ret, ret1, ret2;
1124 	unsigned int vid1, vid2;
1125 
1126 	alc5632 = devm_kzalloc(&client->dev,
1127 			 sizeof(struct alc5632_priv), GFP_KERNEL);
1128 	if (alc5632 == NULL)
1129 		return -ENOMEM;
1130 
1131 	i2c_set_clientdata(client, alc5632);
1132 
1133 	alc5632->regmap = regmap_init_i2c(client, &alc5632_regmap);
1134 	if (IS_ERR(alc5632->regmap)) {
1135 		ret = PTR_ERR(alc5632->regmap);
1136 		dev_err(&client->dev, "regmap_init() failed: %d\n", ret);
1137 		return ret;
1138 	}
1139 
1140 	ret1 = regmap_read(alc5632->regmap, ALC5632_VENDOR_ID1, &vid1);
1141 	ret2 = regmap_read(alc5632->regmap, ALC5632_VENDOR_ID2, &vid2);
1142 	if (ret1 != 0 || ret2 != 0) {
1143 		dev_err(&client->dev,
1144 		"Failed to read chip ID: ret1=%d, ret2=%d\n", ret1, ret2);
1145 		regmap_exit(alc5632->regmap);
1146 		return -EIO;
1147 	}
1148 
1149 	vid2 >>= 8;
1150 
1151 	if ((vid1 != 0x10EC) || (vid2 != id->driver_data)) {
1152 		dev_err(&client->dev,
1153 		"Device is not a ALC5632: VID1=0x%x, VID2=0x%x\n", vid1, vid2);
1154 		regmap_exit(alc5632->regmap);
1155 		return -EINVAL;
1156 	}
1157 
1158 	ret = alc5632_reset(alc5632->regmap);
1159 	if (ret < 0) {
1160 		dev_err(&client->dev, "Failed to issue reset\n");
1161 		regmap_exit(alc5632->regmap);
1162 		return ret;
1163 	}
1164 
1165 	alc5632->id = vid2;
1166 	switch (alc5632->id) {
1167 	case 0x5c:
1168 		alc5632_dai.name = "alc5632-hifi";
1169 		break;
1170 	default:
1171 		return -EINVAL;
1172 	}
1173 
1174 	ret = snd_soc_register_codec(&client->dev,
1175 		&soc_codec_device_alc5632, &alc5632_dai, 1);
1176 
1177 	if (ret < 0) {
1178 		dev_err(&client->dev, "Failed to register codec: %d\n", ret);
1179 		regmap_exit(alc5632->regmap);
1180 		return ret;
1181 	}
1182 
1183 	return ret;
1184 }
1185 
1186 static __devexit int alc5632_i2c_remove(struct i2c_client *client)
1187 {
1188 	struct alc5632_priv *alc5632 = i2c_get_clientdata(client);
1189 	snd_soc_unregister_codec(&client->dev);
1190 	regmap_exit(alc5632->regmap);
1191 	return 0;
1192 }
1193 
1194 static const struct i2c_device_id alc5632_i2c_table[] = {
1195 	{"alc5632", 0x5c},
1196 	{}
1197 };
1198 MODULE_DEVICE_TABLE(i2c, alc5632_i2c_table);
1199 
1200 /* i2c codec control layer */
1201 static struct i2c_driver alc5632_i2c_driver = {
1202 	.driver = {
1203 		.name = "alc5632",
1204 		.owner = THIS_MODULE,
1205 	},
1206 	.probe = alc5632_i2c_probe,
1207 	.remove =  __devexit_p(alc5632_i2c_remove),
1208 	.id_table = alc5632_i2c_table,
1209 };
1210 
1211 static int __init alc5632_modinit(void)
1212 {
1213 	int ret;
1214 
1215 	ret = i2c_add_driver(&alc5632_i2c_driver);
1216 	if (ret != 0) {
1217 		printk(KERN_ERR "%s: can't add i2c driver", __func__);
1218 		return ret;
1219 	}
1220 
1221 	return ret;
1222 }
1223 module_init(alc5632_modinit);
1224 
1225 static void __exit alc5632_modexit(void)
1226 {
1227 	i2c_del_driver(&alc5632_i2c_driver);
1228 }
1229 module_exit(alc5632_modexit);
1230 
1231 MODULE_DESCRIPTION("ASoC ALC5632 driver");
1232 MODULE_AUTHOR("Leon Romanovsky <leon@leon.nu>");
1233 MODULE_LICENSE("GPL");
1234