blob: 26ad4f0aade3fcf2c26940c008fae9a55c63244c [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 Iwai352f7f92012-12-19 12:52:06 +0100477/* look for an empty DAC slot */
478static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
479 bool is_digital)
480{
481 struct hda_gen_spec *spec = codec->spec;
482 bool cap_digital;
483 int i;
484
485 for (i = 0; i < spec->num_all_dacs; i++) {
486 hda_nid_t nid = spec->all_dacs[i];
487 if (!nid || is_dac_already_used(codec, nid))
488 continue;
489 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
490 if (is_digital != cap_digital)
491 continue;
492 if (is_reachable_path(codec, nid, pin))
493 return nid;
494 }
495 return 0;
496}
497
498/* replace the channels in the composed amp value with the given number */
499static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
500{
501 val &= ~(0x3U << 16);
502 val |= chs << 16;
503 return val;
504}
505
506/* check whether the widget has the given amp capability for the direction */
507static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
508 int dir, unsigned int bits)
509{
510 if (!nid)
511 return false;
512 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
513 if (query_amp_caps(codec, nid, dir) & bits)
514 return true;
515 return false;
516}
517
David Henningsson99a55922013-01-16 15:58:44 +0100518static bool same_amp_caps(struct hda_codec *codec, hda_nid_t nid1,
519 hda_nid_t nid2, int dir)
520{
521 if (!(get_wcaps(codec, nid1) & (1 << (dir + 1))))
522 return !(get_wcaps(codec, nid2) & (1 << (dir + 1)));
523 return (query_amp_caps(codec, nid1, dir) ==
524 query_amp_caps(codec, nid2, dir));
525}
526
Takashi Iwai352f7f92012-12-19 12:52:06 +0100527#define nid_has_mute(codec, nid, dir) \
Takashi Iwaif69910d2013-08-08 09:32:37 +0200528 check_amp_caps(codec, nid, dir, (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100529#define nid_has_volume(codec, nid, dir) \
530 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
531
532/* look for a widget suitable for assigning a mute switch in the path */
533static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
534 struct nid_path *path)
535{
536 int i;
537
538 for (i = path->depth - 1; i >= 0; i--) {
539 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
540 return path->path[i];
541 if (i != path->depth - 1 && i != 0 &&
542 nid_has_mute(codec, path->path[i], HDA_INPUT))
543 return path->path[i];
544 }
545 return 0;
546}
547
548/* look for a widget suitable for assigning a volume ctl in the path */
549static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
550 struct nid_path *path)
551{
552 int i;
553
554 for (i = path->depth - 1; i >= 0; i--) {
555 if (nid_has_volume(codec, path->path[i], HDA_OUTPUT))
556 return path->path[i];
557 }
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200558 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
560
561/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100562 * path activation / deactivation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100564
565/* can have the amp-in capability? */
566static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100568 hda_nid_t nid = path->path[idx];
569 unsigned int caps = get_wcaps(codec, nid);
570 unsigned int type = get_wcaps_type(caps);
571
572 if (!(caps & AC_WCAP_IN_AMP))
573 return false;
574 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
575 return false;
576 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577}
578
Takashi Iwai352f7f92012-12-19 12:52:06 +0100579/* can have the amp-out capability? */
580static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100582 hda_nid_t nid = path->path[idx];
583 unsigned int caps = get_wcaps(codec, nid);
584 unsigned int type = get_wcaps_type(caps);
585
586 if (!(caps & AC_WCAP_OUT_AMP))
587 return false;
588 if (type == AC_WID_PIN && !idx) /* only for output pins */
589 return false;
590 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592
Takashi Iwai352f7f92012-12-19 12:52:06 +0100593/* check whether the given (nid,dir,idx) is active */
594static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai7dddf2ae2013-01-24 16:31:35 +0100595 unsigned int dir, unsigned int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100597 struct hda_gen_spec *spec = codec->spec;
598 int i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Takashi Iwai352f7f92012-12-19 12:52:06 +0100600 for (n = 0; n < spec->paths.used; n++) {
601 struct nid_path *path = snd_array_elem(&spec->paths, n);
602 if (!path->active)
603 continue;
604 for (i = 0; i < path->depth; i++) {
605 if (path->path[i] == nid) {
606 if (dir == HDA_OUTPUT || path->idx[i] == idx)
607 return true;
608 break;
609 }
610 }
611 }
612 return false;
613}
614
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +0200615/* check whether the NID is referred by any active paths */
616#define is_active_nid_for_any(codec, nid) \
617 is_active_nid(codec, nid, HDA_OUTPUT, 0)
618
Takashi Iwai352f7f92012-12-19 12:52:06 +0100619/* get the default amp value for the target state */
620static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai8999bf02013-01-18 11:01:33 +0100621 int dir, unsigned int caps, bool enable)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100622{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100623 unsigned int val = 0;
624
Takashi Iwai352f7f92012-12-19 12:52:06 +0100625 if (caps & AC_AMPCAP_NUM_STEPS) {
626 /* set to 0dB */
627 if (enable)
628 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
629 }
Takashi Iwaif69910d2013-08-08 09:32:37 +0200630 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100631 if (!enable)
632 val |= HDA_AMP_MUTE;
633 }
634 return val;
635}
636
637/* initialize the amp value (only at the first time) */
638static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
639{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100640 unsigned int caps = query_amp_caps(codec, nid, dir);
641 int val = get_amp_val_to_activate(codec, nid, dir, caps, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100642 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
643}
644
Takashi Iwai8999bf02013-01-18 11:01:33 +0100645/* calculate amp value mask we can modify;
646 * if the given amp is controlled by mixers, don't touch it
647 */
648static unsigned int get_amp_mask_to_modify(struct hda_codec *codec,
649 hda_nid_t nid, int dir, int idx,
650 unsigned int caps)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100651{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100652 unsigned int mask = 0xff;
653
Takashi Iwaif69910d2013-08-08 09:32:37 +0200654 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
Takashi Iwai8999bf02013-01-18 11:01:33 +0100655 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_MUTE_CTL))
656 mask &= ~0x80;
657 }
658 if (caps & AC_AMPCAP_NUM_STEPS) {
659 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
660 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
661 mask &= ~0x7f;
662 }
663 return mask;
664}
665
666static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
667 int idx, int idx_to_check, bool enable)
668{
669 unsigned int caps;
670 unsigned int mask, val;
671
Takashi Iwai7dddf2ae2013-01-24 16:31:35 +0100672 if (!enable && is_active_nid(codec, nid, dir, idx_to_check))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100673 return;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100674
675 caps = query_amp_caps(codec, nid, dir);
676 val = get_amp_val_to_activate(codec, nid, dir, caps, enable);
677 mask = get_amp_mask_to_modify(codec, nid, dir, idx_to_check, caps);
678 if (!mask)
679 return;
680
681 val &= mask;
682 snd_hda_codec_amp_stereo(codec, nid, dir, idx, mask, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100683}
684
685static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
686 int i, bool enable)
687{
688 hda_nid_t nid = path->path[i];
689 init_amp(codec, nid, HDA_OUTPUT, 0);
Takashi Iwai8999bf02013-01-18 11:01:33 +0100690 activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100691}
692
693static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
694 int i, bool enable, bool add_aamix)
695{
696 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100697 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100698 int n, nums, idx;
699 int type;
700 hda_nid_t nid = path->path[i];
701
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100702 nums = snd_hda_get_conn_list(codec, nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100703 type = get_wcaps_type(get_wcaps(codec, nid));
704 if (type == AC_WID_PIN ||
705 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
706 nums = 1;
707 idx = 0;
708 } else
709 idx = path->idx[i];
710
711 for (n = 0; n < nums; n++)
712 init_amp(codec, nid, HDA_INPUT, n);
713
Takashi Iwai352f7f92012-12-19 12:52:06 +0100714 /* here is a little bit tricky in comparison with activate_amp_out();
715 * when aa-mixer is available, we need to enable the path as well
716 */
717 for (n = 0; n < nums; n++) {
Takashi Iwaie4a395e2013-01-23 17:00:31 +0100718 if (n != idx && (!add_aamix || conn[n] != spec->mixer_merge_nid))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100719 continue;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100720 activate_amp(codec, nid, HDA_INPUT, n, idx, enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 }
722}
723
Takashi Iwai352f7f92012-12-19 12:52:06 +0100724/* activate or deactivate the given path
725 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100727void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
728 bool enable, bool add_aamix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{
Takashi Iwai55196ff2013-01-24 17:32:56 +0100730 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100731 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Takashi Iwai352f7f92012-12-19 12:52:06 +0100733 if (!enable)
734 path->active = false;
735
736 for (i = path->depth - 1; i >= 0; i--) {
Takashi Iwai55196ff2013-01-24 17:32:56 +0100737 hda_nid_t nid = path->path[i];
738 if (enable && spec->power_down_unused) {
739 /* make sure the widget is powered up */
740 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0))
741 snd_hda_codec_write(codec, nid, 0,
742 AC_VERB_SET_POWER_STATE,
743 AC_PWRST_D0);
744 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100745 if (enable && path->multi[i])
Takashi Iwai55196ff2013-01-24 17:32:56 +0100746 snd_hda_codec_write_cache(codec, nid, 0,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100747 AC_VERB_SET_CONNECT_SEL,
748 path->idx[i]);
749 if (has_amp_in(codec, path, i))
750 activate_amp_in(codec, path, i, enable, add_aamix);
751 if (has_amp_out(codec, path, i))
752 activate_amp_out(codec, path, i, enable);
753 }
754
755 if (enable)
756 path->active = true;
757}
758EXPORT_SYMBOL_HDA(snd_hda_activate_path);
759
Takashi Iwai55196ff2013-01-24 17:32:56 +0100760/* if the given path is inactive, put widgets into D3 (only if suitable) */
761static void path_power_down_sync(struct hda_codec *codec, struct nid_path *path)
762{
763 struct hda_gen_spec *spec = codec->spec;
Jiri Slaby868211d2013-04-04 22:32:10 +0200764 bool changed = false;
Takashi Iwai55196ff2013-01-24 17:32:56 +0100765 int i;
766
767 if (!spec->power_down_unused || path->active)
768 return;
769
770 for (i = 0; i < path->depth; i++) {
771 hda_nid_t nid = path->path[i];
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +0200772 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D3) &&
773 !is_active_nid_for_any(codec, nid)) {
Takashi Iwai55196ff2013-01-24 17:32:56 +0100774 snd_hda_codec_write(codec, nid, 0,
775 AC_VERB_SET_POWER_STATE,
776 AC_PWRST_D3);
777 changed = true;
778 }
779 }
780
781 if (changed) {
782 msleep(10);
783 snd_hda_codec_read(codec, path->path[0], 0,
784 AC_VERB_GET_POWER_STATE, 0);
785 }
786}
787
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100788/* turn on/off EAPD on the given pin */
789static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
790{
791 struct hda_gen_spec *spec = codec->spec;
792 if (spec->own_eapd_ctl ||
793 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
794 return;
Takashi Iwaiecac3ed2012-12-21 15:23:01 +0100795 if (codec->inv_eapd)
796 enable = !enable;
Takashi Iwai05909d5c2013-05-31 19:55:54 +0200797 if (spec->keep_eapd_on && !enable)
798 return;
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100799 snd_hda_codec_update_cache(codec, pin, 0,
800 AC_VERB_SET_EAPD_BTLENABLE,
801 enable ? 0x02 : 0x00);
802}
803
Takashi Iwai3e367f12013-01-23 17:07:23 +0100804/* re-initialize the path specified by the given path index */
805static void resume_path_from_idx(struct hda_codec *codec, int path_idx)
806{
807 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
808 if (path)
809 snd_hda_activate_path(codec, path, path->active, false);
810}
811
Takashi Iwai352f7f92012-12-19 12:52:06 +0100812
813/*
814 * Helper functions for creating mixer ctl elements
815 */
816
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200817static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
818 struct snd_ctl_elem_value *ucontrol);
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200819static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
820 struct snd_ctl_elem_value *ucontrol);
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200821
Takashi Iwai352f7f92012-12-19 12:52:06 +0100822enum {
823 HDA_CTL_WIDGET_VOL,
824 HDA_CTL_WIDGET_MUTE,
825 HDA_CTL_BIND_MUTE,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100826};
827static const struct snd_kcontrol_new control_templates[] = {
828 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200829 /* only the put callback is replaced for handling the special mute */
830 {
831 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
832 .subdevice = HDA_SUBDEV_AMP_FLAG,
833 .info = snd_hda_mixer_amp_switch_info,
834 .get = snd_hda_mixer_amp_switch_get,
835 .put = hda_gen_mixer_mute_put, /* replaced */
836 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
837 },
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200838 {
839 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
840 .info = snd_hda_mixer_amp_switch_info,
841 .get = snd_hda_mixer_bind_switch_get,
842 .put = hda_gen_bind_mute_put, /* replaced */
843 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
844 },
Takashi Iwai352f7f92012-12-19 12:52:06 +0100845};
846
847/* add dynamic controls from template */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100848static struct snd_kcontrol_new *
849add_control(struct hda_gen_spec *spec, int type, const char *name,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100850 int cidx, unsigned long val)
851{
852 struct snd_kcontrol_new *knew;
853
Takashi Iwai12c93df2012-12-19 14:38:33 +0100854 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100855 if (!knew)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100856 return NULL;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100857 knew->index = cidx;
858 if (get_amp_nid_(val))
859 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
860 knew->private_value = val;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100861 return knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100862}
863
864static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
865 const char *pfx, const char *dir,
866 const char *sfx, int cidx, unsigned long val)
867{
Takashi Iwai975cc022013-06-28 11:56:49 +0200868 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai352f7f92012-12-19 12:52:06 +0100869 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100870 if (!add_control(spec, type, name, cidx, val))
871 return -ENOMEM;
872 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100873}
874
875#define add_pb_vol_ctrl(spec, type, pfx, val) \
876 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
877#define add_pb_sw_ctrl(spec, type, pfx, val) \
878 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
879#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
880 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
881#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
882 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
883
884static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
885 unsigned int chs, struct nid_path *path)
886{
887 unsigned int val;
888 if (!path)
889 return 0;
890 val = path->ctls[NID_PATH_VOL_CTL];
891 if (!val)
892 return 0;
893 val = amp_val_replace_channels(val, chs);
894 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
895}
896
897/* return the channel bits suitable for the given path->ctls[] */
898static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
899 int type)
900{
901 int chs = 1; /* mono (left only) */
902 if (path) {
903 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
904 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
905 chs = 3; /* stereo */
906 }
907 return chs;
908}
909
910static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
911 struct nid_path *path)
912{
913 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
914 return add_vol_ctl(codec, pfx, cidx, chs, path);
915}
916
917/* create a mute-switch for the given mixer widget;
918 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
919 */
920static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
921 unsigned int chs, struct nid_path *path)
922{
923 unsigned int val;
924 int type = HDA_CTL_WIDGET_MUTE;
925
926 if (!path)
927 return 0;
928 val = path->ctls[NID_PATH_MUTE_CTL];
929 if (!val)
930 return 0;
931 val = amp_val_replace_channels(val, chs);
932 if (get_amp_direction_(val) == HDA_INPUT) {
933 hda_nid_t nid = get_amp_nid_(val);
934 int nums = snd_hda_get_num_conns(codec, nid);
935 if (nums > 1) {
936 type = HDA_CTL_BIND_MUTE;
937 val |= nums << 19;
938 }
939 }
940 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
941}
942
943static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
944 int cidx, struct nid_path *path)
945{
946 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
947 return add_sw_ctl(codec, pfx, cidx, chs, path);
948}
949
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200950/* playback mute control with the software mute bit check */
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200951static void sync_auto_mute_bits(struct snd_kcontrol *kcontrol,
952 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200953{
954 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
955 struct hda_gen_spec *spec = codec->spec;
956
957 if (spec->auto_mute_via_amp) {
958 hda_nid_t nid = get_amp_nid(kcontrol);
959 bool enabled = !((spec->mute_bits >> nid) & 1);
960 ucontrol->value.integer.value[0] &= enabled;
961 ucontrol->value.integer.value[1] &= enabled;
962 }
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200963}
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200964
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200965static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
966 struct snd_ctl_elem_value *ucontrol)
967{
968 sync_auto_mute_bits(kcontrol, ucontrol);
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200969 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
970}
971
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200972static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
973 struct snd_ctl_elem_value *ucontrol)
974{
975 sync_auto_mute_bits(kcontrol, ucontrol);
976 return snd_hda_mixer_bind_switch_put(kcontrol, ucontrol);
977}
978
Takashi Iwai247d85e2013-01-17 16:18:11 +0100979/* any ctl assigned to the path with the given index? */
980static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
981{
982 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
983 return path && path->ctls[ctl_type];
984}
985
Takashi Iwai352f7f92012-12-19 12:52:06 +0100986static const char * const channel_name[4] = {
987 "Front", "Surround", "CLFE", "Side"
988};
989
990/* give some appropriate ctl name prefix for the given line out channel */
Takashi Iwai247d85e2013-01-17 16:18:11 +0100991static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
992 int *index, int ctl_type)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100993{
Takashi Iwai247d85e2013-01-17 16:18:11 +0100994 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100995 struct auto_pin_cfg *cfg = &spec->autocfg;
996
997 *index = 0;
998 if (cfg->line_outs == 1 && !spec->multi_ios &&
Takashi Iwai247d85e2013-01-17 16:18:11 +0100999 !cfg->hp_outs && !cfg->speaker_outs)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001000 return spec->vmaster_mute.hook ? "PCM" : "Master";
1001
1002 /* if there is really a single DAC used in the whole output paths,
1003 * use it master (or "PCM" if a vmaster hook is present)
1004 */
1005 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
1006 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
1007 return spec->vmaster_mute.hook ? "PCM" : "Master";
1008
Takashi Iwai247d85e2013-01-17 16:18:11 +01001009 /* multi-io channels */
1010 if (ch >= cfg->line_outs)
1011 return channel_name[ch];
1012
Takashi Iwai352f7f92012-12-19 12:52:06 +01001013 switch (cfg->line_out_type) {
1014 case AUTO_PIN_SPEAKER_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +01001015 /* if the primary channel vol/mute is shared with HP volume,
1016 * don't name it as Speaker
1017 */
1018 if (!ch && cfg->hp_outs &&
1019 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
1020 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001021 if (cfg->line_outs == 1)
1022 return "Speaker";
1023 if (cfg->line_outs == 2)
1024 return ch ? "Bass Speaker" : "Speaker";
1025 break;
1026 case AUTO_PIN_HP_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +01001027 /* if the primary channel vol/mute is shared with spk volume,
1028 * don't name it as Headphone
1029 */
1030 if (!ch && cfg->speaker_outs &&
1031 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
1032 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001033 /* for multi-io case, only the primary out */
1034 if (ch && spec->multi_ios)
1035 break;
1036 *index = ch;
1037 return "Headphone";
Takashi Iwai352f7f92012-12-19 12:52:06 +01001038 }
Takashi Iwai247d85e2013-01-17 16:18:11 +01001039
1040 /* for a single channel output, we don't have to name the channel */
1041 if (cfg->line_outs == 1 && !spec->multi_ios)
1042 return "PCM";
1043
Takashi Iwai352f7f92012-12-19 12:52:06 +01001044 if (ch >= ARRAY_SIZE(channel_name)) {
1045 snd_BUG();
1046 return "PCM";
1047 }
1048
1049 return channel_name[ch];
1050}
1051
1052/*
1053 * Parse output paths
1054 */
1055
1056/* badness definition */
1057enum {
1058 /* No primary DAC is found for the main output */
1059 BAD_NO_PRIMARY_DAC = 0x10000,
1060 /* No DAC is found for the extra output */
1061 BAD_NO_DAC = 0x4000,
1062 /* No possible multi-ios */
Takashi Iwai1d739062013-02-13 14:17:32 +01001063 BAD_MULTI_IO = 0x120,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001064 /* No individual DAC for extra output */
1065 BAD_NO_EXTRA_DAC = 0x102,
1066 /* No individual DAC for extra surrounds */
1067 BAD_NO_EXTRA_SURR_DAC = 0x101,
1068 /* Primary DAC shared with main surrounds */
1069 BAD_SHARED_SURROUND = 0x100,
Takashi Iwai55a63d42013-03-21 17:20:12 +01001070 /* No independent HP possible */
Takashi Iwaibec8e682013-03-22 15:10:08 +01001071 BAD_NO_INDEP_HP = 0x10,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001072 /* Primary DAC shared with main CLFE */
1073 BAD_SHARED_CLFE = 0x10,
1074 /* Primary DAC shared with extra surrounds */
1075 BAD_SHARED_EXTRA_SURROUND = 0x10,
1076 /* Volume widget is shared */
1077 BAD_SHARED_VOL = 0x10,
1078};
1079
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001080/* look for widgets in the given path which are appropriate for
Takashi Iwai352f7f92012-12-19 12:52:06 +01001081 * volume and mute controls, and assign the values to ctls[].
1082 *
1083 * When no appropriate widget is found in the path, the badness value
1084 * is incremented depending on the situation. The function returns the
1085 * total badness for both volume and mute controls.
1086 */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001087static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001088{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001089 hda_nid_t nid;
1090 unsigned int val;
1091 int badness = 0;
1092
1093 if (!path)
1094 return BAD_SHARED_VOL * 2;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001095
1096 if (path->ctls[NID_PATH_VOL_CTL] ||
1097 path->ctls[NID_PATH_MUTE_CTL])
1098 return 0; /* already evaluated */
1099
Takashi Iwai352f7f92012-12-19 12:52:06 +01001100 nid = look_for_out_vol_nid(codec, path);
1101 if (nid) {
1102 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1103 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1104 badness += BAD_SHARED_VOL;
1105 else
1106 path->ctls[NID_PATH_VOL_CTL] = val;
1107 } else
1108 badness += BAD_SHARED_VOL;
1109 nid = look_for_out_mute_nid(codec, path);
1110 if (nid) {
1111 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1112 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1113 nid_has_mute(codec, nid, HDA_OUTPUT))
1114 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1115 else
1116 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1117 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1118 badness += BAD_SHARED_VOL;
1119 else
1120 path->ctls[NID_PATH_MUTE_CTL] = val;
1121 } else
1122 badness += BAD_SHARED_VOL;
1123 return badness;
1124}
1125
Takashi Iwai98bd1112013-03-22 14:53:50 +01001126const struct badness_table hda_main_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001127 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1128 .no_dac = BAD_NO_DAC,
1129 .shared_primary = BAD_NO_PRIMARY_DAC,
1130 .shared_surr = BAD_SHARED_SURROUND,
1131 .shared_clfe = BAD_SHARED_CLFE,
1132 .shared_surr_main = BAD_SHARED_SURROUND,
1133};
Takashi Iwai98bd1112013-03-22 14:53:50 +01001134EXPORT_SYMBOL_HDA(hda_main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001135
Takashi Iwai98bd1112013-03-22 14:53:50 +01001136const struct badness_table hda_extra_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001137 .no_primary_dac = BAD_NO_DAC,
1138 .no_dac = BAD_NO_DAC,
1139 .shared_primary = BAD_NO_EXTRA_DAC,
1140 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1141 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1142 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1143};
Takashi Iwai98bd1112013-03-22 14:53:50 +01001144EXPORT_SYMBOL_HDA(hda_extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001145
Takashi Iwai7385df62013-01-07 09:50:52 +01001146/* get the DAC of the primary output corresponding to the given array index */
1147static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1148{
1149 struct hda_gen_spec *spec = codec->spec;
1150 struct auto_pin_cfg *cfg = &spec->autocfg;
1151
1152 if (cfg->line_outs > idx)
1153 return spec->private_dac_nids[idx];
1154 idx -= cfg->line_outs;
1155 if (spec->multi_ios > idx)
1156 return spec->multi_io[idx].dac;
1157 return 0;
1158}
1159
1160/* return the DAC if it's reachable, otherwise zero */
1161static inline hda_nid_t try_dac(struct hda_codec *codec,
1162 hda_nid_t dac, hda_nid_t pin)
1163{
1164 return is_reachable_path(codec, dac, pin) ? dac : 0;
1165}
1166
Takashi Iwai352f7f92012-12-19 12:52:06 +01001167/* try to assign DACs to pins and return the resultant badness */
1168static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1169 const hda_nid_t *pins, hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001170 int *path_idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001171 const struct badness_table *bad)
1172{
1173 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001174 int i, j;
1175 int badness = 0;
1176 hda_nid_t dac;
1177
1178 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 return 0;
1180
Takashi Iwai352f7f92012-12-19 12:52:06 +01001181 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001182 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001183 hda_nid_t pin = pins[i];
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001184
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001185 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1186 if (path) {
1187 badness += assign_out_path_ctls(codec, path);
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001188 continue;
1189 }
1190
1191 dacs[i] = look_for_dac(codec, pin, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001192 if (!dacs[i] && !i) {
Takashi Iwai980428c2013-01-09 09:28:20 +01001193 /* try to steal the DAC of surrounds for the front */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001194 for (j = 1; j < num_outs; j++) {
1195 if (is_reachable_path(codec, dacs[j], pin)) {
1196 dacs[0] = dacs[j];
1197 dacs[j] = 0;
Takashi Iwai980428c2013-01-09 09:28:20 +01001198 invalidate_nid_path(codec, path_idx[j]);
Takashi Iwai196c17662013-01-04 15:01:40 +01001199 path_idx[j] = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001200 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 }
1202 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001203 }
1204 dac = dacs[i];
1205 if (!dac) {
Takashi Iwai7385df62013-01-07 09:50:52 +01001206 if (num_outs > 2)
1207 dac = try_dac(codec, get_primary_out(codec, i), pin);
1208 if (!dac)
1209 dac = try_dac(codec, dacs[0], pin);
1210 if (!dac)
1211 dac = try_dac(codec, get_primary_out(codec, i), pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001212 if (dac) {
1213 if (!i)
1214 badness += bad->shared_primary;
1215 else if (i == 1)
1216 badness += bad->shared_surr;
1217 else
1218 badness += bad->shared_clfe;
1219 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1220 dac = spec->private_dac_nids[0];
1221 badness += bad->shared_surr_main;
1222 } else if (!i)
1223 badness += bad->no_primary_dac;
1224 else
1225 badness += bad->no_dac;
1226 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001227 if (!dac)
1228 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001229 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001230 if (!path && !i && spec->mixer_nid) {
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001231 /* try with aamix */
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001232 path = snd_hda_add_new_path(codec, dac, pin, 0);
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001233 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001234 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001235 dac = dacs[i] = 0;
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001236 badness += bad->no_dac;
1237 } else {
Takashi Iwaia7694092013-01-21 10:43:18 +01001238 /* print_nid_path("output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001239 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001240 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001241 badness += assign_out_path_ctls(codec, path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001242 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001243 }
1244
1245 return badness;
1246}
1247
1248/* return NID if the given pin has only a single connection to a certain DAC */
1249static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1250{
1251 struct hda_gen_spec *spec = codec->spec;
1252 int i;
1253 hda_nid_t nid_found = 0;
1254
1255 for (i = 0; i < spec->num_all_dacs; i++) {
1256 hda_nid_t nid = spec->all_dacs[i];
1257 if (!nid || is_dac_already_used(codec, nid))
1258 continue;
1259 if (is_reachable_path(codec, nid, pin)) {
1260 if (nid_found)
1261 return 0;
1262 nid_found = nid;
1263 }
1264 }
1265 return nid_found;
1266}
1267
1268/* check whether the given pin can be a multi-io pin */
1269static bool can_be_multiio_pin(struct hda_codec *codec,
1270 unsigned int location, hda_nid_t nid)
1271{
1272 unsigned int defcfg, caps;
1273
1274 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1275 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1276 return false;
1277 if (location && get_defcfg_location(defcfg) != location)
1278 return false;
1279 caps = snd_hda_query_pin_caps(codec, nid);
1280 if (!(caps & AC_PINCAP_OUT))
1281 return false;
1282 return true;
1283}
1284
Takashi Iwaie22aab72013-01-04 14:50:04 +01001285/* count the number of input pins that are capable to be multi-io */
1286static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1287{
1288 struct hda_gen_spec *spec = codec->spec;
1289 struct auto_pin_cfg *cfg = &spec->autocfg;
1290 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1291 unsigned int location = get_defcfg_location(defcfg);
1292 int type, i;
1293 int num_pins = 0;
1294
1295 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1296 for (i = 0; i < cfg->num_inputs; i++) {
1297 if (cfg->inputs[i].type != type)
1298 continue;
1299 if (can_be_multiio_pin(codec, location,
1300 cfg->inputs[i].pin))
1301 num_pins++;
1302 }
1303 }
1304 return num_pins;
1305}
1306
Takashi Iwai352f7f92012-12-19 12:52:06 +01001307/*
1308 * multi-io helper
1309 *
1310 * When hardwired is set, try to fill ony hardwired pins, and returns
1311 * zero if any pins are filled, non-zero if nothing found.
1312 * When hardwired is off, try to fill possible input pins, and returns
1313 * the badness value.
1314 */
1315static int fill_multi_ios(struct hda_codec *codec,
1316 hda_nid_t reference_pin,
Takashi Iwaie22aab72013-01-04 14:50:04 +01001317 bool hardwired)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001318{
1319 struct hda_gen_spec *spec = codec->spec;
1320 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie22aab72013-01-04 14:50:04 +01001321 int type, i, j, num_pins, old_pins;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001322 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1323 unsigned int location = get_defcfg_location(defcfg);
1324 int badness = 0;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001325 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001326
1327 old_pins = spec->multi_ios;
1328 if (old_pins >= 2)
1329 goto end_fill;
1330
Takashi Iwaie22aab72013-01-04 14:50:04 +01001331 num_pins = count_multiio_pins(codec, reference_pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001332 if (num_pins < 2)
1333 goto end_fill;
1334
Takashi Iwai352f7f92012-12-19 12:52:06 +01001335 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1336 for (i = 0; i < cfg->num_inputs; i++) {
1337 hda_nid_t nid = cfg->inputs[i].pin;
1338 hda_nid_t dac = 0;
1339
1340 if (cfg->inputs[i].type != type)
1341 continue;
1342 if (!can_be_multiio_pin(codec, location, nid))
1343 continue;
1344 for (j = 0; j < spec->multi_ios; j++) {
1345 if (nid == spec->multi_io[j].pin)
1346 break;
1347 }
1348 if (j < spec->multi_ios)
1349 continue;
1350
Takashi Iwai352f7f92012-12-19 12:52:06 +01001351 if (hardwired)
1352 dac = get_dac_if_single(codec, nid);
1353 else if (!dac)
1354 dac = look_for_dac(codec, nid, false);
1355 if (!dac) {
1356 badness++;
1357 continue;
1358 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001359 path = snd_hda_add_new_path(codec, dac, nid,
1360 -spec->mixer_nid);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001361 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001362 badness++;
1363 continue;
1364 }
Takashi Iwaia7694092013-01-21 10:43:18 +01001365 /* print_nid_path("multiio", path); */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001366 spec->multi_io[spec->multi_ios].pin = nid;
1367 spec->multi_io[spec->multi_ios].dac = dac;
Takashi Iwai196c17662013-01-04 15:01:40 +01001368 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1369 snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001370 spec->multi_ios++;
1371 if (spec->multi_ios >= 2)
1372 break;
1373 }
1374 }
1375 end_fill:
1376 if (badness)
1377 badness = BAD_MULTI_IO;
1378 if (old_pins == spec->multi_ios) {
1379 if (hardwired)
1380 return 1; /* nothing found */
1381 else
1382 return badness; /* no badness if nothing found */
1383 }
1384 if (!hardwired && spec->multi_ios < 2) {
1385 /* cancel newly assigned paths */
1386 spec->paths.used -= spec->multi_ios - old_pins;
1387 spec->multi_ios = old_pins;
1388 return badness;
1389 }
1390
1391 /* assign volume and mute controls */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001392 for (i = old_pins; i < spec->multi_ios; i++) {
1393 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1394 badness += assign_out_path_ctls(codec, path);
1395 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001396
1397 return badness;
1398}
1399
1400/* map DACs for all pins in the list if they are single connections */
1401static bool map_singles(struct hda_codec *codec, int outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001402 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001403{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001404 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001405 int i;
1406 bool found = false;
1407 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001408 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001409 hda_nid_t dac;
1410 if (dacs[i])
1411 continue;
1412 dac = get_dac_if_single(codec, pins[i]);
1413 if (!dac)
1414 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001415 path = snd_hda_add_new_path(codec, dac, pins[i],
1416 -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001417 if (!path && !i && spec->mixer_nid)
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001418 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001419 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001420 dacs[i] = dac;
1421 found = true;
Takashi Iwaia7694092013-01-21 10:43:18 +01001422 /* print_nid_path("output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001423 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001424 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001425 }
1426 }
1427 return found;
1428}
1429
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001430/* create a new path including aamix if available, and return its index */
1431static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1432{
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001433 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001434 struct nid_path *path;
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001435 hda_nid_t path_dac, dac, pin;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001436
1437 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001438 if (!path || !path->depth ||
1439 is_nid_contained(path, spec->mixer_nid))
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001440 return 0;
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001441 path_dac = path->path[0];
1442 dac = spec->private_dac_nids[0];
Takashi Iwaif87498b2013-01-21 14:24:31 +01001443 pin = path->path[path->depth - 1];
1444 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1445 if (!path) {
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001446 if (dac != path_dac)
1447 dac = path_dac;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001448 else if (spec->multiout.hp_out_nid[0])
1449 dac = spec->multiout.hp_out_nid[0];
1450 else if (spec->multiout.extra_out_nid[0])
1451 dac = spec->multiout.extra_out_nid[0];
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001452 else
1453 dac = 0;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001454 if (dac)
1455 path = snd_hda_add_new_path(codec, dac, pin,
1456 spec->mixer_nid);
1457 }
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001458 if (!path)
1459 return 0;
Takashi Iwaia7694092013-01-21 10:43:18 +01001460 /* print_nid_path("output-aamix", path); */
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001461 path->active = false; /* unused as default */
1462 return snd_hda_get_path_idx(codec, path);
1463}
1464
Takashi Iwai55a63d42013-03-21 17:20:12 +01001465/* check whether the independent HP is available with the current config */
1466static bool indep_hp_possible(struct hda_codec *codec)
1467{
1468 struct hda_gen_spec *spec = codec->spec;
1469 struct auto_pin_cfg *cfg = &spec->autocfg;
1470 struct nid_path *path;
1471 int i, idx;
1472
1473 if (cfg->line_out_type == AUTO_PIN_HP_OUT)
1474 idx = spec->out_paths[0];
1475 else
1476 idx = spec->hp_paths[0];
1477 path = snd_hda_get_path_from_idx(codec, idx);
1478 if (!path)
1479 return false;
1480
1481 /* assume no path conflicts unless aamix is involved */
1482 if (!spec->mixer_nid || !is_nid_contained(path, spec->mixer_nid))
1483 return true;
1484
1485 /* check whether output paths contain aamix */
1486 for (i = 0; i < cfg->line_outs; i++) {
1487 if (spec->out_paths[i] == idx)
1488 break;
1489 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1490 if (path && is_nid_contained(path, spec->mixer_nid))
1491 return false;
1492 }
1493 for (i = 0; i < cfg->speaker_outs; i++) {
1494 path = snd_hda_get_path_from_idx(codec, spec->speaker_paths[i]);
1495 if (path && is_nid_contained(path, spec->mixer_nid))
1496 return false;
1497 }
1498
1499 return true;
1500}
1501
Takashi Iwaia07a9492013-01-07 16:44:06 +01001502/* fill the empty entries in the dac array for speaker/hp with the
1503 * shared dac pointed by the paths
1504 */
1505static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1506 hda_nid_t *dacs, int *path_idx)
1507{
1508 struct nid_path *path;
1509 int i;
1510
1511 for (i = 0; i < num_outs; i++) {
1512 if (dacs[i])
1513 continue;
1514 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1515 if (!path)
1516 continue;
1517 dacs[i] = path->path[0];
1518 }
1519}
1520
Takashi Iwai352f7f92012-12-19 12:52:06 +01001521/* fill in the dac_nids table from the parsed pin configuration */
1522static int fill_and_eval_dacs(struct hda_codec *codec,
1523 bool fill_hardwired,
1524 bool fill_mio_first)
1525{
1526 struct hda_gen_spec *spec = codec->spec;
1527 struct auto_pin_cfg *cfg = &spec->autocfg;
1528 int i, err, badness;
1529
1530 /* set num_dacs once to full for look_for_dac() */
1531 spec->multiout.num_dacs = cfg->line_outs;
1532 spec->multiout.dac_nids = spec->private_dac_nids;
1533 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1534 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1535 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1536 spec->multi_ios = 0;
1537 snd_array_free(&spec->paths);
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001538
1539 /* clear path indices */
1540 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1541 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1542 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1543 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1544 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
Takashi Iwaic697b712013-01-07 17:09:26 +01001545 memset(spec->input_paths, 0, sizeof(spec->input_paths));
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001546 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1547 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1548
Takashi Iwai352f7f92012-12-19 12:52:06 +01001549 badness = 0;
1550
1551 /* fill hard-wired DACs first */
1552 if (fill_hardwired) {
1553 bool mapped;
1554 do {
1555 mapped = map_singles(codec, cfg->line_outs,
1556 cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001557 spec->private_dac_nids,
1558 spec->out_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001559 mapped |= map_singles(codec, cfg->hp_outs,
1560 cfg->hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001561 spec->multiout.hp_out_nid,
1562 spec->hp_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001563 mapped |= map_singles(codec, cfg->speaker_outs,
1564 cfg->speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001565 spec->multiout.extra_out_nid,
1566 spec->speaker_paths);
Takashi Iwaida96fb52013-07-29 16:54:36 +02001567 if (!spec->no_multi_io &&
1568 fill_mio_first && cfg->line_outs == 1 &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01001569 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001570 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001571 if (!err)
1572 mapped = true;
1573 }
1574 } while (mapped);
1575 }
1576
1577 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001578 spec->private_dac_nids, spec->out_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001579 spec->main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001580
Takashi Iwaida96fb52013-07-29 16:54:36 +02001581 if (!spec->no_multi_io && fill_mio_first &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01001582 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1583 /* try to fill multi-io first */
Takashi Iwaie22aab72013-01-04 14:50:04 +01001584 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001585 if (err < 0)
1586 return err;
1587 /* we don't count badness at this stage yet */
1588 }
1589
1590 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1591 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1592 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001593 spec->hp_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001594 spec->extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001595 if (err < 0)
1596 return err;
1597 badness += err;
1598 }
1599 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1600 err = try_assign_dacs(codec, cfg->speaker_outs,
1601 cfg->speaker_pins,
1602 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001603 spec->speaker_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001604 spec->extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001605 if (err < 0)
1606 return err;
1607 badness += err;
1608 }
Takashi Iwaida96fb52013-07-29 16:54:36 +02001609 if (!spec->no_multi_io &&
1610 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001611 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001612 if (err < 0)
1613 return err;
1614 badness += err;
1615 }
Takashi Iwaie22aab72013-01-04 14:50:04 +01001616
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001617 if (spec->mixer_nid) {
1618 spec->aamix_out_paths[0] =
1619 check_aamix_out_path(codec, spec->out_paths[0]);
1620 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1621 spec->aamix_out_paths[1] =
1622 check_aamix_out_path(codec, spec->hp_paths[0]);
1623 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1624 spec->aamix_out_paths[2] =
1625 check_aamix_out_path(codec, spec->speaker_paths[0]);
1626 }
1627
Takashi Iwaida96fb52013-07-29 16:54:36 +02001628 if (!spec->no_multi_io &&
1629 cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
Takashi Iwaie22aab72013-01-04 14:50:04 +01001630 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1631 spec->multi_ios = 1; /* give badness */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001632
Takashi Iwaia07a9492013-01-07 16:44:06 +01001633 /* re-count num_dacs and squash invalid entries */
1634 spec->multiout.num_dacs = 0;
1635 for (i = 0; i < cfg->line_outs; i++) {
1636 if (spec->private_dac_nids[i])
1637 spec->multiout.num_dacs++;
1638 else {
1639 memmove(spec->private_dac_nids + i,
1640 spec->private_dac_nids + i + 1,
1641 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1642 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1643 }
1644 }
1645
1646 spec->ext_channel_count = spec->min_channel_count =
David Henningssonc0f3b212013-01-16 11:45:37 +01001647 spec->multiout.num_dacs * 2;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001648
Takashi Iwai352f7f92012-12-19 12:52:06 +01001649 if (spec->multi_ios == 2) {
1650 for (i = 0; i < 2; i++)
1651 spec->private_dac_nids[spec->multiout.num_dacs++] =
1652 spec->multi_io[i].dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001653 } else if (spec->multi_ios) {
1654 spec->multi_ios = 0;
1655 badness += BAD_MULTI_IO;
1656 }
1657
Takashi Iwai55a63d42013-03-21 17:20:12 +01001658 if (spec->indep_hp && !indep_hp_possible(codec))
1659 badness += BAD_NO_INDEP_HP;
1660
Takashi Iwaia07a9492013-01-07 16:44:06 +01001661 /* re-fill the shared DAC for speaker / headphone */
1662 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1663 refill_shared_dacs(codec, cfg->hp_outs,
1664 spec->multiout.hp_out_nid,
1665 spec->hp_paths);
1666 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1667 refill_shared_dacs(codec, cfg->speaker_outs,
1668 spec->multiout.extra_out_nid,
1669 spec->speaker_paths);
1670
Takashi Iwai352f7f92012-12-19 12:52:06 +01001671 return badness;
1672}
1673
1674#define DEBUG_BADNESS
1675
1676#ifdef DEBUG_BADNESS
1677#define debug_badness snd_printdd
1678#else
1679#define debug_badness(...)
1680#endif
1681
Takashi Iwaia7694092013-01-21 10:43:18 +01001682#ifdef DEBUG_BADNESS
1683static inline void print_nid_path_idx(struct hda_codec *codec,
1684 const char *pfx, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001685{
Takashi Iwaia7694092013-01-21 10:43:18 +01001686 struct nid_path *path;
1687
1688 path = snd_hda_get_path_from_idx(codec, idx);
1689 if (path)
1690 print_nid_path(pfx, path);
1691}
1692
1693static void debug_show_configs(struct hda_codec *codec,
1694 struct auto_pin_cfg *cfg)
1695{
1696 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia7694092013-01-21 10:43:18 +01001697 static const char * const lo_type[3] = { "LO", "SP", "HP" };
Takashi Iwaia7694092013-01-21 10:43:18 +01001698 int i;
1699
1700 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001701 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001702 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001703 spec->multiout.dac_nids[0],
1704 spec->multiout.dac_nids[1],
1705 spec->multiout.dac_nids[2],
Takashi Iwaia7694092013-01-21 10:43:18 +01001706 spec->multiout.dac_nids[3],
1707 lo_type[cfg->line_out_type]);
1708 for (i = 0; i < cfg->line_outs; i++)
1709 print_nid_path_idx(codec, " out", spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001710 if (spec->multi_ios > 0)
1711 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1712 spec->multi_ios,
1713 spec->multi_io[0].pin, spec->multi_io[1].pin,
1714 spec->multi_io[0].dac, spec->multi_io[1].dac);
Takashi Iwaia7694092013-01-21 10:43:18 +01001715 for (i = 0; i < spec->multi_ios; i++)
1716 print_nid_path_idx(codec, " mio",
1717 spec->out_paths[cfg->line_outs + i]);
1718 if (cfg->hp_outs)
1719 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001720 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001721 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001722 spec->multiout.hp_out_nid[0],
1723 spec->multiout.hp_out_nid[1],
1724 spec->multiout.hp_out_nid[2],
1725 spec->multiout.hp_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001726 for (i = 0; i < cfg->hp_outs; i++)
1727 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1728 if (cfg->speaker_outs)
1729 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001730 cfg->speaker_pins[0], cfg->speaker_pins[1],
1731 cfg->speaker_pins[2], cfg->speaker_pins[3],
1732 spec->multiout.extra_out_nid[0],
1733 spec->multiout.extra_out_nid[1],
1734 spec->multiout.extra_out_nid[2],
1735 spec->multiout.extra_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001736 for (i = 0; i < cfg->speaker_outs; i++)
1737 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1738 for (i = 0; i < 3; i++)
1739 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001740}
Takashi Iwaia7694092013-01-21 10:43:18 +01001741#else
1742#define debug_show_configs(codec, cfg) /* NOP */
1743#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01001744
1745/* find all available DACs of the codec */
1746static void fill_all_dac_nids(struct hda_codec *codec)
1747{
1748 struct hda_gen_spec *spec = codec->spec;
1749 int i;
1750 hda_nid_t nid = codec->start_nid;
1751
1752 spec->num_all_dacs = 0;
1753 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1754 for (i = 0; i < codec->num_nodes; i++, nid++) {
1755 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1756 continue;
1757 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1758 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1759 break;
1760 }
1761 spec->all_dacs[spec->num_all_dacs++] = nid;
1762 }
1763}
1764
1765static int parse_output_paths(struct hda_codec *codec)
1766{
1767 struct hda_gen_spec *spec = codec->spec;
1768 struct auto_pin_cfg *cfg = &spec->autocfg;
1769 struct auto_pin_cfg *best_cfg;
Takashi Iwai9314a582013-01-21 10:49:05 +01001770 unsigned int val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001771 int best_badness = INT_MAX;
1772 int badness;
1773 bool fill_hardwired = true, fill_mio_first = true;
1774 bool best_wired = true, best_mio = true;
1775 bool hp_spk_swapped = false;
1776
Takashi Iwai352f7f92012-12-19 12:52:06 +01001777 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1778 if (!best_cfg)
1779 return -ENOMEM;
1780 *best_cfg = *cfg;
1781
1782 for (;;) {
1783 badness = fill_and_eval_dacs(codec, fill_hardwired,
1784 fill_mio_first);
1785 if (badness < 0) {
1786 kfree(best_cfg);
1787 return badness;
1788 }
1789 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1790 cfg->line_out_type, fill_hardwired, fill_mio_first,
1791 badness);
Takashi Iwaia7694092013-01-21 10:43:18 +01001792 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001793 if (badness < best_badness) {
1794 best_badness = badness;
1795 *best_cfg = *cfg;
1796 best_wired = fill_hardwired;
1797 best_mio = fill_mio_first;
1798 }
1799 if (!badness)
1800 break;
1801 fill_mio_first = !fill_mio_first;
1802 if (!fill_mio_first)
1803 continue;
1804 fill_hardwired = !fill_hardwired;
1805 if (!fill_hardwired)
1806 continue;
1807 if (hp_spk_swapped)
1808 break;
1809 hp_spk_swapped = true;
1810 if (cfg->speaker_outs > 0 &&
1811 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1812 cfg->hp_outs = cfg->line_outs;
1813 memcpy(cfg->hp_pins, cfg->line_out_pins,
1814 sizeof(cfg->hp_pins));
1815 cfg->line_outs = cfg->speaker_outs;
1816 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1817 sizeof(cfg->speaker_pins));
1818 cfg->speaker_outs = 0;
1819 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1820 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1821 fill_hardwired = true;
1822 continue;
1823 }
1824 if (cfg->hp_outs > 0 &&
1825 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1826 cfg->speaker_outs = cfg->line_outs;
1827 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1828 sizeof(cfg->speaker_pins));
1829 cfg->line_outs = cfg->hp_outs;
1830 memcpy(cfg->line_out_pins, cfg->hp_pins,
1831 sizeof(cfg->hp_pins));
1832 cfg->hp_outs = 0;
1833 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1834 cfg->line_out_type = AUTO_PIN_HP_OUT;
1835 fill_hardwired = true;
1836 continue;
1837 }
1838 break;
1839 }
1840
1841 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001842 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001843 *cfg = *best_cfg;
1844 fill_and_eval_dacs(codec, best_wired, best_mio);
1845 }
1846 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1847 cfg->line_out_type, best_wired, best_mio);
Takashi Iwaia7694092013-01-21 10:43:18 +01001848 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001849
1850 if (cfg->line_out_pins[0]) {
1851 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01001852 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001853 if (path)
1854 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01001855 if (spec->vmaster_nid)
1856 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
1857 HDA_OUTPUT, spec->vmaster_tlv);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001858 }
1859
Takashi Iwai9314a582013-01-21 10:49:05 +01001860 /* set initial pinctl targets */
1861 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
1862 val = PIN_HP;
1863 else
1864 val = PIN_OUT;
1865 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
1866 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1867 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
1868 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1869 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
1870 set_pin_targets(codec, cfg->speaker_outs,
1871 cfg->speaker_pins, val);
1872 }
1873
Takashi Iwai55a63d42013-03-21 17:20:12 +01001874 /* clear indep_hp flag if not available */
1875 if (spec->indep_hp && !indep_hp_possible(codec))
1876 spec->indep_hp = 0;
1877
Takashi Iwai352f7f92012-12-19 12:52:06 +01001878 kfree(best_cfg);
1879 return 0;
1880}
1881
1882/* add playback controls from the parsed DAC table */
1883static int create_multi_out_ctls(struct hda_codec *codec,
1884 const struct auto_pin_cfg *cfg)
1885{
1886 struct hda_gen_spec *spec = codec->spec;
1887 int i, err, noutputs;
1888
1889 noutputs = cfg->line_outs;
1890 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1891 noutputs += spec->multi_ios;
1892
1893 for (i = 0; i < noutputs; i++) {
1894 const char *name;
1895 int index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001896 struct nid_path *path;
1897
Takashi Iwai196c17662013-01-04 15:01:40 +01001898 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001899 if (!path)
1900 continue;
Takashi Iwai247d85e2013-01-17 16:18:11 +01001901
1902 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001903 if (!name || !strcmp(name, "CLFE")) {
1904 /* Center/LFE */
1905 err = add_vol_ctl(codec, "Center", 0, 1, path);
1906 if (err < 0)
1907 return err;
1908 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1909 if (err < 0)
1910 return err;
Takashi Iwai247d85e2013-01-17 16:18:11 +01001911 } else {
1912 err = add_stereo_vol(codec, name, index, path);
1913 if (err < 0)
1914 return err;
1915 }
1916
1917 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
1918 if (!name || !strcmp(name, "CLFE")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001919 err = add_sw_ctl(codec, "Center", 0, 1, path);
1920 if (err < 0)
1921 return err;
1922 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1923 if (err < 0)
1924 return err;
1925 } else {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001926 err = add_stereo_sw(codec, name, index, path);
1927 if (err < 0)
1928 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 }
1930 }
1931 return 0;
1932}
1933
Takashi Iwaic2c80382013-01-07 10:33:57 +01001934static int create_extra_out(struct hda_codec *codec, int path_idx,
Takashi Iwai196c17662013-01-04 15:01:40 +01001935 const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001937 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 int err;
1939
Takashi Iwai196c17662013-01-04 15:01:40 +01001940 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001941 if (!path)
1942 return 0;
Takashi Iwaic2c80382013-01-07 10:33:57 +01001943 err = add_stereo_vol(codec, pfx, cidx, path);
1944 if (err < 0)
1945 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001946 err = add_stereo_sw(codec, pfx, cidx, path);
1947 if (err < 0)
1948 return err;
1949 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950}
1951
Takashi Iwai352f7f92012-12-19 12:52:06 +01001952/* add playback controls for speaker and HP outputs */
1953static int create_extra_outs(struct hda_codec *codec, int num_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001954 const int *paths, const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955{
Takashi Iwaic2c80382013-01-07 10:33:57 +01001956 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001957
1958 for (i = 0; i < num_pins; i++) {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001959 const char *name;
Takashi Iwai975cc022013-06-28 11:56:49 +02001960 char tmp[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwaic2c80382013-01-07 10:33:57 +01001961 int err, idx = 0;
1962
1963 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
1964 name = "Bass Speaker";
1965 else if (num_pins >= 3) {
1966 snprintf(tmp, sizeof(tmp), "%s %s",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001967 pfx, channel_name[i]);
Takashi Iwaic2c80382013-01-07 10:33:57 +01001968 name = tmp;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001969 } else {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001970 name = pfx;
1971 idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 }
Takashi Iwaic2c80382013-01-07 10:33:57 +01001973 err = create_extra_out(codec, paths[i], name, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001974 if (err < 0)
1975 return err;
1976 }
1977 return 0;
1978}
Takashi Iwai97ec5582006-03-21 11:29:07 +01001979
Takashi Iwai352f7f92012-12-19 12:52:06 +01001980static int create_hp_out_ctls(struct hda_codec *codec)
1981{
1982 struct hda_gen_spec *spec = codec->spec;
1983 return create_extra_outs(codec, spec->autocfg.hp_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001984 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001985 "Headphone");
1986}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
Takashi Iwai352f7f92012-12-19 12:52:06 +01001988static int create_speaker_out_ctls(struct hda_codec *codec)
1989{
1990 struct hda_gen_spec *spec = codec->spec;
1991 return create_extra_outs(codec, spec->autocfg.speaker_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001992 spec->speaker_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001993 "Speaker");
1994}
1995
1996/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001997 * independent HP controls
1998 */
1999
Takashi Iwai963afde2013-05-31 15:20:31 +02002000static void call_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002001static int indep_hp_info(struct snd_kcontrol *kcontrol,
2002 struct snd_ctl_elem_info *uinfo)
2003{
2004 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2005}
2006
2007static int indep_hp_get(struct snd_kcontrol *kcontrol,
2008 struct snd_ctl_elem_value *ucontrol)
2009{
2010 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2011 struct hda_gen_spec *spec = codec->spec;
2012 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
2013 return 0;
2014}
2015
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002016static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2017 int nomix_path_idx, int mix_path_idx,
2018 int out_type);
2019
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002020static int indep_hp_put(struct snd_kcontrol *kcontrol,
2021 struct snd_ctl_elem_value *ucontrol)
2022{
2023 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2024 struct hda_gen_spec *spec = codec->spec;
2025 unsigned int select = ucontrol->value.enumerated.item[0];
2026 int ret = 0;
2027
2028 mutex_lock(&spec->pcm_mutex);
2029 if (spec->active_streams) {
2030 ret = -EBUSY;
2031 goto unlock;
2032 }
2033
2034 if (spec->indep_hp_enabled != select) {
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002035 hda_nid_t *dacp;
2036 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2037 dacp = &spec->private_dac_nids[0];
2038 else
2039 dacp = &spec->multiout.hp_out_nid[0];
2040
2041 /* update HP aamix paths in case it conflicts with indep HP */
2042 if (spec->have_aamix_ctl) {
2043 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2044 update_aamix_paths(codec, spec->aamix_mode,
2045 spec->out_paths[0],
2046 spec->aamix_out_paths[0],
2047 spec->autocfg.line_out_type);
2048 else
2049 update_aamix_paths(codec, spec->aamix_mode,
2050 spec->hp_paths[0],
2051 spec->aamix_out_paths[1],
2052 AUTO_PIN_HP_OUT);
2053 }
2054
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002055 spec->indep_hp_enabled = select;
2056 if (spec->indep_hp_enabled)
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002057 *dacp = 0;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002058 else
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002059 *dacp = spec->alt_dac_nid;
Takashi Iwai92603c52013-01-22 07:46:31 +01002060
Takashi Iwai963afde2013-05-31 15:20:31 +02002061 call_hp_automute(codec, NULL);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002062 ret = 1;
2063 }
2064 unlock:
2065 mutex_unlock(&spec->pcm_mutex);
2066 return ret;
2067}
2068
2069static const struct snd_kcontrol_new indep_hp_ctl = {
2070 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2071 .name = "Independent HP",
2072 .info = indep_hp_info,
2073 .get = indep_hp_get,
2074 .put = indep_hp_put,
2075};
2076
2077
2078static int create_indep_hp_ctls(struct hda_codec *codec)
2079{
2080 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002081 hda_nid_t dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002082
2083 if (!spec->indep_hp)
2084 return 0;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002085 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2086 dac = spec->multiout.dac_nids[0];
2087 else
2088 dac = spec->multiout.hp_out_nid[0];
2089 if (!dac) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002090 spec->indep_hp = 0;
2091 return 0;
2092 }
2093
2094 spec->indep_hp_enabled = false;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002095 spec->alt_dac_nid = dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002096 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
2097 return -ENOMEM;
2098 return 0;
2099}
2100
2101/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002102 * channel mode enum control
2103 */
2104
2105static int ch_mode_info(struct snd_kcontrol *kcontrol,
2106 struct snd_ctl_elem_info *uinfo)
2107{
2108 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2109 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002110 int chs;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002111
2112 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2113 uinfo->count = 1;
2114 uinfo->value.enumerated.items = spec->multi_ios + 1;
2115 if (uinfo->value.enumerated.item > spec->multi_ios)
2116 uinfo->value.enumerated.item = spec->multi_ios;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002117 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2118 sprintf(uinfo->value.enumerated.name, "%dch", chs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002119 return 0;
2120}
2121
2122static int ch_mode_get(struct snd_kcontrol *kcontrol,
2123 struct snd_ctl_elem_value *ucontrol)
2124{
2125 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2126 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002127 ucontrol->value.enumerated.item[0] =
2128 (spec->ext_channel_count - spec->min_channel_count) / 2;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002129 return 0;
2130}
2131
Takashi Iwai196c17662013-01-04 15:01:40 +01002132static inline struct nid_path *
2133get_multiio_path(struct hda_codec *codec, int idx)
2134{
2135 struct hda_gen_spec *spec = codec->spec;
2136 return snd_hda_get_path_from_idx(codec,
2137 spec->out_paths[spec->autocfg.line_outs + idx]);
2138}
2139
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002140static void update_automute_all(struct hda_codec *codec);
2141
Takashi Iwai65033cc2013-04-16 12:31:05 +02002142/* Default value to be passed as aamix argument for snd_hda_activate_path();
2143 * used for output paths
2144 */
2145static bool aamix_default(struct hda_gen_spec *spec)
2146{
2147 return !spec->have_aamix_ctl || spec->aamix_mode;
2148}
2149
Takashi Iwai352f7f92012-12-19 12:52:06 +01002150static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2151{
2152 struct hda_gen_spec *spec = codec->spec;
2153 hda_nid_t nid = spec->multi_io[idx].pin;
2154 struct nid_path *path;
2155
Takashi Iwai196c17662013-01-04 15:01:40 +01002156 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002157 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002159
2160 if (path->active == output)
2161 return 0;
2162
2163 if (output) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01002164 set_pin_target(codec, nid, PIN_OUT, true);
Takashi Iwai65033cc2013-04-16 12:31:05 +02002165 snd_hda_activate_path(codec, path, true, aamix_default(spec));
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002166 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002167 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002168 set_pin_eapd(codec, nid, false);
Takashi Iwai65033cc2013-04-16 12:31:05 +02002169 snd_hda_activate_path(codec, path, false, aamix_default(spec));
Takashi Iwai2c12c302013-01-10 09:33:29 +01002170 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002171 path_power_down_sync(codec, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 }
Takashi Iwaia365fed2013-01-10 16:10:06 +01002173
2174 /* update jack retasking in case it modifies any of them */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002175 update_automute_all(codec);
Takashi Iwaia365fed2013-01-10 16:10:06 +01002176
Takashi Iwai352f7f92012-12-19 12:52:06 +01002177 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178}
2179
Takashi Iwai352f7f92012-12-19 12:52:06 +01002180static int ch_mode_put(struct snd_kcontrol *kcontrol,
2181 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002183 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2184 struct hda_gen_spec *spec = codec->spec;
2185 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
Takashi Iwai352f7f92012-12-19 12:52:06 +01002187 ch = ucontrol->value.enumerated.item[0];
2188 if (ch < 0 || ch > spec->multi_ios)
2189 return -EINVAL;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002190 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002191 return 0;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002192 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002193 for (i = 0; i < spec->multi_ios; i++)
2194 set_multi_io(codec, i, i < ch);
2195 spec->multiout.max_channels = max(spec->ext_channel_count,
2196 spec->const_channel_count);
2197 if (spec->need_dac_fix)
2198 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 return 1;
2200}
2201
Takashi Iwai352f7f92012-12-19 12:52:06 +01002202static const struct snd_kcontrol_new channel_mode_enum = {
2203 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2204 .name = "Channel Mode",
2205 .info = ch_mode_info,
2206 .get = ch_mode_get,
2207 .put = ch_mode_put,
2208};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
Takashi Iwai352f7f92012-12-19 12:52:06 +01002210static int create_multi_channel_mode(struct hda_codec *codec)
2211{
2212 struct hda_gen_spec *spec = codec->spec;
2213
2214 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002215 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002216 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 return 0;
2219}
2220
Takashi Iwai352f7f92012-12-19 12:52:06 +01002221/*
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002222 * aamix loopback enable/disable switch
2223 */
2224
2225#define loopback_mixing_info indep_hp_info
2226
2227static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2228 struct snd_ctl_elem_value *ucontrol)
2229{
2230 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2231 struct hda_gen_spec *spec = codec->spec;
2232 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2233 return 0;
2234}
2235
2236static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002237 int nomix_path_idx, int mix_path_idx,
2238 int out_type)
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002239{
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002240 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002241 struct nid_path *nomix_path, *mix_path;
2242
2243 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2244 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2245 if (!nomix_path || !mix_path)
2246 return;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002247
2248 /* if HP aamix path is driven from a different DAC and the
2249 * independent HP mode is ON, can't turn on aamix path
2250 */
2251 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2252 mix_path->path[0] != spec->alt_dac_nid)
2253 do_mix = false;
2254
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002255 if (do_mix) {
2256 snd_hda_activate_path(codec, nomix_path, false, true);
2257 snd_hda_activate_path(codec, mix_path, true, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002258 path_power_down_sync(codec, nomix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002259 } else {
Takashi Iwai65033cc2013-04-16 12:31:05 +02002260 snd_hda_activate_path(codec, mix_path, false, false);
2261 snd_hda_activate_path(codec, nomix_path, true, false);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002262 path_power_down_sync(codec, mix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002263 }
2264}
2265
2266static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2267 struct snd_ctl_elem_value *ucontrol)
2268{
2269 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2270 struct hda_gen_spec *spec = codec->spec;
2271 unsigned int val = ucontrol->value.enumerated.item[0];
2272
2273 if (val == spec->aamix_mode)
2274 return 0;
2275 spec->aamix_mode = val;
2276 update_aamix_paths(codec, val, spec->out_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002277 spec->aamix_out_paths[0],
2278 spec->autocfg.line_out_type);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002279 update_aamix_paths(codec, val, spec->hp_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002280 spec->aamix_out_paths[1],
2281 AUTO_PIN_HP_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002282 update_aamix_paths(codec, val, spec->speaker_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002283 spec->aamix_out_paths[2],
2284 AUTO_PIN_SPEAKER_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002285 return 1;
2286}
2287
2288static const struct snd_kcontrol_new loopback_mixing_enum = {
2289 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2290 .name = "Loopback Mixing",
2291 .info = loopback_mixing_info,
2292 .get = loopback_mixing_get,
2293 .put = loopback_mixing_put,
2294};
2295
2296static int create_loopback_mixing_ctl(struct hda_codec *codec)
2297{
2298 struct hda_gen_spec *spec = codec->spec;
2299
2300 if (!spec->mixer_nid)
2301 return 0;
2302 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
2303 spec->aamix_out_paths[2]))
2304 return 0;
2305 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2306 return -ENOMEM;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002307 spec->have_aamix_ctl = 1;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002308 return 0;
2309}
2310
2311/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002312 * shared headphone/mic handling
2313 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002314
Takashi Iwai352f7f92012-12-19 12:52:06 +01002315static void call_update_outputs(struct hda_codec *codec);
2316
2317/* for shared I/O, change the pin-control accordingly */
Takashi Iwai967303d2013-02-19 17:12:42 +01002318static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002319{
2320 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai967303d2013-02-19 17:12:42 +01002321 bool as_mic;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002322 unsigned int val;
Takashi Iwai967303d2013-02-19 17:12:42 +01002323 hda_nid_t pin;
2324
2325 pin = spec->hp_mic_pin;
2326 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
2327
2328 if (!force) {
2329 val = snd_hda_codec_get_pin_target(codec, pin);
2330 if (as_mic) {
2331 if (val & PIN_IN)
2332 return;
2333 } else {
2334 if (val & PIN_OUT)
2335 return;
2336 }
2337 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002338
2339 val = snd_hda_get_default_vref(codec, pin);
Takashi Iwai967303d2013-02-19 17:12:42 +01002340 /* if the HP pin doesn't support VREF and the codec driver gives an
2341 * alternative pin, set up the VREF on that pin instead
2342 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002343 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2344 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2345 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2346 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01002347 snd_hda_set_pin_ctl_cache(codec, vref_pin,
Takashi Iwai967303d2013-02-19 17:12:42 +01002348 PIN_IN | (as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02002349 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002350
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002351 if (!spec->hp_mic_jack_modes) {
2352 if (as_mic)
2353 val |= PIN_IN;
2354 else
2355 val = PIN_HP;
2356 set_pin_target(codec, pin, val, true);
Takashi Iwai963afde2013-05-31 15:20:31 +02002357 call_hp_automute(codec, NULL);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002358 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002359}
2360
2361/* create a shared input with the headphone out */
Takashi Iwai967303d2013-02-19 17:12:42 +01002362static int create_hp_mic(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002363{
2364 struct hda_gen_spec *spec = codec->spec;
2365 struct auto_pin_cfg *cfg = &spec->autocfg;
2366 unsigned int defcfg;
2367 hda_nid_t nid;
2368
Takashi Iwai967303d2013-02-19 17:12:42 +01002369 if (!spec->hp_mic) {
2370 if (spec->suppress_hp_mic_detect)
2371 return 0;
2372 /* automatic detection: only if no input or a single internal
2373 * input pin is found, try to detect the shared hp/mic
2374 */
2375 if (cfg->num_inputs > 1)
2376 return 0;
2377 else if (cfg->num_inputs == 1) {
2378 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2379 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2380 return 0;
2381 }
2382 }
2383
2384 spec->hp_mic = 0; /* clear once */
2385 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002386 return 0;
2387
Takashi Iwai967303d2013-02-19 17:12:42 +01002388 nid = 0;
2389 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2390 nid = cfg->line_out_pins[0];
2391 else if (cfg->hp_outs > 0)
2392 nid = cfg->hp_pins[0];
2393 if (!nid)
2394 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002395
2396 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2397 return 0; /* no input */
2398
Takashi Iwai967303d2013-02-19 17:12:42 +01002399 cfg->inputs[cfg->num_inputs].pin = nid;
2400 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
David Henningssoncb420b12013-04-11 11:30:28 +02002401 cfg->inputs[cfg->num_inputs].is_headphone_mic = 1;
Takashi Iwai967303d2013-02-19 17:12:42 +01002402 cfg->num_inputs++;
2403 spec->hp_mic = 1;
2404 spec->hp_mic_pin = nid;
2405 /* we can't handle auto-mic together with HP-mic */
2406 spec->suppress_auto_mic = 1;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002407 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
2408 return 0;
2409}
2410
Takashi Iwai978e77e2013-01-10 16:57:58 +01002411/*
2412 * output jack mode
2413 */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002414
2415static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2416
2417static const char * const out_jack_texts[] = {
2418 "Line Out", "Headphone Out",
2419};
2420
Takashi Iwai978e77e2013-01-10 16:57:58 +01002421static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2422 struct snd_ctl_elem_info *uinfo)
2423{
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002424 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
Takashi Iwai978e77e2013-01-10 16:57:58 +01002425}
2426
2427static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2428 struct snd_ctl_elem_value *ucontrol)
2429{
2430 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2431 hda_nid_t nid = kcontrol->private_value;
2432 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2433 ucontrol->value.enumerated.item[0] = 1;
2434 else
2435 ucontrol->value.enumerated.item[0] = 0;
2436 return 0;
2437}
2438
2439static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2440 struct snd_ctl_elem_value *ucontrol)
2441{
2442 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2443 hda_nid_t nid = kcontrol->private_value;
2444 unsigned int val;
2445
2446 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2447 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2448 return 0;
2449 snd_hda_set_pin_ctl_cache(codec, nid, val);
2450 return 1;
2451}
2452
2453static const struct snd_kcontrol_new out_jack_mode_enum = {
2454 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2455 .info = out_jack_mode_info,
2456 .get = out_jack_mode_get,
2457 .put = out_jack_mode_put,
2458};
2459
2460static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2461{
2462 struct hda_gen_spec *spec = codec->spec;
2463 int i;
2464
2465 for (i = 0; i < spec->kctls.used; i++) {
2466 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2467 if (!strcmp(kctl->name, name) && kctl->index == idx)
2468 return true;
2469 }
2470 return false;
2471}
2472
2473static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2474 char *name, size_t name_len)
2475{
2476 struct hda_gen_spec *spec = codec->spec;
2477 int idx = 0;
2478
2479 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2480 strlcat(name, " Jack Mode", name_len);
2481
2482 for (; find_kctl_name(codec, name, idx); idx++)
2483 ;
2484}
2485
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002486static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2487{
2488 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002489 if (spec->add_jack_modes) {
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002490 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2491 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2492 return 2;
2493 }
2494 return 1;
2495}
2496
Takashi Iwai978e77e2013-01-10 16:57:58 +01002497static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2498 hda_nid_t *pins)
2499{
2500 struct hda_gen_spec *spec = codec->spec;
2501 int i;
2502
2503 for (i = 0; i < num_pins; i++) {
2504 hda_nid_t pin = pins[i];
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002505 if (pin == spec->hp_mic_pin) {
2506 int ret = create_hp_mic_jack_mode(codec, pin);
2507 if (ret < 0)
2508 return ret;
2509 continue;
2510 }
2511 if (get_out_jack_num_items(codec, pin) > 1) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01002512 struct snd_kcontrol_new *knew;
Takashi Iwai975cc022013-06-28 11:56:49 +02002513 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai978e77e2013-01-10 16:57:58 +01002514 get_jack_mode_name(codec, pin, name, sizeof(name));
2515 knew = snd_hda_gen_add_kctl(spec, name,
2516 &out_jack_mode_enum);
2517 if (!knew)
2518 return -ENOMEM;
2519 knew->private_value = pin;
2520 }
2521 }
2522
2523 return 0;
2524}
2525
Takashi Iwai294765582013-01-17 09:52:11 +01002526/*
2527 * input jack mode
2528 */
2529
2530/* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2531#define NUM_VREFS 6
2532
2533static const char * const vref_texts[NUM_VREFS] = {
2534 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2535 "", "Mic 80pc Bias", "Mic 100pc Bias"
2536};
2537
2538static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2539{
2540 unsigned int pincap;
2541
2542 pincap = snd_hda_query_pin_caps(codec, pin);
2543 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2544 /* filter out unusual vrefs */
2545 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2546 return pincap;
2547}
2548
2549/* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2550static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2551{
2552 unsigned int i, n = 0;
2553
2554 for (i = 0; i < NUM_VREFS; i++) {
2555 if (vref_caps & (1 << i)) {
2556 if (n == item_idx)
2557 return i;
2558 n++;
2559 }
2560 }
2561 return 0;
2562}
2563
2564/* convert back from the vref ctl index to the enum item index */
2565static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2566{
2567 unsigned int i, n = 0;
2568
2569 for (i = 0; i < NUM_VREFS; i++) {
2570 if (i == idx)
2571 return n;
2572 if (vref_caps & (1 << i))
2573 n++;
2574 }
2575 return 0;
2576}
2577
2578static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2579 struct snd_ctl_elem_info *uinfo)
2580{
2581 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2582 hda_nid_t nid = kcontrol->private_value;
2583 unsigned int vref_caps = get_vref_caps(codec, nid);
2584
2585 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2586 vref_texts);
2587 /* set the right text */
2588 strcpy(uinfo->value.enumerated.name,
2589 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2590 return 0;
2591}
2592
2593static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2594 struct snd_ctl_elem_value *ucontrol)
2595{
2596 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2597 hda_nid_t nid = kcontrol->private_value;
2598 unsigned int vref_caps = get_vref_caps(codec, nid);
2599 unsigned int idx;
2600
2601 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2602 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2603 return 0;
2604}
2605
2606static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2607 struct snd_ctl_elem_value *ucontrol)
2608{
2609 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2610 hda_nid_t nid = kcontrol->private_value;
2611 unsigned int vref_caps = get_vref_caps(codec, nid);
2612 unsigned int val, idx;
2613
2614 val = snd_hda_codec_get_pin_target(codec, nid);
2615 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2616 if (idx == ucontrol->value.enumerated.item[0])
2617 return 0;
2618
2619 val &= ~AC_PINCTL_VREFEN;
2620 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2621 snd_hda_set_pin_ctl_cache(codec, nid, val);
2622 return 1;
2623}
2624
2625static const struct snd_kcontrol_new in_jack_mode_enum = {
2626 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2627 .info = in_jack_mode_info,
2628 .get = in_jack_mode_get,
2629 .put = in_jack_mode_put,
2630};
2631
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002632static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2633{
2634 struct hda_gen_spec *spec = codec->spec;
2635 int nitems = 0;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002636 if (spec->add_jack_modes)
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002637 nitems = hweight32(get_vref_caps(codec, pin));
2638 return nitems ? nitems : 1;
2639}
2640
Takashi Iwai294765582013-01-17 09:52:11 +01002641static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2642{
2643 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai294765582013-01-17 09:52:11 +01002644 struct snd_kcontrol_new *knew;
Takashi Iwai975cc022013-06-28 11:56:49 +02002645 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002646 unsigned int defcfg;
2647
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002648 if (pin == spec->hp_mic_pin)
2649 return 0; /* already done in create_out_jack_mode() */
Takashi Iwai294765582013-01-17 09:52:11 +01002650
2651 /* no jack mode for fixed pins */
2652 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2653 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2654 return 0;
2655
2656 /* no multiple vref caps? */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002657 if (get_in_jack_num_items(codec, pin) <= 1)
Takashi Iwai294765582013-01-17 09:52:11 +01002658 return 0;
2659
2660 get_jack_mode_name(codec, pin, name, sizeof(name));
2661 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2662 if (!knew)
2663 return -ENOMEM;
2664 knew->private_value = pin;
2665 return 0;
2666}
2667
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002668/*
2669 * HP/mic shared jack mode
2670 */
2671static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2672 struct snd_ctl_elem_info *uinfo)
2673{
2674 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2675 hda_nid_t nid = kcontrol->private_value;
2676 int out_jacks = get_out_jack_num_items(codec, nid);
2677 int in_jacks = get_in_jack_num_items(codec, nid);
2678 const char *text = NULL;
2679 int idx;
2680
2681 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2682 uinfo->count = 1;
2683 uinfo->value.enumerated.items = out_jacks + in_jacks;
2684 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2685 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2686 idx = uinfo->value.enumerated.item;
2687 if (idx < out_jacks) {
2688 if (out_jacks > 1)
2689 text = out_jack_texts[idx];
2690 else
2691 text = "Headphone Out";
2692 } else {
2693 idx -= out_jacks;
2694 if (in_jacks > 1) {
2695 unsigned int vref_caps = get_vref_caps(codec, nid);
2696 text = vref_texts[get_vref_idx(vref_caps, idx)];
2697 } else
2698 text = "Mic In";
2699 }
2700
2701 strcpy(uinfo->value.enumerated.name, text);
2702 return 0;
2703}
2704
2705static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2706{
2707 int out_jacks = get_out_jack_num_items(codec, nid);
2708 int in_jacks = get_in_jack_num_items(codec, nid);
2709 unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2710 int idx = 0;
2711
2712 if (val & PIN_OUT) {
2713 if (out_jacks > 1 && val == PIN_HP)
2714 idx = 1;
2715 } else if (val & PIN_IN) {
2716 idx = out_jacks;
2717 if (in_jacks > 1) {
2718 unsigned int vref_caps = get_vref_caps(codec, nid);
2719 val &= AC_PINCTL_VREFEN;
2720 idx += cvt_from_vref_idx(vref_caps, val);
2721 }
2722 }
2723 return idx;
2724}
2725
2726static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2727 struct snd_ctl_elem_value *ucontrol)
2728{
2729 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2730 hda_nid_t nid = kcontrol->private_value;
2731 ucontrol->value.enumerated.item[0] =
2732 get_cur_hp_mic_jack_mode(codec, nid);
2733 return 0;
2734}
2735
2736static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2737 struct snd_ctl_elem_value *ucontrol)
2738{
2739 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2740 hda_nid_t nid = kcontrol->private_value;
2741 int out_jacks = get_out_jack_num_items(codec, nid);
2742 int in_jacks = get_in_jack_num_items(codec, nid);
2743 unsigned int val, oldval, idx;
2744
2745 oldval = get_cur_hp_mic_jack_mode(codec, nid);
2746 idx = ucontrol->value.enumerated.item[0];
2747 if (oldval == idx)
2748 return 0;
2749
2750 if (idx < out_jacks) {
2751 if (out_jacks > 1)
2752 val = idx ? PIN_HP : PIN_OUT;
2753 else
2754 val = PIN_HP;
2755 } else {
2756 idx -= out_jacks;
2757 if (in_jacks > 1) {
2758 unsigned int vref_caps = get_vref_caps(codec, nid);
2759 val = snd_hda_codec_get_pin_target(codec, nid);
Takashi Iwai3f550e32013-03-07 18:30:27 +01002760 val &= ~(AC_PINCTL_VREFEN | PIN_HP);
2761 val |= get_vref_idx(vref_caps, idx) | PIN_IN;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002762 } else
2763 val = snd_hda_get_default_vref(codec, nid);
2764 }
2765 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwai963afde2013-05-31 15:20:31 +02002766 call_hp_automute(codec, NULL);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002767
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002768 return 1;
2769}
2770
2771static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
2772 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2773 .info = hp_mic_jack_mode_info,
2774 .get = hp_mic_jack_mode_get,
2775 .put = hp_mic_jack_mode_put,
2776};
2777
2778static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2779{
2780 struct hda_gen_spec *spec = codec->spec;
2781 struct snd_kcontrol_new *knew;
2782
2783 if (get_out_jack_num_items(codec, pin) <= 1 &&
2784 get_in_jack_num_items(codec, pin) <= 1)
2785 return 0; /* no need */
2786 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
2787 &hp_mic_jack_mode_enum);
2788 if (!knew)
2789 return -ENOMEM;
2790 knew->private_value = pin;
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002791 spec->hp_mic_jack_modes = 1;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002792 return 0;
2793}
Takashi Iwai352f7f92012-12-19 12:52:06 +01002794
2795/*
2796 * Parse input paths
2797 */
2798
Takashi Iwai352f7f92012-12-19 12:52:06 +01002799/* add the powersave loopback-list entry */
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002800static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002801{
2802 struct hda_amp_list *list;
2803
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002804 list = snd_array_new(&spec->loopback_list);
2805 if (!list)
2806 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002807 list->nid = mix;
2808 list->dir = HDA_INPUT;
2809 list->idx = idx;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002810 spec->loopback.amplist = spec->loopback_list.list;
2811 return 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002812}
Takashi Iwaicb53c622007-08-10 17:21:45 +02002813
Takashi Iwai352f7f92012-12-19 12:52:06 +01002814/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01002815static int new_analog_input(struct hda_codec *codec, int input_idx,
2816 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002817 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002819 struct hda_gen_spec *spec = codec->spec;
2820 struct nid_path *path;
2821 unsigned int val;
2822 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823
Takashi Iwai352f7f92012-12-19 12:52:06 +01002824 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
2825 !nid_has_mute(codec, mix_nid, HDA_INPUT))
2826 return 0; /* no need for analog loopback */
2827
Takashi Iwai3ca529d2013-01-07 17:25:08 +01002828 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002829 if (!path)
2830 return -EINVAL;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002831 print_nid_path("loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01002832 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002833
2834 idx = path->idx[path->depth - 1];
2835 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
2836 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2837 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002838 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002840 path->ctls[NID_PATH_VOL_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 }
2842
Takashi Iwai352f7f92012-12-19 12:52:06 +01002843 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
2844 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2845 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002846 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002848 path->ctls[NID_PATH_MUTE_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 }
2850
Takashi Iwai352f7f92012-12-19 12:52:06 +01002851 path->active = true;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002852 err = add_loopback_list(spec, mix_nid, idx);
2853 if (err < 0)
2854 return err;
Takashi Iwaie4a395e2013-01-23 17:00:31 +01002855
2856 if (spec->mixer_nid != spec->mixer_merge_nid &&
2857 !spec->loopback_merge_path) {
2858 path = snd_hda_add_new_path(codec, spec->mixer_nid,
2859 spec->mixer_merge_nid, 0);
2860 if (path) {
2861 print_nid_path("loopback-merge", path);
2862 path->active = true;
2863 spec->loopback_merge_path =
2864 snd_hda_get_path_idx(codec, path);
2865 }
2866 }
2867
Takashi Iwai352f7f92012-12-19 12:52:06 +01002868 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869}
2870
Takashi Iwai352f7f92012-12-19 12:52:06 +01002871static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002873 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
2874 return (pincap & AC_PINCAP_IN) != 0;
2875}
2876
2877/* Parse the codec tree and retrieve ADCs */
2878static int fill_adc_nids(struct hda_codec *codec)
2879{
2880 struct hda_gen_spec *spec = codec->spec;
2881 hda_nid_t nid;
2882 hda_nid_t *adc_nids = spec->adc_nids;
2883 int max_nums = ARRAY_SIZE(spec->adc_nids);
2884 int i, nums = 0;
2885
2886 nid = codec->start_nid;
2887 for (i = 0; i < codec->num_nodes; i++, nid++) {
2888 unsigned int caps = get_wcaps(codec, nid);
2889 int type = get_wcaps_type(caps);
2890
2891 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2892 continue;
2893 adc_nids[nums] = nid;
2894 if (++nums >= max_nums)
2895 break;
2896 }
2897 spec->num_adc_nids = nums;
Takashi Iwai0ffd5342013-01-17 15:53:29 +01002898
2899 /* copy the detected ADCs to all_adcs[] */
2900 spec->num_all_adcs = nums;
2901 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
2902
Takashi Iwai352f7f92012-12-19 12:52:06 +01002903 return nums;
2904}
2905
2906/* filter out invalid adc_nids that don't give all active input pins;
2907 * if needed, check whether dynamic ADC-switching is available
2908 */
2909static int check_dyn_adc_switch(struct hda_codec *codec)
2910{
2911 struct hda_gen_spec *spec = codec->spec;
2912 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002913 unsigned int ok_bits;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002914 int i, n, nums;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002915
Takashi Iwai352f7f92012-12-19 12:52:06 +01002916 nums = 0;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002917 ok_bits = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002918 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002919 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002920 if (!spec->input_paths[i][n])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002921 break;
2922 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002923 if (i >= imux->num_items) {
2924 ok_bits |= (1 << n);
2925 nums++;
2926 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002927 }
2928
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002929 if (!ok_bits) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002930 /* check whether ADC-switch is possible */
2931 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002932 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002933 if (spec->input_paths[i][n]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002934 spec->dyn_adc_idx[i] = n;
2935 break;
2936 }
2937 }
2938 }
2939
2940 snd_printdd("hda-codec: enabling ADC switching\n");
2941 spec->dyn_adc_switch = 1;
2942 } else if (nums != spec->num_adc_nids) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002943 /* shrink the invalid adcs and input paths */
2944 nums = 0;
2945 for (n = 0; n < spec->num_adc_nids; n++) {
2946 if (!(ok_bits & (1 << n)))
2947 continue;
2948 if (n != nums) {
2949 spec->adc_nids[nums] = spec->adc_nids[n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002950 for (i = 0; i < imux->num_items; i++) {
2951 invalidate_nid_path(codec,
2952 spec->input_paths[i][nums]);
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002953 spec->input_paths[i][nums] =
2954 spec->input_paths[i][n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002955 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002956 }
2957 nums++;
2958 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002959 spec->num_adc_nids = nums;
2960 }
2961
Takashi Iwai967303d2013-02-19 17:12:42 +01002962 if (imux->num_items == 1 ||
2963 (imux->num_items == 2 && spec->hp_mic)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002964 snd_printdd("hda-codec: reducing to a single ADC\n");
2965 spec->num_adc_nids = 1; /* reduce to a single ADC */
2966 }
2967
2968 /* single index for individual volumes ctls */
2969 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
2970 spec->num_adc_nids = 1;
2971
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 return 0;
2973}
2974
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002975/* parse capture source paths from the given pin and create imux items */
2976static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
Takashi Iwai9dba2052013-01-18 10:01:15 +01002977 int cfg_idx, int num_adcs,
2978 const char *label, int anchor)
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002979{
2980 struct hda_gen_spec *spec = codec->spec;
2981 struct hda_input_mux *imux = &spec->input_mux;
2982 int imux_idx = imux->num_items;
2983 bool imux_added = false;
2984 int c;
2985
2986 for (c = 0; c < num_adcs; c++) {
2987 struct nid_path *path;
2988 hda_nid_t adc = spec->adc_nids[c];
2989
2990 if (!is_reachable_path(codec, pin, adc))
2991 continue;
2992 path = snd_hda_add_new_path(codec, pin, adc, anchor);
2993 if (!path)
2994 continue;
2995 print_nid_path("input", path);
2996 spec->input_paths[imux_idx][c] =
2997 snd_hda_get_path_idx(codec, path);
2998
2999 if (!imux_added) {
Takashi Iwai967303d2013-02-19 17:12:42 +01003000 if (spec->hp_mic_pin == pin)
3001 spec->hp_mic_mux_idx = imux->num_items;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003002 spec->imux_pins[imux->num_items] = pin;
Takashi Iwai9dba2052013-01-18 10:01:15 +01003003 snd_hda_add_imux_item(imux, label, cfg_idx, NULL);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003004 imux_added = true;
3005 }
3006 }
3007
3008 return 0;
3009}
3010
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003012 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 */
Takashi Iwai9dba2052013-01-18 10:01:15 +01003014
Takashi Iwaic9700422013-01-18 10:17:30 +01003015/* fill the label for each input at first */
3016static int fill_input_pin_labels(struct hda_codec *codec)
3017{
3018 struct hda_gen_spec *spec = codec->spec;
3019 const struct auto_pin_cfg *cfg = &spec->autocfg;
3020 int i;
3021
3022 for (i = 0; i < cfg->num_inputs; i++) {
3023 hda_nid_t pin = cfg->inputs[i].pin;
3024 const char *label;
3025 int j, idx;
3026
3027 if (!is_input_pin(codec, pin))
3028 continue;
3029
3030 label = hda_get_autocfg_input_label(codec, cfg, i);
3031 idx = 0;
David Henningsson8e8db7f2013-01-18 15:43:02 +01003032 for (j = i - 1; j >= 0; j--) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003033 if (spec->input_labels[j] &&
3034 !strcmp(spec->input_labels[j], label)) {
3035 idx = spec->input_label_idxs[j] + 1;
3036 break;
3037 }
3038 }
3039
3040 spec->input_labels[i] = label;
3041 spec->input_label_idxs[i] = idx;
3042 }
3043
3044 return 0;
3045}
3046
Takashi Iwai9dba2052013-01-18 10:01:15 +01003047#define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
3048
Takashi Iwai352f7f92012-12-19 12:52:06 +01003049static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003050{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003051 struct hda_gen_spec *spec = codec->spec;
3052 const struct auto_pin_cfg *cfg = &spec->autocfg;
3053 hda_nid_t mixer = spec->mixer_nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003054 int num_adcs;
Takashi Iwaic9700422013-01-18 10:17:30 +01003055 int i, err;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003056 unsigned int val;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003057
Takashi Iwai352f7f92012-12-19 12:52:06 +01003058 num_adcs = fill_adc_nids(codec);
3059 if (num_adcs < 0)
3060 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003061
Takashi Iwaic9700422013-01-18 10:17:30 +01003062 err = fill_input_pin_labels(codec);
3063 if (err < 0)
3064 return err;
3065
Takashi Iwai352f7f92012-12-19 12:52:06 +01003066 for (i = 0; i < cfg->num_inputs; i++) {
3067 hda_nid_t pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068
Takashi Iwai352f7f92012-12-19 12:52:06 +01003069 pin = cfg->inputs[i].pin;
3070 if (!is_input_pin(codec, pin))
3071 continue;
3072
Takashi Iwai2c12c302013-01-10 09:33:29 +01003073 val = PIN_IN;
3074 if (cfg->inputs[i].type == AUTO_PIN_MIC)
3075 val |= snd_hda_get_default_vref(codec, pin);
Takashi Iwai93c9d8a2013-03-11 09:48:43 +01003076 if (pin != spec->hp_mic_pin)
3077 set_pin_target(codec, pin, val, false);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003078
Takashi Iwai352f7f92012-12-19 12:52:06 +01003079 if (mixer) {
3080 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01003081 err = new_analog_input(codec, i, pin,
Takashi Iwaic9700422013-01-18 10:17:30 +01003082 spec->input_labels[i],
3083 spec->input_label_idxs[i],
3084 mixer);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003085 if (err < 0)
3086 return err;
3087 }
3088 }
3089
Takashi Iwaic9700422013-01-18 10:17:30 +01003090 err = parse_capture_source(codec, pin, i, num_adcs,
3091 spec->input_labels[i], -mixer);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003092 if (err < 0)
3093 return err;
Takashi Iwai294765582013-01-17 09:52:11 +01003094
Takashi Iwaif811c3c2013-03-07 18:32:59 +01003095 if (spec->add_jack_modes) {
Takashi Iwai294765582013-01-17 09:52:11 +01003096 err = create_in_jack_mode(codec, pin);
3097 if (err < 0)
3098 return err;
3099 }
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003100 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003101
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003102 if (mixer && spec->add_stereo_mix_input) {
Takashi Iwai9dba2052013-01-18 10:01:15 +01003103 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003104 "Stereo Mix", 0);
3105 if (err < 0)
3106 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003107 }
3108
3109 return 0;
3110}
3111
3112
3113/*
3114 * input source mux
3115 */
3116
Takashi Iwaic697b712013-01-07 17:09:26 +01003117/* get the input path specified by the given adc and imux indices */
3118static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003119{
3120 struct hda_gen_spec *spec = codec->spec;
David Henningssonb56fa1e2013-01-16 11:45:35 +01003121 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3122 snd_BUG();
3123 return NULL;
3124 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003125 if (spec->dyn_adc_switch)
3126 adc_idx = spec->dyn_adc_idx[imux_idx];
David Henningssond3d982f2013-01-18 15:43:01 +01003127 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
David Henningssonb56fa1e2013-01-16 11:45:35 +01003128 snd_BUG();
3129 return NULL;
3130 }
Takashi Iwaic697b712013-01-07 17:09:26 +01003131 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003132}
3133
3134static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3135 unsigned int idx);
3136
3137static int mux_enum_info(struct snd_kcontrol *kcontrol,
3138 struct snd_ctl_elem_info *uinfo)
3139{
3140 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3141 struct hda_gen_spec *spec = codec->spec;
3142 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3143}
3144
3145static int mux_enum_get(struct snd_kcontrol *kcontrol,
3146 struct snd_ctl_elem_value *ucontrol)
3147{
3148 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3149 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003150 /* the ctls are created at once with multiple counts */
3151 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003152
3153 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3154 return 0;
3155}
3156
3157static int mux_enum_put(struct snd_kcontrol *kcontrol,
3158 struct snd_ctl_elem_value *ucontrol)
3159{
3160 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003161 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003162 return mux_select(codec, adc_idx,
3163 ucontrol->value.enumerated.item[0]);
3164}
3165
Takashi Iwai352f7f92012-12-19 12:52:06 +01003166static const struct snd_kcontrol_new cap_src_temp = {
3167 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3168 .name = "Input Source",
3169 .info = mux_enum_info,
3170 .get = mux_enum_get,
3171 .put = mux_enum_put,
3172};
3173
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003174/*
3175 * capture volume and capture switch ctls
3176 */
3177
Takashi Iwai352f7f92012-12-19 12:52:06 +01003178typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3179 struct snd_ctl_elem_value *ucontrol);
3180
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003181/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003182static int cap_put_caller(struct snd_kcontrol *kcontrol,
3183 struct snd_ctl_elem_value *ucontrol,
3184 put_call_t func, int type)
3185{
3186 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3187 struct hda_gen_spec *spec = codec->spec;
3188 const struct hda_input_mux *imux;
3189 struct nid_path *path;
3190 int i, adc_idx, err = 0;
3191
3192 imux = &spec->input_mux;
David Henningssona053d1e2013-01-16 11:45:36 +01003193 adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003194 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003195 /* we use the cache-only update at first since multiple input paths
3196 * may shared the same amp; by updating only caches, the redundant
3197 * writes to hardware can be reduced.
3198 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003199 codec->cached_write = 1;
3200 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003201 path = get_input_path(codec, adc_idx, i);
3202 if (!path || !path->ctls[type])
Takashi Iwai352f7f92012-12-19 12:52:06 +01003203 continue;
3204 kcontrol->private_value = path->ctls[type];
3205 err = func(kcontrol, ucontrol);
3206 if (err < 0)
3207 goto error;
3208 }
3209 error:
3210 codec->cached_write = 0;
3211 mutex_unlock(&codec->control_mutex);
Takashi Iwaidc870f32013-01-22 15:24:30 +01003212 snd_hda_codec_flush_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003213 if (err >= 0 && spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01003214 spec->cap_sync_hook(codec, ucontrol);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003215 return err;
3216}
3217
3218/* capture volume ctl callbacks */
3219#define cap_vol_info snd_hda_mixer_amp_volume_info
3220#define cap_vol_get snd_hda_mixer_amp_volume_get
3221#define cap_vol_tlv snd_hda_mixer_amp_tlv
3222
3223static int cap_vol_put(struct snd_kcontrol *kcontrol,
3224 struct snd_ctl_elem_value *ucontrol)
3225{
3226 return cap_put_caller(kcontrol, ucontrol,
3227 snd_hda_mixer_amp_volume_put,
3228 NID_PATH_VOL_CTL);
3229}
3230
3231static const struct snd_kcontrol_new cap_vol_temp = {
3232 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3233 .name = "Capture Volume",
3234 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3235 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3236 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3237 .info = cap_vol_info,
3238 .get = cap_vol_get,
3239 .put = cap_vol_put,
3240 .tlv = { .c = cap_vol_tlv },
3241};
3242
3243/* capture switch ctl callbacks */
3244#define cap_sw_info snd_ctl_boolean_stereo_info
3245#define cap_sw_get snd_hda_mixer_amp_switch_get
3246
3247static int cap_sw_put(struct snd_kcontrol *kcontrol,
3248 struct snd_ctl_elem_value *ucontrol)
3249{
Takashi Iwaia90229e2013-01-18 14:10:00 +01003250 return cap_put_caller(kcontrol, ucontrol,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003251 snd_hda_mixer_amp_switch_put,
3252 NID_PATH_MUTE_CTL);
3253}
3254
3255static const struct snd_kcontrol_new cap_sw_temp = {
3256 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3257 .name = "Capture Switch",
3258 .info = cap_sw_info,
3259 .get = cap_sw_get,
3260 .put = cap_sw_put,
3261};
3262
3263static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3264{
3265 hda_nid_t nid;
3266 int i, depth;
3267
3268 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3269 for (depth = 0; depth < 3; depth++) {
3270 if (depth >= path->depth)
3271 return -EINVAL;
3272 i = path->depth - depth - 1;
3273 nid = path->path[i];
3274 if (!path->ctls[NID_PATH_VOL_CTL]) {
3275 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3276 path->ctls[NID_PATH_VOL_CTL] =
3277 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3278 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3279 int idx = path->idx[i];
3280 if (!depth && codec->single_adc_amp)
3281 idx = 0;
3282 path->ctls[NID_PATH_VOL_CTL] =
3283 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3284 }
3285 }
3286 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3287 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3288 path->ctls[NID_PATH_MUTE_CTL] =
3289 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3290 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3291 int idx = path->idx[i];
3292 if (!depth && codec->single_adc_amp)
3293 idx = 0;
3294 path->ctls[NID_PATH_MUTE_CTL] =
3295 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3296 }
3297 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01003298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 return 0;
3300}
3301
Takashi Iwai352f7f92012-12-19 12:52:06 +01003302static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003304 struct hda_gen_spec *spec = codec->spec;
3305 struct auto_pin_cfg *cfg = &spec->autocfg;
3306 unsigned int val;
3307 int i;
3308
3309 if (!spec->inv_dmic_split)
3310 return false;
3311 for (i = 0; i < cfg->num_inputs; i++) {
3312 if (cfg->inputs[i].pin != nid)
3313 continue;
3314 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3315 return false;
3316 val = snd_hda_codec_get_pincfg(codec, nid);
3317 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3318 }
3319 return false;
3320}
3321
Takashi Iwaia90229e2013-01-18 14:10:00 +01003322/* capture switch put callback for a single control with hook call */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003323static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3324 struct snd_ctl_elem_value *ucontrol)
3325{
3326 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3327 struct hda_gen_spec *spec = codec->spec;
3328 int ret;
3329
3330 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3331 if (ret < 0)
3332 return ret;
3333
Takashi Iwaia90229e2013-01-18 14:10:00 +01003334 if (spec->cap_sync_hook)
3335 spec->cap_sync_hook(codec, ucontrol);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003336
3337 return ret;
3338}
3339
Takashi Iwai352f7f92012-12-19 12:52:06 +01003340static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3341 int idx, bool is_switch, unsigned int ctl,
3342 bool inv_dmic)
3343{
3344 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai975cc022013-06-28 11:56:49 +02003345 char tmpname[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003346 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3347 const char *sfx = is_switch ? "Switch" : "Volume";
3348 unsigned int chs = inv_dmic ? 1 : 3;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003349 struct snd_kcontrol_new *knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003350
3351 if (!ctl)
3352 return 0;
3353
3354 if (label)
3355 snprintf(tmpname, sizeof(tmpname),
3356 "%s Capture %s", label, sfx);
3357 else
3358 snprintf(tmpname, sizeof(tmpname),
3359 "Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003360 knew = add_control(spec, type, tmpname, idx,
3361 amp_val_replace_channels(ctl, chs));
3362 if (!knew)
3363 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003364 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003365 knew->put = cap_single_sw_put;
3366 if (!inv_dmic)
3367 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003368
3369 /* Make independent right kcontrol */
3370 if (label)
3371 snprintf(tmpname, sizeof(tmpname),
3372 "Inverted %s Capture %s", label, sfx);
3373 else
3374 snprintf(tmpname, sizeof(tmpname),
3375 "Inverted Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003376 knew = add_control(spec, type, tmpname, idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003377 amp_val_replace_channels(ctl, 2));
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003378 if (!knew)
3379 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003380 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003381 knew->put = cap_single_sw_put;
3382 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003383}
3384
3385/* create single (and simple) capture volume and switch controls */
3386static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3387 unsigned int vol_ctl, unsigned int sw_ctl,
3388 bool inv_dmic)
3389{
3390 int err;
3391 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3392 if (err < 0)
3393 return err;
3394 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3395 if (err < 0)
3396 return err;
3397 return 0;
3398}
3399
3400/* create bound capture volume and switch controls */
3401static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3402 unsigned int vol_ctl, unsigned int sw_ctl)
3403{
3404 struct hda_gen_spec *spec = codec->spec;
3405 struct snd_kcontrol_new *knew;
3406
3407 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003408 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003409 if (!knew)
3410 return -ENOMEM;
3411 knew->index = idx;
3412 knew->private_value = vol_ctl;
3413 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3414 }
3415 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003416 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003417 if (!knew)
3418 return -ENOMEM;
3419 knew->index = idx;
3420 knew->private_value = sw_ctl;
3421 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3422 }
3423 return 0;
3424}
3425
3426/* return the vol ctl when used first in the imux list */
3427static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3428{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003429 struct nid_path *path;
3430 unsigned int ctl;
3431 int i;
3432
Takashi Iwaic697b712013-01-07 17:09:26 +01003433 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003434 if (!path)
3435 return 0;
3436 ctl = path->ctls[type];
3437 if (!ctl)
3438 return 0;
3439 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003440 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003441 if (path && path->ctls[type] == ctl)
3442 return 0;
3443 }
3444 return ctl;
3445}
3446
3447/* create individual capture volume and switch controls per input */
3448static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3449{
3450 struct hda_gen_spec *spec = codec->spec;
3451 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaic9700422013-01-18 10:17:30 +01003452 int i, err, type;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003453
3454 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003455 bool inv_dmic;
Takashi Iwaic9700422013-01-18 10:17:30 +01003456 int idx;
Takashi Iwai9dba2052013-01-18 10:01:15 +01003457
Takashi Iwaic9700422013-01-18 10:17:30 +01003458 idx = imux->items[i].index;
3459 if (idx >= spec->autocfg.num_inputs)
Takashi Iwai9dba2052013-01-18 10:01:15 +01003460 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003461 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3462
3463 for (type = 0; type < 2; type++) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003464 err = add_single_cap_ctl(codec,
3465 spec->input_labels[idx],
3466 spec->input_label_idxs[idx],
3467 type,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003468 get_first_cap_ctl(codec, i, type),
3469 inv_dmic);
3470 if (err < 0)
3471 return err;
3472 }
3473 }
3474 return 0;
3475}
3476
3477static int create_capture_mixers(struct hda_codec *codec)
3478{
3479 struct hda_gen_spec *spec = codec->spec;
3480 struct hda_input_mux *imux = &spec->input_mux;
3481 int i, n, nums, err;
3482
3483 if (spec->dyn_adc_switch)
3484 nums = 1;
3485 else
3486 nums = spec->num_adc_nids;
3487
3488 if (!spec->auto_mic && imux->num_items > 1) {
3489 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01003490 const char *name;
3491 name = nums > 1 ? "Input Source" : "Capture Source";
3492 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003493 if (!knew)
3494 return -ENOMEM;
3495 knew->count = nums;
3496 }
3497
3498 for (n = 0; n < nums; n++) {
3499 bool multi = false;
David Henningsson99a55922013-01-16 15:58:44 +01003500 bool multi_cap_vol = spec->multi_cap_vol;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003501 bool inv_dmic = false;
3502 int vol, sw;
3503
3504 vol = sw = 0;
3505 for (i = 0; i < imux->num_items; i++) {
3506 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01003507 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003508 if (!path)
3509 continue;
3510 parse_capvol_in_path(codec, path);
3511 if (!vol)
3512 vol = path->ctls[NID_PATH_VOL_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003513 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003514 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003515 if (!same_amp_caps(codec, vol,
3516 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3517 multi_cap_vol = true;
3518 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003519 if (!sw)
3520 sw = path->ctls[NID_PATH_MUTE_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003521 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003522 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003523 if (!same_amp_caps(codec, sw,
3524 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3525 multi_cap_vol = true;
3526 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003527 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3528 inv_dmic = true;
3529 }
3530
3531 if (!multi)
3532 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3533 inv_dmic);
David Henningssonccb04152013-10-14 10:16:22 +02003534 else if (!multi_cap_vol && !inv_dmic)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003535 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3536 else
3537 err = create_multi_cap_vol_ctl(codec);
3538 if (err < 0)
3539 return err;
3540 }
3541
3542 return 0;
3543}
3544
3545/*
3546 * add mic boosts if needed
3547 */
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003548
3549/* check whether the given amp is feasible as a boost volume */
3550static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3551 int dir, int idx)
3552{
3553 unsigned int step;
3554
3555 if (!nid_has_volume(codec, nid, dir) ||
3556 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3557 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3558 return false;
3559
3560 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3561 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3562 if (step < 0x20)
3563 return false;
3564 return true;
3565}
3566
3567/* look for a boost amp in a widget close to the pin */
3568static unsigned int look_for_boost_amp(struct hda_codec *codec,
3569 struct nid_path *path)
3570{
3571 unsigned int val = 0;
3572 hda_nid_t nid;
3573 int depth;
3574
3575 for (depth = 0; depth < 3; depth++) {
3576 if (depth >= path->depth - 1)
3577 break;
3578 nid = path->path[depth];
3579 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3580 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3581 break;
3582 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3583 path->idx[depth])) {
3584 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3585 HDA_INPUT);
3586 break;
3587 }
3588 }
3589
3590 return val;
3591}
3592
Takashi Iwai352f7f92012-12-19 12:52:06 +01003593static int parse_mic_boost(struct hda_codec *codec)
3594{
3595 struct hda_gen_spec *spec = codec->spec;
3596 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003597 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003598 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003599
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003600 if (!spec->num_adc_nids)
3601 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003602
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003603 for (i = 0; i < imux->num_items; i++) {
3604 struct nid_path *path;
3605 unsigned int val;
3606 int idx;
Takashi Iwai975cc022013-06-28 11:56:49 +02003607 char boost_label[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
David Henningsson02aba552013-01-16 15:58:43 +01003608
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003609 idx = imux->items[i].index;
3610 if (idx >= imux->num_items)
3611 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003612
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003613 /* check only line-in and mic pins */
Takashi Iwai1799cdd2013-01-18 14:37:16 +01003614 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003615 continue;
3616
3617 path = get_input_path(codec, 0, i);
3618 if (!path)
3619 continue;
3620
3621 val = look_for_boost_amp(codec, path);
3622 if (!val)
3623 continue;
3624
3625 /* create a boost control */
3626 snprintf(boost_label, sizeof(boost_label),
3627 "%s Boost Volume", spec->input_labels[idx]);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003628 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3629 spec->input_label_idxs[idx], val))
3630 return -ENOMEM;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003631
3632 path->ctls[NID_PATH_BOOST_CTL] = val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003633 }
3634 return 0;
3635}
3636
3637/*
3638 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3639 */
3640static void parse_digital(struct hda_codec *codec)
3641{
3642 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003643 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003644 int i, nums;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003645 hda_nid_t dig_nid, pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003646
3647 /* support multiple SPDIFs; the secondary is set up as a slave */
3648 nums = 0;
3649 for (i = 0; i < spec->autocfg.dig_outs; i++) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003650 pin = spec->autocfg.dig_out_pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003651 dig_nid = look_for_dac(codec, pin, true);
3652 if (!dig_nid)
3653 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003654 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003655 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003656 continue;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003657 print_nid_path("digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01003658 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01003659 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003660 set_pin_target(codec, pin, PIN_OUT, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003661 if (!nums) {
3662 spec->multiout.dig_out_nid = dig_nid;
3663 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3664 } else {
3665 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3666 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
3667 break;
3668 spec->slave_dig_outs[nums - 1] = dig_nid;
3669 }
3670 nums++;
3671 }
3672
3673 if (spec->autocfg.dig_in_pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003674 pin = spec->autocfg.dig_in_pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003675 dig_nid = codec->start_nid;
3676 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003677 unsigned int wcaps = get_wcaps(codec, dig_nid);
3678 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3679 continue;
3680 if (!(wcaps & AC_WCAP_DIGITAL))
3681 continue;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003682 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003683 if (path) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003684 print_nid_path("digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003685 path->active = true;
3686 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003687 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003688 set_pin_target(codec, pin, PIN_IN, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003689 break;
3690 }
3691 }
3692 }
3693}
3694
3695
3696/*
3697 * input MUX handling
3698 */
3699
3700static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3701
3702/* select the given imux item; either unmute exclusively or select the route */
3703static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3704 unsigned int idx)
3705{
3706 struct hda_gen_spec *spec = codec->spec;
3707 const struct hda_input_mux *imux;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003708 struct nid_path *old_path, *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003709
3710 imux = &spec->input_mux;
3711 if (!imux->num_items)
3712 return 0;
3713
3714 if (idx >= imux->num_items)
3715 idx = imux->num_items - 1;
3716 if (spec->cur_mux[adc_idx] == idx)
3717 return 0;
3718
Takashi Iwai55196ff2013-01-24 17:32:56 +01003719 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3720 if (!old_path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003721 return 0;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003722 if (old_path->active)
3723 snd_hda_activate_path(codec, old_path, false, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003724
3725 spec->cur_mux[adc_idx] = idx;
3726
Takashi Iwai967303d2013-02-19 17:12:42 +01003727 if (spec->hp_mic)
3728 update_hp_mic(codec, adc_idx, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003729
3730 if (spec->dyn_adc_switch)
3731 dyn_adc_pcm_resetup(codec, idx);
3732
Takashi Iwaic697b712013-01-07 17:09:26 +01003733 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003734 if (!path)
3735 return 0;
3736 if (path->active)
3737 return 0;
3738 snd_hda_activate_path(codec, path, true, false);
3739 if (spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01003740 spec->cap_sync_hook(codec, NULL);
Takashi Iwai55196ff2013-01-24 17:32:56 +01003741 path_power_down_sync(codec, old_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003742 return 1;
3743}
3744
3745
3746/*
3747 * Jack detections for HP auto-mute and mic-switch
3748 */
3749
3750/* check each pin in the given array; returns true if any of them is plugged */
3751static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
3752{
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02003753 int i;
3754 bool present = false;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003755
3756 for (i = 0; i < num_pins; i++) {
3757 hda_nid_t nid = pins[i];
3758 if (!nid)
3759 break;
Takashi Iwai0b4df932013-01-10 09:45:13 +01003760 /* don't detect pins retasked as inputs */
3761 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
3762 continue;
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02003763 if (snd_hda_jack_detect_state(codec, nid) == HDA_JACK_PRESENT)
3764 present = true;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003765 }
3766 return present;
3767}
3768
3769/* standard HP/line-out auto-mute helper */
3770static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003771 int *paths, bool mute)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003772{
3773 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003774 int i;
3775
3776 for (i = 0; i < num_pins; i++) {
3777 hda_nid_t nid = pins[i];
Takashi Iwai967303d2013-02-19 17:12:42 +01003778 unsigned int val, oldval;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003779 if (!nid)
3780 break;
Takashi Iwai7eebffd2013-06-24 16:00:21 +02003781
3782 if (spec->auto_mute_via_amp) {
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003783 struct nid_path *path;
3784 hda_nid_t mute_nid;
3785
3786 path = snd_hda_get_path_from_idx(codec, paths[i]);
3787 if (!path)
3788 continue;
3789 mute_nid = get_amp_nid_(path->ctls[NID_PATH_MUTE_CTL]);
3790 if (!mute_nid)
3791 continue;
Takashi Iwai7eebffd2013-06-24 16:00:21 +02003792 if (mute)
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003793 spec->mute_bits |= (1ULL << mute_nid);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02003794 else
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003795 spec->mute_bits &= ~(1ULL << mute_nid);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02003796 set_pin_eapd(codec, nid, !mute);
3797 continue;
3798 }
3799
Takashi Iwai967303d2013-02-19 17:12:42 +01003800 oldval = snd_hda_codec_get_pin_target(codec, nid);
3801 if (oldval & PIN_IN)
3802 continue; /* no mute for inputs */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003803 /* don't reset VREF value in case it's controlling
3804 * the amp (see alc861_fixup_asus_amp_vref_0f())
3805 */
Takashi Iwai2c12c302013-01-10 09:33:29 +01003806 if (spec->keep_vref_in_automute)
Takashi Iwai967303d2013-02-19 17:12:42 +01003807 val = oldval & ~PIN_HP;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003808 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01003809 val = 0;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003810 if (!mute)
Takashi Iwai967303d2013-02-19 17:12:42 +01003811 val |= oldval;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003812 /* here we call update_pin_ctl() so that the pinctl is changed
3813 * without changing the pinctl target value;
3814 * the original target value will be still referred at the
3815 * init / resume again
3816 */
3817 update_pin_ctl(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01003818 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003819 }
3820}
3821
3822/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003823void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003824{
3825 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003826 int *paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003827 int on;
3828
3829 /* Control HP pins/amps depending on master_mute state;
3830 * in general, HP pins/amps control should be enabled in all cases,
3831 * but currently set only for master_mute, just to be safe
3832 */
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003833 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
3834 paths = spec->out_paths;
3835 else
3836 paths = spec->hp_paths;
Takashi Iwai967303d2013-02-19 17:12:42 +01003837 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003838 spec->autocfg.hp_pins, paths, spec->master_mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003839
3840 if (!spec->automute_speaker)
3841 on = 0;
3842 else
3843 on = spec->hp_jack_present | spec->line_jack_present;
3844 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003845 spec->speaker_muted = on;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003846 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3847 paths = spec->out_paths;
3848 else
3849 paths = spec->speaker_paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003850 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003851 spec->autocfg.speaker_pins, paths, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003852
3853 /* toggle line-out mutes if needed, too */
3854 /* if LO is a copy of either HP or Speaker, don't need to handle it */
3855 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
3856 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
3857 return;
3858 if (!spec->automute_lo)
3859 on = 0;
3860 else
3861 on = spec->hp_jack_present;
3862 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003863 spec->line_out_muted = on;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003864 paths = spec->out_paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003865 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003866 spec->autocfg.line_out_pins, paths, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003867}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003868EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003869
3870static void call_update_outputs(struct hda_codec *codec)
3871{
3872 struct hda_gen_spec *spec = codec->spec;
3873 if (spec->automute_hook)
3874 spec->automute_hook(codec);
3875 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01003876 snd_hda_gen_update_outputs(codec);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02003877
3878 /* sync the whole vmaster slaves to reflect the new auto-mute status */
3879 if (spec->auto_mute_via_amp && !codec->bus->shutdown)
3880 snd_ctl_sync_vmaster(spec->vmaster_mute.sw_kctl, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003881}
3882
3883/* standard HP-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003884void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003885{
3886 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai92603c52013-01-22 07:46:31 +01003887 hda_nid_t *pins = spec->autocfg.hp_pins;
3888 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003889
Takashi Iwai92603c52013-01-22 07:46:31 +01003890 /* No detection for the first HP jack during indep-HP mode */
3891 if (spec->indep_hp_enabled) {
3892 pins++;
3893 num_pins--;
3894 }
3895
3896 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003897 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
3898 return;
3899 call_update_outputs(codec);
3900}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003901EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003902
3903/* standard line-out-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003904void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003905{
3906 struct hda_gen_spec *spec = codec->spec;
3907
3908 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3909 return;
3910 /* check LO jack only when it's different from HP */
3911 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
3912 return;
3913
3914 spec->line_jack_present =
3915 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3916 spec->autocfg.line_out_pins);
3917 if (!spec->automute_speaker || !spec->detect_lo)
3918 return;
3919 call_update_outputs(codec);
3920}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003921EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003922
3923/* standard mic auto-switch helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003924void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003925{
3926 struct hda_gen_spec *spec = codec->spec;
3927 int i;
3928
3929 if (!spec->auto_mic)
3930 return;
3931
3932 for (i = spec->am_num_entries - 1; i > 0; i--) {
Takashi Iwai0b4df932013-01-10 09:45:13 +01003933 hda_nid_t pin = spec->am_entry[i].pin;
3934 /* don't detect pins retasked as outputs */
3935 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
3936 continue;
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02003937 if (snd_hda_jack_detect_state(codec, pin) == HDA_JACK_PRESENT) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003938 mux_select(codec, 0, spec->am_entry[i].idx);
3939 return;
3940 }
3941 }
3942 mux_select(codec, 0, spec->am_entry[0].idx);
3943}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003944EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003945
Takashi Iwai77afe0e2013-05-31 14:10:03 +02003946/* call appropriate hooks */
3947static void call_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
3948{
3949 struct hda_gen_spec *spec = codec->spec;
3950 if (spec->hp_automute_hook)
3951 spec->hp_automute_hook(codec, jack);
3952 else
3953 snd_hda_gen_hp_automute(codec, jack);
3954}
3955
3956static void call_line_automute(struct hda_codec *codec,
3957 struct hda_jack_tbl *jack)
3958{
3959 struct hda_gen_spec *spec = codec->spec;
3960 if (spec->line_automute_hook)
3961 spec->line_automute_hook(codec, jack);
3962 else
3963 snd_hda_gen_line_automute(codec, jack);
3964}
3965
3966static void call_mic_autoswitch(struct hda_codec *codec,
3967 struct hda_jack_tbl *jack)
3968{
3969 struct hda_gen_spec *spec = codec->spec;
3970 if (spec->mic_autoswitch_hook)
3971 spec->mic_autoswitch_hook(codec, jack);
3972 else
3973 snd_hda_gen_mic_autoswitch(codec, jack);
3974}
3975
Takashi Iwai963afde2013-05-31 15:20:31 +02003976/* update jack retasking */
3977static void update_automute_all(struct hda_codec *codec)
3978{
3979 call_hp_automute(codec, NULL);
3980 call_line_automute(codec, NULL);
3981 call_mic_autoswitch(codec, NULL);
3982}
3983
Takashi Iwai352f7f92012-12-19 12:52:06 +01003984/*
3985 * Auto-Mute mode mixer enum support
3986 */
3987static int automute_mode_info(struct snd_kcontrol *kcontrol,
3988 struct snd_ctl_elem_info *uinfo)
3989{
3990 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3991 struct hda_gen_spec *spec = codec->spec;
3992 static const char * const texts3[] = {
3993 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02003994 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995
Takashi Iwai352f7f92012-12-19 12:52:06 +01003996 if (spec->automute_speaker_possible && spec->automute_lo_possible)
3997 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
3998 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
3999}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000
Takashi Iwai352f7f92012-12-19 12:52:06 +01004001static int automute_mode_get(struct snd_kcontrol *kcontrol,
4002 struct snd_ctl_elem_value *ucontrol)
4003{
4004 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4005 struct hda_gen_spec *spec = codec->spec;
4006 unsigned int val = 0;
4007 if (spec->automute_speaker)
4008 val++;
4009 if (spec->automute_lo)
4010 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004011
Takashi Iwai352f7f92012-12-19 12:52:06 +01004012 ucontrol->value.enumerated.item[0] = val;
4013 return 0;
4014}
4015
4016static int automute_mode_put(struct snd_kcontrol *kcontrol,
4017 struct snd_ctl_elem_value *ucontrol)
4018{
4019 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4020 struct hda_gen_spec *spec = codec->spec;
4021
4022 switch (ucontrol->value.enumerated.item[0]) {
4023 case 0:
4024 if (!spec->automute_speaker && !spec->automute_lo)
4025 return 0;
4026 spec->automute_speaker = 0;
4027 spec->automute_lo = 0;
4028 break;
4029 case 1:
4030 if (spec->automute_speaker_possible) {
4031 if (!spec->automute_lo && spec->automute_speaker)
4032 return 0;
4033 spec->automute_speaker = 1;
4034 spec->automute_lo = 0;
4035 } else if (spec->automute_lo_possible) {
4036 if (spec->automute_lo)
4037 return 0;
4038 spec->automute_lo = 1;
4039 } else
4040 return -EINVAL;
4041 break;
4042 case 2:
4043 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
4044 return -EINVAL;
4045 if (spec->automute_speaker && spec->automute_lo)
4046 return 0;
4047 spec->automute_speaker = 1;
4048 spec->automute_lo = 1;
4049 break;
4050 default:
4051 return -EINVAL;
4052 }
4053 call_update_outputs(codec);
4054 return 1;
4055}
4056
4057static const struct snd_kcontrol_new automute_mode_enum = {
4058 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4059 .name = "Auto-Mute Mode",
4060 .info = automute_mode_info,
4061 .get = automute_mode_get,
4062 .put = automute_mode_put,
4063};
4064
4065static int add_automute_mode_enum(struct hda_codec *codec)
4066{
4067 struct hda_gen_spec *spec = codec->spec;
4068
Takashi Iwai12c93df2012-12-19 14:38:33 +01004069 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01004070 return -ENOMEM;
4071 return 0;
4072}
4073
4074/*
4075 * Check the availability of HP/line-out auto-mute;
4076 * Set up appropriately if really supported
4077 */
4078static int check_auto_mute_availability(struct hda_codec *codec)
4079{
4080 struct hda_gen_spec *spec = codec->spec;
4081 struct auto_pin_cfg *cfg = &spec->autocfg;
4082 int present = 0;
4083 int i, err;
4084
Takashi Iwaif72706b2013-01-16 18:20:07 +01004085 if (spec->suppress_auto_mute)
4086 return 0;
4087
Takashi Iwai352f7f92012-12-19 12:52:06 +01004088 if (cfg->hp_pins[0])
4089 present++;
4090 if (cfg->line_out_pins[0])
4091 present++;
4092 if (cfg->speaker_pins[0])
4093 present++;
4094 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02004095 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004096
4097 if (!cfg->speaker_pins[0] &&
4098 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
4099 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4100 sizeof(cfg->speaker_pins));
4101 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103
Takashi Iwai352f7f92012-12-19 12:52:06 +01004104 if (!cfg->hp_pins[0] &&
4105 cfg->line_out_type == AUTO_PIN_HP_OUT) {
4106 memcpy(cfg->hp_pins, cfg->line_out_pins,
4107 sizeof(cfg->hp_pins));
4108 cfg->hp_outs = cfg->line_outs;
4109 }
4110
4111 for (i = 0; i < cfg->hp_outs; i++) {
4112 hda_nid_t nid = cfg->hp_pins[i];
4113 if (!is_jack_detectable(codec, nid))
4114 continue;
4115 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
4116 nid);
4117 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004118 call_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004119 spec->detect_hp = 1;
4120 }
4121
4122 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
4123 if (cfg->speaker_outs)
4124 for (i = 0; i < cfg->line_outs; i++) {
4125 hda_nid_t nid = cfg->line_out_pins[i];
4126 if (!is_jack_detectable(codec, nid))
4127 continue;
4128 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
4129 snd_hda_jack_detect_enable_callback(codec, nid,
4130 HDA_GEN_FRONT_EVENT,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004131 call_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004132 spec->detect_lo = 1;
4133 }
4134 spec->automute_lo_possible = spec->detect_hp;
4135 }
4136
4137 spec->automute_speaker_possible = cfg->speaker_outs &&
4138 (spec->detect_hp || spec->detect_lo);
4139
4140 spec->automute_lo = spec->automute_lo_possible;
4141 spec->automute_speaker = spec->automute_speaker_possible;
4142
4143 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
4144 /* create a control for automute mode */
4145 err = add_automute_mode_enum(codec);
4146 if (err < 0)
4147 return err;
4148 }
4149 return 0;
4150}
4151
Takashi Iwai352f7f92012-12-19 12:52:06 +01004152/* check whether all auto-mic pins are valid; setup indices if OK */
4153static bool auto_mic_check_imux(struct hda_codec *codec)
4154{
4155 struct hda_gen_spec *spec = codec->spec;
4156 const struct hda_input_mux *imux;
4157 int i;
4158
4159 imux = &spec->input_mux;
4160 for (i = 0; i < spec->am_num_entries; i++) {
4161 spec->am_entry[i].idx =
4162 find_idx_in_nid_list(spec->am_entry[i].pin,
4163 spec->imux_pins, imux->num_items);
4164 if (spec->am_entry[i].idx < 0)
4165 return false; /* no corresponding imux */
4166 }
4167
4168 /* we don't need the jack detection for the first pin */
4169 for (i = 1; i < spec->am_num_entries; i++)
4170 snd_hda_jack_detect_enable_callback(codec,
4171 spec->am_entry[i].pin,
4172 HDA_GEN_MIC_EVENT,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004173 call_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004174 return true;
4175}
4176
4177static int compare_attr(const void *ap, const void *bp)
4178{
4179 const struct automic_entry *a = ap;
4180 const struct automic_entry *b = bp;
4181 return (int)(a->attr - b->attr);
4182}
4183
4184/*
4185 * Check the availability of auto-mic switch;
4186 * Set up if really supported
4187 */
4188static int check_auto_mic_availability(struct hda_codec *codec)
4189{
4190 struct hda_gen_spec *spec = codec->spec;
4191 struct auto_pin_cfg *cfg = &spec->autocfg;
4192 unsigned int types;
4193 int i, num_pins;
4194
Takashi Iwaid12daf62013-01-07 16:32:11 +01004195 if (spec->suppress_auto_mic)
4196 return 0;
4197
Takashi Iwai352f7f92012-12-19 12:52:06 +01004198 types = 0;
4199 num_pins = 0;
4200 for (i = 0; i < cfg->num_inputs; i++) {
4201 hda_nid_t nid = cfg->inputs[i].pin;
4202 unsigned int attr;
4203 attr = snd_hda_codec_get_pincfg(codec, nid);
4204 attr = snd_hda_get_input_pin_attr(attr);
4205 if (types & (1 << attr))
4206 return 0; /* already occupied */
4207 switch (attr) {
4208 case INPUT_PIN_ATTR_INT:
4209 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4210 return 0; /* invalid type */
4211 break;
4212 case INPUT_PIN_ATTR_UNUSED:
4213 return 0; /* invalid entry */
4214 default:
4215 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4216 return 0; /* invalid type */
4217 if (!spec->line_in_auto_switch &&
4218 cfg->inputs[i].type != AUTO_PIN_MIC)
4219 return 0; /* only mic is allowed */
4220 if (!is_jack_detectable(codec, nid))
4221 return 0; /* no unsol support */
4222 break;
4223 }
4224 if (num_pins >= MAX_AUTO_MIC_PINS)
4225 return 0;
4226 types |= (1 << attr);
4227 spec->am_entry[num_pins].pin = nid;
4228 spec->am_entry[num_pins].attr = attr;
4229 num_pins++;
4230 }
4231
4232 if (num_pins < 2)
4233 return 0;
4234
4235 spec->am_num_entries = num_pins;
4236 /* sort the am_entry in the order of attr so that the pin with a
4237 * higher attr will be selected when the jack is plugged.
4238 */
4239 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4240 compare_attr, NULL);
4241
4242 if (!auto_mic_check_imux(codec))
4243 return 0;
4244
4245 spec->auto_mic = 1;
4246 spec->num_adc_nids = 1;
4247 spec->cur_mux[0] = spec->am_entry[0].idx;
4248 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
4249 spec->am_entry[0].pin,
4250 spec->am_entry[1].pin,
4251 spec->am_entry[2].pin);
4252
4253 return 0;
4254}
4255
Takashi Iwai55196ff2013-01-24 17:32:56 +01004256/* power_filter hook; make inactive widgets into power down */
4257static unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
4258 hda_nid_t nid,
4259 unsigned int power_state)
4260{
4261 if (power_state != AC_PWRST_D0)
4262 return power_state;
4263 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4264 return power_state;
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +02004265 if (is_active_nid_for_any(codec, nid))
Takashi Iwai55196ff2013-01-24 17:32:56 +01004266 return power_state;
4267 return AC_PWRST_D3;
4268}
4269
Takashi Iwai352f7f92012-12-19 12:52:06 +01004270
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004271/*
4272 * Parse the given BIOS configuration and set up the hda_gen_spec
4273 *
4274 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004275 * or a negative error code
4276 */
4277int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004278 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004279{
4280 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004281 int err;
4282
Takashi Iwai1c70a582013-01-11 17:48:22 +01004283 parse_user_hints(codec);
4284
Takashi Iwaie4a395e2013-01-23 17:00:31 +01004285 if (spec->mixer_nid && !spec->mixer_merge_nid)
4286 spec->mixer_merge_nid = spec->mixer_nid;
4287
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004288 if (cfg != &spec->autocfg) {
4289 spec->autocfg = *cfg;
4290 cfg = &spec->autocfg;
4291 }
4292
Takashi Iwai98bd1112013-03-22 14:53:50 +01004293 if (!spec->main_out_badness)
4294 spec->main_out_badness = &hda_main_out_badness;
4295 if (!spec->extra_out_badness)
4296 spec->extra_out_badness = &hda_extra_out_badness;
4297
David Henningsson6fc4cb92013-01-16 15:58:45 +01004298 fill_all_dac_nids(codec);
4299
Takashi Iwai352f7f92012-12-19 12:52:06 +01004300 if (!cfg->line_outs) {
4301 if (cfg->dig_outs || cfg->dig_in_pin) {
4302 spec->multiout.max_channels = 2;
4303 spec->no_analog = 1;
4304 goto dig_only;
4305 }
4306 return 0; /* can't find valid BIOS pin config */
4307 }
4308
4309 if (!spec->no_primary_hp &&
4310 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
4311 cfg->line_outs <= cfg->hp_outs) {
4312 /* use HP as primary out */
4313 cfg->speaker_outs = cfg->line_outs;
4314 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4315 sizeof(cfg->speaker_pins));
4316 cfg->line_outs = cfg->hp_outs;
4317 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4318 cfg->hp_outs = 0;
4319 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4320 cfg->line_out_type = AUTO_PIN_HP_OUT;
4321 }
4322
4323 err = parse_output_paths(codec);
4324 if (err < 0)
4325 return err;
4326 err = create_multi_channel_mode(codec);
4327 if (err < 0)
4328 return err;
4329 err = create_multi_out_ctls(codec, cfg);
4330 if (err < 0)
4331 return err;
4332 err = create_hp_out_ctls(codec);
4333 if (err < 0)
4334 return err;
4335 err = create_speaker_out_ctls(codec);
4336 if (err < 0)
4337 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004338 err = create_indep_hp_ctls(codec);
4339 if (err < 0)
4340 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01004341 err = create_loopback_mixing_ctl(codec);
4342 if (err < 0)
4343 return err;
Takashi Iwai967303d2013-02-19 17:12:42 +01004344 err = create_hp_mic(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004345 if (err < 0)
4346 return err;
4347 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02004348 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02004349 return err;
4350
Takashi Iwaia07a9492013-01-07 16:44:06 +01004351 spec->const_channel_count = spec->ext_channel_count;
4352 /* check the multiple speaker and headphone pins */
4353 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4354 spec->const_channel_count = max(spec->const_channel_count,
4355 cfg->speaker_outs * 2);
4356 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4357 spec->const_channel_count = max(spec->const_channel_count,
4358 cfg->hp_outs * 2);
4359 spec->multiout.max_channels = max(spec->ext_channel_count,
4360 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004361
4362 err = check_auto_mute_availability(codec);
4363 if (err < 0)
4364 return err;
4365
4366 err = check_dyn_adc_switch(codec);
4367 if (err < 0)
4368 return err;
4369
Takashi Iwai967303d2013-02-19 17:12:42 +01004370 err = check_auto_mic_availability(codec);
4371 if (err < 0)
4372 return err;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004373
Takashi Iwai352f7f92012-12-19 12:52:06 +01004374 err = create_capture_mixers(codec);
4375 if (err < 0)
4376 return err;
4377
4378 err = parse_mic_boost(codec);
4379 if (err < 0)
4380 return err;
4381
Takashi Iwaif811c3c2013-03-07 18:32:59 +01004382 if (spec->add_jack_modes) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01004383 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4384 err = create_out_jack_modes(codec, cfg->line_outs,
4385 cfg->line_out_pins);
4386 if (err < 0)
4387 return err;
4388 }
4389 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4390 err = create_out_jack_modes(codec, cfg->hp_outs,
4391 cfg->hp_pins);
4392 if (err < 0)
4393 return err;
4394 }
4395 }
4396
Takashi Iwai352f7f92012-12-19 12:52:06 +01004397 dig_only:
4398 parse_digital(codec);
4399
Takashi Iwai55196ff2013-01-24 17:32:56 +01004400 if (spec->power_down_unused)
4401 codec->power_filter = snd_hda_gen_path_power_filter;
4402
Takashi Iwai7504b6c2013-03-18 11:25:51 +01004403 if (!spec->no_analog && spec->beep_nid) {
4404 err = snd_hda_attach_beep_device(codec, spec->beep_nid);
4405 if (err < 0)
4406 return err;
4407 }
4408
Takashi Iwai352f7f92012-12-19 12:52:06 +01004409 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004411EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412
4413
4414/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004415 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004417
4418/* slave controls for virtual master */
4419static const char * const slave_pfxs[] = {
4420 "Front", "Surround", "Center", "LFE", "Side",
4421 "Headphone", "Speaker", "Mono", "Line Out",
4422 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01004423 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
4424 "Headphone Front", "Headphone Surround", "Headphone CLFE",
4425 "Headphone Side",
Takashi Iwai352f7f92012-12-19 12:52:06 +01004426 NULL,
4427};
4428
4429int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004431 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433
Takashi Iwai36502d02012-12-19 15:15:10 +01004434 if (spec->kctls.used) {
4435 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
4436 if (err < 0)
4437 return err;
4438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439
Takashi Iwai352f7f92012-12-19 12:52:06 +01004440 if (spec->multiout.dig_out_nid) {
4441 err = snd_hda_create_dig_out_ctls(codec,
4442 spec->multiout.dig_out_nid,
4443 spec->multiout.dig_out_nid,
4444 spec->pcm_rec[1].pcm_type);
4445 if (err < 0)
4446 return err;
4447 if (!spec->no_analog) {
4448 err = snd_hda_create_spdif_share_sw(codec,
4449 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 if (err < 0)
4451 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004452 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 }
4454 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004455 if (spec->dig_in_nid) {
4456 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
4457 if (err < 0)
4458 return err;
4459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460
Takashi Iwai352f7f92012-12-19 12:52:06 +01004461 /* if we have no master control, let's create it */
4462 if (!spec->no_analog &&
4463 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004464 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01004465 spec->vmaster_tlv, slave_pfxs,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004466 "Playback Volume");
4467 if (err < 0)
4468 return err;
4469 }
4470 if (!spec->no_analog &&
4471 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
4472 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
4473 NULL, slave_pfxs,
4474 "Playback Switch",
4475 true, &spec->vmaster_mute.sw_kctl);
4476 if (err < 0)
4477 return err;
4478 if (spec->vmaster_mute.hook)
Takashi Iwaifd25a972012-12-20 14:57:18 +01004479 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
4480 spec->vmaster_mute_enum);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482
Takashi Iwai352f7f92012-12-19 12:52:06 +01004483 free_kctls(spec); /* no longer needed */
4484
Takashi Iwai352f7f92012-12-19 12:52:06 +01004485 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
4486 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 return err;
4488
4489 return 0;
4490}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004491EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
4492
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493
4494/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004495 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004498static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
4499 struct hda_codec *codec,
4500 struct snd_pcm_substream *substream,
4501 int action)
4502{
4503 struct hda_gen_spec *spec = codec->spec;
4504 if (spec->pcm_playback_hook)
4505 spec->pcm_playback_hook(hinfo, codec, substream, action);
4506}
4507
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004508static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
4509 struct hda_codec *codec,
4510 struct snd_pcm_substream *substream,
4511 int action)
4512{
4513 struct hda_gen_spec *spec = codec->spec;
4514 if (spec->pcm_capture_hook)
4515 spec->pcm_capture_hook(hinfo, codec, substream, action);
4516}
4517
Takashi Iwai352f7f92012-12-19 12:52:06 +01004518/*
4519 * Analog playback callbacks
4520 */
4521static int playback_pcm_open(struct hda_pcm_stream *hinfo,
4522 struct hda_codec *codec,
4523 struct snd_pcm_substream *substream)
4524{
4525 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004526 int err;
4527
4528 mutex_lock(&spec->pcm_mutex);
4529 err = snd_hda_multi_out_analog_open(codec,
4530 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004531 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004532 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004533 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004534 call_pcm_playback_hook(hinfo, codec, substream,
4535 HDA_GEN_PCM_ACT_OPEN);
4536 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004537 mutex_unlock(&spec->pcm_mutex);
4538 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004539}
4540
4541static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01004542 struct hda_codec *codec,
4543 unsigned int stream_tag,
4544 unsigned int format,
4545 struct snd_pcm_substream *substream)
4546{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004547 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004548 int err;
4549
4550 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
4551 stream_tag, format, substream);
4552 if (!err)
4553 call_pcm_playback_hook(hinfo, codec, substream,
4554 HDA_GEN_PCM_ACT_PREPARE);
4555 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004556}
Takashi Iwai97ec5582006-03-21 11:29:07 +01004557
Takashi Iwai352f7f92012-12-19 12:52:06 +01004558static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4559 struct hda_codec *codec,
4560 struct snd_pcm_substream *substream)
4561{
4562 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004563 int err;
4564
4565 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
4566 if (!err)
4567 call_pcm_playback_hook(hinfo, codec, substream,
4568 HDA_GEN_PCM_ACT_CLEANUP);
4569 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004570}
4571
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004572static int playback_pcm_close(struct hda_pcm_stream *hinfo,
4573 struct hda_codec *codec,
4574 struct snd_pcm_substream *substream)
4575{
4576 struct hda_gen_spec *spec = codec->spec;
4577 mutex_lock(&spec->pcm_mutex);
4578 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004579 call_pcm_playback_hook(hinfo, codec, substream,
4580 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004581 mutex_unlock(&spec->pcm_mutex);
4582 return 0;
4583}
4584
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004585static int capture_pcm_open(struct hda_pcm_stream *hinfo,
4586 struct hda_codec *codec,
4587 struct snd_pcm_substream *substream)
4588{
4589 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
4590 return 0;
4591}
4592
4593static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4594 struct hda_codec *codec,
4595 unsigned int stream_tag,
4596 unsigned int format,
4597 struct snd_pcm_substream *substream)
4598{
4599 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4600 call_pcm_capture_hook(hinfo, codec, substream,
4601 HDA_GEN_PCM_ACT_PREPARE);
4602 return 0;
4603}
4604
4605static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4606 struct hda_codec *codec,
4607 struct snd_pcm_substream *substream)
4608{
4609 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4610 call_pcm_capture_hook(hinfo, codec, substream,
4611 HDA_GEN_PCM_ACT_CLEANUP);
4612 return 0;
4613}
4614
4615static int capture_pcm_close(struct hda_pcm_stream *hinfo,
4616 struct hda_codec *codec,
4617 struct snd_pcm_substream *substream)
4618{
4619 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
4620 return 0;
4621}
4622
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004623static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
4624 struct hda_codec *codec,
4625 struct snd_pcm_substream *substream)
4626{
4627 struct hda_gen_spec *spec = codec->spec;
4628 int err = 0;
4629
4630 mutex_lock(&spec->pcm_mutex);
4631 if (!spec->indep_hp_enabled)
4632 err = -EBUSY;
4633 else
4634 spec->active_streams |= 1 << STREAM_INDEP_HP;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004635 call_pcm_playback_hook(hinfo, codec, substream,
4636 HDA_GEN_PCM_ACT_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004637 mutex_unlock(&spec->pcm_mutex);
4638 return err;
4639}
4640
4641static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
4642 struct hda_codec *codec,
4643 struct snd_pcm_substream *substream)
4644{
4645 struct hda_gen_spec *spec = codec->spec;
4646 mutex_lock(&spec->pcm_mutex);
4647 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004648 call_pcm_playback_hook(hinfo, codec, substream,
4649 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004650 mutex_unlock(&spec->pcm_mutex);
4651 return 0;
4652}
4653
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004654static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4655 struct hda_codec *codec,
4656 unsigned int stream_tag,
4657 unsigned int format,
4658 struct snd_pcm_substream *substream)
4659{
4660 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4661 call_pcm_playback_hook(hinfo, codec, substream,
4662 HDA_GEN_PCM_ACT_PREPARE);
4663 return 0;
4664}
4665
4666static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4667 struct hda_codec *codec,
4668 struct snd_pcm_substream *substream)
4669{
4670 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4671 call_pcm_playback_hook(hinfo, codec, substream,
4672 HDA_GEN_PCM_ACT_CLEANUP);
4673 return 0;
4674}
4675
Takashi Iwai352f7f92012-12-19 12:52:06 +01004676/*
4677 * Digital out
4678 */
4679static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
4680 struct hda_codec *codec,
4681 struct snd_pcm_substream *substream)
4682{
4683 struct hda_gen_spec *spec = codec->spec;
4684 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
4685}
4686
4687static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4688 struct hda_codec *codec,
4689 unsigned int stream_tag,
4690 unsigned int format,
4691 struct snd_pcm_substream *substream)
4692{
4693 struct hda_gen_spec *spec = codec->spec;
4694 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
4695 stream_tag, format, substream);
4696}
4697
4698static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4699 struct hda_codec *codec,
4700 struct snd_pcm_substream *substream)
4701{
4702 struct hda_gen_spec *spec = codec->spec;
4703 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
4704}
4705
4706static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
4707 struct hda_codec *codec,
4708 struct snd_pcm_substream *substream)
4709{
4710 struct hda_gen_spec *spec = codec->spec;
4711 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
4712}
4713
4714/*
4715 * Analog capture
4716 */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004717#define alt_capture_pcm_open capture_pcm_open
4718#define alt_capture_pcm_close capture_pcm_close
4719
Takashi Iwai352f7f92012-12-19 12:52:06 +01004720static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4721 struct hda_codec *codec,
4722 unsigned int stream_tag,
4723 unsigned int format,
4724 struct snd_pcm_substream *substream)
4725{
4726 struct hda_gen_spec *spec = codec->spec;
4727
4728 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01004729 stream_tag, 0, format);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004730 call_pcm_capture_hook(hinfo, codec, substream,
4731 HDA_GEN_PCM_ACT_PREPARE);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004732 return 0;
4733}
4734
Takashi Iwai352f7f92012-12-19 12:52:06 +01004735static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4736 struct hda_codec *codec,
4737 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01004738{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004739 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01004740
Takashi Iwai352f7f92012-12-19 12:52:06 +01004741 snd_hda_codec_cleanup_stream(codec,
4742 spec->adc_nids[substream->number + 1]);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004743 call_pcm_capture_hook(hinfo, codec, substream,
4744 HDA_GEN_PCM_ACT_CLEANUP);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004745 return 0;
4746}
4747
Takashi Iwai352f7f92012-12-19 12:52:06 +01004748/*
4749 */
4750static const struct hda_pcm_stream pcm_analog_playback = {
4751 .substreams = 1,
4752 .channels_min = 2,
4753 .channels_max = 8,
4754 /* NID is set in build_pcms */
4755 .ops = {
4756 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004757 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004758 .prepare = playback_pcm_prepare,
4759 .cleanup = playback_pcm_cleanup
4760 },
4761};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762
Takashi Iwai352f7f92012-12-19 12:52:06 +01004763static const struct hda_pcm_stream pcm_analog_capture = {
4764 .substreams = 1,
4765 .channels_min = 2,
4766 .channels_max = 2,
4767 /* NID is set in build_pcms */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004768 .ops = {
4769 .open = capture_pcm_open,
4770 .close = capture_pcm_close,
4771 .prepare = capture_pcm_prepare,
4772 .cleanup = capture_pcm_cleanup
4773 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004774};
4775
4776static const struct hda_pcm_stream pcm_analog_alt_playback = {
4777 .substreams = 1,
4778 .channels_min = 2,
4779 .channels_max = 2,
4780 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004781 .ops = {
4782 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004783 .close = alt_playback_pcm_close,
4784 .prepare = alt_playback_pcm_prepare,
4785 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004786 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004787};
4788
4789static const struct hda_pcm_stream pcm_analog_alt_capture = {
4790 .substreams = 2, /* can be overridden */
4791 .channels_min = 2,
4792 .channels_max = 2,
4793 /* NID is set in build_pcms */
4794 .ops = {
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004795 .open = alt_capture_pcm_open,
4796 .close = alt_capture_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004797 .prepare = alt_capture_pcm_prepare,
4798 .cleanup = alt_capture_pcm_cleanup
4799 },
4800};
4801
4802static const struct hda_pcm_stream pcm_digital_playback = {
4803 .substreams = 1,
4804 .channels_min = 2,
4805 .channels_max = 2,
4806 /* NID is set in build_pcms */
4807 .ops = {
4808 .open = dig_playback_pcm_open,
4809 .close = dig_playback_pcm_close,
4810 .prepare = dig_playback_pcm_prepare,
4811 .cleanup = dig_playback_pcm_cleanup
4812 },
4813};
4814
4815static const struct hda_pcm_stream pcm_digital_capture = {
4816 .substreams = 1,
4817 .channels_min = 2,
4818 .channels_max = 2,
4819 /* NID is set in build_pcms */
4820};
4821
4822/* Used by build_pcms to flag that a PCM has no playback stream */
4823static const struct hda_pcm_stream pcm_null_stream = {
4824 .substreams = 0,
4825 .channels_min = 0,
4826 .channels_max = 0,
4827};
4828
4829/*
4830 * dynamic changing ADC PCM streams
4831 */
4832static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
4833{
4834 struct hda_gen_spec *spec = codec->spec;
4835 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
4836
4837 if (spec->cur_adc && spec->cur_adc != new_adc) {
4838 /* stream is running, let's swap the current ADC */
4839 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
4840 spec->cur_adc = new_adc;
4841 snd_hda_codec_setup_stream(codec, new_adc,
4842 spec->cur_adc_stream_tag, 0,
4843 spec->cur_adc_format);
4844 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004846 return false;
4847}
4848
4849/* analog capture with dynamic dual-adc changes */
4850static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4851 struct hda_codec *codec,
4852 unsigned int stream_tag,
4853 unsigned int format,
4854 struct snd_pcm_substream *substream)
4855{
4856 struct hda_gen_spec *spec = codec->spec;
4857 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
4858 spec->cur_adc_stream_tag = stream_tag;
4859 spec->cur_adc_format = format;
4860 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
4861 return 0;
4862}
4863
4864static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4865 struct hda_codec *codec,
4866 struct snd_pcm_substream *substream)
4867{
4868 struct hda_gen_spec *spec = codec->spec;
4869 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
4870 spec->cur_adc = 0;
4871 return 0;
4872}
4873
4874static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
4875 .substreams = 1,
4876 .channels_min = 2,
4877 .channels_max = 2,
4878 .nid = 0, /* fill later */
4879 .ops = {
4880 .prepare = dyn_adc_capture_pcm_prepare,
4881 .cleanup = dyn_adc_capture_pcm_cleanup
4882 },
4883};
4884
Takashi Iwaif873e532012-12-20 16:58:39 +01004885static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
4886 const char *chip_name)
4887{
4888 char *p;
4889
4890 if (*str)
4891 return;
4892 strlcpy(str, chip_name, len);
4893
4894 /* drop non-alnum chars after a space */
4895 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
4896 if (!isalnum(p[1])) {
4897 *p = 0;
4898 break;
4899 }
4900 }
4901 strlcat(str, sfx, len);
4902}
4903
Takashi Iwai352f7f92012-12-19 12:52:06 +01004904/* build PCM streams based on the parsed results */
4905int snd_hda_gen_build_pcms(struct hda_codec *codec)
4906{
4907 struct hda_gen_spec *spec = codec->spec;
4908 struct hda_pcm *info = spec->pcm_rec;
4909 const struct hda_pcm_stream *p;
4910 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911
4912 codec->num_pcms = 1;
4913 codec->pcm_info = info;
4914
Takashi Iwai352f7f92012-12-19 12:52:06 +01004915 if (spec->no_analog)
4916 goto skip_analog;
4917
Takashi Iwaif873e532012-12-20 16:58:39 +01004918 fill_pcm_stream_name(spec->stream_name_analog,
4919 sizeof(spec->stream_name_analog),
4920 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004921 info->name = spec->stream_name_analog;
4922
4923 if (spec->multiout.num_dacs > 0) {
4924 p = spec->stream_analog_playback;
4925 if (!p)
4926 p = &pcm_analog_playback;
4927 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4928 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
4929 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
4930 spec->multiout.max_channels;
4931 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
4932 spec->autocfg.line_outs == 2)
4933 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
4934 snd_pcm_2_1_chmaps;
4935 }
4936 if (spec->num_adc_nids) {
4937 p = spec->stream_analog_capture;
4938 if (!p) {
4939 if (spec->dyn_adc_switch)
4940 p = &dyn_adc_pcm_analog_capture;
4941 else
4942 p = &pcm_analog_capture;
4943 }
4944 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4945 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
4946 }
4947
Takashi Iwai352f7f92012-12-19 12:52:06 +01004948 skip_analog:
4949 /* SPDIF for stream index #1 */
4950 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01004951 fill_pcm_stream_name(spec->stream_name_digital,
4952 sizeof(spec->stream_name_digital),
4953 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004954 codec->num_pcms = 2;
4955 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
4956 info = spec->pcm_rec + 1;
4957 info->name = spec->stream_name_digital;
4958 if (spec->dig_out_type)
4959 info->pcm_type = spec->dig_out_type;
4960 else
4961 info->pcm_type = HDA_PCM_TYPE_SPDIF;
4962 if (spec->multiout.dig_out_nid) {
4963 p = spec->stream_digital_playback;
4964 if (!p)
4965 p = &pcm_digital_playback;
4966 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4967 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
4968 }
4969 if (spec->dig_in_nid) {
4970 p = spec->stream_digital_capture;
4971 if (!p)
4972 p = &pcm_digital_capture;
4973 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4974 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
4975 }
4976 }
4977
4978 if (spec->no_analog)
4979 return 0;
4980
4981 /* If the use of more than one ADC is requested for the current
4982 * model, configure a second analog capture-only PCM.
4983 */
4984 have_multi_adcs = (spec->num_adc_nids > 1) &&
4985 !spec->dyn_adc_switch && !spec->auto_mic;
4986 /* Additional Analaog capture for index #2 */
4987 if (spec->alt_dac_nid || have_multi_adcs) {
Takashi Iwaia6071482013-01-21 16:50:09 +01004988 fill_pcm_stream_name(spec->stream_name_alt_analog,
4989 sizeof(spec->stream_name_alt_analog),
4990 " Alt Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004991 codec->num_pcms = 3;
4992 info = spec->pcm_rec + 2;
Takashi Iwaia6071482013-01-21 16:50:09 +01004993 info->name = spec->stream_name_alt_analog;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004994 if (spec->alt_dac_nid) {
4995 p = spec->stream_analog_alt_playback;
4996 if (!p)
4997 p = &pcm_analog_alt_playback;
4998 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4999 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
5000 spec->alt_dac_nid;
5001 } else {
5002 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
5003 pcm_null_stream;
5004 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
5005 }
5006 if (have_multi_adcs) {
5007 p = spec->stream_analog_alt_capture;
5008 if (!p)
5009 p = &pcm_analog_alt_capture;
5010 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
5011 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
5012 spec->adc_nids[1];
5013 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
5014 spec->num_adc_nids - 1;
5015 } else {
5016 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
5017 pcm_null_stream;
5018 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
5019 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020 }
5021
5022 return 0;
5023}
Takashi Iwai352f7f92012-12-19 12:52:06 +01005024EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
5025
5026
5027/*
5028 * Standard auto-parser initializations
5029 */
5030
Takashi Iwaid4156932013-01-07 10:08:02 +01005031/* configure the given path as a proper output */
Takashi Iwai2c12c302013-01-10 09:33:29 +01005032static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005033{
5034 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01005035 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005036
Takashi Iwai196c17662013-01-04 15:01:40 +01005037 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01005038 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005039 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01005040 pin = path->path[path->depth - 1];
Takashi Iwai2c12c302013-01-10 09:33:29 +01005041 restore_pin_ctl(codec, pin);
Takashi Iwai65033cc2013-04-16 12:31:05 +02005042 snd_hda_activate_path(codec, path, path->active,
5043 aamix_default(codec->spec));
Takashi Iwaie1284af2013-01-03 16:33:02 +01005044 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005045}
5046
5047/* initialize primary output paths */
5048static void init_multi_out(struct hda_codec *codec)
5049{
5050 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005051 int i;
5052
Takashi Iwaid4156932013-01-07 10:08:02 +01005053 for (i = 0; i < spec->autocfg.line_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005054 set_output_and_unmute(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005055}
5056
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005057
Takashi Iwai2c12c302013-01-10 09:33:29 +01005058static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005059{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005060 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005061
Takashi Iwaid4156932013-01-07 10:08:02 +01005062 for (i = 0; i < num_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005063 set_output_and_unmute(codec, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005064}
5065
5066/* initialize hp and speaker paths */
5067static void init_extra_out(struct hda_codec *codec)
5068{
5069 struct hda_gen_spec *spec = codec->spec;
5070
5071 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005072 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005073 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
5074 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai2c12c302013-01-10 09:33:29 +01005075 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005076}
5077
5078/* initialize multi-io paths */
5079static void init_multi_io(struct hda_codec *codec)
5080{
5081 struct hda_gen_spec *spec = codec->spec;
5082 int i;
5083
5084 for (i = 0; i < spec->multi_ios; i++) {
5085 hda_nid_t pin = spec->multi_io[i].pin;
5086 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01005087 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005088 if (!path)
5089 continue;
5090 if (!spec->multi_io[i].ctl_in)
5091 spec->multi_io[i].ctl_in =
Takashi Iwai2c12c302013-01-10 09:33:29 +01005092 snd_hda_codec_get_pin_target(codec, pin);
Takashi Iwai65033cc2013-04-16 12:31:05 +02005093 snd_hda_activate_path(codec, path, path->active,
5094 aamix_default(spec));
Takashi Iwai352f7f92012-12-19 12:52:06 +01005095 }
5096}
5097
Takashi Iwai352f7f92012-12-19 12:52:06 +01005098/* set up input pins and loopback paths */
5099static void init_analog_input(struct hda_codec *codec)
5100{
5101 struct hda_gen_spec *spec = codec->spec;
5102 struct auto_pin_cfg *cfg = &spec->autocfg;
5103 int i;
5104
5105 for (i = 0; i < cfg->num_inputs; i++) {
5106 hda_nid_t nid = cfg->inputs[i].pin;
5107 if (is_input_pin(codec, nid))
Takashi Iwai2c12c302013-01-10 09:33:29 +01005108 restore_pin_ctl(codec, nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005109
5110 /* init loopback inputs */
5111 if (spec->mixer_nid) {
Takashi Iwai3e367f12013-01-23 17:07:23 +01005112 resume_path_from_idx(codec, spec->loopback_paths[i]);
5113 resume_path_from_idx(codec, spec->loopback_merge_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005114 }
5115 }
5116}
5117
5118/* initialize ADC paths */
5119static void init_input_src(struct hda_codec *codec)
5120{
5121 struct hda_gen_spec *spec = codec->spec;
5122 struct hda_input_mux *imux = &spec->input_mux;
5123 struct nid_path *path;
5124 int i, c, nums;
5125
5126 if (spec->dyn_adc_switch)
5127 nums = 1;
5128 else
5129 nums = spec->num_adc_nids;
5130
5131 for (c = 0; c < nums; c++) {
5132 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01005133 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005134 if (path) {
5135 bool active = path->active;
5136 if (i == spec->cur_mux[c])
5137 active = true;
5138 snd_hda_activate_path(codec, path, active, false);
5139 }
5140 }
Takashi Iwai967303d2013-02-19 17:12:42 +01005141 if (spec->hp_mic)
5142 update_hp_mic(codec, c, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005143 }
5144
Takashi Iwai352f7f92012-12-19 12:52:06 +01005145 if (spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01005146 spec->cap_sync_hook(codec, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005147}
5148
5149/* set right pin controls for digital I/O */
5150static void init_digital(struct hda_codec *codec)
5151{
5152 struct hda_gen_spec *spec = codec->spec;
5153 int i;
5154 hda_nid_t pin;
5155
Takashi Iwaid4156932013-01-07 10:08:02 +01005156 for (i = 0; i < spec->autocfg.dig_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005157 set_output_and_unmute(codec, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005158 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005159 if (pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01005160 restore_pin_ctl(codec, pin);
Takashi Iwai3e367f12013-01-23 17:07:23 +01005161 resume_path_from_idx(codec, spec->digin_path);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005162 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005163}
5164
Takashi Iwai973e4972012-12-20 15:16:09 +01005165/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5166 * invalid unsol tags by some reason
5167 */
5168static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5169{
5170 int i;
5171
5172 for (i = 0; i < codec->init_pins.used; i++) {
5173 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
5174 hda_nid_t nid = pin->nid;
5175 if (is_jack_detectable(codec, nid) &&
5176 !snd_hda_jack_tbl_get(codec, nid))
5177 snd_hda_codec_update_cache(codec, nid, 0,
5178 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5179 }
5180}
5181
Takashi Iwai5187ac12013-01-07 12:52:16 +01005182/*
5183 * initialize the generic spec;
5184 * this can be put as patch_ops.init function
5185 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005186int snd_hda_gen_init(struct hda_codec *codec)
5187{
5188 struct hda_gen_spec *spec = codec->spec;
5189
5190 if (spec->init_hook)
5191 spec->init_hook(codec);
5192
5193 snd_hda_apply_verbs(codec);
5194
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005195 codec->cached_write = 1;
5196
Takashi Iwai352f7f92012-12-19 12:52:06 +01005197 init_multi_out(codec);
5198 init_extra_out(codec);
5199 init_multi_io(codec);
5200 init_analog_input(codec);
5201 init_input_src(codec);
5202 init_digital(codec);
5203
Takashi Iwai973e4972012-12-20 15:16:09 +01005204 clear_unsol_on_unused_pins(codec);
5205
Takashi Iwai352f7f92012-12-19 12:52:06 +01005206 /* call init functions of standard auto-mute helpers */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01005207 update_automute_all(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005208
Takashi Iwaidc870f32013-01-22 15:24:30 +01005209 snd_hda_codec_flush_cache(codec);
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005210
Takashi Iwai352f7f92012-12-19 12:52:06 +01005211 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
5212 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5213
5214 hda_call_check_power_status(codec, 0x01);
5215 return 0;
5216}
Takashi Iwaifce52a32013-01-07 12:42:48 +01005217EXPORT_SYMBOL_HDA(snd_hda_gen_init);
5218
Takashi Iwai5187ac12013-01-07 12:52:16 +01005219/*
5220 * free the generic spec;
5221 * this can be put as patch_ops.free function
5222 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005223void snd_hda_gen_free(struct hda_codec *codec)
5224{
Takashi Iwai7504b6c2013-03-18 11:25:51 +01005225 snd_hda_detach_beep_device(codec);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005226 snd_hda_gen_spec_free(codec->spec);
5227 kfree(codec->spec);
5228 codec->spec = NULL;
5229}
5230EXPORT_SYMBOL_HDA(snd_hda_gen_free);
5231
5232#ifdef CONFIG_PM
Takashi Iwai5187ac12013-01-07 12:52:16 +01005233/*
5234 * check the loopback power save state;
5235 * this can be put as patch_ops.check_power_status function
5236 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005237int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
5238{
5239 struct hda_gen_spec *spec = codec->spec;
5240 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
5241}
5242EXPORT_SYMBOL_HDA(snd_hda_gen_check_power_status);
5243#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01005244
5245
5246/*
5247 * the generic codec support
5248 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249
Takashi Iwai352f7f92012-12-19 12:52:06 +01005250static const struct hda_codec_ops generic_patch_ops = {
5251 .build_controls = snd_hda_gen_build_controls,
5252 .build_pcms = snd_hda_gen_build_pcms,
5253 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01005254 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005255 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02005256#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01005257 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02005258#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259};
5260
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261int snd_hda_parse_generic_codec(struct hda_codec *codec)
5262{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005263 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005264 int err;
5265
Takashi Iwaie560d8d2005-09-09 14:21:46 +02005266 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005267 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005269 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005271
Takashi Iwai9eb413e2012-12-19 14:41:21 +01005272 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
5273 if (err < 0)
5274 return err;
5275
5276 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005277 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005278 goto error;
5279
5280 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281 return 0;
5282
Takashi Iwai352f7f92012-12-19 12:52:06 +01005283error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01005284 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285 return err;
5286}
Takashi Iwaifce52a32013-01-07 12:42:48 +01005287EXPORT_SYMBOL_HDA(snd_hda_parse_generic_codec);