source: GPL/trunk/alsa-kernel/pci/hda/hda_generic.c

Last change on this file was 777, checked in by David Azarewicz, 7 months ago

Merge from uniaud32-exp branch

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