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