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