blob: ff15aea836daeceb6c966587b4eb9769d96f0c59 [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 Iwai352f7f92012-12-19 12:52:06 +0100119/* get the path between the given NIDs;
120 * passing 0 to either @pin or @dac behaves as a wildcard
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100122struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
123 hda_nid_t from_nid, hda_nid_t to_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100125 struct hda_gen_spec *spec = codec->spec;
126 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Takashi Iwai352f7f92012-12-19 12:52:06 +0100128 for (i = 0; i < spec->paths.used; i++) {
129 struct nid_path *path = snd_array_elem(&spec->paths, i);
130 if (path->depth <= 0)
131 continue;
132 if ((!from_nid || path->path[0] == from_nid) &&
133 (!to_nid || path->path[path->depth - 1] == to_nid))
134 return path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 }
136 return NULL;
137}
Takashi Iwai352f7f92012-12-19 12:52:06 +0100138EXPORT_SYMBOL_HDA(snd_hda_get_nid_path);
139
140/* check whether the given DAC is already found in any existing paths */
141static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
142{
143 struct hda_gen_spec *spec = codec->spec;
144 int i;
145
146 for (i = 0; i < spec->paths.used; i++) {
147 struct nid_path *path = snd_array_elem(&spec->paths, i);
148 if (path->path[0] == nid)
149 return true;
150 }
151 return false;
152}
153
154/* check whether the given two widgets can be connected */
155static bool is_reachable_path(struct hda_codec *codec,
156 hda_nid_t from_nid, hda_nid_t to_nid)
157{
158 if (!from_nid || !to_nid)
159 return false;
160 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
161}
162
163/* nid, dir and idx */
164#define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
165
166/* check whether the given ctl is already assigned in any path elements */
167static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
168{
169 struct hda_gen_spec *spec = codec->spec;
170 int i;
171
172 val &= AMP_VAL_COMPARE_MASK;
173 for (i = 0; i < spec->paths.used; i++) {
174 struct nid_path *path = snd_array_elem(&spec->paths, i);
175 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
176 return true;
177 }
178 return false;
179}
180
181/* check whether a control with the given (nid, dir, idx) was assigned */
182static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
183 int dir, int idx)
184{
185 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
186 return is_ctl_used(codec, val, NID_PATH_VOL_CTL) ||
187 is_ctl_used(codec, val, NID_PATH_MUTE_CTL);
188}
189
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100190static void print_nid_path(const char *pfx, struct nid_path *path)
191{
192 char buf[40];
193 int i;
194
195
196 buf[0] = 0;
197 for (i = 0; i < path->depth; i++) {
198 char tmp[4];
199 sprintf(tmp, ":%02x", path->path[i]);
200 strlcat(buf, tmp, sizeof(buf));
201 }
202 snd_printdd("%s path: depth=%d %s\n", pfx, path->depth, buf);
203}
204
Takashi Iwai352f7f92012-12-19 12:52:06 +0100205/* called recursively */
206static bool __parse_nid_path(struct hda_codec *codec,
207 hda_nid_t from_nid, hda_nid_t to_nid,
208 int with_aa_mix, struct nid_path *path, int depth)
209{
210 struct hda_gen_spec *spec = codec->spec;
211 hda_nid_t conn[16];
212 int i, nums;
213
214 if (to_nid == spec->mixer_nid) {
Takashi Iwai4ac0eef2012-12-20 18:10:51 +0100215 if (with_aa_mix == HDA_PARSE_NO_AAMIX)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100216 return false;
Takashi Iwai4ac0eef2012-12-20 18:10:51 +0100217 with_aa_mix = HDA_PARSE_ALL; /* mark aa-mix is included */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100218 }
219
220 nums = snd_hda_get_connections(codec, to_nid, conn, ARRAY_SIZE(conn));
221 for (i = 0; i < nums; i++) {
222 if (conn[i] != from_nid) {
223 /* special case: when from_nid is 0,
224 * try to find an empty DAC
225 */
226 if (from_nid ||
227 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
228 is_dac_already_used(codec, conn[i]))
229 continue;
230 }
231 /* aa-mix is requested but not included? */
Takashi Iwai4ac0eef2012-12-20 18:10:51 +0100232 if (!(spec->mixer_nid && with_aa_mix == HDA_PARSE_ONLY_AAMIX))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100233 goto found;
234 }
235 if (depth >= MAX_NID_PATH_DEPTH)
236 return false;
237 for (i = 0; i < nums; i++) {
238 unsigned int type;
239 type = get_wcaps_type(get_wcaps(codec, conn[i]));
240 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
241 type == AC_WID_PIN)
242 continue;
243 if (__parse_nid_path(codec, from_nid, conn[i],
244 with_aa_mix, path, depth + 1))
245 goto found;
246 }
247 return false;
248
249 found:
250 path->path[path->depth] = conn[i];
251 path->idx[path->depth + 1] = i;
252 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
253 path->multi[path->depth + 1] = 1;
254 path->depth++;
255 return true;
256}
257
258/* parse the widget path from the given nid to the target nid;
259 * when @from_nid is 0, try to find an empty DAC;
Takashi Iwai4ac0eef2012-12-20 18:10:51 +0100260 * when @with_aa_mix is HDA_PARSE_NO_AAMIX, paths with spec->mixer_nid are
261 * excluded, only the paths that don't go through the mixer will be chosen.
262 * when @with_aa_mix is HDA_PARSE_ONLY_AAMIX, only the paths going through
263 * spec->mixer_nid will be chosen.
264 * when @with_aa_mix is HDA_PARSE_ALL, no special handling about mixer widget.
Takashi Iwai352f7f92012-12-19 12:52:06 +0100265 */
266bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
267 hda_nid_t to_nid, int with_aa_mix,
268 struct nid_path *path)
269{
270 if (__parse_nid_path(codec, from_nid, to_nid, with_aa_mix, path, 1)) {
271 path->path[path->depth] = to_nid;
272 path->depth++;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100273 return true;
274 }
275 return false;
276}
277EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100280 * parse the path between the given NIDs and add to the path list.
281 * if no valid path is found, return NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100283struct nid_path *
284snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
285 hda_nid_t to_nid, int with_aa_mix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100287 struct hda_gen_spec *spec = codec->spec;
288 struct nid_path *path;
289
290 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
291 return NULL;
292
293 path = snd_array_new(&spec->paths);
294 if (!path)
295 return NULL;
296 memset(path, 0, sizeof(*path));
297 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, with_aa_mix, path))
298 return path;
299 /* push back */
300 spec->paths.used--;
301 return NULL;
302}
303EXPORT_SYMBOL_HDA(snd_hda_add_new_path);
304
305/* look for an empty DAC slot */
306static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
307 bool is_digital)
308{
309 struct hda_gen_spec *spec = codec->spec;
310 bool cap_digital;
311 int i;
312
313 for (i = 0; i < spec->num_all_dacs; i++) {
314 hda_nid_t nid = spec->all_dacs[i];
315 if (!nid || is_dac_already_used(codec, nid))
316 continue;
317 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
318 if (is_digital != cap_digital)
319 continue;
320 if (is_reachable_path(codec, nid, pin))
321 return nid;
322 }
323 return 0;
324}
325
326/* replace the channels in the composed amp value with the given number */
327static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
328{
329 val &= ~(0x3U << 16);
330 val |= chs << 16;
331 return val;
332}
333
334/* check whether the widget has the given amp capability for the direction */
335static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
336 int dir, unsigned int bits)
337{
338 if (!nid)
339 return false;
340 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
341 if (query_amp_caps(codec, nid, dir) & bits)
342 return true;
343 return false;
344}
345
346#define nid_has_mute(codec, nid, dir) \
347 check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
348#define nid_has_volume(codec, nid, dir) \
349 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
350
351/* look for a widget suitable for assigning a mute switch in the path */
352static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
353 struct nid_path *path)
354{
355 int i;
356
357 for (i = path->depth - 1; i >= 0; i--) {
358 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
359 return path->path[i];
360 if (i != path->depth - 1 && i != 0 &&
361 nid_has_mute(codec, path->path[i], HDA_INPUT))
362 return path->path[i];
363 }
364 return 0;
365}
366
367/* look for a widget suitable for assigning a volume ctl in the path */
368static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
369 struct nid_path *path)
370{
371 int i;
372
373 for (i = path->depth - 1; i >= 0; i--) {
374 if (nid_has_volume(codec, path->path[i], HDA_OUTPUT))
375 return path->path[i];
376 }
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200377 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
379
380/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100381 * path activation / deactivation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100383
384/* can have the amp-in capability? */
385static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100387 hda_nid_t nid = path->path[idx];
388 unsigned int caps = get_wcaps(codec, nid);
389 unsigned int type = get_wcaps_type(caps);
390
391 if (!(caps & AC_WCAP_IN_AMP))
392 return false;
393 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
394 return false;
395 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
397
Takashi Iwai352f7f92012-12-19 12:52:06 +0100398/* can have the amp-out capability? */
399static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100401 hda_nid_t nid = path->path[idx];
402 unsigned int caps = get_wcaps(codec, nid);
403 unsigned int type = get_wcaps_type(caps);
404
405 if (!(caps & AC_WCAP_OUT_AMP))
406 return false;
407 if (type == AC_WID_PIN && !idx) /* only for output pins */
408 return false;
409 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410}
411
Takashi Iwai352f7f92012-12-19 12:52:06 +0100412/* check whether the given (nid,dir,idx) is active */
413static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
414 unsigned int idx, unsigned int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100416 struct hda_gen_spec *spec = codec->spec;
417 int i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Takashi Iwai352f7f92012-12-19 12:52:06 +0100419 for (n = 0; n < spec->paths.used; n++) {
420 struct nid_path *path = snd_array_elem(&spec->paths, n);
421 if (!path->active)
422 continue;
423 for (i = 0; i < path->depth; i++) {
424 if (path->path[i] == nid) {
425 if (dir == HDA_OUTPUT || path->idx[i] == idx)
426 return true;
427 break;
428 }
429 }
430 }
431 return false;
432}
433
434/* get the default amp value for the target state */
435static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
436 int dir, bool enable)
437{
438 unsigned int caps;
439 unsigned int val = 0;
440
441 caps = query_amp_caps(codec, nid, dir);
442 if (caps & AC_AMPCAP_NUM_STEPS) {
443 /* set to 0dB */
444 if (enable)
445 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
446 }
447 if (caps & AC_AMPCAP_MUTE) {
448 if (!enable)
449 val |= HDA_AMP_MUTE;
450 }
451 return val;
452}
453
454/* initialize the amp value (only at the first time) */
455static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
456{
457 int val = get_amp_val_to_activate(codec, nid, dir, false);
458 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
459}
460
461static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
462 int idx, bool enable)
463{
464 int val;
465 if (is_ctl_associated(codec, nid, dir, idx) ||
466 is_active_nid(codec, nid, dir, idx))
467 return;
468 val = get_amp_val_to_activate(codec, nid, dir, enable);
469 snd_hda_codec_amp_stereo(codec, nid, dir, idx, 0xff, val);
470}
471
472static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
473 int i, bool enable)
474{
475 hda_nid_t nid = path->path[i];
476 init_amp(codec, nid, HDA_OUTPUT, 0);
477 activate_amp(codec, nid, HDA_OUTPUT, 0, enable);
478}
479
480static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
481 int i, bool enable, bool add_aamix)
482{
483 struct hda_gen_spec *spec = codec->spec;
484 hda_nid_t conn[16];
485 int n, nums, idx;
486 int type;
487 hda_nid_t nid = path->path[i];
488
489 nums = snd_hda_get_connections(codec, nid, conn, ARRAY_SIZE(conn));
490 type = get_wcaps_type(get_wcaps(codec, nid));
491 if (type == AC_WID_PIN ||
492 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
493 nums = 1;
494 idx = 0;
495 } else
496 idx = path->idx[i];
497
498 for (n = 0; n < nums; n++)
499 init_amp(codec, nid, HDA_INPUT, n);
500
501 if (is_ctl_associated(codec, nid, HDA_INPUT, idx))
502 return;
503
504 /* here is a little bit tricky in comparison with activate_amp_out();
505 * when aa-mixer is available, we need to enable the path as well
506 */
507 for (n = 0; n < nums; n++) {
508 if (n != idx && (!add_aamix || conn[n] != spec->mixer_nid))
509 continue;
510 activate_amp(codec, nid, HDA_INPUT, n, enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 }
512}
513
Takashi Iwai352f7f92012-12-19 12:52:06 +0100514/* activate or deactivate the given path
515 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100517void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
518 bool enable, bool add_aamix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100520 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Takashi Iwai352f7f92012-12-19 12:52:06 +0100522 if (!enable)
523 path->active = false;
524
525 for (i = path->depth - 1; i >= 0; i--) {
526 if (enable && path->multi[i])
527 snd_hda_codec_write_cache(codec, path->path[i], 0,
528 AC_VERB_SET_CONNECT_SEL,
529 path->idx[i]);
530 if (has_amp_in(codec, path, i))
531 activate_amp_in(codec, path, i, enable, add_aamix);
532 if (has_amp_out(codec, path, i))
533 activate_amp_out(codec, path, i, enable);
534 }
535
536 if (enable)
537 path->active = true;
538}
539EXPORT_SYMBOL_HDA(snd_hda_activate_path);
540
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100541/* turn on/off EAPD on the given pin */
542static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
543{
544 struct hda_gen_spec *spec = codec->spec;
545 if (spec->own_eapd_ctl ||
546 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
547 return;
548 snd_hda_codec_update_cache(codec, pin, 0,
549 AC_VERB_SET_EAPD_BTLENABLE,
550 enable ? 0x02 : 0x00);
551}
552
Takashi Iwai352f7f92012-12-19 12:52:06 +0100553
554/*
555 * Helper functions for creating mixer ctl elements
556 */
557
558enum {
559 HDA_CTL_WIDGET_VOL,
560 HDA_CTL_WIDGET_MUTE,
561 HDA_CTL_BIND_MUTE,
562 HDA_CTL_BIND_VOL,
563 HDA_CTL_BIND_SW,
564};
565static const struct snd_kcontrol_new control_templates[] = {
566 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
567 HDA_CODEC_MUTE(NULL, 0, 0, 0),
568 HDA_BIND_MUTE(NULL, 0, 0, 0),
569 HDA_BIND_VOL(NULL, 0),
570 HDA_BIND_SW(NULL, 0),
571};
572
573/* add dynamic controls from template */
574static int add_control(struct hda_gen_spec *spec, int type, const char *name,
575 int cidx, unsigned long val)
576{
577 struct snd_kcontrol_new *knew;
578
Takashi Iwai12c93df2012-12-19 14:38:33 +0100579 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100580 if (!knew)
581 return -ENOMEM;
582 knew->index = cidx;
583 if (get_amp_nid_(val))
584 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
585 knew->private_value = val;
586 return 0;
587}
588
589static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
590 const char *pfx, const char *dir,
591 const char *sfx, int cidx, unsigned long val)
592{
593 char name[32];
594 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
595 return add_control(spec, type, name, cidx, val);
596}
597
598#define add_pb_vol_ctrl(spec, type, pfx, val) \
599 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
600#define add_pb_sw_ctrl(spec, type, pfx, val) \
601 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
602#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
603 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
604#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
605 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
606
607static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
608 unsigned int chs, struct nid_path *path)
609{
610 unsigned int val;
611 if (!path)
612 return 0;
613 val = path->ctls[NID_PATH_VOL_CTL];
614 if (!val)
615 return 0;
616 val = amp_val_replace_channels(val, chs);
617 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
618}
619
620/* return the channel bits suitable for the given path->ctls[] */
621static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
622 int type)
623{
624 int chs = 1; /* mono (left only) */
625 if (path) {
626 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
627 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
628 chs = 3; /* stereo */
629 }
630 return chs;
631}
632
633static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
634 struct nid_path *path)
635{
636 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
637 return add_vol_ctl(codec, pfx, cidx, chs, path);
638}
639
640/* create a mute-switch for the given mixer widget;
641 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
642 */
643static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
644 unsigned int chs, struct nid_path *path)
645{
646 unsigned int val;
647 int type = HDA_CTL_WIDGET_MUTE;
648
649 if (!path)
650 return 0;
651 val = path->ctls[NID_PATH_MUTE_CTL];
652 if (!val)
653 return 0;
654 val = amp_val_replace_channels(val, chs);
655 if (get_amp_direction_(val) == HDA_INPUT) {
656 hda_nid_t nid = get_amp_nid_(val);
657 int nums = snd_hda_get_num_conns(codec, nid);
658 if (nums > 1) {
659 type = HDA_CTL_BIND_MUTE;
660 val |= nums << 19;
661 }
662 }
663 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
664}
665
666static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
667 int cidx, struct nid_path *path)
668{
669 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
670 return add_sw_ctl(codec, pfx, cidx, chs, path);
671}
672
673static const char * const channel_name[4] = {
674 "Front", "Surround", "CLFE", "Side"
675};
676
677/* give some appropriate ctl name prefix for the given line out channel */
678static const char *get_line_out_pfx(struct hda_gen_spec *spec, int ch,
679 bool can_be_master, int *index)
680{
681 struct auto_pin_cfg *cfg = &spec->autocfg;
682
683 *index = 0;
684 if (cfg->line_outs == 1 && !spec->multi_ios &&
685 !cfg->hp_outs && !cfg->speaker_outs && can_be_master)
686 return spec->vmaster_mute.hook ? "PCM" : "Master";
687
688 /* if there is really a single DAC used in the whole output paths,
689 * use it master (or "PCM" if a vmaster hook is present)
690 */
691 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
692 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
693 return spec->vmaster_mute.hook ? "PCM" : "Master";
694
695 switch (cfg->line_out_type) {
696 case AUTO_PIN_SPEAKER_OUT:
697 if (cfg->line_outs == 1)
698 return "Speaker";
699 if (cfg->line_outs == 2)
700 return ch ? "Bass Speaker" : "Speaker";
701 break;
702 case AUTO_PIN_HP_OUT:
703 /* for multi-io case, only the primary out */
704 if (ch && spec->multi_ios)
705 break;
706 *index = ch;
707 return "Headphone";
708 default:
709 if (cfg->line_outs == 1 && !spec->multi_ios)
710 return "PCM";
711 break;
712 }
713 if (ch >= ARRAY_SIZE(channel_name)) {
714 snd_BUG();
715 return "PCM";
716 }
717
718 return channel_name[ch];
719}
720
721/*
722 * Parse output paths
723 */
724
725/* badness definition */
726enum {
727 /* No primary DAC is found for the main output */
728 BAD_NO_PRIMARY_DAC = 0x10000,
729 /* No DAC is found for the extra output */
730 BAD_NO_DAC = 0x4000,
731 /* No possible multi-ios */
732 BAD_MULTI_IO = 0x103,
733 /* No individual DAC for extra output */
734 BAD_NO_EXTRA_DAC = 0x102,
735 /* No individual DAC for extra surrounds */
736 BAD_NO_EXTRA_SURR_DAC = 0x101,
737 /* Primary DAC shared with main surrounds */
738 BAD_SHARED_SURROUND = 0x100,
739 /* Primary DAC shared with main CLFE */
740 BAD_SHARED_CLFE = 0x10,
741 /* Primary DAC shared with extra surrounds */
742 BAD_SHARED_EXTRA_SURROUND = 0x10,
743 /* Volume widget is shared */
744 BAD_SHARED_VOL = 0x10,
745};
746
747/* look for widgets in the path between the given NIDs appropriate for
748 * volume and mute controls, and assign the values to ctls[].
749 *
750 * When no appropriate widget is found in the path, the badness value
751 * is incremented depending on the situation. The function returns the
752 * total badness for both volume and mute controls.
753 */
754static int assign_out_path_ctls(struct hda_codec *codec, hda_nid_t pin,
755 hda_nid_t dac)
756{
757 struct nid_path *path = snd_hda_get_nid_path(codec, dac, pin);
758 hda_nid_t nid;
759 unsigned int val;
760 int badness = 0;
761
762 if (!path)
763 return BAD_SHARED_VOL * 2;
764 nid = look_for_out_vol_nid(codec, path);
765 if (nid) {
766 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
767 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
768 badness += BAD_SHARED_VOL;
769 else
770 path->ctls[NID_PATH_VOL_CTL] = val;
771 } else
772 badness += BAD_SHARED_VOL;
773 nid = look_for_out_mute_nid(codec, path);
774 if (nid) {
775 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
776 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
777 nid_has_mute(codec, nid, HDA_OUTPUT))
778 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
779 else
780 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
781 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
782 badness += BAD_SHARED_VOL;
783 else
784 path->ctls[NID_PATH_MUTE_CTL] = val;
785 } else
786 badness += BAD_SHARED_VOL;
787 return badness;
788}
789
790struct badness_table {
791 int no_primary_dac; /* no primary DAC */
792 int no_dac; /* no secondary DACs */
793 int shared_primary; /* primary DAC is shared with main output */
794 int shared_surr; /* secondary DAC shared with main or primary */
795 int shared_clfe; /* third DAC shared with main or primary */
796 int shared_surr_main; /* secondary DAC sahred with main/DAC0 */
797};
798
799static struct badness_table main_out_badness = {
800 .no_primary_dac = BAD_NO_PRIMARY_DAC,
801 .no_dac = BAD_NO_DAC,
802 .shared_primary = BAD_NO_PRIMARY_DAC,
803 .shared_surr = BAD_SHARED_SURROUND,
804 .shared_clfe = BAD_SHARED_CLFE,
805 .shared_surr_main = BAD_SHARED_SURROUND,
806};
807
808static struct badness_table extra_out_badness = {
809 .no_primary_dac = BAD_NO_DAC,
810 .no_dac = BAD_NO_DAC,
811 .shared_primary = BAD_NO_EXTRA_DAC,
812 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
813 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
814 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
815};
816
817/* try to assign DACs to pins and return the resultant badness */
818static int try_assign_dacs(struct hda_codec *codec, int num_outs,
819 const hda_nid_t *pins, hda_nid_t *dacs,
820 const struct badness_table *bad)
821{
822 struct hda_gen_spec *spec = codec->spec;
823 struct auto_pin_cfg *cfg = &spec->autocfg;
824 int i, j;
825 int badness = 0;
826 hda_nid_t dac;
827
828 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 return 0;
830
Takashi Iwai352f7f92012-12-19 12:52:06 +0100831 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100832 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100833 hda_nid_t pin = pins[i];
834 if (!dacs[i])
835 dacs[i] = look_for_dac(codec, pin, false);
836 if (!dacs[i] && !i) {
837 for (j = 1; j < num_outs; j++) {
838 if (is_reachable_path(codec, dacs[j], pin)) {
839 dacs[0] = dacs[j];
840 dacs[j] = 0;
841 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 }
843 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100844 }
845 dac = dacs[i];
846 if (!dac) {
847 if (is_reachable_path(codec, dacs[0], pin))
848 dac = dacs[0];
849 else if (cfg->line_outs > i &&
850 is_reachable_path(codec, spec->private_dac_nids[i], pin))
851 dac = spec->private_dac_nids[i];
852 if (dac) {
853 if (!i)
854 badness += bad->shared_primary;
855 else if (i == 1)
856 badness += bad->shared_surr;
857 else
858 badness += bad->shared_clfe;
859 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
860 dac = spec->private_dac_nids[0];
861 badness += bad->shared_surr_main;
862 } else if (!i)
863 badness += bad->no_primary_dac;
864 else
865 badness += bad->no_dac;
866 }
Takashi Iwai4ac0eef2012-12-20 18:10:51 +0100867 path = snd_hda_add_new_path(codec, dac, pin, HDA_PARSE_NO_AAMIX);
Takashi Iwaib3a8c742012-12-20 18:29:16 +0100868 if (!path && i > 0 && spec->mixer_nid) {
869 /* try with aamix */
870 path = snd_hda_add_new_path(codec, dac, pin, HDA_PARSE_ALL);
871 }
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100872 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100873 dac = dacs[i] = 0;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100874 else
875 print_nid_path("output", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100876 if (dac)
877 badness += assign_out_path_ctls(codec, pin, dac);
878 }
879
880 return badness;
881}
882
883/* return NID if the given pin has only a single connection to a certain DAC */
884static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
885{
886 struct hda_gen_spec *spec = codec->spec;
887 int i;
888 hda_nid_t nid_found = 0;
889
890 for (i = 0; i < spec->num_all_dacs; i++) {
891 hda_nid_t nid = spec->all_dacs[i];
892 if (!nid || is_dac_already_used(codec, nid))
893 continue;
894 if (is_reachable_path(codec, nid, pin)) {
895 if (nid_found)
896 return 0;
897 nid_found = nid;
898 }
899 }
900 return nid_found;
901}
902
903/* check whether the given pin can be a multi-io pin */
904static bool can_be_multiio_pin(struct hda_codec *codec,
905 unsigned int location, hda_nid_t nid)
906{
907 unsigned int defcfg, caps;
908
909 defcfg = snd_hda_codec_get_pincfg(codec, nid);
910 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
911 return false;
912 if (location && get_defcfg_location(defcfg) != location)
913 return false;
914 caps = snd_hda_query_pin_caps(codec, nid);
915 if (!(caps & AC_PINCAP_OUT))
916 return false;
917 return true;
918}
919
920/*
921 * multi-io helper
922 *
923 * When hardwired is set, try to fill ony hardwired pins, and returns
924 * zero if any pins are filled, non-zero if nothing found.
925 * When hardwired is off, try to fill possible input pins, and returns
926 * the badness value.
927 */
928static int fill_multi_ios(struct hda_codec *codec,
929 hda_nid_t reference_pin,
930 bool hardwired, int offset)
931{
932 struct hda_gen_spec *spec = codec->spec;
933 struct auto_pin_cfg *cfg = &spec->autocfg;
934 int type, i, j, dacs, num_pins, old_pins;
935 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
936 unsigned int location = get_defcfg_location(defcfg);
937 int badness = 0;
938
939 old_pins = spec->multi_ios;
940 if (old_pins >= 2)
941 goto end_fill;
942
943 num_pins = 0;
944 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
945 for (i = 0; i < cfg->num_inputs; i++) {
946 if (cfg->inputs[i].type != type)
947 continue;
948 if (can_be_multiio_pin(codec, location,
949 cfg->inputs[i].pin))
950 num_pins++;
951 }
952 }
953 if (num_pins < 2)
954 goto end_fill;
955
956 dacs = spec->multiout.num_dacs;
957 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
958 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100959 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100960 hda_nid_t nid = cfg->inputs[i].pin;
961 hda_nid_t dac = 0;
962
963 if (cfg->inputs[i].type != type)
964 continue;
965 if (!can_be_multiio_pin(codec, location, nid))
966 continue;
967 for (j = 0; j < spec->multi_ios; j++) {
968 if (nid == spec->multi_io[j].pin)
969 break;
970 }
971 if (j < spec->multi_ios)
972 continue;
973
974 if (offset && offset + spec->multi_ios < dacs) {
975 dac = spec->private_dac_nids[offset + spec->multi_ios];
976 if (!is_reachable_path(codec, dac, nid))
977 dac = 0;
978 }
979 if (hardwired)
980 dac = get_dac_if_single(codec, nid);
981 else if (!dac)
982 dac = look_for_dac(codec, nid, false);
983 if (!dac) {
984 badness++;
985 continue;
986 }
Takashi Iwai4ac0eef2012-12-20 18:10:51 +0100987 path = snd_hda_add_new_path(codec, dac, nid, HDA_PARSE_NO_AAMIX);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100988 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100989 badness++;
990 continue;
991 }
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100992 print_nid_path("multiio", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100993 spec->multi_io[spec->multi_ios].pin = nid;
994 spec->multi_io[spec->multi_ios].dac = dac;
995 spec->multi_ios++;
996 if (spec->multi_ios >= 2)
997 break;
998 }
999 }
1000 end_fill:
1001 if (badness)
1002 badness = BAD_MULTI_IO;
1003 if (old_pins == spec->multi_ios) {
1004 if (hardwired)
1005 return 1; /* nothing found */
1006 else
1007 return badness; /* no badness if nothing found */
1008 }
1009 if (!hardwired && spec->multi_ios < 2) {
1010 /* cancel newly assigned paths */
1011 spec->paths.used -= spec->multi_ios - old_pins;
1012 spec->multi_ios = old_pins;
1013 return badness;
1014 }
1015
1016 /* assign volume and mute controls */
1017 for (i = old_pins; i < spec->multi_ios; i++)
1018 badness += assign_out_path_ctls(codec, spec->multi_io[i].pin,
1019 spec->multi_io[i].dac);
1020
1021 return badness;
1022}
1023
1024/* map DACs for all pins in the list if they are single connections */
1025static bool map_singles(struct hda_codec *codec, int outs,
1026 const hda_nid_t *pins, hda_nid_t *dacs)
1027{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001028 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001029 int i;
1030 bool found = false;
1031 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001032 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001033 hda_nid_t dac;
1034 if (dacs[i])
1035 continue;
1036 dac = get_dac_if_single(codec, pins[i]);
1037 if (!dac)
1038 continue;
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01001039 path = snd_hda_add_new_path(codec, dac, pins[i], HDA_PARSE_NO_AAMIX);
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001040 if (!path && i > 0 && spec->mixer_nid)
1041 path = snd_hda_add_new_path(codec, dac, pins[i], HDA_PARSE_ALL);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001042 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001043 dacs[i] = dac;
1044 found = true;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001045 print_nid_path("output", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001046 }
1047 }
1048 return found;
1049}
1050
1051/* fill in the dac_nids table from the parsed pin configuration */
1052static int fill_and_eval_dacs(struct hda_codec *codec,
1053 bool fill_hardwired,
1054 bool fill_mio_first)
1055{
1056 struct hda_gen_spec *spec = codec->spec;
1057 struct auto_pin_cfg *cfg = &spec->autocfg;
1058 int i, err, badness;
1059
1060 /* set num_dacs once to full for look_for_dac() */
1061 spec->multiout.num_dacs = cfg->line_outs;
1062 spec->multiout.dac_nids = spec->private_dac_nids;
1063 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1064 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1065 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1066 spec->multi_ios = 0;
1067 snd_array_free(&spec->paths);
1068 badness = 0;
1069
1070 /* fill hard-wired DACs first */
1071 if (fill_hardwired) {
1072 bool mapped;
1073 do {
1074 mapped = map_singles(codec, cfg->line_outs,
1075 cfg->line_out_pins,
1076 spec->private_dac_nids);
1077 mapped |= map_singles(codec, cfg->hp_outs,
1078 cfg->hp_pins,
1079 spec->multiout.hp_out_nid);
1080 mapped |= map_singles(codec, cfg->speaker_outs,
1081 cfg->speaker_pins,
1082 spec->multiout.extra_out_nid);
1083 if (fill_mio_first && cfg->line_outs == 1 &&
1084 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1085 err = fill_multi_ios(codec, cfg->line_out_pins[0], true, 0);
1086 if (!err)
1087 mapped = true;
1088 }
1089 } while (mapped);
1090 }
1091
1092 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
1093 spec->private_dac_nids,
1094 &main_out_badness);
1095
1096 /* re-count num_dacs and squash invalid entries */
1097 spec->multiout.num_dacs = 0;
1098 for (i = 0; i < cfg->line_outs; i++) {
1099 if (spec->private_dac_nids[i])
1100 spec->multiout.num_dacs++;
1101 else {
1102 memmove(spec->private_dac_nids + i,
1103 spec->private_dac_nids + i + 1,
1104 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1105 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1106 }
1107 }
1108
1109 if (fill_mio_first &&
1110 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1111 /* try to fill multi-io first */
1112 err = fill_multi_ios(codec, cfg->line_out_pins[0], false, 0);
1113 if (err < 0)
1114 return err;
1115 /* we don't count badness at this stage yet */
1116 }
1117
1118 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1119 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1120 spec->multiout.hp_out_nid,
1121 &extra_out_badness);
1122 if (err < 0)
1123 return err;
1124 badness += err;
1125 }
1126 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1127 err = try_assign_dacs(codec, cfg->speaker_outs,
1128 cfg->speaker_pins,
1129 spec->multiout.extra_out_nid,
1130 &extra_out_badness);
1131 if (err < 0)
1132 return err;
1133 badness += err;
1134 }
1135 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1136 err = fill_multi_ios(codec, cfg->line_out_pins[0], false, 0);
1137 if (err < 0)
1138 return err;
1139 badness += err;
1140 }
1141 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1142 /* try multi-ios with HP + inputs */
1143 int offset = 0;
1144 if (cfg->line_outs >= 3)
1145 offset = 1;
1146 err = fill_multi_ios(codec, cfg->hp_pins[0], false, offset);
1147 if (err < 0)
1148 return err;
1149 badness += err;
1150 }
1151
1152 if (spec->multi_ios == 2) {
1153 for (i = 0; i < 2; i++)
1154 spec->private_dac_nids[spec->multiout.num_dacs++] =
1155 spec->multi_io[i].dac;
1156 spec->ext_channel_count = 2;
1157 } else if (spec->multi_ios) {
1158 spec->multi_ios = 0;
1159 badness += BAD_MULTI_IO;
1160 }
1161
1162 return badness;
1163}
1164
1165#define DEBUG_BADNESS
1166
1167#ifdef DEBUG_BADNESS
1168#define debug_badness snd_printdd
1169#else
1170#define debug_badness(...)
1171#endif
1172
1173static void debug_show_configs(struct hda_gen_spec *spec, struct auto_pin_cfg *cfg)
1174{
1175 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1176 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001177 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001178 spec->multiout.dac_nids[0],
1179 spec->multiout.dac_nids[1],
1180 spec->multiout.dac_nids[2],
1181 spec->multiout.dac_nids[3]);
1182 if (spec->multi_ios > 0)
1183 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1184 spec->multi_ios,
1185 spec->multi_io[0].pin, spec->multi_io[1].pin,
1186 spec->multi_io[0].dac, spec->multi_io[1].dac);
1187 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1188 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001189 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001190 spec->multiout.hp_out_nid[0],
1191 spec->multiout.hp_out_nid[1],
1192 spec->multiout.hp_out_nid[2],
1193 spec->multiout.hp_out_nid[3]);
1194 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1195 cfg->speaker_pins[0], cfg->speaker_pins[1],
1196 cfg->speaker_pins[2], cfg->speaker_pins[3],
1197 spec->multiout.extra_out_nid[0],
1198 spec->multiout.extra_out_nid[1],
1199 spec->multiout.extra_out_nid[2],
1200 spec->multiout.extra_out_nid[3]);
1201}
1202
1203/* find all available DACs of the codec */
1204static void fill_all_dac_nids(struct hda_codec *codec)
1205{
1206 struct hda_gen_spec *spec = codec->spec;
1207 int i;
1208 hda_nid_t nid = codec->start_nid;
1209
1210 spec->num_all_dacs = 0;
1211 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1212 for (i = 0; i < codec->num_nodes; i++, nid++) {
1213 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1214 continue;
1215 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1216 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1217 break;
1218 }
1219 spec->all_dacs[spec->num_all_dacs++] = nid;
1220 }
1221}
1222
1223static int parse_output_paths(struct hda_codec *codec)
1224{
1225 struct hda_gen_spec *spec = codec->spec;
1226 struct auto_pin_cfg *cfg = &spec->autocfg;
1227 struct auto_pin_cfg *best_cfg;
1228 int best_badness = INT_MAX;
1229 int badness;
1230 bool fill_hardwired = true, fill_mio_first = true;
1231 bool best_wired = true, best_mio = true;
1232 bool hp_spk_swapped = false;
1233
1234 fill_all_dac_nids(codec);
1235
1236 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1237 if (!best_cfg)
1238 return -ENOMEM;
1239 *best_cfg = *cfg;
1240
1241 for (;;) {
1242 badness = fill_and_eval_dacs(codec, fill_hardwired,
1243 fill_mio_first);
1244 if (badness < 0) {
1245 kfree(best_cfg);
1246 return badness;
1247 }
1248 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1249 cfg->line_out_type, fill_hardwired, fill_mio_first,
1250 badness);
1251 debug_show_configs(spec, cfg);
1252 if (badness < best_badness) {
1253 best_badness = badness;
1254 *best_cfg = *cfg;
1255 best_wired = fill_hardwired;
1256 best_mio = fill_mio_first;
1257 }
1258 if (!badness)
1259 break;
1260 fill_mio_first = !fill_mio_first;
1261 if (!fill_mio_first)
1262 continue;
1263 fill_hardwired = !fill_hardwired;
1264 if (!fill_hardwired)
1265 continue;
1266 if (hp_spk_swapped)
1267 break;
1268 hp_spk_swapped = true;
1269 if (cfg->speaker_outs > 0 &&
1270 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1271 cfg->hp_outs = cfg->line_outs;
1272 memcpy(cfg->hp_pins, cfg->line_out_pins,
1273 sizeof(cfg->hp_pins));
1274 cfg->line_outs = cfg->speaker_outs;
1275 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1276 sizeof(cfg->speaker_pins));
1277 cfg->speaker_outs = 0;
1278 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1279 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1280 fill_hardwired = true;
1281 continue;
1282 }
1283 if (cfg->hp_outs > 0 &&
1284 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1285 cfg->speaker_outs = cfg->line_outs;
1286 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1287 sizeof(cfg->speaker_pins));
1288 cfg->line_outs = cfg->hp_outs;
1289 memcpy(cfg->line_out_pins, cfg->hp_pins,
1290 sizeof(cfg->hp_pins));
1291 cfg->hp_outs = 0;
1292 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1293 cfg->line_out_type = AUTO_PIN_HP_OUT;
1294 fill_hardwired = true;
1295 continue;
1296 }
1297 break;
1298 }
1299
1300 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001301 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001302 *cfg = *best_cfg;
1303 fill_and_eval_dacs(codec, best_wired, best_mio);
1304 }
1305 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1306 cfg->line_out_type, best_wired, best_mio);
1307 debug_show_configs(spec, cfg);
1308
1309 if (cfg->line_out_pins[0]) {
1310 struct nid_path *path;
1311 path = snd_hda_get_nid_path(codec,
1312 spec->multiout.dac_nids[0],
1313 cfg->line_out_pins[0]);
1314 if (path)
1315 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
1316 }
1317
1318 kfree(best_cfg);
1319 return 0;
1320}
1321
1322/* add playback controls from the parsed DAC table */
1323static int create_multi_out_ctls(struct hda_codec *codec,
1324 const struct auto_pin_cfg *cfg)
1325{
1326 struct hda_gen_spec *spec = codec->spec;
1327 int i, err, noutputs;
1328
1329 noutputs = cfg->line_outs;
1330 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1331 noutputs += spec->multi_ios;
1332
1333 for (i = 0; i < noutputs; i++) {
1334 const char *name;
1335 int index;
1336 hda_nid_t dac, pin;
1337 struct nid_path *path;
1338
1339 dac = spec->multiout.dac_nids[i];
1340 if (!dac)
1341 continue;
1342 if (i >= cfg->line_outs) {
1343 pin = spec->multi_io[i - 1].pin;
1344 index = 0;
1345 name = channel_name[i];
1346 } else {
1347 pin = cfg->line_out_pins[i];
1348 name = get_line_out_pfx(spec, i, true, &index);
1349 }
1350
1351 path = snd_hda_get_nid_path(codec, dac, pin);
1352 if (!path)
1353 continue;
1354 if (!name || !strcmp(name, "CLFE")) {
1355 /* Center/LFE */
1356 err = add_vol_ctl(codec, "Center", 0, 1, path);
1357 if (err < 0)
1358 return err;
1359 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1360 if (err < 0)
1361 return err;
1362 err = add_sw_ctl(codec, "Center", 0, 1, path);
1363 if (err < 0)
1364 return err;
1365 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1366 if (err < 0)
1367 return err;
1368 } else {
1369 err = add_stereo_vol(codec, name, index, path);
1370 if (err < 0)
1371 return err;
1372 err = add_stereo_sw(codec, name, index, path);
1373 if (err < 0)
1374 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 }
1376 }
1377 return 0;
1378}
1379
Takashi Iwai352f7f92012-12-19 12:52:06 +01001380static int create_extra_out(struct hda_codec *codec, hda_nid_t pin,
1381 hda_nid_t dac, const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001383 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 int err;
1385
Takashi Iwai352f7f92012-12-19 12:52:06 +01001386 path = snd_hda_get_nid_path(codec, dac, pin);
1387 if (!path)
1388 return 0;
1389 /* bind volume control will be created in the case of dac = 0 */
1390 if (dac) {
1391 err = add_stereo_vol(codec, pfx, cidx, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 if (err < 0)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001393 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001395 err = add_stereo_sw(codec, pfx, cidx, path);
1396 if (err < 0)
1397 return err;
1398 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399}
1400
Takashi Iwai352f7f92012-12-19 12:52:06 +01001401/* add playback controls for speaker and HP outputs */
1402static int create_extra_outs(struct hda_codec *codec, int num_pins,
1403 const hda_nid_t *pins, const hda_nid_t *dacs,
1404 const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001406 struct hda_gen_spec *spec = codec->spec;
1407 struct hda_bind_ctls *ctl;
1408 char name[32];
1409 int i, n, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
Takashi Iwai352f7f92012-12-19 12:52:06 +01001411 if (!num_pins || !pins[0])
1412 return 0;
1413
1414 if (num_pins == 1) {
1415 hda_nid_t dac = *dacs;
1416 if (!dac)
1417 dac = spec->multiout.dac_nids[0];
1418 return create_extra_out(codec, *pins, dac, pfx, 0);
Takashi Iwai97ec5582006-03-21 11:29:07 +01001419 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001420
1421 for (i = 0; i < num_pins; i++) {
1422 hda_nid_t dac;
1423 if (dacs[num_pins - 1])
1424 dac = dacs[i]; /* with individual volumes */
Takashi Iwai97ec5582006-03-21 11:29:07 +01001425 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01001426 dac = 0;
1427 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker")) {
1428 err = create_extra_out(codec, pins[i], dac,
1429 "Bass Speaker", 0);
1430 } else if (num_pins >= 3) {
1431 snprintf(name, sizeof(name), "%s %s",
1432 pfx, channel_name[i]);
1433 err = create_extra_out(codec, pins[i], dac, name, 0);
1434 } else {
1435 err = create_extra_out(codec, pins[i], dac, pfx, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001437 if (err < 0)
1438 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001440 if (dacs[num_pins - 1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 return 0;
1442
Takashi Iwai352f7f92012-12-19 12:52:06 +01001443 /* Let's create a bind-controls for volumes */
1444 ctl = new_bind_ctl(codec, num_pins, &snd_hda_bind_vol);
1445 if (!ctl)
1446 return -ENOMEM;
1447 n = 0;
1448 for (i = 0; i < num_pins; i++) {
1449 hda_nid_t vol;
1450 struct nid_path *path;
1451 if (!pins[i] || !dacs[i])
1452 continue;
1453 path = snd_hda_get_nid_path(codec, dacs[i], pins[i]);
1454 if (!path)
1455 continue;
1456 vol = look_for_out_vol_nid(codec, path);
1457 if (vol)
1458 ctl->values[n++] =
1459 HDA_COMPOSE_AMP_VAL(vol, 3, 0, HDA_OUTPUT);
1460 }
1461 if (n) {
1462 snprintf(name, sizeof(name), "%s Playback Volume", pfx);
1463 err = add_control(spec, HDA_CTL_BIND_VOL, name, 0, (long)ctl);
1464 if (err < 0)
1465 return err;
1466 }
1467 return 0;
1468}
Takashi Iwai97ec5582006-03-21 11:29:07 +01001469
Takashi Iwai352f7f92012-12-19 12:52:06 +01001470static int create_hp_out_ctls(struct hda_codec *codec)
1471{
1472 struct hda_gen_spec *spec = codec->spec;
1473 return create_extra_outs(codec, spec->autocfg.hp_outs,
1474 spec->autocfg.hp_pins,
1475 spec->multiout.hp_out_nid,
1476 "Headphone");
1477}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Takashi Iwai352f7f92012-12-19 12:52:06 +01001479static int create_speaker_out_ctls(struct hda_codec *codec)
1480{
1481 struct hda_gen_spec *spec = codec->spec;
1482 return create_extra_outs(codec, spec->autocfg.speaker_outs,
1483 spec->autocfg.speaker_pins,
1484 spec->multiout.extra_out_nid,
1485 "Speaker");
1486}
1487
1488/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001489 * independent HP controls
1490 */
1491
1492static int indep_hp_info(struct snd_kcontrol *kcontrol,
1493 struct snd_ctl_elem_info *uinfo)
1494{
1495 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
1496}
1497
1498static int indep_hp_get(struct snd_kcontrol *kcontrol,
1499 struct snd_ctl_elem_value *ucontrol)
1500{
1501 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1502 struct hda_gen_spec *spec = codec->spec;
1503 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
1504 return 0;
1505}
1506
1507static int indep_hp_put(struct snd_kcontrol *kcontrol,
1508 struct snd_ctl_elem_value *ucontrol)
1509{
1510 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1511 struct hda_gen_spec *spec = codec->spec;
1512 unsigned int select = ucontrol->value.enumerated.item[0];
1513 int ret = 0;
1514
1515 mutex_lock(&spec->pcm_mutex);
1516 if (spec->active_streams) {
1517 ret = -EBUSY;
1518 goto unlock;
1519 }
1520
1521 if (spec->indep_hp_enabled != select) {
1522 spec->indep_hp_enabled = select;
1523 if (spec->indep_hp_enabled)
1524 spec->multiout.hp_out_nid[0] = 0;
1525 else
1526 spec->multiout.hp_out_nid[0] = spec->alt_dac_nid;
1527 ret = 1;
1528 }
1529 unlock:
1530 mutex_unlock(&spec->pcm_mutex);
1531 return ret;
1532}
1533
1534static const struct snd_kcontrol_new indep_hp_ctl = {
1535 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1536 .name = "Independent HP",
1537 .info = indep_hp_info,
1538 .get = indep_hp_get,
1539 .put = indep_hp_put,
1540};
1541
1542
1543static int create_indep_hp_ctls(struct hda_codec *codec)
1544{
1545 struct hda_gen_spec *spec = codec->spec;
1546
1547 if (!spec->indep_hp)
1548 return 0;
1549 if (!spec->multiout.hp_out_nid[0]) {
1550 spec->indep_hp = 0;
1551 return 0;
1552 }
1553
1554 spec->indep_hp_enabled = false;
1555 spec->alt_dac_nid = spec->multiout.hp_out_nid[0];
1556 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
1557 return -ENOMEM;
1558 return 0;
1559}
1560
1561/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01001562 * channel mode enum control
1563 */
1564
1565static int ch_mode_info(struct snd_kcontrol *kcontrol,
1566 struct snd_ctl_elem_info *uinfo)
1567{
1568 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1569 struct hda_gen_spec *spec = codec->spec;
1570
1571 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1572 uinfo->count = 1;
1573 uinfo->value.enumerated.items = spec->multi_ios + 1;
1574 if (uinfo->value.enumerated.item > spec->multi_ios)
1575 uinfo->value.enumerated.item = spec->multi_ios;
1576 sprintf(uinfo->value.enumerated.name, "%dch",
1577 (uinfo->value.enumerated.item + 1) * 2);
1578 return 0;
1579}
1580
1581static int ch_mode_get(struct snd_kcontrol *kcontrol,
1582 struct snd_ctl_elem_value *ucontrol)
1583{
1584 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1585 struct hda_gen_spec *spec = codec->spec;
1586 ucontrol->value.enumerated.item[0] = (spec->ext_channel_count - 1) / 2;
1587 return 0;
1588}
1589
1590static int set_multi_io(struct hda_codec *codec, int idx, bool output)
1591{
1592 struct hda_gen_spec *spec = codec->spec;
1593 hda_nid_t nid = spec->multi_io[idx].pin;
1594 struct nid_path *path;
1595
1596 path = snd_hda_get_nid_path(codec, spec->multi_io[idx].dac, nid);
1597 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001599
1600 if (path->active == output)
1601 return 0;
1602
1603 if (output) {
1604 snd_hda_set_pin_ctl_cache(codec, nid, PIN_OUT);
1605 snd_hda_activate_path(codec, path, true, true);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01001606 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001607 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01001608 set_pin_eapd(codec, nid, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001609 snd_hda_activate_path(codec, path, false, true);
1610 snd_hda_set_pin_ctl_cache(codec, nid,
1611 spec->multi_io[idx].ctl_in);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001613 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614}
1615
Takashi Iwai352f7f92012-12-19 12:52:06 +01001616static int ch_mode_put(struct snd_kcontrol *kcontrol,
1617 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001619 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1620 struct hda_gen_spec *spec = codec->spec;
1621 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
Takashi Iwai352f7f92012-12-19 12:52:06 +01001623 ch = ucontrol->value.enumerated.item[0];
1624 if (ch < 0 || ch > spec->multi_ios)
1625 return -EINVAL;
1626 if (ch == (spec->ext_channel_count - 1) / 2)
1627 return 0;
1628 spec->ext_channel_count = (ch + 1) * 2;
1629 for (i = 0; i < spec->multi_ios; i++)
1630 set_multi_io(codec, i, i < ch);
1631 spec->multiout.max_channels = max(spec->ext_channel_count,
1632 spec->const_channel_count);
1633 if (spec->need_dac_fix)
1634 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 return 1;
1636}
1637
Takashi Iwai352f7f92012-12-19 12:52:06 +01001638static const struct snd_kcontrol_new channel_mode_enum = {
1639 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1640 .name = "Channel Mode",
1641 .info = ch_mode_info,
1642 .get = ch_mode_get,
1643 .put = ch_mode_put,
1644};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
Takashi Iwai352f7f92012-12-19 12:52:06 +01001646static int create_multi_channel_mode(struct hda_codec *codec)
1647{
1648 struct hda_gen_spec *spec = codec->spec;
1649
1650 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01001651 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01001652 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 return 0;
1655}
1656
Takashi Iwai352f7f92012-12-19 12:52:06 +01001657/*
1658 * shared headphone/mic handling
1659 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02001660
Takashi Iwai352f7f92012-12-19 12:52:06 +01001661static void call_update_outputs(struct hda_codec *codec);
1662
1663/* for shared I/O, change the pin-control accordingly */
1664static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic)
1665{
1666 struct hda_gen_spec *spec = codec->spec;
1667 unsigned int val;
1668 hda_nid_t pin = spec->autocfg.inputs[1].pin;
1669 /* NOTE: this assumes that there are only two inputs, the
1670 * first is the real internal mic and the second is HP/mic jack.
1671 */
1672
1673 val = snd_hda_get_default_vref(codec, pin);
1674
1675 /* This pin does not have vref caps - let's enable vref on pin 0x18
1676 instead, as suggested by Realtek */
1677 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
1678 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
1679 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
1680 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01001681 snd_hda_set_pin_ctl_cache(codec, vref_pin,
1682 PIN_IN | (set_as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02001683 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001684
1685 val = set_as_mic ? val | PIN_IN : PIN_HP;
Takashi Iwai7594aa32012-12-20 15:38:40 +01001686 snd_hda_set_pin_ctl_cache(codec, pin, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001687
1688 spec->automute_speaker = !set_as_mic;
1689 call_update_outputs(codec);
1690}
1691
1692/* create a shared input with the headphone out */
1693static int create_shared_input(struct hda_codec *codec)
1694{
1695 struct hda_gen_spec *spec = codec->spec;
1696 struct auto_pin_cfg *cfg = &spec->autocfg;
1697 unsigned int defcfg;
1698 hda_nid_t nid;
1699
1700 /* only one internal input pin? */
1701 if (cfg->num_inputs != 1)
1702 return 0;
1703 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
1704 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
1705 return 0;
1706
1707 if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1708 nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
1709 else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
1710 nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
1711 else
1712 return 0; /* both not available */
1713
1714 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
1715 return 0; /* no input */
1716
1717 cfg->inputs[1].pin = nid;
1718 cfg->inputs[1].type = AUTO_PIN_MIC;
1719 cfg->num_inputs = 2;
1720 spec->shared_mic_hp = 1;
1721 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
1722 return 0;
1723}
1724
1725
1726/*
1727 * Parse input paths
1728 */
1729
1730#ifdef CONFIG_PM
1731/* add the powersave loopback-list entry */
1732static void add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
1733{
1734 struct hda_amp_list *list;
1735
1736 if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
1737 return;
1738 list = spec->loopback_list + spec->num_loopbacks;
1739 list->nid = mix;
1740 list->dir = HDA_INPUT;
1741 list->idx = idx;
1742 spec->num_loopbacks++;
Takashi Iwaicb53c622007-08-10 17:21:45 +02001743 spec->loopback.amplist = spec->loopback_list;
1744}
1745#else
Takashi Iwai352f7f92012-12-19 12:52:06 +01001746#define add_loopback_list(spec, mix, idx) /* NOP */
Takashi Iwaicb53c622007-08-10 17:21:45 +02001747#endif
1748
Takashi Iwai352f7f92012-12-19 12:52:06 +01001749/* create input playback/capture controls for the given pin */
1750static int new_analog_input(struct hda_codec *codec, hda_nid_t pin,
1751 const char *ctlname, int ctlidx,
1752 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001754 struct hda_gen_spec *spec = codec->spec;
1755 struct nid_path *path;
1756 unsigned int val;
1757 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
Takashi Iwai352f7f92012-12-19 12:52:06 +01001759 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
1760 !nid_has_mute(codec, mix_nid, HDA_INPUT))
1761 return 0; /* no need for analog loopback */
1762
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01001763 path = snd_hda_add_new_path(codec, pin, mix_nid, HDA_PARSE_ALL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001764 if (!path)
1765 return -EINVAL;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001766 print_nid_path("loopback", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001767
1768 idx = path->idx[path->depth - 1];
1769 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
1770 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1771 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001772 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001774 path->ctls[NID_PATH_VOL_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 }
1776
Takashi Iwai352f7f92012-12-19 12:52:06 +01001777 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
1778 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1779 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001780 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001782 path->ctls[NID_PATH_MUTE_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 }
1784
Takashi Iwai352f7f92012-12-19 12:52:06 +01001785 path->active = true;
1786 add_loopback_list(spec, mix_nid, idx);
1787 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788}
1789
Takashi Iwai352f7f92012-12-19 12:52:06 +01001790static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001792 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
1793 return (pincap & AC_PINCAP_IN) != 0;
1794}
1795
1796/* Parse the codec tree and retrieve ADCs */
1797static int fill_adc_nids(struct hda_codec *codec)
1798{
1799 struct hda_gen_spec *spec = codec->spec;
1800 hda_nid_t nid;
1801 hda_nid_t *adc_nids = spec->adc_nids;
1802 int max_nums = ARRAY_SIZE(spec->adc_nids);
1803 int i, nums = 0;
1804
1805 nid = codec->start_nid;
1806 for (i = 0; i < codec->num_nodes; i++, nid++) {
1807 unsigned int caps = get_wcaps(codec, nid);
1808 int type = get_wcaps_type(caps);
1809
1810 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
1811 continue;
1812 adc_nids[nums] = nid;
1813 if (++nums >= max_nums)
1814 break;
1815 }
1816 spec->num_adc_nids = nums;
1817 return nums;
1818}
1819
1820/* filter out invalid adc_nids that don't give all active input pins;
1821 * if needed, check whether dynamic ADC-switching is available
1822 */
1823static int check_dyn_adc_switch(struct hda_codec *codec)
1824{
1825 struct hda_gen_spec *spec = codec->spec;
1826 struct hda_input_mux *imux = &spec->input_mux;
1827 hda_nid_t adc_nids[ARRAY_SIZE(spec->adc_nids)];
1828 int i, n, nums;
1829 hda_nid_t pin, adc;
1830
1831 again:
1832 nums = 0;
1833 for (n = 0; n < spec->num_adc_nids; n++) {
1834 adc = spec->adc_nids[n];
1835 for (i = 0; i < imux->num_items; i++) {
1836 pin = spec->imux_pins[i];
1837 if (!is_reachable_path(codec, pin, adc))
1838 break;
1839 }
1840 if (i >= imux->num_items)
1841 adc_nids[nums++] = adc;
1842 }
1843
1844 if (!nums) {
1845 if (spec->shared_mic_hp) {
1846 spec->shared_mic_hp = 0;
1847 imux->num_items = 1;
1848 goto again;
1849 }
1850
1851 /* check whether ADC-switch is possible */
1852 for (i = 0; i < imux->num_items; i++) {
1853 pin = spec->imux_pins[i];
1854 for (n = 0; n < spec->num_adc_nids; n++) {
1855 adc = spec->adc_nids[n];
1856 if (is_reachable_path(codec, pin, adc)) {
1857 spec->dyn_adc_idx[i] = n;
1858 break;
1859 }
1860 }
1861 }
1862
1863 snd_printdd("hda-codec: enabling ADC switching\n");
1864 spec->dyn_adc_switch = 1;
1865 } else if (nums != spec->num_adc_nids) {
1866 memcpy(spec->adc_nids, adc_nids, nums * sizeof(hda_nid_t));
1867 spec->num_adc_nids = nums;
1868 }
1869
1870 if (imux->num_items == 1 || spec->shared_mic_hp) {
1871 snd_printdd("hda-codec: reducing to a single ADC\n");
1872 spec->num_adc_nids = 1; /* reduce to a single ADC */
1873 }
1874
1875 /* single index for individual volumes ctls */
1876 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
1877 spec->num_adc_nids = 1;
1878
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 return 0;
1880}
1881
1882/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01001883 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001885static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02001886{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001887 struct hda_gen_spec *spec = codec->spec;
1888 const struct auto_pin_cfg *cfg = &spec->autocfg;
1889 hda_nid_t mixer = spec->mixer_nid;
1890 struct hda_input_mux *imux = &spec->input_mux;
1891 int num_adcs;
1892 int i, c, err, type_idx = 0;
1893 const char *prev_label = NULL;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02001894
Takashi Iwai352f7f92012-12-19 12:52:06 +01001895 num_adcs = fill_adc_nids(codec);
1896 if (num_adcs < 0)
1897 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02001898
Takashi Iwai352f7f92012-12-19 12:52:06 +01001899 for (i = 0; i < cfg->num_inputs; i++) {
1900 hda_nid_t pin;
1901 const char *label;
1902 bool imux_added;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
Takashi Iwai352f7f92012-12-19 12:52:06 +01001904 pin = cfg->inputs[i].pin;
1905 if (!is_input_pin(codec, pin))
1906 continue;
1907
1908 label = hda_get_autocfg_input_label(codec, cfg, i);
1909 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
1910 label = "Headphone Mic";
1911 if (prev_label && !strcmp(label, prev_label))
1912 type_idx++;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02001913 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01001914 type_idx = 0;
1915 prev_label = label;
1916
1917 if (mixer) {
1918 if (is_reachable_path(codec, pin, mixer)) {
1919 err = new_analog_input(codec, pin,
1920 label, type_idx, mixer);
1921 if (err < 0)
1922 return err;
1923 }
1924 }
1925
1926 imux_added = false;
1927 for (c = 0; c < num_adcs; c++) {
1928 struct nid_path *path;
1929 hda_nid_t adc = spec->adc_nids[c];
1930
1931 if (!is_reachable_path(codec, pin, adc))
1932 continue;
1933 path = snd_array_new(&spec->paths);
1934 if (!path)
1935 return -ENOMEM;
1936 memset(path, 0, sizeof(*path));
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01001937 if (!snd_hda_parse_nid_path(codec, pin, adc, HDA_PARSE_ALL, path)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001938 snd_printd(KERN_ERR
1939 "invalid input path 0x%x -> 0x%x\n",
1940 pin, adc);
1941 spec->paths.used--;
1942 continue;
1943 }
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001944 print_nid_path("input", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001945
1946 if (!imux_added) {
1947 spec->imux_pins[imux->num_items] = pin;
1948 snd_hda_add_imux_item(imux, label,
1949 imux->num_items, NULL);
1950 imux_added = true;
1951 }
1952 }
1953 }
1954
1955 return 0;
1956}
1957
1958
1959/*
1960 * input source mux
1961 */
1962
1963/* get the ADC NID corresponding to the given index */
1964static hda_nid_t get_adc_nid(struct hda_codec *codec, int adc_idx, int imux_idx)
1965{
1966 struct hda_gen_spec *spec = codec->spec;
1967 if (spec->dyn_adc_switch)
1968 adc_idx = spec->dyn_adc_idx[imux_idx];
1969 return spec->adc_nids[adc_idx];
1970}
1971
1972static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
1973 unsigned int idx);
1974
1975static int mux_enum_info(struct snd_kcontrol *kcontrol,
1976 struct snd_ctl_elem_info *uinfo)
1977{
1978 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1979 struct hda_gen_spec *spec = codec->spec;
1980 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
1981}
1982
1983static int mux_enum_get(struct snd_kcontrol *kcontrol,
1984 struct snd_ctl_elem_value *ucontrol)
1985{
1986 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1987 struct hda_gen_spec *spec = codec->spec;
1988 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1989
1990 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
1991 return 0;
1992}
1993
1994static int mux_enum_put(struct snd_kcontrol *kcontrol,
1995 struct snd_ctl_elem_value *ucontrol)
1996{
1997 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1998 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1999 return mux_select(codec, adc_idx,
2000 ucontrol->value.enumerated.item[0]);
2001}
2002
Takashi Iwai352f7f92012-12-19 12:52:06 +01002003static const struct snd_kcontrol_new cap_src_temp = {
2004 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2005 .name = "Input Source",
2006 .info = mux_enum_info,
2007 .get = mux_enum_get,
2008 .put = mux_enum_put,
2009};
2010
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002011/*
2012 * capture volume and capture switch ctls
2013 */
2014
Takashi Iwai352f7f92012-12-19 12:52:06 +01002015typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
2016 struct snd_ctl_elem_value *ucontrol);
2017
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002018/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002019static int cap_put_caller(struct snd_kcontrol *kcontrol,
2020 struct snd_ctl_elem_value *ucontrol,
2021 put_call_t func, int type)
2022{
2023 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2024 struct hda_gen_spec *spec = codec->spec;
2025 const struct hda_input_mux *imux;
2026 struct nid_path *path;
2027 int i, adc_idx, err = 0;
2028
2029 imux = &spec->input_mux;
2030 adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2031 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002032 /* we use the cache-only update at first since multiple input paths
2033 * may shared the same amp; by updating only caches, the redundant
2034 * writes to hardware can be reduced.
2035 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002036 codec->cached_write = 1;
2037 for (i = 0; i < imux->num_items; i++) {
2038 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2039 get_adc_nid(codec, adc_idx, i));
2040 if (!path->ctls[type])
2041 continue;
2042 kcontrol->private_value = path->ctls[type];
2043 err = func(kcontrol, ucontrol);
2044 if (err < 0)
2045 goto error;
2046 }
2047 error:
2048 codec->cached_write = 0;
2049 mutex_unlock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002050 snd_hda_codec_flush_amp_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002051 if (err >= 0 && spec->cap_sync_hook)
2052 spec->cap_sync_hook(codec);
2053 return err;
2054}
2055
2056/* capture volume ctl callbacks */
2057#define cap_vol_info snd_hda_mixer_amp_volume_info
2058#define cap_vol_get snd_hda_mixer_amp_volume_get
2059#define cap_vol_tlv snd_hda_mixer_amp_tlv
2060
2061static int cap_vol_put(struct snd_kcontrol *kcontrol,
2062 struct snd_ctl_elem_value *ucontrol)
2063{
2064 return cap_put_caller(kcontrol, ucontrol,
2065 snd_hda_mixer_amp_volume_put,
2066 NID_PATH_VOL_CTL);
2067}
2068
2069static const struct snd_kcontrol_new cap_vol_temp = {
2070 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2071 .name = "Capture Volume",
2072 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
2073 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2074 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
2075 .info = cap_vol_info,
2076 .get = cap_vol_get,
2077 .put = cap_vol_put,
2078 .tlv = { .c = cap_vol_tlv },
2079};
2080
2081/* capture switch ctl callbacks */
2082#define cap_sw_info snd_ctl_boolean_stereo_info
2083#define cap_sw_get snd_hda_mixer_amp_switch_get
2084
2085static int cap_sw_put(struct snd_kcontrol *kcontrol,
2086 struct snd_ctl_elem_value *ucontrol)
2087{
2088 return cap_put_caller(kcontrol, ucontrol,
2089 snd_hda_mixer_amp_switch_put,
2090 NID_PATH_MUTE_CTL);
2091}
2092
2093static const struct snd_kcontrol_new cap_sw_temp = {
2094 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2095 .name = "Capture Switch",
2096 .info = cap_sw_info,
2097 .get = cap_sw_get,
2098 .put = cap_sw_put,
2099};
2100
2101static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
2102{
2103 hda_nid_t nid;
2104 int i, depth;
2105
2106 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
2107 for (depth = 0; depth < 3; depth++) {
2108 if (depth >= path->depth)
2109 return -EINVAL;
2110 i = path->depth - depth - 1;
2111 nid = path->path[i];
2112 if (!path->ctls[NID_PATH_VOL_CTL]) {
2113 if (nid_has_volume(codec, nid, HDA_OUTPUT))
2114 path->ctls[NID_PATH_VOL_CTL] =
2115 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2116 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
2117 int idx = path->idx[i];
2118 if (!depth && codec->single_adc_amp)
2119 idx = 0;
2120 path->ctls[NID_PATH_VOL_CTL] =
2121 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2122 }
2123 }
2124 if (!path->ctls[NID_PATH_MUTE_CTL]) {
2125 if (nid_has_mute(codec, nid, HDA_OUTPUT))
2126 path->ctls[NID_PATH_MUTE_CTL] =
2127 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2128 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
2129 int idx = path->idx[i];
2130 if (!depth && codec->single_adc_amp)
2131 idx = 0;
2132 path->ctls[NID_PATH_MUTE_CTL] =
2133 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2134 }
2135 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01002136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 return 0;
2138}
2139
Takashi Iwai352f7f92012-12-19 12:52:06 +01002140static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002142 struct hda_gen_spec *spec = codec->spec;
2143 struct auto_pin_cfg *cfg = &spec->autocfg;
2144 unsigned int val;
2145 int i;
2146
2147 if (!spec->inv_dmic_split)
2148 return false;
2149 for (i = 0; i < cfg->num_inputs; i++) {
2150 if (cfg->inputs[i].pin != nid)
2151 continue;
2152 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2153 return false;
2154 val = snd_hda_codec_get_pincfg(codec, nid);
2155 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
2156 }
2157 return false;
2158}
2159
2160static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
2161 int idx, bool is_switch, unsigned int ctl,
2162 bool inv_dmic)
2163{
2164 struct hda_gen_spec *spec = codec->spec;
2165 char tmpname[44];
2166 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
2167 const char *sfx = is_switch ? "Switch" : "Volume";
2168 unsigned int chs = inv_dmic ? 1 : 3;
2169 int err;
2170
2171 if (!ctl)
2172 return 0;
2173
2174 if (label)
2175 snprintf(tmpname, sizeof(tmpname),
2176 "%s Capture %s", label, sfx);
2177 else
2178 snprintf(tmpname, sizeof(tmpname),
2179 "Capture %s", sfx);
2180 err = add_control(spec, type, tmpname, idx,
2181 amp_val_replace_channels(ctl, chs));
2182 if (err < 0 || !inv_dmic)
2183 return err;
2184
2185 /* Make independent right kcontrol */
2186 if (label)
2187 snprintf(tmpname, sizeof(tmpname),
2188 "Inverted %s Capture %s", label, sfx);
2189 else
2190 snprintf(tmpname, sizeof(tmpname),
2191 "Inverted Capture %s", sfx);
2192 return add_control(spec, type, tmpname, idx,
2193 amp_val_replace_channels(ctl, 2));
2194}
2195
2196/* create single (and simple) capture volume and switch controls */
2197static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
2198 unsigned int vol_ctl, unsigned int sw_ctl,
2199 bool inv_dmic)
2200{
2201 int err;
2202 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
2203 if (err < 0)
2204 return err;
2205 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
2206 if (err < 0)
2207 return err;
2208 return 0;
2209}
2210
2211/* create bound capture volume and switch controls */
2212static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
2213 unsigned int vol_ctl, unsigned int sw_ctl)
2214{
2215 struct hda_gen_spec *spec = codec->spec;
2216 struct snd_kcontrol_new *knew;
2217
2218 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002219 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002220 if (!knew)
2221 return -ENOMEM;
2222 knew->index = idx;
2223 knew->private_value = vol_ctl;
2224 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2225 }
2226 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002227 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002228 if (!knew)
2229 return -ENOMEM;
2230 knew->index = idx;
2231 knew->private_value = sw_ctl;
2232 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2233 }
2234 return 0;
2235}
2236
2237/* return the vol ctl when used first in the imux list */
2238static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
2239{
2240 struct hda_gen_spec *spec = codec->spec;
2241 struct nid_path *path;
2242 unsigned int ctl;
2243 int i;
2244
2245 path = snd_hda_get_nid_path(codec, spec->imux_pins[idx],
2246 get_adc_nid(codec, 0, idx));
2247 if (!path)
2248 return 0;
2249 ctl = path->ctls[type];
2250 if (!ctl)
2251 return 0;
2252 for (i = 0; i < idx - 1; i++) {
2253 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2254 get_adc_nid(codec, 0, i));
2255 if (path && path->ctls[type] == ctl)
2256 return 0;
2257 }
2258 return ctl;
2259}
2260
2261/* create individual capture volume and switch controls per input */
2262static int create_multi_cap_vol_ctl(struct hda_codec *codec)
2263{
2264 struct hda_gen_spec *spec = codec->spec;
2265 struct hda_input_mux *imux = &spec->input_mux;
2266 int i, err, type, type_idx = 0;
2267 const char *prev_label = NULL;
2268
2269 for (i = 0; i < imux->num_items; i++) {
2270 const char *label;
2271 bool inv_dmic;
2272 label = hda_get_autocfg_input_label(codec, &spec->autocfg, i);
2273 if (prev_label && !strcmp(label, prev_label))
2274 type_idx++;
2275 else
2276 type_idx = 0;
2277 prev_label = label;
2278 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
2279
2280 for (type = 0; type < 2; type++) {
2281 err = add_single_cap_ctl(codec, label, type_idx, type,
2282 get_first_cap_ctl(codec, i, type),
2283 inv_dmic);
2284 if (err < 0)
2285 return err;
2286 }
2287 }
2288 return 0;
2289}
2290
2291static int create_capture_mixers(struct hda_codec *codec)
2292{
2293 struct hda_gen_spec *spec = codec->spec;
2294 struct hda_input_mux *imux = &spec->input_mux;
2295 int i, n, nums, err;
2296
2297 if (spec->dyn_adc_switch)
2298 nums = 1;
2299 else
2300 nums = spec->num_adc_nids;
2301
2302 if (!spec->auto_mic && imux->num_items > 1) {
2303 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01002304 const char *name;
2305 name = nums > 1 ? "Input Source" : "Capture Source";
2306 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002307 if (!knew)
2308 return -ENOMEM;
2309 knew->count = nums;
2310 }
2311
2312 for (n = 0; n < nums; n++) {
2313 bool multi = false;
2314 bool inv_dmic = false;
2315 int vol, sw;
2316
2317 vol = sw = 0;
2318 for (i = 0; i < imux->num_items; i++) {
2319 struct nid_path *path;
2320 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2321 get_adc_nid(codec, n, i));
2322 if (!path)
2323 continue;
2324 parse_capvol_in_path(codec, path);
2325 if (!vol)
2326 vol = path->ctls[NID_PATH_VOL_CTL];
2327 else if (vol != path->ctls[NID_PATH_VOL_CTL])
2328 multi = true;
2329 if (!sw)
2330 sw = path->ctls[NID_PATH_MUTE_CTL];
2331 else if (sw != path->ctls[NID_PATH_MUTE_CTL])
2332 multi = true;
2333 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
2334 inv_dmic = true;
2335 }
2336
2337 if (!multi)
2338 err = create_single_cap_vol_ctl(codec, n, vol, sw,
2339 inv_dmic);
2340 else if (!spec->multi_cap_vol)
2341 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
2342 else
2343 err = create_multi_cap_vol_ctl(codec);
2344 if (err < 0)
2345 return err;
2346 }
2347
2348 return 0;
2349}
2350
2351/*
2352 * add mic boosts if needed
2353 */
2354static int parse_mic_boost(struct hda_codec *codec)
2355{
2356 struct hda_gen_spec *spec = codec->spec;
2357 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002358 int i, err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002359 int type_idx = 0;
2360 hda_nid_t nid;
2361 const char *prev_label = NULL;
2362
2363 for (i = 0; i < cfg->num_inputs; i++) {
2364 if (cfg->inputs[i].type > AUTO_PIN_MIC)
2365 break;
2366 nid = cfg->inputs[i].pin;
2367 if (get_wcaps(codec, nid) & AC_WCAP_IN_AMP) {
2368 const char *label;
2369 char boost_label[32];
2370 struct nid_path *path;
2371 unsigned int val;
2372
2373 label = hda_get_autocfg_input_label(codec, cfg, i);
2374 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
2375 label = "Headphone Mic";
2376 if (prev_label && !strcmp(label, prev_label))
2377 type_idx++;
2378 else
2379 type_idx = 0;
2380 prev_label = label;
2381
2382 snprintf(boost_label, sizeof(boost_label),
2383 "%s Boost Volume", label);
2384 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
2385 err = add_control(spec, HDA_CTL_WIDGET_VOL,
2386 boost_label, type_idx, val);
2387 if (err < 0)
2388 return err;
2389
2390 path = snd_hda_get_nid_path(codec, nid, 0);
2391 if (path)
2392 path->ctls[NID_PATH_BOOST_CTL] = val;
2393 }
2394 }
2395 return 0;
2396}
2397
2398/*
2399 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
2400 */
2401static void parse_digital(struct hda_codec *codec)
2402{
2403 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002404 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002405 int i, nums;
2406 hda_nid_t dig_nid;
2407
2408 /* support multiple SPDIFs; the secondary is set up as a slave */
2409 nums = 0;
2410 for (i = 0; i < spec->autocfg.dig_outs; i++) {
2411 hda_nid_t pin = spec->autocfg.dig_out_pins[i];
2412 dig_nid = look_for_dac(codec, pin, true);
2413 if (!dig_nid)
2414 continue;
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01002415 path = snd_hda_add_new_path(codec, dig_nid, pin, HDA_PARSE_ALL);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002416 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002417 continue;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002418 print_nid_path("digout", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002419 if (!nums) {
2420 spec->multiout.dig_out_nid = dig_nid;
2421 spec->dig_out_type = spec->autocfg.dig_out_type[0];
2422 } else {
2423 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
2424 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
2425 break;
2426 spec->slave_dig_outs[nums - 1] = dig_nid;
2427 }
2428 nums++;
2429 }
2430
2431 if (spec->autocfg.dig_in_pin) {
2432 dig_nid = codec->start_nid;
2433 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002434 unsigned int wcaps = get_wcaps(codec, dig_nid);
2435 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
2436 continue;
2437 if (!(wcaps & AC_WCAP_DIGITAL))
2438 continue;
2439 path = snd_hda_add_new_path(codec,
2440 spec->autocfg.dig_in_pin,
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01002441 dig_nid, HDA_PARSE_ALL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002442 if (path) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002443 print_nid_path("digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002444 path->active = true;
2445 spec->dig_in_nid = dig_nid;
2446 break;
2447 }
2448 }
2449 }
2450}
2451
2452
2453/*
2454 * input MUX handling
2455 */
2456
2457static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
2458
2459/* select the given imux item; either unmute exclusively or select the route */
2460static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2461 unsigned int idx)
2462{
2463 struct hda_gen_spec *spec = codec->spec;
2464 const struct hda_input_mux *imux;
2465 struct nid_path *path;
2466
2467 imux = &spec->input_mux;
2468 if (!imux->num_items)
2469 return 0;
2470
2471 if (idx >= imux->num_items)
2472 idx = imux->num_items - 1;
2473 if (spec->cur_mux[adc_idx] == idx)
2474 return 0;
2475
2476 path = snd_hda_get_nid_path(codec,
2477 spec->imux_pins[spec->cur_mux[adc_idx]],
2478 spec->adc_nids[adc_idx]);
2479 if (!path)
2480 return 0;
2481 if (path->active)
2482 snd_hda_activate_path(codec, path, false, false);
2483
2484 spec->cur_mux[adc_idx] = idx;
2485
2486 if (spec->shared_mic_hp)
2487 update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
2488
2489 if (spec->dyn_adc_switch)
2490 dyn_adc_pcm_resetup(codec, idx);
2491
2492 path = snd_hda_get_nid_path(codec, spec->imux_pins[idx],
2493 get_adc_nid(codec, adc_idx, idx));
2494 if (!path)
2495 return 0;
2496 if (path->active)
2497 return 0;
2498 snd_hda_activate_path(codec, path, true, false);
2499 if (spec->cap_sync_hook)
2500 spec->cap_sync_hook(codec);
2501 return 1;
2502}
2503
2504
2505/*
2506 * Jack detections for HP auto-mute and mic-switch
2507 */
2508
2509/* check each pin in the given array; returns true if any of them is plugged */
2510static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
2511{
2512 int i, present = 0;
2513
2514 for (i = 0; i < num_pins; i++) {
2515 hda_nid_t nid = pins[i];
2516 if (!nid)
2517 break;
2518 present |= snd_hda_jack_detect(codec, nid);
2519 }
2520 return present;
2521}
2522
2523/* standard HP/line-out auto-mute helper */
2524static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
2525 bool mute, bool hp_out)
2526{
2527 struct hda_gen_spec *spec = codec->spec;
2528 unsigned int pin_bits = mute ? 0 : (hp_out ? PIN_HP : PIN_OUT);
2529 int i;
2530
2531 for (i = 0; i < num_pins; i++) {
2532 hda_nid_t nid = pins[i];
2533 unsigned int val;
2534 if (!nid)
2535 break;
2536 /* don't reset VREF value in case it's controlling
2537 * the amp (see alc861_fixup_asus_amp_vref_0f())
2538 */
2539 if (spec->keep_vref_in_automute) {
2540 val = snd_hda_codec_read(codec, nid, 0,
2541 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2542 val &= ~PIN_HP;
2543 } else
2544 val = 0;
2545 val |= pin_bits;
Takashi Iwai7594aa32012-12-20 15:38:40 +01002546 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002547 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002548 }
2549}
2550
2551/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002552void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002553{
2554 struct hda_gen_spec *spec = codec->spec;
2555 int on;
2556
2557 /* Control HP pins/amps depending on master_mute state;
2558 * in general, HP pins/amps control should be enabled in all cases,
2559 * but currently set only for master_mute, just to be safe
2560 */
2561 if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
2562 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2563 spec->autocfg.hp_pins, spec->master_mute, true);
2564
2565 if (!spec->automute_speaker)
2566 on = 0;
2567 else
2568 on = spec->hp_jack_present | spec->line_jack_present;
2569 on |= spec->master_mute;
2570 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
2571 spec->autocfg.speaker_pins, on, false);
2572
2573 /* toggle line-out mutes if needed, too */
2574 /* if LO is a copy of either HP or Speaker, don't need to handle it */
2575 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
2576 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
2577 return;
2578 if (!spec->automute_lo)
2579 on = 0;
2580 else
2581 on = spec->hp_jack_present;
2582 on |= spec->master_mute;
2583 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2584 spec->autocfg.line_out_pins, on, false);
2585}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002586EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002587
2588static void call_update_outputs(struct hda_codec *codec)
2589{
2590 struct hda_gen_spec *spec = codec->spec;
2591 if (spec->automute_hook)
2592 spec->automute_hook(codec);
2593 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01002594 snd_hda_gen_update_outputs(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002595}
2596
2597/* standard HP-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002598void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002599{
2600 struct hda_gen_spec *spec = codec->spec;
2601
2602 spec->hp_jack_present =
2603 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2604 spec->autocfg.hp_pins);
2605 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
2606 return;
2607 call_update_outputs(codec);
2608}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002609EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002610
2611/* standard line-out-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002612void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002613{
2614 struct hda_gen_spec *spec = codec->spec;
2615
2616 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
2617 return;
2618 /* check LO jack only when it's different from HP */
2619 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
2620 return;
2621
2622 spec->line_jack_present =
2623 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2624 spec->autocfg.line_out_pins);
2625 if (!spec->automute_speaker || !spec->detect_lo)
2626 return;
2627 call_update_outputs(codec);
2628}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002629EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002630
2631/* standard mic auto-switch helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002632void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002633{
2634 struct hda_gen_spec *spec = codec->spec;
2635 int i;
2636
2637 if (!spec->auto_mic)
2638 return;
2639
2640 for (i = spec->am_num_entries - 1; i > 0; i--) {
2641 if (snd_hda_jack_detect(codec, spec->am_entry[i].pin)) {
2642 mux_select(codec, 0, spec->am_entry[i].idx);
2643 return;
2644 }
2645 }
2646 mux_select(codec, 0, spec->am_entry[0].idx);
2647}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002648EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002649
2650/*
2651 * Auto-Mute mode mixer enum support
2652 */
2653static int automute_mode_info(struct snd_kcontrol *kcontrol,
2654 struct snd_ctl_elem_info *uinfo)
2655{
2656 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2657 struct hda_gen_spec *spec = codec->spec;
2658 static const char * const texts3[] = {
2659 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02002660 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661
Takashi Iwai352f7f92012-12-19 12:52:06 +01002662 if (spec->automute_speaker_possible && spec->automute_lo_possible)
2663 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
2664 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2665}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666
Takashi Iwai352f7f92012-12-19 12:52:06 +01002667static int automute_mode_get(struct snd_kcontrol *kcontrol,
2668 struct snd_ctl_elem_value *ucontrol)
2669{
2670 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2671 struct hda_gen_spec *spec = codec->spec;
2672 unsigned int val = 0;
2673 if (spec->automute_speaker)
2674 val++;
2675 if (spec->automute_lo)
2676 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002677
Takashi Iwai352f7f92012-12-19 12:52:06 +01002678 ucontrol->value.enumerated.item[0] = val;
2679 return 0;
2680}
2681
2682static int automute_mode_put(struct snd_kcontrol *kcontrol,
2683 struct snd_ctl_elem_value *ucontrol)
2684{
2685 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2686 struct hda_gen_spec *spec = codec->spec;
2687
2688 switch (ucontrol->value.enumerated.item[0]) {
2689 case 0:
2690 if (!spec->automute_speaker && !spec->automute_lo)
2691 return 0;
2692 spec->automute_speaker = 0;
2693 spec->automute_lo = 0;
2694 break;
2695 case 1:
2696 if (spec->automute_speaker_possible) {
2697 if (!spec->automute_lo && spec->automute_speaker)
2698 return 0;
2699 spec->automute_speaker = 1;
2700 spec->automute_lo = 0;
2701 } else if (spec->automute_lo_possible) {
2702 if (spec->automute_lo)
2703 return 0;
2704 spec->automute_lo = 1;
2705 } else
2706 return -EINVAL;
2707 break;
2708 case 2:
2709 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
2710 return -EINVAL;
2711 if (spec->automute_speaker && spec->automute_lo)
2712 return 0;
2713 spec->automute_speaker = 1;
2714 spec->automute_lo = 1;
2715 break;
2716 default:
2717 return -EINVAL;
2718 }
2719 call_update_outputs(codec);
2720 return 1;
2721}
2722
2723static const struct snd_kcontrol_new automute_mode_enum = {
2724 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2725 .name = "Auto-Mute Mode",
2726 .info = automute_mode_info,
2727 .get = automute_mode_get,
2728 .put = automute_mode_put,
2729};
2730
2731static int add_automute_mode_enum(struct hda_codec *codec)
2732{
2733 struct hda_gen_spec *spec = codec->spec;
2734
Takashi Iwai12c93df2012-12-19 14:38:33 +01002735 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002736 return -ENOMEM;
2737 return 0;
2738}
2739
2740/*
2741 * Check the availability of HP/line-out auto-mute;
2742 * Set up appropriately if really supported
2743 */
2744static int check_auto_mute_availability(struct hda_codec *codec)
2745{
2746 struct hda_gen_spec *spec = codec->spec;
2747 struct auto_pin_cfg *cfg = &spec->autocfg;
2748 int present = 0;
2749 int i, err;
2750
2751 if (cfg->hp_pins[0])
2752 present++;
2753 if (cfg->line_out_pins[0])
2754 present++;
2755 if (cfg->speaker_pins[0])
2756 present++;
2757 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02002758 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002759
2760 if (!cfg->speaker_pins[0] &&
2761 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
2762 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2763 sizeof(cfg->speaker_pins));
2764 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766
Takashi Iwai352f7f92012-12-19 12:52:06 +01002767 if (!cfg->hp_pins[0] &&
2768 cfg->line_out_type == AUTO_PIN_HP_OUT) {
2769 memcpy(cfg->hp_pins, cfg->line_out_pins,
2770 sizeof(cfg->hp_pins));
2771 cfg->hp_outs = cfg->line_outs;
2772 }
2773
2774 for (i = 0; i < cfg->hp_outs; i++) {
2775 hda_nid_t nid = cfg->hp_pins[i];
2776 if (!is_jack_detectable(codec, nid))
2777 continue;
2778 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
2779 nid);
2780 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
Takashi Iwai5d550e12012-12-19 15:16:44 +01002781 snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002782 spec->detect_hp = 1;
2783 }
2784
2785 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
2786 if (cfg->speaker_outs)
2787 for (i = 0; i < cfg->line_outs; i++) {
2788 hda_nid_t nid = cfg->line_out_pins[i];
2789 if (!is_jack_detectable(codec, nid))
2790 continue;
2791 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
2792 snd_hda_jack_detect_enable_callback(codec, nid,
2793 HDA_GEN_FRONT_EVENT,
Takashi Iwai5d550e12012-12-19 15:16:44 +01002794 snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002795 spec->detect_lo = 1;
2796 }
2797 spec->automute_lo_possible = spec->detect_hp;
2798 }
2799
2800 spec->automute_speaker_possible = cfg->speaker_outs &&
2801 (spec->detect_hp || spec->detect_lo);
2802
2803 spec->automute_lo = spec->automute_lo_possible;
2804 spec->automute_speaker = spec->automute_speaker_possible;
2805
2806 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
2807 /* create a control for automute mode */
2808 err = add_automute_mode_enum(codec);
2809 if (err < 0)
2810 return err;
2811 }
2812 return 0;
2813}
2814
2815/* return the position of NID in the list, or -1 if not found */
2816static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
2817{
2818 int i;
2819 for (i = 0; i < nums; i++)
2820 if (list[i] == nid)
2821 return i;
2822 return -1;
2823}
2824
2825/* check whether all auto-mic pins are valid; setup indices if OK */
2826static bool auto_mic_check_imux(struct hda_codec *codec)
2827{
2828 struct hda_gen_spec *spec = codec->spec;
2829 const struct hda_input_mux *imux;
2830 int i;
2831
2832 imux = &spec->input_mux;
2833 for (i = 0; i < spec->am_num_entries; i++) {
2834 spec->am_entry[i].idx =
2835 find_idx_in_nid_list(spec->am_entry[i].pin,
2836 spec->imux_pins, imux->num_items);
2837 if (spec->am_entry[i].idx < 0)
2838 return false; /* no corresponding imux */
2839 }
2840
2841 /* we don't need the jack detection for the first pin */
2842 for (i = 1; i < spec->am_num_entries; i++)
2843 snd_hda_jack_detect_enable_callback(codec,
2844 spec->am_entry[i].pin,
2845 HDA_GEN_MIC_EVENT,
Takashi Iwai5d550e12012-12-19 15:16:44 +01002846 snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002847 return true;
2848}
2849
2850static int compare_attr(const void *ap, const void *bp)
2851{
2852 const struct automic_entry *a = ap;
2853 const struct automic_entry *b = bp;
2854 return (int)(a->attr - b->attr);
2855}
2856
2857/*
2858 * Check the availability of auto-mic switch;
2859 * Set up if really supported
2860 */
2861static int check_auto_mic_availability(struct hda_codec *codec)
2862{
2863 struct hda_gen_spec *spec = codec->spec;
2864 struct auto_pin_cfg *cfg = &spec->autocfg;
2865 unsigned int types;
2866 int i, num_pins;
2867
2868 types = 0;
2869 num_pins = 0;
2870 for (i = 0; i < cfg->num_inputs; i++) {
2871 hda_nid_t nid = cfg->inputs[i].pin;
2872 unsigned int attr;
2873 attr = snd_hda_codec_get_pincfg(codec, nid);
2874 attr = snd_hda_get_input_pin_attr(attr);
2875 if (types & (1 << attr))
2876 return 0; /* already occupied */
2877 switch (attr) {
2878 case INPUT_PIN_ATTR_INT:
2879 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2880 return 0; /* invalid type */
2881 break;
2882 case INPUT_PIN_ATTR_UNUSED:
2883 return 0; /* invalid entry */
2884 default:
2885 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
2886 return 0; /* invalid type */
2887 if (!spec->line_in_auto_switch &&
2888 cfg->inputs[i].type != AUTO_PIN_MIC)
2889 return 0; /* only mic is allowed */
2890 if (!is_jack_detectable(codec, nid))
2891 return 0; /* no unsol support */
2892 break;
2893 }
2894 if (num_pins >= MAX_AUTO_MIC_PINS)
2895 return 0;
2896 types |= (1 << attr);
2897 spec->am_entry[num_pins].pin = nid;
2898 spec->am_entry[num_pins].attr = attr;
2899 num_pins++;
2900 }
2901
2902 if (num_pins < 2)
2903 return 0;
2904
2905 spec->am_num_entries = num_pins;
2906 /* sort the am_entry in the order of attr so that the pin with a
2907 * higher attr will be selected when the jack is plugged.
2908 */
2909 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
2910 compare_attr, NULL);
2911
2912 if (!auto_mic_check_imux(codec))
2913 return 0;
2914
2915 spec->auto_mic = 1;
2916 spec->num_adc_nids = 1;
2917 spec->cur_mux[0] = spec->am_entry[0].idx;
2918 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
2919 spec->am_entry[0].pin,
2920 spec->am_entry[1].pin,
2921 spec->am_entry[2].pin);
2922
2923 return 0;
2924}
2925
2926
Takashi Iwai9eb413e2012-12-19 14:41:21 +01002927/*
2928 * Parse the given BIOS configuration and set up the hda_gen_spec
2929 *
2930 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002931 * or a negative error code
2932 */
2933int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01002934 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002935{
2936 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002937 int err;
2938
Takashi Iwai9eb413e2012-12-19 14:41:21 +01002939 if (cfg != &spec->autocfg) {
2940 spec->autocfg = *cfg;
2941 cfg = &spec->autocfg;
2942 }
2943
Takashi Iwai352f7f92012-12-19 12:52:06 +01002944 if (!cfg->line_outs) {
2945 if (cfg->dig_outs || cfg->dig_in_pin) {
2946 spec->multiout.max_channels = 2;
2947 spec->no_analog = 1;
2948 goto dig_only;
2949 }
2950 return 0; /* can't find valid BIOS pin config */
2951 }
2952
2953 if (!spec->no_primary_hp &&
2954 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
2955 cfg->line_outs <= cfg->hp_outs) {
2956 /* use HP as primary out */
2957 cfg->speaker_outs = cfg->line_outs;
2958 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2959 sizeof(cfg->speaker_pins));
2960 cfg->line_outs = cfg->hp_outs;
2961 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
2962 cfg->hp_outs = 0;
2963 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
2964 cfg->line_out_type = AUTO_PIN_HP_OUT;
2965 }
2966
2967 err = parse_output_paths(codec);
2968 if (err < 0)
2969 return err;
2970 err = create_multi_channel_mode(codec);
2971 if (err < 0)
2972 return err;
2973 err = create_multi_out_ctls(codec, cfg);
2974 if (err < 0)
2975 return err;
2976 err = create_hp_out_ctls(codec);
2977 if (err < 0)
2978 return err;
2979 err = create_speaker_out_ctls(codec);
2980 if (err < 0)
2981 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002982 err = create_indep_hp_ctls(codec);
2983 if (err < 0)
2984 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002985 err = create_shared_input(codec);
2986 if (err < 0)
2987 return err;
2988 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002989 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02002990 return err;
2991
Takashi Iwai352f7f92012-12-19 12:52:06 +01002992 /* check the multiple speaker pins */
2993 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
2994 spec->const_channel_count = cfg->line_outs * 2;
2995 else
2996 spec->const_channel_count = cfg->speaker_outs * 2;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002997
Takashi Iwai352f7f92012-12-19 12:52:06 +01002998 if (spec->multi_ios > 0)
2999 spec->multiout.max_channels = max(spec->ext_channel_count,
3000 spec->const_channel_count);
3001 else
3002 spec->multiout.max_channels = spec->multiout.num_dacs * 2;
3003
3004 err = check_auto_mute_availability(codec);
3005 if (err < 0)
3006 return err;
3007
3008 err = check_dyn_adc_switch(codec);
3009 if (err < 0)
3010 return err;
3011
3012 if (!spec->shared_mic_hp) {
3013 err = check_auto_mic_availability(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003014 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 }
Takashi Iwai071c73a2006-08-23 18:34:06 +02003017
Takashi Iwai352f7f92012-12-19 12:52:06 +01003018 err = create_capture_mixers(codec);
3019 if (err < 0)
3020 return err;
3021
3022 err = parse_mic_boost(codec);
3023 if (err < 0)
3024 return err;
3025
3026 dig_only:
3027 parse_digital(codec);
3028
3029 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003031EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032
3033
3034/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003035 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003037
3038/* slave controls for virtual master */
3039static const char * const slave_pfxs[] = {
3040 "Front", "Surround", "Center", "LFE", "Side",
3041 "Headphone", "Speaker", "Mono", "Line Out",
3042 "CLFE", "Bass Speaker", "PCM",
3043 NULL,
3044};
3045
3046int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003048 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050
Takashi Iwai36502d02012-12-19 15:15:10 +01003051 if (spec->kctls.used) {
3052 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
3053 if (err < 0)
3054 return err;
3055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056
Takashi Iwai352f7f92012-12-19 12:52:06 +01003057 if (spec->multiout.dig_out_nid) {
3058 err = snd_hda_create_dig_out_ctls(codec,
3059 spec->multiout.dig_out_nid,
3060 spec->multiout.dig_out_nid,
3061 spec->pcm_rec[1].pcm_type);
3062 if (err < 0)
3063 return err;
3064 if (!spec->no_analog) {
3065 err = snd_hda_create_spdif_share_sw(codec,
3066 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 if (err < 0)
3068 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003069 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 }
3071 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003072 if (spec->dig_in_nid) {
3073 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
3074 if (err < 0)
3075 return err;
3076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077
Takashi Iwai352f7f92012-12-19 12:52:06 +01003078 /* if we have no master control, let's create it */
3079 if (!spec->no_analog &&
3080 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
3081 unsigned int vmaster_tlv[4];
3082 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
3083 HDA_OUTPUT, vmaster_tlv);
3084 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
3085 vmaster_tlv, slave_pfxs,
3086 "Playback Volume");
3087 if (err < 0)
3088 return err;
3089 }
3090 if (!spec->no_analog &&
3091 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
3092 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
3093 NULL, slave_pfxs,
3094 "Playback Switch",
3095 true, &spec->vmaster_mute.sw_kctl);
3096 if (err < 0)
3097 return err;
3098 if (spec->vmaster_mute.hook)
Takashi Iwaifd25a972012-12-20 14:57:18 +01003099 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
3100 spec->vmaster_mute_enum);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102
Takashi Iwai352f7f92012-12-19 12:52:06 +01003103 free_kctls(spec); /* no longer needed */
3104
3105 if (spec->shared_mic_hp) {
3106 int err;
3107 int nid = spec->autocfg.inputs[1].pin;
3108 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
3109 if (err < 0)
3110 return err;
3111 err = snd_hda_jack_detect_enable(codec, nid, 0);
3112 if (err < 0)
3113 return err;
3114 }
3115
3116 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
3117 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 return err;
3119
3120 return 0;
3121}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003122EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
3123
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124
3125/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003126 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128
Takashi Iwai352f7f92012-12-19 12:52:06 +01003129/*
3130 * Analog playback callbacks
3131 */
3132static int playback_pcm_open(struct hda_pcm_stream *hinfo,
3133 struct hda_codec *codec,
3134 struct snd_pcm_substream *substream)
3135{
3136 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003137 int err;
3138
3139 mutex_lock(&spec->pcm_mutex);
3140 err = snd_hda_multi_out_analog_open(codec,
3141 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003142 hinfo);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003143 if (!err)
3144 spec->active_streams |= 1 << STREAM_MULTI_OUT;
3145 mutex_unlock(&spec->pcm_mutex);
3146 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003147}
3148
3149static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01003150 struct hda_codec *codec,
3151 unsigned int stream_tag,
3152 unsigned int format,
3153 struct snd_pcm_substream *substream)
3154{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003155 struct hda_gen_spec *spec = codec->spec;
3156 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
3157 stream_tag, format, substream);
3158}
Takashi Iwai97ec5582006-03-21 11:29:07 +01003159
Takashi Iwai352f7f92012-12-19 12:52:06 +01003160static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3161 struct hda_codec *codec,
3162 struct snd_pcm_substream *substream)
3163{
3164 struct hda_gen_spec *spec = codec->spec;
3165 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
3166}
3167
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003168static int playback_pcm_close(struct hda_pcm_stream *hinfo,
3169 struct hda_codec *codec,
3170 struct snd_pcm_substream *substream)
3171{
3172 struct hda_gen_spec *spec = codec->spec;
3173 mutex_lock(&spec->pcm_mutex);
3174 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
3175 mutex_unlock(&spec->pcm_mutex);
3176 return 0;
3177}
3178
3179static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
3180 struct hda_codec *codec,
3181 struct snd_pcm_substream *substream)
3182{
3183 struct hda_gen_spec *spec = codec->spec;
3184 int err = 0;
3185
3186 mutex_lock(&spec->pcm_mutex);
3187 if (!spec->indep_hp_enabled)
3188 err = -EBUSY;
3189 else
3190 spec->active_streams |= 1 << STREAM_INDEP_HP;
3191 mutex_unlock(&spec->pcm_mutex);
3192 return err;
3193}
3194
3195static int alt_playback_pcm_close(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 mutex_lock(&spec->pcm_mutex);
3201 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
3202 mutex_unlock(&spec->pcm_mutex);
3203 return 0;
3204}
3205
Takashi Iwai352f7f92012-12-19 12:52:06 +01003206/*
3207 * Digital out
3208 */
3209static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
3210 struct hda_codec *codec,
3211 struct snd_pcm_substream *substream)
3212{
3213 struct hda_gen_spec *spec = codec->spec;
3214 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3215}
3216
3217static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3218 struct hda_codec *codec,
3219 unsigned int stream_tag,
3220 unsigned int format,
3221 struct snd_pcm_substream *substream)
3222{
3223 struct hda_gen_spec *spec = codec->spec;
3224 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3225 stream_tag, format, substream);
3226}
3227
3228static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3229 struct hda_codec *codec,
3230 struct snd_pcm_substream *substream)
3231{
3232 struct hda_gen_spec *spec = codec->spec;
3233 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
3234}
3235
3236static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
3237 struct hda_codec *codec,
3238 struct snd_pcm_substream *substream)
3239{
3240 struct hda_gen_spec *spec = codec->spec;
3241 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3242}
3243
3244/*
3245 * Analog capture
3246 */
3247static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3248 struct hda_codec *codec,
3249 unsigned int stream_tag,
3250 unsigned int format,
3251 struct snd_pcm_substream *substream)
3252{
3253 struct hda_gen_spec *spec = codec->spec;
3254
3255 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01003256 stream_tag, 0, format);
3257 return 0;
3258}
3259
Takashi Iwai352f7f92012-12-19 12:52:06 +01003260static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3261 struct hda_codec *codec,
3262 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01003263{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003264 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01003265
Takashi Iwai352f7f92012-12-19 12:52:06 +01003266 snd_hda_codec_cleanup_stream(codec,
3267 spec->adc_nids[substream->number + 1]);
Takashi Iwai97ec5582006-03-21 11:29:07 +01003268 return 0;
3269}
3270
Takashi Iwai352f7f92012-12-19 12:52:06 +01003271/*
3272 */
3273static const struct hda_pcm_stream pcm_analog_playback = {
3274 .substreams = 1,
3275 .channels_min = 2,
3276 .channels_max = 8,
3277 /* NID is set in build_pcms */
3278 .ops = {
3279 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003280 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003281 .prepare = playback_pcm_prepare,
3282 .cleanup = playback_pcm_cleanup
3283 },
3284};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285
Takashi Iwai352f7f92012-12-19 12:52:06 +01003286static const struct hda_pcm_stream pcm_analog_capture = {
3287 .substreams = 1,
3288 .channels_min = 2,
3289 .channels_max = 2,
3290 /* NID is set in build_pcms */
3291};
3292
3293static const struct hda_pcm_stream pcm_analog_alt_playback = {
3294 .substreams = 1,
3295 .channels_min = 2,
3296 .channels_max = 2,
3297 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003298 .ops = {
3299 .open = alt_playback_pcm_open,
3300 .close = alt_playback_pcm_close
3301 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01003302};
3303
3304static const struct hda_pcm_stream pcm_analog_alt_capture = {
3305 .substreams = 2, /* can be overridden */
3306 .channels_min = 2,
3307 .channels_max = 2,
3308 /* NID is set in build_pcms */
3309 .ops = {
3310 .prepare = alt_capture_pcm_prepare,
3311 .cleanup = alt_capture_pcm_cleanup
3312 },
3313};
3314
3315static const struct hda_pcm_stream pcm_digital_playback = {
3316 .substreams = 1,
3317 .channels_min = 2,
3318 .channels_max = 2,
3319 /* NID is set in build_pcms */
3320 .ops = {
3321 .open = dig_playback_pcm_open,
3322 .close = dig_playback_pcm_close,
3323 .prepare = dig_playback_pcm_prepare,
3324 .cleanup = dig_playback_pcm_cleanup
3325 },
3326};
3327
3328static const struct hda_pcm_stream pcm_digital_capture = {
3329 .substreams = 1,
3330 .channels_min = 2,
3331 .channels_max = 2,
3332 /* NID is set in build_pcms */
3333};
3334
3335/* Used by build_pcms to flag that a PCM has no playback stream */
3336static const struct hda_pcm_stream pcm_null_stream = {
3337 .substreams = 0,
3338 .channels_min = 0,
3339 .channels_max = 0,
3340};
3341
3342/*
3343 * dynamic changing ADC PCM streams
3344 */
3345static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
3346{
3347 struct hda_gen_spec *spec = codec->spec;
3348 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
3349
3350 if (spec->cur_adc && spec->cur_adc != new_adc) {
3351 /* stream is running, let's swap the current ADC */
3352 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
3353 spec->cur_adc = new_adc;
3354 snd_hda_codec_setup_stream(codec, new_adc,
3355 spec->cur_adc_stream_tag, 0,
3356 spec->cur_adc_format);
3357 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003359 return false;
3360}
3361
3362/* analog capture with dynamic dual-adc changes */
3363static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3364 struct hda_codec *codec,
3365 unsigned int stream_tag,
3366 unsigned int format,
3367 struct snd_pcm_substream *substream)
3368{
3369 struct hda_gen_spec *spec = codec->spec;
3370 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
3371 spec->cur_adc_stream_tag = stream_tag;
3372 spec->cur_adc_format = format;
3373 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
3374 return 0;
3375}
3376
3377static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3378 struct hda_codec *codec,
3379 struct snd_pcm_substream *substream)
3380{
3381 struct hda_gen_spec *spec = codec->spec;
3382 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
3383 spec->cur_adc = 0;
3384 return 0;
3385}
3386
3387static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
3388 .substreams = 1,
3389 .channels_min = 2,
3390 .channels_max = 2,
3391 .nid = 0, /* fill later */
3392 .ops = {
3393 .prepare = dyn_adc_capture_pcm_prepare,
3394 .cleanup = dyn_adc_capture_pcm_cleanup
3395 },
3396};
3397
Takashi Iwaif873e532012-12-20 16:58:39 +01003398static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
3399 const char *chip_name)
3400{
3401 char *p;
3402
3403 if (*str)
3404 return;
3405 strlcpy(str, chip_name, len);
3406
3407 /* drop non-alnum chars after a space */
3408 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
3409 if (!isalnum(p[1])) {
3410 *p = 0;
3411 break;
3412 }
3413 }
3414 strlcat(str, sfx, len);
3415}
3416
Takashi Iwai352f7f92012-12-19 12:52:06 +01003417/* build PCM streams based on the parsed results */
3418int snd_hda_gen_build_pcms(struct hda_codec *codec)
3419{
3420 struct hda_gen_spec *spec = codec->spec;
3421 struct hda_pcm *info = spec->pcm_rec;
3422 const struct hda_pcm_stream *p;
3423 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424
3425 codec->num_pcms = 1;
3426 codec->pcm_info = info;
3427
Takashi Iwai352f7f92012-12-19 12:52:06 +01003428 if (spec->no_analog)
3429 goto skip_analog;
3430
Takashi Iwaif873e532012-12-20 16:58:39 +01003431 fill_pcm_stream_name(spec->stream_name_analog,
3432 sizeof(spec->stream_name_analog),
3433 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003434 info->name = spec->stream_name_analog;
3435
3436 if (spec->multiout.num_dacs > 0) {
3437 p = spec->stream_analog_playback;
3438 if (!p)
3439 p = &pcm_analog_playback;
3440 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3441 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
3442 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
3443 spec->multiout.max_channels;
3444 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
3445 spec->autocfg.line_outs == 2)
3446 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
3447 snd_pcm_2_1_chmaps;
3448 }
3449 if (spec->num_adc_nids) {
3450 p = spec->stream_analog_capture;
3451 if (!p) {
3452 if (spec->dyn_adc_switch)
3453 p = &dyn_adc_pcm_analog_capture;
3454 else
3455 p = &pcm_analog_capture;
3456 }
3457 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3458 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
3459 }
3460
Takashi Iwai352f7f92012-12-19 12:52:06 +01003461 skip_analog:
3462 /* SPDIF for stream index #1 */
3463 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01003464 fill_pcm_stream_name(spec->stream_name_digital,
3465 sizeof(spec->stream_name_digital),
3466 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003467 codec->num_pcms = 2;
3468 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
3469 info = spec->pcm_rec + 1;
3470 info->name = spec->stream_name_digital;
3471 if (spec->dig_out_type)
3472 info->pcm_type = spec->dig_out_type;
3473 else
3474 info->pcm_type = HDA_PCM_TYPE_SPDIF;
3475 if (spec->multiout.dig_out_nid) {
3476 p = spec->stream_digital_playback;
3477 if (!p)
3478 p = &pcm_digital_playback;
3479 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3480 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
3481 }
3482 if (spec->dig_in_nid) {
3483 p = spec->stream_digital_capture;
3484 if (!p)
3485 p = &pcm_digital_capture;
3486 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3487 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
3488 }
3489 }
3490
3491 if (spec->no_analog)
3492 return 0;
3493
3494 /* If the use of more than one ADC is requested for the current
3495 * model, configure a second analog capture-only PCM.
3496 */
3497 have_multi_adcs = (spec->num_adc_nids > 1) &&
3498 !spec->dyn_adc_switch && !spec->auto_mic;
3499 /* Additional Analaog capture for index #2 */
3500 if (spec->alt_dac_nid || have_multi_adcs) {
3501 codec->num_pcms = 3;
3502 info = spec->pcm_rec + 2;
3503 info->name = spec->stream_name_analog;
3504 if (spec->alt_dac_nid) {
3505 p = spec->stream_analog_alt_playback;
3506 if (!p)
3507 p = &pcm_analog_alt_playback;
3508 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3509 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
3510 spec->alt_dac_nid;
3511 } else {
3512 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
3513 pcm_null_stream;
3514 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
3515 }
3516 if (have_multi_adcs) {
3517 p = spec->stream_analog_alt_capture;
3518 if (!p)
3519 p = &pcm_analog_alt_capture;
3520 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3521 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
3522 spec->adc_nids[1];
3523 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
3524 spec->num_adc_nids - 1;
3525 } else {
3526 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
3527 pcm_null_stream;
3528 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
3529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 }
3531
3532 return 0;
3533}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003534EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
3535
3536
3537/*
3538 * Standard auto-parser initializations
3539 */
3540
3541/* configure the path from the given dac to the pin as the proper output */
3542static void set_output_and_unmute(struct hda_codec *codec, hda_nid_t pin,
3543 int pin_type, hda_nid_t dac)
3544{
3545 struct nid_path *path;
3546
3547 snd_hda_set_pin_ctl_cache(codec, pin, pin_type);
3548 path = snd_hda_get_nid_path(codec, dac, pin);
3549 if (!path)
3550 return;
3551 if (path->active)
3552 return;
3553 snd_hda_activate_path(codec, path, true, true);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01003554 set_pin_eapd(codec, pin, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003555}
3556
3557/* initialize primary output paths */
3558static void init_multi_out(struct hda_codec *codec)
3559{
3560 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai64049c82012-12-20 15:29:21 +01003561 hda_nid_t nid, dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003562 int pin_type;
3563 int i;
3564
3565 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
3566 pin_type = PIN_HP;
3567 else
3568 pin_type = PIN_OUT;
3569
Takashi Iwai64049c82012-12-20 15:29:21 +01003570 for (i = 0; i < spec->autocfg.line_outs; i++) {
3571 nid = spec->autocfg.line_out_pins[i];
3572 if (nid) {
3573 dac = spec->multiout.dac_nids[i];
3574 if (!dac)
3575 dac = spec->multiout.dac_nids[0];
3576 set_output_and_unmute(codec, nid, pin_type, dac);
3577 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003578 }
3579}
3580
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003581
3582static void __init_extra_out(struct hda_codec *codec, int num_outs,
3583 hda_nid_t *pins, hda_nid_t *dacs, int type)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003584{
3585 struct hda_gen_spec *spec = codec->spec;
3586 int i;
3587 hda_nid_t pin, dac;
3588
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003589 for (i = 0; i < num_outs; i++) {
3590 pin = pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003591 if (!pin)
3592 break;
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003593 dac = dacs[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003594 if (!dac) {
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003595 if (i > 0 && dacs[0])
3596 dac = dacs[0];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003597 else
3598 dac = spec->multiout.dac_nids[0];
3599 }
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003600 set_output_and_unmute(codec, pin, type, dac);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003601 }
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003602}
3603
3604/* initialize hp and speaker paths */
3605static void init_extra_out(struct hda_codec *codec)
3606{
3607 struct hda_gen_spec *spec = codec->spec;
3608
3609 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
3610 __init_extra_out(codec, spec->autocfg.hp_outs,
3611 spec->autocfg.hp_pins,
3612 spec->multiout.hp_out_nid, PIN_HP);
3613 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
3614 __init_extra_out(codec, spec->autocfg.speaker_outs,
3615 spec->autocfg.speaker_pins,
3616 spec->multiout.extra_out_nid, PIN_OUT);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003617}
3618
3619/* initialize multi-io paths */
3620static void init_multi_io(struct hda_codec *codec)
3621{
3622 struct hda_gen_spec *spec = codec->spec;
3623 int i;
3624
3625 for (i = 0; i < spec->multi_ios; i++) {
3626 hda_nid_t pin = spec->multi_io[i].pin;
3627 struct nid_path *path;
3628 path = snd_hda_get_nid_path(codec, spec->multi_io[i].dac, pin);
3629 if (!path)
3630 continue;
3631 if (!spec->multi_io[i].ctl_in)
3632 spec->multi_io[i].ctl_in =
3633 snd_hda_codec_update_cache(codec, pin, 0,
3634 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3635 snd_hda_activate_path(codec, path, path->active, true);
3636 }
3637}
3638
3639/* set up the input pin config, depending on the given auto-pin type */
3640static void set_input_pin(struct hda_codec *codec, hda_nid_t nid,
3641 int auto_pin_type)
3642{
3643 unsigned int val = PIN_IN;
3644 if (auto_pin_type == AUTO_PIN_MIC)
3645 val |= snd_hda_get_default_vref(codec, nid);
Takashi Iwai7594aa32012-12-20 15:38:40 +01003646 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003647}
3648
3649/* set up input pins and loopback paths */
3650static void init_analog_input(struct hda_codec *codec)
3651{
3652 struct hda_gen_spec *spec = codec->spec;
3653 struct auto_pin_cfg *cfg = &spec->autocfg;
3654 int i;
3655
3656 for (i = 0; i < cfg->num_inputs; i++) {
3657 hda_nid_t nid = cfg->inputs[i].pin;
3658 if (is_input_pin(codec, nid))
3659 set_input_pin(codec, nid, cfg->inputs[i].type);
3660
3661 /* init loopback inputs */
3662 if (spec->mixer_nid) {
3663 struct nid_path *path;
3664 path = snd_hda_get_nid_path(codec, nid, spec->mixer_nid);
3665 if (path)
3666 snd_hda_activate_path(codec, path,
3667 path->active, false);
3668 }
3669 }
3670}
3671
3672/* initialize ADC paths */
3673static void init_input_src(struct hda_codec *codec)
3674{
3675 struct hda_gen_spec *spec = codec->spec;
3676 struct hda_input_mux *imux = &spec->input_mux;
3677 struct nid_path *path;
3678 int i, c, nums;
3679
3680 if (spec->dyn_adc_switch)
3681 nums = 1;
3682 else
3683 nums = spec->num_adc_nids;
3684
3685 for (c = 0; c < nums; c++) {
3686 for (i = 0; i < imux->num_items; i++) {
3687 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
3688 get_adc_nid(codec, c, i));
3689 if (path) {
3690 bool active = path->active;
3691 if (i == spec->cur_mux[c])
3692 active = true;
3693 snd_hda_activate_path(codec, path, active, false);
3694 }
3695 }
3696 }
3697
3698 if (spec->shared_mic_hp)
3699 update_shared_mic_hp(codec, spec->cur_mux[0]);
3700
3701 if (spec->cap_sync_hook)
3702 spec->cap_sync_hook(codec);
3703}
3704
3705/* set right pin controls for digital I/O */
3706static void init_digital(struct hda_codec *codec)
3707{
3708 struct hda_gen_spec *spec = codec->spec;
3709 int i;
3710 hda_nid_t pin;
3711
3712 for (i = 0; i < spec->autocfg.dig_outs; i++) {
3713 pin = spec->autocfg.dig_out_pins[i];
3714 if (!pin)
3715 continue;
3716 set_output_and_unmute(codec, pin, PIN_OUT, 0);
3717 }
3718 pin = spec->autocfg.dig_in_pin;
3719 if (pin)
Takashi Iwai7594aa32012-12-20 15:38:40 +01003720 snd_hda_set_pin_ctl_cache(codec, pin, PIN_IN);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003721}
3722
Takashi Iwai973e4972012-12-20 15:16:09 +01003723/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
3724 * invalid unsol tags by some reason
3725 */
3726static void clear_unsol_on_unused_pins(struct hda_codec *codec)
3727{
3728 int i;
3729
3730 for (i = 0; i < codec->init_pins.used; i++) {
3731 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
3732 hda_nid_t nid = pin->nid;
3733 if (is_jack_detectable(codec, nid) &&
3734 !snd_hda_jack_tbl_get(codec, nid))
3735 snd_hda_codec_update_cache(codec, nid, 0,
3736 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
3737 }
3738}
3739
Takashi Iwai352f7f92012-12-19 12:52:06 +01003740int snd_hda_gen_init(struct hda_codec *codec)
3741{
3742 struct hda_gen_spec *spec = codec->spec;
3743
3744 if (spec->init_hook)
3745 spec->init_hook(codec);
3746
3747 snd_hda_apply_verbs(codec);
3748
Takashi Iwai3bbcd272012-12-20 11:50:58 +01003749 codec->cached_write = 1;
3750
Takashi Iwai352f7f92012-12-19 12:52:06 +01003751 init_multi_out(codec);
3752 init_extra_out(codec);
3753 init_multi_io(codec);
3754 init_analog_input(codec);
3755 init_input_src(codec);
3756 init_digital(codec);
3757
Takashi Iwai973e4972012-12-20 15:16:09 +01003758 clear_unsol_on_unused_pins(codec);
3759
Takashi Iwai352f7f92012-12-19 12:52:06 +01003760 /* call init functions of standard auto-mute helpers */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003761 snd_hda_gen_hp_automute(codec, NULL);
3762 snd_hda_gen_line_automute(codec, NULL);
3763 snd_hda_gen_mic_autoswitch(codec, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003764
Takashi Iwai3bbcd272012-12-20 11:50:58 +01003765 snd_hda_codec_flush_amp_cache(codec);
3766 snd_hda_codec_flush_cmd_cache(codec);
3767
Takashi Iwai352f7f92012-12-19 12:52:06 +01003768 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
3769 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
3770
3771 hda_call_check_power_status(codec, 0x01);
3772 return 0;
3773}
3774EXPORT_SYMBOL(snd_hda_gen_init);
3775
3776
3777/*
3778 * the generic codec support
3779 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780
Takashi Iwai83012a72012-08-24 18:38:08 +02003781#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003782static int generic_check_power_status(struct hda_codec *codec, hda_nid_t nid)
3783{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003784 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003785 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
3786}
3787#endif
3788
Takashi Iwai352f7f92012-12-19 12:52:06 +01003789static void generic_free(struct hda_codec *codec)
3790{
3791 snd_hda_gen_spec_free(codec->spec);
3792 kfree(codec->spec);
3793 codec->spec = NULL;
3794}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795
Takashi Iwai352f7f92012-12-19 12:52:06 +01003796static const struct hda_codec_ops generic_patch_ops = {
3797 .build_controls = snd_hda_gen_build_controls,
3798 .build_pcms = snd_hda_gen_build_pcms,
3799 .init = snd_hda_gen_init,
3800 .free = generic_free,
3801 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02003802#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003803 .check_power_status = generic_check_power_status,
3804#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805};
3806
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807int snd_hda_parse_generic_codec(struct hda_codec *codec)
3808{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003809 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 int err;
3811
Takashi Iwaie560d8d2005-09-09 14:21:46 +02003812 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003813 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003815 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003818 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
3819 if (err < 0)
3820 return err;
3821
3822 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003823 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 goto error;
3825
3826 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 return 0;
3828
Takashi Iwai352f7f92012-12-19 12:52:06 +01003829error:
3830 generic_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831 return err;
3832}
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003833EXPORT_SYMBOL(snd_hda_parse_generic_codec);