blob: 932e6a133f3d5e0947c9efda457eb2a04fb06c0c [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
487#define nid_has_mute(codec, nid, dir) \
488 check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
489#define nid_has_volume(codec, nid, dir) \
490 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
491
492/* look for a widget suitable for assigning a mute switch in the path */
493static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
494 struct nid_path *path)
495{
496 int i;
497
498 for (i = path->depth - 1; i >= 0; i--) {
499 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
500 return path->path[i];
501 if (i != path->depth - 1 && i != 0 &&
502 nid_has_mute(codec, path->path[i], HDA_INPUT))
503 return path->path[i];
504 }
505 return 0;
506}
507
508/* look for a widget suitable for assigning a volume ctl in the path */
509static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
510 struct nid_path *path)
511{
512 int i;
513
514 for (i = path->depth - 1; i >= 0; i--) {
515 if (nid_has_volume(codec, path->path[i], HDA_OUTPUT))
516 return path->path[i];
517 }
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200518 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519}
520
521/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100522 * path activation / deactivation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100524
525/* can have the amp-in capability? */
526static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100528 hda_nid_t nid = path->path[idx];
529 unsigned int caps = get_wcaps(codec, nid);
530 unsigned int type = get_wcaps_type(caps);
531
532 if (!(caps & AC_WCAP_IN_AMP))
533 return false;
534 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
535 return false;
536 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
Takashi Iwai352f7f92012-12-19 12:52:06 +0100539/* can have the amp-out capability? */
540static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100542 hda_nid_t nid = path->path[idx];
543 unsigned int caps = get_wcaps(codec, nid);
544 unsigned int type = get_wcaps_type(caps);
545
546 if (!(caps & AC_WCAP_OUT_AMP))
547 return false;
548 if (type == AC_WID_PIN && !idx) /* only for output pins */
549 return false;
550 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551}
552
Takashi Iwai352f7f92012-12-19 12:52:06 +0100553/* check whether the given (nid,dir,idx) is active */
554static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
555 unsigned int idx, unsigned int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100557 struct hda_gen_spec *spec = codec->spec;
558 int i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Takashi Iwai352f7f92012-12-19 12:52:06 +0100560 for (n = 0; n < spec->paths.used; n++) {
561 struct nid_path *path = snd_array_elem(&spec->paths, n);
562 if (!path->active)
563 continue;
564 for (i = 0; i < path->depth; i++) {
565 if (path->path[i] == nid) {
566 if (dir == HDA_OUTPUT || path->idx[i] == idx)
567 return true;
568 break;
569 }
570 }
571 }
572 return false;
573}
574
575/* get the default amp value for the target state */
576static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
577 int dir, bool enable)
578{
579 unsigned int caps;
580 unsigned int val = 0;
581
582 caps = query_amp_caps(codec, nid, dir);
583 if (caps & AC_AMPCAP_NUM_STEPS) {
584 /* set to 0dB */
585 if (enable)
586 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
587 }
588 if (caps & AC_AMPCAP_MUTE) {
589 if (!enable)
590 val |= HDA_AMP_MUTE;
591 }
592 return val;
593}
594
595/* initialize the amp value (only at the first time) */
596static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
597{
598 int val = get_amp_val_to_activate(codec, nid, dir, false);
599 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
600}
601
602static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
603 int idx, bool enable)
604{
605 int val;
606 if (is_ctl_associated(codec, nid, dir, idx) ||
Takashi Iwai985803c2013-01-03 16:30:04 +0100607 (!enable && is_active_nid(codec, nid, dir, idx)))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100608 return;
609 val = get_amp_val_to_activate(codec, nid, dir, enable);
610 snd_hda_codec_amp_stereo(codec, nid, dir, idx, 0xff, val);
611}
612
613static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
614 int i, bool enable)
615{
616 hda_nid_t nid = path->path[i];
617 init_amp(codec, nid, HDA_OUTPUT, 0);
618 activate_amp(codec, nid, HDA_OUTPUT, 0, enable);
619}
620
621static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
622 int i, bool enable, bool add_aamix)
623{
624 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100625 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100626 int n, nums, idx;
627 int type;
628 hda_nid_t nid = path->path[i];
629
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100630 nums = snd_hda_get_conn_list(codec, nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100631 type = get_wcaps_type(get_wcaps(codec, nid));
632 if (type == AC_WID_PIN ||
633 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
634 nums = 1;
635 idx = 0;
636 } else
637 idx = path->idx[i];
638
639 for (n = 0; n < nums; n++)
640 init_amp(codec, nid, HDA_INPUT, n);
641
642 if (is_ctl_associated(codec, nid, HDA_INPUT, idx))
643 return;
644
645 /* here is a little bit tricky in comparison with activate_amp_out();
646 * when aa-mixer is available, we need to enable the path as well
647 */
648 for (n = 0; n < nums; n++) {
649 if (n != idx && (!add_aamix || conn[n] != spec->mixer_nid))
650 continue;
651 activate_amp(codec, nid, HDA_INPUT, n, enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 }
653}
654
Takashi Iwai352f7f92012-12-19 12:52:06 +0100655/* activate or deactivate the given path
656 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100658void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
659 bool enable, bool add_aamix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100661 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Takashi Iwai352f7f92012-12-19 12:52:06 +0100663 if (!enable)
664 path->active = false;
665
666 for (i = path->depth - 1; i >= 0; i--) {
667 if (enable && path->multi[i])
668 snd_hda_codec_write_cache(codec, path->path[i], 0,
669 AC_VERB_SET_CONNECT_SEL,
670 path->idx[i]);
671 if (has_amp_in(codec, path, i))
672 activate_amp_in(codec, path, i, enable, add_aamix);
673 if (has_amp_out(codec, path, i))
674 activate_amp_out(codec, path, i, enable);
675 }
676
677 if (enable)
678 path->active = true;
679}
680EXPORT_SYMBOL_HDA(snd_hda_activate_path);
681
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100682/* turn on/off EAPD on the given pin */
683static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
684{
685 struct hda_gen_spec *spec = codec->spec;
686 if (spec->own_eapd_ctl ||
687 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
688 return;
Takashi Iwaiecac3ed2012-12-21 15:23:01 +0100689 if (codec->inv_eapd)
690 enable = !enable;
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100691 snd_hda_codec_update_cache(codec, pin, 0,
692 AC_VERB_SET_EAPD_BTLENABLE,
693 enable ? 0x02 : 0x00);
694}
695
Takashi Iwai352f7f92012-12-19 12:52:06 +0100696
697/*
698 * Helper functions for creating mixer ctl elements
699 */
700
701enum {
702 HDA_CTL_WIDGET_VOL,
703 HDA_CTL_WIDGET_MUTE,
704 HDA_CTL_BIND_MUTE,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100705};
706static const struct snd_kcontrol_new control_templates[] = {
707 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
708 HDA_CODEC_MUTE(NULL, 0, 0, 0),
709 HDA_BIND_MUTE(NULL, 0, 0, 0),
Takashi Iwai352f7f92012-12-19 12:52:06 +0100710};
711
712/* add dynamic controls from template */
713static int add_control(struct hda_gen_spec *spec, int type, const char *name,
714 int cidx, unsigned long val)
715{
716 struct snd_kcontrol_new *knew;
717
Takashi Iwai12c93df2012-12-19 14:38:33 +0100718 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100719 if (!knew)
720 return -ENOMEM;
721 knew->index = cidx;
722 if (get_amp_nid_(val))
723 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
724 knew->private_value = val;
725 return 0;
726}
727
728static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
729 const char *pfx, const char *dir,
730 const char *sfx, int cidx, unsigned long val)
731{
732 char name[32];
733 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
734 return add_control(spec, type, name, cidx, val);
735}
736
737#define add_pb_vol_ctrl(spec, type, pfx, val) \
738 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
739#define add_pb_sw_ctrl(spec, type, pfx, val) \
740 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
741#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
742 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
743#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
744 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
745
746static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
747 unsigned int chs, struct nid_path *path)
748{
749 unsigned int val;
750 if (!path)
751 return 0;
752 val = path->ctls[NID_PATH_VOL_CTL];
753 if (!val)
754 return 0;
755 val = amp_val_replace_channels(val, chs);
756 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
757}
758
759/* return the channel bits suitable for the given path->ctls[] */
760static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
761 int type)
762{
763 int chs = 1; /* mono (left only) */
764 if (path) {
765 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
766 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
767 chs = 3; /* stereo */
768 }
769 return chs;
770}
771
772static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
773 struct nid_path *path)
774{
775 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
776 return add_vol_ctl(codec, pfx, cidx, chs, path);
777}
778
779/* create a mute-switch for the given mixer widget;
780 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
781 */
782static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
783 unsigned int chs, struct nid_path *path)
784{
785 unsigned int val;
786 int type = HDA_CTL_WIDGET_MUTE;
787
788 if (!path)
789 return 0;
790 val = path->ctls[NID_PATH_MUTE_CTL];
791 if (!val)
792 return 0;
793 val = amp_val_replace_channels(val, chs);
794 if (get_amp_direction_(val) == HDA_INPUT) {
795 hda_nid_t nid = get_amp_nid_(val);
796 int nums = snd_hda_get_num_conns(codec, nid);
797 if (nums > 1) {
798 type = HDA_CTL_BIND_MUTE;
799 val |= nums << 19;
800 }
801 }
802 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
803}
804
805static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
806 int cidx, struct nid_path *path)
807{
808 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
809 return add_sw_ctl(codec, pfx, cidx, chs, path);
810}
811
812static const char * const channel_name[4] = {
813 "Front", "Surround", "CLFE", "Side"
814};
815
816/* give some appropriate ctl name prefix for the given line out channel */
817static const char *get_line_out_pfx(struct hda_gen_spec *spec, int ch,
818 bool can_be_master, int *index)
819{
820 struct auto_pin_cfg *cfg = &spec->autocfg;
821
822 *index = 0;
823 if (cfg->line_outs == 1 && !spec->multi_ios &&
824 !cfg->hp_outs && !cfg->speaker_outs && can_be_master)
825 return spec->vmaster_mute.hook ? "PCM" : "Master";
826
827 /* if there is really a single DAC used in the whole output paths,
828 * use it master (or "PCM" if a vmaster hook is present)
829 */
830 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
831 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
832 return spec->vmaster_mute.hook ? "PCM" : "Master";
833
834 switch (cfg->line_out_type) {
835 case AUTO_PIN_SPEAKER_OUT:
836 if (cfg->line_outs == 1)
837 return "Speaker";
838 if (cfg->line_outs == 2)
839 return ch ? "Bass Speaker" : "Speaker";
840 break;
841 case AUTO_PIN_HP_OUT:
842 /* for multi-io case, only the primary out */
843 if (ch && spec->multi_ios)
844 break;
845 *index = ch;
846 return "Headphone";
847 default:
848 if (cfg->line_outs == 1 && !spec->multi_ios)
849 return "PCM";
850 break;
851 }
852 if (ch >= ARRAY_SIZE(channel_name)) {
853 snd_BUG();
854 return "PCM";
855 }
856
857 return channel_name[ch];
858}
859
860/*
861 * Parse output paths
862 */
863
864/* badness definition */
865enum {
866 /* No primary DAC is found for the main output */
867 BAD_NO_PRIMARY_DAC = 0x10000,
868 /* No DAC is found for the extra output */
869 BAD_NO_DAC = 0x4000,
870 /* No possible multi-ios */
871 BAD_MULTI_IO = 0x103,
872 /* No individual DAC for extra output */
873 BAD_NO_EXTRA_DAC = 0x102,
874 /* No individual DAC for extra surrounds */
875 BAD_NO_EXTRA_SURR_DAC = 0x101,
876 /* Primary DAC shared with main surrounds */
877 BAD_SHARED_SURROUND = 0x100,
878 /* Primary DAC shared with main CLFE */
879 BAD_SHARED_CLFE = 0x10,
880 /* Primary DAC shared with extra surrounds */
881 BAD_SHARED_EXTRA_SURROUND = 0x10,
882 /* Volume widget is shared */
883 BAD_SHARED_VOL = 0x10,
884};
885
Takashi Iwai0e614dd2013-01-07 15:11:44 +0100886/* look for widgets in the given path which are appropriate for
Takashi Iwai352f7f92012-12-19 12:52:06 +0100887 * volume and mute controls, and assign the values to ctls[].
888 *
889 * When no appropriate widget is found in the path, the badness value
890 * is incremented depending on the situation. The function returns the
891 * total badness for both volume and mute controls.
892 */
Takashi Iwai0e614dd2013-01-07 15:11:44 +0100893static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100894{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100895 hda_nid_t nid;
896 unsigned int val;
897 int badness = 0;
898
899 if (!path)
900 return BAD_SHARED_VOL * 2;
Takashi Iwai0e614dd2013-01-07 15:11:44 +0100901
902 if (path->ctls[NID_PATH_VOL_CTL] ||
903 path->ctls[NID_PATH_MUTE_CTL])
904 return 0; /* already evaluated */
905
Takashi Iwai352f7f92012-12-19 12:52:06 +0100906 nid = look_for_out_vol_nid(codec, path);
907 if (nid) {
908 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
909 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
910 badness += BAD_SHARED_VOL;
911 else
912 path->ctls[NID_PATH_VOL_CTL] = val;
913 } else
914 badness += BAD_SHARED_VOL;
915 nid = look_for_out_mute_nid(codec, path);
916 if (nid) {
917 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
918 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
919 nid_has_mute(codec, nid, HDA_OUTPUT))
920 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
921 else
922 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
923 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
924 badness += BAD_SHARED_VOL;
925 else
926 path->ctls[NID_PATH_MUTE_CTL] = val;
927 } else
928 badness += BAD_SHARED_VOL;
929 return badness;
930}
931
932struct badness_table {
933 int no_primary_dac; /* no primary DAC */
934 int no_dac; /* no secondary DACs */
935 int shared_primary; /* primary DAC is shared with main output */
936 int shared_surr; /* secondary DAC shared with main or primary */
937 int shared_clfe; /* third DAC shared with main or primary */
938 int shared_surr_main; /* secondary DAC sahred with main/DAC0 */
939};
940
941static struct badness_table main_out_badness = {
942 .no_primary_dac = BAD_NO_PRIMARY_DAC,
943 .no_dac = BAD_NO_DAC,
944 .shared_primary = BAD_NO_PRIMARY_DAC,
945 .shared_surr = BAD_SHARED_SURROUND,
946 .shared_clfe = BAD_SHARED_CLFE,
947 .shared_surr_main = BAD_SHARED_SURROUND,
948};
949
950static struct badness_table extra_out_badness = {
951 .no_primary_dac = BAD_NO_DAC,
952 .no_dac = BAD_NO_DAC,
953 .shared_primary = BAD_NO_EXTRA_DAC,
954 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
955 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
956 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
957};
958
Takashi Iwai7385df62013-01-07 09:50:52 +0100959/* get the DAC of the primary output corresponding to the given array index */
960static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
961{
962 struct hda_gen_spec *spec = codec->spec;
963 struct auto_pin_cfg *cfg = &spec->autocfg;
964
965 if (cfg->line_outs > idx)
966 return spec->private_dac_nids[idx];
967 idx -= cfg->line_outs;
968 if (spec->multi_ios > idx)
969 return spec->multi_io[idx].dac;
970 return 0;
971}
972
973/* return the DAC if it's reachable, otherwise zero */
974static inline hda_nid_t try_dac(struct hda_codec *codec,
975 hda_nid_t dac, hda_nid_t pin)
976{
977 return is_reachable_path(codec, dac, pin) ? dac : 0;
978}
979
Takashi Iwai352f7f92012-12-19 12:52:06 +0100980/* try to assign DACs to pins and return the resultant badness */
981static int try_assign_dacs(struct hda_codec *codec, int num_outs,
982 const hda_nid_t *pins, hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +0100983 int *path_idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100984 const struct badness_table *bad)
985{
986 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100987 int i, j;
988 int badness = 0;
989 hda_nid_t dac;
990
991 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 return 0;
993
Takashi Iwai352f7f92012-12-19 12:52:06 +0100994 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100995 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100996 hda_nid_t pin = pins[i];
Takashi Iwai1e0b5282013-01-04 12:56:52 +0100997
Takashi Iwai0e614dd2013-01-07 15:11:44 +0100998 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
999 if (path) {
1000 badness += assign_out_path_ctls(codec, path);
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001001 continue;
1002 }
1003
1004 dacs[i] = look_for_dac(codec, pin, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001005 if (!dacs[i] && !i) {
Takashi Iwai980428c2013-01-09 09:28:20 +01001006 /* try to steal the DAC of surrounds for the front */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001007 for (j = 1; j < num_outs; j++) {
1008 if (is_reachable_path(codec, dacs[j], pin)) {
1009 dacs[0] = dacs[j];
1010 dacs[j] = 0;
Takashi Iwai980428c2013-01-09 09:28:20 +01001011 invalidate_nid_path(codec, path_idx[j]);
Takashi Iwai196c17662013-01-04 15:01:40 +01001012 path_idx[j] = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001013 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 }
1015 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001016 }
1017 dac = dacs[i];
1018 if (!dac) {
Takashi Iwai7385df62013-01-07 09:50:52 +01001019 if (num_outs > 2)
1020 dac = try_dac(codec, get_primary_out(codec, i), pin);
1021 if (!dac)
1022 dac = try_dac(codec, dacs[0], pin);
1023 if (!dac)
1024 dac = try_dac(codec, get_primary_out(codec, i), pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001025 if (dac) {
1026 if (!i)
1027 badness += bad->shared_primary;
1028 else if (i == 1)
1029 badness += bad->shared_surr;
1030 else
1031 badness += bad->shared_clfe;
1032 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1033 dac = spec->private_dac_nids[0];
1034 badness += bad->shared_surr_main;
1035 } else if (!i)
1036 badness += bad->no_primary_dac;
1037 else
1038 badness += bad->no_dac;
1039 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001040 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001041 if (!path && !i && spec->mixer_nid) {
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001042 /* try with aamix */
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001043 path = snd_hda_add_new_path(codec, dac, pin, 0);
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001044 }
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001045 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001046 dac = dacs[i] = 0;
Takashi Iwaie1284af2013-01-03 16:33:02 +01001047 else {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001048 print_nid_path("output", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001049 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001050 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001051 badness += assign_out_path_ctls(codec, path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001052 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001053 }
1054
1055 return badness;
1056}
1057
1058/* return NID if the given pin has only a single connection to a certain DAC */
1059static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1060{
1061 struct hda_gen_spec *spec = codec->spec;
1062 int i;
1063 hda_nid_t nid_found = 0;
1064
1065 for (i = 0; i < spec->num_all_dacs; i++) {
1066 hda_nid_t nid = spec->all_dacs[i];
1067 if (!nid || is_dac_already_used(codec, nid))
1068 continue;
1069 if (is_reachable_path(codec, nid, pin)) {
1070 if (nid_found)
1071 return 0;
1072 nid_found = nid;
1073 }
1074 }
1075 return nid_found;
1076}
1077
1078/* check whether the given pin can be a multi-io pin */
1079static bool can_be_multiio_pin(struct hda_codec *codec,
1080 unsigned int location, hda_nid_t nid)
1081{
1082 unsigned int defcfg, caps;
1083
1084 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1085 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1086 return false;
1087 if (location && get_defcfg_location(defcfg) != location)
1088 return false;
1089 caps = snd_hda_query_pin_caps(codec, nid);
1090 if (!(caps & AC_PINCAP_OUT))
1091 return false;
1092 return true;
1093}
1094
Takashi Iwaie22aab72013-01-04 14:50:04 +01001095/* count the number of input pins that are capable to be multi-io */
1096static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1097{
1098 struct hda_gen_spec *spec = codec->spec;
1099 struct auto_pin_cfg *cfg = &spec->autocfg;
1100 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1101 unsigned int location = get_defcfg_location(defcfg);
1102 int type, i;
1103 int num_pins = 0;
1104
1105 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1106 for (i = 0; i < cfg->num_inputs; i++) {
1107 if (cfg->inputs[i].type != type)
1108 continue;
1109 if (can_be_multiio_pin(codec, location,
1110 cfg->inputs[i].pin))
1111 num_pins++;
1112 }
1113 }
1114 return num_pins;
1115}
1116
Takashi Iwai352f7f92012-12-19 12:52:06 +01001117/*
1118 * multi-io helper
1119 *
1120 * When hardwired is set, try to fill ony hardwired pins, and returns
1121 * zero if any pins are filled, non-zero if nothing found.
1122 * When hardwired is off, try to fill possible input pins, and returns
1123 * the badness value.
1124 */
1125static int fill_multi_ios(struct hda_codec *codec,
1126 hda_nid_t reference_pin,
Takashi Iwaie22aab72013-01-04 14:50:04 +01001127 bool hardwired)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001128{
1129 struct hda_gen_spec *spec = codec->spec;
1130 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie22aab72013-01-04 14:50:04 +01001131 int type, i, j, num_pins, old_pins;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001132 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1133 unsigned int location = get_defcfg_location(defcfg);
1134 int badness = 0;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001135 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001136
1137 old_pins = spec->multi_ios;
1138 if (old_pins >= 2)
1139 goto end_fill;
1140
Takashi Iwaie22aab72013-01-04 14:50:04 +01001141 num_pins = count_multiio_pins(codec, reference_pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001142 if (num_pins < 2)
1143 goto end_fill;
1144
Takashi Iwai352f7f92012-12-19 12:52:06 +01001145 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1146 for (i = 0; i < cfg->num_inputs; i++) {
1147 hda_nid_t nid = cfg->inputs[i].pin;
1148 hda_nid_t dac = 0;
1149
1150 if (cfg->inputs[i].type != type)
1151 continue;
1152 if (!can_be_multiio_pin(codec, location, nid))
1153 continue;
1154 for (j = 0; j < spec->multi_ios; j++) {
1155 if (nid == spec->multi_io[j].pin)
1156 break;
1157 }
1158 if (j < spec->multi_ios)
1159 continue;
1160
Takashi Iwai352f7f92012-12-19 12:52:06 +01001161 if (hardwired)
1162 dac = get_dac_if_single(codec, nid);
1163 else if (!dac)
1164 dac = look_for_dac(codec, nid, false);
1165 if (!dac) {
1166 badness++;
1167 continue;
1168 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001169 path = snd_hda_add_new_path(codec, dac, nid,
1170 -spec->mixer_nid);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001171 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001172 badness++;
1173 continue;
1174 }
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001175 print_nid_path("multiio", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001176 spec->multi_io[spec->multi_ios].pin = nid;
1177 spec->multi_io[spec->multi_ios].dac = dac;
Takashi Iwai196c17662013-01-04 15:01:40 +01001178 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1179 snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001180 spec->multi_ios++;
1181 if (spec->multi_ios >= 2)
1182 break;
1183 }
1184 }
1185 end_fill:
1186 if (badness)
1187 badness = BAD_MULTI_IO;
1188 if (old_pins == spec->multi_ios) {
1189 if (hardwired)
1190 return 1; /* nothing found */
1191 else
1192 return badness; /* no badness if nothing found */
1193 }
1194 if (!hardwired && spec->multi_ios < 2) {
1195 /* cancel newly assigned paths */
1196 spec->paths.used -= spec->multi_ios - old_pins;
1197 spec->multi_ios = old_pins;
1198 return badness;
1199 }
1200
1201 /* assign volume and mute controls */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001202 for (i = old_pins; i < spec->multi_ios; i++) {
1203 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1204 badness += assign_out_path_ctls(codec, path);
1205 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001206
1207 return badness;
1208}
1209
1210/* map DACs for all pins in the list if they are single connections */
1211static bool map_singles(struct hda_codec *codec, int outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001212 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001213{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001214 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001215 int i;
1216 bool found = false;
1217 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001218 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001219 hda_nid_t dac;
1220 if (dacs[i])
1221 continue;
1222 dac = get_dac_if_single(codec, pins[i]);
1223 if (!dac)
1224 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001225 path = snd_hda_add_new_path(codec, dac, pins[i],
1226 -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001227 if (!path && !i && spec->mixer_nid)
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001228 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001229 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001230 dacs[i] = dac;
1231 found = true;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001232 print_nid_path("output", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001233 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001234 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001235 }
1236 }
1237 return found;
1238}
1239
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001240/* create a new path including aamix if available, and return its index */
1241static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1242{
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001243 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001244 struct nid_path *path;
1245
1246 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001247 if (!path || !path->depth ||
1248 is_nid_contained(path, spec->mixer_nid))
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001249 return 0;
1250 path = snd_hda_add_new_path(codec, path->path[0],
1251 path->path[path->depth - 1],
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001252 spec->mixer_nid);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001253 if (!path)
1254 return 0;
1255 print_nid_path("output-aamix", path);
1256 path->active = false; /* unused as default */
1257 return snd_hda_get_path_idx(codec, path);
1258}
1259
Takashi Iwaia07a9492013-01-07 16:44:06 +01001260/* fill the empty entries in the dac array for speaker/hp with the
1261 * shared dac pointed by the paths
1262 */
1263static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1264 hda_nid_t *dacs, int *path_idx)
1265{
1266 struct nid_path *path;
1267 int i;
1268
1269 for (i = 0; i < num_outs; i++) {
1270 if (dacs[i])
1271 continue;
1272 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1273 if (!path)
1274 continue;
1275 dacs[i] = path->path[0];
1276 }
1277}
1278
Takashi Iwai352f7f92012-12-19 12:52:06 +01001279/* fill in the dac_nids table from the parsed pin configuration */
1280static int fill_and_eval_dacs(struct hda_codec *codec,
1281 bool fill_hardwired,
1282 bool fill_mio_first)
1283{
1284 struct hda_gen_spec *spec = codec->spec;
1285 struct auto_pin_cfg *cfg = &spec->autocfg;
1286 int i, err, badness;
1287
1288 /* set num_dacs once to full for look_for_dac() */
1289 spec->multiout.num_dacs = cfg->line_outs;
1290 spec->multiout.dac_nids = spec->private_dac_nids;
1291 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1292 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1293 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1294 spec->multi_ios = 0;
1295 snd_array_free(&spec->paths);
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001296
1297 /* clear path indices */
1298 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1299 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1300 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1301 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1302 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
Takashi Iwaic697b712013-01-07 17:09:26 +01001303 memset(spec->input_paths, 0, sizeof(spec->input_paths));
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001304 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1305 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1306
Takashi Iwai352f7f92012-12-19 12:52:06 +01001307 badness = 0;
1308
1309 /* fill hard-wired DACs first */
1310 if (fill_hardwired) {
1311 bool mapped;
1312 do {
1313 mapped = map_singles(codec, cfg->line_outs,
1314 cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001315 spec->private_dac_nids,
1316 spec->out_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001317 mapped |= map_singles(codec, cfg->hp_outs,
1318 cfg->hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001319 spec->multiout.hp_out_nid,
1320 spec->hp_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001321 mapped |= map_singles(codec, cfg->speaker_outs,
1322 cfg->speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001323 spec->multiout.extra_out_nid,
1324 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001325 if (fill_mio_first && cfg->line_outs == 1 &&
1326 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001327 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001328 if (!err)
1329 mapped = true;
1330 }
1331 } while (mapped);
1332 }
1333
1334 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001335 spec->private_dac_nids, spec->out_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001336 &main_out_badness);
1337
Takashi Iwai352f7f92012-12-19 12:52:06 +01001338 if (fill_mio_first &&
1339 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1340 /* try to fill multi-io first */
Takashi Iwaie22aab72013-01-04 14:50:04 +01001341 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001342 if (err < 0)
1343 return err;
1344 /* we don't count badness at this stage yet */
1345 }
1346
1347 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1348 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1349 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001350 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001351 &extra_out_badness);
1352 if (err < 0)
1353 return err;
1354 badness += err;
1355 }
1356 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1357 err = try_assign_dacs(codec, cfg->speaker_outs,
1358 cfg->speaker_pins,
1359 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001360 spec->speaker_paths,
1361 &extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001362 if (err < 0)
1363 return err;
1364 badness += err;
1365 }
1366 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001367 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001368 if (err < 0)
1369 return err;
1370 badness += err;
1371 }
Takashi Iwaie22aab72013-01-04 14:50:04 +01001372
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001373 if (spec->mixer_nid) {
1374 spec->aamix_out_paths[0] =
1375 check_aamix_out_path(codec, spec->out_paths[0]);
1376 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1377 spec->aamix_out_paths[1] =
1378 check_aamix_out_path(codec, spec->hp_paths[0]);
1379 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1380 spec->aamix_out_paths[2] =
1381 check_aamix_out_path(codec, spec->speaker_paths[0]);
1382 }
1383
Takashi Iwaie22aab72013-01-04 14:50:04 +01001384 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1385 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1386 spec->multi_ios = 1; /* give badness */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001387
Takashi Iwaia07a9492013-01-07 16:44:06 +01001388 /* re-count num_dacs and squash invalid entries */
1389 spec->multiout.num_dacs = 0;
1390 for (i = 0; i < cfg->line_outs; i++) {
1391 if (spec->private_dac_nids[i])
1392 spec->multiout.num_dacs++;
1393 else {
1394 memmove(spec->private_dac_nids + i,
1395 spec->private_dac_nids + i + 1,
1396 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1397 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1398 }
1399 }
1400
1401 spec->ext_channel_count = spec->min_channel_count =
1402 spec->multiout.num_dacs;
1403
Takashi Iwai352f7f92012-12-19 12:52:06 +01001404 if (spec->multi_ios == 2) {
1405 for (i = 0; i < 2; i++)
1406 spec->private_dac_nids[spec->multiout.num_dacs++] =
1407 spec->multi_io[i].dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001408 } else if (spec->multi_ios) {
1409 spec->multi_ios = 0;
1410 badness += BAD_MULTI_IO;
1411 }
1412
Takashi Iwaia07a9492013-01-07 16:44:06 +01001413 /* re-fill the shared DAC for speaker / headphone */
1414 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1415 refill_shared_dacs(codec, cfg->hp_outs,
1416 spec->multiout.hp_out_nid,
1417 spec->hp_paths);
1418 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1419 refill_shared_dacs(codec, cfg->speaker_outs,
1420 spec->multiout.extra_out_nid,
1421 spec->speaker_paths);
1422
Takashi Iwai2c12c302013-01-10 09:33:29 +01001423 /* set initial pinctl targets */
1424 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins,
1425 cfg->line_out_type == AUTO_PIN_HP_OUT ? PIN_HP : PIN_OUT);
1426 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1427 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
1428 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1429 set_pin_targets(codec, cfg->speaker_outs,
1430 cfg->speaker_pins, PIN_OUT);
1431
Takashi Iwai352f7f92012-12-19 12:52:06 +01001432 return badness;
1433}
1434
1435#define DEBUG_BADNESS
1436
1437#ifdef DEBUG_BADNESS
1438#define debug_badness snd_printdd
1439#else
1440#define debug_badness(...)
1441#endif
1442
1443static void debug_show_configs(struct hda_gen_spec *spec, struct auto_pin_cfg *cfg)
1444{
1445 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1446 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001447 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001448 spec->multiout.dac_nids[0],
1449 spec->multiout.dac_nids[1],
1450 spec->multiout.dac_nids[2],
1451 spec->multiout.dac_nids[3]);
1452 if (spec->multi_ios > 0)
1453 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1454 spec->multi_ios,
1455 spec->multi_io[0].pin, spec->multi_io[1].pin,
1456 spec->multi_io[0].dac, spec->multi_io[1].dac);
1457 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1458 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001459 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001460 spec->multiout.hp_out_nid[0],
1461 spec->multiout.hp_out_nid[1],
1462 spec->multiout.hp_out_nid[2],
1463 spec->multiout.hp_out_nid[3]);
1464 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1465 cfg->speaker_pins[0], cfg->speaker_pins[1],
1466 cfg->speaker_pins[2], cfg->speaker_pins[3],
1467 spec->multiout.extra_out_nid[0],
1468 spec->multiout.extra_out_nid[1],
1469 spec->multiout.extra_out_nid[2],
1470 spec->multiout.extra_out_nid[3]);
1471}
1472
1473/* find all available DACs of the codec */
1474static void fill_all_dac_nids(struct hda_codec *codec)
1475{
1476 struct hda_gen_spec *spec = codec->spec;
1477 int i;
1478 hda_nid_t nid = codec->start_nid;
1479
1480 spec->num_all_dacs = 0;
1481 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1482 for (i = 0; i < codec->num_nodes; i++, nid++) {
1483 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1484 continue;
1485 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1486 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1487 break;
1488 }
1489 spec->all_dacs[spec->num_all_dacs++] = nid;
1490 }
1491}
1492
1493static int parse_output_paths(struct hda_codec *codec)
1494{
1495 struct hda_gen_spec *spec = codec->spec;
1496 struct auto_pin_cfg *cfg = &spec->autocfg;
1497 struct auto_pin_cfg *best_cfg;
1498 int best_badness = INT_MAX;
1499 int badness;
1500 bool fill_hardwired = true, fill_mio_first = true;
1501 bool best_wired = true, best_mio = true;
1502 bool hp_spk_swapped = false;
1503
1504 fill_all_dac_nids(codec);
1505
1506 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1507 if (!best_cfg)
1508 return -ENOMEM;
1509 *best_cfg = *cfg;
1510
1511 for (;;) {
1512 badness = fill_and_eval_dacs(codec, fill_hardwired,
1513 fill_mio_first);
1514 if (badness < 0) {
1515 kfree(best_cfg);
1516 return badness;
1517 }
1518 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1519 cfg->line_out_type, fill_hardwired, fill_mio_first,
1520 badness);
1521 debug_show_configs(spec, cfg);
1522 if (badness < best_badness) {
1523 best_badness = badness;
1524 *best_cfg = *cfg;
1525 best_wired = fill_hardwired;
1526 best_mio = fill_mio_first;
1527 }
1528 if (!badness)
1529 break;
1530 fill_mio_first = !fill_mio_first;
1531 if (!fill_mio_first)
1532 continue;
1533 fill_hardwired = !fill_hardwired;
1534 if (!fill_hardwired)
1535 continue;
1536 if (hp_spk_swapped)
1537 break;
1538 hp_spk_swapped = true;
1539 if (cfg->speaker_outs > 0 &&
1540 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1541 cfg->hp_outs = cfg->line_outs;
1542 memcpy(cfg->hp_pins, cfg->line_out_pins,
1543 sizeof(cfg->hp_pins));
1544 cfg->line_outs = cfg->speaker_outs;
1545 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1546 sizeof(cfg->speaker_pins));
1547 cfg->speaker_outs = 0;
1548 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1549 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1550 fill_hardwired = true;
1551 continue;
1552 }
1553 if (cfg->hp_outs > 0 &&
1554 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1555 cfg->speaker_outs = cfg->line_outs;
1556 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1557 sizeof(cfg->speaker_pins));
1558 cfg->line_outs = cfg->hp_outs;
1559 memcpy(cfg->line_out_pins, cfg->hp_pins,
1560 sizeof(cfg->hp_pins));
1561 cfg->hp_outs = 0;
1562 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1563 cfg->line_out_type = AUTO_PIN_HP_OUT;
1564 fill_hardwired = true;
1565 continue;
1566 }
1567 break;
1568 }
1569
1570 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001571 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001572 *cfg = *best_cfg;
1573 fill_and_eval_dacs(codec, best_wired, best_mio);
1574 }
1575 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1576 cfg->line_out_type, best_wired, best_mio);
1577 debug_show_configs(spec, cfg);
1578
1579 if (cfg->line_out_pins[0]) {
1580 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01001581 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001582 if (path)
1583 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
1584 }
1585
1586 kfree(best_cfg);
1587 return 0;
1588}
1589
1590/* add playback controls from the parsed DAC table */
1591static int create_multi_out_ctls(struct hda_codec *codec,
1592 const struct auto_pin_cfg *cfg)
1593{
1594 struct hda_gen_spec *spec = codec->spec;
1595 int i, err, noutputs;
1596
1597 noutputs = cfg->line_outs;
1598 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1599 noutputs += spec->multi_ios;
1600
1601 for (i = 0; i < noutputs; i++) {
1602 const char *name;
1603 int index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001604 struct nid_path *path;
1605
Takashi Iwai352f7f92012-12-19 12:52:06 +01001606 if (i >= cfg->line_outs) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001607 index = 0;
1608 name = channel_name[i];
1609 } else {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001610 name = get_line_out_pfx(spec, i, true, &index);
1611 }
1612
Takashi Iwai196c17662013-01-04 15:01:40 +01001613 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001614 if (!path)
1615 continue;
1616 if (!name || !strcmp(name, "CLFE")) {
1617 /* Center/LFE */
1618 err = add_vol_ctl(codec, "Center", 0, 1, path);
1619 if (err < 0)
1620 return err;
1621 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1622 if (err < 0)
1623 return err;
1624 err = add_sw_ctl(codec, "Center", 0, 1, path);
1625 if (err < 0)
1626 return err;
1627 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1628 if (err < 0)
1629 return err;
1630 } else {
1631 err = add_stereo_vol(codec, name, index, path);
1632 if (err < 0)
1633 return err;
1634 err = add_stereo_sw(codec, name, index, path);
1635 if (err < 0)
1636 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 }
1638 }
1639 return 0;
1640}
1641
Takashi Iwaic2c80382013-01-07 10:33:57 +01001642static int create_extra_out(struct hda_codec *codec, int path_idx,
Takashi Iwai196c17662013-01-04 15:01:40 +01001643 const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001645 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 int err;
1647
Takashi Iwai196c17662013-01-04 15:01:40 +01001648 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001649 if (!path)
1650 return 0;
Takashi Iwaic2c80382013-01-07 10:33:57 +01001651 err = add_stereo_vol(codec, pfx, cidx, path);
1652 if (err < 0)
1653 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001654 err = add_stereo_sw(codec, pfx, cidx, path);
1655 if (err < 0)
1656 return err;
1657 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658}
1659
Takashi Iwai352f7f92012-12-19 12:52:06 +01001660/* add playback controls for speaker and HP outputs */
1661static int create_extra_outs(struct hda_codec *codec, int num_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001662 const int *paths, const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663{
Takashi Iwaic2c80382013-01-07 10:33:57 +01001664 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001665
1666 for (i = 0; i < num_pins; i++) {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001667 const char *name;
1668 char tmp[44];
1669 int err, idx = 0;
1670
1671 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
1672 name = "Bass Speaker";
1673 else if (num_pins >= 3) {
1674 snprintf(tmp, sizeof(tmp), "%s %s",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001675 pfx, channel_name[i]);
Takashi Iwaic2c80382013-01-07 10:33:57 +01001676 name = tmp;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001677 } else {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001678 name = pfx;
1679 idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 }
Takashi Iwaic2c80382013-01-07 10:33:57 +01001681 err = create_extra_out(codec, paths[i], name, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001682 if (err < 0)
1683 return err;
1684 }
1685 return 0;
1686}
Takashi Iwai97ec5582006-03-21 11:29:07 +01001687
Takashi Iwai352f7f92012-12-19 12:52:06 +01001688static int create_hp_out_ctls(struct hda_codec *codec)
1689{
1690 struct hda_gen_spec *spec = codec->spec;
1691 return create_extra_outs(codec, spec->autocfg.hp_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001692 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001693 "Headphone");
1694}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
Takashi Iwai352f7f92012-12-19 12:52:06 +01001696static int create_speaker_out_ctls(struct hda_codec *codec)
1697{
1698 struct hda_gen_spec *spec = codec->spec;
1699 return create_extra_outs(codec, spec->autocfg.speaker_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001700 spec->speaker_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001701 "Speaker");
1702}
1703
1704/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001705 * independent HP controls
1706 */
1707
1708static int indep_hp_info(struct snd_kcontrol *kcontrol,
1709 struct snd_ctl_elem_info *uinfo)
1710{
1711 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
1712}
1713
1714static int indep_hp_get(struct snd_kcontrol *kcontrol,
1715 struct snd_ctl_elem_value *ucontrol)
1716{
1717 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1718 struct hda_gen_spec *spec = codec->spec;
1719 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
1720 return 0;
1721}
1722
1723static int indep_hp_put(struct snd_kcontrol *kcontrol,
1724 struct snd_ctl_elem_value *ucontrol)
1725{
1726 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1727 struct hda_gen_spec *spec = codec->spec;
1728 unsigned int select = ucontrol->value.enumerated.item[0];
1729 int ret = 0;
1730
1731 mutex_lock(&spec->pcm_mutex);
1732 if (spec->active_streams) {
1733 ret = -EBUSY;
1734 goto unlock;
1735 }
1736
1737 if (spec->indep_hp_enabled != select) {
1738 spec->indep_hp_enabled = select;
1739 if (spec->indep_hp_enabled)
1740 spec->multiout.hp_out_nid[0] = 0;
1741 else
1742 spec->multiout.hp_out_nid[0] = spec->alt_dac_nid;
1743 ret = 1;
1744 }
1745 unlock:
1746 mutex_unlock(&spec->pcm_mutex);
1747 return ret;
1748}
1749
1750static const struct snd_kcontrol_new indep_hp_ctl = {
1751 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1752 .name = "Independent HP",
1753 .info = indep_hp_info,
1754 .get = indep_hp_get,
1755 .put = indep_hp_put,
1756};
1757
1758
1759static int create_indep_hp_ctls(struct hda_codec *codec)
1760{
1761 struct hda_gen_spec *spec = codec->spec;
1762
1763 if (!spec->indep_hp)
1764 return 0;
1765 if (!spec->multiout.hp_out_nid[0]) {
1766 spec->indep_hp = 0;
1767 return 0;
1768 }
1769
1770 spec->indep_hp_enabled = false;
1771 spec->alt_dac_nid = spec->multiout.hp_out_nid[0];
1772 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
1773 return -ENOMEM;
1774 return 0;
1775}
1776
1777/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01001778 * channel mode enum control
1779 */
1780
1781static int ch_mode_info(struct snd_kcontrol *kcontrol,
1782 struct snd_ctl_elem_info *uinfo)
1783{
1784 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1785 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001786 int chs;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001787
1788 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1789 uinfo->count = 1;
1790 uinfo->value.enumerated.items = spec->multi_ios + 1;
1791 if (uinfo->value.enumerated.item > spec->multi_ios)
1792 uinfo->value.enumerated.item = spec->multi_ios;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001793 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
1794 sprintf(uinfo->value.enumerated.name, "%dch", chs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001795 return 0;
1796}
1797
1798static int ch_mode_get(struct snd_kcontrol *kcontrol,
1799 struct snd_ctl_elem_value *ucontrol)
1800{
1801 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1802 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001803 ucontrol->value.enumerated.item[0] =
1804 (spec->ext_channel_count - spec->min_channel_count) / 2;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001805 return 0;
1806}
1807
Takashi Iwai196c17662013-01-04 15:01:40 +01001808static inline struct nid_path *
1809get_multiio_path(struct hda_codec *codec, int idx)
1810{
1811 struct hda_gen_spec *spec = codec->spec;
1812 return snd_hda_get_path_from_idx(codec,
1813 spec->out_paths[spec->autocfg.line_outs + idx]);
1814}
1815
Takashi Iwai352f7f92012-12-19 12:52:06 +01001816static int set_multi_io(struct hda_codec *codec, int idx, bool output)
1817{
1818 struct hda_gen_spec *spec = codec->spec;
1819 hda_nid_t nid = spec->multi_io[idx].pin;
1820 struct nid_path *path;
1821
Takashi Iwai196c17662013-01-04 15:01:40 +01001822 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001823 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001825
1826 if (path->active == output)
1827 return 0;
1828
1829 if (output) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01001830 set_pin_target(codec, nid, PIN_OUT, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001831 snd_hda_activate_path(codec, path, true, true);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01001832 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001833 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01001834 set_pin_eapd(codec, nid, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001835 snd_hda_activate_path(codec, path, false, true);
Takashi Iwai2c12c302013-01-10 09:33:29 +01001836 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 }
Takashi Iwaia365fed2013-01-10 16:10:06 +01001838
1839 /* update jack retasking in case it modifies any of them */
1840 snd_hda_gen_hp_automute(codec, NULL);
1841 snd_hda_gen_line_automute(codec, NULL);
1842 snd_hda_gen_mic_autoswitch(codec, NULL);
1843
Takashi Iwai352f7f92012-12-19 12:52:06 +01001844 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845}
1846
Takashi Iwai352f7f92012-12-19 12:52:06 +01001847static int ch_mode_put(struct snd_kcontrol *kcontrol,
1848 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001850 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1851 struct hda_gen_spec *spec = codec->spec;
1852 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853
Takashi Iwai352f7f92012-12-19 12:52:06 +01001854 ch = ucontrol->value.enumerated.item[0];
1855 if (ch < 0 || ch > spec->multi_ios)
1856 return -EINVAL;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001857 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001858 return 0;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001859 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001860 for (i = 0; i < spec->multi_ios; i++)
1861 set_multi_io(codec, i, i < ch);
1862 spec->multiout.max_channels = max(spec->ext_channel_count,
1863 spec->const_channel_count);
1864 if (spec->need_dac_fix)
1865 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 return 1;
1867}
1868
Takashi Iwai352f7f92012-12-19 12:52:06 +01001869static const struct snd_kcontrol_new channel_mode_enum = {
1870 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1871 .name = "Channel Mode",
1872 .info = ch_mode_info,
1873 .get = ch_mode_get,
1874 .put = ch_mode_put,
1875};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
Takashi Iwai352f7f92012-12-19 12:52:06 +01001877static int create_multi_channel_mode(struct hda_codec *codec)
1878{
1879 struct hda_gen_spec *spec = codec->spec;
1880
1881 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01001882 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01001883 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 return 0;
1886}
1887
Takashi Iwai352f7f92012-12-19 12:52:06 +01001888/*
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001889 * aamix loopback enable/disable switch
1890 */
1891
1892#define loopback_mixing_info indep_hp_info
1893
1894static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
1895 struct snd_ctl_elem_value *ucontrol)
1896{
1897 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1898 struct hda_gen_spec *spec = codec->spec;
1899 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
1900 return 0;
1901}
1902
1903static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
1904 int nomix_path_idx, int mix_path_idx)
1905{
1906 struct nid_path *nomix_path, *mix_path;
1907
1908 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
1909 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
1910 if (!nomix_path || !mix_path)
1911 return;
1912 if (do_mix) {
1913 snd_hda_activate_path(codec, nomix_path, false, true);
1914 snd_hda_activate_path(codec, mix_path, true, true);
1915 } else {
1916 snd_hda_activate_path(codec, mix_path, false, true);
1917 snd_hda_activate_path(codec, nomix_path, true, true);
1918 }
1919}
1920
1921static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
1922 struct snd_ctl_elem_value *ucontrol)
1923{
1924 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1925 struct hda_gen_spec *spec = codec->spec;
1926 unsigned int val = ucontrol->value.enumerated.item[0];
1927
1928 if (val == spec->aamix_mode)
1929 return 0;
1930 spec->aamix_mode = val;
1931 update_aamix_paths(codec, val, spec->out_paths[0],
1932 spec->aamix_out_paths[0]);
1933 update_aamix_paths(codec, val, spec->hp_paths[0],
1934 spec->aamix_out_paths[1]);
1935 update_aamix_paths(codec, val, spec->speaker_paths[0],
1936 spec->aamix_out_paths[2]);
1937 return 1;
1938}
1939
1940static const struct snd_kcontrol_new loopback_mixing_enum = {
1941 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1942 .name = "Loopback Mixing",
1943 .info = loopback_mixing_info,
1944 .get = loopback_mixing_get,
1945 .put = loopback_mixing_put,
1946};
1947
1948static int create_loopback_mixing_ctl(struct hda_codec *codec)
1949{
1950 struct hda_gen_spec *spec = codec->spec;
1951
1952 if (!spec->mixer_nid)
1953 return 0;
1954 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
1955 spec->aamix_out_paths[2]))
1956 return 0;
1957 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
1958 return -ENOMEM;
1959 return 0;
1960}
1961
1962/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01001963 * shared headphone/mic handling
1964 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02001965
Takashi Iwai352f7f92012-12-19 12:52:06 +01001966static void call_update_outputs(struct hda_codec *codec);
1967
1968/* for shared I/O, change the pin-control accordingly */
1969static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic)
1970{
1971 struct hda_gen_spec *spec = codec->spec;
1972 unsigned int val;
1973 hda_nid_t pin = spec->autocfg.inputs[1].pin;
1974 /* NOTE: this assumes that there are only two inputs, the
1975 * first is the real internal mic and the second is HP/mic jack.
1976 */
1977
1978 val = snd_hda_get_default_vref(codec, pin);
1979
1980 /* This pin does not have vref caps - let's enable vref on pin 0x18
1981 instead, as suggested by Realtek */
1982 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
1983 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
1984 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
1985 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01001986 snd_hda_set_pin_ctl_cache(codec, vref_pin,
1987 PIN_IN | (set_as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02001988 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001989
1990 val = set_as_mic ? val | PIN_IN : PIN_HP;
Takashi Iwai2c12c302013-01-10 09:33:29 +01001991 set_pin_target(codec, pin, val, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001992
1993 spec->automute_speaker = !set_as_mic;
1994 call_update_outputs(codec);
1995}
1996
1997/* create a shared input with the headphone out */
1998static int create_shared_input(struct hda_codec *codec)
1999{
2000 struct hda_gen_spec *spec = codec->spec;
2001 struct auto_pin_cfg *cfg = &spec->autocfg;
2002 unsigned int defcfg;
2003 hda_nid_t nid;
2004
2005 /* only one internal input pin? */
2006 if (cfg->num_inputs != 1)
2007 return 0;
2008 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2009 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2010 return 0;
2011
2012 if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
2013 nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
2014 else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
2015 nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
2016 else
2017 return 0; /* both not available */
2018
2019 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2020 return 0; /* no input */
2021
2022 cfg->inputs[1].pin = nid;
2023 cfg->inputs[1].type = AUTO_PIN_MIC;
2024 cfg->num_inputs = 2;
2025 spec->shared_mic_hp = 1;
2026 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
2027 return 0;
2028}
2029
Takashi Iwai978e77e2013-01-10 16:57:58 +01002030/*
2031 * output jack mode
2032 */
2033static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2034 struct snd_ctl_elem_info *uinfo)
2035{
2036 static const char * const texts[] = {
2037 "Line Out", "Headphone Out",
2038 };
2039 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, texts);
2040}
2041
2042static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2043 struct snd_ctl_elem_value *ucontrol)
2044{
2045 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2046 hda_nid_t nid = kcontrol->private_value;
2047 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2048 ucontrol->value.enumerated.item[0] = 1;
2049 else
2050 ucontrol->value.enumerated.item[0] = 0;
2051 return 0;
2052}
2053
2054static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2055 struct snd_ctl_elem_value *ucontrol)
2056{
2057 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2058 hda_nid_t nid = kcontrol->private_value;
2059 unsigned int val;
2060
2061 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2062 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2063 return 0;
2064 snd_hda_set_pin_ctl_cache(codec, nid, val);
2065 return 1;
2066}
2067
2068static const struct snd_kcontrol_new out_jack_mode_enum = {
2069 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2070 .info = out_jack_mode_info,
2071 .get = out_jack_mode_get,
2072 .put = out_jack_mode_put,
2073};
2074
2075static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2076{
2077 struct hda_gen_spec *spec = codec->spec;
2078 int i;
2079
2080 for (i = 0; i < spec->kctls.used; i++) {
2081 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2082 if (!strcmp(kctl->name, name) && kctl->index == idx)
2083 return true;
2084 }
2085 return false;
2086}
2087
2088static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2089 char *name, size_t name_len)
2090{
2091 struct hda_gen_spec *spec = codec->spec;
2092 int idx = 0;
2093
2094 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2095 strlcat(name, " Jack Mode", name_len);
2096
2097 for (; find_kctl_name(codec, name, idx); idx++)
2098 ;
2099}
2100
2101static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2102 hda_nid_t *pins)
2103{
2104 struct hda_gen_spec *spec = codec->spec;
2105 int i;
2106
2107 for (i = 0; i < num_pins; i++) {
2108 hda_nid_t pin = pins[i];
2109 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2110 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV)) {
2111 struct snd_kcontrol_new *knew;
2112 char name[44];
2113 get_jack_mode_name(codec, pin, name, sizeof(name));
2114 knew = snd_hda_gen_add_kctl(spec, name,
2115 &out_jack_mode_enum);
2116 if (!knew)
2117 return -ENOMEM;
2118 knew->private_value = pin;
2119 }
2120 }
2121
2122 return 0;
2123}
2124
Takashi Iwai352f7f92012-12-19 12:52:06 +01002125
2126/*
2127 * Parse input paths
2128 */
2129
2130#ifdef CONFIG_PM
2131/* add the powersave loopback-list entry */
2132static void add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
2133{
2134 struct hda_amp_list *list;
2135
2136 if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
2137 return;
2138 list = spec->loopback_list + spec->num_loopbacks;
2139 list->nid = mix;
2140 list->dir = HDA_INPUT;
2141 list->idx = idx;
2142 spec->num_loopbacks++;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002143 spec->loopback.amplist = spec->loopback_list;
2144}
2145#else
Takashi Iwai352f7f92012-12-19 12:52:06 +01002146#define add_loopback_list(spec, mix, idx) /* NOP */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002147#endif
2148
Takashi Iwai352f7f92012-12-19 12:52:06 +01002149/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01002150static int new_analog_input(struct hda_codec *codec, int input_idx,
2151 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002152 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002154 struct hda_gen_spec *spec = codec->spec;
2155 struct nid_path *path;
2156 unsigned int val;
2157 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158
Takashi Iwai352f7f92012-12-19 12:52:06 +01002159 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
2160 !nid_has_mute(codec, mix_nid, HDA_INPUT))
2161 return 0; /* no need for analog loopback */
2162
Takashi Iwai3ca529d2013-01-07 17:25:08 +01002163 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002164 if (!path)
2165 return -EINVAL;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002166 print_nid_path("loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01002167 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002168
2169 idx = path->idx[path->depth - 1];
2170 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
2171 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2172 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002173 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002175 path->ctls[NID_PATH_VOL_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 }
2177
Takashi Iwai352f7f92012-12-19 12:52:06 +01002178 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
2179 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2180 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002181 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002183 path->ctls[NID_PATH_MUTE_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 }
2185
Takashi Iwai352f7f92012-12-19 12:52:06 +01002186 path->active = true;
2187 add_loopback_list(spec, mix_nid, idx);
2188 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189}
2190
Takashi Iwai352f7f92012-12-19 12:52:06 +01002191static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002193 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
2194 return (pincap & AC_PINCAP_IN) != 0;
2195}
2196
2197/* Parse the codec tree and retrieve ADCs */
2198static int fill_adc_nids(struct hda_codec *codec)
2199{
2200 struct hda_gen_spec *spec = codec->spec;
2201 hda_nid_t nid;
2202 hda_nid_t *adc_nids = spec->adc_nids;
2203 int max_nums = ARRAY_SIZE(spec->adc_nids);
2204 int i, nums = 0;
2205
2206 nid = codec->start_nid;
2207 for (i = 0; i < codec->num_nodes; i++, nid++) {
2208 unsigned int caps = get_wcaps(codec, nid);
2209 int type = get_wcaps_type(caps);
2210
2211 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2212 continue;
2213 adc_nids[nums] = nid;
2214 if (++nums >= max_nums)
2215 break;
2216 }
2217 spec->num_adc_nids = nums;
2218 return nums;
2219}
2220
2221/* filter out invalid adc_nids that don't give all active input pins;
2222 * if needed, check whether dynamic ADC-switching is available
2223 */
2224static int check_dyn_adc_switch(struct hda_codec *codec)
2225{
2226 struct hda_gen_spec *spec = codec->spec;
2227 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002228 unsigned int ok_bits;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002229 int i, n, nums;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002230
2231 again:
2232 nums = 0;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002233 ok_bits = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002234 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002235 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002236 if (!spec->input_paths[i][n])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002237 break;
2238 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002239 if (i >= imux->num_items) {
2240 ok_bits |= (1 << n);
2241 nums++;
2242 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002243 }
2244
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002245 if (!ok_bits) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002246 if (spec->shared_mic_hp) {
2247 spec->shared_mic_hp = 0;
2248 imux->num_items = 1;
2249 goto again;
2250 }
2251
2252 /* check whether ADC-switch is possible */
2253 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002254 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002255 if (spec->input_paths[i][n]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002256 spec->dyn_adc_idx[i] = n;
2257 break;
2258 }
2259 }
2260 }
2261
2262 snd_printdd("hda-codec: enabling ADC switching\n");
2263 spec->dyn_adc_switch = 1;
2264 } else if (nums != spec->num_adc_nids) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002265 /* shrink the invalid adcs and input paths */
2266 nums = 0;
2267 for (n = 0; n < spec->num_adc_nids; n++) {
2268 if (!(ok_bits & (1 << n)))
2269 continue;
2270 if (n != nums) {
2271 spec->adc_nids[nums] = spec->adc_nids[n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002272 for (i = 0; i < imux->num_items; i++) {
2273 invalidate_nid_path(codec,
2274 spec->input_paths[i][nums]);
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002275 spec->input_paths[i][nums] =
2276 spec->input_paths[i][n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002277 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002278 }
2279 nums++;
2280 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002281 spec->num_adc_nids = nums;
2282 }
2283
2284 if (imux->num_items == 1 || spec->shared_mic_hp) {
2285 snd_printdd("hda-codec: reducing to a single ADC\n");
2286 spec->num_adc_nids = 1; /* reduce to a single ADC */
2287 }
2288
2289 /* single index for individual volumes ctls */
2290 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
2291 spec->num_adc_nids = 1;
2292
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 return 0;
2294}
2295
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002296/* parse capture source paths from the given pin and create imux items */
2297static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
2298 int num_adcs, const char *label, int anchor)
2299{
2300 struct hda_gen_spec *spec = codec->spec;
2301 struct hda_input_mux *imux = &spec->input_mux;
2302 int imux_idx = imux->num_items;
2303 bool imux_added = false;
2304 int c;
2305
2306 for (c = 0; c < num_adcs; c++) {
2307 struct nid_path *path;
2308 hda_nid_t adc = spec->adc_nids[c];
2309
2310 if (!is_reachable_path(codec, pin, adc))
2311 continue;
2312 path = snd_hda_add_new_path(codec, pin, adc, anchor);
2313 if (!path)
2314 continue;
2315 print_nid_path("input", path);
2316 spec->input_paths[imux_idx][c] =
2317 snd_hda_get_path_idx(codec, path);
2318
2319 if (!imux_added) {
2320 spec->imux_pins[imux->num_items] = pin;
2321 snd_hda_add_imux_item(imux, label,
2322 imux->num_items, NULL);
2323 imux_added = true;
2324 }
2325 }
2326
2327 return 0;
2328}
2329
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002331 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002333static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002334{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002335 struct hda_gen_spec *spec = codec->spec;
2336 const struct auto_pin_cfg *cfg = &spec->autocfg;
2337 hda_nid_t mixer = spec->mixer_nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002338 int num_adcs;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002339 int i, err, type_idx = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002340 const char *prev_label = NULL;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002341 unsigned int val;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002342
Takashi Iwai352f7f92012-12-19 12:52:06 +01002343 num_adcs = fill_adc_nids(codec);
2344 if (num_adcs < 0)
2345 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002346
Takashi Iwai352f7f92012-12-19 12:52:06 +01002347 for (i = 0; i < cfg->num_inputs; i++) {
2348 hda_nid_t pin;
2349 const char *label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350
Takashi Iwai352f7f92012-12-19 12:52:06 +01002351 pin = cfg->inputs[i].pin;
2352 if (!is_input_pin(codec, pin))
2353 continue;
2354
2355 label = hda_get_autocfg_input_label(codec, cfg, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002356 if (prev_label && !strcmp(label, prev_label))
2357 type_idx++;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002358 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01002359 type_idx = 0;
2360 prev_label = label;
2361
Takashi Iwai2c12c302013-01-10 09:33:29 +01002362 val = PIN_IN;
2363 if (cfg->inputs[i].type == AUTO_PIN_MIC)
2364 val |= snd_hda_get_default_vref(codec, pin);
2365 set_pin_target(codec, pin, val, false);
2366
Takashi Iwai352f7f92012-12-19 12:52:06 +01002367 if (mixer) {
2368 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01002369 err = new_analog_input(codec, i, pin,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002370 label, type_idx, mixer);
2371 if (err < 0)
2372 return err;
2373 }
2374 }
2375
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002376 err = parse_capture_source(codec, pin, num_adcs, label, -mixer);
2377 if (err < 0)
2378 return err;
2379 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002380
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002381 if (mixer && spec->add_stereo_mix_input) {
2382 err = parse_capture_source(codec, mixer, num_adcs,
2383 "Stereo Mix", 0);
2384 if (err < 0)
2385 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002386 }
2387
2388 return 0;
2389}
2390
2391
2392/*
2393 * input source mux
2394 */
2395
Takashi Iwaic697b712013-01-07 17:09:26 +01002396/* get the input path specified by the given adc and imux indices */
2397static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002398{
2399 struct hda_gen_spec *spec = codec->spec;
2400 if (spec->dyn_adc_switch)
2401 adc_idx = spec->dyn_adc_idx[imux_idx];
Takashi Iwaic697b712013-01-07 17:09:26 +01002402 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002403}
2404
2405static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2406 unsigned int idx);
2407
2408static int mux_enum_info(struct snd_kcontrol *kcontrol,
2409 struct snd_ctl_elem_info *uinfo)
2410{
2411 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2412 struct hda_gen_spec *spec = codec->spec;
2413 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
2414}
2415
2416static int mux_enum_get(struct snd_kcontrol *kcontrol,
2417 struct snd_ctl_elem_value *ucontrol)
2418{
2419 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2420 struct hda_gen_spec *spec = codec->spec;
2421 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2422
2423 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
2424 return 0;
2425}
2426
2427static int mux_enum_put(struct snd_kcontrol *kcontrol,
2428 struct snd_ctl_elem_value *ucontrol)
2429{
2430 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2431 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2432 return mux_select(codec, adc_idx,
2433 ucontrol->value.enumerated.item[0]);
2434}
2435
Takashi Iwai352f7f92012-12-19 12:52:06 +01002436static const struct snd_kcontrol_new cap_src_temp = {
2437 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2438 .name = "Input Source",
2439 .info = mux_enum_info,
2440 .get = mux_enum_get,
2441 .put = mux_enum_put,
2442};
2443
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002444/*
2445 * capture volume and capture switch ctls
2446 */
2447
Takashi Iwai352f7f92012-12-19 12:52:06 +01002448typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
2449 struct snd_ctl_elem_value *ucontrol);
2450
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002451/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002452static int cap_put_caller(struct snd_kcontrol *kcontrol,
2453 struct snd_ctl_elem_value *ucontrol,
2454 put_call_t func, int type)
2455{
2456 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2457 struct hda_gen_spec *spec = codec->spec;
2458 const struct hda_input_mux *imux;
2459 struct nid_path *path;
2460 int i, adc_idx, err = 0;
2461
2462 imux = &spec->input_mux;
2463 adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2464 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002465 /* we use the cache-only update at first since multiple input paths
2466 * may shared the same amp; by updating only caches, the redundant
2467 * writes to hardware can be reduced.
2468 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002469 codec->cached_write = 1;
2470 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01002471 path = get_input_path(codec, adc_idx, i);
2472 if (!path || !path->ctls[type])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002473 continue;
2474 kcontrol->private_value = path->ctls[type];
2475 err = func(kcontrol, ucontrol);
2476 if (err < 0)
2477 goto error;
2478 }
2479 error:
2480 codec->cached_write = 0;
2481 mutex_unlock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002482 snd_hda_codec_flush_amp_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002483 if (err >= 0 && spec->cap_sync_hook)
2484 spec->cap_sync_hook(codec);
2485 return err;
2486}
2487
2488/* capture volume ctl callbacks */
2489#define cap_vol_info snd_hda_mixer_amp_volume_info
2490#define cap_vol_get snd_hda_mixer_amp_volume_get
2491#define cap_vol_tlv snd_hda_mixer_amp_tlv
2492
2493static int cap_vol_put(struct snd_kcontrol *kcontrol,
2494 struct snd_ctl_elem_value *ucontrol)
2495{
2496 return cap_put_caller(kcontrol, ucontrol,
2497 snd_hda_mixer_amp_volume_put,
2498 NID_PATH_VOL_CTL);
2499}
2500
2501static const struct snd_kcontrol_new cap_vol_temp = {
2502 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2503 .name = "Capture Volume",
2504 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
2505 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2506 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
2507 .info = cap_vol_info,
2508 .get = cap_vol_get,
2509 .put = cap_vol_put,
2510 .tlv = { .c = cap_vol_tlv },
2511};
2512
2513/* capture switch ctl callbacks */
2514#define cap_sw_info snd_ctl_boolean_stereo_info
2515#define cap_sw_get snd_hda_mixer_amp_switch_get
2516
2517static int cap_sw_put(struct snd_kcontrol *kcontrol,
2518 struct snd_ctl_elem_value *ucontrol)
2519{
Takashi Iwaiae177c32013-01-14 12:13:06 +01002520 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2521 struct hda_gen_spec *spec = codec->spec;
2522 int ret;
2523
2524 ret = cap_put_caller(kcontrol, ucontrol,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002525 snd_hda_mixer_amp_switch_put,
2526 NID_PATH_MUTE_CTL);
Takashi Iwaiae177c32013-01-14 12:13:06 +01002527 if (ret < 0)
2528 return ret;
2529
2530 if (spec->capture_switch_hook) {
2531 bool enable = (ucontrol->value.integer.value[0] ||
2532 ucontrol->value.integer.value[1]);
2533 spec->capture_switch_hook(codec, enable);
2534 }
2535
2536 return ret;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002537}
2538
2539static const struct snd_kcontrol_new cap_sw_temp = {
2540 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2541 .name = "Capture Switch",
2542 .info = cap_sw_info,
2543 .get = cap_sw_get,
2544 .put = cap_sw_put,
2545};
2546
2547static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
2548{
2549 hda_nid_t nid;
2550 int i, depth;
2551
2552 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
2553 for (depth = 0; depth < 3; depth++) {
2554 if (depth >= path->depth)
2555 return -EINVAL;
2556 i = path->depth - depth - 1;
2557 nid = path->path[i];
2558 if (!path->ctls[NID_PATH_VOL_CTL]) {
2559 if (nid_has_volume(codec, nid, HDA_OUTPUT))
2560 path->ctls[NID_PATH_VOL_CTL] =
2561 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2562 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
2563 int idx = path->idx[i];
2564 if (!depth && codec->single_adc_amp)
2565 idx = 0;
2566 path->ctls[NID_PATH_VOL_CTL] =
2567 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2568 }
2569 }
2570 if (!path->ctls[NID_PATH_MUTE_CTL]) {
2571 if (nid_has_mute(codec, nid, HDA_OUTPUT))
2572 path->ctls[NID_PATH_MUTE_CTL] =
2573 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2574 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
2575 int idx = path->idx[i];
2576 if (!depth && codec->single_adc_amp)
2577 idx = 0;
2578 path->ctls[NID_PATH_MUTE_CTL] =
2579 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2580 }
2581 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01002582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 return 0;
2584}
2585
Takashi Iwai352f7f92012-12-19 12:52:06 +01002586static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002588 struct hda_gen_spec *spec = codec->spec;
2589 struct auto_pin_cfg *cfg = &spec->autocfg;
2590 unsigned int val;
2591 int i;
2592
2593 if (!spec->inv_dmic_split)
2594 return false;
2595 for (i = 0; i < cfg->num_inputs; i++) {
2596 if (cfg->inputs[i].pin != nid)
2597 continue;
2598 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2599 return false;
2600 val = snd_hda_codec_get_pincfg(codec, nid);
2601 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
2602 }
2603 return false;
2604}
2605
2606static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
2607 int idx, bool is_switch, unsigned int ctl,
2608 bool inv_dmic)
2609{
2610 struct hda_gen_spec *spec = codec->spec;
2611 char tmpname[44];
2612 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
2613 const char *sfx = is_switch ? "Switch" : "Volume";
2614 unsigned int chs = inv_dmic ? 1 : 3;
2615 int err;
2616
2617 if (!ctl)
2618 return 0;
2619
2620 if (label)
2621 snprintf(tmpname, sizeof(tmpname),
2622 "%s Capture %s", label, sfx);
2623 else
2624 snprintf(tmpname, sizeof(tmpname),
2625 "Capture %s", sfx);
2626 err = add_control(spec, type, tmpname, idx,
2627 amp_val_replace_channels(ctl, chs));
2628 if (err < 0 || !inv_dmic)
2629 return err;
2630
2631 /* Make independent right kcontrol */
2632 if (label)
2633 snprintf(tmpname, sizeof(tmpname),
2634 "Inverted %s Capture %s", label, sfx);
2635 else
2636 snprintf(tmpname, sizeof(tmpname),
2637 "Inverted Capture %s", sfx);
2638 return add_control(spec, type, tmpname, idx,
2639 amp_val_replace_channels(ctl, 2));
2640}
2641
2642/* create single (and simple) capture volume and switch controls */
2643static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
2644 unsigned int vol_ctl, unsigned int sw_ctl,
2645 bool inv_dmic)
2646{
2647 int err;
2648 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
2649 if (err < 0)
2650 return err;
2651 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
2652 if (err < 0)
2653 return err;
2654 return 0;
2655}
2656
2657/* create bound capture volume and switch controls */
2658static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
2659 unsigned int vol_ctl, unsigned int sw_ctl)
2660{
2661 struct hda_gen_spec *spec = codec->spec;
2662 struct snd_kcontrol_new *knew;
2663
2664 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002665 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002666 if (!knew)
2667 return -ENOMEM;
2668 knew->index = idx;
2669 knew->private_value = vol_ctl;
2670 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2671 }
2672 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002673 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002674 if (!knew)
2675 return -ENOMEM;
2676 knew->index = idx;
2677 knew->private_value = sw_ctl;
2678 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2679 }
2680 return 0;
2681}
2682
2683/* return the vol ctl when used first in the imux list */
2684static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
2685{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002686 struct nid_path *path;
2687 unsigned int ctl;
2688 int i;
2689
Takashi Iwaic697b712013-01-07 17:09:26 +01002690 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002691 if (!path)
2692 return 0;
2693 ctl = path->ctls[type];
2694 if (!ctl)
2695 return 0;
2696 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01002697 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002698 if (path && path->ctls[type] == ctl)
2699 return 0;
2700 }
2701 return ctl;
2702}
2703
2704/* create individual capture volume and switch controls per input */
2705static int create_multi_cap_vol_ctl(struct hda_codec *codec)
2706{
2707 struct hda_gen_spec *spec = codec->spec;
2708 struct hda_input_mux *imux = &spec->input_mux;
2709 int i, err, type, type_idx = 0;
2710 const char *prev_label = NULL;
2711
2712 for (i = 0; i < imux->num_items; i++) {
2713 const char *label;
2714 bool inv_dmic;
2715 label = hda_get_autocfg_input_label(codec, &spec->autocfg, i);
2716 if (prev_label && !strcmp(label, prev_label))
2717 type_idx++;
2718 else
2719 type_idx = 0;
2720 prev_label = label;
2721 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
2722
2723 for (type = 0; type < 2; type++) {
2724 err = add_single_cap_ctl(codec, label, type_idx, type,
2725 get_first_cap_ctl(codec, i, type),
2726 inv_dmic);
2727 if (err < 0)
2728 return err;
2729 }
2730 }
2731 return 0;
2732}
2733
2734static int create_capture_mixers(struct hda_codec *codec)
2735{
2736 struct hda_gen_spec *spec = codec->spec;
2737 struct hda_input_mux *imux = &spec->input_mux;
2738 int i, n, nums, err;
2739
2740 if (spec->dyn_adc_switch)
2741 nums = 1;
2742 else
2743 nums = spec->num_adc_nids;
2744
2745 if (!spec->auto_mic && imux->num_items > 1) {
2746 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01002747 const char *name;
2748 name = nums > 1 ? "Input Source" : "Capture Source";
2749 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002750 if (!knew)
2751 return -ENOMEM;
2752 knew->count = nums;
2753 }
2754
2755 for (n = 0; n < nums; n++) {
2756 bool multi = false;
2757 bool inv_dmic = false;
2758 int vol, sw;
2759
2760 vol = sw = 0;
2761 for (i = 0; i < imux->num_items; i++) {
2762 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01002763 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002764 if (!path)
2765 continue;
2766 parse_capvol_in_path(codec, path);
2767 if (!vol)
2768 vol = path->ctls[NID_PATH_VOL_CTL];
2769 else if (vol != path->ctls[NID_PATH_VOL_CTL])
2770 multi = true;
2771 if (!sw)
2772 sw = path->ctls[NID_PATH_MUTE_CTL];
2773 else if (sw != path->ctls[NID_PATH_MUTE_CTL])
2774 multi = true;
2775 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
2776 inv_dmic = true;
2777 }
2778
2779 if (!multi)
2780 err = create_single_cap_vol_ctl(codec, n, vol, sw,
2781 inv_dmic);
2782 else if (!spec->multi_cap_vol)
2783 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
2784 else
2785 err = create_multi_cap_vol_ctl(codec);
2786 if (err < 0)
2787 return err;
2788 }
2789
2790 return 0;
2791}
2792
2793/*
2794 * add mic boosts if needed
2795 */
2796static int parse_mic_boost(struct hda_codec *codec)
2797{
2798 struct hda_gen_spec *spec = codec->spec;
2799 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002800 int i, err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002801 int type_idx = 0;
2802 hda_nid_t nid;
2803 const char *prev_label = NULL;
2804
2805 for (i = 0; i < cfg->num_inputs; i++) {
2806 if (cfg->inputs[i].type > AUTO_PIN_MIC)
2807 break;
2808 nid = cfg->inputs[i].pin;
2809 if (get_wcaps(codec, nid) & AC_WCAP_IN_AMP) {
2810 const char *label;
Takashi Iwai5abd4882013-01-07 09:43:18 +01002811 char boost_label[44];
Takashi Iwai352f7f92012-12-19 12:52:06 +01002812 struct nid_path *path;
2813 unsigned int val;
2814
2815 label = hda_get_autocfg_input_label(codec, cfg, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002816 if (prev_label && !strcmp(label, prev_label))
2817 type_idx++;
2818 else
2819 type_idx = 0;
2820 prev_label = label;
2821
2822 snprintf(boost_label, sizeof(boost_label),
2823 "%s Boost Volume", label);
2824 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
2825 err = add_control(spec, HDA_CTL_WIDGET_VOL,
2826 boost_label, type_idx, val);
2827 if (err < 0)
2828 return err;
2829
2830 path = snd_hda_get_nid_path(codec, nid, 0);
2831 if (path)
2832 path->ctls[NID_PATH_BOOST_CTL] = val;
2833 }
2834 }
2835 return 0;
2836}
2837
2838/*
2839 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
2840 */
2841static void parse_digital(struct hda_codec *codec)
2842{
2843 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002844 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002845 int i, nums;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002846 hda_nid_t dig_nid, pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002847
2848 /* support multiple SPDIFs; the secondary is set up as a slave */
2849 nums = 0;
2850 for (i = 0; i < spec->autocfg.dig_outs; i++) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01002851 pin = spec->autocfg.dig_out_pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01002852 dig_nid = look_for_dac(codec, pin, true);
2853 if (!dig_nid)
2854 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01002855 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002856 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002857 continue;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002858 print_nid_path("digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01002859 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01002860 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01002861 set_pin_target(codec, pin, PIN_OUT, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002862 if (!nums) {
2863 spec->multiout.dig_out_nid = dig_nid;
2864 spec->dig_out_type = spec->autocfg.dig_out_type[0];
2865 } else {
2866 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
2867 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
2868 break;
2869 spec->slave_dig_outs[nums - 1] = dig_nid;
2870 }
2871 nums++;
2872 }
2873
2874 if (spec->autocfg.dig_in_pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01002875 pin = spec->autocfg.dig_in_pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002876 dig_nid = codec->start_nid;
2877 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002878 unsigned int wcaps = get_wcaps(codec, dig_nid);
2879 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
2880 continue;
2881 if (!(wcaps & AC_WCAP_DIGITAL))
2882 continue;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002883 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002884 if (path) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002885 print_nid_path("digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002886 path->active = true;
2887 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01002888 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01002889 set_pin_target(codec, pin, PIN_IN, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002890 break;
2891 }
2892 }
2893 }
2894}
2895
2896
2897/*
2898 * input MUX handling
2899 */
2900
2901static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
2902
2903/* select the given imux item; either unmute exclusively or select the route */
2904static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2905 unsigned int idx)
2906{
2907 struct hda_gen_spec *spec = codec->spec;
2908 const struct hda_input_mux *imux;
2909 struct nid_path *path;
2910
2911 imux = &spec->input_mux;
2912 if (!imux->num_items)
2913 return 0;
2914
2915 if (idx >= imux->num_items)
2916 idx = imux->num_items - 1;
2917 if (spec->cur_mux[adc_idx] == idx)
2918 return 0;
2919
Takashi Iwaic697b712013-01-07 17:09:26 +01002920 path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002921 if (!path)
2922 return 0;
2923 if (path->active)
2924 snd_hda_activate_path(codec, path, false, false);
2925
2926 spec->cur_mux[adc_idx] = idx;
2927
2928 if (spec->shared_mic_hp)
2929 update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
2930
2931 if (spec->dyn_adc_switch)
2932 dyn_adc_pcm_resetup(codec, idx);
2933
Takashi Iwaic697b712013-01-07 17:09:26 +01002934 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002935 if (!path)
2936 return 0;
2937 if (path->active)
2938 return 0;
2939 snd_hda_activate_path(codec, path, true, false);
2940 if (spec->cap_sync_hook)
2941 spec->cap_sync_hook(codec);
2942 return 1;
2943}
2944
2945
2946/*
2947 * Jack detections for HP auto-mute and mic-switch
2948 */
2949
2950/* check each pin in the given array; returns true if any of them is plugged */
2951static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
2952{
2953 int i, present = 0;
2954
2955 for (i = 0; i < num_pins; i++) {
2956 hda_nid_t nid = pins[i];
2957 if (!nid)
2958 break;
Takashi Iwai0b4df932013-01-10 09:45:13 +01002959 /* don't detect pins retasked as inputs */
2960 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
2961 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002962 present |= snd_hda_jack_detect(codec, nid);
2963 }
2964 return present;
2965}
2966
2967/* standard HP/line-out auto-mute helper */
2968static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
Takashi Iwai2c12c302013-01-10 09:33:29 +01002969 bool mute)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002970{
2971 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002972 int i;
2973
2974 for (i = 0; i < num_pins; i++) {
2975 hda_nid_t nid = pins[i];
2976 unsigned int val;
2977 if (!nid)
2978 break;
2979 /* don't reset VREF value in case it's controlling
2980 * the amp (see alc861_fixup_asus_amp_vref_0f())
2981 */
Takashi Iwai2c12c302013-01-10 09:33:29 +01002982 if (spec->keep_vref_in_automute)
2983 val = snd_hda_codec_get_pin_target(codec, nid) & ~PIN_HP;
2984 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01002985 val = 0;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002986 if (!mute)
2987 val |= snd_hda_codec_get_pin_target(codec, nid);
2988 /* here we call update_pin_ctl() so that the pinctl is changed
2989 * without changing the pinctl target value;
2990 * the original target value will be still referred at the
2991 * init / resume again
2992 */
2993 update_pin_ctl(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002994 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002995 }
2996}
2997
2998/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002999void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003000{
3001 struct hda_gen_spec *spec = codec->spec;
3002 int on;
3003
3004 /* Control HP pins/amps depending on master_mute state;
3005 * in general, HP pins/amps control should be enabled in all cases,
3006 * but currently set only for master_mute, just to be safe
3007 */
3008 if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
3009 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003010 spec->autocfg.hp_pins, spec->master_mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003011
3012 if (!spec->automute_speaker)
3013 on = 0;
3014 else
3015 on = spec->hp_jack_present | spec->line_jack_present;
3016 on |= spec->master_mute;
3017 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003018 spec->autocfg.speaker_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003019
3020 /* toggle line-out mutes if needed, too */
3021 /* if LO is a copy of either HP or Speaker, don't need to handle it */
3022 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
3023 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
3024 return;
3025 if (!spec->automute_lo)
3026 on = 0;
3027 else
3028 on = spec->hp_jack_present;
3029 on |= spec->master_mute;
3030 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003031 spec->autocfg.line_out_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003032}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003033EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003034
3035static void call_update_outputs(struct hda_codec *codec)
3036{
3037 struct hda_gen_spec *spec = codec->spec;
3038 if (spec->automute_hook)
3039 spec->automute_hook(codec);
3040 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01003041 snd_hda_gen_update_outputs(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003042}
3043
3044/* standard HP-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003045void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003046{
3047 struct hda_gen_spec *spec = codec->spec;
3048
3049 spec->hp_jack_present =
3050 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
3051 spec->autocfg.hp_pins);
3052 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
3053 return;
3054 call_update_outputs(codec);
3055}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003056EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003057
3058/* standard line-out-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003059void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003060{
3061 struct hda_gen_spec *spec = codec->spec;
3062
3063 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3064 return;
3065 /* check LO jack only when it's different from HP */
3066 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
3067 return;
3068
3069 spec->line_jack_present =
3070 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3071 spec->autocfg.line_out_pins);
3072 if (!spec->automute_speaker || !spec->detect_lo)
3073 return;
3074 call_update_outputs(codec);
3075}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003076EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003077
3078/* standard mic auto-switch helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003079void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003080{
3081 struct hda_gen_spec *spec = codec->spec;
3082 int i;
3083
3084 if (!spec->auto_mic)
3085 return;
3086
3087 for (i = spec->am_num_entries - 1; i > 0; i--) {
Takashi Iwai0b4df932013-01-10 09:45:13 +01003088 hda_nid_t pin = spec->am_entry[i].pin;
3089 /* don't detect pins retasked as outputs */
3090 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
3091 continue;
3092 if (snd_hda_jack_detect(codec, pin)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003093 mux_select(codec, 0, spec->am_entry[i].idx);
3094 return;
3095 }
3096 }
3097 mux_select(codec, 0, spec->am_entry[0].idx);
3098}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003099EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003100
3101/*
3102 * Auto-Mute mode mixer enum support
3103 */
3104static int automute_mode_info(struct snd_kcontrol *kcontrol,
3105 struct snd_ctl_elem_info *uinfo)
3106{
3107 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3108 struct hda_gen_spec *spec = codec->spec;
3109 static const char * const texts3[] = {
3110 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02003111 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112
Takashi Iwai352f7f92012-12-19 12:52:06 +01003113 if (spec->automute_speaker_possible && spec->automute_lo_possible)
3114 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
3115 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
3116}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117
Takashi Iwai352f7f92012-12-19 12:52:06 +01003118static int automute_mode_get(struct snd_kcontrol *kcontrol,
3119 struct snd_ctl_elem_value *ucontrol)
3120{
3121 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3122 struct hda_gen_spec *spec = codec->spec;
3123 unsigned int val = 0;
3124 if (spec->automute_speaker)
3125 val++;
3126 if (spec->automute_lo)
3127 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003128
Takashi Iwai352f7f92012-12-19 12:52:06 +01003129 ucontrol->value.enumerated.item[0] = val;
3130 return 0;
3131}
3132
3133static int automute_mode_put(struct snd_kcontrol *kcontrol,
3134 struct snd_ctl_elem_value *ucontrol)
3135{
3136 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3137 struct hda_gen_spec *spec = codec->spec;
3138
3139 switch (ucontrol->value.enumerated.item[0]) {
3140 case 0:
3141 if (!spec->automute_speaker && !spec->automute_lo)
3142 return 0;
3143 spec->automute_speaker = 0;
3144 spec->automute_lo = 0;
3145 break;
3146 case 1:
3147 if (spec->automute_speaker_possible) {
3148 if (!spec->automute_lo && spec->automute_speaker)
3149 return 0;
3150 spec->automute_speaker = 1;
3151 spec->automute_lo = 0;
3152 } else if (spec->automute_lo_possible) {
3153 if (spec->automute_lo)
3154 return 0;
3155 spec->automute_lo = 1;
3156 } else
3157 return -EINVAL;
3158 break;
3159 case 2:
3160 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
3161 return -EINVAL;
3162 if (spec->automute_speaker && spec->automute_lo)
3163 return 0;
3164 spec->automute_speaker = 1;
3165 spec->automute_lo = 1;
3166 break;
3167 default:
3168 return -EINVAL;
3169 }
3170 call_update_outputs(codec);
3171 return 1;
3172}
3173
3174static const struct snd_kcontrol_new automute_mode_enum = {
3175 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3176 .name = "Auto-Mute Mode",
3177 .info = automute_mode_info,
3178 .get = automute_mode_get,
3179 .put = automute_mode_put,
3180};
3181
3182static int add_automute_mode_enum(struct hda_codec *codec)
3183{
3184 struct hda_gen_spec *spec = codec->spec;
3185
Takashi Iwai12c93df2012-12-19 14:38:33 +01003186 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01003187 return -ENOMEM;
3188 return 0;
3189}
3190
3191/*
3192 * Check the availability of HP/line-out auto-mute;
3193 * Set up appropriately if really supported
3194 */
3195static int check_auto_mute_availability(struct hda_codec *codec)
3196{
3197 struct hda_gen_spec *spec = codec->spec;
3198 struct auto_pin_cfg *cfg = &spec->autocfg;
3199 int present = 0;
3200 int i, err;
3201
3202 if (cfg->hp_pins[0])
3203 present++;
3204 if (cfg->line_out_pins[0])
3205 present++;
3206 if (cfg->speaker_pins[0])
3207 present++;
3208 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02003209 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003210
3211 if (!cfg->speaker_pins[0] &&
3212 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
3213 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3214 sizeof(cfg->speaker_pins));
3215 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217
Takashi Iwai352f7f92012-12-19 12:52:06 +01003218 if (!cfg->hp_pins[0] &&
3219 cfg->line_out_type == AUTO_PIN_HP_OUT) {
3220 memcpy(cfg->hp_pins, cfg->line_out_pins,
3221 sizeof(cfg->hp_pins));
3222 cfg->hp_outs = cfg->line_outs;
3223 }
3224
3225 for (i = 0; i < cfg->hp_outs; i++) {
3226 hda_nid_t nid = cfg->hp_pins[i];
3227 if (!is_jack_detectable(codec, nid))
3228 continue;
3229 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
3230 nid);
3231 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003232 spec->hp_automute_hook ?
3233 spec->hp_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003234 snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003235 spec->detect_hp = 1;
3236 }
3237
3238 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
3239 if (cfg->speaker_outs)
3240 for (i = 0; i < cfg->line_outs; i++) {
3241 hda_nid_t nid = cfg->line_out_pins[i];
3242 if (!is_jack_detectable(codec, nid))
3243 continue;
3244 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
3245 snd_hda_jack_detect_enable_callback(codec, nid,
3246 HDA_GEN_FRONT_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003247 spec->line_automute_hook ?
3248 spec->line_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003249 snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003250 spec->detect_lo = 1;
3251 }
3252 spec->automute_lo_possible = spec->detect_hp;
3253 }
3254
3255 spec->automute_speaker_possible = cfg->speaker_outs &&
3256 (spec->detect_hp || spec->detect_lo);
3257
3258 spec->automute_lo = spec->automute_lo_possible;
3259 spec->automute_speaker = spec->automute_speaker_possible;
3260
3261 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
3262 /* create a control for automute mode */
3263 err = add_automute_mode_enum(codec);
3264 if (err < 0)
3265 return err;
3266 }
3267 return 0;
3268}
3269
Takashi Iwai352f7f92012-12-19 12:52:06 +01003270/* check whether all auto-mic pins are valid; setup indices if OK */
3271static bool auto_mic_check_imux(struct hda_codec *codec)
3272{
3273 struct hda_gen_spec *spec = codec->spec;
3274 const struct hda_input_mux *imux;
3275 int i;
3276
3277 imux = &spec->input_mux;
3278 for (i = 0; i < spec->am_num_entries; i++) {
3279 spec->am_entry[i].idx =
3280 find_idx_in_nid_list(spec->am_entry[i].pin,
3281 spec->imux_pins, imux->num_items);
3282 if (spec->am_entry[i].idx < 0)
3283 return false; /* no corresponding imux */
3284 }
3285
3286 /* we don't need the jack detection for the first pin */
3287 for (i = 1; i < spec->am_num_entries; i++)
3288 snd_hda_jack_detect_enable_callback(codec,
3289 spec->am_entry[i].pin,
3290 HDA_GEN_MIC_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003291 spec->mic_autoswitch_hook ?
3292 spec->mic_autoswitch_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003293 snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003294 return true;
3295}
3296
3297static int compare_attr(const void *ap, const void *bp)
3298{
3299 const struct automic_entry *a = ap;
3300 const struct automic_entry *b = bp;
3301 return (int)(a->attr - b->attr);
3302}
3303
3304/*
3305 * Check the availability of auto-mic switch;
3306 * Set up if really supported
3307 */
3308static int check_auto_mic_availability(struct hda_codec *codec)
3309{
3310 struct hda_gen_spec *spec = codec->spec;
3311 struct auto_pin_cfg *cfg = &spec->autocfg;
3312 unsigned int types;
3313 int i, num_pins;
3314
Takashi Iwaid12daf62013-01-07 16:32:11 +01003315 if (spec->suppress_auto_mic)
3316 return 0;
3317
Takashi Iwai352f7f92012-12-19 12:52:06 +01003318 types = 0;
3319 num_pins = 0;
3320 for (i = 0; i < cfg->num_inputs; i++) {
3321 hda_nid_t nid = cfg->inputs[i].pin;
3322 unsigned int attr;
3323 attr = snd_hda_codec_get_pincfg(codec, nid);
3324 attr = snd_hda_get_input_pin_attr(attr);
3325 if (types & (1 << attr))
3326 return 0; /* already occupied */
3327 switch (attr) {
3328 case INPUT_PIN_ATTR_INT:
3329 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3330 return 0; /* invalid type */
3331 break;
3332 case INPUT_PIN_ATTR_UNUSED:
3333 return 0; /* invalid entry */
3334 default:
3335 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
3336 return 0; /* invalid type */
3337 if (!spec->line_in_auto_switch &&
3338 cfg->inputs[i].type != AUTO_PIN_MIC)
3339 return 0; /* only mic is allowed */
3340 if (!is_jack_detectable(codec, nid))
3341 return 0; /* no unsol support */
3342 break;
3343 }
3344 if (num_pins >= MAX_AUTO_MIC_PINS)
3345 return 0;
3346 types |= (1 << attr);
3347 spec->am_entry[num_pins].pin = nid;
3348 spec->am_entry[num_pins].attr = attr;
3349 num_pins++;
3350 }
3351
3352 if (num_pins < 2)
3353 return 0;
3354
3355 spec->am_num_entries = num_pins;
3356 /* sort the am_entry in the order of attr so that the pin with a
3357 * higher attr will be selected when the jack is plugged.
3358 */
3359 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
3360 compare_attr, NULL);
3361
3362 if (!auto_mic_check_imux(codec))
3363 return 0;
3364
3365 spec->auto_mic = 1;
3366 spec->num_adc_nids = 1;
3367 spec->cur_mux[0] = spec->am_entry[0].idx;
3368 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
3369 spec->am_entry[0].pin,
3370 spec->am_entry[1].pin,
3371 spec->am_entry[2].pin);
3372
3373 return 0;
3374}
3375
3376
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003377/*
3378 * Parse the given BIOS configuration and set up the hda_gen_spec
3379 *
3380 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003381 * or a negative error code
3382 */
3383int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003384 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003385{
3386 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003387 int err;
3388
Takashi Iwai1c70a582013-01-11 17:48:22 +01003389 parse_user_hints(codec);
3390
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003391 if (cfg != &spec->autocfg) {
3392 spec->autocfg = *cfg;
3393 cfg = &spec->autocfg;
3394 }
3395
Takashi Iwai352f7f92012-12-19 12:52:06 +01003396 if (!cfg->line_outs) {
3397 if (cfg->dig_outs || cfg->dig_in_pin) {
3398 spec->multiout.max_channels = 2;
3399 spec->no_analog = 1;
3400 goto dig_only;
3401 }
3402 return 0; /* can't find valid BIOS pin config */
3403 }
3404
3405 if (!spec->no_primary_hp &&
3406 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
3407 cfg->line_outs <= cfg->hp_outs) {
3408 /* use HP as primary out */
3409 cfg->speaker_outs = cfg->line_outs;
3410 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3411 sizeof(cfg->speaker_pins));
3412 cfg->line_outs = cfg->hp_outs;
3413 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
3414 cfg->hp_outs = 0;
3415 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3416 cfg->line_out_type = AUTO_PIN_HP_OUT;
3417 }
3418
3419 err = parse_output_paths(codec);
3420 if (err < 0)
3421 return err;
3422 err = create_multi_channel_mode(codec);
3423 if (err < 0)
3424 return err;
3425 err = create_multi_out_ctls(codec, cfg);
3426 if (err < 0)
3427 return err;
3428 err = create_hp_out_ctls(codec);
3429 if (err < 0)
3430 return err;
3431 err = create_speaker_out_ctls(codec);
3432 if (err < 0)
3433 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003434 err = create_indep_hp_ctls(codec);
3435 if (err < 0)
3436 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01003437 err = create_loopback_mixing_ctl(codec);
3438 if (err < 0)
3439 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003440 err = create_shared_input(codec);
3441 if (err < 0)
3442 return err;
3443 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003444 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02003445 return err;
3446
Takashi Iwaia07a9492013-01-07 16:44:06 +01003447 spec->const_channel_count = spec->ext_channel_count;
3448 /* check the multiple speaker and headphone pins */
3449 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
3450 spec->const_channel_count = max(spec->const_channel_count,
3451 cfg->speaker_outs * 2);
3452 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
3453 spec->const_channel_count = max(spec->const_channel_count,
3454 cfg->hp_outs * 2);
3455 spec->multiout.max_channels = max(spec->ext_channel_count,
3456 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003457
3458 err = check_auto_mute_availability(codec);
3459 if (err < 0)
3460 return err;
3461
3462 err = check_dyn_adc_switch(codec);
3463 if (err < 0)
3464 return err;
3465
3466 if (!spec->shared_mic_hp) {
3467 err = check_auto_mic_availability(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003468 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 }
Takashi Iwai071c73a2006-08-23 18:34:06 +02003471
Takashi Iwai352f7f92012-12-19 12:52:06 +01003472 err = create_capture_mixers(codec);
3473 if (err < 0)
3474 return err;
3475
3476 err = parse_mic_boost(codec);
3477 if (err < 0)
3478 return err;
3479
Takashi Iwai978e77e2013-01-10 16:57:58 +01003480 if (spec->add_out_jack_modes) {
3481 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
3482 err = create_out_jack_modes(codec, cfg->line_outs,
3483 cfg->line_out_pins);
3484 if (err < 0)
3485 return err;
3486 }
3487 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
3488 err = create_out_jack_modes(codec, cfg->hp_outs,
3489 cfg->hp_pins);
3490 if (err < 0)
3491 return err;
3492 }
3493 }
3494
Takashi Iwai352f7f92012-12-19 12:52:06 +01003495 dig_only:
3496 parse_digital(codec);
3497
3498 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003500EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501
3502
3503/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003504 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003506
3507/* slave controls for virtual master */
3508static const char * const slave_pfxs[] = {
3509 "Front", "Surround", "Center", "LFE", "Side",
3510 "Headphone", "Speaker", "Mono", "Line Out",
3511 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01003512 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
3513 "Headphone Front", "Headphone Surround", "Headphone CLFE",
3514 "Headphone Side",
Takashi Iwai352f7f92012-12-19 12:52:06 +01003515 NULL,
3516};
3517
3518int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003520 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522
Takashi Iwai36502d02012-12-19 15:15:10 +01003523 if (spec->kctls.used) {
3524 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
3525 if (err < 0)
3526 return err;
3527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528
Takashi Iwai352f7f92012-12-19 12:52:06 +01003529 if (spec->multiout.dig_out_nid) {
3530 err = snd_hda_create_dig_out_ctls(codec,
3531 spec->multiout.dig_out_nid,
3532 spec->multiout.dig_out_nid,
3533 spec->pcm_rec[1].pcm_type);
3534 if (err < 0)
3535 return err;
3536 if (!spec->no_analog) {
3537 err = snd_hda_create_spdif_share_sw(codec,
3538 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539 if (err < 0)
3540 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003541 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 }
3543 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003544 if (spec->dig_in_nid) {
3545 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
3546 if (err < 0)
3547 return err;
3548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549
Takashi Iwai352f7f92012-12-19 12:52:06 +01003550 /* if we have no master control, let's create it */
3551 if (!spec->no_analog &&
3552 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
3553 unsigned int vmaster_tlv[4];
3554 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
3555 HDA_OUTPUT, vmaster_tlv);
3556 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
3557 vmaster_tlv, slave_pfxs,
3558 "Playback Volume");
3559 if (err < 0)
3560 return err;
3561 }
3562 if (!spec->no_analog &&
3563 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
3564 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
3565 NULL, slave_pfxs,
3566 "Playback Switch",
3567 true, &spec->vmaster_mute.sw_kctl);
3568 if (err < 0)
3569 return err;
3570 if (spec->vmaster_mute.hook)
Takashi Iwaifd25a972012-12-20 14:57:18 +01003571 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
3572 spec->vmaster_mute_enum);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574
Takashi Iwai352f7f92012-12-19 12:52:06 +01003575 free_kctls(spec); /* no longer needed */
3576
3577 if (spec->shared_mic_hp) {
3578 int err;
3579 int nid = spec->autocfg.inputs[1].pin;
3580 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
3581 if (err < 0)
3582 return err;
3583 err = snd_hda_jack_detect_enable(codec, nid, 0);
3584 if (err < 0)
3585 return err;
3586 }
3587
3588 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
3589 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 return err;
3591
3592 return 0;
3593}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003594EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
3595
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596
3597/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003598 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003601static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
3602 struct hda_codec *codec,
3603 struct snd_pcm_substream *substream,
3604 int action)
3605{
3606 struct hda_gen_spec *spec = codec->spec;
3607 if (spec->pcm_playback_hook)
3608 spec->pcm_playback_hook(hinfo, codec, substream, action);
3609}
3610
Takashi Iwai352f7f92012-12-19 12:52:06 +01003611/*
3612 * Analog playback callbacks
3613 */
3614static int playback_pcm_open(struct hda_pcm_stream *hinfo,
3615 struct hda_codec *codec,
3616 struct snd_pcm_substream *substream)
3617{
3618 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003619 int err;
3620
3621 mutex_lock(&spec->pcm_mutex);
3622 err = snd_hda_multi_out_analog_open(codec,
3623 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003624 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003625 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003626 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003627 call_pcm_playback_hook(hinfo, codec, substream,
3628 HDA_GEN_PCM_ACT_OPEN);
3629 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003630 mutex_unlock(&spec->pcm_mutex);
3631 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003632}
3633
3634static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01003635 struct hda_codec *codec,
3636 unsigned int stream_tag,
3637 unsigned int format,
3638 struct snd_pcm_substream *substream)
3639{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003640 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003641 int err;
3642
3643 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
3644 stream_tag, format, substream);
3645 if (!err)
3646 call_pcm_playback_hook(hinfo, codec, substream,
3647 HDA_GEN_PCM_ACT_PREPARE);
3648 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003649}
Takashi Iwai97ec5582006-03-21 11:29:07 +01003650
Takashi Iwai352f7f92012-12-19 12:52:06 +01003651static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3652 struct hda_codec *codec,
3653 struct snd_pcm_substream *substream)
3654{
3655 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003656 int err;
3657
3658 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
3659 if (!err)
3660 call_pcm_playback_hook(hinfo, codec, substream,
3661 HDA_GEN_PCM_ACT_CLEANUP);
3662 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003663}
3664
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003665static int playback_pcm_close(struct hda_pcm_stream *hinfo,
3666 struct hda_codec *codec,
3667 struct snd_pcm_substream *substream)
3668{
3669 struct hda_gen_spec *spec = codec->spec;
3670 mutex_lock(&spec->pcm_mutex);
3671 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003672 call_pcm_playback_hook(hinfo, codec, substream,
3673 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003674 mutex_unlock(&spec->pcm_mutex);
3675 return 0;
3676}
3677
3678static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
3679 struct hda_codec *codec,
3680 struct snd_pcm_substream *substream)
3681{
3682 struct hda_gen_spec *spec = codec->spec;
3683 int err = 0;
3684
3685 mutex_lock(&spec->pcm_mutex);
3686 if (!spec->indep_hp_enabled)
3687 err = -EBUSY;
3688 else
3689 spec->active_streams |= 1 << STREAM_INDEP_HP;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003690 call_pcm_playback_hook(hinfo, codec, substream,
3691 HDA_GEN_PCM_ACT_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003692 mutex_unlock(&spec->pcm_mutex);
3693 return err;
3694}
3695
3696static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
3697 struct hda_codec *codec,
3698 struct snd_pcm_substream *substream)
3699{
3700 struct hda_gen_spec *spec = codec->spec;
3701 mutex_lock(&spec->pcm_mutex);
3702 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003703 call_pcm_playback_hook(hinfo, codec, substream,
3704 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003705 mutex_unlock(&spec->pcm_mutex);
3706 return 0;
3707}
3708
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003709static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3710 struct hda_codec *codec,
3711 unsigned int stream_tag,
3712 unsigned int format,
3713 struct snd_pcm_substream *substream)
3714{
3715 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3716 call_pcm_playback_hook(hinfo, codec, substream,
3717 HDA_GEN_PCM_ACT_PREPARE);
3718 return 0;
3719}
3720
3721static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3722 struct hda_codec *codec,
3723 struct snd_pcm_substream *substream)
3724{
3725 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
3726 call_pcm_playback_hook(hinfo, codec, substream,
3727 HDA_GEN_PCM_ACT_CLEANUP);
3728 return 0;
3729}
3730
Takashi Iwai352f7f92012-12-19 12:52:06 +01003731/*
3732 * Digital out
3733 */
3734static int dig_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 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3740}
3741
3742static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3743 struct hda_codec *codec,
3744 unsigned int stream_tag,
3745 unsigned int format,
3746 struct snd_pcm_substream *substream)
3747{
3748 struct hda_gen_spec *spec = codec->spec;
3749 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3750 stream_tag, format, substream);
3751}
3752
3753static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3754 struct hda_codec *codec,
3755 struct snd_pcm_substream *substream)
3756{
3757 struct hda_gen_spec *spec = codec->spec;
3758 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
3759}
3760
3761static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
3762 struct hda_codec *codec,
3763 struct snd_pcm_substream *substream)
3764{
3765 struct hda_gen_spec *spec = codec->spec;
3766 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3767}
3768
3769/*
3770 * Analog capture
3771 */
3772static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3773 struct hda_codec *codec,
3774 unsigned int stream_tag,
3775 unsigned int format,
3776 struct snd_pcm_substream *substream)
3777{
3778 struct hda_gen_spec *spec = codec->spec;
3779
3780 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01003781 stream_tag, 0, format);
3782 return 0;
3783}
3784
Takashi Iwai352f7f92012-12-19 12:52:06 +01003785static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3786 struct hda_codec *codec,
3787 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01003788{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003789 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01003790
Takashi Iwai352f7f92012-12-19 12:52:06 +01003791 snd_hda_codec_cleanup_stream(codec,
3792 spec->adc_nids[substream->number + 1]);
Takashi Iwai97ec5582006-03-21 11:29:07 +01003793 return 0;
3794}
3795
Takashi Iwai352f7f92012-12-19 12:52:06 +01003796/*
3797 */
3798static const struct hda_pcm_stream pcm_analog_playback = {
3799 .substreams = 1,
3800 .channels_min = 2,
3801 .channels_max = 8,
3802 /* NID is set in build_pcms */
3803 .ops = {
3804 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003805 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003806 .prepare = playback_pcm_prepare,
3807 .cleanup = playback_pcm_cleanup
3808 },
3809};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810
Takashi Iwai352f7f92012-12-19 12:52:06 +01003811static const struct hda_pcm_stream pcm_analog_capture = {
3812 .substreams = 1,
3813 .channels_min = 2,
3814 .channels_max = 2,
3815 /* NID is set in build_pcms */
3816};
3817
3818static const struct hda_pcm_stream pcm_analog_alt_playback = {
3819 .substreams = 1,
3820 .channels_min = 2,
3821 .channels_max = 2,
3822 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003823 .ops = {
3824 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003825 .close = alt_playback_pcm_close,
3826 .prepare = alt_playback_pcm_prepare,
3827 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003828 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01003829};
3830
3831static const struct hda_pcm_stream pcm_analog_alt_capture = {
3832 .substreams = 2, /* can be overridden */
3833 .channels_min = 2,
3834 .channels_max = 2,
3835 /* NID is set in build_pcms */
3836 .ops = {
3837 .prepare = alt_capture_pcm_prepare,
3838 .cleanup = alt_capture_pcm_cleanup
3839 },
3840};
3841
3842static const struct hda_pcm_stream pcm_digital_playback = {
3843 .substreams = 1,
3844 .channels_min = 2,
3845 .channels_max = 2,
3846 /* NID is set in build_pcms */
3847 .ops = {
3848 .open = dig_playback_pcm_open,
3849 .close = dig_playback_pcm_close,
3850 .prepare = dig_playback_pcm_prepare,
3851 .cleanup = dig_playback_pcm_cleanup
3852 },
3853};
3854
3855static const struct hda_pcm_stream pcm_digital_capture = {
3856 .substreams = 1,
3857 .channels_min = 2,
3858 .channels_max = 2,
3859 /* NID is set in build_pcms */
3860};
3861
3862/* Used by build_pcms to flag that a PCM has no playback stream */
3863static const struct hda_pcm_stream pcm_null_stream = {
3864 .substreams = 0,
3865 .channels_min = 0,
3866 .channels_max = 0,
3867};
3868
3869/*
3870 * dynamic changing ADC PCM streams
3871 */
3872static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
3873{
3874 struct hda_gen_spec *spec = codec->spec;
3875 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
3876
3877 if (spec->cur_adc && spec->cur_adc != new_adc) {
3878 /* stream is running, let's swap the current ADC */
3879 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
3880 spec->cur_adc = new_adc;
3881 snd_hda_codec_setup_stream(codec, new_adc,
3882 spec->cur_adc_stream_tag, 0,
3883 spec->cur_adc_format);
3884 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003886 return false;
3887}
3888
3889/* analog capture with dynamic dual-adc changes */
3890static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3891 struct hda_codec *codec,
3892 unsigned int stream_tag,
3893 unsigned int format,
3894 struct snd_pcm_substream *substream)
3895{
3896 struct hda_gen_spec *spec = codec->spec;
3897 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
3898 spec->cur_adc_stream_tag = stream_tag;
3899 spec->cur_adc_format = format;
3900 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
3901 return 0;
3902}
3903
3904static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3905 struct hda_codec *codec,
3906 struct snd_pcm_substream *substream)
3907{
3908 struct hda_gen_spec *spec = codec->spec;
3909 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
3910 spec->cur_adc = 0;
3911 return 0;
3912}
3913
3914static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
3915 .substreams = 1,
3916 .channels_min = 2,
3917 .channels_max = 2,
3918 .nid = 0, /* fill later */
3919 .ops = {
3920 .prepare = dyn_adc_capture_pcm_prepare,
3921 .cleanup = dyn_adc_capture_pcm_cleanup
3922 },
3923};
3924
Takashi Iwaif873e532012-12-20 16:58:39 +01003925static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
3926 const char *chip_name)
3927{
3928 char *p;
3929
3930 if (*str)
3931 return;
3932 strlcpy(str, chip_name, len);
3933
3934 /* drop non-alnum chars after a space */
3935 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
3936 if (!isalnum(p[1])) {
3937 *p = 0;
3938 break;
3939 }
3940 }
3941 strlcat(str, sfx, len);
3942}
3943
Takashi Iwai352f7f92012-12-19 12:52:06 +01003944/* build PCM streams based on the parsed results */
3945int snd_hda_gen_build_pcms(struct hda_codec *codec)
3946{
3947 struct hda_gen_spec *spec = codec->spec;
3948 struct hda_pcm *info = spec->pcm_rec;
3949 const struct hda_pcm_stream *p;
3950 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951
3952 codec->num_pcms = 1;
3953 codec->pcm_info = info;
3954
Takashi Iwai352f7f92012-12-19 12:52:06 +01003955 if (spec->no_analog)
3956 goto skip_analog;
3957
Takashi Iwaif873e532012-12-20 16:58:39 +01003958 fill_pcm_stream_name(spec->stream_name_analog,
3959 sizeof(spec->stream_name_analog),
3960 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003961 info->name = spec->stream_name_analog;
3962
3963 if (spec->multiout.num_dacs > 0) {
3964 p = spec->stream_analog_playback;
3965 if (!p)
3966 p = &pcm_analog_playback;
3967 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3968 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
3969 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
3970 spec->multiout.max_channels;
3971 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
3972 spec->autocfg.line_outs == 2)
3973 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
3974 snd_pcm_2_1_chmaps;
3975 }
3976 if (spec->num_adc_nids) {
3977 p = spec->stream_analog_capture;
3978 if (!p) {
3979 if (spec->dyn_adc_switch)
3980 p = &dyn_adc_pcm_analog_capture;
3981 else
3982 p = &pcm_analog_capture;
3983 }
3984 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3985 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
3986 }
3987
Takashi Iwai352f7f92012-12-19 12:52:06 +01003988 skip_analog:
3989 /* SPDIF for stream index #1 */
3990 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01003991 fill_pcm_stream_name(spec->stream_name_digital,
3992 sizeof(spec->stream_name_digital),
3993 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003994 codec->num_pcms = 2;
3995 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
3996 info = spec->pcm_rec + 1;
3997 info->name = spec->stream_name_digital;
3998 if (spec->dig_out_type)
3999 info->pcm_type = spec->dig_out_type;
4000 else
4001 info->pcm_type = HDA_PCM_TYPE_SPDIF;
4002 if (spec->multiout.dig_out_nid) {
4003 p = spec->stream_digital_playback;
4004 if (!p)
4005 p = &pcm_digital_playback;
4006 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4007 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
4008 }
4009 if (spec->dig_in_nid) {
4010 p = spec->stream_digital_capture;
4011 if (!p)
4012 p = &pcm_digital_capture;
4013 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4014 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
4015 }
4016 }
4017
4018 if (spec->no_analog)
4019 return 0;
4020
4021 /* If the use of more than one ADC is requested for the current
4022 * model, configure a second analog capture-only PCM.
4023 */
4024 have_multi_adcs = (spec->num_adc_nids > 1) &&
4025 !spec->dyn_adc_switch && !spec->auto_mic;
4026 /* Additional Analaog capture for index #2 */
4027 if (spec->alt_dac_nid || have_multi_adcs) {
4028 codec->num_pcms = 3;
4029 info = spec->pcm_rec + 2;
4030 info->name = spec->stream_name_analog;
4031 if (spec->alt_dac_nid) {
4032 p = spec->stream_analog_alt_playback;
4033 if (!p)
4034 p = &pcm_analog_alt_playback;
4035 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4036 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
4037 spec->alt_dac_nid;
4038 } else {
4039 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
4040 pcm_null_stream;
4041 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
4042 }
4043 if (have_multi_adcs) {
4044 p = spec->stream_analog_alt_capture;
4045 if (!p)
4046 p = &pcm_analog_alt_capture;
4047 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4048 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
4049 spec->adc_nids[1];
4050 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
4051 spec->num_adc_nids - 1;
4052 } else {
4053 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
4054 pcm_null_stream;
4055 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
4056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057 }
4058
4059 return 0;
4060}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004061EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
4062
4063
4064/*
4065 * Standard auto-parser initializations
4066 */
4067
Takashi Iwaid4156932013-01-07 10:08:02 +01004068/* configure the given path as a proper output */
Takashi Iwai2c12c302013-01-10 09:33:29 +01004069static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004070{
4071 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01004072 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004073
Takashi Iwai196c17662013-01-04 15:01:40 +01004074 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01004075 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004076 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01004077 pin = path->path[path->depth - 1];
Takashi Iwai2c12c302013-01-10 09:33:29 +01004078 restore_pin_ctl(codec, pin);
Takashi Iwaie1284af2013-01-03 16:33:02 +01004079 snd_hda_activate_path(codec, path, path->active, true);
4080 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004081}
4082
4083/* initialize primary output paths */
4084static void init_multi_out(struct hda_codec *codec)
4085{
4086 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004087 int i;
4088
Takashi Iwaid4156932013-01-07 10:08:02 +01004089 for (i = 0; i < spec->autocfg.line_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004090 set_output_and_unmute(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004091}
4092
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004093
Takashi Iwai2c12c302013-01-10 09:33:29 +01004094static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004095{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004096 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004097
Takashi Iwaid4156932013-01-07 10:08:02 +01004098 for (i = 0; i < num_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004099 set_output_and_unmute(codec, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004100}
4101
4102/* initialize hp and speaker paths */
4103static void init_extra_out(struct hda_codec *codec)
4104{
4105 struct hda_gen_spec *spec = codec->spec;
4106
4107 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004108 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004109 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
4110 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai2c12c302013-01-10 09:33:29 +01004111 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004112}
4113
4114/* initialize multi-io paths */
4115static void init_multi_io(struct hda_codec *codec)
4116{
4117 struct hda_gen_spec *spec = codec->spec;
4118 int i;
4119
4120 for (i = 0; i < spec->multi_ios; i++) {
4121 hda_nid_t pin = spec->multi_io[i].pin;
4122 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01004123 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004124 if (!path)
4125 continue;
4126 if (!spec->multi_io[i].ctl_in)
4127 spec->multi_io[i].ctl_in =
Takashi Iwai2c12c302013-01-10 09:33:29 +01004128 snd_hda_codec_get_pin_target(codec, pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004129 snd_hda_activate_path(codec, path, path->active, true);
4130 }
4131}
4132
Takashi Iwai352f7f92012-12-19 12:52:06 +01004133/* set up input pins and loopback paths */
4134static void init_analog_input(struct hda_codec *codec)
4135{
4136 struct hda_gen_spec *spec = codec->spec;
4137 struct auto_pin_cfg *cfg = &spec->autocfg;
4138 int i;
4139
4140 for (i = 0; i < cfg->num_inputs; i++) {
4141 hda_nid_t nid = cfg->inputs[i].pin;
4142 if (is_input_pin(codec, nid))
Takashi Iwai2c12c302013-01-10 09:33:29 +01004143 restore_pin_ctl(codec, nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004144
4145 /* init loopback inputs */
4146 if (spec->mixer_nid) {
4147 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01004148 path = snd_hda_get_path_from_idx(codec, spec->loopback_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004149 if (path)
4150 snd_hda_activate_path(codec, path,
4151 path->active, false);
4152 }
4153 }
4154}
4155
4156/* initialize ADC paths */
4157static void init_input_src(struct hda_codec *codec)
4158{
4159 struct hda_gen_spec *spec = codec->spec;
4160 struct hda_input_mux *imux = &spec->input_mux;
4161 struct nid_path *path;
4162 int i, c, nums;
4163
4164 if (spec->dyn_adc_switch)
4165 nums = 1;
4166 else
4167 nums = spec->num_adc_nids;
4168
4169 for (c = 0; c < nums; c++) {
4170 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01004171 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004172 if (path) {
4173 bool active = path->active;
4174 if (i == spec->cur_mux[c])
4175 active = true;
4176 snd_hda_activate_path(codec, path, active, false);
4177 }
4178 }
4179 }
4180
4181 if (spec->shared_mic_hp)
4182 update_shared_mic_hp(codec, spec->cur_mux[0]);
4183
4184 if (spec->cap_sync_hook)
4185 spec->cap_sync_hook(codec);
4186}
4187
4188/* set right pin controls for digital I/O */
4189static void init_digital(struct hda_codec *codec)
4190{
4191 struct hda_gen_spec *spec = codec->spec;
4192 int i;
4193 hda_nid_t pin;
4194
Takashi Iwaid4156932013-01-07 10:08:02 +01004195 for (i = 0; i < spec->autocfg.dig_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004196 set_output_and_unmute(codec, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004197 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01004198 if (pin) {
4199 struct nid_path *path;
Takashi Iwai2c12c302013-01-10 09:33:29 +01004200 restore_pin_ctl(codec, pin);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01004201 path = snd_hda_get_path_from_idx(codec, spec->digin_path);
4202 if (path)
4203 snd_hda_activate_path(codec, path, path->active, false);
4204 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004205}
4206
Takashi Iwai973e4972012-12-20 15:16:09 +01004207/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
4208 * invalid unsol tags by some reason
4209 */
4210static void clear_unsol_on_unused_pins(struct hda_codec *codec)
4211{
4212 int i;
4213
4214 for (i = 0; i < codec->init_pins.used; i++) {
4215 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
4216 hda_nid_t nid = pin->nid;
4217 if (is_jack_detectable(codec, nid) &&
4218 !snd_hda_jack_tbl_get(codec, nid))
4219 snd_hda_codec_update_cache(codec, nid, 0,
4220 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
4221 }
4222}
4223
Takashi Iwai5187ac12013-01-07 12:52:16 +01004224/*
4225 * initialize the generic spec;
4226 * this can be put as patch_ops.init function
4227 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004228int snd_hda_gen_init(struct hda_codec *codec)
4229{
4230 struct hda_gen_spec *spec = codec->spec;
4231
4232 if (spec->init_hook)
4233 spec->init_hook(codec);
4234
4235 snd_hda_apply_verbs(codec);
4236
Takashi Iwai3bbcd272012-12-20 11:50:58 +01004237 codec->cached_write = 1;
4238
Takashi Iwai352f7f92012-12-19 12:52:06 +01004239 init_multi_out(codec);
4240 init_extra_out(codec);
4241 init_multi_io(codec);
4242 init_analog_input(codec);
4243 init_input_src(codec);
4244 init_digital(codec);
4245
Takashi Iwai973e4972012-12-20 15:16:09 +01004246 clear_unsol_on_unused_pins(codec);
4247
Takashi Iwai352f7f92012-12-19 12:52:06 +01004248 /* call init functions of standard auto-mute helpers */
Takashi Iwai5d550e12012-12-19 15:16:44 +01004249 snd_hda_gen_hp_automute(codec, NULL);
4250 snd_hda_gen_line_automute(codec, NULL);
4251 snd_hda_gen_mic_autoswitch(codec, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004252
Takashi Iwai3bbcd272012-12-20 11:50:58 +01004253 snd_hda_codec_flush_amp_cache(codec);
4254 snd_hda_codec_flush_cmd_cache(codec);
4255
Takashi Iwai352f7f92012-12-19 12:52:06 +01004256 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
4257 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
4258
4259 hda_call_check_power_status(codec, 0x01);
4260 return 0;
4261}
Takashi Iwaifce52a32013-01-07 12:42:48 +01004262EXPORT_SYMBOL_HDA(snd_hda_gen_init);
4263
Takashi Iwai5187ac12013-01-07 12:52:16 +01004264/*
4265 * free the generic spec;
4266 * this can be put as patch_ops.free function
4267 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01004268void snd_hda_gen_free(struct hda_codec *codec)
4269{
4270 snd_hda_gen_spec_free(codec->spec);
4271 kfree(codec->spec);
4272 codec->spec = NULL;
4273}
4274EXPORT_SYMBOL_HDA(snd_hda_gen_free);
4275
4276#ifdef CONFIG_PM
Takashi Iwai5187ac12013-01-07 12:52:16 +01004277/*
4278 * check the loopback power save state;
4279 * this can be put as patch_ops.check_power_status function
4280 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01004281int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
4282{
4283 struct hda_gen_spec *spec = codec->spec;
4284 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
4285}
4286EXPORT_SYMBOL_HDA(snd_hda_gen_check_power_status);
4287#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01004288
4289
4290/*
4291 * the generic codec support
4292 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293
Takashi Iwai352f7f92012-12-19 12:52:06 +01004294static const struct hda_codec_ops generic_patch_ops = {
4295 .build_controls = snd_hda_gen_build_controls,
4296 .build_pcms = snd_hda_gen_build_pcms,
4297 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01004298 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004299 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02004300#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01004301 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02004302#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303};
4304
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305int snd_hda_parse_generic_codec(struct hda_codec *codec)
4306{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004307 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 int err;
4309
Takashi Iwaie560d8d2005-09-09 14:21:46 +02004310 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004311 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004313 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004316 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
4317 if (err < 0)
4318 return err;
4319
4320 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004321 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 goto error;
4323
4324 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 return 0;
4326
Takashi Iwai352f7f92012-12-19 12:52:06 +01004327error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01004328 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 return err;
4330}
Takashi Iwaifce52a32013-01-07 12:42:48 +01004331EXPORT_SYMBOL_HDA(snd_hda_parse_generic_codec);