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