hda_generic.c 135 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*
 * Universal Interface for Intel High Definition Audio Codec
 *
 * Generic widget tree parser
 *
 * 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/slab.h>
25
#include <linux/export.h>
26
#include <linux/sort.h>
27
#include <linux/delay.h>
28
29
#include <linux/ctype.h>
#include <linux/string.h>
30
#include <linux/bitops.h>
Linus Torvalds's avatar
Linus Torvalds committed
31
#include <sound/core.h>
32
#include <sound/jack.h>
Linus Torvalds's avatar
Linus Torvalds committed
33
34
#include "hda_codec.h"
#include "hda_local.h"
35
36
37
#include "hda_auto_parser.h"
#include "hda_jack.h"
#include "hda_generic.h"
Linus Torvalds's avatar
Linus Torvalds committed
38

39

40
41
42
43
44
/* initialize hda_gen_spec struct */
int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
{
	snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
	snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
45
	snd_array_init(&spec->loopback_list, sizeof(struct hda_amp_list), 8);
46
	mutex_init(&spec->pcm_mutex);
47
48
49
	return 0;
}
EXPORT_SYMBOL_HDA(snd_hda_gen_spec_init);
Linus Torvalds's avatar
Linus Torvalds committed
50

51
52
53
struct snd_kcontrol_new *
snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
		     const struct snd_kcontrol_new *temp)
54
55
56
57
58
59
60
61
62
63
64
65
66
{
	struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
	if (!knew)
		return NULL;
	*knew = *temp;
	if (name)
		knew->name = kstrdup(name, GFP_KERNEL);
	else if (knew->name)
		knew->name = kstrdup(knew->name, GFP_KERNEL);
	if (!knew->name)
		return NULL;
	return knew;
}
67
EXPORT_SYMBOL_HDA(snd_hda_gen_add_kctl);
Linus Torvalds's avatar
Linus Torvalds committed
68

69
70
71
72
73
74
75
76
77
78
static void free_kctls(struct hda_gen_spec *spec)
{
	if (spec->kctls.list) {
		struct snd_kcontrol_new *kctl = spec->kctls.list;
		int i;
		for (i = 0; i < spec->kctls.used; i++)
			kfree(kctl[i].name);
	}
	snd_array_free(&spec->kctls);
}
Linus Torvalds's avatar
Linus Torvalds committed
79

80
81
82
83
84
85
void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
{
	if (!spec)
		return;
	free_kctls(spec);
	snd_array_free(&spec->paths);
86
	snd_array_free(&spec->loopback_list);
87
88
}
EXPORT_SYMBOL_HDA(snd_hda_gen_spec_free);
Linus Torvalds's avatar
Linus Torvalds committed
89

90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
 * store user hints
 */
static void parse_user_hints(struct hda_codec *codec)
{
	struct hda_gen_spec *spec = codec->spec;
	int val;

	val = snd_hda_get_bool_hint(codec, "jack_detect");
	if (val >= 0)
		codec->no_jack_detect = !val;
	val = snd_hda_get_bool_hint(codec, "inv_jack_detect");
	if (val >= 0)
		codec->inv_jack_detect = !!val;
	val = snd_hda_get_bool_hint(codec, "trigger_sense");
	if (val >= 0)
		codec->no_trigger_sense = !val;
	val = snd_hda_get_bool_hint(codec, "inv_eapd");
	if (val >= 0)
		codec->inv_eapd = !!val;
	val = snd_hda_get_bool_hint(codec, "pcm_format_first");
	if (val >= 0)
		codec->pcm_format_first = !!val;
	val = snd_hda_get_bool_hint(codec, "sticky_stream");
	if (val >= 0)
		codec->no_sticky_stream = !val;
	val = snd_hda_get_bool_hint(codec, "spdif_status_reset");
	if (val >= 0)
		codec->spdif_status_reset = !!val;
	val = snd_hda_get_bool_hint(codec, "pin_amp_workaround");
	if (val >= 0)
		codec->pin_amp_workaround = !!val;
	val = snd_hda_get_bool_hint(codec, "single_adc_amp");
	if (val >= 0)
		codec->single_adc_amp = !!val;

126
127
128
	val = snd_hda_get_bool_hint(codec, "auto_mute");
	if (val >= 0)
		spec->suppress_auto_mute = !val;
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
	val = snd_hda_get_bool_hint(codec, "auto_mic");
	if (val >= 0)
		spec->suppress_auto_mic = !val;
	val = snd_hda_get_bool_hint(codec, "line_in_auto_switch");
	if (val >= 0)
		spec->line_in_auto_switch = !!val;
	val = snd_hda_get_bool_hint(codec, "need_dac_fix");
	if (val >= 0)
		spec->need_dac_fix = !!val;
	val = snd_hda_get_bool_hint(codec, "primary_hp");
	if (val >= 0)
		spec->no_primary_hp = !val;
	val = snd_hda_get_bool_hint(codec, "multi_cap_vol");
	if (val >= 0)
		spec->multi_cap_vol = !!val;
	val = snd_hda_get_bool_hint(codec, "inv_dmic_split");
	if (val >= 0)
		spec->inv_dmic_split = !!val;
	val = snd_hda_get_bool_hint(codec, "indep_hp");
	if (val >= 0)
		spec->indep_hp = !!val;
	val = snd_hda_get_bool_hint(codec, "add_stereo_mix_input");
	if (val >= 0)
		spec->add_stereo_mix_input = !!val;
	val = snd_hda_get_bool_hint(codec, "add_out_jack_modes");
	if (val >= 0)
		spec->add_out_jack_modes = !!val;
156
157
158
	val = snd_hda_get_bool_hint(codec, "add_in_jack_modes");
	if (val >= 0)
		spec->add_in_jack_modes = !!val;
159
160
161
	val = snd_hda_get_bool_hint(codec, "power_down_unused");
	if (val >= 0)
		spec->power_down_unused = !!val;
162
163
164
165
166
167
	val = snd_hda_get_bool_hint(codec, "add_hp_mic");
	if (val >= 0)
		spec->hp_mic = !!val;
	val = snd_hda_get_bool_hint(codec, "hp_mic_detect");
	if (val >= 0)
		spec->suppress_hp_mic_detect = !val;
168
169
170
171
172

	if (!snd_hda_get_int_hint(codec, "mixer_nid", &val))
		spec->mixer_nid = val;
}

173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
 * pin control value accesses
 */

#define update_pin_ctl(codec, pin, val) \
	snd_hda_codec_update_cache(codec, pin, 0, \
				   AC_VERB_SET_PIN_WIDGET_CONTROL, val)

/* restore the pinctl based on the cached value */
static inline void restore_pin_ctl(struct hda_codec *codec, hda_nid_t pin)
{
	update_pin_ctl(codec, pin, snd_hda_codec_get_pin_target(codec, pin));
}

/* set the pinctl target value and write it if requested */
static void set_pin_target(struct hda_codec *codec, hda_nid_t pin,
			   unsigned int val, bool do_write)
{
	if (!pin)
		return;
	val = snd_hda_correct_pin_ctl(codec, pin, val);
	snd_hda_codec_set_pin_target(codec, pin, val);
	if (do_write)
		update_pin_ctl(codec, pin, val);
}

/* set pinctl target values for all given pins */
static void set_pin_targets(struct hda_codec *codec, int num_pins,
			    hda_nid_t *pins, unsigned int val)
{
	int i;
	for (i = 0; i < num_pins; i++)
		set_pin_target(codec, pins[i], val, false);
}

Linus Torvalds's avatar
Linus Torvalds committed
208
/*
209
 * parsing paths
Linus Torvalds's avatar
Linus Torvalds committed
210
211
 */

212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/* return the position of NID in the list, or -1 if not found */
static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
{
	int i;
	for (i = 0; i < nums; i++)
		if (list[i] == nid)
			return i;
	return -1;
}

/* return true if the given NID is contained in the path */
static bool is_nid_contained(struct nid_path *path, hda_nid_t nid)
{
	return find_idx_in_nid_list(nid, path->path, path->depth) >= 0;
}

228
229
static struct nid_path *get_nid_path(struct hda_codec *codec,
				     hda_nid_t from_nid, hda_nid_t to_nid,
230
				     int anchor_nid)
Linus Torvalds's avatar
Linus Torvalds committed
231
{
232
233
	struct hda_gen_spec *spec = codec->spec;
	int i;
Linus Torvalds's avatar
Linus Torvalds committed
234

235
236
237
238
239
	for (i = 0; i < spec->paths.used; i++) {
		struct nid_path *path = snd_array_elem(&spec->paths, i);
		if (path->depth <= 0)
			continue;
		if ((!from_nid || path->path[0] == from_nid) &&
240
		    (!to_nid || path->path[path->depth - 1] == to_nid)) {
241
242
243
			if (!anchor_nid ||
			    (anchor_nid > 0 && is_nid_contained(path, anchor_nid)) ||
			    (anchor_nid < 0 && !is_nid_contained(path, anchor_nid)))
244
245
				return path;
		}
Linus Torvalds's avatar
Linus Torvalds committed
246
	}
247
	return NULL;
Linus Torvalds's avatar
Linus Torvalds committed
248
}
249
250
251
252
253
254
255

/* get the path between the given NIDs;
 * passing 0 to either @pin or @dac behaves as a wildcard
 */
struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
				      hda_nid_t from_nid, hda_nid_t to_nid)
{
256
	return get_nid_path(codec, from_nid, to_nid, 0);
257
}
258
EXPORT_SYMBOL_HDA(snd_hda_get_nid_path);
Linus Torvalds's avatar
Linus Torvalds committed
259

260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/* get the index number corresponding to the path instance;
 * the index starts from 1, for easier checking the invalid value
 */
int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
{
	struct hda_gen_spec *spec = codec->spec;
	struct nid_path *array = spec->paths.list;
	ssize_t idx;

	if (!spec->paths.used)
		return 0;
	idx = path - array;
	if (idx < 0 || idx >= spec->paths.used)
		return 0;
	return idx + 1;
}
276
EXPORT_SYMBOL_HDA(snd_hda_get_path_idx);
277
278
279
280
281
282
283
284
285
286

/* get the path instance corresponding to the given index number */
struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
{
	struct hda_gen_spec *spec = codec->spec;

	if (idx <= 0 || idx > spec->paths.used)
		return NULL;
	return snd_array_elem(&spec->paths, idx - 1);
}
287
EXPORT_SYMBOL_HDA(snd_hda_get_path_from_idx);
288

289
290
/* check whether the given DAC is already found in any existing paths */
static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds's avatar
Linus Torvalds committed
291
{
292
293
	struct hda_gen_spec *spec = codec->spec;
	int i;
Linus Torvalds's avatar
Linus Torvalds committed
294

295
296
297
298
	for (i = 0; i < spec->paths.used; i++) {
		struct nid_path *path = snd_array_elem(&spec->paths, i);
		if (path->path[0] == nid)
			return true;
299
	}
300
301
	return false;
}
Linus Torvalds's avatar
Linus Torvalds committed
302

303
304
305
306
307
308
309
310
/* check whether the given two widgets can be connected */
static bool is_reachable_path(struct hda_codec *codec,
			      hda_nid_t from_nid, hda_nid_t to_nid)
{
	if (!from_nid || !to_nid)
		return false;
	return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
}
Linus Torvalds's avatar
Linus Torvalds committed
311

312
313
314
315
316
317
318
319
320
321
322
323
324
325
/* nid, dir and idx */
#define AMP_VAL_COMPARE_MASK	(0xffff | (1U << 18) | (0x0f << 19))

/* check whether the given ctl is already assigned in any path elements */
static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
{
	struct hda_gen_spec *spec = codec->spec;
	int i;

	val &= AMP_VAL_COMPARE_MASK;
	for (i = 0; i < spec->paths.used; i++) {
		struct nid_path *path = snd_array_elem(&spec->paths, i);
		if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
			return true;
Linus Torvalds's avatar
Linus Torvalds committed
326
	}
327
	return false;
Linus Torvalds's avatar
Linus Torvalds committed
328
329
}

330
331
/* check whether a control with the given (nid, dir, idx) was assigned */
static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
332
			      int dir, int idx, int type)
Linus Torvalds's avatar
Linus Torvalds committed
333
{
334
	unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
335
	return is_ctl_used(codec, val, type);
336
}
Linus Torvalds's avatar
Linus Torvalds committed
337

338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
static void print_nid_path(const char *pfx, struct nid_path *path)
{
	char buf[40];
	int i;


	buf[0] = 0;
	for (i = 0; i < path->depth; i++) {
		char tmp[4];
		sprintf(tmp, ":%02x", path->path[i]);
		strlcat(buf, tmp, sizeof(buf));
	}
	snd_printdd("%s path: depth=%d %s\n", pfx, path->depth, buf);
}

353
354
355
/* called recursively */
static bool __parse_nid_path(struct hda_codec *codec,
			     hda_nid_t from_nid, hda_nid_t to_nid,
356
357
			     int anchor_nid, struct nid_path *path,
			     int depth)
358
{
359
	const hda_nid_t *conn;
360
361
	int i, nums;

362
363
364
365
	if (to_nid == anchor_nid)
		anchor_nid = 0; /* anchor passed */
	else if (to_nid == (hda_nid_t)(-anchor_nid))
		return false; /* hit the exclusive nid */
Linus Torvalds's avatar
Linus Torvalds committed
366

367
	nums = snd_hda_get_conn_list(codec, to_nid, &conn);
368
369
370
371
372
373
374
375
376
377
	for (i = 0; i < nums; i++) {
		if (conn[i] != from_nid) {
			/* special case: when from_nid is 0,
			 * try to find an empty DAC
			 */
			if (from_nid ||
			    get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
			    is_dac_already_used(codec, conn[i]))
				continue;
		}
378
379
		/* anchor is not requested or already passed? */
		if (anchor_nid <= 0)
380
			goto found;
Linus Torvalds's avatar
Linus Torvalds committed
381
	}
382
383
384
385
386
387
388
389
390
	if (depth >= MAX_NID_PATH_DEPTH)
		return false;
	for (i = 0; i < nums; i++) {
		unsigned int type;
		type = get_wcaps_type(get_wcaps(codec, conn[i]));
		if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
		    type == AC_WID_PIN)
			continue;
		if (__parse_nid_path(codec, from_nid, conn[i],
391
				     anchor_nid, path, depth + 1))
392
393
394
395
396
397
398
399
400
401
402
			goto found;
	}
	return false;

 found:
	path->path[path->depth] = conn[i];
	path->idx[path->depth + 1] = i;
	if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
		path->multi[path->depth + 1] = 1;
	path->depth++;
	return true;
Linus Torvalds's avatar
Linus Torvalds committed
403
404
}

405
406
/* parse the widget path from the given nid to the target nid;
 * when @from_nid is 0, try to find an empty DAC;
407
408
409
410
411
 * when @anchor_nid is set to a positive value, only paths through the widget
 * with the given value are evaluated.
 * when @anchor_nid is set to a negative value, paths through the widget
 * with the negative of given value are excluded, only other paths are chosen.
 * when @anchor_nid is zero, no special handling about path selection.
Linus Torvalds's avatar
Linus Torvalds committed
412
 */
413
bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
414
			    hda_nid_t to_nid, int anchor_nid,
415
			    struct nid_path *path)
Linus Torvalds's avatar
Linus Torvalds committed
416
{
417
	if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) {
418
419
420
		path->path[path->depth] = to_nid;
		path->depth++;
		return true;
Linus Torvalds's avatar
Linus Torvalds committed
421
	}
422
	return false;
Linus Torvalds's avatar
Linus Torvalds committed
423
}
424
EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path);
Linus Torvalds's avatar
Linus Torvalds committed
425
426

/*
427
428
 * parse the path between the given NIDs and add to the path list.
 * if no valid path is found, return NULL
Linus Torvalds's avatar
Linus Torvalds committed
429
 */
430
431
struct nid_path *
snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
432
		     hda_nid_t to_nid, int anchor_nid)
433
434
435
436
437
438
439
{
	struct hda_gen_spec *spec = codec->spec;
	struct nid_path *path;

	if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
		return NULL;

440
	/* check whether the path has been already added */
441
	path = get_nid_path(codec, from_nid, to_nid, anchor_nid);
442
443
444
	if (path)
		return path;

445
446
447
448
	path = snd_array_new(&spec->paths);
	if (!path)
		return NULL;
	memset(path, 0, sizeof(*path));
449
	if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path))
450
451
452
453
		return path;
	/* push back */
	spec->paths.used--;
	return NULL;
Linus Torvalds's avatar
Linus Torvalds committed
454
}
455
EXPORT_SYMBOL_HDA(snd_hda_add_new_path);
Linus Torvalds's avatar
Linus Torvalds committed
456

457
458
459
460
461
462
463
464
465
/* clear the given path as invalid so that it won't be picked up later */
static void invalidate_nid_path(struct hda_codec *codec, int idx)
{
	struct nid_path *path = snd_hda_get_path_from_idx(codec, idx);
	if (!path)
		return;
	memset(path, 0, sizeof(*path));
}

466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/* look for an empty DAC slot */
static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
			      bool is_digital)
{
	struct hda_gen_spec *spec = codec->spec;
	bool cap_digital;
	int i;

	for (i = 0; i < spec->num_all_dacs; i++) {
		hda_nid_t nid = spec->all_dacs[i];
		if (!nid || is_dac_already_used(codec, nid))
			continue;
		cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
		if (is_digital != cap_digital)
			continue;
		if (is_reachable_path(codec, nid, pin))
			return nid;
	}
484
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
485
486
}

487
488
/* replace the channels in the composed amp value with the given number */
static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
Linus Torvalds's avatar
Linus Torvalds committed
489
{
490
491
492
	val &= ~(0x3U << 16);
	val |= chs << 16;
	return val;
Linus Torvalds's avatar
Linus Torvalds committed
493
494
}

495
496
497
/* check whether the widget has the given amp capability for the direction */
static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
			   int dir, unsigned int bits)
Linus Torvalds's avatar
Linus Torvalds committed
498
{
499
500
501
502
503
504
505
506
	if (!nid)
		return false;
	if (get_wcaps(codec, nid) & (1 << (dir + 1)))
		if (query_amp_caps(codec, nid, dir) & bits)
			return true;
	return false;
}

507
508
509
510
511
512
513
514
515
static bool same_amp_caps(struct hda_codec *codec, hda_nid_t nid1,
			  hda_nid_t nid2, int dir)
{
	if (!(get_wcaps(codec, nid1) & (1 << (dir + 1))))
		return !(get_wcaps(codec, nid2) & (1 << (dir + 1)));
	return (query_amp_caps(codec, nid1, dir) ==
		query_amp_caps(codec, nid2, dir));
}

516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
#define nid_has_mute(codec, nid, dir) \
	check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
#define nid_has_volume(codec, nid, dir) \
	check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)

/* look for a widget suitable for assigning a mute switch in the path */
static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
				       struct nid_path *path)
{
	int i;

	for (i = path->depth - 1; i >= 0; i--) {
		if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
			return path->path[i];
		if (i != path->depth - 1 && i != 0 &&
		    nid_has_mute(codec, path->path[i], HDA_INPUT))
			return path->path[i];
	}
	return 0;
}

/* look for a widget suitable for assigning a volume ctl in the path */
static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
				      struct nid_path *path)
{
	int i;
Linus Torvalds's avatar
Linus Torvalds committed
542

543
544
545
	for (i = path->depth - 1; i >= 0; i--) {
		if (nid_has_volume(codec, path->path[i], HDA_OUTPUT))
			return path->path[i];
Linus Torvalds's avatar
Linus Torvalds committed
546
	}
547
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
548
549
550
}

/*
551
 * path activation / deactivation
Linus Torvalds's avatar
Linus Torvalds committed
552
 */
553
554
555

/* can have the amp-in capability? */
static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds's avatar
Linus Torvalds committed
556
{
557
558
559
560
561
562
563
564
565
566
	hda_nid_t nid = path->path[idx];
	unsigned int caps = get_wcaps(codec, nid);
	unsigned int type = get_wcaps_type(caps);

	if (!(caps & AC_WCAP_IN_AMP))
		return false;
	if (type == AC_WID_PIN && idx > 0) /* only for input pins */
		return false;
	return true;
}
Linus Torvalds's avatar
Linus Torvalds committed
567

568
569
570
571
572
573
574
575
576
577
578
579
580
/* can have the amp-out capability? */
static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
{
	hda_nid_t nid = path->path[idx];
	unsigned int caps = get_wcaps(codec, nid);
	unsigned int type = get_wcaps_type(caps);

	if (!(caps & AC_WCAP_OUT_AMP))
		return false;
	if (type == AC_WID_PIN && !idx) /* only for output pins */
		return false;
	return true;
}
Linus Torvalds's avatar
Linus Torvalds committed
581

582
583
/* check whether the given (nid,dir,idx) is active */
static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
584
			  unsigned int dir, unsigned int idx)
585
586
587
{
	struct hda_gen_spec *spec = codec->spec;
	int i, n;
Linus Torvalds's avatar
Linus Torvalds committed
588

589
590
591
	for (n = 0; n < spec->paths.used; n++) {
		struct nid_path *path = snd_array_elem(&spec->paths, n);
		if (!path->active)
Linus Torvalds's avatar
Linus Torvalds committed
592
			continue;
593
594
595
596
597
		for (i = 0; i < path->depth; i++) {
			if (path->path[i] == nid) {
				if (dir == HDA_OUTPUT || path->idx[i] == idx)
					return true;
				break;
Linus Torvalds's avatar
Linus Torvalds committed
598
599
600
			}
		}
	}
601
	return false;
Linus Torvalds's avatar
Linus Torvalds committed
602
603
}

604
605
/* get the default amp value for the target state */
static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
606
				   int dir, unsigned int caps, bool enable)
Linus Torvalds's avatar
Linus Torvalds committed
607
{
608
609
610
611
612
613
	unsigned int val = 0;

	if (caps & AC_AMPCAP_NUM_STEPS) {
		/* set to 0dB */
		if (enable)
			val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
Linus Torvalds's avatar
Linus Torvalds committed
614
	}
615
616
617
618
619
	if (caps & AC_AMPCAP_MUTE) {
		if (!enable)
			val |= HDA_AMP_MUTE;
	}
	return val;
Linus Torvalds's avatar
Linus Torvalds committed
620
621
}

622
623
624
/* initialize the amp value (only at the first time) */
static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
{
625
626
	unsigned int caps = query_amp_caps(codec, nid, dir);
	int val = get_amp_val_to_activate(codec, nid, dir, caps, false);
627
628
	snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
}
Linus Torvalds's avatar
Linus Torvalds committed
629

630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
/* calculate amp value mask we can modify;
 * if the given amp is controlled by mixers, don't touch it
 */
static unsigned int get_amp_mask_to_modify(struct hda_codec *codec,
					   hda_nid_t nid, int dir, int idx,
					   unsigned int caps)
{
	unsigned int mask = 0xff;

	if (caps & AC_AMPCAP_MUTE) {
		if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_MUTE_CTL))
			mask &= ~0x80;
	}
	if (caps & AC_AMPCAP_NUM_STEPS) {
		if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
		    is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
			mask &= ~0x7f;
	}
	return mask;
}

651
static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
652
			 int idx, int idx_to_check, bool enable)
653
{
654
655
656
	unsigned int caps;
	unsigned int mask, val;

657
	if (!enable && is_active_nid(codec, nid, dir, idx_to_check))
658
659
660
661
662
663
		return;

	caps = query_amp_caps(codec, nid, dir);
	val = get_amp_val_to_activate(codec, nid, dir, caps, enable);
	mask = get_amp_mask_to_modify(codec, nid, dir, idx_to_check, caps);
	if (!mask)
664
		return;
665
666
667

	val &= mask;
	snd_hda_codec_amp_stereo(codec, nid, dir, idx, mask, val);
668
669
670
671
672
673
674
}

static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
			     int i, bool enable)
{
	hda_nid_t nid = path->path[i];
	init_amp(codec, nid, HDA_OUTPUT, 0);
675
	activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable);
676
677
678
679
}

static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
			    int i, bool enable, bool add_aamix)
Linus Torvalds's avatar
Linus Torvalds committed
680
{
681
	struct hda_gen_spec *spec = codec->spec;
682
	const hda_nid_t *conn;
683
684
685
686
	int n, nums, idx;
	int type;
	hda_nid_t nid = path->path[i];

687
	nums = snd_hda_get_conn_list(codec, nid, &conn);
688
689
690
691
692
693
694
695
696
697
698
699
700
	type = get_wcaps_type(get_wcaps(codec, nid));
	if (type == AC_WID_PIN ||
	    (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
		nums = 1;
		idx = 0;
	} else
		idx = path->idx[i];

	for (n = 0; n < nums; n++)
		init_amp(codec, nid, HDA_INPUT, n);

	/* here is a little bit tricky in comparison with activate_amp_out();
	 * when aa-mixer is available, we need to enable the path as well
Linus Torvalds's avatar
Linus Torvalds committed
701
	 */
702
	for (n = 0; n < nums; n++) {
703
		if (n != idx && (!add_aamix || conn[n] != spec->mixer_merge_nid))
704
			continue;
705
		activate_amp(codec, nid, HDA_INPUT, n, idx, enable);
Linus Torvalds's avatar
Linus Torvalds committed
706
	}
707
}
Linus Torvalds's avatar
Linus Torvalds committed
708

709
710
711
712
713
714
/* activate or deactivate the given path
 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
 */
void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
			   bool enable, bool add_aamix)
{
715
	struct hda_gen_spec *spec = codec->spec;
716
717
718
719
720
721
	int i;

	if (!enable)
		path->active = false;

	for (i = path->depth - 1; i >= 0; i--) {
722
723
724
725
726
727
728
729
		hda_nid_t nid = path->path[i];
		if (enable && spec->power_down_unused) {
			/* make sure the widget is powered up */
			if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0))
				snd_hda_codec_write(codec, nid, 0,
						    AC_VERB_SET_POWER_STATE,
						    AC_PWRST_D0);
		}
730
		if (enable && path->multi[i])
731
			snd_hda_codec_write_cache(codec, nid, 0,
732
733
734
735
736
737
					    AC_VERB_SET_CONNECT_SEL,
					    path->idx[i]);
		if (has_amp_in(codec, path, i))
			activate_amp_in(codec, path, i, enable, add_aamix);
		if (has_amp_out(codec, path, i))
			activate_amp_out(codec, path, i, enable);
Linus Torvalds's avatar
Linus Torvalds committed
738
739
	}

740
741
	if (enable)
		path->active = true;
Linus Torvalds's avatar
Linus Torvalds committed
742
}
743
744
EXPORT_SYMBOL_HDA(snd_hda_activate_path);

745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
/* if the given path is inactive, put widgets into D3 (only if suitable) */
static void path_power_down_sync(struct hda_codec *codec, struct nid_path *path)
{
	struct hda_gen_spec *spec = codec->spec;
	bool changed;
	int i;

	if (!spec->power_down_unused || path->active)
		return;

	for (i = 0; i < path->depth; i++) {
		hda_nid_t nid = path->path[i];
		if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D3)) {
			snd_hda_codec_write(codec, nid, 0,
					    AC_VERB_SET_POWER_STATE,
					    AC_PWRST_D3);
			changed = true;
		}
	}

	if (changed) {
		msleep(10);
		snd_hda_codec_read(codec, path->path[0], 0,
				   AC_VERB_GET_POWER_STATE, 0);
	}
}

772
773
774
775
776
777
778
/* turn on/off EAPD on the given pin */
static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
{
	struct hda_gen_spec *spec = codec->spec;
	if (spec->own_eapd_ctl ||
	    !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
		return;
779
780
	if (codec->inv_eapd)
		enable = !enable;
781
782
783
784
785
	snd_hda_codec_update_cache(codec, pin, 0,
				   AC_VERB_SET_EAPD_BTLENABLE,
				   enable ? 0x02 : 0x00);
}

786
787
788
789
790
791
792
793
/* re-initialize the path specified by the given path index */
static void resume_path_from_idx(struct hda_codec *codec, int path_idx)
{
	struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
	if (path)
		snd_hda_activate_path(codec, path, path->active, false);
}

Linus Torvalds's avatar
Linus Torvalds committed
794
795

/*
796
 * Helper functions for creating mixer ctl elements
Linus Torvalds's avatar
Linus Torvalds committed
797
798
 */

799
800
801
802
803
804
805
806
807
808
enum {
	HDA_CTL_WIDGET_VOL,
	HDA_CTL_WIDGET_MUTE,
	HDA_CTL_BIND_MUTE,
};
static const struct snd_kcontrol_new control_templates[] = {
	HDA_CODEC_VOLUME(NULL, 0, 0, 0),
	HDA_CODEC_MUTE(NULL, 0, 0, 0),
	HDA_BIND_MUTE(NULL, 0, 0, 0),
};
Linus Torvalds's avatar
Linus Torvalds committed
809

810
/* add dynamic controls from template */
811
812
static struct snd_kcontrol_new *
add_control(struct hda_gen_spec *spec, int type, const char *name,
813
		       int cidx, unsigned long val)
Linus Torvalds's avatar
Linus Torvalds committed
814
{
815
	struct snd_kcontrol_new *knew;
Linus Torvalds's avatar
Linus Torvalds committed
816

817
	knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
818
	if (!knew)
819
		return NULL;
820
821
822
823
	knew->index = cidx;
	if (get_amp_nid_(val))
		knew->subdevice = HDA_SUBDEV_AMP_FLAG;
	knew->private_value = val;
824
	return knew;
Linus Torvalds's avatar
Linus Torvalds committed
825
826
}

827
828
829
static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
				const char *pfx, const char *dir,
				const char *sfx, int cidx, unsigned long val)
Linus Torvalds's avatar
Linus Torvalds committed
830
{
831
832
	char name[32];
	snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
833
834
835
	if (!add_control(spec, type, name, cidx, val))
		return -ENOMEM;
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
836
837
}

838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
#define add_pb_vol_ctrl(spec, type, pfx, val)			\
	add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
#define add_pb_sw_ctrl(spec, type, pfx, val)			\
	add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val)			\
	add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val)			\
	add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)

static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
		       unsigned int chs, struct nid_path *path)
{
	unsigned int val;
	if (!path)
		return 0;
	val = path->ctls[NID_PATH_VOL_CTL];
	if (!val)
		return 0;
	val = amp_val_replace_channels(val, chs);
	return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
}

/* return the channel bits suitable for the given path->ctls[] */
static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
			       int type)
{
	int chs = 1; /* mono (left only) */
	if (path) {
		hda_nid_t nid = get_amp_nid_(path->ctls[type]);
		if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
			chs = 3; /* stereo */
Linus Torvalds's avatar
Linus Torvalds committed
869
	}
870
	return chs;
Linus Torvalds's avatar
Linus Torvalds committed
871
872
}

873
874
875
876
877
878
879
880
881
static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
			  struct nid_path *path)
{
	int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
	return add_vol_ctl(codec, pfx, cidx, chs, path);
}

/* create a mute-switch for the given mixer widget;
 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
Linus Torvalds's avatar
Linus Torvalds committed
882
 */
883
884
static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
		      unsigned int chs, struct nid_path *path)
Linus Torvalds's avatar
Linus Torvalds committed
885
{
886
887
	unsigned int val;
	int type = HDA_CTL_WIDGET_MUTE;
Linus Torvalds's avatar
Linus Torvalds committed
888

889
	if (!path)
Linus Torvalds's avatar
Linus Torvalds committed
890
		return 0;
891
892
	val = path->ctls[NID_PATH_MUTE_CTL];
	if (!val)
Linus Torvalds's avatar
Linus Torvalds committed
893
		return 0;
894
895
896
897
898
899
900
901
	val = amp_val_replace_channels(val, chs);
	if (get_amp_direction_(val) == HDA_INPUT) {
		hda_nid_t nid = get_amp_nid_(val);
		int nums = snd_hda_get_num_conns(codec, nid);
		if (nums > 1) {
			type = HDA_CTL_BIND_MUTE;
			val |= nums << 19;
		}
Linus Torvalds's avatar
Linus Torvalds committed
902
	}
903
904
	return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
}
Linus Torvalds's avatar
Linus Torvalds committed
905

906
907
908
909
910
911
static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
				  int cidx, struct nid_path *path)
{
	int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
	return add_sw_ctl(codec, pfx, cidx, chs, path);
}
Linus Torvalds's avatar
Linus Torvalds committed
912

913
914
915
916
917
918
919
/* any ctl assigned to the path with the given index? */
static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
{
	struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
	return path && path->ctls[ctl_type];
}

920
921
922
static const char * const channel_name[4] = {
	"Front", "Surround", "CLFE", "Side"
};
923

924
/* give some appropriate ctl name prefix for the given line out channel */
925
926
static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
				    int *index, int ctl_type)
927
{
928
	struct hda_gen_spec *spec = codec->spec;
929
	struct auto_pin_cfg *cfg = &spec->autocfg;
Linus Torvalds's avatar
Linus Torvalds committed
930

931
932
	*index = 0;
	if (cfg->line_outs == 1 && !spec->multi_ios &&
933
	    !cfg->hp_outs && !cfg->speaker_outs)
934
		return spec->vmaster_mute.hook ? "PCM" : "Master";
Linus Torvalds's avatar
Linus Torvalds committed
935

936
937
938
939
940
941
942
	/* if there is really a single DAC used in the whole output paths,
	 * use it master (or "PCM" if a vmaster hook is present)
	 */
	if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
	    !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
		return spec->vmaster_mute.hook ? "PCM" : "Master";

943
944
945
946
	/* multi-io channels */
	if (ch >= cfg->line_outs)
		return channel_name[ch];

947
948
	switch (cfg->line_out_type) {
	case AUTO_PIN_SPEAKER_OUT:
949
950
951
952
953
954
		/* if the primary channel vol/mute is shared with HP volume,
		 * don't name it as Speaker
		 */
		if (!ch && cfg->hp_outs &&
		    !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
			break;
955
956
957
958
959
960
		if (cfg->line_outs == 1)
			return "Speaker";
		if (cfg->line_outs == 2)
			return ch ? "Bass Speaker" : "Speaker";
		break;
	case AUTO_PIN_HP_OUT:
961
962
963
964
965
966
		/* if the primary channel vol/mute is shared with spk volume,
		 * don't name it as Headphone
		 */
		if (!ch && cfg->speaker_outs &&
		    !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
			break;
967
968
969
970
971
972
		/* for multi-io case, only the primary out */
		if (ch && spec->multi_ios)
			break;
		*index = ch;
		return "Headphone";
	}
973
974
975
976
977

	/* for a single channel output, we don't have to name the channel */
	if (cfg->line_outs == 1 && !spec->multi_ios)
		return "PCM";

978
979
980
	if (ch >= ARRAY_SIZE(channel_name)) {
		snd_BUG();
		return "PCM";
Linus Torvalds's avatar
Linus Torvalds committed
981
982
	}

983
	return channel_name[ch];
Linus Torvalds's avatar
Linus Torvalds committed
984
985
986
}

/*
987
 * Parse output paths
Linus Torvalds's avatar
Linus Torvalds committed
988
 */
989
990
991
992
993
994
995
996

/* badness definition */
enum {
	/* No primary DAC is found for the main output */
	BAD_NO_PRIMARY_DAC = 0x10000,
	/* No DAC is found for the extra output */
	BAD_NO_DAC = 0x4000,
	/* No possible multi-ios */
997
	BAD_MULTI_IO = 0x120,
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
	/* No individual DAC for extra output */
	BAD_NO_EXTRA_DAC = 0x102,
	/* No individual DAC for extra surrounds */
	BAD_NO_EXTRA_SURR_DAC = 0x101,
	/* Primary DAC shared with main surrounds */
	BAD_SHARED_SURROUND = 0x100,
	/* Primary DAC shared with main CLFE */
	BAD_SHARED_CLFE = 0x10,
	/* Primary DAC shared with extra surrounds */
	BAD_SHARED_EXTRA_SURROUND = 0x10,
	/* Volume widget is shared */
	BAD_SHARED_VOL = 0x10,
};

1012
/* look for widgets in the given path which are appropriate for
1013
1014
1015
1016
1017
1018
 * volume and mute controls, and assign the values to ctls[].
 *
 * When no appropriate widget is found in the path, the badness value
 * is incremented depending on the situation.  The function returns the
 * total badness for both volume and mute controls.
 */
1019
static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
Linus Torvalds's avatar
Linus Torvalds committed
1020
{
1021
1022
1023
1024
1025
1026
	hda_nid_t nid;
	unsigned int val;
	int badness = 0;

	if (!path)
		return BAD_SHARED_VOL * 2;
1027
1028
1029
1030
1031

	if (path->ctls[NID_PATH_VOL_CTL] ||
	    path->ctls[NID_PATH_MUTE_CTL])
		return 0; /* already evaluated */

1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
	nid = look_for_out_vol_nid(codec, path);
	if (nid) {
		val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
		if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
			badness += BAD_SHARED_VOL;
		else
			path->ctls[NID_PATH_VOL_CTL] = val;
	} else
		badness += BAD_SHARED_VOL;
	nid = look_for_out_mute_nid(codec, path);
	if (nid) {
		unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
		if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
		    nid_has_mute(codec, nid, HDA_OUTPUT))
			val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
		else
			val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
		if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
			badness += BAD_SHARED_VOL;
		else
			path->ctls[NID_PATH_MUTE_CTL] = val;
	} else
		badness += BAD_SHARED_VOL;
	return badness;
}
Linus Torvalds's avatar
Linus Torvalds committed
1057

1058
1059
1060
1061
1062
1063
1064
1065
struct badness_table {
	int no_primary_dac;	/* no primary DAC */
	int no_dac;		/* no secondary DACs */
	int shared_primary;	/* primary DAC is shared with main output */
	int shared_surr;	/* secondary DAC shared with main or primary */
	int shared_clfe;	/* third DAC shared with main or primary */
	int shared_surr_main;	/* secondary DAC sahred with main/DAC0 */
};
Linus Torvalds's avatar
Linus Torvalds committed
1066

1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
static struct badness_table main_out_badness = {
	.no_primary_dac = BAD_NO_PRIMARY_DAC,
	.no_dac = BAD_NO_DAC,
	.shared_primary = BAD_NO_PRIMARY_DAC,
	.shared_surr = BAD_SHARED_SURROUND,
	.shared_clfe = BAD_SHARED_CLFE,
	.shared_surr_main = BAD_SHARED_SURROUND,
};

static struct badness_table extra_out_badness = {
	.no_primary_dac = BAD_NO_DAC,
	.no_dac = BAD_NO_DAC,
	.shared_primary = BAD_NO_EXTRA_DAC,
	.shared_surr = BAD_SHARED_EXTRA_SURROUND,
	.shared_clfe = BAD_SHARED_EXTRA_SURROUND,
	.shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
};

1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
/* get the DAC of the primary output corresponding to the given array index */
static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
{
	struct hda_gen_spec *spec = codec->spec;
	struct auto_pin_cfg *cfg = &spec->autocfg;

	if (cfg->line_outs > idx)
		return spec->private_dac_nids[idx];
	idx -= cfg->line_outs;
	if (spec->multi_ios > idx)
		return spec->multi_io[idx].dac;
	return 0;
}

/* return the DAC if it's reachable, otherwise zero */
static inline hda_nid_t try_dac(struct hda_codec *codec,
				hda_nid_t dac, hda_nid_t pin)
{
	return is_reachable_path(codec, dac, pin) ? dac : 0;
}

1106
1107
1108
/* try to assign DACs to pins and return the resultant badness */
static int try_assign_dacs(struct hda_codec *codec, int num_outs,
			   const hda_nid_t *pins, hda_nid_t *dacs,
1109
			   int *path_idx,
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
			   const struct badness_table *bad)
{
	struct hda_gen_spec *spec = codec->spec;
	int i, j;
	int badness = 0;
	hda_nid_t dac;

	if (!num_outs)
		return 0;

	for (i = 0; i < num_outs; i++) {
1121
		struct nid_path *path;
1122
		hda_nid_t pin = pins[i];
1123

1124
1125
1126
		path = snd_hda_get_path_from_idx(codec, path_idx[i]);
		if (path) {
			badness += assign_out_path_ctls(codec, path);
1127
1128
1129
1130
			continue;
		}

		dacs[i] = look_for_dac(codec, pin, false);
1131
		if (!dacs[i] && !i) {
1132
			/* try to steal the DAC of surrounds for the front */
1133
1134
1135
1136
			for (j = 1; j < num_outs; j++) {
				if (is_reachable_path(codec, dacs[j], pin)) {
					dacs[0] = dacs[j];
					dacs[j] = 0;
1137
					invalidate_nid_path(codec, path_idx[j]);
1138
					path_idx[j] = 0;
1139
1140
1141
					break;
				}
			}
1142
		}
1143
1144
		dac = dacs[i];
		if (!dac) {
1145
1146
1147
1148
1149
1150
			if (num_outs > 2)
				dac = try_dac(codec, get_primary_out(codec, i), pin);
			if (!dac)
				dac = try_dac(codec, dacs[0], pin);
			if (!dac)
				dac = try_dac(codec, get_primary_out(codec, i), pin);
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
			if (dac) {
				if (!i)
					badness += bad->shared_primary;
				else if (i == 1)
					badness += bad->shared_surr;
				else
					badness += bad->shared_clfe;
			} else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
				dac = spec->private_dac_nids[0];
				badness += bad->shared_surr_main;
			} else if (!i)
				badness += bad->no_primary_dac;
			else
				badness += bad->no_dac;
Linus Torvalds's avatar
Linus Torvalds committed
1165
		}
1166
1167
		if (!dac)
			continue;
1168
		path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
1169
		if (!path && !i && spec->mixer_nid) {
1170
			/* try with aamix */
1171
			path = snd_hda_add_new_path(codec, dac, pin, 0);
1172
		}
1173
		if (!path) {
1174
			dac = dacs[i] = 0;
1175
1176
			badness += bad->no_dac;
		} else {
1177
			/* print_nid_path("output", path); */
1178
			path->active = true;
1179
			path_idx[i] = snd_hda_get_path_idx(codec, path);
1180
			badness += assign_out_path_ctls(codec, path);
1181
		}
Linus Torvalds's avatar
Linus Torvalds committed
1182
1183
	}

1184
	return badness;
Linus Torvalds's avatar
Linus Torvalds committed
1185
1186
}

1187
1188
/* return NID if the given pin has only a single connection to a certain DAC */
static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
Linus Torvalds's avatar
Linus Torvalds committed
1189
{
1190
1191
1192
	struct hda_gen_spec *spec = codec->spec;
	int i;
	hda_nid_t nid_found = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1193

1194
1195
1196
1197
1198
1199
	for (i = 0; i < spec->num_all_dacs; i++) {
		hda_nid_t nid = spec->all_dacs[i];
		if (!nid || is_dac_already_used(codec, nid))
			continue;
		if (is_reachable_path(codec, nid, pin)) {
			if (nid_found)
Linus Torvalds's avatar
Linus Torvalds committed
1200
				return 0;
1201
			nid_found = nid;
Linus Torvalds's avatar
Linus Torvalds committed
1202
1203
		}
	}
1204
	return nid_found;
Linus Torvalds's avatar
Linus Torvalds committed
1205
1206
}

1207
1208
1209
/* check whether the given pin can be a multi-io pin */
static bool can_be_multiio_pin(struct hda_codec *codec,
			       unsigned int location, hda_nid_t nid)
1210
{
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
	unsigned int defcfg, caps;

	defcfg = snd_hda_codec_get_pincfg(codec, nid);
	if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
		return false;
	if (location && get_defcfg_location(defcfg) != location)
		return false;
	caps = snd_hda_query_pin_caps(codec, nid);
	if (!(caps & AC_PINCAP_OUT))
		return false;
	return true;
1222
1223
}

1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
/* count the number of input pins that are capable to be multi-io */
static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
{
	struct hda_gen_spec *spec = codec->spec;
	struct auto_pin_cfg *cfg = &spec->autocfg;
	unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
	unsigned int location = get_defcfg_location(defcfg);
	int type, i;
	int num_pins = 0;

	for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
		for (i = 0; i < cfg->num_inputs; i++) {
			if (cfg->inputs[i].type != type)
				continue;
			if (can_be_multiio_pin(codec, location,
					       cfg->inputs[i].pin))
				num_pins++;
		}
	}
	return num_pins;
}

Linus Torvalds's avatar
Linus Torvalds committed
1246
/*
1247
1248
1249
1250
1251
1252
 * multi-io helper
 *
 * When hardwired is set, try to fill ony hardwired pins, and returns
 * zero if any pins are filled, non-zero if nothing found.
 * When hardwired is off, try to fill possible input pins, and returns
 * the badness value.
Linus Torvalds's avatar
Linus Torvalds committed
1253
 */
1254
1255
static int fill_multi_ios(struct hda_codec *codec,
			  hda_nid_t reference_pin,
1256
			  bool hardwired)
Linus Torvalds's avatar
Linus Torvalds committed
1257
{
1258
1259
	struct hda_gen_spec *spec = codec->spec;
	struct auto_pin_cfg *cfg = &spec->autocfg;
1260
	int type, i, j, num_pins, old_pins;
1261
1262
1263
	unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
	unsigned int location = get_defcfg_location(defcfg);
	int badness = 0;
1264
	struct nid_path *path;
1265
1266
1267
1268
1269

	old_pins = spec->multi_ios;
	if (old_pins >= 2)
		goto end_fill;

1270
	num_pins = count_multiio_pins(codec, reference_pin);
1271
1272
	if (num_pins < 2)
		goto end_fill;
Linus Torvalds's avatar
Linus Torvalds committed
1273

1274
1275
1276
1277
	for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
		for (i = 0; i < cfg->num_inputs; i++) {
			hda_nid_t nid = cfg->inputs[i].pin;
			hda_nid_t dac = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1278

1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
			if (cfg->inputs[i].type != type)
				continue;
			if (!can_be_multiio_pin(codec, location, nid))
				continue;
			for (j = 0; j < spec->multi_ios; j++) {
				if (nid == spec->multi_io[j].pin)
					break;
			}
			if (j < spec->multi_ios)
				continue;

			if (hardwired)
				dac = get_dac_if_single(codec, nid);
			else if (!dac)
				dac = look_for_dac(codec, nid, false);
			if (!dac) {
				badness++;
				continue;
			}
1298
1299
			path = snd_hda_add_new_path(codec, dac, nid,
						    -spec->mixer_nid);
1300
			if (!path) {
1301
1302
1303
				badness++;