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