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