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