blob: 5ba3106b9712890492032bdb83e8fcb57e3547cc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Universal Interface for Intel High Definition Audio Codec
3 *
4 * Generic widget tree parser
5 *
6 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
7 *
8 * This driver is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This driver is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/init.h>
24#include <linux/slab.h>
Paul Gortmakerd81a6d72011-09-22 09:34:58 -040025#include <linux/export.h>
Takashi Iwai352f7f92012-12-19 12:52:06 +010026#include <linux/sort.h>
Takashi Iwai55196ff2013-01-24 17:32:56 +010027#include <linux/delay.h>
Takashi Iwaif873e532012-12-20 16:58:39 +010028#include <linux/ctype.h>
29#include <linux/string.h>
Takashi Iwai294765582013-01-17 09:52:11 +010030#include <linux/bitops.h>
Takashi Iwaib21bdd02013-11-18 12:03:56 +010031#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <sound/core.h>
Takashi Iwai352f7f92012-12-19 12:52:06 +010033#include <sound/jack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "hda_codec.h"
35#include "hda_local.h"
Takashi Iwai352f7f92012-12-19 12:52:06 +010036#include "hda_auto_parser.h"
37#include "hda_jack.h"
Takashi Iwai7504b6c2013-03-18 11:25:51 +010038#include "hda_beep.h"
Takashi Iwai352f7f92012-12-19 12:52:06 +010039#include "hda_generic.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Takashi Iwai352f7f92012-12-19 12:52:06 +010042/* initialize hda_gen_spec struct */
43int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
Takashi Iwai352f7f92012-12-19 12:52:06 +010045 snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
Takashi Iwai352f7f92012-12-19 12:52:06 +010046 snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
Takashi Iwai0186f4f2013-02-07 10:45:11 +010047 snd_array_init(&spec->loopback_list, sizeof(struct hda_amp_list), 8);
Takashi Iwai38cf6f12012-12-21 14:09:42 +010048 mutex_init(&spec->pcm_mutex);
Takashi Iwai352f7f92012-12-19 12:52:06 +010049 return 0;
50}
51EXPORT_SYMBOL_HDA(snd_hda_gen_spec_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Takashi Iwai12c93df2012-12-19 14:38:33 +010053struct snd_kcontrol_new *
54snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
55 const struct snd_kcontrol_new *temp)
Takashi Iwai352f7f92012-12-19 12:52:06 +010056{
57 struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
58 if (!knew)
59 return NULL;
60 *knew = *temp;
61 if (name)
62 knew->name = kstrdup(name, GFP_KERNEL);
63 else if (knew->name)
64 knew->name = kstrdup(knew->name, GFP_KERNEL);
65 if (!knew->name)
66 return NULL;
67 return knew;
68}
Takashi Iwai12c93df2012-12-19 14:38:33 +010069EXPORT_SYMBOL_HDA(snd_hda_gen_add_kctl);
Takashi Iwai352f7f92012-12-19 12:52:06 +010070
71static void free_kctls(struct hda_gen_spec *spec)
72{
73 if (spec->kctls.list) {
74 struct snd_kcontrol_new *kctl = spec->kctls.list;
75 int i;
76 for (i = 0; i < spec->kctls.used; i++)
77 kfree(kctl[i].name);
78 }
79 snd_array_free(&spec->kctls);
80}
81
Takashi Iwai352f7f92012-12-19 12:52:06 +010082void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
83{
84 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 return;
Takashi Iwai352f7f92012-12-19 12:52:06 +010086 free_kctls(spec);
Takashi Iwai352f7f92012-12-19 12:52:06 +010087 snd_array_free(&spec->paths);
Takashi Iwai0186f4f2013-02-07 10:45:11 +010088 snd_array_free(&spec->loopback_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
Takashi Iwai352f7f92012-12-19 12:52:06 +010090EXPORT_SYMBOL_HDA(snd_hda_gen_spec_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92/*
Takashi Iwai1c70a582013-01-11 17:48:22 +010093 * store user hints
94 */
95static void parse_user_hints(struct hda_codec *codec)
96{
97 struct hda_gen_spec *spec = codec->spec;
98 int val;
99
100 val = snd_hda_get_bool_hint(codec, "jack_detect");
101 if (val >= 0)
102 codec->no_jack_detect = !val;
103 val = snd_hda_get_bool_hint(codec, "inv_jack_detect");
104 if (val >= 0)
105 codec->inv_jack_detect = !!val;
106 val = snd_hda_get_bool_hint(codec, "trigger_sense");
107 if (val >= 0)
108 codec->no_trigger_sense = !val;
109 val = snd_hda_get_bool_hint(codec, "inv_eapd");
110 if (val >= 0)
111 codec->inv_eapd = !!val;
112 val = snd_hda_get_bool_hint(codec, "pcm_format_first");
113 if (val >= 0)
114 codec->pcm_format_first = !!val;
115 val = snd_hda_get_bool_hint(codec, "sticky_stream");
116 if (val >= 0)
117 codec->no_sticky_stream = !val;
118 val = snd_hda_get_bool_hint(codec, "spdif_status_reset");
119 if (val >= 0)
120 codec->spdif_status_reset = !!val;
121 val = snd_hda_get_bool_hint(codec, "pin_amp_workaround");
122 if (val >= 0)
123 codec->pin_amp_workaround = !!val;
124 val = snd_hda_get_bool_hint(codec, "single_adc_amp");
125 if (val >= 0)
126 codec->single_adc_amp = !!val;
127
Takashi Iwaif72706b2013-01-16 18:20:07 +0100128 val = snd_hda_get_bool_hint(codec, "auto_mute");
129 if (val >= 0)
130 spec->suppress_auto_mute = !val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100131 val = snd_hda_get_bool_hint(codec, "auto_mic");
132 if (val >= 0)
133 spec->suppress_auto_mic = !val;
134 val = snd_hda_get_bool_hint(codec, "line_in_auto_switch");
135 if (val >= 0)
136 spec->line_in_auto_switch = !!val;
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200137 val = snd_hda_get_bool_hint(codec, "auto_mute_via_amp");
138 if (val >= 0)
139 spec->auto_mute_via_amp = !!val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100140 val = snd_hda_get_bool_hint(codec, "need_dac_fix");
141 if (val >= 0)
142 spec->need_dac_fix = !!val;
143 val = snd_hda_get_bool_hint(codec, "primary_hp");
144 if (val >= 0)
145 spec->no_primary_hp = !val;
Takashi Iwaida96fb52013-07-29 16:54:36 +0200146 val = snd_hda_get_bool_hint(codec, "multi_io");
147 if (val >= 0)
148 spec->no_multi_io = !val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100149 val = snd_hda_get_bool_hint(codec, "multi_cap_vol");
150 if (val >= 0)
151 spec->multi_cap_vol = !!val;
152 val = snd_hda_get_bool_hint(codec, "inv_dmic_split");
153 if (val >= 0)
154 spec->inv_dmic_split = !!val;
155 val = snd_hda_get_bool_hint(codec, "indep_hp");
156 if (val >= 0)
157 spec->indep_hp = !!val;
158 val = snd_hda_get_bool_hint(codec, "add_stereo_mix_input");
159 if (val >= 0)
160 spec->add_stereo_mix_input = !!val;
Takashi Iwaif811c3c2013-03-07 18:32:59 +0100161 /* the following two are just for compatibility */
Takashi Iwai1c70a582013-01-11 17:48:22 +0100162 val = snd_hda_get_bool_hint(codec, "add_out_jack_modes");
163 if (val >= 0)
Takashi Iwaif811c3c2013-03-07 18:32:59 +0100164 spec->add_jack_modes = !!val;
Takashi Iwai294765582013-01-17 09:52:11 +0100165 val = snd_hda_get_bool_hint(codec, "add_in_jack_modes");
166 if (val >= 0)
Takashi Iwaif811c3c2013-03-07 18:32:59 +0100167 spec->add_jack_modes = !!val;
168 val = snd_hda_get_bool_hint(codec, "add_jack_modes");
169 if (val >= 0)
170 spec->add_jack_modes = !!val;
Takashi Iwai55196ff2013-01-24 17:32:56 +0100171 val = snd_hda_get_bool_hint(codec, "power_down_unused");
172 if (val >= 0)
173 spec->power_down_unused = !!val;
Takashi Iwai967303d2013-02-19 17:12:42 +0100174 val = snd_hda_get_bool_hint(codec, "add_hp_mic");
175 if (val >= 0)
176 spec->hp_mic = !!val;
177 val = snd_hda_get_bool_hint(codec, "hp_mic_detect");
178 if (val >= 0)
179 spec->suppress_hp_mic_detect = !val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100180
181 if (!snd_hda_get_int_hint(codec, "mixer_nid", &val))
182 spec->mixer_nid = val;
183}
184
185/*
Takashi Iwai2c12c302013-01-10 09:33:29 +0100186 * pin control value accesses
187 */
188
189#define update_pin_ctl(codec, pin, val) \
190 snd_hda_codec_update_cache(codec, pin, 0, \
191 AC_VERB_SET_PIN_WIDGET_CONTROL, val)
192
193/* restore the pinctl based on the cached value */
194static inline void restore_pin_ctl(struct hda_codec *codec, hda_nid_t pin)
195{
196 update_pin_ctl(codec, pin, snd_hda_codec_get_pin_target(codec, pin));
197}
198
199/* set the pinctl target value and write it if requested */
200static void set_pin_target(struct hda_codec *codec, hda_nid_t pin,
201 unsigned int val, bool do_write)
202{
203 if (!pin)
204 return;
205 val = snd_hda_correct_pin_ctl(codec, pin, val);
206 snd_hda_codec_set_pin_target(codec, pin, val);
207 if (do_write)
208 update_pin_ctl(codec, pin, val);
209}
210
211/* set pinctl target values for all given pins */
212static void set_pin_targets(struct hda_codec *codec, int num_pins,
213 hda_nid_t *pins, unsigned int val)
214{
215 int i;
216 for (i = 0; i < num_pins; i++)
217 set_pin_target(codec, pins[i], val, false);
218}
219
220/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100221 * parsing paths
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100224/* return the position of NID in the list, or -1 if not found */
225static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
226{
227 int i;
228 for (i = 0; i < nums; i++)
229 if (list[i] == nid)
230 return i;
231 return -1;
232}
233
234/* return true if the given NID is contained in the path */
235static bool is_nid_contained(struct nid_path *path, hda_nid_t nid)
236{
237 return find_idx_in_nid_list(nid, path->path, path->depth) >= 0;
238}
239
Takashi Iwaif5172a72013-01-04 13:19:55 +0100240static struct nid_path *get_nid_path(struct hda_codec *codec,
241 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100242 int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100244 struct hda_gen_spec *spec = codec->spec;
245 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Takashi Iwai352f7f92012-12-19 12:52:06 +0100247 for (i = 0; i < spec->paths.used; i++) {
248 struct nid_path *path = snd_array_elem(&spec->paths, i);
249 if (path->depth <= 0)
250 continue;
251 if ((!from_nid || path->path[0] == from_nid) &&
Takashi Iwaif5172a72013-01-04 13:19:55 +0100252 (!to_nid || path->path[path->depth - 1] == to_nid)) {
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100253 if (!anchor_nid ||
254 (anchor_nid > 0 && is_nid_contained(path, anchor_nid)) ||
255 (anchor_nid < 0 && !is_nid_contained(path, anchor_nid)))
Takashi Iwaif5172a72013-01-04 13:19:55 +0100256 return path;
257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 }
259 return NULL;
260}
Takashi Iwaif5172a72013-01-04 13:19:55 +0100261
262/* get the path between the given NIDs;
263 * passing 0 to either @pin or @dac behaves as a wildcard
264 */
265struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
266 hda_nid_t from_nid, hda_nid_t to_nid)
267{
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100268 return get_nid_path(codec, from_nid, to_nid, 0);
Takashi Iwaif5172a72013-01-04 13:19:55 +0100269}
Takashi Iwai352f7f92012-12-19 12:52:06 +0100270EXPORT_SYMBOL_HDA(snd_hda_get_nid_path);
271
Takashi Iwai196c17662013-01-04 15:01:40 +0100272/* get the index number corresponding to the path instance;
273 * the index starts from 1, for easier checking the invalid value
274 */
275int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
276{
277 struct hda_gen_spec *spec = codec->spec;
278 struct nid_path *array = spec->paths.list;
279 ssize_t idx;
280
281 if (!spec->paths.used)
282 return 0;
283 idx = path - array;
284 if (idx < 0 || idx >= spec->paths.used)
285 return 0;
286 return idx + 1;
287}
Takashi Iwai4bd01e92013-01-22 15:17:20 +0100288EXPORT_SYMBOL_HDA(snd_hda_get_path_idx);
Takashi Iwai196c17662013-01-04 15:01:40 +0100289
290/* get the path instance corresponding to the given index number */
291struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
292{
293 struct hda_gen_spec *spec = codec->spec;
294
295 if (idx <= 0 || idx > spec->paths.used)
296 return NULL;
297 return snd_array_elem(&spec->paths, idx - 1);
298}
Takashi Iwai4bd01e92013-01-22 15:17:20 +0100299EXPORT_SYMBOL_HDA(snd_hda_get_path_from_idx);
Takashi Iwai196c17662013-01-04 15:01:40 +0100300
Takashi Iwai352f7f92012-12-19 12:52:06 +0100301/* check whether the given DAC is already found in any existing paths */
302static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
303{
304 struct hda_gen_spec *spec = codec->spec;
305 int i;
306
307 for (i = 0; i < spec->paths.used; i++) {
308 struct nid_path *path = snd_array_elem(&spec->paths, i);
309 if (path->path[0] == nid)
310 return true;
311 }
312 return false;
313}
314
315/* check whether the given two widgets can be connected */
316static bool is_reachable_path(struct hda_codec *codec,
317 hda_nid_t from_nid, hda_nid_t to_nid)
318{
319 if (!from_nid || !to_nid)
320 return false;
321 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
322}
323
324/* nid, dir and idx */
325#define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
326
327/* check whether the given ctl is already assigned in any path elements */
328static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
329{
330 struct hda_gen_spec *spec = codec->spec;
331 int i;
332
333 val &= AMP_VAL_COMPARE_MASK;
334 for (i = 0; i < spec->paths.used; i++) {
335 struct nid_path *path = snd_array_elem(&spec->paths, i);
336 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
337 return true;
338 }
339 return false;
340}
341
342/* check whether a control with the given (nid, dir, idx) was assigned */
343static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai8999bf02013-01-18 11:01:33 +0100344 int dir, int idx, int type)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100345{
346 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
Takashi Iwai8999bf02013-01-18 11:01:33 +0100347 return is_ctl_used(codec, val, type);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100348}
349
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100350static void print_nid_path(const char *pfx, struct nid_path *path)
351{
352 char buf[40];
353 int i;
354
355
356 buf[0] = 0;
357 for (i = 0; i < path->depth; i++) {
358 char tmp[4];
359 sprintf(tmp, ":%02x", path->path[i]);
360 strlcat(buf, tmp, sizeof(buf));
361 }
362 snd_printdd("%s path: depth=%d %s\n", pfx, path->depth, buf);
363}
364
Takashi Iwai352f7f92012-12-19 12:52:06 +0100365/* called recursively */
366static bool __parse_nid_path(struct hda_codec *codec,
367 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100368 int anchor_nid, struct nid_path *path,
369 int depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100370{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100371 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100372 int i, nums;
373
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100374 if (to_nid == anchor_nid)
375 anchor_nid = 0; /* anchor passed */
376 else if (to_nid == (hda_nid_t)(-anchor_nid))
377 return false; /* hit the exclusive nid */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100378
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100379 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100380 for (i = 0; i < nums; i++) {
381 if (conn[i] != from_nid) {
382 /* special case: when from_nid is 0,
383 * try to find an empty DAC
384 */
385 if (from_nid ||
386 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
387 is_dac_already_used(codec, conn[i]))
388 continue;
389 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100390 /* anchor is not requested or already passed? */
391 if (anchor_nid <= 0)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100392 goto found;
393 }
394 if (depth >= MAX_NID_PATH_DEPTH)
395 return false;
396 for (i = 0; i < nums; i++) {
397 unsigned int type;
398 type = get_wcaps_type(get_wcaps(codec, conn[i]));
399 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
400 type == AC_WID_PIN)
401 continue;
402 if (__parse_nid_path(codec, from_nid, conn[i],
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100403 anchor_nid, path, depth + 1))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100404 goto found;
405 }
406 return false;
407
408 found:
409 path->path[path->depth] = conn[i];
410 path->idx[path->depth + 1] = i;
411 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
412 path->multi[path->depth + 1] = 1;
413 path->depth++;
414 return true;
415}
416
417/* parse the widget path from the given nid to the target nid;
418 * when @from_nid is 0, try to find an empty DAC;
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100419 * when @anchor_nid is set to a positive value, only paths through the widget
420 * with the given value are evaluated.
421 * when @anchor_nid is set to a negative value, paths through the widget
422 * with the negative of given value are excluded, only other paths are chosen.
423 * when @anchor_nid is zero, no special handling about path selection.
Takashi Iwai352f7f92012-12-19 12:52:06 +0100424 */
425bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100426 hda_nid_t to_nid, int anchor_nid,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100427 struct nid_path *path)
428{
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100429 if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100430 path->path[path->depth] = to_nid;
431 path->depth++;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100432 return true;
433 }
434 return false;
435}
436EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100439 * parse the path between the given NIDs and add to the path list.
440 * if no valid path is found, return NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100442struct nid_path *
443snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100444 hda_nid_t to_nid, int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100446 struct hda_gen_spec *spec = codec->spec;
447 struct nid_path *path;
448
449 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
450 return NULL;
451
Takashi Iwaif5172a72013-01-04 13:19:55 +0100452 /* check whether the path has been already added */
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100453 path = get_nid_path(codec, from_nid, to_nid, anchor_nid);
Takashi Iwaif5172a72013-01-04 13:19:55 +0100454 if (path)
455 return path;
456
Takashi Iwai352f7f92012-12-19 12:52:06 +0100457 path = snd_array_new(&spec->paths);
458 if (!path)
459 return NULL;
460 memset(path, 0, sizeof(*path));
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100461 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100462 return path;
463 /* push back */
464 spec->paths.used--;
465 return NULL;
466}
467EXPORT_SYMBOL_HDA(snd_hda_add_new_path);
468
Takashi Iwai980428c2013-01-09 09:28:20 +0100469/* clear the given path as invalid so that it won't be picked up later */
470static void invalidate_nid_path(struct hda_codec *codec, int idx)
471{
472 struct nid_path *path = snd_hda_get_path_from_idx(codec, idx);
473 if (!path)
474 return;
475 memset(path, 0, sizeof(*path));
476}
477
Takashi Iwai352f7f92012-12-19 12:52:06 +0100478/* look for an empty DAC slot */
479static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
480 bool is_digital)
481{
482 struct hda_gen_spec *spec = codec->spec;
483 bool cap_digital;
484 int i;
485
486 for (i = 0; i < spec->num_all_dacs; i++) {
487 hda_nid_t nid = spec->all_dacs[i];
488 if (!nid || is_dac_already_used(codec, nid))
489 continue;
490 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
491 if (is_digital != cap_digital)
492 continue;
493 if (is_reachable_path(codec, nid, pin))
494 return nid;
495 }
496 return 0;
497}
498
499/* replace the channels in the composed amp value with the given number */
500static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
501{
502 val &= ~(0x3U << 16);
503 val |= chs << 16;
504 return val;
505}
506
507/* check whether the widget has the given amp capability for the direction */
508static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
509 int dir, unsigned int bits)
510{
511 if (!nid)
512 return false;
513 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
514 if (query_amp_caps(codec, nid, dir) & bits)
515 return true;
516 return false;
517}
518
David Henningsson99a55922013-01-16 15:58:44 +0100519static bool same_amp_caps(struct hda_codec *codec, hda_nid_t nid1,
520 hda_nid_t nid2, int dir)
521{
522 if (!(get_wcaps(codec, nid1) & (1 << (dir + 1))))
523 return !(get_wcaps(codec, nid2) & (1 << (dir + 1)));
524 return (query_amp_caps(codec, nid1, dir) ==
525 query_amp_caps(codec, nid2, dir));
526}
527
Takashi Iwai352f7f92012-12-19 12:52:06 +0100528#define nid_has_mute(codec, nid, dir) \
Takashi Iwaif69910d2013-08-08 09:32:37 +0200529 check_amp_caps(codec, nid, dir, (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100530#define nid_has_volume(codec, nid, dir) \
531 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
532
533/* look for a widget suitable for assigning a mute switch in the path */
534static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
535 struct nid_path *path)
536{
537 int i;
538
539 for (i = path->depth - 1; i >= 0; i--) {
540 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
541 return path->path[i];
542 if (i != path->depth - 1 && i != 0 &&
543 nid_has_mute(codec, path->path[i], HDA_INPUT))
544 return path->path[i];
545 }
546 return 0;
547}
548
549/* look for a widget suitable for assigning a volume ctl in the path */
550static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
551 struct nid_path *path)
552{
Takashi Iwaia1114a82013-11-04 16:32:01 +0100553 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100554 int i;
555
556 for (i = path->depth - 1; i >= 0; i--) {
Takashi Iwaia1114a82013-11-04 16:32:01 +0100557 hda_nid_t nid = path->path[i];
558 if ((spec->out_vol_mask >> nid) & 1)
559 continue;
560 if (nid_has_volume(codec, nid, HDA_OUTPUT))
561 return nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100562 }
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200563 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
565
566/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100567 * path activation / deactivation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100569
570/* can have the amp-in capability? */
571static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100573 hda_nid_t nid = path->path[idx];
574 unsigned int caps = get_wcaps(codec, nid);
575 unsigned int type = get_wcaps_type(caps);
576
577 if (!(caps & AC_WCAP_IN_AMP))
578 return false;
579 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
580 return false;
581 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582}
583
Takashi Iwai352f7f92012-12-19 12:52:06 +0100584/* can have the amp-out capability? */
585static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100587 hda_nid_t nid = path->path[idx];
588 unsigned int caps = get_wcaps(codec, nid);
589 unsigned int type = get_wcaps_type(caps);
590
591 if (!(caps & AC_WCAP_OUT_AMP))
592 return false;
593 if (type == AC_WID_PIN && !idx) /* only for output pins */
594 return false;
595 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596}
597
Takashi Iwai352f7f92012-12-19 12:52:06 +0100598/* check whether the given (nid,dir,idx) is active */
599static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai7dddf2ae2013-01-24 16:31:35 +0100600 unsigned int dir, unsigned int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100602 struct hda_gen_spec *spec = codec->spec;
603 int i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Takashi Iwai352f7f92012-12-19 12:52:06 +0100605 for (n = 0; n < spec->paths.used; n++) {
606 struct nid_path *path = snd_array_elem(&spec->paths, n);
607 if (!path->active)
608 continue;
609 for (i = 0; i < path->depth; i++) {
610 if (path->path[i] == nid) {
611 if (dir == HDA_OUTPUT || path->idx[i] == idx)
612 return true;
613 break;
614 }
615 }
616 }
617 return false;
618}
619
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +0200620/* check whether the NID is referred by any active paths */
621#define is_active_nid_for_any(codec, nid) \
622 is_active_nid(codec, nid, HDA_OUTPUT, 0)
623
Takashi Iwai352f7f92012-12-19 12:52:06 +0100624/* get the default amp value for the target state */
625static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai8999bf02013-01-18 11:01:33 +0100626 int dir, unsigned int caps, bool enable)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100627{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100628 unsigned int val = 0;
629
Takashi Iwai352f7f92012-12-19 12:52:06 +0100630 if (caps & AC_AMPCAP_NUM_STEPS) {
631 /* set to 0dB */
632 if (enable)
633 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
634 }
Takashi Iwaif69910d2013-08-08 09:32:37 +0200635 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100636 if (!enable)
637 val |= HDA_AMP_MUTE;
638 }
639 return val;
640}
641
642/* initialize the amp value (only at the first time) */
643static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
644{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100645 unsigned int caps = query_amp_caps(codec, nid, dir);
646 int val = get_amp_val_to_activate(codec, nid, dir, caps, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100647 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
648}
649
Takashi Iwai8999bf02013-01-18 11:01:33 +0100650/* calculate amp value mask we can modify;
651 * if the given amp is controlled by mixers, don't touch it
652 */
653static unsigned int get_amp_mask_to_modify(struct hda_codec *codec,
654 hda_nid_t nid, int dir, int idx,
655 unsigned int caps)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100656{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100657 unsigned int mask = 0xff;
658
Takashi Iwaif69910d2013-08-08 09:32:37 +0200659 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
Takashi Iwai8999bf02013-01-18 11:01:33 +0100660 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_MUTE_CTL))
661 mask &= ~0x80;
662 }
663 if (caps & AC_AMPCAP_NUM_STEPS) {
664 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
665 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
666 mask &= ~0x7f;
667 }
668 return mask;
669}
670
671static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
672 int idx, int idx_to_check, bool enable)
673{
674 unsigned int caps;
675 unsigned int mask, val;
676
Takashi Iwai7dddf2ae2013-01-24 16:31:35 +0100677 if (!enable && is_active_nid(codec, nid, dir, idx_to_check))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100678 return;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100679
680 caps = query_amp_caps(codec, nid, dir);
681 val = get_amp_val_to_activate(codec, nid, dir, caps, enable);
682 mask = get_amp_mask_to_modify(codec, nid, dir, idx_to_check, caps);
683 if (!mask)
684 return;
685
686 val &= mask;
687 snd_hda_codec_amp_stereo(codec, nid, dir, idx, mask, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100688}
689
690static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
691 int i, bool enable)
692{
693 hda_nid_t nid = path->path[i];
694 init_amp(codec, nid, HDA_OUTPUT, 0);
Takashi Iwai8999bf02013-01-18 11:01:33 +0100695 activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100696}
697
698static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
699 int i, bool enable, bool add_aamix)
700{
701 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100702 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100703 int n, nums, idx;
704 int type;
705 hda_nid_t nid = path->path[i];
706
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100707 nums = snd_hda_get_conn_list(codec, nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100708 type = get_wcaps_type(get_wcaps(codec, nid));
709 if (type == AC_WID_PIN ||
710 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
711 nums = 1;
712 idx = 0;
713 } else
714 idx = path->idx[i];
715
716 for (n = 0; n < nums; n++)
717 init_amp(codec, nid, HDA_INPUT, n);
718
Takashi Iwai352f7f92012-12-19 12:52:06 +0100719 /* here is a little bit tricky in comparison with activate_amp_out();
720 * when aa-mixer is available, we need to enable the path as well
721 */
722 for (n = 0; n < nums; n++) {
Takashi Iwaie4a395e2013-01-23 17:00:31 +0100723 if (n != idx && (!add_aamix || conn[n] != spec->mixer_merge_nid))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100724 continue;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100725 activate_amp(codec, nid, HDA_INPUT, n, idx, enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 }
727}
728
Takashi Iwai352f7f92012-12-19 12:52:06 +0100729/* activate or deactivate the given path
730 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100732void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
733 bool enable, bool add_aamix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
Takashi Iwai55196ff2013-01-24 17:32:56 +0100735 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100736 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Takashi Iwai352f7f92012-12-19 12:52:06 +0100738 if (!enable)
739 path->active = false;
740
741 for (i = path->depth - 1; i >= 0; i--) {
Takashi Iwai55196ff2013-01-24 17:32:56 +0100742 hda_nid_t nid = path->path[i];
743 if (enable && spec->power_down_unused) {
744 /* make sure the widget is powered up */
745 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0))
746 snd_hda_codec_write(codec, nid, 0,
747 AC_VERB_SET_POWER_STATE,
748 AC_PWRST_D0);
749 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100750 if (enable && path->multi[i])
Takashi Iwai55196ff2013-01-24 17:32:56 +0100751 snd_hda_codec_write_cache(codec, nid, 0,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100752 AC_VERB_SET_CONNECT_SEL,
753 path->idx[i]);
754 if (has_amp_in(codec, path, i))
755 activate_amp_in(codec, path, i, enable, add_aamix);
756 if (has_amp_out(codec, path, i))
757 activate_amp_out(codec, path, i, enable);
758 }
759
760 if (enable)
761 path->active = true;
762}
763EXPORT_SYMBOL_HDA(snd_hda_activate_path);
764
Takashi Iwai55196ff2013-01-24 17:32:56 +0100765/* if the given path is inactive, put widgets into D3 (only if suitable) */
766static void path_power_down_sync(struct hda_codec *codec, struct nid_path *path)
767{
768 struct hda_gen_spec *spec = codec->spec;
Jiri Slaby868211d2013-04-04 22:32:10 +0200769 bool changed = false;
Takashi Iwai55196ff2013-01-24 17:32:56 +0100770 int i;
771
772 if (!spec->power_down_unused || path->active)
773 return;
774
775 for (i = 0; i < path->depth; i++) {
776 hda_nid_t nid = path->path[i];
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +0200777 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D3) &&
778 !is_active_nid_for_any(codec, nid)) {
Takashi Iwai55196ff2013-01-24 17:32:56 +0100779 snd_hda_codec_write(codec, nid, 0,
780 AC_VERB_SET_POWER_STATE,
781 AC_PWRST_D3);
782 changed = true;
783 }
784 }
785
786 if (changed) {
787 msleep(10);
788 snd_hda_codec_read(codec, path->path[0], 0,
789 AC_VERB_GET_POWER_STATE, 0);
790 }
791}
792
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100793/* turn on/off EAPD on the given pin */
794static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
795{
796 struct hda_gen_spec *spec = codec->spec;
797 if (spec->own_eapd_ctl ||
798 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
799 return;
Takashi Iwai05909d5c2013-05-31 19:55:54 +0200800 if (spec->keep_eapd_on && !enable)
801 return;
Takashi Iwai468ac412013-11-12 11:36:00 +0100802 if (codec->inv_eapd)
803 enable = !enable;
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100804 snd_hda_codec_update_cache(codec, pin, 0,
805 AC_VERB_SET_EAPD_BTLENABLE,
806 enable ? 0x02 : 0x00);
807}
808
Takashi Iwai3e367f12013-01-23 17:07:23 +0100809/* re-initialize the path specified by the given path index */
810static void resume_path_from_idx(struct hda_codec *codec, int path_idx)
811{
812 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
813 if (path)
814 snd_hda_activate_path(codec, path, path->active, false);
815}
816
Takashi Iwai352f7f92012-12-19 12:52:06 +0100817
818/*
819 * Helper functions for creating mixer ctl elements
820 */
821
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200822static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
823 struct snd_ctl_elem_value *ucontrol);
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200824static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
825 struct snd_ctl_elem_value *ucontrol);
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200826
Takashi Iwai352f7f92012-12-19 12:52:06 +0100827enum {
828 HDA_CTL_WIDGET_VOL,
829 HDA_CTL_WIDGET_MUTE,
830 HDA_CTL_BIND_MUTE,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100831};
832static const struct snd_kcontrol_new control_templates[] = {
833 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200834 /* only the put callback is replaced for handling the special mute */
835 {
836 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
837 .subdevice = HDA_SUBDEV_AMP_FLAG,
838 .info = snd_hda_mixer_amp_switch_info,
839 .get = snd_hda_mixer_amp_switch_get,
840 .put = hda_gen_mixer_mute_put, /* replaced */
841 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
842 },
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200843 {
844 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
845 .info = snd_hda_mixer_amp_switch_info,
846 .get = snd_hda_mixer_bind_switch_get,
847 .put = hda_gen_bind_mute_put, /* replaced */
848 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
849 },
Takashi Iwai352f7f92012-12-19 12:52:06 +0100850};
851
852/* add dynamic controls from template */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100853static struct snd_kcontrol_new *
854add_control(struct hda_gen_spec *spec, int type, const char *name,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100855 int cidx, unsigned long val)
856{
857 struct snd_kcontrol_new *knew;
858
Takashi Iwai12c93df2012-12-19 14:38:33 +0100859 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100860 if (!knew)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100861 return NULL;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100862 knew->index = cidx;
863 if (get_amp_nid_(val))
864 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
865 knew->private_value = val;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100866 return knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100867}
868
869static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
870 const char *pfx, const char *dir,
871 const char *sfx, int cidx, unsigned long val)
872{
Takashi Iwai975cc022013-06-28 11:56:49 +0200873 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai352f7f92012-12-19 12:52:06 +0100874 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100875 if (!add_control(spec, type, name, cidx, val))
876 return -ENOMEM;
877 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100878}
879
880#define add_pb_vol_ctrl(spec, type, pfx, val) \
881 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
882#define add_pb_sw_ctrl(spec, type, pfx, val) \
883 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
884#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
885 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
886#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
887 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
888
889static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
890 unsigned int chs, struct nid_path *path)
891{
892 unsigned int val;
893 if (!path)
894 return 0;
895 val = path->ctls[NID_PATH_VOL_CTL];
896 if (!val)
897 return 0;
898 val = amp_val_replace_channels(val, chs);
899 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
900}
901
902/* return the channel bits suitable for the given path->ctls[] */
903static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
904 int type)
905{
906 int chs = 1; /* mono (left only) */
907 if (path) {
908 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
909 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
910 chs = 3; /* stereo */
911 }
912 return chs;
913}
914
915static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
916 struct nid_path *path)
917{
918 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
919 return add_vol_ctl(codec, pfx, cidx, chs, path);
920}
921
922/* create a mute-switch for the given mixer widget;
923 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
924 */
925static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
926 unsigned int chs, struct nid_path *path)
927{
928 unsigned int val;
929 int type = HDA_CTL_WIDGET_MUTE;
930
931 if (!path)
932 return 0;
933 val = path->ctls[NID_PATH_MUTE_CTL];
934 if (!val)
935 return 0;
936 val = amp_val_replace_channels(val, chs);
937 if (get_amp_direction_(val) == HDA_INPUT) {
938 hda_nid_t nid = get_amp_nid_(val);
939 int nums = snd_hda_get_num_conns(codec, nid);
940 if (nums > 1) {
941 type = HDA_CTL_BIND_MUTE;
942 val |= nums << 19;
943 }
944 }
945 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
946}
947
948static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
949 int cidx, struct nid_path *path)
950{
951 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
952 return add_sw_ctl(codec, pfx, cidx, chs, path);
953}
954
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200955/* playback mute control with the software mute bit check */
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200956static void sync_auto_mute_bits(struct snd_kcontrol *kcontrol,
957 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200958{
959 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
960 struct hda_gen_spec *spec = codec->spec;
961
962 if (spec->auto_mute_via_amp) {
963 hda_nid_t nid = get_amp_nid(kcontrol);
964 bool enabled = !((spec->mute_bits >> nid) & 1);
965 ucontrol->value.integer.value[0] &= enabled;
966 ucontrol->value.integer.value[1] &= enabled;
967 }
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200968}
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200969
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200970static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
971 struct snd_ctl_elem_value *ucontrol)
972{
973 sync_auto_mute_bits(kcontrol, ucontrol);
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200974 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
975}
976
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200977static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
978 struct snd_ctl_elem_value *ucontrol)
979{
980 sync_auto_mute_bits(kcontrol, ucontrol);
981 return snd_hda_mixer_bind_switch_put(kcontrol, ucontrol);
982}
983
Takashi Iwai247d85e2013-01-17 16:18:11 +0100984/* any ctl assigned to the path with the given index? */
985static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
986{
987 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
988 return path && path->ctls[ctl_type];
989}
990
Takashi Iwai352f7f92012-12-19 12:52:06 +0100991static const char * const channel_name[4] = {
992 "Front", "Surround", "CLFE", "Side"
993};
994
995/* give some appropriate ctl name prefix for the given line out channel */
Takashi Iwai247d85e2013-01-17 16:18:11 +0100996static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
997 int *index, int ctl_type)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100998{
Takashi Iwai247d85e2013-01-17 16:18:11 +0100999 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001000 struct auto_pin_cfg *cfg = &spec->autocfg;
1001
1002 *index = 0;
1003 if (cfg->line_outs == 1 && !spec->multi_ios &&
Takashi Iwai247d85e2013-01-17 16:18:11 +01001004 !cfg->hp_outs && !cfg->speaker_outs)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001005 return spec->vmaster_mute.hook ? "PCM" : "Master";
1006
1007 /* if there is really a single DAC used in the whole output paths,
1008 * use it master (or "PCM" if a vmaster hook is present)
1009 */
1010 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
1011 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
1012 return spec->vmaster_mute.hook ? "PCM" : "Master";
1013
Takashi Iwai247d85e2013-01-17 16:18:11 +01001014 /* multi-io channels */
1015 if (ch >= cfg->line_outs)
1016 return channel_name[ch];
1017
Takashi Iwai352f7f92012-12-19 12:52:06 +01001018 switch (cfg->line_out_type) {
1019 case AUTO_PIN_SPEAKER_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +01001020 /* if the primary channel vol/mute is shared with HP volume,
1021 * don't name it as Speaker
1022 */
1023 if (!ch && cfg->hp_outs &&
1024 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
1025 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001026 if (cfg->line_outs == 1)
1027 return "Speaker";
1028 if (cfg->line_outs == 2)
1029 return ch ? "Bass Speaker" : "Speaker";
1030 break;
1031 case AUTO_PIN_HP_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +01001032 /* if the primary channel vol/mute is shared with spk volume,
1033 * don't name it as Headphone
1034 */
1035 if (!ch && cfg->speaker_outs &&
1036 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
1037 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001038 /* for multi-io case, only the primary out */
1039 if (ch && spec->multi_ios)
1040 break;
1041 *index = ch;
1042 return "Headphone";
Takashi Iwai352f7f92012-12-19 12:52:06 +01001043 }
Takashi Iwai247d85e2013-01-17 16:18:11 +01001044
1045 /* for a single channel output, we don't have to name the channel */
1046 if (cfg->line_outs == 1 && !spec->multi_ios)
1047 return "PCM";
1048
Takashi Iwai352f7f92012-12-19 12:52:06 +01001049 if (ch >= ARRAY_SIZE(channel_name)) {
1050 snd_BUG();
1051 return "PCM";
1052 }
1053
1054 return channel_name[ch];
1055}
1056
1057/*
1058 * Parse output paths
1059 */
1060
1061/* badness definition */
1062enum {
1063 /* No primary DAC is found for the main output */
1064 BAD_NO_PRIMARY_DAC = 0x10000,
1065 /* No DAC is found for the extra output */
1066 BAD_NO_DAC = 0x4000,
1067 /* No possible multi-ios */
Takashi Iwai1d739062013-02-13 14:17:32 +01001068 BAD_MULTI_IO = 0x120,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001069 /* No individual DAC for extra output */
1070 BAD_NO_EXTRA_DAC = 0x102,
1071 /* No individual DAC for extra surrounds */
1072 BAD_NO_EXTRA_SURR_DAC = 0x101,
1073 /* Primary DAC shared with main surrounds */
1074 BAD_SHARED_SURROUND = 0x100,
Takashi Iwai55a63d42013-03-21 17:20:12 +01001075 /* No independent HP possible */
Takashi Iwaibec8e682013-03-22 15:10:08 +01001076 BAD_NO_INDEP_HP = 0x10,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001077 /* Primary DAC shared with main CLFE */
1078 BAD_SHARED_CLFE = 0x10,
1079 /* Primary DAC shared with extra surrounds */
1080 BAD_SHARED_EXTRA_SURROUND = 0x10,
1081 /* Volume widget is shared */
1082 BAD_SHARED_VOL = 0x10,
1083};
1084
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001085/* look for widgets in the given path which are appropriate for
Takashi Iwai352f7f92012-12-19 12:52:06 +01001086 * volume and mute controls, and assign the values to ctls[].
1087 *
1088 * When no appropriate widget is found in the path, the badness value
1089 * is incremented depending on the situation. The function returns the
1090 * total badness for both volume and mute controls.
1091 */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001092static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001093{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001094 hda_nid_t nid;
1095 unsigned int val;
1096 int badness = 0;
1097
1098 if (!path)
1099 return BAD_SHARED_VOL * 2;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001100
1101 if (path->ctls[NID_PATH_VOL_CTL] ||
1102 path->ctls[NID_PATH_MUTE_CTL])
1103 return 0; /* already evaluated */
1104
Takashi Iwai352f7f92012-12-19 12:52:06 +01001105 nid = look_for_out_vol_nid(codec, path);
1106 if (nid) {
1107 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1108 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1109 badness += BAD_SHARED_VOL;
1110 else
1111 path->ctls[NID_PATH_VOL_CTL] = val;
1112 } else
1113 badness += BAD_SHARED_VOL;
1114 nid = look_for_out_mute_nid(codec, path);
1115 if (nid) {
1116 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1117 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1118 nid_has_mute(codec, nid, HDA_OUTPUT))
1119 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1120 else
1121 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1122 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1123 badness += BAD_SHARED_VOL;
1124 else
1125 path->ctls[NID_PATH_MUTE_CTL] = val;
1126 } else
1127 badness += BAD_SHARED_VOL;
1128 return badness;
1129}
1130
Takashi Iwai98bd1112013-03-22 14:53:50 +01001131const struct badness_table hda_main_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001132 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1133 .no_dac = BAD_NO_DAC,
1134 .shared_primary = BAD_NO_PRIMARY_DAC,
1135 .shared_surr = BAD_SHARED_SURROUND,
1136 .shared_clfe = BAD_SHARED_CLFE,
1137 .shared_surr_main = BAD_SHARED_SURROUND,
1138};
Takashi Iwai98bd1112013-03-22 14:53:50 +01001139EXPORT_SYMBOL_HDA(hda_main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001140
Takashi Iwai98bd1112013-03-22 14:53:50 +01001141const struct badness_table hda_extra_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001142 .no_primary_dac = BAD_NO_DAC,
1143 .no_dac = BAD_NO_DAC,
1144 .shared_primary = BAD_NO_EXTRA_DAC,
1145 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1146 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1147 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1148};
Takashi Iwai98bd1112013-03-22 14:53:50 +01001149EXPORT_SYMBOL_HDA(hda_extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001150
Takashi Iwai7385df62013-01-07 09:50:52 +01001151/* get the DAC of the primary output corresponding to the given array index */
1152static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1153{
1154 struct hda_gen_spec *spec = codec->spec;
1155 struct auto_pin_cfg *cfg = &spec->autocfg;
1156
1157 if (cfg->line_outs > idx)
1158 return spec->private_dac_nids[idx];
1159 idx -= cfg->line_outs;
1160 if (spec->multi_ios > idx)
1161 return spec->multi_io[idx].dac;
1162 return 0;
1163}
1164
1165/* return the DAC if it's reachable, otherwise zero */
1166static inline hda_nid_t try_dac(struct hda_codec *codec,
1167 hda_nid_t dac, hda_nid_t pin)
1168{
1169 return is_reachable_path(codec, dac, pin) ? dac : 0;
1170}
1171
Takashi Iwai352f7f92012-12-19 12:52:06 +01001172/* try to assign DACs to pins and return the resultant badness */
1173static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1174 const hda_nid_t *pins, hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001175 int *path_idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001176 const struct badness_table *bad)
1177{
1178 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001179 int i, j;
1180 int badness = 0;
1181 hda_nid_t dac;
1182
1183 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 return 0;
1185
Takashi Iwai352f7f92012-12-19 12:52:06 +01001186 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001187 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001188 hda_nid_t pin = pins[i];
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001189
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001190 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1191 if (path) {
1192 badness += assign_out_path_ctls(codec, path);
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001193 continue;
1194 }
1195
1196 dacs[i] = look_for_dac(codec, pin, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001197 if (!dacs[i] && !i) {
Takashi Iwai980428c2013-01-09 09:28:20 +01001198 /* try to steal the DAC of surrounds for the front */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001199 for (j = 1; j < num_outs; j++) {
1200 if (is_reachable_path(codec, dacs[j], pin)) {
1201 dacs[0] = dacs[j];
1202 dacs[j] = 0;
Takashi Iwai980428c2013-01-09 09:28:20 +01001203 invalidate_nid_path(codec, path_idx[j]);
Takashi Iwai196c17662013-01-04 15:01:40 +01001204 path_idx[j] = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001205 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 }
1207 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001208 }
1209 dac = dacs[i];
1210 if (!dac) {
Takashi Iwai7385df62013-01-07 09:50:52 +01001211 if (num_outs > 2)
1212 dac = try_dac(codec, get_primary_out(codec, i), pin);
1213 if (!dac)
1214 dac = try_dac(codec, dacs[0], pin);
1215 if (!dac)
1216 dac = try_dac(codec, get_primary_out(codec, i), pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001217 if (dac) {
1218 if (!i)
1219 badness += bad->shared_primary;
1220 else if (i == 1)
1221 badness += bad->shared_surr;
1222 else
1223 badness += bad->shared_clfe;
1224 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1225 dac = spec->private_dac_nids[0];
1226 badness += bad->shared_surr_main;
1227 } else if (!i)
1228 badness += bad->no_primary_dac;
1229 else
1230 badness += bad->no_dac;
1231 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001232 if (!dac)
1233 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001234 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001235 if (!path && !i && spec->mixer_nid) {
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001236 /* try with aamix */
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001237 path = snd_hda_add_new_path(codec, dac, pin, 0);
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001238 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001239 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001240 dac = dacs[i] = 0;
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001241 badness += bad->no_dac;
1242 } else {
Takashi Iwaia7694092013-01-21 10:43:18 +01001243 /* print_nid_path("output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001244 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001245 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001246 badness += assign_out_path_ctls(codec, path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001247 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001248 }
1249
1250 return badness;
1251}
1252
1253/* return NID if the given pin has only a single connection to a certain DAC */
1254static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1255{
1256 struct hda_gen_spec *spec = codec->spec;
1257 int i;
1258 hda_nid_t nid_found = 0;
1259
1260 for (i = 0; i < spec->num_all_dacs; i++) {
1261 hda_nid_t nid = spec->all_dacs[i];
1262 if (!nid || is_dac_already_used(codec, nid))
1263 continue;
1264 if (is_reachable_path(codec, nid, pin)) {
1265 if (nid_found)
1266 return 0;
1267 nid_found = nid;
1268 }
1269 }
1270 return nid_found;
1271}
1272
1273/* check whether the given pin can be a multi-io pin */
1274static bool can_be_multiio_pin(struct hda_codec *codec,
1275 unsigned int location, hda_nid_t nid)
1276{
1277 unsigned int defcfg, caps;
1278
1279 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1280 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1281 return false;
1282 if (location && get_defcfg_location(defcfg) != location)
1283 return false;
1284 caps = snd_hda_query_pin_caps(codec, nid);
1285 if (!(caps & AC_PINCAP_OUT))
1286 return false;
1287 return true;
1288}
1289
Takashi Iwaie22aab72013-01-04 14:50:04 +01001290/* count the number of input pins that are capable to be multi-io */
1291static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1292{
1293 struct hda_gen_spec *spec = codec->spec;
1294 struct auto_pin_cfg *cfg = &spec->autocfg;
1295 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1296 unsigned int location = get_defcfg_location(defcfg);
1297 int type, i;
1298 int num_pins = 0;
1299
1300 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1301 for (i = 0; i < cfg->num_inputs; i++) {
1302 if (cfg->inputs[i].type != type)
1303 continue;
1304 if (can_be_multiio_pin(codec, location,
1305 cfg->inputs[i].pin))
1306 num_pins++;
1307 }
1308 }
1309 return num_pins;
1310}
1311
Takashi Iwai352f7f92012-12-19 12:52:06 +01001312/*
1313 * multi-io helper
1314 *
1315 * When hardwired is set, try to fill ony hardwired pins, and returns
1316 * zero if any pins are filled, non-zero if nothing found.
1317 * When hardwired is off, try to fill possible input pins, and returns
1318 * the badness value.
1319 */
1320static int fill_multi_ios(struct hda_codec *codec,
1321 hda_nid_t reference_pin,
Takashi Iwaie22aab72013-01-04 14:50:04 +01001322 bool hardwired)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001323{
1324 struct hda_gen_spec *spec = codec->spec;
1325 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie22aab72013-01-04 14:50:04 +01001326 int type, i, j, num_pins, old_pins;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001327 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1328 unsigned int location = get_defcfg_location(defcfg);
1329 int badness = 0;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001330 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001331
1332 old_pins = spec->multi_ios;
1333 if (old_pins >= 2)
1334 goto end_fill;
1335
Takashi Iwaie22aab72013-01-04 14:50:04 +01001336 num_pins = count_multiio_pins(codec, reference_pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001337 if (num_pins < 2)
1338 goto end_fill;
1339
Takashi Iwai352f7f92012-12-19 12:52:06 +01001340 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1341 for (i = 0; i < cfg->num_inputs; i++) {
1342 hda_nid_t nid = cfg->inputs[i].pin;
1343 hda_nid_t dac = 0;
1344
1345 if (cfg->inputs[i].type != type)
1346 continue;
1347 if (!can_be_multiio_pin(codec, location, nid))
1348 continue;
1349 for (j = 0; j < spec->multi_ios; j++) {
1350 if (nid == spec->multi_io[j].pin)
1351 break;
1352 }
1353 if (j < spec->multi_ios)
1354 continue;
1355
Takashi Iwai352f7f92012-12-19 12:52:06 +01001356 if (hardwired)
1357 dac = get_dac_if_single(codec, nid);
1358 else if (!dac)
1359 dac = look_for_dac(codec, nid, false);
1360 if (!dac) {
1361 badness++;
1362 continue;
1363 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001364 path = snd_hda_add_new_path(codec, dac, nid,
1365 -spec->mixer_nid);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001366 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001367 badness++;
1368 continue;
1369 }
Takashi Iwaia7694092013-01-21 10:43:18 +01001370 /* print_nid_path("multiio", path); */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001371 spec->multi_io[spec->multi_ios].pin = nid;
1372 spec->multi_io[spec->multi_ios].dac = dac;
Takashi Iwai196c17662013-01-04 15:01:40 +01001373 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1374 snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001375 spec->multi_ios++;
1376 if (spec->multi_ios >= 2)
1377 break;
1378 }
1379 }
1380 end_fill:
1381 if (badness)
1382 badness = BAD_MULTI_IO;
1383 if (old_pins == spec->multi_ios) {
1384 if (hardwired)
1385 return 1; /* nothing found */
1386 else
1387 return badness; /* no badness if nothing found */
1388 }
1389 if (!hardwired && spec->multi_ios < 2) {
1390 /* cancel newly assigned paths */
1391 spec->paths.used -= spec->multi_ios - old_pins;
1392 spec->multi_ios = old_pins;
1393 return badness;
1394 }
1395
1396 /* assign volume and mute controls */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001397 for (i = old_pins; i < spec->multi_ios; i++) {
1398 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1399 badness += assign_out_path_ctls(codec, path);
1400 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001401
1402 return badness;
1403}
1404
1405/* map DACs for all pins in the list if they are single connections */
1406static bool map_singles(struct hda_codec *codec, int outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001407 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001408{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001409 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001410 int i;
1411 bool found = false;
1412 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001413 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001414 hda_nid_t dac;
1415 if (dacs[i])
1416 continue;
1417 dac = get_dac_if_single(codec, pins[i]);
1418 if (!dac)
1419 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001420 path = snd_hda_add_new_path(codec, dac, pins[i],
1421 -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001422 if (!path && !i && spec->mixer_nid)
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001423 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001424 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001425 dacs[i] = dac;
1426 found = true;
Takashi Iwaia7694092013-01-21 10:43:18 +01001427 /* print_nid_path("output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001428 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001429 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001430 }
1431 }
1432 return found;
1433}
1434
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001435/* create a new path including aamix if available, and return its index */
1436static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1437{
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001438 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001439 struct nid_path *path;
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001440 hda_nid_t path_dac, dac, pin;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001441
1442 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001443 if (!path || !path->depth ||
1444 is_nid_contained(path, spec->mixer_nid))
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001445 return 0;
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001446 path_dac = path->path[0];
1447 dac = spec->private_dac_nids[0];
Takashi Iwaif87498b2013-01-21 14:24:31 +01001448 pin = path->path[path->depth - 1];
1449 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1450 if (!path) {
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001451 if (dac != path_dac)
1452 dac = path_dac;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001453 else if (spec->multiout.hp_out_nid[0])
1454 dac = spec->multiout.hp_out_nid[0];
1455 else if (spec->multiout.extra_out_nid[0])
1456 dac = spec->multiout.extra_out_nid[0];
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001457 else
1458 dac = 0;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001459 if (dac)
1460 path = snd_hda_add_new_path(codec, dac, pin,
1461 spec->mixer_nid);
1462 }
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001463 if (!path)
1464 return 0;
Takashi Iwaia7694092013-01-21 10:43:18 +01001465 /* print_nid_path("output-aamix", path); */
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001466 path->active = false; /* unused as default */
1467 return snd_hda_get_path_idx(codec, path);
1468}
1469
Takashi Iwai55a63d42013-03-21 17:20:12 +01001470/* check whether the independent HP is available with the current config */
1471static bool indep_hp_possible(struct hda_codec *codec)
1472{
1473 struct hda_gen_spec *spec = codec->spec;
1474 struct auto_pin_cfg *cfg = &spec->autocfg;
1475 struct nid_path *path;
1476 int i, idx;
1477
1478 if (cfg->line_out_type == AUTO_PIN_HP_OUT)
1479 idx = spec->out_paths[0];
1480 else
1481 idx = spec->hp_paths[0];
1482 path = snd_hda_get_path_from_idx(codec, idx);
1483 if (!path)
1484 return false;
1485
1486 /* assume no path conflicts unless aamix is involved */
1487 if (!spec->mixer_nid || !is_nid_contained(path, spec->mixer_nid))
1488 return true;
1489
1490 /* check whether output paths contain aamix */
1491 for (i = 0; i < cfg->line_outs; i++) {
1492 if (spec->out_paths[i] == idx)
1493 break;
1494 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1495 if (path && is_nid_contained(path, spec->mixer_nid))
1496 return false;
1497 }
1498 for (i = 0; i < cfg->speaker_outs; i++) {
1499 path = snd_hda_get_path_from_idx(codec, spec->speaker_paths[i]);
1500 if (path && is_nid_contained(path, spec->mixer_nid))
1501 return false;
1502 }
1503
1504 return true;
1505}
1506
Takashi Iwaia07a9492013-01-07 16:44:06 +01001507/* fill the empty entries in the dac array for speaker/hp with the
1508 * shared dac pointed by the paths
1509 */
1510static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1511 hda_nid_t *dacs, int *path_idx)
1512{
1513 struct nid_path *path;
1514 int i;
1515
1516 for (i = 0; i < num_outs; i++) {
1517 if (dacs[i])
1518 continue;
1519 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1520 if (!path)
1521 continue;
1522 dacs[i] = path->path[0];
1523 }
1524}
1525
Takashi Iwai352f7f92012-12-19 12:52:06 +01001526/* fill in the dac_nids table from the parsed pin configuration */
1527static int fill_and_eval_dacs(struct hda_codec *codec,
1528 bool fill_hardwired,
1529 bool fill_mio_first)
1530{
1531 struct hda_gen_spec *spec = codec->spec;
1532 struct auto_pin_cfg *cfg = &spec->autocfg;
1533 int i, err, badness;
1534
1535 /* set num_dacs once to full for look_for_dac() */
1536 spec->multiout.num_dacs = cfg->line_outs;
1537 spec->multiout.dac_nids = spec->private_dac_nids;
1538 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1539 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1540 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1541 spec->multi_ios = 0;
1542 snd_array_free(&spec->paths);
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001543
1544 /* clear path indices */
1545 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1546 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1547 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1548 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1549 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
Takashi Iwaic697b712013-01-07 17:09:26 +01001550 memset(spec->input_paths, 0, sizeof(spec->input_paths));
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001551 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1552 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1553
Takashi Iwai352f7f92012-12-19 12:52:06 +01001554 badness = 0;
1555
1556 /* fill hard-wired DACs first */
1557 if (fill_hardwired) {
1558 bool mapped;
1559 do {
1560 mapped = map_singles(codec, cfg->line_outs,
1561 cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001562 spec->private_dac_nids,
1563 spec->out_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001564 mapped |= map_singles(codec, cfg->hp_outs,
1565 cfg->hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001566 spec->multiout.hp_out_nid,
1567 spec->hp_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001568 mapped |= map_singles(codec, cfg->speaker_outs,
1569 cfg->speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001570 spec->multiout.extra_out_nid,
1571 spec->speaker_paths);
Takashi Iwaida96fb52013-07-29 16:54:36 +02001572 if (!spec->no_multi_io &&
1573 fill_mio_first && cfg->line_outs == 1 &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01001574 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001575 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001576 if (!err)
1577 mapped = true;
1578 }
1579 } while (mapped);
1580 }
1581
1582 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001583 spec->private_dac_nids, spec->out_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001584 spec->main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001585
Takashi Iwaida96fb52013-07-29 16:54:36 +02001586 if (!spec->no_multi_io && fill_mio_first &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01001587 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1588 /* try to fill multi-io first */
Takashi Iwaie22aab72013-01-04 14:50:04 +01001589 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001590 if (err < 0)
1591 return err;
1592 /* we don't count badness at this stage yet */
1593 }
1594
1595 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1596 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1597 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001598 spec->hp_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001599 spec->extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001600 if (err < 0)
1601 return err;
1602 badness += err;
1603 }
1604 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1605 err = try_assign_dacs(codec, cfg->speaker_outs,
1606 cfg->speaker_pins,
1607 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001608 spec->speaker_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001609 spec->extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001610 if (err < 0)
1611 return err;
1612 badness += err;
1613 }
Takashi Iwaida96fb52013-07-29 16:54:36 +02001614 if (!spec->no_multi_io &&
1615 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001616 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001617 if (err < 0)
1618 return err;
1619 badness += err;
1620 }
Takashi Iwaie22aab72013-01-04 14:50:04 +01001621
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001622 if (spec->mixer_nid) {
1623 spec->aamix_out_paths[0] =
1624 check_aamix_out_path(codec, spec->out_paths[0]);
1625 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1626 spec->aamix_out_paths[1] =
1627 check_aamix_out_path(codec, spec->hp_paths[0]);
1628 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1629 spec->aamix_out_paths[2] =
1630 check_aamix_out_path(codec, spec->speaker_paths[0]);
1631 }
1632
Takashi Iwaida96fb52013-07-29 16:54:36 +02001633 if (!spec->no_multi_io &&
1634 cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
Takashi Iwaie22aab72013-01-04 14:50:04 +01001635 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1636 spec->multi_ios = 1; /* give badness */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001637
Takashi Iwaia07a9492013-01-07 16:44:06 +01001638 /* re-count num_dacs and squash invalid entries */
1639 spec->multiout.num_dacs = 0;
1640 for (i = 0; i < cfg->line_outs; i++) {
1641 if (spec->private_dac_nids[i])
1642 spec->multiout.num_dacs++;
1643 else {
1644 memmove(spec->private_dac_nids + i,
1645 spec->private_dac_nids + i + 1,
1646 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1647 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1648 }
1649 }
1650
1651 spec->ext_channel_count = spec->min_channel_count =
David Henningssonc0f3b212013-01-16 11:45:37 +01001652 spec->multiout.num_dacs * 2;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001653
Takashi Iwai352f7f92012-12-19 12:52:06 +01001654 if (spec->multi_ios == 2) {
1655 for (i = 0; i < 2; i++)
1656 spec->private_dac_nids[spec->multiout.num_dacs++] =
1657 spec->multi_io[i].dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001658 } else if (spec->multi_ios) {
1659 spec->multi_ios = 0;
1660 badness += BAD_MULTI_IO;
1661 }
1662
Takashi Iwai55a63d42013-03-21 17:20:12 +01001663 if (spec->indep_hp && !indep_hp_possible(codec))
1664 badness += BAD_NO_INDEP_HP;
1665
Takashi Iwaia07a9492013-01-07 16:44:06 +01001666 /* re-fill the shared DAC for speaker / headphone */
1667 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1668 refill_shared_dacs(codec, cfg->hp_outs,
1669 spec->multiout.hp_out_nid,
1670 spec->hp_paths);
1671 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1672 refill_shared_dacs(codec, cfg->speaker_outs,
1673 spec->multiout.extra_out_nid,
1674 spec->speaker_paths);
1675
Takashi Iwai352f7f92012-12-19 12:52:06 +01001676 return badness;
1677}
1678
1679#define DEBUG_BADNESS
1680
1681#ifdef DEBUG_BADNESS
1682#define debug_badness snd_printdd
1683#else
1684#define debug_badness(...)
1685#endif
1686
Takashi Iwaia7694092013-01-21 10:43:18 +01001687#ifdef DEBUG_BADNESS
1688static inline void print_nid_path_idx(struct hda_codec *codec,
1689 const char *pfx, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001690{
Takashi Iwaia7694092013-01-21 10:43:18 +01001691 struct nid_path *path;
1692
1693 path = snd_hda_get_path_from_idx(codec, idx);
1694 if (path)
1695 print_nid_path(pfx, path);
1696}
1697
1698static void debug_show_configs(struct hda_codec *codec,
1699 struct auto_pin_cfg *cfg)
1700{
1701 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia7694092013-01-21 10:43:18 +01001702 static const char * const lo_type[3] = { "LO", "SP", "HP" };
Takashi Iwaia7694092013-01-21 10:43:18 +01001703 int i;
1704
1705 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001706 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001707 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001708 spec->multiout.dac_nids[0],
1709 spec->multiout.dac_nids[1],
1710 spec->multiout.dac_nids[2],
Takashi Iwaia7694092013-01-21 10:43:18 +01001711 spec->multiout.dac_nids[3],
1712 lo_type[cfg->line_out_type]);
1713 for (i = 0; i < cfg->line_outs; i++)
1714 print_nid_path_idx(codec, " out", spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001715 if (spec->multi_ios > 0)
1716 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1717 spec->multi_ios,
1718 spec->multi_io[0].pin, spec->multi_io[1].pin,
1719 spec->multi_io[0].dac, spec->multi_io[1].dac);
Takashi Iwaia7694092013-01-21 10:43:18 +01001720 for (i = 0; i < spec->multi_ios; i++)
1721 print_nid_path_idx(codec, " mio",
1722 spec->out_paths[cfg->line_outs + i]);
1723 if (cfg->hp_outs)
1724 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001725 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001726 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001727 spec->multiout.hp_out_nid[0],
1728 spec->multiout.hp_out_nid[1],
1729 spec->multiout.hp_out_nid[2],
1730 spec->multiout.hp_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001731 for (i = 0; i < cfg->hp_outs; i++)
1732 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1733 if (cfg->speaker_outs)
1734 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001735 cfg->speaker_pins[0], cfg->speaker_pins[1],
1736 cfg->speaker_pins[2], cfg->speaker_pins[3],
1737 spec->multiout.extra_out_nid[0],
1738 spec->multiout.extra_out_nid[1],
1739 spec->multiout.extra_out_nid[2],
1740 spec->multiout.extra_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001741 for (i = 0; i < cfg->speaker_outs; i++)
1742 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1743 for (i = 0; i < 3; i++)
1744 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001745}
Takashi Iwaia7694092013-01-21 10:43:18 +01001746#else
1747#define debug_show_configs(codec, cfg) /* NOP */
1748#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01001749
1750/* find all available DACs of the codec */
1751static void fill_all_dac_nids(struct hda_codec *codec)
1752{
1753 struct hda_gen_spec *spec = codec->spec;
1754 int i;
1755 hda_nid_t nid = codec->start_nid;
1756
1757 spec->num_all_dacs = 0;
1758 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1759 for (i = 0; i < codec->num_nodes; i++, nid++) {
1760 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1761 continue;
1762 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1763 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1764 break;
1765 }
1766 spec->all_dacs[spec->num_all_dacs++] = nid;
1767 }
1768}
1769
1770static int parse_output_paths(struct hda_codec *codec)
1771{
1772 struct hda_gen_spec *spec = codec->spec;
1773 struct auto_pin_cfg *cfg = &spec->autocfg;
1774 struct auto_pin_cfg *best_cfg;
Takashi Iwai9314a582013-01-21 10:49:05 +01001775 unsigned int val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001776 int best_badness = INT_MAX;
1777 int badness;
1778 bool fill_hardwired = true, fill_mio_first = true;
1779 bool best_wired = true, best_mio = true;
1780 bool hp_spk_swapped = false;
1781
Takashi Iwai352f7f92012-12-19 12:52:06 +01001782 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1783 if (!best_cfg)
1784 return -ENOMEM;
1785 *best_cfg = *cfg;
1786
1787 for (;;) {
1788 badness = fill_and_eval_dacs(codec, fill_hardwired,
1789 fill_mio_first);
1790 if (badness < 0) {
1791 kfree(best_cfg);
1792 return badness;
1793 }
1794 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1795 cfg->line_out_type, fill_hardwired, fill_mio_first,
1796 badness);
Takashi Iwaia7694092013-01-21 10:43:18 +01001797 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001798 if (badness < best_badness) {
1799 best_badness = badness;
1800 *best_cfg = *cfg;
1801 best_wired = fill_hardwired;
1802 best_mio = fill_mio_first;
1803 }
1804 if (!badness)
1805 break;
1806 fill_mio_first = !fill_mio_first;
1807 if (!fill_mio_first)
1808 continue;
1809 fill_hardwired = !fill_hardwired;
1810 if (!fill_hardwired)
1811 continue;
1812 if (hp_spk_swapped)
1813 break;
1814 hp_spk_swapped = true;
1815 if (cfg->speaker_outs > 0 &&
1816 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1817 cfg->hp_outs = cfg->line_outs;
1818 memcpy(cfg->hp_pins, cfg->line_out_pins,
1819 sizeof(cfg->hp_pins));
1820 cfg->line_outs = cfg->speaker_outs;
1821 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1822 sizeof(cfg->speaker_pins));
1823 cfg->speaker_outs = 0;
1824 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1825 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1826 fill_hardwired = true;
1827 continue;
1828 }
1829 if (cfg->hp_outs > 0 &&
1830 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1831 cfg->speaker_outs = cfg->line_outs;
1832 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1833 sizeof(cfg->speaker_pins));
1834 cfg->line_outs = cfg->hp_outs;
1835 memcpy(cfg->line_out_pins, cfg->hp_pins,
1836 sizeof(cfg->hp_pins));
1837 cfg->hp_outs = 0;
1838 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1839 cfg->line_out_type = AUTO_PIN_HP_OUT;
1840 fill_hardwired = true;
1841 continue;
1842 }
1843 break;
1844 }
1845
1846 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001847 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001848 *cfg = *best_cfg;
1849 fill_and_eval_dacs(codec, best_wired, best_mio);
1850 }
1851 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1852 cfg->line_out_type, best_wired, best_mio);
Takashi Iwaia7694092013-01-21 10:43:18 +01001853 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001854
1855 if (cfg->line_out_pins[0]) {
1856 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01001857 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001858 if (path)
1859 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01001860 if (spec->vmaster_nid)
1861 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
1862 HDA_OUTPUT, spec->vmaster_tlv);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001863 }
1864
Takashi Iwai9314a582013-01-21 10:49:05 +01001865 /* set initial pinctl targets */
1866 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
1867 val = PIN_HP;
1868 else
1869 val = PIN_OUT;
1870 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
1871 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1872 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
1873 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1874 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
1875 set_pin_targets(codec, cfg->speaker_outs,
1876 cfg->speaker_pins, val);
1877 }
1878
Takashi Iwai55a63d42013-03-21 17:20:12 +01001879 /* clear indep_hp flag if not available */
1880 if (spec->indep_hp && !indep_hp_possible(codec))
1881 spec->indep_hp = 0;
1882
Takashi Iwai352f7f92012-12-19 12:52:06 +01001883 kfree(best_cfg);
1884 return 0;
1885}
1886
1887/* add playback controls from the parsed DAC table */
1888static int create_multi_out_ctls(struct hda_codec *codec,
1889 const struct auto_pin_cfg *cfg)
1890{
1891 struct hda_gen_spec *spec = codec->spec;
1892 int i, err, noutputs;
1893
1894 noutputs = cfg->line_outs;
1895 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1896 noutputs += spec->multi_ios;
1897
1898 for (i = 0; i < noutputs; i++) {
1899 const char *name;
1900 int index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001901 struct nid_path *path;
1902
Takashi Iwai196c17662013-01-04 15:01:40 +01001903 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001904 if (!path)
1905 continue;
Takashi Iwai247d85e2013-01-17 16:18:11 +01001906
1907 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001908 if (!name || !strcmp(name, "CLFE")) {
1909 /* Center/LFE */
1910 err = add_vol_ctl(codec, "Center", 0, 1, path);
1911 if (err < 0)
1912 return err;
1913 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1914 if (err < 0)
1915 return err;
Takashi Iwai247d85e2013-01-17 16:18:11 +01001916 } else {
1917 err = add_stereo_vol(codec, name, index, path);
1918 if (err < 0)
1919 return err;
1920 }
1921
1922 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
1923 if (!name || !strcmp(name, "CLFE")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001924 err = add_sw_ctl(codec, "Center", 0, 1, path);
1925 if (err < 0)
1926 return err;
1927 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1928 if (err < 0)
1929 return err;
1930 } else {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001931 err = add_stereo_sw(codec, name, index, path);
1932 if (err < 0)
1933 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 }
1935 }
1936 return 0;
1937}
1938
Takashi Iwaic2c80382013-01-07 10:33:57 +01001939static int create_extra_out(struct hda_codec *codec, int path_idx,
Takashi Iwai196c17662013-01-04 15:01:40 +01001940 const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001942 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 int err;
1944
Takashi Iwai196c17662013-01-04 15:01:40 +01001945 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001946 if (!path)
1947 return 0;
Takashi Iwaic2c80382013-01-07 10:33:57 +01001948 err = add_stereo_vol(codec, pfx, cidx, path);
1949 if (err < 0)
1950 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001951 err = add_stereo_sw(codec, pfx, cidx, path);
1952 if (err < 0)
1953 return err;
1954 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955}
1956
Takashi Iwai352f7f92012-12-19 12:52:06 +01001957/* add playback controls for speaker and HP outputs */
1958static int create_extra_outs(struct hda_codec *codec, int num_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001959 const int *paths, const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960{
Takashi Iwaic2c80382013-01-07 10:33:57 +01001961 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001962
1963 for (i = 0; i < num_pins; i++) {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001964 const char *name;
Takashi Iwai975cc022013-06-28 11:56:49 +02001965 char tmp[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwaic2c80382013-01-07 10:33:57 +01001966 int err, idx = 0;
1967
1968 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
1969 name = "Bass Speaker";
1970 else if (num_pins >= 3) {
1971 snprintf(tmp, sizeof(tmp), "%s %s",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001972 pfx, channel_name[i]);
Takashi Iwaic2c80382013-01-07 10:33:57 +01001973 name = tmp;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001974 } else {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001975 name = pfx;
1976 idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 }
Takashi Iwaic2c80382013-01-07 10:33:57 +01001978 err = create_extra_out(codec, paths[i], name, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001979 if (err < 0)
1980 return err;
1981 }
1982 return 0;
1983}
Takashi Iwai97ec5582006-03-21 11:29:07 +01001984
Takashi Iwai352f7f92012-12-19 12:52:06 +01001985static int create_hp_out_ctls(struct hda_codec *codec)
1986{
1987 struct hda_gen_spec *spec = codec->spec;
1988 return create_extra_outs(codec, spec->autocfg.hp_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001989 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001990 "Headphone");
1991}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
Takashi Iwai352f7f92012-12-19 12:52:06 +01001993static int create_speaker_out_ctls(struct hda_codec *codec)
1994{
1995 struct hda_gen_spec *spec = codec->spec;
1996 return create_extra_outs(codec, spec->autocfg.speaker_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001997 spec->speaker_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001998 "Speaker");
1999}
2000
2001/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002002 * independent HP controls
2003 */
2004
Takashi Iwai963afde2013-05-31 15:20:31 +02002005static void call_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002006static int indep_hp_info(struct snd_kcontrol *kcontrol,
2007 struct snd_ctl_elem_info *uinfo)
2008{
2009 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2010}
2011
2012static int indep_hp_get(struct snd_kcontrol *kcontrol,
2013 struct snd_ctl_elem_value *ucontrol)
2014{
2015 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2016 struct hda_gen_spec *spec = codec->spec;
2017 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
2018 return 0;
2019}
2020
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002021static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2022 int nomix_path_idx, int mix_path_idx,
2023 int out_type);
2024
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002025static int indep_hp_put(struct snd_kcontrol *kcontrol,
2026 struct snd_ctl_elem_value *ucontrol)
2027{
2028 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2029 struct hda_gen_spec *spec = codec->spec;
2030 unsigned int select = ucontrol->value.enumerated.item[0];
2031 int ret = 0;
2032
2033 mutex_lock(&spec->pcm_mutex);
2034 if (spec->active_streams) {
2035 ret = -EBUSY;
2036 goto unlock;
2037 }
2038
2039 if (spec->indep_hp_enabled != select) {
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002040 hda_nid_t *dacp;
2041 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2042 dacp = &spec->private_dac_nids[0];
2043 else
2044 dacp = &spec->multiout.hp_out_nid[0];
2045
2046 /* update HP aamix paths in case it conflicts with indep HP */
2047 if (spec->have_aamix_ctl) {
2048 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2049 update_aamix_paths(codec, spec->aamix_mode,
2050 spec->out_paths[0],
2051 spec->aamix_out_paths[0],
2052 spec->autocfg.line_out_type);
2053 else
2054 update_aamix_paths(codec, spec->aamix_mode,
2055 spec->hp_paths[0],
2056 spec->aamix_out_paths[1],
2057 AUTO_PIN_HP_OUT);
2058 }
2059
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002060 spec->indep_hp_enabled = select;
2061 if (spec->indep_hp_enabled)
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002062 *dacp = 0;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002063 else
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002064 *dacp = spec->alt_dac_nid;
Takashi Iwai92603c52013-01-22 07:46:31 +01002065
Takashi Iwai963afde2013-05-31 15:20:31 +02002066 call_hp_automute(codec, NULL);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002067 ret = 1;
2068 }
2069 unlock:
2070 mutex_unlock(&spec->pcm_mutex);
2071 return ret;
2072}
2073
2074static const struct snd_kcontrol_new indep_hp_ctl = {
2075 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2076 .name = "Independent HP",
2077 .info = indep_hp_info,
2078 .get = indep_hp_get,
2079 .put = indep_hp_put,
2080};
2081
2082
2083static int create_indep_hp_ctls(struct hda_codec *codec)
2084{
2085 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002086 hda_nid_t dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002087
2088 if (!spec->indep_hp)
2089 return 0;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002090 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2091 dac = spec->multiout.dac_nids[0];
2092 else
2093 dac = spec->multiout.hp_out_nid[0];
2094 if (!dac) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002095 spec->indep_hp = 0;
2096 return 0;
2097 }
2098
2099 spec->indep_hp_enabled = false;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002100 spec->alt_dac_nid = dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002101 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
2102 return -ENOMEM;
2103 return 0;
2104}
2105
2106/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002107 * channel mode enum control
2108 */
2109
2110static int ch_mode_info(struct snd_kcontrol *kcontrol,
2111 struct snd_ctl_elem_info *uinfo)
2112{
2113 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2114 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002115 int chs;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002116
2117 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2118 uinfo->count = 1;
2119 uinfo->value.enumerated.items = spec->multi_ios + 1;
2120 if (uinfo->value.enumerated.item > spec->multi_ios)
2121 uinfo->value.enumerated.item = spec->multi_ios;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002122 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2123 sprintf(uinfo->value.enumerated.name, "%dch", chs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002124 return 0;
2125}
2126
2127static int ch_mode_get(struct snd_kcontrol *kcontrol,
2128 struct snd_ctl_elem_value *ucontrol)
2129{
2130 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2131 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002132 ucontrol->value.enumerated.item[0] =
2133 (spec->ext_channel_count - spec->min_channel_count) / 2;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002134 return 0;
2135}
2136
Takashi Iwai196c17662013-01-04 15:01:40 +01002137static inline struct nid_path *
2138get_multiio_path(struct hda_codec *codec, int idx)
2139{
2140 struct hda_gen_spec *spec = codec->spec;
2141 return snd_hda_get_path_from_idx(codec,
2142 spec->out_paths[spec->autocfg.line_outs + idx]);
2143}
2144
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002145static void update_automute_all(struct hda_codec *codec);
2146
Takashi Iwai65033cc2013-04-16 12:31:05 +02002147/* Default value to be passed as aamix argument for snd_hda_activate_path();
2148 * used for output paths
2149 */
2150static bool aamix_default(struct hda_gen_spec *spec)
2151{
2152 return !spec->have_aamix_ctl || spec->aamix_mode;
2153}
2154
Takashi Iwai352f7f92012-12-19 12:52:06 +01002155static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2156{
2157 struct hda_gen_spec *spec = codec->spec;
2158 hda_nid_t nid = spec->multi_io[idx].pin;
2159 struct nid_path *path;
2160
Takashi Iwai196c17662013-01-04 15:01:40 +01002161 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002162 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002164
2165 if (path->active == output)
2166 return 0;
2167
2168 if (output) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01002169 set_pin_target(codec, nid, PIN_OUT, true);
Takashi Iwai65033cc2013-04-16 12:31:05 +02002170 snd_hda_activate_path(codec, path, true, aamix_default(spec));
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002171 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002172 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002173 set_pin_eapd(codec, nid, false);
Takashi Iwai65033cc2013-04-16 12:31:05 +02002174 snd_hda_activate_path(codec, path, false, aamix_default(spec));
Takashi Iwai2c12c302013-01-10 09:33:29 +01002175 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002176 path_power_down_sync(codec, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 }
Takashi Iwaia365fed2013-01-10 16:10:06 +01002178
2179 /* update jack retasking in case it modifies any of them */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002180 update_automute_all(codec);
Takashi Iwaia365fed2013-01-10 16:10:06 +01002181
Takashi Iwai352f7f92012-12-19 12:52:06 +01002182 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183}
2184
Takashi Iwai352f7f92012-12-19 12:52:06 +01002185static int ch_mode_put(struct snd_kcontrol *kcontrol,
2186 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002188 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2189 struct hda_gen_spec *spec = codec->spec;
2190 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
Takashi Iwai352f7f92012-12-19 12:52:06 +01002192 ch = ucontrol->value.enumerated.item[0];
2193 if (ch < 0 || ch > spec->multi_ios)
2194 return -EINVAL;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002195 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002196 return 0;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002197 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002198 for (i = 0; i < spec->multi_ios; i++)
2199 set_multi_io(codec, i, i < ch);
2200 spec->multiout.max_channels = max(spec->ext_channel_count,
2201 spec->const_channel_count);
2202 if (spec->need_dac_fix)
2203 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 return 1;
2205}
2206
Takashi Iwai352f7f92012-12-19 12:52:06 +01002207static const struct snd_kcontrol_new channel_mode_enum = {
2208 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2209 .name = "Channel Mode",
2210 .info = ch_mode_info,
2211 .get = ch_mode_get,
2212 .put = ch_mode_put,
2213};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214
Takashi Iwai352f7f92012-12-19 12:52:06 +01002215static int create_multi_channel_mode(struct hda_codec *codec)
2216{
2217 struct hda_gen_spec *spec = codec->spec;
2218
2219 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002220 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002221 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 return 0;
2224}
2225
Takashi Iwai352f7f92012-12-19 12:52:06 +01002226/*
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002227 * aamix loopback enable/disable switch
2228 */
2229
2230#define loopback_mixing_info indep_hp_info
2231
2232static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2233 struct snd_ctl_elem_value *ucontrol)
2234{
2235 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2236 struct hda_gen_spec *spec = codec->spec;
2237 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2238 return 0;
2239}
2240
2241static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002242 int nomix_path_idx, int mix_path_idx,
2243 int out_type)
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002244{
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002245 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002246 struct nid_path *nomix_path, *mix_path;
2247
2248 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2249 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2250 if (!nomix_path || !mix_path)
2251 return;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002252
2253 /* if HP aamix path is driven from a different DAC and the
2254 * independent HP mode is ON, can't turn on aamix path
2255 */
2256 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2257 mix_path->path[0] != spec->alt_dac_nid)
2258 do_mix = false;
2259
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002260 if (do_mix) {
2261 snd_hda_activate_path(codec, nomix_path, false, true);
2262 snd_hda_activate_path(codec, mix_path, true, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002263 path_power_down_sync(codec, nomix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002264 } else {
Takashi Iwai65033cc2013-04-16 12:31:05 +02002265 snd_hda_activate_path(codec, mix_path, false, false);
2266 snd_hda_activate_path(codec, nomix_path, true, false);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002267 path_power_down_sync(codec, mix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002268 }
2269}
2270
2271static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2272 struct snd_ctl_elem_value *ucontrol)
2273{
2274 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2275 struct hda_gen_spec *spec = codec->spec;
2276 unsigned int val = ucontrol->value.enumerated.item[0];
2277
2278 if (val == spec->aamix_mode)
2279 return 0;
2280 spec->aamix_mode = val;
2281 update_aamix_paths(codec, val, spec->out_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002282 spec->aamix_out_paths[0],
2283 spec->autocfg.line_out_type);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002284 update_aamix_paths(codec, val, spec->hp_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002285 spec->aamix_out_paths[1],
2286 AUTO_PIN_HP_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002287 update_aamix_paths(codec, val, spec->speaker_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002288 spec->aamix_out_paths[2],
2289 AUTO_PIN_SPEAKER_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002290 return 1;
2291}
2292
2293static const struct snd_kcontrol_new loopback_mixing_enum = {
2294 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2295 .name = "Loopback Mixing",
2296 .info = loopback_mixing_info,
2297 .get = loopback_mixing_get,
2298 .put = loopback_mixing_put,
2299};
2300
2301static int create_loopback_mixing_ctl(struct hda_codec *codec)
2302{
2303 struct hda_gen_spec *spec = codec->spec;
2304
2305 if (!spec->mixer_nid)
2306 return 0;
2307 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
2308 spec->aamix_out_paths[2]))
2309 return 0;
2310 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2311 return -ENOMEM;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002312 spec->have_aamix_ctl = 1;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002313 return 0;
2314}
2315
2316/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002317 * shared headphone/mic handling
2318 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002319
Takashi Iwai352f7f92012-12-19 12:52:06 +01002320static void call_update_outputs(struct hda_codec *codec);
2321
2322/* for shared I/O, change the pin-control accordingly */
Takashi Iwai967303d2013-02-19 17:12:42 +01002323static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002324{
2325 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai967303d2013-02-19 17:12:42 +01002326 bool as_mic;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002327 unsigned int val;
Takashi Iwai967303d2013-02-19 17:12:42 +01002328 hda_nid_t pin;
2329
2330 pin = spec->hp_mic_pin;
2331 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
2332
2333 if (!force) {
2334 val = snd_hda_codec_get_pin_target(codec, pin);
2335 if (as_mic) {
2336 if (val & PIN_IN)
2337 return;
2338 } else {
2339 if (val & PIN_OUT)
2340 return;
2341 }
2342 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002343
2344 val = snd_hda_get_default_vref(codec, pin);
Takashi Iwai967303d2013-02-19 17:12:42 +01002345 /* if the HP pin doesn't support VREF and the codec driver gives an
2346 * alternative pin, set up the VREF on that pin instead
2347 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002348 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2349 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2350 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2351 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01002352 snd_hda_set_pin_ctl_cache(codec, vref_pin,
Takashi Iwai967303d2013-02-19 17:12:42 +01002353 PIN_IN | (as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02002354 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002355
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002356 if (!spec->hp_mic_jack_modes) {
2357 if (as_mic)
2358 val |= PIN_IN;
2359 else
2360 val = PIN_HP;
2361 set_pin_target(codec, pin, val, true);
Takashi Iwai963afde2013-05-31 15:20:31 +02002362 call_hp_automute(codec, NULL);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002363 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002364}
2365
2366/* create a shared input with the headphone out */
Takashi Iwai967303d2013-02-19 17:12:42 +01002367static int create_hp_mic(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002368{
2369 struct hda_gen_spec *spec = codec->spec;
2370 struct auto_pin_cfg *cfg = &spec->autocfg;
2371 unsigned int defcfg;
2372 hda_nid_t nid;
2373
Takashi Iwai967303d2013-02-19 17:12:42 +01002374 if (!spec->hp_mic) {
2375 if (spec->suppress_hp_mic_detect)
2376 return 0;
2377 /* automatic detection: only if no input or a single internal
2378 * input pin is found, try to detect the shared hp/mic
2379 */
2380 if (cfg->num_inputs > 1)
2381 return 0;
2382 else if (cfg->num_inputs == 1) {
2383 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2384 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2385 return 0;
2386 }
2387 }
2388
2389 spec->hp_mic = 0; /* clear once */
2390 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002391 return 0;
2392
Takashi Iwai967303d2013-02-19 17:12:42 +01002393 nid = 0;
2394 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2395 nid = cfg->line_out_pins[0];
2396 else if (cfg->hp_outs > 0)
2397 nid = cfg->hp_pins[0];
2398 if (!nid)
2399 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002400
2401 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2402 return 0; /* no input */
2403
Takashi Iwai967303d2013-02-19 17:12:42 +01002404 cfg->inputs[cfg->num_inputs].pin = nid;
2405 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
David Henningssoncb420b12013-04-11 11:30:28 +02002406 cfg->inputs[cfg->num_inputs].is_headphone_mic = 1;
Takashi Iwai967303d2013-02-19 17:12:42 +01002407 cfg->num_inputs++;
2408 spec->hp_mic = 1;
2409 spec->hp_mic_pin = nid;
2410 /* we can't handle auto-mic together with HP-mic */
2411 spec->suppress_auto_mic = 1;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002412 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
2413 return 0;
2414}
2415
Takashi Iwai978e77e2013-01-10 16:57:58 +01002416/*
2417 * output jack mode
2418 */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002419
2420static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2421
2422static const char * const out_jack_texts[] = {
2423 "Line Out", "Headphone Out",
2424};
2425
Takashi Iwai978e77e2013-01-10 16:57:58 +01002426static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2427 struct snd_ctl_elem_info *uinfo)
2428{
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002429 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
Takashi Iwai978e77e2013-01-10 16:57:58 +01002430}
2431
2432static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2433 struct snd_ctl_elem_value *ucontrol)
2434{
2435 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2436 hda_nid_t nid = kcontrol->private_value;
2437 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2438 ucontrol->value.enumerated.item[0] = 1;
2439 else
2440 ucontrol->value.enumerated.item[0] = 0;
2441 return 0;
2442}
2443
2444static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2445 struct snd_ctl_elem_value *ucontrol)
2446{
2447 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2448 hda_nid_t nid = kcontrol->private_value;
2449 unsigned int val;
2450
2451 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2452 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2453 return 0;
2454 snd_hda_set_pin_ctl_cache(codec, nid, val);
2455 return 1;
2456}
2457
2458static const struct snd_kcontrol_new out_jack_mode_enum = {
2459 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2460 .info = out_jack_mode_info,
2461 .get = out_jack_mode_get,
2462 .put = out_jack_mode_put,
2463};
2464
2465static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2466{
2467 struct hda_gen_spec *spec = codec->spec;
2468 int i;
2469
2470 for (i = 0; i < spec->kctls.used; i++) {
2471 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2472 if (!strcmp(kctl->name, name) && kctl->index == idx)
2473 return true;
2474 }
2475 return false;
2476}
2477
2478static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2479 char *name, size_t name_len)
2480{
2481 struct hda_gen_spec *spec = codec->spec;
2482 int idx = 0;
2483
2484 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2485 strlcat(name, " Jack Mode", name_len);
2486
2487 for (; find_kctl_name(codec, name, idx); idx++)
2488 ;
2489}
2490
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002491static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2492{
2493 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002494 if (spec->add_jack_modes) {
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002495 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2496 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2497 return 2;
2498 }
2499 return 1;
2500}
2501
Takashi Iwai978e77e2013-01-10 16:57:58 +01002502static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2503 hda_nid_t *pins)
2504{
2505 struct hda_gen_spec *spec = codec->spec;
2506 int i;
2507
2508 for (i = 0; i < num_pins; i++) {
2509 hda_nid_t pin = pins[i];
Takashi Iwaiced4cef2013-11-26 08:33:45 +01002510 if (pin == spec->hp_mic_pin)
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002511 continue;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002512 if (get_out_jack_num_items(codec, pin) > 1) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01002513 struct snd_kcontrol_new *knew;
Takashi Iwai975cc022013-06-28 11:56:49 +02002514 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai978e77e2013-01-10 16:57:58 +01002515 get_jack_mode_name(codec, pin, name, sizeof(name));
2516 knew = snd_hda_gen_add_kctl(spec, name,
2517 &out_jack_mode_enum);
2518 if (!knew)
2519 return -ENOMEM;
2520 knew->private_value = pin;
2521 }
2522 }
2523
2524 return 0;
2525}
2526
Takashi Iwai294765582013-01-17 09:52:11 +01002527/*
2528 * input jack mode
2529 */
2530
2531/* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2532#define NUM_VREFS 6
2533
2534static const char * const vref_texts[NUM_VREFS] = {
2535 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2536 "", "Mic 80pc Bias", "Mic 100pc Bias"
2537};
2538
2539static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2540{
2541 unsigned int pincap;
2542
2543 pincap = snd_hda_query_pin_caps(codec, pin);
2544 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2545 /* filter out unusual vrefs */
2546 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2547 return pincap;
2548}
2549
2550/* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2551static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2552{
2553 unsigned int i, n = 0;
2554
2555 for (i = 0; i < NUM_VREFS; i++) {
2556 if (vref_caps & (1 << i)) {
2557 if (n == item_idx)
2558 return i;
2559 n++;
2560 }
2561 }
2562 return 0;
2563}
2564
2565/* convert back from the vref ctl index to the enum item index */
2566static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2567{
2568 unsigned int i, n = 0;
2569
2570 for (i = 0; i < NUM_VREFS; i++) {
2571 if (i == idx)
2572 return n;
2573 if (vref_caps & (1 << i))
2574 n++;
2575 }
2576 return 0;
2577}
2578
2579static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2580 struct snd_ctl_elem_info *uinfo)
2581{
2582 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2583 hda_nid_t nid = kcontrol->private_value;
2584 unsigned int vref_caps = get_vref_caps(codec, nid);
2585
2586 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2587 vref_texts);
2588 /* set the right text */
2589 strcpy(uinfo->value.enumerated.name,
2590 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2591 return 0;
2592}
2593
2594static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2595 struct snd_ctl_elem_value *ucontrol)
2596{
2597 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2598 hda_nid_t nid = kcontrol->private_value;
2599 unsigned int vref_caps = get_vref_caps(codec, nid);
2600 unsigned int idx;
2601
2602 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2603 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2604 return 0;
2605}
2606
2607static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2608 struct snd_ctl_elem_value *ucontrol)
2609{
2610 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2611 hda_nid_t nid = kcontrol->private_value;
2612 unsigned int vref_caps = get_vref_caps(codec, nid);
2613 unsigned int val, idx;
2614
2615 val = snd_hda_codec_get_pin_target(codec, nid);
2616 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2617 if (idx == ucontrol->value.enumerated.item[0])
2618 return 0;
2619
2620 val &= ~AC_PINCTL_VREFEN;
2621 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2622 snd_hda_set_pin_ctl_cache(codec, nid, val);
2623 return 1;
2624}
2625
2626static const struct snd_kcontrol_new in_jack_mode_enum = {
2627 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2628 .info = in_jack_mode_info,
2629 .get = in_jack_mode_get,
2630 .put = in_jack_mode_put,
2631};
2632
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002633static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2634{
2635 struct hda_gen_spec *spec = codec->spec;
2636 int nitems = 0;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002637 if (spec->add_jack_modes)
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002638 nitems = hweight32(get_vref_caps(codec, pin));
2639 return nitems ? nitems : 1;
2640}
2641
Takashi Iwai294765582013-01-17 09:52:11 +01002642static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2643{
2644 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai294765582013-01-17 09:52:11 +01002645 struct snd_kcontrol_new *knew;
Takashi Iwai975cc022013-06-28 11:56:49 +02002646 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002647 unsigned int defcfg;
2648
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002649 if (pin == spec->hp_mic_pin)
2650 return 0; /* already done in create_out_jack_mode() */
Takashi Iwai294765582013-01-17 09:52:11 +01002651
2652 /* no jack mode for fixed pins */
2653 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2654 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2655 return 0;
2656
2657 /* no multiple vref caps? */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002658 if (get_in_jack_num_items(codec, pin) <= 1)
Takashi Iwai294765582013-01-17 09:52:11 +01002659 return 0;
2660
2661 get_jack_mode_name(codec, pin, name, sizeof(name));
2662 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2663 if (!knew)
2664 return -ENOMEM;
2665 knew->private_value = pin;
2666 return 0;
2667}
2668
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002669/*
2670 * HP/mic shared jack mode
2671 */
2672static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2673 struct snd_ctl_elem_info *uinfo)
2674{
2675 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2676 hda_nid_t nid = kcontrol->private_value;
2677 int out_jacks = get_out_jack_num_items(codec, nid);
2678 int in_jacks = get_in_jack_num_items(codec, nid);
2679 const char *text = NULL;
2680 int idx;
2681
2682 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2683 uinfo->count = 1;
2684 uinfo->value.enumerated.items = out_jacks + in_jacks;
2685 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2686 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2687 idx = uinfo->value.enumerated.item;
2688 if (idx < out_jacks) {
2689 if (out_jacks > 1)
2690 text = out_jack_texts[idx];
2691 else
2692 text = "Headphone Out";
2693 } else {
2694 idx -= out_jacks;
2695 if (in_jacks > 1) {
2696 unsigned int vref_caps = get_vref_caps(codec, nid);
2697 text = vref_texts[get_vref_idx(vref_caps, idx)];
2698 } else
2699 text = "Mic In";
2700 }
2701
2702 strcpy(uinfo->value.enumerated.name, text);
2703 return 0;
2704}
2705
2706static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2707{
2708 int out_jacks = get_out_jack_num_items(codec, nid);
2709 int in_jacks = get_in_jack_num_items(codec, nid);
2710 unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2711 int idx = 0;
2712
2713 if (val & PIN_OUT) {
2714 if (out_jacks > 1 && val == PIN_HP)
2715 idx = 1;
2716 } else if (val & PIN_IN) {
2717 idx = out_jacks;
2718 if (in_jacks > 1) {
2719 unsigned int vref_caps = get_vref_caps(codec, nid);
2720 val &= AC_PINCTL_VREFEN;
2721 idx += cvt_from_vref_idx(vref_caps, val);
2722 }
2723 }
2724 return idx;
2725}
2726
2727static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2728 struct snd_ctl_elem_value *ucontrol)
2729{
2730 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2731 hda_nid_t nid = kcontrol->private_value;
2732 ucontrol->value.enumerated.item[0] =
2733 get_cur_hp_mic_jack_mode(codec, nid);
2734 return 0;
2735}
2736
2737static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2738 struct snd_ctl_elem_value *ucontrol)
2739{
2740 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2741 hda_nid_t nid = kcontrol->private_value;
2742 int out_jacks = get_out_jack_num_items(codec, nid);
2743 int in_jacks = get_in_jack_num_items(codec, nid);
2744 unsigned int val, oldval, idx;
2745
2746 oldval = get_cur_hp_mic_jack_mode(codec, nid);
2747 idx = ucontrol->value.enumerated.item[0];
2748 if (oldval == idx)
2749 return 0;
2750
2751 if (idx < out_jacks) {
2752 if (out_jacks > 1)
2753 val = idx ? PIN_HP : PIN_OUT;
2754 else
2755 val = PIN_HP;
2756 } else {
2757 idx -= out_jacks;
2758 if (in_jacks > 1) {
2759 unsigned int vref_caps = get_vref_caps(codec, nid);
2760 val = snd_hda_codec_get_pin_target(codec, nid);
Takashi Iwai3f550e32013-03-07 18:30:27 +01002761 val &= ~(AC_PINCTL_VREFEN | PIN_HP);
2762 val |= get_vref_idx(vref_caps, idx) | PIN_IN;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002763 } else
Takashi Iwai16c0cefe2013-11-26 08:44:26 +01002764 val = snd_hda_get_default_vref(codec, nid) | PIN_IN;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002765 }
2766 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwai963afde2013-05-31 15:20:31 +02002767 call_hp_automute(codec, NULL);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002768
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002769 return 1;
2770}
2771
2772static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
2773 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2774 .info = hp_mic_jack_mode_info,
2775 .get = hp_mic_jack_mode_get,
2776 .put = hp_mic_jack_mode_put,
2777};
2778
2779static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2780{
2781 struct hda_gen_spec *spec = codec->spec;
2782 struct snd_kcontrol_new *knew;
2783
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002784 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
2785 &hp_mic_jack_mode_enum);
2786 if (!knew)
2787 return -ENOMEM;
2788 knew->private_value = pin;
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002789 spec->hp_mic_jack_modes = 1;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002790 return 0;
2791}
Takashi Iwai352f7f92012-12-19 12:52:06 +01002792
2793/*
2794 * Parse input paths
2795 */
2796
Takashi Iwai352f7f92012-12-19 12:52:06 +01002797/* add the powersave loopback-list entry */
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002798static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002799{
2800 struct hda_amp_list *list;
2801
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002802 list = snd_array_new(&spec->loopback_list);
2803 if (!list)
2804 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002805 list->nid = mix;
2806 list->dir = HDA_INPUT;
2807 list->idx = idx;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002808 spec->loopback.amplist = spec->loopback_list.list;
2809 return 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002810}
Takashi Iwaicb53c622007-08-10 17:21:45 +02002811
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002812/* return true if either a volume or a mute amp is found for the given
2813 * aamix path; the amp has to be either in the mixer node or its direct leaf
2814 */
2815static bool look_for_mix_leaf_ctls(struct hda_codec *codec, hda_nid_t mix_nid,
2816 hda_nid_t pin, unsigned int *mix_val,
2817 unsigned int *mute_val)
2818{
2819 int idx, num_conns;
2820 const hda_nid_t *list;
2821 hda_nid_t nid;
2822
2823 idx = snd_hda_get_conn_index(codec, mix_nid, pin, true);
2824 if (idx < 0)
2825 return false;
2826
2827 *mix_val = *mute_val = 0;
2828 if (nid_has_volume(codec, mix_nid, HDA_INPUT))
2829 *mix_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2830 if (nid_has_mute(codec, mix_nid, HDA_INPUT))
2831 *mute_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2832 if (*mix_val && *mute_val)
2833 return true;
2834
2835 /* check leaf node */
2836 num_conns = snd_hda_get_conn_list(codec, mix_nid, &list);
2837 if (num_conns < idx)
2838 return false;
2839 nid = list[idx];
2840 if (!*mix_val && nid_has_volume(codec, nid, HDA_OUTPUT))
2841 *mix_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2842 if (!*mute_val && nid_has_mute(codec, nid, HDA_OUTPUT))
2843 *mute_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2844
2845 return *mix_val || *mute_val;
2846}
2847
Takashi Iwai352f7f92012-12-19 12:52:06 +01002848/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01002849static int new_analog_input(struct hda_codec *codec, int input_idx,
2850 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002851 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002853 struct hda_gen_spec *spec = codec->spec;
2854 struct nid_path *path;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002855 unsigned int mix_val, mute_val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002856 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002858 if (!look_for_mix_leaf_ctls(codec, mix_nid, pin, &mix_val, &mute_val))
2859 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002860
Takashi Iwai3ca529d2013-01-07 17:25:08 +01002861 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002862 if (!path)
2863 return -EINVAL;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002864 print_nid_path("loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01002865 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002866
2867 idx = path->idx[path->depth - 1];
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002868 if (mix_val) {
2869 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, mix_val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002870 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 return err;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002872 path->ctls[NID_PATH_VOL_CTL] = mix_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 }
2874
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002875 if (mute_val) {
2876 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, mute_val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002877 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 return err;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002879 path->ctls[NID_PATH_MUTE_CTL] = mute_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 }
2881
Takashi Iwai352f7f92012-12-19 12:52:06 +01002882 path->active = true;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002883 err = add_loopback_list(spec, mix_nid, idx);
2884 if (err < 0)
2885 return err;
Takashi Iwaie4a395e2013-01-23 17:00:31 +01002886
2887 if (spec->mixer_nid != spec->mixer_merge_nid &&
2888 !spec->loopback_merge_path) {
2889 path = snd_hda_add_new_path(codec, spec->mixer_nid,
2890 spec->mixer_merge_nid, 0);
2891 if (path) {
2892 print_nid_path("loopback-merge", path);
2893 path->active = true;
2894 spec->loopback_merge_path =
2895 snd_hda_get_path_idx(codec, path);
2896 }
2897 }
2898
Takashi Iwai352f7f92012-12-19 12:52:06 +01002899 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900}
2901
Takashi Iwai352f7f92012-12-19 12:52:06 +01002902static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002904 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
2905 return (pincap & AC_PINCAP_IN) != 0;
2906}
2907
2908/* Parse the codec tree and retrieve ADCs */
2909static int fill_adc_nids(struct hda_codec *codec)
2910{
2911 struct hda_gen_spec *spec = codec->spec;
2912 hda_nid_t nid;
2913 hda_nid_t *adc_nids = spec->adc_nids;
2914 int max_nums = ARRAY_SIZE(spec->adc_nids);
2915 int i, nums = 0;
2916
2917 nid = codec->start_nid;
2918 for (i = 0; i < codec->num_nodes; i++, nid++) {
2919 unsigned int caps = get_wcaps(codec, nid);
2920 int type = get_wcaps_type(caps);
2921
2922 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2923 continue;
2924 adc_nids[nums] = nid;
2925 if (++nums >= max_nums)
2926 break;
2927 }
2928 spec->num_adc_nids = nums;
Takashi Iwai0ffd5342013-01-17 15:53:29 +01002929
2930 /* copy the detected ADCs to all_adcs[] */
2931 spec->num_all_adcs = nums;
2932 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
2933
Takashi Iwai352f7f92012-12-19 12:52:06 +01002934 return nums;
2935}
2936
2937/* filter out invalid adc_nids that don't give all active input pins;
2938 * if needed, check whether dynamic ADC-switching is available
2939 */
2940static int check_dyn_adc_switch(struct hda_codec *codec)
2941{
2942 struct hda_gen_spec *spec = codec->spec;
2943 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002944 unsigned int ok_bits;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002945 int i, n, nums;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002946
Takashi Iwai352f7f92012-12-19 12:52:06 +01002947 nums = 0;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002948 ok_bits = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002949 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002950 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002951 if (!spec->input_paths[i][n])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002952 break;
2953 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002954 if (i >= imux->num_items) {
2955 ok_bits |= (1 << n);
2956 nums++;
2957 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002958 }
2959
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002960 if (!ok_bits) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002961 /* check whether ADC-switch is possible */
2962 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002963 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002964 if (spec->input_paths[i][n]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002965 spec->dyn_adc_idx[i] = n;
2966 break;
2967 }
2968 }
2969 }
2970
2971 snd_printdd("hda-codec: enabling ADC switching\n");
2972 spec->dyn_adc_switch = 1;
2973 } else if (nums != spec->num_adc_nids) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002974 /* shrink the invalid adcs and input paths */
2975 nums = 0;
2976 for (n = 0; n < spec->num_adc_nids; n++) {
2977 if (!(ok_bits & (1 << n)))
2978 continue;
2979 if (n != nums) {
2980 spec->adc_nids[nums] = spec->adc_nids[n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002981 for (i = 0; i < imux->num_items; i++) {
2982 invalidate_nid_path(codec,
2983 spec->input_paths[i][nums]);
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002984 spec->input_paths[i][nums] =
2985 spec->input_paths[i][n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002986 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002987 }
2988 nums++;
2989 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002990 spec->num_adc_nids = nums;
2991 }
2992
Takashi Iwai967303d2013-02-19 17:12:42 +01002993 if (imux->num_items == 1 ||
2994 (imux->num_items == 2 && spec->hp_mic)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002995 snd_printdd("hda-codec: reducing to a single ADC\n");
2996 spec->num_adc_nids = 1; /* reduce to a single ADC */
2997 }
2998
2999 /* single index for individual volumes ctls */
3000 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
3001 spec->num_adc_nids = 1;
3002
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 return 0;
3004}
3005
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003006/* parse capture source paths from the given pin and create imux items */
3007static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
Takashi Iwai9dba2052013-01-18 10:01:15 +01003008 int cfg_idx, int num_adcs,
3009 const char *label, int anchor)
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003010{
3011 struct hda_gen_spec *spec = codec->spec;
3012 struct hda_input_mux *imux = &spec->input_mux;
3013 int imux_idx = imux->num_items;
3014 bool imux_added = false;
3015 int c;
3016
3017 for (c = 0; c < num_adcs; c++) {
3018 struct nid_path *path;
3019 hda_nid_t adc = spec->adc_nids[c];
3020
3021 if (!is_reachable_path(codec, pin, adc))
3022 continue;
3023 path = snd_hda_add_new_path(codec, pin, adc, anchor);
3024 if (!path)
3025 continue;
3026 print_nid_path("input", path);
3027 spec->input_paths[imux_idx][c] =
3028 snd_hda_get_path_idx(codec, path);
3029
3030 if (!imux_added) {
Takashi Iwai967303d2013-02-19 17:12:42 +01003031 if (spec->hp_mic_pin == pin)
3032 spec->hp_mic_mux_idx = imux->num_items;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003033 spec->imux_pins[imux->num_items] = pin;
Takashi Iwai9dba2052013-01-18 10:01:15 +01003034 snd_hda_add_imux_item(imux, label, cfg_idx, NULL);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003035 imux_added = true;
3036 }
3037 }
3038
3039 return 0;
3040}
3041
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003043 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 */
Takashi Iwai9dba2052013-01-18 10:01:15 +01003045
Takashi Iwaic9700422013-01-18 10:17:30 +01003046/* fill the label for each input at first */
3047static int fill_input_pin_labels(struct hda_codec *codec)
3048{
3049 struct hda_gen_spec *spec = codec->spec;
3050 const struct auto_pin_cfg *cfg = &spec->autocfg;
3051 int i;
3052
3053 for (i = 0; i < cfg->num_inputs; i++) {
3054 hda_nid_t pin = cfg->inputs[i].pin;
3055 const char *label;
3056 int j, idx;
3057
3058 if (!is_input_pin(codec, pin))
3059 continue;
3060
3061 label = hda_get_autocfg_input_label(codec, cfg, i);
3062 idx = 0;
David Henningsson8e8db7f2013-01-18 15:43:02 +01003063 for (j = i - 1; j >= 0; j--) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003064 if (spec->input_labels[j] &&
3065 !strcmp(spec->input_labels[j], label)) {
3066 idx = spec->input_label_idxs[j] + 1;
3067 break;
3068 }
3069 }
3070
3071 spec->input_labels[i] = label;
3072 spec->input_label_idxs[i] = idx;
3073 }
3074
3075 return 0;
3076}
3077
Takashi Iwai9dba2052013-01-18 10:01:15 +01003078#define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
3079
Takashi Iwai352f7f92012-12-19 12:52:06 +01003080static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003081{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003082 struct hda_gen_spec *spec = codec->spec;
3083 const struct auto_pin_cfg *cfg = &spec->autocfg;
3084 hda_nid_t mixer = spec->mixer_nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003085 int num_adcs;
Takashi Iwaic9700422013-01-18 10:17:30 +01003086 int i, err;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003087 unsigned int val;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003088
Takashi Iwai352f7f92012-12-19 12:52:06 +01003089 num_adcs = fill_adc_nids(codec);
3090 if (num_adcs < 0)
3091 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003092
Takashi Iwaic9700422013-01-18 10:17:30 +01003093 err = fill_input_pin_labels(codec);
3094 if (err < 0)
3095 return err;
3096
Takashi Iwai352f7f92012-12-19 12:52:06 +01003097 for (i = 0; i < cfg->num_inputs; i++) {
3098 hda_nid_t pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099
Takashi Iwai352f7f92012-12-19 12:52:06 +01003100 pin = cfg->inputs[i].pin;
3101 if (!is_input_pin(codec, pin))
3102 continue;
3103
Takashi Iwai2c12c302013-01-10 09:33:29 +01003104 val = PIN_IN;
3105 if (cfg->inputs[i].type == AUTO_PIN_MIC)
3106 val |= snd_hda_get_default_vref(codec, pin);
Takashi Iwai93c9d8a2013-03-11 09:48:43 +01003107 if (pin != spec->hp_mic_pin)
3108 set_pin_target(codec, pin, val, false);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003109
Takashi Iwai352f7f92012-12-19 12:52:06 +01003110 if (mixer) {
3111 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01003112 err = new_analog_input(codec, i, pin,
Takashi Iwaic9700422013-01-18 10:17:30 +01003113 spec->input_labels[i],
3114 spec->input_label_idxs[i],
3115 mixer);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003116 if (err < 0)
3117 return err;
3118 }
3119 }
3120
Takashi Iwaic9700422013-01-18 10:17:30 +01003121 err = parse_capture_source(codec, pin, i, num_adcs,
3122 spec->input_labels[i], -mixer);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003123 if (err < 0)
3124 return err;
Takashi Iwai294765582013-01-17 09:52:11 +01003125
Takashi Iwaif811c3c2013-03-07 18:32:59 +01003126 if (spec->add_jack_modes) {
Takashi Iwai294765582013-01-17 09:52:11 +01003127 err = create_in_jack_mode(codec, pin);
3128 if (err < 0)
3129 return err;
3130 }
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003131 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003132
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003133 if (mixer && spec->add_stereo_mix_input) {
Takashi Iwai9dba2052013-01-18 10:01:15 +01003134 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003135 "Stereo Mix", 0);
3136 if (err < 0)
3137 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003138 }
3139
3140 return 0;
3141}
3142
3143
3144/*
3145 * input source mux
3146 */
3147
Takashi Iwaic697b712013-01-07 17:09:26 +01003148/* get the input path specified by the given adc and imux indices */
3149static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003150{
3151 struct hda_gen_spec *spec = codec->spec;
David Henningssonb56fa1e2013-01-16 11:45:35 +01003152 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3153 snd_BUG();
3154 return NULL;
3155 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003156 if (spec->dyn_adc_switch)
3157 adc_idx = spec->dyn_adc_idx[imux_idx];
David Henningssond3d982f2013-01-18 15:43:01 +01003158 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
David Henningssonb56fa1e2013-01-16 11:45:35 +01003159 snd_BUG();
3160 return NULL;
3161 }
Takashi Iwaic697b712013-01-07 17:09:26 +01003162 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003163}
3164
3165static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3166 unsigned int idx);
3167
3168static int mux_enum_info(struct snd_kcontrol *kcontrol,
3169 struct snd_ctl_elem_info *uinfo)
3170{
3171 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3172 struct hda_gen_spec *spec = codec->spec;
3173 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3174}
3175
3176static int mux_enum_get(struct snd_kcontrol *kcontrol,
3177 struct snd_ctl_elem_value *ucontrol)
3178{
3179 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3180 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003181 /* the ctls are created at once with multiple counts */
3182 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003183
3184 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3185 return 0;
3186}
3187
3188static int mux_enum_put(struct snd_kcontrol *kcontrol,
3189 struct snd_ctl_elem_value *ucontrol)
3190{
3191 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003192 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003193 return mux_select(codec, adc_idx,
3194 ucontrol->value.enumerated.item[0]);
3195}
3196
Takashi Iwai352f7f92012-12-19 12:52:06 +01003197static const struct snd_kcontrol_new cap_src_temp = {
3198 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3199 .name = "Input Source",
3200 .info = mux_enum_info,
3201 .get = mux_enum_get,
3202 .put = mux_enum_put,
3203};
3204
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003205/*
3206 * capture volume and capture switch ctls
3207 */
3208
Takashi Iwai352f7f92012-12-19 12:52:06 +01003209typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3210 struct snd_ctl_elem_value *ucontrol);
3211
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003212/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003213static int cap_put_caller(struct snd_kcontrol *kcontrol,
3214 struct snd_ctl_elem_value *ucontrol,
3215 put_call_t func, int type)
3216{
3217 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3218 struct hda_gen_spec *spec = codec->spec;
3219 const struct hda_input_mux *imux;
3220 struct nid_path *path;
3221 int i, adc_idx, err = 0;
3222
3223 imux = &spec->input_mux;
David Henningssona053d1e2013-01-16 11:45:36 +01003224 adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003225 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003226 /* we use the cache-only update at first since multiple input paths
3227 * may shared the same amp; by updating only caches, the redundant
3228 * writes to hardware can be reduced.
3229 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003230 codec->cached_write = 1;
3231 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003232 path = get_input_path(codec, adc_idx, i);
3233 if (!path || !path->ctls[type])
Takashi Iwai352f7f92012-12-19 12:52:06 +01003234 continue;
3235 kcontrol->private_value = path->ctls[type];
3236 err = func(kcontrol, ucontrol);
3237 if (err < 0)
3238 goto error;
3239 }
3240 error:
3241 codec->cached_write = 0;
3242 mutex_unlock(&codec->control_mutex);
Takashi Iwaidc870f32013-01-22 15:24:30 +01003243 snd_hda_codec_flush_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003244 if (err >= 0 && spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01003245 spec->cap_sync_hook(codec, ucontrol);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003246 return err;
3247}
3248
3249/* capture volume ctl callbacks */
3250#define cap_vol_info snd_hda_mixer_amp_volume_info
3251#define cap_vol_get snd_hda_mixer_amp_volume_get
3252#define cap_vol_tlv snd_hda_mixer_amp_tlv
3253
3254static int cap_vol_put(struct snd_kcontrol *kcontrol,
3255 struct snd_ctl_elem_value *ucontrol)
3256{
3257 return cap_put_caller(kcontrol, ucontrol,
3258 snd_hda_mixer_amp_volume_put,
3259 NID_PATH_VOL_CTL);
3260}
3261
3262static const struct snd_kcontrol_new cap_vol_temp = {
3263 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3264 .name = "Capture Volume",
3265 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3266 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3267 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3268 .info = cap_vol_info,
3269 .get = cap_vol_get,
3270 .put = cap_vol_put,
3271 .tlv = { .c = cap_vol_tlv },
3272};
3273
3274/* capture switch ctl callbacks */
3275#define cap_sw_info snd_ctl_boolean_stereo_info
3276#define cap_sw_get snd_hda_mixer_amp_switch_get
3277
3278static int cap_sw_put(struct snd_kcontrol *kcontrol,
3279 struct snd_ctl_elem_value *ucontrol)
3280{
Takashi Iwaia90229e2013-01-18 14:10:00 +01003281 return cap_put_caller(kcontrol, ucontrol,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003282 snd_hda_mixer_amp_switch_put,
3283 NID_PATH_MUTE_CTL);
3284}
3285
3286static const struct snd_kcontrol_new cap_sw_temp = {
3287 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3288 .name = "Capture Switch",
3289 .info = cap_sw_info,
3290 .get = cap_sw_get,
3291 .put = cap_sw_put,
3292};
3293
3294static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3295{
3296 hda_nid_t nid;
3297 int i, depth;
3298
3299 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3300 for (depth = 0; depth < 3; depth++) {
3301 if (depth >= path->depth)
3302 return -EINVAL;
3303 i = path->depth - depth - 1;
3304 nid = path->path[i];
3305 if (!path->ctls[NID_PATH_VOL_CTL]) {
3306 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3307 path->ctls[NID_PATH_VOL_CTL] =
3308 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3309 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3310 int idx = path->idx[i];
3311 if (!depth && codec->single_adc_amp)
3312 idx = 0;
3313 path->ctls[NID_PATH_VOL_CTL] =
3314 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3315 }
3316 }
3317 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3318 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3319 path->ctls[NID_PATH_MUTE_CTL] =
3320 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3321 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3322 int idx = path->idx[i];
3323 if (!depth && codec->single_adc_amp)
3324 idx = 0;
3325 path->ctls[NID_PATH_MUTE_CTL] =
3326 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3327 }
3328 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01003329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 return 0;
3331}
3332
Takashi Iwai352f7f92012-12-19 12:52:06 +01003333static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003335 struct hda_gen_spec *spec = codec->spec;
3336 struct auto_pin_cfg *cfg = &spec->autocfg;
3337 unsigned int val;
3338 int i;
3339
3340 if (!spec->inv_dmic_split)
3341 return false;
3342 for (i = 0; i < cfg->num_inputs; i++) {
3343 if (cfg->inputs[i].pin != nid)
3344 continue;
3345 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3346 return false;
3347 val = snd_hda_codec_get_pincfg(codec, nid);
3348 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3349 }
3350 return false;
3351}
3352
Takashi Iwaia90229e2013-01-18 14:10:00 +01003353/* capture switch put callback for a single control with hook call */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003354static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3355 struct snd_ctl_elem_value *ucontrol)
3356{
3357 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3358 struct hda_gen_spec *spec = codec->spec;
3359 int ret;
3360
3361 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3362 if (ret < 0)
3363 return ret;
3364
Takashi Iwaia90229e2013-01-18 14:10:00 +01003365 if (spec->cap_sync_hook)
3366 spec->cap_sync_hook(codec, ucontrol);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003367
3368 return ret;
3369}
3370
Takashi Iwai352f7f92012-12-19 12:52:06 +01003371static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3372 int idx, bool is_switch, unsigned int ctl,
3373 bool inv_dmic)
3374{
3375 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai975cc022013-06-28 11:56:49 +02003376 char tmpname[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003377 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3378 const char *sfx = is_switch ? "Switch" : "Volume";
3379 unsigned int chs = inv_dmic ? 1 : 3;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003380 struct snd_kcontrol_new *knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003381
3382 if (!ctl)
3383 return 0;
3384
3385 if (label)
3386 snprintf(tmpname, sizeof(tmpname),
3387 "%s Capture %s", label, sfx);
3388 else
3389 snprintf(tmpname, sizeof(tmpname),
3390 "Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003391 knew = add_control(spec, type, tmpname, idx,
3392 amp_val_replace_channels(ctl, chs));
3393 if (!knew)
3394 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003395 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003396 knew->put = cap_single_sw_put;
3397 if (!inv_dmic)
3398 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003399
3400 /* Make independent right kcontrol */
3401 if (label)
3402 snprintf(tmpname, sizeof(tmpname),
3403 "Inverted %s Capture %s", label, sfx);
3404 else
3405 snprintf(tmpname, sizeof(tmpname),
3406 "Inverted Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003407 knew = add_control(spec, type, tmpname, idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003408 amp_val_replace_channels(ctl, 2));
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003409 if (!knew)
3410 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003411 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003412 knew->put = cap_single_sw_put;
3413 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003414}
3415
3416/* create single (and simple) capture volume and switch controls */
3417static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3418 unsigned int vol_ctl, unsigned int sw_ctl,
3419 bool inv_dmic)
3420{
3421 int err;
3422 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3423 if (err < 0)
3424 return err;
3425 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3426 if (err < 0)
3427 return err;
3428 return 0;
3429}
3430
3431/* create bound capture volume and switch controls */
3432static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3433 unsigned int vol_ctl, unsigned int sw_ctl)
3434{
3435 struct hda_gen_spec *spec = codec->spec;
3436 struct snd_kcontrol_new *knew;
3437
3438 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003439 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003440 if (!knew)
3441 return -ENOMEM;
3442 knew->index = idx;
3443 knew->private_value = vol_ctl;
3444 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3445 }
3446 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003447 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003448 if (!knew)
3449 return -ENOMEM;
3450 knew->index = idx;
3451 knew->private_value = sw_ctl;
3452 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3453 }
3454 return 0;
3455}
3456
3457/* return the vol ctl when used first in the imux list */
3458static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3459{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003460 struct nid_path *path;
3461 unsigned int ctl;
3462 int i;
3463
Takashi Iwaic697b712013-01-07 17:09:26 +01003464 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003465 if (!path)
3466 return 0;
3467 ctl = path->ctls[type];
3468 if (!ctl)
3469 return 0;
3470 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003471 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003472 if (path && path->ctls[type] == ctl)
3473 return 0;
3474 }
3475 return ctl;
3476}
3477
3478/* create individual capture volume and switch controls per input */
3479static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3480{
3481 struct hda_gen_spec *spec = codec->spec;
3482 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaic9700422013-01-18 10:17:30 +01003483 int i, err, type;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003484
3485 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003486 bool inv_dmic;
Takashi Iwaic9700422013-01-18 10:17:30 +01003487 int idx;
Takashi Iwai9dba2052013-01-18 10:01:15 +01003488
Takashi Iwaic9700422013-01-18 10:17:30 +01003489 idx = imux->items[i].index;
3490 if (idx >= spec->autocfg.num_inputs)
Takashi Iwai9dba2052013-01-18 10:01:15 +01003491 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003492 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3493
3494 for (type = 0; type < 2; type++) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003495 err = add_single_cap_ctl(codec,
3496 spec->input_labels[idx],
3497 spec->input_label_idxs[idx],
3498 type,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003499 get_first_cap_ctl(codec, i, type),
3500 inv_dmic);
3501 if (err < 0)
3502 return err;
3503 }
3504 }
3505 return 0;
3506}
3507
3508static int create_capture_mixers(struct hda_codec *codec)
3509{
3510 struct hda_gen_spec *spec = codec->spec;
3511 struct hda_input_mux *imux = &spec->input_mux;
3512 int i, n, nums, err;
3513
3514 if (spec->dyn_adc_switch)
3515 nums = 1;
3516 else
3517 nums = spec->num_adc_nids;
3518
3519 if (!spec->auto_mic && imux->num_items > 1) {
3520 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01003521 const char *name;
3522 name = nums > 1 ? "Input Source" : "Capture Source";
3523 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003524 if (!knew)
3525 return -ENOMEM;
3526 knew->count = nums;
3527 }
3528
3529 for (n = 0; n < nums; n++) {
3530 bool multi = false;
David Henningsson99a55922013-01-16 15:58:44 +01003531 bool multi_cap_vol = spec->multi_cap_vol;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003532 bool inv_dmic = false;
3533 int vol, sw;
3534
3535 vol = sw = 0;
3536 for (i = 0; i < imux->num_items; i++) {
3537 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01003538 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003539 if (!path)
3540 continue;
3541 parse_capvol_in_path(codec, path);
3542 if (!vol)
3543 vol = path->ctls[NID_PATH_VOL_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003544 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003545 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003546 if (!same_amp_caps(codec, vol,
3547 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3548 multi_cap_vol = true;
3549 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003550 if (!sw)
3551 sw = path->ctls[NID_PATH_MUTE_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003552 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003553 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003554 if (!same_amp_caps(codec, sw,
3555 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3556 multi_cap_vol = true;
3557 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003558 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3559 inv_dmic = true;
3560 }
3561
3562 if (!multi)
3563 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3564 inv_dmic);
David Henningssonccb04152013-10-14 10:16:22 +02003565 else if (!multi_cap_vol && !inv_dmic)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003566 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3567 else
3568 err = create_multi_cap_vol_ctl(codec);
3569 if (err < 0)
3570 return err;
3571 }
3572
3573 return 0;
3574}
3575
3576/*
3577 * add mic boosts if needed
3578 */
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003579
3580/* check whether the given amp is feasible as a boost volume */
3581static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3582 int dir, int idx)
3583{
3584 unsigned int step;
3585
3586 if (!nid_has_volume(codec, nid, dir) ||
3587 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3588 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3589 return false;
3590
3591 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3592 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3593 if (step < 0x20)
3594 return false;
3595 return true;
3596}
3597
3598/* look for a boost amp in a widget close to the pin */
3599static unsigned int look_for_boost_amp(struct hda_codec *codec,
3600 struct nid_path *path)
3601{
3602 unsigned int val = 0;
3603 hda_nid_t nid;
3604 int depth;
3605
3606 for (depth = 0; depth < 3; depth++) {
3607 if (depth >= path->depth - 1)
3608 break;
3609 nid = path->path[depth];
3610 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3611 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3612 break;
3613 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3614 path->idx[depth])) {
3615 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3616 HDA_INPUT);
3617 break;
3618 }
3619 }
3620
3621 return val;
3622}
3623
Takashi Iwai352f7f92012-12-19 12:52:06 +01003624static int parse_mic_boost(struct hda_codec *codec)
3625{
3626 struct hda_gen_spec *spec = codec->spec;
3627 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003628 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003629 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003630
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003631 if (!spec->num_adc_nids)
3632 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003633
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003634 for (i = 0; i < imux->num_items; i++) {
3635 struct nid_path *path;
3636 unsigned int val;
3637 int idx;
Takashi Iwai975cc022013-06-28 11:56:49 +02003638 char boost_label[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
David Henningsson02aba552013-01-16 15:58:43 +01003639
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003640 idx = imux->items[i].index;
3641 if (idx >= imux->num_items)
3642 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003643
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003644 /* check only line-in and mic pins */
Takashi Iwai1799cdd52013-01-18 14:37:16 +01003645 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003646 continue;
3647
3648 path = get_input_path(codec, 0, i);
3649 if (!path)
3650 continue;
3651
3652 val = look_for_boost_amp(codec, path);
3653 if (!val)
3654 continue;
3655
3656 /* create a boost control */
3657 snprintf(boost_label, sizeof(boost_label),
3658 "%s Boost Volume", spec->input_labels[idx]);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003659 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3660 spec->input_label_idxs[idx], val))
3661 return -ENOMEM;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003662
3663 path->ctls[NID_PATH_BOOST_CTL] = val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003664 }
3665 return 0;
3666}
3667
3668/*
3669 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3670 */
3671static void parse_digital(struct hda_codec *codec)
3672{
3673 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003674 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003675 int i, nums;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003676 hda_nid_t dig_nid, pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003677
3678 /* support multiple SPDIFs; the secondary is set up as a slave */
3679 nums = 0;
3680 for (i = 0; i < spec->autocfg.dig_outs; i++) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003681 pin = spec->autocfg.dig_out_pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003682 dig_nid = look_for_dac(codec, pin, true);
3683 if (!dig_nid)
3684 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003685 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003686 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003687 continue;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003688 print_nid_path("digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01003689 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01003690 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003691 set_pin_target(codec, pin, PIN_OUT, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003692 if (!nums) {
3693 spec->multiout.dig_out_nid = dig_nid;
3694 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3695 } else {
3696 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3697 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
3698 break;
3699 spec->slave_dig_outs[nums - 1] = dig_nid;
3700 }
3701 nums++;
3702 }
3703
3704 if (spec->autocfg.dig_in_pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003705 pin = spec->autocfg.dig_in_pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003706 dig_nid = codec->start_nid;
3707 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003708 unsigned int wcaps = get_wcaps(codec, dig_nid);
3709 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3710 continue;
3711 if (!(wcaps & AC_WCAP_DIGITAL))
3712 continue;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003713 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003714 if (path) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003715 print_nid_path("digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003716 path->active = true;
3717 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003718 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003719 set_pin_target(codec, pin, PIN_IN, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003720 break;
3721 }
3722 }
3723 }
3724}
3725
3726
3727/*
3728 * input MUX handling
3729 */
3730
3731static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3732
3733/* select the given imux item; either unmute exclusively or select the route */
3734static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3735 unsigned int idx)
3736{
3737 struct hda_gen_spec *spec = codec->spec;
3738 const struct hda_input_mux *imux;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003739 struct nid_path *old_path, *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003740
3741 imux = &spec->input_mux;
3742 if (!imux->num_items)
3743 return 0;
3744
3745 if (idx >= imux->num_items)
3746 idx = imux->num_items - 1;
3747 if (spec->cur_mux[adc_idx] == idx)
3748 return 0;
3749
Takashi Iwai55196ff2013-01-24 17:32:56 +01003750 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3751 if (!old_path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003752 return 0;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003753 if (old_path->active)
3754 snd_hda_activate_path(codec, old_path, false, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003755
3756 spec->cur_mux[adc_idx] = idx;
3757
Takashi Iwai967303d2013-02-19 17:12:42 +01003758 if (spec->hp_mic)
3759 update_hp_mic(codec, adc_idx, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003760
3761 if (spec->dyn_adc_switch)
3762 dyn_adc_pcm_resetup(codec, idx);
3763
Takashi Iwaic697b712013-01-07 17:09:26 +01003764 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003765 if (!path)
3766 return 0;
3767 if (path->active)
3768 return 0;
3769 snd_hda_activate_path(codec, path, true, false);
3770 if (spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01003771 spec->cap_sync_hook(codec, NULL);
Takashi Iwai55196ff2013-01-24 17:32:56 +01003772 path_power_down_sync(codec, old_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003773 return 1;
3774}
3775
3776
3777/*
3778 * Jack detections for HP auto-mute and mic-switch
3779 */
3780
3781/* check each pin in the given array; returns true if any of them is plugged */
3782static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
3783{
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02003784 int i;
3785 bool present = false;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003786
3787 for (i = 0; i < num_pins; i++) {
3788 hda_nid_t nid = pins[i];
3789 if (!nid)
3790 break;
Takashi Iwai0b4df932013-01-10 09:45:13 +01003791 /* don't detect pins retasked as inputs */
3792 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
3793 continue;
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02003794 if (snd_hda_jack_detect_state(codec, nid) == HDA_JACK_PRESENT)
3795 present = true;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003796 }
3797 return present;
3798}
3799
3800/* standard HP/line-out auto-mute helper */
3801static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003802 int *paths, bool mute)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003803{
3804 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003805 int i;
3806
3807 for (i = 0; i < num_pins; i++) {
3808 hda_nid_t nid = pins[i];
Takashi Iwai967303d2013-02-19 17:12:42 +01003809 unsigned int val, oldval;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003810 if (!nid)
3811 break;
Takashi Iwai7eebffd2013-06-24 16:00:21 +02003812
3813 if (spec->auto_mute_via_amp) {
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003814 struct nid_path *path;
3815 hda_nid_t mute_nid;
3816
3817 path = snd_hda_get_path_from_idx(codec, paths[i]);
3818 if (!path)
3819 continue;
3820 mute_nid = get_amp_nid_(path->ctls[NID_PATH_MUTE_CTL]);
3821 if (!mute_nid)
3822 continue;
Takashi Iwai7eebffd2013-06-24 16:00:21 +02003823 if (mute)
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003824 spec->mute_bits |= (1ULL << mute_nid);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02003825 else
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003826 spec->mute_bits &= ~(1ULL << mute_nid);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02003827 set_pin_eapd(codec, nid, !mute);
3828 continue;
3829 }
3830
Takashi Iwai967303d2013-02-19 17:12:42 +01003831 oldval = snd_hda_codec_get_pin_target(codec, nid);
3832 if (oldval & PIN_IN)
3833 continue; /* no mute for inputs */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003834 /* don't reset VREF value in case it's controlling
3835 * the amp (see alc861_fixup_asus_amp_vref_0f())
3836 */
Takashi Iwai2c12c302013-01-10 09:33:29 +01003837 if (spec->keep_vref_in_automute)
Takashi Iwai967303d2013-02-19 17:12:42 +01003838 val = oldval & ~PIN_HP;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003839 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01003840 val = 0;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003841 if (!mute)
Takashi Iwai967303d2013-02-19 17:12:42 +01003842 val |= oldval;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003843 /* here we call update_pin_ctl() so that the pinctl is changed
3844 * without changing the pinctl target value;
3845 * the original target value will be still referred at the
3846 * init / resume again
3847 */
3848 update_pin_ctl(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01003849 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003850 }
3851}
3852
3853/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003854void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003855{
3856 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003857 int *paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003858 int on;
3859
3860 /* Control HP pins/amps depending on master_mute state;
3861 * in general, HP pins/amps control should be enabled in all cases,
3862 * but currently set only for master_mute, just to be safe
3863 */
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003864 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
3865 paths = spec->out_paths;
3866 else
3867 paths = spec->hp_paths;
Takashi Iwai967303d2013-02-19 17:12:42 +01003868 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003869 spec->autocfg.hp_pins, paths, spec->master_mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003870
3871 if (!spec->automute_speaker)
3872 on = 0;
3873 else
3874 on = spec->hp_jack_present | spec->line_jack_present;
3875 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003876 spec->speaker_muted = on;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003877 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3878 paths = spec->out_paths;
3879 else
3880 paths = spec->speaker_paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003881 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003882 spec->autocfg.speaker_pins, paths, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003883
3884 /* toggle line-out mutes if needed, too */
3885 /* if LO is a copy of either HP or Speaker, don't need to handle it */
3886 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
3887 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
3888 return;
3889 if (!spec->automute_lo)
3890 on = 0;
3891 else
3892 on = spec->hp_jack_present;
3893 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003894 spec->line_out_muted = on;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003895 paths = spec->out_paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003896 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003897 spec->autocfg.line_out_pins, paths, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003898}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003899EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003900
3901static void call_update_outputs(struct hda_codec *codec)
3902{
3903 struct hda_gen_spec *spec = codec->spec;
3904 if (spec->automute_hook)
3905 spec->automute_hook(codec);
3906 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01003907 snd_hda_gen_update_outputs(codec);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02003908
3909 /* sync the whole vmaster slaves to reflect the new auto-mute status */
3910 if (spec->auto_mute_via_amp && !codec->bus->shutdown)
3911 snd_ctl_sync_vmaster(spec->vmaster_mute.sw_kctl, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003912}
3913
3914/* standard HP-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003915void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003916{
3917 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai92603c52013-01-22 07:46:31 +01003918 hda_nid_t *pins = spec->autocfg.hp_pins;
3919 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003920
Takashi Iwai92603c52013-01-22 07:46:31 +01003921 /* No detection for the first HP jack during indep-HP mode */
3922 if (spec->indep_hp_enabled) {
3923 pins++;
3924 num_pins--;
3925 }
3926
3927 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003928 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
3929 return;
3930 call_update_outputs(codec);
3931}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003932EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003933
3934/* standard line-out-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003935void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003936{
3937 struct hda_gen_spec *spec = codec->spec;
3938
3939 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3940 return;
3941 /* check LO jack only when it's different from HP */
3942 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
3943 return;
3944
3945 spec->line_jack_present =
3946 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3947 spec->autocfg.line_out_pins);
3948 if (!spec->automute_speaker || !spec->detect_lo)
3949 return;
3950 call_update_outputs(codec);
3951}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003952EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003953
3954/* standard mic auto-switch helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003955void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003956{
3957 struct hda_gen_spec *spec = codec->spec;
3958 int i;
3959
3960 if (!spec->auto_mic)
3961 return;
3962
3963 for (i = spec->am_num_entries - 1; i > 0; i--) {
Takashi Iwai0b4df932013-01-10 09:45:13 +01003964 hda_nid_t pin = spec->am_entry[i].pin;
3965 /* don't detect pins retasked as outputs */
3966 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
3967 continue;
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02003968 if (snd_hda_jack_detect_state(codec, pin) == HDA_JACK_PRESENT) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003969 mux_select(codec, 0, spec->am_entry[i].idx);
3970 return;
3971 }
3972 }
3973 mux_select(codec, 0, spec->am_entry[0].idx);
3974}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003975EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003976
Takashi Iwai77afe0e2013-05-31 14:10:03 +02003977/* call appropriate hooks */
3978static void call_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
3979{
3980 struct hda_gen_spec *spec = codec->spec;
3981 if (spec->hp_automute_hook)
3982 spec->hp_automute_hook(codec, jack);
3983 else
3984 snd_hda_gen_hp_automute(codec, jack);
3985}
3986
3987static void call_line_automute(struct hda_codec *codec,
3988 struct hda_jack_tbl *jack)
3989{
3990 struct hda_gen_spec *spec = codec->spec;
3991 if (spec->line_automute_hook)
3992 spec->line_automute_hook(codec, jack);
3993 else
3994 snd_hda_gen_line_automute(codec, jack);
3995}
3996
3997static void call_mic_autoswitch(struct hda_codec *codec,
3998 struct hda_jack_tbl *jack)
3999{
4000 struct hda_gen_spec *spec = codec->spec;
4001 if (spec->mic_autoswitch_hook)
4002 spec->mic_autoswitch_hook(codec, jack);
4003 else
4004 snd_hda_gen_mic_autoswitch(codec, jack);
4005}
4006
Takashi Iwai963afde2013-05-31 15:20:31 +02004007/* update jack retasking */
4008static void update_automute_all(struct hda_codec *codec)
4009{
4010 call_hp_automute(codec, NULL);
4011 call_line_automute(codec, NULL);
4012 call_mic_autoswitch(codec, NULL);
4013}
4014
Takashi Iwai352f7f92012-12-19 12:52:06 +01004015/*
4016 * Auto-Mute mode mixer enum support
4017 */
4018static int automute_mode_info(struct snd_kcontrol *kcontrol,
4019 struct snd_ctl_elem_info *uinfo)
4020{
4021 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4022 struct hda_gen_spec *spec = codec->spec;
4023 static const char * const texts3[] = {
4024 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02004025 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026
Takashi Iwai352f7f92012-12-19 12:52:06 +01004027 if (spec->automute_speaker_possible && spec->automute_lo_possible)
4028 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
4029 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
4030}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031
Takashi Iwai352f7f92012-12-19 12:52:06 +01004032static int automute_mode_get(struct snd_kcontrol *kcontrol,
4033 struct snd_ctl_elem_value *ucontrol)
4034{
4035 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4036 struct hda_gen_spec *spec = codec->spec;
4037 unsigned int val = 0;
4038 if (spec->automute_speaker)
4039 val++;
4040 if (spec->automute_lo)
4041 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004042
Takashi Iwai352f7f92012-12-19 12:52:06 +01004043 ucontrol->value.enumerated.item[0] = val;
4044 return 0;
4045}
4046
4047static int automute_mode_put(struct snd_kcontrol *kcontrol,
4048 struct snd_ctl_elem_value *ucontrol)
4049{
4050 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4051 struct hda_gen_spec *spec = codec->spec;
4052
4053 switch (ucontrol->value.enumerated.item[0]) {
4054 case 0:
4055 if (!spec->automute_speaker && !spec->automute_lo)
4056 return 0;
4057 spec->automute_speaker = 0;
4058 spec->automute_lo = 0;
4059 break;
4060 case 1:
4061 if (spec->automute_speaker_possible) {
4062 if (!spec->automute_lo && spec->automute_speaker)
4063 return 0;
4064 spec->automute_speaker = 1;
4065 spec->automute_lo = 0;
4066 } else if (spec->automute_lo_possible) {
4067 if (spec->automute_lo)
4068 return 0;
4069 spec->automute_lo = 1;
4070 } else
4071 return -EINVAL;
4072 break;
4073 case 2:
4074 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
4075 return -EINVAL;
4076 if (spec->automute_speaker && spec->automute_lo)
4077 return 0;
4078 spec->automute_speaker = 1;
4079 spec->automute_lo = 1;
4080 break;
4081 default:
4082 return -EINVAL;
4083 }
4084 call_update_outputs(codec);
4085 return 1;
4086}
4087
4088static const struct snd_kcontrol_new automute_mode_enum = {
4089 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4090 .name = "Auto-Mute Mode",
4091 .info = automute_mode_info,
4092 .get = automute_mode_get,
4093 .put = automute_mode_put,
4094};
4095
4096static int add_automute_mode_enum(struct hda_codec *codec)
4097{
4098 struct hda_gen_spec *spec = codec->spec;
4099
Takashi Iwai12c93df2012-12-19 14:38:33 +01004100 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01004101 return -ENOMEM;
4102 return 0;
4103}
4104
4105/*
4106 * Check the availability of HP/line-out auto-mute;
4107 * Set up appropriately if really supported
4108 */
4109static int check_auto_mute_availability(struct hda_codec *codec)
4110{
4111 struct hda_gen_spec *spec = codec->spec;
4112 struct auto_pin_cfg *cfg = &spec->autocfg;
4113 int present = 0;
4114 int i, err;
4115
Takashi Iwaif72706b2013-01-16 18:20:07 +01004116 if (spec->suppress_auto_mute)
4117 return 0;
4118
Takashi Iwai352f7f92012-12-19 12:52:06 +01004119 if (cfg->hp_pins[0])
4120 present++;
4121 if (cfg->line_out_pins[0])
4122 present++;
4123 if (cfg->speaker_pins[0])
4124 present++;
4125 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02004126 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004127
4128 if (!cfg->speaker_pins[0] &&
4129 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
4130 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4131 sizeof(cfg->speaker_pins));
4132 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134
Takashi Iwai352f7f92012-12-19 12:52:06 +01004135 if (!cfg->hp_pins[0] &&
4136 cfg->line_out_type == AUTO_PIN_HP_OUT) {
4137 memcpy(cfg->hp_pins, cfg->line_out_pins,
4138 sizeof(cfg->hp_pins));
4139 cfg->hp_outs = cfg->line_outs;
4140 }
4141
4142 for (i = 0; i < cfg->hp_outs; i++) {
4143 hda_nid_t nid = cfg->hp_pins[i];
4144 if (!is_jack_detectable(codec, nid))
4145 continue;
4146 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
4147 nid);
4148 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004149 call_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004150 spec->detect_hp = 1;
4151 }
4152
4153 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
4154 if (cfg->speaker_outs)
4155 for (i = 0; i < cfg->line_outs; i++) {
4156 hda_nid_t nid = cfg->line_out_pins[i];
4157 if (!is_jack_detectable(codec, nid))
4158 continue;
4159 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
4160 snd_hda_jack_detect_enable_callback(codec, nid,
4161 HDA_GEN_FRONT_EVENT,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004162 call_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004163 spec->detect_lo = 1;
4164 }
4165 spec->automute_lo_possible = spec->detect_hp;
4166 }
4167
4168 spec->automute_speaker_possible = cfg->speaker_outs &&
4169 (spec->detect_hp || spec->detect_lo);
4170
4171 spec->automute_lo = spec->automute_lo_possible;
4172 spec->automute_speaker = spec->automute_speaker_possible;
4173
4174 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
4175 /* create a control for automute mode */
4176 err = add_automute_mode_enum(codec);
4177 if (err < 0)
4178 return err;
4179 }
4180 return 0;
4181}
4182
Takashi Iwai352f7f92012-12-19 12:52:06 +01004183/* check whether all auto-mic pins are valid; setup indices if OK */
4184static bool auto_mic_check_imux(struct hda_codec *codec)
4185{
4186 struct hda_gen_spec *spec = codec->spec;
4187 const struct hda_input_mux *imux;
4188 int i;
4189
4190 imux = &spec->input_mux;
4191 for (i = 0; i < spec->am_num_entries; i++) {
4192 spec->am_entry[i].idx =
4193 find_idx_in_nid_list(spec->am_entry[i].pin,
4194 spec->imux_pins, imux->num_items);
4195 if (spec->am_entry[i].idx < 0)
4196 return false; /* no corresponding imux */
4197 }
4198
4199 /* we don't need the jack detection for the first pin */
4200 for (i = 1; i < spec->am_num_entries; i++)
4201 snd_hda_jack_detect_enable_callback(codec,
4202 spec->am_entry[i].pin,
4203 HDA_GEN_MIC_EVENT,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004204 call_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004205 return true;
4206}
4207
4208static int compare_attr(const void *ap, const void *bp)
4209{
4210 const struct automic_entry *a = ap;
4211 const struct automic_entry *b = bp;
4212 return (int)(a->attr - b->attr);
4213}
4214
4215/*
4216 * Check the availability of auto-mic switch;
4217 * Set up if really supported
4218 */
4219static int check_auto_mic_availability(struct hda_codec *codec)
4220{
4221 struct hda_gen_spec *spec = codec->spec;
4222 struct auto_pin_cfg *cfg = &spec->autocfg;
4223 unsigned int types;
4224 int i, num_pins;
4225
Takashi Iwaid12daf62013-01-07 16:32:11 +01004226 if (spec->suppress_auto_mic)
4227 return 0;
4228
Takashi Iwai352f7f92012-12-19 12:52:06 +01004229 types = 0;
4230 num_pins = 0;
4231 for (i = 0; i < cfg->num_inputs; i++) {
4232 hda_nid_t nid = cfg->inputs[i].pin;
4233 unsigned int attr;
4234 attr = snd_hda_codec_get_pincfg(codec, nid);
4235 attr = snd_hda_get_input_pin_attr(attr);
4236 if (types & (1 << attr))
4237 return 0; /* already occupied */
4238 switch (attr) {
4239 case INPUT_PIN_ATTR_INT:
4240 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4241 return 0; /* invalid type */
4242 break;
4243 case INPUT_PIN_ATTR_UNUSED:
4244 return 0; /* invalid entry */
4245 default:
4246 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4247 return 0; /* invalid type */
4248 if (!spec->line_in_auto_switch &&
4249 cfg->inputs[i].type != AUTO_PIN_MIC)
4250 return 0; /* only mic is allowed */
4251 if (!is_jack_detectable(codec, nid))
4252 return 0; /* no unsol support */
4253 break;
4254 }
4255 if (num_pins >= MAX_AUTO_MIC_PINS)
4256 return 0;
4257 types |= (1 << attr);
4258 spec->am_entry[num_pins].pin = nid;
4259 spec->am_entry[num_pins].attr = attr;
4260 num_pins++;
4261 }
4262
4263 if (num_pins < 2)
4264 return 0;
4265
4266 spec->am_num_entries = num_pins;
4267 /* sort the am_entry in the order of attr so that the pin with a
4268 * higher attr will be selected when the jack is plugged.
4269 */
4270 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4271 compare_attr, NULL);
4272
4273 if (!auto_mic_check_imux(codec))
4274 return 0;
4275
4276 spec->auto_mic = 1;
4277 spec->num_adc_nids = 1;
4278 spec->cur_mux[0] = spec->am_entry[0].idx;
4279 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
4280 spec->am_entry[0].pin,
4281 spec->am_entry[1].pin,
4282 spec->am_entry[2].pin);
4283
4284 return 0;
4285}
4286
Takashi Iwai55196ff2013-01-24 17:32:56 +01004287/* power_filter hook; make inactive widgets into power down */
4288static unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
4289 hda_nid_t nid,
4290 unsigned int power_state)
4291{
4292 if (power_state != AC_PWRST_D0)
4293 return power_state;
4294 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4295 return power_state;
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +02004296 if (is_active_nid_for_any(codec, nid))
Takashi Iwai55196ff2013-01-24 17:32:56 +01004297 return power_state;
4298 return AC_PWRST_D3;
4299}
4300
Takashi Iwai352f7f92012-12-19 12:52:06 +01004301
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004302/*
4303 * Parse the given BIOS configuration and set up the hda_gen_spec
4304 *
4305 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004306 * or a negative error code
4307 */
4308int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004309 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004310{
4311 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004312 int err;
4313
Takashi Iwai1c70a582013-01-11 17:48:22 +01004314 parse_user_hints(codec);
4315
Takashi Iwaie4a395e2013-01-23 17:00:31 +01004316 if (spec->mixer_nid && !spec->mixer_merge_nid)
4317 spec->mixer_merge_nid = spec->mixer_nid;
4318
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004319 if (cfg != &spec->autocfg) {
4320 spec->autocfg = *cfg;
4321 cfg = &spec->autocfg;
4322 }
4323
Takashi Iwai98bd1112013-03-22 14:53:50 +01004324 if (!spec->main_out_badness)
4325 spec->main_out_badness = &hda_main_out_badness;
4326 if (!spec->extra_out_badness)
4327 spec->extra_out_badness = &hda_extra_out_badness;
4328
David Henningsson6fc4cb92013-01-16 15:58:45 +01004329 fill_all_dac_nids(codec);
4330
Takashi Iwai352f7f92012-12-19 12:52:06 +01004331 if (!cfg->line_outs) {
4332 if (cfg->dig_outs || cfg->dig_in_pin) {
4333 spec->multiout.max_channels = 2;
4334 spec->no_analog = 1;
4335 goto dig_only;
4336 }
Takashi Iwaic9e4bdb2013-12-06 09:30:14 +01004337 if (!cfg->num_inputs && !cfg->dig_in_pin)
4338 return 0; /* can't find valid BIOS pin config */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004339 }
4340
4341 if (!spec->no_primary_hp &&
4342 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
4343 cfg->line_outs <= cfg->hp_outs) {
4344 /* use HP as primary out */
4345 cfg->speaker_outs = cfg->line_outs;
4346 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4347 sizeof(cfg->speaker_pins));
4348 cfg->line_outs = cfg->hp_outs;
4349 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4350 cfg->hp_outs = 0;
4351 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4352 cfg->line_out_type = AUTO_PIN_HP_OUT;
4353 }
4354
4355 err = parse_output_paths(codec);
4356 if (err < 0)
4357 return err;
4358 err = create_multi_channel_mode(codec);
4359 if (err < 0)
4360 return err;
4361 err = create_multi_out_ctls(codec, cfg);
4362 if (err < 0)
4363 return err;
4364 err = create_hp_out_ctls(codec);
4365 if (err < 0)
4366 return err;
4367 err = create_speaker_out_ctls(codec);
4368 if (err < 0)
4369 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004370 err = create_indep_hp_ctls(codec);
4371 if (err < 0)
4372 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01004373 err = create_loopback_mixing_ctl(codec);
4374 if (err < 0)
4375 return err;
Takashi Iwai967303d2013-02-19 17:12:42 +01004376 err = create_hp_mic(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004377 if (err < 0)
4378 return err;
4379 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02004380 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02004381 return err;
4382
Takashi Iwaia07a9492013-01-07 16:44:06 +01004383 spec->const_channel_count = spec->ext_channel_count;
4384 /* check the multiple speaker and headphone pins */
4385 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4386 spec->const_channel_count = max(spec->const_channel_count,
4387 cfg->speaker_outs * 2);
4388 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4389 spec->const_channel_count = max(spec->const_channel_count,
4390 cfg->hp_outs * 2);
4391 spec->multiout.max_channels = max(spec->ext_channel_count,
4392 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004393
4394 err = check_auto_mute_availability(codec);
4395 if (err < 0)
4396 return err;
4397
4398 err = check_dyn_adc_switch(codec);
4399 if (err < 0)
4400 return err;
4401
Takashi Iwai967303d2013-02-19 17:12:42 +01004402 err = check_auto_mic_availability(codec);
4403 if (err < 0)
4404 return err;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004405
Takashi Iwai352f7f92012-12-19 12:52:06 +01004406 err = create_capture_mixers(codec);
4407 if (err < 0)
4408 return err;
4409
4410 err = parse_mic_boost(codec);
4411 if (err < 0)
4412 return err;
4413
Takashi Iwaiced4cef2013-11-26 08:33:45 +01004414 /* create "Headphone Mic Jack Mode" if no input selection is
4415 * available (or user specifies add_jack_modes hint)
4416 */
4417 if (spec->hp_mic_pin &&
4418 (spec->auto_mic || spec->input_mux.num_items == 1 ||
4419 spec->add_jack_modes)) {
4420 err = create_hp_mic_jack_mode(codec, spec->hp_mic_pin);
4421 if (err < 0)
4422 return err;
4423 }
4424
Takashi Iwaif811c3c2013-03-07 18:32:59 +01004425 if (spec->add_jack_modes) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01004426 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4427 err = create_out_jack_modes(codec, cfg->line_outs,
4428 cfg->line_out_pins);
4429 if (err < 0)
4430 return err;
4431 }
4432 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4433 err = create_out_jack_modes(codec, cfg->hp_outs,
4434 cfg->hp_pins);
4435 if (err < 0)
4436 return err;
4437 }
4438 }
4439
Takashi Iwai352f7f92012-12-19 12:52:06 +01004440 dig_only:
4441 parse_digital(codec);
4442
Takashi Iwai55196ff2013-01-24 17:32:56 +01004443 if (spec->power_down_unused)
4444 codec->power_filter = snd_hda_gen_path_power_filter;
4445
Takashi Iwai7504b6c2013-03-18 11:25:51 +01004446 if (!spec->no_analog && spec->beep_nid) {
4447 err = snd_hda_attach_beep_device(codec, spec->beep_nid);
4448 if (err < 0)
4449 return err;
4450 }
4451
Takashi Iwai352f7f92012-12-19 12:52:06 +01004452 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004454EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455
4456
4457/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004458 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004460
4461/* slave controls for virtual master */
4462static const char * const slave_pfxs[] = {
4463 "Front", "Surround", "Center", "LFE", "Side",
4464 "Headphone", "Speaker", "Mono", "Line Out",
4465 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01004466 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
4467 "Headphone Front", "Headphone Surround", "Headphone CLFE",
4468 "Headphone Side",
Takashi Iwai352f7f92012-12-19 12:52:06 +01004469 NULL,
4470};
4471
4472int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004474 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476
Takashi Iwai36502d02012-12-19 15:15:10 +01004477 if (spec->kctls.used) {
4478 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
4479 if (err < 0)
4480 return err;
4481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482
Takashi Iwai352f7f92012-12-19 12:52:06 +01004483 if (spec->multiout.dig_out_nid) {
4484 err = snd_hda_create_dig_out_ctls(codec,
4485 spec->multiout.dig_out_nid,
4486 spec->multiout.dig_out_nid,
4487 spec->pcm_rec[1].pcm_type);
4488 if (err < 0)
4489 return err;
4490 if (!spec->no_analog) {
4491 err = snd_hda_create_spdif_share_sw(codec,
4492 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 if (err < 0)
4494 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004495 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 }
4497 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004498 if (spec->dig_in_nid) {
4499 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
4500 if (err < 0)
4501 return err;
4502 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503
Takashi Iwai352f7f92012-12-19 12:52:06 +01004504 /* if we have no master control, let's create it */
4505 if (!spec->no_analog &&
4506 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004507 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01004508 spec->vmaster_tlv, slave_pfxs,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004509 "Playback Volume");
4510 if (err < 0)
4511 return err;
4512 }
4513 if (!spec->no_analog &&
4514 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
4515 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
4516 NULL, slave_pfxs,
4517 "Playback Switch",
4518 true, &spec->vmaster_mute.sw_kctl);
4519 if (err < 0)
4520 return err;
Takashi Iwaib63eae02013-10-25 23:43:10 +02004521 if (spec->vmaster_mute.hook) {
Takashi Iwaifd25a972012-12-20 14:57:18 +01004522 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
4523 spec->vmaster_mute_enum);
Takashi Iwaib63eae02013-10-25 23:43:10 +02004524 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
4525 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527
Takashi Iwai352f7f92012-12-19 12:52:06 +01004528 free_kctls(spec); /* no longer needed */
4529
Takashi Iwai352f7f92012-12-19 12:52:06 +01004530 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
4531 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 return err;
4533
4534 return 0;
4535}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004536EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
4537
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538
4539/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004540 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004543static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
4544 struct hda_codec *codec,
4545 struct snd_pcm_substream *substream,
4546 int action)
4547{
4548 struct hda_gen_spec *spec = codec->spec;
4549 if (spec->pcm_playback_hook)
4550 spec->pcm_playback_hook(hinfo, codec, substream, action);
4551}
4552
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004553static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
4554 struct hda_codec *codec,
4555 struct snd_pcm_substream *substream,
4556 int action)
4557{
4558 struct hda_gen_spec *spec = codec->spec;
4559 if (spec->pcm_capture_hook)
4560 spec->pcm_capture_hook(hinfo, codec, substream, action);
4561}
4562
Takashi Iwai352f7f92012-12-19 12:52:06 +01004563/*
4564 * Analog playback callbacks
4565 */
4566static int playback_pcm_open(struct hda_pcm_stream *hinfo,
4567 struct hda_codec *codec,
4568 struct snd_pcm_substream *substream)
4569{
4570 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004571 int err;
4572
4573 mutex_lock(&spec->pcm_mutex);
4574 err = snd_hda_multi_out_analog_open(codec,
4575 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004576 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004577 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004578 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004579 call_pcm_playback_hook(hinfo, codec, substream,
4580 HDA_GEN_PCM_ACT_OPEN);
4581 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004582 mutex_unlock(&spec->pcm_mutex);
4583 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004584}
4585
4586static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01004587 struct hda_codec *codec,
4588 unsigned int stream_tag,
4589 unsigned int format,
4590 struct snd_pcm_substream *substream)
4591{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004592 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004593 int err;
4594
4595 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
4596 stream_tag, format, substream);
4597 if (!err)
4598 call_pcm_playback_hook(hinfo, codec, substream,
4599 HDA_GEN_PCM_ACT_PREPARE);
4600 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004601}
Takashi Iwai97ec5582006-03-21 11:29:07 +01004602
Takashi Iwai352f7f92012-12-19 12:52:06 +01004603static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4604 struct hda_codec *codec,
4605 struct snd_pcm_substream *substream)
4606{
4607 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004608 int err;
4609
4610 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
4611 if (!err)
4612 call_pcm_playback_hook(hinfo, codec, substream,
4613 HDA_GEN_PCM_ACT_CLEANUP);
4614 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004615}
4616
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004617static int playback_pcm_close(struct hda_pcm_stream *hinfo,
4618 struct hda_codec *codec,
4619 struct snd_pcm_substream *substream)
4620{
4621 struct hda_gen_spec *spec = codec->spec;
4622 mutex_lock(&spec->pcm_mutex);
4623 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004624 call_pcm_playback_hook(hinfo, codec, substream,
4625 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004626 mutex_unlock(&spec->pcm_mutex);
4627 return 0;
4628}
4629
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004630static int capture_pcm_open(struct hda_pcm_stream *hinfo,
4631 struct hda_codec *codec,
4632 struct snd_pcm_substream *substream)
4633{
4634 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
4635 return 0;
4636}
4637
4638static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4639 struct hda_codec *codec,
4640 unsigned int stream_tag,
4641 unsigned int format,
4642 struct snd_pcm_substream *substream)
4643{
4644 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4645 call_pcm_capture_hook(hinfo, codec, substream,
4646 HDA_GEN_PCM_ACT_PREPARE);
4647 return 0;
4648}
4649
4650static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4651 struct hda_codec *codec,
4652 struct snd_pcm_substream *substream)
4653{
4654 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4655 call_pcm_capture_hook(hinfo, codec, substream,
4656 HDA_GEN_PCM_ACT_CLEANUP);
4657 return 0;
4658}
4659
4660static int capture_pcm_close(struct hda_pcm_stream *hinfo,
4661 struct hda_codec *codec,
4662 struct snd_pcm_substream *substream)
4663{
4664 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
4665 return 0;
4666}
4667
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004668static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
4669 struct hda_codec *codec,
4670 struct snd_pcm_substream *substream)
4671{
4672 struct hda_gen_spec *spec = codec->spec;
4673 int err = 0;
4674
4675 mutex_lock(&spec->pcm_mutex);
4676 if (!spec->indep_hp_enabled)
4677 err = -EBUSY;
4678 else
4679 spec->active_streams |= 1 << STREAM_INDEP_HP;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004680 call_pcm_playback_hook(hinfo, codec, substream,
4681 HDA_GEN_PCM_ACT_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004682 mutex_unlock(&spec->pcm_mutex);
4683 return err;
4684}
4685
4686static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
4687 struct hda_codec *codec,
4688 struct snd_pcm_substream *substream)
4689{
4690 struct hda_gen_spec *spec = codec->spec;
4691 mutex_lock(&spec->pcm_mutex);
4692 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004693 call_pcm_playback_hook(hinfo, codec, substream,
4694 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004695 mutex_unlock(&spec->pcm_mutex);
4696 return 0;
4697}
4698
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004699static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4700 struct hda_codec *codec,
4701 unsigned int stream_tag,
4702 unsigned int format,
4703 struct snd_pcm_substream *substream)
4704{
4705 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4706 call_pcm_playback_hook(hinfo, codec, substream,
4707 HDA_GEN_PCM_ACT_PREPARE);
4708 return 0;
4709}
4710
4711static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4712 struct hda_codec *codec,
4713 struct snd_pcm_substream *substream)
4714{
4715 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4716 call_pcm_playback_hook(hinfo, codec, substream,
4717 HDA_GEN_PCM_ACT_CLEANUP);
4718 return 0;
4719}
4720
Takashi Iwai352f7f92012-12-19 12:52:06 +01004721/*
4722 * Digital out
4723 */
4724static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
4725 struct hda_codec *codec,
4726 struct snd_pcm_substream *substream)
4727{
4728 struct hda_gen_spec *spec = codec->spec;
4729 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
4730}
4731
4732static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4733 struct hda_codec *codec,
4734 unsigned int stream_tag,
4735 unsigned int format,
4736 struct snd_pcm_substream *substream)
4737{
4738 struct hda_gen_spec *spec = codec->spec;
4739 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
4740 stream_tag, format, substream);
4741}
4742
4743static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4744 struct hda_codec *codec,
4745 struct snd_pcm_substream *substream)
4746{
4747 struct hda_gen_spec *spec = codec->spec;
4748 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
4749}
4750
4751static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
4752 struct hda_codec *codec,
4753 struct snd_pcm_substream *substream)
4754{
4755 struct hda_gen_spec *spec = codec->spec;
4756 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
4757}
4758
4759/*
4760 * Analog capture
4761 */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004762#define alt_capture_pcm_open capture_pcm_open
4763#define alt_capture_pcm_close capture_pcm_close
4764
Takashi Iwai352f7f92012-12-19 12:52:06 +01004765static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4766 struct hda_codec *codec,
4767 unsigned int stream_tag,
4768 unsigned int format,
4769 struct snd_pcm_substream *substream)
4770{
4771 struct hda_gen_spec *spec = codec->spec;
4772
4773 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01004774 stream_tag, 0, format);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004775 call_pcm_capture_hook(hinfo, codec, substream,
4776 HDA_GEN_PCM_ACT_PREPARE);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004777 return 0;
4778}
4779
Takashi Iwai352f7f92012-12-19 12:52:06 +01004780static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4781 struct hda_codec *codec,
4782 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01004783{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004784 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01004785
Takashi Iwai352f7f92012-12-19 12:52:06 +01004786 snd_hda_codec_cleanup_stream(codec,
4787 spec->adc_nids[substream->number + 1]);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004788 call_pcm_capture_hook(hinfo, codec, substream,
4789 HDA_GEN_PCM_ACT_CLEANUP);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004790 return 0;
4791}
4792
Takashi Iwai352f7f92012-12-19 12:52:06 +01004793/*
4794 */
4795static const struct hda_pcm_stream pcm_analog_playback = {
4796 .substreams = 1,
4797 .channels_min = 2,
4798 .channels_max = 8,
4799 /* NID is set in build_pcms */
4800 .ops = {
4801 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004802 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004803 .prepare = playback_pcm_prepare,
4804 .cleanup = playback_pcm_cleanup
4805 },
4806};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807
Takashi Iwai352f7f92012-12-19 12:52:06 +01004808static const struct hda_pcm_stream pcm_analog_capture = {
4809 .substreams = 1,
4810 .channels_min = 2,
4811 .channels_max = 2,
4812 /* NID is set in build_pcms */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004813 .ops = {
4814 .open = capture_pcm_open,
4815 .close = capture_pcm_close,
4816 .prepare = capture_pcm_prepare,
4817 .cleanup = capture_pcm_cleanup
4818 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004819};
4820
4821static const struct hda_pcm_stream pcm_analog_alt_playback = {
4822 .substreams = 1,
4823 .channels_min = 2,
4824 .channels_max = 2,
4825 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004826 .ops = {
4827 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004828 .close = alt_playback_pcm_close,
4829 .prepare = alt_playback_pcm_prepare,
4830 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004831 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004832};
4833
4834static const struct hda_pcm_stream pcm_analog_alt_capture = {
4835 .substreams = 2, /* can be overridden */
4836 .channels_min = 2,
4837 .channels_max = 2,
4838 /* NID is set in build_pcms */
4839 .ops = {
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004840 .open = alt_capture_pcm_open,
4841 .close = alt_capture_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004842 .prepare = alt_capture_pcm_prepare,
4843 .cleanup = alt_capture_pcm_cleanup
4844 },
4845};
4846
4847static const struct hda_pcm_stream pcm_digital_playback = {
4848 .substreams = 1,
4849 .channels_min = 2,
4850 .channels_max = 2,
4851 /* NID is set in build_pcms */
4852 .ops = {
4853 .open = dig_playback_pcm_open,
4854 .close = dig_playback_pcm_close,
4855 .prepare = dig_playback_pcm_prepare,
4856 .cleanup = dig_playback_pcm_cleanup
4857 },
4858};
4859
4860static const struct hda_pcm_stream pcm_digital_capture = {
4861 .substreams = 1,
4862 .channels_min = 2,
4863 .channels_max = 2,
4864 /* NID is set in build_pcms */
4865};
4866
4867/* Used by build_pcms to flag that a PCM has no playback stream */
4868static const struct hda_pcm_stream pcm_null_stream = {
4869 .substreams = 0,
4870 .channels_min = 0,
4871 .channels_max = 0,
4872};
4873
4874/*
4875 * dynamic changing ADC PCM streams
4876 */
4877static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
4878{
4879 struct hda_gen_spec *spec = codec->spec;
4880 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
4881
4882 if (spec->cur_adc && spec->cur_adc != new_adc) {
4883 /* stream is running, let's swap the current ADC */
4884 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
4885 spec->cur_adc = new_adc;
4886 snd_hda_codec_setup_stream(codec, new_adc,
4887 spec->cur_adc_stream_tag, 0,
4888 spec->cur_adc_format);
4889 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004891 return false;
4892}
4893
4894/* analog capture with dynamic dual-adc changes */
4895static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4896 struct hda_codec *codec,
4897 unsigned int stream_tag,
4898 unsigned int format,
4899 struct snd_pcm_substream *substream)
4900{
4901 struct hda_gen_spec *spec = codec->spec;
4902 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
4903 spec->cur_adc_stream_tag = stream_tag;
4904 spec->cur_adc_format = format;
4905 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
4906 return 0;
4907}
4908
4909static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4910 struct hda_codec *codec,
4911 struct snd_pcm_substream *substream)
4912{
4913 struct hda_gen_spec *spec = codec->spec;
4914 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
4915 spec->cur_adc = 0;
4916 return 0;
4917}
4918
4919static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
4920 .substreams = 1,
4921 .channels_min = 2,
4922 .channels_max = 2,
4923 .nid = 0, /* fill later */
4924 .ops = {
4925 .prepare = dyn_adc_capture_pcm_prepare,
4926 .cleanup = dyn_adc_capture_pcm_cleanup
4927 },
4928};
4929
Takashi Iwaif873e532012-12-20 16:58:39 +01004930static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
4931 const char *chip_name)
4932{
4933 char *p;
4934
4935 if (*str)
4936 return;
4937 strlcpy(str, chip_name, len);
4938
4939 /* drop non-alnum chars after a space */
4940 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
4941 if (!isalnum(p[1])) {
4942 *p = 0;
4943 break;
4944 }
4945 }
4946 strlcat(str, sfx, len);
4947}
4948
Takashi Iwai352f7f92012-12-19 12:52:06 +01004949/* build PCM streams based on the parsed results */
4950int snd_hda_gen_build_pcms(struct hda_codec *codec)
4951{
4952 struct hda_gen_spec *spec = codec->spec;
4953 struct hda_pcm *info = spec->pcm_rec;
4954 const struct hda_pcm_stream *p;
4955 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956
4957 codec->num_pcms = 1;
4958 codec->pcm_info = info;
4959
Takashi Iwai352f7f92012-12-19 12:52:06 +01004960 if (spec->no_analog)
4961 goto skip_analog;
4962
Takashi Iwaif873e532012-12-20 16:58:39 +01004963 fill_pcm_stream_name(spec->stream_name_analog,
4964 sizeof(spec->stream_name_analog),
4965 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004966 info->name = spec->stream_name_analog;
4967
4968 if (spec->multiout.num_dacs > 0) {
4969 p = spec->stream_analog_playback;
4970 if (!p)
4971 p = &pcm_analog_playback;
4972 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4973 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
4974 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
4975 spec->multiout.max_channels;
4976 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
4977 spec->autocfg.line_outs == 2)
4978 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
4979 snd_pcm_2_1_chmaps;
4980 }
4981 if (spec->num_adc_nids) {
4982 p = spec->stream_analog_capture;
4983 if (!p) {
4984 if (spec->dyn_adc_switch)
4985 p = &dyn_adc_pcm_analog_capture;
4986 else
4987 p = &pcm_analog_capture;
4988 }
4989 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4990 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
4991 }
4992
Takashi Iwai352f7f92012-12-19 12:52:06 +01004993 skip_analog:
4994 /* SPDIF for stream index #1 */
4995 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01004996 fill_pcm_stream_name(spec->stream_name_digital,
4997 sizeof(spec->stream_name_digital),
4998 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004999 codec->num_pcms = 2;
5000 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
5001 info = spec->pcm_rec + 1;
5002 info->name = spec->stream_name_digital;
5003 if (spec->dig_out_type)
5004 info->pcm_type = spec->dig_out_type;
5005 else
5006 info->pcm_type = HDA_PCM_TYPE_SPDIF;
5007 if (spec->multiout.dig_out_nid) {
5008 p = spec->stream_digital_playback;
5009 if (!p)
5010 p = &pcm_digital_playback;
5011 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
5012 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
5013 }
5014 if (spec->dig_in_nid) {
5015 p = spec->stream_digital_capture;
5016 if (!p)
5017 p = &pcm_digital_capture;
5018 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
5019 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
5020 }
5021 }
5022
5023 if (spec->no_analog)
5024 return 0;
5025
5026 /* If the use of more than one ADC is requested for the current
5027 * model, configure a second analog capture-only PCM.
5028 */
5029 have_multi_adcs = (spec->num_adc_nids > 1) &&
5030 !spec->dyn_adc_switch && !spec->auto_mic;
5031 /* Additional Analaog capture for index #2 */
5032 if (spec->alt_dac_nid || have_multi_adcs) {
Takashi Iwaia6071482013-01-21 16:50:09 +01005033 fill_pcm_stream_name(spec->stream_name_alt_analog,
5034 sizeof(spec->stream_name_alt_analog),
5035 " Alt Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005036 codec->num_pcms = 3;
5037 info = spec->pcm_rec + 2;
Takashi Iwaia6071482013-01-21 16:50:09 +01005038 info->name = spec->stream_name_alt_analog;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005039 if (spec->alt_dac_nid) {
5040 p = spec->stream_analog_alt_playback;
5041 if (!p)
5042 p = &pcm_analog_alt_playback;
5043 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
5044 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
5045 spec->alt_dac_nid;
5046 } else {
5047 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
5048 pcm_null_stream;
5049 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
5050 }
5051 if (have_multi_adcs) {
5052 p = spec->stream_analog_alt_capture;
5053 if (!p)
5054 p = &pcm_analog_alt_capture;
5055 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
5056 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
5057 spec->adc_nids[1];
5058 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
5059 spec->num_adc_nids - 1;
5060 } else {
5061 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
5062 pcm_null_stream;
5063 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
5064 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065 }
5066
5067 return 0;
5068}
Takashi Iwai352f7f92012-12-19 12:52:06 +01005069EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
5070
5071
5072/*
5073 * Standard auto-parser initializations
5074 */
5075
Takashi Iwaid4156932013-01-07 10:08:02 +01005076/* configure the given path as a proper output */
Takashi Iwai2c12c302013-01-10 09:33:29 +01005077static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005078{
5079 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01005080 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005081
Takashi Iwai196c17662013-01-04 15:01:40 +01005082 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01005083 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005084 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01005085 pin = path->path[path->depth - 1];
Takashi Iwai2c12c302013-01-10 09:33:29 +01005086 restore_pin_ctl(codec, pin);
Takashi Iwai65033cc2013-04-16 12:31:05 +02005087 snd_hda_activate_path(codec, path, path->active,
5088 aamix_default(codec->spec));
Takashi Iwaie1284af2013-01-03 16:33:02 +01005089 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005090}
5091
5092/* initialize primary output paths */
5093static void init_multi_out(struct hda_codec *codec)
5094{
5095 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005096 int i;
5097
Takashi Iwaid4156932013-01-07 10:08:02 +01005098 for (i = 0; i < spec->autocfg.line_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005099 set_output_and_unmute(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005100}
5101
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005102
Takashi Iwai2c12c302013-01-10 09:33:29 +01005103static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005104{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005105 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005106
Takashi Iwaid4156932013-01-07 10:08:02 +01005107 for (i = 0; i < num_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005108 set_output_and_unmute(codec, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005109}
5110
5111/* initialize hp and speaker paths */
5112static void init_extra_out(struct hda_codec *codec)
5113{
5114 struct hda_gen_spec *spec = codec->spec;
5115
5116 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005117 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005118 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
5119 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai2c12c302013-01-10 09:33:29 +01005120 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005121}
5122
5123/* initialize multi-io paths */
5124static void init_multi_io(struct hda_codec *codec)
5125{
5126 struct hda_gen_spec *spec = codec->spec;
5127 int i;
5128
5129 for (i = 0; i < spec->multi_ios; i++) {
5130 hda_nid_t pin = spec->multi_io[i].pin;
5131 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01005132 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005133 if (!path)
5134 continue;
5135 if (!spec->multi_io[i].ctl_in)
5136 spec->multi_io[i].ctl_in =
Takashi Iwai2c12c302013-01-10 09:33:29 +01005137 snd_hda_codec_get_pin_target(codec, pin);
Takashi Iwai65033cc2013-04-16 12:31:05 +02005138 snd_hda_activate_path(codec, path, path->active,
5139 aamix_default(spec));
Takashi Iwai352f7f92012-12-19 12:52:06 +01005140 }
5141}
5142
Takashi Iwai352f7f92012-12-19 12:52:06 +01005143/* set up input pins and loopback paths */
5144static void init_analog_input(struct hda_codec *codec)
5145{
5146 struct hda_gen_spec *spec = codec->spec;
5147 struct auto_pin_cfg *cfg = &spec->autocfg;
5148 int i;
5149
5150 for (i = 0; i < cfg->num_inputs; i++) {
5151 hda_nid_t nid = cfg->inputs[i].pin;
5152 if (is_input_pin(codec, nid))
Takashi Iwai2c12c302013-01-10 09:33:29 +01005153 restore_pin_ctl(codec, nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005154
5155 /* init loopback inputs */
5156 if (spec->mixer_nid) {
Takashi Iwai3e367f12013-01-23 17:07:23 +01005157 resume_path_from_idx(codec, spec->loopback_paths[i]);
5158 resume_path_from_idx(codec, spec->loopback_merge_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005159 }
5160 }
5161}
5162
5163/* initialize ADC paths */
5164static void init_input_src(struct hda_codec *codec)
5165{
5166 struct hda_gen_spec *spec = codec->spec;
5167 struct hda_input_mux *imux = &spec->input_mux;
5168 struct nid_path *path;
5169 int i, c, nums;
5170
5171 if (spec->dyn_adc_switch)
5172 nums = 1;
5173 else
5174 nums = spec->num_adc_nids;
5175
5176 for (c = 0; c < nums; c++) {
5177 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01005178 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005179 if (path) {
5180 bool active = path->active;
5181 if (i == spec->cur_mux[c])
5182 active = true;
5183 snd_hda_activate_path(codec, path, active, false);
5184 }
5185 }
Takashi Iwai967303d2013-02-19 17:12:42 +01005186 if (spec->hp_mic)
5187 update_hp_mic(codec, c, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005188 }
5189
Takashi Iwai352f7f92012-12-19 12:52:06 +01005190 if (spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01005191 spec->cap_sync_hook(codec, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005192}
5193
5194/* set right pin controls for digital I/O */
5195static void init_digital(struct hda_codec *codec)
5196{
5197 struct hda_gen_spec *spec = codec->spec;
5198 int i;
5199 hda_nid_t pin;
5200
Takashi Iwaid4156932013-01-07 10:08:02 +01005201 for (i = 0; i < spec->autocfg.dig_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005202 set_output_and_unmute(codec, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005203 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005204 if (pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01005205 restore_pin_ctl(codec, pin);
Takashi Iwai3e367f12013-01-23 17:07:23 +01005206 resume_path_from_idx(codec, spec->digin_path);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005207 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005208}
5209
Takashi Iwai973e4972012-12-20 15:16:09 +01005210/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5211 * invalid unsol tags by some reason
5212 */
5213static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5214{
5215 int i;
5216
5217 for (i = 0; i < codec->init_pins.used; i++) {
5218 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
5219 hda_nid_t nid = pin->nid;
5220 if (is_jack_detectable(codec, nid) &&
5221 !snd_hda_jack_tbl_get(codec, nid))
5222 snd_hda_codec_update_cache(codec, nid, 0,
5223 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5224 }
5225}
5226
Takashi Iwai5187ac12013-01-07 12:52:16 +01005227/*
5228 * initialize the generic spec;
5229 * this can be put as patch_ops.init function
5230 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005231int snd_hda_gen_init(struct hda_codec *codec)
5232{
5233 struct hda_gen_spec *spec = codec->spec;
5234
5235 if (spec->init_hook)
5236 spec->init_hook(codec);
5237
5238 snd_hda_apply_verbs(codec);
5239
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005240 codec->cached_write = 1;
5241
Takashi Iwai352f7f92012-12-19 12:52:06 +01005242 init_multi_out(codec);
5243 init_extra_out(codec);
5244 init_multi_io(codec);
5245 init_analog_input(codec);
5246 init_input_src(codec);
5247 init_digital(codec);
5248
Takashi Iwai973e4972012-12-20 15:16:09 +01005249 clear_unsol_on_unused_pins(codec);
5250
Takashi Iwai352f7f92012-12-19 12:52:06 +01005251 /* call init functions of standard auto-mute helpers */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01005252 update_automute_all(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005253
Takashi Iwaidc870f32013-01-22 15:24:30 +01005254 snd_hda_codec_flush_cache(codec);
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005255
Takashi Iwai352f7f92012-12-19 12:52:06 +01005256 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
5257 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5258
5259 hda_call_check_power_status(codec, 0x01);
5260 return 0;
5261}
Takashi Iwaifce52a32013-01-07 12:42:48 +01005262EXPORT_SYMBOL_HDA(snd_hda_gen_init);
5263
Takashi Iwai5187ac12013-01-07 12:52:16 +01005264/*
5265 * free the generic spec;
5266 * this can be put as patch_ops.free function
5267 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005268void snd_hda_gen_free(struct hda_codec *codec)
5269{
Takashi Iwai7504b6c2013-03-18 11:25:51 +01005270 snd_hda_detach_beep_device(codec);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005271 snd_hda_gen_spec_free(codec->spec);
5272 kfree(codec->spec);
5273 codec->spec = NULL;
5274}
5275EXPORT_SYMBOL_HDA(snd_hda_gen_free);
5276
5277#ifdef CONFIG_PM
Takashi Iwai5187ac12013-01-07 12:52:16 +01005278/*
5279 * check the loopback power save state;
5280 * this can be put as patch_ops.check_power_status function
5281 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005282int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
5283{
5284 struct hda_gen_spec *spec = codec->spec;
5285 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
5286}
5287EXPORT_SYMBOL_HDA(snd_hda_gen_check_power_status);
5288#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01005289
5290
5291/*
5292 * the generic codec support
5293 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294
Takashi Iwai352f7f92012-12-19 12:52:06 +01005295static const struct hda_codec_ops generic_patch_ops = {
5296 .build_controls = snd_hda_gen_build_controls,
5297 .build_pcms = snd_hda_gen_build_pcms,
5298 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01005299 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005300 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02005301#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01005302 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02005303#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304};
5305
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306int snd_hda_parse_generic_codec(struct hda_codec *codec)
5307{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005308 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309 int err;
5310
Takashi Iwaie560d8d2005-09-09 14:21:46 +02005311 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005312 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005314 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005315 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316
Takashi Iwai9eb413e2012-12-19 14:41:21 +01005317 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
5318 if (err < 0)
5319 return err;
5320
5321 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005322 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323 goto error;
5324
5325 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326 return 0;
5327
Takashi Iwai352f7f92012-12-19 12:52:06 +01005328error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01005329 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330 return err;
5331}
Takashi Iwaifce52a32013-01-07 12:42:48 +01005332EXPORT_SYMBOL_HDA(snd_hda_parse_generic_codec);
Takashi Iwaib21bdd02013-11-18 12:03:56 +01005333
5334MODULE_LICENSE("GPL");
5335MODULE_DESCRIPTION("Generic HD-audio codec parser");