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