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