blob: ce004326d54bd86ea20f9cad25214cb91fc9e972 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "hda_codec.h"
35#include "hda_local.h"
Takashi Iwai352f7f92012-12-19 12:52:06 +010036#include "hda_auto_parser.h"
37#include "hda_jack.h"
Takashi Iwai7504b6c2013-03-18 11:25:51 +010038#include "hda_beep.h"
Takashi Iwai352f7f92012-12-19 12:52:06 +010039#include "hda_generic.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Takashi Iwai352f7f92012-12-19 12:52:06 +010042/* initialize hda_gen_spec struct */
43int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
Takashi Iwai352f7f92012-12-19 12:52:06 +010045 snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
Takashi Iwai352f7f92012-12-19 12:52:06 +010046 snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
Takashi Iwai0186f4f2013-02-07 10:45:11 +010047 snd_array_init(&spec->loopback_list, sizeof(struct hda_amp_list), 8);
Takashi Iwai38cf6f12012-12-21 14:09:42 +010048 mutex_init(&spec->pcm_mutex);
Takashi Iwai352f7f92012-12-19 12:52:06 +010049 return 0;
50}
Takashi Iwai2698ea92013-12-18 07:45:52 +010051EXPORT_SYMBOL_GPL(snd_hda_gen_spec_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Takashi Iwai12c93df2012-12-19 14:38:33 +010053struct snd_kcontrol_new *
54snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
55 const struct snd_kcontrol_new *temp)
Takashi Iwai352f7f92012-12-19 12:52:06 +010056{
57 struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
58 if (!knew)
59 return NULL;
60 *knew = *temp;
61 if (name)
62 knew->name = kstrdup(name, GFP_KERNEL);
63 else if (knew->name)
64 knew->name = kstrdup(knew->name, GFP_KERNEL);
65 if (!knew->name)
66 return NULL;
67 return knew;
68}
Takashi Iwai2698ea92013-12-18 07:45:52 +010069EXPORT_SYMBOL_GPL(snd_hda_gen_add_kctl);
Takashi Iwai352f7f92012-12-19 12:52:06 +010070
71static void free_kctls(struct hda_gen_spec *spec)
72{
73 if (spec->kctls.list) {
74 struct snd_kcontrol_new *kctl = spec->kctls.list;
75 int i;
76 for (i = 0; i < spec->kctls.used; i++)
77 kfree(kctl[i].name);
78 }
79 snd_array_free(&spec->kctls);
80}
81
Takashi Iwai352f7f92012-12-19 12:52:06 +010082void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
83{
84 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 return;
Takashi Iwai352f7f92012-12-19 12:52:06 +010086 free_kctls(spec);
Takashi Iwai352f7f92012-12-19 12:52:06 +010087 snd_array_free(&spec->paths);
Takashi Iwai0186f4f2013-02-07 10:45:11 +010088 snd_array_free(&spec->loopback_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
Takashi Iwai2698ea92013-12-18 07:45:52 +010090EXPORT_SYMBOL_GPL(snd_hda_gen_spec_free);
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 Iwai0c8c0f52012-12-20 17:54:22 +0100350static void print_nid_path(const char *pfx, struct nid_path *path)
351{
352 char buf[40];
353 int i;
354
355
356 buf[0] = 0;
357 for (i = 0; i < path->depth; i++) {
358 char tmp[4];
359 sprintf(tmp, ":%02x", path->path[i]);
360 strlcat(buf, tmp, sizeof(buf));
361 }
362 snd_printdd("%s path: depth=%d %s\n", pfx, path->depth, buf);
363}
364
Takashi Iwai352f7f92012-12-19 12:52:06 +0100365/* called recursively */
366static bool __parse_nid_path(struct hda_codec *codec,
367 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100368 int anchor_nid, struct nid_path *path,
369 int depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100370{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100371 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100372 int i, nums;
373
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100374 if (to_nid == anchor_nid)
375 anchor_nid = 0; /* anchor passed */
376 else if (to_nid == (hda_nid_t)(-anchor_nid))
377 return false; /* hit the exclusive nid */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100378
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100379 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100380 for (i = 0; i < nums; i++) {
381 if (conn[i] != from_nid) {
382 /* special case: when from_nid is 0,
383 * try to find an empty DAC
384 */
385 if (from_nid ||
386 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
387 is_dac_already_used(codec, conn[i]))
388 continue;
389 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100390 /* anchor is not requested or already passed? */
391 if (anchor_nid <= 0)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100392 goto found;
393 }
394 if (depth >= MAX_NID_PATH_DEPTH)
395 return false;
396 for (i = 0; i < nums; i++) {
397 unsigned int type;
398 type = get_wcaps_type(get_wcaps(codec, conn[i]));
399 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
400 type == AC_WID_PIN)
401 continue;
402 if (__parse_nid_path(codec, from_nid, conn[i],
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100403 anchor_nid, path, depth + 1))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100404 goto found;
405 }
406 return false;
407
408 found:
409 path->path[path->depth] = conn[i];
410 path->idx[path->depth + 1] = i;
411 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
412 path->multi[path->depth + 1] = 1;
413 path->depth++;
414 return true;
415}
416
417/* parse the widget path from the given nid to the target nid;
418 * when @from_nid is 0, try to find an empty DAC;
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100419 * when @anchor_nid is set to a positive value, only paths through the widget
420 * with the given value are evaluated.
421 * when @anchor_nid is set to a negative value, paths through the widget
422 * with the negative of given value are excluded, only other paths are chosen.
423 * when @anchor_nid is zero, no special handling about path selection.
Takashi Iwai352f7f92012-12-19 12:52:06 +0100424 */
425bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100426 hda_nid_t to_nid, int anchor_nid,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100427 struct nid_path *path)
428{
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100429 if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100430 path->path[path->depth] = to_nid;
431 path->depth++;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100432 return true;
433 }
434 return false;
435}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100436EXPORT_SYMBOL_GPL(snd_hda_parse_nid_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100439 * parse the path between the given NIDs and add to the path list.
440 * if no valid path is found, return NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100442struct nid_path *
443snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100444 hda_nid_t to_nid, int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100446 struct hda_gen_spec *spec = codec->spec;
447 struct nid_path *path;
448
449 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
450 return NULL;
451
Takashi Iwaif5172a72013-01-04 13:19:55 +0100452 /* check whether the path has been already added */
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100453 path = get_nid_path(codec, from_nid, to_nid, anchor_nid);
Takashi Iwaif5172a72013-01-04 13:19:55 +0100454 if (path)
455 return path;
456
Takashi Iwai352f7f92012-12-19 12:52:06 +0100457 path = snd_array_new(&spec->paths);
458 if (!path)
459 return NULL;
460 memset(path, 0, sizeof(*path));
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100461 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100462 return path;
463 /* push back */
464 spec->paths.used--;
465 return NULL;
466}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100467EXPORT_SYMBOL_GPL(snd_hda_add_new_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100468
Takashi Iwai980428c2013-01-09 09:28:20 +0100469/* clear the given path as invalid so that it won't be picked up later */
470static void invalidate_nid_path(struct hda_codec *codec, int idx)
471{
472 struct nid_path *path = snd_hda_get_path_from_idx(codec, idx);
473 if (!path)
474 return;
475 memset(path, 0, sizeof(*path));
476}
477
Takashi Iwai36907392013-12-10 17:29:26 +0100478/* return a DAC if paired to the given pin by codec driver */
479static hda_nid_t get_preferred_dac(struct hda_codec *codec, hda_nid_t pin)
480{
481 struct hda_gen_spec *spec = codec->spec;
482 const hda_nid_t *list = spec->preferred_dacs;
483
484 if (!list)
485 return 0;
486 for (; *list; list += 2)
487 if (*list == pin)
488 return list[1];
489 return 0;
490}
491
Takashi Iwai352f7f92012-12-19 12:52:06 +0100492/* look for an empty DAC slot */
493static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
494 bool is_digital)
495{
496 struct hda_gen_spec *spec = codec->spec;
497 bool cap_digital;
498 int i;
499
500 for (i = 0; i < spec->num_all_dacs; i++) {
501 hda_nid_t nid = spec->all_dacs[i];
502 if (!nid || is_dac_already_used(codec, nid))
503 continue;
504 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
505 if (is_digital != cap_digital)
506 continue;
507 if (is_reachable_path(codec, nid, pin))
508 return nid;
509 }
510 return 0;
511}
512
513/* replace the channels in the composed amp value with the given number */
514static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
515{
516 val &= ~(0x3U << 16);
517 val |= chs << 16;
518 return val;
519}
520
521/* check whether the widget has the given amp capability for the direction */
522static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
523 int dir, unsigned int bits)
524{
525 if (!nid)
526 return false;
527 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
528 if (query_amp_caps(codec, nid, dir) & bits)
529 return true;
530 return false;
531}
532
David Henningsson99a55922013-01-16 15:58:44 +0100533static bool same_amp_caps(struct hda_codec *codec, hda_nid_t nid1,
534 hda_nid_t nid2, int dir)
535{
536 if (!(get_wcaps(codec, nid1) & (1 << (dir + 1))))
537 return !(get_wcaps(codec, nid2) & (1 << (dir + 1)));
538 return (query_amp_caps(codec, nid1, dir) ==
539 query_amp_caps(codec, nid2, dir));
540}
541
Takashi Iwai352f7f92012-12-19 12:52:06 +0100542#define nid_has_mute(codec, nid, dir) \
Takashi Iwaif69910d2013-08-08 09:32:37 +0200543 check_amp_caps(codec, nid, dir, (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100544#define nid_has_volume(codec, nid, dir) \
545 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
546
547/* look for a widget suitable for assigning a mute switch in the path */
548static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
549 struct nid_path *path)
550{
551 int i;
552
553 for (i = path->depth - 1; i >= 0; i--) {
554 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
555 return path->path[i];
556 if (i != path->depth - 1 && i != 0 &&
557 nid_has_mute(codec, path->path[i], HDA_INPUT))
558 return path->path[i];
559 }
560 return 0;
561}
562
563/* look for a widget suitable for assigning a volume ctl in the path */
564static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
565 struct nid_path *path)
566{
Takashi Iwaia1114a82013-11-04 16:32:01 +0100567 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100568 int i;
569
570 for (i = path->depth - 1; i >= 0; i--) {
Takashi Iwaia1114a82013-11-04 16:32:01 +0100571 hda_nid_t nid = path->path[i];
572 if ((spec->out_vol_mask >> nid) & 1)
573 continue;
574 if (nid_has_volume(codec, nid, HDA_OUTPUT))
575 return nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100576 }
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200577 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578}
579
580/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100581 * path activation / deactivation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100583
584/* can have the amp-in capability? */
585static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100587 hda_nid_t nid = path->path[idx];
588 unsigned int caps = get_wcaps(codec, nid);
589 unsigned int type = get_wcaps_type(caps);
590
591 if (!(caps & AC_WCAP_IN_AMP))
592 return false;
593 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
594 return false;
595 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596}
597
Takashi Iwai352f7f92012-12-19 12:52:06 +0100598/* can have the amp-out capability? */
599static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100601 hda_nid_t nid = path->path[idx];
602 unsigned int caps = get_wcaps(codec, nid);
603 unsigned int type = get_wcaps_type(caps);
604
605 if (!(caps & AC_WCAP_OUT_AMP))
606 return false;
607 if (type == AC_WID_PIN && !idx) /* only for output pins */
608 return false;
609 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
611
Takashi Iwai352f7f92012-12-19 12:52:06 +0100612/* check whether the given (nid,dir,idx) is active */
613static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai7dddf2ae2013-01-24 16:31:35 +0100614 unsigned int dir, unsigned int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100616 struct hda_gen_spec *spec = codec->spec;
617 int i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Takashi Iwai352f7f92012-12-19 12:52:06 +0100619 for (n = 0; n < spec->paths.used; n++) {
620 struct nid_path *path = snd_array_elem(&spec->paths, n);
621 if (!path->active)
622 continue;
623 for (i = 0; i < path->depth; i++) {
624 if (path->path[i] == nid) {
625 if (dir == HDA_OUTPUT || path->idx[i] == idx)
626 return true;
627 break;
628 }
629 }
630 }
631 return false;
632}
633
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +0200634/* check whether the NID is referred by any active paths */
635#define is_active_nid_for_any(codec, nid) \
636 is_active_nid(codec, nid, HDA_OUTPUT, 0)
637
Takashi Iwai352f7f92012-12-19 12:52:06 +0100638/* get the default amp value for the target state */
639static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai8999bf02013-01-18 11:01:33 +0100640 int dir, unsigned int caps, bool enable)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100641{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100642 unsigned int val = 0;
643
Takashi Iwai352f7f92012-12-19 12:52:06 +0100644 if (caps & AC_AMPCAP_NUM_STEPS) {
645 /* set to 0dB */
646 if (enable)
647 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
648 }
Takashi Iwaif69910d2013-08-08 09:32:37 +0200649 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100650 if (!enable)
651 val |= HDA_AMP_MUTE;
652 }
653 return val;
654}
655
656/* initialize the amp value (only at the first time) */
657static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
658{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100659 unsigned int caps = query_amp_caps(codec, nid, dir);
660 int val = get_amp_val_to_activate(codec, nid, dir, caps, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100661 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
662}
663
Takashi Iwai8999bf02013-01-18 11:01:33 +0100664/* calculate amp value mask we can modify;
665 * if the given amp is controlled by mixers, don't touch it
666 */
667static unsigned int get_amp_mask_to_modify(struct hda_codec *codec,
668 hda_nid_t nid, int dir, int idx,
669 unsigned int caps)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100670{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100671 unsigned int mask = 0xff;
672
Takashi Iwaif69910d2013-08-08 09:32:37 +0200673 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
Takashi Iwai8999bf02013-01-18 11:01:33 +0100674 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_MUTE_CTL))
675 mask &= ~0x80;
676 }
677 if (caps & AC_AMPCAP_NUM_STEPS) {
678 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
679 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
680 mask &= ~0x7f;
681 }
682 return mask;
683}
684
685static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
686 int idx, int idx_to_check, bool enable)
687{
688 unsigned int caps;
689 unsigned int mask, val;
690
Takashi Iwai7dddf2ae2013-01-24 16:31:35 +0100691 if (!enable && is_active_nid(codec, nid, dir, idx_to_check))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100692 return;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100693
694 caps = query_amp_caps(codec, nid, dir);
695 val = get_amp_val_to_activate(codec, nid, dir, caps, enable);
696 mask = get_amp_mask_to_modify(codec, nid, dir, idx_to_check, caps);
697 if (!mask)
698 return;
699
700 val &= mask;
701 snd_hda_codec_amp_stereo(codec, nid, dir, idx, mask, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100702}
703
704static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
705 int i, bool enable)
706{
707 hda_nid_t nid = path->path[i];
708 init_amp(codec, nid, HDA_OUTPUT, 0);
Takashi Iwai8999bf02013-01-18 11:01:33 +0100709 activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100710}
711
712static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
713 int i, bool enable, bool add_aamix)
714{
715 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100716 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100717 int n, nums, idx;
718 int type;
719 hda_nid_t nid = path->path[i];
720
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100721 nums = snd_hda_get_conn_list(codec, nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100722 type = get_wcaps_type(get_wcaps(codec, nid));
723 if (type == AC_WID_PIN ||
724 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
725 nums = 1;
726 idx = 0;
727 } else
728 idx = path->idx[i];
729
730 for (n = 0; n < nums; n++)
731 init_amp(codec, nid, HDA_INPUT, n);
732
Takashi Iwai352f7f92012-12-19 12:52:06 +0100733 /* here is a little bit tricky in comparison with activate_amp_out();
734 * when aa-mixer is available, we need to enable the path as well
735 */
736 for (n = 0; n < nums; n++) {
Takashi Iwaie4a395e2013-01-23 17:00:31 +0100737 if (n != idx && (!add_aamix || conn[n] != spec->mixer_merge_nid))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100738 continue;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100739 activate_amp(codec, nid, HDA_INPUT, n, idx, enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 }
741}
742
Takashi Iwai352f7f92012-12-19 12:52:06 +0100743/* activate or deactivate the given path
744 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100746void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
747 bool enable, bool add_aamix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
Takashi Iwai55196ff2013-01-24 17:32:56 +0100749 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100750 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Takashi Iwai352f7f92012-12-19 12:52:06 +0100752 if (!enable)
753 path->active = false;
754
755 for (i = path->depth - 1; i >= 0; i--) {
Takashi Iwai55196ff2013-01-24 17:32:56 +0100756 hda_nid_t nid = path->path[i];
757 if (enable && spec->power_down_unused) {
758 /* make sure the widget is powered up */
759 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0))
760 snd_hda_codec_write(codec, nid, 0,
761 AC_VERB_SET_POWER_STATE,
762 AC_PWRST_D0);
763 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100764 if (enable && path->multi[i])
Takashi Iwai55196ff2013-01-24 17:32:56 +0100765 snd_hda_codec_write_cache(codec, nid, 0,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100766 AC_VERB_SET_CONNECT_SEL,
767 path->idx[i]);
768 if (has_amp_in(codec, path, i))
769 activate_amp_in(codec, path, i, enable, add_aamix);
770 if (has_amp_out(codec, path, i))
771 activate_amp_out(codec, path, i, enable);
772 }
773
774 if (enable)
775 path->active = true;
776}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100777EXPORT_SYMBOL_GPL(snd_hda_activate_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100778
Takashi Iwai55196ff2013-01-24 17:32:56 +0100779/* if the given path is inactive, put widgets into D3 (only if suitable) */
780static void path_power_down_sync(struct hda_codec *codec, struct nid_path *path)
781{
782 struct hda_gen_spec *spec = codec->spec;
Jiri Slaby868211d2013-04-04 22:32:10 +0200783 bool changed = false;
Takashi Iwai55196ff2013-01-24 17:32:56 +0100784 int i;
785
786 if (!spec->power_down_unused || path->active)
787 return;
788
789 for (i = 0; i < path->depth; i++) {
790 hda_nid_t nid = path->path[i];
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +0200791 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D3) &&
792 !is_active_nid_for_any(codec, nid)) {
Takashi Iwai55196ff2013-01-24 17:32:56 +0100793 snd_hda_codec_write(codec, nid, 0,
794 AC_VERB_SET_POWER_STATE,
795 AC_PWRST_D3);
796 changed = true;
797 }
798 }
799
800 if (changed) {
801 msleep(10);
802 snd_hda_codec_read(codec, path->path[0], 0,
803 AC_VERB_GET_POWER_STATE, 0);
804 }
805}
806
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100807/* turn on/off EAPD on the given pin */
808static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
809{
810 struct hda_gen_spec *spec = codec->spec;
811 if (spec->own_eapd_ctl ||
812 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
813 return;
Takashi Iwai05909d5c2013-05-31 19:55:54 +0200814 if (spec->keep_eapd_on && !enable)
815 return;
Takashi Iwai468ac412013-11-12 11:36:00 +0100816 if (codec->inv_eapd)
817 enable = !enable;
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100818 snd_hda_codec_update_cache(codec, pin, 0,
819 AC_VERB_SET_EAPD_BTLENABLE,
820 enable ? 0x02 : 0x00);
821}
822
Takashi Iwai3e367f12013-01-23 17:07:23 +0100823/* re-initialize the path specified by the given path index */
824static void resume_path_from_idx(struct hda_codec *codec, int path_idx)
825{
826 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
827 if (path)
828 snd_hda_activate_path(codec, path, path->active, false);
829}
830
Takashi Iwai352f7f92012-12-19 12:52:06 +0100831
832/*
833 * Helper functions for creating mixer ctl elements
834 */
835
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200836static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
837 struct snd_ctl_elem_value *ucontrol);
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200838static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
839 struct snd_ctl_elem_value *ucontrol);
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200840
Takashi Iwai352f7f92012-12-19 12:52:06 +0100841enum {
842 HDA_CTL_WIDGET_VOL,
843 HDA_CTL_WIDGET_MUTE,
844 HDA_CTL_BIND_MUTE,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100845};
846static const struct snd_kcontrol_new control_templates[] = {
847 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200848 /* only the put callback is replaced for handling the special mute */
849 {
850 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
851 .subdevice = HDA_SUBDEV_AMP_FLAG,
852 .info = snd_hda_mixer_amp_switch_info,
853 .get = snd_hda_mixer_amp_switch_get,
854 .put = hda_gen_mixer_mute_put, /* replaced */
855 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
856 },
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200857 {
858 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
859 .info = snd_hda_mixer_amp_switch_info,
860 .get = snd_hda_mixer_bind_switch_get,
861 .put = hda_gen_bind_mute_put, /* replaced */
862 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
863 },
Takashi Iwai352f7f92012-12-19 12:52:06 +0100864};
865
866/* add dynamic controls from template */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100867static struct snd_kcontrol_new *
868add_control(struct hda_gen_spec *spec, int type, const char *name,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100869 int cidx, unsigned long val)
870{
871 struct snd_kcontrol_new *knew;
872
Takashi Iwai12c93df2012-12-19 14:38:33 +0100873 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100874 if (!knew)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100875 return NULL;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100876 knew->index = cidx;
877 if (get_amp_nid_(val))
878 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
879 knew->private_value = val;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100880 return knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100881}
882
883static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
884 const char *pfx, const char *dir,
885 const char *sfx, int cidx, unsigned long val)
886{
Takashi Iwai975cc022013-06-28 11:56:49 +0200887 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai352f7f92012-12-19 12:52:06 +0100888 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100889 if (!add_control(spec, type, name, cidx, val))
890 return -ENOMEM;
891 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100892}
893
894#define add_pb_vol_ctrl(spec, type, pfx, val) \
895 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
896#define add_pb_sw_ctrl(spec, type, pfx, val) \
897 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
898#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
899 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
900#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
901 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
902
903static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
904 unsigned int chs, struct nid_path *path)
905{
906 unsigned int val;
907 if (!path)
908 return 0;
909 val = path->ctls[NID_PATH_VOL_CTL];
910 if (!val)
911 return 0;
912 val = amp_val_replace_channels(val, chs);
913 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
914}
915
916/* return the channel bits suitable for the given path->ctls[] */
917static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
918 int type)
919{
920 int chs = 1; /* mono (left only) */
921 if (path) {
922 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
923 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
924 chs = 3; /* stereo */
925 }
926 return chs;
927}
928
929static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
930 struct nid_path *path)
931{
932 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
933 return add_vol_ctl(codec, pfx, cidx, chs, path);
934}
935
936/* create a mute-switch for the given mixer widget;
937 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
938 */
939static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
940 unsigned int chs, struct nid_path *path)
941{
942 unsigned int val;
943 int type = HDA_CTL_WIDGET_MUTE;
944
945 if (!path)
946 return 0;
947 val = path->ctls[NID_PATH_MUTE_CTL];
948 if (!val)
949 return 0;
950 val = amp_val_replace_channels(val, chs);
951 if (get_amp_direction_(val) == HDA_INPUT) {
952 hda_nid_t nid = get_amp_nid_(val);
953 int nums = snd_hda_get_num_conns(codec, nid);
954 if (nums > 1) {
955 type = HDA_CTL_BIND_MUTE;
956 val |= nums << 19;
957 }
958 }
959 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
960}
961
962static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
963 int cidx, struct nid_path *path)
964{
965 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
966 return add_sw_ctl(codec, pfx, cidx, chs, path);
967}
968
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200969/* playback mute control with the software mute bit check */
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200970static void sync_auto_mute_bits(struct snd_kcontrol *kcontrol,
971 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200972{
973 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
974 struct hda_gen_spec *spec = codec->spec;
975
976 if (spec->auto_mute_via_amp) {
977 hda_nid_t nid = get_amp_nid(kcontrol);
978 bool enabled = !((spec->mute_bits >> nid) & 1);
979 ucontrol->value.integer.value[0] &= enabled;
980 ucontrol->value.integer.value[1] &= enabled;
981 }
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200982}
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200983
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200984static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
985 struct snd_ctl_elem_value *ucontrol)
986{
987 sync_auto_mute_bits(kcontrol, ucontrol);
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200988 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
989}
990
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200991static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
992 struct snd_ctl_elem_value *ucontrol)
993{
994 sync_auto_mute_bits(kcontrol, ucontrol);
995 return snd_hda_mixer_bind_switch_put(kcontrol, ucontrol);
996}
997
Takashi Iwai247d85e2013-01-17 16:18:11 +0100998/* any ctl assigned to the path with the given index? */
999static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
1000{
1001 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
1002 return path && path->ctls[ctl_type];
1003}
1004
Takashi Iwai352f7f92012-12-19 12:52:06 +01001005static const char * const channel_name[4] = {
1006 "Front", "Surround", "CLFE", "Side"
1007};
1008
1009/* give some appropriate ctl name prefix for the given line out channel */
Takashi Iwai247d85e2013-01-17 16:18:11 +01001010static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
1011 int *index, int ctl_type)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001012{
Takashi Iwai247d85e2013-01-17 16:18:11 +01001013 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001014 struct auto_pin_cfg *cfg = &spec->autocfg;
1015
1016 *index = 0;
1017 if (cfg->line_outs == 1 && !spec->multi_ios &&
Takashi Iwai247d85e2013-01-17 16:18:11 +01001018 !cfg->hp_outs && !cfg->speaker_outs)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001019 return spec->vmaster_mute.hook ? "PCM" : "Master";
1020
1021 /* if there is really a single DAC used in the whole output paths,
1022 * use it master (or "PCM" if a vmaster hook is present)
1023 */
1024 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
1025 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
1026 return spec->vmaster_mute.hook ? "PCM" : "Master";
1027
Takashi Iwai247d85e2013-01-17 16:18:11 +01001028 /* multi-io channels */
1029 if (ch >= cfg->line_outs)
1030 return channel_name[ch];
1031
Takashi Iwai352f7f92012-12-19 12:52:06 +01001032 switch (cfg->line_out_type) {
1033 case AUTO_PIN_SPEAKER_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +01001034 /* if the primary channel vol/mute is shared with HP volume,
1035 * don't name it as Speaker
1036 */
1037 if (!ch && cfg->hp_outs &&
1038 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
1039 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001040 if (cfg->line_outs == 1)
1041 return "Speaker";
1042 if (cfg->line_outs == 2)
1043 return ch ? "Bass Speaker" : "Speaker";
1044 break;
1045 case AUTO_PIN_HP_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +01001046 /* if the primary channel vol/mute is shared with spk volume,
1047 * don't name it as Headphone
1048 */
1049 if (!ch && cfg->speaker_outs &&
1050 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
1051 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001052 /* for multi-io case, only the primary out */
1053 if (ch && spec->multi_ios)
1054 break;
1055 *index = ch;
1056 return "Headphone";
Takashi Iwai352f7f92012-12-19 12:52:06 +01001057 }
Takashi Iwai247d85e2013-01-17 16:18:11 +01001058
1059 /* for a single channel output, we don't have to name the channel */
1060 if (cfg->line_outs == 1 && !spec->multi_ios)
1061 return "PCM";
1062
Takashi Iwai352f7f92012-12-19 12:52:06 +01001063 if (ch >= ARRAY_SIZE(channel_name)) {
1064 snd_BUG();
1065 return "PCM";
1066 }
1067
1068 return channel_name[ch];
1069}
1070
1071/*
1072 * Parse output paths
1073 */
1074
1075/* badness definition */
1076enum {
1077 /* No primary DAC is found for the main output */
1078 BAD_NO_PRIMARY_DAC = 0x10000,
1079 /* No DAC is found for the extra output */
1080 BAD_NO_DAC = 0x4000,
1081 /* No possible multi-ios */
Takashi Iwai1d739062013-02-13 14:17:32 +01001082 BAD_MULTI_IO = 0x120,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001083 /* No individual DAC for extra output */
1084 BAD_NO_EXTRA_DAC = 0x102,
1085 /* No individual DAC for extra surrounds */
1086 BAD_NO_EXTRA_SURR_DAC = 0x101,
1087 /* Primary DAC shared with main surrounds */
1088 BAD_SHARED_SURROUND = 0x100,
Takashi Iwai55a63d42013-03-21 17:20:12 +01001089 /* No independent HP possible */
Takashi Iwaibec8e682013-03-22 15:10:08 +01001090 BAD_NO_INDEP_HP = 0x10,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001091 /* Primary DAC shared with main CLFE */
1092 BAD_SHARED_CLFE = 0x10,
1093 /* Primary DAC shared with extra surrounds */
1094 BAD_SHARED_EXTRA_SURROUND = 0x10,
1095 /* Volume widget is shared */
1096 BAD_SHARED_VOL = 0x10,
1097};
1098
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001099/* look for widgets in the given path which are appropriate for
Takashi Iwai352f7f92012-12-19 12:52:06 +01001100 * volume and mute controls, and assign the values to ctls[].
1101 *
1102 * When no appropriate widget is found in the path, the badness value
1103 * is incremented depending on the situation. The function returns the
1104 * total badness for both volume and mute controls.
1105 */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001106static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001107{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001108 hda_nid_t nid;
1109 unsigned int val;
1110 int badness = 0;
1111
1112 if (!path)
1113 return BAD_SHARED_VOL * 2;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001114
1115 if (path->ctls[NID_PATH_VOL_CTL] ||
1116 path->ctls[NID_PATH_MUTE_CTL])
1117 return 0; /* already evaluated */
1118
Takashi Iwai352f7f92012-12-19 12:52:06 +01001119 nid = look_for_out_vol_nid(codec, path);
1120 if (nid) {
1121 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1122 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1123 badness += BAD_SHARED_VOL;
1124 else
1125 path->ctls[NID_PATH_VOL_CTL] = val;
1126 } else
1127 badness += BAD_SHARED_VOL;
1128 nid = look_for_out_mute_nid(codec, path);
1129 if (nid) {
1130 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1131 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1132 nid_has_mute(codec, nid, HDA_OUTPUT))
1133 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1134 else
1135 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1136 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1137 badness += BAD_SHARED_VOL;
1138 else
1139 path->ctls[NID_PATH_MUTE_CTL] = val;
1140 } else
1141 badness += BAD_SHARED_VOL;
1142 return badness;
1143}
1144
Takashi Iwai98bd1112013-03-22 14:53:50 +01001145const struct badness_table hda_main_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001146 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1147 .no_dac = BAD_NO_DAC,
1148 .shared_primary = BAD_NO_PRIMARY_DAC,
1149 .shared_surr = BAD_SHARED_SURROUND,
1150 .shared_clfe = BAD_SHARED_CLFE,
1151 .shared_surr_main = BAD_SHARED_SURROUND,
1152};
Takashi Iwai2698ea92013-12-18 07:45:52 +01001153EXPORT_SYMBOL_GPL(hda_main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001154
Takashi Iwai98bd1112013-03-22 14:53:50 +01001155const struct badness_table hda_extra_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001156 .no_primary_dac = BAD_NO_DAC,
1157 .no_dac = BAD_NO_DAC,
1158 .shared_primary = BAD_NO_EXTRA_DAC,
1159 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1160 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1161 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1162};
Takashi Iwai2698ea92013-12-18 07:45:52 +01001163EXPORT_SYMBOL_GPL(hda_extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001164
Takashi Iwai7385df62013-01-07 09:50:52 +01001165/* get the DAC of the primary output corresponding to the given array index */
1166static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1167{
1168 struct hda_gen_spec *spec = codec->spec;
1169 struct auto_pin_cfg *cfg = &spec->autocfg;
1170
1171 if (cfg->line_outs > idx)
1172 return spec->private_dac_nids[idx];
1173 idx -= cfg->line_outs;
1174 if (spec->multi_ios > idx)
1175 return spec->multi_io[idx].dac;
1176 return 0;
1177}
1178
1179/* return the DAC if it's reachable, otherwise zero */
1180static inline hda_nid_t try_dac(struct hda_codec *codec,
1181 hda_nid_t dac, hda_nid_t pin)
1182{
1183 return is_reachable_path(codec, dac, pin) ? dac : 0;
1184}
1185
Takashi Iwai352f7f92012-12-19 12:52:06 +01001186/* try to assign DACs to pins and return the resultant badness */
1187static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1188 const hda_nid_t *pins, hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001189 int *path_idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001190 const struct badness_table *bad)
1191{
1192 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001193 int i, j;
1194 int badness = 0;
1195 hda_nid_t dac;
1196
1197 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 return 0;
1199
Takashi Iwai352f7f92012-12-19 12:52:06 +01001200 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001201 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001202 hda_nid_t pin = pins[i];
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001203
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001204 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1205 if (path) {
1206 badness += assign_out_path_ctls(codec, path);
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001207 continue;
1208 }
1209
Takashi Iwai36907392013-12-10 17:29:26 +01001210 dacs[i] = get_preferred_dac(codec, pin);
1211 if (dacs[i]) {
1212 if (is_dac_already_used(codec, dacs[i]))
1213 badness += bad->shared_primary;
1214 }
1215
1216 if (!dacs[i])
1217 dacs[i] = look_for_dac(codec, pin, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001218 if (!dacs[i] && !i) {
Takashi Iwai980428c2013-01-09 09:28:20 +01001219 /* try to steal the DAC of surrounds for the front */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001220 for (j = 1; j < num_outs; j++) {
1221 if (is_reachable_path(codec, dacs[j], pin)) {
1222 dacs[0] = dacs[j];
1223 dacs[j] = 0;
Takashi Iwai980428c2013-01-09 09:28:20 +01001224 invalidate_nid_path(codec, path_idx[j]);
Takashi Iwai196c17662013-01-04 15:01:40 +01001225 path_idx[j] = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001226 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 }
1228 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001229 }
1230 dac = dacs[i];
1231 if (!dac) {
Takashi Iwai7385df62013-01-07 09:50:52 +01001232 if (num_outs > 2)
1233 dac = try_dac(codec, get_primary_out(codec, i), pin);
1234 if (!dac)
1235 dac = try_dac(codec, dacs[0], pin);
1236 if (!dac)
1237 dac = try_dac(codec, get_primary_out(codec, i), pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001238 if (dac) {
1239 if (!i)
1240 badness += bad->shared_primary;
1241 else if (i == 1)
1242 badness += bad->shared_surr;
1243 else
1244 badness += bad->shared_clfe;
1245 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1246 dac = spec->private_dac_nids[0];
1247 badness += bad->shared_surr_main;
1248 } else if (!i)
1249 badness += bad->no_primary_dac;
1250 else
1251 badness += bad->no_dac;
1252 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001253 if (!dac)
1254 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001255 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001256 if (!path && !i && spec->mixer_nid) {
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001257 /* try with aamix */
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001258 path = snd_hda_add_new_path(codec, dac, pin, 0);
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001259 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001260 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001261 dac = dacs[i] = 0;
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001262 badness += bad->no_dac;
1263 } else {
Takashi Iwaia7694092013-01-21 10:43:18 +01001264 /* print_nid_path("output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001265 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001266 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001267 badness += assign_out_path_ctls(codec, path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001268 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001269 }
1270
1271 return badness;
1272}
1273
1274/* return NID if the given pin has only a single connection to a certain DAC */
1275static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1276{
1277 struct hda_gen_spec *spec = codec->spec;
1278 int i;
1279 hda_nid_t nid_found = 0;
1280
1281 for (i = 0; i < spec->num_all_dacs; i++) {
1282 hda_nid_t nid = spec->all_dacs[i];
1283 if (!nid || is_dac_already_used(codec, nid))
1284 continue;
1285 if (is_reachable_path(codec, nid, pin)) {
1286 if (nid_found)
1287 return 0;
1288 nid_found = nid;
1289 }
1290 }
1291 return nid_found;
1292}
1293
1294/* check whether the given pin can be a multi-io pin */
1295static bool can_be_multiio_pin(struct hda_codec *codec,
1296 unsigned int location, hda_nid_t nid)
1297{
1298 unsigned int defcfg, caps;
1299
1300 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1301 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1302 return false;
1303 if (location && get_defcfg_location(defcfg) != location)
1304 return false;
1305 caps = snd_hda_query_pin_caps(codec, nid);
1306 if (!(caps & AC_PINCAP_OUT))
1307 return false;
1308 return true;
1309}
1310
Takashi Iwaie22aab72013-01-04 14:50:04 +01001311/* count the number of input pins that are capable to be multi-io */
1312static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1313{
1314 struct hda_gen_spec *spec = codec->spec;
1315 struct auto_pin_cfg *cfg = &spec->autocfg;
1316 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1317 unsigned int location = get_defcfg_location(defcfg);
1318 int type, i;
1319 int num_pins = 0;
1320
1321 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1322 for (i = 0; i < cfg->num_inputs; i++) {
1323 if (cfg->inputs[i].type != type)
1324 continue;
1325 if (can_be_multiio_pin(codec, location,
1326 cfg->inputs[i].pin))
1327 num_pins++;
1328 }
1329 }
1330 return num_pins;
1331}
1332
Takashi Iwai352f7f92012-12-19 12:52:06 +01001333/*
1334 * multi-io helper
1335 *
1336 * When hardwired is set, try to fill ony hardwired pins, and returns
1337 * zero if any pins are filled, non-zero if nothing found.
1338 * When hardwired is off, try to fill possible input pins, and returns
1339 * the badness value.
1340 */
1341static int fill_multi_ios(struct hda_codec *codec,
1342 hda_nid_t reference_pin,
Takashi Iwaie22aab72013-01-04 14:50:04 +01001343 bool hardwired)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001344{
1345 struct hda_gen_spec *spec = codec->spec;
1346 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie22aab72013-01-04 14:50:04 +01001347 int type, i, j, num_pins, old_pins;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001348 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1349 unsigned int location = get_defcfg_location(defcfg);
1350 int badness = 0;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001351 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001352
1353 old_pins = spec->multi_ios;
1354 if (old_pins >= 2)
1355 goto end_fill;
1356
Takashi Iwaie22aab72013-01-04 14:50:04 +01001357 num_pins = count_multiio_pins(codec, reference_pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001358 if (num_pins < 2)
1359 goto end_fill;
1360
Takashi Iwai352f7f92012-12-19 12:52:06 +01001361 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1362 for (i = 0; i < cfg->num_inputs; i++) {
1363 hda_nid_t nid = cfg->inputs[i].pin;
1364 hda_nid_t dac = 0;
1365
1366 if (cfg->inputs[i].type != type)
1367 continue;
1368 if (!can_be_multiio_pin(codec, location, nid))
1369 continue;
1370 for (j = 0; j < spec->multi_ios; j++) {
1371 if (nid == spec->multi_io[j].pin)
1372 break;
1373 }
1374 if (j < spec->multi_ios)
1375 continue;
1376
Takashi Iwai352f7f92012-12-19 12:52:06 +01001377 if (hardwired)
1378 dac = get_dac_if_single(codec, nid);
1379 else if (!dac)
1380 dac = look_for_dac(codec, nid, false);
1381 if (!dac) {
1382 badness++;
1383 continue;
1384 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001385 path = snd_hda_add_new_path(codec, dac, nid,
1386 -spec->mixer_nid);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001387 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001388 badness++;
1389 continue;
1390 }
Takashi Iwaia7694092013-01-21 10:43:18 +01001391 /* print_nid_path("multiio", path); */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001392 spec->multi_io[spec->multi_ios].pin = nid;
1393 spec->multi_io[spec->multi_ios].dac = dac;
Takashi Iwai196c17662013-01-04 15:01:40 +01001394 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1395 snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001396 spec->multi_ios++;
1397 if (spec->multi_ios >= 2)
1398 break;
1399 }
1400 }
1401 end_fill:
1402 if (badness)
1403 badness = BAD_MULTI_IO;
1404 if (old_pins == spec->multi_ios) {
1405 if (hardwired)
1406 return 1; /* nothing found */
1407 else
1408 return badness; /* no badness if nothing found */
1409 }
1410 if (!hardwired && spec->multi_ios < 2) {
1411 /* cancel newly assigned paths */
1412 spec->paths.used -= spec->multi_ios - old_pins;
1413 spec->multi_ios = old_pins;
1414 return badness;
1415 }
1416
1417 /* assign volume and mute controls */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001418 for (i = old_pins; i < spec->multi_ios; i++) {
1419 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1420 badness += assign_out_path_ctls(codec, path);
1421 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001422
1423 return badness;
1424}
1425
1426/* map DACs for all pins in the list if they are single connections */
1427static bool map_singles(struct hda_codec *codec, int outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001428 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001429{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001430 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001431 int i;
1432 bool found = false;
1433 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001434 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001435 hda_nid_t dac;
1436 if (dacs[i])
1437 continue;
1438 dac = get_dac_if_single(codec, pins[i]);
1439 if (!dac)
1440 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001441 path = snd_hda_add_new_path(codec, dac, pins[i],
1442 -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001443 if (!path && !i && spec->mixer_nid)
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001444 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001445 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001446 dacs[i] = dac;
1447 found = true;
Takashi Iwaia7694092013-01-21 10:43:18 +01001448 /* print_nid_path("output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001449 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001450 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001451 }
1452 }
1453 return found;
1454}
1455
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001456/* create a new path including aamix if available, and return its index */
1457static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1458{
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001459 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001460 struct nid_path *path;
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001461 hda_nid_t path_dac, dac, pin;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001462
1463 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001464 if (!path || !path->depth ||
1465 is_nid_contained(path, spec->mixer_nid))
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001466 return 0;
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001467 path_dac = path->path[0];
1468 dac = spec->private_dac_nids[0];
Takashi Iwaif87498b2013-01-21 14:24:31 +01001469 pin = path->path[path->depth - 1];
1470 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1471 if (!path) {
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001472 if (dac != path_dac)
1473 dac = path_dac;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001474 else if (spec->multiout.hp_out_nid[0])
1475 dac = spec->multiout.hp_out_nid[0];
1476 else if (spec->multiout.extra_out_nid[0])
1477 dac = spec->multiout.extra_out_nid[0];
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001478 else
1479 dac = 0;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001480 if (dac)
1481 path = snd_hda_add_new_path(codec, dac, pin,
1482 spec->mixer_nid);
1483 }
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001484 if (!path)
1485 return 0;
Takashi Iwaia7694092013-01-21 10:43:18 +01001486 /* print_nid_path("output-aamix", path); */
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001487 path->active = false; /* unused as default */
1488 return snd_hda_get_path_idx(codec, path);
1489}
1490
Takashi Iwai55a63d42013-03-21 17:20:12 +01001491/* check whether the independent HP is available with the current config */
1492static bool indep_hp_possible(struct hda_codec *codec)
1493{
1494 struct hda_gen_spec *spec = codec->spec;
1495 struct auto_pin_cfg *cfg = &spec->autocfg;
1496 struct nid_path *path;
1497 int i, idx;
1498
1499 if (cfg->line_out_type == AUTO_PIN_HP_OUT)
1500 idx = spec->out_paths[0];
1501 else
1502 idx = spec->hp_paths[0];
1503 path = snd_hda_get_path_from_idx(codec, idx);
1504 if (!path)
1505 return false;
1506
1507 /* assume no path conflicts unless aamix is involved */
1508 if (!spec->mixer_nid || !is_nid_contained(path, spec->mixer_nid))
1509 return true;
1510
1511 /* check whether output paths contain aamix */
1512 for (i = 0; i < cfg->line_outs; i++) {
1513 if (spec->out_paths[i] == idx)
1514 break;
1515 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1516 if (path && is_nid_contained(path, spec->mixer_nid))
1517 return false;
1518 }
1519 for (i = 0; i < cfg->speaker_outs; i++) {
1520 path = snd_hda_get_path_from_idx(codec, spec->speaker_paths[i]);
1521 if (path && is_nid_contained(path, spec->mixer_nid))
1522 return false;
1523 }
1524
1525 return true;
1526}
1527
Takashi Iwaia07a9492013-01-07 16:44:06 +01001528/* fill the empty entries in the dac array for speaker/hp with the
1529 * shared dac pointed by the paths
1530 */
1531static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1532 hda_nid_t *dacs, int *path_idx)
1533{
1534 struct nid_path *path;
1535 int i;
1536
1537 for (i = 0; i < num_outs; i++) {
1538 if (dacs[i])
1539 continue;
1540 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1541 if (!path)
1542 continue;
1543 dacs[i] = path->path[0];
1544 }
1545}
1546
Takashi Iwai352f7f92012-12-19 12:52:06 +01001547/* fill in the dac_nids table from the parsed pin configuration */
1548static int fill_and_eval_dacs(struct hda_codec *codec,
1549 bool fill_hardwired,
1550 bool fill_mio_first)
1551{
1552 struct hda_gen_spec *spec = codec->spec;
1553 struct auto_pin_cfg *cfg = &spec->autocfg;
1554 int i, err, badness;
1555
1556 /* set num_dacs once to full for look_for_dac() */
1557 spec->multiout.num_dacs = cfg->line_outs;
1558 spec->multiout.dac_nids = spec->private_dac_nids;
1559 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1560 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1561 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1562 spec->multi_ios = 0;
1563 snd_array_free(&spec->paths);
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001564
1565 /* clear path indices */
1566 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1567 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1568 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1569 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1570 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
Takashi Iwaic697b712013-01-07 17:09:26 +01001571 memset(spec->input_paths, 0, sizeof(spec->input_paths));
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001572 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1573 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1574
Takashi Iwai352f7f92012-12-19 12:52:06 +01001575 badness = 0;
1576
1577 /* fill hard-wired DACs first */
1578 if (fill_hardwired) {
1579 bool mapped;
1580 do {
1581 mapped = map_singles(codec, cfg->line_outs,
1582 cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001583 spec->private_dac_nids,
1584 spec->out_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001585 mapped |= map_singles(codec, cfg->hp_outs,
1586 cfg->hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001587 spec->multiout.hp_out_nid,
1588 spec->hp_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001589 mapped |= map_singles(codec, cfg->speaker_outs,
1590 cfg->speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001591 spec->multiout.extra_out_nid,
1592 spec->speaker_paths);
Takashi Iwaida96fb52013-07-29 16:54:36 +02001593 if (!spec->no_multi_io &&
1594 fill_mio_first && cfg->line_outs == 1 &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01001595 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001596 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001597 if (!err)
1598 mapped = true;
1599 }
1600 } while (mapped);
1601 }
1602
1603 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001604 spec->private_dac_nids, spec->out_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001605 spec->main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001606
Takashi Iwaida96fb52013-07-29 16:54:36 +02001607 if (!spec->no_multi_io && fill_mio_first &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01001608 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1609 /* try to fill multi-io first */
Takashi Iwaie22aab72013-01-04 14:50:04 +01001610 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001611 if (err < 0)
1612 return err;
1613 /* we don't count badness at this stage yet */
1614 }
1615
1616 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1617 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1618 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001619 spec->hp_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001620 spec->extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001621 if (err < 0)
1622 return err;
1623 badness += err;
1624 }
1625 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1626 err = try_assign_dacs(codec, cfg->speaker_outs,
1627 cfg->speaker_pins,
1628 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001629 spec->speaker_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001630 spec->extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001631 if (err < 0)
1632 return err;
1633 badness += err;
1634 }
Takashi Iwaida96fb52013-07-29 16:54:36 +02001635 if (!spec->no_multi_io &&
1636 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001637 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001638 if (err < 0)
1639 return err;
1640 badness += err;
1641 }
Takashi Iwaie22aab72013-01-04 14:50:04 +01001642
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001643 if (spec->mixer_nid) {
1644 spec->aamix_out_paths[0] =
1645 check_aamix_out_path(codec, spec->out_paths[0]);
1646 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1647 spec->aamix_out_paths[1] =
1648 check_aamix_out_path(codec, spec->hp_paths[0]);
1649 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1650 spec->aamix_out_paths[2] =
1651 check_aamix_out_path(codec, spec->speaker_paths[0]);
1652 }
1653
Takashi Iwaida96fb52013-07-29 16:54:36 +02001654 if (!spec->no_multi_io &&
1655 cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
Takashi Iwaie22aab72013-01-04 14:50:04 +01001656 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1657 spec->multi_ios = 1; /* give badness */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001658
Takashi Iwaia07a9492013-01-07 16:44:06 +01001659 /* re-count num_dacs and squash invalid entries */
1660 spec->multiout.num_dacs = 0;
1661 for (i = 0; i < cfg->line_outs; i++) {
1662 if (spec->private_dac_nids[i])
1663 spec->multiout.num_dacs++;
1664 else {
1665 memmove(spec->private_dac_nids + i,
1666 spec->private_dac_nids + i + 1,
1667 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1668 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1669 }
1670 }
1671
1672 spec->ext_channel_count = spec->min_channel_count =
David Henningssonc0f3b212013-01-16 11:45:37 +01001673 spec->multiout.num_dacs * 2;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001674
Takashi Iwai352f7f92012-12-19 12:52:06 +01001675 if (spec->multi_ios == 2) {
1676 for (i = 0; i < 2; i++)
1677 spec->private_dac_nids[spec->multiout.num_dacs++] =
1678 spec->multi_io[i].dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001679 } else if (spec->multi_ios) {
1680 spec->multi_ios = 0;
1681 badness += BAD_MULTI_IO;
1682 }
1683
Takashi Iwai55a63d42013-03-21 17:20:12 +01001684 if (spec->indep_hp && !indep_hp_possible(codec))
1685 badness += BAD_NO_INDEP_HP;
1686
Takashi Iwaia07a9492013-01-07 16:44:06 +01001687 /* re-fill the shared DAC for speaker / headphone */
1688 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1689 refill_shared_dacs(codec, cfg->hp_outs,
1690 spec->multiout.hp_out_nid,
1691 spec->hp_paths);
1692 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1693 refill_shared_dacs(codec, cfg->speaker_outs,
1694 spec->multiout.extra_out_nid,
1695 spec->speaker_paths);
1696
Takashi Iwai352f7f92012-12-19 12:52:06 +01001697 return badness;
1698}
1699
1700#define DEBUG_BADNESS
1701
1702#ifdef DEBUG_BADNESS
1703#define debug_badness snd_printdd
1704#else
1705#define debug_badness(...)
1706#endif
1707
Takashi Iwaia7694092013-01-21 10:43:18 +01001708#ifdef DEBUG_BADNESS
1709static inline void print_nid_path_idx(struct hda_codec *codec,
1710 const char *pfx, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001711{
Takashi Iwaia7694092013-01-21 10:43:18 +01001712 struct nid_path *path;
1713
1714 path = snd_hda_get_path_from_idx(codec, idx);
1715 if (path)
1716 print_nid_path(pfx, path);
1717}
1718
1719static void debug_show_configs(struct hda_codec *codec,
1720 struct auto_pin_cfg *cfg)
1721{
1722 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia7694092013-01-21 10:43:18 +01001723 static const char * const lo_type[3] = { "LO", "SP", "HP" };
Takashi Iwaia7694092013-01-21 10:43:18 +01001724 int i;
1725
1726 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001727 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001728 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001729 spec->multiout.dac_nids[0],
1730 spec->multiout.dac_nids[1],
1731 spec->multiout.dac_nids[2],
Takashi Iwaia7694092013-01-21 10:43:18 +01001732 spec->multiout.dac_nids[3],
1733 lo_type[cfg->line_out_type]);
1734 for (i = 0; i < cfg->line_outs; i++)
1735 print_nid_path_idx(codec, " out", spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001736 if (spec->multi_ios > 0)
1737 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1738 spec->multi_ios,
1739 spec->multi_io[0].pin, spec->multi_io[1].pin,
1740 spec->multi_io[0].dac, spec->multi_io[1].dac);
Takashi Iwaia7694092013-01-21 10:43:18 +01001741 for (i = 0; i < spec->multi_ios; i++)
1742 print_nid_path_idx(codec, " mio",
1743 spec->out_paths[cfg->line_outs + i]);
1744 if (cfg->hp_outs)
1745 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001746 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001747 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001748 spec->multiout.hp_out_nid[0],
1749 spec->multiout.hp_out_nid[1],
1750 spec->multiout.hp_out_nid[2],
1751 spec->multiout.hp_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001752 for (i = 0; i < cfg->hp_outs; i++)
1753 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1754 if (cfg->speaker_outs)
1755 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001756 cfg->speaker_pins[0], cfg->speaker_pins[1],
1757 cfg->speaker_pins[2], cfg->speaker_pins[3],
1758 spec->multiout.extra_out_nid[0],
1759 spec->multiout.extra_out_nid[1],
1760 spec->multiout.extra_out_nid[2],
1761 spec->multiout.extra_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001762 for (i = 0; i < cfg->speaker_outs; i++)
1763 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1764 for (i = 0; i < 3; i++)
1765 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001766}
Takashi Iwaia7694092013-01-21 10:43:18 +01001767#else
1768#define debug_show_configs(codec, cfg) /* NOP */
1769#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01001770
1771/* find all available DACs of the codec */
1772static void fill_all_dac_nids(struct hda_codec *codec)
1773{
1774 struct hda_gen_spec *spec = codec->spec;
1775 int i;
1776 hda_nid_t nid = codec->start_nid;
1777
1778 spec->num_all_dacs = 0;
1779 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1780 for (i = 0; i < codec->num_nodes; i++, nid++) {
1781 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1782 continue;
1783 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1784 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1785 break;
1786 }
1787 spec->all_dacs[spec->num_all_dacs++] = nid;
1788 }
1789}
1790
1791static int parse_output_paths(struct hda_codec *codec)
1792{
1793 struct hda_gen_spec *spec = codec->spec;
1794 struct auto_pin_cfg *cfg = &spec->autocfg;
1795 struct auto_pin_cfg *best_cfg;
Takashi Iwai9314a582013-01-21 10:49:05 +01001796 unsigned int val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001797 int best_badness = INT_MAX;
1798 int badness;
1799 bool fill_hardwired = true, fill_mio_first = true;
1800 bool best_wired = true, best_mio = true;
1801 bool hp_spk_swapped = false;
1802
Takashi Iwai352f7f92012-12-19 12:52:06 +01001803 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1804 if (!best_cfg)
1805 return -ENOMEM;
1806 *best_cfg = *cfg;
1807
1808 for (;;) {
1809 badness = fill_and_eval_dacs(codec, fill_hardwired,
1810 fill_mio_first);
1811 if (badness < 0) {
1812 kfree(best_cfg);
1813 return badness;
1814 }
1815 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1816 cfg->line_out_type, fill_hardwired, fill_mio_first,
1817 badness);
Takashi Iwaia7694092013-01-21 10:43:18 +01001818 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001819 if (badness < best_badness) {
1820 best_badness = badness;
1821 *best_cfg = *cfg;
1822 best_wired = fill_hardwired;
1823 best_mio = fill_mio_first;
1824 }
1825 if (!badness)
1826 break;
1827 fill_mio_first = !fill_mio_first;
1828 if (!fill_mio_first)
1829 continue;
1830 fill_hardwired = !fill_hardwired;
1831 if (!fill_hardwired)
1832 continue;
1833 if (hp_spk_swapped)
1834 break;
1835 hp_spk_swapped = true;
1836 if (cfg->speaker_outs > 0 &&
1837 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1838 cfg->hp_outs = cfg->line_outs;
1839 memcpy(cfg->hp_pins, cfg->line_out_pins,
1840 sizeof(cfg->hp_pins));
1841 cfg->line_outs = cfg->speaker_outs;
1842 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1843 sizeof(cfg->speaker_pins));
1844 cfg->speaker_outs = 0;
1845 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1846 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1847 fill_hardwired = true;
1848 continue;
1849 }
1850 if (cfg->hp_outs > 0 &&
1851 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1852 cfg->speaker_outs = cfg->line_outs;
1853 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1854 sizeof(cfg->speaker_pins));
1855 cfg->line_outs = cfg->hp_outs;
1856 memcpy(cfg->line_out_pins, cfg->hp_pins,
1857 sizeof(cfg->hp_pins));
1858 cfg->hp_outs = 0;
1859 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1860 cfg->line_out_type = AUTO_PIN_HP_OUT;
1861 fill_hardwired = true;
1862 continue;
1863 }
1864 break;
1865 }
1866
1867 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001868 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001869 *cfg = *best_cfg;
1870 fill_and_eval_dacs(codec, best_wired, best_mio);
1871 }
1872 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1873 cfg->line_out_type, best_wired, best_mio);
Takashi Iwaia7694092013-01-21 10:43:18 +01001874 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001875
1876 if (cfg->line_out_pins[0]) {
1877 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01001878 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001879 if (path)
1880 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01001881 if (spec->vmaster_nid)
1882 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
1883 HDA_OUTPUT, spec->vmaster_tlv);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001884 }
1885
Takashi Iwai9314a582013-01-21 10:49:05 +01001886 /* set initial pinctl targets */
1887 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
1888 val = PIN_HP;
1889 else
1890 val = PIN_OUT;
1891 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
1892 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1893 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
1894 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1895 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
1896 set_pin_targets(codec, cfg->speaker_outs,
1897 cfg->speaker_pins, val);
1898 }
1899
Takashi Iwai55a63d42013-03-21 17:20:12 +01001900 /* clear indep_hp flag if not available */
1901 if (spec->indep_hp && !indep_hp_possible(codec))
1902 spec->indep_hp = 0;
1903
Takashi Iwai352f7f92012-12-19 12:52:06 +01001904 kfree(best_cfg);
1905 return 0;
1906}
1907
1908/* add playback controls from the parsed DAC table */
1909static int create_multi_out_ctls(struct hda_codec *codec,
1910 const struct auto_pin_cfg *cfg)
1911{
1912 struct hda_gen_spec *spec = codec->spec;
1913 int i, err, noutputs;
1914
1915 noutputs = cfg->line_outs;
1916 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1917 noutputs += spec->multi_ios;
1918
1919 for (i = 0; i < noutputs; i++) {
1920 const char *name;
1921 int index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001922 struct nid_path *path;
1923
Takashi Iwai196c17662013-01-04 15:01:40 +01001924 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001925 if (!path)
1926 continue;
Takashi Iwai247d85e2013-01-17 16:18:11 +01001927
1928 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001929 if (!name || !strcmp(name, "CLFE")) {
1930 /* Center/LFE */
1931 err = add_vol_ctl(codec, "Center", 0, 1, path);
1932 if (err < 0)
1933 return err;
1934 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1935 if (err < 0)
1936 return err;
Takashi Iwai247d85e2013-01-17 16:18:11 +01001937 } else {
1938 err = add_stereo_vol(codec, name, index, path);
1939 if (err < 0)
1940 return err;
1941 }
1942
1943 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
1944 if (!name || !strcmp(name, "CLFE")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001945 err = add_sw_ctl(codec, "Center", 0, 1, path);
1946 if (err < 0)
1947 return err;
1948 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1949 if (err < 0)
1950 return err;
1951 } else {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001952 err = add_stereo_sw(codec, name, index, path);
1953 if (err < 0)
1954 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 }
1956 }
1957 return 0;
1958}
1959
Takashi Iwaic2c80382013-01-07 10:33:57 +01001960static int create_extra_out(struct hda_codec *codec, int path_idx,
Takashi Iwai196c17662013-01-04 15:01:40 +01001961 const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001963 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 int err;
1965
Takashi Iwai196c17662013-01-04 15:01:40 +01001966 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001967 if (!path)
1968 return 0;
Takashi Iwaic2c80382013-01-07 10:33:57 +01001969 err = add_stereo_vol(codec, pfx, cidx, path);
1970 if (err < 0)
1971 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001972 err = add_stereo_sw(codec, pfx, cidx, path);
1973 if (err < 0)
1974 return err;
1975 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976}
1977
Takashi Iwai352f7f92012-12-19 12:52:06 +01001978/* add playback controls for speaker and HP outputs */
1979static int create_extra_outs(struct hda_codec *codec, int num_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001980 const int *paths, const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981{
Takashi Iwaic2c80382013-01-07 10:33:57 +01001982 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001983
1984 for (i = 0; i < num_pins; i++) {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001985 const char *name;
Takashi Iwai975cc022013-06-28 11:56:49 +02001986 char tmp[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwaic2c80382013-01-07 10:33:57 +01001987 int err, idx = 0;
1988
1989 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
1990 name = "Bass Speaker";
1991 else if (num_pins >= 3) {
1992 snprintf(tmp, sizeof(tmp), "%s %s",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001993 pfx, channel_name[i]);
Takashi Iwaic2c80382013-01-07 10:33:57 +01001994 name = tmp;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001995 } else {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001996 name = pfx;
1997 idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 }
Takashi Iwaic2c80382013-01-07 10:33:57 +01001999 err = create_extra_out(codec, paths[i], name, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002000 if (err < 0)
2001 return err;
2002 }
2003 return 0;
2004}
Takashi Iwai97ec5582006-03-21 11:29:07 +01002005
Takashi Iwai352f7f92012-12-19 12:52:06 +01002006static int create_hp_out_ctls(struct hda_codec *codec)
2007{
2008 struct hda_gen_spec *spec = codec->spec;
2009 return create_extra_outs(codec, spec->autocfg.hp_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01002010 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002011 "Headphone");
2012}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
Takashi Iwai352f7f92012-12-19 12:52:06 +01002014static int create_speaker_out_ctls(struct hda_codec *codec)
2015{
2016 struct hda_gen_spec *spec = codec->spec;
2017 return create_extra_outs(codec, spec->autocfg.speaker_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01002018 spec->speaker_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002019 "Speaker");
2020}
2021
2022/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002023 * independent HP controls
2024 */
2025
Takashi Iwai963afde2013-05-31 15:20:31 +02002026static void call_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002027static int indep_hp_info(struct snd_kcontrol *kcontrol,
2028 struct snd_ctl_elem_info *uinfo)
2029{
2030 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2031}
2032
2033static int indep_hp_get(struct snd_kcontrol *kcontrol,
2034 struct snd_ctl_elem_value *ucontrol)
2035{
2036 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2037 struct hda_gen_spec *spec = codec->spec;
2038 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
2039 return 0;
2040}
2041
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002042static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2043 int nomix_path_idx, int mix_path_idx,
2044 int out_type);
2045
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002046static int indep_hp_put(struct snd_kcontrol *kcontrol,
2047 struct snd_ctl_elem_value *ucontrol)
2048{
2049 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2050 struct hda_gen_spec *spec = codec->spec;
2051 unsigned int select = ucontrol->value.enumerated.item[0];
2052 int ret = 0;
2053
2054 mutex_lock(&spec->pcm_mutex);
2055 if (spec->active_streams) {
2056 ret = -EBUSY;
2057 goto unlock;
2058 }
2059
2060 if (spec->indep_hp_enabled != select) {
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002061 hda_nid_t *dacp;
2062 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2063 dacp = &spec->private_dac_nids[0];
2064 else
2065 dacp = &spec->multiout.hp_out_nid[0];
2066
2067 /* update HP aamix paths in case it conflicts with indep HP */
2068 if (spec->have_aamix_ctl) {
2069 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2070 update_aamix_paths(codec, spec->aamix_mode,
2071 spec->out_paths[0],
2072 spec->aamix_out_paths[0],
2073 spec->autocfg.line_out_type);
2074 else
2075 update_aamix_paths(codec, spec->aamix_mode,
2076 spec->hp_paths[0],
2077 spec->aamix_out_paths[1],
2078 AUTO_PIN_HP_OUT);
2079 }
2080
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002081 spec->indep_hp_enabled = select;
2082 if (spec->indep_hp_enabled)
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002083 *dacp = 0;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002084 else
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002085 *dacp = spec->alt_dac_nid;
Takashi Iwai92603c52013-01-22 07:46:31 +01002086
Takashi Iwai963afde2013-05-31 15:20:31 +02002087 call_hp_automute(codec, NULL);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002088 ret = 1;
2089 }
2090 unlock:
2091 mutex_unlock(&spec->pcm_mutex);
2092 return ret;
2093}
2094
2095static const struct snd_kcontrol_new indep_hp_ctl = {
2096 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2097 .name = "Independent HP",
2098 .info = indep_hp_info,
2099 .get = indep_hp_get,
2100 .put = indep_hp_put,
2101};
2102
2103
2104static int create_indep_hp_ctls(struct hda_codec *codec)
2105{
2106 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002107 hda_nid_t dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002108
2109 if (!spec->indep_hp)
2110 return 0;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002111 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2112 dac = spec->multiout.dac_nids[0];
2113 else
2114 dac = spec->multiout.hp_out_nid[0];
2115 if (!dac) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002116 spec->indep_hp = 0;
2117 return 0;
2118 }
2119
2120 spec->indep_hp_enabled = false;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002121 spec->alt_dac_nid = dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002122 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
2123 return -ENOMEM;
2124 return 0;
2125}
2126
2127/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002128 * channel mode enum control
2129 */
2130
2131static int ch_mode_info(struct snd_kcontrol *kcontrol,
2132 struct snd_ctl_elem_info *uinfo)
2133{
2134 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2135 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002136 int chs;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002137
2138 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2139 uinfo->count = 1;
2140 uinfo->value.enumerated.items = spec->multi_ios + 1;
2141 if (uinfo->value.enumerated.item > spec->multi_ios)
2142 uinfo->value.enumerated.item = spec->multi_ios;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002143 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2144 sprintf(uinfo->value.enumerated.name, "%dch", chs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002145 return 0;
2146}
2147
2148static int ch_mode_get(struct snd_kcontrol *kcontrol,
2149 struct snd_ctl_elem_value *ucontrol)
2150{
2151 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2152 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002153 ucontrol->value.enumerated.item[0] =
2154 (spec->ext_channel_count - spec->min_channel_count) / 2;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002155 return 0;
2156}
2157
Takashi Iwai196c17662013-01-04 15:01:40 +01002158static inline struct nid_path *
2159get_multiio_path(struct hda_codec *codec, int idx)
2160{
2161 struct hda_gen_spec *spec = codec->spec;
2162 return snd_hda_get_path_from_idx(codec,
2163 spec->out_paths[spec->autocfg.line_outs + idx]);
2164}
2165
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002166static void update_automute_all(struct hda_codec *codec);
2167
Takashi Iwai65033cc2013-04-16 12:31:05 +02002168/* Default value to be passed as aamix argument for snd_hda_activate_path();
2169 * used for output paths
2170 */
2171static bool aamix_default(struct hda_gen_spec *spec)
2172{
2173 return !spec->have_aamix_ctl || spec->aamix_mode;
2174}
2175
Takashi Iwai352f7f92012-12-19 12:52:06 +01002176static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2177{
2178 struct hda_gen_spec *spec = codec->spec;
2179 hda_nid_t nid = spec->multi_io[idx].pin;
2180 struct nid_path *path;
2181
Takashi Iwai196c17662013-01-04 15:01:40 +01002182 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002183 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002185
2186 if (path->active == output)
2187 return 0;
2188
2189 if (output) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01002190 set_pin_target(codec, nid, PIN_OUT, true);
Takashi Iwai65033cc2013-04-16 12:31:05 +02002191 snd_hda_activate_path(codec, path, true, aamix_default(spec));
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002192 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002193 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002194 set_pin_eapd(codec, nid, false);
Takashi Iwai65033cc2013-04-16 12:31:05 +02002195 snd_hda_activate_path(codec, path, false, aamix_default(spec));
Takashi Iwai2c12c302013-01-10 09:33:29 +01002196 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002197 path_power_down_sync(codec, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 }
Takashi Iwaia365fed2013-01-10 16:10:06 +01002199
2200 /* update jack retasking in case it modifies any of them */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002201 update_automute_all(codec);
Takashi Iwaia365fed2013-01-10 16:10:06 +01002202
Takashi Iwai352f7f92012-12-19 12:52:06 +01002203 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204}
2205
Takashi Iwai352f7f92012-12-19 12:52:06 +01002206static int ch_mode_put(struct snd_kcontrol *kcontrol,
2207 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002209 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2210 struct hda_gen_spec *spec = codec->spec;
2211 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
Takashi Iwai352f7f92012-12-19 12:52:06 +01002213 ch = ucontrol->value.enumerated.item[0];
2214 if (ch < 0 || ch > spec->multi_ios)
2215 return -EINVAL;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002216 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002217 return 0;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002218 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002219 for (i = 0; i < spec->multi_ios; i++)
2220 set_multi_io(codec, i, i < ch);
2221 spec->multiout.max_channels = max(spec->ext_channel_count,
2222 spec->const_channel_count);
2223 if (spec->need_dac_fix)
2224 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 return 1;
2226}
2227
Takashi Iwai352f7f92012-12-19 12:52:06 +01002228static const struct snd_kcontrol_new channel_mode_enum = {
2229 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2230 .name = "Channel Mode",
2231 .info = ch_mode_info,
2232 .get = ch_mode_get,
2233 .put = ch_mode_put,
2234};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
Takashi Iwai352f7f92012-12-19 12:52:06 +01002236static int create_multi_channel_mode(struct hda_codec *codec)
2237{
2238 struct hda_gen_spec *spec = codec->spec;
2239
2240 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002241 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002242 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 return 0;
2245}
2246
Takashi Iwai352f7f92012-12-19 12:52:06 +01002247/*
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002248 * aamix loopback enable/disable switch
2249 */
2250
2251#define loopback_mixing_info indep_hp_info
2252
2253static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2254 struct snd_ctl_elem_value *ucontrol)
2255{
2256 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2257 struct hda_gen_spec *spec = codec->spec;
2258 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2259 return 0;
2260}
2261
2262static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002263 int nomix_path_idx, int mix_path_idx,
2264 int out_type)
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002265{
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002266 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002267 struct nid_path *nomix_path, *mix_path;
2268
2269 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2270 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2271 if (!nomix_path || !mix_path)
2272 return;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002273
2274 /* if HP aamix path is driven from a different DAC and the
2275 * independent HP mode is ON, can't turn on aamix path
2276 */
2277 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2278 mix_path->path[0] != spec->alt_dac_nid)
2279 do_mix = false;
2280
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002281 if (do_mix) {
2282 snd_hda_activate_path(codec, nomix_path, false, true);
2283 snd_hda_activate_path(codec, mix_path, true, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002284 path_power_down_sync(codec, nomix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002285 } else {
Takashi Iwai65033cc2013-04-16 12:31:05 +02002286 snd_hda_activate_path(codec, mix_path, false, false);
2287 snd_hda_activate_path(codec, nomix_path, true, false);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002288 path_power_down_sync(codec, mix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002289 }
2290}
2291
2292static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2293 struct snd_ctl_elem_value *ucontrol)
2294{
2295 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2296 struct hda_gen_spec *spec = codec->spec;
2297 unsigned int val = ucontrol->value.enumerated.item[0];
2298
2299 if (val == spec->aamix_mode)
2300 return 0;
2301 spec->aamix_mode = val;
2302 update_aamix_paths(codec, val, spec->out_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002303 spec->aamix_out_paths[0],
2304 spec->autocfg.line_out_type);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002305 update_aamix_paths(codec, val, spec->hp_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002306 spec->aamix_out_paths[1],
2307 AUTO_PIN_HP_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002308 update_aamix_paths(codec, val, spec->speaker_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002309 spec->aamix_out_paths[2],
2310 AUTO_PIN_SPEAKER_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002311 return 1;
2312}
2313
2314static const struct snd_kcontrol_new loopback_mixing_enum = {
2315 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2316 .name = "Loopback Mixing",
2317 .info = loopback_mixing_info,
2318 .get = loopback_mixing_get,
2319 .put = loopback_mixing_put,
2320};
2321
2322static int create_loopback_mixing_ctl(struct hda_codec *codec)
2323{
2324 struct hda_gen_spec *spec = codec->spec;
2325
2326 if (!spec->mixer_nid)
2327 return 0;
2328 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
2329 spec->aamix_out_paths[2]))
2330 return 0;
2331 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2332 return -ENOMEM;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002333 spec->have_aamix_ctl = 1;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002334 return 0;
2335}
2336
2337/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002338 * shared headphone/mic handling
2339 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002340
Takashi Iwai352f7f92012-12-19 12:52:06 +01002341static void call_update_outputs(struct hda_codec *codec);
2342
2343/* for shared I/O, change the pin-control accordingly */
Takashi Iwai967303d2013-02-19 17:12:42 +01002344static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002345{
2346 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai967303d2013-02-19 17:12:42 +01002347 bool as_mic;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002348 unsigned int val;
Takashi Iwai967303d2013-02-19 17:12:42 +01002349 hda_nid_t pin;
2350
2351 pin = spec->hp_mic_pin;
2352 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
2353
2354 if (!force) {
2355 val = snd_hda_codec_get_pin_target(codec, pin);
2356 if (as_mic) {
2357 if (val & PIN_IN)
2358 return;
2359 } else {
2360 if (val & PIN_OUT)
2361 return;
2362 }
2363 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002364
2365 val = snd_hda_get_default_vref(codec, pin);
Takashi Iwai967303d2013-02-19 17:12:42 +01002366 /* if the HP pin doesn't support VREF and the codec driver gives an
2367 * alternative pin, set up the VREF on that pin instead
2368 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002369 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2370 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2371 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2372 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01002373 snd_hda_set_pin_ctl_cache(codec, vref_pin,
Takashi Iwai967303d2013-02-19 17:12:42 +01002374 PIN_IN | (as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02002375 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002376
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002377 if (!spec->hp_mic_jack_modes) {
2378 if (as_mic)
2379 val |= PIN_IN;
2380 else
2381 val = PIN_HP;
2382 set_pin_target(codec, pin, val, true);
Takashi Iwai963afde2013-05-31 15:20:31 +02002383 call_hp_automute(codec, NULL);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002384 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002385}
2386
2387/* create a shared input with the headphone out */
Takashi Iwai967303d2013-02-19 17:12:42 +01002388static int create_hp_mic(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002389{
2390 struct hda_gen_spec *spec = codec->spec;
2391 struct auto_pin_cfg *cfg = &spec->autocfg;
2392 unsigned int defcfg;
2393 hda_nid_t nid;
2394
Takashi Iwai967303d2013-02-19 17:12:42 +01002395 if (!spec->hp_mic) {
2396 if (spec->suppress_hp_mic_detect)
2397 return 0;
2398 /* automatic detection: only if no input or a single internal
2399 * input pin is found, try to detect the shared hp/mic
2400 */
2401 if (cfg->num_inputs > 1)
2402 return 0;
2403 else if (cfg->num_inputs == 1) {
2404 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2405 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2406 return 0;
2407 }
2408 }
2409
2410 spec->hp_mic = 0; /* clear once */
2411 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002412 return 0;
2413
Takashi Iwai967303d2013-02-19 17:12:42 +01002414 nid = 0;
2415 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2416 nid = cfg->line_out_pins[0];
2417 else if (cfg->hp_outs > 0)
2418 nid = cfg->hp_pins[0];
2419 if (!nid)
2420 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002421
2422 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2423 return 0; /* no input */
2424
Takashi Iwai967303d2013-02-19 17:12:42 +01002425 cfg->inputs[cfg->num_inputs].pin = nid;
2426 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
David Henningssoncb420b12013-04-11 11:30:28 +02002427 cfg->inputs[cfg->num_inputs].is_headphone_mic = 1;
Takashi Iwai967303d2013-02-19 17:12:42 +01002428 cfg->num_inputs++;
2429 spec->hp_mic = 1;
2430 spec->hp_mic_pin = nid;
2431 /* we can't handle auto-mic together with HP-mic */
2432 spec->suppress_auto_mic = 1;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002433 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
2434 return 0;
2435}
2436
Takashi Iwai978e77e2013-01-10 16:57:58 +01002437/*
2438 * output jack mode
2439 */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002440
2441static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2442
2443static const char * const out_jack_texts[] = {
2444 "Line Out", "Headphone Out",
2445};
2446
Takashi Iwai978e77e2013-01-10 16:57:58 +01002447static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2448 struct snd_ctl_elem_info *uinfo)
2449{
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002450 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
Takashi Iwai978e77e2013-01-10 16:57:58 +01002451}
2452
2453static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2454 struct snd_ctl_elem_value *ucontrol)
2455{
2456 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2457 hda_nid_t nid = kcontrol->private_value;
2458 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2459 ucontrol->value.enumerated.item[0] = 1;
2460 else
2461 ucontrol->value.enumerated.item[0] = 0;
2462 return 0;
2463}
2464
2465static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2466 struct snd_ctl_elem_value *ucontrol)
2467{
2468 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2469 hda_nid_t nid = kcontrol->private_value;
2470 unsigned int val;
2471
2472 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2473 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2474 return 0;
2475 snd_hda_set_pin_ctl_cache(codec, nid, val);
2476 return 1;
2477}
2478
2479static const struct snd_kcontrol_new out_jack_mode_enum = {
2480 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2481 .info = out_jack_mode_info,
2482 .get = out_jack_mode_get,
2483 .put = out_jack_mode_put,
2484};
2485
2486static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2487{
2488 struct hda_gen_spec *spec = codec->spec;
2489 int i;
2490
2491 for (i = 0; i < spec->kctls.used; i++) {
2492 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2493 if (!strcmp(kctl->name, name) && kctl->index == idx)
2494 return true;
2495 }
2496 return false;
2497}
2498
2499static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2500 char *name, size_t name_len)
2501{
2502 struct hda_gen_spec *spec = codec->spec;
2503 int idx = 0;
2504
2505 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2506 strlcat(name, " Jack Mode", name_len);
2507
2508 for (; find_kctl_name(codec, name, idx); idx++)
2509 ;
2510}
2511
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002512static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2513{
2514 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002515 if (spec->add_jack_modes) {
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002516 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2517 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2518 return 2;
2519 }
2520 return 1;
2521}
2522
Takashi Iwai978e77e2013-01-10 16:57:58 +01002523static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2524 hda_nid_t *pins)
2525{
2526 struct hda_gen_spec *spec = codec->spec;
2527 int i;
2528
2529 for (i = 0; i < num_pins; i++) {
2530 hda_nid_t pin = pins[i];
Takashi Iwaiced4cef2013-11-26 08:33:45 +01002531 if (pin == spec->hp_mic_pin)
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002532 continue;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002533 if (get_out_jack_num_items(codec, pin) > 1) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01002534 struct snd_kcontrol_new *knew;
Takashi Iwai975cc022013-06-28 11:56:49 +02002535 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai978e77e2013-01-10 16:57:58 +01002536 get_jack_mode_name(codec, pin, name, sizeof(name));
2537 knew = snd_hda_gen_add_kctl(spec, name,
2538 &out_jack_mode_enum);
2539 if (!knew)
2540 return -ENOMEM;
2541 knew->private_value = pin;
2542 }
2543 }
2544
2545 return 0;
2546}
2547
Takashi Iwai294765582013-01-17 09:52:11 +01002548/*
2549 * input jack mode
2550 */
2551
2552/* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2553#define NUM_VREFS 6
2554
2555static const char * const vref_texts[NUM_VREFS] = {
2556 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2557 "", "Mic 80pc Bias", "Mic 100pc Bias"
2558};
2559
2560static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2561{
2562 unsigned int pincap;
2563
2564 pincap = snd_hda_query_pin_caps(codec, pin);
2565 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2566 /* filter out unusual vrefs */
2567 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2568 return pincap;
2569}
2570
2571/* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2572static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2573{
2574 unsigned int i, n = 0;
2575
2576 for (i = 0; i < NUM_VREFS; i++) {
2577 if (vref_caps & (1 << i)) {
2578 if (n == item_idx)
2579 return i;
2580 n++;
2581 }
2582 }
2583 return 0;
2584}
2585
2586/* convert back from the vref ctl index to the enum item index */
2587static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2588{
2589 unsigned int i, n = 0;
2590
2591 for (i = 0; i < NUM_VREFS; i++) {
2592 if (i == idx)
2593 return n;
2594 if (vref_caps & (1 << i))
2595 n++;
2596 }
2597 return 0;
2598}
2599
2600static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2601 struct snd_ctl_elem_info *uinfo)
2602{
2603 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2604 hda_nid_t nid = kcontrol->private_value;
2605 unsigned int vref_caps = get_vref_caps(codec, nid);
2606
2607 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2608 vref_texts);
2609 /* set the right text */
2610 strcpy(uinfo->value.enumerated.name,
2611 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2612 return 0;
2613}
2614
2615static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2616 struct snd_ctl_elem_value *ucontrol)
2617{
2618 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2619 hda_nid_t nid = kcontrol->private_value;
2620 unsigned int vref_caps = get_vref_caps(codec, nid);
2621 unsigned int idx;
2622
2623 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2624 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2625 return 0;
2626}
2627
2628static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2629 struct snd_ctl_elem_value *ucontrol)
2630{
2631 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2632 hda_nid_t nid = kcontrol->private_value;
2633 unsigned int vref_caps = get_vref_caps(codec, nid);
2634 unsigned int val, idx;
2635
2636 val = snd_hda_codec_get_pin_target(codec, nid);
2637 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2638 if (idx == ucontrol->value.enumerated.item[0])
2639 return 0;
2640
2641 val &= ~AC_PINCTL_VREFEN;
2642 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2643 snd_hda_set_pin_ctl_cache(codec, nid, val);
2644 return 1;
2645}
2646
2647static const struct snd_kcontrol_new in_jack_mode_enum = {
2648 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2649 .info = in_jack_mode_info,
2650 .get = in_jack_mode_get,
2651 .put = in_jack_mode_put,
2652};
2653
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002654static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2655{
2656 struct hda_gen_spec *spec = codec->spec;
2657 int nitems = 0;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002658 if (spec->add_jack_modes)
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002659 nitems = hweight32(get_vref_caps(codec, pin));
2660 return nitems ? nitems : 1;
2661}
2662
Takashi Iwai294765582013-01-17 09:52:11 +01002663static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2664{
2665 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai294765582013-01-17 09:52:11 +01002666 struct snd_kcontrol_new *knew;
Takashi Iwai975cc022013-06-28 11:56:49 +02002667 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002668 unsigned int defcfg;
2669
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002670 if (pin == spec->hp_mic_pin)
2671 return 0; /* already done in create_out_jack_mode() */
Takashi Iwai294765582013-01-17 09:52:11 +01002672
2673 /* no jack mode for fixed pins */
2674 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2675 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2676 return 0;
2677
2678 /* no multiple vref caps? */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002679 if (get_in_jack_num_items(codec, pin) <= 1)
Takashi Iwai294765582013-01-17 09:52:11 +01002680 return 0;
2681
2682 get_jack_mode_name(codec, pin, name, sizeof(name));
2683 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2684 if (!knew)
2685 return -ENOMEM;
2686 knew->private_value = pin;
2687 return 0;
2688}
2689
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002690/*
2691 * HP/mic shared jack mode
2692 */
2693static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2694 struct snd_ctl_elem_info *uinfo)
2695{
2696 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2697 hda_nid_t nid = kcontrol->private_value;
2698 int out_jacks = get_out_jack_num_items(codec, nid);
2699 int in_jacks = get_in_jack_num_items(codec, nid);
2700 const char *text = NULL;
2701 int idx;
2702
2703 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2704 uinfo->count = 1;
2705 uinfo->value.enumerated.items = out_jacks + in_jacks;
2706 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2707 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2708 idx = uinfo->value.enumerated.item;
2709 if (idx < out_jacks) {
2710 if (out_jacks > 1)
2711 text = out_jack_texts[idx];
2712 else
2713 text = "Headphone Out";
2714 } else {
2715 idx -= out_jacks;
2716 if (in_jacks > 1) {
2717 unsigned int vref_caps = get_vref_caps(codec, nid);
2718 text = vref_texts[get_vref_idx(vref_caps, idx)];
2719 } else
2720 text = "Mic In";
2721 }
2722
2723 strcpy(uinfo->value.enumerated.name, text);
2724 return 0;
2725}
2726
2727static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2728{
2729 int out_jacks = get_out_jack_num_items(codec, nid);
2730 int in_jacks = get_in_jack_num_items(codec, nid);
2731 unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2732 int idx = 0;
2733
2734 if (val & PIN_OUT) {
2735 if (out_jacks > 1 && val == PIN_HP)
2736 idx = 1;
2737 } else if (val & PIN_IN) {
2738 idx = out_jacks;
2739 if (in_jacks > 1) {
2740 unsigned int vref_caps = get_vref_caps(codec, nid);
2741 val &= AC_PINCTL_VREFEN;
2742 idx += cvt_from_vref_idx(vref_caps, val);
2743 }
2744 }
2745 return idx;
2746}
2747
2748static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2749 struct snd_ctl_elem_value *ucontrol)
2750{
2751 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2752 hda_nid_t nid = kcontrol->private_value;
2753 ucontrol->value.enumerated.item[0] =
2754 get_cur_hp_mic_jack_mode(codec, nid);
2755 return 0;
2756}
2757
2758static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2759 struct snd_ctl_elem_value *ucontrol)
2760{
2761 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2762 hda_nid_t nid = kcontrol->private_value;
2763 int out_jacks = get_out_jack_num_items(codec, nid);
2764 int in_jacks = get_in_jack_num_items(codec, nid);
2765 unsigned int val, oldval, idx;
2766
2767 oldval = get_cur_hp_mic_jack_mode(codec, nid);
2768 idx = ucontrol->value.enumerated.item[0];
2769 if (oldval == idx)
2770 return 0;
2771
2772 if (idx < out_jacks) {
2773 if (out_jacks > 1)
2774 val = idx ? PIN_HP : PIN_OUT;
2775 else
2776 val = PIN_HP;
2777 } else {
2778 idx -= out_jacks;
2779 if (in_jacks > 1) {
2780 unsigned int vref_caps = get_vref_caps(codec, nid);
2781 val = snd_hda_codec_get_pin_target(codec, nid);
Takashi Iwai3f550e32013-03-07 18:30:27 +01002782 val &= ~(AC_PINCTL_VREFEN | PIN_HP);
2783 val |= get_vref_idx(vref_caps, idx) | PIN_IN;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002784 } else
Takashi Iwai16c0cefe2013-11-26 08:44:26 +01002785 val = snd_hda_get_default_vref(codec, nid) | PIN_IN;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002786 }
2787 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwai963afde2013-05-31 15:20:31 +02002788 call_hp_automute(codec, NULL);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002789
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002790 return 1;
2791}
2792
2793static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
2794 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2795 .info = hp_mic_jack_mode_info,
2796 .get = hp_mic_jack_mode_get,
2797 .put = hp_mic_jack_mode_put,
2798};
2799
2800static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2801{
2802 struct hda_gen_spec *spec = codec->spec;
2803 struct snd_kcontrol_new *knew;
2804
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002805 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
2806 &hp_mic_jack_mode_enum);
2807 if (!knew)
2808 return -ENOMEM;
2809 knew->private_value = pin;
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002810 spec->hp_mic_jack_modes = 1;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002811 return 0;
2812}
Takashi Iwai352f7f92012-12-19 12:52:06 +01002813
2814/*
2815 * Parse input paths
2816 */
2817
Takashi Iwai352f7f92012-12-19 12:52:06 +01002818/* add the powersave loopback-list entry */
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002819static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002820{
2821 struct hda_amp_list *list;
2822
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002823 list = snd_array_new(&spec->loopback_list);
2824 if (!list)
2825 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002826 list->nid = mix;
2827 list->dir = HDA_INPUT;
2828 list->idx = idx;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002829 spec->loopback.amplist = spec->loopback_list.list;
2830 return 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002831}
Takashi Iwaicb53c622007-08-10 17:21:45 +02002832
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002833/* return true if either a volume or a mute amp is found for the given
2834 * aamix path; the amp has to be either in the mixer node or its direct leaf
2835 */
2836static bool look_for_mix_leaf_ctls(struct hda_codec *codec, hda_nid_t mix_nid,
2837 hda_nid_t pin, unsigned int *mix_val,
2838 unsigned int *mute_val)
2839{
2840 int idx, num_conns;
2841 const hda_nid_t *list;
2842 hda_nid_t nid;
2843
2844 idx = snd_hda_get_conn_index(codec, mix_nid, pin, true);
2845 if (idx < 0)
2846 return false;
2847
2848 *mix_val = *mute_val = 0;
2849 if (nid_has_volume(codec, mix_nid, HDA_INPUT))
2850 *mix_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2851 if (nid_has_mute(codec, mix_nid, HDA_INPUT))
2852 *mute_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2853 if (*mix_val && *mute_val)
2854 return true;
2855
2856 /* check leaf node */
2857 num_conns = snd_hda_get_conn_list(codec, mix_nid, &list);
2858 if (num_conns < idx)
2859 return false;
2860 nid = list[idx];
2861 if (!*mix_val && nid_has_volume(codec, nid, HDA_OUTPUT))
2862 *mix_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2863 if (!*mute_val && nid_has_mute(codec, nid, HDA_OUTPUT))
2864 *mute_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2865
2866 return *mix_val || *mute_val;
2867}
2868
Takashi Iwai352f7f92012-12-19 12:52:06 +01002869/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01002870static int new_analog_input(struct hda_codec *codec, int input_idx,
2871 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002872 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002874 struct hda_gen_spec *spec = codec->spec;
2875 struct nid_path *path;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002876 unsigned int mix_val, mute_val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002877 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002879 if (!look_for_mix_leaf_ctls(codec, mix_nid, pin, &mix_val, &mute_val))
2880 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002881
Takashi Iwai3ca529d2013-01-07 17:25:08 +01002882 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002883 if (!path)
2884 return -EINVAL;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002885 print_nid_path("loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01002886 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002887
2888 idx = path->idx[path->depth - 1];
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002889 if (mix_val) {
2890 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, mix_val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002891 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 return err;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002893 path->ctls[NID_PATH_VOL_CTL] = mix_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 }
2895
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002896 if (mute_val) {
2897 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, mute_val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002898 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 return err;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002900 path->ctls[NID_PATH_MUTE_CTL] = mute_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 }
2902
Takashi Iwai352f7f92012-12-19 12:52:06 +01002903 path->active = true;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002904 err = add_loopback_list(spec, mix_nid, idx);
2905 if (err < 0)
2906 return err;
Takashi Iwaie4a395e2013-01-23 17:00:31 +01002907
2908 if (spec->mixer_nid != spec->mixer_merge_nid &&
2909 !spec->loopback_merge_path) {
2910 path = snd_hda_add_new_path(codec, spec->mixer_nid,
2911 spec->mixer_merge_nid, 0);
2912 if (path) {
2913 print_nid_path("loopback-merge", path);
2914 path->active = true;
2915 spec->loopback_merge_path =
2916 snd_hda_get_path_idx(codec, path);
2917 }
2918 }
2919
Takashi Iwai352f7f92012-12-19 12:52:06 +01002920 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921}
2922
Takashi Iwai352f7f92012-12-19 12:52:06 +01002923static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002925 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
2926 return (pincap & AC_PINCAP_IN) != 0;
2927}
2928
2929/* Parse the codec tree and retrieve ADCs */
2930static int fill_adc_nids(struct hda_codec *codec)
2931{
2932 struct hda_gen_spec *spec = codec->spec;
2933 hda_nid_t nid;
2934 hda_nid_t *adc_nids = spec->adc_nids;
2935 int max_nums = ARRAY_SIZE(spec->adc_nids);
2936 int i, nums = 0;
2937
2938 nid = codec->start_nid;
2939 for (i = 0; i < codec->num_nodes; i++, nid++) {
2940 unsigned int caps = get_wcaps(codec, nid);
2941 int type = get_wcaps_type(caps);
2942
2943 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2944 continue;
2945 adc_nids[nums] = nid;
2946 if (++nums >= max_nums)
2947 break;
2948 }
2949 spec->num_adc_nids = nums;
Takashi Iwai0ffd5342013-01-17 15:53:29 +01002950
2951 /* copy the detected ADCs to all_adcs[] */
2952 spec->num_all_adcs = nums;
2953 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
2954
Takashi Iwai352f7f92012-12-19 12:52:06 +01002955 return nums;
2956}
2957
2958/* filter out invalid adc_nids that don't give all active input pins;
2959 * if needed, check whether dynamic ADC-switching is available
2960 */
2961static int check_dyn_adc_switch(struct hda_codec *codec)
2962{
2963 struct hda_gen_spec *spec = codec->spec;
2964 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002965 unsigned int ok_bits;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002966 int i, n, nums;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002967
Takashi Iwai352f7f92012-12-19 12:52:06 +01002968 nums = 0;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002969 ok_bits = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002970 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002971 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002972 if (!spec->input_paths[i][n])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002973 break;
2974 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002975 if (i >= imux->num_items) {
2976 ok_bits |= (1 << n);
2977 nums++;
2978 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002979 }
2980
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002981 if (!ok_bits) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002982 /* check whether ADC-switch is possible */
2983 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002984 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002985 if (spec->input_paths[i][n]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002986 spec->dyn_adc_idx[i] = n;
2987 break;
2988 }
2989 }
2990 }
2991
2992 snd_printdd("hda-codec: enabling ADC switching\n");
2993 spec->dyn_adc_switch = 1;
2994 } else if (nums != spec->num_adc_nids) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002995 /* shrink the invalid adcs and input paths */
2996 nums = 0;
2997 for (n = 0; n < spec->num_adc_nids; n++) {
2998 if (!(ok_bits & (1 << n)))
2999 continue;
3000 if (n != nums) {
3001 spec->adc_nids[nums] = spec->adc_nids[n];
Takashi Iwai980428c2013-01-09 09:28:20 +01003002 for (i = 0; i < imux->num_items; i++) {
3003 invalidate_nid_path(codec,
3004 spec->input_paths[i][nums]);
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003005 spec->input_paths[i][nums] =
3006 spec->input_paths[i][n];
Takashi Iwai980428c2013-01-09 09:28:20 +01003007 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003008 }
3009 nums++;
3010 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003011 spec->num_adc_nids = nums;
3012 }
3013
Takashi Iwai967303d2013-02-19 17:12:42 +01003014 if (imux->num_items == 1 ||
3015 (imux->num_items == 2 && spec->hp_mic)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003016 snd_printdd("hda-codec: reducing to a single ADC\n");
3017 spec->num_adc_nids = 1; /* reduce to a single ADC */
3018 }
3019
3020 /* single index for individual volumes ctls */
3021 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
3022 spec->num_adc_nids = 1;
3023
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 return 0;
3025}
3026
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003027/* parse capture source paths from the given pin and create imux items */
3028static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
Takashi Iwai9dba2052013-01-18 10:01:15 +01003029 int cfg_idx, int num_adcs,
3030 const char *label, int anchor)
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003031{
3032 struct hda_gen_spec *spec = codec->spec;
3033 struct hda_input_mux *imux = &spec->input_mux;
3034 int imux_idx = imux->num_items;
3035 bool imux_added = false;
3036 int c;
3037
3038 for (c = 0; c < num_adcs; c++) {
3039 struct nid_path *path;
3040 hda_nid_t adc = spec->adc_nids[c];
3041
3042 if (!is_reachable_path(codec, pin, adc))
3043 continue;
3044 path = snd_hda_add_new_path(codec, pin, adc, anchor);
3045 if (!path)
3046 continue;
3047 print_nid_path("input", path);
3048 spec->input_paths[imux_idx][c] =
3049 snd_hda_get_path_idx(codec, path);
3050
3051 if (!imux_added) {
Takashi Iwai967303d2013-02-19 17:12:42 +01003052 if (spec->hp_mic_pin == pin)
3053 spec->hp_mic_mux_idx = imux->num_items;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003054 spec->imux_pins[imux->num_items] = pin;
Takashi Iwai9dba2052013-01-18 10:01:15 +01003055 snd_hda_add_imux_item(imux, label, cfg_idx, NULL);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003056 imux_added = true;
Takashi Iwaif1e762d2013-12-09 16:02:24 +01003057 if (spec->dyn_adc_switch)
3058 spec->dyn_adc_idx[imux_idx] = c;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003059 }
3060 }
3061
3062 return 0;
3063}
3064
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003066 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 */
Takashi Iwai9dba2052013-01-18 10:01:15 +01003068
Takashi Iwaic9700422013-01-18 10:17:30 +01003069/* fill the label for each input at first */
3070static int fill_input_pin_labels(struct hda_codec *codec)
3071{
3072 struct hda_gen_spec *spec = codec->spec;
3073 const struct auto_pin_cfg *cfg = &spec->autocfg;
3074 int i;
3075
3076 for (i = 0; i < cfg->num_inputs; i++) {
3077 hda_nid_t pin = cfg->inputs[i].pin;
3078 const char *label;
3079 int j, idx;
3080
3081 if (!is_input_pin(codec, pin))
3082 continue;
3083
3084 label = hda_get_autocfg_input_label(codec, cfg, i);
3085 idx = 0;
David Henningsson8e8db7f2013-01-18 15:43:02 +01003086 for (j = i - 1; j >= 0; j--) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003087 if (spec->input_labels[j] &&
3088 !strcmp(spec->input_labels[j], label)) {
3089 idx = spec->input_label_idxs[j] + 1;
3090 break;
3091 }
3092 }
3093
3094 spec->input_labels[i] = label;
3095 spec->input_label_idxs[i] = idx;
3096 }
3097
3098 return 0;
3099}
3100
Takashi Iwai9dba2052013-01-18 10:01:15 +01003101#define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
3102
Takashi Iwai352f7f92012-12-19 12:52:06 +01003103static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003104{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003105 struct hda_gen_spec *spec = codec->spec;
3106 const struct auto_pin_cfg *cfg = &spec->autocfg;
3107 hda_nid_t mixer = spec->mixer_nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003108 int num_adcs;
Takashi Iwaic9700422013-01-18 10:17:30 +01003109 int i, err;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003110 unsigned int val;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003111
Takashi Iwai352f7f92012-12-19 12:52:06 +01003112 num_adcs = fill_adc_nids(codec);
3113 if (num_adcs < 0)
3114 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003115
Takashi Iwaic9700422013-01-18 10:17:30 +01003116 err = fill_input_pin_labels(codec);
3117 if (err < 0)
3118 return err;
3119
Takashi Iwai352f7f92012-12-19 12:52:06 +01003120 for (i = 0; i < cfg->num_inputs; i++) {
3121 hda_nid_t pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122
Takashi Iwai352f7f92012-12-19 12:52:06 +01003123 pin = cfg->inputs[i].pin;
3124 if (!is_input_pin(codec, pin))
3125 continue;
3126
Takashi Iwai2c12c302013-01-10 09:33:29 +01003127 val = PIN_IN;
3128 if (cfg->inputs[i].type == AUTO_PIN_MIC)
3129 val |= snd_hda_get_default_vref(codec, pin);
Takashi Iwai93c9d8a2013-03-11 09:48:43 +01003130 if (pin != spec->hp_mic_pin)
3131 set_pin_target(codec, pin, val, false);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003132
Takashi Iwai352f7f92012-12-19 12:52:06 +01003133 if (mixer) {
3134 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01003135 err = new_analog_input(codec, i, pin,
Takashi Iwaic9700422013-01-18 10:17:30 +01003136 spec->input_labels[i],
3137 spec->input_label_idxs[i],
3138 mixer);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003139 if (err < 0)
3140 return err;
3141 }
3142 }
3143
Takashi Iwaic9700422013-01-18 10:17:30 +01003144 err = parse_capture_source(codec, pin, i, num_adcs,
3145 spec->input_labels[i], -mixer);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003146 if (err < 0)
3147 return err;
Takashi Iwai294765582013-01-17 09:52:11 +01003148
Takashi Iwaif811c3c2013-03-07 18:32:59 +01003149 if (spec->add_jack_modes) {
Takashi Iwai294765582013-01-17 09:52:11 +01003150 err = create_in_jack_mode(codec, pin);
3151 if (err < 0)
3152 return err;
3153 }
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003154 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003155
Takashi Iwaif1e762d2013-12-09 16:02:24 +01003156 /* add stereo mix when explicitly enabled via hint */
3157 if (mixer && spec->add_stereo_mix_input &&
3158 snd_hda_get_bool_hint(codec, "add_stereo_mix_input") > 0) {
Takashi Iwai9dba2052013-01-18 10:01:15 +01003159 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003160 "Stereo Mix", 0);
3161 if (err < 0)
3162 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003163 }
3164
3165 return 0;
3166}
3167
3168
3169/*
3170 * input source mux
3171 */
3172
Takashi Iwaic697b712013-01-07 17:09:26 +01003173/* get the input path specified by the given adc and imux indices */
3174static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003175{
3176 struct hda_gen_spec *spec = codec->spec;
David Henningssonb56fa1e2013-01-16 11:45:35 +01003177 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3178 snd_BUG();
3179 return NULL;
3180 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003181 if (spec->dyn_adc_switch)
3182 adc_idx = spec->dyn_adc_idx[imux_idx];
David Henningssond3d982f2013-01-18 15:43:01 +01003183 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
David Henningssonb56fa1e2013-01-16 11:45:35 +01003184 snd_BUG();
3185 return NULL;
3186 }
Takashi Iwaic697b712013-01-07 17:09:26 +01003187 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003188}
3189
3190static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3191 unsigned int idx);
3192
3193static int mux_enum_info(struct snd_kcontrol *kcontrol,
3194 struct snd_ctl_elem_info *uinfo)
3195{
3196 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3197 struct hda_gen_spec *spec = codec->spec;
3198 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3199}
3200
3201static int mux_enum_get(struct snd_kcontrol *kcontrol,
3202 struct snd_ctl_elem_value *ucontrol)
3203{
3204 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3205 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003206 /* the ctls are created at once with multiple counts */
3207 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003208
3209 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3210 return 0;
3211}
3212
3213static int mux_enum_put(struct snd_kcontrol *kcontrol,
3214 struct snd_ctl_elem_value *ucontrol)
3215{
3216 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003217 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003218 return mux_select(codec, adc_idx,
3219 ucontrol->value.enumerated.item[0]);
3220}
3221
Takashi Iwai352f7f92012-12-19 12:52:06 +01003222static const struct snd_kcontrol_new cap_src_temp = {
3223 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3224 .name = "Input Source",
3225 .info = mux_enum_info,
3226 .get = mux_enum_get,
3227 .put = mux_enum_put,
3228};
3229
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003230/*
3231 * capture volume and capture switch ctls
3232 */
3233
Takashi Iwai352f7f92012-12-19 12:52:06 +01003234typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3235 struct snd_ctl_elem_value *ucontrol);
3236
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003237/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003238static int cap_put_caller(struct snd_kcontrol *kcontrol,
3239 struct snd_ctl_elem_value *ucontrol,
3240 put_call_t func, int type)
3241{
3242 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3243 struct hda_gen_spec *spec = codec->spec;
3244 const struct hda_input_mux *imux;
3245 struct nid_path *path;
3246 int i, adc_idx, err = 0;
3247
3248 imux = &spec->input_mux;
David Henningssona053d1e2013-01-16 11:45:36 +01003249 adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003250 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003251 /* we use the cache-only update at first since multiple input paths
3252 * may shared the same amp; by updating only caches, the redundant
3253 * writes to hardware can be reduced.
3254 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003255 codec->cached_write = 1;
3256 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003257 path = get_input_path(codec, adc_idx, i);
3258 if (!path || !path->ctls[type])
Takashi Iwai352f7f92012-12-19 12:52:06 +01003259 continue;
3260 kcontrol->private_value = path->ctls[type];
3261 err = func(kcontrol, ucontrol);
3262 if (err < 0)
3263 goto error;
3264 }
3265 error:
3266 codec->cached_write = 0;
3267 mutex_unlock(&codec->control_mutex);
Takashi Iwaidc870f32013-01-22 15:24:30 +01003268 snd_hda_codec_flush_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003269 if (err >= 0 && spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01003270 spec->cap_sync_hook(codec, ucontrol);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003271 return err;
3272}
3273
3274/* capture volume ctl callbacks */
3275#define cap_vol_info snd_hda_mixer_amp_volume_info
3276#define cap_vol_get snd_hda_mixer_amp_volume_get
3277#define cap_vol_tlv snd_hda_mixer_amp_tlv
3278
3279static int cap_vol_put(struct snd_kcontrol *kcontrol,
3280 struct snd_ctl_elem_value *ucontrol)
3281{
3282 return cap_put_caller(kcontrol, ucontrol,
3283 snd_hda_mixer_amp_volume_put,
3284 NID_PATH_VOL_CTL);
3285}
3286
3287static const struct snd_kcontrol_new cap_vol_temp = {
3288 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3289 .name = "Capture Volume",
3290 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3291 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3292 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3293 .info = cap_vol_info,
3294 .get = cap_vol_get,
3295 .put = cap_vol_put,
3296 .tlv = { .c = cap_vol_tlv },
3297};
3298
3299/* capture switch ctl callbacks */
3300#define cap_sw_info snd_ctl_boolean_stereo_info
3301#define cap_sw_get snd_hda_mixer_amp_switch_get
3302
3303static int cap_sw_put(struct snd_kcontrol *kcontrol,
3304 struct snd_ctl_elem_value *ucontrol)
3305{
Takashi Iwaia90229e2013-01-18 14:10:00 +01003306 return cap_put_caller(kcontrol, ucontrol,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003307 snd_hda_mixer_amp_switch_put,
3308 NID_PATH_MUTE_CTL);
3309}
3310
3311static const struct snd_kcontrol_new cap_sw_temp = {
3312 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3313 .name = "Capture Switch",
3314 .info = cap_sw_info,
3315 .get = cap_sw_get,
3316 .put = cap_sw_put,
3317};
3318
3319static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3320{
3321 hda_nid_t nid;
3322 int i, depth;
3323
3324 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3325 for (depth = 0; depth < 3; depth++) {
3326 if (depth >= path->depth)
3327 return -EINVAL;
3328 i = path->depth - depth - 1;
3329 nid = path->path[i];
3330 if (!path->ctls[NID_PATH_VOL_CTL]) {
3331 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3332 path->ctls[NID_PATH_VOL_CTL] =
3333 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3334 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3335 int idx = path->idx[i];
3336 if (!depth && codec->single_adc_amp)
3337 idx = 0;
3338 path->ctls[NID_PATH_VOL_CTL] =
3339 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3340 }
3341 }
3342 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3343 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3344 path->ctls[NID_PATH_MUTE_CTL] =
3345 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3346 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3347 int idx = path->idx[i];
3348 if (!depth && codec->single_adc_amp)
3349 idx = 0;
3350 path->ctls[NID_PATH_MUTE_CTL] =
3351 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3352 }
3353 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01003354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 return 0;
3356}
3357
Takashi Iwai352f7f92012-12-19 12:52:06 +01003358static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003360 struct hda_gen_spec *spec = codec->spec;
3361 struct auto_pin_cfg *cfg = &spec->autocfg;
3362 unsigned int val;
3363 int i;
3364
3365 if (!spec->inv_dmic_split)
3366 return false;
3367 for (i = 0; i < cfg->num_inputs; i++) {
3368 if (cfg->inputs[i].pin != nid)
3369 continue;
3370 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3371 return false;
3372 val = snd_hda_codec_get_pincfg(codec, nid);
3373 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3374 }
3375 return false;
3376}
3377
Takashi Iwaia90229e2013-01-18 14:10:00 +01003378/* capture switch put callback for a single control with hook call */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003379static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3380 struct snd_ctl_elem_value *ucontrol)
3381{
3382 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3383 struct hda_gen_spec *spec = codec->spec;
3384 int ret;
3385
3386 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3387 if (ret < 0)
3388 return ret;
3389
Takashi Iwaia90229e2013-01-18 14:10:00 +01003390 if (spec->cap_sync_hook)
3391 spec->cap_sync_hook(codec, ucontrol);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003392
3393 return ret;
3394}
3395
Takashi Iwai352f7f92012-12-19 12:52:06 +01003396static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3397 int idx, bool is_switch, unsigned int ctl,
3398 bool inv_dmic)
3399{
3400 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai975cc022013-06-28 11:56:49 +02003401 char tmpname[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003402 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3403 const char *sfx = is_switch ? "Switch" : "Volume";
3404 unsigned int chs = inv_dmic ? 1 : 3;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003405 struct snd_kcontrol_new *knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003406
3407 if (!ctl)
3408 return 0;
3409
3410 if (label)
3411 snprintf(tmpname, sizeof(tmpname),
3412 "%s Capture %s", label, sfx);
3413 else
3414 snprintf(tmpname, sizeof(tmpname),
3415 "Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003416 knew = add_control(spec, type, tmpname, idx,
3417 amp_val_replace_channels(ctl, chs));
3418 if (!knew)
3419 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003420 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003421 knew->put = cap_single_sw_put;
3422 if (!inv_dmic)
3423 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003424
3425 /* Make independent right kcontrol */
3426 if (label)
3427 snprintf(tmpname, sizeof(tmpname),
3428 "Inverted %s Capture %s", label, sfx);
3429 else
3430 snprintf(tmpname, sizeof(tmpname),
3431 "Inverted Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003432 knew = add_control(spec, type, tmpname, idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003433 amp_val_replace_channels(ctl, 2));
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003434 if (!knew)
3435 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003436 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003437 knew->put = cap_single_sw_put;
3438 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003439}
3440
3441/* create single (and simple) capture volume and switch controls */
3442static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3443 unsigned int vol_ctl, unsigned int sw_ctl,
3444 bool inv_dmic)
3445{
3446 int err;
3447 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3448 if (err < 0)
3449 return err;
3450 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3451 if (err < 0)
3452 return err;
3453 return 0;
3454}
3455
3456/* create bound capture volume and switch controls */
3457static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3458 unsigned int vol_ctl, unsigned int sw_ctl)
3459{
3460 struct hda_gen_spec *spec = codec->spec;
3461 struct snd_kcontrol_new *knew;
3462
3463 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003464 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003465 if (!knew)
3466 return -ENOMEM;
3467 knew->index = idx;
3468 knew->private_value = vol_ctl;
3469 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3470 }
3471 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003472 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003473 if (!knew)
3474 return -ENOMEM;
3475 knew->index = idx;
3476 knew->private_value = sw_ctl;
3477 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3478 }
3479 return 0;
3480}
3481
3482/* return the vol ctl when used first in the imux list */
3483static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3484{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003485 struct nid_path *path;
3486 unsigned int ctl;
3487 int i;
3488
Takashi Iwaic697b712013-01-07 17:09:26 +01003489 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003490 if (!path)
3491 return 0;
3492 ctl = path->ctls[type];
3493 if (!ctl)
3494 return 0;
3495 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003496 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003497 if (path && path->ctls[type] == ctl)
3498 return 0;
3499 }
3500 return ctl;
3501}
3502
3503/* create individual capture volume and switch controls per input */
3504static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3505{
3506 struct hda_gen_spec *spec = codec->spec;
3507 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaic9700422013-01-18 10:17:30 +01003508 int i, err, type;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003509
3510 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003511 bool inv_dmic;
Takashi Iwaic9700422013-01-18 10:17:30 +01003512 int idx;
Takashi Iwai9dba2052013-01-18 10:01:15 +01003513
Takashi Iwaic9700422013-01-18 10:17:30 +01003514 idx = imux->items[i].index;
3515 if (idx >= spec->autocfg.num_inputs)
Takashi Iwai9dba2052013-01-18 10:01:15 +01003516 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003517 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3518
3519 for (type = 0; type < 2; type++) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003520 err = add_single_cap_ctl(codec,
3521 spec->input_labels[idx],
3522 spec->input_label_idxs[idx],
3523 type,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003524 get_first_cap_ctl(codec, i, type),
3525 inv_dmic);
3526 if (err < 0)
3527 return err;
3528 }
3529 }
3530 return 0;
3531}
3532
3533static int create_capture_mixers(struct hda_codec *codec)
3534{
3535 struct hda_gen_spec *spec = codec->spec;
3536 struct hda_input_mux *imux = &spec->input_mux;
3537 int i, n, nums, err;
3538
3539 if (spec->dyn_adc_switch)
3540 nums = 1;
3541 else
3542 nums = spec->num_adc_nids;
3543
3544 if (!spec->auto_mic && imux->num_items > 1) {
3545 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01003546 const char *name;
3547 name = nums > 1 ? "Input Source" : "Capture Source";
3548 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003549 if (!knew)
3550 return -ENOMEM;
3551 knew->count = nums;
3552 }
3553
3554 for (n = 0; n < nums; n++) {
3555 bool multi = false;
David Henningsson99a55922013-01-16 15:58:44 +01003556 bool multi_cap_vol = spec->multi_cap_vol;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003557 bool inv_dmic = false;
3558 int vol, sw;
3559
3560 vol = sw = 0;
3561 for (i = 0; i < imux->num_items; i++) {
3562 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01003563 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003564 if (!path)
3565 continue;
3566 parse_capvol_in_path(codec, path);
3567 if (!vol)
3568 vol = path->ctls[NID_PATH_VOL_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003569 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003570 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003571 if (!same_amp_caps(codec, vol,
3572 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3573 multi_cap_vol = true;
3574 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003575 if (!sw)
3576 sw = path->ctls[NID_PATH_MUTE_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003577 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003578 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003579 if (!same_amp_caps(codec, sw,
3580 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3581 multi_cap_vol = true;
3582 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003583 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3584 inv_dmic = true;
3585 }
3586
3587 if (!multi)
3588 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3589 inv_dmic);
David Henningssonccb04152013-10-14 10:16:22 +02003590 else if (!multi_cap_vol && !inv_dmic)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003591 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3592 else
3593 err = create_multi_cap_vol_ctl(codec);
3594 if (err < 0)
3595 return err;
3596 }
3597
3598 return 0;
3599}
3600
3601/*
3602 * add mic boosts if needed
3603 */
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003604
3605/* check whether the given amp is feasible as a boost volume */
3606static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3607 int dir, int idx)
3608{
3609 unsigned int step;
3610
3611 if (!nid_has_volume(codec, nid, dir) ||
3612 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3613 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3614 return false;
3615
3616 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3617 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3618 if (step < 0x20)
3619 return false;
3620 return true;
3621}
3622
3623/* look for a boost amp in a widget close to the pin */
3624static unsigned int look_for_boost_amp(struct hda_codec *codec,
3625 struct nid_path *path)
3626{
3627 unsigned int val = 0;
3628 hda_nid_t nid;
3629 int depth;
3630
3631 for (depth = 0; depth < 3; depth++) {
3632 if (depth >= path->depth - 1)
3633 break;
3634 nid = path->path[depth];
3635 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3636 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3637 break;
3638 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3639 path->idx[depth])) {
3640 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3641 HDA_INPUT);
3642 break;
3643 }
3644 }
3645
3646 return val;
3647}
3648
Takashi Iwai352f7f92012-12-19 12:52:06 +01003649static int parse_mic_boost(struct hda_codec *codec)
3650{
3651 struct hda_gen_spec *spec = codec->spec;
3652 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003653 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003654 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003655
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003656 if (!spec->num_adc_nids)
3657 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003658
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003659 for (i = 0; i < imux->num_items; i++) {
3660 struct nid_path *path;
3661 unsigned int val;
3662 int idx;
Takashi Iwai975cc022013-06-28 11:56:49 +02003663 char boost_label[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
David Henningsson02aba552013-01-16 15:58:43 +01003664
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003665 idx = imux->items[i].index;
3666 if (idx >= imux->num_items)
3667 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003668
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003669 /* check only line-in and mic pins */
Takashi Iwai1799cdd2013-01-18 14:37:16 +01003670 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003671 continue;
3672
3673 path = get_input_path(codec, 0, i);
3674 if (!path)
3675 continue;
3676
3677 val = look_for_boost_amp(codec, path);
3678 if (!val)
3679 continue;
3680
3681 /* create a boost control */
3682 snprintf(boost_label, sizeof(boost_label),
3683 "%s Boost Volume", spec->input_labels[idx]);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003684 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3685 spec->input_label_idxs[idx], val))
3686 return -ENOMEM;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003687
3688 path->ctls[NID_PATH_BOOST_CTL] = val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003689 }
3690 return 0;
3691}
3692
3693/*
3694 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3695 */
3696static void parse_digital(struct hda_codec *codec)
3697{
3698 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003699 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003700 int i, nums;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003701 hda_nid_t dig_nid, pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003702
3703 /* support multiple SPDIFs; the secondary is set up as a slave */
3704 nums = 0;
3705 for (i = 0; i < spec->autocfg.dig_outs; i++) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003706 pin = spec->autocfg.dig_out_pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003707 dig_nid = look_for_dac(codec, pin, true);
3708 if (!dig_nid)
3709 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003710 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003711 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003712 continue;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003713 print_nid_path("digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01003714 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01003715 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003716 set_pin_target(codec, pin, PIN_OUT, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003717 if (!nums) {
3718 spec->multiout.dig_out_nid = dig_nid;
3719 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3720 } else {
3721 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3722 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
3723 break;
3724 spec->slave_dig_outs[nums - 1] = dig_nid;
3725 }
3726 nums++;
3727 }
3728
3729 if (spec->autocfg.dig_in_pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003730 pin = spec->autocfg.dig_in_pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003731 dig_nid = codec->start_nid;
3732 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003733 unsigned int wcaps = get_wcaps(codec, dig_nid);
3734 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3735 continue;
3736 if (!(wcaps & AC_WCAP_DIGITAL))
3737 continue;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003738 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003739 if (path) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003740 print_nid_path("digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003741 path->active = true;
3742 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003743 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003744 set_pin_target(codec, pin, PIN_IN, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003745 break;
3746 }
3747 }
3748 }
3749}
3750
3751
3752/*
3753 * input MUX handling
3754 */
3755
3756static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3757
3758/* select the given imux item; either unmute exclusively or select the route */
3759static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3760 unsigned int idx)
3761{
3762 struct hda_gen_spec *spec = codec->spec;
3763 const struct hda_input_mux *imux;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003764 struct nid_path *old_path, *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003765
3766 imux = &spec->input_mux;
3767 if (!imux->num_items)
3768 return 0;
3769
3770 if (idx >= imux->num_items)
3771 idx = imux->num_items - 1;
3772 if (spec->cur_mux[adc_idx] == idx)
3773 return 0;
3774
Takashi Iwai55196ff2013-01-24 17:32:56 +01003775 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3776 if (!old_path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003777 return 0;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003778 if (old_path->active)
3779 snd_hda_activate_path(codec, old_path, false, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003780
3781 spec->cur_mux[adc_idx] = idx;
3782
Takashi Iwai967303d2013-02-19 17:12:42 +01003783 if (spec->hp_mic)
3784 update_hp_mic(codec, adc_idx, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003785
3786 if (spec->dyn_adc_switch)
3787 dyn_adc_pcm_resetup(codec, idx);
3788
Takashi Iwaic697b712013-01-07 17:09:26 +01003789 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003790 if (!path)
3791 return 0;
3792 if (path->active)
3793 return 0;
3794 snd_hda_activate_path(codec, path, true, false);
3795 if (spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01003796 spec->cap_sync_hook(codec, NULL);
Takashi Iwai55196ff2013-01-24 17:32:56 +01003797 path_power_down_sync(codec, old_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003798 return 1;
3799}
3800
3801
3802/*
3803 * Jack detections for HP auto-mute and mic-switch
3804 */
3805
3806/* check each pin in the given array; returns true if any of them is plugged */
3807static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
3808{
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02003809 int i;
3810 bool present = false;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003811
3812 for (i = 0; i < num_pins; i++) {
3813 hda_nid_t nid = pins[i];
3814 if (!nid)
3815 break;
Takashi Iwai0b4df932013-01-10 09:45:13 +01003816 /* don't detect pins retasked as inputs */
3817 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
3818 continue;
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02003819 if (snd_hda_jack_detect_state(codec, nid) == HDA_JACK_PRESENT)
3820 present = true;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003821 }
3822 return present;
3823}
3824
3825/* standard HP/line-out auto-mute helper */
3826static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003827 int *paths, bool mute)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003828{
3829 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003830 int i;
3831
3832 for (i = 0; i < num_pins; i++) {
3833 hda_nid_t nid = pins[i];
Takashi Iwai967303d2013-02-19 17:12:42 +01003834 unsigned int val, oldval;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003835 if (!nid)
3836 break;
Takashi Iwai7eebffd2013-06-24 16:00:21 +02003837
3838 if (spec->auto_mute_via_amp) {
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003839 struct nid_path *path;
3840 hda_nid_t mute_nid;
3841
3842 path = snd_hda_get_path_from_idx(codec, paths[i]);
3843 if (!path)
3844 continue;
3845 mute_nid = get_amp_nid_(path->ctls[NID_PATH_MUTE_CTL]);
3846 if (!mute_nid)
3847 continue;
Takashi Iwai7eebffd2013-06-24 16:00:21 +02003848 if (mute)
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003849 spec->mute_bits |= (1ULL << mute_nid);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02003850 else
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003851 spec->mute_bits &= ~(1ULL << mute_nid);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02003852 set_pin_eapd(codec, nid, !mute);
3853 continue;
3854 }
3855
Takashi Iwai967303d2013-02-19 17:12:42 +01003856 oldval = snd_hda_codec_get_pin_target(codec, nid);
3857 if (oldval & PIN_IN)
3858 continue; /* no mute for inputs */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003859 /* don't reset VREF value in case it's controlling
3860 * the amp (see alc861_fixup_asus_amp_vref_0f())
3861 */
Takashi Iwai2c12c302013-01-10 09:33:29 +01003862 if (spec->keep_vref_in_automute)
Takashi Iwai967303d2013-02-19 17:12:42 +01003863 val = oldval & ~PIN_HP;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003864 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01003865 val = 0;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003866 if (!mute)
Takashi Iwai967303d2013-02-19 17:12:42 +01003867 val |= oldval;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003868 /* here we call update_pin_ctl() so that the pinctl is changed
3869 * without changing the pinctl target value;
3870 * the original target value will be still referred at the
3871 * init / resume again
3872 */
3873 update_pin_ctl(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01003874 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003875 }
3876}
3877
3878/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003879void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003880{
3881 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003882 int *paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003883 int on;
3884
3885 /* Control HP pins/amps depending on master_mute state;
3886 * in general, HP pins/amps control should be enabled in all cases,
3887 * but currently set only for master_mute, just to be safe
3888 */
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003889 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
3890 paths = spec->out_paths;
3891 else
3892 paths = spec->hp_paths;
Takashi Iwai967303d2013-02-19 17:12:42 +01003893 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003894 spec->autocfg.hp_pins, paths, spec->master_mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003895
3896 if (!spec->automute_speaker)
3897 on = 0;
3898 else
3899 on = spec->hp_jack_present | spec->line_jack_present;
3900 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003901 spec->speaker_muted = on;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003902 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3903 paths = spec->out_paths;
3904 else
3905 paths = spec->speaker_paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003906 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003907 spec->autocfg.speaker_pins, paths, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003908
3909 /* toggle line-out mutes if needed, too */
3910 /* if LO is a copy of either HP or Speaker, don't need to handle it */
3911 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
3912 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
3913 return;
3914 if (!spec->automute_lo)
3915 on = 0;
3916 else
3917 on = spec->hp_jack_present;
3918 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003919 spec->line_out_muted = on;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003920 paths = spec->out_paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003921 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003922 spec->autocfg.line_out_pins, paths, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003923}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003924EXPORT_SYMBOL_GPL(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003925
3926static void call_update_outputs(struct hda_codec *codec)
3927{
3928 struct hda_gen_spec *spec = codec->spec;
3929 if (spec->automute_hook)
3930 spec->automute_hook(codec);
3931 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01003932 snd_hda_gen_update_outputs(codec);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02003933
3934 /* sync the whole vmaster slaves to reflect the new auto-mute status */
3935 if (spec->auto_mute_via_amp && !codec->bus->shutdown)
3936 snd_ctl_sync_vmaster(spec->vmaster_mute.sw_kctl, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003937}
3938
3939/* standard HP-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003940void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003941{
3942 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai92603c52013-01-22 07:46:31 +01003943 hda_nid_t *pins = spec->autocfg.hp_pins;
3944 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003945
Takashi Iwai92603c52013-01-22 07:46:31 +01003946 /* No detection for the first HP jack during indep-HP mode */
3947 if (spec->indep_hp_enabled) {
3948 pins++;
3949 num_pins--;
3950 }
3951
3952 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003953 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
3954 return;
3955 call_update_outputs(codec);
3956}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003957EXPORT_SYMBOL_GPL(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003958
3959/* standard line-out-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003960void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003961{
3962 struct hda_gen_spec *spec = codec->spec;
3963
3964 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3965 return;
3966 /* check LO jack only when it's different from HP */
3967 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
3968 return;
3969
3970 spec->line_jack_present =
3971 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3972 spec->autocfg.line_out_pins);
3973 if (!spec->automute_speaker || !spec->detect_lo)
3974 return;
3975 call_update_outputs(codec);
3976}
Takashi Iwai2698ea92013-12-18 07:45:52 +01003977EXPORT_SYMBOL_GPL(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003978
3979/* standard mic auto-switch helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003980void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003981{
3982 struct hda_gen_spec *spec = codec->spec;
3983 int i;
3984
3985 if (!spec->auto_mic)
3986 return;
3987
3988 for (i = spec->am_num_entries - 1; i > 0; i--) {
Takashi Iwai0b4df932013-01-10 09:45:13 +01003989 hda_nid_t pin = spec->am_entry[i].pin;
3990 /* don't detect pins retasked as outputs */
3991 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
3992 continue;
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02003993 if (snd_hda_jack_detect_state(codec, pin) == HDA_JACK_PRESENT) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003994 mux_select(codec, 0, spec->am_entry[i].idx);
3995 return;
3996 }
3997 }
3998 mux_select(codec, 0, spec->am_entry[0].idx);
3999}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004000EXPORT_SYMBOL_GPL(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004001
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004002/* call appropriate hooks */
4003static void call_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
4004{
4005 struct hda_gen_spec *spec = codec->spec;
4006 if (spec->hp_automute_hook)
4007 spec->hp_automute_hook(codec, jack);
4008 else
4009 snd_hda_gen_hp_automute(codec, jack);
4010}
4011
4012static void call_line_automute(struct hda_codec *codec,
4013 struct hda_jack_tbl *jack)
4014{
4015 struct hda_gen_spec *spec = codec->spec;
4016 if (spec->line_automute_hook)
4017 spec->line_automute_hook(codec, jack);
4018 else
4019 snd_hda_gen_line_automute(codec, jack);
4020}
4021
4022static void call_mic_autoswitch(struct hda_codec *codec,
4023 struct hda_jack_tbl *jack)
4024{
4025 struct hda_gen_spec *spec = codec->spec;
4026 if (spec->mic_autoswitch_hook)
4027 spec->mic_autoswitch_hook(codec, jack);
4028 else
4029 snd_hda_gen_mic_autoswitch(codec, jack);
4030}
4031
Takashi Iwai963afde2013-05-31 15:20:31 +02004032/* update jack retasking */
4033static void update_automute_all(struct hda_codec *codec)
4034{
4035 call_hp_automute(codec, NULL);
4036 call_line_automute(codec, NULL);
4037 call_mic_autoswitch(codec, NULL);
4038}
4039
Takashi Iwai352f7f92012-12-19 12:52:06 +01004040/*
4041 * Auto-Mute mode mixer enum support
4042 */
4043static int automute_mode_info(struct snd_kcontrol *kcontrol,
4044 struct snd_ctl_elem_info *uinfo)
4045{
4046 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4047 struct hda_gen_spec *spec = codec->spec;
4048 static const char * const texts3[] = {
4049 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02004050 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051
Takashi Iwai352f7f92012-12-19 12:52:06 +01004052 if (spec->automute_speaker_possible && spec->automute_lo_possible)
4053 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
4054 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
4055}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056
Takashi Iwai352f7f92012-12-19 12:52:06 +01004057static int automute_mode_get(struct snd_kcontrol *kcontrol,
4058 struct snd_ctl_elem_value *ucontrol)
4059{
4060 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4061 struct hda_gen_spec *spec = codec->spec;
4062 unsigned int val = 0;
4063 if (spec->automute_speaker)
4064 val++;
4065 if (spec->automute_lo)
4066 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004067
Takashi Iwai352f7f92012-12-19 12:52:06 +01004068 ucontrol->value.enumerated.item[0] = val;
4069 return 0;
4070}
4071
4072static int automute_mode_put(struct snd_kcontrol *kcontrol,
4073 struct snd_ctl_elem_value *ucontrol)
4074{
4075 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4076 struct hda_gen_spec *spec = codec->spec;
4077
4078 switch (ucontrol->value.enumerated.item[0]) {
4079 case 0:
4080 if (!spec->automute_speaker && !spec->automute_lo)
4081 return 0;
4082 spec->automute_speaker = 0;
4083 spec->automute_lo = 0;
4084 break;
4085 case 1:
4086 if (spec->automute_speaker_possible) {
4087 if (!spec->automute_lo && spec->automute_speaker)
4088 return 0;
4089 spec->automute_speaker = 1;
4090 spec->automute_lo = 0;
4091 } else if (spec->automute_lo_possible) {
4092 if (spec->automute_lo)
4093 return 0;
4094 spec->automute_lo = 1;
4095 } else
4096 return -EINVAL;
4097 break;
4098 case 2:
4099 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
4100 return -EINVAL;
4101 if (spec->automute_speaker && spec->automute_lo)
4102 return 0;
4103 spec->automute_speaker = 1;
4104 spec->automute_lo = 1;
4105 break;
4106 default:
4107 return -EINVAL;
4108 }
4109 call_update_outputs(codec);
4110 return 1;
4111}
4112
4113static const struct snd_kcontrol_new automute_mode_enum = {
4114 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4115 .name = "Auto-Mute Mode",
4116 .info = automute_mode_info,
4117 .get = automute_mode_get,
4118 .put = automute_mode_put,
4119};
4120
4121static int add_automute_mode_enum(struct hda_codec *codec)
4122{
4123 struct hda_gen_spec *spec = codec->spec;
4124
Takashi Iwai12c93df2012-12-19 14:38:33 +01004125 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01004126 return -ENOMEM;
4127 return 0;
4128}
4129
4130/*
4131 * Check the availability of HP/line-out auto-mute;
4132 * Set up appropriately if really supported
4133 */
4134static int check_auto_mute_availability(struct hda_codec *codec)
4135{
4136 struct hda_gen_spec *spec = codec->spec;
4137 struct auto_pin_cfg *cfg = &spec->autocfg;
4138 int present = 0;
4139 int i, err;
4140
Takashi Iwaif72706b2013-01-16 18:20:07 +01004141 if (spec->suppress_auto_mute)
4142 return 0;
4143
Takashi Iwai352f7f92012-12-19 12:52:06 +01004144 if (cfg->hp_pins[0])
4145 present++;
4146 if (cfg->line_out_pins[0])
4147 present++;
4148 if (cfg->speaker_pins[0])
4149 present++;
4150 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02004151 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004152
4153 if (!cfg->speaker_pins[0] &&
4154 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
4155 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4156 sizeof(cfg->speaker_pins));
4157 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159
Takashi Iwai352f7f92012-12-19 12:52:06 +01004160 if (!cfg->hp_pins[0] &&
4161 cfg->line_out_type == AUTO_PIN_HP_OUT) {
4162 memcpy(cfg->hp_pins, cfg->line_out_pins,
4163 sizeof(cfg->hp_pins));
4164 cfg->hp_outs = cfg->line_outs;
4165 }
4166
4167 for (i = 0; i < cfg->hp_outs; i++) {
4168 hda_nid_t nid = cfg->hp_pins[i];
4169 if (!is_jack_detectable(codec, nid))
4170 continue;
4171 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
4172 nid);
4173 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004174 call_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004175 spec->detect_hp = 1;
4176 }
4177
4178 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
4179 if (cfg->speaker_outs)
4180 for (i = 0; i < cfg->line_outs; i++) {
4181 hda_nid_t nid = cfg->line_out_pins[i];
4182 if (!is_jack_detectable(codec, nid))
4183 continue;
4184 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
4185 snd_hda_jack_detect_enable_callback(codec, nid,
4186 HDA_GEN_FRONT_EVENT,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004187 call_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004188 spec->detect_lo = 1;
4189 }
4190 spec->automute_lo_possible = spec->detect_hp;
4191 }
4192
4193 spec->automute_speaker_possible = cfg->speaker_outs &&
4194 (spec->detect_hp || spec->detect_lo);
4195
4196 spec->automute_lo = spec->automute_lo_possible;
4197 spec->automute_speaker = spec->automute_speaker_possible;
4198
4199 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
4200 /* create a control for automute mode */
4201 err = add_automute_mode_enum(codec);
4202 if (err < 0)
4203 return err;
4204 }
4205 return 0;
4206}
4207
Takashi Iwai352f7f92012-12-19 12:52:06 +01004208/* check whether all auto-mic pins are valid; setup indices if OK */
4209static bool auto_mic_check_imux(struct hda_codec *codec)
4210{
4211 struct hda_gen_spec *spec = codec->spec;
4212 const struct hda_input_mux *imux;
4213 int i;
4214
4215 imux = &spec->input_mux;
4216 for (i = 0; i < spec->am_num_entries; i++) {
4217 spec->am_entry[i].idx =
4218 find_idx_in_nid_list(spec->am_entry[i].pin,
4219 spec->imux_pins, imux->num_items);
4220 if (spec->am_entry[i].idx < 0)
4221 return false; /* no corresponding imux */
4222 }
4223
4224 /* we don't need the jack detection for the first pin */
4225 for (i = 1; i < spec->am_num_entries; i++)
4226 snd_hda_jack_detect_enable_callback(codec,
4227 spec->am_entry[i].pin,
4228 HDA_GEN_MIC_EVENT,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004229 call_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004230 return true;
4231}
4232
4233static int compare_attr(const void *ap, const void *bp)
4234{
4235 const struct automic_entry *a = ap;
4236 const struct automic_entry *b = bp;
4237 return (int)(a->attr - b->attr);
4238}
4239
4240/*
4241 * Check the availability of auto-mic switch;
4242 * Set up if really supported
4243 */
4244static int check_auto_mic_availability(struct hda_codec *codec)
4245{
4246 struct hda_gen_spec *spec = codec->spec;
4247 struct auto_pin_cfg *cfg = &spec->autocfg;
4248 unsigned int types;
4249 int i, num_pins;
4250
Takashi Iwaid12daf62013-01-07 16:32:11 +01004251 if (spec->suppress_auto_mic)
4252 return 0;
4253
Takashi Iwai352f7f92012-12-19 12:52:06 +01004254 types = 0;
4255 num_pins = 0;
4256 for (i = 0; i < cfg->num_inputs; i++) {
4257 hda_nid_t nid = cfg->inputs[i].pin;
4258 unsigned int attr;
4259 attr = snd_hda_codec_get_pincfg(codec, nid);
4260 attr = snd_hda_get_input_pin_attr(attr);
4261 if (types & (1 << attr))
4262 return 0; /* already occupied */
4263 switch (attr) {
4264 case INPUT_PIN_ATTR_INT:
4265 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4266 return 0; /* invalid type */
4267 break;
4268 case INPUT_PIN_ATTR_UNUSED:
4269 return 0; /* invalid entry */
4270 default:
4271 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4272 return 0; /* invalid type */
4273 if (!spec->line_in_auto_switch &&
4274 cfg->inputs[i].type != AUTO_PIN_MIC)
4275 return 0; /* only mic is allowed */
4276 if (!is_jack_detectable(codec, nid))
4277 return 0; /* no unsol support */
4278 break;
4279 }
4280 if (num_pins >= MAX_AUTO_MIC_PINS)
4281 return 0;
4282 types |= (1 << attr);
4283 spec->am_entry[num_pins].pin = nid;
4284 spec->am_entry[num_pins].attr = attr;
4285 num_pins++;
4286 }
4287
4288 if (num_pins < 2)
4289 return 0;
4290
4291 spec->am_num_entries = num_pins;
4292 /* sort the am_entry in the order of attr so that the pin with a
4293 * higher attr will be selected when the jack is plugged.
4294 */
4295 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4296 compare_attr, NULL);
4297
4298 if (!auto_mic_check_imux(codec))
4299 return 0;
4300
4301 spec->auto_mic = 1;
4302 spec->num_adc_nids = 1;
4303 spec->cur_mux[0] = spec->am_entry[0].idx;
4304 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
4305 spec->am_entry[0].pin,
4306 spec->am_entry[1].pin,
4307 spec->am_entry[2].pin);
4308
4309 return 0;
4310}
4311
Takashi Iwai55196ff2013-01-24 17:32:56 +01004312/* power_filter hook; make inactive widgets into power down */
4313static unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
4314 hda_nid_t nid,
4315 unsigned int power_state)
4316{
4317 if (power_state != AC_PWRST_D0)
4318 return power_state;
4319 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4320 return power_state;
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +02004321 if (is_active_nid_for_any(codec, nid))
Takashi Iwai55196ff2013-01-24 17:32:56 +01004322 return power_state;
4323 return AC_PWRST_D3;
4324}
4325
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004326/* mute all aamix inputs initially; parse up to the first leaves */
4327static void mute_all_mixer_nid(struct hda_codec *codec, hda_nid_t mix)
4328{
4329 int i, nums;
4330 const hda_nid_t *conn;
4331 bool has_amp;
4332
4333 nums = snd_hda_get_conn_list(codec, mix, &conn);
4334 has_amp = nid_has_mute(codec, mix, HDA_INPUT);
4335 for (i = 0; i < nums; i++) {
4336 if (has_amp)
4337 snd_hda_codec_amp_stereo(codec, mix,
4338 HDA_INPUT, i,
4339 0xff, HDA_AMP_MUTE);
4340 else if (nid_has_volume(codec, conn[i], HDA_OUTPUT))
4341 snd_hda_codec_amp_stereo(codec, conn[i],
4342 HDA_OUTPUT, 0,
4343 0xff, HDA_AMP_MUTE);
4344 }
4345}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004346
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004347/*
4348 * Parse the given BIOS configuration and set up the hda_gen_spec
4349 *
4350 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004351 * or a negative error code
4352 */
4353int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004354 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004355{
4356 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004357 int err;
4358
Takashi Iwai1c70a582013-01-11 17:48:22 +01004359 parse_user_hints(codec);
4360
Takashi Iwaie4a395e2013-01-23 17:00:31 +01004361 if (spec->mixer_nid && !spec->mixer_merge_nid)
4362 spec->mixer_merge_nid = spec->mixer_nid;
4363
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004364 if (cfg != &spec->autocfg) {
4365 spec->autocfg = *cfg;
4366 cfg = &spec->autocfg;
4367 }
4368
Takashi Iwai98bd1112013-03-22 14:53:50 +01004369 if (!spec->main_out_badness)
4370 spec->main_out_badness = &hda_main_out_badness;
4371 if (!spec->extra_out_badness)
4372 spec->extra_out_badness = &hda_extra_out_badness;
4373
David Henningsson6fc4cb92013-01-16 15:58:45 +01004374 fill_all_dac_nids(codec);
4375
Takashi Iwai352f7f92012-12-19 12:52:06 +01004376 if (!cfg->line_outs) {
4377 if (cfg->dig_outs || cfg->dig_in_pin) {
4378 spec->multiout.max_channels = 2;
4379 spec->no_analog = 1;
4380 goto dig_only;
4381 }
Takashi Iwaic9e4bdb2013-12-06 09:30:14 +01004382 if (!cfg->num_inputs && !cfg->dig_in_pin)
4383 return 0; /* can't find valid BIOS pin config */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004384 }
4385
4386 if (!spec->no_primary_hp &&
4387 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
4388 cfg->line_outs <= cfg->hp_outs) {
4389 /* use HP as primary out */
4390 cfg->speaker_outs = cfg->line_outs;
4391 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4392 sizeof(cfg->speaker_pins));
4393 cfg->line_outs = cfg->hp_outs;
4394 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4395 cfg->hp_outs = 0;
4396 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4397 cfg->line_out_type = AUTO_PIN_HP_OUT;
4398 }
4399
4400 err = parse_output_paths(codec);
4401 if (err < 0)
4402 return err;
4403 err = create_multi_channel_mode(codec);
4404 if (err < 0)
4405 return err;
4406 err = create_multi_out_ctls(codec, cfg);
4407 if (err < 0)
4408 return err;
4409 err = create_hp_out_ctls(codec);
4410 if (err < 0)
4411 return err;
4412 err = create_speaker_out_ctls(codec);
4413 if (err < 0)
4414 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004415 err = create_indep_hp_ctls(codec);
4416 if (err < 0)
4417 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01004418 err = create_loopback_mixing_ctl(codec);
4419 if (err < 0)
4420 return err;
Takashi Iwai967303d2013-02-19 17:12:42 +01004421 err = create_hp_mic(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004422 if (err < 0)
4423 return err;
4424 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02004425 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02004426 return err;
4427
Takashi Iwaia07a9492013-01-07 16:44:06 +01004428 spec->const_channel_count = spec->ext_channel_count;
4429 /* check the multiple speaker and headphone pins */
4430 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4431 spec->const_channel_count = max(spec->const_channel_count,
4432 cfg->speaker_outs * 2);
4433 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4434 spec->const_channel_count = max(spec->const_channel_count,
4435 cfg->hp_outs * 2);
4436 spec->multiout.max_channels = max(spec->ext_channel_count,
4437 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004438
4439 err = check_auto_mute_availability(codec);
4440 if (err < 0)
4441 return err;
4442
4443 err = check_dyn_adc_switch(codec);
4444 if (err < 0)
4445 return err;
4446
Takashi Iwai967303d2013-02-19 17:12:42 +01004447 err = check_auto_mic_availability(codec);
4448 if (err < 0)
4449 return err;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004450
Takashi Iwaif1e762d2013-12-09 16:02:24 +01004451 /* add stereo mix if available and not enabled yet */
4452 if (!spec->auto_mic && spec->mixer_nid &&
4453 spec->add_stereo_mix_input &&
4454 spec->input_mux.num_items > 1 &&
4455 snd_hda_get_bool_hint(codec, "add_stereo_mix_input") < 0) {
4456 err = parse_capture_source(codec, spec->mixer_nid,
4457 CFG_IDX_MIX, spec->num_all_adcs,
4458 "Stereo Mix", 0);
4459 if (err < 0)
4460 return err;
4461 }
4462
4463
Takashi Iwai352f7f92012-12-19 12:52:06 +01004464 err = create_capture_mixers(codec);
4465 if (err < 0)
4466 return err;
4467
4468 err = parse_mic_boost(codec);
4469 if (err < 0)
4470 return err;
4471
Takashi Iwaiced4cef2013-11-26 08:33:45 +01004472 /* create "Headphone Mic Jack Mode" if no input selection is
4473 * available (or user specifies add_jack_modes hint)
4474 */
4475 if (spec->hp_mic_pin &&
4476 (spec->auto_mic || spec->input_mux.num_items == 1 ||
4477 spec->add_jack_modes)) {
4478 err = create_hp_mic_jack_mode(codec, spec->hp_mic_pin);
4479 if (err < 0)
4480 return err;
4481 }
4482
Takashi Iwaif811c3c2013-03-07 18:32:59 +01004483 if (spec->add_jack_modes) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01004484 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4485 err = create_out_jack_modes(codec, cfg->line_outs,
4486 cfg->line_out_pins);
4487 if (err < 0)
4488 return err;
4489 }
4490 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4491 err = create_out_jack_modes(codec, cfg->hp_outs,
4492 cfg->hp_pins);
4493 if (err < 0)
4494 return err;
4495 }
4496 }
4497
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004498 /* mute all aamix input initially */
4499 if (spec->mixer_nid)
4500 mute_all_mixer_nid(codec, spec->mixer_nid);
4501
Takashi Iwai352f7f92012-12-19 12:52:06 +01004502 dig_only:
4503 parse_digital(codec);
4504
Takashi Iwai55196ff2013-01-24 17:32:56 +01004505 if (spec->power_down_unused)
4506 codec->power_filter = snd_hda_gen_path_power_filter;
4507
Takashi Iwai7504b6c2013-03-18 11:25:51 +01004508 if (!spec->no_analog && spec->beep_nid) {
4509 err = snd_hda_attach_beep_device(codec, spec->beep_nid);
4510 if (err < 0)
4511 return err;
4512 }
4513
Takashi Iwai352f7f92012-12-19 12:52:06 +01004514 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004516EXPORT_SYMBOL_GPL(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517
4518
4519/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004520 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004522
4523/* slave controls for virtual master */
4524static const char * const slave_pfxs[] = {
4525 "Front", "Surround", "Center", "LFE", "Side",
4526 "Headphone", "Speaker", "Mono", "Line Out",
4527 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01004528 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
4529 "Headphone Front", "Headphone Surround", "Headphone CLFE",
4530 "Headphone Side",
Takashi Iwai352f7f92012-12-19 12:52:06 +01004531 NULL,
4532};
4533
4534int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004536 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538
Takashi Iwai36502d02012-12-19 15:15:10 +01004539 if (spec->kctls.used) {
4540 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
4541 if (err < 0)
4542 return err;
4543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544
Takashi Iwai352f7f92012-12-19 12:52:06 +01004545 if (spec->multiout.dig_out_nid) {
4546 err = snd_hda_create_dig_out_ctls(codec,
4547 spec->multiout.dig_out_nid,
4548 spec->multiout.dig_out_nid,
4549 spec->pcm_rec[1].pcm_type);
4550 if (err < 0)
4551 return err;
4552 if (!spec->no_analog) {
4553 err = snd_hda_create_spdif_share_sw(codec,
4554 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555 if (err < 0)
4556 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004557 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 }
4559 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004560 if (spec->dig_in_nid) {
4561 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
4562 if (err < 0)
4563 return err;
4564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565
Takashi Iwai352f7f92012-12-19 12:52:06 +01004566 /* if we have no master control, let's create it */
4567 if (!spec->no_analog &&
4568 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004569 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01004570 spec->vmaster_tlv, slave_pfxs,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004571 "Playback Volume");
4572 if (err < 0)
4573 return err;
4574 }
4575 if (!spec->no_analog &&
4576 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
4577 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
4578 NULL, slave_pfxs,
4579 "Playback Switch",
4580 true, &spec->vmaster_mute.sw_kctl);
4581 if (err < 0)
4582 return err;
Takashi Iwaib63eae02013-10-25 23:43:10 +02004583 if (spec->vmaster_mute.hook) {
Takashi Iwaifd25a972012-12-20 14:57:18 +01004584 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
4585 spec->vmaster_mute_enum);
Takashi Iwaib63eae02013-10-25 23:43:10 +02004586 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
4587 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589
Takashi Iwai352f7f92012-12-19 12:52:06 +01004590 free_kctls(spec); /* no longer needed */
4591
Takashi Iwai352f7f92012-12-19 12:52:06 +01004592 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
4593 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594 return err;
4595
4596 return 0;
4597}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004598EXPORT_SYMBOL_GPL(snd_hda_gen_build_controls);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004599
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600
4601/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004602 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004605static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
4606 struct hda_codec *codec,
4607 struct snd_pcm_substream *substream,
4608 int action)
4609{
4610 struct hda_gen_spec *spec = codec->spec;
4611 if (spec->pcm_playback_hook)
4612 spec->pcm_playback_hook(hinfo, codec, substream, action);
4613}
4614
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004615static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
4616 struct hda_codec *codec,
4617 struct snd_pcm_substream *substream,
4618 int action)
4619{
4620 struct hda_gen_spec *spec = codec->spec;
4621 if (spec->pcm_capture_hook)
4622 spec->pcm_capture_hook(hinfo, codec, substream, action);
4623}
4624
Takashi Iwai352f7f92012-12-19 12:52:06 +01004625/*
4626 * Analog playback callbacks
4627 */
4628static int playback_pcm_open(struct hda_pcm_stream *hinfo,
4629 struct hda_codec *codec,
4630 struct snd_pcm_substream *substream)
4631{
4632 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004633 int err;
4634
4635 mutex_lock(&spec->pcm_mutex);
4636 err = snd_hda_multi_out_analog_open(codec,
4637 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004638 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004639 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004640 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004641 call_pcm_playback_hook(hinfo, codec, substream,
4642 HDA_GEN_PCM_ACT_OPEN);
4643 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004644 mutex_unlock(&spec->pcm_mutex);
4645 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004646}
4647
4648static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01004649 struct hda_codec *codec,
4650 unsigned int stream_tag,
4651 unsigned int format,
4652 struct snd_pcm_substream *substream)
4653{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004654 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004655 int err;
4656
4657 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
4658 stream_tag, format, substream);
4659 if (!err)
4660 call_pcm_playback_hook(hinfo, codec, substream,
4661 HDA_GEN_PCM_ACT_PREPARE);
4662 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004663}
Takashi Iwai97ec5582006-03-21 11:29:07 +01004664
Takashi Iwai352f7f92012-12-19 12:52:06 +01004665static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4666 struct hda_codec *codec,
4667 struct snd_pcm_substream *substream)
4668{
4669 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004670 int err;
4671
4672 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
4673 if (!err)
4674 call_pcm_playback_hook(hinfo, codec, substream,
4675 HDA_GEN_PCM_ACT_CLEANUP);
4676 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004677}
4678
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004679static int playback_pcm_close(struct hda_pcm_stream *hinfo,
4680 struct hda_codec *codec,
4681 struct snd_pcm_substream *substream)
4682{
4683 struct hda_gen_spec *spec = codec->spec;
4684 mutex_lock(&spec->pcm_mutex);
4685 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004686 call_pcm_playback_hook(hinfo, codec, substream,
4687 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004688 mutex_unlock(&spec->pcm_mutex);
4689 return 0;
4690}
4691
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004692static int capture_pcm_open(struct hda_pcm_stream *hinfo,
4693 struct hda_codec *codec,
4694 struct snd_pcm_substream *substream)
4695{
4696 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
4697 return 0;
4698}
4699
4700static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4701 struct hda_codec *codec,
4702 unsigned int stream_tag,
4703 unsigned int format,
4704 struct snd_pcm_substream *substream)
4705{
4706 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4707 call_pcm_capture_hook(hinfo, codec, substream,
4708 HDA_GEN_PCM_ACT_PREPARE);
4709 return 0;
4710}
4711
4712static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4713 struct hda_codec *codec,
4714 struct snd_pcm_substream *substream)
4715{
4716 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4717 call_pcm_capture_hook(hinfo, codec, substream,
4718 HDA_GEN_PCM_ACT_CLEANUP);
4719 return 0;
4720}
4721
4722static int capture_pcm_close(struct hda_pcm_stream *hinfo,
4723 struct hda_codec *codec,
4724 struct snd_pcm_substream *substream)
4725{
4726 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
4727 return 0;
4728}
4729
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004730static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
4731 struct hda_codec *codec,
4732 struct snd_pcm_substream *substream)
4733{
4734 struct hda_gen_spec *spec = codec->spec;
4735 int err = 0;
4736
4737 mutex_lock(&spec->pcm_mutex);
4738 if (!spec->indep_hp_enabled)
4739 err = -EBUSY;
4740 else
4741 spec->active_streams |= 1 << STREAM_INDEP_HP;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004742 call_pcm_playback_hook(hinfo, codec, substream,
4743 HDA_GEN_PCM_ACT_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004744 mutex_unlock(&spec->pcm_mutex);
4745 return err;
4746}
4747
4748static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
4749 struct hda_codec *codec,
4750 struct snd_pcm_substream *substream)
4751{
4752 struct hda_gen_spec *spec = codec->spec;
4753 mutex_lock(&spec->pcm_mutex);
4754 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004755 call_pcm_playback_hook(hinfo, codec, substream,
4756 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004757 mutex_unlock(&spec->pcm_mutex);
4758 return 0;
4759}
4760
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004761static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4762 struct hda_codec *codec,
4763 unsigned int stream_tag,
4764 unsigned int format,
4765 struct snd_pcm_substream *substream)
4766{
4767 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4768 call_pcm_playback_hook(hinfo, codec, substream,
4769 HDA_GEN_PCM_ACT_PREPARE);
4770 return 0;
4771}
4772
4773static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4774 struct hda_codec *codec,
4775 struct snd_pcm_substream *substream)
4776{
4777 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4778 call_pcm_playback_hook(hinfo, codec, substream,
4779 HDA_GEN_PCM_ACT_CLEANUP);
4780 return 0;
4781}
4782
Takashi Iwai352f7f92012-12-19 12:52:06 +01004783/*
4784 * Digital out
4785 */
4786static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
4787 struct hda_codec *codec,
4788 struct snd_pcm_substream *substream)
4789{
4790 struct hda_gen_spec *spec = codec->spec;
4791 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
4792}
4793
4794static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4795 struct hda_codec *codec,
4796 unsigned int stream_tag,
4797 unsigned int format,
4798 struct snd_pcm_substream *substream)
4799{
4800 struct hda_gen_spec *spec = codec->spec;
4801 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
4802 stream_tag, format, substream);
4803}
4804
4805static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4806 struct hda_codec *codec,
4807 struct snd_pcm_substream *substream)
4808{
4809 struct hda_gen_spec *spec = codec->spec;
4810 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
4811}
4812
4813static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
4814 struct hda_codec *codec,
4815 struct snd_pcm_substream *substream)
4816{
4817 struct hda_gen_spec *spec = codec->spec;
4818 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
4819}
4820
4821/*
4822 * Analog capture
4823 */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004824#define alt_capture_pcm_open capture_pcm_open
4825#define alt_capture_pcm_close capture_pcm_close
4826
Takashi Iwai352f7f92012-12-19 12:52:06 +01004827static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4828 struct hda_codec *codec,
4829 unsigned int stream_tag,
4830 unsigned int format,
4831 struct snd_pcm_substream *substream)
4832{
4833 struct hda_gen_spec *spec = codec->spec;
4834
4835 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01004836 stream_tag, 0, format);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004837 call_pcm_capture_hook(hinfo, codec, substream,
4838 HDA_GEN_PCM_ACT_PREPARE);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004839 return 0;
4840}
4841
Takashi Iwai352f7f92012-12-19 12:52:06 +01004842static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4843 struct hda_codec *codec,
4844 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01004845{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004846 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01004847
Takashi Iwai352f7f92012-12-19 12:52:06 +01004848 snd_hda_codec_cleanup_stream(codec,
4849 spec->adc_nids[substream->number + 1]);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004850 call_pcm_capture_hook(hinfo, codec, substream,
4851 HDA_GEN_PCM_ACT_CLEANUP);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004852 return 0;
4853}
4854
Takashi Iwai352f7f92012-12-19 12:52:06 +01004855/*
4856 */
4857static const struct hda_pcm_stream pcm_analog_playback = {
4858 .substreams = 1,
4859 .channels_min = 2,
4860 .channels_max = 8,
4861 /* NID is set in build_pcms */
4862 .ops = {
4863 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004864 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004865 .prepare = playback_pcm_prepare,
4866 .cleanup = playback_pcm_cleanup
4867 },
4868};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869
Takashi Iwai352f7f92012-12-19 12:52:06 +01004870static const struct hda_pcm_stream pcm_analog_capture = {
4871 .substreams = 1,
4872 .channels_min = 2,
4873 .channels_max = 2,
4874 /* NID is set in build_pcms */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004875 .ops = {
4876 .open = capture_pcm_open,
4877 .close = capture_pcm_close,
4878 .prepare = capture_pcm_prepare,
4879 .cleanup = capture_pcm_cleanup
4880 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004881};
4882
4883static const struct hda_pcm_stream pcm_analog_alt_playback = {
4884 .substreams = 1,
4885 .channels_min = 2,
4886 .channels_max = 2,
4887 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004888 .ops = {
4889 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004890 .close = alt_playback_pcm_close,
4891 .prepare = alt_playback_pcm_prepare,
4892 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004893 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004894};
4895
4896static const struct hda_pcm_stream pcm_analog_alt_capture = {
4897 .substreams = 2, /* can be overridden */
4898 .channels_min = 2,
4899 .channels_max = 2,
4900 /* NID is set in build_pcms */
4901 .ops = {
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004902 .open = alt_capture_pcm_open,
4903 .close = alt_capture_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004904 .prepare = alt_capture_pcm_prepare,
4905 .cleanup = alt_capture_pcm_cleanup
4906 },
4907};
4908
4909static const struct hda_pcm_stream pcm_digital_playback = {
4910 .substreams = 1,
4911 .channels_min = 2,
4912 .channels_max = 2,
4913 /* NID is set in build_pcms */
4914 .ops = {
4915 .open = dig_playback_pcm_open,
4916 .close = dig_playback_pcm_close,
4917 .prepare = dig_playback_pcm_prepare,
4918 .cleanup = dig_playback_pcm_cleanup
4919 },
4920};
4921
4922static const struct hda_pcm_stream pcm_digital_capture = {
4923 .substreams = 1,
4924 .channels_min = 2,
4925 .channels_max = 2,
4926 /* NID is set in build_pcms */
4927};
4928
4929/* Used by build_pcms to flag that a PCM has no playback stream */
4930static const struct hda_pcm_stream pcm_null_stream = {
4931 .substreams = 0,
4932 .channels_min = 0,
4933 .channels_max = 0,
4934};
4935
4936/*
4937 * dynamic changing ADC PCM streams
4938 */
4939static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
4940{
4941 struct hda_gen_spec *spec = codec->spec;
4942 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
4943
4944 if (spec->cur_adc && spec->cur_adc != new_adc) {
4945 /* stream is running, let's swap the current ADC */
4946 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
4947 spec->cur_adc = new_adc;
4948 snd_hda_codec_setup_stream(codec, new_adc,
4949 spec->cur_adc_stream_tag, 0,
4950 spec->cur_adc_format);
4951 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004953 return false;
4954}
4955
4956/* analog capture with dynamic dual-adc changes */
4957static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4958 struct hda_codec *codec,
4959 unsigned int stream_tag,
4960 unsigned int format,
4961 struct snd_pcm_substream *substream)
4962{
4963 struct hda_gen_spec *spec = codec->spec;
4964 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
4965 spec->cur_adc_stream_tag = stream_tag;
4966 spec->cur_adc_format = format;
4967 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
4968 return 0;
4969}
4970
4971static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4972 struct hda_codec *codec,
4973 struct snd_pcm_substream *substream)
4974{
4975 struct hda_gen_spec *spec = codec->spec;
4976 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
4977 spec->cur_adc = 0;
4978 return 0;
4979}
4980
4981static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
4982 .substreams = 1,
4983 .channels_min = 2,
4984 .channels_max = 2,
4985 .nid = 0, /* fill later */
4986 .ops = {
4987 .prepare = dyn_adc_capture_pcm_prepare,
4988 .cleanup = dyn_adc_capture_pcm_cleanup
4989 },
4990};
4991
Takashi Iwaif873e532012-12-20 16:58:39 +01004992static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
4993 const char *chip_name)
4994{
4995 char *p;
4996
4997 if (*str)
4998 return;
4999 strlcpy(str, chip_name, len);
5000
5001 /* drop non-alnum chars after a space */
5002 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
5003 if (!isalnum(p[1])) {
5004 *p = 0;
5005 break;
5006 }
5007 }
5008 strlcat(str, sfx, len);
5009}
5010
Takashi Iwai352f7f92012-12-19 12:52:06 +01005011/* build PCM streams based on the parsed results */
5012int snd_hda_gen_build_pcms(struct hda_codec *codec)
5013{
5014 struct hda_gen_spec *spec = codec->spec;
5015 struct hda_pcm *info = spec->pcm_rec;
5016 const struct hda_pcm_stream *p;
5017 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018
5019 codec->num_pcms = 1;
5020 codec->pcm_info = info;
5021
Takashi Iwai352f7f92012-12-19 12:52:06 +01005022 if (spec->no_analog)
5023 goto skip_analog;
5024
Takashi Iwaif873e532012-12-20 16:58:39 +01005025 fill_pcm_stream_name(spec->stream_name_analog,
5026 sizeof(spec->stream_name_analog),
5027 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005028 info->name = spec->stream_name_analog;
5029
5030 if (spec->multiout.num_dacs > 0) {
5031 p = spec->stream_analog_playback;
5032 if (!p)
5033 p = &pcm_analog_playback;
5034 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
5035 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
5036 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
5037 spec->multiout.max_channels;
5038 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
5039 spec->autocfg.line_outs == 2)
5040 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
5041 snd_pcm_2_1_chmaps;
5042 }
5043 if (spec->num_adc_nids) {
5044 p = spec->stream_analog_capture;
5045 if (!p) {
5046 if (spec->dyn_adc_switch)
5047 p = &dyn_adc_pcm_analog_capture;
5048 else
5049 p = &pcm_analog_capture;
5050 }
5051 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
5052 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
5053 }
5054
Takashi Iwai352f7f92012-12-19 12:52:06 +01005055 skip_analog:
5056 /* SPDIF for stream index #1 */
5057 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01005058 fill_pcm_stream_name(spec->stream_name_digital,
5059 sizeof(spec->stream_name_digital),
5060 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005061 codec->num_pcms = 2;
5062 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
5063 info = spec->pcm_rec + 1;
5064 info->name = spec->stream_name_digital;
5065 if (spec->dig_out_type)
5066 info->pcm_type = spec->dig_out_type;
5067 else
5068 info->pcm_type = HDA_PCM_TYPE_SPDIF;
5069 if (spec->multiout.dig_out_nid) {
5070 p = spec->stream_digital_playback;
5071 if (!p)
5072 p = &pcm_digital_playback;
5073 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
5074 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
5075 }
5076 if (spec->dig_in_nid) {
5077 p = spec->stream_digital_capture;
5078 if (!p)
5079 p = &pcm_digital_capture;
5080 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
5081 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
5082 }
5083 }
5084
5085 if (spec->no_analog)
5086 return 0;
5087
5088 /* If the use of more than one ADC is requested for the current
5089 * model, configure a second analog capture-only PCM.
5090 */
5091 have_multi_adcs = (spec->num_adc_nids > 1) &&
5092 !spec->dyn_adc_switch && !spec->auto_mic;
5093 /* Additional Analaog capture for index #2 */
5094 if (spec->alt_dac_nid || have_multi_adcs) {
Takashi Iwaia6071482013-01-21 16:50:09 +01005095 fill_pcm_stream_name(spec->stream_name_alt_analog,
5096 sizeof(spec->stream_name_alt_analog),
5097 " Alt Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005098 codec->num_pcms = 3;
5099 info = spec->pcm_rec + 2;
Takashi Iwaia6071482013-01-21 16:50:09 +01005100 info->name = spec->stream_name_alt_analog;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005101 if (spec->alt_dac_nid) {
5102 p = spec->stream_analog_alt_playback;
5103 if (!p)
5104 p = &pcm_analog_alt_playback;
5105 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
5106 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
5107 spec->alt_dac_nid;
5108 } else {
5109 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
5110 pcm_null_stream;
5111 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
5112 }
5113 if (have_multi_adcs) {
5114 p = spec->stream_analog_alt_capture;
5115 if (!p)
5116 p = &pcm_analog_alt_capture;
5117 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
5118 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
5119 spec->adc_nids[1];
5120 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
5121 spec->num_adc_nids - 1;
5122 } else {
5123 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
5124 pcm_null_stream;
5125 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
5126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127 }
5128
5129 return 0;
5130}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005131EXPORT_SYMBOL_GPL(snd_hda_gen_build_pcms);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005132
5133
5134/*
5135 * Standard auto-parser initializations
5136 */
5137
Takashi Iwaid4156932013-01-07 10:08:02 +01005138/* configure the given path as a proper output */
Takashi Iwai2c12c302013-01-10 09:33:29 +01005139static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005140{
5141 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01005142 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005143
Takashi Iwai196c17662013-01-04 15:01:40 +01005144 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01005145 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005146 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01005147 pin = path->path[path->depth - 1];
Takashi Iwai2c12c302013-01-10 09:33:29 +01005148 restore_pin_ctl(codec, pin);
Takashi Iwai65033cc2013-04-16 12:31:05 +02005149 snd_hda_activate_path(codec, path, path->active,
5150 aamix_default(codec->spec));
Takashi Iwaie1284af2013-01-03 16:33:02 +01005151 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005152}
5153
5154/* initialize primary output paths */
5155static void init_multi_out(struct hda_codec *codec)
5156{
5157 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005158 int i;
5159
Takashi Iwaid4156932013-01-07 10:08:02 +01005160 for (i = 0; i < spec->autocfg.line_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005161 set_output_and_unmute(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005162}
5163
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005164
Takashi Iwai2c12c302013-01-10 09:33:29 +01005165static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005166{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005167 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005168
Takashi Iwaid4156932013-01-07 10:08:02 +01005169 for (i = 0; i < num_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005170 set_output_and_unmute(codec, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005171}
5172
5173/* initialize hp and speaker paths */
5174static void init_extra_out(struct hda_codec *codec)
5175{
5176 struct hda_gen_spec *spec = codec->spec;
5177
5178 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005179 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005180 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
5181 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai2c12c302013-01-10 09:33:29 +01005182 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005183}
5184
5185/* initialize multi-io paths */
5186static void init_multi_io(struct hda_codec *codec)
5187{
5188 struct hda_gen_spec *spec = codec->spec;
5189 int i;
5190
5191 for (i = 0; i < spec->multi_ios; i++) {
5192 hda_nid_t pin = spec->multi_io[i].pin;
5193 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01005194 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005195 if (!path)
5196 continue;
5197 if (!spec->multi_io[i].ctl_in)
5198 spec->multi_io[i].ctl_in =
Takashi Iwai2c12c302013-01-10 09:33:29 +01005199 snd_hda_codec_get_pin_target(codec, pin);
Takashi Iwai65033cc2013-04-16 12:31:05 +02005200 snd_hda_activate_path(codec, path, path->active,
5201 aamix_default(spec));
Takashi Iwai352f7f92012-12-19 12:52:06 +01005202 }
5203}
5204
Takashi Iwai4f7f67f2013-12-03 10:51:37 +01005205static void init_aamix_paths(struct hda_codec *codec)
5206{
5207 struct hda_gen_spec *spec = codec->spec;
5208
5209 if (!spec->have_aamix_ctl)
5210 return;
5211 update_aamix_paths(codec, spec->aamix_mode, spec->out_paths[0],
5212 spec->aamix_out_paths[0],
5213 spec->autocfg.line_out_type);
5214 update_aamix_paths(codec, spec->aamix_mode, spec->hp_paths[0],
5215 spec->aamix_out_paths[1],
5216 AUTO_PIN_HP_OUT);
5217 update_aamix_paths(codec, spec->aamix_mode, spec->speaker_paths[0],
5218 spec->aamix_out_paths[2],
5219 AUTO_PIN_SPEAKER_OUT);
5220}
5221
Takashi Iwai352f7f92012-12-19 12:52:06 +01005222/* set up input pins and loopback paths */
5223static void init_analog_input(struct hda_codec *codec)
5224{
5225 struct hda_gen_spec *spec = codec->spec;
5226 struct auto_pin_cfg *cfg = &spec->autocfg;
5227 int i;
5228
5229 for (i = 0; i < cfg->num_inputs; i++) {
5230 hda_nid_t nid = cfg->inputs[i].pin;
5231 if (is_input_pin(codec, nid))
Takashi Iwai2c12c302013-01-10 09:33:29 +01005232 restore_pin_ctl(codec, nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005233
5234 /* init loopback inputs */
5235 if (spec->mixer_nid) {
Takashi Iwai3e367f12013-01-23 17:07:23 +01005236 resume_path_from_idx(codec, spec->loopback_paths[i]);
5237 resume_path_from_idx(codec, spec->loopback_merge_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005238 }
5239 }
5240}
5241
5242/* initialize ADC paths */
5243static void init_input_src(struct hda_codec *codec)
5244{
5245 struct hda_gen_spec *spec = codec->spec;
5246 struct hda_input_mux *imux = &spec->input_mux;
5247 struct nid_path *path;
5248 int i, c, nums;
5249
5250 if (spec->dyn_adc_switch)
5251 nums = 1;
5252 else
5253 nums = spec->num_adc_nids;
5254
5255 for (c = 0; c < nums; c++) {
5256 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01005257 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005258 if (path) {
5259 bool active = path->active;
5260 if (i == spec->cur_mux[c])
5261 active = true;
5262 snd_hda_activate_path(codec, path, active, false);
5263 }
5264 }
Takashi Iwai967303d2013-02-19 17:12:42 +01005265 if (spec->hp_mic)
5266 update_hp_mic(codec, c, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005267 }
5268
Takashi Iwai352f7f92012-12-19 12:52:06 +01005269 if (spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01005270 spec->cap_sync_hook(codec, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005271}
5272
5273/* set right pin controls for digital I/O */
5274static void init_digital(struct hda_codec *codec)
5275{
5276 struct hda_gen_spec *spec = codec->spec;
5277 int i;
5278 hda_nid_t pin;
5279
Takashi Iwaid4156932013-01-07 10:08:02 +01005280 for (i = 0; i < spec->autocfg.dig_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005281 set_output_and_unmute(codec, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005282 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005283 if (pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01005284 restore_pin_ctl(codec, pin);
Takashi Iwai3e367f12013-01-23 17:07:23 +01005285 resume_path_from_idx(codec, spec->digin_path);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005286 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005287}
5288
Takashi Iwai973e4972012-12-20 15:16:09 +01005289/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5290 * invalid unsol tags by some reason
5291 */
5292static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5293{
5294 int i;
5295
5296 for (i = 0; i < codec->init_pins.used; i++) {
5297 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
5298 hda_nid_t nid = pin->nid;
5299 if (is_jack_detectable(codec, nid) &&
5300 !snd_hda_jack_tbl_get(codec, nid))
5301 snd_hda_codec_update_cache(codec, nid, 0,
5302 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5303 }
5304}
5305
Takashi Iwai5187ac12013-01-07 12:52:16 +01005306/*
5307 * initialize the generic spec;
5308 * this can be put as patch_ops.init function
5309 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005310int snd_hda_gen_init(struct hda_codec *codec)
5311{
5312 struct hda_gen_spec *spec = codec->spec;
5313
5314 if (spec->init_hook)
5315 spec->init_hook(codec);
5316
5317 snd_hda_apply_verbs(codec);
5318
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005319 codec->cached_write = 1;
5320
Takashi Iwai352f7f92012-12-19 12:52:06 +01005321 init_multi_out(codec);
5322 init_extra_out(codec);
5323 init_multi_io(codec);
Takashi Iwai4f7f67f2013-12-03 10:51:37 +01005324 init_aamix_paths(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005325 init_analog_input(codec);
5326 init_input_src(codec);
5327 init_digital(codec);
5328
Takashi Iwai973e4972012-12-20 15:16:09 +01005329 clear_unsol_on_unused_pins(codec);
5330
Takashi Iwai352f7f92012-12-19 12:52:06 +01005331 /* call init functions of standard auto-mute helpers */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01005332 update_automute_all(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005333
Takashi Iwaidc870f32013-01-22 15:24:30 +01005334 snd_hda_codec_flush_cache(codec);
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005335
Takashi Iwai352f7f92012-12-19 12:52:06 +01005336 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
5337 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5338
5339 hda_call_check_power_status(codec, 0x01);
5340 return 0;
5341}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005342EXPORT_SYMBOL_GPL(snd_hda_gen_init);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005343
Takashi Iwai5187ac12013-01-07 12:52:16 +01005344/*
5345 * free the generic spec;
5346 * this can be put as patch_ops.free function
5347 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005348void snd_hda_gen_free(struct hda_codec *codec)
5349{
Takashi Iwai7504b6c2013-03-18 11:25:51 +01005350 snd_hda_detach_beep_device(codec);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005351 snd_hda_gen_spec_free(codec->spec);
5352 kfree(codec->spec);
5353 codec->spec = NULL;
5354}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005355EXPORT_SYMBOL_GPL(snd_hda_gen_free);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005356
5357#ifdef CONFIG_PM
Takashi Iwai5187ac12013-01-07 12:52:16 +01005358/*
5359 * check the loopback power save state;
5360 * this can be put as patch_ops.check_power_status function
5361 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005362int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
5363{
5364 struct hda_gen_spec *spec = codec->spec;
5365 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
5366}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005367EXPORT_SYMBOL_GPL(snd_hda_gen_check_power_status);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005368#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01005369
5370
5371/*
5372 * the generic codec support
5373 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374
Takashi Iwai352f7f92012-12-19 12:52:06 +01005375static const struct hda_codec_ops generic_patch_ops = {
5376 .build_controls = snd_hda_gen_build_controls,
5377 .build_pcms = snd_hda_gen_build_pcms,
5378 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01005379 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005380 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02005381#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01005382 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02005383#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384};
5385
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386int snd_hda_parse_generic_codec(struct hda_codec *codec)
5387{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005388 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389 int err;
5390
Takashi Iwaie560d8d2005-09-09 14:21:46 +02005391 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005392 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005394 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396
Takashi Iwai9eb413e2012-12-19 14:41:21 +01005397 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
5398 if (err < 0)
5399 return err;
5400
5401 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005402 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403 goto error;
5404
5405 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406 return 0;
5407
Takashi Iwai352f7f92012-12-19 12:52:06 +01005408error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01005409 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410 return err;
5411}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005412EXPORT_SYMBOL_GPL(snd_hda_parse_generic_codec);
Takashi Iwaib21bdd02013-11-18 12:03:56 +01005413
5414MODULE_LICENSE("GPL");
5415MODULE_DESCRIPTION("Generic HD-audio codec parser");