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