blob: 296628b6ffc2a140bae22508556e11c2ce385758 [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);
Takashi Iwai352f7f92012-12-19 12:52:06 +010042 snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
Takashi Iwai38cf6f12012-12-21 14:09:42 +010043 mutex_init(&spec->pcm_mutex);
Takashi Iwai352f7f92012-12-19 12:52:06 +010044 return 0;
45}
46EXPORT_SYMBOL_HDA(snd_hda_gen_spec_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Takashi Iwai12c93df2012-12-19 14:38:33 +010048struct snd_kcontrol_new *
49snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
50 const struct snd_kcontrol_new *temp)
Takashi Iwai352f7f92012-12-19 12:52:06 +010051{
52 struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
53 if (!knew)
54 return NULL;
55 *knew = *temp;
56 if (name)
57 knew->name = kstrdup(name, GFP_KERNEL);
58 else if (knew->name)
59 knew->name = kstrdup(knew->name, GFP_KERNEL);
60 if (!knew->name)
61 return NULL;
62 return knew;
63}
Takashi Iwai12c93df2012-12-19 14:38:33 +010064EXPORT_SYMBOL_HDA(snd_hda_gen_add_kctl);
Takashi Iwai352f7f92012-12-19 12:52:06 +010065
66static void free_kctls(struct hda_gen_spec *spec)
67{
68 if (spec->kctls.list) {
69 struct snd_kcontrol_new *kctl = spec->kctls.list;
70 int i;
71 for (i = 0; i < spec->kctls.used; i++)
72 kfree(kctl[i].name);
73 }
74 snd_array_free(&spec->kctls);
75}
76
Takashi Iwai352f7f92012-12-19 12:52:06 +010077void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
78{
79 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 return;
Takashi Iwai352f7f92012-12-19 12:52:06 +010081 free_kctls(spec);
Takashi Iwai352f7f92012-12-19 12:52:06 +010082 snd_array_free(&spec->paths);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
Takashi Iwai352f7f92012-12-19 12:52:06 +010084EXPORT_SYMBOL_HDA(snd_hda_gen_spec_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86/*
Takashi Iwai352f7f92012-12-19 12:52:06 +010087 * parsing paths
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Takashi Iwaif5172a72013-01-04 13:19:55 +010090static struct nid_path *get_nid_path(struct hda_codec *codec,
91 hda_nid_t from_nid, hda_nid_t to_nid,
92 int with_aa_mix)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Takashi Iwai352f7f92012-12-19 12:52:06 +010094 struct hda_gen_spec *spec = codec->spec;
95 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Takashi Iwai352f7f92012-12-19 12:52:06 +010097 for (i = 0; i < spec->paths.used; i++) {
98 struct nid_path *path = snd_array_elem(&spec->paths, i);
99 if (path->depth <= 0)
100 continue;
101 if ((!from_nid || path->path[0] == from_nid) &&
Takashi Iwaif5172a72013-01-04 13:19:55 +0100102 (!to_nid || path->path[path->depth - 1] == to_nid)) {
103 if (with_aa_mix == HDA_PARSE_ALL ||
104 path->with_aa_mix == with_aa_mix)
105 return path;
106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 }
108 return NULL;
109}
Takashi Iwaif5172a72013-01-04 13:19:55 +0100110
111/* get the path between the given NIDs;
112 * passing 0 to either @pin or @dac behaves as a wildcard
113 */
114struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
115 hda_nid_t from_nid, hda_nid_t to_nid)
116{
117 return get_nid_path(codec, from_nid, to_nid, HDA_PARSE_ALL);
118}
Takashi Iwai352f7f92012-12-19 12:52:06 +0100119EXPORT_SYMBOL_HDA(snd_hda_get_nid_path);
120
Takashi Iwai196c17662013-01-04 15:01:40 +0100121/* get the index number corresponding to the path instance;
122 * the index starts from 1, for easier checking the invalid value
123 */
124int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
125{
126 struct hda_gen_spec *spec = codec->spec;
127 struct nid_path *array = spec->paths.list;
128 ssize_t idx;
129
130 if (!spec->paths.used)
131 return 0;
132 idx = path - array;
133 if (idx < 0 || idx >= spec->paths.used)
134 return 0;
135 return idx + 1;
136}
137
138/* get the path instance corresponding to the given index number */
139struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
140{
141 struct hda_gen_spec *spec = codec->spec;
142
143 if (idx <= 0 || idx > spec->paths.used)
144 return NULL;
145 return snd_array_elem(&spec->paths, idx - 1);
146}
147
Takashi Iwai352f7f92012-12-19 12:52:06 +0100148/* check whether the given DAC is already found in any existing paths */
149static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
150{
151 struct hda_gen_spec *spec = codec->spec;
152 int i;
153
154 for (i = 0; i < spec->paths.used; i++) {
155 struct nid_path *path = snd_array_elem(&spec->paths, i);
156 if (path->path[0] == nid)
157 return true;
158 }
159 return false;
160}
161
162/* check whether the given two widgets can be connected */
163static bool is_reachable_path(struct hda_codec *codec,
164 hda_nid_t from_nid, hda_nid_t to_nid)
165{
166 if (!from_nid || !to_nid)
167 return false;
168 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
169}
170
171/* nid, dir and idx */
172#define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
173
174/* check whether the given ctl is already assigned in any path elements */
175static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
176{
177 struct hda_gen_spec *spec = codec->spec;
178 int i;
179
180 val &= AMP_VAL_COMPARE_MASK;
181 for (i = 0; i < spec->paths.used; i++) {
182 struct nid_path *path = snd_array_elem(&spec->paths, i);
183 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
184 return true;
185 }
186 return false;
187}
188
189/* check whether a control with the given (nid, dir, idx) was assigned */
190static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
191 int dir, int idx)
192{
193 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
194 return is_ctl_used(codec, val, NID_PATH_VOL_CTL) ||
195 is_ctl_used(codec, val, NID_PATH_MUTE_CTL);
196}
197
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100198static void print_nid_path(const char *pfx, struct nid_path *path)
199{
200 char buf[40];
201 int i;
202
203
204 buf[0] = 0;
205 for (i = 0; i < path->depth; i++) {
206 char tmp[4];
207 sprintf(tmp, ":%02x", path->path[i]);
208 strlcat(buf, tmp, sizeof(buf));
209 }
210 snd_printdd("%s path: depth=%d %s\n", pfx, path->depth, buf);
211}
212
Takashi Iwai352f7f92012-12-19 12:52:06 +0100213/* called recursively */
214static bool __parse_nid_path(struct hda_codec *codec,
215 hda_nid_t from_nid, hda_nid_t to_nid,
216 int with_aa_mix, struct nid_path *path, int depth)
217{
218 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100219 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100220 int i, nums;
221
222 if (to_nid == spec->mixer_nid) {
Takashi Iwai4ac0eef2012-12-20 18:10:51 +0100223 if (with_aa_mix == HDA_PARSE_NO_AAMIX)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100224 return false;
Takashi Iwai4ac0eef2012-12-20 18:10:51 +0100225 with_aa_mix = HDA_PARSE_ALL; /* mark aa-mix is included */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100226 }
227
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100228 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100229 for (i = 0; i < nums; i++) {
230 if (conn[i] != from_nid) {
231 /* special case: when from_nid is 0,
232 * try to find an empty DAC
233 */
234 if (from_nid ||
235 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
236 is_dac_already_used(codec, conn[i]))
237 continue;
238 }
239 /* aa-mix is requested but not included? */
Takashi Iwai4ac0eef2012-12-20 18:10:51 +0100240 if (!(spec->mixer_nid && with_aa_mix == HDA_PARSE_ONLY_AAMIX))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100241 goto found;
242 }
243 if (depth >= MAX_NID_PATH_DEPTH)
244 return false;
245 for (i = 0; i < nums; i++) {
246 unsigned int type;
247 type = get_wcaps_type(get_wcaps(codec, conn[i]));
248 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
249 type == AC_WID_PIN)
250 continue;
251 if (__parse_nid_path(codec, from_nid, conn[i],
252 with_aa_mix, path, depth + 1))
253 goto found;
254 }
255 return false;
256
257 found:
258 path->path[path->depth] = conn[i];
Takashi Iwaif5172a72013-01-04 13:19:55 +0100259 if (conn[i] == spec->mixer_nid)
260 path->with_aa_mix = true;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100261 path->idx[path->depth + 1] = i;
262 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
263 path->multi[path->depth + 1] = 1;
264 path->depth++;
265 return true;
266}
267
268/* parse the widget path from the given nid to the target nid;
269 * when @from_nid is 0, try to find an empty DAC;
Takashi Iwai4ac0eef2012-12-20 18:10:51 +0100270 * when @with_aa_mix is HDA_PARSE_NO_AAMIX, paths with spec->mixer_nid are
271 * excluded, only the paths that don't go through the mixer will be chosen.
272 * when @with_aa_mix is HDA_PARSE_ONLY_AAMIX, only the paths going through
273 * spec->mixer_nid will be chosen.
274 * when @with_aa_mix is HDA_PARSE_ALL, no special handling about mixer widget.
Takashi Iwai352f7f92012-12-19 12:52:06 +0100275 */
276bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
277 hda_nid_t to_nid, int with_aa_mix,
278 struct nid_path *path)
279{
280 if (__parse_nid_path(codec, from_nid, to_nid, with_aa_mix, path, 1)) {
281 path->path[path->depth] = to_nid;
282 path->depth++;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100283 return true;
284 }
285 return false;
286}
287EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100290 * parse the path between the given NIDs and add to the path list.
291 * if no valid path is found, return NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100293struct nid_path *
294snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
295 hda_nid_t to_nid, int with_aa_mix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100297 struct hda_gen_spec *spec = codec->spec;
298 struct nid_path *path;
299
300 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
301 return NULL;
302
Takashi Iwaif5172a72013-01-04 13:19:55 +0100303 /* check whether the path has been already added */
304 path = get_nid_path(codec, from_nid, to_nid, with_aa_mix);
305 if (path)
306 return path;
307
Takashi Iwai352f7f92012-12-19 12:52:06 +0100308 path = snd_array_new(&spec->paths);
309 if (!path)
310 return NULL;
311 memset(path, 0, sizeof(*path));
312 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, with_aa_mix, path))
313 return path;
314 /* push back */
315 spec->paths.used--;
316 return NULL;
317}
318EXPORT_SYMBOL_HDA(snd_hda_add_new_path);
319
320/* look for an empty DAC slot */
321static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
322 bool is_digital)
323{
324 struct hda_gen_spec *spec = codec->spec;
325 bool cap_digital;
326 int i;
327
328 for (i = 0; i < spec->num_all_dacs; i++) {
329 hda_nid_t nid = spec->all_dacs[i];
330 if (!nid || is_dac_already_used(codec, nid))
331 continue;
332 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
333 if (is_digital != cap_digital)
334 continue;
335 if (is_reachable_path(codec, nid, pin))
336 return nid;
337 }
338 return 0;
339}
340
341/* replace the channels in the composed amp value with the given number */
342static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
343{
344 val &= ~(0x3U << 16);
345 val |= chs << 16;
346 return val;
347}
348
349/* check whether the widget has the given amp capability for the direction */
350static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
351 int dir, unsigned int bits)
352{
353 if (!nid)
354 return false;
355 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
356 if (query_amp_caps(codec, nid, dir) & bits)
357 return true;
358 return false;
359}
360
361#define nid_has_mute(codec, nid, dir) \
362 check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
363#define nid_has_volume(codec, nid, dir) \
364 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
365
366/* look for a widget suitable for assigning a mute switch in the path */
367static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
368 struct nid_path *path)
369{
370 int i;
371
372 for (i = path->depth - 1; i >= 0; i--) {
373 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
374 return path->path[i];
375 if (i != path->depth - 1 && i != 0 &&
376 nid_has_mute(codec, path->path[i], HDA_INPUT))
377 return path->path[i];
378 }
379 return 0;
380}
381
382/* look for a widget suitable for assigning a volume ctl in the path */
383static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
384 struct nid_path *path)
385{
386 int i;
387
388 for (i = path->depth - 1; i >= 0; i--) {
389 if (nid_has_volume(codec, path->path[i], HDA_OUTPUT))
390 return path->path[i];
391 }
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200392 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393}
394
395/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100396 * path activation / deactivation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100398
399/* can have the amp-in capability? */
400static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100402 hda_nid_t nid = path->path[idx];
403 unsigned int caps = get_wcaps(codec, nid);
404 unsigned int type = get_wcaps_type(caps);
405
406 if (!(caps & AC_WCAP_IN_AMP))
407 return false;
408 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
409 return false;
410 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411}
412
Takashi Iwai352f7f92012-12-19 12:52:06 +0100413/* can have the amp-out capability? */
414static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100416 hda_nid_t nid = path->path[idx];
417 unsigned int caps = get_wcaps(codec, nid);
418 unsigned int type = get_wcaps_type(caps);
419
420 if (!(caps & AC_WCAP_OUT_AMP))
421 return false;
422 if (type == AC_WID_PIN && !idx) /* only for output pins */
423 return false;
424 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
426
Takashi Iwai352f7f92012-12-19 12:52:06 +0100427/* check whether the given (nid,dir,idx) is active */
428static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
429 unsigned int idx, unsigned int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100431 struct hda_gen_spec *spec = codec->spec;
432 int i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Takashi Iwai352f7f92012-12-19 12:52:06 +0100434 for (n = 0; n < spec->paths.used; n++) {
435 struct nid_path *path = snd_array_elem(&spec->paths, n);
436 if (!path->active)
437 continue;
438 for (i = 0; i < path->depth; i++) {
439 if (path->path[i] == nid) {
440 if (dir == HDA_OUTPUT || path->idx[i] == idx)
441 return true;
442 break;
443 }
444 }
445 }
446 return false;
447}
448
449/* get the default amp value for the target state */
450static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
451 int dir, bool enable)
452{
453 unsigned int caps;
454 unsigned int val = 0;
455
456 caps = query_amp_caps(codec, nid, dir);
457 if (caps & AC_AMPCAP_NUM_STEPS) {
458 /* set to 0dB */
459 if (enable)
460 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
461 }
462 if (caps & AC_AMPCAP_MUTE) {
463 if (!enable)
464 val |= HDA_AMP_MUTE;
465 }
466 return val;
467}
468
469/* initialize the amp value (only at the first time) */
470static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
471{
472 int val = get_amp_val_to_activate(codec, nid, dir, false);
473 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
474}
475
476static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
477 int idx, bool enable)
478{
479 int val;
480 if (is_ctl_associated(codec, nid, dir, idx) ||
Takashi Iwai985803c2013-01-03 16:30:04 +0100481 (!enable && is_active_nid(codec, nid, dir, idx)))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100482 return;
483 val = get_amp_val_to_activate(codec, nid, dir, enable);
484 snd_hda_codec_amp_stereo(codec, nid, dir, idx, 0xff, val);
485}
486
487static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
488 int i, bool enable)
489{
490 hda_nid_t nid = path->path[i];
491 init_amp(codec, nid, HDA_OUTPUT, 0);
492 activate_amp(codec, nid, HDA_OUTPUT, 0, enable);
493}
494
495static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
496 int i, bool enable, bool add_aamix)
497{
498 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100499 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100500 int n, nums, idx;
501 int type;
502 hda_nid_t nid = path->path[i];
503
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100504 nums = snd_hda_get_conn_list(codec, nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100505 type = get_wcaps_type(get_wcaps(codec, nid));
506 if (type == AC_WID_PIN ||
507 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
508 nums = 1;
509 idx = 0;
510 } else
511 idx = path->idx[i];
512
513 for (n = 0; n < nums; n++)
514 init_amp(codec, nid, HDA_INPUT, n);
515
516 if (is_ctl_associated(codec, nid, HDA_INPUT, idx))
517 return;
518
519 /* here is a little bit tricky in comparison with activate_amp_out();
520 * when aa-mixer is available, we need to enable the path as well
521 */
522 for (n = 0; n < nums; n++) {
523 if (n != idx && (!add_aamix || conn[n] != spec->mixer_nid))
524 continue;
525 activate_amp(codec, nid, HDA_INPUT, n, enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 }
527}
528
Takashi Iwai352f7f92012-12-19 12:52:06 +0100529/* activate or deactivate the given path
530 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100532void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
533 bool enable, bool add_aamix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100535 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Takashi Iwai352f7f92012-12-19 12:52:06 +0100537 if (!enable)
538 path->active = false;
539
540 for (i = path->depth - 1; i >= 0; i--) {
541 if (enable && path->multi[i])
542 snd_hda_codec_write_cache(codec, path->path[i], 0,
543 AC_VERB_SET_CONNECT_SEL,
544 path->idx[i]);
545 if (has_amp_in(codec, path, i))
546 activate_amp_in(codec, path, i, enable, add_aamix);
547 if (has_amp_out(codec, path, i))
548 activate_amp_out(codec, path, i, enable);
549 }
550
551 if (enable)
552 path->active = true;
553}
554EXPORT_SYMBOL_HDA(snd_hda_activate_path);
555
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100556/* turn on/off EAPD on the given pin */
557static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
558{
559 struct hda_gen_spec *spec = codec->spec;
560 if (spec->own_eapd_ctl ||
561 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
562 return;
Takashi Iwaiecac3ed2012-12-21 15:23:01 +0100563 if (codec->inv_eapd)
564 enable = !enable;
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100565 snd_hda_codec_update_cache(codec, pin, 0,
566 AC_VERB_SET_EAPD_BTLENABLE,
567 enable ? 0x02 : 0x00);
568}
569
Takashi Iwai352f7f92012-12-19 12:52:06 +0100570
571/*
572 * Helper functions for creating mixer ctl elements
573 */
574
575enum {
576 HDA_CTL_WIDGET_VOL,
577 HDA_CTL_WIDGET_MUTE,
578 HDA_CTL_BIND_MUTE,
579 HDA_CTL_BIND_VOL,
580 HDA_CTL_BIND_SW,
581};
582static const struct snd_kcontrol_new control_templates[] = {
583 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
584 HDA_CODEC_MUTE(NULL, 0, 0, 0),
585 HDA_BIND_MUTE(NULL, 0, 0, 0),
586 HDA_BIND_VOL(NULL, 0),
587 HDA_BIND_SW(NULL, 0),
588};
589
590/* add dynamic controls from template */
591static int add_control(struct hda_gen_spec *spec, int type, const char *name,
592 int cidx, unsigned long val)
593{
594 struct snd_kcontrol_new *knew;
595
Takashi Iwai12c93df2012-12-19 14:38:33 +0100596 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100597 if (!knew)
598 return -ENOMEM;
599 knew->index = cidx;
600 if (get_amp_nid_(val))
601 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
602 knew->private_value = val;
603 return 0;
604}
605
606static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
607 const char *pfx, const char *dir,
608 const char *sfx, int cidx, unsigned long val)
609{
610 char name[32];
611 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
612 return add_control(spec, type, name, cidx, val);
613}
614
615#define add_pb_vol_ctrl(spec, type, pfx, val) \
616 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
617#define add_pb_sw_ctrl(spec, type, pfx, val) \
618 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
619#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
620 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
621#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
622 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
623
624static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
625 unsigned int chs, struct nid_path *path)
626{
627 unsigned int val;
628 if (!path)
629 return 0;
630 val = path->ctls[NID_PATH_VOL_CTL];
631 if (!val)
632 return 0;
633 val = amp_val_replace_channels(val, chs);
634 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
635}
636
637/* return the channel bits suitable for the given path->ctls[] */
638static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
639 int type)
640{
641 int chs = 1; /* mono (left only) */
642 if (path) {
643 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
644 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
645 chs = 3; /* stereo */
646 }
647 return chs;
648}
649
650static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
651 struct nid_path *path)
652{
653 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
654 return add_vol_ctl(codec, pfx, cidx, chs, path);
655}
656
657/* create a mute-switch for the given mixer widget;
658 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
659 */
660static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
661 unsigned int chs, struct nid_path *path)
662{
663 unsigned int val;
664 int type = HDA_CTL_WIDGET_MUTE;
665
666 if (!path)
667 return 0;
668 val = path->ctls[NID_PATH_MUTE_CTL];
669 if (!val)
670 return 0;
671 val = amp_val_replace_channels(val, chs);
672 if (get_amp_direction_(val) == HDA_INPUT) {
673 hda_nid_t nid = get_amp_nid_(val);
674 int nums = snd_hda_get_num_conns(codec, nid);
675 if (nums > 1) {
676 type = HDA_CTL_BIND_MUTE;
677 val |= nums << 19;
678 }
679 }
680 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
681}
682
683static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
684 int cidx, struct nid_path *path)
685{
686 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
687 return add_sw_ctl(codec, pfx, cidx, chs, path);
688}
689
690static const char * const channel_name[4] = {
691 "Front", "Surround", "CLFE", "Side"
692};
693
694/* give some appropriate ctl name prefix for the given line out channel */
695static const char *get_line_out_pfx(struct hda_gen_spec *spec, int ch,
696 bool can_be_master, int *index)
697{
698 struct auto_pin_cfg *cfg = &spec->autocfg;
699
700 *index = 0;
701 if (cfg->line_outs == 1 && !spec->multi_ios &&
702 !cfg->hp_outs && !cfg->speaker_outs && can_be_master)
703 return spec->vmaster_mute.hook ? "PCM" : "Master";
704
705 /* if there is really a single DAC used in the whole output paths,
706 * use it master (or "PCM" if a vmaster hook is present)
707 */
708 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
709 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
710 return spec->vmaster_mute.hook ? "PCM" : "Master";
711
712 switch (cfg->line_out_type) {
713 case AUTO_PIN_SPEAKER_OUT:
714 if (cfg->line_outs == 1)
715 return "Speaker";
716 if (cfg->line_outs == 2)
717 return ch ? "Bass Speaker" : "Speaker";
718 break;
719 case AUTO_PIN_HP_OUT:
720 /* for multi-io case, only the primary out */
721 if (ch && spec->multi_ios)
722 break;
723 *index = ch;
724 return "Headphone";
725 default:
726 if (cfg->line_outs == 1 && !spec->multi_ios)
727 return "PCM";
728 break;
729 }
730 if (ch >= ARRAY_SIZE(channel_name)) {
731 snd_BUG();
732 return "PCM";
733 }
734
735 return channel_name[ch];
736}
737
738/*
739 * Parse output paths
740 */
741
742/* badness definition */
743enum {
744 /* No primary DAC is found for the main output */
745 BAD_NO_PRIMARY_DAC = 0x10000,
746 /* No DAC is found for the extra output */
747 BAD_NO_DAC = 0x4000,
748 /* No possible multi-ios */
749 BAD_MULTI_IO = 0x103,
750 /* No individual DAC for extra output */
751 BAD_NO_EXTRA_DAC = 0x102,
752 /* No individual DAC for extra surrounds */
753 BAD_NO_EXTRA_SURR_DAC = 0x101,
754 /* Primary DAC shared with main surrounds */
755 BAD_SHARED_SURROUND = 0x100,
756 /* Primary DAC shared with main CLFE */
757 BAD_SHARED_CLFE = 0x10,
758 /* Primary DAC shared with extra surrounds */
759 BAD_SHARED_EXTRA_SURROUND = 0x10,
760 /* Volume widget is shared */
761 BAD_SHARED_VOL = 0x10,
762};
763
764/* look for widgets in the path between the given NIDs appropriate for
765 * volume and mute controls, and assign the values to ctls[].
766 *
767 * When no appropriate widget is found in the path, the badness value
768 * is incremented depending on the situation. The function returns the
769 * total badness for both volume and mute controls.
770 */
771static int assign_out_path_ctls(struct hda_codec *codec, hda_nid_t pin,
772 hda_nid_t dac)
773{
774 struct nid_path *path = snd_hda_get_nid_path(codec, dac, pin);
775 hda_nid_t nid;
776 unsigned int val;
777 int badness = 0;
778
779 if (!path)
780 return BAD_SHARED_VOL * 2;
781 nid = look_for_out_vol_nid(codec, path);
782 if (nid) {
783 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
784 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
785 badness += BAD_SHARED_VOL;
786 else
787 path->ctls[NID_PATH_VOL_CTL] = val;
788 } else
789 badness += BAD_SHARED_VOL;
790 nid = look_for_out_mute_nid(codec, path);
791 if (nid) {
792 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
793 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
794 nid_has_mute(codec, nid, HDA_OUTPUT))
795 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
796 else
797 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
798 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
799 badness += BAD_SHARED_VOL;
800 else
801 path->ctls[NID_PATH_MUTE_CTL] = val;
802 } else
803 badness += BAD_SHARED_VOL;
804 return badness;
805}
806
807struct badness_table {
808 int no_primary_dac; /* no primary DAC */
809 int no_dac; /* no secondary DACs */
810 int shared_primary; /* primary DAC is shared with main output */
811 int shared_surr; /* secondary DAC shared with main or primary */
812 int shared_clfe; /* third DAC shared with main or primary */
813 int shared_surr_main; /* secondary DAC sahred with main/DAC0 */
814};
815
816static struct badness_table main_out_badness = {
817 .no_primary_dac = BAD_NO_PRIMARY_DAC,
818 .no_dac = BAD_NO_DAC,
819 .shared_primary = BAD_NO_PRIMARY_DAC,
820 .shared_surr = BAD_SHARED_SURROUND,
821 .shared_clfe = BAD_SHARED_CLFE,
822 .shared_surr_main = BAD_SHARED_SURROUND,
823};
824
825static struct badness_table extra_out_badness = {
826 .no_primary_dac = BAD_NO_DAC,
827 .no_dac = BAD_NO_DAC,
828 .shared_primary = BAD_NO_EXTRA_DAC,
829 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
830 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
831 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
832};
833
Takashi Iwai7385df62013-01-07 09:50:52 +0100834/* get the DAC of the primary output corresponding to the given array index */
835static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
836{
837 struct hda_gen_spec *spec = codec->spec;
838 struct auto_pin_cfg *cfg = &spec->autocfg;
839
840 if (cfg->line_outs > idx)
841 return spec->private_dac_nids[idx];
842 idx -= cfg->line_outs;
843 if (spec->multi_ios > idx)
844 return spec->multi_io[idx].dac;
845 return 0;
846}
847
848/* return the DAC if it's reachable, otherwise zero */
849static inline hda_nid_t try_dac(struct hda_codec *codec,
850 hda_nid_t dac, hda_nid_t pin)
851{
852 return is_reachable_path(codec, dac, pin) ? dac : 0;
853}
854
Takashi Iwai352f7f92012-12-19 12:52:06 +0100855/* try to assign DACs to pins and return the resultant badness */
856static int try_assign_dacs(struct hda_codec *codec, int num_outs,
857 const hda_nid_t *pins, hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +0100858 int *path_idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100859 const struct badness_table *bad)
860{
861 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100862 int i, j;
863 int badness = 0;
864 hda_nid_t dac;
865
866 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 return 0;
868
Takashi Iwai352f7f92012-12-19 12:52:06 +0100869 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100870 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100871 hda_nid_t pin = pins[i];
Takashi Iwai1e0b5282013-01-04 12:56:52 +0100872
873 if (dacs[i]) {
874 badness += assign_out_path_ctls(codec, pin, dacs[i]);
875 continue;
876 }
877
878 dacs[i] = look_for_dac(codec, pin, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100879 if (!dacs[i] && !i) {
880 for (j = 1; j < num_outs; j++) {
881 if (is_reachable_path(codec, dacs[j], pin)) {
882 dacs[0] = dacs[j];
883 dacs[j] = 0;
Takashi Iwai196c17662013-01-04 15:01:40 +0100884 path_idx[j] = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100885 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 }
887 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100888 }
889 dac = dacs[i];
890 if (!dac) {
Takashi Iwai7385df62013-01-07 09:50:52 +0100891 if (num_outs > 2)
892 dac = try_dac(codec, get_primary_out(codec, i), pin);
893 if (!dac)
894 dac = try_dac(codec, dacs[0], pin);
895 if (!dac)
896 dac = try_dac(codec, get_primary_out(codec, i), pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100897 if (dac) {
898 if (!i)
899 badness += bad->shared_primary;
900 else if (i == 1)
901 badness += bad->shared_surr;
902 else
903 badness += bad->shared_clfe;
904 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
905 dac = spec->private_dac_nids[0];
906 badness += bad->shared_surr_main;
907 } else if (!i)
908 badness += bad->no_primary_dac;
909 else
910 badness += bad->no_dac;
911 }
Takashi Iwai4ac0eef2012-12-20 18:10:51 +0100912 path = snd_hda_add_new_path(codec, dac, pin, HDA_PARSE_NO_AAMIX);
Takashi Iwai117688a2013-01-04 15:41:41 +0100913 if (!path && !i && spec->mixer_nid) {
Takashi Iwaib3a8c742012-12-20 18:29:16 +0100914 /* try with aamix */
915 path = snd_hda_add_new_path(codec, dac, pin, HDA_PARSE_ALL);
916 }
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100917 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100918 dac = dacs[i] = 0;
Takashi Iwaie1284af2013-01-03 16:33:02 +0100919 else {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100920 print_nid_path("output", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +0100921 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +0100922 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwaie1284af2013-01-03 16:33:02 +0100923 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100924 if (dac)
925 badness += assign_out_path_ctls(codec, pin, dac);
926 }
927
928 return badness;
929}
930
931/* return NID if the given pin has only a single connection to a certain DAC */
932static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
933{
934 struct hda_gen_spec *spec = codec->spec;
935 int i;
936 hda_nid_t nid_found = 0;
937
938 for (i = 0; i < spec->num_all_dacs; i++) {
939 hda_nid_t nid = spec->all_dacs[i];
940 if (!nid || is_dac_already_used(codec, nid))
941 continue;
942 if (is_reachable_path(codec, nid, pin)) {
943 if (nid_found)
944 return 0;
945 nid_found = nid;
946 }
947 }
948 return nid_found;
949}
950
951/* check whether the given pin can be a multi-io pin */
952static bool can_be_multiio_pin(struct hda_codec *codec,
953 unsigned int location, hda_nid_t nid)
954{
955 unsigned int defcfg, caps;
956
957 defcfg = snd_hda_codec_get_pincfg(codec, nid);
958 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
959 return false;
960 if (location && get_defcfg_location(defcfg) != location)
961 return false;
962 caps = snd_hda_query_pin_caps(codec, nid);
963 if (!(caps & AC_PINCAP_OUT))
964 return false;
965 return true;
966}
967
Takashi Iwaie22aab72013-01-04 14:50:04 +0100968/* count the number of input pins that are capable to be multi-io */
969static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
970{
971 struct hda_gen_spec *spec = codec->spec;
972 struct auto_pin_cfg *cfg = &spec->autocfg;
973 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
974 unsigned int location = get_defcfg_location(defcfg);
975 int type, i;
976 int num_pins = 0;
977
978 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
979 for (i = 0; i < cfg->num_inputs; i++) {
980 if (cfg->inputs[i].type != type)
981 continue;
982 if (can_be_multiio_pin(codec, location,
983 cfg->inputs[i].pin))
984 num_pins++;
985 }
986 }
987 return num_pins;
988}
989
Takashi Iwai352f7f92012-12-19 12:52:06 +0100990/*
991 * multi-io helper
992 *
993 * When hardwired is set, try to fill ony hardwired pins, and returns
994 * zero if any pins are filled, non-zero if nothing found.
995 * When hardwired is off, try to fill possible input pins, and returns
996 * the badness value.
997 */
998static int fill_multi_ios(struct hda_codec *codec,
999 hda_nid_t reference_pin,
Takashi Iwaie22aab72013-01-04 14:50:04 +01001000 bool hardwired)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001001{
1002 struct hda_gen_spec *spec = codec->spec;
1003 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie22aab72013-01-04 14:50:04 +01001004 int type, i, j, num_pins, old_pins;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001005 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1006 unsigned int location = get_defcfg_location(defcfg);
1007 int badness = 0;
1008
1009 old_pins = spec->multi_ios;
1010 if (old_pins >= 2)
1011 goto end_fill;
1012
Takashi Iwaie22aab72013-01-04 14:50:04 +01001013 num_pins = count_multiio_pins(codec, reference_pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001014 if (num_pins < 2)
1015 goto end_fill;
1016
Takashi Iwai352f7f92012-12-19 12:52:06 +01001017 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1018 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001019 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001020 hda_nid_t nid = cfg->inputs[i].pin;
1021 hda_nid_t dac = 0;
1022
1023 if (cfg->inputs[i].type != type)
1024 continue;
1025 if (!can_be_multiio_pin(codec, location, nid))
1026 continue;
1027 for (j = 0; j < spec->multi_ios; j++) {
1028 if (nid == spec->multi_io[j].pin)
1029 break;
1030 }
1031 if (j < spec->multi_ios)
1032 continue;
1033
Takashi Iwai352f7f92012-12-19 12:52:06 +01001034 if (hardwired)
1035 dac = get_dac_if_single(codec, nid);
1036 else if (!dac)
1037 dac = look_for_dac(codec, nid, false);
1038 if (!dac) {
1039 badness++;
1040 continue;
1041 }
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01001042 path = snd_hda_add_new_path(codec, dac, nid, HDA_PARSE_NO_AAMIX);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001043 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001044 badness++;
1045 continue;
1046 }
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001047 print_nid_path("multiio", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001048 spec->multi_io[spec->multi_ios].pin = nid;
1049 spec->multi_io[spec->multi_ios].dac = dac;
Takashi Iwai196c17662013-01-04 15:01:40 +01001050 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1051 snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001052 spec->multi_ios++;
1053 if (spec->multi_ios >= 2)
1054 break;
1055 }
1056 }
1057 end_fill:
1058 if (badness)
1059 badness = BAD_MULTI_IO;
1060 if (old_pins == spec->multi_ios) {
1061 if (hardwired)
1062 return 1; /* nothing found */
1063 else
1064 return badness; /* no badness if nothing found */
1065 }
1066 if (!hardwired && spec->multi_ios < 2) {
1067 /* cancel newly assigned paths */
1068 spec->paths.used -= spec->multi_ios - old_pins;
1069 spec->multi_ios = old_pins;
1070 return badness;
1071 }
1072
1073 /* assign volume and mute controls */
1074 for (i = old_pins; i < spec->multi_ios; i++)
1075 badness += assign_out_path_ctls(codec, spec->multi_io[i].pin,
1076 spec->multi_io[i].dac);
1077
1078 return badness;
1079}
1080
1081/* map DACs for all pins in the list if they are single connections */
1082static bool map_singles(struct hda_codec *codec, int outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001083 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001084{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001085 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001086 int i;
1087 bool found = false;
1088 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001089 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001090 hda_nid_t dac;
1091 if (dacs[i])
1092 continue;
1093 dac = get_dac_if_single(codec, pins[i]);
1094 if (!dac)
1095 continue;
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01001096 path = snd_hda_add_new_path(codec, dac, pins[i], HDA_PARSE_NO_AAMIX);
Takashi Iwai117688a2013-01-04 15:41:41 +01001097 if (!path && !i && spec->mixer_nid)
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001098 path = snd_hda_add_new_path(codec, dac, pins[i], HDA_PARSE_ALL);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001099 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001100 dacs[i] = dac;
1101 found = true;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001102 print_nid_path("output", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001103 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001104 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001105 }
1106 }
1107 return found;
1108}
1109
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001110/* create a new path including aamix if available, and return its index */
1111static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1112{
1113 struct nid_path *path;
1114
1115 path = snd_hda_get_path_from_idx(codec, path_idx);
1116 if (!path || !path->depth || path->with_aa_mix)
1117 return 0;
1118 path = snd_hda_add_new_path(codec, path->path[0],
1119 path->path[path->depth - 1],
1120 HDA_PARSE_ONLY_AAMIX);
1121 if (!path)
1122 return 0;
1123 print_nid_path("output-aamix", path);
1124 path->active = false; /* unused as default */
1125 return snd_hda_get_path_idx(codec, path);
1126}
1127
Takashi Iwai352f7f92012-12-19 12:52:06 +01001128/* fill in the dac_nids table from the parsed pin configuration */
1129static int fill_and_eval_dacs(struct hda_codec *codec,
1130 bool fill_hardwired,
1131 bool fill_mio_first)
1132{
1133 struct hda_gen_spec *spec = codec->spec;
1134 struct auto_pin_cfg *cfg = &spec->autocfg;
1135 int i, err, badness;
1136
1137 /* set num_dacs once to full for look_for_dac() */
1138 spec->multiout.num_dacs = cfg->line_outs;
1139 spec->multiout.dac_nids = spec->private_dac_nids;
1140 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1141 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1142 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1143 spec->multi_ios = 0;
1144 snd_array_free(&spec->paths);
1145 badness = 0;
1146
1147 /* fill hard-wired DACs first */
1148 if (fill_hardwired) {
1149 bool mapped;
1150 do {
1151 mapped = map_singles(codec, cfg->line_outs,
1152 cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001153 spec->private_dac_nids,
1154 spec->out_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001155 mapped |= map_singles(codec, cfg->hp_outs,
1156 cfg->hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001157 spec->multiout.hp_out_nid,
1158 spec->hp_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001159 mapped |= map_singles(codec, cfg->speaker_outs,
1160 cfg->speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001161 spec->multiout.extra_out_nid,
1162 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001163 if (fill_mio_first && cfg->line_outs == 1 &&
1164 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001165 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001166 if (!err)
1167 mapped = true;
1168 }
1169 } while (mapped);
1170 }
1171
1172 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001173 spec->private_dac_nids, spec->out_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001174 &main_out_badness);
1175
1176 /* re-count num_dacs and squash invalid entries */
1177 spec->multiout.num_dacs = 0;
1178 for (i = 0; i < cfg->line_outs; i++) {
1179 if (spec->private_dac_nids[i])
1180 spec->multiout.num_dacs++;
1181 else {
1182 memmove(spec->private_dac_nids + i,
1183 spec->private_dac_nids + i + 1,
1184 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1185 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1186 }
1187 }
1188
1189 if (fill_mio_first &&
1190 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1191 /* try to fill multi-io first */
Takashi Iwaie22aab72013-01-04 14:50:04 +01001192 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001193 if (err < 0)
1194 return err;
1195 /* we don't count badness at this stage yet */
1196 }
1197
1198 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1199 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1200 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001201 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001202 &extra_out_badness);
1203 if (err < 0)
1204 return err;
1205 badness += err;
1206 }
1207 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1208 err = try_assign_dacs(codec, cfg->speaker_outs,
1209 cfg->speaker_pins,
1210 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001211 spec->speaker_paths,
1212 &extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001213 if (err < 0)
1214 return err;
1215 badness += err;
1216 }
1217 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001218 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001219 if (err < 0)
1220 return err;
1221 badness += err;
1222 }
Takashi Iwaie22aab72013-01-04 14:50:04 +01001223
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001224 if (spec->mixer_nid) {
1225 spec->aamix_out_paths[0] =
1226 check_aamix_out_path(codec, spec->out_paths[0]);
1227 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1228 spec->aamix_out_paths[1] =
1229 check_aamix_out_path(codec, spec->hp_paths[0]);
1230 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1231 spec->aamix_out_paths[2] =
1232 check_aamix_out_path(codec, spec->speaker_paths[0]);
1233 }
1234
Takashi Iwaie22aab72013-01-04 14:50:04 +01001235 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1236 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1237 spec->multi_ios = 1; /* give badness */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001238
1239 if (spec->multi_ios == 2) {
1240 for (i = 0; i < 2; i++)
1241 spec->private_dac_nids[spec->multiout.num_dacs++] =
1242 spec->multi_io[i].dac;
1243 spec->ext_channel_count = 2;
1244 } else if (spec->multi_ios) {
1245 spec->multi_ios = 0;
1246 badness += BAD_MULTI_IO;
1247 }
1248
1249 return badness;
1250}
1251
1252#define DEBUG_BADNESS
1253
1254#ifdef DEBUG_BADNESS
1255#define debug_badness snd_printdd
1256#else
1257#define debug_badness(...)
1258#endif
1259
1260static void debug_show_configs(struct hda_gen_spec *spec, struct auto_pin_cfg *cfg)
1261{
1262 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1263 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001264 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001265 spec->multiout.dac_nids[0],
1266 spec->multiout.dac_nids[1],
1267 spec->multiout.dac_nids[2],
1268 spec->multiout.dac_nids[3]);
1269 if (spec->multi_ios > 0)
1270 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1271 spec->multi_ios,
1272 spec->multi_io[0].pin, spec->multi_io[1].pin,
1273 spec->multi_io[0].dac, spec->multi_io[1].dac);
1274 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1275 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001276 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001277 spec->multiout.hp_out_nid[0],
1278 spec->multiout.hp_out_nid[1],
1279 spec->multiout.hp_out_nid[2],
1280 spec->multiout.hp_out_nid[3]);
1281 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1282 cfg->speaker_pins[0], cfg->speaker_pins[1],
1283 cfg->speaker_pins[2], cfg->speaker_pins[3],
1284 spec->multiout.extra_out_nid[0],
1285 spec->multiout.extra_out_nid[1],
1286 spec->multiout.extra_out_nid[2],
1287 spec->multiout.extra_out_nid[3]);
1288}
1289
1290/* find all available DACs of the codec */
1291static void fill_all_dac_nids(struct hda_codec *codec)
1292{
1293 struct hda_gen_spec *spec = codec->spec;
1294 int i;
1295 hda_nid_t nid = codec->start_nid;
1296
1297 spec->num_all_dacs = 0;
1298 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1299 for (i = 0; i < codec->num_nodes; i++, nid++) {
1300 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1301 continue;
1302 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1303 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1304 break;
1305 }
1306 spec->all_dacs[spec->num_all_dacs++] = nid;
1307 }
1308}
1309
1310static int parse_output_paths(struct hda_codec *codec)
1311{
1312 struct hda_gen_spec *spec = codec->spec;
1313 struct auto_pin_cfg *cfg = &spec->autocfg;
1314 struct auto_pin_cfg *best_cfg;
1315 int best_badness = INT_MAX;
1316 int badness;
1317 bool fill_hardwired = true, fill_mio_first = true;
1318 bool best_wired = true, best_mio = true;
1319 bool hp_spk_swapped = false;
1320
1321 fill_all_dac_nids(codec);
1322
1323 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1324 if (!best_cfg)
1325 return -ENOMEM;
1326 *best_cfg = *cfg;
1327
1328 for (;;) {
1329 badness = fill_and_eval_dacs(codec, fill_hardwired,
1330 fill_mio_first);
1331 if (badness < 0) {
1332 kfree(best_cfg);
1333 return badness;
1334 }
1335 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1336 cfg->line_out_type, fill_hardwired, fill_mio_first,
1337 badness);
1338 debug_show_configs(spec, cfg);
1339 if (badness < best_badness) {
1340 best_badness = badness;
1341 *best_cfg = *cfg;
1342 best_wired = fill_hardwired;
1343 best_mio = fill_mio_first;
1344 }
1345 if (!badness)
1346 break;
1347 fill_mio_first = !fill_mio_first;
1348 if (!fill_mio_first)
1349 continue;
1350 fill_hardwired = !fill_hardwired;
1351 if (!fill_hardwired)
1352 continue;
1353 if (hp_spk_swapped)
1354 break;
1355 hp_spk_swapped = true;
1356 if (cfg->speaker_outs > 0 &&
1357 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1358 cfg->hp_outs = cfg->line_outs;
1359 memcpy(cfg->hp_pins, cfg->line_out_pins,
1360 sizeof(cfg->hp_pins));
1361 cfg->line_outs = cfg->speaker_outs;
1362 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1363 sizeof(cfg->speaker_pins));
1364 cfg->speaker_outs = 0;
1365 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1366 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1367 fill_hardwired = true;
1368 continue;
1369 }
1370 if (cfg->hp_outs > 0 &&
1371 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1372 cfg->speaker_outs = cfg->line_outs;
1373 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1374 sizeof(cfg->speaker_pins));
1375 cfg->line_outs = cfg->hp_outs;
1376 memcpy(cfg->line_out_pins, cfg->hp_pins,
1377 sizeof(cfg->hp_pins));
1378 cfg->hp_outs = 0;
1379 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1380 cfg->line_out_type = AUTO_PIN_HP_OUT;
1381 fill_hardwired = true;
1382 continue;
1383 }
1384 break;
1385 }
1386
1387 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001388 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001389 *cfg = *best_cfg;
1390 fill_and_eval_dacs(codec, best_wired, best_mio);
1391 }
1392 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1393 cfg->line_out_type, best_wired, best_mio);
1394 debug_show_configs(spec, cfg);
1395
1396 if (cfg->line_out_pins[0]) {
1397 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01001398 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001399 if (path)
1400 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
1401 }
1402
1403 kfree(best_cfg);
1404 return 0;
1405}
1406
1407/* add playback controls from the parsed DAC table */
1408static int create_multi_out_ctls(struct hda_codec *codec,
1409 const struct auto_pin_cfg *cfg)
1410{
1411 struct hda_gen_spec *spec = codec->spec;
1412 int i, err, noutputs;
1413
1414 noutputs = cfg->line_outs;
1415 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1416 noutputs += spec->multi_ios;
1417
1418 for (i = 0; i < noutputs; i++) {
1419 const char *name;
1420 int index;
Takashi Iwai196c17662013-01-04 15:01:40 +01001421 hda_nid_t dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001422 struct nid_path *path;
1423
1424 dac = spec->multiout.dac_nids[i];
1425 if (!dac)
1426 continue;
1427 if (i >= cfg->line_outs) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001428 index = 0;
1429 name = channel_name[i];
1430 } else {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001431 name = get_line_out_pfx(spec, i, true, &index);
1432 }
1433
Takashi Iwai196c17662013-01-04 15:01:40 +01001434 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001435 if (!path)
1436 continue;
1437 if (!name || !strcmp(name, "CLFE")) {
1438 /* Center/LFE */
1439 err = add_vol_ctl(codec, "Center", 0, 1, path);
1440 if (err < 0)
1441 return err;
1442 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1443 if (err < 0)
1444 return err;
1445 err = add_sw_ctl(codec, "Center", 0, 1, path);
1446 if (err < 0)
1447 return err;
1448 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1449 if (err < 0)
1450 return err;
1451 } else {
1452 err = add_stereo_vol(codec, name, index, path);
1453 if (err < 0)
1454 return err;
1455 err = add_stereo_sw(codec, name, index, path);
1456 if (err < 0)
1457 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 }
1459 }
1460 return 0;
1461}
1462
Takashi Iwaic2c80382013-01-07 10:33:57 +01001463static int create_extra_out(struct hda_codec *codec, int path_idx,
Takashi Iwai196c17662013-01-04 15:01:40 +01001464 const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001466 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 int err;
1468
Takashi Iwai196c17662013-01-04 15:01:40 +01001469 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001470 if (!path)
1471 return 0;
Takashi Iwaic2c80382013-01-07 10:33:57 +01001472 err = add_stereo_vol(codec, pfx, cidx, path);
1473 if (err < 0)
1474 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001475 err = add_stereo_sw(codec, pfx, cidx, path);
1476 if (err < 0)
1477 return err;
1478 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479}
1480
Takashi Iwai352f7f92012-12-19 12:52:06 +01001481/* add playback controls for speaker and HP outputs */
1482static int create_extra_outs(struct hda_codec *codec, int num_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001483 const int *paths, const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484{
Takashi Iwaic2c80382013-01-07 10:33:57 +01001485 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001486
1487 for (i = 0; i < num_pins; i++) {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001488 const char *name;
1489 char tmp[44];
1490 int err, idx = 0;
1491
1492 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
1493 name = "Bass Speaker";
1494 else if (num_pins >= 3) {
1495 snprintf(tmp, sizeof(tmp), "%s %s",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001496 pfx, channel_name[i]);
Takashi Iwaic2c80382013-01-07 10:33:57 +01001497 name = tmp;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001498 } else {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001499 name = pfx;
1500 idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 }
Takashi Iwaic2c80382013-01-07 10:33:57 +01001502 err = create_extra_out(codec, paths[i], name, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001503 if (err < 0)
1504 return err;
1505 }
1506 return 0;
1507}
Takashi Iwai97ec5582006-03-21 11:29:07 +01001508
Takashi Iwai352f7f92012-12-19 12:52:06 +01001509static int create_hp_out_ctls(struct hda_codec *codec)
1510{
1511 struct hda_gen_spec *spec = codec->spec;
1512 return create_extra_outs(codec, spec->autocfg.hp_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001513 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001514 "Headphone");
1515}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
Takashi Iwai352f7f92012-12-19 12:52:06 +01001517static int create_speaker_out_ctls(struct hda_codec *codec)
1518{
1519 struct hda_gen_spec *spec = codec->spec;
1520 return create_extra_outs(codec, spec->autocfg.speaker_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001521 spec->speaker_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001522 "Speaker");
1523}
1524
1525/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001526 * independent HP controls
1527 */
1528
1529static int indep_hp_info(struct snd_kcontrol *kcontrol,
1530 struct snd_ctl_elem_info *uinfo)
1531{
1532 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
1533}
1534
1535static int indep_hp_get(struct snd_kcontrol *kcontrol,
1536 struct snd_ctl_elem_value *ucontrol)
1537{
1538 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1539 struct hda_gen_spec *spec = codec->spec;
1540 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
1541 return 0;
1542}
1543
1544static int indep_hp_put(struct snd_kcontrol *kcontrol,
1545 struct snd_ctl_elem_value *ucontrol)
1546{
1547 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1548 struct hda_gen_spec *spec = codec->spec;
1549 unsigned int select = ucontrol->value.enumerated.item[0];
1550 int ret = 0;
1551
1552 mutex_lock(&spec->pcm_mutex);
1553 if (spec->active_streams) {
1554 ret = -EBUSY;
1555 goto unlock;
1556 }
1557
1558 if (spec->indep_hp_enabled != select) {
1559 spec->indep_hp_enabled = select;
1560 if (spec->indep_hp_enabled)
1561 spec->multiout.hp_out_nid[0] = 0;
1562 else
1563 spec->multiout.hp_out_nid[0] = spec->alt_dac_nid;
1564 ret = 1;
1565 }
1566 unlock:
1567 mutex_unlock(&spec->pcm_mutex);
1568 return ret;
1569}
1570
1571static const struct snd_kcontrol_new indep_hp_ctl = {
1572 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1573 .name = "Independent HP",
1574 .info = indep_hp_info,
1575 .get = indep_hp_get,
1576 .put = indep_hp_put,
1577};
1578
1579
1580static int create_indep_hp_ctls(struct hda_codec *codec)
1581{
1582 struct hda_gen_spec *spec = codec->spec;
1583
1584 if (!spec->indep_hp)
1585 return 0;
1586 if (!spec->multiout.hp_out_nid[0]) {
1587 spec->indep_hp = 0;
1588 return 0;
1589 }
1590
1591 spec->indep_hp_enabled = false;
1592 spec->alt_dac_nid = spec->multiout.hp_out_nid[0];
1593 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
1594 return -ENOMEM;
1595 return 0;
1596}
1597
1598/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01001599 * channel mode enum control
1600 */
1601
1602static int ch_mode_info(struct snd_kcontrol *kcontrol,
1603 struct snd_ctl_elem_info *uinfo)
1604{
1605 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1606 struct hda_gen_spec *spec = codec->spec;
1607
1608 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1609 uinfo->count = 1;
1610 uinfo->value.enumerated.items = spec->multi_ios + 1;
1611 if (uinfo->value.enumerated.item > spec->multi_ios)
1612 uinfo->value.enumerated.item = spec->multi_ios;
1613 sprintf(uinfo->value.enumerated.name, "%dch",
1614 (uinfo->value.enumerated.item + 1) * 2);
1615 return 0;
1616}
1617
1618static int ch_mode_get(struct snd_kcontrol *kcontrol,
1619 struct snd_ctl_elem_value *ucontrol)
1620{
1621 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1622 struct hda_gen_spec *spec = codec->spec;
1623 ucontrol->value.enumerated.item[0] = (spec->ext_channel_count - 1) / 2;
1624 return 0;
1625}
1626
Takashi Iwai196c17662013-01-04 15:01:40 +01001627static inline struct nid_path *
1628get_multiio_path(struct hda_codec *codec, int idx)
1629{
1630 struct hda_gen_spec *spec = codec->spec;
1631 return snd_hda_get_path_from_idx(codec,
1632 spec->out_paths[spec->autocfg.line_outs + idx]);
1633}
1634
Takashi Iwai352f7f92012-12-19 12:52:06 +01001635static int set_multi_io(struct hda_codec *codec, int idx, bool output)
1636{
1637 struct hda_gen_spec *spec = codec->spec;
1638 hda_nid_t nid = spec->multi_io[idx].pin;
1639 struct nid_path *path;
1640
Takashi Iwai196c17662013-01-04 15:01:40 +01001641 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001642 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001644
1645 if (path->active == output)
1646 return 0;
1647
1648 if (output) {
1649 snd_hda_set_pin_ctl_cache(codec, nid, PIN_OUT);
1650 snd_hda_activate_path(codec, path, true, true);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01001651 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001652 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01001653 set_pin_eapd(codec, nid, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001654 snd_hda_activate_path(codec, path, false, true);
1655 snd_hda_set_pin_ctl_cache(codec, nid,
1656 spec->multi_io[idx].ctl_in);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001658 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659}
1660
Takashi Iwai352f7f92012-12-19 12:52:06 +01001661static int ch_mode_put(struct snd_kcontrol *kcontrol,
1662 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001664 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1665 struct hda_gen_spec *spec = codec->spec;
1666 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
Takashi Iwai352f7f92012-12-19 12:52:06 +01001668 ch = ucontrol->value.enumerated.item[0];
1669 if (ch < 0 || ch > spec->multi_ios)
1670 return -EINVAL;
1671 if (ch == (spec->ext_channel_count - 1) / 2)
1672 return 0;
1673 spec->ext_channel_count = (ch + 1) * 2;
1674 for (i = 0; i < spec->multi_ios; i++)
1675 set_multi_io(codec, i, i < ch);
1676 spec->multiout.max_channels = max(spec->ext_channel_count,
1677 spec->const_channel_count);
1678 if (spec->need_dac_fix)
1679 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 return 1;
1681}
1682
Takashi Iwai352f7f92012-12-19 12:52:06 +01001683static const struct snd_kcontrol_new channel_mode_enum = {
1684 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1685 .name = "Channel Mode",
1686 .info = ch_mode_info,
1687 .get = ch_mode_get,
1688 .put = ch_mode_put,
1689};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
Takashi Iwai352f7f92012-12-19 12:52:06 +01001691static int create_multi_channel_mode(struct hda_codec *codec)
1692{
1693 struct hda_gen_spec *spec = codec->spec;
1694
1695 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01001696 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01001697 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 return 0;
1700}
1701
Takashi Iwai352f7f92012-12-19 12:52:06 +01001702/*
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001703 * aamix loopback enable/disable switch
1704 */
1705
1706#define loopback_mixing_info indep_hp_info
1707
1708static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
1709 struct snd_ctl_elem_value *ucontrol)
1710{
1711 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1712 struct hda_gen_spec *spec = codec->spec;
1713 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
1714 return 0;
1715}
1716
1717static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
1718 int nomix_path_idx, int mix_path_idx)
1719{
1720 struct nid_path *nomix_path, *mix_path;
1721
1722 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
1723 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
1724 if (!nomix_path || !mix_path)
1725 return;
1726 if (do_mix) {
1727 snd_hda_activate_path(codec, nomix_path, false, true);
1728 snd_hda_activate_path(codec, mix_path, true, true);
1729 } else {
1730 snd_hda_activate_path(codec, mix_path, false, true);
1731 snd_hda_activate_path(codec, nomix_path, true, true);
1732 }
1733}
1734
1735static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
1736 struct snd_ctl_elem_value *ucontrol)
1737{
1738 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1739 struct hda_gen_spec *spec = codec->spec;
1740 unsigned int val = ucontrol->value.enumerated.item[0];
1741
1742 if (val == spec->aamix_mode)
1743 return 0;
1744 spec->aamix_mode = val;
1745 update_aamix_paths(codec, val, spec->out_paths[0],
1746 spec->aamix_out_paths[0]);
1747 update_aamix_paths(codec, val, spec->hp_paths[0],
1748 spec->aamix_out_paths[1]);
1749 update_aamix_paths(codec, val, spec->speaker_paths[0],
1750 spec->aamix_out_paths[2]);
1751 return 1;
1752}
1753
1754static const struct snd_kcontrol_new loopback_mixing_enum = {
1755 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1756 .name = "Loopback Mixing",
1757 .info = loopback_mixing_info,
1758 .get = loopback_mixing_get,
1759 .put = loopback_mixing_put,
1760};
1761
1762static int create_loopback_mixing_ctl(struct hda_codec *codec)
1763{
1764 struct hda_gen_spec *spec = codec->spec;
1765
1766 if (!spec->mixer_nid)
1767 return 0;
1768 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
1769 spec->aamix_out_paths[2]))
1770 return 0;
1771 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
1772 return -ENOMEM;
1773 return 0;
1774}
1775
1776/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01001777 * shared headphone/mic handling
1778 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02001779
Takashi Iwai352f7f92012-12-19 12:52:06 +01001780static void call_update_outputs(struct hda_codec *codec);
1781
1782/* for shared I/O, change the pin-control accordingly */
1783static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic)
1784{
1785 struct hda_gen_spec *spec = codec->spec;
1786 unsigned int val;
1787 hda_nid_t pin = spec->autocfg.inputs[1].pin;
1788 /* NOTE: this assumes that there are only two inputs, the
1789 * first is the real internal mic and the second is HP/mic jack.
1790 */
1791
1792 val = snd_hda_get_default_vref(codec, pin);
1793
1794 /* This pin does not have vref caps - let's enable vref on pin 0x18
1795 instead, as suggested by Realtek */
1796 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
1797 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
1798 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
1799 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01001800 snd_hda_set_pin_ctl_cache(codec, vref_pin,
1801 PIN_IN | (set_as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02001802 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001803
1804 val = set_as_mic ? val | PIN_IN : PIN_HP;
Takashi Iwai7594aa32012-12-20 15:38:40 +01001805 snd_hda_set_pin_ctl_cache(codec, pin, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001806
1807 spec->automute_speaker = !set_as_mic;
1808 call_update_outputs(codec);
1809}
1810
1811/* create a shared input with the headphone out */
1812static int create_shared_input(struct hda_codec *codec)
1813{
1814 struct hda_gen_spec *spec = codec->spec;
1815 struct auto_pin_cfg *cfg = &spec->autocfg;
1816 unsigned int defcfg;
1817 hda_nid_t nid;
1818
1819 /* only one internal input pin? */
1820 if (cfg->num_inputs != 1)
1821 return 0;
1822 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
1823 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
1824 return 0;
1825
1826 if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1827 nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
1828 else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
1829 nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
1830 else
1831 return 0; /* both not available */
1832
1833 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
1834 return 0; /* no input */
1835
1836 cfg->inputs[1].pin = nid;
1837 cfg->inputs[1].type = AUTO_PIN_MIC;
1838 cfg->num_inputs = 2;
1839 spec->shared_mic_hp = 1;
1840 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
1841 return 0;
1842}
1843
1844
1845/*
1846 * Parse input paths
1847 */
1848
1849#ifdef CONFIG_PM
1850/* add the powersave loopback-list entry */
1851static void add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
1852{
1853 struct hda_amp_list *list;
1854
1855 if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
1856 return;
1857 list = spec->loopback_list + spec->num_loopbacks;
1858 list->nid = mix;
1859 list->dir = HDA_INPUT;
1860 list->idx = idx;
1861 spec->num_loopbacks++;
Takashi Iwaicb53c622007-08-10 17:21:45 +02001862 spec->loopback.amplist = spec->loopback_list;
1863}
1864#else
Takashi Iwai352f7f92012-12-19 12:52:06 +01001865#define add_loopback_list(spec, mix, idx) /* NOP */
Takashi Iwaicb53c622007-08-10 17:21:45 +02001866#endif
1867
Takashi Iwai352f7f92012-12-19 12:52:06 +01001868/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01001869static int new_analog_input(struct hda_codec *codec, int input_idx,
1870 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001871 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001873 struct hda_gen_spec *spec = codec->spec;
1874 struct nid_path *path;
1875 unsigned int val;
1876 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
Takashi Iwai352f7f92012-12-19 12:52:06 +01001878 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
1879 !nid_has_mute(codec, mix_nid, HDA_INPUT))
1880 return 0; /* no need for analog loopback */
1881
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01001882 path = snd_hda_add_new_path(codec, pin, mix_nid, HDA_PARSE_ALL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001883 if (!path)
1884 return -EINVAL;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001885 print_nid_path("loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01001886 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001887
1888 idx = path->idx[path->depth - 1];
1889 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
1890 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1891 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001892 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001894 path->ctls[NID_PATH_VOL_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 }
1896
Takashi Iwai352f7f92012-12-19 12:52:06 +01001897 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
1898 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1899 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001900 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001902 path->ctls[NID_PATH_MUTE_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 }
1904
Takashi Iwai352f7f92012-12-19 12:52:06 +01001905 path->active = true;
1906 add_loopback_list(spec, mix_nid, idx);
1907 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908}
1909
Takashi Iwai352f7f92012-12-19 12:52:06 +01001910static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001912 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
1913 return (pincap & AC_PINCAP_IN) != 0;
1914}
1915
1916/* Parse the codec tree and retrieve ADCs */
1917static int fill_adc_nids(struct hda_codec *codec)
1918{
1919 struct hda_gen_spec *spec = codec->spec;
1920 hda_nid_t nid;
1921 hda_nid_t *adc_nids = spec->adc_nids;
1922 int max_nums = ARRAY_SIZE(spec->adc_nids);
1923 int i, nums = 0;
1924
1925 nid = codec->start_nid;
1926 for (i = 0; i < codec->num_nodes; i++, nid++) {
1927 unsigned int caps = get_wcaps(codec, nid);
1928 int type = get_wcaps_type(caps);
1929
1930 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
1931 continue;
1932 adc_nids[nums] = nid;
1933 if (++nums >= max_nums)
1934 break;
1935 }
1936 spec->num_adc_nids = nums;
1937 return nums;
1938}
1939
1940/* filter out invalid adc_nids that don't give all active input pins;
1941 * if needed, check whether dynamic ADC-switching is available
1942 */
1943static int check_dyn_adc_switch(struct hda_codec *codec)
1944{
1945 struct hda_gen_spec *spec = codec->spec;
1946 struct hda_input_mux *imux = &spec->input_mux;
1947 hda_nid_t adc_nids[ARRAY_SIZE(spec->adc_nids)];
1948 int i, n, nums;
1949 hda_nid_t pin, adc;
1950
1951 again:
1952 nums = 0;
1953 for (n = 0; n < spec->num_adc_nids; n++) {
1954 adc = spec->adc_nids[n];
1955 for (i = 0; i < imux->num_items; i++) {
1956 pin = spec->imux_pins[i];
1957 if (!is_reachable_path(codec, pin, adc))
1958 break;
1959 }
1960 if (i >= imux->num_items)
1961 adc_nids[nums++] = adc;
1962 }
1963
1964 if (!nums) {
1965 if (spec->shared_mic_hp) {
1966 spec->shared_mic_hp = 0;
1967 imux->num_items = 1;
1968 goto again;
1969 }
1970
1971 /* check whether ADC-switch is possible */
1972 for (i = 0; i < imux->num_items; i++) {
1973 pin = spec->imux_pins[i];
1974 for (n = 0; n < spec->num_adc_nids; n++) {
1975 adc = spec->adc_nids[n];
1976 if (is_reachable_path(codec, pin, adc)) {
1977 spec->dyn_adc_idx[i] = n;
1978 break;
1979 }
1980 }
1981 }
1982
1983 snd_printdd("hda-codec: enabling ADC switching\n");
1984 spec->dyn_adc_switch = 1;
1985 } else if (nums != spec->num_adc_nids) {
1986 memcpy(spec->adc_nids, adc_nids, nums * sizeof(hda_nid_t));
1987 spec->num_adc_nids = nums;
1988 }
1989
1990 if (imux->num_items == 1 || spec->shared_mic_hp) {
1991 snd_printdd("hda-codec: reducing to a single ADC\n");
1992 spec->num_adc_nids = 1; /* reduce to a single ADC */
1993 }
1994
1995 /* single index for individual volumes ctls */
1996 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
1997 spec->num_adc_nids = 1;
1998
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 return 0;
2000}
2001
2002/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002003 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002005static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002006{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002007 struct hda_gen_spec *spec = codec->spec;
2008 const struct auto_pin_cfg *cfg = &spec->autocfg;
2009 hda_nid_t mixer = spec->mixer_nid;
2010 struct hda_input_mux *imux = &spec->input_mux;
2011 int num_adcs;
2012 int i, c, err, type_idx = 0;
2013 const char *prev_label = NULL;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002014
Takashi Iwai352f7f92012-12-19 12:52:06 +01002015 num_adcs = fill_adc_nids(codec);
2016 if (num_adcs < 0)
2017 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002018
Takashi Iwai352f7f92012-12-19 12:52:06 +01002019 for (i = 0; i < cfg->num_inputs; i++) {
2020 hda_nid_t pin;
2021 const char *label;
2022 bool imux_added;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
Takashi Iwai352f7f92012-12-19 12:52:06 +01002024 pin = cfg->inputs[i].pin;
2025 if (!is_input_pin(codec, pin))
2026 continue;
2027
2028 label = hda_get_autocfg_input_label(codec, cfg, i);
2029 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
2030 label = "Headphone Mic";
2031 if (prev_label && !strcmp(label, prev_label))
2032 type_idx++;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002033 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01002034 type_idx = 0;
2035 prev_label = label;
2036
2037 if (mixer) {
2038 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01002039 err = new_analog_input(codec, i, pin,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002040 label, type_idx, mixer);
2041 if (err < 0)
2042 return err;
2043 }
2044 }
2045
2046 imux_added = false;
2047 for (c = 0; c < num_adcs; c++) {
2048 struct nid_path *path;
2049 hda_nid_t adc = spec->adc_nids[c];
2050
2051 if (!is_reachable_path(codec, pin, adc))
2052 continue;
2053 path = snd_array_new(&spec->paths);
2054 if (!path)
2055 return -ENOMEM;
2056 memset(path, 0, sizeof(*path));
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01002057 if (!snd_hda_parse_nid_path(codec, pin, adc, HDA_PARSE_ALL, path)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002058 snd_printd(KERN_ERR
2059 "invalid input path 0x%x -> 0x%x\n",
2060 pin, adc);
2061 spec->paths.used--;
2062 continue;
2063 }
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002064 print_nid_path("input", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002065
2066 if (!imux_added) {
2067 spec->imux_pins[imux->num_items] = pin;
2068 snd_hda_add_imux_item(imux, label,
2069 imux->num_items, NULL);
2070 imux_added = true;
2071 }
2072 }
2073 }
2074
2075 return 0;
2076}
2077
2078
2079/*
2080 * input source mux
2081 */
2082
2083/* get the ADC NID corresponding to the given index */
2084static hda_nid_t get_adc_nid(struct hda_codec *codec, int adc_idx, int imux_idx)
2085{
2086 struct hda_gen_spec *spec = codec->spec;
2087 if (spec->dyn_adc_switch)
2088 adc_idx = spec->dyn_adc_idx[imux_idx];
2089 return spec->adc_nids[adc_idx];
2090}
2091
2092static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2093 unsigned int idx);
2094
2095static int mux_enum_info(struct snd_kcontrol *kcontrol,
2096 struct snd_ctl_elem_info *uinfo)
2097{
2098 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2099 struct hda_gen_spec *spec = codec->spec;
2100 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
2101}
2102
2103static int mux_enum_get(struct snd_kcontrol *kcontrol,
2104 struct snd_ctl_elem_value *ucontrol)
2105{
2106 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2107 struct hda_gen_spec *spec = codec->spec;
2108 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2109
2110 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
2111 return 0;
2112}
2113
2114static int mux_enum_put(struct snd_kcontrol *kcontrol,
2115 struct snd_ctl_elem_value *ucontrol)
2116{
2117 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2118 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2119 return mux_select(codec, adc_idx,
2120 ucontrol->value.enumerated.item[0]);
2121}
2122
Takashi Iwai352f7f92012-12-19 12:52:06 +01002123static const struct snd_kcontrol_new cap_src_temp = {
2124 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2125 .name = "Input Source",
2126 .info = mux_enum_info,
2127 .get = mux_enum_get,
2128 .put = mux_enum_put,
2129};
2130
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002131/*
2132 * capture volume and capture switch ctls
2133 */
2134
Takashi Iwai352f7f92012-12-19 12:52:06 +01002135typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
2136 struct snd_ctl_elem_value *ucontrol);
2137
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002138/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002139static int cap_put_caller(struct snd_kcontrol *kcontrol,
2140 struct snd_ctl_elem_value *ucontrol,
2141 put_call_t func, int type)
2142{
2143 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2144 struct hda_gen_spec *spec = codec->spec;
2145 const struct hda_input_mux *imux;
2146 struct nid_path *path;
2147 int i, adc_idx, err = 0;
2148
2149 imux = &spec->input_mux;
2150 adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2151 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002152 /* we use the cache-only update at first since multiple input paths
2153 * may shared the same amp; by updating only caches, the redundant
2154 * writes to hardware can be reduced.
2155 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002156 codec->cached_write = 1;
2157 for (i = 0; i < imux->num_items; i++) {
2158 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2159 get_adc_nid(codec, adc_idx, i));
2160 if (!path->ctls[type])
2161 continue;
2162 kcontrol->private_value = path->ctls[type];
2163 err = func(kcontrol, ucontrol);
2164 if (err < 0)
2165 goto error;
2166 }
2167 error:
2168 codec->cached_write = 0;
2169 mutex_unlock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002170 snd_hda_codec_flush_amp_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002171 if (err >= 0 && spec->cap_sync_hook)
2172 spec->cap_sync_hook(codec);
2173 return err;
2174}
2175
2176/* capture volume ctl callbacks */
2177#define cap_vol_info snd_hda_mixer_amp_volume_info
2178#define cap_vol_get snd_hda_mixer_amp_volume_get
2179#define cap_vol_tlv snd_hda_mixer_amp_tlv
2180
2181static int cap_vol_put(struct snd_kcontrol *kcontrol,
2182 struct snd_ctl_elem_value *ucontrol)
2183{
2184 return cap_put_caller(kcontrol, ucontrol,
2185 snd_hda_mixer_amp_volume_put,
2186 NID_PATH_VOL_CTL);
2187}
2188
2189static const struct snd_kcontrol_new cap_vol_temp = {
2190 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2191 .name = "Capture Volume",
2192 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
2193 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2194 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
2195 .info = cap_vol_info,
2196 .get = cap_vol_get,
2197 .put = cap_vol_put,
2198 .tlv = { .c = cap_vol_tlv },
2199};
2200
2201/* capture switch ctl callbacks */
2202#define cap_sw_info snd_ctl_boolean_stereo_info
2203#define cap_sw_get snd_hda_mixer_amp_switch_get
2204
2205static int cap_sw_put(struct snd_kcontrol *kcontrol,
2206 struct snd_ctl_elem_value *ucontrol)
2207{
2208 return cap_put_caller(kcontrol, ucontrol,
2209 snd_hda_mixer_amp_switch_put,
2210 NID_PATH_MUTE_CTL);
2211}
2212
2213static const struct snd_kcontrol_new cap_sw_temp = {
2214 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2215 .name = "Capture Switch",
2216 .info = cap_sw_info,
2217 .get = cap_sw_get,
2218 .put = cap_sw_put,
2219};
2220
2221static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
2222{
2223 hda_nid_t nid;
2224 int i, depth;
2225
2226 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
2227 for (depth = 0; depth < 3; depth++) {
2228 if (depth >= path->depth)
2229 return -EINVAL;
2230 i = path->depth - depth - 1;
2231 nid = path->path[i];
2232 if (!path->ctls[NID_PATH_VOL_CTL]) {
2233 if (nid_has_volume(codec, nid, HDA_OUTPUT))
2234 path->ctls[NID_PATH_VOL_CTL] =
2235 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2236 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
2237 int idx = path->idx[i];
2238 if (!depth && codec->single_adc_amp)
2239 idx = 0;
2240 path->ctls[NID_PATH_VOL_CTL] =
2241 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2242 }
2243 }
2244 if (!path->ctls[NID_PATH_MUTE_CTL]) {
2245 if (nid_has_mute(codec, nid, HDA_OUTPUT))
2246 path->ctls[NID_PATH_MUTE_CTL] =
2247 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2248 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
2249 int idx = path->idx[i];
2250 if (!depth && codec->single_adc_amp)
2251 idx = 0;
2252 path->ctls[NID_PATH_MUTE_CTL] =
2253 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2254 }
2255 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01002256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 return 0;
2258}
2259
Takashi Iwai352f7f92012-12-19 12:52:06 +01002260static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002262 struct hda_gen_spec *spec = codec->spec;
2263 struct auto_pin_cfg *cfg = &spec->autocfg;
2264 unsigned int val;
2265 int i;
2266
2267 if (!spec->inv_dmic_split)
2268 return false;
2269 for (i = 0; i < cfg->num_inputs; i++) {
2270 if (cfg->inputs[i].pin != nid)
2271 continue;
2272 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2273 return false;
2274 val = snd_hda_codec_get_pincfg(codec, nid);
2275 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
2276 }
2277 return false;
2278}
2279
2280static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
2281 int idx, bool is_switch, unsigned int ctl,
2282 bool inv_dmic)
2283{
2284 struct hda_gen_spec *spec = codec->spec;
2285 char tmpname[44];
2286 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
2287 const char *sfx = is_switch ? "Switch" : "Volume";
2288 unsigned int chs = inv_dmic ? 1 : 3;
2289 int err;
2290
2291 if (!ctl)
2292 return 0;
2293
2294 if (label)
2295 snprintf(tmpname, sizeof(tmpname),
2296 "%s Capture %s", label, sfx);
2297 else
2298 snprintf(tmpname, sizeof(tmpname),
2299 "Capture %s", sfx);
2300 err = add_control(spec, type, tmpname, idx,
2301 amp_val_replace_channels(ctl, chs));
2302 if (err < 0 || !inv_dmic)
2303 return err;
2304
2305 /* Make independent right kcontrol */
2306 if (label)
2307 snprintf(tmpname, sizeof(tmpname),
2308 "Inverted %s Capture %s", label, sfx);
2309 else
2310 snprintf(tmpname, sizeof(tmpname),
2311 "Inverted Capture %s", sfx);
2312 return add_control(spec, type, tmpname, idx,
2313 amp_val_replace_channels(ctl, 2));
2314}
2315
2316/* create single (and simple) capture volume and switch controls */
2317static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
2318 unsigned int vol_ctl, unsigned int sw_ctl,
2319 bool inv_dmic)
2320{
2321 int err;
2322 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
2323 if (err < 0)
2324 return err;
2325 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
2326 if (err < 0)
2327 return err;
2328 return 0;
2329}
2330
2331/* create bound capture volume and switch controls */
2332static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
2333 unsigned int vol_ctl, unsigned int sw_ctl)
2334{
2335 struct hda_gen_spec *spec = codec->spec;
2336 struct snd_kcontrol_new *knew;
2337
2338 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002339 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002340 if (!knew)
2341 return -ENOMEM;
2342 knew->index = idx;
2343 knew->private_value = vol_ctl;
2344 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2345 }
2346 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002347 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002348 if (!knew)
2349 return -ENOMEM;
2350 knew->index = idx;
2351 knew->private_value = sw_ctl;
2352 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2353 }
2354 return 0;
2355}
2356
2357/* return the vol ctl when used first in the imux list */
2358static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
2359{
2360 struct hda_gen_spec *spec = codec->spec;
2361 struct nid_path *path;
2362 unsigned int ctl;
2363 int i;
2364
2365 path = snd_hda_get_nid_path(codec, spec->imux_pins[idx],
2366 get_adc_nid(codec, 0, idx));
2367 if (!path)
2368 return 0;
2369 ctl = path->ctls[type];
2370 if (!ctl)
2371 return 0;
2372 for (i = 0; i < idx - 1; i++) {
2373 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2374 get_adc_nid(codec, 0, i));
2375 if (path && path->ctls[type] == ctl)
2376 return 0;
2377 }
2378 return ctl;
2379}
2380
2381/* create individual capture volume and switch controls per input */
2382static int create_multi_cap_vol_ctl(struct hda_codec *codec)
2383{
2384 struct hda_gen_spec *spec = codec->spec;
2385 struct hda_input_mux *imux = &spec->input_mux;
2386 int i, err, type, type_idx = 0;
2387 const char *prev_label = NULL;
2388
2389 for (i = 0; i < imux->num_items; i++) {
2390 const char *label;
2391 bool inv_dmic;
2392 label = hda_get_autocfg_input_label(codec, &spec->autocfg, i);
2393 if (prev_label && !strcmp(label, prev_label))
2394 type_idx++;
2395 else
2396 type_idx = 0;
2397 prev_label = label;
2398 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
2399
2400 for (type = 0; type < 2; type++) {
2401 err = add_single_cap_ctl(codec, label, type_idx, type,
2402 get_first_cap_ctl(codec, i, type),
2403 inv_dmic);
2404 if (err < 0)
2405 return err;
2406 }
2407 }
2408 return 0;
2409}
2410
2411static int create_capture_mixers(struct hda_codec *codec)
2412{
2413 struct hda_gen_spec *spec = codec->spec;
2414 struct hda_input_mux *imux = &spec->input_mux;
2415 int i, n, nums, err;
2416
2417 if (spec->dyn_adc_switch)
2418 nums = 1;
2419 else
2420 nums = spec->num_adc_nids;
2421
2422 if (!spec->auto_mic && imux->num_items > 1) {
2423 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01002424 const char *name;
2425 name = nums > 1 ? "Input Source" : "Capture Source";
2426 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002427 if (!knew)
2428 return -ENOMEM;
2429 knew->count = nums;
2430 }
2431
2432 for (n = 0; n < nums; n++) {
2433 bool multi = false;
2434 bool inv_dmic = false;
2435 int vol, sw;
2436
2437 vol = sw = 0;
2438 for (i = 0; i < imux->num_items; i++) {
2439 struct nid_path *path;
2440 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2441 get_adc_nid(codec, n, i));
2442 if (!path)
2443 continue;
2444 parse_capvol_in_path(codec, path);
2445 if (!vol)
2446 vol = path->ctls[NID_PATH_VOL_CTL];
2447 else if (vol != path->ctls[NID_PATH_VOL_CTL])
2448 multi = true;
2449 if (!sw)
2450 sw = path->ctls[NID_PATH_MUTE_CTL];
2451 else if (sw != path->ctls[NID_PATH_MUTE_CTL])
2452 multi = true;
2453 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
2454 inv_dmic = true;
2455 }
2456
2457 if (!multi)
2458 err = create_single_cap_vol_ctl(codec, n, vol, sw,
2459 inv_dmic);
2460 else if (!spec->multi_cap_vol)
2461 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
2462 else
2463 err = create_multi_cap_vol_ctl(codec);
2464 if (err < 0)
2465 return err;
2466 }
2467
2468 return 0;
2469}
2470
2471/*
2472 * add mic boosts if needed
2473 */
2474static int parse_mic_boost(struct hda_codec *codec)
2475{
2476 struct hda_gen_spec *spec = codec->spec;
2477 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002478 int i, err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002479 int type_idx = 0;
2480 hda_nid_t nid;
2481 const char *prev_label = NULL;
2482
2483 for (i = 0; i < cfg->num_inputs; i++) {
2484 if (cfg->inputs[i].type > AUTO_PIN_MIC)
2485 break;
2486 nid = cfg->inputs[i].pin;
2487 if (get_wcaps(codec, nid) & AC_WCAP_IN_AMP) {
2488 const char *label;
Takashi Iwai5abd4882013-01-07 09:43:18 +01002489 char boost_label[44];
Takashi Iwai352f7f92012-12-19 12:52:06 +01002490 struct nid_path *path;
2491 unsigned int val;
2492
2493 label = hda_get_autocfg_input_label(codec, cfg, i);
2494 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
2495 label = "Headphone Mic";
2496 if (prev_label && !strcmp(label, prev_label))
2497 type_idx++;
2498 else
2499 type_idx = 0;
2500 prev_label = label;
2501
2502 snprintf(boost_label, sizeof(boost_label),
2503 "%s Boost Volume", label);
2504 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
2505 err = add_control(spec, HDA_CTL_WIDGET_VOL,
2506 boost_label, type_idx, val);
2507 if (err < 0)
2508 return err;
2509
2510 path = snd_hda_get_nid_path(codec, nid, 0);
2511 if (path)
2512 path->ctls[NID_PATH_BOOST_CTL] = val;
2513 }
2514 }
2515 return 0;
2516}
2517
2518/*
2519 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
2520 */
2521static void parse_digital(struct hda_codec *codec)
2522{
2523 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002524 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002525 int i, nums;
2526 hda_nid_t dig_nid;
2527
2528 /* support multiple SPDIFs; the secondary is set up as a slave */
2529 nums = 0;
2530 for (i = 0; i < spec->autocfg.dig_outs; i++) {
2531 hda_nid_t pin = spec->autocfg.dig_out_pins[i];
2532 dig_nid = look_for_dac(codec, pin, true);
2533 if (!dig_nid)
2534 continue;
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01002535 path = snd_hda_add_new_path(codec, dig_nid, pin, HDA_PARSE_ALL);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002536 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002537 continue;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002538 print_nid_path("digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01002539 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01002540 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002541 if (!nums) {
2542 spec->multiout.dig_out_nid = dig_nid;
2543 spec->dig_out_type = spec->autocfg.dig_out_type[0];
2544 } else {
2545 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
2546 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
2547 break;
2548 spec->slave_dig_outs[nums - 1] = dig_nid;
2549 }
2550 nums++;
2551 }
2552
2553 if (spec->autocfg.dig_in_pin) {
2554 dig_nid = codec->start_nid;
2555 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002556 unsigned int wcaps = get_wcaps(codec, dig_nid);
2557 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
2558 continue;
2559 if (!(wcaps & AC_WCAP_DIGITAL))
2560 continue;
2561 path = snd_hda_add_new_path(codec,
2562 spec->autocfg.dig_in_pin,
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01002563 dig_nid, HDA_PARSE_ALL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002564 if (path) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002565 print_nid_path("digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002566 path->active = true;
2567 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01002568 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002569 break;
2570 }
2571 }
2572 }
2573}
2574
2575
2576/*
2577 * input MUX handling
2578 */
2579
2580static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
2581
2582/* select the given imux item; either unmute exclusively or select the route */
2583static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2584 unsigned int idx)
2585{
2586 struct hda_gen_spec *spec = codec->spec;
2587 const struct hda_input_mux *imux;
2588 struct nid_path *path;
2589
2590 imux = &spec->input_mux;
2591 if (!imux->num_items)
2592 return 0;
2593
2594 if (idx >= imux->num_items)
2595 idx = imux->num_items - 1;
2596 if (spec->cur_mux[adc_idx] == idx)
2597 return 0;
2598
2599 path = snd_hda_get_nid_path(codec,
2600 spec->imux_pins[spec->cur_mux[adc_idx]],
2601 spec->adc_nids[adc_idx]);
2602 if (!path)
2603 return 0;
2604 if (path->active)
2605 snd_hda_activate_path(codec, path, false, false);
2606
2607 spec->cur_mux[adc_idx] = idx;
2608
2609 if (spec->shared_mic_hp)
2610 update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
2611
2612 if (spec->dyn_adc_switch)
2613 dyn_adc_pcm_resetup(codec, idx);
2614
2615 path = snd_hda_get_nid_path(codec, spec->imux_pins[idx],
2616 get_adc_nid(codec, adc_idx, idx));
2617 if (!path)
2618 return 0;
2619 if (path->active)
2620 return 0;
2621 snd_hda_activate_path(codec, path, true, false);
2622 if (spec->cap_sync_hook)
2623 spec->cap_sync_hook(codec);
2624 return 1;
2625}
2626
2627
2628/*
2629 * Jack detections for HP auto-mute and mic-switch
2630 */
2631
2632/* check each pin in the given array; returns true if any of them is plugged */
2633static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
2634{
2635 int i, present = 0;
2636
2637 for (i = 0; i < num_pins; i++) {
2638 hda_nid_t nid = pins[i];
2639 if (!nid)
2640 break;
2641 present |= snd_hda_jack_detect(codec, nid);
2642 }
2643 return present;
2644}
2645
2646/* standard HP/line-out auto-mute helper */
2647static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
2648 bool mute, bool hp_out)
2649{
2650 struct hda_gen_spec *spec = codec->spec;
2651 unsigned int pin_bits = mute ? 0 : (hp_out ? PIN_HP : PIN_OUT);
2652 int i;
2653
2654 for (i = 0; i < num_pins; i++) {
2655 hda_nid_t nid = pins[i];
2656 unsigned int val;
2657 if (!nid)
2658 break;
2659 /* don't reset VREF value in case it's controlling
2660 * the amp (see alc861_fixup_asus_amp_vref_0f())
2661 */
2662 if (spec->keep_vref_in_automute) {
2663 val = snd_hda_codec_read(codec, nid, 0,
2664 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2665 val &= ~PIN_HP;
2666 } else
2667 val = 0;
2668 val |= pin_bits;
Takashi Iwai7594aa32012-12-20 15:38:40 +01002669 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002670 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002671 }
2672}
2673
2674/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002675void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002676{
2677 struct hda_gen_spec *spec = codec->spec;
2678 int on;
2679
2680 /* Control HP pins/amps depending on master_mute state;
2681 * in general, HP pins/amps control should be enabled in all cases,
2682 * but currently set only for master_mute, just to be safe
2683 */
2684 if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
2685 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2686 spec->autocfg.hp_pins, spec->master_mute, true);
2687
2688 if (!spec->automute_speaker)
2689 on = 0;
2690 else
2691 on = spec->hp_jack_present | spec->line_jack_present;
2692 on |= spec->master_mute;
2693 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
2694 spec->autocfg.speaker_pins, on, false);
2695
2696 /* toggle line-out mutes if needed, too */
2697 /* if LO is a copy of either HP or Speaker, don't need to handle it */
2698 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
2699 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
2700 return;
2701 if (!spec->automute_lo)
2702 on = 0;
2703 else
2704 on = spec->hp_jack_present;
2705 on |= spec->master_mute;
2706 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2707 spec->autocfg.line_out_pins, on, false);
2708}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002709EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002710
2711static void call_update_outputs(struct hda_codec *codec)
2712{
2713 struct hda_gen_spec *spec = codec->spec;
2714 if (spec->automute_hook)
2715 spec->automute_hook(codec);
2716 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01002717 snd_hda_gen_update_outputs(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002718}
2719
2720/* standard HP-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002721void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002722{
2723 struct hda_gen_spec *spec = codec->spec;
2724
2725 spec->hp_jack_present =
2726 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2727 spec->autocfg.hp_pins);
2728 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
2729 return;
2730 call_update_outputs(codec);
2731}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002732EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002733
2734/* standard line-out-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002735void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002736{
2737 struct hda_gen_spec *spec = codec->spec;
2738
2739 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
2740 return;
2741 /* check LO jack only when it's different from HP */
2742 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
2743 return;
2744
2745 spec->line_jack_present =
2746 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2747 spec->autocfg.line_out_pins);
2748 if (!spec->automute_speaker || !spec->detect_lo)
2749 return;
2750 call_update_outputs(codec);
2751}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002752EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002753
2754/* standard mic auto-switch helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002755void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002756{
2757 struct hda_gen_spec *spec = codec->spec;
2758 int i;
2759
2760 if (!spec->auto_mic)
2761 return;
2762
2763 for (i = spec->am_num_entries - 1; i > 0; i--) {
2764 if (snd_hda_jack_detect(codec, spec->am_entry[i].pin)) {
2765 mux_select(codec, 0, spec->am_entry[i].idx);
2766 return;
2767 }
2768 }
2769 mux_select(codec, 0, spec->am_entry[0].idx);
2770}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002771EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002772
2773/*
2774 * Auto-Mute mode mixer enum support
2775 */
2776static int automute_mode_info(struct snd_kcontrol *kcontrol,
2777 struct snd_ctl_elem_info *uinfo)
2778{
2779 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2780 struct hda_gen_spec *spec = codec->spec;
2781 static const char * const texts3[] = {
2782 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02002783 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784
Takashi Iwai352f7f92012-12-19 12:52:06 +01002785 if (spec->automute_speaker_possible && spec->automute_lo_possible)
2786 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
2787 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2788}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789
Takashi Iwai352f7f92012-12-19 12:52:06 +01002790static int automute_mode_get(struct snd_kcontrol *kcontrol,
2791 struct snd_ctl_elem_value *ucontrol)
2792{
2793 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2794 struct hda_gen_spec *spec = codec->spec;
2795 unsigned int val = 0;
2796 if (spec->automute_speaker)
2797 val++;
2798 if (spec->automute_lo)
2799 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002800
Takashi Iwai352f7f92012-12-19 12:52:06 +01002801 ucontrol->value.enumerated.item[0] = val;
2802 return 0;
2803}
2804
2805static int automute_mode_put(struct snd_kcontrol *kcontrol,
2806 struct snd_ctl_elem_value *ucontrol)
2807{
2808 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2809 struct hda_gen_spec *spec = codec->spec;
2810
2811 switch (ucontrol->value.enumerated.item[0]) {
2812 case 0:
2813 if (!spec->automute_speaker && !spec->automute_lo)
2814 return 0;
2815 spec->automute_speaker = 0;
2816 spec->automute_lo = 0;
2817 break;
2818 case 1:
2819 if (spec->automute_speaker_possible) {
2820 if (!spec->automute_lo && spec->automute_speaker)
2821 return 0;
2822 spec->automute_speaker = 1;
2823 spec->automute_lo = 0;
2824 } else if (spec->automute_lo_possible) {
2825 if (spec->automute_lo)
2826 return 0;
2827 spec->automute_lo = 1;
2828 } else
2829 return -EINVAL;
2830 break;
2831 case 2:
2832 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
2833 return -EINVAL;
2834 if (spec->automute_speaker && spec->automute_lo)
2835 return 0;
2836 spec->automute_speaker = 1;
2837 spec->automute_lo = 1;
2838 break;
2839 default:
2840 return -EINVAL;
2841 }
2842 call_update_outputs(codec);
2843 return 1;
2844}
2845
2846static const struct snd_kcontrol_new automute_mode_enum = {
2847 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2848 .name = "Auto-Mute Mode",
2849 .info = automute_mode_info,
2850 .get = automute_mode_get,
2851 .put = automute_mode_put,
2852};
2853
2854static int add_automute_mode_enum(struct hda_codec *codec)
2855{
2856 struct hda_gen_spec *spec = codec->spec;
2857
Takashi Iwai12c93df2012-12-19 14:38:33 +01002858 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002859 return -ENOMEM;
2860 return 0;
2861}
2862
2863/*
2864 * Check the availability of HP/line-out auto-mute;
2865 * Set up appropriately if really supported
2866 */
2867static int check_auto_mute_availability(struct hda_codec *codec)
2868{
2869 struct hda_gen_spec *spec = codec->spec;
2870 struct auto_pin_cfg *cfg = &spec->autocfg;
2871 int present = 0;
2872 int i, err;
2873
2874 if (cfg->hp_pins[0])
2875 present++;
2876 if (cfg->line_out_pins[0])
2877 present++;
2878 if (cfg->speaker_pins[0])
2879 present++;
2880 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02002881 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002882
2883 if (!cfg->speaker_pins[0] &&
2884 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
2885 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2886 sizeof(cfg->speaker_pins));
2887 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889
Takashi Iwai352f7f92012-12-19 12:52:06 +01002890 if (!cfg->hp_pins[0] &&
2891 cfg->line_out_type == AUTO_PIN_HP_OUT) {
2892 memcpy(cfg->hp_pins, cfg->line_out_pins,
2893 sizeof(cfg->hp_pins));
2894 cfg->hp_outs = cfg->line_outs;
2895 }
2896
2897 for (i = 0; i < cfg->hp_outs; i++) {
2898 hda_nid_t nid = cfg->hp_pins[i];
2899 if (!is_jack_detectable(codec, nid))
2900 continue;
2901 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
2902 nid);
2903 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01002904 spec->hp_automute_hook ?
2905 spec->hp_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01002906 snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002907 spec->detect_hp = 1;
2908 }
2909
2910 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
2911 if (cfg->speaker_outs)
2912 for (i = 0; i < cfg->line_outs; i++) {
2913 hda_nid_t nid = cfg->line_out_pins[i];
2914 if (!is_jack_detectable(codec, nid))
2915 continue;
2916 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
2917 snd_hda_jack_detect_enable_callback(codec, nid,
2918 HDA_GEN_FRONT_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01002919 spec->line_automute_hook ?
2920 spec->line_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01002921 snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002922 spec->detect_lo = 1;
2923 }
2924 spec->automute_lo_possible = spec->detect_hp;
2925 }
2926
2927 spec->automute_speaker_possible = cfg->speaker_outs &&
2928 (spec->detect_hp || spec->detect_lo);
2929
2930 spec->automute_lo = spec->automute_lo_possible;
2931 spec->automute_speaker = spec->automute_speaker_possible;
2932
2933 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
2934 /* create a control for automute mode */
2935 err = add_automute_mode_enum(codec);
2936 if (err < 0)
2937 return err;
2938 }
2939 return 0;
2940}
2941
2942/* return the position of NID in the list, or -1 if not found */
2943static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
2944{
2945 int i;
2946 for (i = 0; i < nums; i++)
2947 if (list[i] == nid)
2948 return i;
2949 return -1;
2950}
2951
2952/* check whether all auto-mic pins are valid; setup indices if OK */
2953static bool auto_mic_check_imux(struct hda_codec *codec)
2954{
2955 struct hda_gen_spec *spec = codec->spec;
2956 const struct hda_input_mux *imux;
2957 int i;
2958
2959 imux = &spec->input_mux;
2960 for (i = 0; i < spec->am_num_entries; i++) {
2961 spec->am_entry[i].idx =
2962 find_idx_in_nid_list(spec->am_entry[i].pin,
2963 spec->imux_pins, imux->num_items);
2964 if (spec->am_entry[i].idx < 0)
2965 return false; /* no corresponding imux */
2966 }
2967
2968 /* we don't need the jack detection for the first pin */
2969 for (i = 1; i < spec->am_num_entries; i++)
2970 snd_hda_jack_detect_enable_callback(codec,
2971 spec->am_entry[i].pin,
2972 HDA_GEN_MIC_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01002973 spec->mic_autoswitch_hook ?
2974 spec->mic_autoswitch_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01002975 snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002976 return true;
2977}
2978
2979static int compare_attr(const void *ap, const void *bp)
2980{
2981 const struct automic_entry *a = ap;
2982 const struct automic_entry *b = bp;
2983 return (int)(a->attr - b->attr);
2984}
2985
2986/*
2987 * Check the availability of auto-mic switch;
2988 * Set up if really supported
2989 */
2990static int check_auto_mic_availability(struct hda_codec *codec)
2991{
2992 struct hda_gen_spec *spec = codec->spec;
2993 struct auto_pin_cfg *cfg = &spec->autocfg;
2994 unsigned int types;
2995 int i, num_pins;
2996
2997 types = 0;
2998 num_pins = 0;
2999 for (i = 0; i < cfg->num_inputs; i++) {
3000 hda_nid_t nid = cfg->inputs[i].pin;
3001 unsigned int attr;
3002 attr = snd_hda_codec_get_pincfg(codec, nid);
3003 attr = snd_hda_get_input_pin_attr(attr);
3004 if (types & (1 << attr))
3005 return 0; /* already occupied */
3006 switch (attr) {
3007 case INPUT_PIN_ATTR_INT:
3008 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3009 return 0; /* invalid type */
3010 break;
3011 case INPUT_PIN_ATTR_UNUSED:
3012 return 0; /* invalid entry */
3013 default:
3014 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
3015 return 0; /* invalid type */
3016 if (!spec->line_in_auto_switch &&
3017 cfg->inputs[i].type != AUTO_PIN_MIC)
3018 return 0; /* only mic is allowed */
3019 if (!is_jack_detectable(codec, nid))
3020 return 0; /* no unsol support */
3021 break;
3022 }
3023 if (num_pins >= MAX_AUTO_MIC_PINS)
3024 return 0;
3025 types |= (1 << attr);
3026 spec->am_entry[num_pins].pin = nid;
3027 spec->am_entry[num_pins].attr = attr;
3028 num_pins++;
3029 }
3030
3031 if (num_pins < 2)
3032 return 0;
3033
3034 spec->am_num_entries = num_pins;
3035 /* sort the am_entry in the order of attr so that the pin with a
3036 * higher attr will be selected when the jack is plugged.
3037 */
3038 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
3039 compare_attr, NULL);
3040
3041 if (!auto_mic_check_imux(codec))
3042 return 0;
3043
3044 spec->auto_mic = 1;
3045 spec->num_adc_nids = 1;
3046 spec->cur_mux[0] = spec->am_entry[0].idx;
3047 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
3048 spec->am_entry[0].pin,
3049 spec->am_entry[1].pin,
3050 spec->am_entry[2].pin);
3051
3052 return 0;
3053}
3054
3055
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003056/*
3057 * Parse the given BIOS configuration and set up the hda_gen_spec
3058 *
3059 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003060 * or a negative error code
3061 */
3062int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003063 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003064{
3065 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003066 int err;
3067
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003068 if (cfg != &spec->autocfg) {
3069 spec->autocfg = *cfg;
3070 cfg = &spec->autocfg;
3071 }
3072
Takashi Iwai352f7f92012-12-19 12:52:06 +01003073 if (!cfg->line_outs) {
3074 if (cfg->dig_outs || cfg->dig_in_pin) {
3075 spec->multiout.max_channels = 2;
3076 spec->no_analog = 1;
3077 goto dig_only;
3078 }
3079 return 0; /* can't find valid BIOS pin config */
3080 }
3081
3082 if (!spec->no_primary_hp &&
3083 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
3084 cfg->line_outs <= cfg->hp_outs) {
3085 /* use HP as primary out */
3086 cfg->speaker_outs = cfg->line_outs;
3087 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3088 sizeof(cfg->speaker_pins));
3089 cfg->line_outs = cfg->hp_outs;
3090 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
3091 cfg->hp_outs = 0;
3092 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3093 cfg->line_out_type = AUTO_PIN_HP_OUT;
3094 }
3095
3096 err = parse_output_paths(codec);
3097 if (err < 0)
3098 return err;
3099 err = create_multi_channel_mode(codec);
3100 if (err < 0)
3101 return err;
3102 err = create_multi_out_ctls(codec, cfg);
3103 if (err < 0)
3104 return err;
3105 err = create_hp_out_ctls(codec);
3106 if (err < 0)
3107 return err;
3108 err = create_speaker_out_ctls(codec);
3109 if (err < 0)
3110 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003111 err = create_indep_hp_ctls(codec);
3112 if (err < 0)
3113 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01003114 err = create_loopback_mixing_ctl(codec);
3115 if (err < 0)
3116 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003117 err = create_shared_input(codec);
3118 if (err < 0)
3119 return err;
3120 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003121 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02003122 return err;
3123
Takashi Iwai352f7f92012-12-19 12:52:06 +01003124 /* check the multiple speaker pins */
3125 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
3126 spec->const_channel_count = cfg->line_outs * 2;
3127 else
3128 spec->const_channel_count = cfg->speaker_outs * 2;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003129
Takashi Iwai352f7f92012-12-19 12:52:06 +01003130 if (spec->multi_ios > 0)
3131 spec->multiout.max_channels = max(spec->ext_channel_count,
3132 spec->const_channel_count);
3133 else
3134 spec->multiout.max_channels = spec->multiout.num_dacs * 2;
3135
3136 err = check_auto_mute_availability(codec);
3137 if (err < 0)
3138 return err;
3139
3140 err = check_dyn_adc_switch(codec);
3141 if (err < 0)
3142 return err;
3143
3144 if (!spec->shared_mic_hp) {
3145 err = check_auto_mic_availability(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003146 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 }
Takashi Iwai071c73a2006-08-23 18:34:06 +02003149
Takashi Iwai352f7f92012-12-19 12:52:06 +01003150 err = create_capture_mixers(codec);
3151 if (err < 0)
3152 return err;
3153
3154 err = parse_mic_boost(codec);
3155 if (err < 0)
3156 return err;
3157
3158 dig_only:
3159 parse_digital(codec);
3160
3161 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003163EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164
3165
3166/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003167 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003169
3170/* slave controls for virtual master */
3171static const char * const slave_pfxs[] = {
3172 "Front", "Surround", "Center", "LFE", "Side",
3173 "Headphone", "Speaker", "Mono", "Line Out",
3174 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01003175 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
3176 "Headphone Front", "Headphone Surround", "Headphone CLFE",
3177 "Headphone Side",
Takashi Iwai352f7f92012-12-19 12:52:06 +01003178 NULL,
3179};
3180
3181int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003183 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185
Takashi Iwai36502d02012-12-19 15:15:10 +01003186 if (spec->kctls.used) {
3187 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
3188 if (err < 0)
3189 return err;
3190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191
Takashi Iwai352f7f92012-12-19 12:52:06 +01003192 if (spec->multiout.dig_out_nid) {
3193 err = snd_hda_create_dig_out_ctls(codec,
3194 spec->multiout.dig_out_nid,
3195 spec->multiout.dig_out_nid,
3196 spec->pcm_rec[1].pcm_type);
3197 if (err < 0)
3198 return err;
3199 if (!spec->no_analog) {
3200 err = snd_hda_create_spdif_share_sw(codec,
3201 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 if (err < 0)
3203 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003204 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 }
3206 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003207 if (spec->dig_in_nid) {
3208 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
3209 if (err < 0)
3210 return err;
3211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212
Takashi Iwai352f7f92012-12-19 12:52:06 +01003213 /* if we have no master control, let's create it */
3214 if (!spec->no_analog &&
3215 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
3216 unsigned int vmaster_tlv[4];
3217 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
3218 HDA_OUTPUT, vmaster_tlv);
3219 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
3220 vmaster_tlv, slave_pfxs,
3221 "Playback Volume");
3222 if (err < 0)
3223 return err;
3224 }
3225 if (!spec->no_analog &&
3226 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
3227 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
3228 NULL, slave_pfxs,
3229 "Playback Switch",
3230 true, &spec->vmaster_mute.sw_kctl);
3231 if (err < 0)
3232 return err;
3233 if (spec->vmaster_mute.hook)
Takashi Iwaifd25a972012-12-20 14:57:18 +01003234 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
3235 spec->vmaster_mute_enum);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237
Takashi Iwai352f7f92012-12-19 12:52:06 +01003238 free_kctls(spec); /* no longer needed */
3239
3240 if (spec->shared_mic_hp) {
3241 int err;
3242 int nid = spec->autocfg.inputs[1].pin;
3243 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
3244 if (err < 0)
3245 return err;
3246 err = snd_hda_jack_detect_enable(codec, nid, 0);
3247 if (err < 0)
3248 return err;
3249 }
3250
3251 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
3252 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 return err;
3254
3255 return 0;
3256}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003257EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
3258
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259
3260/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003261 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263
Takashi Iwai352f7f92012-12-19 12:52:06 +01003264/*
3265 * Analog playback callbacks
3266 */
3267static int playback_pcm_open(struct hda_pcm_stream *hinfo,
3268 struct hda_codec *codec,
3269 struct snd_pcm_substream *substream)
3270{
3271 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003272 int err;
3273
3274 mutex_lock(&spec->pcm_mutex);
3275 err = snd_hda_multi_out_analog_open(codec,
3276 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003277 hinfo);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003278 if (!err)
3279 spec->active_streams |= 1 << STREAM_MULTI_OUT;
3280 mutex_unlock(&spec->pcm_mutex);
3281 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003282}
3283
3284static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01003285 struct hda_codec *codec,
3286 unsigned int stream_tag,
3287 unsigned int format,
3288 struct snd_pcm_substream *substream)
3289{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003290 struct hda_gen_spec *spec = codec->spec;
3291 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
3292 stream_tag, format, substream);
3293}
Takashi Iwai97ec5582006-03-21 11:29:07 +01003294
Takashi Iwai352f7f92012-12-19 12:52:06 +01003295static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3296 struct hda_codec *codec,
3297 struct snd_pcm_substream *substream)
3298{
3299 struct hda_gen_spec *spec = codec->spec;
3300 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
3301}
3302
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003303static int playback_pcm_close(struct hda_pcm_stream *hinfo,
3304 struct hda_codec *codec,
3305 struct snd_pcm_substream *substream)
3306{
3307 struct hda_gen_spec *spec = codec->spec;
3308 mutex_lock(&spec->pcm_mutex);
3309 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
3310 mutex_unlock(&spec->pcm_mutex);
3311 return 0;
3312}
3313
3314static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
3315 struct hda_codec *codec,
3316 struct snd_pcm_substream *substream)
3317{
3318 struct hda_gen_spec *spec = codec->spec;
3319 int err = 0;
3320
3321 mutex_lock(&spec->pcm_mutex);
3322 if (!spec->indep_hp_enabled)
3323 err = -EBUSY;
3324 else
3325 spec->active_streams |= 1 << STREAM_INDEP_HP;
3326 mutex_unlock(&spec->pcm_mutex);
3327 return err;
3328}
3329
3330static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
3331 struct hda_codec *codec,
3332 struct snd_pcm_substream *substream)
3333{
3334 struct hda_gen_spec *spec = codec->spec;
3335 mutex_lock(&spec->pcm_mutex);
3336 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
3337 mutex_unlock(&spec->pcm_mutex);
3338 return 0;
3339}
3340
Takashi Iwai352f7f92012-12-19 12:52:06 +01003341/*
3342 * Digital out
3343 */
3344static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
3345 struct hda_codec *codec,
3346 struct snd_pcm_substream *substream)
3347{
3348 struct hda_gen_spec *spec = codec->spec;
3349 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3350}
3351
3352static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3353 struct hda_codec *codec,
3354 unsigned int stream_tag,
3355 unsigned int format,
3356 struct snd_pcm_substream *substream)
3357{
3358 struct hda_gen_spec *spec = codec->spec;
3359 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3360 stream_tag, format, substream);
3361}
3362
3363static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3364 struct hda_codec *codec,
3365 struct snd_pcm_substream *substream)
3366{
3367 struct hda_gen_spec *spec = codec->spec;
3368 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
3369}
3370
3371static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
3372 struct hda_codec *codec,
3373 struct snd_pcm_substream *substream)
3374{
3375 struct hda_gen_spec *spec = codec->spec;
3376 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3377}
3378
3379/*
3380 * Analog capture
3381 */
3382static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3383 struct hda_codec *codec,
3384 unsigned int stream_tag,
3385 unsigned int format,
3386 struct snd_pcm_substream *substream)
3387{
3388 struct hda_gen_spec *spec = codec->spec;
3389
3390 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01003391 stream_tag, 0, format);
3392 return 0;
3393}
3394
Takashi Iwai352f7f92012-12-19 12:52:06 +01003395static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3396 struct hda_codec *codec,
3397 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01003398{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003399 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01003400
Takashi Iwai352f7f92012-12-19 12:52:06 +01003401 snd_hda_codec_cleanup_stream(codec,
3402 spec->adc_nids[substream->number + 1]);
Takashi Iwai97ec5582006-03-21 11:29:07 +01003403 return 0;
3404}
3405
Takashi Iwai352f7f92012-12-19 12:52:06 +01003406/*
3407 */
3408static const struct hda_pcm_stream pcm_analog_playback = {
3409 .substreams = 1,
3410 .channels_min = 2,
3411 .channels_max = 8,
3412 /* NID is set in build_pcms */
3413 .ops = {
3414 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003415 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003416 .prepare = playback_pcm_prepare,
3417 .cleanup = playback_pcm_cleanup
3418 },
3419};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420
Takashi Iwai352f7f92012-12-19 12:52:06 +01003421static const struct hda_pcm_stream pcm_analog_capture = {
3422 .substreams = 1,
3423 .channels_min = 2,
3424 .channels_max = 2,
3425 /* NID is set in build_pcms */
3426};
3427
3428static const struct hda_pcm_stream pcm_analog_alt_playback = {
3429 .substreams = 1,
3430 .channels_min = 2,
3431 .channels_max = 2,
3432 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003433 .ops = {
3434 .open = alt_playback_pcm_open,
3435 .close = alt_playback_pcm_close
3436 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01003437};
3438
3439static const struct hda_pcm_stream pcm_analog_alt_capture = {
3440 .substreams = 2, /* can be overridden */
3441 .channels_min = 2,
3442 .channels_max = 2,
3443 /* NID is set in build_pcms */
3444 .ops = {
3445 .prepare = alt_capture_pcm_prepare,
3446 .cleanup = alt_capture_pcm_cleanup
3447 },
3448};
3449
3450static const struct hda_pcm_stream pcm_digital_playback = {
3451 .substreams = 1,
3452 .channels_min = 2,
3453 .channels_max = 2,
3454 /* NID is set in build_pcms */
3455 .ops = {
3456 .open = dig_playback_pcm_open,
3457 .close = dig_playback_pcm_close,
3458 .prepare = dig_playback_pcm_prepare,
3459 .cleanup = dig_playback_pcm_cleanup
3460 },
3461};
3462
3463static const struct hda_pcm_stream pcm_digital_capture = {
3464 .substreams = 1,
3465 .channels_min = 2,
3466 .channels_max = 2,
3467 /* NID is set in build_pcms */
3468};
3469
3470/* Used by build_pcms to flag that a PCM has no playback stream */
3471static const struct hda_pcm_stream pcm_null_stream = {
3472 .substreams = 0,
3473 .channels_min = 0,
3474 .channels_max = 0,
3475};
3476
3477/*
3478 * dynamic changing ADC PCM streams
3479 */
3480static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
3481{
3482 struct hda_gen_spec *spec = codec->spec;
3483 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
3484
3485 if (spec->cur_adc && spec->cur_adc != new_adc) {
3486 /* stream is running, let's swap the current ADC */
3487 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
3488 spec->cur_adc = new_adc;
3489 snd_hda_codec_setup_stream(codec, new_adc,
3490 spec->cur_adc_stream_tag, 0,
3491 spec->cur_adc_format);
3492 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003494 return false;
3495}
3496
3497/* analog capture with dynamic dual-adc changes */
3498static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3499 struct hda_codec *codec,
3500 unsigned int stream_tag,
3501 unsigned int format,
3502 struct snd_pcm_substream *substream)
3503{
3504 struct hda_gen_spec *spec = codec->spec;
3505 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
3506 spec->cur_adc_stream_tag = stream_tag;
3507 spec->cur_adc_format = format;
3508 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
3509 return 0;
3510}
3511
3512static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3513 struct hda_codec *codec,
3514 struct snd_pcm_substream *substream)
3515{
3516 struct hda_gen_spec *spec = codec->spec;
3517 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
3518 spec->cur_adc = 0;
3519 return 0;
3520}
3521
3522static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
3523 .substreams = 1,
3524 .channels_min = 2,
3525 .channels_max = 2,
3526 .nid = 0, /* fill later */
3527 .ops = {
3528 .prepare = dyn_adc_capture_pcm_prepare,
3529 .cleanup = dyn_adc_capture_pcm_cleanup
3530 },
3531};
3532
Takashi Iwaif873e532012-12-20 16:58:39 +01003533static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
3534 const char *chip_name)
3535{
3536 char *p;
3537
3538 if (*str)
3539 return;
3540 strlcpy(str, chip_name, len);
3541
3542 /* drop non-alnum chars after a space */
3543 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
3544 if (!isalnum(p[1])) {
3545 *p = 0;
3546 break;
3547 }
3548 }
3549 strlcat(str, sfx, len);
3550}
3551
Takashi Iwai352f7f92012-12-19 12:52:06 +01003552/* build PCM streams based on the parsed results */
3553int snd_hda_gen_build_pcms(struct hda_codec *codec)
3554{
3555 struct hda_gen_spec *spec = codec->spec;
3556 struct hda_pcm *info = spec->pcm_rec;
3557 const struct hda_pcm_stream *p;
3558 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559
3560 codec->num_pcms = 1;
3561 codec->pcm_info = info;
3562
Takashi Iwai352f7f92012-12-19 12:52:06 +01003563 if (spec->no_analog)
3564 goto skip_analog;
3565
Takashi Iwaif873e532012-12-20 16:58:39 +01003566 fill_pcm_stream_name(spec->stream_name_analog,
3567 sizeof(spec->stream_name_analog),
3568 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003569 info->name = spec->stream_name_analog;
3570
3571 if (spec->multiout.num_dacs > 0) {
3572 p = spec->stream_analog_playback;
3573 if (!p)
3574 p = &pcm_analog_playback;
3575 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3576 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
3577 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
3578 spec->multiout.max_channels;
3579 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
3580 spec->autocfg.line_outs == 2)
3581 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
3582 snd_pcm_2_1_chmaps;
3583 }
3584 if (spec->num_adc_nids) {
3585 p = spec->stream_analog_capture;
3586 if (!p) {
3587 if (spec->dyn_adc_switch)
3588 p = &dyn_adc_pcm_analog_capture;
3589 else
3590 p = &pcm_analog_capture;
3591 }
3592 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3593 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
3594 }
3595
Takashi Iwai352f7f92012-12-19 12:52:06 +01003596 skip_analog:
3597 /* SPDIF for stream index #1 */
3598 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01003599 fill_pcm_stream_name(spec->stream_name_digital,
3600 sizeof(spec->stream_name_digital),
3601 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003602 codec->num_pcms = 2;
3603 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
3604 info = spec->pcm_rec + 1;
3605 info->name = spec->stream_name_digital;
3606 if (spec->dig_out_type)
3607 info->pcm_type = spec->dig_out_type;
3608 else
3609 info->pcm_type = HDA_PCM_TYPE_SPDIF;
3610 if (spec->multiout.dig_out_nid) {
3611 p = spec->stream_digital_playback;
3612 if (!p)
3613 p = &pcm_digital_playback;
3614 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3615 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
3616 }
3617 if (spec->dig_in_nid) {
3618 p = spec->stream_digital_capture;
3619 if (!p)
3620 p = &pcm_digital_capture;
3621 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3622 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
3623 }
3624 }
3625
3626 if (spec->no_analog)
3627 return 0;
3628
3629 /* If the use of more than one ADC is requested for the current
3630 * model, configure a second analog capture-only PCM.
3631 */
3632 have_multi_adcs = (spec->num_adc_nids > 1) &&
3633 !spec->dyn_adc_switch && !spec->auto_mic;
3634 /* Additional Analaog capture for index #2 */
3635 if (spec->alt_dac_nid || have_multi_adcs) {
3636 codec->num_pcms = 3;
3637 info = spec->pcm_rec + 2;
3638 info->name = spec->stream_name_analog;
3639 if (spec->alt_dac_nid) {
3640 p = spec->stream_analog_alt_playback;
3641 if (!p)
3642 p = &pcm_analog_alt_playback;
3643 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3644 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
3645 spec->alt_dac_nid;
3646 } else {
3647 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
3648 pcm_null_stream;
3649 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
3650 }
3651 if (have_multi_adcs) {
3652 p = spec->stream_analog_alt_capture;
3653 if (!p)
3654 p = &pcm_analog_alt_capture;
3655 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3656 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
3657 spec->adc_nids[1];
3658 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
3659 spec->num_adc_nids - 1;
3660 } else {
3661 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
3662 pcm_null_stream;
3663 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
3664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 }
3666
3667 return 0;
3668}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003669EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
3670
3671
3672/*
3673 * Standard auto-parser initializations
3674 */
3675
Takashi Iwaid4156932013-01-07 10:08:02 +01003676/* configure the given path as a proper output */
3677static void set_output_and_unmute(struct hda_codec *codec,
Takashi Iwai196c17662013-01-04 15:01:40 +01003678 int pin_type, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003679{
3680 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01003681 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003682
Takashi Iwai196c17662013-01-04 15:01:40 +01003683 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01003684 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003685 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01003686 pin = path->path[path->depth - 1];
3687 snd_hda_set_pin_ctl_cache(codec, pin, pin_type);
Takashi Iwaie1284af2013-01-03 16:33:02 +01003688 snd_hda_activate_path(codec, path, path->active, true);
3689 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003690}
3691
3692/* initialize primary output paths */
3693static void init_multi_out(struct hda_codec *codec)
3694{
3695 struct hda_gen_spec *spec = codec->spec;
3696 int pin_type;
3697 int i;
3698
3699 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
3700 pin_type = PIN_HP;
3701 else
3702 pin_type = PIN_OUT;
3703
Takashi Iwaid4156932013-01-07 10:08:02 +01003704 for (i = 0; i < spec->autocfg.line_outs; i++)
3705 set_output_and_unmute(codec, pin_type, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003706}
3707
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003708
3709static void __init_extra_out(struct hda_codec *codec, int num_outs,
Takashi Iwaid4156932013-01-07 10:08:02 +01003710 int *paths, int type)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003711{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003712 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003713
Takashi Iwaid4156932013-01-07 10:08:02 +01003714 for (i = 0; i < num_outs; i++)
3715 set_output_and_unmute(codec, type, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003716}
3717
3718/* initialize hp and speaker paths */
3719static void init_extra_out(struct hda_codec *codec)
3720{
3721 struct hda_gen_spec *spec = codec->spec;
3722
3723 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
3724 __init_extra_out(codec, spec->autocfg.hp_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01003725 spec->hp_paths, PIN_HP);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003726 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
3727 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01003728 spec->speaker_paths, PIN_OUT);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003729}
3730
3731/* initialize multi-io paths */
3732static void init_multi_io(struct hda_codec *codec)
3733{
3734 struct hda_gen_spec *spec = codec->spec;
3735 int i;
3736
3737 for (i = 0; i < spec->multi_ios; i++) {
3738 hda_nid_t pin = spec->multi_io[i].pin;
3739 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01003740 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003741 if (!path)
3742 continue;
3743 if (!spec->multi_io[i].ctl_in)
3744 spec->multi_io[i].ctl_in =
3745 snd_hda_codec_update_cache(codec, pin, 0,
3746 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3747 snd_hda_activate_path(codec, path, path->active, true);
3748 }
3749}
3750
3751/* set up the input pin config, depending on the given auto-pin type */
3752static void set_input_pin(struct hda_codec *codec, hda_nid_t nid,
3753 int auto_pin_type)
3754{
3755 unsigned int val = PIN_IN;
3756 if (auto_pin_type == AUTO_PIN_MIC)
3757 val |= snd_hda_get_default_vref(codec, nid);
Takashi Iwai7594aa32012-12-20 15:38:40 +01003758 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003759}
3760
3761/* set up input pins and loopback paths */
3762static void init_analog_input(struct hda_codec *codec)
3763{
3764 struct hda_gen_spec *spec = codec->spec;
3765 struct auto_pin_cfg *cfg = &spec->autocfg;
3766 int i;
3767
3768 for (i = 0; i < cfg->num_inputs; i++) {
3769 hda_nid_t nid = cfg->inputs[i].pin;
3770 if (is_input_pin(codec, nid))
3771 set_input_pin(codec, nid, cfg->inputs[i].type);
3772
3773 /* init loopback inputs */
3774 if (spec->mixer_nid) {
3775 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01003776 path = snd_hda_get_path_from_idx(codec, spec->loopback_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003777 if (path)
3778 snd_hda_activate_path(codec, path,
3779 path->active, false);
3780 }
3781 }
3782}
3783
3784/* initialize ADC paths */
3785static void init_input_src(struct hda_codec *codec)
3786{
3787 struct hda_gen_spec *spec = codec->spec;
3788 struct hda_input_mux *imux = &spec->input_mux;
3789 struct nid_path *path;
3790 int i, c, nums;
3791
3792 if (spec->dyn_adc_switch)
3793 nums = 1;
3794 else
3795 nums = spec->num_adc_nids;
3796
3797 for (c = 0; c < nums; c++) {
3798 for (i = 0; i < imux->num_items; i++) {
3799 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
3800 get_adc_nid(codec, c, i));
3801 if (path) {
3802 bool active = path->active;
3803 if (i == spec->cur_mux[c])
3804 active = true;
3805 snd_hda_activate_path(codec, path, active, false);
3806 }
3807 }
3808 }
3809
3810 if (spec->shared_mic_hp)
3811 update_shared_mic_hp(codec, spec->cur_mux[0]);
3812
3813 if (spec->cap_sync_hook)
3814 spec->cap_sync_hook(codec);
3815}
3816
3817/* set right pin controls for digital I/O */
3818static void init_digital(struct hda_codec *codec)
3819{
3820 struct hda_gen_spec *spec = codec->spec;
3821 int i;
3822 hda_nid_t pin;
3823
Takashi Iwaid4156932013-01-07 10:08:02 +01003824 for (i = 0; i < spec->autocfg.dig_outs; i++)
3825 set_output_and_unmute(codec, PIN_OUT, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003826 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003827 if (pin) {
3828 struct nid_path *path;
Takashi Iwai7594aa32012-12-20 15:38:40 +01003829 snd_hda_set_pin_ctl_cache(codec, pin, PIN_IN);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003830 path = snd_hda_get_path_from_idx(codec, spec->digin_path);
3831 if (path)
3832 snd_hda_activate_path(codec, path, path->active, false);
3833 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003834}
3835
Takashi Iwai973e4972012-12-20 15:16:09 +01003836/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
3837 * invalid unsol tags by some reason
3838 */
3839static void clear_unsol_on_unused_pins(struct hda_codec *codec)
3840{
3841 int i;
3842
3843 for (i = 0; i < codec->init_pins.used; i++) {
3844 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
3845 hda_nid_t nid = pin->nid;
3846 if (is_jack_detectable(codec, nid) &&
3847 !snd_hda_jack_tbl_get(codec, nid))
3848 snd_hda_codec_update_cache(codec, nid, 0,
3849 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
3850 }
3851}
3852
Takashi Iwai352f7f92012-12-19 12:52:06 +01003853int snd_hda_gen_init(struct hda_codec *codec)
3854{
3855 struct hda_gen_spec *spec = codec->spec;
3856
3857 if (spec->init_hook)
3858 spec->init_hook(codec);
3859
3860 snd_hda_apply_verbs(codec);
3861
Takashi Iwai3bbcd272012-12-20 11:50:58 +01003862 codec->cached_write = 1;
3863
Takashi Iwai352f7f92012-12-19 12:52:06 +01003864 init_multi_out(codec);
3865 init_extra_out(codec);
3866 init_multi_io(codec);
3867 init_analog_input(codec);
3868 init_input_src(codec);
3869 init_digital(codec);
3870
Takashi Iwai973e4972012-12-20 15:16:09 +01003871 clear_unsol_on_unused_pins(codec);
3872
Takashi Iwai352f7f92012-12-19 12:52:06 +01003873 /* call init functions of standard auto-mute helpers */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003874 snd_hda_gen_hp_automute(codec, NULL);
3875 snd_hda_gen_line_automute(codec, NULL);
3876 snd_hda_gen_mic_autoswitch(codec, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003877
Takashi Iwai3bbcd272012-12-20 11:50:58 +01003878 snd_hda_codec_flush_amp_cache(codec);
3879 snd_hda_codec_flush_cmd_cache(codec);
3880
Takashi Iwai352f7f92012-12-19 12:52:06 +01003881 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
3882 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
3883
3884 hda_call_check_power_status(codec, 0x01);
3885 return 0;
3886}
3887EXPORT_SYMBOL(snd_hda_gen_init);
3888
3889
3890/*
3891 * the generic codec support
3892 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893
Takashi Iwai83012a72012-08-24 18:38:08 +02003894#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003895static int generic_check_power_status(struct hda_codec *codec, hda_nid_t nid)
3896{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003897 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003898 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
3899}
3900#endif
3901
Takashi Iwai352f7f92012-12-19 12:52:06 +01003902static void generic_free(struct hda_codec *codec)
3903{
3904 snd_hda_gen_spec_free(codec->spec);
3905 kfree(codec->spec);
3906 codec->spec = NULL;
3907}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908
Takashi Iwai352f7f92012-12-19 12:52:06 +01003909static const struct hda_codec_ops generic_patch_ops = {
3910 .build_controls = snd_hda_gen_build_controls,
3911 .build_pcms = snd_hda_gen_build_pcms,
3912 .init = snd_hda_gen_init,
3913 .free = generic_free,
3914 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02003915#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003916 .check_power_status = generic_check_power_status,
3917#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918};
3919
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920int snd_hda_parse_generic_codec(struct hda_codec *codec)
3921{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003922 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 int err;
3924
Takashi Iwaie560d8d2005-09-09 14:21:46 +02003925 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003926 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003928 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003931 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
3932 if (err < 0)
3933 return err;
3934
3935 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003936 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 goto error;
3938
3939 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 return 0;
3941
Takashi Iwai352f7f92012-12-19 12:52:06 +01003942error:
3943 generic_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 return err;
3945}
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003946EXPORT_SYMBOL(snd_hda_parse_generic_codec);