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