2 * alc5623.c -- alc562[123] ALSA Soc Audio driver
4 * Copyright 2008 Realtek Microelectronics
5 * Author: flove <flove@realtek.com> Ethan <eku@marvell.com>
7 * Copyright 2010 Arnaud Patard <arnaud.patard@rtp-net.org>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/init.h>
21 #include <linux/delay.h>
23 #include <linux/i2c.h>
24 #include <linux/regmap.h>
25 #include <linux/slab.h>
27 #include <sound/core.h>
28 #include <sound/pcm.h>
29 #include <sound/pcm_params.h>
30 #include <sound/tlv.h>
31 #include <sound/soc.h>
32 #include <sound/initval.h>
33 #include <sound/alc5623.h>
37 static int caps_charge = 2000;
38 module_param(caps_charge, int, 0);
39 MODULE_PARM_DESC(caps_charge, "ALC5623 cap charge time (msecs)");
41 /* codec private data */
43 struct regmap *regmap;
46 unsigned int add_ctrl;
47 unsigned int jack_det_ctrl;
50 static inline int alc5623_reset(struct snd_soc_codec *codec)
52 return snd_soc_write(codec, ALC5623_RESET, 0);
55 static int amp_mixer_event(struct snd_soc_dapm_widget *w,
56 struct snd_kcontrol *kcontrol, int event)
58 /* to power-on/off class-d amp generators/speaker */
59 /* need to write to 'index-46h' register : */
60 /* so write index num (here 0x46) to reg 0x6a */
61 /* and then 0xffff/0 to reg 0x6c */
62 snd_soc_write(w->codec, ALC5623_HID_CTRL_INDEX, 0x46);
65 case SND_SOC_DAPM_PRE_PMU:
66 snd_soc_write(w->codec, ALC5623_HID_CTRL_DATA, 0xFFFF);
68 case SND_SOC_DAPM_POST_PMD:
69 snd_soc_write(w->codec, ALC5623_HID_CTRL_DATA, 0);
80 static const DECLARE_TLV_DB_SCALE(vol_tlv, -3450, 150, 0);
81 static const DECLARE_TLV_DB_SCALE(hp_tlv, -4650, 150, 0);
82 static const DECLARE_TLV_DB_SCALE(adc_rec_tlv, -1650, 150, 0);
83 static const unsigned int boost_tlv[] = {
85 0, 0, TLV_DB_SCALE_ITEM(0, 0, 0),
86 1, 1, TLV_DB_SCALE_ITEM(2000, 0, 0),
87 2, 2, TLV_DB_SCALE_ITEM(3000, 0, 0),
89 static const DECLARE_TLV_DB_SCALE(dig_tlv, 0, 600, 0);
91 static const struct snd_kcontrol_new alc5621_vol_snd_controls[] = {
92 SOC_DOUBLE_TLV("Speaker Playback Volume",
93 ALC5623_SPK_OUT_VOL, 8, 0, 31, 1, hp_tlv),
94 SOC_DOUBLE("Speaker Playback Switch",
95 ALC5623_SPK_OUT_VOL, 15, 7, 1, 1),
96 SOC_DOUBLE_TLV("Headphone Playback Volume",
97 ALC5623_HP_OUT_VOL, 8, 0, 31, 1, hp_tlv),
98 SOC_DOUBLE("Headphone Playback Switch",
99 ALC5623_HP_OUT_VOL, 15, 7, 1, 1),
102 static const struct snd_kcontrol_new alc5622_vol_snd_controls[] = {
103 SOC_DOUBLE_TLV("Speaker Playback Volume",
104 ALC5623_SPK_OUT_VOL, 8, 0, 31, 1, hp_tlv),
105 SOC_DOUBLE("Speaker Playback Switch",
106 ALC5623_SPK_OUT_VOL, 15, 7, 1, 1),
107 SOC_DOUBLE_TLV("Line Playback Volume",
108 ALC5623_HP_OUT_VOL, 8, 0, 31, 1, hp_tlv),
109 SOC_DOUBLE("Line Playback Switch",
110 ALC5623_HP_OUT_VOL, 15, 7, 1, 1),
113 static const struct snd_kcontrol_new alc5623_vol_snd_controls[] = {
114 SOC_DOUBLE_TLV("Line Playback Volume",
115 ALC5623_SPK_OUT_VOL, 8, 0, 31, 1, hp_tlv),
116 SOC_DOUBLE("Line Playback Switch",
117 ALC5623_SPK_OUT_VOL, 15, 7, 1, 1),
118 SOC_DOUBLE_TLV("Headphone Playback Volume",
119 ALC5623_HP_OUT_VOL, 8, 0, 31, 1, hp_tlv),
120 SOC_DOUBLE("Headphone Playback Switch",
121 ALC5623_HP_OUT_VOL, 15, 7, 1, 1),
124 static const struct snd_kcontrol_new alc5623_snd_controls[] = {
125 SOC_DOUBLE_TLV("Auxout Playback Volume",
126 ALC5623_MONO_AUX_OUT_VOL, 8, 0, 31, 1, hp_tlv),
127 SOC_DOUBLE("Auxout Playback Switch",
128 ALC5623_MONO_AUX_OUT_VOL, 15, 7, 1, 1),
129 SOC_DOUBLE_TLV("PCM Playback Volume",
130 ALC5623_STEREO_DAC_VOL, 8, 0, 31, 1, vol_tlv),
131 SOC_DOUBLE_TLV("AuxI Capture Volume",
132 ALC5623_AUXIN_VOL, 8, 0, 31, 1, vol_tlv),
133 SOC_DOUBLE_TLV("LineIn Capture Volume",
134 ALC5623_LINE_IN_VOL, 8, 0, 31, 1, vol_tlv),
135 SOC_SINGLE_TLV("Mic1 Capture Volume",
136 ALC5623_MIC_VOL, 8, 31, 1, vol_tlv),
137 SOC_SINGLE_TLV("Mic2 Capture Volume",
138 ALC5623_MIC_VOL, 0, 31, 1, vol_tlv),
139 SOC_DOUBLE_TLV("Rec Capture Volume",
140 ALC5623_ADC_REC_GAIN, 7, 0, 31, 0, adc_rec_tlv),
141 SOC_SINGLE_TLV("Mic 1 Boost Volume",
142 ALC5623_MIC_CTRL, 10, 2, 0, boost_tlv),
143 SOC_SINGLE_TLV("Mic 2 Boost Volume",
144 ALC5623_MIC_CTRL, 8, 2, 0, boost_tlv),
145 SOC_SINGLE_TLV("Digital Boost Volume",
146 ALC5623_ADD_CTRL_REG, 4, 3, 0, dig_tlv),
152 static const struct snd_kcontrol_new alc5623_hp_mixer_controls[] = {
153 SOC_DAPM_SINGLE("LI2HP Playback Switch", ALC5623_LINE_IN_VOL, 15, 1, 1),
154 SOC_DAPM_SINGLE("AUXI2HP Playback Switch", ALC5623_AUXIN_VOL, 15, 1, 1),
155 SOC_DAPM_SINGLE("MIC12HP Playback Switch", ALC5623_MIC_ROUTING_CTRL, 15, 1, 1),
156 SOC_DAPM_SINGLE("MIC22HP Playback Switch", ALC5623_MIC_ROUTING_CTRL, 7, 1, 1),
157 SOC_DAPM_SINGLE("DAC2HP Playback Switch", ALC5623_STEREO_DAC_VOL, 15, 1, 1),
160 static const struct snd_kcontrol_new alc5623_hpl_mixer_controls[] = {
161 SOC_DAPM_SINGLE("ADC2HP_L Playback Switch", ALC5623_ADC_REC_GAIN, 15, 1, 1),
164 static const struct snd_kcontrol_new alc5623_hpr_mixer_controls[] = {
165 SOC_DAPM_SINGLE("ADC2HP_R Playback Switch", ALC5623_ADC_REC_GAIN, 14, 1, 1),
168 static const struct snd_kcontrol_new alc5623_mono_mixer_controls[] = {
169 SOC_DAPM_SINGLE("ADC2MONO_L Playback Switch", ALC5623_ADC_REC_GAIN, 13, 1, 1),
170 SOC_DAPM_SINGLE("ADC2MONO_R Playback Switch", ALC5623_ADC_REC_GAIN, 12, 1, 1),
171 SOC_DAPM_SINGLE("LI2MONO Playback Switch", ALC5623_LINE_IN_VOL, 13, 1, 1),
172 SOC_DAPM_SINGLE("AUXI2MONO Playback Switch", ALC5623_AUXIN_VOL, 13, 1, 1),
173 SOC_DAPM_SINGLE("MIC12MONO Playback Switch", ALC5623_MIC_ROUTING_CTRL, 13, 1, 1),
174 SOC_DAPM_SINGLE("MIC22MONO Playback Switch", ALC5623_MIC_ROUTING_CTRL, 5, 1, 1),
175 SOC_DAPM_SINGLE("DAC2MONO Playback Switch", ALC5623_STEREO_DAC_VOL, 13, 1, 1),
178 static const struct snd_kcontrol_new alc5623_speaker_mixer_controls[] = {
179 SOC_DAPM_SINGLE("LI2SPK Playback Switch", ALC5623_LINE_IN_VOL, 14, 1, 1),
180 SOC_DAPM_SINGLE("AUXI2SPK Playback Switch", ALC5623_AUXIN_VOL, 14, 1, 1),
181 SOC_DAPM_SINGLE("MIC12SPK Playback Switch", ALC5623_MIC_ROUTING_CTRL, 14, 1, 1),
182 SOC_DAPM_SINGLE("MIC22SPK Playback Switch", ALC5623_MIC_ROUTING_CTRL, 6, 1, 1),
183 SOC_DAPM_SINGLE("DAC2SPK Playback Switch", ALC5623_STEREO_DAC_VOL, 14, 1, 1),
186 /* Left Record Mixer */
187 static const struct snd_kcontrol_new alc5623_captureL_mixer_controls[] = {
188 SOC_DAPM_SINGLE("Mic1 Capture Switch", ALC5623_ADC_REC_MIXER, 14, 1, 1),
189 SOC_DAPM_SINGLE("Mic2 Capture Switch", ALC5623_ADC_REC_MIXER, 13, 1, 1),
190 SOC_DAPM_SINGLE("LineInL Capture Switch", ALC5623_ADC_REC_MIXER, 12, 1, 1),
191 SOC_DAPM_SINGLE("Left AuxI Capture Switch", ALC5623_ADC_REC_MIXER, 11, 1, 1),
192 SOC_DAPM_SINGLE("HPMixerL Capture Switch", ALC5623_ADC_REC_MIXER, 10, 1, 1),
193 SOC_DAPM_SINGLE("SPKMixer Capture Switch", ALC5623_ADC_REC_MIXER, 9, 1, 1),
194 SOC_DAPM_SINGLE("MonoMixer Capture Switch", ALC5623_ADC_REC_MIXER, 8, 1, 1),
197 /* Right Record Mixer */
198 static const struct snd_kcontrol_new alc5623_captureR_mixer_controls[] = {
199 SOC_DAPM_SINGLE("Mic1 Capture Switch", ALC5623_ADC_REC_MIXER, 6, 1, 1),
200 SOC_DAPM_SINGLE("Mic2 Capture Switch", ALC5623_ADC_REC_MIXER, 5, 1, 1),
201 SOC_DAPM_SINGLE("LineInR Capture Switch", ALC5623_ADC_REC_MIXER, 4, 1, 1),
202 SOC_DAPM_SINGLE("Right AuxI Capture Switch", ALC5623_ADC_REC_MIXER, 3, 1, 1),
203 SOC_DAPM_SINGLE("HPMixerR Capture Switch", ALC5623_ADC_REC_MIXER, 2, 1, 1),
204 SOC_DAPM_SINGLE("SPKMixer Capture Switch", ALC5623_ADC_REC_MIXER, 1, 1, 1),
205 SOC_DAPM_SINGLE("MonoMixer Capture Switch", ALC5623_ADC_REC_MIXER, 0, 1, 1),
208 static const char *alc5623_spk_n_sour_sel[] = {
209 "RN/-R", "RP/+R", "LN/-R", "Vmid" };
210 static const char *alc5623_hpl_out_input_sel[] = {
211 "Vmid", "HP Left Mix"};
212 static const char *alc5623_hpr_out_input_sel[] = {
213 "Vmid", "HP Right Mix"};
214 static const char *alc5623_spkout_input_sel[] = {
215 "Vmid", "HPOut Mix", "Speaker Mix", "Mono Mix"};
216 static const char *alc5623_aux_out_input_sel[] = {
217 "Vmid", "HPOut Mix", "Speaker Mix", "Mono Mix"};
219 /* auxout output mux */
220 static SOC_ENUM_SINGLE_DECL(alc5623_aux_out_input_enum,
221 ALC5623_OUTPUT_MIXER_CTRL, 6,
222 alc5623_aux_out_input_sel);
223 static const struct snd_kcontrol_new alc5623_auxout_mux_controls =
224 SOC_DAPM_ENUM("Route", alc5623_aux_out_input_enum);
226 /* speaker output mux */
227 static SOC_ENUM_SINGLE_DECL(alc5623_spkout_input_enum,
228 ALC5623_OUTPUT_MIXER_CTRL, 10,
229 alc5623_spkout_input_sel);
230 static const struct snd_kcontrol_new alc5623_spkout_mux_controls =
231 SOC_DAPM_ENUM("Route", alc5623_spkout_input_enum);
233 /* headphone left output mux */
234 static SOC_ENUM_SINGLE_DECL(alc5623_hpl_out_input_enum,
235 ALC5623_OUTPUT_MIXER_CTRL, 9,
236 alc5623_hpl_out_input_sel);
237 static const struct snd_kcontrol_new alc5623_hpl_out_mux_controls =
238 SOC_DAPM_ENUM("Route", alc5623_hpl_out_input_enum);
240 /* headphone right output mux */
241 static SOC_ENUM_SINGLE_DECL(alc5623_hpr_out_input_enum,
242 ALC5623_OUTPUT_MIXER_CTRL, 8,
243 alc5623_hpr_out_input_sel);
244 static const struct snd_kcontrol_new alc5623_hpr_out_mux_controls =
245 SOC_DAPM_ENUM("Route", alc5623_hpr_out_input_enum);
247 /* speaker output N select */
248 static SOC_ENUM_SINGLE_DECL(alc5623_spk_n_sour_enum,
249 ALC5623_OUTPUT_MIXER_CTRL, 14,
250 alc5623_spk_n_sour_sel);
251 static const struct snd_kcontrol_new alc5623_spkoutn_mux_controls =
252 SOC_DAPM_ENUM("Route", alc5623_spk_n_sour_enum);
254 static const struct snd_soc_dapm_widget alc5623_dapm_widgets[] = {
256 SND_SOC_DAPM_MUX("AuxOut Mux", SND_SOC_NOPM, 0, 0,
257 &alc5623_auxout_mux_controls),
258 SND_SOC_DAPM_MUX("SpeakerOut Mux", SND_SOC_NOPM, 0, 0,
259 &alc5623_spkout_mux_controls),
260 SND_SOC_DAPM_MUX("Left Headphone Mux", SND_SOC_NOPM, 0, 0,
261 &alc5623_hpl_out_mux_controls),
262 SND_SOC_DAPM_MUX("Right Headphone Mux", SND_SOC_NOPM, 0, 0,
263 &alc5623_hpr_out_mux_controls),
264 SND_SOC_DAPM_MUX("SpeakerOut N Mux", SND_SOC_NOPM, 0, 0,
265 &alc5623_spkoutn_mux_controls),
268 SND_SOC_DAPM_MIXER("HP Mix", SND_SOC_NOPM, 0, 0,
269 &alc5623_hp_mixer_controls[0],
270 ARRAY_SIZE(alc5623_hp_mixer_controls)),
271 SND_SOC_DAPM_MIXER("HPR Mix", ALC5623_PWR_MANAG_ADD2, 4, 0,
272 &alc5623_hpr_mixer_controls[0],
273 ARRAY_SIZE(alc5623_hpr_mixer_controls)),
274 SND_SOC_DAPM_MIXER("HPL Mix", ALC5623_PWR_MANAG_ADD2, 5, 0,
275 &alc5623_hpl_mixer_controls[0],
276 ARRAY_SIZE(alc5623_hpl_mixer_controls)),
277 SND_SOC_DAPM_MIXER("HPOut Mix", SND_SOC_NOPM, 0, 0, NULL, 0),
278 SND_SOC_DAPM_MIXER("Mono Mix", ALC5623_PWR_MANAG_ADD2, 2, 0,
279 &alc5623_mono_mixer_controls[0],
280 ARRAY_SIZE(alc5623_mono_mixer_controls)),
281 SND_SOC_DAPM_MIXER("Speaker Mix", ALC5623_PWR_MANAG_ADD2, 3, 0,
282 &alc5623_speaker_mixer_controls[0],
283 ARRAY_SIZE(alc5623_speaker_mixer_controls)),
286 SND_SOC_DAPM_MIXER("Left Capture Mix", ALC5623_PWR_MANAG_ADD2, 1, 0,
287 &alc5623_captureL_mixer_controls[0],
288 ARRAY_SIZE(alc5623_captureL_mixer_controls)),
289 SND_SOC_DAPM_MIXER("Right Capture Mix", ALC5623_PWR_MANAG_ADD2, 0, 0,
290 &alc5623_captureR_mixer_controls[0],
291 ARRAY_SIZE(alc5623_captureR_mixer_controls)),
293 SND_SOC_DAPM_DAC("Left DAC", "Left HiFi Playback",
294 ALC5623_PWR_MANAG_ADD2, 9, 0),
295 SND_SOC_DAPM_DAC("Right DAC", "Right HiFi Playback",
296 ALC5623_PWR_MANAG_ADD2, 8, 0),
297 SND_SOC_DAPM_MIXER("I2S Mix", ALC5623_PWR_MANAG_ADD1, 15, 0, NULL, 0),
298 SND_SOC_DAPM_MIXER("AuxI Mix", SND_SOC_NOPM, 0, 0, NULL, 0),
299 SND_SOC_DAPM_MIXER("Line Mix", SND_SOC_NOPM, 0, 0, NULL, 0),
300 SND_SOC_DAPM_ADC("Left ADC", "Left HiFi Capture",
301 ALC5623_PWR_MANAG_ADD2, 7, 0),
302 SND_SOC_DAPM_ADC("Right ADC", "Right HiFi Capture",
303 ALC5623_PWR_MANAG_ADD2, 6, 0),
304 SND_SOC_DAPM_PGA("Left Headphone", ALC5623_PWR_MANAG_ADD3, 10, 0, NULL, 0),
305 SND_SOC_DAPM_PGA("Right Headphone", ALC5623_PWR_MANAG_ADD3, 9, 0, NULL, 0),
306 SND_SOC_DAPM_PGA("SpeakerOut", ALC5623_PWR_MANAG_ADD3, 12, 0, NULL, 0),
307 SND_SOC_DAPM_PGA("Left AuxOut", ALC5623_PWR_MANAG_ADD3, 14, 0, NULL, 0),
308 SND_SOC_DAPM_PGA("Right AuxOut", ALC5623_PWR_MANAG_ADD3, 13, 0, NULL, 0),
309 SND_SOC_DAPM_PGA("Left LineIn", ALC5623_PWR_MANAG_ADD3, 7, 0, NULL, 0),
310 SND_SOC_DAPM_PGA("Right LineIn", ALC5623_PWR_MANAG_ADD3, 6, 0, NULL, 0),
311 SND_SOC_DAPM_PGA("Left AuxI", ALC5623_PWR_MANAG_ADD3, 5, 0, NULL, 0),
312 SND_SOC_DAPM_PGA("Right AuxI", ALC5623_PWR_MANAG_ADD3, 4, 0, NULL, 0),
313 SND_SOC_DAPM_PGA("MIC1 PGA", ALC5623_PWR_MANAG_ADD3, 3, 0, NULL, 0),
314 SND_SOC_DAPM_PGA("MIC2 PGA", ALC5623_PWR_MANAG_ADD3, 2, 0, NULL, 0),
315 SND_SOC_DAPM_PGA("MIC1 Pre Amp", ALC5623_PWR_MANAG_ADD3, 1, 0, NULL, 0),
316 SND_SOC_DAPM_PGA("MIC2 Pre Amp", ALC5623_PWR_MANAG_ADD3, 0, 0, NULL, 0),
317 SND_SOC_DAPM_MICBIAS("Mic Bias1", ALC5623_PWR_MANAG_ADD1, 11, 0),
319 SND_SOC_DAPM_OUTPUT("AUXOUTL"),
320 SND_SOC_DAPM_OUTPUT("AUXOUTR"),
321 SND_SOC_DAPM_OUTPUT("HPL"),
322 SND_SOC_DAPM_OUTPUT("HPR"),
323 SND_SOC_DAPM_OUTPUT("SPKOUT"),
324 SND_SOC_DAPM_OUTPUT("SPKOUTN"),
325 SND_SOC_DAPM_INPUT("LINEINL"),
326 SND_SOC_DAPM_INPUT("LINEINR"),
327 SND_SOC_DAPM_INPUT("AUXINL"),
328 SND_SOC_DAPM_INPUT("AUXINR"),
329 SND_SOC_DAPM_INPUT("MIC1"),
330 SND_SOC_DAPM_INPUT("MIC2"),
331 SND_SOC_DAPM_VMID("Vmid"),
334 static const char *alc5623_amp_names[] = {"AB Amp", "D Amp"};
335 static SOC_ENUM_SINGLE_DECL(alc5623_amp_enum,
336 ALC5623_OUTPUT_MIXER_CTRL, 13,
338 static const struct snd_kcontrol_new alc5623_amp_mux_controls =
339 SOC_DAPM_ENUM("Route", alc5623_amp_enum);
341 static const struct snd_soc_dapm_widget alc5623_dapm_amp_widgets[] = {
342 SND_SOC_DAPM_PGA_E("D Amp", ALC5623_PWR_MANAG_ADD2, 14, 0, NULL, 0,
343 amp_mixer_event, SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
344 SND_SOC_DAPM_PGA("AB Amp", ALC5623_PWR_MANAG_ADD2, 15, 0, NULL, 0),
345 SND_SOC_DAPM_MUX("AB-D Amp Mux", SND_SOC_NOPM, 0, 0,
346 &alc5623_amp_mux_controls),
349 static const struct snd_soc_dapm_route intercon[] = {
350 /* virtual mixer - mixes left & right channels */
351 {"I2S Mix", NULL, "Left DAC"},
352 {"I2S Mix", NULL, "Right DAC"},
353 {"Line Mix", NULL, "Right LineIn"},
354 {"Line Mix", NULL, "Left LineIn"},
355 {"AuxI Mix", NULL, "Left AuxI"},
356 {"AuxI Mix", NULL, "Right AuxI"},
357 {"AUXOUTL", NULL, "Left AuxOut"},
358 {"AUXOUTR", NULL, "Right AuxOut"},
361 {"HPL Mix", "ADC2HP_L Playback Switch", "Left Capture Mix"},
362 {"HPL Mix", NULL, "HP Mix"},
363 {"HPR Mix", "ADC2HP_R Playback Switch", "Right Capture Mix"},
364 {"HPR Mix", NULL, "HP Mix"},
365 {"HP Mix", "LI2HP Playback Switch", "Line Mix"},
366 {"HP Mix", "AUXI2HP Playback Switch", "AuxI Mix"},
367 {"HP Mix", "MIC12HP Playback Switch", "MIC1 PGA"},
368 {"HP Mix", "MIC22HP Playback Switch", "MIC2 PGA"},
369 {"HP Mix", "DAC2HP Playback Switch", "I2S Mix"},
372 {"Speaker Mix", "LI2SPK Playback Switch", "Line Mix"},
373 {"Speaker Mix", "AUXI2SPK Playback Switch", "AuxI Mix"},
374 {"Speaker Mix", "MIC12SPK Playback Switch", "MIC1 PGA"},
375 {"Speaker Mix", "MIC22SPK Playback Switch", "MIC2 PGA"},
376 {"Speaker Mix", "DAC2SPK Playback Switch", "I2S Mix"},
379 {"Mono Mix", "ADC2MONO_L Playback Switch", "Left Capture Mix"},
380 {"Mono Mix", "ADC2MONO_R Playback Switch", "Right Capture Mix"},
381 {"Mono Mix", "LI2MONO Playback Switch", "Line Mix"},
382 {"Mono Mix", "AUXI2MONO Playback Switch", "AuxI Mix"},
383 {"Mono Mix", "MIC12MONO Playback Switch", "MIC1 PGA"},
384 {"Mono Mix", "MIC22MONO Playback Switch", "MIC2 PGA"},
385 {"Mono Mix", "DAC2MONO Playback Switch", "I2S Mix"},
387 /* Left record mixer */
388 {"Left Capture Mix", "LineInL Capture Switch", "LINEINL"},
389 {"Left Capture Mix", "Left AuxI Capture Switch", "AUXINL"},
390 {"Left Capture Mix", "Mic1 Capture Switch", "MIC1 Pre Amp"},
391 {"Left Capture Mix", "Mic2 Capture Switch", "MIC2 Pre Amp"},
392 {"Left Capture Mix", "HPMixerL Capture Switch", "HPL Mix"},
393 {"Left Capture Mix", "SPKMixer Capture Switch", "Speaker Mix"},
394 {"Left Capture Mix", "MonoMixer Capture Switch", "Mono Mix"},
396 /*Right record mixer */
397 {"Right Capture Mix", "LineInR Capture Switch", "LINEINR"},
398 {"Right Capture Mix", "Right AuxI Capture Switch", "AUXINR"},
399 {"Right Capture Mix", "Mic1 Capture Switch", "MIC1 Pre Amp"},
400 {"Right Capture Mix", "Mic2 Capture Switch", "MIC2 Pre Amp"},
401 {"Right Capture Mix", "HPMixerR Capture Switch", "HPR Mix"},
402 {"Right Capture Mix", "SPKMixer Capture Switch", "Speaker Mix"},
403 {"Right Capture Mix", "MonoMixer Capture Switch", "Mono Mix"},
405 /* headphone left mux */
406 {"Left Headphone Mux", "HP Left Mix", "HPL Mix"},
407 {"Left Headphone Mux", "Vmid", "Vmid"},
409 /* headphone right mux */
410 {"Right Headphone Mux", "HP Right Mix", "HPR Mix"},
411 {"Right Headphone Mux", "Vmid", "Vmid"},
413 /* speaker out mux */
414 {"SpeakerOut Mux", "Vmid", "Vmid"},
415 {"SpeakerOut Mux", "HPOut Mix", "HPOut Mix"},
416 {"SpeakerOut Mux", "Speaker Mix", "Speaker Mix"},
417 {"SpeakerOut Mux", "Mono Mix", "Mono Mix"},
419 /* Mono/Aux Out mux */
420 {"AuxOut Mux", "Vmid", "Vmid"},
421 {"AuxOut Mux", "HPOut Mix", "HPOut Mix"},
422 {"AuxOut Mux", "Speaker Mix", "Speaker Mix"},
423 {"AuxOut Mux", "Mono Mix", "Mono Mix"},
426 {"HPL", NULL, "Left Headphone"},
427 {"Left Headphone", NULL, "Left Headphone Mux"},
428 {"HPR", NULL, "Right Headphone"},
429 {"Right Headphone", NULL, "Right Headphone Mux"},
430 {"Left AuxOut", NULL, "AuxOut Mux"},
431 {"Right AuxOut", NULL, "AuxOut Mux"},
434 {"Left LineIn", NULL, "LINEINL"},
435 {"Right LineIn", NULL, "LINEINR"},
436 {"Left AuxI", NULL, "AUXINL"},
437 {"Right AuxI", NULL, "AUXINR"},
438 {"MIC1 Pre Amp", NULL, "MIC1"},
439 {"MIC2 Pre Amp", NULL, "MIC2"},
440 {"MIC1 PGA", NULL, "MIC1 Pre Amp"},
441 {"MIC2 PGA", NULL, "MIC2 Pre Amp"},
444 {"Left ADC", NULL, "Left Capture Mix"},
447 {"Right ADC", NULL, "Right Capture Mix"},
449 {"SpeakerOut N Mux", "RN/-R", "SpeakerOut"},
450 {"SpeakerOut N Mux", "RP/+R", "SpeakerOut"},
451 {"SpeakerOut N Mux", "LN/-R", "SpeakerOut"},
452 {"SpeakerOut N Mux", "Vmid", "Vmid"},
454 {"SPKOUT", NULL, "SpeakerOut"},
455 {"SPKOUTN", NULL, "SpeakerOut N Mux"},
458 static const struct snd_soc_dapm_route intercon_spk[] = {
459 {"SpeakerOut", NULL, "SpeakerOut Mux"},
462 static const struct snd_soc_dapm_route intercon_amp_spk[] = {
463 {"AB Amp", NULL, "SpeakerOut Mux"},
464 {"D Amp", NULL, "SpeakerOut Mux"},
465 {"AB-D Amp Mux", "AB Amp", "AB Amp"},
466 {"AB-D Amp Mux", "D Amp", "D Amp"},
467 {"SpeakerOut", NULL, "AB-D Amp Mux"},
477 /* Note : pll code from original alc5623 driver. Not sure of how good it is */
478 /* useful only for master mode */
479 static const struct _pll_div codec_master_pll_div[] = {
481 { 2048000, 8192000, 0x0ea0},
482 { 3686400, 8192000, 0x4e27},
483 { 12000000, 8192000, 0x456b},
484 { 13000000, 8192000, 0x495f},
485 { 13100000, 8192000, 0x0320},
486 { 2048000, 11289600, 0xf637},
487 { 3686400, 11289600, 0x2f22},
488 { 12000000, 11289600, 0x3e2f},
489 { 13000000, 11289600, 0x4d5b},
490 { 13100000, 11289600, 0x363b},
491 { 2048000, 16384000, 0x1ea0},
492 { 3686400, 16384000, 0x9e27},
493 { 12000000, 16384000, 0x452b},
494 { 13000000, 16384000, 0x542f},
495 { 13100000, 16384000, 0x03a0},
496 { 2048000, 16934400, 0xe625},
497 { 3686400, 16934400, 0x9126},
498 { 12000000, 16934400, 0x4d2c},
499 { 13000000, 16934400, 0x742f},
500 { 13100000, 16934400, 0x3c27},
501 { 2048000, 22579200, 0x2aa0},
502 { 3686400, 22579200, 0x2f20},
503 { 12000000, 22579200, 0x7e2f},
504 { 13000000, 22579200, 0x742f},
505 { 13100000, 22579200, 0x3c27},
506 { 2048000, 24576000, 0x2ea0},
507 { 3686400, 24576000, 0xee27},
508 { 12000000, 24576000, 0x2915},
509 { 13000000, 24576000, 0x772e},
510 { 13100000, 24576000, 0x0d20},
513 static const struct _pll_div codec_slave_pll_div[] = {
515 { 1024000, 16384000, 0x3ea0},
516 { 1411200, 22579200, 0x3ea0},
517 { 1536000, 24576000, 0x3ea0},
518 { 2048000, 16384000, 0x1ea0},
519 { 2822400, 22579200, 0x1ea0},
520 { 3072000, 24576000, 0x1ea0},
524 static int alc5623_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id,
525 int source, unsigned int freq_in, unsigned int freq_out)
528 struct snd_soc_codec *codec = codec_dai->codec;
529 int gbl_clk = 0, pll_div = 0;
532 if (pll_id < ALC5623_PLL_FR_MCLK || pll_id > ALC5623_PLL_FR_BCK)
535 /* Disable PLL power */
536 snd_soc_update_bits(codec, ALC5623_PWR_MANAG_ADD2,
537 ALC5623_PWR_ADD2_PLL,
540 /* pll is not used in slave mode */
541 reg = snd_soc_read(codec, ALC5623_DAI_CONTROL);
542 if (reg & ALC5623_DAI_SDP_SLAVE_MODE)
545 if (!freq_in || !freq_out)
549 case ALC5623_PLL_FR_MCLK:
550 for (i = 0; i < ARRAY_SIZE(codec_master_pll_div); i++) {
551 if (codec_master_pll_div[i].pll_in == freq_in
552 && codec_master_pll_div[i].pll_out == freq_out) {
553 /* PLL source from MCLK */
554 pll_div = codec_master_pll_div[i].regvalue;
559 case ALC5623_PLL_FR_BCK:
560 for (i = 0; i < ARRAY_SIZE(codec_slave_pll_div); i++) {
561 if (codec_slave_pll_div[i].pll_in == freq_in
562 && codec_slave_pll_div[i].pll_out == freq_out) {
563 /* PLL source from Bitclk */
564 gbl_clk = ALC5623_GBL_CLK_PLL_SOUR_SEL_BITCLK;
565 pll_div = codec_slave_pll_div[i].regvalue;
577 snd_soc_write(codec, ALC5623_GLOBAL_CLK_CTRL_REG, gbl_clk);
578 snd_soc_write(codec, ALC5623_PLL_CTRL, pll_div);
579 snd_soc_update_bits(codec, ALC5623_PWR_MANAG_ADD2,
580 ALC5623_PWR_ADD2_PLL,
581 ALC5623_PWR_ADD2_PLL);
582 gbl_clk |= ALC5623_GBL_CLK_SYS_SOUR_SEL_PLL;
583 snd_soc_write(codec, ALC5623_GLOBAL_CLK_CTRL_REG, gbl_clk);
593 /* codec hifi mclk (after PLL) clock divider coefficients */
594 /* values inspired from column BCLK=32Fs of Appendix A table */
595 static const struct _coeff_div coeff_div[] = {
606 static int get_coeff(struct snd_soc_codec *codec, int rate)
608 struct alc5623_priv *alc5623 = snd_soc_codec_get_drvdata(codec);
611 for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
612 if (coeff_div[i].fs * rate == alc5623->sysclk)
619 * Clock after PLL and dividers
621 static int alc5623_set_dai_sysclk(struct snd_soc_dai *codec_dai,
622 int clk_id, unsigned int freq, int dir)
624 struct snd_soc_codec *codec = codec_dai->codec;
625 struct alc5623_priv *alc5623 = snd_soc_codec_get_drvdata(codec);
636 alc5623->sysclk = freq;
642 static int alc5623_set_dai_fmt(struct snd_soc_dai *codec_dai,
645 struct snd_soc_codec *codec = codec_dai->codec;
648 /* set master/slave audio interface */
649 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
650 case SND_SOC_DAIFMT_CBM_CFM:
651 iface = ALC5623_DAI_SDP_MASTER_MODE;
653 case SND_SOC_DAIFMT_CBS_CFS:
654 iface = ALC5623_DAI_SDP_SLAVE_MODE;
660 /* interface format */
661 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
662 case SND_SOC_DAIFMT_I2S:
663 iface |= ALC5623_DAI_I2S_DF_I2S;
665 case SND_SOC_DAIFMT_RIGHT_J:
666 iface |= ALC5623_DAI_I2S_DF_RIGHT;
668 case SND_SOC_DAIFMT_LEFT_J:
669 iface |= ALC5623_DAI_I2S_DF_LEFT;
671 case SND_SOC_DAIFMT_DSP_A:
672 iface |= ALC5623_DAI_I2S_DF_PCM;
674 case SND_SOC_DAIFMT_DSP_B:
675 iface |= ALC5623_DAI_I2S_DF_PCM | ALC5623_DAI_I2S_PCM_MODE;
681 /* clock inversion */
682 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
683 case SND_SOC_DAIFMT_NB_NF:
685 case SND_SOC_DAIFMT_IB_IF:
686 iface |= ALC5623_DAI_MAIN_I2S_BCLK_POL_CTRL;
688 case SND_SOC_DAIFMT_IB_NF:
689 iface |= ALC5623_DAI_MAIN_I2S_BCLK_POL_CTRL;
691 case SND_SOC_DAIFMT_NB_IF:
697 return snd_soc_write(codec, ALC5623_DAI_CONTROL, iface);
700 static int alc5623_pcm_hw_params(struct snd_pcm_substream *substream,
701 struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
703 struct snd_soc_codec *codec = dai->codec;
704 struct alc5623_priv *alc5623 = snd_soc_codec_get_drvdata(codec);
708 iface = snd_soc_read(codec, ALC5623_DAI_CONTROL);
709 iface &= ~ALC5623_DAI_I2S_DL_MASK;
712 switch (params_width(params)) {
714 iface |= ALC5623_DAI_I2S_DL_16;
717 iface |= ALC5623_DAI_I2S_DL_20;
720 iface |= ALC5623_DAI_I2S_DL_24;
723 iface |= ALC5623_DAI_I2S_DL_32;
729 /* set iface & srate */
730 snd_soc_write(codec, ALC5623_DAI_CONTROL, iface);
731 rate = params_rate(params);
732 coeff = get_coeff(codec, rate);
736 coeff = coeff_div[coeff].regvalue;
737 dev_dbg(codec->dev, "%s: sysclk=%d,rate=%d,coeff=0x%04x\n",
738 __func__, alc5623->sysclk, rate, coeff);
739 snd_soc_write(codec, ALC5623_STEREO_AD_DA_CLK_CTRL, coeff);
744 static int alc5623_mute(struct snd_soc_dai *dai, int mute)
746 struct snd_soc_codec *codec = dai->codec;
747 u16 hp_mute = ALC5623_MISC_M_DAC_L_INPUT | ALC5623_MISC_M_DAC_R_INPUT;
748 u16 mute_reg = snd_soc_read(codec, ALC5623_MISC_CTRL) & ~hp_mute;
753 return snd_soc_write(codec, ALC5623_MISC_CTRL, mute_reg);
756 #define ALC5623_ADD2_POWER_EN (ALC5623_PWR_ADD2_VREF \
757 | ALC5623_PWR_ADD2_DAC_REF_CIR)
759 #define ALC5623_ADD3_POWER_EN (ALC5623_PWR_ADD3_MAIN_BIAS \
760 | ALC5623_PWR_ADD3_MIC1_BOOST_AD)
762 #define ALC5623_ADD1_POWER_EN \
763 (ALC5623_PWR_ADD1_SHORT_CURR_DET_EN | ALC5623_PWR_ADD1_SOFTGEN_EN \
764 | ALC5623_PWR_ADD1_DEPOP_BUF_HP | ALC5623_PWR_ADD1_HP_OUT_AMP \
765 | ALC5623_PWR_ADD1_HP_OUT_ENH_AMP)
767 #define ALC5623_ADD1_POWER_EN_5622 \
768 (ALC5623_PWR_ADD1_SHORT_CURR_DET_EN \
769 | ALC5623_PWR_ADD1_HP_OUT_AMP)
771 static void enable_power_depop(struct snd_soc_codec *codec)
773 struct alc5623_priv *alc5623 = snd_soc_codec_get_drvdata(codec);
775 snd_soc_update_bits(codec, ALC5623_PWR_MANAG_ADD1,
776 ALC5623_PWR_ADD1_SOFTGEN_EN,
777 ALC5623_PWR_ADD1_SOFTGEN_EN);
779 snd_soc_write(codec, ALC5623_PWR_MANAG_ADD3, ALC5623_ADD3_POWER_EN);
781 snd_soc_update_bits(codec, ALC5623_MISC_CTRL,
782 ALC5623_MISC_HP_DEPOP_MODE2_EN,
783 ALC5623_MISC_HP_DEPOP_MODE2_EN);
787 snd_soc_write(codec, ALC5623_PWR_MANAG_ADD2, ALC5623_ADD2_POWER_EN);
789 /* avoid writing '1' into 5622 reserved bits */
790 if (alc5623->id == 0x22)
791 snd_soc_write(codec, ALC5623_PWR_MANAG_ADD1,
792 ALC5623_ADD1_POWER_EN_5622);
794 snd_soc_write(codec, ALC5623_PWR_MANAG_ADD1,
795 ALC5623_ADD1_POWER_EN);
797 /* disable HP Depop2 */
798 snd_soc_update_bits(codec, ALC5623_MISC_CTRL,
799 ALC5623_MISC_HP_DEPOP_MODE2_EN,
804 static int alc5623_set_bias_level(struct snd_soc_codec *codec,
805 enum snd_soc_bias_level level)
808 case SND_SOC_BIAS_ON:
809 enable_power_depop(codec);
811 case SND_SOC_BIAS_PREPARE:
813 case SND_SOC_BIAS_STANDBY:
814 /* everything off except vref/vmid, */
815 snd_soc_write(codec, ALC5623_PWR_MANAG_ADD2,
816 ALC5623_PWR_ADD2_VREF);
817 snd_soc_write(codec, ALC5623_PWR_MANAG_ADD3,
818 ALC5623_PWR_ADD3_MAIN_BIAS);
820 case SND_SOC_BIAS_OFF:
821 /* everything off, dac mute, inactive */
822 snd_soc_write(codec, ALC5623_PWR_MANAG_ADD2, 0);
823 snd_soc_write(codec, ALC5623_PWR_MANAG_ADD3, 0);
824 snd_soc_write(codec, ALC5623_PWR_MANAG_ADD1, 0);
827 codec->dapm.bias_level = level;
831 #define ALC5623_FORMATS (SNDRV_PCM_FMTBIT_S16_LE \
832 | SNDRV_PCM_FMTBIT_S24_LE \
833 | SNDRV_PCM_FMTBIT_S32_LE)
835 static const struct snd_soc_dai_ops alc5623_dai_ops = {
836 .hw_params = alc5623_pcm_hw_params,
837 .digital_mute = alc5623_mute,
838 .set_fmt = alc5623_set_dai_fmt,
839 .set_sysclk = alc5623_set_dai_sysclk,
840 .set_pll = alc5623_set_dai_pll,
843 static struct snd_soc_dai_driver alc5623_dai = {
844 .name = "alc5623-hifi",
846 .stream_name = "Playback",
851 .rates = SNDRV_PCM_RATE_8000_48000,
852 .formats = ALC5623_FORMATS,},
854 .stream_name = "Capture",
859 .rates = SNDRV_PCM_RATE_8000_48000,
860 .formats = ALC5623_FORMATS,},
862 .ops = &alc5623_dai_ops,
865 static int alc5623_suspend(struct snd_soc_codec *codec)
867 struct alc5623_priv *alc5623 = snd_soc_codec_get_drvdata(codec);
869 regcache_cache_only(alc5623->regmap, true);
874 static int alc5623_resume(struct snd_soc_codec *codec)
876 struct alc5623_priv *alc5623 = snd_soc_codec_get_drvdata(codec);
879 /* Sync reg_cache with the hardware */
880 regcache_cache_only(alc5623->regmap, false);
881 ret = regcache_sync(alc5623->regmap);
883 dev_err(codec->dev, "Failed to sync register cache: %d\n",
885 regcache_cache_only(alc5623->regmap, true);
892 static int alc5623_probe(struct snd_soc_codec *codec)
894 struct alc5623_priv *alc5623 = snd_soc_codec_get_drvdata(codec);
895 struct snd_soc_dapm_context *dapm = &codec->dapm;
897 alc5623_reset(codec);
899 if (alc5623->add_ctrl) {
900 snd_soc_write(codec, ALC5623_ADD_CTRL_REG,
904 if (alc5623->jack_det_ctrl) {
905 snd_soc_write(codec, ALC5623_JACK_DET_CTRL,
906 alc5623->jack_det_ctrl);
909 switch (alc5623->id) {
911 snd_soc_add_codec_controls(codec, alc5621_vol_snd_controls,
912 ARRAY_SIZE(alc5621_vol_snd_controls));
915 snd_soc_add_codec_controls(codec, alc5622_vol_snd_controls,
916 ARRAY_SIZE(alc5622_vol_snd_controls));
919 snd_soc_add_codec_controls(codec, alc5623_vol_snd_controls,
920 ARRAY_SIZE(alc5623_vol_snd_controls));
926 snd_soc_add_codec_controls(codec, alc5623_snd_controls,
927 ARRAY_SIZE(alc5623_snd_controls));
929 snd_soc_dapm_new_controls(dapm, alc5623_dapm_widgets,
930 ARRAY_SIZE(alc5623_dapm_widgets));
932 /* set up audio path interconnects */
933 snd_soc_dapm_add_routes(dapm, intercon, ARRAY_SIZE(intercon));
935 switch (alc5623->id) {
938 snd_soc_dapm_new_controls(dapm, alc5623_dapm_amp_widgets,
939 ARRAY_SIZE(alc5623_dapm_amp_widgets));
940 snd_soc_dapm_add_routes(dapm, intercon_amp_spk,
941 ARRAY_SIZE(intercon_amp_spk));
944 snd_soc_dapm_add_routes(dapm, intercon_spk,
945 ARRAY_SIZE(intercon_spk));
954 static struct snd_soc_codec_driver soc_codec_device_alc5623 = {
955 .probe = alc5623_probe,
956 .suspend = alc5623_suspend,
957 .resume = alc5623_resume,
958 .set_bias_level = alc5623_set_bias_level,
959 .suspend_bias_off = true,
962 static const struct regmap_config alc5623_regmap = {
967 .max_register = ALC5623_VENDOR_ID2,
968 .cache_type = REGCACHE_RBTREE,
972 * ALC5623 2 wire address is determined by A1 pin
973 * state during powerup.
977 static int alc5623_i2c_probe(struct i2c_client *client,
978 const struct i2c_device_id *id)
980 struct alc5623_platform_data *pdata;
981 struct alc5623_priv *alc5623;
982 struct device_node *np;
983 unsigned int vid1, vid2;
987 alc5623 = devm_kzalloc(&client->dev, sizeof(struct alc5623_priv),
992 alc5623->regmap = devm_regmap_init_i2c(client, &alc5623_regmap);
993 if (IS_ERR(alc5623->regmap)) {
994 ret = PTR_ERR(alc5623->regmap);
995 dev_err(&client->dev, "Failed to initialise I/O: %d\n", ret);
999 ret = regmap_read(alc5623->regmap, ALC5623_VENDOR_ID1, &vid1);
1001 dev_err(&client->dev, "failed to read vendor ID1: %d\n", ret);
1005 ret = regmap_read(alc5623->regmap, ALC5623_VENDOR_ID2, &vid2);
1007 dev_err(&client->dev, "failed to read vendor ID2: %d\n", ret);
1012 if ((vid1 != 0x10ec) || (vid2 != id->driver_data)) {
1013 dev_err(&client->dev, "unknown or wrong codec\n");
1014 dev_err(&client->dev, "Expected %x:%lx, got %x:%x\n",
1015 0x10ec, id->driver_data,
1020 dev_dbg(&client->dev, "Found codec id : alc56%02x\n", vid2);
1022 pdata = client->dev.platform_data;
1024 alc5623->add_ctrl = pdata->add_ctrl;
1025 alc5623->jack_det_ctrl = pdata->jack_det_ctrl;
1027 if (client->dev.of_node) {
1028 np = client->dev.of_node;
1029 ret = of_property_read_u32(np, "add-ctrl", &val32);
1031 alc5623->add_ctrl = val32;
1032 ret = of_property_read_u32(np, "jack-det-ctrl", &val32);
1034 alc5623->jack_det_ctrl = val32;
1039 switch (alc5623->id) {
1041 alc5623_dai.name = "alc5621-hifi";
1044 alc5623_dai.name = "alc5622-hifi";
1047 alc5623_dai.name = "alc5623-hifi";
1053 i2c_set_clientdata(client, alc5623);
1055 ret = snd_soc_register_codec(&client->dev,
1056 &soc_codec_device_alc5623, &alc5623_dai, 1);
1058 dev_err(&client->dev, "Failed to register codec: %d\n", ret);
1063 static int alc5623_i2c_remove(struct i2c_client *client)
1065 snd_soc_unregister_codec(&client->dev);
1069 static const struct i2c_device_id alc5623_i2c_table[] = {
1075 MODULE_DEVICE_TABLE(i2c, alc5623_i2c_table);
1077 static const struct of_device_id alc5623_of_match[] = {
1078 { .compatible = "realtek,alc5623", },
1081 MODULE_DEVICE_TABLE(of, alc5623_of_match);
1083 /* i2c codec control layer */
1084 static struct i2c_driver alc5623_i2c_driver = {
1086 .name = "alc562x-codec",
1087 .owner = THIS_MODULE,
1088 .of_match_table = of_match_ptr(alc5623_of_match),
1090 .probe = alc5623_i2c_probe,
1091 .remove = alc5623_i2c_remove,
1092 .id_table = alc5623_i2c_table,
1095 module_i2c_driver(alc5623_i2c_driver);
1097 MODULE_DESCRIPTION("ASoC alc5621/2/3 driver");
1098 MODULE_AUTHOR("Arnaud Patard <arnaud.patard@rtp-net.org>");
1099 MODULE_LICENSE("GPL");