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