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