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