blob: 1960478ce6bb883dbf02af92e242e2b538ea8823 [file] [log] [blame]
Arnaud Patard (Rtp)6f4bc952010-10-21 19:40:02 +02001/*
2 * alc5623.c -- alc562[123] ALSA Soc Audio driver
3 *
4 * Copyright 2008 Realtek Microelectronics
5 * Author: flove <flove@realtek.com> Ethan <eku@marvell.com>
6 *
7 * Copyright 2010 Arnaud Patard <arnaud.patard@rtp-net.org>
8 *
9 *
10 * Based on WM8753.c
11 *
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.
15 *
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>
Arnaud Patard (Rtp)6f4bc952010-10-21 19:40:02 +020025#include <sound/core.h>
26#include <sound/pcm.h>
27#include <sound/pcm_params.h>
28#include <sound/tlv.h>
29#include <sound/soc.h>
Arnaud Patard (Rtp)6f4bc952010-10-21 19:40:02 +020030#include <sound/initval.h>
31#include <sound/alc5623.h>
32
33#include "alc5623.h"
34
35static int caps_charge = 2000;
36module_param(caps_charge, int, 0);
37MODULE_PARM_DESC(caps_charge, "ALC5623 cap charge time (msecs)");
38
39/* codec private data */
40struct alc5623_priv {
41 enum snd_soc_control_type control_type;
Arnaud Patard (Rtp)6f4bc952010-10-21 19:40:02 +020042 u8 id;
43 unsigned int sysclk;
44 u16 reg_cache[ALC5623_VENDOR_ID2+2];
45 unsigned int add_ctrl;
46 unsigned int jack_det_ctrl;
47};
48
49static void alc5623_fill_cache(struct snd_soc_codec *codec)
50{
51 int i, step = codec->driver->reg_cache_step;
52 u16 *cache = codec->reg_cache;
53
54 /* not really efficient ... */
Axel Lin524205c2011-10-14 09:35:20 +080055 codec->cache_bypass = 1;
Arnaud Patard (Rtp)6f4bc952010-10-21 19:40:02 +020056 for (i = 0 ; i < codec->driver->reg_cache_size ; i += step)
Axel Lin524205c2011-10-14 09:35:20 +080057 cache[i] = snd_soc_read(codec, i);
58 codec->cache_bypass = 0;
Arnaud Patard (Rtp)6f4bc952010-10-21 19:40:02 +020059}
60
61static inline int alc5623_reset(struct snd_soc_codec *codec)
62{
63 return snd_soc_write(codec, ALC5623_RESET, 0);
64}
65
66static int amp_mixer_event(struct snd_soc_dapm_widget *w,
67 struct snd_kcontrol *kcontrol, int event)
68{
69 /* to power-on/off class-d amp generators/speaker */
70 /* need to write to 'index-46h' register : */
71 /* so write index num (here 0x46) to reg 0x6a */
72 /* and then 0xffff/0 to reg 0x6c */
73 snd_soc_write(w->codec, ALC5623_HID_CTRL_INDEX, 0x46);
74
75 switch (event) {
76 case SND_SOC_DAPM_PRE_PMU:
77 snd_soc_write(w->codec, ALC5623_HID_CTRL_DATA, 0xFFFF);
78 break;
79 case SND_SOC_DAPM_POST_PMD:
80 snd_soc_write(w->codec, ALC5623_HID_CTRL_DATA, 0);
81 break;
82 }
83
84 return 0;
85}
86
87/*
88 * ALC5623 Controls
89 */
90
91static const DECLARE_TLV_DB_SCALE(vol_tlv, -3450, 150, 0);
92static const DECLARE_TLV_DB_SCALE(hp_tlv, -4650, 150, 0);
93static const DECLARE_TLV_DB_SCALE(adc_rec_tlv, -1650, 150, 0);
94static const unsigned int boost_tlv[] = {
95 TLV_DB_RANGE_HEAD(3),
96 0, 0, TLV_DB_SCALE_ITEM(0, 0, 0),
97 1, 1, TLV_DB_SCALE_ITEM(2000, 0, 0),
98 2, 2, TLV_DB_SCALE_ITEM(3000, 0, 0),
99};
100static const DECLARE_TLV_DB_SCALE(dig_tlv, 0, 600, 0);
101
Axel Lin6048ef72011-12-15 11:57:57 +0800102static const struct snd_kcontrol_new alc5621_vol_snd_controls[] = {
Arnaud Patard (Rtp)6f4bc952010-10-21 19:40:02 +0200103 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("Headphone Playback Volume",
108 ALC5623_HP_OUT_VOL, 8, 0, 31, 1, hp_tlv),
109 SOC_DOUBLE("Headphone Playback Switch",
110 ALC5623_HP_OUT_VOL, 15, 7, 1, 1),
111};
112
Axel Lin6048ef72011-12-15 11:57:57 +0800113static const struct snd_kcontrol_new alc5622_vol_snd_controls[] = {
Arnaud Patard (Rtp)6f4bc952010-10-21 19:40:02 +0200114 SOC_DOUBLE_TLV("Speaker Playback Volume",
115 ALC5623_SPK_OUT_VOL, 8, 0, 31, 1, hp_tlv),
116 SOC_DOUBLE("Speaker Playback Switch",
117 ALC5623_SPK_OUT_VOL, 15, 7, 1, 1),
118 SOC_DOUBLE_TLV("Line Playback Volume",
119 ALC5623_HP_OUT_VOL, 8, 0, 31, 1, hp_tlv),
120 SOC_DOUBLE("Line Playback Switch",
121 ALC5623_HP_OUT_VOL, 15, 7, 1, 1),
122};
123
124static const struct snd_kcontrol_new alc5623_vol_snd_controls[] = {
125 SOC_DOUBLE_TLV("Line Playback Volume",
126 ALC5623_SPK_OUT_VOL, 8, 0, 31, 1, hp_tlv),
127 SOC_DOUBLE("Line Playback Switch",
128 ALC5623_SPK_OUT_VOL, 15, 7, 1, 1),
129 SOC_DOUBLE_TLV("Headphone Playback Volume",
130 ALC5623_HP_OUT_VOL, 8, 0, 31, 1, hp_tlv),
131 SOC_DOUBLE("Headphone Playback Switch",
132 ALC5623_HP_OUT_VOL, 15, 7, 1, 1),
133};
134
135static const struct snd_kcontrol_new alc5623_snd_controls[] = {
136 SOC_DOUBLE_TLV("Auxout Playback Volume",
137 ALC5623_MONO_AUX_OUT_VOL, 8, 0, 31, 1, hp_tlv),
138 SOC_DOUBLE("Auxout Playback Switch",
139 ALC5623_MONO_AUX_OUT_VOL, 15, 7, 1, 1),
140 SOC_DOUBLE_TLV("PCM Playback Volume",
141 ALC5623_STEREO_DAC_VOL, 8, 0, 31, 1, vol_tlv),
142 SOC_DOUBLE_TLV("AuxI Capture Volume",
143 ALC5623_AUXIN_VOL, 8, 0, 31, 1, vol_tlv),
144 SOC_DOUBLE_TLV("LineIn Capture Volume",
145 ALC5623_LINE_IN_VOL, 8, 0, 31, 1, vol_tlv),
146 SOC_SINGLE_TLV("Mic1 Capture Volume",
147 ALC5623_MIC_VOL, 8, 31, 1, vol_tlv),
148 SOC_SINGLE_TLV("Mic2 Capture Volume",
149 ALC5623_MIC_VOL, 0, 31, 1, vol_tlv),
150 SOC_DOUBLE_TLV("Rec Capture Volume",
151 ALC5623_ADC_REC_GAIN, 7, 0, 31, 0, adc_rec_tlv),
152 SOC_SINGLE_TLV("Mic 1 Boost Volume",
153 ALC5623_MIC_CTRL, 10, 2, 0, boost_tlv),
154 SOC_SINGLE_TLV("Mic 2 Boost Volume",
155 ALC5623_MIC_CTRL, 8, 2, 0, boost_tlv),
156 SOC_SINGLE_TLV("Digital Boost Volume",
157 ALC5623_ADD_CTRL_REG, 4, 3, 0, dig_tlv),
158};
159
160/*
161 * DAPM Controls
162 */
163static const struct snd_kcontrol_new alc5623_hp_mixer_controls[] = {
164SOC_DAPM_SINGLE("LI2HP Playback Switch", ALC5623_LINE_IN_VOL, 15, 1, 1),
165SOC_DAPM_SINGLE("AUXI2HP Playback Switch", ALC5623_AUXIN_VOL, 15, 1, 1),
166SOC_DAPM_SINGLE("MIC12HP Playback Switch", ALC5623_MIC_ROUTING_CTRL, 15, 1, 1),
167SOC_DAPM_SINGLE("MIC22HP Playback Switch", ALC5623_MIC_ROUTING_CTRL, 7, 1, 1),
168SOC_DAPM_SINGLE("DAC2HP Playback Switch", ALC5623_STEREO_DAC_VOL, 15, 1, 1),
169};
170
171static const struct snd_kcontrol_new alc5623_hpl_mixer_controls[] = {
172SOC_DAPM_SINGLE("ADC2HP_L Playback Switch", ALC5623_ADC_REC_GAIN, 15, 1, 1),
173};
174
175static const struct snd_kcontrol_new alc5623_hpr_mixer_controls[] = {
176SOC_DAPM_SINGLE("ADC2HP_R Playback Switch", ALC5623_ADC_REC_GAIN, 14, 1, 1),
177};
178
179static const struct snd_kcontrol_new alc5623_mono_mixer_controls[] = {
180SOC_DAPM_SINGLE("ADC2MONO_L Playback Switch", ALC5623_ADC_REC_GAIN, 13, 1, 1),
181SOC_DAPM_SINGLE("ADC2MONO_R Playback Switch", ALC5623_ADC_REC_GAIN, 12, 1, 1),
182SOC_DAPM_SINGLE("LI2MONO Playback Switch", ALC5623_LINE_IN_VOL, 13, 1, 1),
183SOC_DAPM_SINGLE("AUXI2MONO Playback Switch", ALC5623_AUXIN_VOL, 13, 1, 1),
184SOC_DAPM_SINGLE("MIC12MONO Playback Switch", ALC5623_MIC_ROUTING_CTRL, 13, 1, 1),
185SOC_DAPM_SINGLE("MIC22MONO Playback Switch", ALC5623_MIC_ROUTING_CTRL, 5, 1, 1),
186SOC_DAPM_SINGLE("DAC2MONO Playback Switch", ALC5623_STEREO_DAC_VOL, 13, 1, 1),
187};
188
189static const struct snd_kcontrol_new alc5623_speaker_mixer_controls[] = {
190SOC_DAPM_SINGLE("LI2SPK Playback Switch", ALC5623_LINE_IN_VOL, 14, 1, 1),
191SOC_DAPM_SINGLE("AUXI2SPK Playback Switch", ALC5623_AUXIN_VOL, 14, 1, 1),
192SOC_DAPM_SINGLE("MIC12SPK Playback Switch", ALC5623_MIC_ROUTING_CTRL, 14, 1, 1),
193SOC_DAPM_SINGLE("MIC22SPK Playback Switch", ALC5623_MIC_ROUTING_CTRL, 6, 1, 1),
194SOC_DAPM_SINGLE("DAC2SPK Playback Switch", ALC5623_STEREO_DAC_VOL, 14, 1, 1),
195};
196
197/* Left Record Mixer */
198static const struct snd_kcontrol_new alc5623_captureL_mixer_controls[] = {
199SOC_DAPM_SINGLE("Mic1 Capture Switch", ALC5623_ADC_REC_MIXER, 14, 1, 1),
200SOC_DAPM_SINGLE("Mic2 Capture Switch", ALC5623_ADC_REC_MIXER, 13, 1, 1),
201SOC_DAPM_SINGLE("LineInL Capture Switch", ALC5623_ADC_REC_MIXER, 12, 1, 1),
202SOC_DAPM_SINGLE("Left AuxI Capture Switch", ALC5623_ADC_REC_MIXER, 11, 1, 1),
203SOC_DAPM_SINGLE("HPMixerL Capture Switch", ALC5623_ADC_REC_MIXER, 10, 1, 1),
204SOC_DAPM_SINGLE("SPKMixer Capture Switch", ALC5623_ADC_REC_MIXER, 9, 1, 1),
205SOC_DAPM_SINGLE("MonoMixer Capture Switch", ALC5623_ADC_REC_MIXER, 8, 1, 1),
206};
207
208/* Right Record Mixer */
209static const struct snd_kcontrol_new alc5623_captureR_mixer_controls[] = {
210SOC_DAPM_SINGLE("Mic1 Capture Switch", ALC5623_ADC_REC_MIXER, 6, 1, 1),
211SOC_DAPM_SINGLE("Mic2 Capture Switch", ALC5623_ADC_REC_MIXER, 5, 1, 1),
212SOC_DAPM_SINGLE("LineInR Capture Switch", ALC5623_ADC_REC_MIXER, 4, 1, 1),
213SOC_DAPM_SINGLE("Right AuxI Capture Switch", ALC5623_ADC_REC_MIXER, 3, 1, 1),
214SOC_DAPM_SINGLE("HPMixerR Capture Switch", ALC5623_ADC_REC_MIXER, 2, 1, 1),
215SOC_DAPM_SINGLE("SPKMixer Capture Switch", ALC5623_ADC_REC_MIXER, 1, 1, 1),
216SOC_DAPM_SINGLE("MonoMixer Capture Switch", ALC5623_ADC_REC_MIXER, 0, 1, 1),
217};
218
219static const char *alc5623_spk_n_sour_sel[] = {
220 "RN/-R", "RP/+R", "LN/-R", "Vmid" };
221static const char *alc5623_hpl_out_input_sel[] = {
222 "Vmid", "HP Left Mix"};
223static const char *alc5623_hpr_out_input_sel[] = {
224 "Vmid", "HP Right Mix"};
225static const char *alc5623_spkout_input_sel[] = {
226 "Vmid", "HPOut Mix", "Speaker Mix", "Mono Mix"};
227static const char *alc5623_aux_out_input_sel[] = {
228 "Vmid", "HPOut Mix", "Speaker Mix", "Mono Mix"};
229
230/* auxout output mux */
231static const struct soc_enum alc5623_aux_out_input_enum =
232SOC_ENUM_SINGLE(ALC5623_OUTPUT_MIXER_CTRL, 6, 4, alc5623_aux_out_input_sel);
233static const struct snd_kcontrol_new alc5623_auxout_mux_controls =
234SOC_DAPM_ENUM("Route", alc5623_aux_out_input_enum);
235
236/* speaker output mux */
237static const struct soc_enum alc5623_spkout_input_enum =
238SOC_ENUM_SINGLE(ALC5623_OUTPUT_MIXER_CTRL, 10, 4, alc5623_spkout_input_sel);
239static const struct snd_kcontrol_new alc5623_spkout_mux_controls =
240SOC_DAPM_ENUM("Route", alc5623_spkout_input_enum);
241
242/* headphone left output mux */
243static const struct soc_enum alc5623_hpl_out_input_enum =
244SOC_ENUM_SINGLE(ALC5623_OUTPUT_MIXER_CTRL, 9, 2, alc5623_hpl_out_input_sel);
245static const struct snd_kcontrol_new alc5623_hpl_out_mux_controls =
246SOC_DAPM_ENUM("Route", alc5623_hpl_out_input_enum);
247
248/* headphone right output mux */
249static const struct soc_enum alc5623_hpr_out_input_enum =
250SOC_ENUM_SINGLE(ALC5623_OUTPUT_MIXER_CTRL, 8, 2, alc5623_hpr_out_input_sel);
251static const struct snd_kcontrol_new alc5623_hpr_out_mux_controls =
252SOC_DAPM_ENUM("Route", alc5623_hpr_out_input_enum);
253
254/* speaker output N select */
255static const struct soc_enum alc5623_spk_n_sour_enum =
256SOC_ENUM_SINGLE(ALC5623_OUTPUT_MIXER_CTRL, 14, 4, alc5623_spk_n_sour_sel);
257static const struct snd_kcontrol_new alc5623_spkoutn_mux_controls =
258SOC_DAPM_ENUM("Route", alc5623_spk_n_sour_enum);
259
260static const struct snd_soc_dapm_widget alc5623_dapm_widgets[] = {
261/* Muxes */
262SND_SOC_DAPM_MUX("AuxOut Mux", SND_SOC_NOPM, 0, 0,
263 &alc5623_auxout_mux_controls),
264SND_SOC_DAPM_MUX("SpeakerOut Mux", SND_SOC_NOPM, 0, 0,
265 &alc5623_spkout_mux_controls),
266SND_SOC_DAPM_MUX("Left Headphone Mux", SND_SOC_NOPM, 0, 0,
267 &alc5623_hpl_out_mux_controls),
268SND_SOC_DAPM_MUX("Right Headphone Mux", SND_SOC_NOPM, 0, 0,
269 &alc5623_hpr_out_mux_controls),
270SND_SOC_DAPM_MUX("SpeakerOut N Mux", SND_SOC_NOPM, 0, 0,
271 &alc5623_spkoutn_mux_controls),
272
273/* output mixers */
274SND_SOC_DAPM_MIXER("HP Mix", SND_SOC_NOPM, 0, 0,
275 &alc5623_hp_mixer_controls[0],
276 ARRAY_SIZE(alc5623_hp_mixer_controls)),
277SND_SOC_DAPM_MIXER("HPR Mix", ALC5623_PWR_MANAG_ADD2, 4, 0,
278 &alc5623_hpr_mixer_controls[0],
279 ARRAY_SIZE(alc5623_hpr_mixer_controls)),
280SND_SOC_DAPM_MIXER("HPL Mix", ALC5623_PWR_MANAG_ADD2, 5, 0,
281 &alc5623_hpl_mixer_controls[0],
282 ARRAY_SIZE(alc5623_hpl_mixer_controls)),
283SND_SOC_DAPM_MIXER("HPOut Mix", SND_SOC_NOPM, 0, 0, NULL, 0),
284SND_SOC_DAPM_MIXER("Mono Mix", ALC5623_PWR_MANAG_ADD2, 2, 0,
285 &alc5623_mono_mixer_controls[0],
286 ARRAY_SIZE(alc5623_mono_mixer_controls)),
287SND_SOC_DAPM_MIXER("Speaker Mix", ALC5623_PWR_MANAG_ADD2, 3, 0,
288 &alc5623_speaker_mixer_controls[0],
289 ARRAY_SIZE(alc5623_speaker_mixer_controls)),
290
291/* input mixers */
292SND_SOC_DAPM_MIXER("Left Capture Mix", ALC5623_PWR_MANAG_ADD2, 1, 0,
293 &alc5623_captureL_mixer_controls[0],
294 ARRAY_SIZE(alc5623_captureL_mixer_controls)),
295SND_SOC_DAPM_MIXER("Right Capture Mix", ALC5623_PWR_MANAG_ADD2, 0, 0,
296 &alc5623_captureR_mixer_controls[0],
297 ARRAY_SIZE(alc5623_captureR_mixer_controls)),
298
299SND_SOC_DAPM_DAC("Left DAC", "Left HiFi Playback",
300 ALC5623_PWR_MANAG_ADD2, 9, 0),
301SND_SOC_DAPM_DAC("Right DAC", "Right HiFi Playback",
302 ALC5623_PWR_MANAG_ADD2, 8, 0),
303SND_SOC_DAPM_MIXER("I2S Mix", ALC5623_PWR_MANAG_ADD1, 15, 0, NULL, 0),
304SND_SOC_DAPM_MIXER("AuxI Mix", SND_SOC_NOPM, 0, 0, NULL, 0),
305SND_SOC_DAPM_MIXER("Line Mix", SND_SOC_NOPM, 0, 0, NULL, 0),
306SND_SOC_DAPM_ADC("Left ADC", "Left HiFi Capture",
307 ALC5623_PWR_MANAG_ADD2, 7, 0),
308SND_SOC_DAPM_ADC("Right ADC", "Right HiFi Capture",
309 ALC5623_PWR_MANAG_ADD2, 6, 0),
310SND_SOC_DAPM_PGA("Left Headphone", ALC5623_PWR_MANAG_ADD3, 10, 0, NULL, 0),
311SND_SOC_DAPM_PGA("Right Headphone", ALC5623_PWR_MANAG_ADD3, 9, 0, NULL, 0),
312SND_SOC_DAPM_PGA("SpeakerOut", ALC5623_PWR_MANAG_ADD3, 12, 0, NULL, 0),
313SND_SOC_DAPM_PGA("Left AuxOut", ALC5623_PWR_MANAG_ADD3, 14, 0, NULL, 0),
314SND_SOC_DAPM_PGA("Right AuxOut", ALC5623_PWR_MANAG_ADD3, 13, 0, NULL, 0),
315SND_SOC_DAPM_PGA("Left LineIn", ALC5623_PWR_MANAG_ADD3, 7, 0, NULL, 0),
316SND_SOC_DAPM_PGA("Right LineIn", ALC5623_PWR_MANAG_ADD3, 6, 0, NULL, 0),
317SND_SOC_DAPM_PGA("Left AuxI", ALC5623_PWR_MANAG_ADD3, 5, 0, NULL, 0),
318SND_SOC_DAPM_PGA("Right AuxI", ALC5623_PWR_MANAG_ADD3, 4, 0, NULL, 0),
319SND_SOC_DAPM_PGA("MIC1 PGA", ALC5623_PWR_MANAG_ADD3, 3, 0, NULL, 0),
320SND_SOC_DAPM_PGA("MIC2 PGA", ALC5623_PWR_MANAG_ADD3, 2, 0, NULL, 0),
321SND_SOC_DAPM_PGA("MIC1 Pre Amp", ALC5623_PWR_MANAG_ADD3, 1, 0, NULL, 0),
322SND_SOC_DAPM_PGA("MIC2 Pre Amp", ALC5623_PWR_MANAG_ADD3, 0, 0, NULL, 0),
323SND_SOC_DAPM_MICBIAS("Mic Bias1", ALC5623_PWR_MANAG_ADD1, 11, 0),
324
325SND_SOC_DAPM_OUTPUT("AUXOUTL"),
326SND_SOC_DAPM_OUTPUT("AUXOUTR"),
327SND_SOC_DAPM_OUTPUT("HPL"),
328SND_SOC_DAPM_OUTPUT("HPR"),
329SND_SOC_DAPM_OUTPUT("SPKOUT"),
330SND_SOC_DAPM_OUTPUT("SPKOUTN"),
331SND_SOC_DAPM_INPUT("LINEINL"),
332SND_SOC_DAPM_INPUT("LINEINR"),
333SND_SOC_DAPM_INPUT("AUXINL"),
334SND_SOC_DAPM_INPUT("AUXINR"),
335SND_SOC_DAPM_INPUT("MIC1"),
336SND_SOC_DAPM_INPUT("MIC2"),
337SND_SOC_DAPM_VMID("Vmid"),
338};
339
340static const char *alc5623_amp_names[] = {"AB Amp", "D Amp"};
341static const struct soc_enum alc5623_amp_enum =
342 SOC_ENUM_SINGLE(ALC5623_OUTPUT_MIXER_CTRL, 13, 2, alc5623_amp_names);
343static const struct snd_kcontrol_new alc5623_amp_mux_controls =
344 SOC_DAPM_ENUM("Route", alc5623_amp_enum);
345
346static const struct snd_soc_dapm_widget alc5623_dapm_amp_widgets[] = {
347SND_SOC_DAPM_PGA_E("D Amp", ALC5623_PWR_MANAG_ADD2, 14, 0, NULL, 0,
348 amp_mixer_event, SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
349SND_SOC_DAPM_PGA("AB Amp", ALC5623_PWR_MANAG_ADD2, 15, 0, NULL, 0),
350SND_SOC_DAPM_MUX("AB-D Amp Mux", SND_SOC_NOPM, 0, 0,
351 &alc5623_amp_mux_controls),
352};
353
354static const struct snd_soc_dapm_route intercon[] = {
355 /* virtual mixer - mixes left & right channels */
356 {"I2S Mix", NULL, "Left DAC"},
357 {"I2S Mix", NULL, "Right DAC"},
358 {"Line Mix", NULL, "Right LineIn"},
359 {"Line Mix", NULL, "Left LineIn"},
360 {"AuxI Mix", NULL, "Left AuxI"},
361 {"AuxI Mix", NULL, "Right AuxI"},
362 {"AUXOUTL", NULL, "Left AuxOut"},
363 {"AUXOUTR", NULL, "Right AuxOut"},
364
365 /* HP mixer */
366 {"HPL Mix", "ADC2HP_L Playback Switch", "Left Capture Mix"},
367 {"HPL Mix", NULL, "HP Mix"},
368 {"HPR Mix", "ADC2HP_R Playback Switch", "Right Capture Mix"},
369 {"HPR Mix", NULL, "HP Mix"},
370 {"HP Mix", "LI2HP Playback Switch", "Line Mix"},
371 {"HP Mix", "AUXI2HP Playback Switch", "AuxI Mix"},
372 {"HP Mix", "MIC12HP Playback Switch", "MIC1 PGA"},
373 {"HP Mix", "MIC22HP Playback Switch", "MIC2 PGA"},
374 {"HP Mix", "DAC2HP Playback Switch", "I2S Mix"},
375
376 /* speaker mixer */
377 {"Speaker Mix", "LI2SPK Playback Switch", "Line Mix"},
378 {"Speaker Mix", "AUXI2SPK Playback Switch", "AuxI Mix"},
379 {"Speaker Mix", "MIC12SPK Playback Switch", "MIC1 PGA"},
380 {"Speaker Mix", "MIC22SPK Playback Switch", "MIC2 PGA"},
381 {"Speaker Mix", "DAC2SPK Playback Switch", "I2S Mix"},
382
383 /* mono mixer */
384 {"Mono Mix", "ADC2MONO_L Playback Switch", "Left Capture Mix"},
385 {"Mono Mix", "ADC2MONO_R Playback Switch", "Right Capture Mix"},
386 {"Mono Mix", "LI2MONO Playback Switch", "Line Mix"},
387 {"Mono Mix", "AUXI2MONO Playback Switch", "AuxI Mix"},
388 {"Mono Mix", "MIC12MONO Playback Switch", "MIC1 PGA"},
389 {"Mono Mix", "MIC22MONO Playback Switch", "MIC2 PGA"},
390 {"Mono Mix", "DAC2MONO Playback Switch", "I2S Mix"},
391
392 /* Left record mixer */
393 {"Left Capture Mix", "LineInL Capture Switch", "LINEINL"},
394 {"Left Capture Mix", "Left AuxI Capture Switch", "AUXINL"},
395 {"Left Capture Mix", "Mic1 Capture Switch", "MIC1 Pre Amp"},
396 {"Left Capture Mix", "Mic2 Capture Switch", "MIC2 Pre Amp"},
397 {"Left Capture Mix", "HPMixerL Capture Switch", "HPL Mix"},
398 {"Left Capture Mix", "SPKMixer Capture Switch", "Speaker Mix"},
399 {"Left Capture Mix", "MonoMixer Capture Switch", "Mono Mix"},
400
401 /*Right record mixer */
402 {"Right Capture Mix", "LineInR Capture Switch", "LINEINR"},
403 {"Right Capture Mix", "Right AuxI Capture Switch", "AUXINR"},
404 {"Right Capture Mix", "Mic1 Capture Switch", "MIC1 Pre Amp"},
405 {"Right Capture Mix", "Mic2 Capture Switch", "MIC2 Pre Amp"},
406 {"Right Capture Mix", "HPMixerR Capture Switch", "HPR Mix"},
407 {"Right Capture Mix", "SPKMixer Capture Switch", "Speaker Mix"},
408 {"Right Capture Mix", "MonoMixer Capture Switch", "Mono Mix"},
409
410 /* headphone left mux */
411 {"Left Headphone Mux", "HP Left Mix", "HPL Mix"},
412 {"Left Headphone Mux", "Vmid", "Vmid"},
413
414 /* headphone right mux */
415 {"Right Headphone Mux", "HP Right Mix", "HPR Mix"},
416 {"Right Headphone Mux", "Vmid", "Vmid"},
417
418 /* speaker out mux */
419 {"SpeakerOut Mux", "Vmid", "Vmid"},
420 {"SpeakerOut Mux", "HPOut Mix", "HPOut Mix"},
421 {"SpeakerOut Mux", "Speaker Mix", "Speaker Mix"},
422 {"SpeakerOut Mux", "Mono Mix", "Mono Mix"},
423
424 /* Mono/Aux Out mux */
425 {"AuxOut Mux", "Vmid", "Vmid"},
426 {"AuxOut Mux", "HPOut Mix", "HPOut Mix"},
427 {"AuxOut Mux", "Speaker Mix", "Speaker Mix"},
428 {"AuxOut Mux", "Mono Mix", "Mono Mix"},
429
430 /* output pga */
431 {"HPL", NULL, "Left Headphone"},
432 {"Left Headphone", NULL, "Left Headphone Mux"},
433 {"HPR", NULL, "Right Headphone"},
434 {"Right Headphone", NULL, "Right Headphone Mux"},
435 {"Left AuxOut", NULL, "AuxOut Mux"},
436 {"Right AuxOut", NULL, "AuxOut Mux"},
437
438 /* input pga */
439 {"Left LineIn", NULL, "LINEINL"},
440 {"Right LineIn", NULL, "LINEINR"},
441 {"Left AuxI", NULL, "AUXINL"},
442 {"Right AuxI", NULL, "AUXINR"},
443 {"MIC1 Pre Amp", NULL, "MIC1"},
444 {"MIC2 Pre Amp", NULL, "MIC2"},
445 {"MIC1 PGA", NULL, "MIC1 Pre Amp"},
446 {"MIC2 PGA", NULL, "MIC2 Pre Amp"},
447
448 /* left ADC */
449 {"Left ADC", NULL, "Left Capture Mix"},
450
451 /* right ADC */
452 {"Right ADC", NULL, "Right Capture Mix"},
453
454 {"SpeakerOut N Mux", "RN/-R", "SpeakerOut"},
455 {"SpeakerOut N Mux", "RP/+R", "SpeakerOut"},
456 {"SpeakerOut N Mux", "LN/-R", "SpeakerOut"},
457 {"SpeakerOut N Mux", "Vmid", "Vmid"},
458
459 {"SPKOUT", NULL, "SpeakerOut"},
460 {"SPKOUTN", NULL, "SpeakerOut N Mux"},
461};
462
463static const struct snd_soc_dapm_route intercon_spk[] = {
464 {"SpeakerOut", NULL, "SpeakerOut Mux"},
465};
466
467static const struct snd_soc_dapm_route intercon_amp_spk[] = {
468 {"AB Amp", NULL, "SpeakerOut Mux"},
469 {"D Amp", NULL, "SpeakerOut Mux"},
470 {"AB-D Amp Mux", "AB Amp", "AB Amp"},
471 {"AB-D Amp Mux", "D Amp", "D Amp"},
472 {"SpeakerOut", NULL, "AB-D Amp Mux"},
473};
474
475/* PLL divisors */
476struct _pll_div {
477 u32 pll_in;
478 u32 pll_out;
479 u16 regvalue;
480};
481
482/* Note : pll code from original alc5623 driver. Not sure of how good it is */
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300483/* useful only for master mode */
Arnaud Patard (Rtp)6f4bc952010-10-21 19:40:02 +0200484static const struct _pll_div codec_master_pll_div[] = {
485
486 { 2048000, 8192000, 0x0ea0},
487 { 3686400, 8192000, 0x4e27},
488 { 12000000, 8192000, 0x456b},
489 { 13000000, 8192000, 0x495f},
490 { 13100000, 8192000, 0x0320},
491 { 2048000, 11289600, 0xf637},
492 { 3686400, 11289600, 0x2f22},
493 { 12000000, 11289600, 0x3e2f},
494 { 13000000, 11289600, 0x4d5b},
495 { 13100000, 11289600, 0x363b},
496 { 2048000, 16384000, 0x1ea0},
497 { 3686400, 16384000, 0x9e27},
498 { 12000000, 16384000, 0x452b},
499 { 13000000, 16384000, 0x542f},
500 { 13100000, 16384000, 0x03a0},
501 { 2048000, 16934400, 0xe625},
502 { 3686400, 16934400, 0x9126},
503 { 12000000, 16934400, 0x4d2c},
504 { 13000000, 16934400, 0x742f},
505 { 13100000, 16934400, 0x3c27},
506 { 2048000, 22579200, 0x2aa0},
507 { 3686400, 22579200, 0x2f20},
508 { 12000000, 22579200, 0x7e2f},
509 { 13000000, 22579200, 0x742f},
510 { 13100000, 22579200, 0x3c27},
511 { 2048000, 24576000, 0x2ea0},
512 { 3686400, 24576000, 0xee27},
513 { 12000000, 24576000, 0x2915},
514 { 13000000, 24576000, 0x772e},
515 { 13100000, 24576000, 0x0d20},
516};
517
518static const struct _pll_div codec_slave_pll_div[] = {
519
520 { 1024000, 16384000, 0x3ea0},
521 { 1411200, 22579200, 0x3ea0},
522 { 1536000, 24576000, 0x3ea0},
523 { 2048000, 16384000, 0x1ea0},
524 { 2822400, 22579200, 0x1ea0},
525 { 3072000, 24576000, 0x1ea0},
526
527};
528
529static int alc5623_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id,
530 int source, unsigned int freq_in, unsigned int freq_out)
531{
532 int i;
533 struct snd_soc_codec *codec = codec_dai->codec;
534 int gbl_clk = 0, pll_div = 0;
535 u16 reg;
536
537 if (pll_id < ALC5623_PLL_FR_MCLK || pll_id > ALC5623_PLL_FR_BCK)
538 return -ENODEV;
539
540 /* Disable PLL power */
541 snd_soc_update_bits(codec, ALC5623_PWR_MANAG_ADD2,
542 ALC5623_PWR_ADD2_PLL,
543 0);
544
545 /* pll is not used in slave mode */
546 reg = snd_soc_read(codec, ALC5623_DAI_CONTROL);
547 if (reg & ALC5623_DAI_SDP_SLAVE_MODE)
548 return 0;
549
550 if (!freq_in || !freq_out)
551 return 0;
552
553 switch (pll_id) {
554 case ALC5623_PLL_FR_MCLK:
555 for (i = 0; i < ARRAY_SIZE(codec_master_pll_div); i++) {
556 if (codec_master_pll_div[i].pll_in == freq_in
557 && codec_master_pll_div[i].pll_out == freq_out) {
558 /* PLL source from MCLK */
559 pll_div = codec_master_pll_div[i].regvalue;
560 break;
561 }
562 }
563 break;
564 case ALC5623_PLL_FR_BCK:
565 for (i = 0; i < ARRAY_SIZE(codec_slave_pll_div); i++) {
566 if (codec_slave_pll_div[i].pll_in == freq_in
567 && codec_slave_pll_div[i].pll_out == freq_out) {
568 /* PLL source from Bitclk */
569 gbl_clk = ALC5623_GBL_CLK_PLL_SOUR_SEL_BITCLK;
570 pll_div = codec_slave_pll_div[i].regvalue;
571 break;
572 }
573 }
574 break;
575 default:
576 return -EINVAL;
577 }
578
579 if (!pll_div)
580 return -EINVAL;
581
582 snd_soc_write(codec, ALC5623_GLOBAL_CLK_CTRL_REG, gbl_clk);
583 snd_soc_write(codec, ALC5623_PLL_CTRL, pll_div);
584 snd_soc_update_bits(codec, ALC5623_PWR_MANAG_ADD2,
585 ALC5623_PWR_ADD2_PLL,
586 ALC5623_PWR_ADD2_PLL);
587 gbl_clk |= ALC5623_GBL_CLK_SYS_SOUR_SEL_PLL;
588 snd_soc_write(codec, ALC5623_GLOBAL_CLK_CTRL_REG, gbl_clk);
589
590 return 0;
591}
592
593struct _coeff_div {
594 u16 fs;
595 u16 regvalue;
596};
597
598/* codec hifi mclk (after PLL) clock divider coefficients */
599/* values inspired from column BCLK=32Fs of Appendix A table */
600static const struct _coeff_div coeff_div[] = {
601 {256*8, 0x3a69},
602 {384*8, 0x3c6b},
603 {256*4, 0x2a69},
604 {384*4, 0x2c6b},
605 {256*2, 0x1a69},
606 {384*2, 0x1c6b},
607 {256*1, 0x0a69},
608 {384*1, 0x0c6b},
609};
610
611static int get_coeff(struct snd_soc_codec *codec, int rate)
612{
613 struct alc5623_priv *alc5623 = snd_soc_codec_get_drvdata(codec);
614 int i;
615
616 for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
617 if (coeff_div[i].fs * rate == alc5623->sysclk)
618 return i;
619 }
620 return -EINVAL;
621}
622
623/*
624 * Clock after PLL and dividers
625 */
626static int alc5623_set_dai_sysclk(struct snd_soc_dai *codec_dai,
627 int clk_id, unsigned int freq, int dir)
628{
629 struct snd_soc_codec *codec = codec_dai->codec;
630 struct alc5623_priv *alc5623 = snd_soc_codec_get_drvdata(codec);
631
632 switch (freq) {
633 case 8192000:
634 case 11289600:
635 case 12288000:
636 case 16384000:
637 case 16934400:
638 case 18432000:
639 case 22579200:
640 case 24576000:
641 alc5623->sysclk = freq;
642 return 0;
643 }
644 return -EINVAL;
645}
646
647static int alc5623_set_dai_fmt(struct snd_soc_dai *codec_dai,
648 unsigned int fmt)
649{
650 struct snd_soc_codec *codec = codec_dai->codec;
651 u16 iface = 0;
652
653 /* set master/slave audio interface */
654 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
655 case SND_SOC_DAIFMT_CBM_CFM:
656 iface = ALC5623_DAI_SDP_MASTER_MODE;
657 break;
658 case SND_SOC_DAIFMT_CBS_CFS:
659 iface = ALC5623_DAI_SDP_SLAVE_MODE;
660 break;
661 default:
662 return -EINVAL;
663 }
664
665 /* interface format */
666 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
667 case SND_SOC_DAIFMT_I2S:
668 iface |= ALC5623_DAI_I2S_DF_I2S;
669 break;
670 case SND_SOC_DAIFMT_RIGHT_J:
671 iface |= ALC5623_DAI_I2S_DF_RIGHT;
672 break;
673 case SND_SOC_DAIFMT_LEFT_J:
674 iface |= ALC5623_DAI_I2S_DF_LEFT;
675 break;
676 case SND_SOC_DAIFMT_DSP_A:
677 iface |= ALC5623_DAI_I2S_DF_PCM;
678 break;
679 case SND_SOC_DAIFMT_DSP_B:
680 iface |= ALC5623_DAI_I2S_DF_PCM | ALC5623_DAI_I2S_PCM_MODE;
681 break;
682 default:
683 return -EINVAL;
684 }
685
686 /* clock inversion */
687 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
688 case SND_SOC_DAIFMT_NB_NF:
689 break;
690 case SND_SOC_DAIFMT_IB_IF:
691 iface |= ALC5623_DAI_MAIN_I2S_BCLK_POL_CTRL;
692 break;
693 case SND_SOC_DAIFMT_IB_NF:
694 iface |= ALC5623_DAI_MAIN_I2S_BCLK_POL_CTRL;
695 break;
696 case SND_SOC_DAIFMT_NB_IF:
697 break;
698 default:
699 return -EINVAL;
700 }
701
702 return snd_soc_write(codec, ALC5623_DAI_CONTROL, iface);
703}
704
705static int alc5623_pcm_hw_params(struct snd_pcm_substream *substream,
706 struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
707{
Mark Browne6968a12012-04-04 15:58:16 +0100708 struct snd_soc_codec *codec = dai->codec;
Arnaud Patard (Rtp)6f4bc952010-10-21 19:40:02 +0200709 struct alc5623_priv *alc5623 = snd_soc_codec_get_drvdata(codec);
710 int coeff, rate;
711 u16 iface;
712
713 iface = snd_soc_read(codec, ALC5623_DAI_CONTROL);
714 iface &= ~ALC5623_DAI_I2S_DL_MASK;
715
716 /* bit size */
717 switch (params_format(params)) {
718 case SNDRV_PCM_FORMAT_S16_LE:
719 iface |= ALC5623_DAI_I2S_DL_16;
720 break;
721 case SNDRV_PCM_FORMAT_S20_3LE:
722 iface |= ALC5623_DAI_I2S_DL_20;
723 break;
724 case SNDRV_PCM_FORMAT_S24_LE:
725 iface |= ALC5623_DAI_I2S_DL_24;
726 break;
727 case SNDRV_PCM_FORMAT_S32_LE:
728 iface |= ALC5623_DAI_I2S_DL_32;
729 break;
730 default:
731 return -EINVAL;
732 }
733
734 /* set iface & srate */
735 snd_soc_write(codec, ALC5623_DAI_CONTROL, iface);
736 rate = params_rate(params);
737 coeff = get_coeff(codec, rate);
738 if (coeff < 0)
739 return -EINVAL;
740
741 coeff = coeff_div[coeff].regvalue;
742 dev_dbg(codec->dev, "%s: sysclk=%d,rate=%d,coeff=0x%04x\n",
743 __func__, alc5623->sysclk, rate, coeff);
744 snd_soc_write(codec, ALC5623_STEREO_AD_DA_CLK_CTRL, coeff);
745
746 return 0;
747}
748
749static int alc5623_mute(struct snd_soc_dai *dai, int mute)
750{
751 struct snd_soc_codec *codec = dai->codec;
752 u16 hp_mute = ALC5623_MISC_M_DAC_L_INPUT | ALC5623_MISC_M_DAC_R_INPUT;
753 u16 mute_reg = snd_soc_read(codec, ALC5623_MISC_CTRL) & ~hp_mute;
754
755 if (mute)
756 mute_reg |= hp_mute;
757
758 return snd_soc_write(codec, ALC5623_MISC_CTRL, mute_reg);
759}
760
761#define ALC5623_ADD2_POWER_EN (ALC5623_PWR_ADD2_VREF \
762 | ALC5623_PWR_ADD2_DAC_REF_CIR)
763
764#define ALC5623_ADD3_POWER_EN (ALC5623_PWR_ADD3_MAIN_BIAS \
765 | ALC5623_PWR_ADD3_MIC1_BOOST_AD)
766
767#define ALC5623_ADD1_POWER_EN \
768 (ALC5623_PWR_ADD1_SHORT_CURR_DET_EN | ALC5623_PWR_ADD1_SOFTGEN_EN \
769 | ALC5623_PWR_ADD1_DEPOP_BUF_HP | ALC5623_PWR_ADD1_HP_OUT_AMP \
770 | ALC5623_PWR_ADD1_HP_OUT_ENH_AMP)
771
772#define ALC5623_ADD1_POWER_EN_5622 \
773 (ALC5623_PWR_ADD1_SHORT_CURR_DET_EN \
774 | ALC5623_PWR_ADD1_HP_OUT_AMP)
775
776static void enable_power_depop(struct snd_soc_codec *codec)
777{
778 struct alc5623_priv *alc5623 = snd_soc_codec_get_drvdata(codec);
779
780 snd_soc_update_bits(codec, ALC5623_PWR_MANAG_ADD1,
781 ALC5623_PWR_ADD1_SOFTGEN_EN,
782 ALC5623_PWR_ADD1_SOFTGEN_EN);
783
784 snd_soc_write(codec, ALC5623_PWR_MANAG_ADD3, ALC5623_ADD3_POWER_EN);
785
786 snd_soc_update_bits(codec, ALC5623_MISC_CTRL,
787 ALC5623_MISC_HP_DEPOP_MODE2_EN,
788 ALC5623_MISC_HP_DEPOP_MODE2_EN);
789
790 msleep(500);
791
792 snd_soc_write(codec, ALC5623_PWR_MANAG_ADD2, ALC5623_ADD2_POWER_EN);
793
794 /* avoid writing '1' into 5622 reserved bits */
795 if (alc5623->id == 0x22)
796 snd_soc_write(codec, ALC5623_PWR_MANAG_ADD1,
797 ALC5623_ADD1_POWER_EN_5622);
798 else
799 snd_soc_write(codec, ALC5623_PWR_MANAG_ADD1,
800 ALC5623_ADD1_POWER_EN);
801
802 /* disable HP Depop2 */
803 snd_soc_update_bits(codec, ALC5623_MISC_CTRL,
804 ALC5623_MISC_HP_DEPOP_MODE2_EN,
805 0);
806
807}
808
809static int alc5623_set_bias_level(struct snd_soc_codec *codec,
810 enum snd_soc_bias_level level)
811{
812 switch (level) {
813 case SND_SOC_BIAS_ON:
814 enable_power_depop(codec);
815 break;
816 case SND_SOC_BIAS_PREPARE:
817 break;
818 case SND_SOC_BIAS_STANDBY:
819 /* everything off except vref/vmid, */
820 snd_soc_write(codec, ALC5623_PWR_MANAG_ADD2,
821 ALC5623_PWR_ADD2_VREF);
822 snd_soc_write(codec, ALC5623_PWR_MANAG_ADD3,
823 ALC5623_PWR_ADD3_MAIN_BIAS);
824 break;
825 case SND_SOC_BIAS_OFF:
826 /* everything off, dac mute, inactive */
827 snd_soc_write(codec, ALC5623_PWR_MANAG_ADD2, 0);
828 snd_soc_write(codec, ALC5623_PWR_MANAG_ADD3, 0);
829 snd_soc_write(codec, ALC5623_PWR_MANAG_ADD1, 0);
830 break;
831 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200832 codec->dapm.bias_level = level;
Arnaud Patard (Rtp)6f4bc952010-10-21 19:40:02 +0200833 return 0;
834}
835
836#define ALC5623_FORMATS (SNDRV_PCM_FMTBIT_S16_LE \
837 | SNDRV_PCM_FMTBIT_S24_LE \
838 | SNDRV_PCM_FMTBIT_S32_LE)
839
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100840static const struct snd_soc_dai_ops alc5623_dai_ops = {
Arnaud Patard (Rtp)6f4bc952010-10-21 19:40:02 +0200841 .hw_params = alc5623_pcm_hw_params,
842 .digital_mute = alc5623_mute,
843 .set_fmt = alc5623_set_dai_fmt,
844 .set_sysclk = alc5623_set_dai_sysclk,
845 .set_pll = alc5623_set_dai_pll,
846};
847
848static struct snd_soc_dai_driver alc5623_dai = {
849 .name = "alc5623-hifi",
850 .playback = {
851 .stream_name = "Playback",
852 .channels_min = 1,
853 .channels_max = 2,
854 .rate_min = 8000,
855 .rate_max = 48000,
856 .rates = SNDRV_PCM_RATE_8000_48000,
857 .formats = ALC5623_FORMATS,},
858 .capture = {
859 .stream_name = "Capture",
860 .channels_min = 1,
861 .channels_max = 2,
862 .rate_min = 8000,
863 .rate_max = 48000,
864 .rates = SNDRV_PCM_RATE_8000_48000,
865 .formats = ALC5623_FORMATS,},
866
867 .ops = &alc5623_dai_ops,
868};
869
Lars-Peter Clausen84b315e2011-12-02 10:18:28 +0100870static int alc5623_suspend(struct snd_soc_codec *codec)
Arnaud Patard (Rtp)6f4bc952010-10-21 19:40:02 +0200871{
872 alc5623_set_bias_level(codec, SND_SOC_BIAS_OFF);
873 return 0;
874}
875
876static int alc5623_resume(struct snd_soc_codec *codec)
877{
878 int i, step = codec->driver->reg_cache_step;
879 u16 *cache = codec->reg_cache;
880
881 /* Sync reg_cache with the hardware */
882 for (i = 2 ; i < codec->driver->reg_cache_size ; i += step)
883 snd_soc_write(codec, i, cache[i]);
884
885 alc5623_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
886
887 /* charge alc5623 caps */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200888 if (codec->dapm.suspend_bias_level == SND_SOC_BIAS_ON) {
Arnaud Patard (Rtp)6f4bc952010-10-21 19:40:02 +0200889 alc5623_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200890 codec->dapm.bias_level = SND_SOC_BIAS_ON;
891 alc5623_set_bias_level(codec, codec->dapm.bias_level);
Arnaud Patard (Rtp)6f4bc952010-10-21 19:40:02 +0200892 }
893
894 return 0;
895}
896
897static int alc5623_probe(struct snd_soc_codec *codec)
898{
899 struct alc5623_priv *alc5623 = snd_soc_codec_get_drvdata(codec);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200900 struct snd_soc_dapm_context *dapm = &codec->dapm;
Arnaud Patard (Rtp)6f4bc952010-10-21 19:40:02 +0200901 int ret;
902
903 ret = snd_soc_codec_set_cache_io(codec, 8, 16, alc5623->control_type);
904 if (ret < 0) {
905 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
906 return ret;
907 }
908
909 alc5623_reset(codec);
910 alc5623_fill_cache(codec);
911
912 /* power on device */
913 alc5623_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
914
915 if (alc5623->add_ctrl) {
916 snd_soc_write(codec, ALC5623_ADD_CTRL_REG,
917 alc5623->add_ctrl);
918 }
919
920 if (alc5623->jack_det_ctrl) {
921 snd_soc_write(codec, ALC5623_JACK_DET_CTRL,
922 alc5623->jack_det_ctrl);
923 }
924
925 switch (alc5623->id) {
Arnaud Patard (Rtp)6f4bc952010-10-21 19:40:02 +0200926 case 0x21:
Liam Girdwood022658b2012-02-03 17:43:09 +0000927 snd_soc_add_codec_controls(codec, alc5621_vol_snd_controls,
Axel Lin6048ef72011-12-15 11:57:57 +0800928 ARRAY_SIZE(alc5621_vol_snd_controls));
Arnaud Patard (Rtp)6f4bc952010-10-21 19:40:02 +0200929 break;
930 case 0x22:
Liam Girdwood022658b2012-02-03 17:43:09 +0000931 snd_soc_add_codec_controls(codec, alc5622_vol_snd_controls,
Axel Lin6048ef72011-12-15 11:57:57 +0800932 ARRAY_SIZE(alc5622_vol_snd_controls));
Arnaud Patard (Rtp)6f4bc952010-10-21 19:40:02 +0200933 break;
934 case 0x23:
Liam Girdwood022658b2012-02-03 17:43:09 +0000935 snd_soc_add_codec_controls(codec, alc5623_vol_snd_controls,
Arnaud Patard (Rtp)6f4bc952010-10-21 19:40:02 +0200936 ARRAY_SIZE(alc5623_vol_snd_controls));
937 break;
Axel Lin4bd3a1f2010-12-03 17:25:57 +0800938 default:
939 return -EINVAL;
Arnaud Patard (Rtp)6f4bc952010-10-21 19:40:02 +0200940 }
941
Liam Girdwood022658b2012-02-03 17:43:09 +0000942 snd_soc_add_codec_controls(codec, alc5623_snd_controls,
Arnaud Patard (Rtp)6f4bc952010-10-21 19:40:02 +0200943 ARRAY_SIZE(alc5623_snd_controls));
944
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200945 snd_soc_dapm_new_controls(dapm, alc5623_dapm_widgets,
Arnaud Patard (Rtp)6f4bc952010-10-21 19:40:02 +0200946 ARRAY_SIZE(alc5623_dapm_widgets));
947
948 /* set up audio path interconnects */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200949 snd_soc_dapm_add_routes(dapm, intercon, ARRAY_SIZE(intercon));
Arnaud Patard (Rtp)6f4bc952010-10-21 19:40:02 +0200950
951 switch (alc5623->id) {
Arnaud Patard (Rtp)6f4bc952010-10-21 19:40:02 +0200952 case 0x21:
953 case 0x22:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200954 snd_soc_dapm_new_controls(dapm, alc5623_dapm_amp_widgets,
Arnaud Patard (Rtp)6f4bc952010-10-21 19:40:02 +0200955 ARRAY_SIZE(alc5623_dapm_amp_widgets));
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200956 snd_soc_dapm_add_routes(dapm, intercon_amp_spk,
957 ARRAY_SIZE(intercon_amp_spk));
Arnaud Patard (Rtp)6f4bc952010-10-21 19:40:02 +0200958 break;
959 case 0x23:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200960 snd_soc_dapm_add_routes(dapm, intercon_spk,
961 ARRAY_SIZE(intercon_spk));
Arnaud Patard (Rtp)6f4bc952010-10-21 19:40:02 +0200962 break;
Axel Lin4bd3a1f2010-12-03 17:25:57 +0800963 default:
964 return -EINVAL;
Arnaud Patard (Rtp)6f4bc952010-10-21 19:40:02 +0200965 }
966
967 return ret;
968}
969
970/* power down chip */
971static int alc5623_remove(struct snd_soc_codec *codec)
972{
973 alc5623_set_bias_level(codec, SND_SOC_BIAS_OFF);
974 return 0;
975}
976
977static struct snd_soc_codec_driver soc_codec_device_alc5623 = {
978 .probe = alc5623_probe,
979 .remove = alc5623_remove,
980 .suspend = alc5623_suspend,
981 .resume = alc5623_resume,
982 .set_bias_level = alc5623_set_bias_level,
983 .reg_cache_size = ALC5623_VENDOR_ID2+2,
984 .reg_word_size = sizeof(u16),
985 .reg_cache_step = 2,
986};
987
988/*
989 * ALC5623 2 wire address is determined by A1 pin
990 * state during powerup.
991 * low = 0x1a
992 * high = 0x1b
993 */
Axel Lin5091f5b2012-01-23 11:18:17 +0800994static __devinit int alc5623_i2c_probe(struct i2c_client *client,
Arnaud Patard (Rtp)6f4bc952010-10-21 19:40:02 +0200995 const struct i2c_device_id *id)
996{
997 struct alc5623_platform_data *pdata;
998 struct alc5623_priv *alc5623;
999 int ret, vid1, vid2;
1000
1001 vid1 = i2c_smbus_read_word_data(client, ALC5623_VENDOR_ID1);
1002 if (vid1 < 0) {
1003 dev_err(&client->dev, "failed to read I2C\n");
1004 return -EIO;
1005 }
1006 vid1 = ((vid1 & 0xff) << 8) | (vid1 >> 8);
1007
1008 vid2 = i2c_smbus_read_byte_data(client, ALC5623_VENDOR_ID2);
1009 if (vid2 < 0) {
1010 dev_err(&client->dev, "failed to read I2C\n");
1011 return -EIO;
1012 }
1013
1014 if ((vid1 != 0x10ec) || (vid2 != id->driver_data)) {
1015 dev_err(&client->dev, "unknown or wrong codec\n");
1016 dev_err(&client->dev, "Expected %x:%lx, got %x:%x\n",
1017 0x10ec, id->driver_data,
1018 vid1, vid2);
1019 return -ENODEV;
1020 }
1021
1022 dev_dbg(&client->dev, "Found codec id : alc56%02x\n", vid2);
1023
Axel Lin360b70c2011-12-29 11:56:23 +08001024 alc5623 = devm_kzalloc(&client->dev, sizeof(struct alc5623_priv),
1025 GFP_KERNEL);
Axel Linc7b64292010-11-22 08:34:07 +08001026 if (alc5623 == NULL)
1027 return -ENOMEM;
Arnaud Patard (Rtp)6f4bc952010-10-21 19:40:02 +02001028
1029 pdata = client->dev.platform_data;
1030 if (pdata) {
1031 alc5623->add_ctrl = pdata->add_ctrl;
1032 alc5623->jack_det_ctrl = pdata->jack_det_ctrl;
1033 }
1034
1035 alc5623->id = vid2;
1036 switch (alc5623->id) {
1037 case 0x21:
1038 alc5623_dai.name = "alc5621-hifi";
1039 break;
1040 case 0x22:
1041 alc5623_dai.name = "alc5622-hifi";
1042 break;
Arnaud Patard (Rtp)6f4bc952010-10-21 19:40:02 +02001043 case 0x23:
1044 alc5623_dai.name = "alc5623-hifi";
1045 break;
Axel Lin4bd3a1f2010-12-03 17:25:57 +08001046 default:
Axel Lin4bd3a1f2010-12-03 17:25:57 +08001047 return -EINVAL;
Arnaud Patard (Rtp)6f4bc952010-10-21 19:40:02 +02001048 }
1049
1050 i2c_set_clientdata(client, alc5623);
Arnaud Patard (Rtp)6f4bc952010-10-21 19:40:02 +02001051 alc5623->control_type = SND_SOC_I2C;
Arnaud Patard (Rtp)6f4bc952010-10-21 19:40:02 +02001052
1053 ret = snd_soc_register_codec(&client->dev,
1054 &soc_codec_device_alc5623, &alc5623_dai, 1);
Axel Lin360b70c2011-12-29 11:56:23 +08001055 if (ret != 0)
Arnaud Patard (Rtp)6f4bc952010-10-21 19:40:02 +02001056 dev_err(&client->dev, "Failed to register codec: %d\n", ret);
Arnaud Patard (Rtp)6f4bc952010-10-21 19:40:02 +02001057
Arnaud Patard (Rtp)6f4bc952010-10-21 19:40:02 +02001058 return ret;
1059}
1060
Axel Lin5091f5b2012-01-23 11:18:17 +08001061static __devexit int alc5623_i2c_remove(struct i2c_client *client)
Arnaud Patard (Rtp)6f4bc952010-10-21 19:40:02 +02001062{
Arnaud Patard (Rtp)6f4bc952010-10-21 19:40:02 +02001063 snd_soc_unregister_codec(&client->dev);
Arnaud Patard (Rtp)6f4bc952010-10-21 19:40:02 +02001064 return 0;
1065}
1066
1067static const struct i2c_device_id alc5623_i2c_table[] = {
1068 {"alc5621", 0x21},
1069 {"alc5622", 0x22},
1070 {"alc5623", 0x23},
1071 {}
1072};
1073MODULE_DEVICE_TABLE(i2c, alc5623_i2c_table);
1074
1075/* i2c codec control layer */
1076static struct i2c_driver alc5623_i2c_driver = {
1077 .driver = {
1078 .name = "alc562x-codec",
1079 .owner = THIS_MODULE,
1080 },
1081 .probe = alc5623_i2c_probe,
1082 .remove = __devexit_p(alc5623_i2c_remove),
1083 .id_table = alc5623_i2c_table,
1084};
1085
Axel Lin9c78a012012-05-08 23:42:23 +08001086module_i2c_driver(alc5623_i2c_driver);
Arnaud Patard (Rtp)6f4bc952010-10-21 19:40:02 +02001087
1088MODULE_DESCRIPTION("ASoC alc5621/2/3 driver");
1089MODULE_AUTHOR("Arnaud Patard <arnaud.patard@rtp-net.org>");
1090MODULE_LICENSE("GPL");