blob: e7574a863d21b4fe5f6a4d449fcbbcb3ad79c2f7 [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
1118/* fill in the dac_nids table from the parsed pin configuration */
1119static int fill_and_eval_dacs(struct hda_codec *codec,
1120 bool fill_hardwired,
1121 bool fill_mio_first)
1122{
1123 struct hda_gen_spec *spec = codec->spec;
1124 struct auto_pin_cfg *cfg = &spec->autocfg;
1125 int i, err, badness;
1126
1127 /* set num_dacs once to full for look_for_dac() */
1128 spec->multiout.num_dacs = cfg->line_outs;
1129 spec->multiout.dac_nids = spec->private_dac_nids;
1130 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1131 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1132 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1133 spec->multi_ios = 0;
1134 snd_array_free(&spec->paths);
1135 badness = 0;
1136
1137 /* fill hard-wired DACs first */
1138 if (fill_hardwired) {
1139 bool mapped;
1140 do {
1141 mapped = map_singles(codec, cfg->line_outs,
1142 cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001143 spec->private_dac_nids,
1144 spec->out_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001145 mapped |= map_singles(codec, cfg->hp_outs,
1146 cfg->hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001147 spec->multiout.hp_out_nid,
1148 spec->hp_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001149 mapped |= map_singles(codec, cfg->speaker_outs,
1150 cfg->speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001151 spec->multiout.extra_out_nid,
1152 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001153 if (fill_mio_first && cfg->line_outs == 1 &&
1154 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001155 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001156 if (!err)
1157 mapped = true;
1158 }
1159 } while (mapped);
1160 }
1161
1162 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001163 spec->private_dac_nids, spec->out_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001164 &main_out_badness);
1165
1166 /* re-count num_dacs and squash invalid entries */
1167 spec->multiout.num_dacs = 0;
1168 for (i = 0; i < cfg->line_outs; i++) {
1169 if (spec->private_dac_nids[i])
1170 spec->multiout.num_dacs++;
1171 else {
1172 memmove(spec->private_dac_nids + i,
1173 spec->private_dac_nids + i + 1,
1174 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1175 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1176 }
1177 }
1178
1179 if (fill_mio_first &&
1180 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1181 /* try to fill multi-io first */
Takashi Iwaie22aab72013-01-04 14:50:04 +01001182 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001183 if (err < 0)
1184 return err;
1185 /* we don't count badness at this stage yet */
1186 }
1187
1188 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1189 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1190 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001191 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001192 &extra_out_badness);
1193 if (err < 0)
1194 return err;
1195 badness += err;
1196 }
1197 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1198 err = try_assign_dacs(codec, cfg->speaker_outs,
1199 cfg->speaker_pins,
1200 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001201 spec->speaker_paths,
1202 &extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001203 if (err < 0)
1204 return err;
1205 badness += err;
1206 }
1207 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001208 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001209 if (err < 0)
1210 return err;
1211 badness += err;
1212 }
Takashi Iwaie22aab72013-01-04 14:50:04 +01001213
1214 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1215 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1216 spec->multi_ios = 1; /* give badness */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001217
1218 if (spec->multi_ios == 2) {
1219 for (i = 0; i < 2; i++)
1220 spec->private_dac_nids[spec->multiout.num_dacs++] =
1221 spec->multi_io[i].dac;
1222 spec->ext_channel_count = 2;
1223 } else if (spec->multi_ios) {
1224 spec->multi_ios = 0;
1225 badness += BAD_MULTI_IO;
1226 }
1227
1228 return badness;
1229}
1230
1231#define DEBUG_BADNESS
1232
1233#ifdef DEBUG_BADNESS
1234#define debug_badness snd_printdd
1235#else
1236#define debug_badness(...)
1237#endif
1238
1239static void debug_show_configs(struct hda_gen_spec *spec, struct auto_pin_cfg *cfg)
1240{
1241 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1242 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001243 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001244 spec->multiout.dac_nids[0],
1245 spec->multiout.dac_nids[1],
1246 spec->multiout.dac_nids[2],
1247 spec->multiout.dac_nids[3]);
1248 if (spec->multi_ios > 0)
1249 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1250 spec->multi_ios,
1251 spec->multi_io[0].pin, spec->multi_io[1].pin,
1252 spec->multi_io[0].dac, spec->multi_io[1].dac);
1253 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1254 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001255 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001256 spec->multiout.hp_out_nid[0],
1257 spec->multiout.hp_out_nid[1],
1258 spec->multiout.hp_out_nid[2],
1259 spec->multiout.hp_out_nid[3]);
1260 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1261 cfg->speaker_pins[0], cfg->speaker_pins[1],
1262 cfg->speaker_pins[2], cfg->speaker_pins[3],
1263 spec->multiout.extra_out_nid[0],
1264 spec->multiout.extra_out_nid[1],
1265 spec->multiout.extra_out_nid[2],
1266 spec->multiout.extra_out_nid[3]);
1267}
1268
1269/* find all available DACs of the codec */
1270static void fill_all_dac_nids(struct hda_codec *codec)
1271{
1272 struct hda_gen_spec *spec = codec->spec;
1273 int i;
1274 hda_nid_t nid = codec->start_nid;
1275
1276 spec->num_all_dacs = 0;
1277 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1278 for (i = 0; i < codec->num_nodes; i++, nid++) {
1279 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1280 continue;
1281 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1282 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1283 break;
1284 }
1285 spec->all_dacs[spec->num_all_dacs++] = nid;
1286 }
1287}
1288
1289static int parse_output_paths(struct hda_codec *codec)
1290{
1291 struct hda_gen_spec *spec = codec->spec;
1292 struct auto_pin_cfg *cfg = &spec->autocfg;
1293 struct auto_pin_cfg *best_cfg;
1294 int best_badness = INT_MAX;
1295 int badness;
1296 bool fill_hardwired = true, fill_mio_first = true;
1297 bool best_wired = true, best_mio = true;
1298 bool hp_spk_swapped = false;
1299
1300 fill_all_dac_nids(codec);
1301
1302 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1303 if (!best_cfg)
1304 return -ENOMEM;
1305 *best_cfg = *cfg;
1306
1307 for (;;) {
1308 badness = fill_and_eval_dacs(codec, fill_hardwired,
1309 fill_mio_first);
1310 if (badness < 0) {
1311 kfree(best_cfg);
1312 return badness;
1313 }
1314 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1315 cfg->line_out_type, fill_hardwired, fill_mio_first,
1316 badness);
1317 debug_show_configs(spec, cfg);
1318 if (badness < best_badness) {
1319 best_badness = badness;
1320 *best_cfg = *cfg;
1321 best_wired = fill_hardwired;
1322 best_mio = fill_mio_first;
1323 }
1324 if (!badness)
1325 break;
1326 fill_mio_first = !fill_mio_first;
1327 if (!fill_mio_first)
1328 continue;
1329 fill_hardwired = !fill_hardwired;
1330 if (!fill_hardwired)
1331 continue;
1332 if (hp_spk_swapped)
1333 break;
1334 hp_spk_swapped = true;
1335 if (cfg->speaker_outs > 0 &&
1336 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1337 cfg->hp_outs = cfg->line_outs;
1338 memcpy(cfg->hp_pins, cfg->line_out_pins,
1339 sizeof(cfg->hp_pins));
1340 cfg->line_outs = cfg->speaker_outs;
1341 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1342 sizeof(cfg->speaker_pins));
1343 cfg->speaker_outs = 0;
1344 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1345 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1346 fill_hardwired = true;
1347 continue;
1348 }
1349 if (cfg->hp_outs > 0 &&
1350 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1351 cfg->speaker_outs = cfg->line_outs;
1352 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1353 sizeof(cfg->speaker_pins));
1354 cfg->line_outs = cfg->hp_outs;
1355 memcpy(cfg->line_out_pins, cfg->hp_pins,
1356 sizeof(cfg->hp_pins));
1357 cfg->hp_outs = 0;
1358 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1359 cfg->line_out_type = AUTO_PIN_HP_OUT;
1360 fill_hardwired = true;
1361 continue;
1362 }
1363 break;
1364 }
1365
1366 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001367 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001368 *cfg = *best_cfg;
1369 fill_and_eval_dacs(codec, best_wired, best_mio);
1370 }
1371 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1372 cfg->line_out_type, best_wired, best_mio);
1373 debug_show_configs(spec, cfg);
1374
1375 if (cfg->line_out_pins[0]) {
1376 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01001377 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001378 if (path)
1379 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
1380 }
1381
1382 kfree(best_cfg);
1383 return 0;
1384}
1385
1386/* add playback controls from the parsed DAC table */
1387static int create_multi_out_ctls(struct hda_codec *codec,
1388 const struct auto_pin_cfg *cfg)
1389{
1390 struct hda_gen_spec *spec = codec->spec;
1391 int i, err, noutputs;
1392
1393 noutputs = cfg->line_outs;
1394 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1395 noutputs += spec->multi_ios;
1396
1397 for (i = 0; i < noutputs; i++) {
1398 const char *name;
1399 int index;
Takashi Iwai196c17662013-01-04 15:01:40 +01001400 hda_nid_t dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001401 struct nid_path *path;
1402
1403 dac = spec->multiout.dac_nids[i];
1404 if (!dac)
1405 continue;
1406 if (i >= cfg->line_outs) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001407 index = 0;
1408 name = channel_name[i];
1409 } else {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001410 name = get_line_out_pfx(spec, i, true, &index);
1411 }
1412
Takashi Iwai196c17662013-01-04 15:01:40 +01001413 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001414 if (!path)
1415 continue;
1416 if (!name || !strcmp(name, "CLFE")) {
1417 /* Center/LFE */
1418 err = add_vol_ctl(codec, "Center", 0, 1, path);
1419 if (err < 0)
1420 return err;
1421 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1422 if (err < 0)
1423 return err;
1424 err = add_sw_ctl(codec, "Center", 0, 1, path);
1425 if (err < 0)
1426 return err;
1427 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1428 if (err < 0)
1429 return err;
1430 } else {
1431 err = add_stereo_vol(codec, name, index, path);
1432 if (err < 0)
1433 return err;
1434 err = add_stereo_sw(codec, name, index, path);
1435 if (err < 0)
1436 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 }
1438 }
1439 return 0;
1440}
1441
Takashi Iwai352f7f92012-12-19 12:52:06 +01001442static int create_extra_out(struct hda_codec *codec, hda_nid_t pin,
Takashi Iwai196c17662013-01-04 15:01:40 +01001443 hda_nid_t dac, int path_idx,
1444 const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001446 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 int err;
1448
Takashi Iwai196c17662013-01-04 15:01:40 +01001449 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001450 if (!path)
1451 return 0;
1452 /* bind volume control will be created in the case of dac = 0 */
1453 if (dac) {
1454 err = add_stereo_vol(codec, pfx, cidx, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 if (err < 0)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001456 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001458 err = add_stereo_sw(codec, pfx, cidx, path);
1459 if (err < 0)
1460 return err;
1461 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462}
1463
Takashi Iwai352f7f92012-12-19 12:52:06 +01001464/* add playback controls for speaker and HP outputs */
1465static int create_extra_outs(struct hda_codec *codec, int num_pins,
1466 const hda_nid_t *pins, const hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001467 const int *paths, const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001469 struct hda_gen_spec *spec = codec->spec;
1470 struct hda_bind_ctls *ctl;
1471 char name[32];
1472 int i, n, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Takashi Iwai352f7f92012-12-19 12:52:06 +01001474 if (!num_pins || !pins[0])
1475 return 0;
1476
1477 if (num_pins == 1) {
1478 hda_nid_t dac = *dacs;
1479 if (!dac)
1480 dac = spec->multiout.dac_nids[0];
Takashi Iwai196c17662013-01-04 15:01:40 +01001481 return create_extra_out(codec, *pins, dac, paths[0], pfx, 0);
Takashi Iwai97ec5582006-03-21 11:29:07 +01001482 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001483
1484 for (i = 0; i < num_pins; i++) {
1485 hda_nid_t dac;
1486 if (dacs[num_pins - 1])
1487 dac = dacs[i]; /* with individual volumes */
Takashi Iwai97ec5582006-03-21 11:29:07 +01001488 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01001489 dac = 0;
1490 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker")) {
Takashi Iwai196c17662013-01-04 15:01:40 +01001491 err = create_extra_out(codec, pins[i], dac, paths[i],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001492 "Bass Speaker", 0);
1493 } else if (num_pins >= 3) {
1494 snprintf(name, sizeof(name), "%s %s",
1495 pfx, channel_name[i]);
Takashi Iwai196c17662013-01-04 15:01:40 +01001496 err = create_extra_out(codec, pins[i], dac, paths[i],
1497 name, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001498 } else {
Takashi Iwai196c17662013-01-04 15:01:40 +01001499 err = create_extra_out(codec, pins[i], dac, paths[i],
1500 pfx, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001502 if (err < 0)
1503 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001505 if (dacs[num_pins - 1])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 return 0;
1507
Takashi Iwai352f7f92012-12-19 12:52:06 +01001508 /* Let's create a bind-controls for volumes */
1509 ctl = new_bind_ctl(codec, num_pins, &snd_hda_bind_vol);
1510 if (!ctl)
1511 return -ENOMEM;
1512 n = 0;
1513 for (i = 0; i < num_pins; i++) {
1514 hda_nid_t vol;
1515 struct nid_path *path;
1516 if (!pins[i] || !dacs[i])
1517 continue;
Takashi Iwai196c17662013-01-04 15:01:40 +01001518 path = snd_hda_get_path_from_idx(codec, paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001519 if (!path)
1520 continue;
1521 vol = look_for_out_vol_nid(codec, path);
1522 if (vol)
1523 ctl->values[n++] =
1524 HDA_COMPOSE_AMP_VAL(vol, 3, 0, HDA_OUTPUT);
1525 }
1526 if (n) {
1527 snprintf(name, sizeof(name), "%s Playback Volume", pfx);
1528 err = add_control(spec, HDA_CTL_BIND_VOL, name, 0, (long)ctl);
1529 if (err < 0)
1530 return err;
1531 }
1532 return 0;
1533}
Takashi Iwai97ec5582006-03-21 11:29:07 +01001534
Takashi Iwai352f7f92012-12-19 12:52:06 +01001535static int create_hp_out_ctls(struct hda_codec *codec)
1536{
1537 struct hda_gen_spec *spec = codec->spec;
1538 return create_extra_outs(codec, spec->autocfg.hp_outs,
1539 spec->autocfg.hp_pins,
1540 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001541 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001542 "Headphone");
1543}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
Takashi Iwai352f7f92012-12-19 12:52:06 +01001545static int create_speaker_out_ctls(struct hda_codec *codec)
1546{
1547 struct hda_gen_spec *spec = codec->spec;
1548 return create_extra_outs(codec, spec->autocfg.speaker_outs,
1549 spec->autocfg.speaker_pins,
1550 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001551 spec->speaker_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001552 "Speaker");
1553}
1554
1555/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001556 * independent HP controls
1557 */
1558
1559static int indep_hp_info(struct snd_kcontrol *kcontrol,
1560 struct snd_ctl_elem_info *uinfo)
1561{
1562 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
1563}
1564
1565static int indep_hp_get(struct snd_kcontrol *kcontrol,
1566 struct snd_ctl_elem_value *ucontrol)
1567{
1568 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1569 struct hda_gen_spec *spec = codec->spec;
1570 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
1571 return 0;
1572}
1573
1574static int indep_hp_put(struct snd_kcontrol *kcontrol,
1575 struct snd_ctl_elem_value *ucontrol)
1576{
1577 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1578 struct hda_gen_spec *spec = codec->spec;
1579 unsigned int select = ucontrol->value.enumerated.item[0];
1580 int ret = 0;
1581
1582 mutex_lock(&spec->pcm_mutex);
1583 if (spec->active_streams) {
1584 ret = -EBUSY;
1585 goto unlock;
1586 }
1587
1588 if (spec->indep_hp_enabled != select) {
1589 spec->indep_hp_enabled = select;
1590 if (spec->indep_hp_enabled)
1591 spec->multiout.hp_out_nid[0] = 0;
1592 else
1593 spec->multiout.hp_out_nid[0] = spec->alt_dac_nid;
1594 ret = 1;
1595 }
1596 unlock:
1597 mutex_unlock(&spec->pcm_mutex);
1598 return ret;
1599}
1600
1601static const struct snd_kcontrol_new indep_hp_ctl = {
1602 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1603 .name = "Independent HP",
1604 .info = indep_hp_info,
1605 .get = indep_hp_get,
1606 .put = indep_hp_put,
1607};
1608
1609
1610static int create_indep_hp_ctls(struct hda_codec *codec)
1611{
1612 struct hda_gen_spec *spec = codec->spec;
1613
1614 if (!spec->indep_hp)
1615 return 0;
1616 if (!spec->multiout.hp_out_nid[0]) {
1617 spec->indep_hp = 0;
1618 return 0;
1619 }
1620
1621 spec->indep_hp_enabled = false;
1622 spec->alt_dac_nid = spec->multiout.hp_out_nid[0];
1623 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
1624 return -ENOMEM;
1625 return 0;
1626}
1627
1628/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01001629 * channel mode enum control
1630 */
1631
1632static int ch_mode_info(struct snd_kcontrol *kcontrol,
1633 struct snd_ctl_elem_info *uinfo)
1634{
1635 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1636 struct hda_gen_spec *spec = codec->spec;
1637
1638 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1639 uinfo->count = 1;
1640 uinfo->value.enumerated.items = spec->multi_ios + 1;
1641 if (uinfo->value.enumerated.item > spec->multi_ios)
1642 uinfo->value.enumerated.item = spec->multi_ios;
1643 sprintf(uinfo->value.enumerated.name, "%dch",
1644 (uinfo->value.enumerated.item + 1) * 2);
1645 return 0;
1646}
1647
1648static int ch_mode_get(struct snd_kcontrol *kcontrol,
1649 struct snd_ctl_elem_value *ucontrol)
1650{
1651 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1652 struct hda_gen_spec *spec = codec->spec;
1653 ucontrol->value.enumerated.item[0] = (spec->ext_channel_count - 1) / 2;
1654 return 0;
1655}
1656
Takashi Iwai196c17662013-01-04 15:01:40 +01001657static inline struct nid_path *
1658get_multiio_path(struct hda_codec *codec, int idx)
1659{
1660 struct hda_gen_spec *spec = codec->spec;
1661 return snd_hda_get_path_from_idx(codec,
1662 spec->out_paths[spec->autocfg.line_outs + idx]);
1663}
1664
Takashi Iwai352f7f92012-12-19 12:52:06 +01001665static int set_multi_io(struct hda_codec *codec, int idx, bool output)
1666{
1667 struct hda_gen_spec *spec = codec->spec;
1668 hda_nid_t nid = spec->multi_io[idx].pin;
1669 struct nid_path *path;
1670
Takashi Iwai196c17662013-01-04 15:01:40 +01001671 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001672 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001674
1675 if (path->active == output)
1676 return 0;
1677
1678 if (output) {
1679 snd_hda_set_pin_ctl_cache(codec, nid, PIN_OUT);
1680 snd_hda_activate_path(codec, path, true, true);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01001681 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001682 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01001683 set_pin_eapd(codec, nid, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001684 snd_hda_activate_path(codec, path, false, true);
1685 snd_hda_set_pin_ctl_cache(codec, nid,
1686 spec->multi_io[idx].ctl_in);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001688 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689}
1690
Takashi Iwai352f7f92012-12-19 12:52:06 +01001691static int ch_mode_put(struct snd_kcontrol *kcontrol,
1692 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001694 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1695 struct hda_gen_spec *spec = codec->spec;
1696 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
Takashi Iwai352f7f92012-12-19 12:52:06 +01001698 ch = ucontrol->value.enumerated.item[0];
1699 if (ch < 0 || ch > spec->multi_ios)
1700 return -EINVAL;
1701 if (ch == (spec->ext_channel_count - 1) / 2)
1702 return 0;
1703 spec->ext_channel_count = (ch + 1) * 2;
1704 for (i = 0; i < spec->multi_ios; i++)
1705 set_multi_io(codec, i, i < ch);
1706 spec->multiout.max_channels = max(spec->ext_channel_count,
1707 spec->const_channel_count);
1708 if (spec->need_dac_fix)
1709 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 return 1;
1711}
1712
Takashi Iwai352f7f92012-12-19 12:52:06 +01001713static const struct snd_kcontrol_new channel_mode_enum = {
1714 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1715 .name = "Channel Mode",
1716 .info = ch_mode_info,
1717 .get = ch_mode_get,
1718 .put = ch_mode_put,
1719};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
Takashi Iwai352f7f92012-12-19 12:52:06 +01001721static int create_multi_channel_mode(struct hda_codec *codec)
1722{
1723 struct hda_gen_spec *spec = codec->spec;
1724
1725 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01001726 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01001727 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 return 0;
1730}
1731
Takashi Iwai352f7f92012-12-19 12:52:06 +01001732/*
1733 * shared headphone/mic handling
1734 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02001735
Takashi Iwai352f7f92012-12-19 12:52:06 +01001736static void call_update_outputs(struct hda_codec *codec);
1737
1738/* for shared I/O, change the pin-control accordingly */
1739static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic)
1740{
1741 struct hda_gen_spec *spec = codec->spec;
1742 unsigned int val;
1743 hda_nid_t pin = spec->autocfg.inputs[1].pin;
1744 /* NOTE: this assumes that there are only two inputs, the
1745 * first is the real internal mic and the second is HP/mic jack.
1746 */
1747
1748 val = snd_hda_get_default_vref(codec, pin);
1749
1750 /* This pin does not have vref caps - let's enable vref on pin 0x18
1751 instead, as suggested by Realtek */
1752 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
1753 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
1754 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
1755 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01001756 snd_hda_set_pin_ctl_cache(codec, vref_pin,
1757 PIN_IN | (set_as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02001758 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001759
1760 val = set_as_mic ? val | PIN_IN : PIN_HP;
Takashi Iwai7594aa32012-12-20 15:38:40 +01001761 snd_hda_set_pin_ctl_cache(codec, pin, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001762
1763 spec->automute_speaker = !set_as_mic;
1764 call_update_outputs(codec);
1765}
1766
1767/* create a shared input with the headphone out */
1768static int create_shared_input(struct hda_codec *codec)
1769{
1770 struct hda_gen_spec *spec = codec->spec;
1771 struct auto_pin_cfg *cfg = &spec->autocfg;
1772 unsigned int defcfg;
1773 hda_nid_t nid;
1774
1775 /* only one internal input pin? */
1776 if (cfg->num_inputs != 1)
1777 return 0;
1778 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
1779 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
1780 return 0;
1781
1782 if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1783 nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
1784 else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
1785 nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
1786 else
1787 return 0; /* both not available */
1788
1789 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
1790 return 0; /* no input */
1791
1792 cfg->inputs[1].pin = nid;
1793 cfg->inputs[1].type = AUTO_PIN_MIC;
1794 cfg->num_inputs = 2;
1795 spec->shared_mic_hp = 1;
1796 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
1797 return 0;
1798}
1799
1800
1801/*
1802 * Parse input paths
1803 */
1804
1805#ifdef CONFIG_PM
1806/* add the powersave loopback-list entry */
1807static void add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
1808{
1809 struct hda_amp_list *list;
1810
1811 if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
1812 return;
1813 list = spec->loopback_list + spec->num_loopbacks;
1814 list->nid = mix;
1815 list->dir = HDA_INPUT;
1816 list->idx = idx;
1817 spec->num_loopbacks++;
Takashi Iwaicb53c622007-08-10 17:21:45 +02001818 spec->loopback.amplist = spec->loopback_list;
1819}
1820#else
Takashi Iwai352f7f92012-12-19 12:52:06 +01001821#define add_loopback_list(spec, mix, idx) /* NOP */
Takashi Iwaicb53c622007-08-10 17:21:45 +02001822#endif
1823
Takashi Iwai352f7f92012-12-19 12:52:06 +01001824/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01001825static int new_analog_input(struct hda_codec *codec, int input_idx,
1826 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001827 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001829 struct hda_gen_spec *spec = codec->spec;
1830 struct nid_path *path;
1831 unsigned int val;
1832 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
Takashi Iwai352f7f92012-12-19 12:52:06 +01001834 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
1835 !nid_has_mute(codec, mix_nid, HDA_INPUT))
1836 return 0; /* no need for analog loopback */
1837
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01001838 path = snd_hda_add_new_path(codec, pin, mix_nid, HDA_PARSE_ALL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001839 if (!path)
1840 return -EINVAL;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001841 print_nid_path("loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01001842 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001843
1844 idx = path->idx[path->depth - 1];
1845 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
1846 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1847 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001848 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001850 path->ctls[NID_PATH_VOL_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 }
1852
Takashi Iwai352f7f92012-12-19 12:52:06 +01001853 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
1854 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1855 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001856 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001858 path->ctls[NID_PATH_MUTE_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 }
1860
Takashi Iwai352f7f92012-12-19 12:52:06 +01001861 path->active = true;
1862 add_loopback_list(spec, mix_nid, idx);
1863 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864}
1865
Takashi Iwai352f7f92012-12-19 12:52:06 +01001866static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001868 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
1869 return (pincap & AC_PINCAP_IN) != 0;
1870}
1871
1872/* Parse the codec tree and retrieve ADCs */
1873static int fill_adc_nids(struct hda_codec *codec)
1874{
1875 struct hda_gen_spec *spec = codec->spec;
1876 hda_nid_t nid;
1877 hda_nid_t *adc_nids = spec->adc_nids;
1878 int max_nums = ARRAY_SIZE(spec->adc_nids);
1879 int i, nums = 0;
1880
1881 nid = codec->start_nid;
1882 for (i = 0; i < codec->num_nodes; i++, nid++) {
1883 unsigned int caps = get_wcaps(codec, nid);
1884 int type = get_wcaps_type(caps);
1885
1886 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
1887 continue;
1888 adc_nids[nums] = nid;
1889 if (++nums >= max_nums)
1890 break;
1891 }
1892 spec->num_adc_nids = nums;
1893 return nums;
1894}
1895
1896/* filter out invalid adc_nids that don't give all active input pins;
1897 * if needed, check whether dynamic ADC-switching is available
1898 */
1899static int check_dyn_adc_switch(struct hda_codec *codec)
1900{
1901 struct hda_gen_spec *spec = codec->spec;
1902 struct hda_input_mux *imux = &spec->input_mux;
1903 hda_nid_t adc_nids[ARRAY_SIZE(spec->adc_nids)];
1904 int i, n, nums;
1905 hda_nid_t pin, adc;
1906
1907 again:
1908 nums = 0;
1909 for (n = 0; n < spec->num_adc_nids; n++) {
1910 adc = spec->adc_nids[n];
1911 for (i = 0; i < imux->num_items; i++) {
1912 pin = spec->imux_pins[i];
1913 if (!is_reachable_path(codec, pin, adc))
1914 break;
1915 }
1916 if (i >= imux->num_items)
1917 adc_nids[nums++] = adc;
1918 }
1919
1920 if (!nums) {
1921 if (spec->shared_mic_hp) {
1922 spec->shared_mic_hp = 0;
1923 imux->num_items = 1;
1924 goto again;
1925 }
1926
1927 /* check whether ADC-switch is possible */
1928 for (i = 0; i < imux->num_items; i++) {
1929 pin = spec->imux_pins[i];
1930 for (n = 0; n < spec->num_adc_nids; n++) {
1931 adc = spec->adc_nids[n];
1932 if (is_reachable_path(codec, pin, adc)) {
1933 spec->dyn_adc_idx[i] = n;
1934 break;
1935 }
1936 }
1937 }
1938
1939 snd_printdd("hda-codec: enabling ADC switching\n");
1940 spec->dyn_adc_switch = 1;
1941 } else if (nums != spec->num_adc_nids) {
1942 memcpy(spec->adc_nids, adc_nids, nums * sizeof(hda_nid_t));
1943 spec->num_adc_nids = nums;
1944 }
1945
1946 if (imux->num_items == 1 || spec->shared_mic_hp) {
1947 snd_printdd("hda-codec: reducing to a single ADC\n");
1948 spec->num_adc_nids = 1; /* reduce to a single ADC */
1949 }
1950
1951 /* single index for individual volumes ctls */
1952 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
1953 spec->num_adc_nids = 1;
1954
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 return 0;
1956}
1957
1958/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01001959 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001961static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02001962{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001963 struct hda_gen_spec *spec = codec->spec;
1964 const struct auto_pin_cfg *cfg = &spec->autocfg;
1965 hda_nid_t mixer = spec->mixer_nid;
1966 struct hda_input_mux *imux = &spec->input_mux;
1967 int num_adcs;
1968 int i, c, err, type_idx = 0;
1969 const char *prev_label = NULL;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02001970
Takashi Iwai352f7f92012-12-19 12:52:06 +01001971 num_adcs = fill_adc_nids(codec);
1972 if (num_adcs < 0)
1973 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02001974
Takashi Iwai352f7f92012-12-19 12:52:06 +01001975 for (i = 0; i < cfg->num_inputs; i++) {
1976 hda_nid_t pin;
1977 const char *label;
1978 bool imux_added;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
Takashi Iwai352f7f92012-12-19 12:52:06 +01001980 pin = cfg->inputs[i].pin;
1981 if (!is_input_pin(codec, pin))
1982 continue;
1983
1984 label = hda_get_autocfg_input_label(codec, cfg, i);
1985 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
1986 label = "Headphone Mic";
1987 if (prev_label && !strcmp(label, prev_label))
1988 type_idx++;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02001989 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01001990 type_idx = 0;
1991 prev_label = label;
1992
1993 if (mixer) {
1994 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01001995 err = new_analog_input(codec, i, pin,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001996 label, type_idx, mixer);
1997 if (err < 0)
1998 return err;
1999 }
2000 }
2001
2002 imux_added = false;
2003 for (c = 0; c < num_adcs; c++) {
2004 struct nid_path *path;
2005 hda_nid_t adc = spec->adc_nids[c];
2006
2007 if (!is_reachable_path(codec, pin, adc))
2008 continue;
2009 path = snd_array_new(&spec->paths);
2010 if (!path)
2011 return -ENOMEM;
2012 memset(path, 0, sizeof(*path));
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01002013 if (!snd_hda_parse_nid_path(codec, pin, adc, HDA_PARSE_ALL, path)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002014 snd_printd(KERN_ERR
2015 "invalid input path 0x%x -> 0x%x\n",
2016 pin, adc);
2017 spec->paths.used--;
2018 continue;
2019 }
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002020 print_nid_path("input", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002021
2022 if (!imux_added) {
2023 spec->imux_pins[imux->num_items] = pin;
2024 snd_hda_add_imux_item(imux, label,
2025 imux->num_items, NULL);
2026 imux_added = true;
2027 }
2028 }
2029 }
2030
2031 return 0;
2032}
2033
2034
2035/*
2036 * input source mux
2037 */
2038
2039/* get the ADC NID corresponding to the given index */
2040static hda_nid_t get_adc_nid(struct hda_codec *codec, int adc_idx, int imux_idx)
2041{
2042 struct hda_gen_spec *spec = codec->spec;
2043 if (spec->dyn_adc_switch)
2044 adc_idx = spec->dyn_adc_idx[imux_idx];
2045 return spec->adc_nids[adc_idx];
2046}
2047
2048static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2049 unsigned int idx);
2050
2051static int mux_enum_info(struct snd_kcontrol *kcontrol,
2052 struct snd_ctl_elem_info *uinfo)
2053{
2054 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2055 struct hda_gen_spec *spec = codec->spec;
2056 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
2057}
2058
2059static int mux_enum_get(struct snd_kcontrol *kcontrol,
2060 struct snd_ctl_elem_value *ucontrol)
2061{
2062 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2063 struct hda_gen_spec *spec = codec->spec;
2064 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2065
2066 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
2067 return 0;
2068}
2069
2070static int mux_enum_put(struct snd_kcontrol *kcontrol,
2071 struct snd_ctl_elem_value *ucontrol)
2072{
2073 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2074 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2075 return mux_select(codec, adc_idx,
2076 ucontrol->value.enumerated.item[0]);
2077}
2078
Takashi Iwai352f7f92012-12-19 12:52:06 +01002079static const struct snd_kcontrol_new cap_src_temp = {
2080 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2081 .name = "Input Source",
2082 .info = mux_enum_info,
2083 .get = mux_enum_get,
2084 .put = mux_enum_put,
2085};
2086
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002087/*
2088 * capture volume and capture switch ctls
2089 */
2090
Takashi Iwai352f7f92012-12-19 12:52:06 +01002091typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
2092 struct snd_ctl_elem_value *ucontrol);
2093
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002094/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002095static int cap_put_caller(struct snd_kcontrol *kcontrol,
2096 struct snd_ctl_elem_value *ucontrol,
2097 put_call_t func, int type)
2098{
2099 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2100 struct hda_gen_spec *spec = codec->spec;
2101 const struct hda_input_mux *imux;
2102 struct nid_path *path;
2103 int i, adc_idx, err = 0;
2104
2105 imux = &spec->input_mux;
2106 adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2107 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002108 /* we use the cache-only update at first since multiple input paths
2109 * may shared the same amp; by updating only caches, the redundant
2110 * writes to hardware can be reduced.
2111 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002112 codec->cached_write = 1;
2113 for (i = 0; i < imux->num_items; i++) {
2114 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2115 get_adc_nid(codec, adc_idx, i));
2116 if (!path->ctls[type])
2117 continue;
2118 kcontrol->private_value = path->ctls[type];
2119 err = func(kcontrol, ucontrol);
2120 if (err < 0)
2121 goto error;
2122 }
2123 error:
2124 codec->cached_write = 0;
2125 mutex_unlock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002126 snd_hda_codec_flush_amp_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002127 if (err >= 0 && spec->cap_sync_hook)
2128 spec->cap_sync_hook(codec);
2129 return err;
2130}
2131
2132/* capture volume ctl callbacks */
2133#define cap_vol_info snd_hda_mixer_amp_volume_info
2134#define cap_vol_get snd_hda_mixer_amp_volume_get
2135#define cap_vol_tlv snd_hda_mixer_amp_tlv
2136
2137static int cap_vol_put(struct snd_kcontrol *kcontrol,
2138 struct snd_ctl_elem_value *ucontrol)
2139{
2140 return cap_put_caller(kcontrol, ucontrol,
2141 snd_hda_mixer_amp_volume_put,
2142 NID_PATH_VOL_CTL);
2143}
2144
2145static const struct snd_kcontrol_new cap_vol_temp = {
2146 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2147 .name = "Capture Volume",
2148 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
2149 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2150 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
2151 .info = cap_vol_info,
2152 .get = cap_vol_get,
2153 .put = cap_vol_put,
2154 .tlv = { .c = cap_vol_tlv },
2155};
2156
2157/* capture switch ctl callbacks */
2158#define cap_sw_info snd_ctl_boolean_stereo_info
2159#define cap_sw_get snd_hda_mixer_amp_switch_get
2160
2161static int cap_sw_put(struct snd_kcontrol *kcontrol,
2162 struct snd_ctl_elem_value *ucontrol)
2163{
2164 return cap_put_caller(kcontrol, ucontrol,
2165 snd_hda_mixer_amp_switch_put,
2166 NID_PATH_MUTE_CTL);
2167}
2168
2169static const struct snd_kcontrol_new cap_sw_temp = {
2170 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2171 .name = "Capture Switch",
2172 .info = cap_sw_info,
2173 .get = cap_sw_get,
2174 .put = cap_sw_put,
2175};
2176
2177static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
2178{
2179 hda_nid_t nid;
2180 int i, depth;
2181
2182 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
2183 for (depth = 0; depth < 3; depth++) {
2184 if (depth >= path->depth)
2185 return -EINVAL;
2186 i = path->depth - depth - 1;
2187 nid = path->path[i];
2188 if (!path->ctls[NID_PATH_VOL_CTL]) {
2189 if (nid_has_volume(codec, nid, HDA_OUTPUT))
2190 path->ctls[NID_PATH_VOL_CTL] =
2191 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2192 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
2193 int idx = path->idx[i];
2194 if (!depth && codec->single_adc_amp)
2195 idx = 0;
2196 path->ctls[NID_PATH_VOL_CTL] =
2197 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2198 }
2199 }
2200 if (!path->ctls[NID_PATH_MUTE_CTL]) {
2201 if (nid_has_mute(codec, nid, HDA_OUTPUT))
2202 path->ctls[NID_PATH_MUTE_CTL] =
2203 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2204 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
2205 int idx = path->idx[i];
2206 if (!depth && codec->single_adc_amp)
2207 idx = 0;
2208 path->ctls[NID_PATH_MUTE_CTL] =
2209 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2210 }
2211 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01002212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 return 0;
2214}
2215
Takashi Iwai352f7f92012-12-19 12:52:06 +01002216static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002218 struct hda_gen_spec *spec = codec->spec;
2219 struct auto_pin_cfg *cfg = &spec->autocfg;
2220 unsigned int val;
2221 int i;
2222
2223 if (!spec->inv_dmic_split)
2224 return false;
2225 for (i = 0; i < cfg->num_inputs; i++) {
2226 if (cfg->inputs[i].pin != nid)
2227 continue;
2228 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2229 return false;
2230 val = snd_hda_codec_get_pincfg(codec, nid);
2231 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
2232 }
2233 return false;
2234}
2235
2236static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
2237 int idx, bool is_switch, unsigned int ctl,
2238 bool inv_dmic)
2239{
2240 struct hda_gen_spec *spec = codec->spec;
2241 char tmpname[44];
2242 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
2243 const char *sfx = is_switch ? "Switch" : "Volume";
2244 unsigned int chs = inv_dmic ? 1 : 3;
2245 int err;
2246
2247 if (!ctl)
2248 return 0;
2249
2250 if (label)
2251 snprintf(tmpname, sizeof(tmpname),
2252 "%s Capture %s", label, sfx);
2253 else
2254 snprintf(tmpname, sizeof(tmpname),
2255 "Capture %s", sfx);
2256 err = add_control(spec, type, tmpname, idx,
2257 amp_val_replace_channels(ctl, chs));
2258 if (err < 0 || !inv_dmic)
2259 return err;
2260
2261 /* Make independent right kcontrol */
2262 if (label)
2263 snprintf(tmpname, sizeof(tmpname),
2264 "Inverted %s Capture %s", label, sfx);
2265 else
2266 snprintf(tmpname, sizeof(tmpname),
2267 "Inverted Capture %s", sfx);
2268 return add_control(spec, type, tmpname, idx,
2269 amp_val_replace_channels(ctl, 2));
2270}
2271
2272/* create single (and simple) capture volume and switch controls */
2273static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
2274 unsigned int vol_ctl, unsigned int sw_ctl,
2275 bool inv_dmic)
2276{
2277 int err;
2278 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
2279 if (err < 0)
2280 return err;
2281 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
2282 if (err < 0)
2283 return err;
2284 return 0;
2285}
2286
2287/* create bound capture volume and switch controls */
2288static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
2289 unsigned int vol_ctl, unsigned int sw_ctl)
2290{
2291 struct hda_gen_spec *spec = codec->spec;
2292 struct snd_kcontrol_new *knew;
2293
2294 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002295 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002296 if (!knew)
2297 return -ENOMEM;
2298 knew->index = idx;
2299 knew->private_value = vol_ctl;
2300 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2301 }
2302 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002303 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002304 if (!knew)
2305 return -ENOMEM;
2306 knew->index = idx;
2307 knew->private_value = sw_ctl;
2308 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2309 }
2310 return 0;
2311}
2312
2313/* return the vol ctl when used first in the imux list */
2314static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
2315{
2316 struct hda_gen_spec *spec = codec->spec;
2317 struct nid_path *path;
2318 unsigned int ctl;
2319 int i;
2320
2321 path = snd_hda_get_nid_path(codec, spec->imux_pins[idx],
2322 get_adc_nid(codec, 0, idx));
2323 if (!path)
2324 return 0;
2325 ctl = path->ctls[type];
2326 if (!ctl)
2327 return 0;
2328 for (i = 0; i < idx - 1; i++) {
2329 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2330 get_adc_nid(codec, 0, i));
2331 if (path && path->ctls[type] == ctl)
2332 return 0;
2333 }
2334 return ctl;
2335}
2336
2337/* create individual capture volume and switch controls per input */
2338static int create_multi_cap_vol_ctl(struct hda_codec *codec)
2339{
2340 struct hda_gen_spec *spec = codec->spec;
2341 struct hda_input_mux *imux = &spec->input_mux;
2342 int i, err, type, type_idx = 0;
2343 const char *prev_label = NULL;
2344
2345 for (i = 0; i < imux->num_items; i++) {
2346 const char *label;
2347 bool inv_dmic;
2348 label = hda_get_autocfg_input_label(codec, &spec->autocfg, i);
2349 if (prev_label && !strcmp(label, prev_label))
2350 type_idx++;
2351 else
2352 type_idx = 0;
2353 prev_label = label;
2354 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
2355
2356 for (type = 0; type < 2; type++) {
2357 err = add_single_cap_ctl(codec, label, type_idx, type,
2358 get_first_cap_ctl(codec, i, type),
2359 inv_dmic);
2360 if (err < 0)
2361 return err;
2362 }
2363 }
2364 return 0;
2365}
2366
2367static int create_capture_mixers(struct hda_codec *codec)
2368{
2369 struct hda_gen_spec *spec = codec->spec;
2370 struct hda_input_mux *imux = &spec->input_mux;
2371 int i, n, nums, err;
2372
2373 if (spec->dyn_adc_switch)
2374 nums = 1;
2375 else
2376 nums = spec->num_adc_nids;
2377
2378 if (!spec->auto_mic && imux->num_items > 1) {
2379 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01002380 const char *name;
2381 name = nums > 1 ? "Input Source" : "Capture Source";
2382 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002383 if (!knew)
2384 return -ENOMEM;
2385 knew->count = nums;
2386 }
2387
2388 for (n = 0; n < nums; n++) {
2389 bool multi = false;
2390 bool inv_dmic = false;
2391 int vol, sw;
2392
2393 vol = sw = 0;
2394 for (i = 0; i < imux->num_items; i++) {
2395 struct nid_path *path;
2396 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2397 get_adc_nid(codec, n, i));
2398 if (!path)
2399 continue;
2400 parse_capvol_in_path(codec, path);
2401 if (!vol)
2402 vol = path->ctls[NID_PATH_VOL_CTL];
2403 else if (vol != path->ctls[NID_PATH_VOL_CTL])
2404 multi = true;
2405 if (!sw)
2406 sw = path->ctls[NID_PATH_MUTE_CTL];
2407 else if (sw != path->ctls[NID_PATH_MUTE_CTL])
2408 multi = true;
2409 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
2410 inv_dmic = true;
2411 }
2412
2413 if (!multi)
2414 err = create_single_cap_vol_ctl(codec, n, vol, sw,
2415 inv_dmic);
2416 else if (!spec->multi_cap_vol)
2417 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
2418 else
2419 err = create_multi_cap_vol_ctl(codec);
2420 if (err < 0)
2421 return err;
2422 }
2423
2424 return 0;
2425}
2426
2427/*
2428 * add mic boosts if needed
2429 */
2430static int parse_mic_boost(struct hda_codec *codec)
2431{
2432 struct hda_gen_spec *spec = codec->spec;
2433 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002434 int i, err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002435 int type_idx = 0;
2436 hda_nid_t nid;
2437 const char *prev_label = NULL;
2438
2439 for (i = 0; i < cfg->num_inputs; i++) {
2440 if (cfg->inputs[i].type > AUTO_PIN_MIC)
2441 break;
2442 nid = cfg->inputs[i].pin;
2443 if (get_wcaps(codec, nid) & AC_WCAP_IN_AMP) {
2444 const char *label;
2445 char boost_label[32];
2446 struct nid_path *path;
2447 unsigned int val;
2448
2449 label = hda_get_autocfg_input_label(codec, cfg, i);
2450 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
2451 label = "Headphone Mic";
2452 if (prev_label && !strcmp(label, prev_label))
2453 type_idx++;
2454 else
2455 type_idx = 0;
2456 prev_label = label;
2457
2458 snprintf(boost_label, sizeof(boost_label),
2459 "%s Boost Volume", label);
2460 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
2461 err = add_control(spec, HDA_CTL_WIDGET_VOL,
2462 boost_label, type_idx, val);
2463 if (err < 0)
2464 return err;
2465
2466 path = snd_hda_get_nid_path(codec, nid, 0);
2467 if (path)
2468 path->ctls[NID_PATH_BOOST_CTL] = val;
2469 }
2470 }
2471 return 0;
2472}
2473
2474/*
2475 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
2476 */
2477static void parse_digital(struct hda_codec *codec)
2478{
2479 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002480 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002481 int i, nums;
2482 hda_nid_t dig_nid;
2483
2484 /* support multiple SPDIFs; the secondary is set up as a slave */
2485 nums = 0;
2486 for (i = 0; i < spec->autocfg.dig_outs; i++) {
2487 hda_nid_t pin = spec->autocfg.dig_out_pins[i];
2488 dig_nid = look_for_dac(codec, pin, true);
2489 if (!dig_nid)
2490 continue;
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01002491 path = snd_hda_add_new_path(codec, dig_nid, pin, HDA_PARSE_ALL);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002492 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002493 continue;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002494 print_nid_path("digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01002495 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01002496 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002497 if (!nums) {
2498 spec->multiout.dig_out_nid = dig_nid;
2499 spec->dig_out_type = spec->autocfg.dig_out_type[0];
2500 } else {
2501 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
2502 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
2503 break;
2504 spec->slave_dig_outs[nums - 1] = dig_nid;
2505 }
2506 nums++;
2507 }
2508
2509 if (spec->autocfg.dig_in_pin) {
2510 dig_nid = codec->start_nid;
2511 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002512 unsigned int wcaps = get_wcaps(codec, dig_nid);
2513 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
2514 continue;
2515 if (!(wcaps & AC_WCAP_DIGITAL))
2516 continue;
2517 path = snd_hda_add_new_path(codec,
2518 spec->autocfg.dig_in_pin,
Takashi Iwai4ac0eef2012-12-20 18:10:51 +01002519 dig_nid, HDA_PARSE_ALL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002520 if (path) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002521 print_nid_path("digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002522 path->active = true;
2523 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01002524 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002525 break;
2526 }
2527 }
2528 }
2529}
2530
2531
2532/*
2533 * input MUX handling
2534 */
2535
2536static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
2537
2538/* select the given imux item; either unmute exclusively or select the route */
2539static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2540 unsigned int idx)
2541{
2542 struct hda_gen_spec *spec = codec->spec;
2543 const struct hda_input_mux *imux;
2544 struct nid_path *path;
2545
2546 imux = &spec->input_mux;
2547 if (!imux->num_items)
2548 return 0;
2549
2550 if (idx >= imux->num_items)
2551 idx = imux->num_items - 1;
2552 if (spec->cur_mux[adc_idx] == idx)
2553 return 0;
2554
2555 path = snd_hda_get_nid_path(codec,
2556 spec->imux_pins[spec->cur_mux[adc_idx]],
2557 spec->adc_nids[adc_idx]);
2558 if (!path)
2559 return 0;
2560 if (path->active)
2561 snd_hda_activate_path(codec, path, false, false);
2562
2563 spec->cur_mux[adc_idx] = idx;
2564
2565 if (spec->shared_mic_hp)
2566 update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
2567
2568 if (spec->dyn_adc_switch)
2569 dyn_adc_pcm_resetup(codec, idx);
2570
2571 path = snd_hda_get_nid_path(codec, spec->imux_pins[idx],
2572 get_adc_nid(codec, adc_idx, idx));
2573 if (!path)
2574 return 0;
2575 if (path->active)
2576 return 0;
2577 snd_hda_activate_path(codec, path, true, false);
2578 if (spec->cap_sync_hook)
2579 spec->cap_sync_hook(codec);
2580 return 1;
2581}
2582
2583
2584/*
2585 * Jack detections for HP auto-mute and mic-switch
2586 */
2587
2588/* check each pin in the given array; returns true if any of them is plugged */
2589static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
2590{
2591 int i, present = 0;
2592
2593 for (i = 0; i < num_pins; i++) {
2594 hda_nid_t nid = pins[i];
2595 if (!nid)
2596 break;
2597 present |= snd_hda_jack_detect(codec, nid);
2598 }
2599 return present;
2600}
2601
2602/* standard HP/line-out auto-mute helper */
2603static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
2604 bool mute, bool hp_out)
2605{
2606 struct hda_gen_spec *spec = codec->spec;
2607 unsigned int pin_bits = mute ? 0 : (hp_out ? PIN_HP : PIN_OUT);
2608 int i;
2609
2610 for (i = 0; i < num_pins; i++) {
2611 hda_nid_t nid = pins[i];
2612 unsigned int val;
2613 if (!nid)
2614 break;
2615 /* don't reset VREF value in case it's controlling
2616 * the amp (see alc861_fixup_asus_amp_vref_0f())
2617 */
2618 if (spec->keep_vref_in_automute) {
2619 val = snd_hda_codec_read(codec, nid, 0,
2620 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2621 val &= ~PIN_HP;
2622 } else
2623 val = 0;
2624 val |= pin_bits;
Takashi Iwai7594aa32012-12-20 15:38:40 +01002625 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002626 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002627 }
2628}
2629
2630/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002631void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002632{
2633 struct hda_gen_spec *spec = codec->spec;
2634 int on;
2635
2636 /* Control HP pins/amps depending on master_mute state;
2637 * in general, HP pins/amps control should be enabled in all cases,
2638 * but currently set only for master_mute, just to be safe
2639 */
2640 if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
2641 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2642 spec->autocfg.hp_pins, spec->master_mute, true);
2643
2644 if (!spec->automute_speaker)
2645 on = 0;
2646 else
2647 on = spec->hp_jack_present | spec->line_jack_present;
2648 on |= spec->master_mute;
2649 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
2650 spec->autocfg.speaker_pins, on, false);
2651
2652 /* toggle line-out mutes if needed, too */
2653 /* if LO is a copy of either HP or Speaker, don't need to handle it */
2654 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
2655 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
2656 return;
2657 if (!spec->automute_lo)
2658 on = 0;
2659 else
2660 on = spec->hp_jack_present;
2661 on |= spec->master_mute;
2662 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2663 spec->autocfg.line_out_pins, on, false);
2664}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002665EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002666
2667static void call_update_outputs(struct hda_codec *codec)
2668{
2669 struct hda_gen_spec *spec = codec->spec;
2670 if (spec->automute_hook)
2671 spec->automute_hook(codec);
2672 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01002673 snd_hda_gen_update_outputs(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002674}
2675
2676/* standard HP-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002677void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002678{
2679 struct hda_gen_spec *spec = codec->spec;
2680
2681 spec->hp_jack_present =
2682 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2683 spec->autocfg.hp_pins);
2684 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
2685 return;
2686 call_update_outputs(codec);
2687}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002688EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002689
2690/* standard line-out-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002691void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002692{
2693 struct hda_gen_spec *spec = codec->spec;
2694
2695 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
2696 return;
2697 /* check LO jack only when it's different from HP */
2698 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
2699 return;
2700
2701 spec->line_jack_present =
2702 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2703 spec->autocfg.line_out_pins);
2704 if (!spec->automute_speaker || !spec->detect_lo)
2705 return;
2706 call_update_outputs(codec);
2707}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002708EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002709
2710/* standard mic auto-switch helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002711void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002712{
2713 struct hda_gen_spec *spec = codec->spec;
2714 int i;
2715
2716 if (!spec->auto_mic)
2717 return;
2718
2719 for (i = spec->am_num_entries - 1; i > 0; i--) {
2720 if (snd_hda_jack_detect(codec, spec->am_entry[i].pin)) {
2721 mux_select(codec, 0, spec->am_entry[i].idx);
2722 return;
2723 }
2724 }
2725 mux_select(codec, 0, spec->am_entry[0].idx);
2726}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002727EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002728
2729/*
2730 * Auto-Mute mode mixer enum support
2731 */
2732static int automute_mode_info(struct snd_kcontrol *kcontrol,
2733 struct snd_ctl_elem_info *uinfo)
2734{
2735 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2736 struct hda_gen_spec *spec = codec->spec;
2737 static const char * const texts3[] = {
2738 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02002739 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740
Takashi Iwai352f7f92012-12-19 12:52:06 +01002741 if (spec->automute_speaker_possible && spec->automute_lo_possible)
2742 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
2743 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2744}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745
Takashi Iwai352f7f92012-12-19 12:52:06 +01002746static int automute_mode_get(struct snd_kcontrol *kcontrol,
2747 struct snd_ctl_elem_value *ucontrol)
2748{
2749 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2750 struct hda_gen_spec *spec = codec->spec;
2751 unsigned int val = 0;
2752 if (spec->automute_speaker)
2753 val++;
2754 if (spec->automute_lo)
2755 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002756
Takashi Iwai352f7f92012-12-19 12:52:06 +01002757 ucontrol->value.enumerated.item[0] = val;
2758 return 0;
2759}
2760
2761static int automute_mode_put(struct snd_kcontrol *kcontrol,
2762 struct snd_ctl_elem_value *ucontrol)
2763{
2764 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2765 struct hda_gen_spec *spec = codec->spec;
2766
2767 switch (ucontrol->value.enumerated.item[0]) {
2768 case 0:
2769 if (!spec->automute_speaker && !spec->automute_lo)
2770 return 0;
2771 spec->automute_speaker = 0;
2772 spec->automute_lo = 0;
2773 break;
2774 case 1:
2775 if (spec->automute_speaker_possible) {
2776 if (!spec->automute_lo && spec->automute_speaker)
2777 return 0;
2778 spec->automute_speaker = 1;
2779 spec->automute_lo = 0;
2780 } else if (spec->automute_lo_possible) {
2781 if (spec->automute_lo)
2782 return 0;
2783 spec->automute_lo = 1;
2784 } else
2785 return -EINVAL;
2786 break;
2787 case 2:
2788 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
2789 return -EINVAL;
2790 if (spec->automute_speaker && spec->automute_lo)
2791 return 0;
2792 spec->automute_speaker = 1;
2793 spec->automute_lo = 1;
2794 break;
2795 default:
2796 return -EINVAL;
2797 }
2798 call_update_outputs(codec);
2799 return 1;
2800}
2801
2802static const struct snd_kcontrol_new automute_mode_enum = {
2803 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2804 .name = "Auto-Mute Mode",
2805 .info = automute_mode_info,
2806 .get = automute_mode_get,
2807 .put = automute_mode_put,
2808};
2809
2810static int add_automute_mode_enum(struct hda_codec *codec)
2811{
2812 struct hda_gen_spec *spec = codec->spec;
2813
Takashi Iwai12c93df2012-12-19 14:38:33 +01002814 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002815 return -ENOMEM;
2816 return 0;
2817}
2818
2819/*
2820 * Check the availability of HP/line-out auto-mute;
2821 * Set up appropriately if really supported
2822 */
2823static int check_auto_mute_availability(struct hda_codec *codec)
2824{
2825 struct hda_gen_spec *spec = codec->spec;
2826 struct auto_pin_cfg *cfg = &spec->autocfg;
2827 int present = 0;
2828 int i, err;
2829
2830 if (cfg->hp_pins[0])
2831 present++;
2832 if (cfg->line_out_pins[0])
2833 present++;
2834 if (cfg->speaker_pins[0])
2835 present++;
2836 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02002837 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002838
2839 if (!cfg->speaker_pins[0] &&
2840 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
2841 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2842 sizeof(cfg->speaker_pins));
2843 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845
Takashi Iwai352f7f92012-12-19 12:52:06 +01002846 if (!cfg->hp_pins[0] &&
2847 cfg->line_out_type == AUTO_PIN_HP_OUT) {
2848 memcpy(cfg->hp_pins, cfg->line_out_pins,
2849 sizeof(cfg->hp_pins));
2850 cfg->hp_outs = cfg->line_outs;
2851 }
2852
2853 for (i = 0; i < cfg->hp_outs; i++) {
2854 hda_nid_t nid = cfg->hp_pins[i];
2855 if (!is_jack_detectable(codec, nid))
2856 continue;
2857 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
2858 nid);
2859 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01002860 spec->hp_automute_hook ?
2861 spec->hp_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01002862 snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002863 spec->detect_hp = 1;
2864 }
2865
2866 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
2867 if (cfg->speaker_outs)
2868 for (i = 0; i < cfg->line_outs; i++) {
2869 hda_nid_t nid = cfg->line_out_pins[i];
2870 if (!is_jack_detectable(codec, nid))
2871 continue;
2872 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
2873 snd_hda_jack_detect_enable_callback(codec, nid,
2874 HDA_GEN_FRONT_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01002875 spec->line_automute_hook ?
2876 spec->line_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01002877 snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002878 spec->detect_lo = 1;
2879 }
2880 spec->automute_lo_possible = spec->detect_hp;
2881 }
2882
2883 spec->automute_speaker_possible = cfg->speaker_outs &&
2884 (spec->detect_hp || spec->detect_lo);
2885
2886 spec->automute_lo = spec->automute_lo_possible;
2887 spec->automute_speaker = spec->automute_speaker_possible;
2888
2889 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
2890 /* create a control for automute mode */
2891 err = add_automute_mode_enum(codec);
2892 if (err < 0)
2893 return err;
2894 }
2895 return 0;
2896}
2897
2898/* return the position of NID in the list, or -1 if not found */
2899static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
2900{
2901 int i;
2902 for (i = 0; i < nums; i++)
2903 if (list[i] == nid)
2904 return i;
2905 return -1;
2906}
2907
2908/* check whether all auto-mic pins are valid; setup indices if OK */
2909static bool auto_mic_check_imux(struct hda_codec *codec)
2910{
2911 struct hda_gen_spec *spec = codec->spec;
2912 const struct hda_input_mux *imux;
2913 int i;
2914
2915 imux = &spec->input_mux;
2916 for (i = 0; i < spec->am_num_entries; i++) {
2917 spec->am_entry[i].idx =
2918 find_idx_in_nid_list(spec->am_entry[i].pin,
2919 spec->imux_pins, imux->num_items);
2920 if (spec->am_entry[i].idx < 0)
2921 return false; /* no corresponding imux */
2922 }
2923
2924 /* we don't need the jack detection for the first pin */
2925 for (i = 1; i < spec->am_num_entries; i++)
2926 snd_hda_jack_detect_enable_callback(codec,
2927 spec->am_entry[i].pin,
2928 HDA_GEN_MIC_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01002929 spec->mic_autoswitch_hook ?
2930 spec->mic_autoswitch_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01002931 snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002932 return true;
2933}
2934
2935static int compare_attr(const void *ap, const void *bp)
2936{
2937 const struct automic_entry *a = ap;
2938 const struct automic_entry *b = bp;
2939 return (int)(a->attr - b->attr);
2940}
2941
2942/*
2943 * Check the availability of auto-mic switch;
2944 * Set up if really supported
2945 */
2946static int check_auto_mic_availability(struct hda_codec *codec)
2947{
2948 struct hda_gen_spec *spec = codec->spec;
2949 struct auto_pin_cfg *cfg = &spec->autocfg;
2950 unsigned int types;
2951 int i, num_pins;
2952
2953 types = 0;
2954 num_pins = 0;
2955 for (i = 0; i < cfg->num_inputs; i++) {
2956 hda_nid_t nid = cfg->inputs[i].pin;
2957 unsigned int attr;
2958 attr = snd_hda_codec_get_pincfg(codec, nid);
2959 attr = snd_hda_get_input_pin_attr(attr);
2960 if (types & (1 << attr))
2961 return 0; /* already occupied */
2962 switch (attr) {
2963 case INPUT_PIN_ATTR_INT:
2964 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2965 return 0; /* invalid type */
2966 break;
2967 case INPUT_PIN_ATTR_UNUSED:
2968 return 0; /* invalid entry */
2969 default:
2970 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
2971 return 0; /* invalid type */
2972 if (!spec->line_in_auto_switch &&
2973 cfg->inputs[i].type != AUTO_PIN_MIC)
2974 return 0; /* only mic is allowed */
2975 if (!is_jack_detectable(codec, nid))
2976 return 0; /* no unsol support */
2977 break;
2978 }
2979 if (num_pins >= MAX_AUTO_MIC_PINS)
2980 return 0;
2981 types |= (1 << attr);
2982 spec->am_entry[num_pins].pin = nid;
2983 spec->am_entry[num_pins].attr = attr;
2984 num_pins++;
2985 }
2986
2987 if (num_pins < 2)
2988 return 0;
2989
2990 spec->am_num_entries = num_pins;
2991 /* sort the am_entry in the order of attr so that the pin with a
2992 * higher attr will be selected when the jack is plugged.
2993 */
2994 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
2995 compare_attr, NULL);
2996
2997 if (!auto_mic_check_imux(codec))
2998 return 0;
2999
3000 spec->auto_mic = 1;
3001 spec->num_adc_nids = 1;
3002 spec->cur_mux[0] = spec->am_entry[0].idx;
3003 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
3004 spec->am_entry[0].pin,
3005 spec->am_entry[1].pin,
3006 spec->am_entry[2].pin);
3007
3008 return 0;
3009}
3010
3011
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003012/*
3013 * Parse the given BIOS configuration and set up the hda_gen_spec
3014 *
3015 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003016 * or a negative error code
3017 */
3018int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003019 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003020{
3021 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003022 int err;
3023
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003024 if (cfg != &spec->autocfg) {
3025 spec->autocfg = *cfg;
3026 cfg = &spec->autocfg;
3027 }
3028
Takashi Iwai352f7f92012-12-19 12:52:06 +01003029 if (!cfg->line_outs) {
3030 if (cfg->dig_outs || cfg->dig_in_pin) {
3031 spec->multiout.max_channels = 2;
3032 spec->no_analog = 1;
3033 goto dig_only;
3034 }
3035 return 0; /* can't find valid BIOS pin config */
3036 }
3037
3038 if (!spec->no_primary_hp &&
3039 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
3040 cfg->line_outs <= cfg->hp_outs) {
3041 /* use HP as primary out */
3042 cfg->speaker_outs = cfg->line_outs;
3043 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3044 sizeof(cfg->speaker_pins));
3045 cfg->line_outs = cfg->hp_outs;
3046 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
3047 cfg->hp_outs = 0;
3048 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3049 cfg->line_out_type = AUTO_PIN_HP_OUT;
3050 }
3051
3052 err = parse_output_paths(codec);
3053 if (err < 0)
3054 return err;
3055 err = create_multi_channel_mode(codec);
3056 if (err < 0)
3057 return err;
3058 err = create_multi_out_ctls(codec, cfg);
3059 if (err < 0)
3060 return err;
3061 err = create_hp_out_ctls(codec);
3062 if (err < 0)
3063 return err;
3064 err = create_speaker_out_ctls(codec);
3065 if (err < 0)
3066 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003067 err = create_indep_hp_ctls(codec);
3068 if (err < 0)
3069 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003070 err = create_shared_input(codec);
3071 if (err < 0)
3072 return err;
3073 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003074 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02003075 return err;
3076
Takashi Iwai352f7f92012-12-19 12:52:06 +01003077 /* check the multiple speaker pins */
3078 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
3079 spec->const_channel_count = cfg->line_outs * 2;
3080 else
3081 spec->const_channel_count = cfg->speaker_outs * 2;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003082
Takashi Iwai352f7f92012-12-19 12:52:06 +01003083 if (spec->multi_ios > 0)
3084 spec->multiout.max_channels = max(spec->ext_channel_count,
3085 spec->const_channel_count);
3086 else
3087 spec->multiout.max_channels = spec->multiout.num_dacs * 2;
3088
3089 err = check_auto_mute_availability(codec);
3090 if (err < 0)
3091 return err;
3092
3093 err = check_dyn_adc_switch(codec);
3094 if (err < 0)
3095 return err;
3096
3097 if (!spec->shared_mic_hp) {
3098 err = check_auto_mic_availability(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003099 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 }
Takashi Iwai071c73a2006-08-23 18:34:06 +02003102
Takashi Iwai352f7f92012-12-19 12:52:06 +01003103 err = create_capture_mixers(codec);
3104 if (err < 0)
3105 return err;
3106
3107 err = parse_mic_boost(codec);
3108 if (err < 0)
3109 return err;
3110
3111 dig_only:
3112 parse_digital(codec);
3113
3114 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003116EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117
3118
3119/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003120 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003122
3123/* slave controls for virtual master */
3124static const char * const slave_pfxs[] = {
3125 "Front", "Surround", "Center", "LFE", "Side",
3126 "Headphone", "Speaker", "Mono", "Line Out",
3127 "CLFE", "Bass Speaker", "PCM",
3128 NULL,
3129};
3130
3131int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003133 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135
Takashi Iwai36502d02012-12-19 15:15:10 +01003136 if (spec->kctls.used) {
3137 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
3138 if (err < 0)
3139 return err;
3140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141
Takashi Iwai352f7f92012-12-19 12:52:06 +01003142 if (spec->multiout.dig_out_nid) {
3143 err = snd_hda_create_dig_out_ctls(codec,
3144 spec->multiout.dig_out_nid,
3145 spec->multiout.dig_out_nid,
3146 spec->pcm_rec[1].pcm_type);
3147 if (err < 0)
3148 return err;
3149 if (!spec->no_analog) {
3150 err = snd_hda_create_spdif_share_sw(codec,
3151 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 if (err < 0)
3153 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003154 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 }
3156 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003157 if (spec->dig_in_nid) {
3158 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
3159 if (err < 0)
3160 return err;
3161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162
Takashi Iwai352f7f92012-12-19 12:52:06 +01003163 /* if we have no master control, let's create it */
3164 if (!spec->no_analog &&
3165 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
3166 unsigned int vmaster_tlv[4];
3167 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
3168 HDA_OUTPUT, vmaster_tlv);
3169 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
3170 vmaster_tlv, slave_pfxs,
3171 "Playback Volume");
3172 if (err < 0)
3173 return err;
3174 }
3175 if (!spec->no_analog &&
3176 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
3177 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
3178 NULL, slave_pfxs,
3179 "Playback Switch",
3180 true, &spec->vmaster_mute.sw_kctl);
3181 if (err < 0)
3182 return err;
3183 if (spec->vmaster_mute.hook)
Takashi Iwaifd25a972012-12-20 14:57:18 +01003184 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
3185 spec->vmaster_mute_enum);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187
Takashi Iwai352f7f92012-12-19 12:52:06 +01003188 free_kctls(spec); /* no longer needed */
3189
3190 if (spec->shared_mic_hp) {
3191 int err;
3192 int nid = spec->autocfg.inputs[1].pin;
3193 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
3194 if (err < 0)
3195 return err;
3196 err = snd_hda_jack_detect_enable(codec, nid, 0);
3197 if (err < 0)
3198 return err;
3199 }
3200
3201 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
3202 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 return err;
3204
3205 return 0;
3206}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003207EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
3208
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209
3210/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003211 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213
Takashi Iwai352f7f92012-12-19 12:52:06 +01003214/*
3215 * Analog playback callbacks
3216 */
3217static int playback_pcm_open(struct hda_pcm_stream *hinfo,
3218 struct hda_codec *codec,
3219 struct snd_pcm_substream *substream)
3220{
3221 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003222 int err;
3223
3224 mutex_lock(&spec->pcm_mutex);
3225 err = snd_hda_multi_out_analog_open(codec,
3226 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003227 hinfo);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003228 if (!err)
3229 spec->active_streams |= 1 << STREAM_MULTI_OUT;
3230 mutex_unlock(&spec->pcm_mutex);
3231 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003232}
3233
3234static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01003235 struct hda_codec *codec,
3236 unsigned int stream_tag,
3237 unsigned int format,
3238 struct snd_pcm_substream *substream)
3239{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003240 struct hda_gen_spec *spec = codec->spec;
3241 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
3242 stream_tag, format, substream);
3243}
Takashi Iwai97ec5582006-03-21 11:29:07 +01003244
Takashi Iwai352f7f92012-12-19 12:52:06 +01003245static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3246 struct hda_codec *codec,
3247 struct snd_pcm_substream *substream)
3248{
3249 struct hda_gen_spec *spec = codec->spec;
3250 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
3251}
3252
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003253static int playback_pcm_close(struct hda_pcm_stream *hinfo,
3254 struct hda_codec *codec,
3255 struct snd_pcm_substream *substream)
3256{
3257 struct hda_gen_spec *spec = codec->spec;
3258 mutex_lock(&spec->pcm_mutex);
3259 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
3260 mutex_unlock(&spec->pcm_mutex);
3261 return 0;
3262}
3263
3264static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
3265 struct hda_codec *codec,
3266 struct snd_pcm_substream *substream)
3267{
3268 struct hda_gen_spec *spec = codec->spec;
3269 int err = 0;
3270
3271 mutex_lock(&spec->pcm_mutex);
3272 if (!spec->indep_hp_enabled)
3273 err = -EBUSY;
3274 else
3275 spec->active_streams |= 1 << STREAM_INDEP_HP;
3276 mutex_unlock(&spec->pcm_mutex);
3277 return err;
3278}
3279
3280static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
3281 struct hda_codec *codec,
3282 struct snd_pcm_substream *substream)
3283{
3284 struct hda_gen_spec *spec = codec->spec;
3285 mutex_lock(&spec->pcm_mutex);
3286 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
3287 mutex_unlock(&spec->pcm_mutex);
3288 return 0;
3289}
3290
Takashi Iwai352f7f92012-12-19 12:52:06 +01003291/*
3292 * Digital out
3293 */
3294static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
3295 struct hda_codec *codec,
3296 struct snd_pcm_substream *substream)
3297{
3298 struct hda_gen_spec *spec = codec->spec;
3299 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3300}
3301
3302static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3303 struct hda_codec *codec,
3304 unsigned int stream_tag,
3305 unsigned int format,
3306 struct snd_pcm_substream *substream)
3307{
3308 struct hda_gen_spec *spec = codec->spec;
3309 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3310 stream_tag, format, substream);
3311}
3312
3313static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3314 struct hda_codec *codec,
3315 struct snd_pcm_substream *substream)
3316{
3317 struct hda_gen_spec *spec = codec->spec;
3318 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
3319}
3320
3321static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
3322 struct hda_codec *codec,
3323 struct snd_pcm_substream *substream)
3324{
3325 struct hda_gen_spec *spec = codec->spec;
3326 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3327}
3328
3329/*
3330 * Analog capture
3331 */
3332static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3333 struct hda_codec *codec,
3334 unsigned int stream_tag,
3335 unsigned int format,
3336 struct snd_pcm_substream *substream)
3337{
3338 struct hda_gen_spec *spec = codec->spec;
3339
3340 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01003341 stream_tag, 0, format);
3342 return 0;
3343}
3344
Takashi Iwai352f7f92012-12-19 12:52:06 +01003345static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3346 struct hda_codec *codec,
3347 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01003348{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003349 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01003350
Takashi Iwai352f7f92012-12-19 12:52:06 +01003351 snd_hda_codec_cleanup_stream(codec,
3352 spec->adc_nids[substream->number + 1]);
Takashi Iwai97ec5582006-03-21 11:29:07 +01003353 return 0;
3354}
3355
Takashi Iwai352f7f92012-12-19 12:52:06 +01003356/*
3357 */
3358static const struct hda_pcm_stream pcm_analog_playback = {
3359 .substreams = 1,
3360 .channels_min = 2,
3361 .channels_max = 8,
3362 /* NID is set in build_pcms */
3363 .ops = {
3364 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003365 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003366 .prepare = playback_pcm_prepare,
3367 .cleanup = playback_pcm_cleanup
3368 },
3369};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370
Takashi Iwai352f7f92012-12-19 12:52:06 +01003371static const struct hda_pcm_stream pcm_analog_capture = {
3372 .substreams = 1,
3373 .channels_min = 2,
3374 .channels_max = 2,
3375 /* NID is set in build_pcms */
3376};
3377
3378static const struct hda_pcm_stream pcm_analog_alt_playback = {
3379 .substreams = 1,
3380 .channels_min = 2,
3381 .channels_max = 2,
3382 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003383 .ops = {
3384 .open = alt_playback_pcm_open,
3385 .close = alt_playback_pcm_close
3386 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01003387};
3388
3389static const struct hda_pcm_stream pcm_analog_alt_capture = {
3390 .substreams = 2, /* can be overridden */
3391 .channels_min = 2,
3392 .channels_max = 2,
3393 /* NID is set in build_pcms */
3394 .ops = {
3395 .prepare = alt_capture_pcm_prepare,
3396 .cleanup = alt_capture_pcm_cleanup
3397 },
3398};
3399
3400static const struct hda_pcm_stream pcm_digital_playback = {
3401 .substreams = 1,
3402 .channels_min = 2,
3403 .channels_max = 2,
3404 /* NID is set in build_pcms */
3405 .ops = {
3406 .open = dig_playback_pcm_open,
3407 .close = dig_playback_pcm_close,
3408 .prepare = dig_playback_pcm_prepare,
3409 .cleanup = dig_playback_pcm_cleanup
3410 },
3411};
3412
3413static const struct hda_pcm_stream pcm_digital_capture = {
3414 .substreams = 1,
3415 .channels_min = 2,
3416 .channels_max = 2,
3417 /* NID is set in build_pcms */
3418};
3419
3420/* Used by build_pcms to flag that a PCM has no playback stream */
3421static const struct hda_pcm_stream pcm_null_stream = {
3422 .substreams = 0,
3423 .channels_min = 0,
3424 .channels_max = 0,
3425};
3426
3427/*
3428 * dynamic changing ADC PCM streams
3429 */
3430static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
3431{
3432 struct hda_gen_spec *spec = codec->spec;
3433 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
3434
3435 if (spec->cur_adc && spec->cur_adc != new_adc) {
3436 /* stream is running, let's swap the current ADC */
3437 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
3438 spec->cur_adc = new_adc;
3439 snd_hda_codec_setup_stream(codec, new_adc,
3440 spec->cur_adc_stream_tag, 0,
3441 spec->cur_adc_format);
3442 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003444 return false;
3445}
3446
3447/* analog capture with dynamic dual-adc changes */
3448static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3449 struct hda_codec *codec,
3450 unsigned int stream_tag,
3451 unsigned int format,
3452 struct snd_pcm_substream *substream)
3453{
3454 struct hda_gen_spec *spec = codec->spec;
3455 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
3456 spec->cur_adc_stream_tag = stream_tag;
3457 spec->cur_adc_format = format;
3458 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
3459 return 0;
3460}
3461
3462static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3463 struct hda_codec *codec,
3464 struct snd_pcm_substream *substream)
3465{
3466 struct hda_gen_spec *spec = codec->spec;
3467 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
3468 spec->cur_adc = 0;
3469 return 0;
3470}
3471
3472static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
3473 .substreams = 1,
3474 .channels_min = 2,
3475 .channels_max = 2,
3476 .nid = 0, /* fill later */
3477 .ops = {
3478 .prepare = dyn_adc_capture_pcm_prepare,
3479 .cleanup = dyn_adc_capture_pcm_cleanup
3480 },
3481};
3482
Takashi Iwaif873e532012-12-20 16:58:39 +01003483static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
3484 const char *chip_name)
3485{
3486 char *p;
3487
3488 if (*str)
3489 return;
3490 strlcpy(str, chip_name, len);
3491
3492 /* drop non-alnum chars after a space */
3493 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
3494 if (!isalnum(p[1])) {
3495 *p = 0;
3496 break;
3497 }
3498 }
3499 strlcat(str, sfx, len);
3500}
3501
Takashi Iwai352f7f92012-12-19 12:52:06 +01003502/* build PCM streams based on the parsed results */
3503int snd_hda_gen_build_pcms(struct hda_codec *codec)
3504{
3505 struct hda_gen_spec *spec = codec->spec;
3506 struct hda_pcm *info = spec->pcm_rec;
3507 const struct hda_pcm_stream *p;
3508 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509
3510 codec->num_pcms = 1;
3511 codec->pcm_info = info;
3512
Takashi Iwai352f7f92012-12-19 12:52:06 +01003513 if (spec->no_analog)
3514 goto skip_analog;
3515
Takashi Iwaif873e532012-12-20 16:58:39 +01003516 fill_pcm_stream_name(spec->stream_name_analog,
3517 sizeof(spec->stream_name_analog),
3518 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003519 info->name = spec->stream_name_analog;
3520
3521 if (spec->multiout.num_dacs > 0) {
3522 p = spec->stream_analog_playback;
3523 if (!p)
3524 p = &pcm_analog_playback;
3525 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3526 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
3527 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
3528 spec->multiout.max_channels;
3529 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
3530 spec->autocfg.line_outs == 2)
3531 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
3532 snd_pcm_2_1_chmaps;
3533 }
3534 if (spec->num_adc_nids) {
3535 p = spec->stream_analog_capture;
3536 if (!p) {
3537 if (spec->dyn_adc_switch)
3538 p = &dyn_adc_pcm_analog_capture;
3539 else
3540 p = &pcm_analog_capture;
3541 }
3542 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3543 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
3544 }
3545
Takashi Iwai352f7f92012-12-19 12:52:06 +01003546 skip_analog:
3547 /* SPDIF for stream index #1 */
3548 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01003549 fill_pcm_stream_name(spec->stream_name_digital,
3550 sizeof(spec->stream_name_digital),
3551 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003552 codec->num_pcms = 2;
3553 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
3554 info = spec->pcm_rec + 1;
3555 info->name = spec->stream_name_digital;
3556 if (spec->dig_out_type)
3557 info->pcm_type = spec->dig_out_type;
3558 else
3559 info->pcm_type = HDA_PCM_TYPE_SPDIF;
3560 if (spec->multiout.dig_out_nid) {
3561 p = spec->stream_digital_playback;
3562 if (!p)
3563 p = &pcm_digital_playback;
3564 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3565 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
3566 }
3567 if (spec->dig_in_nid) {
3568 p = spec->stream_digital_capture;
3569 if (!p)
3570 p = &pcm_digital_capture;
3571 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3572 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
3573 }
3574 }
3575
3576 if (spec->no_analog)
3577 return 0;
3578
3579 /* If the use of more than one ADC is requested for the current
3580 * model, configure a second analog capture-only PCM.
3581 */
3582 have_multi_adcs = (spec->num_adc_nids > 1) &&
3583 !spec->dyn_adc_switch && !spec->auto_mic;
3584 /* Additional Analaog capture for index #2 */
3585 if (spec->alt_dac_nid || have_multi_adcs) {
3586 codec->num_pcms = 3;
3587 info = spec->pcm_rec + 2;
3588 info->name = spec->stream_name_analog;
3589 if (spec->alt_dac_nid) {
3590 p = spec->stream_analog_alt_playback;
3591 if (!p)
3592 p = &pcm_analog_alt_playback;
3593 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3594 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
3595 spec->alt_dac_nid;
3596 } else {
3597 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
3598 pcm_null_stream;
3599 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
3600 }
3601 if (have_multi_adcs) {
3602 p = spec->stream_analog_alt_capture;
3603 if (!p)
3604 p = &pcm_analog_alt_capture;
3605 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3606 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
3607 spec->adc_nids[1];
3608 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
3609 spec->num_adc_nids - 1;
3610 } else {
3611 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
3612 pcm_null_stream;
3613 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
3614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 }
3616
3617 return 0;
3618}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003619EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
3620
3621
3622/*
3623 * Standard auto-parser initializations
3624 */
3625
3626/* configure the path from the given dac to the pin as the proper output */
3627static void set_output_and_unmute(struct hda_codec *codec, hda_nid_t pin,
Takashi Iwai196c17662013-01-04 15:01:40 +01003628 int pin_type, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003629{
3630 struct nid_path *path;
3631
3632 snd_hda_set_pin_ctl_cache(codec, pin, pin_type);
Takashi Iwai196c17662013-01-04 15:01:40 +01003633 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003634 if (!path)
3635 return;
Takashi Iwaie1284af2013-01-03 16:33:02 +01003636 snd_hda_activate_path(codec, path, path->active, true);
3637 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003638}
3639
3640/* initialize primary output paths */
3641static void init_multi_out(struct hda_codec *codec)
3642{
3643 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai196c17662013-01-04 15:01:40 +01003644 hda_nid_t nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003645 int pin_type;
3646 int i;
3647
3648 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
3649 pin_type = PIN_HP;
3650 else
3651 pin_type = PIN_OUT;
3652
Takashi Iwai64049c82012-12-20 15:29:21 +01003653 for (i = 0; i < spec->autocfg.line_outs; i++) {
3654 nid = spec->autocfg.line_out_pins[i];
Takashi Iwai196c17662013-01-04 15:01:40 +01003655 if (nid)
3656 set_output_and_unmute(codec, nid, pin_type,
3657 spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003658 }
3659}
3660
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003661
3662static void __init_extra_out(struct hda_codec *codec, int num_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01003663 hda_nid_t *pins, int *paths, int type)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003664{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003665 int i;
Takashi Iwai196c17662013-01-04 15:01:40 +01003666 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003667
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003668 for (i = 0; i < num_outs; i++) {
3669 pin = pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003670 if (!pin)
3671 break;
Takashi Iwai196c17662013-01-04 15:01:40 +01003672 set_output_and_unmute(codec, pin, type, paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003673 }
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003674}
3675
3676/* initialize hp and speaker paths */
3677static void init_extra_out(struct hda_codec *codec)
3678{
3679 struct hda_gen_spec *spec = codec->spec;
3680
3681 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
3682 __init_extra_out(codec, spec->autocfg.hp_outs,
3683 spec->autocfg.hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01003684 spec->hp_paths, PIN_HP);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003685 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
3686 __init_extra_out(codec, spec->autocfg.speaker_outs,
3687 spec->autocfg.speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01003688 spec->speaker_paths, PIN_OUT);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003689}
3690
3691/* initialize multi-io paths */
3692static void init_multi_io(struct hda_codec *codec)
3693{
3694 struct hda_gen_spec *spec = codec->spec;
3695 int i;
3696
3697 for (i = 0; i < spec->multi_ios; i++) {
3698 hda_nid_t pin = spec->multi_io[i].pin;
3699 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01003700 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003701 if (!path)
3702 continue;
3703 if (!spec->multi_io[i].ctl_in)
3704 spec->multi_io[i].ctl_in =
3705 snd_hda_codec_update_cache(codec, pin, 0,
3706 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3707 snd_hda_activate_path(codec, path, path->active, true);
3708 }
3709}
3710
3711/* set up the input pin config, depending on the given auto-pin type */
3712static void set_input_pin(struct hda_codec *codec, hda_nid_t nid,
3713 int auto_pin_type)
3714{
3715 unsigned int val = PIN_IN;
3716 if (auto_pin_type == AUTO_PIN_MIC)
3717 val |= snd_hda_get_default_vref(codec, nid);
Takashi Iwai7594aa32012-12-20 15:38:40 +01003718 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003719}
3720
3721/* set up input pins and loopback paths */
3722static void init_analog_input(struct hda_codec *codec)
3723{
3724 struct hda_gen_spec *spec = codec->spec;
3725 struct auto_pin_cfg *cfg = &spec->autocfg;
3726 int i;
3727
3728 for (i = 0; i < cfg->num_inputs; i++) {
3729 hda_nid_t nid = cfg->inputs[i].pin;
3730 if (is_input_pin(codec, nid))
3731 set_input_pin(codec, nid, cfg->inputs[i].type);
3732
3733 /* init loopback inputs */
3734 if (spec->mixer_nid) {
3735 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01003736 path = snd_hda_get_path_from_idx(codec, spec->loopback_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003737 if (path)
3738 snd_hda_activate_path(codec, path,
3739 path->active, false);
3740 }
3741 }
3742}
3743
3744/* initialize ADC paths */
3745static void init_input_src(struct hda_codec *codec)
3746{
3747 struct hda_gen_spec *spec = codec->spec;
3748 struct hda_input_mux *imux = &spec->input_mux;
3749 struct nid_path *path;
3750 int i, c, nums;
3751
3752 if (spec->dyn_adc_switch)
3753 nums = 1;
3754 else
3755 nums = spec->num_adc_nids;
3756
3757 for (c = 0; c < nums; c++) {
3758 for (i = 0; i < imux->num_items; i++) {
3759 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
3760 get_adc_nid(codec, c, i));
3761 if (path) {
3762 bool active = path->active;
3763 if (i == spec->cur_mux[c])
3764 active = true;
3765 snd_hda_activate_path(codec, path, active, false);
3766 }
3767 }
3768 }
3769
3770 if (spec->shared_mic_hp)
3771 update_shared_mic_hp(codec, spec->cur_mux[0]);
3772
3773 if (spec->cap_sync_hook)
3774 spec->cap_sync_hook(codec);
3775}
3776
3777/* set right pin controls for digital I/O */
3778static void init_digital(struct hda_codec *codec)
3779{
3780 struct hda_gen_spec *spec = codec->spec;
3781 int i;
3782 hda_nid_t pin;
3783
3784 for (i = 0; i < spec->autocfg.dig_outs; i++) {
3785 pin = spec->autocfg.dig_out_pins[i];
3786 if (!pin)
3787 continue;
Takashi Iwai196c17662013-01-04 15:01:40 +01003788 set_output_and_unmute(codec, pin, PIN_OUT,
3789 spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003790 }
3791 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003792 if (pin) {
3793 struct nid_path *path;
Takashi Iwai7594aa32012-12-20 15:38:40 +01003794 snd_hda_set_pin_ctl_cache(codec, pin, PIN_IN);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003795 path = snd_hda_get_path_from_idx(codec, spec->digin_path);
3796 if (path)
3797 snd_hda_activate_path(codec, path, path->active, false);
3798 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003799}
3800
Takashi Iwai973e4972012-12-20 15:16:09 +01003801/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
3802 * invalid unsol tags by some reason
3803 */
3804static void clear_unsol_on_unused_pins(struct hda_codec *codec)
3805{
3806 int i;
3807
3808 for (i = 0; i < codec->init_pins.used; i++) {
3809 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
3810 hda_nid_t nid = pin->nid;
3811 if (is_jack_detectable(codec, nid) &&
3812 !snd_hda_jack_tbl_get(codec, nid))
3813 snd_hda_codec_update_cache(codec, nid, 0,
3814 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
3815 }
3816}
3817
Takashi Iwai352f7f92012-12-19 12:52:06 +01003818int snd_hda_gen_init(struct hda_codec *codec)
3819{
3820 struct hda_gen_spec *spec = codec->spec;
3821
3822 if (spec->init_hook)
3823 spec->init_hook(codec);
3824
3825 snd_hda_apply_verbs(codec);
3826
Takashi Iwai3bbcd272012-12-20 11:50:58 +01003827 codec->cached_write = 1;
3828
Takashi Iwai352f7f92012-12-19 12:52:06 +01003829 init_multi_out(codec);
3830 init_extra_out(codec);
3831 init_multi_io(codec);
3832 init_analog_input(codec);
3833 init_input_src(codec);
3834 init_digital(codec);
3835
Takashi Iwai973e4972012-12-20 15:16:09 +01003836 clear_unsol_on_unused_pins(codec);
3837
Takashi Iwai352f7f92012-12-19 12:52:06 +01003838 /* call init functions of standard auto-mute helpers */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003839 snd_hda_gen_hp_automute(codec, NULL);
3840 snd_hda_gen_line_automute(codec, NULL);
3841 snd_hda_gen_mic_autoswitch(codec, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003842
Takashi Iwai3bbcd272012-12-20 11:50:58 +01003843 snd_hda_codec_flush_amp_cache(codec);
3844 snd_hda_codec_flush_cmd_cache(codec);
3845
Takashi Iwai352f7f92012-12-19 12:52:06 +01003846 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
3847 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
3848
3849 hda_call_check_power_status(codec, 0x01);
3850 return 0;
3851}
3852EXPORT_SYMBOL(snd_hda_gen_init);
3853
3854
3855/*
3856 * the generic codec support
3857 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858
Takashi Iwai83012a72012-08-24 18:38:08 +02003859#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003860static int generic_check_power_status(struct hda_codec *codec, hda_nid_t nid)
3861{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003862 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaicb53c622007-08-10 17:21:45 +02003863 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
3864}
3865#endif
3866
Takashi Iwai352f7f92012-12-19 12:52:06 +01003867static void generic_free(struct hda_codec *codec)
3868{
3869 snd_hda_gen_spec_free(codec->spec);
3870 kfree(codec->spec);
3871 codec->spec = NULL;
3872}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873
Takashi Iwai352f7f92012-12-19 12:52:06 +01003874static const struct hda_codec_ops generic_patch_ops = {
3875 .build_controls = snd_hda_gen_build_controls,
3876 .build_pcms = snd_hda_gen_build_pcms,
3877 .init = snd_hda_gen_init,
3878 .free = generic_free,
3879 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02003880#ifdef CONFIG_PM
Takashi Iwaicb53c622007-08-10 17:21:45 +02003881 .check_power_status = generic_check_power_status,
3882#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883};
3884
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885int snd_hda_parse_generic_codec(struct hda_codec *codec)
3886{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003887 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888 int err;
3889
Takashi Iwaie560d8d2005-09-09 14:21:46 +02003890 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003891 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003893 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003896 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
3897 if (err < 0)
3898 return err;
3899
3900 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003901 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 goto error;
3903
3904 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905 return 0;
3906
Takashi Iwai352f7f92012-12-19 12:52:06 +01003907error:
3908 generic_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 return err;
3910}
Takashi Iwai1289e9e2008-11-27 15:47:11 +01003911EXPORT_SYMBOL(snd_hda_parse_generic_codec);