blob: 29f37c90225d9c143de7e094cc45c1721f3e277f [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>
Takashi Iwai294765582013-01-17 09:52:11 +010029#include <linux/bitops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <sound/core.h>
Takashi Iwai352f7f92012-12-19 12:52:06 +010031#include <sound/jack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "hda_codec.h"
33#include "hda_local.h"
Takashi Iwai352f7f92012-12-19 12:52:06 +010034#include "hda_auto_parser.h"
35#include "hda_jack.h"
36#include "hda_generic.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Takashi Iwai352f7f92012-12-19 12:52:06 +010039/* initialize hda_gen_spec struct */
40int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
Takashi Iwai352f7f92012-12-19 12:52:06 +010042 snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
Takashi Iwai352f7f92012-12-19 12:52:06 +010043 snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
Takashi Iwai38cf6f12012-12-21 14:09:42 +010044 mutex_init(&spec->pcm_mutex);
Takashi Iwai352f7f92012-12-19 12:52:06 +010045 return 0;
46}
47EXPORT_SYMBOL_HDA(snd_hda_gen_spec_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Takashi Iwai12c93df2012-12-19 14:38:33 +010049struct snd_kcontrol_new *
50snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
51 const struct snd_kcontrol_new *temp)
Takashi Iwai352f7f92012-12-19 12:52:06 +010052{
53 struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
54 if (!knew)
55 return NULL;
56 *knew = *temp;
57 if (name)
58 knew->name = kstrdup(name, GFP_KERNEL);
59 else if (knew->name)
60 knew->name = kstrdup(knew->name, GFP_KERNEL);
61 if (!knew->name)
62 return NULL;
63 return knew;
64}
Takashi Iwai12c93df2012-12-19 14:38:33 +010065EXPORT_SYMBOL_HDA(snd_hda_gen_add_kctl);
Takashi Iwai352f7f92012-12-19 12:52:06 +010066
67static void free_kctls(struct hda_gen_spec *spec)
68{
69 if (spec->kctls.list) {
70 struct snd_kcontrol_new *kctl = spec->kctls.list;
71 int i;
72 for (i = 0; i < spec->kctls.used; i++)
73 kfree(kctl[i].name);
74 }
75 snd_array_free(&spec->kctls);
76}
77
Takashi Iwai352f7f92012-12-19 12:52:06 +010078void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
79{
80 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 return;
Takashi Iwai352f7f92012-12-19 12:52:06 +010082 free_kctls(spec);
Takashi Iwai352f7f92012-12-19 12:52:06 +010083 snd_array_free(&spec->paths);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084}
Takashi Iwai352f7f92012-12-19 12:52:06 +010085EXPORT_SYMBOL_HDA(snd_hda_gen_spec_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87/*
Takashi Iwai1c70a582013-01-11 17:48:22 +010088 * store user hints
89 */
90static void parse_user_hints(struct hda_codec *codec)
91{
92 struct hda_gen_spec *spec = codec->spec;
93 int val;
94
95 val = snd_hda_get_bool_hint(codec, "jack_detect");
96 if (val >= 0)
97 codec->no_jack_detect = !val;
98 val = snd_hda_get_bool_hint(codec, "inv_jack_detect");
99 if (val >= 0)
100 codec->inv_jack_detect = !!val;
101 val = snd_hda_get_bool_hint(codec, "trigger_sense");
102 if (val >= 0)
103 codec->no_trigger_sense = !val;
104 val = snd_hda_get_bool_hint(codec, "inv_eapd");
105 if (val >= 0)
106 codec->inv_eapd = !!val;
107 val = snd_hda_get_bool_hint(codec, "pcm_format_first");
108 if (val >= 0)
109 codec->pcm_format_first = !!val;
110 val = snd_hda_get_bool_hint(codec, "sticky_stream");
111 if (val >= 0)
112 codec->no_sticky_stream = !val;
113 val = snd_hda_get_bool_hint(codec, "spdif_status_reset");
114 if (val >= 0)
115 codec->spdif_status_reset = !!val;
116 val = snd_hda_get_bool_hint(codec, "pin_amp_workaround");
117 if (val >= 0)
118 codec->pin_amp_workaround = !!val;
119 val = snd_hda_get_bool_hint(codec, "single_adc_amp");
120 if (val >= 0)
121 codec->single_adc_amp = !!val;
122
Takashi Iwaif72706b2013-01-16 18:20:07 +0100123 val = snd_hda_get_bool_hint(codec, "auto_mute");
124 if (val >= 0)
125 spec->suppress_auto_mute = !val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100126 val = snd_hda_get_bool_hint(codec, "auto_mic");
127 if (val >= 0)
128 spec->suppress_auto_mic = !val;
129 val = snd_hda_get_bool_hint(codec, "line_in_auto_switch");
130 if (val >= 0)
131 spec->line_in_auto_switch = !!val;
132 val = snd_hda_get_bool_hint(codec, "need_dac_fix");
133 if (val >= 0)
134 spec->need_dac_fix = !!val;
135 val = snd_hda_get_bool_hint(codec, "primary_hp");
136 if (val >= 0)
137 spec->no_primary_hp = !val;
138 val = snd_hda_get_bool_hint(codec, "multi_cap_vol");
139 if (val >= 0)
140 spec->multi_cap_vol = !!val;
141 val = snd_hda_get_bool_hint(codec, "inv_dmic_split");
142 if (val >= 0)
143 spec->inv_dmic_split = !!val;
144 val = snd_hda_get_bool_hint(codec, "indep_hp");
145 if (val >= 0)
146 spec->indep_hp = !!val;
147 val = snd_hda_get_bool_hint(codec, "add_stereo_mix_input");
148 if (val >= 0)
149 spec->add_stereo_mix_input = !!val;
150 val = snd_hda_get_bool_hint(codec, "add_out_jack_modes");
151 if (val >= 0)
152 spec->add_out_jack_modes = !!val;
Takashi Iwai294765582013-01-17 09:52:11 +0100153 val = snd_hda_get_bool_hint(codec, "add_in_jack_modes");
154 if (val >= 0)
155 spec->add_in_jack_modes = !!val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100156
157 if (!snd_hda_get_int_hint(codec, "mixer_nid", &val))
158 spec->mixer_nid = val;
159}
160
161/*
Takashi Iwai2c12c302013-01-10 09:33:29 +0100162 * pin control value accesses
163 */
164
165#define update_pin_ctl(codec, pin, val) \
166 snd_hda_codec_update_cache(codec, pin, 0, \
167 AC_VERB_SET_PIN_WIDGET_CONTROL, val)
168
169/* restore the pinctl based on the cached value */
170static inline void restore_pin_ctl(struct hda_codec *codec, hda_nid_t pin)
171{
172 update_pin_ctl(codec, pin, snd_hda_codec_get_pin_target(codec, pin));
173}
174
175/* set the pinctl target value and write it if requested */
176static void set_pin_target(struct hda_codec *codec, hda_nid_t pin,
177 unsigned int val, bool do_write)
178{
179 if (!pin)
180 return;
181 val = snd_hda_correct_pin_ctl(codec, pin, val);
182 snd_hda_codec_set_pin_target(codec, pin, val);
183 if (do_write)
184 update_pin_ctl(codec, pin, val);
185}
186
187/* set pinctl target values for all given pins */
188static void set_pin_targets(struct hda_codec *codec, int num_pins,
189 hda_nid_t *pins, unsigned int val)
190{
191 int i;
192 for (i = 0; i < num_pins; i++)
193 set_pin_target(codec, pins[i], val, false);
194}
195
196/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100197 * parsing paths
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100200/* return the position of NID in the list, or -1 if not found */
201static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
202{
203 int i;
204 for (i = 0; i < nums; i++)
205 if (list[i] == nid)
206 return i;
207 return -1;
208}
209
210/* return true if the given NID is contained in the path */
211static bool is_nid_contained(struct nid_path *path, hda_nid_t nid)
212{
213 return find_idx_in_nid_list(nid, path->path, path->depth) >= 0;
214}
215
Takashi Iwaif5172a72013-01-04 13:19:55 +0100216static struct nid_path *get_nid_path(struct hda_codec *codec,
217 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100218 int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100220 struct hda_gen_spec *spec = codec->spec;
221 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Takashi Iwai352f7f92012-12-19 12:52:06 +0100223 for (i = 0; i < spec->paths.used; i++) {
224 struct nid_path *path = snd_array_elem(&spec->paths, i);
225 if (path->depth <= 0)
226 continue;
227 if ((!from_nid || path->path[0] == from_nid) &&
Takashi Iwaif5172a72013-01-04 13:19:55 +0100228 (!to_nid || path->path[path->depth - 1] == to_nid)) {
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100229 if (!anchor_nid ||
230 (anchor_nid > 0 && is_nid_contained(path, anchor_nid)) ||
231 (anchor_nid < 0 && !is_nid_contained(path, anchor_nid)))
Takashi Iwaif5172a72013-01-04 13:19:55 +0100232 return path;
233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 }
235 return NULL;
236}
Takashi Iwaif5172a72013-01-04 13:19:55 +0100237
238/* get the path between the given NIDs;
239 * passing 0 to either @pin or @dac behaves as a wildcard
240 */
241struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
242 hda_nid_t from_nid, hda_nid_t to_nid)
243{
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100244 return get_nid_path(codec, from_nid, to_nid, 0);
Takashi Iwaif5172a72013-01-04 13:19:55 +0100245}
Takashi Iwai352f7f92012-12-19 12:52:06 +0100246EXPORT_SYMBOL_HDA(snd_hda_get_nid_path);
247
Takashi Iwai196c17662013-01-04 15:01:40 +0100248/* get the index number corresponding to the path instance;
249 * the index starts from 1, for easier checking the invalid value
250 */
251int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
252{
253 struct hda_gen_spec *spec = codec->spec;
254 struct nid_path *array = spec->paths.list;
255 ssize_t idx;
256
257 if (!spec->paths.used)
258 return 0;
259 idx = path - array;
260 if (idx < 0 || idx >= spec->paths.used)
261 return 0;
262 return idx + 1;
263}
264
265/* get the path instance corresponding to the given index number */
266struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
267{
268 struct hda_gen_spec *spec = codec->spec;
269
270 if (idx <= 0 || idx > spec->paths.used)
271 return NULL;
272 return snd_array_elem(&spec->paths, idx - 1);
273}
274
Takashi Iwai352f7f92012-12-19 12:52:06 +0100275/* check whether the given DAC is already found in any existing paths */
276static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
277{
278 struct hda_gen_spec *spec = codec->spec;
279 int i;
280
281 for (i = 0; i < spec->paths.used; i++) {
282 struct nid_path *path = snd_array_elem(&spec->paths, i);
283 if (path->path[0] == nid)
284 return true;
285 }
286 return false;
287}
288
289/* check whether the given two widgets can be connected */
290static bool is_reachable_path(struct hda_codec *codec,
291 hda_nid_t from_nid, hda_nid_t to_nid)
292{
293 if (!from_nid || !to_nid)
294 return false;
295 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
296}
297
298/* nid, dir and idx */
299#define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
300
301/* check whether the given ctl is already assigned in any path elements */
302static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
303{
304 struct hda_gen_spec *spec = codec->spec;
305 int i;
306
307 val &= AMP_VAL_COMPARE_MASK;
308 for (i = 0; i < spec->paths.used; i++) {
309 struct nid_path *path = snd_array_elem(&spec->paths, i);
310 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
311 return true;
312 }
313 return false;
314}
315
316/* check whether a control with the given (nid, dir, idx) was assigned */
317static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai8999bf02013-01-18 11:01:33 +0100318 int dir, int idx, int type)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100319{
320 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
Takashi Iwai8999bf02013-01-18 11:01:33 +0100321 return is_ctl_used(codec, val, type);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100322}
323
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100324static void print_nid_path(const char *pfx, struct nid_path *path)
325{
326 char buf[40];
327 int i;
328
329
330 buf[0] = 0;
331 for (i = 0; i < path->depth; i++) {
332 char tmp[4];
333 sprintf(tmp, ":%02x", path->path[i]);
334 strlcat(buf, tmp, sizeof(buf));
335 }
336 snd_printdd("%s path: depth=%d %s\n", pfx, path->depth, buf);
337}
338
Takashi Iwai352f7f92012-12-19 12:52:06 +0100339/* called recursively */
340static bool __parse_nid_path(struct hda_codec *codec,
341 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100342 int anchor_nid, struct nid_path *path,
343 int depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100344{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100345 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100346 int i, nums;
347
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100348 if (to_nid == anchor_nid)
349 anchor_nid = 0; /* anchor passed */
350 else if (to_nid == (hda_nid_t)(-anchor_nid))
351 return false; /* hit the exclusive nid */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100352
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100353 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100354 for (i = 0; i < nums; i++) {
355 if (conn[i] != from_nid) {
356 /* special case: when from_nid is 0,
357 * try to find an empty DAC
358 */
359 if (from_nid ||
360 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
361 is_dac_already_used(codec, conn[i]))
362 continue;
363 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100364 /* anchor is not requested or already passed? */
365 if (anchor_nid <= 0)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100366 goto found;
367 }
368 if (depth >= MAX_NID_PATH_DEPTH)
369 return false;
370 for (i = 0; i < nums; i++) {
371 unsigned int type;
372 type = get_wcaps_type(get_wcaps(codec, conn[i]));
373 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
374 type == AC_WID_PIN)
375 continue;
376 if (__parse_nid_path(codec, from_nid, conn[i],
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100377 anchor_nid, path, depth + 1))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100378 goto found;
379 }
380 return false;
381
382 found:
383 path->path[path->depth] = conn[i];
384 path->idx[path->depth + 1] = i;
385 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
386 path->multi[path->depth + 1] = 1;
387 path->depth++;
388 return true;
389}
390
391/* parse the widget path from the given nid to the target nid;
392 * when @from_nid is 0, try to find an empty DAC;
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100393 * when @anchor_nid is set to a positive value, only paths through the widget
394 * with the given value are evaluated.
395 * when @anchor_nid is set to a negative value, paths through the widget
396 * with the negative of given value are excluded, only other paths are chosen.
397 * when @anchor_nid is zero, no special handling about path selection.
Takashi Iwai352f7f92012-12-19 12:52:06 +0100398 */
399bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100400 hda_nid_t to_nid, int anchor_nid,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100401 struct nid_path *path)
402{
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100403 if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100404 path->path[path->depth] = to_nid;
405 path->depth++;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100406 return true;
407 }
408 return false;
409}
410EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100413 * parse the path between the given NIDs and add to the path list.
414 * if no valid path is found, return NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100416struct nid_path *
417snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100418 hda_nid_t to_nid, int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100420 struct hda_gen_spec *spec = codec->spec;
421 struct nid_path *path;
422
423 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
424 return NULL;
425
Takashi Iwaif5172a72013-01-04 13:19:55 +0100426 /* check whether the path has been already added */
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100427 path = get_nid_path(codec, from_nid, to_nid, anchor_nid);
Takashi Iwaif5172a72013-01-04 13:19:55 +0100428 if (path)
429 return path;
430
Takashi Iwai352f7f92012-12-19 12:52:06 +0100431 path = snd_array_new(&spec->paths);
432 if (!path)
433 return NULL;
434 memset(path, 0, sizeof(*path));
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100435 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100436 return path;
437 /* push back */
438 spec->paths.used--;
439 return NULL;
440}
441EXPORT_SYMBOL_HDA(snd_hda_add_new_path);
442
Takashi Iwai980428c2013-01-09 09:28:20 +0100443/* clear the given path as invalid so that it won't be picked up later */
444static void invalidate_nid_path(struct hda_codec *codec, int idx)
445{
446 struct nid_path *path = snd_hda_get_path_from_idx(codec, idx);
447 if (!path)
448 return;
449 memset(path, 0, sizeof(*path));
450}
451
Takashi Iwai352f7f92012-12-19 12:52:06 +0100452/* look for an empty DAC slot */
453static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
454 bool is_digital)
455{
456 struct hda_gen_spec *spec = codec->spec;
457 bool cap_digital;
458 int i;
459
460 for (i = 0; i < spec->num_all_dacs; i++) {
461 hda_nid_t nid = spec->all_dacs[i];
462 if (!nid || is_dac_already_used(codec, nid))
463 continue;
464 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
465 if (is_digital != cap_digital)
466 continue;
467 if (is_reachable_path(codec, nid, pin))
468 return nid;
469 }
470 return 0;
471}
472
473/* replace the channels in the composed amp value with the given number */
474static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
475{
476 val &= ~(0x3U << 16);
477 val |= chs << 16;
478 return val;
479}
480
481/* check whether the widget has the given amp capability for the direction */
482static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
483 int dir, unsigned int bits)
484{
485 if (!nid)
486 return false;
487 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
488 if (query_amp_caps(codec, nid, dir) & bits)
489 return true;
490 return false;
491}
492
David Henningsson99a55922013-01-16 15:58:44 +0100493static bool same_amp_caps(struct hda_codec *codec, hda_nid_t nid1,
494 hda_nid_t nid2, int dir)
495{
496 if (!(get_wcaps(codec, nid1) & (1 << (dir + 1))))
497 return !(get_wcaps(codec, nid2) & (1 << (dir + 1)));
498 return (query_amp_caps(codec, nid1, dir) ==
499 query_amp_caps(codec, nid2, dir));
500}
501
Takashi Iwai352f7f92012-12-19 12:52:06 +0100502#define nid_has_mute(codec, nid, dir) \
503 check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
504#define nid_has_volume(codec, nid, dir) \
505 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
506
507/* look for a widget suitable for assigning a mute switch in the path */
508static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
509 struct nid_path *path)
510{
511 int i;
512
513 for (i = path->depth - 1; i >= 0; i--) {
514 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
515 return path->path[i];
516 if (i != path->depth - 1 && i != 0 &&
517 nid_has_mute(codec, path->path[i], HDA_INPUT))
518 return path->path[i];
519 }
520 return 0;
521}
522
523/* look for a widget suitable for assigning a volume ctl in the path */
524static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
525 struct nid_path *path)
526{
527 int i;
528
529 for (i = path->depth - 1; i >= 0; i--) {
530 if (nid_has_volume(codec, path->path[i], HDA_OUTPUT))
531 return path->path[i];
532 }
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200533 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
535
536/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100537 * path activation / deactivation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100539
540/* can have the amp-in capability? */
541static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100543 hda_nid_t nid = path->path[idx];
544 unsigned int caps = get_wcaps(codec, nid);
545 unsigned int type = get_wcaps_type(caps);
546
547 if (!(caps & AC_WCAP_IN_AMP))
548 return false;
549 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
550 return false;
551 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}
553
Takashi Iwai352f7f92012-12-19 12:52:06 +0100554/* can have the amp-out capability? */
555static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100557 hda_nid_t nid = path->path[idx];
558 unsigned int caps = get_wcaps(codec, nid);
559 unsigned int type = get_wcaps_type(caps);
560
561 if (!(caps & AC_WCAP_OUT_AMP))
562 return false;
563 if (type == AC_WID_PIN && !idx) /* only for output pins */
564 return false;
565 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566}
567
Takashi Iwai352f7f92012-12-19 12:52:06 +0100568/* check whether the given (nid,dir,idx) is active */
569static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
570 unsigned int idx, unsigned int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100572 struct hda_gen_spec *spec = codec->spec;
573 int i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Takashi Iwai352f7f92012-12-19 12:52:06 +0100575 for (n = 0; n < spec->paths.used; n++) {
576 struct nid_path *path = snd_array_elem(&spec->paths, n);
577 if (!path->active)
578 continue;
579 for (i = 0; i < path->depth; i++) {
580 if (path->path[i] == nid) {
581 if (dir == HDA_OUTPUT || path->idx[i] == idx)
582 return true;
583 break;
584 }
585 }
586 }
587 return false;
588}
589
590/* get the default amp value for the target state */
591static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai8999bf02013-01-18 11:01:33 +0100592 int dir, unsigned int caps, bool enable)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100593{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100594 unsigned int val = 0;
595
Takashi Iwai352f7f92012-12-19 12:52:06 +0100596 if (caps & AC_AMPCAP_NUM_STEPS) {
597 /* set to 0dB */
598 if (enable)
599 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
600 }
601 if (caps & AC_AMPCAP_MUTE) {
602 if (!enable)
603 val |= HDA_AMP_MUTE;
604 }
605 return val;
606}
607
608/* initialize the amp value (only at the first time) */
609static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
610{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100611 unsigned int caps = query_amp_caps(codec, nid, dir);
612 int val = get_amp_val_to_activate(codec, nid, dir, caps, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100613 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
614}
615
Takashi Iwai8999bf02013-01-18 11:01:33 +0100616/* calculate amp value mask we can modify;
617 * if the given amp is controlled by mixers, don't touch it
618 */
619static unsigned int get_amp_mask_to_modify(struct hda_codec *codec,
620 hda_nid_t nid, int dir, int idx,
621 unsigned int caps)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100622{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100623 unsigned int mask = 0xff;
624
625 if (caps & AC_AMPCAP_MUTE) {
626 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_MUTE_CTL))
627 mask &= ~0x80;
628 }
629 if (caps & AC_AMPCAP_NUM_STEPS) {
630 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
631 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
632 mask &= ~0x7f;
633 }
634 return mask;
635}
636
637static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
638 int idx, int idx_to_check, bool enable)
639{
640 unsigned int caps;
641 unsigned int mask, val;
642
643 if (!enable && is_active_nid(codec, nid, dir, idx))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100644 return;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100645
646 caps = query_amp_caps(codec, nid, dir);
647 val = get_amp_val_to_activate(codec, nid, dir, caps, enable);
648 mask = get_amp_mask_to_modify(codec, nid, dir, idx_to_check, caps);
649 if (!mask)
650 return;
651
652 val &= mask;
653 snd_hda_codec_amp_stereo(codec, nid, dir, idx, mask, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100654}
655
656static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
657 int i, bool enable)
658{
659 hda_nid_t nid = path->path[i];
660 init_amp(codec, nid, HDA_OUTPUT, 0);
Takashi Iwai8999bf02013-01-18 11:01:33 +0100661 activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100662}
663
664static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
665 int i, bool enable, bool add_aamix)
666{
667 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100668 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100669 int n, nums, idx;
670 int type;
671 hda_nid_t nid = path->path[i];
672
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100673 nums = snd_hda_get_conn_list(codec, nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100674 type = get_wcaps_type(get_wcaps(codec, nid));
675 if (type == AC_WID_PIN ||
676 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
677 nums = 1;
678 idx = 0;
679 } else
680 idx = path->idx[i];
681
682 for (n = 0; n < nums; n++)
683 init_amp(codec, nid, HDA_INPUT, n);
684
Takashi Iwai352f7f92012-12-19 12:52:06 +0100685 /* here is a little bit tricky in comparison with activate_amp_out();
686 * when aa-mixer is available, we need to enable the path as well
687 */
688 for (n = 0; n < nums; n++) {
689 if (n != idx && (!add_aamix || conn[n] != spec->mixer_nid))
690 continue;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100691 activate_amp(codec, nid, HDA_INPUT, n, idx, enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 }
693}
694
Takashi Iwai352f7f92012-12-19 12:52:06 +0100695/* activate or deactivate the given path
696 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100698void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
699 bool enable, bool add_aamix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100701 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Takashi Iwai352f7f92012-12-19 12:52:06 +0100703 if (!enable)
704 path->active = false;
705
706 for (i = path->depth - 1; i >= 0; i--) {
707 if (enable && path->multi[i])
708 snd_hda_codec_write_cache(codec, path->path[i], 0,
709 AC_VERB_SET_CONNECT_SEL,
710 path->idx[i]);
711 if (has_amp_in(codec, path, i))
712 activate_amp_in(codec, path, i, enable, add_aamix);
713 if (has_amp_out(codec, path, i))
714 activate_amp_out(codec, path, i, enable);
715 }
716
717 if (enable)
718 path->active = true;
719}
720EXPORT_SYMBOL_HDA(snd_hda_activate_path);
721
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100722/* turn on/off EAPD on the given pin */
723static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
724{
725 struct hda_gen_spec *spec = codec->spec;
726 if (spec->own_eapd_ctl ||
727 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
728 return;
Takashi Iwaiecac3ed2012-12-21 15:23:01 +0100729 if (codec->inv_eapd)
730 enable = !enable;
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100731 snd_hda_codec_update_cache(codec, pin, 0,
732 AC_VERB_SET_EAPD_BTLENABLE,
733 enable ? 0x02 : 0x00);
734}
735
Takashi Iwai352f7f92012-12-19 12:52:06 +0100736
737/*
738 * Helper functions for creating mixer ctl elements
739 */
740
741enum {
742 HDA_CTL_WIDGET_VOL,
743 HDA_CTL_WIDGET_MUTE,
744 HDA_CTL_BIND_MUTE,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100745};
746static const struct snd_kcontrol_new control_templates[] = {
747 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
748 HDA_CODEC_MUTE(NULL, 0, 0, 0),
749 HDA_BIND_MUTE(NULL, 0, 0, 0),
Takashi Iwai352f7f92012-12-19 12:52:06 +0100750};
751
752/* add dynamic controls from template */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100753static struct snd_kcontrol_new *
754add_control(struct hda_gen_spec *spec, int type, const char *name,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100755 int cidx, unsigned long val)
756{
757 struct snd_kcontrol_new *knew;
758
Takashi Iwai12c93df2012-12-19 14:38:33 +0100759 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100760 if (!knew)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100761 return NULL;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100762 knew->index = cidx;
763 if (get_amp_nid_(val))
764 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
765 knew->private_value = val;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100766 return knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100767}
768
769static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
770 const char *pfx, const char *dir,
771 const char *sfx, int cidx, unsigned long val)
772{
773 char name[32];
774 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100775 if (!add_control(spec, type, name, cidx, val))
776 return -ENOMEM;
777 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100778}
779
780#define add_pb_vol_ctrl(spec, type, pfx, val) \
781 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
782#define add_pb_sw_ctrl(spec, type, pfx, val) \
783 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
784#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
785 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
786#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
787 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
788
789static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
790 unsigned int chs, struct nid_path *path)
791{
792 unsigned int val;
793 if (!path)
794 return 0;
795 val = path->ctls[NID_PATH_VOL_CTL];
796 if (!val)
797 return 0;
798 val = amp_val_replace_channels(val, chs);
799 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
800}
801
802/* return the channel bits suitable for the given path->ctls[] */
803static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
804 int type)
805{
806 int chs = 1; /* mono (left only) */
807 if (path) {
808 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
809 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
810 chs = 3; /* stereo */
811 }
812 return chs;
813}
814
815static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
816 struct nid_path *path)
817{
818 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
819 return add_vol_ctl(codec, pfx, cidx, chs, path);
820}
821
822/* create a mute-switch for the given mixer widget;
823 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
824 */
825static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
826 unsigned int chs, struct nid_path *path)
827{
828 unsigned int val;
829 int type = HDA_CTL_WIDGET_MUTE;
830
831 if (!path)
832 return 0;
833 val = path->ctls[NID_PATH_MUTE_CTL];
834 if (!val)
835 return 0;
836 val = amp_val_replace_channels(val, chs);
837 if (get_amp_direction_(val) == HDA_INPUT) {
838 hda_nid_t nid = get_amp_nid_(val);
839 int nums = snd_hda_get_num_conns(codec, nid);
840 if (nums > 1) {
841 type = HDA_CTL_BIND_MUTE;
842 val |= nums << 19;
843 }
844 }
845 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
846}
847
848static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
849 int cidx, struct nid_path *path)
850{
851 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
852 return add_sw_ctl(codec, pfx, cidx, chs, path);
853}
854
Takashi Iwai247d85e2013-01-17 16:18:11 +0100855/* any ctl assigned to the path with the given index? */
856static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
857{
858 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
859 return path && path->ctls[ctl_type];
860}
861
Takashi Iwai352f7f92012-12-19 12:52:06 +0100862static const char * const channel_name[4] = {
863 "Front", "Surround", "CLFE", "Side"
864};
865
866/* give some appropriate ctl name prefix for the given line out channel */
Takashi Iwai247d85e2013-01-17 16:18:11 +0100867static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
868 int *index, int ctl_type)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100869{
Takashi Iwai247d85e2013-01-17 16:18:11 +0100870 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100871 struct auto_pin_cfg *cfg = &spec->autocfg;
872
873 *index = 0;
874 if (cfg->line_outs == 1 && !spec->multi_ios &&
Takashi Iwai247d85e2013-01-17 16:18:11 +0100875 !cfg->hp_outs && !cfg->speaker_outs)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100876 return spec->vmaster_mute.hook ? "PCM" : "Master";
877
878 /* if there is really a single DAC used in the whole output paths,
879 * use it master (or "PCM" if a vmaster hook is present)
880 */
881 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
882 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
883 return spec->vmaster_mute.hook ? "PCM" : "Master";
884
Takashi Iwai247d85e2013-01-17 16:18:11 +0100885 /* multi-io channels */
886 if (ch >= cfg->line_outs)
887 return channel_name[ch];
888
Takashi Iwai352f7f92012-12-19 12:52:06 +0100889 switch (cfg->line_out_type) {
890 case AUTO_PIN_SPEAKER_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +0100891 /* if the primary channel vol/mute is shared with HP volume,
892 * don't name it as Speaker
893 */
894 if (!ch && cfg->hp_outs &&
895 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
896 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100897 if (cfg->line_outs == 1)
898 return "Speaker";
899 if (cfg->line_outs == 2)
900 return ch ? "Bass Speaker" : "Speaker";
901 break;
902 case AUTO_PIN_HP_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +0100903 /* if the primary channel vol/mute is shared with spk volume,
904 * don't name it as Headphone
905 */
906 if (!ch && cfg->speaker_outs &&
907 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
908 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100909 /* for multi-io case, only the primary out */
910 if (ch && spec->multi_ios)
911 break;
912 *index = ch;
913 return "Headphone";
Takashi Iwai352f7f92012-12-19 12:52:06 +0100914 }
Takashi Iwai247d85e2013-01-17 16:18:11 +0100915
916 /* for a single channel output, we don't have to name the channel */
917 if (cfg->line_outs == 1 && !spec->multi_ios)
918 return "PCM";
919
Takashi Iwai352f7f92012-12-19 12:52:06 +0100920 if (ch >= ARRAY_SIZE(channel_name)) {
921 snd_BUG();
922 return "PCM";
923 }
924
925 return channel_name[ch];
926}
927
928/*
929 * Parse output paths
930 */
931
932/* badness definition */
933enum {
934 /* No primary DAC is found for the main output */
935 BAD_NO_PRIMARY_DAC = 0x10000,
936 /* No DAC is found for the extra output */
937 BAD_NO_DAC = 0x4000,
938 /* No possible multi-ios */
939 BAD_MULTI_IO = 0x103,
940 /* No individual DAC for extra output */
941 BAD_NO_EXTRA_DAC = 0x102,
942 /* No individual DAC for extra surrounds */
943 BAD_NO_EXTRA_SURR_DAC = 0x101,
944 /* Primary DAC shared with main surrounds */
945 BAD_SHARED_SURROUND = 0x100,
946 /* Primary DAC shared with main CLFE */
947 BAD_SHARED_CLFE = 0x10,
948 /* Primary DAC shared with extra surrounds */
949 BAD_SHARED_EXTRA_SURROUND = 0x10,
950 /* Volume widget is shared */
951 BAD_SHARED_VOL = 0x10,
952};
953
Takashi Iwai0e614dd2013-01-07 15:11:44 +0100954/* look for widgets in the given path which are appropriate for
Takashi Iwai352f7f92012-12-19 12:52:06 +0100955 * volume and mute controls, and assign the values to ctls[].
956 *
957 * When no appropriate widget is found in the path, the badness value
958 * is incremented depending on the situation. The function returns the
959 * total badness for both volume and mute controls.
960 */
Takashi Iwai0e614dd2013-01-07 15:11:44 +0100961static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100962{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100963 hda_nid_t nid;
964 unsigned int val;
965 int badness = 0;
966
967 if (!path)
968 return BAD_SHARED_VOL * 2;
Takashi Iwai0e614dd2013-01-07 15:11:44 +0100969
970 if (path->ctls[NID_PATH_VOL_CTL] ||
971 path->ctls[NID_PATH_MUTE_CTL])
972 return 0; /* already evaluated */
973
Takashi Iwai352f7f92012-12-19 12:52:06 +0100974 nid = look_for_out_vol_nid(codec, path);
975 if (nid) {
976 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
977 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
978 badness += BAD_SHARED_VOL;
979 else
980 path->ctls[NID_PATH_VOL_CTL] = val;
981 } else
982 badness += BAD_SHARED_VOL;
983 nid = look_for_out_mute_nid(codec, path);
984 if (nid) {
985 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
986 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
987 nid_has_mute(codec, nid, HDA_OUTPUT))
988 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
989 else
990 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
991 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
992 badness += BAD_SHARED_VOL;
993 else
994 path->ctls[NID_PATH_MUTE_CTL] = val;
995 } else
996 badness += BAD_SHARED_VOL;
997 return badness;
998}
999
1000struct badness_table {
1001 int no_primary_dac; /* no primary DAC */
1002 int no_dac; /* no secondary DACs */
1003 int shared_primary; /* primary DAC is shared with main output */
1004 int shared_surr; /* secondary DAC shared with main or primary */
1005 int shared_clfe; /* third DAC shared with main or primary */
1006 int shared_surr_main; /* secondary DAC sahred with main/DAC0 */
1007};
1008
1009static struct badness_table main_out_badness = {
1010 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1011 .no_dac = BAD_NO_DAC,
1012 .shared_primary = BAD_NO_PRIMARY_DAC,
1013 .shared_surr = BAD_SHARED_SURROUND,
1014 .shared_clfe = BAD_SHARED_CLFE,
1015 .shared_surr_main = BAD_SHARED_SURROUND,
1016};
1017
1018static struct badness_table extra_out_badness = {
1019 .no_primary_dac = BAD_NO_DAC,
1020 .no_dac = BAD_NO_DAC,
1021 .shared_primary = BAD_NO_EXTRA_DAC,
1022 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1023 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1024 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1025};
1026
Takashi Iwai7385df62013-01-07 09:50:52 +01001027/* get the DAC of the primary output corresponding to the given array index */
1028static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1029{
1030 struct hda_gen_spec *spec = codec->spec;
1031 struct auto_pin_cfg *cfg = &spec->autocfg;
1032
1033 if (cfg->line_outs > idx)
1034 return spec->private_dac_nids[idx];
1035 idx -= cfg->line_outs;
1036 if (spec->multi_ios > idx)
1037 return spec->multi_io[idx].dac;
1038 return 0;
1039}
1040
1041/* return the DAC if it's reachable, otherwise zero */
1042static inline hda_nid_t try_dac(struct hda_codec *codec,
1043 hda_nid_t dac, hda_nid_t pin)
1044{
1045 return is_reachable_path(codec, dac, pin) ? dac : 0;
1046}
1047
Takashi Iwai352f7f92012-12-19 12:52:06 +01001048/* try to assign DACs to pins and return the resultant badness */
1049static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1050 const hda_nid_t *pins, hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001051 int *path_idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001052 const struct badness_table *bad)
1053{
1054 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001055 int i, j;
1056 int badness = 0;
1057 hda_nid_t dac;
1058
1059 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 return 0;
1061
Takashi Iwai352f7f92012-12-19 12:52:06 +01001062 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001063 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001064 hda_nid_t pin = pins[i];
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001065
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001066 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1067 if (path) {
1068 badness += assign_out_path_ctls(codec, path);
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001069 continue;
1070 }
1071
1072 dacs[i] = look_for_dac(codec, pin, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001073 if (!dacs[i] && !i) {
Takashi Iwai980428c2013-01-09 09:28:20 +01001074 /* try to steal the DAC of surrounds for the front */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001075 for (j = 1; j < num_outs; j++) {
1076 if (is_reachable_path(codec, dacs[j], pin)) {
1077 dacs[0] = dacs[j];
1078 dacs[j] = 0;
Takashi Iwai980428c2013-01-09 09:28:20 +01001079 invalidate_nid_path(codec, path_idx[j]);
Takashi Iwai196c17662013-01-04 15:01:40 +01001080 path_idx[j] = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001081 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 }
1083 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001084 }
1085 dac = dacs[i];
1086 if (!dac) {
Takashi Iwai7385df62013-01-07 09:50:52 +01001087 if (num_outs > 2)
1088 dac = try_dac(codec, get_primary_out(codec, i), pin);
1089 if (!dac)
1090 dac = try_dac(codec, dacs[0], pin);
1091 if (!dac)
1092 dac = try_dac(codec, get_primary_out(codec, i), pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001093 if (dac) {
1094 if (!i)
1095 badness += bad->shared_primary;
1096 else if (i == 1)
1097 badness += bad->shared_surr;
1098 else
1099 badness += bad->shared_clfe;
1100 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1101 dac = spec->private_dac_nids[0];
1102 badness += bad->shared_surr_main;
1103 } else if (!i)
1104 badness += bad->no_primary_dac;
1105 else
1106 badness += bad->no_dac;
1107 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001108 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001109 if (!path && !i && spec->mixer_nid) {
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001110 /* try with aamix */
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001111 path = snd_hda_add_new_path(codec, dac, pin, 0);
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001112 }
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001113 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001114 dac = dacs[i] = 0;
Takashi Iwaie1284af2013-01-03 16:33:02 +01001115 else {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001116 print_nid_path("output", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001117 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001118 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001119 badness += assign_out_path_ctls(codec, path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001120 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001121 }
1122
1123 return badness;
1124}
1125
1126/* return NID if the given pin has only a single connection to a certain DAC */
1127static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1128{
1129 struct hda_gen_spec *spec = codec->spec;
1130 int i;
1131 hda_nid_t nid_found = 0;
1132
1133 for (i = 0; i < spec->num_all_dacs; i++) {
1134 hda_nid_t nid = spec->all_dacs[i];
1135 if (!nid || is_dac_already_used(codec, nid))
1136 continue;
1137 if (is_reachable_path(codec, nid, pin)) {
1138 if (nid_found)
1139 return 0;
1140 nid_found = nid;
1141 }
1142 }
1143 return nid_found;
1144}
1145
1146/* check whether the given pin can be a multi-io pin */
1147static bool can_be_multiio_pin(struct hda_codec *codec,
1148 unsigned int location, hda_nid_t nid)
1149{
1150 unsigned int defcfg, caps;
1151
1152 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1153 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1154 return false;
1155 if (location && get_defcfg_location(defcfg) != location)
1156 return false;
1157 caps = snd_hda_query_pin_caps(codec, nid);
1158 if (!(caps & AC_PINCAP_OUT))
1159 return false;
1160 return true;
1161}
1162
Takashi Iwaie22aab72013-01-04 14:50:04 +01001163/* count the number of input pins that are capable to be multi-io */
1164static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1165{
1166 struct hda_gen_spec *spec = codec->spec;
1167 struct auto_pin_cfg *cfg = &spec->autocfg;
1168 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1169 unsigned int location = get_defcfg_location(defcfg);
1170 int type, i;
1171 int num_pins = 0;
1172
1173 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1174 for (i = 0; i < cfg->num_inputs; i++) {
1175 if (cfg->inputs[i].type != type)
1176 continue;
1177 if (can_be_multiio_pin(codec, location,
1178 cfg->inputs[i].pin))
1179 num_pins++;
1180 }
1181 }
1182 return num_pins;
1183}
1184
Takashi Iwai352f7f92012-12-19 12:52:06 +01001185/*
1186 * multi-io helper
1187 *
1188 * When hardwired is set, try to fill ony hardwired pins, and returns
1189 * zero if any pins are filled, non-zero if nothing found.
1190 * When hardwired is off, try to fill possible input pins, and returns
1191 * the badness value.
1192 */
1193static int fill_multi_ios(struct hda_codec *codec,
1194 hda_nid_t reference_pin,
Takashi Iwaie22aab72013-01-04 14:50:04 +01001195 bool hardwired)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001196{
1197 struct hda_gen_spec *spec = codec->spec;
1198 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie22aab72013-01-04 14:50:04 +01001199 int type, i, j, num_pins, old_pins;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001200 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1201 unsigned int location = get_defcfg_location(defcfg);
1202 int badness = 0;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001203 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001204
1205 old_pins = spec->multi_ios;
1206 if (old_pins >= 2)
1207 goto end_fill;
1208
Takashi Iwaie22aab72013-01-04 14:50:04 +01001209 num_pins = count_multiio_pins(codec, reference_pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001210 if (num_pins < 2)
1211 goto end_fill;
1212
Takashi Iwai352f7f92012-12-19 12:52:06 +01001213 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1214 for (i = 0; i < cfg->num_inputs; i++) {
1215 hda_nid_t nid = cfg->inputs[i].pin;
1216 hda_nid_t dac = 0;
1217
1218 if (cfg->inputs[i].type != type)
1219 continue;
1220 if (!can_be_multiio_pin(codec, location, nid))
1221 continue;
1222 for (j = 0; j < spec->multi_ios; j++) {
1223 if (nid == spec->multi_io[j].pin)
1224 break;
1225 }
1226 if (j < spec->multi_ios)
1227 continue;
1228
Takashi Iwai352f7f92012-12-19 12:52:06 +01001229 if (hardwired)
1230 dac = get_dac_if_single(codec, nid);
1231 else if (!dac)
1232 dac = look_for_dac(codec, nid, false);
1233 if (!dac) {
1234 badness++;
1235 continue;
1236 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001237 path = snd_hda_add_new_path(codec, dac, nid,
1238 -spec->mixer_nid);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001239 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001240 badness++;
1241 continue;
1242 }
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001243 print_nid_path("multiio", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001244 spec->multi_io[spec->multi_ios].pin = nid;
1245 spec->multi_io[spec->multi_ios].dac = dac;
Takashi Iwai196c17662013-01-04 15:01:40 +01001246 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1247 snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001248 spec->multi_ios++;
1249 if (spec->multi_ios >= 2)
1250 break;
1251 }
1252 }
1253 end_fill:
1254 if (badness)
1255 badness = BAD_MULTI_IO;
1256 if (old_pins == spec->multi_ios) {
1257 if (hardwired)
1258 return 1; /* nothing found */
1259 else
1260 return badness; /* no badness if nothing found */
1261 }
1262 if (!hardwired && spec->multi_ios < 2) {
1263 /* cancel newly assigned paths */
1264 spec->paths.used -= spec->multi_ios - old_pins;
1265 spec->multi_ios = old_pins;
1266 return badness;
1267 }
1268
1269 /* assign volume and mute controls */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001270 for (i = old_pins; i < spec->multi_ios; i++) {
1271 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1272 badness += assign_out_path_ctls(codec, path);
1273 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001274
1275 return badness;
1276}
1277
1278/* map DACs for all pins in the list if they are single connections */
1279static bool map_singles(struct hda_codec *codec, int outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001280 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001281{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001282 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001283 int i;
1284 bool found = false;
1285 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001286 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001287 hda_nid_t dac;
1288 if (dacs[i])
1289 continue;
1290 dac = get_dac_if_single(codec, pins[i]);
1291 if (!dac)
1292 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001293 path = snd_hda_add_new_path(codec, dac, pins[i],
1294 -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001295 if (!path && !i && spec->mixer_nid)
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001296 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001297 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001298 dacs[i] = dac;
1299 found = true;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001300 print_nid_path("output", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001301 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001302 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001303 }
1304 }
1305 return found;
1306}
1307
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001308/* create a new path including aamix if available, and return its index */
1309static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1310{
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001311 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001312 struct nid_path *path;
1313
1314 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001315 if (!path || !path->depth ||
1316 is_nid_contained(path, spec->mixer_nid))
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001317 return 0;
1318 path = snd_hda_add_new_path(codec, path->path[0],
1319 path->path[path->depth - 1],
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001320 spec->mixer_nid);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001321 if (!path)
1322 return 0;
1323 print_nid_path("output-aamix", path);
1324 path->active = false; /* unused as default */
1325 return snd_hda_get_path_idx(codec, path);
1326}
1327
Takashi Iwaia07a9492013-01-07 16:44:06 +01001328/* fill the empty entries in the dac array for speaker/hp with the
1329 * shared dac pointed by the paths
1330 */
1331static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1332 hda_nid_t *dacs, int *path_idx)
1333{
1334 struct nid_path *path;
1335 int i;
1336
1337 for (i = 0; i < num_outs; i++) {
1338 if (dacs[i])
1339 continue;
1340 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1341 if (!path)
1342 continue;
1343 dacs[i] = path->path[0];
1344 }
1345}
1346
Takashi Iwai352f7f92012-12-19 12:52:06 +01001347/* fill in the dac_nids table from the parsed pin configuration */
1348static int fill_and_eval_dacs(struct hda_codec *codec,
1349 bool fill_hardwired,
1350 bool fill_mio_first)
1351{
1352 struct hda_gen_spec *spec = codec->spec;
1353 struct auto_pin_cfg *cfg = &spec->autocfg;
1354 int i, err, badness;
Takashi Iwaiea46c3c2013-01-15 18:45:53 +01001355 unsigned int val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001356
1357 /* set num_dacs once to full for look_for_dac() */
1358 spec->multiout.num_dacs = cfg->line_outs;
1359 spec->multiout.dac_nids = spec->private_dac_nids;
1360 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1361 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1362 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1363 spec->multi_ios = 0;
1364 snd_array_free(&spec->paths);
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001365
1366 /* clear path indices */
1367 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1368 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1369 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1370 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1371 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
Takashi Iwaic697b712013-01-07 17:09:26 +01001372 memset(spec->input_paths, 0, sizeof(spec->input_paths));
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001373 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1374 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1375
Takashi Iwai352f7f92012-12-19 12:52:06 +01001376 badness = 0;
1377
1378 /* fill hard-wired DACs first */
1379 if (fill_hardwired) {
1380 bool mapped;
1381 do {
1382 mapped = map_singles(codec, cfg->line_outs,
1383 cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001384 spec->private_dac_nids,
1385 spec->out_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001386 mapped |= map_singles(codec, cfg->hp_outs,
1387 cfg->hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001388 spec->multiout.hp_out_nid,
1389 spec->hp_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001390 mapped |= map_singles(codec, cfg->speaker_outs,
1391 cfg->speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001392 spec->multiout.extra_out_nid,
1393 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001394 if (fill_mio_first && cfg->line_outs == 1 &&
1395 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001396 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001397 if (!err)
1398 mapped = true;
1399 }
1400 } while (mapped);
1401 }
1402
1403 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001404 spec->private_dac_nids, spec->out_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001405 &main_out_badness);
1406
Takashi Iwai352f7f92012-12-19 12:52:06 +01001407 if (fill_mio_first &&
1408 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1409 /* try to fill multi-io first */
Takashi Iwaie22aab72013-01-04 14:50:04 +01001410 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001411 if (err < 0)
1412 return err;
1413 /* we don't count badness at this stage yet */
1414 }
1415
1416 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1417 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1418 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001419 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001420 &extra_out_badness);
1421 if (err < 0)
1422 return err;
1423 badness += err;
1424 }
1425 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1426 err = try_assign_dacs(codec, cfg->speaker_outs,
1427 cfg->speaker_pins,
1428 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001429 spec->speaker_paths,
1430 &extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001431 if (err < 0)
1432 return err;
1433 badness += err;
1434 }
1435 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001436 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001437 if (err < 0)
1438 return err;
1439 badness += err;
1440 }
Takashi Iwaie22aab72013-01-04 14:50:04 +01001441
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001442 if (spec->mixer_nid) {
1443 spec->aamix_out_paths[0] =
1444 check_aamix_out_path(codec, spec->out_paths[0]);
1445 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1446 spec->aamix_out_paths[1] =
1447 check_aamix_out_path(codec, spec->hp_paths[0]);
1448 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1449 spec->aamix_out_paths[2] =
1450 check_aamix_out_path(codec, spec->speaker_paths[0]);
1451 }
1452
Takashi Iwaie22aab72013-01-04 14:50:04 +01001453 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1454 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1455 spec->multi_ios = 1; /* give badness */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001456
Takashi Iwaia07a9492013-01-07 16:44:06 +01001457 /* re-count num_dacs and squash invalid entries */
1458 spec->multiout.num_dacs = 0;
1459 for (i = 0; i < cfg->line_outs; i++) {
1460 if (spec->private_dac_nids[i])
1461 spec->multiout.num_dacs++;
1462 else {
1463 memmove(spec->private_dac_nids + i,
1464 spec->private_dac_nids + i + 1,
1465 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1466 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1467 }
1468 }
1469
1470 spec->ext_channel_count = spec->min_channel_count =
David Henningssonc0f3b212013-01-16 11:45:37 +01001471 spec->multiout.num_dacs * 2;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001472
Takashi Iwai352f7f92012-12-19 12:52:06 +01001473 if (spec->multi_ios == 2) {
1474 for (i = 0; i < 2; i++)
1475 spec->private_dac_nids[spec->multiout.num_dacs++] =
1476 spec->multi_io[i].dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001477 } else if (spec->multi_ios) {
1478 spec->multi_ios = 0;
1479 badness += BAD_MULTI_IO;
1480 }
1481
Takashi Iwaia07a9492013-01-07 16:44:06 +01001482 /* re-fill the shared DAC for speaker / headphone */
1483 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1484 refill_shared_dacs(codec, cfg->hp_outs,
1485 spec->multiout.hp_out_nid,
1486 spec->hp_paths);
1487 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1488 refill_shared_dacs(codec, cfg->speaker_outs,
1489 spec->multiout.extra_out_nid,
1490 spec->speaker_paths);
1491
Takashi Iwai2c12c302013-01-10 09:33:29 +01001492 /* set initial pinctl targets */
Takashi Iwaiea46c3c2013-01-15 18:45:53 +01001493 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
1494 val = PIN_HP;
1495 else
1496 val = PIN_OUT;
1497 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
Takashi Iwai2c12c302013-01-10 09:33:29 +01001498 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1499 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
Takashi Iwaiea46c3c2013-01-15 18:45:53 +01001500 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1501 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
Takashi Iwai2c12c302013-01-10 09:33:29 +01001502 set_pin_targets(codec, cfg->speaker_outs,
Takashi Iwaiea46c3c2013-01-15 18:45:53 +01001503 cfg->speaker_pins, val);
1504 }
Takashi Iwai2c12c302013-01-10 09:33:29 +01001505
Takashi Iwai352f7f92012-12-19 12:52:06 +01001506 return badness;
1507}
1508
1509#define DEBUG_BADNESS
1510
1511#ifdef DEBUG_BADNESS
1512#define debug_badness snd_printdd
1513#else
1514#define debug_badness(...)
1515#endif
1516
1517static void debug_show_configs(struct hda_gen_spec *spec, struct auto_pin_cfg *cfg)
1518{
1519 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1520 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001521 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001522 spec->multiout.dac_nids[0],
1523 spec->multiout.dac_nids[1],
1524 spec->multiout.dac_nids[2],
1525 spec->multiout.dac_nids[3]);
1526 if (spec->multi_ios > 0)
1527 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1528 spec->multi_ios,
1529 spec->multi_io[0].pin, spec->multi_io[1].pin,
1530 spec->multi_io[0].dac, spec->multi_io[1].dac);
1531 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1532 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001533 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001534 spec->multiout.hp_out_nid[0],
1535 spec->multiout.hp_out_nid[1],
1536 spec->multiout.hp_out_nid[2],
1537 spec->multiout.hp_out_nid[3]);
1538 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1539 cfg->speaker_pins[0], cfg->speaker_pins[1],
1540 cfg->speaker_pins[2], cfg->speaker_pins[3],
1541 spec->multiout.extra_out_nid[0],
1542 spec->multiout.extra_out_nid[1],
1543 spec->multiout.extra_out_nid[2],
1544 spec->multiout.extra_out_nid[3]);
1545}
1546
1547/* find all available DACs of the codec */
1548static void fill_all_dac_nids(struct hda_codec *codec)
1549{
1550 struct hda_gen_spec *spec = codec->spec;
1551 int i;
1552 hda_nid_t nid = codec->start_nid;
1553
1554 spec->num_all_dacs = 0;
1555 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1556 for (i = 0; i < codec->num_nodes; i++, nid++) {
1557 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1558 continue;
1559 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1560 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1561 break;
1562 }
1563 spec->all_dacs[spec->num_all_dacs++] = nid;
1564 }
1565}
1566
1567static int parse_output_paths(struct hda_codec *codec)
1568{
1569 struct hda_gen_spec *spec = codec->spec;
1570 struct auto_pin_cfg *cfg = &spec->autocfg;
1571 struct auto_pin_cfg *best_cfg;
1572 int best_badness = INT_MAX;
1573 int badness;
1574 bool fill_hardwired = true, fill_mio_first = true;
1575 bool best_wired = true, best_mio = true;
1576 bool hp_spk_swapped = false;
1577
Takashi Iwai352f7f92012-12-19 12:52:06 +01001578 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1579 if (!best_cfg)
1580 return -ENOMEM;
1581 *best_cfg = *cfg;
1582
1583 for (;;) {
1584 badness = fill_and_eval_dacs(codec, fill_hardwired,
1585 fill_mio_first);
1586 if (badness < 0) {
1587 kfree(best_cfg);
1588 return badness;
1589 }
1590 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1591 cfg->line_out_type, fill_hardwired, fill_mio_first,
1592 badness);
1593 debug_show_configs(spec, cfg);
1594 if (badness < best_badness) {
1595 best_badness = badness;
1596 *best_cfg = *cfg;
1597 best_wired = fill_hardwired;
1598 best_mio = fill_mio_first;
1599 }
1600 if (!badness)
1601 break;
1602 fill_mio_first = !fill_mio_first;
1603 if (!fill_mio_first)
1604 continue;
1605 fill_hardwired = !fill_hardwired;
1606 if (!fill_hardwired)
1607 continue;
1608 if (hp_spk_swapped)
1609 break;
1610 hp_spk_swapped = true;
1611 if (cfg->speaker_outs > 0 &&
1612 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1613 cfg->hp_outs = cfg->line_outs;
1614 memcpy(cfg->hp_pins, cfg->line_out_pins,
1615 sizeof(cfg->hp_pins));
1616 cfg->line_outs = cfg->speaker_outs;
1617 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1618 sizeof(cfg->speaker_pins));
1619 cfg->speaker_outs = 0;
1620 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1621 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1622 fill_hardwired = true;
1623 continue;
1624 }
1625 if (cfg->hp_outs > 0 &&
1626 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1627 cfg->speaker_outs = cfg->line_outs;
1628 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1629 sizeof(cfg->speaker_pins));
1630 cfg->line_outs = cfg->hp_outs;
1631 memcpy(cfg->line_out_pins, cfg->hp_pins,
1632 sizeof(cfg->hp_pins));
1633 cfg->hp_outs = 0;
1634 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1635 cfg->line_out_type = AUTO_PIN_HP_OUT;
1636 fill_hardwired = true;
1637 continue;
1638 }
1639 break;
1640 }
1641
1642 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001643 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001644 *cfg = *best_cfg;
1645 fill_and_eval_dacs(codec, best_wired, best_mio);
1646 }
1647 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1648 cfg->line_out_type, best_wired, best_mio);
1649 debug_show_configs(spec, cfg);
1650
1651 if (cfg->line_out_pins[0]) {
1652 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01001653 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001654 if (path)
1655 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01001656 if (spec->vmaster_nid)
1657 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
1658 HDA_OUTPUT, spec->vmaster_tlv);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001659 }
1660
1661 kfree(best_cfg);
1662 return 0;
1663}
1664
1665/* add playback controls from the parsed DAC table */
1666static int create_multi_out_ctls(struct hda_codec *codec,
1667 const struct auto_pin_cfg *cfg)
1668{
1669 struct hda_gen_spec *spec = codec->spec;
1670 int i, err, noutputs;
1671
1672 noutputs = cfg->line_outs;
1673 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1674 noutputs += spec->multi_ios;
1675
1676 for (i = 0; i < noutputs; i++) {
1677 const char *name;
1678 int index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001679 struct nid_path *path;
1680
Takashi Iwai196c17662013-01-04 15:01:40 +01001681 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001682 if (!path)
1683 continue;
Takashi Iwai247d85e2013-01-17 16:18:11 +01001684
1685 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001686 if (!name || !strcmp(name, "CLFE")) {
1687 /* Center/LFE */
1688 err = add_vol_ctl(codec, "Center", 0, 1, path);
1689 if (err < 0)
1690 return err;
1691 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1692 if (err < 0)
1693 return err;
Takashi Iwai247d85e2013-01-17 16:18:11 +01001694 } else {
1695 err = add_stereo_vol(codec, name, index, path);
1696 if (err < 0)
1697 return err;
1698 }
1699
1700 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
1701 if (!name || !strcmp(name, "CLFE")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001702 err = add_sw_ctl(codec, "Center", 0, 1, path);
1703 if (err < 0)
1704 return err;
1705 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1706 if (err < 0)
1707 return err;
1708 } else {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001709 err = add_stereo_sw(codec, name, index, path);
1710 if (err < 0)
1711 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 }
1713 }
1714 return 0;
1715}
1716
Takashi Iwaic2c80382013-01-07 10:33:57 +01001717static int create_extra_out(struct hda_codec *codec, int path_idx,
Takashi Iwai196c17662013-01-04 15:01:40 +01001718 const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001720 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 int err;
1722
Takashi Iwai196c17662013-01-04 15:01:40 +01001723 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001724 if (!path)
1725 return 0;
Takashi Iwaic2c80382013-01-07 10:33:57 +01001726 err = add_stereo_vol(codec, pfx, cidx, path);
1727 if (err < 0)
1728 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001729 err = add_stereo_sw(codec, pfx, cidx, path);
1730 if (err < 0)
1731 return err;
1732 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733}
1734
Takashi Iwai352f7f92012-12-19 12:52:06 +01001735/* add playback controls for speaker and HP outputs */
1736static int create_extra_outs(struct hda_codec *codec, int num_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001737 const int *paths, const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738{
Takashi Iwaic2c80382013-01-07 10:33:57 +01001739 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001740
1741 for (i = 0; i < num_pins; i++) {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001742 const char *name;
1743 char tmp[44];
1744 int err, idx = 0;
1745
1746 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
1747 name = "Bass Speaker";
1748 else if (num_pins >= 3) {
1749 snprintf(tmp, sizeof(tmp), "%s %s",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001750 pfx, channel_name[i]);
Takashi Iwaic2c80382013-01-07 10:33:57 +01001751 name = tmp;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001752 } else {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001753 name = pfx;
1754 idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 }
Takashi Iwaic2c80382013-01-07 10:33:57 +01001756 err = create_extra_out(codec, paths[i], name, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001757 if (err < 0)
1758 return err;
1759 }
1760 return 0;
1761}
Takashi Iwai97ec5582006-03-21 11:29:07 +01001762
Takashi Iwai352f7f92012-12-19 12:52:06 +01001763static int create_hp_out_ctls(struct hda_codec *codec)
1764{
1765 struct hda_gen_spec *spec = codec->spec;
1766 return create_extra_outs(codec, spec->autocfg.hp_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001767 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001768 "Headphone");
1769}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
Takashi Iwai352f7f92012-12-19 12:52:06 +01001771static int create_speaker_out_ctls(struct hda_codec *codec)
1772{
1773 struct hda_gen_spec *spec = codec->spec;
1774 return create_extra_outs(codec, spec->autocfg.speaker_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001775 spec->speaker_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001776 "Speaker");
1777}
1778
1779/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001780 * independent HP controls
1781 */
1782
1783static int indep_hp_info(struct snd_kcontrol *kcontrol,
1784 struct snd_ctl_elem_info *uinfo)
1785{
1786 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
1787}
1788
1789static int indep_hp_get(struct snd_kcontrol *kcontrol,
1790 struct snd_ctl_elem_value *ucontrol)
1791{
1792 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1793 struct hda_gen_spec *spec = codec->spec;
1794 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
1795 return 0;
1796}
1797
1798static int indep_hp_put(struct snd_kcontrol *kcontrol,
1799 struct snd_ctl_elem_value *ucontrol)
1800{
1801 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1802 struct hda_gen_spec *spec = codec->spec;
1803 unsigned int select = ucontrol->value.enumerated.item[0];
1804 int ret = 0;
1805
1806 mutex_lock(&spec->pcm_mutex);
1807 if (spec->active_streams) {
1808 ret = -EBUSY;
1809 goto unlock;
1810 }
1811
1812 if (spec->indep_hp_enabled != select) {
1813 spec->indep_hp_enabled = select;
1814 if (spec->indep_hp_enabled)
1815 spec->multiout.hp_out_nid[0] = 0;
1816 else
1817 spec->multiout.hp_out_nid[0] = spec->alt_dac_nid;
1818 ret = 1;
1819 }
1820 unlock:
1821 mutex_unlock(&spec->pcm_mutex);
1822 return ret;
1823}
1824
1825static const struct snd_kcontrol_new indep_hp_ctl = {
1826 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1827 .name = "Independent HP",
1828 .info = indep_hp_info,
1829 .get = indep_hp_get,
1830 .put = indep_hp_put,
1831};
1832
1833
1834static int create_indep_hp_ctls(struct hda_codec *codec)
1835{
1836 struct hda_gen_spec *spec = codec->spec;
1837
1838 if (!spec->indep_hp)
1839 return 0;
1840 if (!spec->multiout.hp_out_nid[0]) {
1841 spec->indep_hp = 0;
1842 return 0;
1843 }
1844
1845 spec->indep_hp_enabled = false;
1846 spec->alt_dac_nid = spec->multiout.hp_out_nid[0];
1847 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
1848 return -ENOMEM;
1849 return 0;
1850}
1851
1852/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01001853 * channel mode enum control
1854 */
1855
1856static int ch_mode_info(struct snd_kcontrol *kcontrol,
1857 struct snd_ctl_elem_info *uinfo)
1858{
1859 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1860 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001861 int chs;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001862
1863 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1864 uinfo->count = 1;
1865 uinfo->value.enumerated.items = spec->multi_ios + 1;
1866 if (uinfo->value.enumerated.item > spec->multi_ios)
1867 uinfo->value.enumerated.item = spec->multi_ios;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001868 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
1869 sprintf(uinfo->value.enumerated.name, "%dch", chs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001870 return 0;
1871}
1872
1873static int ch_mode_get(struct snd_kcontrol *kcontrol,
1874 struct snd_ctl_elem_value *ucontrol)
1875{
1876 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1877 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001878 ucontrol->value.enumerated.item[0] =
1879 (spec->ext_channel_count - spec->min_channel_count) / 2;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001880 return 0;
1881}
1882
Takashi Iwai196c17662013-01-04 15:01:40 +01001883static inline struct nid_path *
1884get_multiio_path(struct hda_codec *codec, int idx)
1885{
1886 struct hda_gen_spec *spec = codec->spec;
1887 return snd_hda_get_path_from_idx(codec,
1888 spec->out_paths[spec->autocfg.line_outs + idx]);
1889}
1890
Takashi Iwaia5cc2502013-01-16 18:08:55 +01001891static void update_automute_all(struct hda_codec *codec);
1892
Takashi Iwai352f7f92012-12-19 12:52:06 +01001893static int set_multi_io(struct hda_codec *codec, int idx, bool output)
1894{
1895 struct hda_gen_spec *spec = codec->spec;
1896 hda_nid_t nid = spec->multi_io[idx].pin;
1897 struct nid_path *path;
1898
Takashi Iwai196c17662013-01-04 15:01:40 +01001899 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001900 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001902
1903 if (path->active == output)
1904 return 0;
1905
1906 if (output) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01001907 set_pin_target(codec, nid, PIN_OUT, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001908 snd_hda_activate_path(codec, path, true, true);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01001909 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001910 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01001911 set_pin_eapd(codec, nid, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001912 snd_hda_activate_path(codec, path, false, true);
Takashi Iwai2c12c302013-01-10 09:33:29 +01001913 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 }
Takashi Iwaia365fed2013-01-10 16:10:06 +01001915
1916 /* update jack retasking in case it modifies any of them */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01001917 update_automute_all(codec);
Takashi Iwaia365fed2013-01-10 16:10:06 +01001918
Takashi Iwai352f7f92012-12-19 12:52:06 +01001919 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920}
1921
Takashi Iwai352f7f92012-12-19 12:52:06 +01001922static int ch_mode_put(struct snd_kcontrol *kcontrol,
1923 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001925 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1926 struct hda_gen_spec *spec = codec->spec;
1927 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
Takashi Iwai352f7f92012-12-19 12:52:06 +01001929 ch = ucontrol->value.enumerated.item[0];
1930 if (ch < 0 || ch > spec->multi_ios)
1931 return -EINVAL;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001932 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001933 return 0;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001934 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001935 for (i = 0; i < spec->multi_ios; i++)
1936 set_multi_io(codec, i, i < ch);
1937 spec->multiout.max_channels = max(spec->ext_channel_count,
1938 spec->const_channel_count);
1939 if (spec->need_dac_fix)
1940 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 return 1;
1942}
1943
Takashi Iwai352f7f92012-12-19 12:52:06 +01001944static const struct snd_kcontrol_new channel_mode_enum = {
1945 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1946 .name = "Channel Mode",
1947 .info = ch_mode_info,
1948 .get = ch_mode_get,
1949 .put = ch_mode_put,
1950};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
Takashi Iwai352f7f92012-12-19 12:52:06 +01001952static int create_multi_channel_mode(struct hda_codec *codec)
1953{
1954 struct hda_gen_spec *spec = codec->spec;
1955
1956 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01001957 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01001958 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 return 0;
1961}
1962
Takashi Iwai352f7f92012-12-19 12:52:06 +01001963/*
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001964 * aamix loopback enable/disable switch
1965 */
1966
1967#define loopback_mixing_info indep_hp_info
1968
1969static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
1970 struct snd_ctl_elem_value *ucontrol)
1971{
1972 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1973 struct hda_gen_spec *spec = codec->spec;
1974 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
1975 return 0;
1976}
1977
1978static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
1979 int nomix_path_idx, int mix_path_idx)
1980{
1981 struct nid_path *nomix_path, *mix_path;
1982
1983 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
1984 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
1985 if (!nomix_path || !mix_path)
1986 return;
1987 if (do_mix) {
1988 snd_hda_activate_path(codec, nomix_path, false, true);
1989 snd_hda_activate_path(codec, mix_path, true, true);
1990 } else {
1991 snd_hda_activate_path(codec, mix_path, false, true);
1992 snd_hda_activate_path(codec, nomix_path, true, true);
1993 }
1994}
1995
1996static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
1997 struct snd_ctl_elem_value *ucontrol)
1998{
1999 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2000 struct hda_gen_spec *spec = codec->spec;
2001 unsigned int val = ucontrol->value.enumerated.item[0];
2002
2003 if (val == spec->aamix_mode)
2004 return 0;
2005 spec->aamix_mode = val;
2006 update_aamix_paths(codec, val, spec->out_paths[0],
2007 spec->aamix_out_paths[0]);
2008 update_aamix_paths(codec, val, spec->hp_paths[0],
2009 spec->aamix_out_paths[1]);
2010 update_aamix_paths(codec, val, spec->speaker_paths[0],
2011 spec->aamix_out_paths[2]);
2012 return 1;
2013}
2014
2015static const struct snd_kcontrol_new loopback_mixing_enum = {
2016 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2017 .name = "Loopback Mixing",
2018 .info = loopback_mixing_info,
2019 .get = loopback_mixing_get,
2020 .put = loopback_mixing_put,
2021};
2022
2023static int create_loopback_mixing_ctl(struct hda_codec *codec)
2024{
2025 struct hda_gen_spec *spec = codec->spec;
2026
2027 if (!spec->mixer_nid)
2028 return 0;
2029 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
2030 spec->aamix_out_paths[2]))
2031 return 0;
2032 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2033 return -ENOMEM;
2034 return 0;
2035}
2036
2037/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002038 * shared headphone/mic handling
2039 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002040
Takashi Iwai352f7f92012-12-19 12:52:06 +01002041static void call_update_outputs(struct hda_codec *codec);
2042
2043/* for shared I/O, change the pin-control accordingly */
2044static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic)
2045{
2046 struct hda_gen_spec *spec = codec->spec;
2047 unsigned int val;
2048 hda_nid_t pin = spec->autocfg.inputs[1].pin;
2049 /* NOTE: this assumes that there are only two inputs, the
2050 * first is the real internal mic and the second is HP/mic jack.
2051 */
2052
2053 val = snd_hda_get_default_vref(codec, pin);
2054
2055 /* This pin does not have vref caps - let's enable vref on pin 0x18
2056 instead, as suggested by Realtek */
2057 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2058 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2059 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2060 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01002061 snd_hda_set_pin_ctl_cache(codec, vref_pin,
2062 PIN_IN | (set_as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02002063 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002064
2065 val = set_as_mic ? val | PIN_IN : PIN_HP;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002066 set_pin_target(codec, pin, val, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002067
2068 spec->automute_speaker = !set_as_mic;
2069 call_update_outputs(codec);
2070}
2071
2072/* create a shared input with the headphone out */
2073static int create_shared_input(struct hda_codec *codec)
2074{
2075 struct hda_gen_spec *spec = codec->spec;
2076 struct auto_pin_cfg *cfg = &spec->autocfg;
2077 unsigned int defcfg;
2078 hda_nid_t nid;
2079
2080 /* only one internal input pin? */
2081 if (cfg->num_inputs != 1)
2082 return 0;
2083 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2084 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2085 return 0;
2086
2087 if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
2088 nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
2089 else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
2090 nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
2091 else
2092 return 0; /* both not available */
2093
2094 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2095 return 0; /* no input */
2096
2097 cfg->inputs[1].pin = nid;
2098 cfg->inputs[1].type = AUTO_PIN_MIC;
2099 cfg->num_inputs = 2;
2100 spec->shared_mic_hp = 1;
2101 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
2102 return 0;
2103}
2104
Takashi Iwai978e77e2013-01-10 16:57:58 +01002105/*
2106 * output jack mode
2107 */
2108static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2109 struct snd_ctl_elem_info *uinfo)
2110{
2111 static const char * const texts[] = {
2112 "Line Out", "Headphone Out",
2113 };
2114 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, texts);
2115}
2116
2117static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2118 struct snd_ctl_elem_value *ucontrol)
2119{
2120 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2121 hda_nid_t nid = kcontrol->private_value;
2122 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2123 ucontrol->value.enumerated.item[0] = 1;
2124 else
2125 ucontrol->value.enumerated.item[0] = 0;
2126 return 0;
2127}
2128
2129static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2130 struct snd_ctl_elem_value *ucontrol)
2131{
2132 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2133 hda_nid_t nid = kcontrol->private_value;
2134 unsigned int val;
2135
2136 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2137 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2138 return 0;
2139 snd_hda_set_pin_ctl_cache(codec, nid, val);
2140 return 1;
2141}
2142
2143static const struct snd_kcontrol_new out_jack_mode_enum = {
2144 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2145 .info = out_jack_mode_info,
2146 .get = out_jack_mode_get,
2147 .put = out_jack_mode_put,
2148};
2149
2150static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2151{
2152 struct hda_gen_spec *spec = codec->spec;
2153 int i;
2154
2155 for (i = 0; i < spec->kctls.used; i++) {
2156 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2157 if (!strcmp(kctl->name, name) && kctl->index == idx)
2158 return true;
2159 }
2160 return false;
2161}
2162
2163static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2164 char *name, size_t name_len)
2165{
2166 struct hda_gen_spec *spec = codec->spec;
2167 int idx = 0;
2168
2169 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2170 strlcat(name, " Jack Mode", name_len);
2171
2172 for (; find_kctl_name(codec, name, idx); idx++)
2173 ;
2174}
2175
2176static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2177 hda_nid_t *pins)
2178{
2179 struct hda_gen_spec *spec = codec->spec;
2180 int i;
2181
2182 for (i = 0; i < num_pins; i++) {
2183 hda_nid_t pin = pins[i];
2184 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2185 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV)) {
2186 struct snd_kcontrol_new *knew;
2187 char name[44];
2188 get_jack_mode_name(codec, pin, name, sizeof(name));
2189 knew = snd_hda_gen_add_kctl(spec, name,
2190 &out_jack_mode_enum);
2191 if (!knew)
2192 return -ENOMEM;
2193 knew->private_value = pin;
2194 }
2195 }
2196
2197 return 0;
2198}
2199
Takashi Iwai294765582013-01-17 09:52:11 +01002200/*
2201 * input jack mode
2202 */
2203
2204/* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2205#define NUM_VREFS 6
2206
2207static const char * const vref_texts[NUM_VREFS] = {
2208 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2209 "", "Mic 80pc Bias", "Mic 100pc Bias"
2210};
2211
2212static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2213{
2214 unsigned int pincap;
2215
2216 pincap = snd_hda_query_pin_caps(codec, pin);
2217 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2218 /* filter out unusual vrefs */
2219 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2220 return pincap;
2221}
2222
2223/* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2224static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2225{
2226 unsigned int i, n = 0;
2227
2228 for (i = 0; i < NUM_VREFS; i++) {
2229 if (vref_caps & (1 << i)) {
2230 if (n == item_idx)
2231 return i;
2232 n++;
2233 }
2234 }
2235 return 0;
2236}
2237
2238/* convert back from the vref ctl index to the enum item index */
2239static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2240{
2241 unsigned int i, n = 0;
2242
2243 for (i = 0; i < NUM_VREFS; i++) {
2244 if (i == idx)
2245 return n;
2246 if (vref_caps & (1 << i))
2247 n++;
2248 }
2249 return 0;
2250}
2251
2252static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2253 struct snd_ctl_elem_info *uinfo)
2254{
2255 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2256 hda_nid_t nid = kcontrol->private_value;
2257 unsigned int vref_caps = get_vref_caps(codec, nid);
2258
2259 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2260 vref_texts);
2261 /* set the right text */
2262 strcpy(uinfo->value.enumerated.name,
2263 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2264 return 0;
2265}
2266
2267static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2268 struct snd_ctl_elem_value *ucontrol)
2269{
2270 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2271 hda_nid_t nid = kcontrol->private_value;
2272 unsigned int vref_caps = get_vref_caps(codec, nid);
2273 unsigned int idx;
2274
2275 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2276 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2277 return 0;
2278}
2279
2280static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2281 struct snd_ctl_elem_value *ucontrol)
2282{
2283 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2284 hda_nid_t nid = kcontrol->private_value;
2285 unsigned int vref_caps = get_vref_caps(codec, nid);
2286 unsigned int val, idx;
2287
2288 val = snd_hda_codec_get_pin_target(codec, nid);
2289 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2290 if (idx == ucontrol->value.enumerated.item[0])
2291 return 0;
2292
2293 val &= ~AC_PINCTL_VREFEN;
2294 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2295 snd_hda_set_pin_ctl_cache(codec, nid, val);
2296 return 1;
2297}
2298
2299static const struct snd_kcontrol_new in_jack_mode_enum = {
2300 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2301 .info = in_jack_mode_info,
2302 .get = in_jack_mode_get,
2303 .put = in_jack_mode_put,
2304};
2305
2306static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2307{
2308 struct hda_gen_spec *spec = codec->spec;
2309 unsigned int defcfg;
2310 struct snd_kcontrol_new *knew;
2311 char name[44];
2312
2313 /* no jack mode for fixed pins */
2314 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2315 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2316 return 0;
2317
2318 /* no multiple vref caps? */
2319 if (hweight32(get_vref_caps(codec, pin)) <= 1)
2320 return 0;
2321
2322 get_jack_mode_name(codec, pin, name, sizeof(name));
2323 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2324 if (!knew)
2325 return -ENOMEM;
2326 knew->private_value = pin;
2327 return 0;
2328}
2329
Takashi Iwai352f7f92012-12-19 12:52:06 +01002330
2331/*
2332 * Parse input paths
2333 */
2334
2335#ifdef CONFIG_PM
2336/* add the powersave loopback-list entry */
2337static void add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
2338{
2339 struct hda_amp_list *list;
2340
2341 if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
2342 return;
2343 list = spec->loopback_list + spec->num_loopbacks;
2344 list->nid = mix;
2345 list->dir = HDA_INPUT;
2346 list->idx = idx;
2347 spec->num_loopbacks++;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002348 spec->loopback.amplist = spec->loopback_list;
2349}
2350#else
Takashi Iwai352f7f92012-12-19 12:52:06 +01002351#define add_loopback_list(spec, mix, idx) /* NOP */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002352#endif
2353
Takashi Iwai352f7f92012-12-19 12:52:06 +01002354/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01002355static int new_analog_input(struct hda_codec *codec, int input_idx,
2356 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002357 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002359 struct hda_gen_spec *spec = codec->spec;
2360 struct nid_path *path;
2361 unsigned int val;
2362 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
Takashi Iwai352f7f92012-12-19 12:52:06 +01002364 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
2365 !nid_has_mute(codec, mix_nid, HDA_INPUT))
2366 return 0; /* no need for analog loopback */
2367
Takashi Iwai3ca529d2013-01-07 17:25:08 +01002368 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002369 if (!path)
2370 return -EINVAL;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002371 print_nid_path("loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01002372 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002373
2374 idx = path->idx[path->depth - 1];
2375 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
2376 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2377 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002378 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002380 path->ctls[NID_PATH_VOL_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 }
2382
Takashi Iwai352f7f92012-12-19 12:52:06 +01002383 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
2384 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2385 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002386 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002388 path->ctls[NID_PATH_MUTE_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 }
2390
Takashi Iwai352f7f92012-12-19 12:52:06 +01002391 path->active = true;
2392 add_loopback_list(spec, mix_nid, idx);
2393 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394}
2395
Takashi Iwai352f7f92012-12-19 12:52:06 +01002396static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002398 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
2399 return (pincap & AC_PINCAP_IN) != 0;
2400}
2401
2402/* Parse the codec tree and retrieve ADCs */
2403static int fill_adc_nids(struct hda_codec *codec)
2404{
2405 struct hda_gen_spec *spec = codec->spec;
2406 hda_nid_t nid;
2407 hda_nid_t *adc_nids = spec->adc_nids;
2408 int max_nums = ARRAY_SIZE(spec->adc_nids);
2409 int i, nums = 0;
2410
2411 nid = codec->start_nid;
2412 for (i = 0; i < codec->num_nodes; i++, nid++) {
2413 unsigned int caps = get_wcaps(codec, nid);
2414 int type = get_wcaps_type(caps);
2415
2416 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2417 continue;
2418 adc_nids[nums] = nid;
2419 if (++nums >= max_nums)
2420 break;
2421 }
2422 spec->num_adc_nids = nums;
Takashi Iwai0ffd5342013-01-17 15:53:29 +01002423
2424 /* copy the detected ADCs to all_adcs[] */
2425 spec->num_all_adcs = nums;
2426 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
2427
Takashi Iwai352f7f92012-12-19 12:52:06 +01002428 return nums;
2429}
2430
2431/* filter out invalid adc_nids that don't give all active input pins;
2432 * if needed, check whether dynamic ADC-switching is available
2433 */
2434static int check_dyn_adc_switch(struct hda_codec *codec)
2435{
2436 struct hda_gen_spec *spec = codec->spec;
2437 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002438 unsigned int ok_bits;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002439 int i, n, nums;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002440
2441 again:
2442 nums = 0;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002443 ok_bits = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002444 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002445 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002446 if (!spec->input_paths[i][n])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002447 break;
2448 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002449 if (i >= imux->num_items) {
2450 ok_bits |= (1 << n);
2451 nums++;
2452 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002453 }
2454
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002455 if (!ok_bits) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002456 if (spec->shared_mic_hp) {
2457 spec->shared_mic_hp = 0;
2458 imux->num_items = 1;
2459 goto again;
2460 }
2461
2462 /* check whether ADC-switch is possible */
2463 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002464 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002465 if (spec->input_paths[i][n]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002466 spec->dyn_adc_idx[i] = n;
2467 break;
2468 }
2469 }
2470 }
2471
2472 snd_printdd("hda-codec: enabling ADC switching\n");
2473 spec->dyn_adc_switch = 1;
2474 } else if (nums != spec->num_adc_nids) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002475 /* shrink the invalid adcs and input paths */
2476 nums = 0;
2477 for (n = 0; n < spec->num_adc_nids; n++) {
2478 if (!(ok_bits & (1 << n)))
2479 continue;
2480 if (n != nums) {
2481 spec->adc_nids[nums] = spec->adc_nids[n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002482 for (i = 0; i < imux->num_items; i++) {
2483 invalidate_nid_path(codec,
2484 spec->input_paths[i][nums]);
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002485 spec->input_paths[i][nums] =
2486 spec->input_paths[i][n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002487 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002488 }
2489 nums++;
2490 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002491 spec->num_adc_nids = nums;
2492 }
2493
2494 if (imux->num_items == 1 || spec->shared_mic_hp) {
2495 snd_printdd("hda-codec: reducing to a single ADC\n");
2496 spec->num_adc_nids = 1; /* reduce to a single ADC */
2497 }
2498
2499 /* single index for individual volumes ctls */
2500 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
2501 spec->num_adc_nids = 1;
2502
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 return 0;
2504}
2505
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002506/* parse capture source paths from the given pin and create imux items */
2507static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
Takashi Iwai9dba2052013-01-18 10:01:15 +01002508 int cfg_idx, int num_adcs,
2509 const char *label, int anchor)
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002510{
2511 struct hda_gen_spec *spec = codec->spec;
2512 struct hda_input_mux *imux = &spec->input_mux;
2513 int imux_idx = imux->num_items;
2514 bool imux_added = false;
2515 int c;
2516
2517 for (c = 0; c < num_adcs; c++) {
2518 struct nid_path *path;
2519 hda_nid_t adc = spec->adc_nids[c];
2520
2521 if (!is_reachable_path(codec, pin, adc))
2522 continue;
2523 path = snd_hda_add_new_path(codec, pin, adc, anchor);
2524 if (!path)
2525 continue;
2526 print_nid_path("input", path);
2527 spec->input_paths[imux_idx][c] =
2528 snd_hda_get_path_idx(codec, path);
2529
2530 if (!imux_added) {
2531 spec->imux_pins[imux->num_items] = pin;
Takashi Iwai9dba2052013-01-18 10:01:15 +01002532 snd_hda_add_imux_item(imux, label, cfg_idx, NULL);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002533 imux_added = true;
2534 }
2535 }
2536
2537 return 0;
2538}
2539
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002541 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 */
Takashi Iwai9dba2052013-01-18 10:01:15 +01002543
Takashi Iwaic9700422013-01-18 10:17:30 +01002544/* fill the label for each input at first */
2545static int fill_input_pin_labels(struct hda_codec *codec)
2546{
2547 struct hda_gen_spec *spec = codec->spec;
2548 const struct auto_pin_cfg *cfg = &spec->autocfg;
2549 int i;
2550
2551 for (i = 0; i < cfg->num_inputs; i++) {
2552 hda_nid_t pin = cfg->inputs[i].pin;
2553 const char *label;
2554 int j, idx;
2555
2556 if (!is_input_pin(codec, pin))
2557 continue;
2558
2559 label = hda_get_autocfg_input_label(codec, cfg, i);
2560 idx = 0;
David Henningsson8e8db7f2013-01-18 15:43:02 +01002561 for (j = i - 1; j >= 0; j--) {
Takashi Iwaic9700422013-01-18 10:17:30 +01002562 if (spec->input_labels[j] &&
2563 !strcmp(spec->input_labels[j], label)) {
2564 idx = spec->input_label_idxs[j] + 1;
2565 break;
2566 }
2567 }
2568
2569 spec->input_labels[i] = label;
2570 spec->input_label_idxs[i] = idx;
2571 }
2572
2573 return 0;
2574}
2575
Takashi Iwai9dba2052013-01-18 10:01:15 +01002576#define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
2577
Takashi Iwai352f7f92012-12-19 12:52:06 +01002578static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002579{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002580 struct hda_gen_spec *spec = codec->spec;
2581 const struct auto_pin_cfg *cfg = &spec->autocfg;
2582 hda_nid_t mixer = spec->mixer_nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002583 int num_adcs;
Takashi Iwaic9700422013-01-18 10:17:30 +01002584 int i, err;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002585 unsigned int val;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002586
Takashi Iwai352f7f92012-12-19 12:52:06 +01002587 num_adcs = fill_adc_nids(codec);
2588 if (num_adcs < 0)
2589 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002590
Takashi Iwaic9700422013-01-18 10:17:30 +01002591 err = fill_input_pin_labels(codec);
2592 if (err < 0)
2593 return err;
2594
Takashi Iwai352f7f92012-12-19 12:52:06 +01002595 for (i = 0; i < cfg->num_inputs; i++) {
2596 hda_nid_t pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597
Takashi Iwai352f7f92012-12-19 12:52:06 +01002598 pin = cfg->inputs[i].pin;
2599 if (!is_input_pin(codec, pin))
2600 continue;
2601
Takashi Iwai2c12c302013-01-10 09:33:29 +01002602 val = PIN_IN;
2603 if (cfg->inputs[i].type == AUTO_PIN_MIC)
2604 val |= snd_hda_get_default_vref(codec, pin);
2605 set_pin_target(codec, pin, val, false);
2606
Takashi Iwai352f7f92012-12-19 12:52:06 +01002607 if (mixer) {
2608 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01002609 err = new_analog_input(codec, i, pin,
Takashi Iwaic9700422013-01-18 10:17:30 +01002610 spec->input_labels[i],
2611 spec->input_label_idxs[i],
2612 mixer);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002613 if (err < 0)
2614 return err;
2615 }
2616 }
2617
Takashi Iwaic9700422013-01-18 10:17:30 +01002618 err = parse_capture_source(codec, pin, i, num_adcs,
2619 spec->input_labels[i], -mixer);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002620 if (err < 0)
2621 return err;
Takashi Iwai294765582013-01-17 09:52:11 +01002622
2623 if (spec->add_in_jack_modes) {
2624 err = create_in_jack_mode(codec, pin);
2625 if (err < 0)
2626 return err;
2627 }
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002628 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002629
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002630 if (mixer && spec->add_stereo_mix_input) {
Takashi Iwai9dba2052013-01-18 10:01:15 +01002631 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002632 "Stereo Mix", 0);
2633 if (err < 0)
2634 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002635 }
2636
2637 return 0;
2638}
2639
2640
2641/*
2642 * input source mux
2643 */
2644
Takashi Iwaic697b712013-01-07 17:09:26 +01002645/* get the input path specified by the given adc and imux indices */
2646static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002647{
2648 struct hda_gen_spec *spec = codec->spec;
David Henningssonb56fa1e2013-01-16 11:45:35 +01002649 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
2650 snd_BUG();
2651 return NULL;
2652 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002653 if (spec->dyn_adc_switch)
2654 adc_idx = spec->dyn_adc_idx[imux_idx];
David Henningssond3d982f2013-01-18 15:43:01 +01002655 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
David Henningssonb56fa1e2013-01-16 11:45:35 +01002656 snd_BUG();
2657 return NULL;
2658 }
Takashi Iwaic697b712013-01-07 17:09:26 +01002659 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002660}
2661
2662static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2663 unsigned int idx);
2664
2665static int mux_enum_info(struct snd_kcontrol *kcontrol,
2666 struct snd_ctl_elem_info *uinfo)
2667{
2668 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2669 struct hda_gen_spec *spec = codec->spec;
2670 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
2671}
2672
2673static int mux_enum_get(struct snd_kcontrol *kcontrol,
2674 struct snd_ctl_elem_value *ucontrol)
2675{
2676 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2677 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai2a8d5392013-01-18 16:23:25 +01002678 /* the ctls are created at once with multiple counts */
2679 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002680
2681 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
2682 return 0;
2683}
2684
2685static int mux_enum_put(struct snd_kcontrol *kcontrol,
2686 struct snd_ctl_elem_value *ucontrol)
2687{
2688 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Takashi Iwai2a8d5392013-01-18 16:23:25 +01002689 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002690 return mux_select(codec, adc_idx,
2691 ucontrol->value.enumerated.item[0]);
2692}
2693
Takashi Iwai352f7f92012-12-19 12:52:06 +01002694static const struct snd_kcontrol_new cap_src_temp = {
2695 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2696 .name = "Input Source",
2697 .info = mux_enum_info,
2698 .get = mux_enum_get,
2699 .put = mux_enum_put,
2700};
2701
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002702/*
2703 * capture volume and capture switch ctls
2704 */
2705
Takashi Iwai352f7f92012-12-19 12:52:06 +01002706typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
2707 struct snd_ctl_elem_value *ucontrol);
2708
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002709/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002710static int cap_put_caller(struct snd_kcontrol *kcontrol,
2711 struct snd_ctl_elem_value *ucontrol,
2712 put_call_t func, int type)
2713{
2714 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2715 struct hda_gen_spec *spec = codec->spec;
2716 const struct hda_input_mux *imux;
2717 struct nid_path *path;
2718 int i, adc_idx, err = 0;
2719
2720 imux = &spec->input_mux;
David Henningssona053d1e2013-01-16 11:45:36 +01002721 adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002722 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002723 /* we use the cache-only update at first since multiple input paths
2724 * may shared the same amp; by updating only caches, the redundant
2725 * writes to hardware can be reduced.
2726 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002727 codec->cached_write = 1;
2728 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01002729 path = get_input_path(codec, adc_idx, i);
2730 if (!path || !path->ctls[type])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002731 continue;
2732 kcontrol->private_value = path->ctls[type];
2733 err = func(kcontrol, ucontrol);
2734 if (err < 0)
2735 goto error;
2736 }
2737 error:
2738 codec->cached_write = 0;
2739 mutex_unlock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002740 snd_hda_codec_flush_amp_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002741 if (err >= 0 && spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01002742 spec->cap_sync_hook(codec, ucontrol);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002743 return err;
2744}
2745
2746/* capture volume ctl callbacks */
2747#define cap_vol_info snd_hda_mixer_amp_volume_info
2748#define cap_vol_get snd_hda_mixer_amp_volume_get
2749#define cap_vol_tlv snd_hda_mixer_amp_tlv
2750
2751static int cap_vol_put(struct snd_kcontrol *kcontrol,
2752 struct snd_ctl_elem_value *ucontrol)
2753{
2754 return cap_put_caller(kcontrol, ucontrol,
2755 snd_hda_mixer_amp_volume_put,
2756 NID_PATH_VOL_CTL);
2757}
2758
2759static const struct snd_kcontrol_new cap_vol_temp = {
2760 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2761 .name = "Capture Volume",
2762 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
2763 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2764 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
2765 .info = cap_vol_info,
2766 .get = cap_vol_get,
2767 .put = cap_vol_put,
2768 .tlv = { .c = cap_vol_tlv },
2769};
2770
2771/* capture switch ctl callbacks */
2772#define cap_sw_info snd_ctl_boolean_stereo_info
2773#define cap_sw_get snd_hda_mixer_amp_switch_get
2774
2775static int cap_sw_put(struct snd_kcontrol *kcontrol,
2776 struct snd_ctl_elem_value *ucontrol)
2777{
Takashi Iwaia90229e2013-01-18 14:10:00 +01002778 return cap_put_caller(kcontrol, ucontrol,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002779 snd_hda_mixer_amp_switch_put,
2780 NID_PATH_MUTE_CTL);
2781}
2782
2783static const struct snd_kcontrol_new cap_sw_temp = {
2784 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2785 .name = "Capture Switch",
2786 .info = cap_sw_info,
2787 .get = cap_sw_get,
2788 .put = cap_sw_put,
2789};
2790
2791static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
2792{
2793 hda_nid_t nid;
2794 int i, depth;
2795
2796 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
2797 for (depth = 0; depth < 3; depth++) {
2798 if (depth >= path->depth)
2799 return -EINVAL;
2800 i = path->depth - depth - 1;
2801 nid = path->path[i];
2802 if (!path->ctls[NID_PATH_VOL_CTL]) {
2803 if (nid_has_volume(codec, nid, HDA_OUTPUT))
2804 path->ctls[NID_PATH_VOL_CTL] =
2805 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2806 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
2807 int idx = path->idx[i];
2808 if (!depth && codec->single_adc_amp)
2809 idx = 0;
2810 path->ctls[NID_PATH_VOL_CTL] =
2811 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2812 }
2813 }
2814 if (!path->ctls[NID_PATH_MUTE_CTL]) {
2815 if (nid_has_mute(codec, nid, HDA_OUTPUT))
2816 path->ctls[NID_PATH_MUTE_CTL] =
2817 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2818 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
2819 int idx = path->idx[i];
2820 if (!depth && codec->single_adc_amp)
2821 idx = 0;
2822 path->ctls[NID_PATH_MUTE_CTL] =
2823 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2824 }
2825 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01002826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 return 0;
2828}
2829
Takashi Iwai352f7f92012-12-19 12:52:06 +01002830static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002832 struct hda_gen_spec *spec = codec->spec;
2833 struct auto_pin_cfg *cfg = &spec->autocfg;
2834 unsigned int val;
2835 int i;
2836
2837 if (!spec->inv_dmic_split)
2838 return false;
2839 for (i = 0; i < cfg->num_inputs; i++) {
2840 if (cfg->inputs[i].pin != nid)
2841 continue;
2842 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2843 return false;
2844 val = snd_hda_codec_get_pincfg(codec, nid);
2845 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
2846 }
2847 return false;
2848}
2849
Takashi Iwaia90229e2013-01-18 14:10:00 +01002850/* capture switch put callback for a single control with hook call */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01002851static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
2852 struct snd_ctl_elem_value *ucontrol)
2853{
2854 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2855 struct hda_gen_spec *spec = codec->spec;
2856 int ret;
2857
2858 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2859 if (ret < 0)
2860 return ret;
2861
Takashi Iwaia90229e2013-01-18 14:10:00 +01002862 if (spec->cap_sync_hook)
2863 spec->cap_sync_hook(codec, ucontrol);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01002864
2865 return ret;
2866}
2867
Takashi Iwai352f7f92012-12-19 12:52:06 +01002868static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
2869 int idx, bool is_switch, unsigned int ctl,
2870 bool inv_dmic)
2871{
2872 struct hda_gen_spec *spec = codec->spec;
2873 char tmpname[44];
2874 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
2875 const char *sfx = is_switch ? "Switch" : "Volume";
2876 unsigned int chs = inv_dmic ? 1 : 3;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01002877 struct snd_kcontrol_new *knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002878
2879 if (!ctl)
2880 return 0;
2881
2882 if (label)
2883 snprintf(tmpname, sizeof(tmpname),
2884 "%s Capture %s", label, sfx);
2885 else
2886 snprintf(tmpname, sizeof(tmpname),
2887 "Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01002888 knew = add_control(spec, type, tmpname, idx,
2889 amp_val_replace_channels(ctl, chs));
2890 if (!knew)
2891 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01002892 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01002893 knew->put = cap_single_sw_put;
2894 if (!inv_dmic)
2895 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002896
2897 /* Make independent right kcontrol */
2898 if (label)
2899 snprintf(tmpname, sizeof(tmpname),
2900 "Inverted %s Capture %s", label, sfx);
2901 else
2902 snprintf(tmpname, sizeof(tmpname),
2903 "Inverted Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01002904 knew = add_control(spec, type, tmpname, idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002905 amp_val_replace_channels(ctl, 2));
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01002906 if (!knew)
2907 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01002908 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01002909 knew->put = cap_single_sw_put;
2910 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002911}
2912
2913/* create single (and simple) capture volume and switch controls */
2914static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
2915 unsigned int vol_ctl, unsigned int sw_ctl,
2916 bool inv_dmic)
2917{
2918 int err;
2919 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
2920 if (err < 0)
2921 return err;
2922 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
2923 if (err < 0)
2924 return err;
2925 return 0;
2926}
2927
2928/* create bound capture volume and switch controls */
2929static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
2930 unsigned int vol_ctl, unsigned int sw_ctl)
2931{
2932 struct hda_gen_spec *spec = codec->spec;
2933 struct snd_kcontrol_new *knew;
2934
2935 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002936 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002937 if (!knew)
2938 return -ENOMEM;
2939 knew->index = idx;
2940 knew->private_value = vol_ctl;
2941 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2942 }
2943 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002944 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002945 if (!knew)
2946 return -ENOMEM;
2947 knew->index = idx;
2948 knew->private_value = sw_ctl;
2949 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2950 }
2951 return 0;
2952}
2953
2954/* return the vol ctl when used first in the imux list */
2955static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
2956{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002957 struct nid_path *path;
2958 unsigned int ctl;
2959 int i;
2960
Takashi Iwaic697b712013-01-07 17:09:26 +01002961 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002962 if (!path)
2963 return 0;
2964 ctl = path->ctls[type];
2965 if (!ctl)
2966 return 0;
2967 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01002968 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002969 if (path && path->ctls[type] == ctl)
2970 return 0;
2971 }
2972 return ctl;
2973}
2974
2975/* create individual capture volume and switch controls per input */
2976static int create_multi_cap_vol_ctl(struct hda_codec *codec)
2977{
2978 struct hda_gen_spec *spec = codec->spec;
2979 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaic9700422013-01-18 10:17:30 +01002980 int i, err, type;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002981
2982 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002983 bool inv_dmic;
Takashi Iwaic9700422013-01-18 10:17:30 +01002984 int idx;
Takashi Iwai9dba2052013-01-18 10:01:15 +01002985
Takashi Iwaic9700422013-01-18 10:17:30 +01002986 idx = imux->items[i].index;
2987 if (idx >= spec->autocfg.num_inputs)
Takashi Iwai9dba2052013-01-18 10:01:15 +01002988 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002989 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
2990
2991 for (type = 0; type < 2; type++) {
Takashi Iwaic9700422013-01-18 10:17:30 +01002992 err = add_single_cap_ctl(codec,
2993 spec->input_labels[idx],
2994 spec->input_label_idxs[idx],
2995 type,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002996 get_first_cap_ctl(codec, i, type),
2997 inv_dmic);
2998 if (err < 0)
2999 return err;
3000 }
3001 }
3002 return 0;
3003}
3004
3005static int create_capture_mixers(struct hda_codec *codec)
3006{
3007 struct hda_gen_spec *spec = codec->spec;
3008 struct hda_input_mux *imux = &spec->input_mux;
3009 int i, n, nums, err;
3010
3011 if (spec->dyn_adc_switch)
3012 nums = 1;
3013 else
3014 nums = spec->num_adc_nids;
3015
3016 if (!spec->auto_mic && imux->num_items > 1) {
3017 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01003018 const char *name;
3019 name = nums > 1 ? "Input Source" : "Capture Source";
3020 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003021 if (!knew)
3022 return -ENOMEM;
3023 knew->count = nums;
3024 }
3025
3026 for (n = 0; n < nums; n++) {
3027 bool multi = false;
David Henningsson99a55922013-01-16 15:58:44 +01003028 bool multi_cap_vol = spec->multi_cap_vol;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003029 bool inv_dmic = false;
3030 int vol, sw;
3031
3032 vol = sw = 0;
3033 for (i = 0; i < imux->num_items; i++) {
3034 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01003035 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003036 if (!path)
3037 continue;
3038 parse_capvol_in_path(codec, path);
3039 if (!vol)
3040 vol = path->ctls[NID_PATH_VOL_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003041 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003042 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003043 if (!same_amp_caps(codec, vol,
3044 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3045 multi_cap_vol = true;
3046 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003047 if (!sw)
3048 sw = path->ctls[NID_PATH_MUTE_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003049 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003050 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003051 if (!same_amp_caps(codec, sw,
3052 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3053 multi_cap_vol = true;
3054 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003055 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3056 inv_dmic = true;
3057 }
3058
3059 if (!multi)
3060 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3061 inv_dmic);
David Henningsson99a55922013-01-16 15:58:44 +01003062 else if (!multi_cap_vol)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003063 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3064 else
3065 err = create_multi_cap_vol_ctl(codec);
3066 if (err < 0)
3067 return err;
3068 }
3069
3070 return 0;
3071}
3072
3073/*
3074 * add mic boosts if needed
3075 */
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003076
3077/* check whether the given amp is feasible as a boost volume */
3078static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3079 int dir, int idx)
3080{
3081 unsigned int step;
3082
3083 if (!nid_has_volume(codec, nid, dir) ||
3084 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3085 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3086 return false;
3087
3088 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3089 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3090 if (step < 0x20)
3091 return false;
3092 return true;
3093}
3094
3095/* look for a boost amp in a widget close to the pin */
3096static unsigned int look_for_boost_amp(struct hda_codec *codec,
3097 struct nid_path *path)
3098{
3099 unsigned int val = 0;
3100 hda_nid_t nid;
3101 int depth;
3102
3103 for (depth = 0; depth < 3; depth++) {
3104 if (depth >= path->depth - 1)
3105 break;
3106 nid = path->path[depth];
3107 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3108 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3109 break;
3110 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3111 path->idx[depth])) {
3112 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3113 HDA_INPUT);
3114 break;
3115 }
3116 }
3117
3118 return val;
3119}
3120
Takashi Iwai352f7f92012-12-19 12:52:06 +01003121static int parse_mic_boost(struct hda_codec *codec)
3122{
3123 struct hda_gen_spec *spec = codec->spec;
3124 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003125 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003126 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003127
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003128 if (!spec->num_adc_nids)
3129 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003130
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003131 for (i = 0; i < imux->num_items; i++) {
3132 struct nid_path *path;
3133 unsigned int val;
3134 int idx;
3135 char boost_label[44];
David Henningsson02aba552013-01-16 15:58:43 +01003136
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003137 idx = imux->items[i].index;
3138 if (idx >= imux->num_items)
3139 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003140
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003141 /* check only line-in and mic pins */
Takashi Iwai1799cdd52013-01-18 14:37:16 +01003142 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003143 continue;
3144
3145 path = get_input_path(codec, 0, i);
3146 if (!path)
3147 continue;
3148
3149 val = look_for_boost_amp(codec, path);
3150 if (!val)
3151 continue;
3152
3153 /* create a boost control */
3154 snprintf(boost_label, sizeof(boost_label),
3155 "%s Boost Volume", spec->input_labels[idx]);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003156 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3157 spec->input_label_idxs[idx], val))
3158 return -ENOMEM;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003159
3160 path->ctls[NID_PATH_BOOST_CTL] = val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003161 }
3162 return 0;
3163}
3164
3165/*
3166 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3167 */
3168static void parse_digital(struct hda_codec *codec)
3169{
3170 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003171 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003172 int i, nums;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003173 hda_nid_t dig_nid, pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003174
3175 /* support multiple SPDIFs; the secondary is set up as a slave */
3176 nums = 0;
3177 for (i = 0; i < spec->autocfg.dig_outs; i++) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003178 pin = spec->autocfg.dig_out_pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003179 dig_nid = look_for_dac(codec, pin, true);
3180 if (!dig_nid)
3181 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003182 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003183 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003184 continue;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003185 print_nid_path("digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01003186 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01003187 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003188 set_pin_target(codec, pin, PIN_OUT, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003189 if (!nums) {
3190 spec->multiout.dig_out_nid = dig_nid;
3191 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3192 } else {
3193 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3194 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
3195 break;
3196 spec->slave_dig_outs[nums - 1] = dig_nid;
3197 }
3198 nums++;
3199 }
3200
3201 if (spec->autocfg.dig_in_pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003202 pin = spec->autocfg.dig_in_pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003203 dig_nid = codec->start_nid;
3204 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003205 unsigned int wcaps = get_wcaps(codec, dig_nid);
3206 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3207 continue;
3208 if (!(wcaps & AC_WCAP_DIGITAL))
3209 continue;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003210 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003211 if (path) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003212 print_nid_path("digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003213 path->active = true;
3214 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003215 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003216 set_pin_target(codec, pin, PIN_IN, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003217 break;
3218 }
3219 }
3220 }
3221}
3222
3223
3224/*
3225 * input MUX handling
3226 */
3227
3228static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3229
3230/* select the given imux item; either unmute exclusively or select the route */
3231static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3232 unsigned int idx)
3233{
3234 struct hda_gen_spec *spec = codec->spec;
3235 const struct hda_input_mux *imux;
3236 struct nid_path *path;
3237
3238 imux = &spec->input_mux;
3239 if (!imux->num_items)
3240 return 0;
3241
3242 if (idx >= imux->num_items)
3243 idx = imux->num_items - 1;
3244 if (spec->cur_mux[adc_idx] == idx)
3245 return 0;
3246
Takashi Iwaic697b712013-01-07 17:09:26 +01003247 path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003248 if (!path)
3249 return 0;
3250 if (path->active)
3251 snd_hda_activate_path(codec, path, false, false);
3252
3253 spec->cur_mux[adc_idx] = idx;
3254
3255 if (spec->shared_mic_hp)
3256 update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
3257
3258 if (spec->dyn_adc_switch)
3259 dyn_adc_pcm_resetup(codec, idx);
3260
Takashi Iwaic697b712013-01-07 17:09:26 +01003261 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003262 if (!path)
3263 return 0;
3264 if (path->active)
3265 return 0;
3266 snd_hda_activate_path(codec, path, true, false);
3267 if (spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01003268 spec->cap_sync_hook(codec, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003269 return 1;
3270}
3271
3272
3273/*
3274 * Jack detections for HP auto-mute and mic-switch
3275 */
3276
3277/* check each pin in the given array; returns true if any of them is plugged */
3278static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
3279{
3280 int i, present = 0;
3281
3282 for (i = 0; i < num_pins; i++) {
3283 hda_nid_t nid = pins[i];
3284 if (!nid)
3285 break;
Takashi Iwai0b4df932013-01-10 09:45:13 +01003286 /* don't detect pins retasked as inputs */
3287 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
3288 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003289 present |= snd_hda_jack_detect(codec, nid);
3290 }
3291 return present;
3292}
3293
3294/* standard HP/line-out auto-mute helper */
3295static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
Takashi Iwai2c12c302013-01-10 09:33:29 +01003296 bool mute)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003297{
3298 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003299 int i;
3300
3301 for (i = 0; i < num_pins; i++) {
3302 hda_nid_t nid = pins[i];
3303 unsigned int val;
3304 if (!nid)
3305 break;
3306 /* don't reset VREF value in case it's controlling
3307 * the amp (see alc861_fixup_asus_amp_vref_0f())
3308 */
Takashi Iwai2c12c302013-01-10 09:33:29 +01003309 if (spec->keep_vref_in_automute)
3310 val = snd_hda_codec_get_pin_target(codec, nid) & ~PIN_HP;
3311 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01003312 val = 0;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003313 if (!mute)
3314 val |= snd_hda_codec_get_pin_target(codec, nid);
3315 /* here we call update_pin_ctl() so that the pinctl is changed
3316 * without changing the pinctl target value;
3317 * the original target value will be still referred at the
3318 * init / resume again
3319 */
3320 update_pin_ctl(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01003321 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003322 }
3323}
3324
3325/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003326void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003327{
3328 struct hda_gen_spec *spec = codec->spec;
3329 int on;
3330
3331 /* Control HP pins/amps depending on master_mute state;
3332 * in general, HP pins/amps control should be enabled in all cases,
3333 * but currently set only for master_mute, just to be safe
3334 */
3335 if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
3336 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003337 spec->autocfg.hp_pins, spec->master_mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003338
3339 if (!spec->automute_speaker)
3340 on = 0;
3341 else
3342 on = spec->hp_jack_present | spec->line_jack_present;
3343 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003344 spec->speaker_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003345 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003346 spec->autocfg.speaker_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003347
3348 /* toggle line-out mutes if needed, too */
3349 /* if LO is a copy of either HP or Speaker, don't need to handle it */
3350 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
3351 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
3352 return;
3353 if (!spec->automute_lo)
3354 on = 0;
3355 else
3356 on = spec->hp_jack_present;
3357 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003358 spec->line_out_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003359 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003360 spec->autocfg.line_out_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003361}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003362EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003363
3364static void call_update_outputs(struct hda_codec *codec)
3365{
3366 struct hda_gen_spec *spec = codec->spec;
3367 if (spec->automute_hook)
3368 spec->automute_hook(codec);
3369 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01003370 snd_hda_gen_update_outputs(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003371}
3372
3373/* standard HP-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003374void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003375{
3376 struct hda_gen_spec *spec = codec->spec;
3377
3378 spec->hp_jack_present =
3379 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
3380 spec->autocfg.hp_pins);
3381 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
3382 return;
3383 call_update_outputs(codec);
3384}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003385EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003386
3387/* standard line-out-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003388void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003389{
3390 struct hda_gen_spec *spec = codec->spec;
3391
3392 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3393 return;
3394 /* check LO jack only when it's different from HP */
3395 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
3396 return;
3397
3398 spec->line_jack_present =
3399 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3400 spec->autocfg.line_out_pins);
3401 if (!spec->automute_speaker || !spec->detect_lo)
3402 return;
3403 call_update_outputs(codec);
3404}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003405EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003406
3407/* standard mic auto-switch helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003408void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003409{
3410 struct hda_gen_spec *spec = codec->spec;
3411 int i;
3412
3413 if (!spec->auto_mic)
3414 return;
3415
3416 for (i = spec->am_num_entries - 1; i > 0; i--) {
Takashi Iwai0b4df932013-01-10 09:45:13 +01003417 hda_nid_t pin = spec->am_entry[i].pin;
3418 /* don't detect pins retasked as outputs */
3419 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
3420 continue;
3421 if (snd_hda_jack_detect(codec, pin)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003422 mux_select(codec, 0, spec->am_entry[i].idx);
3423 return;
3424 }
3425 }
3426 mux_select(codec, 0, spec->am_entry[0].idx);
3427}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003428EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003429
Takashi Iwaia5cc2502013-01-16 18:08:55 +01003430/* update jack retasking */
3431static void update_automute_all(struct hda_codec *codec)
3432{
3433 struct hda_gen_spec *spec = codec->spec;
3434
3435 if (spec->hp_automute_hook)
3436 spec->hp_automute_hook(codec, NULL);
3437 else
3438 snd_hda_gen_hp_automute(codec, NULL);
3439 if (spec->line_automute_hook)
3440 spec->line_automute_hook(codec, NULL);
3441 else
3442 snd_hda_gen_line_automute(codec, NULL);
3443 if (spec->mic_autoswitch_hook)
3444 spec->mic_autoswitch_hook(codec, NULL);
3445 else
3446 snd_hda_gen_mic_autoswitch(codec, NULL);
3447}
3448
Takashi Iwai352f7f92012-12-19 12:52:06 +01003449/*
3450 * Auto-Mute mode mixer enum support
3451 */
3452static int automute_mode_info(struct snd_kcontrol *kcontrol,
3453 struct snd_ctl_elem_info *uinfo)
3454{
3455 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3456 struct hda_gen_spec *spec = codec->spec;
3457 static const char * const texts3[] = {
3458 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02003459 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460
Takashi Iwai352f7f92012-12-19 12:52:06 +01003461 if (spec->automute_speaker_possible && spec->automute_lo_possible)
3462 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
3463 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
3464}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465
Takashi Iwai352f7f92012-12-19 12:52:06 +01003466static int automute_mode_get(struct snd_kcontrol *kcontrol,
3467 struct snd_ctl_elem_value *ucontrol)
3468{
3469 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3470 struct hda_gen_spec *spec = codec->spec;
3471 unsigned int val = 0;
3472 if (spec->automute_speaker)
3473 val++;
3474 if (spec->automute_lo)
3475 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003476
Takashi Iwai352f7f92012-12-19 12:52:06 +01003477 ucontrol->value.enumerated.item[0] = val;
3478 return 0;
3479}
3480
3481static int automute_mode_put(struct snd_kcontrol *kcontrol,
3482 struct snd_ctl_elem_value *ucontrol)
3483{
3484 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3485 struct hda_gen_spec *spec = codec->spec;
3486
3487 switch (ucontrol->value.enumerated.item[0]) {
3488 case 0:
3489 if (!spec->automute_speaker && !spec->automute_lo)
3490 return 0;
3491 spec->automute_speaker = 0;
3492 spec->automute_lo = 0;
3493 break;
3494 case 1:
3495 if (spec->automute_speaker_possible) {
3496 if (!spec->automute_lo && spec->automute_speaker)
3497 return 0;
3498 spec->automute_speaker = 1;
3499 spec->automute_lo = 0;
3500 } else if (spec->automute_lo_possible) {
3501 if (spec->automute_lo)
3502 return 0;
3503 spec->automute_lo = 1;
3504 } else
3505 return -EINVAL;
3506 break;
3507 case 2:
3508 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
3509 return -EINVAL;
3510 if (spec->automute_speaker && spec->automute_lo)
3511 return 0;
3512 spec->automute_speaker = 1;
3513 spec->automute_lo = 1;
3514 break;
3515 default:
3516 return -EINVAL;
3517 }
3518 call_update_outputs(codec);
3519 return 1;
3520}
3521
3522static const struct snd_kcontrol_new automute_mode_enum = {
3523 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3524 .name = "Auto-Mute Mode",
3525 .info = automute_mode_info,
3526 .get = automute_mode_get,
3527 .put = automute_mode_put,
3528};
3529
3530static int add_automute_mode_enum(struct hda_codec *codec)
3531{
3532 struct hda_gen_spec *spec = codec->spec;
3533
Takashi Iwai12c93df2012-12-19 14:38:33 +01003534 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01003535 return -ENOMEM;
3536 return 0;
3537}
3538
3539/*
3540 * Check the availability of HP/line-out auto-mute;
3541 * Set up appropriately if really supported
3542 */
3543static int check_auto_mute_availability(struct hda_codec *codec)
3544{
3545 struct hda_gen_spec *spec = codec->spec;
3546 struct auto_pin_cfg *cfg = &spec->autocfg;
3547 int present = 0;
3548 int i, err;
3549
Takashi Iwaif72706b2013-01-16 18:20:07 +01003550 if (spec->suppress_auto_mute)
3551 return 0;
3552
Takashi Iwai352f7f92012-12-19 12:52:06 +01003553 if (cfg->hp_pins[0])
3554 present++;
3555 if (cfg->line_out_pins[0])
3556 present++;
3557 if (cfg->speaker_pins[0])
3558 present++;
3559 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02003560 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003561
3562 if (!cfg->speaker_pins[0] &&
3563 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
3564 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3565 sizeof(cfg->speaker_pins));
3566 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568
Takashi Iwai352f7f92012-12-19 12:52:06 +01003569 if (!cfg->hp_pins[0] &&
3570 cfg->line_out_type == AUTO_PIN_HP_OUT) {
3571 memcpy(cfg->hp_pins, cfg->line_out_pins,
3572 sizeof(cfg->hp_pins));
3573 cfg->hp_outs = cfg->line_outs;
3574 }
3575
3576 for (i = 0; i < cfg->hp_outs; i++) {
3577 hda_nid_t nid = cfg->hp_pins[i];
3578 if (!is_jack_detectable(codec, nid))
3579 continue;
3580 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
3581 nid);
3582 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003583 spec->hp_automute_hook ?
3584 spec->hp_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003585 snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003586 spec->detect_hp = 1;
3587 }
3588
3589 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
3590 if (cfg->speaker_outs)
3591 for (i = 0; i < cfg->line_outs; i++) {
3592 hda_nid_t nid = cfg->line_out_pins[i];
3593 if (!is_jack_detectable(codec, nid))
3594 continue;
3595 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
3596 snd_hda_jack_detect_enable_callback(codec, nid,
3597 HDA_GEN_FRONT_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003598 spec->line_automute_hook ?
3599 spec->line_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003600 snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003601 spec->detect_lo = 1;
3602 }
3603 spec->automute_lo_possible = spec->detect_hp;
3604 }
3605
3606 spec->automute_speaker_possible = cfg->speaker_outs &&
3607 (spec->detect_hp || spec->detect_lo);
3608
3609 spec->automute_lo = spec->automute_lo_possible;
3610 spec->automute_speaker = spec->automute_speaker_possible;
3611
3612 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
3613 /* create a control for automute mode */
3614 err = add_automute_mode_enum(codec);
3615 if (err < 0)
3616 return err;
3617 }
3618 return 0;
3619}
3620
Takashi Iwai352f7f92012-12-19 12:52:06 +01003621/* check whether all auto-mic pins are valid; setup indices if OK */
3622static bool auto_mic_check_imux(struct hda_codec *codec)
3623{
3624 struct hda_gen_spec *spec = codec->spec;
3625 const struct hda_input_mux *imux;
3626 int i;
3627
3628 imux = &spec->input_mux;
3629 for (i = 0; i < spec->am_num_entries; i++) {
3630 spec->am_entry[i].idx =
3631 find_idx_in_nid_list(spec->am_entry[i].pin,
3632 spec->imux_pins, imux->num_items);
3633 if (spec->am_entry[i].idx < 0)
3634 return false; /* no corresponding imux */
3635 }
3636
3637 /* we don't need the jack detection for the first pin */
3638 for (i = 1; i < spec->am_num_entries; i++)
3639 snd_hda_jack_detect_enable_callback(codec,
3640 spec->am_entry[i].pin,
3641 HDA_GEN_MIC_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003642 spec->mic_autoswitch_hook ?
3643 spec->mic_autoswitch_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003644 snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003645 return true;
3646}
3647
3648static int compare_attr(const void *ap, const void *bp)
3649{
3650 const struct automic_entry *a = ap;
3651 const struct automic_entry *b = bp;
3652 return (int)(a->attr - b->attr);
3653}
3654
3655/*
3656 * Check the availability of auto-mic switch;
3657 * Set up if really supported
3658 */
3659static int check_auto_mic_availability(struct hda_codec *codec)
3660{
3661 struct hda_gen_spec *spec = codec->spec;
3662 struct auto_pin_cfg *cfg = &spec->autocfg;
3663 unsigned int types;
3664 int i, num_pins;
3665
Takashi Iwaid12daf62013-01-07 16:32:11 +01003666 if (spec->suppress_auto_mic)
3667 return 0;
3668
Takashi Iwai352f7f92012-12-19 12:52:06 +01003669 types = 0;
3670 num_pins = 0;
3671 for (i = 0; i < cfg->num_inputs; i++) {
3672 hda_nid_t nid = cfg->inputs[i].pin;
3673 unsigned int attr;
3674 attr = snd_hda_codec_get_pincfg(codec, nid);
3675 attr = snd_hda_get_input_pin_attr(attr);
3676 if (types & (1 << attr))
3677 return 0; /* already occupied */
3678 switch (attr) {
3679 case INPUT_PIN_ATTR_INT:
3680 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3681 return 0; /* invalid type */
3682 break;
3683 case INPUT_PIN_ATTR_UNUSED:
3684 return 0; /* invalid entry */
3685 default:
3686 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
3687 return 0; /* invalid type */
3688 if (!spec->line_in_auto_switch &&
3689 cfg->inputs[i].type != AUTO_PIN_MIC)
3690 return 0; /* only mic is allowed */
3691 if (!is_jack_detectable(codec, nid))
3692 return 0; /* no unsol support */
3693 break;
3694 }
3695 if (num_pins >= MAX_AUTO_MIC_PINS)
3696 return 0;
3697 types |= (1 << attr);
3698 spec->am_entry[num_pins].pin = nid;
3699 spec->am_entry[num_pins].attr = attr;
3700 num_pins++;
3701 }
3702
3703 if (num_pins < 2)
3704 return 0;
3705
3706 spec->am_num_entries = num_pins;
3707 /* sort the am_entry in the order of attr so that the pin with a
3708 * higher attr will be selected when the jack is plugged.
3709 */
3710 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
3711 compare_attr, NULL);
3712
3713 if (!auto_mic_check_imux(codec))
3714 return 0;
3715
3716 spec->auto_mic = 1;
3717 spec->num_adc_nids = 1;
3718 spec->cur_mux[0] = spec->am_entry[0].idx;
3719 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
3720 spec->am_entry[0].pin,
3721 spec->am_entry[1].pin,
3722 spec->am_entry[2].pin);
3723
3724 return 0;
3725}
3726
3727
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003728/*
3729 * Parse the given BIOS configuration and set up the hda_gen_spec
3730 *
3731 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003732 * or a negative error code
3733 */
3734int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003735 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003736{
3737 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003738 int err;
3739
Takashi Iwai1c70a582013-01-11 17:48:22 +01003740 parse_user_hints(codec);
3741
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003742 if (cfg != &spec->autocfg) {
3743 spec->autocfg = *cfg;
3744 cfg = &spec->autocfg;
3745 }
3746
David Henningsson6fc4cb92013-01-16 15:58:45 +01003747 fill_all_dac_nids(codec);
3748
Takashi Iwai352f7f92012-12-19 12:52:06 +01003749 if (!cfg->line_outs) {
3750 if (cfg->dig_outs || cfg->dig_in_pin) {
3751 spec->multiout.max_channels = 2;
3752 spec->no_analog = 1;
3753 goto dig_only;
3754 }
3755 return 0; /* can't find valid BIOS pin config */
3756 }
3757
3758 if (!spec->no_primary_hp &&
3759 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
3760 cfg->line_outs <= cfg->hp_outs) {
3761 /* use HP as primary out */
3762 cfg->speaker_outs = cfg->line_outs;
3763 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3764 sizeof(cfg->speaker_pins));
3765 cfg->line_outs = cfg->hp_outs;
3766 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
3767 cfg->hp_outs = 0;
3768 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3769 cfg->line_out_type = AUTO_PIN_HP_OUT;
3770 }
3771
3772 err = parse_output_paths(codec);
3773 if (err < 0)
3774 return err;
3775 err = create_multi_channel_mode(codec);
3776 if (err < 0)
3777 return err;
3778 err = create_multi_out_ctls(codec, cfg);
3779 if (err < 0)
3780 return err;
3781 err = create_hp_out_ctls(codec);
3782 if (err < 0)
3783 return err;
3784 err = create_speaker_out_ctls(codec);
3785 if (err < 0)
3786 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003787 err = create_indep_hp_ctls(codec);
3788 if (err < 0)
3789 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01003790 err = create_loopback_mixing_ctl(codec);
3791 if (err < 0)
3792 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003793 err = create_shared_input(codec);
3794 if (err < 0)
3795 return err;
3796 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003797 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02003798 return err;
3799
Takashi Iwaia07a9492013-01-07 16:44:06 +01003800 spec->const_channel_count = spec->ext_channel_count;
3801 /* check the multiple speaker and headphone pins */
3802 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
3803 spec->const_channel_count = max(spec->const_channel_count,
3804 cfg->speaker_outs * 2);
3805 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
3806 spec->const_channel_count = max(spec->const_channel_count,
3807 cfg->hp_outs * 2);
3808 spec->multiout.max_channels = max(spec->ext_channel_count,
3809 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003810
3811 err = check_auto_mute_availability(codec);
3812 if (err < 0)
3813 return err;
3814
3815 err = check_dyn_adc_switch(codec);
3816 if (err < 0)
3817 return err;
3818
3819 if (!spec->shared_mic_hp) {
3820 err = check_auto_mic_availability(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003821 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823 }
Takashi Iwai071c73a2006-08-23 18:34:06 +02003824
Takashi Iwai352f7f92012-12-19 12:52:06 +01003825 err = create_capture_mixers(codec);
3826 if (err < 0)
3827 return err;
3828
3829 err = parse_mic_boost(codec);
3830 if (err < 0)
3831 return err;
3832
Takashi Iwai978e77e2013-01-10 16:57:58 +01003833 if (spec->add_out_jack_modes) {
3834 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
3835 err = create_out_jack_modes(codec, cfg->line_outs,
3836 cfg->line_out_pins);
3837 if (err < 0)
3838 return err;
3839 }
3840 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
3841 err = create_out_jack_modes(codec, cfg->hp_outs,
3842 cfg->hp_pins);
3843 if (err < 0)
3844 return err;
3845 }
3846 }
3847
Takashi Iwai352f7f92012-12-19 12:52:06 +01003848 dig_only:
3849 parse_digital(codec);
3850
3851 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003853EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854
3855
3856/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003857 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003859
3860/* slave controls for virtual master */
3861static const char * const slave_pfxs[] = {
3862 "Front", "Surround", "Center", "LFE", "Side",
3863 "Headphone", "Speaker", "Mono", "Line Out",
3864 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01003865 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
3866 "Headphone Front", "Headphone Surround", "Headphone CLFE",
3867 "Headphone Side",
Takashi Iwai352f7f92012-12-19 12:52:06 +01003868 NULL,
3869};
3870
3871int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003873 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875
Takashi Iwai36502d02012-12-19 15:15:10 +01003876 if (spec->kctls.used) {
3877 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
3878 if (err < 0)
3879 return err;
3880 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881
Takashi Iwai352f7f92012-12-19 12:52:06 +01003882 if (spec->multiout.dig_out_nid) {
3883 err = snd_hda_create_dig_out_ctls(codec,
3884 spec->multiout.dig_out_nid,
3885 spec->multiout.dig_out_nid,
3886 spec->pcm_rec[1].pcm_type);
3887 if (err < 0)
3888 return err;
3889 if (!spec->no_analog) {
3890 err = snd_hda_create_spdif_share_sw(codec,
3891 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 if (err < 0)
3893 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003894 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 }
3896 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003897 if (spec->dig_in_nid) {
3898 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
3899 if (err < 0)
3900 return err;
3901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902
Takashi Iwai352f7f92012-12-19 12:52:06 +01003903 /* if we have no master control, let's create it */
3904 if (!spec->no_analog &&
3905 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003906 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01003907 spec->vmaster_tlv, slave_pfxs,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003908 "Playback Volume");
3909 if (err < 0)
3910 return err;
3911 }
3912 if (!spec->no_analog &&
3913 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
3914 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
3915 NULL, slave_pfxs,
3916 "Playback Switch",
3917 true, &spec->vmaster_mute.sw_kctl);
3918 if (err < 0)
3919 return err;
3920 if (spec->vmaster_mute.hook)
Takashi Iwaifd25a972012-12-20 14:57:18 +01003921 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
3922 spec->vmaster_mute_enum);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924
Takashi Iwai352f7f92012-12-19 12:52:06 +01003925 free_kctls(spec); /* no longer needed */
3926
3927 if (spec->shared_mic_hp) {
3928 int err;
3929 int nid = spec->autocfg.inputs[1].pin;
3930 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
3931 if (err < 0)
3932 return err;
3933 err = snd_hda_jack_detect_enable(codec, nid, 0);
3934 if (err < 0)
3935 return err;
3936 }
3937
3938 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
3939 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 return err;
3941
3942 return 0;
3943}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003944EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
3945
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946
3947/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003948 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003951static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
3952 struct hda_codec *codec,
3953 struct snd_pcm_substream *substream,
3954 int action)
3955{
3956 struct hda_gen_spec *spec = codec->spec;
3957 if (spec->pcm_playback_hook)
3958 spec->pcm_playback_hook(hinfo, codec, substream, action);
3959}
3960
Takashi Iwaiac2e8732013-01-17 15:57:10 +01003961static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
3962 struct hda_codec *codec,
3963 struct snd_pcm_substream *substream,
3964 int action)
3965{
3966 struct hda_gen_spec *spec = codec->spec;
3967 if (spec->pcm_capture_hook)
3968 spec->pcm_capture_hook(hinfo, codec, substream, action);
3969}
3970
Takashi Iwai352f7f92012-12-19 12:52:06 +01003971/*
3972 * Analog playback callbacks
3973 */
3974static int playback_pcm_open(struct hda_pcm_stream *hinfo,
3975 struct hda_codec *codec,
3976 struct snd_pcm_substream *substream)
3977{
3978 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003979 int err;
3980
3981 mutex_lock(&spec->pcm_mutex);
3982 err = snd_hda_multi_out_analog_open(codec,
3983 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003984 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003985 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003986 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003987 call_pcm_playback_hook(hinfo, codec, substream,
3988 HDA_GEN_PCM_ACT_OPEN);
3989 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003990 mutex_unlock(&spec->pcm_mutex);
3991 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003992}
3993
3994static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01003995 struct hda_codec *codec,
3996 unsigned int stream_tag,
3997 unsigned int format,
3998 struct snd_pcm_substream *substream)
3999{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004000 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004001 int err;
4002
4003 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
4004 stream_tag, format, substream);
4005 if (!err)
4006 call_pcm_playback_hook(hinfo, codec, substream,
4007 HDA_GEN_PCM_ACT_PREPARE);
4008 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004009}
Takashi Iwai97ec5582006-03-21 11:29:07 +01004010
Takashi Iwai352f7f92012-12-19 12:52:06 +01004011static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4012 struct hda_codec *codec,
4013 struct snd_pcm_substream *substream)
4014{
4015 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004016 int err;
4017
4018 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
4019 if (!err)
4020 call_pcm_playback_hook(hinfo, codec, substream,
4021 HDA_GEN_PCM_ACT_CLEANUP);
4022 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004023}
4024
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004025static int playback_pcm_close(struct hda_pcm_stream *hinfo,
4026 struct hda_codec *codec,
4027 struct snd_pcm_substream *substream)
4028{
4029 struct hda_gen_spec *spec = codec->spec;
4030 mutex_lock(&spec->pcm_mutex);
4031 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004032 call_pcm_playback_hook(hinfo, codec, substream,
4033 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004034 mutex_unlock(&spec->pcm_mutex);
4035 return 0;
4036}
4037
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004038static int capture_pcm_open(struct hda_pcm_stream *hinfo,
4039 struct hda_codec *codec,
4040 struct snd_pcm_substream *substream)
4041{
4042 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
4043 return 0;
4044}
4045
4046static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4047 struct hda_codec *codec,
4048 unsigned int stream_tag,
4049 unsigned int format,
4050 struct snd_pcm_substream *substream)
4051{
4052 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4053 call_pcm_capture_hook(hinfo, codec, substream,
4054 HDA_GEN_PCM_ACT_PREPARE);
4055 return 0;
4056}
4057
4058static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4059 struct hda_codec *codec,
4060 struct snd_pcm_substream *substream)
4061{
4062 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4063 call_pcm_capture_hook(hinfo, codec, substream,
4064 HDA_GEN_PCM_ACT_CLEANUP);
4065 return 0;
4066}
4067
4068static int capture_pcm_close(struct hda_pcm_stream *hinfo,
4069 struct hda_codec *codec,
4070 struct snd_pcm_substream *substream)
4071{
4072 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
4073 return 0;
4074}
4075
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004076static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
4077 struct hda_codec *codec,
4078 struct snd_pcm_substream *substream)
4079{
4080 struct hda_gen_spec *spec = codec->spec;
4081 int err = 0;
4082
4083 mutex_lock(&spec->pcm_mutex);
4084 if (!spec->indep_hp_enabled)
4085 err = -EBUSY;
4086 else
4087 spec->active_streams |= 1 << STREAM_INDEP_HP;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004088 call_pcm_playback_hook(hinfo, codec, substream,
4089 HDA_GEN_PCM_ACT_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004090 mutex_unlock(&spec->pcm_mutex);
4091 return err;
4092}
4093
4094static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
4095 struct hda_codec *codec,
4096 struct snd_pcm_substream *substream)
4097{
4098 struct hda_gen_spec *spec = codec->spec;
4099 mutex_lock(&spec->pcm_mutex);
4100 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004101 call_pcm_playback_hook(hinfo, codec, substream,
4102 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004103 mutex_unlock(&spec->pcm_mutex);
4104 return 0;
4105}
4106
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004107static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4108 struct hda_codec *codec,
4109 unsigned int stream_tag,
4110 unsigned int format,
4111 struct snd_pcm_substream *substream)
4112{
4113 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4114 call_pcm_playback_hook(hinfo, codec, substream,
4115 HDA_GEN_PCM_ACT_PREPARE);
4116 return 0;
4117}
4118
4119static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4120 struct hda_codec *codec,
4121 struct snd_pcm_substream *substream)
4122{
4123 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4124 call_pcm_playback_hook(hinfo, codec, substream,
4125 HDA_GEN_PCM_ACT_CLEANUP);
4126 return 0;
4127}
4128
Takashi Iwai352f7f92012-12-19 12:52:06 +01004129/*
4130 * Digital out
4131 */
4132static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
4133 struct hda_codec *codec,
4134 struct snd_pcm_substream *substream)
4135{
4136 struct hda_gen_spec *spec = codec->spec;
4137 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
4138}
4139
4140static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4141 struct hda_codec *codec,
4142 unsigned int stream_tag,
4143 unsigned int format,
4144 struct snd_pcm_substream *substream)
4145{
4146 struct hda_gen_spec *spec = codec->spec;
4147 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
4148 stream_tag, format, substream);
4149}
4150
4151static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4152 struct hda_codec *codec,
4153 struct snd_pcm_substream *substream)
4154{
4155 struct hda_gen_spec *spec = codec->spec;
4156 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
4157}
4158
4159static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
4160 struct hda_codec *codec,
4161 struct snd_pcm_substream *substream)
4162{
4163 struct hda_gen_spec *spec = codec->spec;
4164 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
4165}
4166
4167/*
4168 * Analog capture
4169 */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004170#define alt_capture_pcm_open capture_pcm_open
4171#define alt_capture_pcm_close capture_pcm_close
4172
Takashi Iwai352f7f92012-12-19 12:52:06 +01004173static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4174 struct hda_codec *codec,
4175 unsigned int stream_tag,
4176 unsigned int format,
4177 struct snd_pcm_substream *substream)
4178{
4179 struct hda_gen_spec *spec = codec->spec;
4180
4181 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01004182 stream_tag, 0, format);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004183 call_pcm_capture_hook(hinfo, codec, substream,
4184 HDA_GEN_PCM_ACT_PREPARE);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004185 return 0;
4186}
4187
Takashi Iwai352f7f92012-12-19 12:52:06 +01004188static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4189 struct hda_codec *codec,
4190 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01004191{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004192 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01004193
Takashi Iwai352f7f92012-12-19 12:52:06 +01004194 snd_hda_codec_cleanup_stream(codec,
4195 spec->adc_nids[substream->number + 1]);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004196 call_pcm_capture_hook(hinfo, codec, substream,
4197 HDA_GEN_PCM_ACT_CLEANUP);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004198 return 0;
4199}
4200
Takashi Iwai352f7f92012-12-19 12:52:06 +01004201/*
4202 */
4203static const struct hda_pcm_stream pcm_analog_playback = {
4204 .substreams = 1,
4205 .channels_min = 2,
4206 .channels_max = 8,
4207 /* NID is set in build_pcms */
4208 .ops = {
4209 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004210 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004211 .prepare = playback_pcm_prepare,
4212 .cleanup = playback_pcm_cleanup
4213 },
4214};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215
Takashi Iwai352f7f92012-12-19 12:52:06 +01004216static const struct hda_pcm_stream pcm_analog_capture = {
4217 .substreams = 1,
4218 .channels_min = 2,
4219 .channels_max = 2,
4220 /* NID is set in build_pcms */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004221 .ops = {
4222 .open = capture_pcm_open,
4223 .close = capture_pcm_close,
4224 .prepare = capture_pcm_prepare,
4225 .cleanup = capture_pcm_cleanup
4226 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004227};
4228
4229static const struct hda_pcm_stream pcm_analog_alt_playback = {
4230 .substreams = 1,
4231 .channels_min = 2,
4232 .channels_max = 2,
4233 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004234 .ops = {
4235 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004236 .close = alt_playback_pcm_close,
4237 .prepare = alt_playback_pcm_prepare,
4238 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004239 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004240};
4241
4242static const struct hda_pcm_stream pcm_analog_alt_capture = {
4243 .substreams = 2, /* can be overridden */
4244 .channels_min = 2,
4245 .channels_max = 2,
4246 /* NID is set in build_pcms */
4247 .ops = {
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004248 .open = alt_capture_pcm_open,
4249 .close = alt_capture_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004250 .prepare = alt_capture_pcm_prepare,
4251 .cleanup = alt_capture_pcm_cleanup
4252 },
4253};
4254
4255static const struct hda_pcm_stream pcm_digital_playback = {
4256 .substreams = 1,
4257 .channels_min = 2,
4258 .channels_max = 2,
4259 /* NID is set in build_pcms */
4260 .ops = {
4261 .open = dig_playback_pcm_open,
4262 .close = dig_playback_pcm_close,
4263 .prepare = dig_playback_pcm_prepare,
4264 .cleanup = dig_playback_pcm_cleanup
4265 },
4266};
4267
4268static const struct hda_pcm_stream pcm_digital_capture = {
4269 .substreams = 1,
4270 .channels_min = 2,
4271 .channels_max = 2,
4272 /* NID is set in build_pcms */
4273};
4274
4275/* Used by build_pcms to flag that a PCM has no playback stream */
4276static const struct hda_pcm_stream pcm_null_stream = {
4277 .substreams = 0,
4278 .channels_min = 0,
4279 .channels_max = 0,
4280};
4281
4282/*
4283 * dynamic changing ADC PCM streams
4284 */
4285static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
4286{
4287 struct hda_gen_spec *spec = codec->spec;
4288 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
4289
4290 if (spec->cur_adc && spec->cur_adc != new_adc) {
4291 /* stream is running, let's swap the current ADC */
4292 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
4293 spec->cur_adc = new_adc;
4294 snd_hda_codec_setup_stream(codec, new_adc,
4295 spec->cur_adc_stream_tag, 0,
4296 spec->cur_adc_format);
4297 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004299 return false;
4300}
4301
4302/* analog capture with dynamic dual-adc changes */
4303static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4304 struct hda_codec *codec,
4305 unsigned int stream_tag,
4306 unsigned int format,
4307 struct snd_pcm_substream *substream)
4308{
4309 struct hda_gen_spec *spec = codec->spec;
4310 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
4311 spec->cur_adc_stream_tag = stream_tag;
4312 spec->cur_adc_format = format;
4313 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
4314 return 0;
4315}
4316
4317static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4318 struct hda_codec *codec,
4319 struct snd_pcm_substream *substream)
4320{
4321 struct hda_gen_spec *spec = codec->spec;
4322 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
4323 spec->cur_adc = 0;
4324 return 0;
4325}
4326
4327static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
4328 .substreams = 1,
4329 .channels_min = 2,
4330 .channels_max = 2,
4331 .nid = 0, /* fill later */
4332 .ops = {
4333 .prepare = dyn_adc_capture_pcm_prepare,
4334 .cleanup = dyn_adc_capture_pcm_cleanup
4335 },
4336};
4337
Takashi Iwaif873e532012-12-20 16:58:39 +01004338static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
4339 const char *chip_name)
4340{
4341 char *p;
4342
4343 if (*str)
4344 return;
4345 strlcpy(str, chip_name, len);
4346
4347 /* drop non-alnum chars after a space */
4348 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
4349 if (!isalnum(p[1])) {
4350 *p = 0;
4351 break;
4352 }
4353 }
4354 strlcat(str, sfx, len);
4355}
4356
Takashi Iwai352f7f92012-12-19 12:52:06 +01004357/* build PCM streams based on the parsed results */
4358int snd_hda_gen_build_pcms(struct hda_codec *codec)
4359{
4360 struct hda_gen_spec *spec = codec->spec;
4361 struct hda_pcm *info = spec->pcm_rec;
4362 const struct hda_pcm_stream *p;
4363 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364
4365 codec->num_pcms = 1;
4366 codec->pcm_info = info;
4367
Takashi Iwai352f7f92012-12-19 12:52:06 +01004368 if (spec->no_analog)
4369 goto skip_analog;
4370
Takashi Iwaif873e532012-12-20 16:58:39 +01004371 fill_pcm_stream_name(spec->stream_name_analog,
4372 sizeof(spec->stream_name_analog),
4373 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004374 info->name = spec->stream_name_analog;
4375
4376 if (spec->multiout.num_dacs > 0) {
4377 p = spec->stream_analog_playback;
4378 if (!p)
4379 p = &pcm_analog_playback;
4380 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4381 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
4382 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
4383 spec->multiout.max_channels;
4384 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
4385 spec->autocfg.line_outs == 2)
4386 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
4387 snd_pcm_2_1_chmaps;
4388 }
4389 if (spec->num_adc_nids) {
4390 p = spec->stream_analog_capture;
4391 if (!p) {
4392 if (spec->dyn_adc_switch)
4393 p = &dyn_adc_pcm_analog_capture;
4394 else
4395 p = &pcm_analog_capture;
4396 }
4397 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4398 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
4399 }
4400
Takashi Iwai352f7f92012-12-19 12:52:06 +01004401 skip_analog:
4402 /* SPDIF for stream index #1 */
4403 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01004404 fill_pcm_stream_name(spec->stream_name_digital,
4405 sizeof(spec->stream_name_digital),
4406 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004407 codec->num_pcms = 2;
4408 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
4409 info = spec->pcm_rec + 1;
4410 info->name = spec->stream_name_digital;
4411 if (spec->dig_out_type)
4412 info->pcm_type = spec->dig_out_type;
4413 else
4414 info->pcm_type = HDA_PCM_TYPE_SPDIF;
4415 if (spec->multiout.dig_out_nid) {
4416 p = spec->stream_digital_playback;
4417 if (!p)
4418 p = &pcm_digital_playback;
4419 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4420 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
4421 }
4422 if (spec->dig_in_nid) {
4423 p = spec->stream_digital_capture;
4424 if (!p)
4425 p = &pcm_digital_capture;
4426 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4427 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
4428 }
4429 }
4430
4431 if (spec->no_analog)
4432 return 0;
4433
4434 /* If the use of more than one ADC is requested for the current
4435 * model, configure a second analog capture-only PCM.
4436 */
4437 have_multi_adcs = (spec->num_adc_nids > 1) &&
4438 !spec->dyn_adc_switch && !spec->auto_mic;
4439 /* Additional Analaog capture for index #2 */
4440 if (spec->alt_dac_nid || have_multi_adcs) {
4441 codec->num_pcms = 3;
4442 info = spec->pcm_rec + 2;
4443 info->name = spec->stream_name_analog;
4444 if (spec->alt_dac_nid) {
4445 p = spec->stream_analog_alt_playback;
4446 if (!p)
4447 p = &pcm_analog_alt_playback;
4448 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4449 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
4450 spec->alt_dac_nid;
4451 } else {
4452 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
4453 pcm_null_stream;
4454 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
4455 }
4456 if (have_multi_adcs) {
4457 p = spec->stream_analog_alt_capture;
4458 if (!p)
4459 p = &pcm_analog_alt_capture;
4460 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4461 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
4462 spec->adc_nids[1];
4463 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
4464 spec->num_adc_nids - 1;
4465 } else {
4466 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
4467 pcm_null_stream;
4468 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
4469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 }
4471
4472 return 0;
4473}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004474EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
4475
4476
4477/*
4478 * Standard auto-parser initializations
4479 */
4480
Takashi Iwaid4156932013-01-07 10:08:02 +01004481/* configure the given path as a proper output */
Takashi Iwai2c12c302013-01-10 09:33:29 +01004482static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004483{
4484 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01004485 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004486
Takashi Iwai196c17662013-01-04 15:01:40 +01004487 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01004488 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004489 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01004490 pin = path->path[path->depth - 1];
Takashi Iwai2c12c302013-01-10 09:33:29 +01004491 restore_pin_ctl(codec, pin);
Takashi Iwaie1284af2013-01-03 16:33:02 +01004492 snd_hda_activate_path(codec, path, path->active, true);
4493 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004494}
4495
4496/* initialize primary output paths */
4497static void init_multi_out(struct hda_codec *codec)
4498{
4499 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004500 int i;
4501
Takashi Iwaid4156932013-01-07 10:08:02 +01004502 for (i = 0; i < spec->autocfg.line_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004503 set_output_and_unmute(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004504}
4505
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004506
Takashi Iwai2c12c302013-01-10 09:33:29 +01004507static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004508{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004509 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004510
Takashi Iwaid4156932013-01-07 10:08:02 +01004511 for (i = 0; i < num_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004512 set_output_and_unmute(codec, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004513}
4514
4515/* initialize hp and speaker paths */
4516static void init_extra_out(struct hda_codec *codec)
4517{
4518 struct hda_gen_spec *spec = codec->spec;
4519
4520 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004521 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004522 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
4523 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai2c12c302013-01-10 09:33:29 +01004524 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004525}
4526
4527/* initialize multi-io paths */
4528static void init_multi_io(struct hda_codec *codec)
4529{
4530 struct hda_gen_spec *spec = codec->spec;
4531 int i;
4532
4533 for (i = 0; i < spec->multi_ios; i++) {
4534 hda_nid_t pin = spec->multi_io[i].pin;
4535 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01004536 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004537 if (!path)
4538 continue;
4539 if (!spec->multi_io[i].ctl_in)
4540 spec->multi_io[i].ctl_in =
Takashi Iwai2c12c302013-01-10 09:33:29 +01004541 snd_hda_codec_get_pin_target(codec, pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004542 snd_hda_activate_path(codec, path, path->active, true);
4543 }
4544}
4545
Takashi Iwai352f7f92012-12-19 12:52:06 +01004546/* set up input pins and loopback paths */
4547static void init_analog_input(struct hda_codec *codec)
4548{
4549 struct hda_gen_spec *spec = codec->spec;
4550 struct auto_pin_cfg *cfg = &spec->autocfg;
4551 int i;
4552
4553 for (i = 0; i < cfg->num_inputs; i++) {
4554 hda_nid_t nid = cfg->inputs[i].pin;
4555 if (is_input_pin(codec, nid))
Takashi Iwai2c12c302013-01-10 09:33:29 +01004556 restore_pin_ctl(codec, nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004557
4558 /* init loopback inputs */
4559 if (spec->mixer_nid) {
4560 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01004561 path = snd_hda_get_path_from_idx(codec, spec->loopback_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004562 if (path)
4563 snd_hda_activate_path(codec, path,
4564 path->active, false);
4565 }
4566 }
4567}
4568
4569/* initialize ADC paths */
4570static void init_input_src(struct hda_codec *codec)
4571{
4572 struct hda_gen_spec *spec = codec->spec;
4573 struct hda_input_mux *imux = &spec->input_mux;
4574 struct nid_path *path;
4575 int i, c, nums;
4576
4577 if (spec->dyn_adc_switch)
4578 nums = 1;
4579 else
4580 nums = spec->num_adc_nids;
4581
4582 for (c = 0; c < nums; c++) {
4583 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01004584 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004585 if (path) {
4586 bool active = path->active;
4587 if (i == spec->cur_mux[c])
4588 active = true;
4589 snd_hda_activate_path(codec, path, active, false);
4590 }
4591 }
4592 }
4593
4594 if (spec->shared_mic_hp)
4595 update_shared_mic_hp(codec, spec->cur_mux[0]);
4596
4597 if (spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01004598 spec->cap_sync_hook(codec, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004599}
4600
4601/* set right pin controls for digital I/O */
4602static void init_digital(struct hda_codec *codec)
4603{
4604 struct hda_gen_spec *spec = codec->spec;
4605 int i;
4606 hda_nid_t pin;
4607
Takashi Iwaid4156932013-01-07 10:08:02 +01004608 for (i = 0; i < spec->autocfg.dig_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004609 set_output_and_unmute(codec, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004610 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01004611 if (pin) {
4612 struct nid_path *path;
Takashi Iwai2c12c302013-01-10 09:33:29 +01004613 restore_pin_ctl(codec, pin);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01004614 path = snd_hda_get_path_from_idx(codec, spec->digin_path);
4615 if (path)
4616 snd_hda_activate_path(codec, path, path->active, false);
4617 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004618}
4619
Takashi Iwai973e4972012-12-20 15:16:09 +01004620/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
4621 * invalid unsol tags by some reason
4622 */
4623static void clear_unsol_on_unused_pins(struct hda_codec *codec)
4624{
4625 int i;
4626
4627 for (i = 0; i < codec->init_pins.used; i++) {
4628 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
4629 hda_nid_t nid = pin->nid;
4630 if (is_jack_detectable(codec, nid) &&
4631 !snd_hda_jack_tbl_get(codec, nid))
4632 snd_hda_codec_update_cache(codec, nid, 0,
4633 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
4634 }
4635}
4636
Takashi Iwai5187ac12013-01-07 12:52:16 +01004637/*
4638 * initialize the generic spec;
4639 * this can be put as patch_ops.init function
4640 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004641int snd_hda_gen_init(struct hda_codec *codec)
4642{
4643 struct hda_gen_spec *spec = codec->spec;
4644
4645 if (spec->init_hook)
4646 spec->init_hook(codec);
4647
4648 snd_hda_apply_verbs(codec);
4649
Takashi Iwai3bbcd272012-12-20 11:50:58 +01004650 codec->cached_write = 1;
4651
Takashi Iwai352f7f92012-12-19 12:52:06 +01004652 init_multi_out(codec);
4653 init_extra_out(codec);
4654 init_multi_io(codec);
4655 init_analog_input(codec);
4656 init_input_src(codec);
4657 init_digital(codec);
4658
Takashi Iwai973e4972012-12-20 15:16:09 +01004659 clear_unsol_on_unused_pins(codec);
4660
Takashi Iwai352f7f92012-12-19 12:52:06 +01004661 /* call init functions of standard auto-mute helpers */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01004662 update_automute_all(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004663
Takashi Iwai3bbcd272012-12-20 11:50:58 +01004664 snd_hda_codec_flush_amp_cache(codec);
4665 snd_hda_codec_flush_cmd_cache(codec);
4666
Takashi Iwai352f7f92012-12-19 12:52:06 +01004667 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
4668 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
4669
4670 hda_call_check_power_status(codec, 0x01);
4671 return 0;
4672}
Takashi Iwaifce52a32013-01-07 12:42:48 +01004673EXPORT_SYMBOL_HDA(snd_hda_gen_init);
4674
Takashi Iwai5187ac12013-01-07 12:52:16 +01004675/*
4676 * free the generic spec;
4677 * this can be put as patch_ops.free function
4678 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01004679void snd_hda_gen_free(struct hda_codec *codec)
4680{
4681 snd_hda_gen_spec_free(codec->spec);
4682 kfree(codec->spec);
4683 codec->spec = NULL;
4684}
4685EXPORT_SYMBOL_HDA(snd_hda_gen_free);
4686
4687#ifdef CONFIG_PM
Takashi Iwai5187ac12013-01-07 12:52:16 +01004688/*
4689 * check the loopback power save state;
4690 * this can be put as patch_ops.check_power_status function
4691 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01004692int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
4693{
4694 struct hda_gen_spec *spec = codec->spec;
4695 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
4696}
4697EXPORT_SYMBOL_HDA(snd_hda_gen_check_power_status);
4698#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01004699
4700
4701/*
4702 * the generic codec support
4703 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704
Takashi Iwai352f7f92012-12-19 12:52:06 +01004705static const struct hda_codec_ops generic_patch_ops = {
4706 .build_controls = snd_hda_gen_build_controls,
4707 .build_pcms = snd_hda_gen_build_pcms,
4708 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01004709 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004710 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02004711#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01004712 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02004713#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714};
4715
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716int snd_hda_parse_generic_codec(struct hda_codec *codec)
4717{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004718 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 int err;
4720
Takashi Iwaie560d8d2005-09-09 14:21:46 +02004721 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004722 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004724 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004727 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
4728 if (err < 0)
4729 return err;
4730
4731 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004732 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733 goto error;
4734
4735 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736 return 0;
4737
Takashi Iwai352f7f92012-12-19 12:52:06 +01004738error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01004739 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740 return err;
4741}
Takashi Iwaifce52a32013-01-07 12:42:48 +01004742EXPORT_SYMBOL_HDA(snd_hda_parse_generic_codec);