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