blob: 05dfeb7bfc970fdbe913b5c89a8d7ca8a68dfa15 [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;
Takashi Iwaiea46c3c2013-01-15 18:45:53 +01001287 unsigned int val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001288
1289 /* set num_dacs once to full for look_for_dac() */
1290 spec->multiout.num_dacs = cfg->line_outs;
1291 spec->multiout.dac_nids = spec->private_dac_nids;
1292 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1293 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1294 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1295 spec->multi_ios = 0;
1296 snd_array_free(&spec->paths);
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001297
1298 /* clear path indices */
1299 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1300 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1301 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1302 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1303 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
Takashi Iwaic697b712013-01-07 17:09:26 +01001304 memset(spec->input_paths, 0, sizeof(spec->input_paths));
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001305 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1306 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1307
Takashi Iwai352f7f92012-12-19 12:52:06 +01001308 badness = 0;
1309
1310 /* fill hard-wired DACs first */
1311 if (fill_hardwired) {
1312 bool mapped;
1313 do {
1314 mapped = map_singles(codec, cfg->line_outs,
1315 cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001316 spec->private_dac_nids,
1317 spec->out_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001318 mapped |= map_singles(codec, cfg->hp_outs,
1319 cfg->hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001320 spec->multiout.hp_out_nid,
1321 spec->hp_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001322 mapped |= map_singles(codec, cfg->speaker_outs,
1323 cfg->speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001324 spec->multiout.extra_out_nid,
1325 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001326 if (fill_mio_first && cfg->line_outs == 1 &&
1327 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001328 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001329 if (!err)
1330 mapped = true;
1331 }
1332 } while (mapped);
1333 }
1334
1335 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001336 spec->private_dac_nids, spec->out_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001337 &main_out_badness);
1338
Takashi Iwai352f7f92012-12-19 12:52:06 +01001339 if (fill_mio_first &&
1340 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1341 /* try to fill multi-io first */
Takashi Iwaie22aab72013-01-04 14:50:04 +01001342 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001343 if (err < 0)
1344 return err;
1345 /* we don't count badness at this stage yet */
1346 }
1347
1348 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1349 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1350 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001351 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001352 &extra_out_badness);
1353 if (err < 0)
1354 return err;
1355 badness += err;
1356 }
1357 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1358 err = try_assign_dacs(codec, cfg->speaker_outs,
1359 cfg->speaker_pins,
1360 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001361 spec->speaker_paths,
1362 &extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001363 if (err < 0)
1364 return err;
1365 badness += err;
1366 }
1367 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001368 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001369 if (err < 0)
1370 return err;
1371 badness += err;
1372 }
Takashi Iwaie22aab72013-01-04 14:50:04 +01001373
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001374 if (spec->mixer_nid) {
1375 spec->aamix_out_paths[0] =
1376 check_aamix_out_path(codec, spec->out_paths[0]);
1377 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1378 spec->aamix_out_paths[1] =
1379 check_aamix_out_path(codec, spec->hp_paths[0]);
1380 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1381 spec->aamix_out_paths[2] =
1382 check_aamix_out_path(codec, spec->speaker_paths[0]);
1383 }
1384
Takashi Iwaie22aab72013-01-04 14:50:04 +01001385 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1386 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1387 spec->multi_ios = 1; /* give badness */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001388
Takashi Iwaia07a9492013-01-07 16:44:06 +01001389 /* re-count num_dacs and squash invalid entries */
1390 spec->multiout.num_dacs = 0;
1391 for (i = 0; i < cfg->line_outs; i++) {
1392 if (spec->private_dac_nids[i])
1393 spec->multiout.num_dacs++;
1394 else {
1395 memmove(spec->private_dac_nids + i,
1396 spec->private_dac_nids + i + 1,
1397 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1398 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1399 }
1400 }
1401
1402 spec->ext_channel_count = spec->min_channel_count =
David Henningssonc0f3b212013-01-16 11:45:37 +01001403 spec->multiout.num_dacs * 2;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001404
Takashi Iwai352f7f92012-12-19 12:52:06 +01001405 if (spec->multi_ios == 2) {
1406 for (i = 0; i < 2; i++)
1407 spec->private_dac_nids[spec->multiout.num_dacs++] =
1408 spec->multi_io[i].dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001409 } else if (spec->multi_ios) {
1410 spec->multi_ios = 0;
1411 badness += BAD_MULTI_IO;
1412 }
1413
Takashi Iwaia07a9492013-01-07 16:44:06 +01001414 /* re-fill the shared DAC for speaker / headphone */
1415 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1416 refill_shared_dacs(codec, cfg->hp_outs,
1417 spec->multiout.hp_out_nid,
1418 spec->hp_paths);
1419 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1420 refill_shared_dacs(codec, cfg->speaker_outs,
1421 spec->multiout.extra_out_nid,
1422 spec->speaker_paths);
1423
Takashi Iwai2c12c302013-01-10 09:33:29 +01001424 /* set initial pinctl targets */
Takashi Iwaiea46c3c2013-01-15 18:45:53 +01001425 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
1426 val = PIN_HP;
1427 else
1428 val = PIN_OUT;
1429 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
Takashi Iwai2c12c302013-01-10 09:33:29 +01001430 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1431 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
Takashi Iwaiea46c3c2013-01-15 18:45:53 +01001432 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1433 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
Takashi Iwai2c12c302013-01-10 09:33:29 +01001434 set_pin_targets(codec, cfg->speaker_outs,
Takashi Iwaiea46c3c2013-01-15 18:45:53 +01001435 cfg->speaker_pins, val);
1436 }
Takashi Iwai2c12c302013-01-10 09:33:29 +01001437
Takashi Iwai352f7f92012-12-19 12:52:06 +01001438 return badness;
1439}
1440
1441#define DEBUG_BADNESS
1442
1443#ifdef DEBUG_BADNESS
1444#define debug_badness snd_printdd
1445#else
1446#define debug_badness(...)
1447#endif
1448
1449static void debug_show_configs(struct hda_gen_spec *spec, struct auto_pin_cfg *cfg)
1450{
1451 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1452 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001453 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001454 spec->multiout.dac_nids[0],
1455 spec->multiout.dac_nids[1],
1456 spec->multiout.dac_nids[2],
1457 spec->multiout.dac_nids[3]);
1458 if (spec->multi_ios > 0)
1459 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1460 spec->multi_ios,
1461 spec->multi_io[0].pin, spec->multi_io[1].pin,
1462 spec->multi_io[0].dac, spec->multi_io[1].dac);
1463 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1464 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001465 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001466 spec->multiout.hp_out_nid[0],
1467 spec->multiout.hp_out_nid[1],
1468 spec->multiout.hp_out_nid[2],
1469 spec->multiout.hp_out_nid[3]);
1470 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1471 cfg->speaker_pins[0], cfg->speaker_pins[1],
1472 cfg->speaker_pins[2], cfg->speaker_pins[3],
1473 spec->multiout.extra_out_nid[0],
1474 spec->multiout.extra_out_nid[1],
1475 spec->multiout.extra_out_nid[2],
1476 spec->multiout.extra_out_nid[3]);
1477}
1478
1479/* find all available DACs of the codec */
1480static void fill_all_dac_nids(struct hda_codec *codec)
1481{
1482 struct hda_gen_spec *spec = codec->spec;
1483 int i;
1484 hda_nid_t nid = codec->start_nid;
1485
1486 spec->num_all_dacs = 0;
1487 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1488 for (i = 0; i < codec->num_nodes; i++, nid++) {
1489 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1490 continue;
1491 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1492 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1493 break;
1494 }
1495 spec->all_dacs[spec->num_all_dacs++] = nid;
1496 }
1497}
1498
1499static int parse_output_paths(struct hda_codec *codec)
1500{
1501 struct hda_gen_spec *spec = codec->spec;
1502 struct auto_pin_cfg *cfg = &spec->autocfg;
1503 struct auto_pin_cfg *best_cfg;
1504 int best_badness = INT_MAX;
1505 int badness;
1506 bool fill_hardwired = true, fill_mio_first = true;
1507 bool best_wired = true, best_mio = true;
1508 bool hp_spk_swapped = false;
1509
1510 fill_all_dac_nids(codec);
1511
1512 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1513 if (!best_cfg)
1514 return -ENOMEM;
1515 *best_cfg = *cfg;
1516
1517 for (;;) {
1518 badness = fill_and_eval_dacs(codec, fill_hardwired,
1519 fill_mio_first);
1520 if (badness < 0) {
1521 kfree(best_cfg);
1522 return badness;
1523 }
1524 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1525 cfg->line_out_type, fill_hardwired, fill_mio_first,
1526 badness);
1527 debug_show_configs(spec, cfg);
1528 if (badness < best_badness) {
1529 best_badness = badness;
1530 *best_cfg = *cfg;
1531 best_wired = fill_hardwired;
1532 best_mio = fill_mio_first;
1533 }
1534 if (!badness)
1535 break;
1536 fill_mio_first = !fill_mio_first;
1537 if (!fill_mio_first)
1538 continue;
1539 fill_hardwired = !fill_hardwired;
1540 if (!fill_hardwired)
1541 continue;
1542 if (hp_spk_swapped)
1543 break;
1544 hp_spk_swapped = true;
1545 if (cfg->speaker_outs > 0 &&
1546 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1547 cfg->hp_outs = cfg->line_outs;
1548 memcpy(cfg->hp_pins, cfg->line_out_pins,
1549 sizeof(cfg->hp_pins));
1550 cfg->line_outs = cfg->speaker_outs;
1551 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1552 sizeof(cfg->speaker_pins));
1553 cfg->speaker_outs = 0;
1554 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1555 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1556 fill_hardwired = true;
1557 continue;
1558 }
1559 if (cfg->hp_outs > 0 &&
1560 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1561 cfg->speaker_outs = cfg->line_outs;
1562 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1563 sizeof(cfg->speaker_pins));
1564 cfg->line_outs = cfg->hp_outs;
1565 memcpy(cfg->line_out_pins, cfg->hp_pins,
1566 sizeof(cfg->hp_pins));
1567 cfg->hp_outs = 0;
1568 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1569 cfg->line_out_type = AUTO_PIN_HP_OUT;
1570 fill_hardwired = true;
1571 continue;
1572 }
1573 break;
1574 }
1575
1576 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001577 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001578 *cfg = *best_cfg;
1579 fill_and_eval_dacs(codec, best_wired, best_mio);
1580 }
1581 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1582 cfg->line_out_type, best_wired, best_mio);
1583 debug_show_configs(spec, cfg);
1584
1585 if (cfg->line_out_pins[0]) {
1586 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01001587 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001588 if (path)
1589 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
1590 }
1591
1592 kfree(best_cfg);
1593 return 0;
1594}
1595
1596/* add playback controls from the parsed DAC table */
1597static int create_multi_out_ctls(struct hda_codec *codec,
1598 const struct auto_pin_cfg *cfg)
1599{
1600 struct hda_gen_spec *spec = codec->spec;
1601 int i, err, noutputs;
1602
1603 noutputs = cfg->line_outs;
1604 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1605 noutputs += spec->multi_ios;
1606
1607 for (i = 0; i < noutputs; i++) {
1608 const char *name;
1609 int index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001610 struct nid_path *path;
1611
Takashi Iwai352f7f92012-12-19 12:52:06 +01001612 if (i >= cfg->line_outs) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001613 index = 0;
1614 name = channel_name[i];
1615 } else {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001616 name = get_line_out_pfx(spec, i, true, &index);
1617 }
1618
Takashi Iwai196c17662013-01-04 15:01:40 +01001619 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001620 if (!path)
1621 continue;
1622 if (!name || !strcmp(name, "CLFE")) {
1623 /* Center/LFE */
1624 err = add_vol_ctl(codec, "Center", 0, 1, path);
1625 if (err < 0)
1626 return err;
1627 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1628 if (err < 0)
1629 return err;
1630 err = add_sw_ctl(codec, "Center", 0, 1, path);
1631 if (err < 0)
1632 return err;
1633 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1634 if (err < 0)
1635 return err;
1636 } else {
1637 err = add_stereo_vol(codec, name, index, path);
1638 if (err < 0)
1639 return err;
1640 err = add_stereo_sw(codec, name, index, path);
1641 if (err < 0)
1642 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 }
1644 }
1645 return 0;
1646}
1647
Takashi Iwaic2c80382013-01-07 10:33:57 +01001648static int create_extra_out(struct hda_codec *codec, int path_idx,
Takashi Iwai196c17662013-01-04 15:01:40 +01001649 const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001651 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 int err;
1653
Takashi Iwai196c17662013-01-04 15:01:40 +01001654 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001655 if (!path)
1656 return 0;
Takashi Iwaic2c80382013-01-07 10:33:57 +01001657 err = add_stereo_vol(codec, pfx, cidx, path);
1658 if (err < 0)
1659 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001660 err = add_stereo_sw(codec, pfx, cidx, path);
1661 if (err < 0)
1662 return err;
1663 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664}
1665
Takashi Iwai352f7f92012-12-19 12:52:06 +01001666/* add playback controls for speaker and HP outputs */
1667static int create_extra_outs(struct hda_codec *codec, int num_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001668 const int *paths, const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669{
Takashi Iwaic2c80382013-01-07 10:33:57 +01001670 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001671
1672 for (i = 0; i < num_pins; i++) {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001673 const char *name;
1674 char tmp[44];
1675 int err, idx = 0;
1676
1677 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
1678 name = "Bass Speaker";
1679 else if (num_pins >= 3) {
1680 snprintf(tmp, sizeof(tmp), "%s %s",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001681 pfx, channel_name[i]);
Takashi Iwaic2c80382013-01-07 10:33:57 +01001682 name = tmp;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001683 } else {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001684 name = pfx;
1685 idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 }
Takashi Iwaic2c80382013-01-07 10:33:57 +01001687 err = create_extra_out(codec, paths[i], name, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001688 if (err < 0)
1689 return err;
1690 }
1691 return 0;
1692}
Takashi Iwai97ec5582006-03-21 11:29:07 +01001693
Takashi Iwai352f7f92012-12-19 12:52:06 +01001694static int create_hp_out_ctls(struct hda_codec *codec)
1695{
1696 struct hda_gen_spec *spec = codec->spec;
1697 return create_extra_outs(codec, spec->autocfg.hp_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001698 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001699 "Headphone");
1700}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
Takashi Iwai352f7f92012-12-19 12:52:06 +01001702static int create_speaker_out_ctls(struct hda_codec *codec)
1703{
1704 struct hda_gen_spec *spec = codec->spec;
1705 return create_extra_outs(codec, spec->autocfg.speaker_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001706 spec->speaker_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001707 "Speaker");
1708}
1709
1710/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001711 * independent HP controls
1712 */
1713
1714static int indep_hp_info(struct snd_kcontrol *kcontrol,
1715 struct snd_ctl_elem_info *uinfo)
1716{
1717 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
1718}
1719
1720static int indep_hp_get(struct snd_kcontrol *kcontrol,
1721 struct snd_ctl_elem_value *ucontrol)
1722{
1723 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1724 struct hda_gen_spec *spec = codec->spec;
1725 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
1726 return 0;
1727}
1728
1729static int indep_hp_put(struct snd_kcontrol *kcontrol,
1730 struct snd_ctl_elem_value *ucontrol)
1731{
1732 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1733 struct hda_gen_spec *spec = codec->spec;
1734 unsigned int select = ucontrol->value.enumerated.item[0];
1735 int ret = 0;
1736
1737 mutex_lock(&spec->pcm_mutex);
1738 if (spec->active_streams) {
1739 ret = -EBUSY;
1740 goto unlock;
1741 }
1742
1743 if (spec->indep_hp_enabled != select) {
1744 spec->indep_hp_enabled = select;
1745 if (spec->indep_hp_enabled)
1746 spec->multiout.hp_out_nid[0] = 0;
1747 else
1748 spec->multiout.hp_out_nid[0] = spec->alt_dac_nid;
1749 ret = 1;
1750 }
1751 unlock:
1752 mutex_unlock(&spec->pcm_mutex);
1753 return ret;
1754}
1755
1756static const struct snd_kcontrol_new indep_hp_ctl = {
1757 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1758 .name = "Independent HP",
1759 .info = indep_hp_info,
1760 .get = indep_hp_get,
1761 .put = indep_hp_put,
1762};
1763
1764
1765static int create_indep_hp_ctls(struct hda_codec *codec)
1766{
1767 struct hda_gen_spec *spec = codec->spec;
1768
1769 if (!spec->indep_hp)
1770 return 0;
1771 if (!spec->multiout.hp_out_nid[0]) {
1772 spec->indep_hp = 0;
1773 return 0;
1774 }
1775
1776 spec->indep_hp_enabled = false;
1777 spec->alt_dac_nid = spec->multiout.hp_out_nid[0];
1778 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
1779 return -ENOMEM;
1780 return 0;
1781}
1782
1783/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01001784 * channel mode enum control
1785 */
1786
1787static int ch_mode_info(struct snd_kcontrol *kcontrol,
1788 struct snd_ctl_elem_info *uinfo)
1789{
1790 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1791 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001792 int chs;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001793
1794 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1795 uinfo->count = 1;
1796 uinfo->value.enumerated.items = spec->multi_ios + 1;
1797 if (uinfo->value.enumerated.item > spec->multi_ios)
1798 uinfo->value.enumerated.item = spec->multi_ios;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001799 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
1800 sprintf(uinfo->value.enumerated.name, "%dch", chs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001801 return 0;
1802}
1803
1804static int ch_mode_get(struct snd_kcontrol *kcontrol,
1805 struct snd_ctl_elem_value *ucontrol)
1806{
1807 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1808 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001809 ucontrol->value.enumerated.item[0] =
1810 (spec->ext_channel_count - spec->min_channel_count) / 2;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001811 return 0;
1812}
1813
Takashi Iwai196c17662013-01-04 15:01:40 +01001814static inline struct nid_path *
1815get_multiio_path(struct hda_codec *codec, int idx)
1816{
1817 struct hda_gen_spec *spec = codec->spec;
1818 return snd_hda_get_path_from_idx(codec,
1819 spec->out_paths[spec->autocfg.line_outs + idx]);
1820}
1821
Takashi Iwai352f7f92012-12-19 12:52:06 +01001822static int set_multi_io(struct hda_codec *codec, int idx, bool output)
1823{
1824 struct hda_gen_spec *spec = codec->spec;
1825 hda_nid_t nid = spec->multi_io[idx].pin;
1826 struct nid_path *path;
1827
Takashi Iwai196c17662013-01-04 15:01:40 +01001828 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001829 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001831
1832 if (path->active == output)
1833 return 0;
1834
1835 if (output) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01001836 set_pin_target(codec, nid, PIN_OUT, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001837 snd_hda_activate_path(codec, path, true, true);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01001838 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001839 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01001840 set_pin_eapd(codec, nid, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001841 snd_hda_activate_path(codec, path, false, true);
Takashi Iwai2c12c302013-01-10 09:33:29 +01001842 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 }
Takashi Iwaia365fed2013-01-10 16:10:06 +01001844
1845 /* update jack retasking in case it modifies any of them */
1846 snd_hda_gen_hp_automute(codec, NULL);
1847 snd_hda_gen_line_automute(codec, NULL);
1848 snd_hda_gen_mic_autoswitch(codec, NULL);
1849
Takashi Iwai352f7f92012-12-19 12:52:06 +01001850 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851}
1852
Takashi Iwai352f7f92012-12-19 12:52:06 +01001853static int ch_mode_put(struct snd_kcontrol *kcontrol,
1854 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001856 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1857 struct hda_gen_spec *spec = codec->spec;
1858 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
Takashi Iwai352f7f92012-12-19 12:52:06 +01001860 ch = ucontrol->value.enumerated.item[0];
1861 if (ch < 0 || ch > spec->multi_ios)
1862 return -EINVAL;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001863 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001864 return 0;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001865 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001866 for (i = 0; i < spec->multi_ios; i++)
1867 set_multi_io(codec, i, i < ch);
1868 spec->multiout.max_channels = max(spec->ext_channel_count,
1869 spec->const_channel_count);
1870 if (spec->need_dac_fix)
1871 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 return 1;
1873}
1874
Takashi Iwai352f7f92012-12-19 12:52:06 +01001875static const struct snd_kcontrol_new channel_mode_enum = {
1876 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1877 .name = "Channel Mode",
1878 .info = ch_mode_info,
1879 .get = ch_mode_get,
1880 .put = ch_mode_put,
1881};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
Takashi Iwai352f7f92012-12-19 12:52:06 +01001883static int create_multi_channel_mode(struct hda_codec *codec)
1884{
1885 struct hda_gen_spec *spec = codec->spec;
1886
1887 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01001888 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01001889 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 return 0;
1892}
1893
Takashi Iwai352f7f92012-12-19 12:52:06 +01001894/*
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001895 * aamix loopback enable/disable switch
1896 */
1897
1898#define loopback_mixing_info indep_hp_info
1899
1900static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
1901 struct snd_ctl_elem_value *ucontrol)
1902{
1903 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1904 struct hda_gen_spec *spec = codec->spec;
1905 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
1906 return 0;
1907}
1908
1909static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
1910 int nomix_path_idx, int mix_path_idx)
1911{
1912 struct nid_path *nomix_path, *mix_path;
1913
1914 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
1915 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
1916 if (!nomix_path || !mix_path)
1917 return;
1918 if (do_mix) {
1919 snd_hda_activate_path(codec, nomix_path, false, true);
1920 snd_hda_activate_path(codec, mix_path, true, true);
1921 } else {
1922 snd_hda_activate_path(codec, mix_path, false, true);
1923 snd_hda_activate_path(codec, nomix_path, true, true);
1924 }
1925}
1926
1927static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
1928 struct snd_ctl_elem_value *ucontrol)
1929{
1930 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1931 struct hda_gen_spec *spec = codec->spec;
1932 unsigned int val = ucontrol->value.enumerated.item[0];
1933
1934 if (val == spec->aamix_mode)
1935 return 0;
1936 spec->aamix_mode = val;
1937 update_aamix_paths(codec, val, spec->out_paths[0],
1938 spec->aamix_out_paths[0]);
1939 update_aamix_paths(codec, val, spec->hp_paths[0],
1940 spec->aamix_out_paths[1]);
1941 update_aamix_paths(codec, val, spec->speaker_paths[0],
1942 spec->aamix_out_paths[2]);
1943 return 1;
1944}
1945
1946static const struct snd_kcontrol_new loopback_mixing_enum = {
1947 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1948 .name = "Loopback Mixing",
1949 .info = loopback_mixing_info,
1950 .get = loopback_mixing_get,
1951 .put = loopback_mixing_put,
1952};
1953
1954static int create_loopback_mixing_ctl(struct hda_codec *codec)
1955{
1956 struct hda_gen_spec *spec = codec->spec;
1957
1958 if (!spec->mixer_nid)
1959 return 0;
1960 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
1961 spec->aamix_out_paths[2]))
1962 return 0;
1963 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
1964 return -ENOMEM;
1965 return 0;
1966}
1967
1968/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01001969 * shared headphone/mic handling
1970 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02001971
Takashi Iwai352f7f92012-12-19 12:52:06 +01001972static void call_update_outputs(struct hda_codec *codec);
1973
1974/* for shared I/O, change the pin-control accordingly */
1975static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic)
1976{
1977 struct hda_gen_spec *spec = codec->spec;
1978 unsigned int val;
1979 hda_nid_t pin = spec->autocfg.inputs[1].pin;
1980 /* NOTE: this assumes that there are only two inputs, the
1981 * first is the real internal mic and the second is HP/mic jack.
1982 */
1983
1984 val = snd_hda_get_default_vref(codec, pin);
1985
1986 /* This pin does not have vref caps - let's enable vref on pin 0x18
1987 instead, as suggested by Realtek */
1988 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
1989 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
1990 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
1991 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01001992 snd_hda_set_pin_ctl_cache(codec, vref_pin,
1993 PIN_IN | (set_as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02001994 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001995
1996 val = set_as_mic ? val | PIN_IN : PIN_HP;
Takashi Iwai2c12c302013-01-10 09:33:29 +01001997 set_pin_target(codec, pin, val, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001998
1999 spec->automute_speaker = !set_as_mic;
2000 call_update_outputs(codec);
2001}
2002
2003/* create a shared input with the headphone out */
2004static int create_shared_input(struct hda_codec *codec)
2005{
2006 struct hda_gen_spec *spec = codec->spec;
2007 struct auto_pin_cfg *cfg = &spec->autocfg;
2008 unsigned int defcfg;
2009 hda_nid_t nid;
2010
2011 /* only one internal input pin? */
2012 if (cfg->num_inputs != 1)
2013 return 0;
2014 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2015 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2016 return 0;
2017
2018 if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
2019 nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
2020 else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
2021 nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
2022 else
2023 return 0; /* both not available */
2024
2025 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2026 return 0; /* no input */
2027
2028 cfg->inputs[1].pin = nid;
2029 cfg->inputs[1].type = AUTO_PIN_MIC;
2030 cfg->num_inputs = 2;
2031 spec->shared_mic_hp = 1;
2032 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
2033 return 0;
2034}
2035
Takashi Iwai978e77e2013-01-10 16:57:58 +01002036/*
2037 * output jack mode
2038 */
2039static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2040 struct snd_ctl_elem_info *uinfo)
2041{
2042 static const char * const texts[] = {
2043 "Line Out", "Headphone Out",
2044 };
2045 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, texts);
2046}
2047
2048static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2049 struct snd_ctl_elem_value *ucontrol)
2050{
2051 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2052 hda_nid_t nid = kcontrol->private_value;
2053 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2054 ucontrol->value.enumerated.item[0] = 1;
2055 else
2056 ucontrol->value.enumerated.item[0] = 0;
2057 return 0;
2058}
2059
2060static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2061 struct snd_ctl_elem_value *ucontrol)
2062{
2063 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2064 hda_nid_t nid = kcontrol->private_value;
2065 unsigned int val;
2066
2067 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2068 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2069 return 0;
2070 snd_hda_set_pin_ctl_cache(codec, nid, val);
2071 return 1;
2072}
2073
2074static const struct snd_kcontrol_new out_jack_mode_enum = {
2075 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2076 .info = out_jack_mode_info,
2077 .get = out_jack_mode_get,
2078 .put = out_jack_mode_put,
2079};
2080
2081static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2082{
2083 struct hda_gen_spec *spec = codec->spec;
2084 int i;
2085
2086 for (i = 0; i < spec->kctls.used; i++) {
2087 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2088 if (!strcmp(kctl->name, name) && kctl->index == idx)
2089 return true;
2090 }
2091 return false;
2092}
2093
2094static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2095 char *name, size_t name_len)
2096{
2097 struct hda_gen_spec *spec = codec->spec;
2098 int idx = 0;
2099
2100 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2101 strlcat(name, " Jack Mode", name_len);
2102
2103 for (; find_kctl_name(codec, name, idx); idx++)
2104 ;
2105}
2106
2107static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2108 hda_nid_t *pins)
2109{
2110 struct hda_gen_spec *spec = codec->spec;
2111 int i;
2112
2113 for (i = 0; i < num_pins; i++) {
2114 hda_nid_t pin = pins[i];
2115 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2116 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV)) {
2117 struct snd_kcontrol_new *knew;
2118 char name[44];
2119 get_jack_mode_name(codec, pin, name, sizeof(name));
2120 knew = snd_hda_gen_add_kctl(spec, name,
2121 &out_jack_mode_enum);
2122 if (!knew)
2123 return -ENOMEM;
2124 knew->private_value = pin;
2125 }
2126 }
2127
2128 return 0;
2129}
2130
Takashi Iwai352f7f92012-12-19 12:52:06 +01002131
2132/*
2133 * Parse input paths
2134 */
2135
2136#ifdef CONFIG_PM
2137/* add the powersave loopback-list entry */
2138static void add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
2139{
2140 struct hda_amp_list *list;
2141
2142 if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
2143 return;
2144 list = spec->loopback_list + spec->num_loopbacks;
2145 list->nid = mix;
2146 list->dir = HDA_INPUT;
2147 list->idx = idx;
2148 spec->num_loopbacks++;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002149 spec->loopback.amplist = spec->loopback_list;
2150}
2151#else
Takashi Iwai352f7f92012-12-19 12:52:06 +01002152#define add_loopback_list(spec, mix, idx) /* NOP */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002153#endif
2154
Takashi Iwai352f7f92012-12-19 12:52:06 +01002155/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01002156static int new_analog_input(struct hda_codec *codec, int input_idx,
2157 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002158 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002160 struct hda_gen_spec *spec = codec->spec;
2161 struct nid_path *path;
2162 unsigned int val;
2163 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
Takashi Iwai352f7f92012-12-19 12:52:06 +01002165 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
2166 !nid_has_mute(codec, mix_nid, HDA_INPUT))
2167 return 0; /* no need for analog loopback */
2168
Takashi Iwai3ca529d2013-01-07 17:25:08 +01002169 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002170 if (!path)
2171 return -EINVAL;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002172 print_nid_path("loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01002173 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002174
2175 idx = path->idx[path->depth - 1];
2176 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
2177 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2178 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002179 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002181 path->ctls[NID_PATH_VOL_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 }
2183
Takashi Iwai352f7f92012-12-19 12:52:06 +01002184 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
2185 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2186 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002187 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002189 path->ctls[NID_PATH_MUTE_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 }
2191
Takashi Iwai352f7f92012-12-19 12:52:06 +01002192 path->active = true;
2193 add_loopback_list(spec, mix_nid, idx);
2194 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195}
2196
Takashi Iwai352f7f92012-12-19 12:52:06 +01002197static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002199 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
2200 return (pincap & AC_PINCAP_IN) != 0;
2201}
2202
2203/* Parse the codec tree and retrieve ADCs */
2204static int fill_adc_nids(struct hda_codec *codec)
2205{
2206 struct hda_gen_spec *spec = codec->spec;
2207 hda_nid_t nid;
2208 hda_nid_t *adc_nids = spec->adc_nids;
2209 int max_nums = ARRAY_SIZE(spec->adc_nids);
2210 int i, nums = 0;
2211
2212 nid = codec->start_nid;
2213 for (i = 0; i < codec->num_nodes; i++, nid++) {
2214 unsigned int caps = get_wcaps(codec, nid);
2215 int type = get_wcaps_type(caps);
2216
2217 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2218 continue;
2219 adc_nids[nums] = nid;
2220 if (++nums >= max_nums)
2221 break;
2222 }
2223 spec->num_adc_nids = nums;
2224 return nums;
2225}
2226
2227/* filter out invalid adc_nids that don't give all active input pins;
2228 * if needed, check whether dynamic ADC-switching is available
2229 */
2230static int check_dyn_adc_switch(struct hda_codec *codec)
2231{
2232 struct hda_gen_spec *spec = codec->spec;
2233 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002234 unsigned int ok_bits;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002235 int i, n, nums;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002236
2237 again:
2238 nums = 0;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002239 ok_bits = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002240 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002241 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002242 if (!spec->input_paths[i][n])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002243 break;
2244 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002245 if (i >= imux->num_items) {
2246 ok_bits |= (1 << n);
2247 nums++;
2248 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002249 }
2250
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002251 if (!ok_bits) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002252 if (spec->shared_mic_hp) {
2253 spec->shared_mic_hp = 0;
2254 imux->num_items = 1;
2255 goto again;
2256 }
2257
2258 /* check whether ADC-switch is possible */
2259 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002260 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002261 if (spec->input_paths[i][n]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002262 spec->dyn_adc_idx[i] = n;
2263 break;
2264 }
2265 }
2266 }
2267
2268 snd_printdd("hda-codec: enabling ADC switching\n");
2269 spec->dyn_adc_switch = 1;
2270 } else if (nums != spec->num_adc_nids) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002271 /* shrink the invalid adcs and input paths */
2272 nums = 0;
2273 for (n = 0; n < spec->num_adc_nids; n++) {
2274 if (!(ok_bits & (1 << n)))
2275 continue;
2276 if (n != nums) {
2277 spec->adc_nids[nums] = spec->adc_nids[n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002278 for (i = 0; i < imux->num_items; i++) {
2279 invalidate_nid_path(codec,
2280 spec->input_paths[i][nums]);
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002281 spec->input_paths[i][nums] =
2282 spec->input_paths[i][n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002283 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002284 }
2285 nums++;
2286 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002287 spec->num_adc_nids = nums;
2288 }
2289
2290 if (imux->num_items == 1 || spec->shared_mic_hp) {
2291 snd_printdd("hda-codec: reducing to a single ADC\n");
2292 spec->num_adc_nids = 1; /* reduce to a single ADC */
2293 }
2294
2295 /* single index for individual volumes ctls */
2296 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
2297 spec->num_adc_nids = 1;
2298
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 return 0;
2300}
2301
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002302/* parse capture source paths from the given pin and create imux items */
2303static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
2304 int num_adcs, const char *label, int anchor)
2305{
2306 struct hda_gen_spec *spec = codec->spec;
2307 struct hda_input_mux *imux = &spec->input_mux;
2308 int imux_idx = imux->num_items;
2309 bool imux_added = false;
2310 int c;
2311
2312 for (c = 0; c < num_adcs; c++) {
2313 struct nid_path *path;
2314 hda_nid_t adc = spec->adc_nids[c];
2315
2316 if (!is_reachable_path(codec, pin, adc))
2317 continue;
2318 path = snd_hda_add_new_path(codec, pin, adc, anchor);
2319 if (!path)
2320 continue;
2321 print_nid_path("input", path);
2322 spec->input_paths[imux_idx][c] =
2323 snd_hda_get_path_idx(codec, path);
2324
2325 if (!imux_added) {
2326 spec->imux_pins[imux->num_items] = pin;
2327 snd_hda_add_imux_item(imux, label,
2328 imux->num_items, NULL);
2329 imux_added = true;
2330 }
2331 }
2332
2333 return 0;
2334}
2335
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002337 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002339static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002340{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002341 struct hda_gen_spec *spec = codec->spec;
2342 const struct auto_pin_cfg *cfg = &spec->autocfg;
2343 hda_nid_t mixer = spec->mixer_nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002344 int num_adcs;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002345 int i, err, type_idx = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002346 const char *prev_label = NULL;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002347 unsigned int val;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002348
Takashi Iwai352f7f92012-12-19 12:52:06 +01002349 num_adcs = fill_adc_nids(codec);
2350 if (num_adcs < 0)
2351 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002352
Takashi Iwai352f7f92012-12-19 12:52:06 +01002353 for (i = 0; i < cfg->num_inputs; i++) {
2354 hda_nid_t pin;
2355 const char *label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356
Takashi Iwai352f7f92012-12-19 12:52:06 +01002357 pin = cfg->inputs[i].pin;
2358 if (!is_input_pin(codec, pin))
2359 continue;
2360
2361 label = hda_get_autocfg_input_label(codec, cfg, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002362 if (prev_label && !strcmp(label, prev_label))
2363 type_idx++;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002364 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01002365 type_idx = 0;
2366 prev_label = label;
2367
Takashi Iwai2c12c302013-01-10 09:33:29 +01002368 val = PIN_IN;
2369 if (cfg->inputs[i].type == AUTO_PIN_MIC)
2370 val |= snd_hda_get_default_vref(codec, pin);
2371 set_pin_target(codec, pin, val, false);
2372
Takashi Iwai352f7f92012-12-19 12:52:06 +01002373 if (mixer) {
2374 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01002375 err = new_analog_input(codec, i, pin,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002376 label, type_idx, mixer);
2377 if (err < 0)
2378 return err;
2379 }
2380 }
2381
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002382 err = parse_capture_source(codec, pin, num_adcs, label, -mixer);
2383 if (err < 0)
2384 return err;
2385 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002386
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002387 if (mixer && spec->add_stereo_mix_input) {
2388 err = parse_capture_source(codec, mixer, num_adcs,
2389 "Stereo Mix", 0);
2390 if (err < 0)
2391 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002392 }
2393
2394 return 0;
2395}
2396
2397
2398/*
2399 * input source mux
2400 */
2401
Takashi Iwaic697b712013-01-07 17:09:26 +01002402/* get the input path specified by the given adc and imux indices */
2403static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002404{
2405 struct hda_gen_spec *spec = codec->spec;
David Henningssonb56fa1e2013-01-16 11:45:35 +01002406 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
2407 snd_BUG();
2408 return NULL;
2409 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002410 if (spec->dyn_adc_switch)
2411 adc_idx = spec->dyn_adc_idx[imux_idx];
David Henningssonb56fa1e2013-01-16 11:45:35 +01002412 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_OUTS) {
2413 snd_BUG();
2414 return NULL;
2415 }
Takashi Iwaic697b712013-01-07 17:09:26 +01002416 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002417}
2418
2419static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2420 unsigned int idx);
2421
2422static int mux_enum_info(struct snd_kcontrol *kcontrol,
2423 struct snd_ctl_elem_info *uinfo)
2424{
2425 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2426 struct hda_gen_spec *spec = codec->spec;
2427 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
2428}
2429
2430static int mux_enum_get(struct snd_kcontrol *kcontrol,
2431 struct snd_ctl_elem_value *ucontrol)
2432{
2433 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2434 struct hda_gen_spec *spec = codec->spec;
David Henningssona053d1e2013-01-16 11:45:36 +01002435 unsigned int adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002436
2437 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
2438 return 0;
2439}
2440
2441static int mux_enum_put(struct snd_kcontrol *kcontrol,
2442 struct snd_ctl_elem_value *ucontrol)
2443{
2444 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
David Henningssona053d1e2013-01-16 11:45:36 +01002445 unsigned int adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002446 return mux_select(codec, adc_idx,
2447 ucontrol->value.enumerated.item[0]);
2448}
2449
Takashi Iwai352f7f92012-12-19 12:52:06 +01002450static const struct snd_kcontrol_new cap_src_temp = {
2451 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2452 .name = "Input Source",
2453 .info = mux_enum_info,
2454 .get = mux_enum_get,
2455 .put = mux_enum_put,
2456};
2457
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002458/*
2459 * capture volume and capture switch ctls
2460 */
2461
Takashi Iwai352f7f92012-12-19 12:52:06 +01002462typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
2463 struct snd_ctl_elem_value *ucontrol);
2464
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002465/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002466static int cap_put_caller(struct snd_kcontrol *kcontrol,
2467 struct snd_ctl_elem_value *ucontrol,
2468 put_call_t func, int type)
2469{
2470 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2471 struct hda_gen_spec *spec = codec->spec;
2472 const struct hda_input_mux *imux;
2473 struct nid_path *path;
2474 int i, adc_idx, err = 0;
2475
2476 imux = &spec->input_mux;
David Henningssona053d1e2013-01-16 11:45:36 +01002477 adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002478 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002479 /* we use the cache-only update at first since multiple input paths
2480 * may shared the same amp; by updating only caches, the redundant
2481 * writes to hardware can be reduced.
2482 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002483 codec->cached_write = 1;
2484 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01002485 path = get_input_path(codec, adc_idx, i);
2486 if (!path || !path->ctls[type])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002487 continue;
2488 kcontrol->private_value = path->ctls[type];
2489 err = func(kcontrol, ucontrol);
2490 if (err < 0)
2491 goto error;
2492 }
2493 error:
2494 codec->cached_write = 0;
2495 mutex_unlock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002496 snd_hda_codec_flush_amp_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002497 if (err >= 0 && spec->cap_sync_hook)
2498 spec->cap_sync_hook(codec);
2499 return err;
2500}
2501
2502/* capture volume ctl callbacks */
2503#define cap_vol_info snd_hda_mixer_amp_volume_info
2504#define cap_vol_get snd_hda_mixer_amp_volume_get
2505#define cap_vol_tlv snd_hda_mixer_amp_tlv
2506
2507static int cap_vol_put(struct snd_kcontrol *kcontrol,
2508 struct snd_ctl_elem_value *ucontrol)
2509{
2510 return cap_put_caller(kcontrol, ucontrol,
2511 snd_hda_mixer_amp_volume_put,
2512 NID_PATH_VOL_CTL);
2513}
2514
2515static const struct snd_kcontrol_new cap_vol_temp = {
2516 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2517 .name = "Capture Volume",
2518 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
2519 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2520 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
2521 .info = cap_vol_info,
2522 .get = cap_vol_get,
2523 .put = cap_vol_put,
2524 .tlv = { .c = cap_vol_tlv },
2525};
2526
2527/* capture switch ctl callbacks */
2528#define cap_sw_info snd_ctl_boolean_stereo_info
2529#define cap_sw_get snd_hda_mixer_amp_switch_get
2530
2531static int cap_sw_put(struct snd_kcontrol *kcontrol,
2532 struct snd_ctl_elem_value *ucontrol)
2533{
Takashi Iwaiae177c32013-01-14 12:13:06 +01002534 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2535 struct hda_gen_spec *spec = codec->spec;
2536 int ret;
2537
2538 ret = cap_put_caller(kcontrol, ucontrol,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002539 snd_hda_mixer_amp_switch_put,
2540 NID_PATH_MUTE_CTL);
Takashi Iwaiae177c32013-01-14 12:13:06 +01002541 if (ret < 0)
2542 return ret;
2543
2544 if (spec->capture_switch_hook) {
2545 bool enable = (ucontrol->value.integer.value[0] ||
2546 ucontrol->value.integer.value[1]);
2547 spec->capture_switch_hook(codec, enable);
2548 }
2549
2550 return ret;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002551}
2552
2553static const struct snd_kcontrol_new cap_sw_temp = {
2554 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2555 .name = "Capture Switch",
2556 .info = cap_sw_info,
2557 .get = cap_sw_get,
2558 .put = cap_sw_put,
2559};
2560
2561static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
2562{
2563 hda_nid_t nid;
2564 int i, depth;
2565
2566 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
2567 for (depth = 0; depth < 3; depth++) {
2568 if (depth >= path->depth)
2569 return -EINVAL;
2570 i = path->depth - depth - 1;
2571 nid = path->path[i];
2572 if (!path->ctls[NID_PATH_VOL_CTL]) {
2573 if (nid_has_volume(codec, nid, HDA_OUTPUT))
2574 path->ctls[NID_PATH_VOL_CTL] =
2575 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2576 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
2577 int idx = path->idx[i];
2578 if (!depth && codec->single_adc_amp)
2579 idx = 0;
2580 path->ctls[NID_PATH_VOL_CTL] =
2581 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2582 }
2583 }
2584 if (!path->ctls[NID_PATH_MUTE_CTL]) {
2585 if (nid_has_mute(codec, nid, HDA_OUTPUT))
2586 path->ctls[NID_PATH_MUTE_CTL] =
2587 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2588 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
2589 int idx = path->idx[i];
2590 if (!depth && codec->single_adc_amp)
2591 idx = 0;
2592 path->ctls[NID_PATH_MUTE_CTL] =
2593 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2594 }
2595 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01002596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 return 0;
2598}
2599
Takashi Iwai352f7f92012-12-19 12:52:06 +01002600static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002602 struct hda_gen_spec *spec = codec->spec;
2603 struct auto_pin_cfg *cfg = &spec->autocfg;
2604 unsigned int val;
2605 int i;
2606
2607 if (!spec->inv_dmic_split)
2608 return false;
2609 for (i = 0; i < cfg->num_inputs; i++) {
2610 if (cfg->inputs[i].pin != nid)
2611 continue;
2612 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2613 return false;
2614 val = snd_hda_codec_get_pincfg(codec, nid);
2615 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
2616 }
2617 return false;
2618}
2619
2620static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
2621 int idx, bool is_switch, unsigned int ctl,
2622 bool inv_dmic)
2623{
2624 struct hda_gen_spec *spec = codec->spec;
2625 char tmpname[44];
2626 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
2627 const char *sfx = is_switch ? "Switch" : "Volume";
2628 unsigned int chs = inv_dmic ? 1 : 3;
2629 int err;
2630
2631 if (!ctl)
2632 return 0;
2633
2634 if (label)
2635 snprintf(tmpname, sizeof(tmpname),
2636 "%s Capture %s", label, sfx);
2637 else
2638 snprintf(tmpname, sizeof(tmpname),
2639 "Capture %s", sfx);
2640 err = add_control(spec, type, tmpname, idx,
2641 amp_val_replace_channels(ctl, chs));
2642 if (err < 0 || !inv_dmic)
2643 return err;
2644
2645 /* Make independent right kcontrol */
2646 if (label)
2647 snprintf(tmpname, sizeof(tmpname),
2648 "Inverted %s Capture %s", label, sfx);
2649 else
2650 snprintf(tmpname, sizeof(tmpname),
2651 "Inverted Capture %s", sfx);
2652 return add_control(spec, type, tmpname, idx,
2653 amp_val_replace_channels(ctl, 2));
2654}
2655
2656/* create single (and simple) capture volume and switch controls */
2657static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
2658 unsigned int vol_ctl, unsigned int sw_ctl,
2659 bool inv_dmic)
2660{
2661 int err;
2662 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
2663 if (err < 0)
2664 return err;
2665 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
2666 if (err < 0)
2667 return err;
2668 return 0;
2669}
2670
2671/* create bound capture volume and switch controls */
2672static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
2673 unsigned int vol_ctl, unsigned int sw_ctl)
2674{
2675 struct hda_gen_spec *spec = codec->spec;
2676 struct snd_kcontrol_new *knew;
2677
2678 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002679 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002680 if (!knew)
2681 return -ENOMEM;
2682 knew->index = idx;
2683 knew->private_value = vol_ctl;
2684 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2685 }
2686 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002687 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002688 if (!knew)
2689 return -ENOMEM;
2690 knew->index = idx;
2691 knew->private_value = sw_ctl;
2692 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2693 }
2694 return 0;
2695}
2696
2697/* return the vol ctl when used first in the imux list */
2698static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
2699{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002700 struct nid_path *path;
2701 unsigned int ctl;
2702 int i;
2703
Takashi Iwaic697b712013-01-07 17:09:26 +01002704 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002705 if (!path)
2706 return 0;
2707 ctl = path->ctls[type];
2708 if (!ctl)
2709 return 0;
2710 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01002711 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002712 if (path && path->ctls[type] == ctl)
2713 return 0;
2714 }
2715 return ctl;
2716}
2717
2718/* create individual capture volume and switch controls per input */
2719static int create_multi_cap_vol_ctl(struct hda_codec *codec)
2720{
2721 struct hda_gen_spec *spec = codec->spec;
2722 struct hda_input_mux *imux = &spec->input_mux;
2723 int i, err, type, type_idx = 0;
2724 const char *prev_label = NULL;
2725
2726 for (i = 0; i < imux->num_items; i++) {
2727 const char *label;
2728 bool inv_dmic;
2729 label = hda_get_autocfg_input_label(codec, &spec->autocfg, i);
2730 if (prev_label && !strcmp(label, prev_label))
2731 type_idx++;
2732 else
2733 type_idx = 0;
2734 prev_label = label;
2735 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
2736
2737 for (type = 0; type < 2; type++) {
2738 err = add_single_cap_ctl(codec, label, type_idx, type,
2739 get_first_cap_ctl(codec, i, type),
2740 inv_dmic);
2741 if (err < 0)
2742 return err;
2743 }
2744 }
2745 return 0;
2746}
2747
2748static int create_capture_mixers(struct hda_codec *codec)
2749{
2750 struct hda_gen_spec *spec = codec->spec;
2751 struct hda_input_mux *imux = &spec->input_mux;
2752 int i, n, nums, err;
2753
2754 if (spec->dyn_adc_switch)
2755 nums = 1;
2756 else
2757 nums = spec->num_adc_nids;
2758
2759 if (!spec->auto_mic && imux->num_items > 1) {
2760 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01002761 const char *name;
2762 name = nums > 1 ? "Input Source" : "Capture Source";
2763 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002764 if (!knew)
2765 return -ENOMEM;
2766 knew->count = nums;
2767 }
2768
2769 for (n = 0; n < nums; n++) {
2770 bool multi = false;
2771 bool inv_dmic = false;
2772 int vol, sw;
2773
2774 vol = sw = 0;
2775 for (i = 0; i < imux->num_items; i++) {
2776 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01002777 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002778 if (!path)
2779 continue;
2780 parse_capvol_in_path(codec, path);
2781 if (!vol)
2782 vol = path->ctls[NID_PATH_VOL_CTL];
2783 else if (vol != path->ctls[NID_PATH_VOL_CTL])
2784 multi = true;
2785 if (!sw)
2786 sw = path->ctls[NID_PATH_MUTE_CTL];
2787 else if (sw != path->ctls[NID_PATH_MUTE_CTL])
2788 multi = true;
2789 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
2790 inv_dmic = true;
2791 }
2792
2793 if (!multi)
2794 err = create_single_cap_vol_ctl(codec, n, vol, sw,
2795 inv_dmic);
2796 else if (!spec->multi_cap_vol)
2797 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
2798 else
2799 err = create_multi_cap_vol_ctl(codec);
2800 if (err < 0)
2801 return err;
2802 }
2803
2804 return 0;
2805}
2806
2807/*
2808 * add mic boosts if needed
2809 */
2810static int parse_mic_boost(struct hda_codec *codec)
2811{
2812 struct hda_gen_spec *spec = codec->spec;
2813 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002814 int i, err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002815 int type_idx = 0;
2816 hda_nid_t nid;
2817 const char *prev_label = NULL;
2818
2819 for (i = 0; i < cfg->num_inputs; i++) {
2820 if (cfg->inputs[i].type > AUTO_PIN_MIC)
2821 break;
2822 nid = cfg->inputs[i].pin;
2823 if (get_wcaps(codec, nid) & AC_WCAP_IN_AMP) {
2824 const char *label;
Takashi Iwai5abd4882013-01-07 09:43:18 +01002825 char boost_label[44];
Takashi Iwai352f7f92012-12-19 12:52:06 +01002826 struct nid_path *path;
2827 unsigned int val;
2828
David Henningsson02aba552013-01-16 15:58:43 +01002829 if (!nid_has_volume(codec, nid, HDA_INPUT))
2830 continue;
2831
Takashi Iwai352f7f92012-12-19 12:52:06 +01002832 label = hda_get_autocfg_input_label(codec, cfg, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002833 if (prev_label && !strcmp(label, prev_label))
2834 type_idx++;
2835 else
2836 type_idx = 0;
2837 prev_label = label;
2838
2839 snprintf(boost_label, sizeof(boost_label),
2840 "%s Boost Volume", label);
2841 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
2842 err = add_control(spec, HDA_CTL_WIDGET_VOL,
2843 boost_label, type_idx, val);
2844 if (err < 0)
2845 return err;
2846
2847 path = snd_hda_get_nid_path(codec, nid, 0);
2848 if (path)
2849 path->ctls[NID_PATH_BOOST_CTL] = val;
2850 }
2851 }
2852 return 0;
2853}
2854
2855/*
2856 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
2857 */
2858static void parse_digital(struct hda_codec *codec)
2859{
2860 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002861 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002862 int i, nums;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002863 hda_nid_t dig_nid, pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002864
2865 /* support multiple SPDIFs; the secondary is set up as a slave */
2866 nums = 0;
2867 for (i = 0; i < spec->autocfg.dig_outs; i++) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01002868 pin = spec->autocfg.dig_out_pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01002869 dig_nid = look_for_dac(codec, pin, true);
2870 if (!dig_nid)
2871 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01002872 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002873 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002874 continue;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002875 print_nid_path("digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01002876 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01002877 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01002878 set_pin_target(codec, pin, PIN_OUT, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002879 if (!nums) {
2880 spec->multiout.dig_out_nid = dig_nid;
2881 spec->dig_out_type = spec->autocfg.dig_out_type[0];
2882 } else {
2883 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
2884 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
2885 break;
2886 spec->slave_dig_outs[nums - 1] = dig_nid;
2887 }
2888 nums++;
2889 }
2890
2891 if (spec->autocfg.dig_in_pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01002892 pin = spec->autocfg.dig_in_pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002893 dig_nid = codec->start_nid;
2894 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002895 unsigned int wcaps = get_wcaps(codec, dig_nid);
2896 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
2897 continue;
2898 if (!(wcaps & AC_WCAP_DIGITAL))
2899 continue;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002900 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002901 if (path) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002902 print_nid_path("digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002903 path->active = true;
2904 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01002905 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01002906 set_pin_target(codec, pin, PIN_IN, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002907 break;
2908 }
2909 }
2910 }
2911}
2912
2913
2914/*
2915 * input MUX handling
2916 */
2917
2918static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
2919
2920/* select the given imux item; either unmute exclusively or select the route */
2921static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2922 unsigned int idx)
2923{
2924 struct hda_gen_spec *spec = codec->spec;
2925 const struct hda_input_mux *imux;
2926 struct nid_path *path;
2927
2928 imux = &spec->input_mux;
2929 if (!imux->num_items)
2930 return 0;
2931
2932 if (idx >= imux->num_items)
2933 idx = imux->num_items - 1;
2934 if (spec->cur_mux[adc_idx] == idx)
2935 return 0;
2936
Takashi Iwaic697b712013-01-07 17:09:26 +01002937 path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002938 if (!path)
2939 return 0;
2940 if (path->active)
2941 snd_hda_activate_path(codec, path, false, false);
2942
2943 spec->cur_mux[adc_idx] = idx;
2944
2945 if (spec->shared_mic_hp)
2946 update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
2947
2948 if (spec->dyn_adc_switch)
2949 dyn_adc_pcm_resetup(codec, idx);
2950
Takashi Iwaic697b712013-01-07 17:09:26 +01002951 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002952 if (!path)
2953 return 0;
2954 if (path->active)
2955 return 0;
2956 snd_hda_activate_path(codec, path, true, false);
2957 if (spec->cap_sync_hook)
2958 spec->cap_sync_hook(codec);
2959 return 1;
2960}
2961
2962
2963/*
2964 * Jack detections for HP auto-mute and mic-switch
2965 */
2966
2967/* check each pin in the given array; returns true if any of them is plugged */
2968static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
2969{
2970 int i, present = 0;
2971
2972 for (i = 0; i < num_pins; i++) {
2973 hda_nid_t nid = pins[i];
2974 if (!nid)
2975 break;
Takashi Iwai0b4df932013-01-10 09:45:13 +01002976 /* don't detect pins retasked as inputs */
2977 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
2978 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002979 present |= snd_hda_jack_detect(codec, nid);
2980 }
2981 return present;
2982}
2983
2984/* standard HP/line-out auto-mute helper */
2985static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
Takashi Iwai2c12c302013-01-10 09:33:29 +01002986 bool mute)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002987{
2988 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002989 int i;
2990
2991 for (i = 0; i < num_pins; i++) {
2992 hda_nid_t nid = pins[i];
2993 unsigned int val;
2994 if (!nid)
2995 break;
2996 /* don't reset VREF value in case it's controlling
2997 * the amp (see alc861_fixup_asus_amp_vref_0f())
2998 */
Takashi Iwai2c12c302013-01-10 09:33:29 +01002999 if (spec->keep_vref_in_automute)
3000 val = snd_hda_codec_get_pin_target(codec, nid) & ~PIN_HP;
3001 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01003002 val = 0;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003003 if (!mute)
3004 val |= snd_hda_codec_get_pin_target(codec, nid);
3005 /* here we call update_pin_ctl() so that the pinctl is changed
3006 * without changing the pinctl target value;
3007 * the original target value will be still referred at the
3008 * init / resume again
3009 */
3010 update_pin_ctl(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01003011 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003012 }
3013}
3014
3015/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003016void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003017{
3018 struct hda_gen_spec *spec = codec->spec;
3019 int on;
3020
3021 /* Control HP pins/amps depending on master_mute state;
3022 * in general, HP pins/amps control should be enabled in all cases,
3023 * but currently set only for master_mute, just to be safe
3024 */
3025 if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
3026 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003027 spec->autocfg.hp_pins, spec->master_mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003028
3029 if (!spec->automute_speaker)
3030 on = 0;
3031 else
3032 on = spec->hp_jack_present | spec->line_jack_present;
3033 on |= spec->master_mute;
3034 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003035 spec->autocfg.speaker_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003036
3037 /* toggle line-out mutes if needed, too */
3038 /* if LO is a copy of either HP or Speaker, don't need to handle it */
3039 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
3040 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
3041 return;
3042 if (!spec->automute_lo)
3043 on = 0;
3044 else
3045 on = spec->hp_jack_present;
3046 on |= spec->master_mute;
3047 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003048 spec->autocfg.line_out_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003049}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003050EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003051
3052static void call_update_outputs(struct hda_codec *codec)
3053{
3054 struct hda_gen_spec *spec = codec->spec;
3055 if (spec->automute_hook)
3056 spec->automute_hook(codec);
3057 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01003058 snd_hda_gen_update_outputs(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003059}
3060
3061/* standard HP-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003062void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003063{
3064 struct hda_gen_spec *spec = codec->spec;
3065
3066 spec->hp_jack_present =
3067 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
3068 spec->autocfg.hp_pins);
3069 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
3070 return;
3071 call_update_outputs(codec);
3072}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003073EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003074
3075/* standard line-out-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003076void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003077{
3078 struct hda_gen_spec *spec = codec->spec;
3079
3080 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3081 return;
3082 /* check LO jack only when it's different from HP */
3083 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
3084 return;
3085
3086 spec->line_jack_present =
3087 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3088 spec->autocfg.line_out_pins);
3089 if (!spec->automute_speaker || !spec->detect_lo)
3090 return;
3091 call_update_outputs(codec);
3092}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003093EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003094
3095/* standard mic auto-switch helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003096void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003097{
3098 struct hda_gen_spec *spec = codec->spec;
3099 int i;
3100
3101 if (!spec->auto_mic)
3102 return;
3103
3104 for (i = spec->am_num_entries - 1; i > 0; i--) {
Takashi Iwai0b4df932013-01-10 09:45:13 +01003105 hda_nid_t pin = spec->am_entry[i].pin;
3106 /* don't detect pins retasked as outputs */
3107 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
3108 continue;
3109 if (snd_hda_jack_detect(codec, pin)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003110 mux_select(codec, 0, spec->am_entry[i].idx);
3111 return;
3112 }
3113 }
3114 mux_select(codec, 0, spec->am_entry[0].idx);
3115}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003116EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003117
3118/*
3119 * Auto-Mute mode mixer enum support
3120 */
3121static int automute_mode_info(struct snd_kcontrol *kcontrol,
3122 struct snd_ctl_elem_info *uinfo)
3123{
3124 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3125 struct hda_gen_spec *spec = codec->spec;
3126 static const char * const texts3[] = {
3127 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02003128 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129
Takashi Iwai352f7f92012-12-19 12:52:06 +01003130 if (spec->automute_speaker_possible && spec->automute_lo_possible)
3131 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
3132 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
3133}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134
Takashi Iwai352f7f92012-12-19 12:52:06 +01003135static int automute_mode_get(struct snd_kcontrol *kcontrol,
3136 struct snd_ctl_elem_value *ucontrol)
3137{
3138 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3139 struct hda_gen_spec *spec = codec->spec;
3140 unsigned int val = 0;
3141 if (spec->automute_speaker)
3142 val++;
3143 if (spec->automute_lo)
3144 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003145
Takashi Iwai352f7f92012-12-19 12:52:06 +01003146 ucontrol->value.enumerated.item[0] = val;
3147 return 0;
3148}
3149
3150static int automute_mode_put(struct snd_kcontrol *kcontrol,
3151 struct snd_ctl_elem_value *ucontrol)
3152{
3153 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3154 struct hda_gen_spec *spec = codec->spec;
3155
3156 switch (ucontrol->value.enumerated.item[0]) {
3157 case 0:
3158 if (!spec->automute_speaker && !spec->automute_lo)
3159 return 0;
3160 spec->automute_speaker = 0;
3161 spec->automute_lo = 0;
3162 break;
3163 case 1:
3164 if (spec->automute_speaker_possible) {
3165 if (!spec->automute_lo && spec->automute_speaker)
3166 return 0;
3167 spec->automute_speaker = 1;
3168 spec->automute_lo = 0;
3169 } else if (spec->automute_lo_possible) {
3170 if (spec->automute_lo)
3171 return 0;
3172 spec->automute_lo = 1;
3173 } else
3174 return -EINVAL;
3175 break;
3176 case 2:
3177 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
3178 return -EINVAL;
3179 if (spec->automute_speaker && spec->automute_lo)
3180 return 0;
3181 spec->automute_speaker = 1;
3182 spec->automute_lo = 1;
3183 break;
3184 default:
3185 return -EINVAL;
3186 }
3187 call_update_outputs(codec);
3188 return 1;
3189}
3190
3191static const struct snd_kcontrol_new automute_mode_enum = {
3192 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3193 .name = "Auto-Mute Mode",
3194 .info = automute_mode_info,
3195 .get = automute_mode_get,
3196 .put = automute_mode_put,
3197};
3198
3199static int add_automute_mode_enum(struct hda_codec *codec)
3200{
3201 struct hda_gen_spec *spec = codec->spec;
3202
Takashi Iwai12c93df2012-12-19 14:38:33 +01003203 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01003204 return -ENOMEM;
3205 return 0;
3206}
3207
3208/*
3209 * Check the availability of HP/line-out auto-mute;
3210 * Set up appropriately if really supported
3211 */
3212static int check_auto_mute_availability(struct hda_codec *codec)
3213{
3214 struct hda_gen_spec *spec = codec->spec;
3215 struct auto_pin_cfg *cfg = &spec->autocfg;
3216 int present = 0;
3217 int i, err;
3218
3219 if (cfg->hp_pins[0])
3220 present++;
3221 if (cfg->line_out_pins[0])
3222 present++;
3223 if (cfg->speaker_pins[0])
3224 present++;
3225 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02003226 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003227
3228 if (!cfg->speaker_pins[0] &&
3229 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
3230 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3231 sizeof(cfg->speaker_pins));
3232 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234
Takashi Iwai352f7f92012-12-19 12:52:06 +01003235 if (!cfg->hp_pins[0] &&
3236 cfg->line_out_type == AUTO_PIN_HP_OUT) {
3237 memcpy(cfg->hp_pins, cfg->line_out_pins,
3238 sizeof(cfg->hp_pins));
3239 cfg->hp_outs = cfg->line_outs;
3240 }
3241
3242 for (i = 0; i < cfg->hp_outs; i++) {
3243 hda_nid_t nid = cfg->hp_pins[i];
3244 if (!is_jack_detectable(codec, nid))
3245 continue;
3246 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
3247 nid);
3248 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003249 spec->hp_automute_hook ?
3250 spec->hp_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003251 snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003252 spec->detect_hp = 1;
3253 }
3254
3255 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
3256 if (cfg->speaker_outs)
3257 for (i = 0; i < cfg->line_outs; i++) {
3258 hda_nid_t nid = cfg->line_out_pins[i];
3259 if (!is_jack_detectable(codec, nid))
3260 continue;
3261 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
3262 snd_hda_jack_detect_enable_callback(codec, nid,
3263 HDA_GEN_FRONT_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003264 spec->line_automute_hook ?
3265 spec->line_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003266 snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003267 spec->detect_lo = 1;
3268 }
3269 spec->automute_lo_possible = spec->detect_hp;
3270 }
3271
3272 spec->automute_speaker_possible = cfg->speaker_outs &&
3273 (spec->detect_hp || spec->detect_lo);
3274
3275 spec->automute_lo = spec->automute_lo_possible;
3276 spec->automute_speaker = spec->automute_speaker_possible;
3277
3278 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
3279 /* create a control for automute mode */
3280 err = add_automute_mode_enum(codec);
3281 if (err < 0)
3282 return err;
3283 }
3284 return 0;
3285}
3286
Takashi Iwai352f7f92012-12-19 12:52:06 +01003287/* check whether all auto-mic pins are valid; setup indices if OK */
3288static bool auto_mic_check_imux(struct hda_codec *codec)
3289{
3290 struct hda_gen_spec *spec = codec->spec;
3291 const struct hda_input_mux *imux;
3292 int i;
3293
3294 imux = &spec->input_mux;
3295 for (i = 0; i < spec->am_num_entries; i++) {
3296 spec->am_entry[i].idx =
3297 find_idx_in_nid_list(spec->am_entry[i].pin,
3298 spec->imux_pins, imux->num_items);
3299 if (spec->am_entry[i].idx < 0)
3300 return false; /* no corresponding imux */
3301 }
3302
3303 /* we don't need the jack detection for the first pin */
3304 for (i = 1; i < spec->am_num_entries; i++)
3305 snd_hda_jack_detect_enable_callback(codec,
3306 spec->am_entry[i].pin,
3307 HDA_GEN_MIC_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003308 spec->mic_autoswitch_hook ?
3309 spec->mic_autoswitch_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003310 snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003311 return true;
3312}
3313
3314static int compare_attr(const void *ap, const void *bp)
3315{
3316 const struct automic_entry *a = ap;
3317 const struct automic_entry *b = bp;
3318 return (int)(a->attr - b->attr);
3319}
3320
3321/*
3322 * Check the availability of auto-mic switch;
3323 * Set up if really supported
3324 */
3325static int check_auto_mic_availability(struct hda_codec *codec)
3326{
3327 struct hda_gen_spec *spec = codec->spec;
3328 struct auto_pin_cfg *cfg = &spec->autocfg;
3329 unsigned int types;
3330 int i, num_pins;
3331
Takashi Iwaid12daf62013-01-07 16:32:11 +01003332 if (spec->suppress_auto_mic)
3333 return 0;
3334
Takashi Iwai352f7f92012-12-19 12:52:06 +01003335 types = 0;
3336 num_pins = 0;
3337 for (i = 0; i < cfg->num_inputs; i++) {
3338 hda_nid_t nid = cfg->inputs[i].pin;
3339 unsigned int attr;
3340 attr = snd_hda_codec_get_pincfg(codec, nid);
3341 attr = snd_hda_get_input_pin_attr(attr);
3342 if (types & (1 << attr))
3343 return 0; /* already occupied */
3344 switch (attr) {
3345 case INPUT_PIN_ATTR_INT:
3346 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3347 return 0; /* invalid type */
3348 break;
3349 case INPUT_PIN_ATTR_UNUSED:
3350 return 0; /* invalid entry */
3351 default:
3352 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
3353 return 0; /* invalid type */
3354 if (!spec->line_in_auto_switch &&
3355 cfg->inputs[i].type != AUTO_PIN_MIC)
3356 return 0; /* only mic is allowed */
3357 if (!is_jack_detectable(codec, nid))
3358 return 0; /* no unsol support */
3359 break;
3360 }
3361 if (num_pins >= MAX_AUTO_MIC_PINS)
3362 return 0;
3363 types |= (1 << attr);
3364 spec->am_entry[num_pins].pin = nid;
3365 spec->am_entry[num_pins].attr = attr;
3366 num_pins++;
3367 }
3368
3369 if (num_pins < 2)
3370 return 0;
3371
3372 spec->am_num_entries = num_pins;
3373 /* sort the am_entry in the order of attr so that the pin with a
3374 * higher attr will be selected when the jack is plugged.
3375 */
3376 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
3377 compare_attr, NULL);
3378
3379 if (!auto_mic_check_imux(codec))
3380 return 0;
3381
3382 spec->auto_mic = 1;
3383 spec->num_adc_nids = 1;
3384 spec->cur_mux[0] = spec->am_entry[0].idx;
3385 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
3386 spec->am_entry[0].pin,
3387 spec->am_entry[1].pin,
3388 spec->am_entry[2].pin);
3389
3390 return 0;
3391}
3392
3393
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003394/*
3395 * Parse the given BIOS configuration and set up the hda_gen_spec
3396 *
3397 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003398 * or a negative error code
3399 */
3400int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003401 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003402{
3403 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003404 int err;
3405
Takashi Iwai1c70a582013-01-11 17:48:22 +01003406 parse_user_hints(codec);
3407
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003408 if (cfg != &spec->autocfg) {
3409 spec->autocfg = *cfg;
3410 cfg = &spec->autocfg;
3411 }
3412
Takashi Iwai352f7f92012-12-19 12:52:06 +01003413 if (!cfg->line_outs) {
3414 if (cfg->dig_outs || cfg->dig_in_pin) {
3415 spec->multiout.max_channels = 2;
3416 spec->no_analog = 1;
3417 goto dig_only;
3418 }
3419 return 0; /* can't find valid BIOS pin config */
3420 }
3421
3422 if (!spec->no_primary_hp &&
3423 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
3424 cfg->line_outs <= cfg->hp_outs) {
3425 /* use HP as primary out */
3426 cfg->speaker_outs = cfg->line_outs;
3427 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3428 sizeof(cfg->speaker_pins));
3429 cfg->line_outs = cfg->hp_outs;
3430 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
3431 cfg->hp_outs = 0;
3432 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3433 cfg->line_out_type = AUTO_PIN_HP_OUT;
3434 }
3435
3436 err = parse_output_paths(codec);
3437 if (err < 0)
3438 return err;
3439 err = create_multi_channel_mode(codec);
3440 if (err < 0)
3441 return err;
3442 err = create_multi_out_ctls(codec, cfg);
3443 if (err < 0)
3444 return err;
3445 err = create_hp_out_ctls(codec);
3446 if (err < 0)
3447 return err;
3448 err = create_speaker_out_ctls(codec);
3449 if (err < 0)
3450 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003451 err = create_indep_hp_ctls(codec);
3452 if (err < 0)
3453 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01003454 err = create_loopback_mixing_ctl(codec);
3455 if (err < 0)
3456 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003457 err = create_shared_input(codec);
3458 if (err < 0)
3459 return err;
3460 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003461 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02003462 return err;
3463
Takashi Iwaia07a9492013-01-07 16:44:06 +01003464 spec->const_channel_count = spec->ext_channel_count;
3465 /* check the multiple speaker and headphone pins */
3466 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
3467 spec->const_channel_count = max(spec->const_channel_count,
3468 cfg->speaker_outs * 2);
3469 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
3470 spec->const_channel_count = max(spec->const_channel_count,
3471 cfg->hp_outs * 2);
3472 spec->multiout.max_channels = max(spec->ext_channel_count,
3473 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003474
3475 err = check_auto_mute_availability(codec);
3476 if (err < 0)
3477 return err;
3478
3479 err = check_dyn_adc_switch(codec);
3480 if (err < 0)
3481 return err;
3482
3483 if (!spec->shared_mic_hp) {
3484 err = check_auto_mic_availability(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003485 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 }
Takashi Iwai071c73a2006-08-23 18:34:06 +02003488
Takashi Iwai352f7f92012-12-19 12:52:06 +01003489 err = create_capture_mixers(codec);
3490 if (err < 0)
3491 return err;
3492
3493 err = parse_mic_boost(codec);
3494 if (err < 0)
3495 return err;
3496
Takashi Iwai978e77e2013-01-10 16:57:58 +01003497 if (spec->add_out_jack_modes) {
3498 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
3499 err = create_out_jack_modes(codec, cfg->line_outs,
3500 cfg->line_out_pins);
3501 if (err < 0)
3502 return err;
3503 }
3504 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
3505 err = create_out_jack_modes(codec, cfg->hp_outs,
3506 cfg->hp_pins);
3507 if (err < 0)
3508 return err;
3509 }
3510 }
3511
Takashi Iwai352f7f92012-12-19 12:52:06 +01003512 dig_only:
3513 parse_digital(codec);
3514
3515 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003517EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518
3519
3520/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003521 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003523
3524/* slave controls for virtual master */
3525static const char * const slave_pfxs[] = {
3526 "Front", "Surround", "Center", "LFE", "Side",
3527 "Headphone", "Speaker", "Mono", "Line Out",
3528 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01003529 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
3530 "Headphone Front", "Headphone Surround", "Headphone CLFE",
3531 "Headphone Side",
Takashi Iwai352f7f92012-12-19 12:52:06 +01003532 NULL,
3533};
3534
3535int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003537 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539
Takashi Iwai36502d02012-12-19 15:15:10 +01003540 if (spec->kctls.used) {
3541 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
3542 if (err < 0)
3543 return err;
3544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545
Takashi Iwai352f7f92012-12-19 12:52:06 +01003546 if (spec->multiout.dig_out_nid) {
3547 err = snd_hda_create_dig_out_ctls(codec,
3548 spec->multiout.dig_out_nid,
3549 spec->multiout.dig_out_nid,
3550 spec->pcm_rec[1].pcm_type);
3551 if (err < 0)
3552 return err;
3553 if (!spec->no_analog) {
3554 err = snd_hda_create_spdif_share_sw(codec,
3555 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 if (err < 0)
3557 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003558 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 }
3560 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003561 if (spec->dig_in_nid) {
3562 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
3563 if (err < 0)
3564 return err;
3565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566
Takashi Iwai352f7f92012-12-19 12:52:06 +01003567 /* if we have no master control, let's create it */
3568 if (!spec->no_analog &&
3569 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
3570 unsigned int vmaster_tlv[4];
3571 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
3572 HDA_OUTPUT, vmaster_tlv);
3573 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
3574 vmaster_tlv, slave_pfxs,
3575 "Playback Volume");
3576 if (err < 0)
3577 return err;
3578 }
3579 if (!spec->no_analog &&
3580 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
3581 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
3582 NULL, slave_pfxs,
3583 "Playback Switch",
3584 true, &spec->vmaster_mute.sw_kctl);
3585 if (err < 0)
3586 return err;
3587 if (spec->vmaster_mute.hook)
Takashi Iwaifd25a972012-12-20 14:57:18 +01003588 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
3589 spec->vmaster_mute_enum);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591
Takashi Iwai352f7f92012-12-19 12:52:06 +01003592 free_kctls(spec); /* no longer needed */
3593
3594 if (spec->shared_mic_hp) {
3595 int err;
3596 int nid = spec->autocfg.inputs[1].pin;
3597 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
3598 if (err < 0)
3599 return err;
3600 err = snd_hda_jack_detect_enable(codec, nid, 0);
3601 if (err < 0)
3602 return err;
3603 }
3604
3605 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
3606 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 return err;
3608
3609 return 0;
3610}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003611EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
3612
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613
3614/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003615 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003618static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
3619 struct hda_codec *codec,
3620 struct snd_pcm_substream *substream,
3621 int action)
3622{
3623 struct hda_gen_spec *spec = codec->spec;
3624 if (spec->pcm_playback_hook)
3625 spec->pcm_playback_hook(hinfo, codec, substream, action);
3626}
3627
Takashi Iwai352f7f92012-12-19 12:52:06 +01003628/*
3629 * Analog playback callbacks
3630 */
3631static int playback_pcm_open(struct hda_pcm_stream *hinfo,
3632 struct hda_codec *codec,
3633 struct snd_pcm_substream *substream)
3634{
3635 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003636 int err;
3637
3638 mutex_lock(&spec->pcm_mutex);
3639 err = snd_hda_multi_out_analog_open(codec,
3640 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003641 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003642 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003643 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003644 call_pcm_playback_hook(hinfo, codec, substream,
3645 HDA_GEN_PCM_ACT_OPEN);
3646 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003647 mutex_unlock(&spec->pcm_mutex);
3648 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003649}
3650
3651static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01003652 struct hda_codec *codec,
3653 unsigned int stream_tag,
3654 unsigned int format,
3655 struct snd_pcm_substream *substream)
3656{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003657 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003658 int err;
3659
3660 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
3661 stream_tag, format, substream);
3662 if (!err)
3663 call_pcm_playback_hook(hinfo, codec, substream,
3664 HDA_GEN_PCM_ACT_PREPARE);
3665 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003666}
Takashi Iwai97ec5582006-03-21 11:29:07 +01003667
Takashi Iwai352f7f92012-12-19 12:52:06 +01003668static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3669 struct hda_codec *codec,
3670 struct snd_pcm_substream *substream)
3671{
3672 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003673 int err;
3674
3675 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
3676 if (!err)
3677 call_pcm_playback_hook(hinfo, codec, substream,
3678 HDA_GEN_PCM_ACT_CLEANUP);
3679 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003680}
3681
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003682static int 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_MULTI_OUT);
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
3695static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
3696 struct hda_codec *codec,
3697 struct snd_pcm_substream *substream)
3698{
3699 struct hda_gen_spec *spec = codec->spec;
3700 int err = 0;
3701
3702 mutex_lock(&spec->pcm_mutex);
3703 if (!spec->indep_hp_enabled)
3704 err = -EBUSY;
3705 else
3706 spec->active_streams |= 1 << STREAM_INDEP_HP;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003707 call_pcm_playback_hook(hinfo, codec, substream,
3708 HDA_GEN_PCM_ACT_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003709 mutex_unlock(&spec->pcm_mutex);
3710 return err;
3711}
3712
3713static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
3714 struct hda_codec *codec,
3715 struct snd_pcm_substream *substream)
3716{
3717 struct hda_gen_spec *spec = codec->spec;
3718 mutex_lock(&spec->pcm_mutex);
3719 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003720 call_pcm_playback_hook(hinfo, codec, substream,
3721 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003722 mutex_unlock(&spec->pcm_mutex);
3723 return 0;
3724}
3725
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003726static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3727 struct hda_codec *codec,
3728 unsigned int stream_tag,
3729 unsigned int format,
3730 struct snd_pcm_substream *substream)
3731{
3732 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3733 call_pcm_playback_hook(hinfo, codec, substream,
3734 HDA_GEN_PCM_ACT_PREPARE);
3735 return 0;
3736}
3737
3738static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3739 struct hda_codec *codec,
3740 struct snd_pcm_substream *substream)
3741{
3742 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
3743 call_pcm_playback_hook(hinfo, codec, substream,
3744 HDA_GEN_PCM_ACT_CLEANUP);
3745 return 0;
3746}
3747
Takashi Iwai352f7f92012-12-19 12:52:06 +01003748/*
3749 * Digital out
3750 */
3751static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
3752 struct hda_codec *codec,
3753 struct snd_pcm_substream *substream)
3754{
3755 struct hda_gen_spec *spec = codec->spec;
3756 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3757}
3758
3759static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3760 struct hda_codec *codec,
3761 unsigned int stream_tag,
3762 unsigned int format,
3763 struct snd_pcm_substream *substream)
3764{
3765 struct hda_gen_spec *spec = codec->spec;
3766 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3767 stream_tag, format, substream);
3768}
3769
3770static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3771 struct hda_codec *codec,
3772 struct snd_pcm_substream *substream)
3773{
3774 struct hda_gen_spec *spec = codec->spec;
3775 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
3776}
3777
3778static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
3779 struct hda_codec *codec,
3780 struct snd_pcm_substream *substream)
3781{
3782 struct hda_gen_spec *spec = codec->spec;
3783 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3784}
3785
3786/*
3787 * Analog capture
3788 */
3789static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3790 struct hda_codec *codec,
3791 unsigned int stream_tag,
3792 unsigned int format,
3793 struct snd_pcm_substream *substream)
3794{
3795 struct hda_gen_spec *spec = codec->spec;
3796
3797 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01003798 stream_tag, 0, format);
3799 return 0;
3800}
3801
Takashi Iwai352f7f92012-12-19 12:52:06 +01003802static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3803 struct hda_codec *codec,
3804 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01003805{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003806 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01003807
Takashi Iwai352f7f92012-12-19 12:52:06 +01003808 snd_hda_codec_cleanup_stream(codec,
3809 spec->adc_nids[substream->number + 1]);
Takashi Iwai97ec5582006-03-21 11:29:07 +01003810 return 0;
3811}
3812
Takashi Iwai352f7f92012-12-19 12:52:06 +01003813/*
3814 */
3815static const struct hda_pcm_stream pcm_analog_playback = {
3816 .substreams = 1,
3817 .channels_min = 2,
3818 .channels_max = 8,
3819 /* NID is set in build_pcms */
3820 .ops = {
3821 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003822 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003823 .prepare = playback_pcm_prepare,
3824 .cleanup = playback_pcm_cleanup
3825 },
3826};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827
Takashi Iwai352f7f92012-12-19 12:52:06 +01003828static const struct hda_pcm_stream pcm_analog_capture = {
3829 .substreams = 1,
3830 .channels_min = 2,
3831 .channels_max = 2,
3832 /* NID is set in build_pcms */
3833};
3834
3835static const struct hda_pcm_stream pcm_analog_alt_playback = {
3836 .substreams = 1,
3837 .channels_min = 2,
3838 .channels_max = 2,
3839 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003840 .ops = {
3841 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003842 .close = alt_playback_pcm_close,
3843 .prepare = alt_playback_pcm_prepare,
3844 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003845 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01003846};
3847
3848static const struct hda_pcm_stream pcm_analog_alt_capture = {
3849 .substreams = 2, /* can be overridden */
3850 .channels_min = 2,
3851 .channels_max = 2,
3852 /* NID is set in build_pcms */
3853 .ops = {
3854 .prepare = alt_capture_pcm_prepare,
3855 .cleanup = alt_capture_pcm_cleanup
3856 },
3857};
3858
3859static const struct hda_pcm_stream pcm_digital_playback = {
3860 .substreams = 1,
3861 .channels_min = 2,
3862 .channels_max = 2,
3863 /* NID is set in build_pcms */
3864 .ops = {
3865 .open = dig_playback_pcm_open,
3866 .close = dig_playback_pcm_close,
3867 .prepare = dig_playback_pcm_prepare,
3868 .cleanup = dig_playback_pcm_cleanup
3869 },
3870};
3871
3872static const struct hda_pcm_stream pcm_digital_capture = {
3873 .substreams = 1,
3874 .channels_min = 2,
3875 .channels_max = 2,
3876 /* NID is set in build_pcms */
3877};
3878
3879/* Used by build_pcms to flag that a PCM has no playback stream */
3880static const struct hda_pcm_stream pcm_null_stream = {
3881 .substreams = 0,
3882 .channels_min = 0,
3883 .channels_max = 0,
3884};
3885
3886/*
3887 * dynamic changing ADC PCM streams
3888 */
3889static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
3890{
3891 struct hda_gen_spec *spec = codec->spec;
3892 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
3893
3894 if (spec->cur_adc && spec->cur_adc != new_adc) {
3895 /* stream is running, let's swap the current ADC */
3896 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
3897 spec->cur_adc = new_adc;
3898 snd_hda_codec_setup_stream(codec, new_adc,
3899 spec->cur_adc_stream_tag, 0,
3900 spec->cur_adc_format);
3901 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003903 return false;
3904}
3905
3906/* analog capture with dynamic dual-adc changes */
3907static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3908 struct hda_codec *codec,
3909 unsigned int stream_tag,
3910 unsigned int format,
3911 struct snd_pcm_substream *substream)
3912{
3913 struct hda_gen_spec *spec = codec->spec;
3914 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
3915 spec->cur_adc_stream_tag = stream_tag;
3916 spec->cur_adc_format = format;
3917 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
3918 return 0;
3919}
3920
3921static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3922 struct hda_codec *codec,
3923 struct snd_pcm_substream *substream)
3924{
3925 struct hda_gen_spec *spec = codec->spec;
3926 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
3927 spec->cur_adc = 0;
3928 return 0;
3929}
3930
3931static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
3932 .substreams = 1,
3933 .channels_min = 2,
3934 .channels_max = 2,
3935 .nid = 0, /* fill later */
3936 .ops = {
3937 .prepare = dyn_adc_capture_pcm_prepare,
3938 .cleanup = dyn_adc_capture_pcm_cleanup
3939 },
3940};
3941
Takashi Iwaif873e532012-12-20 16:58:39 +01003942static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
3943 const char *chip_name)
3944{
3945 char *p;
3946
3947 if (*str)
3948 return;
3949 strlcpy(str, chip_name, len);
3950
3951 /* drop non-alnum chars after a space */
3952 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
3953 if (!isalnum(p[1])) {
3954 *p = 0;
3955 break;
3956 }
3957 }
3958 strlcat(str, sfx, len);
3959}
3960
Takashi Iwai352f7f92012-12-19 12:52:06 +01003961/* build PCM streams based on the parsed results */
3962int snd_hda_gen_build_pcms(struct hda_codec *codec)
3963{
3964 struct hda_gen_spec *spec = codec->spec;
3965 struct hda_pcm *info = spec->pcm_rec;
3966 const struct hda_pcm_stream *p;
3967 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968
3969 codec->num_pcms = 1;
3970 codec->pcm_info = info;
3971
Takashi Iwai352f7f92012-12-19 12:52:06 +01003972 if (spec->no_analog)
3973 goto skip_analog;
3974
Takashi Iwaif873e532012-12-20 16:58:39 +01003975 fill_pcm_stream_name(spec->stream_name_analog,
3976 sizeof(spec->stream_name_analog),
3977 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003978 info->name = spec->stream_name_analog;
3979
3980 if (spec->multiout.num_dacs > 0) {
3981 p = spec->stream_analog_playback;
3982 if (!p)
3983 p = &pcm_analog_playback;
3984 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3985 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
3986 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
3987 spec->multiout.max_channels;
3988 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
3989 spec->autocfg.line_outs == 2)
3990 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
3991 snd_pcm_2_1_chmaps;
3992 }
3993 if (spec->num_adc_nids) {
3994 p = spec->stream_analog_capture;
3995 if (!p) {
3996 if (spec->dyn_adc_switch)
3997 p = &dyn_adc_pcm_analog_capture;
3998 else
3999 p = &pcm_analog_capture;
4000 }
4001 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4002 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
4003 }
4004
Takashi Iwai352f7f92012-12-19 12:52:06 +01004005 skip_analog:
4006 /* SPDIF for stream index #1 */
4007 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01004008 fill_pcm_stream_name(spec->stream_name_digital,
4009 sizeof(spec->stream_name_digital),
4010 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004011 codec->num_pcms = 2;
4012 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
4013 info = spec->pcm_rec + 1;
4014 info->name = spec->stream_name_digital;
4015 if (spec->dig_out_type)
4016 info->pcm_type = spec->dig_out_type;
4017 else
4018 info->pcm_type = HDA_PCM_TYPE_SPDIF;
4019 if (spec->multiout.dig_out_nid) {
4020 p = spec->stream_digital_playback;
4021 if (!p)
4022 p = &pcm_digital_playback;
4023 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4024 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
4025 }
4026 if (spec->dig_in_nid) {
4027 p = spec->stream_digital_capture;
4028 if (!p)
4029 p = &pcm_digital_capture;
4030 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4031 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
4032 }
4033 }
4034
4035 if (spec->no_analog)
4036 return 0;
4037
4038 /* If the use of more than one ADC is requested for the current
4039 * model, configure a second analog capture-only PCM.
4040 */
4041 have_multi_adcs = (spec->num_adc_nids > 1) &&
4042 !spec->dyn_adc_switch && !spec->auto_mic;
4043 /* Additional Analaog capture for index #2 */
4044 if (spec->alt_dac_nid || have_multi_adcs) {
4045 codec->num_pcms = 3;
4046 info = spec->pcm_rec + 2;
4047 info->name = spec->stream_name_analog;
4048 if (spec->alt_dac_nid) {
4049 p = spec->stream_analog_alt_playback;
4050 if (!p)
4051 p = &pcm_analog_alt_playback;
4052 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4053 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
4054 spec->alt_dac_nid;
4055 } else {
4056 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
4057 pcm_null_stream;
4058 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
4059 }
4060 if (have_multi_adcs) {
4061 p = spec->stream_analog_alt_capture;
4062 if (!p)
4063 p = &pcm_analog_alt_capture;
4064 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4065 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
4066 spec->adc_nids[1];
4067 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
4068 spec->num_adc_nids - 1;
4069 } else {
4070 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
4071 pcm_null_stream;
4072 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
4073 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 }
4075
4076 return 0;
4077}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004078EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
4079
4080
4081/*
4082 * Standard auto-parser initializations
4083 */
4084
Takashi Iwaid4156932013-01-07 10:08:02 +01004085/* configure the given path as a proper output */
Takashi Iwai2c12c302013-01-10 09:33:29 +01004086static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004087{
4088 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01004089 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004090
Takashi Iwai196c17662013-01-04 15:01:40 +01004091 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01004092 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004093 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01004094 pin = path->path[path->depth - 1];
Takashi Iwai2c12c302013-01-10 09:33:29 +01004095 restore_pin_ctl(codec, pin);
Takashi Iwaie1284af2013-01-03 16:33:02 +01004096 snd_hda_activate_path(codec, path, path->active, true);
4097 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004098}
4099
4100/* initialize primary output paths */
4101static void init_multi_out(struct hda_codec *codec)
4102{
4103 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004104 int i;
4105
Takashi Iwaid4156932013-01-07 10:08:02 +01004106 for (i = 0; i < spec->autocfg.line_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004107 set_output_and_unmute(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004108}
4109
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004110
Takashi Iwai2c12c302013-01-10 09:33:29 +01004111static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004112{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004113 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004114
Takashi Iwaid4156932013-01-07 10:08:02 +01004115 for (i = 0; i < num_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004116 set_output_and_unmute(codec, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004117}
4118
4119/* initialize hp and speaker paths */
4120static void init_extra_out(struct hda_codec *codec)
4121{
4122 struct hda_gen_spec *spec = codec->spec;
4123
4124 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004125 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004126 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
4127 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai2c12c302013-01-10 09:33:29 +01004128 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004129}
4130
4131/* initialize multi-io paths */
4132static void init_multi_io(struct hda_codec *codec)
4133{
4134 struct hda_gen_spec *spec = codec->spec;
4135 int i;
4136
4137 for (i = 0; i < spec->multi_ios; i++) {
4138 hda_nid_t pin = spec->multi_io[i].pin;
4139 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01004140 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004141 if (!path)
4142 continue;
4143 if (!spec->multi_io[i].ctl_in)
4144 spec->multi_io[i].ctl_in =
Takashi Iwai2c12c302013-01-10 09:33:29 +01004145 snd_hda_codec_get_pin_target(codec, pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004146 snd_hda_activate_path(codec, path, path->active, true);
4147 }
4148}
4149
Takashi Iwai352f7f92012-12-19 12:52:06 +01004150/* set up input pins and loopback paths */
4151static void init_analog_input(struct hda_codec *codec)
4152{
4153 struct hda_gen_spec *spec = codec->spec;
4154 struct auto_pin_cfg *cfg = &spec->autocfg;
4155 int i;
4156
4157 for (i = 0; i < cfg->num_inputs; i++) {
4158 hda_nid_t nid = cfg->inputs[i].pin;
4159 if (is_input_pin(codec, nid))
Takashi Iwai2c12c302013-01-10 09:33:29 +01004160 restore_pin_ctl(codec, nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004161
4162 /* init loopback inputs */
4163 if (spec->mixer_nid) {
4164 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01004165 path = snd_hda_get_path_from_idx(codec, spec->loopback_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004166 if (path)
4167 snd_hda_activate_path(codec, path,
4168 path->active, false);
4169 }
4170 }
4171}
4172
4173/* initialize ADC paths */
4174static void init_input_src(struct hda_codec *codec)
4175{
4176 struct hda_gen_spec *spec = codec->spec;
4177 struct hda_input_mux *imux = &spec->input_mux;
4178 struct nid_path *path;
4179 int i, c, nums;
4180
4181 if (spec->dyn_adc_switch)
4182 nums = 1;
4183 else
4184 nums = spec->num_adc_nids;
4185
4186 for (c = 0; c < nums; c++) {
4187 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01004188 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004189 if (path) {
4190 bool active = path->active;
4191 if (i == spec->cur_mux[c])
4192 active = true;
4193 snd_hda_activate_path(codec, path, active, false);
4194 }
4195 }
4196 }
4197
4198 if (spec->shared_mic_hp)
4199 update_shared_mic_hp(codec, spec->cur_mux[0]);
4200
4201 if (spec->cap_sync_hook)
4202 spec->cap_sync_hook(codec);
4203}
4204
4205/* set right pin controls for digital I/O */
4206static void init_digital(struct hda_codec *codec)
4207{
4208 struct hda_gen_spec *spec = codec->spec;
4209 int i;
4210 hda_nid_t pin;
4211
Takashi Iwaid4156932013-01-07 10:08:02 +01004212 for (i = 0; i < spec->autocfg.dig_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004213 set_output_and_unmute(codec, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004214 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01004215 if (pin) {
4216 struct nid_path *path;
Takashi Iwai2c12c302013-01-10 09:33:29 +01004217 restore_pin_ctl(codec, pin);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01004218 path = snd_hda_get_path_from_idx(codec, spec->digin_path);
4219 if (path)
4220 snd_hda_activate_path(codec, path, path->active, false);
4221 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004222}
4223
Takashi Iwai973e4972012-12-20 15:16:09 +01004224/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
4225 * invalid unsol tags by some reason
4226 */
4227static void clear_unsol_on_unused_pins(struct hda_codec *codec)
4228{
4229 int i;
4230
4231 for (i = 0; i < codec->init_pins.used; i++) {
4232 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
4233 hda_nid_t nid = pin->nid;
4234 if (is_jack_detectable(codec, nid) &&
4235 !snd_hda_jack_tbl_get(codec, nid))
4236 snd_hda_codec_update_cache(codec, nid, 0,
4237 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
4238 }
4239}
4240
Takashi Iwai5187ac12013-01-07 12:52:16 +01004241/*
4242 * initialize the generic spec;
4243 * this can be put as patch_ops.init function
4244 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004245int snd_hda_gen_init(struct hda_codec *codec)
4246{
4247 struct hda_gen_spec *spec = codec->spec;
4248
4249 if (spec->init_hook)
4250 spec->init_hook(codec);
4251
4252 snd_hda_apply_verbs(codec);
4253
Takashi Iwai3bbcd272012-12-20 11:50:58 +01004254 codec->cached_write = 1;
4255
Takashi Iwai352f7f92012-12-19 12:52:06 +01004256 init_multi_out(codec);
4257 init_extra_out(codec);
4258 init_multi_io(codec);
4259 init_analog_input(codec);
4260 init_input_src(codec);
4261 init_digital(codec);
4262
Takashi Iwai973e4972012-12-20 15:16:09 +01004263 clear_unsol_on_unused_pins(codec);
4264
Takashi Iwai352f7f92012-12-19 12:52:06 +01004265 /* call init functions of standard auto-mute helpers */
Takashi Iwai5d550e12012-12-19 15:16:44 +01004266 snd_hda_gen_hp_automute(codec, NULL);
4267 snd_hda_gen_line_automute(codec, NULL);
4268 snd_hda_gen_mic_autoswitch(codec, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004269
Takashi Iwai3bbcd272012-12-20 11:50:58 +01004270 snd_hda_codec_flush_amp_cache(codec);
4271 snd_hda_codec_flush_cmd_cache(codec);
4272
Takashi Iwai352f7f92012-12-19 12:52:06 +01004273 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
4274 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
4275
4276 hda_call_check_power_status(codec, 0x01);
4277 return 0;
4278}
Takashi Iwaifce52a32013-01-07 12:42:48 +01004279EXPORT_SYMBOL_HDA(snd_hda_gen_init);
4280
Takashi Iwai5187ac12013-01-07 12:52:16 +01004281/*
4282 * free the generic spec;
4283 * this can be put as patch_ops.free function
4284 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01004285void snd_hda_gen_free(struct hda_codec *codec)
4286{
4287 snd_hda_gen_spec_free(codec->spec);
4288 kfree(codec->spec);
4289 codec->spec = NULL;
4290}
4291EXPORT_SYMBOL_HDA(snd_hda_gen_free);
4292
4293#ifdef CONFIG_PM
Takashi Iwai5187ac12013-01-07 12:52:16 +01004294/*
4295 * check the loopback power save state;
4296 * this can be put as patch_ops.check_power_status function
4297 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01004298int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
4299{
4300 struct hda_gen_spec *spec = codec->spec;
4301 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
4302}
4303EXPORT_SYMBOL_HDA(snd_hda_gen_check_power_status);
4304#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01004305
4306
4307/*
4308 * the generic codec support
4309 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310
Takashi Iwai352f7f92012-12-19 12:52:06 +01004311static const struct hda_codec_ops generic_patch_ops = {
4312 .build_controls = snd_hda_gen_build_controls,
4313 .build_pcms = snd_hda_gen_build_pcms,
4314 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01004315 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004316 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02004317#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01004318 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02004319#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320};
4321
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322int snd_hda_parse_generic_codec(struct hda_codec *codec)
4323{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004324 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 int err;
4326
Takashi Iwaie560d8d2005-09-09 14:21:46 +02004327 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004328 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004330 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004333 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
4334 if (err < 0)
4335 return err;
4336
4337 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004338 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 goto error;
4340
4341 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 return 0;
4343
Takashi Iwai352f7f92012-12-19 12:52:06 +01004344error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01004345 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346 return err;
4347}
Takashi Iwaifce52a32013-01-07 12:42:48 +01004348EXPORT_SYMBOL_HDA(snd_hda_parse_generic_codec);