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