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