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