blob: a34c581b60821c8acc2c6f594178b982c6a5da30 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Universal Interface for Intel High Definition Audio Codec
3 *
4 * Generic widget tree parser
5 *
6 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
7 *
8 * This driver is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This driver is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/init.h>
24#include <linux/slab.h>
Paul Gortmakerd81a6d72011-09-22 09:34:58 -040025#include <linux/export.h>
Takashi Iwai352f7f92012-12-19 12:52:06 +010026#include <linux/sort.h>
Takashi Iwaif873e532012-12-20 16:58:39 +010027#include <linux/ctype.h>
28#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <sound/core.h>
Takashi Iwai352f7f92012-12-19 12:52:06 +010030#include <sound/jack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "hda_codec.h"
32#include "hda_local.h"
Takashi Iwai352f7f92012-12-19 12:52:06 +010033#include "hda_auto_parser.h"
34#include "hda_jack.h"
35#include "hda_generic.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Takashi Iwai352f7f92012-12-19 12:52:06 +010038/* initialize hda_gen_spec struct */
39int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
Takashi Iwai352f7f92012-12-19 12:52:06 +010041 snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
42 snd_array_init(&spec->bind_ctls, sizeof(struct hda_bind_ctls *), 8);
43 snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
Takashi Iwai38cf6f12012-12-21 14:09:42 +010044 mutex_init(&spec->pcm_mutex);
Takashi Iwai352f7f92012-12-19 12:52:06 +010045 return 0;
46}
47EXPORT_SYMBOL_HDA(snd_hda_gen_spec_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Takashi Iwai12c93df2012-12-19 14:38:33 +010049struct snd_kcontrol_new *
50snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
51 const struct snd_kcontrol_new *temp)
Takashi Iwai352f7f92012-12-19 12:52:06 +010052{
53 struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
54 if (!knew)
55 return NULL;
56 *knew = *temp;
57 if (name)
58 knew->name = kstrdup(name, GFP_KERNEL);
59 else if (knew->name)
60 knew->name = kstrdup(knew->name, GFP_KERNEL);
61 if (!knew->name)
62 return NULL;
63 return knew;
64}
Takashi Iwai12c93df2012-12-19 14:38:33 +010065EXPORT_SYMBOL_HDA(snd_hda_gen_add_kctl);
Takashi Iwai352f7f92012-12-19 12:52:06 +010066
67static void free_kctls(struct hda_gen_spec *spec)
68{
69 if (spec->kctls.list) {
70 struct snd_kcontrol_new *kctl = spec->kctls.list;
71 int i;
72 for (i = 0; i < spec->kctls.used; i++)
73 kfree(kctl[i].name);
74 }
75 snd_array_free(&spec->kctls);
76}
77
78static struct hda_bind_ctls *new_bind_ctl(struct hda_codec *codec,
79 unsigned int nums,
80 struct hda_ctl_ops *ops)
81{
82 struct hda_gen_spec *spec = codec->spec;
83 struct hda_bind_ctls **ctlp, *ctl;
84 ctlp = snd_array_new(&spec->bind_ctls);
85 if (!ctlp)
86 return NULL;
87 ctl = kzalloc(sizeof(*ctl) + sizeof(long) * (nums + 1), GFP_KERNEL);
88 *ctlp = ctl;
89 if (ctl)
90 ctl->ops = ops;
91 return ctl;
92}
93
94static void free_bind_ctls(struct hda_gen_spec *spec)
95{
96 if (spec->bind_ctls.list) {
97 struct hda_bind_ctls **ctl = spec->bind_ctls.list;
98 int i;
99 for (i = 0; i < spec->bind_ctls.used; i++)
100 kfree(ctl[i]);
101 }
102 snd_array_free(&spec->bind_ctls);
103}
104
105void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
106{
107 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 return;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100109 free_kctls(spec);
110 free_bind_ctls(spec);
111 snd_array_free(&spec->paths);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
Takashi Iwai352f7f92012-12-19 12:52:06 +0100113EXPORT_SYMBOL_HDA(snd_hda_gen_spec_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100116 * parsing paths
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Takashi Iwaif5172a72013-01-04 13:19:55 +0100119static struct nid_path *get_nid_path(struct hda_codec *codec,
120 hda_nid_t from_nid, hda_nid_t to_nid,
121 int with_aa_mix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100123 struct hda_gen_spec *spec = codec->spec;
124 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Takashi Iwai352f7f92012-12-19 12:52:06 +0100126 for (i = 0; i < spec->paths.used; i++) {
127 struct nid_path *path = snd_array_elem(&spec->paths, i);
128 if (path->depth <= 0)
129 continue;
130 if ((!from_nid || path->path[0] == from_nid) &&
Takashi Iwaif5172a72013-01-04 13:19:55 +0100131 (!to_nid || path->path[path->depth - 1] == to_nid)) {
132 if (with_aa_mix == HDA_PARSE_ALL ||
133 path->with_aa_mix == with_aa_mix)
134 return path;
135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 }
137 return NULL;
138}
Takashi Iwaif5172a72013-01-04 13:19:55 +0100139
140/* get the path between the given NIDs;
141 * passing 0 to either @pin or @dac behaves as a wildcard
142 */
143struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
144 hda_nid_t from_nid, hda_nid_t to_nid)
145{
146 return get_nid_path(codec, from_nid, to_nid, HDA_PARSE_ALL);
147}
Takashi Iwai352f7f92012-12-19 12:52:06 +0100148EXPORT_SYMBOL_HDA(snd_hda_get_nid_path);
149
Takashi Iwai196c17662013-01-04 15:01:40 +0100150/* get the index number corresponding to the path instance;
151 * the index starts from 1, for easier checking the invalid value
152 */
153int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
154{
155 struct hda_gen_spec *spec = codec->spec;
156 struct nid_path *array = spec->paths.list;
157 ssize_t idx;
158
159 if (!spec->paths.used)
160 return 0;
161 idx = path - array;
162 if (idx < 0 || idx >= spec->paths.used)
163 return 0;
164 return idx + 1;
165}
166
167/* get the path instance corresponding to the given index number */
168struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
169{
170 struct hda_gen_spec *spec = codec->spec;
171
172 if (idx <= 0 || idx > spec->paths.used)
173 return NULL;
174 return snd_array_elem(&spec->paths, idx - 1);
175}
176
Takashi Iwai352f7f92012-12-19 12:52:06 +0100177/* check whether the given DAC is already found in any existing paths */
178static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
179{
180 struct hda_gen_spec *spec = codec->spec;
181 int i;
182
183 for (i = 0; i < spec->paths.used; i++) {
184 struct nid_path *path = snd_array_elem(&spec->paths, i);
185 if (path->path[0] == nid)
186 return true;
187 }
188 return false;
189}
190
191/* check whether the given two widgets can be connected */
192static bool is_reachable_path(struct hda_codec *codec,
193 hda_nid_t from_nid, hda_nid_t to_nid)
194{
195 if (!from_nid || !to_nid)
196 return false;
197 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
198}
199
200/* nid, dir and idx */
201#define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
202
203/* check whether the given ctl is already assigned in any path elements */
204static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
205{
206 struct hda_gen_spec *spec = codec->spec;
207 int i;
208
209 val &= AMP_VAL_COMPARE_MASK;
210 for (i = 0; i < spec->paths.used; i++) {
211 struct nid_path *path = snd_array_elem(&spec->paths, i);
212 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
213 return true;
214 }
215 return false;
216}
217
218/* check whether a control with the given (nid, dir, idx) was assigned */
219static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
220 int dir, int idx)
221{
222 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
223 return is_ctl_used(codec, val, NID_PATH_VOL_CTL) ||
224 is_ctl_used(codec, val, NID_PATH_MUTE_CTL);
225}
226
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100227static void print_nid_path(const char *pfx, struct nid_path *path)
228{
229 char buf[40];
230 int i;
231
232
233 buf[0] = 0;
234 for (i = 0; i < path->depth; i++) {
235 char tmp[4];
236 sprintf(tmp, ":%02x", path->path[i]);
237 strlcat(buf, tmp, sizeof(buf));
238 }
239 snd_printdd("%s path: depth=%d %s\n", pfx, path->depth, buf);
240}
241
Takashi Iwai352f7f92012-12-19 12:52:06 +0100242/* called recursively */
243static bool __parse_nid_path(struct hda_codec *codec,
244 hda_nid_t from_nid, hda_nid_t to_nid,
245 int with_aa_mix, struct nid_path *path, int depth)
246{
247 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100248 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100249 int i, nums;
250
251 if (to_nid == spec->mixer_nid) {
Takashi Iwai4ac0eef2012-12-20 18:10:51 +0100252 if (with_aa_mix == HDA_PARSE_NO_AAMIX)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100253 return false;
Takashi Iwai4ac0eef2012-12-20 18:10:51 +0100254 with_aa_mix = HDA_PARSE_ALL; /* mark aa-mix is included */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100255 }
256
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100257 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100258 for (i = 0; i < nums; i++) {
259 if (conn[i] != from_nid) {
260 /* special case: when from_nid is 0,
261 * try to find an empty DAC
262 */
263 if (from_nid ||
264 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
265 is_dac_already_used(codec, conn[i]))
266 continue;
267 }
268 /* aa-mix is requested but not included? */
Takashi Iwai4ac0eef2012-12-20 18:10:51 +0100269 if (!(spec->mixer_nid && with_aa_mix == HDA_PARSE_ONLY_AAMIX))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100270 goto found;
271 }
272 if (depth >= MAX_NID_PATH_DEPTH)
273 return false;
274 for (i = 0; i < nums; i++) {
275 unsigned int type;
276 type = get_wcaps_type(get_wcaps(codec, conn[i]));
277 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
278 type == AC_WID_PIN)
279 continue;
280 if (__parse_nid_path(codec, from_nid, conn[i],
281 with_aa_mix, path, depth + 1))
282 goto found;
283 }
284 return false;
285
286 found:
287 path->path[path->depth] = conn[i];
Takashi Iwaif5172a72013-01-04 13:19:55 +0100288 if (conn[i] == spec->mixer_nid)
289 path->with_aa_mix = true;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100290 path->idx[path->depth + 1] = i;
291 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
292 path->multi[path->depth + 1] = 1;
293 path->depth++;
294 return true;
295}
296
297/* parse the widget path from the given nid to the target nid;
298 * when @from_nid is 0, try to find an empty DAC;
Takashi Iwai4ac0eef2012-12-20 18:10:51 +0100299 * when @with_aa_mix is HDA_PARSE_NO_AAMIX, paths with spec->mixer_nid are
300 * excluded, only the paths that don't go through the mixer will be chosen.
301 * when @with_aa_mix is HDA_PARSE_ONLY_AAMIX, only the paths going through
302 * spec->mixer_nid will be chosen.
303 * when @with_aa_mix is HDA_PARSE_ALL, no special handling about mixer widget.
Takashi Iwai352f7f92012-12-19 12:52:06 +0100304 */
305bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
306 hda_nid_t to_nid, int with_aa_mix,
307 struct nid_path *path)
308{
309 if (__parse_nid_path(codec, from_nid, to_nid, with_aa_mix, path, 1)) {
310 path->path[path->depth] = to_nid;
311 path->depth++;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100312 return true;
313 }
314 return false;
315}
316EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100319 * parse the path between the given NIDs and add to the path list.
320 * if no valid path is found, return NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100322struct nid_path *
323snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
324 hda_nid_t to_nid, int with_aa_mix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100326 struct hda_gen_spec *spec = codec->spec;
327 struct nid_path *path;
328
329 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
330 return NULL;
331
Takashi Iwaif5172a72013-01-04 13:19:55 +0100332 /* check whether the path has been already added */
333 path = get_nid_path(codec, from_nid, to_nid, with_aa_mix);
334 if (path)
335 return path;
336
Takashi Iwai352f7f92012-12-19 12:52:06 +0100337 path = snd_array_new(&spec->paths);
338 if (!path)
339 return NULL;
340 memset(path, 0, sizeof(*path));
341 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, with_aa_mix, path))
342 return path;
343 /* push back */
344 spec->paths.used--;
345 return NULL;
346}
347EXPORT_SYMBOL_HDA(snd_hda_add_new_path);
348
349/* look for an empty DAC slot */
350static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
351 bool is_digital)
352{
353 struct hda_gen_spec *spec = codec->spec;
354 bool cap_digital;
355 int i;
356
357 for (i = 0; i < spec->num_all_dacs; i++) {
358 hda_nid_t nid = spec->all_dacs[i];
359 if (!nid || is_dac_already_used(codec, nid))
360 continue;
361 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
362 if (is_digital != cap_digital)
363 continue;
364 if (is_reachable_path(codec, nid, pin))
365 return nid;
366 }
367 return 0;
368}
369
370/* replace the channels in the composed amp value with the given number */
371static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
372{
373 val &= ~(0x3U << 16);
374 val |= chs << 16;
375 return val;
376}
377
378/* check whether the widget has the given amp capability for the direction */
379static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
380 int dir, unsigned int bits)
381{
382 if (!nid)
383 return false;
384 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
385 if (query_amp_caps(codec, nid, dir) & bits)
386 return true;
387 return false;
388}
389
390#define nid_has_mute(codec, nid, dir) \
391 check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
392#define nid_has_volume(codec, nid, dir) \
393 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
394
395/* look for a widget suitable for assigning a mute switch in the path */
396static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
397 struct nid_path *path)
398{
399 int i;
400
401 for (i = path->depth - 1; i >= 0; i--) {
402 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
403 return path->path[i];
404 if (i != path->depth - 1 && i != 0 &&
405 nid_has_mute(codec, path->path[i], HDA_INPUT))
406 return path->path[i];
407 }
408 return 0;
409}
410
411/* look for a widget suitable for assigning a volume ctl in the path */
412static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
413 struct nid_path *path)
414{
415 int i;
416
417 for (i = path->depth - 1; i >= 0; i--) {
418 if (nid_has_volume(codec, path->path[i], HDA_OUTPUT))
419 return path->path[i];
420 }
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200421 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
424/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100425 * path activation / deactivation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100427
428/* can have the amp-in capability? */
429static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100431 hda_nid_t nid = path->path[idx];
432 unsigned int caps = get_wcaps(codec, nid);
433 unsigned int type = get_wcaps_type(caps);
434
435 if (!(caps & AC_WCAP_IN_AMP))
436 return false;
437 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
438 return false;
439 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440}
441
Takashi Iwai352f7f92012-12-19 12:52:06 +0100442/* can have the amp-out capability? */
443static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100445 hda_nid_t nid = path->path[idx];
446 unsigned int caps = get_wcaps(codec, nid);
447 unsigned int type = get_wcaps_type(caps);
448
449 if (!(caps & AC_WCAP_OUT_AMP))
450 return false;
451 if (type == AC_WID_PIN && !idx) /* only for output pins */
452 return false;
453 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455
Takashi Iwai352f7f92012-12-19 12:52:06 +0100456/* check whether the given (nid,dir,idx) is active */
457static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
458 unsigned int idx, unsigned int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100460 struct hda_gen_spec *spec = codec->spec;
461 int i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Takashi Iwai352f7f92012-12-19 12:52:06 +0100463 for (n = 0; n < spec->paths.used; n++) {
464 struct nid_path *path = snd_array_elem(&spec->paths, n);
465 if (!path->active)
466 continue;
467 for (i = 0; i < path->depth; i++) {
468 if (path->path[i] == nid) {
469 if (dir == HDA_OUTPUT || path->idx[i] == idx)
470 return true;
471 break;
472 }
473 }
474 }
475 return false;
476}
477
478/* get the default amp value for the target state */
479static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
480 int dir, bool enable)
481{
482 unsigned int caps;
483 unsigned int val = 0;
484
485 caps = query_amp_caps(codec, nid, dir);
486 if (caps & AC_AMPCAP_NUM_STEPS) {
487 /* set to 0dB */
488 if (enable)
489 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
490 }
491 if (caps & AC_AMPCAP_MUTE) {
492 if (!enable)
493 val |= HDA_AMP_MUTE;
494 }
495 return val;
496}
497
498/* initialize the amp value (only at the first time) */
499static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
500{
501 int val = get_amp_val_to_activate(codec, nid, dir, false);
502 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
503}
504
505static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
506 int idx, bool enable)
507{
508 int val;
509 if (is_ctl_associated(codec, nid, dir, idx) ||
Takashi Iwai985803c2013-01-03 16:30:04 +0100510 (!enable && is_active_nid(codec, nid, dir, idx)))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100511 return;
512 val = get_amp_val_to_activate(codec, nid, dir, enable);
513 snd_hda_codec_amp_stereo(codec, nid, dir, idx, 0xff, val);
514}
515
516static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
517 int i, bool enable)
518{
519 hda_nid_t nid = path->path[i];
520 init_amp(codec, nid, HDA_OUTPUT, 0);
521 activate_amp(codec, nid, HDA_OUTPUT, 0, enable);
522}
523
524static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
525 int i, bool enable, bool add_aamix)
526{
527 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100528 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100529 int n, nums, idx;
530 int type;
531 hda_nid_t nid = path->path[i];
532
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100533 nums = snd_hda_get_conn_list(codec, nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100534 type = get_wcaps_type(get_wcaps(codec, nid));
535 if (type == AC_WID_PIN ||
536 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
537 nums = 1;
538 idx = 0;
539 } else
540 idx = path->idx[i];
541
542 for (n = 0; n < nums; n++)
543 init_amp(codec, nid, HDA_INPUT, n);
544
545 if (is_ctl_associated(codec, nid, HDA_INPUT, idx))
546 return;
547
548 /* here is a little bit tricky in comparison with activate_amp_out();
549 * when aa-mixer is available, we need to enable the path as well
550 */
551 for (n = 0; n < nums; n++) {
552 if (n != idx && (!add_aamix || conn[n] != spec->mixer_nid))
553 continue;
554 activate_amp(codec, nid, HDA_INPUT, n, enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 }
556}
557
Takashi Iwai352f7f92012-12-19 12:52:06 +0100558/* activate or deactivate the given path
559 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100561void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
562 bool enable, bool add_aamix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100564 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Takashi Iwai352f7f92012-12-19 12:52:06 +0100566 if (!enable)
567 path->active = false;
568
569 for (i = path->depth - 1; i >= 0; i--) {
570 if (enable && path->multi[i])
571 snd_hda_codec_write_cache(codec, path->path[i], 0,
572 AC_VERB_SET_CONNECT_SEL,
573 path->idx[i]);
574 if (has_amp_in(codec, path, i))
575 activate_amp_in(codec, path, i, enable, add_aamix);
576 if (has_amp_out(codec, path, i))
577 activate_amp_out(codec, path, i, enable);
578 }
579
580 if (enable)
581 path->active = true;
582}
583EXPORT_SYMBOL_HDA(snd_hda_activate_path);
584
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100585/* turn on/off EAPD on the given pin */
586static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
587{
588 struct hda_gen_spec *spec = codec->spec;
589 if (spec->own_eapd_ctl ||
590 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
591 return;
Takashi Iwaiecac3ed2012-12-21 15:23:01 +0100592 if (codec->inv_eapd)
593 enable = !enable;
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100594 snd_hda_codec_update_cache(codec, pin, 0,
595 AC_VERB_SET_EAPD_BTLENABLE,
596 enable ? 0x02 : 0x00);
597}
598
Takashi Iwai352f7f92012-12-19 12:52:06 +0100599
600/*
601 * Helper functions for creating mixer ctl elements
602 */
603
604enum {
605 HDA_CTL_WIDGET_VOL,
606 HDA_CTL_WIDGET_MUTE,
607 HDA_CTL_BIND_MUTE,
608 HDA_CTL_BIND_VOL,
609 HDA_CTL_BIND_SW,
610};
611static const struct snd_kcontrol_new control_templates[] = {
612 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
613 HDA_CODEC_MUTE(NULL, 0, 0, 0),
614 HDA_BIND_MUTE(NULL, 0, 0, 0),
615 HDA_BIND_VOL(NULL, 0),
616 HDA_BIND_SW(NULL, 0),
617};
618
619/* add dynamic controls from template */
620static int add_control(struct hda_gen_spec *spec, int type, const char *name,
621 int cidx, unsigned long val)
622{
623 struct snd_kcontrol_new *knew;
624
Takashi Iwai12c93df2012-12-19 14:38:33 +0100625 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100626 if (!knew)
627 return -ENOMEM;
628 knew->index = cidx;
629 if (get_amp_nid_(val))
630 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
631 knew->private_value = val;
632 return 0;
633}
634
635static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
636 const char *pfx, const char *dir,
637 const char *sfx, int cidx, unsigned long val)
638{
639 char name[32];
640 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
641 return add_control(spec, type, name, cidx, val);
642}
643
644#define add_pb_vol_ctrl(spec, type, pfx, val) \
645 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
646#define add_pb_sw_ctrl(spec, type, pfx, val) \
647 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
648#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
649 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
650#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
651 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
652
653static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
654 unsigned int chs, struct nid_path *path)
655{
656 unsigned int val;
657 if (!path)
658 return 0;
659 val = path->ctls[NID_PATH_VOL_CTL];
660 if (!val)
661 return 0;
662 val = amp_val_replace_channels(val, chs);
663 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
664}
665
666/* return the channel bits suitable for the given path->ctls[] */
667static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
668 int type)
669{
670 int chs = 1; /* mono (left only) */
671 if (path) {
672 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
673 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
674 chs = 3; /* stereo */
675 }
676 return chs;
677}
678
679static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
680 struct nid_path *path)
681{
682 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
683 return add_vol_ctl(codec, pfx, cidx, chs, path);
684}
685
686/* create a mute-switch for the given mixer widget;
687 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
688 */
689static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
690 unsigned int chs, struct nid_path *path)
691{
692 unsigned int val;
693 int type = HDA_CTL_WIDGET_MUTE;
694
695 if (!path)
696 return 0;
697 val = path->ctls[NID_PATH_MUTE_CTL];
698 if (!val)
699 return 0;
700 val = amp_val_replace_channels(val, chs);
701 if (get_amp_direction_(val) == HDA_INPUT) {
702 hda_nid_t nid = get_amp_nid_(val);
703 int nums = snd_hda_get_num_conns(codec, nid);
704 if (nums > 1) {
705 type = HDA_CTL_BIND_MUTE;
706 val |= nums << 19;
707 }
708 }
709 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
710}
711
712static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
713 int cidx, struct nid_path *path)
714{
715 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
716 return add_sw_ctl(codec, pfx, cidx, chs, path);
717}
718
719static const char * const channel_name[4] = {
720 "Front", "Surround", "CLFE", "Side"
721};
722
723/* give some appropriate ctl name prefix for the given line out channel */
724static const char *get_line_out_pfx(struct hda_gen_spec *spec, int ch,
725 bool can_be_master, int *index)
726{
727 struct auto_pin_cfg *cfg = &spec->autocfg;
728
729 *index = 0;
730 if (cfg->line_outs == 1 && !spec->multi_ios &&
731 !cfg->hp_outs && !cfg->speaker_outs && can_be_master)
732 return spec->vmaster_mute.hook ? "PCM" : "Master";
733
734 /* if there is really a single DAC used in the whole output paths,
735 * use it master (or "PCM" if a vmaster hook is present)
736 */
737 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
738 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
739 return spec->vmaster_mute.hook ? "PCM" : "Master";
740
741 switch (cfg->line_out_type) {
742 case AUTO_PIN_SPEAKER_OUT:
743 if (cfg->line_outs == 1)
744 return "Speaker";
745 if (cfg->line_outs == 2)
746 return ch ? "Bass Speaker" : "Speaker";
747 break;
748 case AUTO_PIN_HP_OUT:
749 /* for multi-io case, only the primary out */
750 if (ch && spec->multi_ios)
751 break;
752 *index = ch;
753 return "Headphone";
754 default:
755 if (cfg->line_outs == 1 && !spec->multi_ios)
756 return "PCM";
757 break;
758 }
759 if (ch >= ARRAY_SIZE(channel_name)) {
760 snd_BUG();
761 return "PCM";
762 }
763
764 return channel_name[ch];
765}
766
767/*
768 * Parse output paths
769 */
770
771/* badness definition */
772enum {
773 /* No primary DAC is found for the main output */
774 BAD_NO_PRIMARY_DAC = 0x10000,
775 /* No DAC is found for the extra output */
776 BAD_NO_DAC = 0x4000,
777 /* No possible multi-ios */
778 BAD_MULTI_IO = 0x103,
779 /* No individual DAC for extra output */
780 BAD_NO_EXTRA_DAC = 0x102,
781 /* No individual DAC for extra surrounds */
782 BAD_NO_EXTRA_SURR_DAC = 0x101,
783 /* Primary DAC shared with main surrounds */
784 BAD_SHARED_SURROUND = 0x100,
785 /* Primary DAC shared with main CLFE */
786 BAD_SHARED_CLFE = 0x10,
787 /* Primary DAC shared with extra surrounds */
788 BAD_SHARED_EXTRA_SURROUND = 0x10,
789 /* Volume widget is shared */
790 BAD_SHARED_VOL = 0x10,
791};
792
793/* look for widgets in the path between the given NIDs appropriate for
794 * volume and mute controls, and assign the values to ctls[].
795 *
796 * When no appropriate widget is found in the path, the badness value
797 * is incremented depending on the situation. The function returns the
798 * total badness for both volume and mute controls.
799 */
800static int assign_out_path_ctls(struct hda_codec *codec, hda_nid_t pin,
801 hda_nid_t dac)
802{
803 struct nid_path *path = snd_hda_get_nid_path(codec, dac, pin);
804 hda_nid_t nid;
805 unsigned int val;
806 int badness = 0;
807
808 if (!path)
809 return BAD_SHARED_VOL * 2;
810 nid = look_for_out_vol_nid(codec, path);
811 if (nid) {
812 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
813 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
814 badness += BAD_SHARED_VOL;
815 else
816 path->ctls[NID_PATH_VOL_CTL] = val;
817 } else
818 badness += BAD_SHARED_VOL;
819 nid = look_for_out_mute_nid(codec, path);
820 if (nid) {
821 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
822 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
823 nid_has_mute(codec, nid, HDA_OUTPUT))
824 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
825 else
826 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
827 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
828 badness += BAD_SHARED_VOL;
829 else
830 path->ctls[NID_PATH_MUTE_CTL] = val;
831 } else
832 badness += BAD_SHARED_VOL;
833 return badness;
834}
835
836struct badness_table {
837 int no_primary_dac; /* no primary DAC */
838 int no_dac; /* no secondary DACs */
839 int shared_primary; /* primary DAC is shared with main output */
840 int shared_surr; /* secondary DAC shared with main or primary */
841 int shared_clfe; /* third DAC shared with main or primary */
842 int shared_surr_main; /* secondary DAC sahred with main/DAC0 */
843};
844
845static struct badness_table main_out_badness = {
846 .no_primary_dac = BAD_NO_PRIMARY_DAC,
847 .no_dac = BAD_NO_DAC,
848 .shared_primary = BAD_NO_PRIMARY_DAC,
849 .shared_surr = BAD_SHARED_SURROUND,
850 .shared_clfe = BAD_SHARED_CLFE,
851 .shared_surr_main = BAD_SHARED_SURROUND,
852};
853
854static struct badness_table extra_out_badness = {
855 .no_primary_dac = BAD_NO_DAC,
856 .no_dac = BAD_NO_DAC,
857 .shared_primary = BAD_NO_EXTRA_DAC,
858 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
859 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
860 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
861};
862
863/* try to assign DACs to pins and return the resultant badness */
864static int try_assign_dacs(struct hda_codec *codec, int num_outs,
865 const hda_nid_t *pins, hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +0100866 int *path_idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100867 const struct badness_table *bad)
868{
869 struct hda_gen_spec *spec = codec->spec;
870 struct auto_pin_cfg *cfg = &spec->autocfg;
871 int i, j;
872 int badness = 0;
873 hda_nid_t dac;
874
875 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 return 0;
877
Takashi Iwai352f7f92012-12-19 12:52:06 +0100878 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100879 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100880 hda_nid_t pin = pins[i];
Takashi Iwai1e0b5282013-01-04 12:56:52 +0100881
882 if (dacs[i]) {
883 badness += assign_out_path_ctls(codec, pin, dacs[i]);
884 continue;
885 }
886
887 dacs[i] = look_for_dac(codec, pin, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100888 if (!dacs[i] && !i) {
889 for (j = 1; j < num_outs; j++) {
890 if (is_reachable_path(codec, dacs[j], pin)) {
891 dacs[0] = dacs[j];
892 dacs[j] = 0;
Takashi Iwai196c17662013-01-04 15:01:40 +0100893 path_idx[j] = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100894 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 }
896 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100897 }
898 dac = dacs[i];
899 if (!dac) {
900 if (is_reachable_path(codec, dacs[0], pin))
901 dac = dacs[0];
902 else if (cfg->line_outs > i &&
903 is_reachable_path(codec, spec->private_dac_nids[i], pin))
904 dac = spec->private_dac_nids[i];
905 if (dac) {
906 if (!i)
907 badness += bad->shared_primary;
908 else if (i == 1)
909 badness += bad->shared_surr;
910 else
911 badness += bad->shared_clfe;
912 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
913 dac = spec->private_dac_nids[0];
914 badness += bad->shared_surr_main;
915 } else if (!i)
916 badness += bad->no_primary_dac;
917 else
918 badness += bad->no_dac;
919 }
Takashi Iwai4ac0eef2012-12-20 18:10:51 +0100920 path = snd_hda_add_new_path(codec, dac, pin, HDA_PARSE_NO_AAMIX);
Takashi Iwai117688a2013-01-04 15:41:41 +0100921 if (!path && !i && spec->mixer_nid) {
Takashi Iwaib3a8c742012-12-20 18:29:16 +0100922 /* try with aamix */
923 path = snd_hda_add_new_path(codec, dac, pin, HDA_PARSE_ALL);
924 }
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100925 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100926 dac = dacs[i] = 0;
Takashi Iwaie1284af2013-01-03 16:33:02 +0100927 else {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100928 print_nid_path("output", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +0100929 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +0100930 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwaie1284af2013-01-03 16:33:02 +0100931 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100932 if (dac)
933 badness += assign_out_path_ctls(codec, pin, dac);
934 }
935
936 return badness;
937}
938
939/* return NID if the given pin has only a single connection to a certain DAC */
940static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
941{
942 struct hda_gen_spec *spec = codec->spec;
943 int i;
944 hda_nid_t nid_found = 0;
945
946 for (i = 0; i < spec->num_all_dacs; i++) {
947 hda_nid_t nid = spec->all_dacs[i];
948 if (!nid || is_dac_already_used(codec, nid))
949 continue;
950 if (is_reachable_path(codec, nid, pin)) {
951 if (nid_found)
952 return 0;
953 nid_found = nid;
954 }
955 }
956 return nid_found;
957}
958
959/* check whether the given pin can be a multi-io pin */
960static bool can_be_multiio_pin(struct hda_codec *codec,
961 unsigned int location, hda_nid_t nid)
962{
963 unsigned int defcfg, caps;
964
965 defcfg = snd_hda_codec_get_pincfg(codec, nid);
966 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
967 return false;
968 if (location && get_defcfg_location(defcfg) != location)
969 return false;
970 caps = snd_hda_query_pin_caps(codec, nid);
971 if (!(caps & AC_PINCAP_OUT))
972 return false;
973 return true;
974}
975
Takashi Iwaie22aab72013-01-04 14:50:04 +0100976/* count the number of input pins that are capable to be multi-io */
977static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
978{
979 struct hda_gen_spec *spec = codec->spec;
980 struct auto_pin_cfg *cfg = &spec->autocfg;
981 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
982 unsigned int location = get_defcfg_location(defcfg);
983 int type, i;
984 int num_pins = 0;
985
986 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
987 for (i = 0; i < cfg->num_inputs; i++) {
988 if (cfg->inputs[i].type != type)
989 continue;
990 if (can_be_multiio_pin(codec, location,
991 cfg->inputs[i].pin))
992 num_pins++;
993 }
994 }
995 return num_pins;
996}
997
Takashi Iwai352f7f92012-12-19 12:52:06 +0100998/*
999 * multi-io helper
1000 *
1001 * When hardwired is set, try to fill ony hardwired pins, and returns
1002 * zero if any pins are filled, non-zero if nothing found.
1003 * When hardwired is off, try to fill possible input pins, and returns
1004 * the badness value.
1005 */
1006static int fill_multi_ios(struct hda_codec *codec,
1007 hda_nid_t reference_pin,
Takashi Iwaie22aab72013-01-04 14:50:04 +01001008 bool hardwired)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001009{
1010 struct hda_gen_spec *spec = codec->spec;
1011 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie22aab72013-01-04 14:50:04 +01001012 int type, i, j, num_pins, old_pins;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001013 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1014 unsigned int location = get_defcfg_location(defcfg);
1015 int badness = 0;
1016
1017 old_pins = spec->multi_ios;
1018 if (old_pins >= 2)
1019 goto end_fill;
1020
Takashi Iwaie22aab72013-01-04 14:50:04 +01001021 num_pins = count_multiio_pins(codec, reference_pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001022 if (num_pins < 2)
1023 goto end_fill;
1024
Takashi Iwai352f7f92012-12-19 12:52:06 +01001025 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1026 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001027 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001028 hda_nid_t nid = cfg->inputs[i].pin;
1029 hda_nid_t dac = 0;
1030
1031 if (cfg->inputs[i].type != type)
1032 continue;
1033 if (!can_be_multiio_pin(codec, location, nid))
1034 continue;
1035 for (j = 0; j < spec->multi_ios; j++) {
1036 if (nid == spec->multi_io[j].pin)
1037 break;
1038 }
1039 if (j < spec->multi_ios)
1040 continue;
1041
Takashi Iwai352f7f92012-12-19 12:52:06 +01001042 if (hardwired)
1043 dac = get_dac_if_single(codec, nid);
1044 else if (!dac)
1045 dac = look_for_dac(codec, nid, false);
1046 if (!dac) {
1047 badness++;
1048 continue;
1049 }
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01001050 path = snd_hda_add_new_path(codec, dac, nid, HDA_PARSE_NO_AAMIX);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001051 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001052 badness++;
1053 continue;
1054 }
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001055 print_nid_path("multiio", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001056 spec->multi_io[spec->multi_ios].pin = nid;
1057 spec->multi_io[spec->multi_ios].dac = dac;
Takashi Iwai196c17662013-01-04 15:01:40 +01001058 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1059 snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001060 spec->multi_ios++;
1061 if (spec->multi_ios >= 2)
1062 break;
1063 }
1064 }
1065 end_fill:
1066 if (badness)
1067 badness = BAD_MULTI_IO;
1068 if (old_pins == spec->multi_ios) {
1069 if (hardwired)
1070 return 1; /* nothing found */
1071 else
1072 return badness; /* no badness if nothing found */
1073 }
1074 if (!hardwired && spec->multi_ios < 2) {
1075 /* cancel newly assigned paths */
1076 spec->paths.used -= spec->multi_ios - old_pins;
1077 spec->multi_ios = old_pins;
1078 return badness;
1079 }
1080
1081 /* assign volume and mute controls */
1082 for (i = old_pins; i < spec->multi_ios; i++)
1083 badness += assign_out_path_ctls(codec, spec->multi_io[i].pin,
1084 spec->multi_io[i].dac);
1085
1086 return badness;
1087}
1088
1089/* map DACs for all pins in the list if they are single connections */
1090static bool map_singles(struct hda_codec *codec, int outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001091 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001092{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001093 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001094 int i;
1095 bool found = false;
1096 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001097 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001098 hda_nid_t dac;
1099 if (dacs[i])
1100 continue;
1101 dac = get_dac_if_single(codec, pins[i]);
1102 if (!dac)
1103 continue;
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01001104 path = snd_hda_add_new_path(codec, dac, pins[i], HDA_PARSE_NO_AAMIX);
Takashi Iwai117688a2013-01-04 15:41:41 +01001105 if (!path && !i && spec->mixer_nid)
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001106 path = snd_hda_add_new_path(codec, dac, pins[i], HDA_PARSE_ALL);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001107 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001108 dacs[i] = dac;
1109 found = true;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001110 print_nid_path("output", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001111 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001112 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001113 }
1114 }
1115 return found;
1116}
1117
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001118/* create a new path including aamix if available, and return its index */
1119static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1120{
1121 struct nid_path *path;
1122
1123 path = snd_hda_get_path_from_idx(codec, path_idx);
1124 if (!path || !path->depth || path->with_aa_mix)
1125 return 0;
1126 path = snd_hda_add_new_path(codec, path->path[0],
1127 path->path[path->depth - 1],
1128 HDA_PARSE_ONLY_AAMIX);
1129 if (!path)
1130 return 0;
1131 print_nid_path("output-aamix", path);
1132 path->active = false; /* unused as default */
1133 return snd_hda_get_path_idx(codec, path);
1134}
1135
Takashi Iwai352f7f92012-12-19 12:52:06 +01001136/* fill in the dac_nids table from the parsed pin configuration */
1137static int fill_and_eval_dacs(struct hda_codec *codec,
1138 bool fill_hardwired,
1139 bool fill_mio_first)
1140{
1141 struct hda_gen_spec *spec = codec->spec;
1142 struct auto_pin_cfg *cfg = &spec->autocfg;
1143 int i, err, badness;
1144
1145 /* set num_dacs once to full for look_for_dac() */
1146 spec->multiout.num_dacs = cfg->line_outs;
1147 spec->multiout.dac_nids = spec->private_dac_nids;
1148 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1149 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1150 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1151 spec->multi_ios = 0;
1152 snd_array_free(&spec->paths);
1153 badness = 0;
1154
1155 /* fill hard-wired DACs first */
1156 if (fill_hardwired) {
1157 bool mapped;
1158 do {
1159 mapped = map_singles(codec, cfg->line_outs,
1160 cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001161 spec->private_dac_nids,
1162 spec->out_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001163 mapped |= map_singles(codec, cfg->hp_outs,
1164 cfg->hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001165 spec->multiout.hp_out_nid,
1166 spec->hp_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001167 mapped |= map_singles(codec, cfg->speaker_outs,
1168 cfg->speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001169 spec->multiout.extra_out_nid,
1170 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001171 if (fill_mio_first && cfg->line_outs == 1 &&
1172 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001173 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001174 if (!err)
1175 mapped = true;
1176 }
1177 } while (mapped);
1178 }
1179
1180 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001181 spec->private_dac_nids, spec->out_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001182 &main_out_badness);
1183
1184 /* re-count num_dacs and squash invalid entries */
1185 spec->multiout.num_dacs = 0;
1186 for (i = 0; i < cfg->line_outs; i++) {
1187 if (spec->private_dac_nids[i])
1188 spec->multiout.num_dacs++;
1189 else {
1190 memmove(spec->private_dac_nids + i,
1191 spec->private_dac_nids + i + 1,
1192 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1193 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1194 }
1195 }
1196
1197 if (fill_mio_first &&
1198 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1199 /* try to fill multi-io first */
Takashi Iwaie22aab72013-01-04 14:50:04 +01001200 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001201 if (err < 0)
1202 return err;
1203 /* we don't count badness at this stage yet */
1204 }
1205
1206 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1207 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1208 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001209 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001210 &extra_out_badness);
1211 if (err < 0)
1212 return err;
1213 badness += err;
1214 }
1215 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1216 err = try_assign_dacs(codec, cfg->speaker_outs,
1217 cfg->speaker_pins,
1218 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001219 spec->speaker_paths,
1220 &extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001221 if (err < 0)
1222 return err;
1223 badness += err;
1224 }
1225 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001226 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001227 if (err < 0)
1228 return err;
1229 badness += err;
1230 }
Takashi Iwaie22aab72013-01-04 14:50:04 +01001231
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001232 if (spec->mixer_nid) {
1233 spec->aamix_out_paths[0] =
1234 check_aamix_out_path(codec, spec->out_paths[0]);
1235 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1236 spec->aamix_out_paths[1] =
1237 check_aamix_out_path(codec, spec->hp_paths[0]);
1238 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1239 spec->aamix_out_paths[2] =
1240 check_aamix_out_path(codec, spec->speaker_paths[0]);
1241 }
1242
Takashi Iwaie22aab72013-01-04 14:50:04 +01001243 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1244 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1245 spec->multi_ios = 1; /* give badness */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001246
1247 if (spec->multi_ios == 2) {
1248 for (i = 0; i < 2; i++)
1249 spec->private_dac_nids[spec->multiout.num_dacs++] =
1250 spec->multi_io[i].dac;
1251 spec->ext_channel_count = 2;
1252 } else if (spec->multi_ios) {
1253 spec->multi_ios = 0;
1254 badness += BAD_MULTI_IO;
1255 }
1256
1257 return badness;
1258}
1259
1260#define DEBUG_BADNESS
1261
1262#ifdef DEBUG_BADNESS
1263#define debug_badness snd_printdd
1264#else
1265#define debug_badness(...)
1266#endif
1267
1268static void debug_show_configs(struct hda_gen_spec *spec, struct auto_pin_cfg *cfg)
1269{
1270 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1271 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001272 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001273 spec->multiout.dac_nids[0],
1274 spec->multiout.dac_nids[1],
1275 spec->multiout.dac_nids[2],
1276 spec->multiout.dac_nids[3]);
1277 if (spec->multi_ios > 0)
1278 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1279 spec->multi_ios,
1280 spec->multi_io[0].pin, spec->multi_io[1].pin,
1281 spec->multi_io[0].dac, spec->multi_io[1].dac);
1282 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1283 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001284 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001285 spec->multiout.hp_out_nid[0],
1286 spec->multiout.hp_out_nid[1],
1287 spec->multiout.hp_out_nid[2],
1288 spec->multiout.hp_out_nid[3]);
1289 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1290 cfg->speaker_pins[0], cfg->speaker_pins[1],
1291 cfg->speaker_pins[2], cfg->speaker_pins[3],
1292 spec->multiout.extra_out_nid[0],
1293 spec->multiout.extra_out_nid[1],
1294 spec->multiout.extra_out_nid[2],
1295 spec->multiout.extra_out_nid[3]);
1296}
1297
1298/* find all available DACs of the codec */
1299static void fill_all_dac_nids(struct hda_codec *codec)
1300{
1301 struct hda_gen_spec *spec = codec->spec;
1302 int i;
1303 hda_nid_t nid = codec->start_nid;
1304
1305 spec->num_all_dacs = 0;
1306 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1307 for (i = 0; i < codec->num_nodes; i++, nid++) {
1308 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1309 continue;
1310 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1311 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1312 break;
1313 }
1314 spec->all_dacs[spec->num_all_dacs++] = nid;
1315 }
1316}
1317
1318static int parse_output_paths(struct hda_codec *codec)
1319{
1320 struct hda_gen_spec *spec = codec->spec;
1321 struct auto_pin_cfg *cfg = &spec->autocfg;
1322 struct auto_pin_cfg *best_cfg;
1323 int best_badness = INT_MAX;
1324 int badness;
1325 bool fill_hardwired = true, fill_mio_first = true;
1326 bool best_wired = true, best_mio = true;
1327 bool hp_spk_swapped = false;
1328
1329 fill_all_dac_nids(codec);
1330
1331 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1332 if (!best_cfg)
1333 return -ENOMEM;
1334 *best_cfg = *cfg;
1335
1336 for (;;) {
1337 badness = fill_and_eval_dacs(codec, fill_hardwired,
1338 fill_mio_first);
1339 if (badness < 0) {
1340 kfree(best_cfg);
1341 return badness;
1342 }
1343 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1344 cfg->line_out_type, fill_hardwired, fill_mio_first,
1345 badness);
1346 debug_show_configs(spec, cfg);
1347 if (badness < best_badness) {
1348 best_badness = badness;
1349 *best_cfg = *cfg;
1350 best_wired = fill_hardwired;
1351 best_mio = fill_mio_first;
1352 }
1353 if (!badness)
1354 break;
1355 fill_mio_first = !fill_mio_first;
1356 if (!fill_mio_first)
1357 continue;
1358 fill_hardwired = !fill_hardwired;
1359 if (!fill_hardwired)
1360 continue;
1361 if (hp_spk_swapped)
1362 break;
1363 hp_spk_swapped = true;
1364 if (cfg->speaker_outs > 0 &&
1365 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1366 cfg->hp_outs = cfg->line_outs;
1367 memcpy(cfg->hp_pins, cfg->line_out_pins,
1368 sizeof(cfg->hp_pins));
1369 cfg->line_outs = cfg->speaker_outs;
1370 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1371 sizeof(cfg->speaker_pins));
1372 cfg->speaker_outs = 0;
1373 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1374 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1375 fill_hardwired = true;
1376 continue;
1377 }
1378 if (cfg->hp_outs > 0 &&
1379 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1380 cfg->speaker_outs = cfg->line_outs;
1381 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1382 sizeof(cfg->speaker_pins));
1383 cfg->line_outs = cfg->hp_outs;
1384 memcpy(cfg->line_out_pins, cfg->hp_pins,
1385 sizeof(cfg->hp_pins));
1386 cfg->hp_outs = 0;
1387 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1388 cfg->line_out_type = AUTO_PIN_HP_OUT;
1389 fill_hardwired = true;
1390 continue;
1391 }
1392 break;
1393 }
1394
1395 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001396 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001397 *cfg = *best_cfg;
1398 fill_and_eval_dacs(codec, best_wired, best_mio);
1399 }
1400 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1401 cfg->line_out_type, best_wired, best_mio);
1402 debug_show_configs(spec, cfg);
1403
1404 if (cfg->line_out_pins[0]) {
1405 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01001406 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001407 if (path)
1408 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
1409 }
1410
1411 kfree(best_cfg);
1412 return 0;
1413}
1414
1415/* add playback controls from the parsed DAC table */
1416static int create_multi_out_ctls(struct hda_codec *codec,
1417 const struct auto_pin_cfg *cfg)
1418{
1419 struct hda_gen_spec *spec = codec->spec;
1420 int i, err, noutputs;
1421
1422 noutputs = cfg->line_outs;
1423 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1424 noutputs += spec->multi_ios;
1425
1426 for (i = 0; i < noutputs; i++) {
1427 const char *name;
1428 int index;
Takashi Iwai196c17662013-01-04 15:01:40 +01001429 hda_nid_t dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001430 struct nid_path *path;
1431
1432 dac = spec->multiout.dac_nids[i];
1433 if (!dac)
1434 continue;
1435 if (i >= cfg->line_outs) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001436 index = 0;
1437 name = channel_name[i];
1438 } else {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001439 name = get_line_out_pfx(spec, i, true, &index);
1440 }
1441
Takashi Iwai196c17662013-01-04 15:01:40 +01001442 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001443 if (!path)
1444 continue;
1445 if (!name || !strcmp(name, "CLFE")) {
1446 /* Center/LFE */
1447 err = add_vol_ctl(codec, "Center", 0, 1, path);
1448 if (err < 0)
1449 return err;
1450 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1451 if (err < 0)
1452 return err;
1453 err = add_sw_ctl(codec, "Center", 0, 1, path);
1454 if (err < 0)
1455 return err;
1456 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1457 if (err < 0)
1458 return err;
1459 } else {
1460 err = add_stereo_vol(codec, name, index, path);
1461 if (err < 0)
1462 return err;
1463 err = add_stereo_sw(codec, name, index, path);
1464 if (err < 0)
1465 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 }
1467 }
1468 return 0;
1469}
1470
Takashi Iwai352f7f92012-12-19 12:52:06 +01001471static int create_extra_out(struct hda_codec *codec, hda_nid_t pin,
Takashi Iwai196c17662013-01-04 15:01:40 +01001472 hda_nid_t dac, int path_idx,
1473 const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001475 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 int err;
1477
Takashi Iwai196c17662013-01-04 15:01:40 +01001478 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001479 if (!path)
1480 return 0;
1481 /* bind volume control will be created in the case of dac = 0 */
1482 if (dac) {
1483 err = add_stereo_vol(codec, pfx, cidx, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 if (err < 0)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001485 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001487 err = add_stereo_sw(codec, pfx, cidx, path);
1488 if (err < 0)
1489 return err;
1490 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491}
1492
Takashi Iwai352f7f92012-12-19 12:52:06 +01001493/* add playback controls for speaker and HP outputs */
1494static int create_extra_outs(struct hda_codec *codec, int num_pins,
1495 const hda_nid_t *pins, const hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001496 const int *paths, const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001498 struct hda_gen_spec *spec = codec->spec;
1499 struct hda_bind_ctls *ctl;
1500 char name[32];
1501 int i, n, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
Takashi Iwai352f7f92012-12-19 12:52:06 +01001503 if (!num_pins || !pins[0])
1504 return 0;
1505
1506 if (num_pins == 1) {
1507 hda_nid_t dac = *dacs;
1508 if (!dac)
1509 dac = spec->multiout.dac_nids[0];
Takashi Iwai196c17662013-01-04 15:01:40 +01001510 return create_extra_out(codec, *pins, dac, paths[0], pfx, 0);
Takashi Iwai97ec5582006-03-21 11:29:07 +01001511 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001512
1513 for (i = 0; i < num_pins; i++) {
1514 hda_nid_t dac;
1515 if (dacs[num_pins - 1])
1516 dac = dacs[i]; /* with individual volumes */
Takashi Iwai97ec5582006-03-21 11:29:07 +01001517 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01001518 dac = 0;
1519 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker")) {
Takashi Iwai196c17662013-01-04 15:01:40 +01001520 err = create_extra_out(codec, pins[i], dac, paths[i],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001521 "Bass Speaker", 0);
1522 } else if (num_pins >= 3) {
1523 snprintf(name, sizeof(name), "%s %s",
1524 pfx, channel_name[i]);
Takashi Iwai196c17662013-01-04 15:01:40 +01001525 err = create_extra_out(codec, pins[i], dac, paths[i],
1526 name, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001527 } else {
Takashi Iwai196c17662013-01-04 15:01:40 +01001528 err = create_extra_out(codec, pins[i], dac, paths[i],
1529 pfx, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001531 if (err < 0)
1532 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001534 if (dacs[num_pins - 1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 return 0;
1536
Takashi Iwai352f7f92012-12-19 12:52:06 +01001537 /* Let's create a bind-controls for volumes */
1538 ctl = new_bind_ctl(codec, num_pins, &snd_hda_bind_vol);
1539 if (!ctl)
1540 return -ENOMEM;
1541 n = 0;
1542 for (i = 0; i < num_pins; i++) {
1543 hda_nid_t vol;
1544 struct nid_path *path;
1545 if (!pins[i] || !dacs[i])
1546 continue;
Takashi Iwai196c17662013-01-04 15:01:40 +01001547 path = snd_hda_get_path_from_idx(codec, paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001548 if (!path)
1549 continue;
1550 vol = look_for_out_vol_nid(codec, path);
1551 if (vol)
1552 ctl->values[n++] =
1553 HDA_COMPOSE_AMP_VAL(vol, 3, 0, HDA_OUTPUT);
1554 }
1555 if (n) {
1556 snprintf(name, sizeof(name), "%s Playback Volume", pfx);
1557 err = add_control(spec, HDA_CTL_BIND_VOL, name, 0, (long)ctl);
1558 if (err < 0)
1559 return err;
1560 }
1561 return 0;
1562}
Takashi Iwai97ec5582006-03-21 11:29:07 +01001563
Takashi Iwai352f7f92012-12-19 12:52:06 +01001564static int create_hp_out_ctls(struct hda_codec *codec)
1565{
1566 struct hda_gen_spec *spec = codec->spec;
1567 return create_extra_outs(codec, spec->autocfg.hp_outs,
1568 spec->autocfg.hp_pins,
1569 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001570 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001571 "Headphone");
1572}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
Takashi Iwai352f7f92012-12-19 12:52:06 +01001574static int create_speaker_out_ctls(struct hda_codec *codec)
1575{
1576 struct hda_gen_spec *spec = codec->spec;
1577 return create_extra_outs(codec, spec->autocfg.speaker_outs,
1578 spec->autocfg.speaker_pins,
1579 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001580 spec->speaker_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001581 "Speaker");
1582}
1583
1584/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001585 * independent HP controls
1586 */
1587
1588static int indep_hp_info(struct snd_kcontrol *kcontrol,
1589 struct snd_ctl_elem_info *uinfo)
1590{
1591 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
1592}
1593
1594static int indep_hp_get(struct snd_kcontrol *kcontrol,
1595 struct snd_ctl_elem_value *ucontrol)
1596{
1597 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1598 struct hda_gen_spec *spec = codec->spec;
1599 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
1600 return 0;
1601}
1602
1603static int indep_hp_put(struct snd_kcontrol *kcontrol,
1604 struct snd_ctl_elem_value *ucontrol)
1605{
1606 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1607 struct hda_gen_spec *spec = codec->spec;
1608 unsigned int select = ucontrol->value.enumerated.item[0];
1609 int ret = 0;
1610
1611 mutex_lock(&spec->pcm_mutex);
1612 if (spec->active_streams) {
1613 ret = -EBUSY;
1614 goto unlock;
1615 }
1616
1617 if (spec->indep_hp_enabled != select) {
1618 spec->indep_hp_enabled = select;
1619 if (spec->indep_hp_enabled)
1620 spec->multiout.hp_out_nid[0] = 0;
1621 else
1622 spec->multiout.hp_out_nid[0] = spec->alt_dac_nid;
1623 ret = 1;
1624 }
1625 unlock:
1626 mutex_unlock(&spec->pcm_mutex);
1627 return ret;
1628}
1629
1630static const struct snd_kcontrol_new indep_hp_ctl = {
1631 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1632 .name = "Independent HP",
1633 .info = indep_hp_info,
1634 .get = indep_hp_get,
1635 .put = indep_hp_put,
1636};
1637
1638
1639static int create_indep_hp_ctls(struct hda_codec *codec)
1640{
1641 struct hda_gen_spec *spec = codec->spec;
1642
1643 if (!spec->indep_hp)
1644 return 0;
1645 if (!spec->multiout.hp_out_nid[0]) {
1646 spec->indep_hp = 0;
1647 return 0;
1648 }
1649
1650 spec->indep_hp_enabled = false;
1651 spec->alt_dac_nid = spec->multiout.hp_out_nid[0];
1652 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
1653 return -ENOMEM;
1654 return 0;
1655}
1656
1657/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01001658 * channel mode enum control
1659 */
1660
1661static int ch_mode_info(struct snd_kcontrol *kcontrol,
1662 struct snd_ctl_elem_info *uinfo)
1663{
1664 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1665 struct hda_gen_spec *spec = codec->spec;
1666
1667 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1668 uinfo->count = 1;
1669 uinfo->value.enumerated.items = spec->multi_ios + 1;
1670 if (uinfo->value.enumerated.item > spec->multi_ios)
1671 uinfo->value.enumerated.item = spec->multi_ios;
1672 sprintf(uinfo->value.enumerated.name, "%dch",
1673 (uinfo->value.enumerated.item + 1) * 2);
1674 return 0;
1675}
1676
1677static int ch_mode_get(struct snd_kcontrol *kcontrol,
1678 struct snd_ctl_elem_value *ucontrol)
1679{
1680 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1681 struct hda_gen_spec *spec = codec->spec;
1682 ucontrol->value.enumerated.item[0] = (spec->ext_channel_count - 1) / 2;
1683 return 0;
1684}
1685
Takashi Iwai196c17662013-01-04 15:01:40 +01001686static inline struct nid_path *
1687get_multiio_path(struct hda_codec *codec, int idx)
1688{
1689 struct hda_gen_spec *spec = codec->spec;
1690 return snd_hda_get_path_from_idx(codec,
1691 spec->out_paths[spec->autocfg.line_outs + idx]);
1692}
1693
Takashi Iwai352f7f92012-12-19 12:52:06 +01001694static int set_multi_io(struct hda_codec *codec, int idx, bool output)
1695{
1696 struct hda_gen_spec *spec = codec->spec;
1697 hda_nid_t nid = spec->multi_io[idx].pin;
1698 struct nid_path *path;
1699
Takashi Iwai196c17662013-01-04 15:01:40 +01001700 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001701 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001703
1704 if (path->active == output)
1705 return 0;
1706
1707 if (output) {
1708 snd_hda_set_pin_ctl_cache(codec, nid, PIN_OUT);
1709 snd_hda_activate_path(codec, path, true, true);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01001710 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001711 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01001712 set_pin_eapd(codec, nid, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001713 snd_hda_activate_path(codec, path, false, true);
1714 snd_hda_set_pin_ctl_cache(codec, nid,
1715 spec->multi_io[idx].ctl_in);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001717 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718}
1719
Takashi Iwai352f7f92012-12-19 12:52:06 +01001720static int ch_mode_put(struct snd_kcontrol *kcontrol,
1721 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001723 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1724 struct hda_gen_spec *spec = codec->spec;
1725 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
Takashi Iwai352f7f92012-12-19 12:52:06 +01001727 ch = ucontrol->value.enumerated.item[0];
1728 if (ch < 0 || ch > spec->multi_ios)
1729 return -EINVAL;
1730 if (ch == (spec->ext_channel_count - 1) / 2)
1731 return 0;
1732 spec->ext_channel_count = (ch + 1) * 2;
1733 for (i = 0; i < spec->multi_ios; i++)
1734 set_multi_io(codec, i, i < ch);
1735 spec->multiout.max_channels = max(spec->ext_channel_count,
1736 spec->const_channel_count);
1737 if (spec->need_dac_fix)
1738 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 return 1;
1740}
1741
Takashi Iwai352f7f92012-12-19 12:52:06 +01001742static const struct snd_kcontrol_new channel_mode_enum = {
1743 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1744 .name = "Channel Mode",
1745 .info = ch_mode_info,
1746 .get = ch_mode_get,
1747 .put = ch_mode_put,
1748};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
Takashi Iwai352f7f92012-12-19 12:52:06 +01001750static int create_multi_channel_mode(struct hda_codec *codec)
1751{
1752 struct hda_gen_spec *spec = codec->spec;
1753
1754 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01001755 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01001756 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 return 0;
1759}
1760
Takashi Iwai352f7f92012-12-19 12:52:06 +01001761/*
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001762 * aamix loopback enable/disable switch
1763 */
1764
1765#define loopback_mixing_info indep_hp_info
1766
1767static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
1768 struct snd_ctl_elem_value *ucontrol)
1769{
1770 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1771 struct hda_gen_spec *spec = codec->spec;
1772 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
1773 return 0;
1774}
1775
1776static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
1777 int nomix_path_idx, int mix_path_idx)
1778{
1779 struct nid_path *nomix_path, *mix_path;
1780
1781 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
1782 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
1783 if (!nomix_path || !mix_path)
1784 return;
1785 if (do_mix) {
1786 snd_hda_activate_path(codec, nomix_path, false, true);
1787 snd_hda_activate_path(codec, mix_path, true, true);
1788 } else {
1789 snd_hda_activate_path(codec, mix_path, false, true);
1790 snd_hda_activate_path(codec, nomix_path, true, true);
1791 }
1792}
1793
1794static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
1795 struct snd_ctl_elem_value *ucontrol)
1796{
1797 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1798 struct hda_gen_spec *spec = codec->spec;
1799 unsigned int val = ucontrol->value.enumerated.item[0];
1800
1801 if (val == spec->aamix_mode)
1802 return 0;
1803 spec->aamix_mode = val;
1804 update_aamix_paths(codec, val, spec->out_paths[0],
1805 spec->aamix_out_paths[0]);
1806 update_aamix_paths(codec, val, spec->hp_paths[0],
1807 spec->aamix_out_paths[1]);
1808 update_aamix_paths(codec, val, spec->speaker_paths[0],
1809 spec->aamix_out_paths[2]);
1810 return 1;
1811}
1812
1813static const struct snd_kcontrol_new loopback_mixing_enum = {
1814 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1815 .name = "Loopback Mixing",
1816 .info = loopback_mixing_info,
1817 .get = loopback_mixing_get,
1818 .put = loopback_mixing_put,
1819};
1820
1821static int create_loopback_mixing_ctl(struct hda_codec *codec)
1822{
1823 struct hda_gen_spec *spec = codec->spec;
1824
1825 if (!spec->mixer_nid)
1826 return 0;
1827 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
1828 spec->aamix_out_paths[2]))
1829 return 0;
1830 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
1831 return -ENOMEM;
1832 return 0;
1833}
1834
1835/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01001836 * shared headphone/mic handling
1837 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02001838
Takashi Iwai352f7f92012-12-19 12:52:06 +01001839static void call_update_outputs(struct hda_codec *codec);
1840
1841/* for shared I/O, change the pin-control accordingly */
1842static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic)
1843{
1844 struct hda_gen_spec *spec = codec->spec;
1845 unsigned int val;
1846 hda_nid_t pin = spec->autocfg.inputs[1].pin;
1847 /* NOTE: this assumes that there are only two inputs, the
1848 * first is the real internal mic and the second is HP/mic jack.
1849 */
1850
1851 val = snd_hda_get_default_vref(codec, pin);
1852
1853 /* This pin does not have vref caps - let's enable vref on pin 0x18
1854 instead, as suggested by Realtek */
1855 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
1856 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
1857 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
1858 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01001859 snd_hda_set_pin_ctl_cache(codec, vref_pin,
1860 PIN_IN | (set_as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02001861 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001862
1863 val = set_as_mic ? val | PIN_IN : PIN_HP;
Takashi Iwai7594aa32012-12-20 15:38:40 +01001864 snd_hda_set_pin_ctl_cache(codec, pin, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001865
1866 spec->automute_speaker = !set_as_mic;
1867 call_update_outputs(codec);
1868}
1869
1870/* create a shared input with the headphone out */
1871static int create_shared_input(struct hda_codec *codec)
1872{
1873 struct hda_gen_spec *spec = codec->spec;
1874 struct auto_pin_cfg *cfg = &spec->autocfg;
1875 unsigned int defcfg;
1876 hda_nid_t nid;
1877
1878 /* only one internal input pin? */
1879 if (cfg->num_inputs != 1)
1880 return 0;
1881 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
1882 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
1883 return 0;
1884
1885 if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1886 nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
1887 else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
1888 nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
1889 else
1890 return 0; /* both not available */
1891
1892 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
1893 return 0; /* no input */
1894
1895 cfg->inputs[1].pin = nid;
1896 cfg->inputs[1].type = AUTO_PIN_MIC;
1897 cfg->num_inputs = 2;
1898 spec->shared_mic_hp = 1;
1899 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
1900 return 0;
1901}
1902
1903
1904/*
1905 * Parse input paths
1906 */
1907
1908#ifdef CONFIG_PM
1909/* add the powersave loopback-list entry */
1910static void add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
1911{
1912 struct hda_amp_list *list;
1913
1914 if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
1915 return;
1916 list = spec->loopback_list + spec->num_loopbacks;
1917 list->nid = mix;
1918 list->dir = HDA_INPUT;
1919 list->idx = idx;
1920 spec->num_loopbacks++;
Takashi Iwaicb53c622007-08-10 17:21:45 +02001921 spec->loopback.amplist = spec->loopback_list;
1922}
1923#else
Takashi Iwai352f7f92012-12-19 12:52:06 +01001924#define add_loopback_list(spec, mix, idx) /* NOP */
Takashi Iwaicb53c622007-08-10 17:21:45 +02001925#endif
1926
Takashi Iwai352f7f92012-12-19 12:52:06 +01001927/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01001928static int new_analog_input(struct hda_codec *codec, int input_idx,
1929 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001930 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001932 struct hda_gen_spec *spec = codec->spec;
1933 struct nid_path *path;
1934 unsigned int val;
1935 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
Takashi Iwai352f7f92012-12-19 12:52:06 +01001937 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
1938 !nid_has_mute(codec, mix_nid, HDA_INPUT))
1939 return 0; /* no need for analog loopback */
1940
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01001941 path = snd_hda_add_new_path(codec, pin, mix_nid, HDA_PARSE_ALL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001942 if (!path)
1943 return -EINVAL;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001944 print_nid_path("loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01001945 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001946
1947 idx = path->idx[path->depth - 1];
1948 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
1949 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1950 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001951 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001953 path->ctls[NID_PATH_VOL_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 }
1955
Takashi Iwai352f7f92012-12-19 12:52:06 +01001956 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
1957 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1958 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001959 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001961 path->ctls[NID_PATH_MUTE_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 }
1963
Takashi Iwai352f7f92012-12-19 12:52:06 +01001964 path->active = true;
1965 add_loopback_list(spec, mix_nid, idx);
1966 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967}
1968
Takashi Iwai352f7f92012-12-19 12:52:06 +01001969static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001971 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
1972 return (pincap & AC_PINCAP_IN) != 0;
1973}
1974
1975/* Parse the codec tree and retrieve ADCs */
1976static int fill_adc_nids(struct hda_codec *codec)
1977{
1978 struct hda_gen_spec *spec = codec->spec;
1979 hda_nid_t nid;
1980 hda_nid_t *adc_nids = spec->adc_nids;
1981 int max_nums = ARRAY_SIZE(spec->adc_nids);
1982 int i, nums = 0;
1983
1984 nid = codec->start_nid;
1985 for (i = 0; i < codec->num_nodes; i++, nid++) {
1986 unsigned int caps = get_wcaps(codec, nid);
1987 int type = get_wcaps_type(caps);
1988
1989 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
1990 continue;
1991 adc_nids[nums] = nid;
1992 if (++nums >= max_nums)
1993 break;
1994 }
1995 spec->num_adc_nids = nums;
1996 return nums;
1997}
1998
1999/* filter out invalid adc_nids that don't give all active input pins;
2000 * if needed, check whether dynamic ADC-switching is available
2001 */
2002static int check_dyn_adc_switch(struct hda_codec *codec)
2003{
2004 struct hda_gen_spec *spec = codec->spec;
2005 struct hda_input_mux *imux = &spec->input_mux;
2006 hda_nid_t adc_nids[ARRAY_SIZE(spec->adc_nids)];
2007 int i, n, nums;
2008 hda_nid_t pin, adc;
2009
2010 again:
2011 nums = 0;
2012 for (n = 0; n < spec->num_adc_nids; n++) {
2013 adc = spec->adc_nids[n];
2014 for (i = 0; i < imux->num_items; i++) {
2015 pin = spec->imux_pins[i];
2016 if (!is_reachable_path(codec, pin, adc))
2017 break;
2018 }
2019 if (i >= imux->num_items)
2020 adc_nids[nums++] = adc;
2021 }
2022
2023 if (!nums) {
2024 if (spec->shared_mic_hp) {
2025 spec->shared_mic_hp = 0;
2026 imux->num_items = 1;
2027 goto again;
2028 }
2029
2030 /* check whether ADC-switch is possible */
2031 for (i = 0; i < imux->num_items; i++) {
2032 pin = spec->imux_pins[i];
2033 for (n = 0; n < spec->num_adc_nids; n++) {
2034 adc = spec->adc_nids[n];
2035 if (is_reachable_path(codec, pin, adc)) {
2036 spec->dyn_adc_idx[i] = n;
2037 break;
2038 }
2039 }
2040 }
2041
2042 snd_printdd("hda-codec: enabling ADC switching\n");
2043 spec->dyn_adc_switch = 1;
2044 } else if (nums != spec->num_adc_nids) {
2045 memcpy(spec->adc_nids, adc_nids, nums * sizeof(hda_nid_t));
2046 spec->num_adc_nids = nums;
2047 }
2048
2049 if (imux->num_items == 1 || spec->shared_mic_hp) {
2050 snd_printdd("hda-codec: reducing to a single ADC\n");
2051 spec->num_adc_nids = 1; /* reduce to a single ADC */
2052 }
2053
2054 /* single index for individual volumes ctls */
2055 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
2056 spec->num_adc_nids = 1;
2057
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 return 0;
2059}
2060
2061/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002062 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002064static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002065{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002066 struct hda_gen_spec *spec = codec->spec;
2067 const struct auto_pin_cfg *cfg = &spec->autocfg;
2068 hda_nid_t mixer = spec->mixer_nid;
2069 struct hda_input_mux *imux = &spec->input_mux;
2070 int num_adcs;
2071 int i, c, err, type_idx = 0;
2072 const char *prev_label = NULL;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002073
Takashi Iwai352f7f92012-12-19 12:52:06 +01002074 num_adcs = fill_adc_nids(codec);
2075 if (num_adcs < 0)
2076 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002077
Takashi Iwai352f7f92012-12-19 12:52:06 +01002078 for (i = 0; i < cfg->num_inputs; i++) {
2079 hda_nid_t pin;
2080 const char *label;
2081 bool imux_added;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
Takashi Iwai352f7f92012-12-19 12:52:06 +01002083 pin = cfg->inputs[i].pin;
2084 if (!is_input_pin(codec, pin))
2085 continue;
2086
2087 label = hda_get_autocfg_input_label(codec, cfg, i);
2088 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
2089 label = "Headphone Mic";
2090 if (prev_label && !strcmp(label, prev_label))
2091 type_idx++;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002092 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01002093 type_idx = 0;
2094 prev_label = label;
2095
2096 if (mixer) {
2097 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01002098 err = new_analog_input(codec, i, pin,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002099 label, type_idx, mixer);
2100 if (err < 0)
2101 return err;
2102 }
2103 }
2104
2105 imux_added = false;
2106 for (c = 0; c < num_adcs; c++) {
2107 struct nid_path *path;
2108 hda_nid_t adc = spec->adc_nids[c];
2109
2110 if (!is_reachable_path(codec, pin, adc))
2111 continue;
2112 path = snd_array_new(&spec->paths);
2113 if (!path)
2114 return -ENOMEM;
2115 memset(path, 0, sizeof(*path));
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01002116 if (!snd_hda_parse_nid_path(codec, pin, adc, HDA_PARSE_ALL, path)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002117 snd_printd(KERN_ERR
2118 "invalid input path 0x%x -> 0x%x\n",
2119 pin, adc);
2120 spec->paths.used--;
2121 continue;
2122 }
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002123 print_nid_path("input", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002124
2125 if (!imux_added) {
2126 spec->imux_pins[imux->num_items] = pin;
2127 snd_hda_add_imux_item(imux, label,
2128 imux->num_items, NULL);
2129 imux_added = true;
2130 }
2131 }
2132 }
2133
2134 return 0;
2135}
2136
2137
2138/*
2139 * input source mux
2140 */
2141
2142/* get the ADC NID corresponding to the given index */
2143static hda_nid_t get_adc_nid(struct hda_codec *codec, int adc_idx, int imux_idx)
2144{
2145 struct hda_gen_spec *spec = codec->spec;
2146 if (spec->dyn_adc_switch)
2147 adc_idx = spec->dyn_adc_idx[imux_idx];
2148 return spec->adc_nids[adc_idx];
2149}
2150
2151static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2152 unsigned int idx);
2153
2154static int mux_enum_info(struct snd_kcontrol *kcontrol,
2155 struct snd_ctl_elem_info *uinfo)
2156{
2157 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2158 struct hda_gen_spec *spec = codec->spec;
2159 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
2160}
2161
2162static int mux_enum_get(struct snd_kcontrol *kcontrol,
2163 struct snd_ctl_elem_value *ucontrol)
2164{
2165 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2166 struct hda_gen_spec *spec = codec->spec;
2167 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2168
2169 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
2170 return 0;
2171}
2172
2173static int mux_enum_put(struct snd_kcontrol *kcontrol,
2174 struct snd_ctl_elem_value *ucontrol)
2175{
2176 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2177 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2178 return mux_select(codec, adc_idx,
2179 ucontrol->value.enumerated.item[0]);
2180}
2181
Takashi Iwai352f7f92012-12-19 12:52:06 +01002182static const struct snd_kcontrol_new cap_src_temp = {
2183 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2184 .name = "Input Source",
2185 .info = mux_enum_info,
2186 .get = mux_enum_get,
2187 .put = mux_enum_put,
2188};
2189
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002190/*
2191 * capture volume and capture switch ctls
2192 */
2193
Takashi Iwai352f7f92012-12-19 12:52:06 +01002194typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
2195 struct snd_ctl_elem_value *ucontrol);
2196
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002197/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002198static int cap_put_caller(struct snd_kcontrol *kcontrol,
2199 struct snd_ctl_elem_value *ucontrol,
2200 put_call_t func, int type)
2201{
2202 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2203 struct hda_gen_spec *spec = codec->spec;
2204 const struct hda_input_mux *imux;
2205 struct nid_path *path;
2206 int i, adc_idx, err = 0;
2207
2208 imux = &spec->input_mux;
2209 adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2210 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002211 /* we use the cache-only update at first since multiple input paths
2212 * may shared the same amp; by updating only caches, the redundant
2213 * writes to hardware can be reduced.
2214 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002215 codec->cached_write = 1;
2216 for (i = 0; i < imux->num_items; i++) {
2217 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2218 get_adc_nid(codec, adc_idx, i));
2219 if (!path->ctls[type])
2220 continue;
2221 kcontrol->private_value = path->ctls[type];
2222 err = func(kcontrol, ucontrol);
2223 if (err < 0)
2224 goto error;
2225 }
2226 error:
2227 codec->cached_write = 0;
2228 mutex_unlock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002229 snd_hda_codec_flush_amp_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002230 if (err >= 0 && spec->cap_sync_hook)
2231 spec->cap_sync_hook(codec);
2232 return err;
2233}
2234
2235/* capture volume ctl callbacks */
2236#define cap_vol_info snd_hda_mixer_amp_volume_info
2237#define cap_vol_get snd_hda_mixer_amp_volume_get
2238#define cap_vol_tlv snd_hda_mixer_amp_tlv
2239
2240static int cap_vol_put(struct snd_kcontrol *kcontrol,
2241 struct snd_ctl_elem_value *ucontrol)
2242{
2243 return cap_put_caller(kcontrol, ucontrol,
2244 snd_hda_mixer_amp_volume_put,
2245 NID_PATH_VOL_CTL);
2246}
2247
2248static const struct snd_kcontrol_new cap_vol_temp = {
2249 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2250 .name = "Capture Volume",
2251 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
2252 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2253 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
2254 .info = cap_vol_info,
2255 .get = cap_vol_get,
2256 .put = cap_vol_put,
2257 .tlv = { .c = cap_vol_tlv },
2258};
2259
2260/* capture switch ctl callbacks */
2261#define cap_sw_info snd_ctl_boolean_stereo_info
2262#define cap_sw_get snd_hda_mixer_amp_switch_get
2263
2264static int cap_sw_put(struct snd_kcontrol *kcontrol,
2265 struct snd_ctl_elem_value *ucontrol)
2266{
2267 return cap_put_caller(kcontrol, ucontrol,
2268 snd_hda_mixer_amp_switch_put,
2269 NID_PATH_MUTE_CTL);
2270}
2271
2272static const struct snd_kcontrol_new cap_sw_temp = {
2273 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2274 .name = "Capture Switch",
2275 .info = cap_sw_info,
2276 .get = cap_sw_get,
2277 .put = cap_sw_put,
2278};
2279
2280static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
2281{
2282 hda_nid_t nid;
2283 int i, depth;
2284
2285 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
2286 for (depth = 0; depth < 3; depth++) {
2287 if (depth >= path->depth)
2288 return -EINVAL;
2289 i = path->depth - depth - 1;
2290 nid = path->path[i];
2291 if (!path->ctls[NID_PATH_VOL_CTL]) {
2292 if (nid_has_volume(codec, nid, HDA_OUTPUT))
2293 path->ctls[NID_PATH_VOL_CTL] =
2294 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2295 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
2296 int idx = path->idx[i];
2297 if (!depth && codec->single_adc_amp)
2298 idx = 0;
2299 path->ctls[NID_PATH_VOL_CTL] =
2300 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2301 }
2302 }
2303 if (!path->ctls[NID_PATH_MUTE_CTL]) {
2304 if (nid_has_mute(codec, nid, HDA_OUTPUT))
2305 path->ctls[NID_PATH_MUTE_CTL] =
2306 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2307 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
2308 int idx = path->idx[i];
2309 if (!depth && codec->single_adc_amp)
2310 idx = 0;
2311 path->ctls[NID_PATH_MUTE_CTL] =
2312 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2313 }
2314 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01002315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 return 0;
2317}
2318
Takashi Iwai352f7f92012-12-19 12:52:06 +01002319static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002321 struct hda_gen_spec *spec = codec->spec;
2322 struct auto_pin_cfg *cfg = &spec->autocfg;
2323 unsigned int val;
2324 int i;
2325
2326 if (!spec->inv_dmic_split)
2327 return false;
2328 for (i = 0; i < cfg->num_inputs; i++) {
2329 if (cfg->inputs[i].pin != nid)
2330 continue;
2331 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2332 return false;
2333 val = snd_hda_codec_get_pincfg(codec, nid);
2334 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
2335 }
2336 return false;
2337}
2338
2339static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
2340 int idx, bool is_switch, unsigned int ctl,
2341 bool inv_dmic)
2342{
2343 struct hda_gen_spec *spec = codec->spec;
2344 char tmpname[44];
2345 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
2346 const char *sfx = is_switch ? "Switch" : "Volume";
2347 unsigned int chs = inv_dmic ? 1 : 3;
2348 int err;
2349
2350 if (!ctl)
2351 return 0;
2352
2353 if (label)
2354 snprintf(tmpname, sizeof(tmpname),
2355 "%s Capture %s", label, sfx);
2356 else
2357 snprintf(tmpname, sizeof(tmpname),
2358 "Capture %s", sfx);
2359 err = add_control(spec, type, tmpname, idx,
2360 amp_val_replace_channels(ctl, chs));
2361 if (err < 0 || !inv_dmic)
2362 return err;
2363
2364 /* Make independent right kcontrol */
2365 if (label)
2366 snprintf(tmpname, sizeof(tmpname),
2367 "Inverted %s Capture %s", label, sfx);
2368 else
2369 snprintf(tmpname, sizeof(tmpname),
2370 "Inverted Capture %s", sfx);
2371 return add_control(spec, type, tmpname, idx,
2372 amp_val_replace_channels(ctl, 2));
2373}
2374
2375/* create single (and simple) capture volume and switch controls */
2376static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
2377 unsigned int vol_ctl, unsigned int sw_ctl,
2378 bool inv_dmic)
2379{
2380 int err;
2381 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
2382 if (err < 0)
2383 return err;
2384 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
2385 if (err < 0)
2386 return err;
2387 return 0;
2388}
2389
2390/* create bound capture volume and switch controls */
2391static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
2392 unsigned int vol_ctl, unsigned int sw_ctl)
2393{
2394 struct hda_gen_spec *spec = codec->spec;
2395 struct snd_kcontrol_new *knew;
2396
2397 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002398 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002399 if (!knew)
2400 return -ENOMEM;
2401 knew->index = idx;
2402 knew->private_value = vol_ctl;
2403 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2404 }
2405 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002406 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002407 if (!knew)
2408 return -ENOMEM;
2409 knew->index = idx;
2410 knew->private_value = sw_ctl;
2411 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2412 }
2413 return 0;
2414}
2415
2416/* return the vol ctl when used first in the imux list */
2417static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
2418{
2419 struct hda_gen_spec *spec = codec->spec;
2420 struct nid_path *path;
2421 unsigned int ctl;
2422 int i;
2423
2424 path = snd_hda_get_nid_path(codec, spec->imux_pins[idx],
2425 get_adc_nid(codec, 0, idx));
2426 if (!path)
2427 return 0;
2428 ctl = path->ctls[type];
2429 if (!ctl)
2430 return 0;
2431 for (i = 0; i < idx - 1; i++) {
2432 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2433 get_adc_nid(codec, 0, i));
2434 if (path && path->ctls[type] == ctl)
2435 return 0;
2436 }
2437 return ctl;
2438}
2439
2440/* create individual capture volume and switch controls per input */
2441static int create_multi_cap_vol_ctl(struct hda_codec *codec)
2442{
2443 struct hda_gen_spec *spec = codec->spec;
2444 struct hda_input_mux *imux = &spec->input_mux;
2445 int i, err, type, type_idx = 0;
2446 const char *prev_label = NULL;
2447
2448 for (i = 0; i < imux->num_items; i++) {
2449 const char *label;
2450 bool inv_dmic;
2451 label = hda_get_autocfg_input_label(codec, &spec->autocfg, i);
2452 if (prev_label && !strcmp(label, prev_label))
2453 type_idx++;
2454 else
2455 type_idx = 0;
2456 prev_label = label;
2457 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
2458
2459 for (type = 0; type < 2; type++) {
2460 err = add_single_cap_ctl(codec, label, type_idx, type,
2461 get_first_cap_ctl(codec, i, type),
2462 inv_dmic);
2463 if (err < 0)
2464 return err;
2465 }
2466 }
2467 return 0;
2468}
2469
2470static int create_capture_mixers(struct hda_codec *codec)
2471{
2472 struct hda_gen_spec *spec = codec->spec;
2473 struct hda_input_mux *imux = &spec->input_mux;
2474 int i, n, nums, err;
2475
2476 if (spec->dyn_adc_switch)
2477 nums = 1;
2478 else
2479 nums = spec->num_adc_nids;
2480
2481 if (!spec->auto_mic && imux->num_items > 1) {
2482 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01002483 const char *name;
2484 name = nums > 1 ? "Input Source" : "Capture Source";
2485 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002486 if (!knew)
2487 return -ENOMEM;
2488 knew->count = nums;
2489 }
2490
2491 for (n = 0; n < nums; n++) {
2492 bool multi = false;
2493 bool inv_dmic = false;
2494 int vol, sw;
2495
2496 vol = sw = 0;
2497 for (i = 0; i < imux->num_items; i++) {
2498 struct nid_path *path;
2499 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2500 get_adc_nid(codec, n, i));
2501 if (!path)
2502 continue;
2503 parse_capvol_in_path(codec, path);
2504 if (!vol)
2505 vol = path->ctls[NID_PATH_VOL_CTL];
2506 else if (vol != path->ctls[NID_PATH_VOL_CTL])
2507 multi = true;
2508 if (!sw)
2509 sw = path->ctls[NID_PATH_MUTE_CTL];
2510 else if (sw != path->ctls[NID_PATH_MUTE_CTL])
2511 multi = true;
2512 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
2513 inv_dmic = true;
2514 }
2515
2516 if (!multi)
2517 err = create_single_cap_vol_ctl(codec, n, vol, sw,
2518 inv_dmic);
2519 else if (!spec->multi_cap_vol)
2520 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
2521 else
2522 err = create_multi_cap_vol_ctl(codec);
2523 if (err < 0)
2524 return err;
2525 }
2526
2527 return 0;
2528}
2529
2530/*
2531 * add mic boosts if needed
2532 */
2533static int parse_mic_boost(struct hda_codec *codec)
2534{
2535 struct hda_gen_spec *spec = codec->spec;
2536 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002537 int i, err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002538 int type_idx = 0;
2539 hda_nid_t nid;
2540 const char *prev_label = NULL;
2541
2542 for (i = 0; i < cfg->num_inputs; i++) {
2543 if (cfg->inputs[i].type > AUTO_PIN_MIC)
2544 break;
2545 nid = cfg->inputs[i].pin;
2546 if (get_wcaps(codec, nid) & AC_WCAP_IN_AMP) {
2547 const char *label;
2548 char boost_label[32];
2549 struct nid_path *path;
2550 unsigned int val;
2551
2552 label = hda_get_autocfg_input_label(codec, cfg, i);
2553 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
2554 label = "Headphone Mic";
2555 if (prev_label && !strcmp(label, prev_label))
2556 type_idx++;
2557 else
2558 type_idx = 0;
2559 prev_label = label;
2560
2561 snprintf(boost_label, sizeof(boost_label),
2562 "%s Boost Volume", label);
2563 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
2564 err = add_control(spec, HDA_CTL_WIDGET_VOL,
2565 boost_label, type_idx, val);
2566 if (err < 0)
2567 return err;
2568
2569 path = snd_hda_get_nid_path(codec, nid, 0);
2570 if (path)
2571 path->ctls[NID_PATH_BOOST_CTL] = val;
2572 }
2573 }
2574 return 0;
2575}
2576
2577/*
2578 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
2579 */
2580static void parse_digital(struct hda_codec *codec)
2581{
2582 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002583 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002584 int i, nums;
2585 hda_nid_t dig_nid;
2586
2587 /* support multiple SPDIFs; the secondary is set up as a slave */
2588 nums = 0;
2589 for (i = 0; i < spec->autocfg.dig_outs; i++) {
2590 hda_nid_t pin = spec->autocfg.dig_out_pins[i];
2591 dig_nid = look_for_dac(codec, pin, true);
2592 if (!dig_nid)
2593 continue;
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01002594 path = snd_hda_add_new_path(codec, dig_nid, pin, HDA_PARSE_ALL);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002595 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002596 continue;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002597 print_nid_path("digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01002598 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01002599 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002600 if (!nums) {
2601 spec->multiout.dig_out_nid = dig_nid;
2602 spec->dig_out_type = spec->autocfg.dig_out_type[0];
2603 } else {
2604 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
2605 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
2606 break;
2607 spec->slave_dig_outs[nums - 1] = dig_nid;
2608 }
2609 nums++;
2610 }
2611
2612 if (spec->autocfg.dig_in_pin) {
2613 dig_nid = codec->start_nid;
2614 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002615 unsigned int wcaps = get_wcaps(codec, dig_nid);
2616 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
2617 continue;
2618 if (!(wcaps & AC_WCAP_DIGITAL))
2619 continue;
2620 path = snd_hda_add_new_path(codec,
2621 spec->autocfg.dig_in_pin,
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01002622 dig_nid, HDA_PARSE_ALL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002623 if (path) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002624 print_nid_path("digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002625 path->active = true;
2626 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01002627 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002628 break;
2629 }
2630 }
2631 }
2632}
2633
2634
2635/*
2636 * input MUX handling
2637 */
2638
2639static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
2640
2641/* select the given imux item; either unmute exclusively or select the route */
2642static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2643 unsigned int idx)
2644{
2645 struct hda_gen_spec *spec = codec->spec;
2646 const struct hda_input_mux *imux;
2647 struct nid_path *path;
2648
2649 imux = &spec->input_mux;
2650 if (!imux->num_items)
2651 return 0;
2652
2653 if (idx >= imux->num_items)
2654 idx = imux->num_items - 1;
2655 if (spec->cur_mux[adc_idx] == idx)
2656 return 0;
2657
2658 path = snd_hda_get_nid_path(codec,
2659 spec->imux_pins[spec->cur_mux[adc_idx]],
2660 spec->adc_nids[adc_idx]);
2661 if (!path)
2662 return 0;
2663 if (path->active)
2664 snd_hda_activate_path(codec, path, false, false);
2665
2666 spec->cur_mux[adc_idx] = idx;
2667
2668 if (spec->shared_mic_hp)
2669 update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
2670
2671 if (spec->dyn_adc_switch)
2672 dyn_adc_pcm_resetup(codec, idx);
2673
2674 path = snd_hda_get_nid_path(codec, spec->imux_pins[idx],
2675 get_adc_nid(codec, adc_idx, idx));
2676 if (!path)
2677 return 0;
2678 if (path->active)
2679 return 0;
2680 snd_hda_activate_path(codec, path, true, false);
2681 if (spec->cap_sync_hook)
2682 spec->cap_sync_hook(codec);
2683 return 1;
2684}
2685
2686
2687/*
2688 * Jack detections for HP auto-mute and mic-switch
2689 */
2690
2691/* check each pin in the given array; returns true if any of them is plugged */
2692static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
2693{
2694 int i, present = 0;
2695
2696 for (i = 0; i < num_pins; i++) {
2697 hda_nid_t nid = pins[i];
2698 if (!nid)
2699 break;
2700 present |= snd_hda_jack_detect(codec, nid);
2701 }
2702 return present;
2703}
2704
2705/* standard HP/line-out auto-mute helper */
2706static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
2707 bool mute, bool hp_out)
2708{
2709 struct hda_gen_spec *spec = codec->spec;
2710 unsigned int pin_bits = mute ? 0 : (hp_out ? PIN_HP : PIN_OUT);
2711 int i;
2712
2713 for (i = 0; i < num_pins; i++) {
2714 hda_nid_t nid = pins[i];
2715 unsigned int val;
2716 if (!nid)
2717 break;
2718 /* don't reset VREF value in case it's controlling
2719 * the amp (see alc861_fixup_asus_amp_vref_0f())
2720 */
2721 if (spec->keep_vref_in_automute) {
2722 val = snd_hda_codec_read(codec, nid, 0,
2723 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2724 val &= ~PIN_HP;
2725 } else
2726 val = 0;
2727 val |= pin_bits;
Takashi Iwai7594aa32012-12-20 15:38:40 +01002728 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002729 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002730 }
2731}
2732
2733/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002734void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002735{
2736 struct hda_gen_spec *spec = codec->spec;
2737 int on;
2738
2739 /* Control HP pins/amps depending on master_mute state;
2740 * in general, HP pins/amps control should be enabled in all cases,
2741 * but currently set only for master_mute, just to be safe
2742 */
2743 if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
2744 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2745 spec->autocfg.hp_pins, spec->master_mute, true);
2746
2747 if (!spec->automute_speaker)
2748 on = 0;
2749 else
2750 on = spec->hp_jack_present | spec->line_jack_present;
2751 on |= spec->master_mute;
2752 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
2753 spec->autocfg.speaker_pins, on, false);
2754
2755 /* toggle line-out mutes if needed, too */
2756 /* if LO is a copy of either HP or Speaker, don't need to handle it */
2757 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
2758 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
2759 return;
2760 if (!spec->automute_lo)
2761 on = 0;
2762 else
2763 on = spec->hp_jack_present;
2764 on |= spec->master_mute;
2765 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2766 spec->autocfg.line_out_pins, on, false);
2767}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002768EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002769
2770static void call_update_outputs(struct hda_codec *codec)
2771{
2772 struct hda_gen_spec *spec = codec->spec;
2773 if (spec->automute_hook)
2774 spec->automute_hook(codec);
2775 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01002776 snd_hda_gen_update_outputs(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002777}
2778
2779/* standard HP-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002780void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002781{
2782 struct hda_gen_spec *spec = codec->spec;
2783
2784 spec->hp_jack_present =
2785 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2786 spec->autocfg.hp_pins);
2787 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
2788 return;
2789 call_update_outputs(codec);
2790}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002791EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002792
2793/* standard line-out-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002794void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002795{
2796 struct hda_gen_spec *spec = codec->spec;
2797
2798 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
2799 return;
2800 /* check LO jack only when it's different from HP */
2801 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
2802 return;
2803
2804 spec->line_jack_present =
2805 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2806 spec->autocfg.line_out_pins);
2807 if (!spec->automute_speaker || !spec->detect_lo)
2808 return;
2809 call_update_outputs(codec);
2810}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002811EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002812
2813/* standard mic auto-switch helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002814void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002815{
2816 struct hda_gen_spec *spec = codec->spec;
2817 int i;
2818
2819 if (!spec->auto_mic)
2820 return;
2821
2822 for (i = spec->am_num_entries - 1; i > 0; i--) {
2823 if (snd_hda_jack_detect(codec, spec->am_entry[i].pin)) {
2824 mux_select(codec, 0, spec->am_entry[i].idx);
2825 return;
2826 }
2827 }
2828 mux_select(codec, 0, spec->am_entry[0].idx);
2829}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002830EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002831
2832/*
2833 * Auto-Mute mode mixer enum support
2834 */
2835static int automute_mode_info(struct snd_kcontrol *kcontrol,
2836 struct snd_ctl_elem_info *uinfo)
2837{
2838 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2839 struct hda_gen_spec *spec = codec->spec;
2840 static const char * const texts3[] = {
2841 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02002842 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843
Takashi Iwai352f7f92012-12-19 12:52:06 +01002844 if (spec->automute_speaker_possible && spec->automute_lo_possible)
2845 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
2846 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2847}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848
Takashi Iwai352f7f92012-12-19 12:52:06 +01002849static int automute_mode_get(struct snd_kcontrol *kcontrol,
2850 struct snd_ctl_elem_value *ucontrol)
2851{
2852 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2853 struct hda_gen_spec *spec = codec->spec;
2854 unsigned int val = 0;
2855 if (spec->automute_speaker)
2856 val++;
2857 if (spec->automute_lo)
2858 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002859
Takashi Iwai352f7f92012-12-19 12:52:06 +01002860 ucontrol->value.enumerated.item[0] = val;
2861 return 0;
2862}
2863
2864static int automute_mode_put(struct snd_kcontrol *kcontrol,
2865 struct snd_ctl_elem_value *ucontrol)
2866{
2867 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2868 struct hda_gen_spec *spec = codec->spec;
2869
2870 switch (ucontrol->value.enumerated.item[0]) {
2871 case 0:
2872 if (!spec->automute_speaker && !spec->automute_lo)
2873 return 0;
2874 spec->automute_speaker = 0;
2875 spec->automute_lo = 0;
2876 break;
2877 case 1:
2878 if (spec->automute_speaker_possible) {
2879 if (!spec->automute_lo && spec->automute_speaker)
2880 return 0;
2881 spec->automute_speaker = 1;
2882 spec->automute_lo = 0;
2883 } else if (spec->automute_lo_possible) {
2884 if (spec->automute_lo)
2885 return 0;
2886 spec->automute_lo = 1;
2887 } else
2888 return -EINVAL;
2889 break;
2890 case 2:
2891 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
2892 return -EINVAL;
2893 if (spec->automute_speaker && spec->automute_lo)
2894 return 0;
2895 spec->automute_speaker = 1;
2896 spec->automute_lo = 1;
2897 break;
2898 default:
2899 return -EINVAL;
2900 }
2901 call_update_outputs(codec);
2902 return 1;
2903}
2904
2905static const struct snd_kcontrol_new automute_mode_enum = {
2906 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2907 .name = "Auto-Mute Mode",
2908 .info = automute_mode_info,
2909 .get = automute_mode_get,
2910 .put = automute_mode_put,
2911};
2912
2913static int add_automute_mode_enum(struct hda_codec *codec)
2914{
2915 struct hda_gen_spec *spec = codec->spec;
2916
Takashi Iwai12c93df2012-12-19 14:38:33 +01002917 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002918 return -ENOMEM;
2919 return 0;
2920}
2921
2922/*
2923 * Check the availability of HP/line-out auto-mute;
2924 * Set up appropriately if really supported
2925 */
2926static int check_auto_mute_availability(struct hda_codec *codec)
2927{
2928 struct hda_gen_spec *spec = codec->spec;
2929 struct auto_pin_cfg *cfg = &spec->autocfg;
2930 int present = 0;
2931 int i, err;
2932
2933 if (cfg->hp_pins[0])
2934 present++;
2935 if (cfg->line_out_pins[0])
2936 present++;
2937 if (cfg->speaker_pins[0])
2938 present++;
2939 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02002940 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002941
2942 if (!cfg->speaker_pins[0] &&
2943 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
2944 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2945 sizeof(cfg->speaker_pins));
2946 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948
Takashi Iwai352f7f92012-12-19 12:52:06 +01002949 if (!cfg->hp_pins[0] &&
2950 cfg->line_out_type == AUTO_PIN_HP_OUT) {
2951 memcpy(cfg->hp_pins, cfg->line_out_pins,
2952 sizeof(cfg->hp_pins));
2953 cfg->hp_outs = cfg->line_outs;
2954 }
2955
2956 for (i = 0; i < cfg->hp_outs; i++) {
2957 hda_nid_t nid = cfg->hp_pins[i];
2958 if (!is_jack_detectable(codec, nid))
2959 continue;
2960 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
2961 nid);
2962 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01002963 spec->hp_automute_hook ?
2964 spec->hp_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01002965 snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002966 spec->detect_hp = 1;
2967 }
2968
2969 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
2970 if (cfg->speaker_outs)
2971 for (i = 0; i < cfg->line_outs; i++) {
2972 hda_nid_t nid = cfg->line_out_pins[i];
2973 if (!is_jack_detectable(codec, nid))
2974 continue;
2975 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
2976 snd_hda_jack_detect_enable_callback(codec, nid,
2977 HDA_GEN_FRONT_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01002978 spec->line_automute_hook ?
2979 spec->line_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01002980 snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002981 spec->detect_lo = 1;
2982 }
2983 spec->automute_lo_possible = spec->detect_hp;
2984 }
2985
2986 spec->automute_speaker_possible = cfg->speaker_outs &&
2987 (spec->detect_hp || spec->detect_lo);
2988
2989 spec->automute_lo = spec->automute_lo_possible;
2990 spec->automute_speaker = spec->automute_speaker_possible;
2991
2992 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
2993 /* create a control for automute mode */
2994 err = add_automute_mode_enum(codec);
2995 if (err < 0)
2996 return err;
2997 }
2998 return 0;
2999}
3000
3001/* return the position of NID in the list, or -1 if not found */
3002static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
3003{
3004 int i;
3005 for (i = 0; i < nums; i++)
3006 if (list[i] == nid)
3007 return i;
3008 return -1;
3009}
3010
3011/* check whether all auto-mic pins are valid; setup indices if OK */
3012static bool auto_mic_check_imux(struct hda_codec *codec)
3013{
3014 struct hda_gen_spec *spec = codec->spec;
3015 const struct hda_input_mux *imux;
3016 int i;
3017
3018 imux = &spec->input_mux;
3019 for (i = 0; i < spec->am_num_entries; i++) {
3020 spec->am_entry[i].idx =
3021 find_idx_in_nid_list(spec->am_entry[i].pin,
3022 spec->imux_pins, imux->num_items);
3023 if (spec->am_entry[i].idx < 0)
3024 return false; /* no corresponding imux */
3025 }
3026
3027 /* we don't need the jack detection for the first pin */
3028 for (i = 1; i < spec->am_num_entries; i++)
3029 snd_hda_jack_detect_enable_callback(codec,
3030 spec->am_entry[i].pin,
3031 HDA_GEN_MIC_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003032 spec->mic_autoswitch_hook ?
3033 spec->mic_autoswitch_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003034 snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003035 return true;
3036}
3037
3038static int compare_attr(const void *ap, const void *bp)
3039{
3040 const struct automic_entry *a = ap;
3041 const struct automic_entry *b = bp;
3042 return (int)(a->attr - b->attr);
3043}
3044
3045/*
3046 * Check the availability of auto-mic switch;
3047 * Set up if really supported
3048 */
3049static int check_auto_mic_availability(struct hda_codec *codec)
3050{
3051 struct hda_gen_spec *spec = codec->spec;
3052 struct auto_pin_cfg *cfg = &spec->autocfg;
3053 unsigned int types;
3054 int i, num_pins;
3055
3056 types = 0;
3057 num_pins = 0;
3058 for (i = 0; i < cfg->num_inputs; i++) {
3059 hda_nid_t nid = cfg->inputs[i].pin;
3060 unsigned int attr;
3061 attr = snd_hda_codec_get_pincfg(codec, nid);
3062 attr = snd_hda_get_input_pin_attr(attr);
3063 if (types & (1 << attr))
3064 return 0; /* already occupied */
3065 switch (attr) {
3066 case INPUT_PIN_ATTR_INT:
3067 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3068 return 0; /* invalid type */
3069 break;
3070 case INPUT_PIN_ATTR_UNUSED:
3071 return 0; /* invalid entry */
3072 default:
3073 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
3074 return 0; /* invalid type */
3075 if (!spec->line_in_auto_switch &&
3076 cfg->inputs[i].type != AUTO_PIN_MIC)
3077 return 0; /* only mic is allowed */
3078 if (!is_jack_detectable(codec, nid))
3079 return 0; /* no unsol support */
3080 break;
3081 }
3082 if (num_pins >= MAX_AUTO_MIC_PINS)
3083 return 0;
3084 types |= (1 << attr);
3085 spec->am_entry[num_pins].pin = nid;
3086 spec->am_entry[num_pins].attr = attr;
3087 num_pins++;
3088 }
3089
3090 if (num_pins < 2)
3091 return 0;
3092
3093 spec->am_num_entries = num_pins;
3094 /* sort the am_entry in the order of attr so that the pin with a
3095 * higher attr will be selected when the jack is plugged.
3096 */
3097 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
3098 compare_attr, NULL);
3099
3100 if (!auto_mic_check_imux(codec))
3101 return 0;
3102
3103 spec->auto_mic = 1;
3104 spec->num_adc_nids = 1;
3105 spec->cur_mux[0] = spec->am_entry[0].idx;
3106 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
3107 spec->am_entry[0].pin,
3108 spec->am_entry[1].pin,
3109 spec->am_entry[2].pin);
3110
3111 return 0;
3112}
3113
3114
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003115/*
3116 * Parse the given BIOS configuration and set up the hda_gen_spec
3117 *
3118 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003119 * or a negative error code
3120 */
3121int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003122 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003123{
3124 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003125 int err;
3126
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003127 if (cfg != &spec->autocfg) {
3128 spec->autocfg = *cfg;
3129 cfg = &spec->autocfg;
3130 }
3131
Takashi Iwai352f7f92012-12-19 12:52:06 +01003132 if (!cfg->line_outs) {
3133 if (cfg->dig_outs || cfg->dig_in_pin) {
3134 spec->multiout.max_channels = 2;
3135 spec->no_analog = 1;
3136 goto dig_only;
3137 }
3138 return 0; /* can't find valid BIOS pin config */
3139 }
3140
3141 if (!spec->no_primary_hp &&
3142 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
3143 cfg->line_outs <= cfg->hp_outs) {
3144 /* use HP as primary out */
3145 cfg->speaker_outs = cfg->line_outs;
3146 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3147 sizeof(cfg->speaker_pins));
3148 cfg->line_outs = cfg->hp_outs;
3149 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
3150 cfg->hp_outs = 0;
3151 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3152 cfg->line_out_type = AUTO_PIN_HP_OUT;
3153 }
3154
3155 err = parse_output_paths(codec);
3156 if (err < 0)
3157 return err;
3158 err = create_multi_channel_mode(codec);
3159 if (err < 0)
3160 return err;
3161 err = create_multi_out_ctls(codec, cfg);
3162 if (err < 0)
3163 return err;
3164 err = create_hp_out_ctls(codec);
3165 if (err < 0)
3166 return err;
3167 err = create_speaker_out_ctls(codec);
3168 if (err < 0)
3169 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003170 err = create_indep_hp_ctls(codec);
3171 if (err < 0)
3172 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01003173 err = create_loopback_mixing_ctl(codec);
3174 if (err < 0)
3175 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003176 err = create_shared_input(codec);
3177 if (err < 0)
3178 return err;
3179 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003180 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02003181 return err;
3182
Takashi Iwai352f7f92012-12-19 12:52:06 +01003183 /* check the multiple speaker pins */
3184 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
3185 spec->const_channel_count = cfg->line_outs * 2;
3186 else
3187 spec->const_channel_count = cfg->speaker_outs * 2;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003188
Takashi Iwai352f7f92012-12-19 12:52:06 +01003189 if (spec->multi_ios > 0)
3190 spec->multiout.max_channels = max(spec->ext_channel_count,
3191 spec->const_channel_count);
3192 else
3193 spec->multiout.max_channels = spec->multiout.num_dacs * 2;
3194
3195 err = check_auto_mute_availability(codec);
3196 if (err < 0)
3197 return err;
3198
3199 err = check_dyn_adc_switch(codec);
3200 if (err < 0)
3201 return err;
3202
3203 if (!spec->shared_mic_hp) {
3204 err = check_auto_mic_availability(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003205 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 }
Takashi Iwai071c73a2006-08-23 18:34:06 +02003208
Takashi Iwai352f7f92012-12-19 12:52:06 +01003209 err = create_capture_mixers(codec);
3210 if (err < 0)
3211 return err;
3212
3213 err = parse_mic_boost(codec);
3214 if (err < 0)
3215 return err;
3216
3217 dig_only:
3218 parse_digital(codec);
3219
3220 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003222EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223
3224
3225/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003226 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003228
3229/* slave controls for virtual master */
3230static const char * const slave_pfxs[] = {
3231 "Front", "Surround", "Center", "LFE", "Side",
3232 "Headphone", "Speaker", "Mono", "Line Out",
3233 "CLFE", "Bass Speaker", "PCM",
3234 NULL,
3235};
3236
3237int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003239 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241
Takashi Iwai36502d02012-12-19 15:15:10 +01003242 if (spec->kctls.used) {
3243 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
3244 if (err < 0)
3245 return err;
3246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247
Takashi Iwai352f7f92012-12-19 12:52:06 +01003248 if (spec->multiout.dig_out_nid) {
3249 err = snd_hda_create_dig_out_ctls(codec,
3250 spec->multiout.dig_out_nid,
3251 spec->multiout.dig_out_nid,
3252 spec->pcm_rec[1].pcm_type);
3253 if (err < 0)
3254 return err;
3255 if (!spec->no_analog) {
3256 err = snd_hda_create_spdif_share_sw(codec,
3257 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 if (err < 0)
3259 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003260 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 }
3262 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003263 if (spec->dig_in_nid) {
3264 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
3265 if (err < 0)
3266 return err;
3267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268
Takashi Iwai352f7f92012-12-19 12:52:06 +01003269 /* if we have no master control, let's create it */
3270 if (!spec->no_analog &&
3271 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
3272 unsigned int vmaster_tlv[4];
3273 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
3274 HDA_OUTPUT, vmaster_tlv);
3275 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
3276 vmaster_tlv, slave_pfxs,
3277 "Playback Volume");
3278 if (err < 0)
3279 return err;
3280 }
3281 if (!spec->no_analog &&
3282 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
3283 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
3284 NULL, slave_pfxs,
3285 "Playback Switch",
3286 true, &spec->vmaster_mute.sw_kctl);
3287 if (err < 0)
3288 return err;
3289 if (spec->vmaster_mute.hook)
Takashi Iwaifd25a972012-12-20 14:57:18 +01003290 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
3291 spec->vmaster_mute_enum);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293
Takashi Iwai352f7f92012-12-19 12:52:06 +01003294 free_kctls(spec); /* no longer needed */
3295
3296 if (spec->shared_mic_hp) {
3297 int err;
3298 int nid = spec->autocfg.inputs[1].pin;
3299 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
3300 if (err < 0)
3301 return err;
3302 err = snd_hda_jack_detect_enable(codec, nid, 0);
3303 if (err < 0)
3304 return err;
3305 }
3306
3307 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
3308 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 return err;
3310
3311 return 0;
3312}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003313EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
3314
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315
3316/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003317 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319
Takashi Iwai352f7f92012-12-19 12:52:06 +01003320/*
3321 * Analog playback callbacks
3322 */
3323static int playback_pcm_open(struct hda_pcm_stream *hinfo,
3324 struct hda_codec *codec,
3325 struct snd_pcm_substream *substream)
3326{
3327 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003328 int err;
3329
3330 mutex_lock(&spec->pcm_mutex);
3331 err = snd_hda_multi_out_analog_open(codec,
3332 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003333 hinfo);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003334 if (!err)
3335 spec->active_streams |= 1 << STREAM_MULTI_OUT;
3336 mutex_unlock(&spec->pcm_mutex);
3337 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003338}
3339
3340static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01003341 struct hda_codec *codec,
3342 unsigned int stream_tag,
3343 unsigned int format,
3344 struct snd_pcm_substream *substream)
3345{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003346 struct hda_gen_spec *spec = codec->spec;
3347 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
3348 stream_tag, format, substream);
3349}
Takashi Iwai97ec5582006-03-21 11:29:07 +01003350
Takashi Iwai352f7f92012-12-19 12:52:06 +01003351static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3352 struct hda_codec *codec,
3353 struct snd_pcm_substream *substream)
3354{
3355 struct hda_gen_spec *spec = codec->spec;
3356 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
3357}
3358
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003359static int playback_pcm_close(struct hda_pcm_stream *hinfo,
3360 struct hda_codec *codec,
3361 struct snd_pcm_substream *substream)
3362{
3363 struct hda_gen_spec *spec = codec->spec;
3364 mutex_lock(&spec->pcm_mutex);
3365 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
3366 mutex_unlock(&spec->pcm_mutex);
3367 return 0;
3368}
3369
3370static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
3371 struct hda_codec *codec,
3372 struct snd_pcm_substream *substream)
3373{
3374 struct hda_gen_spec *spec = codec->spec;
3375 int err = 0;
3376
3377 mutex_lock(&spec->pcm_mutex);
3378 if (!spec->indep_hp_enabled)
3379 err = -EBUSY;
3380 else
3381 spec->active_streams |= 1 << STREAM_INDEP_HP;
3382 mutex_unlock(&spec->pcm_mutex);
3383 return err;
3384}
3385
3386static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
3387 struct hda_codec *codec,
3388 struct snd_pcm_substream *substream)
3389{
3390 struct hda_gen_spec *spec = codec->spec;
3391 mutex_lock(&spec->pcm_mutex);
3392 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
3393 mutex_unlock(&spec->pcm_mutex);
3394 return 0;
3395}
3396
Takashi Iwai352f7f92012-12-19 12:52:06 +01003397/*
3398 * Digital out
3399 */
3400static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
3401 struct hda_codec *codec,
3402 struct snd_pcm_substream *substream)
3403{
3404 struct hda_gen_spec *spec = codec->spec;
3405 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3406}
3407
3408static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3409 struct hda_codec *codec,
3410 unsigned int stream_tag,
3411 unsigned int format,
3412 struct snd_pcm_substream *substream)
3413{
3414 struct hda_gen_spec *spec = codec->spec;
3415 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3416 stream_tag, format, substream);
3417}
3418
3419static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3420 struct hda_codec *codec,
3421 struct snd_pcm_substream *substream)
3422{
3423 struct hda_gen_spec *spec = codec->spec;
3424 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
3425}
3426
3427static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
3428 struct hda_codec *codec,
3429 struct snd_pcm_substream *substream)
3430{
3431 struct hda_gen_spec *spec = codec->spec;
3432 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3433}
3434
3435/*
3436 * Analog capture
3437 */
3438static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3439 struct hda_codec *codec,
3440 unsigned int stream_tag,
3441 unsigned int format,
3442 struct snd_pcm_substream *substream)
3443{
3444 struct hda_gen_spec *spec = codec->spec;
3445
3446 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01003447 stream_tag, 0, format);
3448 return 0;
3449}
3450
Takashi Iwai352f7f92012-12-19 12:52:06 +01003451static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3452 struct hda_codec *codec,
3453 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01003454{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003455 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01003456
Takashi Iwai352f7f92012-12-19 12:52:06 +01003457 snd_hda_codec_cleanup_stream(codec,
3458 spec->adc_nids[substream->number + 1]);
Takashi Iwai97ec5582006-03-21 11:29:07 +01003459 return 0;
3460}
3461
Takashi Iwai352f7f92012-12-19 12:52:06 +01003462/*
3463 */
3464static const struct hda_pcm_stream pcm_analog_playback = {
3465 .substreams = 1,
3466 .channels_min = 2,
3467 .channels_max = 8,
3468 /* NID is set in build_pcms */
3469 .ops = {
3470 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003471 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003472 .prepare = playback_pcm_prepare,
3473 .cleanup = playback_pcm_cleanup
3474 },
3475};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476
Takashi Iwai352f7f92012-12-19 12:52:06 +01003477static const struct hda_pcm_stream pcm_analog_capture = {
3478 .substreams = 1,
3479 .channels_min = 2,
3480 .channels_max = 2,
3481 /* NID is set in build_pcms */
3482};
3483
3484static const struct hda_pcm_stream pcm_analog_alt_playback = {
3485 .substreams = 1,
3486 .channels_min = 2,
3487 .channels_max = 2,
3488 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003489 .ops = {
3490 .open = alt_playback_pcm_open,
3491 .close = alt_playback_pcm_close
3492 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01003493};
3494
3495static const struct hda_pcm_stream pcm_analog_alt_capture = {
3496 .substreams = 2, /* can be overridden */
3497 .channels_min = 2,
3498 .channels_max = 2,
3499 /* NID is set in build_pcms */
3500 .ops = {
3501 .prepare = alt_capture_pcm_prepare,
3502 .cleanup = alt_capture_pcm_cleanup
3503 },
3504};
3505
3506static const struct hda_pcm_stream pcm_digital_playback = {
3507 .substreams = 1,
3508 .channels_min = 2,
3509 .channels_max = 2,
3510 /* NID is set in build_pcms */
3511 .ops = {
3512 .open = dig_playback_pcm_open,
3513 .close = dig_playback_pcm_close,
3514 .prepare = dig_playback_pcm_prepare,
3515 .cleanup = dig_playback_pcm_cleanup
3516 },
3517};
3518
3519static const struct hda_pcm_stream pcm_digital_capture = {
3520 .substreams = 1,
3521 .channels_min = 2,
3522 .channels_max = 2,
3523 /* NID is set in build_pcms */
3524};
3525
3526/* Used by build_pcms to flag that a PCM has no playback stream */
3527static const struct hda_pcm_stream pcm_null_stream = {
3528 .substreams = 0,
3529 .channels_min = 0,
3530 .channels_max = 0,
3531};
3532
3533/*
3534 * dynamic changing ADC PCM streams
3535 */
3536static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
3537{
3538 struct hda_gen_spec *spec = codec->spec;
3539 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
3540
3541 if (spec->cur_adc && spec->cur_adc != new_adc) {
3542 /* stream is running, let's swap the current ADC */
3543 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
3544 spec->cur_adc = new_adc;
3545 snd_hda_codec_setup_stream(codec, new_adc,
3546 spec->cur_adc_stream_tag, 0,
3547 spec->cur_adc_format);
3548 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003550 return false;
3551}
3552
3553/* analog capture with dynamic dual-adc changes */
3554static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3555 struct hda_codec *codec,
3556 unsigned int stream_tag,
3557 unsigned int format,
3558 struct snd_pcm_substream *substream)
3559{
3560 struct hda_gen_spec *spec = codec->spec;
3561 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
3562 spec->cur_adc_stream_tag = stream_tag;
3563 spec->cur_adc_format = format;
3564 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
3565 return 0;
3566}
3567
3568static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3569 struct hda_codec *codec,
3570 struct snd_pcm_substream *substream)
3571{
3572 struct hda_gen_spec *spec = codec->spec;
3573 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
3574 spec->cur_adc = 0;
3575 return 0;
3576}
3577
3578static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
3579 .substreams = 1,
3580 .channels_min = 2,
3581 .channels_max = 2,
3582 .nid = 0, /* fill later */
3583 .ops = {
3584 .prepare = dyn_adc_capture_pcm_prepare,
3585 .cleanup = dyn_adc_capture_pcm_cleanup
3586 },
3587};
3588
Takashi Iwaif873e532012-12-20 16:58:39 +01003589static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
3590 const char *chip_name)
3591{
3592 char *p;
3593
3594 if (*str)
3595 return;
3596 strlcpy(str, chip_name, len);
3597
3598 /* drop non-alnum chars after a space */
3599 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
3600 if (!isalnum(p[1])) {
3601 *p = 0;
3602 break;
3603 }
3604 }
3605 strlcat(str, sfx, len);
3606}
3607
Takashi Iwai352f7f92012-12-19 12:52:06 +01003608/* build PCM streams based on the parsed results */
3609int snd_hda_gen_build_pcms(struct hda_codec *codec)
3610{
3611 struct hda_gen_spec *spec = codec->spec;
3612 struct hda_pcm *info = spec->pcm_rec;
3613 const struct hda_pcm_stream *p;
3614 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615
3616 codec->num_pcms = 1;
3617 codec->pcm_info = info;
3618
Takashi Iwai352f7f92012-12-19 12:52:06 +01003619 if (spec->no_analog)
3620 goto skip_analog;
3621
Takashi Iwaif873e532012-12-20 16:58:39 +01003622 fill_pcm_stream_name(spec->stream_name_analog,
3623 sizeof(spec->stream_name_analog),
3624 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003625 info->name = spec->stream_name_analog;
3626
3627 if (spec->multiout.num_dacs > 0) {
3628 p = spec->stream_analog_playback;
3629 if (!p)
3630 p = &pcm_analog_playback;
3631 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3632 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
3633 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
3634 spec->multiout.max_channels;
3635 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
3636 spec->autocfg.line_outs == 2)
3637 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
3638 snd_pcm_2_1_chmaps;
3639 }
3640 if (spec->num_adc_nids) {
3641 p = spec->stream_analog_capture;
3642 if (!p) {
3643 if (spec->dyn_adc_switch)
3644 p = &dyn_adc_pcm_analog_capture;
3645 else
3646 p = &pcm_analog_capture;
3647 }
3648 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3649 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
3650 }
3651
Takashi Iwai352f7f92012-12-19 12:52:06 +01003652 skip_analog:
3653 /* SPDIF for stream index #1 */
3654 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01003655 fill_pcm_stream_name(spec->stream_name_digital,
3656 sizeof(spec->stream_name_digital),
3657 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003658 codec->num_pcms = 2;
3659 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
3660 info = spec->pcm_rec + 1;
3661 info->name = spec->stream_name_digital;
3662 if (spec->dig_out_type)
3663 info->pcm_type = spec->dig_out_type;
3664 else
3665 info->pcm_type = HDA_PCM_TYPE_SPDIF;
3666 if (spec->multiout.dig_out_nid) {
3667 p = spec->stream_digital_playback;
3668 if (!p)
3669 p = &pcm_digital_playback;
3670 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3671 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
3672 }
3673 if (spec->dig_in_nid) {
3674 p = spec->stream_digital_capture;
3675 if (!p)
3676 p = &pcm_digital_capture;
3677 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3678 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
3679 }
3680 }
3681
3682 if (spec->no_analog)
3683 return 0;
3684
3685 /* If the use of more than one ADC is requested for the current
3686 * model, configure a second analog capture-only PCM.
3687 */
3688 have_multi_adcs = (spec->num_adc_nids > 1) &&
3689 !spec->dyn_adc_switch && !spec->auto_mic;
3690 /* Additional Analaog capture for index #2 */
3691 if (spec->alt_dac_nid || have_multi_adcs) {
3692 codec->num_pcms = 3;
3693 info = spec->pcm_rec + 2;
3694 info->name = spec->stream_name_analog;
3695 if (spec->alt_dac_nid) {
3696 p = spec->stream_analog_alt_playback;
3697 if (!p)
3698 p = &pcm_analog_alt_playback;
3699 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3700 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
3701 spec->alt_dac_nid;
3702 } else {
3703 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
3704 pcm_null_stream;
3705 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
3706 }
3707 if (have_multi_adcs) {
3708 p = spec->stream_analog_alt_capture;
3709 if (!p)
3710 p = &pcm_analog_alt_capture;
3711 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3712 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
3713 spec->adc_nids[1];
3714 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
3715 spec->num_adc_nids - 1;
3716 } else {
3717 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
3718 pcm_null_stream;
3719 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
3720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 }
3722
3723 return 0;
3724}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003725EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
3726
3727
3728/*
3729 * Standard auto-parser initializations
3730 */
3731
3732/* configure the path from the given dac to the pin as the proper output */
3733static void set_output_and_unmute(struct hda_codec *codec, hda_nid_t pin,
Takashi Iwai196c17662013-01-04 15:01:40 +01003734 int pin_type, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003735{
3736 struct nid_path *path;
3737
3738 snd_hda_set_pin_ctl_cache(codec, pin, pin_type);
Takashi Iwai196c17662013-01-04 15:01:40 +01003739 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003740 if (!path)
3741 return;
Takashi Iwaie1284af2013-01-03 16:33:02 +01003742 snd_hda_activate_path(codec, path, path->active, true);
3743 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003744}
3745
3746/* initialize primary output paths */
3747static void init_multi_out(struct hda_codec *codec)
3748{
3749 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai196c17662013-01-04 15:01:40 +01003750 hda_nid_t nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003751 int pin_type;
3752 int i;
3753
3754 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
3755 pin_type = PIN_HP;
3756 else
3757 pin_type = PIN_OUT;
3758
Takashi Iwai64049c82012-12-20 15:29:21 +01003759 for (i = 0; i < spec->autocfg.line_outs; i++) {
3760 nid = spec->autocfg.line_out_pins[i];
Takashi Iwai196c17662013-01-04 15:01:40 +01003761 if (nid)
3762 set_output_and_unmute(codec, nid, pin_type,
3763 spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003764 }
3765}
3766
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003767
3768static void __init_extra_out(struct hda_codec *codec, int num_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01003769 hda_nid_t *pins, int *paths, int type)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003770{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003771 int i;
Takashi Iwai196c17662013-01-04 15:01:40 +01003772 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003773
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003774 for (i = 0; i < num_outs; i++) {
3775 pin = pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003776 if (!pin)
3777 break;
Takashi Iwai196c17662013-01-04 15:01:40 +01003778 set_output_and_unmute(codec, pin, type, paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003779 }
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003780}
3781
3782/* initialize hp and speaker paths */
3783static void init_extra_out(struct hda_codec *codec)
3784{
3785 struct hda_gen_spec *spec = codec->spec;
3786
3787 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
3788 __init_extra_out(codec, spec->autocfg.hp_outs,
3789 spec->autocfg.hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01003790 spec->hp_paths, PIN_HP);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003791 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
3792 __init_extra_out(codec, spec->autocfg.speaker_outs,
3793 spec->autocfg.speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01003794 spec->speaker_paths, PIN_OUT);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003795}
3796
3797/* initialize multi-io paths */
3798static void init_multi_io(struct hda_codec *codec)
3799{
3800 struct hda_gen_spec *spec = codec->spec;
3801 int i;
3802
3803 for (i = 0; i < spec->multi_ios; i++) {
3804 hda_nid_t pin = spec->multi_io[i].pin;
3805 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01003806 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003807 if (!path)
3808 continue;
3809 if (!spec->multi_io[i].ctl_in)
3810 spec->multi_io[i].ctl_in =
3811 snd_hda_codec_update_cache(codec, pin, 0,
3812 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3813 snd_hda_activate_path(codec, path, path->active, true);
3814 }
3815}
3816
3817/* set up the input pin config, depending on the given auto-pin type */
3818static void set_input_pin(struct hda_codec *codec, hda_nid_t nid,
3819 int auto_pin_type)
3820{
3821 unsigned int val = PIN_IN;
3822 if (auto_pin_type == AUTO_PIN_MIC)
3823 val |= snd_hda_get_default_vref(codec, nid);
Takashi Iwai7594aa32012-12-20 15:38:40 +01003824 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003825}
3826
3827/* set up input pins and loopback paths */
3828static void init_analog_input(struct hda_codec *codec)
3829{
3830 struct hda_gen_spec *spec = codec->spec;
3831 struct auto_pin_cfg *cfg = &spec->autocfg;
3832 int i;
3833
3834 for (i = 0; i < cfg->num_inputs; i++) {
3835 hda_nid_t nid = cfg->inputs[i].pin;
3836 if (is_input_pin(codec, nid))
3837 set_input_pin(codec, nid, cfg->inputs[i].type);
3838
3839 /* init loopback inputs */
3840 if (spec->mixer_nid) {
3841 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01003842 path = snd_hda_get_path_from_idx(codec, spec->loopback_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003843 if (path)
3844 snd_hda_activate_path(codec, path,
3845 path->active, false);
3846 }
3847 }
3848}
3849
3850/* initialize ADC paths */
3851static void init_input_src(struct hda_codec *codec)
3852{
3853 struct hda_gen_spec *spec = codec->spec;
3854 struct hda_input_mux *imux = &spec->input_mux;
3855 struct nid_path *path;
3856 int i, c, nums;
3857
3858 if (spec->dyn_adc_switch)
3859 nums = 1;
3860 else
3861 nums = spec->num_adc_nids;
3862
3863 for (c = 0; c < nums; c++) {
3864 for (i = 0; i < imux->num_items; i++) {
3865 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
3866 get_adc_nid(codec, c, i));
3867 if (path) {
3868 bool active = path->active;
3869 if (i == spec->cur_mux[c])
3870 active = true;
3871 snd_hda_activate_path(codec, path, active, false);
3872 }
3873 }
3874 }
3875
3876 if (spec->shared_mic_hp)
3877 update_shared_mic_hp(codec, spec->cur_mux[0]);
3878
3879 if (spec->cap_sync_hook)
3880 spec->cap_sync_hook(codec);
3881}
3882
3883/* set right pin controls for digital I/O */
3884static void init_digital(struct hda_codec *codec)
3885{
3886 struct hda_gen_spec *spec = codec->spec;
3887 int i;
3888 hda_nid_t pin;
3889
3890 for (i = 0; i < spec->autocfg.dig_outs; i++) {
3891 pin = spec->autocfg.dig_out_pins[i];
3892 if (!pin)
3893 continue;
Takashi Iwai196c17662013-01-04 15:01:40 +01003894 set_output_and_unmute(codec, pin, PIN_OUT,
3895 spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003896 }
3897 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003898 if (pin) {
3899 struct nid_path *path;
Takashi Iwai7594aa32012-12-20 15:38:40 +01003900 snd_hda_set_pin_ctl_cache(codec, pin, PIN_IN);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003901 path = snd_hda_get_path_from_idx(codec, spec->digin_path);
3902 if (path)
3903 snd_hda_activate_path(codec, path, path->active, false);
3904 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003905}
3906
Takashi Iwai973e4972012-12-20 15:16:09 +01003907/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
3908 * invalid unsol tags by some reason
3909 */
3910static void clear_unsol_on_unused_pins(struct hda_codec *codec)
3911{
3912 int i;
3913
3914 for (i = 0; i < codec->init_pins.used; i++) {
3915 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
3916 hda_nid_t nid = pin->nid;
3917 if (is_jack_detectable(codec, nid) &&
3918 !snd_hda_jack_tbl_get(codec, nid))
3919 snd_hda_codec_update_cache(codec, nid, 0,
3920 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
3921 }
3922}
3923
Takashi Iwai352f7f92012-12-19 12:52:06 +01003924int snd_hda_gen_init(struct hda_codec *codec)
3925{
3926 struct hda_gen_spec *spec = codec->spec;
3927
3928 if (spec->init_hook)
3929 spec->init_hook(codec);
3930
3931 snd_hda_apply_verbs(codec);
3932
Takashi Iwai3bbcd272012-12-20 11:50:58 +01003933 codec->cached_write = 1;
3934
Takashi Iwai352f7f92012-12-19 12:52:06 +01003935 init_multi_out(codec);
3936 init_extra_out(codec);
3937 init_multi_io(codec);
3938 init_analog_input(codec);
3939 init_input_src(codec);
3940 init_digital(codec);
3941
Takashi Iwai973e4972012-12-20 15:16:09 +01003942 clear_unsol_on_unused_pins(codec);
3943
Takashi Iwai352f7f92012-12-19 12:52:06 +01003944 /* call init functions of standard auto-mute helpers */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003945 snd_hda_gen_hp_automute(codec, NULL);
3946 snd_hda_gen_line_automute(codec, NULL);
3947 snd_hda_gen_mic_autoswitch(codec, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003948
Takashi Iwai3bbcd272012-12-20 11:50:58 +01003949 snd_hda_codec_flush_amp_cache(codec);
3950 snd_hda_codec_flush_cmd_cache(codec);
3951
Takashi Iwai352f7f92012-12-19 12:52:06 +01003952 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
3953 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
3954
3955 hda_call_check_power_status(codec, 0x01);
3956 return 0;
3957}
3958EXPORT_SYMBOL(snd_hda_gen_init);
3959
3960
3961/*
3962 * the generic codec support
3963 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964
Takashi Iwai83012a72012-08-24 18:38:08 +02003965#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003966static int generic_check_power_status(struct hda_codec *codec, hda_nid_t nid)
3967{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003968 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003969 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
3970}
3971#endif
3972
Takashi Iwai352f7f92012-12-19 12:52:06 +01003973static void generic_free(struct hda_codec *codec)
3974{
3975 snd_hda_gen_spec_free(codec->spec);
3976 kfree(codec->spec);
3977 codec->spec = NULL;
3978}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979
Takashi Iwai352f7f92012-12-19 12:52:06 +01003980static const struct hda_codec_ops generic_patch_ops = {
3981 .build_controls = snd_hda_gen_build_controls,
3982 .build_pcms = snd_hda_gen_build_pcms,
3983 .init = snd_hda_gen_init,
3984 .free = generic_free,
3985 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02003986#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003987 .check_power_status = generic_check_power_status,
3988#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989};
3990
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991int snd_hda_parse_generic_codec(struct hda_codec *codec)
3992{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003993 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 int err;
3995
Takashi Iwaie560d8d2005-09-09 14:21:46 +02003996 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003997 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003999 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004002 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
4003 if (err < 0)
4004 return err;
4005
4006 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004007 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 goto error;
4009
4010 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011 return 0;
4012
Takashi Iwai352f7f92012-12-19 12:52:06 +01004013error:
4014 generic_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 return err;
4016}
Takashi Iwai1289e9e2008-11-27 15:47:11 +01004017EXPORT_SYMBOL(snd_hda_parse_generic_codec);