blob: c7f6d1cab6063a6483802ee6c80391c18c2de884 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Universal Interface for Intel High Definition Audio Codec
3 *
4 * Generic widget tree parser
5 *
6 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
7 *
8 * This driver is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This driver is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/init.h>
24#include <linux/slab.h>
Paul Gortmakerd81a6d72011-09-22 09:34:58 -040025#include <linux/export.h>
Takashi Iwai352f7f92012-12-19 12:52:06 +010026#include <linux/sort.h>
Takashi Iwai55196ff2013-01-24 17:32:56 +010027#include <linux/delay.h>
Takashi Iwaif873e532012-12-20 16:58:39 +010028#include <linux/ctype.h>
29#include <linux/string.h>
Takashi Iwai294765582013-01-17 09:52:11 +010030#include <linux/bitops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <sound/core.h>
Takashi Iwai352f7f92012-12-19 12:52:06 +010032#include <sound/jack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "hda_codec.h"
34#include "hda_local.h"
Takashi Iwai352f7f92012-12-19 12:52:06 +010035#include "hda_auto_parser.h"
36#include "hda_jack.h"
Takashi Iwai7504b6c2013-03-18 11:25:51 +010037#include "hda_beep.h"
Takashi Iwai352f7f92012-12-19 12:52:06 +010038#include "hda_generic.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Takashi Iwai352f7f92012-12-19 12:52:06 +010041/* initialize hda_gen_spec struct */
42int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
Takashi Iwai352f7f92012-12-19 12:52:06 +010044 snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
Takashi Iwai352f7f92012-12-19 12:52:06 +010045 snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
Takashi Iwai0186f4f2013-02-07 10:45:11 +010046 snd_array_init(&spec->loopback_list, sizeof(struct hda_amp_list), 8);
Takashi Iwai38cf6f12012-12-21 14:09:42 +010047 mutex_init(&spec->pcm_mutex);
Takashi Iwai352f7f92012-12-19 12:52:06 +010048 return 0;
49}
50EXPORT_SYMBOL_HDA(snd_hda_gen_spec_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Takashi Iwai12c93df2012-12-19 14:38:33 +010052struct snd_kcontrol_new *
53snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
54 const struct snd_kcontrol_new *temp)
Takashi Iwai352f7f92012-12-19 12:52:06 +010055{
56 struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
57 if (!knew)
58 return NULL;
59 *knew = *temp;
60 if (name)
61 knew->name = kstrdup(name, GFP_KERNEL);
62 else if (knew->name)
63 knew->name = kstrdup(knew->name, GFP_KERNEL);
64 if (!knew->name)
65 return NULL;
66 return knew;
67}
Takashi Iwai12c93df2012-12-19 14:38:33 +010068EXPORT_SYMBOL_HDA(snd_hda_gen_add_kctl);
Takashi Iwai352f7f92012-12-19 12:52:06 +010069
70static void free_kctls(struct hda_gen_spec *spec)
71{
72 if (spec->kctls.list) {
73 struct snd_kcontrol_new *kctl = spec->kctls.list;
74 int i;
75 for (i = 0; i < spec->kctls.used; i++)
76 kfree(kctl[i].name);
77 }
78 snd_array_free(&spec->kctls);
79}
80
Takashi Iwai352f7f92012-12-19 12:52:06 +010081void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
82{
83 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 return;
Takashi Iwai352f7f92012-12-19 12:52:06 +010085 free_kctls(spec);
Takashi Iwai352f7f92012-12-19 12:52:06 +010086 snd_array_free(&spec->paths);
Takashi Iwai0186f4f2013-02-07 10:45:11 +010087 snd_array_free(&spec->loopback_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
Takashi Iwai352f7f92012-12-19 12:52:06 +010089EXPORT_SYMBOL_HDA(snd_hda_gen_spec_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91/*
Takashi Iwai1c70a582013-01-11 17:48:22 +010092 * store user hints
93 */
94static void parse_user_hints(struct hda_codec *codec)
95{
96 struct hda_gen_spec *spec = codec->spec;
97 int val;
98
99 val = snd_hda_get_bool_hint(codec, "jack_detect");
100 if (val >= 0)
101 codec->no_jack_detect = !val;
102 val = snd_hda_get_bool_hint(codec, "inv_jack_detect");
103 if (val >= 0)
104 codec->inv_jack_detect = !!val;
105 val = snd_hda_get_bool_hint(codec, "trigger_sense");
106 if (val >= 0)
107 codec->no_trigger_sense = !val;
108 val = snd_hda_get_bool_hint(codec, "inv_eapd");
109 if (val >= 0)
110 codec->inv_eapd = !!val;
111 val = snd_hda_get_bool_hint(codec, "pcm_format_first");
112 if (val >= 0)
113 codec->pcm_format_first = !!val;
114 val = snd_hda_get_bool_hint(codec, "sticky_stream");
115 if (val >= 0)
116 codec->no_sticky_stream = !val;
117 val = snd_hda_get_bool_hint(codec, "spdif_status_reset");
118 if (val >= 0)
119 codec->spdif_status_reset = !!val;
120 val = snd_hda_get_bool_hint(codec, "pin_amp_workaround");
121 if (val >= 0)
122 codec->pin_amp_workaround = !!val;
123 val = snd_hda_get_bool_hint(codec, "single_adc_amp");
124 if (val >= 0)
125 codec->single_adc_amp = !!val;
126
Takashi Iwaif72706b2013-01-16 18:20:07 +0100127 val = snd_hda_get_bool_hint(codec, "auto_mute");
128 if (val >= 0)
129 spec->suppress_auto_mute = !val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100130 val = snd_hda_get_bool_hint(codec, "auto_mic");
131 if (val >= 0)
132 spec->suppress_auto_mic = !val;
133 val = snd_hda_get_bool_hint(codec, "line_in_auto_switch");
134 if (val >= 0)
135 spec->line_in_auto_switch = !!val;
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200136 val = snd_hda_get_bool_hint(codec, "auto_mute_via_amp");
137 if (val >= 0)
138 spec->auto_mute_via_amp = !!val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100139 val = snd_hda_get_bool_hint(codec, "need_dac_fix");
140 if (val >= 0)
141 spec->need_dac_fix = !!val;
142 val = snd_hda_get_bool_hint(codec, "primary_hp");
143 if (val >= 0)
144 spec->no_primary_hp = !val;
Takashi Iwaida96fb52013-07-29 16:54:36 +0200145 val = snd_hda_get_bool_hint(codec, "multi_io");
146 if (val >= 0)
147 spec->no_multi_io = !val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100148 val = snd_hda_get_bool_hint(codec, "multi_cap_vol");
149 if (val >= 0)
150 spec->multi_cap_vol = !!val;
151 val = snd_hda_get_bool_hint(codec, "inv_dmic_split");
152 if (val >= 0)
153 spec->inv_dmic_split = !!val;
154 val = snd_hda_get_bool_hint(codec, "indep_hp");
155 if (val >= 0)
156 spec->indep_hp = !!val;
157 val = snd_hda_get_bool_hint(codec, "add_stereo_mix_input");
158 if (val >= 0)
159 spec->add_stereo_mix_input = !!val;
Takashi Iwaif811c3c2013-03-07 18:32:59 +0100160 /* the following two are just for compatibility */
Takashi Iwai1c70a582013-01-11 17:48:22 +0100161 val = snd_hda_get_bool_hint(codec, "add_out_jack_modes");
162 if (val >= 0)
Takashi Iwaif811c3c2013-03-07 18:32:59 +0100163 spec->add_jack_modes = !!val;
Takashi Iwai294765582013-01-17 09:52:11 +0100164 val = snd_hda_get_bool_hint(codec, "add_in_jack_modes");
165 if (val >= 0)
Takashi Iwaif811c3c2013-03-07 18:32:59 +0100166 spec->add_jack_modes = !!val;
167 val = snd_hda_get_bool_hint(codec, "add_jack_modes");
168 if (val >= 0)
169 spec->add_jack_modes = !!val;
Takashi Iwai55196ff2013-01-24 17:32:56 +0100170 val = snd_hda_get_bool_hint(codec, "power_down_unused");
171 if (val >= 0)
172 spec->power_down_unused = !!val;
Takashi Iwai967303d2013-02-19 17:12:42 +0100173 val = snd_hda_get_bool_hint(codec, "add_hp_mic");
174 if (val >= 0)
175 spec->hp_mic = !!val;
176 val = snd_hda_get_bool_hint(codec, "hp_mic_detect");
177 if (val >= 0)
178 spec->suppress_hp_mic_detect = !val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100179
180 if (!snd_hda_get_int_hint(codec, "mixer_nid", &val))
181 spec->mixer_nid = val;
182}
183
184/*
Takashi Iwai2c12c302013-01-10 09:33:29 +0100185 * pin control value accesses
186 */
187
188#define update_pin_ctl(codec, pin, val) \
189 snd_hda_codec_update_cache(codec, pin, 0, \
190 AC_VERB_SET_PIN_WIDGET_CONTROL, val)
191
192/* restore the pinctl based on the cached value */
193static inline void restore_pin_ctl(struct hda_codec *codec, hda_nid_t pin)
194{
195 update_pin_ctl(codec, pin, snd_hda_codec_get_pin_target(codec, pin));
196}
197
198/* set the pinctl target value and write it if requested */
199static void set_pin_target(struct hda_codec *codec, hda_nid_t pin,
200 unsigned int val, bool do_write)
201{
202 if (!pin)
203 return;
204 val = snd_hda_correct_pin_ctl(codec, pin, val);
205 snd_hda_codec_set_pin_target(codec, pin, val);
206 if (do_write)
207 update_pin_ctl(codec, pin, val);
208}
209
210/* set pinctl target values for all given pins */
211static void set_pin_targets(struct hda_codec *codec, int num_pins,
212 hda_nid_t *pins, unsigned int val)
213{
214 int i;
215 for (i = 0; i < num_pins; i++)
216 set_pin_target(codec, pins[i], val, false);
217}
218
219/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100220 * parsing paths
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100223/* return the position of NID in the list, or -1 if not found */
224static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
225{
226 int i;
227 for (i = 0; i < nums; i++)
228 if (list[i] == nid)
229 return i;
230 return -1;
231}
232
233/* return true if the given NID is contained in the path */
234static bool is_nid_contained(struct nid_path *path, hda_nid_t nid)
235{
236 return find_idx_in_nid_list(nid, path->path, path->depth) >= 0;
237}
238
Takashi Iwaif5172a72013-01-04 13:19:55 +0100239static struct nid_path *get_nid_path(struct hda_codec *codec,
240 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100241 int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100243 struct hda_gen_spec *spec = codec->spec;
244 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Takashi Iwai352f7f92012-12-19 12:52:06 +0100246 for (i = 0; i < spec->paths.used; i++) {
247 struct nid_path *path = snd_array_elem(&spec->paths, i);
248 if (path->depth <= 0)
249 continue;
250 if ((!from_nid || path->path[0] == from_nid) &&
Takashi Iwaif5172a72013-01-04 13:19:55 +0100251 (!to_nid || path->path[path->depth - 1] == to_nid)) {
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100252 if (!anchor_nid ||
253 (anchor_nid > 0 && is_nid_contained(path, anchor_nid)) ||
254 (anchor_nid < 0 && !is_nid_contained(path, anchor_nid)))
Takashi Iwaif5172a72013-01-04 13:19:55 +0100255 return path;
256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
258 return NULL;
259}
Takashi Iwaif5172a72013-01-04 13:19:55 +0100260
261/* get the path between the given NIDs;
262 * passing 0 to either @pin or @dac behaves as a wildcard
263 */
264struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
265 hda_nid_t from_nid, hda_nid_t to_nid)
266{
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100267 return get_nid_path(codec, from_nid, to_nid, 0);
Takashi Iwaif5172a72013-01-04 13:19:55 +0100268}
Takashi Iwai352f7f92012-12-19 12:52:06 +0100269EXPORT_SYMBOL_HDA(snd_hda_get_nid_path);
270
Takashi Iwai196c17662013-01-04 15:01:40 +0100271/* get the index number corresponding to the path instance;
272 * the index starts from 1, for easier checking the invalid value
273 */
274int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
275{
276 struct hda_gen_spec *spec = codec->spec;
277 struct nid_path *array = spec->paths.list;
278 ssize_t idx;
279
280 if (!spec->paths.used)
281 return 0;
282 idx = path - array;
283 if (idx < 0 || idx >= spec->paths.used)
284 return 0;
285 return idx + 1;
286}
Takashi Iwai4bd01e92013-01-22 15:17:20 +0100287EXPORT_SYMBOL_HDA(snd_hda_get_path_idx);
Takashi Iwai196c17662013-01-04 15:01:40 +0100288
289/* get the path instance corresponding to the given index number */
290struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
291{
292 struct hda_gen_spec *spec = codec->spec;
293
294 if (idx <= 0 || idx > spec->paths.used)
295 return NULL;
296 return snd_array_elem(&spec->paths, idx - 1);
297}
Takashi Iwai4bd01e92013-01-22 15:17:20 +0100298EXPORT_SYMBOL_HDA(snd_hda_get_path_from_idx);
Takashi Iwai196c17662013-01-04 15:01:40 +0100299
Takashi Iwai352f7f92012-12-19 12:52:06 +0100300/* check whether the given DAC is already found in any existing paths */
301static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
302{
303 struct hda_gen_spec *spec = codec->spec;
304 int i;
305
306 for (i = 0; i < spec->paths.used; i++) {
307 struct nid_path *path = snd_array_elem(&spec->paths, i);
308 if (path->path[0] == nid)
309 return true;
310 }
311 return false;
312}
313
314/* check whether the given two widgets can be connected */
315static bool is_reachable_path(struct hda_codec *codec,
316 hda_nid_t from_nid, hda_nid_t to_nid)
317{
318 if (!from_nid || !to_nid)
319 return false;
320 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
321}
322
323/* nid, dir and idx */
324#define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
325
326/* check whether the given ctl is already assigned in any path elements */
327static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
328{
329 struct hda_gen_spec *spec = codec->spec;
330 int i;
331
332 val &= AMP_VAL_COMPARE_MASK;
333 for (i = 0; i < spec->paths.used; i++) {
334 struct nid_path *path = snd_array_elem(&spec->paths, i);
335 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
336 return true;
337 }
338 return false;
339}
340
341/* check whether a control with the given (nid, dir, idx) was assigned */
342static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai8999bf02013-01-18 11:01:33 +0100343 int dir, int idx, int type)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100344{
345 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
Takashi Iwai8999bf02013-01-18 11:01:33 +0100346 return is_ctl_used(codec, val, type);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100347}
348
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100349static void print_nid_path(const char *pfx, struct nid_path *path)
350{
351 char buf[40];
352 int i;
353
354
355 buf[0] = 0;
356 for (i = 0; i < path->depth; i++) {
357 char tmp[4];
358 sprintf(tmp, ":%02x", path->path[i]);
359 strlcat(buf, tmp, sizeof(buf));
360 }
361 snd_printdd("%s path: depth=%d %s\n", pfx, path->depth, buf);
362}
363
Takashi Iwai352f7f92012-12-19 12:52:06 +0100364/* called recursively */
365static bool __parse_nid_path(struct hda_codec *codec,
366 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100367 int anchor_nid, struct nid_path *path,
368 int depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100369{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100370 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100371 int i, nums;
372
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100373 if (to_nid == anchor_nid)
374 anchor_nid = 0; /* anchor passed */
375 else if (to_nid == (hda_nid_t)(-anchor_nid))
376 return false; /* hit the exclusive nid */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100377
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100378 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100379 for (i = 0; i < nums; i++) {
380 if (conn[i] != from_nid) {
381 /* special case: when from_nid is 0,
382 * try to find an empty DAC
383 */
384 if (from_nid ||
385 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
386 is_dac_already_used(codec, conn[i]))
387 continue;
388 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100389 /* anchor is not requested or already passed? */
390 if (anchor_nid <= 0)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100391 goto found;
392 }
393 if (depth >= MAX_NID_PATH_DEPTH)
394 return false;
395 for (i = 0; i < nums; i++) {
396 unsigned int type;
397 type = get_wcaps_type(get_wcaps(codec, conn[i]));
398 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
399 type == AC_WID_PIN)
400 continue;
401 if (__parse_nid_path(codec, from_nid, conn[i],
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100402 anchor_nid, path, depth + 1))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100403 goto found;
404 }
405 return false;
406
407 found:
408 path->path[path->depth] = conn[i];
409 path->idx[path->depth + 1] = i;
410 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
411 path->multi[path->depth + 1] = 1;
412 path->depth++;
413 return true;
414}
415
416/* parse the widget path from the given nid to the target nid;
417 * when @from_nid is 0, try to find an empty DAC;
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100418 * when @anchor_nid is set to a positive value, only paths through the widget
419 * with the given value are evaluated.
420 * when @anchor_nid is set to a negative value, paths through the widget
421 * with the negative of given value are excluded, only other paths are chosen.
422 * when @anchor_nid is zero, no special handling about path selection.
Takashi Iwai352f7f92012-12-19 12:52:06 +0100423 */
424bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100425 hda_nid_t to_nid, int anchor_nid,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100426 struct nid_path *path)
427{
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100428 if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100429 path->path[path->depth] = to_nid;
430 path->depth++;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100431 return true;
432 }
433 return false;
434}
435EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100438 * parse the path between the given NIDs and add to the path list.
439 * if no valid path is found, return NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100441struct nid_path *
442snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100443 hda_nid_t to_nid, int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100445 struct hda_gen_spec *spec = codec->spec;
446 struct nid_path *path;
447
448 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
449 return NULL;
450
Takashi Iwaif5172a72013-01-04 13:19:55 +0100451 /* check whether the path has been already added */
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100452 path = get_nid_path(codec, from_nid, to_nid, anchor_nid);
Takashi Iwaif5172a72013-01-04 13:19:55 +0100453 if (path)
454 return path;
455
Takashi Iwai352f7f92012-12-19 12:52:06 +0100456 path = snd_array_new(&spec->paths);
457 if (!path)
458 return NULL;
459 memset(path, 0, sizeof(*path));
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100460 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100461 return path;
462 /* push back */
463 spec->paths.used--;
464 return NULL;
465}
466EXPORT_SYMBOL_HDA(snd_hda_add_new_path);
467
Takashi Iwai980428c2013-01-09 09:28:20 +0100468/* clear the given path as invalid so that it won't be picked up later */
469static void invalidate_nid_path(struct hda_codec *codec, int idx)
470{
471 struct nid_path *path = snd_hda_get_path_from_idx(codec, idx);
472 if (!path)
473 return;
474 memset(path, 0, sizeof(*path));
475}
476
Takashi Iwai36907392013-12-10 17:29:26 +0100477/* return a DAC if paired to the given pin by codec driver */
478static hda_nid_t get_preferred_dac(struct hda_codec *codec, hda_nid_t pin)
479{
480 struct hda_gen_spec *spec = codec->spec;
481 const hda_nid_t *list = spec->preferred_dacs;
482
483 if (!list)
484 return 0;
485 for (; *list; list += 2)
486 if (*list == pin)
487 return list[1];
488 return 0;
489}
490
Takashi Iwai352f7f92012-12-19 12:52:06 +0100491/* look for an empty DAC slot */
492static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
493 bool is_digital)
494{
495 struct hda_gen_spec *spec = codec->spec;
496 bool cap_digital;
497 int i;
498
499 for (i = 0; i < spec->num_all_dacs; i++) {
500 hda_nid_t nid = spec->all_dacs[i];
501 if (!nid || is_dac_already_used(codec, nid))
502 continue;
503 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
504 if (is_digital != cap_digital)
505 continue;
506 if (is_reachable_path(codec, nid, pin))
507 return nid;
508 }
509 return 0;
510}
511
512/* replace the channels in the composed amp value with the given number */
513static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
514{
515 val &= ~(0x3U << 16);
516 val |= chs << 16;
517 return val;
518}
519
520/* check whether the widget has the given amp capability for the direction */
521static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
522 int dir, unsigned int bits)
523{
524 if (!nid)
525 return false;
526 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
527 if (query_amp_caps(codec, nid, dir) & bits)
528 return true;
529 return false;
530}
531
David Henningsson99a55922013-01-16 15:58:44 +0100532static bool same_amp_caps(struct hda_codec *codec, hda_nid_t nid1,
533 hda_nid_t nid2, int dir)
534{
535 if (!(get_wcaps(codec, nid1) & (1 << (dir + 1))))
536 return !(get_wcaps(codec, nid2) & (1 << (dir + 1)));
537 return (query_amp_caps(codec, nid1, dir) ==
538 query_amp_caps(codec, nid2, dir));
539}
540
Takashi Iwai352f7f92012-12-19 12:52:06 +0100541#define nid_has_mute(codec, nid, dir) \
Takashi Iwaif69910d2013-08-08 09:32:37 +0200542 check_amp_caps(codec, nid, dir, (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100543#define nid_has_volume(codec, nid, dir) \
544 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
545
546/* look for a widget suitable for assigning a mute switch in the path */
547static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
548 struct nid_path *path)
549{
550 int i;
551
552 for (i = path->depth - 1; i >= 0; i--) {
553 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
554 return path->path[i];
555 if (i != path->depth - 1 && i != 0 &&
556 nid_has_mute(codec, path->path[i], HDA_INPUT))
557 return path->path[i];
558 }
559 return 0;
560}
561
562/* look for a widget suitable for assigning a volume ctl in the path */
563static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
564 struct nid_path *path)
565{
Takashi Iwaia1114a82013-11-04 16:32:01 +0100566 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100567 int i;
568
569 for (i = path->depth - 1; i >= 0; i--) {
Takashi Iwaia1114a82013-11-04 16:32:01 +0100570 hda_nid_t nid = path->path[i];
571 if ((spec->out_vol_mask >> nid) & 1)
572 continue;
573 if (nid_has_volume(codec, nid, HDA_OUTPUT))
574 return nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100575 }
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200576 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577}
578
579/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100580 * path activation / deactivation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100582
583/* can have the amp-in capability? */
584static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100586 hda_nid_t nid = path->path[idx];
587 unsigned int caps = get_wcaps(codec, nid);
588 unsigned int type = get_wcaps_type(caps);
589
590 if (!(caps & AC_WCAP_IN_AMP))
591 return false;
592 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
593 return false;
594 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595}
596
Takashi Iwai352f7f92012-12-19 12:52:06 +0100597/* can have the amp-out capability? */
598static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100600 hda_nid_t nid = path->path[idx];
601 unsigned int caps = get_wcaps(codec, nid);
602 unsigned int type = get_wcaps_type(caps);
603
604 if (!(caps & AC_WCAP_OUT_AMP))
605 return false;
606 if (type == AC_WID_PIN && !idx) /* only for output pins */
607 return false;
608 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609}
610
Takashi Iwai352f7f92012-12-19 12:52:06 +0100611/* check whether the given (nid,dir,idx) is active */
612static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai7dddf2ae2013-01-24 16:31:35 +0100613 unsigned int dir, unsigned int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100615 struct hda_gen_spec *spec = codec->spec;
616 int i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Takashi Iwai352f7f92012-12-19 12:52:06 +0100618 for (n = 0; n < spec->paths.used; n++) {
619 struct nid_path *path = snd_array_elem(&spec->paths, n);
620 if (!path->active)
621 continue;
622 for (i = 0; i < path->depth; i++) {
623 if (path->path[i] == nid) {
624 if (dir == HDA_OUTPUT || path->idx[i] == idx)
625 return true;
626 break;
627 }
628 }
629 }
630 return false;
631}
632
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +0200633/* check whether the NID is referred by any active paths */
634#define is_active_nid_for_any(codec, nid) \
635 is_active_nid(codec, nid, HDA_OUTPUT, 0)
636
Takashi Iwai352f7f92012-12-19 12:52:06 +0100637/* get the default amp value for the target state */
638static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai8999bf02013-01-18 11:01:33 +0100639 int dir, unsigned int caps, bool enable)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100640{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100641 unsigned int val = 0;
642
Takashi Iwai352f7f92012-12-19 12:52:06 +0100643 if (caps & AC_AMPCAP_NUM_STEPS) {
644 /* set to 0dB */
645 if (enable)
646 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
647 }
Takashi Iwaif69910d2013-08-08 09:32:37 +0200648 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100649 if (!enable)
650 val |= HDA_AMP_MUTE;
651 }
652 return val;
653}
654
655/* initialize the amp value (only at the first time) */
656static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
657{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100658 unsigned int caps = query_amp_caps(codec, nid, dir);
659 int val = get_amp_val_to_activate(codec, nid, dir, caps, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100660 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
661}
662
Takashi Iwai8999bf02013-01-18 11:01:33 +0100663/* calculate amp value mask we can modify;
664 * if the given amp is controlled by mixers, don't touch it
665 */
666static unsigned int get_amp_mask_to_modify(struct hda_codec *codec,
667 hda_nid_t nid, int dir, int idx,
668 unsigned int caps)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100669{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100670 unsigned int mask = 0xff;
671
Takashi Iwaif69910d2013-08-08 09:32:37 +0200672 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
Takashi Iwai8999bf02013-01-18 11:01:33 +0100673 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_MUTE_CTL))
674 mask &= ~0x80;
675 }
676 if (caps & AC_AMPCAP_NUM_STEPS) {
677 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
678 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
679 mask &= ~0x7f;
680 }
681 return mask;
682}
683
684static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
685 int idx, int idx_to_check, bool enable)
686{
687 unsigned int caps;
688 unsigned int mask, val;
689
Takashi Iwai7dddf2ae2013-01-24 16:31:35 +0100690 if (!enable && is_active_nid(codec, nid, dir, idx_to_check))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100691 return;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100692
693 caps = query_amp_caps(codec, nid, dir);
694 val = get_amp_val_to_activate(codec, nid, dir, caps, enable);
695 mask = get_amp_mask_to_modify(codec, nid, dir, idx_to_check, caps);
696 if (!mask)
697 return;
698
699 val &= mask;
700 snd_hda_codec_amp_stereo(codec, nid, dir, idx, mask, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100701}
702
703static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
704 int i, bool enable)
705{
706 hda_nid_t nid = path->path[i];
707 init_amp(codec, nid, HDA_OUTPUT, 0);
Takashi Iwai8999bf02013-01-18 11:01:33 +0100708 activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100709}
710
711static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
712 int i, bool enable, bool add_aamix)
713{
714 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100715 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100716 int n, nums, idx;
717 int type;
718 hda_nid_t nid = path->path[i];
719
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100720 nums = snd_hda_get_conn_list(codec, nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100721 type = get_wcaps_type(get_wcaps(codec, nid));
722 if (type == AC_WID_PIN ||
723 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
724 nums = 1;
725 idx = 0;
726 } else
727 idx = path->idx[i];
728
729 for (n = 0; n < nums; n++)
730 init_amp(codec, nid, HDA_INPUT, n);
731
Takashi Iwai352f7f92012-12-19 12:52:06 +0100732 /* here is a little bit tricky in comparison with activate_amp_out();
733 * when aa-mixer is available, we need to enable the path as well
734 */
735 for (n = 0; n < nums; n++) {
Takashi Iwaie4a395e2013-01-23 17:00:31 +0100736 if (n != idx && (!add_aamix || conn[n] != spec->mixer_merge_nid))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100737 continue;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100738 activate_amp(codec, nid, HDA_INPUT, n, idx, enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 }
740}
741
Takashi Iwai352f7f92012-12-19 12:52:06 +0100742/* activate or deactivate the given path
743 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100745void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
746 bool enable, bool add_aamix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
Takashi Iwai55196ff2013-01-24 17:32:56 +0100748 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100749 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Takashi Iwai352f7f92012-12-19 12:52:06 +0100751 if (!enable)
752 path->active = false;
753
754 for (i = path->depth - 1; i >= 0; i--) {
Takashi Iwai55196ff2013-01-24 17:32:56 +0100755 hda_nid_t nid = path->path[i];
756 if (enable && spec->power_down_unused) {
757 /* make sure the widget is powered up */
758 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0))
759 snd_hda_codec_write(codec, nid, 0,
760 AC_VERB_SET_POWER_STATE,
761 AC_PWRST_D0);
762 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100763 if (enable && path->multi[i])
Takashi Iwai55196ff2013-01-24 17:32:56 +0100764 snd_hda_codec_write_cache(codec, nid, 0,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100765 AC_VERB_SET_CONNECT_SEL,
766 path->idx[i]);
767 if (has_amp_in(codec, path, i))
768 activate_amp_in(codec, path, i, enable, add_aamix);
769 if (has_amp_out(codec, path, i))
770 activate_amp_out(codec, path, i, enable);
771 }
772
773 if (enable)
774 path->active = true;
775}
776EXPORT_SYMBOL_HDA(snd_hda_activate_path);
777
Takashi Iwai55196ff2013-01-24 17:32:56 +0100778/* if the given path is inactive, put widgets into D3 (only if suitable) */
779static void path_power_down_sync(struct hda_codec *codec, struct nid_path *path)
780{
781 struct hda_gen_spec *spec = codec->spec;
Jiri Slaby868211d2013-04-04 22:32:10 +0200782 bool changed = false;
Takashi Iwai55196ff2013-01-24 17:32:56 +0100783 int i;
784
785 if (!spec->power_down_unused || path->active)
786 return;
787
788 for (i = 0; i < path->depth; i++) {
789 hda_nid_t nid = path->path[i];
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +0200790 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D3) &&
791 !is_active_nid_for_any(codec, nid)) {
Takashi Iwai55196ff2013-01-24 17:32:56 +0100792 snd_hda_codec_write(codec, nid, 0,
793 AC_VERB_SET_POWER_STATE,
794 AC_PWRST_D3);
795 changed = true;
796 }
797 }
798
799 if (changed) {
800 msleep(10);
801 snd_hda_codec_read(codec, path->path[0], 0,
802 AC_VERB_GET_POWER_STATE, 0);
803 }
804}
805
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100806/* turn on/off EAPD on the given pin */
807static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
808{
809 struct hda_gen_spec *spec = codec->spec;
810 if (spec->own_eapd_ctl ||
811 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
812 return;
Takashi Iwai05909d5c2013-05-31 19:55:54 +0200813 if (spec->keep_eapd_on && !enable)
814 return;
Takashi Iwai468ac412013-11-12 11:36:00 +0100815 if (codec->inv_eapd)
816 enable = !enable;
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100817 snd_hda_codec_update_cache(codec, pin, 0,
818 AC_VERB_SET_EAPD_BTLENABLE,
819 enable ? 0x02 : 0x00);
820}
821
Takashi Iwai3e367f12013-01-23 17:07:23 +0100822/* re-initialize the path specified by the given path index */
823static void resume_path_from_idx(struct hda_codec *codec, int path_idx)
824{
825 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
826 if (path)
827 snd_hda_activate_path(codec, path, path->active, false);
828}
829
Takashi Iwai352f7f92012-12-19 12:52:06 +0100830
831/*
832 * Helper functions for creating mixer ctl elements
833 */
834
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200835static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
836 struct snd_ctl_elem_value *ucontrol);
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200837static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
838 struct snd_ctl_elem_value *ucontrol);
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200839
Takashi Iwai352f7f92012-12-19 12:52:06 +0100840enum {
841 HDA_CTL_WIDGET_VOL,
842 HDA_CTL_WIDGET_MUTE,
843 HDA_CTL_BIND_MUTE,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100844};
845static const struct snd_kcontrol_new control_templates[] = {
846 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200847 /* only the put callback is replaced for handling the special mute */
848 {
849 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
850 .subdevice = HDA_SUBDEV_AMP_FLAG,
851 .info = snd_hda_mixer_amp_switch_info,
852 .get = snd_hda_mixer_amp_switch_get,
853 .put = hda_gen_mixer_mute_put, /* replaced */
854 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
855 },
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200856 {
857 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
858 .info = snd_hda_mixer_amp_switch_info,
859 .get = snd_hda_mixer_bind_switch_get,
860 .put = hda_gen_bind_mute_put, /* replaced */
861 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
862 },
Takashi Iwai352f7f92012-12-19 12:52:06 +0100863};
864
865/* add dynamic controls from template */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100866static struct snd_kcontrol_new *
867add_control(struct hda_gen_spec *spec, int type, const char *name,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100868 int cidx, unsigned long val)
869{
870 struct snd_kcontrol_new *knew;
871
Takashi Iwai12c93df2012-12-19 14:38:33 +0100872 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100873 if (!knew)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100874 return NULL;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100875 knew->index = cidx;
876 if (get_amp_nid_(val))
877 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
878 knew->private_value = val;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100879 return knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100880}
881
882static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
883 const char *pfx, const char *dir,
884 const char *sfx, int cidx, unsigned long val)
885{
Takashi Iwai975cc022013-06-28 11:56:49 +0200886 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai352f7f92012-12-19 12:52:06 +0100887 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100888 if (!add_control(spec, type, name, cidx, val))
889 return -ENOMEM;
890 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100891}
892
893#define add_pb_vol_ctrl(spec, type, pfx, val) \
894 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
895#define add_pb_sw_ctrl(spec, type, pfx, val) \
896 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
897#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
898 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
899#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
900 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
901
902static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
903 unsigned int chs, struct nid_path *path)
904{
905 unsigned int val;
906 if (!path)
907 return 0;
908 val = path->ctls[NID_PATH_VOL_CTL];
909 if (!val)
910 return 0;
911 val = amp_val_replace_channels(val, chs);
912 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
913}
914
915/* return the channel bits suitable for the given path->ctls[] */
916static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
917 int type)
918{
919 int chs = 1; /* mono (left only) */
920 if (path) {
921 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
922 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
923 chs = 3; /* stereo */
924 }
925 return chs;
926}
927
928static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
929 struct nid_path *path)
930{
931 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
932 return add_vol_ctl(codec, pfx, cidx, chs, path);
933}
934
935/* create a mute-switch for the given mixer widget;
936 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
937 */
938static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
939 unsigned int chs, struct nid_path *path)
940{
941 unsigned int val;
942 int type = HDA_CTL_WIDGET_MUTE;
943
944 if (!path)
945 return 0;
946 val = path->ctls[NID_PATH_MUTE_CTL];
947 if (!val)
948 return 0;
949 val = amp_val_replace_channels(val, chs);
950 if (get_amp_direction_(val) == HDA_INPUT) {
951 hda_nid_t nid = get_amp_nid_(val);
952 int nums = snd_hda_get_num_conns(codec, nid);
953 if (nums > 1) {
954 type = HDA_CTL_BIND_MUTE;
955 val |= nums << 19;
956 }
957 }
958 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
959}
960
961static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
962 int cidx, struct nid_path *path)
963{
964 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
965 return add_sw_ctl(codec, pfx, cidx, chs, path);
966}
967
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200968/* playback mute control with the software mute bit check */
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200969static void sync_auto_mute_bits(struct snd_kcontrol *kcontrol,
970 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200971{
972 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
973 struct hda_gen_spec *spec = codec->spec;
974
975 if (spec->auto_mute_via_amp) {
976 hda_nid_t nid = get_amp_nid(kcontrol);
977 bool enabled = !((spec->mute_bits >> nid) & 1);
978 ucontrol->value.integer.value[0] &= enabled;
979 ucontrol->value.integer.value[1] &= enabled;
980 }
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200981}
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200982
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200983static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
984 struct snd_ctl_elem_value *ucontrol)
985{
986 sync_auto_mute_bits(kcontrol, ucontrol);
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200987 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
988}
989
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200990static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
991 struct snd_ctl_elem_value *ucontrol)
992{
993 sync_auto_mute_bits(kcontrol, ucontrol);
994 return snd_hda_mixer_bind_switch_put(kcontrol, ucontrol);
995}
996
Takashi Iwai247d85e2013-01-17 16:18:11 +0100997/* any ctl assigned to the path with the given index? */
998static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
999{
1000 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
1001 return path && path->ctls[ctl_type];
1002}
1003
Takashi Iwai352f7f92012-12-19 12:52:06 +01001004static const char * const channel_name[4] = {
1005 "Front", "Surround", "CLFE", "Side"
1006};
1007
1008/* give some appropriate ctl name prefix for the given line out channel */
Takashi Iwai247d85e2013-01-17 16:18:11 +01001009static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
1010 int *index, int ctl_type)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001011{
Takashi Iwai247d85e2013-01-17 16:18:11 +01001012 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001013 struct auto_pin_cfg *cfg = &spec->autocfg;
1014
1015 *index = 0;
1016 if (cfg->line_outs == 1 && !spec->multi_ios &&
Takashi Iwai247d85e2013-01-17 16:18:11 +01001017 !cfg->hp_outs && !cfg->speaker_outs)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001018 return spec->vmaster_mute.hook ? "PCM" : "Master";
1019
1020 /* if there is really a single DAC used in the whole output paths,
1021 * use it master (or "PCM" if a vmaster hook is present)
1022 */
1023 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
1024 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
1025 return spec->vmaster_mute.hook ? "PCM" : "Master";
1026
Takashi Iwai247d85e2013-01-17 16:18:11 +01001027 /* multi-io channels */
1028 if (ch >= cfg->line_outs)
1029 return channel_name[ch];
1030
Takashi Iwai352f7f92012-12-19 12:52:06 +01001031 switch (cfg->line_out_type) {
1032 case AUTO_PIN_SPEAKER_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +01001033 /* if the primary channel vol/mute is shared with HP volume,
1034 * don't name it as Speaker
1035 */
1036 if (!ch && cfg->hp_outs &&
1037 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
1038 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001039 if (cfg->line_outs == 1)
1040 return "Speaker";
1041 if (cfg->line_outs == 2)
1042 return ch ? "Bass Speaker" : "Speaker";
1043 break;
1044 case AUTO_PIN_HP_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +01001045 /* if the primary channel vol/mute is shared with spk volume,
1046 * don't name it as Headphone
1047 */
1048 if (!ch && cfg->speaker_outs &&
1049 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
1050 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001051 /* for multi-io case, only the primary out */
1052 if (ch && spec->multi_ios)
1053 break;
1054 *index = ch;
1055 return "Headphone";
Takashi Iwai352f7f92012-12-19 12:52:06 +01001056 }
Takashi Iwai247d85e2013-01-17 16:18:11 +01001057
1058 /* for a single channel output, we don't have to name the channel */
1059 if (cfg->line_outs == 1 && !spec->multi_ios)
1060 return "PCM";
1061
Takashi Iwai352f7f92012-12-19 12:52:06 +01001062 if (ch >= ARRAY_SIZE(channel_name)) {
1063 snd_BUG();
1064 return "PCM";
1065 }
1066
1067 return channel_name[ch];
1068}
1069
1070/*
1071 * Parse output paths
1072 */
1073
1074/* badness definition */
1075enum {
1076 /* No primary DAC is found for the main output */
1077 BAD_NO_PRIMARY_DAC = 0x10000,
1078 /* No DAC is found for the extra output */
1079 BAD_NO_DAC = 0x4000,
1080 /* No possible multi-ios */
Takashi Iwai1d739062013-02-13 14:17:32 +01001081 BAD_MULTI_IO = 0x120,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001082 /* No individual DAC for extra output */
1083 BAD_NO_EXTRA_DAC = 0x102,
1084 /* No individual DAC for extra surrounds */
1085 BAD_NO_EXTRA_SURR_DAC = 0x101,
1086 /* Primary DAC shared with main surrounds */
1087 BAD_SHARED_SURROUND = 0x100,
Takashi Iwai55a63d42013-03-21 17:20:12 +01001088 /* No independent HP possible */
Takashi Iwaibec8e682013-03-22 15:10:08 +01001089 BAD_NO_INDEP_HP = 0x10,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001090 /* Primary DAC shared with main CLFE */
1091 BAD_SHARED_CLFE = 0x10,
1092 /* Primary DAC shared with extra surrounds */
1093 BAD_SHARED_EXTRA_SURROUND = 0x10,
1094 /* Volume widget is shared */
1095 BAD_SHARED_VOL = 0x10,
1096};
1097
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001098/* look for widgets in the given path which are appropriate for
Takashi Iwai352f7f92012-12-19 12:52:06 +01001099 * volume and mute controls, and assign the values to ctls[].
1100 *
1101 * When no appropriate widget is found in the path, the badness value
1102 * is incremented depending on the situation. The function returns the
1103 * total badness for both volume and mute controls.
1104 */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001105static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001106{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001107 hda_nid_t nid;
1108 unsigned int val;
1109 int badness = 0;
1110
1111 if (!path)
1112 return BAD_SHARED_VOL * 2;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001113
1114 if (path->ctls[NID_PATH_VOL_CTL] ||
1115 path->ctls[NID_PATH_MUTE_CTL])
1116 return 0; /* already evaluated */
1117
Takashi Iwai352f7f92012-12-19 12:52:06 +01001118 nid = look_for_out_vol_nid(codec, path);
1119 if (nid) {
1120 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1121 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1122 badness += BAD_SHARED_VOL;
1123 else
1124 path->ctls[NID_PATH_VOL_CTL] = val;
1125 } else
1126 badness += BAD_SHARED_VOL;
1127 nid = look_for_out_mute_nid(codec, path);
1128 if (nid) {
1129 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1130 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1131 nid_has_mute(codec, nid, HDA_OUTPUT))
1132 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1133 else
1134 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1135 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1136 badness += BAD_SHARED_VOL;
1137 else
1138 path->ctls[NID_PATH_MUTE_CTL] = val;
1139 } else
1140 badness += BAD_SHARED_VOL;
1141 return badness;
1142}
1143
Takashi Iwai98bd1112013-03-22 14:53:50 +01001144const struct badness_table hda_main_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001145 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1146 .no_dac = BAD_NO_DAC,
1147 .shared_primary = BAD_NO_PRIMARY_DAC,
1148 .shared_surr = BAD_SHARED_SURROUND,
1149 .shared_clfe = BAD_SHARED_CLFE,
1150 .shared_surr_main = BAD_SHARED_SURROUND,
1151};
Takashi Iwai98bd1112013-03-22 14:53:50 +01001152EXPORT_SYMBOL_HDA(hda_main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001153
Takashi Iwai98bd1112013-03-22 14:53:50 +01001154const struct badness_table hda_extra_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001155 .no_primary_dac = BAD_NO_DAC,
1156 .no_dac = BAD_NO_DAC,
1157 .shared_primary = BAD_NO_EXTRA_DAC,
1158 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1159 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1160 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1161};
Takashi Iwai98bd1112013-03-22 14:53:50 +01001162EXPORT_SYMBOL_HDA(hda_extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001163
Takashi Iwai7385df62013-01-07 09:50:52 +01001164/* get the DAC of the primary output corresponding to the given array index */
1165static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1166{
1167 struct hda_gen_spec *spec = codec->spec;
1168 struct auto_pin_cfg *cfg = &spec->autocfg;
1169
1170 if (cfg->line_outs > idx)
1171 return spec->private_dac_nids[idx];
1172 idx -= cfg->line_outs;
1173 if (spec->multi_ios > idx)
1174 return spec->multi_io[idx].dac;
1175 return 0;
1176}
1177
1178/* return the DAC if it's reachable, otherwise zero */
1179static inline hda_nid_t try_dac(struct hda_codec *codec,
1180 hda_nid_t dac, hda_nid_t pin)
1181{
1182 return is_reachable_path(codec, dac, pin) ? dac : 0;
1183}
1184
Takashi Iwai352f7f92012-12-19 12:52:06 +01001185/* try to assign DACs to pins and return the resultant badness */
1186static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1187 const hda_nid_t *pins, hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001188 int *path_idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001189 const struct badness_table *bad)
1190{
1191 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001192 int i, j;
1193 int badness = 0;
1194 hda_nid_t dac;
1195
1196 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 return 0;
1198
Takashi Iwai352f7f92012-12-19 12:52:06 +01001199 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001200 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001201 hda_nid_t pin = pins[i];
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001202
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001203 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1204 if (path) {
1205 badness += assign_out_path_ctls(codec, path);
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001206 continue;
1207 }
1208
Takashi Iwai36907392013-12-10 17:29:26 +01001209 dacs[i] = get_preferred_dac(codec, pin);
1210 if (dacs[i]) {
1211 if (is_dac_already_used(codec, dacs[i]))
1212 badness += bad->shared_primary;
1213 }
1214
1215 if (!dacs[i])
1216 dacs[i] = look_for_dac(codec, pin, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001217 if (!dacs[i] && !i) {
Takashi Iwai980428c2013-01-09 09:28:20 +01001218 /* try to steal the DAC of surrounds for the front */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001219 for (j = 1; j < num_outs; j++) {
1220 if (is_reachable_path(codec, dacs[j], pin)) {
1221 dacs[0] = dacs[j];
1222 dacs[j] = 0;
Takashi Iwai980428c2013-01-09 09:28:20 +01001223 invalidate_nid_path(codec, path_idx[j]);
Takashi Iwai196c17662013-01-04 15:01:40 +01001224 path_idx[j] = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001225 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 }
1227 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001228 }
1229 dac = dacs[i];
1230 if (!dac) {
Takashi Iwai7385df62013-01-07 09:50:52 +01001231 if (num_outs > 2)
1232 dac = try_dac(codec, get_primary_out(codec, i), pin);
1233 if (!dac)
1234 dac = try_dac(codec, dacs[0], pin);
1235 if (!dac)
1236 dac = try_dac(codec, get_primary_out(codec, i), pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001237 if (dac) {
1238 if (!i)
1239 badness += bad->shared_primary;
1240 else if (i == 1)
1241 badness += bad->shared_surr;
1242 else
1243 badness += bad->shared_clfe;
1244 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1245 dac = spec->private_dac_nids[0];
1246 badness += bad->shared_surr_main;
1247 } else if (!i)
1248 badness += bad->no_primary_dac;
1249 else
1250 badness += bad->no_dac;
1251 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001252 if (!dac)
1253 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001254 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001255 if (!path && !i && spec->mixer_nid) {
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001256 /* try with aamix */
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001257 path = snd_hda_add_new_path(codec, dac, pin, 0);
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001258 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001259 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001260 dac = dacs[i] = 0;
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001261 badness += bad->no_dac;
1262 } else {
Takashi Iwaia7694092013-01-21 10:43:18 +01001263 /* print_nid_path("output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001264 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001265 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001266 badness += assign_out_path_ctls(codec, path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001267 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001268 }
1269
1270 return badness;
1271}
1272
1273/* return NID if the given pin has only a single connection to a certain DAC */
1274static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1275{
1276 struct hda_gen_spec *spec = codec->spec;
1277 int i;
1278 hda_nid_t nid_found = 0;
1279
1280 for (i = 0; i < spec->num_all_dacs; i++) {
1281 hda_nid_t nid = spec->all_dacs[i];
1282 if (!nid || is_dac_already_used(codec, nid))
1283 continue;
1284 if (is_reachable_path(codec, nid, pin)) {
1285 if (nid_found)
1286 return 0;
1287 nid_found = nid;
1288 }
1289 }
1290 return nid_found;
1291}
1292
1293/* check whether the given pin can be a multi-io pin */
1294static bool can_be_multiio_pin(struct hda_codec *codec,
1295 unsigned int location, hda_nid_t nid)
1296{
1297 unsigned int defcfg, caps;
1298
1299 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1300 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1301 return false;
1302 if (location && get_defcfg_location(defcfg) != location)
1303 return false;
1304 caps = snd_hda_query_pin_caps(codec, nid);
1305 if (!(caps & AC_PINCAP_OUT))
1306 return false;
1307 return true;
1308}
1309
Takashi Iwaie22aab72013-01-04 14:50:04 +01001310/* count the number of input pins that are capable to be multi-io */
1311static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1312{
1313 struct hda_gen_spec *spec = codec->spec;
1314 struct auto_pin_cfg *cfg = &spec->autocfg;
1315 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1316 unsigned int location = get_defcfg_location(defcfg);
1317 int type, i;
1318 int num_pins = 0;
1319
1320 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1321 for (i = 0; i < cfg->num_inputs; i++) {
1322 if (cfg->inputs[i].type != type)
1323 continue;
1324 if (can_be_multiio_pin(codec, location,
1325 cfg->inputs[i].pin))
1326 num_pins++;
1327 }
1328 }
1329 return num_pins;
1330}
1331
Takashi Iwai352f7f92012-12-19 12:52:06 +01001332/*
1333 * multi-io helper
1334 *
1335 * When hardwired is set, try to fill ony hardwired pins, and returns
1336 * zero if any pins are filled, non-zero if nothing found.
1337 * When hardwired is off, try to fill possible input pins, and returns
1338 * the badness value.
1339 */
1340static int fill_multi_ios(struct hda_codec *codec,
1341 hda_nid_t reference_pin,
Takashi Iwaie22aab72013-01-04 14:50:04 +01001342 bool hardwired)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001343{
1344 struct hda_gen_spec *spec = codec->spec;
1345 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie22aab72013-01-04 14:50:04 +01001346 int type, i, j, num_pins, old_pins;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001347 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1348 unsigned int location = get_defcfg_location(defcfg);
1349 int badness = 0;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001350 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001351
1352 old_pins = spec->multi_ios;
1353 if (old_pins >= 2)
1354 goto end_fill;
1355
Takashi Iwaie22aab72013-01-04 14:50:04 +01001356 num_pins = count_multiio_pins(codec, reference_pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001357 if (num_pins < 2)
1358 goto end_fill;
1359
Takashi Iwai352f7f92012-12-19 12:52:06 +01001360 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1361 for (i = 0; i < cfg->num_inputs; i++) {
1362 hda_nid_t nid = cfg->inputs[i].pin;
1363 hda_nid_t dac = 0;
1364
1365 if (cfg->inputs[i].type != type)
1366 continue;
1367 if (!can_be_multiio_pin(codec, location, nid))
1368 continue;
1369 for (j = 0; j < spec->multi_ios; j++) {
1370 if (nid == spec->multi_io[j].pin)
1371 break;
1372 }
1373 if (j < spec->multi_ios)
1374 continue;
1375
Takashi Iwai352f7f92012-12-19 12:52:06 +01001376 if (hardwired)
1377 dac = get_dac_if_single(codec, nid);
1378 else if (!dac)
1379 dac = look_for_dac(codec, nid, false);
1380 if (!dac) {
1381 badness++;
1382 continue;
1383 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001384 path = snd_hda_add_new_path(codec, dac, nid,
1385 -spec->mixer_nid);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001386 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001387 badness++;
1388 continue;
1389 }
Takashi Iwaia7694092013-01-21 10:43:18 +01001390 /* print_nid_path("multiio", path); */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001391 spec->multi_io[spec->multi_ios].pin = nid;
1392 spec->multi_io[spec->multi_ios].dac = dac;
Takashi Iwai196c17662013-01-04 15:01:40 +01001393 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1394 snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001395 spec->multi_ios++;
1396 if (spec->multi_ios >= 2)
1397 break;
1398 }
1399 }
1400 end_fill:
1401 if (badness)
1402 badness = BAD_MULTI_IO;
1403 if (old_pins == spec->multi_ios) {
1404 if (hardwired)
1405 return 1; /* nothing found */
1406 else
1407 return badness; /* no badness if nothing found */
1408 }
1409 if (!hardwired && spec->multi_ios < 2) {
1410 /* cancel newly assigned paths */
1411 spec->paths.used -= spec->multi_ios - old_pins;
1412 spec->multi_ios = old_pins;
1413 return badness;
1414 }
1415
1416 /* assign volume and mute controls */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001417 for (i = old_pins; i < spec->multi_ios; i++) {
1418 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1419 badness += assign_out_path_ctls(codec, path);
1420 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001421
1422 return badness;
1423}
1424
1425/* map DACs for all pins in the list if they are single connections */
1426static bool map_singles(struct hda_codec *codec, int outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001427 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001428{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001429 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001430 int i;
1431 bool found = false;
1432 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001433 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001434 hda_nid_t dac;
1435 if (dacs[i])
1436 continue;
1437 dac = get_dac_if_single(codec, pins[i]);
1438 if (!dac)
1439 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001440 path = snd_hda_add_new_path(codec, dac, pins[i],
1441 -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001442 if (!path && !i && spec->mixer_nid)
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001443 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001444 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001445 dacs[i] = dac;
1446 found = true;
Takashi Iwaia7694092013-01-21 10:43:18 +01001447 /* print_nid_path("output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001448 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001449 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001450 }
1451 }
1452 return found;
1453}
1454
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001455/* create a new path including aamix if available, and return its index */
1456static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1457{
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001458 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001459 struct nid_path *path;
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001460 hda_nid_t path_dac, dac, pin;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001461
1462 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001463 if (!path || !path->depth ||
1464 is_nid_contained(path, spec->mixer_nid))
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001465 return 0;
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001466 path_dac = path->path[0];
1467 dac = spec->private_dac_nids[0];
Takashi Iwaif87498b2013-01-21 14:24:31 +01001468 pin = path->path[path->depth - 1];
1469 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1470 if (!path) {
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001471 if (dac != path_dac)
1472 dac = path_dac;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001473 else if (spec->multiout.hp_out_nid[0])
1474 dac = spec->multiout.hp_out_nid[0];
1475 else if (spec->multiout.extra_out_nid[0])
1476 dac = spec->multiout.extra_out_nid[0];
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001477 else
1478 dac = 0;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001479 if (dac)
1480 path = snd_hda_add_new_path(codec, dac, pin,
1481 spec->mixer_nid);
1482 }
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001483 if (!path)
1484 return 0;
Takashi Iwaia7694092013-01-21 10:43:18 +01001485 /* print_nid_path("output-aamix", path); */
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001486 path->active = false; /* unused as default */
1487 return snd_hda_get_path_idx(codec, path);
1488}
1489
Takashi Iwai55a63d42013-03-21 17:20:12 +01001490/* check whether the independent HP is available with the current config */
1491static bool indep_hp_possible(struct hda_codec *codec)
1492{
1493 struct hda_gen_spec *spec = codec->spec;
1494 struct auto_pin_cfg *cfg = &spec->autocfg;
1495 struct nid_path *path;
1496 int i, idx;
1497
1498 if (cfg->line_out_type == AUTO_PIN_HP_OUT)
1499 idx = spec->out_paths[0];
1500 else
1501 idx = spec->hp_paths[0];
1502 path = snd_hda_get_path_from_idx(codec, idx);
1503 if (!path)
1504 return false;
1505
1506 /* assume no path conflicts unless aamix is involved */
1507 if (!spec->mixer_nid || !is_nid_contained(path, spec->mixer_nid))
1508 return true;
1509
1510 /* check whether output paths contain aamix */
1511 for (i = 0; i < cfg->line_outs; i++) {
1512 if (spec->out_paths[i] == idx)
1513 break;
1514 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1515 if (path && is_nid_contained(path, spec->mixer_nid))
1516 return false;
1517 }
1518 for (i = 0; i < cfg->speaker_outs; i++) {
1519 path = snd_hda_get_path_from_idx(codec, spec->speaker_paths[i]);
1520 if (path && is_nid_contained(path, spec->mixer_nid))
1521 return false;
1522 }
1523
1524 return true;
1525}
1526
Takashi Iwaia07a9492013-01-07 16:44:06 +01001527/* fill the empty entries in the dac array for speaker/hp with the
1528 * shared dac pointed by the paths
1529 */
1530static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1531 hda_nid_t *dacs, int *path_idx)
1532{
1533 struct nid_path *path;
1534 int i;
1535
1536 for (i = 0; i < num_outs; i++) {
1537 if (dacs[i])
1538 continue;
1539 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1540 if (!path)
1541 continue;
1542 dacs[i] = path->path[0];
1543 }
1544}
1545
Takashi Iwai352f7f92012-12-19 12:52:06 +01001546/* fill in the dac_nids table from the parsed pin configuration */
1547static int fill_and_eval_dacs(struct hda_codec *codec,
1548 bool fill_hardwired,
1549 bool fill_mio_first)
1550{
1551 struct hda_gen_spec *spec = codec->spec;
1552 struct auto_pin_cfg *cfg = &spec->autocfg;
1553 int i, err, badness;
1554
1555 /* set num_dacs once to full for look_for_dac() */
1556 spec->multiout.num_dacs = cfg->line_outs;
1557 spec->multiout.dac_nids = spec->private_dac_nids;
1558 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1559 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1560 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1561 spec->multi_ios = 0;
1562 snd_array_free(&spec->paths);
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001563
1564 /* clear path indices */
1565 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1566 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1567 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1568 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1569 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
Takashi Iwaic697b712013-01-07 17:09:26 +01001570 memset(spec->input_paths, 0, sizeof(spec->input_paths));
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001571 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1572 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1573
Takashi Iwai352f7f92012-12-19 12:52:06 +01001574 badness = 0;
1575
1576 /* fill hard-wired DACs first */
1577 if (fill_hardwired) {
1578 bool mapped;
1579 do {
1580 mapped = map_singles(codec, cfg->line_outs,
1581 cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001582 spec->private_dac_nids,
1583 spec->out_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001584 mapped |= map_singles(codec, cfg->hp_outs,
1585 cfg->hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001586 spec->multiout.hp_out_nid,
1587 spec->hp_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001588 mapped |= map_singles(codec, cfg->speaker_outs,
1589 cfg->speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001590 spec->multiout.extra_out_nid,
1591 spec->speaker_paths);
Takashi Iwaida96fb52013-07-29 16:54:36 +02001592 if (!spec->no_multi_io &&
1593 fill_mio_first && cfg->line_outs == 1 &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01001594 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001595 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001596 if (!err)
1597 mapped = true;
1598 }
1599 } while (mapped);
1600 }
1601
1602 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001603 spec->private_dac_nids, spec->out_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001604 spec->main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001605
Takashi Iwaida96fb52013-07-29 16:54:36 +02001606 if (!spec->no_multi_io && fill_mio_first &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01001607 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1608 /* try to fill multi-io first */
Takashi Iwaie22aab72013-01-04 14:50:04 +01001609 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001610 if (err < 0)
1611 return err;
1612 /* we don't count badness at this stage yet */
1613 }
1614
1615 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1616 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1617 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001618 spec->hp_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001619 spec->extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001620 if (err < 0)
1621 return err;
1622 badness += err;
1623 }
1624 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1625 err = try_assign_dacs(codec, cfg->speaker_outs,
1626 cfg->speaker_pins,
1627 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001628 spec->speaker_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001629 spec->extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001630 if (err < 0)
1631 return err;
1632 badness += err;
1633 }
Takashi Iwaida96fb52013-07-29 16:54:36 +02001634 if (!spec->no_multi_io &&
1635 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001636 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001637 if (err < 0)
1638 return err;
1639 badness += err;
1640 }
Takashi Iwaie22aab72013-01-04 14:50:04 +01001641
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001642 if (spec->mixer_nid) {
1643 spec->aamix_out_paths[0] =
1644 check_aamix_out_path(codec, spec->out_paths[0]);
1645 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1646 spec->aamix_out_paths[1] =
1647 check_aamix_out_path(codec, spec->hp_paths[0]);
1648 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1649 spec->aamix_out_paths[2] =
1650 check_aamix_out_path(codec, spec->speaker_paths[0]);
1651 }
1652
Takashi Iwaida96fb52013-07-29 16:54:36 +02001653 if (!spec->no_multi_io &&
1654 cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
Takashi Iwaie22aab72013-01-04 14:50:04 +01001655 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1656 spec->multi_ios = 1; /* give badness */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001657
Takashi Iwaia07a9492013-01-07 16:44:06 +01001658 /* re-count num_dacs and squash invalid entries */
1659 spec->multiout.num_dacs = 0;
1660 for (i = 0; i < cfg->line_outs; i++) {
1661 if (spec->private_dac_nids[i])
1662 spec->multiout.num_dacs++;
1663 else {
1664 memmove(spec->private_dac_nids + i,
1665 spec->private_dac_nids + i + 1,
1666 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1667 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1668 }
1669 }
1670
1671 spec->ext_channel_count = spec->min_channel_count =
David Henningssonc0f3b212013-01-16 11:45:37 +01001672 spec->multiout.num_dacs * 2;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001673
Takashi Iwai352f7f92012-12-19 12:52:06 +01001674 if (spec->multi_ios == 2) {
1675 for (i = 0; i < 2; i++)
1676 spec->private_dac_nids[spec->multiout.num_dacs++] =
1677 spec->multi_io[i].dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001678 } else if (spec->multi_ios) {
1679 spec->multi_ios = 0;
1680 badness += BAD_MULTI_IO;
1681 }
1682
Takashi Iwai55a63d42013-03-21 17:20:12 +01001683 if (spec->indep_hp && !indep_hp_possible(codec))
1684 badness += BAD_NO_INDEP_HP;
1685
Takashi Iwaia07a9492013-01-07 16:44:06 +01001686 /* re-fill the shared DAC for speaker / headphone */
1687 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1688 refill_shared_dacs(codec, cfg->hp_outs,
1689 spec->multiout.hp_out_nid,
1690 spec->hp_paths);
1691 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1692 refill_shared_dacs(codec, cfg->speaker_outs,
1693 spec->multiout.extra_out_nid,
1694 spec->speaker_paths);
1695
Takashi Iwai352f7f92012-12-19 12:52:06 +01001696 return badness;
1697}
1698
1699#define DEBUG_BADNESS
1700
1701#ifdef DEBUG_BADNESS
1702#define debug_badness snd_printdd
1703#else
1704#define debug_badness(...)
1705#endif
1706
Takashi Iwaia7694092013-01-21 10:43:18 +01001707#ifdef DEBUG_BADNESS
1708static inline void print_nid_path_idx(struct hda_codec *codec,
1709 const char *pfx, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001710{
Takashi Iwaia7694092013-01-21 10:43:18 +01001711 struct nid_path *path;
1712
1713 path = snd_hda_get_path_from_idx(codec, idx);
1714 if (path)
1715 print_nid_path(pfx, path);
1716}
1717
1718static void debug_show_configs(struct hda_codec *codec,
1719 struct auto_pin_cfg *cfg)
1720{
1721 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia7694092013-01-21 10:43:18 +01001722 static const char * const lo_type[3] = { "LO", "SP", "HP" };
Takashi Iwaia7694092013-01-21 10:43:18 +01001723 int i;
1724
1725 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001726 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001727 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001728 spec->multiout.dac_nids[0],
1729 spec->multiout.dac_nids[1],
1730 spec->multiout.dac_nids[2],
Takashi Iwaia7694092013-01-21 10:43:18 +01001731 spec->multiout.dac_nids[3],
1732 lo_type[cfg->line_out_type]);
1733 for (i = 0; i < cfg->line_outs; i++)
1734 print_nid_path_idx(codec, " out", spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001735 if (spec->multi_ios > 0)
1736 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1737 spec->multi_ios,
1738 spec->multi_io[0].pin, spec->multi_io[1].pin,
1739 spec->multi_io[0].dac, spec->multi_io[1].dac);
Takashi Iwaia7694092013-01-21 10:43:18 +01001740 for (i = 0; i < spec->multi_ios; i++)
1741 print_nid_path_idx(codec, " mio",
1742 spec->out_paths[cfg->line_outs + i]);
1743 if (cfg->hp_outs)
1744 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001745 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001746 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001747 spec->multiout.hp_out_nid[0],
1748 spec->multiout.hp_out_nid[1],
1749 spec->multiout.hp_out_nid[2],
1750 spec->multiout.hp_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001751 for (i = 0; i < cfg->hp_outs; i++)
1752 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1753 if (cfg->speaker_outs)
1754 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001755 cfg->speaker_pins[0], cfg->speaker_pins[1],
1756 cfg->speaker_pins[2], cfg->speaker_pins[3],
1757 spec->multiout.extra_out_nid[0],
1758 spec->multiout.extra_out_nid[1],
1759 spec->multiout.extra_out_nid[2],
1760 spec->multiout.extra_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001761 for (i = 0; i < cfg->speaker_outs; i++)
1762 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1763 for (i = 0; i < 3; i++)
1764 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001765}
Takashi Iwaia7694092013-01-21 10:43:18 +01001766#else
1767#define debug_show_configs(codec, cfg) /* NOP */
1768#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01001769
1770/* find all available DACs of the codec */
1771static void fill_all_dac_nids(struct hda_codec *codec)
1772{
1773 struct hda_gen_spec *spec = codec->spec;
1774 int i;
1775 hda_nid_t nid = codec->start_nid;
1776
1777 spec->num_all_dacs = 0;
1778 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1779 for (i = 0; i < codec->num_nodes; i++, nid++) {
1780 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1781 continue;
1782 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1783 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1784 break;
1785 }
1786 spec->all_dacs[spec->num_all_dacs++] = nid;
1787 }
1788}
1789
1790static int parse_output_paths(struct hda_codec *codec)
1791{
1792 struct hda_gen_spec *spec = codec->spec;
1793 struct auto_pin_cfg *cfg = &spec->autocfg;
1794 struct auto_pin_cfg *best_cfg;
Takashi Iwai9314a582013-01-21 10:49:05 +01001795 unsigned int val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001796 int best_badness = INT_MAX;
1797 int badness;
1798 bool fill_hardwired = true, fill_mio_first = true;
1799 bool best_wired = true, best_mio = true;
1800 bool hp_spk_swapped = false;
1801
Takashi Iwai352f7f92012-12-19 12:52:06 +01001802 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1803 if (!best_cfg)
1804 return -ENOMEM;
1805 *best_cfg = *cfg;
1806
1807 for (;;) {
1808 badness = fill_and_eval_dacs(codec, fill_hardwired,
1809 fill_mio_first);
1810 if (badness < 0) {
1811 kfree(best_cfg);
1812 return badness;
1813 }
1814 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1815 cfg->line_out_type, fill_hardwired, fill_mio_first,
1816 badness);
Takashi Iwaia7694092013-01-21 10:43:18 +01001817 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001818 if (badness < best_badness) {
1819 best_badness = badness;
1820 *best_cfg = *cfg;
1821 best_wired = fill_hardwired;
1822 best_mio = fill_mio_first;
1823 }
1824 if (!badness)
1825 break;
1826 fill_mio_first = !fill_mio_first;
1827 if (!fill_mio_first)
1828 continue;
1829 fill_hardwired = !fill_hardwired;
1830 if (!fill_hardwired)
1831 continue;
1832 if (hp_spk_swapped)
1833 break;
1834 hp_spk_swapped = true;
1835 if (cfg->speaker_outs > 0 &&
1836 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1837 cfg->hp_outs = cfg->line_outs;
1838 memcpy(cfg->hp_pins, cfg->line_out_pins,
1839 sizeof(cfg->hp_pins));
1840 cfg->line_outs = cfg->speaker_outs;
1841 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1842 sizeof(cfg->speaker_pins));
1843 cfg->speaker_outs = 0;
1844 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1845 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1846 fill_hardwired = true;
1847 continue;
1848 }
1849 if (cfg->hp_outs > 0 &&
1850 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1851 cfg->speaker_outs = cfg->line_outs;
1852 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1853 sizeof(cfg->speaker_pins));
1854 cfg->line_outs = cfg->hp_outs;
1855 memcpy(cfg->line_out_pins, cfg->hp_pins,
1856 sizeof(cfg->hp_pins));
1857 cfg->hp_outs = 0;
1858 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1859 cfg->line_out_type = AUTO_PIN_HP_OUT;
1860 fill_hardwired = true;
1861 continue;
1862 }
1863 break;
1864 }
1865
1866 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001867 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001868 *cfg = *best_cfg;
1869 fill_and_eval_dacs(codec, best_wired, best_mio);
1870 }
1871 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1872 cfg->line_out_type, best_wired, best_mio);
Takashi Iwaia7694092013-01-21 10:43:18 +01001873 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001874
1875 if (cfg->line_out_pins[0]) {
1876 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01001877 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001878 if (path)
1879 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01001880 if (spec->vmaster_nid)
1881 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
1882 HDA_OUTPUT, spec->vmaster_tlv);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001883 }
1884
Takashi Iwai9314a582013-01-21 10:49:05 +01001885 /* set initial pinctl targets */
1886 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
1887 val = PIN_HP;
1888 else
1889 val = PIN_OUT;
1890 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
1891 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1892 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
1893 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1894 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
1895 set_pin_targets(codec, cfg->speaker_outs,
1896 cfg->speaker_pins, val);
1897 }
1898
Takashi Iwai55a63d42013-03-21 17:20:12 +01001899 /* clear indep_hp flag if not available */
1900 if (spec->indep_hp && !indep_hp_possible(codec))
1901 spec->indep_hp = 0;
1902
Takashi Iwai352f7f92012-12-19 12:52:06 +01001903 kfree(best_cfg);
1904 return 0;
1905}
1906
1907/* add playback controls from the parsed DAC table */
1908static int create_multi_out_ctls(struct hda_codec *codec,
1909 const struct auto_pin_cfg *cfg)
1910{
1911 struct hda_gen_spec *spec = codec->spec;
1912 int i, err, noutputs;
1913
1914 noutputs = cfg->line_outs;
1915 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1916 noutputs += spec->multi_ios;
1917
1918 for (i = 0; i < noutputs; i++) {
1919 const char *name;
1920 int index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001921 struct nid_path *path;
1922
Takashi Iwai196c17662013-01-04 15:01:40 +01001923 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001924 if (!path)
1925 continue;
Takashi Iwai247d85e2013-01-17 16:18:11 +01001926
1927 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001928 if (!name || !strcmp(name, "CLFE")) {
1929 /* Center/LFE */
1930 err = add_vol_ctl(codec, "Center", 0, 1, path);
1931 if (err < 0)
1932 return err;
1933 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1934 if (err < 0)
1935 return err;
Takashi Iwai247d85e2013-01-17 16:18:11 +01001936 } else {
1937 err = add_stereo_vol(codec, name, index, path);
1938 if (err < 0)
1939 return err;
1940 }
1941
1942 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
1943 if (!name || !strcmp(name, "CLFE")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001944 err = add_sw_ctl(codec, "Center", 0, 1, path);
1945 if (err < 0)
1946 return err;
1947 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1948 if (err < 0)
1949 return err;
1950 } else {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001951 err = add_stereo_sw(codec, name, index, path);
1952 if (err < 0)
1953 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 }
1955 }
1956 return 0;
1957}
1958
Takashi Iwaic2c80382013-01-07 10:33:57 +01001959static int create_extra_out(struct hda_codec *codec, int path_idx,
Takashi Iwai196c17662013-01-04 15:01:40 +01001960 const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001962 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 int err;
1964
Takashi Iwai196c17662013-01-04 15:01:40 +01001965 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001966 if (!path)
1967 return 0;
Takashi Iwaic2c80382013-01-07 10:33:57 +01001968 err = add_stereo_vol(codec, pfx, cidx, path);
1969 if (err < 0)
1970 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001971 err = add_stereo_sw(codec, pfx, cidx, path);
1972 if (err < 0)
1973 return err;
1974 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975}
1976
Takashi Iwai352f7f92012-12-19 12:52:06 +01001977/* add playback controls for speaker and HP outputs */
1978static int create_extra_outs(struct hda_codec *codec, int num_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001979 const int *paths, const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980{
Takashi Iwaic2c80382013-01-07 10:33:57 +01001981 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001982
1983 for (i = 0; i < num_pins; i++) {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001984 const char *name;
Takashi Iwai975cc022013-06-28 11:56:49 +02001985 char tmp[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwaic2c80382013-01-07 10:33:57 +01001986 int err, idx = 0;
1987
1988 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
1989 name = "Bass Speaker";
1990 else if (num_pins >= 3) {
1991 snprintf(tmp, sizeof(tmp), "%s %s",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001992 pfx, channel_name[i]);
Takashi Iwaic2c80382013-01-07 10:33:57 +01001993 name = tmp;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001994 } else {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001995 name = pfx;
1996 idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 }
Takashi Iwaic2c80382013-01-07 10:33:57 +01001998 err = create_extra_out(codec, paths[i], name, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001999 if (err < 0)
2000 return err;
2001 }
2002 return 0;
2003}
Takashi Iwai97ec5582006-03-21 11:29:07 +01002004
Takashi Iwai352f7f92012-12-19 12:52:06 +01002005static int create_hp_out_ctls(struct hda_codec *codec)
2006{
2007 struct hda_gen_spec *spec = codec->spec;
2008 return create_extra_outs(codec, spec->autocfg.hp_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01002009 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002010 "Headphone");
2011}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
Takashi Iwai352f7f92012-12-19 12:52:06 +01002013static int create_speaker_out_ctls(struct hda_codec *codec)
2014{
2015 struct hda_gen_spec *spec = codec->spec;
2016 return create_extra_outs(codec, spec->autocfg.speaker_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01002017 spec->speaker_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002018 "Speaker");
2019}
2020
2021/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002022 * independent HP controls
2023 */
2024
Takashi Iwai963afde2013-05-31 15:20:31 +02002025static void call_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002026static int indep_hp_info(struct snd_kcontrol *kcontrol,
2027 struct snd_ctl_elem_info *uinfo)
2028{
2029 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2030}
2031
2032static int indep_hp_get(struct snd_kcontrol *kcontrol,
2033 struct snd_ctl_elem_value *ucontrol)
2034{
2035 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2036 struct hda_gen_spec *spec = codec->spec;
2037 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
2038 return 0;
2039}
2040
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002041static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2042 int nomix_path_idx, int mix_path_idx,
2043 int out_type);
2044
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002045static int indep_hp_put(struct snd_kcontrol *kcontrol,
2046 struct snd_ctl_elem_value *ucontrol)
2047{
2048 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2049 struct hda_gen_spec *spec = codec->spec;
2050 unsigned int select = ucontrol->value.enumerated.item[0];
2051 int ret = 0;
2052
2053 mutex_lock(&spec->pcm_mutex);
2054 if (spec->active_streams) {
2055 ret = -EBUSY;
2056 goto unlock;
2057 }
2058
2059 if (spec->indep_hp_enabled != select) {
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002060 hda_nid_t *dacp;
2061 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2062 dacp = &spec->private_dac_nids[0];
2063 else
2064 dacp = &spec->multiout.hp_out_nid[0];
2065
2066 /* update HP aamix paths in case it conflicts with indep HP */
2067 if (spec->have_aamix_ctl) {
2068 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2069 update_aamix_paths(codec, spec->aamix_mode,
2070 spec->out_paths[0],
2071 spec->aamix_out_paths[0],
2072 spec->autocfg.line_out_type);
2073 else
2074 update_aamix_paths(codec, spec->aamix_mode,
2075 spec->hp_paths[0],
2076 spec->aamix_out_paths[1],
2077 AUTO_PIN_HP_OUT);
2078 }
2079
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002080 spec->indep_hp_enabled = select;
2081 if (spec->indep_hp_enabled)
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002082 *dacp = 0;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002083 else
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002084 *dacp = spec->alt_dac_nid;
Takashi Iwai92603c52013-01-22 07:46:31 +01002085
Takashi Iwai963afde2013-05-31 15:20:31 +02002086 call_hp_automute(codec, NULL);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002087 ret = 1;
2088 }
2089 unlock:
2090 mutex_unlock(&spec->pcm_mutex);
2091 return ret;
2092}
2093
2094static const struct snd_kcontrol_new indep_hp_ctl = {
2095 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2096 .name = "Independent HP",
2097 .info = indep_hp_info,
2098 .get = indep_hp_get,
2099 .put = indep_hp_put,
2100};
2101
2102
2103static int create_indep_hp_ctls(struct hda_codec *codec)
2104{
2105 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002106 hda_nid_t dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002107
2108 if (!spec->indep_hp)
2109 return 0;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002110 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2111 dac = spec->multiout.dac_nids[0];
2112 else
2113 dac = spec->multiout.hp_out_nid[0];
2114 if (!dac) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002115 spec->indep_hp = 0;
2116 return 0;
2117 }
2118
2119 spec->indep_hp_enabled = false;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002120 spec->alt_dac_nid = dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002121 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
2122 return -ENOMEM;
2123 return 0;
2124}
2125
2126/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002127 * channel mode enum control
2128 */
2129
2130static int ch_mode_info(struct snd_kcontrol *kcontrol,
2131 struct snd_ctl_elem_info *uinfo)
2132{
2133 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2134 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002135 int chs;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002136
2137 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2138 uinfo->count = 1;
2139 uinfo->value.enumerated.items = spec->multi_ios + 1;
2140 if (uinfo->value.enumerated.item > spec->multi_ios)
2141 uinfo->value.enumerated.item = spec->multi_ios;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002142 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2143 sprintf(uinfo->value.enumerated.name, "%dch", chs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002144 return 0;
2145}
2146
2147static int ch_mode_get(struct snd_kcontrol *kcontrol,
2148 struct snd_ctl_elem_value *ucontrol)
2149{
2150 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2151 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002152 ucontrol->value.enumerated.item[0] =
2153 (spec->ext_channel_count - spec->min_channel_count) / 2;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002154 return 0;
2155}
2156
Takashi Iwai196c17662013-01-04 15:01:40 +01002157static inline struct nid_path *
2158get_multiio_path(struct hda_codec *codec, int idx)
2159{
2160 struct hda_gen_spec *spec = codec->spec;
2161 return snd_hda_get_path_from_idx(codec,
2162 spec->out_paths[spec->autocfg.line_outs + idx]);
2163}
2164
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002165static void update_automute_all(struct hda_codec *codec);
2166
Takashi Iwai65033cc2013-04-16 12:31:05 +02002167/* Default value to be passed as aamix argument for snd_hda_activate_path();
2168 * used for output paths
2169 */
2170static bool aamix_default(struct hda_gen_spec *spec)
2171{
2172 return !spec->have_aamix_ctl || spec->aamix_mode;
2173}
2174
Takashi Iwai352f7f92012-12-19 12:52:06 +01002175static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2176{
2177 struct hda_gen_spec *spec = codec->spec;
2178 hda_nid_t nid = spec->multi_io[idx].pin;
2179 struct nid_path *path;
2180
Takashi Iwai196c17662013-01-04 15:01:40 +01002181 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002182 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002184
2185 if (path->active == output)
2186 return 0;
2187
2188 if (output) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01002189 set_pin_target(codec, nid, PIN_OUT, true);
Takashi Iwai65033cc2013-04-16 12:31:05 +02002190 snd_hda_activate_path(codec, path, true, aamix_default(spec));
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002191 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002192 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002193 set_pin_eapd(codec, nid, false);
Takashi Iwai65033cc2013-04-16 12:31:05 +02002194 snd_hda_activate_path(codec, path, false, aamix_default(spec));
Takashi Iwai2c12c302013-01-10 09:33:29 +01002195 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002196 path_power_down_sync(codec, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 }
Takashi Iwaia365fed2013-01-10 16:10:06 +01002198
2199 /* update jack retasking in case it modifies any of them */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002200 update_automute_all(codec);
Takashi Iwaia365fed2013-01-10 16:10:06 +01002201
Takashi Iwai352f7f92012-12-19 12:52:06 +01002202 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203}
2204
Takashi Iwai352f7f92012-12-19 12:52:06 +01002205static int ch_mode_put(struct snd_kcontrol *kcontrol,
2206 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002208 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2209 struct hda_gen_spec *spec = codec->spec;
2210 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
Takashi Iwai352f7f92012-12-19 12:52:06 +01002212 ch = ucontrol->value.enumerated.item[0];
2213 if (ch < 0 || ch > spec->multi_ios)
2214 return -EINVAL;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002215 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002216 return 0;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002217 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002218 for (i = 0; i < spec->multi_ios; i++)
2219 set_multi_io(codec, i, i < ch);
2220 spec->multiout.max_channels = max(spec->ext_channel_count,
2221 spec->const_channel_count);
2222 if (spec->need_dac_fix)
2223 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 return 1;
2225}
2226
Takashi Iwai352f7f92012-12-19 12:52:06 +01002227static const struct snd_kcontrol_new channel_mode_enum = {
2228 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2229 .name = "Channel Mode",
2230 .info = ch_mode_info,
2231 .get = ch_mode_get,
2232 .put = ch_mode_put,
2233};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
Takashi Iwai352f7f92012-12-19 12:52:06 +01002235static int create_multi_channel_mode(struct hda_codec *codec)
2236{
2237 struct hda_gen_spec *spec = codec->spec;
2238
2239 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002240 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002241 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 return 0;
2244}
2245
Takashi Iwai352f7f92012-12-19 12:52:06 +01002246/*
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002247 * aamix loopback enable/disable switch
2248 */
2249
2250#define loopback_mixing_info indep_hp_info
2251
2252static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2253 struct snd_ctl_elem_value *ucontrol)
2254{
2255 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2256 struct hda_gen_spec *spec = codec->spec;
2257 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2258 return 0;
2259}
2260
2261static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002262 int nomix_path_idx, int mix_path_idx,
2263 int out_type)
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002264{
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002265 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002266 struct nid_path *nomix_path, *mix_path;
2267
2268 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2269 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2270 if (!nomix_path || !mix_path)
2271 return;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002272
2273 /* if HP aamix path is driven from a different DAC and the
2274 * independent HP mode is ON, can't turn on aamix path
2275 */
2276 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2277 mix_path->path[0] != spec->alt_dac_nid)
2278 do_mix = false;
2279
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002280 if (do_mix) {
2281 snd_hda_activate_path(codec, nomix_path, false, true);
2282 snd_hda_activate_path(codec, mix_path, true, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002283 path_power_down_sync(codec, nomix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002284 } else {
Takashi Iwai65033cc2013-04-16 12:31:05 +02002285 snd_hda_activate_path(codec, mix_path, false, false);
2286 snd_hda_activate_path(codec, nomix_path, true, false);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002287 path_power_down_sync(codec, mix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002288 }
2289}
2290
2291static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2292 struct snd_ctl_elem_value *ucontrol)
2293{
2294 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2295 struct hda_gen_spec *spec = codec->spec;
2296 unsigned int val = ucontrol->value.enumerated.item[0];
2297
2298 if (val == spec->aamix_mode)
2299 return 0;
2300 spec->aamix_mode = val;
2301 update_aamix_paths(codec, val, spec->out_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002302 spec->aamix_out_paths[0],
2303 spec->autocfg.line_out_type);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002304 update_aamix_paths(codec, val, spec->hp_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002305 spec->aamix_out_paths[1],
2306 AUTO_PIN_HP_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002307 update_aamix_paths(codec, val, spec->speaker_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002308 spec->aamix_out_paths[2],
2309 AUTO_PIN_SPEAKER_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002310 return 1;
2311}
2312
2313static const struct snd_kcontrol_new loopback_mixing_enum = {
2314 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2315 .name = "Loopback Mixing",
2316 .info = loopback_mixing_info,
2317 .get = loopback_mixing_get,
2318 .put = loopback_mixing_put,
2319};
2320
2321static int create_loopback_mixing_ctl(struct hda_codec *codec)
2322{
2323 struct hda_gen_spec *spec = codec->spec;
2324
2325 if (!spec->mixer_nid)
2326 return 0;
2327 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
2328 spec->aamix_out_paths[2]))
2329 return 0;
2330 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2331 return -ENOMEM;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002332 spec->have_aamix_ctl = 1;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002333 return 0;
2334}
2335
2336/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002337 * shared headphone/mic handling
2338 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002339
Takashi Iwai352f7f92012-12-19 12:52:06 +01002340static void call_update_outputs(struct hda_codec *codec);
2341
2342/* for shared I/O, change the pin-control accordingly */
Takashi Iwai967303d2013-02-19 17:12:42 +01002343static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002344{
2345 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai967303d2013-02-19 17:12:42 +01002346 bool as_mic;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002347 unsigned int val;
Takashi Iwai967303d2013-02-19 17:12:42 +01002348 hda_nid_t pin;
2349
2350 pin = spec->hp_mic_pin;
2351 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
2352
2353 if (!force) {
2354 val = snd_hda_codec_get_pin_target(codec, pin);
2355 if (as_mic) {
2356 if (val & PIN_IN)
2357 return;
2358 } else {
2359 if (val & PIN_OUT)
2360 return;
2361 }
2362 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002363
2364 val = snd_hda_get_default_vref(codec, pin);
Takashi Iwai967303d2013-02-19 17:12:42 +01002365 /* if the HP pin doesn't support VREF and the codec driver gives an
2366 * alternative pin, set up the VREF on that pin instead
2367 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002368 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2369 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2370 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2371 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01002372 snd_hda_set_pin_ctl_cache(codec, vref_pin,
Takashi Iwai967303d2013-02-19 17:12:42 +01002373 PIN_IN | (as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02002374 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002375
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002376 if (!spec->hp_mic_jack_modes) {
2377 if (as_mic)
2378 val |= PIN_IN;
2379 else
2380 val = PIN_HP;
2381 set_pin_target(codec, pin, val, true);
Takashi Iwai963afde2013-05-31 15:20:31 +02002382 call_hp_automute(codec, NULL);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002383 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002384}
2385
2386/* create a shared input with the headphone out */
Takashi Iwai967303d2013-02-19 17:12:42 +01002387static int create_hp_mic(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002388{
2389 struct hda_gen_spec *spec = codec->spec;
2390 struct auto_pin_cfg *cfg = &spec->autocfg;
2391 unsigned int defcfg;
2392 hda_nid_t nid;
2393
Takashi Iwai967303d2013-02-19 17:12:42 +01002394 if (!spec->hp_mic) {
2395 if (spec->suppress_hp_mic_detect)
2396 return 0;
2397 /* automatic detection: only if no input or a single internal
2398 * input pin is found, try to detect the shared hp/mic
2399 */
2400 if (cfg->num_inputs > 1)
2401 return 0;
2402 else if (cfg->num_inputs == 1) {
2403 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2404 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2405 return 0;
2406 }
2407 }
2408
2409 spec->hp_mic = 0; /* clear once */
2410 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002411 return 0;
2412
Takashi Iwai967303d2013-02-19 17:12:42 +01002413 nid = 0;
2414 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2415 nid = cfg->line_out_pins[0];
2416 else if (cfg->hp_outs > 0)
2417 nid = cfg->hp_pins[0];
2418 if (!nid)
2419 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002420
2421 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2422 return 0; /* no input */
2423
Takashi Iwai967303d2013-02-19 17:12:42 +01002424 cfg->inputs[cfg->num_inputs].pin = nid;
2425 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
David Henningssoncb420b12013-04-11 11:30:28 +02002426 cfg->inputs[cfg->num_inputs].is_headphone_mic = 1;
Takashi Iwai967303d2013-02-19 17:12:42 +01002427 cfg->num_inputs++;
2428 spec->hp_mic = 1;
2429 spec->hp_mic_pin = nid;
2430 /* we can't handle auto-mic together with HP-mic */
2431 spec->suppress_auto_mic = 1;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002432 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
2433 return 0;
2434}
2435
Takashi Iwai978e77e2013-01-10 16:57:58 +01002436/*
2437 * output jack mode
2438 */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002439
2440static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2441
2442static const char * const out_jack_texts[] = {
2443 "Line Out", "Headphone Out",
2444};
2445
Takashi Iwai978e77e2013-01-10 16:57:58 +01002446static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2447 struct snd_ctl_elem_info *uinfo)
2448{
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002449 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
Takashi Iwai978e77e2013-01-10 16:57:58 +01002450}
2451
2452static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2453 struct snd_ctl_elem_value *ucontrol)
2454{
2455 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2456 hda_nid_t nid = kcontrol->private_value;
2457 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2458 ucontrol->value.enumerated.item[0] = 1;
2459 else
2460 ucontrol->value.enumerated.item[0] = 0;
2461 return 0;
2462}
2463
2464static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2465 struct snd_ctl_elem_value *ucontrol)
2466{
2467 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2468 hda_nid_t nid = kcontrol->private_value;
2469 unsigned int val;
2470
2471 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2472 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2473 return 0;
2474 snd_hda_set_pin_ctl_cache(codec, nid, val);
2475 return 1;
2476}
2477
2478static const struct snd_kcontrol_new out_jack_mode_enum = {
2479 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2480 .info = out_jack_mode_info,
2481 .get = out_jack_mode_get,
2482 .put = out_jack_mode_put,
2483};
2484
2485static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2486{
2487 struct hda_gen_spec *spec = codec->spec;
2488 int i;
2489
2490 for (i = 0; i < spec->kctls.used; i++) {
2491 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2492 if (!strcmp(kctl->name, name) && kctl->index == idx)
2493 return true;
2494 }
2495 return false;
2496}
2497
2498static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2499 char *name, size_t name_len)
2500{
2501 struct hda_gen_spec *spec = codec->spec;
2502 int idx = 0;
2503
2504 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2505 strlcat(name, " Jack Mode", name_len);
2506
2507 for (; find_kctl_name(codec, name, idx); idx++)
2508 ;
2509}
2510
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002511static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2512{
2513 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002514 if (spec->add_jack_modes) {
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002515 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2516 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2517 return 2;
2518 }
2519 return 1;
2520}
2521
Takashi Iwai978e77e2013-01-10 16:57:58 +01002522static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2523 hda_nid_t *pins)
2524{
2525 struct hda_gen_spec *spec = codec->spec;
2526 int i;
2527
2528 for (i = 0; i < num_pins; i++) {
2529 hda_nid_t pin = pins[i];
Takashi Iwaiced4cef2013-11-26 08:33:45 +01002530 if (pin == spec->hp_mic_pin)
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002531 continue;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002532 if (get_out_jack_num_items(codec, pin) > 1) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01002533 struct snd_kcontrol_new *knew;
Takashi Iwai975cc022013-06-28 11:56:49 +02002534 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai978e77e2013-01-10 16:57:58 +01002535 get_jack_mode_name(codec, pin, name, sizeof(name));
2536 knew = snd_hda_gen_add_kctl(spec, name,
2537 &out_jack_mode_enum);
2538 if (!knew)
2539 return -ENOMEM;
2540 knew->private_value = pin;
2541 }
2542 }
2543
2544 return 0;
2545}
2546
Takashi Iwai294765582013-01-17 09:52:11 +01002547/*
2548 * input jack mode
2549 */
2550
2551/* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2552#define NUM_VREFS 6
2553
2554static const char * const vref_texts[NUM_VREFS] = {
2555 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2556 "", "Mic 80pc Bias", "Mic 100pc Bias"
2557};
2558
2559static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2560{
2561 unsigned int pincap;
2562
2563 pincap = snd_hda_query_pin_caps(codec, pin);
2564 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2565 /* filter out unusual vrefs */
2566 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2567 return pincap;
2568}
2569
2570/* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2571static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2572{
2573 unsigned int i, n = 0;
2574
2575 for (i = 0; i < NUM_VREFS; i++) {
2576 if (vref_caps & (1 << i)) {
2577 if (n == item_idx)
2578 return i;
2579 n++;
2580 }
2581 }
2582 return 0;
2583}
2584
2585/* convert back from the vref ctl index to the enum item index */
2586static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2587{
2588 unsigned int i, n = 0;
2589
2590 for (i = 0; i < NUM_VREFS; i++) {
2591 if (i == idx)
2592 return n;
2593 if (vref_caps & (1 << i))
2594 n++;
2595 }
2596 return 0;
2597}
2598
2599static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2600 struct snd_ctl_elem_info *uinfo)
2601{
2602 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2603 hda_nid_t nid = kcontrol->private_value;
2604 unsigned int vref_caps = get_vref_caps(codec, nid);
2605
2606 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2607 vref_texts);
2608 /* set the right text */
2609 strcpy(uinfo->value.enumerated.name,
2610 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2611 return 0;
2612}
2613
2614static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2615 struct snd_ctl_elem_value *ucontrol)
2616{
2617 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2618 hda_nid_t nid = kcontrol->private_value;
2619 unsigned int vref_caps = get_vref_caps(codec, nid);
2620 unsigned int idx;
2621
2622 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2623 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2624 return 0;
2625}
2626
2627static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2628 struct snd_ctl_elem_value *ucontrol)
2629{
2630 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2631 hda_nid_t nid = kcontrol->private_value;
2632 unsigned int vref_caps = get_vref_caps(codec, nid);
2633 unsigned int val, idx;
2634
2635 val = snd_hda_codec_get_pin_target(codec, nid);
2636 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2637 if (idx == ucontrol->value.enumerated.item[0])
2638 return 0;
2639
2640 val &= ~AC_PINCTL_VREFEN;
2641 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2642 snd_hda_set_pin_ctl_cache(codec, nid, val);
2643 return 1;
2644}
2645
2646static const struct snd_kcontrol_new in_jack_mode_enum = {
2647 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2648 .info = in_jack_mode_info,
2649 .get = in_jack_mode_get,
2650 .put = in_jack_mode_put,
2651};
2652
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002653static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2654{
2655 struct hda_gen_spec *spec = codec->spec;
2656 int nitems = 0;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002657 if (spec->add_jack_modes)
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002658 nitems = hweight32(get_vref_caps(codec, pin));
2659 return nitems ? nitems : 1;
2660}
2661
Takashi Iwai294765582013-01-17 09:52:11 +01002662static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2663{
2664 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai294765582013-01-17 09:52:11 +01002665 struct snd_kcontrol_new *knew;
Takashi Iwai975cc022013-06-28 11:56:49 +02002666 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002667 unsigned int defcfg;
2668
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002669 if (pin == spec->hp_mic_pin)
2670 return 0; /* already done in create_out_jack_mode() */
Takashi Iwai294765582013-01-17 09:52:11 +01002671
2672 /* no jack mode for fixed pins */
2673 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2674 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2675 return 0;
2676
2677 /* no multiple vref caps? */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002678 if (get_in_jack_num_items(codec, pin) <= 1)
Takashi Iwai294765582013-01-17 09:52:11 +01002679 return 0;
2680
2681 get_jack_mode_name(codec, pin, name, sizeof(name));
2682 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2683 if (!knew)
2684 return -ENOMEM;
2685 knew->private_value = pin;
2686 return 0;
2687}
2688
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002689/*
2690 * HP/mic shared jack mode
2691 */
2692static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2693 struct snd_ctl_elem_info *uinfo)
2694{
2695 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2696 hda_nid_t nid = kcontrol->private_value;
2697 int out_jacks = get_out_jack_num_items(codec, nid);
2698 int in_jacks = get_in_jack_num_items(codec, nid);
2699 const char *text = NULL;
2700 int idx;
2701
2702 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2703 uinfo->count = 1;
2704 uinfo->value.enumerated.items = out_jacks + in_jacks;
2705 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2706 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2707 idx = uinfo->value.enumerated.item;
2708 if (idx < out_jacks) {
2709 if (out_jacks > 1)
2710 text = out_jack_texts[idx];
2711 else
2712 text = "Headphone Out";
2713 } else {
2714 idx -= out_jacks;
2715 if (in_jacks > 1) {
2716 unsigned int vref_caps = get_vref_caps(codec, nid);
2717 text = vref_texts[get_vref_idx(vref_caps, idx)];
2718 } else
2719 text = "Mic In";
2720 }
2721
2722 strcpy(uinfo->value.enumerated.name, text);
2723 return 0;
2724}
2725
2726static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2727{
2728 int out_jacks = get_out_jack_num_items(codec, nid);
2729 int in_jacks = get_in_jack_num_items(codec, nid);
2730 unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2731 int idx = 0;
2732
2733 if (val & PIN_OUT) {
2734 if (out_jacks > 1 && val == PIN_HP)
2735 idx = 1;
2736 } else if (val & PIN_IN) {
2737 idx = out_jacks;
2738 if (in_jacks > 1) {
2739 unsigned int vref_caps = get_vref_caps(codec, nid);
2740 val &= AC_PINCTL_VREFEN;
2741 idx += cvt_from_vref_idx(vref_caps, val);
2742 }
2743 }
2744 return idx;
2745}
2746
2747static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2748 struct snd_ctl_elem_value *ucontrol)
2749{
2750 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2751 hda_nid_t nid = kcontrol->private_value;
2752 ucontrol->value.enumerated.item[0] =
2753 get_cur_hp_mic_jack_mode(codec, nid);
2754 return 0;
2755}
2756
2757static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2758 struct snd_ctl_elem_value *ucontrol)
2759{
2760 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2761 hda_nid_t nid = kcontrol->private_value;
2762 int out_jacks = get_out_jack_num_items(codec, nid);
2763 int in_jacks = get_in_jack_num_items(codec, nid);
2764 unsigned int val, oldval, idx;
2765
2766 oldval = get_cur_hp_mic_jack_mode(codec, nid);
2767 idx = ucontrol->value.enumerated.item[0];
2768 if (oldval == idx)
2769 return 0;
2770
2771 if (idx < out_jacks) {
2772 if (out_jacks > 1)
2773 val = idx ? PIN_HP : PIN_OUT;
2774 else
2775 val = PIN_HP;
2776 } else {
2777 idx -= out_jacks;
2778 if (in_jacks > 1) {
2779 unsigned int vref_caps = get_vref_caps(codec, nid);
2780 val = snd_hda_codec_get_pin_target(codec, nid);
Takashi Iwai3f550e32013-03-07 18:30:27 +01002781 val &= ~(AC_PINCTL_VREFEN | PIN_HP);
2782 val |= get_vref_idx(vref_caps, idx) | PIN_IN;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002783 } else
Takashi Iwai16c0cefe2013-11-26 08:44:26 +01002784 val = snd_hda_get_default_vref(codec, nid) | PIN_IN;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002785 }
2786 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwai963afde2013-05-31 15:20:31 +02002787 call_hp_automute(codec, NULL);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002788
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002789 return 1;
2790}
2791
2792static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
2793 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2794 .info = hp_mic_jack_mode_info,
2795 .get = hp_mic_jack_mode_get,
2796 .put = hp_mic_jack_mode_put,
2797};
2798
2799static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2800{
2801 struct hda_gen_spec *spec = codec->spec;
2802 struct snd_kcontrol_new *knew;
2803
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002804 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
2805 &hp_mic_jack_mode_enum);
2806 if (!knew)
2807 return -ENOMEM;
2808 knew->private_value = pin;
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002809 spec->hp_mic_jack_modes = 1;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002810 return 0;
2811}
Takashi Iwai352f7f92012-12-19 12:52:06 +01002812
2813/*
2814 * Parse input paths
2815 */
2816
Takashi Iwai352f7f92012-12-19 12:52:06 +01002817/* add the powersave loopback-list entry */
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002818static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002819{
2820 struct hda_amp_list *list;
2821
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002822 list = snd_array_new(&spec->loopback_list);
2823 if (!list)
2824 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002825 list->nid = mix;
2826 list->dir = HDA_INPUT;
2827 list->idx = idx;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002828 spec->loopback.amplist = spec->loopback_list.list;
2829 return 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002830}
Takashi Iwaicb53c622007-08-10 17:21:45 +02002831
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002832/* return true if either a volume or a mute amp is found for the given
2833 * aamix path; the amp has to be either in the mixer node or its direct leaf
2834 */
2835static bool look_for_mix_leaf_ctls(struct hda_codec *codec, hda_nid_t mix_nid,
2836 hda_nid_t pin, unsigned int *mix_val,
2837 unsigned int *mute_val)
2838{
2839 int idx, num_conns;
2840 const hda_nid_t *list;
2841 hda_nid_t nid;
2842
2843 idx = snd_hda_get_conn_index(codec, mix_nid, pin, true);
2844 if (idx < 0)
2845 return false;
2846
2847 *mix_val = *mute_val = 0;
2848 if (nid_has_volume(codec, mix_nid, HDA_INPUT))
2849 *mix_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2850 if (nid_has_mute(codec, mix_nid, HDA_INPUT))
2851 *mute_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2852 if (*mix_val && *mute_val)
2853 return true;
2854
2855 /* check leaf node */
2856 num_conns = snd_hda_get_conn_list(codec, mix_nid, &list);
2857 if (num_conns < idx)
2858 return false;
2859 nid = list[idx];
2860 if (!*mix_val && nid_has_volume(codec, nid, HDA_OUTPUT))
2861 *mix_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2862 if (!*mute_val && nid_has_mute(codec, nid, HDA_OUTPUT))
2863 *mute_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2864
2865 return *mix_val || *mute_val;
2866}
2867
Takashi Iwai352f7f92012-12-19 12:52:06 +01002868/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01002869static int new_analog_input(struct hda_codec *codec, int input_idx,
2870 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002871 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002873 struct hda_gen_spec *spec = codec->spec;
2874 struct nid_path *path;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002875 unsigned int mix_val, mute_val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002876 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002878 if (!look_for_mix_leaf_ctls(codec, mix_nid, pin, &mix_val, &mute_val))
2879 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002880
Takashi Iwai3ca529d2013-01-07 17:25:08 +01002881 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002882 if (!path)
2883 return -EINVAL;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002884 print_nid_path("loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01002885 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002886
2887 idx = path->idx[path->depth - 1];
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002888 if (mix_val) {
2889 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, mix_val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002890 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 return err;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002892 path->ctls[NID_PATH_VOL_CTL] = mix_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 }
2894
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002895 if (mute_val) {
2896 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, mute_val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002897 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 return err;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002899 path->ctls[NID_PATH_MUTE_CTL] = mute_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 }
2901
Takashi Iwai352f7f92012-12-19 12:52:06 +01002902 path->active = true;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002903 err = add_loopback_list(spec, mix_nid, idx);
2904 if (err < 0)
2905 return err;
Takashi Iwaie4a395e2013-01-23 17:00:31 +01002906
2907 if (spec->mixer_nid != spec->mixer_merge_nid &&
2908 !spec->loopback_merge_path) {
2909 path = snd_hda_add_new_path(codec, spec->mixer_nid,
2910 spec->mixer_merge_nid, 0);
2911 if (path) {
2912 print_nid_path("loopback-merge", path);
2913 path->active = true;
2914 spec->loopback_merge_path =
2915 snd_hda_get_path_idx(codec, path);
2916 }
2917 }
2918
Takashi Iwai352f7f92012-12-19 12:52:06 +01002919 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920}
2921
Takashi Iwai352f7f92012-12-19 12:52:06 +01002922static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002924 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
2925 return (pincap & AC_PINCAP_IN) != 0;
2926}
2927
2928/* Parse the codec tree and retrieve ADCs */
2929static int fill_adc_nids(struct hda_codec *codec)
2930{
2931 struct hda_gen_spec *spec = codec->spec;
2932 hda_nid_t nid;
2933 hda_nid_t *adc_nids = spec->adc_nids;
2934 int max_nums = ARRAY_SIZE(spec->adc_nids);
2935 int i, nums = 0;
2936
2937 nid = codec->start_nid;
2938 for (i = 0; i < codec->num_nodes; i++, nid++) {
2939 unsigned int caps = get_wcaps(codec, nid);
2940 int type = get_wcaps_type(caps);
2941
2942 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2943 continue;
2944 adc_nids[nums] = nid;
2945 if (++nums >= max_nums)
2946 break;
2947 }
2948 spec->num_adc_nids = nums;
Takashi Iwai0ffd5342013-01-17 15:53:29 +01002949
2950 /* copy the detected ADCs to all_adcs[] */
2951 spec->num_all_adcs = nums;
2952 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
2953
Takashi Iwai352f7f92012-12-19 12:52:06 +01002954 return nums;
2955}
2956
2957/* filter out invalid adc_nids that don't give all active input pins;
2958 * if needed, check whether dynamic ADC-switching is available
2959 */
2960static int check_dyn_adc_switch(struct hda_codec *codec)
2961{
2962 struct hda_gen_spec *spec = codec->spec;
2963 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002964 unsigned int ok_bits;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002965 int i, n, nums;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002966
Takashi Iwai352f7f92012-12-19 12:52:06 +01002967 nums = 0;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002968 ok_bits = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002969 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002970 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002971 if (!spec->input_paths[i][n])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002972 break;
2973 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002974 if (i >= imux->num_items) {
2975 ok_bits |= (1 << n);
2976 nums++;
2977 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002978 }
2979
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002980 if (!ok_bits) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002981 /* check whether ADC-switch is possible */
2982 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002983 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002984 if (spec->input_paths[i][n]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002985 spec->dyn_adc_idx[i] = n;
2986 break;
2987 }
2988 }
2989 }
2990
2991 snd_printdd("hda-codec: enabling ADC switching\n");
2992 spec->dyn_adc_switch = 1;
2993 } else if (nums != spec->num_adc_nids) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002994 /* shrink the invalid adcs and input paths */
2995 nums = 0;
2996 for (n = 0; n < spec->num_adc_nids; n++) {
2997 if (!(ok_bits & (1 << n)))
2998 continue;
2999 if (n != nums) {
3000 spec->adc_nids[nums] = spec->adc_nids[n];
Takashi Iwai980428c2013-01-09 09:28:20 +01003001 for (i = 0; i < imux->num_items; i++) {
3002 invalidate_nid_path(codec,
3003 spec->input_paths[i][nums]);
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003004 spec->input_paths[i][nums] =
3005 spec->input_paths[i][n];
Takashi Iwai980428c2013-01-09 09:28:20 +01003006 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003007 }
3008 nums++;
3009 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003010 spec->num_adc_nids = nums;
3011 }
3012
Takashi Iwai967303d2013-02-19 17:12:42 +01003013 if (imux->num_items == 1 ||
3014 (imux->num_items == 2 && spec->hp_mic)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003015 snd_printdd("hda-codec: reducing to a single ADC\n");
3016 spec->num_adc_nids = 1; /* reduce to a single ADC */
3017 }
3018
3019 /* single index for individual volumes ctls */
3020 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
3021 spec->num_adc_nids = 1;
3022
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 return 0;
3024}
3025
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003026/* parse capture source paths from the given pin and create imux items */
3027static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
Takashi Iwai9dba2052013-01-18 10:01:15 +01003028 int cfg_idx, int num_adcs,
3029 const char *label, int anchor)
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003030{
3031 struct hda_gen_spec *spec = codec->spec;
3032 struct hda_input_mux *imux = &spec->input_mux;
3033 int imux_idx = imux->num_items;
3034 bool imux_added = false;
3035 int c;
3036
3037 for (c = 0; c < num_adcs; c++) {
3038 struct nid_path *path;
3039 hda_nid_t adc = spec->adc_nids[c];
3040
3041 if (!is_reachable_path(codec, pin, adc))
3042 continue;
3043 path = snd_hda_add_new_path(codec, pin, adc, anchor);
3044 if (!path)
3045 continue;
3046 print_nid_path("input", path);
3047 spec->input_paths[imux_idx][c] =
3048 snd_hda_get_path_idx(codec, path);
3049
3050 if (!imux_added) {
Takashi Iwai967303d2013-02-19 17:12:42 +01003051 if (spec->hp_mic_pin == pin)
3052 spec->hp_mic_mux_idx = imux->num_items;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003053 spec->imux_pins[imux->num_items] = pin;
Takashi Iwai9dba2052013-01-18 10:01:15 +01003054 snd_hda_add_imux_item(imux, label, cfg_idx, NULL);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003055 imux_added = true;
3056 }
3057 }
3058
3059 return 0;
3060}
3061
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003063 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 */
Takashi Iwai9dba2052013-01-18 10:01:15 +01003065
Takashi Iwaic9700422013-01-18 10:17:30 +01003066/* fill the label for each input at first */
3067static int fill_input_pin_labels(struct hda_codec *codec)
3068{
3069 struct hda_gen_spec *spec = codec->spec;
3070 const struct auto_pin_cfg *cfg = &spec->autocfg;
3071 int i;
3072
3073 for (i = 0; i < cfg->num_inputs; i++) {
3074 hda_nid_t pin = cfg->inputs[i].pin;
3075 const char *label;
3076 int j, idx;
3077
3078 if (!is_input_pin(codec, pin))
3079 continue;
3080
3081 label = hda_get_autocfg_input_label(codec, cfg, i);
3082 idx = 0;
David Henningsson8e8db7f2013-01-18 15:43:02 +01003083 for (j = i - 1; j >= 0; j--) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003084 if (spec->input_labels[j] &&
3085 !strcmp(spec->input_labels[j], label)) {
3086 idx = spec->input_label_idxs[j] + 1;
3087 break;
3088 }
3089 }
3090
3091 spec->input_labels[i] = label;
3092 spec->input_label_idxs[i] = idx;
3093 }
3094
3095 return 0;
3096}
3097
Takashi Iwai9dba2052013-01-18 10:01:15 +01003098#define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
3099
Takashi Iwai352f7f92012-12-19 12:52:06 +01003100static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003101{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003102 struct hda_gen_spec *spec = codec->spec;
3103 const struct auto_pin_cfg *cfg = &spec->autocfg;
3104 hda_nid_t mixer = spec->mixer_nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003105 int num_adcs;
Takashi Iwaic9700422013-01-18 10:17:30 +01003106 int i, err;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003107 unsigned int val;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003108
Takashi Iwai352f7f92012-12-19 12:52:06 +01003109 num_adcs = fill_adc_nids(codec);
3110 if (num_adcs < 0)
3111 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003112
Takashi Iwaic9700422013-01-18 10:17:30 +01003113 err = fill_input_pin_labels(codec);
3114 if (err < 0)
3115 return err;
3116
Takashi Iwai352f7f92012-12-19 12:52:06 +01003117 for (i = 0; i < cfg->num_inputs; i++) {
3118 hda_nid_t pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119
Takashi Iwai352f7f92012-12-19 12:52:06 +01003120 pin = cfg->inputs[i].pin;
3121 if (!is_input_pin(codec, pin))
3122 continue;
3123
Takashi Iwai2c12c302013-01-10 09:33:29 +01003124 val = PIN_IN;
3125 if (cfg->inputs[i].type == AUTO_PIN_MIC)
3126 val |= snd_hda_get_default_vref(codec, pin);
Takashi Iwai93c9d8a2013-03-11 09:48:43 +01003127 if (pin != spec->hp_mic_pin)
3128 set_pin_target(codec, pin, val, false);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003129
Takashi Iwai352f7f92012-12-19 12:52:06 +01003130 if (mixer) {
3131 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01003132 err = new_analog_input(codec, i, pin,
Takashi Iwaic9700422013-01-18 10:17:30 +01003133 spec->input_labels[i],
3134 spec->input_label_idxs[i],
3135 mixer);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003136 if (err < 0)
3137 return err;
3138 }
3139 }
3140
Takashi Iwaic9700422013-01-18 10:17:30 +01003141 err = parse_capture_source(codec, pin, i, num_adcs,
3142 spec->input_labels[i], -mixer);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003143 if (err < 0)
3144 return err;
Takashi Iwai294765582013-01-17 09:52:11 +01003145
Takashi Iwaif811c3c2013-03-07 18:32:59 +01003146 if (spec->add_jack_modes) {
Takashi Iwai294765582013-01-17 09:52:11 +01003147 err = create_in_jack_mode(codec, pin);
3148 if (err < 0)
3149 return err;
3150 }
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003151 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003152
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003153 if (mixer && spec->add_stereo_mix_input) {
Takashi Iwai9dba2052013-01-18 10:01:15 +01003154 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003155 "Stereo Mix", 0);
3156 if (err < 0)
3157 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003158 }
3159
3160 return 0;
3161}
3162
3163
3164/*
3165 * input source mux
3166 */
3167
Takashi Iwaic697b712013-01-07 17:09:26 +01003168/* get the input path specified by the given adc and imux indices */
3169static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003170{
3171 struct hda_gen_spec *spec = codec->spec;
David Henningssonb56fa1e2013-01-16 11:45:35 +01003172 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3173 snd_BUG();
3174 return NULL;
3175 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003176 if (spec->dyn_adc_switch)
3177 adc_idx = spec->dyn_adc_idx[imux_idx];
David Henningssond3d982f2013-01-18 15:43:01 +01003178 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
David Henningssonb56fa1e2013-01-16 11:45:35 +01003179 snd_BUG();
3180 return NULL;
3181 }
Takashi Iwaic697b712013-01-07 17:09:26 +01003182 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003183}
3184
3185static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3186 unsigned int idx);
3187
3188static int mux_enum_info(struct snd_kcontrol *kcontrol,
3189 struct snd_ctl_elem_info *uinfo)
3190{
3191 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3192 struct hda_gen_spec *spec = codec->spec;
3193 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3194}
3195
3196static int mux_enum_get(struct snd_kcontrol *kcontrol,
3197 struct snd_ctl_elem_value *ucontrol)
3198{
3199 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3200 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003201 /* the ctls are created at once with multiple counts */
3202 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003203
3204 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3205 return 0;
3206}
3207
3208static int mux_enum_put(struct snd_kcontrol *kcontrol,
3209 struct snd_ctl_elem_value *ucontrol)
3210{
3211 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003212 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003213 return mux_select(codec, adc_idx,
3214 ucontrol->value.enumerated.item[0]);
3215}
3216
Takashi Iwai352f7f92012-12-19 12:52:06 +01003217static const struct snd_kcontrol_new cap_src_temp = {
3218 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3219 .name = "Input Source",
3220 .info = mux_enum_info,
3221 .get = mux_enum_get,
3222 .put = mux_enum_put,
3223};
3224
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003225/*
3226 * capture volume and capture switch ctls
3227 */
3228
Takashi Iwai352f7f92012-12-19 12:52:06 +01003229typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3230 struct snd_ctl_elem_value *ucontrol);
3231
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003232/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003233static int cap_put_caller(struct snd_kcontrol *kcontrol,
3234 struct snd_ctl_elem_value *ucontrol,
3235 put_call_t func, int type)
3236{
3237 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3238 struct hda_gen_spec *spec = codec->spec;
3239 const struct hda_input_mux *imux;
3240 struct nid_path *path;
3241 int i, adc_idx, err = 0;
3242
3243 imux = &spec->input_mux;
David Henningssona053d1e2013-01-16 11:45:36 +01003244 adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003245 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003246 /* we use the cache-only update at first since multiple input paths
3247 * may shared the same amp; by updating only caches, the redundant
3248 * writes to hardware can be reduced.
3249 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003250 codec->cached_write = 1;
3251 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003252 path = get_input_path(codec, adc_idx, i);
3253 if (!path || !path->ctls[type])
Takashi Iwai352f7f92012-12-19 12:52:06 +01003254 continue;
3255 kcontrol->private_value = path->ctls[type];
3256 err = func(kcontrol, ucontrol);
3257 if (err < 0)
3258 goto error;
3259 }
3260 error:
3261 codec->cached_write = 0;
3262 mutex_unlock(&codec->control_mutex);
Takashi Iwaidc870f32013-01-22 15:24:30 +01003263 snd_hda_codec_flush_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003264 if (err >= 0 && spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01003265 spec->cap_sync_hook(codec, ucontrol);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003266 return err;
3267}
3268
3269/* capture volume ctl callbacks */
3270#define cap_vol_info snd_hda_mixer_amp_volume_info
3271#define cap_vol_get snd_hda_mixer_amp_volume_get
3272#define cap_vol_tlv snd_hda_mixer_amp_tlv
3273
3274static int cap_vol_put(struct snd_kcontrol *kcontrol,
3275 struct snd_ctl_elem_value *ucontrol)
3276{
3277 return cap_put_caller(kcontrol, ucontrol,
3278 snd_hda_mixer_amp_volume_put,
3279 NID_PATH_VOL_CTL);
3280}
3281
3282static const struct snd_kcontrol_new cap_vol_temp = {
3283 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3284 .name = "Capture Volume",
3285 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3286 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3287 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3288 .info = cap_vol_info,
3289 .get = cap_vol_get,
3290 .put = cap_vol_put,
3291 .tlv = { .c = cap_vol_tlv },
3292};
3293
3294/* capture switch ctl callbacks */
3295#define cap_sw_info snd_ctl_boolean_stereo_info
3296#define cap_sw_get snd_hda_mixer_amp_switch_get
3297
3298static int cap_sw_put(struct snd_kcontrol *kcontrol,
3299 struct snd_ctl_elem_value *ucontrol)
3300{
Takashi Iwaia90229e2013-01-18 14:10:00 +01003301 return cap_put_caller(kcontrol, ucontrol,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003302 snd_hda_mixer_amp_switch_put,
3303 NID_PATH_MUTE_CTL);
3304}
3305
3306static const struct snd_kcontrol_new cap_sw_temp = {
3307 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3308 .name = "Capture Switch",
3309 .info = cap_sw_info,
3310 .get = cap_sw_get,
3311 .put = cap_sw_put,
3312};
3313
3314static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3315{
3316 hda_nid_t nid;
3317 int i, depth;
3318
3319 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3320 for (depth = 0; depth < 3; depth++) {
3321 if (depth >= path->depth)
3322 return -EINVAL;
3323 i = path->depth - depth - 1;
3324 nid = path->path[i];
3325 if (!path->ctls[NID_PATH_VOL_CTL]) {
3326 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3327 path->ctls[NID_PATH_VOL_CTL] =
3328 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3329 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3330 int idx = path->idx[i];
3331 if (!depth && codec->single_adc_amp)
3332 idx = 0;
3333 path->ctls[NID_PATH_VOL_CTL] =
3334 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3335 }
3336 }
3337 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3338 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3339 path->ctls[NID_PATH_MUTE_CTL] =
3340 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3341 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3342 int idx = path->idx[i];
3343 if (!depth && codec->single_adc_amp)
3344 idx = 0;
3345 path->ctls[NID_PATH_MUTE_CTL] =
3346 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3347 }
3348 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01003349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 return 0;
3351}
3352
Takashi Iwai352f7f92012-12-19 12:52:06 +01003353static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003355 struct hda_gen_spec *spec = codec->spec;
3356 struct auto_pin_cfg *cfg = &spec->autocfg;
3357 unsigned int val;
3358 int i;
3359
3360 if (!spec->inv_dmic_split)
3361 return false;
3362 for (i = 0; i < cfg->num_inputs; i++) {
3363 if (cfg->inputs[i].pin != nid)
3364 continue;
3365 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3366 return false;
3367 val = snd_hda_codec_get_pincfg(codec, nid);
3368 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3369 }
3370 return false;
3371}
3372
Takashi Iwaia90229e2013-01-18 14:10:00 +01003373/* capture switch put callback for a single control with hook call */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003374static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3375 struct snd_ctl_elem_value *ucontrol)
3376{
3377 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3378 struct hda_gen_spec *spec = codec->spec;
3379 int ret;
3380
3381 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3382 if (ret < 0)
3383 return ret;
3384
Takashi Iwaia90229e2013-01-18 14:10:00 +01003385 if (spec->cap_sync_hook)
3386 spec->cap_sync_hook(codec, ucontrol);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003387
3388 return ret;
3389}
3390
Takashi Iwai352f7f92012-12-19 12:52:06 +01003391static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3392 int idx, bool is_switch, unsigned int ctl,
3393 bool inv_dmic)
3394{
3395 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai975cc022013-06-28 11:56:49 +02003396 char tmpname[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003397 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3398 const char *sfx = is_switch ? "Switch" : "Volume";
3399 unsigned int chs = inv_dmic ? 1 : 3;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003400 struct snd_kcontrol_new *knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003401
3402 if (!ctl)
3403 return 0;
3404
3405 if (label)
3406 snprintf(tmpname, sizeof(tmpname),
3407 "%s Capture %s", label, sfx);
3408 else
3409 snprintf(tmpname, sizeof(tmpname),
3410 "Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003411 knew = add_control(spec, type, tmpname, idx,
3412 amp_val_replace_channels(ctl, chs));
3413 if (!knew)
3414 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003415 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003416 knew->put = cap_single_sw_put;
3417 if (!inv_dmic)
3418 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003419
3420 /* Make independent right kcontrol */
3421 if (label)
3422 snprintf(tmpname, sizeof(tmpname),
3423 "Inverted %s Capture %s", label, sfx);
3424 else
3425 snprintf(tmpname, sizeof(tmpname),
3426 "Inverted Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003427 knew = add_control(spec, type, tmpname, idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003428 amp_val_replace_channels(ctl, 2));
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003429 if (!knew)
3430 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003431 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003432 knew->put = cap_single_sw_put;
3433 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003434}
3435
3436/* create single (and simple) capture volume and switch controls */
3437static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3438 unsigned int vol_ctl, unsigned int sw_ctl,
3439 bool inv_dmic)
3440{
3441 int err;
3442 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3443 if (err < 0)
3444 return err;
3445 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3446 if (err < 0)
3447 return err;
3448 return 0;
3449}
3450
3451/* create bound capture volume and switch controls */
3452static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3453 unsigned int vol_ctl, unsigned int sw_ctl)
3454{
3455 struct hda_gen_spec *spec = codec->spec;
3456 struct snd_kcontrol_new *knew;
3457
3458 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003459 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003460 if (!knew)
3461 return -ENOMEM;
3462 knew->index = idx;
3463 knew->private_value = vol_ctl;
3464 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3465 }
3466 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003467 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003468 if (!knew)
3469 return -ENOMEM;
3470 knew->index = idx;
3471 knew->private_value = sw_ctl;
3472 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3473 }
3474 return 0;
3475}
3476
3477/* return the vol ctl when used first in the imux list */
3478static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3479{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003480 struct nid_path *path;
3481 unsigned int ctl;
3482 int i;
3483
Takashi Iwaic697b712013-01-07 17:09:26 +01003484 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003485 if (!path)
3486 return 0;
3487 ctl = path->ctls[type];
3488 if (!ctl)
3489 return 0;
3490 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003491 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003492 if (path && path->ctls[type] == ctl)
3493 return 0;
3494 }
3495 return ctl;
3496}
3497
3498/* create individual capture volume and switch controls per input */
3499static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3500{
3501 struct hda_gen_spec *spec = codec->spec;
3502 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaic9700422013-01-18 10:17:30 +01003503 int i, err, type;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003504
3505 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003506 bool inv_dmic;
Takashi Iwaic9700422013-01-18 10:17:30 +01003507 int idx;
Takashi Iwai9dba2052013-01-18 10:01:15 +01003508
Takashi Iwaic9700422013-01-18 10:17:30 +01003509 idx = imux->items[i].index;
3510 if (idx >= spec->autocfg.num_inputs)
Takashi Iwai9dba2052013-01-18 10:01:15 +01003511 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003512 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3513
3514 for (type = 0; type < 2; type++) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003515 err = add_single_cap_ctl(codec,
3516 spec->input_labels[idx],
3517 spec->input_label_idxs[idx],
3518 type,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003519 get_first_cap_ctl(codec, i, type),
3520 inv_dmic);
3521 if (err < 0)
3522 return err;
3523 }
3524 }
3525 return 0;
3526}
3527
3528static int create_capture_mixers(struct hda_codec *codec)
3529{
3530 struct hda_gen_spec *spec = codec->spec;
3531 struct hda_input_mux *imux = &spec->input_mux;
3532 int i, n, nums, err;
3533
3534 if (spec->dyn_adc_switch)
3535 nums = 1;
3536 else
3537 nums = spec->num_adc_nids;
3538
3539 if (!spec->auto_mic && imux->num_items > 1) {
3540 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01003541 const char *name;
3542 name = nums > 1 ? "Input Source" : "Capture Source";
3543 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003544 if (!knew)
3545 return -ENOMEM;
3546 knew->count = nums;
3547 }
3548
3549 for (n = 0; n < nums; n++) {
3550 bool multi = false;
David Henningsson99a55922013-01-16 15:58:44 +01003551 bool multi_cap_vol = spec->multi_cap_vol;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003552 bool inv_dmic = false;
3553 int vol, sw;
3554
3555 vol = sw = 0;
3556 for (i = 0; i < imux->num_items; i++) {
3557 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01003558 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003559 if (!path)
3560 continue;
3561 parse_capvol_in_path(codec, path);
3562 if (!vol)
3563 vol = path->ctls[NID_PATH_VOL_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003564 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003565 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003566 if (!same_amp_caps(codec, vol,
3567 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3568 multi_cap_vol = true;
3569 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003570 if (!sw)
3571 sw = path->ctls[NID_PATH_MUTE_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003572 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003573 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003574 if (!same_amp_caps(codec, sw,
3575 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3576 multi_cap_vol = true;
3577 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003578 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3579 inv_dmic = true;
3580 }
3581
3582 if (!multi)
3583 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3584 inv_dmic);
David Henningssonccb04152013-10-14 10:16:22 +02003585 else if (!multi_cap_vol && !inv_dmic)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003586 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3587 else
3588 err = create_multi_cap_vol_ctl(codec);
3589 if (err < 0)
3590 return err;
3591 }
3592
3593 return 0;
3594}
3595
3596/*
3597 * add mic boosts if needed
3598 */
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003599
3600/* check whether the given amp is feasible as a boost volume */
3601static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3602 int dir, int idx)
3603{
3604 unsigned int step;
3605
3606 if (!nid_has_volume(codec, nid, dir) ||
3607 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3608 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3609 return false;
3610
3611 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3612 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3613 if (step < 0x20)
3614 return false;
3615 return true;
3616}
3617
3618/* look for a boost amp in a widget close to the pin */
3619static unsigned int look_for_boost_amp(struct hda_codec *codec,
3620 struct nid_path *path)
3621{
3622 unsigned int val = 0;
3623 hda_nid_t nid;
3624 int depth;
3625
3626 for (depth = 0; depth < 3; depth++) {
3627 if (depth >= path->depth - 1)
3628 break;
3629 nid = path->path[depth];
3630 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3631 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3632 break;
3633 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3634 path->idx[depth])) {
3635 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3636 HDA_INPUT);
3637 break;
3638 }
3639 }
3640
3641 return val;
3642}
3643
Takashi Iwai352f7f92012-12-19 12:52:06 +01003644static int parse_mic_boost(struct hda_codec *codec)
3645{
3646 struct hda_gen_spec *spec = codec->spec;
3647 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003648 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003649 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003650
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003651 if (!spec->num_adc_nids)
3652 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003653
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003654 for (i = 0; i < imux->num_items; i++) {
3655 struct nid_path *path;
3656 unsigned int val;
3657 int idx;
Takashi Iwai975cc022013-06-28 11:56:49 +02003658 char boost_label[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
David Henningsson02aba552013-01-16 15:58:43 +01003659
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003660 idx = imux->items[i].index;
3661 if (idx >= imux->num_items)
3662 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003663
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003664 /* check only line-in and mic pins */
Takashi Iwai1799cdd52013-01-18 14:37:16 +01003665 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003666 continue;
3667
3668 path = get_input_path(codec, 0, i);
3669 if (!path)
3670 continue;
3671
3672 val = look_for_boost_amp(codec, path);
3673 if (!val)
3674 continue;
3675
3676 /* create a boost control */
3677 snprintf(boost_label, sizeof(boost_label),
3678 "%s Boost Volume", spec->input_labels[idx]);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003679 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3680 spec->input_label_idxs[idx], val))
3681 return -ENOMEM;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003682
3683 path->ctls[NID_PATH_BOOST_CTL] = val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003684 }
3685 return 0;
3686}
3687
3688/*
3689 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3690 */
3691static void parse_digital(struct hda_codec *codec)
3692{
3693 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003694 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003695 int i, nums;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003696 hda_nid_t dig_nid, pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003697
3698 /* support multiple SPDIFs; the secondary is set up as a slave */
3699 nums = 0;
3700 for (i = 0; i < spec->autocfg.dig_outs; i++) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003701 pin = spec->autocfg.dig_out_pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003702 dig_nid = look_for_dac(codec, pin, true);
3703 if (!dig_nid)
3704 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003705 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003706 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003707 continue;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003708 print_nid_path("digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01003709 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01003710 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003711 set_pin_target(codec, pin, PIN_OUT, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003712 if (!nums) {
3713 spec->multiout.dig_out_nid = dig_nid;
3714 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3715 } else {
3716 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3717 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
3718 break;
3719 spec->slave_dig_outs[nums - 1] = dig_nid;
3720 }
3721 nums++;
3722 }
3723
3724 if (spec->autocfg.dig_in_pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003725 pin = spec->autocfg.dig_in_pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003726 dig_nid = codec->start_nid;
3727 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003728 unsigned int wcaps = get_wcaps(codec, dig_nid);
3729 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3730 continue;
3731 if (!(wcaps & AC_WCAP_DIGITAL))
3732 continue;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003733 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003734 if (path) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003735 print_nid_path("digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003736 path->active = true;
3737 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003738 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003739 set_pin_target(codec, pin, PIN_IN, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003740 break;
3741 }
3742 }
3743 }
3744}
3745
3746
3747/*
3748 * input MUX handling
3749 */
3750
3751static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3752
3753/* select the given imux item; either unmute exclusively or select the route */
3754static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3755 unsigned int idx)
3756{
3757 struct hda_gen_spec *spec = codec->spec;
3758 const struct hda_input_mux *imux;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003759 struct nid_path *old_path, *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003760
3761 imux = &spec->input_mux;
3762 if (!imux->num_items)
3763 return 0;
3764
3765 if (idx >= imux->num_items)
3766 idx = imux->num_items - 1;
3767 if (spec->cur_mux[adc_idx] == idx)
3768 return 0;
3769
Takashi Iwai55196ff2013-01-24 17:32:56 +01003770 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3771 if (!old_path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003772 return 0;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003773 if (old_path->active)
3774 snd_hda_activate_path(codec, old_path, false, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003775
3776 spec->cur_mux[adc_idx] = idx;
3777
Takashi Iwai967303d2013-02-19 17:12:42 +01003778 if (spec->hp_mic)
3779 update_hp_mic(codec, adc_idx, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003780
3781 if (spec->dyn_adc_switch)
3782 dyn_adc_pcm_resetup(codec, idx);
3783
Takashi Iwaic697b712013-01-07 17:09:26 +01003784 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003785 if (!path)
3786 return 0;
3787 if (path->active)
3788 return 0;
3789 snd_hda_activate_path(codec, path, true, false);
3790 if (spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01003791 spec->cap_sync_hook(codec, NULL);
Takashi Iwai55196ff2013-01-24 17:32:56 +01003792 path_power_down_sync(codec, old_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003793 return 1;
3794}
3795
3796
3797/*
3798 * Jack detections for HP auto-mute and mic-switch
3799 */
3800
3801/* check each pin in the given array; returns true if any of them is plugged */
3802static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
3803{
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02003804 int i;
3805 bool present = false;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003806
3807 for (i = 0; i < num_pins; i++) {
3808 hda_nid_t nid = pins[i];
3809 if (!nid)
3810 break;
Takashi Iwai0b4df932013-01-10 09:45:13 +01003811 /* don't detect pins retasked as inputs */
3812 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
3813 continue;
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02003814 if (snd_hda_jack_detect_state(codec, nid) == HDA_JACK_PRESENT)
3815 present = true;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003816 }
3817 return present;
3818}
3819
3820/* standard HP/line-out auto-mute helper */
3821static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003822 int *paths, bool mute)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003823{
3824 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003825 int i;
3826
3827 for (i = 0; i < num_pins; i++) {
3828 hda_nid_t nid = pins[i];
Takashi Iwai967303d2013-02-19 17:12:42 +01003829 unsigned int val, oldval;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003830 if (!nid)
3831 break;
Takashi Iwai7eebffd2013-06-24 16:00:21 +02003832
3833 if (spec->auto_mute_via_amp) {
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003834 struct nid_path *path;
3835 hda_nid_t mute_nid;
3836
3837 path = snd_hda_get_path_from_idx(codec, paths[i]);
3838 if (!path)
3839 continue;
3840 mute_nid = get_amp_nid_(path->ctls[NID_PATH_MUTE_CTL]);
3841 if (!mute_nid)
3842 continue;
Takashi Iwai7eebffd2013-06-24 16:00:21 +02003843 if (mute)
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003844 spec->mute_bits |= (1ULL << mute_nid);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02003845 else
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003846 spec->mute_bits &= ~(1ULL << mute_nid);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02003847 set_pin_eapd(codec, nid, !mute);
3848 continue;
3849 }
3850
Takashi Iwai967303d2013-02-19 17:12:42 +01003851 oldval = snd_hda_codec_get_pin_target(codec, nid);
3852 if (oldval & PIN_IN)
3853 continue; /* no mute for inputs */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003854 /* don't reset VREF value in case it's controlling
3855 * the amp (see alc861_fixup_asus_amp_vref_0f())
3856 */
Takashi Iwai2c12c302013-01-10 09:33:29 +01003857 if (spec->keep_vref_in_automute)
Takashi Iwai967303d2013-02-19 17:12:42 +01003858 val = oldval & ~PIN_HP;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003859 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01003860 val = 0;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003861 if (!mute)
Takashi Iwai967303d2013-02-19 17:12:42 +01003862 val |= oldval;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003863 /* here we call update_pin_ctl() so that the pinctl is changed
3864 * without changing the pinctl target value;
3865 * the original target value will be still referred at the
3866 * init / resume again
3867 */
3868 update_pin_ctl(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01003869 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003870 }
3871}
3872
3873/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003874void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003875{
3876 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003877 int *paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003878 int on;
3879
3880 /* Control HP pins/amps depending on master_mute state;
3881 * in general, HP pins/amps control should be enabled in all cases,
3882 * but currently set only for master_mute, just to be safe
3883 */
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003884 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
3885 paths = spec->out_paths;
3886 else
3887 paths = spec->hp_paths;
Takashi Iwai967303d2013-02-19 17:12:42 +01003888 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003889 spec->autocfg.hp_pins, paths, spec->master_mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003890
3891 if (!spec->automute_speaker)
3892 on = 0;
3893 else
3894 on = spec->hp_jack_present | spec->line_jack_present;
3895 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003896 spec->speaker_muted = on;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003897 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3898 paths = spec->out_paths;
3899 else
3900 paths = spec->speaker_paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003901 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003902 spec->autocfg.speaker_pins, paths, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003903
3904 /* toggle line-out mutes if needed, too */
3905 /* if LO is a copy of either HP or Speaker, don't need to handle it */
3906 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
3907 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
3908 return;
3909 if (!spec->automute_lo)
3910 on = 0;
3911 else
3912 on = spec->hp_jack_present;
3913 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003914 spec->line_out_muted = on;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003915 paths = spec->out_paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003916 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003917 spec->autocfg.line_out_pins, paths, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003918}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003919EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003920
3921static void call_update_outputs(struct hda_codec *codec)
3922{
3923 struct hda_gen_spec *spec = codec->spec;
3924 if (spec->automute_hook)
3925 spec->automute_hook(codec);
3926 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01003927 snd_hda_gen_update_outputs(codec);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02003928
3929 /* sync the whole vmaster slaves to reflect the new auto-mute status */
3930 if (spec->auto_mute_via_amp && !codec->bus->shutdown)
3931 snd_ctl_sync_vmaster(spec->vmaster_mute.sw_kctl, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003932}
3933
3934/* standard HP-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003935void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003936{
3937 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai92603c52013-01-22 07:46:31 +01003938 hda_nid_t *pins = spec->autocfg.hp_pins;
3939 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003940
Takashi Iwai92603c52013-01-22 07:46:31 +01003941 /* No detection for the first HP jack during indep-HP mode */
3942 if (spec->indep_hp_enabled) {
3943 pins++;
3944 num_pins--;
3945 }
3946
3947 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003948 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
3949 return;
3950 call_update_outputs(codec);
3951}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003952EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003953
3954/* standard line-out-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003955void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003956{
3957 struct hda_gen_spec *spec = codec->spec;
3958
3959 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3960 return;
3961 /* check LO jack only when it's different from HP */
3962 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
3963 return;
3964
3965 spec->line_jack_present =
3966 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3967 spec->autocfg.line_out_pins);
3968 if (!spec->automute_speaker || !spec->detect_lo)
3969 return;
3970 call_update_outputs(codec);
3971}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003972EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003973
3974/* standard mic auto-switch helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003975void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003976{
3977 struct hda_gen_spec *spec = codec->spec;
3978 int i;
3979
3980 if (!spec->auto_mic)
3981 return;
3982
3983 for (i = spec->am_num_entries - 1; i > 0; i--) {
Takashi Iwai0b4df932013-01-10 09:45:13 +01003984 hda_nid_t pin = spec->am_entry[i].pin;
3985 /* don't detect pins retasked as outputs */
3986 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
3987 continue;
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02003988 if (snd_hda_jack_detect_state(codec, pin) == HDA_JACK_PRESENT) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003989 mux_select(codec, 0, spec->am_entry[i].idx);
3990 return;
3991 }
3992 }
3993 mux_select(codec, 0, spec->am_entry[0].idx);
3994}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003995EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003996
Takashi Iwai77afe0e2013-05-31 14:10:03 +02003997/* call appropriate hooks */
3998static void call_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
3999{
4000 struct hda_gen_spec *spec = codec->spec;
4001 if (spec->hp_automute_hook)
4002 spec->hp_automute_hook(codec, jack);
4003 else
4004 snd_hda_gen_hp_automute(codec, jack);
4005}
4006
4007static void call_line_automute(struct hda_codec *codec,
4008 struct hda_jack_tbl *jack)
4009{
4010 struct hda_gen_spec *spec = codec->spec;
4011 if (spec->line_automute_hook)
4012 spec->line_automute_hook(codec, jack);
4013 else
4014 snd_hda_gen_line_automute(codec, jack);
4015}
4016
4017static void call_mic_autoswitch(struct hda_codec *codec,
4018 struct hda_jack_tbl *jack)
4019{
4020 struct hda_gen_spec *spec = codec->spec;
4021 if (spec->mic_autoswitch_hook)
4022 spec->mic_autoswitch_hook(codec, jack);
4023 else
4024 snd_hda_gen_mic_autoswitch(codec, jack);
4025}
4026
Takashi Iwai963afde2013-05-31 15:20:31 +02004027/* update jack retasking */
4028static void update_automute_all(struct hda_codec *codec)
4029{
4030 call_hp_automute(codec, NULL);
4031 call_line_automute(codec, NULL);
4032 call_mic_autoswitch(codec, NULL);
4033}
4034
Takashi Iwai352f7f92012-12-19 12:52:06 +01004035/*
4036 * Auto-Mute mode mixer enum support
4037 */
4038static int automute_mode_info(struct snd_kcontrol *kcontrol,
4039 struct snd_ctl_elem_info *uinfo)
4040{
4041 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4042 struct hda_gen_spec *spec = codec->spec;
4043 static const char * const texts3[] = {
4044 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02004045 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046
Takashi Iwai352f7f92012-12-19 12:52:06 +01004047 if (spec->automute_speaker_possible && spec->automute_lo_possible)
4048 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
4049 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
4050}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051
Takashi Iwai352f7f92012-12-19 12:52:06 +01004052static int automute_mode_get(struct snd_kcontrol *kcontrol,
4053 struct snd_ctl_elem_value *ucontrol)
4054{
4055 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4056 struct hda_gen_spec *spec = codec->spec;
4057 unsigned int val = 0;
4058 if (spec->automute_speaker)
4059 val++;
4060 if (spec->automute_lo)
4061 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004062
Takashi Iwai352f7f92012-12-19 12:52:06 +01004063 ucontrol->value.enumerated.item[0] = val;
4064 return 0;
4065}
4066
4067static int automute_mode_put(struct snd_kcontrol *kcontrol,
4068 struct snd_ctl_elem_value *ucontrol)
4069{
4070 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4071 struct hda_gen_spec *spec = codec->spec;
4072
4073 switch (ucontrol->value.enumerated.item[0]) {
4074 case 0:
4075 if (!spec->automute_speaker && !spec->automute_lo)
4076 return 0;
4077 spec->automute_speaker = 0;
4078 spec->automute_lo = 0;
4079 break;
4080 case 1:
4081 if (spec->automute_speaker_possible) {
4082 if (!spec->automute_lo && spec->automute_speaker)
4083 return 0;
4084 spec->automute_speaker = 1;
4085 spec->automute_lo = 0;
4086 } else if (spec->automute_lo_possible) {
4087 if (spec->automute_lo)
4088 return 0;
4089 spec->automute_lo = 1;
4090 } else
4091 return -EINVAL;
4092 break;
4093 case 2:
4094 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
4095 return -EINVAL;
4096 if (spec->automute_speaker && spec->automute_lo)
4097 return 0;
4098 spec->automute_speaker = 1;
4099 spec->automute_lo = 1;
4100 break;
4101 default:
4102 return -EINVAL;
4103 }
4104 call_update_outputs(codec);
4105 return 1;
4106}
4107
4108static const struct snd_kcontrol_new automute_mode_enum = {
4109 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4110 .name = "Auto-Mute Mode",
4111 .info = automute_mode_info,
4112 .get = automute_mode_get,
4113 .put = automute_mode_put,
4114};
4115
4116static int add_automute_mode_enum(struct hda_codec *codec)
4117{
4118 struct hda_gen_spec *spec = codec->spec;
4119
Takashi Iwai12c93df2012-12-19 14:38:33 +01004120 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01004121 return -ENOMEM;
4122 return 0;
4123}
4124
4125/*
4126 * Check the availability of HP/line-out auto-mute;
4127 * Set up appropriately if really supported
4128 */
4129static int check_auto_mute_availability(struct hda_codec *codec)
4130{
4131 struct hda_gen_spec *spec = codec->spec;
4132 struct auto_pin_cfg *cfg = &spec->autocfg;
4133 int present = 0;
4134 int i, err;
4135
Takashi Iwaif72706b2013-01-16 18:20:07 +01004136 if (spec->suppress_auto_mute)
4137 return 0;
4138
Takashi Iwai352f7f92012-12-19 12:52:06 +01004139 if (cfg->hp_pins[0])
4140 present++;
4141 if (cfg->line_out_pins[0])
4142 present++;
4143 if (cfg->speaker_pins[0])
4144 present++;
4145 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02004146 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004147
4148 if (!cfg->speaker_pins[0] &&
4149 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
4150 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4151 sizeof(cfg->speaker_pins));
4152 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154
Takashi Iwai352f7f92012-12-19 12:52:06 +01004155 if (!cfg->hp_pins[0] &&
4156 cfg->line_out_type == AUTO_PIN_HP_OUT) {
4157 memcpy(cfg->hp_pins, cfg->line_out_pins,
4158 sizeof(cfg->hp_pins));
4159 cfg->hp_outs = cfg->line_outs;
4160 }
4161
4162 for (i = 0; i < cfg->hp_outs; i++) {
4163 hda_nid_t nid = cfg->hp_pins[i];
4164 if (!is_jack_detectable(codec, nid))
4165 continue;
4166 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
4167 nid);
4168 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004169 call_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004170 spec->detect_hp = 1;
4171 }
4172
4173 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
4174 if (cfg->speaker_outs)
4175 for (i = 0; i < cfg->line_outs; i++) {
4176 hda_nid_t nid = cfg->line_out_pins[i];
4177 if (!is_jack_detectable(codec, nid))
4178 continue;
4179 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
4180 snd_hda_jack_detect_enable_callback(codec, nid,
4181 HDA_GEN_FRONT_EVENT,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004182 call_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004183 spec->detect_lo = 1;
4184 }
4185 spec->automute_lo_possible = spec->detect_hp;
4186 }
4187
4188 spec->automute_speaker_possible = cfg->speaker_outs &&
4189 (spec->detect_hp || spec->detect_lo);
4190
4191 spec->automute_lo = spec->automute_lo_possible;
4192 spec->automute_speaker = spec->automute_speaker_possible;
4193
4194 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
4195 /* create a control for automute mode */
4196 err = add_automute_mode_enum(codec);
4197 if (err < 0)
4198 return err;
4199 }
4200 return 0;
4201}
4202
Takashi Iwai352f7f92012-12-19 12:52:06 +01004203/* check whether all auto-mic pins are valid; setup indices if OK */
4204static bool auto_mic_check_imux(struct hda_codec *codec)
4205{
4206 struct hda_gen_spec *spec = codec->spec;
4207 const struct hda_input_mux *imux;
4208 int i;
4209
4210 imux = &spec->input_mux;
4211 for (i = 0; i < spec->am_num_entries; i++) {
4212 spec->am_entry[i].idx =
4213 find_idx_in_nid_list(spec->am_entry[i].pin,
4214 spec->imux_pins, imux->num_items);
4215 if (spec->am_entry[i].idx < 0)
4216 return false; /* no corresponding imux */
4217 }
4218
4219 /* we don't need the jack detection for the first pin */
4220 for (i = 1; i < spec->am_num_entries; i++)
4221 snd_hda_jack_detect_enable_callback(codec,
4222 spec->am_entry[i].pin,
4223 HDA_GEN_MIC_EVENT,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004224 call_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004225 return true;
4226}
4227
4228static int compare_attr(const void *ap, const void *bp)
4229{
4230 const struct automic_entry *a = ap;
4231 const struct automic_entry *b = bp;
4232 return (int)(a->attr - b->attr);
4233}
4234
4235/*
4236 * Check the availability of auto-mic switch;
4237 * Set up if really supported
4238 */
4239static int check_auto_mic_availability(struct hda_codec *codec)
4240{
4241 struct hda_gen_spec *spec = codec->spec;
4242 struct auto_pin_cfg *cfg = &spec->autocfg;
4243 unsigned int types;
4244 int i, num_pins;
4245
Takashi Iwaid12daf62013-01-07 16:32:11 +01004246 if (spec->suppress_auto_mic)
4247 return 0;
4248
Takashi Iwai352f7f92012-12-19 12:52:06 +01004249 types = 0;
4250 num_pins = 0;
4251 for (i = 0; i < cfg->num_inputs; i++) {
4252 hda_nid_t nid = cfg->inputs[i].pin;
4253 unsigned int attr;
4254 attr = snd_hda_codec_get_pincfg(codec, nid);
4255 attr = snd_hda_get_input_pin_attr(attr);
4256 if (types & (1 << attr))
4257 return 0; /* already occupied */
4258 switch (attr) {
4259 case INPUT_PIN_ATTR_INT:
4260 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4261 return 0; /* invalid type */
4262 break;
4263 case INPUT_PIN_ATTR_UNUSED:
4264 return 0; /* invalid entry */
4265 default:
4266 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4267 return 0; /* invalid type */
4268 if (!spec->line_in_auto_switch &&
4269 cfg->inputs[i].type != AUTO_PIN_MIC)
4270 return 0; /* only mic is allowed */
4271 if (!is_jack_detectable(codec, nid))
4272 return 0; /* no unsol support */
4273 break;
4274 }
4275 if (num_pins >= MAX_AUTO_MIC_PINS)
4276 return 0;
4277 types |= (1 << attr);
4278 spec->am_entry[num_pins].pin = nid;
4279 spec->am_entry[num_pins].attr = attr;
4280 num_pins++;
4281 }
4282
4283 if (num_pins < 2)
4284 return 0;
4285
4286 spec->am_num_entries = num_pins;
4287 /* sort the am_entry in the order of attr so that the pin with a
4288 * higher attr will be selected when the jack is plugged.
4289 */
4290 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4291 compare_attr, NULL);
4292
4293 if (!auto_mic_check_imux(codec))
4294 return 0;
4295
4296 spec->auto_mic = 1;
4297 spec->num_adc_nids = 1;
4298 spec->cur_mux[0] = spec->am_entry[0].idx;
4299 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
4300 spec->am_entry[0].pin,
4301 spec->am_entry[1].pin,
4302 spec->am_entry[2].pin);
4303
4304 return 0;
4305}
4306
Takashi Iwai55196ff2013-01-24 17:32:56 +01004307/* power_filter hook; make inactive widgets into power down */
4308static unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
4309 hda_nid_t nid,
4310 unsigned int power_state)
4311{
4312 if (power_state != AC_PWRST_D0)
4313 return power_state;
4314 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4315 return power_state;
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +02004316 if (is_active_nid_for_any(codec, nid))
Takashi Iwai55196ff2013-01-24 17:32:56 +01004317 return power_state;
4318 return AC_PWRST_D3;
4319}
4320
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004321/* mute all aamix inputs initially; parse up to the first leaves */
4322static void mute_all_mixer_nid(struct hda_codec *codec, hda_nid_t mix)
4323{
4324 int i, nums;
4325 const hda_nid_t *conn;
4326 bool has_amp;
4327
4328 nums = snd_hda_get_conn_list(codec, mix, &conn);
4329 has_amp = nid_has_mute(codec, mix, HDA_INPUT);
4330 for (i = 0; i < nums; i++) {
4331 if (has_amp)
4332 snd_hda_codec_amp_stereo(codec, mix,
4333 HDA_INPUT, i,
4334 0xff, HDA_AMP_MUTE);
4335 else if (nid_has_volume(codec, conn[i], HDA_OUTPUT))
4336 snd_hda_codec_amp_stereo(codec, conn[i],
4337 HDA_OUTPUT, 0,
4338 0xff, HDA_AMP_MUTE);
4339 }
4340}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004341
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004342/*
4343 * Parse the given BIOS configuration and set up the hda_gen_spec
4344 *
4345 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004346 * or a negative error code
4347 */
4348int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004349 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004350{
4351 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004352 int err;
4353
Takashi Iwai1c70a582013-01-11 17:48:22 +01004354 parse_user_hints(codec);
4355
Takashi Iwaie4a395e2013-01-23 17:00:31 +01004356 if (spec->mixer_nid && !spec->mixer_merge_nid)
4357 spec->mixer_merge_nid = spec->mixer_nid;
4358
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004359 if (cfg != &spec->autocfg) {
4360 spec->autocfg = *cfg;
4361 cfg = &spec->autocfg;
4362 }
4363
Takashi Iwai98bd1112013-03-22 14:53:50 +01004364 if (!spec->main_out_badness)
4365 spec->main_out_badness = &hda_main_out_badness;
4366 if (!spec->extra_out_badness)
4367 spec->extra_out_badness = &hda_extra_out_badness;
4368
David Henningsson6fc4cb92013-01-16 15:58:45 +01004369 fill_all_dac_nids(codec);
4370
Takashi Iwai352f7f92012-12-19 12:52:06 +01004371 if (!cfg->line_outs) {
4372 if (cfg->dig_outs || cfg->dig_in_pin) {
4373 spec->multiout.max_channels = 2;
4374 spec->no_analog = 1;
4375 goto dig_only;
4376 }
4377 return 0; /* can't find valid BIOS pin config */
4378 }
4379
4380 if (!spec->no_primary_hp &&
4381 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
4382 cfg->line_outs <= cfg->hp_outs) {
4383 /* use HP as primary out */
4384 cfg->speaker_outs = cfg->line_outs;
4385 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4386 sizeof(cfg->speaker_pins));
4387 cfg->line_outs = cfg->hp_outs;
4388 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4389 cfg->hp_outs = 0;
4390 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4391 cfg->line_out_type = AUTO_PIN_HP_OUT;
4392 }
4393
4394 err = parse_output_paths(codec);
4395 if (err < 0)
4396 return err;
4397 err = create_multi_channel_mode(codec);
4398 if (err < 0)
4399 return err;
4400 err = create_multi_out_ctls(codec, cfg);
4401 if (err < 0)
4402 return err;
4403 err = create_hp_out_ctls(codec);
4404 if (err < 0)
4405 return err;
4406 err = create_speaker_out_ctls(codec);
4407 if (err < 0)
4408 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004409 err = create_indep_hp_ctls(codec);
4410 if (err < 0)
4411 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01004412 err = create_loopback_mixing_ctl(codec);
4413 if (err < 0)
4414 return err;
Takashi Iwai967303d2013-02-19 17:12:42 +01004415 err = create_hp_mic(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004416 if (err < 0)
4417 return err;
4418 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02004419 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02004420 return err;
4421
Takashi Iwaia07a9492013-01-07 16:44:06 +01004422 spec->const_channel_count = spec->ext_channel_count;
4423 /* check the multiple speaker and headphone pins */
4424 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4425 spec->const_channel_count = max(spec->const_channel_count,
4426 cfg->speaker_outs * 2);
4427 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4428 spec->const_channel_count = max(spec->const_channel_count,
4429 cfg->hp_outs * 2);
4430 spec->multiout.max_channels = max(spec->ext_channel_count,
4431 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004432
4433 err = check_auto_mute_availability(codec);
4434 if (err < 0)
4435 return err;
4436
4437 err = check_dyn_adc_switch(codec);
4438 if (err < 0)
4439 return err;
4440
Takashi Iwai967303d2013-02-19 17:12:42 +01004441 err = check_auto_mic_availability(codec);
4442 if (err < 0)
4443 return err;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004444
Takashi Iwai352f7f92012-12-19 12:52:06 +01004445 err = create_capture_mixers(codec);
4446 if (err < 0)
4447 return err;
4448
4449 err = parse_mic_boost(codec);
4450 if (err < 0)
4451 return err;
4452
Takashi Iwaiced4cef2013-11-26 08:33:45 +01004453 /* create "Headphone Mic Jack Mode" if no input selection is
4454 * available (or user specifies add_jack_modes hint)
4455 */
4456 if (spec->hp_mic_pin &&
4457 (spec->auto_mic || spec->input_mux.num_items == 1 ||
4458 spec->add_jack_modes)) {
4459 err = create_hp_mic_jack_mode(codec, spec->hp_mic_pin);
4460 if (err < 0)
4461 return err;
4462 }
4463
Takashi Iwaif811c3c2013-03-07 18:32:59 +01004464 if (spec->add_jack_modes) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01004465 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4466 err = create_out_jack_modes(codec, cfg->line_outs,
4467 cfg->line_out_pins);
4468 if (err < 0)
4469 return err;
4470 }
4471 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4472 err = create_out_jack_modes(codec, cfg->hp_outs,
4473 cfg->hp_pins);
4474 if (err < 0)
4475 return err;
4476 }
4477 }
4478
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004479 /* mute all aamix input initially */
4480 if (spec->mixer_nid)
4481 mute_all_mixer_nid(codec, spec->mixer_nid);
4482
Takashi Iwai352f7f92012-12-19 12:52:06 +01004483 dig_only:
4484 parse_digital(codec);
4485
Takashi Iwai55196ff2013-01-24 17:32:56 +01004486 if (spec->power_down_unused)
4487 codec->power_filter = snd_hda_gen_path_power_filter;
4488
Takashi Iwai7504b6c2013-03-18 11:25:51 +01004489 if (!spec->no_analog && spec->beep_nid) {
4490 err = snd_hda_attach_beep_device(codec, spec->beep_nid);
4491 if (err < 0)
4492 return err;
4493 }
4494
Takashi Iwai352f7f92012-12-19 12:52:06 +01004495 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004497EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498
4499
4500/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004501 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004503
4504/* slave controls for virtual master */
4505static const char * const slave_pfxs[] = {
4506 "Front", "Surround", "Center", "LFE", "Side",
4507 "Headphone", "Speaker", "Mono", "Line Out",
4508 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01004509 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
4510 "Headphone Front", "Headphone Surround", "Headphone CLFE",
4511 "Headphone Side",
Takashi Iwai352f7f92012-12-19 12:52:06 +01004512 NULL,
4513};
4514
4515int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004517 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519
Takashi Iwai36502d02012-12-19 15:15:10 +01004520 if (spec->kctls.used) {
4521 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
4522 if (err < 0)
4523 return err;
4524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525
Takashi Iwai352f7f92012-12-19 12:52:06 +01004526 if (spec->multiout.dig_out_nid) {
4527 err = snd_hda_create_dig_out_ctls(codec,
4528 spec->multiout.dig_out_nid,
4529 spec->multiout.dig_out_nid,
4530 spec->pcm_rec[1].pcm_type);
4531 if (err < 0)
4532 return err;
4533 if (!spec->no_analog) {
4534 err = snd_hda_create_spdif_share_sw(codec,
4535 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536 if (err < 0)
4537 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004538 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539 }
4540 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004541 if (spec->dig_in_nid) {
4542 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
4543 if (err < 0)
4544 return err;
4545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546
Takashi Iwai352f7f92012-12-19 12:52:06 +01004547 /* if we have no master control, let's create it */
4548 if (!spec->no_analog &&
4549 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004550 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01004551 spec->vmaster_tlv, slave_pfxs,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004552 "Playback Volume");
4553 if (err < 0)
4554 return err;
4555 }
4556 if (!spec->no_analog &&
4557 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
4558 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
4559 NULL, slave_pfxs,
4560 "Playback Switch",
4561 true, &spec->vmaster_mute.sw_kctl);
4562 if (err < 0)
4563 return err;
Takashi Iwaib63eae02013-10-25 23:43:10 +02004564 if (spec->vmaster_mute.hook) {
Takashi Iwaifd25a972012-12-20 14:57:18 +01004565 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
4566 spec->vmaster_mute_enum);
Takashi Iwaib63eae02013-10-25 23:43:10 +02004567 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
4568 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570
Takashi Iwai352f7f92012-12-19 12:52:06 +01004571 free_kctls(spec); /* no longer needed */
4572
Takashi Iwai352f7f92012-12-19 12:52:06 +01004573 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
4574 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575 return err;
4576
4577 return 0;
4578}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004579EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
4580
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581
4582/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004583 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004586static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
4587 struct hda_codec *codec,
4588 struct snd_pcm_substream *substream,
4589 int action)
4590{
4591 struct hda_gen_spec *spec = codec->spec;
4592 if (spec->pcm_playback_hook)
4593 spec->pcm_playback_hook(hinfo, codec, substream, action);
4594}
4595
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004596static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
4597 struct hda_codec *codec,
4598 struct snd_pcm_substream *substream,
4599 int action)
4600{
4601 struct hda_gen_spec *spec = codec->spec;
4602 if (spec->pcm_capture_hook)
4603 spec->pcm_capture_hook(hinfo, codec, substream, action);
4604}
4605
Takashi Iwai352f7f92012-12-19 12:52:06 +01004606/*
4607 * Analog playback callbacks
4608 */
4609static int playback_pcm_open(struct hda_pcm_stream *hinfo,
4610 struct hda_codec *codec,
4611 struct snd_pcm_substream *substream)
4612{
4613 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004614 int err;
4615
4616 mutex_lock(&spec->pcm_mutex);
4617 err = snd_hda_multi_out_analog_open(codec,
4618 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004619 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004620 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004621 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004622 call_pcm_playback_hook(hinfo, codec, substream,
4623 HDA_GEN_PCM_ACT_OPEN);
4624 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004625 mutex_unlock(&spec->pcm_mutex);
4626 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004627}
4628
4629static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01004630 struct hda_codec *codec,
4631 unsigned int stream_tag,
4632 unsigned int format,
4633 struct snd_pcm_substream *substream)
4634{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004635 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004636 int err;
4637
4638 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
4639 stream_tag, format, substream);
4640 if (!err)
4641 call_pcm_playback_hook(hinfo, codec, substream,
4642 HDA_GEN_PCM_ACT_PREPARE);
4643 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004644}
Takashi Iwai97ec5582006-03-21 11:29:07 +01004645
Takashi Iwai352f7f92012-12-19 12:52:06 +01004646static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4647 struct hda_codec *codec,
4648 struct snd_pcm_substream *substream)
4649{
4650 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004651 int err;
4652
4653 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
4654 if (!err)
4655 call_pcm_playback_hook(hinfo, codec, substream,
4656 HDA_GEN_PCM_ACT_CLEANUP);
4657 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004658}
4659
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004660static int playback_pcm_close(struct hda_pcm_stream *hinfo,
4661 struct hda_codec *codec,
4662 struct snd_pcm_substream *substream)
4663{
4664 struct hda_gen_spec *spec = codec->spec;
4665 mutex_lock(&spec->pcm_mutex);
4666 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004667 call_pcm_playback_hook(hinfo, codec, substream,
4668 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004669 mutex_unlock(&spec->pcm_mutex);
4670 return 0;
4671}
4672
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004673static int capture_pcm_open(struct hda_pcm_stream *hinfo,
4674 struct hda_codec *codec,
4675 struct snd_pcm_substream *substream)
4676{
4677 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
4678 return 0;
4679}
4680
4681static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4682 struct hda_codec *codec,
4683 unsigned int stream_tag,
4684 unsigned int format,
4685 struct snd_pcm_substream *substream)
4686{
4687 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4688 call_pcm_capture_hook(hinfo, codec, substream,
4689 HDA_GEN_PCM_ACT_PREPARE);
4690 return 0;
4691}
4692
4693static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4694 struct hda_codec *codec,
4695 struct snd_pcm_substream *substream)
4696{
4697 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4698 call_pcm_capture_hook(hinfo, codec, substream,
4699 HDA_GEN_PCM_ACT_CLEANUP);
4700 return 0;
4701}
4702
4703static int capture_pcm_close(struct hda_pcm_stream *hinfo,
4704 struct hda_codec *codec,
4705 struct snd_pcm_substream *substream)
4706{
4707 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
4708 return 0;
4709}
4710
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004711static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
4712 struct hda_codec *codec,
4713 struct snd_pcm_substream *substream)
4714{
4715 struct hda_gen_spec *spec = codec->spec;
4716 int err = 0;
4717
4718 mutex_lock(&spec->pcm_mutex);
4719 if (!spec->indep_hp_enabled)
4720 err = -EBUSY;
4721 else
4722 spec->active_streams |= 1 << STREAM_INDEP_HP;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004723 call_pcm_playback_hook(hinfo, codec, substream,
4724 HDA_GEN_PCM_ACT_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004725 mutex_unlock(&spec->pcm_mutex);
4726 return err;
4727}
4728
4729static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
4730 struct hda_codec *codec,
4731 struct snd_pcm_substream *substream)
4732{
4733 struct hda_gen_spec *spec = codec->spec;
4734 mutex_lock(&spec->pcm_mutex);
4735 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004736 call_pcm_playback_hook(hinfo, codec, substream,
4737 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004738 mutex_unlock(&spec->pcm_mutex);
4739 return 0;
4740}
4741
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004742static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4743 struct hda_codec *codec,
4744 unsigned int stream_tag,
4745 unsigned int format,
4746 struct snd_pcm_substream *substream)
4747{
4748 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4749 call_pcm_playback_hook(hinfo, codec, substream,
4750 HDA_GEN_PCM_ACT_PREPARE);
4751 return 0;
4752}
4753
4754static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4755 struct hda_codec *codec,
4756 struct snd_pcm_substream *substream)
4757{
4758 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4759 call_pcm_playback_hook(hinfo, codec, substream,
4760 HDA_GEN_PCM_ACT_CLEANUP);
4761 return 0;
4762}
4763
Takashi Iwai352f7f92012-12-19 12:52:06 +01004764/*
4765 * Digital out
4766 */
4767static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
4768 struct hda_codec *codec,
4769 struct snd_pcm_substream *substream)
4770{
4771 struct hda_gen_spec *spec = codec->spec;
4772 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
4773}
4774
4775static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4776 struct hda_codec *codec,
4777 unsigned int stream_tag,
4778 unsigned int format,
4779 struct snd_pcm_substream *substream)
4780{
4781 struct hda_gen_spec *spec = codec->spec;
4782 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
4783 stream_tag, format, substream);
4784}
4785
4786static int dig_playback_pcm_cleanup(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_cleanup(codec, &spec->multiout);
4792}
4793
4794static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
4795 struct hda_codec *codec,
4796 struct snd_pcm_substream *substream)
4797{
4798 struct hda_gen_spec *spec = codec->spec;
4799 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
4800}
4801
4802/*
4803 * Analog capture
4804 */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004805#define alt_capture_pcm_open capture_pcm_open
4806#define alt_capture_pcm_close capture_pcm_close
4807
Takashi Iwai352f7f92012-12-19 12:52:06 +01004808static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4809 struct hda_codec *codec,
4810 unsigned int stream_tag,
4811 unsigned int format,
4812 struct snd_pcm_substream *substream)
4813{
4814 struct hda_gen_spec *spec = codec->spec;
4815
4816 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01004817 stream_tag, 0, format);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004818 call_pcm_capture_hook(hinfo, codec, substream,
4819 HDA_GEN_PCM_ACT_PREPARE);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004820 return 0;
4821}
4822
Takashi Iwai352f7f92012-12-19 12:52:06 +01004823static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4824 struct hda_codec *codec,
4825 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01004826{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004827 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01004828
Takashi Iwai352f7f92012-12-19 12:52:06 +01004829 snd_hda_codec_cleanup_stream(codec,
4830 spec->adc_nids[substream->number + 1]);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004831 call_pcm_capture_hook(hinfo, codec, substream,
4832 HDA_GEN_PCM_ACT_CLEANUP);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004833 return 0;
4834}
4835
Takashi Iwai352f7f92012-12-19 12:52:06 +01004836/*
4837 */
4838static const struct hda_pcm_stream pcm_analog_playback = {
4839 .substreams = 1,
4840 .channels_min = 2,
4841 .channels_max = 8,
4842 /* NID is set in build_pcms */
4843 .ops = {
4844 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004845 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004846 .prepare = playback_pcm_prepare,
4847 .cleanup = playback_pcm_cleanup
4848 },
4849};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850
Takashi Iwai352f7f92012-12-19 12:52:06 +01004851static const struct hda_pcm_stream pcm_analog_capture = {
4852 .substreams = 1,
4853 .channels_min = 2,
4854 .channels_max = 2,
4855 /* NID is set in build_pcms */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004856 .ops = {
4857 .open = capture_pcm_open,
4858 .close = capture_pcm_close,
4859 .prepare = capture_pcm_prepare,
4860 .cleanup = capture_pcm_cleanup
4861 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004862};
4863
4864static const struct hda_pcm_stream pcm_analog_alt_playback = {
4865 .substreams = 1,
4866 .channels_min = 2,
4867 .channels_max = 2,
4868 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004869 .ops = {
4870 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004871 .close = alt_playback_pcm_close,
4872 .prepare = alt_playback_pcm_prepare,
4873 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004874 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004875};
4876
4877static const struct hda_pcm_stream pcm_analog_alt_capture = {
4878 .substreams = 2, /* can be overridden */
4879 .channels_min = 2,
4880 .channels_max = 2,
4881 /* NID is set in build_pcms */
4882 .ops = {
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004883 .open = alt_capture_pcm_open,
4884 .close = alt_capture_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004885 .prepare = alt_capture_pcm_prepare,
4886 .cleanup = alt_capture_pcm_cleanup
4887 },
4888};
4889
4890static const struct hda_pcm_stream pcm_digital_playback = {
4891 .substreams = 1,
4892 .channels_min = 2,
4893 .channels_max = 2,
4894 /* NID is set in build_pcms */
4895 .ops = {
4896 .open = dig_playback_pcm_open,
4897 .close = dig_playback_pcm_close,
4898 .prepare = dig_playback_pcm_prepare,
4899 .cleanup = dig_playback_pcm_cleanup
4900 },
4901};
4902
4903static const struct hda_pcm_stream pcm_digital_capture = {
4904 .substreams = 1,
4905 .channels_min = 2,
4906 .channels_max = 2,
4907 /* NID is set in build_pcms */
4908};
4909
4910/* Used by build_pcms to flag that a PCM has no playback stream */
4911static const struct hda_pcm_stream pcm_null_stream = {
4912 .substreams = 0,
4913 .channels_min = 0,
4914 .channels_max = 0,
4915};
4916
4917/*
4918 * dynamic changing ADC PCM streams
4919 */
4920static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
4921{
4922 struct hda_gen_spec *spec = codec->spec;
4923 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
4924
4925 if (spec->cur_adc && spec->cur_adc != new_adc) {
4926 /* stream is running, let's swap the current ADC */
4927 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
4928 spec->cur_adc = new_adc;
4929 snd_hda_codec_setup_stream(codec, new_adc,
4930 spec->cur_adc_stream_tag, 0,
4931 spec->cur_adc_format);
4932 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004934 return false;
4935}
4936
4937/* analog capture with dynamic dual-adc changes */
4938static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4939 struct hda_codec *codec,
4940 unsigned int stream_tag,
4941 unsigned int format,
4942 struct snd_pcm_substream *substream)
4943{
4944 struct hda_gen_spec *spec = codec->spec;
4945 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
4946 spec->cur_adc_stream_tag = stream_tag;
4947 spec->cur_adc_format = format;
4948 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
4949 return 0;
4950}
4951
4952static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4953 struct hda_codec *codec,
4954 struct snd_pcm_substream *substream)
4955{
4956 struct hda_gen_spec *spec = codec->spec;
4957 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
4958 spec->cur_adc = 0;
4959 return 0;
4960}
4961
4962static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
4963 .substreams = 1,
4964 .channels_min = 2,
4965 .channels_max = 2,
4966 .nid = 0, /* fill later */
4967 .ops = {
4968 .prepare = dyn_adc_capture_pcm_prepare,
4969 .cleanup = dyn_adc_capture_pcm_cleanup
4970 },
4971};
4972
Takashi Iwaif873e532012-12-20 16:58:39 +01004973static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
4974 const char *chip_name)
4975{
4976 char *p;
4977
4978 if (*str)
4979 return;
4980 strlcpy(str, chip_name, len);
4981
4982 /* drop non-alnum chars after a space */
4983 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
4984 if (!isalnum(p[1])) {
4985 *p = 0;
4986 break;
4987 }
4988 }
4989 strlcat(str, sfx, len);
4990}
4991
Takashi Iwai352f7f92012-12-19 12:52:06 +01004992/* build PCM streams based on the parsed results */
4993int snd_hda_gen_build_pcms(struct hda_codec *codec)
4994{
4995 struct hda_gen_spec *spec = codec->spec;
4996 struct hda_pcm *info = spec->pcm_rec;
4997 const struct hda_pcm_stream *p;
4998 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999
5000 codec->num_pcms = 1;
5001 codec->pcm_info = info;
5002
Takashi Iwai352f7f92012-12-19 12:52:06 +01005003 if (spec->no_analog)
5004 goto skip_analog;
5005
Takashi Iwaif873e532012-12-20 16:58:39 +01005006 fill_pcm_stream_name(spec->stream_name_analog,
5007 sizeof(spec->stream_name_analog),
5008 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005009 info->name = spec->stream_name_analog;
5010
5011 if (spec->multiout.num_dacs > 0) {
5012 p = spec->stream_analog_playback;
5013 if (!p)
5014 p = &pcm_analog_playback;
5015 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
5016 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
5017 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
5018 spec->multiout.max_channels;
5019 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
5020 spec->autocfg.line_outs == 2)
5021 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
5022 snd_pcm_2_1_chmaps;
5023 }
5024 if (spec->num_adc_nids) {
5025 p = spec->stream_analog_capture;
5026 if (!p) {
5027 if (spec->dyn_adc_switch)
5028 p = &dyn_adc_pcm_analog_capture;
5029 else
5030 p = &pcm_analog_capture;
5031 }
5032 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
5033 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
5034 }
5035
Takashi Iwai352f7f92012-12-19 12:52:06 +01005036 skip_analog:
5037 /* SPDIF for stream index #1 */
5038 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01005039 fill_pcm_stream_name(spec->stream_name_digital,
5040 sizeof(spec->stream_name_digital),
5041 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005042 codec->num_pcms = 2;
5043 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
5044 info = spec->pcm_rec + 1;
5045 info->name = spec->stream_name_digital;
5046 if (spec->dig_out_type)
5047 info->pcm_type = spec->dig_out_type;
5048 else
5049 info->pcm_type = HDA_PCM_TYPE_SPDIF;
5050 if (spec->multiout.dig_out_nid) {
5051 p = spec->stream_digital_playback;
5052 if (!p)
5053 p = &pcm_digital_playback;
5054 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
5055 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
5056 }
5057 if (spec->dig_in_nid) {
5058 p = spec->stream_digital_capture;
5059 if (!p)
5060 p = &pcm_digital_capture;
5061 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
5062 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
5063 }
5064 }
5065
5066 if (spec->no_analog)
5067 return 0;
5068
5069 /* If the use of more than one ADC is requested for the current
5070 * model, configure a second analog capture-only PCM.
5071 */
5072 have_multi_adcs = (spec->num_adc_nids > 1) &&
5073 !spec->dyn_adc_switch && !spec->auto_mic;
5074 /* Additional Analaog capture for index #2 */
5075 if (spec->alt_dac_nid || have_multi_adcs) {
Takashi Iwaia6071482013-01-21 16:50:09 +01005076 fill_pcm_stream_name(spec->stream_name_alt_analog,
5077 sizeof(spec->stream_name_alt_analog),
5078 " Alt Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005079 codec->num_pcms = 3;
5080 info = spec->pcm_rec + 2;
Takashi Iwaia6071482013-01-21 16:50:09 +01005081 info->name = spec->stream_name_alt_analog;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005082 if (spec->alt_dac_nid) {
5083 p = spec->stream_analog_alt_playback;
5084 if (!p)
5085 p = &pcm_analog_alt_playback;
5086 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
5087 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
5088 spec->alt_dac_nid;
5089 } else {
5090 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
5091 pcm_null_stream;
5092 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
5093 }
5094 if (have_multi_adcs) {
5095 p = spec->stream_analog_alt_capture;
5096 if (!p)
5097 p = &pcm_analog_alt_capture;
5098 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
5099 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
5100 spec->adc_nids[1];
5101 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
5102 spec->num_adc_nids - 1;
5103 } else {
5104 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
5105 pcm_null_stream;
5106 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
5107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108 }
5109
5110 return 0;
5111}
Takashi Iwai352f7f92012-12-19 12:52:06 +01005112EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
5113
5114
5115/*
5116 * Standard auto-parser initializations
5117 */
5118
Takashi Iwaid4156932013-01-07 10:08:02 +01005119/* configure the given path as a proper output */
Takashi Iwai2c12c302013-01-10 09:33:29 +01005120static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005121{
5122 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01005123 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005124
Takashi Iwai196c17662013-01-04 15:01:40 +01005125 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01005126 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005127 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01005128 pin = path->path[path->depth - 1];
Takashi Iwai2c12c302013-01-10 09:33:29 +01005129 restore_pin_ctl(codec, pin);
Takashi Iwai65033cc2013-04-16 12:31:05 +02005130 snd_hda_activate_path(codec, path, path->active,
5131 aamix_default(codec->spec));
Takashi Iwaie1284af2013-01-03 16:33:02 +01005132 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005133}
5134
5135/* initialize primary output paths */
5136static void init_multi_out(struct hda_codec *codec)
5137{
5138 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005139 int i;
5140
Takashi Iwaid4156932013-01-07 10:08:02 +01005141 for (i = 0; i < spec->autocfg.line_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005142 set_output_and_unmute(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005143}
5144
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005145
Takashi Iwai2c12c302013-01-10 09:33:29 +01005146static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005147{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005148 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005149
Takashi Iwaid4156932013-01-07 10:08:02 +01005150 for (i = 0; i < num_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005151 set_output_and_unmute(codec, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005152}
5153
5154/* initialize hp and speaker paths */
5155static void init_extra_out(struct hda_codec *codec)
5156{
5157 struct hda_gen_spec *spec = codec->spec;
5158
5159 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005160 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005161 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
5162 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai2c12c302013-01-10 09:33:29 +01005163 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005164}
5165
5166/* initialize multi-io paths */
5167static void init_multi_io(struct hda_codec *codec)
5168{
5169 struct hda_gen_spec *spec = codec->spec;
5170 int i;
5171
5172 for (i = 0; i < spec->multi_ios; i++) {
5173 hda_nid_t pin = spec->multi_io[i].pin;
5174 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01005175 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005176 if (!path)
5177 continue;
5178 if (!spec->multi_io[i].ctl_in)
5179 spec->multi_io[i].ctl_in =
Takashi Iwai2c12c302013-01-10 09:33:29 +01005180 snd_hda_codec_get_pin_target(codec, pin);
Takashi Iwai65033cc2013-04-16 12:31:05 +02005181 snd_hda_activate_path(codec, path, path->active,
5182 aamix_default(spec));
Takashi Iwai352f7f92012-12-19 12:52:06 +01005183 }
5184}
5185
Takashi Iwai352f7f92012-12-19 12:52:06 +01005186/* set up input pins and loopback paths */
5187static void init_analog_input(struct hda_codec *codec)
5188{
5189 struct hda_gen_spec *spec = codec->spec;
5190 struct auto_pin_cfg *cfg = &spec->autocfg;
5191 int i;
5192
5193 for (i = 0; i < cfg->num_inputs; i++) {
5194 hda_nid_t nid = cfg->inputs[i].pin;
5195 if (is_input_pin(codec, nid))
Takashi Iwai2c12c302013-01-10 09:33:29 +01005196 restore_pin_ctl(codec, nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005197
5198 /* init loopback inputs */
5199 if (spec->mixer_nid) {
Takashi Iwai3e367f12013-01-23 17:07:23 +01005200 resume_path_from_idx(codec, spec->loopback_paths[i]);
5201 resume_path_from_idx(codec, spec->loopback_merge_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005202 }
5203 }
5204}
5205
5206/* initialize ADC paths */
5207static void init_input_src(struct hda_codec *codec)
5208{
5209 struct hda_gen_spec *spec = codec->spec;
5210 struct hda_input_mux *imux = &spec->input_mux;
5211 struct nid_path *path;
5212 int i, c, nums;
5213
5214 if (spec->dyn_adc_switch)
5215 nums = 1;
5216 else
5217 nums = spec->num_adc_nids;
5218
5219 for (c = 0; c < nums; c++) {
5220 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01005221 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005222 if (path) {
5223 bool active = path->active;
5224 if (i == spec->cur_mux[c])
5225 active = true;
5226 snd_hda_activate_path(codec, path, active, false);
5227 }
5228 }
Takashi Iwai967303d2013-02-19 17:12:42 +01005229 if (spec->hp_mic)
5230 update_hp_mic(codec, c, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005231 }
5232
Takashi Iwai352f7f92012-12-19 12:52:06 +01005233 if (spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01005234 spec->cap_sync_hook(codec, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005235}
5236
5237/* set right pin controls for digital I/O */
5238static void init_digital(struct hda_codec *codec)
5239{
5240 struct hda_gen_spec *spec = codec->spec;
5241 int i;
5242 hda_nid_t pin;
5243
Takashi Iwaid4156932013-01-07 10:08:02 +01005244 for (i = 0; i < spec->autocfg.dig_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005245 set_output_and_unmute(codec, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005246 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005247 if (pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01005248 restore_pin_ctl(codec, pin);
Takashi Iwai3e367f12013-01-23 17:07:23 +01005249 resume_path_from_idx(codec, spec->digin_path);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005250 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005251}
5252
Takashi Iwai973e4972012-12-20 15:16:09 +01005253/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5254 * invalid unsol tags by some reason
5255 */
5256static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5257{
5258 int i;
5259
5260 for (i = 0; i < codec->init_pins.used; i++) {
5261 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
5262 hda_nid_t nid = pin->nid;
5263 if (is_jack_detectable(codec, nid) &&
5264 !snd_hda_jack_tbl_get(codec, nid))
5265 snd_hda_codec_update_cache(codec, nid, 0,
5266 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5267 }
5268}
5269
Takashi Iwai5187ac12013-01-07 12:52:16 +01005270/*
5271 * initialize the generic spec;
5272 * this can be put as patch_ops.init function
5273 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005274int snd_hda_gen_init(struct hda_codec *codec)
5275{
5276 struct hda_gen_spec *spec = codec->spec;
5277
5278 if (spec->init_hook)
5279 spec->init_hook(codec);
5280
5281 snd_hda_apply_verbs(codec);
5282
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005283 codec->cached_write = 1;
5284
Takashi Iwai352f7f92012-12-19 12:52:06 +01005285 init_multi_out(codec);
5286 init_extra_out(codec);
5287 init_multi_io(codec);
5288 init_analog_input(codec);
5289 init_input_src(codec);
5290 init_digital(codec);
5291
Takashi Iwai973e4972012-12-20 15:16:09 +01005292 clear_unsol_on_unused_pins(codec);
5293
Takashi Iwai352f7f92012-12-19 12:52:06 +01005294 /* call init functions of standard auto-mute helpers */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01005295 update_automute_all(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005296
Takashi Iwaidc870f32013-01-22 15:24:30 +01005297 snd_hda_codec_flush_cache(codec);
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005298
Takashi Iwai352f7f92012-12-19 12:52:06 +01005299 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
5300 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5301
5302 hda_call_check_power_status(codec, 0x01);
5303 return 0;
5304}
Takashi Iwaifce52a32013-01-07 12:42:48 +01005305EXPORT_SYMBOL_HDA(snd_hda_gen_init);
5306
Takashi Iwai5187ac12013-01-07 12:52:16 +01005307/*
5308 * free the generic spec;
5309 * this can be put as patch_ops.free function
5310 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005311void snd_hda_gen_free(struct hda_codec *codec)
5312{
Takashi Iwai7504b6c2013-03-18 11:25:51 +01005313 snd_hda_detach_beep_device(codec);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005314 snd_hda_gen_spec_free(codec->spec);
5315 kfree(codec->spec);
5316 codec->spec = NULL;
5317}
5318EXPORT_SYMBOL_HDA(snd_hda_gen_free);
5319
5320#ifdef CONFIG_PM
Takashi Iwai5187ac12013-01-07 12:52:16 +01005321/*
5322 * check the loopback power save state;
5323 * this can be put as patch_ops.check_power_status function
5324 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005325int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
5326{
5327 struct hda_gen_spec *spec = codec->spec;
5328 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
5329}
5330EXPORT_SYMBOL_HDA(snd_hda_gen_check_power_status);
5331#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01005332
5333
5334/*
5335 * the generic codec support
5336 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337
Takashi Iwai352f7f92012-12-19 12:52:06 +01005338static const struct hda_codec_ops generic_patch_ops = {
5339 .build_controls = snd_hda_gen_build_controls,
5340 .build_pcms = snd_hda_gen_build_pcms,
5341 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01005342 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005343 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02005344#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01005345 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02005346#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347};
5348
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349int snd_hda_parse_generic_codec(struct hda_codec *codec)
5350{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005351 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352 int err;
5353
Takashi Iwaie560d8d2005-09-09 14:21:46 +02005354 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005355 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005357 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359
Takashi Iwai9eb413e2012-12-19 14:41:21 +01005360 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
5361 if (err < 0)
5362 return err;
5363
5364 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005365 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366 goto error;
5367
5368 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369 return 0;
5370
Takashi Iwai352f7f92012-12-19 12:52:06 +01005371error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01005372 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373 return err;
5374}
Takashi Iwaifce52a32013-01-07 12:42:48 +01005375EXPORT_SYMBOL_HDA(snd_hda_parse_generic_codec);