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