blob: dc13cce70932aa917cd3524625c6b89d471032b3 [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
David Henningsson99a55922013-01-16 15:58:44 +0100522static bool same_amp_caps(struct hda_codec *codec, hda_nid_t nid1,
523 hda_nid_t nid2, int dir)
524{
525 if (!(get_wcaps(codec, nid1) & (1 << (dir + 1))))
526 return !(get_wcaps(codec, nid2) & (1 << (dir + 1)));
527 return (query_amp_caps(codec, nid1, dir) ==
528 query_amp_caps(codec, nid2, dir));
529}
530
Takashi Iwai352f7f92012-12-19 12:52:06 +0100531/* look for a widget suitable for assigning a mute switch in the path */
532static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
533 struct nid_path *path)
534{
535 int i;
536
537 for (i = path->depth - 1; i >= 0; i--) {
538 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
539 return path->path[i];
540 if (i != path->depth - 1 && i != 0 &&
541 nid_has_mute(codec, path->path[i], HDA_INPUT))
542 return path->path[i];
543 }
544 return 0;
545}
546
547/* look for a widget suitable for assigning a volume ctl in the path */
548static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
549 struct nid_path *path)
550{
Takashi Iwaia1114a82013-11-04 16:32:01 +0100551 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100552 int i;
553
554 for (i = path->depth - 1; i >= 0; i--) {
Takashi Iwaia1114a82013-11-04 16:32:01 +0100555 hda_nid_t nid = path->path[i];
556 if ((spec->out_vol_mask >> nid) & 1)
557 continue;
558 if (nid_has_volume(codec, nid, HDA_OUTPUT))
559 return nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100560 }
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200561 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
563
564/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100565 * path activation / deactivation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100567
568/* can have the amp-in capability? */
569static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100571 hda_nid_t nid = path->path[idx];
572 unsigned int caps = get_wcaps(codec, nid);
573 unsigned int type = get_wcaps_type(caps);
574
575 if (!(caps & AC_WCAP_IN_AMP))
576 return false;
577 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
578 return false;
579 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580}
581
Takashi Iwai352f7f92012-12-19 12:52:06 +0100582/* can have the amp-out capability? */
583static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100585 hda_nid_t nid = path->path[idx];
586 unsigned int caps = get_wcaps(codec, nid);
587 unsigned int type = get_wcaps_type(caps);
588
589 if (!(caps & AC_WCAP_OUT_AMP))
590 return false;
591 if (type == AC_WID_PIN && !idx) /* only for output pins */
592 return false;
593 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594}
595
Takashi Iwai352f7f92012-12-19 12:52:06 +0100596/* check whether the given (nid,dir,idx) is active */
597static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai7dddf2ae2013-01-24 16:31:35 +0100598 unsigned int dir, unsigned int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100600 struct hda_gen_spec *spec = codec->spec;
601 int i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Takashi Iwai352f7f92012-12-19 12:52:06 +0100603 for (n = 0; n < spec->paths.used; n++) {
604 struct nid_path *path = snd_array_elem(&spec->paths, n);
605 if (!path->active)
606 continue;
607 for (i = 0; i < path->depth; i++) {
608 if (path->path[i] == nid) {
609 if (dir == HDA_OUTPUT || path->idx[i] == idx)
610 return true;
611 break;
612 }
613 }
614 }
615 return false;
616}
617
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +0200618/* check whether the NID is referred by any active paths */
619#define is_active_nid_for_any(codec, nid) \
620 is_active_nid(codec, nid, HDA_OUTPUT, 0)
621
Takashi Iwai352f7f92012-12-19 12:52:06 +0100622/* get the default amp value for the target state */
623static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai8999bf02013-01-18 11:01:33 +0100624 int dir, unsigned int caps, bool enable)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100625{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100626 unsigned int val = 0;
627
Takashi Iwai352f7f92012-12-19 12:52:06 +0100628 if (caps & AC_AMPCAP_NUM_STEPS) {
629 /* set to 0dB */
630 if (enable)
631 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
632 }
Takashi Iwaif69910d2013-08-08 09:32:37 +0200633 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100634 if (!enable)
635 val |= HDA_AMP_MUTE;
636 }
637 return val;
638}
639
640/* initialize the amp value (only at the first time) */
641static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
642{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100643 unsigned int caps = query_amp_caps(codec, nid, dir);
644 int val = get_amp_val_to_activate(codec, nid, dir, caps, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100645 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
646}
647
Takashi Iwai8999bf02013-01-18 11:01:33 +0100648/* calculate amp value mask we can modify;
649 * if the given amp is controlled by mixers, don't touch it
650 */
651static unsigned int get_amp_mask_to_modify(struct hda_codec *codec,
652 hda_nid_t nid, int dir, int idx,
653 unsigned int caps)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100654{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100655 unsigned int mask = 0xff;
656
Takashi Iwaif69910d2013-08-08 09:32:37 +0200657 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
Takashi Iwai8999bf02013-01-18 11:01:33 +0100658 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_MUTE_CTL))
659 mask &= ~0x80;
660 }
661 if (caps & AC_AMPCAP_NUM_STEPS) {
662 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
663 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
664 mask &= ~0x7f;
665 }
666 return mask;
667}
668
669static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
670 int idx, int idx_to_check, bool enable)
671{
672 unsigned int caps;
673 unsigned int mask, val;
674
Takashi Iwai7dddf2ae2013-01-24 16:31:35 +0100675 if (!enable && is_active_nid(codec, nid, dir, idx_to_check))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100676 return;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100677
678 caps = query_amp_caps(codec, nid, dir);
679 val = get_amp_val_to_activate(codec, nid, dir, caps, enable);
680 mask = get_amp_mask_to_modify(codec, nid, dir, idx_to_check, caps);
681 if (!mask)
682 return;
683
684 val &= mask;
685 snd_hda_codec_amp_stereo(codec, nid, dir, idx, mask, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100686}
687
688static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
689 int i, bool enable)
690{
691 hda_nid_t nid = path->path[i];
692 init_amp(codec, nid, HDA_OUTPUT, 0);
Takashi Iwai8999bf02013-01-18 11:01:33 +0100693 activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100694}
695
696static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
697 int i, bool enable, bool add_aamix)
698{
699 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100700 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100701 int n, nums, idx;
702 int type;
703 hda_nid_t nid = path->path[i];
704
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100705 nums = snd_hda_get_conn_list(codec, nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100706 type = get_wcaps_type(get_wcaps(codec, nid));
707 if (type == AC_WID_PIN ||
708 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
709 nums = 1;
710 idx = 0;
711 } else
712 idx = path->idx[i];
713
714 for (n = 0; n < nums; n++)
715 init_amp(codec, nid, HDA_INPUT, n);
716
Takashi Iwai352f7f92012-12-19 12:52:06 +0100717 /* here is a little bit tricky in comparison with activate_amp_out();
718 * when aa-mixer is available, we need to enable the path as well
719 */
720 for (n = 0; n < nums; n++) {
Takashi Iwaie4a395e2013-01-23 17:00:31 +0100721 if (n != idx && (!add_aamix || conn[n] != spec->mixer_merge_nid))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100722 continue;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100723 activate_amp(codec, nid, HDA_INPUT, n, idx, enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 }
725}
726
Takashi Iwai352f7f92012-12-19 12:52:06 +0100727/* activate or deactivate the given path
728 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100730void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
731 bool enable, bool add_aamix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
Takashi Iwai55196ff2013-01-24 17:32:56 +0100733 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100734 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Takashi Iwai352f7f92012-12-19 12:52:06 +0100736 if (!enable)
737 path->active = false;
738
739 for (i = path->depth - 1; i >= 0; i--) {
Takashi Iwai55196ff2013-01-24 17:32:56 +0100740 hda_nid_t nid = path->path[i];
741 if (enable && spec->power_down_unused) {
742 /* make sure the widget is powered up */
743 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0))
744 snd_hda_codec_write(codec, nid, 0,
745 AC_VERB_SET_POWER_STATE,
746 AC_PWRST_D0);
747 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100748 if (enable && path->multi[i])
Takashi Iwai8f0972d2014-01-27 16:26:16 +0100749 snd_hda_codec_update_cache(codec, nid, 0,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100750 AC_VERB_SET_CONNECT_SEL,
751 path->idx[i]);
752 if (has_amp_in(codec, path, i))
753 activate_amp_in(codec, path, i, enable, add_aamix);
754 if (has_amp_out(codec, path, i))
755 activate_amp_out(codec, path, i, enable);
756 }
757
758 if (enable)
759 path->active = true;
760}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100761EXPORT_SYMBOL_GPL(snd_hda_activate_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100762
Takashi Iwai55196ff2013-01-24 17:32:56 +0100763/* if the given path is inactive, put widgets into D3 (only if suitable) */
764static void path_power_down_sync(struct hda_codec *codec, struct nid_path *path)
765{
766 struct hda_gen_spec *spec = codec->spec;
Jiri Slaby868211d2013-04-04 22:32:10 +0200767 bool changed = false;
Takashi Iwai55196ff2013-01-24 17:32:56 +0100768 int i;
769
770 if (!spec->power_down_unused || path->active)
771 return;
772
773 for (i = 0; i < path->depth; i++) {
774 hda_nid_t nid = path->path[i];
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +0200775 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D3) &&
776 !is_active_nid_for_any(codec, nid)) {
Takashi Iwai55196ff2013-01-24 17:32:56 +0100777 snd_hda_codec_write(codec, nid, 0,
778 AC_VERB_SET_POWER_STATE,
779 AC_PWRST_D3);
780 changed = true;
781 }
782 }
783
784 if (changed) {
785 msleep(10);
786 snd_hda_codec_read(codec, path->path[0], 0,
787 AC_VERB_GET_POWER_STATE, 0);
788 }
789}
790
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100791/* turn on/off EAPD on the given pin */
792static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
793{
794 struct hda_gen_spec *spec = codec->spec;
795 if (spec->own_eapd_ctl ||
796 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
797 return;
Takashi Iwai05909d5c2013-05-31 19:55:54 +0200798 if (spec->keep_eapd_on && !enable)
799 return;
Takashi Iwai468ac412013-11-12 11:36:00 +0100800 if (codec->inv_eapd)
801 enable = !enable;
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100802 snd_hda_codec_update_cache(codec, pin, 0,
803 AC_VERB_SET_EAPD_BTLENABLE,
804 enable ? 0x02 : 0x00);
805}
806
Takashi Iwai3e367f12013-01-23 17:07:23 +0100807/* re-initialize the path specified by the given path index */
808static void resume_path_from_idx(struct hda_codec *codec, int path_idx)
809{
810 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
811 if (path)
812 snd_hda_activate_path(codec, path, path->active, false);
813}
814
Takashi Iwai352f7f92012-12-19 12:52:06 +0100815
816/*
817 * Helper functions for creating mixer ctl elements
818 */
819
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200820static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
821 struct snd_ctl_elem_value *ucontrol);
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200822static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
823 struct snd_ctl_elem_value *ucontrol);
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200824
Takashi Iwai352f7f92012-12-19 12:52:06 +0100825enum {
826 HDA_CTL_WIDGET_VOL,
827 HDA_CTL_WIDGET_MUTE,
828 HDA_CTL_BIND_MUTE,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100829};
830static const struct snd_kcontrol_new control_templates[] = {
831 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200832 /* only the put callback is replaced for handling the special mute */
833 {
834 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
835 .subdevice = HDA_SUBDEV_AMP_FLAG,
836 .info = snd_hda_mixer_amp_switch_info,
837 .get = snd_hda_mixer_amp_switch_get,
838 .put = hda_gen_mixer_mute_put, /* replaced */
839 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
840 },
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200841 {
842 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
843 .info = snd_hda_mixer_amp_switch_info,
844 .get = snd_hda_mixer_bind_switch_get,
845 .put = hda_gen_bind_mute_put, /* replaced */
846 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
847 },
Takashi Iwai352f7f92012-12-19 12:52:06 +0100848};
849
850/* add dynamic controls from template */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100851static struct snd_kcontrol_new *
852add_control(struct hda_gen_spec *spec, int type, const char *name,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100853 int cidx, unsigned long val)
854{
855 struct snd_kcontrol_new *knew;
856
Takashi Iwai12c93df2012-12-19 14:38:33 +0100857 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100858 if (!knew)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100859 return NULL;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100860 knew->index = cidx;
861 if (get_amp_nid_(val))
862 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
863 knew->private_value = val;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100864 return knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100865}
866
867static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
868 const char *pfx, const char *dir,
869 const char *sfx, int cidx, unsigned long val)
870{
Takashi Iwai975cc022013-06-28 11:56:49 +0200871 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai352f7f92012-12-19 12:52:06 +0100872 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100873 if (!add_control(spec, type, name, cidx, val))
874 return -ENOMEM;
875 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100876}
877
878#define add_pb_vol_ctrl(spec, type, pfx, val) \
879 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
880#define add_pb_sw_ctrl(spec, type, pfx, val) \
881 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
882#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
883 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
884#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
885 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
886
887static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
888 unsigned int chs, struct nid_path *path)
889{
890 unsigned int val;
891 if (!path)
892 return 0;
893 val = path->ctls[NID_PATH_VOL_CTL];
894 if (!val)
895 return 0;
896 val = amp_val_replace_channels(val, chs);
897 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
898}
899
900/* return the channel bits suitable for the given path->ctls[] */
901static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
902 int type)
903{
904 int chs = 1; /* mono (left only) */
905 if (path) {
906 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
907 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
908 chs = 3; /* stereo */
909 }
910 return chs;
911}
912
913static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
914 struct nid_path *path)
915{
916 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
917 return add_vol_ctl(codec, pfx, cidx, chs, path);
918}
919
920/* create a mute-switch for the given mixer widget;
921 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
922 */
923static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
924 unsigned int chs, struct nid_path *path)
925{
926 unsigned int val;
927 int type = HDA_CTL_WIDGET_MUTE;
928
929 if (!path)
930 return 0;
931 val = path->ctls[NID_PATH_MUTE_CTL];
932 if (!val)
933 return 0;
934 val = amp_val_replace_channels(val, chs);
935 if (get_amp_direction_(val) == HDA_INPUT) {
936 hda_nid_t nid = get_amp_nid_(val);
937 int nums = snd_hda_get_num_conns(codec, nid);
938 if (nums > 1) {
939 type = HDA_CTL_BIND_MUTE;
940 val |= nums << 19;
941 }
942 }
943 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
944}
945
946static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
947 int cidx, struct nid_path *path)
948{
949 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
950 return add_sw_ctl(codec, pfx, cidx, chs, path);
951}
952
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200953/* playback mute control with the software mute bit check */
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200954static void sync_auto_mute_bits(struct snd_kcontrol *kcontrol,
955 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200956{
957 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
958 struct hda_gen_spec *spec = codec->spec;
959
960 if (spec->auto_mute_via_amp) {
961 hda_nid_t nid = get_amp_nid(kcontrol);
962 bool enabled = !((spec->mute_bits >> nid) & 1);
963 ucontrol->value.integer.value[0] &= enabled;
964 ucontrol->value.integer.value[1] &= enabled;
965 }
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200966}
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200967
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200968static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
969 struct snd_ctl_elem_value *ucontrol)
970{
971 sync_auto_mute_bits(kcontrol, ucontrol);
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200972 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
973}
974
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200975static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
976 struct snd_ctl_elem_value *ucontrol)
977{
978 sync_auto_mute_bits(kcontrol, ucontrol);
979 return snd_hda_mixer_bind_switch_put(kcontrol, ucontrol);
980}
981
Takashi Iwai247d85e2013-01-17 16:18:11 +0100982/* any ctl assigned to the path with the given index? */
983static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
984{
985 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
986 return path && path->ctls[ctl_type];
987}
988
Takashi Iwai352f7f92012-12-19 12:52:06 +0100989static const char * const channel_name[4] = {
990 "Front", "Surround", "CLFE", "Side"
991};
992
993/* give some appropriate ctl name prefix for the given line out channel */
Takashi Iwai247d85e2013-01-17 16:18:11 +0100994static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
995 int *index, int ctl_type)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100996{
Takashi Iwai247d85e2013-01-17 16:18:11 +0100997 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100998 struct auto_pin_cfg *cfg = &spec->autocfg;
999
1000 *index = 0;
1001 if (cfg->line_outs == 1 && !spec->multi_ios &&
Takashi Iwai247d85e2013-01-17 16:18:11 +01001002 !cfg->hp_outs && !cfg->speaker_outs)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001003 return spec->vmaster_mute.hook ? "PCM" : "Master";
1004
1005 /* if there is really a single DAC used in the whole output paths,
1006 * use it master (or "PCM" if a vmaster hook is present)
1007 */
1008 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
1009 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
1010 return spec->vmaster_mute.hook ? "PCM" : "Master";
1011
Takashi Iwai247d85e2013-01-17 16:18:11 +01001012 /* multi-io channels */
1013 if (ch >= cfg->line_outs)
1014 return channel_name[ch];
1015
Takashi Iwai352f7f92012-12-19 12:52:06 +01001016 switch (cfg->line_out_type) {
1017 case AUTO_PIN_SPEAKER_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +01001018 /* if the primary channel vol/mute is shared with HP volume,
1019 * don't name it as Speaker
1020 */
1021 if (!ch && cfg->hp_outs &&
1022 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
1023 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001024 if (cfg->line_outs == 1)
1025 return "Speaker";
1026 if (cfg->line_outs == 2)
1027 return ch ? "Bass Speaker" : "Speaker";
1028 break;
1029 case AUTO_PIN_HP_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +01001030 /* if the primary channel vol/mute is shared with spk volume,
1031 * don't name it as Headphone
1032 */
1033 if (!ch && cfg->speaker_outs &&
1034 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
1035 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001036 /* for multi-io case, only the primary out */
1037 if (ch && spec->multi_ios)
1038 break;
1039 *index = ch;
1040 return "Headphone";
David Henningsson03ad6a82014-10-16 15:33:45 +02001041 case AUTO_PIN_LINE_OUT:
1042 /* This deals with the case where we have two DACs and
1043 * one LO, one HP and one Speaker */
1044 if (!ch && cfg->speaker_outs && cfg->hp_outs) {
1045 bool hp_lo_shared = !path_has_mixer(codec, spec->hp_paths[0], ctl_type);
1046 bool spk_lo_shared = !path_has_mixer(codec, spec->speaker_paths[0], ctl_type);
1047 if (hp_lo_shared && spk_lo_shared)
1048 return spec->vmaster_mute.hook ? "PCM" : "Master";
1049 if (hp_lo_shared)
1050 return "Headphone+LO";
1051 if (spk_lo_shared)
1052 return "Speaker+LO";
1053 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001054 }
Takashi Iwai247d85e2013-01-17 16:18:11 +01001055
1056 /* for a single channel output, we don't have to name the channel */
1057 if (cfg->line_outs == 1 && !spec->multi_ios)
1058 return "PCM";
1059
Takashi Iwai352f7f92012-12-19 12:52:06 +01001060 if (ch >= ARRAY_SIZE(channel_name)) {
1061 snd_BUG();
1062 return "PCM";
1063 }
1064
1065 return channel_name[ch];
1066}
1067
1068/*
1069 * Parse output paths
1070 */
1071
1072/* badness definition */
1073enum {
1074 /* No primary DAC is found for the main output */
1075 BAD_NO_PRIMARY_DAC = 0x10000,
1076 /* No DAC is found for the extra output */
1077 BAD_NO_DAC = 0x4000,
1078 /* No possible multi-ios */
Takashi Iwai1d739062013-02-13 14:17:32 +01001079 BAD_MULTI_IO = 0x120,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001080 /* No individual DAC for extra output */
1081 BAD_NO_EXTRA_DAC = 0x102,
1082 /* No individual DAC for extra surrounds */
1083 BAD_NO_EXTRA_SURR_DAC = 0x101,
1084 /* Primary DAC shared with main surrounds */
1085 BAD_SHARED_SURROUND = 0x100,
Takashi Iwai55a63d42013-03-21 17:20:12 +01001086 /* No independent HP possible */
Takashi Iwaibec8e682013-03-22 15:10:08 +01001087 BAD_NO_INDEP_HP = 0x10,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001088 /* Primary DAC shared with main CLFE */
1089 BAD_SHARED_CLFE = 0x10,
1090 /* Primary DAC shared with extra surrounds */
1091 BAD_SHARED_EXTRA_SURROUND = 0x10,
1092 /* Volume widget is shared */
1093 BAD_SHARED_VOL = 0x10,
1094};
1095
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001096/* look for widgets in the given path which are appropriate for
Takashi Iwai352f7f92012-12-19 12:52:06 +01001097 * volume and mute controls, and assign the values to ctls[].
1098 *
1099 * When no appropriate widget is found in the path, the badness value
1100 * is incremented depending on the situation. The function returns the
1101 * total badness for both volume and mute controls.
1102 */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001103static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001104{
Takashi Iwaid89c6c02014-09-01 10:07:04 +02001105 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001106 hda_nid_t nid;
1107 unsigned int val;
1108 int badness = 0;
1109
1110 if (!path)
1111 return BAD_SHARED_VOL * 2;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001112
1113 if (path->ctls[NID_PATH_VOL_CTL] ||
1114 path->ctls[NID_PATH_MUTE_CTL])
1115 return 0; /* already evaluated */
1116
Takashi Iwai352f7f92012-12-19 12:52:06 +01001117 nid = look_for_out_vol_nid(codec, path);
1118 if (nid) {
1119 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
Takashi Iwaid89c6c02014-09-01 10:07:04 +02001120 if (spec->dac_min_mute)
1121 val |= HDA_AMP_VAL_MIN_MUTE;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001122 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1123 badness += BAD_SHARED_VOL;
1124 else
1125 path->ctls[NID_PATH_VOL_CTL] = val;
1126 } else
1127 badness += BAD_SHARED_VOL;
1128 nid = look_for_out_mute_nid(codec, path);
1129 if (nid) {
1130 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1131 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1132 nid_has_mute(codec, nid, HDA_OUTPUT))
1133 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1134 else
1135 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1136 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1137 badness += BAD_SHARED_VOL;
1138 else
1139 path->ctls[NID_PATH_MUTE_CTL] = val;
1140 } else
1141 badness += BAD_SHARED_VOL;
1142 return badness;
1143}
1144
Takashi Iwai98bd1112013-03-22 14:53:50 +01001145const struct badness_table hda_main_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001146 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1147 .no_dac = BAD_NO_DAC,
1148 .shared_primary = BAD_NO_PRIMARY_DAC,
1149 .shared_surr = BAD_SHARED_SURROUND,
1150 .shared_clfe = BAD_SHARED_CLFE,
1151 .shared_surr_main = BAD_SHARED_SURROUND,
1152};
Takashi Iwai2698ea92013-12-18 07:45:52 +01001153EXPORT_SYMBOL_GPL(hda_main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001154
Takashi Iwai98bd1112013-03-22 14:53:50 +01001155const struct badness_table hda_extra_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001156 .no_primary_dac = BAD_NO_DAC,
1157 .no_dac = BAD_NO_DAC,
1158 .shared_primary = BAD_NO_EXTRA_DAC,
1159 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1160 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1161 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1162};
Takashi Iwai2698ea92013-12-18 07:45:52 +01001163EXPORT_SYMBOL_GPL(hda_extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001164
Takashi Iwai7385df62013-01-07 09:50:52 +01001165/* get the DAC of the primary output corresponding to the given array index */
1166static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1167{
1168 struct hda_gen_spec *spec = codec->spec;
1169 struct auto_pin_cfg *cfg = &spec->autocfg;
1170
1171 if (cfg->line_outs > idx)
1172 return spec->private_dac_nids[idx];
1173 idx -= cfg->line_outs;
1174 if (spec->multi_ios > idx)
1175 return spec->multi_io[idx].dac;
1176 return 0;
1177}
1178
1179/* return the DAC if it's reachable, otherwise zero */
1180static inline hda_nid_t try_dac(struct hda_codec *codec,
1181 hda_nid_t dac, hda_nid_t pin)
1182{
1183 return is_reachable_path(codec, dac, pin) ? dac : 0;
1184}
1185
Takashi Iwai352f7f92012-12-19 12:52:06 +01001186/* try to assign DACs to pins and return the resultant badness */
1187static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1188 const hda_nid_t *pins, hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001189 int *path_idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001190 const struct badness_table *bad)
1191{
1192 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001193 int i, j;
1194 int badness = 0;
1195 hda_nid_t dac;
1196
1197 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 return 0;
1199
Takashi Iwai352f7f92012-12-19 12:52:06 +01001200 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001201 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001202 hda_nid_t pin = pins[i];
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001203
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001204 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1205 if (path) {
1206 badness += assign_out_path_ctls(codec, path);
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001207 continue;
1208 }
1209
Takashi Iwai36907392013-12-10 17:29:26 +01001210 dacs[i] = get_preferred_dac(codec, pin);
1211 if (dacs[i]) {
1212 if (is_dac_already_used(codec, dacs[i]))
1213 badness += bad->shared_primary;
1214 }
1215
1216 if (!dacs[i])
1217 dacs[i] = look_for_dac(codec, pin, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001218 if (!dacs[i] && !i) {
Takashi Iwai980428c2013-01-09 09:28:20 +01001219 /* try to steal the DAC of surrounds for the front */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001220 for (j = 1; j < num_outs; j++) {
1221 if (is_reachable_path(codec, dacs[j], pin)) {
1222 dacs[0] = dacs[j];
1223 dacs[j] = 0;
Takashi Iwai980428c2013-01-09 09:28:20 +01001224 invalidate_nid_path(codec, path_idx[j]);
Takashi Iwai196c17662013-01-04 15:01:40 +01001225 path_idx[j] = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001226 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 }
1228 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001229 }
1230 dac = dacs[i];
1231 if (!dac) {
Takashi Iwai7385df62013-01-07 09:50:52 +01001232 if (num_outs > 2)
1233 dac = try_dac(codec, get_primary_out(codec, i), pin);
1234 if (!dac)
1235 dac = try_dac(codec, dacs[0], pin);
1236 if (!dac)
1237 dac = try_dac(codec, get_primary_out(codec, i), pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001238 if (dac) {
1239 if (!i)
1240 badness += bad->shared_primary;
1241 else if (i == 1)
1242 badness += bad->shared_surr;
1243 else
1244 badness += bad->shared_clfe;
1245 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1246 dac = spec->private_dac_nids[0];
1247 badness += bad->shared_surr_main;
1248 } else if (!i)
1249 badness += bad->no_primary_dac;
1250 else
1251 badness += bad->no_dac;
1252 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001253 if (!dac)
1254 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001255 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001256 if (!path && !i && spec->mixer_nid) {
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001257 /* try with aamix */
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001258 path = snd_hda_add_new_path(codec, dac, pin, 0);
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001259 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001260 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001261 dac = dacs[i] = 0;
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001262 badness += bad->no_dac;
1263 } else {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001264 /* print_nid_path(codec, "output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001265 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001266 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001267 badness += assign_out_path_ctls(codec, path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001268 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001269 }
1270
1271 return badness;
1272}
1273
1274/* return NID if the given pin has only a single connection to a certain DAC */
1275static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1276{
1277 struct hda_gen_spec *spec = codec->spec;
1278 int i;
1279 hda_nid_t nid_found = 0;
1280
1281 for (i = 0; i < spec->num_all_dacs; i++) {
1282 hda_nid_t nid = spec->all_dacs[i];
1283 if (!nid || is_dac_already_used(codec, nid))
1284 continue;
1285 if (is_reachable_path(codec, nid, pin)) {
1286 if (nid_found)
1287 return 0;
1288 nid_found = nid;
1289 }
1290 }
1291 return nid_found;
1292}
1293
1294/* check whether the given pin can be a multi-io pin */
1295static bool can_be_multiio_pin(struct hda_codec *codec,
1296 unsigned int location, hda_nid_t nid)
1297{
1298 unsigned int defcfg, caps;
1299
1300 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1301 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1302 return false;
1303 if (location && get_defcfg_location(defcfg) != location)
1304 return false;
1305 caps = snd_hda_query_pin_caps(codec, nid);
1306 if (!(caps & AC_PINCAP_OUT))
1307 return false;
1308 return true;
1309}
1310
Takashi Iwaie22aab72013-01-04 14:50:04 +01001311/* count the number of input pins that are capable to be multi-io */
1312static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1313{
1314 struct hda_gen_spec *spec = codec->spec;
1315 struct auto_pin_cfg *cfg = &spec->autocfg;
1316 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1317 unsigned int location = get_defcfg_location(defcfg);
1318 int type, i;
1319 int num_pins = 0;
1320
1321 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1322 for (i = 0; i < cfg->num_inputs; i++) {
1323 if (cfg->inputs[i].type != type)
1324 continue;
1325 if (can_be_multiio_pin(codec, location,
1326 cfg->inputs[i].pin))
1327 num_pins++;
1328 }
1329 }
1330 return num_pins;
1331}
1332
Takashi Iwai352f7f92012-12-19 12:52:06 +01001333/*
1334 * multi-io helper
1335 *
1336 * When hardwired is set, try to fill ony hardwired pins, and returns
1337 * zero if any pins are filled, non-zero if nothing found.
1338 * When hardwired is off, try to fill possible input pins, and returns
1339 * the badness value.
1340 */
1341static int fill_multi_ios(struct hda_codec *codec,
1342 hda_nid_t reference_pin,
Takashi Iwaie22aab72013-01-04 14:50:04 +01001343 bool hardwired)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001344{
1345 struct hda_gen_spec *spec = codec->spec;
1346 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie22aab72013-01-04 14:50:04 +01001347 int type, i, j, num_pins, old_pins;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001348 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1349 unsigned int location = get_defcfg_location(defcfg);
1350 int badness = 0;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001351 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001352
1353 old_pins = spec->multi_ios;
1354 if (old_pins >= 2)
1355 goto end_fill;
1356
Takashi Iwaie22aab72013-01-04 14:50:04 +01001357 num_pins = count_multiio_pins(codec, reference_pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001358 if (num_pins < 2)
1359 goto end_fill;
1360
Takashi Iwai352f7f92012-12-19 12:52:06 +01001361 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1362 for (i = 0; i < cfg->num_inputs; i++) {
1363 hda_nid_t nid = cfg->inputs[i].pin;
1364 hda_nid_t dac = 0;
1365
1366 if (cfg->inputs[i].type != type)
1367 continue;
1368 if (!can_be_multiio_pin(codec, location, nid))
1369 continue;
1370 for (j = 0; j < spec->multi_ios; j++) {
1371 if (nid == spec->multi_io[j].pin)
1372 break;
1373 }
1374 if (j < spec->multi_ios)
1375 continue;
1376
Takashi Iwai352f7f92012-12-19 12:52:06 +01001377 if (hardwired)
1378 dac = get_dac_if_single(codec, nid);
1379 else if (!dac)
1380 dac = look_for_dac(codec, nid, false);
1381 if (!dac) {
1382 badness++;
1383 continue;
1384 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001385 path = snd_hda_add_new_path(codec, dac, nid,
1386 -spec->mixer_nid);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001387 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001388 badness++;
1389 continue;
1390 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01001391 /* print_nid_path(codec, "multiio", path); */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001392 spec->multi_io[spec->multi_ios].pin = nid;
1393 spec->multi_io[spec->multi_ios].dac = dac;
Takashi Iwai196c17662013-01-04 15:01:40 +01001394 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1395 snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001396 spec->multi_ios++;
1397 if (spec->multi_ios >= 2)
1398 break;
1399 }
1400 }
1401 end_fill:
1402 if (badness)
1403 badness = BAD_MULTI_IO;
1404 if (old_pins == spec->multi_ios) {
1405 if (hardwired)
1406 return 1; /* nothing found */
1407 else
1408 return badness; /* no badness if nothing found */
1409 }
1410 if (!hardwired && spec->multi_ios < 2) {
1411 /* cancel newly assigned paths */
1412 spec->paths.used -= spec->multi_ios - old_pins;
1413 spec->multi_ios = old_pins;
1414 return badness;
1415 }
1416
1417 /* assign volume and mute controls */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001418 for (i = old_pins; i < spec->multi_ios; i++) {
1419 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1420 badness += assign_out_path_ctls(codec, path);
1421 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001422
1423 return badness;
1424}
1425
1426/* map DACs for all pins in the list if they are single connections */
1427static bool map_singles(struct hda_codec *codec, int outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001428 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001429{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001430 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001431 int i;
1432 bool found = false;
1433 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001434 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001435 hda_nid_t dac;
1436 if (dacs[i])
1437 continue;
1438 dac = get_dac_if_single(codec, pins[i]);
1439 if (!dac)
1440 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001441 path = snd_hda_add_new_path(codec, dac, pins[i],
1442 -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001443 if (!path && !i && spec->mixer_nid)
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001444 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001445 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001446 dacs[i] = dac;
1447 found = true;
Takashi Iwai4e76a882014-02-25 12:21:03 +01001448 /* print_nid_path(codec, "output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001449 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001450 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001451 }
1452 }
1453 return found;
1454}
1455
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001456/* create a new path including aamix if available, and return its index */
1457static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1458{
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001459 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001460 struct nid_path *path;
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001461 hda_nid_t path_dac, dac, pin;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001462
1463 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001464 if (!path || !path->depth ||
1465 is_nid_contained(path, spec->mixer_nid))
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001466 return 0;
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001467 path_dac = path->path[0];
1468 dac = spec->private_dac_nids[0];
Takashi Iwaif87498b2013-01-21 14:24:31 +01001469 pin = path->path[path->depth - 1];
1470 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1471 if (!path) {
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001472 if (dac != path_dac)
1473 dac = path_dac;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001474 else if (spec->multiout.hp_out_nid[0])
1475 dac = spec->multiout.hp_out_nid[0];
1476 else if (spec->multiout.extra_out_nid[0])
1477 dac = spec->multiout.extra_out_nid[0];
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001478 else
1479 dac = 0;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001480 if (dac)
1481 path = snd_hda_add_new_path(codec, dac, pin,
1482 spec->mixer_nid);
1483 }
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001484 if (!path)
1485 return 0;
Takashi Iwai4e76a882014-02-25 12:21:03 +01001486 /* print_nid_path(codec, "output-aamix", path); */
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001487 path->active = false; /* unused as default */
1488 return snd_hda_get_path_idx(codec, path);
1489}
1490
Takashi Iwai55a63d42013-03-21 17:20:12 +01001491/* check whether the independent HP is available with the current config */
1492static bool indep_hp_possible(struct hda_codec *codec)
1493{
1494 struct hda_gen_spec *spec = codec->spec;
1495 struct auto_pin_cfg *cfg = &spec->autocfg;
1496 struct nid_path *path;
1497 int i, idx;
1498
1499 if (cfg->line_out_type == AUTO_PIN_HP_OUT)
1500 idx = spec->out_paths[0];
1501 else
1502 idx = spec->hp_paths[0];
1503 path = snd_hda_get_path_from_idx(codec, idx);
1504 if (!path)
1505 return false;
1506
1507 /* assume no path conflicts unless aamix is involved */
1508 if (!spec->mixer_nid || !is_nid_contained(path, spec->mixer_nid))
1509 return true;
1510
1511 /* check whether output paths contain aamix */
1512 for (i = 0; i < cfg->line_outs; i++) {
1513 if (spec->out_paths[i] == idx)
1514 break;
1515 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1516 if (path && is_nid_contained(path, spec->mixer_nid))
1517 return false;
1518 }
1519 for (i = 0; i < cfg->speaker_outs; i++) {
1520 path = snd_hda_get_path_from_idx(codec, spec->speaker_paths[i]);
1521 if (path && is_nid_contained(path, spec->mixer_nid))
1522 return false;
1523 }
1524
1525 return true;
1526}
1527
Takashi Iwaia07a9492013-01-07 16:44:06 +01001528/* fill the empty entries in the dac array for speaker/hp with the
1529 * shared dac pointed by the paths
1530 */
1531static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1532 hda_nid_t *dacs, int *path_idx)
1533{
1534 struct nid_path *path;
1535 int i;
1536
1537 for (i = 0; i < num_outs; i++) {
1538 if (dacs[i])
1539 continue;
1540 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1541 if (!path)
1542 continue;
1543 dacs[i] = path->path[0];
1544 }
1545}
1546
Takashi Iwai352f7f92012-12-19 12:52:06 +01001547/* fill in the dac_nids table from the parsed pin configuration */
1548static int fill_and_eval_dacs(struct hda_codec *codec,
1549 bool fill_hardwired,
1550 bool fill_mio_first)
1551{
1552 struct hda_gen_spec *spec = codec->spec;
1553 struct auto_pin_cfg *cfg = &spec->autocfg;
1554 int i, err, badness;
1555
1556 /* set num_dacs once to full for look_for_dac() */
1557 spec->multiout.num_dacs = cfg->line_outs;
1558 spec->multiout.dac_nids = spec->private_dac_nids;
1559 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1560 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1561 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1562 spec->multi_ios = 0;
1563 snd_array_free(&spec->paths);
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001564
1565 /* clear path indices */
1566 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1567 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1568 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1569 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1570 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
Takashi Iwaic697b712013-01-07 17:09:26 +01001571 memset(spec->input_paths, 0, sizeof(spec->input_paths));
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001572 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1573 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1574
Takashi Iwai352f7f92012-12-19 12:52:06 +01001575 badness = 0;
1576
1577 /* fill hard-wired DACs first */
1578 if (fill_hardwired) {
1579 bool mapped;
1580 do {
1581 mapped = map_singles(codec, cfg->line_outs,
1582 cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001583 spec->private_dac_nids,
1584 spec->out_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001585 mapped |= map_singles(codec, cfg->hp_outs,
1586 cfg->hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001587 spec->multiout.hp_out_nid,
1588 spec->hp_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001589 mapped |= map_singles(codec, cfg->speaker_outs,
1590 cfg->speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001591 spec->multiout.extra_out_nid,
1592 spec->speaker_paths);
Takashi Iwaida96fb52013-07-29 16:54:36 +02001593 if (!spec->no_multi_io &&
1594 fill_mio_first && cfg->line_outs == 1 &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01001595 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001596 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001597 if (!err)
1598 mapped = true;
1599 }
1600 } while (mapped);
1601 }
1602
1603 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001604 spec->private_dac_nids, spec->out_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001605 spec->main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001606
Takashi Iwaida96fb52013-07-29 16:54:36 +02001607 if (!spec->no_multi_io && fill_mio_first &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01001608 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1609 /* try to fill multi-io first */
Takashi Iwaie22aab72013-01-04 14:50:04 +01001610 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001611 if (err < 0)
1612 return err;
1613 /* we don't count badness at this stage yet */
1614 }
1615
1616 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1617 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1618 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001619 spec->hp_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001620 spec->extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001621 if (err < 0)
1622 return err;
1623 badness += err;
1624 }
1625 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1626 err = try_assign_dacs(codec, cfg->speaker_outs,
1627 cfg->speaker_pins,
1628 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001629 spec->speaker_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001630 spec->extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001631 if (err < 0)
1632 return err;
1633 badness += err;
1634 }
Takashi Iwaida96fb52013-07-29 16:54:36 +02001635 if (!spec->no_multi_io &&
1636 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001637 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001638 if (err < 0)
1639 return err;
1640 badness += err;
1641 }
Takashi Iwaie22aab72013-01-04 14:50:04 +01001642
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001643 if (spec->mixer_nid) {
1644 spec->aamix_out_paths[0] =
1645 check_aamix_out_path(codec, spec->out_paths[0]);
1646 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1647 spec->aamix_out_paths[1] =
1648 check_aamix_out_path(codec, spec->hp_paths[0]);
1649 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1650 spec->aamix_out_paths[2] =
1651 check_aamix_out_path(codec, spec->speaker_paths[0]);
1652 }
1653
Takashi Iwaida96fb52013-07-29 16:54:36 +02001654 if (!spec->no_multi_io &&
1655 cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
Takashi Iwaie22aab72013-01-04 14:50:04 +01001656 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1657 spec->multi_ios = 1; /* give badness */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001658
Takashi Iwaia07a9492013-01-07 16:44:06 +01001659 /* re-count num_dacs and squash invalid entries */
1660 spec->multiout.num_dacs = 0;
1661 for (i = 0; i < cfg->line_outs; i++) {
1662 if (spec->private_dac_nids[i])
1663 spec->multiout.num_dacs++;
1664 else {
1665 memmove(spec->private_dac_nids + i,
1666 spec->private_dac_nids + i + 1,
1667 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1668 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1669 }
1670 }
1671
1672 spec->ext_channel_count = spec->min_channel_count =
David Henningssonc0f3b212013-01-16 11:45:37 +01001673 spec->multiout.num_dacs * 2;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001674
Takashi Iwai352f7f92012-12-19 12:52:06 +01001675 if (spec->multi_ios == 2) {
1676 for (i = 0; i < 2; i++)
1677 spec->private_dac_nids[spec->multiout.num_dacs++] =
1678 spec->multi_io[i].dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001679 } else if (spec->multi_ios) {
1680 spec->multi_ios = 0;
1681 badness += BAD_MULTI_IO;
1682 }
1683
Takashi Iwai55a63d42013-03-21 17:20:12 +01001684 if (spec->indep_hp && !indep_hp_possible(codec))
1685 badness += BAD_NO_INDEP_HP;
1686
Takashi Iwaia07a9492013-01-07 16:44:06 +01001687 /* re-fill the shared DAC for speaker / headphone */
1688 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1689 refill_shared_dacs(codec, cfg->hp_outs,
1690 spec->multiout.hp_out_nid,
1691 spec->hp_paths);
1692 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1693 refill_shared_dacs(codec, cfg->speaker_outs,
1694 spec->multiout.extra_out_nid,
1695 spec->speaker_paths);
1696
Takashi Iwai352f7f92012-12-19 12:52:06 +01001697 return badness;
1698}
1699
1700#define DEBUG_BADNESS
1701
1702#ifdef DEBUG_BADNESS
Joe Perchesd82353e2014-07-05 13:02:02 -07001703#define debug_badness(fmt, ...) \
1704 codec_dbg(codec, fmt, ##__VA_ARGS__)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001705#else
Joe Perchesd82353e2014-07-05 13:02:02 -07001706#define debug_badness(fmt, ...) \
1707 do { if (0) codec_dbg(codec, fmt, ##__VA_ARGS__); } while (0)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001708#endif
1709
Takashi Iwaia7694092013-01-21 10:43:18 +01001710#ifdef DEBUG_BADNESS
1711static inline void print_nid_path_idx(struct hda_codec *codec,
1712 const char *pfx, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001713{
Takashi Iwaia7694092013-01-21 10:43:18 +01001714 struct nid_path *path;
1715
1716 path = snd_hda_get_path_from_idx(codec, idx);
1717 if (path)
Takashi Iwai4e76a882014-02-25 12:21:03 +01001718 print_nid_path(codec, pfx, path);
Takashi Iwaia7694092013-01-21 10:43:18 +01001719}
1720
1721static void debug_show_configs(struct hda_codec *codec,
1722 struct auto_pin_cfg *cfg)
1723{
1724 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia7694092013-01-21 10:43:18 +01001725 static const char * const lo_type[3] = { "LO", "SP", "HP" };
Takashi Iwaia7694092013-01-21 10:43:18 +01001726 int i;
1727
1728 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001729 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001730 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001731 spec->multiout.dac_nids[0],
1732 spec->multiout.dac_nids[1],
1733 spec->multiout.dac_nids[2],
Takashi Iwaia7694092013-01-21 10:43:18 +01001734 spec->multiout.dac_nids[3],
1735 lo_type[cfg->line_out_type]);
1736 for (i = 0; i < cfg->line_outs; i++)
1737 print_nid_path_idx(codec, " out", spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001738 if (spec->multi_ios > 0)
1739 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1740 spec->multi_ios,
1741 spec->multi_io[0].pin, spec->multi_io[1].pin,
1742 spec->multi_io[0].dac, spec->multi_io[1].dac);
Takashi Iwaia7694092013-01-21 10:43:18 +01001743 for (i = 0; i < spec->multi_ios; i++)
1744 print_nid_path_idx(codec, " mio",
1745 spec->out_paths[cfg->line_outs + i]);
1746 if (cfg->hp_outs)
1747 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001748 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001749 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001750 spec->multiout.hp_out_nid[0],
1751 spec->multiout.hp_out_nid[1],
1752 spec->multiout.hp_out_nid[2],
1753 spec->multiout.hp_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001754 for (i = 0; i < cfg->hp_outs; i++)
1755 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1756 if (cfg->speaker_outs)
1757 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001758 cfg->speaker_pins[0], cfg->speaker_pins[1],
1759 cfg->speaker_pins[2], cfg->speaker_pins[3],
1760 spec->multiout.extra_out_nid[0],
1761 spec->multiout.extra_out_nid[1],
1762 spec->multiout.extra_out_nid[2],
1763 spec->multiout.extra_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001764 for (i = 0; i < cfg->speaker_outs; i++)
1765 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1766 for (i = 0; i < 3; i++)
1767 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001768}
Takashi Iwaia7694092013-01-21 10:43:18 +01001769#else
1770#define debug_show_configs(codec, cfg) /* NOP */
1771#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01001772
1773/* find all available DACs of the codec */
1774static void fill_all_dac_nids(struct hda_codec *codec)
1775{
1776 struct hda_gen_spec *spec = codec->spec;
1777 int i;
1778 hda_nid_t nid = codec->start_nid;
1779
1780 spec->num_all_dacs = 0;
1781 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1782 for (i = 0; i < codec->num_nodes; i++, nid++) {
1783 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1784 continue;
1785 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001786 codec_err(codec, "Too many DACs!\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001787 break;
1788 }
1789 spec->all_dacs[spec->num_all_dacs++] = nid;
1790 }
1791}
1792
1793static int parse_output_paths(struct hda_codec *codec)
1794{
1795 struct hda_gen_spec *spec = codec->spec;
1796 struct auto_pin_cfg *cfg = &spec->autocfg;
1797 struct auto_pin_cfg *best_cfg;
Takashi Iwai9314a582013-01-21 10:49:05 +01001798 unsigned int val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001799 int best_badness = INT_MAX;
1800 int badness;
1801 bool fill_hardwired = true, fill_mio_first = true;
1802 bool best_wired = true, best_mio = true;
1803 bool hp_spk_swapped = false;
1804
Takashi Iwai352f7f92012-12-19 12:52:06 +01001805 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1806 if (!best_cfg)
1807 return -ENOMEM;
1808 *best_cfg = *cfg;
1809
1810 for (;;) {
1811 badness = fill_and_eval_dacs(codec, fill_hardwired,
1812 fill_mio_first);
1813 if (badness < 0) {
1814 kfree(best_cfg);
1815 return badness;
1816 }
1817 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1818 cfg->line_out_type, fill_hardwired, fill_mio_first,
1819 badness);
Takashi Iwaia7694092013-01-21 10:43:18 +01001820 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001821 if (badness < best_badness) {
1822 best_badness = badness;
1823 *best_cfg = *cfg;
1824 best_wired = fill_hardwired;
1825 best_mio = fill_mio_first;
1826 }
1827 if (!badness)
1828 break;
1829 fill_mio_first = !fill_mio_first;
1830 if (!fill_mio_first)
1831 continue;
1832 fill_hardwired = !fill_hardwired;
1833 if (!fill_hardwired)
1834 continue;
1835 if (hp_spk_swapped)
1836 break;
1837 hp_spk_swapped = true;
1838 if (cfg->speaker_outs > 0 &&
1839 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1840 cfg->hp_outs = cfg->line_outs;
1841 memcpy(cfg->hp_pins, cfg->line_out_pins,
1842 sizeof(cfg->hp_pins));
1843 cfg->line_outs = cfg->speaker_outs;
1844 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1845 sizeof(cfg->speaker_pins));
1846 cfg->speaker_outs = 0;
1847 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1848 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1849 fill_hardwired = true;
1850 continue;
1851 }
1852 if (cfg->hp_outs > 0 &&
1853 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1854 cfg->speaker_outs = cfg->line_outs;
1855 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1856 sizeof(cfg->speaker_pins));
1857 cfg->line_outs = cfg->hp_outs;
1858 memcpy(cfg->line_out_pins, cfg->hp_pins,
1859 sizeof(cfg->hp_pins));
1860 cfg->hp_outs = 0;
1861 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1862 cfg->line_out_type = AUTO_PIN_HP_OUT;
1863 fill_hardwired = true;
1864 continue;
1865 }
1866 break;
1867 }
1868
1869 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001870 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001871 *cfg = *best_cfg;
1872 fill_and_eval_dacs(codec, best_wired, best_mio);
1873 }
1874 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1875 cfg->line_out_type, best_wired, best_mio);
Takashi Iwaia7694092013-01-21 10:43:18 +01001876 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001877
1878 if (cfg->line_out_pins[0]) {
1879 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01001880 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001881 if (path)
1882 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
Takashi Iwaid89c6c02014-09-01 10:07:04 +02001883 if (spec->vmaster_nid) {
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01001884 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
1885 HDA_OUTPUT, spec->vmaster_tlv);
Takashi Iwaid89c6c02014-09-01 10:07:04 +02001886 if (spec->dac_min_mute)
1887 spec->vmaster_tlv[3] |= TLV_DB_SCALE_MUTE;
1888 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001889 }
1890
Takashi Iwai9314a582013-01-21 10:49:05 +01001891 /* set initial pinctl targets */
1892 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
1893 val = PIN_HP;
1894 else
1895 val = PIN_OUT;
1896 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
1897 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1898 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
1899 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1900 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
1901 set_pin_targets(codec, cfg->speaker_outs,
1902 cfg->speaker_pins, val);
1903 }
1904
Takashi Iwai55a63d42013-03-21 17:20:12 +01001905 /* clear indep_hp flag if not available */
1906 if (spec->indep_hp && !indep_hp_possible(codec))
1907 spec->indep_hp = 0;
1908
Takashi Iwai352f7f92012-12-19 12:52:06 +01001909 kfree(best_cfg);
1910 return 0;
1911}
1912
1913/* add playback controls from the parsed DAC table */
1914static int create_multi_out_ctls(struct hda_codec *codec,
1915 const struct auto_pin_cfg *cfg)
1916{
1917 struct hda_gen_spec *spec = codec->spec;
1918 int i, err, noutputs;
1919
1920 noutputs = cfg->line_outs;
1921 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1922 noutputs += spec->multi_ios;
1923
1924 for (i = 0; i < noutputs; i++) {
1925 const char *name;
1926 int index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001927 struct nid_path *path;
1928
Takashi Iwai196c17662013-01-04 15:01:40 +01001929 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001930 if (!path)
1931 continue;
Takashi Iwai247d85e2013-01-17 16:18:11 +01001932
1933 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001934 if (!name || !strcmp(name, "CLFE")) {
1935 /* Center/LFE */
1936 err = add_vol_ctl(codec, "Center", 0, 1, path);
1937 if (err < 0)
1938 return err;
1939 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1940 if (err < 0)
1941 return err;
Takashi Iwai247d85e2013-01-17 16:18:11 +01001942 } else {
1943 err = add_stereo_vol(codec, name, index, path);
1944 if (err < 0)
1945 return err;
1946 }
1947
1948 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
1949 if (!name || !strcmp(name, "CLFE")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001950 err = add_sw_ctl(codec, "Center", 0, 1, path);
1951 if (err < 0)
1952 return err;
1953 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1954 if (err < 0)
1955 return err;
1956 } else {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001957 err = add_stereo_sw(codec, name, index, path);
1958 if (err < 0)
1959 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 }
1961 }
1962 return 0;
1963}
1964
Takashi Iwaic2c80382013-01-07 10:33:57 +01001965static int create_extra_out(struct hda_codec *codec, int path_idx,
Takashi Iwai196c17662013-01-04 15:01:40 +01001966 const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001968 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 int err;
1970
Takashi Iwai196c17662013-01-04 15:01:40 +01001971 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001972 if (!path)
1973 return 0;
Takashi Iwaic2c80382013-01-07 10:33:57 +01001974 err = add_stereo_vol(codec, pfx, cidx, path);
1975 if (err < 0)
1976 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001977 err = add_stereo_sw(codec, pfx, cidx, path);
1978 if (err < 0)
1979 return err;
1980 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981}
1982
Takashi Iwai352f7f92012-12-19 12:52:06 +01001983/* add playback controls for speaker and HP outputs */
1984static int create_extra_outs(struct hda_codec *codec, int num_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001985 const int *paths, const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986{
Takashi Iwaic2c80382013-01-07 10:33:57 +01001987 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001988
1989 for (i = 0; i < num_pins; i++) {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001990 const char *name;
Takashi Iwai975cc022013-06-28 11:56:49 +02001991 char tmp[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwaic2c80382013-01-07 10:33:57 +01001992 int err, idx = 0;
1993
1994 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
1995 name = "Bass Speaker";
1996 else if (num_pins >= 3) {
1997 snprintf(tmp, sizeof(tmp), "%s %s",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001998 pfx, channel_name[i]);
Takashi Iwaic2c80382013-01-07 10:33:57 +01001999 name = tmp;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002000 } else {
Takashi Iwaic2c80382013-01-07 10:33:57 +01002001 name = pfx;
2002 idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 }
Takashi Iwaic2c80382013-01-07 10:33:57 +01002004 err = create_extra_out(codec, paths[i], name, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002005 if (err < 0)
2006 return err;
2007 }
2008 return 0;
2009}
Takashi Iwai97ec5582006-03-21 11:29:07 +01002010
Takashi Iwai352f7f92012-12-19 12:52:06 +01002011static int create_hp_out_ctls(struct hda_codec *codec)
2012{
2013 struct hda_gen_spec *spec = codec->spec;
2014 return create_extra_outs(codec, spec->autocfg.hp_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01002015 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002016 "Headphone");
2017}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
Takashi Iwai352f7f92012-12-19 12:52:06 +01002019static int create_speaker_out_ctls(struct hda_codec *codec)
2020{
2021 struct hda_gen_spec *spec = codec->spec;
2022 return create_extra_outs(codec, spec->autocfg.speaker_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01002023 spec->speaker_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002024 "Speaker");
2025}
2026
2027/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002028 * independent HP controls
2029 */
2030
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02002031static void call_hp_automute(struct hda_codec *codec,
2032 struct hda_jack_callback *jack);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002033static int indep_hp_info(struct snd_kcontrol *kcontrol,
2034 struct snd_ctl_elem_info *uinfo)
2035{
2036 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2037}
2038
2039static int indep_hp_get(struct snd_kcontrol *kcontrol,
2040 struct snd_ctl_elem_value *ucontrol)
2041{
2042 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2043 struct hda_gen_spec *spec = codec->spec;
2044 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
2045 return 0;
2046}
2047
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002048static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2049 int nomix_path_idx, int mix_path_idx,
2050 int out_type);
2051
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002052static int indep_hp_put(struct snd_kcontrol *kcontrol,
2053 struct snd_ctl_elem_value *ucontrol)
2054{
2055 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2056 struct hda_gen_spec *spec = codec->spec;
2057 unsigned int select = ucontrol->value.enumerated.item[0];
2058 int ret = 0;
2059
2060 mutex_lock(&spec->pcm_mutex);
2061 if (spec->active_streams) {
2062 ret = -EBUSY;
2063 goto unlock;
2064 }
2065
2066 if (spec->indep_hp_enabled != select) {
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002067 hda_nid_t *dacp;
2068 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2069 dacp = &spec->private_dac_nids[0];
2070 else
2071 dacp = &spec->multiout.hp_out_nid[0];
2072
2073 /* update HP aamix paths in case it conflicts with indep HP */
2074 if (spec->have_aamix_ctl) {
2075 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2076 update_aamix_paths(codec, spec->aamix_mode,
2077 spec->out_paths[0],
2078 spec->aamix_out_paths[0],
2079 spec->autocfg.line_out_type);
2080 else
2081 update_aamix_paths(codec, spec->aamix_mode,
2082 spec->hp_paths[0],
2083 spec->aamix_out_paths[1],
2084 AUTO_PIN_HP_OUT);
2085 }
2086
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002087 spec->indep_hp_enabled = select;
2088 if (spec->indep_hp_enabled)
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002089 *dacp = 0;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002090 else
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002091 *dacp = spec->alt_dac_nid;
Takashi Iwai92603c52013-01-22 07:46:31 +01002092
Takashi Iwai963afde2013-05-31 15:20:31 +02002093 call_hp_automute(codec, NULL);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002094 ret = 1;
2095 }
2096 unlock:
2097 mutex_unlock(&spec->pcm_mutex);
2098 return ret;
2099}
2100
2101static const struct snd_kcontrol_new indep_hp_ctl = {
2102 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2103 .name = "Independent HP",
2104 .info = indep_hp_info,
2105 .get = indep_hp_get,
2106 .put = indep_hp_put,
2107};
2108
2109
2110static int create_indep_hp_ctls(struct hda_codec *codec)
2111{
2112 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002113 hda_nid_t dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002114
2115 if (!spec->indep_hp)
2116 return 0;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002117 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2118 dac = spec->multiout.dac_nids[0];
2119 else
2120 dac = spec->multiout.hp_out_nid[0];
2121 if (!dac) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002122 spec->indep_hp = 0;
2123 return 0;
2124 }
2125
2126 spec->indep_hp_enabled = false;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002127 spec->alt_dac_nid = dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002128 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
2129 return -ENOMEM;
2130 return 0;
2131}
2132
2133/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002134 * channel mode enum control
2135 */
2136
2137static int ch_mode_info(struct snd_kcontrol *kcontrol,
2138 struct snd_ctl_elem_info *uinfo)
2139{
2140 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2141 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002142 int chs;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002143
2144 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2145 uinfo->count = 1;
2146 uinfo->value.enumerated.items = spec->multi_ios + 1;
2147 if (uinfo->value.enumerated.item > spec->multi_ios)
2148 uinfo->value.enumerated.item = spec->multi_ios;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002149 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2150 sprintf(uinfo->value.enumerated.name, "%dch", chs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002151 return 0;
2152}
2153
2154static int ch_mode_get(struct snd_kcontrol *kcontrol,
2155 struct snd_ctl_elem_value *ucontrol)
2156{
2157 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2158 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002159 ucontrol->value.enumerated.item[0] =
2160 (spec->ext_channel_count - spec->min_channel_count) / 2;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002161 return 0;
2162}
2163
Takashi Iwai196c17662013-01-04 15:01:40 +01002164static inline struct nid_path *
2165get_multiio_path(struct hda_codec *codec, int idx)
2166{
2167 struct hda_gen_spec *spec = codec->spec;
2168 return snd_hda_get_path_from_idx(codec,
2169 spec->out_paths[spec->autocfg.line_outs + idx]);
2170}
2171
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002172static void update_automute_all(struct hda_codec *codec);
2173
Takashi Iwai65033cc2013-04-16 12:31:05 +02002174/* Default value to be passed as aamix argument for snd_hda_activate_path();
2175 * used for output paths
2176 */
2177static bool aamix_default(struct hda_gen_spec *spec)
2178{
2179 return !spec->have_aamix_ctl || spec->aamix_mode;
2180}
2181
Takashi Iwai352f7f92012-12-19 12:52:06 +01002182static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2183{
2184 struct hda_gen_spec *spec = codec->spec;
2185 hda_nid_t nid = spec->multi_io[idx].pin;
2186 struct nid_path *path;
2187
Takashi Iwai196c17662013-01-04 15:01:40 +01002188 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002189 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002191
2192 if (path->active == output)
2193 return 0;
2194
2195 if (output) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01002196 set_pin_target(codec, nid, PIN_OUT, true);
Takashi Iwai65033cc2013-04-16 12:31:05 +02002197 snd_hda_activate_path(codec, path, true, aamix_default(spec));
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002198 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002199 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002200 set_pin_eapd(codec, nid, false);
Takashi Iwai65033cc2013-04-16 12:31:05 +02002201 snd_hda_activate_path(codec, path, false, aamix_default(spec));
Takashi Iwai2c12c302013-01-10 09:33:29 +01002202 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002203 path_power_down_sync(codec, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 }
Takashi Iwaia365fed2013-01-10 16:10:06 +01002205
2206 /* update jack retasking in case it modifies any of them */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002207 update_automute_all(codec);
Takashi Iwaia365fed2013-01-10 16:10:06 +01002208
Takashi Iwai352f7f92012-12-19 12:52:06 +01002209 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210}
2211
Takashi Iwai352f7f92012-12-19 12:52:06 +01002212static int ch_mode_put(struct snd_kcontrol *kcontrol,
2213 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002215 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2216 struct hda_gen_spec *spec = codec->spec;
2217 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218
Takashi Iwai352f7f92012-12-19 12:52:06 +01002219 ch = ucontrol->value.enumerated.item[0];
2220 if (ch < 0 || ch > spec->multi_ios)
2221 return -EINVAL;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002222 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002223 return 0;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002224 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002225 for (i = 0; i < spec->multi_ios; i++)
2226 set_multi_io(codec, i, i < ch);
2227 spec->multiout.max_channels = max(spec->ext_channel_count,
2228 spec->const_channel_count);
2229 if (spec->need_dac_fix)
2230 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 return 1;
2232}
2233
Takashi Iwai352f7f92012-12-19 12:52:06 +01002234static const struct snd_kcontrol_new channel_mode_enum = {
2235 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2236 .name = "Channel Mode",
2237 .info = ch_mode_info,
2238 .get = ch_mode_get,
2239 .put = ch_mode_put,
2240};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241
Takashi Iwai352f7f92012-12-19 12:52:06 +01002242static int create_multi_channel_mode(struct hda_codec *codec)
2243{
2244 struct hda_gen_spec *spec = codec->spec;
2245
2246 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002247 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002248 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 return 0;
2251}
2252
Takashi Iwai352f7f92012-12-19 12:52:06 +01002253/*
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002254 * aamix loopback enable/disable switch
2255 */
2256
2257#define loopback_mixing_info indep_hp_info
2258
2259static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2260 struct snd_ctl_elem_value *ucontrol)
2261{
2262 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2263 struct hda_gen_spec *spec = codec->spec;
2264 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2265 return 0;
2266}
2267
2268static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002269 int nomix_path_idx, int mix_path_idx,
2270 int out_type)
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002271{
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002272 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002273 struct nid_path *nomix_path, *mix_path;
2274
2275 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2276 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2277 if (!nomix_path || !mix_path)
2278 return;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002279
2280 /* if HP aamix path is driven from a different DAC and the
2281 * independent HP mode is ON, can't turn on aamix path
2282 */
2283 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2284 mix_path->path[0] != spec->alt_dac_nid)
2285 do_mix = false;
2286
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002287 if (do_mix) {
2288 snd_hda_activate_path(codec, nomix_path, false, true);
2289 snd_hda_activate_path(codec, mix_path, true, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002290 path_power_down_sync(codec, nomix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002291 } else {
Takashi Iwai65033cc2013-04-16 12:31:05 +02002292 snd_hda_activate_path(codec, mix_path, false, false);
2293 snd_hda_activate_path(codec, nomix_path, true, false);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002294 path_power_down_sync(codec, mix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002295 }
2296}
2297
2298static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2299 struct snd_ctl_elem_value *ucontrol)
2300{
2301 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2302 struct hda_gen_spec *spec = codec->spec;
2303 unsigned int val = ucontrol->value.enumerated.item[0];
2304
2305 if (val == spec->aamix_mode)
2306 return 0;
2307 spec->aamix_mode = val;
2308 update_aamix_paths(codec, val, spec->out_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002309 spec->aamix_out_paths[0],
2310 spec->autocfg.line_out_type);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002311 update_aamix_paths(codec, val, spec->hp_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002312 spec->aamix_out_paths[1],
2313 AUTO_PIN_HP_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002314 update_aamix_paths(codec, val, spec->speaker_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002315 spec->aamix_out_paths[2],
2316 AUTO_PIN_SPEAKER_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002317 return 1;
2318}
2319
2320static const struct snd_kcontrol_new loopback_mixing_enum = {
2321 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2322 .name = "Loopback Mixing",
2323 .info = loopback_mixing_info,
2324 .get = loopback_mixing_get,
2325 .put = loopback_mixing_put,
2326};
2327
2328static int create_loopback_mixing_ctl(struct hda_codec *codec)
2329{
2330 struct hda_gen_spec *spec = codec->spec;
2331
2332 if (!spec->mixer_nid)
2333 return 0;
2334 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
2335 spec->aamix_out_paths[2]))
2336 return 0;
2337 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2338 return -ENOMEM;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002339 spec->have_aamix_ctl = 1;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002340 return 0;
2341}
2342
2343/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002344 * shared headphone/mic handling
2345 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002346
Takashi Iwai352f7f92012-12-19 12:52:06 +01002347static void call_update_outputs(struct hda_codec *codec);
2348
2349/* for shared I/O, change the pin-control accordingly */
Takashi Iwai967303d2013-02-19 17:12:42 +01002350static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002351{
2352 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai967303d2013-02-19 17:12:42 +01002353 bool as_mic;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002354 unsigned int val;
Takashi Iwai967303d2013-02-19 17:12:42 +01002355 hda_nid_t pin;
2356
2357 pin = spec->hp_mic_pin;
2358 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
2359
2360 if (!force) {
2361 val = snd_hda_codec_get_pin_target(codec, pin);
2362 if (as_mic) {
2363 if (val & PIN_IN)
2364 return;
2365 } else {
2366 if (val & PIN_OUT)
2367 return;
2368 }
2369 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002370
2371 val = snd_hda_get_default_vref(codec, pin);
Takashi Iwai967303d2013-02-19 17:12:42 +01002372 /* if the HP pin doesn't support VREF and the codec driver gives an
2373 * alternative pin, set up the VREF on that pin instead
2374 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002375 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2376 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2377 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2378 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01002379 snd_hda_set_pin_ctl_cache(codec, vref_pin,
Takashi Iwai967303d2013-02-19 17:12:42 +01002380 PIN_IN | (as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02002381 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002382
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002383 if (!spec->hp_mic_jack_modes) {
2384 if (as_mic)
2385 val |= PIN_IN;
2386 else
2387 val = PIN_HP;
2388 set_pin_target(codec, pin, val, true);
Takashi Iwai963afde2013-05-31 15:20:31 +02002389 call_hp_automute(codec, NULL);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002390 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002391}
2392
2393/* create a shared input with the headphone out */
Takashi Iwai967303d2013-02-19 17:12:42 +01002394static int create_hp_mic(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002395{
2396 struct hda_gen_spec *spec = codec->spec;
2397 struct auto_pin_cfg *cfg = &spec->autocfg;
2398 unsigned int defcfg;
2399 hda_nid_t nid;
2400
Takashi Iwai967303d2013-02-19 17:12:42 +01002401 if (!spec->hp_mic) {
2402 if (spec->suppress_hp_mic_detect)
2403 return 0;
2404 /* automatic detection: only if no input or a single internal
2405 * input pin is found, try to detect the shared hp/mic
2406 */
2407 if (cfg->num_inputs > 1)
2408 return 0;
2409 else if (cfg->num_inputs == 1) {
2410 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2411 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2412 return 0;
2413 }
2414 }
2415
2416 spec->hp_mic = 0; /* clear once */
2417 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002418 return 0;
2419
Takashi Iwai967303d2013-02-19 17:12:42 +01002420 nid = 0;
2421 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2422 nid = cfg->line_out_pins[0];
2423 else if (cfg->hp_outs > 0)
2424 nid = cfg->hp_pins[0];
2425 if (!nid)
2426 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002427
2428 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2429 return 0; /* no input */
2430
Takashi Iwai967303d2013-02-19 17:12:42 +01002431 cfg->inputs[cfg->num_inputs].pin = nid;
2432 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
David Henningssoncb420b12013-04-11 11:30:28 +02002433 cfg->inputs[cfg->num_inputs].is_headphone_mic = 1;
Takashi Iwai967303d2013-02-19 17:12:42 +01002434 cfg->num_inputs++;
2435 spec->hp_mic = 1;
2436 spec->hp_mic_pin = nid;
2437 /* we can't handle auto-mic together with HP-mic */
2438 spec->suppress_auto_mic = 1;
Takashi Iwai4e76a882014-02-25 12:21:03 +01002439 codec_dbg(codec, "Enable shared I/O jack on NID 0x%x\n", nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002440 return 0;
2441}
2442
Takashi Iwai978e77e2013-01-10 16:57:58 +01002443/*
2444 * output jack mode
2445 */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002446
2447static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2448
2449static const char * const out_jack_texts[] = {
2450 "Line Out", "Headphone Out",
2451};
2452
Takashi Iwai978e77e2013-01-10 16:57:58 +01002453static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2454 struct snd_ctl_elem_info *uinfo)
2455{
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002456 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
Takashi Iwai978e77e2013-01-10 16:57:58 +01002457}
2458
2459static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2460 struct snd_ctl_elem_value *ucontrol)
2461{
2462 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2463 hda_nid_t nid = kcontrol->private_value;
2464 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2465 ucontrol->value.enumerated.item[0] = 1;
2466 else
2467 ucontrol->value.enumerated.item[0] = 0;
2468 return 0;
2469}
2470
2471static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2472 struct snd_ctl_elem_value *ucontrol)
2473{
2474 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2475 hda_nid_t nid = kcontrol->private_value;
2476 unsigned int val;
2477
2478 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2479 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2480 return 0;
2481 snd_hda_set_pin_ctl_cache(codec, nid, val);
2482 return 1;
2483}
2484
2485static const struct snd_kcontrol_new out_jack_mode_enum = {
2486 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2487 .info = out_jack_mode_info,
2488 .get = out_jack_mode_get,
2489 .put = out_jack_mode_put,
2490};
2491
2492static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2493{
2494 struct hda_gen_spec *spec = codec->spec;
2495 int i;
2496
2497 for (i = 0; i < spec->kctls.used; i++) {
2498 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2499 if (!strcmp(kctl->name, name) && kctl->index == idx)
2500 return true;
2501 }
2502 return false;
2503}
2504
2505static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2506 char *name, size_t name_len)
2507{
2508 struct hda_gen_spec *spec = codec->spec;
2509 int idx = 0;
2510
2511 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2512 strlcat(name, " Jack Mode", name_len);
2513
2514 for (; find_kctl_name(codec, name, idx); idx++)
2515 ;
2516}
2517
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002518static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2519{
2520 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002521 if (spec->add_jack_modes) {
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002522 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2523 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2524 return 2;
2525 }
2526 return 1;
2527}
2528
Takashi Iwai978e77e2013-01-10 16:57:58 +01002529static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2530 hda_nid_t *pins)
2531{
2532 struct hda_gen_spec *spec = codec->spec;
2533 int i;
2534
2535 for (i = 0; i < num_pins; i++) {
2536 hda_nid_t pin = pins[i];
Takashi Iwaiced4cef2013-11-26 08:33:45 +01002537 if (pin == spec->hp_mic_pin)
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002538 continue;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002539 if (get_out_jack_num_items(codec, pin) > 1) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01002540 struct snd_kcontrol_new *knew;
Takashi Iwai975cc022013-06-28 11:56:49 +02002541 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai978e77e2013-01-10 16:57:58 +01002542 get_jack_mode_name(codec, pin, name, sizeof(name));
2543 knew = snd_hda_gen_add_kctl(spec, name,
2544 &out_jack_mode_enum);
2545 if (!knew)
2546 return -ENOMEM;
2547 knew->private_value = pin;
2548 }
2549 }
2550
2551 return 0;
2552}
2553
Takashi Iwai294765582013-01-17 09:52:11 +01002554/*
2555 * input jack mode
2556 */
2557
2558/* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2559#define NUM_VREFS 6
2560
2561static const char * const vref_texts[NUM_VREFS] = {
2562 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2563 "", "Mic 80pc Bias", "Mic 100pc Bias"
2564};
2565
2566static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2567{
2568 unsigned int pincap;
2569
2570 pincap = snd_hda_query_pin_caps(codec, pin);
2571 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2572 /* filter out unusual vrefs */
2573 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2574 return pincap;
2575}
2576
2577/* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2578static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2579{
2580 unsigned int i, n = 0;
2581
2582 for (i = 0; i < NUM_VREFS; i++) {
2583 if (vref_caps & (1 << i)) {
2584 if (n == item_idx)
2585 return i;
2586 n++;
2587 }
2588 }
2589 return 0;
2590}
2591
2592/* convert back from the vref ctl index to the enum item index */
2593static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2594{
2595 unsigned int i, n = 0;
2596
2597 for (i = 0; i < NUM_VREFS; i++) {
2598 if (i == idx)
2599 return n;
2600 if (vref_caps & (1 << i))
2601 n++;
2602 }
2603 return 0;
2604}
2605
2606static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2607 struct snd_ctl_elem_info *uinfo)
2608{
2609 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2610 hda_nid_t nid = kcontrol->private_value;
2611 unsigned int vref_caps = get_vref_caps(codec, nid);
2612
2613 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2614 vref_texts);
2615 /* set the right text */
2616 strcpy(uinfo->value.enumerated.name,
2617 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2618 return 0;
2619}
2620
2621static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2622 struct snd_ctl_elem_value *ucontrol)
2623{
2624 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2625 hda_nid_t nid = kcontrol->private_value;
2626 unsigned int vref_caps = get_vref_caps(codec, nid);
2627 unsigned int idx;
2628
2629 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2630 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2631 return 0;
2632}
2633
2634static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2635 struct snd_ctl_elem_value *ucontrol)
2636{
2637 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2638 hda_nid_t nid = kcontrol->private_value;
2639 unsigned int vref_caps = get_vref_caps(codec, nid);
2640 unsigned int val, idx;
2641
2642 val = snd_hda_codec_get_pin_target(codec, nid);
2643 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2644 if (idx == ucontrol->value.enumerated.item[0])
2645 return 0;
2646
2647 val &= ~AC_PINCTL_VREFEN;
2648 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2649 snd_hda_set_pin_ctl_cache(codec, nid, val);
2650 return 1;
2651}
2652
2653static const struct snd_kcontrol_new in_jack_mode_enum = {
2654 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2655 .info = in_jack_mode_info,
2656 .get = in_jack_mode_get,
2657 .put = in_jack_mode_put,
2658};
2659
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002660static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2661{
2662 struct hda_gen_spec *spec = codec->spec;
2663 int nitems = 0;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002664 if (spec->add_jack_modes)
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002665 nitems = hweight32(get_vref_caps(codec, pin));
2666 return nitems ? nitems : 1;
2667}
2668
Takashi Iwai294765582013-01-17 09:52:11 +01002669static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2670{
2671 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai294765582013-01-17 09:52:11 +01002672 struct snd_kcontrol_new *knew;
Takashi Iwai975cc022013-06-28 11:56:49 +02002673 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002674 unsigned int defcfg;
2675
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002676 if (pin == spec->hp_mic_pin)
2677 return 0; /* already done in create_out_jack_mode() */
Takashi Iwai294765582013-01-17 09:52:11 +01002678
2679 /* no jack mode for fixed pins */
2680 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2681 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2682 return 0;
2683
2684 /* no multiple vref caps? */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002685 if (get_in_jack_num_items(codec, pin) <= 1)
Takashi Iwai294765582013-01-17 09:52:11 +01002686 return 0;
2687
2688 get_jack_mode_name(codec, pin, name, sizeof(name));
2689 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2690 if (!knew)
2691 return -ENOMEM;
2692 knew->private_value = pin;
2693 return 0;
2694}
2695
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002696/*
2697 * HP/mic shared jack mode
2698 */
2699static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2700 struct snd_ctl_elem_info *uinfo)
2701{
2702 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2703 hda_nid_t nid = kcontrol->private_value;
2704 int out_jacks = get_out_jack_num_items(codec, nid);
2705 int in_jacks = get_in_jack_num_items(codec, nid);
2706 const char *text = NULL;
2707 int idx;
2708
2709 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2710 uinfo->count = 1;
2711 uinfo->value.enumerated.items = out_jacks + in_jacks;
2712 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2713 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2714 idx = uinfo->value.enumerated.item;
2715 if (idx < out_jacks) {
2716 if (out_jacks > 1)
2717 text = out_jack_texts[idx];
2718 else
2719 text = "Headphone Out";
2720 } else {
2721 idx -= out_jacks;
2722 if (in_jacks > 1) {
2723 unsigned int vref_caps = get_vref_caps(codec, nid);
2724 text = vref_texts[get_vref_idx(vref_caps, idx)];
2725 } else
2726 text = "Mic In";
2727 }
2728
2729 strcpy(uinfo->value.enumerated.name, text);
2730 return 0;
2731}
2732
2733static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2734{
2735 int out_jacks = get_out_jack_num_items(codec, nid);
2736 int in_jacks = get_in_jack_num_items(codec, nid);
2737 unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2738 int idx = 0;
2739
2740 if (val & PIN_OUT) {
2741 if (out_jacks > 1 && val == PIN_HP)
2742 idx = 1;
2743 } else if (val & PIN_IN) {
2744 idx = out_jacks;
2745 if (in_jacks > 1) {
2746 unsigned int vref_caps = get_vref_caps(codec, nid);
2747 val &= AC_PINCTL_VREFEN;
2748 idx += cvt_from_vref_idx(vref_caps, val);
2749 }
2750 }
2751 return idx;
2752}
2753
2754static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2755 struct snd_ctl_elem_value *ucontrol)
2756{
2757 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2758 hda_nid_t nid = kcontrol->private_value;
2759 ucontrol->value.enumerated.item[0] =
2760 get_cur_hp_mic_jack_mode(codec, nid);
2761 return 0;
2762}
2763
2764static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2765 struct snd_ctl_elem_value *ucontrol)
2766{
2767 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2768 hda_nid_t nid = kcontrol->private_value;
2769 int out_jacks = get_out_jack_num_items(codec, nid);
2770 int in_jacks = get_in_jack_num_items(codec, nid);
2771 unsigned int val, oldval, idx;
2772
2773 oldval = get_cur_hp_mic_jack_mode(codec, nid);
2774 idx = ucontrol->value.enumerated.item[0];
2775 if (oldval == idx)
2776 return 0;
2777
2778 if (idx < out_jacks) {
2779 if (out_jacks > 1)
2780 val = idx ? PIN_HP : PIN_OUT;
2781 else
2782 val = PIN_HP;
2783 } else {
2784 idx -= out_jacks;
2785 if (in_jacks > 1) {
2786 unsigned int vref_caps = get_vref_caps(codec, nid);
2787 val = snd_hda_codec_get_pin_target(codec, nid);
Takashi Iwai3f550e32013-03-07 18:30:27 +01002788 val &= ~(AC_PINCTL_VREFEN | PIN_HP);
2789 val |= get_vref_idx(vref_caps, idx) | PIN_IN;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002790 } else
Takashi Iwai16c0cefe2013-11-26 08:44:26 +01002791 val = snd_hda_get_default_vref(codec, nid) | PIN_IN;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002792 }
2793 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwai963afde2013-05-31 15:20:31 +02002794 call_hp_automute(codec, NULL);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002795
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002796 return 1;
2797}
2798
2799static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
2800 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2801 .info = hp_mic_jack_mode_info,
2802 .get = hp_mic_jack_mode_get,
2803 .put = hp_mic_jack_mode_put,
2804};
2805
2806static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2807{
2808 struct hda_gen_spec *spec = codec->spec;
2809 struct snd_kcontrol_new *knew;
2810
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002811 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
2812 &hp_mic_jack_mode_enum);
2813 if (!knew)
2814 return -ENOMEM;
2815 knew->private_value = pin;
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002816 spec->hp_mic_jack_modes = 1;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002817 return 0;
2818}
Takashi Iwai352f7f92012-12-19 12:52:06 +01002819
2820/*
2821 * Parse input paths
2822 */
2823
Takashi Iwai352f7f92012-12-19 12:52:06 +01002824/* add the powersave loopback-list entry */
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002825static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002826{
2827 struct hda_amp_list *list;
2828
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002829 list = snd_array_new(&spec->loopback_list);
2830 if (!list)
2831 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002832 list->nid = mix;
2833 list->dir = HDA_INPUT;
2834 list->idx = idx;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002835 spec->loopback.amplist = spec->loopback_list.list;
2836 return 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002837}
Takashi Iwaicb53c622007-08-10 17:21:45 +02002838
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002839/* return true if either a volume or a mute amp is found for the given
2840 * aamix path; the amp has to be either in the mixer node or its direct leaf
2841 */
2842static bool look_for_mix_leaf_ctls(struct hda_codec *codec, hda_nid_t mix_nid,
2843 hda_nid_t pin, unsigned int *mix_val,
2844 unsigned int *mute_val)
2845{
2846 int idx, num_conns;
2847 const hda_nid_t *list;
2848 hda_nid_t nid;
2849
2850 idx = snd_hda_get_conn_index(codec, mix_nid, pin, true);
2851 if (idx < 0)
2852 return false;
2853
2854 *mix_val = *mute_val = 0;
2855 if (nid_has_volume(codec, mix_nid, HDA_INPUT))
2856 *mix_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2857 if (nid_has_mute(codec, mix_nid, HDA_INPUT))
2858 *mute_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2859 if (*mix_val && *mute_val)
2860 return true;
2861
2862 /* check leaf node */
2863 num_conns = snd_hda_get_conn_list(codec, mix_nid, &list);
2864 if (num_conns < idx)
2865 return false;
2866 nid = list[idx];
Takashi Iwai43a8e502014-01-07 18:11:44 +01002867 if (!*mix_val && nid_has_volume(codec, nid, HDA_OUTPUT) &&
2868 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_VOL_CTL))
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002869 *mix_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
Takashi Iwai43a8e502014-01-07 18:11:44 +01002870 if (!*mute_val && nid_has_mute(codec, nid, HDA_OUTPUT) &&
2871 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_MUTE_CTL))
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002872 *mute_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2873
2874 return *mix_val || *mute_val;
2875}
2876
Takashi Iwai352f7f92012-12-19 12:52:06 +01002877/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01002878static int new_analog_input(struct hda_codec *codec, int input_idx,
2879 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002880 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002882 struct hda_gen_spec *spec = codec->spec;
2883 struct nid_path *path;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002884 unsigned int mix_val, mute_val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002885 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002887 if (!look_for_mix_leaf_ctls(codec, mix_nid, pin, &mix_val, &mute_val))
2888 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002889
Takashi Iwai3ca529d2013-01-07 17:25:08 +01002890 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002891 if (!path)
2892 return -EINVAL;
Takashi Iwai4e76a882014-02-25 12:21:03 +01002893 print_nid_path(codec, "loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01002894 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002895
2896 idx = path->idx[path->depth - 1];
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002897 if (mix_val) {
2898 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, mix_val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002899 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 return err;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002901 path->ctls[NID_PATH_VOL_CTL] = mix_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 }
2903
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002904 if (mute_val) {
2905 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, mute_val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002906 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 return err;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002908 path->ctls[NID_PATH_MUTE_CTL] = mute_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 }
2910
Takashi Iwai352f7f92012-12-19 12:52:06 +01002911 path->active = true;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002912 err = add_loopback_list(spec, mix_nid, idx);
2913 if (err < 0)
2914 return err;
Takashi Iwaie4a395e2013-01-23 17:00:31 +01002915
2916 if (spec->mixer_nid != spec->mixer_merge_nid &&
2917 !spec->loopback_merge_path) {
2918 path = snd_hda_add_new_path(codec, spec->mixer_nid,
2919 spec->mixer_merge_nid, 0);
2920 if (path) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002921 print_nid_path(codec, "loopback-merge", path);
Takashi Iwaie4a395e2013-01-23 17:00:31 +01002922 path->active = true;
2923 spec->loopback_merge_path =
2924 snd_hda_get_path_idx(codec, path);
2925 }
2926 }
2927
Takashi Iwai352f7f92012-12-19 12:52:06 +01002928 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929}
2930
Takashi Iwai352f7f92012-12-19 12:52:06 +01002931static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002933 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
2934 return (pincap & AC_PINCAP_IN) != 0;
2935}
2936
2937/* Parse the codec tree and retrieve ADCs */
2938static int fill_adc_nids(struct hda_codec *codec)
2939{
2940 struct hda_gen_spec *spec = codec->spec;
2941 hda_nid_t nid;
2942 hda_nid_t *adc_nids = spec->adc_nids;
2943 int max_nums = ARRAY_SIZE(spec->adc_nids);
2944 int i, nums = 0;
2945
2946 nid = codec->start_nid;
2947 for (i = 0; i < codec->num_nodes; i++, nid++) {
2948 unsigned int caps = get_wcaps(codec, nid);
2949 int type = get_wcaps_type(caps);
2950
2951 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2952 continue;
2953 adc_nids[nums] = nid;
2954 if (++nums >= max_nums)
2955 break;
2956 }
2957 spec->num_adc_nids = nums;
Takashi Iwai0ffd5342013-01-17 15:53:29 +01002958
2959 /* copy the detected ADCs to all_adcs[] */
2960 spec->num_all_adcs = nums;
2961 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
2962
Takashi Iwai352f7f92012-12-19 12:52:06 +01002963 return nums;
2964}
2965
2966/* filter out invalid adc_nids that don't give all active input pins;
2967 * if needed, check whether dynamic ADC-switching is available
2968 */
2969static int check_dyn_adc_switch(struct hda_codec *codec)
2970{
2971 struct hda_gen_spec *spec = codec->spec;
2972 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002973 unsigned int ok_bits;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002974 int i, n, nums;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002975
Takashi Iwai352f7f92012-12-19 12:52:06 +01002976 nums = 0;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002977 ok_bits = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002978 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002979 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002980 if (!spec->input_paths[i][n])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002981 break;
2982 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002983 if (i >= imux->num_items) {
2984 ok_bits |= (1 << n);
2985 nums++;
2986 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002987 }
2988
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002989 if (!ok_bits) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002990 /* check whether ADC-switch is possible */
2991 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002992 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002993 if (spec->input_paths[i][n]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002994 spec->dyn_adc_idx[i] = n;
2995 break;
2996 }
2997 }
2998 }
2999
Takashi Iwai4e76a882014-02-25 12:21:03 +01003000 codec_dbg(codec, "enabling ADC switching\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01003001 spec->dyn_adc_switch = 1;
3002 } else if (nums != spec->num_adc_nids) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003003 /* shrink the invalid adcs and input paths */
3004 nums = 0;
3005 for (n = 0; n < spec->num_adc_nids; n++) {
3006 if (!(ok_bits & (1 << n)))
3007 continue;
3008 if (n != nums) {
3009 spec->adc_nids[nums] = spec->adc_nids[n];
Takashi Iwai980428c2013-01-09 09:28:20 +01003010 for (i = 0; i < imux->num_items; i++) {
3011 invalidate_nid_path(codec,
3012 spec->input_paths[i][nums]);
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003013 spec->input_paths[i][nums] =
3014 spec->input_paths[i][n];
Takashi Iwai980428c2013-01-09 09:28:20 +01003015 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003016 }
3017 nums++;
3018 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003019 spec->num_adc_nids = nums;
3020 }
3021
Takashi Iwai967303d2013-02-19 17:12:42 +01003022 if (imux->num_items == 1 ||
3023 (imux->num_items == 2 && spec->hp_mic)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003024 codec_dbg(codec, "reducing to a single ADC\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01003025 spec->num_adc_nids = 1; /* reduce to a single ADC */
3026 }
3027
3028 /* single index for individual volumes ctls */
3029 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
3030 spec->num_adc_nids = 1;
3031
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 return 0;
3033}
3034
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003035/* parse capture source paths from the given pin and create imux items */
3036static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
Takashi Iwai9dba2052013-01-18 10:01:15 +01003037 int cfg_idx, int num_adcs,
3038 const char *label, int anchor)
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003039{
3040 struct hda_gen_spec *spec = codec->spec;
3041 struct hda_input_mux *imux = &spec->input_mux;
3042 int imux_idx = imux->num_items;
3043 bool imux_added = false;
3044 int c;
3045
3046 for (c = 0; c < num_adcs; c++) {
3047 struct nid_path *path;
3048 hda_nid_t adc = spec->adc_nids[c];
3049
3050 if (!is_reachable_path(codec, pin, adc))
3051 continue;
3052 path = snd_hda_add_new_path(codec, pin, adc, anchor);
3053 if (!path)
3054 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01003055 print_nid_path(codec, "input", path);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003056 spec->input_paths[imux_idx][c] =
3057 snd_hda_get_path_idx(codec, path);
3058
3059 if (!imux_added) {
Takashi Iwai967303d2013-02-19 17:12:42 +01003060 if (spec->hp_mic_pin == pin)
3061 spec->hp_mic_mux_idx = imux->num_items;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003062 spec->imux_pins[imux->num_items] = pin;
Takashi Iwai6194b992014-06-06 18:12:16 +02003063 snd_hda_add_imux_item(codec, imux, label, cfg_idx, NULL);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003064 imux_added = true;
Takashi Iwaif1e762d2013-12-09 16:02:24 +01003065 if (spec->dyn_adc_switch)
3066 spec->dyn_adc_idx[imux_idx] = c;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003067 }
3068 }
3069
3070 return 0;
3071}
3072
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003074 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 */
Takashi Iwai9dba2052013-01-18 10:01:15 +01003076
Takashi Iwaic9700422013-01-18 10:17:30 +01003077/* fill the label for each input at first */
3078static int fill_input_pin_labels(struct hda_codec *codec)
3079{
3080 struct hda_gen_spec *spec = codec->spec;
3081 const struct auto_pin_cfg *cfg = &spec->autocfg;
3082 int i;
3083
3084 for (i = 0; i < cfg->num_inputs; i++) {
3085 hda_nid_t pin = cfg->inputs[i].pin;
3086 const char *label;
3087 int j, idx;
3088
3089 if (!is_input_pin(codec, pin))
3090 continue;
3091
3092 label = hda_get_autocfg_input_label(codec, cfg, i);
3093 idx = 0;
David Henningsson8e8db7f2013-01-18 15:43:02 +01003094 for (j = i - 1; j >= 0; j--) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003095 if (spec->input_labels[j] &&
3096 !strcmp(spec->input_labels[j], label)) {
3097 idx = spec->input_label_idxs[j] + 1;
3098 break;
3099 }
3100 }
3101
3102 spec->input_labels[i] = label;
3103 spec->input_label_idxs[i] = idx;
3104 }
3105
3106 return 0;
3107}
3108
Takashi Iwai9dba2052013-01-18 10:01:15 +01003109#define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
3110
Takashi Iwai352f7f92012-12-19 12:52:06 +01003111static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003112{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003113 struct hda_gen_spec *spec = codec->spec;
3114 const struct auto_pin_cfg *cfg = &spec->autocfg;
3115 hda_nid_t mixer = spec->mixer_nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003116 int num_adcs;
Takashi Iwaic9700422013-01-18 10:17:30 +01003117 int i, err;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003118 unsigned int val;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003119
Takashi Iwai352f7f92012-12-19 12:52:06 +01003120 num_adcs = fill_adc_nids(codec);
3121 if (num_adcs < 0)
3122 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003123
Takashi Iwaic9700422013-01-18 10:17:30 +01003124 err = fill_input_pin_labels(codec);
3125 if (err < 0)
3126 return err;
3127
Takashi Iwai352f7f92012-12-19 12:52:06 +01003128 for (i = 0; i < cfg->num_inputs; i++) {
3129 hda_nid_t pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130
Takashi Iwai352f7f92012-12-19 12:52:06 +01003131 pin = cfg->inputs[i].pin;
3132 if (!is_input_pin(codec, pin))
3133 continue;
3134
Takashi Iwai2c12c302013-01-10 09:33:29 +01003135 val = PIN_IN;
3136 if (cfg->inputs[i].type == AUTO_PIN_MIC)
3137 val |= snd_hda_get_default_vref(codec, pin);
Takashi Iwai93c9d8a2013-03-11 09:48:43 +01003138 if (pin != spec->hp_mic_pin)
3139 set_pin_target(codec, pin, val, false);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003140
Takashi Iwai352f7f92012-12-19 12:52:06 +01003141 if (mixer) {
3142 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01003143 err = new_analog_input(codec, i, pin,
Takashi Iwaic9700422013-01-18 10:17:30 +01003144 spec->input_labels[i],
3145 spec->input_label_idxs[i],
3146 mixer);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003147 if (err < 0)
3148 return err;
3149 }
3150 }
3151
Takashi Iwaic9700422013-01-18 10:17:30 +01003152 err = parse_capture_source(codec, pin, i, num_adcs,
3153 spec->input_labels[i], -mixer);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003154 if (err < 0)
3155 return err;
Takashi Iwai294765582013-01-17 09:52:11 +01003156
Takashi Iwaif811c3c2013-03-07 18:32:59 +01003157 if (spec->add_jack_modes) {
Takashi Iwai294765582013-01-17 09:52:11 +01003158 err = create_in_jack_mode(codec, pin);
3159 if (err < 0)
3160 return err;
3161 }
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003162 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003163
Takashi Iwaif1e762d2013-12-09 16:02:24 +01003164 /* add stereo mix when explicitly enabled via hint */
3165 if (mixer && spec->add_stereo_mix_input &&
3166 snd_hda_get_bool_hint(codec, "add_stereo_mix_input") > 0) {
Takashi Iwai9dba2052013-01-18 10:01:15 +01003167 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003168 "Stereo Mix", 0);
3169 if (err < 0)
3170 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003171 }
3172
3173 return 0;
3174}
3175
3176
3177/*
3178 * input source mux
3179 */
3180
Takashi Iwaic697b712013-01-07 17:09:26 +01003181/* get the input path specified by the given adc and imux indices */
3182static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003183{
3184 struct hda_gen_spec *spec = codec->spec;
David Henningssonb56fa1e2013-01-16 11:45:35 +01003185 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3186 snd_BUG();
3187 return NULL;
3188 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003189 if (spec->dyn_adc_switch)
3190 adc_idx = spec->dyn_adc_idx[imux_idx];
David Henningssond3d982f2013-01-18 15:43:01 +01003191 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
David Henningssonb56fa1e2013-01-16 11:45:35 +01003192 snd_BUG();
3193 return NULL;
3194 }
Takashi Iwaic697b712013-01-07 17:09:26 +01003195 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003196}
3197
3198static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3199 unsigned int idx);
3200
3201static int mux_enum_info(struct snd_kcontrol *kcontrol,
3202 struct snd_ctl_elem_info *uinfo)
3203{
3204 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3205 struct hda_gen_spec *spec = codec->spec;
3206 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3207}
3208
3209static int mux_enum_get(struct snd_kcontrol *kcontrol,
3210 struct snd_ctl_elem_value *ucontrol)
3211{
3212 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3213 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003214 /* the ctls are created at once with multiple counts */
3215 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003216
3217 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3218 return 0;
3219}
3220
3221static int mux_enum_put(struct snd_kcontrol *kcontrol,
3222 struct snd_ctl_elem_value *ucontrol)
3223{
3224 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003225 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003226 return mux_select(codec, adc_idx,
3227 ucontrol->value.enumerated.item[0]);
3228}
3229
Takashi Iwai352f7f92012-12-19 12:52:06 +01003230static const struct snd_kcontrol_new cap_src_temp = {
3231 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3232 .name = "Input Source",
3233 .info = mux_enum_info,
3234 .get = mux_enum_get,
3235 .put = mux_enum_put,
3236};
3237
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003238/*
3239 * capture volume and capture switch ctls
3240 */
3241
Takashi Iwai352f7f92012-12-19 12:52:06 +01003242typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3243 struct snd_ctl_elem_value *ucontrol);
3244
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003245/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003246static int cap_put_caller(struct snd_kcontrol *kcontrol,
3247 struct snd_ctl_elem_value *ucontrol,
3248 put_call_t func, int type)
3249{
3250 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3251 struct hda_gen_spec *spec = codec->spec;
3252 const struct hda_input_mux *imux;
3253 struct nid_path *path;
3254 int i, adc_idx, err = 0;
3255
3256 imux = &spec->input_mux;
David Henningssona053d1e2013-01-16 11:45:36 +01003257 adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003258 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003259 /* we use the cache-only update at first since multiple input paths
3260 * may shared the same amp; by updating only caches, the redundant
3261 * writes to hardware can be reduced.
3262 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003263 codec->cached_write = 1;
3264 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003265 path = get_input_path(codec, adc_idx, i);
3266 if (!path || !path->ctls[type])
Takashi Iwai352f7f92012-12-19 12:52:06 +01003267 continue;
3268 kcontrol->private_value = path->ctls[type];
3269 err = func(kcontrol, ucontrol);
3270 if (err < 0)
3271 goto error;
3272 }
3273 error:
3274 codec->cached_write = 0;
3275 mutex_unlock(&codec->control_mutex);
Takashi Iwaidc870f32013-01-22 15:24:30 +01003276 snd_hda_codec_flush_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003277 if (err >= 0 && spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01003278 spec->cap_sync_hook(codec, kcontrol, ucontrol);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003279 return err;
3280}
3281
3282/* capture volume ctl callbacks */
3283#define cap_vol_info snd_hda_mixer_amp_volume_info
3284#define cap_vol_get snd_hda_mixer_amp_volume_get
3285#define cap_vol_tlv snd_hda_mixer_amp_tlv
3286
3287static int cap_vol_put(struct snd_kcontrol *kcontrol,
3288 struct snd_ctl_elem_value *ucontrol)
3289{
3290 return cap_put_caller(kcontrol, ucontrol,
3291 snd_hda_mixer_amp_volume_put,
3292 NID_PATH_VOL_CTL);
3293}
3294
3295static const struct snd_kcontrol_new cap_vol_temp = {
3296 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3297 .name = "Capture Volume",
3298 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3299 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3300 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3301 .info = cap_vol_info,
3302 .get = cap_vol_get,
3303 .put = cap_vol_put,
3304 .tlv = { .c = cap_vol_tlv },
3305};
3306
3307/* capture switch ctl callbacks */
3308#define cap_sw_info snd_ctl_boolean_stereo_info
3309#define cap_sw_get snd_hda_mixer_amp_switch_get
3310
3311static int cap_sw_put(struct snd_kcontrol *kcontrol,
3312 struct snd_ctl_elem_value *ucontrol)
3313{
Takashi Iwaia90229e2013-01-18 14:10:00 +01003314 return cap_put_caller(kcontrol, ucontrol,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003315 snd_hda_mixer_amp_switch_put,
3316 NID_PATH_MUTE_CTL);
3317}
3318
3319static const struct snd_kcontrol_new cap_sw_temp = {
3320 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3321 .name = "Capture Switch",
3322 .info = cap_sw_info,
3323 .get = cap_sw_get,
3324 .put = cap_sw_put,
3325};
3326
3327static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3328{
3329 hda_nid_t nid;
3330 int i, depth;
3331
3332 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3333 for (depth = 0; depth < 3; depth++) {
3334 if (depth >= path->depth)
3335 return -EINVAL;
3336 i = path->depth - depth - 1;
3337 nid = path->path[i];
3338 if (!path->ctls[NID_PATH_VOL_CTL]) {
3339 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3340 path->ctls[NID_PATH_VOL_CTL] =
3341 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3342 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3343 int idx = path->idx[i];
3344 if (!depth && codec->single_adc_amp)
3345 idx = 0;
3346 path->ctls[NID_PATH_VOL_CTL] =
3347 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3348 }
3349 }
3350 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3351 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3352 path->ctls[NID_PATH_MUTE_CTL] =
3353 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3354 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3355 int idx = path->idx[i];
3356 if (!depth && codec->single_adc_amp)
3357 idx = 0;
3358 path->ctls[NID_PATH_MUTE_CTL] =
3359 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3360 }
3361 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01003362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 return 0;
3364}
3365
Takashi Iwai352f7f92012-12-19 12:52:06 +01003366static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003368 struct hda_gen_spec *spec = codec->spec;
3369 struct auto_pin_cfg *cfg = &spec->autocfg;
3370 unsigned int val;
3371 int i;
3372
3373 if (!spec->inv_dmic_split)
3374 return false;
3375 for (i = 0; i < cfg->num_inputs; i++) {
3376 if (cfg->inputs[i].pin != nid)
3377 continue;
3378 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3379 return false;
3380 val = snd_hda_codec_get_pincfg(codec, nid);
3381 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3382 }
3383 return false;
3384}
3385
Takashi Iwaia90229e2013-01-18 14:10:00 +01003386/* capture switch put callback for a single control with hook call */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003387static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3388 struct snd_ctl_elem_value *ucontrol)
3389{
3390 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3391 struct hda_gen_spec *spec = codec->spec;
3392 int ret;
3393
3394 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3395 if (ret < 0)
3396 return ret;
3397
Takashi Iwaia90229e2013-01-18 14:10:00 +01003398 if (spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01003399 spec->cap_sync_hook(codec, kcontrol, ucontrol);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003400
3401 return ret;
3402}
3403
Takashi Iwai352f7f92012-12-19 12:52:06 +01003404static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3405 int idx, bool is_switch, unsigned int ctl,
3406 bool inv_dmic)
3407{
3408 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai975cc022013-06-28 11:56:49 +02003409 char tmpname[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003410 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3411 const char *sfx = is_switch ? "Switch" : "Volume";
3412 unsigned int chs = inv_dmic ? 1 : 3;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003413 struct snd_kcontrol_new *knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003414
3415 if (!ctl)
3416 return 0;
3417
3418 if (label)
3419 snprintf(tmpname, sizeof(tmpname),
3420 "%s Capture %s", label, sfx);
3421 else
3422 snprintf(tmpname, sizeof(tmpname),
3423 "Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003424 knew = add_control(spec, type, tmpname, idx,
3425 amp_val_replace_channels(ctl, chs));
3426 if (!knew)
3427 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003428 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003429 knew->put = cap_single_sw_put;
3430 if (!inv_dmic)
3431 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003432
3433 /* Make independent right kcontrol */
3434 if (label)
3435 snprintf(tmpname, sizeof(tmpname),
3436 "Inverted %s Capture %s", label, sfx);
3437 else
3438 snprintf(tmpname, sizeof(tmpname),
3439 "Inverted Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003440 knew = add_control(spec, type, tmpname, idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003441 amp_val_replace_channels(ctl, 2));
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003442 if (!knew)
3443 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003444 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003445 knew->put = cap_single_sw_put;
3446 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003447}
3448
3449/* create single (and simple) capture volume and switch controls */
3450static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3451 unsigned int vol_ctl, unsigned int sw_ctl,
3452 bool inv_dmic)
3453{
3454 int err;
3455 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3456 if (err < 0)
3457 return err;
3458 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3459 if (err < 0)
3460 return err;
3461 return 0;
3462}
3463
3464/* create bound capture volume and switch controls */
3465static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3466 unsigned int vol_ctl, unsigned int sw_ctl)
3467{
3468 struct hda_gen_spec *spec = codec->spec;
3469 struct snd_kcontrol_new *knew;
3470
3471 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003472 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003473 if (!knew)
3474 return -ENOMEM;
3475 knew->index = idx;
3476 knew->private_value = vol_ctl;
3477 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3478 }
3479 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003480 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003481 if (!knew)
3482 return -ENOMEM;
3483 knew->index = idx;
3484 knew->private_value = sw_ctl;
3485 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3486 }
3487 return 0;
3488}
3489
3490/* return the vol ctl when used first in the imux list */
3491static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3492{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003493 struct nid_path *path;
3494 unsigned int ctl;
3495 int i;
3496
Takashi Iwaic697b712013-01-07 17:09:26 +01003497 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003498 if (!path)
3499 return 0;
3500 ctl = path->ctls[type];
3501 if (!ctl)
3502 return 0;
3503 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003504 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003505 if (path && path->ctls[type] == ctl)
3506 return 0;
3507 }
3508 return ctl;
3509}
3510
3511/* create individual capture volume and switch controls per input */
3512static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3513{
3514 struct hda_gen_spec *spec = codec->spec;
3515 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaic9700422013-01-18 10:17:30 +01003516 int i, err, type;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003517
3518 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003519 bool inv_dmic;
Takashi Iwaic9700422013-01-18 10:17:30 +01003520 int idx;
Takashi Iwai9dba2052013-01-18 10:01:15 +01003521
Takashi Iwaic9700422013-01-18 10:17:30 +01003522 idx = imux->items[i].index;
3523 if (idx >= spec->autocfg.num_inputs)
Takashi Iwai9dba2052013-01-18 10:01:15 +01003524 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003525 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3526
3527 for (type = 0; type < 2; type++) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003528 err = add_single_cap_ctl(codec,
3529 spec->input_labels[idx],
3530 spec->input_label_idxs[idx],
3531 type,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003532 get_first_cap_ctl(codec, i, type),
3533 inv_dmic);
3534 if (err < 0)
3535 return err;
3536 }
3537 }
3538 return 0;
3539}
3540
3541static int create_capture_mixers(struct hda_codec *codec)
3542{
3543 struct hda_gen_spec *spec = codec->spec;
3544 struct hda_input_mux *imux = &spec->input_mux;
3545 int i, n, nums, err;
3546
3547 if (spec->dyn_adc_switch)
3548 nums = 1;
3549 else
3550 nums = spec->num_adc_nids;
3551
3552 if (!spec->auto_mic && imux->num_items > 1) {
3553 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01003554 const char *name;
3555 name = nums > 1 ? "Input Source" : "Capture Source";
3556 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003557 if (!knew)
3558 return -ENOMEM;
3559 knew->count = nums;
3560 }
3561
3562 for (n = 0; n < nums; n++) {
3563 bool multi = false;
David Henningsson99a55922013-01-16 15:58:44 +01003564 bool multi_cap_vol = spec->multi_cap_vol;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003565 bool inv_dmic = false;
3566 int vol, sw;
3567
3568 vol = sw = 0;
3569 for (i = 0; i < imux->num_items; i++) {
3570 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01003571 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003572 if (!path)
3573 continue;
3574 parse_capvol_in_path(codec, path);
3575 if (!vol)
3576 vol = path->ctls[NID_PATH_VOL_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003577 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003578 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003579 if (!same_amp_caps(codec, vol,
3580 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3581 multi_cap_vol = true;
3582 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003583 if (!sw)
3584 sw = path->ctls[NID_PATH_MUTE_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003585 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003586 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003587 if (!same_amp_caps(codec, sw,
3588 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3589 multi_cap_vol = true;
3590 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003591 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3592 inv_dmic = true;
3593 }
3594
3595 if (!multi)
3596 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3597 inv_dmic);
David Henningssonccb04152013-10-14 10:16:22 +02003598 else if (!multi_cap_vol && !inv_dmic)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003599 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3600 else
3601 err = create_multi_cap_vol_ctl(codec);
3602 if (err < 0)
3603 return err;
3604 }
3605
3606 return 0;
3607}
3608
3609/*
3610 * add mic boosts if needed
3611 */
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003612
3613/* check whether the given amp is feasible as a boost volume */
3614static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3615 int dir, int idx)
3616{
3617 unsigned int step;
3618
3619 if (!nid_has_volume(codec, nid, dir) ||
3620 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3621 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3622 return false;
3623
3624 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3625 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3626 if (step < 0x20)
3627 return false;
3628 return true;
3629}
3630
3631/* look for a boost amp in a widget close to the pin */
3632static unsigned int look_for_boost_amp(struct hda_codec *codec,
3633 struct nid_path *path)
3634{
3635 unsigned int val = 0;
3636 hda_nid_t nid;
3637 int depth;
3638
3639 for (depth = 0; depth < 3; depth++) {
3640 if (depth >= path->depth - 1)
3641 break;
3642 nid = path->path[depth];
3643 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3644 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3645 break;
3646 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3647 path->idx[depth])) {
3648 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3649 HDA_INPUT);
3650 break;
3651 }
3652 }
3653
3654 return val;
3655}
3656
Takashi Iwai352f7f92012-12-19 12:52:06 +01003657static int parse_mic_boost(struct hda_codec *codec)
3658{
3659 struct hda_gen_spec *spec = codec->spec;
3660 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003661 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003662 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003663
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003664 if (!spec->num_adc_nids)
3665 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003666
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003667 for (i = 0; i < imux->num_items; i++) {
3668 struct nid_path *path;
3669 unsigned int val;
3670 int idx;
Takashi Iwai975cc022013-06-28 11:56:49 +02003671 char boost_label[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
David Henningsson02aba552013-01-16 15:58:43 +01003672
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003673 idx = imux->items[i].index;
3674 if (idx >= imux->num_items)
3675 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003676
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003677 /* check only line-in and mic pins */
Takashi Iwai1799cdd52013-01-18 14:37:16 +01003678 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003679 continue;
3680
3681 path = get_input_path(codec, 0, i);
3682 if (!path)
3683 continue;
3684
3685 val = look_for_boost_amp(codec, path);
3686 if (!val)
3687 continue;
3688
3689 /* create a boost control */
3690 snprintf(boost_label, sizeof(boost_label),
3691 "%s Boost Volume", spec->input_labels[idx]);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003692 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3693 spec->input_label_idxs[idx], val))
3694 return -ENOMEM;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003695
3696 path->ctls[NID_PATH_BOOST_CTL] = val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003697 }
3698 return 0;
3699}
3700
3701/*
3702 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3703 */
3704static void parse_digital(struct hda_codec *codec)
3705{
3706 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003707 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003708 int i, nums;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003709 hda_nid_t dig_nid, pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003710
3711 /* support multiple SPDIFs; the secondary is set up as a slave */
3712 nums = 0;
3713 for (i = 0; i < spec->autocfg.dig_outs; i++) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003714 pin = spec->autocfg.dig_out_pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003715 dig_nid = look_for_dac(codec, pin, true);
3716 if (!dig_nid)
3717 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003718 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003719 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003720 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01003721 print_nid_path(codec, "digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01003722 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01003723 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003724 set_pin_target(codec, pin, PIN_OUT, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003725 if (!nums) {
3726 spec->multiout.dig_out_nid = dig_nid;
3727 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3728 } else {
3729 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3730 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
Dan Carpenterd5764222014-05-14 17:18:31 +03003731 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003732 spec->slave_dig_outs[nums - 1] = dig_nid;
3733 }
3734 nums++;
3735 }
3736
3737 if (spec->autocfg.dig_in_pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003738 pin = spec->autocfg.dig_in_pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003739 dig_nid = codec->start_nid;
3740 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003741 unsigned int wcaps = get_wcaps(codec, dig_nid);
3742 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3743 continue;
3744 if (!(wcaps & AC_WCAP_DIGITAL))
3745 continue;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003746 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003747 if (path) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003748 print_nid_path(codec, "digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003749 path->active = true;
3750 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003751 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003752 set_pin_target(codec, pin, PIN_IN, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003753 break;
3754 }
3755 }
3756 }
3757}
3758
3759
3760/*
3761 * input MUX handling
3762 */
3763
3764static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3765
3766/* select the given imux item; either unmute exclusively or select the route */
3767static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3768 unsigned int idx)
3769{
3770 struct hda_gen_spec *spec = codec->spec;
3771 const struct hda_input_mux *imux;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003772 struct nid_path *old_path, *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003773
3774 imux = &spec->input_mux;
3775 if (!imux->num_items)
3776 return 0;
3777
3778 if (idx >= imux->num_items)
3779 idx = imux->num_items - 1;
3780 if (spec->cur_mux[adc_idx] == idx)
3781 return 0;
3782
Takashi Iwai55196ff2013-01-24 17:32:56 +01003783 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3784 if (!old_path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003785 return 0;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003786 if (old_path->active)
3787 snd_hda_activate_path(codec, old_path, false, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003788
3789 spec->cur_mux[adc_idx] = idx;
3790
Takashi Iwai967303d2013-02-19 17:12:42 +01003791 if (spec->hp_mic)
3792 update_hp_mic(codec, adc_idx, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003793
3794 if (spec->dyn_adc_switch)
3795 dyn_adc_pcm_resetup(codec, idx);
3796
Takashi Iwaic697b712013-01-07 17:09:26 +01003797 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003798 if (!path)
3799 return 0;
3800 if (path->active)
3801 return 0;
3802 snd_hda_activate_path(codec, path, true, false);
3803 if (spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01003804 spec->cap_sync_hook(codec, NULL, NULL);
Takashi Iwai55196ff2013-01-24 17:32:56 +01003805 path_power_down_sync(codec, old_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003806 return 1;
3807}
3808
3809
3810/*
3811 * Jack detections for HP auto-mute and mic-switch
3812 */
3813
3814/* check each pin in the given array; returns true if any of them is plugged */
3815static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
3816{
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02003817 int i;
3818 bool present = false;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003819
3820 for (i = 0; i < num_pins; i++) {
3821 hda_nid_t nid = pins[i];
3822 if (!nid)
3823 break;
Takashi Iwai0b4df932013-01-10 09:45:13 +01003824 /* don't detect pins retasked as inputs */
3825 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
3826 continue;
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02003827 if (snd_hda_jack_detect_state(codec, nid) == HDA_JACK_PRESENT)
3828 present = true;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003829 }
3830 return present;
3831}
3832
3833/* standard HP/line-out auto-mute helper */
3834static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003835 int *paths, bool mute)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003836{
3837 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003838 int i;
3839
3840 for (i = 0; i < num_pins; i++) {
3841 hda_nid_t nid = pins[i];
Takashi Iwai967303d2013-02-19 17:12:42 +01003842 unsigned int val, oldval;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003843 if (!nid)
3844 break;
Takashi Iwai7eebffd2013-06-24 16:00:21 +02003845
3846 if (spec->auto_mute_via_amp) {
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003847 struct nid_path *path;
3848 hda_nid_t mute_nid;
3849
3850 path = snd_hda_get_path_from_idx(codec, paths[i]);
3851 if (!path)
3852 continue;
3853 mute_nid = get_amp_nid_(path->ctls[NID_PATH_MUTE_CTL]);
3854 if (!mute_nid)
3855 continue;
Takashi Iwai7eebffd2013-06-24 16:00:21 +02003856 if (mute)
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003857 spec->mute_bits |= (1ULL << mute_nid);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02003858 else
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003859 spec->mute_bits &= ~(1ULL << mute_nid);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02003860 set_pin_eapd(codec, nid, !mute);
3861 continue;
3862 }
3863
Takashi Iwai967303d2013-02-19 17:12:42 +01003864 oldval = snd_hda_codec_get_pin_target(codec, nid);
3865 if (oldval & PIN_IN)
3866 continue; /* no mute for inputs */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003867 /* don't reset VREF value in case it's controlling
3868 * the amp (see alc861_fixup_asus_amp_vref_0f())
3869 */
Takashi Iwai2c12c302013-01-10 09:33:29 +01003870 if (spec->keep_vref_in_automute)
Takashi Iwai967303d2013-02-19 17:12:42 +01003871 val = oldval & ~PIN_HP;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003872 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01003873 val = 0;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003874 if (!mute)
Takashi Iwai967303d2013-02-19 17:12:42 +01003875 val |= oldval;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003876 /* here we call update_pin_ctl() so that the pinctl is changed
3877 * without changing the pinctl target value;
3878 * the original target value will be still referred at the
3879 * init / resume again
3880 */
3881 update_pin_ctl(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01003882 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003883 }
3884}
3885
3886/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003887void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003888{
3889 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003890 int *paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003891 int on;
3892
3893 /* Control HP pins/amps depending on master_mute state;
3894 * in general, HP pins/amps control should be enabled in all cases,
3895 * but currently set only for master_mute, just to be safe
3896 */
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003897 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
3898 paths = spec->out_paths;
3899 else
3900 paths = spec->hp_paths;
Takashi Iwai967303d2013-02-19 17:12:42 +01003901 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003902 spec->autocfg.hp_pins, paths, spec->master_mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003903
3904 if (!spec->automute_speaker)
3905 on = 0;
3906 else
3907 on = spec->hp_jack_present | spec->line_jack_present;
3908 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003909 spec->speaker_muted = on;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003910 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3911 paths = spec->out_paths;
3912 else
3913 paths = spec->speaker_paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003914 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003915 spec->autocfg.speaker_pins, paths, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003916
3917 /* toggle line-out mutes if needed, too */
3918 /* if LO is a copy of either HP or Speaker, don't need to handle it */
3919 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
3920 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
3921 return;
3922 if (!spec->automute_lo)
3923 on = 0;
3924 else
3925 on = spec->hp_jack_present;
3926 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003927 spec->line_out_muted = on;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003928 paths = spec->out_paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003929 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003930 spec->autocfg.line_out_pins, paths, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003931}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003932EXPORT_SYMBOL_GPL(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003933
3934static void call_update_outputs(struct hda_codec *codec)
3935{
3936 struct hda_gen_spec *spec = codec->spec;
3937 if (spec->automute_hook)
3938 spec->automute_hook(codec);
3939 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01003940 snd_hda_gen_update_outputs(codec);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02003941
3942 /* sync the whole vmaster slaves to reflect the new auto-mute status */
3943 if (spec->auto_mute_via_amp && !codec->bus->shutdown)
3944 snd_ctl_sync_vmaster(spec->vmaster_mute.sw_kctl, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003945}
3946
3947/* standard HP-automute helper */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02003948void snd_hda_gen_hp_automute(struct hda_codec *codec,
3949 struct hda_jack_callback *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003950{
3951 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai92603c52013-01-22 07:46:31 +01003952 hda_nid_t *pins = spec->autocfg.hp_pins;
3953 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003954
Takashi Iwai92603c52013-01-22 07:46:31 +01003955 /* No detection for the first HP jack during indep-HP mode */
3956 if (spec->indep_hp_enabled) {
3957 pins++;
3958 num_pins--;
3959 }
3960
3961 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003962 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
3963 return;
3964 call_update_outputs(codec);
3965}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003966EXPORT_SYMBOL_GPL(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003967
3968/* standard line-out-automute helper */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02003969void snd_hda_gen_line_automute(struct hda_codec *codec,
3970 struct hda_jack_callback *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003971{
3972 struct hda_gen_spec *spec = codec->spec;
3973
3974 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3975 return;
3976 /* check LO jack only when it's different from HP */
3977 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
3978 return;
3979
3980 spec->line_jack_present =
3981 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3982 spec->autocfg.line_out_pins);
3983 if (!spec->automute_speaker || !spec->detect_lo)
3984 return;
3985 call_update_outputs(codec);
3986}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003987EXPORT_SYMBOL_GPL(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003988
3989/* standard mic auto-switch helper */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02003990void snd_hda_gen_mic_autoswitch(struct hda_codec *codec,
3991 struct hda_jack_callback *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003992{
3993 struct hda_gen_spec *spec = codec->spec;
3994 int i;
3995
3996 if (!spec->auto_mic)
3997 return;
3998
3999 for (i = spec->am_num_entries - 1; i > 0; i--) {
Takashi Iwai0b4df932013-01-10 09:45:13 +01004000 hda_nid_t pin = spec->am_entry[i].pin;
4001 /* don't detect pins retasked as outputs */
4002 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
4003 continue;
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02004004 if (snd_hda_jack_detect_state(codec, pin) == HDA_JACK_PRESENT) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004005 mux_select(codec, 0, spec->am_entry[i].idx);
4006 return;
4007 }
4008 }
4009 mux_select(codec, 0, spec->am_entry[0].idx);
4010}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004011EXPORT_SYMBOL_GPL(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004012
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004013/* call appropriate hooks */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004014static void call_hp_automute(struct hda_codec *codec,
4015 struct hda_jack_callback *jack)
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004016{
4017 struct hda_gen_spec *spec = codec->spec;
4018 if (spec->hp_automute_hook)
4019 spec->hp_automute_hook(codec, jack);
4020 else
4021 snd_hda_gen_hp_automute(codec, jack);
4022}
4023
4024static void call_line_automute(struct hda_codec *codec,
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004025 struct hda_jack_callback *jack)
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004026{
4027 struct hda_gen_spec *spec = codec->spec;
4028 if (spec->line_automute_hook)
4029 spec->line_automute_hook(codec, jack);
4030 else
4031 snd_hda_gen_line_automute(codec, jack);
4032}
4033
4034static void call_mic_autoswitch(struct hda_codec *codec,
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004035 struct hda_jack_callback *jack)
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004036{
4037 struct hda_gen_spec *spec = codec->spec;
4038 if (spec->mic_autoswitch_hook)
4039 spec->mic_autoswitch_hook(codec, jack);
4040 else
4041 snd_hda_gen_mic_autoswitch(codec, jack);
4042}
4043
Takashi Iwai963afde2013-05-31 15:20:31 +02004044/* update jack retasking */
4045static void update_automute_all(struct hda_codec *codec)
4046{
4047 call_hp_automute(codec, NULL);
4048 call_line_automute(codec, NULL);
4049 call_mic_autoswitch(codec, NULL);
4050}
4051
Takashi Iwai352f7f92012-12-19 12:52:06 +01004052/*
4053 * Auto-Mute mode mixer enum support
4054 */
4055static int automute_mode_info(struct snd_kcontrol *kcontrol,
4056 struct snd_ctl_elem_info *uinfo)
4057{
4058 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4059 struct hda_gen_spec *spec = codec->spec;
4060 static const char * const texts3[] = {
4061 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02004062 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063
Takashi Iwai352f7f92012-12-19 12:52:06 +01004064 if (spec->automute_speaker_possible && spec->automute_lo_possible)
4065 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
4066 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
4067}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068
Takashi Iwai352f7f92012-12-19 12:52:06 +01004069static int automute_mode_get(struct snd_kcontrol *kcontrol,
4070 struct snd_ctl_elem_value *ucontrol)
4071{
4072 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4073 struct hda_gen_spec *spec = codec->spec;
4074 unsigned int val = 0;
4075 if (spec->automute_speaker)
4076 val++;
4077 if (spec->automute_lo)
4078 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004079
Takashi Iwai352f7f92012-12-19 12:52:06 +01004080 ucontrol->value.enumerated.item[0] = val;
4081 return 0;
4082}
4083
4084static int automute_mode_put(struct snd_kcontrol *kcontrol,
4085 struct snd_ctl_elem_value *ucontrol)
4086{
4087 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4088 struct hda_gen_spec *spec = codec->spec;
4089
4090 switch (ucontrol->value.enumerated.item[0]) {
4091 case 0:
4092 if (!spec->automute_speaker && !spec->automute_lo)
4093 return 0;
4094 spec->automute_speaker = 0;
4095 spec->automute_lo = 0;
4096 break;
4097 case 1:
4098 if (spec->automute_speaker_possible) {
4099 if (!spec->automute_lo && spec->automute_speaker)
4100 return 0;
4101 spec->automute_speaker = 1;
4102 spec->automute_lo = 0;
4103 } else if (spec->automute_lo_possible) {
4104 if (spec->automute_lo)
4105 return 0;
4106 spec->automute_lo = 1;
4107 } else
4108 return -EINVAL;
4109 break;
4110 case 2:
4111 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
4112 return -EINVAL;
4113 if (spec->automute_speaker && spec->automute_lo)
4114 return 0;
4115 spec->automute_speaker = 1;
4116 spec->automute_lo = 1;
4117 break;
4118 default:
4119 return -EINVAL;
4120 }
4121 call_update_outputs(codec);
4122 return 1;
4123}
4124
4125static const struct snd_kcontrol_new automute_mode_enum = {
4126 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4127 .name = "Auto-Mute Mode",
4128 .info = automute_mode_info,
4129 .get = automute_mode_get,
4130 .put = automute_mode_put,
4131};
4132
4133static int add_automute_mode_enum(struct hda_codec *codec)
4134{
4135 struct hda_gen_spec *spec = codec->spec;
4136
Takashi Iwai12c93df2012-12-19 14:38:33 +01004137 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01004138 return -ENOMEM;
4139 return 0;
4140}
4141
4142/*
4143 * Check the availability of HP/line-out auto-mute;
4144 * Set up appropriately if really supported
4145 */
4146static int check_auto_mute_availability(struct hda_codec *codec)
4147{
4148 struct hda_gen_spec *spec = codec->spec;
4149 struct auto_pin_cfg *cfg = &spec->autocfg;
4150 int present = 0;
4151 int i, err;
4152
Takashi Iwaif72706b2013-01-16 18:20:07 +01004153 if (spec->suppress_auto_mute)
4154 return 0;
4155
Takashi Iwai352f7f92012-12-19 12:52:06 +01004156 if (cfg->hp_pins[0])
4157 present++;
4158 if (cfg->line_out_pins[0])
4159 present++;
4160 if (cfg->speaker_pins[0])
4161 present++;
4162 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02004163 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004164
4165 if (!cfg->speaker_pins[0] &&
4166 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
4167 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4168 sizeof(cfg->speaker_pins));
4169 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171
Takashi Iwai352f7f92012-12-19 12:52:06 +01004172 if (!cfg->hp_pins[0] &&
4173 cfg->line_out_type == AUTO_PIN_HP_OUT) {
4174 memcpy(cfg->hp_pins, cfg->line_out_pins,
4175 sizeof(cfg->hp_pins));
4176 cfg->hp_outs = cfg->line_outs;
4177 }
4178
4179 for (i = 0; i < cfg->hp_outs; i++) {
4180 hda_nid_t nid = cfg->hp_pins[i];
4181 if (!is_jack_detectable(codec, nid))
4182 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004183 codec_dbg(codec, "Enable HP auto-muting on NID 0x%x\n", nid);
Takashi Iwai62f949b2014-09-11 14:06:53 +02004184 snd_hda_jack_detect_enable_callback(codec, nid,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004185 call_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004186 spec->detect_hp = 1;
4187 }
4188
4189 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
4190 if (cfg->speaker_outs)
4191 for (i = 0; i < cfg->line_outs; i++) {
4192 hda_nid_t nid = cfg->line_out_pins[i];
4193 if (!is_jack_detectable(codec, nid))
4194 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004195 codec_dbg(codec, "Enable Line-Out auto-muting on NID 0x%x\n", nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004196 snd_hda_jack_detect_enable_callback(codec, nid,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004197 call_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004198 spec->detect_lo = 1;
4199 }
4200 spec->automute_lo_possible = spec->detect_hp;
4201 }
4202
4203 spec->automute_speaker_possible = cfg->speaker_outs &&
4204 (spec->detect_hp || spec->detect_lo);
4205
4206 spec->automute_lo = spec->automute_lo_possible;
4207 spec->automute_speaker = spec->automute_speaker_possible;
4208
4209 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
4210 /* create a control for automute mode */
4211 err = add_automute_mode_enum(codec);
4212 if (err < 0)
4213 return err;
4214 }
4215 return 0;
4216}
4217
Takashi Iwai352f7f92012-12-19 12:52:06 +01004218/* check whether all auto-mic pins are valid; setup indices if OK */
4219static bool auto_mic_check_imux(struct hda_codec *codec)
4220{
4221 struct hda_gen_spec *spec = codec->spec;
4222 const struct hda_input_mux *imux;
4223 int i;
4224
4225 imux = &spec->input_mux;
4226 for (i = 0; i < spec->am_num_entries; i++) {
4227 spec->am_entry[i].idx =
4228 find_idx_in_nid_list(spec->am_entry[i].pin,
4229 spec->imux_pins, imux->num_items);
4230 if (spec->am_entry[i].idx < 0)
4231 return false; /* no corresponding imux */
4232 }
4233
4234 /* we don't need the jack detection for the first pin */
4235 for (i = 1; i < spec->am_num_entries; i++)
4236 snd_hda_jack_detect_enable_callback(codec,
4237 spec->am_entry[i].pin,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004238 call_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004239 return true;
4240}
4241
4242static int compare_attr(const void *ap, const void *bp)
4243{
4244 const struct automic_entry *a = ap;
4245 const struct automic_entry *b = bp;
4246 return (int)(a->attr - b->attr);
4247}
4248
4249/*
4250 * Check the availability of auto-mic switch;
4251 * Set up if really supported
4252 */
4253static int check_auto_mic_availability(struct hda_codec *codec)
4254{
4255 struct hda_gen_spec *spec = codec->spec;
4256 struct auto_pin_cfg *cfg = &spec->autocfg;
4257 unsigned int types;
4258 int i, num_pins;
4259
Takashi Iwaid12daf62013-01-07 16:32:11 +01004260 if (spec->suppress_auto_mic)
4261 return 0;
4262
Takashi Iwai352f7f92012-12-19 12:52:06 +01004263 types = 0;
4264 num_pins = 0;
4265 for (i = 0; i < cfg->num_inputs; i++) {
4266 hda_nid_t nid = cfg->inputs[i].pin;
4267 unsigned int attr;
4268 attr = snd_hda_codec_get_pincfg(codec, nid);
4269 attr = snd_hda_get_input_pin_attr(attr);
4270 if (types & (1 << attr))
4271 return 0; /* already occupied */
4272 switch (attr) {
4273 case INPUT_PIN_ATTR_INT:
4274 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4275 return 0; /* invalid type */
4276 break;
4277 case INPUT_PIN_ATTR_UNUSED:
4278 return 0; /* invalid entry */
4279 default:
4280 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4281 return 0; /* invalid type */
4282 if (!spec->line_in_auto_switch &&
4283 cfg->inputs[i].type != AUTO_PIN_MIC)
4284 return 0; /* only mic is allowed */
4285 if (!is_jack_detectable(codec, nid))
4286 return 0; /* no unsol support */
4287 break;
4288 }
4289 if (num_pins >= MAX_AUTO_MIC_PINS)
4290 return 0;
4291 types |= (1 << attr);
4292 spec->am_entry[num_pins].pin = nid;
4293 spec->am_entry[num_pins].attr = attr;
4294 num_pins++;
4295 }
4296
4297 if (num_pins < 2)
4298 return 0;
4299
4300 spec->am_num_entries = num_pins;
4301 /* sort the am_entry in the order of attr so that the pin with a
4302 * higher attr will be selected when the jack is plugged.
4303 */
4304 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4305 compare_attr, NULL);
4306
4307 if (!auto_mic_check_imux(codec))
4308 return 0;
4309
4310 spec->auto_mic = 1;
4311 spec->num_adc_nids = 1;
4312 spec->cur_mux[0] = spec->am_entry[0].idx;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004313 codec_dbg(codec, "Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01004314 spec->am_entry[0].pin,
4315 spec->am_entry[1].pin,
4316 spec->am_entry[2].pin);
4317
4318 return 0;
4319}
4320
Takashi Iwai55196ff2013-01-24 17:32:56 +01004321/* power_filter hook; make inactive widgets into power down */
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004322unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
Takashi Iwai55196ff2013-01-24 17:32:56 +01004323 hda_nid_t nid,
4324 unsigned int power_state)
4325{
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004326 if (power_state != AC_PWRST_D0 || nid == codec->afg)
Takashi Iwai55196ff2013-01-24 17:32:56 +01004327 return power_state;
4328 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4329 return power_state;
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +02004330 if (is_active_nid_for_any(codec, nid))
Takashi Iwai55196ff2013-01-24 17:32:56 +01004331 return power_state;
4332 return AC_PWRST_D3;
4333}
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004334EXPORT_SYMBOL_GPL(snd_hda_gen_path_power_filter);
Takashi Iwai55196ff2013-01-24 17:32:56 +01004335
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004336/* mute all aamix inputs initially; parse up to the first leaves */
4337static void mute_all_mixer_nid(struct hda_codec *codec, hda_nid_t mix)
4338{
4339 int i, nums;
4340 const hda_nid_t *conn;
4341 bool has_amp;
4342
4343 nums = snd_hda_get_conn_list(codec, mix, &conn);
4344 has_amp = nid_has_mute(codec, mix, HDA_INPUT);
4345 for (i = 0; i < nums; i++) {
4346 if (has_amp)
4347 snd_hda_codec_amp_stereo(codec, mix,
4348 HDA_INPUT, i,
4349 0xff, HDA_AMP_MUTE);
4350 else if (nid_has_volume(codec, conn[i], HDA_OUTPUT))
4351 snd_hda_codec_amp_stereo(codec, conn[i],
4352 HDA_OUTPUT, 0,
4353 0xff, HDA_AMP_MUTE);
4354 }
4355}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004356
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004357/*
4358 * Parse the given BIOS configuration and set up the hda_gen_spec
4359 *
4360 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004361 * or a negative error code
4362 */
4363int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004364 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004365{
4366 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004367 int err;
4368
Takashi Iwai1c70a582013-01-11 17:48:22 +01004369 parse_user_hints(codec);
4370
Takashi Iwaie4a395e2013-01-23 17:00:31 +01004371 if (spec->mixer_nid && !spec->mixer_merge_nid)
4372 spec->mixer_merge_nid = spec->mixer_nid;
4373
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004374 if (cfg != &spec->autocfg) {
4375 spec->autocfg = *cfg;
4376 cfg = &spec->autocfg;
4377 }
4378
Takashi Iwai98bd1112013-03-22 14:53:50 +01004379 if (!spec->main_out_badness)
4380 spec->main_out_badness = &hda_main_out_badness;
4381 if (!spec->extra_out_badness)
4382 spec->extra_out_badness = &hda_extra_out_badness;
4383
David Henningsson6fc4cb92013-01-16 15:58:45 +01004384 fill_all_dac_nids(codec);
4385
Takashi Iwai352f7f92012-12-19 12:52:06 +01004386 if (!cfg->line_outs) {
4387 if (cfg->dig_outs || cfg->dig_in_pin) {
4388 spec->multiout.max_channels = 2;
4389 spec->no_analog = 1;
4390 goto dig_only;
4391 }
Takashi Iwaic9e4bdb2013-12-06 09:30:14 +01004392 if (!cfg->num_inputs && !cfg->dig_in_pin)
4393 return 0; /* can't find valid BIOS pin config */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004394 }
4395
4396 if (!spec->no_primary_hp &&
4397 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
4398 cfg->line_outs <= cfg->hp_outs) {
4399 /* use HP as primary out */
4400 cfg->speaker_outs = cfg->line_outs;
4401 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4402 sizeof(cfg->speaker_pins));
4403 cfg->line_outs = cfg->hp_outs;
4404 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4405 cfg->hp_outs = 0;
4406 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4407 cfg->line_out_type = AUTO_PIN_HP_OUT;
4408 }
4409
4410 err = parse_output_paths(codec);
4411 if (err < 0)
4412 return err;
4413 err = create_multi_channel_mode(codec);
4414 if (err < 0)
4415 return err;
4416 err = create_multi_out_ctls(codec, cfg);
4417 if (err < 0)
4418 return err;
4419 err = create_hp_out_ctls(codec);
4420 if (err < 0)
4421 return err;
4422 err = create_speaker_out_ctls(codec);
4423 if (err < 0)
4424 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004425 err = create_indep_hp_ctls(codec);
4426 if (err < 0)
4427 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01004428 err = create_loopback_mixing_ctl(codec);
4429 if (err < 0)
4430 return err;
Takashi Iwai967303d2013-02-19 17:12:42 +01004431 err = create_hp_mic(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004432 if (err < 0)
4433 return err;
4434 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02004435 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02004436 return err;
4437
Takashi Iwaia07a9492013-01-07 16:44:06 +01004438 spec->const_channel_count = spec->ext_channel_count;
4439 /* check the multiple speaker and headphone pins */
4440 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4441 spec->const_channel_count = max(spec->const_channel_count,
4442 cfg->speaker_outs * 2);
4443 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4444 spec->const_channel_count = max(spec->const_channel_count,
4445 cfg->hp_outs * 2);
4446 spec->multiout.max_channels = max(spec->ext_channel_count,
4447 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004448
4449 err = check_auto_mute_availability(codec);
4450 if (err < 0)
4451 return err;
4452
4453 err = check_dyn_adc_switch(codec);
4454 if (err < 0)
4455 return err;
4456
Takashi Iwai967303d2013-02-19 17:12:42 +01004457 err = check_auto_mic_availability(codec);
4458 if (err < 0)
4459 return err;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004460
Takashi Iwaif1e762d2013-12-09 16:02:24 +01004461 /* add stereo mix if available and not enabled yet */
4462 if (!spec->auto_mic && spec->mixer_nid &&
4463 spec->add_stereo_mix_input &&
4464 spec->input_mux.num_items > 1 &&
4465 snd_hda_get_bool_hint(codec, "add_stereo_mix_input") < 0) {
4466 err = parse_capture_source(codec, spec->mixer_nid,
4467 CFG_IDX_MIX, spec->num_all_adcs,
4468 "Stereo Mix", 0);
4469 if (err < 0)
4470 return err;
4471 }
4472
4473
Takashi Iwai352f7f92012-12-19 12:52:06 +01004474 err = create_capture_mixers(codec);
4475 if (err < 0)
4476 return err;
4477
4478 err = parse_mic_boost(codec);
4479 if (err < 0)
4480 return err;
4481
Takashi Iwaiced4cef2013-11-26 08:33:45 +01004482 /* create "Headphone Mic Jack Mode" if no input selection is
4483 * available (or user specifies add_jack_modes hint)
4484 */
4485 if (spec->hp_mic_pin &&
4486 (spec->auto_mic || spec->input_mux.num_items == 1 ||
4487 spec->add_jack_modes)) {
4488 err = create_hp_mic_jack_mode(codec, spec->hp_mic_pin);
4489 if (err < 0)
4490 return err;
4491 }
4492
Takashi Iwaif811c3c2013-03-07 18:32:59 +01004493 if (spec->add_jack_modes) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01004494 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4495 err = create_out_jack_modes(codec, cfg->line_outs,
4496 cfg->line_out_pins);
4497 if (err < 0)
4498 return err;
4499 }
4500 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4501 err = create_out_jack_modes(codec, cfg->hp_outs,
4502 cfg->hp_pins);
4503 if (err < 0)
4504 return err;
4505 }
4506 }
4507
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004508 /* mute all aamix input initially */
4509 if (spec->mixer_nid)
4510 mute_all_mixer_nid(codec, spec->mixer_nid);
4511
Takashi Iwai352f7f92012-12-19 12:52:06 +01004512 dig_only:
4513 parse_digital(codec);
4514
Takashi Iwai55196ff2013-01-24 17:32:56 +01004515 if (spec->power_down_unused)
4516 codec->power_filter = snd_hda_gen_path_power_filter;
4517
Takashi Iwai7504b6c2013-03-18 11:25:51 +01004518 if (!spec->no_analog && spec->beep_nid) {
4519 err = snd_hda_attach_beep_device(codec, spec->beep_nid);
4520 if (err < 0)
4521 return err;
4522 }
4523
Takashi Iwai352f7f92012-12-19 12:52:06 +01004524 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004526EXPORT_SYMBOL_GPL(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527
4528
4529/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004530 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004532
4533/* slave controls for virtual master */
4534static const char * const slave_pfxs[] = {
4535 "Front", "Surround", "Center", "LFE", "Side",
4536 "Headphone", "Speaker", "Mono", "Line Out",
4537 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01004538 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
4539 "Headphone Front", "Headphone Surround", "Headphone CLFE",
David Henningsson03ad6a82014-10-16 15:33:45 +02004540 "Headphone Side", "Headphone+LO", "Speaker+LO",
Takashi Iwai352f7f92012-12-19 12:52:06 +01004541 NULL,
4542};
4543
4544int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004546 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548
Takashi Iwai36502d02012-12-19 15:15:10 +01004549 if (spec->kctls.used) {
4550 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
4551 if (err < 0)
4552 return err;
4553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554
Takashi Iwai352f7f92012-12-19 12:52:06 +01004555 if (spec->multiout.dig_out_nid) {
4556 err = snd_hda_create_dig_out_ctls(codec,
4557 spec->multiout.dig_out_nid,
4558 spec->multiout.dig_out_nid,
4559 spec->pcm_rec[1].pcm_type);
4560 if (err < 0)
4561 return err;
4562 if (!spec->no_analog) {
4563 err = snd_hda_create_spdif_share_sw(codec,
4564 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 if (err < 0)
4566 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004567 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568 }
4569 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004570 if (spec->dig_in_nid) {
4571 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
4572 if (err < 0)
4573 return err;
4574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575
Takashi Iwai352f7f92012-12-19 12:52:06 +01004576 /* if we have no master control, let's create it */
4577 if (!spec->no_analog &&
4578 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004579 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01004580 spec->vmaster_tlv, slave_pfxs,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004581 "Playback Volume");
4582 if (err < 0)
4583 return err;
4584 }
4585 if (!spec->no_analog &&
4586 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
4587 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
4588 NULL, slave_pfxs,
4589 "Playback Switch",
4590 true, &spec->vmaster_mute.sw_kctl);
4591 if (err < 0)
4592 return err;
Takashi Iwaib63eae02013-10-25 23:43:10 +02004593 if (spec->vmaster_mute.hook) {
Takashi Iwaifd25a972012-12-20 14:57:18 +01004594 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
4595 spec->vmaster_mute_enum);
Takashi Iwaib63eae02013-10-25 23:43:10 +02004596 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
4597 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004598 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599
Takashi Iwai352f7f92012-12-19 12:52:06 +01004600 free_kctls(spec); /* no longer needed */
4601
Takashi Iwai352f7f92012-12-19 12:52:06 +01004602 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
4603 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604 return err;
4605
4606 return 0;
4607}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004608EXPORT_SYMBOL_GPL(snd_hda_gen_build_controls);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004609
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610
4611/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004612 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004615static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
4616 struct hda_codec *codec,
4617 struct snd_pcm_substream *substream,
4618 int action)
4619{
4620 struct hda_gen_spec *spec = codec->spec;
4621 if (spec->pcm_playback_hook)
4622 spec->pcm_playback_hook(hinfo, codec, substream, action);
4623}
4624
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004625static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
4626 struct hda_codec *codec,
4627 struct snd_pcm_substream *substream,
4628 int action)
4629{
4630 struct hda_gen_spec *spec = codec->spec;
4631 if (spec->pcm_capture_hook)
4632 spec->pcm_capture_hook(hinfo, codec, substream, action);
4633}
4634
Takashi Iwai352f7f92012-12-19 12:52:06 +01004635/*
4636 * Analog playback callbacks
4637 */
4638static int playback_pcm_open(struct hda_pcm_stream *hinfo,
4639 struct hda_codec *codec,
4640 struct snd_pcm_substream *substream)
4641{
4642 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004643 int err;
4644
4645 mutex_lock(&spec->pcm_mutex);
4646 err = snd_hda_multi_out_analog_open(codec,
4647 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004648 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004649 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004650 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004651 call_pcm_playback_hook(hinfo, codec, substream,
4652 HDA_GEN_PCM_ACT_OPEN);
4653 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004654 mutex_unlock(&spec->pcm_mutex);
4655 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004656}
4657
4658static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01004659 struct hda_codec *codec,
4660 unsigned int stream_tag,
4661 unsigned int format,
4662 struct snd_pcm_substream *substream)
4663{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004664 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004665 int err;
4666
4667 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
4668 stream_tag, format, substream);
4669 if (!err)
4670 call_pcm_playback_hook(hinfo, codec, substream,
4671 HDA_GEN_PCM_ACT_PREPARE);
4672 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004673}
Takashi Iwai97ec5582006-03-21 11:29:07 +01004674
Takashi Iwai352f7f92012-12-19 12:52:06 +01004675static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4676 struct hda_codec *codec,
4677 struct snd_pcm_substream *substream)
4678{
4679 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004680 int err;
4681
4682 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
4683 if (!err)
4684 call_pcm_playback_hook(hinfo, codec, substream,
4685 HDA_GEN_PCM_ACT_CLEANUP);
4686 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004687}
4688
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004689static int playback_pcm_close(struct hda_pcm_stream *hinfo,
4690 struct hda_codec *codec,
4691 struct snd_pcm_substream *substream)
4692{
4693 struct hda_gen_spec *spec = codec->spec;
4694 mutex_lock(&spec->pcm_mutex);
4695 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004696 call_pcm_playback_hook(hinfo, codec, substream,
4697 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004698 mutex_unlock(&spec->pcm_mutex);
4699 return 0;
4700}
4701
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004702static int capture_pcm_open(struct hda_pcm_stream *hinfo,
4703 struct hda_codec *codec,
4704 struct snd_pcm_substream *substream)
4705{
4706 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
4707 return 0;
4708}
4709
4710static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4711 struct hda_codec *codec,
4712 unsigned int stream_tag,
4713 unsigned int format,
4714 struct snd_pcm_substream *substream)
4715{
4716 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4717 call_pcm_capture_hook(hinfo, codec, substream,
4718 HDA_GEN_PCM_ACT_PREPARE);
4719 return 0;
4720}
4721
4722static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4723 struct hda_codec *codec,
4724 struct snd_pcm_substream *substream)
4725{
4726 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4727 call_pcm_capture_hook(hinfo, codec, substream,
4728 HDA_GEN_PCM_ACT_CLEANUP);
4729 return 0;
4730}
4731
4732static int capture_pcm_close(struct hda_pcm_stream *hinfo,
4733 struct hda_codec *codec,
4734 struct snd_pcm_substream *substream)
4735{
4736 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
4737 return 0;
4738}
4739
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004740static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
4741 struct hda_codec *codec,
4742 struct snd_pcm_substream *substream)
4743{
4744 struct hda_gen_spec *spec = codec->spec;
4745 int err = 0;
4746
4747 mutex_lock(&spec->pcm_mutex);
4748 if (!spec->indep_hp_enabled)
4749 err = -EBUSY;
4750 else
4751 spec->active_streams |= 1 << STREAM_INDEP_HP;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004752 call_pcm_playback_hook(hinfo, codec, substream,
4753 HDA_GEN_PCM_ACT_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004754 mutex_unlock(&spec->pcm_mutex);
4755 return err;
4756}
4757
4758static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
4759 struct hda_codec *codec,
4760 struct snd_pcm_substream *substream)
4761{
4762 struct hda_gen_spec *spec = codec->spec;
4763 mutex_lock(&spec->pcm_mutex);
4764 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004765 call_pcm_playback_hook(hinfo, codec, substream,
4766 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004767 mutex_unlock(&spec->pcm_mutex);
4768 return 0;
4769}
4770
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004771static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4772 struct hda_codec *codec,
4773 unsigned int stream_tag,
4774 unsigned int format,
4775 struct snd_pcm_substream *substream)
4776{
4777 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4778 call_pcm_playback_hook(hinfo, codec, substream,
4779 HDA_GEN_PCM_ACT_PREPARE);
4780 return 0;
4781}
4782
4783static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4784 struct hda_codec *codec,
4785 struct snd_pcm_substream *substream)
4786{
4787 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4788 call_pcm_playback_hook(hinfo, codec, substream,
4789 HDA_GEN_PCM_ACT_CLEANUP);
4790 return 0;
4791}
4792
Takashi Iwai352f7f92012-12-19 12:52:06 +01004793/*
4794 * Digital out
4795 */
4796static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
4797 struct hda_codec *codec,
4798 struct snd_pcm_substream *substream)
4799{
4800 struct hda_gen_spec *spec = codec->spec;
4801 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
4802}
4803
4804static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4805 struct hda_codec *codec,
4806 unsigned int stream_tag,
4807 unsigned int format,
4808 struct snd_pcm_substream *substream)
4809{
4810 struct hda_gen_spec *spec = codec->spec;
4811 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
4812 stream_tag, format, substream);
4813}
4814
4815static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4816 struct hda_codec *codec,
4817 struct snd_pcm_substream *substream)
4818{
4819 struct hda_gen_spec *spec = codec->spec;
4820 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
4821}
4822
4823static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
4824 struct hda_codec *codec,
4825 struct snd_pcm_substream *substream)
4826{
4827 struct hda_gen_spec *spec = codec->spec;
4828 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
4829}
4830
4831/*
4832 * Analog capture
4833 */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004834#define alt_capture_pcm_open capture_pcm_open
4835#define alt_capture_pcm_close capture_pcm_close
4836
Takashi Iwai352f7f92012-12-19 12:52:06 +01004837static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4838 struct hda_codec *codec,
4839 unsigned int stream_tag,
4840 unsigned int format,
4841 struct snd_pcm_substream *substream)
4842{
4843 struct hda_gen_spec *spec = codec->spec;
4844
4845 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01004846 stream_tag, 0, format);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004847 call_pcm_capture_hook(hinfo, codec, substream,
4848 HDA_GEN_PCM_ACT_PREPARE);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004849 return 0;
4850}
4851
Takashi Iwai352f7f92012-12-19 12:52:06 +01004852static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4853 struct hda_codec *codec,
4854 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01004855{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004856 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01004857
Takashi Iwai352f7f92012-12-19 12:52:06 +01004858 snd_hda_codec_cleanup_stream(codec,
4859 spec->adc_nids[substream->number + 1]);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004860 call_pcm_capture_hook(hinfo, codec, substream,
4861 HDA_GEN_PCM_ACT_CLEANUP);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004862 return 0;
4863}
4864
Takashi Iwai352f7f92012-12-19 12:52:06 +01004865/*
4866 */
4867static const struct hda_pcm_stream pcm_analog_playback = {
4868 .substreams = 1,
4869 .channels_min = 2,
4870 .channels_max = 8,
4871 /* NID is set in build_pcms */
4872 .ops = {
4873 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004874 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004875 .prepare = playback_pcm_prepare,
4876 .cleanup = playback_pcm_cleanup
4877 },
4878};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879
Takashi Iwai352f7f92012-12-19 12:52:06 +01004880static const struct hda_pcm_stream pcm_analog_capture = {
4881 .substreams = 1,
4882 .channels_min = 2,
4883 .channels_max = 2,
4884 /* NID is set in build_pcms */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004885 .ops = {
4886 .open = capture_pcm_open,
4887 .close = capture_pcm_close,
4888 .prepare = capture_pcm_prepare,
4889 .cleanup = capture_pcm_cleanup
4890 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004891};
4892
4893static const struct hda_pcm_stream pcm_analog_alt_playback = {
4894 .substreams = 1,
4895 .channels_min = 2,
4896 .channels_max = 2,
4897 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004898 .ops = {
4899 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004900 .close = alt_playback_pcm_close,
4901 .prepare = alt_playback_pcm_prepare,
4902 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004903 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004904};
4905
4906static const struct hda_pcm_stream pcm_analog_alt_capture = {
4907 .substreams = 2, /* can be overridden */
4908 .channels_min = 2,
4909 .channels_max = 2,
4910 /* NID is set in build_pcms */
4911 .ops = {
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004912 .open = alt_capture_pcm_open,
4913 .close = alt_capture_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004914 .prepare = alt_capture_pcm_prepare,
4915 .cleanup = alt_capture_pcm_cleanup
4916 },
4917};
4918
4919static const struct hda_pcm_stream pcm_digital_playback = {
4920 .substreams = 1,
4921 .channels_min = 2,
4922 .channels_max = 2,
4923 /* NID is set in build_pcms */
4924 .ops = {
4925 .open = dig_playback_pcm_open,
4926 .close = dig_playback_pcm_close,
4927 .prepare = dig_playback_pcm_prepare,
4928 .cleanup = dig_playback_pcm_cleanup
4929 },
4930};
4931
4932static const struct hda_pcm_stream pcm_digital_capture = {
4933 .substreams = 1,
4934 .channels_min = 2,
4935 .channels_max = 2,
4936 /* NID is set in build_pcms */
4937};
4938
4939/* Used by build_pcms to flag that a PCM has no playback stream */
4940static const struct hda_pcm_stream pcm_null_stream = {
4941 .substreams = 0,
4942 .channels_min = 0,
4943 .channels_max = 0,
4944};
4945
4946/*
4947 * dynamic changing ADC PCM streams
4948 */
4949static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
4950{
4951 struct hda_gen_spec *spec = codec->spec;
4952 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
4953
4954 if (spec->cur_adc && spec->cur_adc != new_adc) {
4955 /* stream is running, let's swap the current ADC */
4956 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
4957 spec->cur_adc = new_adc;
4958 snd_hda_codec_setup_stream(codec, new_adc,
4959 spec->cur_adc_stream_tag, 0,
4960 spec->cur_adc_format);
4961 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004963 return false;
4964}
4965
4966/* analog capture with dynamic dual-adc changes */
4967static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4968 struct hda_codec *codec,
4969 unsigned int stream_tag,
4970 unsigned int format,
4971 struct snd_pcm_substream *substream)
4972{
4973 struct hda_gen_spec *spec = codec->spec;
4974 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
4975 spec->cur_adc_stream_tag = stream_tag;
4976 spec->cur_adc_format = format;
4977 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
4978 return 0;
4979}
4980
4981static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4982 struct hda_codec *codec,
4983 struct snd_pcm_substream *substream)
4984{
4985 struct hda_gen_spec *spec = codec->spec;
4986 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
4987 spec->cur_adc = 0;
4988 return 0;
4989}
4990
4991static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
4992 .substreams = 1,
4993 .channels_min = 2,
4994 .channels_max = 2,
4995 .nid = 0, /* fill later */
4996 .ops = {
4997 .prepare = dyn_adc_capture_pcm_prepare,
4998 .cleanup = dyn_adc_capture_pcm_cleanup
4999 },
5000};
5001
Takashi Iwaif873e532012-12-20 16:58:39 +01005002static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
5003 const char *chip_name)
5004{
5005 char *p;
5006
5007 if (*str)
5008 return;
5009 strlcpy(str, chip_name, len);
5010
5011 /* drop non-alnum chars after a space */
5012 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
5013 if (!isalnum(p[1])) {
5014 *p = 0;
5015 break;
5016 }
5017 }
5018 strlcat(str, sfx, len);
5019}
5020
Takashi Iwai352f7f92012-12-19 12:52:06 +01005021/* build PCM streams based on the parsed results */
5022int snd_hda_gen_build_pcms(struct hda_codec *codec)
5023{
5024 struct hda_gen_spec *spec = codec->spec;
5025 struct hda_pcm *info = spec->pcm_rec;
5026 const struct hda_pcm_stream *p;
5027 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028
5029 codec->num_pcms = 1;
5030 codec->pcm_info = info;
5031
Takashi Iwai352f7f92012-12-19 12:52:06 +01005032 if (spec->no_analog)
5033 goto skip_analog;
5034
Takashi Iwaif873e532012-12-20 16:58:39 +01005035 fill_pcm_stream_name(spec->stream_name_analog,
5036 sizeof(spec->stream_name_analog),
5037 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005038 info->name = spec->stream_name_analog;
5039
5040 if (spec->multiout.num_dacs > 0) {
5041 p = spec->stream_analog_playback;
5042 if (!p)
5043 p = &pcm_analog_playback;
5044 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
5045 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
5046 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
5047 spec->multiout.max_channels;
5048 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
5049 spec->autocfg.line_outs == 2)
5050 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
5051 snd_pcm_2_1_chmaps;
5052 }
5053 if (spec->num_adc_nids) {
5054 p = spec->stream_analog_capture;
5055 if (!p) {
5056 if (spec->dyn_adc_switch)
5057 p = &dyn_adc_pcm_analog_capture;
5058 else
5059 p = &pcm_analog_capture;
5060 }
5061 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
5062 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
5063 }
5064
Takashi Iwai352f7f92012-12-19 12:52:06 +01005065 skip_analog:
5066 /* SPDIF for stream index #1 */
5067 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01005068 fill_pcm_stream_name(spec->stream_name_digital,
5069 sizeof(spec->stream_name_digital),
5070 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005071 codec->num_pcms = 2;
5072 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
5073 info = spec->pcm_rec + 1;
5074 info->name = spec->stream_name_digital;
5075 if (spec->dig_out_type)
5076 info->pcm_type = spec->dig_out_type;
5077 else
5078 info->pcm_type = HDA_PCM_TYPE_SPDIF;
5079 if (spec->multiout.dig_out_nid) {
5080 p = spec->stream_digital_playback;
5081 if (!p)
5082 p = &pcm_digital_playback;
5083 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
5084 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
5085 }
5086 if (spec->dig_in_nid) {
5087 p = spec->stream_digital_capture;
5088 if (!p)
5089 p = &pcm_digital_capture;
5090 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
5091 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
5092 }
5093 }
5094
5095 if (spec->no_analog)
5096 return 0;
5097
5098 /* If the use of more than one ADC is requested for the current
5099 * model, configure a second analog capture-only PCM.
5100 */
5101 have_multi_adcs = (spec->num_adc_nids > 1) &&
5102 !spec->dyn_adc_switch && !spec->auto_mic;
5103 /* Additional Analaog capture for index #2 */
5104 if (spec->alt_dac_nid || have_multi_adcs) {
Takashi Iwaia6071482013-01-21 16:50:09 +01005105 fill_pcm_stream_name(spec->stream_name_alt_analog,
5106 sizeof(spec->stream_name_alt_analog),
5107 " Alt Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005108 codec->num_pcms = 3;
5109 info = spec->pcm_rec + 2;
Takashi Iwaia6071482013-01-21 16:50:09 +01005110 info->name = spec->stream_name_alt_analog;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005111 if (spec->alt_dac_nid) {
5112 p = spec->stream_analog_alt_playback;
5113 if (!p)
5114 p = &pcm_analog_alt_playback;
5115 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
5116 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
5117 spec->alt_dac_nid;
5118 } else {
5119 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
5120 pcm_null_stream;
5121 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
5122 }
5123 if (have_multi_adcs) {
5124 p = spec->stream_analog_alt_capture;
5125 if (!p)
5126 p = &pcm_analog_alt_capture;
5127 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
5128 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
5129 spec->adc_nids[1];
5130 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
5131 spec->num_adc_nids - 1;
5132 } else {
5133 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
5134 pcm_null_stream;
5135 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
5136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137 }
5138
5139 return 0;
5140}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005141EXPORT_SYMBOL_GPL(snd_hda_gen_build_pcms);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005142
5143
5144/*
5145 * Standard auto-parser initializations
5146 */
5147
Takashi Iwaid4156932013-01-07 10:08:02 +01005148/* configure the given path as a proper output */
Takashi Iwai2c12c302013-01-10 09:33:29 +01005149static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005150{
5151 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01005152 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005153
Takashi Iwai196c17662013-01-04 15:01:40 +01005154 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01005155 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005156 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01005157 pin = path->path[path->depth - 1];
Takashi Iwai2c12c302013-01-10 09:33:29 +01005158 restore_pin_ctl(codec, pin);
Takashi Iwai65033cc2013-04-16 12:31:05 +02005159 snd_hda_activate_path(codec, path, path->active,
5160 aamix_default(codec->spec));
Takashi Iwaie1284af2013-01-03 16:33:02 +01005161 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005162}
5163
5164/* initialize primary output paths */
5165static void init_multi_out(struct hda_codec *codec)
5166{
5167 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005168 int i;
5169
Takashi Iwaid4156932013-01-07 10:08:02 +01005170 for (i = 0; i < spec->autocfg.line_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005171 set_output_and_unmute(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005172}
5173
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005174
Takashi Iwai2c12c302013-01-10 09:33:29 +01005175static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005176{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005177 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005178
Takashi Iwaid4156932013-01-07 10:08:02 +01005179 for (i = 0; i < num_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005180 set_output_and_unmute(codec, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005181}
5182
5183/* initialize hp and speaker paths */
5184static void init_extra_out(struct hda_codec *codec)
5185{
5186 struct hda_gen_spec *spec = codec->spec;
5187
5188 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005189 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005190 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
5191 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai2c12c302013-01-10 09:33:29 +01005192 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005193}
5194
5195/* initialize multi-io paths */
5196static void init_multi_io(struct hda_codec *codec)
5197{
5198 struct hda_gen_spec *spec = codec->spec;
5199 int i;
5200
5201 for (i = 0; i < spec->multi_ios; i++) {
5202 hda_nid_t pin = spec->multi_io[i].pin;
5203 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01005204 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005205 if (!path)
5206 continue;
5207 if (!spec->multi_io[i].ctl_in)
5208 spec->multi_io[i].ctl_in =
Takashi Iwai2c12c302013-01-10 09:33:29 +01005209 snd_hda_codec_get_pin_target(codec, pin);
Takashi Iwai65033cc2013-04-16 12:31:05 +02005210 snd_hda_activate_path(codec, path, path->active,
5211 aamix_default(spec));
Takashi Iwai352f7f92012-12-19 12:52:06 +01005212 }
5213}
5214
Takashi Iwai4f7f67f2013-12-03 10:51:37 +01005215static void init_aamix_paths(struct hda_codec *codec)
5216{
5217 struct hda_gen_spec *spec = codec->spec;
5218
5219 if (!spec->have_aamix_ctl)
5220 return;
5221 update_aamix_paths(codec, spec->aamix_mode, spec->out_paths[0],
5222 spec->aamix_out_paths[0],
5223 spec->autocfg.line_out_type);
5224 update_aamix_paths(codec, spec->aamix_mode, spec->hp_paths[0],
5225 spec->aamix_out_paths[1],
5226 AUTO_PIN_HP_OUT);
5227 update_aamix_paths(codec, spec->aamix_mode, spec->speaker_paths[0],
5228 spec->aamix_out_paths[2],
5229 AUTO_PIN_SPEAKER_OUT);
5230}
5231
Takashi Iwai352f7f92012-12-19 12:52:06 +01005232/* set up input pins and loopback paths */
5233static void init_analog_input(struct hda_codec *codec)
5234{
5235 struct hda_gen_spec *spec = codec->spec;
5236 struct auto_pin_cfg *cfg = &spec->autocfg;
5237 int i;
5238
5239 for (i = 0; i < cfg->num_inputs; i++) {
5240 hda_nid_t nid = cfg->inputs[i].pin;
5241 if (is_input_pin(codec, nid))
Takashi Iwai2c12c302013-01-10 09:33:29 +01005242 restore_pin_ctl(codec, nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005243
5244 /* init loopback inputs */
5245 if (spec->mixer_nid) {
Takashi Iwai3e367f12013-01-23 17:07:23 +01005246 resume_path_from_idx(codec, spec->loopback_paths[i]);
5247 resume_path_from_idx(codec, spec->loopback_merge_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005248 }
5249 }
5250}
5251
5252/* initialize ADC paths */
5253static void init_input_src(struct hda_codec *codec)
5254{
5255 struct hda_gen_spec *spec = codec->spec;
5256 struct hda_input_mux *imux = &spec->input_mux;
5257 struct nid_path *path;
5258 int i, c, nums;
5259
5260 if (spec->dyn_adc_switch)
5261 nums = 1;
5262 else
5263 nums = spec->num_adc_nids;
5264
5265 for (c = 0; c < nums; c++) {
5266 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01005267 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005268 if (path) {
5269 bool active = path->active;
5270 if (i == spec->cur_mux[c])
5271 active = true;
5272 snd_hda_activate_path(codec, path, active, false);
5273 }
5274 }
Takashi Iwai967303d2013-02-19 17:12:42 +01005275 if (spec->hp_mic)
5276 update_hp_mic(codec, c, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005277 }
5278
Takashi Iwai352f7f92012-12-19 12:52:06 +01005279 if (spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01005280 spec->cap_sync_hook(codec, NULL, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005281}
5282
5283/* set right pin controls for digital I/O */
5284static void init_digital(struct hda_codec *codec)
5285{
5286 struct hda_gen_spec *spec = codec->spec;
5287 int i;
5288 hda_nid_t pin;
5289
Takashi Iwaid4156932013-01-07 10:08:02 +01005290 for (i = 0; i < spec->autocfg.dig_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005291 set_output_and_unmute(codec, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005292 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005293 if (pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01005294 restore_pin_ctl(codec, pin);
Takashi Iwai3e367f12013-01-23 17:07:23 +01005295 resume_path_from_idx(codec, spec->digin_path);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005296 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005297}
5298
Takashi Iwai973e4972012-12-20 15:16:09 +01005299/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5300 * invalid unsol tags by some reason
5301 */
5302static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5303{
5304 int i;
5305
5306 for (i = 0; i < codec->init_pins.used; i++) {
5307 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
5308 hda_nid_t nid = pin->nid;
5309 if (is_jack_detectable(codec, nid) &&
5310 !snd_hda_jack_tbl_get(codec, nid))
5311 snd_hda_codec_update_cache(codec, nid, 0,
5312 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5313 }
5314}
5315
Takashi Iwai5187ac12013-01-07 12:52:16 +01005316/*
5317 * initialize the generic spec;
5318 * this can be put as patch_ops.init function
5319 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005320int snd_hda_gen_init(struct hda_codec *codec)
5321{
5322 struct hda_gen_spec *spec = codec->spec;
5323
5324 if (spec->init_hook)
5325 spec->init_hook(codec);
5326
5327 snd_hda_apply_verbs(codec);
5328
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005329 codec->cached_write = 1;
5330
Takashi Iwai352f7f92012-12-19 12:52:06 +01005331 init_multi_out(codec);
5332 init_extra_out(codec);
5333 init_multi_io(codec);
Takashi Iwai4f7f67f2013-12-03 10:51:37 +01005334 init_aamix_paths(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005335 init_analog_input(codec);
5336 init_input_src(codec);
5337 init_digital(codec);
5338
Takashi Iwai973e4972012-12-20 15:16:09 +01005339 clear_unsol_on_unused_pins(codec);
5340
Takashi Iwai352f7f92012-12-19 12:52:06 +01005341 /* call init functions of standard auto-mute helpers */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01005342 update_automute_all(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005343
Takashi Iwaidc870f32013-01-22 15:24:30 +01005344 snd_hda_codec_flush_cache(codec);
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005345
Takashi Iwai352f7f92012-12-19 12:52:06 +01005346 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
5347 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5348
5349 hda_call_check_power_status(codec, 0x01);
5350 return 0;
5351}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005352EXPORT_SYMBOL_GPL(snd_hda_gen_init);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005353
Takashi Iwai5187ac12013-01-07 12:52:16 +01005354/*
5355 * free the generic spec;
5356 * this can be put as patch_ops.free function
5357 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005358void snd_hda_gen_free(struct hda_codec *codec)
5359{
Takashi Iwai8a02c0c2014-02-10 18:09:45 +01005360 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_FREE);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005361 snd_hda_gen_spec_free(codec->spec);
5362 kfree(codec->spec);
5363 codec->spec = NULL;
5364}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005365EXPORT_SYMBOL_GPL(snd_hda_gen_free);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005366
5367#ifdef CONFIG_PM
Takashi Iwai5187ac12013-01-07 12:52:16 +01005368/*
5369 * check the loopback power save state;
5370 * this can be put as patch_ops.check_power_status function
5371 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005372int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
5373{
5374 struct hda_gen_spec *spec = codec->spec;
5375 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
5376}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005377EXPORT_SYMBOL_GPL(snd_hda_gen_check_power_status);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005378#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01005379
5380
5381/*
5382 * the generic codec support
5383 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384
Takashi Iwai352f7f92012-12-19 12:52:06 +01005385static const struct hda_codec_ops generic_patch_ops = {
5386 .build_controls = snd_hda_gen_build_controls,
5387 .build_pcms = snd_hda_gen_build_pcms,
5388 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01005389 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005390 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02005391#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01005392 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02005393#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394};
5395
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396int snd_hda_parse_generic_codec(struct hda_codec *codec)
5397{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005398 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399 int err;
5400
Takashi Iwaie560d8d2005-09-09 14:21:46 +02005401 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005402 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005404 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406
Takashi Iwai9eb413e2012-12-19 14:41:21 +01005407 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
5408 if (err < 0)
5409 return err;
5410
5411 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005412 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413 goto error;
5414
5415 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416 return 0;
5417
Takashi Iwai352f7f92012-12-19 12:52:06 +01005418error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01005419 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420 return err;
5421}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005422EXPORT_SYMBOL_GPL(snd_hda_parse_generic_codec);
Takashi Iwaib21bdd02013-11-18 12:03:56 +01005423
5424MODULE_LICENSE("GPL");
5425MODULE_DESCRIPTION("Generic HD-audio codec parser");