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