blob: f6c0344258ac579154345f15ca15ff80a97ec144 [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) \
528 check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
529#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 }
630 if (caps & AC_AMPCAP_MUTE) {
631 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
654 if (caps & AC_AMPCAP_MUTE) {
655 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);
819
Takashi Iwai352f7f92012-12-19 12:52:06 +0100820enum {
821 HDA_CTL_WIDGET_VOL,
822 HDA_CTL_WIDGET_MUTE,
823 HDA_CTL_BIND_MUTE,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100824};
825static const struct snd_kcontrol_new control_templates[] = {
826 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200827 /* only the put callback is replaced for handling the special mute */
828 {
829 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
830 .subdevice = HDA_SUBDEV_AMP_FLAG,
831 .info = snd_hda_mixer_amp_switch_info,
832 .get = snd_hda_mixer_amp_switch_get,
833 .put = hda_gen_mixer_mute_put, /* replaced */
834 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
835 },
Takashi Iwai352f7f92012-12-19 12:52:06 +0100836 HDA_BIND_MUTE(NULL, 0, 0, 0),
Takashi Iwai352f7f92012-12-19 12:52:06 +0100837};
838
839/* add dynamic controls from template */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100840static struct snd_kcontrol_new *
841add_control(struct hda_gen_spec *spec, int type, const char *name,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100842 int cidx, unsigned long val)
843{
844 struct snd_kcontrol_new *knew;
845
Takashi Iwai12c93df2012-12-19 14:38:33 +0100846 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100847 if (!knew)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100848 return NULL;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100849 knew->index = cidx;
850 if (get_amp_nid_(val))
851 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
852 knew->private_value = val;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100853 return knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100854}
855
856static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
857 const char *pfx, const char *dir,
858 const char *sfx, int cidx, unsigned long val)
859{
Takashi Iwai975cc022013-06-28 11:56:49 +0200860 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai352f7f92012-12-19 12:52:06 +0100861 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100862 if (!add_control(spec, type, name, cidx, val))
863 return -ENOMEM;
864 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100865}
866
867#define add_pb_vol_ctrl(spec, type, pfx, val) \
868 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
869#define add_pb_sw_ctrl(spec, type, pfx, val) \
870 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
871#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
872 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
873#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
874 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
875
876static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
877 unsigned int chs, struct nid_path *path)
878{
879 unsigned int val;
880 if (!path)
881 return 0;
882 val = path->ctls[NID_PATH_VOL_CTL];
883 if (!val)
884 return 0;
885 val = amp_val_replace_channels(val, chs);
886 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
887}
888
889/* return the channel bits suitable for the given path->ctls[] */
890static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
891 int type)
892{
893 int chs = 1; /* mono (left only) */
894 if (path) {
895 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
896 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
897 chs = 3; /* stereo */
898 }
899 return chs;
900}
901
902static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
903 struct nid_path *path)
904{
905 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
906 return add_vol_ctl(codec, pfx, cidx, chs, path);
907}
908
909/* create a mute-switch for the given mixer widget;
910 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
911 */
912static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
913 unsigned int chs, struct nid_path *path)
914{
915 unsigned int val;
916 int type = HDA_CTL_WIDGET_MUTE;
917
918 if (!path)
919 return 0;
920 val = path->ctls[NID_PATH_MUTE_CTL];
921 if (!val)
922 return 0;
923 val = amp_val_replace_channels(val, chs);
924 if (get_amp_direction_(val) == HDA_INPUT) {
925 hda_nid_t nid = get_amp_nid_(val);
926 int nums = snd_hda_get_num_conns(codec, nid);
927 if (nums > 1) {
928 type = HDA_CTL_BIND_MUTE;
929 val |= nums << 19;
930 }
931 }
932 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
933}
934
935static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
936 int cidx, struct nid_path *path)
937{
938 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
939 return add_sw_ctl(codec, pfx, cidx, chs, path);
940}
941
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200942/* playback mute control with the software mute bit check */
943static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
944 struct snd_ctl_elem_value *ucontrol)
945{
946 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
947 struct hda_gen_spec *spec = codec->spec;
948
949 if (spec->auto_mute_via_amp) {
950 hda_nid_t nid = get_amp_nid(kcontrol);
951 bool enabled = !((spec->mute_bits >> nid) & 1);
952 ucontrol->value.integer.value[0] &= enabled;
953 ucontrol->value.integer.value[1] &= enabled;
954 }
955
956 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
957}
958
Takashi Iwai247d85e2013-01-17 16:18:11 +0100959/* any ctl assigned to the path with the given index? */
960static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
961{
962 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
963 return path && path->ctls[ctl_type];
964}
965
Takashi Iwai352f7f92012-12-19 12:52:06 +0100966static const char * const channel_name[4] = {
967 "Front", "Surround", "CLFE", "Side"
968};
969
970/* give some appropriate ctl name prefix for the given line out channel */
Takashi Iwai247d85e2013-01-17 16:18:11 +0100971static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
972 int *index, int ctl_type)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100973{
Takashi Iwai247d85e2013-01-17 16:18:11 +0100974 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100975 struct auto_pin_cfg *cfg = &spec->autocfg;
976
977 *index = 0;
978 if (cfg->line_outs == 1 && !spec->multi_ios &&
Takashi Iwai247d85e2013-01-17 16:18:11 +0100979 !cfg->hp_outs && !cfg->speaker_outs)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100980 return spec->vmaster_mute.hook ? "PCM" : "Master";
981
982 /* if there is really a single DAC used in the whole output paths,
983 * use it master (or "PCM" if a vmaster hook is present)
984 */
985 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
986 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
987 return spec->vmaster_mute.hook ? "PCM" : "Master";
988
Takashi Iwai247d85e2013-01-17 16:18:11 +0100989 /* multi-io channels */
990 if (ch >= cfg->line_outs)
991 return channel_name[ch];
992
Takashi Iwai352f7f92012-12-19 12:52:06 +0100993 switch (cfg->line_out_type) {
994 case AUTO_PIN_SPEAKER_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +0100995 /* if the primary channel vol/mute is shared with HP volume,
996 * don't name it as Speaker
997 */
998 if (!ch && cfg->hp_outs &&
999 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
1000 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001001 if (cfg->line_outs == 1)
1002 return "Speaker";
1003 if (cfg->line_outs == 2)
1004 return ch ? "Bass Speaker" : "Speaker";
1005 break;
1006 case AUTO_PIN_HP_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +01001007 /* if the primary channel vol/mute is shared with spk volume,
1008 * don't name it as Headphone
1009 */
1010 if (!ch && cfg->speaker_outs &&
1011 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
1012 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001013 /* for multi-io case, only the primary out */
1014 if (ch && spec->multi_ios)
1015 break;
1016 *index = ch;
1017 return "Headphone";
Takashi Iwai352f7f92012-12-19 12:52:06 +01001018 }
Takashi Iwai247d85e2013-01-17 16:18:11 +01001019
1020 /* for a single channel output, we don't have to name the channel */
1021 if (cfg->line_outs == 1 && !spec->multi_ios)
1022 return "PCM";
1023
Takashi Iwai352f7f92012-12-19 12:52:06 +01001024 if (ch >= ARRAY_SIZE(channel_name)) {
1025 snd_BUG();
1026 return "PCM";
1027 }
1028
1029 return channel_name[ch];
1030}
1031
1032/*
1033 * Parse output paths
1034 */
1035
1036/* badness definition */
1037enum {
1038 /* No primary DAC is found for the main output */
1039 BAD_NO_PRIMARY_DAC = 0x10000,
1040 /* No DAC is found for the extra output */
1041 BAD_NO_DAC = 0x4000,
1042 /* No possible multi-ios */
Takashi Iwai1d739062013-02-13 14:17:32 +01001043 BAD_MULTI_IO = 0x120,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001044 /* No individual DAC for extra output */
1045 BAD_NO_EXTRA_DAC = 0x102,
1046 /* No individual DAC for extra surrounds */
1047 BAD_NO_EXTRA_SURR_DAC = 0x101,
1048 /* Primary DAC shared with main surrounds */
1049 BAD_SHARED_SURROUND = 0x100,
Takashi Iwai55a63d42013-03-21 17:20:12 +01001050 /* No independent HP possible */
Takashi Iwaibec8e682013-03-22 15:10:08 +01001051 BAD_NO_INDEP_HP = 0x10,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001052 /* Primary DAC shared with main CLFE */
1053 BAD_SHARED_CLFE = 0x10,
1054 /* Primary DAC shared with extra surrounds */
1055 BAD_SHARED_EXTRA_SURROUND = 0x10,
1056 /* Volume widget is shared */
1057 BAD_SHARED_VOL = 0x10,
1058};
1059
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001060/* look for widgets in the given path which are appropriate for
Takashi Iwai352f7f92012-12-19 12:52:06 +01001061 * volume and mute controls, and assign the values to ctls[].
1062 *
1063 * When no appropriate widget is found in the path, the badness value
1064 * is incremented depending on the situation. The function returns the
1065 * total badness for both volume and mute controls.
1066 */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001067static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001068{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001069 hda_nid_t nid;
1070 unsigned int val;
1071 int badness = 0;
1072
1073 if (!path)
1074 return BAD_SHARED_VOL * 2;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001075
1076 if (path->ctls[NID_PATH_VOL_CTL] ||
1077 path->ctls[NID_PATH_MUTE_CTL])
1078 return 0; /* already evaluated */
1079
Takashi Iwai352f7f92012-12-19 12:52:06 +01001080 nid = look_for_out_vol_nid(codec, path);
1081 if (nid) {
1082 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1083 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1084 badness += BAD_SHARED_VOL;
1085 else
1086 path->ctls[NID_PATH_VOL_CTL] = val;
1087 } else
1088 badness += BAD_SHARED_VOL;
1089 nid = look_for_out_mute_nid(codec, path);
1090 if (nid) {
1091 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1092 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1093 nid_has_mute(codec, nid, HDA_OUTPUT))
1094 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1095 else
1096 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1097 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1098 badness += BAD_SHARED_VOL;
1099 else
1100 path->ctls[NID_PATH_MUTE_CTL] = val;
1101 } else
1102 badness += BAD_SHARED_VOL;
1103 return badness;
1104}
1105
Takashi Iwai98bd1112013-03-22 14:53:50 +01001106const struct badness_table hda_main_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001107 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1108 .no_dac = BAD_NO_DAC,
1109 .shared_primary = BAD_NO_PRIMARY_DAC,
1110 .shared_surr = BAD_SHARED_SURROUND,
1111 .shared_clfe = BAD_SHARED_CLFE,
1112 .shared_surr_main = BAD_SHARED_SURROUND,
1113};
Takashi Iwai98bd1112013-03-22 14:53:50 +01001114EXPORT_SYMBOL_HDA(hda_main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001115
Takashi Iwai98bd1112013-03-22 14:53:50 +01001116const struct badness_table hda_extra_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001117 .no_primary_dac = BAD_NO_DAC,
1118 .no_dac = BAD_NO_DAC,
1119 .shared_primary = BAD_NO_EXTRA_DAC,
1120 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1121 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1122 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1123};
Takashi Iwai98bd1112013-03-22 14:53:50 +01001124EXPORT_SYMBOL_HDA(hda_extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001125
Takashi Iwai7385df62013-01-07 09:50:52 +01001126/* get the DAC of the primary output corresponding to the given array index */
1127static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1128{
1129 struct hda_gen_spec *spec = codec->spec;
1130 struct auto_pin_cfg *cfg = &spec->autocfg;
1131
1132 if (cfg->line_outs > idx)
1133 return spec->private_dac_nids[idx];
1134 idx -= cfg->line_outs;
1135 if (spec->multi_ios > idx)
1136 return spec->multi_io[idx].dac;
1137 return 0;
1138}
1139
1140/* return the DAC if it's reachable, otherwise zero */
1141static inline hda_nid_t try_dac(struct hda_codec *codec,
1142 hda_nid_t dac, hda_nid_t pin)
1143{
1144 return is_reachable_path(codec, dac, pin) ? dac : 0;
1145}
1146
Takashi Iwai352f7f92012-12-19 12:52:06 +01001147/* try to assign DACs to pins and return the resultant badness */
1148static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1149 const hda_nid_t *pins, hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001150 int *path_idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001151 const struct badness_table *bad)
1152{
1153 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001154 int i, j;
1155 int badness = 0;
1156 hda_nid_t dac;
1157
1158 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 return 0;
1160
Takashi Iwai352f7f92012-12-19 12:52:06 +01001161 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001162 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001163 hda_nid_t pin = pins[i];
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001164
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001165 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1166 if (path) {
1167 badness += assign_out_path_ctls(codec, path);
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001168 continue;
1169 }
1170
1171 dacs[i] = look_for_dac(codec, pin, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001172 if (!dacs[i] && !i) {
Takashi Iwai980428c2013-01-09 09:28:20 +01001173 /* try to steal the DAC of surrounds for the front */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001174 for (j = 1; j < num_outs; j++) {
1175 if (is_reachable_path(codec, dacs[j], pin)) {
1176 dacs[0] = dacs[j];
1177 dacs[j] = 0;
Takashi Iwai980428c2013-01-09 09:28:20 +01001178 invalidate_nid_path(codec, path_idx[j]);
Takashi Iwai196c17662013-01-04 15:01:40 +01001179 path_idx[j] = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001180 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 }
1182 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001183 }
1184 dac = dacs[i];
1185 if (!dac) {
Takashi Iwai7385df62013-01-07 09:50:52 +01001186 if (num_outs > 2)
1187 dac = try_dac(codec, get_primary_out(codec, i), pin);
1188 if (!dac)
1189 dac = try_dac(codec, dacs[0], pin);
1190 if (!dac)
1191 dac = try_dac(codec, get_primary_out(codec, i), pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001192 if (dac) {
1193 if (!i)
1194 badness += bad->shared_primary;
1195 else if (i == 1)
1196 badness += bad->shared_surr;
1197 else
1198 badness += bad->shared_clfe;
1199 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1200 dac = spec->private_dac_nids[0];
1201 badness += bad->shared_surr_main;
1202 } else if (!i)
1203 badness += bad->no_primary_dac;
1204 else
1205 badness += bad->no_dac;
1206 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001207 if (!dac)
1208 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001209 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001210 if (!path && !i && spec->mixer_nid) {
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001211 /* try with aamix */
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001212 path = snd_hda_add_new_path(codec, dac, pin, 0);
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001213 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001214 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001215 dac = dacs[i] = 0;
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001216 badness += bad->no_dac;
1217 } else {
Takashi Iwaia7694092013-01-21 10:43:18 +01001218 /* print_nid_path("output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001219 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001220 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001221 badness += assign_out_path_ctls(codec, path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001222 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001223 }
1224
1225 return badness;
1226}
1227
1228/* return NID if the given pin has only a single connection to a certain DAC */
1229static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1230{
1231 struct hda_gen_spec *spec = codec->spec;
1232 int i;
1233 hda_nid_t nid_found = 0;
1234
1235 for (i = 0; i < spec->num_all_dacs; i++) {
1236 hda_nid_t nid = spec->all_dacs[i];
1237 if (!nid || is_dac_already_used(codec, nid))
1238 continue;
1239 if (is_reachable_path(codec, nid, pin)) {
1240 if (nid_found)
1241 return 0;
1242 nid_found = nid;
1243 }
1244 }
1245 return nid_found;
1246}
1247
1248/* check whether the given pin can be a multi-io pin */
1249static bool can_be_multiio_pin(struct hda_codec *codec,
1250 unsigned int location, hda_nid_t nid)
1251{
1252 unsigned int defcfg, caps;
1253
1254 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1255 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1256 return false;
1257 if (location && get_defcfg_location(defcfg) != location)
1258 return false;
1259 caps = snd_hda_query_pin_caps(codec, nid);
1260 if (!(caps & AC_PINCAP_OUT))
1261 return false;
1262 return true;
1263}
1264
Takashi Iwaie22aab72013-01-04 14:50:04 +01001265/* count the number of input pins that are capable to be multi-io */
1266static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1267{
1268 struct hda_gen_spec *spec = codec->spec;
1269 struct auto_pin_cfg *cfg = &spec->autocfg;
1270 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1271 unsigned int location = get_defcfg_location(defcfg);
1272 int type, i;
1273 int num_pins = 0;
1274
1275 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1276 for (i = 0; i < cfg->num_inputs; i++) {
1277 if (cfg->inputs[i].type != type)
1278 continue;
1279 if (can_be_multiio_pin(codec, location,
1280 cfg->inputs[i].pin))
1281 num_pins++;
1282 }
1283 }
1284 return num_pins;
1285}
1286
Takashi Iwai352f7f92012-12-19 12:52:06 +01001287/*
1288 * multi-io helper
1289 *
1290 * When hardwired is set, try to fill ony hardwired pins, and returns
1291 * zero if any pins are filled, non-zero if nothing found.
1292 * When hardwired is off, try to fill possible input pins, and returns
1293 * the badness value.
1294 */
1295static int fill_multi_ios(struct hda_codec *codec,
1296 hda_nid_t reference_pin,
Takashi Iwaie22aab72013-01-04 14:50:04 +01001297 bool hardwired)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001298{
1299 struct hda_gen_spec *spec = codec->spec;
1300 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie22aab72013-01-04 14:50:04 +01001301 int type, i, j, num_pins, old_pins;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001302 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1303 unsigned int location = get_defcfg_location(defcfg);
1304 int badness = 0;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001305 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001306
1307 old_pins = spec->multi_ios;
1308 if (old_pins >= 2)
1309 goto end_fill;
1310
Takashi Iwaie22aab72013-01-04 14:50:04 +01001311 num_pins = count_multiio_pins(codec, reference_pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001312 if (num_pins < 2)
1313 goto end_fill;
1314
Takashi Iwai352f7f92012-12-19 12:52:06 +01001315 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1316 for (i = 0; i < cfg->num_inputs; i++) {
1317 hda_nid_t nid = cfg->inputs[i].pin;
1318 hda_nid_t dac = 0;
1319
1320 if (cfg->inputs[i].type != type)
1321 continue;
1322 if (!can_be_multiio_pin(codec, location, nid))
1323 continue;
1324 for (j = 0; j < spec->multi_ios; j++) {
1325 if (nid == spec->multi_io[j].pin)
1326 break;
1327 }
1328 if (j < spec->multi_ios)
1329 continue;
1330
Takashi Iwai352f7f92012-12-19 12:52:06 +01001331 if (hardwired)
1332 dac = get_dac_if_single(codec, nid);
1333 else if (!dac)
1334 dac = look_for_dac(codec, nid, false);
1335 if (!dac) {
1336 badness++;
1337 continue;
1338 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001339 path = snd_hda_add_new_path(codec, dac, nid,
1340 -spec->mixer_nid);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001341 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001342 badness++;
1343 continue;
1344 }
Takashi Iwaia7694092013-01-21 10:43:18 +01001345 /* print_nid_path("multiio", path); */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001346 spec->multi_io[spec->multi_ios].pin = nid;
1347 spec->multi_io[spec->multi_ios].dac = dac;
Takashi Iwai196c17662013-01-04 15:01:40 +01001348 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1349 snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001350 spec->multi_ios++;
1351 if (spec->multi_ios >= 2)
1352 break;
1353 }
1354 }
1355 end_fill:
1356 if (badness)
1357 badness = BAD_MULTI_IO;
1358 if (old_pins == spec->multi_ios) {
1359 if (hardwired)
1360 return 1; /* nothing found */
1361 else
1362 return badness; /* no badness if nothing found */
1363 }
1364 if (!hardwired && spec->multi_ios < 2) {
1365 /* cancel newly assigned paths */
1366 spec->paths.used -= spec->multi_ios - old_pins;
1367 spec->multi_ios = old_pins;
1368 return badness;
1369 }
1370
1371 /* assign volume and mute controls */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001372 for (i = old_pins; i < spec->multi_ios; i++) {
1373 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1374 badness += assign_out_path_ctls(codec, path);
1375 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001376
1377 return badness;
1378}
1379
1380/* map DACs for all pins in the list if they are single connections */
1381static bool map_singles(struct hda_codec *codec, int outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001382 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001383{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001384 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001385 int i;
1386 bool found = false;
1387 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001388 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001389 hda_nid_t dac;
1390 if (dacs[i])
1391 continue;
1392 dac = get_dac_if_single(codec, pins[i]);
1393 if (!dac)
1394 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001395 path = snd_hda_add_new_path(codec, dac, pins[i],
1396 -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001397 if (!path && !i && spec->mixer_nid)
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001398 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001399 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001400 dacs[i] = dac;
1401 found = true;
Takashi Iwaia7694092013-01-21 10:43:18 +01001402 /* print_nid_path("output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001403 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001404 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001405 }
1406 }
1407 return found;
1408}
1409
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001410/* create a new path including aamix if available, and return its index */
1411static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1412{
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001413 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001414 struct nid_path *path;
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001415 hda_nid_t path_dac, dac, pin;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001416
1417 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001418 if (!path || !path->depth ||
1419 is_nid_contained(path, spec->mixer_nid))
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001420 return 0;
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001421 path_dac = path->path[0];
1422 dac = spec->private_dac_nids[0];
Takashi Iwaif87498b2013-01-21 14:24:31 +01001423 pin = path->path[path->depth - 1];
1424 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1425 if (!path) {
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001426 if (dac != path_dac)
1427 dac = path_dac;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001428 else if (spec->multiout.hp_out_nid[0])
1429 dac = spec->multiout.hp_out_nid[0];
1430 else if (spec->multiout.extra_out_nid[0])
1431 dac = spec->multiout.extra_out_nid[0];
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001432 else
1433 dac = 0;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001434 if (dac)
1435 path = snd_hda_add_new_path(codec, dac, pin,
1436 spec->mixer_nid);
1437 }
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001438 if (!path)
1439 return 0;
Takashi Iwaia7694092013-01-21 10:43:18 +01001440 /* print_nid_path("output-aamix", path); */
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001441 path->active = false; /* unused as default */
1442 return snd_hda_get_path_idx(codec, path);
1443}
1444
Takashi Iwai55a63d42013-03-21 17:20:12 +01001445/* check whether the independent HP is available with the current config */
1446static bool indep_hp_possible(struct hda_codec *codec)
1447{
1448 struct hda_gen_spec *spec = codec->spec;
1449 struct auto_pin_cfg *cfg = &spec->autocfg;
1450 struct nid_path *path;
1451 int i, idx;
1452
1453 if (cfg->line_out_type == AUTO_PIN_HP_OUT)
1454 idx = spec->out_paths[0];
1455 else
1456 idx = spec->hp_paths[0];
1457 path = snd_hda_get_path_from_idx(codec, idx);
1458 if (!path)
1459 return false;
1460
1461 /* assume no path conflicts unless aamix is involved */
1462 if (!spec->mixer_nid || !is_nid_contained(path, spec->mixer_nid))
1463 return true;
1464
1465 /* check whether output paths contain aamix */
1466 for (i = 0; i < cfg->line_outs; i++) {
1467 if (spec->out_paths[i] == idx)
1468 break;
1469 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1470 if (path && is_nid_contained(path, spec->mixer_nid))
1471 return false;
1472 }
1473 for (i = 0; i < cfg->speaker_outs; i++) {
1474 path = snd_hda_get_path_from_idx(codec, spec->speaker_paths[i]);
1475 if (path && is_nid_contained(path, spec->mixer_nid))
1476 return false;
1477 }
1478
1479 return true;
1480}
1481
Takashi Iwaia07a9492013-01-07 16:44:06 +01001482/* fill the empty entries in the dac array for speaker/hp with the
1483 * shared dac pointed by the paths
1484 */
1485static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1486 hda_nid_t *dacs, int *path_idx)
1487{
1488 struct nid_path *path;
1489 int i;
1490
1491 for (i = 0; i < num_outs; i++) {
1492 if (dacs[i])
1493 continue;
1494 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1495 if (!path)
1496 continue;
1497 dacs[i] = path->path[0];
1498 }
1499}
1500
Takashi Iwai352f7f92012-12-19 12:52:06 +01001501/* fill in the dac_nids table from the parsed pin configuration */
1502static int fill_and_eval_dacs(struct hda_codec *codec,
1503 bool fill_hardwired,
1504 bool fill_mio_first)
1505{
1506 struct hda_gen_spec *spec = codec->spec;
1507 struct auto_pin_cfg *cfg = &spec->autocfg;
1508 int i, err, badness;
1509
1510 /* set num_dacs once to full for look_for_dac() */
1511 spec->multiout.num_dacs = cfg->line_outs;
1512 spec->multiout.dac_nids = spec->private_dac_nids;
1513 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1514 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1515 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1516 spec->multi_ios = 0;
1517 snd_array_free(&spec->paths);
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001518
1519 /* clear path indices */
1520 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1521 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1522 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1523 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1524 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
Takashi Iwaic697b712013-01-07 17:09:26 +01001525 memset(spec->input_paths, 0, sizeof(spec->input_paths));
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001526 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1527 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1528
Takashi Iwai352f7f92012-12-19 12:52:06 +01001529 badness = 0;
1530
1531 /* fill hard-wired DACs first */
1532 if (fill_hardwired) {
1533 bool mapped;
1534 do {
1535 mapped = map_singles(codec, cfg->line_outs,
1536 cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001537 spec->private_dac_nids,
1538 spec->out_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001539 mapped |= map_singles(codec, cfg->hp_outs,
1540 cfg->hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001541 spec->multiout.hp_out_nid,
1542 spec->hp_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001543 mapped |= map_singles(codec, cfg->speaker_outs,
1544 cfg->speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001545 spec->multiout.extra_out_nid,
1546 spec->speaker_paths);
Takashi Iwaida96fb52013-07-29 16:54:36 +02001547 if (!spec->no_multi_io &&
1548 fill_mio_first && cfg->line_outs == 1 &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01001549 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001550 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001551 if (!err)
1552 mapped = true;
1553 }
1554 } while (mapped);
1555 }
1556
1557 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001558 spec->private_dac_nids, spec->out_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001559 spec->main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001560
Takashi Iwaida96fb52013-07-29 16:54:36 +02001561 if (!spec->no_multi_io && fill_mio_first &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01001562 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1563 /* try to fill multi-io first */
Takashi Iwaie22aab72013-01-04 14:50:04 +01001564 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001565 if (err < 0)
1566 return err;
1567 /* we don't count badness at this stage yet */
1568 }
1569
1570 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1571 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1572 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001573 spec->hp_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001574 spec->extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001575 if (err < 0)
1576 return err;
1577 badness += err;
1578 }
1579 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1580 err = try_assign_dacs(codec, cfg->speaker_outs,
1581 cfg->speaker_pins,
1582 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001583 spec->speaker_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001584 spec->extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001585 if (err < 0)
1586 return err;
1587 badness += err;
1588 }
Takashi Iwaida96fb52013-07-29 16:54:36 +02001589 if (!spec->no_multi_io &&
1590 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001591 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001592 if (err < 0)
1593 return err;
1594 badness += err;
1595 }
Takashi Iwaie22aab72013-01-04 14:50:04 +01001596
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001597 if (spec->mixer_nid) {
1598 spec->aamix_out_paths[0] =
1599 check_aamix_out_path(codec, spec->out_paths[0]);
1600 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1601 spec->aamix_out_paths[1] =
1602 check_aamix_out_path(codec, spec->hp_paths[0]);
1603 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1604 spec->aamix_out_paths[2] =
1605 check_aamix_out_path(codec, spec->speaker_paths[0]);
1606 }
1607
Takashi Iwaida96fb52013-07-29 16:54:36 +02001608 if (!spec->no_multi_io &&
1609 cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
Takashi Iwaie22aab72013-01-04 14:50:04 +01001610 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1611 spec->multi_ios = 1; /* give badness */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001612
Takashi Iwaia07a9492013-01-07 16:44:06 +01001613 /* re-count num_dacs and squash invalid entries */
1614 spec->multiout.num_dacs = 0;
1615 for (i = 0; i < cfg->line_outs; i++) {
1616 if (spec->private_dac_nids[i])
1617 spec->multiout.num_dacs++;
1618 else {
1619 memmove(spec->private_dac_nids + i,
1620 spec->private_dac_nids + i + 1,
1621 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1622 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1623 }
1624 }
1625
1626 spec->ext_channel_count = spec->min_channel_count =
David Henningssonc0f3b212013-01-16 11:45:37 +01001627 spec->multiout.num_dacs * 2;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001628
Takashi Iwai352f7f92012-12-19 12:52:06 +01001629 if (spec->multi_ios == 2) {
1630 for (i = 0; i < 2; i++)
1631 spec->private_dac_nids[spec->multiout.num_dacs++] =
1632 spec->multi_io[i].dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001633 } else if (spec->multi_ios) {
1634 spec->multi_ios = 0;
1635 badness += BAD_MULTI_IO;
1636 }
1637
Takashi Iwai55a63d42013-03-21 17:20:12 +01001638 if (spec->indep_hp && !indep_hp_possible(codec))
1639 badness += BAD_NO_INDEP_HP;
1640
Takashi Iwaia07a9492013-01-07 16:44:06 +01001641 /* re-fill the shared DAC for speaker / headphone */
1642 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1643 refill_shared_dacs(codec, cfg->hp_outs,
1644 spec->multiout.hp_out_nid,
1645 spec->hp_paths);
1646 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1647 refill_shared_dacs(codec, cfg->speaker_outs,
1648 spec->multiout.extra_out_nid,
1649 spec->speaker_paths);
1650
Takashi Iwai352f7f92012-12-19 12:52:06 +01001651 return badness;
1652}
1653
1654#define DEBUG_BADNESS
1655
1656#ifdef DEBUG_BADNESS
1657#define debug_badness snd_printdd
1658#else
1659#define debug_badness(...)
1660#endif
1661
Takashi Iwaia7694092013-01-21 10:43:18 +01001662#ifdef DEBUG_BADNESS
1663static inline void print_nid_path_idx(struct hda_codec *codec,
1664 const char *pfx, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001665{
Takashi Iwaia7694092013-01-21 10:43:18 +01001666 struct nid_path *path;
1667
1668 path = snd_hda_get_path_from_idx(codec, idx);
1669 if (path)
1670 print_nid_path(pfx, path);
1671}
1672
1673static void debug_show_configs(struct hda_codec *codec,
1674 struct auto_pin_cfg *cfg)
1675{
1676 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia7694092013-01-21 10:43:18 +01001677 static const char * const lo_type[3] = { "LO", "SP", "HP" };
Takashi Iwaia7694092013-01-21 10:43:18 +01001678 int i;
1679
1680 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001681 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001682 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001683 spec->multiout.dac_nids[0],
1684 spec->multiout.dac_nids[1],
1685 spec->multiout.dac_nids[2],
Takashi Iwaia7694092013-01-21 10:43:18 +01001686 spec->multiout.dac_nids[3],
1687 lo_type[cfg->line_out_type]);
1688 for (i = 0; i < cfg->line_outs; i++)
1689 print_nid_path_idx(codec, " out", spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001690 if (spec->multi_ios > 0)
1691 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1692 spec->multi_ios,
1693 spec->multi_io[0].pin, spec->multi_io[1].pin,
1694 spec->multi_io[0].dac, spec->multi_io[1].dac);
Takashi Iwaia7694092013-01-21 10:43:18 +01001695 for (i = 0; i < spec->multi_ios; i++)
1696 print_nid_path_idx(codec, " mio",
1697 spec->out_paths[cfg->line_outs + i]);
1698 if (cfg->hp_outs)
1699 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001700 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001701 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001702 spec->multiout.hp_out_nid[0],
1703 spec->multiout.hp_out_nid[1],
1704 spec->multiout.hp_out_nid[2],
1705 spec->multiout.hp_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001706 for (i = 0; i < cfg->hp_outs; i++)
1707 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1708 if (cfg->speaker_outs)
1709 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001710 cfg->speaker_pins[0], cfg->speaker_pins[1],
1711 cfg->speaker_pins[2], cfg->speaker_pins[3],
1712 spec->multiout.extra_out_nid[0],
1713 spec->multiout.extra_out_nid[1],
1714 spec->multiout.extra_out_nid[2],
1715 spec->multiout.extra_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001716 for (i = 0; i < cfg->speaker_outs; i++)
1717 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1718 for (i = 0; i < 3; i++)
1719 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001720}
Takashi Iwaia7694092013-01-21 10:43:18 +01001721#else
1722#define debug_show_configs(codec, cfg) /* NOP */
1723#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01001724
1725/* find all available DACs of the codec */
1726static void fill_all_dac_nids(struct hda_codec *codec)
1727{
1728 struct hda_gen_spec *spec = codec->spec;
1729 int i;
1730 hda_nid_t nid = codec->start_nid;
1731
1732 spec->num_all_dacs = 0;
1733 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1734 for (i = 0; i < codec->num_nodes; i++, nid++) {
1735 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1736 continue;
1737 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1738 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1739 break;
1740 }
1741 spec->all_dacs[spec->num_all_dacs++] = nid;
1742 }
1743}
1744
1745static int parse_output_paths(struct hda_codec *codec)
1746{
1747 struct hda_gen_spec *spec = codec->spec;
1748 struct auto_pin_cfg *cfg = &spec->autocfg;
1749 struct auto_pin_cfg *best_cfg;
Takashi Iwai9314a582013-01-21 10:49:05 +01001750 unsigned int val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001751 int best_badness = INT_MAX;
1752 int badness;
1753 bool fill_hardwired = true, fill_mio_first = true;
1754 bool best_wired = true, best_mio = true;
1755 bool hp_spk_swapped = false;
1756
Takashi Iwai352f7f92012-12-19 12:52:06 +01001757 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1758 if (!best_cfg)
1759 return -ENOMEM;
1760 *best_cfg = *cfg;
1761
1762 for (;;) {
1763 badness = fill_and_eval_dacs(codec, fill_hardwired,
1764 fill_mio_first);
1765 if (badness < 0) {
1766 kfree(best_cfg);
1767 return badness;
1768 }
1769 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1770 cfg->line_out_type, fill_hardwired, fill_mio_first,
1771 badness);
Takashi Iwaia7694092013-01-21 10:43:18 +01001772 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001773 if (badness < best_badness) {
1774 best_badness = badness;
1775 *best_cfg = *cfg;
1776 best_wired = fill_hardwired;
1777 best_mio = fill_mio_first;
1778 }
1779 if (!badness)
1780 break;
1781 fill_mio_first = !fill_mio_first;
1782 if (!fill_mio_first)
1783 continue;
1784 fill_hardwired = !fill_hardwired;
1785 if (!fill_hardwired)
1786 continue;
1787 if (hp_spk_swapped)
1788 break;
1789 hp_spk_swapped = true;
1790 if (cfg->speaker_outs > 0 &&
1791 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1792 cfg->hp_outs = cfg->line_outs;
1793 memcpy(cfg->hp_pins, cfg->line_out_pins,
1794 sizeof(cfg->hp_pins));
1795 cfg->line_outs = cfg->speaker_outs;
1796 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1797 sizeof(cfg->speaker_pins));
1798 cfg->speaker_outs = 0;
1799 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1800 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1801 fill_hardwired = true;
1802 continue;
1803 }
1804 if (cfg->hp_outs > 0 &&
1805 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1806 cfg->speaker_outs = cfg->line_outs;
1807 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1808 sizeof(cfg->speaker_pins));
1809 cfg->line_outs = cfg->hp_outs;
1810 memcpy(cfg->line_out_pins, cfg->hp_pins,
1811 sizeof(cfg->hp_pins));
1812 cfg->hp_outs = 0;
1813 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1814 cfg->line_out_type = AUTO_PIN_HP_OUT;
1815 fill_hardwired = true;
1816 continue;
1817 }
1818 break;
1819 }
1820
1821 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001822 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001823 *cfg = *best_cfg;
1824 fill_and_eval_dacs(codec, best_wired, best_mio);
1825 }
1826 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1827 cfg->line_out_type, best_wired, best_mio);
Takashi Iwaia7694092013-01-21 10:43:18 +01001828 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001829
1830 if (cfg->line_out_pins[0]) {
1831 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01001832 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001833 if (path)
1834 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01001835 if (spec->vmaster_nid)
1836 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
1837 HDA_OUTPUT, spec->vmaster_tlv);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001838 }
1839
Takashi Iwai9314a582013-01-21 10:49:05 +01001840 /* set initial pinctl targets */
1841 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
1842 val = PIN_HP;
1843 else
1844 val = PIN_OUT;
1845 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
1846 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1847 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
1848 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1849 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
1850 set_pin_targets(codec, cfg->speaker_outs,
1851 cfg->speaker_pins, val);
1852 }
1853
Takashi Iwai55a63d42013-03-21 17:20:12 +01001854 /* clear indep_hp flag if not available */
1855 if (spec->indep_hp && !indep_hp_possible(codec))
1856 spec->indep_hp = 0;
1857
Takashi Iwai352f7f92012-12-19 12:52:06 +01001858 kfree(best_cfg);
1859 return 0;
1860}
1861
1862/* add playback controls from the parsed DAC table */
1863static int create_multi_out_ctls(struct hda_codec *codec,
1864 const struct auto_pin_cfg *cfg)
1865{
1866 struct hda_gen_spec *spec = codec->spec;
1867 int i, err, noutputs;
1868
1869 noutputs = cfg->line_outs;
1870 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1871 noutputs += spec->multi_ios;
1872
1873 for (i = 0; i < noutputs; i++) {
1874 const char *name;
1875 int index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001876 struct nid_path *path;
1877
Takashi Iwai196c17662013-01-04 15:01:40 +01001878 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001879 if (!path)
1880 continue;
Takashi Iwai247d85e2013-01-17 16:18:11 +01001881
1882 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001883 if (!name || !strcmp(name, "CLFE")) {
1884 /* Center/LFE */
1885 err = add_vol_ctl(codec, "Center", 0, 1, path);
1886 if (err < 0)
1887 return err;
1888 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1889 if (err < 0)
1890 return err;
Takashi Iwai247d85e2013-01-17 16:18:11 +01001891 } else {
1892 err = add_stereo_vol(codec, name, index, path);
1893 if (err < 0)
1894 return err;
1895 }
1896
1897 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
1898 if (!name || !strcmp(name, "CLFE")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001899 err = add_sw_ctl(codec, "Center", 0, 1, path);
1900 if (err < 0)
1901 return err;
1902 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1903 if (err < 0)
1904 return err;
1905 } else {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001906 err = add_stereo_sw(codec, name, index, path);
1907 if (err < 0)
1908 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 }
1910 }
1911 return 0;
1912}
1913
Takashi Iwaic2c80382013-01-07 10:33:57 +01001914static int create_extra_out(struct hda_codec *codec, int path_idx,
Takashi Iwai196c17662013-01-04 15:01:40 +01001915 const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001917 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 int err;
1919
Takashi Iwai196c17662013-01-04 15:01:40 +01001920 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001921 if (!path)
1922 return 0;
Takashi Iwaic2c80382013-01-07 10:33:57 +01001923 err = add_stereo_vol(codec, pfx, cidx, path);
1924 if (err < 0)
1925 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001926 err = add_stereo_sw(codec, pfx, cidx, path);
1927 if (err < 0)
1928 return err;
1929 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930}
1931
Takashi Iwai352f7f92012-12-19 12:52:06 +01001932/* add playback controls for speaker and HP outputs */
1933static int create_extra_outs(struct hda_codec *codec, int num_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001934 const int *paths, const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935{
Takashi Iwaic2c80382013-01-07 10:33:57 +01001936 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001937
1938 for (i = 0; i < num_pins; i++) {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001939 const char *name;
Takashi Iwai975cc022013-06-28 11:56:49 +02001940 char tmp[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwaic2c80382013-01-07 10:33:57 +01001941 int err, idx = 0;
1942
1943 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
1944 name = "Bass Speaker";
1945 else if (num_pins >= 3) {
1946 snprintf(tmp, sizeof(tmp), "%s %s",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001947 pfx, channel_name[i]);
Takashi Iwaic2c80382013-01-07 10:33:57 +01001948 name = tmp;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001949 } else {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001950 name = pfx;
1951 idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 }
Takashi Iwaic2c80382013-01-07 10:33:57 +01001953 err = create_extra_out(codec, paths[i], name, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001954 if (err < 0)
1955 return err;
1956 }
1957 return 0;
1958}
Takashi Iwai97ec5582006-03-21 11:29:07 +01001959
Takashi Iwai352f7f92012-12-19 12:52:06 +01001960static int create_hp_out_ctls(struct hda_codec *codec)
1961{
1962 struct hda_gen_spec *spec = codec->spec;
1963 return create_extra_outs(codec, spec->autocfg.hp_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001964 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001965 "Headphone");
1966}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
Takashi Iwai352f7f92012-12-19 12:52:06 +01001968static int create_speaker_out_ctls(struct hda_codec *codec)
1969{
1970 struct hda_gen_spec *spec = codec->spec;
1971 return create_extra_outs(codec, spec->autocfg.speaker_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001972 spec->speaker_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001973 "Speaker");
1974}
1975
1976/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001977 * independent HP controls
1978 */
1979
Takashi Iwai963afde2013-05-31 15:20:31 +02001980static void call_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001981static int indep_hp_info(struct snd_kcontrol *kcontrol,
1982 struct snd_ctl_elem_info *uinfo)
1983{
1984 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
1985}
1986
1987static int indep_hp_get(struct snd_kcontrol *kcontrol,
1988 struct snd_ctl_elem_value *ucontrol)
1989{
1990 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1991 struct hda_gen_spec *spec = codec->spec;
1992 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
1993 return 0;
1994}
1995
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001996static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
1997 int nomix_path_idx, int mix_path_idx,
1998 int out_type);
1999
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002000static int indep_hp_put(struct snd_kcontrol *kcontrol,
2001 struct snd_ctl_elem_value *ucontrol)
2002{
2003 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2004 struct hda_gen_spec *spec = codec->spec;
2005 unsigned int select = ucontrol->value.enumerated.item[0];
2006 int ret = 0;
2007
2008 mutex_lock(&spec->pcm_mutex);
2009 if (spec->active_streams) {
2010 ret = -EBUSY;
2011 goto unlock;
2012 }
2013
2014 if (spec->indep_hp_enabled != select) {
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002015 hda_nid_t *dacp;
2016 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2017 dacp = &spec->private_dac_nids[0];
2018 else
2019 dacp = &spec->multiout.hp_out_nid[0];
2020
2021 /* update HP aamix paths in case it conflicts with indep HP */
2022 if (spec->have_aamix_ctl) {
2023 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2024 update_aamix_paths(codec, spec->aamix_mode,
2025 spec->out_paths[0],
2026 spec->aamix_out_paths[0],
2027 spec->autocfg.line_out_type);
2028 else
2029 update_aamix_paths(codec, spec->aamix_mode,
2030 spec->hp_paths[0],
2031 spec->aamix_out_paths[1],
2032 AUTO_PIN_HP_OUT);
2033 }
2034
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002035 spec->indep_hp_enabled = select;
2036 if (spec->indep_hp_enabled)
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002037 *dacp = 0;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002038 else
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002039 *dacp = spec->alt_dac_nid;
Takashi Iwai92603c52013-01-22 07:46:31 +01002040
Takashi Iwai963afde2013-05-31 15:20:31 +02002041 call_hp_automute(codec, NULL);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002042 ret = 1;
2043 }
2044 unlock:
2045 mutex_unlock(&spec->pcm_mutex);
2046 return ret;
2047}
2048
2049static const struct snd_kcontrol_new indep_hp_ctl = {
2050 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2051 .name = "Independent HP",
2052 .info = indep_hp_info,
2053 .get = indep_hp_get,
2054 .put = indep_hp_put,
2055};
2056
2057
2058static int create_indep_hp_ctls(struct hda_codec *codec)
2059{
2060 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002061 hda_nid_t dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002062
2063 if (!spec->indep_hp)
2064 return 0;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002065 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2066 dac = spec->multiout.dac_nids[0];
2067 else
2068 dac = spec->multiout.hp_out_nid[0];
2069 if (!dac) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002070 spec->indep_hp = 0;
2071 return 0;
2072 }
2073
2074 spec->indep_hp_enabled = false;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002075 spec->alt_dac_nid = dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002076 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
2077 return -ENOMEM;
2078 return 0;
2079}
2080
2081/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002082 * channel mode enum control
2083 */
2084
2085static int ch_mode_info(struct snd_kcontrol *kcontrol,
2086 struct snd_ctl_elem_info *uinfo)
2087{
2088 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2089 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002090 int chs;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002091
2092 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2093 uinfo->count = 1;
2094 uinfo->value.enumerated.items = spec->multi_ios + 1;
2095 if (uinfo->value.enumerated.item > spec->multi_ios)
2096 uinfo->value.enumerated.item = spec->multi_ios;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002097 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2098 sprintf(uinfo->value.enumerated.name, "%dch", chs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002099 return 0;
2100}
2101
2102static int ch_mode_get(struct snd_kcontrol *kcontrol,
2103 struct snd_ctl_elem_value *ucontrol)
2104{
2105 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2106 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002107 ucontrol->value.enumerated.item[0] =
2108 (spec->ext_channel_count - spec->min_channel_count) / 2;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002109 return 0;
2110}
2111
Takashi Iwai196c17662013-01-04 15:01:40 +01002112static inline struct nid_path *
2113get_multiio_path(struct hda_codec *codec, int idx)
2114{
2115 struct hda_gen_spec *spec = codec->spec;
2116 return snd_hda_get_path_from_idx(codec,
2117 spec->out_paths[spec->autocfg.line_outs + idx]);
2118}
2119
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002120static void update_automute_all(struct hda_codec *codec);
2121
Takashi Iwai65033cc2013-04-16 12:31:05 +02002122/* Default value to be passed as aamix argument for snd_hda_activate_path();
2123 * used for output paths
2124 */
2125static bool aamix_default(struct hda_gen_spec *spec)
2126{
2127 return !spec->have_aamix_ctl || spec->aamix_mode;
2128}
2129
Takashi Iwai352f7f92012-12-19 12:52:06 +01002130static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2131{
2132 struct hda_gen_spec *spec = codec->spec;
2133 hda_nid_t nid = spec->multi_io[idx].pin;
2134 struct nid_path *path;
2135
Takashi Iwai196c17662013-01-04 15:01:40 +01002136 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002137 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002139
2140 if (path->active == output)
2141 return 0;
2142
2143 if (output) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01002144 set_pin_target(codec, nid, PIN_OUT, true);
Takashi Iwai65033cc2013-04-16 12:31:05 +02002145 snd_hda_activate_path(codec, path, true, aamix_default(spec));
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002146 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002147 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002148 set_pin_eapd(codec, nid, false);
Takashi Iwai65033cc2013-04-16 12:31:05 +02002149 snd_hda_activate_path(codec, path, false, aamix_default(spec));
Takashi Iwai2c12c302013-01-10 09:33:29 +01002150 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002151 path_power_down_sync(codec, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 }
Takashi Iwaia365fed2013-01-10 16:10:06 +01002153
2154 /* update jack retasking in case it modifies any of them */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002155 update_automute_all(codec);
Takashi Iwaia365fed2013-01-10 16:10:06 +01002156
Takashi Iwai352f7f92012-12-19 12:52:06 +01002157 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158}
2159
Takashi Iwai352f7f92012-12-19 12:52:06 +01002160static int ch_mode_put(struct snd_kcontrol *kcontrol,
2161 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002163 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2164 struct hda_gen_spec *spec = codec->spec;
2165 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
Takashi Iwai352f7f92012-12-19 12:52:06 +01002167 ch = ucontrol->value.enumerated.item[0];
2168 if (ch < 0 || ch > spec->multi_ios)
2169 return -EINVAL;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002170 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002171 return 0;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002172 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002173 for (i = 0; i < spec->multi_ios; i++)
2174 set_multi_io(codec, i, i < ch);
2175 spec->multiout.max_channels = max(spec->ext_channel_count,
2176 spec->const_channel_count);
2177 if (spec->need_dac_fix)
2178 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 return 1;
2180}
2181
Takashi Iwai352f7f92012-12-19 12:52:06 +01002182static const struct snd_kcontrol_new channel_mode_enum = {
2183 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2184 .name = "Channel Mode",
2185 .info = ch_mode_info,
2186 .get = ch_mode_get,
2187 .put = ch_mode_put,
2188};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
Takashi Iwai352f7f92012-12-19 12:52:06 +01002190static int create_multi_channel_mode(struct hda_codec *codec)
2191{
2192 struct hda_gen_spec *spec = codec->spec;
2193
2194 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002195 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002196 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 return 0;
2199}
2200
Takashi Iwai352f7f92012-12-19 12:52:06 +01002201/*
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002202 * aamix loopback enable/disable switch
2203 */
2204
2205#define loopback_mixing_info indep_hp_info
2206
2207static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2208 struct snd_ctl_elem_value *ucontrol)
2209{
2210 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2211 struct hda_gen_spec *spec = codec->spec;
2212 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2213 return 0;
2214}
2215
2216static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002217 int nomix_path_idx, int mix_path_idx,
2218 int out_type)
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002219{
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002220 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002221 struct nid_path *nomix_path, *mix_path;
2222
2223 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2224 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2225 if (!nomix_path || !mix_path)
2226 return;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002227
2228 /* if HP aamix path is driven from a different DAC and the
2229 * independent HP mode is ON, can't turn on aamix path
2230 */
2231 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2232 mix_path->path[0] != spec->alt_dac_nid)
2233 do_mix = false;
2234
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002235 if (do_mix) {
2236 snd_hda_activate_path(codec, nomix_path, false, true);
2237 snd_hda_activate_path(codec, mix_path, true, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002238 path_power_down_sync(codec, nomix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002239 } else {
Takashi Iwai65033cc2013-04-16 12:31:05 +02002240 snd_hda_activate_path(codec, mix_path, false, false);
2241 snd_hda_activate_path(codec, nomix_path, true, false);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002242 path_power_down_sync(codec, mix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002243 }
2244}
2245
2246static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2247 struct snd_ctl_elem_value *ucontrol)
2248{
2249 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2250 struct hda_gen_spec *spec = codec->spec;
2251 unsigned int val = ucontrol->value.enumerated.item[0];
2252
2253 if (val == spec->aamix_mode)
2254 return 0;
2255 spec->aamix_mode = val;
2256 update_aamix_paths(codec, val, spec->out_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002257 spec->aamix_out_paths[0],
2258 spec->autocfg.line_out_type);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002259 update_aamix_paths(codec, val, spec->hp_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002260 spec->aamix_out_paths[1],
2261 AUTO_PIN_HP_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002262 update_aamix_paths(codec, val, spec->speaker_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002263 spec->aamix_out_paths[2],
2264 AUTO_PIN_SPEAKER_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002265 return 1;
2266}
2267
2268static const struct snd_kcontrol_new loopback_mixing_enum = {
2269 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2270 .name = "Loopback Mixing",
2271 .info = loopback_mixing_info,
2272 .get = loopback_mixing_get,
2273 .put = loopback_mixing_put,
2274};
2275
2276static int create_loopback_mixing_ctl(struct hda_codec *codec)
2277{
2278 struct hda_gen_spec *spec = codec->spec;
2279
2280 if (!spec->mixer_nid)
2281 return 0;
2282 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
2283 spec->aamix_out_paths[2]))
2284 return 0;
2285 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2286 return -ENOMEM;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002287 spec->have_aamix_ctl = 1;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002288 return 0;
2289}
2290
2291/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002292 * shared headphone/mic handling
2293 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002294
Takashi Iwai352f7f92012-12-19 12:52:06 +01002295static void call_update_outputs(struct hda_codec *codec);
2296
2297/* for shared I/O, change the pin-control accordingly */
Takashi Iwai967303d2013-02-19 17:12:42 +01002298static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002299{
2300 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai967303d2013-02-19 17:12:42 +01002301 bool as_mic;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002302 unsigned int val;
Takashi Iwai967303d2013-02-19 17:12:42 +01002303 hda_nid_t pin;
2304
2305 pin = spec->hp_mic_pin;
2306 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
2307
2308 if (!force) {
2309 val = snd_hda_codec_get_pin_target(codec, pin);
2310 if (as_mic) {
2311 if (val & PIN_IN)
2312 return;
2313 } else {
2314 if (val & PIN_OUT)
2315 return;
2316 }
2317 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002318
2319 val = snd_hda_get_default_vref(codec, pin);
Takashi Iwai967303d2013-02-19 17:12:42 +01002320 /* if the HP pin doesn't support VREF and the codec driver gives an
2321 * alternative pin, set up the VREF on that pin instead
2322 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002323 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2324 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2325 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2326 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01002327 snd_hda_set_pin_ctl_cache(codec, vref_pin,
Takashi Iwai967303d2013-02-19 17:12:42 +01002328 PIN_IN | (as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02002329 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002330
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002331 if (!spec->hp_mic_jack_modes) {
2332 if (as_mic)
2333 val |= PIN_IN;
2334 else
2335 val = PIN_HP;
2336 set_pin_target(codec, pin, val, true);
Takashi Iwai963afde2013-05-31 15:20:31 +02002337 call_hp_automute(codec, NULL);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002338 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002339}
2340
2341/* create a shared input with the headphone out */
Takashi Iwai967303d2013-02-19 17:12:42 +01002342static int create_hp_mic(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002343{
2344 struct hda_gen_spec *spec = codec->spec;
2345 struct auto_pin_cfg *cfg = &spec->autocfg;
2346 unsigned int defcfg;
2347 hda_nid_t nid;
2348
Takashi Iwai967303d2013-02-19 17:12:42 +01002349 if (!spec->hp_mic) {
2350 if (spec->suppress_hp_mic_detect)
2351 return 0;
2352 /* automatic detection: only if no input or a single internal
2353 * input pin is found, try to detect the shared hp/mic
2354 */
2355 if (cfg->num_inputs > 1)
2356 return 0;
2357 else if (cfg->num_inputs == 1) {
2358 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2359 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2360 return 0;
2361 }
2362 }
2363
2364 spec->hp_mic = 0; /* clear once */
2365 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002366 return 0;
2367
Takashi Iwai967303d2013-02-19 17:12:42 +01002368 nid = 0;
2369 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2370 nid = cfg->line_out_pins[0];
2371 else if (cfg->hp_outs > 0)
2372 nid = cfg->hp_pins[0];
2373 if (!nid)
2374 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002375
2376 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2377 return 0; /* no input */
2378
Takashi Iwai967303d2013-02-19 17:12:42 +01002379 cfg->inputs[cfg->num_inputs].pin = nid;
2380 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
David Henningssoncb420b12013-04-11 11:30:28 +02002381 cfg->inputs[cfg->num_inputs].is_headphone_mic = 1;
Takashi Iwai967303d2013-02-19 17:12:42 +01002382 cfg->num_inputs++;
2383 spec->hp_mic = 1;
2384 spec->hp_mic_pin = nid;
2385 /* we can't handle auto-mic together with HP-mic */
2386 spec->suppress_auto_mic = 1;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002387 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
2388 return 0;
2389}
2390
Takashi Iwai978e77e2013-01-10 16:57:58 +01002391/*
2392 * output jack mode
2393 */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002394
2395static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2396
2397static const char * const out_jack_texts[] = {
2398 "Line Out", "Headphone Out",
2399};
2400
Takashi Iwai978e77e2013-01-10 16:57:58 +01002401static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2402 struct snd_ctl_elem_info *uinfo)
2403{
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002404 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
Takashi Iwai978e77e2013-01-10 16:57:58 +01002405}
2406
2407static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2408 struct snd_ctl_elem_value *ucontrol)
2409{
2410 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2411 hda_nid_t nid = kcontrol->private_value;
2412 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2413 ucontrol->value.enumerated.item[0] = 1;
2414 else
2415 ucontrol->value.enumerated.item[0] = 0;
2416 return 0;
2417}
2418
2419static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2420 struct snd_ctl_elem_value *ucontrol)
2421{
2422 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2423 hda_nid_t nid = kcontrol->private_value;
2424 unsigned int val;
2425
2426 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2427 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2428 return 0;
2429 snd_hda_set_pin_ctl_cache(codec, nid, val);
2430 return 1;
2431}
2432
2433static const struct snd_kcontrol_new out_jack_mode_enum = {
2434 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2435 .info = out_jack_mode_info,
2436 .get = out_jack_mode_get,
2437 .put = out_jack_mode_put,
2438};
2439
2440static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2441{
2442 struct hda_gen_spec *spec = codec->spec;
2443 int i;
2444
2445 for (i = 0; i < spec->kctls.used; i++) {
2446 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2447 if (!strcmp(kctl->name, name) && kctl->index == idx)
2448 return true;
2449 }
2450 return false;
2451}
2452
2453static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2454 char *name, size_t name_len)
2455{
2456 struct hda_gen_spec *spec = codec->spec;
2457 int idx = 0;
2458
2459 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2460 strlcat(name, " Jack Mode", name_len);
2461
2462 for (; find_kctl_name(codec, name, idx); idx++)
2463 ;
2464}
2465
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002466static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2467{
2468 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002469 if (spec->add_jack_modes) {
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002470 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2471 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2472 return 2;
2473 }
2474 return 1;
2475}
2476
Takashi Iwai978e77e2013-01-10 16:57:58 +01002477static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2478 hda_nid_t *pins)
2479{
2480 struct hda_gen_spec *spec = codec->spec;
2481 int i;
2482
2483 for (i = 0; i < num_pins; i++) {
2484 hda_nid_t pin = pins[i];
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002485 if (pin == spec->hp_mic_pin) {
2486 int ret = create_hp_mic_jack_mode(codec, pin);
2487 if (ret < 0)
2488 return ret;
2489 continue;
2490 }
2491 if (get_out_jack_num_items(codec, pin) > 1) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01002492 struct snd_kcontrol_new *knew;
Takashi Iwai975cc022013-06-28 11:56:49 +02002493 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai978e77e2013-01-10 16:57:58 +01002494 get_jack_mode_name(codec, pin, name, sizeof(name));
2495 knew = snd_hda_gen_add_kctl(spec, name,
2496 &out_jack_mode_enum);
2497 if (!knew)
2498 return -ENOMEM;
2499 knew->private_value = pin;
2500 }
2501 }
2502
2503 return 0;
2504}
2505
Takashi Iwai294765582013-01-17 09:52:11 +01002506/*
2507 * input jack mode
2508 */
2509
2510/* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2511#define NUM_VREFS 6
2512
2513static const char * const vref_texts[NUM_VREFS] = {
2514 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2515 "", "Mic 80pc Bias", "Mic 100pc Bias"
2516};
2517
2518static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2519{
2520 unsigned int pincap;
2521
2522 pincap = snd_hda_query_pin_caps(codec, pin);
2523 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2524 /* filter out unusual vrefs */
2525 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2526 return pincap;
2527}
2528
2529/* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2530static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2531{
2532 unsigned int i, n = 0;
2533
2534 for (i = 0; i < NUM_VREFS; i++) {
2535 if (vref_caps & (1 << i)) {
2536 if (n == item_idx)
2537 return i;
2538 n++;
2539 }
2540 }
2541 return 0;
2542}
2543
2544/* convert back from the vref ctl index to the enum item index */
2545static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2546{
2547 unsigned int i, n = 0;
2548
2549 for (i = 0; i < NUM_VREFS; i++) {
2550 if (i == idx)
2551 return n;
2552 if (vref_caps & (1 << i))
2553 n++;
2554 }
2555 return 0;
2556}
2557
2558static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2559 struct snd_ctl_elem_info *uinfo)
2560{
2561 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2562 hda_nid_t nid = kcontrol->private_value;
2563 unsigned int vref_caps = get_vref_caps(codec, nid);
2564
2565 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2566 vref_texts);
2567 /* set the right text */
2568 strcpy(uinfo->value.enumerated.name,
2569 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2570 return 0;
2571}
2572
2573static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2574 struct snd_ctl_elem_value *ucontrol)
2575{
2576 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2577 hda_nid_t nid = kcontrol->private_value;
2578 unsigned int vref_caps = get_vref_caps(codec, nid);
2579 unsigned int idx;
2580
2581 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2582 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2583 return 0;
2584}
2585
2586static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2587 struct snd_ctl_elem_value *ucontrol)
2588{
2589 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2590 hda_nid_t nid = kcontrol->private_value;
2591 unsigned int vref_caps = get_vref_caps(codec, nid);
2592 unsigned int val, idx;
2593
2594 val = snd_hda_codec_get_pin_target(codec, nid);
2595 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2596 if (idx == ucontrol->value.enumerated.item[0])
2597 return 0;
2598
2599 val &= ~AC_PINCTL_VREFEN;
2600 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2601 snd_hda_set_pin_ctl_cache(codec, nid, val);
2602 return 1;
2603}
2604
2605static const struct snd_kcontrol_new in_jack_mode_enum = {
2606 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2607 .info = in_jack_mode_info,
2608 .get = in_jack_mode_get,
2609 .put = in_jack_mode_put,
2610};
2611
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002612static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2613{
2614 struct hda_gen_spec *spec = codec->spec;
2615 int nitems = 0;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002616 if (spec->add_jack_modes)
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002617 nitems = hweight32(get_vref_caps(codec, pin));
2618 return nitems ? nitems : 1;
2619}
2620
Takashi Iwai294765582013-01-17 09:52:11 +01002621static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2622{
2623 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai294765582013-01-17 09:52:11 +01002624 struct snd_kcontrol_new *knew;
Takashi Iwai975cc022013-06-28 11:56:49 +02002625 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002626 unsigned int defcfg;
2627
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002628 if (pin == spec->hp_mic_pin)
2629 return 0; /* already done in create_out_jack_mode() */
Takashi Iwai294765582013-01-17 09:52:11 +01002630
2631 /* no jack mode for fixed pins */
2632 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2633 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2634 return 0;
2635
2636 /* no multiple vref caps? */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002637 if (get_in_jack_num_items(codec, pin) <= 1)
Takashi Iwai294765582013-01-17 09:52:11 +01002638 return 0;
2639
2640 get_jack_mode_name(codec, pin, name, sizeof(name));
2641 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2642 if (!knew)
2643 return -ENOMEM;
2644 knew->private_value = pin;
2645 return 0;
2646}
2647
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002648/*
2649 * HP/mic shared jack mode
2650 */
2651static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2652 struct snd_ctl_elem_info *uinfo)
2653{
2654 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2655 hda_nid_t nid = kcontrol->private_value;
2656 int out_jacks = get_out_jack_num_items(codec, nid);
2657 int in_jacks = get_in_jack_num_items(codec, nid);
2658 const char *text = NULL;
2659 int idx;
2660
2661 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2662 uinfo->count = 1;
2663 uinfo->value.enumerated.items = out_jacks + in_jacks;
2664 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2665 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2666 idx = uinfo->value.enumerated.item;
2667 if (idx < out_jacks) {
2668 if (out_jacks > 1)
2669 text = out_jack_texts[idx];
2670 else
2671 text = "Headphone Out";
2672 } else {
2673 idx -= out_jacks;
2674 if (in_jacks > 1) {
2675 unsigned int vref_caps = get_vref_caps(codec, nid);
2676 text = vref_texts[get_vref_idx(vref_caps, idx)];
2677 } else
2678 text = "Mic In";
2679 }
2680
2681 strcpy(uinfo->value.enumerated.name, text);
2682 return 0;
2683}
2684
2685static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2686{
2687 int out_jacks = get_out_jack_num_items(codec, nid);
2688 int in_jacks = get_in_jack_num_items(codec, nid);
2689 unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2690 int idx = 0;
2691
2692 if (val & PIN_OUT) {
2693 if (out_jacks > 1 && val == PIN_HP)
2694 idx = 1;
2695 } else if (val & PIN_IN) {
2696 idx = out_jacks;
2697 if (in_jacks > 1) {
2698 unsigned int vref_caps = get_vref_caps(codec, nid);
2699 val &= AC_PINCTL_VREFEN;
2700 idx += cvt_from_vref_idx(vref_caps, val);
2701 }
2702 }
2703 return idx;
2704}
2705
2706static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2707 struct snd_ctl_elem_value *ucontrol)
2708{
2709 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2710 hda_nid_t nid = kcontrol->private_value;
2711 ucontrol->value.enumerated.item[0] =
2712 get_cur_hp_mic_jack_mode(codec, nid);
2713 return 0;
2714}
2715
2716static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2717 struct snd_ctl_elem_value *ucontrol)
2718{
2719 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2720 hda_nid_t nid = kcontrol->private_value;
2721 int out_jacks = get_out_jack_num_items(codec, nid);
2722 int in_jacks = get_in_jack_num_items(codec, nid);
2723 unsigned int val, oldval, idx;
2724
2725 oldval = get_cur_hp_mic_jack_mode(codec, nid);
2726 idx = ucontrol->value.enumerated.item[0];
2727 if (oldval == idx)
2728 return 0;
2729
2730 if (idx < out_jacks) {
2731 if (out_jacks > 1)
2732 val = idx ? PIN_HP : PIN_OUT;
2733 else
2734 val = PIN_HP;
2735 } else {
2736 idx -= out_jacks;
2737 if (in_jacks > 1) {
2738 unsigned int vref_caps = get_vref_caps(codec, nid);
2739 val = snd_hda_codec_get_pin_target(codec, nid);
Takashi Iwai3f550e32013-03-07 18:30:27 +01002740 val &= ~(AC_PINCTL_VREFEN | PIN_HP);
2741 val |= get_vref_idx(vref_caps, idx) | PIN_IN;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002742 } else
2743 val = snd_hda_get_default_vref(codec, nid);
2744 }
2745 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwai963afde2013-05-31 15:20:31 +02002746 call_hp_automute(codec, NULL);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002747
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002748 return 1;
2749}
2750
2751static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
2752 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2753 .info = hp_mic_jack_mode_info,
2754 .get = hp_mic_jack_mode_get,
2755 .put = hp_mic_jack_mode_put,
2756};
2757
2758static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2759{
2760 struct hda_gen_spec *spec = codec->spec;
2761 struct snd_kcontrol_new *knew;
2762
2763 if (get_out_jack_num_items(codec, pin) <= 1 &&
2764 get_in_jack_num_items(codec, pin) <= 1)
2765 return 0; /* no need */
2766 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
2767 &hp_mic_jack_mode_enum);
2768 if (!knew)
2769 return -ENOMEM;
2770 knew->private_value = pin;
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002771 spec->hp_mic_jack_modes = 1;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002772 return 0;
2773}
Takashi Iwai352f7f92012-12-19 12:52:06 +01002774
2775/*
2776 * Parse input paths
2777 */
2778
Takashi Iwai352f7f92012-12-19 12:52:06 +01002779/* add the powersave loopback-list entry */
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002780static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002781{
2782 struct hda_amp_list *list;
2783
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002784 list = snd_array_new(&spec->loopback_list);
2785 if (!list)
2786 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002787 list->nid = mix;
2788 list->dir = HDA_INPUT;
2789 list->idx = idx;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002790 spec->loopback.amplist = spec->loopback_list.list;
2791 return 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002792}
Takashi Iwaicb53c622007-08-10 17:21:45 +02002793
Takashi Iwai352f7f92012-12-19 12:52:06 +01002794/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01002795static int new_analog_input(struct hda_codec *codec, int input_idx,
2796 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002797 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002799 struct hda_gen_spec *spec = codec->spec;
2800 struct nid_path *path;
2801 unsigned int val;
2802 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803
Takashi Iwai352f7f92012-12-19 12:52:06 +01002804 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
2805 !nid_has_mute(codec, mix_nid, HDA_INPUT))
2806 return 0; /* no need for analog loopback */
2807
Takashi Iwai3ca529d2013-01-07 17:25:08 +01002808 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002809 if (!path)
2810 return -EINVAL;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002811 print_nid_path("loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01002812 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002813
2814 idx = path->idx[path->depth - 1];
2815 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
2816 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2817 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002818 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002820 path->ctls[NID_PATH_VOL_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 }
2822
Takashi Iwai352f7f92012-12-19 12:52:06 +01002823 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
2824 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2825 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002826 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002828 path->ctls[NID_PATH_MUTE_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 }
2830
Takashi Iwai352f7f92012-12-19 12:52:06 +01002831 path->active = true;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002832 err = add_loopback_list(spec, mix_nid, idx);
2833 if (err < 0)
2834 return err;
Takashi Iwaie4a395e2013-01-23 17:00:31 +01002835
2836 if (spec->mixer_nid != spec->mixer_merge_nid &&
2837 !spec->loopback_merge_path) {
2838 path = snd_hda_add_new_path(codec, spec->mixer_nid,
2839 spec->mixer_merge_nid, 0);
2840 if (path) {
2841 print_nid_path("loopback-merge", path);
2842 path->active = true;
2843 spec->loopback_merge_path =
2844 snd_hda_get_path_idx(codec, path);
2845 }
2846 }
2847
Takashi Iwai352f7f92012-12-19 12:52:06 +01002848 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849}
2850
Takashi Iwai352f7f92012-12-19 12:52:06 +01002851static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002853 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
2854 return (pincap & AC_PINCAP_IN) != 0;
2855}
2856
2857/* Parse the codec tree and retrieve ADCs */
2858static int fill_adc_nids(struct hda_codec *codec)
2859{
2860 struct hda_gen_spec *spec = codec->spec;
2861 hda_nid_t nid;
2862 hda_nid_t *adc_nids = spec->adc_nids;
2863 int max_nums = ARRAY_SIZE(spec->adc_nids);
2864 int i, nums = 0;
2865
2866 nid = codec->start_nid;
2867 for (i = 0; i < codec->num_nodes; i++, nid++) {
2868 unsigned int caps = get_wcaps(codec, nid);
2869 int type = get_wcaps_type(caps);
2870
2871 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2872 continue;
2873 adc_nids[nums] = nid;
2874 if (++nums >= max_nums)
2875 break;
2876 }
2877 spec->num_adc_nids = nums;
Takashi Iwai0ffd5342013-01-17 15:53:29 +01002878
2879 /* copy the detected ADCs to all_adcs[] */
2880 spec->num_all_adcs = nums;
2881 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
2882
Takashi Iwai352f7f92012-12-19 12:52:06 +01002883 return nums;
2884}
2885
2886/* filter out invalid adc_nids that don't give all active input pins;
2887 * if needed, check whether dynamic ADC-switching is available
2888 */
2889static int check_dyn_adc_switch(struct hda_codec *codec)
2890{
2891 struct hda_gen_spec *spec = codec->spec;
2892 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002893 unsigned int ok_bits;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002894 int i, n, nums;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002895
Takashi Iwai352f7f92012-12-19 12:52:06 +01002896 nums = 0;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002897 ok_bits = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002898 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002899 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002900 if (!spec->input_paths[i][n])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002901 break;
2902 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002903 if (i >= imux->num_items) {
2904 ok_bits |= (1 << n);
2905 nums++;
2906 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002907 }
2908
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002909 if (!ok_bits) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002910 /* check whether ADC-switch is possible */
2911 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002912 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002913 if (spec->input_paths[i][n]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002914 spec->dyn_adc_idx[i] = n;
2915 break;
2916 }
2917 }
2918 }
2919
2920 snd_printdd("hda-codec: enabling ADC switching\n");
2921 spec->dyn_adc_switch = 1;
2922 } else if (nums != spec->num_adc_nids) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002923 /* shrink the invalid adcs and input paths */
2924 nums = 0;
2925 for (n = 0; n < spec->num_adc_nids; n++) {
2926 if (!(ok_bits & (1 << n)))
2927 continue;
2928 if (n != nums) {
2929 spec->adc_nids[nums] = spec->adc_nids[n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002930 for (i = 0; i < imux->num_items; i++) {
2931 invalidate_nid_path(codec,
2932 spec->input_paths[i][nums]);
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002933 spec->input_paths[i][nums] =
2934 spec->input_paths[i][n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002935 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002936 }
2937 nums++;
2938 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002939 spec->num_adc_nids = nums;
2940 }
2941
Takashi Iwai967303d2013-02-19 17:12:42 +01002942 if (imux->num_items == 1 ||
2943 (imux->num_items == 2 && spec->hp_mic)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002944 snd_printdd("hda-codec: reducing to a single ADC\n");
2945 spec->num_adc_nids = 1; /* reduce to a single ADC */
2946 }
2947
2948 /* single index for individual volumes ctls */
2949 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
2950 spec->num_adc_nids = 1;
2951
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 return 0;
2953}
2954
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002955/* parse capture source paths from the given pin and create imux items */
2956static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
Takashi Iwai9dba2052013-01-18 10:01:15 +01002957 int cfg_idx, int num_adcs,
2958 const char *label, int anchor)
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002959{
2960 struct hda_gen_spec *spec = codec->spec;
2961 struct hda_input_mux *imux = &spec->input_mux;
2962 int imux_idx = imux->num_items;
2963 bool imux_added = false;
2964 int c;
2965
2966 for (c = 0; c < num_adcs; c++) {
2967 struct nid_path *path;
2968 hda_nid_t adc = spec->adc_nids[c];
2969
2970 if (!is_reachable_path(codec, pin, adc))
2971 continue;
2972 path = snd_hda_add_new_path(codec, pin, adc, anchor);
2973 if (!path)
2974 continue;
2975 print_nid_path("input", path);
2976 spec->input_paths[imux_idx][c] =
2977 snd_hda_get_path_idx(codec, path);
2978
2979 if (!imux_added) {
Takashi Iwai967303d2013-02-19 17:12:42 +01002980 if (spec->hp_mic_pin == pin)
2981 spec->hp_mic_mux_idx = imux->num_items;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002982 spec->imux_pins[imux->num_items] = pin;
Takashi Iwai9dba2052013-01-18 10:01:15 +01002983 snd_hda_add_imux_item(imux, label, cfg_idx, NULL);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002984 imux_added = true;
2985 }
2986 }
2987
2988 return 0;
2989}
2990
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002992 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 */
Takashi Iwai9dba2052013-01-18 10:01:15 +01002994
Takashi Iwaic9700422013-01-18 10:17:30 +01002995/* fill the label for each input at first */
2996static int fill_input_pin_labels(struct hda_codec *codec)
2997{
2998 struct hda_gen_spec *spec = codec->spec;
2999 const struct auto_pin_cfg *cfg = &spec->autocfg;
3000 int i;
3001
3002 for (i = 0; i < cfg->num_inputs; i++) {
3003 hda_nid_t pin = cfg->inputs[i].pin;
3004 const char *label;
3005 int j, idx;
3006
3007 if (!is_input_pin(codec, pin))
3008 continue;
3009
3010 label = hda_get_autocfg_input_label(codec, cfg, i);
3011 idx = 0;
David Henningsson8e8db7f2013-01-18 15:43:02 +01003012 for (j = i - 1; j >= 0; j--) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003013 if (spec->input_labels[j] &&
3014 !strcmp(spec->input_labels[j], label)) {
3015 idx = spec->input_label_idxs[j] + 1;
3016 break;
3017 }
3018 }
3019
3020 spec->input_labels[i] = label;
3021 spec->input_label_idxs[i] = idx;
3022 }
3023
3024 return 0;
3025}
3026
Takashi Iwai9dba2052013-01-18 10:01:15 +01003027#define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
3028
Takashi Iwai352f7f92012-12-19 12:52:06 +01003029static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003030{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003031 struct hda_gen_spec *spec = codec->spec;
3032 const struct auto_pin_cfg *cfg = &spec->autocfg;
3033 hda_nid_t mixer = spec->mixer_nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003034 int num_adcs;
Takashi Iwaic9700422013-01-18 10:17:30 +01003035 int i, err;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003036 unsigned int val;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003037
Takashi Iwai352f7f92012-12-19 12:52:06 +01003038 num_adcs = fill_adc_nids(codec);
3039 if (num_adcs < 0)
3040 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003041
Takashi Iwaic9700422013-01-18 10:17:30 +01003042 err = fill_input_pin_labels(codec);
3043 if (err < 0)
3044 return err;
3045
Takashi Iwai352f7f92012-12-19 12:52:06 +01003046 for (i = 0; i < cfg->num_inputs; i++) {
3047 hda_nid_t pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048
Takashi Iwai352f7f92012-12-19 12:52:06 +01003049 pin = cfg->inputs[i].pin;
3050 if (!is_input_pin(codec, pin))
3051 continue;
3052
Takashi Iwai2c12c302013-01-10 09:33:29 +01003053 val = PIN_IN;
3054 if (cfg->inputs[i].type == AUTO_PIN_MIC)
3055 val |= snd_hda_get_default_vref(codec, pin);
Takashi Iwai93c9d8a2013-03-11 09:48:43 +01003056 if (pin != spec->hp_mic_pin)
3057 set_pin_target(codec, pin, val, false);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003058
Takashi Iwai352f7f92012-12-19 12:52:06 +01003059 if (mixer) {
3060 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01003061 err = new_analog_input(codec, i, pin,
Takashi Iwaic9700422013-01-18 10:17:30 +01003062 spec->input_labels[i],
3063 spec->input_label_idxs[i],
3064 mixer);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003065 if (err < 0)
3066 return err;
3067 }
3068 }
3069
Takashi Iwaic9700422013-01-18 10:17:30 +01003070 err = parse_capture_source(codec, pin, i, num_adcs,
3071 spec->input_labels[i], -mixer);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003072 if (err < 0)
3073 return err;
Takashi Iwai294765582013-01-17 09:52:11 +01003074
Takashi Iwaif811c3c2013-03-07 18:32:59 +01003075 if (spec->add_jack_modes) {
Takashi Iwai294765582013-01-17 09:52:11 +01003076 err = create_in_jack_mode(codec, pin);
3077 if (err < 0)
3078 return err;
3079 }
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003080 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003081
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003082 if (mixer && spec->add_stereo_mix_input) {
Takashi Iwai9dba2052013-01-18 10:01:15 +01003083 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003084 "Stereo Mix", 0);
3085 if (err < 0)
3086 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003087 }
3088
3089 return 0;
3090}
3091
3092
3093/*
3094 * input source mux
3095 */
3096
Takashi Iwaic697b712013-01-07 17:09:26 +01003097/* get the input path specified by the given adc and imux indices */
3098static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003099{
3100 struct hda_gen_spec *spec = codec->spec;
David Henningssonb56fa1e2013-01-16 11:45:35 +01003101 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3102 snd_BUG();
3103 return NULL;
3104 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003105 if (spec->dyn_adc_switch)
3106 adc_idx = spec->dyn_adc_idx[imux_idx];
David Henningssond3d982f2013-01-18 15:43:01 +01003107 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
David Henningssonb56fa1e2013-01-16 11:45:35 +01003108 snd_BUG();
3109 return NULL;
3110 }
Takashi Iwaic697b712013-01-07 17:09:26 +01003111 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003112}
3113
3114static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3115 unsigned int idx);
3116
3117static int mux_enum_info(struct snd_kcontrol *kcontrol,
3118 struct snd_ctl_elem_info *uinfo)
3119{
3120 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3121 struct hda_gen_spec *spec = codec->spec;
3122 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3123}
3124
3125static int mux_enum_get(struct snd_kcontrol *kcontrol,
3126 struct snd_ctl_elem_value *ucontrol)
3127{
3128 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3129 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003130 /* the ctls are created at once with multiple counts */
3131 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003132
3133 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3134 return 0;
3135}
3136
3137static int mux_enum_put(struct snd_kcontrol *kcontrol,
3138 struct snd_ctl_elem_value *ucontrol)
3139{
3140 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003141 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003142 return mux_select(codec, adc_idx,
3143 ucontrol->value.enumerated.item[0]);
3144}
3145
Takashi Iwai352f7f92012-12-19 12:52:06 +01003146static const struct snd_kcontrol_new cap_src_temp = {
3147 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3148 .name = "Input Source",
3149 .info = mux_enum_info,
3150 .get = mux_enum_get,
3151 .put = mux_enum_put,
3152};
3153
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003154/*
3155 * capture volume and capture switch ctls
3156 */
3157
Takashi Iwai352f7f92012-12-19 12:52:06 +01003158typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3159 struct snd_ctl_elem_value *ucontrol);
3160
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003161/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003162static int cap_put_caller(struct snd_kcontrol *kcontrol,
3163 struct snd_ctl_elem_value *ucontrol,
3164 put_call_t func, int type)
3165{
3166 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3167 struct hda_gen_spec *spec = codec->spec;
3168 const struct hda_input_mux *imux;
3169 struct nid_path *path;
3170 int i, adc_idx, err = 0;
3171
3172 imux = &spec->input_mux;
David Henningssona053d1e2013-01-16 11:45:36 +01003173 adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003174 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003175 /* we use the cache-only update at first since multiple input paths
3176 * may shared the same amp; by updating only caches, the redundant
3177 * writes to hardware can be reduced.
3178 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003179 codec->cached_write = 1;
3180 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003181 path = get_input_path(codec, adc_idx, i);
3182 if (!path || !path->ctls[type])
Takashi Iwai352f7f92012-12-19 12:52:06 +01003183 continue;
3184 kcontrol->private_value = path->ctls[type];
3185 err = func(kcontrol, ucontrol);
3186 if (err < 0)
3187 goto error;
3188 }
3189 error:
3190 codec->cached_write = 0;
3191 mutex_unlock(&codec->control_mutex);
Takashi Iwaidc870f32013-01-22 15:24:30 +01003192 snd_hda_codec_flush_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003193 if (err >= 0 && spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01003194 spec->cap_sync_hook(codec, ucontrol);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003195 return err;
3196}
3197
3198/* capture volume ctl callbacks */
3199#define cap_vol_info snd_hda_mixer_amp_volume_info
3200#define cap_vol_get snd_hda_mixer_amp_volume_get
3201#define cap_vol_tlv snd_hda_mixer_amp_tlv
3202
3203static int cap_vol_put(struct snd_kcontrol *kcontrol,
3204 struct snd_ctl_elem_value *ucontrol)
3205{
3206 return cap_put_caller(kcontrol, ucontrol,
3207 snd_hda_mixer_amp_volume_put,
3208 NID_PATH_VOL_CTL);
3209}
3210
3211static const struct snd_kcontrol_new cap_vol_temp = {
3212 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3213 .name = "Capture Volume",
3214 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3215 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3216 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3217 .info = cap_vol_info,
3218 .get = cap_vol_get,
3219 .put = cap_vol_put,
3220 .tlv = { .c = cap_vol_tlv },
3221};
3222
3223/* capture switch ctl callbacks */
3224#define cap_sw_info snd_ctl_boolean_stereo_info
3225#define cap_sw_get snd_hda_mixer_amp_switch_get
3226
3227static int cap_sw_put(struct snd_kcontrol *kcontrol,
3228 struct snd_ctl_elem_value *ucontrol)
3229{
Takashi Iwaia90229e2013-01-18 14:10:00 +01003230 return cap_put_caller(kcontrol, ucontrol,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003231 snd_hda_mixer_amp_switch_put,
3232 NID_PATH_MUTE_CTL);
3233}
3234
3235static const struct snd_kcontrol_new cap_sw_temp = {
3236 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3237 .name = "Capture Switch",
3238 .info = cap_sw_info,
3239 .get = cap_sw_get,
3240 .put = cap_sw_put,
3241};
3242
3243static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3244{
3245 hda_nid_t nid;
3246 int i, depth;
3247
3248 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3249 for (depth = 0; depth < 3; depth++) {
3250 if (depth >= path->depth)
3251 return -EINVAL;
3252 i = path->depth - depth - 1;
3253 nid = path->path[i];
3254 if (!path->ctls[NID_PATH_VOL_CTL]) {
3255 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3256 path->ctls[NID_PATH_VOL_CTL] =
3257 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3258 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3259 int idx = path->idx[i];
3260 if (!depth && codec->single_adc_amp)
3261 idx = 0;
3262 path->ctls[NID_PATH_VOL_CTL] =
3263 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3264 }
3265 }
3266 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3267 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3268 path->ctls[NID_PATH_MUTE_CTL] =
3269 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3270 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3271 int idx = path->idx[i];
3272 if (!depth && codec->single_adc_amp)
3273 idx = 0;
3274 path->ctls[NID_PATH_MUTE_CTL] =
3275 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3276 }
3277 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01003278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 return 0;
3280}
3281
Takashi Iwai352f7f92012-12-19 12:52:06 +01003282static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003284 struct hda_gen_spec *spec = codec->spec;
3285 struct auto_pin_cfg *cfg = &spec->autocfg;
3286 unsigned int val;
3287 int i;
3288
3289 if (!spec->inv_dmic_split)
3290 return false;
3291 for (i = 0; i < cfg->num_inputs; i++) {
3292 if (cfg->inputs[i].pin != nid)
3293 continue;
3294 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3295 return false;
3296 val = snd_hda_codec_get_pincfg(codec, nid);
3297 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3298 }
3299 return false;
3300}
3301
Takashi Iwaia90229e2013-01-18 14:10:00 +01003302/* capture switch put callback for a single control with hook call */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003303static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3304 struct snd_ctl_elem_value *ucontrol)
3305{
3306 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3307 struct hda_gen_spec *spec = codec->spec;
3308 int ret;
3309
3310 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3311 if (ret < 0)
3312 return ret;
3313
Takashi Iwaia90229e2013-01-18 14:10:00 +01003314 if (spec->cap_sync_hook)
3315 spec->cap_sync_hook(codec, ucontrol);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003316
3317 return ret;
3318}
3319
Takashi Iwai352f7f92012-12-19 12:52:06 +01003320static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3321 int idx, bool is_switch, unsigned int ctl,
3322 bool inv_dmic)
3323{
3324 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai975cc022013-06-28 11:56:49 +02003325 char tmpname[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003326 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3327 const char *sfx = is_switch ? "Switch" : "Volume";
3328 unsigned int chs = inv_dmic ? 1 : 3;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003329 struct snd_kcontrol_new *knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003330
3331 if (!ctl)
3332 return 0;
3333
3334 if (label)
3335 snprintf(tmpname, sizeof(tmpname),
3336 "%s Capture %s", label, sfx);
3337 else
3338 snprintf(tmpname, sizeof(tmpname),
3339 "Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003340 knew = add_control(spec, type, tmpname, idx,
3341 amp_val_replace_channels(ctl, chs));
3342 if (!knew)
3343 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003344 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003345 knew->put = cap_single_sw_put;
3346 if (!inv_dmic)
3347 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003348
3349 /* Make independent right kcontrol */
3350 if (label)
3351 snprintf(tmpname, sizeof(tmpname),
3352 "Inverted %s Capture %s", label, sfx);
3353 else
3354 snprintf(tmpname, sizeof(tmpname),
3355 "Inverted Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003356 knew = add_control(spec, type, tmpname, idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003357 amp_val_replace_channels(ctl, 2));
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003358 if (!knew)
3359 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003360 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003361 knew->put = cap_single_sw_put;
3362 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003363}
3364
3365/* create single (and simple) capture volume and switch controls */
3366static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3367 unsigned int vol_ctl, unsigned int sw_ctl,
3368 bool inv_dmic)
3369{
3370 int err;
3371 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3372 if (err < 0)
3373 return err;
3374 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3375 if (err < 0)
3376 return err;
3377 return 0;
3378}
3379
3380/* create bound capture volume and switch controls */
3381static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3382 unsigned int vol_ctl, unsigned int sw_ctl)
3383{
3384 struct hda_gen_spec *spec = codec->spec;
3385 struct snd_kcontrol_new *knew;
3386
3387 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003388 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003389 if (!knew)
3390 return -ENOMEM;
3391 knew->index = idx;
3392 knew->private_value = vol_ctl;
3393 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3394 }
3395 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003396 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003397 if (!knew)
3398 return -ENOMEM;
3399 knew->index = idx;
3400 knew->private_value = sw_ctl;
3401 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3402 }
3403 return 0;
3404}
3405
3406/* return the vol ctl when used first in the imux list */
3407static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3408{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003409 struct nid_path *path;
3410 unsigned int ctl;
3411 int i;
3412
Takashi Iwaic697b712013-01-07 17:09:26 +01003413 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003414 if (!path)
3415 return 0;
3416 ctl = path->ctls[type];
3417 if (!ctl)
3418 return 0;
3419 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003420 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003421 if (path && path->ctls[type] == ctl)
3422 return 0;
3423 }
3424 return ctl;
3425}
3426
3427/* create individual capture volume and switch controls per input */
3428static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3429{
3430 struct hda_gen_spec *spec = codec->spec;
3431 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaic9700422013-01-18 10:17:30 +01003432 int i, err, type;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003433
3434 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003435 bool inv_dmic;
Takashi Iwaic9700422013-01-18 10:17:30 +01003436 int idx;
Takashi Iwai9dba2052013-01-18 10:01:15 +01003437
Takashi Iwaic9700422013-01-18 10:17:30 +01003438 idx = imux->items[i].index;
3439 if (idx >= spec->autocfg.num_inputs)
Takashi Iwai9dba2052013-01-18 10:01:15 +01003440 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003441 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3442
3443 for (type = 0; type < 2; type++) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003444 err = add_single_cap_ctl(codec,
3445 spec->input_labels[idx],
3446 spec->input_label_idxs[idx],
3447 type,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003448 get_first_cap_ctl(codec, i, type),
3449 inv_dmic);
3450 if (err < 0)
3451 return err;
3452 }
3453 }
3454 return 0;
3455}
3456
3457static int create_capture_mixers(struct hda_codec *codec)
3458{
3459 struct hda_gen_spec *spec = codec->spec;
3460 struct hda_input_mux *imux = &spec->input_mux;
3461 int i, n, nums, err;
3462
3463 if (spec->dyn_adc_switch)
3464 nums = 1;
3465 else
3466 nums = spec->num_adc_nids;
3467
3468 if (!spec->auto_mic && imux->num_items > 1) {
3469 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01003470 const char *name;
3471 name = nums > 1 ? "Input Source" : "Capture Source";
3472 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003473 if (!knew)
3474 return -ENOMEM;
3475 knew->count = nums;
3476 }
3477
3478 for (n = 0; n < nums; n++) {
3479 bool multi = false;
David Henningsson99a55922013-01-16 15:58:44 +01003480 bool multi_cap_vol = spec->multi_cap_vol;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003481 bool inv_dmic = false;
3482 int vol, sw;
3483
3484 vol = sw = 0;
3485 for (i = 0; i < imux->num_items; i++) {
3486 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01003487 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003488 if (!path)
3489 continue;
3490 parse_capvol_in_path(codec, path);
3491 if (!vol)
3492 vol = path->ctls[NID_PATH_VOL_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003493 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003494 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003495 if (!same_amp_caps(codec, vol,
3496 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3497 multi_cap_vol = true;
3498 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003499 if (!sw)
3500 sw = path->ctls[NID_PATH_MUTE_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003501 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003502 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003503 if (!same_amp_caps(codec, sw,
3504 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3505 multi_cap_vol = true;
3506 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003507 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3508 inv_dmic = true;
3509 }
3510
3511 if (!multi)
3512 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3513 inv_dmic);
David Henningsson99a55922013-01-16 15:58:44 +01003514 else if (!multi_cap_vol)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003515 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3516 else
3517 err = create_multi_cap_vol_ctl(codec);
3518 if (err < 0)
3519 return err;
3520 }
3521
3522 return 0;
3523}
3524
3525/*
3526 * add mic boosts if needed
3527 */
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003528
3529/* check whether the given amp is feasible as a boost volume */
3530static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3531 int dir, int idx)
3532{
3533 unsigned int step;
3534
3535 if (!nid_has_volume(codec, nid, dir) ||
3536 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3537 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3538 return false;
3539
3540 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3541 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3542 if (step < 0x20)
3543 return false;
3544 return true;
3545}
3546
3547/* look for a boost amp in a widget close to the pin */
3548static unsigned int look_for_boost_amp(struct hda_codec *codec,
3549 struct nid_path *path)
3550{
3551 unsigned int val = 0;
3552 hda_nid_t nid;
3553 int depth;
3554
3555 for (depth = 0; depth < 3; depth++) {
3556 if (depth >= path->depth - 1)
3557 break;
3558 nid = path->path[depth];
3559 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3560 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3561 break;
3562 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3563 path->idx[depth])) {
3564 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3565 HDA_INPUT);
3566 break;
3567 }
3568 }
3569
3570 return val;
3571}
3572
Takashi Iwai352f7f92012-12-19 12:52:06 +01003573static int parse_mic_boost(struct hda_codec *codec)
3574{
3575 struct hda_gen_spec *spec = codec->spec;
3576 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003577 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003578 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003579
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003580 if (!spec->num_adc_nids)
3581 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003582
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003583 for (i = 0; i < imux->num_items; i++) {
3584 struct nid_path *path;
3585 unsigned int val;
3586 int idx;
Takashi Iwai975cc022013-06-28 11:56:49 +02003587 char boost_label[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
David Henningsson02aba552013-01-16 15:58:43 +01003588
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003589 idx = imux->items[i].index;
3590 if (idx >= imux->num_items)
3591 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003592
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003593 /* check only line-in and mic pins */
Takashi Iwai1799cdd52013-01-18 14:37:16 +01003594 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003595 continue;
3596
3597 path = get_input_path(codec, 0, i);
3598 if (!path)
3599 continue;
3600
3601 val = look_for_boost_amp(codec, path);
3602 if (!val)
3603 continue;
3604
3605 /* create a boost control */
3606 snprintf(boost_label, sizeof(boost_label),
3607 "%s Boost Volume", spec->input_labels[idx]);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003608 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3609 spec->input_label_idxs[idx], val))
3610 return -ENOMEM;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003611
3612 path->ctls[NID_PATH_BOOST_CTL] = val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003613 }
3614 return 0;
3615}
3616
3617/*
3618 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3619 */
3620static void parse_digital(struct hda_codec *codec)
3621{
3622 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003623 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003624 int i, nums;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003625 hda_nid_t dig_nid, pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003626
3627 /* support multiple SPDIFs; the secondary is set up as a slave */
3628 nums = 0;
3629 for (i = 0; i < spec->autocfg.dig_outs; i++) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003630 pin = spec->autocfg.dig_out_pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003631 dig_nid = look_for_dac(codec, pin, true);
3632 if (!dig_nid)
3633 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003634 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003635 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003636 continue;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003637 print_nid_path("digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01003638 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01003639 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003640 set_pin_target(codec, pin, PIN_OUT, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003641 if (!nums) {
3642 spec->multiout.dig_out_nid = dig_nid;
3643 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3644 } else {
3645 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3646 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
3647 break;
3648 spec->slave_dig_outs[nums - 1] = dig_nid;
3649 }
3650 nums++;
3651 }
3652
3653 if (spec->autocfg.dig_in_pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003654 pin = spec->autocfg.dig_in_pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003655 dig_nid = codec->start_nid;
3656 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003657 unsigned int wcaps = get_wcaps(codec, dig_nid);
3658 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3659 continue;
3660 if (!(wcaps & AC_WCAP_DIGITAL))
3661 continue;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003662 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003663 if (path) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003664 print_nid_path("digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003665 path->active = true;
3666 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003667 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003668 set_pin_target(codec, pin, PIN_IN, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003669 break;
3670 }
3671 }
3672 }
3673}
3674
3675
3676/*
3677 * input MUX handling
3678 */
3679
3680static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3681
3682/* select the given imux item; either unmute exclusively or select the route */
3683static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3684 unsigned int idx)
3685{
3686 struct hda_gen_spec *spec = codec->spec;
3687 const struct hda_input_mux *imux;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003688 struct nid_path *old_path, *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003689
3690 imux = &spec->input_mux;
3691 if (!imux->num_items)
3692 return 0;
3693
3694 if (idx >= imux->num_items)
3695 idx = imux->num_items - 1;
3696 if (spec->cur_mux[adc_idx] == idx)
3697 return 0;
3698
Takashi Iwai55196ff2013-01-24 17:32:56 +01003699 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3700 if (!old_path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003701 return 0;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003702 if (old_path->active)
3703 snd_hda_activate_path(codec, old_path, false, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003704
3705 spec->cur_mux[adc_idx] = idx;
3706
Takashi Iwai967303d2013-02-19 17:12:42 +01003707 if (spec->hp_mic)
3708 update_hp_mic(codec, adc_idx, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003709
3710 if (spec->dyn_adc_switch)
3711 dyn_adc_pcm_resetup(codec, idx);
3712
Takashi Iwaic697b712013-01-07 17:09:26 +01003713 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003714 if (!path)
3715 return 0;
3716 if (path->active)
3717 return 0;
3718 snd_hda_activate_path(codec, path, true, false);
3719 if (spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01003720 spec->cap_sync_hook(codec, NULL);
Takashi Iwai55196ff2013-01-24 17:32:56 +01003721 path_power_down_sync(codec, old_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003722 return 1;
3723}
3724
3725
3726/*
3727 * Jack detections for HP auto-mute and mic-switch
3728 */
3729
3730/* check each pin in the given array; returns true if any of them is plugged */
3731static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
3732{
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02003733 int i;
3734 bool present = false;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003735
3736 for (i = 0; i < num_pins; i++) {
3737 hda_nid_t nid = pins[i];
3738 if (!nid)
3739 break;
Takashi Iwai0b4df932013-01-10 09:45:13 +01003740 /* don't detect pins retasked as inputs */
3741 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
3742 continue;
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02003743 if (snd_hda_jack_detect_state(codec, nid) == HDA_JACK_PRESENT)
3744 present = true;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003745 }
3746 return present;
3747}
3748
3749/* standard HP/line-out auto-mute helper */
3750static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
Takashi Iwai2c12c302013-01-10 09:33:29 +01003751 bool mute)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003752{
3753 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003754 int i;
3755
3756 for (i = 0; i < num_pins; i++) {
3757 hda_nid_t nid = pins[i];
Takashi Iwai967303d2013-02-19 17:12:42 +01003758 unsigned int val, oldval;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003759 if (!nid)
3760 break;
Takashi Iwai7eebffd2013-06-24 16:00:21 +02003761
3762 if (spec->auto_mute_via_amp) {
3763 if (mute)
3764 spec->mute_bits |= (1ULL << nid);
3765 else
3766 spec->mute_bits &= ~(1ULL << nid);
3767 set_pin_eapd(codec, nid, !mute);
3768 continue;
3769 }
3770
Takashi Iwai967303d2013-02-19 17:12:42 +01003771 oldval = snd_hda_codec_get_pin_target(codec, nid);
3772 if (oldval & PIN_IN)
3773 continue; /* no mute for inputs */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003774 /* don't reset VREF value in case it's controlling
3775 * the amp (see alc861_fixup_asus_amp_vref_0f())
3776 */
Takashi Iwai2c12c302013-01-10 09:33:29 +01003777 if (spec->keep_vref_in_automute)
Takashi Iwai967303d2013-02-19 17:12:42 +01003778 val = oldval & ~PIN_HP;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003779 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01003780 val = 0;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003781 if (!mute)
Takashi Iwai967303d2013-02-19 17:12:42 +01003782 val |= oldval;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003783 /* here we call update_pin_ctl() so that the pinctl is changed
3784 * without changing the pinctl target value;
3785 * the original target value will be still referred at the
3786 * init / resume again
3787 */
3788 update_pin_ctl(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01003789 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003790 }
3791}
3792
3793/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003794void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003795{
3796 struct hda_gen_spec *spec = codec->spec;
3797 int on;
3798
3799 /* Control HP pins/amps depending on master_mute state;
3800 * in general, HP pins/amps control should be enabled in all cases,
3801 * but currently set only for master_mute, just to be safe
3802 */
Takashi Iwai967303d2013-02-19 17:12:42 +01003803 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003804 spec->autocfg.hp_pins, spec->master_mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003805
3806 if (!spec->automute_speaker)
3807 on = 0;
3808 else
3809 on = spec->hp_jack_present | spec->line_jack_present;
3810 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003811 spec->speaker_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003812 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003813 spec->autocfg.speaker_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003814
3815 /* toggle line-out mutes if needed, too */
3816 /* if LO is a copy of either HP or Speaker, don't need to handle it */
3817 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
3818 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
3819 return;
3820 if (!spec->automute_lo)
3821 on = 0;
3822 else
3823 on = spec->hp_jack_present;
3824 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003825 spec->line_out_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003826 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003827 spec->autocfg.line_out_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003828}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003829EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003830
3831static void call_update_outputs(struct hda_codec *codec)
3832{
3833 struct hda_gen_spec *spec = codec->spec;
3834 if (spec->automute_hook)
3835 spec->automute_hook(codec);
3836 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01003837 snd_hda_gen_update_outputs(codec);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02003838
3839 /* sync the whole vmaster slaves to reflect the new auto-mute status */
3840 if (spec->auto_mute_via_amp && !codec->bus->shutdown)
3841 snd_ctl_sync_vmaster(spec->vmaster_mute.sw_kctl, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003842}
3843
3844/* standard HP-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003845void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003846{
3847 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai92603c52013-01-22 07:46:31 +01003848 hda_nid_t *pins = spec->autocfg.hp_pins;
3849 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003850
Takashi Iwai92603c52013-01-22 07:46:31 +01003851 /* No detection for the first HP jack during indep-HP mode */
3852 if (spec->indep_hp_enabled) {
3853 pins++;
3854 num_pins--;
3855 }
3856
3857 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003858 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
3859 return;
3860 call_update_outputs(codec);
3861}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003862EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003863
3864/* standard line-out-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003865void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003866{
3867 struct hda_gen_spec *spec = codec->spec;
3868
3869 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3870 return;
3871 /* check LO jack only when it's different from HP */
3872 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
3873 return;
3874
3875 spec->line_jack_present =
3876 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3877 spec->autocfg.line_out_pins);
3878 if (!spec->automute_speaker || !spec->detect_lo)
3879 return;
3880 call_update_outputs(codec);
3881}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003882EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003883
3884/* standard mic auto-switch helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003885void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003886{
3887 struct hda_gen_spec *spec = codec->spec;
3888 int i;
3889
3890 if (!spec->auto_mic)
3891 return;
3892
3893 for (i = spec->am_num_entries - 1; i > 0; i--) {
Takashi Iwai0b4df932013-01-10 09:45:13 +01003894 hda_nid_t pin = spec->am_entry[i].pin;
3895 /* don't detect pins retasked as outputs */
3896 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
3897 continue;
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02003898 if (snd_hda_jack_detect_state(codec, pin) == HDA_JACK_PRESENT) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003899 mux_select(codec, 0, spec->am_entry[i].idx);
3900 return;
3901 }
3902 }
3903 mux_select(codec, 0, spec->am_entry[0].idx);
3904}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003905EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003906
Takashi Iwai77afe0e2013-05-31 14:10:03 +02003907/* call appropriate hooks */
3908static void call_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
3909{
3910 struct hda_gen_spec *spec = codec->spec;
3911 if (spec->hp_automute_hook)
3912 spec->hp_automute_hook(codec, jack);
3913 else
3914 snd_hda_gen_hp_automute(codec, jack);
3915}
3916
3917static void call_line_automute(struct hda_codec *codec,
3918 struct hda_jack_tbl *jack)
3919{
3920 struct hda_gen_spec *spec = codec->spec;
3921 if (spec->line_automute_hook)
3922 spec->line_automute_hook(codec, jack);
3923 else
3924 snd_hda_gen_line_automute(codec, jack);
3925}
3926
3927static void call_mic_autoswitch(struct hda_codec *codec,
3928 struct hda_jack_tbl *jack)
3929{
3930 struct hda_gen_spec *spec = codec->spec;
3931 if (spec->mic_autoswitch_hook)
3932 spec->mic_autoswitch_hook(codec, jack);
3933 else
3934 snd_hda_gen_mic_autoswitch(codec, jack);
3935}
3936
Takashi Iwai963afde2013-05-31 15:20:31 +02003937/* update jack retasking */
3938static void update_automute_all(struct hda_codec *codec)
3939{
3940 call_hp_automute(codec, NULL);
3941 call_line_automute(codec, NULL);
3942 call_mic_autoswitch(codec, NULL);
3943}
3944
Takashi Iwai352f7f92012-12-19 12:52:06 +01003945/*
3946 * Auto-Mute mode mixer enum support
3947 */
3948static int automute_mode_info(struct snd_kcontrol *kcontrol,
3949 struct snd_ctl_elem_info *uinfo)
3950{
3951 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3952 struct hda_gen_spec *spec = codec->spec;
3953 static const char * const texts3[] = {
3954 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02003955 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956
Takashi Iwai352f7f92012-12-19 12:52:06 +01003957 if (spec->automute_speaker_possible && spec->automute_lo_possible)
3958 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
3959 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
3960}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961
Takashi Iwai352f7f92012-12-19 12:52:06 +01003962static int automute_mode_get(struct snd_kcontrol *kcontrol,
3963 struct snd_ctl_elem_value *ucontrol)
3964{
3965 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3966 struct hda_gen_spec *spec = codec->spec;
3967 unsigned int val = 0;
3968 if (spec->automute_speaker)
3969 val++;
3970 if (spec->automute_lo)
3971 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003972
Takashi Iwai352f7f92012-12-19 12:52:06 +01003973 ucontrol->value.enumerated.item[0] = val;
3974 return 0;
3975}
3976
3977static int automute_mode_put(struct snd_kcontrol *kcontrol,
3978 struct snd_ctl_elem_value *ucontrol)
3979{
3980 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3981 struct hda_gen_spec *spec = codec->spec;
3982
3983 switch (ucontrol->value.enumerated.item[0]) {
3984 case 0:
3985 if (!spec->automute_speaker && !spec->automute_lo)
3986 return 0;
3987 spec->automute_speaker = 0;
3988 spec->automute_lo = 0;
3989 break;
3990 case 1:
3991 if (spec->automute_speaker_possible) {
3992 if (!spec->automute_lo && spec->automute_speaker)
3993 return 0;
3994 spec->automute_speaker = 1;
3995 spec->automute_lo = 0;
3996 } else if (spec->automute_lo_possible) {
3997 if (spec->automute_lo)
3998 return 0;
3999 spec->automute_lo = 1;
4000 } else
4001 return -EINVAL;
4002 break;
4003 case 2:
4004 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
4005 return -EINVAL;
4006 if (spec->automute_speaker && spec->automute_lo)
4007 return 0;
4008 spec->automute_speaker = 1;
4009 spec->automute_lo = 1;
4010 break;
4011 default:
4012 return -EINVAL;
4013 }
4014 call_update_outputs(codec);
4015 return 1;
4016}
4017
4018static const struct snd_kcontrol_new automute_mode_enum = {
4019 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4020 .name = "Auto-Mute Mode",
4021 .info = automute_mode_info,
4022 .get = automute_mode_get,
4023 .put = automute_mode_put,
4024};
4025
4026static int add_automute_mode_enum(struct hda_codec *codec)
4027{
4028 struct hda_gen_spec *spec = codec->spec;
4029
Takashi Iwai12c93df2012-12-19 14:38:33 +01004030 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01004031 return -ENOMEM;
4032 return 0;
4033}
4034
4035/*
4036 * Check the availability of HP/line-out auto-mute;
4037 * Set up appropriately if really supported
4038 */
4039static int check_auto_mute_availability(struct hda_codec *codec)
4040{
4041 struct hda_gen_spec *spec = codec->spec;
4042 struct auto_pin_cfg *cfg = &spec->autocfg;
4043 int present = 0;
4044 int i, err;
4045
Takashi Iwaif72706b2013-01-16 18:20:07 +01004046 if (spec->suppress_auto_mute)
4047 return 0;
4048
Takashi Iwai352f7f92012-12-19 12:52:06 +01004049 if (cfg->hp_pins[0])
4050 present++;
4051 if (cfg->line_out_pins[0])
4052 present++;
4053 if (cfg->speaker_pins[0])
4054 present++;
4055 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02004056 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004057
4058 if (!cfg->speaker_pins[0] &&
4059 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
4060 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4061 sizeof(cfg->speaker_pins));
4062 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064
Takashi Iwai352f7f92012-12-19 12:52:06 +01004065 if (!cfg->hp_pins[0] &&
4066 cfg->line_out_type == AUTO_PIN_HP_OUT) {
4067 memcpy(cfg->hp_pins, cfg->line_out_pins,
4068 sizeof(cfg->hp_pins));
4069 cfg->hp_outs = cfg->line_outs;
4070 }
4071
4072 for (i = 0; i < cfg->hp_outs; i++) {
4073 hda_nid_t nid = cfg->hp_pins[i];
4074 if (!is_jack_detectable(codec, nid))
4075 continue;
4076 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
4077 nid);
4078 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004079 call_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004080 spec->detect_hp = 1;
4081 }
4082
4083 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
4084 if (cfg->speaker_outs)
4085 for (i = 0; i < cfg->line_outs; i++) {
4086 hda_nid_t nid = cfg->line_out_pins[i];
4087 if (!is_jack_detectable(codec, nid))
4088 continue;
4089 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
4090 snd_hda_jack_detect_enable_callback(codec, nid,
4091 HDA_GEN_FRONT_EVENT,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004092 call_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004093 spec->detect_lo = 1;
4094 }
4095 spec->automute_lo_possible = spec->detect_hp;
4096 }
4097
4098 spec->automute_speaker_possible = cfg->speaker_outs &&
4099 (spec->detect_hp || spec->detect_lo);
4100
4101 spec->automute_lo = spec->automute_lo_possible;
4102 spec->automute_speaker = spec->automute_speaker_possible;
4103
4104 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
4105 /* create a control for automute mode */
4106 err = add_automute_mode_enum(codec);
4107 if (err < 0)
4108 return err;
4109 }
4110 return 0;
4111}
4112
Takashi Iwai352f7f92012-12-19 12:52:06 +01004113/* check whether all auto-mic pins are valid; setup indices if OK */
4114static bool auto_mic_check_imux(struct hda_codec *codec)
4115{
4116 struct hda_gen_spec *spec = codec->spec;
4117 const struct hda_input_mux *imux;
4118 int i;
4119
4120 imux = &spec->input_mux;
4121 for (i = 0; i < spec->am_num_entries; i++) {
4122 spec->am_entry[i].idx =
4123 find_idx_in_nid_list(spec->am_entry[i].pin,
4124 spec->imux_pins, imux->num_items);
4125 if (spec->am_entry[i].idx < 0)
4126 return false; /* no corresponding imux */
4127 }
4128
4129 /* we don't need the jack detection for the first pin */
4130 for (i = 1; i < spec->am_num_entries; i++)
4131 snd_hda_jack_detect_enable_callback(codec,
4132 spec->am_entry[i].pin,
4133 HDA_GEN_MIC_EVENT,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004134 call_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004135 return true;
4136}
4137
4138static int compare_attr(const void *ap, const void *bp)
4139{
4140 const struct automic_entry *a = ap;
4141 const struct automic_entry *b = bp;
4142 return (int)(a->attr - b->attr);
4143}
4144
4145/*
4146 * Check the availability of auto-mic switch;
4147 * Set up if really supported
4148 */
4149static int check_auto_mic_availability(struct hda_codec *codec)
4150{
4151 struct hda_gen_spec *spec = codec->spec;
4152 struct auto_pin_cfg *cfg = &spec->autocfg;
4153 unsigned int types;
4154 int i, num_pins;
4155
Takashi Iwaid12daf62013-01-07 16:32:11 +01004156 if (spec->suppress_auto_mic)
4157 return 0;
4158
Takashi Iwai352f7f92012-12-19 12:52:06 +01004159 types = 0;
4160 num_pins = 0;
4161 for (i = 0; i < cfg->num_inputs; i++) {
4162 hda_nid_t nid = cfg->inputs[i].pin;
4163 unsigned int attr;
4164 attr = snd_hda_codec_get_pincfg(codec, nid);
4165 attr = snd_hda_get_input_pin_attr(attr);
4166 if (types & (1 << attr))
4167 return 0; /* already occupied */
4168 switch (attr) {
4169 case INPUT_PIN_ATTR_INT:
4170 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4171 return 0; /* invalid type */
4172 break;
4173 case INPUT_PIN_ATTR_UNUSED:
4174 return 0; /* invalid entry */
4175 default:
4176 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4177 return 0; /* invalid type */
4178 if (!spec->line_in_auto_switch &&
4179 cfg->inputs[i].type != AUTO_PIN_MIC)
4180 return 0; /* only mic is allowed */
4181 if (!is_jack_detectable(codec, nid))
4182 return 0; /* no unsol support */
4183 break;
4184 }
4185 if (num_pins >= MAX_AUTO_MIC_PINS)
4186 return 0;
4187 types |= (1 << attr);
4188 spec->am_entry[num_pins].pin = nid;
4189 spec->am_entry[num_pins].attr = attr;
4190 num_pins++;
4191 }
4192
4193 if (num_pins < 2)
4194 return 0;
4195
4196 spec->am_num_entries = num_pins;
4197 /* sort the am_entry in the order of attr so that the pin with a
4198 * higher attr will be selected when the jack is plugged.
4199 */
4200 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4201 compare_attr, NULL);
4202
4203 if (!auto_mic_check_imux(codec))
4204 return 0;
4205
4206 spec->auto_mic = 1;
4207 spec->num_adc_nids = 1;
4208 spec->cur_mux[0] = spec->am_entry[0].idx;
4209 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
4210 spec->am_entry[0].pin,
4211 spec->am_entry[1].pin,
4212 spec->am_entry[2].pin);
4213
4214 return 0;
4215}
4216
Takashi Iwai55196ff2013-01-24 17:32:56 +01004217/* power_filter hook; make inactive widgets into power down */
4218static unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
4219 hda_nid_t nid,
4220 unsigned int power_state)
4221{
4222 if (power_state != AC_PWRST_D0)
4223 return power_state;
4224 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4225 return power_state;
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +02004226 if (is_active_nid_for_any(codec, nid))
Takashi Iwai55196ff2013-01-24 17:32:56 +01004227 return power_state;
4228 return AC_PWRST_D3;
4229}
4230
Takashi Iwai352f7f92012-12-19 12:52:06 +01004231
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004232/*
4233 * Parse the given BIOS configuration and set up the hda_gen_spec
4234 *
4235 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004236 * or a negative error code
4237 */
4238int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004239 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004240{
4241 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004242 int err;
4243
Takashi Iwai1c70a582013-01-11 17:48:22 +01004244 parse_user_hints(codec);
4245
Takashi Iwaie4a395e2013-01-23 17:00:31 +01004246 if (spec->mixer_nid && !spec->mixer_merge_nid)
4247 spec->mixer_merge_nid = spec->mixer_nid;
4248
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004249 if (cfg != &spec->autocfg) {
4250 spec->autocfg = *cfg;
4251 cfg = &spec->autocfg;
4252 }
4253
Takashi Iwai98bd1112013-03-22 14:53:50 +01004254 if (!spec->main_out_badness)
4255 spec->main_out_badness = &hda_main_out_badness;
4256 if (!spec->extra_out_badness)
4257 spec->extra_out_badness = &hda_extra_out_badness;
4258
David Henningsson6fc4cb92013-01-16 15:58:45 +01004259 fill_all_dac_nids(codec);
4260
Takashi Iwai352f7f92012-12-19 12:52:06 +01004261 if (!cfg->line_outs) {
4262 if (cfg->dig_outs || cfg->dig_in_pin) {
4263 spec->multiout.max_channels = 2;
4264 spec->no_analog = 1;
4265 goto dig_only;
4266 }
4267 return 0; /* can't find valid BIOS pin config */
4268 }
4269
4270 if (!spec->no_primary_hp &&
4271 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
4272 cfg->line_outs <= cfg->hp_outs) {
4273 /* use HP as primary out */
4274 cfg->speaker_outs = cfg->line_outs;
4275 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4276 sizeof(cfg->speaker_pins));
4277 cfg->line_outs = cfg->hp_outs;
4278 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4279 cfg->hp_outs = 0;
4280 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4281 cfg->line_out_type = AUTO_PIN_HP_OUT;
4282 }
4283
4284 err = parse_output_paths(codec);
4285 if (err < 0)
4286 return err;
4287 err = create_multi_channel_mode(codec);
4288 if (err < 0)
4289 return err;
4290 err = create_multi_out_ctls(codec, cfg);
4291 if (err < 0)
4292 return err;
4293 err = create_hp_out_ctls(codec);
4294 if (err < 0)
4295 return err;
4296 err = create_speaker_out_ctls(codec);
4297 if (err < 0)
4298 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004299 err = create_indep_hp_ctls(codec);
4300 if (err < 0)
4301 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01004302 err = create_loopback_mixing_ctl(codec);
4303 if (err < 0)
4304 return err;
Takashi Iwai967303d2013-02-19 17:12:42 +01004305 err = create_hp_mic(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004306 if (err < 0)
4307 return err;
4308 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02004309 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02004310 return err;
4311
Takashi Iwaia07a9492013-01-07 16:44:06 +01004312 spec->const_channel_count = spec->ext_channel_count;
4313 /* check the multiple speaker and headphone pins */
4314 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4315 spec->const_channel_count = max(spec->const_channel_count,
4316 cfg->speaker_outs * 2);
4317 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4318 spec->const_channel_count = max(spec->const_channel_count,
4319 cfg->hp_outs * 2);
4320 spec->multiout.max_channels = max(spec->ext_channel_count,
4321 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004322
4323 err = check_auto_mute_availability(codec);
4324 if (err < 0)
4325 return err;
4326
4327 err = check_dyn_adc_switch(codec);
4328 if (err < 0)
4329 return err;
4330
Takashi Iwai967303d2013-02-19 17:12:42 +01004331 err = check_auto_mic_availability(codec);
4332 if (err < 0)
4333 return err;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004334
Takashi Iwai352f7f92012-12-19 12:52:06 +01004335 err = create_capture_mixers(codec);
4336 if (err < 0)
4337 return err;
4338
4339 err = parse_mic_boost(codec);
4340 if (err < 0)
4341 return err;
4342
Takashi Iwaif811c3c2013-03-07 18:32:59 +01004343 if (spec->add_jack_modes) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01004344 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4345 err = create_out_jack_modes(codec, cfg->line_outs,
4346 cfg->line_out_pins);
4347 if (err < 0)
4348 return err;
4349 }
4350 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4351 err = create_out_jack_modes(codec, cfg->hp_outs,
4352 cfg->hp_pins);
4353 if (err < 0)
4354 return err;
4355 }
4356 }
4357
Takashi Iwai352f7f92012-12-19 12:52:06 +01004358 dig_only:
4359 parse_digital(codec);
4360
Takashi Iwai55196ff2013-01-24 17:32:56 +01004361 if (spec->power_down_unused)
4362 codec->power_filter = snd_hda_gen_path_power_filter;
4363
Takashi Iwai7504b6c2013-03-18 11:25:51 +01004364 if (!spec->no_analog && spec->beep_nid) {
4365 err = snd_hda_attach_beep_device(codec, spec->beep_nid);
4366 if (err < 0)
4367 return err;
4368 }
4369
Takashi Iwai352f7f92012-12-19 12:52:06 +01004370 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004372EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373
4374
4375/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004376 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004378
4379/* slave controls for virtual master */
4380static const char * const slave_pfxs[] = {
4381 "Front", "Surround", "Center", "LFE", "Side",
4382 "Headphone", "Speaker", "Mono", "Line Out",
4383 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01004384 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
4385 "Headphone Front", "Headphone Surround", "Headphone CLFE",
4386 "Headphone Side",
Takashi Iwai352f7f92012-12-19 12:52:06 +01004387 NULL,
4388};
4389
4390int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004392 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394
Takashi Iwai36502d02012-12-19 15:15:10 +01004395 if (spec->kctls.used) {
4396 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
4397 if (err < 0)
4398 return err;
4399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400
Takashi Iwai352f7f92012-12-19 12:52:06 +01004401 if (spec->multiout.dig_out_nid) {
4402 err = snd_hda_create_dig_out_ctls(codec,
4403 spec->multiout.dig_out_nid,
4404 spec->multiout.dig_out_nid,
4405 spec->pcm_rec[1].pcm_type);
4406 if (err < 0)
4407 return err;
4408 if (!spec->no_analog) {
4409 err = snd_hda_create_spdif_share_sw(codec,
4410 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411 if (err < 0)
4412 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004413 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 }
4415 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004416 if (spec->dig_in_nid) {
4417 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
4418 if (err < 0)
4419 return err;
4420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421
Takashi Iwai352f7f92012-12-19 12:52:06 +01004422 /* if we have no master control, let's create it */
4423 if (!spec->no_analog &&
4424 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004425 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01004426 spec->vmaster_tlv, slave_pfxs,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004427 "Playback Volume");
4428 if (err < 0)
4429 return err;
4430 }
4431 if (!spec->no_analog &&
4432 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
4433 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
4434 NULL, slave_pfxs,
4435 "Playback Switch",
4436 true, &spec->vmaster_mute.sw_kctl);
4437 if (err < 0)
4438 return err;
4439 if (spec->vmaster_mute.hook)
Takashi Iwaifd25a972012-12-20 14:57:18 +01004440 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
4441 spec->vmaster_mute_enum);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443
Takashi Iwai352f7f92012-12-19 12:52:06 +01004444 free_kctls(spec); /* no longer needed */
4445
Takashi Iwai352f7f92012-12-19 12:52:06 +01004446 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
4447 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 return err;
4449
4450 return 0;
4451}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004452EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
4453
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454
4455/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004456 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004459static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
4460 struct hda_codec *codec,
4461 struct snd_pcm_substream *substream,
4462 int action)
4463{
4464 struct hda_gen_spec *spec = codec->spec;
4465 if (spec->pcm_playback_hook)
4466 spec->pcm_playback_hook(hinfo, codec, substream, action);
4467}
4468
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004469static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
4470 struct hda_codec *codec,
4471 struct snd_pcm_substream *substream,
4472 int action)
4473{
4474 struct hda_gen_spec *spec = codec->spec;
4475 if (spec->pcm_capture_hook)
4476 spec->pcm_capture_hook(hinfo, codec, substream, action);
4477}
4478
Takashi Iwai352f7f92012-12-19 12:52:06 +01004479/*
4480 * Analog playback callbacks
4481 */
4482static int playback_pcm_open(struct hda_pcm_stream *hinfo,
4483 struct hda_codec *codec,
4484 struct snd_pcm_substream *substream)
4485{
4486 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004487 int err;
4488
4489 mutex_lock(&spec->pcm_mutex);
4490 err = snd_hda_multi_out_analog_open(codec,
4491 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004492 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004493 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004494 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004495 call_pcm_playback_hook(hinfo, codec, substream,
4496 HDA_GEN_PCM_ACT_OPEN);
4497 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004498 mutex_unlock(&spec->pcm_mutex);
4499 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004500}
4501
4502static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01004503 struct hda_codec *codec,
4504 unsigned int stream_tag,
4505 unsigned int format,
4506 struct snd_pcm_substream *substream)
4507{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004508 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004509 int err;
4510
4511 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
4512 stream_tag, format, substream);
4513 if (!err)
4514 call_pcm_playback_hook(hinfo, codec, substream,
4515 HDA_GEN_PCM_ACT_PREPARE);
4516 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004517}
Takashi Iwai97ec5582006-03-21 11:29:07 +01004518
Takashi Iwai352f7f92012-12-19 12:52:06 +01004519static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4520 struct hda_codec *codec,
4521 struct snd_pcm_substream *substream)
4522{
4523 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004524 int err;
4525
4526 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
4527 if (!err)
4528 call_pcm_playback_hook(hinfo, codec, substream,
4529 HDA_GEN_PCM_ACT_CLEANUP);
4530 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004531}
4532
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004533static int playback_pcm_close(struct hda_pcm_stream *hinfo,
4534 struct hda_codec *codec,
4535 struct snd_pcm_substream *substream)
4536{
4537 struct hda_gen_spec *spec = codec->spec;
4538 mutex_lock(&spec->pcm_mutex);
4539 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004540 call_pcm_playback_hook(hinfo, codec, substream,
4541 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004542 mutex_unlock(&spec->pcm_mutex);
4543 return 0;
4544}
4545
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004546static int capture_pcm_open(struct hda_pcm_stream *hinfo,
4547 struct hda_codec *codec,
4548 struct snd_pcm_substream *substream)
4549{
4550 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
4551 return 0;
4552}
4553
4554static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4555 struct hda_codec *codec,
4556 unsigned int stream_tag,
4557 unsigned int format,
4558 struct snd_pcm_substream *substream)
4559{
4560 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4561 call_pcm_capture_hook(hinfo, codec, substream,
4562 HDA_GEN_PCM_ACT_PREPARE);
4563 return 0;
4564}
4565
4566static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4567 struct hda_codec *codec,
4568 struct snd_pcm_substream *substream)
4569{
4570 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4571 call_pcm_capture_hook(hinfo, codec, substream,
4572 HDA_GEN_PCM_ACT_CLEANUP);
4573 return 0;
4574}
4575
4576static int capture_pcm_close(struct hda_pcm_stream *hinfo,
4577 struct hda_codec *codec,
4578 struct snd_pcm_substream *substream)
4579{
4580 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
4581 return 0;
4582}
4583
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004584static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
4585 struct hda_codec *codec,
4586 struct snd_pcm_substream *substream)
4587{
4588 struct hda_gen_spec *spec = codec->spec;
4589 int err = 0;
4590
4591 mutex_lock(&spec->pcm_mutex);
4592 if (!spec->indep_hp_enabled)
4593 err = -EBUSY;
4594 else
4595 spec->active_streams |= 1 << STREAM_INDEP_HP;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004596 call_pcm_playback_hook(hinfo, codec, substream,
4597 HDA_GEN_PCM_ACT_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004598 mutex_unlock(&spec->pcm_mutex);
4599 return err;
4600}
4601
4602static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
4603 struct hda_codec *codec,
4604 struct snd_pcm_substream *substream)
4605{
4606 struct hda_gen_spec *spec = codec->spec;
4607 mutex_lock(&spec->pcm_mutex);
4608 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004609 call_pcm_playback_hook(hinfo, codec, substream,
4610 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004611 mutex_unlock(&spec->pcm_mutex);
4612 return 0;
4613}
4614
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004615static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4616 struct hda_codec *codec,
4617 unsigned int stream_tag,
4618 unsigned int format,
4619 struct snd_pcm_substream *substream)
4620{
4621 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4622 call_pcm_playback_hook(hinfo, codec, substream,
4623 HDA_GEN_PCM_ACT_PREPARE);
4624 return 0;
4625}
4626
4627static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4628 struct hda_codec *codec,
4629 struct snd_pcm_substream *substream)
4630{
4631 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4632 call_pcm_playback_hook(hinfo, codec, substream,
4633 HDA_GEN_PCM_ACT_CLEANUP);
4634 return 0;
4635}
4636
Takashi Iwai352f7f92012-12-19 12:52:06 +01004637/*
4638 * Digital out
4639 */
4640static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
4641 struct hda_codec *codec,
4642 struct snd_pcm_substream *substream)
4643{
4644 struct hda_gen_spec *spec = codec->spec;
4645 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
4646}
4647
4648static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4649 struct hda_codec *codec,
4650 unsigned int stream_tag,
4651 unsigned int format,
4652 struct snd_pcm_substream *substream)
4653{
4654 struct hda_gen_spec *spec = codec->spec;
4655 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
4656 stream_tag, format, substream);
4657}
4658
4659static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4660 struct hda_codec *codec,
4661 struct snd_pcm_substream *substream)
4662{
4663 struct hda_gen_spec *spec = codec->spec;
4664 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
4665}
4666
4667static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
4668 struct hda_codec *codec,
4669 struct snd_pcm_substream *substream)
4670{
4671 struct hda_gen_spec *spec = codec->spec;
4672 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
4673}
4674
4675/*
4676 * Analog capture
4677 */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004678#define alt_capture_pcm_open capture_pcm_open
4679#define alt_capture_pcm_close capture_pcm_close
4680
Takashi Iwai352f7f92012-12-19 12:52:06 +01004681static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4682 struct hda_codec *codec,
4683 unsigned int stream_tag,
4684 unsigned int format,
4685 struct snd_pcm_substream *substream)
4686{
4687 struct hda_gen_spec *spec = codec->spec;
4688
4689 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01004690 stream_tag, 0, format);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004691 call_pcm_capture_hook(hinfo, codec, substream,
4692 HDA_GEN_PCM_ACT_PREPARE);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004693 return 0;
4694}
4695
Takashi Iwai352f7f92012-12-19 12:52:06 +01004696static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4697 struct hda_codec *codec,
4698 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01004699{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004700 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01004701
Takashi Iwai352f7f92012-12-19 12:52:06 +01004702 snd_hda_codec_cleanup_stream(codec,
4703 spec->adc_nids[substream->number + 1]);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004704 call_pcm_capture_hook(hinfo, codec, substream,
4705 HDA_GEN_PCM_ACT_CLEANUP);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004706 return 0;
4707}
4708
Takashi Iwai352f7f92012-12-19 12:52:06 +01004709/*
4710 */
4711static const struct hda_pcm_stream pcm_analog_playback = {
4712 .substreams = 1,
4713 .channels_min = 2,
4714 .channels_max = 8,
4715 /* NID is set in build_pcms */
4716 .ops = {
4717 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004718 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004719 .prepare = playback_pcm_prepare,
4720 .cleanup = playback_pcm_cleanup
4721 },
4722};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723
Takashi Iwai352f7f92012-12-19 12:52:06 +01004724static const struct hda_pcm_stream pcm_analog_capture = {
4725 .substreams = 1,
4726 .channels_min = 2,
4727 .channels_max = 2,
4728 /* NID is set in build_pcms */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004729 .ops = {
4730 .open = capture_pcm_open,
4731 .close = capture_pcm_close,
4732 .prepare = capture_pcm_prepare,
4733 .cleanup = capture_pcm_cleanup
4734 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004735};
4736
4737static const struct hda_pcm_stream pcm_analog_alt_playback = {
4738 .substreams = 1,
4739 .channels_min = 2,
4740 .channels_max = 2,
4741 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004742 .ops = {
4743 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004744 .close = alt_playback_pcm_close,
4745 .prepare = alt_playback_pcm_prepare,
4746 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004747 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004748};
4749
4750static const struct hda_pcm_stream pcm_analog_alt_capture = {
4751 .substreams = 2, /* can be overridden */
4752 .channels_min = 2,
4753 .channels_max = 2,
4754 /* NID is set in build_pcms */
4755 .ops = {
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004756 .open = alt_capture_pcm_open,
4757 .close = alt_capture_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004758 .prepare = alt_capture_pcm_prepare,
4759 .cleanup = alt_capture_pcm_cleanup
4760 },
4761};
4762
4763static const struct hda_pcm_stream pcm_digital_playback = {
4764 .substreams = 1,
4765 .channels_min = 2,
4766 .channels_max = 2,
4767 /* NID is set in build_pcms */
4768 .ops = {
4769 .open = dig_playback_pcm_open,
4770 .close = dig_playback_pcm_close,
4771 .prepare = dig_playback_pcm_prepare,
4772 .cleanup = dig_playback_pcm_cleanup
4773 },
4774};
4775
4776static const struct hda_pcm_stream pcm_digital_capture = {
4777 .substreams = 1,
4778 .channels_min = 2,
4779 .channels_max = 2,
4780 /* NID is set in build_pcms */
4781};
4782
4783/* Used by build_pcms to flag that a PCM has no playback stream */
4784static const struct hda_pcm_stream pcm_null_stream = {
4785 .substreams = 0,
4786 .channels_min = 0,
4787 .channels_max = 0,
4788};
4789
4790/*
4791 * dynamic changing ADC PCM streams
4792 */
4793static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
4794{
4795 struct hda_gen_spec *spec = codec->spec;
4796 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
4797
4798 if (spec->cur_adc && spec->cur_adc != new_adc) {
4799 /* stream is running, let's swap the current ADC */
4800 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
4801 spec->cur_adc = new_adc;
4802 snd_hda_codec_setup_stream(codec, new_adc,
4803 spec->cur_adc_stream_tag, 0,
4804 spec->cur_adc_format);
4805 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004807 return false;
4808}
4809
4810/* analog capture with dynamic dual-adc changes */
4811static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4812 struct hda_codec *codec,
4813 unsigned int stream_tag,
4814 unsigned int format,
4815 struct snd_pcm_substream *substream)
4816{
4817 struct hda_gen_spec *spec = codec->spec;
4818 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
4819 spec->cur_adc_stream_tag = stream_tag;
4820 spec->cur_adc_format = format;
4821 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
4822 return 0;
4823}
4824
4825static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4826 struct hda_codec *codec,
4827 struct snd_pcm_substream *substream)
4828{
4829 struct hda_gen_spec *spec = codec->spec;
4830 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
4831 spec->cur_adc = 0;
4832 return 0;
4833}
4834
4835static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
4836 .substreams = 1,
4837 .channels_min = 2,
4838 .channels_max = 2,
4839 .nid = 0, /* fill later */
4840 .ops = {
4841 .prepare = dyn_adc_capture_pcm_prepare,
4842 .cleanup = dyn_adc_capture_pcm_cleanup
4843 },
4844};
4845
Takashi Iwaif873e532012-12-20 16:58:39 +01004846static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
4847 const char *chip_name)
4848{
4849 char *p;
4850
4851 if (*str)
4852 return;
4853 strlcpy(str, chip_name, len);
4854
4855 /* drop non-alnum chars after a space */
4856 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
4857 if (!isalnum(p[1])) {
4858 *p = 0;
4859 break;
4860 }
4861 }
4862 strlcat(str, sfx, len);
4863}
4864
Takashi Iwai352f7f92012-12-19 12:52:06 +01004865/* build PCM streams based on the parsed results */
4866int snd_hda_gen_build_pcms(struct hda_codec *codec)
4867{
4868 struct hda_gen_spec *spec = codec->spec;
4869 struct hda_pcm *info = spec->pcm_rec;
4870 const struct hda_pcm_stream *p;
4871 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872
4873 codec->num_pcms = 1;
4874 codec->pcm_info = info;
4875
Takashi Iwai352f7f92012-12-19 12:52:06 +01004876 if (spec->no_analog)
4877 goto skip_analog;
4878
Takashi Iwaif873e532012-12-20 16:58:39 +01004879 fill_pcm_stream_name(spec->stream_name_analog,
4880 sizeof(spec->stream_name_analog),
4881 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004882 info->name = spec->stream_name_analog;
4883
4884 if (spec->multiout.num_dacs > 0) {
4885 p = spec->stream_analog_playback;
4886 if (!p)
4887 p = &pcm_analog_playback;
4888 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4889 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
4890 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
4891 spec->multiout.max_channels;
4892 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
4893 spec->autocfg.line_outs == 2)
4894 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
4895 snd_pcm_2_1_chmaps;
4896 }
4897 if (spec->num_adc_nids) {
4898 p = spec->stream_analog_capture;
4899 if (!p) {
4900 if (spec->dyn_adc_switch)
4901 p = &dyn_adc_pcm_analog_capture;
4902 else
4903 p = &pcm_analog_capture;
4904 }
4905 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4906 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
4907 }
4908
Takashi Iwai352f7f92012-12-19 12:52:06 +01004909 skip_analog:
4910 /* SPDIF for stream index #1 */
4911 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01004912 fill_pcm_stream_name(spec->stream_name_digital,
4913 sizeof(spec->stream_name_digital),
4914 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004915 codec->num_pcms = 2;
4916 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
4917 info = spec->pcm_rec + 1;
4918 info->name = spec->stream_name_digital;
4919 if (spec->dig_out_type)
4920 info->pcm_type = spec->dig_out_type;
4921 else
4922 info->pcm_type = HDA_PCM_TYPE_SPDIF;
4923 if (spec->multiout.dig_out_nid) {
4924 p = spec->stream_digital_playback;
4925 if (!p)
4926 p = &pcm_digital_playback;
4927 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4928 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
4929 }
4930 if (spec->dig_in_nid) {
4931 p = spec->stream_digital_capture;
4932 if (!p)
4933 p = &pcm_digital_capture;
4934 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4935 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
4936 }
4937 }
4938
4939 if (spec->no_analog)
4940 return 0;
4941
4942 /* If the use of more than one ADC is requested for the current
4943 * model, configure a second analog capture-only PCM.
4944 */
4945 have_multi_adcs = (spec->num_adc_nids > 1) &&
4946 !spec->dyn_adc_switch && !spec->auto_mic;
4947 /* Additional Analaog capture for index #2 */
4948 if (spec->alt_dac_nid || have_multi_adcs) {
Takashi Iwaia6071482013-01-21 16:50:09 +01004949 fill_pcm_stream_name(spec->stream_name_alt_analog,
4950 sizeof(spec->stream_name_alt_analog),
4951 " Alt Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004952 codec->num_pcms = 3;
4953 info = spec->pcm_rec + 2;
Takashi Iwaia6071482013-01-21 16:50:09 +01004954 info->name = spec->stream_name_alt_analog;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004955 if (spec->alt_dac_nid) {
4956 p = spec->stream_analog_alt_playback;
4957 if (!p)
4958 p = &pcm_analog_alt_playback;
4959 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4960 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
4961 spec->alt_dac_nid;
4962 } else {
4963 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
4964 pcm_null_stream;
4965 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
4966 }
4967 if (have_multi_adcs) {
4968 p = spec->stream_analog_alt_capture;
4969 if (!p)
4970 p = &pcm_analog_alt_capture;
4971 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4972 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
4973 spec->adc_nids[1];
4974 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
4975 spec->num_adc_nids - 1;
4976 } else {
4977 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
4978 pcm_null_stream;
4979 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
4980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 }
4982
4983 return 0;
4984}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004985EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
4986
4987
4988/*
4989 * Standard auto-parser initializations
4990 */
4991
Takashi Iwaid4156932013-01-07 10:08:02 +01004992/* configure the given path as a proper output */
Takashi Iwai2c12c302013-01-10 09:33:29 +01004993static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004994{
4995 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01004996 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004997
Takashi Iwai196c17662013-01-04 15:01:40 +01004998 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01004999 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005000 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01005001 pin = path->path[path->depth - 1];
Takashi Iwai2c12c302013-01-10 09:33:29 +01005002 restore_pin_ctl(codec, pin);
Takashi Iwai65033cc2013-04-16 12:31:05 +02005003 snd_hda_activate_path(codec, path, path->active,
5004 aamix_default(codec->spec));
Takashi Iwaie1284af2013-01-03 16:33:02 +01005005 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005006}
5007
5008/* initialize primary output paths */
5009static void init_multi_out(struct hda_codec *codec)
5010{
5011 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005012 int i;
5013
Takashi Iwaid4156932013-01-07 10:08:02 +01005014 for (i = 0; i < spec->autocfg.line_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005015 set_output_and_unmute(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005016}
5017
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005018
Takashi Iwai2c12c302013-01-10 09:33:29 +01005019static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005020{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005021 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005022
Takashi Iwaid4156932013-01-07 10:08:02 +01005023 for (i = 0; i < num_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005024 set_output_and_unmute(codec, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005025}
5026
5027/* initialize hp and speaker paths */
5028static void init_extra_out(struct hda_codec *codec)
5029{
5030 struct hda_gen_spec *spec = codec->spec;
5031
5032 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005033 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005034 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
5035 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai2c12c302013-01-10 09:33:29 +01005036 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005037}
5038
5039/* initialize multi-io paths */
5040static void init_multi_io(struct hda_codec *codec)
5041{
5042 struct hda_gen_spec *spec = codec->spec;
5043 int i;
5044
5045 for (i = 0; i < spec->multi_ios; i++) {
5046 hda_nid_t pin = spec->multi_io[i].pin;
5047 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01005048 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005049 if (!path)
5050 continue;
5051 if (!spec->multi_io[i].ctl_in)
5052 spec->multi_io[i].ctl_in =
Takashi Iwai2c12c302013-01-10 09:33:29 +01005053 snd_hda_codec_get_pin_target(codec, pin);
Takashi Iwai65033cc2013-04-16 12:31:05 +02005054 snd_hda_activate_path(codec, path, path->active,
5055 aamix_default(spec));
Takashi Iwai352f7f92012-12-19 12:52:06 +01005056 }
5057}
5058
Takashi Iwai352f7f92012-12-19 12:52:06 +01005059/* set up input pins and loopback paths */
5060static void init_analog_input(struct hda_codec *codec)
5061{
5062 struct hda_gen_spec *spec = codec->spec;
5063 struct auto_pin_cfg *cfg = &spec->autocfg;
5064 int i;
5065
5066 for (i = 0; i < cfg->num_inputs; i++) {
5067 hda_nid_t nid = cfg->inputs[i].pin;
5068 if (is_input_pin(codec, nid))
Takashi Iwai2c12c302013-01-10 09:33:29 +01005069 restore_pin_ctl(codec, nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005070
5071 /* init loopback inputs */
5072 if (spec->mixer_nid) {
Takashi Iwai3e367f12013-01-23 17:07:23 +01005073 resume_path_from_idx(codec, spec->loopback_paths[i]);
5074 resume_path_from_idx(codec, spec->loopback_merge_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005075 }
5076 }
5077}
5078
5079/* initialize ADC paths */
5080static void init_input_src(struct hda_codec *codec)
5081{
5082 struct hda_gen_spec *spec = codec->spec;
5083 struct hda_input_mux *imux = &spec->input_mux;
5084 struct nid_path *path;
5085 int i, c, nums;
5086
5087 if (spec->dyn_adc_switch)
5088 nums = 1;
5089 else
5090 nums = spec->num_adc_nids;
5091
5092 for (c = 0; c < nums; c++) {
5093 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01005094 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005095 if (path) {
5096 bool active = path->active;
5097 if (i == spec->cur_mux[c])
5098 active = true;
5099 snd_hda_activate_path(codec, path, active, false);
5100 }
5101 }
Takashi Iwai967303d2013-02-19 17:12:42 +01005102 if (spec->hp_mic)
5103 update_hp_mic(codec, c, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005104 }
5105
Takashi Iwai352f7f92012-12-19 12:52:06 +01005106 if (spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01005107 spec->cap_sync_hook(codec, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005108}
5109
5110/* set right pin controls for digital I/O */
5111static void init_digital(struct hda_codec *codec)
5112{
5113 struct hda_gen_spec *spec = codec->spec;
5114 int i;
5115 hda_nid_t pin;
5116
Takashi Iwaid4156932013-01-07 10:08:02 +01005117 for (i = 0; i < spec->autocfg.dig_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005118 set_output_and_unmute(codec, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005119 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005120 if (pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01005121 restore_pin_ctl(codec, pin);
Takashi Iwai3e367f12013-01-23 17:07:23 +01005122 resume_path_from_idx(codec, spec->digin_path);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005123 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005124}
5125
Takashi Iwai973e4972012-12-20 15:16:09 +01005126/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5127 * invalid unsol tags by some reason
5128 */
5129static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5130{
5131 int i;
5132
5133 for (i = 0; i < codec->init_pins.used; i++) {
5134 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
5135 hda_nid_t nid = pin->nid;
5136 if (is_jack_detectable(codec, nid) &&
5137 !snd_hda_jack_tbl_get(codec, nid))
5138 snd_hda_codec_update_cache(codec, nid, 0,
5139 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5140 }
5141}
5142
Takashi Iwai5187ac12013-01-07 12:52:16 +01005143/*
5144 * initialize the generic spec;
5145 * this can be put as patch_ops.init function
5146 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005147int snd_hda_gen_init(struct hda_codec *codec)
5148{
5149 struct hda_gen_spec *spec = codec->spec;
5150
5151 if (spec->init_hook)
5152 spec->init_hook(codec);
5153
5154 snd_hda_apply_verbs(codec);
5155
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005156 codec->cached_write = 1;
5157
Takashi Iwai352f7f92012-12-19 12:52:06 +01005158 init_multi_out(codec);
5159 init_extra_out(codec);
5160 init_multi_io(codec);
5161 init_analog_input(codec);
5162 init_input_src(codec);
5163 init_digital(codec);
5164
Takashi Iwai973e4972012-12-20 15:16:09 +01005165 clear_unsol_on_unused_pins(codec);
5166
Takashi Iwai352f7f92012-12-19 12:52:06 +01005167 /* call init functions of standard auto-mute helpers */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01005168 update_automute_all(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005169
Takashi Iwaidc870f32013-01-22 15:24:30 +01005170 snd_hda_codec_flush_cache(codec);
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005171
Takashi Iwai352f7f92012-12-19 12:52:06 +01005172 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
5173 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5174
5175 hda_call_check_power_status(codec, 0x01);
5176 return 0;
5177}
Takashi Iwaifce52a32013-01-07 12:42:48 +01005178EXPORT_SYMBOL_HDA(snd_hda_gen_init);
5179
Takashi Iwai5187ac12013-01-07 12:52:16 +01005180/*
5181 * free the generic spec;
5182 * this can be put as patch_ops.free function
5183 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005184void snd_hda_gen_free(struct hda_codec *codec)
5185{
Takashi Iwai7504b6c2013-03-18 11:25:51 +01005186 snd_hda_detach_beep_device(codec);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005187 snd_hda_gen_spec_free(codec->spec);
5188 kfree(codec->spec);
5189 codec->spec = NULL;
5190}
5191EXPORT_SYMBOL_HDA(snd_hda_gen_free);
5192
5193#ifdef CONFIG_PM
Takashi Iwai5187ac12013-01-07 12:52:16 +01005194/*
5195 * check the loopback power save state;
5196 * this can be put as patch_ops.check_power_status function
5197 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005198int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
5199{
5200 struct hda_gen_spec *spec = codec->spec;
5201 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
5202}
5203EXPORT_SYMBOL_HDA(snd_hda_gen_check_power_status);
5204#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01005205
5206
5207/*
5208 * the generic codec support
5209 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210
Takashi Iwai352f7f92012-12-19 12:52:06 +01005211static const struct hda_codec_ops generic_patch_ops = {
5212 .build_controls = snd_hda_gen_build_controls,
5213 .build_pcms = snd_hda_gen_build_pcms,
5214 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01005215 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005216 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02005217#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01005218 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02005219#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220};
5221
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222int snd_hda_parse_generic_codec(struct hda_codec *codec)
5223{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005224 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225 int err;
5226
Takashi Iwaie560d8d2005-09-09 14:21:46 +02005227 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005228 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005230 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232
Takashi Iwai9eb413e2012-12-19 14:41:21 +01005233 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
5234 if (err < 0)
5235 return err;
5236
5237 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005238 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005239 goto error;
5240
5241 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242 return 0;
5243
Takashi Iwai352f7f92012-12-19 12:52:06 +01005244error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01005245 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246 return err;
5247}
Takashi Iwaifce52a32013-01-07 12:42:48 +01005248EXPORT_SYMBOL_HDA(snd_hda_parse_generic_codec);