blob: 95121e818b4d4c9169e3c4a54e98fab1efcde1a4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Universal Interface for Intel High Definition Audio Codec
3 *
4 * Generic widget tree parser
5 *
6 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
7 *
8 * This driver is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This driver is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/init.h>
24#include <linux/slab.h>
Paul Gortmakerd81a6d72011-09-22 09:34:58 -040025#include <linux/export.h>
Takashi Iwai352f7f92012-12-19 12:52:06 +010026#include <linux/sort.h>
Takashi Iwai55196ff2013-01-24 17:32:56 +010027#include <linux/delay.h>
Takashi Iwaif873e532012-12-20 16:58:39 +010028#include <linux/ctype.h>
29#include <linux/string.h>
Takashi Iwai294765582013-01-17 09:52:11 +010030#include <linux/bitops.h>
Takashi Iwaib21bdd02013-11-18 12:03:56 +010031#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <sound/core.h>
Takashi Iwai352f7f92012-12-19 12:52:06 +010033#include <sound/jack.h>
Takashi Iwaid89c6c02014-09-01 10:07:04 +020034#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "hda_codec.h"
36#include "hda_local.h"
Takashi Iwai352f7f92012-12-19 12:52:06 +010037#include "hda_auto_parser.h"
38#include "hda_jack.h"
Takashi Iwai7504b6c2013-03-18 11:25:51 +010039#include "hda_beep.h"
Takashi Iwai352f7f92012-12-19 12:52:06 +010040#include "hda_generic.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Takashi Iwai352f7f92012-12-19 12:52:06 +010043/* initialize hda_gen_spec struct */
44int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
Takashi Iwai352f7f92012-12-19 12:52:06 +010046 snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
Takashi Iwai352f7f92012-12-19 12:52:06 +010047 snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
Takashi Iwai0186f4f2013-02-07 10:45:11 +010048 snd_array_init(&spec->loopback_list, sizeof(struct hda_amp_list), 8);
Takashi Iwai38cf6f12012-12-21 14:09:42 +010049 mutex_init(&spec->pcm_mutex);
Takashi Iwai352f7f92012-12-19 12:52:06 +010050 return 0;
51}
Takashi Iwai2698ea92013-12-18 07:45:52 +010052EXPORT_SYMBOL_GPL(snd_hda_gen_spec_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Takashi Iwai12c93df2012-12-19 14:38:33 +010054struct snd_kcontrol_new *
55snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
56 const struct snd_kcontrol_new *temp)
Takashi Iwai352f7f92012-12-19 12:52:06 +010057{
58 struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
59 if (!knew)
60 return NULL;
61 *knew = *temp;
62 if (name)
63 knew->name = kstrdup(name, GFP_KERNEL);
64 else if (knew->name)
65 knew->name = kstrdup(knew->name, GFP_KERNEL);
66 if (!knew->name)
67 return NULL;
68 return knew;
69}
Takashi Iwai2698ea92013-12-18 07:45:52 +010070EXPORT_SYMBOL_GPL(snd_hda_gen_add_kctl);
Takashi Iwai352f7f92012-12-19 12:52:06 +010071
72static void free_kctls(struct hda_gen_spec *spec)
73{
74 if (spec->kctls.list) {
75 struct snd_kcontrol_new *kctl = spec->kctls.list;
76 int i;
77 for (i = 0; i < spec->kctls.used; i++)
78 kfree(kctl[i].name);
79 }
80 snd_array_free(&spec->kctls);
81}
82
Takashi Iwaia8dca462014-02-10 18:23:57 +010083static void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
Takashi Iwai352f7f92012-12-19 12:52:06 +010084{
85 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 return;
Takashi Iwai352f7f92012-12-19 12:52:06 +010087 free_kctls(spec);
Takashi Iwai352f7f92012-12-19 12:52:06 +010088 snd_array_free(&spec->paths);
Takashi Iwai0186f4f2013-02-07 10:45:11 +010089 snd_array_free(&spec->loopback_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92/*
Takashi Iwai1c70a582013-01-11 17:48:22 +010093 * store user hints
94 */
95static void parse_user_hints(struct hda_codec *codec)
96{
97 struct hda_gen_spec *spec = codec->spec;
98 int val;
99
100 val = snd_hda_get_bool_hint(codec, "jack_detect");
101 if (val >= 0)
102 codec->no_jack_detect = !val;
103 val = snd_hda_get_bool_hint(codec, "inv_jack_detect");
104 if (val >= 0)
105 codec->inv_jack_detect = !!val;
106 val = snd_hda_get_bool_hint(codec, "trigger_sense");
107 if (val >= 0)
108 codec->no_trigger_sense = !val;
109 val = snd_hda_get_bool_hint(codec, "inv_eapd");
110 if (val >= 0)
111 codec->inv_eapd = !!val;
112 val = snd_hda_get_bool_hint(codec, "pcm_format_first");
113 if (val >= 0)
114 codec->pcm_format_first = !!val;
115 val = snd_hda_get_bool_hint(codec, "sticky_stream");
116 if (val >= 0)
117 codec->no_sticky_stream = !val;
118 val = snd_hda_get_bool_hint(codec, "spdif_status_reset");
119 if (val >= 0)
120 codec->spdif_status_reset = !!val;
121 val = snd_hda_get_bool_hint(codec, "pin_amp_workaround");
122 if (val >= 0)
123 codec->pin_amp_workaround = !!val;
124 val = snd_hda_get_bool_hint(codec, "single_adc_amp");
125 if (val >= 0)
126 codec->single_adc_amp = !!val;
127
Takashi Iwaif72706b2013-01-16 18:20:07 +0100128 val = snd_hda_get_bool_hint(codec, "auto_mute");
129 if (val >= 0)
130 spec->suppress_auto_mute = !val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100131 val = snd_hda_get_bool_hint(codec, "auto_mic");
132 if (val >= 0)
133 spec->suppress_auto_mic = !val;
134 val = snd_hda_get_bool_hint(codec, "line_in_auto_switch");
135 if (val >= 0)
136 spec->line_in_auto_switch = !!val;
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200137 val = snd_hda_get_bool_hint(codec, "auto_mute_via_amp");
138 if (val >= 0)
139 spec->auto_mute_via_amp = !!val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100140 val = snd_hda_get_bool_hint(codec, "need_dac_fix");
141 if (val >= 0)
142 spec->need_dac_fix = !!val;
143 val = snd_hda_get_bool_hint(codec, "primary_hp");
144 if (val >= 0)
145 spec->no_primary_hp = !val;
Takashi Iwaida96fb52013-07-29 16:54:36 +0200146 val = snd_hda_get_bool_hint(codec, "multi_io");
147 if (val >= 0)
148 spec->no_multi_io = !val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100149 val = snd_hda_get_bool_hint(codec, "multi_cap_vol");
150 if (val >= 0)
151 spec->multi_cap_vol = !!val;
152 val = snd_hda_get_bool_hint(codec, "inv_dmic_split");
153 if (val >= 0)
154 spec->inv_dmic_split = !!val;
155 val = snd_hda_get_bool_hint(codec, "indep_hp");
156 if (val >= 0)
157 spec->indep_hp = !!val;
158 val = snd_hda_get_bool_hint(codec, "add_stereo_mix_input");
159 if (val >= 0)
160 spec->add_stereo_mix_input = !!val;
Takashi Iwaif811c3c2013-03-07 18:32:59 +0100161 /* the following two are just for compatibility */
Takashi Iwai1c70a582013-01-11 17:48:22 +0100162 val = snd_hda_get_bool_hint(codec, "add_out_jack_modes");
163 if (val >= 0)
Takashi Iwaif811c3c2013-03-07 18:32:59 +0100164 spec->add_jack_modes = !!val;
Takashi Iwai294765582013-01-17 09:52:11 +0100165 val = snd_hda_get_bool_hint(codec, "add_in_jack_modes");
166 if (val >= 0)
Takashi Iwaif811c3c2013-03-07 18:32:59 +0100167 spec->add_jack_modes = !!val;
168 val = snd_hda_get_bool_hint(codec, "add_jack_modes");
169 if (val >= 0)
170 spec->add_jack_modes = !!val;
Takashi Iwai55196ff2013-01-24 17:32:56 +0100171 val = snd_hda_get_bool_hint(codec, "power_down_unused");
172 if (val >= 0)
173 spec->power_down_unused = !!val;
Takashi Iwai967303d2013-02-19 17:12:42 +0100174 val = snd_hda_get_bool_hint(codec, "add_hp_mic");
175 if (val >= 0)
176 spec->hp_mic = !!val;
177 val = snd_hda_get_bool_hint(codec, "hp_mic_detect");
178 if (val >= 0)
179 spec->suppress_hp_mic_detect = !val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100180
181 if (!snd_hda_get_int_hint(codec, "mixer_nid", &val))
182 spec->mixer_nid = val;
183}
184
185/*
Takashi Iwai2c12c302013-01-10 09:33:29 +0100186 * pin control value accesses
187 */
188
189#define update_pin_ctl(codec, pin, val) \
190 snd_hda_codec_update_cache(codec, pin, 0, \
191 AC_VERB_SET_PIN_WIDGET_CONTROL, val)
192
193/* restore the pinctl based on the cached value */
194static inline void restore_pin_ctl(struct hda_codec *codec, hda_nid_t pin)
195{
196 update_pin_ctl(codec, pin, snd_hda_codec_get_pin_target(codec, pin));
197}
198
199/* set the pinctl target value and write it if requested */
200static void set_pin_target(struct hda_codec *codec, hda_nid_t pin,
201 unsigned int val, bool do_write)
202{
203 if (!pin)
204 return;
205 val = snd_hda_correct_pin_ctl(codec, pin, val);
206 snd_hda_codec_set_pin_target(codec, pin, val);
207 if (do_write)
208 update_pin_ctl(codec, pin, val);
209}
210
211/* set pinctl target values for all given pins */
212static void set_pin_targets(struct hda_codec *codec, int num_pins,
213 hda_nid_t *pins, unsigned int val)
214{
215 int i;
216 for (i = 0; i < num_pins; i++)
217 set_pin_target(codec, pins[i], val, false);
218}
219
220/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100221 * parsing paths
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100224/* return the position of NID in the list, or -1 if not found */
225static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
226{
227 int i;
228 for (i = 0; i < nums; i++)
229 if (list[i] == nid)
230 return i;
231 return -1;
232}
233
234/* return true if the given NID is contained in the path */
235static bool is_nid_contained(struct nid_path *path, hda_nid_t nid)
236{
237 return find_idx_in_nid_list(nid, path->path, path->depth) >= 0;
238}
239
Takashi Iwaif5172a72013-01-04 13:19:55 +0100240static struct nid_path *get_nid_path(struct hda_codec *codec,
241 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100242 int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100244 struct hda_gen_spec *spec = codec->spec;
245 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Takashi Iwai352f7f92012-12-19 12:52:06 +0100247 for (i = 0; i < spec->paths.used; i++) {
248 struct nid_path *path = snd_array_elem(&spec->paths, i);
249 if (path->depth <= 0)
250 continue;
251 if ((!from_nid || path->path[0] == from_nid) &&
Takashi Iwaif5172a72013-01-04 13:19:55 +0100252 (!to_nid || path->path[path->depth - 1] == to_nid)) {
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100253 if (!anchor_nid ||
254 (anchor_nid > 0 && is_nid_contained(path, anchor_nid)) ||
255 (anchor_nid < 0 && !is_nid_contained(path, anchor_nid)))
Takashi Iwaif5172a72013-01-04 13:19:55 +0100256 return path;
257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 }
259 return NULL;
260}
Takashi Iwaif5172a72013-01-04 13:19:55 +0100261
262/* get the path between the given NIDs;
263 * passing 0 to either @pin or @dac behaves as a wildcard
264 */
265struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
266 hda_nid_t from_nid, hda_nid_t to_nid)
267{
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100268 return get_nid_path(codec, from_nid, to_nid, 0);
Takashi Iwaif5172a72013-01-04 13:19:55 +0100269}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100270EXPORT_SYMBOL_GPL(snd_hda_get_nid_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100271
Takashi Iwai196c17662013-01-04 15:01:40 +0100272/* get the index number corresponding to the path instance;
273 * the index starts from 1, for easier checking the invalid value
274 */
275int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
276{
277 struct hda_gen_spec *spec = codec->spec;
278 struct nid_path *array = spec->paths.list;
279 ssize_t idx;
280
281 if (!spec->paths.used)
282 return 0;
283 idx = path - array;
284 if (idx < 0 || idx >= spec->paths.used)
285 return 0;
286 return idx + 1;
287}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100288EXPORT_SYMBOL_GPL(snd_hda_get_path_idx);
Takashi Iwai196c17662013-01-04 15:01:40 +0100289
290/* get the path instance corresponding to the given index number */
291struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
292{
293 struct hda_gen_spec *spec = codec->spec;
294
295 if (idx <= 0 || idx > spec->paths.used)
296 return NULL;
297 return snd_array_elem(&spec->paths, idx - 1);
298}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100299EXPORT_SYMBOL_GPL(snd_hda_get_path_from_idx);
Takashi Iwai196c17662013-01-04 15:01:40 +0100300
Takashi Iwai352f7f92012-12-19 12:52:06 +0100301/* check whether the given DAC is already found in any existing paths */
302static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
303{
304 struct hda_gen_spec *spec = codec->spec;
305 int i;
306
307 for (i = 0; i < spec->paths.used; i++) {
308 struct nid_path *path = snd_array_elem(&spec->paths, i);
309 if (path->path[0] == nid)
310 return true;
311 }
312 return false;
313}
314
315/* check whether the given two widgets can be connected */
316static bool is_reachable_path(struct hda_codec *codec,
317 hda_nid_t from_nid, hda_nid_t to_nid)
318{
319 if (!from_nid || !to_nid)
320 return false;
321 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
322}
323
324/* nid, dir and idx */
325#define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
326
327/* check whether the given ctl is already assigned in any path elements */
328static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
329{
330 struct hda_gen_spec *spec = codec->spec;
331 int i;
332
333 val &= AMP_VAL_COMPARE_MASK;
334 for (i = 0; i < spec->paths.used; i++) {
335 struct nid_path *path = snd_array_elem(&spec->paths, i);
336 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
337 return true;
338 }
339 return false;
340}
341
342/* check whether a control with the given (nid, dir, idx) was assigned */
343static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai8999bf02013-01-18 11:01:33 +0100344 int dir, int idx, int type)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100345{
346 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
Takashi Iwai8999bf02013-01-18 11:01:33 +0100347 return is_ctl_used(codec, val, type);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100348}
349
Takashi Iwai4e76a882014-02-25 12:21:03 +0100350static void print_nid_path(struct hda_codec *codec,
351 const char *pfx, struct nid_path *path)
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100352{
353 char buf[40];
Joe Perchesd82353e2014-07-05 13:02:02 -0700354 char *pos = buf;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100355 int i;
356
Joe Perchesd82353e2014-07-05 13:02:02 -0700357 *pos = 0;
358 for (i = 0; i < path->depth; i++)
359 pos += scnprintf(pos, sizeof(buf) - (pos - buf), "%s%02x",
360 pos != buf ? ":" : "",
361 path->path[i]);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100362
Joe Perchesd82353e2014-07-05 13:02:02 -0700363 codec_dbg(codec, "%s path: depth=%d '%s'\n", pfx, path->depth, buf);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100364}
365
Takashi Iwai352f7f92012-12-19 12:52:06 +0100366/* called recursively */
367static bool __parse_nid_path(struct hda_codec *codec,
368 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100369 int anchor_nid, struct nid_path *path,
370 int depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100371{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100372 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100373 int i, nums;
374
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100375 if (to_nid == anchor_nid)
376 anchor_nid = 0; /* anchor passed */
377 else if (to_nid == (hda_nid_t)(-anchor_nid))
378 return false; /* hit the exclusive nid */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100379
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100380 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100381 for (i = 0; i < nums; i++) {
382 if (conn[i] != from_nid) {
383 /* special case: when from_nid is 0,
384 * try to find an empty DAC
385 */
386 if (from_nid ||
387 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
388 is_dac_already_used(codec, conn[i]))
389 continue;
390 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100391 /* anchor is not requested or already passed? */
392 if (anchor_nid <= 0)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100393 goto found;
394 }
395 if (depth >= MAX_NID_PATH_DEPTH)
396 return false;
397 for (i = 0; i < nums; i++) {
398 unsigned int type;
399 type = get_wcaps_type(get_wcaps(codec, conn[i]));
400 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
401 type == AC_WID_PIN)
402 continue;
403 if (__parse_nid_path(codec, from_nid, conn[i],
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100404 anchor_nid, path, depth + 1))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100405 goto found;
406 }
407 return false;
408
409 found:
410 path->path[path->depth] = conn[i];
411 path->idx[path->depth + 1] = i;
412 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
413 path->multi[path->depth + 1] = 1;
414 path->depth++;
415 return true;
416}
417
418/* parse the widget path from the given nid to the target nid;
419 * when @from_nid is 0, try to find an empty DAC;
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100420 * when @anchor_nid is set to a positive value, only paths through the widget
421 * with the given value are evaluated.
422 * when @anchor_nid is set to a negative value, paths through the widget
423 * with the negative of given value are excluded, only other paths are chosen.
424 * when @anchor_nid is zero, no special handling about path selection.
Takashi Iwai352f7f92012-12-19 12:52:06 +0100425 */
426bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100427 hda_nid_t to_nid, int anchor_nid,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100428 struct nid_path *path)
429{
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100430 if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100431 path->path[path->depth] = to_nid;
432 path->depth++;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100433 return true;
434 }
435 return false;
436}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100437EXPORT_SYMBOL_GPL(snd_hda_parse_nid_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100440 * parse the path between the given NIDs and add to the path list.
441 * if no valid path is found, return NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100443struct nid_path *
444snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100445 hda_nid_t to_nid, int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100447 struct hda_gen_spec *spec = codec->spec;
448 struct nid_path *path;
449
450 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
451 return NULL;
452
Takashi Iwaif5172a72013-01-04 13:19:55 +0100453 /* check whether the path has been already added */
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100454 path = get_nid_path(codec, from_nid, to_nid, anchor_nid);
Takashi Iwaif5172a72013-01-04 13:19:55 +0100455 if (path)
456 return path;
457
Takashi Iwai352f7f92012-12-19 12:52:06 +0100458 path = snd_array_new(&spec->paths);
459 if (!path)
460 return NULL;
461 memset(path, 0, sizeof(*path));
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100462 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100463 return path;
464 /* push back */
465 spec->paths.used--;
466 return NULL;
467}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100468EXPORT_SYMBOL_GPL(snd_hda_add_new_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100469
Takashi Iwai980428c2013-01-09 09:28:20 +0100470/* clear the given path as invalid so that it won't be picked up later */
471static void invalidate_nid_path(struct hda_codec *codec, int idx)
472{
473 struct nid_path *path = snd_hda_get_path_from_idx(codec, idx);
474 if (!path)
475 return;
476 memset(path, 0, sizeof(*path));
477}
478
Takashi Iwai36907392013-12-10 17:29:26 +0100479/* return a DAC if paired to the given pin by codec driver */
480static hda_nid_t get_preferred_dac(struct hda_codec *codec, hda_nid_t pin)
481{
482 struct hda_gen_spec *spec = codec->spec;
483 const hda_nid_t *list = spec->preferred_dacs;
484
485 if (!list)
486 return 0;
487 for (; *list; list += 2)
488 if (*list == pin)
489 return list[1];
490 return 0;
491}
492
Takashi Iwai352f7f92012-12-19 12:52:06 +0100493/* look for an empty DAC slot */
494static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
495 bool is_digital)
496{
497 struct hda_gen_spec *spec = codec->spec;
498 bool cap_digital;
499 int i;
500
501 for (i = 0; i < spec->num_all_dacs; i++) {
502 hda_nid_t nid = spec->all_dacs[i];
503 if (!nid || is_dac_already_used(codec, nid))
504 continue;
505 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
506 if (is_digital != cap_digital)
507 continue;
508 if (is_reachable_path(codec, nid, pin))
509 return nid;
510 }
511 return 0;
512}
513
514/* replace the channels in the composed amp value with the given number */
515static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
516{
517 val &= ~(0x3U << 16);
518 val |= chs << 16;
519 return val;
520}
521
522/* check whether the widget has the given amp capability for the direction */
523static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
524 int dir, unsigned int bits)
525{
526 if (!nid)
527 return false;
528 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
529 if (query_amp_caps(codec, nid, dir) & bits)
530 return true;
531 return false;
532}
533
David Henningsson99a55922013-01-16 15:58:44 +0100534static bool same_amp_caps(struct hda_codec *codec, hda_nid_t nid1,
535 hda_nid_t nid2, int dir)
536{
537 if (!(get_wcaps(codec, nid1) & (1 << (dir + 1))))
538 return !(get_wcaps(codec, nid2) & (1 << (dir + 1)));
539 return (query_amp_caps(codec, nid1, dir) ==
540 query_amp_caps(codec, nid2, dir));
541}
542
Takashi Iwai352f7f92012-12-19 12:52:06 +0100543#define nid_has_mute(codec, nid, dir) \
Takashi Iwaif69910d2013-08-08 09:32:37 +0200544 check_amp_caps(codec, nid, dir, (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100545#define nid_has_volume(codec, nid, dir) \
546 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
547
548/* look for a widget suitable for assigning a mute switch in the path */
549static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
550 struct nid_path *path)
551{
552 int i;
553
554 for (i = path->depth - 1; i >= 0; i--) {
555 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
556 return path->path[i];
557 if (i != path->depth - 1 && i != 0 &&
558 nid_has_mute(codec, path->path[i], HDA_INPUT))
559 return path->path[i];
560 }
561 return 0;
562}
563
564/* look for a widget suitable for assigning a volume ctl in the path */
565static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
566 struct nid_path *path)
567{
Takashi Iwaia1114a82013-11-04 16:32:01 +0100568 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100569 int i;
570
571 for (i = path->depth - 1; i >= 0; i--) {
Takashi Iwaia1114a82013-11-04 16:32:01 +0100572 hda_nid_t nid = path->path[i];
573 if ((spec->out_vol_mask >> nid) & 1)
574 continue;
575 if (nid_has_volume(codec, nid, HDA_OUTPUT))
576 return nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100577 }
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200578 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579}
580
581/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100582 * path activation / deactivation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100584
585/* can have the amp-in capability? */
586static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100588 hda_nid_t nid = path->path[idx];
589 unsigned int caps = get_wcaps(codec, nid);
590 unsigned int type = get_wcaps_type(caps);
591
592 if (!(caps & AC_WCAP_IN_AMP))
593 return false;
594 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
595 return false;
596 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597}
598
Takashi Iwai352f7f92012-12-19 12:52:06 +0100599/* can have the amp-out capability? */
600static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100602 hda_nid_t nid = path->path[idx];
603 unsigned int caps = get_wcaps(codec, nid);
604 unsigned int type = get_wcaps_type(caps);
605
606 if (!(caps & AC_WCAP_OUT_AMP))
607 return false;
608 if (type == AC_WID_PIN && !idx) /* only for output pins */
609 return false;
610 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611}
612
Takashi Iwai352f7f92012-12-19 12:52:06 +0100613/* check whether the given (nid,dir,idx) is active */
614static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai7dddf2ae2013-01-24 16:31:35 +0100615 unsigned int dir, unsigned int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100617 struct hda_gen_spec *spec = codec->spec;
618 int i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Takashi Iwai352f7f92012-12-19 12:52:06 +0100620 for (n = 0; n < spec->paths.used; n++) {
621 struct nid_path *path = snd_array_elem(&spec->paths, n);
622 if (!path->active)
623 continue;
624 for (i = 0; i < path->depth; i++) {
625 if (path->path[i] == nid) {
626 if (dir == HDA_OUTPUT || path->idx[i] == idx)
627 return true;
628 break;
629 }
630 }
631 }
632 return false;
633}
634
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +0200635/* check whether the NID is referred by any active paths */
636#define is_active_nid_for_any(codec, nid) \
637 is_active_nid(codec, nid, HDA_OUTPUT, 0)
638
Takashi Iwai352f7f92012-12-19 12:52:06 +0100639/* get the default amp value for the target state */
640static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai8999bf02013-01-18 11:01:33 +0100641 int dir, unsigned int caps, bool enable)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100642{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100643 unsigned int val = 0;
644
Takashi Iwai352f7f92012-12-19 12:52:06 +0100645 if (caps & AC_AMPCAP_NUM_STEPS) {
646 /* set to 0dB */
647 if (enable)
648 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
649 }
Takashi Iwaif69910d2013-08-08 09:32:37 +0200650 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100651 if (!enable)
652 val |= HDA_AMP_MUTE;
653 }
654 return val;
655}
656
657/* initialize the amp value (only at the first time) */
658static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
659{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100660 unsigned int caps = query_amp_caps(codec, nid, dir);
661 int val = get_amp_val_to_activate(codec, nid, dir, caps, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100662 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
663}
664
Takashi Iwai8999bf02013-01-18 11:01:33 +0100665/* calculate amp value mask we can modify;
666 * if the given amp is controlled by mixers, don't touch it
667 */
668static unsigned int get_amp_mask_to_modify(struct hda_codec *codec,
669 hda_nid_t nid, int dir, int idx,
670 unsigned int caps)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100671{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100672 unsigned int mask = 0xff;
673
Takashi Iwaif69910d2013-08-08 09:32:37 +0200674 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
Takashi Iwai8999bf02013-01-18 11:01:33 +0100675 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_MUTE_CTL))
676 mask &= ~0x80;
677 }
678 if (caps & AC_AMPCAP_NUM_STEPS) {
679 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
680 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
681 mask &= ~0x7f;
682 }
683 return mask;
684}
685
686static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
687 int idx, int idx_to_check, bool enable)
688{
689 unsigned int caps;
690 unsigned int mask, val;
691
Takashi Iwai7dddf2ae2013-01-24 16:31:35 +0100692 if (!enable && is_active_nid(codec, nid, dir, idx_to_check))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100693 return;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100694
695 caps = query_amp_caps(codec, nid, dir);
696 val = get_amp_val_to_activate(codec, nid, dir, caps, enable);
697 mask = get_amp_mask_to_modify(codec, nid, dir, idx_to_check, caps);
698 if (!mask)
699 return;
700
701 val &= mask;
702 snd_hda_codec_amp_stereo(codec, nid, dir, idx, mask, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100703}
704
705static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
706 int i, bool enable)
707{
708 hda_nid_t nid = path->path[i];
709 init_amp(codec, nid, HDA_OUTPUT, 0);
Takashi Iwai8999bf02013-01-18 11:01:33 +0100710 activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100711}
712
713static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
714 int i, bool enable, bool add_aamix)
715{
716 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100717 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100718 int n, nums, idx;
719 int type;
720 hda_nid_t nid = path->path[i];
721
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100722 nums = snd_hda_get_conn_list(codec, nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100723 type = get_wcaps_type(get_wcaps(codec, nid));
724 if (type == AC_WID_PIN ||
725 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
726 nums = 1;
727 idx = 0;
728 } else
729 idx = path->idx[i];
730
731 for (n = 0; n < nums; n++)
732 init_amp(codec, nid, HDA_INPUT, n);
733
Takashi Iwai352f7f92012-12-19 12:52:06 +0100734 /* here is a little bit tricky in comparison with activate_amp_out();
735 * when aa-mixer is available, we need to enable the path as well
736 */
737 for (n = 0; n < nums; n++) {
Takashi Iwaie4a395e2013-01-23 17:00:31 +0100738 if (n != idx && (!add_aamix || conn[n] != spec->mixer_merge_nid))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100739 continue;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100740 activate_amp(codec, nid, HDA_INPUT, n, idx, enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 }
742}
743
Takashi Iwai352f7f92012-12-19 12:52:06 +0100744/* activate or deactivate the given path
745 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100747void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
748 bool enable, bool add_aamix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
Takashi Iwai55196ff2013-01-24 17:32:56 +0100750 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100751 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Takashi Iwai352f7f92012-12-19 12:52:06 +0100753 if (!enable)
754 path->active = false;
755
756 for (i = path->depth - 1; i >= 0; i--) {
Takashi Iwai55196ff2013-01-24 17:32:56 +0100757 hda_nid_t nid = path->path[i];
758 if (enable && spec->power_down_unused) {
759 /* make sure the widget is powered up */
760 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0))
761 snd_hda_codec_write(codec, nid, 0,
762 AC_VERB_SET_POWER_STATE,
763 AC_PWRST_D0);
764 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100765 if (enable && path->multi[i])
Takashi Iwai8f0972d2014-01-27 16:26:16 +0100766 snd_hda_codec_update_cache(codec, nid, 0,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100767 AC_VERB_SET_CONNECT_SEL,
768 path->idx[i]);
769 if (has_amp_in(codec, path, i))
770 activate_amp_in(codec, path, i, enable, add_aamix);
771 if (has_amp_out(codec, path, i))
772 activate_amp_out(codec, path, i, enable);
773 }
774
775 if (enable)
776 path->active = true;
777}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100778EXPORT_SYMBOL_GPL(snd_hda_activate_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100779
Takashi Iwai55196ff2013-01-24 17:32:56 +0100780/* if the given path is inactive, put widgets into D3 (only if suitable) */
781static void path_power_down_sync(struct hda_codec *codec, struct nid_path *path)
782{
783 struct hda_gen_spec *spec = codec->spec;
Jiri Slaby868211d2013-04-04 22:32:10 +0200784 bool changed = false;
Takashi Iwai55196ff2013-01-24 17:32:56 +0100785 int i;
786
787 if (!spec->power_down_unused || path->active)
788 return;
789
790 for (i = 0; i < path->depth; i++) {
791 hda_nid_t nid = path->path[i];
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +0200792 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D3) &&
793 !is_active_nid_for_any(codec, nid)) {
Takashi Iwai55196ff2013-01-24 17:32:56 +0100794 snd_hda_codec_write(codec, nid, 0,
795 AC_VERB_SET_POWER_STATE,
796 AC_PWRST_D3);
797 changed = true;
798 }
799 }
800
801 if (changed) {
802 msleep(10);
803 snd_hda_codec_read(codec, path->path[0], 0,
804 AC_VERB_GET_POWER_STATE, 0);
805 }
806}
807
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100808/* turn on/off EAPD on the given pin */
809static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
810{
811 struct hda_gen_spec *spec = codec->spec;
812 if (spec->own_eapd_ctl ||
813 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
814 return;
Takashi Iwai05909d5c2013-05-31 19:55:54 +0200815 if (spec->keep_eapd_on && !enable)
816 return;
Takashi Iwai468ac412013-11-12 11:36:00 +0100817 if (codec->inv_eapd)
818 enable = !enable;
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100819 snd_hda_codec_update_cache(codec, pin, 0,
820 AC_VERB_SET_EAPD_BTLENABLE,
821 enable ? 0x02 : 0x00);
822}
823
Takashi Iwai3e367f12013-01-23 17:07:23 +0100824/* re-initialize the path specified by the given path index */
825static void resume_path_from_idx(struct hda_codec *codec, int path_idx)
826{
827 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
828 if (path)
829 snd_hda_activate_path(codec, path, path->active, false);
830}
831
Takashi Iwai352f7f92012-12-19 12:52:06 +0100832
833/*
834 * Helper functions for creating mixer ctl elements
835 */
836
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200837static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
838 struct snd_ctl_elem_value *ucontrol);
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200839static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
840 struct snd_ctl_elem_value *ucontrol);
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200841
Takashi Iwai352f7f92012-12-19 12:52:06 +0100842enum {
843 HDA_CTL_WIDGET_VOL,
844 HDA_CTL_WIDGET_MUTE,
845 HDA_CTL_BIND_MUTE,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100846};
847static const struct snd_kcontrol_new control_templates[] = {
848 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200849 /* only the put callback is replaced for handling the special mute */
850 {
851 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
852 .subdevice = HDA_SUBDEV_AMP_FLAG,
853 .info = snd_hda_mixer_amp_switch_info,
854 .get = snd_hda_mixer_amp_switch_get,
855 .put = hda_gen_mixer_mute_put, /* replaced */
856 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
857 },
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200858 {
859 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
860 .info = snd_hda_mixer_amp_switch_info,
861 .get = snd_hda_mixer_bind_switch_get,
862 .put = hda_gen_bind_mute_put, /* replaced */
863 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
864 },
Takashi Iwai352f7f92012-12-19 12:52:06 +0100865};
866
867/* add dynamic controls from template */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100868static struct snd_kcontrol_new *
869add_control(struct hda_gen_spec *spec, int type, const char *name,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100870 int cidx, unsigned long val)
871{
872 struct snd_kcontrol_new *knew;
873
Takashi Iwai12c93df2012-12-19 14:38:33 +0100874 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100875 if (!knew)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100876 return NULL;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100877 knew->index = cidx;
878 if (get_amp_nid_(val))
879 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
880 knew->private_value = val;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100881 return knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100882}
883
884static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
885 const char *pfx, const char *dir,
886 const char *sfx, int cidx, unsigned long val)
887{
Takashi Iwai975cc022013-06-28 11:56:49 +0200888 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai352f7f92012-12-19 12:52:06 +0100889 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100890 if (!add_control(spec, type, name, cidx, val))
891 return -ENOMEM;
892 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100893}
894
895#define add_pb_vol_ctrl(spec, type, pfx, val) \
896 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
897#define add_pb_sw_ctrl(spec, type, pfx, val) \
898 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
899#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
900 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
901#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
902 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
903
904static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
905 unsigned int chs, struct nid_path *path)
906{
907 unsigned int val;
908 if (!path)
909 return 0;
910 val = path->ctls[NID_PATH_VOL_CTL];
911 if (!val)
912 return 0;
913 val = amp_val_replace_channels(val, chs);
914 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
915}
916
917/* return the channel bits suitable for the given path->ctls[] */
918static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
919 int type)
920{
921 int chs = 1; /* mono (left only) */
922 if (path) {
923 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
924 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
925 chs = 3; /* stereo */
926 }
927 return chs;
928}
929
930static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
931 struct nid_path *path)
932{
933 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
934 return add_vol_ctl(codec, pfx, cidx, chs, path);
935}
936
937/* create a mute-switch for the given mixer widget;
938 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
939 */
940static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
941 unsigned int chs, struct nid_path *path)
942{
943 unsigned int val;
944 int type = HDA_CTL_WIDGET_MUTE;
945
946 if (!path)
947 return 0;
948 val = path->ctls[NID_PATH_MUTE_CTL];
949 if (!val)
950 return 0;
951 val = amp_val_replace_channels(val, chs);
952 if (get_amp_direction_(val) == HDA_INPUT) {
953 hda_nid_t nid = get_amp_nid_(val);
954 int nums = snd_hda_get_num_conns(codec, nid);
955 if (nums > 1) {
956 type = HDA_CTL_BIND_MUTE;
957 val |= nums << 19;
958 }
959 }
960 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
961}
962
963static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
964 int cidx, struct nid_path *path)
965{
966 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
967 return add_sw_ctl(codec, pfx, cidx, chs, path);
968}
969
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200970/* playback mute control with the software mute bit check */
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200971static void sync_auto_mute_bits(struct snd_kcontrol *kcontrol,
972 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200973{
974 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
975 struct hda_gen_spec *spec = codec->spec;
976
977 if (spec->auto_mute_via_amp) {
978 hda_nid_t nid = get_amp_nid(kcontrol);
979 bool enabled = !((spec->mute_bits >> nid) & 1);
980 ucontrol->value.integer.value[0] &= enabled;
981 ucontrol->value.integer.value[1] &= enabled;
982 }
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200983}
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200984
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200985static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
986 struct snd_ctl_elem_value *ucontrol)
987{
988 sync_auto_mute_bits(kcontrol, ucontrol);
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200989 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
990}
991
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200992static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
993 struct snd_ctl_elem_value *ucontrol)
994{
995 sync_auto_mute_bits(kcontrol, ucontrol);
996 return snd_hda_mixer_bind_switch_put(kcontrol, ucontrol);
997}
998
Takashi Iwai247d85e2013-01-17 16:18:11 +0100999/* any ctl assigned to the path with the given index? */
1000static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
1001{
1002 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
1003 return path && path->ctls[ctl_type];
1004}
1005
Takashi Iwai352f7f92012-12-19 12:52:06 +01001006static const char * const channel_name[4] = {
1007 "Front", "Surround", "CLFE", "Side"
1008};
1009
1010/* give some appropriate ctl name prefix for the given line out channel */
Takashi Iwai247d85e2013-01-17 16:18:11 +01001011static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
1012 int *index, int ctl_type)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001013{
Takashi Iwai247d85e2013-01-17 16:18:11 +01001014 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001015 struct auto_pin_cfg *cfg = &spec->autocfg;
1016
1017 *index = 0;
1018 if (cfg->line_outs == 1 && !spec->multi_ios &&
Takashi Iwai247d85e2013-01-17 16:18:11 +01001019 !cfg->hp_outs && !cfg->speaker_outs)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001020 return spec->vmaster_mute.hook ? "PCM" : "Master";
1021
1022 /* if there is really a single DAC used in the whole output paths,
1023 * use it master (or "PCM" if a vmaster hook is present)
1024 */
1025 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
1026 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
1027 return spec->vmaster_mute.hook ? "PCM" : "Master";
1028
Takashi Iwai247d85e2013-01-17 16:18:11 +01001029 /* multi-io channels */
1030 if (ch >= cfg->line_outs)
1031 return channel_name[ch];
1032
Takashi Iwai352f7f92012-12-19 12:52:06 +01001033 switch (cfg->line_out_type) {
1034 case AUTO_PIN_SPEAKER_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +01001035 /* if the primary channel vol/mute is shared with HP volume,
1036 * don't name it as Speaker
1037 */
1038 if (!ch && cfg->hp_outs &&
1039 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
1040 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001041 if (cfg->line_outs == 1)
1042 return "Speaker";
1043 if (cfg->line_outs == 2)
1044 return ch ? "Bass Speaker" : "Speaker";
1045 break;
1046 case AUTO_PIN_HP_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +01001047 /* if the primary channel vol/mute is shared with spk volume,
1048 * don't name it as Headphone
1049 */
1050 if (!ch && cfg->speaker_outs &&
1051 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
1052 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001053 /* for multi-io case, only the primary out */
1054 if (ch && spec->multi_ios)
1055 break;
1056 *index = ch;
1057 return "Headphone";
Takashi Iwai352f7f92012-12-19 12:52:06 +01001058 }
Takashi Iwai247d85e2013-01-17 16:18:11 +01001059
1060 /* for a single channel output, we don't have to name the channel */
1061 if (cfg->line_outs == 1 && !spec->multi_ios)
1062 return "PCM";
1063
Takashi Iwai352f7f92012-12-19 12:52:06 +01001064 if (ch >= ARRAY_SIZE(channel_name)) {
1065 snd_BUG();
1066 return "PCM";
1067 }
1068
1069 return channel_name[ch];
1070}
1071
1072/*
1073 * Parse output paths
1074 */
1075
1076/* badness definition */
1077enum {
1078 /* No primary DAC is found for the main output */
1079 BAD_NO_PRIMARY_DAC = 0x10000,
1080 /* No DAC is found for the extra output */
1081 BAD_NO_DAC = 0x4000,
1082 /* No possible multi-ios */
Takashi Iwai1d739062013-02-13 14:17:32 +01001083 BAD_MULTI_IO = 0x120,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001084 /* No individual DAC for extra output */
1085 BAD_NO_EXTRA_DAC = 0x102,
1086 /* No individual DAC for extra surrounds */
1087 BAD_NO_EXTRA_SURR_DAC = 0x101,
1088 /* Primary DAC shared with main surrounds */
1089 BAD_SHARED_SURROUND = 0x100,
Takashi Iwai55a63d42013-03-21 17:20:12 +01001090 /* No independent HP possible */
Takashi Iwaibec8e682013-03-22 15:10:08 +01001091 BAD_NO_INDEP_HP = 0x10,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001092 /* Primary DAC shared with main CLFE */
1093 BAD_SHARED_CLFE = 0x10,
1094 /* Primary DAC shared with extra surrounds */
1095 BAD_SHARED_EXTRA_SURROUND = 0x10,
1096 /* Volume widget is shared */
1097 BAD_SHARED_VOL = 0x10,
1098};
1099
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001100/* look for widgets in the given path which are appropriate for
Takashi Iwai352f7f92012-12-19 12:52:06 +01001101 * volume and mute controls, and assign the values to ctls[].
1102 *
1103 * When no appropriate widget is found in the path, the badness value
1104 * is incremented depending on the situation. The function returns the
1105 * total badness for both volume and mute controls.
1106 */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001107static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001108{
Takashi Iwaid89c6c02014-09-01 10:07:04 +02001109 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001110 hda_nid_t nid;
1111 unsigned int val;
1112 int badness = 0;
1113
1114 if (!path)
1115 return BAD_SHARED_VOL * 2;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001116
1117 if (path->ctls[NID_PATH_VOL_CTL] ||
1118 path->ctls[NID_PATH_MUTE_CTL])
1119 return 0; /* already evaluated */
1120
Takashi Iwai352f7f92012-12-19 12:52:06 +01001121 nid = look_for_out_vol_nid(codec, path);
1122 if (nid) {
1123 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
Takashi Iwaid89c6c02014-09-01 10:07:04 +02001124 if (spec->dac_min_mute)
1125 val |= HDA_AMP_VAL_MIN_MUTE;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001126 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1127 badness += BAD_SHARED_VOL;
1128 else
1129 path->ctls[NID_PATH_VOL_CTL] = val;
1130 } else
1131 badness += BAD_SHARED_VOL;
1132 nid = look_for_out_mute_nid(codec, path);
1133 if (nid) {
1134 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1135 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1136 nid_has_mute(codec, nid, HDA_OUTPUT))
1137 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1138 else
1139 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1140 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1141 badness += BAD_SHARED_VOL;
1142 else
1143 path->ctls[NID_PATH_MUTE_CTL] = val;
1144 } else
1145 badness += BAD_SHARED_VOL;
1146 return badness;
1147}
1148
Takashi Iwai98bd1112013-03-22 14:53:50 +01001149const struct badness_table hda_main_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001150 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1151 .no_dac = BAD_NO_DAC,
1152 .shared_primary = BAD_NO_PRIMARY_DAC,
1153 .shared_surr = BAD_SHARED_SURROUND,
1154 .shared_clfe = BAD_SHARED_CLFE,
1155 .shared_surr_main = BAD_SHARED_SURROUND,
1156};
Takashi Iwai2698ea92013-12-18 07:45:52 +01001157EXPORT_SYMBOL_GPL(hda_main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001158
Takashi Iwai98bd1112013-03-22 14:53:50 +01001159const struct badness_table hda_extra_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001160 .no_primary_dac = BAD_NO_DAC,
1161 .no_dac = BAD_NO_DAC,
1162 .shared_primary = BAD_NO_EXTRA_DAC,
1163 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1164 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1165 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1166};
Takashi Iwai2698ea92013-12-18 07:45:52 +01001167EXPORT_SYMBOL_GPL(hda_extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001168
Takashi Iwai7385df62013-01-07 09:50:52 +01001169/* get the DAC of the primary output corresponding to the given array index */
1170static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1171{
1172 struct hda_gen_spec *spec = codec->spec;
1173 struct auto_pin_cfg *cfg = &spec->autocfg;
1174
1175 if (cfg->line_outs > idx)
1176 return spec->private_dac_nids[idx];
1177 idx -= cfg->line_outs;
1178 if (spec->multi_ios > idx)
1179 return spec->multi_io[idx].dac;
1180 return 0;
1181}
1182
1183/* return the DAC if it's reachable, otherwise zero */
1184static inline hda_nid_t try_dac(struct hda_codec *codec,
1185 hda_nid_t dac, hda_nid_t pin)
1186{
1187 return is_reachable_path(codec, dac, pin) ? dac : 0;
1188}
1189
Takashi Iwai352f7f92012-12-19 12:52:06 +01001190/* try to assign DACs to pins and return the resultant badness */
1191static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1192 const hda_nid_t *pins, hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001193 int *path_idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001194 const struct badness_table *bad)
1195{
1196 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001197 int i, j;
1198 int badness = 0;
1199 hda_nid_t dac;
1200
1201 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 return 0;
1203
Takashi Iwai352f7f92012-12-19 12:52:06 +01001204 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001205 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001206 hda_nid_t pin = pins[i];
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001207
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001208 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1209 if (path) {
1210 badness += assign_out_path_ctls(codec, path);
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001211 continue;
1212 }
1213
Takashi Iwai36907392013-12-10 17:29:26 +01001214 dacs[i] = get_preferred_dac(codec, pin);
1215 if (dacs[i]) {
1216 if (is_dac_already_used(codec, dacs[i]))
1217 badness += bad->shared_primary;
1218 }
1219
1220 if (!dacs[i])
1221 dacs[i] = look_for_dac(codec, pin, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001222 if (!dacs[i] && !i) {
Takashi Iwai980428c2013-01-09 09:28:20 +01001223 /* try to steal the DAC of surrounds for the front */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001224 for (j = 1; j < num_outs; j++) {
1225 if (is_reachable_path(codec, dacs[j], pin)) {
1226 dacs[0] = dacs[j];
1227 dacs[j] = 0;
Takashi Iwai980428c2013-01-09 09:28:20 +01001228 invalidate_nid_path(codec, path_idx[j]);
Takashi Iwai196c17662013-01-04 15:01:40 +01001229 path_idx[j] = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001230 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 }
1232 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001233 }
1234 dac = dacs[i];
1235 if (!dac) {
Takashi Iwai7385df62013-01-07 09:50:52 +01001236 if (num_outs > 2)
1237 dac = try_dac(codec, get_primary_out(codec, i), pin);
1238 if (!dac)
1239 dac = try_dac(codec, dacs[0], pin);
1240 if (!dac)
1241 dac = try_dac(codec, get_primary_out(codec, i), pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001242 if (dac) {
1243 if (!i)
1244 badness += bad->shared_primary;
1245 else if (i == 1)
1246 badness += bad->shared_surr;
1247 else
1248 badness += bad->shared_clfe;
1249 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1250 dac = spec->private_dac_nids[0];
1251 badness += bad->shared_surr_main;
1252 } else if (!i)
1253 badness += bad->no_primary_dac;
1254 else
1255 badness += bad->no_dac;
1256 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001257 if (!dac)
1258 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001259 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001260 if (!path && !i && spec->mixer_nid) {
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001261 /* try with aamix */
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001262 path = snd_hda_add_new_path(codec, dac, pin, 0);
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001263 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001264 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001265 dac = dacs[i] = 0;
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001266 badness += bad->no_dac;
1267 } else {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001268 /* print_nid_path(codec, "output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001269 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001270 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001271 badness += assign_out_path_ctls(codec, path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001272 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001273 }
1274
1275 return badness;
1276}
1277
1278/* return NID if the given pin has only a single connection to a certain DAC */
1279static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1280{
1281 struct hda_gen_spec *spec = codec->spec;
1282 int i;
1283 hda_nid_t nid_found = 0;
1284
1285 for (i = 0; i < spec->num_all_dacs; i++) {
1286 hda_nid_t nid = spec->all_dacs[i];
1287 if (!nid || is_dac_already_used(codec, nid))
1288 continue;
1289 if (is_reachable_path(codec, nid, pin)) {
1290 if (nid_found)
1291 return 0;
1292 nid_found = nid;
1293 }
1294 }
1295 return nid_found;
1296}
1297
1298/* check whether the given pin can be a multi-io pin */
1299static bool can_be_multiio_pin(struct hda_codec *codec,
1300 unsigned int location, hda_nid_t nid)
1301{
1302 unsigned int defcfg, caps;
1303
1304 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1305 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1306 return false;
1307 if (location && get_defcfg_location(defcfg) != location)
1308 return false;
1309 caps = snd_hda_query_pin_caps(codec, nid);
1310 if (!(caps & AC_PINCAP_OUT))
1311 return false;
1312 return true;
1313}
1314
Takashi Iwaie22aab72013-01-04 14:50:04 +01001315/* count the number of input pins that are capable to be multi-io */
1316static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1317{
1318 struct hda_gen_spec *spec = codec->spec;
1319 struct auto_pin_cfg *cfg = &spec->autocfg;
1320 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1321 unsigned int location = get_defcfg_location(defcfg);
1322 int type, i;
1323 int num_pins = 0;
1324
1325 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1326 for (i = 0; i < cfg->num_inputs; i++) {
1327 if (cfg->inputs[i].type != type)
1328 continue;
1329 if (can_be_multiio_pin(codec, location,
1330 cfg->inputs[i].pin))
1331 num_pins++;
1332 }
1333 }
1334 return num_pins;
1335}
1336
Takashi Iwai352f7f92012-12-19 12:52:06 +01001337/*
1338 * multi-io helper
1339 *
1340 * When hardwired is set, try to fill ony hardwired pins, and returns
1341 * zero if any pins are filled, non-zero if nothing found.
1342 * When hardwired is off, try to fill possible input pins, and returns
1343 * the badness value.
1344 */
1345static int fill_multi_ios(struct hda_codec *codec,
1346 hda_nid_t reference_pin,
Takashi Iwaie22aab72013-01-04 14:50:04 +01001347 bool hardwired)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001348{
1349 struct hda_gen_spec *spec = codec->spec;
1350 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie22aab72013-01-04 14:50:04 +01001351 int type, i, j, num_pins, old_pins;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001352 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1353 unsigned int location = get_defcfg_location(defcfg);
1354 int badness = 0;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001355 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001356
1357 old_pins = spec->multi_ios;
1358 if (old_pins >= 2)
1359 goto end_fill;
1360
Takashi Iwaie22aab72013-01-04 14:50:04 +01001361 num_pins = count_multiio_pins(codec, reference_pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001362 if (num_pins < 2)
1363 goto end_fill;
1364
Takashi Iwai352f7f92012-12-19 12:52:06 +01001365 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1366 for (i = 0; i < cfg->num_inputs; i++) {
1367 hda_nid_t nid = cfg->inputs[i].pin;
1368 hda_nid_t dac = 0;
1369
1370 if (cfg->inputs[i].type != type)
1371 continue;
1372 if (!can_be_multiio_pin(codec, location, nid))
1373 continue;
1374 for (j = 0; j < spec->multi_ios; j++) {
1375 if (nid == spec->multi_io[j].pin)
1376 break;
1377 }
1378 if (j < spec->multi_ios)
1379 continue;
1380
Takashi Iwai352f7f92012-12-19 12:52:06 +01001381 if (hardwired)
1382 dac = get_dac_if_single(codec, nid);
1383 else if (!dac)
1384 dac = look_for_dac(codec, nid, false);
1385 if (!dac) {
1386 badness++;
1387 continue;
1388 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001389 path = snd_hda_add_new_path(codec, dac, nid,
1390 -spec->mixer_nid);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001391 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001392 badness++;
1393 continue;
1394 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01001395 /* print_nid_path(codec, "multiio", path); */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001396 spec->multi_io[spec->multi_ios].pin = nid;
1397 spec->multi_io[spec->multi_ios].dac = dac;
Takashi Iwai196c17662013-01-04 15:01:40 +01001398 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1399 snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001400 spec->multi_ios++;
1401 if (spec->multi_ios >= 2)
1402 break;
1403 }
1404 }
1405 end_fill:
1406 if (badness)
1407 badness = BAD_MULTI_IO;
1408 if (old_pins == spec->multi_ios) {
1409 if (hardwired)
1410 return 1; /* nothing found */
1411 else
1412 return badness; /* no badness if nothing found */
1413 }
1414 if (!hardwired && spec->multi_ios < 2) {
1415 /* cancel newly assigned paths */
1416 spec->paths.used -= spec->multi_ios - old_pins;
1417 spec->multi_ios = old_pins;
1418 return badness;
1419 }
1420
1421 /* assign volume and mute controls */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001422 for (i = old_pins; i < spec->multi_ios; i++) {
1423 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1424 badness += assign_out_path_ctls(codec, path);
1425 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001426
1427 return badness;
1428}
1429
1430/* map DACs for all pins in the list if they are single connections */
1431static bool map_singles(struct hda_codec *codec, int outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001432 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001433{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001434 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001435 int i;
1436 bool found = false;
1437 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001438 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001439 hda_nid_t dac;
1440 if (dacs[i])
1441 continue;
1442 dac = get_dac_if_single(codec, pins[i]);
1443 if (!dac)
1444 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001445 path = snd_hda_add_new_path(codec, dac, pins[i],
1446 -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001447 if (!path && !i && spec->mixer_nid)
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001448 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001449 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001450 dacs[i] = dac;
1451 found = true;
Takashi Iwai4e76a882014-02-25 12:21:03 +01001452 /* print_nid_path(codec, "output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001453 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001454 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001455 }
1456 }
1457 return found;
1458}
1459
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001460/* create a new path including aamix if available, and return its index */
1461static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1462{
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001463 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001464 struct nid_path *path;
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001465 hda_nid_t path_dac, dac, pin;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001466
1467 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001468 if (!path || !path->depth ||
1469 is_nid_contained(path, spec->mixer_nid))
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001470 return 0;
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001471 path_dac = path->path[0];
1472 dac = spec->private_dac_nids[0];
Takashi Iwaif87498b2013-01-21 14:24:31 +01001473 pin = path->path[path->depth - 1];
1474 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1475 if (!path) {
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001476 if (dac != path_dac)
1477 dac = path_dac;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001478 else if (spec->multiout.hp_out_nid[0])
1479 dac = spec->multiout.hp_out_nid[0];
1480 else if (spec->multiout.extra_out_nid[0])
1481 dac = spec->multiout.extra_out_nid[0];
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001482 else
1483 dac = 0;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001484 if (dac)
1485 path = snd_hda_add_new_path(codec, dac, pin,
1486 spec->mixer_nid);
1487 }
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001488 if (!path)
1489 return 0;
Takashi Iwai4e76a882014-02-25 12:21:03 +01001490 /* print_nid_path(codec, "output-aamix", path); */
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001491 path->active = false; /* unused as default */
1492 return snd_hda_get_path_idx(codec, path);
1493}
1494
Takashi Iwai55a63d42013-03-21 17:20:12 +01001495/* check whether the independent HP is available with the current config */
1496static bool indep_hp_possible(struct hda_codec *codec)
1497{
1498 struct hda_gen_spec *spec = codec->spec;
1499 struct auto_pin_cfg *cfg = &spec->autocfg;
1500 struct nid_path *path;
1501 int i, idx;
1502
1503 if (cfg->line_out_type == AUTO_PIN_HP_OUT)
1504 idx = spec->out_paths[0];
1505 else
1506 idx = spec->hp_paths[0];
1507 path = snd_hda_get_path_from_idx(codec, idx);
1508 if (!path)
1509 return false;
1510
1511 /* assume no path conflicts unless aamix is involved */
1512 if (!spec->mixer_nid || !is_nid_contained(path, spec->mixer_nid))
1513 return true;
1514
1515 /* check whether output paths contain aamix */
1516 for (i = 0; i < cfg->line_outs; i++) {
1517 if (spec->out_paths[i] == idx)
1518 break;
1519 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1520 if (path && is_nid_contained(path, spec->mixer_nid))
1521 return false;
1522 }
1523 for (i = 0; i < cfg->speaker_outs; i++) {
1524 path = snd_hda_get_path_from_idx(codec, spec->speaker_paths[i]);
1525 if (path && is_nid_contained(path, spec->mixer_nid))
1526 return false;
1527 }
1528
1529 return true;
1530}
1531
Takashi Iwaia07a9492013-01-07 16:44:06 +01001532/* fill the empty entries in the dac array for speaker/hp with the
1533 * shared dac pointed by the paths
1534 */
1535static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1536 hda_nid_t *dacs, int *path_idx)
1537{
1538 struct nid_path *path;
1539 int i;
1540
1541 for (i = 0; i < num_outs; i++) {
1542 if (dacs[i])
1543 continue;
1544 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1545 if (!path)
1546 continue;
1547 dacs[i] = path->path[0];
1548 }
1549}
1550
Takashi Iwai352f7f92012-12-19 12:52:06 +01001551/* fill in the dac_nids table from the parsed pin configuration */
1552static int fill_and_eval_dacs(struct hda_codec *codec,
1553 bool fill_hardwired,
1554 bool fill_mio_first)
1555{
1556 struct hda_gen_spec *spec = codec->spec;
1557 struct auto_pin_cfg *cfg = &spec->autocfg;
1558 int i, err, badness;
1559
1560 /* set num_dacs once to full for look_for_dac() */
1561 spec->multiout.num_dacs = cfg->line_outs;
1562 spec->multiout.dac_nids = spec->private_dac_nids;
1563 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1564 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1565 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1566 spec->multi_ios = 0;
1567 snd_array_free(&spec->paths);
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001568
1569 /* clear path indices */
1570 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1571 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1572 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1573 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1574 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
Takashi Iwaic697b712013-01-07 17:09:26 +01001575 memset(spec->input_paths, 0, sizeof(spec->input_paths));
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001576 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1577 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1578
Takashi Iwai352f7f92012-12-19 12:52:06 +01001579 badness = 0;
1580
1581 /* fill hard-wired DACs first */
1582 if (fill_hardwired) {
1583 bool mapped;
1584 do {
1585 mapped = map_singles(codec, cfg->line_outs,
1586 cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001587 spec->private_dac_nids,
1588 spec->out_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001589 mapped |= map_singles(codec, cfg->hp_outs,
1590 cfg->hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001591 spec->multiout.hp_out_nid,
1592 spec->hp_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001593 mapped |= map_singles(codec, cfg->speaker_outs,
1594 cfg->speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001595 spec->multiout.extra_out_nid,
1596 spec->speaker_paths);
Takashi Iwaida96fb52013-07-29 16:54:36 +02001597 if (!spec->no_multi_io &&
1598 fill_mio_first && cfg->line_outs == 1 &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01001599 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001600 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001601 if (!err)
1602 mapped = true;
1603 }
1604 } while (mapped);
1605 }
1606
1607 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001608 spec->private_dac_nids, spec->out_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001609 spec->main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001610
Takashi Iwaida96fb52013-07-29 16:54:36 +02001611 if (!spec->no_multi_io && fill_mio_first &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01001612 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1613 /* try to fill multi-io first */
Takashi Iwaie22aab72013-01-04 14:50:04 +01001614 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001615 if (err < 0)
1616 return err;
1617 /* we don't count badness at this stage yet */
1618 }
1619
1620 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1621 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1622 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001623 spec->hp_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001624 spec->extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001625 if (err < 0)
1626 return err;
1627 badness += err;
1628 }
1629 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1630 err = try_assign_dacs(codec, cfg->speaker_outs,
1631 cfg->speaker_pins,
1632 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001633 spec->speaker_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001634 spec->extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001635 if (err < 0)
1636 return err;
1637 badness += err;
1638 }
Takashi Iwaida96fb52013-07-29 16:54:36 +02001639 if (!spec->no_multi_io &&
1640 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001641 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001642 if (err < 0)
1643 return err;
1644 badness += err;
1645 }
Takashi Iwaie22aab72013-01-04 14:50:04 +01001646
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001647 if (spec->mixer_nid) {
1648 spec->aamix_out_paths[0] =
1649 check_aamix_out_path(codec, spec->out_paths[0]);
1650 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1651 spec->aamix_out_paths[1] =
1652 check_aamix_out_path(codec, spec->hp_paths[0]);
1653 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1654 spec->aamix_out_paths[2] =
1655 check_aamix_out_path(codec, spec->speaker_paths[0]);
1656 }
1657
Takashi Iwaida96fb52013-07-29 16:54:36 +02001658 if (!spec->no_multi_io &&
1659 cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
Takashi Iwaie22aab72013-01-04 14:50:04 +01001660 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1661 spec->multi_ios = 1; /* give badness */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001662
Takashi Iwaia07a9492013-01-07 16:44:06 +01001663 /* re-count num_dacs and squash invalid entries */
1664 spec->multiout.num_dacs = 0;
1665 for (i = 0; i < cfg->line_outs; i++) {
1666 if (spec->private_dac_nids[i])
1667 spec->multiout.num_dacs++;
1668 else {
1669 memmove(spec->private_dac_nids + i,
1670 spec->private_dac_nids + i + 1,
1671 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1672 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1673 }
1674 }
1675
1676 spec->ext_channel_count = spec->min_channel_count =
David Henningssonc0f3b212013-01-16 11:45:37 +01001677 spec->multiout.num_dacs * 2;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001678
Takashi Iwai352f7f92012-12-19 12:52:06 +01001679 if (spec->multi_ios == 2) {
1680 for (i = 0; i < 2; i++)
1681 spec->private_dac_nids[spec->multiout.num_dacs++] =
1682 spec->multi_io[i].dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001683 } else if (spec->multi_ios) {
1684 spec->multi_ios = 0;
1685 badness += BAD_MULTI_IO;
1686 }
1687
Takashi Iwai55a63d42013-03-21 17:20:12 +01001688 if (spec->indep_hp && !indep_hp_possible(codec))
1689 badness += BAD_NO_INDEP_HP;
1690
Takashi Iwaia07a9492013-01-07 16:44:06 +01001691 /* re-fill the shared DAC for speaker / headphone */
1692 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1693 refill_shared_dacs(codec, cfg->hp_outs,
1694 spec->multiout.hp_out_nid,
1695 spec->hp_paths);
1696 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1697 refill_shared_dacs(codec, cfg->speaker_outs,
1698 spec->multiout.extra_out_nid,
1699 spec->speaker_paths);
1700
Takashi Iwai352f7f92012-12-19 12:52:06 +01001701 return badness;
1702}
1703
1704#define DEBUG_BADNESS
1705
1706#ifdef DEBUG_BADNESS
Joe Perchesd82353e2014-07-05 13:02:02 -07001707#define debug_badness(fmt, ...) \
1708 codec_dbg(codec, fmt, ##__VA_ARGS__)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001709#else
Joe Perchesd82353e2014-07-05 13:02:02 -07001710#define debug_badness(fmt, ...) \
1711 do { if (0) codec_dbg(codec, fmt, ##__VA_ARGS__); } while (0)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001712#endif
1713
Takashi Iwaia7694092013-01-21 10:43:18 +01001714#ifdef DEBUG_BADNESS
1715static inline void print_nid_path_idx(struct hda_codec *codec,
1716 const char *pfx, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001717{
Takashi Iwaia7694092013-01-21 10:43:18 +01001718 struct nid_path *path;
1719
1720 path = snd_hda_get_path_from_idx(codec, idx);
1721 if (path)
Takashi Iwai4e76a882014-02-25 12:21:03 +01001722 print_nid_path(codec, pfx, path);
Takashi Iwaia7694092013-01-21 10:43:18 +01001723}
1724
1725static void debug_show_configs(struct hda_codec *codec,
1726 struct auto_pin_cfg *cfg)
1727{
1728 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia7694092013-01-21 10:43:18 +01001729 static const char * const lo_type[3] = { "LO", "SP", "HP" };
Takashi Iwaia7694092013-01-21 10:43:18 +01001730 int i;
1731
1732 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001733 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001734 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001735 spec->multiout.dac_nids[0],
1736 spec->multiout.dac_nids[1],
1737 spec->multiout.dac_nids[2],
Takashi Iwaia7694092013-01-21 10:43:18 +01001738 spec->multiout.dac_nids[3],
1739 lo_type[cfg->line_out_type]);
1740 for (i = 0; i < cfg->line_outs; i++)
1741 print_nid_path_idx(codec, " out", spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001742 if (spec->multi_ios > 0)
1743 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1744 spec->multi_ios,
1745 spec->multi_io[0].pin, spec->multi_io[1].pin,
1746 spec->multi_io[0].dac, spec->multi_io[1].dac);
Takashi Iwaia7694092013-01-21 10:43:18 +01001747 for (i = 0; i < spec->multi_ios; i++)
1748 print_nid_path_idx(codec, " mio",
1749 spec->out_paths[cfg->line_outs + i]);
1750 if (cfg->hp_outs)
1751 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001752 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001753 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001754 spec->multiout.hp_out_nid[0],
1755 spec->multiout.hp_out_nid[1],
1756 spec->multiout.hp_out_nid[2],
1757 spec->multiout.hp_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001758 for (i = 0; i < cfg->hp_outs; i++)
1759 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1760 if (cfg->speaker_outs)
1761 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001762 cfg->speaker_pins[0], cfg->speaker_pins[1],
1763 cfg->speaker_pins[2], cfg->speaker_pins[3],
1764 spec->multiout.extra_out_nid[0],
1765 spec->multiout.extra_out_nid[1],
1766 spec->multiout.extra_out_nid[2],
1767 spec->multiout.extra_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001768 for (i = 0; i < cfg->speaker_outs; i++)
1769 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1770 for (i = 0; i < 3; i++)
1771 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001772}
Takashi Iwaia7694092013-01-21 10:43:18 +01001773#else
1774#define debug_show_configs(codec, cfg) /* NOP */
1775#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01001776
1777/* find all available DACs of the codec */
1778static void fill_all_dac_nids(struct hda_codec *codec)
1779{
1780 struct hda_gen_spec *spec = codec->spec;
1781 int i;
1782 hda_nid_t nid = codec->start_nid;
1783
1784 spec->num_all_dacs = 0;
1785 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1786 for (i = 0; i < codec->num_nodes; i++, nid++) {
1787 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1788 continue;
1789 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001790 codec_err(codec, "Too many DACs!\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001791 break;
1792 }
1793 spec->all_dacs[spec->num_all_dacs++] = nid;
1794 }
1795}
1796
1797static int parse_output_paths(struct hda_codec *codec)
1798{
1799 struct hda_gen_spec *spec = codec->spec;
1800 struct auto_pin_cfg *cfg = &spec->autocfg;
1801 struct auto_pin_cfg *best_cfg;
Takashi Iwai9314a582013-01-21 10:49:05 +01001802 unsigned int val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001803 int best_badness = INT_MAX;
1804 int badness;
1805 bool fill_hardwired = true, fill_mio_first = true;
1806 bool best_wired = true, best_mio = true;
1807 bool hp_spk_swapped = false;
1808
Takashi Iwai352f7f92012-12-19 12:52:06 +01001809 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1810 if (!best_cfg)
1811 return -ENOMEM;
1812 *best_cfg = *cfg;
1813
1814 for (;;) {
1815 badness = fill_and_eval_dacs(codec, fill_hardwired,
1816 fill_mio_first);
1817 if (badness < 0) {
1818 kfree(best_cfg);
1819 return badness;
1820 }
1821 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1822 cfg->line_out_type, fill_hardwired, fill_mio_first,
1823 badness);
Takashi Iwaia7694092013-01-21 10:43:18 +01001824 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001825 if (badness < best_badness) {
1826 best_badness = badness;
1827 *best_cfg = *cfg;
1828 best_wired = fill_hardwired;
1829 best_mio = fill_mio_first;
1830 }
1831 if (!badness)
1832 break;
1833 fill_mio_first = !fill_mio_first;
1834 if (!fill_mio_first)
1835 continue;
1836 fill_hardwired = !fill_hardwired;
1837 if (!fill_hardwired)
1838 continue;
1839 if (hp_spk_swapped)
1840 break;
1841 hp_spk_swapped = true;
1842 if (cfg->speaker_outs > 0 &&
1843 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1844 cfg->hp_outs = cfg->line_outs;
1845 memcpy(cfg->hp_pins, cfg->line_out_pins,
1846 sizeof(cfg->hp_pins));
1847 cfg->line_outs = cfg->speaker_outs;
1848 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1849 sizeof(cfg->speaker_pins));
1850 cfg->speaker_outs = 0;
1851 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1852 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1853 fill_hardwired = true;
1854 continue;
1855 }
1856 if (cfg->hp_outs > 0 &&
1857 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1858 cfg->speaker_outs = cfg->line_outs;
1859 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1860 sizeof(cfg->speaker_pins));
1861 cfg->line_outs = cfg->hp_outs;
1862 memcpy(cfg->line_out_pins, cfg->hp_pins,
1863 sizeof(cfg->hp_pins));
1864 cfg->hp_outs = 0;
1865 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1866 cfg->line_out_type = AUTO_PIN_HP_OUT;
1867 fill_hardwired = true;
1868 continue;
1869 }
1870 break;
1871 }
1872
1873 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001874 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001875 *cfg = *best_cfg;
1876 fill_and_eval_dacs(codec, best_wired, best_mio);
1877 }
1878 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1879 cfg->line_out_type, best_wired, best_mio);
Takashi Iwaia7694092013-01-21 10:43:18 +01001880 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001881
1882 if (cfg->line_out_pins[0]) {
1883 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01001884 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001885 if (path)
1886 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
Takashi Iwaid89c6c02014-09-01 10:07:04 +02001887 if (spec->vmaster_nid) {
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01001888 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
1889 HDA_OUTPUT, spec->vmaster_tlv);
Takashi Iwaid89c6c02014-09-01 10:07:04 +02001890 if (spec->dac_min_mute)
1891 spec->vmaster_tlv[3] |= TLV_DB_SCALE_MUTE;
1892 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001893 }
1894
Takashi Iwai9314a582013-01-21 10:49:05 +01001895 /* set initial pinctl targets */
1896 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
1897 val = PIN_HP;
1898 else
1899 val = PIN_OUT;
1900 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
1901 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1902 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
1903 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1904 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
1905 set_pin_targets(codec, cfg->speaker_outs,
1906 cfg->speaker_pins, val);
1907 }
1908
Takashi Iwai55a63d42013-03-21 17:20:12 +01001909 /* clear indep_hp flag if not available */
1910 if (spec->indep_hp && !indep_hp_possible(codec))
1911 spec->indep_hp = 0;
1912
Takashi Iwai352f7f92012-12-19 12:52:06 +01001913 kfree(best_cfg);
1914 return 0;
1915}
1916
1917/* add playback controls from the parsed DAC table */
1918static int create_multi_out_ctls(struct hda_codec *codec,
1919 const struct auto_pin_cfg *cfg)
1920{
1921 struct hda_gen_spec *spec = codec->spec;
1922 int i, err, noutputs;
1923
1924 noutputs = cfg->line_outs;
1925 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1926 noutputs += spec->multi_ios;
1927
1928 for (i = 0; i < noutputs; i++) {
1929 const char *name;
1930 int index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001931 struct nid_path *path;
1932
Takashi Iwai196c17662013-01-04 15:01:40 +01001933 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001934 if (!path)
1935 continue;
Takashi Iwai247d85e2013-01-17 16:18:11 +01001936
1937 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001938 if (!name || !strcmp(name, "CLFE")) {
1939 /* Center/LFE */
1940 err = add_vol_ctl(codec, "Center", 0, 1, path);
1941 if (err < 0)
1942 return err;
1943 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1944 if (err < 0)
1945 return err;
Takashi Iwai247d85e2013-01-17 16:18:11 +01001946 } else {
1947 err = add_stereo_vol(codec, name, index, path);
1948 if (err < 0)
1949 return err;
1950 }
1951
1952 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
1953 if (!name || !strcmp(name, "CLFE")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001954 err = add_sw_ctl(codec, "Center", 0, 1, path);
1955 if (err < 0)
1956 return err;
1957 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1958 if (err < 0)
1959 return err;
1960 } else {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001961 err = add_stereo_sw(codec, name, index, path);
1962 if (err < 0)
1963 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 }
1965 }
1966 return 0;
1967}
1968
Takashi Iwaic2c80382013-01-07 10:33:57 +01001969static int create_extra_out(struct hda_codec *codec, int path_idx,
Takashi Iwai196c17662013-01-04 15:01:40 +01001970 const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001972 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 int err;
1974
Takashi Iwai196c17662013-01-04 15:01:40 +01001975 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001976 if (!path)
1977 return 0;
Takashi Iwaic2c80382013-01-07 10:33:57 +01001978 err = add_stereo_vol(codec, pfx, cidx, path);
1979 if (err < 0)
1980 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001981 err = add_stereo_sw(codec, pfx, cidx, path);
1982 if (err < 0)
1983 return err;
1984 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985}
1986
Takashi Iwai352f7f92012-12-19 12:52:06 +01001987/* add playback controls for speaker and HP outputs */
1988static int create_extra_outs(struct hda_codec *codec, int num_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001989 const int *paths, const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990{
Takashi Iwaic2c80382013-01-07 10:33:57 +01001991 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001992
1993 for (i = 0; i < num_pins; i++) {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001994 const char *name;
Takashi Iwai975cc022013-06-28 11:56:49 +02001995 char tmp[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwaic2c80382013-01-07 10:33:57 +01001996 int err, idx = 0;
1997
1998 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
1999 name = "Bass Speaker";
2000 else if (num_pins >= 3) {
2001 snprintf(tmp, sizeof(tmp), "%s %s",
Takashi Iwai352f7f92012-12-19 12:52:06 +01002002 pfx, channel_name[i]);
Takashi Iwaic2c80382013-01-07 10:33:57 +01002003 name = tmp;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002004 } else {
Takashi Iwaic2c80382013-01-07 10:33:57 +01002005 name = pfx;
2006 idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 }
Takashi Iwaic2c80382013-01-07 10:33:57 +01002008 err = create_extra_out(codec, paths[i], name, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002009 if (err < 0)
2010 return err;
2011 }
2012 return 0;
2013}
Takashi Iwai97ec5582006-03-21 11:29:07 +01002014
Takashi Iwai352f7f92012-12-19 12:52:06 +01002015static int create_hp_out_ctls(struct hda_codec *codec)
2016{
2017 struct hda_gen_spec *spec = codec->spec;
2018 return create_extra_outs(codec, spec->autocfg.hp_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01002019 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002020 "Headphone");
2021}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
Takashi Iwai352f7f92012-12-19 12:52:06 +01002023static int create_speaker_out_ctls(struct hda_codec *codec)
2024{
2025 struct hda_gen_spec *spec = codec->spec;
2026 return create_extra_outs(codec, spec->autocfg.speaker_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01002027 spec->speaker_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002028 "Speaker");
2029}
2030
2031/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002032 * independent HP controls
2033 */
2034
Takashi Iwai963afde2013-05-31 15:20:31 +02002035static void call_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002036static int indep_hp_info(struct snd_kcontrol *kcontrol,
2037 struct snd_ctl_elem_info *uinfo)
2038{
2039 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2040}
2041
2042static int indep_hp_get(struct snd_kcontrol *kcontrol,
2043 struct snd_ctl_elem_value *ucontrol)
2044{
2045 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2046 struct hda_gen_spec *spec = codec->spec;
2047 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
2048 return 0;
2049}
2050
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002051static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2052 int nomix_path_idx, int mix_path_idx,
2053 int out_type);
2054
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002055static int indep_hp_put(struct snd_kcontrol *kcontrol,
2056 struct snd_ctl_elem_value *ucontrol)
2057{
2058 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2059 struct hda_gen_spec *spec = codec->spec;
2060 unsigned int select = ucontrol->value.enumerated.item[0];
2061 int ret = 0;
2062
2063 mutex_lock(&spec->pcm_mutex);
2064 if (spec->active_streams) {
2065 ret = -EBUSY;
2066 goto unlock;
2067 }
2068
2069 if (spec->indep_hp_enabled != select) {
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002070 hda_nid_t *dacp;
2071 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2072 dacp = &spec->private_dac_nids[0];
2073 else
2074 dacp = &spec->multiout.hp_out_nid[0];
2075
2076 /* update HP aamix paths in case it conflicts with indep HP */
2077 if (spec->have_aamix_ctl) {
2078 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2079 update_aamix_paths(codec, spec->aamix_mode,
2080 spec->out_paths[0],
2081 spec->aamix_out_paths[0],
2082 spec->autocfg.line_out_type);
2083 else
2084 update_aamix_paths(codec, spec->aamix_mode,
2085 spec->hp_paths[0],
2086 spec->aamix_out_paths[1],
2087 AUTO_PIN_HP_OUT);
2088 }
2089
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002090 spec->indep_hp_enabled = select;
2091 if (spec->indep_hp_enabled)
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002092 *dacp = 0;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002093 else
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002094 *dacp = spec->alt_dac_nid;
Takashi Iwai92603c52013-01-22 07:46:31 +01002095
Takashi Iwai963afde2013-05-31 15:20:31 +02002096 call_hp_automute(codec, NULL);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002097 ret = 1;
2098 }
2099 unlock:
2100 mutex_unlock(&spec->pcm_mutex);
2101 return ret;
2102}
2103
2104static const struct snd_kcontrol_new indep_hp_ctl = {
2105 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2106 .name = "Independent HP",
2107 .info = indep_hp_info,
2108 .get = indep_hp_get,
2109 .put = indep_hp_put,
2110};
2111
2112
2113static int create_indep_hp_ctls(struct hda_codec *codec)
2114{
2115 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002116 hda_nid_t dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002117
2118 if (!spec->indep_hp)
2119 return 0;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002120 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2121 dac = spec->multiout.dac_nids[0];
2122 else
2123 dac = spec->multiout.hp_out_nid[0];
2124 if (!dac) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002125 spec->indep_hp = 0;
2126 return 0;
2127 }
2128
2129 spec->indep_hp_enabled = false;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002130 spec->alt_dac_nid = dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002131 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
2132 return -ENOMEM;
2133 return 0;
2134}
2135
2136/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002137 * channel mode enum control
2138 */
2139
2140static int ch_mode_info(struct snd_kcontrol *kcontrol,
2141 struct snd_ctl_elem_info *uinfo)
2142{
2143 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2144 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002145 int chs;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002146
2147 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2148 uinfo->count = 1;
2149 uinfo->value.enumerated.items = spec->multi_ios + 1;
2150 if (uinfo->value.enumerated.item > spec->multi_ios)
2151 uinfo->value.enumerated.item = spec->multi_ios;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002152 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2153 sprintf(uinfo->value.enumerated.name, "%dch", chs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002154 return 0;
2155}
2156
2157static int ch_mode_get(struct snd_kcontrol *kcontrol,
2158 struct snd_ctl_elem_value *ucontrol)
2159{
2160 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2161 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002162 ucontrol->value.enumerated.item[0] =
2163 (spec->ext_channel_count - spec->min_channel_count) / 2;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002164 return 0;
2165}
2166
Takashi Iwai196c17662013-01-04 15:01:40 +01002167static inline struct nid_path *
2168get_multiio_path(struct hda_codec *codec, int idx)
2169{
2170 struct hda_gen_spec *spec = codec->spec;
2171 return snd_hda_get_path_from_idx(codec,
2172 spec->out_paths[spec->autocfg.line_outs + idx]);
2173}
2174
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002175static void update_automute_all(struct hda_codec *codec);
2176
Takashi Iwai65033cc2013-04-16 12:31:05 +02002177/* Default value to be passed as aamix argument for snd_hda_activate_path();
2178 * used for output paths
2179 */
2180static bool aamix_default(struct hda_gen_spec *spec)
2181{
2182 return !spec->have_aamix_ctl || spec->aamix_mode;
2183}
2184
Takashi Iwai352f7f92012-12-19 12:52:06 +01002185static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2186{
2187 struct hda_gen_spec *spec = codec->spec;
2188 hda_nid_t nid = spec->multi_io[idx].pin;
2189 struct nid_path *path;
2190
Takashi Iwai196c17662013-01-04 15:01:40 +01002191 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002192 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002194
2195 if (path->active == output)
2196 return 0;
2197
2198 if (output) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01002199 set_pin_target(codec, nid, PIN_OUT, true);
Takashi Iwai65033cc2013-04-16 12:31:05 +02002200 snd_hda_activate_path(codec, path, true, aamix_default(spec));
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002201 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002202 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002203 set_pin_eapd(codec, nid, false);
Takashi Iwai65033cc2013-04-16 12:31:05 +02002204 snd_hda_activate_path(codec, path, false, aamix_default(spec));
Takashi Iwai2c12c302013-01-10 09:33:29 +01002205 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002206 path_power_down_sync(codec, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 }
Takashi Iwaia365fed2013-01-10 16:10:06 +01002208
2209 /* update jack retasking in case it modifies any of them */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002210 update_automute_all(codec);
Takashi Iwaia365fed2013-01-10 16:10:06 +01002211
Takashi Iwai352f7f92012-12-19 12:52:06 +01002212 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213}
2214
Takashi Iwai352f7f92012-12-19 12:52:06 +01002215static int ch_mode_put(struct snd_kcontrol *kcontrol,
2216 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002218 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2219 struct hda_gen_spec *spec = codec->spec;
2220 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221
Takashi Iwai352f7f92012-12-19 12:52:06 +01002222 ch = ucontrol->value.enumerated.item[0];
2223 if (ch < 0 || ch > spec->multi_ios)
2224 return -EINVAL;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002225 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002226 return 0;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002227 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002228 for (i = 0; i < spec->multi_ios; i++)
2229 set_multi_io(codec, i, i < ch);
2230 spec->multiout.max_channels = max(spec->ext_channel_count,
2231 spec->const_channel_count);
2232 if (spec->need_dac_fix)
2233 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 return 1;
2235}
2236
Takashi Iwai352f7f92012-12-19 12:52:06 +01002237static const struct snd_kcontrol_new channel_mode_enum = {
2238 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2239 .name = "Channel Mode",
2240 .info = ch_mode_info,
2241 .get = ch_mode_get,
2242 .put = ch_mode_put,
2243};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244
Takashi Iwai352f7f92012-12-19 12:52:06 +01002245static int create_multi_channel_mode(struct hda_codec *codec)
2246{
2247 struct hda_gen_spec *spec = codec->spec;
2248
2249 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002250 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002251 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 return 0;
2254}
2255
Takashi Iwai352f7f92012-12-19 12:52:06 +01002256/*
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002257 * aamix loopback enable/disable switch
2258 */
2259
2260#define loopback_mixing_info indep_hp_info
2261
2262static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2263 struct snd_ctl_elem_value *ucontrol)
2264{
2265 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2266 struct hda_gen_spec *spec = codec->spec;
2267 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2268 return 0;
2269}
2270
2271static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002272 int nomix_path_idx, int mix_path_idx,
2273 int out_type)
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002274{
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002275 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002276 struct nid_path *nomix_path, *mix_path;
2277
2278 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2279 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2280 if (!nomix_path || !mix_path)
2281 return;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002282
2283 /* if HP aamix path is driven from a different DAC and the
2284 * independent HP mode is ON, can't turn on aamix path
2285 */
2286 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2287 mix_path->path[0] != spec->alt_dac_nid)
2288 do_mix = false;
2289
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002290 if (do_mix) {
2291 snd_hda_activate_path(codec, nomix_path, false, true);
2292 snd_hda_activate_path(codec, mix_path, true, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002293 path_power_down_sync(codec, nomix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002294 } else {
Takashi Iwai65033cc2013-04-16 12:31:05 +02002295 snd_hda_activate_path(codec, mix_path, false, false);
2296 snd_hda_activate_path(codec, nomix_path, true, false);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002297 path_power_down_sync(codec, mix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002298 }
2299}
2300
2301static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2302 struct snd_ctl_elem_value *ucontrol)
2303{
2304 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2305 struct hda_gen_spec *spec = codec->spec;
2306 unsigned int val = ucontrol->value.enumerated.item[0];
2307
2308 if (val == spec->aamix_mode)
2309 return 0;
2310 spec->aamix_mode = val;
2311 update_aamix_paths(codec, val, spec->out_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002312 spec->aamix_out_paths[0],
2313 spec->autocfg.line_out_type);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002314 update_aamix_paths(codec, val, spec->hp_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002315 spec->aamix_out_paths[1],
2316 AUTO_PIN_HP_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002317 update_aamix_paths(codec, val, spec->speaker_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002318 spec->aamix_out_paths[2],
2319 AUTO_PIN_SPEAKER_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002320 return 1;
2321}
2322
2323static const struct snd_kcontrol_new loopback_mixing_enum = {
2324 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2325 .name = "Loopback Mixing",
2326 .info = loopback_mixing_info,
2327 .get = loopback_mixing_get,
2328 .put = loopback_mixing_put,
2329};
2330
2331static int create_loopback_mixing_ctl(struct hda_codec *codec)
2332{
2333 struct hda_gen_spec *spec = codec->spec;
2334
2335 if (!spec->mixer_nid)
2336 return 0;
2337 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
2338 spec->aamix_out_paths[2]))
2339 return 0;
2340 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2341 return -ENOMEM;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002342 spec->have_aamix_ctl = 1;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002343 return 0;
2344}
2345
2346/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002347 * shared headphone/mic handling
2348 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002349
Takashi Iwai352f7f92012-12-19 12:52:06 +01002350static void call_update_outputs(struct hda_codec *codec);
2351
2352/* for shared I/O, change the pin-control accordingly */
Takashi Iwai967303d2013-02-19 17:12:42 +01002353static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002354{
2355 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai967303d2013-02-19 17:12:42 +01002356 bool as_mic;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002357 unsigned int val;
Takashi Iwai967303d2013-02-19 17:12:42 +01002358 hda_nid_t pin;
2359
2360 pin = spec->hp_mic_pin;
2361 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
2362
2363 if (!force) {
2364 val = snd_hda_codec_get_pin_target(codec, pin);
2365 if (as_mic) {
2366 if (val & PIN_IN)
2367 return;
2368 } else {
2369 if (val & PIN_OUT)
2370 return;
2371 }
2372 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002373
2374 val = snd_hda_get_default_vref(codec, pin);
Takashi Iwai967303d2013-02-19 17:12:42 +01002375 /* if the HP pin doesn't support VREF and the codec driver gives an
2376 * alternative pin, set up the VREF on that pin instead
2377 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002378 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2379 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2380 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2381 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01002382 snd_hda_set_pin_ctl_cache(codec, vref_pin,
Takashi Iwai967303d2013-02-19 17:12:42 +01002383 PIN_IN | (as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02002384 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002385
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002386 if (!spec->hp_mic_jack_modes) {
2387 if (as_mic)
2388 val |= PIN_IN;
2389 else
2390 val = PIN_HP;
2391 set_pin_target(codec, pin, val, true);
Takashi Iwai963afde2013-05-31 15:20:31 +02002392 call_hp_automute(codec, NULL);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002393 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002394}
2395
2396/* create a shared input with the headphone out */
Takashi Iwai967303d2013-02-19 17:12:42 +01002397static int create_hp_mic(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002398{
2399 struct hda_gen_spec *spec = codec->spec;
2400 struct auto_pin_cfg *cfg = &spec->autocfg;
2401 unsigned int defcfg;
2402 hda_nid_t nid;
2403
Takashi Iwai967303d2013-02-19 17:12:42 +01002404 if (!spec->hp_mic) {
2405 if (spec->suppress_hp_mic_detect)
2406 return 0;
2407 /* automatic detection: only if no input or a single internal
2408 * input pin is found, try to detect the shared hp/mic
2409 */
2410 if (cfg->num_inputs > 1)
2411 return 0;
2412 else if (cfg->num_inputs == 1) {
2413 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2414 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2415 return 0;
2416 }
2417 }
2418
2419 spec->hp_mic = 0; /* clear once */
2420 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002421 return 0;
2422
Takashi Iwai967303d2013-02-19 17:12:42 +01002423 nid = 0;
2424 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2425 nid = cfg->line_out_pins[0];
2426 else if (cfg->hp_outs > 0)
2427 nid = cfg->hp_pins[0];
2428 if (!nid)
2429 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002430
2431 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2432 return 0; /* no input */
2433
Takashi Iwai967303d2013-02-19 17:12:42 +01002434 cfg->inputs[cfg->num_inputs].pin = nid;
2435 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
David Henningssoncb420b12013-04-11 11:30:28 +02002436 cfg->inputs[cfg->num_inputs].is_headphone_mic = 1;
Takashi Iwai967303d2013-02-19 17:12:42 +01002437 cfg->num_inputs++;
2438 spec->hp_mic = 1;
2439 spec->hp_mic_pin = nid;
2440 /* we can't handle auto-mic together with HP-mic */
2441 spec->suppress_auto_mic = 1;
Takashi Iwai4e76a882014-02-25 12:21:03 +01002442 codec_dbg(codec, "Enable shared I/O jack on NID 0x%x\n", nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002443 return 0;
2444}
2445
Takashi Iwai978e77e2013-01-10 16:57:58 +01002446/*
2447 * output jack mode
2448 */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002449
2450static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2451
2452static const char * const out_jack_texts[] = {
2453 "Line Out", "Headphone Out",
2454};
2455
Takashi Iwai978e77e2013-01-10 16:57:58 +01002456static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2457 struct snd_ctl_elem_info *uinfo)
2458{
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002459 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
Takashi Iwai978e77e2013-01-10 16:57:58 +01002460}
2461
2462static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2463 struct snd_ctl_elem_value *ucontrol)
2464{
2465 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2466 hda_nid_t nid = kcontrol->private_value;
2467 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2468 ucontrol->value.enumerated.item[0] = 1;
2469 else
2470 ucontrol->value.enumerated.item[0] = 0;
2471 return 0;
2472}
2473
2474static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2475 struct snd_ctl_elem_value *ucontrol)
2476{
2477 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2478 hda_nid_t nid = kcontrol->private_value;
2479 unsigned int val;
2480
2481 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2482 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2483 return 0;
2484 snd_hda_set_pin_ctl_cache(codec, nid, val);
2485 return 1;
2486}
2487
2488static const struct snd_kcontrol_new out_jack_mode_enum = {
2489 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2490 .info = out_jack_mode_info,
2491 .get = out_jack_mode_get,
2492 .put = out_jack_mode_put,
2493};
2494
2495static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2496{
2497 struct hda_gen_spec *spec = codec->spec;
2498 int i;
2499
2500 for (i = 0; i < spec->kctls.used; i++) {
2501 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2502 if (!strcmp(kctl->name, name) && kctl->index == idx)
2503 return true;
2504 }
2505 return false;
2506}
2507
2508static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2509 char *name, size_t name_len)
2510{
2511 struct hda_gen_spec *spec = codec->spec;
2512 int idx = 0;
2513
2514 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2515 strlcat(name, " Jack Mode", name_len);
2516
2517 for (; find_kctl_name(codec, name, idx); idx++)
2518 ;
2519}
2520
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002521static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2522{
2523 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002524 if (spec->add_jack_modes) {
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002525 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2526 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2527 return 2;
2528 }
2529 return 1;
2530}
2531
Takashi Iwai978e77e2013-01-10 16:57:58 +01002532static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2533 hda_nid_t *pins)
2534{
2535 struct hda_gen_spec *spec = codec->spec;
2536 int i;
2537
2538 for (i = 0; i < num_pins; i++) {
2539 hda_nid_t pin = pins[i];
Takashi Iwaiced4cef2013-11-26 08:33:45 +01002540 if (pin == spec->hp_mic_pin)
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002541 continue;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002542 if (get_out_jack_num_items(codec, pin) > 1) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01002543 struct snd_kcontrol_new *knew;
Takashi Iwai975cc022013-06-28 11:56:49 +02002544 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai978e77e2013-01-10 16:57:58 +01002545 get_jack_mode_name(codec, pin, name, sizeof(name));
2546 knew = snd_hda_gen_add_kctl(spec, name,
2547 &out_jack_mode_enum);
2548 if (!knew)
2549 return -ENOMEM;
2550 knew->private_value = pin;
2551 }
2552 }
2553
2554 return 0;
2555}
2556
Takashi Iwai294765582013-01-17 09:52:11 +01002557/*
2558 * input jack mode
2559 */
2560
2561/* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2562#define NUM_VREFS 6
2563
2564static const char * const vref_texts[NUM_VREFS] = {
2565 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2566 "", "Mic 80pc Bias", "Mic 100pc Bias"
2567};
2568
2569static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2570{
2571 unsigned int pincap;
2572
2573 pincap = snd_hda_query_pin_caps(codec, pin);
2574 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2575 /* filter out unusual vrefs */
2576 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2577 return pincap;
2578}
2579
2580/* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2581static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2582{
2583 unsigned int i, n = 0;
2584
2585 for (i = 0; i < NUM_VREFS; i++) {
2586 if (vref_caps & (1 << i)) {
2587 if (n == item_idx)
2588 return i;
2589 n++;
2590 }
2591 }
2592 return 0;
2593}
2594
2595/* convert back from the vref ctl index to the enum item index */
2596static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2597{
2598 unsigned int i, n = 0;
2599
2600 for (i = 0; i < NUM_VREFS; i++) {
2601 if (i == idx)
2602 return n;
2603 if (vref_caps & (1 << i))
2604 n++;
2605 }
2606 return 0;
2607}
2608
2609static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2610 struct snd_ctl_elem_info *uinfo)
2611{
2612 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2613 hda_nid_t nid = kcontrol->private_value;
2614 unsigned int vref_caps = get_vref_caps(codec, nid);
2615
2616 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2617 vref_texts);
2618 /* set the right text */
2619 strcpy(uinfo->value.enumerated.name,
2620 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2621 return 0;
2622}
2623
2624static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2625 struct snd_ctl_elem_value *ucontrol)
2626{
2627 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2628 hda_nid_t nid = kcontrol->private_value;
2629 unsigned int vref_caps = get_vref_caps(codec, nid);
2630 unsigned int idx;
2631
2632 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2633 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2634 return 0;
2635}
2636
2637static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2638 struct snd_ctl_elem_value *ucontrol)
2639{
2640 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2641 hda_nid_t nid = kcontrol->private_value;
2642 unsigned int vref_caps = get_vref_caps(codec, nid);
2643 unsigned int val, idx;
2644
2645 val = snd_hda_codec_get_pin_target(codec, nid);
2646 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2647 if (idx == ucontrol->value.enumerated.item[0])
2648 return 0;
2649
2650 val &= ~AC_PINCTL_VREFEN;
2651 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2652 snd_hda_set_pin_ctl_cache(codec, nid, val);
2653 return 1;
2654}
2655
2656static const struct snd_kcontrol_new in_jack_mode_enum = {
2657 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2658 .info = in_jack_mode_info,
2659 .get = in_jack_mode_get,
2660 .put = in_jack_mode_put,
2661};
2662
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002663static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2664{
2665 struct hda_gen_spec *spec = codec->spec;
2666 int nitems = 0;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002667 if (spec->add_jack_modes)
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002668 nitems = hweight32(get_vref_caps(codec, pin));
2669 return nitems ? nitems : 1;
2670}
2671
Takashi Iwai294765582013-01-17 09:52:11 +01002672static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2673{
2674 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai294765582013-01-17 09:52:11 +01002675 struct snd_kcontrol_new *knew;
Takashi Iwai975cc022013-06-28 11:56:49 +02002676 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002677 unsigned int defcfg;
2678
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002679 if (pin == spec->hp_mic_pin)
2680 return 0; /* already done in create_out_jack_mode() */
Takashi Iwai294765582013-01-17 09:52:11 +01002681
2682 /* no jack mode for fixed pins */
2683 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2684 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2685 return 0;
2686
2687 /* no multiple vref caps? */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002688 if (get_in_jack_num_items(codec, pin) <= 1)
Takashi Iwai294765582013-01-17 09:52:11 +01002689 return 0;
2690
2691 get_jack_mode_name(codec, pin, name, sizeof(name));
2692 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2693 if (!knew)
2694 return -ENOMEM;
2695 knew->private_value = pin;
2696 return 0;
2697}
2698
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002699/*
2700 * HP/mic shared jack mode
2701 */
2702static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2703 struct snd_ctl_elem_info *uinfo)
2704{
2705 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2706 hda_nid_t nid = kcontrol->private_value;
2707 int out_jacks = get_out_jack_num_items(codec, nid);
2708 int in_jacks = get_in_jack_num_items(codec, nid);
2709 const char *text = NULL;
2710 int idx;
2711
2712 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2713 uinfo->count = 1;
2714 uinfo->value.enumerated.items = out_jacks + in_jacks;
2715 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2716 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2717 idx = uinfo->value.enumerated.item;
2718 if (idx < out_jacks) {
2719 if (out_jacks > 1)
2720 text = out_jack_texts[idx];
2721 else
2722 text = "Headphone Out";
2723 } else {
2724 idx -= out_jacks;
2725 if (in_jacks > 1) {
2726 unsigned int vref_caps = get_vref_caps(codec, nid);
2727 text = vref_texts[get_vref_idx(vref_caps, idx)];
2728 } else
2729 text = "Mic In";
2730 }
2731
2732 strcpy(uinfo->value.enumerated.name, text);
2733 return 0;
2734}
2735
2736static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2737{
2738 int out_jacks = get_out_jack_num_items(codec, nid);
2739 int in_jacks = get_in_jack_num_items(codec, nid);
2740 unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2741 int idx = 0;
2742
2743 if (val & PIN_OUT) {
2744 if (out_jacks > 1 && val == PIN_HP)
2745 idx = 1;
2746 } else if (val & PIN_IN) {
2747 idx = out_jacks;
2748 if (in_jacks > 1) {
2749 unsigned int vref_caps = get_vref_caps(codec, nid);
2750 val &= AC_PINCTL_VREFEN;
2751 idx += cvt_from_vref_idx(vref_caps, val);
2752 }
2753 }
2754 return idx;
2755}
2756
2757static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2758 struct snd_ctl_elem_value *ucontrol)
2759{
2760 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2761 hda_nid_t nid = kcontrol->private_value;
2762 ucontrol->value.enumerated.item[0] =
2763 get_cur_hp_mic_jack_mode(codec, nid);
2764 return 0;
2765}
2766
2767static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2768 struct snd_ctl_elem_value *ucontrol)
2769{
2770 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2771 hda_nid_t nid = kcontrol->private_value;
2772 int out_jacks = get_out_jack_num_items(codec, nid);
2773 int in_jacks = get_in_jack_num_items(codec, nid);
2774 unsigned int val, oldval, idx;
2775
2776 oldval = get_cur_hp_mic_jack_mode(codec, nid);
2777 idx = ucontrol->value.enumerated.item[0];
2778 if (oldval == idx)
2779 return 0;
2780
2781 if (idx < out_jacks) {
2782 if (out_jacks > 1)
2783 val = idx ? PIN_HP : PIN_OUT;
2784 else
2785 val = PIN_HP;
2786 } else {
2787 idx -= out_jacks;
2788 if (in_jacks > 1) {
2789 unsigned int vref_caps = get_vref_caps(codec, nid);
2790 val = snd_hda_codec_get_pin_target(codec, nid);
Takashi Iwai3f550e32013-03-07 18:30:27 +01002791 val &= ~(AC_PINCTL_VREFEN | PIN_HP);
2792 val |= get_vref_idx(vref_caps, idx) | PIN_IN;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002793 } else
Takashi Iwai16c0cefe2013-11-26 08:44:26 +01002794 val = snd_hda_get_default_vref(codec, nid) | PIN_IN;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002795 }
2796 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwai963afde2013-05-31 15:20:31 +02002797 call_hp_automute(codec, NULL);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002798
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002799 return 1;
2800}
2801
2802static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
2803 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2804 .info = hp_mic_jack_mode_info,
2805 .get = hp_mic_jack_mode_get,
2806 .put = hp_mic_jack_mode_put,
2807};
2808
2809static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2810{
2811 struct hda_gen_spec *spec = codec->spec;
2812 struct snd_kcontrol_new *knew;
2813
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002814 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
2815 &hp_mic_jack_mode_enum);
2816 if (!knew)
2817 return -ENOMEM;
2818 knew->private_value = pin;
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002819 spec->hp_mic_jack_modes = 1;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002820 return 0;
2821}
Takashi Iwai352f7f92012-12-19 12:52:06 +01002822
2823/*
2824 * Parse input paths
2825 */
2826
Takashi Iwai352f7f92012-12-19 12:52:06 +01002827/* add the powersave loopback-list entry */
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002828static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002829{
2830 struct hda_amp_list *list;
2831
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002832 list = snd_array_new(&spec->loopback_list);
2833 if (!list)
2834 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002835 list->nid = mix;
2836 list->dir = HDA_INPUT;
2837 list->idx = idx;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002838 spec->loopback.amplist = spec->loopback_list.list;
2839 return 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002840}
Takashi Iwaicb53c622007-08-10 17:21:45 +02002841
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002842/* return true if either a volume or a mute amp is found for the given
2843 * aamix path; the amp has to be either in the mixer node or its direct leaf
2844 */
2845static bool look_for_mix_leaf_ctls(struct hda_codec *codec, hda_nid_t mix_nid,
2846 hda_nid_t pin, unsigned int *mix_val,
2847 unsigned int *mute_val)
2848{
2849 int idx, num_conns;
2850 const hda_nid_t *list;
2851 hda_nid_t nid;
2852
2853 idx = snd_hda_get_conn_index(codec, mix_nid, pin, true);
2854 if (idx < 0)
2855 return false;
2856
2857 *mix_val = *mute_val = 0;
2858 if (nid_has_volume(codec, mix_nid, HDA_INPUT))
2859 *mix_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2860 if (nid_has_mute(codec, mix_nid, HDA_INPUT))
2861 *mute_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2862 if (*mix_val && *mute_val)
2863 return true;
2864
2865 /* check leaf node */
2866 num_conns = snd_hda_get_conn_list(codec, mix_nid, &list);
2867 if (num_conns < idx)
2868 return false;
2869 nid = list[idx];
Takashi Iwai43a8e502014-01-07 18:11:44 +01002870 if (!*mix_val && nid_has_volume(codec, nid, HDA_OUTPUT) &&
2871 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_VOL_CTL))
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002872 *mix_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
Takashi Iwai43a8e502014-01-07 18:11:44 +01002873 if (!*mute_val && nid_has_mute(codec, nid, HDA_OUTPUT) &&
2874 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_MUTE_CTL))
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002875 *mute_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2876
2877 return *mix_val || *mute_val;
2878}
2879
Takashi Iwai352f7f92012-12-19 12:52:06 +01002880/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01002881static int new_analog_input(struct hda_codec *codec, int input_idx,
2882 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002883 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002885 struct hda_gen_spec *spec = codec->spec;
2886 struct nid_path *path;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002887 unsigned int mix_val, mute_val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002888 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002890 if (!look_for_mix_leaf_ctls(codec, mix_nid, pin, &mix_val, &mute_val))
2891 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002892
Takashi Iwai3ca529d2013-01-07 17:25:08 +01002893 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002894 if (!path)
2895 return -EINVAL;
Takashi Iwai4e76a882014-02-25 12:21:03 +01002896 print_nid_path(codec, "loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01002897 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002898
2899 idx = path->idx[path->depth - 1];
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002900 if (mix_val) {
2901 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, mix_val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002902 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 return err;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002904 path->ctls[NID_PATH_VOL_CTL] = mix_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 }
2906
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002907 if (mute_val) {
2908 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, mute_val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002909 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 return err;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002911 path->ctls[NID_PATH_MUTE_CTL] = mute_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 }
2913
Takashi Iwai352f7f92012-12-19 12:52:06 +01002914 path->active = true;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002915 err = add_loopback_list(spec, mix_nid, idx);
2916 if (err < 0)
2917 return err;
Takashi Iwaie4a395e2013-01-23 17:00:31 +01002918
2919 if (spec->mixer_nid != spec->mixer_merge_nid &&
2920 !spec->loopback_merge_path) {
2921 path = snd_hda_add_new_path(codec, spec->mixer_nid,
2922 spec->mixer_merge_nid, 0);
2923 if (path) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002924 print_nid_path(codec, "loopback-merge", path);
Takashi Iwaie4a395e2013-01-23 17:00:31 +01002925 path->active = true;
2926 spec->loopback_merge_path =
2927 snd_hda_get_path_idx(codec, path);
2928 }
2929 }
2930
Takashi Iwai352f7f92012-12-19 12:52:06 +01002931 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932}
2933
Takashi Iwai352f7f92012-12-19 12:52:06 +01002934static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002936 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
2937 return (pincap & AC_PINCAP_IN) != 0;
2938}
2939
2940/* Parse the codec tree and retrieve ADCs */
2941static int fill_adc_nids(struct hda_codec *codec)
2942{
2943 struct hda_gen_spec *spec = codec->spec;
2944 hda_nid_t nid;
2945 hda_nid_t *adc_nids = spec->adc_nids;
2946 int max_nums = ARRAY_SIZE(spec->adc_nids);
2947 int i, nums = 0;
2948
2949 nid = codec->start_nid;
2950 for (i = 0; i < codec->num_nodes; i++, nid++) {
2951 unsigned int caps = get_wcaps(codec, nid);
2952 int type = get_wcaps_type(caps);
2953
2954 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2955 continue;
2956 adc_nids[nums] = nid;
2957 if (++nums >= max_nums)
2958 break;
2959 }
2960 spec->num_adc_nids = nums;
Takashi Iwai0ffd5342013-01-17 15:53:29 +01002961
2962 /* copy the detected ADCs to all_adcs[] */
2963 spec->num_all_adcs = nums;
2964 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
2965
Takashi Iwai352f7f92012-12-19 12:52:06 +01002966 return nums;
2967}
2968
2969/* filter out invalid adc_nids that don't give all active input pins;
2970 * if needed, check whether dynamic ADC-switching is available
2971 */
2972static int check_dyn_adc_switch(struct hda_codec *codec)
2973{
2974 struct hda_gen_spec *spec = codec->spec;
2975 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002976 unsigned int ok_bits;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002977 int i, n, nums;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002978
Takashi Iwai352f7f92012-12-19 12:52:06 +01002979 nums = 0;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002980 ok_bits = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002981 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002982 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002983 if (!spec->input_paths[i][n])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002984 break;
2985 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002986 if (i >= imux->num_items) {
2987 ok_bits |= (1 << n);
2988 nums++;
2989 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002990 }
2991
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002992 if (!ok_bits) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002993 /* check whether ADC-switch is possible */
2994 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002995 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002996 if (spec->input_paths[i][n]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002997 spec->dyn_adc_idx[i] = n;
2998 break;
2999 }
3000 }
3001 }
3002
Takashi Iwai4e76a882014-02-25 12:21:03 +01003003 codec_dbg(codec, "enabling ADC switching\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01003004 spec->dyn_adc_switch = 1;
3005 } else if (nums != spec->num_adc_nids) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003006 /* shrink the invalid adcs and input paths */
3007 nums = 0;
3008 for (n = 0; n < spec->num_adc_nids; n++) {
3009 if (!(ok_bits & (1 << n)))
3010 continue;
3011 if (n != nums) {
3012 spec->adc_nids[nums] = spec->adc_nids[n];
Takashi Iwai980428c2013-01-09 09:28:20 +01003013 for (i = 0; i < imux->num_items; i++) {
3014 invalidate_nid_path(codec,
3015 spec->input_paths[i][nums]);
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003016 spec->input_paths[i][nums] =
3017 spec->input_paths[i][n];
Takashi Iwai980428c2013-01-09 09:28:20 +01003018 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003019 }
3020 nums++;
3021 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003022 spec->num_adc_nids = nums;
3023 }
3024
Takashi Iwai967303d2013-02-19 17:12:42 +01003025 if (imux->num_items == 1 ||
3026 (imux->num_items == 2 && spec->hp_mic)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003027 codec_dbg(codec, "reducing to a single ADC\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01003028 spec->num_adc_nids = 1; /* reduce to a single ADC */
3029 }
3030
3031 /* single index for individual volumes ctls */
3032 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
3033 spec->num_adc_nids = 1;
3034
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 return 0;
3036}
3037
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003038/* parse capture source paths from the given pin and create imux items */
3039static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
Takashi Iwai9dba2052013-01-18 10:01:15 +01003040 int cfg_idx, int num_adcs,
3041 const char *label, int anchor)
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003042{
3043 struct hda_gen_spec *spec = codec->spec;
3044 struct hda_input_mux *imux = &spec->input_mux;
3045 int imux_idx = imux->num_items;
3046 bool imux_added = false;
3047 int c;
3048
3049 for (c = 0; c < num_adcs; c++) {
3050 struct nid_path *path;
3051 hda_nid_t adc = spec->adc_nids[c];
3052
3053 if (!is_reachable_path(codec, pin, adc))
3054 continue;
3055 path = snd_hda_add_new_path(codec, pin, adc, anchor);
3056 if (!path)
3057 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01003058 print_nid_path(codec, "input", path);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003059 spec->input_paths[imux_idx][c] =
3060 snd_hda_get_path_idx(codec, path);
3061
3062 if (!imux_added) {
Takashi Iwai967303d2013-02-19 17:12:42 +01003063 if (spec->hp_mic_pin == pin)
3064 spec->hp_mic_mux_idx = imux->num_items;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003065 spec->imux_pins[imux->num_items] = pin;
Takashi Iwai6194b992014-06-06 18:12:16 +02003066 snd_hda_add_imux_item(codec, imux, label, cfg_idx, NULL);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003067 imux_added = true;
Takashi Iwaif1e762d2013-12-09 16:02:24 +01003068 if (spec->dyn_adc_switch)
3069 spec->dyn_adc_idx[imux_idx] = c;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003070 }
3071 }
3072
3073 return 0;
3074}
3075
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003077 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 */
Takashi Iwai9dba2052013-01-18 10:01:15 +01003079
Takashi Iwaic9700422013-01-18 10:17:30 +01003080/* fill the label for each input at first */
3081static int fill_input_pin_labels(struct hda_codec *codec)
3082{
3083 struct hda_gen_spec *spec = codec->spec;
3084 const struct auto_pin_cfg *cfg = &spec->autocfg;
3085 int i;
3086
3087 for (i = 0; i < cfg->num_inputs; i++) {
3088 hda_nid_t pin = cfg->inputs[i].pin;
3089 const char *label;
3090 int j, idx;
3091
3092 if (!is_input_pin(codec, pin))
3093 continue;
3094
3095 label = hda_get_autocfg_input_label(codec, cfg, i);
3096 idx = 0;
David Henningsson8e8db7f2013-01-18 15:43:02 +01003097 for (j = i - 1; j >= 0; j--) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003098 if (spec->input_labels[j] &&
3099 !strcmp(spec->input_labels[j], label)) {
3100 idx = spec->input_label_idxs[j] + 1;
3101 break;
3102 }
3103 }
3104
3105 spec->input_labels[i] = label;
3106 spec->input_label_idxs[i] = idx;
3107 }
3108
3109 return 0;
3110}
3111
Takashi Iwai9dba2052013-01-18 10:01:15 +01003112#define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
3113
Takashi Iwai352f7f92012-12-19 12:52:06 +01003114static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003115{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003116 struct hda_gen_spec *spec = codec->spec;
3117 const struct auto_pin_cfg *cfg = &spec->autocfg;
3118 hda_nid_t mixer = spec->mixer_nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003119 int num_adcs;
Takashi Iwaic9700422013-01-18 10:17:30 +01003120 int i, err;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003121 unsigned int val;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003122
Takashi Iwai352f7f92012-12-19 12:52:06 +01003123 num_adcs = fill_adc_nids(codec);
3124 if (num_adcs < 0)
3125 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003126
Takashi Iwaic9700422013-01-18 10:17:30 +01003127 err = fill_input_pin_labels(codec);
3128 if (err < 0)
3129 return err;
3130
Takashi Iwai352f7f92012-12-19 12:52:06 +01003131 for (i = 0; i < cfg->num_inputs; i++) {
3132 hda_nid_t pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133
Takashi Iwai352f7f92012-12-19 12:52:06 +01003134 pin = cfg->inputs[i].pin;
3135 if (!is_input_pin(codec, pin))
3136 continue;
3137
Takashi Iwai2c12c302013-01-10 09:33:29 +01003138 val = PIN_IN;
3139 if (cfg->inputs[i].type == AUTO_PIN_MIC)
3140 val |= snd_hda_get_default_vref(codec, pin);
Takashi Iwai93c9d8a2013-03-11 09:48:43 +01003141 if (pin != spec->hp_mic_pin)
3142 set_pin_target(codec, pin, val, false);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003143
Takashi Iwai352f7f92012-12-19 12:52:06 +01003144 if (mixer) {
3145 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01003146 err = new_analog_input(codec, i, pin,
Takashi Iwaic9700422013-01-18 10:17:30 +01003147 spec->input_labels[i],
3148 spec->input_label_idxs[i],
3149 mixer);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003150 if (err < 0)
3151 return err;
3152 }
3153 }
3154
Takashi Iwaic9700422013-01-18 10:17:30 +01003155 err = parse_capture_source(codec, pin, i, num_adcs,
3156 spec->input_labels[i], -mixer);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003157 if (err < 0)
3158 return err;
Takashi Iwai294765582013-01-17 09:52:11 +01003159
Takashi Iwaif811c3c2013-03-07 18:32:59 +01003160 if (spec->add_jack_modes) {
Takashi Iwai294765582013-01-17 09:52:11 +01003161 err = create_in_jack_mode(codec, pin);
3162 if (err < 0)
3163 return err;
3164 }
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003165 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003166
Takashi Iwaif1e762d2013-12-09 16:02:24 +01003167 /* add stereo mix when explicitly enabled via hint */
3168 if (mixer && spec->add_stereo_mix_input &&
3169 snd_hda_get_bool_hint(codec, "add_stereo_mix_input") > 0) {
Takashi Iwai9dba2052013-01-18 10:01:15 +01003170 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003171 "Stereo Mix", 0);
3172 if (err < 0)
3173 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003174 }
3175
3176 return 0;
3177}
3178
3179
3180/*
3181 * input source mux
3182 */
3183
Takashi Iwaic697b712013-01-07 17:09:26 +01003184/* get the input path specified by the given adc and imux indices */
3185static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003186{
3187 struct hda_gen_spec *spec = codec->spec;
David Henningssonb56fa1e2013-01-16 11:45:35 +01003188 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3189 snd_BUG();
3190 return NULL;
3191 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003192 if (spec->dyn_adc_switch)
3193 adc_idx = spec->dyn_adc_idx[imux_idx];
David Henningssond3d982f2013-01-18 15:43:01 +01003194 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
David Henningssonb56fa1e2013-01-16 11:45:35 +01003195 snd_BUG();
3196 return NULL;
3197 }
Takashi Iwaic697b712013-01-07 17:09:26 +01003198 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003199}
3200
3201static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3202 unsigned int idx);
3203
3204static int mux_enum_info(struct snd_kcontrol *kcontrol,
3205 struct snd_ctl_elem_info *uinfo)
3206{
3207 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3208 struct hda_gen_spec *spec = codec->spec;
3209 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3210}
3211
3212static int mux_enum_get(struct snd_kcontrol *kcontrol,
3213 struct snd_ctl_elem_value *ucontrol)
3214{
3215 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3216 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003217 /* the ctls are created at once with multiple counts */
3218 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003219
3220 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3221 return 0;
3222}
3223
3224static int mux_enum_put(struct snd_kcontrol *kcontrol,
3225 struct snd_ctl_elem_value *ucontrol)
3226{
3227 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003228 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003229 return mux_select(codec, adc_idx,
3230 ucontrol->value.enumerated.item[0]);
3231}
3232
Takashi Iwai352f7f92012-12-19 12:52:06 +01003233static const struct snd_kcontrol_new cap_src_temp = {
3234 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3235 .name = "Input Source",
3236 .info = mux_enum_info,
3237 .get = mux_enum_get,
3238 .put = mux_enum_put,
3239};
3240
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003241/*
3242 * capture volume and capture switch ctls
3243 */
3244
Takashi Iwai352f7f92012-12-19 12:52:06 +01003245typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3246 struct snd_ctl_elem_value *ucontrol);
3247
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003248/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003249static int cap_put_caller(struct snd_kcontrol *kcontrol,
3250 struct snd_ctl_elem_value *ucontrol,
3251 put_call_t func, int type)
3252{
3253 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3254 struct hda_gen_spec *spec = codec->spec;
3255 const struct hda_input_mux *imux;
3256 struct nid_path *path;
3257 int i, adc_idx, err = 0;
3258
3259 imux = &spec->input_mux;
David Henningssona053d1e2013-01-16 11:45:36 +01003260 adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003261 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003262 /* we use the cache-only update at first since multiple input paths
3263 * may shared the same amp; by updating only caches, the redundant
3264 * writes to hardware can be reduced.
3265 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003266 codec->cached_write = 1;
3267 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003268 path = get_input_path(codec, adc_idx, i);
3269 if (!path || !path->ctls[type])
Takashi Iwai352f7f92012-12-19 12:52:06 +01003270 continue;
3271 kcontrol->private_value = path->ctls[type];
3272 err = func(kcontrol, ucontrol);
3273 if (err < 0)
3274 goto error;
3275 }
3276 error:
3277 codec->cached_write = 0;
3278 mutex_unlock(&codec->control_mutex);
Takashi Iwaidc870f32013-01-22 15:24:30 +01003279 snd_hda_codec_flush_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003280 if (err >= 0 && spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01003281 spec->cap_sync_hook(codec, kcontrol, ucontrol);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003282 return err;
3283}
3284
3285/* capture volume ctl callbacks */
3286#define cap_vol_info snd_hda_mixer_amp_volume_info
3287#define cap_vol_get snd_hda_mixer_amp_volume_get
3288#define cap_vol_tlv snd_hda_mixer_amp_tlv
3289
3290static int cap_vol_put(struct snd_kcontrol *kcontrol,
3291 struct snd_ctl_elem_value *ucontrol)
3292{
3293 return cap_put_caller(kcontrol, ucontrol,
3294 snd_hda_mixer_amp_volume_put,
3295 NID_PATH_VOL_CTL);
3296}
3297
3298static const struct snd_kcontrol_new cap_vol_temp = {
3299 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3300 .name = "Capture Volume",
3301 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3302 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3303 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3304 .info = cap_vol_info,
3305 .get = cap_vol_get,
3306 .put = cap_vol_put,
3307 .tlv = { .c = cap_vol_tlv },
3308};
3309
3310/* capture switch ctl callbacks */
3311#define cap_sw_info snd_ctl_boolean_stereo_info
3312#define cap_sw_get snd_hda_mixer_amp_switch_get
3313
3314static int cap_sw_put(struct snd_kcontrol *kcontrol,
3315 struct snd_ctl_elem_value *ucontrol)
3316{
Takashi Iwaia90229e2013-01-18 14:10:00 +01003317 return cap_put_caller(kcontrol, ucontrol,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003318 snd_hda_mixer_amp_switch_put,
3319 NID_PATH_MUTE_CTL);
3320}
3321
3322static const struct snd_kcontrol_new cap_sw_temp = {
3323 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3324 .name = "Capture Switch",
3325 .info = cap_sw_info,
3326 .get = cap_sw_get,
3327 .put = cap_sw_put,
3328};
3329
3330static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3331{
3332 hda_nid_t nid;
3333 int i, depth;
3334
3335 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3336 for (depth = 0; depth < 3; depth++) {
3337 if (depth >= path->depth)
3338 return -EINVAL;
3339 i = path->depth - depth - 1;
3340 nid = path->path[i];
3341 if (!path->ctls[NID_PATH_VOL_CTL]) {
3342 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3343 path->ctls[NID_PATH_VOL_CTL] =
3344 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3345 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3346 int idx = path->idx[i];
3347 if (!depth && codec->single_adc_amp)
3348 idx = 0;
3349 path->ctls[NID_PATH_VOL_CTL] =
3350 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3351 }
3352 }
3353 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3354 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3355 path->ctls[NID_PATH_MUTE_CTL] =
3356 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3357 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3358 int idx = path->idx[i];
3359 if (!depth && codec->single_adc_amp)
3360 idx = 0;
3361 path->ctls[NID_PATH_MUTE_CTL] =
3362 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3363 }
3364 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01003365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 return 0;
3367}
3368
Takashi Iwai352f7f92012-12-19 12:52:06 +01003369static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003371 struct hda_gen_spec *spec = codec->spec;
3372 struct auto_pin_cfg *cfg = &spec->autocfg;
3373 unsigned int val;
3374 int i;
3375
3376 if (!spec->inv_dmic_split)
3377 return false;
3378 for (i = 0; i < cfg->num_inputs; i++) {
3379 if (cfg->inputs[i].pin != nid)
3380 continue;
3381 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3382 return false;
3383 val = snd_hda_codec_get_pincfg(codec, nid);
3384 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3385 }
3386 return false;
3387}
3388
Takashi Iwaia90229e2013-01-18 14:10:00 +01003389/* capture switch put callback for a single control with hook call */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003390static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3391 struct snd_ctl_elem_value *ucontrol)
3392{
3393 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3394 struct hda_gen_spec *spec = codec->spec;
3395 int ret;
3396
3397 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3398 if (ret < 0)
3399 return ret;
3400
Takashi Iwaia90229e2013-01-18 14:10:00 +01003401 if (spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01003402 spec->cap_sync_hook(codec, kcontrol, ucontrol);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003403
3404 return ret;
3405}
3406
Takashi Iwai352f7f92012-12-19 12:52:06 +01003407static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3408 int idx, bool is_switch, unsigned int ctl,
3409 bool inv_dmic)
3410{
3411 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai975cc022013-06-28 11:56:49 +02003412 char tmpname[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003413 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3414 const char *sfx = is_switch ? "Switch" : "Volume";
3415 unsigned int chs = inv_dmic ? 1 : 3;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003416 struct snd_kcontrol_new *knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003417
3418 if (!ctl)
3419 return 0;
3420
3421 if (label)
3422 snprintf(tmpname, sizeof(tmpname),
3423 "%s Capture %s", label, sfx);
3424 else
3425 snprintf(tmpname, sizeof(tmpname),
3426 "Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003427 knew = add_control(spec, type, tmpname, idx,
3428 amp_val_replace_channels(ctl, chs));
3429 if (!knew)
3430 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003431 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003432 knew->put = cap_single_sw_put;
3433 if (!inv_dmic)
3434 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003435
3436 /* Make independent right kcontrol */
3437 if (label)
3438 snprintf(tmpname, sizeof(tmpname),
3439 "Inverted %s Capture %s", label, sfx);
3440 else
3441 snprintf(tmpname, sizeof(tmpname),
3442 "Inverted Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003443 knew = add_control(spec, type, tmpname, idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003444 amp_val_replace_channels(ctl, 2));
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003445 if (!knew)
3446 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003447 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003448 knew->put = cap_single_sw_put;
3449 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003450}
3451
3452/* create single (and simple) capture volume and switch controls */
3453static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3454 unsigned int vol_ctl, unsigned int sw_ctl,
3455 bool inv_dmic)
3456{
3457 int err;
3458 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3459 if (err < 0)
3460 return err;
3461 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3462 if (err < 0)
3463 return err;
3464 return 0;
3465}
3466
3467/* create bound capture volume and switch controls */
3468static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3469 unsigned int vol_ctl, unsigned int sw_ctl)
3470{
3471 struct hda_gen_spec *spec = codec->spec;
3472 struct snd_kcontrol_new *knew;
3473
3474 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003475 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003476 if (!knew)
3477 return -ENOMEM;
3478 knew->index = idx;
3479 knew->private_value = vol_ctl;
3480 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3481 }
3482 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003483 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003484 if (!knew)
3485 return -ENOMEM;
3486 knew->index = idx;
3487 knew->private_value = sw_ctl;
3488 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3489 }
3490 return 0;
3491}
3492
3493/* return the vol ctl when used first in the imux list */
3494static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3495{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003496 struct nid_path *path;
3497 unsigned int ctl;
3498 int i;
3499
Takashi Iwaic697b712013-01-07 17:09:26 +01003500 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003501 if (!path)
3502 return 0;
3503 ctl = path->ctls[type];
3504 if (!ctl)
3505 return 0;
3506 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003507 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003508 if (path && path->ctls[type] == ctl)
3509 return 0;
3510 }
3511 return ctl;
3512}
3513
3514/* create individual capture volume and switch controls per input */
3515static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3516{
3517 struct hda_gen_spec *spec = codec->spec;
3518 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaic9700422013-01-18 10:17:30 +01003519 int i, err, type;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003520
3521 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003522 bool inv_dmic;
Takashi Iwaic9700422013-01-18 10:17:30 +01003523 int idx;
Takashi Iwai9dba2052013-01-18 10:01:15 +01003524
Takashi Iwaic9700422013-01-18 10:17:30 +01003525 idx = imux->items[i].index;
3526 if (idx >= spec->autocfg.num_inputs)
Takashi Iwai9dba2052013-01-18 10:01:15 +01003527 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003528 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3529
3530 for (type = 0; type < 2; type++) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003531 err = add_single_cap_ctl(codec,
3532 spec->input_labels[idx],
3533 spec->input_label_idxs[idx],
3534 type,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003535 get_first_cap_ctl(codec, i, type),
3536 inv_dmic);
3537 if (err < 0)
3538 return err;
3539 }
3540 }
3541 return 0;
3542}
3543
3544static int create_capture_mixers(struct hda_codec *codec)
3545{
3546 struct hda_gen_spec *spec = codec->spec;
3547 struct hda_input_mux *imux = &spec->input_mux;
3548 int i, n, nums, err;
3549
3550 if (spec->dyn_adc_switch)
3551 nums = 1;
3552 else
3553 nums = spec->num_adc_nids;
3554
3555 if (!spec->auto_mic && imux->num_items > 1) {
3556 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01003557 const char *name;
3558 name = nums > 1 ? "Input Source" : "Capture Source";
3559 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003560 if (!knew)
3561 return -ENOMEM;
3562 knew->count = nums;
3563 }
3564
3565 for (n = 0; n < nums; n++) {
3566 bool multi = false;
David Henningsson99a55922013-01-16 15:58:44 +01003567 bool multi_cap_vol = spec->multi_cap_vol;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003568 bool inv_dmic = false;
3569 int vol, sw;
3570
3571 vol = sw = 0;
3572 for (i = 0; i < imux->num_items; i++) {
3573 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01003574 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003575 if (!path)
3576 continue;
3577 parse_capvol_in_path(codec, path);
3578 if (!vol)
3579 vol = path->ctls[NID_PATH_VOL_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003580 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003581 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003582 if (!same_amp_caps(codec, vol,
3583 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3584 multi_cap_vol = true;
3585 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003586 if (!sw)
3587 sw = path->ctls[NID_PATH_MUTE_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003588 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003589 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003590 if (!same_amp_caps(codec, sw,
3591 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3592 multi_cap_vol = true;
3593 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003594 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3595 inv_dmic = true;
3596 }
3597
3598 if (!multi)
3599 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3600 inv_dmic);
David Henningssonccb04152013-10-14 10:16:22 +02003601 else if (!multi_cap_vol && !inv_dmic)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003602 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3603 else
3604 err = create_multi_cap_vol_ctl(codec);
3605 if (err < 0)
3606 return err;
3607 }
3608
3609 return 0;
3610}
3611
3612/*
3613 * add mic boosts if needed
3614 */
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003615
3616/* check whether the given amp is feasible as a boost volume */
3617static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3618 int dir, int idx)
3619{
3620 unsigned int step;
3621
3622 if (!nid_has_volume(codec, nid, dir) ||
3623 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3624 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3625 return false;
3626
3627 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3628 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3629 if (step < 0x20)
3630 return false;
3631 return true;
3632}
3633
3634/* look for a boost amp in a widget close to the pin */
3635static unsigned int look_for_boost_amp(struct hda_codec *codec,
3636 struct nid_path *path)
3637{
3638 unsigned int val = 0;
3639 hda_nid_t nid;
3640 int depth;
3641
3642 for (depth = 0; depth < 3; depth++) {
3643 if (depth >= path->depth - 1)
3644 break;
3645 nid = path->path[depth];
3646 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3647 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3648 break;
3649 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3650 path->idx[depth])) {
3651 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3652 HDA_INPUT);
3653 break;
3654 }
3655 }
3656
3657 return val;
3658}
3659
Takashi Iwai352f7f92012-12-19 12:52:06 +01003660static int parse_mic_boost(struct hda_codec *codec)
3661{
3662 struct hda_gen_spec *spec = codec->spec;
3663 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003664 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003665 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003666
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003667 if (!spec->num_adc_nids)
3668 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003669
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003670 for (i = 0; i < imux->num_items; i++) {
3671 struct nid_path *path;
3672 unsigned int val;
3673 int idx;
Takashi Iwai975cc022013-06-28 11:56:49 +02003674 char boost_label[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
David Henningsson02aba552013-01-16 15:58:43 +01003675
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003676 idx = imux->items[i].index;
3677 if (idx >= imux->num_items)
3678 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003679
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003680 /* check only line-in and mic pins */
Takashi Iwai1799cdd52013-01-18 14:37:16 +01003681 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003682 continue;
3683
3684 path = get_input_path(codec, 0, i);
3685 if (!path)
3686 continue;
3687
3688 val = look_for_boost_amp(codec, path);
3689 if (!val)
3690 continue;
3691
3692 /* create a boost control */
3693 snprintf(boost_label, sizeof(boost_label),
3694 "%s Boost Volume", spec->input_labels[idx]);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003695 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3696 spec->input_label_idxs[idx], val))
3697 return -ENOMEM;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003698
3699 path->ctls[NID_PATH_BOOST_CTL] = val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003700 }
3701 return 0;
3702}
3703
3704/*
3705 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3706 */
3707static void parse_digital(struct hda_codec *codec)
3708{
3709 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003710 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003711 int i, nums;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003712 hda_nid_t dig_nid, pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003713
3714 /* support multiple SPDIFs; the secondary is set up as a slave */
3715 nums = 0;
3716 for (i = 0; i < spec->autocfg.dig_outs; i++) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003717 pin = spec->autocfg.dig_out_pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003718 dig_nid = look_for_dac(codec, pin, true);
3719 if (!dig_nid)
3720 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003721 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003722 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003723 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01003724 print_nid_path(codec, "digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01003725 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01003726 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003727 set_pin_target(codec, pin, PIN_OUT, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003728 if (!nums) {
3729 spec->multiout.dig_out_nid = dig_nid;
3730 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3731 } else {
3732 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3733 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
Dan Carpenterd576422e2014-05-14 17:18:31 +03003734 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003735 spec->slave_dig_outs[nums - 1] = dig_nid;
3736 }
3737 nums++;
3738 }
3739
3740 if (spec->autocfg.dig_in_pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003741 pin = spec->autocfg.dig_in_pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003742 dig_nid = codec->start_nid;
3743 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003744 unsigned int wcaps = get_wcaps(codec, dig_nid);
3745 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3746 continue;
3747 if (!(wcaps & AC_WCAP_DIGITAL))
3748 continue;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003749 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003750 if (path) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003751 print_nid_path(codec, "digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003752 path->active = true;
3753 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003754 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003755 set_pin_target(codec, pin, PIN_IN, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003756 break;
3757 }
3758 }
3759 }
3760}
3761
3762
3763/*
3764 * input MUX handling
3765 */
3766
3767static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3768
3769/* select the given imux item; either unmute exclusively or select the route */
3770static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3771 unsigned int idx)
3772{
3773 struct hda_gen_spec *spec = codec->spec;
3774 const struct hda_input_mux *imux;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003775 struct nid_path *old_path, *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003776
3777 imux = &spec->input_mux;
3778 if (!imux->num_items)
3779 return 0;
3780
3781 if (idx >= imux->num_items)
3782 idx = imux->num_items - 1;
3783 if (spec->cur_mux[adc_idx] == idx)
3784 return 0;
3785
Takashi Iwai55196ff2013-01-24 17:32:56 +01003786 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3787 if (!old_path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003788 return 0;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003789 if (old_path->active)
3790 snd_hda_activate_path(codec, old_path, false, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003791
3792 spec->cur_mux[adc_idx] = idx;
3793
Takashi Iwai967303d2013-02-19 17:12:42 +01003794 if (spec->hp_mic)
3795 update_hp_mic(codec, adc_idx, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003796
3797 if (spec->dyn_adc_switch)
3798 dyn_adc_pcm_resetup(codec, idx);
3799
Takashi Iwaic697b712013-01-07 17:09:26 +01003800 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003801 if (!path)
3802 return 0;
3803 if (path->active)
3804 return 0;
3805 snd_hda_activate_path(codec, path, true, false);
3806 if (spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01003807 spec->cap_sync_hook(codec, NULL, NULL);
Takashi Iwai55196ff2013-01-24 17:32:56 +01003808 path_power_down_sync(codec, old_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003809 return 1;
3810}
3811
3812
3813/*
3814 * Jack detections for HP auto-mute and mic-switch
3815 */
3816
3817/* check each pin in the given array; returns true if any of them is plugged */
3818static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
3819{
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02003820 int i;
3821 bool present = false;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003822
3823 for (i = 0; i < num_pins; i++) {
3824 hda_nid_t nid = pins[i];
3825 if (!nid)
3826 break;
Takashi Iwai0b4df932013-01-10 09:45:13 +01003827 /* don't detect pins retasked as inputs */
3828 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
3829 continue;
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02003830 if (snd_hda_jack_detect_state(codec, nid) == HDA_JACK_PRESENT)
3831 present = true;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003832 }
3833 return present;
3834}
3835
3836/* standard HP/line-out auto-mute helper */
3837static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003838 int *paths, bool mute)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003839{
3840 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003841 int i;
3842
3843 for (i = 0; i < num_pins; i++) {
3844 hda_nid_t nid = pins[i];
Takashi Iwai967303d2013-02-19 17:12:42 +01003845 unsigned int val, oldval;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003846 if (!nid)
3847 break;
Takashi Iwai7eebffd2013-06-24 16:00:21 +02003848
3849 if (spec->auto_mute_via_amp) {
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003850 struct nid_path *path;
3851 hda_nid_t mute_nid;
3852
3853 path = snd_hda_get_path_from_idx(codec, paths[i]);
3854 if (!path)
3855 continue;
3856 mute_nid = get_amp_nid_(path->ctls[NID_PATH_MUTE_CTL]);
3857 if (!mute_nid)
3858 continue;
Takashi Iwai7eebffd2013-06-24 16:00:21 +02003859 if (mute)
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003860 spec->mute_bits |= (1ULL << mute_nid);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02003861 else
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003862 spec->mute_bits &= ~(1ULL << mute_nid);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02003863 set_pin_eapd(codec, nid, !mute);
3864 continue;
3865 }
3866
Takashi Iwai967303d2013-02-19 17:12:42 +01003867 oldval = snd_hda_codec_get_pin_target(codec, nid);
3868 if (oldval & PIN_IN)
3869 continue; /* no mute for inputs */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003870 /* don't reset VREF value in case it's controlling
3871 * the amp (see alc861_fixup_asus_amp_vref_0f())
3872 */
Takashi Iwai2c12c302013-01-10 09:33:29 +01003873 if (spec->keep_vref_in_automute)
Takashi Iwai967303d2013-02-19 17:12:42 +01003874 val = oldval & ~PIN_HP;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003875 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01003876 val = 0;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003877 if (!mute)
Takashi Iwai967303d2013-02-19 17:12:42 +01003878 val |= oldval;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003879 /* here we call update_pin_ctl() so that the pinctl is changed
3880 * without changing the pinctl target value;
3881 * the original target value will be still referred at the
3882 * init / resume again
3883 */
3884 update_pin_ctl(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01003885 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003886 }
3887}
3888
3889/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003890void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003891{
3892 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003893 int *paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003894 int on;
3895
3896 /* Control HP pins/amps depending on master_mute state;
3897 * in general, HP pins/amps control should be enabled in all cases,
3898 * but currently set only for master_mute, just to be safe
3899 */
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003900 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
3901 paths = spec->out_paths;
3902 else
3903 paths = spec->hp_paths;
Takashi Iwai967303d2013-02-19 17:12:42 +01003904 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003905 spec->autocfg.hp_pins, paths, spec->master_mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003906
3907 if (!spec->automute_speaker)
3908 on = 0;
3909 else
3910 on = spec->hp_jack_present | spec->line_jack_present;
3911 on |= spec->master_mute;
Takashi Iwai47b9ddb82013-01-16 18:18:00 +01003912 spec->speaker_muted = on;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003913 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3914 paths = spec->out_paths;
3915 else
3916 paths = spec->speaker_paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003917 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003918 spec->autocfg.speaker_pins, paths, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003919
3920 /* toggle line-out mutes if needed, too */
3921 /* if LO is a copy of either HP or Speaker, don't need to handle it */
3922 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
3923 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
3924 return;
3925 if (!spec->automute_lo)
3926 on = 0;
3927 else
3928 on = spec->hp_jack_present;
3929 on |= spec->master_mute;
Takashi Iwai47b9ddb82013-01-16 18:18:00 +01003930 spec->line_out_muted = on;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003931 paths = spec->out_paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003932 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003933 spec->autocfg.line_out_pins, paths, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003934}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003935EXPORT_SYMBOL_GPL(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003936
3937static void call_update_outputs(struct hda_codec *codec)
3938{
3939 struct hda_gen_spec *spec = codec->spec;
3940 if (spec->automute_hook)
3941 spec->automute_hook(codec);
3942 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01003943 snd_hda_gen_update_outputs(codec);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02003944
3945 /* sync the whole vmaster slaves to reflect the new auto-mute status */
3946 if (spec->auto_mute_via_amp && !codec->bus->shutdown)
3947 snd_ctl_sync_vmaster(spec->vmaster_mute.sw_kctl, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003948}
3949
3950/* standard HP-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003951void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003952{
3953 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai92603c52013-01-22 07:46:31 +01003954 hda_nid_t *pins = spec->autocfg.hp_pins;
3955 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003956
Takashi Iwai92603c52013-01-22 07:46:31 +01003957 /* No detection for the first HP jack during indep-HP mode */
3958 if (spec->indep_hp_enabled) {
3959 pins++;
3960 num_pins--;
3961 }
3962
3963 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003964 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
3965 return;
3966 call_update_outputs(codec);
3967}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003968EXPORT_SYMBOL_GPL(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003969
3970/* standard line-out-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003971void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003972{
3973 struct hda_gen_spec *spec = codec->spec;
3974
3975 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3976 return;
3977 /* check LO jack only when it's different from HP */
3978 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
3979 return;
3980
3981 spec->line_jack_present =
3982 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3983 spec->autocfg.line_out_pins);
3984 if (!spec->automute_speaker || !spec->detect_lo)
3985 return;
3986 call_update_outputs(codec);
3987}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003988EXPORT_SYMBOL_GPL(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003989
3990/* standard mic auto-switch helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003991void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *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 */
4014static void call_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
4015{
4016 struct hda_gen_spec *spec = codec->spec;
4017 if (spec->hp_automute_hook)
4018 spec->hp_automute_hook(codec, jack);
4019 else
4020 snd_hda_gen_hp_automute(codec, jack);
4021}
4022
4023static void call_line_automute(struct hda_codec *codec,
4024 struct hda_jack_tbl *jack)
4025{
4026 struct hda_gen_spec *spec = codec->spec;
4027 if (spec->line_automute_hook)
4028 spec->line_automute_hook(codec, jack);
4029 else
4030 snd_hda_gen_line_automute(codec, jack);
4031}
4032
4033static void call_mic_autoswitch(struct hda_codec *codec,
4034 struct hda_jack_tbl *jack)
4035{
4036 struct hda_gen_spec *spec = codec->spec;
4037 if (spec->mic_autoswitch_hook)
4038 spec->mic_autoswitch_hook(codec, jack);
4039 else
4040 snd_hda_gen_mic_autoswitch(codec, jack);
4041}
4042
Takashi Iwai963afde2013-05-31 15:20:31 +02004043/* update jack retasking */
4044static void update_automute_all(struct hda_codec *codec)
4045{
4046 call_hp_automute(codec, NULL);
4047 call_line_automute(codec, NULL);
4048 call_mic_autoswitch(codec, NULL);
4049}
4050
Takashi Iwai352f7f92012-12-19 12:52:06 +01004051/*
4052 * Auto-Mute mode mixer enum support
4053 */
4054static int automute_mode_info(struct snd_kcontrol *kcontrol,
4055 struct snd_ctl_elem_info *uinfo)
4056{
4057 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4058 struct hda_gen_spec *spec = codec->spec;
4059 static const char * const texts3[] = {
4060 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02004061 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062
Takashi Iwai352f7f92012-12-19 12:52:06 +01004063 if (spec->automute_speaker_possible && spec->automute_lo_possible)
4064 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
4065 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
4066}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067
Takashi Iwai352f7f92012-12-19 12:52:06 +01004068static int automute_mode_get(struct snd_kcontrol *kcontrol,
4069 struct snd_ctl_elem_value *ucontrol)
4070{
4071 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4072 struct hda_gen_spec *spec = codec->spec;
4073 unsigned int val = 0;
4074 if (spec->automute_speaker)
4075 val++;
4076 if (spec->automute_lo)
4077 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004078
Takashi Iwai352f7f92012-12-19 12:52:06 +01004079 ucontrol->value.enumerated.item[0] = val;
4080 return 0;
4081}
4082
4083static int automute_mode_put(struct snd_kcontrol *kcontrol,
4084 struct snd_ctl_elem_value *ucontrol)
4085{
4086 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4087 struct hda_gen_spec *spec = codec->spec;
4088
4089 switch (ucontrol->value.enumerated.item[0]) {
4090 case 0:
4091 if (!spec->automute_speaker && !spec->automute_lo)
4092 return 0;
4093 spec->automute_speaker = 0;
4094 spec->automute_lo = 0;
4095 break;
4096 case 1:
4097 if (spec->automute_speaker_possible) {
4098 if (!spec->automute_lo && spec->automute_speaker)
4099 return 0;
4100 spec->automute_speaker = 1;
4101 spec->automute_lo = 0;
4102 } else if (spec->automute_lo_possible) {
4103 if (spec->automute_lo)
4104 return 0;
4105 spec->automute_lo = 1;
4106 } else
4107 return -EINVAL;
4108 break;
4109 case 2:
4110 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
4111 return -EINVAL;
4112 if (spec->automute_speaker && spec->automute_lo)
4113 return 0;
4114 spec->automute_speaker = 1;
4115 spec->automute_lo = 1;
4116 break;
4117 default:
4118 return -EINVAL;
4119 }
4120 call_update_outputs(codec);
4121 return 1;
4122}
4123
4124static const struct snd_kcontrol_new automute_mode_enum = {
4125 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4126 .name = "Auto-Mute Mode",
4127 .info = automute_mode_info,
4128 .get = automute_mode_get,
4129 .put = automute_mode_put,
4130};
4131
4132static int add_automute_mode_enum(struct hda_codec *codec)
4133{
4134 struct hda_gen_spec *spec = codec->spec;
4135
Takashi Iwai12c93df2012-12-19 14:38:33 +01004136 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01004137 return -ENOMEM;
4138 return 0;
4139}
4140
4141/*
4142 * Check the availability of HP/line-out auto-mute;
4143 * Set up appropriately if really supported
4144 */
4145static int check_auto_mute_availability(struct hda_codec *codec)
4146{
4147 struct hda_gen_spec *spec = codec->spec;
4148 struct auto_pin_cfg *cfg = &spec->autocfg;
4149 int present = 0;
4150 int i, err;
4151
Takashi Iwaif72706b2013-01-16 18:20:07 +01004152 if (spec->suppress_auto_mute)
4153 return 0;
4154
Takashi Iwai352f7f92012-12-19 12:52:06 +01004155 if (cfg->hp_pins[0])
4156 present++;
4157 if (cfg->line_out_pins[0])
4158 present++;
4159 if (cfg->speaker_pins[0])
4160 present++;
4161 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02004162 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004163
4164 if (!cfg->speaker_pins[0] &&
4165 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
4166 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4167 sizeof(cfg->speaker_pins));
4168 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170
Takashi Iwai352f7f92012-12-19 12:52:06 +01004171 if (!cfg->hp_pins[0] &&
4172 cfg->line_out_type == AUTO_PIN_HP_OUT) {
4173 memcpy(cfg->hp_pins, cfg->line_out_pins,
4174 sizeof(cfg->hp_pins));
4175 cfg->hp_outs = cfg->line_outs;
4176 }
4177
4178 for (i = 0; i < cfg->hp_outs; i++) {
4179 hda_nid_t nid = cfg->hp_pins[i];
4180 if (!is_jack_detectable(codec, nid))
4181 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004182 codec_dbg(codec, "Enable HP auto-muting on NID 0x%x\n", nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004183 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004184 call_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004185 spec->detect_hp = 1;
4186 }
4187
4188 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
4189 if (cfg->speaker_outs)
4190 for (i = 0; i < cfg->line_outs; i++) {
4191 hda_nid_t nid = cfg->line_out_pins[i];
4192 if (!is_jack_detectable(codec, nid))
4193 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004194 codec_dbg(codec, "Enable Line-Out auto-muting on NID 0x%x\n", nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004195 snd_hda_jack_detect_enable_callback(codec, nid,
4196 HDA_GEN_FRONT_EVENT,
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,
4238 HDA_GEN_MIC_EVENT,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004239 call_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004240 return true;
4241}
4242
4243static int compare_attr(const void *ap, const void *bp)
4244{
4245 const struct automic_entry *a = ap;
4246 const struct automic_entry *b = bp;
4247 return (int)(a->attr - b->attr);
4248}
4249
4250/*
4251 * Check the availability of auto-mic switch;
4252 * Set up if really supported
4253 */
4254static int check_auto_mic_availability(struct hda_codec *codec)
4255{
4256 struct hda_gen_spec *spec = codec->spec;
4257 struct auto_pin_cfg *cfg = &spec->autocfg;
4258 unsigned int types;
4259 int i, num_pins;
4260
Takashi Iwaid12daf62013-01-07 16:32:11 +01004261 if (spec->suppress_auto_mic)
4262 return 0;
4263
Takashi Iwai352f7f92012-12-19 12:52:06 +01004264 types = 0;
4265 num_pins = 0;
4266 for (i = 0; i < cfg->num_inputs; i++) {
4267 hda_nid_t nid = cfg->inputs[i].pin;
4268 unsigned int attr;
4269 attr = snd_hda_codec_get_pincfg(codec, nid);
4270 attr = snd_hda_get_input_pin_attr(attr);
4271 if (types & (1 << attr))
4272 return 0; /* already occupied */
4273 switch (attr) {
4274 case INPUT_PIN_ATTR_INT:
4275 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4276 return 0; /* invalid type */
4277 break;
4278 case INPUT_PIN_ATTR_UNUSED:
4279 return 0; /* invalid entry */
4280 default:
4281 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4282 return 0; /* invalid type */
4283 if (!spec->line_in_auto_switch &&
4284 cfg->inputs[i].type != AUTO_PIN_MIC)
4285 return 0; /* only mic is allowed */
4286 if (!is_jack_detectable(codec, nid))
4287 return 0; /* no unsol support */
4288 break;
4289 }
4290 if (num_pins >= MAX_AUTO_MIC_PINS)
4291 return 0;
4292 types |= (1 << attr);
4293 spec->am_entry[num_pins].pin = nid;
4294 spec->am_entry[num_pins].attr = attr;
4295 num_pins++;
4296 }
4297
4298 if (num_pins < 2)
4299 return 0;
4300
4301 spec->am_num_entries = num_pins;
4302 /* sort the am_entry in the order of attr so that the pin with a
4303 * higher attr will be selected when the jack is plugged.
4304 */
4305 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4306 compare_attr, NULL);
4307
4308 if (!auto_mic_check_imux(codec))
4309 return 0;
4310
4311 spec->auto_mic = 1;
4312 spec->num_adc_nids = 1;
4313 spec->cur_mux[0] = spec->am_entry[0].idx;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004314 codec_dbg(codec, "Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01004315 spec->am_entry[0].pin,
4316 spec->am_entry[1].pin,
4317 spec->am_entry[2].pin);
4318
4319 return 0;
4320}
4321
Takashi Iwai55196ff2013-01-24 17:32:56 +01004322/* power_filter hook; make inactive widgets into power down */
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004323unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
Takashi Iwai55196ff2013-01-24 17:32:56 +01004324 hda_nid_t nid,
4325 unsigned int power_state)
4326{
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004327 if (power_state != AC_PWRST_D0 || nid == codec->afg)
Takashi Iwai55196ff2013-01-24 17:32:56 +01004328 return power_state;
4329 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4330 return power_state;
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +02004331 if (is_active_nid_for_any(codec, nid))
Takashi Iwai55196ff2013-01-24 17:32:56 +01004332 return power_state;
4333 return AC_PWRST_D3;
4334}
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004335EXPORT_SYMBOL_GPL(snd_hda_gen_path_power_filter);
Takashi Iwai55196ff2013-01-24 17:32:56 +01004336
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004337/* mute all aamix inputs initially; parse up to the first leaves */
4338static void mute_all_mixer_nid(struct hda_codec *codec, hda_nid_t mix)
4339{
4340 int i, nums;
4341 const hda_nid_t *conn;
4342 bool has_amp;
4343
4344 nums = snd_hda_get_conn_list(codec, mix, &conn);
4345 has_amp = nid_has_mute(codec, mix, HDA_INPUT);
4346 for (i = 0; i < nums; i++) {
4347 if (has_amp)
4348 snd_hda_codec_amp_stereo(codec, mix,
4349 HDA_INPUT, i,
4350 0xff, HDA_AMP_MUTE);
4351 else if (nid_has_volume(codec, conn[i], HDA_OUTPUT))
4352 snd_hda_codec_amp_stereo(codec, conn[i],
4353 HDA_OUTPUT, 0,
4354 0xff, HDA_AMP_MUTE);
4355 }
4356}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004357
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004358/*
4359 * Parse the given BIOS configuration and set up the hda_gen_spec
4360 *
4361 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004362 * or a negative error code
4363 */
4364int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004365 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004366{
4367 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004368 int err;
4369
Takashi Iwai1c70a582013-01-11 17:48:22 +01004370 parse_user_hints(codec);
4371
Takashi Iwaie4a395e2013-01-23 17:00:31 +01004372 if (spec->mixer_nid && !spec->mixer_merge_nid)
4373 spec->mixer_merge_nid = spec->mixer_nid;
4374
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004375 if (cfg != &spec->autocfg) {
4376 spec->autocfg = *cfg;
4377 cfg = &spec->autocfg;
4378 }
4379
Takashi Iwai98bd1112013-03-22 14:53:50 +01004380 if (!spec->main_out_badness)
4381 spec->main_out_badness = &hda_main_out_badness;
4382 if (!spec->extra_out_badness)
4383 spec->extra_out_badness = &hda_extra_out_badness;
4384
David Henningsson6fc4cb92013-01-16 15:58:45 +01004385 fill_all_dac_nids(codec);
4386
Takashi Iwai352f7f92012-12-19 12:52:06 +01004387 if (!cfg->line_outs) {
4388 if (cfg->dig_outs || cfg->dig_in_pin) {
4389 spec->multiout.max_channels = 2;
4390 spec->no_analog = 1;
4391 goto dig_only;
4392 }
Takashi Iwaic9e4bdb2013-12-06 09:30:14 +01004393 if (!cfg->num_inputs && !cfg->dig_in_pin)
4394 return 0; /* can't find valid BIOS pin config */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004395 }
4396
4397 if (!spec->no_primary_hp &&
4398 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
4399 cfg->line_outs <= cfg->hp_outs) {
4400 /* use HP as primary out */
4401 cfg->speaker_outs = cfg->line_outs;
4402 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4403 sizeof(cfg->speaker_pins));
4404 cfg->line_outs = cfg->hp_outs;
4405 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4406 cfg->hp_outs = 0;
4407 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4408 cfg->line_out_type = AUTO_PIN_HP_OUT;
4409 }
4410
4411 err = parse_output_paths(codec);
4412 if (err < 0)
4413 return err;
4414 err = create_multi_channel_mode(codec);
4415 if (err < 0)
4416 return err;
4417 err = create_multi_out_ctls(codec, cfg);
4418 if (err < 0)
4419 return err;
4420 err = create_hp_out_ctls(codec);
4421 if (err < 0)
4422 return err;
4423 err = create_speaker_out_ctls(codec);
4424 if (err < 0)
4425 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004426 err = create_indep_hp_ctls(codec);
4427 if (err < 0)
4428 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01004429 err = create_loopback_mixing_ctl(codec);
4430 if (err < 0)
4431 return err;
Takashi Iwai967303d2013-02-19 17:12:42 +01004432 err = create_hp_mic(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004433 if (err < 0)
4434 return err;
4435 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02004436 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02004437 return err;
4438
Takashi Iwaia07a9492013-01-07 16:44:06 +01004439 spec->const_channel_count = spec->ext_channel_count;
4440 /* check the multiple speaker and headphone pins */
4441 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4442 spec->const_channel_count = max(spec->const_channel_count,
4443 cfg->speaker_outs * 2);
4444 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4445 spec->const_channel_count = max(spec->const_channel_count,
4446 cfg->hp_outs * 2);
4447 spec->multiout.max_channels = max(spec->ext_channel_count,
4448 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004449
4450 err = check_auto_mute_availability(codec);
4451 if (err < 0)
4452 return err;
4453
4454 err = check_dyn_adc_switch(codec);
4455 if (err < 0)
4456 return err;
4457
Takashi Iwai967303d2013-02-19 17:12:42 +01004458 err = check_auto_mic_availability(codec);
4459 if (err < 0)
4460 return err;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004461
Takashi Iwaif1e762d2013-12-09 16:02:24 +01004462 /* add stereo mix if available and not enabled yet */
4463 if (!spec->auto_mic && spec->mixer_nid &&
4464 spec->add_stereo_mix_input &&
4465 spec->input_mux.num_items > 1 &&
4466 snd_hda_get_bool_hint(codec, "add_stereo_mix_input") < 0) {
4467 err = parse_capture_source(codec, spec->mixer_nid,
4468 CFG_IDX_MIX, spec->num_all_adcs,
4469 "Stereo Mix", 0);
4470 if (err < 0)
4471 return err;
4472 }
4473
4474
Takashi Iwai352f7f92012-12-19 12:52:06 +01004475 err = create_capture_mixers(codec);
4476 if (err < 0)
4477 return err;
4478
4479 err = parse_mic_boost(codec);
4480 if (err < 0)
4481 return err;
4482
Takashi Iwaiced4cef2013-11-26 08:33:45 +01004483 /* create "Headphone Mic Jack Mode" if no input selection is
4484 * available (or user specifies add_jack_modes hint)
4485 */
4486 if (spec->hp_mic_pin &&
4487 (spec->auto_mic || spec->input_mux.num_items == 1 ||
4488 spec->add_jack_modes)) {
4489 err = create_hp_mic_jack_mode(codec, spec->hp_mic_pin);
4490 if (err < 0)
4491 return err;
4492 }
4493
Takashi Iwaif811c3c2013-03-07 18:32:59 +01004494 if (spec->add_jack_modes) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01004495 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4496 err = create_out_jack_modes(codec, cfg->line_outs,
4497 cfg->line_out_pins);
4498 if (err < 0)
4499 return err;
4500 }
4501 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4502 err = create_out_jack_modes(codec, cfg->hp_outs,
4503 cfg->hp_pins);
4504 if (err < 0)
4505 return err;
4506 }
4507 }
4508
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004509 /* mute all aamix input initially */
4510 if (spec->mixer_nid)
4511 mute_all_mixer_nid(codec, spec->mixer_nid);
4512
Takashi Iwai352f7f92012-12-19 12:52:06 +01004513 dig_only:
4514 parse_digital(codec);
4515
Takashi Iwai55196ff2013-01-24 17:32:56 +01004516 if (spec->power_down_unused)
4517 codec->power_filter = snd_hda_gen_path_power_filter;
4518
Takashi Iwai7504b6c2013-03-18 11:25:51 +01004519 if (!spec->no_analog && spec->beep_nid) {
4520 err = snd_hda_attach_beep_device(codec, spec->beep_nid);
4521 if (err < 0)
4522 return err;
4523 }
4524
Takashi Iwai352f7f92012-12-19 12:52:06 +01004525 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004527EXPORT_SYMBOL_GPL(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528
4529
4530/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004531 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004533
4534/* slave controls for virtual master */
4535static const char * const slave_pfxs[] = {
4536 "Front", "Surround", "Center", "LFE", "Side",
4537 "Headphone", "Speaker", "Mono", "Line Out",
4538 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01004539 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
4540 "Headphone Front", "Headphone Surround", "Headphone CLFE",
4541 "Headphone Side",
Takashi Iwai352f7f92012-12-19 12:52:06 +01004542 NULL,
4543};
4544
4545int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004547 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549
Takashi Iwai36502d02012-12-19 15:15:10 +01004550 if (spec->kctls.used) {
4551 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
4552 if (err < 0)
4553 return err;
4554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555
Takashi Iwai352f7f92012-12-19 12:52:06 +01004556 if (spec->multiout.dig_out_nid) {
4557 err = snd_hda_create_dig_out_ctls(codec,
4558 spec->multiout.dig_out_nid,
4559 spec->multiout.dig_out_nid,
4560 spec->pcm_rec[1].pcm_type);
4561 if (err < 0)
4562 return err;
4563 if (!spec->no_analog) {
4564 err = snd_hda_create_spdif_share_sw(codec,
4565 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 if (err < 0)
4567 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004568 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569 }
4570 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004571 if (spec->dig_in_nid) {
4572 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
4573 if (err < 0)
4574 return err;
4575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576
Takashi Iwai352f7f92012-12-19 12:52:06 +01004577 /* if we have no master control, let's create it */
4578 if (!spec->no_analog &&
4579 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004580 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01004581 spec->vmaster_tlv, slave_pfxs,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004582 "Playback Volume");
4583 if (err < 0)
4584 return err;
4585 }
4586 if (!spec->no_analog &&
4587 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
4588 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
4589 NULL, slave_pfxs,
4590 "Playback Switch",
4591 true, &spec->vmaster_mute.sw_kctl);
4592 if (err < 0)
4593 return err;
Takashi Iwaib63eae02013-10-25 23:43:10 +02004594 if (spec->vmaster_mute.hook) {
Takashi Iwaifd25a972012-12-20 14:57:18 +01004595 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
4596 spec->vmaster_mute_enum);
Takashi Iwaib63eae02013-10-25 23:43:10 +02004597 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
4598 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004599 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600
Takashi Iwai352f7f92012-12-19 12:52:06 +01004601 free_kctls(spec); /* no longer needed */
4602
Takashi Iwai352f7f92012-12-19 12:52:06 +01004603 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
4604 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605 return err;
4606
4607 return 0;
4608}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004609EXPORT_SYMBOL_GPL(snd_hda_gen_build_controls);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004610
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611
4612/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004613 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004616static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
4617 struct hda_codec *codec,
4618 struct snd_pcm_substream *substream,
4619 int action)
4620{
4621 struct hda_gen_spec *spec = codec->spec;
4622 if (spec->pcm_playback_hook)
4623 spec->pcm_playback_hook(hinfo, codec, substream, action);
4624}
4625
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004626static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
4627 struct hda_codec *codec,
4628 struct snd_pcm_substream *substream,
4629 int action)
4630{
4631 struct hda_gen_spec *spec = codec->spec;
4632 if (spec->pcm_capture_hook)
4633 spec->pcm_capture_hook(hinfo, codec, substream, action);
4634}
4635
Takashi Iwai352f7f92012-12-19 12:52:06 +01004636/*
4637 * Analog playback callbacks
4638 */
4639static int playback_pcm_open(struct hda_pcm_stream *hinfo,
4640 struct hda_codec *codec,
4641 struct snd_pcm_substream *substream)
4642{
4643 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004644 int err;
4645
4646 mutex_lock(&spec->pcm_mutex);
4647 err = snd_hda_multi_out_analog_open(codec,
4648 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004649 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004650 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004651 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004652 call_pcm_playback_hook(hinfo, codec, substream,
4653 HDA_GEN_PCM_ACT_OPEN);
4654 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004655 mutex_unlock(&spec->pcm_mutex);
4656 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004657}
4658
4659static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01004660 struct hda_codec *codec,
4661 unsigned int stream_tag,
4662 unsigned int format,
4663 struct snd_pcm_substream *substream)
4664{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004665 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004666 int err;
4667
4668 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
4669 stream_tag, format, substream);
4670 if (!err)
4671 call_pcm_playback_hook(hinfo, codec, substream,
4672 HDA_GEN_PCM_ACT_PREPARE);
4673 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004674}
Takashi Iwai97ec5582006-03-21 11:29:07 +01004675
Takashi Iwai352f7f92012-12-19 12:52:06 +01004676static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4677 struct hda_codec *codec,
4678 struct snd_pcm_substream *substream)
4679{
4680 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004681 int err;
4682
4683 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
4684 if (!err)
4685 call_pcm_playback_hook(hinfo, codec, substream,
4686 HDA_GEN_PCM_ACT_CLEANUP);
4687 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004688}
4689
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004690static int playback_pcm_close(struct hda_pcm_stream *hinfo,
4691 struct hda_codec *codec,
4692 struct snd_pcm_substream *substream)
4693{
4694 struct hda_gen_spec *spec = codec->spec;
4695 mutex_lock(&spec->pcm_mutex);
4696 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004697 call_pcm_playback_hook(hinfo, codec, substream,
4698 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004699 mutex_unlock(&spec->pcm_mutex);
4700 return 0;
4701}
4702
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004703static int capture_pcm_open(struct hda_pcm_stream *hinfo,
4704 struct hda_codec *codec,
4705 struct snd_pcm_substream *substream)
4706{
4707 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
4708 return 0;
4709}
4710
4711static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4712 struct hda_codec *codec,
4713 unsigned int stream_tag,
4714 unsigned int format,
4715 struct snd_pcm_substream *substream)
4716{
4717 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4718 call_pcm_capture_hook(hinfo, codec, substream,
4719 HDA_GEN_PCM_ACT_PREPARE);
4720 return 0;
4721}
4722
4723static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4724 struct hda_codec *codec,
4725 struct snd_pcm_substream *substream)
4726{
4727 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4728 call_pcm_capture_hook(hinfo, codec, substream,
4729 HDA_GEN_PCM_ACT_CLEANUP);
4730 return 0;
4731}
4732
4733static int capture_pcm_close(struct hda_pcm_stream *hinfo,
4734 struct hda_codec *codec,
4735 struct snd_pcm_substream *substream)
4736{
4737 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
4738 return 0;
4739}
4740
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004741static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
4742 struct hda_codec *codec,
4743 struct snd_pcm_substream *substream)
4744{
4745 struct hda_gen_spec *spec = codec->spec;
4746 int err = 0;
4747
4748 mutex_lock(&spec->pcm_mutex);
4749 if (!spec->indep_hp_enabled)
4750 err = -EBUSY;
4751 else
4752 spec->active_streams |= 1 << STREAM_INDEP_HP;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004753 call_pcm_playback_hook(hinfo, codec, substream,
4754 HDA_GEN_PCM_ACT_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004755 mutex_unlock(&spec->pcm_mutex);
4756 return err;
4757}
4758
4759static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
4760 struct hda_codec *codec,
4761 struct snd_pcm_substream *substream)
4762{
4763 struct hda_gen_spec *spec = codec->spec;
4764 mutex_lock(&spec->pcm_mutex);
4765 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004766 call_pcm_playback_hook(hinfo, codec, substream,
4767 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004768 mutex_unlock(&spec->pcm_mutex);
4769 return 0;
4770}
4771
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004772static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4773 struct hda_codec *codec,
4774 unsigned int stream_tag,
4775 unsigned int format,
4776 struct snd_pcm_substream *substream)
4777{
4778 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4779 call_pcm_playback_hook(hinfo, codec, substream,
4780 HDA_GEN_PCM_ACT_PREPARE);
4781 return 0;
4782}
4783
4784static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4785 struct hda_codec *codec,
4786 struct snd_pcm_substream *substream)
4787{
4788 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4789 call_pcm_playback_hook(hinfo, codec, substream,
4790 HDA_GEN_PCM_ACT_CLEANUP);
4791 return 0;
4792}
4793
Takashi Iwai352f7f92012-12-19 12:52:06 +01004794/*
4795 * Digital out
4796 */
4797static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
4798 struct hda_codec *codec,
4799 struct snd_pcm_substream *substream)
4800{
4801 struct hda_gen_spec *spec = codec->spec;
4802 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
4803}
4804
4805static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4806 struct hda_codec *codec,
4807 unsigned int stream_tag,
4808 unsigned int format,
4809 struct snd_pcm_substream *substream)
4810{
4811 struct hda_gen_spec *spec = codec->spec;
4812 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
4813 stream_tag, format, substream);
4814}
4815
4816static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4817 struct hda_codec *codec,
4818 struct snd_pcm_substream *substream)
4819{
4820 struct hda_gen_spec *spec = codec->spec;
4821 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
4822}
4823
4824static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
4825 struct hda_codec *codec,
4826 struct snd_pcm_substream *substream)
4827{
4828 struct hda_gen_spec *spec = codec->spec;
4829 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
4830}
4831
4832/*
4833 * Analog capture
4834 */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004835#define alt_capture_pcm_open capture_pcm_open
4836#define alt_capture_pcm_close capture_pcm_close
4837
Takashi Iwai352f7f92012-12-19 12:52:06 +01004838static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4839 struct hda_codec *codec,
4840 unsigned int stream_tag,
4841 unsigned int format,
4842 struct snd_pcm_substream *substream)
4843{
4844 struct hda_gen_spec *spec = codec->spec;
4845
4846 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01004847 stream_tag, 0, format);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004848 call_pcm_capture_hook(hinfo, codec, substream,
4849 HDA_GEN_PCM_ACT_PREPARE);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004850 return 0;
4851}
4852
Takashi Iwai352f7f92012-12-19 12:52:06 +01004853static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4854 struct hda_codec *codec,
4855 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01004856{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004857 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01004858
Takashi Iwai352f7f92012-12-19 12:52:06 +01004859 snd_hda_codec_cleanup_stream(codec,
4860 spec->adc_nids[substream->number + 1]);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004861 call_pcm_capture_hook(hinfo, codec, substream,
4862 HDA_GEN_PCM_ACT_CLEANUP);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004863 return 0;
4864}
4865
Takashi Iwai352f7f92012-12-19 12:52:06 +01004866/*
4867 */
4868static const struct hda_pcm_stream pcm_analog_playback = {
4869 .substreams = 1,
4870 .channels_min = 2,
4871 .channels_max = 8,
4872 /* NID is set in build_pcms */
4873 .ops = {
4874 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004875 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004876 .prepare = playback_pcm_prepare,
4877 .cleanup = playback_pcm_cleanup
4878 },
4879};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880
Takashi Iwai352f7f92012-12-19 12:52:06 +01004881static const struct hda_pcm_stream pcm_analog_capture = {
4882 .substreams = 1,
4883 .channels_min = 2,
4884 .channels_max = 2,
4885 /* NID is set in build_pcms */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004886 .ops = {
4887 .open = capture_pcm_open,
4888 .close = capture_pcm_close,
4889 .prepare = capture_pcm_prepare,
4890 .cleanup = capture_pcm_cleanup
4891 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004892};
4893
4894static const struct hda_pcm_stream pcm_analog_alt_playback = {
4895 .substreams = 1,
4896 .channels_min = 2,
4897 .channels_max = 2,
4898 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004899 .ops = {
4900 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004901 .close = alt_playback_pcm_close,
4902 .prepare = alt_playback_pcm_prepare,
4903 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004904 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004905};
4906
4907static const struct hda_pcm_stream pcm_analog_alt_capture = {
4908 .substreams = 2, /* can be overridden */
4909 .channels_min = 2,
4910 .channels_max = 2,
4911 /* NID is set in build_pcms */
4912 .ops = {
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004913 .open = alt_capture_pcm_open,
4914 .close = alt_capture_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004915 .prepare = alt_capture_pcm_prepare,
4916 .cleanup = alt_capture_pcm_cleanup
4917 },
4918};
4919
4920static const struct hda_pcm_stream pcm_digital_playback = {
4921 .substreams = 1,
4922 .channels_min = 2,
4923 .channels_max = 2,
4924 /* NID is set in build_pcms */
4925 .ops = {
4926 .open = dig_playback_pcm_open,
4927 .close = dig_playback_pcm_close,
4928 .prepare = dig_playback_pcm_prepare,
4929 .cleanup = dig_playback_pcm_cleanup
4930 },
4931};
4932
4933static const struct hda_pcm_stream pcm_digital_capture = {
4934 .substreams = 1,
4935 .channels_min = 2,
4936 .channels_max = 2,
4937 /* NID is set in build_pcms */
4938};
4939
4940/* Used by build_pcms to flag that a PCM has no playback stream */
4941static const struct hda_pcm_stream pcm_null_stream = {
4942 .substreams = 0,
4943 .channels_min = 0,
4944 .channels_max = 0,
4945};
4946
4947/*
4948 * dynamic changing ADC PCM streams
4949 */
4950static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
4951{
4952 struct hda_gen_spec *spec = codec->spec;
4953 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
4954
4955 if (spec->cur_adc && spec->cur_adc != new_adc) {
4956 /* stream is running, let's swap the current ADC */
4957 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
4958 spec->cur_adc = new_adc;
4959 snd_hda_codec_setup_stream(codec, new_adc,
4960 spec->cur_adc_stream_tag, 0,
4961 spec->cur_adc_format);
4962 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004964 return false;
4965}
4966
4967/* analog capture with dynamic dual-adc changes */
4968static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4969 struct hda_codec *codec,
4970 unsigned int stream_tag,
4971 unsigned int format,
4972 struct snd_pcm_substream *substream)
4973{
4974 struct hda_gen_spec *spec = codec->spec;
4975 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
4976 spec->cur_adc_stream_tag = stream_tag;
4977 spec->cur_adc_format = format;
4978 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
4979 return 0;
4980}
4981
4982static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4983 struct hda_codec *codec,
4984 struct snd_pcm_substream *substream)
4985{
4986 struct hda_gen_spec *spec = codec->spec;
4987 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
4988 spec->cur_adc = 0;
4989 return 0;
4990}
4991
4992static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
4993 .substreams = 1,
4994 .channels_min = 2,
4995 .channels_max = 2,
4996 .nid = 0, /* fill later */
4997 .ops = {
4998 .prepare = dyn_adc_capture_pcm_prepare,
4999 .cleanup = dyn_adc_capture_pcm_cleanup
5000 },
5001};
5002
Takashi Iwaif873e532012-12-20 16:58:39 +01005003static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
5004 const char *chip_name)
5005{
5006 char *p;
5007
5008 if (*str)
5009 return;
5010 strlcpy(str, chip_name, len);
5011
5012 /* drop non-alnum chars after a space */
5013 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
5014 if (!isalnum(p[1])) {
5015 *p = 0;
5016 break;
5017 }
5018 }
5019 strlcat(str, sfx, len);
5020}
5021
Takashi Iwai352f7f92012-12-19 12:52:06 +01005022/* build PCM streams based on the parsed results */
5023int snd_hda_gen_build_pcms(struct hda_codec *codec)
5024{
5025 struct hda_gen_spec *spec = codec->spec;
5026 struct hda_pcm *info = spec->pcm_rec;
5027 const struct hda_pcm_stream *p;
5028 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029
5030 codec->num_pcms = 1;
5031 codec->pcm_info = info;
5032
Takashi Iwai352f7f92012-12-19 12:52:06 +01005033 if (spec->no_analog)
5034 goto skip_analog;
5035
Takashi Iwaif873e532012-12-20 16:58:39 +01005036 fill_pcm_stream_name(spec->stream_name_analog,
5037 sizeof(spec->stream_name_analog),
5038 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005039 info->name = spec->stream_name_analog;
5040
5041 if (spec->multiout.num_dacs > 0) {
5042 p = spec->stream_analog_playback;
5043 if (!p)
5044 p = &pcm_analog_playback;
5045 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
5046 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
5047 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
5048 spec->multiout.max_channels;
5049 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
5050 spec->autocfg.line_outs == 2)
5051 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
5052 snd_pcm_2_1_chmaps;
5053 }
5054 if (spec->num_adc_nids) {
5055 p = spec->stream_analog_capture;
5056 if (!p) {
5057 if (spec->dyn_adc_switch)
5058 p = &dyn_adc_pcm_analog_capture;
5059 else
5060 p = &pcm_analog_capture;
5061 }
5062 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
5063 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
5064 }
5065
Takashi Iwai352f7f92012-12-19 12:52:06 +01005066 skip_analog:
5067 /* SPDIF for stream index #1 */
5068 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01005069 fill_pcm_stream_name(spec->stream_name_digital,
5070 sizeof(spec->stream_name_digital),
5071 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005072 codec->num_pcms = 2;
5073 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
5074 info = spec->pcm_rec + 1;
5075 info->name = spec->stream_name_digital;
5076 if (spec->dig_out_type)
5077 info->pcm_type = spec->dig_out_type;
5078 else
5079 info->pcm_type = HDA_PCM_TYPE_SPDIF;
5080 if (spec->multiout.dig_out_nid) {
5081 p = spec->stream_digital_playback;
5082 if (!p)
5083 p = &pcm_digital_playback;
5084 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
5085 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
5086 }
5087 if (spec->dig_in_nid) {
5088 p = spec->stream_digital_capture;
5089 if (!p)
5090 p = &pcm_digital_capture;
5091 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
5092 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
5093 }
5094 }
5095
5096 if (spec->no_analog)
5097 return 0;
5098
5099 /* If the use of more than one ADC is requested for the current
5100 * model, configure a second analog capture-only PCM.
5101 */
5102 have_multi_adcs = (spec->num_adc_nids > 1) &&
5103 !spec->dyn_adc_switch && !spec->auto_mic;
5104 /* Additional Analaog capture for index #2 */
5105 if (spec->alt_dac_nid || have_multi_adcs) {
Takashi Iwaia6071482013-01-21 16:50:09 +01005106 fill_pcm_stream_name(spec->stream_name_alt_analog,
5107 sizeof(spec->stream_name_alt_analog),
5108 " Alt Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005109 codec->num_pcms = 3;
5110 info = spec->pcm_rec + 2;
Takashi Iwaia6071482013-01-21 16:50:09 +01005111 info->name = spec->stream_name_alt_analog;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005112 if (spec->alt_dac_nid) {
5113 p = spec->stream_analog_alt_playback;
5114 if (!p)
5115 p = &pcm_analog_alt_playback;
5116 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
5117 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
5118 spec->alt_dac_nid;
5119 } else {
5120 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
5121 pcm_null_stream;
5122 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
5123 }
5124 if (have_multi_adcs) {
5125 p = spec->stream_analog_alt_capture;
5126 if (!p)
5127 p = &pcm_analog_alt_capture;
5128 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
5129 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
5130 spec->adc_nids[1];
5131 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
5132 spec->num_adc_nids - 1;
5133 } else {
5134 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
5135 pcm_null_stream;
5136 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
5137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138 }
5139
5140 return 0;
5141}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005142EXPORT_SYMBOL_GPL(snd_hda_gen_build_pcms);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005143
5144
5145/*
5146 * Standard auto-parser initializations
5147 */
5148
Takashi Iwaid4156932013-01-07 10:08:02 +01005149/* configure the given path as a proper output */
Takashi Iwai2c12c302013-01-10 09:33:29 +01005150static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005151{
5152 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01005153 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005154
Takashi Iwai196c17662013-01-04 15:01:40 +01005155 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01005156 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005157 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01005158 pin = path->path[path->depth - 1];
Takashi Iwai2c12c302013-01-10 09:33:29 +01005159 restore_pin_ctl(codec, pin);
Takashi Iwai65033cc2013-04-16 12:31:05 +02005160 snd_hda_activate_path(codec, path, path->active,
5161 aamix_default(codec->spec));
Takashi Iwaie1284af2013-01-03 16:33:02 +01005162 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005163}
5164
5165/* initialize primary output paths */
5166static void init_multi_out(struct hda_codec *codec)
5167{
5168 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005169 int i;
5170
Takashi Iwaid4156932013-01-07 10:08:02 +01005171 for (i = 0; i < spec->autocfg.line_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005172 set_output_and_unmute(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005173}
5174
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005175
Takashi Iwai2c12c302013-01-10 09:33:29 +01005176static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005177{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005178 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005179
Takashi Iwaid4156932013-01-07 10:08:02 +01005180 for (i = 0; i < num_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005181 set_output_and_unmute(codec, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005182}
5183
5184/* initialize hp and speaker paths */
5185static void init_extra_out(struct hda_codec *codec)
5186{
5187 struct hda_gen_spec *spec = codec->spec;
5188
5189 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005190 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005191 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
5192 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai2c12c302013-01-10 09:33:29 +01005193 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005194}
5195
5196/* initialize multi-io paths */
5197static void init_multi_io(struct hda_codec *codec)
5198{
5199 struct hda_gen_spec *spec = codec->spec;
5200 int i;
5201
5202 for (i = 0; i < spec->multi_ios; i++) {
5203 hda_nid_t pin = spec->multi_io[i].pin;
5204 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01005205 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005206 if (!path)
5207 continue;
5208 if (!spec->multi_io[i].ctl_in)
5209 spec->multi_io[i].ctl_in =
Takashi Iwai2c12c302013-01-10 09:33:29 +01005210 snd_hda_codec_get_pin_target(codec, pin);
Takashi Iwai65033cc2013-04-16 12:31:05 +02005211 snd_hda_activate_path(codec, path, path->active,
5212 aamix_default(spec));
Takashi Iwai352f7f92012-12-19 12:52:06 +01005213 }
5214}
5215
Takashi Iwai4f7f67f2013-12-03 10:51:37 +01005216static void init_aamix_paths(struct hda_codec *codec)
5217{
5218 struct hda_gen_spec *spec = codec->spec;
5219
5220 if (!spec->have_aamix_ctl)
5221 return;
5222 update_aamix_paths(codec, spec->aamix_mode, spec->out_paths[0],
5223 spec->aamix_out_paths[0],
5224 spec->autocfg.line_out_type);
5225 update_aamix_paths(codec, spec->aamix_mode, spec->hp_paths[0],
5226 spec->aamix_out_paths[1],
5227 AUTO_PIN_HP_OUT);
5228 update_aamix_paths(codec, spec->aamix_mode, spec->speaker_paths[0],
5229 spec->aamix_out_paths[2],
5230 AUTO_PIN_SPEAKER_OUT);
5231}
5232
Takashi Iwai352f7f92012-12-19 12:52:06 +01005233/* set up input pins and loopback paths */
5234static void init_analog_input(struct hda_codec *codec)
5235{
5236 struct hda_gen_spec *spec = codec->spec;
5237 struct auto_pin_cfg *cfg = &spec->autocfg;
5238 int i;
5239
5240 for (i = 0; i < cfg->num_inputs; i++) {
5241 hda_nid_t nid = cfg->inputs[i].pin;
5242 if (is_input_pin(codec, nid))
Takashi Iwai2c12c302013-01-10 09:33:29 +01005243 restore_pin_ctl(codec, nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005244
5245 /* init loopback inputs */
5246 if (spec->mixer_nid) {
Takashi Iwai3e367f12013-01-23 17:07:23 +01005247 resume_path_from_idx(codec, spec->loopback_paths[i]);
5248 resume_path_from_idx(codec, spec->loopback_merge_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005249 }
5250 }
5251}
5252
5253/* initialize ADC paths */
5254static void init_input_src(struct hda_codec *codec)
5255{
5256 struct hda_gen_spec *spec = codec->spec;
5257 struct hda_input_mux *imux = &spec->input_mux;
5258 struct nid_path *path;
5259 int i, c, nums;
5260
5261 if (spec->dyn_adc_switch)
5262 nums = 1;
5263 else
5264 nums = spec->num_adc_nids;
5265
5266 for (c = 0; c < nums; c++) {
5267 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01005268 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005269 if (path) {
5270 bool active = path->active;
5271 if (i == spec->cur_mux[c])
5272 active = true;
5273 snd_hda_activate_path(codec, path, active, false);
5274 }
5275 }
Takashi Iwai967303d2013-02-19 17:12:42 +01005276 if (spec->hp_mic)
5277 update_hp_mic(codec, c, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005278 }
5279
Takashi Iwai352f7f92012-12-19 12:52:06 +01005280 if (spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01005281 spec->cap_sync_hook(codec, NULL, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005282}
5283
5284/* set right pin controls for digital I/O */
5285static void init_digital(struct hda_codec *codec)
5286{
5287 struct hda_gen_spec *spec = codec->spec;
5288 int i;
5289 hda_nid_t pin;
5290
Takashi Iwaid4156932013-01-07 10:08:02 +01005291 for (i = 0; i < spec->autocfg.dig_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005292 set_output_and_unmute(codec, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005293 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005294 if (pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01005295 restore_pin_ctl(codec, pin);
Takashi Iwai3e367f12013-01-23 17:07:23 +01005296 resume_path_from_idx(codec, spec->digin_path);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005297 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005298}
5299
Takashi Iwai973e4972012-12-20 15:16:09 +01005300/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5301 * invalid unsol tags by some reason
5302 */
5303static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5304{
5305 int i;
5306
5307 for (i = 0; i < codec->init_pins.used; i++) {
5308 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
5309 hda_nid_t nid = pin->nid;
5310 if (is_jack_detectable(codec, nid) &&
5311 !snd_hda_jack_tbl_get(codec, nid))
5312 snd_hda_codec_update_cache(codec, nid, 0,
5313 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5314 }
5315}
5316
Takashi Iwai5187ac12013-01-07 12:52:16 +01005317/*
5318 * initialize the generic spec;
5319 * this can be put as patch_ops.init function
5320 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005321int snd_hda_gen_init(struct hda_codec *codec)
5322{
5323 struct hda_gen_spec *spec = codec->spec;
5324
5325 if (spec->init_hook)
5326 spec->init_hook(codec);
5327
5328 snd_hda_apply_verbs(codec);
5329
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005330 codec->cached_write = 1;
5331
Takashi Iwai352f7f92012-12-19 12:52:06 +01005332 init_multi_out(codec);
5333 init_extra_out(codec);
5334 init_multi_io(codec);
Takashi Iwai4f7f67f2013-12-03 10:51:37 +01005335 init_aamix_paths(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005336 init_analog_input(codec);
5337 init_input_src(codec);
5338 init_digital(codec);
5339
Takashi Iwai973e4972012-12-20 15:16:09 +01005340 clear_unsol_on_unused_pins(codec);
5341
Takashi Iwai352f7f92012-12-19 12:52:06 +01005342 /* call init functions of standard auto-mute helpers */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01005343 update_automute_all(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005344
Takashi Iwaidc870f32013-01-22 15:24:30 +01005345 snd_hda_codec_flush_cache(codec);
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005346
Takashi Iwai352f7f92012-12-19 12:52:06 +01005347 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
5348 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5349
5350 hda_call_check_power_status(codec, 0x01);
5351 return 0;
5352}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005353EXPORT_SYMBOL_GPL(snd_hda_gen_init);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005354
Takashi Iwai5187ac12013-01-07 12:52:16 +01005355/*
5356 * free the generic spec;
5357 * this can be put as patch_ops.free function
5358 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005359void snd_hda_gen_free(struct hda_codec *codec)
5360{
Takashi Iwai8a02c0c2014-02-10 18:09:45 +01005361 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_FREE);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005362 snd_hda_gen_spec_free(codec->spec);
5363 kfree(codec->spec);
5364 codec->spec = NULL;
5365}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005366EXPORT_SYMBOL_GPL(snd_hda_gen_free);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005367
5368#ifdef CONFIG_PM
Takashi Iwai5187ac12013-01-07 12:52:16 +01005369/*
5370 * check the loopback power save state;
5371 * this can be put as patch_ops.check_power_status function
5372 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005373int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
5374{
5375 struct hda_gen_spec *spec = codec->spec;
5376 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
5377}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005378EXPORT_SYMBOL_GPL(snd_hda_gen_check_power_status);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005379#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01005380
5381
5382/*
5383 * the generic codec support
5384 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385
Takashi Iwai352f7f92012-12-19 12:52:06 +01005386static const struct hda_codec_ops generic_patch_ops = {
5387 .build_controls = snd_hda_gen_build_controls,
5388 .build_pcms = snd_hda_gen_build_pcms,
5389 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01005390 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005391 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02005392#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01005393 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02005394#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395};
5396
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397int snd_hda_parse_generic_codec(struct hda_codec *codec)
5398{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005399 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400 int err;
5401
Takashi Iwaie560d8d2005-09-09 14:21:46 +02005402 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005403 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005405 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407
Takashi Iwai9eb413e2012-12-19 14:41:21 +01005408 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
5409 if (err < 0)
5410 return err;
5411
5412 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005413 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414 goto error;
5415
5416 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417 return 0;
5418
Takashi Iwai352f7f92012-12-19 12:52:06 +01005419error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01005420 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421 return err;
5422}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005423EXPORT_SYMBOL_GPL(snd_hda_parse_generic_codec);
Takashi Iwaib21bdd02013-11-18 12:03:56 +01005424
5425MODULE_LICENSE("GPL");
5426MODULE_DESCRIPTION("Generic HD-audio codec parser");