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