GNU Linux-libre 4.14.290-gnu1
[releases.git] / sound / pci / hda / hda_generic.c
1 /*
2  * Universal Interface for Intel High Definition Audio Codec
3  *
4  * Generic widget tree parser
5  *
6  * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
7  *
8  *  This driver is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This driver is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21  */
22
23 #include <linux/init.h>
24 #include <linux/slab.h>
25 #include <linux/export.h>
26 #include <linux/sort.h>
27 #include <linux/delay.h>
28 #include <linux/ctype.h>
29 #include <linux/string.h>
30 #include <linux/bitops.h>
31 #include <linux/module.h>
32 #include <sound/core.h>
33 #include <sound/jack.h>
34 #include <sound/tlv.h>
35 #include "hda_codec.h"
36 #include "hda_local.h"
37 #include "hda_auto_parser.h"
38 #include "hda_jack.h"
39 #include "hda_beep.h"
40 #include "hda_generic.h"
41
42
43 /**
44  * snd_hda_gen_spec_init - initialize hda_gen_spec struct
45  * @spec: hda_gen_spec object to initialize
46  *
47  * Initialize the given hda_gen_spec object.
48  */
49 int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
50 {
51         snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
52         snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
53         snd_array_init(&spec->loopback_list, sizeof(struct hda_amp_list), 8);
54         mutex_init(&spec->pcm_mutex);
55         return 0;
56 }
57 EXPORT_SYMBOL_GPL(snd_hda_gen_spec_init);
58
59 /**
60  * snd_hda_gen_add_kctl - Add a new kctl_new struct from the template
61  * @spec: hda_gen_spec object
62  * @name: name string to override the template, NULL if unchanged
63  * @temp: template for the new kctl
64  *
65  * Add a new kctl (actually snd_kcontrol_new to be instantiated later)
66  * element based on the given snd_kcontrol_new template @temp and the
67  * name string @name to the list in @spec.
68  * Returns the newly created object or NULL as error.
69  */
70 struct snd_kcontrol_new *
71 snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
72                      const struct snd_kcontrol_new *temp)
73 {
74         struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
75         if (!knew)
76                 return NULL;
77         *knew = *temp;
78         if (name)
79                 knew->name = kstrdup(name, GFP_KERNEL);
80         else if (knew->name)
81                 knew->name = kstrdup(knew->name, GFP_KERNEL);
82         if (!knew->name)
83                 return NULL;
84         return knew;
85 }
86 EXPORT_SYMBOL_GPL(snd_hda_gen_add_kctl);
87
88 static void free_kctls(struct hda_gen_spec *spec)
89 {
90         if (spec->kctls.list) {
91                 struct snd_kcontrol_new *kctl = spec->kctls.list;
92                 int i;
93                 for (i = 0; i < spec->kctls.used; i++)
94                         kfree(kctl[i].name);
95         }
96         snd_array_free(&spec->kctls);
97 }
98
99 static void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
100 {
101         if (!spec)
102                 return;
103         free_kctls(spec);
104         snd_array_free(&spec->paths);
105         snd_array_free(&spec->loopback_list);
106 }
107
108 /*
109  * store user hints
110  */
111 static void parse_user_hints(struct hda_codec *codec)
112 {
113         struct hda_gen_spec *spec = codec->spec;
114         int val;
115
116         val = snd_hda_get_bool_hint(codec, "jack_detect");
117         if (val >= 0)
118                 codec->no_jack_detect = !val;
119         val = snd_hda_get_bool_hint(codec, "inv_jack_detect");
120         if (val >= 0)
121                 codec->inv_jack_detect = !!val;
122         val = snd_hda_get_bool_hint(codec, "trigger_sense");
123         if (val >= 0)
124                 codec->no_trigger_sense = !val;
125         val = snd_hda_get_bool_hint(codec, "inv_eapd");
126         if (val >= 0)
127                 codec->inv_eapd = !!val;
128         val = snd_hda_get_bool_hint(codec, "pcm_format_first");
129         if (val >= 0)
130                 codec->pcm_format_first = !!val;
131         val = snd_hda_get_bool_hint(codec, "sticky_stream");
132         if (val >= 0)
133                 codec->no_sticky_stream = !val;
134         val = snd_hda_get_bool_hint(codec, "spdif_status_reset");
135         if (val >= 0)
136                 codec->spdif_status_reset = !!val;
137         val = snd_hda_get_bool_hint(codec, "pin_amp_workaround");
138         if (val >= 0)
139                 codec->pin_amp_workaround = !!val;
140         val = snd_hda_get_bool_hint(codec, "single_adc_amp");
141         if (val >= 0)
142                 codec->single_adc_amp = !!val;
143         val = snd_hda_get_bool_hint(codec, "power_save_node");
144         if (val >= 0)
145                 codec->power_save_node = !!val;
146
147         val = snd_hda_get_bool_hint(codec, "auto_mute");
148         if (val >= 0)
149                 spec->suppress_auto_mute = !val;
150         val = snd_hda_get_bool_hint(codec, "auto_mic");
151         if (val >= 0)
152                 spec->suppress_auto_mic = !val;
153         val = snd_hda_get_bool_hint(codec, "line_in_auto_switch");
154         if (val >= 0)
155                 spec->line_in_auto_switch = !!val;
156         val = snd_hda_get_bool_hint(codec, "auto_mute_via_amp");
157         if (val >= 0)
158                 spec->auto_mute_via_amp = !!val;
159         val = snd_hda_get_bool_hint(codec, "need_dac_fix");
160         if (val >= 0)
161                 spec->need_dac_fix = !!val;
162         val = snd_hda_get_bool_hint(codec, "primary_hp");
163         if (val >= 0)
164                 spec->no_primary_hp = !val;
165         val = snd_hda_get_bool_hint(codec, "multi_io");
166         if (val >= 0)
167                 spec->no_multi_io = !val;
168         val = snd_hda_get_bool_hint(codec, "multi_cap_vol");
169         if (val >= 0)
170                 spec->multi_cap_vol = !!val;
171         val = snd_hda_get_bool_hint(codec, "inv_dmic_split");
172         if (val >= 0)
173                 spec->inv_dmic_split = !!val;
174         val = snd_hda_get_bool_hint(codec, "indep_hp");
175         if (val >= 0)
176                 spec->indep_hp = !!val;
177         val = snd_hda_get_bool_hint(codec, "add_stereo_mix_input");
178         if (val >= 0)
179                 spec->add_stereo_mix_input = !!val;
180         /* the following two are just for compatibility */
181         val = snd_hda_get_bool_hint(codec, "add_out_jack_modes");
182         if (val >= 0)
183                 spec->add_jack_modes = !!val;
184         val = snd_hda_get_bool_hint(codec, "add_in_jack_modes");
185         if (val >= 0)
186                 spec->add_jack_modes = !!val;
187         val = snd_hda_get_bool_hint(codec, "add_jack_modes");
188         if (val >= 0)
189                 spec->add_jack_modes = !!val;
190         val = snd_hda_get_bool_hint(codec, "power_down_unused");
191         if (val >= 0)
192                 spec->power_down_unused = !!val;
193         val = snd_hda_get_bool_hint(codec, "add_hp_mic");
194         if (val >= 0)
195                 spec->hp_mic = !!val;
196         val = snd_hda_get_bool_hint(codec, "hp_mic_detect");
197         if (val >= 0)
198                 spec->suppress_hp_mic_detect = !val;
199         val = snd_hda_get_bool_hint(codec, "vmaster");
200         if (val >= 0)
201                 spec->suppress_vmaster = !val;
202
203         if (!snd_hda_get_int_hint(codec, "mixer_nid", &val))
204                 spec->mixer_nid = val;
205 }
206
207 /*
208  * pin control value accesses
209  */
210
211 #define update_pin_ctl(codec, pin, val) \
212         snd_hda_codec_update_cache(codec, pin, 0, \
213                                    AC_VERB_SET_PIN_WIDGET_CONTROL, val)
214
215 /* restore the pinctl based on the cached value */
216 static inline void restore_pin_ctl(struct hda_codec *codec, hda_nid_t pin)
217 {
218         update_pin_ctl(codec, pin, snd_hda_codec_get_pin_target(codec, pin));
219 }
220
221 /* set the pinctl target value and write it if requested */
222 static void set_pin_target(struct hda_codec *codec, hda_nid_t pin,
223                            unsigned int val, bool do_write)
224 {
225         if (!pin)
226                 return;
227         val = snd_hda_correct_pin_ctl(codec, pin, val);
228         snd_hda_codec_set_pin_target(codec, pin, val);
229         if (do_write)
230                 update_pin_ctl(codec, pin, val);
231 }
232
233 /* set pinctl target values for all given pins */
234 static void set_pin_targets(struct hda_codec *codec, int num_pins,
235                             hda_nid_t *pins, unsigned int val)
236 {
237         int i;
238         for (i = 0; i < num_pins; i++)
239                 set_pin_target(codec, pins[i], val, false);
240 }
241
242 /*
243  * parsing paths
244  */
245
246 /* return the position of NID in the list, or -1 if not found */
247 static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
248 {
249         int i;
250         for (i = 0; i < nums; i++)
251                 if (list[i] == nid)
252                         return i;
253         return -1;
254 }
255
256 /* return true if the given NID is contained in the path */
257 static bool is_nid_contained(struct nid_path *path, hda_nid_t nid)
258 {
259         return find_idx_in_nid_list(nid, path->path, path->depth) >= 0;
260 }
261
262 static struct nid_path *get_nid_path(struct hda_codec *codec,
263                                      hda_nid_t from_nid, hda_nid_t to_nid,
264                                      int anchor_nid)
265 {
266         struct hda_gen_spec *spec = codec->spec;
267         int i;
268
269         for (i = 0; i < spec->paths.used; i++) {
270                 struct nid_path *path = snd_array_elem(&spec->paths, i);
271                 if (path->depth <= 0)
272                         continue;
273                 if ((!from_nid || path->path[0] == from_nid) &&
274                     (!to_nid || path->path[path->depth - 1] == to_nid)) {
275                         if (!anchor_nid ||
276                             (anchor_nid > 0 && is_nid_contained(path, anchor_nid)) ||
277                             (anchor_nid < 0 && !is_nid_contained(path, anchor_nid)))
278                                 return path;
279                 }
280         }
281         return NULL;
282 }
283
284 /**
285  * snd_hda_get_path_idx - get the index number corresponding to the path
286  * instance
287  * @codec: the HDA codec
288  * @path: nid_path object
289  *
290  * The returned index starts from 1, i.e. the actual array index with offset 1,
291  * and zero is handled as an invalid path
292  */
293 int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
294 {
295         struct hda_gen_spec *spec = codec->spec;
296         struct nid_path *array = spec->paths.list;
297         ssize_t idx;
298
299         if (!spec->paths.used)
300                 return 0;
301         idx = path - array;
302         if (idx < 0 || idx >= spec->paths.used)
303                 return 0;
304         return idx + 1;
305 }
306 EXPORT_SYMBOL_GPL(snd_hda_get_path_idx);
307
308 /**
309  * snd_hda_get_path_from_idx - get the path instance corresponding to the
310  * given index number
311  * @codec: the HDA codec
312  * @idx: the path index
313  */
314 struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
315 {
316         struct hda_gen_spec *spec = codec->spec;
317
318         if (idx <= 0 || idx > spec->paths.used)
319                 return NULL;
320         return snd_array_elem(&spec->paths, idx - 1);
321 }
322 EXPORT_SYMBOL_GPL(snd_hda_get_path_from_idx);
323
324 /* check whether the given DAC is already found in any existing paths */
325 static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
326 {
327         struct hda_gen_spec *spec = codec->spec;
328         int i;
329
330         for (i = 0; i < spec->paths.used; i++) {
331                 struct nid_path *path = snd_array_elem(&spec->paths, i);
332                 if (path->path[0] == nid)
333                         return true;
334         }
335         return false;
336 }
337
338 /* check whether the given two widgets can be connected */
339 static bool is_reachable_path(struct hda_codec *codec,
340                               hda_nid_t from_nid, hda_nid_t to_nid)
341 {
342         if (!from_nid || !to_nid)
343                 return false;
344         return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
345 }
346
347 /* nid, dir and idx */
348 #define AMP_VAL_COMPARE_MASK    (0xffff | (1U << 18) | (0x0f << 19))
349
350 /* check whether the given ctl is already assigned in any path elements */
351 static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
352 {
353         struct hda_gen_spec *spec = codec->spec;
354         int i;
355
356         val &= AMP_VAL_COMPARE_MASK;
357         for (i = 0; i < spec->paths.used; i++) {
358                 struct nid_path *path = snd_array_elem(&spec->paths, i);
359                 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
360                         return true;
361         }
362         return false;
363 }
364
365 /* check whether a control with the given (nid, dir, idx) was assigned */
366 static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
367                               int dir, int idx, int type)
368 {
369         unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
370         return is_ctl_used(codec, val, type);
371 }
372
373 static void print_nid_path(struct hda_codec *codec,
374                            const char *pfx, struct nid_path *path)
375 {
376         char buf[40];
377         char *pos = buf;
378         int i;
379
380         *pos = 0;
381         for (i = 0; i < path->depth; i++)
382                 pos += scnprintf(pos, sizeof(buf) - (pos - buf), "%s%02x",
383                                  pos != buf ? ":" : "",
384                                  path->path[i]);
385
386         codec_dbg(codec, "%s path: depth=%d '%s'\n", pfx, path->depth, buf);
387 }
388
389 /* called recursively */
390 static bool __parse_nid_path(struct hda_codec *codec,
391                              hda_nid_t from_nid, hda_nid_t to_nid,
392                              int anchor_nid, struct nid_path *path,
393                              int depth)
394 {
395         const hda_nid_t *conn;
396         int i, nums;
397
398         if (to_nid == anchor_nid)
399                 anchor_nid = 0; /* anchor passed */
400         else if (to_nid == (hda_nid_t)(-anchor_nid))
401                 return false; /* hit the exclusive nid */
402
403         nums = snd_hda_get_conn_list(codec, to_nid, &conn);
404         for (i = 0; i < nums; i++) {
405                 if (conn[i] != from_nid) {
406                         /* special case: when from_nid is 0,
407                          * try to find an empty DAC
408                          */
409                         if (from_nid ||
410                             get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
411                             is_dac_already_used(codec, conn[i]))
412                                 continue;
413                 }
414                 /* anchor is not requested or already passed? */
415                 if (anchor_nid <= 0)
416                         goto found;
417         }
418         if (depth >= MAX_NID_PATH_DEPTH)
419                 return false;
420         for (i = 0; i < nums; i++) {
421                 unsigned int type;
422                 type = get_wcaps_type(get_wcaps(codec, conn[i]));
423                 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
424                     type == AC_WID_PIN)
425                         continue;
426                 if (__parse_nid_path(codec, from_nid, conn[i],
427                                      anchor_nid, path, depth + 1))
428                         goto found;
429         }
430         return false;
431
432  found:
433         path->path[path->depth] = conn[i];
434         path->idx[path->depth + 1] = i;
435         if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
436                 path->multi[path->depth + 1] = 1;
437         path->depth++;
438         return true;
439 }
440
441 /*
442  * snd_hda_parse_nid_path - parse the widget path from the given nid to
443  * the target nid
444  * @codec: the HDA codec
445  * @from_nid: the NID where the path start from
446  * @to_nid: the NID where the path ends at
447  * @anchor_nid: the anchor indication
448  * @path: the path object to store the result
449  *
450  * Returns true if a matching path is found.
451  *
452  * The parsing behavior depends on parameters:
453  * when @from_nid is 0, try to find an empty DAC;
454  * when @anchor_nid is set to a positive value, only paths through the widget
455  * with the given value are evaluated.
456  * when @anchor_nid is set to a negative value, paths through the widget
457  * with the negative of given value are excluded, only other paths are chosen.
458  * when @anchor_nid is zero, no special handling about path selection.
459  */
460 static bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
461                             hda_nid_t to_nid, int anchor_nid,
462                             struct nid_path *path)
463 {
464         if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) {
465                 path->path[path->depth] = to_nid;
466                 path->depth++;
467                 return true;
468         }
469         return false;
470 }
471
472 /**
473  * snd_hda_add_new_path - parse the path between the given NIDs and
474  * add to the path list
475  * @codec: the HDA codec
476  * @from_nid: the NID where the path start from
477  * @to_nid: the NID where the path ends at
478  * @anchor_nid: the anchor indication, see snd_hda_parse_nid_path()
479  *
480  * If no valid path is found, returns NULL.
481  */
482 struct nid_path *
483 snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
484                      hda_nid_t to_nid, int anchor_nid)
485 {
486         struct hda_gen_spec *spec = codec->spec;
487         struct nid_path *path;
488
489         if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
490                 return NULL;
491
492         /* check whether the path has been already added */
493         path = get_nid_path(codec, from_nid, to_nid, anchor_nid);
494         if (path)
495                 return path;
496
497         path = snd_array_new(&spec->paths);
498         if (!path)
499                 return NULL;
500         memset(path, 0, sizeof(*path));
501         if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path))
502                 return path;
503         /* push back */
504         spec->paths.used--;
505         return NULL;
506 }
507 EXPORT_SYMBOL_GPL(snd_hda_add_new_path);
508
509 /* clear the given path as invalid so that it won't be picked up later */
510 static void invalidate_nid_path(struct hda_codec *codec, int idx)
511 {
512         struct nid_path *path = snd_hda_get_path_from_idx(codec, idx);
513         if (!path)
514                 return;
515         memset(path, 0, sizeof(*path));
516 }
517
518 /* return a DAC if paired to the given pin by codec driver */
519 static hda_nid_t get_preferred_dac(struct hda_codec *codec, hda_nid_t pin)
520 {
521         struct hda_gen_spec *spec = codec->spec;
522         const hda_nid_t *list = spec->preferred_dacs;
523
524         if (!list)
525                 return 0;
526         for (; *list; list += 2)
527                 if (*list == pin)
528                         return list[1];
529         return 0;
530 }
531
532 /* look for an empty DAC slot */
533 static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
534                               bool is_digital)
535 {
536         struct hda_gen_spec *spec = codec->spec;
537         bool cap_digital;
538         int i;
539
540         for (i = 0; i < spec->num_all_dacs; i++) {
541                 hda_nid_t nid = spec->all_dacs[i];
542                 if (!nid || is_dac_already_used(codec, nid))
543                         continue;
544                 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
545                 if (is_digital != cap_digital)
546                         continue;
547                 if (is_reachable_path(codec, nid, pin))
548                         return nid;
549         }
550         return 0;
551 }
552
553 /* replace the channels in the composed amp value with the given number */
554 static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
555 {
556         val &= ~(0x3U << 16);
557         val |= chs << 16;
558         return val;
559 }
560
561 static bool same_amp_caps(struct hda_codec *codec, hda_nid_t nid1,
562                           hda_nid_t nid2, int dir)
563 {
564         if (!(get_wcaps(codec, nid1) & (1 << (dir + 1))))
565                 return !(get_wcaps(codec, nid2) & (1 << (dir + 1)));
566         return (query_amp_caps(codec, nid1, dir) ==
567                 query_amp_caps(codec, nid2, dir));
568 }
569
570 /* look for a widget suitable for assigning a mute switch in the path */
571 static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
572                                        struct nid_path *path)
573 {
574         int i;
575
576         for (i = path->depth - 1; i >= 0; i--) {
577                 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
578                         return path->path[i];
579                 if (i != path->depth - 1 && i != 0 &&
580                     nid_has_mute(codec, path->path[i], HDA_INPUT))
581                         return path->path[i];
582         }
583         return 0;
584 }
585
586 /* look for a widget suitable for assigning a volume ctl in the path */
587 static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
588                                       struct nid_path *path)
589 {
590         struct hda_gen_spec *spec = codec->spec;
591         int i;
592
593         for (i = path->depth - 1; i >= 0; i--) {
594                 hda_nid_t nid = path->path[i];
595                 if ((spec->out_vol_mask >> nid) & 1)
596                         continue;
597                 if (nid_has_volume(codec, nid, HDA_OUTPUT))
598                         return nid;
599         }
600         return 0;
601 }
602
603 /*
604  * path activation / deactivation
605  */
606
607 /* can have the amp-in capability? */
608 static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
609 {
610         hda_nid_t nid = path->path[idx];
611         unsigned int caps = get_wcaps(codec, nid);
612         unsigned int type = get_wcaps_type(caps);
613
614         if (!(caps & AC_WCAP_IN_AMP))
615                 return false;
616         if (type == AC_WID_PIN && idx > 0) /* only for input pins */
617                 return false;
618         return true;
619 }
620
621 /* can have the amp-out capability? */
622 static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
623 {
624         hda_nid_t nid = path->path[idx];
625         unsigned int caps = get_wcaps(codec, nid);
626         unsigned int type = get_wcaps_type(caps);
627
628         if (!(caps & AC_WCAP_OUT_AMP))
629                 return false;
630         if (type == AC_WID_PIN && !idx) /* only for output pins */
631                 return false;
632         return true;
633 }
634
635 /* check whether the given (nid,dir,idx) is active */
636 static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
637                           unsigned int dir, unsigned int idx)
638 {
639         struct hda_gen_spec *spec = codec->spec;
640         int type = get_wcaps_type(get_wcaps(codec, nid));
641         int i, n;
642
643         if (nid == codec->core.afg)
644                 return true;
645
646         for (n = 0; n < spec->paths.used; n++) {
647                 struct nid_path *path = snd_array_elem(&spec->paths, n);
648                 if (!path->active)
649                         continue;
650                 if (codec->power_save_node) {
651                         if (!path->stream_enabled)
652                                 continue;
653                         /* ignore unplugged paths except for DAC/ADC */
654                         if (!(path->pin_enabled || path->pin_fixed) &&
655                             type != AC_WID_AUD_OUT && type != AC_WID_AUD_IN)
656                                 continue;
657                 }
658                 for (i = 0; i < path->depth; i++) {
659                         if (path->path[i] == nid) {
660                                 if (dir == HDA_OUTPUT || idx == -1 ||
661                                     path->idx[i] == idx)
662                                         return true;
663                                 break;
664                         }
665                 }
666         }
667         return false;
668 }
669
670 /* check whether the NID is referred by any active paths */
671 #define is_active_nid_for_any(codec, nid) \
672         is_active_nid(codec, nid, HDA_OUTPUT, -1)
673
674 /* get the default amp value for the target state */
675 static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
676                                    int dir, unsigned int caps, bool enable)
677 {
678         unsigned int val = 0;
679
680         if (caps & AC_AMPCAP_NUM_STEPS) {
681                 /* set to 0dB */
682                 if (enable)
683                         val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
684         }
685         if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
686                 if (!enable)
687                         val |= HDA_AMP_MUTE;
688         }
689         return val;
690 }
691
692 /* is this a stereo widget or a stereo-to-mono mix? */
693 static bool is_stereo_amps(struct hda_codec *codec, hda_nid_t nid, int dir)
694 {
695         unsigned int wcaps = get_wcaps(codec, nid);
696         hda_nid_t conn;
697
698         if (wcaps & AC_WCAP_STEREO)
699                 return true;
700         if (dir != HDA_INPUT || get_wcaps_type(wcaps) != AC_WID_AUD_MIX)
701                 return false;
702         if (snd_hda_get_num_conns(codec, nid) != 1)
703                 return false;
704         if (snd_hda_get_connections(codec, nid, &conn, 1) < 0)
705                 return false;
706         return !!(get_wcaps(codec, conn) & AC_WCAP_STEREO);
707 }
708
709 /* initialize the amp value (only at the first time) */
710 static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
711 {
712         unsigned int caps = query_amp_caps(codec, nid, dir);
713         int val = get_amp_val_to_activate(codec, nid, dir, caps, false);
714
715         if (is_stereo_amps(codec, nid, dir))
716                 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
717         else
718                 snd_hda_codec_amp_init(codec, nid, 0, dir, idx, 0xff, val);
719 }
720
721 /* update the amp, doing in stereo or mono depending on NID */
722 static int update_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx,
723                       unsigned int mask, unsigned int val)
724 {
725         if (is_stereo_amps(codec, nid, dir))
726                 return snd_hda_codec_amp_stereo(codec, nid, dir, idx,
727                                                 mask, val);
728         else
729                 return snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
730                                                 mask, val);
731 }
732
733 /* calculate amp value mask we can modify;
734  * if the given amp is controlled by mixers, don't touch it
735  */
736 static unsigned int get_amp_mask_to_modify(struct hda_codec *codec,
737                                            hda_nid_t nid, int dir, int idx,
738                                            unsigned int caps)
739 {
740         unsigned int mask = 0xff;
741
742         if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
743                 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_MUTE_CTL))
744                         mask &= ~0x80;
745         }
746         if (caps & AC_AMPCAP_NUM_STEPS) {
747                 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
748                     is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
749                         mask &= ~0x7f;
750         }
751         return mask;
752 }
753
754 static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
755                          int idx, int idx_to_check, bool enable)
756 {
757         unsigned int caps;
758         unsigned int mask, val;
759
760         caps = query_amp_caps(codec, nid, dir);
761         val = get_amp_val_to_activate(codec, nid, dir, caps, enable);
762         mask = get_amp_mask_to_modify(codec, nid, dir, idx_to_check, caps);
763         if (!mask)
764                 return;
765
766         val &= mask;
767         update_amp(codec, nid, dir, idx, mask, val);
768 }
769
770 static void check_and_activate_amp(struct hda_codec *codec, hda_nid_t nid,
771                                    int dir, int idx, int idx_to_check,
772                                    bool enable)
773 {
774         /* check whether the given amp is still used by others */
775         if (!enable && is_active_nid(codec, nid, dir, idx_to_check))
776                 return;
777         activate_amp(codec, nid, dir, idx, idx_to_check, enable);
778 }
779
780 static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
781                              int i, bool enable)
782 {
783         hda_nid_t nid = path->path[i];
784         init_amp(codec, nid, HDA_OUTPUT, 0);
785         check_and_activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable);
786 }
787
788 static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
789                             int i, bool enable, bool add_aamix)
790 {
791         struct hda_gen_spec *spec = codec->spec;
792         const hda_nid_t *conn;
793         int n, nums, idx;
794         int type;
795         hda_nid_t nid = path->path[i];
796
797         nums = snd_hda_get_conn_list(codec, nid, &conn);
798         type = get_wcaps_type(get_wcaps(codec, nid));
799         if (type == AC_WID_PIN ||
800             (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
801                 nums = 1;
802                 idx = 0;
803         } else
804                 idx = path->idx[i];
805
806         for (n = 0; n < nums; n++)
807                 init_amp(codec, nid, HDA_INPUT, n);
808
809         /* here is a little bit tricky in comparison with activate_amp_out();
810          * when aa-mixer is available, we need to enable the path as well
811          */
812         for (n = 0; n < nums; n++) {
813                 if (n != idx) {
814                         if (conn[n] != spec->mixer_merge_nid)
815                                 continue;
816                         /* when aamix is disabled, force to off */
817                         if (!add_aamix) {
818                                 activate_amp(codec, nid, HDA_INPUT, n, n, false);
819                                 continue;
820                         }
821                 }
822                 check_and_activate_amp(codec, nid, HDA_INPUT, n, idx, enable);
823         }
824 }
825
826 /* sync power of each widget in the given path */
827 static hda_nid_t path_power_update(struct hda_codec *codec,
828                                    struct nid_path *path,
829                                    bool allow_powerdown)
830 {
831         hda_nid_t nid, changed = 0;
832         int i, state, power;
833
834         for (i = 0; i < path->depth; i++) {
835                 nid = path->path[i];
836                 if (!(get_wcaps(codec, nid) & AC_WCAP_POWER))
837                         continue;
838                 if (nid == codec->core.afg)
839                         continue;
840                 if (!allow_powerdown || is_active_nid_for_any(codec, nid))
841                         state = AC_PWRST_D0;
842                 else
843                         state = AC_PWRST_D3;
844                 power = snd_hda_codec_read(codec, nid, 0,
845                                            AC_VERB_GET_POWER_STATE, 0);
846                 if (power != (state | (state << 4))) {
847                         snd_hda_codec_write(codec, nid, 0,
848                                             AC_VERB_SET_POWER_STATE, state);
849                         changed = nid;
850                         /* all known codecs seem to be capable to handl
851                          * widgets state even in D3, so far.
852                          * if any new codecs need to restore the widget
853                          * states after D0 transition, call the function
854                          * below.
855                          */
856 #if 0 /* disabled */
857                         if (state == AC_PWRST_D0)
858                                 snd_hdac_regmap_sync_node(&codec->core, nid);
859 #endif
860                 }
861         }
862         return changed;
863 }
864
865 /* do sync with the last power state change */
866 static void sync_power_state_change(struct hda_codec *codec, hda_nid_t nid)
867 {
868         if (nid) {
869                 msleep(10);
870                 snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
871         }
872 }
873
874 /**
875  * snd_hda_activate_path - activate or deactivate the given path
876  * @codec: the HDA codec
877  * @path: the path to activate/deactivate
878  * @enable: flag to activate or not
879  * @add_aamix: enable the input from aamix NID
880  *
881  * If @add_aamix is set, enable the input from aa-mix NID as well (if any).
882  */
883 void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
884                            bool enable, bool add_aamix)
885 {
886         struct hda_gen_spec *spec = codec->spec;
887         int i;
888
889         path->active = enable;
890
891         /* make sure the widget is powered up */
892         if (enable && (spec->power_down_unused || codec->power_save_node))
893                 path_power_update(codec, path, codec->power_save_node);
894
895         for (i = path->depth - 1; i >= 0; i--) {
896                 hda_nid_t nid = path->path[i];
897
898                 if (enable && path->multi[i])
899                         snd_hda_codec_update_cache(codec, nid, 0,
900                                             AC_VERB_SET_CONNECT_SEL,
901                                             path->idx[i]);
902                 if (has_amp_in(codec, path, i))
903                         activate_amp_in(codec, path, i, enable, add_aamix);
904                 if (has_amp_out(codec, path, i))
905                         activate_amp_out(codec, path, i, enable);
906         }
907 }
908 EXPORT_SYMBOL_GPL(snd_hda_activate_path);
909
910 /* if the given path is inactive, put widgets into D3 (only if suitable) */
911 static void path_power_down_sync(struct hda_codec *codec, struct nid_path *path)
912 {
913         struct hda_gen_spec *spec = codec->spec;
914
915         if (!(spec->power_down_unused || codec->power_save_node) || path->active)
916                 return;
917         sync_power_state_change(codec, path_power_update(codec, path, true));
918 }
919
920 /* turn on/off EAPD on the given pin */
921 static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
922 {
923         struct hda_gen_spec *spec = codec->spec;
924         if (spec->own_eapd_ctl ||
925             !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
926                 return;
927         if (spec->keep_eapd_on && !enable)
928                 return;
929         if (codec->inv_eapd)
930                 enable = !enable;
931         snd_hda_codec_update_cache(codec, pin, 0,
932                                    AC_VERB_SET_EAPD_BTLENABLE,
933                                    enable ? 0x02 : 0x00);
934 }
935
936 /* re-initialize the path specified by the given path index */
937 static void resume_path_from_idx(struct hda_codec *codec, int path_idx)
938 {
939         struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
940         if (path)
941                 snd_hda_activate_path(codec, path, path->active, false);
942 }
943
944
945 /*
946  * Helper functions for creating mixer ctl elements
947  */
948
949 static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
950                                   struct snd_ctl_elem_value *ucontrol);
951 static int hda_gen_bind_mute_get(struct snd_kcontrol *kcontrol,
952                                  struct snd_ctl_elem_value *ucontrol);
953 static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
954                                  struct snd_ctl_elem_value *ucontrol);
955
956 enum {
957         HDA_CTL_WIDGET_VOL,
958         HDA_CTL_WIDGET_MUTE,
959         HDA_CTL_BIND_MUTE,
960 };
961 static const struct snd_kcontrol_new control_templates[] = {
962         HDA_CODEC_VOLUME(NULL, 0, 0, 0),
963         /* only the put callback is replaced for handling the special mute */
964         {
965                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
966                 .subdevice = HDA_SUBDEV_AMP_FLAG,
967                 .info = snd_hda_mixer_amp_switch_info,
968                 .get = snd_hda_mixer_amp_switch_get,
969                 .put = hda_gen_mixer_mute_put, /* replaced */
970                 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
971         },
972         {
973                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
974                 .info = snd_hda_mixer_amp_switch_info,
975                 .get = hda_gen_bind_mute_get,
976                 .put = hda_gen_bind_mute_put, /* replaced */
977                 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
978         },
979 };
980
981 /* add dynamic controls from template */
982 static struct snd_kcontrol_new *
983 add_control(struct hda_gen_spec *spec, int type, const char *name,
984                        int cidx, unsigned long val)
985 {
986         struct snd_kcontrol_new *knew;
987
988         knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
989         if (!knew)
990                 return NULL;
991         knew->index = cidx;
992         if (get_amp_nid_(val))
993                 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
994         knew->private_value = val;
995         return knew;
996 }
997
998 static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
999                                 const char *pfx, const char *dir,
1000                                 const char *sfx, int cidx, unsigned long val)
1001 {
1002         char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1003         snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
1004         if (!add_control(spec, type, name, cidx, val))
1005                 return -ENOMEM;
1006         return 0;
1007 }
1008
1009 #define add_pb_vol_ctrl(spec, type, pfx, val)                   \
1010         add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
1011 #define add_pb_sw_ctrl(spec, type, pfx, val)                    \
1012         add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
1013 #define __add_pb_vol_ctrl(spec, type, pfx, cidx, val)                   \
1014         add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
1015 #define __add_pb_sw_ctrl(spec, type, pfx, cidx, val)                    \
1016         add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
1017
1018 static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
1019                        unsigned int chs, struct nid_path *path)
1020 {
1021         unsigned int val;
1022         if (!path)
1023                 return 0;
1024         val = path->ctls[NID_PATH_VOL_CTL];
1025         if (!val)
1026                 return 0;
1027         val = amp_val_replace_channels(val, chs);
1028         return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
1029 }
1030
1031 /* return the channel bits suitable for the given path->ctls[] */
1032 static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
1033                                int type)
1034 {
1035         int chs = 1; /* mono (left only) */
1036         if (path) {
1037                 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
1038                 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
1039                         chs = 3; /* stereo */
1040         }
1041         return chs;
1042 }
1043
1044 static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
1045                           struct nid_path *path)
1046 {
1047         int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
1048         return add_vol_ctl(codec, pfx, cidx, chs, path);
1049 }
1050
1051 /* create a mute-switch for the given mixer widget;
1052  * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
1053  */
1054 static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
1055                       unsigned int chs, struct nid_path *path)
1056 {
1057         unsigned int val;
1058         int type = HDA_CTL_WIDGET_MUTE;
1059
1060         if (!path)
1061                 return 0;
1062         val = path->ctls[NID_PATH_MUTE_CTL];
1063         if (!val)
1064                 return 0;
1065         val = amp_val_replace_channels(val, chs);
1066         if (get_amp_direction_(val) == HDA_INPUT) {
1067                 hda_nid_t nid = get_amp_nid_(val);
1068                 int nums = snd_hda_get_num_conns(codec, nid);
1069                 if (nums > 1) {
1070                         type = HDA_CTL_BIND_MUTE;
1071                         val |= nums << 19;
1072                 }
1073         }
1074         return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
1075 }
1076
1077 static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
1078                                   int cidx, struct nid_path *path)
1079 {
1080         int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
1081         return add_sw_ctl(codec, pfx, cidx, chs, path);
1082 }
1083
1084 /* playback mute control with the software mute bit check */
1085 static void sync_auto_mute_bits(struct snd_kcontrol *kcontrol,
1086                                 struct snd_ctl_elem_value *ucontrol)
1087 {
1088         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1089         struct hda_gen_spec *spec = codec->spec;
1090
1091         if (spec->auto_mute_via_amp) {
1092                 hda_nid_t nid = get_amp_nid(kcontrol);
1093                 bool enabled = !((spec->mute_bits >> nid) & 1);
1094                 ucontrol->value.integer.value[0] &= enabled;
1095                 ucontrol->value.integer.value[1] &= enabled;
1096         }
1097 }
1098
1099 static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
1100                                   struct snd_ctl_elem_value *ucontrol)
1101 {
1102         sync_auto_mute_bits(kcontrol, ucontrol);
1103         return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1104 }
1105
1106 /*
1107  * Bound mute controls
1108  */
1109 #define AMP_VAL_IDX_SHIFT       19
1110 #define AMP_VAL_IDX_MASK        (0x0f<<19)
1111
1112 static int hda_gen_bind_mute_get(struct snd_kcontrol *kcontrol,
1113                                  struct snd_ctl_elem_value *ucontrol)
1114 {
1115         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1116         unsigned long pval;
1117         int err;
1118
1119         mutex_lock(&codec->control_mutex);
1120         pval = kcontrol->private_value;
1121         kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
1122         err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
1123         kcontrol->private_value = pval;
1124         mutex_unlock(&codec->control_mutex);
1125         return err;
1126 }
1127
1128 static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
1129                                  struct snd_ctl_elem_value *ucontrol)
1130 {
1131         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1132         unsigned long pval;
1133         int i, indices, err = 0, change = 0;
1134
1135         sync_auto_mute_bits(kcontrol, ucontrol);
1136
1137         mutex_lock(&codec->control_mutex);
1138         pval = kcontrol->private_value;
1139         indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
1140         for (i = 0; i < indices; i++) {
1141                 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
1142                         (i << AMP_VAL_IDX_SHIFT);
1143                 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1144                 if (err < 0)
1145                         break;
1146                 change |= err;
1147         }
1148         kcontrol->private_value = pval;
1149         mutex_unlock(&codec->control_mutex);
1150         return err < 0 ? err : change;
1151 }
1152
1153 /* any ctl assigned to the path with the given index? */
1154 static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
1155 {
1156         struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
1157         return path && path->ctls[ctl_type];
1158 }
1159
1160 static const char * const channel_name[4] = {
1161         "Front", "Surround", "CLFE", "Side"
1162 };
1163
1164 /* give some appropriate ctl name prefix for the given line out channel */
1165 static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
1166                                     int *index, int ctl_type)
1167 {
1168         struct hda_gen_spec *spec = codec->spec;
1169         struct auto_pin_cfg *cfg = &spec->autocfg;
1170
1171         *index = 0;
1172         if (cfg->line_outs == 1 && !spec->multi_ios &&
1173             !codec->force_pin_prefix &&
1174             !cfg->hp_outs && !cfg->speaker_outs)
1175                 return spec->vmaster_mute.hook ? "PCM" : "Master";
1176
1177         /* if there is really a single DAC used in the whole output paths,
1178          * use it master (or "PCM" if a vmaster hook is present)
1179          */
1180         if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
1181             !codec->force_pin_prefix &&
1182             !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
1183                 return spec->vmaster_mute.hook ? "PCM" : "Master";
1184
1185         /* multi-io channels */
1186         if (ch >= cfg->line_outs)
1187                 return channel_name[ch];
1188
1189         switch (cfg->line_out_type) {
1190         case AUTO_PIN_SPEAKER_OUT:
1191                 /* if the primary channel vol/mute is shared with HP volume,
1192                  * don't name it as Speaker
1193                  */
1194                 if (!ch && cfg->hp_outs &&
1195                     !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
1196                         break;
1197                 if (cfg->line_outs == 1)
1198                         return "Speaker";
1199                 if (cfg->line_outs == 2)
1200                         return ch ? "Bass Speaker" : "Speaker";
1201                 break;
1202         case AUTO_PIN_HP_OUT:
1203                 /* if the primary channel vol/mute is shared with spk volume,
1204                  * don't name it as Headphone
1205                  */
1206                 if (!ch && cfg->speaker_outs &&
1207                     !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
1208                         break;
1209                 /* for multi-io case, only the primary out */
1210                 if (ch && spec->multi_ios)
1211                         break;
1212                 *index = ch;
1213                 return "Headphone";
1214         case AUTO_PIN_LINE_OUT:
1215                 /* This deals with the case where one HP or one Speaker or
1216                  * one HP + one Speaker need to share the DAC with LO
1217                  */
1218                 if (!ch) {
1219                         bool hp_lo_shared = false, spk_lo_shared = false;
1220
1221                         if (cfg->speaker_outs)
1222                                 spk_lo_shared = !path_has_mixer(codec,
1223                                                                 spec->speaker_paths[0], ctl_type);
1224                         if (cfg->hp_outs)
1225                                 hp_lo_shared = !path_has_mixer(codec, spec->hp_paths[0], ctl_type);
1226                         if (hp_lo_shared && spk_lo_shared)
1227                                 return spec->vmaster_mute.hook ? "PCM" : "Master";
1228                         if (hp_lo_shared)
1229                                 return "Headphone+LO";
1230                         if (spk_lo_shared)
1231                                 return "Speaker+LO";
1232                 }
1233         }
1234
1235         /* for a single channel output, we don't have to name the channel */
1236         if (cfg->line_outs == 1 && !spec->multi_ios)
1237                 return "Line Out";
1238
1239         if (ch >= ARRAY_SIZE(channel_name)) {
1240                 snd_BUG();
1241                 return "PCM";
1242         }
1243
1244         return channel_name[ch];
1245 }
1246
1247 /*
1248  * Parse output paths
1249  */
1250
1251 /* badness definition */
1252 enum {
1253         /* No primary DAC is found for the main output */
1254         BAD_NO_PRIMARY_DAC = 0x10000,
1255         /* No DAC is found for the extra output */
1256         BAD_NO_DAC = 0x4000,
1257         /* No possible multi-ios */
1258         BAD_MULTI_IO = 0x120,
1259         /* No individual DAC for extra output */
1260         BAD_NO_EXTRA_DAC = 0x102,
1261         /* No individual DAC for extra surrounds */
1262         BAD_NO_EXTRA_SURR_DAC = 0x101,
1263         /* Primary DAC shared with main surrounds */
1264         BAD_SHARED_SURROUND = 0x100,
1265         /* No independent HP possible */
1266         BAD_NO_INDEP_HP = 0x10,
1267         /* Primary DAC shared with main CLFE */
1268         BAD_SHARED_CLFE = 0x10,
1269         /* Primary DAC shared with extra surrounds */
1270         BAD_SHARED_EXTRA_SURROUND = 0x10,
1271         /* Volume widget is shared */
1272         BAD_SHARED_VOL = 0x10,
1273 };
1274
1275 /* look for widgets in the given path which are appropriate for
1276  * volume and mute controls, and assign the values to ctls[].
1277  *
1278  * When no appropriate widget is found in the path, the badness value
1279  * is incremented depending on the situation.  The function returns the
1280  * total badness for both volume and mute controls.
1281  */
1282 static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
1283 {
1284         struct hda_gen_spec *spec = codec->spec;
1285         hda_nid_t nid;
1286         unsigned int val;
1287         int badness = 0;
1288
1289         if (!path)
1290                 return BAD_SHARED_VOL * 2;
1291
1292         if (path->ctls[NID_PATH_VOL_CTL] ||
1293             path->ctls[NID_PATH_MUTE_CTL])
1294                 return 0; /* already evaluated */
1295
1296         nid = look_for_out_vol_nid(codec, path);
1297         if (nid) {
1298                 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1299                 if (spec->dac_min_mute)
1300                         val |= HDA_AMP_VAL_MIN_MUTE;
1301                 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1302                         badness += BAD_SHARED_VOL;
1303                 else
1304                         path->ctls[NID_PATH_VOL_CTL] = val;
1305         } else
1306                 badness += BAD_SHARED_VOL;
1307         nid = look_for_out_mute_nid(codec, path);
1308         if (nid) {
1309                 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1310                 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1311                     nid_has_mute(codec, nid, HDA_OUTPUT))
1312                         val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1313                 else
1314                         val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1315                 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1316                         badness += BAD_SHARED_VOL;
1317                 else
1318                         path->ctls[NID_PATH_MUTE_CTL] = val;
1319         } else
1320                 badness += BAD_SHARED_VOL;
1321         return badness;
1322 }
1323
1324 const struct badness_table hda_main_out_badness = {
1325         .no_primary_dac = BAD_NO_PRIMARY_DAC,
1326         .no_dac = BAD_NO_DAC,
1327         .shared_primary = BAD_NO_PRIMARY_DAC,
1328         .shared_surr = BAD_SHARED_SURROUND,
1329         .shared_clfe = BAD_SHARED_CLFE,
1330         .shared_surr_main = BAD_SHARED_SURROUND,
1331 };
1332 EXPORT_SYMBOL_GPL(hda_main_out_badness);
1333
1334 const struct badness_table hda_extra_out_badness = {
1335         .no_primary_dac = BAD_NO_DAC,
1336         .no_dac = BAD_NO_DAC,
1337         .shared_primary = BAD_NO_EXTRA_DAC,
1338         .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1339         .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1340         .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1341 };
1342 EXPORT_SYMBOL_GPL(hda_extra_out_badness);
1343
1344 /* get the DAC of the primary output corresponding to the given array index */
1345 static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1346 {
1347         struct hda_gen_spec *spec = codec->spec;
1348         struct auto_pin_cfg *cfg = &spec->autocfg;
1349
1350         if (cfg->line_outs > idx)
1351                 return spec->private_dac_nids[idx];
1352         idx -= cfg->line_outs;
1353         if (spec->multi_ios > idx)
1354                 return spec->multi_io[idx].dac;
1355         return 0;
1356 }
1357
1358 /* return the DAC if it's reachable, otherwise zero */
1359 static inline hda_nid_t try_dac(struct hda_codec *codec,
1360                                 hda_nid_t dac, hda_nid_t pin)
1361 {
1362         return is_reachable_path(codec, dac, pin) ? dac : 0;
1363 }
1364
1365 /* try to assign DACs to pins and return the resultant badness */
1366 static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1367                            const hda_nid_t *pins, hda_nid_t *dacs,
1368                            int *path_idx,
1369                            const struct badness_table *bad)
1370 {
1371         struct hda_gen_spec *spec = codec->spec;
1372         int i, j;
1373         int badness = 0;
1374         hda_nid_t dac;
1375
1376         if (!num_outs)
1377                 return 0;
1378
1379         for (i = 0; i < num_outs; i++) {
1380                 struct nid_path *path;
1381                 hda_nid_t pin = pins[i];
1382
1383                 if (!spec->obey_preferred_dacs) {
1384                         path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1385                         if (path) {
1386                                 badness += assign_out_path_ctls(codec, path);
1387                                 continue;
1388                         }
1389                 }
1390
1391                 dacs[i] = get_preferred_dac(codec, pin);
1392                 if (dacs[i]) {
1393                         if (is_dac_already_used(codec, dacs[i]))
1394                                 badness += bad->shared_primary;
1395                 } else if (spec->obey_preferred_dacs) {
1396                         badness += BAD_NO_PRIMARY_DAC;
1397                 }
1398
1399                 if (!dacs[i])
1400                         dacs[i] = look_for_dac(codec, pin, false);
1401                 if (!dacs[i] && !i) {
1402                         /* try to steal the DAC of surrounds for the front */
1403                         for (j = 1; j < num_outs; j++) {
1404                                 if (is_reachable_path(codec, dacs[j], pin)) {
1405                                         dacs[0] = dacs[j];
1406                                         dacs[j] = 0;
1407                                         invalidate_nid_path(codec, path_idx[j]);
1408                                         path_idx[j] = 0;
1409                                         break;
1410                                 }
1411                         }
1412                 }
1413                 dac = dacs[i];
1414                 if (!dac) {
1415                         if (num_outs > 2)
1416                                 dac = try_dac(codec, get_primary_out(codec, i), pin);
1417                         if (!dac)
1418                                 dac = try_dac(codec, dacs[0], pin);
1419                         if (!dac)
1420                                 dac = try_dac(codec, get_primary_out(codec, i), pin);
1421                         if (dac) {
1422                                 if (!i)
1423                                         badness += bad->shared_primary;
1424                                 else if (i == 1)
1425                                         badness += bad->shared_surr;
1426                                 else
1427                                         badness += bad->shared_clfe;
1428                         } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1429                                 dac = spec->private_dac_nids[0];
1430                                 badness += bad->shared_surr_main;
1431                         } else if (!i)
1432                                 badness += bad->no_primary_dac;
1433                         else
1434                                 badness += bad->no_dac;
1435                 }
1436                 if (!dac)
1437                         continue;
1438                 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
1439                 if (!path && !i && spec->mixer_nid) {
1440                         /* try with aamix */
1441                         path = snd_hda_add_new_path(codec, dac, pin, 0);
1442                 }
1443                 if (!path) {
1444                         dac = dacs[i] = 0;
1445                         badness += bad->no_dac;
1446                 } else {
1447                         /* print_nid_path(codec, "output", path); */
1448                         path->active = true;
1449                         path_idx[i] = snd_hda_get_path_idx(codec, path);
1450                         badness += assign_out_path_ctls(codec, path);
1451                 }
1452         }
1453
1454         return badness;
1455 }
1456
1457 /* return NID if the given pin has only a single connection to a certain DAC */
1458 static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1459 {
1460         struct hda_gen_spec *spec = codec->spec;
1461         int i;
1462         hda_nid_t nid_found = 0;
1463
1464         for (i = 0; i < spec->num_all_dacs; i++) {
1465                 hda_nid_t nid = spec->all_dacs[i];
1466                 if (!nid || is_dac_already_used(codec, nid))
1467                         continue;
1468                 if (is_reachable_path(codec, nid, pin)) {
1469                         if (nid_found)
1470                                 return 0;
1471                         nid_found = nid;
1472                 }
1473         }
1474         return nid_found;
1475 }
1476
1477 /* check whether the given pin can be a multi-io pin */
1478 static bool can_be_multiio_pin(struct hda_codec *codec,
1479                                unsigned int location, hda_nid_t nid)
1480 {
1481         unsigned int defcfg, caps;
1482
1483         defcfg = snd_hda_codec_get_pincfg(codec, nid);
1484         if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1485                 return false;
1486         if (location && get_defcfg_location(defcfg) != location)
1487                 return false;
1488         caps = snd_hda_query_pin_caps(codec, nid);
1489         if (!(caps & AC_PINCAP_OUT))
1490                 return false;
1491         return true;
1492 }
1493
1494 /* count the number of input pins that are capable to be multi-io */
1495 static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1496 {
1497         struct hda_gen_spec *spec = codec->spec;
1498         struct auto_pin_cfg *cfg = &spec->autocfg;
1499         unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1500         unsigned int location = get_defcfg_location(defcfg);
1501         int type, i;
1502         int num_pins = 0;
1503
1504         for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1505                 for (i = 0; i < cfg->num_inputs; i++) {
1506                         if (cfg->inputs[i].type != type)
1507                                 continue;
1508                         if (can_be_multiio_pin(codec, location,
1509                                                cfg->inputs[i].pin))
1510                                 num_pins++;
1511                 }
1512         }
1513         return num_pins;
1514 }
1515
1516 /*
1517  * multi-io helper
1518  *
1519  * When hardwired is set, try to fill ony hardwired pins, and returns
1520  * zero if any pins are filled, non-zero if nothing found.
1521  * When hardwired is off, try to fill possible input pins, and returns
1522  * the badness value.
1523  */
1524 static int fill_multi_ios(struct hda_codec *codec,
1525                           hda_nid_t reference_pin,
1526                           bool hardwired)
1527 {
1528         struct hda_gen_spec *spec = codec->spec;
1529         struct auto_pin_cfg *cfg = &spec->autocfg;
1530         int type, i, j, num_pins, old_pins;
1531         unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1532         unsigned int location = get_defcfg_location(defcfg);
1533         int badness = 0;
1534         struct nid_path *path;
1535
1536         old_pins = spec->multi_ios;
1537         if (old_pins >= 2)
1538                 goto end_fill;
1539
1540         num_pins = count_multiio_pins(codec, reference_pin);
1541         if (num_pins < 2)
1542                 goto end_fill;
1543
1544         for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1545                 for (i = 0; i < cfg->num_inputs; i++) {
1546                         hda_nid_t nid = cfg->inputs[i].pin;
1547                         hda_nid_t dac = 0;
1548
1549                         if (cfg->inputs[i].type != type)
1550                                 continue;
1551                         if (!can_be_multiio_pin(codec, location, nid))
1552                                 continue;
1553                         for (j = 0; j < spec->multi_ios; j++) {
1554                                 if (nid == spec->multi_io[j].pin)
1555                                         break;
1556                         }
1557                         if (j < spec->multi_ios)
1558                                 continue;
1559
1560                         if (hardwired)
1561                                 dac = get_dac_if_single(codec, nid);
1562                         else if (!dac)
1563                                 dac = look_for_dac(codec, nid, false);
1564                         if (!dac) {
1565                                 badness++;
1566                                 continue;
1567                         }
1568                         path = snd_hda_add_new_path(codec, dac, nid,
1569                                                     -spec->mixer_nid);
1570                         if (!path) {
1571                                 badness++;
1572                                 continue;
1573                         }
1574                         /* print_nid_path(codec, "multiio", path); */
1575                         spec->multi_io[spec->multi_ios].pin = nid;
1576                         spec->multi_io[spec->multi_ios].dac = dac;
1577                         spec->out_paths[cfg->line_outs + spec->multi_ios] =
1578                                 snd_hda_get_path_idx(codec, path);
1579                         spec->multi_ios++;
1580                         if (spec->multi_ios >= 2)
1581                                 break;
1582                 }
1583         }
1584  end_fill:
1585         if (badness)
1586                 badness = BAD_MULTI_IO;
1587         if (old_pins == spec->multi_ios) {
1588                 if (hardwired)
1589                         return 1; /* nothing found */
1590                 else
1591                         return badness; /* no badness if nothing found */
1592         }
1593         if (!hardwired && spec->multi_ios < 2) {
1594                 /* cancel newly assigned paths */
1595                 spec->paths.used -= spec->multi_ios - old_pins;
1596                 spec->multi_ios = old_pins;
1597                 return badness;
1598         }
1599
1600         /* assign volume and mute controls */
1601         for (i = old_pins; i < spec->multi_ios; i++) {
1602                 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1603                 badness += assign_out_path_ctls(codec, path);
1604         }
1605
1606         return badness;
1607 }
1608
1609 /* map DACs for all pins in the list if they are single connections */
1610 static bool map_singles(struct hda_codec *codec, int outs,
1611                         const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
1612 {
1613         struct hda_gen_spec *spec = codec->spec;
1614         int i;
1615         bool found = false;
1616         for (i = 0; i < outs; i++) {
1617                 struct nid_path *path;
1618                 hda_nid_t dac;
1619                 if (dacs[i])
1620                         continue;
1621                 dac = get_dac_if_single(codec, pins[i]);
1622                 if (!dac)
1623                         continue;
1624                 path = snd_hda_add_new_path(codec, dac, pins[i],
1625                                             -spec->mixer_nid);
1626                 if (!path && !i && spec->mixer_nid)
1627                         path = snd_hda_add_new_path(codec, dac, pins[i], 0);
1628                 if (path) {
1629                         dacs[i] = dac;
1630                         found = true;
1631                         /* print_nid_path(codec, "output", path); */
1632                         path->active = true;
1633                         path_idx[i] = snd_hda_get_path_idx(codec, path);
1634                 }
1635         }
1636         return found;
1637 }
1638
1639 static inline bool has_aamix_out_paths(struct hda_gen_spec *spec)
1640 {
1641         return spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
1642                 spec->aamix_out_paths[2];
1643 }
1644
1645 /* create a new path including aamix if available, and return its index */
1646 static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1647 {
1648         struct hda_gen_spec *spec = codec->spec;
1649         struct nid_path *path;
1650         hda_nid_t path_dac, dac, pin;
1651
1652         path = snd_hda_get_path_from_idx(codec, path_idx);
1653         if (!path || !path->depth ||
1654             is_nid_contained(path, spec->mixer_nid))
1655                 return 0;
1656         path_dac = path->path[0];
1657         dac = spec->private_dac_nids[0];
1658         pin = path->path[path->depth - 1];
1659         path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1660         if (!path) {
1661                 if (dac != path_dac)
1662                         dac = path_dac;
1663                 else if (spec->multiout.hp_out_nid[0])
1664                         dac = spec->multiout.hp_out_nid[0];
1665                 else if (spec->multiout.extra_out_nid[0])
1666                         dac = spec->multiout.extra_out_nid[0];
1667                 else
1668                         dac = 0;
1669                 if (dac)
1670                         path = snd_hda_add_new_path(codec, dac, pin,
1671                                                     spec->mixer_nid);
1672         }
1673         if (!path)
1674                 return 0;
1675         /* print_nid_path(codec, "output-aamix", path); */
1676         path->active = false; /* unused as default */
1677         path->pin_fixed = true; /* static route */
1678         return snd_hda_get_path_idx(codec, path);
1679 }
1680
1681 /* check whether the independent HP is available with the current config */
1682 static bool indep_hp_possible(struct hda_codec *codec)
1683 {
1684         struct hda_gen_spec *spec = codec->spec;
1685         struct auto_pin_cfg *cfg = &spec->autocfg;
1686         struct nid_path *path;
1687         int i, idx;
1688
1689         if (cfg->line_out_type == AUTO_PIN_HP_OUT)
1690                 idx = spec->out_paths[0];
1691         else
1692                 idx = spec->hp_paths[0];
1693         path = snd_hda_get_path_from_idx(codec, idx);
1694         if (!path)
1695                 return false;
1696
1697         /* assume no path conflicts unless aamix is involved */
1698         if (!spec->mixer_nid || !is_nid_contained(path, spec->mixer_nid))
1699                 return true;
1700
1701         /* check whether output paths contain aamix */
1702         for (i = 0; i < cfg->line_outs; i++) {
1703                 if (spec->out_paths[i] == idx)
1704                         break;
1705                 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1706                 if (path && is_nid_contained(path, spec->mixer_nid))
1707                         return false;
1708         }
1709         for (i = 0; i < cfg->speaker_outs; i++) {
1710                 path = snd_hda_get_path_from_idx(codec, spec->speaker_paths[i]);
1711                 if (path && is_nid_contained(path, spec->mixer_nid))
1712                         return false;
1713         }
1714
1715         return true;
1716 }
1717
1718 /* fill the empty entries in the dac array for speaker/hp with the
1719  * shared dac pointed by the paths
1720  */
1721 static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1722                                hda_nid_t *dacs, int *path_idx)
1723 {
1724         struct nid_path *path;
1725         int i;
1726
1727         for (i = 0; i < num_outs; i++) {
1728                 if (dacs[i])
1729                         continue;
1730                 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1731                 if (!path)
1732                         continue;
1733                 dacs[i] = path->path[0];
1734         }
1735 }
1736
1737 /* fill in the dac_nids table from the parsed pin configuration */
1738 static int fill_and_eval_dacs(struct hda_codec *codec,
1739                               bool fill_hardwired,
1740                               bool fill_mio_first)
1741 {
1742         struct hda_gen_spec *spec = codec->spec;
1743         struct auto_pin_cfg *cfg = &spec->autocfg;
1744         int i, err, badness;
1745
1746         /* set num_dacs once to full for look_for_dac() */
1747         spec->multiout.num_dacs = cfg->line_outs;
1748         spec->multiout.dac_nids = spec->private_dac_nids;
1749         memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1750         memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1751         memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1752         spec->multi_ios = 0;
1753         snd_array_free(&spec->paths);
1754
1755         /* clear path indices */
1756         memset(spec->out_paths, 0, sizeof(spec->out_paths));
1757         memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1758         memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1759         memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1760         memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
1761         memset(spec->input_paths, 0, sizeof(spec->input_paths));
1762         memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1763         memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1764
1765         badness = 0;
1766
1767         /* fill hard-wired DACs first */
1768         if (fill_hardwired) {
1769                 bool mapped;
1770                 do {
1771                         mapped = map_singles(codec, cfg->line_outs,
1772                                              cfg->line_out_pins,
1773                                              spec->private_dac_nids,
1774                                              spec->out_paths);
1775                         mapped |= map_singles(codec, cfg->hp_outs,
1776                                               cfg->hp_pins,
1777                                               spec->multiout.hp_out_nid,
1778                                               spec->hp_paths);
1779                         mapped |= map_singles(codec, cfg->speaker_outs,
1780                                               cfg->speaker_pins,
1781                                               spec->multiout.extra_out_nid,
1782                                               spec->speaker_paths);
1783                         if (!spec->no_multi_io &&
1784                             fill_mio_first && cfg->line_outs == 1 &&
1785                             cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1786                                 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
1787                                 if (!err)
1788                                         mapped = true;
1789                         }
1790                 } while (mapped);
1791         }
1792
1793         badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
1794                                    spec->private_dac_nids, spec->out_paths,
1795                                    spec->main_out_badness);
1796
1797         if (!spec->no_multi_io && fill_mio_first &&
1798             cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1799                 /* try to fill multi-io first */
1800                 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1801                 if (err < 0)
1802                         return err;
1803                 /* we don't count badness at this stage yet */
1804         }
1805
1806         if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1807                 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1808                                       spec->multiout.hp_out_nid,
1809                                       spec->hp_paths,
1810                                       spec->extra_out_badness);
1811                 if (err < 0)
1812                         return err;
1813                 badness += err;
1814         }
1815         if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1816                 err = try_assign_dacs(codec, cfg->speaker_outs,
1817                                       cfg->speaker_pins,
1818                                       spec->multiout.extra_out_nid,
1819                                       spec->speaker_paths,
1820                                       spec->extra_out_badness);
1821                 if (err < 0)
1822                         return err;
1823                 badness += err;
1824         }
1825         if (!spec->no_multi_io &&
1826             cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1827                 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1828                 if (err < 0)
1829                         return err;
1830                 badness += err;
1831         }
1832
1833         if (spec->mixer_nid) {
1834                 spec->aamix_out_paths[0] =
1835                         check_aamix_out_path(codec, spec->out_paths[0]);
1836                 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1837                         spec->aamix_out_paths[1] =
1838                                 check_aamix_out_path(codec, spec->hp_paths[0]);
1839                 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1840                         spec->aamix_out_paths[2] =
1841                                 check_aamix_out_path(codec, spec->speaker_paths[0]);
1842         }
1843
1844         if (!spec->no_multi_io &&
1845             cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1846                 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1847                         spec->multi_ios = 1; /* give badness */
1848
1849         /* re-count num_dacs and squash invalid entries */
1850         spec->multiout.num_dacs = 0;
1851         for (i = 0; i < cfg->line_outs; i++) {
1852                 if (spec->private_dac_nids[i])
1853                         spec->multiout.num_dacs++;
1854                 else {
1855                         memmove(spec->private_dac_nids + i,
1856                                 spec->private_dac_nids + i + 1,
1857                                 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1858                         spec->private_dac_nids[cfg->line_outs - 1] = 0;
1859                 }
1860         }
1861
1862         spec->ext_channel_count = spec->min_channel_count =
1863                 spec->multiout.num_dacs * 2;
1864
1865         if (spec->multi_ios == 2) {
1866                 for (i = 0; i < 2; i++)
1867                         spec->private_dac_nids[spec->multiout.num_dacs++] =
1868                                 spec->multi_io[i].dac;
1869         } else if (spec->multi_ios) {
1870                 spec->multi_ios = 0;
1871                 badness += BAD_MULTI_IO;
1872         }
1873
1874         if (spec->indep_hp && !indep_hp_possible(codec))
1875                 badness += BAD_NO_INDEP_HP;
1876
1877         /* re-fill the shared DAC for speaker / headphone */
1878         if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1879                 refill_shared_dacs(codec, cfg->hp_outs,
1880                                    spec->multiout.hp_out_nid,
1881                                    spec->hp_paths);
1882         if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1883                 refill_shared_dacs(codec, cfg->speaker_outs,
1884                                    spec->multiout.extra_out_nid,
1885                                    spec->speaker_paths);
1886
1887         return badness;
1888 }
1889
1890 #define DEBUG_BADNESS
1891
1892 #ifdef DEBUG_BADNESS
1893 #define debug_badness(fmt, ...)                                         \
1894         codec_dbg(codec, fmt, ##__VA_ARGS__)
1895 #else
1896 #define debug_badness(fmt, ...)                                         \
1897         do { if (0) codec_dbg(codec, fmt, ##__VA_ARGS__); } while (0)
1898 #endif
1899
1900 #ifdef DEBUG_BADNESS
1901 static inline void print_nid_path_idx(struct hda_codec *codec,
1902                                       const char *pfx, int idx)
1903 {
1904         struct nid_path *path;
1905
1906         path = snd_hda_get_path_from_idx(codec, idx);
1907         if (path)
1908                 print_nid_path(codec, pfx, path);
1909 }
1910
1911 static void debug_show_configs(struct hda_codec *codec,
1912                                struct auto_pin_cfg *cfg)
1913 {
1914         struct hda_gen_spec *spec = codec->spec;
1915         static const char * const lo_type[3] = { "LO", "SP", "HP" };
1916         int i;
1917
1918         debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
1919                       cfg->line_out_pins[0], cfg->line_out_pins[1],
1920                       cfg->line_out_pins[2], cfg->line_out_pins[3],
1921                       spec->multiout.dac_nids[0],
1922                       spec->multiout.dac_nids[1],
1923                       spec->multiout.dac_nids[2],
1924                       spec->multiout.dac_nids[3],
1925                       lo_type[cfg->line_out_type]);
1926         for (i = 0; i < cfg->line_outs; i++)
1927                 print_nid_path_idx(codec, "  out", spec->out_paths[i]);
1928         if (spec->multi_ios > 0)
1929                 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1930                               spec->multi_ios,
1931                               spec->multi_io[0].pin, spec->multi_io[1].pin,
1932                               spec->multi_io[0].dac, spec->multi_io[1].dac);
1933         for (i = 0; i < spec->multi_ios; i++)
1934                 print_nid_path_idx(codec, "  mio",
1935                                    spec->out_paths[cfg->line_outs + i]);
1936         if (cfg->hp_outs)
1937                 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1938                       cfg->hp_pins[0], cfg->hp_pins[1],
1939                       cfg->hp_pins[2], cfg->hp_pins[3],
1940                       spec->multiout.hp_out_nid[0],
1941                       spec->multiout.hp_out_nid[1],
1942                       spec->multiout.hp_out_nid[2],
1943                       spec->multiout.hp_out_nid[3]);
1944         for (i = 0; i < cfg->hp_outs; i++)
1945                 print_nid_path_idx(codec, "  hp ", spec->hp_paths[i]);
1946         if (cfg->speaker_outs)
1947                 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1948                       cfg->speaker_pins[0], cfg->speaker_pins[1],
1949                       cfg->speaker_pins[2], cfg->speaker_pins[3],
1950                       spec->multiout.extra_out_nid[0],
1951                       spec->multiout.extra_out_nid[1],
1952                       spec->multiout.extra_out_nid[2],
1953                       spec->multiout.extra_out_nid[3]);
1954         for (i = 0; i < cfg->speaker_outs; i++)
1955                 print_nid_path_idx(codec, "  spk", spec->speaker_paths[i]);
1956         for (i = 0; i < 3; i++)
1957                 print_nid_path_idx(codec, "  mix", spec->aamix_out_paths[i]);
1958 }
1959 #else
1960 #define debug_show_configs(codec, cfg) /* NOP */
1961 #endif
1962
1963 /* find all available DACs of the codec */
1964 static void fill_all_dac_nids(struct hda_codec *codec)
1965 {
1966         struct hda_gen_spec *spec = codec->spec;
1967         hda_nid_t nid;
1968
1969         spec->num_all_dacs = 0;
1970         memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1971         for_each_hda_codec_node(nid, codec) {
1972                 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1973                         continue;
1974                 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1975                         codec_err(codec, "Too many DACs!\n");
1976                         break;
1977                 }
1978                 spec->all_dacs[spec->num_all_dacs++] = nid;
1979         }
1980 }
1981
1982 static int parse_output_paths(struct hda_codec *codec)
1983 {
1984         struct hda_gen_spec *spec = codec->spec;
1985         struct auto_pin_cfg *cfg = &spec->autocfg;
1986         struct auto_pin_cfg *best_cfg;
1987         unsigned int val;
1988         int best_badness = INT_MAX;
1989         int badness;
1990         bool fill_hardwired = true, fill_mio_first = true;
1991         bool best_wired = true, best_mio = true;
1992         bool hp_spk_swapped = false;
1993
1994         best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1995         if (!best_cfg)
1996                 return -ENOMEM;
1997         *best_cfg = *cfg;
1998
1999         for (;;) {
2000                 badness = fill_and_eval_dacs(codec, fill_hardwired,
2001                                              fill_mio_first);
2002                 if (badness < 0) {
2003                         kfree(best_cfg);
2004                         return badness;
2005                 }
2006                 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
2007                               cfg->line_out_type, fill_hardwired, fill_mio_first,
2008                               badness);
2009                 debug_show_configs(codec, cfg);
2010                 if (badness < best_badness) {
2011                         best_badness = badness;
2012                         *best_cfg = *cfg;
2013                         best_wired = fill_hardwired;
2014                         best_mio = fill_mio_first;
2015                 }
2016                 if (!badness)
2017                         break;
2018                 fill_mio_first = !fill_mio_first;
2019                 if (!fill_mio_first)
2020                         continue;
2021                 fill_hardwired = !fill_hardwired;
2022                 if (!fill_hardwired)
2023                         continue;
2024                 if (hp_spk_swapped)
2025                         break;
2026                 hp_spk_swapped = true;
2027                 if (cfg->speaker_outs > 0 &&
2028                     cfg->line_out_type == AUTO_PIN_HP_OUT) {
2029                         cfg->hp_outs = cfg->line_outs;
2030                         memcpy(cfg->hp_pins, cfg->line_out_pins,
2031                                sizeof(cfg->hp_pins));
2032                         cfg->line_outs = cfg->speaker_outs;
2033                         memcpy(cfg->line_out_pins, cfg->speaker_pins,
2034                                sizeof(cfg->speaker_pins));
2035                         cfg->speaker_outs = 0;
2036                         memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
2037                         cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
2038                         fill_hardwired = true;
2039                         continue;
2040                 }
2041                 if (cfg->hp_outs > 0 &&
2042                     cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
2043                         cfg->speaker_outs = cfg->line_outs;
2044                         memcpy(cfg->speaker_pins, cfg->line_out_pins,
2045                                sizeof(cfg->speaker_pins));
2046                         cfg->line_outs = cfg->hp_outs;
2047                         memcpy(cfg->line_out_pins, cfg->hp_pins,
2048                                sizeof(cfg->hp_pins));
2049                         cfg->hp_outs = 0;
2050                         memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
2051                         cfg->line_out_type = AUTO_PIN_HP_OUT;
2052                         fill_hardwired = true;
2053                         continue;
2054                 }
2055                 break;
2056         }
2057
2058         if (badness) {
2059                 debug_badness("==> restoring best_cfg\n");
2060                 *cfg = *best_cfg;
2061                 fill_and_eval_dacs(codec, best_wired, best_mio);
2062         }
2063         debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
2064                       cfg->line_out_type, best_wired, best_mio);
2065         debug_show_configs(codec, cfg);
2066
2067         if (cfg->line_out_pins[0]) {
2068                 struct nid_path *path;
2069                 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
2070                 if (path)
2071                         spec->vmaster_nid = look_for_out_vol_nid(codec, path);
2072                 if (spec->vmaster_nid) {
2073                         snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
2074                                                 HDA_OUTPUT, spec->vmaster_tlv);
2075                         if (spec->dac_min_mute)
2076                                 spec->vmaster_tlv[3] |= TLV_DB_SCALE_MUTE;
2077                 }
2078         }
2079
2080         /* set initial pinctl targets */
2081         if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
2082                 val = PIN_HP;
2083         else
2084                 val = PIN_OUT;
2085         set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
2086         if (cfg->line_out_type != AUTO_PIN_HP_OUT)
2087                 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
2088         if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
2089                 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
2090                 set_pin_targets(codec, cfg->speaker_outs,
2091                                 cfg->speaker_pins, val);
2092         }
2093
2094         /* clear indep_hp flag if not available */
2095         if (spec->indep_hp && !indep_hp_possible(codec))
2096                 spec->indep_hp = 0;
2097
2098         kfree(best_cfg);
2099         return 0;
2100 }
2101
2102 /* add playback controls from the parsed DAC table */
2103 static int create_multi_out_ctls(struct hda_codec *codec,
2104                                  const struct auto_pin_cfg *cfg)
2105 {
2106         struct hda_gen_spec *spec = codec->spec;
2107         int i, err, noutputs;
2108
2109         noutputs = cfg->line_outs;
2110         if (spec->multi_ios > 0 && cfg->line_outs < 3)
2111                 noutputs += spec->multi_ios;
2112
2113         for (i = 0; i < noutputs; i++) {
2114                 const char *name;
2115                 int index;
2116                 struct nid_path *path;
2117
2118                 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
2119                 if (!path)
2120                         continue;
2121
2122                 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
2123                 if (!name || !strcmp(name, "CLFE")) {
2124                         /* Center/LFE */
2125                         err = add_vol_ctl(codec, "Center", 0, 1, path);
2126                         if (err < 0)
2127                                 return err;
2128                         err = add_vol_ctl(codec, "LFE", 0, 2, path);
2129                         if (err < 0)
2130                                 return err;
2131                 } else {
2132                         err = add_stereo_vol(codec, name, index, path);
2133                         if (err < 0)
2134                                 return err;
2135                 }
2136
2137                 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
2138                 if (!name || !strcmp(name, "CLFE")) {
2139                         err = add_sw_ctl(codec, "Center", 0, 1, path);
2140                         if (err < 0)
2141                                 return err;
2142                         err = add_sw_ctl(codec, "LFE", 0, 2, path);
2143                         if (err < 0)
2144                                 return err;
2145                 } else {
2146                         err = add_stereo_sw(codec, name, index, path);
2147                         if (err < 0)
2148                                 return err;
2149                 }
2150         }
2151         return 0;
2152 }
2153
2154 static int create_extra_out(struct hda_codec *codec, int path_idx,
2155                             const char *pfx, int cidx)
2156 {
2157         struct nid_path *path;
2158         int err;
2159
2160         path = snd_hda_get_path_from_idx(codec, path_idx);
2161         if (!path)
2162                 return 0;
2163         err = add_stereo_vol(codec, pfx, cidx, path);
2164         if (err < 0)
2165                 return err;
2166         err = add_stereo_sw(codec, pfx, cidx, path);
2167         if (err < 0)
2168                 return err;
2169         return 0;
2170 }
2171
2172 /* add playback controls for speaker and HP outputs */
2173 static int create_extra_outs(struct hda_codec *codec, int num_pins,
2174                              const int *paths, const char *pfx)
2175 {
2176         int i;
2177
2178         for (i = 0; i < num_pins; i++) {
2179                 const char *name;
2180                 char tmp[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2181                 int err, idx = 0;
2182
2183                 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
2184                         name = "Bass Speaker";
2185                 else if (num_pins >= 3) {
2186                         snprintf(tmp, sizeof(tmp), "%s %s",
2187                                  pfx, channel_name[i]);
2188                         name = tmp;
2189                 } else {
2190                         name = pfx;
2191                         idx = i;
2192                 }
2193                 err = create_extra_out(codec, paths[i], name, idx);
2194                 if (err < 0)
2195                         return err;
2196         }
2197         return 0;
2198 }
2199
2200 static int create_hp_out_ctls(struct hda_codec *codec)
2201 {
2202         struct hda_gen_spec *spec = codec->spec;
2203         return create_extra_outs(codec, spec->autocfg.hp_outs,
2204                                  spec->hp_paths,
2205                                  "Headphone");
2206 }
2207
2208 static int create_speaker_out_ctls(struct hda_codec *codec)
2209 {
2210         struct hda_gen_spec *spec = codec->spec;
2211         return create_extra_outs(codec, spec->autocfg.speaker_outs,
2212                                  spec->speaker_paths,
2213                                  "Speaker");
2214 }
2215
2216 /*
2217  * independent HP controls
2218  */
2219
2220 static void call_hp_automute(struct hda_codec *codec,
2221                              struct hda_jack_callback *jack);
2222 static int indep_hp_info(struct snd_kcontrol *kcontrol,
2223                          struct snd_ctl_elem_info *uinfo)
2224 {
2225         return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2226 }
2227
2228 static int indep_hp_get(struct snd_kcontrol *kcontrol,
2229                         struct snd_ctl_elem_value *ucontrol)
2230 {
2231         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2232         struct hda_gen_spec *spec = codec->spec;
2233         ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
2234         return 0;
2235 }
2236
2237 static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2238                                int nomix_path_idx, int mix_path_idx,
2239                                int out_type);
2240
2241 static int indep_hp_put(struct snd_kcontrol *kcontrol,
2242                         struct snd_ctl_elem_value *ucontrol)
2243 {
2244         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2245         struct hda_gen_spec *spec = codec->spec;
2246         unsigned int select = ucontrol->value.enumerated.item[0];
2247         int ret = 0;
2248
2249         mutex_lock(&spec->pcm_mutex);
2250         if (spec->active_streams) {
2251                 ret = -EBUSY;
2252                 goto unlock;
2253         }
2254
2255         if (spec->indep_hp_enabled != select) {
2256                 hda_nid_t *dacp;
2257                 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2258                         dacp = &spec->private_dac_nids[0];
2259                 else
2260                         dacp = &spec->multiout.hp_out_nid[0];
2261
2262                 /* update HP aamix paths in case it conflicts with indep HP */
2263                 if (spec->have_aamix_ctl) {
2264                         if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2265                                 update_aamix_paths(codec, spec->aamix_mode,
2266                                                    spec->out_paths[0],
2267                                                    spec->aamix_out_paths[0],
2268                                                    spec->autocfg.line_out_type);
2269                         else
2270                                 update_aamix_paths(codec, spec->aamix_mode,
2271                                                    spec->hp_paths[0],
2272                                                    spec->aamix_out_paths[1],
2273                                                    AUTO_PIN_HP_OUT);
2274                 }
2275
2276                 spec->indep_hp_enabled = select;
2277                 if (spec->indep_hp_enabled)
2278                         *dacp = 0;
2279                 else
2280                         *dacp = spec->alt_dac_nid;
2281
2282                 call_hp_automute(codec, NULL);
2283                 ret = 1;
2284         }
2285  unlock:
2286         mutex_unlock(&spec->pcm_mutex);
2287         return ret;
2288 }
2289
2290 static const struct snd_kcontrol_new indep_hp_ctl = {
2291         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2292         .name = "Independent HP",
2293         .info = indep_hp_info,
2294         .get = indep_hp_get,
2295         .put = indep_hp_put,
2296 };
2297
2298
2299 static int create_indep_hp_ctls(struct hda_codec *codec)
2300 {
2301         struct hda_gen_spec *spec = codec->spec;
2302         hda_nid_t dac;
2303
2304         if (!spec->indep_hp)
2305                 return 0;
2306         if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2307                 dac = spec->multiout.dac_nids[0];
2308         else
2309                 dac = spec->multiout.hp_out_nid[0];
2310         if (!dac) {
2311                 spec->indep_hp = 0;
2312                 return 0;
2313         }
2314
2315         spec->indep_hp_enabled = false;
2316         spec->alt_dac_nid = dac;
2317         if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
2318                 return -ENOMEM;
2319         return 0;
2320 }
2321
2322 /*
2323  * channel mode enum control
2324  */
2325
2326 static int ch_mode_info(struct snd_kcontrol *kcontrol,
2327                         struct snd_ctl_elem_info *uinfo)
2328 {
2329         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2330         struct hda_gen_spec *spec = codec->spec;
2331         int chs;
2332
2333         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2334         uinfo->count = 1;
2335         uinfo->value.enumerated.items = spec->multi_ios + 1;
2336         if (uinfo->value.enumerated.item > spec->multi_ios)
2337                 uinfo->value.enumerated.item = spec->multi_ios;
2338         chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2339         sprintf(uinfo->value.enumerated.name, "%dch", chs);
2340         return 0;
2341 }
2342
2343 static int ch_mode_get(struct snd_kcontrol *kcontrol,
2344                        struct snd_ctl_elem_value *ucontrol)
2345 {
2346         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2347         struct hda_gen_spec *spec = codec->spec;
2348         ucontrol->value.enumerated.item[0] =
2349                 (spec->ext_channel_count - spec->min_channel_count) / 2;
2350         return 0;
2351 }
2352
2353 static inline struct nid_path *
2354 get_multiio_path(struct hda_codec *codec, int idx)
2355 {
2356         struct hda_gen_spec *spec = codec->spec;
2357         return snd_hda_get_path_from_idx(codec,
2358                 spec->out_paths[spec->autocfg.line_outs + idx]);
2359 }
2360
2361 static void update_automute_all(struct hda_codec *codec);
2362
2363 /* Default value to be passed as aamix argument for snd_hda_activate_path();
2364  * used for output paths
2365  */
2366 static bool aamix_default(struct hda_gen_spec *spec)
2367 {
2368         return !spec->have_aamix_ctl || spec->aamix_mode;
2369 }
2370
2371 static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2372 {
2373         struct hda_gen_spec *spec = codec->spec;
2374         hda_nid_t nid = spec->multi_io[idx].pin;
2375         struct nid_path *path;
2376
2377         path = get_multiio_path(codec, idx);
2378         if (!path)
2379                 return -EINVAL;
2380
2381         if (path->active == output)
2382                 return 0;
2383
2384         if (output) {
2385                 set_pin_target(codec, nid, PIN_OUT, true);
2386                 snd_hda_activate_path(codec, path, true, aamix_default(spec));
2387                 set_pin_eapd(codec, nid, true);
2388         } else {
2389                 set_pin_eapd(codec, nid, false);
2390                 snd_hda_activate_path(codec, path, false, aamix_default(spec));
2391                 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
2392                 path_power_down_sync(codec, path);
2393         }
2394
2395         /* update jack retasking in case it modifies any of them */
2396         update_automute_all(codec);
2397
2398         return 0;
2399 }
2400
2401 static int ch_mode_put(struct snd_kcontrol *kcontrol,
2402                        struct snd_ctl_elem_value *ucontrol)
2403 {
2404         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2405         struct hda_gen_spec *spec = codec->spec;
2406         int i, ch;
2407
2408         ch = ucontrol->value.enumerated.item[0];
2409         if (ch < 0 || ch > spec->multi_ios)
2410                 return -EINVAL;
2411         if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
2412                 return 0;
2413         spec->ext_channel_count = ch * 2 + spec->min_channel_count;
2414         for (i = 0; i < spec->multi_ios; i++)
2415                 set_multi_io(codec, i, i < ch);
2416         spec->multiout.max_channels = max(spec->ext_channel_count,
2417                                           spec->const_channel_count);
2418         if (spec->need_dac_fix)
2419                 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
2420         return 1;
2421 }
2422
2423 static const struct snd_kcontrol_new channel_mode_enum = {
2424         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2425         .name = "Channel Mode",
2426         .info = ch_mode_info,
2427         .get = ch_mode_get,
2428         .put = ch_mode_put,
2429 };
2430
2431 static int create_multi_channel_mode(struct hda_codec *codec)
2432 {
2433         struct hda_gen_spec *spec = codec->spec;
2434
2435         if (spec->multi_ios > 0) {
2436                 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
2437                         return -ENOMEM;
2438         }
2439         return 0;
2440 }
2441
2442 /*
2443  * aamix loopback enable/disable switch
2444  */
2445
2446 #define loopback_mixing_info    indep_hp_info
2447
2448 static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2449                                struct snd_ctl_elem_value *ucontrol)
2450 {
2451         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2452         struct hda_gen_spec *spec = codec->spec;
2453         ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2454         return 0;
2455 }
2456
2457 static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2458                                int nomix_path_idx, int mix_path_idx,
2459                                int out_type)
2460 {
2461         struct hda_gen_spec *spec = codec->spec;
2462         struct nid_path *nomix_path, *mix_path;
2463
2464         nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2465         mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2466         if (!nomix_path || !mix_path)
2467                 return;
2468
2469         /* if HP aamix path is driven from a different DAC and the
2470          * independent HP mode is ON, can't turn on aamix path
2471          */
2472         if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2473             mix_path->path[0] != spec->alt_dac_nid)
2474                 do_mix = false;
2475
2476         if (do_mix) {
2477                 snd_hda_activate_path(codec, nomix_path, false, true);
2478                 snd_hda_activate_path(codec, mix_path, true, true);
2479                 path_power_down_sync(codec, nomix_path);
2480         } else {
2481                 snd_hda_activate_path(codec, mix_path, false, false);
2482                 snd_hda_activate_path(codec, nomix_path, true, false);
2483                 path_power_down_sync(codec, mix_path);
2484         }
2485 }
2486
2487 /* re-initialize the output paths; only called from loopback_mixing_put() */
2488 static void update_output_paths(struct hda_codec *codec, int num_outs,
2489                                 const int *paths)
2490 {
2491         struct hda_gen_spec *spec = codec->spec;
2492         struct nid_path *path;
2493         int i;
2494
2495         for (i = 0; i < num_outs; i++) {
2496                 path = snd_hda_get_path_from_idx(codec, paths[i]);
2497                 if (path)
2498                         snd_hda_activate_path(codec, path, path->active,
2499                                               spec->aamix_mode);
2500         }
2501 }
2502
2503 static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2504                                struct snd_ctl_elem_value *ucontrol)
2505 {
2506         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2507         struct hda_gen_spec *spec = codec->spec;
2508         const struct auto_pin_cfg *cfg = &spec->autocfg;
2509         unsigned int val = ucontrol->value.enumerated.item[0];
2510
2511         if (val == spec->aamix_mode)
2512                 return 0;
2513         spec->aamix_mode = val;
2514         if (has_aamix_out_paths(spec)) {
2515                 update_aamix_paths(codec, val, spec->out_paths[0],
2516                                    spec->aamix_out_paths[0],
2517                                    cfg->line_out_type);
2518                 update_aamix_paths(codec, val, spec->hp_paths[0],
2519                                    spec->aamix_out_paths[1],
2520                                    AUTO_PIN_HP_OUT);
2521                 update_aamix_paths(codec, val, spec->speaker_paths[0],
2522                                    spec->aamix_out_paths[2],
2523                                    AUTO_PIN_SPEAKER_OUT);
2524         } else {
2525                 update_output_paths(codec, cfg->line_outs, spec->out_paths);
2526                 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
2527                         update_output_paths(codec, cfg->hp_outs, spec->hp_paths);
2528                 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
2529                         update_output_paths(codec, cfg->speaker_outs,
2530                                             spec->speaker_paths);
2531         }
2532         return 1;
2533 }
2534
2535 static const struct snd_kcontrol_new loopback_mixing_enum = {
2536         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2537         .name = "Loopback Mixing",
2538         .info = loopback_mixing_info,
2539         .get = loopback_mixing_get,
2540         .put = loopback_mixing_put,
2541 };
2542
2543 static int create_loopback_mixing_ctl(struct hda_codec *codec)
2544 {
2545         struct hda_gen_spec *spec = codec->spec;
2546
2547         if (!spec->mixer_nid)
2548                 return 0;
2549         if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2550                 return -ENOMEM;
2551         spec->have_aamix_ctl = 1;
2552         return 0;
2553 }
2554
2555 /*
2556  * shared headphone/mic handling
2557  */
2558
2559 static void call_update_outputs(struct hda_codec *codec);
2560
2561 /* for shared I/O, change the pin-control accordingly */
2562 static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
2563 {
2564         struct hda_gen_spec *spec = codec->spec;
2565         bool as_mic;
2566         unsigned int val;
2567         hda_nid_t pin;
2568
2569         pin = spec->hp_mic_pin;
2570         as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
2571
2572         if (!force) {
2573                 val = snd_hda_codec_get_pin_target(codec, pin);
2574                 if (as_mic) {
2575                         if (val & PIN_IN)
2576                                 return;
2577                 } else {
2578                         if (val & PIN_OUT)
2579                                 return;
2580                 }
2581         }
2582
2583         val = snd_hda_get_default_vref(codec, pin);
2584         /* if the HP pin doesn't support VREF and the codec driver gives an
2585          * alternative pin, set up the VREF on that pin instead
2586          */
2587         if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2588                 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2589                 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2590                 if (vref_val != AC_PINCTL_VREF_HIZ)
2591                         snd_hda_set_pin_ctl_cache(codec, vref_pin,
2592                                                   PIN_IN | (as_mic ? vref_val : 0));
2593         }
2594
2595         if (!spec->hp_mic_jack_modes) {
2596                 if (as_mic)
2597                         val |= PIN_IN;
2598                 else
2599                         val = PIN_HP;
2600                 set_pin_target(codec, pin, val, true);
2601                 call_hp_automute(codec, NULL);
2602         }
2603 }
2604
2605 /* create a shared input with the headphone out */
2606 static int create_hp_mic(struct hda_codec *codec)
2607 {
2608         struct hda_gen_spec *spec = codec->spec;
2609         struct auto_pin_cfg *cfg = &spec->autocfg;
2610         unsigned int defcfg;
2611         hda_nid_t nid;
2612
2613         if (!spec->hp_mic) {
2614                 if (spec->suppress_hp_mic_detect)
2615                         return 0;
2616                 /* automatic detection: only if no input or a single internal
2617                  * input pin is found, try to detect the shared hp/mic
2618                  */
2619                 if (cfg->num_inputs > 1)
2620                         return 0;
2621                 else if (cfg->num_inputs == 1) {
2622                         defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2623                         if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2624                                 return 0;
2625                 }
2626         }
2627
2628         spec->hp_mic = 0; /* clear once */
2629         if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
2630                 return 0;
2631
2632         nid = 0;
2633         if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2634                 nid = cfg->line_out_pins[0];
2635         else if (cfg->hp_outs > 0)
2636                 nid = cfg->hp_pins[0];
2637         if (!nid)
2638                 return 0;
2639
2640         if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2641                 return 0; /* no input */
2642
2643         cfg->inputs[cfg->num_inputs].pin = nid;
2644         cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
2645         cfg->inputs[cfg->num_inputs].is_headphone_mic = 1;
2646         cfg->num_inputs++;
2647         spec->hp_mic = 1;
2648         spec->hp_mic_pin = nid;
2649         /* we can't handle auto-mic together with HP-mic */
2650         spec->suppress_auto_mic = 1;
2651         codec_dbg(codec, "Enable shared I/O jack on NID 0x%x\n", nid);
2652         return 0;
2653 }
2654
2655 /*
2656  * output jack mode
2657  */
2658
2659 static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2660
2661 static const char * const out_jack_texts[] = {
2662         "Line Out", "Headphone Out",
2663 };
2664
2665 static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2666                               struct snd_ctl_elem_info *uinfo)
2667 {
2668         return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
2669 }
2670
2671 static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2672                              struct snd_ctl_elem_value *ucontrol)
2673 {
2674         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2675         hda_nid_t nid = kcontrol->private_value;
2676         if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2677                 ucontrol->value.enumerated.item[0] = 1;
2678         else
2679                 ucontrol->value.enumerated.item[0] = 0;
2680         return 0;
2681 }
2682
2683 static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2684                              struct snd_ctl_elem_value *ucontrol)
2685 {
2686         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2687         hda_nid_t nid = kcontrol->private_value;
2688         unsigned int val;
2689
2690         val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2691         if (snd_hda_codec_get_pin_target(codec, nid) == val)
2692                 return 0;
2693         snd_hda_set_pin_ctl_cache(codec, nid, val);
2694         return 1;
2695 }
2696
2697 static const struct snd_kcontrol_new out_jack_mode_enum = {
2698         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2699         .info = out_jack_mode_info,
2700         .get = out_jack_mode_get,
2701         .put = out_jack_mode_put,
2702 };
2703
2704 static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2705 {
2706         struct hda_gen_spec *spec = codec->spec;
2707         int i;
2708
2709         for (i = 0; i < spec->kctls.used; i++) {
2710                 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2711                 if (!strcmp(kctl->name, name) && kctl->index == idx)
2712                         return true;
2713         }
2714         return false;
2715 }
2716
2717 static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2718                                char *name, size_t name_len)
2719 {
2720         struct hda_gen_spec *spec = codec->spec;
2721         int idx = 0;
2722
2723         snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2724         strlcat(name, " Jack Mode", name_len);
2725
2726         for (; find_kctl_name(codec, name, idx); idx++)
2727                 ;
2728 }
2729
2730 static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2731 {
2732         struct hda_gen_spec *spec = codec->spec;
2733         if (spec->add_jack_modes) {
2734                 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2735                 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2736                         return 2;
2737         }
2738         return 1;
2739 }
2740
2741 static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2742                                  hda_nid_t *pins)
2743 {
2744         struct hda_gen_spec *spec = codec->spec;
2745         int i;
2746
2747         for (i = 0; i < num_pins; i++) {
2748                 hda_nid_t pin = pins[i];
2749                 if (pin == spec->hp_mic_pin)
2750                         continue;
2751                 if (get_out_jack_num_items(codec, pin) > 1) {
2752                         struct snd_kcontrol_new *knew;
2753                         char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2754                         get_jack_mode_name(codec, pin, name, sizeof(name));
2755                         knew = snd_hda_gen_add_kctl(spec, name,
2756                                                     &out_jack_mode_enum);
2757                         if (!knew)
2758                                 return -ENOMEM;
2759                         knew->private_value = pin;
2760                 }
2761         }
2762
2763         return 0;
2764 }
2765
2766 /*
2767  * input jack mode
2768  */
2769
2770 /* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2771 #define NUM_VREFS       6
2772
2773 static const char * const vref_texts[NUM_VREFS] = {
2774         "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2775         "", "Mic 80pc Bias", "Mic 100pc Bias"
2776 };
2777
2778 static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2779 {
2780         unsigned int pincap;
2781
2782         pincap = snd_hda_query_pin_caps(codec, pin);
2783         pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2784         /* filter out unusual vrefs */
2785         pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2786         return pincap;
2787 }
2788
2789 /* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2790 static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2791 {
2792         unsigned int i, n = 0;
2793
2794         for (i = 0; i < NUM_VREFS; i++) {
2795                 if (vref_caps & (1 << i)) {
2796                         if (n == item_idx)
2797                                 return i;
2798                         n++;
2799                 }
2800         }
2801         return 0;
2802 }
2803
2804 /* convert back from the vref ctl index to the enum item index */
2805 static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2806 {
2807         unsigned int i, n = 0;
2808
2809         for (i = 0; i < NUM_VREFS; i++) {
2810                 if (i == idx)
2811                         return n;
2812                 if (vref_caps & (1 << i))
2813                         n++;
2814         }
2815         return 0;
2816 }
2817
2818 static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2819                              struct snd_ctl_elem_info *uinfo)
2820 {
2821         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2822         hda_nid_t nid = kcontrol->private_value;
2823         unsigned int vref_caps = get_vref_caps(codec, nid);
2824
2825         snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2826                                  vref_texts);
2827         /* set the right text */
2828         strcpy(uinfo->value.enumerated.name,
2829                vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2830         return 0;
2831 }
2832
2833 static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2834                             struct snd_ctl_elem_value *ucontrol)
2835 {
2836         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2837         hda_nid_t nid = kcontrol->private_value;
2838         unsigned int vref_caps = get_vref_caps(codec, nid);
2839         unsigned int idx;
2840
2841         idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2842         ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2843         return 0;
2844 }
2845
2846 static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2847                             struct snd_ctl_elem_value *ucontrol)
2848 {
2849         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2850         hda_nid_t nid = kcontrol->private_value;
2851         unsigned int vref_caps = get_vref_caps(codec, nid);
2852         unsigned int val, idx;
2853
2854         val = snd_hda_codec_get_pin_target(codec, nid);
2855         idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2856         if (idx == ucontrol->value.enumerated.item[0])
2857                 return 0;
2858
2859         val &= ~AC_PINCTL_VREFEN;
2860         val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2861         snd_hda_set_pin_ctl_cache(codec, nid, val);
2862         return 1;
2863 }
2864
2865 static const struct snd_kcontrol_new in_jack_mode_enum = {
2866         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2867         .info = in_jack_mode_info,
2868         .get = in_jack_mode_get,
2869         .put = in_jack_mode_put,
2870 };
2871
2872 static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2873 {
2874         struct hda_gen_spec *spec = codec->spec;
2875         int nitems = 0;
2876         if (spec->add_jack_modes)
2877                 nitems = hweight32(get_vref_caps(codec, pin));
2878         return nitems ? nitems : 1;
2879 }
2880
2881 static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2882 {
2883         struct hda_gen_spec *spec = codec->spec;
2884         struct snd_kcontrol_new *knew;
2885         char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2886         unsigned int defcfg;
2887
2888         if (pin == spec->hp_mic_pin)
2889                 return 0; /* already done in create_out_jack_mode() */
2890
2891         /* no jack mode for fixed pins */
2892         defcfg = snd_hda_codec_get_pincfg(codec, pin);
2893         if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2894                 return 0;
2895
2896         /* no multiple vref caps? */
2897         if (get_in_jack_num_items(codec, pin) <= 1)
2898                 return 0;
2899
2900         get_jack_mode_name(codec, pin, name, sizeof(name));
2901         knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2902         if (!knew)
2903                 return -ENOMEM;
2904         knew->private_value = pin;
2905         return 0;
2906 }
2907
2908 /*
2909  * HP/mic shared jack mode
2910  */
2911 static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2912                                  struct snd_ctl_elem_info *uinfo)
2913 {
2914         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2915         hda_nid_t nid = kcontrol->private_value;
2916         int out_jacks = get_out_jack_num_items(codec, nid);
2917         int in_jacks = get_in_jack_num_items(codec, nid);
2918         const char *text = NULL;
2919         int idx;
2920
2921         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2922         uinfo->count = 1;
2923         uinfo->value.enumerated.items = out_jacks + in_jacks;
2924         if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2925                 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2926         idx = uinfo->value.enumerated.item;
2927         if (idx < out_jacks) {
2928                 if (out_jacks > 1)
2929                         text = out_jack_texts[idx];
2930                 else
2931                         text = "Headphone Out";
2932         } else {
2933                 idx -= out_jacks;
2934                 if (in_jacks > 1) {
2935                         unsigned int vref_caps = get_vref_caps(codec, nid);
2936                         text = vref_texts[get_vref_idx(vref_caps, idx)];
2937                 } else
2938                         text = "Mic In";
2939         }
2940
2941         strcpy(uinfo->value.enumerated.name, text);
2942         return 0;
2943 }
2944
2945 static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2946 {
2947         int out_jacks = get_out_jack_num_items(codec, nid);
2948         int in_jacks = get_in_jack_num_items(codec, nid);
2949         unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2950         int idx = 0;
2951
2952         if (val & PIN_OUT) {
2953                 if (out_jacks > 1 && val == PIN_HP)
2954                         idx = 1;
2955         } else if (val & PIN_IN) {
2956                 idx = out_jacks;
2957                 if (in_jacks > 1) {
2958                         unsigned int vref_caps = get_vref_caps(codec, nid);
2959                         val &= AC_PINCTL_VREFEN;
2960                         idx += cvt_from_vref_idx(vref_caps, val);
2961                 }
2962         }
2963         return idx;
2964 }
2965
2966 static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2967                                 struct snd_ctl_elem_value *ucontrol)
2968 {
2969         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2970         hda_nid_t nid = kcontrol->private_value;
2971         ucontrol->value.enumerated.item[0] =
2972                 get_cur_hp_mic_jack_mode(codec, nid);
2973         return 0;
2974 }
2975
2976 static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2977                                 struct snd_ctl_elem_value *ucontrol)
2978 {
2979         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2980         hda_nid_t nid = kcontrol->private_value;
2981         int out_jacks = get_out_jack_num_items(codec, nid);
2982         int in_jacks = get_in_jack_num_items(codec, nid);
2983         unsigned int val, oldval, idx;
2984
2985         oldval = get_cur_hp_mic_jack_mode(codec, nid);
2986         idx = ucontrol->value.enumerated.item[0];
2987         if (oldval == idx)
2988                 return 0;
2989
2990         if (idx < out_jacks) {
2991                 if (out_jacks > 1)
2992                         val = idx ? PIN_HP : PIN_OUT;
2993                 else
2994                         val = PIN_HP;
2995         } else {
2996                 idx -= out_jacks;
2997                 if (in_jacks > 1) {
2998                         unsigned int vref_caps = get_vref_caps(codec, nid);
2999                         val = snd_hda_codec_get_pin_target(codec, nid);
3000                         val &= ~(AC_PINCTL_VREFEN | PIN_HP);
3001                         val |= get_vref_idx(vref_caps, idx) | PIN_IN;
3002                 } else
3003                         val = snd_hda_get_default_vref(codec, nid) | PIN_IN;
3004         }
3005         snd_hda_set_pin_ctl_cache(codec, nid, val);
3006         call_hp_automute(codec, NULL);
3007
3008         return 1;
3009 }
3010
3011 static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
3012         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3013         .info = hp_mic_jack_mode_info,
3014         .get = hp_mic_jack_mode_get,
3015         .put = hp_mic_jack_mode_put,
3016 };
3017
3018 static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
3019 {
3020         struct hda_gen_spec *spec = codec->spec;
3021         struct snd_kcontrol_new *knew;
3022
3023         knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
3024                                     &hp_mic_jack_mode_enum);
3025         if (!knew)
3026                 return -ENOMEM;
3027         knew->private_value = pin;
3028         spec->hp_mic_jack_modes = 1;
3029         return 0;
3030 }
3031
3032 /*
3033  * Parse input paths
3034  */
3035
3036 /* add the powersave loopback-list entry */
3037 static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
3038 {
3039         struct hda_amp_list *list;
3040
3041         list = snd_array_new(&spec->loopback_list);
3042         if (!list)
3043                 return -ENOMEM;
3044         list->nid = mix;
3045         list->dir = HDA_INPUT;
3046         list->idx = idx;
3047         spec->loopback.amplist = spec->loopback_list.list;
3048         return 0;
3049 }
3050
3051 /* return true if either a volume or a mute amp is found for the given
3052  * aamix path; the amp has to be either in the mixer node or its direct leaf
3053  */
3054 static bool look_for_mix_leaf_ctls(struct hda_codec *codec, hda_nid_t mix_nid,
3055                                    hda_nid_t pin, unsigned int *mix_val,
3056                                    unsigned int *mute_val)
3057 {
3058         int idx, num_conns;
3059         const hda_nid_t *list;
3060         hda_nid_t nid;
3061
3062         idx = snd_hda_get_conn_index(codec, mix_nid, pin, true);
3063         if (idx < 0)
3064                 return false;
3065
3066         *mix_val = *mute_val = 0;
3067         if (nid_has_volume(codec, mix_nid, HDA_INPUT))
3068                 *mix_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
3069         if (nid_has_mute(codec, mix_nid, HDA_INPUT))
3070                 *mute_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
3071         if (*mix_val && *mute_val)
3072                 return true;
3073
3074         /* check leaf node */
3075         num_conns = snd_hda_get_conn_list(codec, mix_nid, &list);
3076         if (num_conns < idx)
3077                 return false;
3078         nid = list[idx];
3079         if (!*mix_val && nid_has_volume(codec, nid, HDA_OUTPUT) &&
3080             !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_VOL_CTL))
3081                 *mix_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3082         if (!*mute_val && nid_has_mute(codec, nid, HDA_OUTPUT) &&
3083             !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_MUTE_CTL))
3084                 *mute_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3085
3086         return *mix_val || *mute_val;
3087 }
3088
3089 /* create input playback/capture controls for the given pin */
3090 static int new_analog_input(struct hda_codec *codec, int input_idx,
3091                             hda_nid_t pin, const char *ctlname, int ctlidx,
3092                             hda_nid_t mix_nid)
3093 {
3094         struct hda_gen_spec *spec = codec->spec;
3095         struct nid_path *path;
3096         unsigned int mix_val, mute_val;
3097         int err, idx;
3098
3099         if (!look_for_mix_leaf_ctls(codec, mix_nid, pin, &mix_val, &mute_val))
3100                 return 0;
3101
3102         path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
3103         if (!path)
3104                 return -EINVAL;
3105         print_nid_path(codec, "loopback", path);
3106         spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
3107
3108         idx = path->idx[path->depth - 1];
3109         if (mix_val) {
3110                 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, mix_val);
3111                 if (err < 0)
3112                         return err;
3113                 path->ctls[NID_PATH_VOL_CTL] = mix_val;
3114         }
3115
3116         if (mute_val) {
3117                 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, mute_val);
3118                 if (err < 0)
3119                         return err;
3120                 path->ctls[NID_PATH_MUTE_CTL] = mute_val;
3121         }
3122
3123         path->active = true;
3124         path->stream_enabled = true; /* no DAC/ADC involved */
3125         err = add_loopback_list(spec, mix_nid, idx);
3126         if (err < 0)
3127                 return err;
3128
3129         if (spec->mixer_nid != spec->mixer_merge_nid &&
3130             !spec->loopback_merge_path) {
3131                 path = snd_hda_add_new_path(codec, spec->mixer_nid,
3132                                             spec->mixer_merge_nid, 0);
3133                 if (path) {
3134                         print_nid_path(codec, "loopback-merge", path);
3135                         path->active = true;
3136                         path->pin_fixed = true; /* static route */
3137                         path->stream_enabled = true; /* no DAC/ADC involved */
3138                         spec->loopback_merge_path =
3139                                 snd_hda_get_path_idx(codec, path);
3140                 }
3141         }
3142
3143         return 0;
3144 }
3145
3146 static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
3147 {
3148         unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
3149         return (pincap & AC_PINCAP_IN) != 0;
3150 }
3151
3152 /* Parse the codec tree and retrieve ADCs */
3153 static int fill_adc_nids(struct hda_codec *codec)
3154 {
3155         struct hda_gen_spec *spec = codec->spec;
3156         hda_nid_t nid;
3157         hda_nid_t *adc_nids = spec->adc_nids;
3158         int max_nums = ARRAY_SIZE(spec->adc_nids);
3159         int nums = 0;
3160
3161         for_each_hda_codec_node(nid, codec) {
3162                 unsigned int caps = get_wcaps(codec, nid);
3163                 int type = get_wcaps_type(caps);
3164
3165                 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
3166                         continue;
3167                 adc_nids[nums] = nid;
3168                 if (++nums >= max_nums)
3169                         break;
3170         }
3171         spec->num_adc_nids = nums;
3172
3173         /* copy the detected ADCs to all_adcs[] */
3174         spec->num_all_adcs = nums;
3175         memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
3176
3177         return nums;
3178 }
3179
3180 /* filter out invalid adc_nids that don't give all active input pins;
3181  * if needed, check whether dynamic ADC-switching is available
3182  */
3183 static int check_dyn_adc_switch(struct hda_codec *codec)
3184 {
3185         struct hda_gen_spec *spec = codec->spec;
3186         struct hda_input_mux *imux = &spec->input_mux;
3187         unsigned int ok_bits;
3188         int i, n, nums;
3189
3190         nums = 0;
3191         ok_bits = 0;
3192         for (n = 0; n < spec->num_adc_nids; n++) {
3193                 for (i = 0; i < imux->num_items; i++) {
3194                         if (!spec->input_paths[i][n])
3195                                 break;
3196                 }
3197                 if (i >= imux->num_items) {
3198                         ok_bits |= (1 << n);
3199                         nums++;
3200                 }
3201         }
3202
3203         if (!ok_bits) {
3204                 /* check whether ADC-switch is possible */
3205                 for (i = 0; i < imux->num_items; i++) {
3206                         for (n = 0; n < spec->num_adc_nids; n++) {
3207                                 if (spec->input_paths[i][n]) {
3208                                         spec->dyn_adc_idx[i] = n;
3209                                         break;
3210                                 }
3211                         }
3212                 }
3213
3214                 codec_dbg(codec, "enabling ADC switching\n");
3215                 spec->dyn_adc_switch = 1;
3216         } else if (nums != spec->num_adc_nids) {
3217                 /* shrink the invalid adcs and input paths */
3218                 nums = 0;
3219                 for (n = 0; n < spec->num_adc_nids; n++) {
3220                         if (!(ok_bits & (1 << n)))
3221                                 continue;
3222                         if (n != nums) {
3223                                 spec->adc_nids[nums] = spec->adc_nids[n];
3224                                 for (i = 0; i < imux->num_items; i++) {
3225                                         invalidate_nid_path(codec,
3226                                                 spec->input_paths[i][nums]);
3227                                         spec->input_paths[i][nums] =
3228                                                 spec->input_paths[i][n];
3229                                         spec->input_paths[i][n] = 0;
3230                                 }
3231                         }
3232                         nums++;
3233                 }
3234                 spec->num_adc_nids = nums;
3235         }
3236
3237         if (imux->num_items == 1 ||
3238             (imux->num_items == 2 && spec->hp_mic)) {
3239                 codec_dbg(codec, "reducing to a single ADC\n");
3240                 spec->num_adc_nids = 1; /* reduce to a single ADC */
3241         }
3242
3243         /* single index for individual volumes ctls */
3244         if (!spec->dyn_adc_switch && spec->multi_cap_vol)
3245                 spec->num_adc_nids = 1;
3246
3247         return 0;
3248 }
3249
3250 /* parse capture source paths from the given pin and create imux items */
3251 static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
3252                                 int cfg_idx, int num_adcs,
3253                                 const char *label, int anchor)
3254 {
3255         struct hda_gen_spec *spec = codec->spec;
3256         struct hda_input_mux *imux = &spec->input_mux;
3257         int imux_idx = imux->num_items;
3258         bool imux_added = false;
3259         int c;
3260
3261         for (c = 0; c < num_adcs; c++) {
3262                 struct nid_path *path;
3263                 hda_nid_t adc = spec->adc_nids[c];
3264
3265                 if (!is_reachable_path(codec, pin, adc))
3266                         continue;
3267                 path = snd_hda_add_new_path(codec, pin, adc, anchor);
3268                 if (!path)
3269                         continue;
3270                 print_nid_path(codec, "input", path);
3271                 spec->input_paths[imux_idx][c] =
3272                         snd_hda_get_path_idx(codec, path);
3273
3274                 if (!imux_added) {
3275                         if (spec->hp_mic_pin == pin)
3276                                 spec->hp_mic_mux_idx = imux->num_items;
3277                         spec->imux_pins[imux->num_items] = pin;
3278                         snd_hda_add_imux_item(codec, imux, label, cfg_idx, NULL);
3279                         imux_added = true;
3280                         if (spec->dyn_adc_switch)
3281                                 spec->dyn_adc_idx[imux_idx] = c;
3282                 }
3283         }
3284
3285         return 0;
3286 }
3287
3288 /*
3289  * create playback/capture controls for input pins
3290  */
3291
3292 /* fill the label for each input at first */
3293 static int fill_input_pin_labels(struct hda_codec *codec)
3294 {
3295         struct hda_gen_spec *spec = codec->spec;
3296         const struct auto_pin_cfg *cfg = &spec->autocfg;
3297         int i;
3298
3299         for (i = 0; i < cfg->num_inputs; i++) {
3300                 hda_nid_t pin = cfg->inputs[i].pin;
3301                 const char *label;
3302                 int j, idx;
3303
3304                 if (!is_input_pin(codec, pin))
3305                         continue;
3306
3307                 label = hda_get_autocfg_input_label(codec, cfg, i);
3308                 idx = 0;
3309                 for (j = i - 1; j >= 0; j--) {
3310                         if (spec->input_labels[j] &&
3311                             !strcmp(spec->input_labels[j], label)) {
3312                                 idx = spec->input_label_idxs[j] + 1;
3313                                 break;
3314                         }
3315                 }
3316
3317                 spec->input_labels[i] = label;
3318                 spec->input_label_idxs[i] = idx;
3319         }
3320
3321         return 0;
3322 }
3323
3324 #define CFG_IDX_MIX     99      /* a dummy cfg->input idx for stereo mix */
3325
3326 static int create_input_ctls(struct hda_codec *codec)
3327 {
3328         struct hda_gen_spec *spec = codec->spec;
3329         const struct auto_pin_cfg *cfg = &spec->autocfg;
3330         hda_nid_t mixer = spec->mixer_nid;
3331         int num_adcs;
3332         int i, err;
3333         unsigned int val;
3334
3335         num_adcs = fill_adc_nids(codec);
3336         if (num_adcs < 0)
3337                 return 0;
3338
3339         err = fill_input_pin_labels(codec);
3340         if (err < 0)
3341                 return err;
3342
3343         for (i = 0; i < cfg->num_inputs; i++) {
3344                 hda_nid_t pin;
3345
3346                 pin = cfg->inputs[i].pin;
3347                 if (!is_input_pin(codec, pin))
3348                         continue;
3349
3350                 val = PIN_IN;
3351                 if (cfg->inputs[i].type == AUTO_PIN_MIC)
3352                         val |= snd_hda_get_default_vref(codec, pin);
3353                 if (pin != spec->hp_mic_pin &&
3354                     !snd_hda_codec_get_pin_target(codec, pin))
3355                         set_pin_target(codec, pin, val, false);
3356
3357                 if (mixer) {
3358                         if (is_reachable_path(codec, pin, mixer)) {
3359                                 err = new_analog_input(codec, i, pin,
3360                                                        spec->input_labels[i],
3361                                                        spec->input_label_idxs[i],
3362                                                        mixer);
3363                                 if (err < 0)
3364                                         return err;
3365                         }
3366                 }
3367
3368                 err = parse_capture_source(codec, pin, i, num_adcs,
3369                                            spec->input_labels[i], -mixer);
3370                 if (err < 0)
3371                         return err;
3372
3373                 if (spec->add_jack_modes) {
3374                         err = create_in_jack_mode(codec, pin);
3375                         if (err < 0)
3376                                 return err;
3377                 }
3378         }
3379
3380         /* add stereo mix when explicitly enabled via hint */
3381         if (mixer && spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_ENABLE) {
3382                 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
3383                                            "Stereo Mix", 0);
3384                 if (err < 0)
3385                         return err;
3386                 else
3387                         spec->suppress_auto_mic = 1;
3388         }
3389
3390         return 0;
3391 }
3392
3393
3394 /*
3395  * input source mux
3396  */
3397
3398 /* get the input path specified by the given adc and imux indices */
3399 static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
3400 {
3401         struct hda_gen_spec *spec = codec->spec;
3402         if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3403                 snd_BUG();
3404                 return NULL;
3405         }
3406         if (spec->dyn_adc_switch)
3407                 adc_idx = spec->dyn_adc_idx[imux_idx];
3408         if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
3409                 snd_BUG();
3410                 return NULL;
3411         }
3412         return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
3413 }
3414
3415 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3416                       unsigned int idx);
3417
3418 static int mux_enum_info(struct snd_kcontrol *kcontrol,
3419                          struct snd_ctl_elem_info *uinfo)
3420 {
3421         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3422         struct hda_gen_spec *spec = codec->spec;
3423         return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3424 }
3425
3426 static int mux_enum_get(struct snd_kcontrol *kcontrol,
3427                         struct snd_ctl_elem_value *ucontrol)
3428 {
3429         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3430         struct hda_gen_spec *spec = codec->spec;
3431         /* the ctls are created at once with multiple counts */
3432         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
3433
3434         ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3435         return 0;
3436 }
3437
3438 static int mux_enum_put(struct snd_kcontrol *kcontrol,
3439                             struct snd_ctl_elem_value *ucontrol)
3440 {
3441         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3442         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
3443         return mux_select(codec, adc_idx,
3444                           ucontrol->value.enumerated.item[0]);
3445 }
3446
3447 static const struct snd_kcontrol_new cap_src_temp = {
3448         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3449         .name = "Input Source",
3450         .info = mux_enum_info,
3451         .get = mux_enum_get,
3452         .put = mux_enum_put,
3453 };
3454
3455 /*
3456  * capture volume and capture switch ctls
3457  */
3458
3459 typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3460                           struct snd_ctl_elem_value *ucontrol);
3461
3462 /* call the given amp update function for all amps in the imux list at once */
3463 static int cap_put_caller(struct snd_kcontrol *kcontrol,
3464                           struct snd_ctl_elem_value *ucontrol,
3465                           put_call_t func, int type)
3466 {
3467         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3468         struct hda_gen_spec *spec = codec->spec;
3469         const struct hda_input_mux *imux;
3470         struct nid_path *path;
3471         int i, adc_idx, ret, err = 0;
3472
3473         imux = &spec->input_mux;
3474         adc_idx = kcontrol->id.index;
3475         mutex_lock(&codec->control_mutex);
3476         for (i = 0; i < imux->num_items; i++) {
3477                 path = get_input_path(codec, adc_idx, i);
3478                 if (!path || !path->ctls[type])
3479                         continue;
3480                 kcontrol->private_value = path->ctls[type];
3481                 ret = func(kcontrol, ucontrol);
3482                 if (ret < 0) {
3483                         err = ret;
3484                         break;
3485                 }
3486                 if (ret > 0)
3487                         err = 1;
3488         }
3489         mutex_unlock(&codec->control_mutex);
3490         if (err >= 0 && spec->cap_sync_hook)
3491                 spec->cap_sync_hook(codec, kcontrol, ucontrol);
3492         return err;
3493 }
3494
3495 /* capture volume ctl callbacks */
3496 #define cap_vol_info            snd_hda_mixer_amp_volume_info
3497 #define cap_vol_get             snd_hda_mixer_amp_volume_get
3498 #define cap_vol_tlv             snd_hda_mixer_amp_tlv
3499
3500 static int cap_vol_put(struct snd_kcontrol *kcontrol,
3501                        struct snd_ctl_elem_value *ucontrol)
3502 {
3503         return cap_put_caller(kcontrol, ucontrol,
3504                               snd_hda_mixer_amp_volume_put,
3505                               NID_PATH_VOL_CTL);
3506 }
3507
3508 static const struct snd_kcontrol_new cap_vol_temp = {
3509         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3510         .name = "Capture Volume",
3511         .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3512                    SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3513                    SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3514         .info = cap_vol_info,
3515         .get = cap_vol_get,
3516         .put = cap_vol_put,
3517         .tlv = { .c = cap_vol_tlv },
3518 };
3519
3520 /* capture switch ctl callbacks */
3521 #define cap_sw_info             snd_ctl_boolean_stereo_info
3522 #define cap_sw_get              snd_hda_mixer_amp_switch_get
3523
3524 static int cap_sw_put(struct snd_kcontrol *kcontrol,
3525                       struct snd_ctl_elem_value *ucontrol)
3526 {
3527         return cap_put_caller(kcontrol, ucontrol,
3528                               snd_hda_mixer_amp_switch_put,
3529                               NID_PATH_MUTE_CTL);
3530 }
3531
3532 static const struct snd_kcontrol_new cap_sw_temp = {
3533         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3534         .name = "Capture Switch",
3535         .info = cap_sw_info,
3536         .get = cap_sw_get,
3537         .put = cap_sw_put,
3538 };
3539
3540 static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3541 {
3542         hda_nid_t nid;
3543         int i, depth;
3544
3545         path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3546         for (depth = 0; depth < 3; depth++) {
3547                 if (depth >= path->depth)
3548                         return -EINVAL;
3549                 i = path->depth - depth - 1;
3550                 nid = path->path[i];
3551                 if (!path->ctls[NID_PATH_VOL_CTL]) {
3552                         if (nid_has_volume(codec, nid, HDA_OUTPUT))
3553                                 path->ctls[NID_PATH_VOL_CTL] =
3554                                         HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3555                         else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3556                                 int idx = path->idx[i];
3557                                 if (!depth && codec->single_adc_amp)
3558                                         idx = 0;
3559                                 path->ctls[NID_PATH_VOL_CTL] =
3560                                         HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3561                         }
3562                 }
3563                 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3564                         if (nid_has_mute(codec, nid, HDA_OUTPUT))
3565                                 path->ctls[NID_PATH_MUTE_CTL] =
3566                                         HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3567                         else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3568                                 int idx = path->idx[i];
3569                                 if (!depth && codec->single_adc_amp)
3570                                         idx = 0;
3571                                 path->ctls[NID_PATH_MUTE_CTL] =
3572                                         HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3573                         }
3574                 }
3575         }
3576         return 0;
3577 }
3578
3579 static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
3580 {
3581         struct hda_gen_spec *spec = codec->spec;
3582         struct auto_pin_cfg *cfg = &spec->autocfg;
3583         unsigned int val;
3584         int i;
3585
3586         if (!spec->inv_dmic_split)
3587                 return false;
3588         for (i = 0; i < cfg->num_inputs; i++) {
3589                 if (cfg->inputs[i].pin != nid)
3590                         continue;
3591                 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3592                         return false;
3593                 val = snd_hda_codec_get_pincfg(codec, nid);
3594                 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3595         }
3596         return false;
3597 }
3598
3599 /* capture switch put callback for a single control with hook call */
3600 static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3601                              struct snd_ctl_elem_value *ucontrol)
3602 {
3603         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3604         struct hda_gen_spec *spec = codec->spec;
3605         int ret;
3606
3607         ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3608         if (ret < 0)
3609                 return ret;
3610
3611         if (spec->cap_sync_hook)
3612                 spec->cap_sync_hook(codec, kcontrol, ucontrol);
3613
3614         return ret;
3615 }
3616
3617 static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3618                               int idx, bool is_switch, unsigned int ctl,
3619                               bool inv_dmic)
3620 {
3621         struct hda_gen_spec *spec = codec->spec;
3622         char tmpname[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3623         int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3624         const char *sfx = is_switch ? "Switch" : "Volume";
3625         unsigned int chs = inv_dmic ? 1 : 3;
3626         struct snd_kcontrol_new *knew;
3627
3628         if (!ctl)
3629                 return 0;
3630
3631         if (label)
3632                 snprintf(tmpname, sizeof(tmpname),
3633                          "%s Capture %s", label, sfx);
3634         else
3635                 snprintf(tmpname, sizeof(tmpname),
3636                          "Capture %s", sfx);
3637         knew = add_control(spec, type, tmpname, idx,
3638                            amp_val_replace_channels(ctl, chs));
3639         if (!knew)
3640                 return -ENOMEM;
3641         if (is_switch)
3642                 knew->put = cap_single_sw_put;
3643         if (!inv_dmic)
3644                 return 0;
3645
3646         /* Make independent right kcontrol */
3647         if (label)
3648                 snprintf(tmpname, sizeof(tmpname),
3649                          "Inverted %s Capture %s", label, sfx);
3650         else
3651                 snprintf(tmpname, sizeof(tmpname),
3652                          "Inverted Capture %s", sfx);
3653         knew = add_control(spec, type, tmpname, idx,
3654                            amp_val_replace_channels(ctl, 2));
3655         if (!knew)
3656                 return -ENOMEM;
3657         if (is_switch)
3658                 knew->put = cap_single_sw_put;
3659         return 0;
3660 }
3661
3662 /* create single (and simple) capture volume and switch controls */
3663 static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3664                                      unsigned int vol_ctl, unsigned int sw_ctl,
3665                                      bool inv_dmic)
3666 {
3667         int err;
3668         err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3669         if (err < 0)
3670                 return err;
3671         err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3672         if (err < 0)
3673                 return err;
3674         return 0;
3675 }
3676
3677 /* create bound capture volume and switch controls */
3678 static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3679                                    unsigned int vol_ctl, unsigned int sw_ctl)
3680 {
3681         struct hda_gen_spec *spec = codec->spec;
3682         struct snd_kcontrol_new *knew;
3683
3684         if (vol_ctl) {
3685                 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
3686                 if (!knew)
3687                         return -ENOMEM;
3688                 knew->index = idx;
3689                 knew->private_value = vol_ctl;
3690                 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3691         }
3692         if (sw_ctl) {
3693                 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
3694                 if (!knew)
3695                         return -ENOMEM;
3696                 knew->index = idx;
3697                 knew->private_value = sw_ctl;
3698                 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3699         }
3700         return 0;
3701 }
3702
3703 /* return the vol ctl when used first in the imux list */
3704 static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3705 {
3706         struct nid_path *path;
3707         unsigned int ctl;
3708         int i;
3709
3710         path = get_input_path(codec, 0, idx);
3711         if (!path)
3712                 return 0;
3713         ctl = path->ctls[type];
3714         if (!ctl)
3715                 return 0;
3716         for (i = 0; i < idx - 1; i++) {
3717                 path = get_input_path(codec, 0, i);
3718                 if (path && path->ctls[type] == ctl)
3719                         return 0;
3720         }
3721         return ctl;
3722 }
3723
3724 /* create individual capture volume and switch controls per input */
3725 static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3726 {
3727         struct hda_gen_spec *spec = codec->spec;
3728         struct hda_input_mux *imux = &spec->input_mux;
3729         int i, err, type;
3730
3731         for (i = 0; i < imux->num_items; i++) {
3732                 bool inv_dmic;
3733                 int idx;
3734
3735                 idx = imux->items[i].index;
3736                 if (idx >= spec->autocfg.num_inputs)
3737                         continue;
3738                 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3739
3740                 for (type = 0; type < 2; type++) {
3741                         err = add_single_cap_ctl(codec,
3742                                                  spec->input_labels[idx],
3743                                                  spec->input_label_idxs[idx],
3744                                                  type,
3745                                                  get_first_cap_ctl(codec, i, type),
3746                                                  inv_dmic);
3747                         if (err < 0)
3748                                 return err;
3749                 }
3750         }
3751         return 0;
3752 }
3753
3754 static int create_capture_mixers(struct hda_codec *codec)
3755 {
3756         struct hda_gen_spec *spec = codec->spec;
3757         struct hda_input_mux *imux = &spec->input_mux;
3758         int i, n, nums, err;
3759
3760         if (spec->dyn_adc_switch)
3761                 nums = 1;
3762         else
3763                 nums = spec->num_adc_nids;
3764
3765         if (!spec->auto_mic && imux->num_items > 1) {
3766                 struct snd_kcontrol_new *knew;
3767                 const char *name;
3768                 name = nums > 1 ? "Input Source" : "Capture Source";
3769                 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
3770                 if (!knew)
3771                         return -ENOMEM;
3772                 knew->count = nums;
3773         }
3774
3775         for (n = 0; n < nums; n++) {
3776                 bool multi = false;
3777                 bool multi_cap_vol = spec->multi_cap_vol;
3778                 bool inv_dmic = false;
3779                 int vol, sw;
3780
3781                 vol = sw = 0;
3782                 for (i = 0; i < imux->num_items; i++) {
3783                         struct nid_path *path;
3784                         path = get_input_path(codec, n, i);
3785                         if (!path)
3786                                 continue;
3787                         parse_capvol_in_path(codec, path);
3788                         if (!vol)
3789                                 vol = path->ctls[NID_PATH_VOL_CTL];
3790                         else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
3791                                 multi = true;
3792                                 if (!same_amp_caps(codec, vol,
3793                                     path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3794                                         multi_cap_vol = true;
3795                         }
3796                         if (!sw)
3797                                 sw = path->ctls[NID_PATH_MUTE_CTL];
3798                         else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
3799                                 multi = true;
3800                                 if (!same_amp_caps(codec, sw,
3801                                     path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3802                                         multi_cap_vol = true;
3803                         }
3804                         if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3805                                 inv_dmic = true;
3806                 }
3807
3808                 if (!multi)
3809                         err = create_single_cap_vol_ctl(codec, n, vol, sw,
3810                                                         inv_dmic);
3811                 else if (!multi_cap_vol && !inv_dmic)
3812                         err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3813                 else
3814                         err = create_multi_cap_vol_ctl(codec);
3815                 if (err < 0)
3816                         return err;
3817         }
3818
3819         return 0;
3820 }
3821
3822 /*
3823  * add mic boosts if needed
3824  */
3825
3826 /* check whether the given amp is feasible as a boost volume */
3827 static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3828                             int dir, int idx)
3829 {
3830         unsigned int step;
3831
3832         if (!nid_has_volume(codec, nid, dir) ||
3833             is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3834             is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3835                 return false;
3836
3837         step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3838                 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3839         if (step < 0x20)
3840                 return false;
3841         return true;
3842 }
3843
3844 /* look for a boost amp in a widget close to the pin */
3845 static unsigned int look_for_boost_amp(struct hda_codec *codec,
3846                                        struct nid_path *path)
3847 {
3848         unsigned int val = 0;
3849         hda_nid_t nid;
3850         int depth;
3851
3852         for (depth = 0; depth < 3; depth++) {
3853                 if (depth >= path->depth - 1)
3854                         break;
3855                 nid = path->path[depth];
3856                 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3857                         val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3858                         break;
3859                 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3860                                            path->idx[depth])) {
3861                         val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3862                                                   HDA_INPUT);
3863                         break;
3864                 }
3865         }
3866
3867         return val;
3868 }
3869
3870 static int parse_mic_boost(struct hda_codec *codec)
3871 {
3872         struct hda_gen_spec *spec = codec->spec;
3873         struct auto_pin_cfg *cfg = &spec->autocfg;
3874         struct hda_input_mux *imux = &spec->input_mux;
3875         int i;
3876
3877         if (!spec->num_adc_nids)
3878                 return 0;
3879
3880         for (i = 0; i < imux->num_items; i++) {
3881                 struct nid_path *path;
3882                 unsigned int val;
3883                 int idx;
3884                 char boost_label[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3885
3886                 idx = imux->items[i].index;
3887                 if (idx >= imux->num_items)
3888                         continue;
3889
3890                 /* check only line-in and mic pins */
3891                 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
3892                         continue;
3893
3894                 path = get_input_path(codec, 0, i);
3895                 if (!path)
3896                         continue;
3897
3898                 val = look_for_boost_amp(codec, path);
3899                 if (!val)
3900                         continue;
3901
3902                 /* create a boost control */
3903                 snprintf(boost_label, sizeof(boost_label),
3904                          "%s Boost Volume", spec->input_labels[idx]);
3905                 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3906                                  spec->input_label_idxs[idx], val))
3907                         return -ENOMEM;
3908
3909                 path->ctls[NID_PATH_BOOST_CTL] = val;
3910         }
3911         return 0;
3912 }
3913
3914 /*
3915  * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3916  */
3917 static void parse_digital(struct hda_codec *codec)
3918 {
3919         struct hda_gen_spec *spec = codec->spec;
3920         struct nid_path *path;
3921         int i, nums;
3922         hda_nid_t dig_nid, pin;
3923
3924         /* support multiple SPDIFs; the secondary is set up as a slave */
3925         nums = 0;
3926         for (i = 0; i < spec->autocfg.dig_outs; i++) {
3927                 pin = spec->autocfg.dig_out_pins[i];
3928                 dig_nid = look_for_dac(codec, pin, true);
3929                 if (!dig_nid)
3930                         continue;
3931                 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
3932                 if (!path)
3933                         continue;
3934                 print_nid_path(codec, "digout", path);
3935                 path->active = true;
3936                 path->pin_fixed = true; /* no jack detection */
3937                 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
3938                 set_pin_target(codec, pin, PIN_OUT, false);
3939                 if (!nums) {
3940                         spec->multiout.dig_out_nid = dig_nid;
3941                         spec->dig_out_type = spec->autocfg.dig_out_type[0];
3942                 } else {
3943                         spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3944                         if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
3945                                 break;
3946                         spec->slave_dig_outs[nums - 1] = dig_nid;
3947                 }
3948                 nums++;
3949         }
3950
3951         if (spec->autocfg.dig_in_pin) {
3952                 pin = spec->autocfg.dig_in_pin;
3953                 for_each_hda_codec_node(dig_nid, codec) {
3954                         unsigned int wcaps = get_wcaps(codec, dig_nid);
3955                         if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3956                                 continue;
3957                         if (!(wcaps & AC_WCAP_DIGITAL))
3958                                 continue;
3959                         path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
3960                         if (path) {
3961                                 print_nid_path(codec, "digin", path);
3962                                 path->active = true;
3963                                 path->pin_fixed = true; /* no jack */
3964                                 spec->dig_in_nid = dig_nid;
3965                                 spec->digin_path = snd_hda_get_path_idx(codec, path);
3966                                 set_pin_target(codec, pin, PIN_IN, false);
3967                                 break;
3968                         }
3969                 }
3970         }
3971 }
3972
3973
3974 /*
3975  * input MUX handling
3976  */
3977
3978 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3979
3980 /* select the given imux item; either unmute exclusively or select the route */
3981 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3982                       unsigned int idx)
3983 {
3984         struct hda_gen_spec *spec = codec->spec;
3985         const struct hda_input_mux *imux;
3986         struct nid_path *old_path, *path;
3987
3988         imux = &spec->input_mux;
3989         if (!imux->num_items)
3990                 return 0;
3991
3992         if (idx >= imux->num_items)
3993                 idx = imux->num_items - 1;
3994         if (spec->cur_mux[adc_idx] == idx)
3995                 return 0;
3996
3997         old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3998         if (!old_path)
3999                 return 0;
4000         if (old_path->active)
4001                 snd_hda_activate_path(codec, old_path, false, false);
4002
4003         spec->cur_mux[adc_idx] = idx;
4004
4005         if (spec->hp_mic)
4006                 update_hp_mic(codec, adc_idx, false);
4007
4008         if (spec->dyn_adc_switch)
4009                 dyn_adc_pcm_resetup(codec, idx);
4010
4011         path = get_input_path(codec, adc_idx, idx);
4012         if (!path)
4013                 return 0;
4014         if (path->active)
4015                 return 0;
4016         snd_hda_activate_path(codec, path, true, false);
4017         if (spec->cap_sync_hook)
4018                 spec->cap_sync_hook(codec, NULL, NULL);
4019         path_power_down_sync(codec, old_path);
4020         return 1;
4021 }
4022
4023 /* power up/down widgets in the all paths that match with the given NID
4024  * as terminals (either start- or endpoint)
4025  *
4026  * returns the last changed NID, or zero if unchanged.
4027  */
4028 static hda_nid_t set_path_power(struct hda_codec *codec, hda_nid_t nid,
4029                                 int pin_state, int stream_state)
4030 {
4031         struct hda_gen_spec *spec = codec->spec;
4032         hda_nid_t last, changed = 0;
4033         struct nid_path *path;
4034         int n;
4035
4036         for (n = 0; n < spec->paths.used; n++) {
4037                 path = snd_array_elem(&spec->paths, n);
4038                 if (!path->depth)
4039                         continue;
4040                 if (path->path[0] == nid ||
4041                     path->path[path->depth - 1] == nid) {
4042                         bool pin_old = path->pin_enabled;
4043                         bool stream_old = path->stream_enabled;
4044
4045                         if (pin_state >= 0)
4046                                 path->pin_enabled = pin_state;
4047                         if (stream_state >= 0)
4048                                 path->stream_enabled = stream_state;
4049                         if ((!path->pin_fixed && path->pin_enabled != pin_old)
4050                             || path->stream_enabled != stream_old) {
4051                                 last = path_power_update(codec, path, true);
4052                                 if (last)
4053                                         changed = last;
4054                         }
4055                 }
4056         }
4057         return changed;
4058 }
4059
4060 /* check the jack status for power control */
4061 static bool detect_pin_state(struct hda_codec *codec, hda_nid_t pin)
4062 {
4063         if (!is_jack_detectable(codec, pin))
4064                 return true;
4065         return snd_hda_jack_detect_state(codec, pin) != HDA_JACK_NOT_PRESENT;
4066 }
4067
4068 /* power up/down the paths of the given pin according to the jack state;
4069  * power = 0/1 : only power up/down if it matches with the jack state,
4070  *       < 0   : force power up/down to follow the jack sate
4071  *
4072  * returns the last changed NID, or zero if unchanged.
4073  */
4074 static hda_nid_t set_pin_power_jack(struct hda_codec *codec, hda_nid_t pin,
4075                                     int power)
4076 {
4077         bool on;
4078
4079         if (!codec->power_save_node)
4080                 return 0;
4081
4082         on = detect_pin_state(codec, pin);
4083
4084         if (power >= 0 && on != power)
4085                 return 0;
4086         return set_path_power(codec, pin, on, -1);
4087 }
4088
4089 static void pin_power_callback(struct hda_codec *codec,
4090                                struct hda_jack_callback *jack,
4091                                bool on)
4092 {
4093         if (jack && jack->nid)
4094                 sync_power_state_change(codec,
4095                                         set_pin_power_jack(codec, jack->nid, on));
4096 }
4097
4098 /* callback only doing power up -- called at first */
4099 static void pin_power_up_callback(struct hda_codec *codec,
4100                                   struct hda_jack_callback *jack)
4101 {
4102         pin_power_callback(codec, jack, true);
4103 }
4104
4105 /* callback only doing power down -- called at last */
4106 static void pin_power_down_callback(struct hda_codec *codec,
4107                                     struct hda_jack_callback *jack)
4108 {
4109         pin_power_callback(codec, jack, false);
4110 }
4111
4112 /* set up the power up/down callbacks */
4113 static void add_pin_power_ctls(struct hda_codec *codec, int num_pins,
4114                                const hda_nid_t *pins, bool on)
4115 {
4116         int i;
4117         hda_jack_callback_fn cb =
4118                 on ? pin_power_up_callback : pin_power_down_callback;
4119
4120         for (i = 0; i < num_pins && pins[i]; i++) {
4121                 if (is_jack_detectable(codec, pins[i]))
4122                         snd_hda_jack_detect_enable_callback(codec, pins[i], cb);
4123                 else
4124                         set_path_power(codec, pins[i], true, -1);
4125         }
4126 }
4127
4128 /* enabled power callback to each available I/O pin with jack detections;
4129  * the digital I/O pins are excluded because of the unreliable detectsion
4130  */
4131 static void add_all_pin_power_ctls(struct hda_codec *codec, bool on)
4132 {
4133         struct hda_gen_spec *spec = codec->spec;
4134         struct auto_pin_cfg *cfg = &spec->autocfg;
4135         int i;
4136
4137         if (!codec->power_save_node)
4138                 return;
4139         add_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins, on);
4140         if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4141                 add_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins, on);
4142         if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4143                 add_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins, on);
4144         for (i = 0; i < cfg->num_inputs; i++)
4145                 add_pin_power_ctls(codec, 1, &cfg->inputs[i].pin, on);
4146 }
4147
4148 /* sync path power up/down with the jack states of given pins */
4149 static void sync_pin_power_ctls(struct hda_codec *codec, int num_pins,
4150                                 const hda_nid_t *pins)
4151 {
4152         int i;
4153
4154         for (i = 0; i < num_pins && pins[i]; i++)
4155                 if (is_jack_detectable(codec, pins[i]))
4156                         set_pin_power_jack(codec, pins[i], -1);
4157 }
4158
4159 /* sync path power up/down with pins; called at init and resume */
4160 static void sync_all_pin_power_ctls(struct hda_codec *codec)
4161 {
4162         struct hda_gen_spec *spec = codec->spec;
4163         struct auto_pin_cfg *cfg = &spec->autocfg;
4164         int i;
4165
4166         if (!codec->power_save_node)
4167                 return;
4168         sync_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins);
4169         if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4170                 sync_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins);
4171         if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4172                 sync_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins);
4173         for (i = 0; i < cfg->num_inputs; i++)
4174                 sync_pin_power_ctls(codec, 1, &cfg->inputs[i].pin);
4175 }
4176
4177 /* add fake paths if not present yet */
4178 static int add_fake_paths(struct hda_codec *codec, hda_nid_t nid,
4179                            int num_pins, const hda_nid_t *pins)
4180 {
4181         struct hda_gen_spec *spec = codec->spec;
4182         struct nid_path *path;
4183         int i;
4184
4185         for (i = 0; i < num_pins; i++) {
4186                 if (!pins[i])
4187                         break;
4188                 if (get_nid_path(codec, nid, pins[i], 0))
4189                         continue;
4190                 path = snd_array_new(&spec->paths);
4191                 if (!path)
4192                         return -ENOMEM;
4193                 memset(path, 0, sizeof(*path));
4194                 path->depth = 2;
4195                 path->path[0] = nid;
4196                 path->path[1] = pins[i];
4197                 path->active = true;
4198         }
4199         return 0;
4200 }
4201
4202 /* create fake paths to all outputs from beep */
4203 static int add_fake_beep_paths(struct hda_codec *codec)
4204 {
4205         struct hda_gen_spec *spec = codec->spec;
4206         struct auto_pin_cfg *cfg = &spec->autocfg;
4207         hda_nid_t nid = spec->beep_nid;
4208         int err;
4209
4210         if (!codec->power_save_node || !nid)
4211                 return 0;
4212         err = add_fake_paths(codec, nid, cfg->line_outs, cfg->line_out_pins);
4213         if (err < 0)
4214                 return err;
4215         if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4216                 err = add_fake_paths(codec, nid, cfg->hp_outs, cfg->hp_pins);
4217                 if (err < 0)
4218                         return err;
4219         }
4220         if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4221                 err = add_fake_paths(codec, nid, cfg->speaker_outs,
4222                                      cfg->speaker_pins);
4223                 if (err < 0)
4224                         return err;
4225         }
4226         return 0;
4227 }
4228
4229 /* power up/down beep widget and its output paths */
4230 static void beep_power_hook(struct hda_beep *beep, bool on)
4231 {
4232         set_path_power(beep->codec, beep->nid, -1, on);
4233 }
4234
4235 /**
4236  * snd_hda_gen_fix_pin_power - Fix the power of the given pin widget to D0
4237  * @codec: the HDA codec
4238  * @pin: NID of pin to fix
4239  */
4240 int snd_hda_gen_fix_pin_power(struct hda_codec *codec, hda_nid_t pin)
4241 {
4242         struct hda_gen_spec *spec = codec->spec;
4243         struct nid_path *path;
4244
4245         path = snd_array_new(&spec->paths);
4246         if (!path)
4247                 return -ENOMEM;
4248         memset(path, 0, sizeof(*path));
4249         path->depth = 1;
4250         path->path[0] = pin;
4251         path->active = true;
4252         path->pin_fixed = true;
4253         path->stream_enabled = true;
4254         return 0;
4255 }
4256 EXPORT_SYMBOL_GPL(snd_hda_gen_fix_pin_power);
4257
4258 /*
4259  * Jack detections for HP auto-mute and mic-switch
4260  */
4261
4262 /* check each pin in the given array; returns true if any of them is plugged */
4263 static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
4264 {
4265         int i;
4266         bool present = false;
4267
4268         for (i = 0; i < num_pins; i++) {
4269                 hda_nid_t nid = pins[i];
4270                 if (!nid)
4271                         break;
4272                 /* don't detect pins retasked as inputs */
4273                 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
4274                         continue;
4275                 if (snd_hda_jack_detect_state(codec, nid) == HDA_JACK_PRESENT)
4276                         present = true;
4277         }
4278         return present;
4279 }
4280
4281 /* standard HP/line-out auto-mute helper */
4282 static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
4283                         int *paths, bool mute)
4284 {
4285         struct hda_gen_spec *spec = codec->spec;
4286         int i;
4287
4288         for (i = 0; i < num_pins; i++) {
4289                 hda_nid_t nid = pins[i];
4290                 unsigned int val, oldval;
4291                 if (!nid)
4292                         break;
4293
4294                 oldval = snd_hda_codec_get_pin_target(codec, nid);
4295                 if (oldval & PIN_IN)
4296                         continue; /* no mute for inputs */
4297
4298                 if (spec->auto_mute_via_amp) {
4299                         struct nid_path *path;
4300                         hda_nid_t mute_nid;
4301
4302                         path = snd_hda_get_path_from_idx(codec, paths[i]);
4303                         if (!path)
4304                                 continue;
4305                         mute_nid = get_amp_nid_(path->ctls[NID_PATH_MUTE_CTL]);
4306                         if (!mute_nid)
4307                                 continue;
4308                         if (mute)
4309                                 spec->mute_bits |= (1ULL << mute_nid);
4310                         else
4311                                 spec->mute_bits &= ~(1ULL << mute_nid);
4312                         continue;
4313                 } else {
4314                         /* don't reset VREF value in case it's controlling
4315                          * the amp (see alc861_fixup_asus_amp_vref_0f())
4316                          */
4317                         if (spec->keep_vref_in_automute)
4318                                 val = oldval & ~PIN_HP;
4319                         else
4320                                 val = 0;
4321                         if (!mute)
4322                                 val |= oldval;
4323                         /* here we call update_pin_ctl() so that the pinctl is
4324                          * changed without changing the pinctl target value;
4325                          * the original target value will be still referred at
4326                          * the init / resume again
4327                          */
4328                         update_pin_ctl(codec, nid, val);
4329                 }
4330
4331                 set_pin_eapd(codec, nid, !mute);
4332                 if (codec->power_save_node) {
4333                         bool on = !mute;
4334                         if (on)
4335                                 on = detect_pin_state(codec, nid);
4336                         set_path_power(codec, nid, on, -1);
4337                 }
4338         }
4339 }
4340
4341 /**
4342  * snd_hda_gen_update_outputs - Toggle outputs muting
4343  * @codec: the HDA codec
4344  *
4345  * Update the mute status of all outputs based on the current jack states.
4346  */
4347 void snd_hda_gen_update_outputs(struct hda_codec *codec)
4348 {
4349         struct hda_gen_spec *spec = codec->spec;
4350         int *paths;
4351         int on;
4352
4353         /* Control HP pins/amps depending on master_mute state;
4354          * in general, HP pins/amps control should be enabled in all cases,
4355          * but currently set only for master_mute, just to be safe
4356          */
4357         if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
4358                 paths = spec->out_paths;
4359         else
4360                 paths = spec->hp_paths;
4361         do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
4362                     spec->autocfg.hp_pins, paths, spec->master_mute);
4363
4364         if (!spec->automute_speaker)
4365                 on = 0;
4366         else
4367                 on = spec->hp_jack_present | spec->line_jack_present;
4368         on |= spec->master_mute;
4369         spec->speaker_muted = on;
4370         if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4371                 paths = spec->out_paths;
4372         else
4373                 paths = spec->speaker_paths;
4374         do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
4375                     spec->autocfg.speaker_pins, paths, on);
4376
4377         /* toggle line-out mutes if needed, too */
4378         /* if LO is a copy of either HP or Speaker, don't need to handle it */
4379         if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
4380             spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
4381                 return;
4382         if (!spec->automute_lo)
4383                 on = 0;
4384         else
4385                 on = spec->hp_jack_present;
4386         on |= spec->master_mute;
4387         spec->line_out_muted = on;
4388         paths = spec->out_paths;
4389         do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
4390                     spec->autocfg.line_out_pins, paths, on);
4391 }
4392 EXPORT_SYMBOL_GPL(snd_hda_gen_update_outputs);
4393
4394 static void call_update_outputs(struct hda_codec *codec)
4395 {
4396         struct hda_gen_spec *spec = codec->spec;
4397         if (spec->automute_hook)
4398                 spec->automute_hook(codec);
4399         else
4400                 snd_hda_gen_update_outputs(codec);
4401
4402         /* sync the whole vmaster slaves to reflect the new auto-mute status */
4403         if (spec->auto_mute_via_amp && !codec->bus->shutdown)
4404                 snd_ctl_sync_vmaster(spec->vmaster_mute.sw_kctl, false);
4405 }
4406
4407 /**
4408  * snd_hda_gen_hp_automute - standard HP-automute helper
4409  * @codec: the HDA codec
4410  * @jack: jack object, NULL for the whole
4411  */
4412 void snd_hda_gen_hp_automute(struct hda_codec *codec,
4413                              struct hda_jack_callback *jack)
4414 {
4415         struct hda_gen_spec *spec = codec->spec;
4416         hda_nid_t *pins = spec->autocfg.hp_pins;
4417         int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
4418
4419         /* No detection for the first HP jack during indep-HP mode */
4420         if (spec->indep_hp_enabled) {
4421                 pins++;
4422                 num_pins--;
4423         }
4424
4425         spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
4426         if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
4427                 return;
4428         call_update_outputs(codec);
4429 }
4430 EXPORT_SYMBOL_GPL(snd_hda_gen_hp_automute);
4431
4432 /**
4433  * snd_hda_gen_line_automute - standard line-out-automute helper
4434  * @codec: the HDA codec
4435  * @jack: jack object, NULL for the whole
4436  */
4437 void snd_hda_gen_line_automute(struct hda_codec *codec,
4438                                struct hda_jack_callback *jack)
4439 {
4440         struct hda_gen_spec *spec = codec->spec;
4441
4442         if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4443                 return;
4444         /* check LO jack only when it's different from HP */
4445         if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
4446                 return;
4447
4448         spec->line_jack_present =
4449                 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
4450                              spec->autocfg.line_out_pins);
4451         if (!spec->automute_speaker || !spec->detect_lo)
4452                 return;
4453         call_update_outputs(codec);
4454 }
4455 EXPORT_SYMBOL_GPL(snd_hda_gen_line_automute);
4456
4457 /**
4458  * snd_hda_gen_mic_autoswitch - standard mic auto-switch helper
4459  * @codec: the HDA codec
4460  * @jack: jack object, NULL for the whole
4461  */
4462 void snd_hda_gen_mic_autoswitch(struct hda_codec *codec,
4463                                 struct hda_jack_callback *jack)
4464 {
4465         struct hda_gen_spec *spec = codec->spec;
4466         int i;
4467
4468         if (!spec->auto_mic)
4469                 return;
4470
4471         for (i = spec->am_num_entries - 1; i > 0; i--) {
4472                 hda_nid_t pin = spec->am_entry[i].pin;
4473                 /* don't detect pins retasked as outputs */
4474                 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
4475                         continue;
4476                 if (snd_hda_jack_detect_state(codec, pin) == HDA_JACK_PRESENT) {
4477                         mux_select(codec, 0, spec->am_entry[i].idx);
4478                         return;
4479                 }
4480         }
4481         mux_select(codec, 0, spec->am_entry[0].idx);
4482 }
4483 EXPORT_SYMBOL_GPL(snd_hda_gen_mic_autoswitch);
4484
4485 /* call appropriate hooks */
4486 static void call_hp_automute(struct hda_codec *codec,
4487                              struct hda_jack_callback *jack)
4488 {
4489         struct hda_gen_spec *spec = codec->spec;
4490         if (spec->hp_automute_hook)
4491                 spec->hp_automute_hook(codec, jack);
4492         else
4493                 snd_hda_gen_hp_automute(codec, jack);
4494 }
4495
4496 static void call_line_automute(struct hda_codec *codec,
4497                                struct hda_jack_callback *jack)
4498 {
4499         struct hda_gen_spec *spec = codec->spec;
4500         if (spec->line_automute_hook)
4501                 spec->line_automute_hook(codec, jack);
4502         else
4503                 snd_hda_gen_line_automute(codec, jack);
4504 }
4505
4506 static void call_mic_autoswitch(struct hda_codec *codec,
4507                                 struct hda_jack_callback *jack)
4508 {
4509         struct hda_gen_spec *spec = codec->spec;
4510         if (spec->mic_autoswitch_hook)
4511                 spec->mic_autoswitch_hook(codec, jack);
4512         else
4513                 snd_hda_gen_mic_autoswitch(codec, jack);
4514 }
4515
4516 /* update jack retasking */
4517 static void update_automute_all(struct hda_codec *codec)
4518 {
4519         call_hp_automute(codec, NULL);
4520         call_line_automute(codec, NULL);
4521         call_mic_autoswitch(codec, NULL);
4522 }
4523
4524 /*
4525  * Auto-Mute mode mixer enum support
4526  */
4527 static int automute_mode_info(struct snd_kcontrol *kcontrol,
4528                               struct snd_ctl_elem_info *uinfo)
4529 {
4530         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4531         struct hda_gen_spec *spec = codec->spec;
4532         static const char * const texts3[] = {
4533                 "Disabled", "Speaker Only", "Line Out+Speaker"
4534         };
4535
4536         if (spec->automute_speaker_possible && spec->automute_lo_possible)
4537                 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
4538         return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
4539 }
4540
4541 static int automute_mode_get(struct snd_kcontrol *kcontrol,
4542                              struct snd_ctl_elem_value *ucontrol)
4543 {
4544         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4545         struct hda_gen_spec *spec = codec->spec;
4546         unsigned int val = 0;
4547         if (spec->automute_speaker)
4548                 val++;
4549         if (spec->automute_lo)
4550                 val++;
4551
4552         ucontrol->value.enumerated.item[0] = val;
4553         return 0;
4554 }
4555
4556 static int automute_mode_put(struct snd_kcontrol *kcontrol,
4557                              struct snd_ctl_elem_value *ucontrol)
4558 {
4559         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4560         struct hda_gen_spec *spec = codec->spec;
4561
4562         switch (ucontrol->value.enumerated.item[0]) {
4563         case 0:
4564                 if (!spec->automute_speaker && !spec->automute_lo)
4565                         return 0;
4566                 spec->automute_speaker = 0;
4567                 spec->automute_lo = 0;
4568                 break;
4569         case 1:
4570                 if (spec->automute_speaker_possible) {
4571                         if (!spec->automute_lo && spec->automute_speaker)
4572                                 return 0;
4573                         spec->automute_speaker = 1;
4574                         spec->automute_lo = 0;
4575                 } else if (spec->automute_lo_possible) {
4576                         if (spec->automute_lo)
4577                                 return 0;
4578                         spec->automute_lo = 1;
4579                 } else
4580                         return -EINVAL;
4581                 break;
4582         case 2:
4583                 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
4584                         return -EINVAL;
4585                 if (spec->automute_speaker && spec->automute_lo)
4586                         return 0;
4587                 spec->automute_speaker = 1;
4588                 spec->automute_lo = 1;
4589                 break;
4590         default:
4591                 return -EINVAL;
4592         }
4593         call_update_outputs(codec);
4594         return 1;
4595 }
4596
4597 static const struct snd_kcontrol_new automute_mode_enum = {
4598         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4599         .name = "Auto-Mute Mode",
4600         .info = automute_mode_info,
4601         .get = automute_mode_get,
4602         .put = automute_mode_put,
4603 };
4604
4605 static int add_automute_mode_enum(struct hda_codec *codec)
4606 {
4607         struct hda_gen_spec *spec = codec->spec;
4608
4609         if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
4610                 return -ENOMEM;
4611         return 0;
4612 }
4613
4614 /*
4615  * Check the availability of HP/line-out auto-mute;
4616  * Set up appropriately if really supported
4617  */
4618 static int check_auto_mute_availability(struct hda_codec *codec)
4619 {
4620         struct hda_gen_spec *spec = codec->spec;
4621         struct auto_pin_cfg *cfg = &spec->autocfg;
4622         int present = 0;
4623         int i, err;
4624
4625         if (spec->suppress_auto_mute)
4626                 return 0;
4627
4628         if (cfg->hp_pins[0])
4629                 present++;
4630         if (cfg->line_out_pins[0])
4631                 present++;
4632         if (cfg->speaker_pins[0])
4633                 present++;
4634         if (present < 2) /* need two different output types */
4635                 return 0;
4636
4637         if (!cfg->speaker_pins[0] &&
4638             cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
4639                 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4640                        sizeof(cfg->speaker_pins));
4641                 cfg->speaker_outs = cfg->line_outs;
4642         }
4643
4644         if (!cfg->hp_pins[0] &&
4645             cfg->line_out_type == AUTO_PIN_HP_OUT) {
4646                 memcpy(cfg->hp_pins, cfg->line_out_pins,
4647                        sizeof(cfg->hp_pins));
4648                 cfg->hp_outs = cfg->line_outs;
4649         }
4650
4651         for (i = 0; i < cfg->hp_outs; i++) {
4652                 hda_nid_t nid = cfg->hp_pins[i];
4653                 if (!is_jack_detectable(codec, nid))
4654                         continue;
4655                 codec_dbg(codec, "Enable HP auto-muting on NID 0x%x\n", nid);
4656                 snd_hda_jack_detect_enable_callback(codec, nid,
4657                                                     call_hp_automute);
4658                 spec->detect_hp = 1;
4659         }
4660
4661         if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
4662                 if (cfg->speaker_outs)
4663                         for (i = 0; i < cfg->line_outs; i++) {
4664                                 hda_nid_t nid = cfg->line_out_pins[i];
4665                                 if (!is_jack_detectable(codec, nid))
4666                                         continue;
4667                                 codec_dbg(codec, "Enable Line-Out auto-muting on NID 0x%x\n", nid);
4668                                 snd_hda_jack_detect_enable_callback(codec, nid,
4669                                                                     call_line_automute);
4670                                 spec->detect_lo = 1;
4671                         }
4672                 spec->automute_lo_possible = spec->detect_hp;
4673         }
4674
4675         spec->automute_speaker_possible = cfg->speaker_outs &&
4676                 (spec->detect_hp || spec->detect_lo);
4677
4678         spec->automute_lo = spec->automute_lo_possible;
4679         spec->automute_speaker = spec->automute_speaker_possible;
4680
4681         if (spec->automute_speaker_possible || spec->automute_lo_possible) {
4682                 /* create a control for automute mode */
4683                 err = add_automute_mode_enum(codec);
4684                 if (err < 0)
4685                         return err;
4686         }
4687         return 0;
4688 }
4689
4690 /* check whether all auto-mic pins are valid; setup indices if OK */
4691 static bool auto_mic_check_imux(struct hda_codec *codec)
4692 {
4693         struct hda_gen_spec *spec = codec->spec;
4694         const struct hda_input_mux *imux;
4695         int i;
4696
4697         imux = &spec->input_mux;
4698         for (i = 0; i < spec->am_num_entries; i++) {
4699                 spec->am_entry[i].idx =
4700                         find_idx_in_nid_list(spec->am_entry[i].pin,
4701                                              spec->imux_pins, imux->num_items);
4702                 if (spec->am_entry[i].idx < 0)
4703                         return false; /* no corresponding imux */
4704         }
4705
4706         /* we don't need the jack detection for the first pin */
4707         for (i = 1; i < spec->am_num_entries; i++)
4708                 snd_hda_jack_detect_enable_callback(codec,
4709                                                     spec->am_entry[i].pin,
4710                                                     call_mic_autoswitch);
4711         return true;
4712 }
4713
4714 static int compare_attr(const void *ap, const void *bp)
4715 {
4716         const struct automic_entry *a = ap;
4717         const struct automic_entry *b = bp;
4718         return (int)(a->attr - b->attr);
4719 }
4720
4721 /*
4722  * Check the availability of auto-mic switch;
4723  * Set up if really supported
4724  */
4725 static int check_auto_mic_availability(struct hda_codec *codec)
4726 {
4727         struct hda_gen_spec *spec = codec->spec;
4728         struct auto_pin_cfg *cfg = &spec->autocfg;
4729         unsigned int types;
4730         int i, num_pins;
4731
4732         if (spec->suppress_auto_mic)
4733                 return 0;
4734
4735         types = 0;
4736         num_pins = 0;
4737         for (i = 0; i < cfg->num_inputs; i++) {
4738                 hda_nid_t nid = cfg->inputs[i].pin;
4739                 unsigned int attr;
4740                 attr = snd_hda_codec_get_pincfg(codec, nid);
4741                 attr = snd_hda_get_input_pin_attr(attr);
4742                 if (types & (1 << attr))
4743                         return 0; /* already occupied */
4744                 switch (attr) {
4745                 case INPUT_PIN_ATTR_INT:
4746                         if (cfg->inputs[i].type != AUTO_PIN_MIC)
4747                                 return 0; /* invalid type */
4748                         break;
4749                 case INPUT_PIN_ATTR_UNUSED:
4750                         return 0; /* invalid entry */
4751                 default:
4752                         if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4753                                 return 0; /* invalid type */
4754                         if (!spec->line_in_auto_switch &&
4755                             cfg->inputs[i].type != AUTO_PIN_MIC)
4756                                 return 0; /* only mic is allowed */
4757                         if (!is_jack_detectable(codec, nid))
4758                                 return 0; /* no unsol support */
4759                         break;
4760                 }
4761                 if (num_pins >= MAX_AUTO_MIC_PINS)
4762                         return 0;
4763                 types |= (1 << attr);
4764                 spec->am_entry[num_pins].pin = nid;
4765                 spec->am_entry[num_pins].attr = attr;
4766                 num_pins++;
4767         }
4768
4769         if (num_pins < 2)
4770                 return 0;
4771
4772         spec->am_num_entries = num_pins;
4773         /* sort the am_entry in the order of attr so that the pin with a
4774          * higher attr will be selected when the jack is plugged.
4775          */
4776         sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4777              compare_attr, NULL);
4778
4779         if (!auto_mic_check_imux(codec))
4780                 return 0;
4781
4782         spec->auto_mic = 1;
4783         spec->num_adc_nids = 1;
4784         spec->cur_mux[0] = spec->am_entry[0].idx;
4785         codec_dbg(codec, "Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
4786                     spec->am_entry[0].pin,
4787                     spec->am_entry[1].pin,
4788                     spec->am_entry[2].pin);
4789
4790         return 0;
4791 }
4792
4793 /**
4794  * snd_hda_gen_path_power_filter - power_filter hook to make inactive widgets
4795  * into power down
4796  * @codec: the HDA codec
4797  * @nid: NID to evalute
4798  * @power_state: target power state
4799  */
4800 unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
4801                                                   hda_nid_t nid,
4802                                                   unsigned int power_state)
4803 {
4804         struct hda_gen_spec *spec = codec->spec;
4805
4806         if (!spec->power_down_unused && !codec->power_save_node)
4807                 return power_state;
4808         if (power_state != AC_PWRST_D0 || nid == codec->core.afg)
4809                 return power_state;
4810         if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4811                 return power_state;
4812         if (is_active_nid_for_any(codec, nid))
4813                 return power_state;
4814         return AC_PWRST_D3;
4815 }
4816 EXPORT_SYMBOL_GPL(snd_hda_gen_path_power_filter);
4817
4818 /* mute all aamix inputs initially; parse up to the first leaves */
4819 static void mute_all_mixer_nid(struct hda_codec *codec, hda_nid_t mix)
4820 {
4821         int i, nums;
4822         const hda_nid_t *conn;
4823         bool has_amp;
4824
4825         nums = snd_hda_get_conn_list(codec, mix, &conn);
4826         has_amp = nid_has_mute(codec, mix, HDA_INPUT);
4827         for (i = 0; i < nums; i++) {
4828                 if (has_amp)
4829                         update_amp(codec, mix, HDA_INPUT, i,
4830                                    0xff, HDA_AMP_MUTE);
4831                 else if (nid_has_volume(codec, conn[i], HDA_OUTPUT))
4832                         update_amp(codec, conn[i], HDA_OUTPUT, 0,
4833                                    0xff, HDA_AMP_MUTE);
4834         }
4835 }
4836
4837 /**
4838  * snd_hda_gen_stream_pm - Stream power management callback
4839  * @codec: the HDA codec
4840  * @nid: audio widget
4841  * @on: power on/off flag
4842  *
4843  * Set this in patch_ops.stream_pm.  Only valid with power_save_node flag.
4844  */
4845 void snd_hda_gen_stream_pm(struct hda_codec *codec, hda_nid_t nid, bool on)
4846 {
4847         if (codec->power_save_node)
4848                 set_path_power(codec, nid, -1, on);
4849 }
4850 EXPORT_SYMBOL_GPL(snd_hda_gen_stream_pm);
4851
4852 /**
4853  * snd_hda_gen_parse_auto_config - Parse the given BIOS configuration and
4854  * set up the hda_gen_spec
4855  * @codec: the HDA codec
4856  * @cfg: Parsed pin configuration
4857  *
4858  * return 1 if successful, 0 if the proper config is not found,
4859  * or a negative error code
4860  */
4861 int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
4862                                   struct auto_pin_cfg *cfg)
4863 {
4864         struct hda_gen_spec *spec = codec->spec;
4865         int err;
4866
4867         parse_user_hints(codec);
4868
4869         if (spec->mixer_nid && !spec->mixer_merge_nid)
4870                 spec->mixer_merge_nid = spec->mixer_nid;
4871
4872         if (cfg != &spec->autocfg) {
4873                 spec->autocfg = *cfg;
4874                 cfg = &spec->autocfg;
4875         }
4876
4877         if (!spec->main_out_badness)
4878                 spec->main_out_badness = &hda_main_out_badness;
4879         if (!spec->extra_out_badness)
4880                 spec->extra_out_badness = &hda_extra_out_badness;
4881
4882         fill_all_dac_nids(codec);
4883
4884         if (!cfg->line_outs) {
4885                 if (cfg->dig_outs || cfg->dig_in_pin) {
4886                         spec->multiout.max_channels = 2;
4887                         spec->no_analog = 1;
4888                         goto dig_only;
4889                 }
4890                 if (!cfg->num_inputs && !cfg->dig_in_pin)
4891                         return 0; /* can't find valid BIOS pin config */
4892         }
4893
4894         if (!spec->no_primary_hp &&
4895             cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
4896             cfg->line_outs <= cfg->hp_outs) {
4897                 /* use HP as primary out */
4898                 cfg->speaker_outs = cfg->line_outs;
4899                 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4900                        sizeof(cfg->speaker_pins));
4901                 cfg->line_outs = cfg->hp_outs;
4902                 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4903                 cfg->hp_outs = 0;
4904                 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4905                 cfg->line_out_type = AUTO_PIN_HP_OUT;
4906         }
4907
4908         err = parse_output_paths(codec);
4909         if (err < 0)
4910                 return err;
4911         err = create_multi_channel_mode(codec);
4912         if (err < 0)
4913                 return err;
4914         err = create_multi_out_ctls(codec, cfg);
4915         if (err < 0)
4916                 return err;
4917         err = create_hp_out_ctls(codec);
4918         if (err < 0)
4919                 return err;
4920         err = create_speaker_out_ctls(codec);
4921         if (err < 0)
4922                 return err;
4923         err = create_indep_hp_ctls(codec);
4924         if (err < 0)
4925                 return err;
4926         err = create_loopback_mixing_ctl(codec);
4927         if (err < 0)
4928                 return err;
4929         err = create_hp_mic(codec);
4930         if (err < 0)
4931                 return err;
4932         err = create_input_ctls(codec);
4933         if (err < 0)
4934                 return err;
4935
4936         /* add power-down pin callbacks at first */
4937         add_all_pin_power_ctls(codec, false);
4938
4939         spec->const_channel_count = spec->ext_channel_count;
4940         /* check the multiple speaker and headphone pins */
4941         if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4942                 spec->const_channel_count = max(spec->const_channel_count,
4943                                                 cfg->speaker_outs * 2);
4944         if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4945                 spec->const_channel_count = max(spec->const_channel_count,
4946                                                 cfg->hp_outs * 2);
4947         spec->multiout.max_channels = max(spec->ext_channel_count,
4948                                           spec->const_channel_count);
4949
4950         err = check_auto_mute_availability(codec);
4951         if (err < 0)
4952                 return err;
4953
4954         err = check_dyn_adc_switch(codec);
4955         if (err < 0)
4956                 return err;
4957
4958         err = check_auto_mic_availability(codec);
4959         if (err < 0)
4960                 return err;
4961
4962         /* add stereo mix if available and not enabled yet */
4963         if (!spec->auto_mic && spec->mixer_nid &&
4964             spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_AUTO &&
4965             spec->input_mux.num_items > 1) {
4966                 err = parse_capture_source(codec, spec->mixer_nid,
4967                                            CFG_IDX_MIX, spec->num_all_adcs,
4968                                            "Stereo Mix", 0);
4969                 if (err < 0)
4970                         return err;
4971         }
4972
4973
4974         err = create_capture_mixers(codec);
4975         if (err < 0)
4976                 return err;
4977
4978         err = parse_mic_boost(codec);
4979         if (err < 0)
4980                 return err;
4981
4982         /* create "Headphone Mic Jack Mode" if no input selection is
4983          * available (or user specifies add_jack_modes hint)
4984          */
4985         if (spec->hp_mic_pin &&
4986             (spec->auto_mic || spec->input_mux.num_items == 1 ||
4987              spec->add_jack_modes)) {
4988                 err = create_hp_mic_jack_mode(codec, spec->hp_mic_pin);
4989                 if (err < 0)
4990                         return err;
4991         }
4992
4993         if (spec->add_jack_modes) {
4994                 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4995                         err = create_out_jack_modes(codec, cfg->line_outs,
4996                                                     cfg->line_out_pins);
4997                         if (err < 0)
4998                                 return err;
4999                 }
5000                 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
5001                         err = create_out_jack_modes(codec, cfg->hp_outs,
5002                                                     cfg->hp_pins);
5003                         if (err < 0)
5004                                 return err;
5005                 }
5006         }
5007
5008         /* add power-up pin callbacks at last */
5009         add_all_pin_power_ctls(codec, true);
5010
5011         /* mute all aamix input initially */
5012         if (spec->mixer_nid)
5013                 mute_all_mixer_nid(codec, spec->mixer_nid);
5014
5015  dig_only:
5016         parse_digital(codec);
5017
5018         if (spec->power_down_unused || codec->power_save_node) {
5019                 if (!codec->power_filter)
5020                         codec->power_filter = snd_hda_gen_path_power_filter;
5021                 if (!codec->patch_ops.stream_pm)
5022                         codec->patch_ops.stream_pm = snd_hda_gen_stream_pm;
5023         }
5024
5025         if (!spec->no_analog && spec->beep_nid) {
5026                 err = snd_hda_attach_beep_device(codec, spec->beep_nid);
5027                 if (err < 0)
5028                         return err;
5029                 if (codec->beep && codec->power_save_node) {
5030                         err = add_fake_beep_paths(codec);
5031                         if (err < 0)
5032                                 return err;
5033                         codec->beep->power_hook = beep_power_hook;
5034                 }
5035         }
5036
5037         return 1;
5038 }
5039 EXPORT_SYMBOL_GPL(snd_hda_gen_parse_auto_config);
5040
5041
5042 /*
5043  * Build control elements
5044  */
5045
5046 /* slave controls for virtual master */
5047 static const char * const slave_pfxs[] = {
5048         "Front", "Surround", "Center", "LFE", "Side",
5049         "Headphone", "Speaker", "Mono", "Line Out",
5050         "CLFE", "Bass Speaker", "PCM",
5051         "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
5052         "Headphone Front", "Headphone Surround", "Headphone CLFE",
5053         "Headphone Side", "Headphone+LO", "Speaker+LO",
5054         NULL,
5055 };
5056
5057 /**
5058  * snd_hda_gen_build_controls - Build controls from the parsed results
5059  * @codec: the HDA codec
5060  *
5061  * Pass this to build_controls patch_ops.
5062  */
5063 int snd_hda_gen_build_controls(struct hda_codec *codec)
5064 {
5065         struct hda_gen_spec *spec = codec->spec;
5066         int err;
5067
5068         if (spec->kctls.used) {
5069                 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
5070                 if (err < 0)
5071                         return err;
5072         }
5073
5074         if (spec->multiout.dig_out_nid) {
5075                 err = snd_hda_create_dig_out_ctls(codec,
5076                                                   spec->multiout.dig_out_nid,
5077                                                   spec->multiout.dig_out_nid,
5078                                                   spec->pcm_rec[1]->pcm_type);
5079                 if (err < 0)
5080                         return err;
5081                 if (!spec->no_analog) {
5082                         err = snd_hda_create_spdif_share_sw(codec,
5083                                                             &spec->multiout);
5084                         if (err < 0)
5085                                 return err;
5086                         spec->multiout.share_spdif = 1;
5087                 }
5088         }
5089         if (spec->dig_in_nid) {
5090                 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
5091                 if (err < 0)
5092                         return err;
5093         }
5094
5095         /* if we have no master control, let's create it */
5096         if (!spec->no_analog && !spec->suppress_vmaster &&
5097             !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
5098                 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
5099                                           spec->vmaster_tlv, slave_pfxs,
5100                                           "Playback Volume");
5101                 if (err < 0)
5102                         return err;
5103         }
5104         if (!spec->no_analog && !spec->suppress_vmaster &&
5105             !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
5106                 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
5107                                             NULL, slave_pfxs,
5108                                             "Playback Switch",
5109                                             true, &spec->vmaster_mute.sw_kctl);
5110                 if (err < 0)
5111                         return err;
5112                 if (spec->vmaster_mute.hook) {
5113                         snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
5114                                                  spec->vmaster_mute_enum);
5115                         snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5116                 }
5117         }
5118
5119         free_kctls(spec); /* no longer needed */
5120
5121         err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
5122         if (err < 0)
5123                 return err;
5124
5125         return 0;
5126 }
5127 EXPORT_SYMBOL_GPL(snd_hda_gen_build_controls);
5128
5129
5130 /*
5131  * PCM definitions
5132  */
5133
5134 static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
5135                                    struct hda_codec *codec,
5136                                    struct snd_pcm_substream *substream,
5137                                    int action)
5138 {
5139         struct hda_gen_spec *spec = codec->spec;
5140         if (spec->pcm_playback_hook)
5141                 spec->pcm_playback_hook(hinfo, codec, substream, action);
5142 }
5143
5144 static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
5145                                   struct hda_codec *codec,
5146                                   struct snd_pcm_substream *substream,
5147                                   int action)
5148 {
5149         struct hda_gen_spec *spec = codec->spec;
5150         if (spec->pcm_capture_hook)
5151                 spec->pcm_capture_hook(hinfo, codec, substream, action);
5152 }
5153
5154 /*
5155  * Analog playback callbacks
5156  */
5157 static int playback_pcm_open(struct hda_pcm_stream *hinfo,
5158                              struct hda_codec *codec,
5159                              struct snd_pcm_substream *substream)
5160 {
5161         struct hda_gen_spec *spec = codec->spec;
5162         int err;
5163
5164         mutex_lock(&spec->pcm_mutex);
5165         err = snd_hda_multi_out_analog_open(codec,
5166                                             &spec->multiout, substream,
5167                                              hinfo);
5168         if (!err) {
5169                 spec->active_streams |= 1 << STREAM_MULTI_OUT;
5170                 call_pcm_playback_hook(hinfo, codec, substream,
5171                                        HDA_GEN_PCM_ACT_OPEN);
5172         }
5173         mutex_unlock(&spec->pcm_mutex);
5174         return err;
5175 }
5176
5177 static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5178                                 struct hda_codec *codec,
5179                                 unsigned int stream_tag,
5180                                 unsigned int format,
5181                                 struct snd_pcm_substream *substream)
5182 {
5183         struct hda_gen_spec *spec = codec->spec;
5184         int err;
5185
5186         err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
5187                                                stream_tag, format, substream);
5188         if (!err)
5189                 call_pcm_playback_hook(hinfo, codec, substream,
5190                                        HDA_GEN_PCM_ACT_PREPARE);
5191         return err;
5192 }
5193
5194 static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5195                                 struct hda_codec *codec,
5196                                 struct snd_pcm_substream *substream)
5197 {
5198         struct hda_gen_spec *spec = codec->spec;
5199         int err;
5200
5201         err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
5202         if (!err)
5203                 call_pcm_playback_hook(hinfo, codec, substream,
5204                                        HDA_GEN_PCM_ACT_CLEANUP);
5205         return err;
5206 }
5207
5208 static int playback_pcm_close(struct hda_pcm_stream *hinfo,
5209                               struct hda_codec *codec,
5210                               struct snd_pcm_substream *substream)
5211 {
5212         struct hda_gen_spec *spec = codec->spec;
5213         mutex_lock(&spec->pcm_mutex);
5214         spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
5215         call_pcm_playback_hook(hinfo, codec, substream,
5216                                HDA_GEN_PCM_ACT_CLOSE);
5217         mutex_unlock(&spec->pcm_mutex);
5218         return 0;
5219 }
5220
5221 static int capture_pcm_open(struct hda_pcm_stream *hinfo,
5222                             struct hda_codec *codec,
5223                             struct snd_pcm_substream *substream)
5224 {
5225         call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
5226         return 0;
5227 }
5228
5229 static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5230                                struct hda_codec *codec,
5231                                unsigned int stream_tag,
5232                                unsigned int format,
5233                                struct snd_pcm_substream *substream)
5234 {
5235         snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5236         call_pcm_capture_hook(hinfo, codec, substream,
5237                               HDA_GEN_PCM_ACT_PREPARE);
5238         return 0;
5239 }
5240
5241 static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5242                                struct hda_codec *codec,
5243                                struct snd_pcm_substream *substream)
5244 {
5245         snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5246         call_pcm_capture_hook(hinfo, codec, substream,
5247                               HDA_GEN_PCM_ACT_CLEANUP);
5248         return 0;
5249 }
5250
5251 static int capture_pcm_close(struct hda_pcm_stream *hinfo,
5252                              struct hda_codec *codec,
5253                              struct snd_pcm_substream *substream)
5254 {
5255         call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
5256         return 0;
5257 }
5258
5259 static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
5260                                  struct hda_codec *codec,
5261                                  struct snd_pcm_substream *substream)
5262 {
5263         struct hda_gen_spec *spec = codec->spec;
5264         int err = 0;
5265
5266         mutex_lock(&spec->pcm_mutex);
5267         if (spec->indep_hp && !spec->indep_hp_enabled)
5268                 err = -EBUSY;
5269         else
5270                 spec->active_streams |= 1 << STREAM_INDEP_HP;
5271         call_pcm_playback_hook(hinfo, codec, substream,
5272                                HDA_GEN_PCM_ACT_OPEN);
5273         mutex_unlock(&spec->pcm_mutex);
5274         return err;
5275 }
5276
5277 static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
5278                                   struct hda_codec *codec,
5279                                   struct snd_pcm_substream *substream)
5280 {
5281         struct hda_gen_spec *spec = codec->spec;
5282         mutex_lock(&spec->pcm_mutex);
5283         spec->active_streams &= ~(1 << STREAM_INDEP_HP);
5284         call_pcm_playback_hook(hinfo, codec, substream,
5285                                HDA_GEN_PCM_ACT_CLOSE);
5286         mutex_unlock(&spec->pcm_mutex);
5287         return 0;
5288 }
5289
5290 static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5291                                     struct hda_codec *codec,
5292                                     unsigned int stream_tag,
5293                                     unsigned int format,
5294                                     struct snd_pcm_substream *substream)
5295 {
5296         snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5297         call_pcm_playback_hook(hinfo, codec, substream,
5298                                HDA_GEN_PCM_ACT_PREPARE);
5299         return 0;
5300 }
5301
5302 static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5303                                     struct hda_codec *codec,
5304                                     struct snd_pcm_substream *substream)
5305 {
5306         snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5307         call_pcm_playback_hook(hinfo, codec, substream,
5308                                HDA_GEN_PCM_ACT_CLEANUP);
5309         return 0;
5310 }
5311
5312 /*
5313  * Digital out
5314  */
5315 static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
5316                                  struct hda_codec *codec,
5317                                  struct snd_pcm_substream *substream)
5318 {
5319         struct hda_gen_spec *spec = codec->spec;
5320         return snd_hda_multi_out_dig_open(codec, &spec->multiout);
5321 }
5322
5323 static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5324                                     struct hda_codec *codec,
5325                                     unsigned int stream_tag,
5326                                     unsigned int format,
5327                                     struct snd_pcm_substream *substream)
5328 {
5329         struct hda_gen_spec *spec = codec->spec;
5330         return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
5331                                              stream_tag, format, substream);
5332 }
5333
5334 static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5335                                     struct hda_codec *codec,
5336                                     struct snd_pcm_substream *substream)
5337 {
5338         struct hda_gen_spec *spec = codec->spec;
5339         return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
5340 }
5341
5342 static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
5343                                   struct hda_codec *codec,
5344                                   struct snd_pcm_substream *substream)
5345 {
5346         struct hda_gen_spec *spec = codec->spec;
5347         return snd_hda_multi_out_dig_close(codec, &spec->multiout);
5348 }
5349
5350 /*
5351  * Analog capture
5352  */
5353 #define alt_capture_pcm_open    capture_pcm_open
5354 #define alt_capture_pcm_close   capture_pcm_close
5355
5356 static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5357                                    struct hda_codec *codec,
5358                                    unsigned int stream_tag,
5359                                    unsigned int format,
5360                                    struct snd_pcm_substream *substream)
5361 {
5362         struct hda_gen_spec *spec = codec->spec;
5363
5364         snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
5365                                    stream_tag, 0, format);
5366         call_pcm_capture_hook(hinfo, codec, substream,
5367                               HDA_GEN_PCM_ACT_PREPARE);
5368         return 0;
5369 }
5370
5371 static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5372                                    struct hda_codec *codec,
5373                                    struct snd_pcm_substream *substream)
5374 {
5375         struct hda_gen_spec *spec = codec->spec;
5376
5377         snd_hda_codec_cleanup_stream(codec,
5378                                      spec->adc_nids[substream->number + 1]);
5379         call_pcm_capture_hook(hinfo, codec, substream,
5380                               HDA_GEN_PCM_ACT_CLEANUP);
5381         return 0;
5382 }
5383
5384 /*
5385  */
5386 static const struct hda_pcm_stream pcm_analog_playback = {
5387         .substreams = 1,
5388         .channels_min = 2,
5389         .channels_max = 8,
5390         /* NID is set in build_pcms */
5391         .ops = {
5392                 .open = playback_pcm_open,
5393                 .close = playback_pcm_close,
5394                 .prepare = playback_pcm_prepare,
5395                 .cleanup = playback_pcm_cleanup
5396         },
5397 };
5398
5399 static const struct hda_pcm_stream pcm_analog_capture = {
5400         .substreams = 1,
5401         .channels_min = 2,
5402         .channels_max = 2,
5403         /* NID is set in build_pcms */
5404         .ops = {
5405                 .open = capture_pcm_open,
5406                 .close = capture_pcm_close,
5407                 .prepare = capture_pcm_prepare,
5408                 .cleanup = capture_pcm_cleanup
5409         },
5410 };
5411
5412 static const struct hda_pcm_stream pcm_analog_alt_playback = {
5413         .substreams = 1,
5414         .channels_min = 2,
5415         .channels_max = 2,
5416         /* NID is set in build_pcms */
5417         .ops = {
5418                 .open = alt_playback_pcm_open,
5419                 .close = alt_playback_pcm_close,
5420                 .prepare = alt_playback_pcm_prepare,
5421                 .cleanup = alt_playback_pcm_cleanup
5422         },
5423 };
5424
5425 static const struct hda_pcm_stream pcm_analog_alt_capture = {
5426         .substreams = 2, /* can be overridden */
5427         .channels_min = 2,
5428         .channels_max = 2,
5429         /* NID is set in build_pcms */
5430         .ops = {
5431                 .open = alt_capture_pcm_open,
5432                 .close = alt_capture_pcm_close,
5433                 .prepare = alt_capture_pcm_prepare,
5434                 .cleanup = alt_capture_pcm_cleanup
5435         },
5436 };
5437
5438 static const struct hda_pcm_stream pcm_digital_playback = {
5439         .substreams = 1,
5440         .channels_min = 2,
5441         .channels_max = 2,
5442         /* NID is set in build_pcms */
5443         .ops = {
5444                 .open = dig_playback_pcm_open,
5445                 .close = dig_playback_pcm_close,
5446                 .prepare = dig_playback_pcm_prepare,
5447                 .cleanup = dig_playback_pcm_cleanup
5448         },
5449 };
5450
5451 static const struct hda_pcm_stream pcm_digital_capture = {
5452         .substreams = 1,
5453         .channels_min = 2,
5454         .channels_max = 2,
5455         /* NID is set in build_pcms */
5456 };
5457
5458 /* Used by build_pcms to flag that a PCM has no playback stream */
5459 static const struct hda_pcm_stream pcm_null_stream = {
5460         .substreams = 0,
5461         .channels_min = 0,
5462         .channels_max = 0,
5463 };
5464
5465 /*
5466  * dynamic changing ADC PCM streams
5467  */
5468 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
5469 {
5470         struct hda_gen_spec *spec = codec->spec;
5471         hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
5472
5473         if (spec->cur_adc && spec->cur_adc != new_adc) {
5474                 /* stream is running, let's swap the current ADC */
5475                 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
5476                 spec->cur_adc = new_adc;
5477                 snd_hda_codec_setup_stream(codec, new_adc,
5478                                            spec->cur_adc_stream_tag, 0,
5479                                            spec->cur_adc_format);
5480                 return true;
5481         }
5482         return false;
5483 }
5484
5485 /* analog capture with dynamic dual-adc changes */
5486 static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5487                                        struct hda_codec *codec,
5488                                        unsigned int stream_tag,
5489                                        unsigned int format,
5490                                        struct snd_pcm_substream *substream)
5491 {
5492         struct hda_gen_spec *spec = codec->spec;
5493         spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
5494         spec->cur_adc_stream_tag = stream_tag;
5495         spec->cur_adc_format = format;
5496         snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
5497         call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_PREPARE);
5498         return 0;
5499 }
5500
5501 static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5502                                        struct hda_codec *codec,
5503                                        struct snd_pcm_substream *substream)
5504 {
5505         struct hda_gen_spec *spec = codec->spec;
5506         snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
5507         spec->cur_adc = 0;
5508         call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLEANUP);
5509         return 0;
5510 }
5511
5512 static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
5513         .substreams = 1,
5514         .channels_min = 2,
5515         .channels_max = 2,
5516         .nid = 0, /* fill later */
5517         .ops = {
5518                 .prepare = dyn_adc_capture_pcm_prepare,
5519                 .cleanup = dyn_adc_capture_pcm_cleanup
5520         },
5521 };
5522
5523 static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
5524                                  const char *chip_name)
5525 {
5526         char *p;
5527
5528         if (*str)
5529                 return;
5530         strlcpy(str, chip_name, len);
5531
5532         /* drop non-alnum chars after a space */
5533         for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
5534                 if (!isalnum(p[1])) {
5535                         *p = 0;
5536                         break;
5537                 }
5538         }
5539         strlcat(str, sfx, len);
5540 }
5541
5542 /* copy PCM stream info from @default_str, and override non-NULL entries
5543  * from @spec_str and @nid
5544  */
5545 static void setup_pcm_stream(struct hda_pcm_stream *str,
5546                              const struct hda_pcm_stream *default_str,
5547                              const struct hda_pcm_stream *spec_str,
5548                              hda_nid_t nid)
5549 {
5550         *str = *default_str;
5551         if (nid)
5552                 str->nid = nid;
5553         if (spec_str) {
5554                 if (spec_str->substreams)
5555                         str->substreams = spec_str->substreams;
5556                 if (spec_str->channels_min)
5557                         str->channels_min = spec_str->channels_min;
5558                 if (spec_str->channels_max)
5559                         str->channels_max = spec_str->channels_max;
5560                 if (spec_str->rates)
5561                         str->rates = spec_str->rates;
5562                 if (spec_str->formats)
5563                         str->formats = spec_str->formats;
5564                 if (spec_str->maxbps)
5565                         str->maxbps = spec_str->maxbps;
5566         }
5567 }
5568
5569 /**
5570  * snd_hda_gen_build_pcms - build PCM streams based on the parsed results
5571  * @codec: the HDA codec
5572  *
5573  * Pass this to build_pcms patch_ops.
5574  */
5575 int snd_hda_gen_build_pcms(struct hda_codec *codec)
5576 {
5577         struct hda_gen_spec *spec = codec->spec;
5578         struct hda_pcm *info;
5579         bool have_multi_adcs;
5580
5581         if (spec->no_analog)
5582                 goto skip_analog;
5583
5584         fill_pcm_stream_name(spec->stream_name_analog,
5585                              sizeof(spec->stream_name_analog),
5586                              " Analog", codec->core.chip_name);
5587         info = snd_hda_codec_pcm_new(codec, "%s", spec->stream_name_analog);
5588         if (!info)
5589                 return -ENOMEM;
5590         spec->pcm_rec[0] = info;
5591
5592         if (spec->multiout.num_dacs > 0) {
5593                 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5594                                  &pcm_analog_playback,
5595                                  spec->stream_analog_playback,
5596                                  spec->multiout.dac_nids[0]);
5597                 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
5598                         spec->multiout.max_channels;
5599                 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
5600                     spec->autocfg.line_outs == 2)
5601                         info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
5602                                 snd_pcm_2_1_chmaps;
5603         }
5604         if (spec->num_adc_nids) {
5605                 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5606                                  (spec->dyn_adc_switch ?
5607                                   &dyn_adc_pcm_analog_capture : &pcm_analog_capture),
5608                                  spec->stream_analog_capture,
5609                                  spec->adc_nids[0]);
5610         }
5611
5612  skip_analog:
5613         /* SPDIF for stream index #1 */
5614         if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
5615                 fill_pcm_stream_name(spec->stream_name_digital,
5616                                      sizeof(spec->stream_name_digital),
5617                                      " Digital", codec->core.chip_name);
5618                 info = snd_hda_codec_pcm_new(codec, "%s",
5619                                              spec->stream_name_digital);
5620                 if (!info)
5621                         return -ENOMEM;
5622                 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
5623                 spec->pcm_rec[1] = info;
5624                 if (spec->dig_out_type)
5625                         info->pcm_type = spec->dig_out_type;
5626                 else
5627                         info->pcm_type = HDA_PCM_TYPE_SPDIF;
5628                 if (spec->multiout.dig_out_nid)
5629                         setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5630                                          &pcm_digital_playback,
5631                                          spec->stream_digital_playback,
5632                                          spec->multiout.dig_out_nid);
5633                 if (spec->dig_in_nid)
5634                         setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5635                                          &pcm_digital_capture,
5636                                          spec->stream_digital_capture,
5637                                          spec->dig_in_nid);
5638         }
5639
5640         if (spec->no_analog)
5641                 return 0;
5642
5643         /* If the use of more than one ADC is requested for the current
5644          * model, configure a second analog capture-only PCM.
5645          */
5646         have_multi_adcs = (spec->num_adc_nids > 1) &&
5647                 !spec->dyn_adc_switch && !spec->auto_mic;
5648         /* Additional Analaog capture for index #2 */
5649         if (spec->alt_dac_nid || have_multi_adcs) {
5650                 fill_pcm_stream_name(spec->stream_name_alt_analog,
5651                                      sizeof(spec->stream_name_alt_analog),
5652                              " Alt Analog", codec->core.chip_name);
5653                 info = snd_hda_codec_pcm_new(codec, "%s",
5654                                              spec->stream_name_alt_analog);
5655                 if (!info)
5656                         return -ENOMEM;
5657                 spec->pcm_rec[2] = info;
5658                 if (spec->alt_dac_nid)
5659                         setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5660                                          &pcm_analog_alt_playback,
5661                                          spec->stream_analog_alt_playback,
5662                                          spec->alt_dac_nid);
5663                 else
5664                         setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5665                                          &pcm_null_stream, NULL, 0);
5666                 if (have_multi_adcs) {
5667                         setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5668                                          &pcm_analog_alt_capture,
5669                                          spec->stream_analog_alt_capture,
5670                                          spec->adc_nids[1]);
5671                         info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
5672                                 spec->num_adc_nids - 1;
5673                 } else {
5674                         setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5675                                          &pcm_null_stream, NULL, 0);
5676                 }
5677         }
5678
5679         return 0;
5680 }
5681 EXPORT_SYMBOL_GPL(snd_hda_gen_build_pcms);
5682
5683
5684 /*
5685  * Standard auto-parser initializations
5686  */
5687
5688 /* configure the given path as a proper output */
5689 static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
5690 {
5691         struct nid_path *path;
5692         hda_nid_t pin;
5693
5694         path = snd_hda_get_path_from_idx(codec, path_idx);
5695         if (!path || !path->depth)
5696                 return;
5697         pin = path->path[path->depth - 1];
5698         restore_pin_ctl(codec, pin);
5699         snd_hda_activate_path(codec, path, path->active,
5700                               aamix_default(codec->spec));
5701         set_pin_eapd(codec, pin, path->active);
5702 }
5703
5704 /* initialize primary output paths */
5705 static void init_multi_out(struct hda_codec *codec)
5706 {
5707         struct hda_gen_spec *spec = codec->spec;
5708         int i;
5709
5710         for (i = 0; i < spec->autocfg.line_outs; i++)
5711                 set_output_and_unmute(codec, spec->out_paths[i]);
5712 }
5713
5714
5715 static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
5716 {
5717         int i;
5718
5719         for (i = 0; i < num_outs; i++)
5720                 set_output_and_unmute(codec, paths[i]);
5721 }
5722
5723 /* initialize hp and speaker paths */
5724 static void init_extra_out(struct hda_codec *codec)
5725 {
5726         struct hda_gen_spec *spec = codec->spec;
5727
5728         if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
5729                 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
5730         if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
5731                 __init_extra_out(codec, spec->autocfg.speaker_outs,
5732                                  spec->speaker_paths);
5733 }
5734
5735 /* initialize multi-io paths */
5736 static void init_multi_io(struct hda_codec *codec)
5737 {
5738         struct hda_gen_spec *spec = codec->spec;
5739         int i;
5740
5741         for (i = 0; i < spec->multi_ios; i++) {
5742                 hda_nid_t pin = spec->multi_io[i].pin;
5743                 struct nid_path *path;
5744                 path = get_multiio_path(codec, i);
5745                 if (!path)
5746                         continue;
5747                 if (!spec->multi_io[i].ctl_in)
5748                         spec->multi_io[i].ctl_in =
5749                                 snd_hda_codec_get_pin_target(codec, pin);
5750                 snd_hda_activate_path(codec, path, path->active,
5751                                       aamix_default(spec));
5752         }
5753 }
5754
5755 static void init_aamix_paths(struct hda_codec *codec)
5756 {
5757         struct hda_gen_spec *spec = codec->spec;
5758
5759         if (!spec->have_aamix_ctl)
5760                 return;
5761         if (!has_aamix_out_paths(spec))
5762                 return;
5763         update_aamix_paths(codec, spec->aamix_mode, spec->out_paths[0],
5764                            spec->aamix_out_paths[0],
5765                            spec->autocfg.line_out_type);
5766         update_aamix_paths(codec, spec->aamix_mode, spec->hp_paths[0],
5767                            spec->aamix_out_paths[1],
5768                            AUTO_PIN_HP_OUT);
5769         update_aamix_paths(codec, spec->aamix_mode, spec->speaker_paths[0],
5770                            spec->aamix_out_paths[2],
5771                            AUTO_PIN_SPEAKER_OUT);
5772 }
5773
5774 /* set up input pins and loopback paths */
5775 static void init_analog_input(struct hda_codec *codec)
5776 {
5777         struct hda_gen_spec *spec = codec->spec;
5778         struct auto_pin_cfg *cfg = &spec->autocfg;
5779         int i;
5780
5781         for (i = 0; i < cfg->num_inputs; i++) {
5782                 hda_nid_t nid = cfg->inputs[i].pin;
5783                 if (is_input_pin(codec, nid))
5784                         restore_pin_ctl(codec, nid);
5785
5786                 /* init loopback inputs */
5787                 if (spec->mixer_nid) {
5788                         resume_path_from_idx(codec, spec->loopback_paths[i]);
5789                         resume_path_from_idx(codec, spec->loopback_merge_path);
5790                 }
5791         }
5792 }
5793
5794 /* initialize ADC paths */
5795 static void init_input_src(struct hda_codec *codec)
5796 {
5797         struct hda_gen_spec *spec = codec->spec;
5798         struct hda_input_mux *imux = &spec->input_mux;
5799         struct nid_path *path;
5800         int i, c, nums;
5801
5802         if (spec->dyn_adc_switch)
5803                 nums = 1;
5804         else
5805                 nums = spec->num_adc_nids;
5806
5807         for (c = 0; c < nums; c++) {
5808                 for (i = 0; i < imux->num_items; i++) {
5809                         path = get_input_path(codec, c, i);
5810                         if (path) {
5811                                 bool active = path->active;
5812                                 if (i == spec->cur_mux[c])
5813                                         active = true;
5814                                 snd_hda_activate_path(codec, path, active, false);
5815                         }
5816                 }
5817                 if (spec->hp_mic)
5818                         update_hp_mic(codec, c, true);
5819         }
5820
5821         if (spec->cap_sync_hook)
5822                 spec->cap_sync_hook(codec, NULL, NULL);
5823 }
5824
5825 /* set right pin controls for digital I/O */
5826 static void init_digital(struct hda_codec *codec)
5827 {
5828         struct hda_gen_spec *spec = codec->spec;
5829         int i;
5830         hda_nid_t pin;
5831
5832         for (i = 0; i < spec->autocfg.dig_outs; i++)
5833                 set_output_and_unmute(codec, spec->digout_paths[i]);
5834         pin = spec->autocfg.dig_in_pin;
5835         if (pin) {
5836                 restore_pin_ctl(codec, pin);
5837                 resume_path_from_idx(codec, spec->digin_path);
5838         }
5839 }
5840
5841 /* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5842  * invalid unsol tags by some reason
5843  */
5844 static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5845 {
5846         int i;
5847
5848         for (i = 0; i < codec->init_pins.used; i++) {
5849                 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
5850                 hda_nid_t nid = pin->nid;
5851                 if (is_jack_detectable(codec, nid) &&
5852                     !snd_hda_jack_tbl_get(codec, nid))
5853                         snd_hda_codec_update_cache(codec, nid, 0,
5854                                         AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5855         }
5856 }
5857
5858 /**
5859  * snd_hda_gen_init - initialize the generic spec
5860  * @codec: the HDA codec
5861  *
5862  * This can be put as patch_ops init function.
5863  */
5864 int snd_hda_gen_init(struct hda_codec *codec)
5865 {
5866         struct hda_gen_spec *spec = codec->spec;
5867
5868         if (spec->init_hook)
5869                 spec->init_hook(codec);
5870
5871         if (!spec->skip_verbs)
5872                 snd_hda_apply_verbs(codec);
5873
5874         init_multi_out(codec);
5875         init_extra_out(codec);
5876         init_multi_io(codec);
5877         init_aamix_paths(codec);
5878         init_analog_input(codec);
5879         init_input_src(codec);
5880         init_digital(codec);
5881
5882         clear_unsol_on_unused_pins(codec);
5883
5884         sync_all_pin_power_ctls(codec);
5885
5886         /* call init functions of standard auto-mute helpers */
5887         update_automute_all(codec);
5888
5889         regcache_sync(codec->core.regmap);
5890
5891         if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
5892                 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5893
5894         hda_call_check_power_status(codec, 0x01);
5895         return 0;
5896 }
5897 EXPORT_SYMBOL_GPL(snd_hda_gen_init);
5898
5899 /**
5900  * snd_hda_gen_free - free the generic spec
5901  * @codec: the HDA codec
5902  *
5903  * This can be put as patch_ops free function.
5904  */
5905 void snd_hda_gen_free(struct hda_codec *codec)
5906 {
5907         snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_FREE);
5908         snd_hda_gen_spec_free(codec->spec);
5909         kfree(codec->spec);
5910         codec->spec = NULL;
5911 }
5912 EXPORT_SYMBOL_GPL(snd_hda_gen_free);
5913
5914 /**
5915  * snd_hda_gen_reboot_notify - Make codec enter D3 before rebooting
5916  * @codec: the HDA codec
5917  *
5918  * This can be put as patch_ops reboot_notify function.
5919  */
5920 void snd_hda_gen_reboot_notify(struct hda_codec *codec)
5921 {
5922         /* Make the codec enter D3 to avoid spurious noises from the internal
5923          * speaker during (and after) reboot
5924          */
5925         snd_hda_codec_set_power_to_all(codec, codec->core.afg, AC_PWRST_D3);
5926         snd_hda_codec_write(codec, codec->core.afg, 0,
5927                             AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
5928         msleep(10);
5929 }
5930 EXPORT_SYMBOL_GPL(snd_hda_gen_reboot_notify);
5931
5932 #ifdef CONFIG_PM
5933 /**
5934  * snd_hda_gen_check_power_status - check the loopback power save state
5935  * @codec: the HDA codec
5936  * @nid: NID to inspect
5937  *
5938  * This can be put as patch_ops check_power_status function.
5939  */
5940 int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
5941 {
5942         struct hda_gen_spec *spec = codec->spec;
5943         return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
5944 }
5945 EXPORT_SYMBOL_GPL(snd_hda_gen_check_power_status);
5946 #endif
5947
5948
5949 /*
5950  * the generic codec support
5951  */
5952
5953 static const struct hda_codec_ops generic_patch_ops = {
5954         .build_controls = snd_hda_gen_build_controls,
5955         .build_pcms = snd_hda_gen_build_pcms,
5956         .init = snd_hda_gen_init,
5957         .free = snd_hda_gen_free,
5958         .unsol_event = snd_hda_jack_unsol_event,
5959         .reboot_notify = snd_hda_gen_reboot_notify,
5960 #ifdef CONFIG_PM
5961         .check_power_status = snd_hda_gen_check_power_status,
5962 #endif
5963 };
5964
5965 /*
5966  * snd_hda_parse_generic_codec - Generic codec parser
5967  * @codec: the HDA codec
5968  */
5969 static int snd_hda_parse_generic_codec(struct hda_codec *codec)
5970 {
5971         struct hda_gen_spec *spec;
5972         int err;
5973
5974         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
5975         if (!spec)
5976                 return -ENOMEM;
5977         snd_hda_gen_spec_init(spec);
5978         codec->spec = spec;
5979
5980         err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
5981         if (err < 0)
5982                 goto error;
5983
5984         err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
5985         if (err < 0)
5986                 goto error;
5987
5988         codec->patch_ops = generic_patch_ops;
5989         return 0;
5990
5991 error:
5992         snd_hda_gen_free(codec);
5993         return err;
5994 }
5995
5996 static const struct hda_device_id snd_hda_id_generic[] = {
5997         HDA_CODEC_ENTRY(HDA_CODEC_ID_GENERIC, "Generic", snd_hda_parse_generic_codec),
5998         {} /* terminator */
5999 };
6000 MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_generic);
6001
6002 static struct hda_codec_driver generic_driver = {
6003         .id = snd_hda_id_generic,
6004 };
6005
6006 module_hda_codec_driver(generic_driver);
6007
6008 MODULE_LICENSE("GPL");
6009 MODULE_DESCRIPTION("Generic HD-audio codec parser");