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