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