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