blob: d4cb9df6e5b3b44c41e27afb64e6cca2608d72c4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Universal Interface for Intel High Definition Audio Codec
3 *
4 * Generic widget tree parser
5 *
6 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
7 *
8 * This driver is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This driver is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/init.h>
24#include <linux/slab.h>
Paul Gortmakerd81a6d72011-09-22 09:34:58 -040025#include <linux/export.h>
Takashi Iwai352f7f92012-12-19 12:52:06 +010026#include <linux/sort.h>
Takashi Iwaif873e532012-12-20 16:58:39 +010027#include <linux/ctype.h>
28#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <sound/core.h>
Takashi Iwai352f7f92012-12-19 12:52:06 +010030#include <sound/jack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "hda_codec.h"
32#include "hda_local.h"
Takashi Iwai352f7f92012-12-19 12:52:06 +010033#include "hda_auto_parser.h"
34#include "hda_jack.h"
35#include "hda_generic.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Takashi Iwai352f7f92012-12-19 12:52:06 +010038/* initialize hda_gen_spec struct */
39int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
Takashi Iwai352f7f92012-12-19 12:52:06 +010041 snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
42 snd_array_init(&spec->bind_ctls, sizeof(struct hda_bind_ctls *), 8);
43 snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
44 return 0;
45}
46EXPORT_SYMBOL_HDA(snd_hda_gen_spec_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Takashi Iwai12c93df2012-12-19 14:38:33 +010048struct snd_kcontrol_new *
49snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
50 const struct snd_kcontrol_new *temp)
Takashi Iwai352f7f92012-12-19 12:52:06 +010051{
52 struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
53 if (!knew)
54 return NULL;
55 *knew = *temp;
56 if (name)
57 knew->name = kstrdup(name, GFP_KERNEL);
58 else if (knew->name)
59 knew->name = kstrdup(knew->name, GFP_KERNEL);
60 if (!knew->name)
61 return NULL;
62 return knew;
63}
Takashi Iwai12c93df2012-12-19 14:38:33 +010064EXPORT_SYMBOL_HDA(snd_hda_gen_add_kctl);
Takashi Iwai352f7f92012-12-19 12:52:06 +010065
66static void free_kctls(struct hda_gen_spec *spec)
67{
68 if (spec->kctls.list) {
69 struct snd_kcontrol_new *kctl = spec->kctls.list;
70 int i;
71 for (i = 0; i < spec->kctls.used; i++)
72 kfree(kctl[i].name);
73 }
74 snd_array_free(&spec->kctls);
75}
76
77static struct hda_bind_ctls *new_bind_ctl(struct hda_codec *codec,
78 unsigned int nums,
79 struct hda_ctl_ops *ops)
80{
81 struct hda_gen_spec *spec = codec->spec;
82 struct hda_bind_ctls **ctlp, *ctl;
83 ctlp = snd_array_new(&spec->bind_ctls);
84 if (!ctlp)
85 return NULL;
86 ctl = kzalloc(sizeof(*ctl) + sizeof(long) * (nums + 1), GFP_KERNEL);
87 *ctlp = ctl;
88 if (ctl)
89 ctl->ops = ops;
90 return ctl;
91}
92
93static void free_bind_ctls(struct hda_gen_spec *spec)
94{
95 if (spec->bind_ctls.list) {
96 struct hda_bind_ctls **ctl = spec->bind_ctls.list;
97 int i;
98 for (i = 0; i < spec->bind_ctls.used; i++)
99 kfree(ctl[i]);
100 }
101 snd_array_free(&spec->bind_ctls);
102}
103
104void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
105{
106 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 return;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100108 free_kctls(spec);
109 free_bind_ctls(spec);
110 snd_array_free(&spec->paths);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
Takashi Iwai352f7f92012-12-19 12:52:06 +0100112EXPORT_SYMBOL_HDA(snd_hda_gen_spec_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100115 * parsing paths
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Takashi Iwai352f7f92012-12-19 12:52:06 +0100118/* get the path between the given NIDs;
119 * passing 0 to either @pin or @dac behaves as a wildcard
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100121struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
122 hda_nid_t from_nid, hda_nid_t to_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100124 struct hda_gen_spec *spec = codec->spec;
125 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Takashi Iwai352f7f92012-12-19 12:52:06 +0100127 for (i = 0; i < spec->paths.used; i++) {
128 struct nid_path *path = snd_array_elem(&spec->paths, i);
129 if (path->depth <= 0)
130 continue;
131 if ((!from_nid || path->path[0] == from_nid) &&
132 (!to_nid || path->path[path->depth - 1] == to_nid))
133 return path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 }
135 return NULL;
136}
Takashi Iwai352f7f92012-12-19 12:52:06 +0100137EXPORT_SYMBOL_HDA(snd_hda_get_nid_path);
138
139/* check whether the given DAC is already found in any existing paths */
140static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
141{
142 struct hda_gen_spec *spec = codec->spec;
143 int i;
144
145 for (i = 0; i < spec->paths.used; i++) {
146 struct nid_path *path = snd_array_elem(&spec->paths, i);
147 if (path->path[0] == nid)
148 return true;
149 }
150 return false;
151}
152
153/* check whether the given two widgets can be connected */
154static bool is_reachable_path(struct hda_codec *codec,
155 hda_nid_t from_nid, hda_nid_t to_nid)
156{
157 if (!from_nid || !to_nid)
158 return false;
159 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
160}
161
162/* nid, dir and idx */
163#define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
164
165/* check whether the given ctl is already assigned in any path elements */
166static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
167{
168 struct hda_gen_spec *spec = codec->spec;
169 int i;
170
171 val &= AMP_VAL_COMPARE_MASK;
172 for (i = 0; i < spec->paths.used; i++) {
173 struct nid_path *path = snd_array_elem(&spec->paths, i);
174 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
175 return true;
176 }
177 return false;
178}
179
180/* check whether a control with the given (nid, dir, idx) was assigned */
181static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
182 int dir, int idx)
183{
184 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
185 return is_ctl_used(codec, val, NID_PATH_VOL_CTL) ||
186 is_ctl_used(codec, val, NID_PATH_MUTE_CTL);
187}
188
189/* called recursively */
190static bool __parse_nid_path(struct hda_codec *codec,
191 hda_nid_t from_nid, hda_nid_t to_nid,
192 int with_aa_mix, struct nid_path *path, int depth)
193{
194 struct hda_gen_spec *spec = codec->spec;
195 hda_nid_t conn[16];
196 int i, nums;
197
198 if (to_nid == spec->mixer_nid) {
199 if (!with_aa_mix)
200 return false;
201 with_aa_mix = 2; /* mark aa-mix is included */
202 }
203
204 nums = snd_hda_get_connections(codec, to_nid, conn, ARRAY_SIZE(conn));
205 for (i = 0; i < nums; i++) {
206 if (conn[i] != from_nid) {
207 /* special case: when from_nid is 0,
208 * try to find an empty DAC
209 */
210 if (from_nid ||
211 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
212 is_dac_already_used(codec, conn[i]))
213 continue;
214 }
215 /* aa-mix is requested but not included? */
216 if (!(spec->mixer_nid && with_aa_mix == 1))
217 goto found;
218 }
219 if (depth >= MAX_NID_PATH_DEPTH)
220 return false;
221 for (i = 0; i < nums; i++) {
222 unsigned int type;
223 type = get_wcaps_type(get_wcaps(codec, conn[i]));
224 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
225 type == AC_WID_PIN)
226 continue;
227 if (__parse_nid_path(codec, from_nid, conn[i],
228 with_aa_mix, path, depth + 1))
229 goto found;
230 }
231 return false;
232
233 found:
234 path->path[path->depth] = conn[i];
235 path->idx[path->depth + 1] = i;
236 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
237 path->multi[path->depth + 1] = 1;
238 path->depth++;
239 return true;
240}
241
242/* parse the widget path from the given nid to the target nid;
243 * when @from_nid is 0, try to find an empty DAC;
244 * when @with_aa_mix is 0, paths with spec->mixer_nid are excluded.
245 * when @with_aa_mix is 1, paths without spec->mixer_nid are excluded.
246 * when @with_aa_mix is 2, no special handling about spec->mixer_nid.
247 */
248bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
249 hda_nid_t to_nid, int with_aa_mix,
250 struct nid_path *path)
251{
252 if (__parse_nid_path(codec, from_nid, to_nid, with_aa_mix, path, 1)) {
253 path->path[path->depth] = to_nid;
254 path->depth++;
255#if 0
256 snd_printdd("path: depth=%d, %02x/%02x/%02x/%02x/%02x\n",
257 path->depth, path->path[0], path->path[1],
258 path->path[2], path->path[3], path->path[4]);
259#endif
260 return true;
261 }
262 return false;
263}
264EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100267 * parse the path between the given NIDs and add to the path list.
268 * if no valid path is found, return NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100270struct nid_path *
271snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
272 hda_nid_t to_nid, int with_aa_mix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100274 struct hda_gen_spec *spec = codec->spec;
275 struct nid_path *path;
276
277 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
278 return NULL;
279
280 path = snd_array_new(&spec->paths);
281 if (!path)
282 return NULL;
283 memset(path, 0, sizeof(*path));
284 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, with_aa_mix, path))
285 return path;
286 /* push back */
287 spec->paths.used--;
288 return NULL;
289}
290EXPORT_SYMBOL_HDA(snd_hda_add_new_path);
291
292/* look for an empty DAC slot */
293static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
294 bool is_digital)
295{
296 struct hda_gen_spec *spec = codec->spec;
297 bool cap_digital;
298 int i;
299
300 for (i = 0; i < spec->num_all_dacs; i++) {
301 hda_nid_t nid = spec->all_dacs[i];
302 if (!nid || is_dac_already_used(codec, nid))
303 continue;
304 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
305 if (is_digital != cap_digital)
306 continue;
307 if (is_reachable_path(codec, nid, pin))
308 return nid;
309 }
310 return 0;
311}
312
313/* replace the channels in the composed amp value with the given number */
314static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
315{
316 val &= ~(0x3U << 16);
317 val |= chs << 16;
318 return val;
319}
320
321/* check whether the widget has the given amp capability for the direction */
322static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
323 int dir, unsigned int bits)
324{
325 if (!nid)
326 return false;
327 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
328 if (query_amp_caps(codec, nid, dir) & bits)
329 return true;
330 return false;
331}
332
333#define nid_has_mute(codec, nid, dir) \
334 check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
335#define nid_has_volume(codec, nid, dir) \
336 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
337
338/* look for a widget suitable for assigning a mute switch in the path */
339static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
340 struct nid_path *path)
341{
342 int i;
343
344 for (i = path->depth - 1; i >= 0; i--) {
345 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
346 return path->path[i];
347 if (i != path->depth - 1 && i != 0 &&
348 nid_has_mute(codec, path->path[i], HDA_INPUT))
349 return path->path[i];
350 }
351 return 0;
352}
353
354/* look for a widget suitable for assigning a volume ctl in the path */
355static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
356 struct nid_path *path)
357{
358 int i;
359
360 for (i = path->depth - 1; i >= 0; i--) {
361 if (nid_has_volume(codec, path->path[i], HDA_OUTPUT))
362 return path->path[i];
363 }
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200364 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365}
366
367/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100368 * path activation / deactivation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100370
371/* can have the amp-in capability? */
372static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100374 hda_nid_t nid = path->path[idx];
375 unsigned int caps = get_wcaps(codec, nid);
376 unsigned int type = get_wcaps_type(caps);
377
378 if (!(caps & AC_WCAP_IN_AMP))
379 return false;
380 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
381 return false;
382 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
384
Takashi Iwai352f7f92012-12-19 12:52:06 +0100385/* can have the amp-out capability? */
386static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100388 hda_nid_t nid = path->path[idx];
389 unsigned int caps = get_wcaps(codec, nid);
390 unsigned int type = get_wcaps_type(caps);
391
392 if (!(caps & AC_WCAP_OUT_AMP))
393 return false;
394 if (type == AC_WID_PIN && !idx) /* only for output pins */
395 return false;
396 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397}
398
Takashi Iwai352f7f92012-12-19 12:52:06 +0100399/* check whether the given (nid,dir,idx) is active */
400static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
401 unsigned int idx, unsigned int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100403 struct hda_gen_spec *spec = codec->spec;
404 int i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Takashi Iwai352f7f92012-12-19 12:52:06 +0100406 for (n = 0; n < spec->paths.used; n++) {
407 struct nid_path *path = snd_array_elem(&spec->paths, n);
408 if (!path->active)
409 continue;
410 for (i = 0; i < path->depth; i++) {
411 if (path->path[i] == nid) {
412 if (dir == HDA_OUTPUT || path->idx[i] == idx)
413 return true;
414 break;
415 }
416 }
417 }
418 return false;
419}
420
421/* get the default amp value for the target state */
422static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
423 int dir, bool enable)
424{
425 unsigned int caps;
426 unsigned int val = 0;
427
428 caps = query_amp_caps(codec, nid, dir);
429 if (caps & AC_AMPCAP_NUM_STEPS) {
430 /* set to 0dB */
431 if (enable)
432 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
433 }
434 if (caps & AC_AMPCAP_MUTE) {
435 if (!enable)
436 val |= HDA_AMP_MUTE;
437 }
438 return val;
439}
440
441/* initialize the amp value (only at the first time) */
442static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
443{
444 int val = get_amp_val_to_activate(codec, nid, dir, false);
445 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
446}
447
448static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
449 int idx, bool enable)
450{
451 int val;
452 if (is_ctl_associated(codec, nid, dir, idx) ||
453 is_active_nid(codec, nid, dir, idx))
454 return;
455 val = get_amp_val_to_activate(codec, nid, dir, enable);
456 snd_hda_codec_amp_stereo(codec, nid, dir, idx, 0xff, val);
457}
458
459static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
460 int i, bool enable)
461{
462 hda_nid_t nid = path->path[i];
463 init_amp(codec, nid, HDA_OUTPUT, 0);
464 activate_amp(codec, nid, HDA_OUTPUT, 0, enable);
465}
466
467static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
468 int i, bool enable, bool add_aamix)
469{
470 struct hda_gen_spec *spec = codec->spec;
471 hda_nid_t conn[16];
472 int n, nums, idx;
473 int type;
474 hda_nid_t nid = path->path[i];
475
476 nums = snd_hda_get_connections(codec, nid, conn, ARRAY_SIZE(conn));
477 type = get_wcaps_type(get_wcaps(codec, nid));
478 if (type == AC_WID_PIN ||
479 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
480 nums = 1;
481 idx = 0;
482 } else
483 idx = path->idx[i];
484
485 for (n = 0; n < nums; n++)
486 init_amp(codec, nid, HDA_INPUT, n);
487
488 if (is_ctl_associated(codec, nid, HDA_INPUT, idx))
489 return;
490
491 /* here is a little bit tricky in comparison with activate_amp_out();
492 * when aa-mixer is available, we need to enable the path as well
493 */
494 for (n = 0; n < nums; n++) {
495 if (n != idx && (!add_aamix || conn[n] != spec->mixer_nid))
496 continue;
497 activate_amp(codec, nid, HDA_INPUT, n, enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 }
499}
500
Takashi Iwai352f7f92012-12-19 12:52:06 +0100501/* activate or deactivate the given path
502 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100504void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
505 bool enable, bool add_aamix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100507 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Takashi Iwai352f7f92012-12-19 12:52:06 +0100509 if (!enable)
510 path->active = false;
511
512 for (i = path->depth - 1; i >= 0; i--) {
513 if (enable && path->multi[i])
514 snd_hda_codec_write_cache(codec, path->path[i], 0,
515 AC_VERB_SET_CONNECT_SEL,
516 path->idx[i]);
517 if (has_amp_in(codec, path, i))
518 activate_amp_in(codec, path, i, enable, add_aamix);
519 if (has_amp_out(codec, path, i))
520 activate_amp_out(codec, path, i, enable);
521 }
522
523 if (enable)
524 path->active = true;
525}
526EXPORT_SYMBOL_HDA(snd_hda_activate_path);
527
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100528/* turn on/off EAPD on the given pin */
529static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
530{
531 struct hda_gen_spec *spec = codec->spec;
532 if (spec->own_eapd_ctl ||
533 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
534 return;
535 snd_hda_codec_update_cache(codec, pin, 0,
536 AC_VERB_SET_EAPD_BTLENABLE,
537 enable ? 0x02 : 0x00);
538}
539
Takashi Iwai352f7f92012-12-19 12:52:06 +0100540
541/*
542 * Helper functions for creating mixer ctl elements
543 */
544
545enum {
546 HDA_CTL_WIDGET_VOL,
547 HDA_CTL_WIDGET_MUTE,
548 HDA_CTL_BIND_MUTE,
549 HDA_CTL_BIND_VOL,
550 HDA_CTL_BIND_SW,
551};
552static const struct snd_kcontrol_new control_templates[] = {
553 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
554 HDA_CODEC_MUTE(NULL, 0, 0, 0),
555 HDA_BIND_MUTE(NULL, 0, 0, 0),
556 HDA_BIND_VOL(NULL, 0),
557 HDA_BIND_SW(NULL, 0),
558};
559
560/* add dynamic controls from template */
561static int add_control(struct hda_gen_spec *spec, int type, const char *name,
562 int cidx, unsigned long val)
563{
564 struct snd_kcontrol_new *knew;
565
Takashi Iwai12c93df2012-12-19 14:38:33 +0100566 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100567 if (!knew)
568 return -ENOMEM;
569 knew->index = cidx;
570 if (get_amp_nid_(val))
571 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
572 knew->private_value = val;
573 return 0;
574}
575
576static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
577 const char *pfx, const char *dir,
578 const char *sfx, int cidx, unsigned long val)
579{
580 char name[32];
581 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
582 return add_control(spec, type, name, cidx, val);
583}
584
585#define add_pb_vol_ctrl(spec, type, pfx, val) \
586 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
587#define add_pb_sw_ctrl(spec, type, pfx, val) \
588 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
589#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
590 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
591#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
592 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
593
594static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
595 unsigned int chs, struct nid_path *path)
596{
597 unsigned int val;
598 if (!path)
599 return 0;
600 val = path->ctls[NID_PATH_VOL_CTL];
601 if (!val)
602 return 0;
603 val = amp_val_replace_channels(val, chs);
604 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
605}
606
607/* return the channel bits suitable for the given path->ctls[] */
608static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
609 int type)
610{
611 int chs = 1; /* mono (left only) */
612 if (path) {
613 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
614 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
615 chs = 3; /* stereo */
616 }
617 return chs;
618}
619
620static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
621 struct nid_path *path)
622{
623 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
624 return add_vol_ctl(codec, pfx, cidx, chs, path);
625}
626
627/* create a mute-switch for the given mixer widget;
628 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
629 */
630static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
631 unsigned int chs, struct nid_path *path)
632{
633 unsigned int val;
634 int type = HDA_CTL_WIDGET_MUTE;
635
636 if (!path)
637 return 0;
638 val = path->ctls[NID_PATH_MUTE_CTL];
639 if (!val)
640 return 0;
641 val = amp_val_replace_channels(val, chs);
642 if (get_amp_direction_(val) == HDA_INPUT) {
643 hda_nid_t nid = get_amp_nid_(val);
644 int nums = snd_hda_get_num_conns(codec, nid);
645 if (nums > 1) {
646 type = HDA_CTL_BIND_MUTE;
647 val |= nums << 19;
648 }
649 }
650 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
651}
652
653static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
654 int cidx, struct nid_path *path)
655{
656 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
657 return add_sw_ctl(codec, pfx, cidx, chs, path);
658}
659
660static const char * const channel_name[4] = {
661 "Front", "Surround", "CLFE", "Side"
662};
663
664/* give some appropriate ctl name prefix for the given line out channel */
665static const char *get_line_out_pfx(struct hda_gen_spec *spec, int ch,
666 bool can_be_master, int *index)
667{
668 struct auto_pin_cfg *cfg = &spec->autocfg;
669
670 *index = 0;
671 if (cfg->line_outs == 1 && !spec->multi_ios &&
672 !cfg->hp_outs && !cfg->speaker_outs && can_be_master)
673 return spec->vmaster_mute.hook ? "PCM" : "Master";
674
675 /* if there is really a single DAC used in the whole output paths,
676 * use it master (or "PCM" if a vmaster hook is present)
677 */
678 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
679 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
680 return spec->vmaster_mute.hook ? "PCM" : "Master";
681
682 switch (cfg->line_out_type) {
683 case AUTO_PIN_SPEAKER_OUT:
684 if (cfg->line_outs == 1)
685 return "Speaker";
686 if (cfg->line_outs == 2)
687 return ch ? "Bass Speaker" : "Speaker";
688 break;
689 case AUTO_PIN_HP_OUT:
690 /* for multi-io case, only the primary out */
691 if (ch && spec->multi_ios)
692 break;
693 *index = ch;
694 return "Headphone";
695 default:
696 if (cfg->line_outs == 1 && !spec->multi_ios)
697 return "PCM";
698 break;
699 }
700 if (ch >= ARRAY_SIZE(channel_name)) {
701 snd_BUG();
702 return "PCM";
703 }
704
705 return channel_name[ch];
706}
707
708/*
709 * Parse output paths
710 */
711
712/* badness definition */
713enum {
714 /* No primary DAC is found for the main output */
715 BAD_NO_PRIMARY_DAC = 0x10000,
716 /* No DAC is found for the extra output */
717 BAD_NO_DAC = 0x4000,
718 /* No possible multi-ios */
719 BAD_MULTI_IO = 0x103,
720 /* No individual DAC for extra output */
721 BAD_NO_EXTRA_DAC = 0x102,
722 /* No individual DAC for extra surrounds */
723 BAD_NO_EXTRA_SURR_DAC = 0x101,
724 /* Primary DAC shared with main surrounds */
725 BAD_SHARED_SURROUND = 0x100,
726 /* Primary DAC shared with main CLFE */
727 BAD_SHARED_CLFE = 0x10,
728 /* Primary DAC shared with extra surrounds */
729 BAD_SHARED_EXTRA_SURROUND = 0x10,
730 /* Volume widget is shared */
731 BAD_SHARED_VOL = 0x10,
732};
733
734/* look for widgets in the path between the given NIDs appropriate for
735 * volume and mute controls, and assign the values to ctls[].
736 *
737 * When no appropriate widget is found in the path, the badness value
738 * is incremented depending on the situation. The function returns the
739 * total badness for both volume and mute controls.
740 */
741static int assign_out_path_ctls(struct hda_codec *codec, hda_nid_t pin,
742 hda_nid_t dac)
743{
744 struct nid_path *path = snd_hda_get_nid_path(codec, dac, pin);
745 hda_nid_t nid;
746 unsigned int val;
747 int badness = 0;
748
749 if (!path)
750 return BAD_SHARED_VOL * 2;
751 nid = look_for_out_vol_nid(codec, path);
752 if (nid) {
753 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
754 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
755 badness += BAD_SHARED_VOL;
756 else
757 path->ctls[NID_PATH_VOL_CTL] = val;
758 } else
759 badness += BAD_SHARED_VOL;
760 nid = look_for_out_mute_nid(codec, path);
761 if (nid) {
762 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
763 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
764 nid_has_mute(codec, nid, HDA_OUTPUT))
765 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
766 else
767 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
768 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
769 badness += BAD_SHARED_VOL;
770 else
771 path->ctls[NID_PATH_MUTE_CTL] = val;
772 } else
773 badness += BAD_SHARED_VOL;
774 return badness;
775}
776
777struct badness_table {
778 int no_primary_dac; /* no primary DAC */
779 int no_dac; /* no secondary DACs */
780 int shared_primary; /* primary DAC is shared with main output */
781 int shared_surr; /* secondary DAC shared with main or primary */
782 int shared_clfe; /* third DAC shared with main or primary */
783 int shared_surr_main; /* secondary DAC sahred with main/DAC0 */
784};
785
786static struct badness_table main_out_badness = {
787 .no_primary_dac = BAD_NO_PRIMARY_DAC,
788 .no_dac = BAD_NO_DAC,
789 .shared_primary = BAD_NO_PRIMARY_DAC,
790 .shared_surr = BAD_SHARED_SURROUND,
791 .shared_clfe = BAD_SHARED_CLFE,
792 .shared_surr_main = BAD_SHARED_SURROUND,
793};
794
795static struct badness_table extra_out_badness = {
796 .no_primary_dac = BAD_NO_DAC,
797 .no_dac = BAD_NO_DAC,
798 .shared_primary = BAD_NO_EXTRA_DAC,
799 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
800 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
801 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
802};
803
804/* try to assign DACs to pins and return the resultant badness */
805static int try_assign_dacs(struct hda_codec *codec, int num_outs,
806 const hda_nid_t *pins, hda_nid_t *dacs,
807 const struct badness_table *bad)
808{
809 struct hda_gen_spec *spec = codec->spec;
810 struct auto_pin_cfg *cfg = &spec->autocfg;
811 int i, j;
812 int badness = 0;
813 hda_nid_t dac;
814
815 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 return 0;
817
Takashi Iwai352f7f92012-12-19 12:52:06 +0100818 for (i = 0; i < num_outs; i++) {
819 hda_nid_t pin = pins[i];
820 if (!dacs[i])
821 dacs[i] = look_for_dac(codec, pin, false);
822 if (!dacs[i] && !i) {
823 for (j = 1; j < num_outs; j++) {
824 if (is_reachable_path(codec, dacs[j], pin)) {
825 dacs[0] = dacs[j];
826 dacs[j] = 0;
827 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 }
829 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100830 }
831 dac = dacs[i];
832 if (!dac) {
833 if (is_reachable_path(codec, dacs[0], pin))
834 dac = dacs[0];
835 else if (cfg->line_outs > i &&
836 is_reachable_path(codec, spec->private_dac_nids[i], pin))
837 dac = spec->private_dac_nids[i];
838 if (dac) {
839 if (!i)
840 badness += bad->shared_primary;
841 else if (i == 1)
842 badness += bad->shared_surr;
843 else
844 badness += bad->shared_clfe;
845 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
846 dac = spec->private_dac_nids[0];
847 badness += bad->shared_surr_main;
848 } else if (!i)
849 badness += bad->no_primary_dac;
850 else
851 badness += bad->no_dac;
852 }
853 if (!snd_hda_add_new_path(codec, dac, pin, 0))
854 dac = dacs[i] = 0;
855 if (dac)
856 badness += assign_out_path_ctls(codec, pin, dac);
857 }
858
859 return badness;
860}
861
862/* return NID if the given pin has only a single connection to a certain DAC */
863static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
864{
865 struct hda_gen_spec *spec = codec->spec;
866 int i;
867 hda_nid_t nid_found = 0;
868
869 for (i = 0; i < spec->num_all_dacs; i++) {
870 hda_nid_t nid = spec->all_dacs[i];
871 if (!nid || is_dac_already_used(codec, nid))
872 continue;
873 if (is_reachable_path(codec, nid, pin)) {
874 if (nid_found)
875 return 0;
876 nid_found = nid;
877 }
878 }
879 return nid_found;
880}
881
882/* check whether the given pin can be a multi-io pin */
883static bool can_be_multiio_pin(struct hda_codec *codec,
884 unsigned int location, hda_nid_t nid)
885{
886 unsigned int defcfg, caps;
887
888 defcfg = snd_hda_codec_get_pincfg(codec, nid);
889 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
890 return false;
891 if (location && get_defcfg_location(defcfg) != location)
892 return false;
893 caps = snd_hda_query_pin_caps(codec, nid);
894 if (!(caps & AC_PINCAP_OUT))
895 return false;
896 return true;
897}
898
899/*
900 * multi-io helper
901 *
902 * When hardwired is set, try to fill ony hardwired pins, and returns
903 * zero if any pins are filled, non-zero if nothing found.
904 * When hardwired is off, try to fill possible input pins, and returns
905 * the badness value.
906 */
907static int fill_multi_ios(struct hda_codec *codec,
908 hda_nid_t reference_pin,
909 bool hardwired, int offset)
910{
911 struct hda_gen_spec *spec = codec->spec;
912 struct auto_pin_cfg *cfg = &spec->autocfg;
913 int type, i, j, dacs, num_pins, old_pins;
914 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
915 unsigned int location = get_defcfg_location(defcfg);
916 int badness = 0;
917
918 old_pins = spec->multi_ios;
919 if (old_pins >= 2)
920 goto end_fill;
921
922 num_pins = 0;
923 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
924 for (i = 0; i < cfg->num_inputs; i++) {
925 if (cfg->inputs[i].type != type)
926 continue;
927 if (can_be_multiio_pin(codec, location,
928 cfg->inputs[i].pin))
929 num_pins++;
930 }
931 }
932 if (num_pins < 2)
933 goto end_fill;
934
935 dacs = spec->multiout.num_dacs;
936 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
937 for (i = 0; i < cfg->num_inputs; i++) {
938 hda_nid_t nid = cfg->inputs[i].pin;
939 hda_nid_t dac = 0;
940
941 if (cfg->inputs[i].type != type)
942 continue;
943 if (!can_be_multiio_pin(codec, location, nid))
944 continue;
945 for (j = 0; j < spec->multi_ios; j++) {
946 if (nid == spec->multi_io[j].pin)
947 break;
948 }
949 if (j < spec->multi_ios)
950 continue;
951
952 if (offset && offset + spec->multi_ios < dacs) {
953 dac = spec->private_dac_nids[offset + spec->multi_ios];
954 if (!is_reachable_path(codec, dac, nid))
955 dac = 0;
956 }
957 if (hardwired)
958 dac = get_dac_if_single(codec, nid);
959 else if (!dac)
960 dac = look_for_dac(codec, nid, false);
961 if (!dac) {
962 badness++;
963 continue;
964 }
965 if (!snd_hda_add_new_path(codec, dac, nid, 0)) {
966 badness++;
967 continue;
968 }
969 spec->multi_io[spec->multi_ios].pin = nid;
970 spec->multi_io[spec->multi_ios].dac = dac;
971 spec->multi_ios++;
972 if (spec->multi_ios >= 2)
973 break;
974 }
975 }
976 end_fill:
977 if (badness)
978 badness = BAD_MULTI_IO;
979 if (old_pins == spec->multi_ios) {
980 if (hardwired)
981 return 1; /* nothing found */
982 else
983 return badness; /* no badness if nothing found */
984 }
985 if (!hardwired && spec->multi_ios < 2) {
986 /* cancel newly assigned paths */
987 spec->paths.used -= spec->multi_ios - old_pins;
988 spec->multi_ios = old_pins;
989 return badness;
990 }
991
992 /* assign volume and mute controls */
993 for (i = old_pins; i < spec->multi_ios; i++)
994 badness += assign_out_path_ctls(codec, spec->multi_io[i].pin,
995 spec->multi_io[i].dac);
996
997 return badness;
998}
999
1000/* map DACs for all pins in the list if they are single connections */
1001static bool map_singles(struct hda_codec *codec, int outs,
1002 const hda_nid_t *pins, hda_nid_t *dacs)
1003{
1004 int i;
1005 bool found = false;
1006 for (i = 0; i < outs; i++) {
1007 hda_nid_t dac;
1008 if (dacs[i])
1009 continue;
1010 dac = get_dac_if_single(codec, pins[i]);
1011 if (!dac)
1012 continue;
1013 if (snd_hda_add_new_path(codec, dac, pins[i], 0)) {
1014 dacs[i] = dac;
1015 found = true;
1016 }
1017 }
1018 return found;
1019}
1020
1021/* fill in the dac_nids table from the parsed pin configuration */
1022static int fill_and_eval_dacs(struct hda_codec *codec,
1023 bool fill_hardwired,
1024 bool fill_mio_first)
1025{
1026 struct hda_gen_spec *spec = codec->spec;
1027 struct auto_pin_cfg *cfg = &spec->autocfg;
1028 int i, err, badness;
1029
1030 /* set num_dacs once to full for look_for_dac() */
1031 spec->multiout.num_dacs = cfg->line_outs;
1032 spec->multiout.dac_nids = spec->private_dac_nids;
1033 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1034 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1035 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1036 spec->multi_ios = 0;
1037 snd_array_free(&spec->paths);
1038 badness = 0;
1039
1040 /* fill hard-wired DACs first */
1041 if (fill_hardwired) {
1042 bool mapped;
1043 do {
1044 mapped = map_singles(codec, cfg->line_outs,
1045 cfg->line_out_pins,
1046 spec->private_dac_nids);
1047 mapped |= map_singles(codec, cfg->hp_outs,
1048 cfg->hp_pins,
1049 spec->multiout.hp_out_nid);
1050 mapped |= map_singles(codec, cfg->speaker_outs,
1051 cfg->speaker_pins,
1052 spec->multiout.extra_out_nid);
1053 if (fill_mio_first && cfg->line_outs == 1 &&
1054 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1055 err = fill_multi_ios(codec, cfg->line_out_pins[0], true, 0);
1056 if (!err)
1057 mapped = true;
1058 }
1059 } while (mapped);
1060 }
1061
1062 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
1063 spec->private_dac_nids,
1064 &main_out_badness);
1065
1066 /* re-count num_dacs and squash invalid entries */
1067 spec->multiout.num_dacs = 0;
1068 for (i = 0; i < cfg->line_outs; i++) {
1069 if (spec->private_dac_nids[i])
1070 spec->multiout.num_dacs++;
1071 else {
1072 memmove(spec->private_dac_nids + i,
1073 spec->private_dac_nids + i + 1,
1074 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1075 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1076 }
1077 }
1078
1079 if (fill_mio_first &&
1080 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1081 /* try to fill multi-io first */
1082 err = fill_multi_ios(codec, cfg->line_out_pins[0], false, 0);
1083 if (err < 0)
1084 return err;
1085 /* we don't count badness at this stage yet */
1086 }
1087
1088 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1089 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1090 spec->multiout.hp_out_nid,
1091 &extra_out_badness);
1092 if (err < 0)
1093 return err;
1094 badness += err;
1095 }
1096 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1097 err = try_assign_dacs(codec, cfg->speaker_outs,
1098 cfg->speaker_pins,
1099 spec->multiout.extra_out_nid,
1100 &extra_out_badness);
1101 if (err < 0)
1102 return err;
1103 badness += err;
1104 }
1105 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1106 err = fill_multi_ios(codec, cfg->line_out_pins[0], false, 0);
1107 if (err < 0)
1108 return err;
1109 badness += err;
1110 }
1111 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1112 /* try multi-ios with HP + inputs */
1113 int offset = 0;
1114 if (cfg->line_outs >= 3)
1115 offset = 1;
1116 err = fill_multi_ios(codec, cfg->hp_pins[0], false, offset);
1117 if (err < 0)
1118 return err;
1119 badness += err;
1120 }
1121
1122 if (spec->multi_ios == 2) {
1123 for (i = 0; i < 2; i++)
1124 spec->private_dac_nids[spec->multiout.num_dacs++] =
1125 spec->multi_io[i].dac;
1126 spec->ext_channel_count = 2;
1127 } else if (spec->multi_ios) {
1128 spec->multi_ios = 0;
1129 badness += BAD_MULTI_IO;
1130 }
1131
1132 return badness;
1133}
1134
1135#define DEBUG_BADNESS
1136
1137#ifdef DEBUG_BADNESS
1138#define debug_badness snd_printdd
1139#else
1140#define debug_badness(...)
1141#endif
1142
1143static void debug_show_configs(struct hda_gen_spec *spec, struct auto_pin_cfg *cfg)
1144{
1145 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1146 cfg->line_out_pins[0], cfg->line_out_pins[1],
1147 cfg->line_out_pins[2], cfg->line_out_pins[2],
1148 spec->multiout.dac_nids[0],
1149 spec->multiout.dac_nids[1],
1150 spec->multiout.dac_nids[2],
1151 spec->multiout.dac_nids[3]);
1152 if (spec->multi_ios > 0)
1153 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1154 spec->multi_ios,
1155 spec->multi_io[0].pin, spec->multi_io[1].pin,
1156 spec->multi_io[0].dac, spec->multi_io[1].dac);
1157 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1158 cfg->hp_pins[0], cfg->hp_pins[1],
1159 cfg->hp_pins[2], cfg->hp_pins[2],
1160 spec->multiout.hp_out_nid[0],
1161 spec->multiout.hp_out_nid[1],
1162 spec->multiout.hp_out_nid[2],
1163 spec->multiout.hp_out_nid[3]);
1164 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1165 cfg->speaker_pins[0], cfg->speaker_pins[1],
1166 cfg->speaker_pins[2], cfg->speaker_pins[3],
1167 spec->multiout.extra_out_nid[0],
1168 spec->multiout.extra_out_nid[1],
1169 spec->multiout.extra_out_nid[2],
1170 spec->multiout.extra_out_nid[3]);
1171}
1172
1173/* find all available DACs of the codec */
1174static void fill_all_dac_nids(struct hda_codec *codec)
1175{
1176 struct hda_gen_spec *spec = codec->spec;
1177 int i;
1178 hda_nid_t nid = codec->start_nid;
1179
1180 spec->num_all_dacs = 0;
1181 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1182 for (i = 0; i < codec->num_nodes; i++, nid++) {
1183 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1184 continue;
1185 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1186 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1187 break;
1188 }
1189 spec->all_dacs[spec->num_all_dacs++] = nid;
1190 }
1191}
1192
1193static int parse_output_paths(struct hda_codec *codec)
1194{
1195 struct hda_gen_spec *spec = codec->spec;
1196 struct auto_pin_cfg *cfg = &spec->autocfg;
1197 struct auto_pin_cfg *best_cfg;
1198 int best_badness = INT_MAX;
1199 int badness;
1200 bool fill_hardwired = true, fill_mio_first = true;
1201 bool best_wired = true, best_mio = true;
1202 bool hp_spk_swapped = false;
1203
1204 fill_all_dac_nids(codec);
1205
1206 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1207 if (!best_cfg)
1208 return -ENOMEM;
1209 *best_cfg = *cfg;
1210
1211 for (;;) {
1212 badness = fill_and_eval_dacs(codec, fill_hardwired,
1213 fill_mio_first);
1214 if (badness < 0) {
1215 kfree(best_cfg);
1216 return badness;
1217 }
1218 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1219 cfg->line_out_type, fill_hardwired, fill_mio_first,
1220 badness);
1221 debug_show_configs(spec, cfg);
1222 if (badness < best_badness) {
1223 best_badness = badness;
1224 *best_cfg = *cfg;
1225 best_wired = fill_hardwired;
1226 best_mio = fill_mio_first;
1227 }
1228 if (!badness)
1229 break;
1230 fill_mio_first = !fill_mio_first;
1231 if (!fill_mio_first)
1232 continue;
1233 fill_hardwired = !fill_hardwired;
1234 if (!fill_hardwired)
1235 continue;
1236 if (hp_spk_swapped)
1237 break;
1238 hp_spk_swapped = true;
1239 if (cfg->speaker_outs > 0 &&
1240 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1241 cfg->hp_outs = cfg->line_outs;
1242 memcpy(cfg->hp_pins, cfg->line_out_pins,
1243 sizeof(cfg->hp_pins));
1244 cfg->line_outs = cfg->speaker_outs;
1245 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1246 sizeof(cfg->speaker_pins));
1247 cfg->speaker_outs = 0;
1248 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1249 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1250 fill_hardwired = true;
1251 continue;
1252 }
1253 if (cfg->hp_outs > 0 &&
1254 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1255 cfg->speaker_outs = cfg->line_outs;
1256 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1257 sizeof(cfg->speaker_pins));
1258 cfg->line_outs = cfg->hp_outs;
1259 memcpy(cfg->line_out_pins, cfg->hp_pins,
1260 sizeof(cfg->hp_pins));
1261 cfg->hp_outs = 0;
1262 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1263 cfg->line_out_type = AUTO_PIN_HP_OUT;
1264 fill_hardwired = true;
1265 continue;
1266 }
1267 break;
1268 }
1269
1270 if (badness) {
1271 *cfg = *best_cfg;
1272 fill_and_eval_dacs(codec, best_wired, best_mio);
1273 }
1274 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1275 cfg->line_out_type, best_wired, best_mio);
1276 debug_show_configs(spec, cfg);
1277
1278 if (cfg->line_out_pins[0]) {
1279 struct nid_path *path;
1280 path = snd_hda_get_nid_path(codec,
1281 spec->multiout.dac_nids[0],
1282 cfg->line_out_pins[0]);
1283 if (path)
1284 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
1285 }
1286
1287 kfree(best_cfg);
1288 return 0;
1289}
1290
1291/* add playback controls from the parsed DAC table */
1292static int create_multi_out_ctls(struct hda_codec *codec,
1293 const struct auto_pin_cfg *cfg)
1294{
1295 struct hda_gen_spec *spec = codec->spec;
1296 int i, err, noutputs;
1297
1298 noutputs = cfg->line_outs;
1299 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1300 noutputs += spec->multi_ios;
1301
1302 for (i = 0; i < noutputs; i++) {
1303 const char *name;
1304 int index;
1305 hda_nid_t dac, pin;
1306 struct nid_path *path;
1307
1308 dac = spec->multiout.dac_nids[i];
1309 if (!dac)
1310 continue;
1311 if (i >= cfg->line_outs) {
1312 pin = spec->multi_io[i - 1].pin;
1313 index = 0;
1314 name = channel_name[i];
1315 } else {
1316 pin = cfg->line_out_pins[i];
1317 name = get_line_out_pfx(spec, i, true, &index);
1318 }
1319
1320 path = snd_hda_get_nid_path(codec, dac, pin);
1321 if (!path)
1322 continue;
1323 if (!name || !strcmp(name, "CLFE")) {
1324 /* Center/LFE */
1325 err = add_vol_ctl(codec, "Center", 0, 1, path);
1326 if (err < 0)
1327 return err;
1328 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1329 if (err < 0)
1330 return err;
1331 err = add_sw_ctl(codec, "Center", 0, 1, path);
1332 if (err < 0)
1333 return err;
1334 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1335 if (err < 0)
1336 return err;
1337 } else {
1338 err = add_stereo_vol(codec, name, index, path);
1339 if (err < 0)
1340 return err;
1341 err = add_stereo_sw(codec, name, index, path);
1342 if (err < 0)
1343 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 }
1345 }
1346 return 0;
1347}
1348
Takashi Iwai352f7f92012-12-19 12:52:06 +01001349static int create_extra_out(struct hda_codec *codec, hda_nid_t pin,
1350 hda_nid_t dac, const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001352 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 int err;
1354
Takashi Iwai352f7f92012-12-19 12:52:06 +01001355 path = snd_hda_get_nid_path(codec, dac, pin);
1356 if (!path)
1357 return 0;
1358 /* bind volume control will be created in the case of dac = 0 */
1359 if (dac) {
1360 err = add_stereo_vol(codec, pfx, cidx, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 if (err < 0)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001362 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001364 err = add_stereo_sw(codec, pfx, cidx, path);
1365 if (err < 0)
1366 return err;
1367 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368}
1369
Takashi Iwai352f7f92012-12-19 12:52:06 +01001370/* add playback controls for speaker and HP outputs */
1371static int create_extra_outs(struct hda_codec *codec, int num_pins,
1372 const hda_nid_t *pins, const hda_nid_t *dacs,
1373 const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001375 struct hda_gen_spec *spec = codec->spec;
1376 struct hda_bind_ctls *ctl;
1377 char name[32];
1378 int i, n, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
Takashi Iwai352f7f92012-12-19 12:52:06 +01001380 if (!num_pins || !pins[0])
1381 return 0;
1382
1383 if (num_pins == 1) {
1384 hda_nid_t dac = *dacs;
1385 if (!dac)
1386 dac = spec->multiout.dac_nids[0];
1387 return create_extra_out(codec, *pins, dac, pfx, 0);
Takashi Iwai97ec5582006-03-21 11:29:07 +01001388 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001389
1390 for (i = 0; i < num_pins; i++) {
1391 hda_nid_t dac;
1392 if (dacs[num_pins - 1])
1393 dac = dacs[i]; /* with individual volumes */
Takashi Iwai97ec5582006-03-21 11:29:07 +01001394 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01001395 dac = 0;
1396 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker")) {
1397 err = create_extra_out(codec, pins[i], dac,
1398 "Bass Speaker", 0);
1399 } else if (num_pins >= 3) {
1400 snprintf(name, sizeof(name), "%s %s",
1401 pfx, channel_name[i]);
1402 err = create_extra_out(codec, pins[i], dac, name, 0);
1403 } else {
1404 err = create_extra_out(codec, pins[i], dac, pfx, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001406 if (err < 0)
1407 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001409 if (dacs[num_pins - 1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 return 0;
1411
Takashi Iwai352f7f92012-12-19 12:52:06 +01001412 /* Let's create a bind-controls for volumes */
1413 ctl = new_bind_ctl(codec, num_pins, &snd_hda_bind_vol);
1414 if (!ctl)
1415 return -ENOMEM;
1416 n = 0;
1417 for (i = 0; i < num_pins; i++) {
1418 hda_nid_t vol;
1419 struct nid_path *path;
1420 if (!pins[i] || !dacs[i])
1421 continue;
1422 path = snd_hda_get_nid_path(codec, dacs[i], pins[i]);
1423 if (!path)
1424 continue;
1425 vol = look_for_out_vol_nid(codec, path);
1426 if (vol)
1427 ctl->values[n++] =
1428 HDA_COMPOSE_AMP_VAL(vol, 3, 0, HDA_OUTPUT);
1429 }
1430 if (n) {
1431 snprintf(name, sizeof(name), "%s Playback Volume", pfx);
1432 err = add_control(spec, HDA_CTL_BIND_VOL, name, 0, (long)ctl);
1433 if (err < 0)
1434 return err;
1435 }
1436 return 0;
1437}
Takashi Iwai97ec5582006-03-21 11:29:07 +01001438
Takashi Iwai352f7f92012-12-19 12:52:06 +01001439static int create_hp_out_ctls(struct hda_codec *codec)
1440{
1441 struct hda_gen_spec *spec = codec->spec;
1442 return create_extra_outs(codec, spec->autocfg.hp_outs,
1443 spec->autocfg.hp_pins,
1444 spec->multiout.hp_out_nid,
1445 "Headphone");
1446}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Takashi Iwai352f7f92012-12-19 12:52:06 +01001448static int create_speaker_out_ctls(struct hda_codec *codec)
1449{
1450 struct hda_gen_spec *spec = codec->spec;
1451 return create_extra_outs(codec, spec->autocfg.speaker_outs,
1452 spec->autocfg.speaker_pins,
1453 spec->multiout.extra_out_nid,
1454 "Speaker");
1455}
1456
1457/*
1458 * channel mode enum control
1459 */
1460
1461static int ch_mode_info(struct snd_kcontrol *kcontrol,
1462 struct snd_ctl_elem_info *uinfo)
1463{
1464 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1465 struct hda_gen_spec *spec = codec->spec;
1466
1467 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1468 uinfo->count = 1;
1469 uinfo->value.enumerated.items = spec->multi_ios + 1;
1470 if (uinfo->value.enumerated.item > spec->multi_ios)
1471 uinfo->value.enumerated.item = spec->multi_ios;
1472 sprintf(uinfo->value.enumerated.name, "%dch",
1473 (uinfo->value.enumerated.item + 1) * 2);
1474 return 0;
1475}
1476
1477static int ch_mode_get(struct snd_kcontrol *kcontrol,
1478 struct snd_ctl_elem_value *ucontrol)
1479{
1480 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1481 struct hda_gen_spec *spec = codec->spec;
1482 ucontrol->value.enumerated.item[0] = (spec->ext_channel_count - 1) / 2;
1483 return 0;
1484}
1485
1486static int set_multi_io(struct hda_codec *codec, int idx, bool output)
1487{
1488 struct hda_gen_spec *spec = codec->spec;
1489 hda_nid_t nid = spec->multi_io[idx].pin;
1490 struct nid_path *path;
1491
1492 path = snd_hda_get_nid_path(codec, spec->multi_io[idx].dac, nid);
1493 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001495
1496 if (path->active == output)
1497 return 0;
1498
1499 if (output) {
1500 snd_hda_set_pin_ctl_cache(codec, nid, PIN_OUT);
1501 snd_hda_activate_path(codec, path, true, true);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01001502 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001503 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01001504 set_pin_eapd(codec, nid, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001505 snd_hda_activate_path(codec, path, false, true);
1506 snd_hda_set_pin_ctl_cache(codec, nid,
1507 spec->multi_io[idx].ctl_in);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001509 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510}
1511
Takashi Iwai352f7f92012-12-19 12:52:06 +01001512static int ch_mode_put(struct snd_kcontrol *kcontrol,
1513 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001515 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1516 struct hda_gen_spec *spec = codec->spec;
1517 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
Takashi Iwai352f7f92012-12-19 12:52:06 +01001519 ch = ucontrol->value.enumerated.item[0];
1520 if (ch < 0 || ch > spec->multi_ios)
1521 return -EINVAL;
1522 if (ch == (spec->ext_channel_count - 1) / 2)
1523 return 0;
1524 spec->ext_channel_count = (ch + 1) * 2;
1525 for (i = 0; i < spec->multi_ios; i++)
1526 set_multi_io(codec, i, i < ch);
1527 spec->multiout.max_channels = max(spec->ext_channel_count,
1528 spec->const_channel_count);
1529 if (spec->need_dac_fix)
1530 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 return 1;
1532}
1533
Takashi Iwai352f7f92012-12-19 12:52:06 +01001534static const struct snd_kcontrol_new channel_mode_enum = {
1535 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1536 .name = "Channel Mode",
1537 .info = ch_mode_info,
1538 .get = ch_mode_get,
1539 .put = ch_mode_put,
1540};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
Takashi Iwai352f7f92012-12-19 12:52:06 +01001542static int create_multi_channel_mode(struct hda_codec *codec)
1543{
1544 struct hda_gen_spec *spec = codec->spec;
1545
1546 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01001547 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01001548 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 return 0;
1551}
1552
Takashi Iwai352f7f92012-12-19 12:52:06 +01001553/*
1554 * shared headphone/mic handling
1555 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02001556
Takashi Iwai352f7f92012-12-19 12:52:06 +01001557static void call_update_outputs(struct hda_codec *codec);
1558
1559/* for shared I/O, change the pin-control accordingly */
1560static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic)
1561{
1562 struct hda_gen_spec *spec = codec->spec;
1563 unsigned int val;
1564 hda_nid_t pin = spec->autocfg.inputs[1].pin;
1565 /* NOTE: this assumes that there are only two inputs, the
1566 * first is the real internal mic and the second is HP/mic jack.
1567 */
1568
1569 val = snd_hda_get_default_vref(codec, pin);
1570
1571 /* This pin does not have vref caps - let's enable vref on pin 0x18
1572 instead, as suggested by Realtek */
1573 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
1574 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
1575 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
1576 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01001577 snd_hda_set_pin_ctl_cache(codec, vref_pin,
1578 PIN_IN | (set_as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02001579 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001580
1581 val = set_as_mic ? val | PIN_IN : PIN_HP;
Takashi Iwai7594aa32012-12-20 15:38:40 +01001582 snd_hda_set_pin_ctl_cache(codec, pin, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001583
1584 spec->automute_speaker = !set_as_mic;
1585 call_update_outputs(codec);
1586}
1587
1588/* create a shared input with the headphone out */
1589static int create_shared_input(struct hda_codec *codec)
1590{
1591 struct hda_gen_spec *spec = codec->spec;
1592 struct auto_pin_cfg *cfg = &spec->autocfg;
1593 unsigned int defcfg;
1594 hda_nid_t nid;
1595
1596 /* only one internal input pin? */
1597 if (cfg->num_inputs != 1)
1598 return 0;
1599 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
1600 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
1601 return 0;
1602
1603 if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1604 nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
1605 else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
1606 nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
1607 else
1608 return 0; /* both not available */
1609
1610 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
1611 return 0; /* no input */
1612
1613 cfg->inputs[1].pin = nid;
1614 cfg->inputs[1].type = AUTO_PIN_MIC;
1615 cfg->num_inputs = 2;
1616 spec->shared_mic_hp = 1;
1617 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
1618 return 0;
1619}
1620
1621
1622/*
1623 * Parse input paths
1624 */
1625
1626#ifdef CONFIG_PM
1627/* add the powersave loopback-list entry */
1628static void add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
1629{
1630 struct hda_amp_list *list;
1631
1632 if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
1633 return;
1634 list = spec->loopback_list + spec->num_loopbacks;
1635 list->nid = mix;
1636 list->dir = HDA_INPUT;
1637 list->idx = idx;
1638 spec->num_loopbacks++;
Takashi Iwaicb53c622007-08-10 17:21:45 +02001639 spec->loopback.amplist = spec->loopback_list;
1640}
1641#else
Takashi Iwai352f7f92012-12-19 12:52:06 +01001642#define add_loopback_list(spec, mix, idx) /* NOP */
Takashi Iwaicb53c622007-08-10 17:21:45 +02001643#endif
1644
Takashi Iwai352f7f92012-12-19 12:52:06 +01001645/* create input playback/capture controls for the given pin */
1646static int new_analog_input(struct hda_codec *codec, hda_nid_t pin,
1647 const char *ctlname, int ctlidx,
1648 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001650 struct hda_gen_spec *spec = codec->spec;
1651 struct nid_path *path;
1652 unsigned int val;
1653 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
Takashi Iwai352f7f92012-12-19 12:52:06 +01001655 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
1656 !nid_has_mute(codec, mix_nid, HDA_INPUT))
1657 return 0; /* no need for analog loopback */
1658
1659 path = snd_hda_add_new_path(codec, pin, mix_nid, 2);
1660 if (!path)
1661 return -EINVAL;
1662
1663 idx = path->idx[path->depth - 1];
1664 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
1665 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1666 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001667 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001669 path->ctls[NID_PATH_VOL_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 }
1671
Takashi Iwai352f7f92012-12-19 12:52:06 +01001672 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
1673 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1674 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001675 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001677 path->ctls[NID_PATH_MUTE_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 }
1679
Takashi Iwai352f7f92012-12-19 12:52:06 +01001680 path->active = true;
1681 add_loopback_list(spec, mix_nid, idx);
1682 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683}
1684
Takashi Iwai352f7f92012-12-19 12:52:06 +01001685static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001687 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
1688 return (pincap & AC_PINCAP_IN) != 0;
1689}
1690
1691/* Parse the codec tree and retrieve ADCs */
1692static int fill_adc_nids(struct hda_codec *codec)
1693{
1694 struct hda_gen_spec *spec = codec->spec;
1695 hda_nid_t nid;
1696 hda_nid_t *adc_nids = spec->adc_nids;
1697 int max_nums = ARRAY_SIZE(spec->adc_nids);
1698 int i, nums = 0;
1699
1700 nid = codec->start_nid;
1701 for (i = 0; i < codec->num_nodes; i++, nid++) {
1702 unsigned int caps = get_wcaps(codec, nid);
1703 int type = get_wcaps_type(caps);
1704
1705 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
1706 continue;
1707 adc_nids[nums] = nid;
1708 if (++nums >= max_nums)
1709 break;
1710 }
1711 spec->num_adc_nids = nums;
1712 return nums;
1713}
1714
1715/* filter out invalid adc_nids that don't give all active input pins;
1716 * if needed, check whether dynamic ADC-switching is available
1717 */
1718static int check_dyn_adc_switch(struct hda_codec *codec)
1719{
1720 struct hda_gen_spec *spec = codec->spec;
1721 struct hda_input_mux *imux = &spec->input_mux;
1722 hda_nid_t adc_nids[ARRAY_SIZE(spec->adc_nids)];
1723 int i, n, nums;
1724 hda_nid_t pin, adc;
1725
1726 again:
1727 nums = 0;
1728 for (n = 0; n < spec->num_adc_nids; n++) {
1729 adc = spec->adc_nids[n];
1730 for (i = 0; i < imux->num_items; i++) {
1731 pin = spec->imux_pins[i];
1732 if (!is_reachable_path(codec, pin, adc))
1733 break;
1734 }
1735 if (i >= imux->num_items)
1736 adc_nids[nums++] = adc;
1737 }
1738
1739 if (!nums) {
1740 if (spec->shared_mic_hp) {
1741 spec->shared_mic_hp = 0;
1742 imux->num_items = 1;
1743 goto again;
1744 }
1745
1746 /* check whether ADC-switch is possible */
1747 for (i = 0; i < imux->num_items; i++) {
1748 pin = spec->imux_pins[i];
1749 for (n = 0; n < spec->num_adc_nids; n++) {
1750 adc = spec->adc_nids[n];
1751 if (is_reachable_path(codec, pin, adc)) {
1752 spec->dyn_adc_idx[i] = n;
1753 break;
1754 }
1755 }
1756 }
1757
1758 snd_printdd("hda-codec: enabling ADC switching\n");
1759 spec->dyn_adc_switch = 1;
1760 } else if (nums != spec->num_adc_nids) {
1761 memcpy(spec->adc_nids, adc_nids, nums * sizeof(hda_nid_t));
1762 spec->num_adc_nids = nums;
1763 }
1764
1765 if (imux->num_items == 1 || spec->shared_mic_hp) {
1766 snd_printdd("hda-codec: reducing to a single ADC\n");
1767 spec->num_adc_nids = 1; /* reduce to a single ADC */
1768 }
1769
1770 /* single index for individual volumes ctls */
1771 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
1772 spec->num_adc_nids = 1;
1773
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 return 0;
1775}
1776
1777/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01001778 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001780static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02001781{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001782 struct hda_gen_spec *spec = codec->spec;
1783 const struct auto_pin_cfg *cfg = &spec->autocfg;
1784 hda_nid_t mixer = spec->mixer_nid;
1785 struct hda_input_mux *imux = &spec->input_mux;
1786 int num_adcs;
1787 int i, c, err, type_idx = 0;
1788 const char *prev_label = NULL;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02001789
Takashi Iwai352f7f92012-12-19 12:52:06 +01001790 num_adcs = fill_adc_nids(codec);
1791 if (num_adcs < 0)
1792 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02001793
Takashi Iwai352f7f92012-12-19 12:52:06 +01001794 for (i = 0; i < cfg->num_inputs; i++) {
1795 hda_nid_t pin;
1796 const char *label;
1797 bool imux_added;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
Takashi Iwai352f7f92012-12-19 12:52:06 +01001799 pin = cfg->inputs[i].pin;
1800 if (!is_input_pin(codec, pin))
1801 continue;
1802
1803 label = hda_get_autocfg_input_label(codec, cfg, i);
1804 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
1805 label = "Headphone Mic";
1806 if (prev_label && !strcmp(label, prev_label))
1807 type_idx++;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02001808 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01001809 type_idx = 0;
1810 prev_label = label;
1811
1812 if (mixer) {
1813 if (is_reachable_path(codec, pin, mixer)) {
1814 err = new_analog_input(codec, pin,
1815 label, type_idx, mixer);
1816 if (err < 0)
1817 return err;
1818 }
1819 }
1820
1821 imux_added = false;
1822 for (c = 0; c < num_adcs; c++) {
1823 struct nid_path *path;
1824 hda_nid_t adc = spec->adc_nids[c];
1825
1826 if (!is_reachable_path(codec, pin, adc))
1827 continue;
1828 path = snd_array_new(&spec->paths);
1829 if (!path)
1830 return -ENOMEM;
1831 memset(path, 0, sizeof(*path));
1832 if (!snd_hda_parse_nid_path(codec, pin, adc, 2, path)) {
1833 snd_printd(KERN_ERR
1834 "invalid input path 0x%x -> 0x%x\n",
1835 pin, adc);
1836 spec->paths.used--;
1837 continue;
1838 }
1839
1840 if (!imux_added) {
1841 spec->imux_pins[imux->num_items] = pin;
1842 snd_hda_add_imux_item(imux, label,
1843 imux->num_items, NULL);
1844 imux_added = true;
1845 }
1846 }
1847 }
1848
1849 return 0;
1850}
1851
1852
1853/*
1854 * input source mux
1855 */
1856
1857/* get the ADC NID corresponding to the given index */
1858static hda_nid_t get_adc_nid(struct hda_codec *codec, int adc_idx, int imux_idx)
1859{
1860 struct hda_gen_spec *spec = codec->spec;
1861 if (spec->dyn_adc_switch)
1862 adc_idx = spec->dyn_adc_idx[imux_idx];
1863 return spec->adc_nids[adc_idx];
1864}
1865
1866static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
1867 unsigned int idx);
1868
1869static int mux_enum_info(struct snd_kcontrol *kcontrol,
1870 struct snd_ctl_elem_info *uinfo)
1871{
1872 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1873 struct hda_gen_spec *spec = codec->spec;
1874 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
1875}
1876
1877static int mux_enum_get(struct snd_kcontrol *kcontrol,
1878 struct snd_ctl_elem_value *ucontrol)
1879{
1880 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1881 struct hda_gen_spec *spec = codec->spec;
1882 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1883
1884 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
1885 return 0;
1886}
1887
1888static int mux_enum_put(struct snd_kcontrol *kcontrol,
1889 struct snd_ctl_elem_value *ucontrol)
1890{
1891 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1892 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1893 return mux_select(codec, adc_idx,
1894 ucontrol->value.enumerated.item[0]);
1895}
1896
Takashi Iwai352f7f92012-12-19 12:52:06 +01001897static const struct snd_kcontrol_new cap_src_temp = {
1898 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1899 .name = "Input Source",
1900 .info = mux_enum_info,
1901 .get = mux_enum_get,
1902 .put = mux_enum_put,
1903};
1904
Takashi Iwai47d46ab2012-12-20 11:48:54 +01001905/*
1906 * capture volume and capture switch ctls
1907 */
1908
Takashi Iwai352f7f92012-12-19 12:52:06 +01001909typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
1910 struct snd_ctl_elem_value *ucontrol);
1911
Takashi Iwai47d46ab2012-12-20 11:48:54 +01001912/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001913static int cap_put_caller(struct snd_kcontrol *kcontrol,
1914 struct snd_ctl_elem_value *ucontrol,
1915 put_call_t func, int type)
1916{
1917 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1918 struct hda_gen_spec *spec = codec->spec;
1919 const struct hda_input_mux *imux;
1920 struct nid_path *path;
1921 int i, adc_idx, err = 0;
1922
1923 imux = &spec->input_mux;
1924 adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1925 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01001926 /* we use the cache-only update at first since multiple input paths
1927 * may shared the same amp; by updating only caches, the redundant
1928 * writes to hardware can be reduced.
1929 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001930 codec->cached_write = 1;
1931 for (i = 0; i < imux->num_items; i++) {
1932 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
1933 get_adc_nid(codec, adc_idx, i));
1934 if (!path->ctls[type])
1935 continue;
1936 kcontrol->private_value = path->ctls[type];
1937 err = func(kcontrol, ucontrol);
1938 if (err < 0)
1939 goto error;
1940 }
1941 error:
1942 codec->cached_write = 0;
1943 mutex_unlock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01001944 snd_hda_codec_flush_amp_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001945 if (err >= 0 && spec->cap_sync_hook)
1946 spec->cap_sync_hook(codec);
1947 return err;
1948}
1949
1950/* capture volume ctl callbacks */
1951#define cap_vol_info snd_hda_mixer_amp_volume_info
1952#define cap_vol_get snd_hda_mixer_amp_volume_get
1953#define cap_vol_tlv snd_hda_mixer_amp_tlv
1954
1955static int cap_vol_put(struct snd_kcontrol *kcontrol,
1956 struct snd_ctl_elem_value *ucontrol)
1957{
1958 return cap_put_caller(kcontrol, ucontrol,
1959 snd_hda_mixer_amp_volume_put,
1960 NID_PATH_VOL_CTL);
1961}
1962
1963static const struct snd_kcontrol_new cap_vol_temp = {
1964 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1965 .name = "Capture Volume",
1966 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
1967 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
1968 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
1969 .info = cap_vol_info,
1970 .get = cap_vol_get,
1971 .put = cap_vol_put,
1972 .tlv = { .c = cap_vol_tlv },
1973};
1974
1975/* capture switch ctl callbacks */
1976#define cap_sw_info snd_ctl_boolean_stereo_info
1977#define cap_sw_get snd_hda_mixer_amp_switch_get
1978
1979static int cap_sw_put(struct snd_kcontrol *kcontrol,
1980 struct snd_ctl_elem_value *ucontrol)
1981{
1982 return cap_put_caller(kcontrol, ucontrol,
1983 snd_hda_mixer_amp_switch_put,
1984 NID_PATH_MUTE_CTL);
1985}
1986
1987static const struct snd_kcontrol_new cap_sw_temp = {
1988 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1989 .name = "Capture Switch",
1990 .info = cap_sw_info,
1991 .get = cap_sw_get,
1992 .put = cap_sw_put,
1993};
1994
1995static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
1996{
1997 hda_nid_t nid;
1998 int i, depth;
1999
2000 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
2001 for (depth = 0; depth < 3; depth++) {
2002 if (depth >= path->depth)
2003 return -EINVAL;
2004 i = path->depth - depth - 1;
2005 nid = path->path[i];
2006 if (!path->ctls[NID_PATH_VOL_CTL]) {
2007 if (nid_has_volume(codec, nid, HDA_OUTPUT))
2008 path->ctls[NID_PATH_VOL_CTL] =
2009 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2010 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
2011 int idx = path->idx[i];
2012 if (!depth && codec->single_adc_amp)
2013 idx = 0;
2014 path->ctls[NID_PATH_VOL_CTL] =
2015 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2016 }
2017 }
2018 if (!path->ctls[NID_PATH_MUTE_CTL]) {
2019 if (nid_has_mute(codec, nid, HDA_OUTPUT))
2020 path->ctls[NID_PATH_MUTE_CTL] =
2021 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2022 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
2023 int idx = path->idx[i];
2024 if (!depth && codec->single_adc_amp)
2025 idx = 0;
2026 path->ctls[NID_PATH_MUTE_CTL] =
2027 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2028 }
2029 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01002030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 return 0;
2032}
2033
Takashi Iwai352f7f92012-12-19 12:52:06 +01002034static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002036 struct hda_gen_spec *spec = codec->spec;
2037 struct auto_pin_cfg *cfg = &spec->autocfg;
2038 unsigned int val;
2039 int i;
2040
2041 if (!spec->inv_dmic_split)
2042 return false;
2043 for (i = 0; i < cfg->num_inputs; i++) {
2044 if (cfg->inputs[i].pin != nid)
2045 continue;
2046 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2047 return false;
2048 val = snd_hda_codec_get_pincfg(codec, nid);
2049 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
2050 }
2051 return false;
2052}
2053
2054static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
2055 int idx, bool is_switch, unsigned int ctl,
2056 bool inv_dmic)
2057{
2058 struct hda_gen_spec *spec = codec->spec;
2059 char tmpname[44];
2060 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
2061 const char *sfx = is_switch ? "Switch" : "Volume";
2062 unsigned int chs = inv_dmic ? 1 : 3;
2063 int err;
2064
2065 if (!ctl)
2066 return 0;
2067
2068 if (label)
2069 snprintf(tmpname, sizeof(tmpname),
2070 "%s Capture %s", label, sfx);
2071 else
2072 snprintf(tmpname, sizeof(tmpname),
2073 "Capture %s", sfx);
2074 err = add_control(spec, type, tmpname, idx,
2075 amp_val_replace_channels(ctl, chs));
2076 if (err < 0 || !inv_dmic)
2077 return err;
2078
2079 /* Make independent right kcontrol */
2080 if (label)
2081 snprintf(tmpname, sizeof(tmpname),
2082 "Inverted %s Capture %s", label, sfx);
2083 else
2084 snprintf(tmpname, sizeof(tmpname),
2085 "Inverted Capture %s", sfx);
2086 return add_control(spec, type, tmpname, idx,
2087 amp_val_replace_channels(ctl, 2));
2088}
2089
2090/* create single (and simple) capture volume and switch controls */
2091static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
2092 unsigned int vol_ctl, unsigned int sw_ctl,
2093 bool inv_dmic)
2094{
2095 int err;
2096 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
2097 if (err < 0)
2098 return err;
2099 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
2100 if (err < 0)
2101 return err;
2102 return 0;
2103}
2104
2105/* create bound capture volume and switch controls */
2106static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
2107 unsigned int vol_ctl, unsigned int sw_ctl)
2108{
2109 struct hda_gen_spec *spec = codec->spec;
2110 struct snd_kcontrol_new *knew;
2111
2112 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002113 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002114 if (!knew)
2115 return -ENOMEM;
2116 knew->index = idx;
2117 knew->private_value = vol_ctl;
2118 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2119 }
2120 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002121 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002122 if (!knew)
2123 return -ENOMEM;
2124 knew->index = idx;
2125 knew->private_value = sw_ctl;
2126 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2127 }
2128 return 0;
2129}
2130
2131/* return the vol ctl when used first in the imux list */
2132static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
2133{
2134 struct hda_gen_spec *spec = codec->spec;
2135 struct nid_path *path;
2136 unsigned int ctl;
2137 int i;
2138
2139 path = snd_hda_get_nid_path(codec, spec->imux_pins[idx],
2140 get_adc_nid(codec, 0, idx));
2141 if (!path)
2142 return 0;
2143 ctl = path->ctls[type];
2144 if (!ctl)
2145 return 0;
2146 for (i = 0; i < idx - 1; i++) {
2147 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2148 get_adc_nid(codec, 0, i));
2149 if (path && path->ctls[type] == ctl)
2150 return 0;
2151 }
2152 return ctl;
2153}
2154
2155/* create individual capture volume and switch controls per input */
2156static int create_multi_cap_vol_ctl(struct hda_codec *codec)
2157{
2158 struct hda_gen_spec *spec = codec->spec;
2159 struct hda_input_mux *imux = &spec->input_mux;
2160 int i, err, type, type_idx = 0;
2161 const char *prev_label = NULL;
2162
2163 for (i = 0; i < imux->num_items; i++) {
2164 const char *label;
2165 bool inv_dmic;
2166 label = hda_get_autocfg_input_label(codec, &spec->autocfg, i);
2167 if (prev_label && !strcmp(label, prev_label))
2168 type_idx++;
2169 else
2170 type_idx = 0;
2171 prev_label = label;
2172 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
2173
2174 for (type = 0; type < 2; type++) {
2175 err = add_single_cap_ctl(codec, label, type_idx, type,
2176 get_first_cap_ctl(codec, i, type),
2177 inv_dmic);
2178 if (err < 0)
2179 return err;
2180 }
2181 }
2182 return 0;
2183}
2184
2185static int create_capture_mixers(struct hda_codec *codec)
2186{
2187 struct hda_gen_spec *spec = codec->spec;
2188 struct hda_input_mux *imux = &spec->input_mux;
2189 int i, n, nums, err;
2190
2191 if (spec->dyn_adc_switch)
2192 nums = 1;
2193 else
2194 nums = spec->num_adc_nids;
2195
2196 if (!spec->auto_mic && imux->num_items > 1) {
2197 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01002198 const char *name;
2199 name = nums > 1 ? "Input Source" : "Capture Source";
2200 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002201 if (!knew)
2202 return -ENOMEM;
2203 knew->count = nums;
2204 }
2205
2206 for (n = 0; n < nums; n++) {
2207 bool multi = false;
2208 bool inv_dmic = false;
2209 int vol, sw;
2210
2211 vol = sw = 0;
2212 for (i = 0; i < imux->num_items; i++) {
2213 struct nid_path *path;
2214 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2215 get_adc_nid(codec, n, i));
2216 if (!path)
2217 continue;
2218 parse_capvol_in_path(codec, path);
2219 if (!vol)
2220 vol = path->ctls[NID_PATH_VOL_CTL];
2221 else if (vol != path->ctls[NID_PATH_VOL_CTL])
2222 multi = true;
2223 if (!sw)
2224 sw = path->ctls[NID_PATH_MUTE_CTL];
2225 else if (sw != path->ctls[NID_PATH_MUTE_CTL])
2226 multi = true;
2227 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
2228 inv_dmic = true;
2229 }
2230
2231 if (!multi)
2232 err = create_single_cap_vol_ctl(codec, n, vol, sw,
2233 inv_dmic);
2234 else if (!spec->multi_cap_vol)
2235 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
2236 else
2237 err = create_multi_cap_vol_ctl(codec);
2238 if (err < 0)
2239 return err;
2240 }
2241
2242 return 0;
2243}
2244
2245/*
2246 * add mic boosts if needed
2247 */
2248static int parse_mic_boost(struct hda_codec *codec)
2249{
2250 struct hda_gen_spec *spec = codec->spec;
2251 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002252 int i, err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002253 int type_idx = 0;
2254 hda_nid_t nid;
2255 const char *prev_label = NULL;
2256
2257 for (i = 0; i < cfg->num_inputs; i++) {
2258 if (cfg->inputs[i].type > AUTO_PIN_MIC)
2259 break;
2260 nid = cfg->inputs[i].pin;
2261 if (get_wcaps(codec, nid) & AC_WCAP_IN_AMP) {
2262 const char *label;
2263 char boost_label[32];
2264 struct nid_path *path;
2265 unsigned int val;
2266
2267 label = hda_get_autocfg_input_label(codec, cfg, i);
2268 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
2269 label = "Headphone Mic";
2270 if (prev_label && !strcmp(label, prev_label))
2271 type_idx++;
2272 else
2273 type_idx = 0;
2274 prev_label = label;
2275
2276 snprintf(boost_label, sizeof(boost_label),
2277 "%s Boost Volume", label);
2278 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
2279 err = add_control(spec, HDA_CTL_WIDGET_VOL,
2280 boost_label, type_idx, val);
2281 if (err < 0)
2282 return err;
2283
2284 path = snd_hda_get_nid_path(codec, nid, 0);
2285 if (path)
2286 path->ctls[NID_PATH_BOOST_CTL] = val;
2287 }
2288 }
2289 return 0;
2290}
2291
2292/*
2293 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
2294 */
2295static void parse_digital(struct hda_codec *codec)
2296{
2297 struct hda_gen_spec *spec = codec->spec;
2298 int i, nums;
2299 hda_nid_t dig_nid;
2300
2301 /* support multiple SPDIFs; the secondary is set up as a slave */
2302 nums = 0;
2303 for (i = 0; i < spec->autocfg.dig_outs; i++) {
2304 hda_nid_t pin = spec->autocfg.dig_out_pins[i];
2305 dig_nid = look_for_dac(codec, pin, true);
2306 if (!dig_nid)
2307 continue;
2308 if (!snd_hda_add_new_path(codec, dig_nid, pin, 2))
2309 continue;
2310 if (!nums) {
2311 spec->multiout.dig_out_nid = dig_nid;
2312 spec->dig_out_type = spec->autocfg.dig_out_type[0];
2313 } else {
2314 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
2315 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
2316 break;
2317 spec->slave_dig_outs[nums - 1] = dig_nid;
2318 }
2319 nums++;
2320 }
2321
2322 if (spec->autocfg.dig_in_pin) {
2323 dig_nid = codec->start_nid;
2324 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
2325 struct nid_path *path;
2326 unsigned int wcaps = get_wcaps(codec, dig_nid);
2327 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
2328 continue;
2329 if (!(wcaps & AC_WCAP_DIGITAL))
2330 continue;
2331 path = snd_hda_add_new_path(codec,
2332 spec->autocfg.dig_in_pin,
2333 dig_nid, 2);
2334 if (path) {
2335 path->active = true;
2336 spec->dig_in_nid = dig_nid;
2337 break;
2338 }
2339 }
2340 }
2341}
2342
2343
2344/*
2345 * input MUX handling
2346 */
2347
2348static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
2349
2350/* select the given imux item; either unmute exclusively or select the route */
2351static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2352 unsigned int idx)
2353{
2354 struct hda_gen_spec *spec = codec->spec;
2355 const struct hda_input_mux *imux;
2356 struct nid_path *path;
2357
2358 imux = &spec->input_mux;
2359 if (!imux->num_items)
2360 return 0;
2361
2362 if (idx >= imux->num_items)
2363 idx = imux->num_items - 1;
2364 if (spec->cur_mux[adc_idx] == idx)
2365 return 0;
2366
2367 path = snd_hda_get_nid_path(codec,
2368 spec->imux_pins[spec->cur_mux[adc_idx]],
2369 spec->adc_nids[adc_idx]);
2370 if (!path)
2371 return 0;
2372 if (path->active)
2373 snd_hda_activate_path(codec, path, false, false);
2374
2375 spec->cur_mux[adc_idx] = idx;
2376
2377 if (spec->shared_mic_hp)
2378 update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
2379
2380 if (spec->dyn_adc_switch)
2381 dyn_adc_pcm_resetup(codec, idx);
2382
2383 path = snd_hda_get_nid_path(codec, spec->imux_pins[idx],
2384 get_adc_nid(codec, adc_idx, idx));
2385 if (!path)
2386 return 0;
2387 if (path->active)
2388 return 0;
2389 snd_hda_activate_path(codec, path, true, false);
2390 if (spec->cap_sync_hook)
2391 spec->cap_sync_hook(codec);
2392 return 1;
2393}
2394
2395
2396/*
2397 * Jack detections for HP auto-mute and mic-switch
2398 */
2399
2400/* check each pin in the given array; returns true if any of them is plugged */
2401static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
2402{
2403 int i, present = 0;
2404
2405 for (i = 0; i < num_pins; i++) {
2406 hda_nid_t nid = pins[i];
2407 if (!nid)
2408 break;
2409 present |= snd_hda_jack_detect(codec, nid);
2410 }
2411 return present;
2412}
2413
2414/* standard HP/line-out auto-mute helper */
2415static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
2416 bool mute, bool hp_out)
2417{
2418 struct hda_gen_spec *spec = codec->spec;
2419 unsigned int pin_bits = mute ? 0 : (hp_out ? PIN_HP : PIN_OUT);
2420 int i;
2421
2422 for (i = 0; i < num_pins; i++) {
2423 hda_nid_t nid = pins[i];
2424 unsigned int val;
2425 if (!nid)
2426 break;
2427 /* don't reset VREF value in case it's controlling
2428 * the amp (see alc861_fixup_asus_amp_vref_0f())
2429 */
2430 if (spec->keep_vref_in_automute) {
2431 val = snd_hda_codec_read(codec, nid, 0,
2432 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2433 val &= ~PIN_HP;
2434 } else
2435 val = 0;
2436 val |= pin_bits;
Takashi Iwai7594aa32012-12-20 15:38:40 +01002437 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002438 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002439 }
2440}
2441
2442/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002443void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002444{
2445 struct hda_gen_spec *spec = codec->spec;
2446 int on;
2447
2448 /* Control HP pins/amps depending on master_mute state;
2449 * in general, HP pins/amps control should be enabled in all cases,
2450 * but currently set only for master_mute, just to be safe
2451 */
2452 if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
2453 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2454 spec->autocfg.hp_pins, spec->master_mute, true);
2455
2456 if (!spec->automute_speaker)
2457 on = 0;
2458 else
2459 on = spec->hp_jack_present | spec->line_jack_present;
2460 on |= spec->master_mute;
2461 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
2462 spec->autocfg.speaker_pins, on, false);
2463
2464 /* toggle line-out mutes if needed, too */
2465 /* if LO is a copy of either HP or Speaker, don't need to handle it */
2466 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
2467 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
2468 return;
2469 if (!spec->automute_lo)
2470 on = 0;
2471 else
2472 on = spec->hp_jack_present;
2473 on |= spec->master_mute;
2474 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2475 spec->autocfg.line_out_pins, on, false);
2476}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002477EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002478
2479static void call_update_outputs(struct hda_codec *codec)
2480{
2481 struct hda_gen_spec *spec = codec->spec;
2482 if (spec->automute_hook)
2483 spec->automute_hook(codec);
2484 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01002485 snd_hda_gen_update_outputs(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002486}
2487
2488/* standard HP-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002489void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002490{
2491 struct hda_gen_spec *spec = codec->spec;
2492
2493 spec->hp_jack_present =
2494 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2495 spec->autocfg.hp_pins);
2496 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
2497 return;
2498 call_update_outputs(codec);
2499}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002500EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002501
2502/* standard line-out-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002503void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002504{
2505 struct hda_gen_spec *spec = codec->spec;
2506
2507 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
2508 return;
2509 /* check LO jack only when it's different from HP */
2510 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
2511 return;
2512
2513 spec->line_jack_present =
2514 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2515 spec->autocfg.line_out_pins);
2516 if (!spec->automute_speaker || !spec->detect_lo)
2517 return;
2518 call_update_outputs(codec);
2519}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002520EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002521
2522/* standard mic auto-switch helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002523void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002524{
2525 struct hda_gen_spec *spec = codec->spec;
2526 int i;
2527
2528 if (!spec->auto_mic)
2529 return;
2530
2531 for (i = spec->am_num_entries - 1; i > 0; i--) {
2532 if (snd_hda_jack_detect(codec, spec->am_entry[i].pin)) {
2533 mux_select(codec, 0, spec->am_entry[i].idx);
2534 return;
2535 }
2536 }
2537 mux_select(codec, 0, spec->am_entry[0].idx);
2538}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002539EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002540
2541/*
2542 * Auto-Mute mode mixer enum support
2543 */
2544static int automute_mode_info(struct snd_kcontrol *kcontrol,
2545 struct snd_ctl_elem_info *uinfo)
2546{
2547 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2548 struct hda_gen_spec *spec = codec->spec;
2549 static const char * const texts3[] = {
2550 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02002551 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552
Takashi Iwai352f7f92012-12-19 12:52:06 +01002553 if (spec->automute_speaker_possible && spec->automute_lo_possible)
2554 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
2555 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2556}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557
Takashi Iwai352f7f92012-12-19 12:52:06 +01002558static int automute_mode_get(struct snd_kcontrol *kcontrol,
2559 struct snd_ctl_elem_value *ucontrol)
2560{
2561 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2562 struct hda_gen_spec *spec = codec->spec;
2563 unsigned int val = 0;
2564 if (spec->automute_speaker)
2565 val++;
2566 if (spec->automute_lo)
2567 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002568
Takashi Iwai352f7f92012-12-19 12:52:06 +01002569 ucontrol->value.enumerated.item[0] = val;
2570 return 0;
2571}
2572
2573static int automute_mode_put(struct snd_kcontrol *kcontrol,
2574 struct snd_ctl_elem_value *ucontrol)
2575{
2576 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2577 struct hda_gen_spec *spec = codec->spec;
2578
2579 switch (ucontrol->value.enumerated.item[0]) {
2580 case 0:
2581 if (!spec->automute_speaker && !spec->automute_lo)
2582 return 0;
2583 spec->automute_speaker = 0;
2584 spec->automute_lo = 0;
2585 break;
2586 case 1:
2587 if (spec->automute_speaker_possible) {
2588 if (!spec->automute_lo && spec->automute_speaker)
2589 return 0;
2590 spec->automute_speaker = 1;
2591 spec->automute_lo = 0;
2592 } else if (spec->automute_lo_possible) {
2593 if (spec->automute_lo)
2594 return 0;
2595 spec->automute_lo = 1;
2596 } else
2597 return -EINVAL;
2598 break;
2599 case 2:
2600 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
2601 return -EINVAL;
2602 if (spec->automute_speaker && spec->automute_lo)
2603 return 0;
2604 spec->automute_speaker = 1;
2605 spec->automute_lo = 1;
2606 break;
2607 default:
2608 return -EINVAL;
2609 }
2610 call_update_outputs(codec);
2611 return 1;
2612}
2613
2614static const struct snd_kcontrol_new automute_mode_enum = {
2615 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2616 .name = "Auto-Mute Mode",
2617 .info = automute_mode_info,
2618 .get = automute_mode_get,
2619 .put = automute_mode_put,
2620};
2621
2622static int add_automute_mode_enum(struct hda_codec *codec)
2623{
2624 struct hda_gen_spec *spec = codec->spec;
2625
Takashi Iwai12c93df2012-12-19 14:38:33 +01002626 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002627 return -ENOMEM;
2628 return 0;
2629}
2630
2631/*
2632 * Check the availability of HP/line-out auto-mute;
2633 * Set up appropriately if really supported
2634 */
2635static int check_auto_mute_availability(struct hda_codec *codec)
2636{
2637 struct hda_gen_spec *spec = codec->spec;
2638 struct auto_pin_cfg *cfg = &spec->autocfg;
2639 int present = 0;
2640 int i, err;
2641
2642 if (cfg->hp_pins[0])
2643 present++;
2644 if (cfg->line_out_pins[0])
2645 present++;
2646 if (cfg->speaker_pins[0])
2647 present++;
2648 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02002649 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002650
2651 if (!cfg->speaker_pins[0] &&
2652 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
2653 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2654 sizeof(cfg->speaker_pins));
2655 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657
Takashi Iwai352f7f92012-12-19 12:52:06 +01002658 if (!cfg->hp_pins[0] &&
2659 cfg->line_out_type == AUTO_PIN_HP_OUT) {
2660 memcpy(cfg->hp_pins, cfg->line_out_pins,
2661 sizeof(cfg->hp_pins));
2662 cfg->hp_outs = cfg->line_outs;
2663 }
2664
2665 for (i = 0; i < cfg->hp_outs; i++) {
2666 hda_nid_t nid = cfg->hp_pins[i];
2667 if (!is_jack_detectable(codec, nid))
2668 continue;
2669 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
2670 nid);
2671 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
Takashi Iwai5d550e12012-12-19 15:16:44 +01002672 snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002673 spec->detect_hp = 1;
2674 }
2675
2676 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
2677 if (cfg->speaker_outs)
2678 for (i = 0; i < cfg->line_outs; i++) {
2679 hda_nid_t nid = cfg->line_out_pins[i];
2680 if (!is_jack_detectable(codec, nid))
2681 continue;
2682 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
2683 snd_hda_jack_detect_enable_callback(codec, nid,
2684 HDA_GEN_FRONT_EVENT,
Takashi Iwai5d550e12012-12-19 15:16:44 +01002685 snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002686 spec->detect_lo = 1;
2687 }
2688 spec->automute_lo_possible = spec->detect_hp;
2689 }
2690
2691 spec->automute_speaker_possible = cfg->speaker_outs &&
2692 (spec->detect_hp || spec->detect_lo);
2693
2694 spec->automute_lo = spec->automute_lo_possible;
2695 spec->automute_speaker = spec->automute_speaker_possible;
2696
2697 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
2698 /* create a control for automute mode */
2699 err = add_automute_mode_enum(codec);
2700 if (err < 0)
2701 return err;
2702 }
2703 return 0;
2704}
2705
2706/* return the position of NID in the list, or -1 if not found */
2707static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
2708{
2709 int i;
2710 for (i = 0; i < nums; i++)
2711 if (list[i] == nid)
2712 return i;
2713 return -1;
2714}
2715
2716/* check whether all auto-mic pins are valid; setup indices if OK */
2717static bool auto_mic_check_imux(struct hda_codec *codec)
2718{
2719 struct hda_gen_spec *spec = codec->spec;
2720 const struct hda_input_mux *imux;
2721 int i;
2722
2723 imux = &spec->input_mux;
2724 for (i = 0; i < spec->am_num_entries; i++) {
2725 spec->am_entry[i].idx =
2726 find_idx_in_nid_list(spec->am_entry[i].pin,
2727 spec->imux_pins, imux->num_items);
2728 if (spec->am_entry[i].idx < 0)
2729 return false; /* no corresponding imux */
2730 }
2731
2732 /* we don't need the jack detection for the first pin */
2733 for (i = 1; i < spec->am_num_entries; i++)
2734 snd_hda_jack_detect_enable_callback(codec,
2735 spec->am_entry[i].pin,
2736 HDA_GEN_MIC_EVENT,
Takashi Iwai5d550e12012-12-19 15:16:44 +01002737 snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002738 return true;
2739}
2740
2741static int compare_attr(const void *ap, const void *bp)
2742{
2743 const struct automic_entry *a = ap;
2744 const struct automic_entry *b = bp;
2745 return (int)(a->attr - b->attr);
2746}
2747
2748/*
2749 * Check the availability of auto-mic switch;
2750 * Set up if really supported
2751 */
2752static int check_auto_mic_availability(struct hda_codec *codec)
2753{
2754 struct hda_gen_spec *spec = codec->spec;
2755 struct auto_pin_cfg *cfg = &spec->autocfg;
2756 unsigned int types;
2757 int i, num_pins;
2758
2759 types = 0;
2760 num_pins = 0;
2761 for (i = 0; i < cfg->num_inputs; i++) {
2762 hda_nid_t nid = cfg->inputs[i].pin;
2763 unsigned int attr;
2764 attr = snd_hda_codec_get_pincfg(codec, nid);
2765 attr = snd_hda_get_input_pin_attr(attr);
2766 if (types & (1 << attr))
2767 return 0; /* already occupied */
2768 switch (attr) {
2769 case INPUT_PIN_ATTR_INT:
2770 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2771 return 0; /* invalid type */
2772 break;
2773 case INPUT_PIN_ATTR_UNUSED:
2774 return 0; /* invalid entry */
2775 default:
2776 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
2777 return 0; /* invalid type */
2778 if (!spec->line_in_auto_switch &&
2779 cfg->inputs[i].type != AUTO_PIN_MIC)
2780 return 0; /* only mic is allowed */
2781 if (!is_jack_detectable(codec, nid))
2782 return 0; /* no unsol support */
2783 break;
2784 }
2785 if (num_pins >= MAX_AUTO_MIC_PINS)
2786 return 0;
2787 types |= (1 << attr);
2788 spec->am_entry[num_pins].pin = nid;
2789 spec->am_entry[num_pins].attr = attr;
2790 num_pins++;
2791 }
2792
2793 if (num_pins < 2)
2794 return 0;
2795
2796 spec->am_num_entries = num_pins;
2797 /* sort the am_entry in the order of attr so that the pin with a
2798 * higher attr will be selected when the jack is plugged.
2799 */
2800 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
2801 compare_attr, NULL);
2802
2803 if (!auto_mic_check_imux(codec))
2804 return 0;
2805
2806 spec->auto_mic = 1;
2807 spec->num_adc_nids = 1;
2808 spec->cur_mux[0] = spec->am_entry[0].idx;
2809 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
2810 spec->am_entry[0].pin,
2811 spec->am_entry[1].pin,
2812 spec->am_entry[2].pin);
2813
2814 return 0;
2815}
2816
2817
Takashi Iwai9eb413e2012-12-19 14:41:21 +01002818/*
2819 * Parse the given BIOS configuration and set up the hda_gen_spec
2820 *
2821 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002822 * or a negative error code
2823 */
2824int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01002825 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002826{
2827 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002828 int err;
2829
Takashi Iwai9eb413e2012-12-19 14:41:21 +01002830 if (cfg != &spec->autocfg) {
2831 spec->autocfg = *cfg;
2832 cfg = &spec->autocfg;
2833 }
2834
Takashi Iwai352f7f92012-12-19 12:52:06 +01002835 if (!cfg->line_outs) {
2836 if (cfg->dig_outs || cfg->dig_in_pin) {
2837 spec->multiout.max_channels = 2;
2838 spec->no_analog = 1;
2839 goto dig_only;
2840 }
2841 return 0; /* can't find valid BIOS pin config */
2842 }
2843
2844 if (!spec->no_primary_hp &&
2845 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
2846 cfg->line_outs <= cfg->hp_outs) {
2847 /* use HP as primary out */
2848 cfg->speaker_outs = cfg->line_outs;
2849 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2850 sizeof(cfg->speaker_pins));
2851 cfg->line_outs = cfg->hp_outs;
2852 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
2853 cfg->hp_outs = 0;
2854 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
2855 cfg->line_out_type = AUTO_PIN_HP_OUT;
2856 }
2857
2858 err = parse_output_paths(codec);
2859 if (err < 0)
2860 return err;
2861 err = create_multi_channel_mode(codec);
2862 if (err < 0)
2863 return err;
2864 err = create_multi_out_ctls(codec, cfg);
2865 if (err < 0)
2866 return err;
2867 err = create_hp_out_ctls(codec);
2868 if (err < 0)
2869 return err;
2870 err = create_speaker_out_ctls(codec);
2871 if (err < 0)
2872 return err;
2873 err = create_shared_input(codec);
2874 if (err < 0)
2875 return err;
2876 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002877 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02002878 return err;
2879
Takashi Iwai352f7f92012-12-19 12:52:06 +01002880 /* check the multiple speaker pins */
2881 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
2882 spec->const_channel_count = cfg->line_outs * 2;
2883 else
2884 spec->const_channel_count = cfg->speaker_outs * 2;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002885
Takashi Iwai352f7f92012-12-19 12:52:06 +01002886 if (spec->multi_ios > 0)
2887 spec->multiout.max_channels = max(spec->ext_channel_count,
2888 spec->const_channel_count);
2889 else
2890 spec->multiout.max_channels = spec->multiout.num_dacs * 2;
2891
2892 err = check_auto_mute_availability(codec);
2893 if (err < 0)
2894 return err;
2895
2896 err = check_dyn_adc_switch(codec);
2897 if (err < 0)
2898 return err;
2899
2900 if (!spec->shared_mic_hp) {
2901 err = check_auto_mic_availability(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002902 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 }
Takashi Iwai071c73a2006-08-23 18:34:06 +02002905
Takashi Iwai352f7f92012-12-19 12:52:06 +01002906 err = create_capture_mixers(codec);
2907 if (err < 0)
2908 return err;
2909
2910 err = parse_mic_boost(codec);
2911 if (err < 0)
2912 return err;
2913
2914 dig_only:
2915 parse_digital(codec);
2916
2917 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918}
Takashi Iwai352f7f92012-12-19 12:52:06 +01002919EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920
2921
2922/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002923 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002925
2926/* slave controls for virtual master */
2927static const char * const slave_pfxs[] = {
2928 "Front", "Surround", "Center", "LFE", "Side",
2929 "Headphone", "Speaker", "Mono", "Line Out",
2930 "CLFE", "Bass Speaker", "PCM",
2931 NULL,
2932};
2933
2934int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002936 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938
Takashi Iwai36502d02012-12-19 15:15:10 +01002939 if (spec->kctls.used) {
2940 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
2941 if (err < 0)
2942 return err;
2943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944
Takashi Iwai352f7f92012-12-19 12:52:06 +01002945 if (spec->multiout.dig_out_nid) {
2946 err = snd_hda_create_dig_out_ctls(codec,
2947 spec->multiout.dig_out_nid,
2948 spec->multiout.dig_out_nid,
2949 spec->pcm_rec[1].pcm_type);
2950 if (err < 0)
2951 return err;
2952 if (!spec->no_analog) {
2953 err = snd_hda_create_spdif_share_sw(codec,
2954 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 if (err < 0)
2956 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002957 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 }
2959 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002960 if (spec->dig_in_nid) {
2961 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
2962 if (err < 0)
2963 return err;
2964 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965
Takashi Iwai352f7f92012-12-19 12:52:06 +01002966 /* if we have no master control, let's create it */
2967 if (!spec->no_analog &&
2968 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
2969 unsigned int vmaster_tlv[4];
2970 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
2971 HDA_OUTPUT, vmaster_tlv);
2972 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
2973 vmaster_tlv, slave_pfxs,
2974 "Playback Volume");
2975 if (err < 0)
2976 return err;
2977 }
2978 if (!spec->no_analog &&
2979 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
2980 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
2981 NULL, slave_pfxs,
2982 "Playback Switch",
2983 true, &spec->vmaster_mute.sw_kctl);
2984 if (err < 0)
2985 return err;
2986 if (spec->vmaster_mute.hook)
Takashi Iwaifd25a972012-12-20 14:57:18 +01002987 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
2988 spec->vmaster_mute_enum);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990
Takashi Iwai352f7f92012-12-19 12:52:06 +01002991 free_kctls(spec); /* no longer needed */
2992
2993 if (spec->shared_mic_hp) {
2994 int err;
2995 int nid = spec->autocfg.inputs[1].pin;
2996 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
2997 if (err < 0)
2998 return err;
2999 err = snd_hda_jack_detect_enable(codec, nid, 0);
3000 if (err < 0)
3001 return err;
3002 }
3003
3004 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
3005 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 return err;
3007
3008 return 0;
3009}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003010EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
3011
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012
3013/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003014 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016
Takashi Iwai352f7f92012-12-19 12:52:06 +01003017/*
3018 * Analog playback callbacks
3019 */
3020static int playback_pcm_open(struct hda_pcm_stream *hinfo,
3021 struct hda_codec *codec,
3022 struct snd_pcm_substream *substream)
3023{
3024 struct hda_gen_spec *spec = codec->spec;
3025 return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
3026 hinfo);
3027}
3028
3029static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01003030 struct hda_codec *codec,
3031 unsigned int stream_tag,
3032 unsigned int format,
3033 struct snd_pcm_substream *substream)
3034{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003035 struct hda_gen_spec *spec = codec->spec;
3036 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
3037 stream_tag, format, substream);
3038}
Takashi Iwai97ec5582006-03-21 11:29:07 +01003039
Takashi Iwai352f7f92012-12-19 12:52:06 +01003040static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3041 struct hda_codec *codec,
3042 struct snd_pcm_substream *substream)
3043{
3044 struct hda_gen_spec *spec = codec->spec;
3045 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
3046}
3047
3048/*
3049 * Digital out
3050 */
3051static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
3052 struct hda_codec *codec,
3053 struct snd_pcm_substream *substream)
3054{
3055 struct hda_gen_spec *spec = codec->spec;
3056 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3057}
3058
3059static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3060 struct hda_codec *codec,
3061 unsigned int stream_tag,
3062 unsigned int format,
3063 struct snd_pcm_substream *substream)
3064{
3065 struct hda_gen_spec *spec = codec->spec;
3066 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3067 stream_tag, format, substream);
3068}
3069
3070static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3071 struct hda_codec *codec,
3072 struct snd_pcm_substream *substream)
3073{
3074 struct hda_gen_spec *spec = codec->spec;
3075 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
3076}
3077
3078static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
3079 struct hda_codec *codec,
3080 struct snd_pcm_substream *substream)
3081{
3082 struct hda_gen_spec *spec = codec->spec;
3083 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3084}
3085
3086/*
3087 * Analog capture
3088 */
3089static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3090 struct hda_codec *codec,
3091 unsigned int stream_tag,
3092 unsigned int format,
3093 struct snd_pcm_substream *substream)
3094{
3095 struct hda_gen_spec *spec = codec->spec;
3096
3097 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01003098 stream_tag, 0, format);
3099 return 0;
3100}
3101
Takashi Iwai352f7f92012-12-19 12:52:06 +01003102static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3103 struct hda_codec *codec,
3104 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01003105{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003106 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01003107
Takashi Iwai352f7f92012-12-19 12:52:06 +01003108 snd_hda_codec_cleanup_stream(codec,
3109 spec->adc_nids[substream->number + 1]);
Takashi Iwai97ec5582006-03-21 11:29:07 +01003110 return 0;
3111}
3112
Takashi Iwai352f7f92012-12-19 12:52:06 +01003113/*
3114 */
3115static const struct hda_pcm_stream pcm_analog_playback = {
3116 .substreams = 1,
3117 .channels_min = 2,
3118 .channels_max = 8,
3119 /* NID is set in build_pcms */
3120 .ops = {
3121 .open = playback_pcm_open,
3122 .prepare = playback_pcm_prepare,
3123 .cleanup = playback_pcm_cleanup
3124 },
3125};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126
Takashi Iwai352f7f92012-12-19 12:52:06 +01003127static const struct hda_pcm_stream pcm_analog_capture = {
3128 .substreams = 1,
3129 .channels_min = 2,
3130 .channels_max = 2,
3131 /* NID is set in build_pcms */
3132};
3133
3134static const struct hda_pcm_stream pcm_analog_alt_playback = {
3135 .substreams = 1,
3136 .channels_min = 2,
3137 .channels_max = 2,
3138 /* NID is set in build_pcms */
3139};
3140
3141static const struct hda_pcm_stream pcm_analog_alt_capture = {
3142 .substreams = 2, /* can be overridden */
3143 .channels_min = 2,
3144 .channels_max = 2,
3145 /* NID is set in build_pcms */
3146 .ops = {
3147 .prepare = alt_capture_pcm_prepare,
3148 .cleanup = alt_capture_pcm_cleanup
3149 },
3150};
3151
3152static const struct hda_pcm_stream pcm_digital_playback = {
3153 .substreams = 1,
3154 .channels_min = 2,
3155 .channels_max = 2,
3156 /* NID is set in build_pcms */
3157 .ops = {
3158 .open = dig_playback_pcm_open,
3159 .close = dig_playback_pcm_close,
3160 .prepare = dig_playback_pcm_prepare,
3161 .cleanup = dig_playback_pcm_cleanup
3162 },
3163};
3164
3165static const struct hda_pcm_stream pcm_digital_capture = {
3166 .substreams = 1,
3167 .channels_min = 2,
3168 .channels_max = 2,
3169 /* NID is set in build_pcms */
3170};
3171
3172/* Used by build_pcms to flag that a PCM has no playback stream */
3173static const struct hda_pcm_stream pcm_null_stream = {
3174 .substreams = 0,
3175 .channels_min = 0,
3176 .channels_max = 0,
3177};
3178
3179/*
3180 * dynamic changing ADC PCM streams
3181 */
3182static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
3183{
3184 struct hda_gen_spec *spec = codec->spec;
3185 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
3186
3187 if (spec->cur_adc && spec->cur_adc != new_adc) {
3188 /* stream is running, let's swap the current ADC */
3189 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
3190 spec->cur_adc = new_adc;
3191 snd_hda_codec_setup_stream(codec, new_adc,
3192 spec->cur_adc_stream_tag, 0,
3193 spec->cur_adc_format);
3194 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003196 return false;
3197}
3198
3199/* analog capture with dynamic dual-adc changes */
3200static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3201 struct hda_codec *codec,
3202 unsigned int stream_tag,
3203 unsigned int format,
3204 struct snd_pcm_substream *substream)
3205{
3206 struct hda_gen_spec *spec = codec->spec;
3207 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
3208 spec->cur_adc_stream_tag = stream_tag;
3209 spec->cur_adc_format = format;
3210 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
3211 return 0;
3212}
3213
3214static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3215 struct hda_codec *codec,
3216 struct snd_pcm_substream *substream)
3217{
3218 struct hda_gen_spec *spec = codec->spec;
3219 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
3220 spec->cur_adc = 0;
3221 return 0;
3222}
3223
3224static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
3225 .substreams = 1,
3226 .channels_min = 2,
3227 .channels_max = 2,
3228 .nid = 0, /* fill later */
3229 .ops = {
3230 .prepare = dyn_adc_capture_pcm_prepare,
3231 .cleanup = dyn_adc_capture_pcm_cleanup
3232 },
3233};
3234
Takashi Iwaif873e532012-12-20 16:58:39 +01003235static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
3236 const char *chip_name)
3237{
3238 char *p;
3239
3240 if (*str)
3241 return;
3242 strlcpy(str, chip_name, len);
3243
3244 /* drop non-alnum chars after a space */
3245 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
3246 if (!isalnum(p[1])) {
3247 *p = 0;
3248 break;
3249 }
3250 }
3251 strlcat(str, sfx, len);
3252}
3253
Takashi Iwai352f7f92012-12-19 12:52:06 +01003254/* build PCM streams based on the parsed results */
3255int snd_hda_gen_build_pcms(struct hda_codec *codec)
3256{
3257 struct hda_gen_spec *spec = codec->spec;
3258 struct hda_pcm *info = spec->pcm_rec;
3259 const struct hda_pcm_stream *p;
3260 bool have_multi_adcs;
3261 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262
3263 codec->num_pcms = 1;
3264 codec->pcm_info = info;
3265
Takashi Iwai352f7f92012-12-19 12:52:06 +01003266 if (spec->no_analog)
3267 goto skip_analog;
3268
Takashi Iwaif873e532012-12-20 16:58:39 +01003269 fill_pcm_stream_name(spec->stream_name_analog,
3270 sizeof(spec->stream_name_analog),
3271 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003272 info->name = spec->stream_name_analog;
3273
3274 if (spec->multiout.num_dacs > 0) {
3275 p = spec->stream_analog_playback;
3276 if (!p)
3277 p = &pcm_analog_playback;
3278 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3279 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
3280 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
3281 spec->multiout.max_channels;
3282 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
3283 spec->autocfg.line_outs == 2)
3284 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
3285 snd_pcm_2_1_chmaps;
3286 }
3287 if (spec->num_adc_nids) {
3288 p = spec->stream_analog_capture;
3289 if (!p) {
3290 if (spec->dyn_adc_switch)
3291 p = &dyn_adc_pcm_analog_capture;
3292 else
3293 p = &pcm_analog_capture;
3294 }
3295 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3296 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
3297 }
3298
3299 if (spec->channel_mode) {
3300 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max = 0;
3301 for (i = 0; i < spec->num_channel_mode; i++) {
3302 if (spec->channel_mode[i].channels > info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max) {
3303 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max = spec->channel_mode[i].channels;
3304 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01003305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003307
3308 skip_analog:
3309 /* SPDIF for stream index #1 */
3310 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01003311 fill_pcm_stream_name(spec->stream_name_digital,
3312 sizeof(spec->stream_name_digital),
3313 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003314 codec->num_pcms = 2;
3315 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
3316 info = spec->pcm_rec + 1;
3317 info->name = spec->stream_name_digital;
3318 if (spec->dig_out_type)
3319 info->pcm_type = spec->dig_out_type;
3320 else
3321 info->pcm_type = HDA_PCM_TYPE_SPDIF;
3322 if (spec->multiout.dig_out_nid) {
3323 p = spec->stream_digital_playback;
3324 if (!p)
3325 p = &pcm_digital_playback;
3326 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3327 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
3328 }
3329 if (spec->dig_in_nid) {
3330 p = spec->stream_digital_capture;
3331 if (!p)
3332 p = &pcm_digital_capture;
3333 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3334 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
3335 }
3336 }
3337
3338 if (spec->no_analog)
3339 return 0;
3340
3341 /* If the use of more than one ADC is requested for the current
3342 * model, configure a second analog capture-only PCM.
3343 */
3344 have_multi_adcs = (spec->num_adc_nids > 1) &&
3345 !spec->dyn_adc_switch && !spec->auto_mic;
3346 /* Additional Analaog capture for index #2 */
3347 if (spec->alt_dac_nid || have_multi_adcs) {
3348 codec->num_pcms = 3;
3349 info = spec->pcm_rec + 2;
3350 info->name = spec->stream_name_analog;
3351 if (spec->alt_dac_nid) {
3352 p = spec->stream_analog_alt_playback;
3353 if (!p)
3354 p = &pcm_analog_alt_playback;
3355 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3356 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
3357 spec->alt_dac_nid;
3358 } else {
3359 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
3360 pcm_null_stream;
3361 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
3362 }
3363 if (have_multi_adcs) {
3364 p = spec->stream_analog_alt_capture;
3365 if (!p)
3366 p = &pcm_analog_alt_capture;
3367 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3368 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
3369 spec->adc_nids[1];
3370 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
3371 spec->num_adc_nids - 1;
3372 } else {
3373 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
3374 pcm_null_stream;
3375 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
3376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 }
3378
3379 return 0;
3380}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003381EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
3382
3383
3384/*
3385 * Standard auto-parser initializations
3386 */
3387
3388/* configure the path from the given dac to the pin as the proper output */
3389static void set_output_and_unmute(struct hda_codec *codec, hda_nid_t pin,
3390 int pin_type, hda_nid_t dac)
3391{
3392 struct nid_path *path;
3393
3394 snd_hda_set_pin_ctl_cache(codec, pin, pin_type);
3395 path = snd_hda_get_nid_path(codec, dac, pin);
3396 if (!path)
3397 return;
3398 if (path->active)
3399 return;
3400 snd_hda_activate_path(codec, path, true, true);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01003401 set_pin_eapd(codec, pin, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003402}
3403
3404/* initialize primary output paths */
3405static void init_multi_out(struct hda_codec *codec)
3406{
3407 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai64049c82012-12-20 15:29:21 +01003408 hda_nid_t nid, dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003409 int pin_type;
3410 int i;
3411
3412 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
3413 pin_type = PIN_HP;
3414 else
3415 pin_type = PIN_OUT;
3416
Takashi Iwai64049c82012-12-20 15:29:21 +01003417 for (i = 0; i < spec->autocfg.line_outs; i++) {
3418 nid = spec->autocfg.line_out_pins[i];
3419 if (nid) {
3420 dac = spec->multiout.dac_nids[i];
3421 if (!dac)
3422 dac = spec->multiout.dac_nids[0];
3423 set_output_and_unmute(codec, nid, pin_type, dac);
3424 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003425 }
3426}
3427
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003428
3429static void __init_extra_out(struct hda_codec *codec, int num_outs,
3430 hda_nid_t *pins, hda_nid_t *dacs, int type)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003431{
3432 struct hda_gen_spec *spec = codec->spec;
3433 int i;
3434 hda_nid_t pin, dac;
3435
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003436 for (i = 0; i < num_outs; i++) {
3437 pin = pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003438 if (!pin)
3439 break;
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003440 dac = dacs[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003441 if (!dac) {
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003442 if (i > 0 && dacs[0])
3443 dac = dacs[0];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003444 else
3445 dac = spec->multiout.dac_nids[0];
3446 }
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003447 set_output_and_unmute(codec, pin, type, dac);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003448 }
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003449}
3450
3451/* initialize hp and speaker paths */
3452static void init_extra_out(struct hda_codec *codec)
3453{
3454 struct hda_gen_spec *spec = codec->spec;
3455
3456 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
3457 __init_extra_out(codec, spec->autocfg.hp_outs,
3458 spec->autocfg.hp_pins,
3459 spec->multiout.hp_out_nid, PIN_HP);
3460 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
3461 __init_extra_out(codec, spec->autocfg.speaker_outs,
3462 spec->autocfg.speaker_pins,
3463 spec->multiout.extra_out_nid, PIN_OUT);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003464}
3465
3466/* initialize multi-io paths */
3467static void init_multi_io(struct hda_codec *codec)
3468{
3469 struct hda_gen_spec *spec = codec->spec;
3470 int i;
3471
3472 for (i = 0; i < spec->multi_ios; i++) {
3473 hda_nid_t pin = spec->multi_io[i].pin;
3474 struct nid_path *path;
3475 path = snd_hda_get_nid_path(codec, spec->multi_io[i].dac, pin);
3476 if (!path)
3477 continue;
3478 if (!spec->multi_io[i].ctl_in)
3479 spec->multi_io[i].ctl_in =
3480 snd_hda_codec_update_cache(codec, pin, 0,
3481 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3482 snd_hda_activate_path(codec, path, path->active, true);
3483 }
3484}
3485
3486/* set up the input pin config, depending on the given auto-pin type */
3487static void set_input_pin(struct hda_codec *codec, hda_nid_t nid,
3488 int auto_pin_type)
3489{
3490 unsigned int val = PIN_IN;
3491 if (auto_pin_type == AUTO_PIN_MIC)
3492 val |= snd_hda_get_default_vref(codec, nid);
Takashi Iwai7594aa32012-12-20 15:38:40 +01003493 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003494}
3495
3496/* set up input pins and loopback paths */
3497static void init_analog_input(struct hda_codec *codec)
3498{
3499 struct hda_gen_spec *spec = codec->spec;
3500 struct auto_pin_cfg *cfg = &spec->autocfg;
3501 int i;
3502
3503 for (i = 0; i < cfg->num_inputs; i++) {
3504 hda_nid_t nid = cfg->inputs[i].pin;
3505 if (is_input_pin(codec, nid))
3506 set_input_pin(codec, nid, cfg->inputs[i].type);
3507
3508 /* init loopback inputs */
3509 if (spec->mixer_nid) {
3510 struct nid_path *path;
3511 path = snd_hda_get_nid_path(codec, nid, spec->mixer_nid);
3512 if (path)
3513 snd_hda_activate_path(codec, path,
3514 path->active, false);
3515 }
3516 }
3517}
3518
3519/* initialize ADC paths */
3520static void init_input_src(struct hda_codec *codec)
3521{
3522 struct hda_gen_spec *spec = codec->spec;
3523 struct hda_input_mux *imux = &spec->input_mux;
3524 struct nid_path *path;
3525 int i, c, nums;
3526
3527 if (spec->dyn_adc_switch)
3528 nums = 1;
3529 else
3530 nums = spec->num_adc_nids;
3531
3532 for (c = 0; c < nums; c++) {
3533 for (i = 0; i < imux->num_items; i++) {
3534 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
3535 get_adc_nid(codec, c, i));
3536 if (path) {
3537 bool active = path->active;
3538 if (i == spec->cur_mux[c])
3539 active = true;
3540 snd_hda_activate_path(codec, path, active, false);
3541 }
3542 }
3543 }
3544
3545 if (spec->shared_mic_hp)
3546 update_shared_mic_hp(codec, spec->cur_mux[0]);
3547
3548 if (spec->cap_sync_hook)
3549 spec->cap_sync_hook(codec);
3550}
3551
3552/* set right pin controls for digital I/O */
3553static void init_digital(struct hda_codec *codec)
3554{
3555 struct hda_gen_spec *spec = codec->spec;
3556 int i;
3557 hda_nid_t pin;
3558
3559 for (i = 0; i < spec->autocfg.dig_outs; i++) {
3560 pin = spec->autocfg.dig_out_pins[i];
3561 if (!pin)
3562 continue;
3563 set_output_and_unmute(codec, pin, PIN_OUT, 0);
3564 }
3565 pin = spec->autocfg.dig_in_pin;
3566 if (pin)
Takashi Iwai7594aa32012-12-20 15:38:40 +01003567 snd_hda_set_pin_ctl_cache(codec, pin, PIN_IN);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003568}
3569
Takashi Iwai973e4972012-12-20 15:16:09 +01003570/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
3571 * invalid unsol tags by some reason
3572 */
3573static void clear_unsol_on_unused_pins(struct hda_codec *codec)
3574{
3575 int i;
3576
3577 for (i = 0; i < codec->init_pins.used; i++) {
3578 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
3579 hda_nid_t nid = pin->nid;
3580 if (is_jack_detectable(codec, nid) &&
3581 !snd_hda_jack_tbl_get(codec, nid))
3582 snd_hda_codec_update_cache(codec, nid, 0,
3583 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
3584 }
3585}
3586
Takashi Iwai352f7f92012-12-19 12:52:06 +01003587int snd_hda_gen_init(struct hda_codec *codec)
3588{
3589 struct hda_gen_spec *spec = codec->spec;
3590
3591 if (spec->init_hook)
3592 spec->init_hook(codec);
3593
3594 snd_hda_apply_verbs(codec);
3595
Takashi Iwai3bbcd272012-12-20 11:50:58 +01003596 codec->cached_write = 1;
3597
Takashi Iwai352f7f92012-12-19 12:52:06 +01003598 init_multi_out(codec);
3599 init_extra_out(codec);
3600 init_multi_io(codec);
3601 init_analog_input(codec);
3602 init_input_src(codec);
3603 init_digital(codec);
3604
Takashi Iwai973e4972012-12-20 15:16:09 +01003605 clear_unsol_on_unused_pins(codec);
3606
Takashi Iwai352f7f92012-12-19 12:52:06 +01003607 /* call init functions of standard auto-mute helpers */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003608 snd_hda_gen_hp_automute(codec, NULL);
3609 snd_hda_gen_line_automute(codec, NULL);
3610 snd_hda_gen_mic_autoswitch(codec, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003611
Takashi Iwai3bbcd272012-12-20 11:50:58 +01003612 snd_hda_codec_flush_amp_cache(codec);
3613 snd_hda_codec_flush_cmd_cache(codec);
3614
Takashi Iwai352f7f92012-12-19 12:52:06 +01003615 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
3616 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
3617
3618 hda_call_check_power_status(codec, 0x01);
3619 return 0;
3620}
3621EXPORT_SYMBOL(snd_hda_gen_init);
3622
3623
3624/*
3625 * the generic codec support
3626 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627
Takashi Iwai83012a72012-08-24 18:38:08 +02003628#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003629static int generic_check_power_status(struct hda_codec *codec, hda_nid_t nid)
3630{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003631 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003632 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
3633}
3634#endif
3635
Takashi Iwai352f7f92012-12-19 12:52:06 +01003636static void generic_free(struct hda_codec *codec)
3637{
3638 snd_hda_gen_spec_free(codec->spec);
3639 kfree(codec->spec);
3640 codec->spec = NULL;
3641}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642
Takashi Iwai352f7f92012-12-19 12:52:06 +01003643static const struct hda_codec_ops generic_patch_ops = {
3644 .build_controls = snd_hda_gen_build_controls,
3645 .build_pcms = snd_hda_gen_build_pcms,
3646 .init = snd_hda_gen_init,
3647 .free = generic_free,
3648 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02003649#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003650 .check_power_status = generic_check_power_status,
3651#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652};
3653
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654int snd_hda_parse_generic_codec(struct hda_codec *codec)
3655{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003656 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 int err;
3658
Takashi Iwaie560d8d2005-09-09 14:21:46 +02003659 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003660 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003662 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003665 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
3666 if (err < 0)
3667 return err;
3668
3669 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003670 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 goto error;
3672
3673 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 return 0;
3675
Takashi Iwai352f7f92012-12-19 12:52:06 +01003676error:
3677 generic_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 return err;
3679}
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003680EXPORT_SYMBOL(snd_hda_parse_generic_codec);