blob: 07fb4f5a54b36558c65575acf630ec6951f5b559 [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;
47 unsigned int surr_switch: 1; /* switchable line,mic */
48 unsigned int no_line_in: 1; /* no line-in (5-jack) */
49 unsigned int front_panel: 1; /* has front-panel 2-jack */
50
51 /* playback */
52 struct hda_multi_out multiout;
Takashi Iwaif953eff2005-04-08 15:05:13 +020053 hda_nid_t dac_nids[4]; /* NID for each DAC */
Takashi Iwaie9edcee2005-06-13 14:16:38 +020054 int num_dacs;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56 /* capture */
57 hda_nid_t *adc_nids;
58 hda_nid_t dig_in_nid;
59
60 /* capture source */
61 const struct hda_input_mux *input_mux;
62 unsigned int cur_mux[2];
63
64 /* channel mode */
65 unsigned int num_ch_modes;
66 unsigned int cur_ch_mode;
67 const struct cmi_channel_mode *channel_modes;
68
69 struct hda_pcm pcm_rec[2]; /* PCM information */
Takashi Iwaif953eff2005-04-08 15:05:13 +020070
71 /* pin deafault configuration */
72 hda_nid_t pin_nid[NUM_PINS];
73 unsigned int def_conf[NUM_PINS];
74 unsigned int pin_def_confs;
75
76 /* multichannel pins */
77 hda_nid_t multich_pin[4]; /* max 8-channel */
78 struct hda_verb multi_init[9]; /* 2 verbs for each pin + terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -070079};
80
Takashi Iwaie9edcee2005-06-13 14:16:38 +020081/* amp values */
82#define AMP_IN_MUTE(idx) (0x7080 | ((idx)<<8))
83#define AMP_IN_UNMUTE(idx) (0x7000 | ((idx)<<8))
84#define AMP_OUT_MUTE 0xb080
85#define AMP_OUT_UNMUTE 0xb000
86#define AMP_OUT_ZERO 0xb000
87/* pinctl values */
88#define PIN_IN 0x20
89#define PIN_VREF80 0x24
90#define PIN_VREF50 0x21
91#define PIN_OUT 0x40
92#define PIN_HP 0xc0
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094/*
95 * input MUX
96 */
97static int cmi_mux_enum_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
98{
99 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
100 struct cmi_spec *spec = codec->spec;
101 return snd_hda_input_mux_info(spec->input_mux, uinfo);
102}
103
104static int cmi_mux_enum_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
105{
106 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
107 struct cmi_spec *spec = codec->spec;
108 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
109
110 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
111 return 0;
112}
113
114static int cmi_mux_enum_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
115{
116 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
117 struct cmi_spec *spec = codec->spec;
118 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
119
120 return snd_hda_input_mux_put(codec, spec->input_mux, ucontrol,
121 spec->adc_nids[adc_idx], &spec->cur_mux[adc_idx]);
122}
123
124/*
125 * shared line-in, mic for surrounds
126 */
127
128/* 3-stack / 2 channel */
129static struct hda_verb cmi9880_ch2_init[] = {
130 /* set line-in PIN for input */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200131 { 0x0c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 /* set mic PIN for input, also enable vref */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200133 { 0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 /* route front PCM (DAC1) to HP */
135 { 0x0f, AC_VERB_SET_CONNECT_SEL, 0x00 },
136 {}
137};
138
139/* 3-stack / 6 channel */
140static struct hda_verb cmi9880_ch6_init[] = {
141 /* set line-in PIN for output */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200142 { 0x0c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 /* set mic PIN for output */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200144 { 0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 /* route front PCM (DAC1) to HP */
146 { 0x0f, AC_VERB_SET_CONNECT_SEL, 0x00 },
147 {}
148};
149
150/* 3-stack+front / 8 channel */
151static struct hda_verb cmi9880_ch8_init[] = {
152 /* set line-in PIN for output */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200153 { 0x0c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 /* set mic PIN for output */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200155 { 0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 /* route rear-surround PCM (DAC4) to HP */
157 { 0x0f, AC_VERB_SET_CONNECT_SEL, 0x03 },
158 {}
159};
160
161struct cmi_channel_mode {
162 unsigned int channels;
163 const struct hda_verb *sequence;
164};
165
166static struct cmi_channel_mode cmi9880_channel_modes[3] = {
167 { 2, cmi9880_ch2_init },
168 { 6, cmi9880_ch6_init },
169 { 8, cmi9880_ch8_init },
170};
171
172static int cmi_ch_mode_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
173{
174 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
175 struct cmi_spec *spec = codec->spec;
176
177 snd_assert(spec->channel_modes, return -EINVAL);
178 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
179 uinfo->count = 1;
180 uinfo->value.enumerated.items = spec->num_ch_modes;
181 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
182 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
183 sprintf(uinfo->value.enumerated.name, "%dch",
184 spec->channel_modes[uinfo->value.enumerated.item].channels);
185 return 0;
186}
187
188static int cmi_ch_mode_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
189{
190 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
191 struct cmi_spec *spec = codec->spec;
192
193 ucontrol->value.enumerated.item[0] = spec->cur_ch_mode;
194 return 0;
195}
196
197static int cmi_ch_mode_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
198{
199 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
200 struct cmi_spec *spec = codec->spec;
201
202 snd_assert(spec->channel_modes, return -EINVAL);
203 if (ucontrol->value.enumerated.item[0] >= spec->num_ch_modes)
204 ucontrol->value.enumerated.item[0] = spec->num_ch_modes;
205 if (ucontrol->value.enumerated.item[0] == spec->cur_ch_mode &&
206 ! codec->in_resume)
207 return 0;
208
209 spec->cur_ch_mode = ucontrol->value.enumerated.item[0];
210 snd_hda_sequence_write(codec, spec->channel_modes[spec->cur_ch_mode].sequence);
211 spec->multiout.max_channels = spec->channel_modes[spec->cur_ch_mode].channels;
212 return 1;
213}
214
215/*
216 */
217static snd_kcontrol_new_t cmi9880_basic_mixer[] = {
218 /* CMI9880 has no playback volumes! */
219 HDA_CODEC_MUTE("PCM Playback Switch", 0x03, 0x0, HDA_OUTPUT), /* front */
220 HDA_CODEC_MUTE("Surround Playback Switch", 0x04, 0x0, HDA_OUTPUT),
221 HDA_CODEC_MUTE_MONO("Center Playback Switch", 0x05, 1, 0x0, HDA_OUTPUT),
222 HDA_CODEC_MUTE_MONO("LFE Playback Switch", 0x05, 2, 0x0, HDA_OUTPUT),
223 HDA_CODEC_MUTE("Side Playback Switch", 0x06, 0x0, HDA_OUTPUT),
224 {
225 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
226 /* The multiple "Capture Source" controls confuse alsamixer
227 * So call somewhat different..
228 * FIXME: the controls appear in the "playback" view!
229 */
230 /* .name = "Capture Source", */
231 .name = "Input Source",
232 .count = 2,
233 .info = cmi_mux_enum_info,
234 .get = cmi_mux_enum_get,
235 .put = cmi_mux_enum_put,
236 },
237 HDA_CODEC_VOLUME("Capture Volume", 0x08, 0, HDA_INPUT),
238 HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x09, 0, HDA_INPUT),
239 HDA_CODEC_MUTE("Capture Switch", 0x08, 0, HDA_INPUT),
240 HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x09, 0, HDA_INPUT),
241 HDA_CODEC_VOLUME("PC Speaker Playback Volume", 0x23, 0, HDA_OUTPUT),
242 HDA_CODEC_MUTE("PC Speaker Playback Switch", 0x23, 0, HDA_OUTPUT),
243 { } /* end */
244};
245
246/*
247 * shared I/O pins
248 */
249static snd_kcontrol_new_t cmi9880_ch_mode_mixer[] = {
250 {
251 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
252 .name = "Channel Mode",
253 .info = cmi_ch_mode_info,
254 .get = cmi_ch_mode_get,
255 .put = cmi_ch_mode_put,
256 },
257 { } /* end */
258};
259
260/* AUD-in selections:
261 * 0x0b 0x0c 0x0d 0x0e 0x0f 0x10 0x11 0x1f 0x20
262 */
263static struct hda_input_mux cmi9880_basic_mux = {
264 .num_items = 4,
265 .items = {
266 { "Front Mic", 0x5 },
267 { "Rear Mic", 0x2 },
268 { "Line", 0x1 },
269 { "CD", 0x7 },
270 }
271};
272
273static struct hda_input_mux cmi9880_no_line_mux = {
274 .num_items = 3,
275 .items = {
276 { "Front Mic", 0x5 },
277 { "Rear Mic", 0x2 },
278 { "CD", 0x7 },
279 }
280};
281
282/* front, rear, clfe, rear_surr */
283static hda_nid_t cmi9880_dac_nids[4] = {
284 0x03, 0x04, 0x05, 0x06
285};
286/* ADC0, ADC1 */
287static hda_nid_t cmi9880_adc_nids[2] = {
288 0x08, 0x09
289};
290
291#define CMI_DIG_OUT_NID 0x07
292#define CMI_DIG_IN_NID 0x0a
293
294/*
295 */
296static struct hda_verb cmi9880_basic_init[] = {
297 /* port-D for line out (rear panel) */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200298 { 0x0b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 /* port-E for HP out (front panel) */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200300 { 0x0f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 /* route front PCM to HP */
302 { 0x0f, AC_VERB_SET_CONNECT_SEL, 0x00 },
303 /* port-A for surround (rear panel) */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200304 { 0x0e, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 /* port-G for CLFE (rear panel) */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200306 { 0x1f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
ChenLi Tiend05b2812005-03-24 20:47:35 +0100307 { 0x1f, AC_VERB_SET_CONNECT_SEL, 0x02 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 /* port-H for side (rear panel) */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200309 { 0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
ChenLi Tiend05b2812005-03-24 20:47:35 +0100310 { 0x20, AC_VERB_SET_CONNECT_SEL, 0x01 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 /* port-C for line-in (rear panel) */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200312 { 0x0c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 /* port-B for mic-in (rear panel) with vref */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200314 { 0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 /* port-F for mic-in (front panel) with vref */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200316 { 0x10, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 /* CD-in */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200318 { 0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 /* route front mic to ADC1/2 */
320 { 0x08, AC_VERB_SET_CONNECT_SEL, 0x05 },
321 { 0x09, AC_VERB_SET_CONNECT_SEL, 0x05 },
322 {} /* terminator */
323};
324
325static struct hda_verb cmi9880_allout_init[] = {
326 /* port-D for line out (rear panel) */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200327 { 0x0b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 /* port-E for HP out (front panel) */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200329 { 0x0f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 /* route front PCM to HP */
331 { 0x0f, AC_VERB_SET_CONNECT_SEL, 0x00 },
332 /* port-A for side (rear panel) */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200333 { 0x0e, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 /* port-G for CLFE (rear panel) */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200335 { 0x1f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
ChenLi Tiend05b2812005-03-24 20:47:35 +0100336 { 0x1f, AC_VERB_SET_CONNECT_SEL, 0x02 },
337 /* port-H for side (rear panel) */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200338 { 0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
ChenLi Tiend05b2812005-03-24 20:47:35 +0100339 { 0x20, AC_VERB_SET_CONNECT_SEL, 0x01 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 /* port-C for surround (rear panel) */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200341 { 0x0c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 /* port-B for mic-in (rear panel) with vref */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200343 { 0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 /* port-F for mic-in (front panel) with vref */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200345 { 0x10, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 /* CD-in */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200347 { 0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 /* route front mic to ADC1/2 */
349 { 0x08, AC_VERB_SET_CONNECT_SEL, 0x05 },
350 { 0x09, AC_VERB_SET_CONNECT_SEL, 0x05 },
351 {} /* terminator */
352};
353
354/*
355 */
356static int cmi9880_build_controls(struct hda_codec *codec)
357{
358 struct cmi_spec *spec = codec->spec;
359 int err;
360
361 err = snd_hda_add_new_ctls(codec, cmi9880_basic_mixer);
362 if (err < 0)
363 return err;
364 if (spec->surr_switch) {
365 err = snd_hda_add_new_ctls(codec, cmi9880_ch_mode_mixer);
366 if (err < 0)
367 return err;
368 }
369 if (spec->multiout.dig_out_nid) {
370 err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid);
371 if (err < 0)
372 return err;
373 }
374 if (spec->dig_in_nid) {
375 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
376 if (err < 0)
377 return err;
378 }
379 return 0;
380}
381
Takashi Iwaif953eff2005-04-08 15:05:13 +0200382/* fill in the multi_dac_nids table, which will decide
383 which audio widget to use for each channel */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200384static int cmi9880_fill_multi_dac_nids(struct hda_codec *codec, const struct auto_pin_cfg *cfg)
Takashi Iwaif953eff2005-04-08 15:05:13 +0200385{
386 struct cmi_spec *spec = codec->spec;
387 hda_nid_t nid;
388 int assigned[4];
389 int i, j;
390
391 /* clear the table, only one c-media dac assumed here */
392 memset(spec->dac_nids, 0, sizeof(spec->dac_nids));
393 memset(assigned, 0, sizeof(assigned));
394 /* check the pins we found */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200395 for (i = 0; i < cfg->line_outs; i++) {
396 nid = cfg->line_out_pins[i];
Takashi Iwaif953eff2005-04-08 15:05:13 +0200397 /* nid 0x0b~0x0e is hardwired to audio widget 0x3~0x6 */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200398 if (nid >= 0x0b && nid <= 0x0e) {
399 spec->dac_nids[i] = (nid - 0x0b) + 0x03;
Takashi Iwaif953eff2005-04-08 15:05:13 +0200400 assigned[nid - 0x0b] = 1;
401 }
402 }
403 /* left pin can be connect to any audio widget */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200404 for (i = 0; i < cfg->line_outs; i++) {
405 nid = cfg->line_out_pins[i];
406 if (nid <= 0x0e)
407 continue;
408 /* search for an empty channel */
409 for (j = 0; j < cfg->line_outs; j++) {
410 if (! assigned[j]) {
Nicolas Grazianofb92e6f2005-07-27 17:25:08 +0200411 spec->dac_nids[i] = j + 0x03;
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200412 assigned[j] = 1;
413 break;
414 }
Takashi Iwaif953eff2005-04-08 15:05:13 +0200415 }
416 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200417 spec->num_dacs = cfg->line_outs;
Takashi Iwaif953eff2005-04-08 15:05:13 +0200418 return 0;
419}
420
421/* create multi_init table, which is used for multichannel initialization */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200422static int cmi9880_fill_multi_init(struct hda_codec *codec, const struct auto_pin_cfg *cfg)
Takashi Iwaif953eff2005-04-08 15:05:13 +0200423{
424 struct cmi_spec *spec = codec->spec;
425 hda_nid_t nid;
426 int i, j, k, len;
427
428 /* clear the table, only one c-media dac assumed here */
429 memset(spec->multi_init, 0, sizeof(spec->multi_init));
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200430 for (j = 0, i = 0; i < cfg->line_outs; i++) {
Takashi Iwaif953eff2005-04-08 15:05:13 +0200431 hda_nid_t conn[4];
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200432 nid = cfg->line_out_pins[i];
Takashi Iwaif953eff2005-04-08 15:05:13 +0200433 /* set as output */
434 spec->multi_init[j].nid = nid;
435 spec->multi_init[j].verb = AC_VERB_SET_PIN_WIDGET_CONTROL;
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200436 spec->multi_init[j].param = PIN_OUT;
Takashi Iwaif953eff2005-04-08 15:05:13 +0200437 j++;
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200438 if (nid > 0x0e) {
Takashi Iwaif953eff2005-04-08 15:05:13 +0200439 /* set connection */
440 spec->multi_init[j].nid = nid;
441 spec->multi_init[j].verb = AC_VERB_SET_CONNECT_SEL;
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200442 spec->multi_init[j].param = 0;
Takashi Iwaif953eff2005-04-08 15:05:13 +0200443 /* find the index in connect list */
444 len = snd_hda_get_connections(codec, nid, conn, 4);
445 for (k = 0; k < len; k++)
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200446 if (conn[k] == spec->dac_nids[i]) {
Nicolas Grazianofb92e6f2005-07-27 17:25:08 +0200447 spec->multi_init[j].param = k;
Takashi Iwaif953eff2005-04-08 15:05:13 +0200448 break;
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200449 }
Takashi Iwaif953eff2005-04-08 15:05:13 +0200450 j++;
Takashi Iwaif953eff2005-04-08 15:05:13 +0200451 }
452 }
453 return 0;
454}
455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456static int cmi9880_init(struct hda_codec *codec)
457{
458 struct cmi_spec *spec = codec->spec;
459 if (spec->board_config == CMI_ALLOUT)
460 snd_hda_sequence_write(codec, cmi9880_allout_init);
461 else
462 snd_hda_sequence_write(codec, cmi9880_basic_init);
Takashi Iwaif953eff2005-04-08 15:05:13 +0200463 if (spec->board_config == CMI_AUTO)
464 snd_hda_sequence_write(codec, spec->multi_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 return 0;
466}
467
468#ifdef CONFIG_PM
469/*
470 * resume
471 */
472static int cmi9880_resume(struct hda_codec *codec)
473{
474 struct cmi_spec *spec = codec->spec;
475
476 cmi9880_init(codec);
477 snd_hda_resume_ctls(codec, cmi9880_basic_mixer);
478 if (spec->surr_switch)
479 snd_hda_resume_ctls(codec, cmi9880_ch_mode_mixer);
480 if (spec->multiout.dig_out_nid)
481 snd_hda_resume_spdif_out(codec);
482 if (spec->dig_in_nid)
483 snd_hda_resume_spdif_in(codec);
484
485 return 0;
486}
487#endif
488
489/*
490 * Analog playback callbacks
491 */
492static int cmi9880_playback_pcm_open(struct hda_pcm_stream *hinfo,
493 struct hda_codec *codec,
494 snd_pcm_substream_t *substream)
495{
496 struct cmi_spec *spec = codec->spec;
497 return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream);
498}
499
500static int cmi9880_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
501 struct hda_codec *codec,
502 unsigned int stream_tag,
503 unsigned int format,
504 snd_pcm_substream_t *substream)
505{
506 struct cmi_spec *spec = codec->spec;
507 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout, stream_tag,
508 format, substream);
509}
510
511static int cmi9880_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
512 struct hda_codec *codec,
513 snd_pcm_substream_t *substream)
514{
515 struct cmi_spec *spec = codec->spec;
516 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
517}
518
519/*
520 * Digital out
521 */
522static int cmi9880_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
523 struct hda_codec *codec,
524 snd_pcm_substream_t *substream)
525{
526 struct cmi_spec *spec = codec->spec;
527 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
528}
529
530static int cmi9880_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
531 struct hda_codec *codec,
532 snd_pcm_substream_t *substream)
533{
534 struct cmi_spec *spec = codec->spec;
535 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
536}
537
538/*
539 * Analog capture
540 */
541static int cmi9880_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
542 struct hda_codec *codec,
543 unsigned int stream_tag,
544 unsigned int format,
545 snd_pcm_substream_t *substream)
546{
547 struct cmi_spec *spec = codec->spec;
548
549 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
550 stream_tag, 0, format);
551 return 0;
552}
553
554static int cmi9880_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
555 struct hda_codec *codec,
556 snd_pcm_substream_t *substream)
557{
558 struct cmi_spec *spec = codec->spec;
559
560 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number], 0, 0, 0);
561 return 0;
562}
563
564
565/*
566 */
567static struct hda_pcm_stream cmi9880_pcm_analog_playback = {
568 .substreams = 1,
569 .channels_min = 2,
570 .channels_max = 8,
571 .nid = 0x03, /* NID to query formats and rates */
572 .ops = {
573 .open = cmi9880_playback_pcm_open,
574 .prepare = cmi9880_playback_pcm_prepare,
575 .cleanup = cmi9880_playback_pcm_cleanup
576 },
577};
578
579static struct hda_pcm_stream cmi9880_pcm_analog_capture = {
580 .substreams = 2,
581 .channels_min = 2,
582 .channels_max = 2,
583 .nid = 0x08, /* NID to query formats and rates */
584 .ops = {
585 .prepare = cmi9880_capture_pcm_prepare,
586 .cleanup = cmi9880_capture_pcm_cleanup
587 },
588};
589
590static struct hda_pcm_stream cmi9880_pcm_digital_playback = {
591 .substreams = 1,
592 .channels_min = 2,
593 .channels_max = 2,
594 /* NID is set in cmi9880_build_pcms */
595 .ops = {
596 .open = cmi9880_dig_playback_pcm_open,
597 .close = cmi9880_dig_playback_pcm_close
598 },
599};
600
601static struct hda_pcm_stream cmi9880_pcm_digital_capture = {
602 .substreams = 1,
603 .channels_min = 2,
604 .channels_max = 2,
605 /* NID is set in cmi9880_build_pcms */
606};
607
608static int cmi9880_build_pcms(struct hda_codec *codec)
609{
610 struct cmi_spec *spec = codec->spec;
611 struct hda_pcm *info = spec->pcm_rec;
612
613 codec->num_pcms = 1;
614 codec->pcm_info = info;
615
616 info->name = "CMI9880";
617 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = cmi9880_pcm_analog_playback;
618 info->stream[SNDRV_PCM_STREAM_CAPTURE] = cmi9880_pcm_analog_capture;
619
620 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
621 codec->num_pcms++;
622 info++;
623 info->name = "CMI9880 Digital";
624 if (spec->multiout.dig_out_nid) {
625 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = cmi9880_pcm_digital_playback;
626 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
627 }
628 if (spec->dig_in_nid) {
629 info->stream[SNDRV_PCM_STREAM_CAPTURE] = cmi9880_pcm_digital_capture;
630 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
631 }
632 }
633
634 return 0;
635}
636
637static void cmi9880_free(struct hda_codec *codec)
638{
639 kfree(codec->spec);
640}
641
642/*
643 */
644
645static struct hda_board_config cmi9880_cfg_tbl[] = {
646 { .modelname = "minimal", .config = CMI_MINIMAL },
647 { .modelname = "min_fp", .config = CMI_MIN_FP },
648 { .modelname = "full", .config = CMI_FULL },
649 { .modelname = "full_dig", .config = CMI_FULL_DIG },
Takashi Iwaid8275602005-07-29 11:56:41 +0200650 { .pci_subvendor = 0x1043, .pci_subdevice = 0x813d, .config = CMI_FULL_DIG }, /* ASUS P5AD2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 { .modelname = "allout", .config = CMI_ALLOUT },
Takashi Iwaif953eff2005-04-08 15:05:13 +0200652 { .modelname = "auto", .config = CMI_AUTO },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 {} /* terminator */
654};
655
656static struct hda_codec_ops cmi9880_patch_ops = {
657 .build_controls = cmi9880_build_controls,
658 .build_pcms = cmi9880_build_pcms,
659 .init = cmi9880_init,
660 .free = cmi9880_free,
661#ifdef CONFIG_PM
662 .resume = cmi9880_resume,
663#endif
664};
665
666static int patch_cmi9880(struct hda_codec *codec)
667{
668 struct cmi_spec *spec;
669
670 spec = kcalloc(1, sizeof(*spec), GFP_KERNEL);
671 if (spec == NULL)
672 return -ENOMEM;
673
674 codec->spec = spec;
675 spec->board_config = snd_hda_check_board_config(codec, cmi9880_cfg_tbl);
676 if (spec->board_config < 0) {
Takashi Iwaif953eff2005-04-08 15:05:13 +0200677 snd_printdd(KERN_INFO "hda_codec: Unknown model for CMI9880\n");
678 spec->board_config = CMI_AUTO; /* try everything */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 }
680
Takashi Iwaif953eff2005-04-08 15:05:13 +0200681 /* copy default DAC NIDs */
682 memcpy(spec->dac_nids, cmi9880_dac_nids, sizeof(spec->dac_nids));
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200683 spec->num_dacs = 4;
Takashi Iwaif953eff2005-04-08 15:05:13 +0200684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 switch (spec->board_config) {
686 case CMI_MINIMAL:
687 case CMI_MIN_FP:
688 spec->surr_switch = 1;
689 if (spec->board_config == CMI_MINIMAL)
690 spec->num_ch_modes = 2;
691 else {
692 spec->front_panel = 1;
693 spec->num_ch_modes = 3;
694 }
695 spec->channel_modes = cmi9880_channel_modes;
696 spec->multiout.max_channels = cmi9880_channel_modes[0].channels;
697 spec->input_mux = &cmi9880_basic_mux;
698 break;
699 case CMI_FULL:
700 case CMI_FULL_DIG:
701 spec->front_panel = 1;
702 spec->multiout.max_channels = 8;
703 spec->input_mux = &cmi9880_basic_mux;
704 if (spec->board_config == CMI_FULL_DIG) {
705 spec->multiout.dig_out_nid = CMI_DIG_OUT_NID;
706 spec->dig_in_nid = CMI_DIG_IN_NID;
707 }
708 break;
709 case CMI_ALLOUT:
710 spec->front_panel = 1;
711 spec->multiout.max_channels = 8;
712 spec->no_line_in = 1;
713 spec->input_mux = &cmi9880_no_line_mux;
714 spec->multiout.dig_out_nid = CMI_DIG_OUT_NID;
715 break;
Takashi Iwaif953eff2005-04-08 15:05:13 +0200716 case CMI_AUTO:
717 {
718 unsigned int port_e, port_f, port_g, port_h;
719 unsigned int port_spdifi, port_spdifo;
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200720 struct auto_pin_cfg cfg;
721
Takashi Iwaif953eff2005-04-08 15:05:13 +0200722 /* collect pin default configuration */
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200723 port_e = snd_hda_codec_read(codec, 0x0f, 0, AC_VERB_GET_CONFIG_DEFAULT, 0);
724 port_f = snd_hda_codec_read(codec, 0x10, 0, AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaif953eff2005-04-08 15:05:13 +0200725 spec->front_panel = 1;
Takashi Iwaid21b37e2005-04-20 13:45:55 +0200726 if (get_defcfg_connect(port_e) == AC_JACK_PORT_NONE ||
727 get_defcfg_connect(port_f) == AC_JACK_PORT_NONE) {
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200728 port_g = snd_hda_codec_read(codec, 0x1f, 0, AC_VERB_GET_CONFIG_DEFAULT, 0);
729 port_h = snd_hda_codec_read(codec, 0x20, 0, AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaif953eff2005-04-08 15:05:13 +0200730 spec->surr_switch = 1;
731 /* no front panel */
Takashi Iwaid21b37e2005-04-20 13:45:55 +0200732 if (get_defcfg_connect(port_g) == AC_JACK_PORT_NONE ||
733 get_defcfg_connect(port_h) == AC_JACK_PORT_NONE) {
Takashi Iwaif953eff2005-04-08 15:05:13 +0200734 /* no optional rear panel */
735 spec->board_config = CMI_MINIMAL;
736 spec->front_panel = 0;
737 spec->num_ch_modes = 2;
Takashi Iwaid21b37e2005-04-20 13:45:55 +0200738 } else {
Takashi Iwaif953eff2005-04-08 15:05:13 +0200739 spec->board_config = CMI_MIN_FP;
740 spec->num_ch_modes = 3;
Takashi Iwaid21b37e2005-04-20 13:45:55 +0200741 }
Takashi Iwaif953eff2005-04-08 15:05:13 +0200742 spec->channel_modes = cmi9880_channel_modes;
743 spec->input_mux = &cmi9880_basic_mux;
Takashi Iwaid21b37e2005-04-20 13:45:55 +0200744 spec->multiout.max_channels = cmi9880_channel_modes[0].channels;
Takashi Iwaif953eff2005-04-08 15:05:13 +0200745 } else {
746 spec->input_mux = &cmi9880_basic_mux;
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200747 port_spdifi = snd_hda_codec_read(codec, 0x13, 0, AC_VERB_GET_CONFIG_DEFAULT, 0);
748 port_spdifo = snd_hda_codec_read(codec, 0x12, 0, AC_VERB_GET_CONFIG_DEFAULT, 0);
Takashi Iwaid21b37e2005-04-20 13:45:55 +0200749 if (get_defcfg_connect(port_spdifo) != AC_JACK_PORT_NONE)
Takashi Iwaif953eff2005-04-08 15:05:13 +0200750 spec->multiout.dig_out_nid = CMI_DIG_OUT_NID;
Takashi Iwaid21b37e2005-04-20 13:45:55 +0200751 if (get_defcfg_connect(port_spdifi) != AC_JACK_PORT_NONE)
Takashi Iwaif953eff2005-04-08 15:05:13 +0200752 spec->dig_in_nid = CMI_DIG_IN_NID;
Takashi Iwaid21b37e2005-04-20 13:45:55 +0200753 spec->multiout.max_channels = 8;
Takashi Iwaif953eff2005-04-08 15:05:13 +0200754 }
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200755 snd_hda_parse_pin_def_config(codec, &cfg);
756 if (cfg.line_outs) {
757 spec->multiout.max_channels = cfg.line_outs * 2;
758 cmi9880_fill_multi_dac_nids(codec, &cfg);
759 cmi9880_fill_multi_init(codec, &cfg);
Takashi Iwaid21b37e2005-04-20 13:45:55 +0200760 } else
761 snd_printd("patch_cmedia: cannot detect association in defcfg\n");
Takashi Iwaif953eff2005-04-08 15:05:13 +0200762 break;
Takashi Iwaid21b37e2005-04-20 13:45:55 +0200763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 }
765
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200766 spec->multiout.num_dacs = spec->num_dacs;
Takashi Iwaif953eff2005-04-08 15:05:13 +0200767 spec->multiout.dac_nids = spec->dac_nids;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
769 spec->adc_nids = cmi9880_adc_nids;
770
771 codec->patch_ops = cmi9880_patch_ops;
772
773 return 0;
774}
775
776/*
777 * patch entries
778 */
779struct hda_codec_preset snd_hda_preset_cmedia[] = {
780 { .id = 0x13f69880, .name = "CMI9880", .patch = patch_cmi9880 },
781 { .id = 0x434d4980, .name = "CMI9880", .patch = patch_cmi9880 },
782 {} /* terminator */
783};