blob: f9d3ea3c5a688fc1765c2691230ee320239ef654 [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);
Takashi Iwai38cf6f12012-12-21 14:09:42 +010044 mutex_init(&spec->pcm_mutex);
Takashi Iwai352f7f92012-12-19 12:52:06 +010045 return 0;
46}
47EXPORT_SYMBOL_HDA(snd_hda_gen_spec_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Takashi Iwai12c93df2012-12-19 14:38:33 +010049struct snd_kcontrol_new *
50snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
51 const struct snd_kcontrol_new *temp)
Takashi Iwai352f7f92012-12-19 12:52:06 +010052{
53 struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
54 if (!knew)
55 return NULL;
56 *knew = *temp;
57 if (name)
58 knew->name = kstrdup(name, GFP_KERNEL);
59 else if (knew->name)
60 knew->name = kstrdup(knew->name, GFP_KERNEL);
61 if (!knew->name)
62 return NULL;
63 return knew;
64}
Takashi Iwai12c93df2012-12-19 14:38:33 +010065EXPORT_SYMBOL_HDA(snd_hda_gen_add_kctl);
Takashi Iwai352f7f92012-12-19 12:52:06 +010066
67static void free_kctls(struct hda_gen_spec *spec)
68{
69 if (spec->kctls.list) {
70 struct snd_kcontrol_new *kctl = spec->kctls.list;
71 int i;
72 for (i = 0; i < spec->kctls.used; i++)
73 kfree(kctl[i].name);
74 }
75 snd_array_free(&spec->kctls);
76}
77
78static struct hda_bind_ctls *new_bind_ctl(struct hda_codec *codec,
79 unsigned int nums,
80 struct hda_ctl_ops *ops)
81{
82 struct hda_gen_spec *spec = codec->spec;
83 struct hda_bind_ctls **ctlp, *ctl;
84 ctlp = snd_array_new(&spec->bind_ctls);
85 if (!ctlp)
86 return NULL;
87 ctl = kzalloc(sizeof(*ctl) + sizeof(long) * (nums + 1), GFP_KERNEL);
88 *ctlp = ctl;
89 if (ctl)
90 ctl->ops = ops;
91 return ctl;
92}
93
94static void free_bind_ctls(struct hda_gen_spec *spec)
95{
96 if (spec->bind_ctls.list) {
97 struct hda_bind_ctls **ctl = spec->bind_ctls.list;
98 int i;
99 for (i = 0; i < spec->bind_ctls.used; i++)
100 kfree(ctl[i]);
101 }
102 snd_array_free(&spec->bind_ctls);
103}
104
105void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
106{
107 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 return;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100109 free_kctls(spec);
110 free_bind_ctls(spec);
111 snd_array_free(&spec->paths);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
Takashi Iwai352f7f92012-12-19 12:52:06 +0100113EXPORT_SYMBOL_HDA(snd_hda_gen_spec_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100116 * parsing paths
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Takashi Iwaif5172a72013-01-04 13:19:55 +0100119static struct nid_path *get_nid_path(struct hda_codec *codec,
120 hda_nid_t from_nid, hda_nid_t to_nid,
121 int with_aa_mix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100123 struct hda_gen_spec *spec = codec->spec;
124 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Takashi Iwai352f7f92012-12-19 12:52:06 +0100126 for (i = 0; i < spec->paths.used; i++) {
127 struct nid_path *path = snd_array_elem(&spec->paths, i);
128 if (path->depth <= 0)
129 continue;
130 if ((!from_nid || path->path[0] == from_nid) &&
Takashi Iwaif5172a72013-01-04 13:19:55 +0100131 (!to_nid || path->path[path->depth - 1] == to_nid)) {
132 if (with_aa_mix == HDA_PARSE_ALL ||
133 path->with_aa_mix == with_aa_mix)
134 return path;
135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 }
137 return NULL;
138}
Takashi Iwaif5172a72013-01-04 13:19:55 +0100139
140/* get the path between the given NIDs;
141 * passing 0 to either @pin or @dac behaves as a wildcard
142 */
143struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
144 hda_nid_t from_nid, hda_nid_t to_nid)
145{
146 return get_nid_path(codec, from_nid, to_nid, HDA_PARSE_ALL);
147}
Takashi Iwai352f7f92012-12-19 12:52:06 +0100148EXPORT_SYMBOL_HDA(snd_hda_get_nid_path);
149
150/* check whether the given DAC is already found in any existing paths */
151static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
152{
153 struct hda_gen_spec *spec = codec->spec;
154 int i;
155
156 for (i = 0; i < spec->paths.used; i++) {
157 struct nid_path *path = snd_array_elem(&spec->paths, i);
158 if (path->path[0] == nid)
159 return true;
160 }
161 return false;
162}
163
164/* check whether the given two widgets can be connected */
165static bool is_reachable_path(struct hda_codec *codec,
166 hda_nid_t from_nid, hda_nid_t to_nid)
167{
168 if (!from_nid || !to_nid)
169 return false;
170 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
171}
172
173/* nid, dir and idx */
174#define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
175
176/* check whether the given ctl is already assigned in any path elements */
177static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
178{
179 struct hda_gen_spec *spec = codec->spec;
180 int i;
181
182 val &= AMP_VAL_COMPARE_MASK;
183 for (i = 0; i < spec->paths.used; i++) {
184 struct nid_path *path = snd_array_elem(&spec->paths, i);
185 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
186 return true;
187 }
188 return false;
189}
190
191/* check whether a control with the given (nid, dir, idx) was assigned */
192static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
193 int dir, int idx)
194{
195 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
196 return is_ctl_used(codec, val, NID_PATH_VOL_CTL) ||
197 is_ctl_used(codec, val, NID_PATH_MUTE_CTL);
198}
199
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100200static void print_nid_path(const char *pfx, struct nid_path *path)
201{
202 char buf[40];
203 int i;
204
205
206 buf[0] = 0;
207 for (i = 0; i < path->depth; i++) {
208 char tmp[4];
209 sprintf(tmp, ":%02x", path->path[i]);
210 strlcat(buf, tmp, sizeof(buf));
211 }
212 snd_printdd("%s path: depth=%d %s\n", pfx, path->depth, buf);
213}
214
Takashi Iwai352f7f92012-12-19 12:52:06 +0100215/* called recursively */
216static bool __parse_nid_path(struct hda_codec *codec,
217 hda_nid_t from_nid, hda_nid_t to_nid,
218 int with_aa_mix, struct nid_path *path, int depth)
219{
220 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100221 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100222 int i, nums;
223
224 if (to_nid == spec->mixer_nid) {
Takashi Iwai4ac0eef2012-12-20 18:10:51 +0100225 if (with_aa_mix == HDA_PARSE_NO_AAMIX)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100226 return false;
Takashi Iwai4ac0eef2012-12-20 18:10:51 +0100227 with_aa_mix = HDA_PARSE_ALL; /* mark aa-mix is included */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100228 }
229
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100230 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100231 for (i = 0; i < nums; i++) {
232 if (conn[i] != from_nid) {
233 /* special case: when from_nid is 0,
234 * try to find an empty DAC
235 */
236 if (from_nid ||
237 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
238 is_dac_already_used(codec, conn[i]))
239 continue;
240 }
241 /* aa-mix is requested but not included? */
Takashi Iwai4ac0eef2012-12-20 18:10:51 +0100242 if (!(spec->mixer_nid && with_aa_mix == HDA_PARSE_ONLY_AAMIX))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100243 goto found;
244 }
245 if (depth >= MAX_NID_PATH_DEPTH)
246 return false;
247 for (i = 0; i < nums; i++) {
248 unsigned int type;
249 type = get_wcaps_type(get_wcaps(codec, conn[i]));
250 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
251 type == AC_WID_PIN)
252 continue;
253 if (__parse_nid_path(codec, from_nid, conn[i],
254 with_aa_mix, path, depth + 1))
255 goto found;
256 }
257 return false;
258
259 found:
260 path->path[path->depth] = conn[i];
Takashi Iwaif5172a72013-01-04 13:19:55 +0100261 if (conn[i] == spec->mixer_nid)
262 path->with_aa_mix = true;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100263 path->idx[path->depth + 1] = i;
264 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
265 path->multi[path->depth + 1] = 1;
266 path->depth++;
267 return true;
268}
269
270/* parse the widget path from the given nid to the target nid;
271 * when @from_nid is 0, try to find an empty DAC;
Takashi Iwai4ac0eef2012-12-20 18:10:51 +0100272 * when @with_aa_mix is HDA_PARSE_NO_AAMIX, paths with spec->mixer_nid are
273 * excluded, only the paths that don't go through the mixer will be chosen.
274 * when @with_aa_mix is HDA_PARSE_ONLY_AAMIX, only the paths going through
275 * spec->mixer_nid will be chosen.
276 * when @with_aa_mix is HDA_PARSE_ALL, no special handling about mixer widget.
Takashi Iwai352f7f92012-12-19 12:52:06 +0100277 */
278bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
279 hda_nid_t to_nid, int with_aa_mix,
280 struct nid_path *path)
281{
282 if (__parse_nid_path(codec, from_nid, to_nid, with_aa_mix, path, 1)) {
283 path->path[path->depth] = to_nid;
284 path->depth++;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100285 return true;
286 }
287 return false;
288}
289EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100292 * parse the path between the given NIDs and add to the path list.
293 * if no valid path is found, return NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100295struct nid_path *
296snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
297 hda_nid_t to_nid, int with_aa_mix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100299 struct hda_gen_spec *spec = codec->spec;
300 struct nid_path *path;
301
302 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
303 return NULL;
304
Takashi Iwaif5172a72013-01-04 13:19:55 +0100305 /* check whether the path has been already added */
306 path = get_nid_path(codec, from_nid, to_nid, with_aa_mix);
307 if (path)
308 return path;
309
Takashi Iwai352f7f92012-12-19 12:52:06 +0100310 path = snd_array_new(&spec->paths);
311 if (!path)
312 return NULL;
313 memset(path, 0, sizeof(*path));
314 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, with_aa_mix, path))
315 return path;
316 /* push back */
317 spec->paths.used--;
318 return NULL;
319}
320EXPORT_SYMBOL_HDA(snd_hda_add_new_path);
321
322/* look for an empty DAC slot */
323static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
324 bool is_digital)
325{
326 struct hda_gen_spec *spec = codec->spec;
327 bool cap_digital;
328 int i;
329
330 for (i = 0; i < spec->num_all_dacs; i++) {
331 hda_nid_t nid = spec->all_dacs[i];
332 if (!nid || is_dac_already_used(codec, nid))
333 continue;
334 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
335 if (is_digital != cap_digital)
336 continue;
337 if (is_reachable_path(codec, nid, pin))
338 return nid;
339 }
340 return 0;
341}
342
343/* replace the channels in the composed amp value with the given number */
344static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
345{
346 val &= ~(0x3U << 16);
347 val |= chs << 16;
348 return val;
349}
350
351/* check whether the widget has the given amp capability for the direction */
352static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
353 int dir, unsigned int bits)
354{
355 if (!nid)
356 return false;
357 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
358 if (query_amp_caps(codec, nid, dir) & bits)
359 return true;
360 return false;
361}
362
363#define nid_has_mute(codec, nid, dir) \
364 check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
365#define nid_has_volume(codec, nid, dir) \
366 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
367
368/* look for a widget suitable for assigning a mute switch in the path */
369static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
370 struct nid_path *path)
371{
372 int i;
373
374 for (i = path->depth - 1; i >= 0; i--) {
375 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
376 return path->path[i];
377 if (i != path->depth - 1 && i != 0 &&
378 nid_has_mute(codec, path->path[i], HDA_INPUT))
379 return path->path[i];
380 }
381 return 0;
382}
383
384/* look for a widget suitable for assigning a volume ctl in the path */
385static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
386 struct nid_path *path)
387{
388 int i;
389
390 for (i = path->depth - 1; i >= 0; i--) {
391 if (nid_has_volume(codec, path->path[i], HDA_OUTPUT))
392 return path->path[i];
393 }
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200394 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395}
396
397/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100398 * path activation / deactivation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100400
401/* can have the amp-in capability? */
402static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100404 hda_nid_t nid = path->path[idx];
405 unsigned int caps = get_wcaps(codec, nid);
406 unsigned int type = get_wcaps_type(caps);
407
408 if (!(caps & AC_WCAP_IN_AMP))
409 return false;
410 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
411 return false;
412 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413}
414
Takashi Iwai352f7f92012-12-19 12:52:06 +0100415/* can have the amp-out capability? */
416static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100418 hda_nid_t nid = path->path[idx];
419 unsigned int caps = get_wcaps(codec, nid);
420 unsigned int type = get_wcaps_type(caps);
421
422 if (!(caps & AC_WCAP_OUT_AMP))
423 return false;
424 if (type == AC_WID_PIN && !idx) /* only for output pins */
425 return false;
426 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427}
428
Takashi Iwai352f7f92012-12-19 12:52:06 +0100429/* check whether the given (nid,dir,idx) is active */
430static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
431 unsigned int idx, unsigned int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100433 struct hda_gen_spec *spec = codec->spec;
434 int i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Takashi Iwai352f7f92012-12-19 12:52:06 +0100436 for (n = 0; n < spec->paths.used; n++) {
437 struct nid_path *path = snd_array_elem(&spec->paths, n);
438 if (!path->active)
439 continue;
440 for (i = 0; i < path->depth; i++) {
441 if (path->path[i] == nid) {
442 if (dir == HDA_OUTPUT || path->idx[i] == idx)
443 return true;
444 break;
445 }
446 }
447 }
448 return false;
449}
450
451/* get the default amp value for the target state */
452static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
453 int dir, bool enable)
454{
455 unsigned int caps;
456 unsigned int val = 0;
457
458 caps = query_amp_caps(codec, nid, dir);
459 if (caps & AC_AMPCAP_NUM_STEPS) {
460 /* set to 0dB */
461 if (enable)
462 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
463 }
464 if (caps & AC_AMPCAP_MUTE) {
465 if (!enable)
466 val |= HDA_AMP_MUTE;
467 }
468 return val;
469}
470
471/* initialize the amp value (only at the first time) */
472static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
473{
474 int val = get_amp_val_to_activate(codec, nid, dir, false);
475 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
476}
477
478static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
479 int idx, bool enable)
480{
481 int val;
482 if (is_ctl_associated(codec, nid, dir, idx) ||
Takashi Iwai985803c2013-01-03 16:30:04 +0100483 (!enable && is_active_nid(codec, nid, dir, idx)))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100484 return;
485 val = get_amp_val_to_activate(codec, nid, dir, enable);
486 snd_hda_codec_amp_stereo(codec, nid, dir, idx, 0xff, val);
487}
488
489static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
490 int i, bool enable)
491{
492 hda_nid_t nid = path->path[i];
493 init_amp(codec, nid, HDA_OUTPUT, 0);
494 activate_amp(codec, nid, HDA_OUTPUT, 0, enable);
495}
496
497static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
498 int i, bool enable, bool add_aamix)
499{
500 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100501 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100502 int n, nums, idx;
503 int type;
504 hda_nid_t nid = path->path[i];
505
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100506 nums = snd_hda_get_conn_list(codec, nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100507 type = get_wcaps_type(get_wcaps(codec, nid));
508 if (type == AC_WID_PIN ||
509 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
510 nums = 1;
511 idx = 0;
512 } else
513 idx = path->idx[i];
514
515 for (n = 0; n < nums; n++)
516 init_amp(codec, nid, HDA_INPUT, n);
517
518 if (is_ctl_associated(codec, nid, HDA_INPUT, idx))
519 return;
520
521 /* here is a little bit tricky in comparison with activate_amp_out();
522 * when aa-mixer is available, we need to enable the path as well
523 */
524 for (n = 0; n < nums; n++) {
525 if (n != idx && (!add_aamix || conn[n] != spec->mixer_nid))
526 continue;
527 activate_amp(codec, nid, HDA_INPUT, n, enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 }
529}
530
Takashi Iwai352f7f92012-12-19 12:52:06 +0100531/* activate or deactivate the given path
532 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100534void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
535 bool enable, bool add_aamix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100537 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Takashi Iwai352f7f92012-12-19 12:52:06 +0100539 if (!enable)
540 path->active = false;
541
542 for (i = path->depth - 1; i >= 0; i--) {
543 if (enable && path->multi[i])
544 snd_hda_codec_write_cache(codec, path->path[i], 0,
545 AC_VERB_SET_CONNECT_SEL,
546 path->idx[i]);
547 if (has_amp_in(codec, path, i))
548 activate_amp_in(codec, path, i, enable, add_aamix);
549 if (has_amp_out(codec, path, i))
550 activate_amp_out(codec, path, i, enable);
551 }
552
553 if (enable)
554 path->active = true;
555}
556EXPORT_SYMBOL_HDA(snd_hda_activate_path);
557
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100558/* turn on/off EAPD on the given pin */
559static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
560{
561 struct hda_gen_spec *spec = codec->spec;
562 if (spec->own_eapd_ctl ||
563 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
564 return;
Takashi Iwaiecac3ed2012-12-21 15:23:01 +0100565 if (codec->inv_eapd)
566 enable = !enable;
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100567 snd_hda_codec_update_cache(codec, pin, 0,
568 AC_VERB_SET_EAPD_BTLENABLE,
569 enable ? 0x02 : 0x00);
570}
571
Takashi Iwai352f7f92012-12-19 12:52:06 +0100572
573/*
574 * Helper functions for creating mixer ctl elements
575 */
576
577enum {
578 HDA_CTL_WIDGET_VOL,
579 HDA_CTL_WIDGET_MUTE,
580 HDA_CTL_BIND_MUTE,
581 HDA_CTL_BIND_VOL,
582 HDA_CTL_BIND_SW,
583};
584static const struct snd_kcontrol_new control_templates[] = {
585 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
586 HDA_CODEC_MUTE(NULL, 0, 0, 0),
587 HDA_BIND_MUTE(NULL, 0, 0, 0),
588 HDA_BIND_VOL(NULL, 0),
589 HDA_BIND_SW(NULL, 0),
590};
591
592/* add dynamic controls from template */
593static int add_control(struct hda_gen_spec *spec, int type, const char *name,
594 int cidx, unsigned long val)
595{
596 struct snd_kcontrol_new *knew;
597
Takashi Iwai12c93df2012-12-19 14:38:33 +0100598 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100599 if (!knew)
600 return -ENOMEM;
601 knew->index = cidx;
602 if (get_amp_nid_(val))
603 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
604 knew->private_value = val;
605 return 0;
606}
607
608static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
609 const char *pfx, const char *dir,
610 const char *sfx, int cidx, unsigned long val)
611{
612 char name[32];
613 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
614 return add_control(spec, type, name, cidx, val);
615}
616
617#define add_pb_vol_ctrl(spec, type, pfx, val) \
618 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
619#define add_pb_sw_ctrl(spec, type, pfx, val) \
620 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
621#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
622 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
623#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
624 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
625
626static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
627 unsigned int chs, struct nid_path *path)
628{
629 unsigned int val;
630 if (!path)
631 return 0;
632 val = path->ctls[NID_PATH_VOL_CTL];
633 if (!val)
634 return 0;
635 val = amp_val_replace_channels(val, chs);
636 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
637}
638
639/* return the channel bits suitable for the given path->ctls[] */
640static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
641 int type)
642{
643 int chs = 1; /* mono (left only) */
644 if (path) {
645 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
646 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
647 chs = 3; /* stereo */
648 }
649 return chs;
650}
651
652static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
653 struct nid_path *path)
654{
655 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
656 return add_vol_ctl(codec, pfx, cidx, chs, path);
657}
658
659/* create a mute-switch for the given mixer widget;
660 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
661 */
662static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
663 unsigned int chs, struct nid_path *path)
664{
665 unsigned int val;
666 int type = HDA_CTL_WIDGET_MUTE;
667
668 if (!path)
669 return 0;
670 val = path->ctls[NID_PATH_MUTE_CTL];
671 if (!val)
672 return 0;
673 val = amp_val_replace_channels(val, chs);
674 if (get_amp_direction_(val) == HDA_INPUT) {
675 hda_nid_t nid = get_amp_nid_(val);
676 int nums = snd_hda_get_num_conns(codec, nid);
677 if (nums > 1) {
678 type = HDA_CTL_BIND_MUTE;
679 val |= nums << 19;
680 }
681 }
682 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
683}
684
685static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
686 int cidx, struct nid_path *path)
687{
688 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
689 return add_sw_ctl(codec, pfx, cidx, chs, path);
690}
691
692static const char * const channel_name[4] = {
693 "Front", "Surround", "CLFE", "Side"
694};
695
696/* give some appropriate ctl name prefix for the given line out channel */
697static const char *get_line_out_pfx(struct hda_gen_spec *spec, int ch,
698 bool can_be_master, int *index)
699{
700 struct auto_pin_cfg *cfg = &spec->autocfg;
701
702 *index = 0;
703 if (cfg->line_outs == 1 && !spec->multi_ios &&
704 !cfg->hp_outs && !cfg->speaker_outs && can_be_master)
705 return spec->vmaster_mute.hook ? "PCM" : "Master";
706
707 /* if there is really a single DAC used in the whole output paths,
708 * use it master (or "PCM" if a vmaster hook is present)
709 */
710 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
711 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
712 return spec->vmaster_mute.hook ? "PCM" : "Master";
713
714 switch (cfg->line_out_type) {
715 case AUTO_PIN_SPEAKER_OUT:
716 if (cfg->line_outs == 1)
717 return "Speaker";
718 if (cfg->line_outs == 2)
719 return ch ? "Bass Speaker" : "Speaker";
720 break;
721 case AUTO_PIN_HP_OUT:
722 /* for multi-io case, only the primary out */
723 if (ch && spec->multi_ios)
724 break;
725 *index = ch;
726 return "Headphone";
727 default:
728 if (cfg->line_outs == 1 && !spec->multi_ios)
729 return "PCM";
730 break;
731 }
732 if (ch >= ARRAY_SIZE(channel_name)) {
733 snd_BUG();
734 return "PCM";
735 }
736
737 return channel_name[ch];
738}
739
740/*
741 * Parse output paths
742 */
743
744/* badness definition */
745enum {
746 /* No primary DAC is found for the main output */
747 BAD_NO_PRIMARY_DAC = 0x10000,
748 /* No DAC is found for the extra output */
749 BAD_NO_DAC = 0x4000,
750 /* No possible multi-ios */
751 BAD_MULTI_IO = 0x103,
752 /* No individual DAC for extra output */
753 BAD_NO_EXTRA_DAC = 0x102,
754 /* No individual DAC for extra surrounds */
755 BAD_NO_EXTRA_SURR_DAC = 0x101,
756 /* Primary DAC shared with main surrounds */
757 BAD_SHARED_SURROUND = 0x100,
758 /* Primary DAC shared with main CLFE */
759 BAD_SHARED_CLFE = 0x10,
760 /* Primary DAC shared with extra surrounds */
761 BAD_SHARED_EXTRA_SURROUND = 0x10,
762 /* Volume widget is shared */
763 BAD_SHARED_VOL = 0x10,
764};
765
766/* look for widgets in the path between the given NIDs appropriate for
767 * volume and mute controls, and assign the values to ctls[].
768 *
769 * When no appropriate widget is found in the path, the badness value
770 * is incremented depending on the situation. The function returns the
771 * total badness for both volume and mute controls.
772 */
773static int assign_out_path_ctls(struct hda_codec *codec, hda_nid_t pin,
774 hda_nid_t dac)
775{
776 struct nid_path *path = snd_hda_get_nid_path(codec, dac, pin);
777 hda_nid_t nid;
778 unsigned int val;
779 int badness = 0;
780
781 if (!path)
782 return BAD_SHARED_VOL * 2;
783 nid = look_for_out_vol_nid(codec, path);
784 if (nid) {
785 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
786 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
787 badness += BAD_SHARED_VOL;
788 else
789 path->ctls[NID_PATH_VOL_CTL] = val;
790 } else
791 badness += BAD_SHARED_VOL;
792 nid = look_for_out_mute_nid(codec, path);
793 if (nid) {
794 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
795 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
796 nid_has_mute(codec, nid, HDA_OUTPUT))
797 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
798 else
799 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
800 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
801 badness += BAD_SHARED_VOL;
802 else
803 path->ctls[NID_PATH_MUTE_CTL] = val;
804 } else
805 badness += BAD_SHARED_VOL;
806 return badness;
807}
808
809struct badness_table {
810 int no_primary_dac; /* no primary DAC */
811 int no_dac; /* no secondary DACs */
812 int shared_primary; /* primary DAC is shared with main output */
813 int shared_surr; /* secondary DAC shared with main or primary */
814 int shared_clfe; /* third DAC shared with main or primary */
815 int shared_surr_main; /* secondary DAC sahred with main/DAC0 */
816};
817
818static struct badness_table main_out_badness = {
819 .no_primary_dac = BAD_NO_PRIMARY_DAC,
820 .no_dac = BAD_NO_DAC,
821 .shared_primary = BAD_NO_PRIMARY_DAC,
822 .shared_surr = BAD_SHARED_SURROUND,
823 .shared_clfe = BAD_SHARED_CLFE,
824 .shared_surr_main = BAD_SHARED_SURROUND,
825};
826
827static struct badness_table extra_out_badness = {
828 .no_primary_dac = BAD_NO_DAC,
829 .no_dac = BAD_NO_DAC,
830 .shared_primary = BAD_NO_EXTRA_DAC,
831 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
832 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
833 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
834};
835
836/* try to assign DACs to pins and return the resultant badness */
837static int try_assign_dacs(struct hda_codec *codec, int num_outs,
838 const hda_nid_t *pins, hda_nid_t *dacs,
839 const struct badness_table *bad)
840{
841 struct hda_gen_spec *spec = codec->spec;
842 struct auto_pin_cfg *cfg = &spec->autocfg;
843 int i, j;
844 int badness = 0;
845 hda_nid_t dac;
846
847 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 return 0;
849
Takashi Iwai352f7f92012-12-19 12:52:06 +0100850 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100851 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100852 hda_nid_t pin = pins[i];
Takashi Iwai1e0b5282013-01-04 12:56:52 +0100853
854 if (dacs[i]) {
855 badness += assign_out_path_ctls(codec, pin, dacs[i]);
856 continue;
857 }
858
859 dacs[i] = look_for_dac(codec, pin, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100860 if (!dacs[i] && !i) {
861 for (j = 1; j < num_outs; j++) {
862 if (is_reachable_path(codec, dacs[j], pin)) {
863 dacs[0] = dacs[j];
864 dacs[j] = 0;
865 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 }
867 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100868 }
869 dac = dacs[i];
870 if (!dac) {
871 if (is_reachable_path(codec, dacs[0], pin))
872 dac = dacs[0];
873 else if (cfg->line_outs > i &&
874 is_reachable_path(codec, spec->private_dac_nids[i], pin))
875 dac = spec->private_dac_nids[i];
876 if (dac) {
877 if (!i)
878 badness += bad->shared_primary;
879 else if (i == 1)
880 badness += bad->shared_surr;
881 else
882 badness += bad->shared_clfe;
883 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
884 dac = spec->private_dac_nids[0];
885 badness += bad->shared_surr_main;
886 } else if (!i)
887 badness += bad->no_primary_dac;
888 else
889 badness += bad->no_dac;
890 }
Takashi Iwai4ac0eef2012-12-20 18:10:51 +0100891 path = snd_hda_add_new_path(codec, dac, pin, HDA_PARSE_NO_AAMIX);
Takashi Iwaib3a8c742012-12-20 18:29:16 +0100892 if (!path && i > 0 && spec->mixer_nid) {
893 /* try with aamix */
894 path = snd_hda_add_new_path(codec, dac, pin, HDA_PARSE_ALL);
895 }
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100896 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100897 dac = dacs[i] = 0;
Takashi Iwaie1284af2013-01-03 16:33:02 +0100898 else {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100899 print_nid_path("output", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +0100900 path->active = true;
901 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100902 if (dac)
903 badness += assign_out_path_ctls(codec, pin, dac);
904 }
905
906 return badness;
907}
908
909/* return NID if the given pin has only a single connection to a certain DAC */
910static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
911{
912 struct hda_gen_spec *spec = codec->spec;
913 int i;
914 hda_nid_t nid_found = 0;
915
916 for (i = 0; i < spec->num_all_dacs; i++) {
917 hda_nid_t nid = spec->all_dacs[i];
918 if (!nid || is_dac_already_used(codec, nid))
919 continue;
920 if (is_reachable_path(codec, nid, pin)) {
921 if (nid_found)
922 return 0;
923 nid_found = nid;
924 }
925 }
926 return nid_found;
927}
928
929/* check whether the given pin can be a multi-io pin */
930static bool can_be_multiio_pin(struct hda_codec *codec,
931 unsigned int location, hda_nid_t nid)
932{
933 unsigned int defcfg, caps;
934
935 defcfg = snd_hda_codec_get_pincfg(codec, nid);
936 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
937 return false;
938 if (location && get_defcfg_location(defcfg) != location)
939 return false;
940 caps = snd_hda_query_pin_caps(codec, nid);
941 if (!(caps & AC_PINCAP_OUT))
942 return false;
943 return true;
944}
945
Takashi Iwaie22aab72013-01-04 14:50:04 +0100946/* count the number of input pins that are capable to be multi-io */
947static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
948{
949 struct hda_gen_spec *spec = codec->spec;
950 struct auto_pin_cfg *cfg = &spec->autocfg;
951 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
952 unsigned int location = get_defcfg_location(defcfg);
953 int type, i;
954 int num_pins = 0;
955
956 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
957 for (i = 0; i < cfg->num_inputs; i++) {
958 if (cfg->inputs[i].type != type)
959 continue;
960 if (can_be_multiio_pin(codec, location,
961 cfg->inputs[i].pin))
962 num_pins++;
963 }
964 }
965 return num_pins;
966}
967
Takashi Iwai352f7f92012-12-19 12:52:06 +0100968/*
969 * multi-io helper
970 *
971 * When hardwired is set, try to fill ony hardwired pins, and returns
972 * zero if any pins are filled, non-zero if nothing found.
973 * When hardwired is off, try to fill possible input pins, and returns
974 * the badness value.
975 */
976static int fill_multi_ios(struct hda_codec *codec,
977 hda_nid_t reference_pin,
Takashi Iwaie22aab72013-01-04 14:50:04 +0100978 bool hardwired)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100979{
980 struct hda_gen_spec *spec = codec->spec;
981 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie22aab72013-01-04 14:50:04 +0100982 int type, i, j, num_pins, old_pins;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100983 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
984 unsigned int location = get_defcfg_location(defcfg);
985 int badness = 0;
986
987 old_pins = spec->multi_ios;
988 if (old_pins >= 2)
989 goto end_fill;
990
Takashi Iwaie22aab72013-01-04 14:50:04 +0100991 num_pins = count_multiio_pins(codec, reference_pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100992 if (num_pins < 2)
993 goto end_fill;
994
Takashi Iwai352f7f92012-12-19 12:52:06 +0100995 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
996 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100997 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100998 hda_nid_t nid = cfg->inputs[i].pin;
999 hda_nid_t dac = 0;
1000
1001 if (cfg->inputs[i].type != type)
1002 continue;
1003 if (!can_be_multiio_pin(codec, location, nid))
1004 continue;
1005 for (j = 0; j < spec->multi_ios; j++) {
1006 if (nid == spec->multi_io[j].pin)
1007 break;
1008 }
1009 if (j < spec->multi_ios)
1010 continue;
1011
Takashi Iwai352f7f92012-12-19 12:52:06 +01001012 if (hardwired)
1013 dac = get_dac_if_single(codec, nid);
1014 else if (!dac)
1015 dac = look_for_dac(codec, nid, false);
1016 if (!dac) {
1017 badness++;
1018 continue;
1019 }
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01001020 path = snd_hda_add_new_path(codec, dac, nid, HDA_PARSE_NO_AAMIX);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001021 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001022 badness++;
1023 continue;
1024 }
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001025 print_nid_path("multiio", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001026 spec->multi_io[spec->multi_ios].pin = nid;
1027 spec->multi_io[spec->multi_ios].dac = dac;
1028 spec->multi_ios++;
1029 if (spec->multi_ios >= 2)
1030 break;
1031 }
1032 }
1033 end_fill:
1034 if (badness)
1035 badness = BAD_MULTI_IO;
1036 if (old_pins == spec->multi_ios) {
1037 if (hardwired)
1038 return 1; /* nothing found */
1039 else
1040 return badness; /* no badness if nothing found */
1041 }
1042 if (!hardwired && spec->multi_ios < 2) {
1043 /* cancel newly assigned paths */
1044 spec->paths.used -= spec->multi_ios - old_pins;
1045 spec->multi_ios = old_pins;
1046 return badness;
1047 }
1048
1049 /* assign volume and mute controls */
1050 for (i = old_pins; i < spec->multi_ios; i++)
1051 badness += assign_out_path_ctls(codec, spec->multi_io[i].pin,
1052 spec->multi_io[i].dac);
1053
1054 return badness;
1055}
1056
1057/* map DACs for all pins in the list if they are single connections */
1058static bool map_singles(struct hda_codec *codec, int outs,
1059 const hda_nid_t *pins, hda_nid_t *dacs)
1060{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001061 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001062 int i;
1063 bool found = false;
1064 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001065 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001066 hda_nid_t dac;
1067 if (dacs[i])
1068 continue;
1069 dac = get_dac_if_single(codec, pins[i]);
1070 if (!dac)
1071 continue;
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01001072 path = snd_hda_add_new_path(codec, dac, pins[i], HDA_PARSE_NO_AAMIX);
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001073 if (!path && i > 0 && spec->mixer_nid)
1074 path = snd_hda_add_new_path(codec, dac, pins[i], HDA_PARSE_ALL);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001075 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001076 dacs[i] = dac;
1077 found = true;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001078 print_nid_path("output", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001079 path->active = true;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001080 }
1081 }
1082 return found;
1083}
1084
1085/* fill in the dac_nids table from the parsed pin configuration */
1086static int fill_and_eval_dacs(struct hda_codec *codec,
1087 bool fill_hardwired,
1088 bool fill_mio_first)
1089{
1090 struct hda_gen_spec *spec = codec->spec;
1091 struct auto_pin_cfg *cfg = &spec->autocfg;
1092 int i, err, badness;
1093
1094 /* set num_dacs once to full for look_for_dac() */
1095 spec->multiout.num_dacs = cfg->line_outs;
1096 spec->multiout.dac_nids = spec->private_dac_nids;
1097 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1098 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1099 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1100 spec->multi_ios = 0;
1101 snd_array_free(&spec->paths);
1102 badness = 0;
1103
1104 /* fill hard-wired DACs first */
1105 if (fill_hardwired) {
1106 bool mapped;
1107 do {
1108 mapped = map_singles(codec, cfg->line_outs,
1109 cfg->line_out_pins,
1110 spec->private_dac_nids);
1111 mapped |= map_singles(codec, cfg->hp_outs,
1112 cfg->hp_pins,
1113 spec->multiout.hp_out_nid);
1114 mapped |= map_singles(codec, cfg->speaker_outs,
1115 cfg->speaker_pins,
1116 spec->multiout.extra_out_nid);
1117 if (fill_mio_first && cfg->line_outs == 1 &&
1118 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001119 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001120 if (!err)
1121 mapped = true;
1122 }
1123 } while (mapped);
1124 }
1125
1126 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
1127 spec->private_dac_nids,
1128 &main_out_badness);
1129
1130 /* re-count num_dacs and squash invalid entries */
1131 spec->multiout.num_dacs = 0;
1132 for (i = 0; i < cfg->line_outs; i++) {
1133 if (spec->private_dac_nids[i])
1134 spec->multiout.num_dacs++;
1135 else {
1136 memmove(spec->private_dac_nids + i,
1137 spec->private_dac_nids + i + 1,
1138 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1139 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1140 }
1141 }
1142
1143 if (fill_mio_first &&
1144 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1145 /* try to fill multi-io first */
Takashi Iwaie22aab72013-01-04 14:50:04 +01001146 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001147 if (err < 0)
1148 return err;
1149 /* we don't count badness at this stage yet */
1150 }
1151
1152 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1153 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1154 spec->multiout.hp_out_nid,
1155 &extra_out_badness);
1156 if (err < 0)
1157 return err;
1158 badness += err;
1159 }
1160 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1161 err = try_assign_dacs(codec, cfg->speaker_outs,
1162 cfg->speaker_pins,
1163 spec->multiout.extra_out_nid,
1164 &extra_out_badness);
1165 if (err < 0)
1166 return err;
1167 badness += err;
1168 }
1169 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001170 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001171 if (err < 0)
1172 return err;
1173 badness += err;
1174 }
Takashi Iwaie22aab72013-01-04 14:50:04 +01001175
1176 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1177 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1178 spec->multi_ios = 1; /* give badness */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001179
1180 if (spec->multi_ios == 2) {
1181 for (i = 0; i < 2; i++)
1182 spec->private_dac_nids[spec->multiout.num_dacs++] =
1183 spec->multi_io[i].dac;
1184 spec->ext_channel_count = 2;
1185 } else if (spec->multi_ios) {
1186 spec->multi_ios = 0;
1187 badness += BAD_MULTI_IO;
1188 }
1189
1190 return badness;
1191}
1192
1193#define DEBUG_BADNESS
1194
1195#ifdef DEBUG_BADNESS
1196#define debug_badness snd_printdd
1197#else
1198#define debug_badness(...)
1199#endif
1200
1201static void debug_show_configs(struct hda_gen_spec *spec, struct auto_pin_cfg *cfg)
1202{
1203 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1204 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001205 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001206 spec->multiout.dac_nids[0],
1207 spec->multiout.dac_nids[1],
1208 spec->multiout.dac_nids[2],
1209 spec->multiout.dac_nids[3]);
1210 if (spec->multi_ios > 0)
1211 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1212 spec->multi_ios,
1213 spec->multi_io[0].pin, spec->multi_io[1].pin,
1214 spec->multi_io[0].dac, spec->multi_io[1].dac);
1215 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1216 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001217 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001218 spec->multiout.hp_out_nid[0],
1219 spec->multiout.hp_out_nid[1],
1220 spec->multiout.hp_out_nid[2],
1221 spec->multiout.hp_out_nid[3]);
1222 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1223 cfg->speaker_pins[0], cfg->speaker_pins[1],
1224 cfg->speaker_pins[2], cfg->speaker_pins[3],
1225 spec->multiout.extra_out_nid[0],
1226 spec->multiout.extra_out_nid[1],
1227 spec->multiout.extra_out_nid[2],
1228 spec->multiout.extra_out_nid[3]);
1229}
1230
1231/* find all available DACs of the codec */
1232static void fill_all_dac_nids(struct hda_codec *codec)
1233{
1234 struct hda_gen_spec *spec = codec->spec;
1235 int i;
1236 hda_nid_t nid = codec->start_nid;
1237
1238 spec->num_all_dacs = 0;
1239 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1240 for (i = 0; i < codec->num_nodes; i++, nid++) {
1241 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1242 continue;
1243 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1244 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1245 break;
1246 }
1247 spec->all_dacs[spec->num_all_dacs++] = nid;
1248 }
1249}
1250
1251static int parse_output_paths(struct hda_codec *codec)
1252{
1253 struct hda_gen_spec *spec = codec->spec;
1254 struct auto_pin_cfg *cfg = &spec->autocfg;
1255 struct auto_pin_cfg *best_cfg;
1256 int best_badness = INT_MAX;
1257 int badness;
1258 bool fill_hardwired = true, fill_mio_first = true;
1259 bool best_wired = true, best_mio = true;
1260 bool hp_spk_swapped = false;
1261
1262 fill_all_dac_nids(codec);
1263
1264 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1265 if (!best_cfg)
1266 return -ENOMEM;
1267 *best_cfg = *cfg;
1268
1269 for (;;) {
1270 badness = fill_and_eval_dacs(codec, fill_hardwired,
1271 fill_mio_first);
1272 if (badness < 0) {
1273 kfree(best_cfg);
1274 return badness;
1275 }
1276 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1277 cfg->line_out_type, fill_hardwired, fill_mio_first,
1278 badness);
1279 debug_show_configs(spec, cfg);
1280 if (badness < best_badness) {
1281 best_badness = badness;
1282 *best_cfg = *cfg;
1283 best_wired = fill_hardwired;
1284 best_mio = fill_mio_first;
1285 }
1286 if (!badness)
1287 break;
1288 fill_mio_first = !fill_mio_first;
1289 if (!fill_mio_first)
1290 continue;
1291 fill_hardwired = !fill_hardwired;
1292 if (!fill_hardwired)
1293 continue;
1294 if (hp_spk_swapped)
1295 break;
1296 hp_spk_swapped = true;
1297 if (cfg->speaker_outs > 0 &&
1298 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1299 cfg->hp_outs = cfg->line_outs;
1300 memcpy(cfg->hp_pins, cfg->line_out_pins,
1301 sizeof(cfg->hp_pins));
1302 cfg->line_outs = cfg->speaker_outs;
1303 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1304 sizeof(cfg->speaker_pins));
1305 cfg->speaker_outs = 0;
1306 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1307 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1308 fill_hardwired = true;
1309 continue;
1310 }
1311 if (cfg->hp_outs > 0 &&
1312 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1313 cfg->speaker_outs = cfg->line_outs;
1314 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1315 sizeof(cfg->speaker_pins));
1316 cfg->line_outs = cfg->hp_outs;
1317 memcpy(cfg->line_out_pins, cfg->hp_pins,
1318 sizeof(cfg->hp_pins));
1319 cfg->hp_outs = 0;
1320 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1321 cfg->line_out_type = AUTO_PIN_HP_OUT;
1322 fill_hardwired = true;
1323 continue;
1324 }
1325 break;
1326 }
1327
1328 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001329 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001330 *cfg = *best_cfg;
1331 fill_and_eval_dacs(codec, best_wired, best_mio);
1332 }
1333 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1334 cfg->line_out_type, best_wired, best_mio);
1335 debug_show_configs(spec, cfg);
1336
1337 if (cfg->line_out_pins[0]) {
1338 struct nid_path *path;
1339 path = snd_hda_get_nid_path(codec,
1340 spec->multiout.dac_nids[0],
1341 cfg->line_out_pins[0]);
1342 if (path)
1343 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
1344 }
1345
1346 kfree(best_cfg);
1347 return 0;
1348}
1349
1350/* add playback controls from the parsed DAC table */
1351static int create_multi_out_ctls(struct hda_codec *codec,
1352 const struct auto_pin_cfg *cfg)
1353{
1354 struct hda_gen_spec *spec = codec->spec;
1355 int i, err, noutputs;
1356
1357 noutputs = cfg->line_outs;
1358 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1359 noutputs += spec->multi_ios;
1360
1361 for (i = 0; i < noutputs; i++) {
1362 const char *name;
1363 int index;
1364 hda_nid_t dac, pin;
1365 struct nid_path *path;
1366
1367 dac = spec->multiout.dac_nids[i];
1368 if (!dac)
1369 continue;
1370 if (i >= cfg->line_outs) {
1371 pin = spec->multi_io[i - 1].pin;
1372 index = 0;
1373 name = channel_name[i];
1374 } else {
1375 pin = cfg->line_out_pins[i];
1376 name = get_line_out_pfx(spec, i, true, &index);
1377 }
1378
1379 path = snd_hda_get_nid_path(codec, dac, pin);
1380 if (!path)
1381 continue;
1382 if (!name || !strcmp(name, "CLFE")) {
1383 /* Center/LFE */
1384 err = add_vol_ctl(codec, "Center", 0, 1, path);
1385 if (err < 0)
1386 return err;
1387 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1388 if (err < 0)
1389 return err;
1390 err = add_sw_ctl(codec, "Center", 0, 1, path);
1391 if (err < 0)
1392 return err;
1393 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1394 if (err < 0)
1395 return err;
1396 } else {
1397 err = add_stereo_vol(codec, name, index, path);
1398 if (err < 0)
1399 return err;
1400 err = add_stereo_sw(codec, name, index, path);
1401 if (err < 0)
1402 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 }
1404 }
1405 return 0;
1406}
1407
Takashi Iwai352f7f92012-12-19 12:52:06 +01001408static int create_extra_out(struct hda_codec *codec, hda_nid_t pin,
1409 hda_nid_t dac, const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001411 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 int err;
1413
Takashi Iwai352f7f92012-12-19 12:52:06 +01001414 path = snd_hda_get_nid_path(codec, dac, pin);
1415 if (!path)
1416 return 0;
1417 /* bind volume control will be created in the case of dac = 0 */
1418 if (dac) {
1419 err = add_stereo_vol(codec, pfx, cidx, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 if (err < 0)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001421 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001423 err = add_stereo_sw(codec, pfx, cidx, path);
1424 if (err < 0)
1425 return err;
1426 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427}
1428
Takashi Iwai352f7f92012-12-19 12:52:06 +01001429/* add playback controls for speaker and HP outputs */
1430static int create_extra_outs(struct hda_codec *codec, int num_pins,
1431 const hda_nid_t *pins, const hda_nid_t *dacs,
1432 const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001434 struct hda_gen_spec *spec = codec->spec;
1435 struct hda_bind_ctls *ctl;
1436 char name[32];
1437 int i, n, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Takashi Iwai352f7f92012-12-19 12:52:06 +01001439 if (!num_pins || !pins[0])
1440 return 0;
1441
1442 if (num_pins == 1) {
1443 hda_nid_t dac = *dacs;
1444 if (!dac)
1445 dac = spec->multiout.dac_nids[0];
1446 return create_extra_out(codec, *pins, dac, pfx, 0);
Takashi Iwai97ec5582006-03-21 11:29:07 +01001447 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001448
1449 for (i = 0; i < num_pins; i++) {
1450 hda_nid_t dac;
1451 if (dacs[num_pins - 1])
1452 dac = dacs[i]; /* with individual volumes */
Takashi Iwai97ec5582006-03-21 11:29:07 +01001453 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01001454 dac = 0;
1455 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker")) {
1456 err = create_extra_out(codec, pins[i], dac,
1457 "Bass Speaker", 0);
1458 } else if (num_pins >= 3) {
1459 snprintf(name, sizeof(name), "%s %s",
1460 pfx, channel_name[i]);
1461 err = create_extra_out(codec, pins[i], dac, name, 0);
1462 } else {
1463 err = create_extra_out(codec, pins[i], dac, pfx, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001465 if (err < 0)
1466 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001468 if (dacs[num_pins - 1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 return 0;
1470
Takashi Iwai352f7f92012-12-19 12:52:06 +01001471 /* Let's create a bind-controls for volumes */
1472 ctl = new_bind_ctl(codec, num_pins, &snd_hda_bind_vol);
1473 if (!ctl)
1474 return -ENOMEM;
1475 n = 0;
1476 for (i = 0; i < num_pins; i++) {
1477 hda_nid_t vol;
1478 struct nid_path *path;
1479 if (!pins[i] || !dacs[i])
1480 continue;
1481 path = snd_hda_get_nid_path(codec, dacs[i], pins[i]);
1482 if (!path)
1483 continue;
1484 vol = look_for_out_vol_nid(codec, path);
1485 if (vol)
1486 ctl->values[n++] =
1487 HDA_COMPOSE_AMP_VAL(vol, 3, 0, HDA_OUTPUT);
1488 }
1489 if (n) {
1490 snprintf(name, sizeof(name), "%s Playback Volume", pfx);
1491 err = add_control(spec, HDA_CTL_BIND_VOL, name, 0, (long)ctl);
1492 if (err < 0)
1493 return err;
1494 }
1495 return 0;
1496}
Takashi Iwai97ec5582006-03-21 11:29:07 +01001497
Takashi Iwai352f7f92012-12-19 12:52:06 +01001498static int create_hp_out_ctls(struct hda_codec *codec)
1499{
1500 struct hda_gen_spec *spec = codec->spec;
1501 return create_extra_outs(codec, spec->autocfg.hp_outs,
1502 spec->autocfg.hp_pins,
1503 spec->multiout.hp_out_nid,
1504 "Headphone");
1505}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
Takashi Iwai352f7f92012-12-19 12:52:06 +01001507static int create_speaker_out_ctls(struct hda_codec *codec)
1508{
1509 struct hda_gen_spec *spec = codec->spec;
1510 return create_extra_outs(codec, spec->autocfg.speaker_outs,
1511 spec->autocfg.speaker_pins,
1512 spec->multiout.extra_out_nid,
1513 "Speaker");
1514}
1515
1516/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001517 * independent HP controls
1518 */
1519
1520static int indep_hp_info(struct snd_kcontrol *kcontrol,
1521 struct snd_ctl_elem_info *uinfo)
1522{
1523 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
1524}
1525
1526static int indep_hp_get(struct snd_kcontrol *kcontrol,
1527 struct snd_ctl_elem_value *ucontrol)
1528{
1529 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1530 struct hda_gen_spec *spec = codec->spec;
1531 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
1532 return 0;
1533}
1534
1535static int indep_hp_put(struct snd_kcontrol *kcontrol,
1536 struct snd_ctl_elem_value *ucontrol)
1537{
1538 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1539 struct hda_gen_spec *spec = codec->spec;
1540 unsigned int select = ucontrol->value.enumerated.item[0];
1541 int ret = 0;
1542
1543 mutex_lock(&spec->pcm_mutex);
1544 if (spec->active_streams) {
1545 ret = -EBUSY;
1546 goto unlock;
1547 }
1548
1549 if (spec->indep_hp_enabled != select) {
1550 spec->indep_hp_enabled = select;
1551 if (spec->indep_hp_enabled)
1552 spec->multiout.hp_out_nid[0] = 0;
1553 else
1554 spec->multiout.hp_out_nid[0] = spec->alt_dac_nid;
1555 ret = 1;
1556 }
1557 unlock:
1558 mutex_unlock(&spec->pcm_mutex);
1559 return ret;
1560}
1561
1562static const struct snd_kcontrol_new indep_hp_ctl = {
1563 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1564 .name = "Independent HP",
1565 .info = indep_hp_info,
1566 .get = indep_hp_get,
1567 .put = indep_hp_put,
1568};
1569
1570
1571static int create_indep_hp_ctls(struct hda_codec *codec)
1572{
1573 struct hda_gen_spec *spec = codec->spec;
1574
1575 if (!spec->indep_hp)
1576 return 0;
1577 if (!spec->multiout.hp_out_nid[0]) {
1578 spec->indep_hp = 0;
1579 return 0;
1580 }
1581
1582 spec->indep_hp_enabled = false;
1583 spec->alt_dac_nid = spec->multiout.hp_out_nid[0];
1584 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
1585 return -ENOMEM;
1586 return 0;
1587}
1588
1589/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01001590 * channel mode enum control
1591 */
1592
1593static int ch_mode_info(struct snd_kcontrol *kcontrol,
1594 struct snd_ctl_elem_info *uinfo)
1595{
1596 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1597 struct hda_gen_spec *spec = codec->spec;
1598
1599 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1600 uinfo->count = 1;
1601 uinfo->value.enumerated.items = spec->multi_ios + 1;
1602 if (uinfo->value.enumerated.item > spec->multi_ios)
1603 uinfo->value.enumerated.item = spec->multi_ios;
1604 sprintf(uinfo->value.enumerated.name, "%dch",
1605 (uinfo->value.enumerated.item + 1) * 2);
1606 return 0;
1607}
1608
1609static int ch_mode_get(struct snd_kcontrol *kcontrol,
1610 struct snd_ctl_elem_value *ucontrol)
1611{
1612 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1613 struct hda_gen_spec *spec = codec->spec;
1614 ucontrol->value.enumerated.item[0] = (spec->ext_channel_count - 1) / 2;
1615 return 0;
1616}
1617
1618static int set_multi_io(struct hda_codec *codec, int idx, bool output)
1619{
1620 struct hda_gen_spec *spec = codec->spec;
1621 hda_nid_t nid = spec->multi_io[idx].pin;
1622 struct nid_path *path;
1623
1624 path = snd_hda_get_nid_path(codec, spec->multi_io[idx].dac, nid);
1625 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001627
1628 if (path->active == output)
1629 return 0;
1630
1631 if (output) {
1632 snd_hda_set_pin_ctl_cache(codec, nid, PIN_OUT);
1633 snd_hda_activate_path(codec, path, true, true);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01001634 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001635 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01001636 set_pin_eapd(codec, nid, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001637 snd_hda_activate_path(codec, path, false, true);
1638 snd_hda_set_pin_ctl_cache(codec, nid,
1639 spec->multi_io[idx].ctl_in);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001641 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642}
1643
Takashi Iwai352f7f92012-12-19 12:52:06 +01001644static int ch_mode_put(struct snd_kcontrol *kcontrol,
1645 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001647 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1648 struct hda_gen_spec *spec = codec->spec;
1649 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
Takashi Iwai352f7f92012-12-19 12:52:06 +01001651 ch = ucontrol->value.enumerated.item[0];
1652 if (ch < 0 || ch > spec->multi_ios)
1653 return -EINVAL;
1654 if (ch == (spec->ext_channel_count - 1) / 2)
1655 return 0;
1656 spec->ext_channel_count = (ch + 1) * 2;
1657 for (i = 0; i < spec->multi_ios; i++)
1658 set_multi_io(codec, i, i < ch);
1659 spec->multiout.max_channels = max(spec->ext_channel_count,
1660 spec->const_channel_count);
1661 if (spec->need_dac_fix)
1662 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 return 1;
1664}
1665
Takashi Iwai352f7f92012-12-19 12:52:06 +01001666static const struct snd_kcontrol_new channel_mode_enum = {
1667 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1668 .name = "Channel Mode",
1669 .info = ch_mode_info,
1670 .get = ch_mode_get,
1671 .put = ch_mode_put,
1672};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
Takashi Iwai352f7f92012-12-19 12:52:06 +01001674static int create_multi_channel_mode(struct hda_codec *codec)
1675{
1676 struct hda_gen_spec *spec = codec->spec;
1677
1678 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01001679 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01001680 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 return 0;
1683}
1684
Takashi Iwai352f7f92012-12-19 12:52:06 +01001685/*
1686 * shared headphone/mic handling
1687 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02001688
Takashi Iwai352f7f92012-12-19 12:52:06 +01001689static void call_update_outputs(struct hda_codec *codec);
1690
1691/* for shared I/O, change the pin-control accordingly */
1692static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic)
1693{
1694 struct hda_gen_spec *spec = codec->spec;
1695 unsigned int val;
1696 hda_nid_t pin = spec->autocfg.inputs[1].pin;
1697 /* NOTE: this assumes that there are only two inputs, the
1698 * first is the real internal mic and the second is HP/mic jack.
1699 */
1700
1701 val = snd_hda_get_default_vref(codec, pin);
1702
1703 /* This pin does not have vref caps - let's enable vref on pin 0x18
1704 instead, as suggested by Realtek */
1705 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
1706 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
1707 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
1708 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01001709 snd_hda_set_pin_ctl_cache(codec, vref_pin,
1710 PIN_IN | (set_as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02001711 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001712
1713 val = set_as_mic ? val | PIN_IN : PIN_HP;
Takashi Iwai7594aa32012-12-20 15:38:40 +01001714 snd_hda_set_pin_ctl_cache(codec, pin, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001715
1716 spec->automute_speaker = !set_as_mic;
1717 call_update_outputs(codec);
1718}
1719
1720/* create a shared input with the headphone out */
1721static int create_shared_input(struct hda_codec *codec)
1722{
1723 struct hda_gen_spec *spec = codec->spec;
1724 struct auto_pin_cfg *cfg = &spec->autocfg;
1725 unsigned int defcfg;
1726 hda_nid_t nid;
1727
1728 /* only one internal input pin? */
1729 if (cfg->num_inputs != 1)
1730 return 0;
1731 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
1732 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
1733 return 0;
1734
1735 if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1736 nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
1737 else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
1738 nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
1739 else
1740 return 0; /* both not available */
1741
1742 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
1743 return 0; /* no input */
1744
1745 cfg->inputs[1].pin = nid;
1746 cfg->inputs[1].type = AUTO_PIN_MIC;
1747 cfg->num_inputs = 2;
1748 spec->shared_mic_hp = 1;
1749 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
1750 return 0;
1751}
1752
1753
1754/*
1755 * Parse input paths
1756 */
1757
1758#ifdef CONFIG_PM
1759/* add the powersave loopback-list entry */
1760static void add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
1761{
1762 struct hda_amp_list *list;
1763
1764 if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
1765 return;
1766 list = spec->loopback_list + spec->num_loopbacks;
1767 list->nid = mix;
1768 list->dir = HDA_INPUT;
1769 list->idx = idx;
1770 spec->num_loopbacks++;
Takashi Iwaicb53c622007-08-10 17:21:45 +02001771 spec->loopback.amplist = spec->loopback_list;
1772}
1773#else
Takashi Iwai352f7f92012-12-19 12:52:06 +01001774#define add_loopback_list(spec, mix, idx) /* NOP */
Takashi Iwaicb53c622007-08-10 17:21:45 +02001775#endif
1776
Takashi Iwai352f7f92012-12-19 12:52:06 +01001777/* create input playback/capture controls for the given pin */
1778static int new_analog_input(struct hda_codec *codec, hda_nid_t pin,
1779 const char *ctlname, int ctlidx,
1780 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001782 struct hda_gen_spec *spec = codec->spec;
1783 struct nid_path *path;
1784 unsigned int val;
1785 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
Takashi Iwai352f7f92012-12-19 12:52:06 +01001787 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
1788 !nid_has_mute(codec, mix_nid, HDA_INPUT))
1789 return 0; /* no need for analog loopback */
1790
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01001791 path = snd_hda_add_new_path(codec, pin, mix_nid, HDA_PARSE_ALL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001792 if (!path)
1793 return -EINVAL;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001794 print_nid_path("loopback", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001795
1796 idx = path->idx[path->depth - 1];
1797 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
1798 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1799 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001800 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001802 path->ctls[NID_PATH_VOL_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 }
1804
Takashi Iwai352f7f92012-12-19 12:52:06 +01001805 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
1806 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1807 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001808 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001810 path->ctls[NID_PATH_MUTE_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 }
1812
Takashi Iwai352f7f92012-12-19 12:52:06 +01001813 path->active = true;
1814 add_loopback_list(spec, mix_nid, idx);
1815 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816}
1817
Takashi Iwai352f7f92012-12-19 12:52:06 +01001818static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001820 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
1821 return (pincap & AC_PINCAP_IN) != 0;
1822}
1823
1824/* Parse the codec tree and retrieve ADCs */
1825static int fill_adc_nids(struct hda_codec *codec)
1826{
1827 struct hda_gen_spec *spec = codec->spec;
1828 hda_nid_t nid;
1829 hda_nid_t *adc_nids = spec->adc_nids;
1830 int max_nums = ARRAY_SIZE(spec->adc_nids);
1831 int i, nums = 0;
1832
1833 nid = codec->start_nid;
1834 for (i = 0; i < codec->num_nodes; i++, nid++) {
1835 unsigned int caps = get_wcaps(codec, nid);
1836 int type = get_wcaps_type(caps);
1837
1838 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
1839 continue;
1840 adc_nids[nums] = nid;
1841 if (++nums >= max_nums)
1842 break;
1843 }
1844 spec->num_adc_nids = nums;
1845 return nums;
1846}
1847
1848/* filter out invalid adc_nids that don't give all active input pins;
1849 * if needed, check whether dynamic ADC-switching is available
1850 */
1851static int check_dyn_adc_switch(struct hda_codec *codec)
1852{
1853 struct hda_gen_spec *spec = codec->spec;
1854 struct hda_input_mux *imux = &spec->input_mux;
1855 hda_nid_t adc_nids[ARRAY_SIZE(spec->adc_nids)];
1856 int i, n, nums;
1857 hda_nid_t pin, adc;
1858
1859 again:
1860 nums = 0;
1861 for (n = 0; n < spec->num_adc_nids; n++) {
1862 adc = spec->adc_nids[n];
1863 for (i = 0; i < imux->num_items; i++) {
1864 pin = spec->imux_pins[i];
1865 if (!is_reachable_path(codec, pin, adc))
1866 break;
1867 }
1868 if (i >= imux->num_items)
1869 adc_nids[nums++] = adc;
1870 }
1871
1872 if (!nums) {
1873 if (spec->shared_mic_hp) {
1874 spec->shared_mic_hp = 0;
1875 imux->num_items = 1;
1876 goto again;
1877 }
1878
1879 /* check whether ADC-switch is possible */
1880 for (i = 0; i < imux->num_items; i++) {
1881 pin = spec->imux_pins[i];
1882 for (n = 0; n < spec->num_adc_nids; n++) {
1883 adc = spec->adc_nids[n];
1884 if (is_reachable_path(codec, pin, adc)) {
1885 spec->dyn_adc_idx[i] = n;
1886 break;
1887 }
1888 }
1889 }
1890
1891 snd_printdd("hda-codec: enabling ADC switching\n");
1892 spec->dyn_adc_switch = 1;
1893 } else if (nums != spec->num_adc_nids) {
1894 memcpy(spec->adc_nids, adc_nids, nums * sizeof(hda_nid_t));
1895 spec->num_adc_nids = nums;
1896 }
1897
1898 if (imux->num_items == 1 || spec->shared_mic_hp) {
1899 snd_printdd("hda-codec: reducing to a single ADC\n");
1900 spec->num_adc_nids = 1; /* reduce to a single ADC */
1901 }
1902
1903 /* single index for individual volumes ctls */
1904 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
1905 spec->num_adc_nids = 1;
1906
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 return 0;
1908}
1909
1910/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01001911 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001913static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02001914{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001915 struct hda_gen_spec *spec = codec->spec;
1916 const struct auto_pin_cfg *cfg = &spec->autocfg;
1917 hda_nid_t mixer = spec->mixer_nid;
1918 struct hda_input_mux *imux = &spec->input_mux;
1919 int num_adcs;
1920 int i, c, err, type_idx = 0;
1921 const char *prev_label = NULL;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02001922
Takashi Iwai352f7f92012-12-19 12:52:06 +01001923 num_adcs = fill_adc_nids(codec);
1924 if (num_adcs < 0)
1925 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02001926
Takashi Iwai352f7f92012-12-19 12:52:06 +01001927 for (i = 0; i < cfg->num_inputs; i++) {
1928 hda_nid_t pin;
1929 const char *label;
1930 bool imux_added;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
Takashi Iwai352f7f92012-12-19 12:52:06 +01001932 pin = cfg->inputs[i].pin;
1933 if (!is_input_pin(codec, pin))
1934 continue;
1935
1936 label = hda_get_autocfg_input_label(codec, cfg, i);
1937 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
1938 label = "Headphone Mic";
1939 if (prev_label && !strcmp(label, prev_label))
1940 type_idx++;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02001941 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01001942 type_idx = 0;
1943 prev_label = label;
1944
1945 if (mixer) {
1946 if (is_reachable_path(codec, pin, mixer)) {
1947 err = new_analog_input(codec, pin,
1948 label, type_idx, mixer);
1949 if (err < 0)
1950 return err;
1951 }
1952 }
1953
1954 imux_added = false;
1955 for (c = 0; c < num_adcs; c++) {
1956 struct nid_path *path;
1957 hda_nid_t adc = spec->adc_nids[c];
1958
1959 if (!is_reachable_path(codec, pin, adc))
1960 continue;
1961 path = snd_array_new(&spec->paths);
1962 if (!path)
1963 return -ENOMEM;
1964 memset(path, 0, sizeof(*path));
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01001965 if (!snd_hda_parse_nid_path(codec, pin, adc, HDA_PARSE_ALL, path)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001966 snd_printd(KERN_ERR
1967 "invalid input path 0x%x -> 0x%x\n",
1968 pin, adc);
1969 spec->paths.used--;
1970 continue;
1971 }
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001972 print_nid_path("input", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001973
1974 if (!imux_added) {
1975 spec->imux_pins[imux->num_items] = pin;
1976 snd_hda_add_imux_item(imux, label,
1977 imux->num_items, NULL);
1978 imux_added = true;
1979 }
1980 }
1981 }
1982
1983 return 0;
1984}
1985
1986
1987/*
1988 * input source mux
1989 */
1990
1991/* get the ADC NID corresponding to the given index */
1992static hda_nid_t get_adc_nid(struct hda_codec *codec, int adc_idx, int imux_idx)
1993{
1994 struct hda_gen_spec *spec = codec->spec;
1995 if (spec->dyn_adc_switch)
1996 adc_idx = spec->dyn_adc_idx[imux_idx];
1997 return spec->adc_nids[adc_idx];
1998}
1999
2000static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2001 unsigned int idx);
2002
2003static int mux_enum_info(struct snd_kcontrol *kcontrol,
2004 struct snd_ctl_elem_info *uinfo)
2005{
2006 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2007 struct hda_gen_spec *spec = codec->spec;
2008 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
2009}
2010
2011static int mux_enum_get(struct snd_kcontrol *kcontrol,
2012 struct snd_ctl_elem_value *ucontrol)
2013{
2014 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2015 struct hda_gen_spec *spec = codec->spec;
2016 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2017
2018 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
2019 return 0;
2020}
2021
2022static int mux_enum_put(struct snd_kcontrol *kcontrol,
2023 struct snd_ctl_elem_value *ucontrol)
2024{
2025 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2026 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2027 return mux_select(codec, adc_idx,
2028 ucontrol->value.enumerated.item[0]);
2029}
2030
Takashi Iwai352f7f92012-12-19 12:52:06 +01002031static const struct snd_kcontrol_new cap_src_temp = {
2032 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2033 .name = "Input Source",
2034 .info = mux_enum_info,
2035 .get = mux_enum_get,
2036 .put = mux_enum_put,
2037};
2038
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002039/*
2040 * capture volume and capture switch ctls
2041 */
2042
Takashi Iwai352f7f92012-12-19 12:52:06 +01002043typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
2044 struct snd_ctl_elem_value *ucontrol);
2045
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002046/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002047static int cap_put_caller(struct snd_kcontrol *kcontrol,
2048 struct snd_ctl_elem_value *ucontrol,
2049 put_call_t func, int type)
2050{
2051 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2052 struct hda_gen_spec *spec = codec->spec;
2053 const struct hda_input_mux *imux;
2054 struct nid_path *path;
2055 int i, adc_idx, err = 0;
2056
2057 imux = &spec->input_mux;
2058 adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2059 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002060 /* we use the cache-only update at first since multiple input paths
2061 * may shared the same amp; by updating only caches, the redundant
2062 * writes to hardware can be reduced.
2063 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002064 codec->cached_write = 1;
2065 for (i = 0; i < imux->num_items; i++) {
2066 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2067 get_adc_nid(codec, adc_idx, i));
2068 if (!path->ctls[type])
2069 continue;
2070 kcontrol->private_value = path->ctls[type];
2071 err = func(kcontrol, ucontrol);
2072 if (err < 0)
2073 goto error;
2074 }
2075 error:
2076 codec->cached_write = 0;
2077 mutex_unlock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002078 snd_hda_codec_flush_amp_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002079 if (err >= 0 && spec->cap_sync_hook)
2080 spec->cap_sync_hook(codec);
2081 return err;
2082}
2083
2084/* capture volume ctl callbacks */
2085#define cap_vol_info snd_hda_mixer_amp_volume_info
2086#define cap_vol_get snd_hda_mixer_amp_volume_get
2087#define cap_vol_tlv snd_hda_mixer_amp_tlv
2088
2089static int cap_vol_put(struct snd_kcontrol *kcontrol,
2090 struct snd_ctl_elem_value *ucontrol)
2091{
2092 return cap_put_caller(kcontrol, ucontrol,
2093 snd_hda_mixer_amp_volume_put,
2094 NID_PATH_VOL_CTL);
2095}
2096
2097static const struct snd_kcontrol_new cap_vol_temp = {
2098 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2099 .name = "Capture Volume",
2100 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
2101 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2102 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
2103 .info = cap_vol_info,
2104 .get = cap_vol_get,
2105 .put = cap_vol_put,
2106 .tlv = { .c = cap_vol_tlv },
2107};
2108
2109/* capture switch ctl callbacks */
2110#define cap_sw_info snd_ctl_boolean_stereo_info
2111#define cap_sw_get snd_hda_mixer_amp_switch_get
2112
2113static int cap_sw_put(struct snd_kcontrol *kcontrol,
2114 struct snd_ctl_elem_value *ucontrol)
2115{
2116 return cap_put_caller(kcontrol, ucontrol,
2117 snd_hda_mixer_amp_switch_put,
2118 NID_PATH_MUTE_CTL);
2119}
2120
2121static const struct snd_kcontrol_new cap_sw_temp = {
2122 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2123 .name = "Capture Switch",
2124 .info = cap_sw_info,
2125 .get = cap_sw_get,
2126 .put = cap_sw_put,
2127};
2128
2129static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
2130{
2131 hda_nid_t nid;
2132 int i, depth;
2133
2134 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
2135 for (depth = 0; depth < 3; depth++) {
2136 if (depth >= path->depth)
2137 return -EINVAL;
2138 i = path->depth - depth - 1;
2139 nid = path->path[i];
2140 if (!path->ctls[NID_PATH_VOL_CTL]) {
2141 if (nid_has_volume(codec, nid, HDA_OUTPUT))
2142 path->ctls[NID_PATH_VOL_CTL] =
2143 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2144 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
2145 int idx = path->idx[i];
2146 if (!depth && codec->single_adc_amp)
2147 idx = 0;
2148 path->ctls[NID_PATH_VOL_CTL] =
2149 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2150 }
2151 }
2152 if (!path->ctls[NID_PATH_MUTE_CTL]) {
2153 if (nid_has_mute(codec, nid, HDA_OUTPUT))
2154 path->ctls[NID_PATH_MUTE_CTL] =
2155 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2156 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
2157 int idx = path->idx[i];
2158 if (!depth && codec->single_adc_amp)
2159 idx = 0;
2160 path->ctls[NID_PATH_MUTE_CTL] =
2161 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2162 }
2163 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01002164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 return 0;
2166}
2167
Takashi Iwai352f7f92012-12-19 12:52:06 +01002168static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002170 struct hda_gen_spec *spec = codec->spec;
2171 struct auto_pin_cfg *cfg = &spec->autocfg;
2172 unsigned int val;
2173 int i;
2174
2175 if (!spec->inv_dmic_split)
2176 return false;
2177 for (i = 0; i < cfg->num_inputs; i++) {
2178 if (cfg->inputs[i].pin != nid)
2179 continue;
2180 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2181 return false;
2182 val = snd_hda_codec_get_pincfg(codec, nid);
2183 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
2184 }
2185 return false;
2186}
2187
2188static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
2189 int idx, bool is_switch, unsigned int ctl,
2190 bool inv_dmic)
2191{
2192 struct hda_gen_spec *spec = codec->spec;
2193 char tmpname[44];
2194 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
2195 const char *sfx = is_switch ? "Switch" : "Volume";
2196 unsigned int chs = inv_dmic ? 1 : 3;
2197 int err;
2198
2199 if (!ctl)
2200 return 0;
2201
2202 if (label)
2203 snprintf(tmpname, sizeof(tmpname),
2204 "%s Capture %s", label, sfx);
2205 else
2206 snprintf(tmpname, sizeof(tmpname),
2207 "Capture %s", sfx);
2208 err = add_control(spec, type, tmpname, idx,
2209 amp_val_replace_channels(ctl, chs));
2210 if (err < 0 || !inv_dmic)
2211 return err;
2212
2213 /* Make independent right kcontrol */
2214 if (label)
2215 snprintf(tmpname, sizeof(tmpname),
2216 "Inverted %s Capture %s", label, sfx);
2217 else
2218 snprintf(tmpname, sizeof(tmpname),
2219 "Inverted Capture %s", sfx);
2220 return add_control(spec, type, tmpname, idx,
2221 amp_val_replace_channels(ctl, 2));
2222}
2223
2224/* create single (and simple) capture volume and switch controls */
2225static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
2226 unsigned int vol_ctl, unsigned int sw_ctl,
2227 bool inv_dmic)
2228{
2229 int err;
2230 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
2231 if (err < 0)
2232 return err;
2233 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
2234 if (err < 0)
2235 return err;
2236 return 0;
2237}
2238
2239/* create bound capture volume and switch controls */
2240static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
2241 unsigned int vol_ctl, unsigned int sw_ctl)
2242{
2243 struct hda_gen_spec *spec = codec->spec;
2244 struct snd_kcontrol_new *knew;
2245
2246 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002247 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002248 if (!knew)
2249 return -ENOMEM;
2250 knew->index = idx;
2251 knew->private_value = vol_ctl;
2252 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2253 }
2254 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002255 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002256 if (!knew)
2257 return -ENOMEM;
2258 knew->index = idx;
2259 knew->private_value = sw_ctl;
2260 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2261 }
2262 return 0;
2263}
2264
2265/* return the vol ctl when used first in the imux list */
2266static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
2267{
2268 struct hda_gen_spec *spec = codec->spec;
2269 struct nid_path *path;
2270 unsigned int ctl;
2271 int i;
2272
2273 path = snd_hda_get_nid_path(codec, spec->imux_pins[idx],
2274 get_adc_nid(codec, 0, idx));
2275 if (!path)
2276 return 0;
2277 ctl = path->ctls[type];
2278 if (!ctl)
2279 return 0;
2280 for (i = 0; i < idx - 1; i++) {
2281 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2282 get_adc_nid(codec, 0, i));
2283 if (path && path->ctls[type] == ctl)
2284 return 0;
2285 }
2286 return ctl;
2287}
2288
2289/* create individual capture volume and switch controls per input */
2290static int create_multi_cap_vol_ctl(struct hda_codec *codec)
2291{
2292 struct hda_gen_spec *spec = codec->spec;
2293 struct hda_input_mux *imux = &spec->input_mux;
2294 int i, err, type, type_idx = 0;
2295 const char *prev_label = NULL;
2296
2297 for (i = 0; i < imux->num_items; i++) {
2298 const char *label;
2299 bool inv_dmic;
2300 label = hda_get_autocfg_input_label(codec, &spec->autocfg, i);
2301 if (prev_label && !strcmp(label, prev_label))
2302 type_idx++;
2303 else
2304 type_idx = 0;
2305 prev_label = label;
2306 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
2307
2308 for (type = 0; type < 2; type++) {
2309 err = add_single_cap_ctl(codec, label, type_idx, type,
2310 get_first_cap_ctl(codec, i, type),
2311 inv_dmic);
2312 if (err < 0)
2313 return err;
2314 }
2315 }
2316 return 0;
2317}
2318
2319static int create_capture_mixers(struct hda_codec *codec)
2320{
2321 struct hda_gen_spec *spec = codec->spec;
2322 struct hda_input_mux *imux = &spec->input_mux;
2323 int i, n, nums, err;
2324
2325 if (spec->dyn_adc_switch)
2326 nums = 1;
2327 else
2328 nums = spec->num_adc_nids;
2329
2330 if (!spec->auto_mic && imux->num_items > 1) {
2331 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01002332 const char *name;
2333 name = nums > 1 ? "Input Source" : "Capture Source";
2334 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002335 if (!knew)
2336 return -ENOMEM;
2337 knew->count = nums;
2338 }
2339
2340 for (n = 0; n < nums; n++) {
2341 bool multi = false;
2342 bool inv_dmic = false;
2343 int vol, sw;
2344
2345 vol = sw = 0;
2346 for (i = 0; i < imux->num_items; i++) {
2347 struct nid_path *path;
2348 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2349 get_adc_nid(codec, n, i));
2350 if (!path)
2351 continue;
2352 parse_capvol_in_path(codec, path);
2353 if (!vol)
2354 vol = path->ctls[NID_PATH_VOL_CTL];
2355 else if (vol != path->ctls[NID_PATH_VOL_CTL])
2356 multi = true;
2357 if (!sw)
2358 sw = path->ctls[NID_PATH_MUTE_CTL];
2359 else if (sw != path->ctls[NID_PATH_MUTE_CTL])
2360 multi = true;
2361 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
2362 inv_dmic = true;
2363 }
2364
2365 if (!multi)
2366 err = create_single_cap_vol_ctl(codec, n, vol, sw,
2367 inv_dmic);
2368 else if (!spec->multi_cap_vol)
2369 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
2370 else
2371 err = create_multi_cap_vol_ctl(codec);
2372 if (err < 0)
2373 return err;
2374 }
2375
2376 return 0;
2377}
2378
2379/*
2380 * add mic boosts if needed
2381 */
2382static int parse_mic_boost(struct hda_codec *codec)
2383{
2384 struct hda_gen_spec *spec = codec->spec;
2385 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002386 int i, err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002387 int type_idx = 0;
2388 hda_nid_t nid;
2389 const char *prev_label = NULL;
2390
2391 for (i = 0; i < cfg->num_inputs; i++) {
2392 if (cfg->inputs[i].type > AUTO_PIN_MIC)
2393 break;
2394 nid = cfg->inputs[i].pin;
2395 if (get_wcaps(codec, nid) & AC_WCAP_IN_AMP) {
2396 const char *label;
2397 char boost_label[32];
2398 struct nid_path *path;
2399 unsigned int val;
2400
2401 label = hda_get_autocfg_input_label(codec, cfg, i);
2402 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
2403 label = "Headphone Mic";
2404 if (prev_label && !strcmp(label, prev_label))
2405 type_idx++;
2406 else
2407 type_idx = 0;
2408 prev_label = label;
2409
2410 snprintf(boost_label, sizeof(boost_label),
2411 "%s Boost Volume", label);
2412 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
2413 err = add_control(spec, HDA_CTL_WIDGET_VOL,
2414 boost_label, type_idx, val);
2415 if (err < 0)
2416 return err;
2417
2418 path = snd_hda_get_nid_path(codec, nid, 0);
2419 if (path)
2420 path->ctls[NID_PATH_BOOST_CTL] = val;
2421 }
2422 }
2423 return 0;
2424}
2425
2426/*
2427 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
2428 */
2429static void parse_digital(struct hda_codec *codec)
2430{
2431 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002432 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002433 int i, nums;
2434 hda_nid_t dig_nid;
2435
2436 /* support multiple SPDIFs; the secondary is set up as a slave */
2437 nums = 0;
2438 for (i = 0; i < spec->autocfg.dig_outs; i++) {
2439 hda_nid_t pin = spec->autocfg.dig_out_pins[i];
2440 dig_nid = look_for_dac(codec, pin, true);
2441 if (!dig_nid)
2442 continue;
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01002443 path = snd_hda_add_new_path(codec, dig_nid, pin, HDA_PARSE_ALL);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002444 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002445 continue;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002446 print_nid_path("digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01002447 path->active = true;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002448 if (!nums) {
2449 spec->multiout.dig_out_nid = dig_nid;
2450 spec->dig_out_type = spec->autocfg.dig_out_type[0];
2451 } else {
2452 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
2453 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
2454 break;
2455 spec->slave_dig_outs[nums - 1] = dig_nid;
2456 }
2457 nums++;
2458 }
2459
2460 if (spec->autocfg.dig_in_pin) {
2461 dig_nid = codec->start_nid;
2462 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002463 unsigned int wcaps = get_wcaps(codec, dig_nid);
2464 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
2465 continue;
2466 if (!(wcaps & AC_WCAP_DIGITAL))
2467 continue;
2468 path = snd_hda_add_new_path(codec,
2469 spec->autocfg.dig_in_pin,
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01002470 dig_nid, HDA_PARSE_ALL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002471 if (path) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002472 print_nid_path("digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002473 path->active = true;
2474 spec->dig_in_nid = dig_nid;
2475 break;
2476 }
2477 }
2478 }
2479}
2480
2481
2482/*
2483 * input MUX handling
2484 */
2485
2486static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
2487
2488/* select the given imux item; either unmute exclusively or select the route */
2489static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2490 unsigned int idx)
2491{
2492 struct hda_gen_spec *spec = codec->spec;
2493 const struct hda_input_mux *imux;
2494 struct nid_path *path;
2495
2496 imux = &spec->input_mux;
2497 if (!imux->num_items)
2498 return 0;
2499
2500 if (idx >= imux->num_items)
2501 idx = imux->num_items - 1;
2502 if (spec->cur_mux[adc_idx] == idx)
2503 return 0;
2504
2505 path = snd_hda_get_nid_path(codec,
2506 spec->imux_pins[spec->cur_mux[adc_idx]],
2507 spec->adc_nids[adc_idx]);
2508 if (!path)
2509 return 0;
2510 if (path->active)
2511 snd_hda_activate_path(codec, path, false, false);
2512
2513 spec->cur_mux[adc_idx] = idx;
2514
2515 if (spec->shared_mic_hp)
2516 update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
2517
2518 if (spec->dyn_adc_switch)
2519 dyn_adc_pcm_resetup(codec, idx);
2520
2521 path = snd_hda_get_nid_path(codec, spec->imux_pins[idx],
2522 get_adc_nid(codec, adc_idx, idx));
2523 if (!path)
2524 return 0;
2525 if (path->active)
2526 return 0;
2527 snd_hda_activate_path(codec, path, true, false);
2528 if (spec->cap_sync_hook)
2529 spec->cap_sync_hook(codec);
2530 return 1;
2531}
2532
2533
2534/*
2535 * Jack detections for HP auto-mute and mic-switch
2536 */
2537
2538/* check each pin in the given array; returns true if any of them is plugged */
2539static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
2540{
2541 int i, present = 0;
2542
2543 for (i = 0; i < num_pins; i++) {
2544 hda_nid_t nid = pins[i];
2545 if (!nid)
2546 break;
2547 present |= snd_hda_jack_detect(codec, nid);
2548 }
2549 return present;
2550}
2551
2552/* standard HP/line-out auto-mute helper */
2553static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
2554 bool mute, bool hp_out)
2555{
2556 struct hda_gen_spec *spec = codec->spec;
2557 unsigned int pin_bits = mute ? 0 : (hp_out ? PIN_HP : PIN_OUT);
2558 int i;
2559
2560 for (i = 0; i < num_pins; i++) {
2561 hda_nid_t nid = pins[i];
2562 unsigned int val;
2563 if (!nid)
2564 break;
2565 /* don't reset VREF value in case it's controlling
2566 * the amp (see alc861_fixup_asus_amp_vref_0f())
2567 */
2568 if (spec->keep_vref_in_automute) {
2569 val = snd_hda_codec_read(codec, nid, 0,
2570 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2571 val &= ~PIN_HP;
2572 } else
2573 val = 0;
2574 val |= pin_bits;
Takashi Iwai7594aa32012-12-20 15:38:40 +01002575 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002576 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002577 }
2578}
2579
2580/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002581void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002582{
2583 struct hda_gen_spec *spec = codec->spec;
2584 int on;
2585
2586 /* Control HP pins/amps depending on master_mute state;
2587 * in general, HP pins/amps control should be enabled in all cases,
2588 * but currently set only for master_mute, just to be safe
2589 */
2590 if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
2591 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2592 spec->autocfg.hp_pins, spec->master_mute, true);
2593
2594 if (!spec->automute_speaker)
2595 on = 0;
2596 else
2597 on = spec->hp_jack_present | spec->line_jack_present;
2598 on |= spec->master_mute;
2599 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
2600 spec->autocfg.speaker_pins, on, false);
2601
2602 /* toggle line-out mutes if needed, too */
2603 /* if LO is a copy of either HP or Speaker, don't need to handle it */
2604 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
2605 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
2606 return;
2607 if (!spec->automute_lo)
2608 on = 0;
2609 else
2610 on = spec->hp_jack_present;
2611 on |= spec->master_mute;
2612 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2613 spec->autocfg.line_out_pins, on, false);
2614}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002615EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002616
2617static void call_update_outputs(struct hda_codec *codec)
2618{
2619 struct hda_gen_spec *spec = codec->spec;
2620 if (spec->automute_hook)
2621 spec->automute_hook(codec);
2622 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01002623 snd_hda_gen_update_outputs(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002624}
2625
2626/* standard HP-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002627void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002628{
2629 struct hda_gen_spec *spec = codec->spec;
2630
2631 spec->hp_jack_present =
2632 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2633 spec->autocfg.hp_pins);
2634 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
2635 return;
2636 call_update_outputs(codec);
2637}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002638EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002639
2640/* standard line-out-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002641void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002642{
2643 struct hda_gen_spec *spec = codec->spec;
2644
2645 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
2646 return;
2647 /* check LO jack only when it's different from HP */
2648 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
2649 return;
2650
2651 spec->line_jack_present =
2652 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2653 spec->autocfg.line_out_pins);
2654 if (!spec->automute_speaker || !spec->detect_lo)
2655 return;
2656 call_update_outputs(codec);
2657}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002658EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002659
2660/* standard mic auto-switch helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002661void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002662{
2663 struct hda_gen_spec *spec = codec->spec;
2664 int i;
2665
2666 if (!spec->auto_mic)
2667 return;
2668
2669 for (i = spec->am_num_entries - 1; i > 0; i--) {
2670 if (snd_hda_jack_detect(codec, spec->am_entry[i].pin)) {
2671 mux_select(codec, 0, spec->am_entry[i].idx);
2672 return;
2673 }
2674 }
2675 mux_select(codec, 0, spec->am_entry[0].idx);
2676}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002677EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002678
2679/*
2680 * Auto-Mute mode mixer enum support
2681 */
2682static int automute_mode_info(struct snd_kcontrol *kcontrol,
2683 struct snd_ctl_elem_info *uinfo)
2684{
2685 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2686 struct hda_gen_spec *spec = codec->spec;
2687 static const char * const texts3[] = {
2688 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02002689 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690
Takashi Iwai352f7f92012-12-19 12:52:06 +01002691 if (spec->automute_speaker_possible && spec->automute_lo_possible)
2692 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
2693 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2694}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695
Takashi Iwai352f7f92012-12-19 12:52:06 +01002696static int automute_mode_get(struct snd_kcontrol *kcontrol,
2697 struct snd_ctl_elem_value *ucontrol)
2698{
2699 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2700 struct hda_gen_spec *spec = codec->spec;
2701 unsigned int val = 0;
2702 if (spec->automute_speaker)
2703 val++;
2704 if (spec->automute_lo)
2705 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002706
Takashi Iwai352f7f92012-12-19 12:52:06 +01002707 ucontrol->value.enumerated.item[0] = val;
2708 return 0;
2709}
2710
2711static int automute_mode_put(struct snd_kcontrol *kcontrol,
2712 struct snd_ctl_elem_value *ucontrol)
2713{
2714 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2715 struct hda_gen_spec *spec = codec->spec;
2716
2717 switch (ucontrol->value.enumerated.item[0]) {
2718 case 0:
2719 if (!spec->automute_speaker && !spec->automute_lo)
2720 return 0;
2721 spec->automute_speaker = 0;
2722 spec->automute_lo = 0;
2723 break;
2724 case 1:
2725 if (spec->automute_speaker_possible) {
2726 if (!spec->automute_lo && spec->automute_speaker)
2727 return 0;
2728 spec->automute_speaker = 1;
2729 spec->automute_lo = 0;
2730 } else if (spec->automute_lo_possible) {
2731 if (spec->automute_lo)
2732 return 0;
2733 spec->automute_lo = 1;
2734 } else
2735 return -EINVAL;
2736 break;
2737 case 2:
2738 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
2739 return -EINVAL;
2740 if (spec->automute_speaker && spec->automute_lo)
2741 return 0;
2742 spec->automute_speaker = 1;
2743 spec->automute_lo = 1;
2744 break;
2745 default:
2746 return -EINVAL;
2747 }
2748 call_update_outputs(codec);
2749 return 1;
2750}
2751
2752static const struct snd_kcontrol_new automute_mode_enum = {
2753 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2754 .name = "Auto-Mute Mode",
2755 .info = automute_mode_info,
2756 .get = automute_mode_get,
2757 .put = automute_mode_put,
2758};
2759
2760static int add_automute_mode_enum(struct hda_codec *codec)
2761{
2762 struct hda_gen_spec *spec = codec->spec;
2763
Takashi Iwai12c93df2012-12-19 14:38:33 +01002764 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002765 return -ENOMEM;
2766 return 0;
2767}
2768
2769/*
2770 * Check the availability of HP/line-out auto-mute;
2771 * Set up appropriately if really supported
2772 */
2773static int check_auto_mute_availability(struct hda_codec *codec)
2774{
2775 struct hda_gen_spec *spec = codec->spec;
2776 struct auto_pin_cfg *cfg = &spec->autocfg;
2777 int present = 0;
2778 int i, err;
2779
2780 if (cfg->hp_pins[0])
2781 present++;
2782 if (cfg->line_out_pins[0])
2783 present++;
2784 if (cfg->speaker_pins[0])
2785 present++;
2786 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02002787 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002788
2789 if (!cfg->speaker_pins[0] &&
2790 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
2791 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2792 sizeof(cfg->speaker_pins));
2793 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795
Takashi Iwai352f7f92012-12-19 12:52:06 +01002796 if (!cfg->hp_pins[0] &&
2797 cfg->line_out_type == AUTO_PIN_HP_OUT) {
2798 memcpy(cfg->hp_pins, cfg->line_out_pins,
2799 sizeof(cfg->hp_pins));
2800 cfg->hp_outs = cfg->line_outs;
2801 }
2802
2803 for (i = 0; i < cfg->hp_outs; i++) {
2804 hda_nid_t nid = cfg->hp_pins[i];
2805 if (!is_jack_detectable(codec, nid))
2806 continue;
2807 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
2808 nid);
2809 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01002810 spec->hp_automute_hook ?
2811 spec->hp_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01002812 snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002813 spec->detect_hp = 1;
2814 }
2815
2816 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
2817 if (cfg->speaker_outs)
2818 for (i = 0; i < cfg->line_outs; i++) {
2819 hda_nid_t nid = cfg->line_out_pins[i];
2820 if (!is_jack_detectable(codec, nid))
2821 continue;
2822 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
2823 snd_hda_jack_detect_enable_callback(codec, nid,
2824 HDA_GEN_FRONT_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01002825 spec->line_automute_hook ?
2826 spec->line_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01002827 snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002828 spec->detect_lo = 1;
2829 }
2830 spec->automute_lo_possible = spec->detect_hp;
2831 }
2832
2833 spec->automute_speaker_possible = cfg->speaker_outs &&
2834 (spec->detect_hp || spec->detect_lo);
2835
2836 spec->automute_lo = spec->automute_lo_possible;
2837 spec->automute_speaker = spec->automute_speaker_possible;
2838
2839 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
2840 /* create a control for automute mode */
2841 err = add_automute_mode_enum(codec);
2842 if (err < 0)
2843 return err;
2844 }
2845 return 0;
2846}
2847
2848/* return the position of NID in the list, or -1 if not found */
2849static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
2850{
2851 int i;
2852 for (i = 0; i < nums; i++)
2853 if (list[i] == nid)
2854 return i;
2855 return -1;
2856}
2857
2858/* check whether all auto-mic pins are valid; setup indices if OK */
2859static bool auto_mic_check_imux(struct hda_codec *codec)
2860{
2861 struct hda_gen_spec *spec = codec->spec;
2862 const struct hda_input_mux *imux;
2863 int i;
2864
2865 imux = &spec->input_mux;
2866 for (i = 0; i < spec->am_num_entries; i++) {
2867 spec->am_entry[i].idx =
2868 find_idx_in_nid_list(spec->am_entry[i].pin,
2869 spec->imux_pins, imux->num_items);
2870 if (spec->am_entry[i].idx < 0)
2871 return false; /* no corresponding imux */
2872 }
2873
2874 /* we don't need the jack detection for the first pin */
2875 for (i = 1; i < spec->am_num_entries; i++)
2876 snd_hda_jack_detect_enable_callback(codec,
2877 spec->am_entry[i].pin,
2878 HDA_GEN_MIC_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01002879 spec->mic_autoswitch_hook ?
2880 spec->mic_autoswitch_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01002881 snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002882 return true;
2883}
2884
2885static int compare_attr(const void *ap, const void *bp)
2886{
2887 const struct automic_entry *a = ap;
2888 const struct automic_entry *b = bp;
2889 return (int)(a->attr - b->attr);
2890}
2891
2892/*
2893 * Check the availability of auto-mic switch;
2894 * Set up if really supported
2895 */
2896static int check_auto_mic_availability(struct hda_codec *codec)
2897{
2898 struct hda_gen_spec *spec = codec->spec;
2899 struct auto_pin_cfg *cfg = &spec->autocfg;
2900 unsigned int types;
2901 int i, num_pins;
2902
2903 types = 0;
2904 num_pins = 0;
2905 for (i = 0; i < cfg->num_inputs; i++) {
2906 hda_nid_t nid = cfg->inputs[i].pin;
2907 unsigned int attr;
2908 attr = snd_hda_codec_get_pincfg(codec, nid);
2909 attr = snd_hda_get_input_pin_attr(attr);
2910 if (types & (1 << attr))
2911 return 0; /* already occupied */
2912 switch (attr) {
2913 case INPUT_PIN_ATTR_INT:
2914 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2915 return 0; /* invalid type */
2916 break;
2917 case INPUT_PIN_ATTR_UNUSED:
2918 return 0; /* invalid entry */
2919 default:
2920 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
2921 return 0; /* invalid type */
2922 if (!spec->line_in_auto_switch &&
2923 cfg->inputs[i].type != AUTO_PIN_MIC)
2924 return 0; /* only mic is allowed */
2925 if (!is_jack_detectable(codec, nid))
2926 return 0; /* no unsol support */
2927 break;
2928 }
2929 if (num_pins >= MAX_AUTO_MIC_PINS)
2930 return 0;
2931 types |= (1 << attr);
2932 spec->am_entry[num_pins].pin = nid;
2933 spec->am_entry[num_pins].attr = attr;
2934 num_pins++;
2935 }
2936
2937 if (num_pins < 2)
2938 return 0;
2939
2940 spec->am_num_entries = num_pins;
2941 /* sort the am_entry in the order of attr so that the pin with a
2942 * higher attr will be selected when the jack is plugged.
2943 */
2944 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
2945 compare_attr, NULL);
2946
2947 if (!auto_mic_check_imux(codec))
2948 return 0;
2949
2950 spec->auto_mic = 1;
2951 spec->num_adc_nids = 1;
2952 spec->cur_mux[0] = spec->am_entry[0].idx;
2953 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
2954 spec->am_entry[0].pin,
2955 spec->am_entry[1].pin,
2956 spec->am_entry[2].pin);
2957
2958 return 0;
2959}
2960
2961
Takashi Iwai9eb413e2012-12-19 14:41:21 +01002962/*
2963 * Parse the given BIOS configuration and set up the hda_gen_spec
2964 *
2965 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002966 * or a negative error code
2967 */
2968int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01002969 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002970{
2971 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002972 int err;
2973
Takashi Iwai9eb413e2012-12-19 14:41:21 +01002974 if (cfg != &spec->autocfg) {
2975 spec->autocfg = *cfg;
2976 cfg = &spec->autocfg;
2977 }
2978
Takashi Iwai352f7f92012-12-19 12:52:06 +01002979 if (!cfg->line_outs) {
2980 if (cfg->dig_outs || cfg->dig_in_pin) {
2981 spec->multiout.max_channels = 2;
2982 spec->no_analog = 1;
2983 goto dig_only;
2984 }
2985 return 0; /* can't find valid BIOS pin config */
2986 }
2987
2988 if (!spec->no_primary_hp &&
2989 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
2990 cfg->line_outs <= cfg->hp_outs) {
2991 /* use HP as primary out */
2992 cfg->speaker_outs = cfg->line_outs;
2993 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2994 sizeof(cfg->speaker_pins));
2995 cfg->line_outs = cfg->hp_outs;
2996 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
2997 cfg->hp_outs = 0;
2998 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
2999 cfg->line_out_type = AUTO_PIN_HP_OUT;
3000 }
3001
3002 err = parse_output_paths(codec);
3003 if (err < 0)
3004 return err;
3005 err = create_multi_channel_mode(codec);
3006 if (err < 0)
3007 return err;
3008 err = create_multi_out_ctls(codec, cfg);
3009 if (err < 0)
3010 return err;
3011 err = create_hp_out_ctls(codec);
3012 if (err < 0)
3013 return err;
3014 err = create_speaker_out_ctls(codec);
3015 if (err < 0)
3016 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003017 err = create_indep_hp_ctls(codec);
3018 if (err < 0)
3019 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003020 err = create_shared_input(codec);
3021 if (err < 0)
3022 return err;
3023 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003024 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02003025 return err;
3026
Takashi Iwai352f7f92012-12-19 12:52:06 +01003027 /* check the multiple speaker pins */
3028 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
3029 spec->const_channel_count = cfg->line_outs * 2;
3030 else
3031 spec->const_channel_count = cfg->speaker_outs * 2;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003032
Takashi Iwai352f7f92012-12-19 12:52:06 +01003033 if (spec->multi_ios > 0)
3034 spec->multiout.max_channels = max(spec->ext_channel_count,
3035 spec->const_channel_count);
3036 else
3037 spec->multiout.max_channels = spec->multiout.num_dacs * 2;
3038
3039 err = check_auto_mute_availability(codec);
3040 if (err < 0)
3041 return err;
3042
3043 err = check_dyn_adc_switch(codec);
3044 if (err < 0)
3045 return err;
3046
3047 if (!spec->shared_mic_hp) {
3048 err = check_auto_mic_availability(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003049 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 }
Takashi Iwai071c73a2006-08-23 18:34:06 +02003052
Takashi Iwai352f7f92012-12-19 12:52:06 +01003053 err = create_capture_mixers(codec);
3054 if (err < 0)
3055 return err;
3056
3057 err = parse_mic_boost(codec);
3058 if (err < 0)
3059 return err;
3060
3061 dig_only:
3062 parse_digital(codec);
3063
3064 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003066EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067
3068
3069/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003070 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003072
3073/* slave controls for virtual master */
3074static const char * const slave_pfxs[] = {
3075 "Front", "Surround", "Center", "LFE", "Side",
3076 "Headphone", "Speaker", "Mono", "Line Out",
3077 "CLFE", "Bass Speaker", "PCM",
3078 NULL,
3079};
3080
3081int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003083 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085
Takashi Iwai36502d02012-12-19 15:15:10 +01003086 if (spec->kctls.used) {
3087 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
3088 if (err < 0)
3089 return err;
3090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091
Takashi Iwai352f7f92012-12-19 12:52:06 +01003092 if (spec->multiout.dig_out_nid) {
3093 err = snd_hda_create_dig_out_ctls(codec,
3094 spec->multiout.dig_out_nid,
3095 spec->multiout.dig_out_nid,
3096 spec->pcm_rec[1].pcm_type);
3097 if (err < 0)
3098 return err;
3099 if (!spec->no_analog) {
3100 err = snd_hda_create_spdif_share_sw(codec,
3101 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 if (err < 0)
3103 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003104 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 }
3106 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003107 if (spec->dig_in_nid) {
3108 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
3109 if (err < 0)
3110 return err;
3111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112
Takashi Iwai352f7f92012-12-19 12:52:06 +01003113 /* if we have no master control, let's create it */
3114 if (!spec->no_analog &&
3115 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
3116 unsigned int vmaster_tlv[4];
3117 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
3118 HDA_OUTPUT, vmaster_tlv);
3119 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
3120 vmaster_tlv, slave_pfxs,
3121 "Playback Volume");
3122 if (err < 0)
3123 return err;
3124 }
3125 if (!spec->no_analog &&
3126 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
3127 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
3128 NULL, slave_pfxs,
3129 "Playback Switch",
3130 true, &spec->vmaster_mute.sw_kctl);
3131 if (err < 0)
3132 return err;
3133 if (spec->vmaster_mute.hook)
Takashi Iwaifd25a972012-12-20 14:57:18 +01003134 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
3135 spec->vmaster_mute_enum);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137
Takashi Iwai352f7f92012-12-19 12:52:06 +01003138 free_kctls(spec); /* no longer needed */
3139
3140 if (spec->shared_mic_hp) {
3141 int err;
3142 int nid = spec->autocfg.inputs[1].pin;
3143 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
3144 if (err < 0)
3145 return err;
3146 err = snd_hda_jack_detect_enable(codec, nid, 0);
3147 if (err < 0)
3148 return err;
3149 }
3150
3151 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
3152 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 return err;
3154
3155 return 0;
3156}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003157EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
3158
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159
3160/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003161 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163
Takashi Iwai352f7f92012-12-19 12:52:06 +01003164/*
3165 * Analog playback callbacks
3166 */
3167static int playback_pcm_open(struct hda_pcm_stream *hinfo,
3168 struct hda_codec *codec,
3169 struct snd_pcm_substream *substream)
3170{
3171 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003172 int err;
3173
3174 mutex_lock(&spec->pcm_mutex);
3175 err = snd_hda_multi_out_analog_open(codec,
3176 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003177 hinfo);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003178 if (!err)
3179 spec->active_streams |= 1 << STREAM_MULTI_OUT;
3180 mutex_unlock(&spec->pcm_mutex);
3181 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003182}
3183
3184static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01003185 struct hda_codec *codec,
3186 unsigned int stream_tag,
3187 unsigned int format,
3188 struct snd_pcm_substream *substream)
3189{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003190 struct hda_gen_spec *spec = codec->spec;
3191 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
3192 stream_tag, format, substream);
3193}
Takashi Iwai97ec5582006-03-21 11:29:07 +01003194
Takashi Iwai352f7f92012-12-19 12:52:06 +01003195static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3196 struct hda_codec *codec,
3197 struct snd_pcm_substream *substream)
3198{
3199 struct hda_gen_spec *spec = codec->spec;
3200 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
3201}
3202
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003203static int playback_pcm_close(struct hda_pcm_stream *hinfo,
3204 struct hda_codec *codec,
3205 struct snd_pcm_substream *substream)
3206{
3207 struct hda_gen_spec *spec = codec->spec;
3208 mutex_lock(&spec->pcm_mutex);
3209 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
3210 mutex_unlock(&spec->pcm_mutex);
3211 return 0;
3212}
3213
3214static int alt_playback_pcm_open(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 int err = 0;
3220
3221 mutex_lock(&spec->pcm_mutex);
3222 if (!spec->indep_hp_enabled)
3223 err = -EBUSY;
3224 else
3225 spec->active_streams |= 1 << STREAM_INDEP_HP;
3226 mutex_unlock(&spec->pcm_mutex);
3227 return err;
3228}
3229
3230static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
3231 struct hda_codec *codec,
3232 struct snd_pcm_substream *substream)
3233{
3234 struct hda_gen_spec *spec = codec->spec;
3235 mutex_lock(&spec->pcm_mutex);
3236 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
3237 mutex_unlock(&spec->pcm_mutex);
3238 return 0;
3239}
3240
Takashi Iwai352f7f92012-12-19 12:52:06 +01003241/*
3242 * Digital out
3243 */
3244static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
3245 struct hda_codec *codec,
3246 struct snd_pcm_substream *substream)
3247{
3248 struct hda_gen_spec *spec = codec->spec;
3249 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3250}
3251
3252static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3253 struct hda_codec *codec,
3254 unsigned int stream_tag,
3255 unsigned int format,
3256 struct snd_pcm_substream *substream)
3257{
3258 struct hda_gen_spec *spec = codec->spec;
3259 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3260 stream_tag, format, substream);
3261}
3262
3263static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3264 struct hda_codec *codec,
3265 struct snd_pcm_substream *substream)
3266{
3267 struct hda_gen_spec *spec = codec->spec;
3268 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
3269}
3270
3271static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
3272 struct hda_codec *codec,
3273 struct snd_pcm_substream *substream)
3274{
3275 struct hda_gen_spec *spec = codec->spec;
3276 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3277}
3278
3279/*
3280 * Analog capture
3281 */
3282static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3283 struct hda_codec *codec,
3284 unsigned int stream_tag,
3285 unsigned int format,
3286 struct snd_pcm_substream *substream)
3287{
3288 struct hda_gen_spec *spec = codec->spec;
3289
3290 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01003291 stream_tag, 0, format);
3292 return 0;
3293}
3294
Takashi Iwai352f7f92012-12-19 12:52:06 +01003295static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3296 struct hda_codec *codec,
3297 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01003298{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003299 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01003300
Takashi Iwai352f7f92012-12-19 12:52:06 +01003301 snd_hda_codec_cleanup_stream(codec,
3302 spec->adc_nids[substream->number + 1]);
Takashi Iwai97ec5582006-03-21 11:29:07 +01003303 return 0;
3304}
3305
Takashi Iwai352f7f92012-12-19 12:52:06 +01003306/*
3307 */
3308static const struct hda_pcm_stream pcm_analog_playback = {
3309 .substreams = 1,
3310 .channels_min = 2,
3311 .channels_max = 8,
3312 /* NID is set in build_pcms */
3313 .ops = {
3314 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003315 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003316 .prepare = playback_pcm_prepare,
3317 .cleanup = playback_pcm_cleanup
3318 },
3319};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320
Takashi Iwai352f7f92012-12-19 12:52:06 +01003321static const struct hda_pcm_stream pcm_analog_capture = {
3322 .substreams = 1,
3323 .channels_min = 2,
3324 .channels_max = 2,
3325 /* NID is set in build_pcms */
3326};
3327
3328static const struct hda_pcm_stream pcm_analog_alt_playback = {
3329 .substreams = 1,
3330 .channels_min = 2,
3331 .channels_max = 2,
3332 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003333 .ops = {
3334 .open = alt_playback_pcm_open,
3335 .close = alt_playback_pcm_close
3336 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01003337};
3338
3339static const struct hda_pcm_stream pcm_analog_alt_capture = {
3340 .substreams = 2, /* can be overridden */
3341 .channels_min = 2,
3342 .channels_max = 2,
3343 /* NID is set in build_pcms */
3344 .ops = {
3345 .prepare = alt_capture_pcm_prepare,
3346 .cleanup = alt_capture_pcm_cleanup
3347 },
3348};
3349
3350static const struct hda_pcm_stream pcm_digital_playback = {
3351 .substreams = 1,
3352 .channels_min = 2,
3353 .channels_max = 2,
3354 /* NID is set in build_pcms */
3355 .ops = {
3356 .open = dig_playback_pcm_open,
3357 .close = dig_playback_pcm_close,
3358 .prepare = dig_playback_pcm_prepare,
3359 .cleanup = dig_playback_pcm_cleanup
3360 },
3361};
3362
3363static const struct hda_pcm_stream pcm_digital_capture = {
3364 .substreams = 1,
3365 .channels_min = 2,
3366 .channels_max = 2,
3367 /* NID is set in build_pcms */
3368};
3369
3370/* Used by build_pcms to flag that a PCM has no playback stream */
3371static const struct hda_pcm_stream pcm_null_stream = {
3372 .substreams = 0,
3373 .channels_min = 0,
3374 .channels_max = 0,
3375};
3376
3377/*
3378 * dynamic changing ADC PCM streams
3379 */
3380static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
3381{
3382 struct hda_gen_spec *spec = codec->spec;
3383 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
3384
3385 if (spec->cur_adc && spec->cur_adc != new_adc) {
3386 /* stream is running, let's swap the current ADC */
3387 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
3388 spec->cur_adc = new_adc;
3389 snd_hda_codec_setup_stream(codec, new_adc,
3390 spec->cur_adc_stream_tag, 0,
3391 spec->cur_adc_format);
3392 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003394 return false;
3395}
3396
3397/* analog capture with dynamic dual-adc changes */
3398static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3399 struct hda_codec *codec,
3400 unsigned int stream_tag,
3401 unsigned int format,
3402 struct snd_pcm_substream *substream)
3403{
3404 struct hda_gen_spec *spec = codec->spec;
3405 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
3406 spec->cur_adc_stream_tag = stream_tag;
3407 spec->cur_adc_format = format;
3408 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
3409 return 0;
3410}
3411
3412static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3413 struct hda_codec *codec,
3414 struct snd_pcm_substream *substream)
3415{
3416 struct hda_gen_spec *spec = codec->spec;
3417 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
3418 spec->cur_adc = 0;
3419 return 0;
3420}
3421
3422static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
3423 .substreams = 1,
3424 .channels_min = 2,
3425 .channels_max = 2,
3426 .nid = 0, /* fill later */
3427 .ops = {
3428 .prepare = dyn_adc_capture_pcm_prepare,
3429 .cleanup = dyn_adc_capture_pcm_cleanup
3430 },
3431};
3432
Takashi Iwaif873e532012-12-20 16:58:39 +01003433static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
3434 const char *chip_name)
3435{
3436 char *p;
3437
3438 if (*str)
3439 return;
3440 strlcpy(str, chip_name, len);
3441
3442 /* drop non-alnum chars after a space */
3443 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
3444 if (!isalnum(p[1])) {
3445 *p = 0;
3446 break;
3447 }
3448 }
3449 strlcat(str, sfx, len);
3450}
3451
Takashi Iwai352f7f92012-12-19 12:52:06 +01003452/* build PCM streams based on the parsed results */
3453int snd_hda_gen_build_pcms(struct hda_codec *codec)
3454{
3455 struct hda_gen_spec *spec = codec->spec;
3456 struct hda_pcm *info = spec->pcm_rec;
3457 const struct hda_pcm_stream *p;
3458 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459
3460 codec->num_pcms = 1;
3461 codec->pcm_info = info;
3462
Takashi Iwai352f7f92012-12-19 12:52:06 +01003463 if (spec->no_analog)
3464 goto skip_analog;
3465
Takashi Iwaif873e532012-12-20 16:58:39 +01003466 fill_pcm_stream_name(spec->stream_name_analog,
3467 sizeof(spec->stream_name_analog),
3468 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003469 info->name = spec->stream_name_analog;
3470
3471 if (spec->multiout.num_dacs > 0) {
3472 p = spec->stream_analog_playback;
3473 if (!p)
3474 p = &pcm_analog_playback;
3475 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3476 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
3477 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
3478 spec->multiout.max_channels;
3479 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
3480 spec->autocfg.line_outs == 2)
3481 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
3482 snd_pcm_2_1_chmaps;
3483 }
3484 if (spec->num_adc_nids) {
3485 p = spec->stream_analog_capture;
3486 if (!p) {
3487 if (spec->dyn_adc_switch)
3488 p = &dyn_adc_pcm_analog_capture;
3489 else
3490 p = &pcm_analog_capture;
3491 }
3492 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3493 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
3494 }
3495
Takashi Iwai352f7f92012-12-19 12:52:06 +01003496 skip_analog:
3497 /* SPDIF for stream index #1 */
3498 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01003499 fill_pcm_stream_name(spec->stream_name_digital,
3500 sizeof(spec->stream_name_digital),
3501 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003502 codec->num_pcms = 2;
3503 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
3504 info = spec->pcm_rec + 1;
3505 info->name = spec->stream_name_digital;
3506 if (spec->dig_out_type)
3507 info->pcm_type = spec->dig_out_type;
3508 else
3509 info->pcm_type = HDA_PCM_TYPE_SPDIF;
3510 if (spec->multiout.dig_out_nid) {
3511 p = spec->stream_digital_playback;
3512 if (!p)
3513 p = &pcm_digital_playback;
3514 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3515 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
3516 }
3517 if (spec->dig_in_nid) {
3518 p = spec->stream_digital_capture;
3519 if (!p)
3520 p = &pcm_digital_capture;
3521 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3522 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
3523 }
3524 }
3525
3526 if (spec->no_analog)
3527 return 0;
3528
3529 /* If the use of more than one ADC is requested for the current
3530 * model, configure a second analog capture-only PCM.
3531 */
3532 have_multi_adcs = (spec->num_adc_nids > 1) &&
3533 !spec->dyn_adc_switch && !spec->auto_mic;
3534 /* Additional Analaog capture for index #2 */
3535 if (spec->alt_dac_nid || have_multi_adcs) {
3536 codec->num_pcms = 3;
3537 info = spec->pcm_rec + 2;
3538 info->name = spec->stream_name_analog;
3539 if (spec->alt_dac_nid) {
3540 p = spec->stream_analog_alt_playback;
3541 if (!p)
3542 p = &pcm_analog_alt_playback;
3543 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3544 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
3545 spec->alt_dac_nid;
3546 } else {
3547 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
3548 pcm_null_stream;
3549 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
3550 }
3551 if (have_multi_adcs) {
3552 p = spec->stream_analog_alt_capture;
3553 if (!p)
3554 p = &pcm_analog_alt_capture;
3555 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3556 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
3557 spec->adc_nids[1];
3558 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
3559 spec->num_adc_nids - 1;
3560 } else {
3561 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
3562 pcm_null_stream;
3563 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
3564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 }
3566
3567 return 0;
3568}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003569EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
3570
3571
3572/*
3573 * Standard auto-parser initializations
3574 */
3575
3576/* configure the path from the given dac to the pin as the proper output */
3577static void set_output_and_unmute(struct hda_codec *codec, hda_nid_t pin,
3578 int pin_type, hda_nid_t dac)
3579{
3580 struct nid_path *path;
3581
3582 snd_hda_set_pin_ctl_cache(codec, pin, pin_type);
3583 path = snd_hda_get_nid_path(codec, dac, pin);
3584 if (!path)
3585 return;
Takashi Iwaie1284af2013-01-03 16:33:02 +01003586 snd_hda_activate_path(codec, path, path->active, true);
3587 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003588}
3589
3590/* initialize primary output paths */
3591static void init_multi_out(struct hda_codec *codec)
3592{
3593 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai64049c82012-12-20 15:29:21 +01003594 hda_nid_t nid, dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003595 int pin_type;
3596 int i;
3597
3598 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
3599 pin_type = PIN_HP;
3600 else
3601 pin_type = PIN_OUT;
3602
Takashi Iwai64049c82012-12-20 15:29:21 +01003603 for (i = 0; i < spec->autocfg.line_outs; i++) {
3604 nid = spec->autocfg.line_out_pins[i];
3605 if (nid) {
3606 dac = spec->multiout.dac_nids[i];
3607 if (!dac)
3608 dac = spec->multiout.dac_nids[0];
3609 set_output_and_unmute(codec, nid, pin_type, dac);
3610 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003611 }
3612}
3613
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003614
3615static void __init_extra_out(struct hda_codec *codec, int num_outs,
3616 hda_nid_t *pins, hda_nid_t *dacs, int type)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003617{
3618 struct hda_gen_spec *spec = codec->spec;
3619 int i;
3620 hda_nid_t pin, dac;
3621
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003622 for (i = 0; i < num_outs; i++) {
3623 pin = pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003624 if (!pin)
3625 break;
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003626 dac = dacs[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003627 if (!dac) {
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003628 if (i > 0 && dacs[0])
3629 dac = dacs[0];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003630 else
3631 dac = spec->multiout.dac_nids[0];
3632 }
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003633 set_output_and_unmute(codec, pin, type, dac);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003634 }
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003635}
3636
3637/* initialize hp and speaker paths */
3638static void init_extra_out(struct hda_codec *codec)
3639{
3640 struct hda_gen_spec *spec = codec->spec;
3641
3642 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
3643 __init_extra_out(codec, spec->autocfg.hp_outs,
3644 spec->autocfg.hp_pins,
3645 spec->multiout.hp_out_nid, PIN_HP);
3646 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
3647 __init_extra_out(codec, spec->autocfg.speaker_outs,
3648 spec->autocfg.speaker_pins,
3649 spec->multiout.extra_out_nid, PIN_OUT);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003650}
3651
3652/* initialize multi-io paths */
3653static void init_multi_io(struct hda_codec *codec)
3654{
3655 struct hda_gen_spec *spec = codec->spec;
3656 int i;
3657
3658 for (i = 0; i < spec->multi_ios; i++) {
3659 hda_nid_t pin = spec->multi_io[i].pin;
3660 struct nid_path *path;
3661 path = snd_hda_get_nid_path(codec, spec->multi_io[i].dac, pin);
3662 if (!path)
3663 continue;
3664 if (!spec->multi_io[i].ctl_in)
3665 spec->multi_io[i].ctl_in =
3666 snd_hda_codec_update_cache(codec, pin, 0,
3667 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3668 snd_hda_activate_path(codec, path, path->active, true);
3669 }
3670}
3671
3672/* set up the input pin config, depending on the given auto-pin type */
3673static void set_input_pin(struct hda_codec *codec, hda_nid_t nid,
3674 int auto_pin_type)
3675{
3676 unsigned int val = PIN_IN;
3677 if (auto_pin_type == AUTO_PIN_MIC)
3678 val |= snd_hda_get_default_vref(codec, nid);
Takashi Iwai7594aa32012-12-20 15:38:40 +01003679 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003680}
3681
3682/* set up input pins and loopback paths */
3683static void init_analog_input(struct hda_codec *codec)
3684{
3685 struct hda_gen_spec *spec = codec->spec;
3686 struct auto_pin_cfg *cfg = &spec->autocfg;
3687 int i;
3688
3689 for (i = 0; i < cfg->num_inputs; i++) {
3690 hda_nid_t nid = cfg->inputs[i].pin;
3691 if (is_input_pin(codec, nid))
3692 set_input_pin(codec, nid, cfg->inputs[i].type);
3693
3694 /* init loopback inputs */
3695 if (spec->mixer_nid) {
3696 struct nid_path *path;
3697 path = snd_hda_get_nid_path(codec, nid, spec->mixer_nid);
3698 if (path)
3699 snd_hda_activate_path(codec, path,
3700 path->active, false);
3701 }
3702 }
3703}
3704
3705/* initialize ADC paths */
3706static void init_input_src(struct hda_codec *codec)
3707{
3708 struct hda_gen_spec *spec = codec->spec;
3709 struct hda_input_mux *imux = &spec->input_mux;
3710 struct nid_path *path;
3711 int i, c, nums;
3712
3713 if (spec->dyn_adc_switch)
3714 nums = 1;
3715 else
3716 nums = spec->num_adc_nids;
3717
3718 for (c = 0; c < nums; c++) {
3719 for (i = 0; i < imux->num_items; i++) {
3720 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
3721 get_adc_nid(codec, c, i));
3722 if (path) {
3723 bool active = path->active;
3724 if (i == spec->cur_mux[c])
3725 active = true;
3726 snd_hda_activate_path(codec, path, active, false);
3727 }
3728 }
3729 }
3730
3731 if (spec->shared_mic_hp)
3732 update_shared_mic_hp(codec, spec->cur_mux[0]);
3733
3734 if (spec->cap_sync_hook)
3735 spec->cap_sync_hook(codec);
3736}
3737
3738/* set right pin controls for digital I/O */
3739static void init_digital(struct hda_codec *codec)
3740{
3741 struct hda_gen_spec *spec = codec->spec;
3742 int i;
3743 hda_nid_t pin;
3744
3745 for (i = 0; i < spec->autocfg.dig_outs; i++) {
3746 pin = spec->autocfg.dig_out_pins[i];
3747 if (!pin)
3748 continue;
3749 set_output_and_unmute(codec, pin, PIN_OUT, 0);
3750 }
3751 pin = spec->autocfg.dig_in_pin;
3752 if (pin)
Takashi Iwai7594aa32012-12-20 15:38:40 +01003753 snd_hda_set_pin_ctl_cache(codec, pin, PIN_IN);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003754}
3755
Takashi Iwai973e4972012-12-20 15:16:09 +01003756/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
3757 * invalid unsol tags by some reason
3758 */
3759static void clear_unsol_on_unused_pins(struct hda_codec *codec)
3760{
3761 int i;
3762
3763 for (i = 0; i < codec->init_pins.used; i++) {
3764 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
3765 hda_nid_t nid = pin->nid;
3766 if (is_jack_detectable(codec, nid) &&
3767 !snd_hda_jack_tbl_get(codec, nid))
3768 snd_hda_codec_update_cache(codec, nid, 0,
3769 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
3770 }
3771}
3772
Takashi Iwai352f7f92012-12-19 12:52:06 +01003773int snd_hda_gen_init(struct hda_codec *codec)
3774{
3775 struct hda_gen_spec *spec = codec->spec;
3776
3777 if (spec->init_hook)
3778 spec->init_hook(codec);
3779
3780 snd_hda_apply_verbs(codec);
3781
Takashi Iwai3bbcd272012-12-20 11:50:58 +01003782 codec->cached_write = 1;
3783
Takashi Iwai352f7f92012-12-19 12:52:06 +01003784 init_multi_out(codec);
3785 init_extra_out(codec);
3786 init_multi_io(codec);
3787 init_analog_input(codec);
3788 init_input_src(codec);
3789 init_digital(codec);
3790
Takashi Iwai973e4972012-12-20 15:16:09 +01003791 clear_unsol_on_unused_pins(codec);
3792
Takashi Iwai352f7f92012-12-19 12:52:06 +01003793 /* call init functions of standard auto-mute helpers */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003794 snd_hda_gen_hp_automute(codec, NULL);
3795 snd_hda_gen_line_automute(codec, NULL);
3796 snd_hda_gen_mic_autoswitch(codec, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003797
Takashi Iwai3bbcd272012-12-20 11:50:58 +01003798 snd_hda_codec_flush_amp_cache(codec);
3799 snd_hda_codec_flush_cmd_cache(codec);
3800
Takashi Iwai352f7f92012-12-19 12:52:06 +01003801 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
3802 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
3803
3804 hda_call_check_power_status(codec, 0x01);
3805 return 0;
3806}
3807EXPORT_SYMBOL(snd_hda_gen_init);
3808
3809
3810/*
3811 * the generic codec support
3812 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813
Takashi Iwai83012a72012-08-24 18:38:08 +02003814#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003815static int generic_check_power_status(struct hda_codec *codec, hda_nid_t nid)
3816{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003817 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003818 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
3819}
3820#endif
3821
Takashi Iwai352f7f92012-12-19 12:52:06 +01003822static void generic_free(struct hda_codec *codec)
3823{
3824 snd_hda_gen_spec_free(codec->spec);
3825 kfree(codec->spec);
3826 codec->spec = NULL;
3827}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828
Takashi Iwai352f7f92012-12-19 12:52:06 +01003829static const struct hda_codec_ops generic_patch_ops = {
3830 .build_controls = snd_hda_gen_build_controls,
3831 .build_pcms = snd_hda_gen_build_pcms,
3832 .init = snd_hda_gen_init,
3833 .free = generic_free,
3834 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02003835#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003836 .check_power_status = generic_check_power_status,
3837#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838};
3839
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840int snd_hda_parse_generic_codec(struct hda_codec *codec)
3841{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003842 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 int err;
3844
Takashi Iwaie560d8d2005-09-09 14:21:46 +02003845 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003846 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003848 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003851 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
3852 if (err < 0)
3853 return err;
3854
3855 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003856 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 goto error;
3858
3859 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860 return 0;
3861
Takashi Iwai352f7f92012-12-19 12:52:06 +01003862error:
3863 generic_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864 return err;
3865}
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003866EXPORT_SYMBOL(snd_hda_parse_generic_codec);