blob: b341450653e2c7c9108c73f8b1416663e0b940ad [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>
Takashi Iwaif873e532012-12-20 16:58:39 +010027#include <linux/ctype.h>
28#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <sound/core.h>
Takashi Iwai352f7f92012-12-19 12:52:06 +010030#include <sound/jack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "hda_codec.h"
32#include "hda_local.h"
Takashi Iwai352f7f92012-12-19 12:52:06 +010033#include "hda_auto_parser.h"
34#include "hda_jack.h"
35#include "hda_generic.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Takashi Iwai352f7f92012-12-19 12:52:06 +010038/* initialize hda_gen_spec struct */
39int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
Takashi Iwai352f7f92012-12-19 12:52:06 +010041 snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
42 snd_array_init(&spec->bind_ctls, sizeof(struct hda_bind_ctls *), 8);
43 snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
44 return 0;
45}
46EXPORT_SYMBOL_HDA(snd_hda_gen_spec_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Takashi Iwai12c93df2012-12-19 14:38:33 +010048struct snd_kcontrol_new *
49snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
50 const struct snd_kcontrol_new *temp)
Takashi Iwai352f7f92012-12-19 12:52:06 +010051{
52 struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
53 if (!knew)
54 return NULL;
55 *knew = *temp;
56 if (name)
57 knew->name = kstrdup(name, GFP_KERNEL);
58 else if (knew->name)
59 knew->name = kstrdup(knew->name, GFP_KERNEL);
60 if (!knew->name)
61 return NULL;
62 return knew;
63}
Takashi Iwai12c93df2012-12-19 14:38:33 +010064EXPORT_SYMBOL_HDA(snd_hda_gen_add_kctl);
Takashi Iwai352f7f92012-12-19 12:52:06 +010065
66static void free_kctls(struct hda_gen_spec *spec)
67{
68 if (spec->kctls.list) {
69 struct snd_kcontrol_new *kctl = spec->kctls.list;
70 int i;
71 for (i = 0; i < spec->kctls.used; i++)
72 kfree(kctl[i].name);
73 }
74 snd_array_free(&spec->kctls);
75}
76
77static struct hda_bind_ctls *new_bind_ctl(struct hda_codec *codec,
78 unsigned int nums,
79 struct hda_ctl_ops *ops)
80{
81 struct hda_gen_spec *spec = codec->spec;
82 struct hda_bind_ctls **ctlp, *ctl;
83 ctlp = snd_array_new(&spec->bind_ctls);
84 if (!ctlp)
85 return NULL;
86 ctl = kzalloc(sizeof(*ctl) + sizeof(long) * (nums + 1), GFP_KERNEL);
87 *ctlp = ctl;
88 if (ctl)
89 ctl->ops = ops;
90 return ctl;
91}
92
93static void free_bind_ctls(struct hda_gen_spec *spec)
94{
95 if (spec->bind_ctls.list) {
96 struct hda_bind_ctls **ctl = spec->bind_ctls.list;
97 int i;
98 for (i = 0; i < spec->bind_ctls.used; i++)
99 kfree(ctl[i]);
100 }
101 snd_array_free(&spec->bind_ctls);
102}
103
104void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
105{
106 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 return;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100108 free_kctls(spec);
109 free_bind_ctls(spec);
110 snd_array_free(&spec->paths);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
Takashi Iwai352f7f92012-12-19 12:52:06 +0100112EXPORT_SYMBOL_HDA(snd_hda_gen_spec_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100115 * parsing paths
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Takashi Iwai352f7f92012-12-19 12:52:06 +0100118/* get the path between the given NIDs;
119 * passing 0 to either @pin or @dac behaves as a wildcard
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100121struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
122 hda_nid_t from_nid, hda_nid_t to_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100124 struct hda_gen_spec *spec = codec->spec;
125 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Takashi Iwai352f7f92012-12-19 12:52:06 +0100127 for (i = 0; i < spec->paths.used; i++) {
128 struct nid_path *path = snd_array_elem(&spec->paths, i);
129 if (path->depth <= 0)
130 continue;
131 if ((!from_nid || path->path[0] == from_nid) &&
132 (!to_nid || path->path[path->depth - 1] == to_nid))
133 return path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 }
135 return NULL;
136}
Takashi Iwai352f7f92012-12-19 12:52:06 +0100137EXPORT_SYMBOL_HDA(snd_hda_get_nid_path);
138
139/* check whether the given DAC is already found in any existing paths */
140static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
141{
142 struct hda_gen_spec *spec = codec->spec;
143 int i;
144
145 for (i = 0; i < spec->paths.used; i++) {
146 struct nid_path *path = snd_array_elem(&spec->paths, i);
147 if (path->path[0] == nid)
148 return true;
149 }
150 return false;
151}
152
153/* check whether the given two widgets can be connected */
154static bool is_reachable_path(struct hda_codec *codec,
155 hda_nid_t from_nid, hda_nid_t to_nid)
156{
157 if (!from_nid || !to_nid)
158 return false;
159 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
160}
161
162/* nid, dir and idx */
163#define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
164
165/* check whether the given ctl is already assigned in any path elements */
166static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
167{
168 struct hda_gen_spec *spec = codec->spec;
169 int i;
170
171 val &= AMP_VAL_COMPARE_MASK;
172 for (i = 0; i < spec->paths.used; i++) {
173 struct nid_path *path = snd_array_elem(&spec->paths, i);
174 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
175 return true;
176 }
177 return false;
178}
179
180/* check whether a control with the given (nid, dir, idx) was assigned */
181static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
182 int dir, int idx)
183{
184 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
185 return is_ctl_used(codec, val, NID_PATH_VOL_CTL) ||
186 is_ctl_used(codec, val, NID_PATH_MUTE_CTL);
187}
188
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100189static void print_nid_path(const char *pfx, struct nid_path *path)
190{
191 char buf[40];
192 int i;
193
194
195 buf[0] = 0;
196 for (i = 0; i < path->depth; i++) {
197 char tmp[4];
198 sprintf(tmp, ":%02x", path->path[i]);
199 strlcat(buf, tmp, sizeof(buf));
200 }
201 snd_printdd("%s path: depth=%d %s\n", pfx, path->depth, buf);
202}
203
Takashi Iwai352f7f92012-12-19 12:52:06 +0100204/* called recursively */
205static bool __parse_nid_path(struct hda_codec *codec,
206 hda_nid_t from_nid, hda_nid_t to_nid,
207 int with_aa_mix, struct nid_path *path, int depth)
208{
209 struct hda_gen_spec *spec = codec->spec;
210 hda_nid_t conn[16];
211 int i, nums;
212
213 if (to_nid == spec->mixer_nid) {
214 if (!with_aa_mix)
215 return false;
216 with_aa_mix = 2; /* mark aa-mix is included */
217 }
218
219 nums = snd_hda_get_connections(codec, to_nid, conn, ARRAY_SIZE(conn));
220 for (i = 0; i < nums; i++) {
221 if (conn[i] != from_nid) {
222 /* special case: when from_nid is 0,
223 * try to find an empty DAC
224 */
225 if (from_nid ||
226 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
227 is_dac_already_used(codec, conn[i]))
228 continue;
229 }
230 /* aa-mix is requested but not included? */
231 if (!(spec->mixer_nid && with_aa_mix == 1))
232 goto found;
233 }
234 if (depth >= MAX_NID_PATH_DEPTH)
235 return false;
236 for (i = 0; i < nums; i++) {
237 unsigned int type;
238 type = get_wcaps_type(get_wcaps(codec, conn[i]));
239 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
240 type == AC_WID_PIN)
241 continue;
242 if (__parse_nid_path(codec, from_nid, conn[i],
243 with_aa_mix, path, depth + 1))
244 goto found;
245 }
246 return false;
247
248 found:
249 path->path[path->depth] = conn[i];
250 path->idx[path->depth + 1] = i;
251 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
252 path->multi[path->depth + 1] = 1;
253 path->depth++;
254 return true;
255}
256
257/* parse the widget path from the given nid to the target nid;
258 * when @from_nid is 0, try to find an empty DAC;
259 * when @with_aa_mix is 0, paths with spec->mixer_nid are excluded.
260 * when @with_aa_mix is 1, paths without spec->mixer_nid are excluded.
261 * when @with_aa_mix is 2, no special handling about spec->mixer_nid.
262 */
263bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
264 hda_nid_t to_nid, int with_aa_mix,
265 struct nid_path *path)
266{
267 if (__parse_nid_path(codec, from_nid, to_nid, with_aa_mix, path, 1)) {
268 path->path[path->depth] = to_nid;
269 path->depth++;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100270 return true;
271 }
272 return false;
273}
274EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100277 * parse the path between the given NIDs and add to the path list.
278 * if no valid path is found, return NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100280struct nid_path *
281snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
282 hda_nid_t to_nid, int with_aa_mix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100284 struct hda_gen_spec *spec = codec->spec;
285 struct nid_path *path;
286
287 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
288 return NULL;
289
290 path = snd_array_new(&spec->paths);
291 if (!path)
292 return NULL;
293 memset(path, 0, sizeof(*path));
294 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, with_aa_mix, path))
295 return path;
296 /* push back */
297 spec->paths.used--;
298 return NULL;
299}
300EXPORT_SYMBOL_HDA(snd_hda_add_new_path);
301
302/* look for an empty DAC slot */
303static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
304 bool is_digital)
305{
306 struct hda_gen_spec *spec = codec->spec;
307 bool cap_digital;
308 int i;
309
310 for (i = 0; i < spec->num_all_dacs; i++) {
311 hda_nid_t nid = spec->all_dacs[i];
312 if (!nid || is_dac_already_used(codec, nid))
313 continue;
314 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
315 if (is_digital != cap_digital)
316 continue;
317 if (is_reachable_path(codec, nid, pin))
318 return nid;
319 }
320 return 0;
321}
322
323/* replace the channels in the composed amp value with the given number */
324static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
325{
326 val &= ~(0x3U << 16);
327 val |= chs << 16;
328 return val;
329}
330
331/* check whether the widget has the given amp capability for the direction */
332static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
333 int dir, unsigned int bits)
334{
335 if (!nid)
336 return false;
337 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
338 if (query_amp_caps(codec, nid, dir) & bits)
339 return true;
340 return false;
341}
342
343#define nid_has_mute(codec, nid, dir) \
344 check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
345#define nid_has_volume(codec, nid, dir) \
346 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
347
348/* look for a widget suitable for assigning a mute switch in the path */
349static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
350 struct nid_path *path)
351{
352 int i;
353
354 for (i = path->depth - 1; i >= 0; i--) {
355 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
356 return path->path[i];
357 if (i != path->depth - 1 && i != 0 &&
358 nid_has_mute(codec, path->path[i], HDA_INPUT))
359 return path->path[i];
360 }
361 return 0;
362}
363
364/* look for a widget suitable for assigning a volume ctl in the path */
365static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
366 struct nid_path *path)
367{
368 int i;
369
370 for (i = path->depth - 1; i >= 0; i--) {
371 if (nid_has_volume(codec, path->path[i], HDA_OUTPUT))
372 return path->path[i];
373 }
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200374 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375}
376
377/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100378 * path activation / deactivation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100380
381/* can have the amp-in capability? */
382static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100384 hda_nid_t nid = path->path[idx];
385 unsigned int caps = get_wcaps(codec, nid);
386 unsigned int type = get_wcaps_type(caps);
387
388 if (!(caps & AC_WCAP_IN_AMP))
389 return false;
390 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
391 return false;
392 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393}
394
Takashi Iwai352f7f92012-12-19 12:52:06 +0100395/* can have the amp-out capability? */
396static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100398 hda_nid_t nid = path->path[idx];
399 unsigned int caps = get_wcaps(codec, nid);
400 unsigned int type = get_wcaps_type(caps);
401
402 if (!(caps & AC_WCAP_OUT_AMP))
403 return false;
404 if (type == AC_WID_PIN && !idx) /* only for output pins */
405 return false;
406 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
Takashi Iwai352f7f92012-12-19 12:52:06 +0100409/* check whether the given (nid,dir,idx) is active */
410static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
411 unsigned int idx, unsigned int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100413 struct hda_gen_spec *spec = codec->spec;
414 int i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Takashi Iwai352f7f92012-12-19 12:52:06 +0100416 for (n = 0; n < spec->paths.used; n++) {
417 struct nid_path *path = snd_array_elem(&spec->paths, n);
418 if (!path->active)
419 continue;
420 for (i = 0; i < path->depth; i++) {
421 if (path->path[i] == nid) {
422 if (dir == HDA_OUTPUT || path->idx[i] == idx)
423 return true;
424 break;
425 }
426 }
427 }
428 return false;
429}
430
431/* get the default amp value for the target state */
432static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
433 int dir, bool enable)
434{
435 unsigned int caps;
436 unsigned int val = 0;
437
438 caps = query_amp_caps(codec, nid, dir);
439 if (caps & AC_AMPCAP_NUM_STEPS) {
440 /* set to 0dB */
441 if (enable)
442 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
443 }
444 if (caps & AC_AMPCAP_MUTE) {
445 if (!enable)
446 val |= HDA_AMP_MUTE;
447 }
448 return val;
449}
450
451/* initialize the amp value (only at the first time) */
452static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
453{
454 int val = get_amp_val_to_activate(codec, nid, dir, false);
455 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
456}
457
458static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
459 int idx, bool enable)
460{
461 int val;
462 if (is_ctl_associated(codec, nid, dir, idx) ||
463 is_active_nid(codec, nid, dir, idx))
464 return;
465 val = get_amp_val_to_activate(codec, nid, dir, enable);
466 snd_hda_codec_amp_stereo(codec, nid, dir, idx, 0xff, val);
467}
468
469static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
470 int i, bool enable)
471{
472 hda_nid_t nid = path->path[i];
473 init_amp(codec, nid, HDA_OUTPUT, 0);
474 activate_amp(codec, nid, HDA_OUTPUT, 0, enable);
475}
476
477static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
478 int i, bool enable, bool add_aamix)
479{
480 struct hda_gen_spec *spec = codec->spec;
481 hda_nid_t conn[16];
482 int n, nums, idx;
483 int type;
484 hda_nid_t nid = path->path[i];
485
486 nums = snd_hda_get_connections(codec, nid, conn, ARRAY_SIZE(conn));
487 type = get_wcaps_type(get_wcaps(codec, nid));
488 if (type == AC_WID_PIN ||
489 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
490 nums = 1;
491 idx = 0;
492 } else
493 idx = path->idx[i];
494
495 for (n = 0; n < nums; n++)
496 init_amp(codec, nid, HDA_INPUT, n);
497
498 if (is_ctl_associated(codec, nid, HDA_INPUT, idx))
499 return;
500
501 /* here is a little bit tricky in comparison with activate_amp_out();
502 * when aa-mixer is available, we need to enable the path as well
503 */
504 for (n = 0; n < nums; n++) {
505 if (n != idx && (!add_aamix || conn[n] != spec->mixer_nid))
506 continue;
507 activate_amp(codec, nid, HDA_INPUT, n, enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 }
509}
510
Takashi Iwai352f7f92012-12-19 12:52:06 +0100511/* activate or deactivate the given path
512 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100514void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
515 bool enable, bool add_aamix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100517 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Takashi Iwai352f7f92012-12-19 12:52:06 +0100519 if (!enable)
520 path->active = false;
521
522 for (i = path->depth - 1; i >= 0; i--) {
523 if (enable && path->multi[i])
524 snd_hda_codec_write_cache(codec, path->path[i], 0,
525 AC_VERB_SET_CONNECT_SEL,
526 path->idx[i]);
527 if (has_amp_in(codec, path, i))
528 activate_amp_in(codec, path, i, enable, add_aamix);
529 if (has_amp_out(codec, path, i))
530 activate_amp_out(codec, path, i, enable);
531 }
532
533 if (enable)
534 path->active = true;
535}
536EXPORT_SYMBOL_HDA(snd_hda_activate_path);
537
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100538/* turn on/off EAPD on the given pin */
539static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
540{
541 struct hda_gen_spec *spec = codec->spec;
542 if (spec->own_eapd_ctl ||
543 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
544 return;
545 snd_hda_codec_update_cache(codec, pin, 0,
546 AC_VERB_SET_EAPD_BTLENABLE,
547 enable ? 0x02 : 0x00);
548}
549
Takashi Iwai352f7f92012-12-19 12:52:06 +0100550
551/*
552 * Helper functions for creating mixer ctl elements
553 */
554
555enum {
556 HDA_CTL_WIDGET_VOL,
557 HDA_CTL_WIDGET_MUTE,
558 HDA_CTL_BIND_MUTE,
559 HDA_CTL_BIND_VOL,
560 HDA_CTL_BIND_SW,
561};
562static const struct snd_kcontrol_new control_templates[] = {
563 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
564 HDA_CODEC_MUTE(NULL, 0, 0, 0),
565 HDA_BIND_MUTE(NULL, 0, 0, 0),
566 HDA_BIND_VOL(NULL, 0),
567 HDA_BIND_SW(NULL, 0),
568};
569
570/* add dynamic controls from template */
571static int add_control(struct hda_gen_spec *spec, int type, const char *name,
572 int cidx, unsigned long val)
573{
574 struct snd_kcontrol_new *knew;
575
Takashi Iwai12c93df2012-12-19 14:38:33 +0100576 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100577 if (!knew)
578 return -ENOMEM;
579 knew->index = cidx;
580 if (get_amp_nid_(val))
581 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
582 knew->private_value = val;
583 return 0;
584}
585
586static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
587 const char *pfx, const char *dir,
588 const char *sfx, int cidx, unsigned long val)
589{
590 char name[32];
591 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
592 return add_control(spec, type, name, cidx, val);
593}
594
595#define add_pb_vol_ctrl(spec, type, pfx, val) \
596 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
597#define add_pb_sw_ctrl(spec, type, pfx, val) \
598 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
599#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
600 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
601#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
602 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
603
604static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
605 unsigned int chs, struct nid_path *path)
606{
607 unsigned int val;
608 if (!path)
609 return 0;
610 val = path->ctls[NID_PATH_VOL_CTL];
611 if (!val)
612 return 0;
613 val = amp_val_replace_channels(val, chs);
614 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
615}
616
617/* return the channel bits suitable for the given path->ctls[] */
618static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
619 int type)
620{
621 int chs = 1; /* mono (left only) */
622 if (path) {
623 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
624 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
625 chs = 3; /* stereo */
626 }
627 return chs;
628}
629
630static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
631 struct nid_path *path)
632{
633 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
634 return add_vol_ctl(codec, pfx, cidx, chs, path);
635}
636
637/* create a mute-switch for the given mixer widget;
638 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
639 */
640static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
641 unsigned int chs, struct nid_path *path)
642{
643 unsigned int val;
644 int type = HDA_CTL_WIDGET_MUTE;
645
646 if (!path)
647 return 0;
648 val = path->ctls[NID_PATH_MUTE_CTL];
649 if (!val)
650 return 0;
651 val = amp_val_replace_channels(val, chs);
652 if (get_amp_direction_(val) == HDA_INPUT) {
653 hda_nid_t nid = get_amp_nid_(val);
654 int nums = snd_hda_get_num_conns(codec, nid);
655 if (nums > 1) {
656 type = HDA_CTL_BIND_MUTE;
657 val |= nums << 19;
658 }
659 }
660 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
661}
662
663static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
664 int cidx, struct nid_path *path)
665{
666 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
667 return add_sw_ctl(codec, pfx, cidx, chs, path);
668}
669
670static const char * const channel_name[4] = {
671 "Front", "Surround", "CLFE", "Side"
672};
673
674/* give some appropriate ctl name prefix for the given line out channel */
675static const char *get_line_out_pfx(struct hda_gen_spec *spec, int ch,
676 bool can_be_master, int *index)
677{
678 struct auto_pin_cfg *cfg = &spec->autocfg;
679
680 *index = 0;
681 if (cfg->line_outs == 1 && !spec->multi_ios &&
682 !cfg->hp_outs && !cfg->speaker_outs && can_be_master)
683 return spec->vmaster_mute.hook ? "PCM" : "Master";
684
685 /* if there is really a single DAC used in the whole output paths,
686 * use it master (or "PCM" if a vmaster hook is present)
687 */
688 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
689 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
690 return spec->vmaster_mute.hook ? "PCM" : "Master";
691
692 switch (cfg->line_out_type) {
693 case AUTO_PIN_SPEAKER_OUT:
694 if (cfg->line_outs == 1)
695 return "Speaker";
696 if (cfg->line_outs == 2)
697 return ch ? "Bass Speaker" : "Speaker";
698 break;
699 case AUTO_PIN_HP_OUT:
700 /* for multi-io case, only the primary out */
701 if (ch && spec->multi_ios)
702 break;
703 *index = ch;
704 return "Headphone";
705 default:
706 if (cfg->line_outs == 1 && !spec->multi_ios)
707 return "PCM";
708 break;
709 }
710 if (ch >= ARRAY_SIZE(channel_name)) {
711 snd_BUG();
712 return "PCM";
713 }
714
715 return channel_name[ch];
716}
717
718/*
719 * Parse output paths
720 */
721
722/* badness definition */
723enum {
724 /* No primary DAC is found for the main output */
725 BAD_NO_PRIMARY_DAC = 0x10000,
726 /* No DAC is found for the extra output */
727 BAD_NO_DAC = 0x4000,
728 /* No possible multi-ios */
729 BAD_MULTI_IO = 0x103,
730 /* No individual DAC for extra output */
731 BAD_NO_EXTRA_DAC = 0x102,
732 /* No individual DAC for extra surrounds */
733 BAD_NO_EXTRA_SURR_DAC = 0x101,
734 /* Primary DAC shared with main surrounds */
735 BAD_SHARED_SURROUND = 0x100,
736 /* Primary DAC shared with main CLFE */
737 BAD_SHARED_CLFE = 0x10,
738 /* Primary DAC shared with extra surrounds */
739 BAD_SHARED_EXTRA_SURROUND = 0x10,
740 /* Volume widget is shared */
741 BAD_SHARED_VOL = 0x10,
742};
743
744/* look for widgets in the path between the given NIDs appropriate for
745 * volume and mute controls, and assign the values to ctls[].
746 *
747 * When no appropriate widget is found in the path, the badness value
748 * is incremented depending on the situation. The function returns the
749 * total badness for both volume and mute controls.
750 */
751static int assign_out_path_ctls(struct hda_codec *codec, hda_nid_t pin,
752 hda_nid_t dac)
753{
754 struct nid_path *path = snd_hda_get_nid_path(codec, dac, pin);
755 hda_nid_t nid;
756 unsigned int val;
757 int badness = 0;
758
759 if (!path)
760 return BAD_SHARED_VOL * 2;
761 nid = look_for_out_vol_nid(codec, path);
762 if (nid) {
763 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
764 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
765 badness += BAD_SHARED_VOL;
766 else
767 path->ctls[NID_PATH_VOL_CTL] = val;
768 } else
769 badness += BAD_SHARED_VOL;
770 nid = look_for_out_mute_nid(codec, path);
771 if (nid) {
772 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
773 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
774 nid_has_mute(codec, nid, HDA_OUTPUT))
775 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
776 else
777 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
778 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
779 badness += BAD_SHARED_VOL;
780 else
781 path->ctls[NID_PATH_MUTE_CTL] = val;
782 } else
783 badness += BAD_SHARED_VOL;
784 return badness;
785}
786
787struct badness_table {
788 int no_primary_dac; /* no primary DAC */
789 int no_dac; /* no secondary DACs */
790 int shared_primary; /* primary DAC is shared with main output */
791 int shared_surr; /* secondary DAC shared with main or primary */
792 int shared_clfe; /* third DAC shared with main or primary */
793 int shared_surr_main; /* secondary DAC sahred with main/DAC0 */
794};
795
796static struct badness_table main_out_badness = {
797 .no_primary_dac = BAD_NO_PRIMARY_DAC,
798 .no_dac = BAD_NO_DAC,
799 .shared_primary = BAD_NO_PRIMARY_DAC,
800 .shared_surr = BAD_SHARED_SURROUND,
801 .shared_clfe = BAD_SHARED_CLFE,
802 .shared_surr_main = BAD_SHARED_SURROUND,
803};
804
805static struct badness_table extra_out_badness = {
806 .no_primary_dac = BAD_NO_DAC,
807 .no_dac = BAD_NO_DAC,
808 .shared_primary = BAD_NO_EXTRA_DAC,
809 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
810 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
811 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
812};
813
814/* try to assign DACs to pins and return the resultant badness */
815static int try_assign_dacs(struct hda_codec *codec, int num_outs,
816 const hda_nid_t *pins, hda_nid_t *dacs,
817 const struct badness_table *bad)
818{
819 struct hda_gen_spec *spec = codec->spec;
820 struct auto_pin_cfg *cfg = &spec->autocfg;
821 int i, j;
822 int badness = 0;
823 hda_nid_t dac;
824
825 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 return 0;
827
Takashi Iwai352f7f92012-12-19 12:52:06 +0100828 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100829 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100830 hda_nid_t pin = pins[i];
831 if (!dacs[i])
832 dacs[i] = look_for_dac(codec, pin, false);
833 if (!dacs[i] && !i) {
834 for (j = 1; j < num_outs; j++) {
835 if (is_reachable_path(codec, dacs[j], pin)) {
836 dacs[0] = dacs[j];
837 dacs[j] = 0;
838 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 }
840 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100841 }
842 dac = dacs[i];
843 if (!dac) {
844 if (is_reachable_path(codec, dacs[0], pin))
845 dac = dacs[0];
846 else if (cfg->line_outs > i &&
847 is_reachable_path(codec, spec->private_dac_nids[i], pin))
848 dac = spec->private_dac_nids[i];
849 if (dac) {
850 if (!i)
851 badness += bad->shared_primary;
852 else if (i == 1)
853 badness += bad->shared_surr;
854 else
855 badness += bad->shared_clfe;
856 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
857 dac = spec->private_dac_nids[0];
858 badness += bad->shared_surr_main;
859 } else if (!i)
860 badness += bad->no_primary_dac;
861 else
862 badness += bad->no_dac;
863 }
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100864 path = snd_hda_add_new_path(codec, dac, pin, 0);
865 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100866 dac = dacs[i] = 0;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100867 else
868 print_nid_path("output", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100869 if (dac)
870 badness += assign_out_path_ctls(codec, pin, dac);
871 }
872
873 return badness;
874}
875
876/* return NID if the given pin has only a single connection to a certain DAC */
877static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
878{
879 struct hda_gen_spec *spec = codec->spec;
880 int i;
881 hda_nid_t nid_found = 0;
882
883 for (i = 0; i < spec->num_all_dacs; i++) {
884 hda_nid_t nid = spec->all_dacs[i];
885 if (!nid || is_dac_already_used(codec, nid))
886 continue;
887 if (is_reachable_path(codec, nid, pin)) {
888 if (nid_found)
889 return 0;
890 nid_found = nid;
891 }
892 }
893 return nid_found;
894}
895
896/* check whether the given pin can be a multi-io pin */
897static bool can_be_multiio_pin(struct hda_codec *codec,
898 unsigned int location, hda_nid_t nid)
899{
900 unsigned int defcfg, caps;
901
902 defcfg = snd_hda_codec_get_pincfg(codec, nid);
903 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
904 return false;
905 if (location && get_defcfg_location(defcfg) != location)
906 return false;
907 caps = snd_hda_query_pin_caps(codec, nid);
908 if (!(caps & AC_PINCAP_OUT))
909 return false;
910 return true;
911}
912
913/*
914 * multi-io helper
915 *
916 * When hardwired is set, try to fill ony hardwired pins, and returns
917 * zero if any pins are filled, non-zero if nothing found.
918 * When hardwired is off, try to fill possible input pins, and returns
919 * the badness value.
920 */
921static int fill_multi_ios(struct hda_codec *codec,
922 hda_nid_t reference_pin,
923 bool hardwired, int offset)
924{
925 struct hda_gen_spec *spec = codec->spec;
926 struct auto_pin_cfg *cfg = &spec->autocfg;
927 int type, i, j, dacs, num_pins, old_pins;
928 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
929 unsigned int location = get_defcfg_location(defcfg);
930 int badness = 0;
931
932 old_pins = spec->multi_ios;
933 if (old_pins >= 2)
934 goto end_fill;
935
936 num_pins = 0;
937 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
938 for (i = 0; i < cfg->num_inputs; i++) {
939 if (cfg->inputs[i].type != type)
940 continue;
941 if (can_be_multiio_pin(codec, location,
942 cfg->inputs[i].pin))
943 num_pins++;
944 }
945 }
946 if (num_pins < 2)
947 goto end_fill;
948
949 dacs = spec->multiout.num_dacs;
950 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
951 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100952 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100953 hda_nid_t nid = cfg->inputs[i].pin;
954 hda_nid_t dac = 0;
955
956 if (cfg->inputs[i].type != type)
957 continue;
958 if (!can_be_multiio_pin(codec, location, nid))
959 continue;
960 for (j = 0; j < spec->multi_ios; j++) {
961 if (nid == spec->multi_io[j].pin)
962 break;
963 }
964 if (j < spec->multi_ios)
965 continue;
966
967 if (offset && offset + spec->multi_ios < dacs) {
968 dac = spec->private_dac_nids[offset + spec->multi_ios];
969 if (!is_reachable_path(codec, dac, nid))
970 dac = 0;
971 }
972 if (hardwired)
973 dac = get_dac_if_single(codec, nid);
974 else if (!dac)
975 dac = look_for_dac(codec, nid, false);
976 if (!dac) {
977 badness++;
978 continue;
979 }
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100980 path = snd_hda_add_new_path(codec, dac, nid, 0);
981 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100982 badness++;
983 continue;
984 }
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100985 print_nid_path("multiio", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100986 spec->multi_io[spec->multi_ios].pin = nid;
987 spec->multi_io[spec->multi_ios].dac = dac;
988 spec->multi_ios++;
989 if (spec->multi_ios >= 2)
990 break;
991 }
992 }
993 end_fill:
994 if (badness)
995 badness = BAD_MULTI_IO;
996 if (old_pins == spec->multi_ios) {
997 if (hardwired)
998 return 1; /* nothing found */
999 else
1000 return badness; /* no badness if nothing found */
1001 }
1002 if (!hardwired && spec->multi_ios < 2) {
1003 /* cancel newly assigned paths */
1004 spec->paths.used -= spec->multi_ios - old_pins;
1005 spec->multi_ios = old_pins;
1006 return badness;
1007 }
1008
1009 /* assign volume and mute controls */
1010 for (i = old_pins; i < spec->multi_ios; i++)
1011 badness += assign_out_path_ctls(codec, spec->multi_io[i].pin,
1012 spec->multi_io[i].dac);
1013
1014 return badness;
1015}
1016
1017/* map DACs for all pins in the list if they are single connections */
1018static bool map_singles(struct hda_codec *codec, int outs,
1019 const hda_nid_t *pins, hda_nid_t *dacs)
1020{
1021 int i;
1022 bool found = false;
1023 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001024 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001025 hda_nid_t dac;
1026 if (dacs[i])
1027 continue;
1028 dac = get_dac_if_single(codec, pins[i]);
1029 if (!dac)
1030 continue;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001031 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
1032 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001033 dacs[i] = dac;
1034 found = true;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001035 print_nid_path("output", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001036 }
1037 }
1038 return found;
1039}
1040
1041/* fill in the dac_nids table from the parsed pin configuration */
1042static int fill_and_eval_dacs(struct hda_codec *codec,
1043 bool fill_hardwired,
1044 bool fill_mio_first)
1045{
1046 struct hda_gen_spec *spec = codec->spec;
1047 struct auto_pin_cfg *cfg = &spec->autocfg;
1048 int i, err, badness;
1049
1050 /* set num_dacs once to full for look_for_dac() */
1051 spec->multiout.num_dacs = cfg->line_outs;
1052 spec->multiout.dac_nids = spec->private_dac_nids;
1053 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1054 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1055 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1056 spec->multi_ios = 0;
1057 snd_array_free(&spec->paths);
1058 badness = 0;
1059
1060 /* fill hard-wired DACs first */
1061 if (fill_hardwired) {
1062 bool mapped;
1063 do {
1064 mapped = map_singles(codec, cfg->line_outs,
1065 cfg->line_out_pins,
1066 spec->private_dac_nids);
1067 mapped |= map_singles(codec, cfg->hp_outs,
1068 cfg->hp_pins,
1069 spec->multiout.hp_out_nid);
1070 mapped |= map_singles(codec, cfg->speaker_outs,
1071 cfg->speaker_pins,
1072 spec->multiout.extra_out_nid);
1073 if (fill_mio_first && cfg->line_outs == 1 &&
1074 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1075 err = fill_multi_ios(codec, cfg->line_out_pins[0], true, 0);
1076 if (!err)
1077 mapped = true;
1078 }
1079 } while (mapped);
1080 }
1081
1082 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
1083 spec->private_dac_nids,
1084 &main_out_badness);
1085
1086 /* re-count num_dacs and squash invalid entries */
1087 spec->multiout.num_dacs = 0;
1088 for (i = 0; i < cfg->line_outs; i++) {
1089 if (spec->private_dac_nids[i])
1090 spec->multiout.num_dacs++;
1091 else {
1092 memmove(spec->private_dac_nids + i,
1093 spec->private_dac_nids + i + 1,
1094 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1095 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1096 }
1097 }
1098
1099 if (fill_mio_first &&
1100 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1101 /* try to fill multi-io first */
1102 err = fill_multi_ios(codec, cfg->line_out_pins[0], false, 0);
1103 if (err < 0)
1104 return err;
1105 /* we don't count badness at this stage yet */
1106 }
1107
1108 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1109 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1110 spec->multiout.hp_out_nid,
1111 &extra_out_badness);
1112 if (err < 0)
1113 return err;
1114 badness += err;
1115 }
1116 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1117 err = try_assign_dacs(codec, cfg->speaker_outs,
1118 cfg->speaker_pins,
1119 spec->multiout.extra_out_nid,
1120 &extra_out_badness);
1121 if (err < 0)
1122 return err;
1123 badness += err;
1124 }
1125 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1126 err = fill_multi_ios(codec, cfg->line_out_pins[0], false, 0);
1127 if (err < 0)
1128 return err;
1129 badness += err;
1130 }
1131 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1132 /* try multi-ios with HP + inputs */
1133 int offset = 0;
1134 if (cfg->line_outs >= 3)
1135 offset = 1;
1136 err = fill_multi_ios(codec, cfg->hp_pins[0], false, offset);
1137 if (err < 0)
1138 return err;
1139 badness += err;
1140 }
1141
1142 if (spec->multi_ios == 2) {
1143 for (i = 0; i < 2; i++)
1144 spec->private_dac_nids[spec->multiout.num_dacs++] =
1145 spec->multi_io[i].dac;
1146 spec->ext_channel_count = 2;
1147 } else if (spec->multi_ios) {
1148 spec->multi_ios = 0;
1149 badness += BAD_MULTI_IO;
1150 }
1151
1152 return badness;
1153}
1154
1155#define DEBUG_BADNESS
1156
1157#ifdef DEBUG_BADNESS
1158#define debug_badness snd_printdd
1159#else
1160#define debug_badness(...)
1161#endif
1162
1163static void debug_show_configs(struct hda_gen_spec *spec, struct auto_pin_cfg *cfg)
1164{
1165 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1166 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001167 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001168 spec->multiout.dac_nids[0],
1169 spec->multiout.dac_nids[1],
1170 spec->multiout.dac_nids[2],
1171 spec->multiout.dac_nids[3]);
1172 if (spec->multi_ios > 0)
1173 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1174 spec->multi_ios,
1175 spec->multi_io[0].pin, spec->multi_io[1].pin,
1176 spec->multi_io[0].dac, spec->multi_io[1].dac);
1177 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1178 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001179 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001180 spec->multiout.hp_out_nid[0],
1181 spec->multiout.hp_out_nid[1],
1182 spec->multiout.hp_out_nid[2],
1183 spec->multiout.hp_out_nid[3]);
1184 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1185 cfg->speaker_pins[0], cfg->speaker_pins[1],
1186 cfg->speaker_pins[2], cfg->speaker_pins[3],
1187 spec->multiout.extra_out_nid[0],
1188 spec->multiout.extra_out_nid[1],
1189 spec->multiout.extra_out_nid[2],
1190 spec->multiout.extra_out_nid[3]);
1191}
1192
1193/* find all available DACs of the codec */
1194static void fill_all_dac_nids(struct hda_codec *codec)
1195{
1196 struct hda_gen_spec *spec = codec->spec;
1197 int i;
1198 hda_nid_t nid = codec->start_nid;
1199
1200 spec->num_all_dacs = 0;
1201 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1202 for (i = 0; i < codec->num_nodes; i++, nid++) {
1203 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1204 continue;
1205 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1206 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1207 break;
1208 }
1209 spec->all_dacs[spec->num_all_dacs++] = nid;
1210 }
1211}
1212
1213static int parse_output_paths(struct hda_codec *codec)
1214{
1215 struct hda_gen_spec *spec = codec->spec;
1216 struct auto_pin_cfg *cfg = &spec->autocfg;
1217 struct auto_pin_cfg *best_cfg;
1218 int best_badness = INT_MAX;
1219 int badness;
1220 bool fill_hardwired = true, fill_mio_first = true;
1221 bool best_wired = true, best_mio = true;
1222 bool hp_spk_swapped = false;
1223
1224 fill_all_dac_nids(codec);
1225
1226 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1227 if (!best_cfg)
1228 return -ENOMEM;
1229 *best_cfg = *cfg;
1230
1231 for (;;) {
1232 badness = fill_and_eval_dacs(codec, fill_hardwired,
1233 fill_mio_first);
1234 if (badness < 0) {
1235 kfree(best_cfg);
1236 return badness;
1237 }
1238 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1239 cfg->line_out_type, fill_hardwired, fill_mio_first,
1240 badness);
1241 debug_show_configs(spec, cfg);
1242 if (badness < best_badness) {
1243 best_badness = badness;
1244 *best_cfg = *cfg;
1245 best_wired = fill_hardwired;
1246 best_mio = fill_mio_first;
1247 }
1248 if (!badness)
1249 break;
1250 fill_mio_first = !fill_mio_first;
1251 if (!fill_mio_first)
1252 continue;
1253 fill_hardwired = !fill_hardwired;
1254 if (!fill_hardwired)
1255 continue;
1256 if (hp_spk_swapped)
1257 break;
1258 hp_spk_swapped = true;
1259 if (cfg->speaker_outs > 0 &&
1260 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1261 cfg->hp_outs = cfg->line_outs;
1262 memcpy(cfg->hp_pins, cfg->line_out_pins,
1263 sizeof(cfg->hp_pins));
1264 cfg->line_outs = cfg->speaker_outs;
1265 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1266 sizeof(cfg->speaker_pins));
1267 cfg->speaker_outs = 0;
1268 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1269 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1270 fill_hardwired = true;
1271 continue;
1272 }
1273 if (cfg->hp_outs > 0 &&
1274 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1275 cfg->speaker_outs = cfg->line_outs;
1276 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1277 sizeof(cfg->speaker_pins));
1278 cfg->line_outs = cfg->hp_outs;
1279 memcpy(cfg->line_out_pins, cfg->hp_pins,
1280 sizeof(cfg->hp_pins));
1281 cfg->hp_outs = 0;
1282 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1283 cfg->line_out_type = AUTO_PIN_HP_OUT;
1284 fill_hardwired = true;
1285 continue;
1286 }
1287 break;
1288 }
1289
1290 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001291 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001292 *cfg = *best_cfg;
1293 fill_and_eval_dacs(codec, best_wired, best_mio);
1294 }
1295 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1296 cfg->line_out_type, best_wired, best_mio);
1297 debug_show_configs(spec, cfg);
1298
1299 if (cfg->line_out_pins[0]) {
1300 struct nid_path *path;
1301 path = snd_hda_get_nid_path(codec,
1302 spec->multiout.dac_nids[0],
1303 cfg->line_out_pins[0]);
1304 if (path)
1305 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
1306 }
1307
1308 kfree(best_cfg);
1309 return 0;
1310}
1311
1312/* add playback controls from the parsed DAC table */
1313static int create_multi_out_ctls(struct hda_codec *codec,
1314 const struct auto_pin_cfg *cfg)
1315{
1316 struct hda_gen_spec *spec = codec->spec;
1317 int i, err, noutputs;
1318
1319 noutputs = cfg->line_outs;
1320 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1321 noutputs += spec->multi_ios;
1322
1323 for (i = 0; i < noutputs; i++) {
1324 const char *name;
1325 int index;
1326 hda_nid_t dac, pin;
1327 struct nid_path *path;
1328
1329 dac = spec->multiout.dac_nids[i];
1330 if (!dac)
1331 continue;
1332 if (i >= cfg->line_outs) {
1333 pin = spec->multi_io[i - 1].pin;
1334 index = 0;
1335 name = channel_name[i];
1336 } else {
1337 pin = cfg->line_out_pins[i];
1338 name = get_line_out_pfx(spec, i, true, &index);
1339 }
1340
1341 path = snd_hda_get_nid_path(codec, dac, pin);
1342 if (!path)
1343 continue;
1344 if (!name || !strcmp(name, "CLFE")) {
1345 /* Center/LFE */
1346 err = add_vol_ctl(codec, "Center", 0, 1, path);
1347 if (err < 0)
1348 return err;
1349 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1350 if (err < 0)
1351 return err;
1352 err = add_sw_ctl(codec, "Center", 0, 1, path);
1353 if (err < 0)
1354 return err;
1355 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1356 if (err < 0)
1357 return err;
1358 } else {
1359 err = add_stereo_vol(codec, name, index, path);
1360 if (err < 0)
1361 return err;
1362 err = add_stereo_sw(codec, name, index, path);
1363 if (err < 0)
1364 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 }
1366 }
1367 return 0;
1368}
1369
Takashi Iwai352f7f92012-12-19 12:52:06 +01001370static int create_extra_out(struct hda_codec *codec, hda_nid_t pin,
1371 hda_nid_t dac, const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001373 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 int err;
1375
Takashi Iwai352f7f92012-12-19 12:52:06 +01001376 path = snd_hda_get_nid_path(codec, dac, pin);
1377 if (!path)
1378 return 0;
1379 /* bind volume control will be created in the case of dac = 0 */
1380 if (dac) {
1381 err = add_stereo_vol(codec, pfx, cidx, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 if (err < 0)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001383 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001385 err = add_stereo_sw(codec, pfx, cidx, path);
1386 if (err < 0)
1387 return err;
1388 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389}
1390
Takashi Iwai352f7f92012-12-19 12:52:06 +01001391/* add playback controls for speaker and HP outputs */
1392static int create_extra_outs(struct hda_codec *codec, int num_pins,
1393 const hda_nid_t *pins, const hda_nid_t *dacs,
1394 const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001396 struct hda_gen_spec *spec = codec->spec;
1397 struct hda_bind_ctls *ctl;
1398 char name[32];
1399 int i, n, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
Takashi Iwai352f7f92012-12-19 12:52:06 +01001401 if (!num_pins || !pins[0])
1402 return 0;
1403
1404 if (num_pins == 1) {
1405 hda_nid_t dac = *dacs;
1406 if (!dac)
1407 dac = spec->multiout.dac_nids[0];
1408 return create_extra_out(codec, *pins, dac, pfx, 0);
Takashi Iwai97ec5582006-03-21 11:29:07 +01001409 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001410
1411 for (i = 0; i < num_pins; i++) {
1412 hda_nid_t dac;
1413 if (dacs[num_pins - 1])
1414 dac = dacs[i]; /* with individual volumes */
Takashi Iwai97ec5582006-03-21 11:29:07 +01001415 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01001416 dac = 0;
1417 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker")) {
1418 err = create_extra_out(codec, pins[i], dac,
1419 "Bass Speaker", 0);
1420 } else if (num_pins >= 3) {
1421 snprintf(name, sizeof(name), "%s %s",
1422 pfx, channel_name[i]);
1423 err = create_extra_out(codec, pins[i], dac, name, 0);
1424 } else {
1425 err = create_extra_out(codec, pins[i], dac, pfx, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001427 if (err < 0)
1428 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001430 if (dacs[num_pins - 1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 return 0;
1432
Takashi Iwai352f7f92012-12-19 12:52:06 +01001433 /* Let's create a bind-controls for volumes */
1434 ctl = new_bind_ctl(codec, num_pins, &snd_hda_bind_vol);
1435 if (!ctl)
1436 return -ENOMEM;
1437 n = 0;
1438 for (i = 0; i < num_pins; i++) {
1439 hda_nid_t vol;
1440 struct nid_path *path;
1441 if (!pins[i] || !dacs[i])
1442 continue;
1443 path = snd_hda_get_nid_path(codec, dacs[i], pins[i]);
1444 if (!path)
1445 continue;
1446 vol = look_for_out_vol_nid(codec, path);
1447 if (vol)
1448 ctl->values[n++] =
1449 HDA_COMPOSE_AMP_VAL(vol, 3, 0, HDA_OUTPUT);
1450 }
1451 if (n) {
1452 snprintf(name, sizeof(name), "%s Playback Volume", pfx);
1453 err = add_control(spec, HDA_CTL_BIND_VOL, name, 0, (long)ctl);
1454 if (err < 0)
1455 return err;
1456 }
1457 return 0;
1458}
Takashi Iwai97ec5582006-03-21 11:29:07 +01001459
Takashi Iwai352f7f92012-12-19 12:52:06 +01001460static int create_hp_out_ctls(struct hda_codec *codec)
1461{
1462 struct hda_gen_spec *spec = codec->spec;
1463 return create_extra_outs(codec, spec->autocfg.hp_outs,
1464 spec->autocfg.hp_pins,
1465 spec->multiout.hp_out_nid,
1466 "Headphone");
1467}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Takashi Iwai352f7f92012-12-19 12:52:06 +01001469static int create_speaker_out_ctls(struct hda_codec *codec)
1470{
1471 struct hda_gen_spec *spec = codec->spec;
1472 return create_extra_outs(codec, spec->autocfg.speaker_outs,
1473 spec->autocfg.speaker_pins,
1474 spec->multiout.extra_out_nid,
1475 "Speaker");
1476}
1477
1478/*
1479 * channel mode enum control
1480 */
1481
1482static int ch_mode_info(struct snd_kcontrol *kcontrol,
1483 struct snd_ctl_elem_info *uinfo)
1484{
1485 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1486 struct hda_gen_spec *spec = codec->spec;
1487
1488 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1489 uinfo->count = 1;
1490 uinfo->value.enumerated.items = spec->multi_ios + 1;
1491 if (uinfo->value.enumerated.item > spec->multi_ios)
1492 uinfo->value.enumerated.item = spec->multi_ios;
1493 sprintf(uinfo->value.enumerated.name, "%dch",
1494 (uinfo->value.enumerated.item + 1) * 2);
1495 return 0;
1496}
1497
1498static int ch_mode_get(struct snd_kcontrol *kcontrol,
1499 struct snd_ctl_elem_value *ucontrol)
1500{
1501 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1502 struct hda_gen_spec *spec = codec->spec;
1503 ucontrol->value.enumerated.item[0] = (spec->ext_channel_count - 1) / 2;
1504 return 0;
1505}
1506
1507static int set_multi_io(struct hda_codec *codec, int idx, bool output)
1508{
1509 struct hda_gen_spec *spec = codec->spec;
1510 hda_nid_t nid = spec->multi_io[idx].pin;
1511 struct nid_path *path;
1512
1513 path = snd_hda_get_nid_path(codec, spec->multi_io[idx].dac, nid);
1514 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001516
1517 if (path->active == output)
1518 return 0;
1519
1520 if (output) {
1521 snd_hda_set_pin_ctl_cache(codec, nid, PIN_OUT);
1522 snd_hda_activate_path(codec, path, true, true);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01001523 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001524 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01001525 set_pin_eapd(codec, nid, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001526 snd_hda_activate_path(codec, path, false, true);
1527 snd_hda_set_pin_ctl_cache(codec, nid,
1528 spec->multi_io[idx].ctl_in);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001530 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531}
1532
Takashi Iwai352f7f92012-12-19 12:52:06 +01001533static int ch_mode_put(struct snd_kcontrol *kcontrol,
1534 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001536 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1537 struct hda_gen_spec *spec = codec->spec;
1538 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
Takashi Iwai352f7f92012-12-19 12:52:06 +01001540 ch = ucontrol->value.enumerated.item[0];
1541 if (ch < 0 || ch > spec->multi_ios)
1542 return -EINVAL;
1543 if (ch == (spec->ext_channel_count - 1) / 2)
1544 return 0;
1545 spec->ext_channel_count = (ch + 1) * 2;
1546 for (i = 0; i < spec->multi_ios; i++)
1547 set_multi_io(codec, i, i < ch);
1548 spec->multiout.max_channels = max(spec->ext_channel_count,
1549 spec->const_channel_count);
1550 if (spec->need_dac_fix)
1551 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 return 1;
1553}
1554
Takashi Iwai352f7f92012-12-19 12:52:06 +01001555static const struct snd_kcontrol_new channel_mode_enum = {
1556 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1557 .name = "Channel Mode",
1558 .info = ch_mode_info,
1559 .get = ch_mode_get,
1560 .put = ch_mode_put,
1561};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
Takashi Iwai352f7f92012-12-19 12:52:06 +01001563static int create_multi_channel_mode(struct hda_codec *codec)
1564{
1565 struct hda_gen_spec *spec = codec->spec;
1566
1567 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01001568 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01001569 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 return 0;
1572}
1573
Takashi Iwai352f7f92012-12-19 12:52:06 +01001574/*
1575 * shared headphone/mic handling
1576 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02001577
Takashi Iwai352f7f92012-12-19 12:52:06 +01001578static void call_update_outputs(struct hda_codec *codec);
1579
1580/* for shared I/O, change the pin-control accordingly */
1581static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic)
1582{
1583 struct hda_gen_spec *spec = codec->spec;
1584 unsigned int val;
1585 hda_nid_t pin = spec->autocfg.inputs[1].pin;
1586 /* NOTE: this assumes that there are only two inputs, the
1587 * first is the real internal mic and the second is HP/mic jack.
1588 */
1589
1590 val = snd_hda_get_default_vref(codec, pin);
1591
1592 /* This pin does not have vref caps - let's enable vref on pin 0x18
1593 instead, as suggested by Realtek */
1594 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
1595 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
1596 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
1597 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01001598 snd_hda_set_pin_ctl_cache(codec, vref_pin,
1599 PIN_IN | (set_as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02001600 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001601
1602 val = set_as_mic ? val | PIN_IN : PIN_HP;
Takashi Iwai7594aa32012-12-20 15:38:40 +01001603 snd_hda_set_pin_ctl_cache(codec, pin, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001604
1605 spec->automute_speaker = !set_as_mic;
1606 call_update_outputs(codec);
1607}
1608
1609/* create a shared input with the headphone out */
1610static int create_shared_input(struct hda_codec *codec)
1611{
1612 struct hda_gen_spec *spec = codec->spec;
1613 struct auto_pin_cfg *cfg = &spec->autocfg;
1614 unsigned int defcfg;
1615 hda_nid_t nid;
1616
1617 /* only one internal input pin? */
1618 if (cfg->num_inputs != 1)
1619 return 0;
1620 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
1621 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
1622 return 0;
1623
1624 if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1625 nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
1626 else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
1627 nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
1628 else
1629 return 0; /* both not available */
1630
1631 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
1632 return 0; /* no input */
1633
1634 cfg->inputs[1].pin = nid;
1635 cfg->inputs[1].type = AUTO_PIN_MIC;
1636 cfg->num_inputs = 2;
1637 spec->shared_mic_hp = 1;
1638 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
1639 return 0;
1640}
1641
1642
1643/*
1644 * Parse input paths
1645 */
1646
1647#ifdef CONFIG_PM
1648/* add the powersave loopback-list entry */
1649static void add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
1650{
1651 struct hda_amp_list *list;
1652
1653 if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
1654 return;
1655 list = spec->loopback_list + spec->num_loopbacks;
1656 list->nid = mix;
1657 list->dir = HDA_INPUT;
1658 list->idx = idx;
1659 spec->num_loopbacks++;
Takashi Iwaicb53c622007-08-10 17:21:45 +02001660 spec->loopback.amplist = spec->loopback_list;
1661}
1662#else
Takashi Iwai352f7f92012-12-19 12:52:06 +01001663#define add_loopback_list(spec, mix, idx) /* NOP */
Takashi Iwaicb53c622007-08-10 17:21:45 +02001664#endif
1665
Takashi Iwai352f7f92012-12-19 12:52:06 +01001666/* create input playback/capture controls for the given pin */
1667static int new_analog_input(struct hda_codec *codec, hda_nid_t pin,
1668 const char *ctlname, int ctlidx,
1669 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001671 struct hda_gen_spec *spec = codec->spec;
1672 struct nid_path *path;
1673 unsigned int val;
1674 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
Takashi Iwai352f7f92012-12-19 12:52:06 +01001676 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
1677 !nid_has_mute(codec, mix_nid, HDA_INPUT))
1678 return 0; /* no need for analog loopback */
1679
1680 path = snd_hda_add_new_path(codec, pin, mix_nid, 2);
1681 if (!path)
1682 return -EINVAL;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001683 print_nid_path("loopback", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001684
1685 idx = path->idx[path->depth - 1];
1686 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
1687 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1688 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001689 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001691 path->ctls[NID_PATH_VOL_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 }
1693
Takashi Iwai352f7f92012-12-19 12:52:06 +01001694 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
1695 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1696 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001697 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001699 path->ctls[NID_PATH_MUTE_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 }
1701
Takashi Iwai352f7f92012-12-19 12:52:06 +01001702 path->active = true;
1703 add_loopback_list(spec, mix_nid, idx);
1704 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705}
1706
Takashi Iwai352f7f92012-12-19 12:52:06 +01001707static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001709 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
1710 return (pincap & AC_PINCAP_IN) != 0;
1711}
1712
1713/* Parse the codec tree and retrieve ADCs */
1714static int fill_adc_nids(struct hda_codec *codec)
1715{
1716 struct hda_gen_spec *spec = codec->spec;
1717 hda_nid_t nid;
1718 hda_nid_t *adc_nids = spec->adc_nids;
1719 int max_nums = ARRAY_SIZE(spec->adc_nids);
1720 int i, nums = 0;
1721
1722 nid = codec->start_nid;
1723 for (i = 0; i < codec->num_nodes; i++, nid++) {
1724 unsigned int caps = get_wcaps(codec, nid);
1725 int type = get_wcaps_type(caps);
1726
1727 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
1728 continue;
1729 adc_nids[nums] = nid;
1730 if (++nums >= max_nums)
1731 break;
1732 }
1733 spec->num_adc_nids = nums;
1734 return nums;
1735}
1736
1737/* filter out invalid adc_nids that don't give all active input pins;
1738 * if needed, check whether dynamic ADC-switching is available
1739 */
1740static int check_dyn_adc_switch(struct hda_codec *codec)
1741{
1742 struct hda_gen_spec *spec = codec->spec;
1743 struct hda_input_mux *imux = &spec->input_mux;
1744 hda_nid_t adc_nids[ARRAY_SIZE(spec->adc_nids)];
1745 int i, n, nums;
1746 hda_nid_t pin, adc;
1747
1748 again:
1749 nums = 0;
1750 for (n = 0; n < spec->num_adc_nids; n++) {
1751 adc = spec->adc_nids[n];
1752 for (i = 0; i < imux->num_items; i++) {
1753 pin = spec->imux_pins[i];
1754 if (!is_reachable_path(codec, pin, adc))
1755 break;
1756 }
1757 if (i >= imux->num_items)
1758 adc_nids[nums++] = adc;
1759 }
1760
1761 if (!nums) {
1762 if (spec->shared_mic_hp) {
1763 spec->shared_mic_hp = 0;
1764 imux->num_items = 1;
1765 goto again;
1766 }
1767
1768 /* check whether ADC-switch is possible */
1769 for (i = 0; i < imux->num_items; i++) {
1770 pin = spec->imux_pins[i];
1771 for (n = 0; n < spec->num_adc_nids; n++) {
1772 adc = spec->adc_nids[n];
1773 if (is_reachable_path(codec, pin, adc)) {
1774 spec->dyn_adc_idx[i] = n;
1775 break;
1776 }
1777 }
1778 }
1779
1780 snd_printdd("hda-codec: enabling ADC switching\n");
1781 spec->dyn_adc_switch = 1;
1782 } else if (nums != spec->num_adc_nids) {
1783 memcpy(spec->adc_nids, adc_nids, nums * sizeof(hda_nid_t));
1784 spec->num_adc_nids = nums;
1785 }
1786
1787 if (imux->num_items == 1 || spec->shared_mic_hp) {
1788 snd_printdd("hda-codec: reducing to a single ADC\n");
1789 spec->num_adc_nids = 1; /* reduce to a single ADC */
1790 }
1791
1792 /* single index for individual volumes ctls */
1793 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
1794 spec->num_adc_nids = 1;
1795
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 return 0;
1797}
1798
1799/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01001800 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001802static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02001803{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001804 struct hda_gen_spec *spec = codec->spec;
1805 const struct auto_pin_cfg *cfg = &spec->autocfg;
1806 hda_nid_t mixer = spec->mixer_nid;
1807 struct hda_input_mux *imux = &spec->input_mux;
1808 int num_adcs;
1809 int i, c, err, type_idx = 0;
1810 const char *prev_label = NULL;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02001811
Takashi Iwai352f7f92012-12-19 12:52:06 +01001812 num_adcs = fill_adc_nids(codec);
1813 if (num_adcs < 0)
1814 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02001815
Takashi Iwai352f7f92012-12-19 12:52:06 +01001816 for (i = 0; i < cfg->num_inputs; i++) {
1817 hda_nid_t pin;
1818 const char *label;
1819 bool imux_added;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
Takashi Iwai352f7f92012-12-19 12:52:06 +01001821 pin = cfg->inputs[i].pin;
1822 if (!is_input_pin(codec, pin))
1823 continue;
1824
1825 label = hda_get_autocfg_input_label(codec, cfg, i);
1826 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
1827 label = "Headphone Mic";
1828 if (prev_label && !strcmp(label, prev_label))
1829 type_idx++;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02001830 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01001831 type_idx = 0;
1832 prev_label = label;
1833
1834 if (mixer) {
1835 if (is_reachable_path(codec, pin, mixer)) {
1836 err = new_analog_input(codec, pin,
1837 label, type_idx, mixer);
1838 if (err < 0)
1839 return err;
1840 }
1841 }
1842
1843 imux_added = false;
1844 for (c = 0; c < num_adcs; c++) {
1845 struct nid_path *path;
1846 hda_nid_t adc = spec->adc_nids[c];
1847
1848 if (!is_reachable_path(codec, pin, adc))
1849 continue;
1850 path = snd_array_new(&spec->paths);
1851 if (!path)
1852 return -ENOMEM;
1853 memset(path, 0, sizeof(*path));
1854 if (!snd_hda_parse_nid_path(codec, pin, adc, 2, path)) {
1855 snd_printd(KERN_ERR
1856 "invalid input path 0x%x -> 0x%x\n",
1857 pin, adc);
1858 spec->paths.used--;
1859 continue;
1860 }
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001861 print_nid_path("input", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001862
1863 if (!imux_added) {
1864 spec->imux_pins[imux->num_items] = pin;
1865 snd_hda_add_imux_item(imux, label,
1866 imux->num_items, NULL);
1867 imux_added = true;
1868 }
1869 }
1870 }
1871
1872 return 0;
1873}
1874
1875
1876/*
1877 * input source mux
1878 */
1879
1880/* get the ADC NID corresponding to the given index */
1881static hda_nid_t get_adc_nid(struct hda_codec *codec, int adc_idx, int imux_idx)
1882{
1883 struct hda_gen_spec *spec = codec->spec;
1884 if (spec->dyn_adc_switch)
1885 adc_idx = spec->dyn_adc_idx[imux_idx];
1886 return spec->adc_nids[adc_idx];
1887}
1888
1889static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
1890 unsigned int idx);
1891
1892static int mux_enum_info(struct snd_kcontrol *kcontrol,
1893 struct snd_ctl_elem_info *uinfo)
1894{
1895 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1896 struct hda_gen_spec *spec = codec->spec;
1897 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
1898}
1899
1900static int mux_enum_get(struct snd_kcontrol *kcontrol,
1901 struct snd_ctl_elem_value *ucontrol)
1902{
1903 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1904 struct hda_gen_spec *spec = codec->spec;
1905 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1906
1907 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
1908 return 0;
1909}
1910
1911static int mux_enum_put(struct snd_kcontrol *kcontrol,
1912 struct snd_ctl_elem_value *ucontrol)
1913{
1914 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1915 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1916 return mux_select(codec, adc_idx,
1917 ucontrol->value.enumerated.item[0]);
1918}
1919
Takashi Iwai352f7f92012-12-19 12:52:06 +01001920static const struct snd_kcontrol_new cap_src_temp = {
1921 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1922 .name = "Input Source",
1923 .info = mux_enum_info,
1924 .get = mux_enum_get,
1925 .put = mux_enum_put,
1926};
1927
Takashi Iwai47d46ab2012-12-20 11:48:54 +01001928/*
1929 * capture volume and capture switch ctls
1930 */
1931
Takashi Iwai352f7f92012-12-19 12:52:06 +01001932typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
1933 struct snd_ctl_elem_value *ucontrol);
1934
Takashi Iwai47d46ab2012-12-20 11:48:54 +01001935/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001936static int cap_put_caller(struct snd_kcontrol *kcontrol,
1937 struct snd_ctl_elem_value *ucontrol,
1938 put_call_t func, int type)
1939{
1940 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1941 struct hda_gen_spec *spec = codec->spec;
1942 const struct hda_input_mux *imux;
1943 struct nid_path *path;
1944 int i, adc_idx, err = 0;
1945
1946 imux = &spec->input_mux;
1947 adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1948 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01001949 /* we use the cache-only update at first since multiple input paths
1950 * may shared the same amp; by updating only caches, the redundant
1951 * writes to hardware can be reduced.
1952 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001953 codec->cached_write = 1;
1954 for (i = 0; i < imux->num_items; i++) {
1955 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
1956 get_adc_nid(codec, adc_idx, i));
1957 if (!path->ctls[type])
1958 continue;
1959 kcontrol->private_value = path->ctls[type];
1960 err = func(kcontrol, ucontrol);
1961 if (err < 0)
1962 goto error;
1963 }
1964 error:
1965 codec->cached_write = 0;
1966 mutex_unlock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01001967 snd_hda_codec_flush_amp_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001968 if (err >= 0 && spec->cap_sync_hook)
1969 spec->cap_sync_hook(codec);
1970 return err;
1971}
1972
1973/* capture volume ctl callbacks */
1974#define cap_vol_info snd_hda_mixer_amp_volume_info
1975#define cap_vol_get snd_hda_mixer_amp_volume_get
1976#define cap_vol_tlv snd_hda_mixer_amp_tlv
1977
1978static int cap_vol_put(struct snd_kcontrol *kcontrol,
1979 struct snd_ctl_elem_value *ucontrol)
1980{
1981 return cap_put_caller(kcontrol, ucontrol,
1982 snd_hda_mixer_amp_volume_put,
1983 NID_PATH_VOL_CTL);
1984}
1985
1986static const struct snd_kcontrol_new cap_vol_temp = {
1987 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1988 .name = "Capture Volume",
1989 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
1990 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
1991 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
1992 .info = cap_vol_info,
1993 .get = cap_vol_get,
1994 .put = cap_vol_put,
1995 .tlv = { .c = cap_vol_tlv },
1996};
1997
1998/* capture switch ctl callbacks */
1999#define cap_sw_info snd_ctl_boolean_stereo_info
2000#define cap_sw_get snd_hda_mixer_amp_switch_get
2001
2002static int cap_sw_put(struct snd_kcontrol *kcontrol,
2003 struct snd_ctl_elem_value *ucontrol)
2004{
2005 return cap_put_caller(kcontrol, ucontrol,
2006 snd_hda_mixer_amp_switch_put,
2007 NID_PATH_MUTE_CTL);
2008}
2009
2010static const struct snd_kcontrol_new cap_sw_temp = {
2011 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2012 .name = "Capture Switch",
2013 .info = cap_sw_info,
2014 .get = cap_sw_get,
2015 .put = cap_sw_put,
2016};
2017
2018static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
2019{
2020 hda_nid_t nid;
2021 int i, depth;
2022
2023 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
2024 for (depth = 0; depth < 3; depth++) {
2025 if (depth >= path->depth)
2026 return -EINVAL;
2027 i = path->depth - depth - 1;
2028 nid = path->path[i];
2029 if (!path->ctls[NID_PATH_VOL_CTL]) {
2030 if (nid_has_volume(codec, nid, HDA_OUTPUT))
2031 path->ctls[NID_PATH_VOL_CTL] =
2032 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2033 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
2034 int idx = path->idx[i];
2035 if (!depth && codec->single_adc_amp)
2036 idx = 0;
2037 path->ctls[NID_PATH_VOL_CTL] =
2038 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2039 }
2040 }
2041 if (!path->ctls[NID_PATH_MUTE_CTL]) {
2042 if (nid_has_mute(codec, nid, HDA_OUTPUT))
2043 path->ctls[NID_PATH_MUTE_CTL] =
2044 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2045 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
2046 int idx = path->idx[i];
2047 if (!depth && codec->single_adc_amp)
2048 idx = 0;
2049 path->ctls[NID_PATH_MUTE_CTL] =
2050 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2051 }
2052 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01002053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 return 0;
2055}
2056
Takashi Iwai352f7f92012-12-19 12:52:06 +01002057static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002059 struct hda_gen_spec *spec = codec->spec;
2060 struct auto_pin_cfg *cfg = &spec->autocfg;
2061 unsigned int val;
2062 int i;
2063
2064 if (!spec->inv_dmic_split)
2065 return false;
2066 for (i = 0; i < cfg->num_inputs; i++) {
2067 if (cfg->inputs[i].pin != nid)
2068 continue;
2069 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2070 return false;
2071 val = snd_hda_codec_get_pincfg(codec, nid);
2072 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
2073 }
2074 return false;
2075}
2076
2077static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
2078 int idx, bool is_switch, unsigned int ctl,
2079 bool inv_dmic)
2080{
2081 struct hda_gen_spec *spec = codec->spec;
2082 char tmpname[44];
2083 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
2084 const char *sfx = is_switch ? "Switch" : "Volume";
2085 unsigned int chs = inv_dmic ? 1 : 3;
2086 int err;
2087
2088 if (!ctl)
2089 return 0;
2090
2091 if (label)
2092 snprintf(tmpname, sizeof(tmpname),
2093 "%s Capture %s", label, sfx);
2094 else
2095 snprintf(tmpname, sizeof(tmpname),
2096 "Capture %s", sfx);
2097 err = add_control(spec, type, tmpname, idx,
2098 amp_val_replace_channels(ctl, chs));
2099 if (err < 0 || !inv_dmic)
2100 return err;
2101
2102 /* Make independent right kcontrol */
2103 if (label)
2104 snprintf(tmpname, sizeof(tmpname),
2105 "Inverted %s Capture %s", label, sfx);
2106 else
2107 snprintf(tmpname, sizeof(tmpname),
2108 "Inverted Capture %s", sfx);
2109 return add_control(spec, type, tmpname, idx,
2110 amp_val_replace_channels(ctl, 2));
2111}
2112
2113/* create single (and simple) capture volume and switch controls */
2114static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
2115 unsigned int vol_ctl, unsigned int sw_ctl,
2116 bool inv_dmic)
2117{
2118 int err;
2119 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
2120 if (err < 0)
2121 return err;
2122 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
2123 if (err < 0)
2124 return err;
2125 return 0;
2126}
2127
2128/* create bound capture volume and switch controls */
2129static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
2130 unsigned int vol_ctl, unsigned int sw_ctl)
2131{
2132 struct hda_gen_spec *spec = codec->spec;
2133 struct snd_kcontrol_new *knew;
2134
2135 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002136 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002137 if (!knew)
2138 return -ENOMEM;
2139 knew->index = idx;
2140 knew->private_value = vol_ctl;
2141 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2142 }
2143 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002144 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002145 if (!knew)
2146 return -ENOMEM;
2147 knew->index = idx;
2148 knew->private_value = sw_ctl;
2149 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2150 }
2151 return 0;
2152}
2153
2154/* return the vol ctl when used first in the imux list */
2155static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
2156{
2157 struct hda_gen_spec *spec = codec->spec;
2158 struct nid_path *path;
2159 unsigned int ctl;
2160 int i;
2161
2162 path = snd_hda_get_nid_path(codec, spec->imux_pins[idx],
2163 get_adc_nid(codec, 0, idx));
2164 if (!path)
2165 return 0;
2166 ctl = path->ctls[type];
2167 if (!ctl)
2168 return 0;
2169 for (i = 0; i < idx - 1; i++) {
2170 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2171 get_adc_nid(codec, 0, i));
2172 if (path && path->ctls[type] == ctl)
2173 return 0;
2174 }
2175 return ctl;
2176}
2177
2178/* create individual capture volume and switch controls per input */
2179static int create_multi_cap_vol_ctl(struct hda_codec *codec)
2180{
2181 struct hda_gen_spec *spec = codec->spec;
2182 struct hda_input_mux *imux = &spec->input_mux;
2183 int i, err, type, type_idx = 0;
2184 const char *prev_label = NULL;
2185
2186 for (i = 0; i < imux->num_items; i++) {
2187 const char *label;
2188 bool inv_dmic;
2189 label = hda_get_autocfg_input_label(codec, &spec->autocfg, i);
2190 if (prev_label && !strcmp(label, prev_label))
2191 type_idx++;
2192 else
2193 type_idx = 0;
2194 prev_label = label;
2195 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
2196
2197 for (type = 0; type < 2; type++) {
2198 err = add_single_cap_ctl(codec, label, type_idx, type,
2199 get_first_cap_ctl(codec, i, type),
2200 inv_dmic);
2201 if (err < 0)
2202 return err;
2203 }
2204 }
2205 return 0;
2206}
2207
2208static int create_capture_mixers(struct hda_codec *codec)
2209{
2210 struct hda_gen_spec *spec = codec->spec;
2211 struct hda_input_mux *imux = &spec->input_mux;
2212 int i, n, nums, err;
2213
2214 if (spec->dyn_adc_switch)
2215 nums = 1;
2216 else
2217 nums = spec->num_adc_nids;
2218
2219 if (!spec->auto_mic && imux->num_items > 1) {
2220 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01002221 const char *name;
2222 name = nums > 1 ? "Input Source" : "Capture Source";
2223 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002224 if (!knew)
2225 return -ENOMEM;
2226 knew->count = nums;
2227 }
2228
2229 for (n = 0; n < nums; n++) {
2230 bool multi = false;
2231 bool inv_dmic = false;
2232 int vol, sw;
2233
2234 vol = sw = 0;
2235 for (i = 0; i < imux->num_items; i++) {
2236 struct nid_path *path;
2237 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2238 get_adc_nid(codec, n, i));
2239 if (!path)
2240 continue;
2241 parse_capvol_in_path(codec, path);
2242 if (!vol)
2243 vol = path->ctls[NID_PATH_VOL_CTL];
2244 else if (vol != path->ctls[NID_PATH_VOL_CTL])
2245 multi = true;
2246 if (!sw)
2247 sw = path->ctls[NID_PATH_MUTE_CTL];
2248 else if (sw != path->ctls[NID_PATH_MUTE_CTL])
2249 multi = true;
2250 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
2251 inv_dmic = true;
2252 }
2253
2254 if (!multi)
2255 err = create_single_cap_vol_ctl(codec, n, vol, sw,
2256 inv_dmic);
2257 else if (!spec->multi_cap_vol)
2258 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
2259 else
2260 err = create_multi_cap_vol_ctl(codec);
2261 if (err < 0)
2262 return err;
2263 }
2264
2265 return 0;
2266}
2267
2268/*
2269 * add mic boosts if needed
2270 */
2271static int parse_mic_boost(struct hda_codec *codec)
2272{
2273 struct hda_gen_spec *spec = codec->spec;
2274 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002275 int i, err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002276 int type_idx = 0;
2277 hda_nid_t nid;
2278 const char *prev_label = NULL;
2279
2280 for (i = 0; i < cfg->num_inputs; i++) {
2281 if (cfg->inputs[i].type > AUTO_PIN_MIC)
2282 break;
2283 nid = cfg->inputs[i].pin;
2284 if (get_wcaps(codec, nid) & AC_WCAP_IN_AMP) {
2285 const char *label;
2286 char boost_label[32];
2287 struct nid_path *path;
2288 unsigned int val;
2289
2290 label = hda_get_autocfg_input_label(codec, cfg, i);
2291 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
2292 label = "Headphone Mic";
2293 if (prev_label && !strcmp(label, prev_label))
2294 type_idx++;
2295 else
2296 type_idx = 0;
2297 prev_label = label;
2298
2299 snprintf(boost_label, sizeof(boost_label),
2300 "%s Boost Volume", label);
2301 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
2302 err = add_control(spec, HDA_CTL_WIDGET_VOL,
2303 boost_label, type_idx, val);
2304 if (err < 0)
2305 return err;
2306
2307 path = snd_hda_get_nid_path(codec, nid, 0);
2308 if (path)
2309 path->ctls[NID_PATH_BOOST_CTL] = val;
2310 }
2311 }
2312 return 0;
2313}
2314
2315/*
2316 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
2317 */
2318static void parse_digital(struct hda_codec *codec)
2319{
2320 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002321 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002322 int i, nums;
2323 hda_nid_t dig_nid;
2324
2325 /* support multiple SPDIFs; the secondary is set up as a slave */
2326 nums = 0;
2327 for (i = 0; i < spec->autocfg.dig_outs; i++) {
2328 hda_nid_t pin = spec->autocfg.dig_out_pins[i];
2329 dig_nid = look_for_dac(codec, pin, true);
2330 if (!dig_nid)
2331 continue;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002332 path = snd_hda_add_new_path(codec, dig_nid, pin, 2);
2333 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002334 continue;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002335 print_nid_path("digout", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002336 if (!nums) {
2337 spec->multiout.dig_out_nid = dig_nid;
2338 spec->dig_out_type = spec->autocfg.dig_out_type[0];
2339 } else {
2340 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
2341 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
2342 break;
2343 spec->slave_dig_outs[nums - 1] = dig_nid;
2344 }
2345 nums++;
2346 }
2347
2348 if (spec->autocfg.dig_in_pin) {
2349 dig_nid = codec->start_nid;
2350 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002351 unsigned int wcaps = get_wcaps(codec, dig_nid);
2352 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
2353 continue;
2354 if (!(wcaps & AC_WCAP_DIGITAL))
2355 continue;
2356 path = snd_hda_add_new_path(codec,
2357 spec->autocfg.dig_in_pin,
2358 dig_nid, 2);
2359 if (path) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002360 print_nid_path("digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002361 path->active = true;
2362 spec->dig_in_nid = dig_nid;
2363 break;
2364 }
2365 }
2366 }
2367}
2368
2369
2370/*
2371 * input MUX handling
2372 */
2373
2374static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
2375
2376/* select the given imux item; either unmute exclusively or select the route */
2377static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2378 unsigned int idx)
2379{
2380 struct hda_gen_spec *spec = codec->spec;
2381 const struct hda_input_mux *imux;
2382 struct nid_path *path;
2383
2384 imux = &spec->input_mux;
2385 if (!imux->num_items)
2386 return 0;
2387
2388 if (idx >= imux->num_items)
2389 idx = imux->num_items - 1;
2390 if (spec->cur_mux[adc_idx] == idx)
2391 return 0;
2392
2393 path = snd_hda_get_nid_path(codec,
2394 spec->imux_pins[spec->cur_mux[adc_idx]],
2395 spec->adc_nids[adc_idx]);
2396 if (!path)
2397 return 0;
2398 if (path->active)
2399 snd_hda_activate_path(codec, path, false, false);
2400
2401 spec->cur_mux[adc_idx] = idx;
2402
2403 if (spec->shared_mic_hp)
2404 update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
2405
2406 if (spec->dyn_adc_switch)
2407 dyn_adc_pcm_resetup(codec, idx);
2408
2409 path = snd_hda_get_nid_path(codec, spec->imux_pins[idx],
2410 get_adc_nid(codec, adc_idx, idx));
2411 if (!path)
2412 return 0;
2413 if (path->active)
2414 return 0;
2415 snd_hda_activate_path(codec, path, true, false);
2416 if (spec->cap_sync_hook)
2417 spec->cap_sync_hook(codec);
2418 return 1;
2419}
2420
2421
2422/*
2423 * Jack detections for HP auto-mute and mic-switch
2424 */
2425
2426/* check each pin in the given array; returns true if any of them is plugged */
2427static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
2428{
2429 int i, present = 0;
2430
2431 for (i = 0; i < num_pins; i++) {
2432 hda_nid_t nid = pins[i];
2433 if (!nid)
2434 break;
2435 present |= snd_hda_jack_detect(codec, nid);
2436 }
2437 return present;
2438}
2439
2440/* standard HP/line-out auto-mute helper */
2441static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
2442 bool mute, bool hp_out)
2443{
2444 struct hda_gen_spec *spec = codec->spec;
2445 unsigned int pin_bits = mute ? 0 : (hp_out ? PIN_HP : PIN_OUT);
2446 int i;
2447
2448 for (i = 0; i < num_pins; i++) {
2449 hda_nid_t nid = pins[i];
2450 unsigned int val;
2451 if (!nid)
2452 break;
2453 /* don't reset VREF value in case it's controlling
2454 * the amp (see alc861_fixup_asus_amp_vref_0f())
2455 */
2456 if (spec->keep_vref_in_automute) {
2457 val = snd_hda_codec_read(codec, nid, 0,
2458 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2459 val &= ~PIN_HP;
2460 } else
2461 val = 0;
2462 val |= pin_bits;
Takashi Iwai7594aa32012-12-20 15:38:40 +01002463 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002464 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002465 }
2466}
2467
2468/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002469void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002470{
2471 struct hda_gen_spec *spec = codec->spec;
2472 int on;
2473
2474 /* Control HP pins/amps depending on master_mute state;
2475 * in general, HP pins/amps control should be enabled in all cases,
2476 * but currently set only for master_mute, just to be safe
2477 */
2478 if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
2479 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2480 spec->autocfg.hp_pins, spec->master_mute, true);
2481
2482 if (!spec->automute_speaker)
2483 on = 0;
2484 else
2485 on = spec->hp_jack_present | spec->line_jack_present;
2486 on |= spec->master_mute;
2487 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
2488 spec->autocfg.speaker_pins, on, false);
2489
2490 /* toggle line-out mutes if needed, too */
2491 /* if LO is a copy of either HP or Speaker, don't need to handle it */
2492 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
2493 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
2494 return;
2495 if (!spec->automute_lo)
2496 on = 0;
2497 else
2498 on = spec->hp_jack_present;
2499 on |= spec->master_mute;
2500 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2501 spec->autocfg.line_out_pins, on, false);
2502}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002503EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002504
2505static void call_update_outputs(struct hda_codec *codec)
2506{
2507 struct hda_gen_spec *spec = codec->spec;
2508 if (spec->automute_hook)
2509 spec->automute_hook(codec);
2510 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01002511 snd_hda_gen_update_outputs(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002512}
2513
2514/* standard HP-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002515void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002516{
2517 struct hda_gen_spec *spec = codec->spec;
2518
2519 spec->hp_jack_present =
2520 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2521 spec->autocfg.hp_pins);
2522 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
2523 return;
2524 call_update_outputs(codec);
2525}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002526EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002527
2528/* standard line-out-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002529void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002530{
2531 struct hda_gen_spec *spec = codec->spec;
2532
2533 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
2534 return;
2535 /* check LO jack only when it's different from HP */
2536 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
2537 return;
2538
2539 spec->line_jack_present =
2540 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2541 spec->autocfg.line_out_pins);
2542 if (!spec->automute_speaker || !spec->detect_lo)
2543 return;
2544 call_update_outputs(codec);
2545}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002546EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002547
2548/* standard mic auto-switch helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002549void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002550{
2551 struct hda_gen_spec *spec = codec->spec;
2552 int i;
2553
2554 if (!spec->auto_mic)
2555 return;
2556
2557 for (i = spec->am_num_entries - 1; i > 0; i--) {
2558 if (snd_hda_jack_detect(codec, spec->am_entry[i].pin)) {
2559 mux_select(codec, 0, spec->am_entry[i].idx);
2560 return;
2561 }
2562 }
2563 mux_select(codec, 0, spec->am_entry[0].idx);
2564}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002565EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002566
2567/*
2568 * Auto-Mute mode mixer enum support
2569 */
2570static int automute_mode_info(struct snd_kcontrol *kcontrol,
2571 struct snd_ctl_elem_info *uinfo)
2572{
2573 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2574 struct hda_gen_spec *spec = codec->spec;
2575 static const char * const texts3[] = {
2576 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02002577 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578
Takashi Iwai352f7f92012-12-19 12:52:06 +01002579 if (spec->automute_speaker_possible && spec->automute_lo_possible)
2580 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
2581 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2582}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583
Takashi Iwai352f7f92012-12-19 12:52:06 +01002584static int automute_mode_get(struct snd_kcontrol *kcontrol,
2585 struct snd_ctl_elem_value *ucontrol)
2586{
2587 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2588 struct hda_gen_spec *spec = codec->spec;
2589 unsigned int val = 0;
2590 if (spec->automute_speaker)
2591 val++;
2592 if (spec->automute_lo)
2593 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002594
Takashi Iwai352f7f92012-12-19 12:52:06 +01002595 ucontrol->value.enumerated.item[0] = val;
2596 return 0;
2597}
2598
2599static int automute_mode_put(struct snd_kcontrol *kcontrol,
2600 struct snd_ctl_elem_value *ucontrol)
2601{
2602 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2603 struct hda_gen_spec *spec = codec->spec;
2604
2605 switch (ucontrol->value.enumerated.item[0]) {
2606 case 0:
2607 if (!spec->automute_speaker && !spec->automute_lo)
2608 return 0;
2609 spec->automute_speaker = 0;
2610 spec->automute_lo = 0;
2611 break;
2612 case 1:
2613 if (spec->automute_speaker_possible) {
2614 if (!spec->automute_lo && spec->automute_speaker)
2615 return 0;
2616 spec->automute_speaker = 1;
2617 spec->automute_lo = 0;
2618 } else if (spec->automute_lo_possible) {
2619 if (spec->automute_lo)
2620 return 0;
2621 spec->automute_lo = 1;
2622 } else
2623 return -EINVAL;
2624 break;
2625 case 2:
2626 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
2627 return -EINVAL;
2628 if (spec->automute_speaker && spec->automute_lo)
2629 return 0;
2630 spec->automute_speaker = 1;
2631 spec->automute_lo = 1;
2632 break;
2633 default:
2634 return -EINVAL;
2635 }
2636 call_update_outputs(codec);
2637 return 1;
2638}
2639
2640static const struct snd_kcontrol_new automute_mode_enum = {
2641 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2642 .name = "Auto-Mute Mode",
2643 .info = automute_mode_info,
2644 .get = automute_mode_get,
2645 .put = automute_mode_put,
2646};
2647
2648static int add_automute_mode_enum(struct hda_codec *codec)
2649{
2650 struct hda_gen_spec *spec = codec->spec;
2651
Takashi Iwai12c93df2012-12-19 14:38:33 +01002652 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002653 return -ENOMEM;
2654 return 0;
2655}
2656
2657/*
2658 * Check the availability of HP/line-out auto-mute;
2659 * Set up appropriately if really supported
2660 */
2661static int check_auto_mute_availability(struct hda_codec *codec)
2662{
2663 struct hda_gen_spec *spec = codec->spec;
2664 struct auto_pin_cfg *cfg = &spec->autocfg;
2665 int present = 0;
2666 int i, err;
2667
2668 if (cfg->hp_pins[0])
2669 present++;
2670 if (cfg->line_out_pins[0])
2671 present++;
2672 if (cfg->speaker_pins[0])
2673 present++;
2674 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02002675 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002676
2677 if (!cfg->speaker_pins[0] &&
2678 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
2679 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2680 sizeof(cfg->speaker_pins));
2681 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683
Takashi Iwai352f7f92012-12-19 12:52:06 +01002684 if (!cfg->hp_pins[0] &&
2685 cfg->line_out_type == AUTO_PIN_HP_OUT) {
2686 memcpy(cfg->hp_pins, cfg->line_out_pins,
2687 sizeof(cfg->hp_pins));
2688 cfg->hp_outs = cfg->line_outs;
2689 }
2690
2691 for (i = 0; i < cfg->hp_outs; i++) {
2692 hda_nid_t nid = cfg->hp_pins[i];
2693 if (!is_jack_detectable(codec, nid))
2694 continue;
2695 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
2696 nid);
2697 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
Takashi Iwai5d550e12012-12-19 15:16:44 +01002698 snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002699 spec->detect_hp = 1;
2700 }
2701
2702 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
2703 if (cfg->speaker_outs)
2704 for (i = 0; i < cfg->line_outs; i++) {
2705 hda_nid_t nid = cfg->line_out_pins[i];
2706 if (!is_jack_detectable(codec, nid))
2707 continue;
2708 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
2709 snd_hda_jack_detect_enable_callback(codec, nid,
2710 HDA_GEN_FRONT_EVENT,
Takashi Iwai5d550e12012-12-19 15:16:44 +01002711 snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002712 spec->detect_lo = 1;
2713 }
2714 spec->automute_lo_possible = spec->detect_hp;
2715 }
2716
2717 spec->automute_speaker_possible = cfg->speaker_outs &&
2718 (spec->detect_hp || spec->detect_lo);
2719
2720 spec->automute_lo = spec->automute_lo_possible;
2721 spec->automute_speaker = spec->automute_speaker_possible;
2722
2723 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
2724 /* create a control for automute mode */
2725 err = add_automute_mode_enum(codec);
2726 if (err < 0)
2727 return err;
2728 }
2729 return 0;
2730}
2731
2732/* return the position of NID in the list, or -1 if not found */
2733static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
2734{
2735 int i;
2736 for (i = 0; i < nums; i++)
2737 if (list[i] == nid)
2738 return i;
2739 return -1;
2740}
2741
2742/* check whether all auto-mic pins are valid; setup indices if OK */
2743static bool auto_mic_check_imux(struct hda_codec *codec)
2744{
2745 struct hda_gen_spec *spec = codec->spec;
2746 const struct hda_input_mux *imux;
2747 int i;
2748
2749 imux = &spec->input_mux;
2750 for (i = 0; i < spec->am_num_entries; i++) {
2751 spec->am_entry[i].idx =
2752 find_idx_in_nid_list(spec->am_entry[i].pin,
2753 spec->imux_pins, imux->num_items);
2754 if (spec->am_entry[i].idx < 0)
2755 return false; /* no corresponding imux */
2756 }
2757
2758 /* we don't need the jack detection for the first pin */
2759 for (i = 1; i < spec->am_num_entries; i++)
2760 snd_hda_jack_detect_enable_callback(codec,
2761 spec->am_entry[i].pin,
2762 HDA_GEN_MIC_EVENT,
Takashi Iwai5d550e12012-12-19 15:16:44 +01002763 snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002764 return true;
2765}
2766
2767static int compare_attr(const void *ap, const void *bp)
2768{
2769 const struct automic_entry *a = ap;
2770 const struct automic_entry *b = bp;
2771 return (int)(a->attr - b->attr);
2772}
2773
2774/*
2775 * Check the availability of auto-mic switch;
2776 * Set up if really supported
2777 */
2778static int check_auto_mic_availability(struct hda_codec *codec)
2779{
2780 struct hda_gen_spec *spec = codec->spec;
2781 struct auto_pin_cfg *cfg = &spec->autocfg;
2782 unsigned int types;
2783 int i, num_pins;
2784
2785 types = 0;
2786 num_pins = 0;
2787 for (i = 0; i < cfg->num_inputs; i++) {
2788 hda_nid_t nid = cfg->inputs[i].pin;
2789 unsigned int attr;
2790 attr = snd_hda_codec_get_pincfg(codec, nid);
2791 attr = snd_hda_get_input_pin_attr(attr);
2792 if (types & (1 << attr))
2793 return 0; /* already occupied */
2794 switch (attr) {
2795 case INPUT_PIN_ATTR_INT:
2796 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2797 return 0; /* invalid type */
2798 break;
2799 case INPUT_PIN_ATTR_UNUSED:
2800 return 0; /* invalid entry */
2801 default:
2802 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
2803 return 0; /* invalid type */
2804 if (!spec->line_in_auto_switch &&
2805 cfg->inputs[i].type != AUTO_PIN_MIC)
2806 return 0; /* only mic is allowed */
2807 if (!is_jack_detectable(codec, nid))
2808 return 0; /* no unsol support */
2809 break;
2810 }
2811 if (num_pins >= MAX_AUTO_MIC_PINS)
2812 return 0;
2813 types |= (1 << attr);
2814 spec->am_entry[num_pins].pin = nid;
2815 spec->am_entry[num_pins].attr = attr;
2816 num_pins++;
2817 }
2818
2819 if (num_pins < 2)
2820 return 0;
2821
2822 spec->am_num_entries = num_pins;
2823 /* sort the am_entry in the order of attr so that the pin with a
2824 * higher attr will be selected when the jack is plugged.
2825 */
2826 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
2827 compare_attr, NULL);
2828
2829 if (!auto_mic_check_imux(codec))
2830 return 0;
2831
2832 spec->auto_mic = 1;
2833 spec->num_adc_nids = 1;
2834 spec->cur_mux[0] = spec->am_entry[0].idx;
2835 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
2836 spec->am_entry[0].pin,
2837 spec->am_entry[1].pin,
2838 spec->am_entry[2].pin);
2839
2840 return 0;
2841}
2842
2843
Takashi Iwai9eb413e2012-12-19 14:41:21 +01002844/*
2845 * Parse the given BIOS configuration and set up the hda_gen_spec
2846 *
2847 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002848 * or a negative error code
2849 */
2850int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01002851 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002852{
2853 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002854 int err;
2855
Takashi Iwai9eb413e2012-12-19 14:41:21 +01002856 if (cfg != &spec->autocfg) {
2857 spec->autocfg = *cfg;
2858 cfg = &spec->autocfg;
2859 }
2860
Takashi Iwai352f7f92012-12-19 12:52:06 +01002861 if (!cfg->line_outs) {
2862 if (cfg->dig_outs || cfg->dig_in_pin) {
2863 spec->multiout.max_channels = 2;
2864 spec->no_analog = 1;
2865 goto dig_only;
2866 }
2867 return 0; /* can't find valid BIOS pin config */
2868 }
2869
2870 if (!spec->no_primary_hp &&
2871 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
2872 cfg->line_outs <= cfg->hp_outs) {
2873 /* use HP as primary out */
2874 cfg->speaker_outs = cfg->line_outs;
2875 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2876 sizeof(cfg->speaker_pins));
2877 cfg->line_outs = cfg->hp_outs;
2878 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
2879 cfg->hp_outs = 0;
2880 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
2881 cfg->line_out_type = AUTO_PIN_HP_OUT;
2882 }
2883
2884 err = parse_output_paths(codec);
2885 if (err < 0)
2886 return err;
2887 err = create_multi_channel_mode(codec);
2888 if (err < 0)
2889 return err;
2890 err = create_multi_out_ctls(codec, cfg);
2891 if (err < 0)
2892 return err;
2893 err = create_hp_out_ctls(codec);
2894 if (err < 0)
2895 return err;
2896 err = create_speaker_out_ctls(codec);
2897 if (err < 0)
2898 return err;
2899 err = create_shared_input(codec);
2900 if (err < 0)
2901 return err;
2902 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002903 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02002904 return err;
2905
Takashi Iwai352f7f92012-12-19 12:52:06 +01002906 /* check the multiple speaker pins */
2907 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
2908 spec->const_channel_count = cfg->line_outs * 2;
2909 else
2910 spec->const_channel_count = cfg->speaker_outs * 2;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002911
Takashi Iwai352f7f92012-12-19 12:52:06 +01002912 if (spec->multi_ios > 0)
2913 spec->multiout.max_channels = max(spec->ext_channel_count,
2914 spec->const_channel_count);
2915 else
2916 spec->multiout.max_channels = spec->multiout.num_dacs * 2;
2917
2918 err = check_auto_mute_availability(codec);
2919 if (err < 0)
2920 return err;
2921
2922 err = check_dyn_adc_switch(codec);
2923 if (err < 0)
2924 return err;
2925
2926 if (!spec->shared_mic_hp) {
2927 err = check_auto_mic_availability(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002928 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 }
Takashi Iwai071c73a2006-08-23 18:34:06 +02002931
Takashi Iwai352f7f92012-12-19 12:52:06 +01002932 err = create_capture_mixers(codec);
2933 if (err < 0)
2934 return err;
2935
2936 err = parse_mic_boost(codec);
2937 if (err < 0)
2938 return err;
2939
2940 dig_only:
2941 parse_digital(codec);
2942
2943 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944}
Takashi Iwai352f7f92012-12-19 12:52:06 +01002945EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946
2947
2948/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002949 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002951
2952/* slave controls for virtual master */
2953static const char * const slave_pfxs[] = {
2954 "Front", "Surround", "Center", "LFE", "Side",
2955 "Headphone", "Speaker", "Mono", "Line Out",
2956 "CLFE", "Bass Speaker", "PCM",
2957 NULL,
2958};
2959
2960int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002962 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964
Takashi Iwai36502d02012-12-19 15:15:10 +01002965 if (spec->kctls.used) {
2966 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
2967 if (err < 0)
2968 return err;
2969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970
Takashi Iwai352f7f92012-12-19 12:52:06 +01002971 if (spec->multiout.dig_out_nid) {
2972 err = snd_hda_create_dig_out_ctls(codec,
2973 spec->multiout.dig_out_nid,
2974 spec->multiout.dig_out_nid,
2975 spec->pcm_rec[1].pcm_type);
2976 if (err < 0)
2977 return err;
2978 if (!spec->no_analog) {
2979 err = snd_hda_create_spdif_share_sw(codec,
2980 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 if (err < 0)
2982 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002983 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 }
2985 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002986 if (spec->dig_in_nid) {
2987 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
2988 if (err < 0)
2989 return err;
2990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991
Takashi Iwai352f7f92012-12-19 12:52:06 +01002992 /* if we have no master control, let's create it */
2993 if (!spec->no_analog &&
2994 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
2995 unsigned int vmaster_tlv[4];
2996 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
2997 HDA_OUTPUT, vmaster_tlv);
2998 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
2999 vmaster_tlv, slave_pfxs,
3000 "Playback Volume");
3001 if (err < 0)
3002 return err;
3003 }
3004 if (!spec->no_analog &&
3005 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
3006 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
3007 NULL, slave_pfxs,
3008 "Playback Switch",
3009 true, &spec->vmaster_mute.sw_kctl);
3010 if (err < 0)
3011 return err;
3012 if (spec->vmaster_mute.hook)
Takashi Iwaifd25a972012-12-20 14:57:18 +01003013 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
3014 spec->vmaster_mute_enum);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003015 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016
Takashi Iwai352f7f92012-12-19 12:52:06 +01003017 free_kctls(spec); /* no longer needed */
3018
3019 if (spec->shared_mic_hp) {
3020 int err;
3021 int nid = spec->autocfg.inputs[1].pin;
3022 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
3023 if (err < 0)
3024 return err;
3025 err = snd_hda_jack_detect_enable(codec, nid, 0);
3026 if (err < 0)
3027 return err;
3028 }
3029
3030 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
3031 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 return err;
3033
3034 return 0;
3035}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003036EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
3037
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038
3039/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003040 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042
Takashi Iwai352f7f92012-12-19 12:52:06 +01003043/*
3044 * Analog playback callbacks
3045 */
3046static int playback_pcm_open(struct hda_pcm_stream *hinfo,
3047 struct hda_codec *codec,
3048 struct snd_pcm_substream *substream)
3049{
3050 struct hda_gen_spec *spec = codec->spec;
3051 return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
3052 hinfo);
3053}
3054
3055static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01003056 struct hda_codec *codec,
3057 unsigned int stream_tag,
3058 unsigned int format,
3059 struct snd_pcm_substream *substream)
3060{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003061 struct hda_gen_spec *spec = codec->spec;
3062 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
3063 stream_tag, format, substream);
3064}
Takashi Iwai97ec5582006-03-21 11:29:07 +01003065
Takashi Iwai352f7f92012-12-19 12:52:06 +01003066static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3067 struct hda_codec *codec,
3068 struct snd_pcm_substream *substream)
3069{
3070 struct hda_gen_spec *spec = codec->spec;
3071 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
3072}
3073
3074/*
3075 * Digital out
3076 */
3077static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
3078 struct hda_codec *codec,
3079 struct snd_pcm_substream *substream)
3080{
3081 struct hda_gen_spec *spec = codec->spec;
3082 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3083}
3084
3085static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3086 struct hda_codec *codec,
3087 unsigned int stream_tag,
3088 unsigned int format,
3089 struct snd_pcm_substream *substream)
3090{
3091 struct hda_gen_spec *spec = codec->spec;
3092 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3093 stream_tag, format, substream);
3094}
3095
3096static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3097 struct hda_codec *codec,
3098 struct snd_pcm_substream *substream)
3099{
3100 struct hda_gen_spec *spec = codec->spec;
3101 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
3102}
3103
3104static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
3105 struct hda_codec *codec,
3106 struct snd_pcm_substream *substream)
3107{
3108 struct hda_gen_spec *spec = codec->spec;
3109 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3110}
3111
3112/*
3113 * Analog capture
3114 */
3115static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3116 struct hda_codec *codec,
3117 unsigned int stream_tag,
3118 unsigned int format,
3119 struct snd_pcm_substream *substream)
3120{
3121 struct hda_gen_spec *spec = codec->spec;
3122
3123 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01003124 stream_tag, 0, format);
3125 return 0;
3126}
3127
Takashi Iwai352f7f92012-12-19 12:52:06 +01003128static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3129 struct hda_codec *codec,
3130 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01003131{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003132 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01003133
Takashi Iwai352f7f92012-12-19 12:52:06 +01003134 snd_hda_codec_cleanup_stream(codec,
3135 spec->adc_nids[substream->number + 1]);
Takashi Iwai97ec5582006-03-21 11:29:07 +01003136 return 0;
3137}
3138
Takashi Iwai352f7f92012-12-19 12:52:06 +01003139/*
3140 */
3141static const struct hda_pcm_stream pcm_analog_playback = {
3142 .substreams = 1,
3143 .channels_min = 2,
3144 .channels_max = 8,
3145 /* NID is set in build_pcms */
3146 .ops = {
3147 .open = playback_pcm_open,
3148 .prepare = playback_pcm_prepare,
3149 .cleanup = playback_pcm_cleanup
3150 },
3151};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152
Takashi Iwai352f7f92012-12-19 12:52:06 +01003153static const struct hda_pcm_stream pcm_analog_capture = {
3154 .substreams = 1,
3155 .channels_min = 2,
3156 .channels_max = 2,
3157 /* NID is set in build_pcms */
3158};
3159
3160static const struct hda_pcm_stream pcm_analog_alt_playback = {
3161 .substreams = 1,
3162 .channels_min = 2,
3163 .channels_max = 2,
3164 /* NID is set in build_pcms */
3165};
3166
3167static const struct hda_pcm_stream pcm_analog_alt_capture = {
3168 .substreams = 2, /* can be overridden */
3169 .channels_min = 2,
3170 .channels_max = 2,
3171 /* NID is set in build_pcms */
3172 .ops = {
3173 .prepare = alt_capture_pcm_prepare,
3174 .cleanup = alt_capture_pcm_cleanup
3175 },
3176};
3177
3178static const struct hda_pcm_stream pcm_digital_playback = {
3179 .substreams = 1,
3180 .channels_min = 2,
3181 .channels_max = 2,
3182 /* NID is set in build_pcms */
3183 .ops = {
3184 .open = dig_playback_pcm_open,
3185 .close = dig_playback_pcm_close,
3186 .prepare = dig_playback_pcm_prepare,
3187 .cleanup = dig_playback_pcm_cleanup
3188 },
3189};
3190
3191static const struct hda_pcm_stream pcm_digital_capture = {
3192 .substreams = 1,
3193 .channels_min = 2,
3194 .channels_max = 2,
3195 /* NID is set in build_pcms */
3196};
3197
3198/* Used by build_pcms to flag that a PCM has no playback stream */
3199static const struct hda_pcm_stream pcm_null_stream = {
3200 .substreams = 0,
3201 .channels_min = 0,
3202 .channels_max = 0,
3203};
3204
3205/*
3206 * dynamic changing ADC PCM streams
3207 */
3208static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
3209{
3210 struct hda_gen_spec *spec = codec->spec;
3211 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
3212
3213 if (spec->cur_adc && spec->cur_adc != new_adc) {
3214 /* stream is running, let's swap the current ADC */
3215 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
3216 spec->cur_adc = new_adc;
3217 snd_hda_codec_setup_stream(codec, new_adc,
3218 spec->cur_adc_stream_tag, 0,
3219 spec->cur_adc_format);
3220 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003222 return false;
3223}
3224
3225/* analog capture with dynamic dual-adc changes */
3226static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3227 struct hda_codec *codec,
3228 unsigned int stream_tag,
3229 unsigned int format,
3230 struct snd_pcm_substream *substream)
3231{
3232 struct hda_gen_spec *spec = codec->spec;
3233 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
3234 spec->cur_adc_stream_tag = stream_tag;
3235 spec->cur_adc_format = format;
3236 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
3237 return 0;
3238}
3239
3240static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3241 struct hda_codec *codec,
3242 struct snd_pcm_substream *substream)
3243{
3244 struct hda_gen_spec *spec = codec->spec;
3245 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
3246 spec->cur_adc = 0;
3247 return 0;
3248}
3249
3250static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
3251 .substreams = 1,
3252 .channels_min = 2,
3253 .channels_max = 2,
3254 .nid = 0, /* fill later */
3255 .ops = {
3256 .prepare = dyn_adc_capture_pcm_prepare,
3257 .cleanup = dyn_adc_capture_pcm_cleanup
3258 },
3259};
3260
Takashi Iwaif873e532012-12-20 16:58:39 +01003261static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
3262 const char *chip_name)
3263{
3264 char *p;
3265
3266 if (*str)
3267 return;
3268 strlcpy(str, chip_name, len);
3269
3270 /* drop non-alnum chars after a space */
3271 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
3272 if (!isalnum(p[1])) {
3273 *p = 0;
3274 break;
3275 }
3276 }
3277 strlcat(str, sfx, len);
3278}
3279
Takashi Iwai352f7f92012-12-19 12:52:06 +01003280/* build PCM streams based on the parsed results */
3281int snd_hda_gen_build_pcms(struct hda_codec *codec)
3282{
3283 struct hda_gen_spec *spec = codec->spec;
3284 struct hda_pcm *info = spec->pcm_rec;
3285 const struct hda_pcm_stream *p;
3286 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287
3288 codec->num_pcms = 1;
3289 codec->pcm_info = info;
3290
Takashi Iwai352f7f92012-12-19 12:52:06 +01003291 if (spec->no_analog)
3292 goto skip_analog;
3293
Takashi Iwaif873e532012-12-20 16:58:39 +01003294 fill_pcm_stream_name(spec->stream_name_analog,
3295 sizeof(spec->stream_name_analog),
3296 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003297 info->name = spec->stream_name_analog;
3298
3299 if (spec->multiout.num_dacs > 0) {
3300 p = spec->stream_analog_playback;
3301 if (!p)
3302 p = &pcm_analog_playback;
3303 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3304 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
3305 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
3306 spec->multiout.max_channels;
3307 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
3308 spec->autocfg.line_outs == 2)
3309 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
3310 snd_pcm_2_1_chmaps;
3311 }
3312 if (spec->num_adc_nids) {
3313 p = spec->stream_analog_capture;
3314 if (!p) {
3315 if (spec->dyn_adc_switch)
3316 p = &dyn_adc_pcm_analog_capture;
3317 else
3318 p = &pcm_analog_capture;
3319 }
3320 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3321 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
3322 }
3323
Takashi Iwai352f7f92012-12-19 12:52:06 +01003324 skip_analog:
3325 /* SPDIF for stream index #1 */
3326 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01003327 fill_pcm_stream_name(spec->stream_name_digital,
3328 sizeof(spec->stream_name_digital),
3329 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003330 codec->num_pcms = 2;
3331 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
3332 info = spec->pcm_rec + 1;
3333 info->name = spec->stream_name_digital;
3334 if (spec->dig_out_type)
3335 info->pcm_type = spec->dig_out_type;
3336 else
3337 info->pcm_type = HDA_PCM_TYPE_SPDIF;
3338 if (spec->multiout.dig_out_nid) {
3339 p = spec->stream_digital_playback;
3340 if (!p)
3341 p = &pcm_digital_playback;
3342 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3343 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
3344 }
3345 if (spec->dig_in_nid) {
3346 p = spec->stream_digital_capture;
3347 if (!p)
3348 p = &pcm_digital_capture;
3349 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3350 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
3351 }
3352 }
3353
3354 if (spec->no_analog)
3355 return 0;
3356
3357 /* If the use of more than one ADC is requested for the current
3358 * model, configure a second analog capture-only PCM.
3359 */
3360 have_multi_adcs = (spec->num_adc_nids > 1) &&
3361 !spec->dyn_adc_switch && !spec->auto_mic;
3362 /* Additional Analaog capture for index #2 */
3363 if (spec->alt_dac_nid || have_multi_adcs) {
3364 codec->num_pcms = 3;
3365 info = spec->pcm_rec + 2;
3366 info->name = spec->stream_name_analog;
3367 if (spec->alt_dac_nid) {
3368 p = spec->stream_analog_alt_playback;
3369 if (!p)
3370 p = &pcm_analog_alt_playback;
3371 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3372 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
3373 spec->alt_dac_nid;
3374 } else {
3375 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
3376 pcm_null_stream;
3377 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
3378 }
3379 if (have_multi_adcs) {
3380 p = spec->stream_analog_alt_capture;
3381 if (!p)
3382 p = &pcm_analog_alt_capture;
3383 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3384 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
3385 spec->adc_nids[1];
3386 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
3387 spec->num_adc_nids - 1;
3388 } else {
3389 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
3390 pcm_null_stream;
3391 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
3392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 }
3394
3395 return 0;
3396}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003397EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
3398
3399
3400/*
3401 * Standard auto-parser initializations
3402 */
3403
3404/* configure the path from the given dac to the pin as the proper output */
3405static void set_output_and_unmute(struct hda_codec *codec, hda_nid_t pin,
3406 int pin_type, hda_nid_t dac)
3407{
3408 struct nid_path *path;
3409
3410 snd_hda_set_pin_ctl_cache(codec, pin, pin_type);
3411 path = snd_hda_get_nid_path(codec, dac, pin);
3412 if (!path)
3413 return;
3414 if (path->active)
3415 return;
3416 snd_hda_activate_path(codec, path, true, true);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01003417 set_pin_eapd(codec, pin, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003418}
3419
3420/* initialize primary output paths */
3421static void init_multi_out(struct hda_codec *codec)
3422{
3423 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai64049c82012-12-20 15:29:21 +01003424 hda_nid_t nid, dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003425 int pin_type;
3426 int i;
3427
3428 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
3429 pin_type = PIN_HP;
3430 else
3431 pin_type = PIN_OUT;
3432
Takashi Iwai64049c82012-12-20 15:29:21 +01003433 for (i = 0; i < spec->autocfg.line_outs; i++) {
3434 nid = spec->autocfg.line_out_pins[i];
3435 if (nid) {
3436 dac = spec->multiout.dac_nids[i];
3437 if (!dac)
3438 dac = spec->multiout.dac_nids[0];
3439 set_output_and_unmute(codec, nid, pin_type, dac);
3440 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003441 }
3442}
3443
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003444
3445static void __init_extra_out(struct hda_codec *codec, int num_outs,
3446 hda_nid_t *pins, hda_nid_t *dacs, int type)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003447{
3448 struct hda_gen_spec *spec = codec->spec;
3449 int i;
3450 hda_nid_t pin, dac;
3451
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003452 for (i = 0; i < num_outs; i++) {
3453 pin = pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003454 if (!pin)
3455 break;
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003456 dac = dacs[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003457 if (!dac) {
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003458 if (i > 0 && dacs[0])
3459 dac = dacs[0];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003460 else
3461 dac = spec->multiout.dac_nids[0];
3462 }
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003463 set_output_and_unmute(codec, pin, type, dac);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003464 }
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003465}
3466
3467/* initialize hp and speaker paths */
3468static void init_extra_out(struct hda_codec *codec)
3469{
3470 struct hda_gen_spec *spec = codec->spec;
3471
3472 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
3473 __init_extra_out(codec, spec->autocfg.hp_outs,
3474 spec->autocfg.hp_pins,
3475 spec->multiout.hp_out_nid, PIN_HP);
3476 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
3477 __init_extra_out(codec, spec->autocfg.speaker_outs,
3478 spec->autocfg.speaker_pins,
3479 spec->multiout.extra_out_nid, PIN_OUT);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003480}
3481
3482/* initialize multi-io paths */
3483static void init_multi_io(struct hda_codec *codec)
3484{
3485 struct hda_gen_spec *spec = codec->spec;
3486 int i;
3487
3488 for (i = 0; i < spec->multi_ios; i++) {
3489 hda_nid_t pin = spec->multi_io[i].pin;
3490 struct nid_path *path;
3491 path = snd_hda_get_nid_path(codec, spec->multi_io[i].dac, pin);
3492 if (!path)
3493 continue;
3494 if (!spec->multi_io[i].ctl_in)
3495 spec->multi_io[i].ctl_in =
3496 snd_hda_codec_update_cache(codec, pin, 0,
3497 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3498 snd_hda_activate_path(codec, path, path->active, true);
3499 }
3500}
3501
3502/* set up the input pin config, depending on the given auto-pin type */
3503static void set_input_pin(struct hda_codec *codec, hda_nid_t nid,
3504 int auto_pin_type)
3505{
3506 unsigned int val = PIN_IN;
3507 if (auto_pin_type == AUTO_PIN_MIC)
3508 val |= snd_hda_get_default_vref(codec, nid);
Takashi Iwai7594aa32012-12-20 15:38:40 +01003509 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003510}
3511
3512/* set up input pins and loopback paths */
3513static void init_analog_input(struct hda_codec *codec)
3514{
3515 struct hda_gen_spec *spec = codec->spec;
3516 struct auto_pin_cfg *cfg = &spec->autocfg;
3517 int i;
3518
3519 for (i = 0; i < cfg->num_inputs; i++) {
3520 hda_nid_t nid = cfg->inputs[i].pin;
3521 if (is_input_pin(codec, nid))
3522 set_input_pin(codec, nid, cfg->inputs[i].type);
3523
3524 /* init loopback inputs */
3525 if (spec->mixer_nid) {
3526 struct nid_path *path;
3527 path = snd_hda_get_nid_path(codec, nid, spec->mixer_nid);
3528 if (path)
3529 snd_hda_activate_path(codec, path,
3530 path->active, false);
3531 }
3532 }
3533}
3534
3535/* initialize ADC paths */
3536static void init_input_src(struct hda_codec *codec)
3537{
3538 struct hda_gen_spec *spec = codec->spec;
3539 struct hda_input_mux *imux = &spec->input_mux;
3540 struct nid_path *path;
3541 int i, c, nums;
3542
3543 if (spec->dyn_adc_switch)
3544 nums = 1;
3545 else
3546 nums = spec->num_adc_nids;
3547
3548 for (c = 0; c < nums; c++) {
3549 for (i = 0; i < imux->num_items; i++) {
3550 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
3551 get_adc_nid(codec, c, i));
3552 if (path) {
3553 bool active = path->active;
3554 if (i == spec->cur_mux[c])
3555 active = true;
3556 snd_hda_activate_path(codec, path, active, false);
3557 }
3558 }
3559 }
3560
3561 if (spec->shared_mic_hp)
3562 update_shared_mic_hp(codec, spec->cur_mux[0]);
3563
3564 if (spec->cap_sync_hook)
3565 spec->cap_sync_hook(codec);
3566}
3567
3568/* set right pin controls for digital I/O */
3569static void init_digital(struct hda_codec *codec)
3570{
3571 struct hda_gen_spec *spec = codec->spec;
3572 int i;
3573 hda_nid_t pin;
3574
3575 for (i = 0; i < spec->autocfg.dig_outs; i++) {
3576 pin = spec->autocfg.dig_out_pins[i];
3577 if (!pin)
3578 continue;
3579 set_output_and_unmute(codec, pin, PIN_OUT, 0);
3580 }
3581 pin = spec->autocfg.dig_in_pin;
3582 if (pin)
Takashi Iwai7594aa32012-12-20 15:38:40 +01003583 snd_hda_set_pin_ctl_cache(codec, pin, PIN_IN);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003584}
3585
Takashi Iwai973e4972012-12-20 15:16:09 +01003586/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
3587 * invalid unsol tags by some reason
3588 */
3589static void clear_unsol_on_unused_pins(struct hda_codec *codec)
3590{
3591 int i;
3592
3593 for (i = 0; i < codec->init_pins.used; i++) {
3594 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
3595 hda_nid_t nid = pin->nid;
3596 if (is_jack_detectable(codec, nid) &&
3597 !snd_hda_jack_tbl_get(codec, nid))
3598 snd_hda_codec_update_cache(codec, nid, 0,
3599 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
3600 }
3601}
3602
Takashi Iwai352f7f92012-12-19 12:52:06 +01003603int snd_hda_gen_init(struct hda_codec *codec)
3604{
3605 struct hda_gen_spec *spec = codec->spec;
3606
3607 if (spec->init_hook)
3608 spec->init_hook(codec);
3609
3610 snd_hda_apply_verbs(codec);
3611
Takashi Iwai3bbcd272012-12-20 11:50:58 +01003612 codec->cached_write = 1;
3613
Takashi Iwai352f7f92012-12-19 12:52:06 +01003614 init_multi_out(codec);
3615 init_extra_out(codec);
3616 init_multi_io(codec);
3617 init_analog_input(codec);
3618 init_input_src(codec);
3619 init_digital(codec);
3620
Takashi Iwai973e4972012-12-20 15:16:09 +01003621 clear_unsol_on_unused_pins(codec);
3622
Takashi Iwai352f7f92012-12-19 12:52:06 +01003623 /* call init functions of standard auto-mute helpers */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003624 snd_hda_gen_hp_automute(codec, NULL);
3625 snd_hda_gen_line_automute(codec, NULL);
3626 snd_hda_gen_mic_autoswitch(codec, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003627
Takashi Iwai3bbcd272012-12-20 11:50:58 +01003628 snd_hda_codec_flush_amp_cache(codec);
3629 snd_hda_codec_flush_cmd_cache(codec);
3630
Takashi Iwai352f7f92012-12-19 12:52:06 +01003631 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
3632 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
3633
3634 hda_call_check_power_status(codec, 0x01);
3635 return 0;
3636}
3637EXPORT_SYMBOL(snd_hda_gen_init);
3638
3639
3640/*
3641 * the generic codec support
3642 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643
Takashi Iwai83012a72012-08-24 18:38:08 +02003644#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003645static int generic_check_power_status(struct hda_codec *codec, hda_nid_t nid)
3646{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003647 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003648 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
3649}
3650#endif
3651
Takashi Iwai352f7f92012-12-19 12:52:06 +01003652static void generic_free(struct hda_codec *codec)
3653{
3654 snd_hda_gen_spec_free(codec->spec);
3655 kfree(codec->spec);
3656 codec->spec = NULL;
3657}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658
Takashi Iwai352f7f92012-12-19 12:52:06 +01003659static const struct hda_codec_ops generic_patch_ops = {
3660 .build_controls = snd_hda_gen_build_controls,
3661 .build_pcms = snd_hda_gen_build_pcms,
3662 .init = snd_hda_gen_init,
3663 .free = generic_free,
3664 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02003665#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003666 .check_power_status = generic_check_power_status,
3667#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668};
3669
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670int snd_hda_parse_generic_codec(struct hda_codec *codec)
3671{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003672 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 int err;
3674
Takashi Iwaie560d8d2005-09-09 14:21:46 +02003675 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003676 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003678 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003681 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
3682 if (err < 0)
3683 return err;
3684
3685 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003686 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 goto error;
3688
3689 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 return 0;
3691
Takashi Iwai352f7f92012-12-19 12:52:06 +01003692error:
3693 generic_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 return err;
3695}
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003696EXPORT_SYMBOL(snd_hda_parse_generic_codec);