patch_cmedia.c 22.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*
 * Universal Interface for Intel High Definition Audio Codec
 *
 * HD audio interface patch for C-Media CMI9880
 *
 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
 *
 *
 *  This driver is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This driver is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 */

#include <linux/init.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/pci.h>
#include <sound/core.h>
#include "hda_codec.h"
#include "hda_local.h"
31
#define NUM_PINS	11
L
Linus Torvalds 已提交
32 33 34 35 36 37 38 39 40


/* board config type */
enum {
	CMI_MINIMAL,	/* back 3-jack */
	CMI_MIN_FP,	/* back 3-jack + front-panel 2-jack */
	CMI_FULL,	/* back 6-jack + front-panel 2-jack */
	CMI_FULL_DIG,	/* back 6-jack + front-panel 2-jack + digital I/O */
	CMI_ALLOUT,	/* back 5-jack + front-panel 2-jack + digital out */
41
	CMI_AUTO,	/* let driver guess it */
42
	CMI_MODELS
L
Linus Torvalds 已提交
43 44 45 46 47 48 49 50 51
};

struct cmi_spec {
	int board_config;
	unsigned int no_line_in: 1;	/* no line-in (5-jack) */
	unsigned int front_panel: 1;	/* has front-panel 2-jack */

	/* playback */
	struct hda_multi_out multiout;
52
	hda_nid_t dac_nids[AUTO_CFG_MAX_OUTS];	/* NID for each DAC */
53
	int num_dacs;
L
Linus Torvalds 已提交
54 55

	/* capture */
56
	const hda_nid_t *adc_nids;
L
Linus Torvalds 已提交
57 58 59 60 61 62 63
	hda_nid_t dig_in_nid;

	/* capture source */
	const struct hda_input_mux *input_mux;
	unsigned int cur_mux[2];

	/* channel mode */
64 65
	int num_channel_modes;
	const struct hda_channel_mode *channel_modes;
L
Linus Torvalds 已提交
66 67

	struct hda_pcm pcm_rec[2];	/* PCM information */
68

69
	/* pin default configuration */
70 71 72 73 74 75
	hda_nid_t pin_nid[NUM_PINS];
	unsigned int def_conf[NUM_PINS];
	unsigned int pin_def_confs;

	/* multichannel pins */
	struct hda_verb multi_init[9];	/* 2 verbs for each pin + terminator */
L
Linus Torvalds 已提交
76 77 78 79 80
};

/*
 * input MUX
 */
81
static int cmi_mux_enum_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
L
Linus Torvalds 已提交
82 83 84 85 86 87
{
	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
	struct cmi_spec *spec = codec->spec;
	return snd_hda_input_mux_info(spec->input_mux, uinfo);
}

88
static int cmi_mux_enum_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
89 90 91 92 93 94 95 96 97
{
	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
	struct cmi_spec *spec = codec->spec;
	unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);

	ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
	return 0;
}

98
static int cmi_mux_enum_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112
{
	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
	struct cmi_spec *spec = codec->spec;
	unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);

	return snd_hda_input_mux_put(codec, spec->input_mux, ucontrol,
				     spec->adc_nids[adc_idx], &spec->cur_mux[adc_idx]);
}

/*
 * shared line-in, mic for surrounds
 */

/* 3-stack / 2 channel */
113
static const struct hda_verb cmi9880_ch2_init[] = {
L
Linus Torvalds 已提交
114
	/* set line-in PIN for input */
115
	{ 0x0c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
L
Linus Torvalds 已提交
116
	/* set mic PIN for input, also enable vref */
117
	{ 0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
L
Linus Torvalds 已提交
118 119 120 121 122 123
	/* route front PCM (DAC1) to HP */
	{ 0x0f, AC_VERB_SET_CONNECT_SEL, 0x00 },
	{}
};

/* 3-stack / 6 channel */
124
static const struct hda_verb cmi9880_ch6_init[] = {
L
Linus Torvalds 已提交
125
	/* set line-in PIN for output */
126
	{ 0x0c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
L
Linus Torvalds 已提交
127
	/* set mic PIN for output */
128
	{ 0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
L
Linus Torvalds 已提交
129 130 131 132 133 134
	/* route front PCM (DAC1) to HP */
	{ 0x0f, AC_VERB_SET_CONNECT_SEL, 0x00 },
	{}
};

/* 3-stack+front / 8 channel */
135
static const struct hda_verb cmi9880_ch8_init[] = {
L
Linus Torvalds 已提交
136
	/* set line-in PIN for output */
137
	{ 0x0c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
L
Linus Torvalds 已提交
138
	/* set mic PIN for output */
139
	{ 0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
L
Linus Torvalds 已提交
140 141 142 143 144
	/* route rear-surround PCM (DAC4) to HP */
	{ 0x0f, AC_VERB_SET_CONNECT_SEL, 0x03 },
	{}
};

145
static const struct hda_channel_mode cmi9880_channel_modes[3] = {
L
Linus Torvalds 已提交
146 147 148 149 150
	{ 2, cmi9880_ch2_init },
	{ 6, cmi9880_ch6_init },
	{ 8, cmi9880_ch8_init },
};

151
static int cmi_ch_mode_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
L
Linus Torvalds 已提交
152 153 154
{
	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
	struct cmi_spec *spec = codec->spec;
155 156
	return snd_hda_ch_mode_info(codec, uinfo, spec->channel_modes,
				    spec->num_channel_modes);
L
Linus Torvalds 已提交
157 158
}

159
static int cmi_ch_mode_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
160 161 162
{
	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
	struct cmi_spec *spec = codec->spec;
163 164
	return snd_hda_ch_mode_get(codec, ucontrol, spec->channel_modes,
				   spec->num_channel_modes, spec->multiout.max_channels);
L
Linus Torvalds 已提交
165 166
}

167
static int cmi_ch_mode_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
168 169 170
{
	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
	struct cmi_spec *spec = codec->spec;
171 172
	return snd_hda_ch_mode_put(codec, ucontrol, spec->channel_modes,
				   spec->num_channel_modes, &spec->multiout.max_channels);
L
Linus Torvalds 已提交
173 174 175 176
}

/*
 */
177
static const struct snd_kcontrol_new cmi9880_basic_mixer[] = {
L
Linus Torvalds 已提交
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
	/* CMI9880 has no playback volumes! */
	HDA_CODEC_MUTE("PCM Playback Switch", 0x03, 0x0, HDA_OUTPUT), /* front */
	HDA_CODEC_MUTE("Surround Playback Switch", 0x04, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE_MONO("Center Playback Switch", 0x05, 1, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE_MONO("LFE Playback Switch", 0x05, 2, 0x0, HDA_OUTPUT),
	HDA_CODEC_MUTE("Side Playback Switch", 0x06, 0x0, HDA_OUTPUT),
	{
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
		/* The multiple "Capture Source" controls confuse alsamixer
		 * So call somewhat different..
		 */
		/* .name = "Capture Source", */
		.name = "Input Source",
		.count = 2,
		.info = cmi_mux_enum_info,
		.get = cmi_mux_enum_get,
		.put = cmi_mux_enum_put,
	},
	HDA_CODEC_VOLUME("Capture Volume", 0x08, 0, HDA_INPUT),
	HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x09, 0, HDA_INPUT),
	HDA_CODEC_MUTE("Capture Switch", 0x08, 0, HDA_INPUT),
	HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x09, 0, HDA_INPUT),
200 201
	HDA_CODEC_VOLUME("Beep Playback Volume", 0x23, 0, HDA_OUTPUT),
	HDA_CODEC_MUTE("Beep Playback Switch", 0x23, 0, HDA_OUTPUT),
L
Linus Torvalds 已提交
202 203 204 205 206 207
	{ } /* end */
};

/*
 * shared I/O pins
 */
208
static const struct snd_kcontrol_new cmi9880_ch_mode_mixer[] = {
L
Linus Torvalds 已提交
209 210 211 212 213 214 215 216 217 218 219 220 221
	{
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
		.name = "Channel Mode",
		.info = cmi_ch_mode_info,
		.get = cmi_ch_mode_get,
		.put = cmi_ch_mode_put,
	},
	{ } /* end */
};

/* AUD-in selections:
 * 0x0b 0x0c 0x0d 0x0e 0x0f 0x10 0x11 0x1f 0x20
 */
222
static const struct hda_input_mux cmi9880_basic_mux = {
L
Linus Torvalds 已提交
223 224 225 226 227 228 229 230 231
	.num_items = 4,
	.items = {
		{ "Front Mic", 0x5 },
		{ "Rear Mic", 0x2 },
		{ "Line", 0x1 },
		{ "CD", 0x7 },
	}
};

232
static const struct hda_input_mux cmi9880_no_line_mux = {
L
Linus Torvalds 已提交
233 234 235 236 237 238 239 240 241
	.num_items = 3,
	.items = {
		{ "Front Mic", 0x5 },
		{ "Rear Mic", 0x2 },
		{ "CD", 0x7 },
	}
};

/* front, rear, clfe, rear_surr */
242
static const hda_nid_t cmi9880_dac_nids[4] = {
L
Linus Torvalds 已提交
243 244 245
	0x03, 0x04, 0x05, 0x06
};
/* ADC0, ADC1 */
246
static const hda_nid_t cmi9880_adc_nids[2] = {
L
Linus Torvalds 已提交
247 248 249 250 251 252 253 254
	0x08, 0x09
};

#define CMI_DIG_OUT_NID	0x07
#define CMI_DIG_IN_NID	0x0a

/*
 */
255
static const struct hda_verb cmi9880_basic_init[] = {
L
Linus Torvalds 已提交
256
	/* port-D for line out (rear panel) */
257
	{ 0x0b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
L
Linus Torvalds 已提交
258
	/* port-E for HP out (front panel) */
259
	{ 0x0f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
L
Linus Torvalds 已提交
260 261 262
	/* route front PCM to HP */
	{ 0x0f, AC_VERB_SET_CONNECT_SEL, 0x00 },
	/* port-A for surround (rear panel) */
263
	{ 0x0e, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
L
Linus Torvalds 已提交
264
	/* port-G for CLFE (rear panel) */
265
	{ 0x1f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
266
	{ 0x1f, AC_VERB_SET_CONNECT_SEL, 0x02 },
L
Linus Torvalds 已提交
267
	/* port-H for side (rear panel) */
268
	{ 0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
269
	{ 0x20, AC_VERB_SET_CONNECT_SEL, 0x01 },
L
Linus Torvalds 已提交
270
	/* port-C for line-in (rear panel) */
271
	{ 0x0c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
L
Linus Torvalds 已提交
272
	/* port-B for mic-in (rear panel) with vref */
273
	{ 0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
L
Linus Torvalds 已提交
274
	/* port-F for mic-in (front panel) with vref */
275
	{ 0x10, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
L
Linus Torvalds 已提交
276
	/* CD-in */
277
	{ 0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
L
Linus Torvalds 已提交
278 279 280 281 282 283
	/* route front mic to ADC1/2 */
	{ 0x08, AC_VERB_SET_CONNECT_SEL, 0x05 },
	{ 0x09, AC_VERB_SET_CONNECT_SEL, 0x05 },
	{} /* terminator */
};

284
static const struct hda_verb cmi9880_allout_init[] = {
L
Linus Torvalds 已提交
285
	/* port-D for line out (rear panel) */
286
	{ 0x0b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
L
Linus Torvalds 已提交
287
	/* port-E for HP out (front panel) */
288
	{ 0x0f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
L
Linus Torvalds 已提交
289 290 291
	/* route front PCM to HP */
	{ 0x0f, AC_VERB_SET_CONNECT_SEL, 0x00 },
	/* port-A for side (rear panel) */
292
	{ 0x0e, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
L
Linus Torvalds 已提交
293
	/* port-G for CLFE (rear panel) */
294
	{ 0x1f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
295 296
	{ 0x1f, AC_VERB_SET_CONNECT_SEL, 0x02 },
	/* port-H for side (rear panel) */
297
	{ 0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
298
	{ 0x20, AC_VERB_SET_CONNECT_SEL, 0x01 },
L
Linus Torvalds 已提交
299
	/* port-C for surround (rear panel) */
300
	{ 0x0c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
L
Linus Torvalds 已提交
301
	/* port-B for mic-in (rear panel) with vref */
302
	{ 0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
L
Linus Torvalds 已提交
303
	/* port-F for mic-in (front panel) with vref */
304
	{ 0x10, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
L
Linus Torvalds 已提交
305
	/* CD-in */
306
	{ 0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
L
Linus Torvalds 已提交
307 308 309 310 311 312 313 314 315 316 317
	/* route front mic to ADC1/2 */
	{ 0x08, AC_VERB_SET_CONNECT_SEL, 0x05 },
	{ 0x09, AC_VERB_SET_CONNECT_SEL, 0x05 },
	{} /* terminator */
};

/*
 */
static int cmi9880_build_controls(struct hda_codec *codec)
{
	struct cmi_spec *spec = codec->spec;
318 319
	struct snd_kcontrol *kctl;
	int i, err;
L
Linus Torvalds 已提交
320 321 322 323

	err = snd_hda_add_new_ctls(codec, cmi9880_basic_mixer);
	if (err < 0)
		return err;
324
	if (spec->channel_modes) {
L
Linus Torvalds 已提交
325 326 327 328 329
		err = snd_hda_add_new_ctls(codec, cmi9880_ch_mode_mixer);
		if (err < 0)
			return err;
	}
	if (spec->multiout.dig_out_nid) {
330 331 332
		err = snd_hda_create_spdif_out_ctls(codec,
						    spec->multiout.dig_out_nid,
						    spec->multiout.dig_out_nid);
L
Linus Torvalds 已提交
333 334
		if (err < 0)
			return err;
335 336 337 338 339
		err = snd_hda_create_spdif_share_sw(codec,
						    &spec->multiout);
		if (err < 0)
			return err;
		spec->multiout.share_spdif = 1;
L
Linus Torvalds 已提交
340 341 342 343 344 345
	}
	if (spec->dig_in_nid) {
		err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
		if (err < 0)
			return err;
	}
346 347 348 349

	/* assign Capture Source enums to NID */
	kctl = snd_hda_find_mixer_ctl(codec, "Capture Source");
	for (i = 0; kctl && i < kctl->count; i++) {
350
		err = snd_hda_add_nid(codec, kctl, i, spec->adc_nids[i]);
351 352 353
		if (err < 0)
			return err;
	}
L
Linus Torvalds 已提交
354 355 356
	return 0;
}

357 358
/* fill in the multi_dac_nids table, which will decide
   which audio widget to use for each channel */
359
static int cmi9880_fill_multi_dac_nids(struct hda_codec *codec, const struct auto_pin_cfg *cfg)
360 361 362 363 364 365 366 367 368 369
{
	struct cmi_spec *spec = codec->spec;
	hda_nid_t nid;
	int assigned[4];
	int i, j;

	/* clear the table, only one c-media dac assumed here */
	memset(spec->dac_nids, 0, sizeof(spec->dac_nids));
	memset(assigned, 0, sizeof(assigned));
	/* check the pins we found */
370 371
	for (i = 0; i < cfg->line_outs; i++) {
		nid = cfg->line_out_pins[i];
372
		/* nid 0x0b~0x0e is hardwired to audio widget 0x3~0x6 */
373 374
		if (nid >= 0x0b && nid <= 0x0e) {
			spec->dac_nids[i] = (nid - 0x0b) + 0x03;
375 376 377 378
			assigned[nid - 0x0b] = 1;
		}
	}
	/* left pin can be connect to any audio widget */
379 380 381 382 383 384 385
	for (i = 0; i < cfg->line_outs; i++) {
		nid = cfg->line_out_pins[i];
		if (nid <= 0x0e)
			continue;
		/* search for an empty channel */
		for (j = 0; j < cfg->line_outs; j++) {
			if (! assigned[j]) {
386
				spec->dac_nids[i] = j + 0x03;
387 388 389
				assigned[j] = 1;
				break;
			}
390 391
		}
	}
392
	spec->num_dacs = cfg->line_outs;
393 394 395 396
	return 0;
}

/* create multi_init table, which is used for multichannel initialization */
397
static int cmi9880_fill_multi_init(struct hda_codec *codec, const struct auto_pin_cfg *cfg)
398 399 400 401 402 403 404
{
	struct cmi_spec *spec = codec->spec;
	hda_nid_t nid;
	int i, j, k, len;

	/* clear the table, only one c-media dac assumed here */
	memset(spec->multi_init, 0, sizeof(spec->multi_init));
405
	for (j = 0, i = 0; i < cfg->line_outs; i++) {
406
		hda_nid_t conn[4];
407
		nid = cfg->line_out_pins[i];
408 409 410
		/* set as output */
		spec->multi_init[j].nid = nid;
		spec->multi_init[j].verb = AC_VERB_SET_PIN_WIDGET_CONTROL;
411
		spec->multi_init[j].param = PIN_OUT;
412
		j++;
413
		if (nid > 0x0e) {
414 415 416
			/* set connection */
			spec->multi_init[j].nid = nid;
			spec->multi_init[j].verb = AC_VERB_SET_CONNECT_SEL;
417
			spec->multi_init[j].param = 0;
418 419 420
			/* find the index in connect list */
			len = snd_hda_get_connections(codec, nid, conn, 4);
			for (k = 0; k < len; k++)
421
				if (conn[k] == spec->dac_nids[i]) {
422
					spec->multi_init[j].param = k;
423
					break;
424
				}
425 426 427 428 429 430
			j++;
		}
	}
	return 0;
}

L
Linus Torvalds 已提交
431 432 433 434 435 436 437
static int cmi9880_init(struct hda_codec *codec)
{
	struct cmi_spec *spec = codec->spec;
	if (spec->board_config == CMI_ALLOUT)
		snd_hda_sequence_write(codec, cmi9880_allout_init);
	else
		snd_hda_sequence_write(codec, cmi9880_basic_init);
438 439
	if (spec->board_config == CMI_AUTO)
		snd_hda_sequence_write(codec, spec->multi_init);
L
Linus Torvalds 已提交
440 441 442 443 444 445 446 447
	return 0;
}

/*
 * Analog playback callbacks
 */
static int cmi9880_playback_pcm_open(struct hda_pcm_stream *hinfo,
				     struct hda_codec *codec,
448
				     struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
449 450
{
	struct cmi_spec *spec = codec->spec;
451 452
	return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
					     hinfo);
L
Linus Torvalds 已提交
453 454 455 456 457 458
}

static int cmi9880_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
					struct hda_codec *codec,
					unsigned int stream_tag,
					unsigned int format,
459
					struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
460 461 462 463 464 465 466 467
{
	struct cmi_spec *spec = codec->spec;
	return snd_hda_multi_out_analog_prepare(codec, &spec->multiout, stream_tag,
						format, substream);
}

static int cmi9880_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
				       struct hda_codec *codec,
468
				       struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
469 470 471 472 473 474 475 476 477 478
{
	struct cmi_spec *spec = codec->spec;
	return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
}

/*
 * Digital out
 */
static int cmi9880_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
					 struct hda_codec *codec,
479
					 struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
480 481 482 483 484 485 486
{
	struct cmi_spec *spec = codec->spec;
	return snd_hda_multi_out_dig_open(codec, &spec->multiout);
}

static int cmi9880_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
					  struct hda_codec *codec,
487
					  struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
488 489 490 491 492
{
	struct cmi_spec *spec = codec->spec;
	return snd_hda_multi_out_dig_close(codec, &spec->multiout);
}

493 494 495 496 497 498 499 500 501 502 503
static int cmi9880_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
					    struct hda_codec *codec,
					    unsigned int stream_tag,
					    unsigned int format,
					    struct snd_pcm_substream *substream)
{
	struct cmi_spec *spec = codec->spec;
	return snd_hda_multi_out_dig_prepare(codec, &spec->multiout, stream_tag,
					     format, substream);
}

L
Linus Torvalds 已提交
504 505 506 507 508 509 510
/*
 * Analog capture
 */
static int cmi9880_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
				      struct hda_codec *codec,
				      unsigned int stream_tag,
				      unsigned int format,
511
				      struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
512 513 514 515 516 517 518 519 520 521
{
	struct cmi_spec *spec = codec->spec;

	snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
				   stream_tag, 0, format);
	return 0;
}

static int cmi9880_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
				      struct hda_codec *codec,
522
				      struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
523 524 525
{
	struct cmi_spec *spec = codec->spec;

526
	snd_hda_codec_cleanup_stream(codec, spec->adc_nids[substream->number]);
L
Linus Torvalds 已提交
527 528 529 530 531 532
	return 0;
}


/*
 */
533
static const struct hda_pcm_stream cmi9880_pcm_analog_playback = {
L
Linus Torvalds 已提交
534 535 536 537 538 539 540 541 542 543 544
	.substreams = 1,
	.channels_min = 2,
	.channels_max = 8,
	.nid = 0x03, /* NID to query formats and rates */
	.ops = {
		.open = cmi9880_playback_pcm_open,
		.prepare = cmi9880_playback_pcm_prepare,
		.cleanup = cmi9880_playback_pcm_cleanup
	},
};

545
static const struct hda_pcm_stream cmi9880_pcm_analog_capture = {
L
Linus Torvalds 已提交
546 547 548 549 550 551 552 553 554 555
	.substreams = 2,
	.channels_min = 2,
	.channels_max = 2,
	.nid = 0x08, /* NID to query formats and rates */
	.ops = {
		.prepare = cmi9880_capture_pcm_prepare,
		.cleanup = cmi9880_capture_pcm_cleanup
	},
};

556
static const struct hda_pcm_stream cmi9880_pcm_digital_playback = {
L
Linus Torvalds 已提交
557 558 559 560 561 562
	.substreams = 1,
	.channels_min = 2,
	.channels_max = 2,
	/* NID is set in cmi9880_build_pcms */
	.ops = {
		.open = cmi9880_dig_playback_pcm_open,
563 564
		.close = cmi9880_dig_playback_pcm_close,
		.prepare = cmi9880_dig_playback_pcm_prepare
L
Linus Torvalds 已提交
565 566 567
	},
};

568
static const struct hda_pcm_stream cmi9880_pcm_digital_capture = {
L
Linus Torvalds 已提交
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
	.substreams = 1,
	.channels_min = 2,
	.channels_max = 2,
	/* NID is set in cmi9880_build_pcms */
};

static int cmi9880_build_pcms(struct hda_codec *codec)
{
	struct cmi_spec *spec = codec->spec;
	struct hda_pcm *info = spec->pcm_rec;

	codec->num_pcms = 1;
	codec->pcm_info = info;

	info->name = "CMI9880";
	info->stream[SNDRV_PCM_STREAM_PLAYBACK] = cmi9880_pcm_analog_playback;
	info->stream[SNDRV_PCM_STREAM_CAPTURE] = cmi9880_pcm_analog_capture;

	if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
		codec->num_pcms++;
		info++;
		info->name = "CMI9880 Digital";
591
		info->pcm_type = HDA_PCM_TYPE_SPDIF;
L
Linus Torvalds 已提交
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
		if (spec->multiout.dig_out_nid) {
			info->stream[SNDRV_PCM_STREAM_PLAYBACK] = cmi9880_pcm_digital_playback;
			info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
		}
		if (spec->dig_in_nid) {
			info->stream[SNDRV_PCM_STREAM_CAPTURE] = cmi9880_pcm_digital_capture;
			info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
		}
	}

	return 0;
}

static void cmi9880_free(struct hda_codec *codec)
{
	kfree(codec->spec);
}

/*
 */

613
static const char * const cmi9880_models[CMI_MODELS] = {
614 615 616 617 618 619 620 621
	[CMI_MINIMAL]	= "minimal",
	[CMI_MIN_FP]	= "min_fp",
	[CMI_FULL]	= "full",
	[CMI_FULL_DIG]	= "full_dig",
	[CMI_ALLOUT]	= "allout",
	[CMI_AUTO]	= "auto",
};

622
static const struct snd_pci_quirk cmi9880_cfg_tbl[] = {
623
	SND_PCI_QUIRK(0x1043, 0x813d, "ASUS P5AD2", CMI_FULL_DIG),
624
	SND_PCI_QUIRK(0x1854, 0x002b, "LG LS75", CMI_MINIMAL),
625
	SND_PCI_QUIRK(0x1854, 0x0032, "LG", CMI_FULL_DIG),
L
Linus Torvalds 已提交
626 627 628
	{} /* terminator */
};

629
static const struct hda_codec_ops cmi9880_patch_ops = {
L
Linus Torvalds 已提交
630 631 632 633 634 635 636 637 638 639
	.build_controls = cmi9880_build_controls,
	.build_pcms = cmi9880_build_pcms,
	.init = cmi9880_init,
	.free = cmi9880_free,
};

static int patch_cmi9880(struct hda_codec *codec)
{
	struct cmi_spec *spec;

640
	spec = kzalloc(sizeof(*spec), GFP_KERNEL);
L
Linus Torvalds 已提交
641 642 643 644
	if (spec == NULL)
		return -ENOMEM;

	codec->spec = spec;
645 646 647
	spec->board_config = snd_hda_check_board_config(codec, CMI_MODELS,
							cmi9880_models,
							cmi9880_cfg_tbl);
L
Linus Torvalds 已提交
648
	if (spec->board_config < 0) {
649 650
		snd_printdd(KERN_INFO "hda_codec: %s: BIOS auto-probing.\n",
			    codec->chip_name);
651
		spec->board_config = CMI_AUTO; /* try everything */
L
Linus Torvalds 已提交
652 653
	}

654 655
	/* copy default DAC NIDs */
	memcpy(spec->dac_nids, cmi9880_dac_nids, sizeof(spec->dac_nids));
656
	spec->num_dacs = 4;
657

L
Linus Torvalds 已提交
658 659 660
	switch (spec->board_config) {
	case CMI_MINIMAL:
	case CMI_MIN_FP:
661
		spec->channel_modes = cmi9880_channel_modes;
L
Linus Torvalds 已提交
662
		if (spec->board_config == CMI_MINIMAL)
663
			spec->num_channel_modes = 2;
L
Linus Torvalds 已提交
664 665
		else {
			spec->front_panel = 1;
666
			spec->num_channel_modes = 3;
L
Linus Torvalds 已提交
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
		}
		spec->multiout.max_channels = cmi9880_channel_modes[0].channels;
		spec->input_mux = &cmi9880_basic_mux;
		break;
	case CMI_FULL:
	case CMI_FULL_DIG:
		spec->front_panel = 1;
		spec->multiout.max_channels = 8;
		spec->input_mux = &cmi9880_basic_mux;
		if (spec->board_config == CMI_FULL_DIG) {
			spec->multiout.dig_out_nid = CMI_DIG_OUT_NID;
			spec->dig_in_nid = CMI_DIG_IN_NID;
		}
		break;
	case CMI_ALLOUT:
		spec->front_panel = 1;
		spec->multiout.max_channels = 8;
		spec->no_line_in = 1;
		spec->input_mux = &cmi9880_no_line_mux;
		spec->multiout.dig_out_nid = CMI_DIG_OUT_NID;
		break;
688 689 690 691
	case CMI_AUTO:
		{
		unsigned int port_e, port_f, port_g, port_h;
		unsigned int port_spdifi, port_spdifo;
692 693
		struct auto_pin_cfg cfg;

694
		/* collect pin default configuration */
695 696
		port_e = snd_hda_codec_get_pincfg(codec, 0x0f);
		port_f = snd_hda_codec_get_pincfg(codec, 0x10);
697
		spec->front_panel = 1;
698 699
		if (get_defcfg_connect(port_e) == AC_JACK_PORT_NONE ||
		    get_defcfg_connect(port_f) == AC_JACK_PORT_NONE) {
700 701
			port_g = snd_hda_codec_get_pincfg(codec, 0x1f);
			port_h = snd_hda_codec_get_pincfg(codec, 0x20);
702
			spec->channel_modes = cmi9880_channel_modes;
703
			/* no front panel */
704 705
			if (get_defcfg_connect(port_g) == AC_JACK_PORT_NONE ||
			    get_defcfg_connect(port_h) == AC_JACK_PORT_NONE) {
706 707 708
				/* no optional rear panel */
				spec->board_config = CMI_MINIMAL;
				spec->front_panel = 0;
709
				spec->num_channel_modes = 2;
710
			} else {
711
				spec->board_config = CMI_MIN_FP;
712
				spec->num_channel_modes = 3;
713
			}
714
			spec->input_mux = &cmi9880_basic_mux;
715
			spec->multiout.max_channels = cmi9880_channel_modes[0].channels;
716 717
		} else {
			spec->input_mux = &cmi9880_basic_mux;
718 719
			port_spdifi = snd_hda_codec_get_pincfg(codec, 0x13);
			port_spdifo = snd_hda_codec_get_pincfg(codec, 0x12);
720
			if (get_defcfg_connect(port_spdifo) != AC_JACK_PORT_NONE)
721
				spec->multiout.dig_out_nid = CMI_DIG_OUT_NID;
722
			if (get_defcfg_connect(port_spdifi) != AC_JACK_PORT_NONE)
723
				spec->dig_in_nid = CMI_DIG_IN_NID;
724
			spec->multiout.max_channels = 8;
725
		}
726
		snd_hda_parse_pin_def_config(codec, &cfg, NULL);
727 728 729 730
		if (cfg.line_outs) {
			spec->multiout.max_channels = cfg.line_outs * 2;
			cmi9880_fill_multi_dac_nids(codec, &cfg);
			cmi9880_fill_multi_init(codec, &cfg);
731 732
		} else
			snd_printd("patch_cmedia: cannot detect association in defcfg\n");
733
		break;
734
		}
L
Linus Torvalds 已提交
735 736
	}

737
	spec->multiout.num_dacs = spec->num_dacs;
738
	spec->multiout.dac_nids = spec->dac_nids;
L
Linus Torvalds 已提交
739 740 741 742 743 744 745 746 747 748 749

	spec->adc_nids = cmi9880_adc_nids;

	codec->patch_ops = cmi9880_patch_ops;

	return 0;
}

/*
 * patch entries
 */
750
static const struct hda_codec_preset snd_hda_preset_cmedia[] = {
L
Linus Torvalds 已提交
751 752 753 754
	{ .id = 0x13f69880, .name = "CMI9880", .patch = patch_cmi9880 },
 	{ .id = 0x434d4980, .name = "CMI9880", .patch = patch_cmi9880 },
	{} /* terminator */
};
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778

MODULE_ALIAS("snd-hda-codec-id:13f69880");
MODULE_ALIAS("snd-hda-codec-id:434d4980");

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("C-Media HD-audio codec");

static struct hda_codec_preset_list cmedia_list = {
	.preset = snd_hda_preset_cmedia,
	.owner = THIS_MODULE,
};

static int __init patch_cmedia_init(void)
{
	return snd_hda_add_codec_preset(&cmedia_list);
}

static void __exit patch_cmedia_exit(void)
{
	snd_hda_delete_codec_preset(&cmedia_list);
}

module_init(patch_cmedia_init)
module_exit(patch_cmedia_exit)