blob: 4bc4cd9338664ffee03ff1257d50708b8355fdbd [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{
2520 return cap_put_caller(kcontrol, ucontrol,
2521 snd_hda_mixer_amp_switch_put,
2522 NID_PATH_MUTE_CTL);
2523}
2524
2525static const struct snd_kcontrol_new cap_sw_temp = {
2526 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2527 .name = "Capture Switch",
2528 .info = cap_sw_info,
2529 .get = cap_sw_get,
2530 .put = cap_sw_put,
2531};
2532
2533static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
2534{
2535 hda_nid_t nid;
2536 int i, depth;
2537
2538 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
2539 for (depth = 0; depth < 3; depth++) {
2540 if (depth >= path->depth)
2541 return -EINVAL;
2542 i = path->depth - depth - 1;
2543 nid = path->path[i];
2544 if (!path->ctls[NID_PATH_VOL_CTL]) {
2545 if (nid_has_volume(codec, nid, HDA_OUTPUT))
2546 path->ctls[NID_PATH_VOL_CTL] =
2547 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2548 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
2549 int idx = path->idx[i];
2550 if (!depth && codec->single_adc_amp)
2551 idx = 0;
2552 path->ctls[NID_PATH_VOL_CTL] =
2553 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2554 }
2555 }
2556 if (!path->ctls[NID_PATH_MUTE_CTL]) {
2557 if (nid_has_mute(codec, nid, HDA_OUTPUT))
2558 path->ctls[NID_PATH_MUTE_CTL] =
2559 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2560 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
2561 int idx = path->idx[i];
2562 if (!depth && codec->single_adc_amp)
2563 idx = 0;
2564 path->ctls[NID_PATH_MUTE_CTL] =
2565 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2566 }
2567 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01002568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 return 0;
2570}
2571
Takashi Iwai352f7f92012-12-19 12:52:06 +01002572static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002574 struct hda_gen_spec *spec = codec->spec;
2575 struct auto_pin_cfg *cfg = &spec->autocfg;
2576 unsigned int val;
2577 int i;
2578
2579 if (!spec->inv_dmic_split)
2580 return false;
2581 for (i = 0; i < cfg->num_inputs; i++) {
2582 if (cfg->inputs[i].pin != nid)
2583 continue;
2584 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2585 return false;
2586 val = snd_hda_codec_get_pincfg(codec, nid);
2587 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
2588 }
2589 return false;
2590}
2591
2592static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
2593 int idx, bool is_switch, unsigned int ctl,
2594 bool inv_dmic)
2595{
2596 struct hda_gen_spec *spec = codec->spec;
2597 char tmpname[44];
2598 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
2599 const char *sfx = is_switch ? "Switch" : "Volume";
2600 unsigned int chs = inv_dmic ? 1 : 3;
2601 int err;
2602
2603 if (!ctl)
2604 return 0;
2605
2606 if (label)
2607 snprintf(tmpname, sizeof(tmpname),
2608 "%s Capture %s", label, sfx);
2609 else
2610 snprintf(tmpname, sizeof(tmpname),
2611 "Capture %s", sfx);
2612 err = add_control(spec, type, tmpname, idx,
2613 amp_val_replace_channels(ctl, chs));
2614 if (err < 0 || !inv_dmic)
2615 return err;
2616
2617 /* Make independent right kcontrol */
2618 if (label)
2619 snprintf(tmpname, sizeof(tmpname),
2620 "Inverted %s Capture %s", label, sfx);
2621 else
2622 snprintf(tmpname, sizeof(tmpname),
2623 "Inverted Capture %s", sfx);
2624 return add_control(spec, type, tmpname, idx,
2625 amp_val_replace_channels(ctl, 2));
2626}
2627
2628/* create single (and simple) capture volume and switch controls */
2629static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
2630 unsigned int vol_ctl, unsigned int sw_ctl,
2631 bool inv_dmic)
2632{
2633 int err;
2634 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
2635 if (err < 0)
2636 return err;
2637 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
2638 if (err < 0)
2639 return err;
2640 return 0;
2641}
2642
2643/* create bound capture volume and switch controls */
2644static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
2645 unsigned int vol_ctl, unsigned int sw_ctl)
2646{
2647 struct hda_gen_spec *spec = codec->spec;
2648 struct snd_kcontrol_new *knew;
2649
2650 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002651 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002652 if (!knew)
2653 return -ENOMEM;
2654 knew->index = idx;
2655 knew->private_value = vol_ctl;
2656 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2657 }
2658 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002659 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002660 if (!knew)
2661 return -ENOMEM;
2662 knew->index = idx;
2663 knew->private_value = sw_ctl;
2664 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2665 }
2666 return 0;
2667}
2668
2669/* return the vol ctl when used first in the imux list */
2670static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
2671{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002672 struct nid_path *path;
2673 unsigned int ctl;
2674 int i;
2675
Takashi Iwaic697b712013-01-07 17:09:26 +01002676 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002677 if (!path)
2678 return 0;
2679 ctl = path->ctls[type];
2680 if (!ctl)
2681 return 0;
2682 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01002683 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002684 if (path && path->ctls[type] == ctl)
2685 return 0;
2686 }
2687 return ctl;
2688}
2689
2690/* create individual capture volume and switch controls per input */
2691static int create_multi_cap_vol_ctl(struct hda_codec *codec)
2692{
2693 struct hda_gen_spec *spec = codec->spec;
2694 struct hda_input_mux *imux = &spec->input_mux;
2695 int i, err, type, type_idx = 0;
2696 const char *prev_label = NULL;
2697
2698 for (i = 0; i < imux->num_items; i++) {
2699 const char *label;
2700 bool inv_dmic;
2701 label = hda_get_autocfg_input_label(codec, &spec->autocfg, i);
2702 if (prev_label && !strcmp(label, prev_label))
2703 type_idx++;
2704 else
2705 type_idx = 0;
2706 prev_label = label;
2707 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
2708
2709 for (type = 0; type < 2; type++) {
2710 err = add_single_cap_ctl(codec, label, type_idx, type,
2711 get_first_cap_ctl(codec, i, type),
2712 inv_dmic);
2713 if (err < 0)
2714 return err;
2715 }
2716 }
2717 return 0;
2718}
2719
2720static int create_capture_mixers(struct hda_codec *codec)
2721{
2722 struct hda_gen_spec *spec = codec->spec;
2723 struct hda_input_mux *imux = &spec->input_mux;
2724 int i, n, nums, err;
2725
2726 if (spec->dyn_adc_switch)
2727 nums = 1;
2728 else
2729 nums = spec->num_adc_nids;
2730
2731 if (!spec->auto_mic && imux->num_items > 1) {
2732 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01002733 const char *name;
2734 name = nums > 1 ? "Input Source" : "Capture Source";
2735 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002736 if (!knew)
2737 return -ENOMEM;
2738 knew->count = nums;
2739 }
2740
2741 for (n = 0; n < nums; n++) {
2742 bool multi = false;
2743 bool inv_dmic = false;
2744 int vol, sw;
2745
2746 vol = sw = 0;
2747 for (i = 0; i < imux->num_items; i++) {
2748 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01002749 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002750 if (!path)
2751 continue;
2752 parse_capvol_in_path(codec, path);
2753 if (!vol)
2754 vol = path->ctls[NID_PATH_VOL_CTL];
2755 else if (vol != path->ctls[NID_PATH_VOL_CTL])
2756 multi = true;
2757 if (!sw)
2758 sw = path->ctls[NID_PATH_MUTE_CTL];
2759 else if (sw != path->ctls[NID_PATH_MUTE_CTL])
2760 multi = true;
2761 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
2762 inv_dmic = true;
2763 }
2764
2765 if (!multi)
2766 err = create_single_cap_vol_ctl(codec, n, vol, sw,
2767 inv_dmic);
2768 else if (!spec->multi_cap_vol)
2769 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
2770 else
2771 err = create_multi_cap_vol_ctl(codec);
2772 if (err < 0)
2773 return err;
2774 }
2775
2776 return 0;
2777}
2778
2779/*
2780 * add mic boosts if needed
2781 */
2782static int parse_mic_boost(struct hda_codec *codec)
2783{
2784 struct hda_gen_spec *spec = codec->spec;
2785 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002786 int i, err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002787 int type_idx = 0;
2788 hda_nid_t nid;
2789 const char *prev_label = NULL;
2790
2791 for (i = 0; i < cfg->num_inputs; i++) {
2792 if (cfg->inputs[i].type > AUTO_PIN_MIC)
2793 break;
2794 nid = cfg->inputs[i].pin;
2795 if (get_wcaps(codec, nid) & AC_WCAP_IN_AMP) {
2796 const char *label;
Takashi Iwai5abd4882013-01-07 09:43:18 +01002797 char boost_label[44];
Takashi Iwai352f7f92012-12-19 12:52:06 +01002798 struct nid_path *path;
2799 unsigned int val;
2800
2801 label = hda_get_autocfg_input_label(codec, cfg, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002802 if (prev_label && !strcmp(label, prev_label))
2803 type_idx++;
2804 else
2805 type_idx = 0;
2806 prev_label = label;
2807
2808 snprintf(boost_label, sizeof(boost_label),
2809 "%s Boost Volume", label);
2810 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
2811 err = add_control(spec, HDA_CTL_WIDGET_VOL,
2812 boost_label, type_idx, val);
2813 if (err < 0)
2814 return err;
2815
2816 path = snd_hda_get_nid_path(codec, nid, 0);
2817 if (path)
2818 path->ctls[NID_PATH_BOOST_CTL] = val;
2819 }
2820 }
2821 return 0;
2822}
2823
2824/*
2825 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
2826 */
2827static void parse_digital(struct hda_codec *codec)
2828{
2829 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002830 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002831 int i, nums;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002832 hda_nid_t dig_nid, pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002833
2834 /* support multiple SPDIFs; the secondary is set up as a slave */
2835 nums = 0;
2836 for (i = 0; i < spec->autocfg.dig_outs; i++) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01002837 pin = spec->autocfg.dig_out_pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01002838 dig_nid = look_for_dac(codec, pin, true);
2839 if (!dig_nid)
2840 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01002841 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002842 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002843 continue;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002844 print_nid_path("digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01002845 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01002846 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01002847 set_pin_target(codec, pin, PIN_OUT, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002848 if (!nums) {
2849 spec->multiout.dig_out_nid = dig_nid;
2850 spec->dig_out_type = spec->autocfg.dig_out_type[0];
2851 } else {
2852 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
2853 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
2854 break;
2855 spec->slave_dig_outs[nums - 1] = dig_nid;
2856 }
2857 nums++;
2858 }
2859
2860 if (spec->autocfg.dig_in_pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01002861 pin = spec->autocfg.dig_in_pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002862 dig_nid = codec->start_nid;
2863 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002864 unsigned int wcaps = get_wcaps(codec, dig_nid);
2865 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
2866 continue;
2867 if (!(wcaps & AC_WCAP_DIGITAL))
2868 continue;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002869 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002870 if (path) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002871 print_nid_path("digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002872 path->active = true;
2873 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01002874 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01002875 set_pin_target(codec, pin, PIN_IN, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002876 break;
2877 }
2878 }
2879 }
2880}
2881
2882
2883/*
2884 * input MUX handling
2885 */
2886
2887static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
2888
2889/* select the given imux item; either unmute exclusively or select the route */
2890static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2891 unsigned int idx)
2892{
2893 struct hda_gen_spec *spec = codec->spec;
2894 const struct hda_input_mux *imux;
2895 struct nid_path *path;
2896
2897 imux = &spec->input_mux;
2898 if (!imux->num_items)
2899 return 0;
2900
2901 if (idx >= imux->num_items)
2902 idx = imux->num_items - 1;
2903 if (spec->cur_mux[adc_idx] == idx)
2904 return 0;
2905
Takashi Iwaic697b712013-01-07 17:09:26 +01002906 path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002907 if (!path)
2908 return 0;
2909 if (path->active)
2910 snd_hda_activate_path(codec, path, false, false);
2911
2912 spec->cur_mux[adc_idx] = idx;
2913
2914 if (spec->shared_mic_hp)
2915 update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
2916
2917 if (spec->dyn_adc_switch)
2918 dyn_adc_pcm_resetup(codec, idx);
2919
Takashi Iwaic697b712013-01-07 17:09:26 +01002920 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002921 if (!path)
2922 return 0;
2923 if (path->active)
2924 return 0;
2925 snd_hda_activate_path(codec, path, true, false);
2926 if (spec->cap_sync_hook)
2927 spec->cap_sync_hook(codec);
2928 return 1;
2929}
2930
2931
2932/*
2933 * Jack detections for HP auto-mute and mic-switch
2934 */
2935
2936/* check each pin in the given array; returns true if any of them is plugged */
2937static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
2938{
2939 int i, present = 0;
2940
2941 for (i = 0; i < num_pins; i++) {
2942 hda_nid_t nid = pins[i];
2943 if (!nid)
2944 break;
Takashi Iwai0b4df932013-01-10 09:45:13 +01002945 /* don't detect pins retasked as inputs */
2946 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
2947 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002948 present |= snd_hda_jack_detect(codec, nid);
2949 }
2950 return present;
2951}
2952
2953/* standard HP/line-out auto-mute helper */
2954static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
Takashi Iwai2c12c302013-01-10 09:33:29 +01002955 bool mute)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002956{
2957 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002958 int i;
2959
2960 for (i = 0; i < num_pins; i++) {
2961 hda_nid_t nid = pins[i];
2962 unsigned int val;
2963 if (!nid)
2964 break;
2965 /* don't reset VREF value in case it's controlling
2966 * the amp (see alc861_fixup_asus_amp_vref_0f())
2967 */
Takashi Iwai2c12c302013-01-10 09:33:29 +01002968 if (spec->keep_vref_in_automute)
2969 val = snd_hda_codec_get_pin_target(codec, nid) & ~PIN_HP;
2970 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01002971 val = 0;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002972 if (!mute)
2973 val |= snd_hda_codec_get_pin_target(codec, nid);
2974 /* here we call update_pin_ctl() so that the pinctl is changed
2975 * without changing the pinctl target value;
2976 * the original target value will be still referred at the
2977 * init / resume again
2978 */
2979 update_pin_ctl(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002980 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002981 }
2982}
2983
2984/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002985void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002986{
2987 struct hda_gen_spec *spec = codec->spec;
2988 int on;
2989
2990 /* Control HP pins/amps depending on master_mute state;
2991 * in general, HP pins/amps control should be enabled in all cases,
2992 * but currently set only for master_mute, just to be safe
2993 */
2994 if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
2995 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01002996 spec->autocfg.hp_pins, spec->master_mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002997
2998 if (!spec->automute_speaker)
2999 on = 0;
3000 else
3001 on = spec->hp_jack_present | spec->line_jack_present;
3002 on |= spec->master_mute;
3003 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003004 spec->autocfg.speaker_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003005
3006 /* toggle line-out mutes if needed, too */
3007 /* if LO is a copy of either HP or Speaker, don't need to handle it */
3008 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
3009 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
3010 return;
3011 if (!spec->automute_lo)
3012 on = 0;
3013 else
3014 on = spec->hp_jack_present;
3015 on |= spec->master_mute;
3016 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003017 spec->autocfg.line_out_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003018}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003019EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003020
3021static void call_update_outputs(struct hda_codec *codec)
3022{
3023 struct hda_gen_spec *spec = codec->spec;
3024 if (spec->automute_hook)
3025 spec->automute_hook(codec);
3026 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01003027 snd_hda_gen_update_outputs(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003028}
3029
3030/* standard HP-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003031void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003032{
3033 struct hda_gen_spec *spec = codec->spec;
3034
3035 spec->hp_jack_present =
3036 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
3037 spec->autocfg.hp_pins);
3038 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
3039 return;
3040 call_update_outputs(codec);
3041}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003042EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003043
3044/* standard line-out-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003045void snd_hda_gen_line_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 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3050 return;
3051 /* check LO jack only when it's different from HP */
3052 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
3053 return;
3054
3055 spec->line_jack_present =
3056 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3057 spec->autocfg.line_out_pins);
3058 if (!spec->automute_speaker || !spec->detect_lo)
3059 return;
3060 call_update_outputs(codec);
3061}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003062EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003063
3064/* standard mic auto-switch helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003065void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003066{
3067 struct hda_gen_spec *spec = codec->spec;
3068 int i;
3069
3070 if (!spec->auto_mic)
3071 return;
3072
3073 for (i = spec->am_num_entries - 1; i > 0; i--) {
Takashi Iwai0b4df932013-01-10 09:45:13 +01003074 hda_nid_t pin = spec->am_entry[i].pin;
3075 /* don't detect pins retasked as outputs */
3076 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
3077 continue;
3078 if (snd_hda_jack_detect(codec, pin)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003079 mux_select(codec, 0, spec->am_entry[i].idx);
3080 return;
3081 }
3082 }
3083 mux_select(codec, 0, spec->am_entry[0].idx);
3084}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003085EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003086
3087/*
3088 * Auto-Mute mode mixer enum support
3089 */
3090static int automute_mode_info(struct snd_kcontrol *kcontrol,
3091 struct snd_ctl_elem_info *uinfo)
3092{
3093 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3094 struct hda_gen_spec *spec = codec->spec;
3095 static const char * const texts3[] = {
3096 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02003097 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098
Takashi Iwai352f7f92012-12-19 12:52:06 +01003099 if (spec->automute_speaker_possible && spec->automute_lo_possible)
3100 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
3101 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
3102}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103
Takashi Iwai352f7f92012-12-19 12:52:06 +01003104static int automute_mode_get(struct snd_kcontrol *kcontrol,
3105 struct snd_ctl_elem_value *ucontrol)
3106{
3107 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3108 struct hda_gen_spec *spec = codec->spec;
3109 unsigned int val = 0;
3110 if (spec->automute_speaker)
3111 val++;
3112 if (spec->automute_lo)
3113 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003114
Takashi Iwai352f7f92012-12-19 12:52:06 +01003115 ucontrol->value.enumerated.item[0] = val;
3116 return 0;
3117}
3118
3119static int automute_mode_put(struct snd_kcontrol *kcontrol,
3120 struct snd_ctl_elem_value *ucontrol)
3121{
3122 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3123 struct hda_gen_spec *spec = codec->spec;
3124
3125 switch (ucontrol->value.enumerated.item[0]) {
3126 case 0:
3127 if (!spec->automute_speaker && !spec->automute_lo)
3128 return 0;
3129 spec->automute_speaker = 0;
3130 spec->automute_lo = 0;
3131 break;
3132 case 1:
3133 if (spec->automute_speaker_possible) {
3134 if (!spec->automute_lo && spec->automute_speaker)
3135 return 0;
3136 spec->automute_speaker = 1;
3137 spec->automute_lo = 0;
3138 } else if (spec->automute_lo_possible) {
3139 if (spec->automute_lo)
3140 return 0;
3141 spec->automute_lo = 1;
3142 } else
3143 return -EINVAL;
3144 break;
3145 case 2:
3146 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
3147 return -EINVAL;
3148 if (spec->automute_speaker && spec->automute_lo)
3149 return 0;
3150 spec->automute_speaker = 1;
3151 spec->automute_lo = 1;
3152 break;
3153 default:
3154 return -EINVAL;
3155 }
3156 call_update_outputs(codec);
3157 return 1;
3158}
3159
3160static const struct snd_kcontrol_new automute_mode_enum = {
3161 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3162 .name = "Auto-Mute Mode",
3163 .info = automute_mode_info,
3164 .get = automute_mode_get,
3165 .put = automute_mode_put,
3166};
3167
3168static int add_automute_mode_enum(struct hda_codec *codec)
3169{
3170 struct hda_gen_spec *spec = codec->spec;
3171
Takashi Iwai12c93df2012-12-19 14:38:33 +01003172 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01003173 return -ENOMEM;
3174 return 0;
3175}
3176
3177/*
3178 * Check the availability of HP/line-out auto-mute;
3179 * Set up appropriately if really supported
3180 */
3181static int check_auto_mute_availability(struct hda_codec *codec)
3182{
3183 struct hda_gen_spec *spec = codec->spec;
3184 struct auto_pin_cfg *cfg = &spec->autocfg;
3185 int present = 0;
3186 int i, err;
3187
3188 if (cfg->hp_pins[0])
3189 present++;
3190 if (cfg->line_out_pins[0])
3191 present++;
3192 if (cfg->speaker_pins[0])
3193 present++;
3194 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02003195 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003196
3197 if (!cfg->speaker_pins[0] &&
3198 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
3199 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3200 sizeof(cfg->speaker_pins));
3201 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203
Takashi Iwai352f7f92012-12-19 12:52:06 +01003204 if (!cfg->hp_pins[0] &&
3205 cfg->line_out_type == AUTO_PIN_HP_OUT) {
3206 memcpy(cfg->hp_pins, cfg->line_out_pins,
3207 sizeof(cfg->hp_pins));
3208 cfg->hp_outs = cfg->line_outs;
3209 }
3210
3211 for (i = 0; i < cfg->hp_outs; i++) {
3212 hda_nid_t nid = cfg->hp_pins[i];
3213 if (!is_jack_detectable(codec, nid))
3214 continue;
3215 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
3216 nid);
3217 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003218 spec->hp_automute_hook ?
3219 spec->hp_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003220 snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003221 spec->detect_hp = 1;
3222 }
3223
3224 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
3225 if (cfg->speaker_outs)
3226 for (i = 0; i < cfg->line_outs; i++) {
3227 hda_nid_t nid = cfg->line_out_pins[i];
3228 if (!is_jack_detectable(codec, nid))
3229 continue;
3230 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
3231 snd_hda_jack_detect_enable_callback(codec, nid,
3232 HDA_GEN_FRONT_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003233 spec->line_automute_hook ?
3234 spec->line_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003235 snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003236 spec->detect_lo = 1;
3237 }
3238 spec->automute_lo_possible = spec->detect_hp;
3239 }
3240
3241 spec->automute_speaker_possible = cfg->speaker_outs &&
3242 (spec->detect_hp || spec->detect_lo);
3243
3244 spec->automute_lo = spec->automute_lo_possible;
3245 spec->automute_speaker = spec->automute_speaker_possible;
3246
3247 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
3248 /* create a control for automute mode */
3249 err = add_automute_mode_enum(codec);
3250 if (err < 0)
3251 return err;
3252 }
3253 return 0;
3254}
3255
Takashi Iwai352f7f92012-12-19 12:52:06 +01003256/* check whether all auto-mic pins are valid; setup indices if OK */
3257static bool auto_mic_check_imux(struct hda_codec *codec)
3258{
3259 struct hda_gen_spec *spec = codec->spec;
3260 const struct hda_input_mux *imux;
3261 int i;
3262
3263 imux = &spec->input_mux;
3264 for (i = 0; i < spec->am_num_entries; i++) {
3265 spec->am_entry[i].idx =
3266 find_idx_in_nid_list(spec->am_entry[i].pin,
3267 spec->imux_pins, imux->num_items);
3268 if (spec->am_entry[i].idx < 0)
3269 return false; /* no corresponding imux */
3270 }
3271
3272 /* we don't need the jack detection for the first pin */
3273 for (i = 1; i < spec->am_num_entries; i++)
3274 snd_hda_jack_detect_enable_callback(codec,
3275 spec->am_entry[i].pin,
3276 HDA_GEN_MIC_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003277 spec->mic_autoswitch_hook ?
3278 spec->mic_autoswitch_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003279 snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003280 return true;
3281}
3282
3283static int compare_attr(const void *ap, const void *bp)
3284{
3285 const struct automic_entry *a = ap;
3286 const struct automic_entry *b = bp;
3287 return (int)(a->attr - b->attr);
3288}
3289
3290/*
3291 * Check the availability of auto-mic switch;
3292 * Set up if really supported
3293 */
3294static int check_auto_mic_availability(struct hda_codec *codec)
3295{
3296 struct hda_gen_spec *spec = codec->spec;
3297 struct auto_pin_cfg *cfg = &spec->autocfg;
3298 unsigned int types;
3299 int i, num_pins;
3300
Takashi Iwaid12daf62013-01-07 16:32:11 +01003301 if (spec->suppress_auto_mic)
3302 return 0;
3303
Takashi Iwai352f7f92012-12-19 12:52:06 +01003304 types = 0;
3305 num_pins = 0;
3306 for (i = 0; i < cfg->num_inputs; i++) {
3307 hda_nid_t nid = cfg->inputs[i].pin;
3308 unsigned int attr;
3309 attr = snd_hda_codec_get_pincfg(codec, nid);
3310 attr = snd_hda_get_input_pin_attr(attr);
3311 if (types & (1 << attr))
3312 return 0; /* already occupied */
3313 switch (attr) {
3314 case INPUT_PIN_ATTR_INT:
3315 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3316 return 0; /* invalid type */
3317 break;
3318 case INPUT_PIN_ATTR_UNUSED:
3319 return 0; /* invalid entry */
3320 default:
3321 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
3322 return 0; /* invalid type */
3323 if (!spec->line_in_auto_switch &&
3324 cfg->inputs[i].type != AUTO_PIN_MIC)
3325 return 0; /* only mic is allowed */
3326 if (!is_jack_detectable(codec, nid))
3327 return 0; /* no unsol support */
3328 break;
3329 }
3330 if (num_pins >= MAX_AUTO_MIC_PINS)
3331 return 0;
3332 types |= (1 << attr);
3333 spec->am_entry[num_pins].pin = nid;
3334 spec->am_entry[num_pins].attr = attr;
3335 num_pins++;
3336 }
3337
3338 if (num_pins < 2)
3339 return 0;
3340
3341 spec->am_num_entries = num_pins;
3342 /* sort the am_entry in the order of attr so that the pin with a
3343 * higher attr will be selected when the jack is plugged.
3344 */
3345 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
3346 compare_attr, NULL);
3347
3348 if (!auto_mic_check_imux(codec))
3349 return 0;
3350
3351 spec->auto_mic = 1;
3352 spec->num_adc_nids = 1;
3353 spec->cur_mux[0] = spec->am_entry[0].idx;
3354 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
3355 spec->am_entry[0].pin,
3356 spec->am_entry[1].pin,
3357 spec->am_entry[2].pin);
3358
3359 return 0;
3360}
3361
3362
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003363/*
3364 * Parse the given BIOS configuration and set up the hda_gen_spec
3365 *
3366 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003367 * or a negative error code
3368 */
3369int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003370 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003371{
3372 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003373 int err;
3374
Takashi Iwai1c70a582013-01-11 17:48:22 +01003375 parse_user_hints(codec);
3376
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003377 if (cfg != &spec->autocfg) {
3378 spec->autocfg = *cfg;
3379 cfg = &spec->autocfg;
3380 }
3381
Takashi Iwai352f7f92012-12-19 12:52:06 +01003382 if (!cfg->line_outs) {
3383 if (cfg->dig_outs || cfg->dig_in_pin) {
3384 spec->multiout.max_channels = 2;
3385 spec->no_analog = 1;
3386 goto dig_only;
3387 }
3388 return 0; /* can't find valid BIOS pin config */
3389 }
3390
3391 if (!spec->no_primary_hp &&
3392 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
3393 cfg->line_outs <= cfg->hp_outs) {
3394 /* use HP as primary out */
3395 cfg->speaker_outs = cfg->line_outs;
3396 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3397 sizeof(cfg->speaker_pins));
3398 cfg->line_outs = cfg->hp_outs;
3399 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
3400 cfg->hp_outs = 0;
3401 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3402 cfg->line_out_type = AUTO_PIN_HP_OUT;
3403 }
3404
3405 err = parse_output_paths(codec);
3406 if (err < 0)
3407 return err;
3408 err = create_multi_channel_mode(codec);
3409 if (err < 0)
3410 return err;
3411 err = create_multi_out_ctls(codec, cfg);
3412 if (err < 0)
3413 return err;
3414 err = create_hp_out_ctls(codec);
3415 if (err < 0)
3416 return err;
3417 err = create_speaker_out_ctls(codec);
3418 if (err < 0)
3419 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003420 err = create_indep_hp_ctls(codec);
3421 if (err < 0)
3422 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01003423 err = create_loopback_mixing_ctl(codec);
3424 if (err < 0)
3425 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003426 err = create_shared_input(codec);
3427 if (err < 0)
3428 return err;
3429 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003430 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02003431 return err;
3432
Takashi Iwaia07a9492013-01-07 16:44:06 +01003433 spec->const_channel_count = spec->ext_channel_count;
3434 /* check the multiple speaker and headphone pins */
3435 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
3436 spec->const_channel_count = max(spec->const_channel_count,
3437 cfg->speaker_outs * 2);
3438 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
3439 spec->const_channel_count = max(spec->const_channel_count,
3440 cfg->hp_outs * 2);
3441 spec->multiout.max_channels = max(spec->ext_channel_count,
3442 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003443
3444 err = check_auto_mute_availability(codec);
3445 if (err < 0)
3446 return err;
3447
3448 err = check_dyn_adc_switch(codec);
3449 if (err < 0)
3450 return err;
3451
3452 if (!spec->shared_mic_hp) {
3453 err = check_auto_mic_availability(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003454 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 }
Takashi Iwai071c73a2006-08-23 18:34:06 +02003457
Takashi Iwai352f7f92012-12-19 12:52:06 +01003458 err = create_capture_mixers(codec);
3459 if (err < 0)
3460 return err;
3461
3462 err = parse_mic_boost(codec);
3463 if (err < 0)
3464 return err;
3465
Takashi Iwai978e77e2013-01-10 16:57:58 +01003466 if (spec->add_out_jack_modes) {
3467 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
3468 err = create_out_jack_modes(codec, cfg->line_outs,
3469 cfg->line_out_pins);
3470 if (err < 0)
3471 return err;
3472 }
3473 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
3474 err = create_out_jack_modes(codec, cfg->hp_outs,
3475 cfg->hp_pins);
3476 if (err < 0)
3477 return err;
3478 }
3479 }
3480
Takashi Iwai352f7f92012-12-19 12:52:06 +01003481 dig_only:
3482 parse_digital(codec);
3483
3484 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003486EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487
3488
3489/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003490 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003492
3493/* slave controls for virtual master */
3494static const char * const slave_pfxs[] = {
3495 "Front", "Surround", "Center", "LFE", "Side",
3496 "Headphone", "Speaker", "Mono", "Line Out",
3497 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01003498 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
3499 "Headphone Front", "Headphone Surround", "Headphone CLFE",
3500 "Headphone Side",
Takashi Iwai352f7f92012-12-19 12:52:06 +01003501 NULL,
3502};
3503
3504int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003506 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508
Takashi Iwai36502d02012-12-19 15:15:10 +01003509 if (spec->kctls.used) {
3510 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
3511 if (err < 0)
3512 return err;
3513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514
Takashi Iwai352f7f92012-12-19 12:52:06 +01003515 if (spec->multiout.dig_out_nid) {
3516 err = snd_hda_create_dig_out_ctls(codec,
3517 spec->multiout.dig_out_nid,
3518 spec->multiout.dig_out_nid,
3519 spec->pcm_rec[1].pcm_type);
3520 if (err < 0)
3521 return err;
3522 if (!spec->no_analog) {
3523 err = snd_hda_create_spdif_share_sw(codec,
3524 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 if (err < 0)
3526 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003527 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 }
3529 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003530 if (spec->dig_in_nid) {
3531 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
3532 if (err < 0)
3533 return err;
3534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535
Takashi Iwai352f7f92012-12-19 12:52:06 +01003536 /* if we have no master control, let's create it */
3537 if (!spec->no_analog &&
3538 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
3539 unsigned int vmaster_tlv[4];
3540 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
3541 HDA_OUTPUT, vmaster_tlv);
3542 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
3543 vmaster_tlv, slave_pfxs,
3544 "Playback Volume");
3545 if (err < 0)
3546 return err;
3547 }
3548 if (!spec->no_analog &&
3549 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
3550 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
3551 NULL, slave_pfxs,
3552 "Playback Switch",
3553 true, &spec->vmaster_mute.sw_kctl);
3554 if (err < 0)
3555 return err;
3556 if (spec->vmaster_mute.hook)
Takashi Iwaifd25a972012-12-20 14:57:18 +01003557 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
3558 spec->vmaster_mute_enum);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003559 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560
Takashi Iwai352f7f92012-12-19 12:52:06 +01003561 free_kctls(spec); /* no longer needed */
3562
3563 if (spec->shared_mic_hp) {
3564 int err;
3565 int nid = spec->autocfg.inputs[1].pin;
3566 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
3567 if (err < 0)
3568 return err;
3569 err = snd_hda_jack_detect_enable(codec, nid, 0);
3570 if (err < 0)
3571 return err;
3572 }
3573
3574 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
3575 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576 return err;
3577
3578 return 0;
3579}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003580EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
3581
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582
3583/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003584 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003587static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
3588 struct hda_codec *codec,
3589 struct snd_pcm_substream *substream,
3590 int action)
3591{
3592 struct hda_gen_spec *spec = codec->spec;
3593 if (spec->pcm_playback_hook)
3594 spec->pcm_playback_hook(hinfo, codec, substream, action);
3595}
3596
Takashi Iwai352f7f92012-12-19 12:52:06 +01003597/*
3598 * Analog playback callbacks
3599 */
3600static int playback_pcm_open(struct hda_pcm_stream *hinfo,
3601 struct hda_codec *codec,
3602 struct snd_pcm_substream *substream)
3603{
3604 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003605 int err;
3606
3607 mutex_lock(&spec->pcm_mutex);
3608 err = snd_hda_multi_out_analog_open(codec,
3609 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003610 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003611 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003612 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003613 call_pcm_playback_hook(hinfo, codec, substream,
3614 HDA_GEN_PCM_ACT_OPEN);
3615 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003616 mutex_unlock(&spec->pcm_mutex);
3617 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003618}
3619
3620static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01003621 struct hda_codec *codec,
3622 unsigned int stream_tag,
3623 unsigned int format,
3624 struct snd_pcm_substream *substream)
3625{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003626 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003627 int err;
3628
3629 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
3630 stream_tag, format, substream);
3631 if (!err)
3632 call_pcm_playback_hook(hinfo, codec, substream,
3633 HDA_GEN_PCM_ACT_PREPARE);
3634 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003635}
Takashi Iwai97ec5582006-03-21 11:29:07 +01003636
Takashi Iwai352f7f92012-12-19 12:52:06 +01003637static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3638 struct hda_codec *codec,
3639 struct snd_pcm_substream *substream)
3640{
3641 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003642 int err;
3643
3644 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
3645 if (!err)
3646 call_pcm_playback_hook(hinfo, codec, substream,
3647 HDA_GEN_PCM_ACT_CLEANUP);
3648 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003649}
3650
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003651static int playback_pcm_close(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;
3656 mutex_lock(&spec->pcm_mutex);
3657 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003658 call_pcm_playback_hook(hinfo, codec, substream,
3659 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003660 mutex_unlock(&spec->pcm_mutex);
3661 return 0;
3662}
3663
3664static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
3665 struct hda_codec *codec,
3666 struct snd_pcm_substream *substream)
3667{
3668 struct hda_gen_spec *spec = codec->spec;
3669 int err = 0;
3670
3671 mutex_lock(&spec->pcm_mutex);
3672 if (!spec->indep_hp_enabled)
3673 err = -EBUSY;
3674 else
3675 spec->active_streams |= 1 << STREAM_INDEP_HP;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003676 call_pcm_playback_hook(hinfo, codec, substream,
3677 HDA_GEN_PCM_ACT_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003678 mutex_unlock(&spec->pcm_mutex);
3679 return err;
3680}
3681
3682static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
3683 struct hda_codec *codec,
3684 struct snd_pcm_substream *substream)
3685{
3686 struct hda_gen_spec *spec = codec->spec;
3687 mutex_lock(&spec->pcm_mutex);
3688 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003689 call_pcm_playback_hook(hinfo, codec, substream,
3690 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003691 mutex_unlock(&spec->pcm_mutex);
3692 return 0;
3693}
3694
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003695static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3696 struct hda_codec *codec,
3697 unsigned int stream_tag,
3698 unsigned int format,
3699 struct snd_pcm_substream *substream)
3700{
3701 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3702 call_pcm_playback_hook(hinfo, codec, substream,
3703 HDA_GEN_PCM_ACT_PREPARE);
3704 return 0;
3705}
3706
3707static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3708 struct hda_codec *codec,
3709 struct snd_pcm_substream *substream)
3710{
3711 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
3712 call_pcm_playback_hook(hinfo, codec, substream,
3713 HDA_GEN_PCM_ACT_CLEANUP);
3714 return 0;
3715}
3716
Takashi Iwai352f7f92012-12-19 12:52:06 +01003717/*
3718 * Digital out
3719 */
3720static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
3721 struct hda_codec *codec,
3722 struct snd_pcm_substream *substream)
3723{
3724 struct hda_gen_spec *spec = codec->spec;
3725 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3726}
3727
3728static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3729 struct hda_codec *codec,
3730 unsigned int stream_tag,
3731 unsigned int format,
3732 struct snd_pcm_substream *substream)
3733{
3734 struct hda_gen_spec *spec = codec->spec;
3735 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3736 stream_tag, format, substream);
3737}
3738
3739static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3740 struct hda_codec *codec,
3741 struct snd_pcm_substream *substream)
3742{
3743 struct hda_gen_spec *spec = codec->spec;
3744 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
3745}
3746
3747static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
3748 struct hda_codec *codec,
3749 struct snd_pcm_substream *substream)
3750{
3751 struct hda_gen_spec *spec = codec->spec;
3752 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3753}
3754
3755/*
3756 * Analog capture
3757 */
3758static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3759 struct hda_codec *codec,
3760 unsigned int stream_tag,
3761 unsigned int format,
3762 struct snd_pcm_substream *substream)
3763{
3764 struct hda_gen_spec *spec = codec->spec;
3765
3766 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01003767 stream_tag, 0, format);
3768 return 0;
3769}
3770
Takashi Iwai352f7f92012-12-19 12:52:06 +01003771static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3772 struct hda_codec *codec,
3773 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01003774{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003775 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01003776
Takashi Iwai352f7f92012-12-19 12:52:06 +01003777 snd_hda_codec_cleanup_stream(codec,
3778 spec->adc_nids[substream->number + 1]);
Takashi Iwai97ec5582006-03-21 11:29:07 +01003779 return 0;
3780}
3781
Takashi Iwai352f7f92012-12-19 12:52:06 +01003782/*
3783 */
3784static const struct hda_pcm_stream pcm_analog_playback = {
3785 .substreams = 1,
3786 .channels_min = 2,
3787 .channels_max = 8,
3788 /* NID is set in build_pcms */
3789 .ops = {
3790 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003791 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003792 .prepare = playback_pcm_prepare,
3793 .cleanup = playback_pcm_cleanup
3794 },
3795};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796
Takashi Iwai352f7f92012-12-19 12:52:06 +01003797static const struct hda_pcm_stream pcm_analog_capture = {
3798 .substreams = 1,
3799 .channels_min = 2,
3800 .channels_max = 2,
3801 /* NID is set in build_pcms */
3802};
3803
3804static const struct hda_pcm_stream pcm_analog_alt_playback = {
3805 .substreams = 1,
3806 .channels_min = 2,
3807 .channels_max = 2,
3808 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003809 .ops = {
3810 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003811 .close = alt_playback_pcm_close,
3812 .prepare = alt_playback_pcm_prepare,
3813 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003814 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01003815};
3816
3817static const struct hda_pcm_stream pcm_analog_alt_capture = {
3818 .substreams = 2, /* can be overridden */
3819 .channels_min = 2,
3820 .channels_max = 2,
3821 /* NID is set in build_pcms */
3822 .ops = {
3823 .prepare = alt_capture_pcm_prepare,
3824 .cleanup = alt_capture_pcm_cleanup
3825 },
3826};
3827
3828static const struct hda_pcm_stream pcm_digital_playback = {
3829 .substreams = 1,
3830 .channels_min = 2,
3831 .channels_max = 2,
3832 /* NID is set in build_pcms */
3833 .ops = {
3834 .open = dig_playback_pcm_open,
3835 .close = dig_playback_pcm_close,
3836 .prepare = dig_playback_pcm_prepare,
3837 .cleanup = dig_playback_pcm_cleanup
3838 },
3839};
3840
3841static const struct hda_pcm_stream pcm_digital_capture = {
3842 .substreams = 1,
3843 .channels_min = 2,
3844 .channels_max = 2,
3845 /* NID is set in build_pcms */
3846};
3847
3848/* Used by build_pcms to flag that a PCM has no playback stream */
3849static const struct hda_pcm_stream pcm_null_stream = {
3850 .substreams = 0,
3851 .channels_min = 0,
3852 .channels_max = 0,
3853};
3854
3855/*
3856 * dynamic changing ADC PCM streams
3857 */
3858static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
3859{
3860 struct hda_gen_spec *spec = codec->spec;
3861 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
3862
3863 if (spec->cur_adc && spec->cur_adc != new_adc) {
3864 /* stream is running, let's swap the current ADC */
3865 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
3866 spec->cur_adc = new_adc;
3867 snd_hda_codec_setup_stream(codec, new_adc,
3868 spec->cur_adc_stream_tag, 0,
3869 spec->cur_adc_format);
3870 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003872 return false;
3873}
3874
3875/* analog capture with dynamic dual-adc changes */
3876static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3877 struct hda_codec *codec,
3878 unsigned int stream_tag,
3879 unsigned int format,
3880 struct snd_pcm_substream *substream)
3881{
3882 struct hda_gen_spec *spec = codec->spec;
3883 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
3884 spec->cur_adc_stream_tag = stream_tag;
3885 spec->cur_adc_format = format;
3886 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
3887 return 0;
3888}
3889
3890static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3891 struct hda_codec *codec,
3892 struct snd_pcm_substream *substream)
3893{
3894 struct hda_gen_spec *spec = codec->spec;
3895 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
3896 spec->cur_adc = 0;
3897 return 0;
3898}
3899
3900static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
3901 .substreams = 1,
3902 .channels_min = 2,
3903 .channels_max = 2,
3904 .nid = 0, /* fill later */
3905 .ops = {
3906 .prepare = dyn_adc_capture_pcm_prepare,
3907 .cleanup = dyn_adc_capture_pcm_cleanup
3908 },
3909};
3910
Takashi Iwaif873e532012-12-20 16:58:39 +01003911static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
3912 const char *chip_name)
3913{
3914 char *p;
3915
3916 if (*str)
3917 return;
3918 strlcpy(str, chip_name, len);
3919
3920 /* drop non-alnum chars after a space */
3921 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
3922 if (!isalnum(p[1])) {
3923 *p = 0;
3924 break;
3925 }
3926 }
3927 strlcat(str, sfx, len);
3928}
3929
Takashi Iwai352f7f92012-12-19 12:52:06 +01003930/* build PCM streams based on the parsed results */
3931int snd_hda_gen_build_pcms(struct hda_codec *codec)
3932{
3933 struct hda_gen_spec *spec = codec->spec;
3934 struct hda_pcm *info = spec->pcm_rec;
3935 const struct hda_pcm_stream *p;
3936 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937
3938 codec->num_pcms = 1;
3939 codec->pcm_info = info;
3940
Takashi Iwai352f7f92012-12-19 12:52:06 +01003941 if (spec->no_analog)
3942 goto skip_analog;
3943
Takashi Iwaif873e532012-12-20 16:58:39 +01003944 fill_pcm_stream_name(spec->stream_name_analog,
3945 sizeof(spec->stream_name_analog),
3946 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003947 info->name = spec->stream_name_analog;
3948
3949 if (spec->multiout.num_dacs > 0) {
3950 p = spec->stream_analog_playback;
3951 if (!p)
3952 p = &pcm_analog_playback;
3953 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3954 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
3955 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
3956 spec->multiout.max_channels;
3957 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
3958 spec->autocfg.line_outs == 2)
3959 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
3960 snd_pcm_2_1_chmaps;
3961 }
3962 if (spec->num_adc_nids) {
3963 p = spec->stream_analog_capture;
3964 if (!p) {
3965 if (spec->dyn_adc_switch)
3966 p = &dyn_adc_pcm_analog_capture;
3967 else
3968 p = &pcm_analog_capture;
3969 }
3970 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3971 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
3972 }
3973
Takashi Iwai352f7f92012-12-19 12:52:06 +01003974 skip_analog:
3975 /* SPDIF for stream index #1 */
3976 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01003977 fill_pcm_stream_name(spec->stream_name_digital,
3978 sizeof(spec->stream_name_digital),
3979 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003980 codec->num_pcms = 2;
3981 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
3982 info = spec->pcm_rec + 1;
3983 info->name = spec->stream_name_digital;
3984 if (spec->dig_out_type)
3985 info->pcm_type = spec->dig_out_type;
3986 else
3987 info->pcm_type = HDA_PCM_TYPE_SPDIF;
3988 if (spec->multiout.dig_out_nid) {
3989 p = spec->stream_digital_playback;
3990 if (!p)
3991 p = &pcm_digital_playback;
3992 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3993 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
3994 }
3995 if (spec->dig_in_nid) {
3996 p = spec->stream_digital_capture;
3997 if (!p)
3998 p = &pcm_digital_capture;
3999 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4000 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
4001 }
4002 }
4003
4004 if (spec->no_analog)
4005 return 0;
4006
4007 /* If the use of more than one ADC is requested for the current
4008 * model, configure a second analog capture-only PCM.
4009 */
4010 have_multi_adcs = (spec->num_adc_nids > 1) &&
4011 !spec->dyn_adc_switch && !spec->auto_mic;
4012 /* Additional Analaog capture for index #2 */
4013 if (spec->alt_dac_nid || have_multi_adcs) {
4014 codec->num_pcms = 3;
4015 info = spec->pcm_rec + 2;
4016 info->name = spec->stream_name_analog;
4017 if (spec->alt_dac_nid) {
4018 p = spec->stream_analog_alt_playback;
4019 if (!p)
4020 p = &pcm_analog_alt_playback;
4021 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4022 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
4023 spec->alt_dac_nid;
4024 } else {
4025 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
4026 pcm_null_stream;
4027 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
4028 }
4029 if (have_multi_adcs) {
4030 p = spec->stream_analog_alt_capture;
4031 if (!p)
4032 p = &pcm_analog_alt_capture;
4033 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4034 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
4035 spec->adc_nids[1];
4036 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
4037 spec->num_adc_nids - 1;
4038 } else {
4039 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
4040 pcm_null_stream;
4041 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
4042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 }
4044
4045 return 0;
4046}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004047EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
4048
4049
4050/*
4051 * Standard auto-parser initializations
4052 */
4053
Takashi Iwaid4156932013-01-07 10:08:02 +01004054/* configure the given path as a proper output */
Takashi Iwai2c12c302013-01-10 09:33:29 +01004055static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004056{
4057 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01004058 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004059
Takashi Iwai196c17662013-01-04 15:01:40 +01004060 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01004061 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004062 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01004063 pin = path->path[path->depth - 1];
Takashi Iwai2c12c302013-01-10 09:33:29 +01004064 restore_pin_ctl(codec, pin);
Takashi Iwaie1284af2013-01-03 16:33:02 +01004065 snd_hda_activate_path(codec, path, path->active, true);
4066 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004067}
4068
4069/* initialize primary output paths */
4070static void init_multi_out(struct hda_codec *codec)
4071{
4072 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004073 int i;
4074
Takashi Iwaid4156932013-01-07 10:08:02 +01004075 for (i = 0; i < spec->autocfg.line_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004076 set_output_and_unmute(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004077}
4078
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004079
Takashi Iwai2c12c302013-01-10 09:33:29 +01004080static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004081{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004082 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004083
Takashi Iwaid4156932013-01-07 10:08:02 +01004084 for (i = 0; i < num_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004085 set_output_and_unmute(codec, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004086}
4087
4088/* initialize hp and speaker paths */
4089static void init_extra_out(struct hda_codec *codec)
4090{
4091 struct hda_gen_spec *spec = codec->spec;
4092
4093 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004094 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004095 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
4096 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai2c12c302013-01-10 09:33:29 +01004097 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004098}
4099
4100/* initialize multi-io paths */
4101static void init_multi_io(struct hda_codec *codec)
4102{
4103 struct hda_gen_spec *spec = codec->spec;
4104 int i;
4105
4106 for (i = 0; i < spec->multi_ios; i++) {
4107 hda_nid_t pin = spec->multi_io[i].pin;
4108 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01004109 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004110 if (!path)
4111 continue;
4112 if (!spec->multi_io[i].ctl_in)
4113 spec->multi_io[i].ctl_in =
Takashi Iwai2c12c302013-01-10 09:33:29 +01004114 snd_hda_codec_get_pin_target(codec, pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004115 snd_hda_activate_path(codec, path, path->active, true);
4116 }
4117}
4118
Takashi Iwai352f7f92012-12-19 12:52:06 +01004119/* set up input pins and loopback paths */
4120static void init_analog_input(struct hda_codec *codec)
4121{
4122 struct hda_gen_spec *spec = codec->spec;
4123 struct auto_pin_cfg *cfg = &spec->autocfg;
4124 int i;
4125
4126 for (i = 0; i < cfg->num_inputs; i++) {
4127 hda_nid_t nid = cfg->inputs[i].pin;
4128 if (is_input_pin(codec, nid))
Takashi Iwai2c12c302013-01-10 09:33:29 +01004129 restore_pin_ctl(codec, nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004130
4131 /* init loopback inputs */
4132 if (spec->mixer_nid) {
4133 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01004134 path = snd_hda_get_path_from_idx(codec, spec->loopback_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004135 if (path)
4136 snd_hda_activate_path(codec, path,
4137 path->active, false);
4138 }
4139 }
4140}
4141
4142/* initialize ADC paths */
4143static void init_input_src(struct hda_codec *codec)
4144{
4145 struct hda_gen_spec *spec = codec->spec;
4146 struct hda_input_mux *imux = &spec->input_mux;
4147 struct nid_path *path;
4148 int i, c, nums;
4149
4150 if (spec->dyn_adc_switch)
4151 nums = 1;
4152 else
4153 nums = spec->num_adc_nids;
4154
4155 for (c = 0; c < nums; c++) {
4156 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01004157 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004158 if (path) {
4159 bool active = path->active;
4160 if (i == spec->cur_mux[c])
4161 active = true;
4162 snd_hda_activate_path(codec, path, active, false);
4163 }
4164 }
4165 }
4166
4167 if (spec->shared_mic_hp)
4168 update_shared_mic_hp(codec, spec->cur_mux[0]);
4169
4170 if (spec->cap_sync_hook)
4171 spec->cap_sync_hook(codec);
4172}
4173
4174/* set right pin controls for digital I/O */
4175static void init_digital(struct hda_codec *codec)
4176{
4177 struct hda_gen_spec *spec = codec->spec;
4178 int i;
4179 hda_nid_t pin;
4180
Takashi Iwaid4156932013-01-07 10:08:02 +01004181 for (i = 0; i < spec->autocfg.dig_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004182 set_output_and_unmute(codec, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004183 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01004184 if (pin) {
4185 struct nid_path *path;
Takashi Iwai2c12c302013-01-10 09:33:29 +01004186 restore_pin_ctl(codec, pin);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01004187 path = snd_hda_get_path_from_idx(codec, spec->digin_path);
4188 if (path)
4189 snd_hda_activate_path(codec, path, path->active, false);
4190 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004191}
4192
Takashi Iwai973e4972012-12-20 15:16:09 +01004193/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
4194 * invalid unsol tags by some reason
4195 */
4196static void clear_unsol_on_unused_pins(struct hda_codec *codec)
4197{
4198 int i;
4199
4200 for (i = 0; i < codec->init_pins.used; i++) {
4201 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
4202 hda_nid_t nid = pin->nid;
4203 if (is_jack_detectable(codec, nid) &&
4204 !snd_hda_jack_tbl_get(codec, nid))
4205 snd_hda_codec_update_cache(codec, nid, 0,
4206 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
4207 }
4208}
4209
Takashi Iwai5187ac12013-01-07 12:52:16 +01004210/*
4211 * initialize the generic spec;
4212 * this can be put as patch_ops.init function
4213 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004214int snd_hda_gen_init(struct hda_codec *codec)
4215{
4216 struct hda_gen_spec *spec = codec->spec;
4217
4218 if (spec->init_hook)
4219 spec->init_hook(codec);
4220
4221 snd_hda_apply_verbs(codec);
4222
Takashi Iwai3bbcd272012-12-20 11:50:58 +01004223 codec->cached_write = 1;
4224
Takashi Iwai352f7f92012-12-19 12:52:06 +01004225 init_multi_out(codec);
4226 init_extra_out(codec);
4227 init_multi_io(codec);
4228 init_analog_input(codec);
4229 init_input_src(codec);
4230 init_digital(codec);
4231
Takashi Iwai973e4972012-12-20 15:16:09 +01004232 clear_unsol_on_unused_pins(codec);
4233
Takashi Iwai352f7f92012-12-19 12:52:06 +01004234 /* call init functions of standard auto-mute helpers */
Takashi Iwai5d550e12012-12-19 15:16:44 +01004235 snd_hda_gen_hp_automute(codec, NULL);
4236 snd_hda_gen_line_automute(codec, NULL);
4237 snd_hda_gen_mic_autoswitch(codec, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004238
Takashi Iwai3bbcd272012-12-20 11:50:58 +01004239 snd_hda_codec_flush_amp_cache(codec);
4240 snd_hda_codec_flush_cmd_cache(codec);
4241
Takashi Iwai352f7f92012-12-19 12:52:06 +01004242 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
4243 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
4244
4245 hda_call_check_power_status(codec, 0x01);
4246 return 0;
4247}
Takashi Iwaifce52a32013-01-07 12:42:48 +01004248EXPORT_SYMBOL_HDA(snd_hda_gen_init);
4249
Takashi Iwai5187ac12013-01-07 12:52:16 +01004250/*
4251 * free the generic spec;
4252 * this can be put as patch_ops.free function
4253 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01004254void snd_hda_gen_free(struct hda_codec *codec)
4255{
4256 snd_hda_gen_spec_free(codec->spec);
4257 kfree(codec->spec);
4258 codec->spec = NULL;
4259}
4260EXPORT_SYMBOL_HDA(snd_hda_gen_free);
4261
4262#ifdef CONFIG_PM
Takashi Iwai5187ac12013-01-07 12:52:16 +01004263/*
4264 * check the loopback power save state;
4265 * this can be put as patch_ops.check_power_status function
4266 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01004267int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
4268{
4269 struct hda_gen_spec *spec = codec->spec;
4270 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
4271}
4272EXPORT_SYMBOL_HDA(snd_hda_gen_check_power_status);
4273#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01004274
4275
4276/*
4277 * the generic codec support
4278 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279
Takashi Iwai352f7f92012-12-19 12:52:06 +01004280static const struct hda_codec_ops generic_patch_ops = {
4281 .build_controls = snd_hda_gen_build_controls,
4282 .build_pcms = snd_hda_gen_build_pcms,
4283 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01004284 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004285 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02004286#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01004287 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02004288#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289};
4290
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291int snd_hda_parse_generic_codec(struct hda_codec *codec)
4292{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004293 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 int err;
4295
Takashi Iwaie560d8d2005-09-09 14:21:46 +02004296 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004297 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004299 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004302 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
4303 if (err < 0)
4304 return err;
4305
4306 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004307 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 goto error;
4309
4310 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 return 0;
4312
Takashi Iwai352f7f92012-12-19 12:52:06 +01004313error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01004314 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 return err;
4316}
Takashi Iwaifce52a32013-01-07 12:42:48 +01004317EXPORT_SYMBOL_HDA(snd_hda_parse_generic_codec);