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