Merge remote-tracking branches 'asoc/topic/davinci' and 'asoc/topic/dpcm' into asoc...
[linux-2.6-block.git] / sound / pci / hda / hda_generic.c
CommitLineData
1da177e4
LT
1/*
2 * Universal Interface for Intel High Definition Audio Codec
3 *
4 * Generic widget tree parser
5 *
6 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
7 *
8 * This driver is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This driver is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
1da177e4
LT
23#include <linux/init.h>
24#include <linux/slab.h>
d81a6d71 25#include <linux/export.h>
352f7f91 26#include <linux/sort.h>
55196fff 27#include <linux/delay.h>
f873e536
TI
28#include <linux/ctype.h>
29#include <linux/string.h>
29476558 30#include <linux/bitops.h>
b21bdd0d 31#include <linux/module.h>
1da177e4 32#include <sound/core.h>
352f7f91 33#include <sound/jack.h>
d89c6c0c 34#include <sound/tlv.h>
1da177e4
LT
35#include "hda_codec.h"
36#include "hda_local.h"
352f7f91
TI
37#include "hda_auto_parser.h"
38#include "hda_jack.h"
7504b6cd 39#include "hda_beep.h"
352f7f91 40#include "hda_generic.h"
1da177e4 41
a7da6ce5 42
dda42bd0
TI
43/**
44 * snd_hda_gen_spec_init - initialize hda_gen_spec struct
45 * @spec: hda_gen_spec object to initialize
46 *
47 * Initialize the given hda_gen_spec object.
48 */
352f7f91
TI
49int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
50{
51 snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
352f7f91 52 snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
0186f4f4 53 snd_array_init(&spec->loopback_list, sizeof(struct hda_amp_list), 8);
38cf6f1a 54 mutex_init(&spec->pcm_mutex);
352f7f91
TI
55 return 0;
56}
2698ea98 57EXPORT_SYMBOL_GPL(snd_hda_gen_spec_init);
1da177e4 58
dda42bd0
TI
59/**
60 * snd_hda_gen_add_kctl - Add a new kctl_new struct from the template
61 * @spec: hda_gen_spec object
62 * @name: name string to override the template, NULL if unchanged
63 * @temp: template for the new kctl
64 *
65 * Add a new kctl (actually snd_kcontrol_new to be instantiated later)
66 * element based on the given snd_kcontrol_new template @temp and the
67 * name string @name to the list in @spec.
68 * Returns the newly created object or NULL as error.
69 */
12c93df6
TI
70struct snd_kcontrol_new *
71snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
72 const struct snd_kcontrol_new *temp)
352f7f91
TI
73{
74 struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
75 if (!knew)
76 return NULL;
77 *knew = *temp;
78 if (name)
79 knew->name = kstrdup(name, GFP_KERNEL);
80 else if (knew->name)
81 knew->name = kstrdup(knew->name, GFP_KERNEL);
82 if (!knew->name)
83 return NULL;
84 return knew;
85}
2698ea98 86EXPORT_SYMBOL_GPL(snd_hda_gen_add_kctl);
1da177e4 87
352f7f91
TI
88static void free_kctls(struct hda_gen_spec *spec)
89{
90 if (spec->kctls.list) {
91 struct snd_kcontrol_new *kctl = spec->kctls.list;
92 int i;
93 for (i = 0; i < spec->kctls.used; i++)
94 kfree(kctl[i].name);
95 }
96 snd_array_free(&spec->kctls);
97}
1da177e4 98
a8dca460 99static void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
352f7f91
TI
100{
101 if (!spec)
102 return;
103 free_kctls(spec);
352f7f91 104 snd_array_free(&spec->paths);
0186f4f4 105 snd_array_free(&spec->loopback_list);
352f7f91 106}
1da177e4 107
1c70a583
TI
108/*
109 * store user hints
110 */
111static void parse_user_hints(struct hda_codec *codec)
112{
113 struct hda_gen_spec *spec = codec->spec;
114 int val;
115
116 val = snd_hda_get_bool_hint(codec, "jack_detect");
117 if (val >= 0)
118 codec->no_jack_detect = !val;
119 val = snd_hda_get_bool_hint(codec, "inv_jack_detect");
120 if (val >= 0)
121 codec->inv_jack_detect = !!val;
122 val = snd_hda_get_bool_hint(codec, "trigger_sense");
123 if (val >= 0)
124 codec->no_trigger_sense = !val;
125 val = snd_hda_get_bool_hint(codec, "inv_eapd");
126 if (val >= 0)
127 codec->inv_eapd = !!val;
128 val = snd_hda_get_bool_hint(codec, "pcm_format_first");
129 if (val >= 0)
130 codec->pcm_format_first = !!val;
131 val = snd_hda_get_bool_hint(codec, "sticky_stream");
132 if (val >= 0)
133 codec->no_sticky_stream = !val;
134 val = snd_hda_get_bool_hint(codec, "spdif_status_reset");
135 if (val >= 0)
136 codec->spdif_status_reset = !!val;
137 val = snd_hda_get_bool_hint(codec, "pin_amp_workaround");
138 if (val >= 0)
139 codec->pin_amp_workaround = !!val;
140 val = snd_hda_get_bool_hint(codec, "single_adc_amp");
141 if (val >= 0)
142 codec->single_adc_amp = !!val;
967b1307 143 val = snd_hda_get_bool_hint(codec, "power_save_node");
e6feb5d0 144 if (val >= 0)
967b1307 145 codec->power_save_node = !!val;
1c70a583 146
f72706be
TI
147 val = snd_hda_get_bool_hint(codec, "auto_mute");
148 if (val >= 0)
149 spec->suppress_auto_mute = !val;
1c70a583
TI
150 val = snd_hda_get_bool_hint(codec, "auto_mic");
151 if (val >= 0)
152 spec->suppress_auto_mic = !val;
153 val = snd_hda_get_bool_hint(codec, "line_in_auto_switch");
154 if (val >= 0)
155 spec->line_in_auto_switch = !!val;
7eebffd3
TI
156 val = snd_hda_get_bool_hint(codec, "auto_mute_via_amp");
157 if (val >= 0)
158 spec->auto_mute_via_amp = !!val;
1c70a583
TI
159 val = snd_hda_get_bool_hint(codec, "need_dac_fix");
160 if (val >= 0)
161 spec->need_dac_fix = !!val;
162 val = snd_hda_get_bool_hint(codec, "primary_hp");
163 if (val >= 0)
164 spec->no_primary_hp = !val;
da96fb5b
TI
165 val = snd_hda_get_bool_hint(codec, "multi_io");
166 if (val >= 0)
167 spec->no_multi_io = !val;
1c70a583
TI
168 val = snd_hda_get_bool_hint(codec, "multi_cap_vol");
169 if (val >= 0)
170 spec->multi_cap_vol = !!val;
171 val = snd_hda_get_bool_hint(codec, "inv_dmic_split");
172 if (val >= 0)
173 spec->inv_dmic_split = !!val;
174 val = snd_hda_get_bool_hint(codec, "indep_hp");
175 if (val >= 0)
176 spec->indep_hp = !!val;
177 val = snd_hda_get_bool_hint(codec, "add_stereo_mix_input");
178 if (val >= 0)
179 spec->add_stereo_mix_input = !!val;
f811c3cf 180 /* the following two are just for compatibility */
1c70a583
TI
181 val = snd_hda_get_bool_hint(codec, "add_out_jack_modes");
182 if (val >= 0)
f811c3cf 183 spec->add_jack_modes = !!val;
29476558
TI
184 val = snd_hda_get_bool_hint(codec, "add_in_jack_modes");
185 if (val >= 0)
f811c3cf
TI
186 spec->add_jack_modes = !!val;
187 val = snd_hda_get_bool_hint(codec, "add_jack_modes");
188 if (val >= 0)
189 spec->add_jack_modes = !!val;
55196fff
TI
190 val = snd_hda_get_bool_hint(codec, "power_down_unused");
191 if (val >= 0)
192 spec->power_down_unused = !!val;
967303da
TI
193 val = snd_hda_get_bool_hint(codec, "add_hp_mic");
194 if (val >= 0)
195 spec->hp_mic = !!val;
196 val = snd_hda_get_bool_hint(codec, "hp_mic_detect");
197 if (val >= 0)
198 spec->suppress_hp_mic_detect = !val;
1c70a583
TI
199
200 if (!snd_hda_get_int_hint(codec, "mixer_nid", &val))
201 spec->mixer_nid = val;
202}
203
2c12c30d
TI
204/*
205 * pin control value accesses
206 */
207
208#define update_pin_ctl(codec, pin, val) \
209 snd_hda_codec_update_cache(codec, pin, 0, \
210 AC_VERB_SET_PIN_WIDGET_CONTROL, val)
211
212/* restore the pinctl based on the cached value */
213static inline void restore_pin_ctl(struct hda_codec *codec, hda_nid_t pin)
214{
215 update_pin_ctl(codec, pin, snd_hda_codec_get_pin_target(codec, pin));
216}
217
218/* set the pinctl target value and write it if requested */
219static void set_pin_target(struct hda_codec *codec, hda_nid_t pin,
220 unsigned int val, bool do_write)
221{
222 if (!pin)
223 return;
224 val = snd_hda_correct_pin_ctl(codec, pin, val);
225 snd_hda_codec_set_pin_target(codec, pin, val);
226 if (do_write)
227 update_pin_ctl(codec, pin, val);
228}
229
230/* set pinctl target values for all given pins */
231static void set_pin_targets(struct hda_codec *codec, int num_pins,
232 hda_nid_t *pins, unsigned int val)
233{
234 int i;
235 for (i = 0; i < num_pins; i++)
236 set_pin_target(codec, pins[i], val, false);
237}
238
1da177e4 239/*
352f7f91 240 * parsing paths
1da177e4 241 */
1da177e4 242
3ca529d3
TI
243/* return the position of NID in the list, or -1 if not found */
244static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
245{
246 int i;
247 for (i = 0; i < nums; i++)
248 if (list[i] == nid)
249 return i;
250 return -1;
251}
252
253/* return true if the given NID is contained in the path */
254static bool is_nid_contained(struct nid_path *path, hda_nid_t nid)
255{
256 return find_idx_in_nid_list(nid, path->path, path->depth) >= 0;
257}
258
f5172a7e
TI
259static struct nid_path *get_nid_path(struct hda_codec *codec,
260 hda_nid_t from_nid, hda_nid_t to_nid,
3ca529d3 261 int anchor_nid)
1da177e4 262{
352f7f91
TI
263 struct hda_gen_spec *spec = codec->spec;
264 int i;
1da177e4 265
352f7f91
TI
266 for (i = 0; i < spec->paths.used; i++) {
267 struct nid_path *path = snd_array_elem(&spec->paths, i);
268 if (path->depth <= 0)
269 continue;
270 if ((!from_nid || path->path[0] == from_nid) &&
f5172a7e 271 (!to_nid || path->path[path->depth - 1] == to_nid)) {
3ca529d3
TI
272 if (!anchor_nid ||
273 (anchor_nid > 0 && is_nid_contained(path, anchor_nid)) ||
274 (anchor_nid < 0 && !is_nid_contained(path, anchor_nid)))
f5172a7e
TI
275 return path;
276 }
1da177e4 277 }
352f7f91 278 return NULL;
1da177e4 279}
f5172a7e 280
dda42bd0
TI
281/**
282 * snd_hda_get_nid_path - get the path between the given NIDs
283 * @codec: the HDA codec
284 * @from_nid: the NID where the path start from
285 * @to_nid: the NID where the path ends at
286 *
287 * Return the found nid_path object or NULL for error.
288 * Passing 0 to either @from_nid or @to_nid behaves as a wildcard.
f5172a7e
TI
289 */
290struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
291 hda_nid_t from_nid, hda_nid_t to_nid)
292{
3ca529d3 293 return get_nid_path(codec, from_nid, to_nid, 0);
f5172a7e 294}
2698ea98 295EXPORT_SYMBOL_GPL(snd_hda_get_nid_path);
1da177e4 296
dda42bd0
TI
297/**
298 * snd_hda_get_path_idx - get the index number corresponding to the path
299 * instance
300 * @codec: the HDA codec
301 * @path: nid_path object
302 *
303 * The returned index starts from 1, i.e. the actual array index with offset 1,
304 * and zero is handled as an invalid path
196c1766
TI
305 */
306int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
307{
308 struct hda_gen_spec *spec = codec->spec;
309 struct nid_path *array = spec->paths.list;
310 ssize_t idx;
311
312 if (!spec->paths.used)
313 return 0;
314 idx = path - array;
315 if (idx < 0 || idx >= spec->paths.used)
316 return 0;
317 return idx + 1;
318}
2698ea98 319EXPORT_SYMBOL_GPL(snd_hda_get_path_idx);
196c1766 320
dda42bd0
TI
321/**
322 * snd_hda_get_path_from_idx - get the path instance corresponding to the
323 * given index number
324 * @codec: the HDA codec
325 * @idx: the path index
326 */
196c1766
TI
327struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
328{
329 struct hda_gen_spec *spec = codec->spec;
330
331 if (idx <= 0 || idx > spec->paths.used)
332 return NULL;
333 return snd_array_elem(&spec->paths, idx - 1);
334}
2698ea98 335EXPORT_SYMBOL_GPL(snd_hda_get_path_from_idx);
196c1766 336
352f7f91
TI
337/* check whether the given DAC is already found in any existing paths */
338static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
1da177e4 339{
352f7f91
TI
340 struct hda_gen_spec *spec = codec->spec;
341 int i;
1da177e4 342
352f7f91
TI
343 for (i = 0; i < spec->paths.used; i++) {
344 struct nid_path *path = snd_array_elem(&spec->paths, i);
345 if (path->path[0] == nid)
346 return true;
d2569505 347 }
352f7f91
TI
348 return false;
349}
1da177e4 350
352f7f91
TI
351/* check whether the given two widgets can be connected */
352static bool is_reachable_path(struct hda_codec *codec,
353 hda_nid_t from_nid, hda_nid_t to_nid)
354{
355 if (!from_nid || !to_nid)
356 return false;
357 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
358}
1da177e4 359
352f7f91
TI
360/* nid, dir and idx */
361#define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
362
363/* check whether the given ctl is already assigned in any path elements */
364static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
365{
366 struct hda_gen_spec *spec = codec->spec;
367 int i;
368
369 val &= AMP_VAL_COMPARE_MASK;
370 for (i = 0; i < spec->paths.used; i++) {
371 struct nid_path *path = snd_array_elem(&spec->paths, i);
372 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
373 return true;
1da177e4 374 }
352f7f91 375 return false;
1da177e4
LT
376}
377
352f7f91
TI
378/* check whether a control with the given (nid, dir, idx) was assigned */
379static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
8999bf0a 380 int dir, int idx, int type)
1da177e4 381{
352f7f91 382 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
8999bf0a 383 return is_ctl_used(codec, val, type);
352f7f91 384}
1da177e4 385
4e76a883
TI
386static void print_nid_path(struct hda_codec *codec,
387 const char *pfx, struct nid_path *path)
0c8c0f56
TI
388{
389 char buf[40];
d82353e5 390 char *pos = buf;
0c8c0f56
TI
391 int i;
392
d82353e5
JP
393 *pos = 0;
394 for (i = 0; i < path->depth; i++)
395 pos += scnprintf(pos, sizeof(buf) - (pos - buf), "%s%02x",
396 pos != buf ? ":" : "",
397 path->path[i]);
0c8c0f56 398
d82353e5 399 codec_dbg(codec, "%s path: depth=%d '%s'\n", pfx, path->depth, buf);
0c8c0f56
TI
400}
401
352f7f91
TI
402/* called recursively */
403static bool __parse_nid_path(struct hda_codec *codec,
404 hda_nid_t from_nid, hda_nid_t to_nid,
3ca529d3
TI
405 int anchor_nid, struct nid_path *path,
406 int depth)
352f7f91 407{
ee8e765b 408 const hda_nid_t *conn;
352f7f91
TI
409 int i, nums;
410
3ca529d3
TI
411 if (to_nid == anchor_nid)
412 anchor_nid = 0; /* anchor passed */
413 else if (to_nid == (hda_nid_t)(-anchor_nid))
414 return false; /* hit the exclusive nid */
1da177e4 415
ee8e765b 416 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
352f7f91
TI
417 for (i = 0; i < nums; i++) {
418 if (conn[i] != from_nid) {
419 /* special case: when from_nid is 0,
420 * try to find an empty DAC
421 */
422 if (from_nid ||
423 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
424 is_dac_already_used(codec, conn[i]))
425 continue;
426 }
3ca529d3
TI
427 /* anchor is not requested or already passed? */
428 if (anchor_nid <= 0)
352f7f91 429 goto found;
1da177e4 430 }
352f7f91
TI
431 if (depth >= MAX_NID_PATH_DEPTH)
432 return false;
433 for (i = 0; i < nums; i++) {
434 unsigned int type;
435 type = get_wcaps_type(get_wcaps(codec, conn[i]));
436 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
437 type == AC_WID_PIN)
438 continue;
439 if (__parse_nid_path(codec, from_nid, conn[i],
3ca529d3 440 anchor_nid, path, depth + 1))
352f7f91
TI
441 goto found;
442 }
443 return false;
444
445 found:
446 path->path[path->depth] = conn[i];
447 path->idx[path->depth + 1] = i;
448 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
449 path->multi[path->depth + 1] = 1;
450 path->depth++;
451 return true;
1da177e4
LT
452}
453
dda42bd0
TI
454/**
455 * snd_hda_parse_nid_path - parse the widget path from the given nid to
456 * the target nid
457 * @codec: the HDA codec
458 * @from_nid: the NID where the path start from
459 * @to_nid: the NID where the path ends at
460 * @anchor_nid: the anchor indication
461 * @path: the path object to store the result
462 *
463 * Returns true if a matching path is found.
464 *
465 * The parsing behavior depends on parameters:
352f7f91 466 * when @from_nid is 0, try to find an empty DAC;
3ca529d3
TI
467 * when @anchor_nid is set to a positive value, only paths through the widget
468 * with the given value are evaluated.
469 * when @anchor_nid is set to a negative value, paths through the widget
470 * with the negative of given value are excluded, only other paths are chosen.
471 * when @anchor_nid is zero, no special handling about path selection.
1da177e4 472 */
352f7f91 473bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
3ca529d3 474 hda_nid_t to_nid, int anchor_nid,
352f7f91 475 struct nid_path *path)
1da177e4 476{
3ca529d3 477 if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) {
352f7f91
TI
478 path->path[path->depth] = to_nid;
479 path->depth++;
352f7f91 480 return true;
1da177e4 481 }
352f7f91 482 return false;
1da177e4 483}
2698ea98 484EXPORT_SYMBOL_GPL(snd_hda_parse_nid_path);
1da177e4 485
dda42bd0
TI
486/**
487 * snd_hda_add_new_path - parse the path between the given NIDs and
488 * add to the path list
489 * @codec: the HDA codec
490 * @from_nid: the NID where the path start from
491 * @to_nid: the NID where the path ends at
492 * @anchor_nid: the anchor indication, see snd_hda_parse_nid_path()
493 *
494 * If no valid path is found, returns NULL.
1da177e4 495 */
352f7f91
TI
496struct nid_path *
497snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
3ca529d3 498 hda_nid_t to_nid, int anchor_nid)
352f7f91
TI
499{
500 struct hda_gen_spec *spec = codec->spec;
501 struct nid_path *path;
502
503 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
504 return NULL;
505
f5172a7e 506 /* check whether the path has been already added */
3ca529d3 507 path = get_nid_path(codec, from_nid, to_nid, anchor_nid);
f5172a7e
TI
508 if (path)
509 return path;
510
352f7f91
TI
511 path = snd_array_new(&spec->paths);
512 if (!path)
513 return NULL;
514 memset(path, 0, sizeof(*path));
3ca529d3 515 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path))
352f7f91
TI
516 return path;
517 /* push back */
518 spec->paths.used--;
519 return NULL;
1da177e4 520}
2698ea98 521EXPORT_SYMBOL_GPL(snd_hda_add_new_path);
1da177e4 522
980428ce
TI
523/* clear the given path as invalid so that it won't be picked up later */
524static void invalidate_nid_path(struct hda_codec *codec, int idx)
525{
526 struct nid_path *path = snd_hda_get_path_from_idx(codec, idx);
527 if (!path)
528 return;
529 memset(path, 0, sizeof(*path));
530}
531
3690739b
TI
532/* return a DAC if paired to the given pin by codec driver */
533static hda_nid_t get_preferred_dac(struct hda_codec *codec, hda_nid_t pin)
534{
535 struct hda_gen_spec *spec = codec->spec;
536 const hda_nid_t *list = spec->preferred_dacs;
537
538 if (!list)
539 return 0;
540 for (; *list; list += 2)
541 if (*list == pin)
542 return list[1];
543 return 0;
544}
545
352f7f91
TI
546/* look for an empty DAC slot */
547static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
548 bool is_digital)
549{
550 struct hda_gen_spec *spec = codec->spec;
551 bool cap_digital;
552 int i;
553
554 for (i = 0; i < spec->num_all_dacs; i++) {
555 hda_nid_t nid = spec->all_dacs[i];
556 if (!nid || is_dac_already_used(codec, nid))
557 continue;
558 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
559 if (is_digital != cap_digital)
560 continue;
561 if (is_reachable_path(codec, nid, pin))
562 return nid;
563 }
82beb8fd 564 return 0;
1da177e4
LT
565}
566
352f7f91
TI
567/* replace the channels in the composed amp value with the given number */
568static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
1da177e4 569{
352f7f91
TI
570 val &= ~(0x3U << 16);
571 val |= chs << 16;
572 return val;
1da177e4
LT
573}
574
99a5592d
DH
575static bool same_amp_caps(struct hda_codec *codec, hda_nid_t nid1,
576 hda_nid_t nid2, int dir)
577{
578 if (!(get_wcaps(codec, nid1) & (1 << (dir + 1))))
579 return !(get_wcaps(codec, nid2) & (1 << (dir + 1)));
580 return (query_amp_caps(codec, nid1, dir) ==
581 query_amp_caps(codec, nid2, dir));
582}
583
352f7f91
TI
584/* look for a widget suitable for assigning a mute switch in the path */
585static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
586 struct nid_path *path)
587{
588 int i;
589
590 for (i = path->depth - 1; i >= 0; i--) {
591 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
592 return path->path[i];
593 if (i != path->depth - 1 && i != 0 &&
594 nid_has_mute(codec, path->path[i], HDA_INPUT))
595 return path->path[i];
596 }
597 return 0;
598}
599
600/* look for a widget suitable for assigning a volume ctl in the path */
601static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
602 struct nid_path *path)
603{
a1114a8c 604 struct hda_gen_spec *spec = codec->spec;
352f7f91 605 int i;
1da177e4 606
352f7f91 607 for (i = path->depth - 1; i >= 0; i--) {
a1114a8c
TI
608 hda_nid_t nid = path->path[i];
609 if ((spec->out_vol_mask >> nid) & 1)
610 continue;
611 if (nid_has_volume(codec, nid, HDA_OUTPUT))
612 return nid;
1da177e4 613 }
352f7f91 614 return 0;
1da177e4
LT
615}
616
617/*
352f7f91 618 * path activation / deactivation
1da177e4 619 */
352f7f91
TI
620
621/* can have the amp-in capability? */
622static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
1da177e4 623{
352f7f91
TI
624 hda_nid_t nid = path->path[idx];
625 unsigned int caps = get_wcaps(codec, nid);
626 unsigned int type = get_wcaps_type(caps);
627
628 if (!(caps & AC_WCAP_IN_AMP))
629 return false;
630 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
631 return false;
632 return true;
633}
1da177e4 634
352f7f91
TI
635/* can have the amp-out capability? */
636static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
637{
638 hda_nid_t nid = path->path[idx];
639 unsigned int caps = get_wcaps(codec, nid);
640 unsigned int type = get_wcaps_type(caps);
641
642 if (!(caps & AC_WCAP_OUT_AMP))
643 return false;
644 if (type == AC_WID_PIN && !idx) /* only for output pins */
645 return false;
646 return true;
647}
1da177e4 648
352f7f91
TI
649/* check whether the given (nid,dir,idx) is active */
650static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
7dddf2ae 651 unsigned int dir, unsigned int idx)
352f7f91
TI
652{
653 struct hda_gen_spec *spec = codec->spec;
e6feb5d0 654 int type = get_wcaps_type(get_wcaps(codec, nid));
352f7f91 655 int i, n;
1da177e4 656
7639a06c 657 if (nid == codec->core.afg)
5ccf835c
TI
658 return true;
659
352f7f91
TI
660 for (n = 0; n < spec->paths.used; n++) {
661 struct nid_path *path = snd_array_elem(&spec->paths, n);
662 if (!path->active)
1da177e4 663 continue;
967b1307 664 if (codec->power_save_node) {
e6feb5d0
TI
665 if (!path->stream_enabled)
666 continue;
667 /* ignore unplugged paths except for DAC/ADC */
6b275b14 668 if (!(path->pin_enabled || path->pin_fixed) &&
e6feb5d0
TI
669 type != AC_WID_AUD_OUT && type != AC_WID_AUD_IN)
670 continue;
671 }
352f7f91
TI
672 for (i = 0; i < path->depth; i++) {
673 if (path->path[i] == nid) {
674 if (dir == HDA_OUTPUT || path->idx[i] == idx)
675 return true;
676 break;
1da177e4 677 }
1da177e4
LT
678 }
679 }
352f7f91 680 return false;
1da177e4
LT
681}
682
b1b9fbd0
TI
683/* check whether the NID is referred by any active paths */
684#define is_active_nid_for_any(codec, nid) \
685 is_active_nid(codec, nid, HDA_OUTPUT, 0)
686
352f7f91
TI
687/* get the default amp value for the target state */
688static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
8999bf0a 689 int dir, unsigned int caps, bool enable)
1da177e4 690{
352f7f91
TI
691 unsigned int val = 0;
692
352f7f91
TI
693 if (caps & AC_AMPCAP_NUM_STEPS) {
694 /* set to 0dB */
695 if (enable)
696 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
1da177e4 697 }
f69910dd 698 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
352f7f91
TI
699 if (!enable)
700 val |= HDA_AMP_MUTE;
701 }
702 return val;
1da177e4
LT
703}
704
cc261738
TI
705/* is this a stereo widget or a stereo-to-mono mix? */
706static bool is_stereo_amps(struct hda_codec *codec, hda_nid_t nid, int dir)
707{
708 unsigned int wcaps = get_wcaps(codec, nid);
709 hda_nid_t conn;
710
711 if (wcaps & AC_WCAP_STEREO)
712 return true;
713 if (dir != HDA_INPUT || get_wcaps_type(wcaps) != AC_WID_AUD_MIX)
714 return false;
715 if (snd_hda_get_num_conns(codec, nid) != 1)
716 return false;
717 if (snd_hda_get_connections(codec, nid, &conn, 1) < 0)
718 return false;
719 return !!(get_wcaps(codec, conn) & AC_WCAP_STEREO);
720}
721
352f7f91
TI
722/* initialize the amp value (only at the first time) */
723static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
724{
8999bf0a
TI
725 unsigned int caps = query_amp_caps(codec, nid, dir);
726 int val = get_amp_val_to_activate(codec, nid, dir, caps, false);
ef403edb 727
cc261738 728 if (is_stereo_amps(codec, nid, dir))
ef403edb
TI
729 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
730 else
731 snd_hda_codec_amp_init(codec, nid, 0, dir, idx, 0xff, val);
732}
733
734/* update the amp, doing in stereo or mono depending on NID */
735static int update_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx,
736 unsigned int mask, unsigned int val)
737{
cc261738 738 if (is_stereo_amps(codec, nid, dir))
ef403edb
TI
739 return snd_hda_codec_amp_stereo(codec, nid, dir, idx,
740 mask, val);
741 else
742 return snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
743 mask, val);
352f7f91 744}
1da177e4 745
8999bf0a
TI
746/* calculate amp value mask we can modify;
747 * if the given amp is controlled by mixers, don't touch it
748 */
749static unsigned int get_amp_mask_to_modify(struct hda_codec *codec,
750 hda_nid_t nid, int dir, int idx,
751 unsigned int caps)
752{
753 unsigned int mask = 0xff;
754
f69910dd 755 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
8999bf0a
TI
756 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_MUTE_CTL))
757 mask &= ~0x80;
758 }
759 if (caps & AC_AMPCAP_NUM_STEPS) {
760 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
761 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
762 mask &= ~0x7f;
763 }
764 return mask;
765}
766
352f7f91 767static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
8999bf0a 768 int idx, int idx_to_check, bool enable)
352f7f91 769{
8999bf0a
TI
770 unsigned int caps;
771 unsigned int mask, val;
772
7dddf2ae 773 if (!enable && is_active_nid(codec, nid, dir, idx_to_check))
8999bf0a
TI
774 return;
775
776 caps = query_amp_caps(codec, nid, dir);
777 val = get_amp_val_to_activate(codec, nid, dir, caps, enable);
778 mask = get_amp_mask_to_modify(codec, nid, dir, idx_to_check, caps);
779 if (!mask)
352f7f91 780 return;
8999bf0a
TI
781
782 val &= mask;
ef403edb 783 update_amp(codec, nid, dir, idx, mask, val);
352f7f91
TI
784}
785
786static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
787 int i, bool enable)
788{
789 hda_nid_t nid = path->path[i];
790 init_amp(codec, nid, HDA_OUTPUT, 0);
8999bf0a 791 activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable);
352f7f91
TI
792}
793
794static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
795 int i, bool enable, bool add_aamix)
1da177e4 796{
352f7f91 797 struct hda_gen_spec *spec = codec->spec;
ee8e765b 798 const hda_nid_t *conn;
352f7f91
TI
799 int n, nums, idx;
800 int type;
801 hda_nid_t nid = path->path[i];
802
ee8e765b 803 nums = snd_hda_get_conn_list(codec, nid, &conn);
352f7f91
TI
804 type = get_wcaps_type(get_wcaps(codec, nid));
805 if (type == AC_WID_PIN ||
806 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
807 nums = 1;
808 idx = 0;
809 } else
810 idx = path->idx[i];
811
812 for (n = 0; n < nums; n++)
813 init_amp(codec, nid, HDA_INPUT, n);
814
352f7f91
TI
815 /* here is a little bit tricky in comparison with activate_amp_out();
816 * when aa-mixer is available, we need to enable the path as well
1da177e4 817 */
352f7f91 818 for (n = 0; n < nums; n++) {
e4a395e7 819 if (n != idx && (!add_aamix || conn[n] != spec->mixer_merge_nid))
352f7f91 820 continue;
8999bf0a 821 activate_amp(codec, nid, HDA_INPUT, n, idx, enable);
1da177e4 822 }
352f7f91 823}
1da177e4 824
e6feb5d0
TI
825/* sync power of each widget in the the given path */
826static hda_nid_t path_power_update(struct hda_codec *codec,
827 struct nid_path *path,
828 bool allow_powerdown)
829{
830 hda_nid_t nid, changed = 0;
831 int i, state;
832
833 for (i = 0; i < path->depth; i++) {
834 nid = path->path[i];
2206dc94
TI
835 if (!(get_wcaps(codec, nid) & AC_WCAP_POWER))
836 continue;
7639a06c 837 if (nid == codec->core.afg)
5ccf835c 838 continue;
e6feb5d0
TI
839 if (!allow_powerdown || is_active_nid_for_any(codec, nid))
840 state = AC_PWRST_D0;
841 else
842 state = AC_PWRST_D3;
843 if (!snd_hda_check_power_state(codec, nid, state)) {
844 snd_hda_codec_write(codec, nid, 0,
845 AC_VERB_SET_POWER_STATE, state);
846 changed = nid;
48f4b3a2
TI
847 /* all known codecs seem to be capable to handl
848 * widgets state even in D3, so far.
849 * if any new codecs need to restore the widget
850 * states after D0 transition, call the function
851 * below.
852 */
853#if 0 /* disabled */
d545a57c
TI
854 if (state == AC_PWRST_D0)
855 snd_hdac_regmap_sync_node(&codec->core, nid);
48f4b3a2 856#endif
e6feb5d0
TI
857 }
858 }
859 return changed;
860}
861
862/* do sync with the last power state change */
863static void sync_power_state_change(struct hda_codec *codec, hda_nid_t nid)
864{
865 if (nid) {
866 msleep(10);
867 snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
868 }
869}
870
dda42bd0
TI
871/**
872 * snd_hda_activate_path - activate or deactivate the given path
873 * @codec: the HDA codec
874 * @path: the path to activate/deactivate
875 * @enable: flag to activate or not
876 * @add_aamix: enable the input from aamix NID
877 *
878 * If @add_aamix is set, enable the input from aa-mix NID as well (if any).
352f7f91
TI
879 */
880void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
881 bool enable, bool add_aamix)
882{
55196fff 883 struct hda_gen_spec *spec = codec->spec;
352f7f91
TI
884 int i;
885
886 if (!enable)
887 path->active = false;
888
e6feb5d0 889 /* make sure the widget is powered up */
967b1307
TI
890 if (enable && (spec->power_down_unused || codec->power_save_node))
891 path_power_update(codec, path, codec->power_save_node);
e6feb5d0 892
352f7f91 893 for (i = path->depth - 1; i >= 0; i--) {
55196fff 894 hda_nid_t nid = path->path[i];
e6feb5d0 895
352f7f91 896 if (enable && path->multi[i])
8f0972df 897 snd_hda_codec_update_cache(codec, nid, 0,
352f7f91
TI
898 AC_VERB_SET_CONNECT_SEL,
899 path->idx[i]);
900 if (has_amp_in(codec, path, i))
901 activate_amp_in(codec, path, i, enable, add_aamix);
902 if (has_amp_out(codec, path, i))
903 activate_amp_out(codec, path, i, enable);
1da177e4
LT
904 }
905
352f7f91
TI
906 if (enable)
907 path->active = true;
1da177e4 908}
2698ea98 909EXPORT_SYMBOL_GPL(snd_hda_activate_path);
352f7f91 910
55196fff
TI
911/* if the given path is inactive, put widgets into D3 (only if suitable) */
912static void path_power_down_sync(struct hda_codec *codec, struct nid_path *path)
913{
914 struct hda_gen_spec *spec = codec->spec;
55196fff 915
967b1307 916 if (!(spec->power_down_unused || codec->power_save_node) || path->active)
55196fff 917 return;
e6feb5d0 918 sync_power_state_change(codec, path_power_update(codec, path, true));
55196fff
TI
919}
920
d5a9f1bb
TI
921/* turn on/off EAPD on the given pin */
922static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
923{
924 struct hda_gen_spec *spec = codec->spec;
925 if (spec->own_eapd_ctl ||
926 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
927 return;
05909d5c
TI
928 if (spec->keep_eapd_on && !enable)
929 return;
468ac413
TI
930 if (codec->inv_eapd)
931 enable = !enable;
d5a9f1bb
TI
932 snd_hda_codec_update_cache(codec, pin, 0,
933 AC_VERB_SET_EAPD_BTLENABLE,
934 enable ? 0x02 : 0x00);
935}
936
3e367f15
TI
937/* re-initialize the path specified by the given path index */
938static void resume_path_from_idx(struct hda_codec *codec, int path_idx)
939{
940 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
941 if (path)
942 snd_hda_activate_path(codec, path, path->active, false);
943}
944
1da177e4
LT
945
946/*
352f7f91 947 * Helper functions for creating mixer ctl elements
1da177e4
LT
948 */
949
7eebffd3
TI
950static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
951 struct snd_ctl_elem_value *ucontrol);
bc2eee29
TI
952static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
953 struct snd_ctl_elem_value *ucontrol);
7eebffd3 954
352f7f91
TI
955enum {
956 HDA_CTL_WIDGET_VOL,
957 HDA_CTL_WIDGET_MUTE,
958 HDA_CTL_BIND_MUTE,
352f7f91
TI
959};
960static const struct snd_kcontrol_new control_templates[] = {
961 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
7eebffd3
TI
962 /* only the put callback is replaced for handling the special mute */
963 {
964 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
965 .subdevice = HDA_SUBDEV_AMP_FLAG,
966 .info = snd_hda_mixer_amp_switch_info,
967 .get = snd_hda_mixer_amp_switch_get,
968 .put = hda_gen_mixer_mute_put, /* replaced */
969 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
970 },
bc2eee29
TI
971 {
972 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
973 .info = snd_hda_mixer_amp_switch_info,
974 .get = snd_hda_mixer_bind_switch_get,
975 .put = hda_gen_bind_mute_put, /* replaced */
976 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
977 },
352f7f91 978};
1da177e4 979
352f7f91 980/* add dynamic controls from template */
a35bd1e3
TI
981static struct snd_kcontrol_new *
982add_control(struct hda_gen_spec *spec, int type, const char *name,
352f7f91 983 int cidx, unsigned long val)
1da177e4 984{
352f7f91 985 struct snd_kcontrol_new *knew;
1da177e4 986
12c93df6 987 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
352f7f91 988 if (!knew)
a35bd1e3 989 return NULL;
352f7f91
TI
990 knew->index = cidx;
991 if (get_amp_nid_(val))
992 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
993 knew->private_value = val;
a35bd1e3 994 return knew;
1da177e4
LT
995}
996
352f7f91
TI
997static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
998 const char *pfx, const char *dir,
999 const char *sfx, int cidx, unsigned long val)
1da177e4 1000{
975cc02a 1001 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
352f7f91 1002 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
a35bd1e3
TI
1003 if (!add_control(spec, type, name, cidx, val))
1004 return -ENOMEM;
1005 return 0;
1da177e4
LT
1006}
1007
352f7f91
TI
1008#define add_pb_vol_ctrl(spec, type, pfx, val) \
1009 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
1010#define add_pb_sw_ctrl(spec, type, pfx, val) \
1011 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
1012#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
1013 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
1014#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
1015 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
1016
1017static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
1018 unsigned int chs, struct nid_path *path)
1019{
1020 unsigned int val;
1021 if (!path)
1022 return 0;
1023 val = path->ctls[NID_PATH_VOL_CTL];
1024 if (!val)
1025 return 0;
1026 val = amp_val_replace_channels(val, chs);
1027 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
1028}
1029
1030/* return the channel bits suitable for the given path->ctls[] */
1031static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
1032 int type)
1033{
1034 int chs = 1; /* mono (left only) */
1035 if (path) {
1036 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
1037 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
1038 chs = 3; /* stereo */
1da177e4 1039 }
352f7f91 1040 return chs;
1da177e4
LT
1041}
1042
352f7f91
TI
1043static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
1044 struct nid_path *path)
1045{
1046 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
1047 return add_vol_ctl(codec, pfx, cidx, chs, path);
1048}
1049
1050/* create a mute-switch for the given mixer widget;
1051 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
1da177e4 1052 */
352f7f91
TI
1053static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
1054 unsigned int chs, struct nid_path *path)
1da177e4 1055{
352f7f91
TI
1056 unsigned int val;
1057 int type = HDA_CTL_WIDGET_MUTE;
1da177e4 1058
352f7f91 1059 if (!path)
1da177e4 1060 return 0;
352f7f91
TI
1061 val = path->ctls[NID_PATH_MUTE_CTL];
1062 if (!val)
1da177e4 1063 return 0;
352f7f91
TI
1064 val = amp_val_replace_channels(val, chs);
1065 if (get_amp_direction_(val) == HDA_INPUT) {
1066 hda_nid_t nid = get_amp_nid_(val);
1067 int nums = snd_hda_get_num_conns(codec, nid);
1068 if (nums > 1) {
1069 type = HDA_CTL_BIND_MUTE;
1070 val |= nums << 19;
1071 }
1da177e4 1072 }
352f7f91
TI
1073 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
1074}
1da177e4 1075
352f7f91
TI
1076static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
1077 int cidx, struct nid_path *path)
1078{
1079 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
1080 return add_sw_ctl(codec, pfx, cidx, chs, path);
1081}
1da177e4 1082
7eebffd3 1083/* playback mute control with the software mute bit check */
bc2eee29
TI
1084static void sync_auto_mute_bits(struct snd_kcontrol *kcontrol,
1085 struct snd_ctl_elem_value *ucontrol)
7eebffd3
TI
1086{
1087 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1088 struct hda_gen_spec *spec = codec->spec;
1089
1090 if (spec->auto_mute_via_amp) {
1091 hda_nid_t nid = get_amp_nid(kcontrol);
1092 bool enabled = !((spec->mute_bits >> nid) & 1);
1093 ucontrol->value.integer.value[0] &= enabled;
1094 ucontrol->value.integer.value[1] &= enabled;
1095 }
bc2eee29 1096}
7eebffd3 1097
bc2eee29
TI
1098static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
1099 struct snd_ctl_elem_value *ucontrol)
1100{
1101 sync_auto_mute_bits(kcontrol, ucontrol);
7eebffd3
TI
1102 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1103}
1104
bc2eee29
TI
1105static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
1106 struct snd_ctl_elem_value *ucontrol)
1107{
1108 sync_auto_mute_bits(kcontrol, ucontrol);
1109 return snd_hda_mixer_bind_switch_put(kcontrol, ucontrol);
1110}
1111
247d85ee
TI
1112/* any ctl assigned to the path with the given index? */
1113static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
1114{
1115 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
1116 return path && path->ctls[ctl_type];
1117}
1118
352f7f91
TI
1119static const char * const channel_name[4] = {
1120 "Front", "Surround", "CLFE", "Side"
1121};
97ec558a 1122
352f7f91 1123/* give some appropriate ctl name prefix for the given line out channel */
247d85ee
TI
1124static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
1125 int *index, int ctl_type)
352f7f91 1126{
247d85ee 1127 struct hda_gen_spec *spec = codec->spec;
352f7f91 1128 struct auto_pin_cfg *cfg = &spec->autocfg;
1da177e4 1129
352f7f91
TI
1130 *index = 0;
1131 if (cfg->line_outs == 1 && !spec->multi_ios &&
247d85ee 1132 !cfg->hp_outs && !cfg->speaker_outs)
352f7f91 1133 return spec->vmaster_mute.hook ? "PCM" : "Master";
1da177e4 1134
352f7f91
TI
1135 /* if there is really a single DAC used in the whole output paths,
1136 * use it master (or "PCM" if a vmaster hook is present)
1137 */
1138 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
1139 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
1140 return spec->vmaster_mute.hook ? "PCM" : "Master";
1141
247d85ee
TI
1142 /* multi-io channels */
1143 if (ch >= cfg->line_outs)
1144 return channel_name[ch];
1145
352f7f91
TI
1146 switch (cfg->line_out_type) {
1147 case AUTO_PIN_SPEAKER_OUT:
247d85ee
TI
1148 /* if the primary channel vol/mute is shared with HP volume,
1149 * don't name it as Speaker
1150 */
1151 if (!ch && cfg->hp_outs &&
1152 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
1153 break;
352f7f91
TI
1154 if (cfg->line_outs == 1)
1155 return "Speaker";
1156 if (cfg->line_outs == 2)
1157 return ch ? "Bass Speaker" : "Speaker";
1158 break;
1159 case AUTO_PIN_HP_OUT:
247d85ee
TI
1160 /* if the primary channel vol/mute is shared with spk volume,
1161 * don't name it as Headphone
1162 */
1163 if (!ch && cfg->speaker_outs &&
1164 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
1165 break;
352f7f91
TI
1166 /* for multi-io case, only the primary out */
1167 if (ch && spec->multi_ios)
1168 break;
1169 *index = ch;
1170 return "Headphone";
03ad6a8c
DH
1171 case AUTO_PIN_LINE_OUT:
1172 /* This deals with the case where we have two DACs and
1173 * one LO, one HP and one Speaker */
1174 if (!ch && cfg->speaker_outs && cfg->hp_outs) {
1175 bool hp_lo_shared = !path_has_mixer(codec, spec->hp_paths[0], ctl_type);
1176 bool spk_lo_shared = !path_has_mixer(codec, spec->speaker_paths[0], ctl_type);
1177 if (hp_lo_shared && spk_lo_shared)
1178 return spec->vmaster_mute.hook ? "PCM" : "Master";
1179 if (hp_lo_shared)
1180 return "Headphone+LO";
1181 if (spk_lo_shared)
1182 return "Speaker+LO";
1183 }
352f7f91 1184 }
247d85ee
TI
1185
1186 /* for a single channel output, we don't have to name the channel */
1187 if (cfg->line_outs == 1 && !spec->multi_ios)
3abb4f4d 1188 return "Line Out";
247d85ee 1189
352f7f91
TI
1190 if (ch >= ARRAY_SIZE(channel_name)) {
1191 snd_BUG();
1192 return "PCM";
1da177e4 1193 }
1da177e4 1194
352f7f91 1195 return channel_name[ch];
1da177e4
LT
1196}
1197
1198/*
352f7f91 1199 * Parse output paths
1da177e4 1200 */
352f7f91
TI
1201
1202/* badness definition */
1203enum {
1204 /* No primary DAC is found for the main output */
1205 BAD_NO_PRIMARY_DAC = 0x10000,
1206 /* No DAC is found for the extra output */
1207 BAD_NO_DAC = 0x4000,
1208 /* No possible multi-ios */
1d739066 1209 BAD_MULTI_IO = 0x120,
352f7f91
TI
1210 /* No individual DAC for extra output */
1211 BAD_NO_EXTRA_DAC = 0x102,
1212 /* No individual DAC for extra surrounds */
1213 BAD_NO_EXTRA_SURR_DAC = 0x101,
1214 /* Primary DAC shared with main surrounds */
1215 BAD_SHARED_SURROUND = 0x100,
55a63d4d 1216 /* No independent HP possible */
bec8e680 1217 BAD_NO_INDEP_HP = 0x10,
352f7f91
TI
1218 /* Primary DAC shared with main CLFE */
1219 BAD_SHARED_CLFE = 0x10,
1220 /* Primary DAC shared with extra surrounds */
1221 BAD_SHARED_EXTRA_SURROUND = 0x10,
1222 /* Volume widget is shared */
1223 BAD_SHARED_VOL = 0x10,
1224};
1225
0e614dd0 1226/* look for widgets in the given path which are appropriate for
352f7f91
TI
1227 * volume and mute controls, and assign the values to ctls[].
1228 *
1229 * When no appropriate widget is found in the path, the badness value
1230 * is incremented depending on the situation. The function returns the
1231 * total badness for both volume and mute controls.
1232 */
0e614dd0 1233static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
1da177e4 1234{
d89c6c0c 1235 struct hda_gen_spec *spec = codec->spec;
352f7f91
TI
1236 hda_nid_t nid;
1237 unsigned int val;
1238 int badness = 0;
1239
1240 if (!path)
1241 return BAD_SHARED_VOL * 2;
0e614dd0
TI
1242
1243 if (path->ctls[NID_PATH_VOL_CTL] ||
1244 path->ctls[NID_PATH_MUTE_CTL])
1245 return 0; /* already evaluated */
1246
352f7f91
TI
1247 nid = look_for_out_vol_nid(codec, path);
1248 if (nid) {
1249 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
d89c6c0c
TI
1250 if (spec->dac_min_mute)
1251 val |= HDA_AMP_VAL_MIN_MUTE;
352f7f91
TI
1252 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1253 badness += BAD_SHARED_VOL;
1254 else
1255 path->ctls[NID_PATH_VOL_CTL] = val;
1256 } else
1257 badness += BAD_SHARED_VOL;
1258 nid = look_for_out_mute_nid(codec, path);
1259 if (nid) {
1260 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1261 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1262 nid_has_mute(codec, nid, HDA_OUTPUT))
1263 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1264 else
1265 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1266 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1267 badness += BAD_SHARED_VOL;
1268 else
1269 path->ctls[NID_PATH_MUTE_CTL] = val;
1270 } else
1271 badness += BAD_SHARED_VOL;
1272 return badness;
1273}
1da177e4 1274
98bd1115 1275const struct badness_table hda_main_out_badness = {
352f7f91
TI
1276 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1277 .no_dac = BAD_NO_DAC,
1278 .shared_primary = BAD_NO_PRIMARY_DAC,
1279 .shared_surr = BAD_SHARED_SURROUND,
1280 .shared_clfe = BAD_SHARED_CLFE,
1281 .shared_surr_main = BAD_SHARED_SURROUND,
1282};
2698ea98 1283EXPORT_SYMBOL_GPL(hda_main_out_badness);
352f7f91 1284
98bd1115 1285const struct badness_table hda_extra_out_badness = {
352f7f91
TI
1286 .no_primary_dac = BAD_NO_DAC,
1287 .no_dac = BAD_NO_DAC,
1288 .shared_primary = BAD_NO_EXTRA_DAC,
1289 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1290 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1291 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1292};
2698ea98 1293EXPORT_SYMBOL_GPL(hda_extra_out_badness);
352f7f91 1294
7385df61
TI
1295/* get the DAC of the primary output corresponding to the given array index */
1296static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1297{
1298 struct hda_gen_spec *spec = codec->spec;
1299 struct auto_pin_cfg *cfg = &spec->autocfg;
1300
1301 if (cfg->line_outs > idx)
1302 return spec->private_dac_nids[idx];
1303 idx -= cfg->line_outs;
1304 if (spec->multi_ios > idx)
1305 return spec->multi_io[idx].dac;
1306 return 0;
1307}
1308
1309/* return the DAC if it's reachable, otherwise zero */
1310static inline hda_nid_t try_dac(struct hda_codec *codec,
1311 hda_nid_t dac, hda_nid_t pin)
1312{
1313 return is_reachable_path(codec, dac, pin) ? dac : 0;
1314}
1315
352f7f91
TI
1316/* try to assign DACs to pins and return the resultant badness */
1317static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1318 const hda_nid_t *pins, hda_nid_t *dacs,
196c1766 1319 int *path_idx,
352f7f91
TI
1320 const struct badness_table *bad)
1321{
1322 struct hda_gen_spec *spec = codec->spec;
352f7f91
TI
1323 int i, j;
1324 int badness = 0;
1325 hda_nid_t dac;
1326
1327 if (!num_outs)
1328 return 0;
1329
1330 for (i = 0; i < num_outs; i++) {
0c8c0f56 1331 struct nid_path *path;
352f7f91 1332 hda_nid_t pin = pins[i];
1e0b5286 1333
0e614dd0
TI
1334 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1335 if (path) {
1336 badness += assign_out_path_ctls(codec, path);
1e0b5286
TI
1337 continue;
1338 }
1339
3690739b
TI
1340 dacs[i] = get_preferred_dac(codec, pin);
1341 if (dacs[i]) {
1342 if (is_dac_already_used(codec, dacs[i]))
1343 badness += bad->shared_primary;
1344 }
1345
1346 if (!dacs[i])
1347 dacs[i] = look_for_dac(codec, pin, false);
352f7f91 1348 if (!dacs[i] && !i) {
980428ce 1349 /* try to steal the DAC of surrounds for the front */
352f7f91
TI
1350 for (j = 1; j < num_outs; j++) {
1351 if (is_reachable_path(codec, dacs[j], pin)) {
1352 dacs[0] = dacs[j];
1353 dacs[j] = 0;
980428ce 1354 invalidate_nid_path(codec, path_idx[j]);
196c1766 1355 path_idx[j] = 0;
352f7f91
TI
1356 break;
1357 }
1358 }
071c73ad 1359 }
352f7f91
TI
1360 dac = dacs[i];
1361 if (!dac) {
7385df61
TI
1362 if (num_outs > 2)
1363 dac = try_dac(codec, get_primary_out(codec, i), pin);
1364 if (!dac)
1365 dac = try_dac(codec, dacs[0], pin);
1366 if (!dac)
1367 dac = try_dac(codec, get_primary_out(codec, i), pin);
352f7f91
TI
1368 if (dac) {
1369 if (!i)
1370 badness += bad->shared_primary;
1371 else if (i == 1)
1372 badness += bad->shared_surr;
1373 else
1374 badness += bad->shared_clfe;
1375 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1376 dac = spec->private_dac_nids[0];
1377 badness += bad->shared_surr_main;
1378 } else if (!i)
1379 badness += bad->no_primary_dac;
1380 else
1381 badness += bad->no_dac;
1da177e4 1382 }
1fa335b0
TI
1383 if (!dac)
1384 continue;
3ca529d3 1385 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
117688a9 1386 if (!path && !i && spec->mixer_nid) {
b3a8c745 1387 /* try with aamix */
3ca529d3 1388 path = snd_hda_add_new_path(codec, dac, pin, 0);
b3a8c745 1389 }
1fa335b0 1390 if (!path) {
352f7f91 1391 dac = dacs[i] = 0;
1fa335b0
TI
1392 badness += bad->no_dac;
1393 } else {
4e76a883 1394 /* print_nid_path(codec, "output", path); */
e1284af7 1395 path->active = true;
196c1766 1396 path_idx[i] = snd_hda_get_path_idx(codec, path);
0e614dd0 1397 badness += assign_out_path_ctls(codec, path);
e1284af7 1398 }
1da177e4
LT
1399 }
1400
352f7f91 1401 return badness;
1da177e4
LT
1402}
1403
352f7f91
TI
1404/* return NID if the given pin has only a single connection to a certain DAC */
1405static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1da177e4 1406{
352f7f91
TI
1407 struct hda_gen_spec *spec = codec->spec;
1408 int i;
1409 hda_nid_t nid_found = 0;
1da177e4 1410
352f7f91
TI
1411 for (i = 0; i < spec->num_all_dacs; i++) {
1412 hda_nid_t nid = spec->all_dacs[i];
1413 if (!nid || is_dac_already_used(codec, nid))
1414 continue;
1415 if (is_reachable_path(codec, nid, pin)) {
1416 if (nid_found)
1da177e4 1417 return 0;
352f7f91 1418 nid_found = nid;
1da177e4
LT
1419 }
1420 }
352f7f91 1421 return nid_found;
1da177e4
LT
1422}
1423
352f7f91
TI
1424/* check whether the given pin can be a multi-io pin */
1425static bool can_be_multiio_pin(struct hda_codec *codec,
1426 unsigned int location, hda_nid_t nid)
cb53c626 1427{
352f7f91
TI
1428 unsigned int defcfg, caps;
1429
1430 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1431 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1432 return false;
1433 if (location && get_defcfg_location(defcfg) != location)
1434 return false;
1435 caps = snd_hda_query_pin_caps(codec, nid);
1436 if (!(caps & AC_PINCAP_OUT))
1437 return false;
1438 return true;
cb53c626 1439}
cb53c626 1440
e22aab7d
TI
1441/* count the number of input pins that are capable to be multi-io */
1442static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1443{
1444 struct hda_gen_spec *spec = codec->spec;
1445 struct auto_pin_cfg *cfg = &spec->autocfg;
1446 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1447 unsigned int location = get_defcfg_location(defcfg);
1448 int type, i;
1449 int num_pins = 0;
1450
1451 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1452 for (i = 0; i < cfg->num_inputs; i++) {
1453 if (cfg->inputs[i].type != type)
1454 continue;
1455 if (can_be_multiio_pin(codec, location,
1456 cfg->inputs[i].pin))
1457 num_pins++;
1458 }
1459 }
1460 return num_pins;
1461}
1462
1da177e4 1463/*
352f7f91
TI
1464 * multi-io helper
1465 *
1466 * When hardwired is set, try to fill ony hardwired pins, and returns
1467 * zero if any pins are filled, non-zero if nothing found.
1468 * When hardwired is off, try to fill possible input pins, and returns
1469 * the badness value.
1da177e4 1470 */
352f7f91
TI
1471static int fill_multi_ios(struct hda_codec *codec,
1472 hda_nid_t reference_pin,
e22aab7d 1473 bool hardwired)
1da177e4 1474{
352f7f91
TI
1475 struct hda_gen_spec *spec = codec->spec;
1476 struct auto_pin_cfg *cfg = &spec->autocfg;
e22aab7d 1477 int type, i, j, num_pins, old_pins;
352f7f91
TI
1478 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1479 unsigned int location = get_defcfg_location(defcfg);
1480 int badness = 0;
0e614dd0 1481 struct nid_path *path;
352f7f91
TI
1482
1483 old_pins = spec->multi_ios;
1484 if (old_pins >= 2)
1485 goto end_fill;
1486
e22aab7d 1487 num_pins = count_multiio_pins(codec, reference_pin);
352f7f91
TI
1488 if (num_pins < 2)
1489 goto end_fill;
1da177e4 1490
352f7f91
TI
1491 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1492 for (i = 0; i < cfg->num_inputs; i++) {
1493 hda_nid_t nid = cfg->inputs[i].pin;
1494 hda_nid_t dac = 0;
1da177e4 1495
352f7f91
TI
1496 if (cfg->inputs[i].type != type)
1497 continue;
1498 if (!can_be_multiio_pin(codec, location, nid))
1499 continue;
1500 for (j = 0; j < spec->multi_ios; j++) {
1501 if (nid == spec->multi_io[j].pin)
1502 break;
1503 }
1504 if (j < spec->multi_ios)
1505 continue;
1506
352f7f91
TI
1507 if (hardwired)
1508 dac = get_dac_if_single(codec, nid);
1509 else if (!dac)
1510 dac = look_for_dac(codec, nid, false);
1511 if (!dac) {
1512 badness++;
1513 continue;
1514 }
3ca529d3
TI
1515 path = snd_hda_add_new_path(codec, dac, nid,
1516 -spec->mixer_nid);
0c8c0f56 1517 if (!path) {
352f7f91
TI
1518 badness++;
1519 continue;
1520 }
4e76a883 1521 /* print_nid_path(codec, "multiio", path); */
352f7f91
TI
1522 spec->multi_io[spec->multi_ios].pin = nid;
1523 spec->multi_io[spec->multi_ios].dac = dac;
196c1766
TI
1524 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1525 snd_hda_get_path_idx(codec, path);
352f7f91
TI
1526 spec->multi_ios++;
1527 if (spec->multi_ios >= 2)
1528 break;
1529 }
1530 }
1531 end_fill:
1532 if (badness)
1533 badness = BAD_MULTI_IO;
1534 if (old_pins == spec->multi_ios) {
1535 if (hardwired)
1536 return 1; /* nothing found */
1537 else
1538 return badness; /* no badness if nothing found */
1539 }
1540 if (!hardwired && spec->multi_ios < 2) {
1541 /* cancel newly assigned paths */
1542 spec->paths.used -= spec->multi_ios - old_pins;
1543 spec->multi_ios = old_pins;
1544 return badness;
1545 }
1546
1547 /* assign volume and mute controls */
0e614dd0
TI
1548 for (i = old_pins; i < spec->multi_ios; i++) {
1549 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1550 badness += assign_out_path_ctls(codec, path);
1551 }
352f7f91
TI
1552
1553 return badness;
1554}
1555
1556/* map DACs for all pins in the list if they are single connections */
1557static bool map_singles(struct hda_codec *codec, int outs,
196c1766 1558 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
352f7f91 1559{
b3a8c745 1560 struct hda_gen_spec *spec = codec->spec;
352f7f91
TI
1561 int i;
1562 bool found = false;
1563 for (i = 0; i < outs; i++) {
0c8c0f56 1564 struct nid_path *path;
352f7f91
TI
1565 hda_nid_t dac;
1566 if (dacs[i])
1567 continue;
1568 dac = get_dac_if_single(codec, pins[i]);
1569 if (!dac)
1570 continue;
3ca529d3
TI
1571 path = snd_hda_add_new_path(codec, dac, pins[i],
1572 -spec->mixer_nid);
117688a9 1573 if (!path && !i && spec->mixer_nid)
3ca529d3 1574 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
0c8c0f56 1575 if (path) {
352f7f91
TI
1576 dacs[i] = dac;
1577 found = true;
4e76a883 1578 /* print_nid_path(codec, "output", path); */
e1284af7 1579 path->active = true;
196c1766 1580 path_idx[i] = snd_hda_get_path_idx(codec, path);
352f7f91
TI
1581 }
1582 }
1583 return found;
1584}
1585
c30aa7b2
TI
1586/* create a new path including aamix if available, and return its index */
1587static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1588{
3ca529d3 1589 struct hda_gen_spec *spec = codec->spec;
c30aa7b2 1590 struct nid_path *path;
5ead56f2 1591 hda_nid_t path_dac, dac, pin;
c30aa7b2
TI
1592
1593 path = snd_hda_get_path_from_idx(codec, path_idx);
3ca529d3
TI
1594 if (!path || !path->depth ||
1595 is_nid_contained(path, spec->mixer_nid))
c30aa7b2 1596 return 0;
5ead56f2
TI
1597 path_dac = path->path[0];
1598 dac = spec->private_dac_nids[0];
f87498b6
TI
1599 pin = path->path[path->depth - 1];
1600 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1601 if (!path) {
5ead56f2
TI
1602 if (dac != path_dac)
1603 dac = path_dac;
f87498b6
TI
1604 else if (spec->multiout.hp_out_nid[0])
1605 dac = spec->multiout.hp_out_nid[0];
1606 else if (spec->multiout.extra_out_nid[0])
1607 dac = spec->multiout.extra_out_nid[0];
5ead56f2
TI
1608 else
1609 dac = 0;
f87498b6
TI
1610 if (dac)
1611 path = snd_hda_add_new_path(codec, dac, pin,
1612 spec->mixer_nid);
1613 }
c30aa7b2
TI
1614 if (!path)
1615 return 0;
4e76a883 1616 /* print_nid_path(codec, "output-aamix", path); */
c30aa7b2 1617 path->active = false; /* unused as default */
6b275b14 1618 path->pin_fixed = true; /* static route */
c30aa7b2
TI
1619 return snd_hda_get_path_idx(codec, path);
1620}
1621
55a63d4d
TI
1622/* check whether the independent HP is available with the current config */
1623static bool indep_hp_possible(struct hda_codec *codec)
1624{
1625 struct hda_gen_spec *spec = codec->spec;
1626 struct auto_pin_cfg *cfg = &spec->autocfg;
1627 struct nid_path *path;
1628 int i, idx;
1629
1630 if (cfg->line_out_type == AUTO_PIN_HP_OUT)
1631 idx = spec->out_paths[0];
1632 else
1633 idx = spec->hp_paths[0];
1634 path = snd_hda_get_path_from_idx(codec, idx);
1635 if (!path)
1636 return false;
1637
1638 /* assume no path conflicts unless aamix is involved */
1639 if (!spec->mixer_nid || !is_nid_contained(path, spec->mixer_nid))
1640 return true;
1641
1642 /* check whether output paths contain aamix */
1643 for (i = 0; i < cfg->line_outs; i++) {
1644 if (spec->out_paths[i] == idx)
1645 break;
1646 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1647 if (path && is_nid_contained(path, spec->mixer_nid))
1648 return false;
1649 }
1650 for (i = 0; i < cfg->speaker_outs; i++) {
1651 path = snd_hda_get_path_from_idx(codec, spec->speaker_paths[i]);
1652 if (path && is_nid_contained(path, spec->mixer_nid))
1653 return false;
1654 }
1655
1656 return true;
1657}
1658
a07a949b
TI
1659/* fill the empty entries in the dac array for speaker/hp with the
1660 * shared dac pointed by the paths
1661 */
1662static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1663 hda_nid_t *dacs, int *path_idx)
1664{
1665 struct nid_path *path;
1666 int i;
1667
1668 for (i = 0; i < num_outs; i++) {
1669 if (dacs[i])
1670 continue;
1671 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1672 if (!path)
1673 continue;
1674 dacs[i] = path->path[0];
1675 }
1676}
1677
352f7f91
TI
1678/* fill in the dac_nids table from the parsed pin configuration */
1679static int fill_and_eval_dacs(struct hda_codec *codec,
1680 bool fill_hardwired,
1681 bool fill_mio_first)
1682{
1683 struct hda_gen_spec *spec = codec->spec;
1684 struct auto_pin_cfg *cfg = &spec->autocfg;
1685 int i, err, badness;
1686
1687 /* set num_dacs once to full for look_for_dac() */
1688 spec->multiout.num_dacs = cfg->line_outs;
1689 spec->multiout.dac_nids = spec->private_dac_nids;
1690 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1691 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1692 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1693 spec->multi_ios = 0;
1694 snd_array_free(&spec->paths);
cd5be3f9
TI
1695
1696 /* clear path indices */
1697 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1698 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1699 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1700 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1701 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
c697b716 1702 memset(spec->input_paths, 0, sizeof(spec->input_paths));
cd5be3f9
TI
1703 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1704 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1705
352f7f91
TI
1706 badness = 0;
1707
1708 /* fill hard-wired DACs first */
1709 if (fill_hardwired) {
1710 bool mapped;
1711 do {
1712 mapped = map_singles(codec, cfg->line_outs,
1713 cfg->line_out_pins,
196c1766
TI
1714 spec->private_dac_nids,
1715 spec->out_paths);
352f7f91
TI
1716 mapped |= map_singles(codec, cfg->hp_outs,
1717 cfg->hp_pins,
196c1766
TI
1718 spec->multiout.hp_out_nid,
1719 spec->hp_paths);
352f7f91
TI
1720 mapped |= map_singles(codec, cfg->speaker_outs,
1721 cfg->speaker_pins,
196c1766
TI
1722 spec->multiout.extra_out_nid,
1723 spec->speaker_paths);
da96fb5b
TI
1724 if (!spec->no_multi_io &&
1725 fill_mio_first && cfg->line_outs == 1 &&
352f7f91 1726 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
e22aab7d 1727 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
352f7f91
TI
1728 if (!err)
1729 mapped = true;
1730 }
1731 } while (mapped);
1732 }
1733
1734 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
196c1766 1735 spec->private_dac_nids, spec->out_paths,
98bd1115 1736 spec->main_out_badness);
352f7f91 1737
da96fb5b 1738 if (!spec->no_multi_io && fill_mio_first &&
352f7f91
TI
1739 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1740 /* try to fill multi-io first */
e22aab7d 1741 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
352f7f91
TI
1742 if (err < 0)
1743 return err;
1744 /* we don't count badness at this stage yet */
1745 }
1746
1747 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1748 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1749 spec->multiout.hp_out_nid,
196c1766 1750 spec->hp_paths,
98bd1115 1751 spec->extra_out_badness);
352f7f91
TI
1752 if (err < 0)
1753 return err;
1754 badness += err;
1755 }
1756 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1757 err = try_assign_dacs(codec, cfg->speaker_outs,
1758 cfg->speaker_pins,
1759 spec->multiout.extra_out_nid,
196c1766 1760 spec->speaker_paths,
98bd1115 1761 spec->extra_out_badness);
352f7f91
TI
1762 if (err < 0)
1763 return err;
1764 badness += err;
1765 }
da96fb5b
TI
1766 if (!spec->no_multi_io &&
1767 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
e22aab7d 1768 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
352f7f91
TI
1769 if (err < 0)
1770 return err;
1771 badness += err;
1772 }
1773
c30aa7b2
TI
1774 if (spec->mixer_nid) {
1775 spec->aamix_out_paths[0] =
1776 check_aamix_out_path(codec, spec->out_paths[0]);
1777 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1778 spec->aamix_out_paths[1] =
1779 check_aamix_out_path(codec, spec->hp_paths[0]);
1780 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1781 spec->aamix_out_paths[2] =
1782 check_aamix_out_path(codec, spec->speaker_paths[0]);
1783 }
1784
da96fb5b
TI
1785 if (!spec->no_multi_io &&
1786 cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
e22aab7d
TI
1787 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1788 spec->multi_ios = 1; /* give badness */
1789
a07a949b
TI
1790 /* re-count num_dacs and squash invalid entries */
1791 spec->multiout.num_dacs = 0;
1792 for (i = 0; i < cfg->line_outs; i++) {
1793 if (spec->private_dac_nids[i])
1794 spec->multiout.num_dacs++;
1795 else {
1796 memmove(spec->private_dac_nids + i,
1797 spec->private_dac_nids + i + 1,
1798 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1799 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1800 }
1801 }
1802
1803 spec->ext_channel_count = spec->min_channel_count =
c0f3b216 1804 spec->multiout.num_dacs * 2;
a07a949b 1805
352f7f91
TI
1806 if (spec->multi_ios == 2) {
1807 for (i = 0; i < 2; i++)
1808 spec->private_dac_nids[spec->multiout.num_dacs++] =
1809 spec->multi_io[i].dac;
352f7f91
TI
1810 } else if (spec->multi_ios) {
1811 spec->multi_ios = 0;
1812 badness += BAD_MULTI_IO;
1813 }
1814
55a63d4d
TI
1815 if (spec->indep_hp && !indep_hp_possible(codec))
1816 badness += BAD_NO_INDEP_HP;
1817
a07a949b
TI
1818 /* re-fill the shared DAC for speaker / headphone */
1819 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1820 refill_shared_dacs(codec, cfg->hp_outs,
1821 spec->multiout.hp_out_nid,
1822 spec->hp_paths);
1823 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1824 refill_shared_dacs(codec, cfg->speaker_outs,
1825 spec->multiout.extra_out_nid,
1826 spec->speaker_paths);
1827
352f7f91
TI
1828 return badness;
1829}
1830
1831#define DEBUG_BADNESS
1832
1833#ifdef DEBUG_BADNESS
d82353e5
JP
1834#define debug_badness(fmt, ...) \
1835 codec_dbg(codec, fmt, ##__VA_ARGS__)
352f7f91 1836#else
d82353e5
JP
1837#define debug_badness(fmt, ...) \
1838 do { if (0) codec_dbg(codec, fmt, ##__VA_ARGS__); } while (0)
352f7f91
TI
1839#endif
1840
a769409c
TI
1841#ifdef DEBUG_BADNESS
1842static inline void print_nid_path_idx(struct hda_codec *codec,
1843 const char *pfx, int idx)
1844{
1845 struct nid_path *path;
1846
1847 path = snd_hda_get_path_from_idx(codec, idx);
1848 if (path)
4e76a883 1849 print_nid_path(codec, pfx, path);
a769409c
TI
1850}
1851
1852static void debug_show_configs(struct hda_codec *codec,
1853 struct auto_pin_cfg *cfg)
352f7f91 1854{
a769409c 1855 struct hda_gen_spec *spec = codec->spec;
a769409c 1856 static const char * const lo_type[3] = { "LO", "SP", "HP" };
a769409c
TI
1857 int i;
1858
1859 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
352f7f91 1860 cfg->line_out_pins[0], cfg->line_out_pins[1],
708122e8 1861 cfg->line_out_pins[2], cfg->line_out_pins[3],
352f7f91
TI
1862 spec->multiout.dac_nids[0],
1863 spec->multiout.dac_nids[1],
1864 spec->multiout.dac_nids[2],
a769409c
TI
1865 spec->multiout.dac_nids[3],
1866 lo_type[cfg->line_out_type]);
1867 for (i = 0; i < cfg->line_outs; i++)
1868 print_nid_path_idx(codec, " out", spec->out_paths[i]);
352f7f91
TI
1869 if (spec->multi_ios > 0)
1870 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1871 spec->multi_ios,
1872 spec->multi_io[0].pin, spec->multi_io[1].pin,
1873 spec->multi_io[0].dac, spec->multi_io[1].dac);
a769409c
TI
1874 for (i = 0; i < spec->multi_ios; i++)
1875 print_nid_path_idx(codec, " mio",
1876 spec->out_paths[cfg->line_outs + i]);
1877 if (cfg->hp_outs)
1878 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
352f7f91 1879 cfg->hp_pins[0], cfg->hp_pins[1],
708122e8 1880 cfg->hp_pins[2], cfg->hp_pins[3],
352f7f91
TI
1881 spec->multiout.hp_out_nid[0],
1882 spec->multiout.hp_out_nid[1],
1883 spec->multiout.hp_out_nid[2],
1884 spec->multiout.hp_out_nid[3]);
a769409c
TI
1885 for (i = 0; i < cfg->hp_outs; i++)
1886 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1887 if (cfg->speaker_outs)
1888 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
352f7f91
TI
1889 cfg->speaker_pins[0], cfg->speaker_pins[1],
1890 cfg->speaker_pins[2], cfg->speaker_pins[3],
1891 spec->multiout.extra_out_nid[0],
1892 spec->multiout.extra_out_nid[1],
1893 spec->multiout.extra_out_nid[2],
1894 spec->multiout.extra_out_nid[3]);
a769409c
TI
1895 for (i = 0; i < cfg->speaker_outs; i++)
1896 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1897 for (i = 0; i < 3; i++)
1898 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
352f7f91 1899}
a769409c
TI
1900#else
1901#define debug_show_configs(codec, cfg) /* NOP */
1902#endif
352f7f91
TI
1903
1904/* find all available DACs of the codec */
1905static void fill_all_dac_nids(struct hda_codec *codec)
1906{
1907 struct hda_gen_spec *spec = codec->spec;
7639a06c 1908 hda_nid_t nid;
352f7f91
TI
1909
1910 spec->num_all_dacs = 0;
1911 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
7639a06c 1912 for_each_hda_codec_node(nid, codec) {
352f7f91
TI
1913 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1914 continue;
1915 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
4e76a883 1916 codec_err(codec, "Too many DACs!\n");
352f7f91
TI
1917 break;
1918 }
1919 spec->all_dacs[spec->num_all_dacs++] = nid;
1920 }
1921}
1922
1923static int parse_output_paths(struct hda_codec *codec)
1924{
1925 struct hda_gen_spec *spec = codec->spec;
1926 struct auto_pin_cfg *cfg = &spec->autocfg;
1927 struct auto_pin_cfg *best_cfg;
9314a581 1928 unsigned int val;
352f7f91
TI
1929 int best_badness = INT_MAX;
1930 int badness;
1931 bool fill_hardwired = true, fill_mio_first = true;
1932 bool best_wired = true, best_mio = true;
1933 bool hp_spk_swapped = false;
1934
352f7f91
TI
1935 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1936 if (!best_cfg)
1937 return -ENOMEM;
1938 *best_cfg = *cfg;
1939
1940 for (;;) {
1941 badness = fill_and_eval_dacs(codec, fill_hardwired,
1942 fill_mio_first);
1943 if (badness < 0) {
1944 kfree(best_cfg);
1945 return badness;
1946 }
1947 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1948 cfg->line_out_type, fill_hardwired, fill_mio_first,
1949 badness);
a769409c 1950 debug_show_configs(codec, cfg);
352f7f91
TI
1951 if (badness < best_badness) {
1952 best_badness = badness;
1953 *best_cfg = *cfg;
1954 best_wired = fill_hardwired;
1955 best_mio = fill_mio_first;
1956 }
1957 if (!badness)
1958 break;
1959 fill_mio_first = !fill_mio_first;
1960 if (!fill_mio_first)
1961 continue;
1962 fill_hardwired = !fill_hardwired;
1963 if (!fill_hardwired)
1964 continue;
1965 if (hp_spk_swapped)
1966 break;
1967 hp_spk_swapped = true;
1968 if (cfg->speaker_outs > 0 &&
1969 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1970 cfg->hp_outs = cfg->line_outs;
1971 memcpy(cfg->hp_pins, cfg->line_out_pins,
1972 sizeof(cfg->hp_pins));
1973 cfg->line_outs = cfg->speaker_outs;
1974 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1975 sizeof(cfg->speaker_pins));
1976 cfg->speaker_outs = 0;
1977 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1978 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1979 fill_hardwired = true;
1980 continue;
1981 }
1982 if (cfg->hp_outs > 0 &&
1983 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1984 cfg->speaker_outs = cfg->line_outs;
1985 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1986 sizeof(cfg->speaker_pins));
1987 cfg->line_outs = cfg->hp_outs;
1988 memcpy(cfg->line_out_pins, cfg->hp_pins,
1989 sizeof(cfg->hp_pins));
1990 cfg->hp_outs = 0;
1991 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1992 cfg->line_out_type = AUTO_PIN_HP_OUT;
1993 fill_hardwired = true;
1994 continue;
1995 }
1996 break;
1997 }
1998
1999 if (badness) {
0c8c0f56 2000 debug_badness("==> restoring best_cfg\n");
352f7f91
TI
2001 *cfg = *best_cfg;
2002 fill_and_eval_dacs(codec, best_wired, best_mio);
2003 }
2004 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
2005 cfg->line_out_type, best_wired, best_mio);
a769409c 2006 debug_show_configs(codec, cfg);
352f7f91
TI
2007
2008 if (cfg->line_out_pins[0]) {
2009 struct nid_path *path;
196c1766 2010 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
352f7f91
TI
2011 if (path)
2012 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
d89c6c0c 2013 if (spec->vmaster_nid) {
7a71bbf3
TI
2014 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
2015 HDA_OUTPUT, spec->vmaster_tlv);
d89c6c0c
TI
2016 if (spec->dac_min_mute)
2017 spec->vmaster_tlv[3] |= TLV_DB_SCALE_MUTE;
2018 }
352f7f91
TI
2019 }
2020
9314a581
TI
2021 /* set initial pinctl targets */
2022 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
2023 val = PIN_HP;
2024 else
2025 val = PIN_OUT;
2026 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
2027 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
2028 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
2029 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
2030 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
2031 set_pin_targets(codec, cfg->speaker_outs,
2032 cfg->speaker_pins, val);
2033 }
2034
55a63d4d
TI
2035 /* clear indep_hp flag if not available */
2036 if (spec->indep_hp && !indep_hp_possible(codec))
2037 spec->indep_hp = 0;
2038
352f7f91
TI
2039 kfree(best_cfg);
2040 return 0;
2041}
2042
2043/* add playback controls from the parsed DAC table */
2044static int create_multi_out_ctls(struct hda_codec *codec,
2045 const struct auto_pin_cfg *cfg)
2046{
2047 struct hda_gen_spec *spec = codec->spec;
2048 int i, err, noutputs;
2049
2050 noutputs = cfg->line_outs;
2051 if (spec->multi_ios > 0 && cfg->line_outs < 3)
2052 noutputs += spec->multi_ios;
2053
2054 for (i = 0; i < noutputs; i++) {
2055 const char *name;
2056 int index;
352f7f91
TI
2057 struct nid_path *path;
2058
196c1766 2059 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
352f7f91
TI
2060 if (!path)
2061 continue;
247d85ee
TI
2062
2063 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
352f7f91
TI
2064 if (!name || !strcmp(name, "CLFE")) {
2065 /* Center/LFE */
2066 err = add_vol_ctl(codec, "Center", 0, 1, path);
2067 if (err < 0)
2068 return err;
2069 err = add_vol_ctl(codec, "LFE", 0, 2, path);
2070 if (err < 0)
2071 return err;
247d85ee
TI
2072 } else {
2073 err = add_stereo_vol(codec, name, index, path);
2074 if (err < 0)
2075 return err;
2076 }
2077
2078 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
2079 if (!name || !strcmp(name, "CLFE")) {
352f7f91
TI
2080 err = add_sw_ctl(codec, "Center", 0, 1, path);
2081 if (err < 0)
2082 return err;
2083 err = add_sw_ctl(codec, "LFE", 0, 2, path);
2084 if (err < 0)
2085 return err;
2086 } else {
352f7f91
TI
2087 err = add_stereo_sw(codec, name, index, path);
2088 if (err < 0)
2089 return err;
2090 }
2091 }
2092 return 0;
2093}
2094
c2c80383 2095static int create_extra_out(struct hda_codec *codec, int path_idx,
196c1766 2096 const char *pfx, int cidx)
352f7f91
TI
2097{
2098 struct nid_path *path;
2099 int err;
2100
196c1766 2101 path = snd_hda_get_path_from_idx(codec, path_idx);
352f7f91
TI
2102 if (!path)
2103 return 0;
c2c80383
TI
2104 err = add_stereo_vol(codec, pfx, cidx, path);
2105 if (err < 0)
2106 return err;
352f7f91
TI
2107 err = add_stereo_sw(codec, pfx, cidx, path);
2108 if (err < 0)
2109 return err;
2110 return 0;
2111}
2112
2113/* add playback controls for speaker and HP outputs */
2114static int create_extra_outs(struct hda_codec *codec, int num_pins,
196c1766 2115 const int *paths, const char *pfx)
352f7f91 2116{
c2c80383 2117 int i;
352f7f91
TI
2118
2119 for (i = 0; i < num_pins; i++) {
c2c80383 2120 const char *name;
975cc02a 2121 char tmp[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
c2c80383
TI
2122 int err, idx = 0;
2123
2124 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
2125 name = "Bass Speaker";
2126 else if (num_pins >= 3) {
2127 snprintf(tmp, sizeof(tmp), "%s %s",
352f7f91 2128 pfx, channel_name[i]);
c2c80383 2129 name = tmp;
352f7f91 2130 } else {
c2c80383
TI
2131 name = pfx;
2132 idx = i;
352f7f91 2133 }
c2c80383 2134 err = create_extra_out(codec, paths[i], name, idx);
352f7f91
TI
2135 if (err < 0)
2136 return err;
2137 }
2138 return 0;
2139}
2140
2141static int create_hp_out_ctls(struct hda_codec *codec)
2142{
2143 struct hda_gen_spec *spec = codec->spec;
2144 return create_extra_outs(codec, spec->autocfg.hp_outs,
196c1766 2145 spec->hp_paths,
352f7f91
TI
2146 "Headphone");
2147}
2148
2149static int create_speaker_out_ctls(struct hda_codec *codec)
2150{
2151 struct hda_gen_spec *spec = codec->spec;
2152 return create_extra_outs(codec, spec->autocfg.speaker_outs,
196c1766 2153 spec->speaker_paths,
352f7f91
TI
2154 "Speaker");
2155}
2156
38cf6f1a
TI
2157/*
2158 * independent HP controls
2159 */
2160
1a4f69d5
TI
2161static void call_hp_automute(struct hda_codec *codec,
2162 struct hda_jack_callback *jack);
38cf6f1a
TI
2163static int indep_hp_info(struct snd_kcontrol *kcontrol,
2164 struct snd_ctl_elem_info *uinfo)
2165{
2166 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2167}
2168
2169static int indep_hp_get(struct snd_kcontrol *kcontrol,
2170 struct snd_ctl_elem_value *ucontrol)
2171{
2172 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2173 struct hda_gen_spec *spec = codec->spec;
2174 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
2175 return 0;
2176}
2177
a1e908ed
TI
2178static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2179 int nomix_path_idx, int mix_path_idx,
2180 int out_type);
2181
38cf6f1a
TI
2182static int indep_hp_put(struct snd_kcontrol *kcontrol,
2183 struct snd_ctl_elem_value *ucontrol)
2184{
2185 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2186 struct hda_gen_spec *spec = codec->spec;
2187 unsigned int select = ucontrol->value.enumerated.item[0];
2188 int ret = 0;
2189
2190 mutex_lock(&spec->pcm_mutex);
2191 if (spec->active_streams) {
2192 ret = -EBUSY;
2193 goto unlock;
2194 }
2195
2196 if (spec->indep_hp_enabled != select) {
a1e908ed
TI
2197 hda_nid_t *dacp;
2198 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2199 dacp = &spec->private_dac_nids[0];
2200 else
2201 dacp = &spec->multiout.hp_out_nid[0];
2202
2203 /* update HP aamix paths in case it conflicts with indep HP */
2204 if (spec->have_aamix_ctl) {
2205 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2206 update_aamix_paths(codec, spec->aamix_mode,
2207 spec->out_paths[0],
2208 spec->aamix_out_paths[0],
2209 spec->autocfg.line_out_type);
2210 else
2211 update_aamix_paths(codec, spec->aamix_mode,
2212 spec->hp_paths[0],
2213 spec->aamix_out_paths[1],
2214 AUTO_PIN_HP_OUT);
2215 }
2216
38cf6f1a
TI
2217 spec->indep_hp_enabled = select;
2218 if (spec->indep_hp_enabled)
a1e908ed 2219 *dacp = 0;
38cf6f1a 2220 else
a1e908ed 2221 *dacp = spec->alt_dac_nid;
92603c59 2222
963afde9 2223 call_hp_automute(codec, NULL);
38cf6f1a
TI
2224 ret = 1;
2225 }
2226 unlock:
2227 mutex_unlock(&spec->pcm_mutex);
2228 return ret;
2229}
2230
2231static const struct snd_kcontrol_new indep_hp_ctl = {
2232 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2233 .name = "Independent HP",
2234 .info = indep_hp_info,
2235 .get = indep_hp_get,
2236 .put = indep_hp_put,
2237};
2238
2239
2240static int create_indep_hp_ctls(struct hda_codec *codec)
2241{
2242 struct hda_gen_spec *spec = codec->spec;
a1e908ed 2243 hda_nid_t dac;
38cf6f1a
TI
2244
2245 if (!spec->indep_hp)
2246 return 0;
a1e908ed
TI
2247 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2248 dac = spec->multiout.dac_nids[0];
2249 else
2250 dac = spec->multiout.hp_out_nid[0];
2251 if (!dac) {
38cf6f1a
TI
2252 spec->indep_hp = 0;
2253 return 0;
2254 }
2255
2256 spec->indep_hp_enabled = false;
a1e908ed 2257 spec->alt_dac_nid = dac;
38cf6f1a
TI
2258 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
2259 return -ENOMEM;
2260 return 0;
2261}
2262
352f7f91
TI
2263/*
2264 * channel mode enum control
2265 */
2266
2267static int ch_mode_info(struct snd_kcontrol *kcontrol,
2268 struct snd_ctl_elem_info *uinfo)
2269{
2270 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2271 struct hda_gen_spec *spec = codec->spec;
a07a949b 2272 int chs;
352f7f91
TI
2273
2274 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2275 uinfo->count = 1;
2276 uinfo->value.enumerated.items = spec->multi_ios + 1;
2277 if (uinfo->value.enumerated.item > spec->multi_ios)
2278 uinfo->value.enumerated.item = spec->multi_ios;
a07a949b
TI
2279 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2280 sprintf(uinfo->value.enumerated.name, "%dch", chs);
352f7f91
TI
2281 return 0;
2282}
2283
2284static int ch_mode_get(struct snd_kcontrol *kcontrol,
2285 struct snd_ctl_elem_value *ucontrol)
2286{
2287 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2288 struct hda_gen_spec *spec = codec->spec;
a07a949b
TI
2289 ucontrol->value.enumerated.item[0] =
2290 (spec->ext_channel_count - spec->min_channel_count) / 2;
352f7f91
TI
2291 return 0;
2292}
2293
196c1766
TI
2294static inline struct nid_path *
2295get_multiio_path(struct hda_codec *codec, int idx)
2296{
2297 struct hda_gen_spec *spec = codec->spec;
2298 return snd_hda_get_path_from_idx(codec,
2299 spec->out_paths[spec->autocfg.line_outs + idx]);
2300}
2301
a5cc2509
TI
2302static void update_automute_all(struct hda_codec *codec);
2303
65033cc8
TI
2304/* Default value to be passed as aamix argument for snd_hda_activate_path();
2305 * used for output paths
2306 */
2307static bool aamix_default(struct hda_gen_spec *spec)
2308{
2309 return !spec->have_aamix_ctl || spec->aamix_mode;
2310}
2311
352f7f91
TI
2312static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2313{
2314 struct hda_gen_spec *spec = codec->spec;
2315 hda_nid_t nid = spec->multi_io[idx].pin;
2316 struct nid_path *path;
2317
196c1766 2318 path = get_multiio_path(codec, idx);
352f7f91
TI
2319 if (!path)
2320 return -EINVAL;
2321
2322 if (path->active == output)
2323 return 0;
2324
2325 if (output) {
2c12c30d 2326 set_pin_target(codec, nid, PIN_OUT, true);
65033cc8 2327 snd_hda_activate_path(codec, path, true, aamix_default(spec));
d5a9f1bb 2328 set_pin_eapd(codec, nid, true);
352f7f91 2329 } else {
d5a9f1bb 2330 set_pin_eapd(codec, nid, false);
65033cc8 2331 snd_hda_activate_path(codec, path, false, aamix_default(spec));
2c12c30d 2332 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
55196fff 2333 path_power_down_sync(codec, path);
352f7f91 2334 }
a365fed9
TI
2335
2336 /* update jack retasking in case it modifies any of them */
a5cc2509 2337 update_automute_all(codec);
a365fed9 2338
352f7f91
TI
2339 return 0;
2340}
2341
2342static int ch_mode_put(struct snd_kcontrol *kcontrol,
2343 struct snd_ctl_elem_value *ucontrol)
2344{
2345 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2346 struct hda_gen_spec *spec = codec->spec;
2347 int i, ch;
2348
2349 ch = ucontrol->value.enumerated.item[0];
2350 if (ch < 0 || ch > spec->multi_ios)
2351 return -EINVAL;
a07a949b 2352 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
352f7f91 2353 return 0;
a07a949b 2354 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
352f7f91
TI
2355 for (i = 0; i < spec->multi_ios; i++)
2356 set_multi_io(codec, i, i < ch);
2357 spec->multiout.max_channels = max(spec->ext_channel_count,
2358 spec->const_channel_count);
2359 if (spec->need_dac_fix)
2360 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
2361 return 1;
2362}
2363
2364static const struct snd_kcontrol_new channel_mode_enum = {
2365 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2366 .name = "Channel Mode",
2367 .info = ch_mode_info,
2368 .get = ch_mode_get,
2369 .put = ch_mode_put,
2370};
2371
2372static int create_multi_channel_mode(struct hda_codec *codec)
2373{
2374 struct hda_gen_spec *spec = codec->spec;
2375
2376 if (spec->multi_ios > 0) {
12c93df6 2377 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
352f7f91
TI
2378 return -ENOMEM;
2379 }
2380 return 0;
2381}
2382
c30aa7b2
TI
2383/*
2384 * aamix loopback enable/disable switch
2385 */
2386
2387#define loopback_mixing_info indep_hp_info
2388
2389static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2390 struct snd_ctl_elem_value *ucontrol)
2391{
2392 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2393 struct hda_gen_spec *spec = codec->spec;
2394 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2395 return 0;
2396}
2397
2398static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
a1e908ed
TI
2399 int nomix_path_idx, int mix_path_idx,
2400 int out_type)
c30aa7b2 2401{
a1e908ed 2402 struct hda_gen_spec *spec = codec->spec;
c30aa7b2
TI
2403 struct nid_path *nomix_path, *mix_path;
2404
2405 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2406 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2407 if (!nomix_path || !mix_path)
2408 return;
a1e908ed
TI
2409
2410 /* if HP aamix path is driven from a different DAC and the
2411 * independent HP mode is ON, can't turn on aamix path
2412 */
2413 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2414 mix_path->path[0] != spec->alt_dac_nid)
2415 do_mix = false;
2416
c30aa7b2
TI
2417 if (do_mix) {
2418 snd_hda_activate_path(codec, nomix_path, false, true);
2419 snd_hda_activate_path(codec, mix_path, true, true);
55196fff 2420 path_power_down_sync(codec, nomix_path);
c30aa7b2 2421 } else {
65033cc8
TI
2422 snd_hda_activate_path(codec, mix_path, false, false);
2423 snd_hda_activate_path(codec, nomix_path, true, false);
55196fff 2424 path_power_down_sync(codec, mix_path);
c30aa7b2
TI
2425 }
2426}
2427
2428static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2429 struct snd_ctl_elem_value *ucontrol)
2430{
2431 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2432 struct hda_gen_spec *spec = codec->spec;
2433 unsigned int val = ucontrol->value.enumerated.item[0];
2434
2435 if (val == spec->aamix_mode)
2436 return 0;
2437 spec->aamix_mode = val;
2438 update_aamix_paths(codec, val, spec->out_paths[0],
a1e908ed
TI
2439 spec->aamix_out_paths[0],
2440 spec->autocfg.line_out_type);
c30aa7b2 2441 update_aamix_paths(codec, val, spec->hp_paths[0],
a1e908ed
TI
2442 spec->aamix_out_paths[1],
2443 AUTO_PIN_HP_OUT);
c30aa7b2 2444 update_aamix_paths(codec, val, spec->speaker_paths[0],
a1e908ed
TI
2445 spec->aamix_out_paths[2],
2446 AUTO_PIN_SPEAKER_OUT);
c30aa7b2
TI
2447 return 1;
2448}
2449
2450static const struct snd_kcontrol_new loopback_mixing_enum = {
2451 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2452 .name = "Loopback Mixing",
2453 .info = loopback_mixing_info,
2454 .get = loopback_mixing_get,
2455 .put = loopback_mixing_put,
2456};
2457
2458static int create_loopback_mixing_ctl(struct hda_codec *codec)
2459{
2460 struct hda_gen_spec *spec = codec->spec;
2461
2462 if (!spec->mixer_nid)
2463 return 0;
2464 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
2465 spec->aamix_out_paths[2]))
2466 return 0;
2467 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2468 return -ENOMEM;
a1e908ed 2469 spec->have_aamix_ctl = 1;
c30aa7b2
TI
2470 return 0;
2471}
2472
352f7f91
TI
2473/*
2474 * shared headphone/mic handling
2475 */
2476
2477static void call_update_outputs(struct hda_codec *codec);
2478
2479/* for shared I/O, change the pin-control accordingly */
967303da 2480static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
352f7f91
TI
2481{
2482 struct hda_gen_spec *spec = codec->spec;
967303da 2483 bool as_mic;
352f7f91 2484 unsigned int val;
967303da 2485 hda_nid_t pin;
352f7f91 2486
967303da
TI
2487 pin = spec->hp_mic_pin;
2488 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
352f7f91 2489
967303da
TI
2490 if (!force) {
2491 val = snd_hda_codec_get_pin_target(codec, pin);
2492 if (as_mic) {
2493 if (val & PIN_IN)
2494 return;
2495 } else {
2496 if (val & PIN_OUT)
2497 return;
2498 }
2499 }
2500
2501 val = snd_hda_get_default_vref(codec, pin);
2502 /* if the HP pin doesn't support VREF and the codec driver gives an
2503 * alternative pin, set up the VREF on that pin instead
2504 */
352f7f91
TI
2505 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2506 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2507 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2508 if (vref_val != AC_PINCTL_VREF_HIZ)
7594aa33 2509 snd_hda_set_pin_ctl_cache(codec, vref_pin,
967303da 2510 PIN_IN | (as_mic ? vref_val : 0));
352f7f91
TI
2511 }
2512
8ba955ce
TI
2513 if (!spec->hp_mic_jack_modes) {
2514 if (as_mic)
2515 val |= PIN_IN;
2516 else
2517 val = PIN_HP;
2518 set_pin_target(codec, pin, val, true);
963afde9 2519 call_hp_automute(codec, NULL);
8ba955ce 2520 }
352f7f91
TI
2521}
2522
2523/* create a shared input with the headphone out */
967303da 2524static int create_hp_mic(struct hda_codec *codec)
352f7f91
TI
2525{
2526 struct hda_gen_spec *spec = codec->spec;
2527 struct auto_pin_cfg *cfg = &spec->autocfg;
2528 unsigned int defcfg;
2529 hda_nid_t nid;
2530
967303da
TI
2531 if (!spec->hp_mic) {
2532 if (spec->suppress_hp_mic_detect)
2533 return 0;
2534 /* automatic detection: only if no input or a single internal
2535 * input pin is found, try to detect the shared hp/mic
2536 */
2537 if (cfg->num_inputs > 1)
2538 return 0;
2539 else if (cfg->num_inputs == 1) {
2540 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2541 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2542 return 0;
2543 }
2544 }
2545
2546 spec->hp_mic = 0; /* clear once */
2547 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
352f7f91
TI
2548 return 0;
2549
967303da
TI
2550 nid = 0;
2551 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2552 nid = cfg->line_out_pins[0];
2553 else if (cfg->hp_outs > 0)
2554 nid = cfg->hp_pins[0];
2555 if (!nid)
2556 return 0;
352f7f91
TI
2557
2558 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2559 return 0; /* no input */
2560
967303da
TI
2561 cfg->inputs[cfg->num_inputs].pin = nid;
2562 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
cb420b11 2563 cfg->inputs[cfg->num_inputs].is_headphone_mic = 1;
967303da
TI
2564 cfg->num_inputs++;
2565 spec->hp_mic = 1;
2566 spec->hp_mic_pin = nid;
2567 /* we can't handle auto-mic together with HP-mic */
2568 spec->suppress_auto_mic = 1;
4e76a883 2569 codec_dbg(codec, "Enable shared I/O jack on NID 0x%x\n", nid);
352f7f91
TI
2570 return 0;
2571}
2572
978e77e7
TI
2573/*
2574 * output jack mode
2575 */
5f171baa
TI
2576
2577static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2578
2579static const char * const out_jack_texts[] = {
2580 "Line Out", "Headphone Out",
2581};
2582
978e77e7
TI
2583static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2584 struct snd_ctl_elem_info *uinfo)
2585{
5f171baa 2586 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
978e77e7
TI
2587}
2588
2589static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2590 struct snd_ctl_elem_value *ucontrol)
2591{
2592 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2593 hda_nid_t nid = kcontrol->private_value;
2594 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2595 ucontrol->value.enumerated.item[0] = 1;
2596 else
2597 ucontrol->value.enumerated.item[0] = 0;
2598 return 0;
2599}
2600
2601static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2602 struct snd_ctl_elem_value *ucontrol)
2603{
2604 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2605 hda_nid_t nid = kcontrol->private_value;
2606 unsigned int val;
2607
2608 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2609 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2610 return 0;
2611 snd_hda_set_pin_ctl_cache(codec, nid, val);
2612 return 1;
2613}
2614
2615static const struct snd_kcontrol_new out_jack_mode_enum = {
2616 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2617 .info = out_jack_mode_info,
2618 .get = out_jack_mode_get,
2619 .put = out_jack_mode_put,
2620};
2621
2622static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2623{
2624 struct hda_gen_spec *spec = codec->spec;
2625 int i;
2626
2627 for (i = 0; i < spec->kctls.used; i++) {
2628 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2629 if (!strcmp(kctl->name, name) && kctl->index == idx)
2630 return true;
2631 }
2632 return false;
2633}
2634
2635static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2636 char *name, size_t name_len)
2637{
2638 struct hda_gen_spec *spec = codec->spec;
2639 int idx = 0;
2640
2641 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2642 strlcat(name, " Jack Mode", name_len);
2643
2644 for (; find_kctl_name(codec, name, idx); idx++)
2645 ;
2646}
2647
5f171baa
TI
2648static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2649{
2650 struct hda_gen_spec *spec = codec->spec;
f811c3cf 2651 if (spec->add_jack_modes) {
5f171baa
TI
2652 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2653 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2654 return 2;
2655 }
2656 return 1;
2657}
2658
978e77e7
TI
2659static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2660 hda_nid_t *pins)
2661{
2662 struct hda_gen_spec *spec = codec->spec;
2663 int i;
2664
2665 for (i = 0; i < num_pins; i++) {
2666 hda_nid_t pin = pins[i];
ced4cefc 2667 if (pin == spec->hp_mic_pin)
5f171baa 2668 continue;
5f171baa 2669 if (get_out_jack_num_items(codec, pin) > 1) {
978e77e7 2670 struct snd_kcontrol_new *knew;
975cc02a 2671 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
978e77e7
TI
2672 get_jack_mode_name(codec, pin, name, sizeof(name));
2673 knew = snd_hda_gen_add_kctl(spec, name,
2674 &out_jack_mode_enum);
2675 if (!knew)
2676 return -ENOMEM;
2677 knew->private_value = pin;
2678 }
2679 }
2680
2681 return 0;
2682}
2683
29476558
TI
2684/*
2685 * input jack mode
2686 */
2687
2688/* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2689#define NUM_VREFS 6
2690
2691static const char * const vref_texts[NUM_VREFS] = {
2692 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2693 "", "Mic 80pc Bias", "Mic 100pc Bias"
2694};
2695
2696static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2697{
2698 unsigned int pincap;
2699
2700 pincap = snd_hda_query_pin_caps(codec, pin);
2701 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2702 /* filter out unusual vrefs */
2703 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2704 return pincap;
2705}
2706
2707/* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2708static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2709{
2710 unsigned int i, n = 0;
2711
2712 for (i = 0; i < NUM_VREFS; i++) {
2713 if (vref_caps & (1 << i)) {
2714 if (n == item_idx)
2715 return i;
2716 n++;
2717 }
2718 }
2719 return 0;
2720}
2721
2722/* convert back from the vref ctl index to the enum item index */
2723static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2724{
2725 unsigned int i, n = 0;
2726
2727 for (i = 0; i < NUM_VREFS; i++) {
2728 if (i == idx)
2729 return n;
2730 if (vref_caps & (1 << i))
2731 n++;
2732 }
2733 return 0;
2734}
2735
2736static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2737 struct snd_ctl_elem_info *uinfo)
2738{
2739 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2740 hda_nid_t nid = kcontrol->private_value;
2741 unsigned int vref_caps = get_vref_caps(codec, nid);
2742
2743 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2744 vref_texts);
2745 /* set the right text */
2746 strcpy(uinfo->value.enumerated.name,
2747 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2748 return 0;
2749}
2750
2751static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2752 struct snd_ctl_elem_value *ucontrol)
2753{
2754 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2755 hda_nid_t nid = kcontrol->private_value;
2756 unsigned int vref_caps = get_vref_caps(codec, nid);
2757 unsigned int idx;
2758
2759 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2760 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2761 return 0;
2762}
2763
2764static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2765 struct snd_ctl_elem_value *ucontrol)
2766{
2767 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2768 hda_nid_t nid = kcontrol->private_value;
2769 unsigned int vref_caps = get_vref_caps(codec, nid);
2770 unsigned int val, idx;
2771
2772 val = snd_hda_codec_get_pin_target(codec, nid);
2773 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2774 if (idx == ucontrol->value.enumerated.item[0])
2775 return 0;
2776
2777 val &= ~AC_PINCTL_VREFEN;
2778 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2779 snd_hda_set_pin_ctl_cache(codec, nid, val);
2780 return 1;
2781}
2782
2783static const struct snd_kcontrol_new in_jack_mode_enum = {
2784 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2785 .info = in_jack_mode_info,
2786 .get = in_jack_mode_get,
2787 .put = in_jack_mode_put,
2788};
2789
5f171baa
TI
2790static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2791{
2792 struct hda_gen_spec *spec = codec->spec;
2793 int nitems = 0;
f811c3cf 2794 if (spec->add_jack_modes)
5f171baa
TI
2795 nitems = hweight32(get_vref_caps(codec, pin));
2796 return nitems ? nitems : 1;
2797}
2798
29476558
TI
2799static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2800{
2801 struct hda_gen_spec *spec = codec->spec;
29476558 2802 struct snd_kcontrol_new *knew;
975cc02a 2803 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
5f171baa
TI
2804 unsigned int defcfg;
2805
f811c3cf
TI
2806 if (pin == spec->hp_mic_pin)
2807 return 0; /* already done in create_out_jack_mode() */
29476558
TI
2808
2809 /* no jack mode for fixed pins */
2810 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2811 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2812 return 0;
2813
2814 /* no multiple vref caps? */
5f171baa 2815 if (get_in_jack_num_items(codec, pin) <= 1)
29476558
TI
2816 return 0;
2817
2818 get_jack_mode_name(codec, pin, name, sizeof(name));
2819 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2820 if (!knew)
2821 return -ENOMEM;
2822 knew->private_value = pin;
2823 return 0;
2824}
2825
5f171baa
TI
2826/*
2827 * HP/mic shared jack mode
2828 */
2829static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2830 struct snd_ctl_elem_info *uinfo)
2831{
2832 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2833 hda_nid_t nid = kcontrol->private_value;
2834 int out_jacks = get_out_jack_num_items(codec, nid);
2835 int in_jacks = get_in_jack_num_items(codec, nid);
2836 const char *text = NULL;
2837 int idx;
2838
2839 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2840 uinfo->count = 1;
2841 uinfo->value.enumerated.items = out_jacks + in_jacks;
2842 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2843 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2844 idx = uinfo->value.enumerated.item;
2845 if (idx < out_jacks) {
2846 if (out_jacks > 1)
2847 text = out_jack_texts[idx];
2848 else
2849 text = "Headphone Out";
2850 } else {
2851 idx -= out_jacks;
2852 if (in_jacks > 1) {
2853 unsigned int vref_caps = get_vref_caps(codec, nid);
2854 text = vref_texts[get_vref_idx(vref_caps, idx)];
2855 } else
2856 text = "Mic In";
2857 }
2858
2859 strcpy(uinfo->value.enumerated.name, text);
2860 return 0;
2861}
2862
2863static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2864{
2865 int out_jacks = get_out_jack_num_items(codec, nid);
2866 int in_jacks = get_in_jack_num_items(codec, nid);
2867 unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2868 int idx = 0;
2869
2870 if (val & PIN_OUT) {
2871 if (out_jacks > 1 && val == PIN_HP)
2872 idx = 1;
2873 } else if (val & PIN_IN) {
2874 idx = out_jacks;
2875 if (in_jacks > 1) {
2876 unsigned int vref_caps = get_vref_caps(codec, nid);
2877 val &= AC_PINCTL_VREFEN;
2878 idx += cvt_from_vref_idx(vref_caps, val);
2879 }
2880 }
2881 return idx;
2882}
2883
2884static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2885 struct snd_ctl_elem_value *ucontrol)
2886{
2887 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2888 hda_nid_t nid = kcontrol->private_value;
2889 ucontrol->value.enumerated.item[0] =
2890 get_cur_hp_mic_jack_mode(codec, nid);
2891 return 0;
2892}
2893
2894static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2895 struct snd_ctl_elem_value *ucontrol)
2896{
2897 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2898 hda_nid_t nid = kcontrol->private_value;
2899 int out_jacks = get_out_jack_num_items(codec, nid);
2900 int in_jacks = get_in_jack_num_items(codec, nid);
2901 unsigned int val, oldval, idx;
2902
2903 oldval = get_cur_hp_mic_jack_mode(codec, nid);
2904 idx = ucontrol->value.enumerated.item[0];
2905 if (oldval == idx)
2906 return 0;
2907
2908 if (idx < out_jacks) {
2909 if (out_jacks > 1)
2910 val = idx ? PIN_HP : PIN_OUT;
2911 else
2912 val = PIN_HP;
2913 } else {
2914 idx -= out_jacks;
2915 if (in_jacks > 1) {
2916 unsigned int vref_caps = get_vref_caps(codec, nid);
2917 val = snd_hda_codec_get_pin_target(codec, nid);
3f550e32
TI
2918 val &= ~(AC_PINCTL_VREFEN | PIN_HP);
2919 val |= get_vref_idx(vref_caps, idx) | PIN_IN;
5f171baa 2920 } else
16c0cefe 2921 val = snd_hda_get_default_vref(codec, nid) | PIN_IN;
5f171baa
TI
2922 }
2923 snd_hda_set_pin_ctl_cache(codec, nid, val);
963afde9 2924 call_hp_automute(codec, NULL);
8ba955ce 2925
5f171baa
TI
2926 return 1;
2927}
2928
2929static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
2930 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2931 .info = hp_mic_jack_mode_info,
2932 .get = hp_mic_jack_mode_get,
2933 .put = hp_mic_jack_mode_put,
2934};
2935
2936static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2937{
2938 struct hda_gen_spec *spec = codec->spec;
2939 struct snd_kcontrol_new *knew;
2940
5f171baa
TI
2941 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
2942 &hp_mic_jack_mode_enum);
2943 if (!knew)
2944 return -ENOMEM;
2945 knew->private_value = pin;
8ba955ce 2946 spec->hp_mic_jack_modes = 1;
5f171baa
TI
2947 return 0;
2948}
352f7f91
TI
2949
2950/*
2951 * Parse input paths
2952 */
2953
352f7f91 2954/* add the powersave loopback-list entry */
0186f4f4 2955static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
352f7f91
TI
2956{
2957 struct hda_amp_list *list;
2958
0186f4f4
TI
2959 list = snd_array_new(&spec->loopback_list);
2960 if (!list)
2961 return -ENOMEM;
352f7f91
TI
2962 list->nid = mix;
2963 list->dir = HDA_INPUT;
2964 list->idx = idx;
0186f4f4
TI
2965 spec->loopback.amplist = spec->loopback_list.list;
2966 return 0;
352f7f91 2967}
352f7f91 2968
2ded3e5b
TI
2969/* return true if either a volume or a mute amp is found for the given
2970 * aamix path; the amp has to be either in the mixer node or its direct leaf
2971 */
2972static bool look_for_mix_leaf_ctls(struct hda_codec *codec, hda_nid_t mix_nid,
2973 hda_nid_t pin, unsigned int *mix_val,
2974 unsigned int *mute_val)
2975{
2976 int idx, num_conns;
2977 const hda_nid_t *list;
2978 hda_nid_t nid;
2979
2980 idx = snd_hda_get_conn_index(codec, mix_nid, pin, true);
2981 if (idx < 0)
2982 return false;
2983
2984 *mix_val = *mute_val = 0;
2985 if (nid_has_volume(codec, mix_nid, HDA_INPUT))
2986 *mix_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2987 if (nid_has_mute(codec, mix_nid, HDA_INPUT))
2988 *mute_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2989 if (*mix_val && *mute_val)
2990 return true;
2991
2992 /* check leaf node */
2993 num_conns = snd_hda_get_conn_list(codec, mix_nid, &list);
2994 if (num_conns < idx)
2995 return false;
2996 nid = list[idx];
43a8e50a
TI
2997 if (!*mix_val && nid_has_volume(codec, nid, HDA_OUTPUT) &&
2998 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_VOL_CTL))
2ded3e5b 2999 *mix_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
43a8e50a
TI
3000 if (!*mute_val && nid_has_mute(codec, nid, HDA_OUTPUT) &&
3001 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_MUTE_CTL))
2ded3e5b
TI
3002 *mute_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3003
3004 return *mix_val || *mute_val;
3005}
3006
352f7f91 3007/* create input playback/capture controls for the given pin */
196c1766
TI
3008static int new_analog_input(struct hda_codec *codec, int input_idx,
3009 hda_nid_t pin, const char *ctlname, int ctlidx,
352f7f91
TI
3010 hda_nid_t mix_nid)
3011{
3012 struct hda_gen_spec *spec = codec->spec;
3013 struct nid_path *path;
2ded3e5b 3014 unsigned int mix_val, mute_val;
352f7f91
TI
3015 int err, idx;
3016
2ded3e5b
TI
3017 if (!look_for_mix_leaf_ctls(codec, mix_nid, pin, &mix_val, &mute_val))
3018 return 0;
352f7f91 3019
3ca529d3 3020 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
352f7f91
TI
3021 if (!path)
3022 return -EINVAL;
4e76a883 3023 print_nid_path(codec, "loopback", path);
196c1766 3024 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
352f7f91
TI
3025
3026 idx = path->idx[path->depth - 1];
2ded3e5b
TI
3027 if (mix_val) {
3028 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, mix_val);
352f7f91
TI
3029 if (err < 0)
3030 return err;
2ded3e5b 3031 path->ctls[NID_PATH_VOL_CTL] = mix_val;
352f7f91
TI
3032 }
3033
2ded3e5b
TI
3034 if (mute_val) {
3035 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, mute_val);
352f7f91
TI
3036 if (err < 0)
3037 return err;
2ded3e5b 3038 path->ctls[NID_PATH_MUTE_CTL] = mute_val;
352f7f91
TI
3039 }
3040
3041 path->active = true;
e6feb5d0 3042 path->stream_enabled = true; /* no DAC/ADC involved */
0186f4f4
TI
3043 err = add_loopback_list(spec, mix_nid, idx);
3044 if (err < 0)
3045 return err;
e4a395e7
TI
3046
3047 if (spec->mixer_nid != spec->mixer_merge_nid &&
3048 !spec->loopback_merge_path) {
3049 path = snd_hda_add_new_path(codec, spec->mixer_nid,
3050 spec->mixer_merge_nid, 0);
3051 if (path) {
4e76a883 3052 print_nid_path(codec, "loopback-merge", path);
e4a395e7 3053 path->active = true;
6b275b14 3054 path->pin_fixed = true; /* static route */
e6feb5d0 3055 path->stream_enabled = true; /* no DAC/ADC involved */
e4a395e7
TI
3056 spec->loopback_merge_path =
3057 snd_hda_get_path_idx(codec, path);
3058 }
3059 }
3060
352f7f91
TI
3061 return 0;
3062}
3063
3064static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
3065{
3066 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
3067 return (pincap & AC_PINCAP_IN) != 0;
3068}
3069
3070/* Parse the codec tree and retrieve ADCs */
3071static int fill_adc_nids(struct hda_codec *codec)
3072{
3073 struct hda_gen_spec *spec = codec->spec;
3074 hda_nid_t nid;
3075 hda_nid_t *adc_nids = spec->adc_nids;
3076 int max_nums = ARRAY_SIZE(spec->adc_nids);
7639a06c 3077 int nums = 0;
352f7f91 3078
7639a06c 3079 for_each_hda_codec_node(nid, codec) {
352f7f91
TI
3080 unsigned int caps = get_wcaps(codec, nid);
3081 int type = get_wcaps_type(caps);
3082
3083 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
3084 continue;
3085 adc_nids[nums] = nid;
3086 if (++nums >= max_nums)
3087 break;
3088 }
3089 spec->num_adc_nids = nums;
0ffd534e
TI
3090
3091 /* copy the detected ADCs to all_adcs[] */
3092 spec->num_all_adcs = nums;
3093 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
3094
352f7f91
TI
3095 return nums;
3096}
3097
3098/* filter out invalid adc_nids that don't give all active input pins;
3099 * if needed, check whether dynamic ADC-switching is available
3100 */
3101static int check_dyn_adc_switch(struct hda_codec *codec)
3102{
3103 struct hda_gen_spec *spec = codec->spec;
3104 struct hda_input_mux *imux = &spec->input_mux;
3a65bcdc 3105 unsigned int ok_bits;
352f7f91 3106 int i, n, nums;
352f7f91 3107
352f7f91 3108 nums = 0;
3a65bcdc 3109 ok_bits = 0;
352f7f91 3110 for (n = 0; n < spec->num_adc_nids; n++) {
352f7f91 3111 for (i = 0; i < imux->num_items; i++) {
3a65bcdc 3112 if (!spec->input_paths[i][n])
352f7f91
TI
3113 break;
3114 }
3a65bcdc
TI
3115 if (i >= imux->num_items) {
3116 ok_bits |= (1 << n);
3117 nums++;
3118 }
352f7f91
TI
3119 }
3120
3a65bcdc 3121 if (!ok_bits) {
352f7f91
TI
3122 /* check whether ADC-switch is possible */
3123 for (i = 0; i < imux->num_items; i++) {
352f7f91 3124 for (n = 0; n < spec->num_adc_nids; n++) {
3a65bcdc 3125 if (spec->input_paths[i][n]) {
352f7f91
TI
3126 spec->dyn_adc_idx[i] = n;
3127 break;
3128 }
3129 }
3130 }
3131
4e76a883 3132 codec_dbg(codec, "enabling ADC switching\n");
352f7f91
TI
3133 spec->dyn_adc_switch = 1;
3134 } else if (nums != spec->num_adc_nids) {
3a65bcdc
TI
3135 /* shrink the invalid adcs and input paths */
3136 nums = 0;
3137 for (n = 0; n < spec->num_adc_nids; n++) {
3138 if (!(ok_bits & (1 << n)))
3139 continue;
3140 if (n != nums) {
3141 spec->adc_nids[nums] = spec->adc_nids[n];
980428ce
TI
3142 for (i = 0; i < imux->num_items; i++) {
3143 invalidate_nid_path(codec,
3144 spec->input_paths[i][nums]);
3a65bcdc
TI
3145 spec->input_paths[i][nums] =
3146 spec->input_paths[i][n];
980428ce 3147 }
3a65bcdc
TI
3148 }
3149 nums++;
3150 }
352f7f91
TI
3151 spec->num_adc_nids = nums;
3152 }
3153
967303da
TI
3154 if (imux->num_items == 1 ||
3155 (imux->num_items == 2 && spec->hp_mic)) {
4e76a883 3156 codec_dbg(codec, "reducing to a single ADC\n");
352f7f91
TI
3157 spec->num_adc_nids = 1; /* reduce to a single ADC */
3158 }
3159
3160 /* single index for individual volumes ctls */
3161 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
3162 spec->num_adc_nids = 1;
3163
3164 return 0;
3165}
3166
f3fc0b0b
TI
3167/* parse capture source paths from the given pin and create imux items */
3168static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
9dba205b
TI
3169 int cfg_idx, int num_adcs,
3170 const char *label, int anchor)
f3fc0b0b
TI
3171{
3172 struct hda_gen_spec *spec = codec->spec;
3173 struct hda_input_mux *imux = &spec->input_mux;
3174 int imux_idx = imux->num_items;
3175 bool imux_added = false;
3176 int c;
3177
3178 for (c = 0; c < num_adcs; c++) {
3179 struct nid_path *path;
3180 hda_nid_t adc = spec->adc_nids[c];
3181
3182 if (!is_reachable_path(codec, pin, adc))
3183 continue;
3184 path = snd_hda_add_new_path(codec, pin, adc, anchor);
3185 if (!path)
3186 continue;
4e76a883 3187 print_nid_path(codec, "input", path);
f3fc0b0b
TI
3188 spec->input_paths[imux_idx][c] =
3189 snd_hda_get_path_idx(codec, path);
3190
3191 if (!imux_added) {
967303da
TI
3192 if (spec->hp_mic_pin == pin)
3193 spec->hp_mic_mux_idx = imux->num_items;
f3fc0b0b 3194 spec->imux_pins[imux->num_items] = pin;
6194b99d 3195 snd_hda_add_imux_item(codec, imux, label, cfg_idx, NULL);
f3fc0b0b 3196 imux_added = true;
f1e762dd
TI
3197 if (spec->dyn_adc_switch)
3198 spec->dyn_adc_idx[imux_idx] = c;
f3fc0b0b
TI
3199 }
3200 }
3201
3202 return 0;
3203}
3204
352f7f91
TI
3205/*
3206 * create playback/capture controls for input pins
3207 */
9dba205b 3208
c970042c
TI
3209/* fill the label for each input at first */
3210static int fill_input_pin_labels(struct hda_codec *codec)
3211{
3212 struct hda_gen_spec *spec = codec->spec;
3213 const struct auto_pin_cfg *cfg = &spec->autocfg;
3214 int i;
3215
3216 for (i = 0; i < cfg->num_inputs; i++) {
3217 hda_nid_t pin = cfg->inputs[i].pin;
3218 const char *label;
3219 int j, idx;
3220
3221 if (!is_input_pin(codec, pin))
3222 continue;
3223
3224 label = hda_get_autocfg_input_label(codec, cfg, i);
3225 idx = 0;
8e8db7f1 3226 for (j = i - 1; j >= 0; j--) {
c970042c
TI
3227 if (spec->input_labels[j] &&
3228 !strcmp(spec->input_labels[j], label)) {
3229 idx = spec->input_label_idxs[j] + 1;
3230 break;
3231 }
3232 }
3233
3234 spec->input_labels[i] = label;
3235 spec->input_label_idxs[i] = idx;
3236 }
3237
3238 return 0;
3239}
3240
9dba205b
TI
3241#define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
3242
352f7f91
TI
3243static int create_input_ctls(struct hda_codec *codec)
3244{
3245 struct hda_gen_spec *spec = codec->spec;
3246 const struct auto_pin_cfg *cfg = &spec->autocfg;
3247 hda_nid_t mixer = spec->mixer_nid;
352f7f91 3248 int num_adcs;
c970042c 3249 int i, err;
2c12c30d 3250 unsigned int val;
352f7f91
TI
3251
3252 num_adcs = fill_adc_nids(codec);
3253 if (num_adcs < 0)
3254 return 0;
3255
c970042c
TI
3256 err = fill_input_pin_labels(codec);
3257 if (err < 0)
3258 return err;
3259
352f7f91
TI
3260 for (i = 0; i < cfg->num_inputs; i++) {
3261 hda_nid_t pin;
352f7f91
TI
3262
3263 pin = cfg->inputs[i].pin;
3264 if (!is_input_pin(codec, pin))
3265 continue;
3266
2c12c30d
TI
3267 val = PIN_IN;
3268 if (cfg->inputs[i].type == AUTO_PIN_MIC)
3269 val |= snd_hda_get_default_vref(codec, pin);
3e1b0c4a
TI
3270 if (pin != spec->hp_mic_pin &&
3271 !snd_hda_codec_get_pin_target(codec, pin))
93c9d8ae 3272 set_pin_target(codec, pin, val, false);
2c12c30d 3273
352f7f91
TI
3274 if (mixer) {
3275 if (is_reachable_path(codec, pin, mixer)) {
196c1766 3276 err = new_analog_input(codec, i, pin,
c970042c
TI
3277 spec->input_labels[i],
3278 spec->input_label_idxs[i],
3279 mixer);
352f7f91
TI
3280 if (err < 0)
3281 return err;
3282 }
3283 }
3284
c970042c
TI
3285 err = parse_capture_source(codec, pin, i, num_adcs,
3286 spec->input_labels[i], -mixer);
f3fc0b0b
TI
3287 if (err < 0)
3288 return err;
29476558 3289
f811c3cf 3290 if (spec->add_jack_modes) {
29476558
TI
3291 err = create_in_jack_mode(codec, pin);
3292 if (err < 0)
3293 return err;
3294 }
f3fc0b0b 3295 }
352f7f91 3296
f1e762dd 3297 /* add stereo mix when explicitly enabled via hint */
74f14b36 3298 if (mixer && spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_ENABLE) {
9dba205b 3299 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
f3fc0b0b
TI
3300 "Stereo Mix", 0);
3301 if (err < 0)
3302 return err;
82d04e10
TI
3303 else
3304 spec->suppress_auto_mic = 1;
352f7f91
TI
3305 }
3306
3307 return 0;
3308}
3309
3310
3311/*
3312 * input source mux
3313 */
3314
c697b716
TI
3315/* get the input path specified by the given adc and imux indices */
3316static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
352f7f91
TI
3317{
3318 struct hda_gen_spec *spec = codec->spec;
b56fa1ed
DH
3319 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3320 snd_BUG();
3321 return NULL;
3322 }
352f7f91
TI
3323 if (spec->dyn_adc_switch)
3324 adc_idx = spec->dyn_adc_idx[imux_idx];
d3d982f7 3325 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
b56fa1ed
DH
3326 snd_BUG();
3327 return NULL;
3328 }
c697b716 3329 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
352f7f91
TI
3330}
3331
3332static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3333 unsigned int idx);
3334
3335static int mux_enum_info(struct snd_kcontrol *kcontrol,
3336 struct snd_ctl_elem_info *uinfo)
3337{
3338 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3339 struct hda_gen_spec *spec = codec->spec;
3340 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3341}
3342
3343static int mux_enum_get(struct snd_kcontrol *kcontrol,
3344 struct snd_ctl_elem_value *ucontrol)
3345{
3346 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3347 struct hda_gen_spec *spec = codec->spec;
2a8d5391
TI
3348 /* the ctls are created at once with multiple counts */
3349 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
352f7f91
TI
3350
3351 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3352 return 0;
3353}
3354
3355static int mux_enum_put(struct snd_kcontrol *kcontrol,
3356 struct snd_ctl_elem_value *ucontrol)
3357{
3358 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2a8d5391 3359 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
352f7f91
TI
3360 return mux_select(codec, adc_idx,
3361 ucontrol->value.enumerated.item[0]);
3362}
3363
352f7f91
TI
3364static const struct snd_kcontrol_new cap_src_temp = {
3365 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3366 .name = "Input Source",
3367 .info = mux_enum_info,
3368 .get = mux_enum_get,
3369 .put = mux_enum_put,
3370};
3371
47d46abb
TI
3372/*
3373 * capture volume and capture switch ctls
3374 */
3375
352f7f91
TI
3376typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3377 struct snd_ctl_elem_value *ucontrol);
3378
47d46abb 3379/* call the given amp update function for all amps in the imux list at once */
352f7f91
TI
3380static int cap_put_caller(struct snd_kcontrol *kcontrol,
3381 struct snd_ctl_elem_value *ucontrol,
3382 put_call_t func, int type)
3383{
3384 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3385 struct hda_gen_spec *spec = codec->spec;
3386 const struct hda_input_mux *imux;
3387 struct nid_path *path;
3388 int i, adc_idx, err = 0;
3389
3390 imux = &spec->input_mux;
a053d1e3 3391 adc_idx = kcontrol->id.index;
352f7f91 3392 mutex_lock(&codec->control_mutex);
352f7f91 3393 for (i = 0; i < imux->num_items; i++) {
c697b716
TI
3394 path = get_input_path(codec, adc_idx, i);
3395 if (!path || !path->ctls[type])
352f7f91
TI
3396 continue;
3397 kcontrol->private_value = path->ctls[type];
3398 err = func(kcontrol, ucontrol);
3399 if (err < 0)
a551d914 3400 break;
352f7f91 3401 }
352f7f91
TI
3402 mutex_unlock(&codec->control_mutex);
3403 if (err >= 0 && spec->cap_sync_hook)
7fe30711 3404 spec->cap_sync_hook(codec, kcontrol, ucontrol);
352f7f91
TI
3405 return err;
3406}
3407
3408/* capture volume ctl callbacks */
3409#define cap_vol_info snd_hda_mixer_amp_volume_info
3410#define cap_vol_get snd_hda_mixer_amp_volume_get
3411#define cap_vol_tlv snd_hda_mixer_amp_tlv
3412
3413static int cap_vol_put(struct snd_kcontrol *kcontrol,
3414 struct snd_ctl_elem_value *ucontrol)
3415{
3416 return cap_put_caller(kcontrol, ucontrol,
3417 snd_hda_mixer_amp_volume_put,
3418 NID_PATH_VOL_CTL);
3419}
3420
3421static const struct snd_kcontrol_new cap_vol_temp = {
3422 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3423 .name = "Capture Volume",
3424 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3425 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3426 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3427 .info = cap_vol_info,
3428 .get = cap_vol_get,
3429 .put = cap_vol_put,
3430 .tlv = { .c = cap_vol_tlv },
3431};
3432
3433/* capture switch ctl callbacks */
3434#define cap_sw_info snd_ctl_boolean_stereo_info
3435#define cap_sw_get snd_hda_mixer_amp_switch_get
3436
3437static int cap_sw_put(struct snd_kcontrol *kcontrol,
3438 struct snd_ctl_elem_value *ucontrol)
3439{
a90229e0 3440 return cap_put_caller(kcontrol, ucontrol,
352f7f91
TI
3441 snd_hda_mixer_amp_switch_put,
3442 NID_PATH_MUTE_CTL);
3443}
3444
3445static const struct snd_kcontrol_new cap_sw_temp = {
3446 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3447 .name = "Capture Switch",
3448 .info = cap_sw_info,
3449 .get = cap_sw_get,
3450 .put = cap_sw_put,
3451};
3452
3453static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3454{
3455 hda_nid_t nid;
3456 int i, depth;
3457
3458 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3459 for (depth = 0; depth < 3; depth++) {
3460 if (depth >= path->depth)
3461 return -EINVAL;
3462 i = path->depth - depth - 1;
3463 nid = path->path[i];
3464 if (!path->ctls[NID_PATH_VOL_CTL]) {
3465 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3466 path->ctls[NID_PATH_VOL_CTL] =
3467 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3468 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3469 int idx = path->idx[i];
3470 if (!depth && codec->single_adc_amp)
3471 idx = 0;
3472 path->ctls[NID_PATH_VOL_CTL] =
3473 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3474 }
3475 }
3476 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3477 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3478 path->ctls[NID_PATH_MUTE_CTL] =
3479 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3480 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3481 int idx = path->idx[i];
3482 if (!depth && codec->single_adc_amp)
3483 idx = 0;
3484 path->ctls[NID_PATH_MUTE_CTL] =
3485 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3486 }
3487 }
3488 }
3489 return 0;
3490}
3491
3492static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
3493{
3494 struct hda_gen_spec *spec = codec->spec;
3495 struct auto_pin_cfg *cfg = &spec->autocfg;
3496 unsigned int val;
3497 int i;
3498
3499 if (!spec->inv_dmic_split)
3500 return false;
3501 for (i = 0; i < cfg->num_inputs; i++) {
3502 if (cfg->inputs[i].pin != nid)
3503 continue;
3504 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3505 return false;
3506 val = snd_hda_codec_get_pincfg(codec, nid);
3507 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3508 }
3509 return false;
3510}
3511
a90229e0 3512/* capture switch put callback for a single control with hook call */
a35bd1e3
TI
3513static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3514 struct snd_ctl_elem_value *ucontrol)
3515{
3516 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3517 struct hda_gen_spec *spec = codec->spec;
3518 int ret;
3519
3520 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3521 if (ret < 0)
3522 return ret;
3523
a90229e0 3524 if (spec->cap_sync_hook)
7fe30711 3525 spec->cap_sync_hook(codec, kcontrol, ucontrol);
a35bd1e3
TI
3526
3527 return ret;
3528}
3529
352f7f91
TI
3530static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3531 int idx, bool is_switch, unsigned int ctl,
3532 bool inv_dmic)
3533{
3534 struct hda_gen_spec *spec = codec->spec;
975cc02a 3535 char tmpname[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
352f7f91
TI
3536 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3537 const char *sfx = is_switch ? "Switch" : "Volume";
3538 unsigned int chs = inv_dmic ? 1 : 3;
a35bd1e3 3539 struct snd_kcontrol_new *knew;
352f7f91
TI
3540
3541 if (!ctl)
3542 return 0;
3543
3544 if (label)
3545 snprintf(tmpname, sizeof(tmpname),
3546 "%s Capture %s", label, sfx);
3547 else
3548 snprintf(tmpname, sizeof(tmpname),
3549 "Capture %s", sfx);
a35bd1e3
TI
3550 knew = add_control(spec, type, tmpname, idx,
3551 amp_val_replace_channels(ctl, chs));
3552 if (!knew)
3553 return -ENOMEM;
a90229e0 3554 if (is_switch)
a35bd1e3
TI
3555 knew->put = cap_single_sw_put;
3556 if (!inv_dmic)
3557 return 0;
352f7f91
TI
3558
3559 /* Make independent right kcontrol */
3560 if (label)
3561 snprintf(tmpname, sizeof(tmpname),
3562 "Inverted %s Capture %s", label, sfx);
3563 else
3564 snprintf(tmpname, sizeof(tmpname),
3565 "Inverted Capture %s", sfx);
a35bd1e3 3566 knew = add_control(spec, type, tmpname, idx,
352f7f91 3567 amp_val_replace_channels(ctl, 2));
a35bd1e3
TI
3568 if (!knew)
3569 return -ENOMEM;
a90229e0 3570 if (is_switch)
a35bd1e3
TI
3571 knew->put = cap_single_sw_put;
3572 return 0;
352f7f91
TI
3573}
3574
3575/* create single (and simple) capture volume and switch controls */
3576static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3577 unsigned int vol_ctl, unsigned int sw_ctl,
3578 bool inv_dmic)
3579{
3580 int err;
3581 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3582 if (err < 0)
3583 return err;
3584 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3585 if (err < 0)
3586 return err;
3587 return 0;
3588}
3589
3590/* create bound capture volume and switch controls */
3591static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3592 unsigned int vol_ctl, unsigned int sw_ctl)
3593{
3594 struct hda_gen_spec *spec = codec->spec;
3595 struct snd_kcontrol_new *knew;
3596
3597 if (vol_ctl) {
12c93df6 3598 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
352f7f91
TI
3599 if (!knew)
3600 return -ENOMEM;
3601 knew->index = idx;
3602 knew->private_value = vol_ctl;
3603 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3604 }
3605 if (sw_ctl) {
12c93df6 3606 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
352f7f91
TI
3607 if (!knew)
3608 return -ENOMEM;
3609 knew->index = idx;
3610 knew->private_value = sw_ctl;
3611 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3612 }
3613 return 0;
3614}
3615
3616/* return the vol ctl when used first in the imux list */
3617static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3618{
352f7f91
TI
3619 struct nid_path *path;
3620 unsigned int ctl;
3621 int i;
3622
c697b716 3623 path = get_input_path(codec, 0, idx);
352f7f91
TI
3624 if (!path)
3625 return 0;
3626 ctl = path->ctls[type];
3627 if (!ctl)
3628 return 0;
3629 for (i = 0; i < idx - 1; i++) {
c697b716 3630 path = get_input_path(codec, 0, i);
352f7f91
TI
3631 if (path && path->ctls[type] == ctl)
3632 return 0;
3633 }
3634 return ctl;
3635}
3636
3637/* create individual capture volume and switch controls per input */
3638static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3639{
3640 struct hda_gen_spec *spec = codec->spec;
3641 struct hda_input_mux *imux = &spec->input_mux;
c970042c 3642 int i, err, type;
352f7f91
TI
3643
3644 for (i = 0; i < imux->num_items; i++) {
352f7f91 3645 bool inv_dmic;
c970042c 3646 int idx;
9dba205b 3647
c970042c
TI
3648 idx = imux->items[i].index;
3649 if (idx >= spec->autocfg.num_inputs)
9dba205b 3650 continue;
352f7f91
TI
3651 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3652
3653 for (type = 0; type < 2; type++) {
c970042c
TI
3654 err = add_single_cap_ctl(codec,
3655 spec->input_labels[idx],
3656 spec->input_label_idxs[idx],
3657 type,
352f7f91
TI
3658 get_first_cap_ctl(codec, i, type),
3659 inv_dmic);
3660 if (err < 0)
3661 return err;
3662 }
3663 }
3664 return 0;
3665}
3666
3667static int create_capture_mixers(struct hda_codec *codec)
3668{
3669 struct hda_gen_spec *spec = codec->spec;
3670 struct hda_input_mux *imux = &spec->input_mux;
3671 int i, n, nums, err;
3672
3673 if (spec->dyn_adc_switch)
3674 nums = 1;
3675 else
3676 nums = spec->num_adc_nids;
3677
3678 if (!spec->auto_mic && imux->num_items > 1) {
3679 struct snd_kcontrol_new *knew;
624d914d
TI
3680 const char *name;
3681 name = nums > 1 ? "Input Source" : "Capture Source";
3682 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
352f7f91
TI
3683 if (!knew)
3684 return -ENOMEM;
3685 knew->count = nums;
3686 }
3687
3688 for (n = 0; n < nums; n++) {
3689 bool multi = false;
99a5592d 3690 bool multi_cap_vol = spec->multi_cap_vol;
352f7f91
TI
3691 bool inv_dmic = false;
3692 int vol, sw;
3693
3694 vol = sw = 0;
3695 for (i = 0; i < imux->num_items; i++) {
3696 struct nid_path *path;
c697b716 3697 path = get_input_path(codec, n, i);
352f7f91
TI
3698 if (!path)
3699 continue;
3700 parse_capvol_in_path(codec, path);
3701 if (!vol)
3702 vol = path->ctls[NID_PATH_VOL_CTL];
99a5592d 3703 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
352f7f91 3704 multi = true;
99a5592d
DH
3705 if (!same_amp_caps(codec, vol,
3706 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3707 multi_cap_vol = true;
3708 }
352f7f91
TI
3709 if (!sw)
3710 sw = path->ctls[NID_PATH_MUTE_CTL];
99a5592d 3711 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
352f7f91 3712 multi = true;
99a5592d
DH
3713 if (!same_amp_caps(codec, sw,
3714 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3715 multi_cap_vol = true;
3716 }
352f7f91
TI
3717 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3718 inv_dmic = true;
3719 }
3720
3721 if (!multi)
3722 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3723 inv_dmic);
ccb04157 3724 else if (!multi_cap_vol && !inv_dmic)
352f7f91
TI
3725 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3726 else
3727 err = create_multi_cap_vol_ctl(codec);
3728 if (err < 0)
3729 return err;
3730 }
3731
3732 return 0;
3733}
3734
3735/*
3736 * add mic boosts if needed
3737 */
6f7c83af
TI
3738
3739/* check whether the given amp is feasible as a boost volume */
3740static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3741 int dir, int idx)
3742{
3743 unsigned int step;
3744
3745 if (!nid_has_volume(codec, nid, dir) ||
3746 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3747 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3748 return false;
3749
3750 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3751 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3752 if (step < 0x20)
3753 return false;
3754 return true;
3755}
3756
3757/* look for a boost amp in a widget close to the pin */
3758static unsigned int look_for_boost_amp(struct hda_codec *codec,
3759 struct nid_path *path)
3760{
3761 unsigned int val = 0;
3762 hda_nid_t nid;
3763 int depth;
3764
3765 for (depth = 0; depth < 3; depth++) {
3766 if (depth >= path->depth - 1)
3767 break;
3768 nid = path->path[depth];
3769 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3770 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3771 break;
3772 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3773 path->idx[depth])) {
3774 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3775 HDA_INPUT);
3776 break;
3777 }
3778 }
3779
3780 return val;
3781}
3782
352f7f91
TI
3783static int parse_mic_boost(struct hda_codec *codec)
3784{
3785 struct hda_gen_spec *spec = codec->spec;
3786 struct auto_pin_cfg *cfg = &spec->autocfg;
6f7c83af 3787 struct hda_input_mux *imux = &spec->input_mux;
a35bd1e3 3788 int i;
352f7f91 3789
6f7c83af
TI
3790 if (!spec->num_adc_nids)
3791 return 0;
352f7f91 3792
6f7c83af
TI
3793 for (i = 0; i < imux->num_items; i++) {
3794 struct nid_path *path;
3795 unsigned int val;
3796 int idx;
975cc02a 3797 char boost_label[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
02aba550 3798
6f7c83af
TI
3799 idx = imux->items[i].index;
3800 if (idx >= imux->num_items)
3801 continue;
352f7f91 3802
6f7c83af 3803 /* check only line-in and mic pins */
1799cdd5 3804 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
6f7c83af
TI
3805 continue;
3806
3807 path = get_input_path(codec, 0, i);
3808 if (!path)
3809 continue;
3810
3811 val = look_for_boost_amp(codec, path);
3812 if (!val)
3813 continue;
3814
3815 /* create a boost control */
3816 snprintf(boost_label, sizeof(boost_label),
3817 "%s Boost Volume", spec->input_labels[idx]);
a35bd1e3
TI
3818 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3819 spec->input_label_idxs[idx], val))
3820 return -ENOMEM;
6f7c83af
TI
3821
3822 path->ctls[NID_PATH_BOOST_CTL] = val;
352f7f91
TI
3823 }
3824 return 0;
3825}
3826
3827/*
3828 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3829 */
3830static void parse_digital(struct hda_codec *codec)
3831{
3832 struct hda_gen_spec *spec = codec->spec;
0c8c0f56 3833 struct nid_path *path;
352f7f91 3834 int i, nums;
2c12c30d 3835 hda_nid_t dig_nid, pin;
352f7f91
TI
3836
3837 /* support multiple SPDIFs; the secondary is set up as a slave */
3838 nums = 0;
3839 for (i = 0; i < spec->autocfg.dig_outs; i++) {
2c12c30d 3840 pin = spec->autocfg.dig_out_pins[i];
352f7f91
TI
3841 dig_nid = look_for_dac(codec, pin, true);
3842 if (!dig_nid)
3843 continue;
3ca529d3 3844 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
0c8c0f56 3845 if (!path)
352f7f91 3846 continue;
4e76a883 3847 print_nid_path(codec, "digout", path);
e1284af7 3848 path->active = true;
6b275b14 3849 path->pin_fixed = true; /* no jack detection */
196c1766 3850 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
2c12c30d 3851 set_pin_target(codec, pin, PIN_OUT, false);
352f7f91
TI
3852 if (!nums) {
3853 spec->multiout.dig_out_nid = dig_nid;
3854 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3855 } else {
3856 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3857 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
d576422e 3858 break;
352f7f91
TI
3859 spec->slave_dig_outs[nums - 1] = dig_nid;
3860 }
3861 nums++;
3862 }
3863
3864 if (spec->autocfg.dig_in_pin) {
2c12c30d 3865 pin = spec->autocfg.dig_in_pin;
7639a06c 3866 for_each_hda_codec_node(dig_nid, codec) {
352f7f91
TI
3867 unsigned int wcaps = get_wcaps(codec, dig_nid);
3868 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3869 continue;
3870 if (!(wcaps & AC_WCAP_DIGITAL))
3871 continue;
2c12c30d 3872 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
352f7f91 3873 if (path) {
4e76a883 3874 print_nid_path(codec, "digin", path);
352f7f91 3875 path->active = true;
6b275b14 3876 path->pin_fixed = true; /* no jack */
352f7f91 3877 spec->dig_in_nid = dig_nid;
2430d7b7 3878 spec->digin_path = snd_hda_get_path_idx(codec, path);
2c12c30d 3879 set_pin_target(codec, pin, PIN_IN, false);
352f7f91
TI
3880 break;
3881 }
3882 }
3883 }
3884}
3885
3886
3887/*
3888 * input MUX handling
3889 */
3890
3891static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3892
3893/* select the given imux item; either unmute exclusively or select the route */
3894static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3895 unsigned int idx)
3896{
3897 struct hda_gen_spec *spec = codec->spec;
3898 const struct hda_input_mux *imux;
55196fff 3899 struct nid_path *old_path, *path;
352f7f91
TI
3900
3901 imux = &spec->input_mux;
3902 if (!imux->num_items)
3903 return 0;
3904
3905 if (idx >= imux->num_items)
3906 idx = imux->num_items - 1;
3907 if (spec->cur_mux[adc_idx] == idx)
3908 return 0;
3909
55196fff
TI
3910 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3911 if (!old_path)
352f7f91 3912 return 0;
55196fff
TI
3913 if (old_path->active)
3914 snd_hda_activate_path(codec, old_path, false, false);
352f7f91
TI
3915
3916 spec->cur_mux[adc_idx] = idx;
3917
967303da
TI
3918 if (spec->hp_mic)
3919 update_hp_mic(codec, adc_idx, false);
352f7f91
TI
3920
3921 if (spec->dyn_adc_switch)
3922 dyn_adc_pcm_resetup(codec, idx);
3923
c697b716 3924 path = get_input_path(codec, adc_idx, idx);
352f7f91
TI
3925 if (!path)
3926 return 0;
3927 if (path->active)
3928 return 0;
3929 snd_hda_activate_path(codec, path, true, false);
3930 if (spec->cap_sync_hook)
7fe30711 3931 spec->cap_sync_hook(codec, NULL, NULL);
55196fff 3932 path_power_down_sync(codec, old_path);
352f7f91
TI
3933 return 1;
3934}
3935
e6feb5d0
TI
3936/* power up/down widgets in the all paths that match with the given NID
3937 * as terminals (either start- or endpoint)
3938 *
3939 * returns the last changed NID, or zero if unchanged.
3940 */
3941static hda_nid_t set_path_power(struct hda_codec *codec, hda_nid_t nid,
3942 int pin_state, int stream_state)
3943{
3944 struct hda_gen_spec *spec = codec->spec;
3945 hda_nid_t last, changed = 0;
3946 struct nid_path *path;
3947 int n;
3948
3949 for (n = 0; n < spec->paths.used; n++) {
3950 path = snd_array_elem(&spec->paths, n);
3951 if (path->path[0] == nid ||
3952 path->path[path->depth - 1] == nid) {
3953 bool pin_old = path->pin_enabled;
3954 bool stream_old = path->stream_enabled;
3955
3956 if (pin_state >= 0)
3957 path->pin_enabled = pin_state;
3958 if (stream_state >= 0)
3959 path->stream_enabled = stream_state;
6b275b14
TI
3960 if ((!path->pin_fixed && path->pin_enabled != pin_old)
3961 || path->stream_enabled != stream_old) {
e6feb5d0
TI
3962 last = path_power_update(codec, path, true);
3963 if (last)
3964 changed = last;
3965 }
3966 }
3967 }
3968 return changed;
3969}
3970
d5ac0100
TI
3971/* check the jack status for power control */
3972static bool detect_pin_state(struct hda_codec *codec, hda_nid_t pin)
3973{
3974 if (!is_jack_detectable(codec, pin))
3975 return true;
3976 return snd_hda_jack_detect_state(codec, pin) != HDA_JACK_NOT_PRESENT;
3977}
3978
e6feb5d0
TI
3979/* power up/down the paths of the given pin according to the jack state;
3980 * power = 0/1 : only power up/down if it matches with the jack state,
3981 * < 0 : force power up/down to follow the jack sate
3982 *
3983 * returns the last changed NID, or zero if unchanged.
3984 */
3985static hda_nid_t set_pin_power_jack(struct hda_codec *codec, hda_nid_t pin,
3986 int power)
3987{
3988 bool on;
3989
967b1307 3990 if (!codec->power_save_node)
e6feb5d0
TI
3991 return 0;
3992
d5ac0100
TI
3993 on = detect_pin_state(codec, pin);
3994
e6feb5d0
TI
3995 if (power >= 0 && on != power)
3996 return 0;
3997 return set_path_power(codec, pin, on, -1);
3998}
3999
4000static void pin_power_callback(struct hda_codec *codec,
4001 struct hda_jack_callback *jack,
4002 bool on)
4003{
4004 if (jack && jack->tbl->nid)
4005 sync_power_state_change(codec,
4006 set_pin_power_jack(codec, jack->tbl->nid, on));
4007}
4008
4009/* callback only doing power up -- called at first */
4010static void pin_power_up_callback(struct hda_codec *codec,
4011 struct hda_jack_callback *jack)
4012{
4013 pin_power_callback(codec, jack, true);
4014}
4015
4016/* callback only doing power down -- called at last */
4017static void pin_power_down_callback(struct hda_codec *codec,
4018 struct hda_jack_callback *jack)
4019{
4020 pin_power_callback(codec, jack, false);
4021}
4022
4023/* set up the power up/down callbacks */
4024static void add_pin_power_ctls(struct hda_codec *codec, int num_pins,
4025 const hda_nid_t *pins, bool on)
4026{
4027 int i;
4028 hda_jack_callback_fn cb =
4029 on ? pin_power_up_callback : pin_power_down_callback;
4030
4031 for (i = 0; i < num_pins && pins[i]; i++) {
4032 if (is_jack_detectable(codec, pins[i]))
4033 snd_hda_jack_detect_enable_callback(codec, pins[i], cb);
4034 else
4035 set_path_power(codec, pins[i], true, -1);
4036 }
4037}
4038
4039/* enabled power callback to each available I/O pin with jack detections;
4040 * the digital I/O pins are excluded because of the unreliable detectsion
4041 */
4042static void add_all_pin_power_ctls(struct hda_codec *codec, bool on)
4043{
4044 struct hda_gen_spec *spec = codec->spec;
4045 struct auto_pin_cfg *cfg = &spec->autocfg;
4046 int i;
4047
967b1307 4048 if (!codec->power_save_node)
e6feb5d0
TI
4049 return;
4050 add_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins, on);
4051 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4052 add_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins, on);
4053 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4054 add_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins, on);
4055 for (i = 0; i < cfg->num_inputs; i++)
4056 add_pin_power_ctls(codec, 1, &cfg->inputs[i].pin, on);
4057}
4058
4059/* sync path power up/down with the jack states of given pins */
4060static void sync_pin_power_ctls(struct hda_codec *codec, int num_pins,
4061 const hda_nid_t *pins)
4062{
4063 int i;
4064
4065 for (i = 0; i < num_pins && pins[i]; i++)
4066 if (is_jack_detectable(codec, pins[i]))
4067 set_pin_power_jack(codec, pins[i], -1);
4068}
4069
4070/* sync path power up/down with pins; called at init and resume */
4071static void sync_all_pin_power_ctls(struct hda_codec *codec)
4072{
4073 struct hda_gen_spec *spec = codec->spec;
4074 struct auto_pin_cfg *cfg = &spec->autocfg;
4075 int i;
4076
967b1307 4077 if (!codec->power_save_node)
e6feb5d0
TI
4078 return;
4079 sync_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins);
4080 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4081 sync_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins);
4082 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4083 sync_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins);
4084 for (i = 0; i < cfg->num_inputs; i++)
4085 sync_pin_power_ctls(codec, 1, &cfg->inputs[i].pin);
4086}
352f7f91 4087
5ccf835c
TI
4088/* add fake paths if not present yet */
4089static int add_fake_paths(struct hda_codec *codec, hda_nid_t nid,
4090 int num_pins, const hda_nid_t *pins)
4091{
4092 struct hda_gen_spec *spec = codec->spec;
4093 struct nid_path *path;
4094 int i;
4095
4096 for (i = 0; i < num_pins; i++) {
4097 if (!pins[i])
4098 break;
4099 if (get_nid_path(codec, nid, pins[i], 0))
4100 continue;
4101 path = snd_array_new(&spec->paths);
4102 if (!path)
4103 return -ENOMEM;
4104 memset(path, 0, sizeof(*path));
4105 path->depth = 2;
4106 path->path[0] = nid;
4107 path->path[1] = pins[i];
4108 path->active = true;
4109 }
4110 return 0;
4111}
4112
4113/* create fake paths to all outputs from beep */
4114static int add_fake_beep_paths(struct hda_codec *codec)
4115{
4116 struct hda_gen_spec *spec = codec->spec;
4117 struct auto_pin_cfg *cfg = &spec->autocfg;
4118 hda_nid_t nid = spec->beep_nid;
4119 int err;
4120
967b1307 4121 if (!codec->power_save_node || !nid)
5ccf835c
TI
4122 return 0;
4123 err = add_fake_paths(codec, nid, cfg->line_outs, cfg->line_out_pins);
4124 if (err < 0)
4125 return err;
4126 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4127 err = add_fake_paths(codec, nid, cfg->hp_outs, cfg->hp_pins);
4128 if (err < 0)
4129 return err;
4130 }
4131 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4132 err = add_fake_paths(codec, nid, cfg->speaker_outs,
4133 cfg->speaker_pins);
4134 if (err < 0)
4135 return err;
4136 }
4137 return 0;
4138}
4139
4140/* power up/down beep widget and its output paths */
4141static void beep_power_hook(struct hda_beep *beep, bool on)
4142{
4143 set_path_power(beep->codec, beep->nid, -1, on);
4144}
4145
6b275b14
TI
4146/**
4147 * snd_hda_gen_fix_pin_power - Fix the power of the given pin widget to D0
4148 * @codec: the HDA codec
4149 * @pin: NID of pin to fix
4150 */
4151int snd_hda_gen_fix_pin_power(struct hda_codec *codec, hda_nid_t pin)
4152{
4153 struct hda_gen_spec *spec = codec->spec;
4154 struct nid_path *path;
4155
4156 path = snd_array_new(&spec->paths);
4157 if (!path)
4158 return -ENOMEM;
4159 memset(path, 0, sizeof(*path));
4160 path->depth = 1;
4161 path->path[0] = pin;
4162 path->active = true;
4163 path->pin_fixed = true;
4164 path->stream_enabled = true;
4165 return 0;
4166}
4167EXPORT_SYMBOL_GPL(snd_hda_gen_fix_pin_power);
4168
352f7f91
TI
4169/*
4170 * Jack detections for HP auto-mute and mic-switch
4171 */
4172
4173/* check each pin in the given array; returns true if any of them is plugged */
4174static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
4175{
60ea8ca2
TI
4176 int i;
4177 bool present = false;
352f7f91
TI
4178
4179 for (i = 0; i < num_pins; i++) {
4180 hda_nid_t nid = pins[i];
4181 if (!nid)
4182 break;
0b4df931
TI
4183 /* don't detect pins retasked as inputs */
4184 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
4185 continue;
60ea8ca2
TI
4186 if (snd_hda_jack_detect_state(codec, nid) == HDA_JACK_PRESENT)
4187 present = true;
352f7f91
TI
4188 }
4189 return present;
4190}
4191
4192/* standard HP/line-out auto-mute helper */
4193static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
e80c60f3 4194 int *paths, bool mute)
352f7f91
TI
4195{
4196 struct hda_gen_spec *spec = codec->spec;
352f7f91
TI
4197 int i;
4198
4199 for (i = 0; i < num_pins; i++) {
4200 hda_nid_t nid = pins[i];
967303da 4201 unsigned int val, oldval;
352f7f91
TI
4202 if (!nid)
4203 break;
7eebffd3 4204
e6feb5d0
TI
4205 oldval = snd_hda_codec_get_pin_target(codec, nid);
4206 if (oldval & PIN_IN)
4207 continue; /* no mute for inputs */
4208
7eebffd3 4209 if (spec->auto_mute_via_amp) {
e80c60f3
TI
4210 struct nid_path *path;
4211 hda_nid_t mute_nid;
4212
4213 path = snd_hda_get_path_from_idx(codec, paths[i]);
4214 if (!path)
4215 continue;
4216 mute_nid = get_amp_nid_(path->ctls[NID_PATH_MUTE_CTL]);
4217 if (!mute_nid)
4218 continue;
7eebffd3 4219 if (mute)
e80c60f3 4220 spec->mute_bits |= (1ULL << mute_nid);
7eebffd3 4221 else
e80c60f3 4222 spec->mute_bits &= ~(1ULL << mute_nid);
7eebffd3 4223 continue;
e6feb5d0
TI
4224 } else {
4225 /* don't reset VREF value in case it's controlling
4226 * the amp (see alc861_fixup_asus_amp_vref_0f())
4227 */
4228 if (spec->keep_vref_in_automute)
4229 val = oldval & ~PIN_HP;
4230 else
4231 val = 0;
4232 if (!mute)
4233 val |= oldval;
4234 /* here we call update_pin_ctl() so that the pinctl is
4235 * changed without changing the pinctl target value;
4236 * the original target value will be still referred at
4237 * the init / resume again
4238 */
4239 update_pin_ctl(codec, nid, val);
7eebffd3
TI
4240 }
4241
d5a9f1bb 4242 set_pin_eapd(codec, nid, !mute);
967b1307 4243 if (codec->power_save_node) {
e6feb5d0
TI
4244 bool on = !mute;
4245 if (on)
d5ac0100 4246 on = detect_pin_state(codec, nid);
e6feb5d0
TI
4247 set_path_power(codec, nid, on, -1);
4248 }
352f7f91
TI
4249 }
4250}
4251
dda42bd0
TI
4252/**
4253 * snd_hda_gen_update_outputs - Toggle outputs muting
4254 * @codec: the HDA codec
4255 *
4256 * Update the mute status of all outputs based on the current jack states.
4257 */
5d550e15 4258void snd_hda_gen_update_outputs(struct hda_codec *codec)
352f7f91
TI
4259{
4260 struct hda_gen_spec *spec = codec->spec;
e80c60f3 4261 int *paths;
352f7f91
TI
4262 int on;
4263
4264 /* Control HP pins/amps depending on master_mute state;
4265 * in general, HP pins/amps control should be enabled in all cases,
4266 * but currently set only for master_mute, just to be safe
4267 */
e80c60f3
TI
4268 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
4269 paths = spec->out_paths;
4270 else
4271 paths = spec->hp_paths;
967303da 4272 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
e80c60f3 4273 spec->autocfg.hp_pins, paths, spec->master_mute);
352f7f91
TI
4274
4275 if (!spec->automute_speaker)
4276 on = 0;
4277 else
4278 on = spec->hp_jack_present | spec->line_jack_present;
4279 on |= spec->master_mute;
47b9ddb8 4280 spec->speaker_muted = on;
e80c60f3
TI
4281 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4282 paths = spec->out_paths;
4283 else
4284 paths = spec->speaker_paths;
352f7f91 4285 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
e80c60f3 4286 spec->autocfg.speaker_pins, paths, on);
352f7f91
TI
4287
4288 /* toggle line-out mutes if needed, too */
4289 /* if LO is a copy of either HP or Speaker, don't need to handle it */
4290 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
4291 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
4292 return;
4293 if (!spec->automute_lo)
4294 on = 0;
4295 else
4296 on = spec->hp_jack_present;
4297 on |= spec->master_mute;
47b9ddb8 4298 spec->line_out_muted = on;
e80c60f3 4299 paths = spec->out_paths;
352f7f91 4300 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
e80c60f3 4301 spec->autocfg.line_out_pins, paths, on);
352f7f91 4302}
2698ea98 4303EXPORT_SYMBOL_GPL(snd_hda_gen_update_outputs);
352f7f91
TI
4304
4305static void call_update_outputs(struct hda_codec *codec)
4306{
4307 struct hda_gen_spec *spec = codec->spec;
4308 if (spec->automute_hook)
4309 spec->automute_hook(codec);
4310 else
5d550e15 4311 snd_hda_gen_update_outputs(codec);
7eebffd3
TI
4312
4313 /* sync the whole vmaster slaves to reflect the new auto-mute status */
4314 if (spec->auto_mute_via_amp && !codec->bus->shutdown)
4315 snd_ctl_sync_vmaster(spec->vmaster_mute.sw_kctl, false);
352f7f91
TI
4316}
4317
dda42bd0
TI
4318/**
4319 * snd_hda_gen_hp_automute - standard HP-automute helper
4320 * @codec: the HDA codec
4321 * @jack: jack object, NULL for the whole
4322 */
1a4f69d5
TI
4323void snd_hda_gen_hp_automute(struct hda_codec *codec,
4324 struct hda_jack_callback *jack)
352f7f91
TI
4325{
4326 struct hda_gen_spec *spec = codec->spec;
92603c59
TI
4327 hda_nid_t *pins = spec->autocfg.hp_pins;
4328 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
4329
4330 /* No detection for the first HP jack during indep-HP mode */
4331 if (spec->indep_hp_enabled) {
4332 pins++;
4333 num_pins--;
4334 }
352f7f91 4335
92603c59 4336 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
352f7f91
TI
4337 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
4338 return;
4339 call_update_outputs(codec);
4340}
2698ea98 4341EXPORT_SYMBOL_GPL(snd_hda_gen_hp_automute);
352f7f91 4342
dda42bd0
TI
4343/**
4344 * snd_hda_gen_line_automute - standard line-out-automute helper
4345 * @codec: the HDA codec
4346 * @jack: jack object, NULL for the whole
4347 */
1a4f69d5
TI
4348void snd_hda_gen_line_automute(struct hda_codec *codec,
4349 struct hda_jack_callback *jack)
352f7f91
TI
4350{
4351 struct hda_gen_spec *spec = codec->spec;
4352
4353 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4354 return;
4355 /* check LO jack only when it's different from HP */
4356 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
4357 return;
4358
4359 spec->line_jack_present =
4360 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
4361 spec->autocfg.line_out_pins);
4362 if (!spec->automute_speaker || !spec->detect_lo)
4363 return;
4364 call_update_outputs(codec);
4365}
2698ea98 4366EXPORT_SYMBOL_GPL(snd_hda_gen_line_automute);
352f7f91 4367
dda42bd0
TI
4368/**
4369 * snd_hda_gen_mic_autoswitch - standard mic auto-switch helper
4370 * @codec: the HDA codec
4371 * @jack: jack object, NULL for the whole
4372 */
1a4f69d5
TI
4373void snd_hda_gen_mic_autoswitch(struct hda_codec *codec,
4374 struct hda_jack_callback *jack)
352f7f91
TI
4375{
4376 struct hda_gen_spec *spec = codec->spec;
4377 int i;
4378
4379 if (!spec->auto_mic)
4380 return;
4381
4382 for (i = spec->am_num_entries - 1; i > 0; i--) {
0b4df931
TI
4383 hda_nid_t pin = spec->am_entry[i].pin;
4384 /* don't detect pins retasked as outputs */
4385 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
4386 continue;
60ea8ca2 4387 if (snd_hda_jack_detect_state(codec, pin) == HDA_JACK_PRESENT) {
352f7f91
TI
4388 mux_select(codec, 0, spec->am_entry[i].idx);
4389 return;
4390 }
4391 }
4392 mux_select(codec, 0, spec->am_entry[0].idx);
4393}
2698ea98 4394EXPORT_SYMBOL_GPL(snd_hda_gen_mic_autoswitch);
352f7f91 4395
77afe0e9 4396/* call appropriate hooks */
1a4f69d5
TI
4397static void call_hp_automute(struct hda_codec *codec,
4398 struct hda_jack_callback *jack)
77afe0e9
TI
4399{
4400 struct hda_gen_spec *spec = codec->spec;
4401 if (spec->hp_automute_hook)
4402 spec->hp_automute_hook(codec, jack);
4403 else
4404 snd_hda_gen_hp_automute(codec, jack);
4405}
4406
4407static void call_line_automute(struct hda_codec *codec,
1a4f69d5 4408 struct hda_jack_callback *jack)
77afe0e9
TI
4409{
4410 struct hda_gen_spec *spec = codec->spec;
4411 if (spec->line_automute_hook)
4412 spec->line_automute_hook(codec, jack);
4413 else
4414 snd_hda_gen_line_automute(codec, jack);
4415}
4416
4417static void call_mic_autoswitch(struct hda_codec *codec,
1a4f69d5 4418 struct hda_jack_callback *jack)
77afe0e9
TI
4419{
4420 struct hda_gen_spec *spec = codec->spec;
4421 if (spec->mic_autoswitch_hook)
4422 spec->mic_autoswitch_hook(codec, jack);
4423 else
4424 snd_hda_gen_mic_autoswitch(codec, jack);
4425}
4426
963afde9
TI
4427/* update jack retasking */
4428static void update_automute_all(struct hda_codec *codec)
4429{
4430 call_hp_automute(codec, NULL);
4431 call_line_automute(codec, NULL);
4432 call_mic_autoswitch(codec, NULL);
4433}
4434
352f7f91
TI
4435/*
4436 * Auto-Mute mode mixer enum support
4437 */
4438static int automute_mode_info(struct snd_kcontrol *kcontrol,
4439 struct snd_ctl_elem_info *uinfo)
4440{
4441 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4442 struct hda_gen_spec *spec = codec->spec;
4443 static const char * const texts3[] = {
4444 "Disabled", "Speaker Only", "Line Out+Speaker"
4445 };
4446
4447 if (spec->automute_speaker_possible && spec->automute_lo_possible)
4448 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
4449 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
4450}
4451
4452static int automute_mode_get(struct snd_kcontrol *kcontrol,
4453 struct snd_ctl_elem_value *ucontrol)
4454{
4455 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4456 struct hda_gen_spec *spec = codec->spec;
4457 unsigned int val = 0;
4458 if (spec->automute_speaker)
4459 val++;
4460 if (spec->automute_lo)
4461 val++;
4462
4463 ucontrol->value.enumerated.item[0] = val;
4464 return 0;
4465}
4466
4467static int automute_mode_put(struct snd_kcontrol *kcontrol,
4468 struct snd_ctl_elem_value *ucontrol)
4469{
4470 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4471 struct hda_gen_spec *spec = codec->spec;
4472
4473 switch (ucontrol->value.enumerated.item[0]) {
4474 case 0:
4475 if (!spec->automute_speaker && !spec->automute_lo)
4476 return 0;
4477 spec->automute_speaker = 0;
4478 spec->automute_lo = 0;
4479 break;
4480 case 1:
4481 if (spec->automute_speaker_possible) {
4482 if (!spec->automute_lo && spec->automute_speaker)
4483 return 0;
4484 spec->automute_speaker = 1;
4485 spec->automute_lo = 0;
4486 } else if (spec->automute_lo_possible) {
4487 if (spec->automute_lo)
4488 return 0;
4489 spec->automute_lo = 1;
4490 } else
4491 return -EINVAL;
4492 break;
4493 case 2:
4494 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
4495 return -EINVAL;
4496 if (spec->automute_speaker && spec->automute_lo)
4497 return 0;
4498 spec->automute_speaker = 1;
4499 spec->automute_lo = 1;
4500 break;
4501 default:
4502 return -EINVAL;
4503 }
4504 call_update_outputs(codec);
4505 return 1;
4506}
4507
4508static const struct snd_kcontrol_new automute_mode_enum = {
4509 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4510 .name = "Auto-Mute Mode",
4511 .info = automute_mode_info,
4512 .get = automute_mode_get,
4513 .put = automute_mode_put,
4514};
4515
4516static int add_automute_mode_enum(struct hda_codec *codec)
4517{
4518 struct hda_gen_spec *spec = codec->spec;
4519
12c93df6 4520 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
352f7f91
TI
4521 return -ENOMEM;
4522 return 0;
4523}
4524
4525/*
4526 * Check the availability of HP/line-out auto-mute;
4527 * Set up appropriately if really supported
4528 */
4529static int check_auto_mute_availability(struct hda_codec *codec)
4530{
4531 struct hda_gen_spec *spec = codec->spec;
4532 struct auto_pin_cfg *cfg = &spec->autocfg;
4533 int present = 0;
4534 int i, err;
4535
f72706be
TI
4536 if (spec->suppress_auto_mute)
4537 return 0;
4538
352f7f91
TI
4539 if (cfg->hp_pins[0])
4540 present++;
4541 if (cfg->line_out_pins[0])
4542 present++;
4543 if (cfg->speaker_pins[0])
4544 present++;
4545 if (present < 2) /* need two different output types */
4546 return 0;
4547
4548 if (!cfg->speaker_pins[0] &&
4549 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
4550 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4551 sizeof(cfg->speaker_pins));
4552 cfg->speaker_outs = cfg->line_outs;
4553 }
4554
4555 if (!cfg->hp_pins[0] &&
4556 cfg->line_out_type == AUTO_PIN_HP_OUT) {
4557 memcpy(cfg->hp_pins, cfg->line_out_pins,
4558 sizeof(cfg->hp_pins));
4559 cfg->hp_outs = cfg->line_outs;
4560 }
4561
4562 for (i = 0; i < cfg->hp_outs; i++) {
4563 hda_nid_t nid = cfg->hp_pins[i];
4564 if (!is_jack_detectable(codec, nid))
4565 continue;
4e76a883 4566 codec_dbg(codec, "Enable HP auto-muting on NID 0x%x\n", nid);
62f949bf 4567 snd_hda_jack_detect_enable_callback(codec, nid,
77afe0e9 4568 call_hp_automute);
352f7f91
TI
4569 spec->detect_hp = 1;
4570 }
4571
4572 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
4573 if (cfg->speaker_outs)
4574 for (i = 0; i < cfg->line_outs; i++) {
4575 hda_nid_t nid = cfg->line_out_pins[i];
4576 if (!is_jack_detectable(codec, nid))
4577 continue;
4e76a883 4578 codec_dbg(codec, "Enable Line-Out auto-muting on NID 0x%x\n", nid);
352f7f91 4579 snd_hda_jack_detect_enable_callback(codec, nid,
77afe0e9 4580 call_line_automute);
352f7f91
TI
4581 spec->detect_lo = 1;
4582 }
4583 spec->automute_lo_possible = spec->detect_hp;
4584 }
4585
4586 spec->automute_speaker_possible = cfg->speaker_outs &&
4587 (spec->detect_hp || spec->detect_lo);
4588
4589 spec->automute_lo = spec->automute_lo_possible;
4590 spec->automute_speaker = spec->automute_speaker_possible;
4591
4592 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
4593 /* create a control for automute mode */
4594 err = add_automute_mode_enum(codec);
4595 if (err < 0)
4596 return err;
4597 }
4598 return 0;
4599}
352f7f91
TI
4600
4601/* check whether all auto-mic pins are valid; setup indices if OK */
4602static bool auto_mic_check_imux(struct hda_codec *codec)
4603{
4604 struct hda_gen_spec *spec = codec->spec;
4605 const struct hda_input_mux *imux;
4606 int i;
4607
4608 imux = &spec->input_mux;
4609 for (i = 0; i < spec->am_num_entries; i++) {
4610 spec->am_entry[i].idx =
4611 find_idx_in_nid_list(spec->am_entry[i].pin,
4612 spec->imux_pins, imux->num_items);
4613 if (spec->am_entry[i].idx < 0)
4614 return false; /* no corresponding imux */
4615 }
4616
4617 /* we don't need the jack detection for the first pin */
4618 for (i = 1; i < spec->am_num_entries; i++)
4619 snd_hda_jack_detect_enable_callback(codec,
4620 spec->am_entry[i].pin,
77afe0e9 4621 call_mic_autoswitch);
352f7f91
TI
4622 return true;
4623}
4624
4625static int compare_attr(const void *ap, const void *bp)
4626{
4627 const struct automic_entry *a = ap;
4628 const struct automic_entry *b = bp;
4629 return (int)(a->attr - b->attr);
4630}
1da177e4
LT
4631
4632/*
352f7f91
TI
4633 * Check the availability of auto-mic switch;
4634 * Set up if really supported
1da177e4 4635 */
352f7f91
TI
4636static int check_auto_mic_availability(struct hda_codec *codec)
4637{
4638 struct hda_gen_spec *spec = codec->spec;
4639 struct auto_pin_cfg *cfg = &spec->autocfg;
4640 unsigned int types;
4641 int i, num_pins;
4642
d12daf6f
TI
4643 if (spec->suppress_auto_mic)
4644 return 0;
4645
352f7f91
TI
4646 types = 0;
4647 num_pins = 0;
4648 for (i = 0; i < cfg->num_inputs; i++) {
4649 hda_nid_t nid = cfg->inputs[i].pin;
4650 unsigned int attr;
4651 attr = snd_hda_codec_get_pincfg(codec, nid);
4652 attr = snd_hda_get_input_pin_attr(attr);
4653 if (types & (1 << attr))
4654 return 0; /* already occupied */
4655 switch (attr) {
4656 case INPUT_PIN_ATTR_INT:
4657 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4658 return 0; /* invalid type */
4659 break;
4660 case INPUT_PIN_ATTR_UNUSED:
4661 return 0; /* invalid entry */
4662 default:
4663 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4664 return 0; /* invalid type */
4665 if (!spec->line_in_auto_switch &&
4666 cfg->inputs[i].type != AUTO_PIN_MIC)
4667 return 0; /* only mic is allowed */
4668 if (!is_jack_detectable(codec, nid))
4669 return 0; /* no unsol support */
4670 break;
4671 }
4672 if (num_pins >= MAX_AUTO_MIC_PINS)
4673 return 0;
4674 types |= (1 << attr);
4675 spec->am_entry[num_pins].pin = nid;
4676 spec->am_entry[num_pins].attr = attr;
4677 num_pins++;
4678 }
4679
4680 if (num_pins < 2)
4681 return 0;
4682
4683 spec->am_num_entries = num_pins;
4684 /* sort the am_entry in the order of attr so that the pin with a
4685 * higher attr will be selected when the jack is plugged.
4686 */
4687 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4688 compare_attr, NULL);
4689
4690 if (!auto_mic_check_imux(codec))
4691 return 0;
4692
4693 spec->auto_mic = 1;
4694 spec->num_adc_nids = 1;
4695 spec->cur_mux[0] = spec->am_entry[0].idx;
4e76a883 4696 codec_dbg(codec, "Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
352f7f91
TI
4697 spec->am_entry[0].pin,
4698 spec->am_entry[1].pin,
4699 spec->am_entry[2].pin);
4700
1da177e4
LT
4701 return 0;
4702}
4703
dda42bd0
TI
4704/**
4705 * snd_hda_gen_path_power_filter - power_filter hook to make inactive widgets
4706 * into power down
4707 * @codec: the HDA codec
4708 * @nid: NID to evalute
4709 * @power_state: target power state
4710 */
dfc6e469 4711unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
55196fff
TI
4712 hda_nid_t nid,
4713 unsigned int power_state)
4714{
b6c09b3c
TI
4715 struct hda_gen_spec *spec = codec->spec;
4716
4717 if (!spec->power_down_unused && !codec->power_save_node)
4718 return power_state;
7639a06c 4719 if (power_state != AC_PWRST_D0 || nid == codec->core.afg)
55196fff
TI
4720 return power_state;
4721 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4722 return power_state;
b1b9fbd0 4723 if (is_active_nid_for_any(codec, nid))
55196fff
TI
4724 return power_state;
4725 return AC_PWRST_D3;
4726}
dfc6e469 4727EXPORT_SYMBOL_GPL(snd_hda_gen_path_power_filter);
55196fff 4728
ebb93c05
TI
4729/* mute all aamix inputs initially; parse up to the first leaves */
4730static void mute_all_mixer_nid(struct hda_codec *codec, hda_nid_t mix)
4731{
4732 int i, nums;
4733 const hda_nid_t *conn;
4734 bool has_amp;
4735
4736 nums = snd_hda_get_conn_list(codec, mix, &conn);
4737 has_amp = nid_has_mute(codec, mix, HDA_INPUT);
4738 for (i = 0; i < nums; i++) {
4739 if (has_amp)
ef403edb
TI
4740 update_amp(codec, mix, HDA_INPUT, i,
4741 0xff, HDA_AMP_MUTE);
ebb93c05 4742 else if (nid_has_volume(codec, conn[i], HDA_OUTPUT))
ef403edb
TI
4743 update_amp(codec, conn[i], HDA_OUTPUT, 0,
4744 0xff, HDA_AMP_MUTE);
ebb93c05
TI
4745 }
4746}
1da177e4 4747
e6feb5d0
TI
4748/**
4749 * snd_hda_gen_stream_pm - Stream power management callback
4750 * @codec: the HDA codec
4751 * @nid: audio widget
4752 * @on: power on/off flag
4753 *
967b1307 4754 * Set this in patch_ops.stream_pm. Only valid with power_save_node flag.
e6feb5d0
TI
4755 */
4756void snd_hda_gen_stream_pm(struct hda_codec *codec, hda_nid_t nid, bool on)
4757{
967b1307 4758 if (codec->power_save_node)
e6feb5d0
TI
4759 set_path_power(codec, nid, -1, on);
4760}
4761EXPORT_SYMBOL_GPL(snd_hda_gen_stream_pm);
4762
dda42bd0
TI
4763/**
4764 * snd_hda_gen_parse_auto_config - Parse the given BIOS configuration and
4765 * set up the hda_gen_spec
4766 * @codec: the HDA codec
4767 * @cfg: Parsed pin configuration
9eb413e5
TI
4768 *
4769 * return 1 if successful, 0 if the proper config is not found,
352f7f91
TI
4770 * or a negative error code
4771 */
4772int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
9eb413e5 4773 struct auto_pin_cfg *cfg)
352f7f91
TI
4774{
4775 struct hda_gen_spec *spec = codec->spec;
352f7f91
TI
4776 int err;
4777
1c70a583
TI
4778 parse_user_hints(codec);
4779
e4a395e7
TI
4780 if (spec->mixer_nid && !spec->mixer_merge_nid)
4781 spec->mixer_merge_nid = spec->mixer_nid;
4782
9eb413e5
TI
4783 if (cfg != &spec->autocfg) {
4784 spec->autocfg = *cfg;
4785 cfg = &spec->autocfg;
4786 }
4787
98bd1115
TI
4788 if (!spec->main_out_badness)
4789 spec->main_out_badness = &hda_main_out_badness;
4790 if (!spec->extra_out_badness)
4791 spec->extra_out_badness = &hda_extra_out_badness;
4792
6fc4cb97
DH
4793 fill_all_dac_nids(codec);
4794
352f7f91
TI
4795 if (!cfg->line_outs) {
4796 if (cfg->dig_outs || cfg->dig_in_pin) {
4797 spec->multiout.max_channels = 2;
4798 spec->no_analog = 1;
4799 goto dig_only;
4800 }
c9e4bdb7
TI
4801 if (!cfg->num_inputs && !cfg->dig_in_pin)
4802 return 0; /* can't find valid BIOS pin config */
352f7f91
TI
4803 }
4804
4805 if (!spec->no_primary_hp &&
4806 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
4807 cfg->line_outs <= cfg->hp_outs) {
4808 /* use HP as primary out */
4809 cfg->speaker_outs = cfg->line_outs;
4810 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4811 sizeof(cfg->speaker_pins));
4812 cfg->line_outs = cfg->hp_outs;
4813 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4814 cfg->hp_outs = 0;
4815 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4816 cfg->line_out_type = AUTO_PIN_HP_OUT;
4817 }
4818
4819 err = parse_output_paths(codec);
4820 if (err < 0)
4821 return err;
4822 err = create_multi_channel_mode(codec);
4823 if (err < 0)
4824 return err;
4825 err = create_multi_out_ctls(codec, cfg);
4826 if (err < 0)
4827 return err;
4828 err = create_hp_out_ctls(codec);
4829 if (err < 0)
4830 return err;
4831 err = create_speaker_out_ctls(codec);
38cf6f1a
TI
4832 if (err < 0)
4833 return err;
4834 err = create_indep_hp_ctls(codec);
c30aa7b2
TI
4835 if (err < 0)
4836 return err;
4837 err = create_loopback_mixing_ctl(codec);
352f7f91
TI
4838 if (err < 0)
4839 return err;
967303da 4840 err = create_hp_mic(codec);
352f7f91
TI
4841 if (err < 0)
4842 return err;
4843 err = create_input_ctls(codec);
4844 if (err < 0)
4845 return err;
4846
e6feb5d0
TI
4847 /* add power-down pin callbacks at first */
4848 add_all_pin_power_ctls(codec, false);
4849
a07a949b
TI
4850 spec->const_channel_count = spec->ext_channel_count;
4851 /* check the multiple speaker and headphone pins */
4852 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4853 spec->const_channel_count = max(spec->const_channel_count,
4854 cfg->speaker_outs * 2);
4855 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4856 spec->const_channel_count = max(spec->const_channel_count,
4857 cfg->hp_outs * 2);
4858 spec->multiout.max_channels = max(spec->ext_channel_count,
4859 spec->const_channel_count);
352f7f91
TI
4860
4861 err = check_auto_mute_availability(codec);
4862 if (err < 0)
4863 return err;
4864
4865 err = check_dyn_adc_switch(codec);
4866 if (err < 0)
4867 return err;
4868
967303da
TI
4869 err = check_auto_mic_availability(codec);
4870 if (err < 0)
4871 return err;
1da177e4 4872
f1e762dd
TI
4873 /* add stereo mix if available and not enabled yet */
4874 if (!spec->auto_mic && spec->mixer_nid &&
74f14b36
TI
4875 spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_AUTO &&
4876 spec->input_mux.num_items > 1) {
f1e762dd
TI
4877 err = parse_capture_source(codec, spec->mixer_nid,
4878 CFG_IDX_MIX, spec->num_all_adcs,
4879 "Stereo Mix", 0);
4880 if (err < 0)
4881 return err;
4882 }
4883
4884
352f7f91
TI
4885 err = create_capture_mixers(codec);
4886 if (err < 0)
4887 return err;
a7da6ce5 4888
352f7f91
TI
4889 err = parse_mic_boost(codec);
4890 if (err < 0)
4891 return err;
4892
ced4cefc
TI
4893 /* create "Headphone Mic Jack Mode" if no input selection is
4894 * available (or user specifies add_jack_modes hint)
4895 */
4896 if (spec->hp_mic_pin &&
4897 (spec->auto_mic || spec->input_mux.num_items == 1 ||
4898 spec->add_jack_modes)) {
4899 err = create_hp_mic_jack_mode(codec, spec->hp_mic_pin);
4900 if (err < 0)
4901 return err;
4902 }
4903
f811c3cf 4904 if (spec->add_jack_modes) {
978e77e7
TI
4905 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4906 err = create_out_jack_modes(codec, cfg->line_outs,
4907 cfg->line_out_pins);
4908 if (err < 0)
4909 return err;
4910 }
4911 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4912 err = create_out_jack_modes(codec, cfg->hp_outs,
4913 cfg->hp_pins);
4914 if (err < 0)
4915 return err;
4916 }
4917 }
4918
e6feb5d0
TI
4919 /* add power-up pin callbacks at last */
4920 add_all_pin_power_ctls(codec, true);
4921
ebb93c05
TI
4922 /* mute all aamix input initially */
4923 if (spec->mixer_nid)
4924 mute_all_mixer_nid(codec, spec->mixer_nid);
4925
352f7f91
TI
4926 dig_only:
4927 parse_digital(codec);
4928
49fb1897 4929 if (spec->power_down_unused || codec->power_save_node) {
24fef902
TI
4930 if (!codec->power_filter)
4931 codec->power_filter = snd_hda_gen_path_power_filter;
49fb1897
TI
4932 if (!codec->patch_ops.stream_pm)
4933 codec->patch_ops.stream_pm = snd_hda_gen_stream_pm;
4934 }
55196fff 4935
7504b6cd
TI
4936 if (!spec->no_analog && spec->beep_nid) {
4937 err = snd_hda_attach_beep_device(codec, spec->beep_nid);
4938 if (err < 0)
4939 return err;
967b1307 4940 if (codec->beep && codec->power_save_node) {
5ccf835c
TI
4941 err = add_fake_beep_paths(codec);
4942 if (err < 0)
4943 return err;
4944 codec->beep->power_hook = beep_power_hook;
4945 }
7504b6cd
TI
4946 }
4947
352f7f91 4948 return 1;
a7da6ce5 4949}
2698ea98 4950EXPORT_SYMBOL_GPL(snd_hda_gen_parse_auto_config);
a7da6ce5 4951
071c73ad 4952
352f7f91
TI
4953/*
4954 * Build control elements
4955 */
4956
4957/* slave controls for virtual master */
4958static const char * const slave_pfxs[] = {
4959 "Front", "Surround", "Center", "LFE", "Side",
4960 "Headphone", "Speaker", "Mono", "Line Out",
4961 "CLFE", "Bass Speaker", "PCM",
ee79c69a
TI
4962 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
4963 "Headphone Front", "Headphone Surround", "Headphone CLFE",
03ad6a8c 4964 "Headphone Side", "Headphone+LO", "Speaker+LO",
352f7f91
TI
4965 NULL,
4966};
4967
dda42bd0
TI
4968/**
4969 * snd_hda_gen_build_controls - Build controls from the parsed results
4970 * @codec: the HDA codec
4971 *
4972 * Pass this to build_controls patch_ops.
4973 */
352f7f91
TI
4974int snd_hda_gen_build_controls(struct hda_codec *codec)
4975{
4976 struct hda_gen_spec *spec = codec->spec;
4977 int err;
1da177e4 4978
36502d02
TI
4979 if (spec->kctls.used) {
4980 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
4981 if (err < 0)
4982 return err;
4983 }
071c73ad 4984
352f7f91
TI
4985 if (spec->multiout.dig_out_nid) {
4986 err = snd_hda_create_dig_out_ctls(codec,
4987 spec->multiout.dig_out_nid,
4988 spec->multiout.dig_out_nid,
bbbc7e85 4989 spec->pcm_rec[1]->pcm_type);
352f7f91
TI
4990 if (err < 0)
4991 return err;
4992 if (!spec->no_analog) {
4993 err = snd_hda_create_spdif_share_sw(codec,
4994 &spec->multiout);
4995 if (err < 0)
4996 return err;
4997 spec->multiout.share_spdif = 1;
4998 }
4999 }
5000 if (spec->dig_in_nid) {
5001 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
071c73ad
TI
5002 if (err < 0)
5003 return err;
071c73ad 5004 }
1da177e4 5005
352f7f91
TI
5006 /* if we have no master control, let's create it */
5007 if (!spec->no_analog &&
5008 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
352f7f91 5009 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
7a71bbf3 5010 spec->vmaster_tlv, slave_pfxs,
352f7f91
TI
5011 "Playback Volume");
5012 if (err < 0)
5013 return err;
5014 }
5015 if (!spec->no_analog &&
5016 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
5017 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
5018 NULL, slave_pfxs,
5019 "Playback Switch",
5020 true, &spec->vmaster_mute.sw_kctl);
5021 if (err < 0)
5022 return err;
b63eae0a 5023 if (spec->vmaster_mute.hook) {
fd25a97a
TI
5024 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
5025 spec->vmaster_mute_enum);
b63eae0a
TI
5026 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5027 }
352f7f91 5028 }
071c73ad 5029
352f7f91 5030 free_kctls(spec); /* no longer needed */
071c73ad 5031
352f7f91
TI
5032 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
5033 if (err < 0)
5034 return err;
5035
1da177e4
LT
5036 return 0;
5037}
2698ea98 5038EXPORT_SYMBOL_GPL(snd_hda_gen_build_controls);
1da177e4
LT
5039
5040
5041/*
352f7f91 5042 * PCM definitions
1da177e4 5043 */
1da177e4 5044
e6b85f3c
TI
5045static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
5046 struct hda_codec *codec,
5047 struct snd_pcm_substream *substream,
5048 int action)
5049{
5050 struct hda_gen_spec *spec = codec->spec;
5051 if (spec->pcm_playback_hook)
5052 spec->pcm_playback_hook(hinfo, codec, substream, action);
5053}
5054
ac2e8736
TI
5055static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
5056 struct hda_codec *codec,
5057 struct snd_pcm_substream *substream,
5058 int action)
5059{
5060 struct hda_gen_spec *spec = codec->spec;
5061 if (spec->pcm_capture_hook)
5062 spec->pcm_capture_hook(hinfo, codec, substream, action);
5063}
5064
352f7f91
TI
5065/*
5066 * Analog playback callbacks
5067 */
5068static int playback_pcm_open(struct hda_pcm_stream *hinfo,
5069 struct hda_codec *codec,
5070 struct snd_pcm_substream *substream)
5071{
5072 struct hda_gen_spec *spec = codec->spec;
38cf6f1a
TI
5073 int err;
5074
5075 mutex_lock(&spec->pcm_mutex);
5076 err = snd_hda_multi_out_analog_open(codec,
5077 &spec->multiout, substream,
352f7f91 5078 hinfo);
e6b85f3c 5079 if (!err) {
38cf6f1a 5080 spec->active_streams |= 1 << STREAM_MULTI_OUT;
e6b85f3c
TI
5081 call_pcm_playback_hook(hinfo, codec, substream,
5082 HDA_GEN_PCM_ACT_OPEN);
5083 }
38cf6f1a
TI
5084 mutex_unlock(&spec->pcm_mutex);
5085 return err;
352f7f91 5086}
1da177e4 5087
352f7f91
TI
5088static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5089 struct hda_codec *codec,
5090 unsigned int stream_tag,
5091 unsigned int format,
5092 struct snd_pcm_substream *substream)
5093{
5094 struct hda_gen_spec *spec = codec->spec;
e6b85f3c
TI
5095 int err;
5096
5097 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
5098 stream_tag, format, substream);
5099 if (!err)
5100 call_pcm_playback_hook(hinfo, codec, substream,
5101 HDA_GEN_PCM_ACT_PREPARE);
5102 return err;
352f7f91 5103}
1da177e4 5104
352f7f91
TI
5105static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5106 struct hda_codec *codec,
5107 struct snd_pcm_substream *substream)
5108{
5109 struct hda_gen_spec *spec = codec->spec;
e6b85f3c
TI
5110 int err;
5111
5112 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
5113 if (!err)
5114 call_pcm_playback_hook(hinfo, codec, substream,
5115 HDA_GEN_PCM_ACT_CLEANUP);
5116 return err;
1da177e4
LT
5117}
5118
38cf6f1a
TI
5119static int playback_pcm_close(struct hda_pcm_stream *hinfo,
5120 struct hda_codec *codec,
5121 struct snd_pcm_substream *substream)
5122{
5123 struct hda_gen_spec *spec = codec->spec;
5124 mutex_lock(&spec->pcm_mutex);
5125 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
e6b85f3c
TI
5126 call_pcm_playback_hook(hinfo, codec, substream,
5127 HDA_GEN_PCM_ACT_CLOSE);
38cf6f1a
TI
5128 mutex_unlock(&spec->pcm_mutex);
5129 return 0;
5130}
5131
ac2e8736
TI
5132static int capture_pcm_open(struct hda_pcm_stream *hinfo,
5133 struct hda_codec *codec,
5134 struct snd_pcm_substream *substream)
5135{
5136 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
5137 return 0;
5138}
5139
5140static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5141 struct hda_codec *codec,
5142 unsigned int stream_tag,
5143 unsigned int format,
5144 struct snd_pcm_substream *substream)
5145{
5146 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5147 call_pcm_capture_hook(hinfo, codec, substream,
5148 HDA_GEN_PCM_ACT_PREPARE);
5149 return 0;
5150}
5151
5152static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5153 struct hda_codec *codec,
5154 struct snd_pcm_substream *substream)
5155{
5156 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5157 call_pcm_capture_hook(hinfo, codec, substream,
5158 HDA_GEN_PCM_ACT_CLEANUP);
5159 return 0;
5160}
5161
5162static int capture_pcm_close(struct hda_pcm_stream *hinfo,
5163 struct hda_codec *codec,
5164 struct snd_pcm_substream *substream)
5165{
5166 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
5167 return 0;
5168}
5169
38cf6f1a
TI
5170static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
5171 struct hda_codec *codec,
5172 struct snd_pcm_substream *substream)
5173{
5174 struct hda_gen_spec *spec = codec->spec;
5175 int err = 0;
5176
5177 mutex_lock(&spec->pcm_mutex);
5178 if (!spec->indep_hp_enabled)
5179 err = -EBUSY;
5180 else
5181 spec->active_streams |= 1 << STREAM_INDEP_HP;
e6b85f3c
TI
5182 call_pcm_playback_hook(hinfo, codec, substream,
5183 HDA_GEN_PCM_ACT_OPEN);
38cf6f1a
TI
5184 mutex_unlock(&spec->pcm_mutex);
5185 return err;
5186}
5187
5188static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
5189 struct hda_codec *codec,
5190 struct snd_pcm_substream *substream)
5191{
5192 struct hda_gen_spec *spec = codec->spec;
5193 mutex_lock(&spec->pcm_mutex);
5194 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
e6b85f3c
TI
5195 call_pcm_playback_hook(hinfo, codec, substream,
5196 HDA_GEN_PCM_ACT_CLOSE);
38cf6f1a
TI
5197 mutex_unlock(&spec->pcm_mutex);
5198 return 0;
5199}
5200
e6b85f3c
TI
5201static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5202 struct hda_codec *codec,
5203 unsigned int stream_tag,
5204 unsigned int format,
5205 struct snd_pcm_substream *substream)
5206{
5207 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5208 call_pcm_playback_hook(hinfo, codec, substream,
5209 HDA_GEN_PCM_ACT_PREPARE);
5210 return 0;
5211}
5212
5213static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5214 struct hda_codec *codec,
5215 struct snd_pcm_substream *substream)
5216{
5217 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5218 call_pcm_playback_hook(hinfo, codec, substream,
5219 HDA_GEN_PCM_ACT_CLEANUP);
5220 return 0;
5221}
5222
1da177e4 5223/*
352f7f91 5224 * Digital out
1da177e4 5225 */
352f7f91
TI
5226static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
5227 struct hda_codec *codec,
5228 struct snd_pcm_substream *substream)
1da177e4 5229{
352f7f91
TI
5230 struct hda_gen_spec *spec = codec->spec;
5231 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
5232}
1da177e4 5233
352f7f91
TI
5234static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5235 struct hda_codec *codec,
5236 unsigned int stream_tag,
5237 unsigned int format,
5238 struct snd_pcm_substream *substream)
5239{
5240 struct hda_gen_spec *spec = codec->spec;
5241 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
5242 stream_tag, format, substream);
5243}
1da177e4 5244
352f7f91
TI
5245static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5246 struct hda_codec *codec,
5247 struct snd_pcm_substream *substream)
5248{
5249 struct hda_gen_spec *spec = codec->spec;
5250 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
5251}
5252
5253static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
5254 struct hda_codec *codec,
5255 struct snd_pcm_substream *substream)
5256{
5257 struct hda_gen_spec *spec = codec->spec;
5258 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
1da177e4
LT
5259}
5260
5261/*
352f7f91 5262 * Analog capture
1da177e4 5263 */
ac2e8736
TI
5264#define alt_capture_pcm_open capture_pcm_open
5265#define alt_capture_pcm_close capture_pcm_close
5266
352f7f91
TI
5267static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5268 struct hda_codec *codec,
5269 unsigned int stream_tag,
5270 unsigned int format,
5271 struct snd_pcm_substream *substream)
1da177e4 5272{
352f7f91 5273 struct hda_gen_spec *spec = codec->spec;
1da177e4 5274
352f7f91
TI
5275 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
5276 stream_tag, 0, format);
ac2e8736
TI
5277 call_pcm_capture_hook(hinfo, codec, substream,
5278 HDA_GEN_PCM_ACT_PREPARE);
352f7f91
TI
5279 return 0;
5280}
5281
5282static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5283 struct hda_codec *codec,
5284 struct snd_pcm_substream *substream)
5285{
5286 struct hda_gen_spec *spec = codec->spec;
1da177e4 5287
352f7f91
TI
5288 snd_hda_codec_cleanup_stream(codec,
5289 spec->adc_nids[substream->number + 1]);
ac2e8736
TI
5290 call_pcm_capture_hook(hinfo, codec, substream,
5291 HDA_GEN_PCM_ACT_CLEANUP);
1da177e4
LT
5292 return 0;
5293}
5294
5295/*
1da177e4 5296 */
352f7f91
TI
5297static const struct hda_pcm_stream pcm_analog_playback = {
5298 .substreams = 1,
5299 .channels_min = 2,
5300 .channels_max = 8,
5301 /* NID is set in build_pcms */
5302 .ops = {
5303 .open = playback_pcm_open,
38cf6f1a 5304 .close = playback_pcm_close,
352f7f91
TI
5305 .prepare = playback_pcm_prepare,
5306 .cleanup = playback_pcm_cleanup
5307 },
5308};
5309
5310static const struct hda_pcm_stream pcm_analog_capture = {
1da177e4
LT
5311 .substreams = 1,
5312 .channels_min = 2,
5313 .channels_max = 2,
352f7f91 5314 /* NID is set in build_pcms */
ac2e8736
TI
5315 .ops = {
5316 .open = capture_pcm_open,
5317 .close = capture_pcm_close,
5318 .prepare = capture_pcm_prepare,
5319 .cleanup = capture_pcm_cleanup
5320 },
1da177e4
LT
5321};
5322
352f7f91
TI
5323static const struct hda_pcm_stream pcm_analog_alt_playback = {
5324 .substreams = 1,
5325 .channels_min = 2,
5326 .channels_max = 2,
5327 /* NID is set in build_pcms */
38cf6f1a
TI
5328 .ops = {
5329 .open = alt_playback_pcm_open,
e6b85f3c
TI
5330 .close = alt_playback_pcm_close,
5331 .prepare = alt_playback_pcm_prepare,
5332 .cleanup = alt_playback_pcm_cleanup
38cf6f1a 5333 },
352f7f91
TI
5334};
5335
5336static const struct hda_pcm_stream pcm_analog_alt_capture = {
5337 .substreams = 2, /* can be overridden */
5338 .channels_min = 2,
5339 .channels_max = 2,
5340 /* NID is set in build_pcms */
5341 .ops = {
ac2e8736
TI
5342 .open = alt_capture_pcm_open,
5343 .close = alt_capture_pcm_close,
352f7f91
TI
5344 .prepare = alt_capture_pcm_prepare,
5345 .cleanup = alt_capture_pcm_cleanup
5346 },
5347};
5348
5349static const struct hda_pcm_stream pcm_digital_playback = {
5350 .substreams = 1,
5351 .channels_min = 2,
5352 .channels_max = 2,
5353 /* NID is set in build_pcms */
5354 .ops = {
5355 .open = dig_playback_pcm_open,
5356 .close = dig_playback_pcm_close,
5357 .prepare = dig_playback_pcm_prepare,
5358 .cleanup = dig_playback_pcm_cleanup
5359 },
5360};
5361
5362static const struct hda_pcm_stream pcm_digital_capture = {
5363 .substreams = 1,
5364 .channels_min = 2,
5365 .channels_max = 2,
5366 /* NID is set in build_pcms */
5367};
5368
5369/* Used by build_pcms to flag that a PCM has no playback stream */
5370static const struct hda_pcm_stream pcm_null_stream = {
5371 .substreams = 0,
5372 .channels_min = 0,
5373 .channels_max = 0,
5374};
5375
5376/*
5377 * dynamic changing ADC PCM streams
5378 */
5379static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
97ec558a 5380{
352f7f91
TI
5381 struct hda_gen_spec *spec = codec->spec;
5382 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
5383
5384 if (spec->cur_adc && spec->cur_adc != new_adc) {
5385 /* stream is running, let's swap the current ADC */
5386 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
5387 spec->cur_adc = new_adc;
5388 snd_hda_codec_setup_stream(codec, new_adc,
5389 spec->cur_adc_stream_tag, 0,
5390 spec->cur_adc_format);
5391 return true;
5392 }
5393 return false;
5394}
97ec558a 5395
352f7f91
TI
5396/* analog capture with dynamic dual-adc changes */
5397static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5398 struct hda_codec *codec,
5399 unsigned int stream_tag,
5400 unsigned int format,
5401 struct snd_pcm_substream *substream)
5402{
5403 struct hda_gen_spec *spec = codec->spec;
5404 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
5405 spec->cur_adc_stream_tag = stream_tag;
5406 spec->cur_adc_format = format;
5407 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
97ec558a
TI
5408 return 0;
5409}
5410
352f7f91
TI
5411static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5412 struct hda_codec *codec,
5413 struct snd_pcm_substream *substream)
97ec558a 5414{
352f7f91
TI
5415 struct hda_gen_spec *spec = codec->spec;
5416 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
5417 spec->cur_adc = 0;
97ec558a
TI
5418 return 0;
5419}
5420
352f7f91
TI
5421static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
5422 .substreams = 1,
5423 .channels_min = 2,
5424 .channels_max = 2,
5425 .nid = 0, /* fill later */
5426 .ops = {
5427 .prepare = dyn_adc_capture_pcm_prepare,
5428 .cleanup = dyn_adc_capture_pcm_cleanup
5429 },
5430};
5431
f873e536
TI
5432static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
5433 const char *chip_name)
5434{
5435 char *p;
5436
5437 if (*str)
5438 return;
5439 strlcpy(str, chip_name, len);
5440
5441 /* drop non-alnum chars after a space */
5442 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
5443 if (!isalnum(p[1])) {
5444 *p = 0;
5445 break;
5446 }
5447 }
5448 strlcat(str, sfx, len);
5449}
5450
fb83b635
TI
5451/* copy PCM stream info from @default_str, and override non-NULL entries
5452 * from @spec_str and @nid
5453 */
5454static void setup_pcm_stream(struct hda_pcm_stream *str,
5455 const struct hda_pcm_stream *default_str,
5456 const struct hda_pcm_stream *spec_str,
5457 hda_nid_t nid)
5458{
5459 *str = *default_str;
5460 if (nid)
5461 str->nid = nid;
5462 if (spec_str) {
5463 if (spec_str->substreams)
5464 str->substreams = spec_str->substreams;
5465 if (spec_str->channels_min)
5466 str->channels_min = spec_str->channels_min;
5467 if (spec_str->channels_max)
5468 str->channels_max = spec_str->channels_max;
5469 if (spec_str->rates)
5470 str->rates = spec_str->rates;
5471 if (spec_str->formats)
5472 str->formats = spec_str->formats;
5473 if (spec_str->maxbps)
5474 str->maxbps = spec_str->maxbps;
5475 }
5476}
5477
dda42bd0
TI
5478/**
5479 * snd_hda_gen_build_pcms - build PCM streams based on the parsed results
5480 * @codec: the HDA codec
5481 *
5482 * Pass this to build_pcms patch_ops.
5483 */
352f7f91 5484int snd_hda_gen_build_pcms(struct hda_codec *codec)
1da177e4 5485{
352f7f91 5486 struct hda_gen_spec *spec = codec->spec;
bbbc7e85 5487 struct hda_pcm *info;
352f7f91 5488 bool have_multi_adcs;
352f7f91 5489
352f7f91
TI
5490 if (spec->no_analog)
5491 goto skip_analog;
5492
f873e536
TI
5493 fill_pcm_stream_name(spec->stream_name_analog,
5494 sizeof(spec->stream_name_analog),
7639a06c 5495 " Analog", codec->core.chip_name);
bbbc7e85
TI
5496 info = snd_hda_codec_pcm_new(codec, "%s", spec->stream_name_analog);
5497 if (!info)
5498 return -ENOMEM;
5499 spec->pcm_rec[0] = info;
352f7f91
TI
5500
5501 if (spec->multiout.num_dacs > 0) {
fb83b635
TI
5502 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5503 &pcm_analog_playback,
5504 spec->stream_analog_playback,
5505 spec->multiout.dac_nids[0]);
352f7f91
TI
5506 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
5507 spec->multiout.max_channels;
5508 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
5509 spec->autocfg.line_outs == 2)
5510 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
5511 snd_pcm_2_1_chmaps;
5512 }
5513 if (spec->num_adc_nids) {
fb83b635
TI
5514 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5515 (spec->dyn_adc_switch ?
5516 &dyn_adc_pcm_analog_capture : &pcm_analog_capture),
5517 spec->stream_analog_capture,
5518 spec->adc_nids[0]);
352f7f91
TI
5519 }
5520
352f7f91
TI
5521 skip_analog:
5522 /* SPDIF for stream index #1 */
5523 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
f873e536
TI
5524 fill_pcm_stream_name(spec->stream_name_digital,
5525 sizeof(spec->stream_name_digital),
7639a06c 5526 " Digital", codec->core.chip_name);
bbbc7e85
TI
5527 info = snd_hda_codec_pcm_new(codec, "%s",
5528 spec->stream_name_digital);
5529 if (!info)
5530 return -ENOMEM;
352f7f91 5531 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
bbbc7e85 5532 spec->pcm_rec[1] = info;
352f7f91
TI
5533 if (spec->dig_out_type)
5534 info->pcm_type = spec->dig_out_type;
5535 else
5536 info->pcm_type = HDA_PCM_TYPE_SPDIF;
fb83b635
TI
5537 if (spec->multiout.dig_out_nid)
5538 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5539 &pcm_digital_playback,
5540 spec->stream_digital_playback,
5541 spec->multiout.dig_out_nid);
5542 if (spec->dig_in_nid)
5543 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5544 &pcm_digital_capture,
5545 spec->stream_digital_capture,
5546 spec->dig_in_nid);
352f7f91 5547 }
1da177e4 5548
352f7f91 5549 if (spec->no_analog)
1da177e4 5550 return 0;
352f7f91
TI
5551
5552 /* If the use of more than one ADC is requested for the current
5553 * model, configure a second analog capture-only PCM.
5554 */
5555 have_multi_adcs = (spec->num_adc_nids > 1) &&
5556 !spec->dyn_adc_switch && !spec->auto_mic;
5557 /* Additional Analaog capture for index #2 */
5558 if (spec->alt_dac_nid || have_multi_adcs) {
a607148f
TI
5559 fill_pcm_stream_name(spec->stream_name_alt_analog,
5560 sizeof(spec->stream_name_alt_analog),
7639a06c 5561 " Alt Analog", codec->core.chip_name);
bbbc7e85
TI
5562 info = snd_hda_codec_pcm_new(codec, "%s",
5563 spec->stream_name_alt_analog);
5564 if (!info)
5565 return -ENOMEM;
5566 spec->pcm_rec[2] = info;
fb83b635
TI
5567 if (spec->alt_dac_nid)
5568 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5569 &pcm_analog_alt_playback,
5570 spec->stream_analog_alt_playback,
5571 spec->alt_dac_nid);
5572 else
5573 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5574 &pcm_null_stream, NULL, 0);
352f7f91 5575 if (have_multi_adcs) {
fb83b635
TI
5576 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5577 &pcm_analog_alt_capture,
5578 spec->stream_analog_alt_capture,
5579 spec->adc_nids[1]);
352f7f91
TI
5580 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
5581 spec->num_adc_nids - 1;
5582 } else {
fb83b635
TI
5583 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5584 &pcm_null_stream, NULL, 0);
352f7f91 5585 }
1da177e4
LT
5586 }
5587
352f7f91
TI
5588 return 0;
5589}
2698ea98 5590EXPORT_SYMBOL_GPL(snd_hda_gen_build_pcms);
352f7f91
TI
5591
5592
5593/*
5594 * Standard auto-parser initializations
5595 */
5596
d4156930 5597/* configure the given path as a proper output */
2c12c30d 5598static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
352f7f91
TI
5599{
5600 struct nid_path *path;
d4156930 5601 hda_nid_t pin;
352f7f91 5602
196c1766 5603 path = snd_hda_get_path_from_idx(codec, path_idx);
d4156930 5604 if (!path || !path->depth)
352f7f91 5605 return;
d4156930 5606 pin = path->path[path->depth - 1];
2c12c30d 5607 restore_pin_ctl(codec, pin);
65033cc8
TI
5608 snd_hda_activate_path(codec, path, path->active,
5609 aamix_default(codec->spec));
e1284af7 5610 set_pin_eapd(codec, pin, path->active);
352f7f91
TI
5611}
5612
5613/* initialize primary output paths */
5614static void init_multi_out(struct hda_codec *codec)
5615{
5616 struct hda_gen_spec *spec = codec->spec;
352f7f91
TI
5617 int i;
5618
d4156930 5619 for (i = 0; i < spec->autocfg.line_outs; i++)
2c12c30d 5620 set_output_and_unmute(codec, spec->out_paths[i]);
352f7f91
TI
5621}
5622
db23fd19 5623
2c12c30d 5624static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
352f7f91 5625{
352f7f91 5626 int i;
352f7f91 5627
d4156930 5628 for (i = 0; i < num_outs; i++)
2c12c30d 5629 set_output_and_unmute(codec, paths[i]);
352f7f91
TI
5630}
5631
db23fd19
TI
5632/* initialize hp and speaker paths */
5633static void init_extra_out(struct hda_codec *codec)
5634{
5635 struct hda_gen_spec *spec = codec->spec;
5636
5637 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
2c12c30d 5638 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
db23fd19
TI
5639 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
5640 __init_extra_out(codec, spec->autocfg.speaker_outs,
2c12c30d 5641 spec->speaker_paths);
db23fd19
TI
5642}
5643
352f7f91
TI
5644/* initialize multi-io paths */
5645static void init_multi_io(struct hda_codec *codec)
5646{
5647 struct hda_gen_spec *spec = codec->spec;
5648 int i;
5649
5650 for (i = 0; i < spec->multi_ios; i++) {
5651 hda_nid_t pin = spec->multi_io[i].pin;
5652 struct nid_path *path;
196c1766 5653 path = get_multiio_path(codec, i);
352f7f91
TI
5654 if (!path)
5655 continue;
5656 if (!spec->multi_io[i].ctl_in)
5657 spec->multi_io[i].ctl_in =
2c12c30d 5658 snd_hda_codec_get_pin_target(codec, pin);
65033cc8
TI
5659 snd_hda_activate_path(codec, path, path->active,
5660 aamix_default(spec));
352f7f91
TI
5661 }
5662}
5663
4f7f67fb
TI
5664static void init_aamix_paths(struct hda_codec *codec)
5665{
5666 struct hda_gen_spec *spec = codec->spec;
5667
5668 if (!spec->have_aamix_ctl)
5669 return;
5670 update_aamix_paths(codec, spec->aamix_mode, spec->out_paths[0],
5671 spec->aamix_out_paths[0],
5672 spec->autocfg.line_out_type);
5673 update_aamix_paths(codec, spec->aamix_mode, spec->hp_paths[0],
5674 spec->aamix_out_paths[1],
5675 AUTO_PIN_HP_OUT);
5676 update_aamix_paths(codec, spec->aamix_mode, spec->speaker_paths[0],
5677 spec->aamix_out_paths[2],
5678 AUTO_PIN_SPEAKER_OUT);
5679}
5680
352f7f91
TI
5681/* set up input pins and loopback paths */
5682static void init_analog_input(struct hda_codec *codec)
5683{
5684 struct hda_gen_spec *spec = codec->spec;
5685 struct auto_pin_cfg *cfg = &spec->autocfg;
5686 int i;
5687
5688 for (i = 0; i < cfg->num_inputs; i++) {
5689 hda_nid_t nid = cfg->inputs[i].pin;
5690 if (is_input_pin(codec, nid))
2c12c30d 5691 restore_pin_ctl(codec, nid);
352f7f91
TI
5692
5693 /* init loopback inputs */
5694 if (spec->mixer_nid) {
3e367f15
TI
5695 resume_path_from_idx(codec, spec->loopback_paths[i]);
5696 resume_path_from_idx(codec, spec->loopback_merge_path);
352f7f91
TI
5697 }
5698 }
5699}
5700
5701/* initialize ADC paths */
5702static void init_input_src(struct hda_codec *codec)
5703{
5704 struct hda_gen_spec *spec = codec->spec;
5705 struct hda_input_mux *imux = &spec->input_mux;
5706 struct nid_path *path;
5707 int i, c, nums;
1da177e4 5708
352f7f91
TI
5709 if (spec->dyn_adc_switch)
5710 nums = 1;
5711 else
5712 nums = spec->num_adc_nids;
5713
5714 for (c = 0; c < nums; c++) {
5715 for (i = 0; i < imux->num_items; i++) {
c697b716 5716 path = get_input_path(codec, c, i);
352f7f91
TI
5717 if (path) {
5718 bool active = path->active;
5719 if (i == spec->cur_mux[c])
5720 active = true;
5721 snd_hda_activate_path(codec, path, active, false);
5722 }
97ec558a 5723 }
967303da
TI
5724 if (spec->hp_mic)
5725 update_hp_mic(codec, c, true);
1da177e4 5726 }
352f7f91 5727
352f7f91 5728 if (spec->cap_sync_hook)
7fe30711 5729 spec->cap_sync_hook(codec, NULL, NULL);
352f7f91
TI
5730}
5731
5732/* set right pin controls for digital I/O */
5733static void init_digital(struct hda_codec *codec)
5734{
5735 struct hda_gen_spec *spec = codec->spec;
5736 int i;
5737 hda_nid_t pin;
5738
d4156930 5739 for (i = 0; i < spec->autocfg.dig_outs; i++)
2c12c30d 5740 set_output_and_unmute(codec, spec->digout_paths[i]);
352f7f91 5741 pin = spec->autocfg.dig_in_pin;
2430d7b7 5742 if (pin) {
2c12c30d 5743 restore_pin_ctl(codec, pin);
3e367f15 5744 resume_path_from_idx(codec, spec->digin_path);
2430d7b7 5745 }
352f7f91
TI
5746}
5747
973e4972
TI
5748/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5749 * invalid unsol tags by some reason
5750 */
5751static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5752{
5753 int i;
5754
5755 for (i = 0; i < codec->init_pins.used; i++) {
5756 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
5757 hda_nid_t nid = pin->nid;
5758 if (is_jack_detectable(codec, nid) &&
5759 !snd_hda_jack_tbl_get(codec, nid))
5760 snd_hda_codec_update_cache(codec, nid, 0,
5761 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5762 }
5763}
5764
dda42bd0
TI
5765/**
5766 * snd_hda_gen_init - initialize the generic spec
5767 * @codec: the HDA codec
5768 *
5769 * This can be put as patch_ops init function.
5187ac16 5770 */
352f7f91
TI
5771int snd_hda_gen_init(struct hda_codec *codec)
5772{
5773 struct hda_gen_spec *spec = codec->spec;
5774
5775 if (spec->init_hook)
5776 spec->init_hook(codec);
5777
5778 snd_hda_apply_verbs(codec);
5779
5780 init_multi_out(codec);
5781 init_extra_out(codec);
5782 init_multi_io(codec);
4f7f67fb 5783 init_aamix_paths(codec);
352f7f91
TI
5784 init_analog_input(codec);
5785 init_input_src(codec);
5786 init_digital(codec);
1da177e4 5787
973e4972
TI
5788 clear_unsol_on_unused_pins(codec);
5789
e6feb5d0
TI
5790 sync_all_pin_power_ctls(codec);
5791
352f7f91 5792 /* call init functions of standard auto-mute helpers */
a5cc2509 5793 update_automute_all(codec);
352f7f91 5794
a551d914 5795 regcache_sync(codec->core.regmap);
3bbcd274 5796
352f7f91
TI
5797 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
5798 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5799
5800 hda_call_check_power_status(codec, 0x01);
1da177e4
LT
5801 return 0;
5802}
2698ea98 5803EXPORT_SYMBOL_GPL(snd_hda_gen_init);
352f7f91 5804
dda42bd0
TI
5805/**
5806 * snd_hda_gen_free - free the generic spec
5807 * @codec: the HDA codec
5808 *
5809 * This can be put as patch_ops free function.
5187ac16 5810 */
fce52a3b
TI
5811void snd_hda_gen_free(struct hda_codec *codec)
5812{
8a02c0cc 5813 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_FREE);
fce52a3b
TI
5814 snd_hda_gen_spec_free(codec->spec);
5815 kfree(codec->spec);
5816 codec->spec = NULL;
5817}
2698ea98 5818EXPORT_SYMBOL_GPL(snd_hda_gen_free);
1da177e4 5819
83012a7c 5820#ifdef CONFIG_PM
dda42bd0
TI
5821/**
5822 * snd_hda_gen_check_power_status - check the loopback power save state
5823 * @codec: the HDA codec
5824 * @nid: NID to inspect
5825 *
5826 * This can be put as patch_ops check_power_status function.
5187ac16 5827 */
fce52a3b 5828int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
cb53c626 5829{
352f7f91 5830 struct hda_gen_spec *spec = codec->spec;
cb53c626
TI
5831 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
5832}
2698ea98 5833EXPORT_SYMBOL_GPL(snd_hda_gen_check_power_status);
cb53c626
TI
5834#endif
5835
fce52a3b
TI
5836
5837/*
5838 * the generic codec support
5839 */
1da177e4 5840
352f7f91
TI
5841static const struct hda_codec_ops generic_patch_ops = {
5842 .build_controls = snd_hda_gen_build_controls,
5843 .build_pcms = snd_hda_gen_build_pcms,
5844 .init = snd_hda_gen_init,
fce52a3b 5845 .free = snd_hda_gen_free,
352f7f91 5846 .unsol_event = snd_hda_jack_unsol_event,
83012a7c 5847#ifdef CONFIG_PM
fce52a3b 5848 .check_power_status = snd_hda_gen_check_power_status,
cb53c626 5849#endif
1da177e4
LT
5850};
5851
d8a766a1 5852/*
dda42bd0
TI
5853 * snd_hda_parse_generic_codec - Generic codec parser
5854 * @codec: the HDA codec
dda42bd0 5855 */
d8a766a1 5856static int snd_hda_parse_generic_codec(struct hda_codec *codec)
1da177e4 5857{
352f7f91 5858 struct hda_gen_spec *spec;
1da177e4
LT
5859 int err;
5860
e560d8d8 5861 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
352f7f91 5862 if (!spec)
1da177e4 5863 return -ENOMEM;
352f7f91 5864 snd_hda_gen_spec_init(spec);
1da177e4 5865 codec->spec = spec;
1da177e4 5866
9eb413e5
TI
5867 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
5868 if (err < 0)
5869 return err;
5870
5871 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
352f7f91 5872 if (err < 0)
1da177e4
LT
5873 goto error;
5874
5875 codec->patch_ops = generic_patch_ops;
1da177e4
LT
5876 return 0;
5877
352f7f91 5878error:
fce52a3b 5879 snd_hda_gen_free(codec);
1da177e4
LT
5880 return err;
5881}
d8a766a1
TI
5882
5883static const struct hda_codec_preset snd_hda_preset_generic[] = {
5884 { .id = HDA_CODEC_ID_GENERIC, .patch = snd_hda_parse_generic_codec },
5885 {} /* terminator */
5886};
5887
5888static struct hda_codec_driver generic_driver = {
5889 .preset = snd_hda_preset_generic,
5890};
5891
5892module_hda_codec_driver(generic_driver);
b21bdd0d
TI
5893
5894MODULE_LICENSE("GPL");
5895MODULE_DESCRIPTION("Generic HD-audio codec parser");