blob: d8d68f5b61316312d95113f1a693ebfe8bb8eed1 [file] [log] [blame]
Matt2f2f4252005-04-13 14:45:30 +02001/*
2 * Universal Interface for Intel High Definition Audio Codec
3 *
4 * HD audio interface patch for SigmaTel STAC92xx
5 *
6 * Copyright (c) 2005 Embedded Alley Solutions, Inc.
7 * <matt@embeddedalley.com>
8 *
9 * Based on patch_cmedia.c and patch_realtek.c
10 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
11 *
12 * This driver is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This driver is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 */
26
27#include <sound/driver.h>
28#include <linux/init.h>
29#include <linux/delay.h>
30#include <linux/slab.h>
31#include <linux/pci.h>
32#include <sound/core.h>
Mattc7d4b2f2005-06-27 14:59:41 +020033#include <sound/asoundef.h>
Matt2f2f4252005-04-13 14:45:30 +020034#include "hda_codec.h"
35#include "hda_local.h"
36
37#undef STAC_TEST
38
Matt4e550962005-07-04 17:51:39 +020039#define NUM_CONTROL_ALLOC 32
40#define STAC_HP_EVENT 0x37
41#define STAC_UNSOL_ENABLE (AC_USRSP_EN | STAC_HP_EVENT)
42
Matt2f2f4252005-04-13 14:45:30 +020043struct sigmatel_spec {
Takashi Iwaic8b6bf92005-11-17 14:57:47 +010044 struct snd_kcontrol_new *mixers[4];
Mattc7d4b2f2005-06-27 14:59:41 +020045 unsigned int num_mixers;
46
47 unsigned int surr_switch: 1;
48
Matt2f2f4252005-04-13 14:45:30 +020049 /* playback */
50 struct hda_multi_out multiout;
Mattc7d4b2f2005-06-27 14:59:41 +020051 hda_nid_t dac_nids[4];
Matt2f2f4252005-04-13 14:45:30 +020052
53 /* capture */
54 hda_nid_t *adc_nids;
Matt2f2f4252005-04-13 14:45:30 +020055 unsigned int num_adcs;
Mattdabbed62005-06-14 10:19:34 +020056 hda_nid_t *mux_nids;
57 unsigned int num_muxes;
Mattdabbed62005-06-14 10:19:34 +020058 hda_nid_t dig_in_nid;
Matt2f2f4252005-04-13 14:45:30 +020059
Mattc7d4b2f2005-06-27 14:59:41 +020060#ifdef STAC_TEST
Matt2f2f4252005-04-13 14:45:30 +020061 /* pin widgets */
62 hda_nid_t *pin_nids;
63 unsigned int num_pins;
Matt2f2f4252005-04-13 14:45:30 +020064 unsigned int *pin_configs;
65#endif
66
67 /* codec specific stuff */
68 struct hda_verb *init;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +010069 struct snd_kcontrol_new *mixer;
Matt2f2f4252005-04-13 14:45:30 +020070
71 /* capture source */
Mattc7d4b2f2005-06-27 14:59:41 +020072 struct hda_input_mux *input_mux;
Matt2f2f4252005-04-13 14:45:30 +020073 unsigned int cur_mux[2];
74
75 /* channel mode */
76 unsigned int num_ch_modes;
77 unsigned int cur_ch_mode;
Matt2f2f4252005-04-13 14:45:30 +020078
Mattc7d4b2f2005-06-27 14:59:41 +020079 struct hda_pcm pcm_rec[2]; /* PCM information */
80
81 /* dynamic controls and input_mux */
82 struct auto_pin_cfg autocfg;
83 unsigned int num_kctl_alloc, num_kctl_used;
Takashi Iwaic8b6bf92005-11-17 14:57:47 +010084 struct snd_kcontrol_new *kctl_alloc;
Mattc7d4b2f2005-06-27 14:59:41 +020085 struct hda_input_mux private_imux;
Matt2f2f4252005-04-13 14:45:30 +020086};
87
88static hda_nid_t stac9200_adc_nids[1] = {
89 0x03,
90};
91
92static hda_nid_t stac9200_mux_nids[1] = {
93 0x0c,
94};
95
96static hda_nid_t stac9200_dac_nids[1] = {
97 0x02,
98};
99
Matt2f2f4252005-04-13 14:45:30 +0200100static hda_nid_t stac922x_adc_nids[2] = {
101 0x06, 0x07,
102};
103
104static hda_nid_t stac922x_mux_nids[2] = {
105 0x12, 0x13,
106};
107
Mattc7d4b2f2005-06-27 14:59:41 +0200108#ifdef STAC_TEST
109static hda_nid_t stac9200_pin_nids[8] = {
110 0x08, 0x09, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12,
Matt2f2f4252005-04-13 14:45:30 +0200111};
112
113static hda_nid_t stac922x_pin_nids[10] = {
114 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
115 0x0f, 0x10, 0x11, 0x15, 0x1b,
116};
Mattc7d4b2f2005-06-27 14:59:41 +0200117#endif
Matt2f2f4252005-04-13 14:45:30 +0200118
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100119static int stac92xx_mux_enum_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Matt2f2f4252005-04-13 14:45:30 +0200120{
121 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
122 struct sigmatel_spec *spec = codec->spec;
Mattc7d4b2f2005-06-27 14:59:41 +0200123 return snd_hda_input_mux_info(spec->input_mux, uinfo);
Matt2f2f4252005-04-13 14:45:30 +0200124}
125
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100126static int stac92xx_mux_enum_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Matt2f2f4252005-04-13 14:45:30 +0200127{
128 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
129 struct sigmatel_spec *spec = codec->spec;
130 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
131
132 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
133 return 0;
134}
135
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100136static int stac92xx_mux_enum_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Matt2f2f4252005-04-13 14:45:30 +0200137{
138 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
139 struct sigmatel_spec *spec = codec->spec;
140 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
141
Mattc7d4b2f2005-06-27 14:59:41 +0200142 return snd_hda_input_mux_put(codec, spec->input_mux, ucontrol,
Matt2f2f4252005-04-13 14:45:30 +0200143 spec->mux_nids[adc_idx], &spec->cur_mux[adc_idx]);
144}
145
Mattc7d4b2f2005-06-27 14:59:41 +0200146static struct hda_verb stac9200_core_init[] = {
Matt2f2f4252005-04-13 14:45:30 +0200147 /* set dac0mux for dac converter */
Mattc7d4b2f2005-06-27 14:59:41 +0200148 { 0x07, AC_VERB_SET_CONNECT_SEL, 0x00},
Matt2f2f4252005-04-13 14:45:30 +0200149 {}
150};
151
Mattc7d4b2f2005-06-27 14:59:41 +0200152static struct hda_verb stac922x_core_init[] = {
Matt2f2f4252005-04-13 14:45:30 +0200153 /* set master volume and direct control */
Mattc7d4b2f2005-06-27 14:59:41 +0200154 { 0x16, AC_VERB_SET_VOLUME_KNOB_CONTROL, 0xff},
Matt2f2f4252005-04-13 14:45:30 +0200155 {}
156};
157
Mattc7d4b2f2005-06-27 14:59:41 +0200158static int stac922x_channel_modes[3] = {2, 6, 8};
159
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100160static int stac922x_ch_mode_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Mattc7d4b2f2005-06-27 14:59:41 +0200161{
162 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
163 struct sigmatel_spec *spec = codec->spec;
164
165 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
166 uinfo->count = 1;
167 uinfo->value.enumerated.items = spec->num_ch_modes;
168 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
169 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
170 sprintf(uinfo->value.enumerated.name, "%dch",
171 stac922x_channel_modes[uinfo->value.enumerated.item]);
172 return 0;
173}
174
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100175static int stac922x_ch_mode_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Mattc7d4b2f2005-06-27 14:59:41 +0200176{
177 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
178 struct sigmatel_spec *spec = codec->spec;
179
180 ucontrol->value.enumerated.item[0] = spec->cur_ch_mode;
181 return 0;
182}
183
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100184static int stac922x_ch_mode_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Mattc7d4b2f2005-06-27 14:59:41 +0200185{
186 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
187 struct sigmatel_spec *spec = codec->spec;
188
189 if (ucontrol->value.enumerated.item[0] >= spec->num_ch_modes)
190 ucontrol->value.enumerated.item[0] = spec->num_ch_modes;
191 if (ucontrol->value.enumerated.item[0] == spec->cur_ch_mode &&
192 ! codec->in_resume)
193 return 0;
194
195 spec->cur_ch_mode = ucontrol->value.enumerated.item[0];
196 spec->multiout.max_channels = stac922x_channel_modes[spec->cur_ch_mode];
197
198 return 1;
199}
Matt2f2f4252005-04-13 14:45:30 +0200200
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100201static struct snd_kcontrol_new stac9200_mixer[] = {
Matt2f2f4252005-04-13 14:45:30 +0200202 HDA_CODEC_VOLUME("Master Playback Volume", 0xb, 0, HDA_OUTPUT),
203 HDA_CODEC_MUTE("Master Playback Switch", 0xb, 0, HDA_OUTPUT),
204 {
205 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
206 .name = "Input Source",
207 .count = 1,
208 .info = stac92xx_mux_enum_info,
209 .get = stac92xx_mux_enum_get,
210 .put = stac92xx_mux_enum_put,
211 },
212 HDA_CODEC_VOLUME("Capture Volume", 0x0a, 0, HDA_OUTPUT),
213 HDA_CODEC_MUTE("Capture Switch", 0x0a, 0, HDA_OUTPUT),
Mattc7d4b2f2005-06-27 14:59:41 +0200214 HDA_CODEC_VOLUME("Capture Mux Volume", 0x0c, 0, HDA_OUTPUT),
Matt2f2f4252005-04-13 14:45:30 +0200215 { } /* end */
216};
217
Mattc7d4b2f2005-06-27 14:59:41 +0200218/* This needs to be generated dynamically based on sequence */
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100219static struct snd_kcontrol_new stac922x_mixer[] = {
Matt2f2f4252005-04-13 14:45:30 +0200220 {
221 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
222 .name = "Input Source",
223 .count = 1,
224 .info = stac92xx_mux_enum_info,
225 .get = stac92xx_mux_enum_get,
226 .put = stac92xx_mux_enum_put,
227 },
228 HDA_CODEC_VOLUME("Capture Volume", 0x17, 0x0, HDA_INPUT),
229 HDA_CODEC_MUTE("Capture Switch", 0x17, 0x0, HDA_INPUT),
230 HDA_CODEC_VOLUME("Mux Capture Volume", 0x12, 0x0, HDA_OUTPUT),
231 { } /* end */
232};
233
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100234static struct snd_kcontrol_new stac922x_ch_mode_mixer[] = {
Mattc7d4b2f2005-06-27 14:59:41 +0200235 {
236 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
237 .name = "Channel Mode",
238 .info = stac922x_ch_mode_info,
239 .get = stac922x_ch_mode_get,
240 .put = stac922x_ch_mode_put,
241 },
242 { } /* end */
243};
244
Matt2f2f4252005-04-13 14:45:30 +0200245static int stac92xx_build_controls(struct hda_codec *codec)
246{
247 struct sigmatel_spec *spec = codec->spec;
248 int err;
Mattc7d4b2f2005-06-27 14:59:41 +0200249 int i;
Matt2f2f4252005-04-13 14:45:30 +0200250
251 err = snd_hda_add_new_ctls(codec, spec->mixer);
252 if (err < 0)
253 return err;
Mattc7d4b2f2005-06-27 14:59:41 +0200254
255 for (i = 0; i < spec->num_mixers; i++) {
256 err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
257 if (err < 0)
258 return err;
259 }
260
261 if (spec->surr_switch) {
262 err = snd_hda_add_new_ctls(codec, stac922x_ch_mode_mixer);
263 if (err < 0)
264 return err;
265 }
Mattdabbed62005-06-14 10:19:34 +0200266 if (spec->multiout.dig_out_nid) {
267 err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid);
268 if (err < 0)
269 return err;
270 }
271 if (spec->dig_in_nid) {
272 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
273 if (err < 0)
274 return err;
275 }
276 return 0;
Matt2f2f4252005-04-13 14:45:30 +0200277}
278
279#ifdef STAC_TEST
280static unsigned int stac9200_pin_configs[8] = {
Mattdabbed62005-06-14 10:19:34 +0200281 0x01c47010, 0x01447010, 0x0221401f, 0x01114010,
Matt2f2f4252005-04-13 14:45:30 +0200282 0x02a19020, 0x01a19021, 0x90100140, 0x01813122,
283};
284
Mattc7d4b2f2005-06-27 14:59:41 +0200285static unsigned int stac922x_pin_configs[10] = {
286 0x01014010, 0x01014011, 0x01014012, 0x0221401f,
287 0x01813122, 0x01014014, 0x01441030, 0x01c41030,
Matt2f2f4252005-04-13 14:45:30 +0200288 0x40000100, 0x40000100,
289};
290
291static void stac92xx_set_config_regs(struct hda_codec *codec)
292{
293 int i;
294 struct sigmatel_spec *spec = codec->spec;
295 unsigned int pin_cfg;
296
297 for (i=0; i < spec->num_pins; i++) {
298 snd_hda_codec_write(codec, spec->pin_nids[i], 0,
299 AC_VERB_SET_CONFIG_DEFAULT_BYTES_0,
300 spec->pin_configs[i] & 0x000000ff);
301 snd_hda_codec_write(codec, spec->pin_nids[i], 0,
302 AC_VERB_SET_CONFIG_DEFAULT_BYTES_1,
303 (spec->pin_configs[i] & 0x0000ff00) >> 8);
304 snd_hda_codec_write(codec, spec->pin_nids[i], 0,
305 AC_VERB_SET_CONFIG_DEFAULT_BYTES_2,
306 (spec->pin_configs[i] & 0x00ff0000) >> 16);
307 snd_hda_codec_write(codec, spec->pin_nids[i], 0,
308 AC_VERB_SET_CONFIG_DEFAULT_BYTES_3,
309 spec->pin_configs[i] >> 24);
310 pin_cfg = snd_hda_codec_read(codec, spec->pin_nids[i], 0,
311 AC_VERB_GET_CONFIG_DEFAULT,
312 0x00);
313 printk("pin nid %2.2x pin config %8.8x\n", spec->pin_nids[i], pin_cfg);
314 }
315}
316#endif
317
Matt2f2f4252005-04-13 14:45:30 +0200318/*
319 * Analog playback callbacks
320 */
321static int stac92xx_playback_pcm_open(struct hda_pcm_stream *hinfo,
322 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100323 struct snd_pcm_substream *substream)
Matt2f2f4252005-04-13 14:45:30 +0200324{
325 struct sigmatel_spec *spec = codec->spec;
326 return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream);
327}
328
Mattc7d4b2f2005-06-27 14:59:41 +0200329/*
330 * set up the i/o for analog out
331 * when the digital out is available, copy the front out to digital out, too.
332 */
333static int stac92xx_multi_out_analog_prepare(struct hda_codec *codec, struct hda_multi_out *mout,
334 unsigned int stream_tag,
335 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100336 struct snd_pcm_substream *substream)
Mattc7d4b2f2005-06-27 14:59:41 +0200337{
338 hda_nid_t *nids = mout->dac_nids;
339 int chs = substream->runtime->channels;
340 int i;
341
342 down(&codec->spdif_mutex);
343 if (mout->dig_out_nid && mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
344 if (chs == 2 &&
345 snd_hda_is_supported_format(codec, mout->dig_out_nid, format) &&
346 ! (codec->spdif_status & IEC958_AES0_NONAUDIO)) {
347 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
348 /* setup digital receiver */
349 snd_hda_codec_setup_stream(codec, mout->dig_out_nid,
350 stream_tag, 0, format);
351 } else {
352 mout->dig_out_used = 0;
353 snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0);
354 }
355 }
356 up(&codec->spdif_mutex);
357
358 /* front */
359 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag, 0, format);
360 if (mout->hp_nid)
361 /* headphone out will just decode front left/right (stereo) */
362 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag, 0, format);
363 /* surrounds */
364 if (mout->max_channels > 2)
365 for (i = 1; i < mout->num_dacs; i++) {
366 if ((mout->max_channels == 6) && (i == 3))
367 break;
368 if (chs >= (i + 1) * 2) /* independent out */
369 snd_hda_codec_setup_stream(codec, nids[i], stream_tag, i * 2,
370 format);
371 else /* copy front */
372 snd_hda_codec_setup_stream(codec, nids[i], stream_tag, 0,
373 format);
374 }
375 return 0;
376}
377
378
Matt2f2f4252005-04-13 14:45:30 +0200379static int stac92xx_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
380 struct hda_codec *codec,
381 unsigned int stream_tag,
382 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100383 struct snd_pcm_substream *substream)
Matt2f2f4252005-04-13 14:45:30 +0200384{
385 struct sigmatel_spec *spec = codec->spec;
Mattc7d4b2f2005-06-27 14:59:41 +0200386 return stac92xx_multi_out_analog_prepare(codec, &spec->multiout, stream_tag,
Matt2f2f4252005-04-13 14:45:30 +0200387 format, substream);
388}
389
390static int stac92xx_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
391 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100392 struct snd_pcm_substream *substream)
Matt2f2f4252005-04-13 14:45:30 +0200393{
394 struct sigmatel_spec *spec = codec->spec;
395 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
396}
397
398/*
Mattdabbed62005-06-14 10:19:34 +0200399 * Digital playback callbacks
400 */
401static int stac92xx_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
402 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100403 struct snd_pcm_substream *substream)
Mattdabbed62005-06-14 10:19:34 +0200404{
405 struct sigmatel_spec *spec = codec->spec;
406 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
407}
408
409static int stac92xx_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
410 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100411 struct snd_pcm_substream *substream)
Mattdabbed62005-06-14 10:19:34 +0200412{
413 struct sigmatel_spec *spec = codec->spec;
414 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
415}
416
417
418/*
Matt2f2f4252005-04-13 14:45:30 +0200419 * Analog capture callbacks
420 */
421static int stac92xx_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
422 struct hda_codec *codec,
423 unsigned int stream_tag,
424 unsigned int format,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100425 struct snd_pcm_substream *substream)
Matt2f2f4252005-04-13 14:45:30 +0200426{
427 struct sigmatel_spec *spec = codec->spec;
428
429 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
430 stream_tag, 0, format);
431 return 0;
432}
433
434static int stac92xx_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
435 struct hda_codec *codec,
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100436 struct snd_pcm_substream *substream)
Matt2f2f4252005-04-13 14:45:30 +0200437{
438 struct sigmatel_spec *spec = codec->spec;
439
440 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number], 0, 0, 0);
441 return 0;
442}
443
Mattdabbed62005-06-14 10:19:34 +0200444static struct hda_pcm_stream stac92xx_pcm_digital_playback = {
445 .substreams = 1,
446 .channels_min = 2,
447 .channels_max = 2,
448 /* NID is set in stac92xx_build_pcms */
449 .ops = {
450 .open = stac92xx_dig_playback_pcm_open,
451 .close = stac92xx_dig_playback_pcm_close
452 },
453};
454
455static struct hda_pcm_stream stac92xx_pcm_digital_capture = {
456 .substreams = 1,
457 .channels_min = 2,
458 .channels_max = 2,
459 /* NID is set in stac92xx_build_pcms */
460};
461
Matt2f2f4252005-04-13 14:45:30 +0200462static struct hda_pcm_stream stac92xx_pcm_analog_playback = {
463 .substreams = 1,
464 .channels_min = 2,
Mattc7d4b2f2005-06-27 14:59:41 +0200465 .channels_max = 8,
Matt2f2f4252005-04-13 14:45:30 +0200466 .nid = 0x02, /* NID to query formats and rates */
467 .ops = {
468 .open = stac92xx_playback_pcm_open,
469 .prepare = stac92xx_playback_pcm_prepare,
470 .cleanup = stac92xx_playback_pcm_cleanup
471 },
472};
473
474static struct hda_pcm_stream stac92xx_pcm_analog_capture = {
475 .substreams = 2,
476 .channels_min = 2,
477 .channels_max = 2,
478 .nid = 0x06, /* NID to query formats and rates */
479 .ops = {
480 .prepare = stac92xx_capture_pcm_prepare,
481 .cleanup = stac92xx_capture_pcm_cleanup
482 },
483};
484
485static int stac92xx_build_pcms(struct hda_codec *codec)
486{
487 struct sigmatel_spec *spec = codec->spec;
488 struct hda_pcm *info = spec->pcm_rec;
489
490 codec->num_pcms = 1;
491 codec->pcm_info = info;
492
Mattc7d4b2f2005-06-27 14:59:41 +0200493 info->name = "STAC92xx Analog";
Matt2f2f4252005-04-13 14:45:30 +0200494 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = stac92xx_pcm_analog_playback;
Matt2f2f4252005-04-13 14:45:30 +0200495 info->stream[SNDRV_PCM_STREAM_CAPTURE] = stac92xx_pcm_analog_capture;
Matt2f2f4252005-04-13 14:45:30 +0200496
Mattdabbed62005-06-14 10:19:34 +0200497 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
498 codec->num_pcms++;
499 info++;
500 info->name = "STAC92xx Digital";
501 if (spec->multiout.dig_out_nid) {
502 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = stac92xx_pcm_digital_playback;
503 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
504 }
505 if (spec->dig_in_nid) {
506 info->stream[SNDRV_PCM_STREAM_CAPTURE] = stac92xx_pcm_digital_capture;
507 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
508 }
509 }
510
Matt2f2f4252005-04-13 14:45:30 +0200511 return 0;
512}
513
Mattc7d4b2f2005-06-27 14:59:41 +0200514enum {
515 STAC_CTL_WIDGET_VOL,
516 STAC_CTL_WIDGET_MUTE,
517};
518
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100519static struct snd_kcontrol_new stac92xx_control_templates[] = {
Mattc7d4b2f2005-06-27 14:59:41 +0200520 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
521 HDA_CODEC_MUTE(NULL, 0, 0, 0),
522};
523
524/* add dynamic controls */
525static int stac92xx_add_control(struct sigmatel_spec *spec, int type, const char *name, unsigned long val)
526{
Takashi Iwaic8b6bf92005-11-17 14:57:47 +0100527 struct snd_kcontrol_new *knew;
Mattc7d4b2f2005-06-27 14:59:41 +0200528
529 if (spec->num_kctl_used >= spec->num_kctl_alloc) {
530 int num = spec->num_kctl_alloc + NUM_CONTROL_ALLOC;
531
532 knew = kcalloc(num + 1, sizeof(*knew), GFP_KERNEL); /* array + terminator */
533 if (! knew)
534 return -ENOMEM;
535 if (spec->kctl_alloc) {
536 memcpy(knew, spec->kctl_alloc, sizeof(*knew) * spec->num_kctl_alloc);
537 kfree(spec->kctl_alloc);
538 }
539 spec->kctl_alloc = knew;
540 spec->num_kctl_alloc = num;
541 }
542
543 knew = &spec->kctl_alloc[spec->num_kctl_used];
544 *knew = stac92xx_control_templates[type];
Takashi Iwai82fe0c52005-06-30 10:54:33 +0200545 knew->name = kstrdup(name, GFP_KERNEL);
Mattc7d4b2f2005-06-27 14:59:41 +0200546 if (! knew->name)
547 return -ENOMEM;
548 knew->private_value = val;
549 spec->num_kctl_used++;
550 return 0;
551}
552
553/* fill in the dac_nids table from the parsed pin configuration */
554static int stac92xx_auto_fill_dac_nids(struct hda_codec *codec, const struct auto_pin_cfg *cfg)
555{
556 struct sigmatel_spec *spec = codec->spec;
557 hda_nid_t nid;
558 int i;
559
560 /* check the pins hardwired to audio widget */
561 for (i = 0; i < cfg->line_outs; i++) {
562 nid = cfg->line_out_pins[i];
563 spec->multiout.dac_nids[i] = snd_hda_codec_read(codec, nid, 0,
564 AC_VERB_GET_CONNECT_LIST, 0) & 0xff;
565 }
566
567 spec->multiout.num_dacs = cfg->line_outs;
568
569 return 0;
570}
571
572/* add playback controls from the parsed DAC table */
573static int stac92xx_auto_create_multi_out_ctls(struct sigmatel_spec *spec, const struct auto_pin_cfg *cfg)
574{
575 char name[32];
576 static const char *chname[4] = { "Front", "Surround", NULL /*CLFE*/, "Side" };
577 hda_nid_t nid;
578 int i, err;
579
580 for (i = 0; i < cfg->line_outs; i++) {
581 if (! spec->multiout.dac_nids[i])
582 continue;
583
584 nid = spec->multiout.dac_nids[i];
585
586 if (i == 2) {
587 /* Center/LFE */
588 if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_VOL, "Center Playback Volume",
589 HDA_COMPOSE_AMP_VAL(nid, 1, 0, HDA_OUTPUT))) < 0)
590 return err;
591 if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_VOL, "LFE Playback Volume",
592 HDA_COMPOSE_AMP_VAL(nid, 2, 0, HDA_OUTPUT))) < 0)
593 return err;
594 if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_MUTE, "Center Playback Switch",
595 HDA_COMPOSE_AMP_VAL(nid, 1, 0, HDA_OUTPUT))) < 0)
596 return err;
597 if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_MUTE, "LFE Playback Switch",
598 HDA_COMPOSE_AMP_VAL(nid, 2, 0, HDA_OUTPUT))) < 0)
599 return err;
600 } else {
601 sprintf(name, "%s Playback Volume", chname[i]);
602 if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_VOL, name,
603 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT))) < 0)
604 return err;
605 sprintf(name, "%s Playback Switch", chname[i]);
606 if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_MUTE, name,
607 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT))) < 0)
608 return err;
609 }
610 }
611
612 return 0;
613}
614
615/* add playback controls for HP output */
616static int stac92xx_auto_create_hp_ctls(struct hda_codec *codec, struct auto_pin_cfg *cfg)
617{
618 struct sigmatel_spec *spec = codec->spec;
619 hda_nid_t pin = cfg->hp_pin;
620 hda_nid_t nid;
621 int i, err;
Matt4e550962005-07-04 17:51:39 +0200622 unsigned int wid_caps;
Mattc7d4b2f2005-06-27 14:59:41 +0200623
624 if (! pin)
625 return 0;
626
Matt4e550962005-07-04 17:51:39 +0200627 wid_caps = snd_hda_param_read(codec, pin, AC_PAR_AUDIO_WIDGET_CAP);
628 if (wid_caps & AC_WCAP_UNSOL_CAP)
629 /* Enable unsolicited responses on the HP widget */
630 snd_hda_codec_write(codec, pin, 0,
631 AC_VERB_SET_UNSOLICITED_ENABLE,
632 STAC_UNSOL_ENABLE);
633
Mattc7d4b2f2005-06-27 14:59:41 +0200634 nid = snd_hda_codec_read(codec, pin, 0, AC_VERB_GET_CONNECT_LIST, 0) & 0xff;
635 for (i = 0; i < cfg->line_outs; i++) {
636 if (! spec->multiout.dac_nids[i])
637 continue;
638 if (spec->multiout.dac_nids[i] == nid)
639 return 0;
640 }
641
642 spec->multiout.hp_nid = nid;
643
644 /* control HP volume/switch on the output mixer amp */
645 if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_VOL, "Headphone Playback Volume",
646 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT))) < 0)
647 return err;
648 if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_MUTE, "Headphone Playback Switch",
649 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT))) < 0)
650 return err;
651
652 return 0;
653}
654
655/* create playback/capture controls for input pins */
656static int stac92xx_auto_create_analog_input_ctls(struct hda_codec *codec, const struct auto_pin_cfg *cfg)
657{
658 struct sigmatel_spec *spec = codec->spec;
659 static char *labels[AUTO_PIN_LAST] = {
660 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
661 };
662 struct hda_input_mux *imux = &spec->private_imux;
663 hda_nid_t con_lst[HDA_MAX_NUM_INPUTS];
664 int i, j, k;
665
666 for (i = 0; i < AUTO_PIN_LAST; i++) {
667 int index = -1;
668 if (cfg->input_pins[i]) {
669 imux->items[imux->num_items].label = labels[i];
670
671 for (j=0; j<spec->num_muxes; j++) {
672 int num_cons = snd_hda_get_connections(codec, spec->mux_nids[j], con_lst, HDA_MAX_NUM_INPUTS);
673 for (k=0; k<num_cons; k++)
674 if (con_lst[k] == cfg->input_pins[i]) {
675 index = k;
676 break;
677 }
678 if (index >= 0)
679 break;
680 }
681 imux->items[imux->num_items].index = index;
682 imux->num_items++;
683 }
684 }
685
686 return 0;
687}
688
689static void stac92xx_auto_set_pinctl(struct hda_codec *codec, hda_nid_t nid, int pin_type)
690
691{
692 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL, pin_type);
693}
694
695static void stac92xx_auto_init_multi_out(struct hda_codec *codec)
696{
697 struct sigmatel_spec *spec = codec->spec;
698 int i;
699
700 for (i = 0; i < spec->autocfg.line_outs; i++) {
701 hda_nid_t nid = spec->autocfg.line_out_pins[i];
702 stac92xx_auto_set_pinctl(codec, nid, AC_PINCTL_OUT_EN);
703 }
704}
705
706static void stac92xx_auto_init_hp_out(struct hda_codec *codec)
707{
708 struct sigmatel_spec *spec = codec->spec;
709 hda_nid_t pin;
710
711 pin = spec->autocfg.hp_pin;
712 if (pin) /* connect to front */
713 stac92xx_auto_set_pinctl(codec, pin, AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
714}
715
716static int stac922x_parse_auto_config(struct hda_codec *codec)
717{
718 struct sigmatel_spec *spec = codec->spec;
719 int err;
720
721 if ((err = snd_hda_parse_pin_def_config(codec, &spec->autocfg)) < 0)
722 return err;
723 if ((err = stac92xx_auto_fill_dac_nids(codec, &spec->autocfg)) < 0)
724 return err;
725 if (! spec->autocfg.line_outs && ! spec->autocfg.hp_pin)
726 return 0; /* can't find valid pin config */
727
728 if ((err = stac92xx_auto_create_multi_out_ctls(spec, &spec->autocfg)) < 0 ||
729 (err = stac92xx_auto_create_hp_ctls(codec, &spec->autocfg)) < 0 ||
730 (err = stac92xx_auto_create_analog_input_ctls(codec, &spec->autocfg)) < 0)
731 return err;
732
733 spec->multiout.max_channels = spec->multiout.num_dacs * 2;
734 if (spec->multiout.max_channels > 2) {
735 spec->surr_switch = 1;
736 spec->cur_ch_mode = 1;
737 spec->num_ch_modes = 2;
738 if (spec->multiout.max_channels == 8) {
739 spec->cur_ch_mode++;
740 spec->num_ch_modes++;
741 }
742 }
743
744 if (spec->autocfg.dig_out_pin) {
745 spec->multiout.dig_out_nid = 0x08;
746 stac92xx_auto_set_pinctl(codec, spec->autocfg.dig_out_pin, AC_PINCTL_OUT_EN);
747 }
748 if (spec->autocfg.dig_in_pin) {
749 spec->dig_in_nid = 0x09;
750 stac92xx_auto_set_pinctl(codec, spec->autocfg.dig_in_pin, AC_PINCTL_IN_EN);
751 }
752
753 if (spec->kctl_alloc)
754 spec->mixers[spec->num_mixers++] = spec->kctl_alloc;
755
756 spec->input_mux = &spec->private_imux;
757
758 return 1;
759}
760
761static int stac9200_parse_auto_config(struct hda_codec *codec)
762{
763 struct sigmatel_spec *spec = codec->spec;
764 int err;
765
766 if ((err = snd_hda_parse_pin_def_config(codec, &spec->autocfg)) < 0)
767 return err;
768
769 if ((err = stac92xx_auto_create_analog_input_ctls(codec, &spec->autocfg)) < 0)
770 return err;
771
772 if (spec->autocfg.dig_out_pin) {
773 spec->multiout.dig_out_nid = 0x05;
774 stac92xx_auto_set_pinctl(codec, spec->autocfg.dig_out_pin, AC_PINCTL_OUT_EN);
775 }
776 if (spec->autocfg.dig_in_pin) {
777 spec->dig_in_nid = 0x04;
778 stac92xx_auto_set_pinctl(codec, spec->autocfg.dig_in_pin, AC_PINCTL_IN_EN);
779 }
780
781 if (spec->kctl_alloc)
782 spec->mixers[spec->num_mixers++] = spec->kctl_alloc;
783
784 spec->input_mux = &spec->private_imux;
785
786 return 1;
787}
788
789static int stac92xx_init_pstate(struct hda_codec *codec)
790{
791 hda_nid_t nid, nid_start;
792 int nodes;
793
794 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_POWER_STATE, 0x00);
795
796 nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid_start);
797 for (nid = nid_start; nid < nodes + nid_start; nid++) {
798 unsigned int wid_caps = snd_hda_param_read(codec, nid,
799 AC_PAR_AUDIO_WIDGET_CAP);
800 if (wid_caps & AC_WCAP_POWER)
801 snd_hda_codec_write(codec, nid, 0,
802 AC_VERB_SET_POWER_STATE, 0x00);
803 }
804
805 mdelay(100);
806
807 return 0;
808}
809
810static int stac92xx_init(struct hda_codec *codec)
811{
812 struct sigmatel_spec *spec = codec->spec;
813
814 stac92xx_init_pstate(codec);
815
816 snd_hda_sequence_write(codec, spec->init);
817
818 stac92xx_auto_init_multi_out(codec);
819 stac92xx_auto_init_hp_out(codec);
820
821 return 0;
822}
823
Matt2f2f4252005-04-13 14:45:30 +0200824static void stac92xx_free(struct hda_codec *codec)
825{
Mattc7d4b2f2005-06-27 14:59:41 +0200826 struct sigmatel_spec *spec = codec->spec;
827 int i;
828
829 if (! spec)
830 return;
831
832 if (spec->kctl_alloc) {
833 for (i = 0; i < spec->num_kctl_used; i++)
834 kfree(spec->kctl_alloc[i].name);
835 kfree(spec->kctl_alloc);
836 }
837
838 kfree(spec);
Matt2f2f4252005-04-13 14:45:30 +0200839}
840
Matt4e550962005-07-04 17:51:39 +0200841static void stac92xx_set_pinctl(struct hda_codec *codec, hda_nid_t nid,
842 unsigned int flag)
843{
844 unsigned int pin_ctl = snd_hda_codec_read(codec, nid,
845 0, AC_VERB_GET_PIN_WIDGET_CONTROL, 0x00);
846 snd_hda_codec_write(codec, nid, 0,
847 AC_VERB_SET_PIN_WIDGET_CONTROL,
848 pin_ctl | flag);
849}
850
851static void stac92xx_reset_pinctl(struct hda_codec *codec, hda_nid_t nid,
852 unsigned int flag)
853{
854 unsigned int pin_ctl = snd_hda_codec_read(codec, nid,
855 0, AC_VERB_GET_PIN_WIDGET_CONTROL, 0x00);
856 snd_hda_codec_write(codec, nid, 0,
857 AC_VERB_SET_PIN_WIDGET_CONTROL,
858 pin_ctl & ~flag);
859}
860
861static void stac92xx_unsol_event(struct hda_codec *codec, unsigned int res)
862{
863 struct sigmatel_spec *spec = codec->spec;
864 struct auto_pin_cfg *cfg = &spec->autocfg;
865 int i, presence;
866
867 if ((res >> 26) != STAC_HP_EVENT)
868 return;
869
870 presence = snd_hda_codec_read(codec, cfg->hp_pin, 0,
871 AC_VERB_GET_PIN_SENSE, 0x00) >> 31;
872
873 if (presence) {
874 /* disable lineouts, enable hp */
875 for (i = 0; i < cfg->line_outs; i++)
876 stac92xx_reset_pinctl(codec, cfg->line_out_pins[i],
877 AC_PINCTL_OUT_EN);
878 stac92xx_set_pinctl(codec, cfg->hp_pin, AC_PINCTL_OUT_EN);
879 } else {
880 /* enable lineouts, disable hp */
881 for (i = 0; i < cfg->line_outs; i++)
882 stac92xx_set_pinctl(codec, cfg->line_out_pins[i],
883 AC_PINCTL_OUT_EN);
884 stac92xx_reset_pinctl(codec, cfg->hp_pin, AC_PINCTL_OUT_EN);
885 }
886}
887
Mattff6fdc32005-06-27 15:06:52 +0200888#ifdef CONFIG_PM
889static int stac92xx_resume(struct hda_codec *codec)
890{
891 struct sigmatel_spec *spec = codec->spec;
892 int i;
893
894 stac92xx_init(codec);
895 for (i = 0; i < spec->num_mixers; i++)
896 snd_hda_resume_ctls(codec, spec->mixers[i]);
897 if (spec->multiout.dig_out_nid)
898 snd_hda_resume_spdif_out(codec);
899 if (spec->dig_in_nid)
900 snd_hda_resume_spdif_in(codec);
901
902 return 0;
903}
904#endif
905
Matt2f2f4252005-04-13 14:45:30 +0200906static struct hda_codec_ops stac92xx_patch_ops = {
907 .build_controls = stac92xx_build_controls,
908 .build_pcms = stac92xx_build_pcms,
909 .init = stac92xx_init,
910 .free = stac92xx_free,
Matt4e550962005-07-04 17:51:39 +0200911 .unsol_event = stac92xx_unsol_event,
Mattff6fdc32005-06-27 15:06:52 +0200912#ifdef CONFIG_PM
913 .resume = stac92xx_resume,
914#endif
Matt2f2f4252005-04-13 14:45:30 +0200915};
916
917static int patch_stac9200(struct hda_codec *codec)
918{
919 struct sigmatel_spec *spec;
Mattc7d4b2f2005-06-27 14:59:41 +0200920 int err;
Matt2f2f4252005-04-13 14:45:30 +0200921
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200922 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Matt2f2f4252005-04-13 14:45:30 +0200923 if (spec == NULL)
924 return -ENOMEM;
925
926 codec->spec = spec;
927
Mattc7d4b2f2005-06-27 14:59:41 +0200928#ifdef STAC_TEST
929 spec->pin_nids = stac9200_pin_nids;
930 spec->num_pins = 8;
931 spec->pin_configs = stac9200_pin_configs;
932 stac92xx_set_config_regs(codec);
933#endif
Matt2f2f4252005-04-13 14:45:30 +0200934 spec->multiout.max_channels = 2;
935 spec->multiout.num_dacs = 1;
936 spec->multiout.dac_nids = stac9200_dac_nids;
937 spec->adc_nids = stac9200_adc_nids;
938 spec->mux_nids = stac9200_mux_nids;
Mattdabbed62005-06-14 10:19:34 +0200939 spec->num_muxes = 1;
Mattc7d4b2f2005-06-27 14:59:41 +0200940
941 spec->init = stac9200_core_init;
Matt2f2f4252005-04-13 14:45:30 +0200942 spec->mixer = stac9200_mixer;
Mattc7d4b2f2005-06-27 14:59:41 +0200943
944 err = stac9200_parse_auto_config(codec);
945 if (err < 0) {
946 stac92xx_free(codec);
947 return err;
948 }
Matt2f2f4252005-04-13 14:45:30 +0200949
950 codec->patch_ops = stac92xx_patch_ops;
951
952 return 0;
953}
954
955static int patch_stac922x(struct hda_codec *codec)
956{
957 struct sigmatel_spec *spec;
Mattc7d4b2f2005-06-27 14:59:41 +0200958 int err;
Matt2f2f4252005-04-13 14:45:30 +0200959
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200960 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Matt2f2f4252005-04-13 14:45:30 +0200961 if (spec == NULL)
962 return -ENOMEM;
963
964 codec->spec = spec;
965
Mattc7d4b2f2005-06-27 14:59:41 +0200966#ifdef STAC_TEST
967 spec->num_pins = 10;
968 spec->pin_nids = stac922x_pin_nids;
969 spec->pin_configs = stac922x_pin_configs;
970 stac92xx_set_config_regs(codec);
971#endif
Matt2f2f4252005-04-13 14:45:30 +0200972 spec->adc_nids = stac922x_adc_nids;
973 spec->mux_nids = stac922x_mux_nids;
Mattdabbed62005-06-14 10:19:34 +0200974 spec->num_muxes = 2;
Mattc7d4b2f2005-06-27 14:59:41 +0200975
976 spec->init = stac922x_core_init;
Matt2f2f4252005-04-13 14:45:30 +0200977 spec->mixer = stac922x_mixer;
Mattc7d4b2f2005-06-27 14:59:41 +0200978
979 spec->multiout.dac_nids = spec->dac_nids;
980
981 err = stac922x_parse_auto_config(codec);
982 if (err < 0) {
983 stac92xx_free(codec);
984 return err;
985 }
Matt2f2f4252005-04-13 14:45:30 +0200986
987 codec->patch_ops = stac92xx_patch_ops;
988
989 return 0;
990}
991
992/*
993 * patch entries
994 */
995struct hda_codec_preset snd_hda_preset_sigmatel[] = {
996 { .id = 0x83847690, .name = "STAC9200", .patch = patch_stac9200 },
997 { .id = 0x83847882, .name = "STAC9220 A1", .patch = patch_stac922x },
998 { .id = 0x83847680, .name = "STAC9221 A1", .patch = patch_stac922x },
999 { .id = 0x83847880, .name = "STAC9220 A2", .patch = patch_stac922x },
1000 { .id = 0x83847681, .name = "STAC9220D/9223D A2", .patch = patch_stac922x },
1001 { .id = 0x83847682, .name = "STAC9221 A2", .patch = patch_stac922x },
1002 { .id = 0x83847683, .name = "STAC9221D A2", .patch = patch_stac922x },
1003 {} /* terminator */
1004};