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