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