blob: 1ea9c2dad95006fd392fa9c120236f6b94371313 [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 Iwaif873e532012-12-20 16:58:39 +010027#include <linux/ctype.h>
28#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <sound/core.h>
Takashi Iwai352f7f92012-12-19 12:52:06 +010030#include <sound/jack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "hda_codec.h"
32#include "hda_local.h"
Takashi Iwai352f7f92012-12-19 12:52:06 +010033#include "hda_auto_parser.h"
34#include "hda_jack.h"
35#include "hda_generic.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Takashi Iwai352f7f92012-12-19 12:52:06 +010038/* initialize hda_gen_spec struct */
39int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
Takashi Iwai352f7f92012-12-19 12:52:06 +010041 snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
Takashi Iwai352f7f92012-12-19 12:52:06 +010042 snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
Takashi Iwai38cf6f12012-12-21 14:09:42 +010043 mutex_init(&spec->pcm_mutex);
Takashi Iwai352f7f92012-12-19 12:52:06 +010044 return 0;
45}
46EXPORT_SYMBOL_HDA(snd_hda_gen_spec_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Takashi Iwai12c93df2012-12-19 14:38:33 +010048struct snd_kcontrol_new *
49snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
50 const struct snd_kcontrol_new *temp)
Takashi Iwai352f7f92012-12-19 12:52:06 +010051{
52 struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
53 if (!knew)
54 return NULL;
55 *knew = *temp;
56 if (name)
57 knew->name = kstrdup(name, GFP_KERNEL);
58 else if (knew->name)
59 knew->name = kstrdup(knew->name, GFP_KERNEL);
60 if (!knew->name)
61 return NULL;
62 return knew;
63}
Takashi Iwai12c93df2012-12-19 14:38:33 +010064EXPORT_SYMBOL_HDA(snd_hda_gen_add_kctl);
Takashi Iwai352f7f92012-12-19 12:52:06 +010065
66static void free_kctls(struct hda_gen_spec *spec)
67{
68 if (spec->kctls.list) {
69 struct snd_kcontrol_new *kctl = spec->kctls.list;
70 int i;
71 for (i = 0; i < spec->kctls.used; i++)
72 kfree(kctl[i].name);
73 }
74 snd_array_free(&spec->kctls);
75}
76
Takashi Iwai352f7f92012-12-19 12:52:06 +010077void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
78{
79 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 return;
Takashi Iwai352f7f92012-12-19 12:52:06 +010081 free_kctls(spec);
Takashi Iwai352f7f92012-12-19 12:52:06 +010082 snd_array_free(&spec->paths);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
Takashi Iwai352f7f92012-12-19 12:52:06 +010084EXPORT_SYMBOL_HDA(snd_hda_gen_spec_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86/*
Takashi Iwai1c70a582013-01-11 17:48:22 +010087 * store user hints
88 */
89static void parse_user_hints(struct hda_codec *codec)
90{
91 struct hda_gen_spec *spec = codec->spec;
92 int val;
93
94 val = snd_hda_get_bool_hint(codec, "jack_detect");
95 if (val >= 0)
96 codec->no_jack_detect = !val;
97 val = snd_hda_get_bool_hint(codec, "inv_jack_detect");
98 if (val >= 0)
99 codec->inv_jack_detect = !!val;
100 val = snd_hda_get_bool_hint(codec, "trigger_sense");
101 if (val >= 0)
102 codec->no_trigger_sense = !val;
103 val = snd_hda_get_bool_hint(codec, "inv_eapd");
104 if (val >= 0)
105 codec->inv_eapd = !!val;
106 val = snd_hda_get_bool_hint(codec, "pcm_format_first");
107 if (val >= 0)
108 codec->pcm_format_first = !!val;
109 val = snd_hda_get_bool_hint(codec, "sticky_stream");
110 if (val >= 0)
111 codec->no_sticky_stream = !val;
112 val = snd_hda_get_bool_hint(codec, "spdif_status_reset");
113 if (val >= 0)
114 codec->spdif_status_reset = !!val;
115 val = snd_hda_get_bool_hint(codec, "pin_amp_workaround");
116 if (val >= 0)
117 codec->pin_amp_workaround = !!val;
118 val = snd_hda_get_bool_hint(codec, "single_adc_amp");
119 if (val >= 0)
120 codec->single_adc_amp = !!val;
121
Takashi Iwaif72706b2013-01-16 18:20:07 +0100122 val = snd_hda_get_bool_hint(codec, "auto_mute");
123 if (val >= 0)
124 spec->suppress_auto_mute = !val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100125 val = snd_hda_get_bool_hint(codec, "auto_mic");
126 if (val >= 0)
127 spec->suppress_auto_mic = !val;
128 val = snd_hda_get_bool_hint(codec, "line_in_auto_switch");
129 if (val >= 0)
130 spec->line_in_auto_switch = !!val;
131 val = snd_hda_get_bool_hint(codec, "need_dac_fix");
132 if (val >= 0)
133 spec->need_dac_fix = !!val;
134 val = snd_hda_get_bool_hint(codec, "primary_hp");
135 if (val >= 0)
136 spec->no_primary_hp = !val;
137 val = snd_hda_get_bool_hint(codec, "multi_cap_vol");
138 if (val >= 0)
139 spec->multi_cap_vol = !!val;
140 val = snd_hda_get_bool_hint(codec, "inv_dmic_split");
141 if (val >= 0)
142 spec->inv_dmic_split = !!val;
143 val = snd_hda_get_bool_hint(codec, "indep_hp");
144 if (val >= 0)
145 spec->indep_hp = !!val;
146 val = snd_hda_get_bool_hint(codec, "add_stereo_mix_input");
147 if (val >= 0)
148 spec->add_stereo_mix_input = !!val;
149 val = snd_hda_get_bool_hint(codec, "add_out_jack_modes");
150 if (val >= 0)
151 spec->add_out_jack_modes = !!val;
152
153 if (!snd_hda_get_int_hint(codec, "mixer_nid", &val))
154 spec->mixer_nid = val;
155}
156
157/*
Takashi Iwai2c12c302013-01-10 09:33:29 +0100158 * pin control value accesses
159 */
160
161#define update_pin_ctl(codec, pin, val) \
162 snd_hda_codec_update_cache(codec, pin, 0, \
163 AC_VERB_SET_PIN_WIDGET_CONTROL, val)
164
165/* restore the pinctl based on the cached value */
166static inline void restore_pin_ctl(struct hda_codec *codec, hda_nid_t pin)
167{
168 update_pin_ctl(codec, pin, snd_hda_codec_get_pin_target(codec, pin));
169}
170
171/* set the pinctl target value and write it if requested */
172static void set_pin_target(struct hda_codec *codec, hda_nid_t pin,
173 unsigned int val, bool do_write)
174{
175 if (!pin)
176 return;
177 val = snd_hda_correct_pin_ctl(codec, pin, val);
178 snd_hda_codec_set_pin_target(codec, pin, val);
179 if (do_write)
180 update_pin_ctl(codec, pin, val);
181}
182
183/* set pinctl target values for all given pins */
184static void set_pin_targets(struct hda_codec *codec, int num_pins,
185 hda_nid_t *pins, unsigned int val)
186{
187 int i;
188 for (i = 0; i < num_pins; i++)
189 set_pin_target(codec, pins[i], val, false);
190}
191
192/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100193 * parsing paths
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100196/* return the position of NID in the list, or -1 if not found */
197static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
198{
199 int i;
200 for (i = 0; i < nums; i++)
201 if (list[i] == nid)
202 return i;
203 return -1;
204}
205
206/* return true if the given NID is contained in the path */
207static bool is_nid_contained(struct nid_path *path, hda_nid_t nid)
208{
209 return find_idx_in_nid_list(nid, path->path, path->depth) >= 0;
210}
211
Takashi Iwaif5172a72013-01-04 13:19:55 +0100212static struct nid_path *get_nid_path(struct hda_codec *codec,
213 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100214 int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100216 struct hda_gen_spec *spec = codec->spec;
217 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Takashi Iwai352f7f92012-12-19 12:52:06 +0100219 for (i = 0; i < spec->paths.used; i++) {
220 struct nid_path *path = snd_array_elem(&spec->paths, i);
221 if (path->depth <= 0)
222 continue;
223 if ((!from_nid || path->path[0] == from_nid) &&
Takashi Iwaif5172a72013-01-04 13:19:55 +0100224 (!to_nid || path->path[path->depth - 1] == to_nid)) {
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100225 if (!anchor_nid ||
226 (anchor_nid > 0 && is_nid_contained(path, anchor_nid)) ||
227 (anchor_nid < 0 && !is_nid_contained(path, anchor_nid)))
Takashi Iwaif5172a72013-01-04 13:19:55 +0100228 return path;
229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 }
231 return NULL;
232}
Takashi Iwaif5172a72013-01-04 13:19:55 +0100233
234/* get the path between the given NIDs;
235 * passing 0 to either @pin or @dac behaves as a wildcard
236 */
237struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
238 hda_nid_t from_nid, hda_nid_t to_nid)
239{
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100240 return get_nid_path(codec, from_nid, to_nid, 0);
Takashi Iwaif5172a72013-01-04 13:19:55 +0100241}
Takashi Iwai352f7f92012-12-19 12:52:06 +0100242EXPORT_SYMBOL_HDA(snd_hda_get_nid_path);
243
Takashi Iwai196c17662013-01-04 15:01:40 +0100244/* get the index number corresponding to the path instance;
245 * the index starts from 1, for easier checking the invalid value
246 */
247int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
248{
249 struct hda_gen_spec *spec = codec->spec;
250 struct nid_path *array = spec->paths.list;
251 ssize_t idx;
252
253 if (!spec->paths.used)
254 return 0;
255 idx = path - array;
256 if (idx < 0 || idx >= spec->paths.used)
257 return 0;
258 return idx + 1;
259}
260
261/* get the path instance corresponding to the given index number */
262struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
263{
264 struct hda_gen_spec *spec = codec->spec;
265
266 if (idx <= 0 || idx > spec->paths.used)
267 return NULL;
268 return snd_array_elem(&spec->paths, idx - 1);
269}
270
Takashi Iwai352f7f92012-12-19 12:52:06 +0100271/* check whether the given DAC is already found in any existing paths */
272static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
273{
274 struct hda_gen_spec *spec = codec->spec;
275 int i;
276
277 for (i = 0; i < spec->paths.used; i++) {
278 struct nid_path *path = snd_array_elem(&spec->paths, i);
279 if (path->path[0] == nid)
280 return true;
281 }
282 return false;
283}
284
285/* check whether the given two widgets can be connected */
286static bool is_reachable_path(struct hda_codec *codec,
287 hda_nid_t from_nid, hda_nid_t to_nid)
288{
289 if (!from_nid || !to_nid)
290 return false;
291 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
292}
293
294/* nid, dir and idx */
295#define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
296
297/* check whether the given ctl is already assigned in any path elements */
298static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
299{
300 struct hda_gen_spec *spec = codec->spec;
301 int i;
302
303 val &= AMP_VAL_COMPARE_MASK;
304 for (i = 0; i < spec->paths.used; i++) {
305 struct nid_path *path = snd_array_elem(&spec->paths, i);
306 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
307 return true;
308 }
309 return false;
310}
311
312/* check whether a control with the given (nid, dir, idx) was assigned */
313static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
314 int dir, int idx)
315{
316 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
317 return is_ctl_used(codec, val, NID_PATH_VOL_CTL) ||
318 is_ctl_used(codec, val, NID_PATH_MUTE_CTL);
319}
320
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100321static void print_nid_path(const char *pfx, struct nid_path *path)
322{
323 char buf[40];
324 int i;
325
326
327 buf[0] = 0;
328 for (i = 0; i < path->depth; i++) {
329 char tmp[4];
330 sprintf(tmp, ":%02x", path->path[i]);
331 strlcat(buf, tmp, sizeof(buf));
332 }
333 snd_printdd("%s path: depth=%d %s\n", pfx, path->depth, buf);
334}
335
Takashi Iwai352f7f92012-12-19 12:52:06 +0100336/* called recursively */
337static bool __parse_nid_path(struct hda_codec *codec,
338 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100339 int anchor_nid, struct nid_path *path,
340 int depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100341{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100342 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100343 int i, nums;
344
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100345 if (to_nid == anchor_nid)
346 anchor_nid = 0; /* anchor passed */
347 else if (to_nid == (hda_nid_t)(-anchor_nid))
348 return false; /* hit the exclusive nid */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100349
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100350 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100351 for (i = 0; i < nums; i++) {
352 if (conn[i] != from_nid) {
353 /* special case: when from_nid is 0,
354 * try to find an empty DAC
355 */
356 if (from_nid ||
357 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
358 is_dac_already_used(codec, conn[i]))
359 continue;
360 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100361 /* anchor is not requested or already passed? */
362 if (anchor_nid <= 0)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100363 goto found;
364 }
365 if (depth >= MAX_NID_PATH_DEPTH)
366 return false;
367 for (i = 0; i < nums; i++) {
368 unsigned int type;
369 type = get_wcaps_type(get_wcaps(codec, conn[i]));
370 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
371 type == AC_WID_PIN)
372 continue;
373 if (__parse_nid_path(codec, from_nid, conn[i],
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100374 anchor_nid, path, depth + 1))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100375 goto found;
376 }
377 return false;
378
379 found:
380 path->path[path->depth] = conn[i];
381 path->idx[path->depth + 1] = i;
382 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
383 path->multi[path->depth + 1] = 1;
384 path->depth++;
385 return true;
386}
387
388/* parse the widget path from the given nid to the target nid;
389 * when @from_nid is 0, try to find an empty DAC;
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100390 * when @anchor_nid is set to a positive value, only paths through the widget
391 * with the given value are evaluated.
392 * when @anchor_nid is set to a negative value, paths through the widget
393 * with the negative of given value are excluded, only other paths are chosen.
394 * when @anchor_nid is zero, no special handling about path selection.
Takashi Iwai352f7f92012-12-19 12:52:06 +0100395 */
396bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100397 hda_nid_t to_nid, int anchor_nid,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100398 struct nid_path *path)
399{
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100400 if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100401 path->path[path->depth] = to_nid;
402 path->depth++;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100403 return true;
404 }
405 return false;
406}
407EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100410 * parse the path between the given NIDs and add to the path list.
411 * if no valid path is found, return NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100413struct nid_path *
414snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100415 hda_nid_t to_nid, int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100417 struct hda_gen_spec *spec = codec->spec;
418 struct nid_path *path;
419
420 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
421 return NULL;
422
Takashi Iwaif5172a72013-01-04 13:19:55 +0100423 /* check whether the path has been already added */
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100424 path = get_nid_path(codec, from_nid, to_nid, anchor_nid);
Takashi Iwaif5172a72013-01-04 13:19:55 +0100425 if (path)
426 return path;
427
Takashi Iwai352f7f92012-12-19 12:52:06 +0100428 path = snd_array_new(&spec->paths);
429 if (!path)
430 return NULL;
431 memset(path, 0, sizeof(*path));
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100432 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100433 return path;
434 /* push back */
435 spec->paths.used--;
436 return NULL;
437}
438EXPORT_SYMBOL_HDA(snd_hda_add_new_path);
439
Takashi Iwai980428c2013-01-09 09:28:20 +0100440/* clear the given path as invalid so that it won't be picked up later */
441static void invalidate_nid_path(struct hda_codec *codec, int idx)
442{
443 struct nid_path *path = snd_hda_get_path_from_idx(codec, idx);
444 if (!path)
445 return;
446 memset(path, 0, sizeof(*path));
447}
448
Takashi Iwai352f7f92012-12-19 12:52:06 +0100449/* look for an empty DAC slot */
450static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
451 bool is_digital)
452{
453 struct hda_gen_spec *spec = codec->spec;
454 bool cap_digital;
455 int i;
456
457 for (i = 0; i < spec->num_all_dacs; i++) {
458 hda_nid_t nid = spec->all_dacs[i];
459 if (!nid || is_dac_already_used(codec, nid))
460 continue;
461 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
462 if (is_digital != cap_digital)
463 continue;
464 if (is_reachable_path(codec, nid, pin))
465 return nid;
466 }
467 return 0;
468}
469
470/* replace the channels in the composed amp value with the given number */
471static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
472{
473 val &= ~(0x3U << 16);
474 val |= chs << 16;
475 return val;
476}
477
478/* check whether the widget has the given amp capability for the direction */
479static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
480 int dir, unsigned int bits)
481{
482 if (!nid)
483 return false;
484 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
485 if (query_amp_caps(codec, nid, dir) & bits)
486 return true;
487 return false;
488}
489
David Henningsson99a55922013-01-16 15:58:44 +0100490static bool same_amp_caps(struct hda_codec *codec, hda_nid_t nid1,
491 hda_nid_t nid2, int dir)
492{
493 if (!(get_wcaps(codec, nid1) & (1 << (dir + 1))))
494 return !(get_wcaps(codec, nid2) & (1 << (dir + 1)));
495 return (query_amp_caps(codec, nid1, dir) ==
496 query_amp_caps(codec, nid2, dir));
497}
498
Takashi Iwai352f7f92012-12-19 12:52:06 +0100499#define nid_has_mute(codec, nid, dir) \
500 check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
501#define nid_has_volume(codec, nid, dir) \
502 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
503
504/* look for a widget suitable for assigning a mute switch in the path */
505static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
506 struct nid_path *path)
507{
508 int i;
509
510 for (i = path->depth - 1; i >= 0; i--) {
511 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
512 return path->path[i];
513 if (i != path->depth - 1 && i != 0 &&
514 nid_has_mute(codec, path->path[i], HDA_INPUT))
515 return path->path[i];
516 }
517 return 0;
518}
519
520/* look for a widget suitable for assigning a volume ctl in the path */
521static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
522 struct nid_path *path)
523{
524 int i;
525
526 for (i = path->depth - 1; i >= 0; i--) {
527 if (nid_has_volume(codec, path->path[i], HDA_OUTPUT))
528 return path->path[i];
529 }
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200530 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531}
532
533/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100534 * path activation / deactivation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100536
537/* can have the amp-in capability? */
538static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100540 hda_nid_t nid = path->path[idx];
541 unsigned int caps = get_wcaps(codec, nid);
542 unsigned int type = get_wcaps_type(caps);
543
544 if (!(caps & AC_WCAP_IN_AMP))
545 return false;
546 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
547 return false;
548 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549}
550
Takashi Iwai352f7f92012-12-19 12:52:06 +0100551/* can have the amp-out capability? */
552static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100554 hda_nid_t nid = path->path[idx];
555 unsigned int caps = get_wcaps(codec, nid);
556 unsigned int type = get_wcaps_type(caps);
557
558 if (!(caps & AC_WCAP_OUT_AMP))
559 return false;
560 if (type == AC_WID_PIN && !idx) /* only for output pins */
561 return false;
562 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563}
564
Takashi Iwai352f7f92012-12-19 12:52:06 +0100565/* check whether the given (nid,dir,idx) is active */
566static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
567 unsigned int idx, unsigned int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100569 struct hda_gen_spec *spec = codec->spec;
570 int i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Takashi Iwai352f7f92012-12-19 12:52:06 +0100572 for (n = 0; n < spec->paths.used; n++) {
573 struct nid_path *path = snd_array_elem(&spec->paths, n);
574 if (!path->active)
575 continue;
576 for (i = 0; i < path->depth; i++) {
577 if (path->path[i] == nid) {
578 if (dir == HDA_OUTPUT || path->idx[i] == idx)
579 return true;
580 break;
581 }
582 }
583 }
584 return false;
585}
586
587/* get the default amp value for the target state */
588static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
589 int dir, bool enable)
590{
591 unsigned int caps;
592 unsigned int val = 0;
593
594 caps = query_amp_caps(codec, nid, dir);
595 if (caps & AC_AMPCAP_NUM_STEPS) {
596 /* set to 0dB */
597 if (enable)
598 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
599 }
600 if (caps & AC_AMPCAP_MUTE) {
601 if (!enable)
602 val |= HDA_AMP_MUTE;
603 }
604 return val;
605}
606
607/* initialize the amp value (only at the first time) */
608static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
609{
610 int val = get_amp_val_to_activate(codec, nid, dir, false);
611 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
612}
613
614static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
615 int idx, bool enable)
616{
617 int val;
618 if (is_ctl_associated(codec, nid, dir, idx) ||
Takashi Iwai985803c2013-01-03 16:30:04 +0100619 (!enable && is_active_nid(codec, nid, dir, idx)))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100620 return;
621 val = get_amp_val_to_activate(codec, nid, dir, enable);
622 snd_hda_codec_amp_stereo(codec, nid, dir, idx, 0xff, val);
623}
624
625static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
626 int i, bool enable)
627{
628 hda_nid_t nid = path->path[i];
629 init_amp(codec, nid, HDA_OUTPUT, 0);
630 activate_amp(codec, nid, HDA_OUTPUT, 0, enable);
631}
632
633static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
634 int i, bool enable, bool add_aamix)
635{
636 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100637 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100638 int n, nums, idx;
639 int type;
640 hda_nid_t nid = path->path[i];
641
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100642 nums = snd_hda_get_conn_list(codec, nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100643 type = get_wcaps_type(get_wcaps(codec, nid));
644 if (type == AC_WID_PIN ||
645 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
646 nums = 1;
647 idx = 0;
648 } else
649 idx = path->idx[i];
650
651 for (n = 0; n < nums; n++)
652 init_amp(codec, nid, HDA_INPUT, n);
653
654 if (is_ctl_associated(codec, nid, HDA_INPUT, idx))
655 return;
656
657 /* here is a little bit tricky in comparison with activate_amp_out();
658 * when aa-mixer is available, we need to enable the path as well
659 */
660 for (n = 0; n < nums; n++) {
661 if (n != idx && (!add_aamix || conn[n] != spec->mixer_nid))
662 continue;
663 activate_amp(codec, nid, HDA_INPUT, n, enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 }
665}
666
Takashi Iwai352f7f92012-12-19 12:52:06 +0100667/* activate or deactivate the given path
668 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100670void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
671 bool enable, bool add_aamix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100673 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Takashi Iwai352f7f92012-12-19 12:52:06 +0100675 if (!enable)
676 path->active = false;
677
678 for (i = path->depth - 1; i >= 0; i--) {
679 if (enable && path->multi[i])
680 snd_hda_codec_write_cache(codec, path->path[i], 0,
681 AC_VERB_SET_CONNECT_SEL,
682 path->idx[i]);
683 if (has_amp_in(codec, path, i))
684 activate_amp_in(codec, path, i, enable, add_aamix);
685 if (has_amp_out(codec, path, i))
686 activate_amp_out(codec, path, i, enable);
687 }
688
689 if (enable)
690 path->active = true;
691}
692EXPORT_SYMBOL_HDA(snd_hda_activate_path);
693
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100694/* turn on/off EAPD on the given pin */
695static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
696{
697 struct hda_gen_spec *spec = codec->spec;
698 if (spec->own_eapd_ctl ||
699 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
700 return;
Takashi Iwaiecac3ed2012-12-21 15:23:01 +0100701 if (codec->inv_eapd)
702 enable = !enable;
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100703 snd_hda_codec_update_cache(codec, pin, 0,
704 AC_VERB_SET_EAPD_BTLENABLE,
705 enable ? 0x02 : 0x00);
706}
707
Takashi Iwai352f7f92012-12-19 12:52:06 +0100708
709/*
710 * Helper functions for creating mixer ctl elements
711 */
712
713enum {
714 HDA_CTL_WIDGET_VOL,
715 HDA_CTL_WIDGET_MUTE,
716 HDA_CTL_BIND_MUTE,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100717};
718static const struct snd_kcontrol_new control_templates[] = {
719 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
720 HDA_CODEC_MUTE(NULL, 0, 0, 0),
721 HDA_BIND_MUTE(NULL, 0, 0, 0),
Takashi Iwai352f7f92012-12-19 12:52:06 +0100722};
723
724/* add dynamic controls from template */
725static int add_control(struct hda_gen_spec *spec, int type, const char *name,
726 int cidx, unsigned long val)
727{
728 struct snd_kcontrol_new *knew;
729
Takashi Iwai12c93df2012-12-19 14:38:33 +0100730 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100731 if (!knew)
732 return -ENOMEM;
733 knew->index = cidx;
734 if (get_amp_nid_(val))
735 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
736 knew->private_value = val;
737 return 0;
738}
739
740static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
741 const char *pfx, const char *dir,
742 const char *sfx, int cidx, unsigned long val)
743{
744 char name[32];
745 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
746 return add_control(spec, type, name, cidx, val);
747}
748
749#define add_pb_vol_ctrl(spec, type, pfx, val) \
750 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
751#define add_pb_sw_ctrl(spec, type, pfx, val) \
752 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
753#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
754 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
755#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
756 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
757
758static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
759 unsigned int chs, struct nid_path *path)
760{
761 unsigned int val;
762 if (!path)
763 return 0;
764 val = path->ctls[NID_PATH_VOL_CTL];
765 if (!val)
766 return 0;
767 val = amp_val_replace_channels(val, chs);
768 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
769}
770
771/* return the channel bits suitable for the given path->ctls[] */
772static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
773 int type)
774{
775 int chs = 1; /* mono (left only) */
776 if (path) {
777 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
778 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
779 chs = 3; /* stereo */
780 }
781 return chs;
782}
783
784static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
785 struct nid_path *path)
786{
787 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
788 return add_vol_ctl(codec, pfx, cidx, chs, path);
789}
790
791/* create a mute-switch for the given mixer widget;
792 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
793 */
794static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
795 unsigned int chs, struct nid_path *path)
796{
797 unsigned int val;
798 int type = HDA_CTL_WIDGET_MUTE;
799
800 if (!path)
801 return 0;
802 val = path->ctls[NID_PATH_MUTE_CTL];
803 if (!val)
804 return 0;
805 val = amp_val_replace_channels(val, chs);
806 if (get_amp_direction_(val) == HDA_INPUT) {
807 hda_nid_t nid = get_amp_nid_(val);
808 int nums = snd_hda_get_num_conns(codec, nid);
809 if (nums > 1) {
810 type = HDA_CTL_BIND_MUTE;
811 val |= nums << 19;
812 }
813 }
814 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
815}
816
817static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
818 int cidx, struct nid_path *path)
819{
820 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
821 return add_sw_ctl(codec, pfx, cidx, chs, path);
822}
823
824static const char * const channel_name[4] = {
825 "Front", "Surround", "CLFE", "Side"
826};
827
828/* give some appropriate ctl name prefix for the given line out channel */
829static const char *get_line_out_pfx(struct hda_gen_spec *spec, int ch,
830 bool can_be_master, int *index)
831{
832 struct auto_pin_cfg *cfg = &spec->autocfg;
833
834 *index = 0;
835 if (cfg->line_outs == 1 && !spec->multi_ios &&
836 !cfg->hp_outs && !cfg->speaker_outs && can_be_master)
837 return spec->vmaster_mute.hook ? "PCM" : "Master";
838
839 /* if there is really a single DAC used in the whole output paths,
840 * use it master (or "PCM" if a vmaster hook is present)
841 */
842 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
843 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
844 return spec->vmaster_mute.hook ? "PCM" : "Master";
845
846 switch (cfg->line_out_type) {
847 case AUTO_PIN_SPEAKER_OUT:
848 if (cfg->line_outs == 1)
849 return "Speaker";
850 if (cfg->line_outs == 2)
851 return ch ? "Bass Speaker" : "Speaker";
852 break;
853 case AUTO_PIN_HP_OUT:
854 /* for multi-io case, only the primary out */
855 if (ch && spec->multi_ios)
856 break;
857 *index = ch;
858 return "Headphone";
859 default:
860 if (cfg->line_outs == 1 && !spec->multi_ios)
861 return "PCM";
862 break;
863 }
864 if (ch >= ARRAY_SIZE(channel_name)) {
865 snd_BUG();
866 return "PCM";
867 }
868
869 return channel_name[ch];
870}
871
872/*
873 * Parse output paths
874 */
875
876/* badness definition */
877enum {
878 /* No primary DAC is found for the main output */
879 BAD_NO_PRIMARY_DAC = 0x10000,
880 /* No DAC is found for the extra output */
881 BAD_NO_DAC = 0x4000,
882 /* No possible multi-ios */
883 BAD_MULTI_IO = 0x103,
884 /* No individual DAC for extra output */
885 BAD_NO_EXTRA_DAC = 0x102,
886 /* No individual DAC for extra surrounds */
887 BAD_NO_EXTRA_SURR_DAC = 0x101,
888 /* Primary DAC shared with main surrounds */
889 BAD_SHARED_SURROUND = 0x100,
890 /* Primary DAC shared with main CLFE */
891 BAD_SHARED_CLFE = 0x10,
892 /* Primary DAC shared with extra surrounds */
893 BAD_SHARED_EXTRA_SURROUND = 0x10,
894 /* Volume widget is shared */
895 BAD_SHARED_VOL = 0x10,
896};
897
Takashi Iwai0e614dd2013-01-07 15:11:44 +0100898/* look for widgets in the given path which are appropriate for
Takashi Iwai352f7f92012-12-19 12:52:06 +0100899 * volume and mute controls, and assign the values to ctls[].
900 *
901 * When no appropriate widget is found in the path, the badness value
902 * is incremented depending on the situation. The function returns the
903 * total badness for both volume and mute controls.
904 */
Takashi Iwai0e614dd2013-01-07 15:11:44 +0100905static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100906{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100907 hda_nid_t nid;
908 unsigned int val;
909 int badness = 0;
910
911 if (!path)
912 return BAD_SHARED_VOL * 2;
Takashi Iwai0e614dd2013-01-07 15:11:44 +0100913
914 if (path->ctls[NID_PATH_VOL_CTL] ||
915 path->ctls[NID_PATH_MUTE_CTL])
916 return 0; /* already evaluated */
917
Takashi Iwai352f7f92012-12-19 12:52:06 +0100918 nid = look_for_out_vol_nid(codec, path);
919 if (nid) {
920 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
921 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
922 badness += BAD_SHARED_VOL;
923 else
924 path->ctls[NID_PATH_VOL_CTL] = val;
925 } else
926 badness += BAD_SHARED_VOL;
927 nid = look_for_out_mute_nid(codec, path);
928 if (nid) {
929 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
930 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
931 nid_has_mute(codec, nid, HDA_OUTPUT))
932 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
933 else
934 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
935 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
936 badness += BAD_SHARED_VOL;
937 else
938 path->ctls[NID_PATH_MUTE_CTL] = val;
939 } else
940 badness += BAD_SHARED_VOL;
941 return badness;
942}
943
944struct badness_table {
945 int no_primary_dac; /* no primary DAC */
946 int no_dac; /* no secondary DACs */
947 int shared_primary; /* primary DAC is shared with main output */
948 int shared_surr; /* secondary DAC shared with main or primary */
949 int shared_clfe; /* third DAC shared with main or primary */
950 int shared_surr_main; /* secondary DAC sahred with main/DAC0 */
951};
952
953static struct badness_table main_out_badness = {
954 .no_primary_dac = BAD_NO_PRIMARY_DAC,
955 .no_dac = BAD_NO_DAC,
956 .shared_primary = BAD_NO_PRIMARY_DAC,
957 .shared_surr = BAD_SHARED_SURROUND,
958 .shared_clfe = BAD_SHARED_CLFE,
959 .shared_surr_main = BAD_SHARED_SURROUND,
960};
961
962static struct badness_table extra_out_badness = {
963 .no_primary_dac = BAD_NO_DAC,
964 .no_dac = BAD_NO_DAC,
965 .shared_primary = BAD_NO_EXTRA_DAC,
966 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
967 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
968 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
969};
970
Takashi Iwai7385df62013-01-07 09:50:52 +0100971/* get the DAC of the primary output corresponding to the given array index */
972static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
973{
974 struct hda_gen_spec *spec = codec->spec;
975 struct auto_pin_cfg *cfg = &spec->autocfg;
976
977 if (cfg->line_outs > idx)
978 return spec->private_dac_nids[idx];
979 idx -= cfg->line_outs;
980 if (spec->multi_ios > idx)
981 return spec->multi_io[idx].dac;
982 return 0;
983}
984
985/* return the DAC if it's reachable, otherwise zero */
986static inline hda_nid_t try_dac(struct hda_codec *codec,
987 hda_nid_t dac, hda_nid_t pin)
988{
989 return is_reachable_path(codec, dac, pin) ? dac : 0;
990}
991
Takashi Iwai352f7f92012-12-19 12:52:06 +0100992/* try to assign DACs to pins and return the resultant badness */
993static int try_assign_dacs(struct hda_codec *codec, int num_outs,
994 const hda_nid_t *pins, hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +0100995 int *path_idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100996 const struct badness_table *bad)
997{
998 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100999 int i, j;
1000 int badness = 0;
1001 hda_nid_t dac;
1002
1003 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 return 0;
1005
Takashi Iwai352f7f92012-12-19 12:52:06 +01001006 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001007 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001008 hda_nid_t pin = pins[i];
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001009
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001010 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1011 if (path) {
1012 badness += assign_out_path_ctls(codec, path);
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001013 continue;
1014 }
1015
1016 dacs[i] = look_for_dac(codec, pin, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001017 if (!dacs[i] && !i) {
Takashi Iwai980428c2013-01-09 09:28:20 +01001018 /* try to steal the DAC of surrounds for the front */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001019 for (j = 1; j < num_outs; j++) {
1020 if (is_reachable_path(codec, dacs[j], pin)) {
1021 dacs[0] = dacs[j];
1022 dacs[j] = 0;
Takashi Iwai980428c2013-01-09 09:28:20 +01001023 invalidate_nid_path(codec, path_idx[j]);
Takashi Iwai196c17662013-01-04 15:01:40 +01001024 path_idx[j] = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001025 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 }
1027 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001028 }
1029 dac = dacs[i];
1030 if (!dac) {
Takashi Iwai7385df62013-01-07 09:50:52 +01001031 if (num_outs > 2)
1032 dac = try_dac(codec, get_primary_out(codec, i), pin);
1033 if (!dac)
1034 dac = try_dac(codec, dacs[0], pin);
1035 if (!dac)
1036 dac = try_dac(codec, get_primary_out(codec, i), pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001037 if (dac) {
1038 if (!i)
1039 badness += bad->shared_primary;
1040 else if (i == 1)
1041 badness += bad->shared_surr;
1042 else
1043 badness += bad->shared_clfe;
1044 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1045 dac = spec->private_dac_nids[0];
1046 badness += bad->shared_surr_main;
1047 } else if (!i)
1048 badness += bad->no_primary_dac;
1049 else
1050 badness += bad->no_dac;
1051 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001052 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001053 if (!path && !i && spec->mixer_nid) {
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001054 /* try with aamix */
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001055 path = snd_hda_add_new_path(codec, dac, pin, 0);
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001056 }
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001057 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001058 dac = dacs[i] = 0;
Takashi Iwaie1284af2013-01-03 16:33:02 +01001059 else {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001060 print_nid_path("output", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001061 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001062 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001063 badness += assign_out_path_ctls(codec, path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001064 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001065 }
1066
1067 return badness;
1068}
1069
1070/* return NID if the given pin has only a single connection to a certain DAC */
1071static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1072{
1073 struct hda_gen_spec *spec = codec->spec;
1074 int i;
1075 hda_nid_t nid_found = 0;
1076
1077 for (i = 0; i < spec->num_all_dacs; i++) {
1078 hda_nid_t nid = spec->all_dacs[i];
1079 if (!nid || is_dac_already_used(codec, nid))
1080 continue;
1081 if (is_reachable_path(codec, nid, pin)) {
1082 if (nid_found)
1083 return 0;
1084 nid_found = nid;
1085 }
1086 }
1087 return nid_found;
1088}
1089
1090/* check whether the given pin can be a multi-io pin */
1091static bool can_be_multiio_pin(struct hda_codec *codec,
1092 unsigned int location, hda_nid_t nid)
1093{
1094 unsigned int defcfg, caps;
1095
1096 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1097 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1098 return false;
1099 if (location && get_defcfg_location(defcfg) != location)
1100 return false;
1101 caps = snd_hda_query_pin_caps(codec, nid);
1102 if (!(caps & AC_PINCAP_OUT))
1103 return false;
1104 return true;
1105}
1106
Takashi Iwaie22aab72013-01-04 14:50:04 +01001107/* count the number of input pins that are capable to be multi-io */
1108static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1109{
1110 struct hda_gen_spec *spec = codec->spec;
1111 struct auto_pin_cfg *cfg = &spec->autocfg;
1112 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1113 unsigned int location = get_defcfg_location(defcfg);
1114 int type, i;
1115 int num_pins = 0;
1116
1117 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1118 for (i = 0; i < cfg->num_inputs; i++) {
1119 if (cfg->inputs[i].type != type)
1120 continue;
1121 if (can_be_multiio_pin(codec, location,
1122 cfg->inputs[i].pin))
1123 num_pins++;
1124 }
1125 }
1126 return num_pins;
1127}
1128
Takashi Iwai352f7f92012-12-19 12:52:06 +01001129/*
1130 * multi-io helper
1131 *
1132 * When hardwired is set, try to fill ony hardwired pins, and returns
1133 * zero if any pins are filled, non-zero if nothing found.
1134 * When hardwired is off, try to fill possible input pins, and returns
1135 * the badness value.
1136 */
1137static int fill_multi_ios(struct hda_codec *codec,
1138 hda_nid_t reference_pin,
Takashi Iwaie22aab72013-01-04 14:50:04 +01001139 bool hardwired)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001140{
1141 struct hda_gen_spec *spec = codec->spec;
1142 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie22aab72013-01-04 14:50:04 +01001143 int type, i, j, num_pins, old_pins;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001144 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1145 unsigned int location = get_defcfg_location(defcfg);
1146 int badness = 0;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001147 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001148
1149 old_pins = spec->multi_ios;
1150 if (old_pins >= 2)
1151 goto end_fill;
1152
Takashi Iwaie22aab72013-01-04 14:50:04 +01001153 num_pins = count_multiio_pins(codec, reference_pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001154 if (num_pins < 2)
1155 goto end_fill;
1156
Takashi Iwai352f7f92012-12-19 12:52:06 +01001157 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1158 for (i = 0; i < cfg->num_inputs; i++) {
1159 hda_nid_t nid = cfg->inputs[i].pin;
1160 hda_nid_t dac = 0;
1161
1162 if (cfg->inputs[i].type != type)
1163 continue;
1164 if (!can_be_multiio_pin(codec, location, nid))
1165 continue;
1166 for (j = 0; j < spec->multi_ios; j++) {
1167 if (nid == spec->multi_io[j].pin)
1168 break;
1169 }
1170 if (j < spec->multi_ios)
1171 continue;
1172
Takashi Iwai352f7f92012-12-19 12:52:06 +01001173 if (hardwired)
1174 dac = get_dac_if_single(codec, nid);
1175 else if (!dac)
1176 dac = look_for_dac(codec, nid, false);
1177 if (!dac) {
1178 badness++;
1179 continue;
1180 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001181 path = snd_hda_add_new_path(codec, dac, nid,
1182 -spec->mixer_nid);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001183 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001184 badness++;
1185 continue;
1186 }
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001187 print_nid_path("multiio", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001188 spec->multi_io[spec->multi_ios].pin = nid;
1189 spec->multi_io[spec->multi_ios].dac = dac;
Takashi Iwai196c17662013-01-04 15:01:40 +01001190 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1191 snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001192 spec->multi_ios++;
1193 if (spec->multi_ios >= 2)
1194 break;
1195 }
1196 }
1197 end_fill:
1198 if (badness)
1199 badness = BAD_MULTI_IO;
1200 if (old_pins == spec->multi_ios) {
1201 if (hardwired)
1202 return 1; /* nothing found */
1203 else
1204 return badness; /* no badness if nothing found */
1205 }
1206 if (!hardwired && spec->multi_ios < 2) {
1207 /* cancel newly assigned paths */
1208 spec->paths.used -= spec->multi_ios - old_pins;
1209 spec->multi_ios = old_pins;
1210 return badness;
1211 }
1212
1213 /* assign volume and mute controls */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001214 for (i = old_pins; i < spec->multi_ios; i++) {
1215 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1216 badness += assign_out_path_ctls(codec, path);
1217 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001218
1219 return badness;
1220}
1221
1222/* map DACs for all pins in the list if they are single connections */
1223static bool map_singles(struct hda_codec *codec, int outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001224 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001225{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001226 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001227 int i;
1228 bool found = false;
1229 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001230 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001231 hda_nid_t dac;
1232 if (dacs[i])
1233 continue;
1234 dac = get_dac_if_single(codec, pins[i]);
1235 if (!dac)
1236 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001237 path = snd_hda_add_new_path(codec, dac, pins[i],
1238 -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001239 if (!path && !i && spec->mixer_nid)
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001240 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001241 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001242 dacs[i] = dac;
1243 found = true;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001244 print_nid_path("output", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001245 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001246 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001247 }
1248 }
1249 return found;
1250}
1251
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001252/* create a new path including aamix if available, and return its index */
1253static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1254{
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001255 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001256 struct nid_path *path;
1257
1258 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001259 if (!path || !path->depth ||
1260 is_nid_contained(path, spec->mixer_nid))
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001261 return 0;
1262 path = snd_hda_add_new_path(codec, path->path[0],
1263 path->path[path->depth - 1],
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001264 spec->mixer_nid);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001265 if (!path)
1266 return 0;
1267 print_nid_path("output-aamix", path);
1268 path->active = false; /* unused as default */
1269 return snd_hda_get_path_idx(codec, path);
1270}
1271
Takashi Iwaia07a9492013-01-07 16:44:06 +01001272/* fill the empty entries in the dac array for speaker/hp with the
1273 * shared dac pointed by the paths
1274 */
1275static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1276 hda_nid_t *dacs, int *path_idx)
1277{
1278 struct nid_path *path;
1279 int i;
1280
1281 for (i = 0; i < num_outs; i++) {
1282 if (dacs[i])
1283 continue;
1284 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1285 if (!path)
1286 continue;
1287 dacs[i] = path->path[0];
1288 }
1289}
1290
Takashi Iwai352f7f92012-12-19 12:52:06 +01001291/* fill in the dac_nids table from the parsed pin configuration */
1292static int fill_and_eval_dacs(struct hda_codec *codec,
1293 bool fill_hardwired,
1294 bool fill_mio_first)
1295{
1296 struct hda_gen_spec *spec = codec->spec;
1297 struct auto_pin_cfg *cfg = &spec->autocfg;
1298 int i, err, badness;
Takashi Iwaiea46c3c2013-01-15 18:45:53 +01001299 unsigned int val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001300
1301 /* set num_dacs once to full for look_for_dac() */
1302 spec->multiout.num_dacs = cfg->line_outs;
1303 spec->multiout.dac_nids = spec->private_dac_nids;
1304 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1305 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1306 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1307 spec->multi_ios = 0;
1308 snd_array_free(&spec->paths);
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001309
1310 /* clear path indices */
1311 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1312 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1313 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1314 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1315 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
Takashi Iwaic697b712013-01-07 17:09:26 +01001316 memset(spec->input_paths, 0, sizeof(spec->input_paths));
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001317 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1318 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1319
Takashi Iwai352f7f92012-12-19 12:52:06 +01001320 badness = 0;
1321
1322 /* fill hard-wired DACs first */
1323 if (fill_hardwired) {
1324 bool mapped;
1325 do {
1326 mapped = map_singles(codec, cfg->line_outs,
1327 cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001328 spec->private_dac_nids,
1329 spec->out_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001330 mapped |= map_singles(codec, cfg->hp_outs,
1331 cfg->hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001332 spec->multiout.hp_out_nid,
1333 spec->hp_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001334 mapped |= map_singles(codec, cfg->speaker_outs,
1335 cfg->speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001336 spec->multiout.extra_out_nid,
1337 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001338 if (fill_mio_first && cfg->line_outs == 1 &&
1339 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001340 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001341 if (!err)
1342 mapped = true;
1343 }
1344 } while (mapped);
1345 }
1346
1347 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001348 spec->private_dac_nids, spec->out_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001349 &main_out_badness);
1350
Takashi Iwai352f7f92012-12-19 12:52:06 +01001351 if (fill_mio_first &&
1352 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1353 /* try to fill multi-io first */
Takashi Iwaie22aab72013-01-04 14:50:04 +01001354 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001355 if (err < 0)
1356 return err;
1357 /* we don't count badness at this stage yet */
1358 }
1359
1360 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1361 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1362 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001363 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001364 &extra_out_badness);
1365 if (err < 0)
1366 return err;
1367 badness += err;
1368 }
1369 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1370 err = try_assign_dacs(codec, cfg->speaker_outs,
1371 cfg->speaker_pins,
1372 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001373 spec->speaker_paths,
1374 &extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001375 if (err < 0)
1376 return err;
1377 badness += err;
1378 }
1379 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001380 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001381 if (err < 0)
1382 return err;
1383 badness += err;
1384 }
Takashi Iwaie22aab72013-01-04 14:50:04 +01001385
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001386 if (spec->mixer_nid) {
1387 spec->aamix_out_paths[0] =
1388 check_aamix_out_path(codec, spec->out_paths[0]);
1389 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1390 spec->aamix_out_paths[1] =
1391 check_aamix_out_path(codec, spec->hp_paths[0]);
1392 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1393 spec->aamix_out_paths[2] =
1394 check_aamix_out_path(codec, spec->speaker_paths[0]);
1395 }
1396
Takashi Iwaie22aab72013-01-04 14:50:04 +01001397 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1398 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1399 spec->multi_ios = 1; /* give badness */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001400
Takashi Iwaia07a9492013-01-07 16:44:06 +01001401 /* re-count num_dacs and squash invalid entries */
1402 spec->multiout.num_dacs = 0;
1403 for (i = 0; i < cfg->line_outs; i++) {
1404 if (spec->private_dac_nids[i])
1405 spec->multiout.num_dacs++;
1406 else {
1407 memmove(spec->private_dac_nids + i,
1408 spec->private_dac_nids + i + 1,
1409 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1410 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1411 }
1412 }
1413
1414 spec->ext_channel_count = spec->min_channel_count =
David Henningssonc0f3b212013-01-16 11:45:37 +01001415 spec->multiout.num_dacs * 2;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001416
Takashi Iwai352f7f92012-12-19 12:52:06 +01001417 if (spec->multi_ios == 2) {
1418 for (i = 0; i < 2; i++)
1419 spec->private_dac_nids[spec->multiout.num_dacs++] =
1420 spec->multi_io[i].dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001421 } else if (spec->multi_ios) {
1422 spec->multi_ios = 0;
1423 badness += BAD_MULTI_IO;
1424 }
1425
Takashi Iwaia07a9492013-01-07 16:44:06 +01001426 /* re-fill the shared DAC for speaker / headphone */
1427 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1428 refill_shared_dacs(codec, cfg->hp_outs,
1429 spec->multiout.hp_out_nid,
1430 spec->hp_paths);
1431 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1432 refill_shared_dacs(codec, cfg->speaker_outs,
1433 spec->multiout.extra_out_nid,
1434 spec->speaker_paths);
1435
Takashi Iwai2c12c302013-01-10 09:33:29 +01001436 /* set initial pinctl targets */
Takashi Iwaiea46c3c2013-01-15 18:45:53 +01001437 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
1438 val = PIN_HP;
1439 else
1440 val = PIN_OUT;
1441 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
Takashi Iwai2c12c302013-01-10 09:33:29 +01001442 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1443 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
Takashi Iwaiea46c3c2013-01-15 18:45:53 +01001444 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1445 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
Takashi Iwai2c12c302013-01-10 09:33:29 +01001446 set_pin_targets(codec, cfg->speaker_outs,
Takashi Iwaiea46c3c2013-01-15 18:45:53 +01001447 cfg->speaker_pins, val);
1448 }
Takashi Iwai2c12c302013-01-10 09:33:29 +01001449
Takashi Iwai352f7f92012-12-19 12:52:06 +01001450 return badness;
1451}
1452
1453#define DEBUG_BADNESS
1454
1455#ifdef DEBUG_BADNESS
1456#define debug_badness snd_printdd
1457#else
1458#define debug_badness(...)
1459#endif
1460
1461static void debug_show_configs(struct hda_gen_spec *spec, struct auto_pin_cfg *cfg)
1462{
1463 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1464 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001465 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001466 spec->multiout.dac_nids[0],
1467 spec->multiout.dac_nids[1],
1468 spec->multiout.dac_nids[2],
1469 spec->multiout.dac_nids[3]);
1470 if (spec->multi_ios > 0)
1471 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1472 spec->multi_ios,
1473 spec->multi_io[0].pin, spec->multi_io[1].pin,
1474 spec->multi_io[0].dac, spec->multi_io[1].dac);
1475 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1476 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001477 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001478 spec->multiout.hp_out_nid[0],
1479 spec->multiout.hp_out_nid[1],
1480 spec->multiout.hp_out_nid[2],
1481 spec->multiout.hp_out_nid[3]);
1482 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1483 cfg->speaker_pins[0], cfg->speaker_pins[1],
1484 cfg->speaker_pins[2], cfg->speaker_pins[3],
1485 spec->multiout.extra_out_nid[0],
1486 spec->multiout.extra_out_nid[1],
1487 spec->multiout.extra_out_nid[2],
1488 spec->multiout.extra_out_nid[3]);
1489}
1490
1491/* find all available DACs of the codec */
1492static void fill_all_dac_nids(struct hda_codec *codec)
1493{
1494 struct hda_gen_spec *spec = codec->spec;
1495 int i;
1496 hda_nid_t nid = codec->start_nid;
1497
1498 spec->num_all_dacs = 0;
1499 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1500 for (i = 0; i < codec->num_nodes; i++, nid++) {
1501 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1502 continue;
1503 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1504 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1505 break;
1506 }
1507 spec->all_dacs[spec->num_all_dacs++] = nid;
1508 }
1509}
1510
1511static int parse_output_paths(struct hda_codec *codec)
1512{
1513 struct hda_gen_spec *spec = codec->spec;
1514 struct auto_pin_cfg *cfg = &spec->autocfg;
1515 struct auto_pin_cfg *best_cfg;
1516 int best_badness = INT_MAX;
1517 int badness;
1518 bool fill_hardwired = true, fill_mio_first = true;
1519 bool best_wired = true, best_mio = true;
1520 bool hp_spk_swapped = false;
1521
Takashi Iwai352f7f92012-12-19 12:52:06 +01001522 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1523 if (!best_cfg)
1524 return -ENOMEM;
1525 *best_cfg = *cfg;
1526
1527 for (;;) {
1528 badness = fill_and_eval_dacs(codec, fill_hardwired,
1529 fill_mio_first);
1530 if (badness < 0) {
1531 kfree(best_cfg);
1532 return badness;
1533 }
1534 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1535 cfg->line_out_type, fill_hardwired, fill_mio_first,
1536 badness);
1537 debug_show_configs(spec, cfg);
1538 if (badness < best_badness) {
1539 best_badness = badness;
1540 *best_cfg = *cfg;
1541 best_wired = fill_hardwired;
1542 best_mio = fill_mio_first;
1543 }
1544 if (!badness)
1545 break;
1546 fill_mio_first = !fill_mio_first;
1547 if (!fill_mio_first)
1548 continue;
1549 fill_hardwired = !fill_hardwired;
1550 if (!fill_hardwired)
1551 continue;
1552 if (hp_spk_swapped)
1553 break;
1554 hp_spk_swapped = true;
1555 if (cfg->speaker_outs > 0 &&
1556 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1557 cfg->hp_outs = cfg->line_outs;
1558 memcpy(cfg->hp_pins, cfg->line_out_pins,
1559 sizeof(cfg->hp_pins));
1560 cfg->line_outs = cfg->speaker_outs;
1561 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1562 sizeof(cfg->speaker_pins));
1563 cfg->speaker_outs = 0;
1564 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1565 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1566 fill_hardwired = true;
1567 continue;
1568 }
1569 if (cfg->hp_outs > 0 &&
1570 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1571 cfg->speaker_outs = cfg->line_outs;
1572 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1573 sizeof(cfg->speaker_pins));
1574 cfg->line_outs = cfg->hp_outs;
1575 memcpy(cfg->line_out_pins, cfg->hp_pins,
1576 sizeof(cfg->hp_pins));
1577 cfg->hp_outs = 0;
1578 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1579 cfg->line_out_type = AUTO_PIN_HP_OUT;
1580 fill_hardwired = true;
1581 continue;
1582 }
1583 break;
1584 }
1585
1586 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001587 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001588 *cfg = *best_cfg;
1589 fill_and_eval_dacs(codec, best_wired, best_mio);
1590 }
1591 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1592 cfg->line_out_type, best_wired, best_mio);
1593 debug_show_configs(spec, cfg);
1594
1595 if (cfg->line_out_pins[0]) {
1596 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01001597 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001598 if (path)
1599 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
1600 }
1601
1602 kfree(best_cfg);
1603 return 0;
1604}
1605
1606/* add playback controls from the parsed DAC table */
1607static int create_multi_out_ctls(struct hda_codec *codec,
1608 const struct auto_pin_cfg *cfg)
1609{
1610 struct hda_gen_spec *spec = codec->spec;
1611 int i, err, noutputs;
1612
1613 noutputs = cfg->line_outs;
1614 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1615 noutputs += spec->multi_ios;
1616
1617 for (i = 0; i < noutputs; i++) {
1618 const char *name;
1619 int index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001620 struct nid_path *path;
1621
Takashi Iwai352f7f92012-12-19 12:52:06 +01001622 if (i >= cfg->line_outs) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001623 index = 0;
1624 name = channel_name[i];
1625 } else {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001626 name = get_line_out_pfx(spec, i, true, &index);
1627 }
1628
Takashi Iwai196c17662013-01-04 15:01:40 +01001629 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001630 if (!path)
1631 continue;
1632 if (!name || !strcmp(name, "CLFE")) {
1633 /* Center/LFE */
1634 err = add_vol_ctl(codec, "Center", 0, 1, path);
1635 if (err < 0)
1636 return err;
1637 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1638 if (err < 0)
1639 return err;
1640 err = add_sw_ctl(codec, "Center", 0, 1, path);
1641 if (err < 0)
1642 return err;
1643 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1644 if (err < 0)
1645 return err;
1646 } else {
1647 err = add_stereo_vol(codec, name, index, path);
1648 if (err < 0)
1649 return err;
1650 err = add_stereo_sw(codec, name, index, path);
1651 if (err < 0)
1652 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 }
1654 }
1655 return 0;
1656}
1657
Takashi Iwaic2c80382013-01-07 10:33:57 +01001658static int create_extra_out(struct hda_codec *codec, int path_idx,
Takashi Iwai196c17662013-01-04 15:01:40 +01001659 const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001661 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 int err;
1663
Takashi Iwai196c17662013-01-04 15:01:40 +01001664 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001665 if (!path)
1666 return 0;
Takashi Iwaic2c80382013-01-07 10:33:57 +01001667 err = add_stereo_vol(codec, pfx, cidx, path);
1668 if (err < 0)
1669 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001670 err = add_stereo_sw(codec, pfx, cidx, path);
1671 if (err < 0)
1672 return err;
1673 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674}
1675
Takashi Iwai352f7f92012-12-19 12:52:06 +01001676/* add playback controls for speaker and HP outputs */
1677static int create_extra_outs(struct hda_codec *codec, int num_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001678 const int *paths, const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679{
Takashi Iwaic2c80382013-01-07 10:33:57 +01001680 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001681
1682 for (i = 0; i < num_pins; i++) {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001683 const char *name;
1684 char tmp[44];
1685 int err, idx = 0;
1686
1687 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
1688 name = "Bass Speaker";
1689 else if (num_pins >= 3) {
1690 snprintf(tmp, sizeof(tmp), "%s %s",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001691 pfx, channel_name[i]);
Takashi Iwaic2c80382013-01-07 10:33:57 +01001692 name = tmp;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001693 } else {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001694 name = pfx;
1695 idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 }
Takashi Iwaic2c80382013-01-07 10:33:57 +01001697 err = create_extra_out(codec, paths[i], name, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001698 if (err < 0)
1699 return err;
1700 }
1701 return 0;
1702}
Takashi Iwai97ec5582006-03-21 11:29:07 +01001703
Takashi Iwai352f7f92012-12-19 12:52:06 +01001704static int create_hp_out_ctls(struct hda_codec *codec)
1705{
1706 struct hda_gen_spec *spec = codec->spec;
1707 return create_extra_outs(codec, spec->autocfg.hp_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001708 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001709 "Headphone");
1710}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
Takashi Iwai352f7f92012-12-19 12:52:06 +01001712static int create_speaker_out_ctls(struct hda_codec *codec)
1713{
1714 struct hda_gen_spec *spec = codec->spec;
1715 return create_extra_outs(codec, spec->autocfg.speaker_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001716 spec->speaker_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001717 "Speaker");
1718}
1719
1720/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001721 * independent HP controls
1722 */
1723
1724static int indep_hp_info(struct snd_kcontrol *kcontrol,
1725 struct snd_ctl_elem_info *uinfo)
1726{
1727 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
1728}
1729
1730static int indep_hp_get(struct snd_kcontrol *kcontrol,
1731 struct snd_ctl_elem_value *ucontrol)
1732{
1733 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1734 struct hda_gen_spec *spec = codec->spec;
1735 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
1736 return 0;
1737}
1738
1739static int indep_hp_put(struct snd_kcontrol *kcontrol,
1740 struct snd_ctl_elem_value *ucontrol)
1741{
1742 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1743 struct hda_gen_spec *spec = codec->spec;
1744 unsigned int select = ucontrol->value.enumerated.item[0];
1745 int ret = 0;
1746
1747 mutex_lock(&spec->pcm_mutex);
1748 if (spec->active_streams) {
1749 ret = -EBUSY;
1750 goto unlock;
1751 }
1752
1753 if (spec->indep_hp_enabled != select) {
1754 spec->indep_hp_enabled = select;
1755 if (spec->indep_hp_enabled)
1756 spec->multiout.hp_out_nid[0] = 0;
1757 else
1758 spec->multiout.hp_out_nid[0] = spec->alt_dac_nid;
1759 ret = 1;
1760 }
1761 unlock:
1762 mutex_unlock(&spec->pcm_mutex);
1763 return ret;
1764}
1765
1766static const struct snd_kcontrol_new indep_hp_ctl = {
1767 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1768 .name = "Independent HP",
1769 .info = indep_hp_info,
1770 .get = indep_hp_get,
1771 .put = indep_hp_put,
1772};
1773
1774
1775static int create_indep_hp_ctls(struct hda_codec *codec)
1776{
1777 struct hda_gen_spec *spec = codec->spec;
1778
1779 if (!spec->indep_hp)
1780 return 0;
1781 if (!spec->multiout.hp_out_nid[0]) {
1782 spec->indep_hp = 0;
1783 return 0;
1784 }
1785
1786 spec->indep_hp_enabled = false;
1787 spec->alt_dac_nid = spec->multiout.hp_out_nid[0];
1788 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
1789 return -ENOMEM;
1790 return 0;
1791}
1792
1793/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01001794 * channel mode enum control
1795 */
1796
1797static int ch_mode_info(struct snd_kcontrol *kcontrol,
1798 struct snd_ctl_elem_info *uinfo)
1799{
1800 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1801 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001802 int chs;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001803
1804 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1805 uinfo->count = 1;
1806 uinfo->value.enumerated.items = spec->multi_ios + 1;
1807 if (uinfo->value.enumerated.item > spec->multi_ios)
1808 uinfo->value.enumerated.item = spec->multi_ios;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001809 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
1810 sprintf(uinfo->value.enumerated.name, "%dch", chs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001811 return 0;
1812}
1813
1814static int ch_mode_get(struct snd_kcontrol *kcontrol,
1815 struct snd_ctl_elem_value *ucontrol)
1816{
1817 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1818 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001819 ucontrol->value.enumerated.item[0] =
1820 (spec->ext_channel_count - spec->min_channel_count) / 2;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001821 return 0;
1822}
1823
Takashi Iwai196c17662013-01-04 15:01:40 +01001824static inline struct nid_path *
1825get_multiio_path(struct hda_codec *codec, int idx)
1826{
1827 struct hda_gen_spec *spec = codec->spec;
1828 return snd_hda_get_path_from_idx(codec,
1829 spec->out_paths[spec->autocfg.line_outs + idx]);
1830}
1831
Takashi Iwaia5cc2502013-01-16 18:08:55 +01001832static void update_automute_all(struct hda_codec *codec);
1833
Takashi Iwai352f7f92012-12-19 12:52:06 +01001834static int set_multi_io(struct hda_codec *codec, int idx, bool output)
1835{
1836 struct hda_gen_spec *spec = codec->spec;
1837 hda_nid_t nid = spec->multi_io[idx].pin;
1838 struct nid_path *path;
1839
Takashi Iwai196c17662013-01-04 15:01:40 +01001840 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001841 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001843
1844 if (path->active == output)
1845 return 0;
1846
1847 if (output) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01001848 set_pin_target(codec, nid, PIN_OUT, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001849 snd_hda_activate_path(codec, path, true, true);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01001850 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001851 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01001852 set_pin_eapd(codec, nid, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001853 snd_hda_activate_path(codec, path, false, true);
Takashi Iwai2c12c302013-01-10 09:33:29 +01001854 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 }
Takashi Iwaia365fed2013-01-10 16:10:06 +01001856
1857 /* update jack retasking in case it modifies any of them */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01001858 update_automute_all(codec);
Takashi Iwaia365fed2013-01-10 16:10:06 +01001859
Takashi Iwai352f7f92012-12-19 12:52:06 +01001860 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861}
1862
Takashi Iwai352f7f92012-12-19 12:52:06 +01001863static int ch_mode_put(struct snd_kcontrol *kcontrol,
1864 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001866 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1867 struct hda_gen_spec *spec = codec->spec;
1868 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
Takashi Iwai352f7f92012-12-19 12:52:06 +01001870 ch = ucontrol->value.enumerated.item[0];
1871 if (ch < 0 || ch > spec->multi_ios)
1872 return -EINVAL;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001873 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001874 return 0;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001875 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001876 for (i = 0; i < spec->multi_ios; i++)
1877 set_multi_io(codec, i, i < ch);
1878 spec->multiout.max_channels = max(spec->ext_channel_count,
1879 spec->const_channel_count);
1880 if (spec->need_dac_fix)
1881 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 return 1;
1883}
1884
Takashi Iwai352f7f92012-12-19 12:52:06 +01001885static const struct snd_kcontrol_new channel_mode_enum = {
1886 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1887 .name = "Channel Mode",
1888 .info = ch_mode_info,
1889 .get = ch_mode_get,
1890 .put = ch_mode_put,
1891};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
Takashi Iwai352f7f92012-12-19 12:52:06 +01001893static int create_multi_channel_mode(struct hda_codec *codec)
1894{
1895 struct hda_gen_spec *spec = codec->spec;
1896
1897 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01001898 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01001899 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 return 0;
1902}
1903
Takashi Iwai352f7f92012-12-19 12:52:06 +01001904/*
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001905 * aamix loopback enable/disable switch
1906 */
1907
1908#define loopback_mixing_info indep_hp_info
1909
1910static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
1911 struct snd_ctl_elem_value *ucontrol)
1912{
1913 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1914 struct hda_gen_spec *spec = codec->spec;
1915 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
1916 return 0;
1917}
1918
1919static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
1920 int nomix_path_idx, int mix_path_idx)
1921{
1922 struct nid_path *nomix_path, *mix_path;
1923
1924 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
1925 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
1926 if (!nomix_path || !mix_path)
1927 return;
1928 if (do_mix) {
1929 snd_hda_activate_path(codec, nomix_path, false, true);
1930 snd_hda_activate_path(codec, mix_path, true, true);
1931 } else {
1932 snd_hda_activate_path(codec, mix_path, false, true);
1933 snd_hda_activate_path(codec, nomix_path, true, true);
1934 }
1935}
1936
1937static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
1938 struct snd_ctl_elem_value *ucontrol)
1939{
1940 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1941 struct hda_gen_spec *spec = codec->spec;
1942 unsigned int val = ucontrol->value.enumerated.item[0];
1943
1944 if (val == spec->aamix_mode)
1945 return 0;
1946 spec->aamix_mode = val;
1947 update_aamix_paths(codec, val, spec->out_paths[0],
1948 spec->aamix_out_paths[0]);
1949 update_aamix_paths(codec, val, spec->hp_paths[0],
1950 spec->aamix_out_paths[1]);
1951 update_aamix_paths(codec, val, spec->speaker_paths[0],
1952 spec->aamix_out_paths[2]);
1953 return 1;
1954}
1955
1956static const struct snd_kcontrol_new loopback_mixing_enum = {
1957 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1958 .name = "Loopback Mixing",
1959 .info = loopback_mixing_info,
1960 .get = loopback_mixing_get,
1961 .put = loopback_mixing_put,
1962};
1963
1964static int create_loopback_mixing_ctl(struct hda_codec *codec)
1965{
1966 struct hda_gen_spec *spec = codec->spec;
1967
1968 if (!spec->mixer_nid)
1969 return 0;
1970 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
1971 spec->aamix_out_paths[2]))
1972 return 0;
1973 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
1974 return -ENOMEM;
1975 return 0;
1976}
1977
1978/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01001979 * shared headphone/mic handling
1980 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02001981
Takashi Iwai352f7f92012-12-19 12:52:06 +01001982static void call_update_outputs(struct hda_codec *codec);
1983
1984/* for shared I/O, change the pin-control accordingly */
1985static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic)
1986{
1987 struct hda_gen_spec *spec = codec->spec;
1988 unsigned int val;
1989 hda_nid_t pin = spec->autocfg.inputs[1].pin;
1990 /* NOTE: this assumes that there are only two inputs, the
1991 * first is the real internal mic and the second is HP/mic jack.
1992 */
1993
1994 val = snd_hda_get_default_vref(codec, pin);
1995
1996 /* This pin does not have vref caps - let's enable vref on pin 0x18
1997 instead, as suggested by Realtek */
1998 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
1999 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2000 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2001 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01002002 snd_hda_set_pin_ctl_cache(codec, vref_pin,
2003 PIN_IN | (set_as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02002004 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002005
2006 val = set_as_mic ? val | PIN_IN : PIN_HP;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002007 set_pin_target(codec, pin, val, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002008
2009 spec->automute_speaker = !set_as_mic;
2010 call_update_outputs(codec);
2011}
2012
2013/* create a shared input with the headphone out */
2014static int create_shared_input(struct hda_codec *codec)
2015{
2016 struct hda_gen_spec *spec = codec->spec;
2017 struct auto_pin_cfg *cfg = &spec->autocfg;
2018 unsigned int defcfg;
2019 hda_nid_t nid;
2020
2021 /* only one internal input pin? */
2022 if (cfg->num_inputs != 1)
2023 return 0;
2024 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2025 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2026 return 0;
2027
2028 if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
2029 nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
2030 else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
2031 nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
2032 else
2033 return 0; /* both not available */
2034
2035 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2036 return 0; /* no input */
2037
2038 cfg->inputs[1].pin = nid;
2039 cfg->inputs[1].type = AUTO_PIN_MIC;
2040 cfg->num_inputs = 2;
2041 spec->shared_mic_hp = 1;
2042 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
2043 return 0;
2044}
2045
Takashi Iwai978e77e2013-01-10 16:57:58 +01002046/*
2047 * output jack mode
2048 */
2049static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2050 struct snd_ctl_elem_info *uinfo)
2051{
2052 static const char * const texts[] = {
2053 "Line Out", "Headphone Out",
2054 };
2055 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, texts);
2056}
2057
2058static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2059 struct snd_ctl_elem_value *ucontrol)
2060{
2061 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2062 hda_nid_t nid = kcontrol->private_value;
2063 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2064 ucontrol->value.enumerated.item[0] = 1;
2065 else
2066 ucontrol->value.enumerated.item[0] = 0;
2067 return 0;
2068}
2069
2070static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2071 struct snd_ctl_elem_value *ucontrol)
2072{
2073 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2074 hda_nid_t nid = kcontrol->private_value;
2075 unsigned int val;
2076
2077 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2078 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2079 return 0;
2080 snd_hda_set_pin_ctl_cache(codec, nid, val);
2081 return 1;
2082}
2083
2084static const struct snd_kcontrol_new out_jack_mode_enum = {
2085 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2086 .info = out_jack_mode_info,
2087 .get = out_jack_mode_get,
2088 .put = out_jack_mode_put,
2089};
2090
2091static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2092{
2093 struct hda_gen_spec *spec = codec->spec;
2094 int i;
2095
2096 for (i = 0; i < spec->kctls.used; i++) {
2097 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2098 if (!strcmp(kctl->name, name) && kctl->index == idx)
2099 return true;
2100 }
2101 return false;
2102}
2103
2104static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2105 char *name, size_t name_len)
2106{
2107 struct hda_gen_spec *spec = codec->spec;
2108 int idx = 0;
2109
2110 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2111 strlcat(name, " Jack Mode", name_len);
2112
2113 for (; find_kctl_name(codec, name, idx); idx++)
2114 ;
2115}
2116
2117static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2118 hda_nid_t *pins)
2119{
2120 struct hda_gen_spec *spec = codec->spec;
2121 int i;
2122
2123 for (i = 0; i < num_pins; i++) {
2124 hda_nid_t pin = pins[i];
2125 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2126 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV)) {
2127 struct snd_kcontrol_new *knew;
2128 char name[44];
2129 get_jack_mode_name(codec, pin, name, sizeof(name));
2130 knew = snd_hda_gen_add_kctl(spec, name,
2131 &out_jack_mode_enum);
2132 if (!knew)
2133 return -ENOMEM;
2134 knew->private_value = pin;
2135 }
2136 }
2137
2138 return 0;
2139}
2140
Takashi Iwai352f7f92012-12-19 12:52:06 +01002141
2142/*
2143 * Parse input paths
2144 */
2145
2146#ifdef CONFIG_PM
2147/* add the powersave loopback-list entry */
2148static void add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
2149{
2150 struct hda_amp_list *list;
2151
2152 if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
2153 return;
2154 list = spec->loopback_list + spec->num_loopbacks;
2155 list->nid = mix;
2156 list->dir = HDA_INPUT;
2157 list->idx = idx;
2158 spec->num_loopbacks++;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002159 spec->loopback.amplist = spec->loopback_list;
2160}
2161#else
Takashi Iwai352f7f92012-12-19 12:52:06 +01002162#define add_loopback_list(spec, mix, idx) /* NOP */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002163#endif
2164
Takashi Iwai352f7f92012-12-19 12:52:06 +01002165/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01002166static int new_analog_input(struct hda_codec *codec, int input_idx,
2167 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002168 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002170 struct hda_gen_spec *spec = codec->spec;
2171 struct nid_path *path;
2172 unsigned int val;
2173 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
Takashi Iwai352f7f92012-12-19 12:52:06 +01002175 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
2176 !nid_has_mute(codec, mix_nid, HDA_INPUT))
2177 return 0; /* no need for analog loopback */
2178
Takashi Iwai3ca529d2013-01-07 17:25:08 +01002179 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002180 if (!path)
2181 return -EINVAL;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002182 print_nid_path("loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01002183 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002184
2185 idx = path->idx[path->depth - 1];
2186 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
2187 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2188 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002189 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002191 path->ctls[NID_PATH_VOL_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 }
2193
Takashi Iwai352f7f92012-12-19 12:52:06 +01002194 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
2195 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2196 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002197 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002199 path->ctls[NID_PATH_MUTE_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 }
2201
Takashi Iwai352f7f92012-12-19 12:52:06 +01002202 path->active = true;
2203 add_loopback_list(spec, mix_nid, idx);
2204 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205}
2206
Takashi Iwai352f7f92012-12-19 12:52:06 +01002207static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002209 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
2210 return (pincap & AC_PINCAP_IN) != 0;
2211}
2212
2213/* Parse the codec tree and retrieve ADCs */
2214static int fill_adc_nids(struct hda_codec *codec)
2215{
2216 struct hda_gen_spec *spec = codec->spec;
2217 hda_nid_t nid;
2218 hda_nid_t *adc_nids = spec->adc_nids;
2219 int max_nums = ARRAY_SIZE(spec->adc_nids);
2220 int i, nums = 0;
2221
2222 nid = codec->start_nid;
2223 for (i = 0; i < codec->num_nodes; i++, nid++) {
2224 unsigned int caps = get_wcaps(codec, nid);
2225 int type = get_wcaps_type(caps);
2226
2227 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2228 continue;
2229 adc_nids[nums] = nid;
2230 if (++nums >= max_nums)
2231 break;
2232 }
2233 spec->num_adc_nids = nums;
2234 return nums;
2235}
2236
2237/* filter out invalid adc_nids that don't give all active input pins;
2238 * if needed, check whether dynamic ADC-switching is available
2239 */
2240static int check_dyn_adc_switch(struct hda_codec *codec)
2241{
2242 struct hda_gen_spec *spec = codec->spec;
2243 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002244 unsigned int ok_bits;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002245 int i, n, nums;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002246
2247 again:
2248 nums = 0;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002249 ok_bits = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002250 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002251 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002252 if (!spec->input_paths[i][n])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002253 break;
2254 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002255 if (i >= imux->num_items) {
2256 ok_bits |= (1 << n);
2257 nums++;
2258 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002259 }
2260
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002261 if (!ok_bits) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002262 if (spec->shared_mic_hp) {
2263 spec->shared_mic_hp = 0;
2264 imux->num_items = 1;
2265 goto again;
2266 }
2267
2268 /* check whether ADC-switch is possible */
2269 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002270 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002271 if (spec->input_paths[i][n]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002272 spec->dyn_adc_idx[i] = n;
2273 break;
2274 }
2275 }
2276 }
2277
2278 snd_printdd("hda-codec: enabling ADC switching\n");
2279 spec->dyn_adc_switch = 1;
2280 } else if (nums != spec->num_adc_nids) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002281 /* shrink the invalid adcs and input paths */
2282 nums = 0;
2283 for (n = 0; n < spec->num_adc_nids; n++) {
2284 if (!(ok_bits & (1 << n)))
2285 continue;
2286 if (n != nums) {
2287 spec->adc_nids[nums] = spec->adc_nids[n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002288 for (i = 0; i < imux->num_items; i++) {
2289 invalidate_nid_path(codec,
2290 spec->input_paths[i][nums]);
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002291 spec->input_paths[i][nums] =
2292 spec->input_paths[i][n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002293 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002294 }
2295 nums++;
2296 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002297 spec->num_adc_nids = nums;
2298 }
2299
2300 if (imux->num_items == 1 || spec->shared_mic_hp) {
2301 snd_printdd("hda-codec: reducing to a single ADC\n");
2302 spec->num_adc_nids = 1; /* reduce to a single ADC */
2303 }
2304
2305 /* single index for individual volumes ctls */
2306 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
2307 spec->num_adc_nids = 1;
2308
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 return 0;
2310}
2311
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002312/* parse capture source paths from the given pin and create imux items */
2313static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
2314 int num_adcs, const char *label, int anchor)
2315{
2316 struct hda_gen_spec *spec = codec->spec;
2317 struct hda_input_mux *imux = &spec->input_mux;
2318 int imux_idx = imux->num_items;
2319 bool imux_added = false;
2320 int c;
2321
2322 for (c = 0; c < num_adcs; c++) {
2323 struct nid_path *path;
2324 hda_nid_t adc = spec->adc_nids[c];
2325
2326 if (!is_reachable_path(codec, pin, adc))
2327 continue;
2328 path = snd_hda_add_new_path(codec, pin, adc, anchor);
2329 if (!path)
2330 continue;
2331 print_nid_path("input", path);
2332 spec->input_paths[imux_idx][c] =
2333 snd_hda_get_path_idx(codec, path);
2334
2335 if (!imux_added) {
2336 spec->imux_pins[imux->num_items] = pin;
2337 snd_hda_add_imux_item(imux, label,
2338 imux->num_items, NULL);
2339 imux_added = true;
2340 }
2341 }
2342
2343 return 0;
2344}
2345
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002347 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002349static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002350{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002351 struct hda_gen_spec *spec = codec->spec;
2352 const struct auto_pin_cfg *cfg = &spec->autocfg;
2353 hda_nid_t mixer = spec->mixer_nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002354 int num_adcs;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002355 int i, err, type_idx = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002356 const char *prev_label = NULL;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002357 unsigned int val;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002358
Takashi Iwai352f7f92012-12-19 12:52:06 +01002359 num_adcs = fill_adc_nids(codec);
2360 if (num_adcs < 0)
2361 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002362
Takashi Iwai352f7f92012-12-19 12:52:06 +01002363 for (i = 0; i < cfg->num_inputs; i++) {
2364 hda_nid_t pin;
2365 const char *label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
Takashi Iwai352f7f92012-12-19 12:52:06 +01002367 pin = cfg->inputs[i].pin;
2368 if (!is_input_pin(codec, pin))
2369 continue;
2370
2371 label = hda_get_autocfg_input_label(codec, cfg, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002372 if (prev_label && !strcmp(label, prev_label))
2373 type_idx++;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002374 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01002375 type_idx = 0;
2376 prev_label = label;
2377
Takashi Iwai2c12c302013-01-10 09:33:29 +01002378 val = PIN_IN;
2379 if (cfg->inputs[i].type == AUTO_PIN_MIC)
2380 val |= snd_hda_get_default_vref(codec, pin);
2381 set_pin_target(codec, pin, val, false);
2382
Takashi Iwai352f7f92012-12-19 12:52:06 +01002383 if (mixer) {
2384 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01002385 err = new_analog_input(codec, i, pin,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002386 label, type_idx, mixer);
2387 if (err < 0)
2388 return err;
2389 }
2390 }
2391
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002392 err = parse_capture_source(codec, pin, num_adcs, label, -mixer);
2393 if (err < 0)
2394 return err;
2395 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002396
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002397 if (mixer && spec->add_stereo_mix_input) {
2398 err = parse_capture_source(codec, mixer, num_adcs,
2399 "Stereo Mix", 0);
2400 if (err < 0)
2401 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002402 }
2403
2404 return 0;
2405}
2406
2407
2408/*
2409 * input source mux
2410 */
2411
Takashi Iwaic697b712013-01-07 17:09:26 +01002412/* get the input path specified by the given adc and imux indices */
2413static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002414{
2415 struct hda_gen_spec *spec = codec->spec;
David Henningssonb56fa1e2013-01-16 11:45:35 +01002416 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
2417 snd_BUG();
2418 return NULL;
2419 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002420 if (spec->dyn_adc_switch)
2421 adc_idx = spec->dyn_adc_idx[imux_idx];
David Henningssonb56fa1e2013-01-16 11:45:35 +01002422 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_OUTS) {
2423 snd_BUG();
2424 return NULL;
2425 }
Takashi Iwaic697b712013-01-07 17:09:26 +01002426 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002427}
2428
2429static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2430 unsigned int idx);
2431
2432static int mux_enum_info(struct snd_kcontrol *kcontrol,
2433 struct snd_ctl_elem_info *uinfo)
2434{
2435 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2436 struct hda_gen_spec *spec = codec->spec;
2437 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
2438}
2439
2440static int mux_enum_get(struct snd_kcontrol *kcontrol,
2441 struct snd_ctl_elem_value *ucontrol)
2442{
2443 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2444 struct hda_gen_spec *spec = codec->spec;
David Henningssona053d1e2013-01-16 11:45:36 +01002445 unsigned int adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002446
2447 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
2448 return 0;
2449}
2450
2451static int mux_enum_put(struct snd_kcontrol *kcontrol,
2452 struct snd_ctl_elem_value *ucontrol)
2453{
2454 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
David Henningssona053d1e2013-01-16 11:45:36 +01002455 unsigned int adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002456 return mux_select(codec, adc_idx,
2457 ucontrol->value.enumerated.item[0]);
2458}
2459
Takashi Iwai352f7f92012-12-19 12:52:06 +01002460static const struct snd_kcontrol_new cap_src_temp = {
2461 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2462 .name = "Input Source",
2463 .info = mux_enum_info,
2464 .get = mux_enum_get,
2465 .put = mux_enum_put,
2466};
2467
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002468/*
2469 * capture volume and capture switch ctls
2470 */
2471
Takashi Iwai352f7f92012-12-19 12:52:06 +01002472typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
2473 struct snd_ctl_elem_value *ucontrol);
2474
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002475/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002476static int cap_put_caller(struct snd_kcontrol *kcontrol,
2477 struct snd_ctl_elem_value *ucontrol,
2478 put_call_t func, int type)
2479{
2480 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2481 struct hda_gen_spec *spec = codec->spec;
2482 const struct hda_input_mux *imux;
2483 struct nid_path *path;
2484 int i, adc_idx, err = 0;
2485
2486 imux = &spec->input_mux;
David Henningssona053d1e2013-01-16 11:45:36 +01002487 adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002488 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002489 /* we use the cache-only update at first since multiple input paths
2490 * may shared the same amp; by updating only caches, the redundant
2491 * writes to hardware can be reduced.
2492 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002493 codec->cached_write = 1;
2494 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01002495 path = get_input_path(codec, adc_idx, i);
2496 if (!path || !path->ctls[type])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002497 continue;
2498 kcontrol->private_value = path->ctls[type];
2499 err = func(kcontrol, ucontrol);
2500 if (err < 0)
2501 goto error;
2502 }
2503 error:
2504 codec->cached_write = 0;
2505 mutex_unlock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002506 snd_hda_codec_flush_amp_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002507 if (err >= 0 && spec->cap_sync_hook)
2508 spec->cap_sync_hook(codec);
2509 return err;
2510}
2511
2512/* capture volume ctl callbacks */
2513#define cap_vol_info snd_hda_mixer_amp_volume_info
2514#define cap_vol_get snd_hda_mixer_amp_volume_get
2515#define cap_vol_tlv snd_hda_mixer_amp_tlv
2516
2517static int cap_vol_put(struct snd_kcontrol *kcontrol,
2518 struct snd_ctl_elem_value *ucontrol)
2519{
2520 return cap_put_caller(kcontrol, ucontrol,
2521 snd_hda_mixer_amp_volume_put,
2522 NID_PATH_VOL_CTL);
2523}
2524
2525static const struct snd_kcontrol_new cap_vol_temp = {
2526 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2527 .name = "Capture Volume",
2528 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
2529 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2530 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
2531 .info = cap_vol_info,
2532 .get = cap_vol_get,
2533 .put = cap_vol_put,
2534 .tlv = { .c = cap_vol_tlv },
2535};
2536
2537/* capture switch ctl callbacks */
2538#define cap_sw_info snd_ctl_boolean_stereo_info
2539#define cap_sw_get snd_hda_mixer_amp_switch_get
2540
2541static int cap_sw_put(struct snd_kcontrol *kcontrol,
2542 struct snd_ctl_elem_value *ucontrol)
2543{
Takashi Iwaiae177c32013-01-14 12:13:06 +01002544 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2545 struct hda_gen_spec *spec = codec->spec;
2546 int ret;
2547
2548 ret = cap_put_caller(kcontrol, ucontrol,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002549 snd_hda_mixer_amp_switch_put,
2550 NID_PATH_MUTE_CTL);
Takashi Iwaiae177c32013-01-14 12:13:06 +01002551 if (ret < 0)
2552 return ret;
2553
2554 if (spec->capture_switch_hook) {
2555 bool enable = (ucontrol->value.integer.value[0] ||
2556 ucontrol->value.integer.value[1]);
2557 spec->capture_switch_hook(codec, enable);
2558 }
2559
2560 return ret;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002561}
2562
2563static const struct snd_kcontrol_new cap_sw_temp = {
2564 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2565 .name = "Capture Switch",
2566 .info = cap_sw_info,
2567 .get = cap_sw_get,
2568 .put = cap_sw_put,
2569};
2570
2571static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
2572{
2573 hda_nid_t nid;
2574 int i, depth;
2575
2576 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
2577 for (depth = 0; depth < 3; depth++) {
2578 if (depth >= path->depth)
2579 return -EINVAL;
2580 i = path->depth - depth - 1;
2581 nid = path->path[i];
2582 if (!path->ctls[NID_PATH_VOL_CTL]) {
2583 if (nid_has_volume(codec, nid, HDA_OUTPUT))
2584 path->ctls[NID_PATH_VOL_CTL] =
2585 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2586 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
2587 int idx = path->idx[i];
2588 if (!depth && codec->single_adc_amp)
2589 idx = 0;
2590 path->ctls[NID_PATH_VOL_CTL] =
2591 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2592 }
2593 }
2594 if (!path->ctls[NID_PATH_MUTE_CTL]) {
2595 if (nid_has_mute(codec, nid, HDA_OUTPUT))
2596 path->ctls[NID_PATH_MUTE_CTL] =
2597 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2598 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
2599 int idx = path->idx[i];
2600 if (!depth && codec->single_adc_amp)
2601 idx = 0;
2602 path->ctls[NID_PATH_MUTE_CTL] =
2603 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2604 }
2605 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01002606 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 return 0;
2608}
2609
Takashi Iwai352f7f92012-12-19 12:52:06 +01002610static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002612 struct hda_gen_spec *spec = codec->spec;
2613 struct auto_pin_cfg *cfg = &spec->autocfg;
2614 unsigned int val;
2615 int i;
2616
2617 if (!spec->inv_dmic_split)
2618 return false;
2619 for (i = 0; i < cfg->num_inputs; i++) {
2620 if (cfg->inputs[i].pin != nid)
2621 continue;
2622 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2623 return false;
2624 val = snd_hda_codec_get_pincfg(codec, nid);
2625 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
2626 }
2627 return false;
2628}
2629
2630static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
2631 int idx, bool is_switch, unsigned int ctl,
2632 bool inv_dmic)
2633{
2634 struct hda_gen_spec *spec = codec->spec;
2635 char tmpname[44];
2636 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
2637 const char *sfx = is_switch ? "Switch" : "Volume";
2638 unsigned int chs = inv_dmic ? 1 : 3;
2639 int err;
2640
2641 if (!ctl)
2642 return 0;
2643
2644 if (label)
2645 snprintf(tmpname, sizeof(tmpname),
2646 "%s Capture %s", label, sfx);
2647 else
2648 snprintf(tmpname, sizeof(tmpname),
2649 "Capture %s", sfx);
2650 err = add_control(spec, type, tmpname, idx,
2651 amp_val_replace_channels(ctl, chs));
2652 if (err < 0 || !inv_dmic)
2653 return err;
2654
2655 /* Make independent right kcontrol */
2656 if (label)
2657 snprintf(tmpname, sizeof(tmpname),
2658 "Inverted %s Capture %s", label, sfx);
2659 else
2660 snprintf(tmpname, sizeof(tmpname),
2661 "Inverted Capture %s", sfx);
2662 return add_control(spec, type, tmpname, idx,
2663 amp_val_replace_channels(ctl, 2));
2664}
2665
2666/* create single (and simple) capture volume and switch controls */
2667static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
2668 unsigned int vol_ctl, unsigned int sw_ctl,
2669 bool inv_dmic)
2670{
2671 int err;
2672 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
2673 if (err < 0)
2674 return err;
2675 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
2676 if (err < 0)
2677 return err;
2678 return 0;
2679}
2680
2681/* create bound capture volume and switch controls */
2682static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
2683 unsigned int vol_ctl, unsigned int sw_ctl)
2684{
2685 struct hda_gen_spec *spec = codec->spec;
2686 struct snd_kcontrol_new *knew;
2687
2688 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002689 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002690 if (!knew)
2691 return -ENOMEM;
2692 knew->index = idx;
2693 knew->private_value = vol_ctl;
2694 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2695 }
2696 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002697 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002698 if (!knew)
2699 return -ENOMEM;
2700 knew->index = idx;
2701 knew->private_value = sw_ctl;
2702 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2703 }
2704 return 0;
2705}
2706
2707/* return the vol ctl when used first in the imux list */
2708static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
2709{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002710 struct nid_path *path;
2711 unsigned int ctl;
2712 int i;
2713
Takashi Iwaic697b712013-01-07 17:09:26 +01002714 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002715 if (!path)
2716 return 0;
2717 ctl = path->ctls[type];
2718 if (!ctl)
2719 return 0;
2720 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01002721 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002722 if (path && path->ctls[type] == ctl)
2723 return 0;
2724 }
2725 return ctl;
2726}
2727
2728/* create individual capture volume and switch controls per input */
2729static int create_multi_cap_vol_ctl(struct hda_codec *codec)
2730{
2731 struct hda_gen_spec *spec = codec->spec;
2732 struct hda_input_mux *imux = &spec->input_mux;
2733 int i, err, type, type_idx = 0;
2734 const char *prev_label = NULL;
2735
2736 for (i = 0; i < imux->num_items; i++) {
2737 const char *label;
2738 bool inv_dmic;
2739 label = hda_get_autocfg_input_label(codec, &spec->autocfg, i);
2740 if (prev_label && !strcmp(label, prev_label))
2741 type_idx++;
2742 else
2743 type_idx = 0;
2744 prev_label = label;
2745 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
2746
2747 for (type = 0; type < 2; type++) {
2748 err = add_single_cap_ctl(codec, label, type_idx, type,
2749 get_first_cap_ctl(codec, i, type),
2750 inv_dmic);
2751 if (err < 0)
2752 return err;
2753 }
2754 }
2755 return 0;
2756}
2757
2758static int create_capture_mixers(struct hda_codec *codec)
2759{
2760 struct hda_gen_spec *spec = codec->spec;
2761 struct hda_input_mux *imux = &spec->input_mux;
2762 int i, n, nums, err;
2763
2764 if (spec->dyn_adc_switch)
2765 nums = 1;
2766 else
2767 nums = spec->num_adc_nids;
2768
2769 if (!spec->auto_mic && imux->num_items > 1) {
2770 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01002771 const char *name;
2772 name = nums > 1 ? "Input Source" : "Capture Source";
2773 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002774 if (!knew)
2775 return -ENOMEM;
2776 knew->count = nums;
2777 }
2778
2779 for (n = 0; n < nums; n++) {
2780 bool multi = false;
David Henningsson99a55922013-01-16 15:58:44 +01002781 bool multi_cap_vol = spec->multi_cap_vol;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002782 bool inv_dmic = false;
2783 int vol, sw;
2784
2785 vol = sw = 0;
2786 for (i = 0; i < imux->num_items; i++) {
2787 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01002788 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002789 if (!path)
2790 continue;
2791 parse_capvol_in_path(codec, path);
2792 if (!vol)
2793 vol = path->ctls[NID_PATH_VOL_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01002794 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002795 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01002796 if (!same_amp_caps(codec, vol,
2797 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
2798 multi_cap_vol = true;
2799 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002800 if (!sw)
2801 sw = path->ctls[NID_PATH_MUTE_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01002802 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002803 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01002804 if (!same_amp_caps(codec, sw,
2805 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
2806 multi_cap_vol = true;
2807 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002808 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
2809 inv_dmic = true;
2810 }
2811
2812 if (!multi)
2813 err = create_single_cap_vol_ctl(codec, n, vol, sw,
2814 inv_dmic);
David Henningsson99a55922013-01-16 15:58:44 +01002815 else if (!multi_cap_vol)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002816 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
2817 else
2818 err = create_multi_cap_vol_ctl(codec);
2819 if (err < 0)
2820 return err;
2821 }
2822
2823 return 0;
2824}
2825
2826/*
2827 * add mic boosts if needed
2828 */
2829static int parse_mic_boost(struct hda_codec *codec)
2830{
2831 struct hda_gen_spec *spec = codec->spec;
2832 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002833 int i, err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002834 int type_idx = 0;
2835 hda_nid_t nid;
2836 const char *prev_label = NULL;
2837
2838 for (i = 0; i < cfg->num_inputs; i++) {
2839 if (cfg->inputs[i].type > AUTO_PIN_MIC)
2840 break;
2841 nid = cfg->inputs[i].pin;
2842 if (get_wcaps(codec, nid) & AC_WCAP_IN_AMP) {
2843 const char *label;
Takashi Iwai5abd4882013-01-07 09:43:18 +01002844 char boost_label[44];
Takashi Iwai352f7f92012-12-19 12:52:06 +01002845 struct nid_path *path;
2846 unsigned int val;
2847
David Henningsson02aba552013-01-16 15:58:43 +01002848 if (!nid_has_volume(codec, nid, HDA_INPUT))
2849 continue;
2850
Takashi Iwai352f7f92012-12-19 12:52:06 +01002851 label = hda_get_autocfg_input_label(codec, cfg, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002852 if (prev_label && !strcmp(label, prev_label))
2853 type_idx++;
2854 else
2855 type_idx = 0;
2856 prev_label = label;
2857
2858 snprintf(boost_label, sizeof(boost_label),
2859 "%s Boost Volume", label);
2860 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
2861 err = add_control(spec, HDA_CTL_WIDGET_VOL,
2862 boost_label, type_idx, val);
2863 if (err < 0)
2864 return err;
2865
2866 path = snd_hda_get_nid_path(codec, nid, 0);
2867 if (path)
2868 path->ctls[NID_PATH_BOOST_CTL] = val;
2869 }
2870 }
2871 return 0;
2872}
2873
2874/*
2875 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
2876 */
2877static void parse_digital(struct hda_codec *codec)
2878{
2879 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002880 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002881 int i, nums;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002882 hda_nid_t dig_nid, pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002883
2884 /* support multiple SPDIFs; the secondary is set up as a slave */
2885 nums = 0;
2886 for (i = 0; i < spec->autocfg.dig_outs; i++) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01002887 pin = spec->autocfg.dig_out_pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01002888 dig_nid = look_for_dac(codec, pin, true);
2889 if (!dig_nid)
2890 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01002891 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002892 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002893 continue;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002894 print_nid_path("digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01002895 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01002896 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01002897 set_pin_target(codec, pin, PIN_OUT, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002898 if (!nums) {
2899 spec->multiout.dig_out_nid = dig_nid;
2900 spec->dig_out_type = spec->autocfg.dig_out_type[0];
2901 } else {
2902 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
2903 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
2904 break;
2905 spec->slave_dig_outs[nums - 1] = dig_nid;
2906 }
2907 nums++;
2908 }
2909
2910 if (spec->autocfg.dig_in_pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01002911 pin = spec->autocfg.dig_in_pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002912 dig_nid = codec->start_nid;
2913 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002914 unsigned int wcaps = get_wcaps(codec, dig_nid);
2915 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
2916 continue;
2917 if (!(wcaps & AC_WCAP_DIGITAL))
2918 continue;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002919 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002920 if (path) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002921 print_nid_path("digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002922 path->active = true;
2923 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01002924 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01002925 set_pin_target(codec, pin, PIN_IN, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002926 break;
2927 }
2928 }
2929 }
2930}
2931
2932
2933/*
2934 * input MUX handling
2935 */
2936
2937static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
2938
2939/* select the given imux item; either unmute exclusively or select the route */
2940static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2941 unsigned int idx)
2942{
2943 struct hda_gen_spec *spec = codec->spec;
2944 const struct hda_input_mux *imux;
2945 struct nid_path *path;
2946
2947 imux = &spec->input_mux;
2948 if (!imux->num_items)
2949 return 0;
2950
2951 if (idx >= imux->num_items)
2952 idx = imux->num_items - 1;
2953 if (spec->cur_mux[adc_idx] == idx)
2954 return 0;
2955
Takashi Iwaic697b712013-01-07 17:09:26 +01002956 path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002957 if (!path)
2958 return 0;
2959 if (path->active)
2960 snd_hda_activate_path(codec, path, false, false);
2961
2962 spec->cur_mux[adc_idx] = idx;
2963
2964 if (spec->shared_mic_hp)
2965 update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
2966
2967 if (spec->dyn_adc_switch)
2968 dyn_adc_pcm_resetup(codec, idx);
2969
Takashi Iwaic697b712013-01-07 17:09:26 +01002970 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002971 if (!path)
2972 return 0;
2973 if (path->active)
2974 return 0;
2975 snd_hda_activate_path(codec, path, true, false);
2976 if (spec->cap_sync_hook)
2977 spec->cap_sync_hook(codec);
2978 return 1;
2979}
2980
2981
2982/*
2983 * Jack detections for HP auto-mute and mic-switch
2984 */
2985
2986/* check each pin in the given array; returns true if any of them is plugged */
2987static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
2988{
2989 int i, present = 0;
2990
2991 for (i = 0; i < num_pins; i++) {
2992 hda_nid_t nid = pins[i];
2993 if (!nid)
2994 break;
Takashi Iwai0b4df932013-01-10 09:45:13 +01002995 /* don't detect pins retasked as inputs */
2996 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
2997 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002998 present |= snd_hda_jack_detect(codec, nid);
2999 }
3000 return present;
3001}
3002
3003/* standard HP/line-out auto-mute helper */
3004static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
Takashi Iwai2c12c302013-01-10 09:33:29 +01003005 bool mute)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003006{
3007 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003008 int i;
3009
3010 for (i = 0; i < num_pins; i++) {
3011 hda_nid_t nid = pins[i];
3012 unsigned int val;
3013 if (!nid)
3014 break;
3015 /* don't reset VREF value in case it's controlling
3016 * the amp (see alc861_fixup_asus_amp_vref_0f())
3017 */
Takashi Iwai2c12c302013-01-10 09:33:29 +01003018 if (spec->keep_vref_in_automute)
3019 val = snd_hda_codec_get_pin_target(codec, nid) & ~PIN_HP;
3020 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01003021 val = 0;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003022 if (!mute)
3023 val |= snd_hda_codec_get_pin_target(codec, nid);
3024 /* here we call update_pin_ctl() so that the pinctl is changed
3025 * without changing the pinctl target value;
3026 * the original target value will be still referred at the
3027 * init / resume again
3028 */
3029 update_pin_ctl(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01003030 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003031 }
3032}
3033
3034/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003035void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003036{
3037 struct hda_gen_spec *spec = codec->spec;
3038 int on;
3039
3040 /* Control HP pins/amps depending on master_mute state;
3041 * in general, HP pins/amps control should be enabled in all cases,
3042 * but currently set only for master_mute, just to be safe
3043 */
3044 if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
3045 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003046 spec->autocfg.hp_pins, spec->master_mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003047
3048 if (!spec->automute_speaker)
3049 on = 0;
3050 else
3051 on = spec->hp_jack_present | spec->line_jack_present;
3052 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003053 spec->speaker_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003054 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003055 spec->autocfg.speaker_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003056
3057 /* toggle line-out mutes if needed, too */
3058 /* if LO is a copy of either HP or Speaker, don't need to handle it */
3059 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
3060 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
3061 return;
3062 if (!spec->automute_lo)
3063 on = 0;
3064 else
3065 on = spec->hp_jack_present;
3066 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003067 spec->line_out_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003068 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003069 spec->autocfg.line_out_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003070}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003071EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003072
3073static void call_update_outputs(struct hda_codec *codec)
3074{
3075 struct hda_gen_spec *spec = codec->spec;
3076 if (spec->automute_hook)
3077 spec->automute_hook(codec);
3078 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01003079 snd_hda_gen_update_outputs(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003080}
3081
3082/* standard HP-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003083void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003084{
3085 struct hda_gen_spec *spec = codec->spec;
3086
3087 spec->hp_jack_present =
3088 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
3089 spec->autocfg.hp_pins);
3090 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
3091 return;
3092 call_update_outputs(codec);
3093}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003094EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003095
3096/* standard line-out-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003097void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003098{
3099 struct hda_gen_spec *spec = codec->spec;
3100
3101 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3102 return;
3103 /* check LO jack only when it's different from HP */
3104 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
3105 return;
3106
3107 spec->line_jack_present =
3108 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3109 spec->autocfg.line_out_pins);
3110 if (!spec->automute_speaker || !spec->detect_lo)
3111 return;
3112 call_update_outputs(codec);
3113}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003114EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003115
3116/* standard mic auto-switch helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003117void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003118{
3119 struct hda_gen_spec *spec = codec->spec;
3120 int i;
3121
3122 if (!spec->auto_mic)
3123 return;
3124
3125 for (i = spec->am_num_entries - 1; i > 0; i--) {
Takashi Iwai0b4df932013-01-10 09:45:13 +01003126 hda_nid_t pin = spec->am_entry[i].pin;
3127 /* don't detect pins retasked as outputs */
3128 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
3129 continue;
3130 if (snd_hda_jack_detect(codec, pin)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003131 mux_select(codec, 0, spec->am_entry[i].idx);
3132 return;
3133 }
3134 }
3135 mux_select(codec, 0, spec->am_entry[0].idx);
3136}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003137EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003138
Takashi Iwaia5cc2502013-01-16 18:08:55 +01003139/* update jack retasking */
3140static void update_automute_all(struct hda_codec *codec)
3141{
3142 struct hda_gen_spec *spec = codec->spec;
3143
3144 if (spec->hp_automute_hook)
3145 spec->hp_automute_hook(codec, NULL);
3146 else
3147 snd_hda_gen_hp_automute(codec, NULL);
3148 if (spec->line_automute_hook)
3149 spec->line_automute_hook(codec, NULL);
3150 else
3151 snd_hda_gen_line_automute(codec, NULL);
3152 if (spec->mic_autoswitch_hook)
3153 spec->mic_autoswitch_hook(codec, NULL);
3154 else
3155 snd_hda_gen_mic_autoswitch(codec, NULL);
3156}
3157
Takashi Iwai352f7f92012-12-19 12:52:06 +01003158/*
3159 * Auto-Mute mode mixer enum support
3160 */
3161static int automute_mode_info(struct snd_kcontrol *kcontrol,
3162 struct snd_ctl_elem_info *uinfo)
3163{
3164 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3165 struct hda_gen_spec *spec = codec->spec;
3166 static const char * const texts3[] = {
3167 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02003168 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169
Takashi Iwai352f7f92012-12-19 12:52:06 +01003170 if (spec->automute_speaker_possible && spec->automute_lo_possible)
3171 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
3172 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
3173}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174
Takashi Iwai352f7f92012-12-19 12:52:06 +01003175static int automute_mode_get(struct snd_kcontrol *kcontrol,
3176 struct snd_ctl_elem_value *ucontrol)
3177{
3178 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3179 struct hda_gen_spec *spec = codec->spec;
3180 unsigned int val = 0;
3181 if (spec->automute_speaker)
3182 val++;
3183 if (spec->automute_lo)
3184 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003185
Takashi Iwai352f7f92012-12-19 12:52:06 +01003186 ucontrol->value.enumerated.item[0] = val;
3187 return 0;
3188}
3189
3190static int automute_mode_put(struct snd_kcontrol *kcontrol,
3191 struct snd_ctl_elem_value *ucontrol)
3192{
3193 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3194 struct hda_gen_spec *spec = codec->spec;
3195
3196 switch (ucontrol->value.enumerated.item[0]) {
3197 case 0:
3198 if (!spec->automute_speaker && !spec->automute_lo)
3199 return 0;
3200 spec->automute_speaker = 0;
3201 spec->automute_lo = 0;
3202 break;
3203 case 1:
3204 if (spec->automute_speaker_possible) {
3205 if (!spec->automute_lo && spec->automute_speaker)
3206 return 0;
3207 spec->automute_speaker = 1;
3208 spec->automute_lo = 0;
3209 } else if (spec->automute_lo_possible) {
3210 if (spec->automute_lo)
3211 return 0;
3212 spec->automute_lo = 1;
3213 } else
3214 return -EINVAL;
3215 break;
3216 case 2:
3217 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
3218 return -EINVAL;
3219 if (spec->automute_speaker && spec->automute_lo)
3220 return 0;
3221 spec->automute_speaker = 1;
3222 spec->automute_lo = 1;
3223 break;
3224 default:
3225 return -EINVAL;
3226 }
3227 call_update_outputs(codec);
3228 return 1;
3229}
3230
3231static const struct snd_kcontrol_new automute_mode_enum = {
3232 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3233 .name = "Auto-Mute Mode",
3234 .info = automute_mode_info,
3235 .get = automute_mode_get,
3236 .put = automute_mode_put,
3237};
3238
3239static int add_automute_mode_enum(struct hda_codec *codec)
3240{
3241 struct hda_gen_spec *spec = codec->spec;
3242
Takashi Iwai12c93df2012-12-19 14:38:33 +01003243 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01003244 return -ENOMEM;
3245 return 0;
3246}
3247
3248/*
3249 * Check the availability of HP/line-out auto-mute;
3250 * Set up appropriately if really supported
3251 */
3252static int check_auto_mute_availability(struct hda_codec *codec)
3253{
3254 struct hda_gen_spec *spec = codec->spec;
3255 struct auto_pin_cfg *cfg = &spec->autocfg;
3256 int present = 0;
3257 int i, err;
3258
Takashi Iwaif72706b2013-01-16 18:20:07 +01003259 if (spec->suppress_auto_mute)
3260 return 0;
3261
Takashi Iwai352f7f92012-12-19 12:52:06 +01003262 if (cfg->hp_pins[0])
3263 present++;
3264 if (cfg->line_out_pins[0])
3265 present++;
3266 if (cfg->speaker_pins[0])
3267 present++;
3268 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02003269 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003270
3271 if (!cfg->speaker_pins[0] &&
3272 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
3273 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3274 sizeof(cfg->speaker_pins));
3275 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277
Takashi Iwai352f7f92012-12-19 12:52:06 +01003278 if (!cfg->hp_pins[0] &&
3279 cfg->line_out_type == AUTO_PIN_HP_OUT) {
3280 memcpy(cfg->hp_pins, cfg->line_out_pins,
3281 sizeof(cfg->hp_pins));
3282 cfg->hp_outs = cfg->line_outs;
3283 }
3284
3285 for (i = 0; i < cfg->hp_outs; i++) {
3286 hda_nid_t nid = cfg->hp_pins[i];
3287 if (!is_jack_detectable(codec, nid))
3288 continue;
3289 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
3290 nid);
3291 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003292 spec->hp_automute_hook ?
3293 spec->hp_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003294 snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003295 spec->detect_hp = 1;
3296 }
3297
3298 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
3299 if (cfg->speaker_outs)
3300 for (i = 0; i < cfg->line_outs; i++) {
3301 hda_nid_t nid = cfg->line_out_pins[i];
3302 if (!is_jack_detectable(codec, nid))
3303 continue;
3304 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
3305 snd_hda_jack_detect_enable_callback(codec, nid,
3306 HDA_GEN_FRONT_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003307 spec->line_automute_hook ?
3308 spec->line_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003309 snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003310 spec->detect_lo = 1;
3311 }
3312 spec->automute_lo_possible = spec->detect_hp;
3313 }
3314
3315 spec->automute_speaker_possible = cfg->speaker_outs &&
3316 (spec->detect_hp || spec->detect_lo);
3317
3318 spec->automute_lo = spec->automute_lo_possible;
3319 spec->automute_speaker = spec->automute_speaker_possible;
3320
3321 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
3322 /* create a control for automute mode */
3323 err = add_automute_mode_enum(codec);
3324 if (err < 0)
3325 return err;
3326 }
3327 return 0;
3328}
3329
Takashi Iwai352f7f92012-12-19 12:52:06 +01003330/* check whether all auto-mic pins are valid; setup indices if OK */
3331static bool auto_mic_check_imux(struct hda_codec *codec)
3332{
3333 struct hda_gen_spec *spec = codec->spec;
3334 const struct hda_input_mux *imux;
3335 int i;
3336
3337 imux = &spec->input_mux;
3338 for (i = 0; i < spec->am_num_entries; i++) {
3339 spec->am_entry[i].idx =
3340 find_idx_in_nid_list(spec->am_entry[i].pin,
3341 spec->imux_pins, imux->num_items);
3342 if (spec->am_entry[i].idx < 0)
3343 return false; /* no corresponding imux */
3344 }
3345
3346 /* we don't need the jack detection for the first pin */
3347 for (i = 1; i < spec->am_num_entries; i++)
3348 snd_hda_jack_detect_enable_callback(codec,
3349 spec->am_entry[i].pin,
3350 HDA_GEN_MIC_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003351 spec->mic_autoswitch_hook ?
3352 spec->mic_autoswitch_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003353 snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003354 return true;
3355}
3356
3357static int compare_attr(const void *ap, const void *bp)
3358{
3359 const struct automic_entry *a = ap;
3360 const struct automic_entry *b = bp;
3361 return (int)(a->attr - b->attr);
3362}
3363
3364/*
3365 * Check the availability of auto-mic switch;
3366 * Set up if really supported
3367 */
3368static int check_auto_mic_availability(struct hda_codec *codec)
3369{
3370 struct hda_gen_spec *spec = codec->spec;
3371 struct auto_pin_cfg *cfg = &spec->autocfg;
3372 unsigned int types;
3373 int i, num_pins;
3374
Takashi Iwaid12daf62013-01-07 16:32:11 +01003375 if (spec->suppress_auto_mic)
3376 return 0;
3377
Takashi Iwai352f7f92012-12-19 12:52:06 +01003378 types = 0;
3379 num_pins = 0;
3380 for (i = 0; i < cfg->num_inputs; i++) {
3381 hda_nid_t nid = cfg->inputs[i].pin;
3382 unsigned int attr;
3383 attr = snd_hda_codec_get_pincfg(codec, nid);
3384 attr = snd_hda_get_input_pin_attr(attr);
3385 if (types & (1 << attr))
3386 return 0; /* already occupied */
3387 switch (attr) {
3388 case INPUT_PIN_ATTR_INT:
3389 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3390 return 0; /* invalid type */
3391 break;
3392 case INPUT_PIN_ATTR_UNUSED:
3393 return 0; /* invalid entry */
3394 default:
3395 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
3396 return 0; /* invalid type */
3397 if (!spec->line_in_auto_switch &&
3398 cfg->inputs[i].type != AUTO_PIN_MIC)
3399 return 0; /* only mic is allowed */
3400 if (!is_jack_detectable(codec, nid))
3401 return 0; /* no unsol support */
3402 break;
3403 }
3404 if (num_pins >= MAX_AUTO_MIC_PINS)
3405 return 0;
3406 types |= (1 << attr);
3407 spec->am_entry[num_pins].pin = nid;
3408 spec->am_entry[num_pins].attr = attr;
3409 num_pins++;
3410 }
3411
3412 if (num_pins < 2)
3413 return 0;
3414
3415 spec->am_num_entries = num_pins;
3416 /* sort the am_entry in the order of attr so that the pin with a
3417 * higher attr will be selected when the jack is plugged.
3418 */
3419 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
3420 compare_attr, NULL);
3421
3422 if (!auto_mic_check_imux(codec))
3423 return 0;
3424
3425 spec->auto_mic = 1;
3426 spec->num_adc_nids = 1;
3427 spec->cur_mux[0] = spec->am_entry[0].idx;
3428 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
3429 spec->am_entry[0].pin,
3430 spec->am_entry[1].pin,
3431 spec->am_entry[2].pin);
3432
3433 return 0;
3434}
3435
3436
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003437/*
3438 * Parse the given BIOS configuration and set up the hda_gen_spec
3439 *
3440 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003441 * or a negative error code
3442 */
3443int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003444 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003445{
3446 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003447 int err;
3448
Takashi Iwai1c70a582013-01-11 17:48:22 +01003449 parse_user_hints(codec);
3450
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003451 if (cfg != &spec->autocfg) {
3452 spec->autocfg = *cfg;
3453 cfg = &spec->autocfg;
3454 }
3455
David Henningsson6fc4cb92013-01-16 15:58:45 +01003456 fill_all_dac_nids(codec);
3457
Takashi Iwai352f7f92012-12-19 12:52:06 +01003458 if (!cfg->line_outs) {
3459 if (cfg->dig_outs || cfg->dig_in_pin) {
3460 spec->multiout.max_channels = 2;
3461 spec->no_analog = 1;
3462 goto dig_only;
3463 }
3464 return 0; /* can't find valid BIOS pin config */
3465 }
3466
3467 if (!spec->no_primary_hp &&
3468 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
3469 cfg->line_outs <= cfg->hp_outs) {
3470 /* use HP as primary out */
3471 cfg->speaker_outs = cfg->line_outs;
3472 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3473 sizeof(cfg->speaker_pins));
3474 cfg->line_outs = cfg->hp_outs;
3475 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
3476 cfg->hp_outs = 0;
3477 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3478 cfg->line_out_type = AUTO_PIN_HP_OUT;
3479 }
3480
3481 err = parse_output_paths(codec);
3482 if (err < 0)
3483 return err;
3484 err = create_multi_channel_mode(codec);
3485 if (err < 0)
3486 return err;
3487 err = create_multi_out_ctls(codec, cfg);
3488 if (err < 0)
3489 return err;
3490 err = create_hp_out_ctls(codec);
3491 if (err < 0)
3492 return err;
3493 err = create_speaker_out_ctls(codec);
3494 if (err < 0)
3495 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003496 err = create_indep_hp_ctls(codec);
3497 if (err < 0)
3498 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01003499 err = create_loopback_mixing_ctl(codec);
3500 if (err < 0)
3501 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003502 err = create_shared_input(codec);
3503 if (err < 0)
3504 return err;
3505 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003506 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02003507 return err;
3508
Takashi Iwaia07a9492013-01-07 16:44:06 +01003509 spec->const_channel_count = spec->ext_channel_count;
3510 /* check the multiple speaker and headphone pins */
3511 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
3512 spec->const_channel_count = max(spec->const_channel_count,
3513 cfg->speaker_outs * 2);
3514 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
3515 spec->const_channel_count = max(spec->const_channel_count,
3516 cfg->hp_outs * 2);
3517 spec->multiout.max_channels = max(spec->ext_channel_count,
3518 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003519
3520 err = check_auto_mute_availability(codec);
3521 if (err < 0)
3522 return err;
3523
3524 err = check_dyn_adc_switch(codec);
3525 if (err < 0)
3526 return err;
3527
3528 if (!spec->shared_mic_hp) {
3529 err = check_auto_mic_availability(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003530 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 }
Takashi Iwai071c73a2006-08-23 18:34:06 +02003533
Takashi Iwai352f7f92012-12-19 12:52:06 +01003534 err = create_capture_mixers(codec);
3535 if (err < 0)
3536 return err;
3537
3538 err = parse_mic_boost(codec);
3539 if (err < 0)
3540 return err;
3541
Takashi Iwai978e77e2013-01-10 16:57:58 +01003542 if (spec->add_out_jack_modes) {
3543 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
3544 err = create_out_jack_modes(codec, cfg->line_outs,
3545 cfg->line_out_pins);
3546 if (err < 0)
3547 return err;
3548 }
3549 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
3550 err = create_out_jack_modes(codec, cfg->hp_outs,
3551 cfg->hp_pins);
3552 if (err < 0)
3553 return err;
3554 }
3555 }
3556
Takashi Iwai352f7f92012-12-19 12:52:06 +01003557 dig_only:
3558 parse_digital(codec);
3559
3560 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003562EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563
3564
3565/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003566 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003568
3569/* slave controls for virtual master */
3570static const char * const slave_pfxs[] = {
3571 "Front", "Surround", "Center", "LFE", "Side",
3572 "Headphone", "Speaker", "Mono", "Line Out",
3573 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01003574 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
3575 "Headphone Front", "Headphone Surround", "Headphone CLFE",
3576 "Headphone Side",
Takashi Iwai352f7f92012-12-19 12:52:06 +01003577 NULL,
3578};
3579
3580int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003582 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584
Takashi Iwai36502d02012-12-19 15:15:10 +01003585 if (spec->kctls.used) {
3586 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
3587 if (err < 0)
3588 return err;
3589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590
Takashi Iwai352f7f92012-12-19 12:52:06 +01003591 if (spec->multiout.dig_out_nid) {
3592 err = snd_hda_create_dig_out_ctls(codec,
3593 spec->multiout.dig_out_nid,
3594 spec->multiout.dig_out_nid,
3595 spec->pcm_rec[1].pcm_type);
3596 if (err < 0)
3597 return err;
3598 if (!spec->no_analog) {
3599 err = snd_hda_create_spdif_share_sw(codec,
3600 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 if (err < 0)
3602 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003603 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 }
3605 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003606 if (spec->dig_in_nid) {
3607 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
3608 if (err < 0)
3609 return err;
3610 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611
Takashi Iwai352f7f92012-12-19 12:52:06 +01003612 /* if we have no master control, let's create it */
3613 if (!spec->no_analog &&
3614 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
3615 unsigned int vmaster_tlv[4];
3616 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
3617 HDA_OUTPUT, vmaster_tlv);
3618 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
3619 vmaster_tlv, slave_pfxs,
3620 "Playback Volume");
3621 if (err < 0)
3622 return err;
3623 }
3624 if (!spec->no_analog &&
3625 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
3626 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
3627 NULL, slave_pfxs,
3628 "Playback Switch",
3629 true, &spec->vmaster_mute.sw_kctl);
3630 if (err < 0)
3631 return err;
3632 if (spec->vmaster_mute.hook)
Takashi Iwaifd25a972012-12-20 14:57:18 +01003633 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
3634 spec->vmaster_mute_enum);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636
Takashi Iwai352f7f92012-12-19 12:52:06 +01003637 free_kctls(spec); /* no longer needed */
3638
3639 if (spec->shared_mic_hp) {
3640 int err;
3641 int nid = spec->autocfg.inputs[1].pin;
3642 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
3643 if (err < 0)
3644 return err;
3645 err = snd_hda_jack_detect_enable(codec, nid, 0);
3646 if (err < 0)
3647 return err;
3648 }
3649
3650 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
3651 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 return err;
3653
3654 return 0;
3655}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003656EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
3657
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658
3659/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003660 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003663static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
3664 struct hda_codec *codec,
3665 struct snd_pcm_substream *substream,
3666 int action)
3667{
3668 struct hda_gen_spec *spec = codec->spec;
3669 if (spec->pcm_playback_hook)
3670 spec->pcm_playback_hook(hinfo, codec, substream, action);
3671}
3672
Takashi Iwai352f7f92012-12-19 12:52:06 +01003673/*
3674 * Analog playback callbacks
3675 */
3676static int playback_pcm_open(struct hda_pcm_stream *hinfo,
3677 struct hda_codec *codec,
3678 struct snd_pcm_substream *substream)
3679{
3680 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003681 int err;
3682
3683 mutex_lock(&spec->pcm_mutex);
3684 err = snd_hda_multi_out_analog_open(codec,
3685 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003686 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003687 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003688 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003689 call_pcm_playback_hook(hinfo, codec, substream,
3690 HDA_GEN_PCM_ACT_OPEN);
3691 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003692 mutex_unlock(&spec->pcm_mutex);
3693 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003694}
3695
3696static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01003697 struct hda_codec *codec,
3698 unsigned int stream_tag,
3699 unsigned int format,
3700 struct snd_pcm_substream *substream)
3701{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003702 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003703 int err;
3704
3705 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
3706 stream_tag, format, substream);
3707 if (!err)
3708 call_pcm_playback_hook(hinfo, codec, substream,
3709 HDA_GEN_PCM_ACT_PREPARE);
3710 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003711}
Takashi Iwai97ec5582006-03-21 11:29:07 +01003712
Takashi Iwai352f7f92012-12-19 12:52:06 +01003713static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3714 struct hda_codec *codec,
3715 struct snd_pcm_substream *substream)
3716{
3717 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003718 int err;
3719
3720 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
3721 if (!err)
3722 call_pcm_playback_hook(hinfo, codec, substream,
3723 HDA_GEN_PCM_ACT_CLEANUP);
3724 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003725}
3726
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003727static int playback_pcm_close(struct hda_pcm_stream *hinfo,
3728 struct hda_codec *codec,
3729 struct snd_pcm_substream *substream)
3730{
3731 struct hda_gen_spec *spec = codec->spec;
3732 mutex_lock(&spec->pcm_mutex);
3733 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003734 call_pcm_playback_hook(hinfo, codec, substream,
3735 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003736 mutex_unlock(&spec->pcm_mutex);
3737 return 0;
3738}
3739
3740static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
3741 struct hda_codec *codec,
3742 struct snd_pcm_substream *substream)
3743{
3744 struct hda_gen_spec *spec = codec->spec;
3745 int err = 0;
3746
3747 mutex_lock(&spec->pcm_mutex);
3748 if (!spec->indep_hp_enabled)
3749 err = -EBUSY;
3750 else
3751 spec->active_streams |= 1 << STREAM_INDEP_HP;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003752 call_pcm_playback_hook(hinfo, codec, substream,
3753 HDA_GEN_PCM_ACT_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003754 mutex_unlock(&spec->pcm_mutex);
3755 return err;
3756}
3757
3758static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
3759 struct hda_codec *codec,
3760 struct snd_pcm_substream *substream)
3761{
3762 struct hda_gen_spec *spec = codec->spec;
3763 mutex_lock(&spec->pcm_mutex);
3764 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003765 call_pcm_playback_hook(hinfo, codec, substream,
3766 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003767 mutex_unlock(&spec->pcm_mutex);
3768 return 0;
3769}
3770
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003771static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3772 struct hda_codec *codec,
3773 unsigned int stream_tag,
3774 unsigned int format,
3775 struct snd_pcm_substream *substream)
3776{
3777 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3778 call_pcm_playback_hook(hinfo, codec, substream,
3779 HDA_GEN_PCM_ACT_PREPARE);
3780 return 0;
3781}
3782
3783static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3784 struct hda_codec *codec,
3785 struct snd_pcm_substream *substream)
3786{
3787 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
3788 call_pcm_playback_hook(hinfo, codec, substream,
3789 HDA_GEN_PCM_ACT_CLEANUP);
3790 return 0;
3791}
3792
Takashi Iwai352f7f92012-12-19 12:52:06 +01003793/*
3794 * Digital out
3795 */
3796static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
3797 struct hda_codec *codec,
3798 struct snd_pcm_substream *substream)
3799{
3800 struct hda_gen_spec *spec = codec->spec;
3801 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3802}
3803
3804static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3805 struct hda_codec *codec,
3806 unsigned int stream_tag,
3807 unsigned int format,
3808 struct snd_pcm_substream *substream)
3809{
3810 struct hda_gen_spec *spec = codec->spec;
3811 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3812 stream_tag, format, substream);
3813}
3814
3815static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3816 struct hda_codec *codec,
3817 struct snd_pcm_substream *substream)
3818{
3819 struct hda_gen_spec *spec = codec->spec;
3820 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
3821}
3822
3823static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
3824 struct hda_codec *codec,
3825 struct snd_pcm_substream *substream)
3826{
3827 struct hda_gen_spec *spec = codec->spec;
3828 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3829}
3830
3831/*
3832 * Analog capture
3833 */
3834static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3835 struct hda_codec *codec,
3836 unsigned int stream_tag,
3837 unsigned int format,
3838 struct snd_pcm_substream *substream)
3839{
3840 struct hda_gen_spec *spec = codec->spec;
3841
3842 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01003843 stream_tag, 0, format);
3844 return 0;
3845}
3846
Takashi Iwai352f7f92012-12-19 12:52:06 +01003847static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3848 struct hda_codec *codec,
3849 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01003850{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003851 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01003852
Takashi Iwai352f7f92012-12-19 12:52:06 +01003853 snd_hda_codec_cleanup_stream(codec,
3854 spec->adc_nids[substream->number + 1]);
Takashi Iwai97ec5582006-03-21 11:29:07 +01003855 return 0;
3856}
3857
Takashi Iwai352f7f92012-12-19 12:52:06 +01003858/*
3859 */
3860static const struct hda_pcm_stream pcm_analog_playback = {
3861 .substreams = 1,
3862 .channels_min = 2,
3863 .channels_max = 8,
3864 /* NID is set in build_pcms */
3865 .ops = {
3866 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003867 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003868 .prepare = playback_pcm_prepare,
3869 .cleanup = playback_pcm_cleanup
3870 },
3871};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872
Takashi Iwai352f7f92012-12-19 12:52:06 +01003873static const struct hda_pcm_stream pcm_analog_capture = {
3874 .substreams = 1,
3875 .channels_min = 2,
3876 .channels_max = 2,
3877 /* NID is set in build_pcms */
3878};
3879
3880static const struct hda_pcm_stream pcm_analog_alt_playback = {
3881 .substreams = 1,
3882 .channels_min = 2,
3883 .channels_max = 2,
3884 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003885 .ops = {
3886 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003887 .close = alt_playback_pcm_close,
3888 .prepare = alt_playback_pcm_prepare,
3889 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003890 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01003891};
3892
3893static const struct hda_pcm_stream pcm_analog_alt_capture = {
3894 .substreams = 2, /* can be overridden */
3895 .channels_min = 2,
3896 .channels_max = 2,
3897 /* NID is set in build_pcms */
3898 .ops = {
3899 .prepare = alt_capture_pcm_prepare,
3900 .cleanup = alt_capture_pcm_cleanup
3901 },
3902};
3903
3904static const struct hda_pcm_stream pcm_digital_playback = {
3905 .substreams = 1,
3906 .channels_min = 2,
3907 .channels_max = 2,
3908 /* NID is set in build_pcms */
3909 .ops = {
3910 .open = dig_playback_pcm_open,
3911 .close = dig_playback_pcm_close,
3912 .prepare = dig_playback_pcm_prepare,
3913 .cleanup = dig_playback_pcm_cleanup
3914 },
3915};
3916
3917static const struct hda_pcm_stream pcm_digital_capture = {
3918 .substreams = 1,
3919 .channels_min = 2,
3920 .channels_max = 2,
3921 /* NID is set in build_pcms */
3922};
3923
3924/* Used by build_pcms to flag that a PCM has no playback stream */
3925static const struct hda_pcm_stream pcm_null_stream = {
3926 .substreams = 0,
3927 .channels_min = 0,
3928 .channels_max = 0,
3929};
3930
3931/*
3932 * dynamic changing ADC PCM streams
3933 */
3934static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
3935{
3936 struct hda_gen_spec *spec = codec->spec;
3937 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
3938
3939 if (spec->cur_adc && spec->cur_adc != new_adc) {
3940 /* stream is running, let's swap the current ADC */
3941 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
3942 spec->cur_adc = new_adc;
3943 snd_hda_codec_setup_stream(codec, new_adc,
3944 spec->cur_adc_stream_tag, 0,
3945 spec->cur_adc_format);
3946 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003948 return false;
3949}
3950
3951/* analog capture with dynamic dual-adc changes */
3952static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3953 struct hda_codec *codec,
3954 unsigned int stream_tag,
3955 unsigned int format,
3956 struct snd_pcm_substream *substream)
3957{
3958 struct hda_gen_spec *spec = codec->spec;
3959 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
3960 spec->cur_adc_stream_tag = stream_tag;
3961 spec->cur_adc_format = format;
3962 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
3963 return 0;
3964}
3965
3966static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3967 struct hda_codec *codec,
3968 struct snd_pcm_substream *substream)
3969{
3970 struct hda_gen_spec *spec = codec->spec;
3971 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
3972 spec->cur_adc = 0;
3973 return 0;
3974}
3975
3976static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
3977 .substreams = 1,
3978 .channels_min = 2,
3979 .channels_max = 2,
3980 .nid = 0, /* fill later */
3981 .ops = {
3982 .prepare = dyn_adc_capture_pcm_prepare,
3983 .cleanup = dyn_adc_capture_pcm_cleanup
3984 },
3985};
3986
Takashi Iwaif873e532012-12-20 16:58:39 +01003987static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
3988 const char *chip_name)
3989{
3990 char *p;
3991
3992 if (*str)
3993 return;
3994 strlcpy(str, chip_name, len);
3995
3996 /* drop non-alnum chars after a space */
3997 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
3998 if (!isalnum(p[1])) {
3999 *p = 0;
4000 break;
4001 }
4002 }
4003 strlcat(str, sfx, len);
4004}
4005
Takashi Iwai352f7f92012-12-19 12:52:06 +01004006/* build PCM streams based on the parsed results */
4007int snd_hda_gen_build_pcms(struct hda_codec *codec)
4008{
4009 struct hda_gen_spec *spec = codec->spec;
4010 struct hda_pcm *info = spec->pcm_rec;
4011 const struct hda_pcm_stream *p;
4012 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013
4014 codec->num_pcms = 1;
4015 codec->pcm_info = info;
4016
Takashi Iwai352f7f92012-12-19 12:52:06 +01004017 if (spec->no_analog)
4018 goto skip_analog;
4019
Takashi Iwaif873e532012-12-20 16:58:39 +01004020 fill_pcm_stream_name(spec->stream_name_analog,
4021 sizeof(spec->stream_name_analog),
4022 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004023 info->name = spec->stream_name_analog;
4024
4025 if (spec->multiout.num_dacs > 0) {
4026 p = spec->stream_analog_playback;
4027 if (!p)
4028 p = &pcm_analog_playback;
4029 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4030 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
4031 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
4032 spec->multiout.max_channels;
4033 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
4034 spec->autocfg.line_outs == 2)
4035 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
4036 snd_pcm_2_1_chmaps;
4037 }
4038 if (spec->num_adc_nids) {
4039 p = spec->stream_analog_capture;
4040 if (!p) {
4041 if (spec->dyn_adc_switch)
4042 p = &dyn_adc_pcm_analog_capture;
4043 else
4044 p = &pcm_analog_capture;
4045 }
4046 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4047 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
4048 }
4049
Takashi Iwai352f7f92012-12-19 12:52:06 +01004050 skip_analog:
4051 /* SPDIF for stream index #1 */
4052 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01004053 fill_pcm_stream_name(spec->stream_name_digital,
4054 sizeof(spec->stream_name_digital),
4055 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004056 codec->num_pcms = 2;
4057 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
4058 info = spec->pcm_rec + 1;
4059 info->name = spec->stream_name_digital;
4060 if (spec->dig_out_type)
4061 info->pcm_type = spec->dig_out_type;
4062 else
4063 info->pcm_type = HDA_PCM_TYPE_SPDIF;
4064 if (spec->multiout.dig_out_nid) {
4065 p = spec->stream_digital_playback;
4066 if (!p)
4067 p = &pcm_digital_playback;
4068 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4069 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
4070 }
4071 if (spec->dig_in_nid) {
4072 p = spec->stream_digital_capture;
4073 if (!p)
4074 p = &pcm_digital_capture;
4075 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4076 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
4077 }
4078 }
4079
4080 if (spec->no_analog)
4081 return 0;
4082
4083 /* If the use of more than one ADC is requested for the current
4084 * model, configure a second analog capture-only PCM.
4085 */
4086 have_multi_adcs = (spec->num_adc_nids > 1) &&
4087 !spec->dyn_adc_switch && !spec->auto_mic;
4088 /* Additional Analaog capture for index #2 */
4089 if (spec->alt_dac_nid || have_multi_adcs) {
4090 codec->num_pcms = 3;
4091 info = spec->pcm_rec + 2;
4092 info->name = spec->stream_name_analog;
4093 if (spec->alt_dac_nid) {
4094 p = spec->stream_analog_alt_playback;
4095 if (!p)
4096 p = &pcm_analog_alt_playback;
4097 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4098 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
4099 spec->alt_dac_nid;
4100 } else {
4101 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
4102 pcm_null_stream;
4103 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
4104 }
4105 if (have_multi_adcs) {
4106 p = spec->stream_analog_alt_capture;
4107 if (!p)
4108 p = &pcm_analog_alt_capture;
4109 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4110 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
4111 spec->adc_nids[1];
4112 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
4113 spec->num_adc_nids - 1;
4114 } else {
4115 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
4116 pcm_null_stream;
4117 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
4118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 }
4120
4121 return 0;
4122}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004123EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
4124
4125
4126/*
4127 * Standard auto-parser initializations
4128 */
4129
Takashi Iwaid4156932013-01-07 10:08:02 +01004130/* configure the given path as a proper output */
Takashi Iwai2c12c302013-01-10 09:33:29 +01004131static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004132{
4133 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01004134 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004135
Takashi Iwai196c17662013-01-04 15:01:40 +01004136 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01004137 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004138 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01004139 pin = path->path[path->depth - 1];
Takashi Iwai2c12c302013-01-10 09:33:29 +01004140 restore_pin_ctl(codec, pin);
Takashi Iwaie1284af2013-01-03 16:33:02 +01004141 snd_hda_activate_path(codec, path, path->active, true);
4142 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004143}
4144
4145/* initialize primary output paths */
4146static void init_multi_out(struct hda_codec *codec)
4147{
4148 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004149 int i;
4150
Takashi Iwaid4156932013-01-07 10:08:02 +01004151 for (i = 0; i < spec->autocfg.line_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004152 set_output_and_unmute(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004153}
4154
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004155
Takashi Iwai2c12c302013-01-10 09:33:29 +01004156static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004157{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004158 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004159
Takashi Iwaid4156932013-01-07 10:08:02 +01004160 for (i = 0; i < num_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004161 set_output_and_unmute(codec, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004162}
4163
4164/* initialize hp and speaker paths */
4165static void init_extra_out(struct hda_codec *codec)
4166{
4167 struct hda_gen_spec *spec = codec->spec;
4168
4169 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004170 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004171 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
4172 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai2c12c302013-01-10 09:33:29 +01004173 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004174}
4175
4176/* initialize multi-io paths */
4177static void init_multi_io(struct hda_codec *codec)
4178{
4179 struct hda_gen_spec *spec = codec->spec;
4180 int i;
4181
4182 for (i = 0; i < spec->multi_ios; i++) {
4183 hda_nid_t pin = spec->multi_io[i].pin;
4184 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01004185 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004186 if (!path)
4187 continue;
4188 if (!spec->multi_io[i].ctl_in)
4189 spec->multi_io[i].ctl_in =
Takashi Iwai2c12c302013-01-10 09:33:29 +01004190 snd_hda_codec_get_pin_target(codec, pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004191 snd_hda_activate_path(codec, path, path->active, true);
4192 }
4193}
4194
Takashi Iwai352f7f92012-12-19 12:52:06 +01004195/* set up input pins and loopback paths */
4196static void init_analog_input(struct hda_codec *codec)
4197{
4198 struct hda_gen_spec *spec = codec->spec;
4199 struct auto_pin_cfg *cfg = &spec->autocfg;
4200 int i;
4201
4202 for (i = 0; i < cfg->num_inputs; i++) {
4203 hda_nid_t nid = cfg->inputs[i].pin;
4204 if (is_input_pin(codec, nid))
Takashi Iwai2c12c302013-01-10 09:33:29 +01004205 restore_pin_ctl(codec, nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004206
4207 /* init loopback inputs */
4208 if (spec->mixer_nid) {
4209 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01004210 path = snd_hda_get_path_from_idx(codec, spec->loopback_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004211 if (path)
4212 snd_hda_activate_path(codec, path,
4213 path->active, false);
4214 }
4215 }
4216}
4217
4218/* initialize ADC paths */
4219static void init_input_src(struct hda_codec *codec)
4220{
4221 struct hda_gen_spec *spec = codec->spec;
4222 struct hda_input_mux *imux = &spec->input_mux;
4223 struct nid_path *path;
4224 int i, c, nums;
4225
4226 if (spec->dyn_adc_switch)
4227 nums = 1;
4228 else
4229 nums = spec->num_adc_nids;
4230
4231 for (c = 0; c < nums; c++) {
4232 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01004233 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004234 if (path) {
4235 bool active = path->active;
4236 if (i == spec->cur_mux[c])
4237 active = true;
4238 snd_hda_activate_path(codec, path, active, false);
4239 }
4240 }
4241 }
4242
4243 if (spec->shared_mic_hp)
4244 update_shared_mic_hp(codec, spec->cur_mux[0]);
4245
4246 if (spec->cap_sync_hook)
4247 spec->cap_sync_hook(codec);
4248}
4249
4250/* set right pin controls for digital I/O */
4251static void init_digital(struct hda_codec *codec)
4252{
4253 struct hda_gen_spec *spec = codec->spec;
4254 int i;
4255 hda_nid_t pin;
4256
Takashi Iwaid4156932013-01-07 10:08:02 +01004257 for (i = 0; i < spec->autocfg.dig_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004258 set_output_and_unmute(codec, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004259 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01004260 if (pin) {
4261 struct nid_path *path;
Takashi Iwai2c12c302013-01-10 09:33:29 +01004262 restore_pin_ctl(codec, pin);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01004263 path = snd_hda_get_path_from_idx(codec, spec->digin_path);
4264 if (path)
4265 snd_hda_activate_path(codec, path, path->active, false);
4266 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004267}
4268
Takashi Iwai973e4972012-12-20 15:16:09 +01004269/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
4270 * invalid unsol tags by some reason
4271 */
4272static void clear_unsol_on_unused_pins(struct hda_codec *codec)
4273{
4274 int i;
4275
4276 for (i = 0; i < codec->init_pins.used; i++) {
4277 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
4278 hda_nid_t nid = pin->nid;
4279 if (is_jack_detectable(codec, nid) &&
4280 !snd_hda_jack_tbl_get(codec, nid))
4281 snd_hda_codec_update_cache(codec, nid, 0,
4282 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
4283 }
4284}
4285
Takashi Iwai5187ac12013-01-07 12:52:16 +01004286/*
4287 * initialize the generic spec;
4288 * this can be put as patch_ops.init function
4289 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004290int snd_hda_gen_init(struct hda_codec *codec)
4291{
4292 struct hda_gen_spec *spec = codec->spec;
4293
4294 if (spec->init_hook)
4295 spec->init_hook(codec);
4296
4297 snd_hda_apply_verbs(codec);
4298
Takashi Iwai3bbcd272012-12-20 11:50:58 +01004299 codec->cached_write = 1;
4300
Takashi Iwai352f7f92012-12-19 12:52:06 +01004301 init_multi_out(codec);
4302 init_extra_out(codec);
4303 init_multi_io(codec);
4304 init_analog_input(codec);
4305 init_input_src(codec);
4306 init_digital(codec);
4307
Takashi Iwai973e4972012-12-20 15:16:09 +01004308 clear_unsol_on_unused_pins(codec);
4309
Takashi Iwai352f7f92012-12-19 12:52:06 +01004310 /* call init functions of standard auto-mute helpers */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01004311 update_automute_all(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004312
Takashi Iwai3bbcd272012-12-20 11:50:58 +01004313 snd_hda_codec_flush_amp_cache(codec);
4314 snd_hda_codec_flush_cmd_cache(codec);
4315
Takashi Iwai352f7f92012-12-19 12:52:06 +01004316 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
4317 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
4318
4319 hda_call_check_power_status(codec, 0x01);
4320 return 0;
4321}
Takashi Iwaifce52a32013-01-07 12:42:48 +01004322EXPORT_SYMBOL_HDA(snd_hda_gen_init);
4323
Takashi Iwai5187ac12013-01-07 12:52:16 +01004324/*
4325 * free the generic spec;
4326 * this can be put as patch_ops.free function
4327 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01004328void snd_hda_gen_free(struct hda_codec *codec)
4329{
4330 snd_hda_gen_spec_free(codec->spec);
4331 kfree(codec->spec);
4332 codec->spec = NULL;
4333}
4334EXPORT_SYMBOL_HDA(snd_hda_gen_free);
4335
4336#ifdef CONFIG_PM
Takashi Iwai5187ac12013-01-07 12:52:16 +01004337/*
4338 * check the loopback power save state;
4339 * this can be put as patch_ops.check_power_status function
4340 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01004341int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
4342{
4343 struct hda_gen_spec *spec = codec->spec;
4344 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
4345}
4346EXPORT_SYMBOL_HDA(snd_hda_gen_check_power_status);
4347#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01004348
4349
4350/*
4351 * the generic codec support
4352 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353
Takashi Iwai352f7f92012-12-19 12:52:06 +01004354static const struct hda_codec_ops generic_patch_ops = {
4355 .build_controls = snd_hda_gen_build_controls,
4356 .build_pcms = snd_hda_gen_build_pcms,
4357 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01004358 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004359 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02004360#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01004361 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02004362#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363};
4364
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365int snd_hda_parse_generic_codec(struct hda_codec *codec)
4366{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004367 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 int err;
4369
Takashi Iwaie560d8d2005-09-09 14:21:46 +02004370 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004371 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004373 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004376 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
4377 if (err < 0)
4378 return err;
4379
4380 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004381 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 goto error;
4383
4384 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385 return 0;
4386
Takashi Iwai352f7f92012-12-19 12:52:06 +01004387error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01004388 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 return err;
4390}
Takashi Iwaifce52a32013-01-07 12:42:48 +01004391EXPORT_SYMBOL_HDA(snd_hda_parse_generic_codec);