blob: 186a5546fcb8cd6f45235785b91b020cb73687d8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Universal Interface for Intel High Definition Audio Codec
3 *
4 * Generic widget tree parser
5 *
6 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
7 *
8 * This driver is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This driver is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/init.h>
24#include <linux/slab.h>
Paul Gortmakerd81a6d72011-09-22 09:34:58 -040025#include <linux/export.h>
Takashi Iwai352f7f92012-12-19 12:52:06 +010026#include <linux/sort.h>
Takashi Iwaif873e532012-12-20 16:58:39 +010027#include <linux/ctype.h>
28#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <sound/core.h>
Takashi Iwai352f7f92012-12-19 12:52:06 +010030#include <sound/jack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "hda_codec.h"
32#include "hda_local.h"
Takashi Iwai352f7f92012-12-19 12:52:06 +010033#include "hda_auto_parser.h"
34#include "hda_jack.h"
35#include "hda_generic.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Takashi Iwai352f7f92012-12-19 12:52:06 +010038/* initialize hda_gen_spec struct */
39int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
Takashi Iwai352f7f92012-12-19 12:52:06 +010041 snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
Takashi Iwai352f7f92012-12-19 12:52:06 +010042 snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
Takashi Iwai38cf6f12012-12-21 14:09:42 +010043 mutex_init(&spec->pcm_mutex);
Takashi Iwai352f7f92012-12-19 12:52:06 +010044 return 0;
45}
46EXPORT_SYMBOL_HDA(snd_hda_gen_spec_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Takashi Iwai12c93df2012-12-19 14:38:33 +010048struct snd_kcontrol_new *
49snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
50 const struct snd_kcontrol_new *temp)
Takashi Iwai352f7f92012-12-19 12:52:06 +010051{
52 struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
53 if (!knew)
54 return NULL;
55 *knew = *temp;
56 if (name)
57 knew->name = kstrdup(name, GFP_KERNEL);
58 else if (knew->name)
59 knew->name = kstrdup(knew->name, GFP_KERNEL);
60 if (!knew->name)
61 return NULL;
62 return knew;
63}
Takashi Iwai12c93df2012-12-19 14:38:33 +010064EXPORT_SYMBOL_HDA(snd_hda_gen_add_kctl);
Takashi Iwai352f7f92012-12-19 12:52:06 +010065
66static void free_kctls(struct hda_gen_spec *spec)
67{
68 if (spec->kctls.list) {
69 struct snd_kcontrol_new *kctl = spec->kctls.list;
70 int i;
71 for (i = 0; i < spec->kctls.used; i++)
72 kfree(kctl[i].name);
73 }
74 snd_array_free(&spec->kctls);
75}
76
Takashi Iwai352f7f92012-12-19 12:52:06 +010077void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
78{
79 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 return;
Takashi Iwai352f7f92012-12-19 12:52:06 +010081 free_kctls(spec);
Takashi Iwai352f7f92012-12-19 12:52:06 +010082 snd_array_free(&spec->paths);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
Takashi Iwai352f7f92012-12-19 12:52:06 +010084EXPORT_SYMBOL_HDA(snd_hda_gen_spec_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86/*
Takashi Iwai1c70a582013-01-11 17:48:22 +010087 * store user hints
88 */
89static void parse_user_hints(struct hda_codec *codec)
90{
91 struct hda_gen_spec *spec = codec->spec;
92 int val;
93
94 val = snd_hda_get_bool_hint(codec, "jack_detect");
95 if (val >= 0)
96 codec->no_jack_detect = !val;
97 val = snd_hda_get_bool_hint(codec, "inv_jack_detect");
98 if (val >= 0)
99 codec->inv_jack_detect = !!val;
100 val = snd_hda_get_bool_hint(codec, "trigger_sense");
101 if (val >= 0)
102 codec->no_trigger_sense = !val;
103 val = snd_hda_get_bool_hint(codec, "inv_eapd");
104 if (val >= 0)
105 codec->inv_eapd = !!val;
106 val = snd_hda_get_bool_hint(codec, "pcm_format_first");
107 if (val >= 0)
108 codec->pcm_format_first = !!val;
109 val = snd_hda_get_bool_hint(codec, "sticky_stream");
110 if (val >= 0)
111 codec->no_sticky_stream = !val;
112 val = snd_hda_get_bool_hint(codec, "spdif_status_reset");
113 if (val >= 0)
114 codec->spdif_status_reset = !!val;
115 val = snd_hda_get_bool_hint(codec, "pin_amp_workaround");
116 if (val >= 0)
117 codec->pin_amp_workaround = !!val;
118 val = snd_hda_get_bool_hint(codec, "single_adc_amp");
119 if (val >= 0)
120 codec->single_adc_amp = !!val;
121
122 val = snd_hda_get_bool_hint(codec, "auto_mic");
123 if (val >= 0)
124 spec->suppress_auto_mic = !val;
125 val = snd_hda_get_bool_hint(codec, "line_in_auto_switch");
126 if (val >= 0)
127 spec->line_in_auto_switch = !!val;
128 val = snd_hda_get_bool_hint(codec, "need_dac_fix");
129 if (val >= 0)
130 spec->need_dac_fix = !!val;
131 val = snd_hda_get_bool_hint(codec, "primary_hp");
132 if (val >= 0)
133 spec->no_primary_hp = !val;
134 val = snd_hda_get_bool_hint(codec, "multi_cap_vol");
135 if (val >= 0)
136 spec->multi_cap_vol = !!val;
137 val = snd_hda_get_bool_hint(codec, "inv_dmic_split");
138 if (val >= 0)
139 spec->inv_dmic_split = !!val;
140 val = snd_hda_get_bool_hint(codec, "indep_hp");
141 if (val >= 0)
142 spec->indep_hp = !!val;
143 val = snd_hda_get_bool_hint(codec, "add_stereo_mix_input");
144 if (val >= 0)
145 spec->add_stereo_mix_input = !!val;
146 val = snd_hda_get_bool_hint(codec, "add_out_jack_modes");
147 if (val >= 0)
148 spec->add_out_jack_modes = !!val;
149
150 if (!snd_hda_get_int_hint(codec, "mixer_nid", &val))
151 spec->mixer_nid = val;
152}
153
154/*
Takashi Iwai2c12c302013-01-10 09:33:29 +0100155 * pin control value accesses
156 */
157
158#define update_pin_ctl(codec, pin, val) \
159 snd_hda_codec_update_cache(codec, pin, 0, \
160 AC_VERB_SET_PIN_WIDGET_CONTROL, val)
161
162/* restore the pinctl based on the cached value */
163static inline void restore_pin_ctl(struct hda_codec *codec, hda_nid_t pin)
164{
165 update_pin_ctl(codec, pin, snd_hda_codec_get_pin_target(codec, pin));
166}
167
168/* set the pinctl target value and write it if requested */
169static void set_pin_target(struct hda_codec *codec, hda_nid_t pin,
170 unsigned int val, bool do_write)
171{
172 if (!pin)
173 return;
174 val = snd_hda_correct_pin_ctl(codec, pin, val);
175 snd_hda_codec_set_pin_target(codec, pin, val);
176 if (do_write)
177 update_pin_ctl(codec, pin, val);
178}
179
180/* set pinctl target values for all given pins */
181static void set_pin_targets(struct hda_codec *codec, int num_pins,
182 hda_nid_t *pins, unsigned int val)
183{
184 int i;
185 for (i = 0; i < num_pins; i++)
186 set_pin_target(codec, pins[i], val, false);
187}
188
189/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100190 * parsing paths
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100193/* return the position of NID in the list, or -1 if not found */
194static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
195{
196 int i;
197 for (i = 0; i < nums; i++)
198 if (list[i] == nid)
199 return i;
200 return -1;
201}
202
203/* return true if the given NID is contained in the path */
204static bool is_nid_contained(struct nid_path *path, hda_nid_t nid)
205{
206 return find_idx_in_nid_list(nid, path->path, path->depth) >= 0;
207}
208
Takashi Iwaif5172a72013-01-04 13:19:55 +0100209static struct nid_path *get_nid_path(struct hda_codec *codec,
210 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100211 int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100213 struct hda_gen_spec *spec = codec->spec;
214 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Takashi Iwai352f7f92012-12-19 12:52:06 +0100216 for (i = 0; i < spec->paths.used; i++) {
217 struct nid_path *path = snd_array_elem(&spec->paths, i);
218 if (path->depth <= 0)
219 continue;
220 if ((!from_nid || path->path[0] == from_nid) &&
Takashi Iwaif5172a72013-01-04 13:19:55 +0100221 (!to_nid || path->path[path->depth - 1] == to_nid)) {
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100222 if (!anchor_nid ||
223 (anchor_nid > 0 && is_nid_contained(path, anchor_nid)) ||
224 (anchor_nid < 0 && !is_nid_contained(path, anchor_nid)))
Takashi Iwaif5172a72013-01-04 13:19:55 +0100225 return path;
226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 }
228 return NULL;
229}
Takashi Iwaif5172a72013-01-04 13:19:55 +0100230
231/* get the path between the given NIDs;
232 * passing 0 to either @pin or @dac behaves as a wildcard
233 */
234struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
235 hda_nid_t from_nid, hda_nid_t to_nid)
236{
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100237 return get_nid_path(codec, from_nid, to_nid, 0);
Takashi Iwaif5172a72013-01-04 13:19:55 +0100238}
Takashi Iwai352f7f92012-12-19 12:52:06 +0100239EXPORT_SYMBOL_HDA(snd_hda_get_nid_path);
240
Takashi Iwai196c17662013-01-04 15:01:40 +0100241/* get the index number corresponding to the path instance;
242 * the index starts from 1, for easier checking the invalid value
243 */
244int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
245{
246 struct hda_gen_spec *spec = codec->spec;
247 struct nid_path *array = spec->paths.list;
248 ssize_t idx;
249
250 if (!spec->paths.used)
251 return 0;
252 idx = path - array;
253 if (idx < 0 || idx >= spec->paths.used)
254 return 0;
255 return idx + 1;
256}
257
258/* get the path instance corresponding to the given index number */
259struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
260{
261 struct hda_gen_spec *spec = codec->spec;
262
263 if (idx <= 0 || idx > spec->paths.used)
264 return NULL;
265 return snd_array_elem(&spec->paths, idx - 1);
266}
267
Takashi Iwai352f7f92012-12-19 12:52:06 +0100268/* check whether the given DAC is already found in any existing paths */
269static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
270{
271 struct hda_gen_spec *spec = codec->spec;
272 int i;
273
274 for (i = 0; i < spec->paths.used; i++) {
275 struct nid_path *path = snd_array_elem(&spec->paths, i);
276 if (path->path[0] == nid)
277 return true;
278 }
279 return false;
280}
281
282/* check whether the given two widgets can be connected */
283static bool is_reachable_path(struct hda_codec *codec,
284 hda_nid_t from_nid, hda_nid_t to_nid)
285{
286 if (!from_nid || !to_nid)
287 return false;
288 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
289}
290
291/* nid, dir and idx */
292#define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
293
294/* check whether the given ctl is already assigned in any path elements */
295static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
296{
297 struct hda_gen_spec *spec = codec->spec;
298 int i;
299
300 val &= AMP_VAL_COMPARE_MASK;
301 for (i = 0; i < spec->paths.used; i++) {
302 struct nid_path *path = snd_array_elem(&spec->paths, i);
303 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
304 return true;
305 }
306 return false;
307}
308
309/* check whether a control with the given (nid, dir, idx) was assigned */
310static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
311 int dir, int idx)
312{
313 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
314 return is_ctl_used(codec, val, NID_PATH_VOL_CTL) ||
315 is_ctl_used(codec, val, NID_PATH_MUTE_CTL);
316}
317
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100318static void print_nid_path(const char *pfx, struct nid_path *path)
319{
320 char buf[40];
321 int i;
322
323
324 buf[0] = 0;
325 for (i = 0; i < path->depth; i++) {
326 char tmp[4];
327 sprintf(tmp, ":%02x", path->path[i]);
328 strlcat(buf, tmp, sizeof(buf));
329 }
330 snd_printdd("%s path: depth=%d %s\n", pfx, path->depth, buf);
331}
332
Takashi Iwai352f7f92012-12-19 12:52:06 +0100333/* called recursively */
334static bool __parse_nid_path(struct hda_codec *codec,
335 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100336 int anchor_nid, struct nid_path *path,
337 int depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100338{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100339 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100340 int i, nums;
341
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100342 if (to_nid == anchor_nid)
343 anchor_nid = 0; /* anchor passed */
344 else if (to_nid == (hda_nid_t)(-anchor_nid))
345 return false; /* hit the exclusive nid */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100346
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100347 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100348 for (i = 0; i < nums; i++) {
349 if (conn[i] != from_nid) {
350 /* special case: when from_nid is 0,
351 * try to find an empty DAC
352 */
353 if (from_nid ||
354 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
355 is_dac_already_used(codec, conn[i]))
356 continue;
357 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100358 /* anchor is not requested or already passed? */
359 if (anchor_nid <= 0)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100360 goto found;
361 }
362 if (depth >= MAX_NID_PATH_DEPTH)
363 return false;
364 for (i = 0; i < nums; i++) {
365 unsigned int type;
366 type = get_wcaps_type(get_wcaps(codec, conn[i]));
367 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
368 type == AC_WID_PIN)
369 continue;
370 if (__parse_nid_path(codec, from_nid, conn[i],
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100371 anchor_nid, path, depth + 1))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100372 goto found;
373 }
374 return false;
375
376 found:
377 path->path[path->depth] = conn[i];
378 path->idx[path->depth + 1] = i;
379 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
380 path->multi[path->depth + 1] = 1;
381 path->depth++;
382 return true;
383}
384
385/* parse the widget path from the given nid to the target nid;
386 * when @from_nid is 0, try to find an empty DAC;
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100387 * when @anchor_nid is set to a positive value, only paths through the widget
388 * with the given value are evaluated.
389 * when @anchor_nid is set to a negative value, paths through the widget
390 * with the negative of given value are excluded, only other paths are chosen.
391 * when @anchor_nid is zero, no special handling about path selection.
Takashi Iwai352f7f92012-12-19 12:52:06 +0100392 */
393bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100394 hda_nid_t to_nid, int anchor_nid,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100395 struct nid_path *path)
396{
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100397 if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100398 path->path[path->depth] = to_nid;
399 path->depth++;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100400 return true;
401 }
402 return false;
403}
404EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100407 * parse the path between the given NIDs and add to the path list.
408 * if no valid path is found, return NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100410struct nid_path *
411snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100412 hda_nid_t to_nid, int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100414 struct hda_gen_spec *spec = codec->spec;
415 struct nid_path *path;
416
417 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
418 return NULL;
419
Takashi Iwaif5172a72013-01-04 13:19:55 +0100420 /* check whether the path has been already added */
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100421 path = get_nid_path(codec, from_nid, to_nid, anchor_nid);
Takashi Iwaif5172a72013-01-04 13:19:55 +0100422 if (path)
423 return path;
424
Takashi Iwai352f7f92012-12-19 12:52:06 +0100425 path = snd_array_new(&spec->paths);
426 if (!path)
427 return NULL;
428 memset(path, 0, sizeof(*path));
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100429 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100430 return path;
431 /* push back */
432 spec->paths.used--;
433 return NULL;
434}
435EXPORT_SYMBOL_HDA(snd_hda_add_new_path);
436
Takashi Iwai980428c2013-01-09 09:28:20 +0100437/* clear the given path as invalid so that it won't be picked up later */
438static void invalidate_nid_path(struct hda_codec *codec, int idx)
439{
440 struct nid_path *path = snd_hda_get_path_from_idx(codec, idx);
441 if (!path)
442 return;
443 memset(path, 0, sizeof(*path));
444}
445
Takashi Iwai352f7f92012-12-19 12:52:06 +0100446/* look for an empty DAC slot */
447static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
448 bool is_digital)
449{
450 struct hda_gen_spec *spec = codec->spec;
451 bool cap_digital;
452 int i;
453
454 for (i = 0; i < spec->num_all_dacs; i++) {
455 hda_nid_t nid = spec->all_dacs[i];
456 if (!nid || is_dac_already_used(codec, nid))
457 continue;
458 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
459 if (is_digital != cap_digital)
460 continue;
461 if (is_reachable_path(codec, nid, pin))
462 return nid;
463 }
464 return 0;
465}
466
467/* replace the channels in the composed amp value with the given number */
468static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
469{
470 val &= ~(0x3U << 16);
471 val |= chs << 16;
472 return val;
473}
474
475/* check whether the widget has the given amp capability for the direction */
476static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
477 int dir, unsigned int bits)
478{
479 if (!nid)
480 return false;
481 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
482 if (query_amp_caps(codec, nid, dir) & bits)
483 return true;
484 return false;
485}
486
David Henningsson99a55922013-01-16 15:58:44 +0100487static bool same_amp_caps(struct hda_codec *codec, hda_nid_t nid1,
488 hda_nid_t nid2, int dir)
489{
490 if (!(get_wcaps(codec, nid1) & (1 << (dir + 1))))
491 return !(get_wcaps(codec, nid2) & (1 << (dir + 1)));
492 return (query_amp_caps(codec, nid1, dir) ==
493 query_amp_caps(codec, nid2, dir));
494}
495
Takashi Iwai352f7f92012-12-19 12:52:06 +0100496#define nid_has_mute(codec, nid, dir) \
497 check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
498#define nid_has_volume(codec, nid, dir) \
499 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
500
501/* look for a widget suitable for assigning a mute switch in the path */
502static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
503 struct nid_path *path)
504{
505 int i;
506
507 for (i = path->depth - 1; i >= 0; i--) {
508 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
509 return path->path[i];
510 if (i != path->depth - 1 && i != 0 &&
511 nid_has_mute(codec, path->path[i], HDA_INPUT))
512 return path->path[i];
513 }
514 return 0;
515}
516
517/* look for a widget suitable for assigning a volume ctl in the path */
518static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
519 struct nid_path *path)
520{
521 int i;
522
523 for (i = path->depth - 1; i >= 0; i--) {
524 if (nid_has_volume(codec, path->path[i], HDA_OUTPUT))
525 return path->path[i];
526 }
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200527 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
530/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100531 * path activation / deactivation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100533
534/* can have the amp-in capability? */
535static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100537 hda_nid_t nid = path->path[idx];
538 unsigned int caps = get_wcaps(codec, nid);
539 unsigned int type = get_wcaps_type(caps);
540
541 if (!(caps & AC_WCAP_IN_AMP))
542 return false;
543 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
544 return false;
545 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546}
547
Takashi Iwai352f7f92012-12-19 12:52:06 +0100548/* can have the amp-out capability? */
549static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100551 hda_nid_t nid = path->path[idx];
552 unsigned int caps = get_wcaps(codec, nid);
553 unsigned int type = get_wcaps_type(caps);
554
555 if (!(caps & AC_WCAP_OUT_AMP))
556 return false;
557 if (type == AC_WID_PIN && !idx) /* only for output pins */
558 return false;
559 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560}
561
Takashi Iwai352f7f92012-12-19 12:52:06 +0100562/* check whether the given (nid,dir,idx) is active */
563static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
564 unsigned int idx, unsigned int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100566 struct hda_gen_spec *spec = codec->spec;
567 int i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Takashi Iwai352f7f92012-12-19 12:52:06 +0100569 for (n = 0; n < spec->paths.used; n++) {
570 struct nid_path *path = snd_array_elem(&spec->paths, n);
571 if (!path->active)
572 continue;
573 for (i = 0; i < path->depth; i++) {
574 if (path->path[i] == nid) {
575 if (dir == HDA_OUTPUT || path->idx[i] == idx)
576 return true;
577 break;
578 }
579 }
580 }
581 return false;
582}
583
584/* get the default amp value for the target state */
585static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
586 int dir, bool enable)
587{
588 unsigned int caps;
589 unsigned int val = 0;
590
591 caps = query_amp_caps(codec, nid, dir);
592 if (caps & AC_AMPCAP_NUM_STEPS) {
593 /* set to 0dB */
594 if (enable)
595 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
596 }
597 if (caps & AC_AMPCAP_MUTE) {
598 if (!enable)
599 val |= HDA_AMP_MUTE;
600 }
601 return val;
602}
603
604/* initialize the amp value (only at the first time) */
605static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
606{
607 int val = get_amp_val_to_activate(codec, nid, dir, false);
608 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
609}
610
611static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
612 int idx, bool enable)
613{
614 int val;
615 if (is_ctl_associated(codec, nid, dir, idx) ||
Takashi Iwai985803c2013-01-03 16:30:04 +0100616 (!enable && is_active_nid(codec, nid, dir, idx)))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100617 return;
618 val = get_amp_val_to_activate(codec, nid, dir, enable);
619 snd_hda_codec_amp_stereo(codec, nid, dir, idx, 0xff, val);
620}
621
622static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
623 int i, bool enable)
624{
625 hda_nid_t nid = path->path[i];
626 init_amp(codec, nid, HDA_OUTPUT, 0);
627 activate_amp(codec, nid, HDA_OUTPUT, 0, enable);
628}
629
630static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
631 int i, bool enable, bool add_aamix)
632{
633 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100634 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100635 int n, nums, idx;
636 int type;
637 hda_nid_t nid = path->path[i];
638
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100639 nums = snd_hda_get_conn_list(codec, nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100640 type = get_wcaps_type(get_wcaps(codec, nid));
641 if (type == AC_WID_PIN ||
642 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
643 nums = 1;
644 idx = 0;
645 } else
646 idx = path->idx[i];
647
648 for (n = 0; n < nums; n++)
649 init_amp(codec, nid, HDA_INPUT, n);
650
651 if (is_ctl_associated(codec, nid, HDA_INPUT, idx))
652 return;
653
654 /* here is a little bit tricky in comparison with activate_amp_out();
655 * when aa-mixer is available, we need to enable the path as well
656 */
657 for (n = 0; n < nums; n++) {
658 if (n != idx && (!add_aamix || conn[n] != spec->mixer_nid))
659 continue;
660 activate_amp(codec, nid, HDA_INPUT, n, enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 }
662}
663
Takashi Iwai352f7f92012-12-19 12:52:06 +0100664/* activate or deactivate the given path
665 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100667void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
668 bool enable, bool add_aamix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100670 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Takashi Iwai352f7f92012-12-19 12:52:06 +0100672 if (!enable)
673 path->active = false;
674
675 for (i = path->depth - 1; i >= 0; i--) {
676 if (enable && path->multi[i])
677 snd_hda_codec_write_cache(codec, path->path[i], 0,
678 AC_VERB_SET_CONNECT_SEL,
679 path->idx[i]);
680 if (has_amp_in(codec, path, i))
681 activate_amp_in(codec, path, i, enable, add_aamix);
682 if (has_amp_out(codec, path, i))
683 activate_amp_out(codec, path, i, enable);
684 }
685
686 if (enable)
687 path->active = true;
688}
689EXPORT_SYMBOL_HDA(snd_hda_activate_path);
690
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100691/* turn on/off EAPD on the given pin */
692static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
693{
694 struct hda_gen_spec *spec = codec->spec;
695 if (spec->own_eapd_ctl ||
696 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
697 return;
Takashi Iwaiecac3ed2012-12-21 15:23:01 +0100698 if (codec->inv_eapd)
699 enable = !enable;
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100700 snd_hda_codec_update_cache(codec, pin, 0,
701 AC_VERB_SET_EAPD_BTLENABLE,
702 enable ? 0x02 : 0x00);
703}
704
Takashi Iwai352f7f92012-12-19 12:52:06 +0100705
706/*
707 * Helper functions for creating mixer ctl elements
708 */
709
710enum {
711 HDA_CTL_WIDGET_VOL,
712 HDA_CTL_WIDGET_MUTE,
713 HDA_CTL_BIND_MUTE,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100714};
715static const struct snd_kcontrol_new control_templates[] = {
716 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
717 HDA_CODEC_MUTE(NULL, 0, 0, 0),
718 HDA_BIND_MUTE(NULL, 0, 0, 0),
Takashi Iwai352f7f92012-12-19 12:52:06 +0100719};
720
721/* add dynamic controls from template */
722static int add_control(struct hda_gen_spec *spec, int type, const char *name,
723 int cidx, unsigned long val)
724{
725 struct snd_kcontrol_new *knew;
726
Takashi Iwai12c93df2012-12-19 14:38:33 +0100727 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100728 if (!knew)
729 return -ENOMEM;
730 knew->index = cidx;
731 if (get_amp_nid_(val))
732 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
733 knew->private_value = val;
734 return 0;
735}
736
737static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
738 const char *pfx, const char *dir,
739 const char *sfx, int cidx, unsigned long val)
740{
741 char name[32];
742 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
743 return add_control(spec, type, name, cidx, val);
744}
745
746#define add_pb_vol_ctrl(spec, type, pfx, val) \
747 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
748#define add_pb_sw_ctrl(spec, type, pfx, val) \
749 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
750#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
751 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
752#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
753 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
754
755static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
756 unsigned int chs, struct nid_path *path)
757{
758 unsigned int val;
759 if (!path)
760 return 0;
761 val = path->ctls[NID_PATH_VOL_CTL];
762 if (!val)
763 return 0;
764 val = amp_val_replace_channels(val, chs);
765 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
766}
767
768/* return the channel bits suitable for the given path->ctls[] */
769static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
770 int type)
771{
772 int chs = 1; /* mono (left only) */
773 if (path) {
774 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
775 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
776 chs = 3; /* stereo */
777 }
778 return chs;
779}
780
781static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
782 struct nid_path *path)
783{
784 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
785 return add_vol_ctl(codec, pfx, cidx, chs, path);
786}
787
788/* create a mute-switch for the given mixer widget;
789 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
790 */
791static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
792 unsigned int chs, struct nid_path *path)
793{
794 unsigned int val;
795 int type = HDA_CTL_WIDGET_MUTE;
796
797 if (!path)
798 return 0;
799 val = path->ctls[NID_PATH_MUTE_CTL];
800 if (!val)
801 return 0;
802 val = amp_val_replace_channels(val, chs);
803 if (get_amp_direction_(val) == HDA_INPUT) {
804 hda_nid_t nid = get_amp_nid_(val);
805 int nums = snd_hda_get_num_conns(codec, nid);
806 if (nums > 1) {
807 type = HDA_CTL_BIND_MUTE;
808 val |= nums << 19;
809 }
810 }
811 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
812}
813
814static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
815 int cidx, struct nid_path *path)
816{
817 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
818 return add_sw_ctl(codec, pfx, cidx, chs, path);
819}
820
821static const char * const channel_name[4] = {
822 "Front", "Surround", "CLFE", "Side"
823};
824
825/* give some appropriate ctl name prefix for the given line out channel */
826static const char *get_line_out_pfx(struct hda_gen_spec *spec, int ch,
827 bool can_be_master, int *index)
828{
829 struct auto_pin_cfg *cfg = &spec->autocfg;
830
831 *index = 0;
832 if (cfg->line_outs == 1 && !spec->multi_ios &&
833 !cfg->hp_outs && !cfg->speaker_outs && can_be_master)
834 return spec->vmaster_mute.hook ? "PCM" : "Master";
835
836 /* if there is really a single DAC used in the whole output paths,
837 * use it master (or "PCM" if a vmaster hook is present)
838 */
839 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
840 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
841 return spec->vmaster_mute.hook ? "PCM" : "Master";
842
843 switch (cfg->line_out_type) {
844 case AUTO_PIN_SPEAKER_OUT:
845 if (cfg->line_outs == 1)
846 return "Speaker";
847 if (cfg->line_outs == 2)
848 return ch ? "Bass Speaker" : "Speaker";
849 break;
850 case AUTO_PIN_HP_OUT:
851 /* for multi-io case, only the primary out */
852 if (ch && spec->multi_ios)
853 break;
854 *index = ch;
855 return "Headphone";
856 default:
857 if (cfg->line_outs == 1 && !spec->multi_ios)
858 return "PCM";
859 break;
860 }
861 if (ch >= ARRAY_SIZE(channel_name)) {
862 snd_BUG();
863 return "PCM";
864 }
865
866 return channel_name[ch];
867}
868
869/*
870 * Parse output paths
871 */
872
873/* badness definition */
874enum {
875 /* No primary DAC is found for the main output */
876 BAD_NO_PRIMARY_DAC = 0x10000,
877 /* No DAC is found for the extra output */
878 BAD_NO_DAC = 0x4000,
879 /* No possible multi-ios */
880 BAD_MULTI_IO = 0x103,
881 /* No individual DAC for extra output */
882 BAD_NO_EXTRA_DAC = 0x102,
883 /* No individual DAC for extra surrounds */
884 BAD_NO_EXTRA_SURR_DAC = 0x101,
885 /* Primary DAC shared with main surrounds */
886 BAD_SHARED_SURROUND = 0x100,
887 /* Primary DAC shared with main CLFE */
888 BAD_SHARED_CLFE = 0x10,
889 /* Primary DAC shared with extra surrounds */
890 BAD_SHARED_EXTRA_SURROUND = 0x10,
891 /* Volume widget is shared */
892 BAD_SHARED_VOL = 0x10,
893};
894
Takashi Iwai0e614dd2013-01-07 15:11:44 +0100895/* look for widgets in the given path which are appropriate for
Takashi Iwai352f7f92012-12-19 12:52:06 +0100896 * volume and mute controls, and assign the values to ctls[].
897 *
898 * When no appropriate widget is found in the path, the badness value
899 * is incremented depending on the situation. The function returns the
900 * total badness for both volume and mute controls.
901 */
Takashi Iwai0e614dd2013-01-07 15:11:44 +0100902static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100903{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100904 hda_nid_t nid;
905 unsigned int val;
906 int badness = 0;
907
908 if (!path)
909 return BAD_SHARED_VOL * 2;
Takashi Iwai0e614dd2013-01-07 15:11:44 +0100910
911 if (path->ctls[NID_PATH_VOL_CTL] ||
912 path->ctls[NID_PATH_MUTE_CTL])
913 return 0; /* already evaluated */
914
Takashi Iwai352f7f92012-12-19 12:52:06 +0100915 nid = look_for_out_vol_nid(codec, path);
916 if (nid) {
917 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
918 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
919 badness += BAD_SHARED_VOL;
920 else
921 path->ctls[NID_PATH_VOL_CTL] = val;
922 } else
923 badness += BAD_SHARED_VOL;
924 nid = look_for_out_mute_nid(codec, path);
925 if (nid) {
926 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
927 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
928 nid_has_mute(codec, nid, HDA_OUTPUT))
929 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
930 else
931 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
932 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
933 badness += BAD_SHARED_VOL;
934 else
935 path->ctls[NID_PATH_MUTE_CTL] = val;
936 } else
937 badness += BAD_SHARED_VOL;
938 return badness;
939}
940
941struct badness_table {
942 int no_primary_dac; /* no primary DAC */
943 int no_dac; /* no secondary DACs */
944 int shared_primary; /* primary DAC is shared with main output */
945 int shared_surr; /* secondary DAC shared with main or primary */
946 int shared_clfe; /* third DAC shared with main or primary */
947 int shared_surr_main; /* secondary DAC sahred with main/DAC0 */
948};
949
950static struct badness_table main_out_badness = {
951 .no_primary_dac = BAD_NO_PRIMARY_DAC,
952 .no_dac = BAD_NO_DAC,
953 .shared_primary = BAD_NO_PRIMARY_DAC,
954 .shared_surr = BAD_SHARED_SURROUND,
955 .shared_clfe = BAD_SHARED_CLFE,
956 .shared_surr_main = BAD_SHARED_SURROUND,
957};
958
959static struct badness_table extra_out_badness = {
960 .no_primary_dac = BAD_NO_DAC,
961 .no_dac = BAD_NO_DAC,
962 .shared_primary = BAD_NO_EXTRA_DAC,
963 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
964 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
965 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
966};
967
Takashi Iwai7385df62013-01-07 09:50:52 +0100968/* get the DAC of the primary output corresponding to the given array index */
969static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
970{
971 struct hda_gen_spec *spec = codec->spec;
972 struct auto_pin_cfg *cfg = &spec->autocfg;
973
974 if (cfg->line_outs > idx)
975 return spec->private_dac_nids[idx];
976 idx -= cfg->line_outs;
977 if (spec->multi_ios > idx)
978 return spec->multi_io[idx].dac;
979 return 0;
980}
981
982/* return the DAC if it's reachable, otherwise zero */
983static inline hda_nid_t try_dac(struct hda_codec *codec,
984 hda_nid_t dac, hda_nid_t pin)
985{
986 return is_reachable_path(codec, dac, pin) ? dac : 0;
987}
988
Takashi Iwai352f7f92012-12-19 12:52:06 +0100989/* try to assign DACs to pins and return the resultant badness */
990static int try_assign_dacs(struct hda_codec *codec, int num_outs,
991 const hda_nid_t *pins, hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +0100992 int *path_idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100993 const struct badness_table *bad)
994{
995 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100996 int i, j;
997 int badness = 0;
998 hda_nid_t dac;
999
1000 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 return 0;
1002
Takashi Iwai352f7f92012-12-19 12:52:06 +01001003 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001004 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001005 hda_nid_t pin = pins[i];
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001006
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001007 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1008 if (path) {
1009 badness += assign_out_path_ctls(codec, path);
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001010 continue;
1011 }
1012
1013 dacs[i] = look_for_dac(codec, pin, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001014 if (!dacs[i] && !i) {
Takashi Iwai980428c2013-01-09 09:28:20 +01001015 /* try to steal the DAC of surrounds for the front */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001016 for (j = 1; j < num_outs; j++) {
1017 if (is_reachable_path(codec, dacs[j], pin)) {
1018 dacs[0] = dacs[j];
1019 dacs[j] = 0;
Takashi Iwai980428c2013-01-09 09:28:20 +01001020 invalidate_nid_path(codec, path_idx[j]);
Takashi Iwai196c17662013-01-04 15:01:40 +01001021 path_idx[j] = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001022 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 }
1024 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001025 }
1026 dac = dacs[i];
1027 if (!dac) {
Takashi Iwai7385df62013-01-07 09:50:52 +01001028 if (num_outs > 2)
1029 dac = try_dac(codec, get_primary_out(codec, i), pin);
1030 if (!dac)
1031 dac = try_dac(codec, dacs[0], pin);
1032 if (!dac)
1033 dac = try_dac(codec, get_primary_out(codec, i), pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001034 if (dac) {
1035 if (!i)
1036 badness += bad->shared_primary;
1037 else if (i == 1)
1038 badness += bad->shared_surr;
1039 else
1040 badness += bad->shared_clfe;
1041 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1042 dac = spec->private_dac_nids[0];
1043 badness += bad->shared_surr_main;
1044 } else if (!i)
1045 badness += bad->no_primary_dac;
1046 else
1047 badness += bad->no_dac;
1048 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001049 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001050 if (!path && !i && spec->mixer_nid) {
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001051 /* try with aamix */
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001052 path = snd_hda_add_new_path(codec, dac, pin, 0);
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001053 }
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001054 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001055 dac = dacs[i] = 0;
Takashi Iwaie1284af2013-01-03 16:33:02 +01001056 else {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001057 print_nid_path("output", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001058 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001059 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001060 badness += assign_out_path_ctls(codec, path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001061 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001062 }
1063
1064 return badness;
1065}
1066
1067/* return NID if the given pin has only a single connection to a certain DAC */
1068static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1069{
1070 struct hda_gen_spec *spec = codec->spec;
1071 int i;
1072 hda_nid_t nid_found = 0;
1073
1074 for (i = 0; i < spec->num_all_dacs; i++) {
1075 hda_nid_t nid = spec->all_dacs[i];
1076 if (!nid || is_dac_already_used(codec, nid))
1077 continue;
1078 if (is_reachable_path(codec, nid, pin)) {
1079 if (nid_found)
1080 return 0;
1081 nid_found = nid;
1082 }
1083 }
1084 return nid_found;
1085}
1086
1087/* check whether the given pin can be a multi-io pin */
1088static bool can_be_multiio_pin(struct hda_codec *codec,
1089 unsigned int location, hda_nid_t nid)
1090{
1091 unsigned int defcfg, caps;
1092
1093 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1094 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1095 return false;
1096 if (location && get_defcfg_location(defcfg) != location)
1097 return false;
1098 caps = snd_hda_query_pin_caps(codec, nid);
1099 if (!(caps & AC_PINCAP_OUT))
1100 return false;
1101 return true;
1102}
1103
Takashi Iwaie22aab72013-01-04 14:50:04 +01001104/* count the number of input pins that are capable to be multi-io */
1105static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1106{
1107 struct hda_gen_spec *spec = codec->spec;
1108 struct auto_pin_cfg *cfg = &spec->autocfg;
1109 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1110 unsigned int location = get_defcfg_location(defcfg);
1111 int type, i;
1112 int num_pins = 0;
1113
1114 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1115 for (i = 0; i < cfg->num_inputs; i++) {
1116 if (cfg->inputs[i].type != type)
1117 continue;
1118 if (can_be_multiio_pin(codec, location,
1119 cfg->inputs[i].pin))
1120 num_pins++;
1121 }
1122 }
1123 return num_pins;
1124}
1125
Takashi Iwai352f7f92012-12-19 12:52:06 +01001126/*
1127 * multi-io helper
1128 *
1129 * When hardwired is set, try to fill ony hardwired pins, and returns
1130 * zero if any pins are filled, non-zero if nothing found.
1131 * When hardwired is off, try to fill possible input pins, and returns
1132 * the badness value.
1133 */
1134static int fill_multi_ios(struct hda_codec *codec,
1135 hda_nid_t reference_pin,
Takashi Iwaie22aab72013-01-04 14:50:04 +01001136 bool hardwired)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001137{
1138 struct hda_gen_spec *spec = codec->spec;
1139 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie22aab72013-01-04 14:50:04 +01001140 int type, i, j, num_pins, old_pins;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001141 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1142 unsigned int location = get_defcfg_location(defcfg);
1143 int badness = 0;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001144 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001145
1146 old_pins = spec->multi_ios;
1147 if (old_pins >= 2)
1148 goto end_fill;
1149
Takashi Iwaie22aab72013-01-04 14:50:04 +01001150 num_pins = count_multiio_pins(codec, reference_pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001151 if (num_pins < 2)
1152 goto end_fill;
1153
Takashi Iwai352f7f92012-12-19 12:52:06 +01001154 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1155 for (i = 0; i < cfg->num_inputs; i++) {
1156 hda_nid_t nid = cfg->inputs[i].pin;
1157 hda_nid_t dac = 0;
1158
1159 if (cfg->inputs[i].type != type)
1160 continue;
1161 if (!can_be_multiio_pin(codec, location, nid))
1162 continue;
1163 for (j = 0; j < spec->multi_ios; j++) {
1164 if (nid == spec->multi_io[j].pin)
1165 break;
1166 }
1167 if (j < spec->multi_ios)
1168 continue;
1169
Takashi Iwai352f7f92012-12-19 12:52:06 +01001170 if (hardwired)
1171 dac = get_dac_if_single(codec, nid);
1172 else if (!dac)
1173 dac = look_for_dac(codec, nid, false);
1174 if (!dac) {
1175 badness++;
1176 continue;
1177 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001178 path = snd_hda_add_new_path(codec, dac, nid,
1179 -spec->mixer_nid);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001180 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001181 badness++;
1182 continue;
1183 }
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001184 print_nid_path("multiio", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001185 spec->multi_io[spec->multi_ios].pin = nid;
1186 spec->multi_io[spec->multi_ios].dac = dac;
Takashi Iwai196c17662013-01-04 15:01:40 +01001187 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1188 snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001189 spec->multi_ios++;
1190 if (spec->multi_ios >= 2)
1191 break;
1192 }
1193 }
1194 end_fill:
1195 if (badness)
1196 badness = BAD_MULTI_IO;
1197 if (old_pins == spec->multi_ios) {
1198 if (hardwired)
1199 return 1; /* nothing found */
1200 else
1201 return badness; /* no badness if nothing found */
1202 }
1203 if (!hardwired && spec->multi_ios < 2) {
1204 /* cancel newly assigned paths */
1205 spec->paths.used -= spec->multi_ios - old_pins;
1206 spec->multi_ios = old_pins;
1207 return badness;
1208 }
1209
1210 /* assign volume and mute controls */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001211 for (i = old_pins; i < spec->multi_ios; i++) {
1212 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1213 badness += assign_out_path_ctls(codec, path);
1214 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001215
1216 return badness;
1217}
1218
1219/* map DACs for all pins in the list if they are single connections */
1220static bool map_singles(struct hda_codec *codec, int outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001221 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001222{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001223 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001224 int i;
1225 bool found = false;
1226 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001227 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001228 hda_nid_t dac;
1229 if (dacs[i])
1230 continue;
1231 dac = get_dac_if_single(codec, pins[i]);
1232 if (!dac)
1233 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001234 path = snd_hda_add_new_path(codec, dac, pins[i],
1235 -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001236 if (!path && !i && spec->mixer_nid)
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001237 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001238 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001239 dacs[i] = dac;
1240 found = true;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001241 print_nid_path("output", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001242 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001243 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001244 }
1245 }
1246 return found;
1247}
1248
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001249/* create a new path including aamix if available, and return its index */
1250static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1251{
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001252 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001253 struct nid_path *path;
1254
1255 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001256 if (!path || !path->depth ||
1257 is_nid_contained(path, spec->mixer_nid))
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001258 return 0;
1259 path = snd_hda_add_new_path(codec, path->path[0],
1260 path->path[path->depth - 1],
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001261 spec->mixer_nid);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001262 if (!path)
1263 return 0;
1264 print_nid_path("output-aamix", path);
1265 path->active = false; /* unused as default */
1266 return snd_hda_get_path_idx(codec, path);
1267}
1268
Takashi Iwaia07a9492013-01-07 16:44:06 +01001269/* fill the empty entries in the dac array for speaker/hp with the
1270 * shared dac pointed by the paths
1271 */
1272static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1273 hda_nid_t *dacs, int *path_idx)
1274{
1275 struct nid_path *path;
1276 int i;
1277
1278 for (i = 0; i < num_outs; i++) {
1279 if (dacs[i])
1280 continue;
1281 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1282 if (!path)
1283 continue;
1284 dacs[i] = path->path[0];
1285 }
1286}
1287
Takashi Iwai352f7f92012-12-19 12:52:06 +01001288/* fill in the dac_nids table from the parsed pin configuration */
1289static int fill_and_eval_dacs(struct hda_codec *codec,
1290 bool fill_hardwired,
1291 bool fill_mio_first)
1292{
1293 struct hda_gen_spec *spec = codec->spec;
1294 struct auto_pin_cfg *cfg = &spec->autocfg;
1295 int i, err, badness;
Takashi Iwaiea46c3c2013-01-15 18:45:53 +01001296 unsigned int val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001297
1298 /* set num_dacs once to full for look_for_dac() */
1299 spec->multiout.num_dacs = cfg->line_outs;
1300 spec->multiout.dac_nids = spec->private_dac_nids;
1301 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1302 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1303 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1304 spec->multi_ios = 0;
1305 snd_array_free(&spec->paths);
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001306
1307 /* clear path indices */
1308 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1309 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1310 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1311 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1312 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
Takashi Iwaic697b712013-01-07 17:09:26 +01001313 memset(spec->input_paths, 0, sizeof(spec->input_paths));
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001314 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1315 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1316
Takashi Iwai352f7f92012-12-19 12:52:06 +01001317 badness = 0;
1318
1319 /* fill hard-wired DACs first */
1320 if (fill_hardwired) {
1321 bool mapped;
1322 do {
1323 mapped = map_singles(codec, cfg->line_outs,
1324 cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001325 spec->private_dac_nids,
1326 spec->out_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001327 mapped |= map_singles(codec, cfg->hp_outs,
1328 cfg->hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001329 spec->multiout.hp_out_nid,
1330 spec->hp_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001331 mapped |= map_singles(codec, cfg->speaker_outs,
1332 cfg->speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001333 spec->multiout.extra_out_nid,
1334 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001335 if (fill_mio_first && cfg->line_outs == 1 &&
1336 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001337 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001338 if (!err)
1339 mapped = true;
1340 }
1341 } while (mapped);
1342 }
1343
1344 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001345 spec->private_dac_nids, spec->out_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001346 &main_out_badness);
1347
Takashi Iwai352f7f92012-12-19 12:52:06 +01001348 if (fill_mio_first &&
1349 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1350 /* try to fill multi-io first */
Takashi Iwaie22aab72013-01-04 14:50:04 +01001351 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001352 if (err < 0)
1353 return err;
1354 /* we don't count badness at this stage yet */
1355 }
1356
1357 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1358 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1359 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001360 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001361 &extra_out_badness);
1362 if (err < 0)
1363 return err;
1364 badness += err;
1365 }
1366 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1367 err = try_assign_dacs(codec, cfg->speaker_outs,
1368 cfg->speaker_pins,
1369 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001370 spec->speaker_paths,
1371 &extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001372 if (err < 0)
1373 return err;
1374 badness += err;
1375 }
1376 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001377 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001378 if (err < 0)
1379 return err;
1380 badness += err;
1381 }
Takashi Iwaie22aab72013-01-04 14:50:04 +01001382
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001383 if (spec->mixer_nid) {
1384 spec->aamix_out_paths[0] =
1385 check_aamix_out_path(codec, spec->out_paths[0]);
1386 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1387 spec->aamix_out_paths[1] =
1388 check_aamix_out_path(codec, spec->hp_paths[0]);
1389 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1390 spec->aamix_out_paths[2] =
1391 check_aamix_out_path(codec, spec->speaker_paths[0]);
1392 }
1393
Takashi Iwaie22aab72013-01-04 14:50:04 +01001394 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1395 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1396 spec->multi_ios = 1; /* give badness */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001397
Takashi Iwaia07a9492013-01-07 16:44:06 +01001398 /* re-count num_dacs and squash invalid entries */
1399 spec->multiout.num_dacs = 0;
1400 for (i = 0; i < cfg->line_outs; i++) {
1401 if (spec->private_dac_nids[i])
1402 spec->multiout.num_dacs++;
1403 else {
1404 memmove(spec->private_dac_nids + i,
1405 spec->private_dac_nids + i + 1,
1406 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1407 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1408 }
1409 }
1410
1411 spec->ext_channel_count = spec->min_channel_count =
David Henningssonc0f3b212013-01-16 11:45:37 +01001412 spec->multiout.num_dacs * 2;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001413
Takashi Iwai352f7f92012-12-19 12:52:06 +01001414 if (spec->multi_ios == 2) {
1415 for (i = 0; i < 2; i++)
1416 spec->private_dac_nids[spec->multiout.num_dacs++] =
1417 spec->multi_io[i].dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001418 } else if (spec->multi_ios) {
1419 spec->multi_ios = 0;
1420 badness += BAD_MULTI_IO;
1421 }
1422
Takashi Iwaia07a9492013-01-07 16:44:06 +01001423 /* re-fill the shared DAC for speaker / headphone */
1424 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1425 refill_shared_dacs(codec, cfg->hp_outs,
1426 spec->multiout.hp_out_nid,
1427 spec->hp_paths);
1428 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1429 refill_shared_dacs(codec, cfg->speaker_outs,
1430 spec->multiout.extra_out_nid,
1431 spec->speaker_paths);
1432
Takashi Iwai2c12c302013-01-10 09:33:29 +01001433 /* set initial pinctl targets */
Takashi Iwaiea46c3c2013-01-15 18:45:53 +01001434 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
1435 val = PIN_HP;
1436 else
1437 val = PIN_OUT;
1438 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
Takashi Iwai2c12c302013-01-10 09:33:29 +01001439 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1440 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
Takashi Iwaiea46c3c2013-01-15 18:45:53 +01001441 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1442 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
Takashi Iwai2c12c302013-01-10 09:33:29 +01001443 set_pin_targets(codec, cfg->speaker_outs,
Takashi Iwaiea46c3c2013-01-15 18:45:53 +01001444 cfg->speaker_pins, val);
1445 }
Takashi Iwai2c12c302013-01-10 09:33:29 +01001446
Takashi Iwai352f7f92012-12-19 12:52:06 +01001447 return badness;
1448}
1449
1450#define DEBUG_BADNESS
1451
1452#ifdef DEBUG_BADNESS
1453#define debug_badness snd_printdd
1454#else
1455#define debug_badness(...)
1456#endif
1457
1458static void debug_show_configs(struct hda_gen_spec *spec, struct auto_pin_cfg *cfg)
1459{
1460 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1461 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001462 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001463 spec->multiout.dac_nids[0],
1464 spec->multiout.dac_nids[1],
1465 spec->multiout.dac_nids[2],
1466 spec->multiout.dac_nids[3]);
1467 if (spec->multi_ios > 0)
1468 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1469 spec->multi_ios,
1470 spec->multi_io[0].pin, spec->multi_io[1].pin,
1471 spec->multi_io[0].dac, spec->multi_io[1].dac);
1472 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1473 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001474 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001475 spec->multiout.hp_out_nid[0],
1476 spec->multiout.hp_out_nid[1],
1477 spec->multiout.hp_out_nid[2],
1478 spec->multiout.hp_out_nid[3]);
1479 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1480 cfg->speaker_pins[0], cfg->speaker_pins[1],
1481 cfg->speaker_pins[2], cfg->speaker_pins[3],
1482 spec->multiout.extra_out_nid[0],
1483 spec->multiout.extra_out_nid[1],
1484 spec->multiout.extra_out_nid[2],
1485 spec->multiout.extra_out_nid[3]);
1486}
1487
1488/* find all available DACs of the codec */
1489static void fill_all_dac_nids(struct hda_codec *codec)
1490{
1491 struct hda_gen_spec *spec = codec->spec;
1492 int i;
1493 hda_nid_t nid = codec->start_nid;
1494
1495 spec->num_all_dacs = 0;
1496 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1497 for (i = 0; i < codec->num_nodes; i++, nid++) {
1498 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1499 continue;
1500 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1501 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1502 break;
1503 }
1504 spec->all_dacs[spec->num_all_dacs++] = nid;
1505 }
1506}
1507
1508static int parse_output_paths(struct hda_codec *codec)
1509{
1510 struct hda_gen_spec *spec = codec->spec;
1511 struct auto_pin_cfg *cfg = &spec->autocfg;
1512 struct auto_pin_cfg *best_cfg;
1513 int best_badness = INT_MAX;
1514 int badness;
1515 bool fill_hardwired = true, fill_mio_first = true;
1516 bool best_wired = true, best_mio = true;
1517 bool hp_spk_swapped = false;
1518
Takashi Iwai352f7f92012-12-19 12:52:06 +01001519 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1520 if (!best_cfg)
1521 return -ENOMEM;
1522 *best_cfg = *cfg;
1523
1524 for (;;) {
1525 badness = fill_and_eval_dacs(codec, fill_hardwired,
1526 fill_mio_first);
1527 if (badness < 0) {
1528 kfree(best_cfg);
1529 return badness;
1530 }
1531 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1532 cfg->line_out_type, fill_hardwired, fill_mio_first,
1533 badness);
1534 debug_show_configs(spec, cfg);
1535 if (badness < best_badness) {
1536 best_badness = badness;
1537 *best_cfg = *cfg;
1538 best_wired = fill_hardwired;
1539 best_mio = fill_mio_first;
1540 }
1541 if (!badness)
1542 break;
1543 fill_mio_first = !fill_mio_first;
1544 if (!fill_mio_first)
1545 continue;
1546 fill_hardwired = !fill_hardwired;
1547 if (!fill_hardwired)
1548 continue;
1549 if (hp_spk_swapped)
1550 break;
1551 hp_spk_swapped = true;
1552 if (cfg->speaker_outs > 0 &&
1553 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1554 cfg->hp_outs = cfg->line_outs;
1555 memcpy(cfg->hp_pins, cfg->line_out_pins,
1556 sizeof(cfg->hp_pins));
1557 cfg->line_outs = cfg->speaker_outs;
1558 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1559 sizeof(cfg->speaker_pins));
1560 cfg->speaker_outs = 0;
1561 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1562 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1563 fill_hardwired = true;
1564 continue;
1565 }
1566 if (cfg->hp_outs > 0 &&
1567 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1568 cfg->speaker_outs = cfg->line_outs;
1569 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1570 sizeof(cfg->speaker_pins));
1571 cfg->line_outs = cfg->hp_outs;
1572 memcpy(cfg->line_out_pins, cfg->hp_pins,
1573 sizeof(cfg->hp_pins));
1574 cfg->hp_outs = 0;
1575 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1576 cfg->line_out_type = AUTO_PIN_HP_OUT;
1577 fill_hardwired = true;
1578 continue;
1579 }
1580 break;
1581 }
1582
1583 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001584 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001585 *cfg = *best_cfg;
1586 fill_and_eval_dacs(codec, best_wired, best_mio);
1587 }
1588 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1589 cfg->line_out_type, best_wired, best_mio);
1590 debug_show_configs(spec, cfg);
1591
1592 if (cfg->line_out_pins[0]) {
1593 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01001594 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001595 if (path)
1596 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
1597 }
1598
1599 kfree(best_cfg);
1600 return 0;
1601}
1602
1603/* add playback controls from the parsed DAC table */
1604static int create_multi_out_ctls(struct hda_codec *codec,
1605 const struct auto_pin_cfg *cfg)
1606{
1607 struct hda_gen_spec *spec = codec->spec;
1608 int i, err, noutputs;
1609
1610 noutputs = cfg->line_outs;
1611 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1612 noutputs += spec->multi_ios;
1613
1614 for (i = 0; i < noutputs; i++) {
1615 const char *name;
1616 int index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001617 struct nid_path *path;
1618
Takashi Iwai352f7f92012-12-19 12:52:06 +01001619 if (i >= cfg->line_outs) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001620 index = 0;
1621 name = channel_name[i];
1622 } else {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001623 name = get_line_out_pfx(spec, i, true, &index);
1624 }
1625
Takashi Iwai196c17662013-01-04 15:01:40 +01001626 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001627 if (!path)
1628 continue;
1629 if (!name || !strcmp(name, "CLFE")) {
1630 /* Center/LFE */
1631 err = add_vol_ctl(codec, "Center", 0, 1, path);
1632 if (err < 0)
1633 return err;
1634 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1635 if (err < 0)
1636 return err;
1637 err = add_sw_ctl(codec, "Center", 0, 1, path);
1638 if (err < 0)
1639 return err;
1640 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1641 if (err < 0)
1642 return err;
1643 } else {
1644 err = add_stereo_vol(codec, name, index, path);
1645 if (err < 0)
1646 return err;
1647 err = add_stereo_sw(codec, name, index, path);
1648 if (err < 0)
1649 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 }
1651 }
1652 return 0;
1653}
1654
Takashi Iwaic2c80382013-01-07 10:33:57 +01001655static int create_extra_out(struct hda_codec *codec, int path_idx,
Takashi Iwai196c17662013-01-04 15:01:40 +01001656 const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001658 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 int err;
1660
Takashi Iwai196c17662013-01-04 15:01:40 +01001661 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001662 if (!path)
1663 return 0;
Takashi Iwaic2c80382013-01-07 10:33:57 +01001664 err = add_stereo_vol(codec, pfx, cidx, path);
1665 if (err < 0)
1666 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001667 err = add_stereo_sw(codec, pfx, cidx, path);
1668 if (err < 0)
1669 return err;
1670 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671}
1672
Takashi Iwai352f7f92012-12-19 12:52:06 +01001673/* add playback controls for speaker and HP outputs */
1674static int create_extra_outs(struct hda_codec *codec, int num_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001675 const int *paths, const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676{
Takashi Iwaic2c80382013-01-07 10:33:57 +01001677 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001678
1679 for (i = 0; i < num_pins; i++) {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001680 const char *name;
1681 char tmp[44];
1682 int err, idx = 0;
1683
1684 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
1685 name = "Bass Speaker";
1686 else if (num_pins >= 3) {
1687 snprintf(tmp, sizeof(tmp), "%s %s",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001688 pfx, channel_name[i]);
Takashi Iwaic2c80382013-01-07 10:33:57 +01001689 name = tmp;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001690 } else {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001691 name = pfx;
1692 idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 }
Takashi Iwaic2c80382013-01-07 10:33:57 +01001694 err = create_extra_out(codec, paths[i], name, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001695 if (err < 0)
1696 return err;
1697 }
1698 return 0;
1699}
Takashi Iwai97ec5582006-03-21 11:29:07 +01001700
Takashi Iwai352f7f92012-12-19 12:52:06 +01001701static int create_hp_out_ctls(struct hda_codec *codec)
1702{
1703 struct hda_gen_spec *spec = codec->spec;
1704 return create_extra_outs(codec, spec->autocfg.hp_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001705 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001706 "Headphone");
1707}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
Takashi Iwai352f7f92012-12-19 12:52:06 +01001709static int create_speaker_out_ctls(struct hda_codec *codec)
1710{
1711 struct hda_gen_spec *spec = codec->spec;
1712 return create_extra_outs(codec, spec->autocfg.speaker_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001713 spec->speaker_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001714 "Speaker");
1715}
1716
1717/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001718 * independent HP controls
1719 */
1720
1721static int indep_hp_info(struct snd_kcontrol *kcontrol,
1722 struct snd_ctl_elem_info *uinfo)
1723{
1724 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
1725}
1726
1727static int indep_hp_get(struct snd_kcontrol *kcontrol,
1728 struct snd_ctl_elem_value *ucontrol)
1729{
1730 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1731 struct hda_gen_spec *spec = codec->spec;
1732 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
1733 return 0;
1734}
1735
1736static int indep_hp_put(struct snd_kcontrol *kcontrol,
1737 struct snd_ctl_elem_value *ucontrol)
1738{
1739 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1740 struct hda_gen_spec *spec = codec->spec;
1741 unsigned int select = ucontrol->value.enumerated.item[0];
1742 int ret = 0;
1743
1744 mutex_lock(&spec->pcm_mutex);
1745 if (spec->active_streams) {
1746 ret = -EBUSY;
1747 goto unlock;
1748 }
1749
1750 if (spec->indep_hp_enabled != select) {
1751 spec->indep_hp_enabled = select;
1752 if (spec->indep_hp_enabled)
1753 spec->multiout.hp_out_nid[0] = 0;
1754 else
1755 spec->multiout.hp_out_nid[0] = spec->alt_dac_nid;
1756 ret = 1;
1757 }
1758 unlock:
1759 mutex_unlock(&spec->pcm_mutex);
1760 return ret;
1761}
1762
1763static const struct snd_kcontrol_new indep_hp_ctl = {
1764 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1765 .name = "Independent HP",
1766 .info = indep_hp_info,
1767 .get = indep_hp_get,
1768 .put = indep_hp_put,
1769};
1770
1771
1772static int create_indep_hp_ctls(struct hda_codec *codec)
1773{
1774 struct hda_gen_spec *spec = codec->spec;
1775
1776 if (!spec->indep_hp)
1777 return 0;
1778 if (!spec->multiout.hp_out_nid[0]) {
1779 spec->indep_hp = 0;
1780 return 0;
1781 }
1782
1783 spec->indep_hp_enabled = false;
1784 spec->alt_dac_nid = spec->multiout.hp_out_nid[0];
1785 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
1786 return -ENOMEM;
1787 return 0;
1788}
1789
1790/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01001791 * channel mode enum control
1792 */
1793
1794static int ch_mode_info(struct snd_kcontrol *kcontrol,
1795 struct snd_ctl_elem_info *uinfo)
1796{
1797 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1798 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001799 int chs;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001800
1801 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1802 uinfo->count = 1;
1803 uinfo->value.enumerated.items = spec->multi_ios + 1;
1804 if (uinfo->value.enumerated.item > spec->multi_ios)
1805 uinfo->value.enumerated.item = spec->multi_ios;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001806 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
1807 sprintf(uinfo->value.enumerated.name, "%dch", chs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001808 return 0;
1809}
1810
1811static int ch_mode_get(struct snd_kcontrol *kcontrol,
1812 struct snd_ctl_elem_value *ucontrol)
1813{
1814 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1815 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001816 ucontrol->value.enumerated.item[0] =
1817 (spec->ext_channel_count - spec->min_channel_count) / 2;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001818 return 0;
1819}
1820
Takashi Iwai196c17662013-01-04 15:01:40 +01001821static inline struct nid_path *
1822get_multiio_path(struct hda_codec *codec, int idx)
1823{
1824 struct hda_gen_spec *spec = codec->spec;
1825 return snd_hda_get_path_from_idx(codec,
1826 spec->out_paths[spec->autocfg.line_outs + idx]);
1827}
1828
Takashi Iwaia5cc2502013-01-16 18:08:55 +01001829static void update_automute_all(struct hda_codec *codec);
1830
Takashi Iwai352f7f92012-12-19 12:52:06 +01001831static int set_multi_io(struct hda_codec *codec, int idx, bool output)
1832{
1833 struct hda_gen_spec *spec = codec->spec;
1834 hda_nid_t nid = spec->multi_io[idx].pin;
1835 struct nid_path *path;
1836
Takashi Iwai196c17662013-01-04 15:01:40 +01001837 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001838 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001840
1841 if (path->active == output)
1842 return 0;
1843
1844 if (output) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01001845 set_pin_target(codec, nid, PIN_OUT, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001846 snd_hda_activate_path(codec, path, true, true);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01001847 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001848 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01001849 set_pin_eapd(codec, nid, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001850 snd_hda_activate_path(codec, path, false, true);
Takashi Iwai2c12c302013-01-10 09:33:29 +01001851 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 }
Takashi Iwaia365fed2013-01-10 16:10:06 +01001853
1854 /* update jack retasking in case it modifies any of them */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01001855 update_automute_all(codec);
Takashi Iwaia365fed2013-01-10 16:10:06 +01001856
Takashi Iwai352f7f92012-12-19 12:52:06 +01001857 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858}
1859
Takashi Iwai352f7f92012-12-19 12:52:06 +01001860static int ch_mode_put(struct snd_kcontrol *kcontrol,
1861 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001863 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1864 struct hda_gen_spec *spec = codec->spec;
1865 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
Takashi Iwai352f7f92012-12-19 12:52:06 +01001867 ch = ucontrol->value.enumerated.item[0];
1868 if (ch < 0 || ch > spec->multi_ios)
1869 return -EINVAL;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001870 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001871 return 0;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001872 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001873 for (i = 0; i < spec->multi_ios; i++)
1874 set_multi_io(codec, i, i < ch);
1875 spec->multiout.max_channels = max(spec->ext_channel_count,
1876 spec->const_channel_count);
1877 if (spec->need_dac_fix)
1878 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 return 1;
1880}
1881
Takashi Iwai352f7f92012-12-19 12:52:06 +01001882static const struct snd_kcontrol_new channel_mode_enum = {
1883 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1884 .name = "Channel Mode",
1885 .info = ch_mode_info,
1886 .get = ch_mode_get,
1887 .put = ch_mode_put,
1888};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
Takashi Iwai352f7f92012-12-19 12:52:06 +01001890static int create_multi_channel_mode(struct hda_codec *codec)
1891{
1892 struct hda_gen_spec *spec = codec->spec;
1893
1894 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01001895 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01001896 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 return 0;
1899}
1900
Takashi Iwai352f7f92012-12-19 12:52:06 +01001901/*
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001902 * aamix loopback enable/disable switch
1903 */
1904
1905#define loopback_mixing_info indep_hp_info
1906
1907static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
1908 struct snd_ctl_elem_value *ucontrol)
1909{
1910 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1911 struct hda_gen_spec *spec = codec->spec;
1912 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
1913 return 0;
1914}
1915
1916static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
1917 int nomix_path_idx, int mix_path_idx)
1918{
1919 struct nid_path *nomix_path, *mix_path;
1920
1921 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
1922 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
1923 if (!nomix_path || !mix_path)
1924 return;
1925 if (do_mix) {
1926 snd_hda_activate_path(codec, nomix_path, false, true);
1927 snd_hda_activate_path(codec, mix_path, true, true);
1928 } else {
1929 snd_hda_activate_path(codec, mix_path, false, true);
1930 snd_hda_activate_path(codec, nomix_path, true, true);
1931 }
1932}
1933
1934static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
1935 struct snd_ctl_elem_value *ucontrol)
1936{
1937 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1938 struct hda_gen_spec *spec = codec->spec;
1939 unsigned int val = ucontrol->value.enumerated.item[0];
1940
1941 if (val == spec->aamix_mode)
1942 return 0;
1943 spec->aamix_mode = val;
1944 update_aamix_paths(codec, val, spec->out_paths[0],
1945 spec->aamix_out_paths[0]);
1946 update_aamix_paths(codec, val, spec->hp_paths[0],
1947 spec->aamix_out_paths[1]);
1948 update_aamix_paths(codec, val, spec->speaker_paths[0],
1949 spec->aamix_out_paths[2]);
1950 return 1;
1951}
1952
1953static const struct snd_kcontrol_new loopback_mixing_enum = {
1954 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1955 .name = "Loopback Mixing",
1956 .info = loopback_mixing_info,
1957 .get = loopback_mixing_get,
1958 .put = loopback_mixing_put,
1959};
1960
1961static int create_loopback_mixing_ctl(struct hda_codec *codec)
1962{
1963 struct hda_gen_spec *spec = codec->spec;
1964
1965 if (!spec->mixer_nid)
1966 return 0;
1967 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
1968 spec->aamix_out_paths[2]))
1969 return 0;
1970 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
1971 return -ENOMEM;
1972 return 0;
1973}
1974
1975/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01001976 * shared headphone/mic handling
1977 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02001978
Takashi Iwai352f7f92012-12-19 12:52:06 +01001979static void call_update_outputs(struct hda_codec *codec);
1980
1981/* for shared I/O, change the pin-control accordingly */
1982static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic)
1983{
1984 struct hda_gen_spec *spec = codec->spec;
1985 unsigned int val;
1986 hda_nid_t pin = spec->autocfg.inputs[1].pin;
1987 /* NOTE: this assumes that there are only two inputs, the
1988 * first is the real internal mic and the second is HP/mic jack.
1989 */
1990
1991 val = snd_hda_get_default_vref(codec, pin);
1992
1993 /* This pin does not have vref caps - let's enable vref on pin 0x18
1994 instead, as suggested by Realtek */
1995 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
1996 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
1997 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
1998 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01001999 snd_hda_set_pin_ctl_cache(codec, vref_pin,
2000 PIN_IN | (set_as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02002001 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002002
2003 val = set_as_mic ? val | PIN_IN : PIN_HP;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002004 set_pin_target(codec, pin, val, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002005
2006 spec->automute_speaker = !set_as_mic;
2007 call_update_outputs(codec);
2008}
2009
2010/* create a shared input with the headphone out */
2011static int create_shared_input(struct hda_codec *codec)
2012{
2013 struct hda_gen_spec *spec = codec->spec;
2014 struct auto_pin_cfg *cfg = &spec->autocfg;
2015 unsigned int defcfg;
2016 hda_nid_t nid;
2017
2018 /* only one internal input pin? */
2019 if (cfg->num_inputs != 1)
2020 return 0;
2021 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2022 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2023 return 0;
2024
2025 if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
2026 nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
2027 else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
2028 nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
2029 else
2030 return 0; /* both not available */
2031
2032 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2033 return 0; /* no input */
2034
2035 cfg->inputs[1].pin = nid;
2036 cfg->inputs[1].type = AUTO_PIN_MIC;
2037 cfg->num_inputs = 2;
2038 spec->shared_mic_hp = 1;
2039 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
2040 return 0;
2041}
2042
Takashi Iwai978e77e2013-01-10 16:57:58 +01002043/*
2044 * output jack mode
2045 */
2046static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2047 struct snd_ctl_elem_info *uinfo)
2048{
2049 static const char * const texts[] = {
2050 "Line Out", "Headphone Out",
2051 };
2052 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, texts);
2053}
2054
2055static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2056 struct snd_ctl_elem_value *ucontrol)
2057{
2058 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2059 hda_nid_t nid = kcontrol->private_value;
2060 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2061 ucontrol->value.enumerated.item[0] = 1;
2062 else
2063 ucontrol->value.enumerated.item[0] = 0;
2064 return 0;
2065}
2066
2067static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2068 struct snd_ctl_elem_value *ucontrol)
2069{
2070 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2071 hda_nid_t nid = kcontrol->private_value;
2072 unsigned int val;
2073
2074 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2075 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2076 return 0;
2077 snd_hda_set_pin_ctl_cache(codec, nid, val);
2078 return 1;
2079}
2080
2081static const struct snd_kcontrol_new out_jack_mode_enum = {
2082 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2083 .info = out_jack_mode_info,
2084 .get = out_jack_mode_get,
2085 .put = out_jack_mode_put,
2086};
2087
2088static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2089{
2090 struct hda_gen_spec *spec = codec->spec;
2091 int i;
2092
2093 for (i = 0; i < spec->kctls.used; i++) {
2094 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2095 if (!strcmp(kctl->name, name) && kctl->index == idx)
2096 return true;
2097 }
2098 return false;
2099}
2100
2101static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2102 char *name, size_t name_len)
2103{
2104 struct hda_gen_spec *spec = codec->spec;
2105 int idx = 0;
2106
2107 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2108 strlcat(name, " Jack Mode", name_len);
2109
2110 for (; find_kctl_name(codec, name, idx); idx++)
2111 ;
2112}
2113
2114static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2115 hda_nid_t *pins)
2116{
2117 struct hda_gen_spec *spec = codec->spec;
2118 int i;
2119
2120 for (i = 0; i < num_pins; i++) {
2121 hda_nid_t pin = pins[i];
2122 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2123 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV)) {
2124 struct snd_kcontrol_new *knew;
2125 char name[44];
2126 get_jack_mode_name(codec, pin, name, sizeof(name));
2127 knew = snd_hda_gen_add_kctl(spec, name,
2128 &out_jack_mode_enum);
2129 if (!knew)
2130 return -ENOMEM;
2131 knew->private_value = pin;
2132 }
2133 }
2134
2135 return 0;
2136}
2137
Takashi Iwai352f7f92012-12-19 12:52:06 +01002138
2139/*
2140 * Parse input paths
2141 */
2142
2143#ifdef CONFIG_PM
2144/* add the powersave loopback-list entry */
2145static void add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
2146{
2147 struct hda_amp_list *list;
2148
2149 if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
2150 return;
2151 list = spec->loopback_list + spec->num_loopbacks;
2152 list->nid = mix;
2153 list->dir = HDA_INPUT;
2154 list->idx = idx;
2155 spec->num_loopbacks++;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002156 spec->loopback.amplist = spec->loopback_list;
2157}
2158#else
Takashi Iwai352f7f92012-12-19 12:52:06 +01002159#define add_loopback_list(spec, mix, idx) /* NOP */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002160#endif
2161
Takashi Iwai352f7f92012-12-19 12:52:06 +01002162/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01002163static int new_analog_input(struct hda_codec *codec, int input_idx,
2164 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002165 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002167 struct hda_gen_spec *spec = codec->spec;
2168 struct nid_path *path;
2169 unsigned int val;
2170 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
Takashi Iwai352f7f92012-12-19 12:52:06 +01002172 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
2173 !nid_has_mute(codec, mix_nid, HDA_INPUT))
2174 return 0; /* no need for analog loopback */
2175
Takashi Iwai3ca529d2013-01-07 17:25:08 +01002176 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002177 if (!path)
2178 return -EINVAL;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002179 print_nid_path("loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01002180 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002181
2182 idx = path->idx[path->depth - 1];
2183 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
2184 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2185 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002186 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002188 path->ctls[NID_PATH_VOL_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 }
2190
Takashi Iwai352f7f92012-12-19 12:52:06 +01002191 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
2192 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2193 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002194 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002196 path->ctls[NID_PATH_MUTE_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 }
2198
Takashi Iwai352f7f92012-12-19 12:52:06 +01002199 path->active = true;
2200 add_loopback_list(spec, mix_nid, idx);
2201 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202}
2203
Takashi Iwai352f7f92012-12-19 12:52:06 +01002204static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002206 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
2207 return (pincap & AC_PINCAP_IN) != 0;
2208}
2209
2210/* Parse the codec tree and retrieve ADCs */
2211static int fill_adc_nids(struct hda_codec *codec)
2212{
2213 struct hda_gen_spec *spec = codec->spec;
2214 hda_nid_t nid;
2215 hda_nid_t *adc_nids = spec->adc_nids;
2216 int max_nums = ARRAY_SIZE(spec->adc_nids);
2217 int i, nums = 0;
2218
2219 nid = codec->start_nid;
2220 for (i = 0; i < codec->num_nodes; i++, nid++) {
2221 unsigned int caps = get_wcaps(codec, nid);
2222 int type = get_wcaps_type(caps);
2223
2224 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2225 continue;
2226 adc_nids[nums] = nid;
2227 if (++nums >= max_nums)
2228 break;
2229 }
2230 spec->num_adc_nids = nums;
2231 return nums;
2232}
2233
2234/* filter out invalid adc_nids that don't give all active input pins;
2235 * if needed, check whether dynamic ADC-switching is available
2236 */
2237static int check_dyn_adc_switch(struct hda_codec *codec)
2238{
2239 struct hda_gen_spec *spec = codec->spec;
2240 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002241 unsigned int ok_bits;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002242 int i, n, nums;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002243
2244 again:
2245 nums = 0;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002246 ok_bits = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002247 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002248 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002249 if (!spec->input_paths[i][n])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002250 break;
2251 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002252 if (i >= imux->num_items) {
2253 ok_bits |= (1 << n);
2254 nums++;
2255 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002256 }
2257
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002258 if (!ok_bits) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002259 if (spec->shared_mic_hp) {
2260 spec->shared_mic_hp = 0;
2261 imux->num_items = 1;
2262 goto again;
2263 }
2264
2265 /* check whether ADC-switch is possible */
2266 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002267 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002268 if (spec->input_paths[i][n]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002269 spec->dyn_adc_idx[i] = n;
2270 break;
2271 }
2272 }
2273 }
2274
2275 snd_printdd("hda-codec: enabling ADC switching\n");
2276 spec->dyn_adc_switch = 1;
2277 } else if (nums != spec->num_adc_nids) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002278 /* shrink the invalid adcs and input paths */
2279 nums = 0;
2280 for (n = 0; n < spec->num_adc_nids; n++) {
2281 if (!(ok_bits & (1 << n)))
2282 continue;
2283 if (n != nums) {
2284 spec->adc_nids[nums] = spec->adc_nids[n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002285 for (i = 0; i < imux->num_items; i++) {
2286 invalidate_nid_path(codec,
2287 spec->input_paths[i][nums]);
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002288 spec->input_paths[i][nums] =
2289 spec->input_paths[i][n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002290 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002291 }
2292 nums++;
2293 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002294 spec->num_adc_nids = nums;
2295 }
2296
2297 if (imux->num_items == 1 || spec->shared_mic_hp) {
2298 snd_printdd("hda-codec: reducing to a single ADC\n");
2299 spec->num_adc_nids = 1; /* reduce to a single ADC */
2300 }
2301
2302 /* single index for individual volumes ctls */
2303 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
2304 spec->num_adc_nids = 1;
2305
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 return 0;
2307}
2308
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002309/* parse capture source paths from the given pin and create imux items */
2310static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
2311 int num_adcs, const char *label, int anchor)
2312{
2313 struct hda_gen_spec *spec = codec->spec;
2314 struct hda_input_mux *imux = &spec->input_mux;
2315 int imux_idx = imux->num_items;
2316 bool imux_added = false;
2317 int c;
2318
2319 for (c = 0; c < num_adcs; c++) {
2320 struct nid_path *path;
2321 hda_nid_t adc = spec->adc_nids[c];
2322
2323 if (!is_reachable_path(codec, pin, adc))
2324 continue;
2325 path = snd_hda_add_new_path(codec, pin, adc, anchor);
2326 if (!path)
2327 continue;
2328 print_nid_path("input", path);
2329 spec->input_paths[imux_idx][c] =
2330 snd_hda_get_path_idx(codec, path);
2331
2332 if (!imux_added) {
2333 spec->imux_pins[imux->num_items] = pin;
2334 snd_hda_add_imux_item(imux, label,
2335 imux->num_items, NULL);
2336 imux_added = true;
2337 }
2338 }
2339
2340 return 0;
2341}
2342
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002344 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002346static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002347{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002348 struct hda_gen_spec *spec = codec->spec;
2349 const struct auto_pin_cfg *cfg = &spec->autocfg;
2350 hda_nid_t mixer = spec->mixer_nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002351 int num_adcs;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002352 int i, err, type_idx = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002353 const char *prev_label = NULL;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002354 unsigned int val;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002355
Takashi Iwai352f7f92012-12-19 12:52:06 +01002356 num_adcs = fill_adc_nids(codec);
2357 if (num_adcs < 0)
2358 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002359
Takashi Iwai352f7f92012-12-19 12:52:06 +01002360 for (i = 0; i < cfg->num_inputs; i++) {
2361 hda_nid_t pin;
2362 const char *label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
Takashi Iwai352f7f92012-12-19 12:52:06 +01002364 pin = cfg->inputs[i].pin;
2365 if (!is_input_pin(codec, pin))
2366 continue;
2367
2368 label = hda_get_autocfg_input_label(codec, cfg, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002369 if (prev_label && !strcmp(label, prev_label))
2370 type_idx++;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002371 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01002372 type_idx = 0;
2373 prev_label = label;
2374
Takashi Iwai2c12c302013-01-10 09:33:29 +01002375 val = PIN_IN;
2376 if (cfg->inputs[i].type == AUTO_PIN_MIC)
2377 val |= snd_hda_get_default_vref(codec, pin);
2378 set_pin_target(codec, pin, val, false);
2379
Takashi Iwai352f7f92012-12-19 12:52:06 +01002380 if (mixer) {
2381 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01002382 err = new_analog_input(codec, i, pin,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002383 label, type_idx, mixer);
2384 if (err < 0)
2385 return err;
2386 }
2387 }
2388
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002389 err = parse_capture_source(codec, pin, num_adcs, label, -mixer);
2390 if (err < 0)
2391 return err;
2392 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002393
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002394 if (mixer && spec->add_stereo_mix_input) {
2395 err = parse_capture_source(codec, mixer, num_adcs,
2396 "Stereo Mix", 0);
2397 if (err < 0)
2398 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002399 }
2400
2401 return 0;
2402}
2403
2404
2405/*
2406 * input source mux
2407 */
2408
Takashi Iwaic697b712013-01-07 17:09:26 +01002409/* get the input path specified by the given adc and imux indices */
2410static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002411{
2412 struct hda_gen_spec *spec = codec->spec;
David Henningssonb56fa1e2013-01-16 11:45:35 +01002413 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
2414 snd_BUG();
2415 return NULL;
2416 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002417 if (spec->dyn_adc_switch)
2418 adc_idx = spec->dyn_adc_idx[imux_idx];
David Henningssonb56fa1e2013-01-16 11:45:35 +01002419 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_OUTS) {
2420 snd_BUG();
2421 return NULL;
2422 }
Takashi Iwaic697b712013-01-07 17:09:26 +01002423 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002424}
2425
2426static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2427 unsigned int idx);
2428
2429static int mux_enum_info(struct snd_kcontrol *kcontrol,
2430 struct snd_ctl_elem_info *uinfo)
2431{
2432 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2433 struct hda_gen_spec *spec = codec->spec;
2434 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
2435}
2436
2437static int mux_enum_get(struct snd_kcontrol *kcontrol,
2438 struct snd_ctl_elem_value *ucontrol)
2439{
2440 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2441 struct hda_gen_spec *spec = codec->spec;
David Henningssona053d1e2013-01-16 11:45:36 +01002442 unsigned int adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002443
2444 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
2445 return 0;
2446}
2447
2448static int mux_enum_put(struct snd_kcontrol *kcontrol,
2449 struct snd_ctl_elem_value *ucontrol)
2450{
2451 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
David Henningssona053d1e2013-01-16 11:45:36 +01002452 unsigned int adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002453 return mux_select(codec, adc_idx,
2454 ucontrol->value.enumerated.item[0]);
2455}
2456
Takashi Iwai352f7f92012-12-19 12:52:06 +01002457static const struct snd_kcontrol_new cap_src_temp = {
2458 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2459 .name = "Input Source",
2460 .info = mux_enum_info,
2461 .get = mux_enum_get,
2462 .put = mux_enum_put,
2463};
2464
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002465/*
2466 * capture volume and capture switch ctls
2467 */
2468
Takashi Iwai352f7f92012-12-19 12:52:06 +01002469typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
2470 struct snd_ctl_elem_value *ucontrol);
2471
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002472/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002473static int cap_put_caller(struct snd_kcontrol *kcontrol,
2474 struct snd_ctl_elem_value *ucontrol,
2475 put_call_t func, int type)
2476{
2477 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2478 struct hda_gen_spec *spec = codec->spec;
2479 const struct hda_input_mux *imux;
2480 struct nid_path *path;
2481 int i, adc_idx, err = 0;
2482
2483 imux = &spec->input_mux;
David Henningssona053d1e2013-01-16 11:45:36 +01002484 adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002485 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002486 /* we use the cache-only update at first since multiple input paths
2487 * may shared the same amp; by updating only caches, the redundant
2488 * writes to hardware can be reduced.
2489 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002490 codec->cached_write = 1;
2491 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01002492 path = get_input_path(codec, adc_idx, i);
2493 if (!path || !path->ctls[type])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002494 continue;
2495 kcontrol->private_value = path->ctls[type];
2496 err = func(kcontrol, ucontrol);
2497 if (err < 0)
2498 goto error;
2499 }
2500 error:
2501 codec->cached_write = 0;
2502 mutex_unlock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002503 snd_hda_codec_flush_amp_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002504 if (err >= 0 && spec->cap_sync_hook)
2505 spec->cap_sync_hook(codec);
2506 return err;
2507}
2508
2509/* capture volume ctl callbacks */
2510#define cap_vol_info snd_hda_mixer_amp_volume_info
2511#define cap_vol_get snd_hda_mixer_amp_volume_get
2512#define cap_vol_tlv snd_hda_mixer_amp_tlv
2513
2514static int cap_vol_put(struct snd_kcontrol *kcontrol,
2515 struct snd_ctl_elem_value *ucontrol)
2516{
2517 return cap_put_caller(kcontrol, ucontrol,
2518 snd_hda_mixer_amp_volume_put,
2519 NID_PATH_VOL_CTL);
2520}
2521
2522static const struct snd_kcontrol_new cap_vol_temp = {
2523 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2524 .name = "Capture Volume",
2525 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
2526 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2527 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
2528 .info = cap_vol_info,
2529 .get = cap_vol_get,
2530 .put = cap_vol_put,
2531 .tlv = { .c = cap_vol_tlv },
2532};
2533
2534/* capture switch ctl callbacks */
2535#define cap_sw_info snd_ctl_boolean_stereo_info
2536#define cap_sw_get snd_hda_mixer_amp_switch_get
2537
2538static int cap_sw_put(struct snd_kcontrol *kcontrol,
2539 struct snd_ctl_elem_value *ucontrol)
2540{
Takashi Iwaiae177c32013-01-14 12:13:06 +01002541 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2542 struct hda_gen_spec *spec = codec->spec;
2543 int ret;
2544
2545 ret = cap_put_caller(kcontrol, ucontrol,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002546 snd_hda_mixer_amp_switch_put,
2547 NID_PATH_MUTE_CTL);
Takashi Iwaiae177c32013-01-14 12:13:06 +01002548 if (ret < 0)
2549 return ret;
2550
2551 if (spec->capture_switch_hook) {
2552 bool enable = (ucontrol->value.integer.value[0] ||
2553 ucontrol->value.integer.value[1]);
2554 spec->capture_switch_hook(codec, enable);
2555 }
2556
2557 return ret;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002558}
2559
2560static const struct snd_kcontrol_new cap_sw_temp = {
2561 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2562 .name = "Capture Switch",
2563 .info = cap_sw_info,
2564 .get = cap_sw_get,
2565 .put = cap_sw_put,
2566};
2567
2568static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
2569{
2570 hda_nid_t nid;
2571 int i, depth;
2572
2573 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
2574 for (depth = 0; depth < 3; depth++) {
2575 if (depth >= path->depth)
2576 return -EINVAL;
2577 i = path->depth - depth - 1;
2578 nid = path->path[i];
2579 if (!path->ctls[NID_PATH_VOL_CTL]) {
2580 if (nid_has_volume(codec, nid, HDA_OUTPUT))
2581 path->ctls[NID_PATH_VOL_CTL] =
2582 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2583 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
2584 int idx = path->idx[i];
2585 if (!depth && codec->single_adc_amp)
2586 idx = 0;
2587 path->ctls[NID_PATH_VOL_CTL] =
2588 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2589 }
2590 }
2591 if (!path->ctls[NID_PATH_MUTE_CTL]) {
2592 if (nid_has_mute(codec, nid, HDA_OUTPUT))
2593 path->ctls[NID_PATH_MUTE_CTL] =
2594 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2595 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
2596 int idx = path->idx[i];
2597 if (!depth && codec->single_adc_amp)
2598 idx = 0;
2599 path->ctls[NID_PATH_MUTE_CTL] =
2600 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2601 }
2602 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01002603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 return 0;
2605}
2606
Takashi Iwai352f7f92012-12-19 12:52:06 +01002607static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002609 struct hda_gen_spec *spec = codec->spec;
2610 struct auto_pin_cfg *cfg = &spec->autocfg;
2611 unsigned int val;
2612 int i;
2613
2614 if (!spec->inv_dmic_split)
2615 return false;
2616 for (i = 0; i < cfg->num_inputs; i++) {
2617 if (cfg->inputs[i].pin != nid)
2618 continue;
2619 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2620 return false;
2621 val = snd_hda_codec_get_pincfg(codec, nid);
2622 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
2623 }
2624 return false;
2625}
2626
2627static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
2628 int idx, bool is_switch, unsigned int ctl,
2629 bool inv_dmic)
2630{
2631 struct hda_gen_spec *spec = codec->spec;
2632 char tmpname[44];
2633 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
2634 const char *sfx = is_switch ? "Switch" : "Volume";
2635 unsigned int chs = inv_dmic ? 1 : 3;
2636 int err;
2637
2638 if (!ctl)
2639 return 0;
2640
2641 if (label)
2642 snprintf(tmpname, sizeof(tmpname),
2643 "%s Capture %s", label, sfx);
2644 else
2645 snprintf(tmpname, sizeof(tmpname),
2646 "Capture %s", sfx);
2647 err = add_control(spec, type, tmpname, idx,
2648 amp_val_replace_channels(ctl, chs));
2649 if (err < 0 || !inv_dmic)
2650 return err;
2651
2652 /* Make independent right kcontrol */
2653 if (label)
2654 snprintf(tmpname, sizeof(tmpname),
2655 "Inverted %s Capture %s", label, sfx);
2656 else
2657 snprintf(tmpname, sizeof(tmpname),
2658 "Inverted Capture %s", sfx);
2659 return add_control(spec, type, tmpname, idx,
2660 amp_val_replace_channels(ctl, 2));
2661}
2662
2663/* create single (and simple) capture volume and switch controls */
2664static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
2665 unsigned int vol_ctl, unsigned int sw_ctl,
2666 bool inv_dmic)
2667{
2668 int err;
2669 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
2670 if (err < 0)
2671 return err;
2672 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
2673 if (err < 0)
2674 return err;
2675 return 0;
2676}
2677
2678/* create bound capture volume and switch controls */
2679static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
2680 unsigned int vol_ctl, unsigned int sw_ctl)
2681{
2682 struct hda_gen_spec *spec = codec->spec;
2683 struct snd_kcontrol_new *knew;
2684
2685 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002686 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002687 if (!knew)
2688 return -ENOMEM;
2689 knew->index = idx;
2690 knew->private_value = vol_ctl;
2691 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2692 }
2693 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002694 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002695 if (!knew)
2696 return -ENOMEM;
2697 knew->index = idx;
2698 knew->private_value = sw_ctl;
2699 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2700 }
2701 return 0;
2702}
2703
2704/* return the vol ctl when used first in the imux list */
2705static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
2706{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002707 struct nid_path *path;
2708 unsigned int ctl;
2709 int i;
2710
Takashi Iwaic697b712013-01-07 17:09:26 +01002711 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002712 if (!path)
2713 return 0;
2714 ctl = path->ctls[type];
2715 if (!ctl)
2716 return 0;
2717 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01002718 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002719 if (path && path->ctls[type] == ctl)
2720 return 0;
2721 }
2722 return ctl;
2723}
2724
2725/* create individual capture volume and switch controls per input */
2726static int create_multi_cap_vol_ctl(struct hda_codec *codec)
2727{
2728 struct hda_gen_spec *spec = codec->spec;
2729 struct hda_input_mux *imux = &spec->input_mux;
2730 int i, err, type, type_idx = 0;
2731 const char *prev_label = NULL;
2732
2733 for (i = 0; i < imux->num_items; i++) {
2734 const char *label;
2735 bool inv_dmic;
2736 label = hda_get_autocfg_input_label(codec, &spec->autocfg, i);
2737 if (prev_label && !strcmp(label, prev_label))
2738 type_idx++;
2739 else
2740 type_idx = 0;
2741 prev_label = label;
2742 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
2743
2744 for (type = 0; type < 2; type++) {
2745 err = add_single_cap_ctl(codec, label, type_idx, type,
2746 get_first_cap_ctl(codec, i, type),
2747 inv_dmic);
2748 if (err < 0)
2749 return err;
2750 }
2751 }
2752 return 0;
2753}
2754
2755static int create_capture_mixers(struct hda_codec *codec)
2756{
2757 struct hda_gen_spec *spec = codec->spec;
2758 struct hda_input_mux *imux = &spec->input_mux;
2759 int i, n, nums, err;
2760
2761 if (spec->dyn_adc_switch)
2762 nums = 1;
2763 else
2764 nums = spec->num_adc_nids;
2765
2766 if (!spec->auto_mic && imux->num_items > 1) {
2767 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01002768 const char *name;
2769 name = nums > 1 ? "Input Source" : "Capture Source";
2770 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002771 if (!knew)
2772 return -ENOMEM;
2773 knew->count = nums;
2774 }
2775
2776 for (n = 0; n < nums; n++) {
2777 bool multi = false;
David Henningsson99a55922013-01-16 15:58:44 +01002778 bool multi_cap_vol = spec->multi_cap_vol;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002779 bool inv_dmic = false;
2780 int vol, sw;
2781
2782 vol = sw = 0;
2783 for (i = 0; i < imux->num_items; i++) {
2784 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01002785 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002786 if (!path)
2787 continue;
2788 parse_capvol_in_path(codec, path);
2789 if (!vol)
2790 vol = path->ctls[NID_PATH_VOL_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01002791 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002792 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01002793 if (!same_amp_caps(codec, vol,
2794 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
2795 multi_cap_vol = true;
2796 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002797 if (!sw)
2798 sw = path->ctls[NID_PATH_MUTE_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01002799 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002800 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01002801 if (!same_amp_caps(codec, sw,
2802 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
2803 multi_cap_vol = true;
2804 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002805 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
2806 inv_dmic = true;
2807 }
2808
2809 if (!multi)
2810 err = create_single_cap_vol_ctl(codec, n, vol, sw,
2811 inv_dmic);
David Henningsson99a55922013-01-16 15:58:44 +01002812 else if (!multi_cap_vol)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002813 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
2814 else
2815 err = create_multi_cap_vol_ctl(codec);
2816 if (err < 0)
2817 return err;
2818 }
2819
2820 return 0;
2821}
2822
2823/*
2824 * add mic boosts if needed
2825 */
2826static int parse_mic_boost(struct hda_codec *codec)
2827{
2828 struct hda_gen_spec *spec = codec->spec;
2829 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002830 int i, err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002831 int type_idx = 0;
2832 hda_nid_t nid;
2833 const char *prev_label = NULL;
2834
2835 for (i = 0; i < cfg->num_inputs; i++) {
2836 if (cfg->inputs[i].type > AUTO_PIN_MIC)
2837 break;
2838 nid = cfg->inputs[i].pin;
2839 if (get_wcaps(codec, nid) & AC_WCAP_IN_AMP) {
2840 const char *label;
Takashi Iwai5abd4882013-01-07 09:43:18 +01002841 char boost_label[44];
Takashi Iwai352f7f92012-12-19 12:52:06 +01002842 struct nid_path *path;
2843 unsigned int val;
2844
David Henningsson02aba552013-01-16 15:58:43 +01002845 if (!nid_has_volume(codec, nid, HDA_INPUT))
2846 continue;
2847
Takashi Iwai352f7f92012-12-19 12:52:06 +01002848 label = hda_get_autocfg_input_label(codec, cfg, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002849 if (prev_label && !strcmp(label, prev_label))
2850 type_idx++;
2851 else
2852 type_idx = 0;
2853 prev_label = label;
2854
2855 snprintf(boost_label, sizeof(boost_label),
2856 "%s Boost Volume", label);
2857 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
2858 err = add_control(spec, HDA_CTL_WIDGET_VOL,
2859 boost_label, type_idx, val);
2860 if (err < 0)
2861 return err;
2862
2863 path = snd_hda_get_nid_path(codec, nid, 0);
2864 if (path)
2865 path->ctls[NID_PATH_BOOST_CTL] = val;
2866 }
2867 }
2868 return 0;
2869}
2870
2871/*
2872 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
2873 */
2874static void parse_digital(struct hda_codec *codec)
2875{
2876 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002877 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002878 int i, nums;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002879 hda_nid_t dig_nid, pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002880
2881 /* support multiple SPDIFs; the secondary is set up as a slave */
2882 nums = 0;
2883 for (i = 0; i < spec->autocfg.dig_outs; i++) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01002884 pin = spec->autocfg.dig_out_pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01002885 dig_nid = look_for_dac(codec, pin, true);
2886 if (!dig_nid)
2887 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01002888 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002889 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002890 continue;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002891 print_nid_path("digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01002892 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01002893 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01002894 set_pin_target(codec, pin, PIN_OUT, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002895 if (!nums) {
2896 spec->multiout.dig_out_nid = dig_nid;
2897 spec->dig_out_type = spec->autocfg.dig_out_type[0];
2898 } else {
2899 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
2900 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
2901 break;
2902 spec->slave_dig_outs[nums - 1] = dig_nid;
2903 }
2904 nums++;
2905 }
2906
2907 if (spec->autocfg.dig_in_pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01002908 pin = spec->autocfg.dig_in_pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002909 dig_nid = codec->start_nid;
2910 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002911 unsigned int wcaps = get_wcaps(codec, dig_nid);
2912 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
2913 continue;
2914 if (!(wcaps & AC_WCAP_DIGITAL))
2915 continue;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002916 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002917 if (path) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002918 print_nid_path("digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002919 path->active = true;
2920 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01002921 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01002922 set_pin_target(codec, pin, PIN_IN, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002923 break;
2924 }
2925 }
2926 }
2927}
2928
2929
2930/*
2931 * input MUX handling
2932 */
2933
2934static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
2935
2936/* select the given imux item; either unmute exclusively or select the route */
2937static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2938 unsigned int idx)
2939{
2940 struct hda_gen_spec *spec = codec->spec;
2941 const struct hda_input_mux *imux;
2942 struct nid_path *path;
2943
2944 imux = &spec->input_mux;
2945 if (!imux->num_items)
2946 return 0;
2947
2948 if (idx >= imux->num_items)
2949 idx = imux->num_items - 1;
2950 if (spec->cur_mux[adc_idx] == idx)
2951 return 0;
2952
Takashi Iwaic697b712013-01-07 17:09:26 +01002953 path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002954 if (!path)
2955 return 0;
2956 if (path->active)
2957 snd_hda_activate_path(codec, path, false, false);
2958
2959 spec->cur_mux[adc_idx] = idx;
2960
2961 if (spec->shared_mic_hp)
2962 update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
2963
2964 if (spec->dyn_adc_switch)
2965 dyn_adc_pcm_resetup(codec, idx);
2966
Takashi Iwaic697b712013-01-07 17:09:26 +01002967 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002968 if (!path)
2969 return 0;
2970 if (path->active)
2971 return 0;
2972 snd_hda_activate_path(codec, path, true, false);
2973 if (spec->cap_sync_hook)
2974 spec->cap_sync_hook(codec);
2975 return 1;
2976}
2977
2978
2979/*
2980 * Jack detections for HP auto-mute and mic-switch
2981 */
2982
2983/* check each pin in the given array; returns true if any of them is plugged */
2984static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
2985{
2986 int i, present = 0;
2987
2988 for (i = 0; i < num_pins; i++) {
2989 hda_nid_t nid = pins[i];
2990 if (!nid)
2991 break;
Takashi Iwai0b4df932013-01-10 09:45:13 +01002992 /* don't detect pins retasked as inputs */
2993 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
2994 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002995 present |= snd_hda_jack_detect(codec, nid);
2996 }
2997 return present;
2998}
2999
3000/* standard HP/line-out auto-mute helper */
3001static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
Takashi Iwai2c12c302013-01-10 09:33:29 +01003002 bool mute)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003003{
3004 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003005 int i;
3006
3007 for (i = 0; i < num_pins; i++) {
3008 hda_nid_t nid = pins[i];
3009 unsigned int val;
3010 if (!nid)
3011 break;
3012 /* don't reset VREF value in case it's controlling
3013 * the amp (see alc861_fixup_asus_amp_vref_0f())
3014 */
Takashi Iwai2c12c302013-01-10 09:33:29 +01003015 if (spec->keep_vref_in_automute)
3016 val = snd_hda_codec_get_pin_target(codec, nid) & ~PIN_HP;
3017 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01003018 val = 0;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003019 if (!mute)
3020 val |= snd_hda_codec_get_pin_target(codec, nid);
3021 /* here we call update_pin_ctl() so that the pinctl is changed
3022 * without changing the pinctl target value;
3023 * the original target value will be still referred at the
3024 * init / resume again
3025 */
3026 update_pin_ctl(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01003027 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003028 }
3029}
3030
3031/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003032void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003033{
3034 struct hda_gen_spec *spec = codec->spec;
3035 int on;
3036
3037 /* Control HP pins/amps depending on master_mute state;
3038 * in general, HP pins/amps control should be enabled in all cases,
3039 * but currently set only for master_mute, just to be safe
3040 */
3041 if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
3042 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003043 spec->autocfg.hp_pins, spec->master_mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003044
3045 if (!spec->automute_speaker)
3046 on = 0;
3047 else
3048 on = spec->hp_jack_present | spec->line_jack_present;
3049 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003050 spec->speaker_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003051 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003052 spec->autocfg.speaker_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003053
3054 /* toggle line-out mutes if needed, too */
3055 /* if LO is a copy of either HP or Speaker, don't need to handle it */
3056 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
3057 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
3058 return;
3059 if (!spec->automute_lo)
3060 on = 0;
3061 else
3062 on = spec->hp_jack_present;
3063 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003064 spec->line_out_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003065 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003066 spec->autocfg.line_out_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003067}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003068EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003069
3070static void call_update_outputs(struct hda_codec *codec)
3071{
3072 struct hda_gen_spec *spec = codec->spec;
3073 if (spec->automute_hook)
3074 spec->automute_hook(codec);
3075 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01003076 snd_hda_gen_update_outputs(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003077}
3078
3079/* standard HP-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003080void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003081{
3082 struct hda_gen_spec *spec = codec->spec;
3083
3084 spec->hp_jack_present =
3085 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
3086 spec->autocfg.hp_pins);
3087 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
3088 return;
3089 call_update_outputs(codec);
3090}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003091EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003092
3093/* standard line-out-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003094void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003095{
3096 struct hda_gen_spec *spec = codec->spec;
3097
3098 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3099 return;
3100 /* check LO jack only when it's different from HP */
3101 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
3102 return;
3103
3104 spec->line_jack_present =
3105 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3106 spec->autocfg.line_out_pins);
3107 if (!spec->automute_speaker || !spec->detect_lo)
3108 return;
3109 call_update_outputs(codec);
3110}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003111EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003112
3113/* standard mic auto-switch helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003114void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003115{
3116 struct hda_gen_spec *spec = codec->spec;
3117 int i;
3118
3119 if (!spec->auto_mic)
3120 return;
3121
3122 for (i = spec->am_num_entries - 1; i > 0; i--) {
Takashi Iwai0b4df932013-01-10 09:45:13 +01003123 hda_nid_t pin = spec->am_entry[i].pin;
3124 /* don't detect pins retasked as outputs */
3125 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
3126 continue;
3127 if (snd_hda_jack_detect(codec, pin)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003128 mux_select(codec, 0, spec->am_entry[i].idx);
3129 return;
3130 }
3131 }
3132 mux_select(codec, 0, spec->am_entry[0].idx);
3133}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003134EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003135
Takashi Iwaia5cc2502013-01-16 18:08:55 +01003136/* update jack retasking */
3137static void update_automute_all(struct hda_codec *codec)
3138{
3139 struct hda_gen_spec *spec = codec->spec;
3140
3141 if (spec->hp_automute_hook)
3142 spec->hp_automute_hook(codec, NULL);
3143 else
3144 snd_hda_gen_hp_automute(codec, NULL);
3145 if (spec->line_automute_hook)
3146 spec->line_automute_hook(codec, NULL);
3147 else
3148 snd_hda_gen_line_automute(codec, NULL);
3149 if (spec->mic_autoswitch_hook)
3150 spec->mic_autoswitch_hook(codec, NULL);
3151 else
3152 snd_hda_gen_mic_autoswitch(codec, NULL);
3153}
3154
Takashi Iwai352f7f92012-12-19 12:52:06 +01003155/*
3156 * Auto-Mute mode mixer enum support
3157 */
3158static int automute_mode_info(struct snd_kcontrol *kcontrol,
3159 struct snd_ctl_elem_info *uinfo)
3160{
3161 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3162 struct hda_gen_spec *spec = codec->spec;
3163 static const char * const texts3[] = {
3164 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02003165 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166
Takashi Iwai352f7f92012-12-19 12:52:06 +01003167 if (spec->automute_speaker_possible && spec->automute_lo_possible)
3168 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
3169 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
3170}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171
Takashi Iwai352f7f92012-12-19 12:52:06 +01003172static int automute_mode_get(struct snd_kcontrol *kcontrol,
3173 struct snd_ctl_elem_value *ucontrol)
3174{
3175 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3176 struct hda_gen_spec *spec = codec->spec;
3177 unsigned int val = 0;
3178 if (spec->automute_speaker)
3179 val++;
3180 if (spec->automute_lo)
3181 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003182
Takashi Iwai352f7f92012-12-19 12:52:06 +01003183 ucontrol->value.enumerated.item[0] = val;
3184 return 0;
3185}
3186
3187static int automute_mode_put(struct snd_kcontrol *kcontrol,
3188 struct snd_ctl_elem_value *ucontrol)
3189{
3190 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3191 struct hda_gen_spec *spec = codec->spec;
3192
3193 switch (ucontrol->value.enumerated.item[0]) {
3194 case 0:
3195 if (!spec->automute_speaker && !spec->automute_lo)
3196 return 0;
3197 spec->automute_speaker = 0;
3198 spec->automute_lo = 0;
3199 break;
3200 case 1:
3201 if (spec->automute_speaker_possible) {
3202 if (!spec->automute_lo && spec->automute_speaker)
3203 return 0;
3204 spec->automute_speaker = 1;
3205 spec->automute_lo = 0;
3206 } else if (spec->automute_lo_possible) {
3207 if (spec->automute_lo)
3208 return 0;
3209 spec->automute_lo = 1;
3210 } else
3211 return -EINVAL;
3212 break;
3213 case 2:
3214 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
3215 return -EINVAL;
3216 if (spec->automute_speaker && spec->automute_lo)
3217 return 0;
3218 spec->automute_speaker = 1;
3219 spec->automute_lo = 1;
3220 break;
3221 default:
3222 return -EINVAL;
3223 }
3224 call_update_outputs(codec);
3225 return 1;
3226}
3227
3228static const struct snd_kcontrol_new automute_mode_enum = {
3229 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3230 .name = "Auto-Mute Mode",
3231 .info = automute_mode_info,
3232 .get = automute_mode_get,
3233 .put = automute_mode_put,
3234};
3235
3236static int add_automute_mode_enum(struct hda_codec *codec)
3237{
3238 struct hda_gen_spec *spec = codec->spec;
3239
Takashi Iwai12c93df2012-12-19 14:38:33 +01003240 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01003241 return -ENOMEM;
3242 return 0;
3243}
3244
3245/*
3246 * Check the availability of HP/line-out auto-mute;
3247 * Set up appropriately if really supported
3248 */
3249static int check_auto_mute_availability(struct hda_codec *codec)
3250{
3251 struct hda_gen_spec *spec = codec->spec;
3252 struct auto_pin_cfg *cfg = &spec->autocfg;
3253 int present = 0;
3254 int i, err;
3255
3256 if (cfg->hp_pins[0])
3257 present++;
3258 if (cfg->line_out_pins[0])
3259 present++;
3260 if (cfg->speaker_pins[0])
3261 present++;
3262 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02003263 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003264
3265 if (!cfg->speaker_pins[0] &&
3266 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
3267 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3268 sizeof(cfg->speaker_pins));
3269 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271
Takashi Iwai352f7f92012-12-19 12:52:06 +01003272 if (!cfg->hp_pins[0] &&
3273 cfg->line_out_type == AUTO_PIN_HP_OUT) {
3274 memcpy(cfg->hp_pins, cfg->line_out_pins,
3275 sizeof(cfg->hp_pins));
3276 cfg->hp_outs = cfg->line_outs;
3277 }
3278
3279 for (i = 0; i < cfg->hp_outs; i++) {
3280 hda_nid_t nid = cfg->hp_pins[i];
3281 if (!is_jack_detectable(codec, nid))
3282 continue;
3283 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
3284 nid);
3285 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003286 spec->hp_automute_hook ?
3287 spec->hp_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003288 snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003289 spec->detect_hp = 1;
3290 }
3291
3292 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
3293 if (cfg->speaker_outs)
3294 for (i = 0; i < cfg->line_outs; i++) {
3295 hda_nid_t nid = cfg->line_out_pins[i];
3296 if (!is_jack_detectable(codec, nid))
3297 continue;
3298 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
3299 snd_hda_jack_detect_enable_callback(codec, nid,
3300 HDA_GEN_FRONT_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003301 spec->line_automute_hook ?
3302 spec->line_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003303 snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003304 spec->detect_lo = 1;
3305 }
3306 spec->automute_lo_possible = spec->detect_hp;
3307 }
3308
3309 spec->automute_speaker_possible = cfg->speaker_outs &&
3310 (spec->detect_hp || spec->detect_lo);
3311
3312 spec->automute_lo = spec->automute_lo_possible;
3313 spec->automute_speaker = spec->automute_speaker_possible;
3314
3315 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
3316 /* create a control for automute mode */
3317 err = add_automute_mode_enum(codec);
3318 if (err < 0)
3319 return err;
3320 }
3321 return 0;
3322}
3323
Takashi Iwai352f7f92012-12-19 12:52:06 +01003324/* check whether all auto-mic pins are valid; setup indices if OK */
3325static bool auto_mic_check_imux(struct hda_codec *codec)
3326{
3327 struct hda_gen_spec *spec = codec->spec;
3328 const struct hda_input_mux *imux;
3329 int i;
3330
3331 imux = &spec->input_mux;
3332 for (i = 0; i < spec->am_num_entries; i++) {
3333 spec->am_entry[i].idx =
3334 find_idx_in_nid_list(spec->am_entry[i].pin,
3335 spec->imux_pins, imux->num_items);
3336 if (spec->am_entry[i].idx < 0)
3337 return false; /* no corresponding imux */
3338 }
3339
3340 /* we don't need the jack detection for the first pin */
3341 for (i = 1; i < spec->am_num_entries; i++)
3342 snd_hda_jack_detect_enable_callback(codec,
3343 spec->am_entry[i].pin,
3344 HDA_GEN_MIC_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003345 spec->mic_autoswitch_hook ?
3346 spec->mic_autoswitch_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003347 snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003348 return true;
3349}
3350
3351static int compare_attr(const void *ap, const void *bp)
3352{
3353 const struct automic_entry *a = ap;
3354 const struct automic_entry *b = bp;
3355 return (int)(a->attr - b->attr);
3356}
3357
3358/*
3359 * Check the availability of auto-mic switch;
3360 * Set up if really supported
3361 */
3362static int check_auto_mic_availability(struct hda_codec *codec)
3363{
3364 struct hda_gen_spec *spec = codec->spec;
3365 struct auto_pin_cfg *cfg = &spec->autocfg;
3366 unsigned int types;
3367 int i, num_pins;
3368
Takashi Iwaid12daf62013-01-07 16:32:11 +01003369 if (spec->suppress_auto_mic)
3370 return 0;
3371
Takashi Iwai352f7f92012-12-19 12:52:06 +01003372 types = 0;
3373 num_pins = 0;
3374 for (i = 0; i < cfg->num_inputs; i++) {
3375 hda_nid_t nid = cfg->inputs[i].pin;
3376 unsigned int attr;
3377 attr = snd_hda_codec_get_pincfg(codec, nid);
3378 attr = snd_hda_get_input_pin_attr(attr);
3379 if (types & (1 << attr))
3380 return 0; /* already occupied */
3381 switch (attr) {
3382 case INPUT_PIN_ATTR_INT:
3383 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3384 return 0; /* invalid type */
3385 break;
3386 case INPUT_PIN_ATTR_UNUSED:
3387 return 0; /* invalid entry */
3388 default:
3389 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
3390 return 0; /* invalid type */
3391 if (!spec->line_in_auto_switch &&
3392 cfg->inputs[i].type != AUTO_PIN_MIC)
3393 return 0; /* only mic is allowed */
3394 if (!is_jack_detectable(codec, nid))
3395 return 0; /* no unsol support */
3396 break;
3397 }
3398 if (num_pins >= MAX_AUTO_MIC_PINS)
3399 return 0;
3400 types |= (1 << attr);
3401 spec->am_entry[num_pins].pin = nid;
3402 spec->am_entry[num_pins].attr = attr;
3403 num_pins++;
3404 }
3405
3406 if (num_pins < 2)
3407 return 0;
3408
3409 spec->am_num_entries = num_pins;
3410 /* sort the am_entry in the order of attr so that the pin with a
3411 * higher attr will be selected when the jack is plugged.
3412 */
3413 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
3414 compare_attr, NULL);
3415
3416 if (!auto_mic_check_imux(codec))
3417 return 0;
3418
3419 spec->auto_mic = 1;
3420 spec->num_adc_nids = 1;
3421 spec->cur_mux[0] = spec->am_entry[0].idx;
3422 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
3423 spec->am_entry[0].pin,
3424 spec->am_entry[1].pin,
3425 spec->am_entry[2].pin);
3426
3427 return 0;
3428}
3429
3430
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003431/*
3432 * Parse the given BIOS configuration and set up the hda_gen_spec
3433 *
3434 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003435 * or a negative error code
3436 */
3437int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003438 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003439{
3440 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003441 int err;
3442
Takashi Iwai1c70a582013-01-11 17:48:22 +01003443 parse_user_hints(codec);
3444
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003445 if (cfg != &spec->autocfg) {
3446 spec->autocfg = *cfg;
3447 cfg = &spec->autocfg;
3448 }
3449
David Henningsson6fc4cb92013-01-16 15:58:45 +01003450 fill_all_dac_nids(codec);
3451
Takashi Iwai352f7f92012-12-19 12:52:06 +01003452 if (!cfg->line_outs) {
3453 if (cfg->dig_outs || cfg->dig_in_pin) {
3454 spec->multiout.max_channels = 2;
3455 spec->no_analog = 1;
3456 goto dig_only;
3457 }
3458 return 0; /* can't find valid BIOS pin config */
3459 }
3460
3461 if (!spec->no_primary_hp &&
3462 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
3463 cfg->line_outs <= cfg->hp_outs) {
3464 /* use HP as primary out */
3465 cfg->speaker_outs = cfg->line_outs;
3466 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3467 sizeof(cfg->speaker_pins));
3468 cfg->line_outs = cfg->hp_outs;
3469 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
3470 cfg->hp_outs = 0;
3471 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3472 cfg->line_out_type = AUTO_PIN_HP_OUT;
3473 }
3474
3475 err = parse_output_paths(codec);
3476 if (err < 0)
3477 return err;
3478 err = create_multi_channel_mode(codec);
3479 if (err < 0)
3480 return err;
3481 err = create_multi_out_ctls(codec, cfg);
3482 if (err < 0)
3483 return err;
3484 err = create_hp_out_ctls(codec);
3485 if (err < 0)
3486 return err;
3487 err = create_speaker_out_ctls(codec);
3488 if (err < 0)
3489 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003490 err = create_indep_hp_ctls(codec);
3491 if (err < 0)
3492 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01003493 err = create_loopback_mixing_ctl(codec);
3494 if (err < 0)
3495 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003496 err = create_shared_input(codec);
3497 if (err < 0)
3498 return err;
3499 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003500 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02003501 return err;
3502
Takashi Iwaia07a9492013-01-07 16:44:06 +01003503 spec->const_channel_count = spec->ext_channel_count;
3504 /* check the multiple speaker and headphone pins */
3505 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
3506 spec->const_channel_count = max(spec->const_channel_count,
3507 cfg->speaker_outs * 2);
3508 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
3509 spec->const_channel_count = max(spec->const_channel_count,
3510 cfg->hp_outs * 2);
3511 spec->multiout.max_channels = max(spec->ext_channel_count,
3512 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003513
3514 err = check_auto_mute_availability(codec);
3515 if (err < 0)
3516 return err;
3517
3518 err = check_dyn_adc_switch(codec);
3519 if (err < 0)
3520 return err;
3521
3522 if (!spec->shared_mic_hp) {
3523 err = check_auto_mic_availability(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003524 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 }
Takashi Iwai071c73a2006-08-23 18:34:06 +02003527
Takashi Iwai352f7f92012-12-19 12:52:06 +01003528 err = create_capture_mixers(codec);
3529 if (err < 0)
3530 return err;
3531
3532 err = parse_mic_boost(codec);
3533 if (err < 0)
3534 return err;
3535
Takashi Iwai978e77e2013-01-10 16:57:58 +01003536 if (spec->add_out_jack_modes) {
3537 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
3538 err = create_out_jack_modes(codec, cfg->line_outs,
3539 cfg->line_out_pins);
3540 if (err < 0)
3541 return err;
3542 }
3543 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
3544 err = create_out_jack_modes(codec, cfg->hp_outs,
3545 cfg->hp_pins);
3546 if (err < 0)
3547 return err;
3548 }
3549 }
3550
Takashi Iwai352f7f92012-12-19 12:52:06 +01003551 dig_only:
3552 parse_digital(codec);
3553
3554 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003556EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557
3558
3559/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003560 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003562
3563/* slave controls for virtual master */
3564static const char * const slave_pfxs[] = {
3565 "Front", "Surround", "Center", "LFE", "Side",
3566 "Headphone", "Speaker", "Mono", "Line Out",
3567 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01003568 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
3569 "Headphone Front", "Headphone Surround", "Headphone CLFE",
3570 "Headphone Side",
Takashi Iwai352f7f92012-12-19 12:52:06 +01003571 NULL,
3572};
3573
3574int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003576 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578
Takashi Iwai36502d02012-12-19 15:15:10 +01003579 if (spec->kctls.used) {
3580 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
3581 if (err < 0)
3582 return err;
3583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584
Takashi Iwai352f7f92012-12-19 12:52:06 +01003585 if (spec->multiout.dig_out_nid) {
3586 err = snd_hda_create_dig_out_ctls(codec,
3587 spec->multiout.dig_out_nid,
3588 spec->multiout.dig_out_nid,
3589 spec->pcm_rec[1].pcm_type);
3590 if (err < 0)
3591 return err;
3592 if (!spec->no_analog) {
3593 err = snd_hda_create_spdif_share_sw(codec,
3594 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 if (err < 0)
3596 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003597 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 }
3599 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003600 if (spec->dig_in_nid) {
3601 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
3602 if (err < 0)
3603 return err;
3604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605
Takashi Iwai352f7f92012-12-19 12:52:06 +01003606 /* if we have no master control, let's create it */
3607 if (!spec->no_analog &&
3608 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
3609 unsigned int vmaster_tlv[4];
3610 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
3611 HDA_OUTPUT, vmaster_tlv);
3612 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
3613 vmaster_tlv, slave_pfxs,
3614 "Playback Volume");
3615 if (err < 0)
3616 return err;
3617 }
3618 if (!spec->no_analog &&
3619 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
3620 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
3621 NULL, slave_pfxs,
3622 "Playback Switch",
3623 true, &spec->vmaster_mute.sw_kctl);
3624 if (err < 0)
3625 return err;
3626 if (spec->vmaster_mute.hook)
Takashi Iwaifd25a972012-12-20 14:57:18 +01003627 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
3628 spec->vmaster_mute_enum);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630
Takashi Iwai352f7f92012-12-19 12:52:06 +01003631 free_kctls(spec); /* no longer needed */
3632
3633 if (spec->shared_mic_hp) {
3634 int err;
3635 int nid = spec->autocfg.inputs[1].pin;
3636 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
3637 if (err < 0)
3638 return err;
3639 err = snd_hda_jack_detect_enable(codec, nid, 0);
3640 if (err < 0)
3641 return err;
3642 }
3643
3644 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
3645 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 return err;
3647
3648 return 0;
3649}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003650EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
3651
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652
3653/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003654 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003657static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
3658 struct hda_codec *codec,
3659 struct snd_pcm_substream *substream,
3660 int action)
3661{
3662 struct hda_gen_spec *spec = codec->spec;
3663 if (spec->pcm_playback_hook)
3664 spec->pcm_playback_hook(hinfo, codec, substream, action);
3665}
3666
Takashi Iwai352f7f92012-12-19 12:52:06 +01003667/*
3668 * Analog playback callbacks
3669 */
3670static int playback_pcm_open(struct hda_pcm_stream *hinfo,
3671 struct hda_codec *codec,
3672 struct snd_pcm_substream *substream)
3673{
3674 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003675 int err;
3676
3677 mutex_lock(&spec->pcm_mutex);
3678 err = snd_hda_multi_out_analog_open(codec,
3679 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003680 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003681 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003682 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003683 call_pcm_playback_hook(hinfo, codec, substream,
3684 HDA_GEN_PCM_ACT_OPEN);
3685 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003686 mutex_unlock(&spec->pcm_mutex);
3687 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003688}
3689
3690static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01003691 struct hda_codec *codec,
3692 unsigned int stream_tag,
3693 unsigned int format,
3694 struct snd_pcm_substream *substream)
3695{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003696 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003697 int err;
3698
3699 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
3700 stream_tag, format, substream);
3701 if (!err)
3702 call_pcm_playback_hook(hinfo, codec, substream,
3703 HDA_GEN_PCM_ACT_PREPARE);
3704 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003705}
Takashi Iwai97ec5582006-03-21 11:29:07 +01003706
Takashi Iwai352f7f92012-12-19 12:52:06 +01003707static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3708 struct hda_codec *codec,
3709 struct snd_pcm_substream *substream)
3710{
3711 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003712 int err;
3713
3714 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
3715 if (!err)
3716 call_pcm_playback_hook(hinfo, codec, substream,
3717 HDA_GEN_PCM_ACT_CLEANUP);
3718 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003719}
3720
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003721static int playback_pcm_close(struct hda_pcm_stream *hinfo,
3722 struct hda_codec *codec,
3723 struct snd_pcm_substream *substream)
3724{
3725 struct hda_gen_spec *spec = codec->spec;
3726 mutex_lock(&spec->pcm_mutex);
3727 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003728 call_pcm_playback_hook(hinfo, codec, substream,
3729 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003730 mutex_unlock(&spec->pcm_mutex);
3731 return 0;
3732}
3733
3734static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
3735 struct hda_codec *codec,
3736 struct snd_pcm_substream *substream)
3737{
3738 struct hda_gen_spec *spec = codec->spec;
3739 int err = 0;
3740
3741 mutex_lock(&spec->pcm_mutex);
3742 if (!spec->indep_hp_enabled)
3743 err = -EBUSY;
3744 else
3745 spec->active_streams |= 1 << STREAM_INDEP_HP;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003746 call_pcm_playback_hook(hinfo, codec, substream,
3747 HDA_GEN_PCM_ACT_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003748 mutex_unlock(&spec->pcm_mutex);
3749 return err;
3750}
3751
3752static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
3753 struct hda_codec *codec,
3754 struct snd_pcm_substream *substream)
3755{
3756 struct hda_gen_spec *spec = codec->spec;
3757 mutex_lock(&spec->pcm_mutex);
3758 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003759 call_pcm_playback_hook(hinfo, codec, substream,
3760 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003761 mutex_unlock(&spec->pcm_mutex);
3762 return 0;
3763}
3764
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003765static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3766 struct hda_codec *codec,
3767 unsigned int stream_tag,
3768 unsigned int format,
3769 struct snd_pcm_substream *substream)
3770{
3771 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3772 call_pcm_playback_hook(hinfo, codec, substream,
3773 HDA_GEN_PCM_ACT_PREPARE);
3774 return 0;
3775}
3776
3777static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3778 struct hda_codec *codec,
3779 struct snd_pcm_substream *substream)
3780{
3781 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
3782 call_pcm_playback_hook(hinfo, codec, substream,
3783 HDA_GEN_PCM_ACT_CLEANUP);
3784 return 0;
3785}
3786
Takashi Iwai352f7f92012-12-19 12:52:06 +01003787/*
3788 * Digital out
3789 */
3790static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
3791 struct hda_codec *codec,
3792 struct snd_pcm_substream *substream)
3793{
3794 struct hda_gen_spec *spec = codec->spec;
3795 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3796}
3797
3798static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3799 struct hda_codec *codec,
3800 unsigned int stream_tag,
3801 unsigned int format,
3802 struct snd_pcm_substream *substream)
3803{
3804 struct hda_gen_spec *spec = codec->spec;
3805 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3806 stream_tag, format, substream);
3807}
3808
3809static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3810 struct hda_codec *codec,
3811 struct snd_pcm_substream *substream)
3812{
3813 struct hda_gen_spec *spec = codec->spec;
3814 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
3815}
3816
3817static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
3818 struct hda_codec *codec,
3819 struct snd_pcm_substream *substream)
3820{
3821 struct hda_gen_spec *spec = codec->spec;
3822 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3823}
3824
3825/*
3826 * Analog capture
3827 */
3828static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3829 struct hda_codec *codec,
3830 unsigned int stream_tag,
3831 unsigned int format,
3832 struct snd_pcm_substream *substream)
3833{
3834 struct hda_gen_spec *spec = codec->spec;
3835
3836 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01003837 stream_tag, 0, format);
3838 return 0;
3839}
3840
Takashi Iwai352f7f92012-12-19 12:52:06 +01003841static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3842 struct hda_codec *codec,
3843 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01003844{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003845 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01003846
Takashi Iwai352f7f92012-12-19 12:52:06 +01003847 snd_hda_codec_cleanup_stream(codec,
3848 spec->adc_nids[substream->number + 1]);
Takashi Iwai97ec5582006-03-21 11:29:07 +01003849 return 0;
3850}
3851
Takashi Iwai352f7f92012-12-19 12:52:06 +01003852/*
3853 */
3854static const struct hda_pcm_stream pcm_analog_playback = {
3855 .substreams = 1,
3856 .channels_min = 2,
3857 .channels_max = 8,
3858 /* NID is set in build_pcms */
3859 .ops = {
3860 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003861 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003862 .prepare = playback_pcm_prepare,
3863 .cleanup = playback_pcm_cleanup
3864 },
3865};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866
Takashi Iwai352f7f92012-12-19 12:52:06 +01003867static const struct hda_pcm_stream pcm_analog_capture = {
3868 .substreams = 1,
3869 .channels_min = 2,
3870 .channels_max = 2,
3871 /* NID is set in build_pcms */
3872};
3873
3874static const struct hda_pcm_stream pcm_analog_alt_playback = {
3875 .substreams = 1,
3876 .channels_min = 2,
3877 .channels_max = 2,
3878 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003879 .ops = {
3880 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003881 .close = alt_playback_pcm_close,
3882 .prepare = alt_playback_pcm_prepare,
3883 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003884 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01003885};
3886
3887static const struct hda_pcm_stream pcm_analog_alt_capture = {
3888 .substreams = 2, /* can be overridden */
3889 .channels_min = 2,
3890 .channels_max = 2,
3891 /* NID is set in build_pcms */
3892 .ops = {
3893 .prepare = alt_capture_pcm_prepare,
3894 .cleanup = alt_capture_pcm_cleanup
3895 },
3896};
3897
3898static const struct hda_pcm_stream pcm_digital_playback = {
3899 .substreams = 1,
3900 .channels_min = 2,
3901 .channels_max = 2,
3902 /* NID is set in build_pcms */
3903 .ops = {
3904 .open = dig_playback_pcm_open,
3905 .close = dig_playback_pcm_close,
3906 .prepare = dig_playback_pcm_prepare,
3907 .cleanup = dig_playback_pcm_cleanup
3908 },
3909};
3910
3911static const struct hda_pcm_stream pcm_digital_capture = {
3912 .substreams = 1,
3913 .channels_min = 2,
3914 .channels_max = 2,
3915 /* NID is set in build_pcms */
3916};
3917
3918/* Used by build_pcms to flag that a PCM has no playback stream */
3919static const struct hda_pcm_stream pcm_null_stream = {
3920 .substreams = 0,
3921 .channels_min = 0,
3922 .channels_max = 0,
3923};
3924
3925/*
3926 * dynamic changing ADC PCM streams
3927 */
3928static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
3929{
3930 struct hda_gen_spec *spec = codec->spec;
3931 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
3932
3933 if (spec->cur_adc && spec->cur_adc != new_adc) {
3934 /* stream is running, let's swap the current ADC */
3935 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
3936 spec->cur_adc = new_adc;
3937 snd_hda_codec_setup_stream(codec, new_adc,
3938 spec->cur_adc_stream_tag, 0,
3939 spec->cur_adc_format);
3940 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003942 return false;
3943}
3944
3945/* analog capture with dynamic dual-adc changes */
3946static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3947 struct hda_codec *codec,
3948 unsigned int stream_tag,
3949 unsigned int format,
3950 struct snd_pcm_substream *substream)
3951{
3952 struct hda_gen_spec *spec = codec->spec;
3953 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
3954 spec->cur_adc_stream_tag = stream_tag;
3955 spec->cur_adc_format = format;
3956 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
3957 return 0;
3958}
3959
3960static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3961 struct hda_codec *codec,
3962 struct snd_pcm_substream *substream)
3963{
3964 struct hda_gen_spec *spec = codec->spec;
3965 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
3966 spec->cur_adc = 0;
3967 return 0;
3968}
3969
3970static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
3971 .substreams = 1,
3972 .channels_min = 2,
3973 .channels_max = 2,
3974 .nid = 0, /* fill later */
3975 .ops = {
3976 .prepare = dyn_adc_capture_pcm_prepare,
3977 .cleanup = dyn_adc_capture_pcm_cleanup
3978 },
3979};
3980
Takashi Iwaif873e532012-12-20 16:58:39 +01003981static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
3982 const char *chip_name)
3983{
3984 char *p;
3985
3986 if (*str)
3987 return;
3988 strlcpy(str, chip_name, len);
3989
3990 /* drop non-alnum chars after a space */
3991 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
3992 if (!isalnum(p[1])) {
3993 *p = 0;
3994 break;
3995 }
3996 }
3997 strlcat(str, sfx, len);
3998}
3999
Takashi Iwai352f7f92012-12-19 12:52:06 +01004000/* build PCM streams based on the parsed results */
4001int snd_hda_gen_build_pcms(struct hda_codec *codec)
4002{
4003 struct hda_gen_spec *spec = codec->spec;
4004 struct hda_pcm *info = spec->pcm_rec;
4005 const struct hda_pcm_stream *p;
4006 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007
4008 codec->num_pcms = 1;
4009 codec->pcm_info = info;
4010
Takashi Iwai352f7f92012-12-19 12:52:06 +01004011 if (spec->no_analog)
4012 goto skip_analog;
4013
Takashi Iwaif873e532012-12-20 16:58:39 +01004014 fill_pcm_stream_name(spec->stream_name_analog,
4015 sizeof(spec->stream_name_analog),
4016 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004017 info->name = spec->stream_name_analog;
4018
4019 if (spec->multiout.num_dacs > 0) {
4020 p = spec->stream_analog_playback;
4021 if (!p)
4022 p = &pcm_analog_playback;
4023 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4024 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
4025 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
4026 spec->multiout.max_channels;
4027 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
4028 spec->autocfg.line_outs == 2)
4029 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
4030 snd_pcm_2_1_chmaps;
4031 }
4032 if (spec->num_adc_nids) {
4033 p = spec->stream_analog_capture;
4034 if (!p) {
4035 if (spec->dyn_adc_switch)
4036 p = &dyn_adc_pcm_analog_capture;
4037 else
4038 p = &pcm_analog_capture;
4039 }
4040 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4041 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
4042 }
4043
Takashi Iwai352f7f92012-12-19 12:52:06 +01004044 skip_analog:
4045 /* SPDIF for stream index #1 */
4046 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01004047 fill_pcm_stream_name(spec->stream_name_digital,
4048 sizeof(spec->stream_name_digital),
4049 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004050 codec->num_pcms = 2;
4051 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
4052 info = spec->pcm_rec + 1;
4053 info->name = spec->stream_name_digital;
4054 if (spec->dig_out_type)
4055 info->pcm_type = spec->dig_out_type;
4056 else
4057 info->pcm_type = HDA_PCM_TYPE_SPDIF;
4058 if (spec->multiout.dig_out_nid) {
4059 p = spec->stream_digital_playback;
4060 if (!p)
4061 p = &pcm_digital_playback;
4062 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4063 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
4064 }
4065 if (spec->dig_in_nid) {
4066 p = spec->stream_digital_capture;
4067 if (!p)
4068 p = &pcm_digital_capture;
4069 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4070 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
4071 }
4072 }
4073
4074 if (spec->no_analog)
4075 return 0;
4076
4077 /* If the use of more than one ADC is requested for the current
4078 * model, configure a second analog capture-only PCM.
4079 */
4080 have_multi_adcs = (spec->num_adc_nids > 1) &&
4081 !spec->dyn_adc_switch && !spec->auto_mic;
4082 /* Additional Analaog capture for index #2 */
4083 if (spec->alt_dac_nid || have_multi_adcs) {
4084 codec->num_pcms = 3;
4085 info = spec->pcm_rec + 2;
4086 info->name = spec->stream_name_analog;
4087 if (spec->alt_dac_nid) {
4088 p = spec->stream_analog_alt_playback;
4089 if (!p)
4090 p = &pcm_analog_alt_playback;
4091 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4092 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
4093 spec->alt_dac_nid;
4094 } else {
4095 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
4096 pcm_null_stream;
4097 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
4098 }
4099 if (have_multi_adcs) {
4100 p = spec->stream_analog_alt_capture;
4101 if (!p)
4102 p = &pcm_analog_alt_capture;
4103 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4104 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
4105 spec->adc_nids[1];
4106 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
4107 spec->num_adc_nids - 1;
4108 } else {
4109 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
4110 pcm_null_stream;
4111 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
4112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113 }
4114
4115 return 0;
4116}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004117EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
4118
4119
4120/*
4121 * Standard auto-parser initializations
4122 */
4123
Takashi Iwaid4156932013-01-07 10:08:02 +01004124/* configure the given path as a proper output */
Takashi Iwai2c12c302013-01-10 09:33:29 +01004125static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004126{
4127 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01004128 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004129
Takashi Iwai196c17662013-01-04 15:01:40 +01004130 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01004131 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004132 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01004133 pin = path->path[path->depth - 1];
Takashi Iwai2c12c302013-01-10 09:33:29 +01004134 restore_pin_ctl(codec, pin);
Takashi Iwaie1284af2013-01-03 16:33:02 +01004135 snd_hda_activate_path(codec, path, path->active, true);
4136 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004137}
4138
4139/* initialize primary output paths */
4140static void init_multi_out(struct hda_codec *codec)
4141{
4142 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004143 int i;
4144
Takashi Iwaid4156932013-01-07 10:08:02 +01004145 for (i = 0; i < spec->autocfg.line_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004146 set_output_and_unmute(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004147}
4148
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004149
Takashi Iwai2c12c302013-01-10 09:33:29 +01004150static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004151{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004152 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004153
Takashi Iwaid4156932013-01-07 10:08:02 +01004154 for (i = 0; i < num_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004155 set_output_and_unmute(codec, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004156}
4157
4158/* initialize hp and speaker paths */
4159static void init_extra_out(struct hda_codec *codec)
4160{
4161 struct hda_gen_spec *spec = codec->spec;
4162
4163 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004164 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004165 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
4166 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai2c12c302013-01-10 09:33:29 +01004167 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004168}
4169
4170/* initialize multi-io paths */
4171static void init_multi_io(struct hda_codec *codec)
4172{
4173 struct hda_gen_spec *spec = codec->spec;
4174 int i;
4175
4176 for (i = 0; i < spec->multi_ios; i++) {
4177 hda_nid_t pin = spec->multi_io[i].pin;
4178 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01004179 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004180 if (!path)
4181 continue;
4182 if (!spec->multi_io[i].ctl_in)
4183 spec->multi_io[i].ctl_in =
Takashi Iwai2c12c302013-01-10 09:33:29 +01004184 snd_hda_codec_get_pin_target(codec, pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004185 snd_hda_activate_path(codec, path, path->active, true);
4186 }
4187}
4188
Takashi Iwai352f7f92012-12-19 12:52:06 +01004189/* set up input pins and loopback paths */
4190static void init_analog_input(struct hda_codec *codec)
4191{
4192 struct hda_gen_spec *spec = codec->spec;
4193 struct auto_pin_cfg *cfg = &spec->autocfg;
4194 int i;
4195
4196 for (i = 0; i < cfg->num_inputs; i++) {
4197 hda_nid_t nid = cfg->inputs[i].pin;
4198 if (is_input_pin(codec, nid))
Takashi Iwai2c12c302013-01-10 09:33:29 +01004199 restore_pin_ctl(codec, nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004200
4201 /* init loopback inputs */
4202 if (spec->mixer_nid) {
4203 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01004204 path = snd_hda_get_path_from_idx(codec, spec->loopback_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004205 if (path)
4206 snd_hda_activate_path(codec, path,
4207 path->active, false);
4208 }
4209 }
4210}
4211
4212/* initialize ADC paths */
4213static void init_input_src(struct hda_codec *codec)
4214{
4215 struct hda_gen_spec *spec = codec->spec;
4216 struct hda_input_mux *imux = &spec->input_mux;
4217 struct nid_path *path;
4218 int i, c, nums;
4219
4220 if (spec->dyn_adc_switch)
4221 nums = 1;
4222 else
4223 nums = spec->num_adc_nids;
4224
4225 for (c = 0; c < nums; c++) {
4226 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01004227 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004228 if (path) {
4229 bool active = path->active;
4230 if (i == spec->cur_mux[c])
4231 active = true;
4232 snd_hda_activate_path(codec, path, active, false);
4233 }
4234 }
4235 }
4236
4237 if (spec->shared_mic_hp)
4238 update_shared_mic_hp(codec, spec->cur_mux[0]);
4239
4240 if (spec->cap_sync_hook)
4241 spec->cap_sync_hook(codec);
4242}
4243
4244/* set right pin controls for digital I/O */
4245static void init_digital(struct hda_codec *codec)
4246{
4247 struct hda_gen_spec *spec = codec->spec;
4248 int i;
4249 hda_nid_t pin;
4250
Takashi Iwaid4156932013-01-07 10:08:02 +01004251 for (i = 0; i < spec->autocfg.dig_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004252 set_output_and_unmute(codec, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004253 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01004254 if (pin) {
4255 struct nid_path *path;
Takashi Iwai2c12c302013-01-10 09:33:29 +01004256 restore_pin_ctl(codec, pin);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01004257 path = snd_hda_get_path_from_idx(codec, spec->digin_path);
4258 if (path)
4259 snd_hda_activate_path(codec, path, path->active, false);
4260 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004261}
4262
Takashi Iwai973e4972012-12-20 15:16:09 +01004263/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
4264 * invalid unsol tags by some reason
4265 */
4266static void clear_unsol_on_unused_pins(struct hda_codec *codec)
4267{
4268 int i;
4269
4270 for (i = 0; i < codec->init_pins.used; i++) {
4271 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
4272 hda_nid_t nid = pin->nid;
4273 if (is_jack_detectable(codec, nid) &&
4274 !snd_hda_jack_tbl_get(codec, nid))
4275 snd_hda_codec_update_cache(codec, nid, 0,
4276 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
4277 }
4278}
4279
Takashi Iwai5187ac12013-01-07 12:52:16 +01004280/*
4281 * initialize the generic spec;
4282 * this can be put as patch_ops.init function
4283 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004284int snd_hda_gen_init(struct hda_codec *codec)
4285{
4286 struct hda_gen_spec *spec = codec->spec;
4287
4288 if (spec->init_hook)
4289 spec->init_hook(codec);
4290
4291 snd_hda_apply_verbs(codec);
4292
Takashi Iwai3bbcd272012-12-20 11:50:58 +01004293 codec->cached_write = 1;
4294
Takashi Iwai352f7f92012-12-19 12:52:06 +01004295 init_multi_out(codec);
4296 init_extra_out(codec);
4297 init_multi_io(codec);
4298 init_analog_input(codec);
4299 init_input_src(codec);
4300 init_digital(codec);
4301
Takashi Iwai973e4972012-12-20 15:16:09 +01004302 clear_unsol_on_unused_pins(codec);
4303
Takashi Iwai352f7f92012-12-19 12:52:06 +01004304 /* call init functions of standard auto-mute helpers */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01004305 update_automute_all(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004306
Takashi Iwai3bbcd272012-12-20 11:50:58 +01004307 snd_hda_codec_flush_amp_cache(codec);
4308 snd_hda_codec_flush_cmd_cache(codec);
4309
Takashi Iwai352f7f92012-12-19 12:52:06 +01004310 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
4311 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
4312
4313 hda_call_check_power_status(codec, 0x01);
4314 return 0;
4315}
Takashi Iwaifce52a32013-01-07 12:42:48 +01004316EXPORT_SYMBOL_HDA(snd_hda_gen_init);
4317
Takashi Iwai5187ac12013-01-07 12:52:16 +01004318/*
4319 * free the generic spec;
4320 * this can be put as patch_ops.free function
4321 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01004322void snd_hda_gen_free(struct hda_codec *codec)
4323{
4324 snd_hda_gen_spec_free(codec->spec);
4325 kfree(codec->spec);
4326 codec->spec = NULL;
4327}
4328EXPORT_SYMBOL_HDA(snd_hda_gen_free);
4329
4330#ifdef CONFIG_PM
Takashi Iwai5187ac12013-01-07 12:52:16 +01004331/*
4332 * check the loopback power save state;
4333 * this can be put as patch_ops.check_power_status function
4334 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01004335int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
4336{
4337 struct hda_gen_spec *spec = codec->spec;
4338 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
4339}
4340EXPORT_SYMBOL_HDA(snd_hda_gen_check_power_status);
4341#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01004342
4343
4344/*
4345 * the generic codec support
4346 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347
Takashi Iwai352f7f92012-12-19 12:52:06 +01004348static const struct hda_codec_ops generic_patch_ops = {
4349 .build_controls = snd_hda_gen_build_controls,
4350 .build_pcms = snd_hda_gen_build_pcms,
4351 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01004352 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004353 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02004354#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01004355 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02004356#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357};
4358
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359int snd_hda_parse_generic_codec(struct hda_codec *codec)
4360{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004361 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 int err;
4363
Takashi Iwaie560d8d2005-09-09 14:21:46 +02004364 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004365 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004367 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004370 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
4371 if (err < 0)
4372 return err;
4373
4374 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004375 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 goto error;
4377
4378 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 return 0;
4380
Takashi Iwai352f7f92012-12-19 12:52:06 +01004381error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01004382 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 return err;
4384}
Takashi Iwaifce52a32013-01-07 12:42:48 +01004385EXPORT_SYMBOL_HDA(snd_hda_parse_generic_codec);