blob: 6e0fd92a2be3fc5f5651ae572fcacdce01e249ea [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Universal Interface for Intel High Definition Audio Codec
3 *
4 * HD audio interface patch for C-Media CMI9880
5 *
6 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
7 *
8 *
9 * This driver is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This driver is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
24#include <sound/driver.h>
25#include <linux/init.h>
26#include <linux/delay.h>
27#include <linux/slab.h>
28#include <linux/pci.h>
29#include <sound/core.h>
30#include "hda_codec.h"
31#include "hda_local.h"
Takashi Iwaif953eff2005-04-08 15:05:13 +020032#define NUM_PINS 11
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34
35/* board config type */
36enum {
37 CMI_MINIMAL, /* back 3-jack */
38 CMI_MIN_FP, /* back 3-jack + front-panel 2-jack */
39 CMI_FULL, /* back 6-jack + front-panel 2-jack */
40 CMI_FULL_DIG, /* back 6-jack + front-panel 2-jack + digital I/O */
41 CMI_ALLOUT, /* back 5-jack + front-panel 2-jack + digital out */
Takashi Iwaif953eff2005-04-08 15:05:13 +020042 CMI_AUTO, /* let driver guess it */
Linus Torvalds1da177e2005-04-16 15:20:36 -070043};
44
45struct cmi_spec {
46 int board_config;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 unsigned int no_line_in: 1; /* no line-in (5-jack) */
48 unsigned int front_panel: 1; /* has front-panel 2-jack */
49
50 /* playback */
51 struct hda_multi_out multiout;
Takashi Iwaif953eff2005-04-08 15:05:13 +020052 hda_nid_t dac_nids[4]; /* NID for each DAC */
Takashi Iwaie9edcee2005-06-13 14:16:38 +020053 int num_dacs;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55 /* capture */
56 hda_nid_t *adc_nids;
57 hda_nid_t dig_in_nid;
58
59 /* capture source */
60 const struct hda_input_mux *input_mux;
61 unsigned int cur_mux[2];
62
63 /* channel mode */
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +010064 int num_channel_modes;
65 const struct hda_channel_mode *channel_modes;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67 struct hda_pcm pcm_rec[2]; /* PCM information */
Takashi Iwaif953eff2005-04-08 15:05:13 +020068
69 /* pin deafault configuration */
70 hda_nid_t pin_nid[NUM_PINS];
71 unsigned int def_conf[NUM_PINS];
72 unsigned int pin_def_confs;
73
74 /* multichannel pins */
75 hda_nid_t multich_pin[4]; /* max 8-channel */
76 struct hda_verb multi_init[9]; /* 2 verbs for each pin + terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -070077};
78
Takashi Iwaie9edcee2005-06-13 14:16:38 +020079/* amp values */
80#define AMP_IN_MUTE(idx) (0x7080 | ((idx)<<8))
81#define AMP_IN_UNMUTE(idx) (0x7000 | ((idx)<<8))
82#define AMP_OUT_MUTE 0xb080
83#define AMP_OUT_UNMUTE 0xb000
84#define AMP_OUT_ZERO 0xb000
85/* pinctl values */
86#define PIN_IN 0x20
87#define PIN_VREF80 0x24
88#define PIN_VREF50 0x21
89#define PIN_OUT 0x40
90#define PIN_HP 0xc0
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/*
93 * input MUX
94 */
95static int cmi_mux_enum_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
96{
97 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
98 struct cmi_spec *spec = codec->spec;
99 return snd_hda_input_mux_info(spec->input_mux, uinfo);
100}
101
102static int cmi_mux_enum_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
103{
104 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
105 struct cmi_spec *spec = codec->spec;
106 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
107
108 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
109 return 0;
110}
111
112static int cmi_mux_enum_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
113{
114 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
115 struct cmi_spec *spec = codec->spec;
116 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
117
118 return snd_hda_input_mux_put(codec, spec->input_mux, ucontrol,
119 spec->adc_nids[adc_idx], &spec->cur_mux[adc_idx]);
120}
121
122/*
123 * shared line-in, mic for surrounds
124 */
125
126/* 3-stack / 2 channel */
127static struct hda_verb cmi9880_ch2_init[] = {
128 /* set line-in PIN for input */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200129 { 0x0c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 /* set mic PIN for input, also enable vref */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200131 { 0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 /* route front PCM (DAC1) to HP */
133 { 0x0f, AC_VERB_SET_CONNECT_SEL, 0x00 },
134 {}
135};
136
137/* 3-stack / 6 channel */
138static struct hda_verb cmi9880_ch6_init[] = {
139 /* set line-in PIN for output */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200140 { 0x0c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 /* set mic PIN for output */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200142 { 0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 /* route front PCM (DAC1) to HP */
144 { 0x0f, AC_VERB_SET_CONNECT_SEL, 0x00 },
145 {}
146};
147
148/* 3-stack+front / 8 channel */
149static struct hda_verb cmi9880_ch8_init[] = {
150 /* set line-in PIN for output */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200151 { 0x0c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 /* set mic PIN for output */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200153 { 0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 /* route rear-surround PCM (DAC4) to HP */
155 { 0x0f, AC_VERB_SET_CONNECT_SEL, 0x03 },
156 {}
157};
158
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +0100159static struct hda_channel_mode cmi9880_channel_modes[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 { 2, cmi9880_ch2_init },
161 { 6, cmi9880_ch6_init },
162 { 8, cmi9880_ch8_init },
163};
164
165static int cmi_ch_mode_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
166{
167 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
168 struct cmi_spec *spec = codec->spec;
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +0100169 return snd_hda_ch_mode_info(codec, uinfo, spec->channel_modes,
170 spec->num_channel_modes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
173static int cmi_ch_mode_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
174{
175 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
176 struct cmi_spec *spec = codec->spec;
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +0100177 return snd_hda_ch_mode_get(codec, ucontrol, spec->channel_modes,
178 spec->num_channel_modes, spec->multiout.max_channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
181static int cmi_ch_mode_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
182{
183 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
184 struct cmi_spec *spec = codec->spec;
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +0100185 return snd_hda_ch_mode_put(codec, ucontrol, spec->channel_modes,
186 spec->num_channel_modes, &spec->multiout.max_channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
188
189/*
190 */
191static snd_kcontrol_new_t cmi9880_basic_mixer[] = {
192 /* CMI9880 has no playback volumes! */
193 HDA_CODEC_MUTE("PCM Playback Switch", 0x03, 0x0, HDA_OUTPUT), /* front */
194 HDA_CODEC_MUTE("Surround Playback Switch", 0x04, 0x0, HDA_OUTPUT),
195 HDA_CODEC_MUTE_MONO("Center Playback Switch", 0x05, 1, 0x0, HDA_OUTPUT),
196 HDA_CODEC_MUTE_MONO("LFE Playback Switch", 0x05, 2, 0x0, HDA_OUTPUT),
197 HDA_CODEC_MUTE("Side Playback Switch", 0x06, 0x0, HDA_OUTPUT),
198 {
199 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
200 /* The multiple "Capture Source" controls confuse alsamixer
201 * So call somewhat different..
202 * FIXME: the controls appear in the "playback" view!
203 */
204 /* .name = "Capture Source", */
205 .name = "Input Source",
206 .count = 2,
207 .info = cmi_mux_enum_info,
208 .get = cmi_mux_enum_get,
209 .put = cmi_mux_enum_put,
210 },
211 HDA_CODEC_VOLUME("Capture Volume", 0x08, 0, HDA_INPUT),
212 HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x09, 0, HDA_INPUT),
213 HDA_CODEC_MUTE("Capture Switch", 0x08, 0, HDA_INPUT),
214 HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x09, 0, HDA_INPUT),
215 HDA_CODEC_VOLUME("PC Speaker Playback Volume", 0x23, 0, HDA_OUTPUT),
216 HDA_CODEC_MUTE("PC Speaker Playback Switch", 0x23, 0, HDA_OUTPUT),
217 { } /* end */
218};
219
220/*
221 * shared I/O pins
222 */
223static snd_kcontrol_new_t cmi9880_ch_mode_mixer[] = {
224 {
225 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
226 .name = "Channel Mode",
227 .info = cmi_ch_mode_info,
228 .get = cmi_ch_mode_get,
229 .put = cmi_ch_mode_put,
230 },
231 { } /* end */
232};
233
234/* AUD-in selections:
235 * 0x0b 0x0c 0x0d 0x0e 0x0f 0x10 0x11 0x1f 0x20
236 */
237static struct hda_input_mux cmi9880_basic_mux = {
238 .num_items = 4,
239 .items = {
240 { "Front Mic", 0x5 },
241 { "Rear Mic", 0x2 },
242 { "Line", 0x1 },
243 { "CD", 0x7 },
244 }
245};
246
247static struct hda_input_mux cmi9880_no_line_mux = {
248 .num_items = 3,
249 .items = {
250 { "Front Mic", 0x5 },
251 { "Rear Mic", 0x2 },
252 { "CD", 0x7 },
253 }
254};
255
256/* front, rear, clfe, rear_surr */
257static hda_nid_t cmi9880_dac_nids[4] = {
258 0x03, 0x04, 0x05, 0x06
259};
260/* ADC0, ADC1 */
261static hda_nid_t cmi9880_adc_nids[2] = {
262 0x08, 0x09
263};
264
265#define CMI_DIG_OUT_NID 0x07
266#define CMI_DIG_IN_NID 0x0a
267
268/*
269 */
270static struct hda_verb cmi9880_basic_init[] = {
271 /* port-D for line out (rear panel) */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200272 { 0x0b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 /* port-E for HP out (front panel) */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200274 { 0x0f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 /* route front PCM to HP */
276 { 0x0f, AC_VERB_SET_CONNECT_SEL, 0x00 },
277 /* port-A for surround (rear panel) */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200278 { 0x0e, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 /* port-G for CLFE (rear panel) */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200280 { 0x1f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
ChenLi Tiend05b2812005-03-24 20:47:35 +0100281 { 0x1f, AC_VERB_SET_CONNECT_SEL, 0x02 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 /* port-H for side (rear panel) */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200283 { 0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
ChenLi Tiend05b2812005-03-24 20:47:35 +0100284 { 0x20, AC_VERB_SET_CONNECT_SEL, 0x01 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 /* port-C for line-in (rear panel) */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200286 { 0x0c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 /* port-B for mic-in (rear panel) with vref */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200288 { 0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 /* port-F for mic-in (front panel) with vref */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200290 { 0x10, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 /* CD-in */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200292 { 0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 /* route front mic to ADC1/2 */
294 { 0x08, AC_VERB_SET_CONNECT_SEL, 0x05 },
295 { 0x09, AC_VERB_SET_CONNECT_SEL, 0x05 },
296 {} /* terminator */
297};
298
299static struct hda_verb cmi9880_allout_init[] = {
300 /* port-D for line out (rear panel) */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200301 { 0x0b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 /* port-E for HP out (front panel) */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200303 { 0x0f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 /* route front PCM to HP */
305 { 0x0f, AC_VERB_SET_CONNECT_SEL, 0x00 },
306 /* port-A for side (rear panel) */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200307 { 0x0e, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 /* port-G for CLFE (rear panel) */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200309 { 0x1f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
ChenLi Tiend05b2812005-03-24 20:47:35 +0100310 { 0x1f, AC_VERB_SET_CONNECT_SEL, 0x02 },
311 /* port-H for side (rear panel) */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200312 { 0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
ChenLi Tiend05b2812005-03-24 20:47:35 +0100313 { 0x20, AC_VERB_SET_CONNECT_SEL, 0x01 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 /* port-C for surround (rear panel) */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200315 { 0x0c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 /* port-B for mic-in (rear panel) with vref */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200317 { 0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 /* port-F for mic-in (front panel) with vref */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200319 { 0x10, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 /* CD-in */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200321 { 0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 /* route front mic to ADC1/2 */
323 { 0x08, AC_VERB_SET_CONNECT_SEL, 0x05 },
324 { 0x09, AC_VERB_SET_CONNECT_SEL, 0x05 },
325 {} /* terminator */
326};
327
328/*
329 */
330static int cmi9880_build_controls(struct hda_codec *codec)
331{
332 struct cmi_spec *spec = codec->spec;
333 int err;
334
335 err = snd_hda_add_new_ctls(codec, cmi9880_basic_mixer);
336 if (err < 0)
337 return err;
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +0100338 if (spec->channel_modes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 err = snd_hda_add_new_ctls(codec, cmi9880_ch_mode_mixer);
340 if (err < 0)
341 return err;
342 }
343 if (spec->multiout.dig_out_nid) {
344 err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid);
345 if (err < 0)
346 return err;
347 }
348 if (spec->dig_in_nid) {
349 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
350 if (err < 0)
351 return err;
352 }
353 return 0;
354}
355
Takashi Iwaif953eff2005-04-08 15:05:13 +0200356/* fill in the multi_dac_nids table, which will decide
357 which audio widget to use for each channel */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200358static int cmi9880_fill_multi_dac_nids(struct hda_codec *codec, const struct auto_pin_cfg *cfg)
Takashi Iwaif953eff2005-04-08 15:05:13 +0200359{
360 struct cmi_spec *spec = codec->spec;
361 hda_nid_t nid;
362 int assigned[4];
363 int i, j;
364
365 /* clear the table, only one c-media dac assumed here */
366 memset(spec->dac_nids, 0, sizeof(spec->dac_nids));
367 memset(assigned, 0, sizeof(assigned));
368 /* check the pins we found */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200369 for (i = 0; i < cfg->line_outs; i++) {
370 nid = cfg->line_out_pins[i];
Takashi Iwaif953eff2005-04-08 15:05:13 +0200371 /* nid 0x0b~0x0e is hardwired to audio widget 0x3~0x6 */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200372 if (nid >= 0x0b && nid <= 0x0e) {
373 spec->dac_nids[i] = (nid - 0x0b) + 0x03;
Takashi Iwaif953eff2005-04-08 15:05:13 +0200374 assigned[nid - 0x0b] = 1;
375 }
376 }
377 /* left pin can be connect to any audio widget */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200378 for (i = 0; i < cfg->line_outs; i++) {
379 nid = cfg->line_out_pins[i];
380 if (nid <= 0x0e)
381 continue;
382 /* search for an empty channel */
383 for (j = 0; j < cfg->line_outs; j++) {
384 if (! assigned[j]) {
Nicolas Grazianofb92e6f2005-07-27 17:25:08 +0200385 spec->dac_nids[i] = j + 0x03;
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200386 assigned[j] = 1;
387 break;
388 }
Takashi Iwaif953eff2005-04-08 15:05:13 +0200389 }
390 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200391 spec->num_dacs = cfg->line_outs;
Takashi Iwaif953eff2005-04-08 15:05:13 +0200392 return 0;
393}
394
395/* create multi_init table, which is used for multichannel initialization */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200396static int cmi9880_fill_multi_init(struct hda_codec *codec, const struct auto_pin_cfg *cfg)
Takashi Iwaif953eff2005-04-08 15:05:13 +0200397{
398 struct cmi_spec *spec = codec->spec;
399 hda_nid_t nid;
400 int i, j, k, len;
401
402 /* clear the table, only one c-media dac assumed here */
403 memset(spec->multi_init, 0, sizeof(spec->multi_init));
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200404 for (j = 0, i = 0; i < cfg->line_outs; i++) {
Takashi Iwaif953eff2005-04-08 15:05:13 +0200405 hda_nid_t conn[4];
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200406 nid = cfg->line_out_pins[i];
Takashi Iwaif953eff2005-04-08 15:05:13 +0200407 /* set as output */
408 spec->multi_init[j].nid = nid;
409 spec->multi_init[j].verb = AC_VERB_SET_PIN_WIDGET_CONTROL;
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200410 spec->multi_init[j].param = PIN_OUT;
Takashi Iwaif953eff2005-04-08 15:05:13 +0200411 j++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200412 if (nid > 0x0e) {
Takashi Iwaif953eff2005-04-08 15:05:13 +0200413 /* set connection */
414 spec->multi_init[j].nid = nid;
415 spec->multi_init[j].verb = AC_VERB_SET_CONNECT_SEL;
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200416 spec->multi_init[j].param = 0;
Takashi Iwaif953eff2005-04-08 15:05:13 +0200417 /* find the index in connect list */
418 len = snd_hda_get_connections(codec, nid, conn, 4);
419 for (k = 0; k < len; k++)
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200420 if (conn[k] == spec->dac_nids[i]) {
Nicolas Grazianofb92e6f2005-07-27 17:25:08 +0200421 spec->multi_init[j].param = k;
Takashi Iwaif953eff2005-04-08 15:05:13 +0200422 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200423 }
Takashi Iwaif953eff2005-04-08 15:05:13 +0200424 j++;
Takashi Iwaif953eff2005-04-08 15:05:13 +0200425 }
426 }
427 return 0;
428}
429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430static int cmi9880_init(struct hda_codec *codec)
431{
432 struct cmi_spec *spec = codec->spec;
433 if (spec->board_config == CMI_ALLOUT)
434 snd_hda_sequence_write(codec, cmi9880_allout_init);
435 else
436 snd_hda_sequence_write(codec, cmi9880_basic_init);
Takashi Iwaif953eff2005-04-08 15:05:13 +0200437 if (spec->board_config == CMI_AUTO)
438 snd_hda_sequence_write(codec, spec->multi_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 return 0;
440}
441
442#ifdef CONFIG_PM
443/*
444 * resume
445 */
446static int cmi9880_resume(struct hda_codec *codec)
447{
448 struct cmi_spec *spec = codec->spec;
449
450 cmi9880_init(codec);
451 snd_hda_resume_ctls(codec, cmi9880_basic_mixer);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +0100452 if (spec->channel_modes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 snd_hda_resume_ctls(codec, cmi9880_ch_mode_mixer);
454 if (spec->multiout.dig_out_nid)
455 snd_hda_resume_spdif_out(codec);
456 if (spec->dig_in_nid)
457 snd_hda_resume_spdif_in(codec);
458
459 return 0;
460}
461#endif
462
463/*
464 * Analog playback callbacks
465 */
466static int cmi9880_playback_pcm_open(struct hda_pcm_stream *hinfo,
467 struct hda_codec *codec,
468 snd_pcm_substream_t *substream)
469{
470 struct cmi_spec *spec = codec->spec;
471 return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream);
472}
473
474static int cmi9880_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
475 struct hda_codec *codec,
476 unsigned int stream_tag,
477 unsigned int format,
478 snd_pcm_substream_t *substream)
479{
480 struct cmi_spec *spec = codec->spec;
481 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout, stream_tag,
482 format, substream);
483}
484
485static int cmi9880_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
486 struct hda_codec *codec,
487 snd_pcm_substream_t *substream)
488{
489 struct cmi_spec *spec = codec->spec;
490 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
491}
492
493/*
494 * Digital out
495 */
496static int cmi9880_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
497 struct hda_codec *codec,
498 snd_pcm_substream_t *substream)
499{
500 struct cmi_spec *spec = codec->spec;
501 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
502}
503
504static int cmi9880_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
505 struct hda_codec *codec,
506 snd_pcm_substream_t *substream)
507{
508 struct cmi_spec *spec = codec->spec;
509 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
510}
511
512/*
513 * Analog capture
514 */
515static int cmi9880_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
516 struct hda_codec *codec,
517 unsigned int stream_tag,
518 unsigned int format,
519 snd_pcm_substream_t *substream)
520{
521 struct cmi_spec *spec = codec->spec;
522
523 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
524 stream_tag, 0, format);
525 return 0;
526}
527
528static int cmi9880_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
529 struct hda_codec *codec,
530 snd_pcm_substream_t *substream)
531{
532 struct cmi_spec *spec = codec->spec;
533
534 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number], 0, 0, 0);
535 return 0;
536}
537
538
539/*
540 */
541static struct hda_pcm_stream cmi9880_pcm_analog_playback = {
542 .substreams = 1,
543 .channels_min = 2,
544 .channels_max = 8,
545 .nid = 0x03, /* NID to query formats and rates */
546 .ops = {
547 .open = cmi9880_playback_pcm_open,
548 .prepare = cmi9880_playback_pcm_prepare,
549 .cleanup = cmi9880_playback_pcm_cleanup
550 },
551};
552
553static struct hda_pcm_stream cmi9880_pcm_analog_capture = {
554 .substreams = 2,
555 .channels_min = 2,
556 .channels_max = 2,
557 .nid = 0x08, /* NID to query formats and rates */
558 .ops = {
559 .prepare = cmi9880_capture_pcm_prepare,
560 .cleanup = cmi9880_capture_pcm_cleanup
561 },
562};
563
564static struct hda_pcm_stream cmi9880_pcm_digital_playback = {
565 .substreams = 1,
566 .channels_min = 2,
567 .channels_max = 2,
568 /* NID is set in cmi9880_build_pcms */
569 .ops = {
570 .open = cmi9880_dig_playback_pcm_open,
571 .close = cmi9880_dig_playback_pcm_close
572 },
573};
574
575static struct hda_pcm_stream cmi9880_pcm_digital_capture = {
576 .substreams = 1,
577 .channels_min = 2,
578 .channels_max = 2,
579 /* NID is set in cmi9880_build_pcms */
580};
581
582static int cmi9880_build_pcms(struct hda_codec *codec)
583{
584 struct cmi_spec *spec = codec->spec;
585 struct hda_pcm *info = spec->pcm_rec;
586
587 codec->num_pcms = 1;
588 codec->pcm_info = info;
589
590 info->name = "CMI9880";
591 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = cmi9880_pcm_analog_playback;
592 info->stream[SNDRV_PCM_STREAM_CAPTURE] = cmi9880_pcm_analog_capture;
593
594 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
595 codec->num_pcms++;
596 info++;
597 info->name = "CMI9880 Digital";
598 if (spec->multiout.dig_out_nid) {
599 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = cmi9880_pcm_digital_playback;
600 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
601 }
602 if (spec->dig_in_nid) {
603 info->stream[SNDRV_PCM_STREAM_CAPTURE] = cmi9880_pcm_digital_capture;
604 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
605 }
606 }
607
608 return 0;
609}
610
611static void cmi9880_free(struct hda_codec *codec)
612{
613 kfree(codec->spec);
614}
615
616/*
617 */
618
619static struct hda_board_config cmi9880_cfg_tbl[] = {
620 { .modelname = "minimal", .config = CMI_MINIMAL },
621 { .modelname = "min_fp", .config = CMI_MIN_FP },
622 { .modelname = "full", .config = CMI_FULL },
623 { .modelname = "full_dig", .config = CMI_FULL_DIG },
Takashi Iwaid8275602005-07-29 11:56:41 +0200624 { .pci_subvendor = 0x1043, .pci_subdevice = 0x813d, .config = CMI_FULL_DIG }, /* ASUS P5AD2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 { .modelname = "allout", .config = CMI_ALLOUT },
Takashi Iwaif953eff2005-04-08 15:05:13 +0200626 { .modelname = "auto", .config = CMI_AUTO },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 {} /* terminator */
628};
629
630static struct hda_codec_ops cmi9880_patch_ops = {
631 .build_controls = cmi9880_build_controls,
632 .build_pcms = cmi9880_build_pcms,
633 .init = cmi9880_init,
634 .free = cmi9880_free,
635#ifdef CONFIG_PM
636 .resume = cmi9880_resume,
637#endif
638};
639
640static int patch_cmi9880(struct hda_codec *codec)
641{
642 struct cmi_spec *spec;
643
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200644 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 if (spec == NULL)
646 return -ENOMEM;
647
648 codec->spec = spec;
649 spec->board_config = snd_hda_check_board_config(codec, cmi9880_cfg_tbl);
650 if (spec->board_config < 0) {
Takashi Iwaif953eff2005-04-08 15:05:13 +0200651 snd_printdd(KERN_INFO "hda_codec: Unknown model for CMI9880\n");
652 spec->board_config = CMI_AUTO; /* try everything */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 }
654
Takashi Iwaif953eff2005-04-08 15:05:13 +0200655 /* copy default DAC NIDs */
656 memcpy(spec->dac_nids, cmi9880_dac_nids, sizeof(spec->dac_nids));
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200657 spec->num_dacs = 4;
Takashi Iwaif953eff2005-04-08 15:05:13 +0200658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 switch (spec->board_config) {
660 case CMI_MINIMAL:
661 case CMI_MIN_FP:
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +0100662 spec->channel_modes = cmi9880_channel_modes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 if (spec->board_config == CMI_MINIMAL)
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +0100664 spec->num_channel_modes = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 else {
666 spec->front_panel = 1;
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +0100667 spec->num_channel_modes = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 spec->multiout.max_channels = cmi9880_channel_modes[0].channels;
670 spec->input_mux = &cmi9880_basic_mux;
671 break;
672 case CMI_FULL:
673 case CMI_FULL_DIG:
674 spec->front_panel = 1;
675 spec->multiout.max_channels = 8;
676 spec->input_mux = &cmi9880_basic_mux;
677 if (spec->board_config == CMI_FULL_DIG) {
678 spec->multiout.dig_out_nid = CMI_DIG_OUT_NID;
679 spec->dig_in_nid = CMI_DIG_IN_NID;
680 }
681 break;
682 case CMI_ALLOUT:
683 spec->front_panel = 1;
684 spec->multiout.max_channels = 8;
685 spec->no_line_in = 1;
686 spec->input_mux = &cmi9880_no_line_mux;
687 spec->multiout.dig_out_nid = CMI_DIG_OUT_NID;
688 break;
Takashi Iwaif953eff2005-04-08 15:05:13 +0200689 case CMI_AUTO:
690 {
691 unsigned int port_e, port_f, port_g, port_h;
692 unsigned int port_spdifi, port_spdifo;
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200693 struct auto_pin_cfg cfg;
694
Takashi Iwaif953eff2005-04-08 15:05:13 +0200695 /* collect pin default configuration */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200696 port_e = snd_hda_codec_read(codec, 0x0f, 0, AC_VERB_GET_CONFIG_DEFAULT, 0);
697 port_f = snd_hda_codec_read(codec, 0x10, 0, AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaif953eff2005-04-08 15:05:13 +0200698 spec->front_panel = 1;
Takashi Iwaid21b37e2005-04-20 13:45:55 +0200699 if (get_defcfg_connect(port_e) == AC_JACK_PORT_NONE ||
700 get_defcfg_connect(port_f) == AC_JACK_PORT_NONE) {
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200701 port_g = snd_hda_codec_read(codec, 0x1f, 0, AC_VERB_GET_CONFIG_DEFAULT, 0);
702 port_h = snd_hda_codec_read(codec, 0x20, 0, AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +0100703 spec->channel_modes = cmi9880_channel_modes;
Takashi Iwaif953eff2005-04-08 15:05:13 +0200704 /* no front panel */
Takashi Iwaid21b37e2005-04-20 13:45:55 +0200705 if (get_defcfg_connect(port_g) == AC_JACK_PORT_NONE ||
706 get_defcfg_connect(port_h) == AC_JACK_PORT_NONE) {
Takashi Iwaif953eff2005-04-08 15:05:13 +0200707 /* no optional rear panel */
708 spec->board_config = CMI_MINIMAL;
709 spec->front_panel = 0;
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +0100710 spec->num_channel_modes = 2;
Takashi Iwaid21b37e2005-04-20 13:45:55 +0200711 } else {
Takashi Iwaif953eff2005-04-08 15:05:13 +0200712 spec->board_config = CMI_MIN_FP;
Takashi Iwaid2a6d7d2005-11-17 11:06:29 +0100713 spec->num_channel_modes = 3;
Takashi Iwaid21b37e2005-04-20 13:45:55 +0200714 }
Takashi Iwaif953eff2005-04-08 15:05:13 +0200715 spec->input_mux = &cmi9880_basic_mux;
Takashi Iwaid21b37e2005-04-20 13:45:55 +0200716 spec->multiout.max_channels = cmi9880_channel_modes[0].channels;
Takashi Iwaif953eff2005-04-08 15:05:13 +0200717 } else {
718 spec->input_mux = &cmi9880_basic_mux;
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200719 port_spdifi = snd_hda_codec_read(codec, 0x13, 0, AC_VERB_GET_CONFIG_DEFAULT, 0);
720 port_spdifo = snd_hda_codec_read(codec, 0x12, 0, AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaid21b37e2005-04-20 13:45:55 +0200721 if (get_defcfg_connect(port_spdifo) != AC_JACK_PORT_NONE)
Takashi Iwaif953eff2005-04-08 15:05:13 +0200722 spec->multiout.dig_out_nid = CMI_DIG_OUT_NID;
Takashi Iwaid21b37e2005-04-20 13:45:55 +0200723 if (get_defcfg_connect(port_spdifi) != AC_JACK_PORT_NONE)
Takashi Iwaif953eff2005-04-08 15:05:13 +0200724 spec->dig_in_nid = CMI_DIG_IN_NID;
Takashi Iwaid21b37e2005-04-20 13:45:55 +0200725 spec->multiout.max_channels = 8;
Takashi Iwaif953eff2005-04-08 15:05:13 +0200726 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200727 snd_hda_parse_pin_def_config(codec, &cfg);
728 if (cfg.line_outs) {
729 spec->multiout.max_channels = cfg.line_outs * 2;
730 cmi9880_fill_multi_dac_nids(codec, &cfg);
731 cmi9880_fill_multi_init(codec, &cfg);
Takashi Iwaid21b37e2005-04-20 13:45:55 +0200732 } else
733 snd_printd("patch_cmedia: cannot detect association in defcfg\n");
Takashi Iwaif953eff2005-04-08 15:05:13 +0200734 break;
Takashi Iwaid21b37e2005-04-20 13:45:55 +0200735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 }
737
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200738 spec->multiout.num_dacs = spec->num_dacs;
Takashi Iwaif953eff2005-04-08 15:05:13 +0200739 spec->multiout.dac_nids = spec->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
741 spec->adc_nids = cmi9880_adc_nids;
742
743 codec->patch_ops = cmi9880_patch_ops;
744
745 return 0;
746}
747
748/*
749 * patch entries
750 */
751struct hda_codec_preset snd_hda_preset_cmedia[] = {
752 { .id = 0x13f69880, .name = "CMI9880", .patch = patch_cmi9880 },
753 { .id = 0x434d4980, .name = "CMI9880", .patch = patch_cmi9880 },
754 {} /* terminator */
755};