blob: 32a85f9cac4bf77cb760880154769ceedcbe7ffc [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>
Takashi Iwaib21bdd02013-11-18 12:03:56 +010031#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <sound/core.h>
Takashi Iwai352f7f92012-12-19 12:52:06 +010033#include <sound/jack.h>
Takashi Iwaid89c6c02014-09-01 10:07:04 +020034#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "hda_codec.h"
36#include "hda_local.h"
Takashi Iwai352f7f92012-12-19 12:52:06 +010037#include "hda_auto_parser.h"
38#include "hda_jack.h"
Takashi Iwai7504b6c2013-03-18 11:25:51 +010039#include "hda_beep.h"
Takashi Iwai352f7f92012-12-19 12:52:06 +010040#include "hda_generic.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Takashi Iwai352f7f92012-12-19 12:52:06 +010043/* initialize hda_gen_spec struct */
44int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
Takashi Iwai352f7f92012-12-19 12:52:06 +010046 snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
Takashi Iwai352f7f92012-12-19 12:52:06 +010047 snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
Takashi Iwai0186f4f2013-02-07 10:45:11 +010048 snd_array_init(&spec->loopback_list, sizeof(struct hda_amp_list), 8);
Takashi Iwai38cf6f12012-12-21 14:09:42 +010049 mutex_init(&spec->pcm_mutex);
Takashi Iwai352f7f92012-12-19 12:52:06 +010050 return 0;
51}
Takashi Iwai2698ea92013-12-18 07:45:52 +010052EXPORT_SYMBOL_GPL(snd_hda_gen_spec_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Takashi Iwai12c93df2012-12-19 14:38:33 +010054struct snd_kcontrol_new *
55snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
56 const struct snd_kcontrol_new *temp)
Takashi Iwai352f7f92012-12-19 12:52:06 +010057{
58 struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
59 if (!knew)
60 return NULL;
61 *knew = *temp;
62 if (name)
63 knew->name = kstrdup(name, GFP_KERNEL);
64 else if (knew->name)
65 knew->name = kstrdup(knew->name, GFP_KERNEL);
66 if (!knew->name)
67 return NULL;
68 return knew;
69}
Takashi Iwai2698ea92013-12-18 07:45:52 +010070EXPORT_SYMBOL_GPL(snd_hda_gen_add_kctl);
Takashi Iwai352f7f92012-12-19 12:52:06 +010071
72static void free_kctls(struct hda_gen_spec *spec)
73{
74 if (spec->kctls.list) {
75 struct snd_kcontrol_new *kctl = spec->kctls.list;
76 int i;
77 for (i = 0; i < spec->kctls.used; i++)
78 kfree(kctl[i].name);
79 }
80 snd_array_free(&spec->kctls);
81}
82
Takashi Iwaia8dca462014-02-10 18:23:57 +010083static void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
Takashi Iwai352f7f92012-12-19 12:52:06 +010084{
85 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 return;
Takashi Iwai352f7f92012-12-19 12:52:06 +010087 free_kctls(spec);
Takashi Iwai352f7f92012-12-19 12:52:06 +010088 snd_array_free(&spec->paths);
Takashi Iwai0186f4f2013-02-07 10:45:11 +010089 snd_array_free(&spec->loopback_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92/*
Takashi Iwai1c70a582013-01-11 17:48:22 +010093 * store user hints
94 */
95static void parse_user_hints(struct hda_codec *codec)
96{
97 struct hda_gen_spec *spec = codec->spec;
98 int val;
99
100 val = snd_hda_get_bool_hint(codec, "jack_detect");
101 if (val >= 0)
102 codec->no_jack_detect = !val;
103 val = snd_hda_get_bool_hint(codec, "inv_jack_detect");
104 if (val >= 0)
105 codec->inv_jack_detect = !!val;
106 val = snd_hda_get_bool_hint(codec, "trigger_sense");
107 if (val >= 0)
108 codec->no_trigger_sense = !val;
109 val = snd_hda_get_bool_hint(codec, "inv_eapd");
110 if (val >= 0)
111 codec->inv_eapd = !!val;
112 val = snd_hda_get_bool_hint(codec, "pcm_format_first");
113 if (val >= 0)
114 codec->pcm_format_first = !!val;
115 val = snd_hda_get_bool_hint(codec, "sticky_stream");
116 if (val >= 0)
117 codec->no_sticky_stream = !val;
118 val = snd_hda_get_bool_hint(codec, "spdif_status_reset");
119 if (val >= 0)
120 codec->spdif_status_reset = !!val;
121 val = snd_hda_get_bool_hint(codec, "pin_amp_workaround");
122 if (val >= 0)
123 codec->pin_amp_workaround = !!val;
124 val = snd_hda_get_bool_hint(codec, "single_adc_amp");
125 if (val >= 0)
126 codec->single_adc_amp = !!val;
127
Takashi Iwaif72706b2013-01-16 18:20:07 +0100128 val = snd_hda_get_bool_hint(codec, "auto_mute");
129 if (val >= 0)
130 spec->suppress_auto_mute = !val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100131 val = snd_hda_get_bool_hint(codec, "auto_mic");
132 if (val >= 0)
133 spec->suppress_auto_mic = !val;
134 val = snd_hda_get_bool_hint(codec, "line_in_auto_switch");
135 if (val >= 0)
136 spec->line_in_auto_switch = !!val;
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200137 val = snd_hda_get_bool_hint(codec, "auto_mute_via_amp");
138 if (val >= 0)
139 spec->auto_mute_via_amp = !!val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100140 val = snd_hda_get_bool_hint(codec, "need_dac_fix");
141 if (val >= 0)
142 spec->need_dac_fix = !!val;
143 val = snd_hda_get_bool_hint(codec, "primary_hp");
144 if (val >= 0)
145 spec->no_primary_hp = !val;
Takashi Iwaida96fb52013-07-29 16:54:36 +0200146 val = snd_hda_get_bool_hint(codec, "multi_io");
147 if (val >= 0)
148 spec->no_multi_io = !val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100149 val = snd_hda_get_bool_hint(codec, "multi_cap_vol");
150 if (val >= 0)
151 spec->multi_cap_vol = !!val;
152 val = snd_hda_get_bool_hint(codec, "inv_dmic_split");
153 if (val >= 0)
154 spec->inv_dmic_split = !!val;
155 val = snd_hda_get_bool_hint(codec, "indep_hp");
156 if (val >= 0)
157 spec->indep_hp = !!val;
158 val = snd_hda_get_bool_hint(codec, "add_stereo_mix_input");
159 if (val >= 0)
160 spec->add_stereo_mix_input = !!val;
Takashi Iwaif811c3c2013-03-07 18:32:59 +0100161 /* the following two are just for compatibility */
Takashi Iwai1c70a582013-01-11 17:48:22 +0100162 val = snd_hda_get_bool_hint(codec, "add_out_jack_modes");
163 if (val >= 0)
Takashi Iwaif811c3c2013-03-07 18:32:59 +0100164 spec->add_jack_modes = !!val;
Takashi Iwai294765582013-01-17 09:52:11 +0100165 val = snd_hda_get_bool_hint(codec, "add_in_jack_modes");
166 if (val >= 0)
Takashi Iwaif811c3c2013-03-07 18:32:59 +0100167 spec->add_jack_modes = !!val;
168 val = snd_hda_get_bool_hint(codec, "add_jack_modes");
169 if (val >= 0)
170 spec->add_jack_modes = !!val;
Takashi Iwai55196ff2013-01-24 17:32:56 +0100171 val = snd_hda_get_bool_hint(codec, "power_down_unused");
172 if (val >= 0)
173 spec->power_down_unused = !!val;
Takashi Iwai967303d2013-02-19 17:12:42 +0100174 val = snd_hda_get_bool_hint(codec, "add_hp_mic");
175 if (val >= 0)
176 spec->hp_mic = !!val;
177 val = snd_hda_get_bool_hint(codec, "hp_mic_detect");
178 if (val >= 0)
179 spec->suppress_hp_mic_detect = !val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100180
181 if (!snd_hda_get_int_hint(codec, "mixer_nid", &val))
182 spec->mixer_nid = val;
183}
184
185/*
Takashi Iwai2c12c302013-01-10 09:33:29 +0100186 * pin control value accesses
187 */
188
189#define update_pin_ctl(codec, pin, val) \
190 snd_hda_codec_update_cache(codec, pin, 0, \
191 AC_VERB_SET_PIN_WIDGET_CONTROL, val)
192
193/* restore the pinctl based on the cached value */
194static inline void restore_pin_ctl(struct hda_codec *codec, hda_nid_t pin)
195{
196 update_pin_ctl(codec, pin, snd_hda_codec_get_pin_target(codec, pin));
197}
198
199/* set the pinctl target value and write it if requested */
200static void set_pin_target(struct hda_codec *codec, hda_nid_t pin,
201 unsigned int val, bool do_write)
202{
203 if (!pin)
204 return;
205 val = snd_hda_correct_pin_ctl(codec, pin, val);
206 snd_hda_codec_set_pin_target(codec, pin, val);
207 if (do_write)
208 update_pin_ctl(codec, pin, val);
209}
210
211/* set pinctl target values for all given pins */
212static void set_pin_targets(struct hda_codec *codec, int num_pins,
213 hda_nid_t *pins, unsigned int val)
214{
215 int i;
216 for (i = 0; i < num_pins; i++)
217 set_pin_target(codec, pins[i], val, false);
218}
219
220/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100221 * parsing paths
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100224/* return the position of NID in the list, or -1 if not found */
225static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
226{
227 int i;
228 for (i = 0; i < nums; i++)
229 if (list[i] == nid)
230 return i;
231 return -1;
232}
233
234/* return true if the given NID is contained in the path */
235static bool is_nid_contained(struct nid_path *path, hda_nid_t nid)
236{
237 return find_idx_in_nid_list(nid, path->path, path->depth) >= 0;
238}
239
Takashi Iwaif5172a72013-01-04 13:19:55 +0100240static struct nid_path *get_nid_path(struct hda_codec *codec,
241 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100242 int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100244 struct hda_gen_spec *spec = codec->spec;
245 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Takashi Iwai352f7f92012-12-19 12:52:06 +0100247 for (i = 0; i < spec->paths.used; i++) {
248 struct nid_path *path = snd_array_elem(&spec->paths, i);
249 if (path->depth <= 0)
250 continue;
251 if ((!from_nid || path->path[0] == from_nid) &&
Takashi Iwaif5172a72013-01-04 13:19:55 +0100252 (!to_nid || path->path[path->depth - 1] == to_nid)) {
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100253 if (!anchor_nid ||
254 (anchor_nid > 0 && is_nid_contained(path, anchor_nid)) ||
255 (anchor_nid < 0 && !is_nid_contained(path, anchor_nid)))
Takashi Iwaif5172a72013-01-04 13:19:55 +0100256 return path;
257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 }
259 return NULL;
260}
Takashi Iwaif5172a72013-01-04 13:19:55 +0100261
262/* get the path between the given NIDs;
263 * passing 0 to either @pin or @dac behaves as a wildcard
264 */
265struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
266 hda_nid_t from_nid, hda_nid_t to_nid)
267{
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100268 return get_nid_path(codec, from_nid, to_nid, 0);
Takashi Iwaif5172a72013-01-04 13:19:55 +0100269}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100270EXPORT_SYMBOL_GPL(snd_hda_get_nid_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100271
Takashi Iwai196c17662013-01-04 15:01:40 +0100272/* get the index number corresponding to the path instance;
273 * the index starts from 1, for easier checking the invalid value
274 */
275int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
276{
277 struct hda_gen_spec *spec = codec->spec;
278 struct nid_path *array = spec->paths.list;
279 ssize_t idx;
280
281 if (!spec->paths.used)
282 return 0;
283 idx = path - array;
284 if (idx < 0 || idx >= spec->paths.used)
285 return 0;
286 return idx + 1;
287}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100288EXPORT_SYMBOL_GPL(snd_hda_get_path_idx);
Takashi Iwai196c17662013-01-04 15:01:40 +0100289
290/* get the path instance corresponding to the given index number */
291struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
292{
293 struct hda_gen_spec *spec = codec->spec;
294
295 if (idx <= 0 || idx > spec->paths.used)
296 return NULL;
297 return snd_array_elem(&spec->paths, idx - 1);
298}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100299EXPORT_SYMBOL_GPL(snd_hda_get_path_from_idx);
Takashi Iwai196c17662013-01-04 15:01:40 +0100300
Takashi Iwai352f7f92012-12-19 12:52:06 +0100301/* check whether the given DAC is already found in any existing paths */
302static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
303{
304 struct hda_gen_spec *spec = codec->spec;
305 int i;
306
307 for (i = 0; i < spec->paths.used; i++) {
308 struct nid_path *path = snd_array_elem(&spec->paths, i);
309 if (path->path[0] == nid)
310 return true;
311 }
312 return false;
313}
314
315/* check whether the given two widgets can be connected */
316static bool is_reachable_path(struct hda_codec *codec,
317 hda_nid_t from_nid, hda_nid_t to_nid)
318{
319 if (!from_nid || !to_nid)
320 return false;
321 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
322}
323
324/* nid, dir and idx */
325#define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
326
327/* check whether the given ctl is already assigned in any path elements */
328static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
329{
330 struct hda_gen_spec *spec = codec->spec;
331 int i;
332
333 val &= AMP_VAL_COMPARE_MASK;
334 for (i = 0; i < spec->paths.used; i++) {
335 struct nid_path *path = snd_array_elem(&spec->paths, i);
336 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
337 return true;
338 }
339 return false;
340}
341
342/* check whether a control with the given (nid, dir, idx) was assigned */
343static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai8999bf02013-01-18 11:01:33 +0100344 int dir, int idx, int type)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100345{
346 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
Takashi Iwai8999bf02013-01-18 11:01:33 +0100347 return is_ctl_used(codec, val, type);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100348}
349
Takashi Iwai4e76a882014-02-25 12:21:03 +0100350static void print_nid_path(struct hda_codec *codec,
351 const char *pfx, struct nid_path *path)
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100352{
353 char buf[40];
Joe Perchesd82353e2014-07-05 13:02:02 -0700354 char *pos = buf;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100355 int i;
356
Joe Perchesd82353e2014-07-05 13:02:02 -0700357 *pos = 0;
358 for (i = 0; i < path->depth; i++)
359 pos += scnprintf(pos, sizeof(buf) - (pos - buf), "%s%02x",
360 pos != buf ? ":" : "",
361 path->path[i]);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100362
Joe Perchesd82353e2014-07-05 13:02:02 -0700363 codec_dbg(codec, "%s path: depth=%d '%s'\n", pfx, path->depth, buf);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100364}
365
Takashi Iwai352f7f92012-12-19 12:52:06 +0100366/* called recursively */
367static bool __parse_nid_path(struct hda_codec *codec,
368 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100369 int anchor_nid, struct nid_path *path,
370 int depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100371{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100372 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100373 int i, nums;
374
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100375 if (to_nid == anchor_nid)
376 anchor_nid = 0; /* anchor passed */
377 else if (to_nid == (hda_nid_t)(-anchor_nid))
378 return false; /* hit the exclusive nid */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100379
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100380 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100381 for (i = 0; i < nums; i++) {
382 if (conn[i] != from_nid) {
383 /* special case: when from_nid is 0,
384 * try to find an empty DAC
385 */
386 if (from_nid ||
387 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
388 is_dac_already_used(codec, conn[i]))
389 continue;
390 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100391 /* anchor is not requested or already passed? */
392 if (anchor_nid <= 0)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100393 goto found;
394 }
395 if (depth >= MAX_NID_PATH_DEPTH)
396 return false;
397 for (i = 0; i < nums; i++) {
398 unsigned int type;
399 type = get_wcaps_type(get_wcaps(codec, conn[i]));
400 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
401 type == AC_WID_PIN)
402 continue;
403 if (__parse_nid_path(codec, from_nid, conn[i],
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100404 anchor_nid, path, depth + 1))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100405 goto found;
406 }
407 return false;
408
409 found:
410 path->path[path->depth] = conn[i];
411 path->idx[path->depth + 1] = i;
412 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
413 path->multi[path->depth + 1] = 1;
414 path->depth++;
415 return true;
416}
417
418/* parse the widget path from the given nid to the target nid;
419 * when @from_nid is 0, try to find an empty DAC;
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100420 * when @anchor_nid is set to a positive value, only paths through the widget
421 * with the given value are evaluated.
422 * when @anchor_nid is set to a negative value, paths through the widget
423 * with the negative of given value are excluded, only other paths are chosen.
424 * when @anchor_nid is zero, no special handling about path selection.
Takashi Iwai352f7f92012-12-19 12:52:06 +0100425 */
426bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100427 hda_nid_t to_nid, int anchor_nid,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100428 struct nid_path *path)
429{
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100430 if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100431 path->path[path->depth] = to_nid;
432 path->depth++;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100433 return true;
434 }
435 return false;
436}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100437EXPORT_SYMBOL_GPL(snd_hda_parse_nid_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100440 * parse the path between the given NIDs and add to the path list.
441 * if no valid path is found, return NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100443struct nid_path *
444snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100445 hda_nid_t to_nid, int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100447 struct hda_gen_spec *spec = codec->spec;
448 struct nid_path *path;
449
450 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
451 return NULL;
452
Takashi Iwaif5172a72013-01-04 13:19:55 +0100453 /* check whether the path has been already added */
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100454 path = get_nid_path(codec, from_nid, to_nid, anchor_nid);
Takashi Iwaif5172a72013-01-04 13:19:55 +0100455 if (path)
456 return path;
457
Takashi Iwai352f7f92012-12-19 12:52:06 +0100458 path = snd_array_new(&spec->paths);
459 if (!path)
460 return NULL;
461 memset(path, 0, sizeof(*path));
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100462 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100463 return path;
464 /* push back */
465 spec->paths.used--;
466 return NULL;
467}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100468EXPORT_SYMBOL_GPL(snd_hda_add_new_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100469
Takashi Iwai980428c2013-01-09 09:28:20 +0100470/* clear the given path as invalid so that it won't be picked up later */
471static void invalidate_nid_path(struct hda_codec *codec, int idx)
472{
473 struct nid_path *path = snd_hda_get_path_from_idx(codec, idx);
474 if (!path)
475 return;
476 memset(path, 0, sizeof(*path));
477}
478
Takashi Iwai36907392013-12-10 17:29:26 +0100479/* return a DAC if paired to the given pin by codec driver */
480static hda_nid_t get_preferred_dac(struct hda_codec *codec, hda_nid_t pin)
481{
482 struct hda_gen_spec *spec = codec->spec;
483 const hda_nid_t *list = spec->preferred_dacs;
484
485 if (!list)
486 return 0;
487 for (; *list; list += 2)
488 if (*list == pin)
489 return list[1];
490 return 0;
491}
492
Takashi Iwai352f7f92012-12-19 12:52:06 +0100493/* look for an empty DAC slot */
494static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
495 bool is_digital)
496{
497 struct hda_gen_spec *spec = codec->spec;
498 bool cap_digital;
499 int i;
500
501 for (i = 0; i < spec->num_all_dacs; i++) {
502 hda_nid_t nid = spec->all_dacs[i];
503 if (!nid || is_dac_already_used(codec, nid))
504 continue;
505 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
506 if (is_digital != cap_digital)
507 continue;
508 if (is_reachable_path(codec, nid, pin))
509 return nid;
510 }
511 return 0;
512}
513
514/* replace the channels in the composed amp value with the given number */
515static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
516{
517 val &= ~(0x3U << 16);
518 val |= chs << 16;
519 return val;
520}
521
522/* check whether the widget has the given amp capability for the direction */
523static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
524 int dir, unsigned int bits)
525{
526 if (!nid)
527 return false;
528 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
529 if (query_amp_caps(codec, nid, dir) & bits)
530 return true;
531 return false;
532}
533
David Henningsson99a55922013-01-16 15:58:44 +0100534static bool same_amp_caps(struct hda_codec *codec, hda_nid_t nid1,
535 hda_nid_t nid2, int dir)
536{
537 if (!(get_wcaps(codec, nid1) & (1 << (dir + 1))))
538 return !(get_wcaps(codec, nid2) & (1 << (dir + 1)));
539 return (query_amp_caps(codec, nid1, dir) ==
540 query_amp_caps(codec, nid2, dir));
541}
542
Takashi Iwai352f7f92012-12-19 12:52:06 +0100543#define nid_has_mute(codec, nid, dir) \
Takashi Iwaif69910d2013-08-08 09:32:37 +0200544 check_amp_caps(codec, nid, dir, (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100545#define nid_has_volume(codec, nid, dir) \
546 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
547
548/* look for a widget suitable for assigning a mute switch in the path */
549static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
550 struct nid_path *path)
551{
552 int i;
553
554 for (i = path->depth - 1; i >= 0; i--) {
555 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
556 return path->path[i];
557 if (i != path->depth - 1 && i != 0 &&
558 nid_has_mute(codec, path->path[i], HDA_INPUT))
559 return path->path[i];
560 }
561 return 0;
562}
563
564/* look for a widget suitable for assigning a volume ctl in the path */
565static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
566 struct nid_path *path)
567{
Takashi Iwaia1114a82013-11-04 16:32:01 +0100568 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100569 int i;
570
571 for (i = path->depth - 1; i >= 0; i--) {
Takashi Iwaia1114a82013-11-04 16:32:01 +0100572 hda_nid_t nid = path->path[i];
573 if ((spec->out_vol_mask >> nid) & 1)
574 continue;
575 if (nid_has_volume(codec, nid, HDA_OUTPUT))
576 return nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100577 }
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200578 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579}
580
581/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100582 * path activation / deactivation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100584
585/* can have the amp-in capability? */
586static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100588 hda_nid_t nid = path->path[idx];
589 unsigned int caps = get_wcaps(codec, nid);
590 unsigned int type = get_wcaps_type(caps);
591
592 if (!(caps & AC_WCAP_IN_AMP))
593 return false;
594 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
595 return false;
596 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597}
598
Takashi Iwai352f7f92012-12-19 12:52:06 +0100599/* can have the amp-out capability? */
600static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100602 hda_nid_t nid = path->path[idx];
603 unsigned int caps = get_wcaps(codec, nid);
604 unsigned int type = get_wcaps_type(caps);
605
606 if (!(caps & AC_WCAP_OUT_AMP))
607 return false;
608 if (type == AC_WID_PIN && !idx) /* only for output pins */
609 return false;
610 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611}
612
Takashi Iwai352f7f92012-12-19 12:52:06 +0100613/* check whether the given (nid,dir,idx) is active */
614static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai7dddf2ae2013-01-24 16:31:35 +0100615 unsigned int dir, unsigned int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100617 struct hda_gen_spec *spec = codec->spec;
618 int i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Takashi Iwai352f7f92012-12-19 12:52:06 +0100620 for (n = 0; n < spec->paths.used; n++) {
621 struct nid_path *path = snd_array_elem(&spec->paths, n);
622 if (!path->active)
623 continue;
624 for (i = 0; i < path->depth; i++) {
625 if (path->path[i] == nid) {
626 if (dir == HDA_OUTPUT || path->idx[i] == idx)
627 return true;
628 break;
629 }
630 }
631 }
632 return false;
633}
634
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +0200635/* check whether the NID is referred by any active paths */
636#define is_active_nid_for_any(codec, nid) \
637 is_active_nid(codec, nid, HDA_OUTPUT, 0)
638
Takashi Iwai352f7f92012-12-19 12:52:06 +0100639/* get the default amp value for the target state */
640static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai8999bf02013-01-18 11:01:33 +0100641 int dir, unsigned int caps, bool enable)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100642{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100643 unsigned int val = 0;
644
Takashi Iwai352f7f92012-12-19 12:52:06 +0100645 if (caps & AC_AMPCAP_NUM_STEPS) {
646 /* set to 0dB */
647 if (enable)
648 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
649 }
Takashi Iwaif69910d2013-08-08 09:32:37 +0200650 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100651 if (!enable)
652 val |= HDA_AMP_MUTE;
653 }
654 return val;
655}
656
657/* initialize the amp value (only at the first time) */
658static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
659{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100660 unsigned int caps = query_amp_caps(codec, nid, dir);
661 int val = get_amp_val_to_activate(codec, nid, dir, caps, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100662 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
663}
664
Takashi Iwai8999bf02013-01-18 11:01:33 +0100665/* calculate amp value mask we can modify;
666 * if the given amp is controlled by mixers, don't touch it
667 */
668static unsigned int get_amp_mask_to_modify(struct hda_codec *codec,
669 hda_nid_t nid, int dir, int idx,
670 unsigned int caps)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100671{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100672 unsigned int mask = 0xff;
673
Takashi Iwaif69910d2013-08-08 09:32:37 +0200674 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
Takashi Iwai8999bf02013-01-18 11:01:33 +0100675 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_MUTE_CTL))
676 mask &= ~0x80;
677 }
678 if (caps & AC_AMPCAP_NUM_STEPS) {
679 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
680 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
681 mask &= ~0x7f;
682 }
683 return mask;
684}
685
686static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
687 int idx, int idx_to_check, bool enable)
688{
689 unsigned int caps;
690 unsigned int mask, val;
691
Takashi Iwai7dddf2ae2013-01-24 16:31:35 +0100692 if (!enable && is_active_nid(codec, nid, dir, idx_to_check))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100693 return;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100694
695 caps = query_amp_caps(codec, nid, dir);
696 val = get_amp_val_to_activate(codec, nid, dir, caps, enable);
697 mask = get_amp_mask_to_modify(codec, nid, dir, idx_to_check, caps);
698 if (!mask)
699 return;
700
701 val &= mask;
702 snd_hda_codec_amp_stereo(codec, nid, dir, idx, mask, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100703}
704
705static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
706 int i, bool enable)
707{
708 hda_nid_t nid = path->path[i];
709 init_amp(codec, nid, HDA_OUTPUT, 0);
Takashi Iwai8999bf02013-01-18 11:01:33 +0100710 activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100711}
712
713static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
714 int i, bool enable, bool add_aamix)
715{
716 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100717 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100718 int n, nums, idx;
719 int type;
720 hda_nid_t nid = path->path[i];
721
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100722 nums = snd_hda_get_conn_list(codec, nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100723 type = get_wcaps_type(get_wcaps(codec, nid));
724 if (type == AC_WID_PIN ||
725 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
726 nums = 1;
727 idx = 0;
728 } else
729 idx = path->idx[i];
730
731 for (n = 0; n < nums; n++)
732 init_amp(codec, nid, HDA_INPUT, n);
733
Takashi Iwai352f7f92012-12-19 12:52:06 +0100734 /* here is a little bit tricky in comparison with activate_amp_out();
735 * when aa-mixer is available, we need to enable the path as well
736 */
737 for (n = 0; n < nums; n++) {
Takashi Iwaie4a395e2013-01-23 17:00:31 +0100738 if (n != idx && (!add_aamix || conn[n] != spec->mixer_merge_nid))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100739 continue;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100740 activate_amp(codec, nid, HDA_INPUT, n, idx, enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 }
742}
743
Takashi Iwai352f7f92012-12-19 12:52:06 +0100744/* activate or deactivate the given path
745 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100747void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
748 bool enable, bool add_aamix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
Takashi Iwai55196ff2013-01-24 17:32:56 +0100750 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100751 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Takashi Iwai352f7f92012-12-19 12:52:06 +0100753 if (!enable)
754 path->active = false;
755
756 for (i = path->depth - 1; i >= 0; i--) {
Takashi Iwai55196ff2013-01-24 17:32:56 +0100757 hda_nid_t nid = path->path[i];
758 if (enable && spec->power_down_unused) {
759 /* make sure the widget is powered up */
760 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0))
761 snd_hda_codec_write(codec, nid, 0,
762 AC_VERB_SET_POWER_STATE,
763 AC_PWRST_D0);
764 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100765 if (enable && path->multi[i])
Takashi Iwai8f0972d2014-01-27 16:26:16 +0100766 snd_hda_codec_update_cache(codec, nid, 0,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100767 AC_VERB_SET_CONNECT_SEL,
768 path->idx[i]);
769 if (has_amp_in(codec, path, i))
770 activate_amp_in(codec, path, i, enable, add_aamix);
771 if (has_amp_out(codec, path, i))
772 activate_amp_out(codec, path, i, enable);
773 }
774
775 if (enable)
776 path->active = true;
777}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100778EXPORT_SYMBOL_GPL(snd_hda_activate_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100779
Takashi Iwai55196ff2013-01-24 17:32:56 +0100780/* if the given path is inactive, put widgets into D3 (only if suitable) */
781static void path_power_down_sync(struct hda_codec *codec, struct nid_path *path)
782{
783 struct hda_gen_spec *spec = codec->spec;
Jiri Slaby868211d2013-04-04 22:32:10 +0200784 bool changed = false;
Takashi Iwai55196ff2013-01-24 17:32:56 +0100785 int i;
786
787 if (!spec->power_down_unused || path->active)
788 return;
789
790 for (i = 0; i < path->depth; i++) {
791 hda_nid_t nid = path->path[i];
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +0200792 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D3) &&
793 !is_active_nid_for_any(codec, nid)) {
Takashi Iwai55196ff2013-01-24 17:32:56 +0100794 snd_hda_codec_write(codec, nid, 0,
795 AC_VERB_SET_POWER_STATE,
796 AC_PWRST_D3);
797 changed = true;
798 }
799 }
800
801 if (changed) {
802 msleep(10);
803 snd_hda_codec_read(codec, path->path[0], 0,
804 AC_VERB_GET_POWER_STATE, 0);
805 }
806}
807
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100808/* turn on/off EAPD on the given pin */
809static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
810{
811 struct hda_gen_spec *spec = codec->spec;
812 if (spec->own_eapd_ctl ||
813 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
814 return;
Takashi Iwai05909d5c2013-05-31 19:55:54 +0200815 if (spec->keep_eapd_on && !enable)
816 return;
Takashi Iwai468ac412013-11-12 11:36:00 +0100817 if (codec->inv_eapd)
818 enable = !enable;
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100819 snd_hda_codec_update_cache(codec, pin, 0,
820 AC_VERB_SET_EAPD_BTLENABLE,
821 enable ? 0x02 : 0x00);
822}
823
Takashi Iwai3e367f12013-01-23 17:07:23 +0100824/* re-initialize the path specified by the given path index */
825static void resume_path_from_idx(struct hda_codec *codec, int path_idx)
826{
827 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
828 if (path)
829 snd_hda_activate_path(codec, path, path->active, false);
830}
831
Takashi Iwai352f7f92012-12-19 12:52:06 +0100832
833/*
834 * Helper functions for creating mixer ctl elements
835 */
836
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200837static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
838 struct snd_ctl_elem_value *ucontrol);
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200839static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
840 struct snd_ctl_elem_value *ucontrol);
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200841
Takashi Iwai352f7f92012-12-19 12:52:06 +0100842enum {
843 HDA_CTL_WIDGET_VOL,
844 HDA_CTL_WIDGET_MUTE,
845 HDA_CTL_BIND_MUTE,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100846};
847static const struct snd_kcontrol_new control_templates[] = {
848 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200849 /* only the put callback is replaced for handling the special mute */
850 {
851 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
852 .subdevice = HDA_SUBDEV_AMP_FLAG,
853 .info = snd_hda_mixer_amp_switch_info,
854 .get = snd_hda_mixer_amp_switch_get,
855 .put = hda_gen_mixer_mute_put, /* replaced */
856 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
857 },
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200858 {
859 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
860 .info = snd_hda_mixer_amp_switch_info,
861 .get = snd_hda_mixer_bind_switch_get,
862 .put = hda_gen_bind_mute_put, /* replaced */
863 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
864 },
Takashi Iwai352f7f92012-12-19 12:52:06 +0100865};
866
867/* add dynamic controls from template */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100868static struct snd_kcontrol_new *
869add_control(struct hda_gen_spec *spec, int type, const char *name,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100870 int cidx, unsigned long val)
871{
872 struct snd_kcontrol_new *knew;
873
Takashi Iwai12c93df2012-12-19 14:38:33 +0100874 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100875 if (!knew)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100876 return NULL;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100877 knew->index = cidx;
878 if (get_amp_nid_(val))
879 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
880 knew->private_value = val;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100881 return knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100882}
883
884static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
885 const char *pfx, const char *dir,
886 const char *sfx, int cidx, unsigned long val)
887{
Takashi Iwai975cc022013-06-28 11:56:49 +0200888 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai352f7f92012-12-19 12:52:06 +0100889 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100890 if (!add_control(spec, type, name, cidx, val))
891 return -ENOMEM;
892 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100893}
894
895#define add_pb_vol_ctrl(spec, type, pfx, val) \
896 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
897#define add_pb_sw_ctrl(spec, type, pfx, val) \
898 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
899#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
900 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
901#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
902 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
903
904static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
905 unsigned int chs, struct nid_path *path)
906{
907 unsigned int val;
908 if (!path)
909 return 0;
910 val = path->ctls[NID_PATH_VOL_CTL];
911 if (!val)
912 return 0;
913 val = amp_val_replace_channels(val, chs);
914 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
915}
916
917/* return the channel bits suitable for the given path->ctls[] */
918static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
919 int type)
920{
921 int chs = 1; /* mono (left only) */
922 if (path) {
923 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
924 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
925 chs = 3; /* stereo */
926 }
927 return chs;
928}
929
930static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
931 struct nid_path *path)
932{
933 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
934 return add_vol_ctl(codec, pfx, cidx, chs, path);
935}
936
937/* create a mute-switch for the given mixer widget;
938 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
939 */
940static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
941 unsigned int chs, struct nid_path *path)
942{
943 unsigned int val;
944 int type = HDA_CTL_WIDGET_MUTE;
945
946 if (!path)
947 return 0;
948 val = path->ctls[NID_PATH_MUTE_CTL];
949 if (!val)
950 return 0;
951 val = amp_val_replace_channels(val, chs);
952 if (get_amp_direction_(val) == HDA_INPUT) {
953 hda_nid_t nid = get_amp_nid_(val);
954 int nums = snd_hda_get_num_conns(codec, nid);
955 if (nums > 1) {
956 type = HDA_CTL_BIND_MUTE;
957 val |= nums << 19;
958 }
959 }
960 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
961}
962
963static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
964 int cidx, struct nid_path *path)
965{
966 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
967 return add_sw_ctl(codec, pfx, cidx, chs, path);
968}
969
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200970/* playback mute control with the software mute bit check */
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200971static void sync_auto_mute_bits(struct snd_kcontrol *kcontrol,
972 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200973{
974 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
975 struct hda_gen_spec *spec = codec->spec;
976
977 if (spec->auto_mute_via_amp) {
978 hda_nid_t nid = get_amp_nid(kcontrol);
979 bool enabled = !((spec->mute_bits >> nid) & 1);
980 ucontrol->value.integer.value[0] &= enabled;
981 ucontrol->value.integer.value[1] &= enabled;
982 }
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200983}
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200984
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200985static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
986 struct snd_ctl_elem_value *ucontrol)
987{
988 sync_auto_mute_bits(kcontrol, ucontrol);
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200989 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
990}
991
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200992static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
993 struct snd_ctl_elem_value *ucontrol)
994{
995 sync_auto_mute_bits(kcontrol, ucontrol);
996 return snd_hda_mixer_bind_switch_put(kcontrol, ucontrol);
997}
998
Takashi Iwai247d85e2013-01-17 16:18:11 +0100999/* any ctl assigned to the path with the given index? */
1000static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
1001{
1002 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
1003 return path && path->ctls[ctl_type];
1004}
1005
Takashi Iwai352f7f92012-12-19 12:52:06 +01001006static const char * const channel_name[4] = {
1007 "Front", "Surround", "CLFE", "Side"
1008};
1009
1010/* give some appropriate ctl name prefix for the given line out channel */
Takashi Iwai247d85e2013-01-17 16:18:11 +01001011static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
1012 int *index, int ctl_type)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001013{
Takashi Iwai247d85e2013-01-17 16:18:11 +01001014 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001015 struct auto_pin_cfg *cfg = &spec->autocfg;
1016
1017 *index = 0;
1018 if (cfg->line_outs == 1 && !spec->multi_ios &&
Takashi Iwai247d85e2013-01-17 16:18:11 +01001019 !cfg->hp_outs && !cfg->speaker_outs)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001020 return spec->vmaster_mute.hook ? "PCM" : "Master";
1021
1022 /* if there is really a single DAC used in the whole output paths,
1023 * use it master (or "PCM" if a vmaster hook is present)
1024 */
1025 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
1026 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
1027 return spec->vmaster_mute.hook ? "PCM" : "Master";
1028
Takashi Iwai247d85e2013-01-17 16:18:11 +01001029 /* multi-io channels */
1030 if (ch >= cfg->line_outs)
1031 return channel_name[ch];
1032
Takashi Iwai352f7f92012-12-19 12:52:06 +01001033 switch (cfg->line_out_type) {
1034 case AUTO_PIN_SPEAKER_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +01001035 /* if the primary channel vol/mute is shared with HP volume,
1036 * don't name it as Speaker
1037 */
1038 if (!ch && cfg->hp_outs &&
1039 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
1040 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001041 if (cfg->line_outs == 1)
1042 return "Speaker";
1043 if (cfg->line_outs == 2)
1044 return ch ? "Bass Speaker" : "Speaker";
1045 break;
1046 case AUTO_PIN_HP_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +01001047 /* if the primary channel vol/mute is shared with spk volume,
1048 * don't name it as Headphone
1049 */
1050 if (!ch && cfg->speaker_outs &&
1051 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
1052 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001053 /* for multi-io case, only the primary out */
1054 if (ch && spec->multi_ios)
1055 break;
1056 *index = ch;
1057 return "Headphone";
Takashi Iwai352f7f92012-12-19 12:52:06 +01001058 }
Takashi Iwai247d85e2013-01-17 16:18:11 +01001059
1060 /* for a single channel output, we don't have to name the channel */
1061 if (cfg->line_outs == 1 && !spec->multi_ios)
1062 return "PCM";
1063
Takashi Iwai352f7f92012-12-19 12:52:06 +01001064 if (ch >= ARRAY_SIZE(channel_name)) {
1065 snd_BUG();
1066 return "PCM";
1067 }
1068
1069 return channel_name[ch];
1070}
1071
1072/*
1073 * Parse output paths
1074 */
1075
1076/* badness definition */
1077enum {
1078 /* No primary DAC is found for the main output */
1079 BAD_NO_PRIMARY_DAC = 0x10000,
1080 /* No DAC is found for the extra output */
1081 BAD_NO_DAC = 0x4000,
1082 /* No possible multi-ios */
Takashi Iwai1d739062013-02-13 14:17:32 +01001083 BAD_MULTI_IO = 0x120,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001084 /* No individual DAC for extra output */
1085 BAD_NO_EXTRA_DAC = 0x102,
1086 /* No individual DAC for extra surrounds */
1087 BAD_NO_EXTRA_SURR_DAC = 0x101,
1088 /* Primary DAC shared with main surrounds */
1089 BAD_SHARED_SURROUND = 0x100,
Takashi Iwai55a63d42013-03-21 17:20:12 +01001090 /* No independent HP possible */
Takashi Iwaibec8e682013-03-22 15:10:08 +01001091 BAD_NO_INDEP_HP = 0x10,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001092 /* Primary DAC shared with main CLFE */
1093 BAD_SHARED_CLFE = 0x10,
1094 /* Primary DAC shared with extra surrounds */
1095 BAD_SHARED_EXTRA_SURROUND = 0x10,
1096 /* Volume widget is shared */
1097 BAD_SHARED_VOL = 0x10,
1098};
1099
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001100/* look for widgets in the given path which are appropriate for
Takashi Iwai352f7f92012-12-19 12:52:06 +01001101 * volume and mute controls, and assign the values to ctls[].
1102 *
1103 * When no appropriate widget is found in the path, the badness value
1104 * is incremented depending on the situation. The function returns the
1105 * total badness for both volume and mute controls.
1106 */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001107static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001108{
Takashi Iwaid89c6c02014-09-01 10:07:04 +02001109 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001110 hda_nid_t nid;
1111 unsigned int val;
1112 int badness = 0;
1113
1114 if (!path)
1115 return BAD_SHARED_VOL * 2;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001116
1117 if (path->ctls[NID_PATH_VOL_CTL] ||
1118 path->ctls[NID_PATH_MUTE_CTL])
1119 return 0; /* already evaluated */
1120
Takashi Iwai352f7f92012-12-19 12:52:06 +01001121 nid = look_for_out_vol_nid(codec, path);
1122 if (nid) {
1123 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
Takashi Iwaid89c6c02014-09-01 10:07:04 +02001124 if (spec->dac_min_mute)
1125 val |= HDA_AMP_VAL_MIN_MUTE;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001126 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1127 badness += BAD_SHARED_VOL;
1128 else
1129 path->ctls[NID_PATH_VOL_CTL] = val;
1130 } else
1131 badness += BAD_SHARED_VOL;
1132 nid = look_for_out_mute_nid(codec, path);
1133 if (nid) {
1134 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1135 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1136 nid_has_mute(codec, nid, HDA_OUTPUT))
1137 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1138 else
1139 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1140 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1141 badness += BAD_SHARED_VOL;
1142 else
1143 path->ctls[NID_PATH_MUTE_CTL] = val;
1144 } else
1145 badness += BAD_SHARED_VOL;
1146 return badness;
1147}
1148
Takashi Iwai98bd1112013-03-22 14:53:50 +01001149const struct badness_table hda_main_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001150 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1151 .no_dac = BAD_NO_DAC,
1152 .shared_primary = BAD_NO_PRIMARY_DAC,
1153 .shared_surr = BAD_SHARED_SURROUND,
1154 .shared_clfe = BAD_SHARED_CLFE,
1155 .shared_surr_main = BAD_SHARED_SURROUND,
1156};
Takashi Iwai2698ea92013-12-18 07:45:52 +01001157EXPORT_SYMBOL_GPL(hda_main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001158
Takashi Iwai98bd1112013-03-22 14:53:50 +01001159const struct badness_table hda_extra_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001160 .no_primary_dac = BAD_NO_DAC,
1161 .no_dac = BAD_NO_DAC,
1162 .shared_primary = BAD_NO_EXTRA_DAC,
1163 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1164 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1165 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1166};
Takashi Iwai2698ea92013-12-18 07:45:52 +01001167EXPORT_SYMBOL_GPL(hda_extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001168
Takashi Iwai7385df62013-01-07 09:50:52 +01001169/* get the DAC of the primary output corresponding to the given array index */
1170static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1171{
1172 struct hda_gen_spec *spec = codec->spec;
1173 struct auto_pin_cfg *cfg = &spec->autocfg;
1174
1175 if (cfg->line_outs > idx)
1176 return spec->private_dac_nids[idx];
1177 idx -= cfg->line_outs;
1178 if (spec->multi_ios > idx)
1179 return spec->multi_io[idx].dac;
1180 return 0;
1181}
1182
1183/* return the DAC if it's reachable, otherwise zero */
1184static inline hda_nid_t try_dac(struct hda_codec *codec,
1185 hda_nid_t dac, hda_nid_t pin)
1186{
1187 return is_reachable_path(codec, dac, pin) ? dac : 0;
1188}
1189
Takashi Iwai352f7f92012-12-19 12:52:06 +01001190/* try to assign DACs to pins and return the resultant badness */
1191static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1192 const hda_nid_t *pins, hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001193 int *path_idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001194 const struct badness_table *bad)
1195{
1196 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001197 int i, j;
1198 int badness = 0;
1199 hda_nid_t dac;
1200
1201 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 return 0;
1203
Takashi Iwai352f7f92012-12-19 12:52:06 +01001204 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001205 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001206 hda_nid_t pin = pins[i];
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001207
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001208 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1209 if (path) {
1210 badness += assign_out_path_ctls(codec, path);
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001211 continue;
1212 }
1213
Takashi Iwai36907392013-12-10 17:29:26 +01001214 dacs[i] = get_preferred_dac(codec, pin);
1215 if (dacs[i]) {
1216 if (is_dac_already_used(codec, dacs[i]))
1217 badness += bad->shared_primary;
1218 }
1219
1220 if (!dacs[i])
1221 dacs[i] = look_for_dac(codec, pin, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001222 if (!dacs[i] && !i) {
Takashi Iwai980428c2013-01-09 09:28:20 +01001223 /* try to steal the DAC of surrounds for the front */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001224 for (j = 1; j < num_outs; j++) {
1225 if (is_reachable_path(codec, dacs[j], pin)) {
1226 dacs[0] = dacs[j];
1227 dacs[j] = 0;
Takashi Iwai980428c2013-01-09 09:28:20 +01001228 invalidate_nid_path(codec, path_idx[j]);
Takashi Iwai196c17662013-01-04 15:01:40 +01001229 path_idx[j] = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001230 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 }
1232 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001233 }
1234 dac = dacs[i];
1235 if (!dac) {
Takashi Iwai7385df62013-01-07 09:50:52 +01001236 if (num_outs > 2)
1237 dac = try_dac(codec, get_primary_out(codec, i), pin);
1238 if (!dac)
1239 dac = try_dac(codec, dacs[0], pin);
1240 if (!dac)
1241 dac = try_dac(codec, get_primary_out(codec, i), pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001242 if (dac) {
1243 if (!i)
1244 badness += bad->shared_primary;
1245 else if (i == 1)
1246 badness += bad->shared_surr;
1247 else
1248 badness += bad->shared_clfe;
1249 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1250 dac = spec->private_dac_nids[0];
1251 badness += bad->shared_surr_main;
1252 } else if (!i)
1253 badness += bad->no_primary_dac;
1254 else
1255 badness += bad->no_dac;
1256 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001257 if (!dac)
1258 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001259 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001260 if (!path && !i && spec->mixer_nid) {
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001261 /* try with aamix */
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001262 path = snd_hda_add_new_path(codec, dac, pin, 0);
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001263 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001264 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001265 dac = dacs[i] = 0;
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001266 badness += bad->no_dac;
1267 } else {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001268 /* print_nid_path(codec, "output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001269 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001270 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001271 badness += assign_out_path_ctls(codec, path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001272 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001273 }
1274
1275 return badness;
1276}
1277
1278/* return NID if the given pin has only a single connection to a certain DAC */
1279static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1280{
1281 struct hda_gen_spec *spec = codec->spec;
1282 int i;
1283 hda_nid_t nid_found = 0;
1284
1285 for (i = 0; i < spec->num_all_dacs; i++) {
1286 hda_nid_t nid = spec->all_dacs[i];
1287 if (!nid || is_dac_already_used(codec, nid))
1288 continue;
1289 if (is_reachable_path(codec, nid, pin)) {
1290 if (nid_found)
1291 return 0;
1292 nid_found = nid;
1293 }
1294 }
1295 return nid_found;
1296}
1297
1298/* check whether the given pin can be a multi-io pin */
1299static bool can_be_multiio_pin(struct hda_codec *codec,
1300 unsigned int location, hda_nid_t nid)
1301{
1302 unsigned int defcfg, caps;
1303
1304 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1305 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1306 return false;
1307 if (location && get_defcfg_location(defcfg) != location)
1308 return false;
1309 caps = snd_hda_query_pin_caps(codec, nid);
1310 if (!(caps & AC_PINCAP_OUT))
1311 return false;
1312 return true;
1313}
1314
Takashi Iwaie22aab72013-01-04 14:50:04 +01001315/* count the number of input pins that are capable to be multi-io */
1316static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1317{
1318 struct hda_gen_spec *spec = codec->spec;
1319 struct auto_pin_cfg *cfg = &spec->autocfg;
1320 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1321 unsigned int location = get_defcfg_location(defcfg);
1322 int type, i;
1323 int num_pins = 0;
1324
1325 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1326 for (i = 0; i < cfg->num_inputs; i++) {
1327 if (cfg->inputs[i].type != type)
1328 continue;
1329 if (can_be_multiio_pin(codec, location,
1330 cfg->inputs[i].pin))
1331 num_pins++;
1332 }
1333 }
1334 return num_pins;
1335}
1336
Takashi Iwai352f7f92012-12-19 12:52:06 +01001337/*
1338 * multi-io helper
1339 *
1340 * When hardwired is set, try to fill ony hardwired pins, and returns
1341 * zero if any pins are filled, non-zero if nothing found.
1342 * When hardwired is off, try to fill possible input pins, and returns
1343 * the badness value.
1344 */
1345static int fill_multi_ios(struct hda_codec *codec,
1346 hda_nid_t reference_pin,
Takashi Iwaie22aab72013-01-04 14:50:04 +01001347 bool hardwired)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001348{
1349 struct hda_gen_spec *spec = codec->spec;
1350 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie22aab72013-01-04 14:50:04 +01001351 int type, i, j, num_pins, old_pins;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001352 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1353 unsigned int location = get_defcfg_location(defcfg);
1354 int badness = 0;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001355 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001356
1357 old_pins = spec->multi_ios;
1358 if (old_pins >= 2)
1359 goto end_fill;
1360
Takashi Iwaie22aab72013-01-04 14:50:04 +01001361 num_pins = count_multiio_pins(codec, reference_pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001362 if (num_pins < 2)
1363 goto end_fill;
1364
Takashi Iwai352f7f92012-12-19 12:52:06 +01001365 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1366 for (i = 0; i < cfg->num_inputs; i++) {
1367 hda_nid_t nid = cfg->inputs[i].pin;
1368 hda_nid_t dac = 0;
1369
1370 if (cfg->inputs[i].type != type)
1371 continue;
1372 if (!can_be_multiio_pin(codec, location, nid))
1373 continue;
1374 for (j = 0; j < spec->multi_ios; j++) {
1375 if (nid == spec->multi_io[j].pin)
1376 break;
1377 }
1378 if (j < spec->multi_ios)
1379 continue;
1380
Takashi Iwai352f7f92012-12-19 12:52:06 +01001381 if (hardwired)
1382 dac = get_dac_if_single(codec, nid);
1383 else if (!dac)
1384 dac = look_for_dac(codec, nid, false);
1385 if (!dac) {
1386 badness++;
1387 continue;
1388 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001389 path = snd_hda_add_new_path(codec, dac, nid,
1390 -spec->mixer_nid);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001391 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001392 badness++;
1393 continue;
1394 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01001395 /* print_nid_path(codec, "multiio", path); */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001396 spec->multi_io[spec->multi_ios].pin = nid;
1397 spec->multi_io[spec->multi_ios].dac = dac;
Takashi Iwai196c17662013-01-04 15:01:40 +01001398 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1399 snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001400 spec->multi_ios++;
1401 if (spec->multi_ios >= 2)
1402 break;
1403 }
1404 }
1405 end_fill:
1406 if (badness)
1407 badness = BAD_MULTI_IO;
1408 if (old_pins == spec->multi_ios) {
1409 if (hardwired)
1410 return 1; /* nothing found */
1411 else
1412 return badness; /* no badness if nothing found */
1413 }
1414 if (!hardwired && spec->multi_ios < 2) {
1415 /* cancel newly assigned paths */
1416 spec->paths.used -= spec->multi_ios - old_pins;
1417 spec->multi_ios = old_pins;
1418 return badness;
1419 }
1420
1421 /* assign volume and mute controls */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001422 for (i = old_pins; i < spec->multi_ios; i++) {
1423 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1424 badness += assign_out_path_ctls(codec, path);
1425 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001426
1427 return badness;
1428}
1429
1430/* map DACs for all pins in the list if they are single connections */
1431static bool map_singles(struct hda_codec *codec, int outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001432 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001433{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001434 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001435 int i;
1436 bool found = false;
1437 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001438 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001439 hda_nid_t dac;
1440 if (dacs[i])
1441 continue;
1442 dac = get_dac_if_single(codec, pins[i]);
1443 if (!dac)
1444 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001445 path = snd_hda_add_new_path(codec, dac, pins[i],
1446 -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001447 if (!path && !i && spec->mixer_nid)
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001448 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001449 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001450 dacs[i] = dac;
1451 found = true;
Takashi Iwai4e76a882014-02-25 12:21:03 +01001452 /* print_nid_path(codec, "output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001453 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001454 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001455 }
1456 }
1457 return found;
1458}
1459
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001460/* create a new path including aamix if available, and return its index */
1461static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1462{
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001463 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001464 struct nid_path *path;
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001465 hda_nid_t path_dac, dac, pin;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001466
1467 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001468 if (!path || !path->depth ||
1469 is_nid_contained(path, spec->mixer_nid))
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001470 return 0;
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001471 path_dac = path->path[0];
1472 dac = spec->private_dac_nids[0];
Takashi Iwaif87498b2013-01-21 14:24:31 +01001473 pin = path->path[path->depth - 1];
1474 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1475 if (!path) {
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001476 if (dac != path_dac)
1477 dac = path_dac;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001478 else if (spec->multiout.hp_out_nid[0])
1479 dac = spec->multiout.hp_out_nid[0];
1480 else if (spec->multiout.extra_out_nid[0])
1481 dac = spec->multiout.extra_out_nid[0];
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001482 else
1483 dac = 0;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001484 if (dac)
1485 path = snd_hda_add_new_path(codec, dac, pin,
1486 spec->mixer_nid);
1487 }
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001488 if (!path)
1489 return 0;
Takashi Iwai4e76a882014-02-25 12:21:03 +01001490 /* print_nid_path(codec, "output-aamix", path); */
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001491 path->active = false; /* unused as default */
1492 return snd_hda_get_path_idx(codec, path);
1493}
1494
Takashi Iwai55a63d42013-03-21 17:20:12 +01001495/* check whether the independent HP is available with the current config */
1496static bool indep_hp_possible(struct hda_codec *codec)
1497{
1498 struct hda_gen_spec *spec = codec->spec;
1499 struct auto_pin_cfg *cfg = &spec->autocfg;
1500 struct nid_path *path;
1501 int i, idx;
1502
1503 if (cfg->line_out_type == AUTO_PIN_HP_OUT)
1504 idx = spec->out_paths[0];
1505 else
1506 idx = spec->hp_paths[0];
1507 path = snd_hda_get_path_from_idx(codec, idx);
1508 if (!path)
1509 return false;
1510
1511 /* assume no path conflicts unless aamix is involved */
1512 if (!spec->mixer_nid || !is_nid_contained(path, spec->mixer_nid))
1513 return true;
1514
1515 /* check whether output paths contain aamix */
1516 for (i = 0; i < cfg->line_outs; i++) {
1517 if (spec->out_paths[i] == idx)
1518 break;
1519 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1520 if (path && is_nid_contained(path, spec->mixer_nid))
1521 return false;
1522 }
1523 for (i = 0; i < cfg->speaker_outs; i++) {
1524 path = snd_hda_get_path_from_idx(codec, spec->speaker_paths[i]);
1525 if (path && is_nid_contained(path, spec->mixer_nid))
1526 return false;
1527 }
1528
1529 return true;
1530}
1531
Takashi Iwaia07a9492013-01-07 16:44:06 +01001532/* fill the empty entries in the dac array for speaker/hp with the
1533 * shared dac pointed by the paths
1534 */
1535static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1536 hda_nid_t *dacs, int *path_idx)
1537{
1538 struct nid_path *path;
1539 int i;
1540
1541 for (i = 0; i < num_outs; i++) {
1542 if (dacs[i])
1543 continue;
1544 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1545 if (!path)
1546 continue;
1547 dacs[i] = path->path[0];
1548 }
1549}
1550
Takashi Iwai352f7f92012-12-19 12:52:06 +01001551/* fill in the dac_nids table from the parsed pin configuration */
1552static int fill_and_eval_dacs(struct hda_codec *codec,
1553 bool fill_hardwired,
1554 bool fill_mio_first)
1555{
1556 struct hda_gen_spec *spec = codec->spec;
1557 struct auto_pin_cfg *cfg = &spec->autocfg;
1558 int i, err, badness;
1559
1560 /* set num_dacs once to full for look_for_dac() */
1561 spec->multiout.num_dacs = cfg->line_outs;
1562 spec->multiout.dac_nids = spec->private_dac_nids;
1563 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1564 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1565 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1566 spec->multi_ios = 0;
1567 snd_array_free(&spec->paths);
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001568
1569 /* clear path indices */
1570 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1571 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1572 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1573 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1574 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
Takashi Iwaic697b712013-01-07 17:09:26 +01001575 memset(spec->input_paths, 0, sizeof(spec->input_paths));
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001576 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1577 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1578
Takashi Iwai352f7f92012-12-19 12:52:06 +01001579 badness = 0;
1580
1581 /* fill hard-wired DACs first */
1582 if (fill_hardwired) {
1583 bool mapped;
1584 do {
1585 mapped = map_singles(codec, cfg->line_outs,
1586 cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001587 spec->private_dac_nids,
1588 spec->out_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001589 mapped |= map_singles(codec, cfg->hp_outs,
1590 cfg->hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001591 spec->multiout.hp_out_nid,
1592 spec->hp_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001593 mapped |= map_singles(codec, cfg->speaker_outs,
1594 cfg->speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001595 spec->multiout.extra_out_nid,
1596 spec->speaker_paths);
Takashi Iwaida96fb52013-07-29 16:54:36 +02001597 if (!spec->no_multi_io &&
1598 fill_mio_first && cfg->line_outs == 1 &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01001599 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001600 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001601 if (!err)
1602 mapped = true;
1603 }
1604 } while (mapped);
1605 }
1606
1607 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001608 spec->private_dac_nids, spec->out_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001609 spec->main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001610
Takashi Iwaida96fb52013-07-29 16:54:36 +02001611 if (!spec->no_multi_io && fill_mio_first &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01001612 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1613 /* try to fill multi-io first */
Takashi Iwaie22aab72013-01-04 14:50:04 +01001614 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001615 if (err < 0)
1616 return err;
1617 /* we don't count badness at this stage yet */
1618 }
1619
1620 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1621 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1622 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001623 spec->hp_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001624 spec->extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001625 if (err < 0)
1626 return err;
1627 badness += err;
1628 }
1629 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1630 err = try_assign_dacs(codec, cfg->speaker_outs,
1631 cfg->speaker_pins,
1632 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001633 spec->speaker_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001634 spec->extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001635 if (err < 0)
1636 return err;
1637 badness += err;
1638 }
Takashi Iwaida96fb52013-07-29 16:54:36 +02001639 if (!spec->no_multi_io &&
1640 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001641 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001642 if (err < 0)
1643 return err;
1644 badness += err;
1645 }
Takashi Iwaie22aab72013-01-04 14:50:04 +01001646
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001647 if (spec->mixer_nid) {
1648 spec->aamix_out_paths[0] =
1649 check_aamix_out_path(codec, spec->out_paths[0]);
1650 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1651 spec->aamix_out_paths[1] =
1652 check_aamix_out_path(codec, spec->hp_paths[0]);
1653 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1654 spec->aamix_out_paths[2] =
1655 check_aamix_out_path(codec, spec->speaker_paths[0]);
1656 }
1657
Takashi Iwaida96fb52013-07-29 16:54:36 +02001658 if (!spec->no_multi_io &&
1659 cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
Takashi Iwaie22aab72013-01-04 14:50:04 +01001660 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1661 spec->multi_ios = 1; /* give badness */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001662
Takashi Iwaia07a9492013-01-07 16:44:06 +01001663 /* re-count num_dacs and squash invalid entries */
1664 spec->multiout.num_dacs = 0;
1665 for (i = 0; i < cfg->line_outs; i++) {
1666 if (spec->private_dac_nids[i])
1667 spec->multiout.num_dacs++;
1668 else {
1669 memmove(spec->private_dac_nids + i,
1670 spec->private_dac_nids + i + 1,
1671 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1672 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1673 }
1674 }
1675
1676 spec->ext_channel_count = spec->min_channel_count =
David Henningssonc0f3b212013-01-16 11:45:37 +01001677 spec->multiout.num_dacs * 2;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001678
Takashi Iwai352f7f92012-12-19 12:52:06 +01001679 if (spec->multi_ios == 2) {
1680 for (i = 0; i < 2; i++)
1681 spec->private_dac_nids[spec->multiout.num_dacs++] =
1682 spec->multi_io[i].dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001683 } else if (spec->multi_ios) {
1684 spec->multi_ios = 0;
1685 badness += BAD_MULTI_IO;
1686 }
1687
Takashi Iwai55a63d42013-03-21 17:20:12 +01001688 if (spec->indep_hp && !indep_hp_possible(codec))
1689 badness += BAD_NO_INDEP_HP;
1690
Takashi Iwaia07a9492013-01-07 16:44:06 +01001691 /* re-fill the shared DAC for speaker / headphone */
1692 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1693 refill_shared_dacs(codec, cfg->hp_outs,
1694 spec->multiout.hp_out_nid,
1695 spec->hp_paths);
1696 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1697 refill_shared_dacs(codec, cfg->speaker_outs,
1698 spec->multiout.extra_out_nid,
1699 spec->speaker_paths);
1700
Takashi Iwai352f7f92012-12-19 12:52:06 +01001701 return badness;
1702}
1703
1704#define DEBUG_BADNESS
1705
1706#ifdef DEBUG_BADNESS
Joe Perchesd82353e2014-07-05 13:02:02 -07001707#define debug_badness(fmt, ...) \
1708 codec_dbg(codec, fmt, ##__VA_ARGS__)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001709#else
Joe Perchesd82353e2014-07-05 13:02:02 -07001710#define debug_badness(fmt, ...) \
1711 do { if (0) codec_dbg(codec, fmt, ##__VA_ARGS__); } while (0)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001712#endif
1713
Takashi Iwaia7694092013-01-21 10:43:18 +01001714#ifdef DEBUG_BADNESS
1715static inline void print_nid_path_idx(struct hda_codec *codec,
1716 const char *pfx, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001717{
Takashi Iwaia7694092013-01-21 10:43:18 +01001718 struct nid_path *path;
1719
1720 path = snd_hda_get_path_from_idx(codec, idx);
1721 if (path)
Takashi Iwai4e76a882014-02-25 12:21:03 +01001722 print_nid_path(codec, pfx, path);
Takashi Iwaia7694092013-01-21 10:43:18 +01001723}
1724
1725static void debug_show_configs(struct hda_codec *codec,
1726 struct auto_pin_cfg *cfg)
1727{
1728 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia7694092013-01-21 10:43:18 +01001729 static const char * const lo_type[3] = { "LO", "SP", "HP" };
Takashi Iwaia7694092013-01-21 10:43:18 +01001730 int i;
1731
1732 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001733 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001734 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001735 spec->multiout.dac_nids[0],
1736 spec->multiout.dac_nids[1],
1737 spec->multiout.dac_nids[2],
Takashi Iwaia7694092013-01-21 10:43:18 +01001738 spec->multiout.dac_nids[3],
1739 lo_type[cfg->line_out_type]);
1740 for (i = 0; i < cfg->line_outs; i++)
1741 print_nid_path_idx(codec, " out", spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001742 if (spec->multi_ios > 0)
1743 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1744 spec->multi_ios,
1745 spec->multi_io[0].pin, spec->multi_io[1].pin,
1746 spec->multi_io[0].dac, spec->multi_io[1].dac);
Takashi Iwaia7694092013-01-21 10:43:18 +01001747 for (i = 0; i < spec->multi_ios; i++)
1748 print_nid_path_idx(codec, " mio",
1749 spec->out_paths[cfg->line_outs + i]);
1750 if (cfg->hp_outs)
1751 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001752 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001753 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001754 spec->multiout.hp_out_nid[0],
1755 spec->multiout.hp_out_nid[1],
1756 spec->multiout.hp_out_nid[2],
1757 spec->multiout.hp_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001758 for (i = 0; i < cfg->hp_outs; i++)
1759 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1760 if (cfg->speaker_outs)
1761 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001762 cfg->speaker_pins[0], cfg->speaker_pins[1],
1763 cfg->speaker_pins[2], cfg->speaker_pins[3],
1764 spec->multiout.extra_out_nid[0],
1765 spec->multiout.extra_out_nid[1],
1766 spec->multiout.extra_out_nid[2],
1767 spec->multiout.extra_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001768 for (i = 0; i < cfg->speaker_outs; i++)
1769 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1770 for (i = 0; i < 3; i++)
1771 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001772}
Takashi Iwaia7694092013-01-21 10:43:18 +01001773#else
1774#define debug_show_configs(codec, cfg) /* NOP */
1775#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01001776
1777/* find all available DACs of the codec */
1778static void fill_all_dac_nids(struct hda_codec *codec)
1779{
1780 struct hda_gen_spec *spec = codec->spec;
1781 int i;
1782 hda_nid_t nid = codec->start_nid;
1783
1784 spec->num_all_dacs = 0;
1785 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1786 for (i = 0; i < codec->num_nodes; i++, nid++) {
1787 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1788 continue;
1789 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001790 codec_err(codec, "Too many DACs!\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001791 break;
1792 }
1793 spec->all_dacs[spec->num_all_dacs++] = nid;
1794 }
1795}
1796
1797static int parse_output_paths(struct hda_codec *codec)
1798{
1799 struct hda_gen_spec *spec = codec->spec;
1800 struct auto_pin_cfg *cfg = &spec->autocfg;
1801 struct auto_pin_cfg *best_cfg;
Takashi Iwai9314a582013-01-21 10:49:05 +01001802 unsigned int val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001803 int best_badness = INT_MAX;
1804 int badness;
1805 bool fill_hardwired = true, fill_mio_first = true;
1806 bool best_wired = true, best_mio = true;
1807 bool hp_spk_swapped = false;
1808
Takashi Iwai352f7f92012-12-19 12:52:06 +01001809 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1810 if (!best_cfg)
1811 return -ENOMEM;
1812 *best_cfg = *cfg;
1813
1814 for (;;) {
1815 badness = fill_and_eval_dacs(codec, fill_hardwired,
1816 fill_mio_first);
1817 if (badness < 0) {
1818 kfree(best_cfg);
1819 return badness;
1820 }
1821 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1822 cfg->line_out_type, fill_hardwired, fill_mio_first,
1823 badness);
Takashi Iwaia7694092013-01-21 10:43:18 +01001824 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001825 if (badness < best_badness) {
1826 best_badness = badness;
1827 *best_cfg = *cfg;
1828 best_wired = fill_hardwired;
1829 best_mio = fill_mio_first;
1830 }
1831 if (!badness)
1832 break;
1833 fill_mio_first = !fill_mio_first;
1834 if (!fill_mio_first)
1835 continue;
1836 fill_hardwired = !fill_hardwired;
1837 if (!fill_hardwired)
1838 continue;
1839 if (hp_spk_swapped)
1840 break;
1841 hp_spk_swapped = true;
1842 if (cfg->speaker_outs > 0 &&
1843 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1844 cfg->hp_outs = cfg->line_outs;
1845 memcpy(cfg->hp_pins, cfg->line_out_pins,
1846 sizeof(cfg->hp_pins));
1847 cfg->line_outs = cfg->speaker_outs;
1848 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1849 sizeof(cfg->speaker_pins));
1850 cfg->speaker_outs = 0;
1851 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1852 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1853 fill_hardwired = true;
1854 continue;
1855 }
1856 if (cfg->hp_outs > 0 &&
1857 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1858 cfg->speaker_outs = cfg->line_outs;
1859 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1860 sizeof(cfg->speaker_pins));
1861 cfg->line_outs = cfg->hp_outs;
1862 memcpy(cfg->line_out_pins, cfg->hp_pins,
1863 sizeof(cfg->hp_pins));
1864 cfg->hp_outs = 0;
1865 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1866 cfg->line_out_type = AUTO_PIN_HP_OUT;
1867 fill_hardwired = true;
1868 continue;
1869 }
1870 break;
1871 }
1872
1873 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001874 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001875 *cfg = *best_cfg;
1876 fill_and_eval_dacs(codec, best_wired, best_mio);
1877 }
1878 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1879 cfg->line_out_type, best_wired, best_mio);
Takashi Iwaia7694092013-01-21 10:43:18 +01001880 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001881
1882 if (cfg->line_out_pins[0]) {
1883 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01001884 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001885 if (path)
1886 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
Takashi Iwaid89c6c02014-09-01 10:07:04 +02001887 if (spec->vmaster_nid) {
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01001888 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
1889 HDA_OUTPUT, spec->vmaster_tlv);
Takashi Iwaid89c6c02014-09-01 10:07:04 +02001890 if (spec->dac_min_mute)
1891 spec->vmaster_tlv[3] |= TLV_DB_SCALE_MUTE;
1892 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001893 }
1894
Takashi Iwai9314a582013-01-21 10:49:05 +01001895 /* set initial pinctl targets */
1896 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
1897 val = PIN_HP;
1898 else
1899 val = PIN_OUT;
1900 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
1901 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1902 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
1903 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1904 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
1905 set_pin_targets(codec, cfg->speaker_outs,
1906 cfg->speaker_pins, val);
1907 }
1908
Takashi Iwai55a63d42013-03-21 17:20:12 +01001909 /* clear indep_hp flag if not available */
1910 if (spec->indep_hp && !indep_hp_possible(codec))
1911 spec->indep_hp = 0;
1912
Takashi Iwai352f7f92012-12-19 12:52:06 +01001913 kfree(best_cfg);
1914 return 0;
1915}
1916
1917/* add playback controls from the parsed DAC table */
1918static int create_multi_out_ctls(struct hda_codec *codec,
1919 const struct auto_pin_cfg *cfg)
1920{
1921 struct hda_gen_spec *spec = codec->spec;
1922 int i, err, noutputs;
1923
1924 noutputs = cfg->line_outs;
1925 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1926 noutputs += spec->multi_ios;
1927
1928 for (i = 0; i < noutputs; i++) {
1929 const char *name;
1930 int index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001931 struct nid_path *path;
1932
Takashi Iwai196c17662013-01-04 15:01:40 +01001933 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001934 if (!path)
1935 continue;
Takashi Iwai247d85e2013-01-17 16:18:11 +01001936
1937 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001938 if (!name || !strcmp(name, "CLFE")) {
1939 /* Center/LFE */
1940 err = add_vol_ctl(codec, "Center", 0, 1, path);
1941 if (err < 0)
1942 return err;
1943 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1944 if (err < 0)
1945 return err;
Takashi Iwai247d85e2013-01-17 16:18:11 +01001946 } else {
1947 err = add_stereo_vol(codec, name, index, path);
1948 if (err < 0)
1949 return err;
1950 }
1951
1952 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
1953 if (!name || !strcmp(name, "CLFE")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001954 err = add_sw_ctl(codec, "Center", 0, 1, path);
1955 if (err < 0)
1956 return err;
1957 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1958 if (err < 0)
1959 return err;
1960 } else {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001961 err = add_stereo_sw(codec, name, index, path);
1962 if (err < 0)
1963 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 }
1965 }
1966 return 0;
1967}
1968
Takashi Iwaic2c80382013-01-07 10:33:57 +01001969static int create_extra_out(struct hda_codec *codec, int path_idx,
Takashi Iwai196c17662013-01-04 15:01:40 +01001970 const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001972 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 int err;
1974
Takashi Iwai196c17662013-01-04 15:01:40 +01001975 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001976 if (!path)
1977 return 0;
Takashi Iwaic2c80382013-01-07 10:33:57 +01001978 err = add_stereo_vol(codec, pfx, cidx, path);
1979 if (err < 0)
1980 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001981 err = add_stereo_sw(codec, pfx, cidx, path);
1982 if (err < 0)
1983 return err;
1984 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985}
1986
Takashi Iwai352f7f92012-12-19 12:52:06 +01001987/* add playback controls for speaker and HP outputs */
1988static int create_extra_outs(struct hda_codec *codec, int num_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001989 const int *paths, const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990{
Takashi Iwaic2c80382013-01-07 10:33:57 +01001991 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001992
1993 for (i = 0; i < num_pins; i++) {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001994 const char *name;
Takashi Iwai975cc022013-06-28 11:56:49 +02001995 char tmp[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwaic2c80382013-01-07 10:33:57 +01001996 int err, idx = 0;
1997
1998 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
1999 name = "Bass Speaker";
2000 else if (num_pins >= 3) {
2001 snprintf(tmp, sizeof(tmp), "%s %s",
Takashi Iwai352f7f92012-12-19 12:52:06 +01002002 pfx, channel_name[i]);
Takashi Iwaic2c80382013-01-07 10:33:57 +01002003 name = tmp;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002004 } else {
Takashi Iwaic2c80382013-01-07 10:33:57 +01002005 name = pfx;
2006 idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 }
Takashi Iwaic2c80382013-01-07 10:33:57 +01002008 err = create_extra_out(codec, paths[i], name, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002009 if (err < 0)
2010 return err;
2011 }
2012 return 0;
2013}
Takashi Iwai97ec5582006-03-21 11:29:07 +01002014
Takashi Iwai352f7f92012-12-19 12:52:06 +01002015static int create_hp_out_ctls(struct hda_codec *codec)
2016{
2017 struct hda_gen_spec *spec = codec->spec;
2018 return create_extra_outs(codec, spec->autocfg.hp_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01002019 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002020 "Headphone");
2021}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
Takashi Iwai352f7f92012-12-19 12:52:06 +01002023static int create_speaker_out_ctls(struct hda_codec *codec)
2024{
2025 struct hda_gen_spec *spec = codec->spec;
2026 return create_extra_outs(codec, spec->autocfg.speaker_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01002027 spec->speaker_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002028 "Speaker");
2029}
2030
2031/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002032 * independent HP controls
2033 */
2034
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02002035static void call_hp_automute(struct hda_codec *codec,
2036 struct hda_jack_callback *jack);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002037static int indep_hp_info(struct snd_kcontrol *kcontrol,
2038 struct snd_ctl_elem_info *uinfo)
2039{
2040 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2041}
2042
2043static int indep_hp_get(struct snd_kcontrol *kcontrol,
2044 struct snd_ctl_elem_value *ucontrol)
2045{
2046 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2047 struct hda_gen_spec *spec = codec->spec;
2048 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
2049 return 0;
2050}
2051
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002052static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2053 int nomix_path_idx, int mix_path_idx,
2054 int out_type);
2055
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002056static int indep_hp_put(struct snd_kcontrol *kcontrol,
2057 struct snd_ctl_elem_value *ucontrol)
2058{
2059 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2060 struct hda_gen_spec *spec = codec->spec;
2061 unsigned int select = ucontrol->value.enumerated.item[0];
2062 int ret = 0;
2063
2064 mutex_lock(&spec->pcm_mutex);
2065 if (spec->active_streams) {
2066 ret = -EBUSY;
2067 goto unlock;
2068 }
2069
2070 if (spec->indep_hp_enabled != select) {
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002071 hda_nid_t *dacp;
2072 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2073 dacp = &spec->private_dac_nids[0];
2074 else
2075 dacp = &spec->multiout.hp_out_nid[0];
2076
2077 /* update HP aamix paths in case it conflicts with indep HP */
2078 if (spec->have_aamix_ctl) {
2079 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2080 update_aamix_paths(codec, spec->aamix_mode,
2081 spec->out_paths[0],
2082 spec->aamix_out_paths[0],
2083 spec->autocfg.line_out_type);
2084 else
2085 update_aamix_paths(codec, spec->aamix_mode,
2086 spec->hp_paths[0],
2087 spec->aamix_out_paths[1],
2088 AUTO_PIN_HP_OUT);
2089 }
2090
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002091 spec->indep_hp_enabled = select;
2092 if (spec->indep_hp_enabled)
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002093 *dacp = 0;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002094 else
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002095 *dacp = spec->alt_dac_nid;
Takashi Iwai92603c52013-01-22 07:46:31 +01002096
Takashi Iwai963afde2013-05-31 15:20:31 +02002097 call_hp_automute(codec, NULL);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002098 ret = 1;
2099 }
2100 unlock:
2101 mutex_unlock(&spec->pcm_mutex);
2102 return ret;
2103}
2104
2105static const struct snd_kcontrol_new indep_hp_ctl = {
2106 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2107 .name = "Independent HP",
2108 .info = indep_hp_info,
2109 .get = indep_hp_get,
2110 .put = indep_hp_put,
2111};
2112
2113
2114static int create_indep_hp_ctls(struct hda_codec *codec)
2115{
2116 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002117 hda_nid_t dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002118
2119 if (!spec->indep_hp)
2120 return 0;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002121 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2122 dac = spec->multiout.dac_nids[0];
2123 else
2124 dac = spec->multiout.hp_out_nid[0];
2125 if (!dac) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002126 spec->indep_hp = 0;
2127 return 0;
2128 }
2129
2130 spec->indep_hp_enabled = false;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002131 spec->alt_dac_nid = dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002132 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
2133 return -ENOMEM;
2134 return 0;
2135}
2136
2137/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002138 * channel mode enum control
2139 */
2140
2141static int ch_mode_info(struct snd_kcontrol *kcontrol,
2142 struct snd_ctl_elem_info *uinfo)
2143{
2144 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2145 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002146 int chs;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002147
2148 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2149 uinfo->count = 1;
2150 uinfo->value.enumerated.items = spec->multi_ios + 1;
2151 if (uinfo->value.enumerated.item > spec->multi_ios)
2152 uinfo->value.enumerated.item = spec->multi_ios;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002153 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2154 sprintf(uinfo->value.enumerated.name, "%dch", chs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002155 return 0;
2156}
2157
2158static int ch_mode_get(struct snd_kcontrol *kcontrol,
2159 struct snd_ctl_elem_value *ucontrol)
2160{
2161 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2162 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002163 ucontrol->value.enumerated.item[0] =
2164 (spec->ext_channel_count - spec->min_channel_count) / 2;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002165 return 0;
2166}
2167
Takashi Iwai196c17662013-01-04 15:01:40 +01002168static inline struct nid_path *
2169get_multiio_path(struct hda_codec *codec, int idx)
2170{
2171 struct hda_gen_spec *spec = codec->spec;
2172 return snd_hda_get_path_from_idx(codec,
2173 spec->out_paths[spec->autocfg.line_outs + idx]);
2174}
2175
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002176static void update_automute_all(struct hda_codec *codec);
2177
Takashi Iwai65033cc2013-04-16 12:31:05 +02002178/* Default value to be passed as aamix argument for snd_hda_activate_path();
2179 * used for output paths
2180 */
2181static bool aamix_default(struct hda_gen_spec *spec)
2182{
2183 return !spec->have_aamix_ctl || spec->aamix_mode;
2184}
2185
Takashi Iwai352f7f92012-12-19 12:52:06 +01002186static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2187{
2188 struct hda_gen_spec *spec = codec->spec;
2189 hda_nid_t nid = spec->multi_io[idx].pin;
2190 struct nid_path *path;
2191
Takashi Iwai196c17662013-01-04 15:01:40 +01002192 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002193 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002195
2196 if (path->active == output)
2197 return 0;
2198
2199 if (output) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01002200 set_pin_target(codec, nid, PIN_OUT, true);
Takashi Iwai65033cc2013-04-16 12:31:05 +02002201 snd_hda_activate_path(codec, path, true, aamix_default(spec));
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002202 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002203 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002204 set_pin_eapd(codec, nid, false);
Takashi Iwai65033cc2013-04-16 12:31:05 +02002205 snd_hda_activate_path(codec, path, false, aamix_default(spec));
Takashi Iwai2c12c302013-01-10 09:33:29 +01002206 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002207 path_power_down_sync(codec, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 }
Takashi Iwaia365fed2013-01-10 16:10:06 +01002209
2210 /* update jack retasking in case it modifies any of them */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002211 update_automute_all(codec);
Takashi Iwaia365fed2013-01-10 16:10:06 +01002212
Takashi Iwai352f7f92012-12-19 12:52:06 +01002213 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214}
2215
Takashi Iwai352f7f92012-12-19 12:52:06 +01002216static int ch_mode_put(struct snd_kcontrol *kcontrol,
2217 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002219 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2220 struct hda_gen_spec *spec = codec->spec;
2221 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222
Takashi Iwai352f7f92012-12-19 12:52:06 +01002223 ch = ucontrol->value.enumerated.item[0];
2224 if (ch < 0 || ch > spec->multi_ios)
2225 return -EINVAL;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002226 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002227 return 0;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002228 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002229 for (i = 0; i < spec->multi_ios; i++)
2230 set_multi_io(codec, i, i < ch);
2231 spec->multiout.max_channels = max(spec->ext_channel_count,
2232 spec->const_channel_count);
2233 if (spec->need_dac_fix)
2234 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 return 1;
2236}
2237
Takashi Iwai352f7f92012-12-19 12:52:06 +01002238static const struct snd_kcontrol_new channel_mode_enum = {
2239 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2240 .name = "Channel Mode",
2241 .info = ch_mode_info,
2242 .get = ch_mode_get,
2243 .put = ch_mode_put,
2244};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245
Takashi Iwai352f7f92012-12-19 12:52:06 +01002246static int create_multi_channel_mode(struct hda_codec *codec)
2247{
2248 struct hda_gen_spec *spec = codec->spec;
2249
2250 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002251 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002252 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 return 0;
2255}
2256
Takashi Iwai352f7f92012-12-19 12:52:06 +01002257/*
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002258 * aamix loopback enable/disable switch
2259 */
2260
2261#define loopback_mixing_info indep_hp_info
2262
2263static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2264 struct snd_ctl_elem_value *ucontrol)
2265{
2266 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2267 struct hda_gen_spec *spec = codec->spec;
2268 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2269 return 0;
2270}
2271
2272static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002273 int nomix_path_idx, int mix_path_idx,
2274 int out_type)
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002275{
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002276 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002277 struct nid_path *nomix_path, *mix_path;
2278
2279 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2280 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2281 if (!nomix_path || !mix_path)
2282 return;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002283
2284 /* if HP aamix path is driven from a different DAC and the
2285 * independent HP mode is ON, can't turn on aamix path
2286 */
2287 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2288 mix_path->path[0] != spec->alt_dac_nid)
2289 do_mix = false;
2290
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002291 if (do_mix) {
2292 snd_hda_activate_path(codec, nomix_path, false, true);
2293 snd_hda_activate_path(codec, mix_path, true, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002294 path_power_down_sync(codec, nomix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002295 } else {
Takashi Iwai65033cc2013-04-16 12:31:05 +02002296 snd_hda_activate_path(codec, mix_path, false, false);
2297 snd_hda_activate_path(codec, nomix_path, true, false);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002298 path_power_down_sync(codec, mix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002299 }
2300}
2301
2302static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2303 struct snd_ctl_elem_value *ucontrol)
2304{
2305 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2306 struct hda_gen_spec *spec = codec->spec;
2307 unsigned int val = ucontrol->value.enumerated.item[0];
2308
2309 if (val == spec->aamix_mode)
2310 return 0;
2311 spec->aamix_mode = val;
2312 update_aamix_paths(codec, val, spec->out_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002313 spec->aamix_out_paths[0],
2314 spec->autocfg.line_out_type);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002315 update_aamix_paths(codec, val, spec->hp_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002316 spec->aamix_out_paths[1],
2317 AUTO_PIN_HP_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002318 update_aamix_paths(codec, val, spec->speaker_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002319 spec->aamix_out_paths[2],
2320 AUTO_PIN_SPEAKER_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002321 return 1;
2322}
2323
2324static const struct snd_kcontrol_new loopback_mixing_enum = {
2325 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2326 .name = "Loopback Mixing",
2327 .info = loopback_mixing_info,
2328 .get = loopback_mixing_get,
2329 .put = loopback_mixing_put,
2330};
2331
2332static int create_loopback_mixing_ctl(struct hda_codec *codec)
2333{
2334 struct hda_gen_spec *spec = codec->spec;
2335
2336 if (!spec->mixer_nid)
2337 return 0;
2338 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
2339 spec->aamix_out_paths[2]))
2340 return 0;
2341 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2342 return -ENOMEM;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002343 spec->have_aamix_ctl = 1;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002344 return 0;
2345}
2346
2347/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002348 * shared headphone/mic handling
2349 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002350
Takashi Iwai352f7f92012-12-19 12:52:06 +01002351static void call_update_outputs(struct hda_codec *codec);
2352
2353/* for shared I/O, change the pin-control accordingly */
Takashi Iwai967303d2013-02-19 17:12:42 +01002354static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002355{
2356 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai967303d2013-02-19 17:12:42 +01002357 bool as_mic;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002358 unsigned int val;
Takashi Iwai967303d2013-02-19 17:12:42 +01002359 hda_nid_t pin;
2360
2361 pin = spec->hp_mic_pin;
2362 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
2363
2364 if (!force) {
2365 val = snd_hda_codec_get_pin_target(codec, pin);
2366 if (as_mic) {
2367 if (val & PIN_IN)
2368 return;
2369 } else {
2370 if (val & PIN_OUT)
2371 return;
2372 }
2373 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002374
2375 val = snd_hda_get_default_vref(codec, pin);
Takashi Iwai967303d2013-02-19 17:12:42 +01002376 /* if the HP pin doesn't support VREF and the codec driver gives an
2377 * alternative pin, set up the VREF on that pin instead
2378 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002379 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2380 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2381 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2382 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01002383 snd_hda_set_pin_ctl_cache(codec, vref_pin,
Takashi Iwai967303d2013-02-19 17:12:42 +01002384 PIN_IN | (as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02002385 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002386
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002387 if (!spec->hp_mic_jack_modes) {
2388 if (as_mic)
2389 val |= PIN_IN;
2390 else
2391 val = PIN_HP;
2392 set_pin_target(codec, pin, val, true);
Takashi Iwai963afde2013-05-31 15:20:31 +02002393 call_hp_automute(codec, NULL);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002394 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002395}
2396
2397/* create a shared input with the headphone out */
Takashi Iwai967303d2013-02-19 17:12:42 +01002398static int create_hp_mic(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002399{
2400 struct hda_gen_spec *spec = codec->spec;
2401 struct auto_pin_cfg *cfg = &spec->autocfg;
2402 unsigned int defcfg;
2403 hda_nid_t nid;
2404
Takashi Iwai967303d2013-02-19 17:12:42 +01002405 if (!spec->hp_mic) {
2406 if (spec->suppress_hp_mic_detect)
2407 return 0;
2408 /* automatic detection: only if no input or a single internal
2409 * input pin is found, try to detect the shared hp/mic
2410 */
2411 if (cfg->num_inputs > 1)
2412 return 0;
2413 else if (cfg->num_inputs == 1) {
2414 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2415 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2416 return 0;
2417 }
2418 }
2419
2420 spec->hp_mic = 0; /* clear once */
2421 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002422 return 0;
2423
Takashi Iwai967303d2013-02-19 17:12:42 +01002424 nid = 0;
2425 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2426 nid = cfg->line_out_pins[0];
2427 else if (cfg->hp_outs > 0)
2428 nid = cfg->hp_pins[0];
2429 if (!nid)
2430 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002431
2432 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2433 return 0; /* no input */
2434
Takashi Iwai967303d2013-02-19 17:12:42 +01002435 cfg->inputs[cfg->num_inputs].pin = nid;
2436 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
David Henningssoncb420b12013-04-11 11:30:28 +02002437 cfg->inputs[cfg->num_inputs].is_headphone_mic = 1;
Takashi Iwai967303d2013-02-19 17:12:42 +01002438 cfg->num_inputs++;
2439 spec->hp_mic = 1;
2440 spec->hp_mic_pin = nid;
2441 /* we can't handle auto-mic together with HP-mic */
2442 spec->suppress_auto_mic = 1;
Takashi Iwai4e76a882014-02-25 12:21:03 +01002443 codec_dbg(codec, "Enable shared I/O jack on NID 0x%x\n", nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002444 return 0;
2445}
2446
Takashi Iwai978e77e2013-01-10 16:57:58 +01002447/*
2448 * output jack mode
2449 */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002450
2451static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2452
2453static const char * const out_jack_texts[] = {
2454 "Line Out", "Headphone Out",
2455};
2456
Takashi Iwai978e77e2013-01-10 16:57:58 +01002457static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2458 struct snd_ctl_elem_info *uinfo)
2459{
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002460 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
Takashi Iwai978e77e2013-01-10 16:57:58 +01002461}
2462
2463static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2464 struct snd_ctl_elem_value *ucontrol)
2465{
2466 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2467 hda_nid_t nid = kcontrol->private_value;
2468 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2469 ucontrol->value.enumerated.item[0] = 1;
2470 else
2471 ucontrol->value.enumerated.item[0] = 0;
2472 return 0;
2473}
2474
2475static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2476 struct snd_ctl_elem_value *ucontrol)
2477{
2478 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2479 hda_nid_t nid = kcontrol->private_value;
2480 unsigned int val;
2481
2482 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2483 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2484 return 0;
2485 snd_hda_set_pin_ctl_cache(codec, nid, val);
2486 return 1;
2487}
2488
2489static const struct snd_kcontrol_new out_jack_mode_enum = {
2490 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2491 .info = out_jack_mode_info,
2492 .get = out_jack_mode_get,
2493 .put = out_jack_mode_put,
2494};
2495
2496static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2497{
2498 struct hda_gen_spec *spec = codec->spec;
2499 int i;
2500
2501 for (i = 0; i < spec->kctls.used; i++) {
2502 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2503 if (!strcmp(kctl->name, name) && kctl->index == idx)
2504 return true;
2505 }
2506 return false;
2507}
2508
2509static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2510 char *name, size_t name_len)
2511{
2512 struct hda_gen_spec *spec = codec->spec;
2513 int idx = 0;
2514
2515 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2516 strlcat(name, " Jack Mode", name_len);
2517
2518 for (; find_kctl_name(codec, name, idx); idx++)
2519 ;
2520}
2521
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002522static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2523{
2524 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002525 if (spec->add_jack_modes) {
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002526 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2527 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2528 return 2;
2529 }
2530 return 1;
2531}
2532
Takashi Iwai978e77e2013-01-10 16:57:58 +01002533static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2534 hda_nid_t *pins)
2535{
2536 struct hda_gen_spec *spec = codec->spec;
2537 int i;
2538
2539 for (i = 0; i < num_pins; i++) {
2540 hda_nid_t pin = pins[i];
Takashi Iwaiced4cef2013-11-26 08:33:45 +01002541 if (pin == spec->hp_mic_pin)
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002542 continue;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002543 if (get_out_jack_num_items(codec, pin) > 1) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01002544 struct snd_kcontrol_new *knew;
Takashi Iwai975cc022013-06-28 11:56:49 +02002545 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai978e77e2013-01-10 16:57:58 +01002546 get_jack_mode_name(codec, pin, name, sizeof(name));
2547 knew = snd_hda_gen_add_kctl(spec, name,
2548 &out_jack_mode_enum);
2549 if (!knew)
2550 return -ENOMEM;
2551 knew->private_value = pin;
2552 }
2553 }
2554
2555 return 0;
2556}
2557
Takashi Iwai294765582013-01-17 09:52:11 +01002558/*
2559 * input jack mode
2560 */
2561
2562/* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2563#define NUM_VREFS 6
2564
2565static const char * const vref_texts[NUM_VREFS] = {
2566 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2567 "", "Mic 80pc Bias", "Mic 100pc Bias"
2568};
2569
2570static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2571{
2572 unsigned int pincap;
2573
2574 pincap = snd_hda_query_pin_caps(codec, pin);
2575 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2576 /* filter out unusual vrefs */
2577 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2578 return pincap;
2579}
2580
2581/* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2582static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2583{
2584 unsigned int i, n = 0;
2585
2586 for (i = 0; i < NUM_VREFS; i++) {
2587 if (vref_caps & (1 << i)) {
2588 if (n == item_idx)
2589 return i;
2590 n++;
2591 }
2592 }
2593 return 0;
2594}
2595
2596/* convert back from the vref ctl index to the enum item index */
2597static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2598{
2599 unsigned int i, n = 0;
2600
2601 for (i = 0; i < NUM_VREFS; i++) {
2602 if (i == idx)
2603 return n;
2604 if (vref_caps & (1 << i))
2605 n++;
2606 }
2607 return 0;
2608}
2609
2610static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2611 struct snd_ctl_elem_info *uinfo)
2612{
2613 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2614 hda_nid_t nid = kcontrol->private_value;
2615 unsigned int vref_caps = get_vref_caps(codec, nid);
2616
2617 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2618 vref_texts);
2619 /* set the right text */
2620 strcpy(uinfo->value.enumerated.name,
2621 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2622 return 0;
2623}
2624
2625static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2626 struct snd_ctl_elem_value *ucontrol)
2627{
2628 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2629 hda_nid_t nid = kcontrol->private_value;
2630 unsigned int vref_caps = get_vref_caps(codec, nid);
2631 unsigned int idx;
2632
2633 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2634 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2635 return 0;
2636}
2637
2638static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2639 struct snd_ctl_elem_value *ucontrol)
2640{
2641 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2642 hda_nid_t nid = kcontrol->private_value;
2643 unsigned int vref_caps = get_vref_caps(codec, nid);
2644 unsigned int val, idx;
2645
2646 val = snd_hda_codec_get_pin_target(codec, nid);
2647 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2648 if (idx == ucontrol->value.enumerated.item[0])
2649 return 0;
2650
2651 val &= ~AC_PINCTL_VREFEN;
2652 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2653 snd_hda_set_pin_ctl_cache(codec, nid, val);
2654 return 1;
2655}
2656
2657static const struct snd_kcontrol_new in_jack_mode_enum = {
2658 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2659 .info = in_jack_mode_info,
2660 .get = in_jack_mode_get,
2661 .put = in_jack_mode_put,
2662};
2663
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002664static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2665{
2666 struct hda_gen_spec *spec = codec->spec;
2667 int nitems = 0;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002668 if (spec->add_jack_modes)
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002669 nitems = hweight32(get_vref_caps(codec, pin));
2670 return nitems ? nitems : 1;
2671}
2672
Takashi Iwai294765582013-01-17 09:52:11 +01002673static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2674{
2675 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai294765582013-01-17 09:52:11 +01002676 struct snd_kcontrol_new *knew;
Takashi Iwai975cc022013-06-28 11:56:49 +02002677 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002678 unsigned int defcfg;
2679
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002680 if (pin == spec->hp_mic_pin)
2681 return 0; /* already done in create_out_jack_mode() */
Takashi Iwai294765582013-01-17 09:52:11 +01002682
2683 /* no jack mode for fixed pins */
2684 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2685 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2686 return 0;
2687
2688 /* no multiple vref caps? */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002689 if (get_in_jack_num_items(codec, pin) <= 1)
Takashi Iwai294765582013-01-17 09:52:11 +01002690 return 0;
2691
2692 get_jack_mode_name(codec, pin, name, sizeof(name));
2693 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2694 if (!knew)
2695 return -ENOMEM;
2696 knew->private_value = pin;
2697 return 0;
2698}
2699
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002700/*
2701 * HP/mic shared jack mode
2702 */
2703static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2704 struct snd_ctl_elem_info *uinfo)
2705{
2706 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2707 hda_nid_t nid = kcontrol->private_value;
2708 int out_jacks = get_out_jack_num_items(codec, nid);
2709 int in_jacks = get_in_jack_num_items(codec, nid);
2710 const char *text = NULL;
2711 int idx;
2712
2713 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2714 uinfo->count = 1;
2715 uinfo->value.enumerated.items = out_jacks + in_jacks;
2716 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2717 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2718 idx = uinfo->value.enumerated.item;
2719 if (idx < out_jacks) {
2720 if (out_jacks > 1)
2721 text = out_jack_texts[idx];
2722 else
2723 text = "Headphone Out";
2724 } else {
2725 idx -= out_jacks;
2726 if (in_jacks > 1) {
2727 unsigned int vref_caps = get_vref_caps(codec, nid);
2728 text = vref_texts[get_vref_idx(vref_caps, idx)];
2729 } else
2730 text = "Mic In";
2731 }
2732
2733 strcpy(uinfo->value.enumerated.name, text);
2734 return 0;
2735}
2736
2737static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2738{
2739 int out_jacks = get_out_jack_num_items(codec, nid);
2740 int in_jacks = get_in_jack_num_items(codec, nid);
2741 unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2742 int idx = 0;
2743
2744 if (val & PIN_OUT) {
2745 if (out_jacks > 1 && val == PIN_HP)
2746 idx = 1;
2747 } else if (val & PIN_IN) {
2748 idx = out_jacks;
2749 if (in_jacks > 1) {
2750 unsigned int vref_caps = get_vref_caps(codec, nid);
2751 val &= AC_PINCTL_VREFEN;
2752 idx += cvt_from_vref_idx(vref_caps, val);
2753 }
2754 }
2755 return idx;
2756}
2757
2758static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2759 struct snd_ctl_elem_value *ucontrol)
2760{
2761 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2762 hda_nid_t nid = kcontrol->private_value;
2763 ucontrol->value.enumerated.item[0] =
2764 get_cur_hp_mic_jack_mode(codec, nid);
2765 return 0;
2766}
2767
2768static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2769 struct snd_ctl_elem_value *ucontrol)
2770{
2771 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2772 hda_nid_t nid = kcontrol->private_value;
2773 int out_jacks = get_out_jack_num_items(codec, nid);
2774 int in_jacks = get_in_jack_num_items(codec, nid);
2775 unsigned int val, oldval, idx;
2776
2777 oldval = get_cur_hp_mic_jack_mode(codec, nid);
2778 idx = ucontrol->value.enumerated.item[0];
2779 if (oldval == idx)
2780 return 0;
2781
2782 if (idx < out_jacks) {
2783 if (out_jacks > 1)
2784 val = idx ? PIN_HP : PIN_OUT;
2785 else
2786 val = PIN_HP;
2787 } else {
2788 idx -= out_jacks;
2789 if (in_jacks > 1) {
2790 unsigned int vref_caps = get_vref_caps(codec, nid);
2791 val = snd_hda_codec_get_pin_target(codec, nid);
Takashi Iwai3f550e32013-03-07 18:30:27 +01002792 val &= ~(AC_PINCTL_VREFEN | PIN_HP);
2793 val |= get_vref_idx(vref_caps, idx) | PIN_IN;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002794 } else
Takashi Iwai16c0cefe2013-11-26 08:44:26 +01002795 val = snd_hda_get_default_vref(codec, nid) | PIN_IN;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002796 }
2797 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwai963afde2013-05-31 15:20:31 +02002798 call_hp_automute(codec, NULL);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002799
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002800 return 1;
2801}
2802
2803static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
2804 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2805 .info = hp_mic_jack_mode_info,
2806 .get = hp_mic_jack_mode_get,
2807 .put = hp_mic_jack_mode_put,
2808};
2809
2810static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2811{
2812 struct hda_gen_spec *spec = codec->spec;
2813 struct snd_kcontrol_new *knew;
2814
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002815 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
2816 &hp_mic_jack_mode_enum);
2817 if (!knew)
2818 return -ENOMEM;
2819 knew->private_value = pin;
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002820 spec->hp_mic_jack_modes = 1;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002821 return 0;
2822}
Takashi Iwai352f7f92012-12-19 12:52:06 +01002823
2824/*
2825 * Parse input paths
2826 */
2827
Takashi Iwai352f7f92012-12-19 12:52:06 +01002828/* add the powersave loopback-list entry */
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002829static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002830{
2831 struct hda_amp_list *list;
2832
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002833 list = snd_array_new(&spec->loopback_list);
2834 if (!list)
2835 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002836 list->nid = mix;
2837 list->dir = HDA_INPUT;
2838 list->idx = idx;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002839 spec->loopback.amplist = spec->loopback_list.list;
2840 return 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002841}
Takashi Iwaicb53c622007-08-10 17:21:45 +02002842
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002843/* return true if either a volume or a mute amp is found for the given
2844 * aamix path; the amp has to be either in the mixer node or its direct leaf
2845 */
2846static bool look_for_mix_leaf_ctls(struct hda_codec *codec, hda_nid_t mix_nid,
2847 hda_nid_t pin, unsigned int *mix_val,
2848 unsigned int *mute_val)
2849{
2850 int idx, num_conns;
2851 const hda_nid_t *list;
2852 hda_nid_t nid;
2853
2854 idx = snd_hda_get_conn_index(codec, mix_nid, pin, true);
2855 if (idx < 0)
2856 return false;
2857
2858 *mix_val = *mute_val = 0;
2859 if (nid_has_volume(codec, mix_nid, HDA_INPUT))
2860 *mix_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2861 if (nid_has_mute(codec, mix_nid, HDA_INPUT))
2862 *mute_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2863 if (*mix_val && *mute_val)
2864 return true;
2865
2866 /* check leaf node */
2867 num_conns = snd_hda_get_conn_list(codec, mix_nid, &list);
2868 if (num_conns < idx)
2869 return false;
2870 nid = list[idx];
Takashi Iwai43a8e502014-01-07 18:11:44 +01002871 if (!*mix_val && nid_has_volume(codec, nid, HDA_OUTPUT) &&
2872 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_VOL_CTL))
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002873 *mix_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
Takashi Iwai43a8e502014-01-07 18:11:44 +01002874 if (!*mute_val && nid_has_mute(codec, nid, HDA_OUTPUT) &&
2875 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_MUTE_CTL))
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002876 *mute_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2877
2878 return *mix_val || *mute_val;
2879}
2880
Takashi Iwai352f7f92012-12-19 12:52:06 +01002881/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01002882static int new_analog_input(struct hda_codec *codec, int input_idx,
2883 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002884 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002886 struct hda_gen_spec *spec = codec->spec;
2887 struct nid_path *path;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002888 unsigned int mix_val, mute_val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002889 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002891 if (!look_for_mix_leaf_ctls(codec, mix_nid, pin, &mix_val, &mute_val))
2892 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002893
Takashi Iwai3ca529d2013-01-07 17:25:08 +01002894 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002895 if (!path)
2896 return -EINVAL;
Takashi Iwai4e76a882014-02-25 12:21:03 +01002897 print_nid_path(codec, "loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01002898 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002899
2900 idx = path->idx[path->depth - 1];
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002901 if (mix_val) {
2902 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, mix_val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002903 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 return err;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002905 path->ctls[NID_PATH_VOL_CTL] = mix_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 }
2907
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002908 if (mute_val) {
2909 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, mute_val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002910 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 return err;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002912 path->ctls[NID_PATH_MUTE_CTL] = mute_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 }
2914
Takashi Iwai352f7f92012-12-19 12:52:06 +01002915 path->active = true;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002916 err = add_loopback_list(spec, mix_nid, idx);
2917 if (err < 0)
2918 return err;
Takashi Iwaie4a395e2013-01-23 17:00:31 +01002919
2920 if (spec->mixer_nid != spec->mixer_merge_nid &&
2921 !spec->loopback_merge_path) {
2922 path = snd_hda_add_new_path(codec, spec->mixer_nid,
2923 spec->mixer_merge_nid, 0);
2924 if (path) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002925 print_nid_path(codec, "loopback-merge", path);
Takashi Iwaie4a395e2013-01-23 17:00:31 +01002926 path->active = true;
2927 spec->loopback_merge_path =
2928 snd_hda_get_path_idx(codec, path);
2929 }
2930 }
2931
Takashi Iwai352f7f92012-12-19 12:52:06 +01002932 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933}
2934
Takashi Iwai352f7f92012-12-19 12:52:06 +01002935static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002937 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
2938 return (pincap & AC_PINCAP_IN) != 0;
2939}
2940
2941/* Parse the codec tree and retrieve ADCs */
2942static int fill_adc_nids(struct hda_codec *codec)
2943{
2944 struct hda_gen_spec *spec = codec->spec;
2945 hda_nid_t nid;
2946 hda_nid_t *adc_nids = spec->adc_nids;
2947 int max_nums = ARRAY_SIZE(spec->adc_nids);
2948 int i, nums = 0;
2949
2950 nid = codec->start_nid;
2951 for (i = 0; i < codec->num_nodes; i++, nid++) {
2952 unsigned int caps = get_wcaps(codec, nid);
2953 int type = get_wcaps_type(caps);
2954
2955 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2956 continue;
2957 adc_nids[nums] = nid;
2958 if (++nums >= max_nums)
2959 break;
2960 }
2961 spec->num_adc_nids = nums;
Takashi Iwai0ffd5342013-01-17 15:53:29 +01002962
2963 /* copy the detected ADCs to all_adcs[] */
2964 spec->num_all_adcs = nums;
2965 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
2966
Takashi Iwai352f7f92012-12-19 12:52:06 +01002967 return nums;
2968}
2969
2970/* filter out invalid adc_nids that don't give all active input pins;
2971 * if needed, check whether dynamic ADC-switching is available
2972 */
2973static int check_dyn_adc_switch(struct hda_codec *codec)
2974{
2975 struct hda_gen_spec *spec = codec->spec;
2976 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002977 unsigned int ok_bits;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002978 int i, n, nums;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002979
Takashi Iwai352f7f92012-12-19 12:52:06 +01002980 nums = 0;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002981 ok_bits = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002982 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002983 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002984 if (!spec->input_paths[i][n])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002985 break;
2986 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002987 if (i >= imux->num_items) {
2988 ok_bits |= (1 << n);
2989 nums++;
2990 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002991 }
2992
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002993 if (!ok_bits) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002994 /* check whether ADC-switch is possible */
2995 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002996 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002997 if (spec->input_paths[i][n]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002998 spec->dyn_adc_idx[i] = n;
2999 break;
3000 }
3001 }
3002 }
3003
Takashi Iwai4e76a882014-02-25 12:21:03 +01003004 codec_dbg(codec, "enabling ADC switching\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01003005 spec->dyn_adc_switch = 1;
3006 } else if (nums != spec->num_adc_nids) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003007 /* shrink the invalid adcs and input paths */
3008 nums = 0;
3009 for (n = 0; n < spec->num_adc_nids; n++) {
3010 if (!(ok_bits & (1 << n)))
3011 continue;
3012 if (n != nums) {
3013 spec->adc_nids[nums] = spec->adc_nids[n];
Takashi Iwai980428c2013-01-09 09:28:20 +01003014 for (i = 0; i < imux->num_items; i++) {
3015 invalidate_nid_path(codec,
3016 spec->input_paths[i][nums]);
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003017 spec->input_paths[i][nums] =
3018 spec->input_paths[i][n];
Takashi Iwai980428c2013-01-09 09:28:20 +01003019 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003020 }
3021 nums++;
3022 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003023 spec->num_adc_nids = nums;
3024 }
3025
Takashi Iwai967303d2013-02-19 17:12:42 +01003026 if (imux->num_items == 1 ||
3027 (imux->num_items == 2 && spec->hp_mic)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003028 codec_dbg(codec, "reducing to a single ADC\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01003029 spec->num_adc_nids = 1; /* reduce to a single ADC */
3030 }
3031
3032 /* single index for individual volumes ctls */
3033 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
3034 spec->num_adc_nids = 1;
3035
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 return 0;
3037}
3038
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003039/* parse capture source paths from the given pin and create imux items */
3040static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
Takashi Iwai9dba2052013-01-18 10:01:15 +01003041 int cfg_idx, int num_adcs,
3042 const char *label, int anchor)
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003043{
3044 struct hda_gen_spec *spec = codec->spec;
3045 struct hda_input_mux *imux = &spec->input_mux;
3046 int imux_idx = imux->num_items;
3047 bool imux_added = false;
3048 int c;
3049
3050 for (c = 0; c < num_adcs; c++) {
3051 struct nid_path *path;
3052 hda_nid_t adc = spec->adc_nids[c];
3053
3054 if (!is_reachable_path(codec, pin, adc))
3055 continue;
3056 path = snd_hda_add_new_path(codec, pin, adc, anchor);
3057 if (!path)
3058 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01003059 print_nid_path(codec, "input", path);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003060 spec->input_paths[imux_idx][c] =
3061 snd_hda_get_path_idx(codec, path);
3062
3063 if (!imux_added) {
Takashi Iwai967303d2013-02-19 17:12:42 +01003064 if (spec->hp_mic_pin == pin)
3065 spec->hp_mic_mux_idx = imux->num_items;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003066 spec->imux_pins[imux->num_items] = pin;
Takashi Iwai6194b992014-06-06 18:12:16 +02003067 snd_hda_add_imux_item(codec, imux, label, cfg_idx, NULL);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003068 imux_added = true;
Takashi Iwaif1e762d2013-12-09 16:02:24 +01003069 if (spec->dyn_adc_switch)
3070 spec->dyn_adc_idx[imux_idx] = c;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003071 }
3072 }
3073
3074 return 0;
3075}
3076
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003078 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 */
Takashi Iwai9dba2052013-01-18 10:01:15 +01003080
Takashi Iwaic9700422013-01-18 10:17:30 +01003081/* fill the label for each input at first */
3082static int fill_input_pin_labels(struct hda_codec *codec)
3083{
3084 struct hda_gen_spec *spec = codec->spec;
3085 const struct auto_pin_cfg *cfg = &spec->autocfg;
3086 int i;
3087
3088 for (i = 0; i < cfg->num_inputs; i++) {
3089 hda_nid_t pin = cfg->inputs[i].pin;
3090 const char *label;
3091 int j, idx;
3092
3093 if (!is_input_pin(codec, pin))
3094 continue;
3095
3096 label = hda_get_autocfg_input_label(codec, cfg, i);
3097 idx = 0;
David Henningsson8e8db7f2013-01-18 15:43:02 +01003098 for (j = i - 1; j >= 0; j--) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003099 if (spec->input_labels[j] &&
3100 !strcmp(spec->input_labels[j], label)) {
3101 idx = spec->input_label_idxs[j] + 1;
3102 break;
3103 }
3104 }
3105
3106 spec->input_labels[i] = label;
3107 spec->input_label_idxs[i] = idx;
3108 }
3109
3110 return 0;
3111}
3112
Takashi Iwai9dba2052013-01-18 10:01:15 +01003113#define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
3114
Takashi Iwai352f7f92012-12-19 12:52:06 +01003115static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003116{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003117 struct hda_gen_spec *spec = codec->spec;
3118 const struct auto_pin_cfg *cfg = &spec->autocfg;
3119 hda_nid_t mixer = spec->mixer_nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003120 int num_adcs;
Takashi Iwaic9700422013-01-18 10:17:30 +01003121 int i, err;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003122 unsigned int val;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003123
Takashi Iwai352f7f92012-12-19 12:52:06 +01003124 num_adcs = fill_adc_nids(codec);
3125 if (num_adcs < 0)
3126 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003127
Takashi Iwaic9700422013-01-18 10:17:30 +01003128 err = fill_input_pin_labels(codec);
3129 if (err < 0)
3130 return err;
3131
Takashi Iwai352f7f92012-12-19 12:52:06 +01003132 for (i = 0; i < cfg->num_inputs; i++) {
3133 hda_nid_t pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134
Takashi Iwai352f7f92012-12-19 12:52:06 +01003135 pin = cfg->inputs[i].pin;
3136 if (!is_input_pin(codec, pin))
3137 continue;
3138
Takashi Iwai2c12c302013-01-10 09:33:29 +01003139 val = PIN_IN;
3140 if (cfg->inputs[i].type == AUTO_PIN_MIC)
3141 val |= snd_hda_get_default_vref(codec, pin);
Takashi Iwai93c9d8a2013-03-11 09:48:43 +01003142 if (pin != spec->hp_mic_pin)
3143 set_pin_target(codec, pin, val, false);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003144
Takashi Iwai352f7f92012-12-19 12:52:06 +01003145 if (mixer) {
3146 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01003147 err = new_analog_input(codec, i, pin,
Takashi Iwaic9700422013-01-18 10:17:30 +01003148 spec->input_labels[i],
3149 spec->input_label_idxs[i],
3150 mixer);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003151 if (err < 0)
3152 return err;
3153 }
3154 }
3155
Takashi Iwaic9700422013-01-18 10:17:30 +01003156 err = parse_capture_source(codec, pin, i, num_adcs,
3157 spec->input_labels[i], -mixer);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003158 if (err < 0)
3159 return err;
Takashi Iwai294765582013-01-17 09:52:11 +01003160
Takashi Iwaif811c3c2013-03-07 18:32:59 +01003161 if (spec->add_jack_modes) {
Takashi Iwai294765582013-01-17 09:52:11 +01003162 err = create_in_jack_mode(codec, pin);
3163 if (err < 0)
3164 return err;
3165 }
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003166 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003167
Takashi Iwaif1e762d2013-12-09 16:02:24 +01003168 /* add stereo mix when explicitly enabled via hint */
3169 if (mixer && spec->add_stereo_mix_input &&
3170 snd_hda_get_bool_hint(codec, "add_stereo_mix_input") > 0) {
Takashi Iwai9dba2052013-01-18 10:01:15 +01003171 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003172 "Stereo Mix", 0);
3173 if (err < 0)
3174 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003175 }
3176
3177 return 0;
3178}
3179
3180
3181/*
3182 * input source mux
3183 */
3184
Takashi Iwaic697b712013-01-07 17:09:26 +01003185/* get the input path specified by the given adc and imux indices */
3186static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003187{
3188 struct hda_gen_spec *spec = codec->spec;
David Henningssonb56fa1e2013-01-16 11:45:35 +01003189 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3190 snd_BUG();
3191 return NULL;
3192 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003193 if (spec->dyn_adc_switch)
3194 adc_idx = spec->dyn_adc_idx[imux_idx];
David Henningssond3d982f2013-01-18 15:43:01 +01003195 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
David Henningssonb56fa1e2013-01-16 11:45:35 +01003196 snd_BUG();
3197 return NULL;
3198 }
Takashi Iwaic697b712013-01-07 17:09:26 +01003199 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003200}
3201
3202static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3203 unsigned int idx);
3204
3205static int mux_enum_info(struct snd_kcontrol *kcontrol,
3206 struct snd_ctl_elem_info *uinfo)
3207{
3208 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3209 struct hda_gen_spec *spec = codec->spec;
3210 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3211}
3212
3213static int mux_enum_get(struct snd_kcontrol *kcontrol,
3214 struct snd_ctl_elem_value *ucontrol)
3215{
3216 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3217 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003218 /* the ctls are created at once with multiple counts */
3219 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003220
3221 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3222 return 0;
3223}
3224
3225static int mux_enum_put(struct snd_kcontrol *kcontrol,
3226 struct snd_ctl_elem_value *ucontrol)
3227{
3228 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003229 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003230 return mux_select(codec, adc_idx,
3231 ucontrol->value.enumerated.item[0]);
3232}
3233
Takashi Iwai352f7f92012-12-19 12:52:06 +01003234static const struct snd_kcontrol_new cap_src_temp = {
3235 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3236 .name = "Input Source",
3237 .info = mux_enum_info,
3238 .get = mux_enum_get,
3239 .put = mux_enum_put,
3240};
3241
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003242/*
3243 * capture volume and capture switch ctls
3244 */
3245
Takashi Iwai352f7f92012-12-19 12:52:06 +01003246typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3247 struct snd_ctl_elem_value *ucontrol);
3248
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003249/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003250static int cap_put_caller(struct snd_kcontrol *kcontrol,
3251 struct snd_ctl_elem_value *ucontrol,
3252 put_call_t func, int type)
3253{
3254 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3255 struct hda_gen_spec *spec = codec->spec;
3256 const struct hda_input_mux *imux;
3257 struct nid_path *path;
3258 int i, adc_idx, err = 0;
3259
3260 imux = &spec->input_mux;
David Henningssona053d1e2013-01-16 11:45:36 +01003261 adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003262 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003263 /* we use the cache-only update at first since multiple input paths
3264 * may shared the same amp; by updating only caches, the redundant
3265 * writes to hardware can be reduced.
3266 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003267 codec->cached_write = 1;
3268 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003269 path = get_input_path(codec, adc_idx, i);
3270 if (!path || !path->ctls[type])
Takashi Iwai352f7f92012-12-19 12:52:06 +01003271 continue;
3272 kcontrol->private_value = path->ctls[type];
3273 err = func(kcontrol, ucontrol);
3274 if (err < 0)
3275 goto error;
3276 }
3277 error:
3278 codec->cached_write = 0;
3279 mutex_unlock(&codec->control_mutex);
Takashi Iwaidc870f32013-01-22 15:24:30 +01003280 snd_hda_codec_flush_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003281 if (err >= 0 && spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01003282 spec->cap_sync_hook(codec, kcontrol, ucontrol);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003283 return err;
3284}
3285
3286/* capture volume ctl callbacks */
3287#define cap_vol_info snd_hda_mixer_amp_volume_info
3288#define cap_vol_get snd_hda_mixer_amp_volume_get
3289#define cap_vol_tlv snd_hda_mixer_amp_tlv
3290
3291static int cap_vol_put(struct snd_kcontrol *kcontrol,
3292 struct snd_ctl_elem_value *ucontrol)
3293{
3294 return cap_put_caller(kcontrol, ucontrol,
3295 snd_hda_mixer_amp_volume_put,
3296 NID_PATH_VOL_CTL);
3297}
3298
3299static const struct snd_kcontrol_new cap_vol_temp = {
3300 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3301 .name = "Capture Volume",
3302 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3303 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3304 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3305 .info = cap_vol_info,
3306 .get = cap_vol_get,
3307 .put = cap_vol_put,
3308 .tlv = { .c = cap_vol_tlv },
3309};
3310
3311/* capture switch ctl callbacks */
3312#define cap_sw_info snd_ctl_boolean_stereo_info
3313#define cap_sw_get snd_hda_mixer_amp_switch_get
3314
3315static int cap_sw_put(struct snd_kcontrol *kcontrol,
3316 struct snd_ctl_elem_value *ucontrol)
3317{
Takashi Iwaia90229e2013-01-18 14:10:00 +01003318 return cap_put_caller(kcontrol, ucontrol,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003319 snd_hda_mixer_amp_switch_put,
3320 NID_PATH_MUTE_CTL);
3321}
3322
3323static const struct snd_kcontrol_new cap_sw_temp = {
3324 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3325 .name = "Capture Switch",
3326 .info = cap_sw_info,
3327 .get = cap_sw_get,
3328 .put = cap_sw_put,
3329};
3330
3331static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3332{
3333 hda_nid_t nid;
3334 int i, depth;
3335
3336 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3337 for (depth = 0; depth < 3; depth++) {
3338 if (depth >= path->depth)
3339 return -EINVAL;
3340 i = path->depth - depth - 1;
3341 nid = path->path[i];
3342 if (!path->ctls[NID_PATH_VOL_CTL]) {
3343 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3344 path->ctls[NID_PATH_VOL_CTL] =
3345 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3346 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3347 int idx = path->idx[i];
3348 if (!depth && codec->single_adc_amp)
3349 idx = 0;
3350 path->ctls[NID_PATH_VOL_CTL] =
3351 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3352 }
3353 }
3354 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3355 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3356 path->ctls[NID_PATH_MUTE_CTL] =
3357 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3358 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3359 int idx = path->idx[i];
3360 if (!depth && codec->single_adc_amp)
3361 idx = 0;
3362 path->ctls[NID_PATH_MUTE_CTL] =
3363 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3364 }
3365 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01003366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 return 0;
3368}
3369
Takashi Iwai352f7f92012-12-19 12:52:06 +01003370static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003372 struct hda_gen_spec *spec = codec->spec;
3373 struct auto_pin_cfg *cfg = &spec->autocfg;
3374 unsigned int val;
3375 int i;
3376
3377 if (!spec->inv_dmic_split)
3378 return false;
3379 for (i = 0; i < cfg->num_inputs; i++) {
3380 if (cfg->inputs[i].pin != nid)
3381 continue;
3382 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3383 return false;
3384 val = snd_hda_codec_get_pincfg(codec, nid);
3385 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3386 }
3387 return false;
3388}
3389
Takashi Iwaia90229e2013-01-18 14:10:00 +01003390/* capture switch put callback for a single control with hook call */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003391static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3392 struct snd_ctl_elem_value *ucontrol)
3393{
3394 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3395 struct hda_gen_spec *spec = codec->spec;
3396 int ret;
3397
3398 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3399 if (ret < 0)
3400 return ret;
3401
Takashi Iwaia90229e2013-01-18 14:10:00 +01003402 if (spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01003403 spec->cap_sync_hook(codec, kcontrol, ucontrol);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003404
3405 return ret;
3406}
3407
Takashi Iwai352f7f92012-12-19 12:52:06 +01003408static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3409 int idx, bool is_switch, unsigned int ctl,
3410 bool inv_dmic)
3411{
3412 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai975cc022013-06-28 11:56:49 +02003413 char tmpname[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003414 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3415 const char *sfx = is_switch ? "Switch" : "Volume";
3416 unsigned int chs = inv_dmic ? 1 : 3;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003417 struct snd_kcontrol_new *knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003418
3419 if (!ctl)
3420 return 0;
3421
3422 if (label)
3423 snprintf(tmpname, sizeof(tmpname),
3424 "%s Capture %s", label, sfx);
3425 else
3426 snprintf(tmpname, sizeof(tmpname),
3427 "Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003428 knew = add_control(spec, type, tmpname, idx,
3429 amp_val_replace_channels(ctl, chs));
3430 if (!knew)
3431 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003432 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003433 knew->put = cap_single_sw_put;
3434 if (!inv_dmic)
3435 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003436
3437 /* Make independent right kcontrol */
3438 if (label)
3439 snprintf(tmpname, sizeof(tmpname),
3440 "Inverted %s Capture %s", label, sfx);
3441 else
3442 snprintf(tmpname, sizeof(tmpname),
3443 "Inverted Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003444 knew = add_control(spec, type, tmpname, idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003445 amp_val_replace_channels(ctl, 2));
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003446 if (!knew)
3447 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003448 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003449 knew->put = cap_single_sw_put;
3450 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003451}
3452
3453/* create single (and simple) capture volume and switch controls */
3454static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3455 unsigned int vol_ctl, unsigned int sw_ctl,
3456 bool inv_dmic)
3457{
3458 int err;
3459 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3460 if (err < 0)
3461 return err;
3462 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3463 if (err < 0)
3464 return err;
3465 return 0;
3466}
3467
3468/* create bound capture volume and switch controls */
3469static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3470 unsigned int vol_ctl, unsigned int sw_ctl)
3471{
3472 struct hda_gen_spec *spec = codec->spec;
3473 struct snd_kcontrol_new *knew;
3474
3475 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003476 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003477 if (!knew)
3478 return -ENOMEM;
3479 knew->index = idx;
3480 knew->private_value = vol_ctl;
3481 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3482 }
3483 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003484 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003485 if (!knew)
3486 return -ENOMEM;
3487 knew->index = idx;
3488 knew->private_value = sw_ctl;
3489 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3490 }
3491 return 0;
3492}
3493
3494/* return the vol ctl when used first in the imux list */
3495static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3496{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003497 struct nid_path *path;
3498 unsigned int ctl;
3499 int i;
3500
Takashi Iwaic697b712013-01-07 17:09:26 +01003501 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003502 if (!path)
3503 return 0;
3504 ctl = path->ctls[type];
3505 if (!ctl)
3506 return 0;
3507 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003508 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003509 if (path && path->ctls[type] == ctl)
3510 return 0;
3511 }
3512 return ctl;
3513}
3514
3515/* create individual capture volume and switch controls per input */
3516static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3517{
3518 struct hda_gen_spec *spec = codec->spec;
3519 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaic9700422013-01-18 10:17:30 +01003520 int i, err, type;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003521
3522 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003523 bool inv_dmic;
Takashi Iwaic9700422013-01-18 10:17:30 +01003524 int idx;
Takashi Iwai9dba2052013-01-18 10:01:15 +01003525
Takashi Iwaic9700422013-01-18 10:17:30 +01003526 idx = imux->items[i].index;
3527 if (idx >= spec->autocfg.num_inputs)
Takashi Iwai9dba2052013-01-18 10:01:15 +01003528 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003529 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3530
3531 for (type = 0; type < 2; type++) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003532 err = add_single_cap_ctl(codec,
3533 spec->input_labels[idx],
3534 spec->input_label_idxs[idx],
3535 type,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003536 get_first_cap_ctl(codec, i, type),
3537 inv_dmic);
3538 if (err < 0)
3539 return err;
3540 }
3541 }
3542 return 0;
3543}
3544
3545static int create_capture_mixers(struct hda_codec *codec)
3546{
3547 struct hda_gen_spec *spec = codec->spec;
3548 struct hda_input_mux *imux = &spec->input_mux;
3549 int i, n, nums, err;
3550
3551 if (spec->dyn_adc_switch)
3552 nums = 1;
3553 else
3554 nums = spec->num_adc_nids;
3555
3556 if (!spec->auto_mic && imux->num_items > 1) {
3557 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01003558 const char *name;
3559 name = nums > 1 ? "Input Source" : "Capture Source";
3560 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003561 if (!knew)
3562 return -ENOMEM;
3563 knew->count = nums;
3564 }
3565
3566 for (n = 0; n < nums; n++) {
3567 bool multi = false;
David Henningsson99a55922013-01-16 15:58:44 +01003568 bool multi_cap_vol = spec->multi_cap_vol;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003569 bool inv_dmic = false;
3570 int vol, sw;
3571
3572 vol = sw = 0;
3573 for (i = 0; i < imux->num_items; i++) {
3574 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01003575 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003576 if (!path)
3577 continue;
3578 parse_capvol_in_path(codec, path);
3579 if (!vol)
3580 vol = path->ctls[NID_PATH_VOL_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003581 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003582 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003583 if (!same_amp_caps(codec, vol,
3584 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3585 multi_cap_vol = true;
3586 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003587 if (!sw)
3588 sw = path->ctls[NID_PATH_MUTE_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003589 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003590 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003591 if (!same_amp_caps(codec, sw,
3592 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3593 multi_cap_vol = true;
3594 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003595 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3596 inv_dmic = true;
3597 }
3598
3599 if (!multi)
3600 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3601 inv_dmic);
David Henningssonccb04152013-10-14 10:16:22 +02003602 else if (!multi_cap_vol && !inv_dmic)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003603 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3604 else
3605 err = create_multi_cap_vol_ctl(codec);
3606 if (err < 0)
3607 return err;
3608 }
3609
3610 return 0;
3611}
3612
3613/*
3614 * add mic boosts if needed
3615 */
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003616
3617/* check whether the given amp is feasible as a boost volume */
3618static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3619 int dir, int idx)
3620{
3621 unsigned int step;
3622
3623 if (!nid_has_volume(codec, nid, dir) ||
3624 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3625 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3626 return false;
3627
3628 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3629 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3630 if (step < 0x20)
3631 return false;
3632 return true;
3633}
3634
3635/* look for a boost amp in a widget close to the pin */
3636static unsigned int look_for_boost_amp(struct hda_codec *codec,
3637 struct nid_path *path)
3638{
3639 unsigned int val = 0;
3640 hda_nid_t nid;
3641 int depth;
3642
3643 for (depth = 0; depth < 3; depth++) {
3644 if (depth >= path->depth - 1)
3645 break;
3646 nid = path->path[depth];
3647 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3648 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3649 break;
3650 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3651 path->idx[depth])) {
3652 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3653 HDA_INPUT);
3654 break;
3655 }
3656 }
3657
3658 return val;
3659}
3660
Takashi Iwai352f7f92012-12-19 12:52:06 +01003661static int parse_mic_boost(struct hda_codec *codec)
3662{
3663 struct hda_gen_spec *spec = codec->spec;
3664 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003665 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003666 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003667
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003668 if (!spec->num_adc_nids)
3669 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003670
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003671 for (i = 0; i < imux->num_items; i++) {
3672 struct nid_path *path;
3673 unsigned int val;
3674 int idx;
Takashi Iwai975cc022013-06-28 11:56:49 +02003675 char boost_label[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
David Henningsson02aba552013-01-16 15:58:43 +01003676
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003677 idx = imux->items[i].index;
3678 if (idx >= imux->num_items)
3679 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003680
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003681 /* check only line-in and mic pins */
Takashi Iwai1799cdd52013-01-18 14:37:16 +01003682 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003683 continue;
3684
3685 path = get_input_path(codec, 0, i);
3686 if (!path)
3687 continue;
3688
3689 val = look_for_boost_amp(codec, path);
3690 if (!val)
3691 continue;
3692
3693 /* create a boost control */
3694 snprintf(boost_label, sizeof(boost_label),
3695 "%s Boost Volume", spec->input_labels[idx]);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003696 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3697 spec->input_label_idxs[idx], val))
3698 return -ENOMEM;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003699
3700 path->ctls[NID_PATH_BOOST_CTL] = val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003701 }
3702 return 0;
3703}
3704
3705/*
3706 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3707 */
3708static void parse_digital(struct hda_codec *codec)
3709{
3710 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003711 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003712 int i, nums;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003713 hda_nid_t dig_nid, pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003714
3715 /* support multiple SPDIFs; the secondary is set up as a slave */
3716 nums = 0;
3717 for (i = 0; i < spec->autocfg.dig_outs; i++) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003718 pin = spec->autocfg.dig_out_pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003719 dig_nid = look_for_dac(codec, pin, true);
3720 if (!dig_nid)
3721 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003722 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003723 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003724 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01003725 print_nid_path(codec, "digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01003726 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01003727 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003728 set_pin_target(codec, pin, PIN_OUT, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003729 if (!nums) {
3730 spec->multiout.dig_out_nid = dig_nid;
3731 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3732 } else {
3733 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3734 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
Dan Carpenterd5764222014-05-14 17:18:31 +03003735 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003736 spec->slave_dig_outs[nums - 1] = dig_nid;
3737 }
3738 nums++;
3739 }
3740
3741 if (spec->autocfg.dig_in_pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003742 pin = spec->autocfg.dig_in_pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003743 dig_nid = codec->start_nid;
3744 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003745 unsigned int wcaps = get_wcaps(codec, dig_nid);
3746 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3747 continue;
3748 if (!(wcaps & AC_WCAP_DIGITAL))
3749 continue;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003750 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003751 if (path) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003752 print_nid_path(codec, "digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003753 path->active = true;
3754 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003755 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003756 set_pin_target(codec, pin, PIN_IN, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003757 break;
3758 }
3759 }
3760 }
3761}
3762
3763
3764/*
3765 * input MUX handling
3766 */
3767
3768static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3769
3770/* select the given imux item; either unmute exclusively or select the route */
3771static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3772 unsigned int idx)
3773{
3774 struct hda_gen_spec *spec = codec->spec;
3775 const struct hda_input_mux *imux;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003776 struct nid_path *old_path, *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003777
3778 imux = &spec->input_mux;
3779 if (!imux->num_items)
3780 return 0;
3781
3782 if (idx >= imux->num_items)
3783 idx = imux->num_items - 1;
3784 if (spec->cur_mux[adc_idx] == idx)
3785 return 0;
3786
Takashi Iwai55196ff2013-01-24 17:32:56 +01003787 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3788 if (!old_path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003789 return 0;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003790 if (old_path->active)
3791 snd_hda_activate_path(codec, old_path, false, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003792
3793 spec->cur_mux[adc_idx] = idx;
3794
Takashi Iwai967303d2013-02-19 17:12:42 +01003795 if (spec->hp_mic)
3796 update_hp_mic(codec, adc_idx, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003797
3798 if (spec->dyn_adc_switch)
3799 dyn_adc_pcm_resetup(codec, idx);
3800
Takashi Iwaic697b712013-01-07 17:09:26 +01003801 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003802 if (!path)
3803 return 0;
3804 if (path->active)
3805 return 0;
3806 snd_hda_activate_path(codec, path, true, false);
3807 if (spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01003808 spec->cap_sync_hook(codec, NULL, NULL);
Takashi Iwai55196ff2013-01-24 17:32:56 +01003809 path_power_down_sync(codec, old_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003810 return 1;
3811}
3812
3813
3814/*
3815 * Jack detections for HP auto-mute and mic-switch
3816 */
3817
3818/* check each pin in the given array; returns true if any of them is plugged */
3819static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
3820{
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02003821 int i;
3822 bool present = false;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003823
3824 for (i = 0; i < num_pins; i++) {
3825 hda_nid_t nid = pins[i];
3826 if (!nid)
3827 break;
Takashi Iwai0b4df932013-01-10 09:45:13 +01003828 /* don't detect pins retasked as inputs */
3829 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
3830 continue;
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02003831 if (snd_hda_jack_detect_state(codec, nid) == HDA_JACK_PRESENT)
3832 present = true;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003833 }
3834 return present;
3835}
3836
3837/* standard HP/line-out auto-mute helper */
3838static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003839 int *paths, bool mute)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003840{
3841 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003842 int i;
3843
3844 for (i = 0; i < num_pins; i++) {
3845 hda_nid_t nid = pins[i];
Takashi Iwai967303d2013-02-19 17:12:42 +01003846 unsigned int val, oldval;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003847 if (!nid)
3848 break;
Takashi Iwai7eebffd2013-06-24 16:00:21 +02003849
3850 if (spec->auto_mute_via_amp) {
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003851 struct nid_path *path;
3852 hda_nid_t mute_nid;
3853
3854 path = snd_hda_get_path_from_idx(codec, paths[i]);
3855 if (!path)
3856 continue;
3857 mute_nid = get_amp_nid_(path->ctls[NID_PATH_MUTE_CTL]);
3858 if (!mute_nid)
3859 continue;
Takashi Iwai7eebffd2013-06-24 16:00:21 +02003860 if (mute)
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003861 spec->mute_bits |= (1ULL << mute_nid);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02003862 else
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003863 spec->mute_bits &= ~(1ULL << mute_nid);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02003864 set_pin_eapd(codec, nid, !mute);
3865 continue;
3866 }
3867
Takashi Iwai967303d2013-02-19 17:12:42 +01003868 oldval = snd_hda_codec_get_pin_target(codec, nid);
3869 if (oldval & PIN_IN)
3870 continue; /* no mute for inputs */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003871 /* don't reset VREF value in case it's controlling
3872 * the amp (see alc861_fixup_asus_amp_vref_0f())
3873 */
Takashi Iwai2c12c302013-01-10 09:33:29 +01003874 if (spec->keep_vref_in_automute)
Takashi Iwai967303d2013-02-19 17:12:42 +01003875 val = oldval & ~PIN_HP;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003876 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01003877 val = 0;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003878 if (!mute)
Takashi Iwai967303d2013-02-19 17:12:42 +01003879 val |= oldval;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003880 /* here we call update_pin_ctl() so that the pinctl is changed
3881 * without changing the pinctl target value;
3882 * the original target value will be still referred at the
3883 * init / resume again
3884 */
3885 update_pin_ctl(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01003886 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003887 }
3888}
3889
3890/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003891void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003892{
3893 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003894 int *paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003895 int on;
3896
3897 /* Control HP pins/amps depending on master_mute state;
3898 * in general, HP pins/amps control should be enabled in all cases,
3899 * but currently set only for master_mute, just to be safe
3900 */
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003901 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
3902 paths = spec->out_paths;
3903 else
3904 paths = spec->hp_paths;
Takashi Iwai967303d2013-02-19 17:12:42 +01003905 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003906 spec->autocfg.hp_pins, paths, spec->master_mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003907
3908 if (!spec->automute_speaker)
3909 on = 0;
3910 else
3911 on = spec->hp_jack_present | spec->line_jack_present;
3912 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003913 spec->speaker_muted = on;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003914 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3915 paths = spec->out_paths;
3916 else
3917 paths = spec->speaker_paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003918 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003919 spec->autocfg.speaker_pins, paths, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003920
3921 /* toggle line-out mutes if needed, too */
3922 /* if LO is a copy of either HP or Speaker, don't need to handle it */
3923 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
3924 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
3925 return;
3926 if (!spec->automute_lo)
3927 on = 0;
3928 else
3929 on = spec->hp_jack_present;
3930 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003931 spec->line_out_muted = on;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003932 paths = spec->out_paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003933 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003934 spec->autocfg.line_out_pins, paths, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003935}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003936EXPORT_SYMBOL_GPL(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003937
3938static void call_update_outputs(struct hda_codec *codec)
3939{
3940 struct hda_gen_spec *spec = codec->spec;
3941 if (spec->automute_hook)
3942 spec->automute_hook(codec);
3943 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01003944 snd_hda_gen_update_outputs(codec);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02003945
3946 /* sync the whole vmaster slaves to reflect the new auto-mute status */
3947 if (spec->auto_mute_via_amp && !codec->bus->shutdown)
3948 snd_ctl_sync_vmaster(spec->vmaster_mute.sw_kctl, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003949}
3950
3951/* standard HP-automute helper */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02003952void snd_hda_gen_hp_automute(struct hda_codec *codec,
3953 struct hda_jack_callback *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003954{
3955 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai92603c52013-01-22 07:46:31 +01003956 hda_nid_t *pins = spec->autocfg.hp_pins;
3957 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003958
Takashi Iwai92603c52013-01-22 07:46:31 +01003959 /* No detection for the first HP jack during indep-HP mode */
3960 if (spec->indep_hp_enabled) {
3961 pins++;
3962 num_pins--;
3963 }
3964
3965 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003966 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
3967 return;
3968 call_update_outputs(codec);
3969}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003970EXPORT_SYMBOL_GPL(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003971
3972/* standard line-out-automute helper */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02003973void snd_hda_gen_line_automute(struct hda_codec *codec,
3974 struct hda_jack_callback *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003975{
3976 struct hda_gen_spec *spec = codec->spec;
3977
3978 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3979 return;
3980 /* check LO jack only when it's different from HP */
3981 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
3982 return;
3983
3984 spec->line_jack_present =
3985 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3986 spec->autocfg.line_out_pins);
3987 if (!spec->automute_speaker || !spec->detect_lo)
3988 return;
3989 call_update_outputs(codec);
3990}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003991EXPORT_SYMBOL_GPL(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003992
3993/* standard mic auto-switch helper */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02003994void snd_hda_gen_mic_autoswitch(struct hda_codec *codec,
3995 struct hda_jack_callback *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003996{
3997 struct hda_gen_spec *spec = codec->spec;
3998 int i;
3999
4000 if (!spec->auto_mic)
4001 return;
4002
4003 for (i = spec->am_num_entries - 1; i > 0; i--) {
Takashi Iwai0b4df932013-01-10 09:45:13 +01004004 hda_nid_t pin = spec->am_entry[i].pin;
4005 /* don't detect pins retasked as outputs */
4006 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
4007 continue;
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02004008 if (snd_hda_jack_detect_state(codec, pin) == HDA_JACK_PRESENT) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004009 mux_select(codec, 0, spec->am_entry[i].idx);
4010 return;
4011 }
4012 }
4013 mux_select(codec, 0, spec->am_entry[0].idx);
4014}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004015EXPORT_SYMBOL_GPL(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004016
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004017/* call appropriate hooks */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004018static void call_hp_automute(struct hda_codec *codec,
4019 struct hda_jack_callback *jack)
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004020{
4021 struct hda_gen_spec *spec = codec->spec;
4022 if (spec->hp_automute_hook)
4023 spec->hp_automute_hook(codec, jack);
4024 else
4025 snd_hda_gen_hp_automute(codec, jack);
4026}
4027
4028static void call_line_automute(struct hda_codec *codec,
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004029 struct hda_jack_callback *jack)
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004030{
4031 struct hda_gen_spec *spec = codec->spec;
4032 if (spec->line_automute_hook)
4033 spec->line_automute_hook(codec, jack);
4034 else
4035 snd_hda_gen_line_automute(codec, jack);
4036}
4037
4038static void call_mic_autoswitch(struct hda_codec *codec,
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004039 struct hda_jack_callback *jack)
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004040{
4041 struct hda_gen_spec *spec = codec->spec;
4042 if (spec->mic_autoswitch_hook)
4043 spec->mic_autoswitch_hook(codec, jack);
4044 else
4045 snd_hda_gen_mic_autoswitch(codec, jack);
4046}
4047
Takashi Iwai963afde2013-05-31 15:20:31 +02004048/* update jack retasking */
4049static void update_automute_all(struct hda_codec *codec)
4050{
4051 call_hp_automute(codec, NULL);
4052 call_line_automute(codec, NULL);
4053 call_mic_autoswitch(codec, NULL);
4054}
4055
Takashi Iwai352f7f92012-12-19 12:52:06 +01004056/*
4057 * Auto-Mute mode mixer enum support
4058 */
4059static int automute_mode_info(struct snd_kcontrol *kcontrol,
4060 struct snd_ctl_elem_info *uinfo)
4061{
4062 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4063 struct hda_gen_spec *spec = codec->spec;
4064 static const char * const texts3[] = {
4065 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02004066 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067
Takashi Iwai352f7f92012-12-19 12:52:06 +01004068 if (spec->automute_speaker_possible && spec->automute_lo_possible)
4069 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
4070 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
4071}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072
Takashi Iwai352f7f92012-12-19 12:52:06 +01004073static int automute_mode_get(struct snd_kcontrol *kcontrol,
4074 struct snd_ctl_elem_value *ucontrol)
4075{
4076 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4077 struct hda_gen_spec *spec = codec->spec;
4078 unsigned int val = 0;
4079 if (spec->automute_speaker)
4080 val++;
4081 if (spec->automute_lo)
4082 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004083
Takashi Iwai352f7f92012-12-19 12:52:06 +01004084 ucontrol->value.enumerated.item[0] = val;
4085 return 0;
4086}
4087
4088static int automute_mode_put(struct snd_kcontrol *kcontrol,
4089 struct snd_ctl_elem_value *ucontrol)
4090{
4091 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4092 struct hda_gen_spec *spec = codec->spec;
4093
4094 switch (ucontrol->value.enumerated.item[0]) {
4095 case 0:
4096 if (!spec->automute_speaker && !spec->automute_lo)
4097 return 0;
4098 spec->automute_speaker = 0;
4099 spec->automute_lo = 0;
4100 break;
4101 case 1:
4102 if (spec->automute_speaker_possible) {
4103 if (!spec->automute_lo && spec->automute_speaker)
4104 return 0;
4105 spec->automute_speaker = 1;
4106 spec->automute_lo = 0;
4107 } else if (spec->automute_lo_possible) {
4108 if (spec->automute_lo)
4109 return 0;
4110 spec->automute_lo = 1;
4111 } else
4112 return -EINVAL;
4113 break;
4114 case 2:
4115 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
4116 return -EINVAL;
4117 if (spec->automute_speaker && spec->automute_lo)
4118 return 0;
4119 spec->automute_speaker = 1;
4120 spec->automute_lo = 1;
4121 break;
4122 default:
4123 return -EINVAL;
4124 }
4125 call_update_outputs(codec);
4126 return 1;
4127}
4128
4129static const struct snd_kcontrol_new automute_mode_enum = {
4130 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4131 .name = "Auto-Mute Mode",
4132 .info = automute_mode_info,
4133 .get = automute_mode_get,
4134 .put = automute_mode_put,
4135};
4136
4137static int add_automute_mode_enum(struct hda_codec *codec)
4138{
4139 struct hda_gen_spec *spec = codec->spec;
4140
Takashi Iwai12c93df2012-12-19 14:38:33 +01004141 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01004142 return -ENOMEM;
4143 return 0;
4144}
4145
4146/*
4147 * Check the availability of HP/line-out auto-mute;
4148 * Set up appropriately if really supported
4149 */
4150static int check_auto_mute_availability(struct hda_codec *codec)
4151{
4152 struct hda_gen_spec *spec = codec->spec;
4153 struct auto_pin_cfg *cfg = &spec->autocfg;
4154 int present = 0;
4155 int i, err;
4156
Takashi Iwaif72706b2013-01-16 18:20:07 +01004157 if (spec->suppress_auto_mute)
4158 return 0;
4159
Takashi Iwai352f7f92012-12-19 12:52:06 +01004160 if (cfg->hp_pins[0])
4161 present++;
4162 if (cfg->line_out_pins[0])
4163 present++;
4164 if (cfg->speaker_pins[0])
4165 present++;
4166 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02004167 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004168
4169 if (!cfg->speaker_pins[0] &&
4170 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
4171 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4172 sizeof(cfg->speaker_pins));
4173 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175
Takashi Iwai352f7f92012-12-19 12:52:06 +01004176 if (!cfg->hp_pins[0] &&
4177 cfg->line_out_type == AUTO_PIN_HP_OUT) {
4178 memcpy(cfg->hp_pins, cfg->line_out_pins,
4179 sizeof(cfg->hp_pins));
4180 cfg->hp_outs = cfg->line_outs;
4181 }
4182
4183 for (i = 0; i < cfg->hp_outs; i++) {
4184 hda_nid_t nid = cfg->hp_pins[i];
4185 if (!is_jack_detectable(codec, nid))
4186 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004187 codec_dbg(codec, "Enable HP auto-muting on NID 0x%x\n", nid);
Takashi Iwai62f949b2014-09-11 14:06:53 +02004188 snd_hda_jack_detect_enable_callback(codec, nid,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004189 call_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004190 spec->detect_hp = 1;
4191 }
4192
4193 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
4194 if (cfg->speaker_outs)
4195 for (i = 0; i < cfg->line_outs; i++) {
4196 hda_nid_t nid = cfg->line_out_pins[i];
4197 if (!is_jack_detectable(codec, nid))
4198 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004199 codec_dbg(codec, "Enable Line-Out auto-muting on NID 0x%x\n", nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004200 snd_hda_jack_detect_enable_callback(codec, nid,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004201 call_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004202 spec->detect_lo = 1;
4203 }
4204 spec->automute_lo_possible = spec->detect_hp;
4205 }
4206
4207 spec->automute_speaker_possible = cfg->speaker_outs &&
4208 (spec->detect_hp || spec->detect_lo);
4209
4210 spec->automute_lo = spec->automute_lo_possible;
4211 spec->automute_speaker = spec->automute_speaker_possible;
4212
4213 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
4214 /* create a control for automute mode */
4215 err = add_automute_mode_enum(codec);
4216 if (err < 0)
4217 return err;
4218 }
4219 return 0;
4220}
4221
Takashi Iwai352f7f92012-12-19 12:52:06 +01004222/* check whether all auto-mic pins are valid; setup indices if OK */
4223static bool auto_mic_check_imux(struct hda_codec *codec)
4224{
4225 struct hda_gen_spec *spec = codec->spec;
4226 const struct hda_input_mux *imux;
4227 int i;
4228
4229 imux = &spec->input_mux;
4230 for (i = 0; i < spec->am_num_entries; i++) {
4231 spec->am_entry[i].idx =
4232 find_idx_in_nid_list(spec->am_entry[i].pin,
4233 spec->imux_pins, imux->num_items);
4234 if (spec->am_entry[i].idx < 0)
4235 return false; /* no corresponding imux */
4236 }
4237
4238 /* we don't need the jack detection for the first pin */
4239 for (i = 1; i < spec->am_num_entries; i++)
4240 snd_hda_jack_detect_enable_callback(codec,
4241 spec->am_entry[i].pin,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004242 call_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004243 return true;
4244}
4245
4246static int compare_attr(const void *ap, const void *bp)
4247{
4248 const struct automic_entry *a = ap;
4249 const struct automic_entry *b = bp;
4250 return (int)(a->attr - b->attr);
4251}
4252
4253/*
4254 * Check the availability of auto-mic switch;
4255 * Set up if really supported
4256 */
4257static int check_auto_mic_availability(struct hda_codec *codec)
4258{
4259 struct hda_gen_spec *spec = codec->spec;
4260 struct auto_pin_cfg *cfg = &spec->autocfg;
4261 unsigned int types;
4262 int i, num_pins;
4263
Takashi Iwaid12daf62013-01-07 16:32:11 +01004264 if (spec->suppress_auto_mic)
4265 return 0;
4266
Takashi Iwai352f7f92012-12-19 12:52:06 +01004267 types = 0;
4268 num_pins = 0;
4269 for (i = 0; i < cfg->num_inputs; i++) {
4270 hda_nid_t nid = cfg->inputs[i].pin;
4271 unsigned int attr;
4272 attr = snd_hda_codec_get_pincfg(codec, nid);
4273 attr = snd_hda_get_input_pin_attr(attr);
4274 if (types & (1 << attr))
4275 return 0; /* already occupied */
4276 switch (attr) {
4277 case INPUT_PIN_ATTR_INT:
4278 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4279 return 0; /* invalid type */
4280 break;
4281 case INPUT_PIN_ATTR_UNUSED:
4282 return 0; /* invalid entry */
4283 default:
4284 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4285 return 0; /* invalid type */
4286 if (!spec->line_in_auto_switch &&
4287 cfg->inputs[i].type != AUTO_PIN_MIC)
4288 return 0; /* only mic is allowed */
4289 if (!is_jack_detectable(codec, nid))
4290 return 0; /* no unsol support */
4291 break;
4292 }
4293 if (num_pins >= MAX_AUTO_MIC_PINS)
4294 return 0;
4295 types |= (1 << attr);
4296 spec->am_entry[num_pins].pin = nid;
4297 spec->am_entry[num_pins].attr = attr;
4298 num_pins++;
4299 }
4300
4301 if (num_pins < 2)
4302 return 0;
4303
4304 spec->am_num_entries = num_pins;
4305 /* sort the am_entry in the order of attr so that the pin with a
4306 * higher attr will be selected when the jack is plugged.
4307 */
4308 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4309 compare_attr, NULL);
4310
4311 if (!auto_mic_check_imux(codec))
4312 return 0;
4313
4314 spec->auto_mic = 1;
4315 spec->num_adc_nids = 1;
4316 spec->cur_mux[0] = spec->am_entry[0].idx;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004317 codec_dbg(codec, "Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01004318 spec->am_entry[0].pin,
4319 spec->am_entry[1].pin,
4320 spec->am_entry[2].pin);
4321
4322 return 0;
4323}
4324
Takashi Iwai55196ff2013-01-24 17:32:56 +01004325/* power_filter hook; make inactive widgets into power down */
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004326unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
Takashi Iwai55196ff2013-01-24 17:32:56 +01004327 hda_nid_t nid,
4328 unsigned int power_state)
4329{
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004330 if (power_state != AC_PWRST_D0 || nid == codec->afg)
Takashi Iwai55196ff2013-01-24 17:32:56 +01004331 return power_state;
4332 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4333 return power_state;
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +02004334 if (is_active_nid_for_any(codec, nid))
Takashi Iwai55196ff2013-01-24 17:32:56 +01004335 return power_state;
4336 return AC_PWRST_D3;
4337}
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004338EXPORT_SYMBOL_GPL(snd_hda_gen_path_power_filter);
Takashi Iwai55196ff2013-01-24 17:32:56 +01004339
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004340/* mute all aamix inputs initially; parse up to the first leaves */
4341static void mute_all_mixer_nid(struct hda_codec *codec, hda_nid_t mix)
4342{
4343 int i, nums;
4344 const hda_nid_t *conn;
4345 bool has_amp;
4346
4347 nums = snd_hda_get_conn_list(codec, mix, &conn);
4348 has_amp = nid_has_mute(codec, mix, HDA_INPUT);
4349 for (i = 0; i < nums; i++) {
4350 if (has_amp)
4351 snd_hda_codec_amp_stereo(codec, mix,
4352 HDA_INPUT, i,
4353 0xff, HDA_AMP_MUTE);
4354 else if (nid_has_volume(codec, conn[i], HDA_OUTPUT))
4355 snd_hda_codec_amp_stereo(codec, conn[i],
4356 HDA_OUTPUT, 0,
4357 0xff, HDA_AMP_MUTE);
4358 }
4359}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004360
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004361/*
4362 * Parse the given BIOS configuration and set up the hda_gen_spec
4363 *
4364 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004365 * or a negative error code
4366 */
4367int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004368 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004369{
4370 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004371 int err;
4372
Takashi Iwai1c70a582013-01-11 17:48:22 +01004373 parse_user_hints(codec);
4374
Takashi Iwaie4a395e2013-01-23 17:00:31 +01004375 if (spec->mixer_nid && !spec->mixer_merge_nid)
4376 spec->mixer_merge_nid = spec->mixer_nid;
4377
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004378 if (cfg != &spec->autocfg) {
4379 spec->autocfg = *cfg;
4380 cfg = &spec->autocfg;
4381 }
4382
Takashi Iwai98bd1112013-03-22 14:53:50 +01004383 if (!spec->main_out_badness)
4384 spec->main_out_badness = &hda_main_out_badness;
4385 if (!spec->extra_out_badness)
4386 spec->extra_out_badness = &hda_extra_out_badness;
4387
David Henningsson6fc4cb92013-01-16 15:58:45 +01004388 fill_all_dac_nids(codec);
4389
Takashi Iwai352f7f92012-12-19 12:52:06 +01004390 if (!cfg->line_outs) {
4391 if (cfg->dig_outs || cfg->dig_in_pin) {
4392 spec->multiout.max_channels = 2;
4393 spec->no_analog = 1;
4394 goto dig_only;
4395 }
Takashi Iwaic9e4bdb2013-12-06 09:30:14 +01004396 if (!cfg->num_inputs && !cfg->dig_in_pin)
4397 return 0; /* can't find valid BIOS pin config */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004398 }
4399
4400 if (!spec->no_primary_hp &&
4401 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
4402 cfg->line_outs <= cfg->hp_outs) {
4403 /* use HP as primary out */
4404 cfg->speaker_outs = cfg->line_outs;
4405 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4406 sizeof(cfg->speaker_pins));
4407 cfg->line_outs = cfg->hp_outs;
4408 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4409 cfg->hp_outs = 0;
4410 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4411 cfg->line_out_type = AUTO_PIN_HP_OUT;
4412 }
4413
4414 err = parse_output_paths(codec);
4415 if (err < 0)
4416 return err;
4417 err = create_multi_channel_mode(codec);
4418 if (err < 0)
4419 return err;
4420 err = create_multi_out_ctls(codec, cfg);
4421 if (err < 0)
4422 return err;
4423 err = create_hp_out_ctls(codec);
4424 if (err < 0)
4425 return err;
4426 err = create_speaker_out_ctls(codec);
4427 if (err < 0)
4428 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004429 err = create_indep_hp_ctls(codec);
4430 if (err < 0)
4431 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01004432 err = create_loopback_mixing_ctl(codec);
4433 if (err < 0)
4434 return err;
Takashi Iwai967303d2013-02-19 17:12:42 +01004435 err = create_hp_mic(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004436 if (err < 0)
4437 return err;
4438 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02004439 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02004440 return err;
4441
Takashi Iwaia07a9492013-01-07 16:44:06 +01004442 spec->const_channel_count = spec->ext_channel_count;
4443 /* check the multiple speaker and headphone pins */
4444 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4445 spec->const_channel_count = max(spec->const_channel_count,
4446 cfg->speaker_outs * 2);
4447 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4448 spec->const_channel_count = max(spec->const_channel_count,
4449 cfg->hp_outs * 2);
4450 spec->multiout.max_channels = max(spec->ext_channel_count,
4451 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004452
4453 err = check_auto_mute_availability(codec);
4454 if (err < 0)
4455 return err;
4456
4457 err = check_dyn_adc_switch(codec);
4458 if (err < 0)
4459 return err;
4460
Takashi Iwai967303d2013-02-19 17:12:42 +01004461 err = check_auto_mic_availability(codec);
4462 if (err < 0)
4463 return err;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004464
Takashi Iwaif1e762d2013-12-09 16:02:24 +01004465 /* add stereo mix if available and not enabled yet */
4466 if (!spec->auto_mic && spec->mixer_nid &&
4467 spec->add_stereo_mix_input &&
4468 spec->input_mux.num_items > 1 &&
4469 snd_hda_get_bool_hint(codec, "add_stereo_mix_input") < 0) {
4470 err = parse_capture_source(codec, spec->mixer_nid,
4471 CFG_IDX_MIX, spec->num_all_adcs,
4472 "Stereo Mix", 0);
4473 if (err < 0)
4474 return err;
4475 }
4476
4477
Takashi Iwai352f7f92012-12-19 12:52:06 +01004478 err = create_capture_mixers(codec);
4479 if (err < 0)
4480 return err;
4481
4482 err = parse_mic_boost(codec);
4483 if (err < 0)
4484 return err;
4485
Takashi Iwaiced4cef2013-11-26 08:33:45 +01004486 /* create "Headphone Mic Jack Mode" if no input selection is
4487 * available (or user specifies add_jack_modes hint)
4488 */
4489 if (spec->hp_mic_pin &&
4490 (spec->auto_mic || spec->input_mux.num_items == 1 ||
4491 spec->add_jack_modes)) {
4492 err = create_hp_mic_jack_mode(codec, spec->hp_mic_pin);
4493 if (err < 0)
4494 return err;
4495 }
4496
Takashi Iwaif811c3c2013-03-07 18:32:59 +01004497 if (spec->add_jack_modes) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01004498 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4499 err = create_out_jack_modes(codec, cfg->line_outs,
4500 cfg->line_out_pins);
4501 if (err < 0)
4502 return err;
4503 }
4504 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4505 err = create_out_jack_modes(codec, cfg->hp_outs,
4506 cfg->hp_pins);
4507 if (err < 0)
4508 return err;
4509 }
4510 }
4511
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004512 /* mute all aamix input initially */
4513 if (spec->mixer_nid)
4514 mute_all_mixer_nid(codec, spec->mixer_nid);
4515
Takashi Iwai352f7f92012-12-19 12:52:06 +01004516 dig_only:
4517 parse_digital(codec);
4518
Takashi Iwai55196ff2013-01-24 17:32:56 +01004519 if (spec->power_down_unused)
4520 codec->power_filter = snd_hda_gen_path_power_filter;
4521
Takashi Iwai7504b6c2013-03-18 11:25:51 +01004522 if (!spec->no_analog && spec->beep_nid) {
4523 err = snd_hda_attach_beep_device(codec, spec->beep_nid);
4524 if (err < 0)
4525 return err;
4526 }
4527
Takashi Iwai352f7f92012-12-19 12:52:06 +01004528 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004530EXPORT_SYMBOL_GPL(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531
4532
4533/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004534 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004536
4537/* slave controls for virtual master */
4538static const char * const slave_pfxs[] = {
4539 "Front", "Surround", "Center", "LFE", "Side",
4540 "Headphone", "Speaker", "Mono", "Line Out",
4541 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01004542 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
4543 "Headphone Front", "Headphone Surround", "Headphone CLFE",
4544 "Headphone Side",
Takashi Iwai352f7f92012-12-19 12:52:06 +01004545 NULL,
4546};
4547
4548int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004550 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552
Takashi Iwai36502d02012-12-19 15:15:10 +01004553 if (spec->kctls.used) {
4554 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
4555 if (err < 0)
4556 return err;
4557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558
Takashi Iwai352f7f92012-12-19 12:52:06 +01004559 if (spec->multiout.dig_out_nid) {
4560 err = snd_hda_create_dig_out_ctls(codec,
4561 spec->multiout.dig_out_nid,
4562 spec->multiout.dig_out_nid,
4563 spec->pcm_rec[1].pcm_type);
4564 if (err < 0)
4565 return err;
4566 if (!spec->no_analog) {
4567 err = snd_hda_create_spdif_share_sw(codec,
4568 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569 if (err < 0)
4570 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004571 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 }
4573 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004574 if (spec->dig_in_nid) {
4575 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
4576 if (err < 0)
4577 return err;
4578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579
Takashi Iwai352f7f92012-12-19 12:52:06 +01004580 /* if we have no master control, let's create it */
4581 if (!spec->no_analog &&
4582 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004583 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01004584 spec->vmaster_tlv, slave_pfxs,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004585 "Playback Volume");
4586 if (err < 0)
4587 return err;
4588 }
4589 if (!spec->no_analog &&
4590 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
4591 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
4592 NULL, slave_pfxs,
4593 "Playback Switch",
4594 true, &spec->vmaster_mute.sw_kctl);
4595 if (err < 0)
4596 return err;
Takashi Iwaib63eae02013-10-25 23:43:10 +02004597 if (spec->vmaster_mute.hook) {
Takashi Iwaifd25a972012-12-20 14:57:18 +01004598 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
4599 spec->vmaster_mute_enum);
Takashi Iwaib63eae02013-10-25 23:43:10 +02004600 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
4601 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603
Takashi Iwai352f7f92012-12-19 12:52:06 +01004604 free_kctls(spec); /* no longer needed */
4605
Takashi Iwai352f7f92012-12-19 12:52:06 +01004606 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
4607 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 return err;
4609
4610 return 0;
4611}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004612EXPORT_SYMBOL_GPL(snd_hda_gen_build_controls);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004613
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614
4615/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004616 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004619static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
4620 struct hda_codec *codec,
4621 struct snd_pcm_substream *substream,
4622 int action)
4623{
4624 struct hda_gen_spec *spec = codec->spec;
4625 if (spec->pcm_playback_hook)
4626 spec->pcm_playback_hook(hinfo, codec, substream, action);
4627}
4628
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004629static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
4630 struct hda_codec *codec,
4631 struct snd_pcm_substream *substream,
4632 int action)
4633{
4634 struct hda_gen_spec *spec = codec->spec;
4635 if (spec->pcm_capture_hook)
4636 spec->pcm_capture_hook(hinfo, codec, substream, action);
4637}
4638
Takashi Iwai352f7f92012-12-19 12:52:06 +01004639/*
4640 * Analog playback callbacks
4641 */
4642static int playback_pcm_open(struct hda_pcm_stream *hinfo,
4643 struct hda_codec *codec,
4644 struct snd_pcm_substream *substream)
4645{
4646 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004647 int err;
4648
4649 mutex_lock(&spec->pcm_mutex);
4650 err = snd_hda_multi_out_analog_open(codec,
4651 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004652 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004653 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004654 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004655 call_pcm_playback_hook(hinfo, codec, substream,
4656 HDA_GEN_PCM_ACT_OPEN);
4657 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004658 mutex_unlock(&spec->pcm_mutex);
4659 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004660}
4661
4662static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01004663 struct hda_codec *codec,
4664 unsigned int stream_tag,
4665 unsigned int format,
4666 struct snd_pcm_substream *substream)
4667{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004668 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004669 int err;
4670
4671 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
4672 stream_tag, format, substream);
4673 if (!err)
4674 call_pcm_playback_hook(hinfo, codec, substream,
4675 HDA_GEN_PCM_ACT_PREPARE);
4676 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004677}
Takashi Iwai97ec5582006-03-21 11:29:07 +01004678
Takashi Iwai352f7f92012-12-19 12:52:06 +01004679static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4680 struct hda_codec *codec,
4681 struct snd_pcm_substream *substream)
4682{
4683 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004684 int err;
4685
4686 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
4687 if (!err)
4688 call_pcm_playback_hook(hinfo, codec, substream,
4689 HDA_GEN_PCM_ACT_CLEANUP);
4690 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004691}
4692
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004693static int playback_pcm_close(struct hda_pcm_stream *hinfo,
4694 struct hda_codec *codec,
4695 struct snd_pcm_substream *substream)
4696{
4697 struct hda_gen_spec *spec = codec->spec;
4698 mutex_lock(&spec->pcm_mutex);
4699 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004700 call_pcm_playback_hook(hinfo, codec, substream,
4701 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004702 mutex_unlock(&spec->pcm_mutex);
4703 return 0;
4704}
4705
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004706static int capture_pcm_open(struct hda_pcm_stream *hinfo,
4707 struct hda_codec *codec,
4708 struct snd_pcm_substream *substream)
4709{
4710 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
4711 return 0;
4712}
4713
4714static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4715 struct hda_codec *codec,
4716 unsigned int stream_tag,
4717 unsigned int format,
4718 struct snd_pcm_substream *substream)
4719{
4720 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4721 call_pcm_capture_hook(hinfo, codec, substream,
4722 HDA_GEN_PCM_ACT_PREPARE);
4723 return 0;
4724}
4725
4726static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4727 struct hda_codec *codec,
4728 struct snd_pcm_substream *substream)
4729{
4730 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4731 call_pcm_capture_hook(hinfo, codec, substream,
4732 HDA_GEN_PCM_ACT_CLEANUP);
4733 return 0;
4734}
4735
4736static int capture_pcm_close(struct hda_pcm_stream *hinfo,
4737 struct hda_codec *codec,
4738 struct snd_pcm_substream *substream)
4739{
4740 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
4741 return 0;
4742}
4743
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004744static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
4745 struct hda_codec *codec,
4746 struct snd_pcm_substream *substream)
4747{
4748 struct hda_gen_spec *spec = codec->spec;
4749 int err = 0;
4750
4751 mutex_lock(&spec->pcm_mutex);
4752 if (!spec->indep_hp_enabled)
4753 err = -EBUSY;
4754 else
4755 spec->active_streams |= 1 << STREAM_INDEP_HP;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004756 call_pcm_playback_hook(hinfo, codec, substream,
4757 HDA_GEN_PCM_ACT_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004758 mutex_unlock(&spec->pcm_mutex);
4759 return err;
4760}
4761
4762static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
4763 struct hda_codec *codec,
4764 struct snd_pcm_substream *substream)
4765{
4766 struct hda_gen_spec *spec = codec->spec;
4767 mutex_lock(&spec->pcm_mutex);
4768 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004769 call_pcm_playback_hook(hinfo, codec, substream,
4770 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004771 mutex_unlock(&spec->pcm_mutex);
4772 return 0;
4773}
4774
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004775static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4776 struct hda_codec *codec,
4777 unsigned int stream_tag,
4778 unsigned int format,
4779 struct snd_pcm_substream *substream)
4780{
4781 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4782 call_pcm_playback_hook(hinfo, codec, substream,
4783 HDA_GEN_PCM_ACT_PREPARE);
4784 return 0;
4785}
4786
4787static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4788 struct hda_codec *codec,
4789 struct snd_pcm_substream *substream)
4790{
4791 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4792 call_pcm_playback_hook(hinfo, codec, substream,
4793 HDA_GEN_PCM_ACT_CLEANUP);
4794 return 0;
4795}
4796
Takashi Iwai352f7f92012-12-19 12:52:06 +01004797/*
4798 * Digital out
4799 */
4800static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
4801 struct hda_codec *codec,
4802 struct snd_pcm_substream *substream)
4803{
4804 struct hda_gen_spec *spec = codec->spec;
4805 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
4806}
4807
4808static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4809 struct hda_codec *codec,
4810 unsigned int stream_tag,
4811 unsigned int format,
4812 struct snd_pcm_substream *substream)
4813{
4814 struct hda_gen_spec *spec = codec->spec;
4815 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
4816 stream_tag, format, substream);
4817}
4818
4819static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4820 struct hda_codec *codec,
4821 struct snd_pcm_substream *substream)
4822{
4823 struct hda_gen_spec *spec = codec->spec;
4824 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
4825}
4826
4827static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
4828 struct hda_codec *codec,
4829 struct snd_pcm_substream *substream)
4830{
4831 struct hda_gen_spec *spec = codec->spec;
4832 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
4833}
4834
4835/*
4836 * Analog capture
4837 */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004838#define alt_capture_pcm_open capture_pcm_open
4839#define alt_capture_pcm_close capture_pcm_close
4840
Takashi Iwai352f7f92012-12-19 12:52:06 +01004841static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4842 struct hda_codec *codec,
4843 unsigned int stream_tag,
4844 unsigned int format,
4845 struct snd_pcm_substream *substream)
4846{
4847 struct hda_gen_spec *spec = codec->spec;
4848
4849 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01004850 stream_tag, 0, format);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004851 call_pcm_capture_hook(hinfo, codec, substream,
4852 HDA_GEN_PCM_ACT_PREPARE);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004853 return 0;
4854}
4855
Takashi Iwai352f7f92012-12-19 12:52:06 +01004856static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4857 struct hda_codec *codec,
4858 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01004859{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004860 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01004861
Takashi Iwai352f7f92012-12-19 12:52:06 +01004862 snd_hda_codec_cleanup_stream(codec,
4863 spec->adc_nids[substream->number + 1]);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004864 call_pcm_capture_hook(hinfo, codec, substream,
4865 HDA_GEN_PCM_ACT_CLEANUP);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004866 return 0;
4867}
4868
Takashi Iwai352f7f92012-12-19 12:52:06 +01004869/*
4870 */
4871static const struct hda_pcm_stream pcm_analog_playback = {
4872 .substreams = 1,
4873 .channels_min = 2,
4874 .channels_max = 8,
4875 /* NID is set in build_pcms */
4876 .ops = {
4877 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004878 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004879 .prepare = playback_pcm_prepare,
4880 .cleanup = playback_pcm_cleanup
4881 },
4882};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883
Takashi Iwai352f7f92012-12-19 12:52:06 +01004884static const struct hda_pcm_stream pcm_analog_capture = {
4885 .substreams = 1,
4886 .channels_min = 2,
4887 .channels_max = 2,
4888 /* NID is set in build_pcms */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004889 .ops = {
4890 .open = capture_pcm_open,
4891 .close = capture_pcm_close,
4892 .prepare = capture_pcm_prepare,
4893 .cleanup = capture_pcm_cleanup
4894 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004895};
4896
4897static const struct hda_pcm_stream pcm_analog_alt_playback = {
4898 .substreams = 1,
4899 .channels_min = 2,
4900 .channels_max = 2,
4901 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004902 .ops = {
4903 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004904 .close = alt_playback_pcm_close,
4905 .prepare = alt_playback_pcm_prepare,
4906 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004907 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004908};
4909
4910static const struct hda_pcm_stream pcm_analog_alt_capture = {
4911 .substreams = 2, /* can be overridden */
4912 .channels_min = 2,
4913 .channels_max = 2,
4914 /* NID is set in build_pcms */
4915 .ops = {
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004916 .open = alt_capture_pcm_open,
4917 .close = alt_capture_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004918 .prepare = alt_capture_pcm_prepare,
4919 .cleanup = alt_capture_pcm_cleanup
4920 },
4921};
4922
4923static const struct hda_pcm_stream pcm_digital_playback = {
4924 .substreams = 1,
4925 .channels_min = 2,
4926 .channels_max = 2,
4927 /* NID is set in build_pcms */
4928 .ops = {
4929 .open = dig_playback_pcm_open,
4930 .close = dig_playback_pcm_close,
4931 .prepare = dig_playback_pcm_prepare,
4932 .cleanup = dig_playback_pcm_cleanup
4933 },
4934};
4935
4936static const struct hda_pcm_stream pcm_digital_capture = {
4937 .substreams = 1,
4938 .channels_min = 2,
4939 .channels_max = 2,
4940 /* NID is set in build_pcms */
4941};
4942
4943/* Used by build_pcms to flag that a PCM has no playback stream */
4944static const struct hda_pcm_stream pcm_null_stream = {
4945 .substreams = 0,
4946 .channels_min = 0,
4947 .channels_max = 0,
4948};
4949
4950/*
4951 * dynamic changing ADC PCM streams
4952 */
4953static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
4954{
4955 struct hda_gen_spec *spec = codec->spec;
4956 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
4957
4958 if (spec->cur_adc && spec->cur_adc != new_adc) {
4959 /* stream is running, let's swap the current ADC */
4960 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
4961 spec->cur_adc = new_adc;
4962 snd_hda_codec_setup_stream(codec, new_adc,
4963 spec->cur_adc_stream_tag, 0,
4964 spec->cur_adc_format);
4965 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004967 return false;
4968}
4969
4970/* analog capture with dynamic dual-adc changes */
4971static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4972 struct hda_codec *codec,
4973 unsigned int stream_tag,
4974 unsigned int format,
4975 struct snd_pcm_substream *substream)
4976{
4977 struct hda_gen_spec *spec = codec->spec;
4978 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
4979 spec->cur_adc_stream_tag = stream_tag;
4980 spec->cur_adc_format = format;
4981 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
4982 return 0;
4983}
4984
4985static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4986 struct hda_codec *codec,
4987 struct snd_pcm_substream *substream)
4988{
4989 struct hda_gen_spec *spec = codec->spec;
4990 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
4991 spec->cur_adc = 0;
4992 return 0;
4993}
4994
4995static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
4996 .substreams = 1,
4997 .channels_min = 2,
4998 .channels_max = 2,
4999 .nid = 0, /* fill later */
5000 .ops = {
5001 .prepare = dyn_adc_capture_pcm_prepare,
5002 .cleanup = dyn_adc_capture_pcm_cleanup
5003 },
5004};
5005
Takashi Iwaif873e532012-12-20 16:58:39 +01005006static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
5007 const char *chip_name)
5008{
5009 char *p;
5010
5011 if (*str)
5012 return;
5013 strlcpy(str, chip_name, len);
5014
5015 /* drop non-alnum chars after a space */
5016 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
5017 if (!isalnum(p[1])) {
5018 *p = 0;
5019 break;
5020 }
5021 }
5022 strlcat(str, sfx, len);
5023}
5024
Takashi Iwai352f7f92012-12-19 12:52:06 +01005025/* build PCM streams based on the parsed results */
5026int snd_hda_gen_build_pcms(struct hda_codec *codec)
5027{
5028 struct hda_gen_spec *spec = codec->spec;
5029 struct hda_pcm *info = spec->pcm_rec;
5030 const struct hda_pcm_stream *p;
5031 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032
5033 codec->num_pcms = 1;
5034 codec->pcm_info = info;
5035
Takashi Iwai352f7f92012-12-19 12:52:06 +01005036 if (spec->no_analog)
5037 goto skip_analog;
5038
Takashi Iwaif873e532012-12-20 16:58:39 +01005039 fill_pcm_stream_name(spec->stream_name_analog,
5040 sizeof(spec->stream_name_analog),
5041 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005042 info->name = spec->stream_name_analog;
5043
5044 if (spec->multiout.num_dacs > 0) {
5045 p = spec->stream_analog_playback;
5046 if (!p)
5047 p = &pcm_analog_playback;
5048 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
5049 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
5050 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
5051 spec->multiout.max_channels;
5052 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
5053 spec->autocfg.line_outs == 2)
5054 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
5055 snd_pcm_2_1_chmaps;
5056 }
5057 if (spec->num_adc_nids) {
5058 p = spec->stream_analog_capture;
5059 if (!p) {
5060 if (spec->dyn_adc_switch)
5061 p = &dyn_adc_pcm_analog_capture;
5062 else
5063 p = &pcm_analog_capture;
5064 }
5065 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
5066 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
5067 }
5068
Takashi Iwai352f7f92012-12-19 12:52:06 +01005069 skip_analog:
5070 /* SPDIF for stream index #1 */
5071 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01005072 fill_pcm_stream_name(spec->stream_name_digital,
5073 sizeof(spec->stream_name_digital),
5074 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005075 codec->num_pcms = 2;
5076 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
5077 info = spec->pcm_rec + 1;
5078 info->name = spec->stream_name_digital;
5079 if (spec->dig_out_type)
5080 info->pcm_type = spec->dig_out_type;
5081 else
5082 info->pcm_type = HDA_PCM_TYPE_SPDIF;
5083 if (spec->multiout.dig_out_nid) {
5084 p = spec->stream_digital_playback;
5085 if (!p)
5086 p = &pcm_digital_playback;
5087 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
5088 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
5089 }
5090 if (spec->dig_in_nid) {
5091 p = spec->stream_digital_capture;
5092 if (!p)
5093 p = &pcm_digital_capture;
5094 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
5095 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
5096 }
5097 }
5098
5099 if (spec->no_analog)
5100 return 0;
5101
5102 /* If the use of more than one ADC is requested for the current
5103 * model, configure a second analog capture-only PCM.
5104 */
5105 have_multi_adcs = (spec->num_adc_nids > 1) &&
5106 !spec->dyn_adc_switch && !spec->auto_mic;
5107 /* Additional Analaog capture for index #2 */
5108 if (spec->alt_dac_nid || have_multi_adcs) {
Takashi Iwaia6071482013-01-21 16:50:09 +01005109 fill_pcm_stream_name(spec->stream_name_alt_analog,
5110 sizeof(spec->stream_name_alt_analog),
5111 " Alt Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005112 codec->num_pcms = 3;
5113 info = spec->pcm_rec + 2;
Takashi Iwaia6071482013-01-21 16:50:09 +01005114 info->name = spec->stream_name_alt_analog;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005115 if (spec->alt_dac_nid) {
5116 p = spec->stream_analog_alt_playback;
5117 if (!p)
5118 p = &pcm_analog_alt_playback;
5119 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
5120 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
5121 spec->alt_dac_nid;
5122 } else {
5123 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
5124 pcm_null_stream;
5125 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
5126 }
5127 if (have_multi_adcs) {
5128 p = spec->stream_analog_alt_capture;
5129 if (!p)
5130 p = &pcm_analog_alt_capture;
5131 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
5132 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
5133 spec->adc_nids[1];
5134 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
5135 spec->num_adc_nids - 1;
5136 } else {
5137 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
5138 pcm_null_stream;
5139 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
5140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141 }
5142
5143 return 0;
5144}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005145EXPORT_SYMBOL_GPL(snd_hda_gen_build_pcms);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005146
5147
5148/*
5149 * Standard auto-parser initializations
5150 */
5151
Takashi Iwaid4156932013-01-07 10:08:02 +01005152/* configure the given path as a proper output */
Takashi Iwai2c12c302013-01-10 09:33:29 +01005153static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005154{
5155 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01005156 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005157
Takashi Iwai196c17662013-01-04 15:01:40 +01005158 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01005159 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005160 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01005161 pin = path->path[path->depth - 1];
Takashi Iwai2c12c302013-01-10 09:33:29 +01005162 restore_pin_ctl(codec, pin);
Takashi Iwai65033cc2013-04-16 12:31:05 +02005163 snd_hda_activate_path(codec, path, path->active,
5164 aamix_default(codec->spec));
Takashi Iwaie1284af2013-01-03 16:33:02 +01005165 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005166}
5167
5168/* initialize primary output paths */
5169static void init_multi_out(struct hda_codec *codec)
5170{
5171 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005172 int i;
5173
Takashi Iwaid4156932013-01-07 10:08:02 +01005174 for (i = 0; i < spec->autocfg.line_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005175 set_output_and_unmute(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005176}
5177
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005178
Takashi Iwai2c12c302013-01-10 09:33:29 +01005179static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005180{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005181 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005182
Takashi Iwaid4156932013-01-07 10:08:02 +01005183 for (i = 0; i < num_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005184 set_output_and_unmute(codec, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005185}
5186
5187/* initialize hp and speaker paths */
5188static void init_extra_out(struct hda_codec *codec)
5189{
5190 struct hda_gen_spec *spec = codec->spec;
5191
5192 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005193 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005194 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
5195 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai2c12c302013-01-10 09:33:29 +01005196 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005197}
5198
5199/* initialize multi-io paths */
5200static void init_multi_io(struct hda_codec *codec)
5201{
5202 struct hda_gen_spec *spec = codec->spec;
5203 int i;
5204
5205 for (i = 0; i < spec->multi_ios; i++) {
5206 hda_nid_t pin = spec->multi_io[i].pin;
5207 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01005208 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005209 if (!path)
5210 continue;
5211 if (!spec->multi_io[i].ctl_in)
5212 spec->multi_io[i].ctl_in =
Takashi Iwai2c12c302013-01-10 09:33:29 +01005213 snd_hda_codec_get_pin_target(codec, pin);
Takashi Iwai65033cc2013-04-16 12:31:05 +02005214 snd_hda_activate_path(codec, path, path->active,
5215 aamix_default(spec));
Takashi Iwai352f7f92012-12-19 12:52:06 +01005216 }
5217}
5218
Takashi Iwai4f7f67f2013-12-03 10:51:37 +01005219static void init_aamix_paths(struct hda_codec *codec)
5220{
5221 struct hda_gen_spec *spec = codec->spec;
5222
5223 if (!spec->have_aamix_ctl)
5224 return;
5225 update_aamix_paths(codec, spec->aamix_mode, spec->out_paths[0],
5226 spec->aamix_out_paths[0],
5227 spec->autocfg.line_out_type);
5228 update_aamix_paths(codec, spec->aamix_mode, spec->hp_paths[0],
5229 spec->aamix_out_paths[1],
5230 AUTO_PIN_HP_OUT);
5231 update_aamix_paths(codec, spec->aamix_mode, spec->speaker_paths[0],
5232 spec->aamix_out_paths[2],
5233 AUTO_PIN_SPEAKER_OUT);
5234}
5235
Takashi Iwai352f7f92012-12-19 12:52:06 +01005236/* set up input pins and loopback paths */
5237static void init_analog_input(struct hda_codec *codec)
5238{
5239 struct hda_gen_spec *spec = codec->spec;
5240 struct auto_pin_cfg *cfg = &spec->autocfg;
5241 int i;
5242
5243 for (i = 0; i < cfg->num_inputs; i++) {
5244 hda_nid_t nid = cfg->inputs[i].pin;
5245 if (is_input_pin(codec, nid))
Takashi Iwai2c12c302013-01-10 09:33:29 +01005246 restore_pin_ctl(codec, nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005247
5248 /* init loopback inputs */
5249 if (spec->mixer_nid) {
Takashi Iwai3e367f12013-01-23 17:07:23 +01005250 resume_path_from_idx(codec, spec->loopback_paths[i]);
5251 resume_path_from_idx(codec, spec->loopback_merge_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005252 }
5253 }
5254}
5255
5256/* initialize ADC paths */
5257static void init_input_src(struct hda_codec *codec)
5258{
5259 struct hda_gen_spec *spec = codec->spec;
5260 struct hda_input_mux *imux = &spec->input_mux;
5261 struct nid_path *path;
5262 int i, c, nums;
5263
5264 if (spec->dyn_adc_switch)
5265 nums = 1;
5266 else
5267 nums = spec->num_adc_nids;
5268
5269 for (c = 0; c < nums; c++) {
5270 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01005271 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005272 if (path) {
5273 bool active = path->active;
5274 if (i == spec->cur_mux[c])
5275 active = true;
5276 snd_hda_activate_path(codec, path, active, false);
5277 }
5278 }
Takashi Iwai967303d2013-02-19 17:12:42 +01005279 if (spec->hp_mic)
5280 update_hp_mic(codec, c, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005281 }
5282
Takashi Iwai352f7f92012-12-19 12:52:06 +01005283 if (spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01005284 spec->cap_sync_hook(codec, NULL, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005285}
5286
5287/* set right pin controls for digital I/O */
5288static void init_digital(struct hda_codec *codec)
5289{
5290 struct hda_gen_spec *spec = codec->spec;
5291 int i;
5292 hda_nid_t pin;
5293
Takashi Iwaid4156932013-01-07 10:08:02 +01005294 for (i = 0; i < spec->autocfg.dig_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005295 set_output_and_unmute(codec, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005296 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005297 if (pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01005298 restore_pin_ctl(codec, pin);
Takashi Iwai3e367f12013-01-23 17:07:23 +01005299 resume_path_from_idx(codec, spec->digin_path);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005300 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005301}
5302
Takashi Iwai973e4972012-12-20 15:16:09 +01005303/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5304 * invalid unsol tags by some reason
5305 */
5306static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5307{
5308 int i;
5309
5310 for (i = 0; i < codec->init_pins.used; i++) {
5311 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
5312 hda_nid_t nid = pin->nid;
5313 if (is_jack_detectable(codec, nid) &&
5314 !snd_hda_jack_tbl_get(codec, nid))
5315 snd_hda_codec_update_cache(codec, nid, 0,
5316 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5317 }
5318}
5319
Takashi Iwai5187ac12013-01-07 12:52:16 +01005320/*
5321 * initialize the generic spec;
5322 * this can be put as patch_ops.init function
5323 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005324int snd_hda_gen_init(struct hda_codec *codec)
5325{
5326 struct hda_gen_spec *spec = codec->spec;
5327
5328 if (spec->init_hook)
5329 spec->init_hook(codec);
5330
5331 snd_hda_apply_verbs(codec);
5332
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005333 codec->cached_write = 1;
5334
Takashi Iwai352f7f92012-12-19 12:52:06 +01005335 init_multi_out(codec);
5336 init_extra_out(codec);
5337 init_multi_io(codec);
Takashi Iwai4f7f67f2013-12-03 10:51:37 +01005338 init_aamix_paths(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005339 init_analog_input(codec);
5340 init_input_src(codec);
5341 init_digital(codec);
5342
Takashi Iwai973e4972012-12-20 15:16:09 +01005343 clear_unsol_on_unused_pins(codec);
5344
Takashi Iwai352f7f92012-12-19 12:52:06 +01005345 /* call init functions of standard auto-mute helpers */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01005346 update_automute_all(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005347
Takashi Iwaidc870f32013-01-22 15:24:30 +01005348 snd_hda_codec_flush_cache(codec);
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005349
Takashi Iwai352f7f92012-12-19 12:52:06 +01005350 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
5351 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5352
5353 hda_call_check_power_status(codec, 0x01);
5354 return 0;
5355}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005356EXPORT_SYMBOL_GPL(snd_hda_gen_init);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005357
Takashi Iwai5187ac12013-01-07 12:52:16 +01005358/*
5359 * free the generic spec;
5360 * this can be put as patch_ops.free function
5361 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005362void snd_hda_gen_free(struct hda_codec *codec)
5363{
Takashi Iwai8a02c0c2014-02-10 18:09:45 +01005364 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_FREE);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005365 snd_hda_gen_spec_free(codec->spec);
5366 kfree(codec->spec);
5367 codec->spec = NULL;
5368}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005369EXPORT_SYMBOL_GPL(snd_hda_gen_free);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005370
5371#ifdef CONFIG_PM
Takashi Iwai5187ac12013-01-07 12:52:16 +01005372/*
5373 * check the loopback power save state;
5374 * this can be put as patch_ops.check_power_status function
5375 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005376int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
5377{
5378 struct hda_gen_spec *spec = codec->spec;
5379 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
5380}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005381EXPORT_SYMBOL_GPL(snd_hda_gen_check_power_status);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005382#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01005383
5384
5385/*
5386 * the generic codec support
5387 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388
Takashi Iwai352f7f92012-12-19 12:52:06 +01005389static const struct hda_codec_ops generic_patch_ops = {
5390 .build_controls = snd_hda_gen_build_controls,
5391 .build_pcms = snd_hda_gen_build_pcms,
5392 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01005393 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005394 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02005395#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01005396 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02005397#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398};
5399
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400int snd_hda_parse_generic_codec(struct hda_codec *codec)
5401{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005402 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403 int err;
5404
Takashi Iwaie560d8d2005-09-09 14:21:46 +02005405 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005406 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005408 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410
Takashi Iwai9eb413e2012-12-19 14:41:21 +01005411 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
5412 if (err < 0)
5413 return err;
5414
5415 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005416 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417 goto error;
5418
5419 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420 return 0;
5421
Takashi Iwai352f7f92012-12-19 12:52:06 +01005422error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01005423 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424 return err;
5425}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005426EXPORT_SYMBOL_GPL(snd_hda_parse_generic_codec);
Takashi Iwaib21bdd02013-11-18 12:03:56 +01005427
5428MODULE_LICENSE("GPL");
5429MODULE_DESCRIPTION("Generic HD-audio codec parser");