blob: 19d014a6a40e73654889a3daafb1b2ce4a78b09a [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 Iwai55196ff2013-01-24 17:32:56 +010027#include <linux/delay.h>
Takashi Iwaif873e532012-12-20 16:58:39 +010028#include <linux/ctype.h>
29#include <linux/string.h>
Takashi Iwai294765582013-01-17 09:52:11 +010030#include <linux/bitops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <sound/core.h>
Takashi Iwai352f7f92012-12-19 12:52:06 +010032#include <sound/jack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "hda_codec.h"
34#include "hda_local.h"
Takashi Iwai352f7f92012-12-19 12:52:06 +010035#include "hda_auto_parser.h"
36#include "hda_jack.h"
37#include "hda_generic.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Takashi Iwai352f7f92012-12-19 12:52:06 +010040/* initialize hda_gen_spec struct */
41int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
Takashi Iwai352f7f92012-12-19 12:52:06 +010043 snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
Takashi Iwai352f7f92012-12-19 12:52:06 +010044 snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
Takashi Iwai38cf6f12012-12-21 14:09:42 +010045 mutex_init(&spec->pcm_mutex);
Takashi Iwai352f7f92012-12-19 12:52:06 +010046 return 0;
47}
48EXPORT_SYMBOL_HDA(snd_hda_gen_spec_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Takashi Iwai12c93df2012-12-19 14:38:33 +010050struct snd_kcontrol_new *
51snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
52 const struct snd_kcontrol_new *temp)
Takashi Iwai352f7f92012-12-19 12:52:06 +010053{
54 struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
55 if (!knew)
56 return NULL;
57 *knew = *temp;
58 if (name)
59 knew->name = kstrdup(name, GFP_KERNEL);
60 else if (knew->name)
61 knew->name = kstrdup(knew->name, GFP_KERNEL);
62 if (!knew->name)
63 return NULL;
64 return knew;
65}
Takashi Iwai12c93df2012-12-19 14:38:33 +010066EXPORT_SYMBOL_HDA(snd_hda_gen_add_kctl);
Takashi Iwai352f7f92012-12-19 12:52:06 +010067
68static void free_kctls(struct hda_gen_spec *spec)
69{
70 if (spec->kctls.list) {
71 struct snd_kcontrol_new *kctl = spec->kctls.list;
72 int i;
73 for (i = 0; i < spec->kctls.used; i++)
74 kfree(kctl[i].name);
75 }
76 snd_array_free(&spec->kctls);
77}
78
Takashi Iwai352f7f92012-12-19 12:52:06 +010079void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
80{
81 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 return;
Takashi Iwai352f7f92012-12-19 12:52:06 +010083 free_kctls(spec);
Takashi Iwai352f7f92012-12-19 12:52:06 +010084 snd_array_free(&spec->paths);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
Takashi Iwai352f7f92012-12-19 12:52:06 +010086EXPORT_SYMBOL_HDA(snd_hda_gen_spec_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88/*
Takashi Iwai1c70a582013-01-11 17:48:22 +010089 * store user hints
90 */
91static void parse_user_hints(struct hda_codec *codec)
92{
93 struct hda_gen_spec *spec = codec->spec;
94 int val;
95
96 val = snd_hda_get_bool_hint(codec, "jack_detect");
97 if (val >= 0)
98 codec->no_jack_detect = !val;
99 val = snd_hda_get_bool_hint(codec, "inv_jack_detect");
100 if (val >= 0)
101 codec->inv_jack_detect = !!val;
102 val = snd_hda_get_bool_hint(codec, "trigger_sense");
103 if (val >= 0)
104 codec->no_trigger_sense = !val;
105 val = snd_hda_get_bool_hint(codec, "inv_eapd");
106 if (val >= 0)
107 codec->inv_eapd = !!val;
108 val = snd_hda_get_bool_hint(codec, "pcm_format_first");
109 if (val >= 0)
110 codec->pcm_format_first = !!val;
111 val = snd_hda_get_bool_hint(codec, "sticky_stream");
112 if (val >= 0)
113 codec->no_sticky_stream = !val;
114 val = snd_hda_get_bool_hint(codec, "spdif_status_reset");
115 if (val >= 0)
116 codec->spdif_status_reset = !!val;
117 val = snd_hda_get_bool_hint(codec, "pin_amp_workaround");
118 if (val >= 0)
119 codec->pin_amp_workaround = !!val;
120 val = snd_hda_get_bool_hint(codec, "single_adc_amp");
121 if (val >= 0)
122 codec->single_adc_amp = !!val;
123
Takashi Iwaif72706b2013-01-16 18:20:07 +0100124 val = snd_hda_get_bool_hint(codec, "auto_mute");
125 if (val >= 0)
126 spec->suppress_auto_mute = !val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100127 val = snd_hda_get_bool_hint(codec, "auto_mic");
128 if (val >= 0)
129 spec->suppress_auto_mic = !val;
130 val = snd_hda_get_bool_hint(codec, "line_in_auto_switch");
131 if (val >= 0)
132 spec->line_in_auto_switch = !!val;
133 val = snd_hda_get_bool_hint(codec, "need_dac_fix");
134 if (val >= 0)
135 spec->need_dac_fix = !!val;
136 val = snd_hda_get_bool_hint(codec, "primary_hp");
137 if (val >= 0)
138 spec->no_primary_hp = !val;
139 val = snd_hda_get_bool_hint(codec, "multi_cap_vol");
140 if (val >= 0)
141 spec->multi_cap_vol = !!val;
142 val = snd_hda_get_bool_hint(codec, "inv_dmic_split");
143 if (val >= 0)
144 spec->inv_dmic_split = !!val;
145 val = snd_hda_get_bool_hint(codec, "indep_hp");
146 if (val >= 0)
147 spec->indep_hp = !!val;
148 val = snd_hda_get_bool_hint(codec, "add_stereo_mix_input");
149 if (val >= 0)
150 spec->add_stereo_mix_input = !!val;
151 val = snd_hda_get_bool_hint(codec, "add_out_jack_modes");
152 if (val >= 0)
153 spec->add_out_jack_modes = !!val;
Takashi Iwai294765582013-01-17 09:52:11 +0100154 val = snd_hda_get_bool_hint(codec, "add_in_jack_modes");
155 if (val >= 0)
156 spec->add_in_jack_modes = !!val;
Takashi Iwai55196ff2013-01-24 17:32:56 +0100157 val = snd_hda_get_bool_hint(codec, "power_down_unused");
158 if (val >= 0)
159 spec->power_down_unused = !!val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100160
161 if (!snd_hda_get_int_hint(codec, "mixer_nid", &val))
162 spec->mixer_nid = val;
163}
164
165/*
Takashi Iwai2c12c302013-01-10 09:33:29 +0100166 * pin control value accesses
167 */
168
169#define update_pin_ctl(codec, pin, val) \
170 snd_hda_codec_update_cache(codec, pin, 0, \
171 AC_VERB_SET_PIN_WIDGET_CONTROL, val)
172
173/* restore the pinctl based on the cached value */
174static inline void restore_pin_ctl(struct hda_codec *codec, hda_nid_t pin)
175{
176 update_pin_ctl(codec, pin, snd_hda_codec_get_pin_target(codec, pin));
177}
178
179/* set the pinctl target value and write it if requested */
180static void set_pin_target(struct hda_codec *codec, hda_nid_t pin,
181 unsigned int val, bool do_write)
182{
183 if (!pin)
184 return;
185 val = snd_hda_correct_pin_ctl(codec, pin, val);
186 snd_hda_codec_set_pin_target(codec, pin, val);
187 if (do_write)
188 update_pin_ctl(codec, pin, val);
189}
190
191/* set pinctl target values for all given pins */
192static void set_pin_targets(struct hda_codec *codec, int num_pins,
193 hda_nid_t *pins, unsigned int val)
194{
195 int i;
196 for (i = 0; i < num_pins; i++)
197 set_pin_target(codec, pins[i], val, false);
198}
199
200/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100201 * parsing paths
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100204/* return the position of NID in the list, or -1 if not found */
205static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
206{
207 int i;
208 for (i = 0; i < nums; i++)
209 if (list[i] == nid)
210 return i;
211 return -1;
212}
213
214/* return true if the given NID is contained in the path */
215static bool is_nid_contained(struct nid_path *path, hda_nid_t nid)
216{
217 return find_idx_in_nid_list(nid, path->path, path->depth) >= 0;
218}
219
Takashi Iwaif5172a72013-01-04 13:19:55 +0100220static struct nid_path *get_nid_path(struct hda_codec *codec,
221 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100222 int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100224 struct hda_gen_spec *spec = codec->spec;
225 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Takashi Iwai352f7f92012-12-19 12:52:06 +0100227 for (i = 0; i < spec->paths.used; i++) {
228 struct nid_path *path = snd_array_elem(&spec->paths, i);
229 if (path->depth <= 0)
230 continue;
231 if ((!from_nid || path->path[0] == from_nid) &&
Takashi Iwaif5172a72013-01-04 13:19:55 +0100232 (!to_nid || path->path[path->depth - 1] == to_nid)) {
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100233 if (!anchor_nid ||
234 (anchor_nid > 0 && is_nid_contained(path, anchor_nid)) ||
235 (anchor_nid < 0 && !is_nid_contained(path, anchor_nid)))
Takashi Iwaif5172a72013-01-04 13:19:55 +0100236 return path;
237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 }
239 return NULL;
240}
Takashi Iwaif5172a72013-01-04 13:19:55 +0100241
242/* get the path between the given NIDs;
243 * passing 0 to either @pin or @dac behaves as a wildcard
244 */
245struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
246 hda_nid_t from_nid, hda_nid_t to_nid)
247{
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100248 return get_nid_path(codec, from_nid, to_nid, 0);
Takashi Iwaif5172a72013-01-04 13:19:55 +0100249}
Takashi Iwai352f7f92012-12-19 12:52:06 +0100250EXPORT_SYMBOL_HDA(snd_hda_get_nid_path);
251
Takashi Iwai196c17662013-01-04 15:01:40 +0100252/* get the index number corresponding to the path instance;
253 * the index starts from 1, for easier checking the invalid value
254 */
255int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
256{
257 struct hda_gen_spec *spec = codec->spec;
258 struct nid_path *array = spec->paths.list;
259 ssize_t idx;
260
261 if (!spec->paths.used)
262 return 0;
263 idx = path - array;
264 if (idx < 0 || idx >= spec->paths.used)
265 return 0;
266 return idx + 1;
267}
Takashi Iwai4bd01e92013-01-22 15:17:20 +0100268EXPORT_SYMBOL_HDA(snd_hda_get_path_idx);
Takashi Iwai196c17662013-01-04 15:01:40 +0100269
270/* get the path instance corresponding to the given index number */
271struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
272{
273 struct hda_gen_spec *spec = codec->spec;
274
275 if (idx <= 0 || idx > spec->paths.used)
276 return NULL;
277 return snd_array_elem(&spec->paths, idx - 1);
278}
Takashi Iwai4bd01e92013-01-22 15:17:20 +0100279EXPORT_SYMBOL_HDA(snd_hda_get_path_from_idx);
Takashi Iwai196c17662013-01-04 15:01:40 +0100280
Takashi Iwai352f7f92012-12-19 12:52:06 +0100281/* check whether the given DAC is already found in any existing paths */
282static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
283{
284 struct hda_gen_spec *spec = codec->spec;
285 int i;
286
287 for (i = 0; i < spec->paths.used; i++) {
288 struct nid_path *path = snd_array_elem(&spec->paths, i);
289 if (path->path[0] == nid)
290 return true;
291 }
292 return false;
293}
294
295/* check whether the given two widgets can be connected */
296static bool is_reachable_path(struct hda_codec *codec,
297 hda_nid_t from_nid, hda_nid_t to_nid)
298{
299 if (!from_nid || !to_nid)
300 return false;
301 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
302}
303
304/* nid, dir and idx */
305#define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
306
307/* check whether the given ctl is already assigned in any path elements */
308static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
309{
310 struct hda_gen_spec *spec = codec->spec;
311 int i;
312
313 val &= AMP_VAL_COMPARE_MASK;
314 for (i = 0; i < spec->paths.used; i++) {
315 struct nid_path *path = snd_array_elem(&spec->paths, i);
316 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
317 return true;
318 }
319 return false;
320}
321
322/* check whether a control with the given (nid, dir, idx) was assigned */
323static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai8999bf02013-01-18 11:01:33 +0100324 int dir, int idx, int type)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100325{
326 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
Takashi Iwai8999bf02013-01-18 11:01:33 +0100327 return is_ctl_used(codec, val, type);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100328}
329
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100330static void print_nid_path(const char *pfx, struct nid_path *path)
331{
332 char buf[40];
333 int i;
334
335
336 buf[0] = 0;
337 for (i = 0; i < path->depth; i++) {
338 char tmp[4];
339 sprintf(tmp, ":%02x", path->path[i]);
340 strlcat(buf, tmp, sizeof(buf));
341 }
342 snd_printdd("%s path: depth=%d %s\n", pfx, path->depth, buf);
343}
344
Takashi Iwai352f7f92012-12-19 12:52:06 +0100345/* called recursively */
346static bool __parse_nid_path(struct hda_codec *codec,
347 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100348 int anchor_nid, struct nid_path *path,
349 int depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100350{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100351 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100352 int i, nums;
353
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100354 if (to_nid == anchor_nid)
355 anchor_nid = 0; /* anchor passed */
356 else if (to_nid == (hda_nid_t)(-anchor_nid))
357 return false; /* hit the exclusive nid */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100358
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100359 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100360 for (i = 0; i < nums; i++) {
361 if (conn[i] != from_nid) {
362 /* special case: when from_nid is 0,
363 * try to find an empty DAC
364 */
365 if (from_nid ||
366 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
367 is_dac_already_used(codec, conn[i]))
368 continue;
369 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100370 /* anchor is not requested or already passed? */
371 if (anchor_nid <= 0)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100372 goto found;
373 }
374 if (depth >= MAX_NID_PATH_DEPTH)
375 return false;
376 for (i = 0; i < nums; i++) {
377 unsigned int type;
378 type = get_wcaps_type(get_wcaps(codec, conn[i]));
379 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
380 type == AC_WID_PIN)
381 continue;
382 if (__parse_nid_path(codec, from_nid, conn[i],
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100383 anchor_nid, path, depth + 1))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100384 goto found;
385 }
386 return false;
387
388 found:
389 path->path[path->depth] = conn[i];
390 path->idx[path->depth + 1] = i;
391 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
392 path->multi[path->depth + 1] = 1;
393 path->depth++;
394 return true;
395}
396
397/* parse the widget path from the given nid to the target nid;
398 * when @from_nid is 0, try to find an empty DAC;
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100399 * when @anchor_nid is set to a positive value, only paths through the widget
400 * with the given value are evaluated.
401 * when @anchor_nid is set to a negative value, paths through the widget
402 * with the negative of given value are excluded, only other paths are chosen.
403 * when @anchor_nid is zero, no special handling about path selection.
Takashi Iwai352f7f92012-12-19 12:52:06 +0100404 */
405bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100406 hda_nid_t to_nid, int anchor_nid,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100407 struct nid_path *path)
408{
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100409 if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100410 path->path[path->depth] = to_nid;
411 path->depth++;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100412 return true;
413 }
414 return false;
415}
416EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100419 * parse the path between the given NIDs and add to the path list.
420 * if no valid path is found, return NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100422struct nid_path *
423snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100424 hda_nid_t to_nid, int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100426 struct hda_gen_spec *spec = codec->spec;
427 struct nid_path *path;
428
429 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
430 return NULL;
431
Takashi Iwaif5172a72013-01-04 13:19:55 +0100432 /* check whether the path has been already added */
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100433 path = get_nid_path(codec, from_nid, to_nid, anchor_nid);
Takashi Iwaif5172a72013-01-04 13:19:55 +0100434 if (path)
435 return path;
436
Takashi Iwai352f7f92012-12-19 12:52:06 +0100437 path = snd_array_new(&spec->paths);
438 if (!path)
439 return NULL;
440 memset(path, 0, sizeof(*path));
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100441 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100442 return path;
443 /* push back */
444 spec->paths.used--;
445 return NULL;
446}
447EXPORT_SYMBOL_HDA(snd_hda_add_new_path);
448
Takashi Iwai980428c2013-01-09 09:28:20 +0100449/* clear the given path as invalid so that it won't be picked up later */
450static void invalidate_nid_path(struct hda_codec *codec, int idx)
451{
452 struct nid_path *path = snd_hda_get_path_from_idx(codec, idx);
453 if (!path)
454 return;
455 memset(path, 0, sizeof(*path));
456}
457
Takashi Iwai352f7f92012-12-19 12:52:06 +0100458/* look for an empty DAC slot */
459static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
460 bool is_digital)
461{
462 struct hda_gen_spec *spec = codec->spec;
463 bool cap_digital;
464 int i;
465
466 for (i = 0; i < spec->num_all_dacs; i++) {
467 hda_nid_t nid = spec->all_dacs[i];
468 if (!nid || is_dac_already_used(codec, nid))
469 continue;
470 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
471 if (is_digital != cap_digital)
472 continue;
473 if (is_reachable_path(codec, nid, pin))
474 return nid;
475 }
476 return 0;
477}
478
479/* replace the channels in the composed amp value with the given number */
480static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
481{
482 val &= ~(0x3U << 16);
483 val |= chs << 16;
484 return val;
485}
486
487/* check whether the widget has the given amp capability for the direction */
488static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
489 int dir, unsigned int bits)
490{
491 if (!nid)
492 return false;
493 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
494 if (query_amp_caps(codec, nid, dir) & bits)
495 return true;
496 return false;
497}
498
David Henningsson99a55922013-01-16 15:58:44 +0100499static bool same_amp_caps(struct hda_codec *codec, hda_nid_t nid1,
500 hda_nid_t nid2, int dir)
501{
502 if (!(get_wcaps(codec, nid1) & (1 << (dir + 1))))
503 return !(get_wcaps(codec, nid2) & (1 << (dir + 1)));
504 return (query_amp_caps(codec, nid1, dir) ==
505 query_amp_caps(codec, nid2, dir));
506}
507
Takashi Iwai352f7f92012-12-19 12:52:06 +0100508#define nid_has_mute(codec, nid, dir) \
509 check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
510#define nid_has_volume(codec, nid, dir) \
511 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
512
513/* look for a widget suitable for assigning a mute switch in the path */
514static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
515 struct nid_path *path)
516{
517 int i;
518
519 for (i = path->depth - 1; i >= 0; i--) {
520 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
521 return path->path[i];
522 if (i != path->depth - 1 && i != 0 &&
523 nid_has_mute(codec, path->path[i], HDA_INPUT))
524 return path->path[i];
525 }
526 return 0;
527}
528
529/* look for a widget suitable for assigning a volume ctl in the path */
530static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
531 struct nid_path *path)
532{
533 int i;
534
535 for (i = path->depth - 1; i >= 0; i--) {
536 if (nid_has_volume(codec, path->path[i], HDA_OUTPUT))
537 return path->path[i];
538 }
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200539 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540}
541
542/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100543 * path activation / deactivation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100545
546/* can have the amp-in capability? */
547static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100549 hda_nid_t nid = path->path[idx];
550 unsigned int caps = get_wcaps(codec, nid);
551 unsigned int type = get_wcaps_type(caps);
552
553 if (!(caps & AC_WCAP_IN_AMP))
554 return false;
555 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
556 return false;
557 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
559
Takashi Iwai352f7f92012-12-19 12:52:06 +0100560/* can have the amp-out capability? */
561static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100563 hda_nid_t nid = path->path[idx];
564 unsigned int caps = get_wcaps(codec, nid);
565 unsigned int type = get_wcaps_type(caps);
566
567 if (!(caps & AC_WCAP_OUT_AMP))
568 return false;
569 if (type == AC_WID_PIN && !idx) /* only for output pins */
570 return false;
571 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572}
573
Takashi Iwai352f7f92012-12-19 12:52:06 +0100574/* check whether the given (nid,dir,idx) is active */
575static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai7dddf2ae2013-01-24 16:31:35 +0100576 unsigned int dir, unsigned int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100578 struct hda_gen_spec *spec = codec->spec;
579 int i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Takashi Iwai352f7f92012-12-19 12:52:06 +0100581 for (n = 0; n < spec->paths.used; n++) {
582 struct nid_path *path = snd_array_elem(&spec->paths, n);
583 if (!path->active)
584 continue;
585 for (i = 0; i < path->depth; i++) {
586 if (path->path[i] == nid) {
587 if (dir == HDA_OUTPUT || path->idx[i] == idx)
588 return true;
589 break;
590 }
591 }
592 }
593 return false;
594}
595
596/* get the default amp value for the target state */
597static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai8999bf02013-01-18 11:01:33 +0100598 int dir, unsigned int caps, bool enable)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100599{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100600 unsigned int val = 0;
601
Takashi Iwai352f7f92012-12-19 12:52:06 +0100602 if (caps & AC_AMPCAP_NUM_STEPS) {
603 /* set to 0dB */
604 if (enable)
605 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
606 }
607 if (caps & AC_AMPCAP_MUTE) {
608 if (!enable)
609 val |= HDA_AMP_MUTE;
610 }
611 return val;
612}
613
614/* initialize the amp value (only at the first time) */
615static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
616{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100617 unsigned int caps = query_amp_caps(codec, nid, dir);
618 int val = get_amp_val_to_activate(codec, nid, dir, caps, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100619 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
620}
621
Takashi Iwai8999bf02013-01-18 11:01:33 +0100622/* calculate amp value mask we can modify;
623 * if the given amp is controlled by mixers, don't touch it
624 */
625static unsigned int get_amp_mask_to_modify(struct hda_codec *codec,
626 hda_nid_t nid, int dir, int idx,
627 unsigned int caps)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100628{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100629 unsigned int mask = 0xff;
630
631 if (caps & AC_AMPCAP_MUTE) {
632 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_MUTE_CTL))
633 mask &= ~0x80;
634 }
635 if (caps & AC_AMPCAP_NUM_STEPS) {
636 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
637 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
638 mask &= ~0x7f;
639 }
640 return mask;
641}
642
643static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
644 int idx, int idx_to_check, bool enable)
645{
646 unsigned int caps;
647 unsigned int mask, val;
648
Takashi Iwai7dddf2ae2013-01-24 16:31:35 +0100649 if (!enable && is_active_nid(codec, nid, dir, idx_to_check))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100650 return;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100651
652 caps = query_amp_caps(codec, nid, dir);
653 val = get_amp_val_to_activate(codec, nid, dir, caps, enable);
654 mask = get_amp_mask_to_modify(codec, nid, dir, idx_to_check, caps);
655 if (!mask)
656 return;
657
658 val &= mask;
659 snd_hda_codec_amp_stereo(codec, nid, dir, idx, mask, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100660}
661
662static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
663 int i, bool enable)
664{
665 hda_nid_t nid = path->path[i];
666 init_amp(codec, nid, HDA_OUTPUT, 0);
Takashi Iwai8999bf02013-01-18 11:01:33 +0100667 activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100668}
669
670static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
671 int i, bool enable, bool add_aamix)
672{
673 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100674 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100675 int n, nums, idx;
676 int type;
677 hda_nid_t nid = path->path[i];
678
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100679 nums = snd_hda_get_conn_list(codec, nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100680 type = get_wcaps_type(get_wcaps(codec, nid));
681 if (type == AC_WID_PIN ||
682 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
683 nums = 1;
684 idx = 0;
685 } else
686 idx = path->idx[i];
687
688 for (n = 0; n < nums; n++)
689 init_amp(codec, nid, HDA_INPUT, n);
690
Takashi Iwai352f7f92012-12-19 12:52:06 +0100691 /* here is a little bit tricky in comparison with activate_amp_out();
692 * when aa-mixer is available, we need to enable the path as well
693 */
694 for (n = 0; n < nums; n++) {
Takashi Iwaie4a395e2013-01-23 17:00:31 +0100695 if (n != idx && (!add_aamix || conn[n] != spec->mixer_merge_nid))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100696 continue;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100697 activate_amp(codec, nid, HDA_INPUT, n, idx, enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 }
699}
700
Takashi Iwai352f7f92012-12-19 12:52:06 +0100701/* activate or deactivate the given path
702 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100704void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
705 bool enable, bool add_aamix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
Takashi Iwai55196ff2013-01-24 17:32:56 +0100707 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100708 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Takashi Iwai352f7f92012-12-19 12:52:06 +0100710 if (!enable)
711 path->active = false;
712
713 for (i = path->depth - 1; i >= 0; i--) {
Takashi Iwai55196ff2013-01-24 17:32:56 +0100714 hda_nid_t nid = path->path[i];
715 if (enable && spec->power_down_unused) {
716 /* make sure the widget is powered up */
717 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0))
718 snd_hda_codec_write(codec, nid, 0,
719 AC_VERB_SET_POWER_STATE,
720 AC_PWRST_D0);
721 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100722 if (enable && path->multi[i])
Takashi Iwai55196ff2013-01-24 17:32:56 +0100723 snd_hda_codec_write_cache(codec, nid, 0,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100724 AC_VERB_SET_CONNECT_SEL,
725 path->idx[i]);
726 if (has_amp_in(codec, path, i))
727 activate_amp_in(codec, path, i, enable, add_aamix);
728 if (has_amp_out(codec, path, i))
729 activate_amp_out(codec, path, i, enable);
730 }
731
732 if (enable)
733 path->active = true;
734}
735EXPORT_SYMBOL_HDA(snd_hda_activate_path);
736
Takashi Iwai55196ff2013-01-24 17:32:56 +0100737/* if the given path is inactive, put widgets into D3 (only if suitable) */
738static void path_power_down_sync(struct hda_codec *codec, struct nid_path *path)
739{
740 struct hda_gen_spec *spec = codec->spec;
741 bool changed;
742 int i;
743
744 if (!spec->power_down_unused || path->active)
745 return;
746
747 for (i = 0; i < path->depth; i++) {
748 hda_nid_t nid = path->path[i];
749 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D3)) {
750 snd_hda_codec_write(codec, nid, 0,
751 AC_VERB_SET_POWER_STATE,
752 AC_PWRST_D3);
753 changed = true;
754 }
755 }
756
757 if (changed) {
758 msleep(10);
759 snd_hda_codec_read(codec, path->path[0], 0,
760 AC_VERB_GET_POWER_STATE, 0);
761 }
762}
763
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100764/* turn on/off EAPD on the given pin */
765static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
766{
767 struct hda_gen_spec *spec = codec->spec;
768 if (spec->own_eapd_ctl ||
769 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
770 return;
Takashi Iwaiecac3ed2012-12-21 15:23:01 +0100771 if (codec->inv_eapd)
772 enable = !enable;
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100773 snd_hda_codec_update_cache(codec, pin, 0,
774 AC_VERB_SET_EAPD_BTLENABLE,
775 enable ? 0x02 : 0x00);
776}
777
Takashi Iwai3e367f12013-01-23 17:07:23 +0100778/* re-initialize the path specified by the given path index */
779static void resume_path_from_idx(struct hda_codec *codec, int path_idx)
780{
781 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
782 if (path)
783 snd_hda_activate_path(codec, path, path->active, false);
784}
785
Takashi Iwai352f7f92012-12-19 12:52:06 +0100786
787/*
788 * Helper functions for creating mixer ctl elements
789 */
790
791enum {
792 HDA_CTL_WIDGET_VOL,
793 HDA_CTL_WIDGET_MUTE,
794 HDA_CTL_BIND_MUTE,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100795};
796static const struct snd_kcontrol_new control_templates[] = {
797 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
798 HDA_CODEC_MUTE(NULL, 0, 0, 0),
799 HDA_BIND_MUTE(NULL, 0, 0, 0),
Takashi Iwai352f7f92012-12-19 12:52:06 +0100800};
801
802/* add dynamic controls from template */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100803static struct snd_kcontrol_new *
804add_control(struct hda_gen_spec *spec, int type, const char *name,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100805 int cidx, unsigned long val)
806{
807 struct snd_kcontrol_new *knew;
808
Takashi Iwai12c93df2012-12-19 14:38:33 +0100809 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100810 if (!knew)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100811 return NULL;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100812 knew->index = cidx;
813 if (get_amp_nid_(val))
814 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
815 knew->private_value = val;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100816 return knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100817}
818
819static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
820 const char *pfx, const char *dir,
821 const char *sfx, int cidx, unsigned long val)
822{
823 char name[32];
824 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100825 if (!add_control(spec, type, name, cidx, val))
826 return -ENOMEM;
827 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100828}
829
830#define add_pb_vol_ctrl(spec, type, pfx, val) \
831 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
832#define add_pb_sw_ctrl(spec, type, pfx, val) \
833 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
834#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
835 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
836#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
837 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
838
839static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
840 unsigned int chs, struct nid_path *path)
841{
842 unsigned int val;
843 if (!path)
844 return 0;
845 val = path->ctls[NID_PATH_VOL_CTL];
846 if (!val)
847 return 0;
848 val = amp_val_replace_channels(val, chs);
849 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
850}
851
852/* return the channel bits suitable for the given path->ctls[] */
853static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
854 int type)
855{
856 int chs = 1; /* mono (left only) */
857 if (path) {
858 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
859 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
860 chs = 3; /* stereo */
861 }
862 return chs;
863}
864
865static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
866 struct nid_path *path)
867{
868 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
869 return add_vol_ctl(codec, pfx, cidx, chs, path);
870}
871
872/* create a mute-switch for the given mixer widget;
873 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
874 */
875static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
876 unsigned int chs, struct nid_path *path)
877{
878 unsigned int val;
879 int type = HDA_CTL_WIDGET_MUTE;
880
881 if (!path)
882 return 0;
883 val = path->ctls[NID_PATH_MUTE_CTL];
884 if (!val)
885 return 0;
886 val = amp_val_replace_channels(val, chs);
887 if (get_amp_direction_(val) == HDA_INPUT) {
888 hda_nid_t nid = get_amp_nid_(val);
889 int nums = snd_hda_get_num_conns(codec, nid);
890 if (nums > 1) {
891 type = HDA_CTL_BIND_MUTE;
892 val |= nums << 19;
893 }
894 }
895 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
896}
897
898static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
899 int cidx, struct nid_path *path)
900{
901 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
902 return add_sw_ctl(codec, pfx, cidx, chs, path);
903}
904
Takashi Iwai247d85e2013-01-17 16:18:11 +0100905/* any ctl assigned to the path with the given index? */
906static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
907{
908 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
909 return path && path->ctls[ctl_type];
910}
911
Takashi Iwai352f7f92012-12-19 12:52:06 +0100912static const char * const channel_name[4] = {
913 "Front", "Surround", "CLFE", "Side"
914};
915
916/* give some appropriate ctl name prefix for the given line out channel */
Takashi Iwai247d85e2013-01-17 16:18:11 +0100917static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
918 int *index, int ctl_type)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100919{
Takashi Iwai247d85e2013-01-17 16:18:11 +0100920 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100921 struct auto_pin_cfg *cfg = &spec->autocfg;
922
923 *index = 0;
924 if (cfg->line_outs == 1 && !spec->multi_ios &&
Takashi Iwai247d85e2013-01-17 16:18:11 +0100925 !cfg->hp_outs && !cfg->speaker_outs)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100926 return spec->vmaster_mute.hook ? "PCM" : "Master";
927
928 /* if there is really a single DAC used in the whole output paths,
929 * use it master (or "PCM" if a vmaster hook is present)
930 */
931 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
932 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
933 return spec->vmaster_mute.hook ? "PCM" : "Master";
934
Takashi Iwai247d85e2013-01-17 16:18:11 +0100935 /* multi-io channels */
936 if (ch >= cfg->line_outs)
937 return channel_name[ch];
938
Takashi Iwai352f7f92012-12-19 12:52:06 +0100939 switch (cfg->line_out_type) {
940 case AUTO_PIN_SPEAKER_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +0100941 /* if the primary channel vol/mute is shared with HP volume,
942 * don't name it as Speaker
943 */
944 if (!ch && cfg->hp_outs &&
945 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
946 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100947 if (cfg->line_outs == 1)
948 return "Speaker";
949 if (cfg->line_outs == 2)
950 return ch ? "Bass Speaker" : "Speaker";
951 break;
952 case AUTO_PIN_HP_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +0100953 /* if the primary channel vol/mute is shared with spk volume,
954 * don't name it as Headphone
955 */
956 if (!ch && cfg->speaker_outs &&
957 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
958 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100959 /* for multi-io case, only the primary out */
960 if (ch && spec->multi_ios)
961 break;
962 *index = ch;
963 return "Headphone";
Takashi Iwai352f7f92012-12-19 12:52:06 +0100964 }
Takashi Iwai247d85e2013-01-17 16:18:11 +0100965
966 /* for a single channel output, we don't have to name the channel */
967 if (cfg->line_outs == 1 && !spec->multi_ios)
968 return "PCM";
969
Takashi Iwai352f7f92012-12-19 12:52:06 +0100970 if (ch >= ARRAY_SIZE(channel_name)) {
971 snd_BUG();
972 return "PCM";
973 }
974
975 return channel_name[ch];
976}
977
978/*
979 * Parse output paths
980 */
981
982/* badness definition */
983enum {
984 /* No primary DAC is found for the main output */
985 BAD_NO_PRIMARY_DAC = 0x10000,
986 /* No DAC is found for the extra output */
987 BAD_NO_DAC = 0x4000,
988 /* No possible multi-ios */
989 BAD_MULTI_IO = 0x103,
990 /* No individual DAC for extra output */
991 BAD_NO_EXTRA_DAC = 0x102,
992 /* No individual DAC for extra surrounds */
993 BAD_NO_EXTRA_SURR_DAC = 0x101,
994 /* Primary DAC shared with main surrounds */
995 BAD_SHARED_SURROUND = 0x100,
996 /* Primary DAC shared with main CLFE */
997 BAD_SHARED_CLFE = 0x10,
998 /* Primary DAC shared with extra surrounds */
999 BAD_SHARED_EXTRA_SURROUND = 0x10,
1000 /* Volume widget is shared */
1001 BAD_SHARED_VOL = 0x10,
1002};
1003
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001004/* look for widgets in the given path which are appropriate for
Takashi Iwai352f7f92012-12-19 12:52:06 +01001005 * volume and mute controls, and assign the values to ctls[].
1006 *
1007 * When no appropriate widget is found in the path, the badness value
1008 * is incremented depending on the situation. The function returns the
1009 * total badness for both volume and mute controls.
1010 */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001011static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001012{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001013 hda_nid_t nid;
1014 unsigned int val;
1015 int badness = 0;
1016
1017 if (!path)
1018 return BAD_SHARED_VOL * 2;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001019
1020 if (path->ctls[NID_PATH_VOL_CTL] ||
1021 path->ctls[NID_PATH_MUTE_CTL])
1022 return 0; /* already evaluated */
1023
Takashi Iwai352f7f92012-12-19 12:52:06 +01001024 nid = look_for_out_vol_nid(codec, path);
1025 if (nid) {
1026 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1027 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1028 badness += BAD_SHARED_VOL;
1029 else
1030 path->ctls[NID_PATH_VOL_CTL] = val;
1031 } else
1032 badness += BAD_SHARED_VOL;
1033 nid = look_for_out_mute_nid(codec, path);
1034 if (nid) {
1035 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1036 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1037 nid_has_mute(codec, nid, HDA_OUTPUT))
1038 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1039 else
1040 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1041 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1042 badness += BAD_SHARED_VOL;
1043 else
1044 path->ctls[NID_PATH_MUTE_CTL] = val;
1045 } else
1046 badness += BAD_SHARED_VOL;
1047 return badness;
1048}
1049
1050struct badness_table {
1051 int no_primary_dac; /* no primary DAC */
1052 int no_dac; /* no secondary DACs */
1053 int shared_primary; /* primary DAC is shared with main output */
1054 int shared_surr; /* secondary DAC shared with main or primary */
1055 int shared_clfe; /* third DAC shared with main or primary */
1056 int shared_surr_main; /* secondary DAC sahred with main/DAC0 */
1057};
1058
1059static struct badness_table main_out_badness = {
1060 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1061 .no_dac = BAD_NO_DAC,
1062 .shared_primary = BAD_NO_PRIMARY_DAC,
1063 .shared_surr = BAD_SHARED_SURROUND,
1064 .shared_clfe = BAD_SHARED_CLFE,
1065 .shared_surr_main = BAD_SHARED_SURROUND,
1066};
1067
1068static struct badness_table extra_out_badness = {
1069 .no_primary_dac = BAD_NO_DAC,
1070 .no_dac = BAD_NO_DAC,
1071 .shared_primary = BAD_NO_EXTRA_DAC,
1072 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1073 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1074 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1075};
1076
Takashi Iwai7385df62013-01-07 09:50:52 +01001077/* get the DAC of the primary output corresponding to the given array index */
1078static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1079{
1080 struct hda_gen_spec *spec = codec->spec;
1081 struct auto_pin_cfg *cfg = &spec->autocfg;
1082
1083 if (cfg->line_outs > idx)
1084 return spec->private_dac_nids[idx];
1085 idx -= cfg->line_outs;
1086 if (spec->multi_ios > idx)
1087 return spec->multi_io[idx].dac;
1088 return 0;
1089}
1090
1091/* return the DAC if it's reachable, otherwise zero */
1092static inline hda_nid_t try_dac(struct hda_codec *codec,
1093 hda_nid_t dac, hda_nid_t pin)
1094{
1095 return is_reachable_path(codec, dac, pin) ? dac : 0;
1096}
1097
Takashi Iwai352f7f92012-12-19 12:52:06 +01001098/* try to assign DACs to pins and return the resultant badness */
1099static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1100 const hda_nid_t *pins, hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001101 int *path_idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001102 const struct badness_table *bad)
1103{
1104 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001105 int i, j;
1106 int badness = 0;
1107 hda_nid_t dac;
1108
1109 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 return 0;
1111
Takashi Iwai352f7f92012-12-19 12:52:06 +01001112 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001113 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001114 hda_nid_t pin = pins[i];
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001115
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001116 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1117 if (path) {
1118 badness += assign_out_path_ctls(codec, path);
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001119 continue;
1120 }
1121
1122 dacs[i] = look_for_dac(codec, pin, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001123 if (!dacs[i] && !i) {
Takashi Iwai980428c2013-01-09 09:28:20 +01001124 /* try to steal the DAC of surrounds for the front */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001125 for (j = 1; j < num_outs; j++) {
1126 if (is_reachable_path(codec, dacs[j], pin)) {
1127 dacs[0] = dacs[j];
1128 dacs[j] = 0;
Takashi Iwai980428c2013-01-09 09:28:20 +01001129 invalidate_nid_path(codec, path_idx[j]);
Takashi Iwai196c17662013-01-04 15:01:40 +01001130 path_idx[j] = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001131 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 }
1133 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001134 }
1135 dac = dacs[i];
1136 if (!dac) {
Takashi Iwai7385df62013-01-07 09:50:52 +01001137 if (num_outs > 2)
1138 dac = try_dac(codec, get_primary_out(codec, i), pin);
1139 if (!dac)
1140 dac = try_dac(codec, dacs[0], pin);
1141 if (!dac)
1142 dac = try_dac(codec, get_primary_out(codec, i), pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001143 if (dac) {
1144 if (!i)
1145 badness += bad->shared_primary;
1146 else if (i == 1)
1147 badness += bad->shared_surr;
1148 else
1149 badness += bad->shared_clfe;
1150 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1151 dac = spec->private_dac_nids[0];
1152 badness += bad->shared_surr_main;
1153 } else if (!i)
1154 badness += bad->no_primary_dac;
1155 else
1156 badness += bad->no_dac;
1157 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001158 if (!dac)
1159 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001160 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001161 if (!path && !i && spec->mixer_nid) {
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001162 /* try with aamix */
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001163 path = snd_hda_add_new_path(codec, dac, pin, 0);
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001164 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001165 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001166 dac = dacs[i] = 0;
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001167 badness += bad->no_dac;
1168 } else {
Takashi Iwaia7694092013-01-21 10:43:18 +01001169 /* print_nid_path("output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001170 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001171 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001172 badness += assign_out_path_ctls(codec, path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001173 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001174 }
1175
1176 return badness;
1177}
1178
1179/* return NID if the given pin has only a single connection to a certain DAC */
1180static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1181{
1182 struct hda_gen_spec *spec = codec->spec;
1183 int i;
1184 hda_nid_t nid_found = 0;
1185
1186 for (i = 0; i < spec->num_all_dacs; i++) {
1187 hda_nid_t nid = spec->all_dacs[i];
1188 if (!nid || is_dac_already_used(codec, nid))
1189 continue;
1190 if (is_reachable_path(codec, nid, pin)) {
1191 if (nid_found)
1192 return 0;
1193 nid_found = nid;
1194 }
1195 }
1196 return nid_found;
1197}
1198
1199/* check whether the given pin can be a multi-io pin */
1200static bool can_be_multiio_pin(struct hda_codec *codec,
1201 unsigned int location, hda_nid_t nid)
1202{
1203 unsigned int defcfg, caps;
1204
1205 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1206 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1207 return false;
1208 if (location && get_defcfg_location(defcfg) != location)
1209 return false;
1210 caps = snd_hda_query_pin_caps(codec, nid);
1211 if (!(caps & AC_PINCAP_OUT))
1212 return false;
1213 return true;
1214}
1215
Takashi Iwaie22aab72013-01-04 14:50:04 +01001216/* count the number of input pins that are capable to be multi-io */
1217static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1218{
1219 struct hda_gen_spec *spec = codec->spec;
1220 struct auto_pin_cfg *cfg = &spec->autocfg;
1221 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1222 unsigned int location = get_defcfg_location(defcfg);
1223 int type, i;
1224 int num_pins = 0;
1225
1226 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1227 for (i = 0; i < cfg->num_inputs; i++) {
1228 if (cfg->inputs[i].type != type)
1229 continue;
1230 if (can_be_multiio_pin(codec, location,
1231 cfg->inputs[i].pin))
1232 num_pins++;
1233 }
1234 }
1235 return num_pins;
1236}
1237
Takashi Iwai352f7f92012-12-19 12:52:06 +01001238/*
1239 * multi-io helper
1240 *
1241 * When hardwired is set, try to fill ony hardwired pins, and returns
1242 * zero if any pins are filled, non-zero if nothing found.
1243 * When hardwired is off, try to fill possible input pins, and returns
1244 * the badness value.
1245 */
1246static int fill_multi_ios(struct hda_codec *codec,
1247 hda_nid_t reference_pin,
Takashi Iwaie22aab72013-01-04 14:50:04 +01001248 bool hardwired)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001249{
1250 struct hda_gen_spec *spec = codec->spec;
1251 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie22aab72013-01-04 14:50:04 +01001252 int type, i, j, num_pins, old_pins;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001253 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1254 unsigned int location = get_defcfg_location(defcfg);
1255 int badness = 0;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001256 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001257
1258 old_pins = spec->multi_ios;
1259 if (old_pins >= 2)
1260 goto end_fill;
1261
Takashi Iwaie22aab72013-01-04 14:50:04 +01001262 num_pins = count_multiio_pins(codec, reference_pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001263 if (num_pins < 2)
1264 goto end_fill;
1265
Takashi Iwai352f7f92012-12-19 12:52:06 +01001266 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1267 for (i = 0; i < cfg->num_inputs; i++) {
1268 hda_nid_t nid = cfg->inputs[i].pin;
1269 hda_nid_t dac = 0;
1270
1271 if (cfg->inputs[i].type != type)
1272 continue;
1273 if (!can_be_multiio_pin(codec, location, nid))
1274 continue;
1275 for (j = 0; j < spec->multi_ios; j++) {
1276 if (nid == spec->multi_io[j].pin)
1277 break;
1278 }
1279 if (j < spec->multi_ios)
1280 continue;
1281
Takashi Iwai352f7f92012-12-19 12:52:06 +01001282 if (hardwired)
1283 dac = get_dac_if_single(codec, nid);
1284 else if (!dac)
1285 dac = look_for_dac(codec, nid, false);
1286 if (!dac) {
1287 badness++;
1288 continue;
1289 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001290 path = snd_hda_add_new_path(codec, dac, nid,
1291 -spec->mixer_nid);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001292 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001293 badness++;
1294 continue;
1295 }
Takashi Iwaia7694092013-01-21 10:43:18 +01001296 /* print_nid_path("multiio", path); */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001297 spec->multi_io[spec->multi_ios].pin = nid;
1298 spec->multi_io[spec->multi_ios].dac = dac;
Takashi Iwai196c17662013-01-04 15:01:40 +01001299 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1300 snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001301 spec->multi_ios++;
1302 if (spec->multi_ios >= 2)
1303 break;
1304 }
1305 }
1306 end_fill:
1307 if (badness)
1308 badness = BAD_MULTI_IO;
1309 if (old_pins == spec->multi_ios) {
1310 if (hardwired)
1311 return 1; /* nothing found */
1312 else
1313 return badness; /* no badness if nothing found */
1314 }
1315 if (!hardwired && spec->multi_ios < 2) {
1316 /* cancel newly assigned paths */
1317 spec->paths.used -= spec->multi_ios - old_pins;
1318 spec->multi_ios = old_pins;
1319 return badness;
1320 }
1321
1322 /* assign volume and mute controls */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001323 for (i = old_pins; i < spec->multi_ios; i++) {
1324 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1325 badness += assign_out_path_ctls(codec, path);
1326 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001327
1328 return badness;
1329}
1330
1331/* map DACs for all pins in the list if they are single connections */
1332static bool map_singles(struct hda_codec *codec, int outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001333 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001334{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001335 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001336 int i;
1337 bool found = false;
1338 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001339 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001340 hda_nid_t dac;
1341 if (dacs[i])
1342 continue;
1343 dac = get_dac_if_single(codec, pins[i]);
1344 if (!dac)
1345 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001346 path = snd_hda_add_new_path(codec, dac, pins[i],
1347 -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001348 if (!path && !i && spec->mixer_nid)
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001349 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001350 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001351 dacs[i] = dac;
1352 found = true;
Takashi Iwaia7694092013-01-21 10:43:18 +01001353 /* print_nid_path("output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001354 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001355 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001356 }
1357 }
1358 return found;
1359}
1360
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001361/* create a new path including aamix if available, and return its index */
1362static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1363{
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001364 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001365 struct nid_path *path;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001366 hda_nid_t dac, pin;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001367
1368 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001369 if (!path || !path->depth ||
1370 is_nid_contained(path, spec->mixer_nid))
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001371 return 0;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001372 dac = path->path[0];
1373 pin = path->path[path->depth - 1];
1374 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1375 if (!path) {
1376 if (dac != spec->multiout.dac_nids[0])
1377 dac = spec->multiout.dac_nids[0];
1378 else if (spec->multiout.hp_out_nid[0])
1379 dac = spec->multiout.hp_out_nid[0];
1380 else if (spec->multiout.extra_out_nid[0])
1381 dac = spec->multiout.extra_out_nid[0];
1382 if (dac)
1383 path = snd_hda_add_new_path(codec, dac, pin,
1384 spec->mixer_nid);
1385 }
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001386 if (!path)
1387 return 0;
Takashi Iwaia7694092013-01-21 10:43:18 +01001388 /* print_nid_path("output-aamix", path); */
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001389 path->active = false; /* unused as default */
1390 return snd_hda_get_path_idx(codec, path);
1391}
1392
Takashi Iwaia07a9492013-01-07 16:44:06 +01001393/* fill the empty entries in the dac array for speaker/hp with the
1394 * shared dac pointed by the paths
1395 */
1396static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1397 hda_nid_t *dacs, int *path_idx)
1398{
1399 struct nid_path *path;
1400 int i;
1401
1402 for (i = 0; i < num_outs; i++) {
1403 if (dacs[i])
1404 continue;
1405 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1406 if (!path)
1407 continue;
1408 dacs[i] = path->path[0];
1409 }
1410}
1411
Takashi Iwai352f7f92012-12-19 12:52:06 +01001412/* fill in the dac_nids table from the parsed pin configuration */
1413static int fill_and_eval_dacs(struct hda_codec *codec,
1414 bool fill_hardwired,
1415 bool fill_mio_first)
1416{
1417 struct hda_gen_spec *spec = codec->spec;
1418 struct auto_pin_cfg *cfg = &spec->autocfg;
1419 int i, err, badness;
1420
1421 /* set num_dacs once to full for look_for_dac() */
1422 spec->multiout.num_dacs = cfg->line_outs;
1423 spec->multiout.dac_nids = spec->private_dac_nids;
1424 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1425 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1426 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1427 spec->multi_ios = 0;
1428 snd_array_free(&spec->paths);
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001429
1430 /* clear path indices */
1431 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1432 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1433 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1434 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1435 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
Takashi Iwaic697b712013-01-07 17:09:26 +01001436 memset(spec->input_paths, 0, sizeof(spec->input_paths));
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001437 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1438 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1439
Takashi Iwai352f7f92012-12-19 12:52:06 +01001440 badness = 0;
1441
1442 /* fill hard-wired DACs first */
1443 if (fill_hardwired) {
1444 bool mapped;
1445 do {
1446 mapped = map_singles(codec, cfg->line_outs,
1447 cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001448 spec->private_dac_nids,
1449 spec->out_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001450 mapped |= map_singles(codec, cfg->hp_outs,
1451 cfg->hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001452 spec->multiout.hp_out_nid,
1453 spec->hp_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001454 mapped |= map_singles(codec, cfg->speaker_outs,
1455 cfg->speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001456 spec->multiout.extra_out_nid,
1457 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001458 if (fill_mio_first && cfg->line_outs == 1 &&
1459 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001460 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001461 if (!err)
1462 mapped = true;
1463 }
1464 } while (mapped);
1465 }
1466
1467 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001468 spec->private_dac_nids, spec->out_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001469 &main_out_badness);
1470
Takashi Iwai352f7f92012-12-19 12:52:06 +01001471 if (fill_mio_first &&
1472 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1473 /* try to fill multi-io first */
Takashi Iwaie22aab72013-01-04 14:50:04 +01001474 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001475 if (err < 0)
1476 return err;
1477 /* we don't count badness at this stage yet */
1478 }
1479
1480 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1481 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1482 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001483 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001484 &extra_out_badness);
1485 if (err < 0)
1486 return err;
1487 badness += err;
1488 }
1489 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1490 err = try_assign_dacs(codec, cfg->speaker_outs,
1491 cfg->speaker_pins,
1492 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001493 spec->speaker_paths,
1494 &extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001495 if (err < 0)
1496 return err;
1497 badness += err;
1498 }
1499 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001500 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001501 if (err < 0)
1502 return err;
1503 badness += err;
1504 }
Takashi Iwaie22aab72013-01-04 14:50:04 +01001505
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001506 if (spec->mixer_nid) {
1507 spec->aamix_out_paths[0] =
1508 check_aamix_out_path(codec, spec->out_paths[0]);
1509 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1510 spec->aamix_out_paths[1] =
1511 check_aamix_out_path(codec, spec->hp_paths[0]);
1512 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1513 spec->aamix_out_paths[2] =
1514 check_aamix_out_path(codec, spec->speaker_paths[0]);
1515 }
1516
Takashi Iwaie22aab72013-01-04 14:50:04 +01001517 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1518 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1519 spec->multi_ios = 1; /* give badness */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001520
Takashi Iwaia07a9492013-01-07 16:44:06 +01001521 /* re-count num_dacs and squash invalid entries */
1522 spec->multiout.num_dacs = 0;
1523 for (i = 0; i < cfg->line_outs; i++) {
1524 if (spec->private_dac_nids[i])
1525 spec->multiout.num_dacs++;
1526 else {
1527 memmove(spec->private_dac_nids + i,
1528 spec->private_dac_nids + i + 1,
1529 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1530 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1531 }
1532 }
1533
1534 spec->ext_channel_count = spec->min_channel_count =
David Henningssonc0f3b212013-01-16 11:45:37 +01001535 spec->multiout.num_dacs * 2;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001536
Takashi Iwai352f7f92012-12-19 12:52:06 +01001537 if (spec->multi_ios == 2) {
1538 for (i = 0; i < 2; i++)
1539 spec->private_dac_nids[spec->multiout.num_dacs++] =
1540 spec->multi_io[i].dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001541 } else if (spec->multi_ios) {
1542 spec->multi_ios = 0;
1543 badness += BAD_MULTI_IO;
1544 }
1545
Takashi Iwaia07a9492013-01-07 16:44:06 +01001546 /* re-fill the shared DAC for speaker / headphone */
1547 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1548 refill_shared_dacs(codec, cfg->hp_outs,
1549 spec->multiout.hp_out_nid,
1550 spec->hp_paths);
1551 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1552 refill_shared_dacs(codec, cfg->speaker_outs,
1553 spec->multiout.extra_out_nid,
1554 spec->speaker_paths);
1555
Takashi Iwai352f7f92012-12-19 12:52:06 +01001556 return badness;
1557}
1558
1559#define DEBUG_BADNESS
1560
1561#ifdef DEBUG_BADNESS
1562#define debug_badness snd_printdd
1563#else
1564#define debug_badness(...)
1565#endif
1566
Takashi Iwaia7694092013-01-21 10:43:18 +01001567#ifdef DEBUG_BADNESS
1568static inline void print_nid_path_idx(struct hda_codec *codec,
1569 const char *pfx, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001570{
Takashi Iwaia7694092013-01-21 10:43:18 +01001571 struct nid_path *path;
1572
1573 path = snd_hda_get_path_from_idx(codec, idx);
1574 if (path)
1575 print_nid_path(pfx, path);
1576}
1577
1578static void debug_show_configs(struct hda_codec *codec,
1579 struct auto_pin_cfg *cfg)
1580{
1581 struct hda_gen_spec *spec = codec->spec;
1582#ifdef CONFIG_SND_DEBUG_VERBOSE
1583 static const char * const lo_type[3] = { "LO", "SP", "HP" };
1584#endif
1585 int i;
1586
1587 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001588 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001589 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001590 spec->multiout.dac_nids[0],
1591 spec->multiout.dac_nids[1],
1592 spec->multiout.dac_nids[2],
Takashi Iwaia7694092013-01-21 10:43:18 +01001593 spec->multiout.dac_nids[3],
1594 lo_type[cfg->line_out_type]);
1595 for (i = 0; i < cfg->line_outs; i++)
1596 print_nid_path_idx(codec, " out", spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001597 if (spec->multi_ios > 0)
1598 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1599 spec->multi_ios,
1600 spec->multi_io[0].pin, spec->multi_io[1].pin,
1601 spec->multi_io[0].dac, spec->multi_io[1].dac);
Takashi Iwaia7694092013-01-21 10:43:18 +01001602 for (i = 0; i < spec->multi_ios; i++)
1603 print_nid_path_idx(codec, " mio",
1604 spec->out_paths[cfg->line_outs + i]);
1605 if (cfg->hp_outs)
1606 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001607 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001608 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001609 spec->multiout.hp_out_nid[0],
1610 spec->multiout.hp_out_nid[1],
1611 spec->multiout.hp_out_nid[2],
1612 spec->multiout.hp_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001613 for (i = 0; i < cfg->hp_outs; i++)
1614 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1615 if (cfg->speaker_outs)
1616 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001617 cfg->speaker_pins[0], cfg->speaker_pins[1],
1618 cfg->speaker_pins[2], cfg->speaker_pins[3],
1619 spec->multiout.extra_out_nid[0],
1620 spec->multiout.extra_out_nid[1],
1621 spec->multiout.extra_out_nid[2],
1622 spec->multiout.extra_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001623 for (i = 0; i < cfg->speaker_outs; i++)
1624 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1625 for (i = 0; i < 3; i++)
1626 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001627}
Takashi Iwaia7694092013-01-21 10:43:18 +01001628#else
1629#define debug_show_configs(codec, cfg) /* NOP */
1630#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01001631
1632/* find all available DACs of the codec */
1633static void fill_all_dac_nids(struct hda_codec *codec)
1634{
1635 struct hda_gen_spec *spec = codec->spec;
1636 int i;
1637 hda_nid_t nid = codec->start_nid;
1638
1639 spec->num_all_dacs = 0;
1640 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1641 for (i = 0; i < codec->num_nodes; i++, nid++) {
1642 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1643 continue;
1644 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1645 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1646 break;
1647 }
1648 spec->all_dacs[spec->num_all_dacs++] = nid;
1649 }
1650}
1651
1652static int parse_output_paths(struct hda_codec *codec)
1653{
1654 struct hda_gen_spec *spec = codec->spec;
1655 struct auto_pin_cfg *cfg = &spec->autocfg;
1656 struct auto_pin_cfg *best_cfg;
Takashi Iwai9314a582013-01-21 10:49:05 +01001657 unsigned int val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001658 int best_badness = INT_MAX;
1659 int badness;
1660 bool fill_hardwired = true, fill_mio_first = true;
1661 bool best_wired = true, best_mio = true;
1662 bool hp_spk_swapped = false;
1663
Takashi Iwai352f7f92012-12-19 12:52:06 +01001664 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1665 if (!best_cfg)
1666 return -ENOMEM;
1667 *best_cfg = *cfg;
1668
1669 for (;;) {
1670 badness = fill_and_eval_dacs(codec, fill_hardwired,
1671 fill_mio_first);
1672 if (badness < 0) {
1673 kfree(best_cfg);
1674 return badness;
1675 }
1676 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1677 cfg->line_out_type, fill_hardwired, fill_mio_first,
1678 badness);
Takashi Iwaia7694092013-01-21 10:43:18 +01001679 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001680 if (badness < best_badness) {
1681 best_badness = badness;
1682 *best_cfg = *cfg;
1683 best_wired = fill_hardwired;
1684 best_mio = fill_mio_first;
1685 }
1686 if (!badness)
1687 break;
1688 fill_mio_first = !fill_mio_first;
1689 if (!fill_mio_first)
1690 continue;
1691 fill_hardwired = !fill_hardwired;
1692 if (!fill_hardwired)
1693 continue;
1694 if (hp_spk_swapped)
1695 break;
1696 hp_spk_swapped = true;
1697 if (cfg->speaker_outs > 0 &&
1698 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1699 cfg->hp_outs = cfg->line_outs;
1700 memcpy(cfg->hp_pins, cfg->line_out_pins,
1701 sizeof(cfg->hp_pins));
1702 cfg->line_outs = cfg->speaker_outs;
1703 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1704 sizeof(cfg->speaker_pins));
1705 cfg->speaker_outs = 0;
1706 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1707 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1708 fill_hardwired = true;
1709 continue;
1710 }
1711 if (cfg->hp_outs > 0 &&
1712 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1713 cfg->speaker_outs = cfg->line_outs;
1714 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1715 sizeof(cfg->speaker_pins));
1716 cfg->line_outs = cfg->hp_outs;
1717 memcpy(cfg->line_out_pins, cfg->hp_pins,
1718 sizeof(cfg->hp_pins));
1719 cfg->hp_outs = 0;
1720 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1721 cfg->line_out_type = AUTO_PIN_HP_OUT;
1722 fill_hardwired = true;
1723 continue;
1724 }
1725 break;
1726 }
1727
1728 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001729 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001730 *cfg = *best_cfg;
1731 fill_and_eval_dacs(codec, best_wired, best_mio);
1732 }
1733 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1734 cfg->line_out_type, best_wired, best_mio);
Takashi Iwaia7694092013-01-21 10:43:18 +01001735 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001736
1737 if (cfg->line_out_pins[0]) {
1738 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01001739 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001740 if (path)
1741 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01001742 if (spec->vmaster_nid)
1743 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
1744 HDA_OUTPUT, spec->vmaster_tlv);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001745 }
1746
Takashi Iwai9314a582013-01-21 10:49:05 +01001747 /* set initial pinctl targets */
1748 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
1749 val = PIN_HP;
1750 else
1751 val = PIN_OUT;
1752 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
1753 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1754 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
1755 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1756 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
1757 set_pin_targets(codec, cfg->speaker_outs,
1758 cfg->speaker_pins, val);
1759 }
1760
Takashi Iwai352f7f92012-12-19 12:52:06 +01001761 kfree(best_cfg);
1762 return 0;
1763}
1764
1765/* add playback controls from the parsed DAC table */
1766static int create_multi_out_ctls(struct hda_codec *codec,
1767 const struct auto_pin_cfg *cfg)
1768{
1769 struct hda_gen_spec *spec = codec->spec;
1770 int i, err, noutputs;
1771
1772 noutputs = cfg->line_outs;
1773 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1774 noutputs += spec->multi_ios;
1775
1776 for (i = 0; i < noutputs; i++) {
1777 const char *name;
1778 int index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001779 struct nid_path *path;
1780
Takashi Iwai196c17662013-01-04 15:01:40 +01001781 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001782 if (!path)
1783 continue;
Takashi Iwai247d85e2013-01-17 16:18:11 +01001784
1785 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001786 if (!name || !strcmp(name, "CLFE")) {
1787 /* Center/LFE */
1788 err = add_vol_ctl(codec, "Center", 0, 1, path);
1789 if (err < 0)
1790 return err;
1791 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1792 if (err < 0)
1793 return err;
Takashi Iwai247d85e2013-01-17 16:18:11 +01001794 } else {
1795 err = add_stereo_vol(codec, name, index, path);
1796 if (err < 0)
1797 return err;
1798 }
1799
1800 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
1801 if (!name || !strcmp(name, "CLFE")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001802 err = add_sw_ctl(codec, "Center", 0, 1, path);
1803 if (err < 0)
1804 return err;
1805 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1806 if (err < 0)
1807 return err;
1808 } else {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001809 err = add_stereo_sw(codec, name, index, path);
1810 if (err < 0)
1811 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 }
1813 }
1814 return 0;
1815}
1816
Takashi Iwaic2c80382013-01-07 10:33:57 +01001817static int create_extra_out(struct hda_codec *codec, int path_idx,
Takashi Iwai196c17662013-01-04 15:01:40 +01001818 const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001820 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 int err;
1822
Takashi Iwai196c17662013-01-04 15:01:40 +01001823 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001824 if (!path)
1825 return 0;
Takashi Iwaic2c80382013-01-07 10:33:57 +01001826 err = add_stereo_vol(codec, pfx, cidx, path);
1827 if (err < 0)
1828 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001829 err = add_stereo_sw(codec, pfx, cidx, path);
1830 if (err < 0)
1831 return err;
1832 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833}
1834
Takashi Iwai352f7f92012-12-19 12:52:06 +01001835/* add playback controls for speaker and HP outputs */
1836static int create_extra_outs(struct hda_codec *codec, int num_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001837 const int *paths, const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838{
Takashi Iwaic2c80382013-01-07 10:33:57 +01001839 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001840
1841 for (i = 0; i < num_pins; i++) {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001842 const char *name;
1843 char tmp[44];
1844 int err, idx = 0;
1845
1846 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
1847 name = "Bass Speaker";
1848 else if (num_pins >= 3) {
1849 snprintf(tmp, sizeof(tmp), "%s %s",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001850 pfx, channel_name[i]);
Takashi Iwaic2c80382013-01-07 10:33:57 +01001851 name = tmp;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001852 } else {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001853 name = pfx;
1854 idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 }
Takashi Iwaic2c80382013-01-07 10:33:57 +01001856 err = create_extra_out(codec, paths[i], name, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001857 if (err < 0)
1858 return err;
1859 }
1860 return 0;
1861}
Takashi Iwai97ec5582006-03-21 11:29:07 +01001862
Takashi Iwai352f7f92012-12-19 12:52:06 +01001863static int create_hp_out_ctls(struct hda_codec *codec)
1864{
1865 struct hda_gen_spec *spec = codec->spec;
1866 return create_extra_outs(codec, spec->autocfg.hp_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001867 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001868 "Headphone");
1869}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
Takashi Iwai352f7f92012-12-19 12:52:06 +01001871static int create_speaker_out_ctls(struct hda_codec *codec)
1872{
1873 struct hda_gen_spec *spec = codec->spec;
1874 return create_extra_outs(codec, spec->autocfg.speaker_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001875 spec->speaker_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001876 "Speaker");
1877}
1878
1879/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001880 * independent HP controls
1881 */
1882
1883static int indep_hp_info(struct snd_kcontrol *kcontrol,
1884 struct snd_ctl_elem_info *uinfo)
1885{
1886 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
1887}
1888
1889static int indep_hp_get(struct snd_kcontrol *kcontrol,
1890 struct snd_ctl_elem_value *ucontrol)
1891{
1892 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1893 struct hda_gen_spec *spec = codec->spec;
1894 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
1895 return 0;
1896}
1897
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001898static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
1899 int nomix_path_idx, int mix_path_idx,
1900 int out_type);
1901
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001902static int indep_hp_put(struct snd_kcontrol *kcontrol,
1903 struct snd_ctl_elem_value *ucontrol)
1904{
1905 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1906 struct hda_gen_spec *spec = codec->spec;
1907 unsigned int select = ucontrol->value.enumerated.item[0];
1908 int ret = 0;
1909
1910 mutex_lock(&spec->pcm_mutex);
1911 if (spec->active_streams) {
1912 ret = -EBUSY;
1913 goto unlock;
1914 }
1915
1916 if (spec->indep_hp_enabled != select) {
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001917 hda_nid_t *dacp;
1918 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
1919 dacp = &spec->private_dac_nids[0];
1920 else
1921 dacp = &spec->multiout.hp_out_nid[0];
1922
1923 /* update HP aamix paths in case it conflicts with indep HP */
1924 if (spec->have_aamix_ctl) {
1925 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
1926 update_aamix_paths(codec, spec->aamix_mode,
1927 spec->out_paths[0],
1928 spec->aamix_out_paths[0],
1929 spec->autocfg.line_out_type);
1930 else
1931 update_aamix_paths(codec, spec->aamix_mode,
1932 spec->hp_paths[0],
1933 spec->aamix_out_paths[1],
1934 AUTO_PIN_HP_OUT);
1935 }
1936
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001937 spec->indep_hp_enabled = select;
1938 if (spec->indep_hp_enabled)
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001939 *dacp = 0;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001940 else
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001941 *dacp = spec->alt_dac_nid;
Takashi Iwai92603c52013-01-22 07:46:31 +01001942
1943 /* update HP auto-mute state too */
1944 if (spec->hp_automute_hook)
1945 spec->hp_automute_hook(codec, NULL);
1946 else
1947 snd_hda_gen_hp_automute(codec, NULL);
1948
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001949 ret = 1;
1950 }
1951 unlock:
1952 mutex_unlock(&spec->pcm_mutex);
1953 return ret;
1954}
1955
1956static const struct snd_kcontrol_new indep_hp_ctl = {
1957 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1958 .name = "Independent HP",
1959 .info = indep_hp_info,
1960 .get = indep_hp_get,
1961 .put = indep_hp_put,
1962};
1963
1964
1965static int create_indep_hp_ctls(struct hda_codec *codec)
1966{
1967 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001968 hda_nid_t dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001969
1970 if (!spec->indep_hp)
1971 return 0;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001972 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
1973 dac = spec->multiout.dac_nids[0];
1974 else
1975 dac = spec->multiout.hp_out_nid[0];
1976 if (!dac) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001977 spec->indep_hp = 0;
1978 return 0;
1979 }
1980
1981 spec->indep_hp_enabled = false;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001982 spec->alt_dac_nid = dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001983 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
1984 return -ENOMEM;
1985 return 0;
1986}
1987
1988/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01001989 * channel mode enum control
1990 */
1991
1992static int ch_mode_info(struct snd_kcontrol *kcontrol,
1993 struct snd_ctl_elem_info *uinfo)
1994{
1995 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1996 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001997 int chs;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001998
1999 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2000 uinfo->count = 1;
2001 uinfo->value.enumerated.items = spec->multi_ios + 1;
2002 if (uinfo->value.enumerated.item > spec->multi_ios)
2003 uinfo->value.enumerated.item = spec->multi_ios;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002004 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2005 sprintf(uinfo->value.enumerated.name, "%dch", chs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002006 return 0;
2007}
2008
2009static int ch_mode_get(struct snd_kcontrol *kcontrol,
2010 struct snd_ctl_elem_value *ucontrol)
2011{
2012 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2013 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002014 ucontrol->value.enumerated.item[0] =
2015 (spec->ext_channel_count - spec->min_channel_count) / 2;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002016 return 0;
2017}
2018
Takashi Iwai196c17662013-01-04 15:01:40 +01002019static inline struct nid_path *
2020get_multiio_path(struct hda_codec *codec, int idx)
2021{
2022 struct hda_gen_spec *spec = codec->spec;
2023 return snd_hda_get_path_from_idx(codec,
2024 spec->out_paths[spec->autocfg.line_outs + idx]);
2025}
2026
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002027static void update_automute_all(struct hda_codec *codec);
2028
Takashi Iwai352f7f92012-12-19 12:52:06 +01002029static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2030{
2031 struct hda_gen_spec *spec = codec->spec;
2032 hda_nid_t nid = spec->multi_io[idx].pin;
2033 struct nid_path *path;
2034
Takashi Iwai196c17662013-01-04 15:01:40 +01002035 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002036 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002038
2039 if (path->active == output)
2040 return 0;
2041
2042 if (output) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01002043 set_pin_target(codec, nid, PIN_OUT, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002044 snd_hda_activate_path(codec, path, true, true);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002045 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002046 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002047 set_pin_eapd(codec, nid, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002048 snd_hda_activate_path(codec, path, false, true);
Takashi Iwai2c12c302013-01-10 09:33:29 +01002049 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002050 path_power_down_sync(codec, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 }
Takashi Iwaia365fed2013-01-10 16:10:06 +01002052
2053 /* update jack retasking in case it modifies any of them */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002054 update_automute_all(codec);
Takashi Iwaia365fed2013-01-10 16:10:06 +01002055
Takashi Iwai352f7f92012-12-19 12:52:06 +01002056 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057}
2058
Takashi Iwai352f7f92012-12-19 12:52:06 +01002059static int ch_mode_put(struct snd_kcontrol *kcontrol,
2060 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002062 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2063 struct hda_gen_spec *spec = codec->spec;
2064 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
Takashi Iwai352f7f92012-12-19 12:52:06 +01002066 ch = ucontrol->value.enumerated.item[0];
2067 if (ch < 0 || ch > spec->multi_ios)
2068 return -EINVAL;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002069 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002070 return 0;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002071 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002072 for (i = 0; i < spec->multi_ios; i++)
2073 set_multi_io(codec, i, i < ch);
2074 spec->multiout.max_channels = max(spec->ext_channel_count,
2075 spec->const_channel_count);
2076 if (spec->need_dac_fix)
2077 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 return 1;
2079}
2080
Takashi Iwai352f7f92012-12-19 12:52:06 +01002081static const struct snd_kcontrol_new channel_mode_enum = {
2082 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2083 .name = "Channel Mode",
2084 .info = ch_mode_info,
2085 .get = ch_mode_get,
2086 .put = ch_mode_put,
2087};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
Takashi Iwai352f7f92012-12-19 12:52:06 +01002089static int create_multi_channel_mode(struct hda_codec *codec)
2090{
2091 struct hda_gen_spec *spec = codec->spec;
2092
2093 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002094 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002095 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 return 0;
2098}
2099
Takashi Iwai352f7f92012-12-19 12:52:06 +01002100/*
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002101 * aamix loopback enable/disable switch
2102 */
2103
2104#define loopback_mixing_info indep_hp_info
2105
2106static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2107 struct snd_ctl_elem_value *ucontrol)
2108{
2109 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2110 struct hda_gen_spec *spec = codec->spec;
2111 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2112 return 0;
2113}
2114
2115static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002116 int nomix_path_idx, int mix_path_idx,
2117 int out_type)
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002118{
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002119 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002120 struct nid_path *nomix_path, *mix_path;
2121
2122 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2123 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2124 if (!nomix_path || !mix_path)
2125 return;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002126
2127 /* if HP aamix path is driven from a different DAC and the
2128 * independent HP mode is ON, can't turn on aamix path
2129 */
2130 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2131 mix_path->path[0] != spec->alt_dac_nid)
2132 do_mix = false;
2133
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002134 if (do_mix) {
2135 snd_hda_activate_path(codec, nomix_path, false, true);
2136 snd_hda_activate_path(codec, mix_path, true, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002137 path_power_down_sync(codec, nomix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002138 } else {
2139 snd_hda_activate_path(codec, mix_path, false, true);
2140 snd_hda_activate_path(codec, nomix_path, true, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002141 path_power_down_sync(codec, mix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002142 }
2143}
2144
2145static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2146 struct snd_ctl_elem_value *ucontrol)
2147{
2148 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2149 struct hda_gen_spec *spec = codec->spec;
2150 unsigned int val = ucontrol->value.enumerated.item[0];
2151
2152 if (val == spec->aamix_mode)
2153 return 0;
2154 spec->aamix_mode = val;
2155 update_aamix_paths(codec, val, spec->out_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002156 spec->aamix_out_paths[0],
2157 spec->autocfg.line_out_type);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002158 update_aamix_paths(codec, val, spec->hp_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002159 spec->aamix_out_paths[1],
2160 AUTO_PIN_HP_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002161 update_aamix_paths(codec, val, spec->speaker_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002162 spec->aamix_out_paths[2],
2163 AUTO_PIN_SPEAKER_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002164 return 1;
2165}
2166
2167static const struct snd_kcontrol_new loopback_mixing_enum = {
2168 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2169 .name = "Loopback Mixing",
2170 .info = loopback_mixing_info,
2171 .get = loopback_mixing_get,
2172 .put = loopback_mixing_put,
2173};
2174
2175static int create_loopback_mixing_ctl(struct hda_codec *codec)
2176{
2177 struct hda_gen_spec *spec = codec->spec;
2178
2179 if (!spec->mixer_nid)
2180 return 0;
2181 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
2182 spec->aamix_out_paths[2]))
2183 return 0;
2184 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2185 return -ENOMEM;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002186 spec->have_aamix_ctl = 1;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002187 return 0;
2188}
2189
2190/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002191 * shared headphone/mic handling
2192 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002193
Takashi Iwai352f7f92012-12-19 12:52:06 +01002194static void call_update_outputs(struct hda_codec *codec);
2195
2196/* for shared I/O, change the pin-control accordingly */
2197static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic)
2198{
2199 struct hda_gen_spec *spec = codec->spec;
2200 unsigned int val;
2201 hda_nid_t pin = spec->autocfg.inputs[1].pin;
2202 /* NOTE: this assumes that there are only two inputs, the
2203 * first is the real internal mic and the second is HP/mic jack.
2204 */
2205
2206 val = snd_hda_get_default_vref(codec, pin);
2207
2208 /* This pin does not have vref caps - let's enable vref on pin 0x18
2209 instead, as suggested by Realtek */
2210 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2211 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2212 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2213 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01002214 snd_hda_set_pin_ctl_cache(codec, vref_pin,
2215 PIN_IN | (set_as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02002216 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002217
2218 val = set_as_mic ? val | PIN_IN : PIN_HP;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002219 set_pin_target(codec, pin, val, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002220
2221 spec->automute_speaker = !set_as_mic;
2222 call_update_outputs(codec);
2223}
2224
2225/* create a shared input with the headphone out */
2226static int create_shared_input(struct hda_codec *codec)
2227{
2228 struct hda_gen_spec *spec = codec->spec;
2229 struct auto_pin_cfg *cfg = &spec->autocfg;
2230 unsigned int defcfg;
2231 hda_nid_t nid;
2232
2233 /* only one internal input pin? */
2234 if (cfg->num_inputs != 1)
2235 return 0;
2236 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2237 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2238 return 0;
2239
2240 if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
2241 nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
2242 else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
2243 nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
2244 else
2245 return 0; /* both not available */
2246
2247 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2248 return 0; /* no input */
2249
2250 cfg->inputs[1].pin = nid;
2251 cfg->inputs[1].type = AUTO_PIN_MIC;
2252 cfg->num_inputs = 2;
2253 spec->shared_mic_hp = 1;
2254 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
2255 return 0;
2256}
2257
Takashi Iwai978e77e2013-01-10 16:57:58 +01002258/*
2259 * output jack mode
2260 */
2261static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2262 struct snd_ctl_elem_info *uinfo)
2263{
2264 static const char * const texts[] = {
2265 "Line Out", "Headphone Out",
2266 };
2267 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, texts);
2268}
2269
2270static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2271 struct snd_ctl_elem_value *ucontrol)
2272{
2273 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2274 hda_nid_t nid = kcontrol->private_value;
2275 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2276 ucontrol->value.enumerated.item[0] = 1;
2277 else
2278 ucontrol->value.enumerated.item[0] = 0;
2279 return 0;
2280}
2281
2282static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2283 struct snd_ctl_elem_value *ucontrol)
2284{
2285 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2286 hda_nid_t nid = kcontrol->private_value;
2287 unsigned int val;
2288
2289 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2290 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2291 return 0;
2292 snd_hda_set_pin_ctl_cache(codec, nid, val);
2293 return 1;
2294}
2295
2296static const struct snd_kcontrol_new out_jack_mode_enum = {
2297 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2298 .info = out_jack_mode_info,
2299 .get = out_jack_mode_get,
2300 .put = out_jack_mode_put,
2301};
2302
2303static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2304{
2305 struct hda_gen_spec *spec = codec->spec;
2306 int i;
2307
2308 for (i = 0; i < spec->kctls.used; i++) {
2309 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2310 if (!strcmp(kctl->name, name) && kctl->index == idx)
2311 return true;
2312 }
2313 return false;
2314}
2315
2316static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2317 char *name, size_t name_len)
2318{
2319 struct hda_gen_spec *spec = codec->spec;
2320 int idx = 0;
2321
2322 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2323 strlcat(name, " Jack Mode", name_len);
2324
2325 for (; find_kctl_name(codec, name, idx); idx++)
2326 ;
2327}
2328
2329static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2330 hda_nid_t *pins)
2331{
2332 struct hda_gen_spec *spec = codec->spec;
2333 int i;
2334
2335 for (i = 0; i < num_pins; i++) {
2336 hda_nid_t pin = pins[i];
2337 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2338 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV)) {
2339 struct snd_kcontrol_new *knew;
2340 char name[44];
2341 get_jack_mode_name(codec, pin, name, sizeof(name));
2342 knew = snd_hda_gen_add_kctl(spec, name,
2343 &out_jack_mode_enum);
2344 if (!knew)
2345 return -ENOMEM;
2346 knew->private_value = pin;
2347 }
2348 }
2349
2350 return 0;
2351}
2352
Takashi Iwai294765582013-01-17 09:52:11 +01002353/*
2354 * input jack mode
2355 */
2356
2357/* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2358#define NUM_VREFS 6
2359
2360static const char * const vref_texts[NUM_VREFS] = {
2361 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2362 "", "Mic 80pc Bias", "Mic 100pc Bias"
2363};
2364
2365static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2366{
2367 unsigned int pincap;
2368
2369 pincap = snd_hda_query_pin_caps(codec, pin);
2370 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2371 /* filter out unusual vrefs */
2372 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2373 return pincap;
2374}
2375
2376/* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2377static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2378{
2379 unsigned int i, n = 0;
2380
2381 for (i = 0; i < NUM_VREFS; i++) {
2382 if (vref_caps & (1 << i)) {
2383 if (n == item_idx)
2384 return i;
2385 n++;
2386 }
2387 }
2388 return 0;
2389}
2390
2391/* convert back from the vref ctl index to the enum item index */
2392static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2393{
2394 unsigned int i, n = 0;
2395
2396 for (i = 0; i < NUM_VREFS; i++) {
2397 if (i == idx)
2398 return n;
2399 if (vref_caps & (1 << i))
2400 n++;
2401 }
2402 return 0;
2403}
2404
2405static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2406 struct snd_ctl_elem_info *uinfo)
2407{
2408 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2409 hda_nid_t nid = kcontrol->private_value;
2410 unsigned int vref_caps = get_vref_caps(codec, nid);
2411
2412 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2413 vref_texts);
2414 /* set the right text */
2415 strcpy(uinfo->value.enumerated.name,
2416 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2417 return 0;
2418}
2419
2420static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2421 struct snd_ctl_elem_value *ucontrol)
2422{
2423 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2424 hda_nid_t nid = kcontrol->private_value;
2425 unsigned int vref_caps = get_vref_caps(codec, nid);
2426 unsigned int idx;
2427
2428 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2429 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2430 return 0;
2431}
2432
2433static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2434 struct snd_ctl_elem_value *ucontrol)
2435{
2436 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2437 hda_nid_t nid = kcontrol->private_value;
2438 unsigned int vref_caps = get_vref_caps(codec, nid);
2439 unsigned int val, idx;
2440
2441 val = snd_hda_codec_get_pin_target(codec, nid);
2442 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2443 if (idx == ucontrol->value.enumerated.item[0])
2444 return 0;
2445
2446 val &= ~AC_PINCTL_VREFEN;
2447 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2448 snd_hda_set_pin_ctl_cache(codec, nid, val);
2449 return 1;
2450}
2451
2452static const struct snd_kcontrol_new in_jack_mode_enum = {
2453 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2454 .info = in_jack_mode_info,
2455 .get = in_jack_mode_get,
2456 .put = in_jack_mode_put,
2457};
2458
2459static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2460{
2461 struct hda_gen_spec *spec = codec->spec;
2462 unsigned int defcfg;
2463 struct snd_kcontrol_new *knew;
2464 char name[44];
2465
2466 /* no jack mode for fixed pins */
2467 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2468 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2469 return 0;
2470
2471 /* no multiple vref caps? */
2472 if (hweight32(get_vref_caps(codec, pin)) <= 1)
2473 return 0;
2474
2475 get_jack_mode_name(codec, pin, name, sizeof(name));
2476 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2477 if (!knew)
2478 return -ENOMEM;
2479 knew->private_value = pin;
2480 return 0;
2481}
2482
Takashi Iwai352f7f92012-12-19 12:52:06 +01002483
2484/*
2485 * Parse input paths
2486 */
2487
2488#ifdef CONFIG_PM
2489/* add the powersave loopback-list entry */
2490static void add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
2491{
2492 struct hda_amp_list *list;
2493
2494 if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
2495 return;
2496 list = spec->loopback_list + spec->num_loopbacks;
2497 list->nid = mix;
2498 list->dir = HDA_INPUT;
2499 list->idx = idx;
2500 spec->num_loopbacks++;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002501 spec->loopback.amplist = spec->loopback_list;
2502}
2503#else
Takashi Iwai352f7f92012-12-19 12:52:06 +01002504#define add_loopback_list(spec, mix, idx) /* NOP */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002505#endif
2506
Takashi Iwai352f7f92012-12-19 12:52:06 +01002507/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01002508static int new_analog_input(struct hda_codec *codec, int input_idx,
2509 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002510 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002512 struct hda_gen_spec *spec = codec->spec;
2513 struct nid_path *path;
2514 unsigned int val;
2515 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516
Takashi Iwai352f7f92012-12-19 12:52:06 +01002517 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
2518 !nid_has_mute(codec, mix_nid, HDA_INPUT))
2519 return 0; /* no need for analog loopback */
2520
Takashi Iwai3ca529d2013-01-07 17:25:08 +01002521 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002522 if (!path)
2523 return -EINVAL;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002524 print_nid_path("loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01002525 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002526
2527 idx = path->idx[path->depth - 1];
2528 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
2529 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2530 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002531 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002533 path->ctls[NID_PATH_VOL_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 }
2535
Takashi Iwai352f7f92012-12-19 12:52:06 +01002536 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
2537 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2538 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002539 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002541 path->ctls[NID_PATH_MUTE_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 }
2543
Takashi Iwai352f7f92012-12-19 12:52:06 +01002544 path->active = true;
2545 add_loopback_list(spec, mix_nid, idx);
Takashi Iwaie4a395e2013-01-23 17:00:31 +01002546
2547 if (spec->mixer_nid != spec->mixer_merge_nid &&
2548 !spec->loopback_merge_path) {
2549 path = snd_hda_add_new_path(codec, spec->mixer_nid,
2550 spec->mixer_merge_nid, 0);
2551 if (path) {
2552 print_nid_path("loopback-merge", path);
2553 path->active = true;
2554 spec->loopback_merge_path =
2555 snd_hda_get_path_idx(codec, path);
2556 }
2557 }
2558
Takashi Iwai352f7f92012-12-19 12:52:06 +01002559 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560}
2561
Takashi Iwai352f7f92012-12-19 12:52:06 +01002562static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002564 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
2565 return (pincap & AC_PINCAP_IN) != 0;
2566}
2567
2568/* Parse the codec tree and retrieve ADCs */
2569static int fill_adc_nids(struct hda_codec *codec)
2570{
2571 struct hda_gen_spec *spec = codec->spec;
2572 hda_nid_t nid;
2573 hda_nid_t *adc_nids = spec->adc_nids;
2574 int max_nums = ARRAY_SIZE(spec->adc_nids);
2575 int i, nums = 0;
2576
2577 nid = codec->start_nid;
2578 for (i = 0; i < codec->num_nodes; i++, nid++) {
2579 unsigned int caps = get_wcaps(codec, nid);
2580 int type = get_wcaps_type(caps);
2581
2582 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2583 continue;
2584 adc_nids[nums] = nid;
2585 if (++nums >= max_nums)
2586 break;
2587 }
2588 spec->num_adc_nids = nums;
Takashi Iwai0ffd5342013-01-17 15:53:29 +01002589
2590 /* copy the detected ADCs to all_adcs[] */
2591 spec->num_all_adcs = nums;
2592 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
2593
Takashi Iwai352f7f92012-12-19 12:52:06 +01002594 return nums;
2595}
2596
2597/* filter out invalid adc_nids that don't give all active input pins;
2598 * if needed, check whether dynamic ADC-switching is available
2599 */
2600static int check_dyn_adc_switch(struct hda_codec *codec)
2601{
2602 struct hda_gen_spec *spec = codec->spec;
2603 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002604 unsigned int ok_bits;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002605 int i, n, nums;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002606
2607 again:
2608 nums = 0;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002609 ok_bits = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002610 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002611 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002612 if (!spec->input_paths[i][n])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002613 break;
2614 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002615 if (i >= imux->num_items) {
2616 ok_bits |= (1 << n);
2617 nums++;
2618 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002619 }
2620
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002621 if (!ok_bits) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002622 if (spec->shared_mic_hp) {
2623 spec->shared_mic_hp = 0;
2624 imux->num_items = 1;
2625 goto again;
2626 }
2627
2628 /* check whether ADC-switch is possible */
2629 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002630 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002631 if (spec->input_paths[i][n]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002632 spec->dyn_adc_idx[i] = n;
2633 break;
2634 }
2635 }
2636 }
2637
2638 snd_printdd("hda-codec: enabling ADC switching\n");
2639 spec->dyn_adc_switch = 1;
2640 } else if (nums != spec->num_adc_nids) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002641 /* shrink the invalid adcs and input paths */
2642 nums = 0;
2643 for (n = 0; n < spec->num_adc_nids; n++) {
2644 if (!(ok_bits & (1 << n)))
2645 continue;
2646 if (n != nums) {
2647 spec->adc_nids[nums] = spec->adc_nids[n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002648 for (i = 0; i < imux->num_items; i++) {
2649 invalidate_nid_path(codec,
2650 spec->input_paths[i][nums]);
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002651 spec->input_paths[i][nums] =
2652 spec->input_paths[i][n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002653 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002654 }
2655 nums++;
2656 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002657 spec->num_adc_nids = nums;
2658 }
2659
2660 if (imux->num_items == 1 || spec->shared_mic_hp) {
2661 snd_printdd("hda-codec: reducing to a single ADC\n");
2662 spec->num_adc_nids = 1; /* reduce to a single ADC */
2663 }
2664
2665 /* single index for individual volumes ctls */
2666 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
2667 spec->num_adc_nids = 1;
2668
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 return 0;
2670}
2671
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002672/* parse capture source paths from the given pin and create imux items */
2673static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
Takashi Iwai9dba2052013-01-18 10:01:15 +01002674 int cfg_idx, int num_adcs,
2675 const char *label, int anchor)
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002676{
2677 struct hda_gen_spec *spec = codec->spec;
2678 struct hda_input_mux *imux = &spec->input_mux;
2679 int imux_idx = imux->num_items;
2680 bool imux_added = false;
2681 int c;
2682
2683 for (c = 0; c < num_adcs; c++) {
2684 struct nid_path *path;
2685 hda_nid_t adc = spec->adc_nids[c];
2686
2687 if (!is_reachable_path(codec, pin, adc))
2688 continue;
2689 path = snd_hda_add_new_path(codec, pin, adc, anchor);
2690 if (!path)
2691 continue;
2692 print_nid_path("input", path);
2693 spec->input_paths[imux_idx][c] =
2694 snd_hda_get_path_idx(codec, path);
2695
2696 if (!imux_added) {
2697 spec->imux_pins[imux->num_items] = pin;
Takashi Iwai9dba2052013-01-18 10:01:15 +01002698 snd_hda_add_imux_item(imux, label, cfg_idx, NULL);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002699 imux_added = true;
2700 }
2701 }
2702
2703 return 0;
2704}
2705
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002707 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 */
Takashi Iwai9dba2052013-01-18 10:01:15 +01002709
Takashi Iwaic9700422013-01-18 10:17:30 +01002710/* fill the label for each input at first */
2711static int fill_input_pin_labels(struct hda_codec *codec)
2712{
2713 struct hda_gen_spec *spec = codec->spec;
2714 const struct auto_pin_cfg *cfg = &spec->autocfg;
2715 int i;
2716
2717 for (i = 0; i < cfg->num_inputs; i++) {
2718 hda_nid_t pin = cfg->inputs[i].pin;
2719 const char *label;
2720 int j, idx;
2721
2722 if (!is_input_pin(codec, pin))
2723 continue;
2724
2725 label = hda_get_autocfg_input_label(codec, cfg, i);
2726 idx = 0;
David Henningsson8e8db7f2013-01-18 15:43:02 +01002727 for (j = i - 1; j >= 0; j--) {
Takashi Iwaic9700422013-01-18 10:17:30 +01002728 if (spec->input_labels[j] &&
2729 !strcmp(spec->input_labels[j], label)) {
2730 idx = spec->input_label_idxs[j] + 1;
2731 break;
2732 }
2733 }
2734
2735 spec->input_labels[i] = label;
2736 spec->input_label_idxs[i] = idx;
2737 }
2738
2739 return 0;
2740}
2741
Takashi Iwai9dba2052013-01-18 10:01:15 +01002742#define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
2743
Takashi Iwai352f7f92012-12-19 12:52:06 +01002744static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002745{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002746 struct hda_gen_spec *spec = codec->spec;
2747 const struct auto_pin_cfg *cfg = &spec->autocfg;
2748 hda_nid_t mixer = spec->mixer_nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002749 int num_adcs;
Takashi Iwaic9700422013-01-18 10:17:30 +01002750 int i, err;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002751 unsigned int val;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002752
Takashi Iwai352f7f92012-12-19 12:52:06 +01002753 num_adcs = fill_adc_nids(codec);
2754 if (num_adcs < 0)
2755 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002756
Takashi Iwaic9700422013-01-18 10:17:30 +01002757 err = fill_input_pin_labels(codec);
2758 if (err < 0)
2759 return err;
2760
Takashi Iwai352f7f92012-12-19 12:52:06 +01002761 for (i = 0; i < cfg->num_inputs; i++) {
2762 hda_nid_t pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763
Takashi Iwai352f7f92012-12-19 12:52:06 +01002764 pin = cfg->inputs[i].pin;
2765 if (!is_input_pin(codec, pin))
2766 continue;
2767
Takashi Iwai2c12c302013-01-10 09:33:29 +01002768 val = PIN_IN;
2769 if (cfg->inputs[i].type == AUTO_PIN_MIC)
2770 val |= snd_hda_get_default_vref(codec, pin);
2771 set_pin_target(codec, pin, val, false);
2772
Takashi Iwai352f7f92012-12-19 12:52:06 +01002773 if (mixer) {
2774 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01002775 err = new_analog_input(codec, i, pin,
Takashi Iwaic9700422013-01-18 10:17:30 +01002776 spec->input_labels[i],
2777 spec->input_label_idxs[i],
2778 mixer);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002779 if (err < 0)
2780 return err;
2781 }
2782 }
2783
Takashi Iwaic9700422013-01-18 10:17:30 +01002784 err = parse_capture_source(codec, pin, i, num_adcs,
2785 spec->input_labels[i], -mixer);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002786 if (err < 0)
2787 return err;
Takashi Iwai294765582013-01-17 09:52:11 +01002788
2789 if (spec->add_in_jack_modes) {
2790 err = create_in_jack_mode(codec, pin);
2791 if (err < 0)
2792 return err;
2793 }
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002794 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002795
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002796 if (mixer && spec->add_stereo_mix_input) {
Takashi Iwai9dba2052013-01-18 10:01:15 +01002797 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002798 "Stereo Mix", 0);
2799 if (err < 0)
2800 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002801 }
2802
2803 return 0;
2804}
2805
2806
2807/*
2808 * input source mux
2809 */
2810
Takashi Iwaic697b712013-01-07 17:09:26 +01002811/* get the input path specified by the given adc and imux indices */
2812static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002813{
2814 struct hda_gen_spec *spec = codec->spec;
David Henningssonb56fa1e2013-01-16 11:45:35 +01002815 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
2816 snd_BUG();
2817 return NULL;
2818 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002819 if (spec->dyn_adc_switch)
2820 adc_idx = spec->dyn_adc_idx[imux_idx];
David Henningssond3d982f2013-01-18 15:43:01 +01002821 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
David Henningssonb56fa1e2013-01-16 11:45:35 +01002822 snd_BUG();
2823 return NULL;
2824 }
Takashi Iwaic697b712013-01-07 17:09:26 +01002825 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002826}
2827
2828static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2829 unsigned int idx);
2830
2831static int mux_enum_info(struct snd_kcontrol *kcontrol,
2832 struct snd_ctl_elem_info *uinfo)
2833{
2834 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2835 struct hda_gen_spec *spec = codec->spec;
2836 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
2837}
2838
2839static int mux_enum_get(struct snd_kcontrol *kcontrol,
2840 struct snd_ctl_elem_value *ucontrol)
2841{
2842 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2843 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai2a8d5392013-01-18 16:23:25 +01002844 /* the ctls are created at once with multiple counts */
2845 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002846
2847 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
2848 return 0;
2849}
2850
2851static int mux_enum_put(struct snd_kcontrol *kcontrol,
2852 struct snd_ctl_elem_value *ucontrol)
2853{
2854 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Takashi Iwai2a8d5392013-01-18 16:23:25 +01002855 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002856 return mux_select(codec, adc_idx,
2857 ucontrol->value.enumerated.item[0]);
2858}
2859
Takashi Iwai352f7f92012-12-19 12:52:06 +01002860static const struct snd_kcontrol_new cap_src_temp = {
2861 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2862 .name = "Input Source",
2863 .info = mux_enum_info,
2864 .get = mux_enum_get,
2865 .put = mux_enum_put,
2866};
2867
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002868/*
2869 * capture volume and capture switch ctls
2870 */
2871
Takashi Iwai352f7f92012-12-19 12:52:06 +01002872typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
2873 struct snd_ctl_elem_value *ucontrol);
2874
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002875/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002876static int cap_put_caller(struct snd_kcontrol *kcontrol,
2877 struct snd_ctl_elem_value *ucontrol,
2878 put_call_t func, int type)
2879{
2880 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2881 struct hda_gen_spec *spec = codec->spec;
2882 const struct hda_input_mux *imux;
2883 struct nid_path *path;
2884 int i, adc_idx, err = 0;
2885
2886 imux = &spec->input_mux;
David Henningssona053d1e2013-01-16 11:45:36 +01002887 adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002888 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002889 /* we use the cache-only update at first since multiple input paths
2890 * may shared the same amp; by updating only caches, the redundant
2891 * writes to hardware can be reduced.
2892 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002893 codec->cached_write = 1;
2894 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01002895 path = get_input_path(codec, adc_idx, i);
2896 if (!path || !path->ctls[type])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002897 continue;
2898 kcontrol->private_value = path->ctls[type];
2899 err = func(kcontrol, ucontrol);
2900 if (err < 0)
2901 goto error;
2902 }
2903 error:
2904 codec->cached_write = 0;
2905 mutex_unlock(&codec->control_mutex);
Takashi Iwaidc870f32013-01-22 15:24:30 +01002906 snd_hda_codec_flush_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002907 if (err >= 0 && spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01002908 spec->cap_sync_hook(codec, ucontrol);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002909 return err;
2910}
2911
2912/* capture volume ctl callbacks */
2913#define cap_vol_info snd_hda_mixer_amp_volume_info
2914#define cap_vol_get snd_hda_mixer_amp_volume_get
2915#define cap_vol_tlv snd_hda_mixer_amp_tlv
2916
2917static int cap_vol_put(struct snd_kcontrol *kcontrol,
2918 struct snd_ctl_elem_value *ucontrol)
2919{
2920 return cap_put_caller(kcontrol, ucontrol,
2921 snd_hda_mixer_amp_volume_put,
2922 NID_PATH_VOL_CTL);
2923}
2924
2925static const struct snd_kcontrol_new cap_vol_temp = {
2926 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2927 .name = "Capture Volume",
2928 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
2929 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2930 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
2931 .info = cap_vol_info,
2932 .get = cap_vol_get,
2933 .put = cap_vol_put,
2934 .tlv = { .c = cap_vol_tlv },
2935};
2936
2937/* capture switch ctl callbacks */
2938#define cap_sw_info snd_ctl_boolean_stereo_info
2939#define cap_sw_get snd_hda_mixer_amp_switch_get
2940
2941static int cap_sw_put(struct snd_kcontrol *kcontrol,
2942 struct snd_ctl_elem_value *ucontrol)
2943{
Takashi Iwaia90229e2013-01-18 14:10:00 +01002944 return cap_put_caller(kcontrol, ucontrol,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002945 snd_hda_mixer_amp_switch_put,
2946 NID_PATH_MUTE_CTL);
2947}
2948
2949static const struct snd_kcontrol_new cap_sw_temp = {
2950 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2951 .name = "Capture Switch",
2952 .info = cap_sw_info,
2953 .get = cap_sw_get,
2954 .put = cap_sw_put,
2955};
2956
2957static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
2958{
2959 hda_nid_t nid;
2960 int i, depth;
2961
2962 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
2963 for (depth = 0; depth < 3; depth++) {
2964 if (depth >= path->depth)
2965 return -EINVAL;
2966 i = path->depth - depth - 1;
2967 nid = path->path[i];
2968 if (!path->ctls[NID_PATH_VOL_CTL]) {
2969 if (nid_has_volume(codec, nid, HDA_OUTPUT))
2970 path->ctls[NID_PATH_VOL_CTL] =
2971 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2972 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
2973 int idx = path->idx[i];
2974 if (!depth && codec->single_adc_amp)
2975 idx = 0;
2976 path->ctls[NID_PATH_VOL_CTL] =
2977 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2978 }
2979 }
2980 if (!path->ctls[NID_PATH_MUTE_CTL]) {
2981 if (nid_has_mute(codec, nid, HDA_OUTPUT))
2982 path->ctls[NID_PATH_MUTE_CTL] =
2983 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2984 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
2985 int idx = path->idx[i];
2986 if (!depth && codec->single_adc_amp)
2987 idx = 0;
2988 path->ctls[NID_PATH_MUTE_CTL] =
2989 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2990 }
2991 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01002992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 return 0;
2994}
2995
Takashi Iwai352f7f92012-12-19 12:52:06 +01002996static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002998 struct hda_gen_spec *spec = codec->spec;
2999 struct auto_pin_cfg *cfg = &spec->autocfg;
3000 unsigned int val;
3001 int i;
3002
3003 if (!spec->inv_dmic_split)
3004 return false;
3005 for (i = 0; i < cfg->num_inputs; i++) {
3006 if (cfg->inputs[i].pin != nid)
3007 continue;
3008 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3009 return false;
3010 val = snd_hda_codec_get_pincfg(codec, nid);
3011 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3012 }
3013 return false;
3014}
3015
Takashi Iwaia90229e2013-01-18 14:10:00 +01003016/* capture switch put callback for a single control with hook call */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003017static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3018 struct snd_ctl_elem_value *ucontrol)
3019{
3020 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3021 struct hda_gen_spec *spec = codec->spec;
3022 int ret;
3023
3024 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3025 if (ret < 0)
3026 return ret;
3027
Takashi Iwaia90229e2013-01-18 14:10:00 +01003028 if (spec->cap_sync_hook)
3029 spec->cap_sync_hook(codec, ucontrol);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003030
3031 return ret;
3032}
3033
Takashi Iwai352f7f92012-12-19 12:52:06 +01003034static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3035 int idx, bool is_switch, unsigned int ctl,
3036 bool inv_dmic)
3037{
3038 struct hda_gen_spec *spec = codec->spec;
3039 char tmpname[44];
3040 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3041 const char *sfx = is_switch ? "Switch" : "Volume";
3042 unsigned int chs = inv_dmic ? 1 : 3;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003043 struct snd_kcontrol_new *knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003044
3045 if (!ctl)
3046 return 0;
3047
3048 if (label)
3049 snprintf(tmpname, sizeof(tmpname),
3050 "%s Capture %s", label, sfx);
3051 else
3052 snprintf(tmpname, sizeof(tmpname),
3053 "Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003054 knew = add_control(spec, type, tmpname, idx,
3055 amp_val_replace_channels(ctl, chs));
3056 if (!knew)
3057 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003058 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003059 knew->put = cap_single_sw_put;
3060 if (!inv_dmic)
3061 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003062
3063 /* Make independent right kcontrol */
3064 if (label)
3065 snprintf(tmpname, sizeof(tmpname),
3066 "Inverted %s Capture %s", label, sfx);
3067 else
3068 snprintf(tmpname, sizeof(tmpname),
3069 "Inverted Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003070 knew = add_control(spec, type, tmpname, idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003071 amp_val_replace_channels(ctl, 2));
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003072 if (!knew)
3073 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003074 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003075 knew->put = cap_single_sw_put;
3076 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003077}
3078
3079/* create single (and simple) capture volume and switch controls */
3080static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3081 unsigned int vol_ctl, unsigned int sw_ctl,
3082 bool inv_dmic)
3083{
3084 int err;
3085 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3086 if (err < 0)
3087 return err;
3088 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3089 if (err < 0)
3090 return err;
3091 return 0;
3092}
3093
3094/* create bound capture volume and switch controls */
3095static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3096 unsigned int vol_ctl, unsigned int sw_ctl)
3097{
3098 struct hda_gen_spec *spec = codec->spec;
3099 struct snd_kcontrol_new *knew;
3100
3101 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003102 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003103 if (!knew)
3104 return -ENOMEM;
3105 knew->index = idx;
3106 knew->private_value = vol_ctl;
3107 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3108 }
3109 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003110 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003111 if (!knew)
3112 return -ENOMEM;
3113 knew->index = idx;
3114 knew->private_value = sw_ctl;
3115 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3116 }
3117 return 0;
3118}
3119
3120/* return the vol ctl when used first in the imux list */
3121static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3122{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003123 struct nid_path *path;
3124 unsigned int ctl;
3125 int i;
3126
Takashi Iwaic697b712013-01-07 17:09:26 +01003127 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003128 if (!path)
3129 return 0;
3130 ctl = path->ctls[type];
3131 if (!ctl)
3132 return 0;
3133 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003134 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003135 if (path && path->ctls[type] == ctl)
3136 return 0;
3137 }
3138 return ctl;
3139}
3140
3141/* create individual capture volume and switch controls per input */
3142static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3143{
3144 struct hda_gen_spec *spec = codec->spec;
3145 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaic9700422013-01-18 10:17:30 +01003146 int i, err, type;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003147
3148 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003149 bool inv_dmic;
Takashi Iwaic9700422013-01-18 10:17:30 +01003150 int idx;
Takashi Iwai9dba2052013-01-18 10:01:15 +01003151
Takashi Iwaic9700422013-01-18 10:17:30 +01003152 idx = imux->items[i].index;
3153 if (idx >= spec->autocfg.num_inputs)
Takashi Iwai9dba2052013-01-18 10:01:15 +01003154 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003155 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3156
3157 for (type = 0; type < 2; type++) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003158 err = add_single_cap_ctl(codec,
3159 spec->input_labels[idx],
3160 spec->input_label_idxs[idx],
3161 type,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003162 get_first_cap_ctl(codec, i, type),
3163 inv_dmic);
3164 if (err < 0)
3165 return err;
3166 }
3167 }
3168 return 0;
3169}
3170
3171static int create_capture_mixers(struct hda_codec *codec)
3172{
3173 struct hda_gen_spec *spec = codec->spec;
3174 struct hda_input_mux *imux = &spec->input_mux;
3175 int i, n, nums, err;
3176
3177 if (spec->dyn_adc_switch)
3178 nums = 1;
3179 else
3180 nums = spec->num_adc_nids;
3181
3182 if (!spec->auto_mic && imux->num_items > 1) {
3183 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01003184 const char *name;
3185 name = nums > 1 ? "Input Source" : "Capture Source";
3186 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003187 if (!knew)
3188 return -ENOMEM;
3189 knew->count = nums;
3190 }
3191
3192 for (n = 0; n < nums; n++) {
3193 bool multi = false;
David Henningsson99a55922013-01-16 15:58:44 +01003194 bool multi_cap_vol = spec->multi_cap_vol;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003195 bool inv_dmic = false;
3196 int vol, sw;
3197
3198 vol = sw = 0;
3199 for (i = 0; i < imux->num_items; i++) {
3200 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01003201 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003202 if (!path)
3203 continue;
3204 parse_capvol_in_path(codec, path);
3205 if (!vol)
3206 vol = path->ctls[NID_PATH_VOL_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003207 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003208 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003209 if (!same_amp_caps(codec, vol,
3210 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3211 multi_cap_vol = true;
3212 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003213 if (!sw)
3214 sw = path->ctls[NID_PATH_MUTE_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003215 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003216 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003217 if (!same_amp_caps(codec, sw,
3218 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3219 multi_cap_vol = true;
3220 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003221 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3222 inv_dmic = true;
3223 }
3224
3225 if (!multi)
3226 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3227 inv_dmic);
David Henningsson99a55922013-01-16 15:58:44 +01003228 else if (!multi_cap_vol)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003229 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3230 else
3231 err = create_multi_cap_vol_ctl(codec);
3232 if (err < 0)
3233 return err;
3234 }
3235
3236 return 0;
3237}
3238
3239/*
3240 * add mic boosts if needed
3241 */
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003242
3243/* check whether the given amp is feasible as a boost volume */
3244static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3245 int dir, int idx)
3246{
3247 unsigned int step;
3248
3249 if (!nid_has_volume(codec, nid, dir) ||
3250 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3251 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3252 return false;
3253
3254 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3255 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3256 if (step < 0x20)
3257 return false;
3258 return true;
3259}
3260
3261/* look for a boost amp in a widget close to the pin */
3262static unsigned int look_for_boost_amp(struct hda_codec *codec,
3263 struct nid_path *path)
3264{
3265 unsigned int val = 0;
3266 hda_nid_t nid;
3267 int depth;
3268
3269 for (depth = 0; depth < 3; depth++) {
3270 if (depth >= path->depth - 1)
3271 break;
3272 nid = path->path[depth];
3273 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3274 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3275 break;
3276 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3277 path->idx[depth])) {
3278 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3279 HDA_INPUT);
3280 break;
3281 }
3282 }
3283
3284 return val;
3285}
3286
Takashi Iwai352f7f92012-12-19 12:52:06 +01003287static int parse_mic_boost(struct hda_codec *codec)
3288{
3289 struct hda_gen_spec *spec = codec->spec;
3290 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003291 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003292 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003293
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003294 if (!spec->num_adc_nids)
3295 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003296
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003297 for (i = 0; i < imux->num_items; i++) {
3298 struct nid_path *path;
3299 unsigned int val;
3300 int idx;
3301 char boost_label[44];
David Henningsson02aba552013-01-16 15:58:43 +01003302
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003303 idx = imux->items[i].index;
3304 if (idx >= imux->num_items)
3305 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003306
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003307 /* check only line-in and mic pins */
Takashi Iwai1799cdd52013-01-18 14:37:16 +01003308 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003309 continue;
3310
3311 path = get_input_path(codec, 0, i);
3312 if (!path)
3313 continue;
3314
3315 val = look_for_boost_amp(codec, path);
3316 if (!val)
3317 continue;
3318
3319 /* create a boost control */
3320 snprintf(boost_label, sizeof(boost_label),
3321 "%s Boost Volume", spec->input_labels[idx]);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003322 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3323 spec->input_label_idxs[idx], val))
3324 return -ENOMEM;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003325
3326 path->ctls[NID_PATH_BOOST_CTL] = val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003327 }
3328 return 0;
3329}
3330
3331/*
3332 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3333 */
3334static void parse_digital(struct hda_codec *codec)
3335{
3336 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003337 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003338 int i, nums;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003339 hda_nid_t dig_nid, pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003340
3341 /* support multiple SPDIFs; the secondary is set up as a slave */
3342 nums = 0;
3343 for (i = 0; i < spec->autocfg.dig_outs; i++) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003344 pin = spec->autocfg.dig_out_pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003345 dig_nid = look_for_dac(codec, pin, true);
3346 if (!dig_nid)
3347 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003348 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003349 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003350 continue;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003351 print_nid_path("digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01003352 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01003353 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003354 set_pin_target(codec, pin, PIN_OUT, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003355 if (!nums) {
3356 spec->multiout.dig_out_nid = dig_nid;
3357 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3358 } else {
3359 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3360 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
3361 break;
3362 spec->slave_dig_outs[nums - 1] = dig_nid;
3363 }
3364 nums++;
3365 }
3366
3367 if (spec->autocfg.dig_in_pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003368 pin = spec->autocfg.dig_in_pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003369 dig_nid = codec->start_nid;
3370 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003371 unsigned int wcaps = get_wcaps(codec, dig_nid);
3372 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3373 continue;
3374 if (!(wcaps & AC_WCAP_DIGITAL))
3375 continue;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003376 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003377 if (path) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003378 print_nid_path("digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003379 path->active = true;
3380 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003381 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003382 set_pin_target(codec, pin, PIN_IN, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003383 break;
3384 }
3385 }
3386 }
3387}
3388
3389
3390/*
3391 * input MUX handling
3392 */
3393
3394static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3395
3396/* select the given imux item; either unmute exclusively or select the route */
3397static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3398 unsigned int idx)
3399{
3400 struct hda_gen_spec *spec = codec->spec;
3401 const struct hda_input_mux *imux;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003402 struct nid_path *old_path, *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003403
3404 imux = &spec->input_mux;
3405 if (!imux->num_items)
3406 return 0;
3407
3408 if (idx >= imux->num_items)
3409 idx = imux->num_items - 1;
3410 if (spec->cur_mux[adc_idx] == idx)
3411 return 0;
3412
Takashi Iwai55196ff2013-01-24 17:32:56 +01003413 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3414 if (!old_path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003415 return 0;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003416 if (old_path->active)
3417 snd_hda_activate_path(codec, old_path, false, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003418
3419 spec->cur_mux[adc_idx] = idx;
3420
3421 if (spec->shared_mic_hp)
3422 update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
3423
3424 if (spec->dyn_adc_switch)
3425 dyn_adc_pcm_resetup(codec, idx);
3426
Takashi Iwaic697b712013-01-07 17:09:26 +01003427 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003428 if (!path)
3429 return 0;
3430 if (path->active)
3431 return 0;
3432 snd_hda_activate_path(codec, path, true, false);
3433 if (spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01003434 spec->cap_sync_hook(codec, NULL);
Takashi Iwai55196ff2013-01-24 17:32:56 +01003435 path_power_down_sync(codec, old_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003436 return 1;
3437}
3438
3439
3440/*
3441 * Jack detections for HP auto-mute and mic-switch
3442 */
3443
3444/* check each pin in the given array; returns true if any of them is plugged */
3445static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
3446{
3447 int i, present = 0;
3448
3449 for (i = 0; i < num_pins; i++) {
3450 hda_nid_t nid = pins[i];
3451 if (!nid)
3452 break;
Takashi Iwai0b4df932013-01-10 09:45:13 +01003453 /* don't detect pins retasked as inputs */
3454 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
3455 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003456 present |= snd_hda_jack_detect(codec, nid);
3457 }
3458 return present;
3459}
3460
3461/* standard HP/line-out auto-mute helper */
3462static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
Takashi Iwai2c12c302013-01-10 09:33:29 +01003463 bool mute)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003464{
3465 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003466 int i;
3467
3468 for (i = 0; i < num_pins; i++) {
3469 hda_nid_t nid = pins[i];
3470 unsigned int val;
3471 if (!nid)
3472 break;
3473 /* don't reset VREF value in case it's controlling
3474 * the amp (see alc861_fixup_asus_amp_vref_0f())
3475 */
Takashi Iwai2c12c302013-01-10 09:33:29 +01003476 if (spec->keep_vref_in_automute)
3477 val = snd_hda_codec_get_pin_target(codec, nid) & ~PIN_HP;
3478 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01003479 val = 0;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003480 if (!mute)
3481 val |= snd_hda_codec_get_pin_target(codec, nid);
3482 /* here we call update_pin_ctl() so that the pinctl is changed
3483 * without changing the pinctl target value;
3484 * the original target value will be still referred at the
3485 * init / resume again
3486 */
3487 update_pin_ctl(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01003488 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003489 }
3490}
3491
3492/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003493void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003494{
3495 struct hda_gen_spec *spec = codec->spec;
3496 int on;
3497
3498 /* Control HP pins/amps depending on master_mute state;
3499 * in general, HP pins/amps control should be enabled in all cases,
3500 * but currently set only for master_mute, just to be safe
3501 */
3502 if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
3503 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003504 spec->autocfg.hp_pins, spec->master_mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003505
3506 if (!spec->automute_speaker)
3507 on = 0;
3508 else
3509 on = spec->hp_jack_present | spec->line_jack_present;
3510 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003511 spec->speaker_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003512 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003513 spec->autocfg.speaker_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003514
3515 /* toggle line-out mutes if needed, too */
3516 /* if LO is a copy of either HP or Speaker, don't need to handle it */
3517 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
3518 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
3519 return;
3520 if (!spec->automute_lo)
3521 on = 0;
3522 else
3523 on = spec->hp_jack_present;
3524 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003525 spec->line_out_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003526 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003527 spec->autocfg.line_out_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003528}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003529EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003530
3531static void call_update_outputs(struct hda_codec *codec)
3532{
3533 struct hda_gen_spec *spec = codec->spec;
3534 if (spec->automute_hook)
3535 spec->automute_hook(codec);
3536 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01003537 snd_hda_gen_update_outputs(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003538}
3539
3540/* standard HP-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003541void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003542{
3543 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai92603c52013-01-22 07:46:31 +01003544 hda_nid_t *pins = spec->autocfg.hp_pins;
3545 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003546
Takashi Iwai92603c52013-01-22 07:46:31 +01003547 /* No detection for the first HP jack during indep-HP mode */
3548 if (spec->indep_hp_enabled) {
3549 pins++;
3550 num_pins--;
3551 }
3552
3553 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003554 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
3555 return;
3556 call_update_outputs(codec);
3557}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003558EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003559
3560/* standard line-out-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003561void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003562{
3563 struct hda_gen_spec *spec = codec->spec;
3564
3565 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3566 return;
3567 /* check LO jack only when it's different from HP */
3568 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
3569 return;
3570
3571 spec->line_jack_present =
3572 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3573 spec->autocfg.line_out_pins);
3574 if (!spec->automute_speaker || !spec->detect_lo)
3575 return;
3576 call_update_outputs(codec);
3577}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003578EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003579
3580/* standard mic auto-switch helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003581void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003582{
3583 struct hda_gen_spec *spec = codec->spec;
3584 int i;
3585
3586 if (!spec->auto_mic)
3587 return;
3588
3589 for (i = spec->am_num_entries - 1; i > 0; i--) {
Takashi Iwai0b4df932013-01-10 09:45:13 +01003590 hda_nid_t pin = spec->am_entry[i].pin;
3591 /* don't detect pins retasked as outputs */
3592 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
3593 continue;
3594 if (snd_hda_jack_detect(codec, pin)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003595 mux_select(codec, 0, spec->am_entry[i].idx);
3596 return;
3597 }
3598 }
3599 mux_select(codec, 0, spec->am_entry[0].idx);
3600}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003601EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003602
Takashi Iwaia5cc2502013-01-16 18:08:55 +01003603/* update jack retasking */
3604static void update_automute_all(struct hda_codec *codec)
3605{
3606 struct hda_gen_spec *spec = codec->spec;
3607
3608 if (spec->hp_automute_hook)
3609 spec->hp_automute_hook(codec, NULL);
3610 else
3611 snd_hda_gen_hp_automute(codec, NULL);
3612 if (spec->line_automute_hook)
3613 spec->line_automute_hook(codec, NULL);
3614 else
3615 snd_hda_gen_line_automute(codec, NULL);
3616 if (spec->mic_autoswitch_hook)
3617 spec->mic_autoswitch_hook(codec, NULL);
3618 else
3619 snd_hda_gen_mic_autoswitch(codec, NULL);
3620}
3621
Takashi Iwai352f7f92012-12-19 12:52:06 +01003622/*
3623 * Auto-Mute mode mixer enum support
3624 */
3625static int automute_mode_info(struct snd_kcontrol *kcontrol,
3626 struct snd_ctl_elem_info *uinfo)
3627{
3628 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3629 struct hda_gen_spec *spec = codec->spec;
3630 static const char * const texts3[] = {
3631 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02003632 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633
Takashi Iwai352f7f92012-12-19 12:52:06 +01003634 if (spec->automute_speaker_possible && spec->automute_lo_possible)
3635 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
3636 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
3637}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638
Takashi Iwai352f7f92012-12-19 12:52:06 +01003639static int automute_mode_get(struct snd_kcontrol *kcontrol,
3640 struct snd_ctl_elem_value *ucontrol)
3641{
3642 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3643 struct hda_gen_spec *spec = codec->spec;
3644 unsigned int val = 0;
3645 if (spec->automute_speaker)
3646 val++;
3647 if (spec->automute_lo)
3648 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003649
Takashi Iwai352f7f92012-12-19 12:52:06 +01003650 ucontrol->value.enumerated.item[0] = val;
3651 return 0;
3652}
3653
3654static int automute_mode_put(struct snd_kcontrol *kcontrol,
3655 struct snd_ctl_elem_value *ucontrol)
3656{
3657 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3658 struct hda_gen_spec *spec = codec->spec;
3659
3660 switch (ucontrol->value.enumerated.item[0]) {
3661 case 0:
3662 if (!spec->automute_speaker && !spec->automute_lo)
3663 return 0;
3664 spec->automute_speaker = 0;
3665 spec->automute_lo = 0;
3666 break;
3667 case 1:
3668 if (spec->automute_speaker_possible) {
3669 if (!spec->automute_lo && spec->automute_speaker)
3670 return 0;
3671 spec->automute_speaker = 1;
3672 spec->automute_lo = 0;
3673 } else if (spec->automute_lo_possible) {
3674 if (spec->automute_lo)
3675 return 0;
3676 spec->automute_lo = 1;
3677 } else
3678 return -EINVAL;
3679 break;
3680 case 2:
3681 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
3682 return -EINVAL;
3683 if (spec->automute_speaker && spec->automute_lo)
3684 return 0;
3685 spec->automute_speaker = 1;
3686 spec->automute_lo = 1;
3687 break;
3688 default:
3689 return -EINVAL;
3690 }
3691 call_update_outputs(codec);
3692 return 1;
3693}
3694
3695static const struct snd_kcontrol_new automute_mode_enum = {
3696 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3697 .name = "Auto-Mute Mode",
3698 .info = automute_mode_info,
3699 .get = automute_mode_get,
3700 .put = automute_mode_put,
3701};
3702
3703static int add_automute_mode_enum(struct hda_codec *codec)
3704{
3705 struct hda_gen_spec *spec = codec->spec;
3706
Takashi Iwai12c93df2012-12-19 14:38:33 +01003707 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01003708 return -ENOMEM;
3709 return 0;
3710}
3711
3712/*
3713 * Check the availability of HP/line-out auto-mute;
3714 * Set up appropriately if really supported
3715 */
3716static int check_auto_mute_availability(struct hda_codec *codec)
3717{
3718 struct hda_gen_spec *spec = codec->spec;
3719 struct auto_pin_cfg *cfg = &spec->autocfg;
3720 int present = 0;
3721 int i, err;
3722
Takashi Iwaif72706b2013-01-16 18:20:07 +01003723 if (spec->suppress_auto_mute)
3724 return 0;
3725
Takashi Iwai352f7f92012-12-19 12:52:06 +01003726 if (cfg->hp_pins[0])
3727 present++;
3728 if (cfg->line_out_pins[0])
3729 present++;
3730 if (cfg->speaker_pins[0])
3731 present++;
3732 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02003733 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003734
3735 if (!cfg->speaker_pins[0] &&
3736 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
3737 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3738 sizeof(cfg->speaker_pins));
3739 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741
Takashi Iwai352f7f92012-12-19 12:52:06 +01003742 if (!cfg->hp_pins[0] &&
3743 cfg->line_out_type == AUTO_PIN_HP_OUT) {
3744 memcpy(cfg->hp_pins, cfg->line_out_pins,
3745 sizeof(cfg->hp_pins));
3746 cfg->hp_outs = cfg->line_outs;
3747 }
3748
3749 for (i = 0; i < cfg->hp_outs; i++) {
3750 hda_nid_t nid = cfg->hp_pins[i];
3751 if (!is_jack_detectable(codec, nid))
3752 continue;
3753 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
3754 nid);
3755 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003756 spec->hp_automute_hook ?
3757 spec->hp_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003758 snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003759 spec->detect_hp = 1;
3760 }
3761
3762 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
3763 if (cfg->speaker_outs)
3764 for (i = 0; i < cfg->line_outs; i++) {
3765 hda_nid_t nid = cfg->line_out_pins[i];
3766 if (!is_jack_detectable(codec, nid))
3767 continue;
3768 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
3769 snd_hda_jack_detect_enable_callback(codec, nid,
3770 HDA_GEN_FRONT_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003771 spec->line_automute_hook ?
3772 spec->line_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003773 snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003774 spec->detect_lo = 1;
3775 }
3776 spec->automute_lo_possible = spec->detect_hp;
3777 }
3778
3779 spec->automute_speaker_possible = cfg->speaker_outs &&
3780 (spec->detect_hp || spec->detect_lo);
3781
3782 spec->automute_lo = spec->automute_lo_possible;
3783 spec->automute_speaker = spec->automute_speaker_possible;
3784
3785 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
3786 /* create a control for automute mode */
3787 err = add_automute_mode_enum(codec);
3788 if (err < 0)
3789 return err;
3790 }
3791 return 0;
3792}
3793
Takashi Iwai352f7f92012-12-19 12:52:06 +01003794/* check whether all auto-mic pins are valid; setup indices if OK */
3795static bool auto_mic_check_imux(struct hda_codec *codec)
3796{
3797 struct hda_gen_spec *spec = codec->spec;
3798 const struct hda_input_mux *imux;
3799 int i;
3800
3801 imux = &spec->input_mux;
3802 for (i = 0; i < spec->am_num_entries; i++) {
3803 spec->am_entry[i].idx =
3804 find_idx_in_nid_list(spec->am_entry[i].pin,
3805 spec->imux_pins, imux->num_items);
3806 if (spec->am_entry[i].idx < 0)
3807 return false; /* no corresponding imux */
3808 }
3809
3810 /* we don't need the jack detection for the first pin */
3811 for (i = 1; i < spec->am_num_entries; i++)
3812 snd_hda_jack_detect_enable_callback(codec,
3813 spec->am_entry[i].pin,
3814 HDA_GEN_MIC_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003815 spec->mic_autoswitch_hook ?
3816 spec->mic_autoswitch_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003817 snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003818 return true;
3819}
3820
3821static int compare_attr(const void *ap, const void *bp)
3822{
3823 const struct automic_entry *a = ap;
3824 const struct automic_entry *b = bp;
3825 return (int)(a->attr - b->attr);
3826}
3827
3828/*
3829 * Check the availability of auto-mic switch;
3830 * Set up if really supported
3831 */
3832static int check_auto_mic_availability(struct hda_codec *codec)
3833{
3834 struct hda_gen_spec *spec = codec->spec;
3835 struct auto_pin_cfg *cfg = &spec->autocfg;
3836 unsigned int types;
3837 int i, num_pins;
3838
Takashi Iwaid12daf62013-01-07 16:32:11 +01003839 if (spec->suppress_auto_mic)
3840 return 0;
3841
Takashi Iwai352f7f92012-12-19 12:52:06 +01003842 types = 0;
3843 num_pins = 0;
3844 for (i = 0; i < cfg->num_inputs; i++) {
3845 hda_nid_t nid = cfg->inputs[i].pin;
3846 unsigned int attr;
3847 attr = snd_hda_codec_get_pincfg(codec, nid);
3848 attr = snd_hda_get_input_pin_attr(attr);
3849 if (types & (1 << attr))
3850 return 0; /* already occupied */
3851 switch (attr) {
3852 case INPUT_PIN_ATTR_INT:
3853 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3854 return 0; /* invalid type */
3855 break;
3856 case INPUT_PIN_ATTR_UNUSED:
3857 return 0; /* invalid entry */
3858 default:
3859 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
3860 return 0; /* invalid type */
3861 if (!spec->line_in_auto_switch &&
3862 cfg->inputs[i].type != AUTO_PIN_MIC)
3863 return 0; /* only mic is allowed */
3864 if (!is_jack_detectable(codec, nid))
3865 return 0; /* no unsol support */
3866 break;
3867 }
3868 if (num_pins >= MAX_AUTO_MIC_PINS)
3869 return 0;
3870 types |= (1 << attr);
3871 spec->am_entry[num_pins].pin = nid;
3872 spec->am_entry[num_pins].attr = attr;
3873 num_pins++;
3874 }
3875
3876 if (num_pins < 2)
3877 return 0;
3878
3879 spec->am_num_entries = num_pins;
3880 /* sort the am_entry in the order of attr so that the pin with a
3881 * higher attr will be selected when the jack is plugged.
3882 */
3883 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
3884 compare_attr, NULL);
3885
3886 if (!auto_mic_check_imux(codec))
3887 return 0;
3888
3889 spec->auto_mic = 1;
3890 spec->num_adc_nids = 1;
3891 spec->cur_mux[0] = spec->am_entry[0].idx;
3892 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
3893 spec->am_entry[0].pin,
3894 spec->am_entry[1].pin,
3895 spec->am_entry[2].pin);
3896
3897 return 0;
3898}
3899
Takashi Iwai55196ff2013-01-24 17:32:56 +01003900/* power_filter hook; make inactive widgets into power down */
3901static unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
3902 hda_nid_t nid,
3903 unsigned int power_state)
3904{
3905 if (power_state != AC_PWRST_D0)
3906 return power_state;
3907 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
3908 return power_state;
3909 if (is_active_nid(codec, nid, HDA_OUTPUT, 0))
3910 return power_state;
3911 return AC_PWRST_D3;
3912}
3913
Takashi Iwai352f7f92012-12-19 12:52:06 +01003914
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003915/*
3916 * Parse the given BIOS configuration and set up the hda_gen_spec
3917 *
3918 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003919 * or a negative error code
3920 */
3921int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003922 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003923{
3924 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003925 int err;
3926
Takashi Iwai1c70a582013-01-11 17:48:22 +01003927 parse_user_hints(codec);
3928
Takashi Iwaie4a395e2013-01-23 17:00:31 +01003929 if (spec->mixer_nid && !spec->mixer_merge_nid)
3930 spec->mixer_merge_nid = spec->mixer_nid;
3931
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003932 if (cfg != &spec->autocfg) {
3933 spec->autocfg = *cfg;
3934 cfg = &spec->autocfg;
3935 }
3936
David Henningsson6fc4cb92013-01-16 15:58:45 +01003937 fill_all_dac_nids(codec);
3938
Takashi Iwai352f7f92012-12-19 12:52:06 +01003939 if (!cfg->line_outs) {
3940 if (cfg->dig_outs || cfg->dig_in_pin) {
3941 spec->multiout.max_channels = 2;
3942 spec->no_analog = 1;
3943 goto dig_only;
3944 }
3945 return 0; /* can't find valid BIOS pin config */
3946 }
3947
3948 if (!spec->no_primary_hp &&
3949 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
3950 cfg->line_outs <= cfg->hp_outs) {
3951 /* use HP as primary out */
3952 cfg->speaker_outs = cfg->line_outs;
3953 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3954 sizeof(cfg->speaker_pins));
3955 cfg->line_outs = cfg->hp_outs;
3956 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
3957 cfg->hp_outs = 0;
3958 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3959 cfg->line_out_type = AUTO_PIN_HP_OUT;
3960 }
3961
3962 err = parse_output_paths(codec);
3963 if (err < 0)
3964 return err;
3965 err = create_multi_channel_mode(codec);
3966 if (err < 0)
3967 return err;
3968 err = create_multi_out_ctls(codec, cfg);
3969 if (err < 0)
3970 return err;
3971 err = create_hp_out_ctls(codec);
3972 if (err < 0)
3973 return err;
3974 err = create_speaker_out_ctls(codec);
3975 if (err < 0)
3976 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003977 err = create_indep_hp_ctls(codec);
3978 if (err < 0)
3979 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01003980 err = create_loopback_mixing_ctl(codec);
3981 if (err < 0)
3982 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003983 err = create_shared_input(codec);
3984 if (err < 0)
3985 return err;
3986 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003987 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02003988 return err;
3989
Takashi Iwaia07a9492013-01-07 16:44:06 +01003990 spec->const_channel_count = spec->ext_channel_count;
3991 /* check the multiple speaker and headphone pins */
3992 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
3993 spec->const_channel_count = max(spec->const_channel_count,
3994 cfg->speaker_outs * 2);
3995 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
3996 spec->const_channel_count = max(spec->const_channel_count,
3997 cfg->hp_outs * 2);
3998 spec->multiout.max_channels = max(spec->ext_channel_count,
3999 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004000
4001 err = check_auto_mute_availability(codec);
4002 if (err < 0)
4003 return err;
4004
4005 err = check_dyn_adc_switch(codec);
4006 if (err < 0)
4007 return err;
4008
4009 if (!spec->shared_mic_hp) {
4010 err = check_auto_mic_availability(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02004011 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 }
Takashi Iwai071c73a2006-08-23 18:34:06 +02004014
Takashi Iwai352f7f92012-12-19 12:52:06 +01004015 err = create_capture_mixers(codec);
4016 if (err < 0)
4017 return err;
4018
4019 err = parse_mic_boost(codec);
4020 if (err < 0)
4021 return err;
4022
Takashi Iwai978e77e2013-01-10 16:57:58 +01004023 if (spec->add_out_jack_modes) {
4024 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4025 err = create_out_jack_modes(codec, cfg->line_outs,
4026 cfg->line_out_pins);
4027 if (err < 0)
4028 return err;
4029 }
4030 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4031 err = create_out_jack_modes(codec, cfg->hp_outs,
4032 cfg->hp_pins);
4033 if (err < 0)
4034 return err;
4035 }
4036 }
4037
Takashi Iwai352f7f92012-12-19 12:52:06 +01004038 dig_only:
4039 parse_digital(codec);
4040
Takashi Iwai55196ff2013-01-24 17:32:56 +01004041 if (spec->power_down_unused)
4042 codec->power_filter = snd_hda_gen_path_power_filter;
4043
Takashi Iwai352f7f92012-12-19 12:52:06 +01004044 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004046EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047
4048
4049/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004050 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004052
4053/* slave controls for virtual master */
4054static const char * const slave_pfxs[] = {
4055 "Front", "Surround", "Center", "LFE", "Side",
4056 "Headphone", "Speaker", "Mono", "Line Out",
4057 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01004058 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
4059 "Headphone Front", "Headphone Surround", "Headphone CLFE",
4060 "Headphone Side",
Takashi Iwai352f7f92012-12-19 12:52:06 +01004061 NULL,
4062};
4063
4064int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004066 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068
Takashi Iwai36502d02012-12-19 15:15:10 +01004069 if (spec->kctls.used) {
4070 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
4071 if (err < 0)
4072 return err;
4073 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074
Takashi Iwai352f7f92012-12-19 12:52:06 +01004075 if (spec->multiout.dig_out_nid) {
4076 err = snd_hda_create_dig_out_ctls(codec,
4077 spec->multiout.dig_out_nid,
4078 spec->multiout.dig_out_nid,
4079 spec->pcm_rec[1].pcm_type);
4080 if (err < 0)
4081 return err;
4082 if (!spec->no_analog) {
4083 err = snd_hda_create_spdif_share_sw(codec,
4084 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 if (err < 0)
4086 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004087 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 }
4089 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004090 if (spec->dig_in_nid) {
4091 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
4092 if (err < 0)
4093 return err;
4094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095
Takashi Iwai352f7f92012-12-19 12:52:06 +01004096 /* if we have no master control, let's create it */
4097 if (!spec->no_analog &&
4098 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004099 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01004100 spec->vmaster_tlv, slave_pfxs,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004101 "Playback Volume");
4102 if (err < 0)
4103 return err;
4104 }
4105 if (!spec->no_analog &&
4106 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
4107 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
4108 NULL, slave_pfxs,
4109 "Playback Switch",
4110 true, &spec->vmaster_mute.sw_kctl);
4111 if (err < 0)
4112 return err;
4113 if (spec->vmaster_mute.hook)
Takashi Iwaifd25a972012-12-20 14:57:18 +01004114 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
4115 spec->vmaster_mute_enum);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117
Takashi Iwai352f7f92012-12-19 12:52:06 +01004118 free_kctls(spec); /* no longer needed */
4119
4120 if (spec->shared_mic_hp) {
4121 int err;
4122 int nid = spec->autocfg.inputs[1].pin;
4123 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
4124 if (err < 0)
4125 return err;
4126 err = snd_hda_jack_detect_enable(codec, nid, 0);
4127 if (err < 0)
4128 return err;
4129 }
4130
4131 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
4132 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 return err;
4134
4135 return 0;
4136}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004137EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
4138
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139
4140/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004141 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004144static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
4145 struct hda_codec *codec,
4146 struct snd_pcm_substream *substream,
4147 int action)
4148{
4149 struct hda_gen_spec *spec = codec->spec;
4150 if (spec->pcm_playback_hook)
4151 spec->pcm_playback_hook(hinfo, codec, substream, action);
4152}
4153
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004154static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
4155 struct hda_codec *codec,
4156 struct snd_pcm_substream *substream,
4157 int action)
4158{
4159 struct hda_gen_spec *spec = codec->spec;
4160 if (spec->pcm_capture_hook)
4161 spec->pcm_capture_hook(hinfo, codec, substream, action);
4162}
4163
Takashi Iwai352f7f92012-12-19 12:52:06 +01004164/*
4165 * Analog playback callbacks
4166 */
4167static int playback_pcm_open(struct hda_pcm_stream *hinfo,
4168 struct hda_codec *codec,
4169 struct snd_pcm_substream *substream)
4170{
4171 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004172 int err;
4173
4174 mutex_lock(&spec->pcm_mutex);
4175 err = snd_hda_multi_out_analog_open(codec,
4176 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004177 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004178 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004179 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004180 call_pcm_playback_hook(hinfo, codec, substream,
4181 HDA_GEN_PCM_ACT_OPEN);
4182 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004183 mutex_unlock(&spec->pcm_mutex);
4184 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004185}
4186
4187static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01004188 struct hda_codec *codec,
4189 unsigned int stream_tag,
4190 unsigned int format,
4191 struct snd_pcm_substream *substream)
4192{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004193 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004194 int err;
4195
4196 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
4197 stream_tag, format, substream);
4198 if (!err)
4199 call_pcm_playback_hook(hinfo, codec, substream,
4200 HDA_GEN_PCM_ACT_PREPARE);
4201 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004202}
Takashi Iwai97ec5582006-03-21 11:29:07 +01004203
Takashi Iwai352f7f92012-12-19 12:52:06 +01004204static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4205 struct hda_codec *codec,
4206 struct snd_pcm_substream *substream)
4207{
4208 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004209 int err;
4210
4211 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
4212 if (!err)
4213 call_pcm_playback_hook(hinfo, codec, substream,
4214 HDA_GEN_PCM_ACT_CLEANUP);
4215 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004216}
4217
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004218static int playback_pcm_close(struct hda_pcm_stream *hinfo,
4219 struct hda_codec *codec,
4220 struct snd_pcm_substream *substream)
4221{
4222 struct hda_gen_spec *spec = codec->spec;
4223 mutex_lock(&spec->pcm_mutex);
4224 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004225 call_pcm_playback_hook(hinfo, codec, substream,
4226 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004227 mutex_unlock(&spec->pcm_mutex);
4228 return 0;
4229}
4230
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004231static int capture_pcm_open(struct hda_pcm_stream *hinfo,
4232 struct hda_codec *codec,
4233 struct snd_pcm_substream *substream)
4234{
4235 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
4236 return 0;
4237}
4238
4239static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4240 struct hda_codec *codec,
4241 unsigned int stream_tag,
4242 unsigned int format,
4243 struct snd_pcm_substream *substream)
4244{
4245 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4246 call_pcm_capture_hook(hinfo, codec, substream,
4247 HDA_GEN_PCM_ACT_PREPARE);
4248 return 0;
4249}
4250
4251static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4252 struct hda_codec *codec,
4253 struct snd_pcm_substream *substream)
4254{
4255 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4256 call_pcm_capture_hook(hinfo, codec, substream,
4257 HDA_GEN_PCM_ACT_CLEANUP);
4258 return 0;
4259}
4260
4261static int capture_pcm_close(struct hda_pcm_stream *hinfo,
4262 struct hda_codec *codec,
4263 struct snd_pcm_substream *substream)
4264{
4265 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
4266 return 0;
4267}
4268
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004269static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
4270 struct hda_codec *codec,
4271 struct snd_pcm_substream *substream)
4272{
4273 struct hda_gen_spec *spec = codec->spec;
4274 int err = 0;
4275
4276 mutex_lock(&spec->pcm_mutex);
4277 if (!spec->indep_hp_enabled)
4278 err = -EBUSY;
4279 else
4280 spec->active_streams |= 1 << STREAM_INDEP_HP;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004281 call_pcm_playback_hook(hinfo, codec, substream,
4282 HDA_GEN_PCM_ACT_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004283 mutex_unlock(&spec->pcm_mutex);
4284 return err;
4285}
4286
4287static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
4288 struct hda_codec *codec,
4289 struct snd_pcm_substream *substream)
4290{
4291 struct hda_gen_spec *spec = codec->spec;
4292 mutex_lock(&spec->pcm_mutex);
4293 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004294 call_pcm_playback_hook(hinfo, codec, substream,
4295 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004296 mutex_unlock(&spec->pcm_mutex);
4297 return 0;
4298}
4299
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004300static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4301 struct hda_codec *codec,
4302 unsigned int stream_tag,
4303 unsigned int format,
4304 struct snd_pcm_substream *substream)
4305{
4306 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4307 call_pcm_playback_hook(hinfo, codec, substream,
4308 HDA_GEN_PCM_ACT_PREPARE);
4309 return 0;
4310}
4311
4312static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4313 struct hda_codec *codec,
4314 struct snd_pcm_substream *substream)
4315{
4316 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4317 call_pcm_playback_hook(hinfo, codec, substream,
4318 HDA_GEN_PCM_ACT_CLEANUP);
4319 return 0;
4320}
4321
Takashi Iwai352f7f92012-12-19 12:52:06 +01004322/*
4323 * Digital out
4324 */
4325static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
4326 struct hda_codec *codec,
4327 struct snd_pcm_substream *substream)
4328{
4329 struct hda_gen_spec *spec = codec->spec;
4330 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
4331}
4332
4333static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4334 struct hda_codec *codec,
4335 unsigned int stream_tag,
4336 unsigned int format,
4337 struct snd_pcm_substream *substream)
4338{
4339 struct hda_gen_spec *spec = codec->spec;
4340 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
4341 stream_tag, format, substream);
4342}
4343
4344static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4345 struct hda_codec *codec,
4346 struct snd_pcm_substream *substream)
4347{
4348 struct hda_gen_spec *spec = codec->spec;
4349 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
4350}
4351
4352static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
4353 struct hda_codec *codec,
4354 struct snd_pcm_substream *substream)
4355{
4356 struct hda_gen_spec *spec = codec->spec;
4357 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
4358}
4359
4360/*
4361 * Analog capture
4362 */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004363#define alt_capture_pcm_open capture_pcm_open
4364#define alt_capture_pcm_close capture_pcm_close
4365
Takashi Iwai352f7f92012-12-19 12:52:06 +01004366static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4367 struct hda_codec *codec,
4368 unsigned int stream_tag,
4369 unsigned int format,
4370 struct snd_pcm_substream *substream)
4371{
4372 struct hda_gen_spec *spec = codec->spec;
4373
4374 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01004375 stream_tag, 0, format);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004376 call_pcm_capture_hook(hinfo, codec, substream,
4377 HDA_GEN_PCM_ACT_PREPARE);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004378 return 0;
4379}
4380
Takashi Iwai352f7f92012-12-19 12:52:06 +01004381static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4382 struct hda_codec *codec,
4383 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01004384{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004385 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01004386
Takashi Iwai352f7f92012-12-19 12:52:06 +01004387 snd_hda_codec_cleanup_stream(codec,
4388 spec->adc_nids[substream->number + 1]);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004389 call_pcm_capture_hook(hinfo, codec, substream,
4390 HDA_GEN_PCM_ACT_CLEANUP);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004391 return 0;
4392}
4393
Takashi Iwai352f7f92012-12-19 12:52:06 +01004394/*
4395 */
4396static const struct hda_pcm_stream pcm_analog_playback = {
4397 .substreams = 1,
4398 .channels_min = 2,
4399 .channels_max = 8,
4400 /* NID is set in build_pcms */
4401 .ops = {
4402 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004403 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004404 .prepare = playback_pcm_prepare,
4405 .cleanup = playback_pcm_cleanup
4406 },
4407};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408
Takashi Iwai352f7f92012-12-19 12:52:06 +01004409static const struct hda_pcm_stream pcm_analog_capture = {
4410 .substreams = 1,
4411 .channels_min = 2,
4412 .channels_max = 2,
4413 /* NID is set in build_pcms */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004414 .ops = {
4415 .open = capture_pcm_open,
4416 .close = capture_pcm_close,
4417 .prepare = capture_pcm_prepare,
4418 .cleanup = capture_pcm_cleanup
4419 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004420};
4421
4422static const struct hda_pcm_stream pcm_analog_alt_playback = {
4423 .substreams = 1,
4424 .channels_min = 2,
4425 .channels_max = 2,
4426 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004427 .ops = {
4428 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004429 .close = alt_playback_pcm_close,
4430 .prepare = alt_playback_pcm_prepare,
4431 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004432 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004433};
4434
4435static const struct hda_pcm_stream pcm_analog_alt_capture = {
4436 .substreams = 2, /* can be overridden */
4437 .channels_min = 2,
4438 .channels_max = 2,
4439 /* NID is set in build_pcms */
4440 .ops = {
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004441 .open = alt_capture_pcm_open,
4442 .close = alt_capture_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004443 .prepare = alt_capture_pcm_prepare,
4444 .cleanup = alt_capture_pcm_cleanup
4445 },
4446};
4447
4448static const struct hda_pcm_stream pcm_digital_playback = {
4449 .substreams = 1,
4450 .channels_min = 2,
4451 .channels_max = 2,
4452 /* NID is set in build_pcms */
4453 .ops = {
4454 .open = dig_playback_pcm_open,
4455 .close = dig_playback_pcm_close,
4456 .prepare = dig_playback_pcm_prepare,
4457 .cleanup = dig_playback_pcm_cleanup
4458 },
4459};
4460
4461static const struct hda_pcm_stream pcm_digital_capture = {
4462 .substreams = 1,
4463 .channels_min = 2,
4464 .channels_max = 2,
4465 /* NID is set in build_pcms */
4466};
4467
4468/* Used by build_pcms to flag that a PCM has no playback stream */
4469static const struct hda_pcm_stream pcm_null_stream = {
4470 .substreams = 0,
4471 .channels_min = 0,
4472 .channels_max = 0,
4473};
4474
4475/*
4476 * dynamic changing ADC PCM streams
4477 */
4478static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
4479{
4480 struct hda_gen_spec *spec = codec->spec;
4481 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
4482
4483 if (spec->cur_adc && spec->cur_adc != new_adc) {
4484 /* stream is running, let's swap the current ADC */
4485 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
4486 spec->cur_adc = new_adc;
4487 snd_hda_codec_setup_stream(codec, new_adc,
4488 spec->cur_adc_stream_tag, 0,
4489 spec->cur_adc_format);
4490 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004492 return false;
4493}
4494
4495/* analog capture with dynamic dual-adc changes */
4496static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4497 struct hda_codec *codec,
4498 unsigned int stream_tag,
4499 unsigned int format,
4500 struct snd_pcm_substream *substream)
4501{
4502 struct hda_gen_spec *spec = codec->spec;
4503 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
4504 spec->cur_adc_stream_tag = stream_tag;
4505 spec->cur_adc_format = format;
4506 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
4507 return 0;
4508}
4509
4510static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4511 struct hda_codec *codec,
4512 struct snd_pcm_substream *substream)
4513{
4514 struct hda_gen_spec *spec = codec->spec;
4515 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
4516 spec->cur_adc = 0;
4517 return 0;
4518}
4519
4520static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
4521 .substreams = 1,
4522 .channels_min = 2,
4523 .channels_max = 2,
4524 .nid = 0, /* fill later */
4525 .ops = {
4526 .prepare = dyn_adc_capture_pcm_prepare,
4527 .cleanup = dyn_adc_capture_pcm_cleanup
4528 },
4529};
4530
Takashi Iwaif873e532012-12-20 16:58:39 +01004531static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
4532 const char *chip_name)
4533{
4534 char *p;
4535
4536 if (*str)
4537 return;
4538 strlcpy(str, chip_name, len);
4539
4540 /* drop non-alnum chars after a space */
4541 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
4542 if (!isalnum(p[1])) {
4543 *p = 0;
4544 break;
4545 }
4546 }
4547 strlcat(str, sfx, len);
4548}
4549
Takashi Iwai352f7f92012-12-19 12:52:06 +01004550/* build PCM streams based on the parsed results */
4551int snd_hda_gen_build_pcms(struct hda_codec *codec)
4552{
4553 struct hda_gen_spec *spec = codec->spec;
4554 struct hda_pcm *info = spec->pcm_rec;
4555 const struct hda_pcm_stream *p;
4556 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557
4558 codec->num_pcms = 1;
4559 codec->pcm_info = info;
4560
Takashi Iwai352f7f92012-12-19 12:52:06 +01004561 if (spec->no_analog)
4562 goto skip_analog;
4563
Takashi Iwaif873e532012-12-20 16:58:39 +01004564 fill_pcm_stream_name(spec->stream_name_analog,
4565 sizeof(spec->stream_name_analog),
4566 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004567 info->name = spec->stream_name_analog;
4568
4569 if (spec->multiout.num_dacs > 0) {
4570 p = spec->stream_analog_playback;
4571 if (!p)
4572 p = &pcm_analog_playback;
4573 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4574 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
4575 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
4576 spec->multiout.max_channels;
4577 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
4578 spec->autocfg.line_outs == 2)
4579 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
4580 snd_pcm_2_1_chmaps;
4581 }
4582 if (spec->num_adc_nids) {
4583 p = spec->stream_analog_capture;
4584 if (!p) {
4585 if (spec->dyn_adc_switch)
4586 p = &dyn_adc_pcm_analog_capture;
4587 else
4588 p = &pcm_analog_capture;
4589 }
4590 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4591 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
4592 }
4593
Takashi Iwai352f7f92012-12-19 12:52:06 +01004594 skip_analog:
4595 /* SPDIF for stream index #1 */
4596 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01004597 fill_pcm_stream_name(spec->stream_name_digital,
4598 sizeof(spec->stream_name_digital),
4599 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004600 codec->num_pcms = 2;
4601 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
4602 info = spec->pcm_rec + 1;
4603 info->name = spec->stream_name_digital;
4604 if (spec->dig_out_type)
4605 info->pcm_type = spec->dig_out_type;
4606 else
4607 info->pcm_type = HDA_PCM_TYPE_SPDIF;
4608 if (spec->multiout.dig_out_nid) {
4609 p = spec->stream_digital_playback;
4610 if (!p)
4611 p = &pcm_digital_playback;
4612 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4613 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
4614 }
4615 if (spec->dig_in_nid) {
4616 p = spec->stream_digital_capture;
4617 if (!p)
4618 p = &pcm_digital_capture;
4619 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4620 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
4621 }
4622 }
4623
4624 if (spec->no_analog)
4625 return 0;
4626
4627 /* If the use of more than one ADC is requested for the current
4628 * model, configure a second analog capture-only PCM.
4629 */
4630 have_multi_adcs = (spec->num_adc_nids > 1) &&
4631 !spec->dyn_adc_switch && !spec->auto_mic;
4632 /* Additional Analaog capture for index #2 */
4633 if (spec->alt_dac_nid || have_multi_adcs) {
Takashi Iwaia6071482013-01-21 16:50:09 +01004634 fill_pcm_stream_name(spec->stream_name_alt_analog,
4635 sizeof(spec->stream_name_alt_analog),
4636 " Alt Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004637 codec->num_pcms = 3;
4638 info = spec->pcm_rec + 2;
Takashi Iwaia6071482013-01-21 16:50:09 +01004639 info->name = spec->stream_name_alt_analog;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004640 if (spec->alt_dac_nid) {
4641 p = spec->stream_analog_alt_playback;
4642 if (!p)
4643 p = &pcm_analog_alt_playback;
4644 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4645 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
4646 spec->alt_dac_nid;
4647 } else {
4648 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
4649 pcm_null_stream;
4650 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
4651 }
4652 if (have_multi_adcs) {
4653 p = spec->stream_analog_alt_capture;
4654 if (!p)
4655 p = &pcm_analog_alt_capture;
4656 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4657 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
4658 spec->adc_nids[1];
4659 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
4660 spec->num_adc_nids - 1;
4661 } else {
4662 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
4663 pcm_null_stream;
4664 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
4665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 }
4667
4668 return 0;
4669}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004670EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
4671
4672
4673/*
4674 * Standard auto-parser initializations
4675 */
4676
Takashi Iwaid4156932013-01-07 10:08:02 +01004677/* configure the given path as a proper output */
Takashi Iwai2c12c302013-01-10 09:33:29 +01004678static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004679{
4680 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01004681 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004682
Takashi Iwai196c17662013-01-04 15:01:40 +01004683 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01004684 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004685 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01004686 pin = path->path[path->depth - 1];
Takashi Iwai2c12c302013-01-10 09:33:29 +01004687 restore_pin_ctl(codec, pin);
Takashi Iwaie1284af2013-01-03 16:33:02 +01004688 snd_hda_activate_path(codec, path, path->active, true);
4689 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004690}
4691
4692/* initialize primary output paths */
4693static void init_multi_out(struct hda_codec *codec)
4694{
4695 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004696 int i;
4697
Takashi Iwaid4156932013-01-07 10:08:02 +01004698 for (i = 0; i < spec->autocfg.line_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004699 set_output_and_unmute(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004700}
4701
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004702
Takashi Iwai2c12c302013-01-10 09:33:29 +01004703static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004704{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004705 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004706
Takashi Iwaid4156932013-01-07 10:08:02 +01004707 for (i = 0; i < num_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004708 set_output_and_unmute(codec, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004709}
4710
4711/* initialize hp and speaker paths */
4712static void init_extra_out(struct hda_codec *codec)
4713{
4714 struct hda_gen_spec *spec = codec->spec;
4715
4716 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004717 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004718 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
4719 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai2c12c302013-01-10 09:33:29 +01004720 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004721}
4722
4723/* initialize multi-io paths */
4724static void init_multi_io(struct hda_codec *codec)
4725{
4726 struct hda_gen_spec *spec = codec->spec;
4727 int i;
4728
4729 for (i = 0; i < spec->multi_ios; i++) {
4730 hda_nid_t pin = spec->multi_io[i].pin;
4731 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01004732 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004733 if (!path)
4734 continue;
4735 if (!spec->multi_io[i].ctl_in)
4736 spec->multi_io[i].ctl_in =
Takashi Iwai2c12c302013-01-10 09:33:29 +01004737 snd_hda_codec_get_pin_target(codec, pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004738 snd_hda_activate_path(codec, path, path->active, true);
4739 }
4740}
4741
Takashi Iwai352f7f92012-12-19 12:52:06 +01004742/* set up input pins and loopback paths */
4743static void init_analog_input(struct hda_codec *codec)
4744{
4745 struct hda_gen_spec *spec = codec->spec;
4746 struct auto_pin_cfg *cfg = &spec->autocfg;
4747 int i;
4748
4749 for (i = 0; i < cfg->num_inputs; i++) {
4750 hda_nid_t nid = cfg->inputs[i].pin;
4751 if (is_input_pin(codec, nid))
Takashi Iwai2c12c302013-01-10 09:33:29 +01004752 restore_pin_ctl(codec, nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004753
4754 /* init loopback inputs */
4755 if (spec->mixer_nid) {
Takashi Iwai3e367f12013-01-23 17:07:23 +01004756 resume_path_from_idx(codec, spec->loopback_paths[i]);
4757 resume_path_from_idx(codec, spec->loopback_merge_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004758 }
4759 }
4760}
4761
4762/* initialize ADC paths */
4763static void init_input_src(struct hda_codec *codec)
4764{
4765 struct hda_gen_spec *spec = codec->spec;
4766 struct hda_input_mux *imux = &spec->input_mux;
4767 struct nid_path *path;
4768 int i, c, nums;
4769
4770 if (spec->dyn_adc_switch)
4771 nums = 1;
4772 else
4773 nums = spec->num_adc_nids;
4774
4775 for (c = 0; c < nums; c++) {
4776 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01004777 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004778 if (path) {
4779 bool active = path->active;
4780 if (i == spec->cur_mux[c])
4781 active = true;
4782 snd_hda_activate_path(codec, path, active, false);
4783 }
4784 }
4785 }
4786
4787 if (spec->shared_mic_hp)
4788 update_shared_mic_hp(codec, spec->cur_mux[0]);
4789
4790 if (spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01004791 spec->cap_sync_hook(codec, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004792}
4793
4794/* set right pin controls for digital I/O */
4795static void init_digital(struct hda_codec *codec)
4796{
4797 struct hda_gen_spec *spec = codec->spec;
4798 int i;
4799 hda_nid_t pin;
4800
Takashi Iwaid4156932013-01-07 10:08:02 +01004801 for (i = 0; i < spec->autocfg.dig_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004802 set_output_and_unmute(codec, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004803 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01004804 if (pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01004805 restore_pin_ctl(codec, pin);
Takashi Iwai3e367f12013-01-23 17:07:23 +01004806 resume_path_from_idx(codec, spec->digin_path);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01004807 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004808}
4809
Takashi Iwai973e4972012-12-20 15:16:09 +01004810/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
4811 * invalid unsol tags by some reason
4812 */
4813static void clear_unsol_on_unused_pins(struct hda_codec *codec)
4814{
4815 int i;
4816
4817 for (i = 0; i < codec->init_pins.used; i++) {
4818 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
4819 hda_nid_t nid = pin->nid;
4820 if (is_jack_detectable(codec, nid) &&
4821 !snd_hda_jack_tbl_get(codec, nid))
4822 snd_hda_codec_update_cache(codec, nid, 0,
4823 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
4824 }
4825}
4826
Takashi Iwai5187ac12013-01-07 12:52:16 +01004827/*
4828 * initialize the generic spec;
4829 * this can be put as patch_ops.init function
4830 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004831int snd_hda_gen_init(struct hda_codec *codec)
4832{
4833 struct hda_gen_spec *spec = codec->spec;
4834
4835 if (spec->init_hook)
4836 spec->init_hook(codec);
4837
4838 snd_hda_apply_verbs(codec);
4839
Takashi Iwai3bbcd272012-12-20 11:50:58 +01004840 codec->cached_write = 1;
4841
Takashi Iwai352f7f92012-12-19 12:52:06 +01004842 init_multi_out(codec);
4843 init_extra_out(codec);
4844 init_multi_io(codec);
4845 init_analog_input(codec);
4846 init_input_src(codec);
4847 init_digital(codec);
4848
Takashi Iwai973e4972012-12-20 15:16:09 +01004849 clear_unsol_on_unused_pins(codec);
4850
Takashi Iwai352f7f92012-12-19 12:52:06 +01004851 /* call init functions of standard auto-mute helpers */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01004852 update_automute_all(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004853
Takashi Iwaidc870f32013-01-22 15:24:30 +01004854 snd_hda_codec_flush_cache(codec);
Takashi Iwai3bbcd272012-12-20 11:50:58 +01004855
Takashi Iwai352f7f92012-12-19 12:52:06 +01004856 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
4857 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
4858
4859 hda_call_check_power_status(codec, 0x01);
4860 return 0;
4861}
Takashi Iwaifce52a32013-01-07 12:42:48 +01004862EXPORT_SYMBOL_HDA(snd_hda_gen_init);
4863
Takashi Iwai5187ac12013-01-07 12:52:16 +01004864/*
4865 * free the generic spec;
4866 * this can be put as patch_ops.free function
4867 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01004868void snd_hda_gen_free(struct hda_codec *codec)
4869{
4870 snd_hda_gen_spec_free(codec->spec);
4871 kfree(codec->spec);
4872 codec->spec = NULL;
4873}
4874EXPORT_SYMBOL_HDA(snd_hda_gen_free);
4875
4876#ifdef CONFIG_PM
Takashi Iwai5187ac12013-01-07 12:52:16 +01004877/*
4878 * check the loopback power save state;
4879 * this can be put as patch_ops.check_power_status function
4880 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01004881int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
4882{
4883 struct hda_gen_spec *spec = codec->spec;
4884 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
4885}
4886EXPORT_SYMBOL_HDA(snd_hda_gen_check_power_status);
4887#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01004888
4889
4890/*
4891 * the generic codec support
4892 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893
Takashi Iwai352f7f92012-12-19 12:52:06 +01004894static const struct hda_codec_ops generic_patch_ops = {
4895 .build_controls = snd_hda_gen_build_controls,
4896 .build_pcms = snd_hda_gen_build_pcms,
4897 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01004898 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004899 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02004900#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01004901 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02004902#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903};
4904
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905int snd_hda_parse_generic_codec(struct hda_codec *codec)
4906{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004907 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908 int err;
4909
Takashi Iwaie560d8d2005-09-09 14:21:46 +02004910 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004911 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004913 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004916 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
4917 if (err < 0)
4918 return err;
4919
4920 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004921 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922 goto error;
4923
4924 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 return 0;
4926
Takashi Iwai352f7f92012-12-19 12:52:06 +01004927error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01004928 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929 return err;
4930}
Takashi Iwaifce52a32013-01-07 12:42:48 +01004931EXPORT_SYMBOL_HDA(snd_hda_parse_generic_codec);