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