blob: cadfe65e2fe1f6ae69246e12cf163d1f1cf03a5e [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 */
753static int add_control(struct hda_gen_spec *spec, int type, const char *name,
754 int cidx, unsigned long val)
755{
756 struct snd_kcontrol_new *knew;
757
Takashi Iwai12c93df2012-12-19 14:38:33 +0100758 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100759 if (!knew)
760 return -ENOMEM;
761 knew->index = cidx;
762 if (get_amp_nid_(val))
763 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
764 knew->private_value = val;
765 return 0;
766}
767
768static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
769 const char *pfx, const char *dir,
770 const char *sfx, int cidx, unsigned long val)
771{
772 char name[32];
773 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
774 return add_control(spec, type, name, cidx, val);
775}
776
777#define add_pb_vol_ctrl(spec, type, pfx, val) \
778 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
779#define add_pb_sw_ctrl(spec, type, pfx, val) \
780 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
781#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
782 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
783#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
784 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
785
786static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
787 unsigned int chs, struct nid_path *path)
788{
789 unsigned int val;
790 if (!path)
791 return 0;
792 val = path->ctls[NID_PATH_VOL_CTL];
793 if (!val)
794 return 0;
795 val = amp_val_replace_channels(val, chs);
796 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
797}
798
799/* return the channel bits suitable for the given path->ctls[] */
800static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
801 int type)
802{
803 int chs = 1; /* mono (left only) */
804 if (path) {
805 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
806 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
807 chs = 3; /* stereo */
808 }
809 return chs;
810}
811
812static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
813 struct nid_path *path)
814{
815 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
816 return add_vol_ctl(codec, pfx, cidx, chs, path);
817}
818
819/* create a mute-switch for the given mixer widget;
820 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
821 */
822static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
823 unsigned int chs, struct nid_path *path)
824{
825 unsigned int val;
826 int type = HDA_CTL_WIDGET_MUTE;
827
828 if (!path)
829 return 0;
830 val = path->ctls[NID_PATH_MUTE_CTL];
831 if (!val)
832 return 0;
833 val = amp_val_replace_channels(val, chs);
834 if (get_amp_direction_(val) == HDA_INPUT) {
835 hda_nid_t nid = get_amp_nid_(val);
836 int nums = snd_hda_get_num_conns(codec, nid);
837 if (nums > 1) {
838 type = HDA_CTL_BIND_MUTE;
839 val |= nums << 19;
840 }
841 }
842 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
843}
844
845static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
846 int cidx, struct nid_path *path)
847{
848 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
849 return add_sw_ctl(codec, pfx, cidx, chs, path);
850}
851
Takashi Iwai247d85e2013-01-17 16:18:11 +0100852/* any ctl assigned to the path with the given index? */
853static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
854{
855 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
856 return path && path->ctls[ctl_type];
857}
858
Takashi Iwai352f7f92012-12-19 12:52:06 +0100859static const char * const channel_name[4] = {
860 "Front", "Surround", "CLFE", "Side"
861};
862
863/* give some appropriate ctl name prefix for the given line out channel */
Takashi Iwai247d85e2013-01-17 16:18:11 +0100864static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
865 int *index, int ctl_type)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100866{
Takashi Iwai247d85e2013-01-17 16:18:11 +0100867 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100868 struct auto_pin_cfg *cfg = &spec->autocfg;
869
870 *index = 0;
871 if (cfg->line_outs == 1 && !spec->multi_ios &&
Takashi Iwai247d85e2013-01-17 16:18:11 +0100872 !cfg->hp_outs && !cfg->speaker_outs)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100873 return spec->vmaster_mute.hook ? "PCM" : "Master";
874
875 /* if there is really a single DAC used in the whole output paths,
876 * use it master (or "PCM" if a vmaster hook is present)
877 */
878 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
879 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
880 return spec->vmaster_mute.hook ? "PCM" : "Master";
881
Takashi Iwai247d85e2013-01-17 16:18:11 +0100882 /* multi-io channels */
883 if (ch >= cfg->line_outs)
884 return channel_name[ch];
885
Takashi Iwai352f7f92012-12-19 12:52:06 +0100886 switch (cfg->line_out_type) {
887 case AUTO_PIN_SPEAKER_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +0100888 /* if the primary channel vol/mute is shared with HP volume,
889 * don't name it as Speaker
890 */
891 if (!ch && cfg->hp_outs &&
892 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
893 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100894 if (cfg->line_outs == 1)
895 return "Speaker";
896 if (cfg->line_outs == 2)
897 return ch ? "Bass Speaker" : "Speaker";
898 break;
899 case AUTO_PIN_HP_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +0100900 /* if the primary channel vol/mute is shared with spk volume,
901 * don't name it as Headphone
902 */
903 if (!ch && cfg->speaker_outs &&
904 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
905 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100906 /* for multi-io case, only the primary out */
907 if (ch && spec->multi_ios)
908 break;
909 *index = ch;
910 return "Headphone";
Takashi Iwai352f7f92012-12-19 12:52:06 +0100911 }
Takashi Iwai247d85e2013-01-17 16:18:11 +0100912
913 /* for a single channel output, we don't have to name the channel */
914 if (cfg->line_outs == 1 && !spec->multi_ios)
915 return "PCM";
916
Takashi Iwai352f7f92012-12-19 12:52:06 +0100917 if (ch >= ARRAY_SIZE(channel_name)) {
918 snd_BUG();
919 return "PCM";
920 }
921
922 return channel_name[ch];
923}
924
925/*
926 * Parse output paths
927 */
928
929/* badness definition */
930enum {
931 /* No primary DAC is found for the main output */
932 BAD_NO_PRIMARY_DAC = 0x10000,
933 /* No DAC is found for the extra output */
934 BAD_NO_DAC = 0x4000,
935 /* No possible multi-ios */
936 BAD_MULTI_IO = 0x103,
937 /* No individual DAC for extra output */
938 BAD_NO_EXTRA_DAC = 0x102,
939 /* No individual DAC for extra surrounds */
940 BAD_NO_EXTRA_SURR_DAC = 0x101,
941 /* Primary DAC shared with main surrounds */
942 BAD_SHARED_SURROUND = 0x100,
943 /* Primary DAC shared with main CLFE */
944 BAD_SHARED_CLFE = 0x10,
945 /* Primary DAC shared with extra surrounds */
946 BAD_SHARED_EXTRA_SURROUND = 0x10,
947 /* Volume widget is shared */
948 BAD_SHARED_VOL = 0x10,
949};
950
Takashi Iwai0e614dd2013-01-07 15:11:44 +0100951/* look for widgets in the given path which are appropriate for
Takashi Iwai352f7f92012-12-19 12:52:06 +0100952 * volume and mute controls, and assign the values to ctls[].
953 *
954 * When no appropriate widget is found in the path, the badness value
955 * is incremented depending on the situation. The function returns the
956 * total badness for both volume and mute controls.
957 */
Takashi Iwai0e614dd2013-01-07 15:11:44 +0100958static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100959{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100960 hda_nid_t nid;
961 unsigned int val;
962 int badness = 0;
963
964 if (!path)
965 return BAD_SHARED_VOL * 2;
Takashi Iwai0e614dd2013-01-07 15:11:44 +0100966
967 if (path->ctls[NID_PATH_VOL_CTL] ||
968 path->ctls[NID_PATH_MUTE_CTL])
969 return 0; /* already evaluated */
970
Takashi Iwai352f7f92012-12-19 12:52:06 +0100971 nid = look_for_out_vol_nid(codec, path);
972 if (nid) {
973 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
974 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
975 badness += BAD_SHARED_VOL;
976 else
977 path->ctls[NID_PATH_VOL_CTL] = val;
978 } else
979 badness += BAD_SHARED_VOL;
980 nid = look_for_out_mute_nid(codec, path);
981 if (nid) {
982 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
983 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
984 nid_has_mute(codec, nid, HDA_OUTPUT))
985 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
986 else
987 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
988 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
989 badness += BAD_SHARED_VOL;
990 else
991 path->ctls[NID_PATH_MUTE_CTL] = val;
992 } else
993 badness += BAD_SHARED_VOL;
994 return badness;
995}
996
997struct badness_table {
998 int no_primary_dac; /* no primary DAC */
999 int no_dac; /* no secondary DACs */
1000 int shared_primary; /* primary DAC is shared with main output */
1001 int shared_surr; /* secondary DAC shared with main or primary */
1002 int shared_clfe; /* third DAC shared with main or primary */
1003 int shared_surr_main; /* secondary DAC sahred with main/DAC0 */
1004};
1005
1006static struct badness_table main_out_badness = {
1007 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1008 .no_dac = BAD_NO_DAC,
1009 .shared_primary = BAD_NO_PRIMARY_DAC,
1010 .shared_surr = BAD_SHARED_SURROUND,
1011 .shared_clfe = BAD_SHARED_CLFE,
1012 .shared_surr_main = BAD_SHARED_SURROUND,
1013};
1014
1015static struct badness_table extra_out_badness = {
1016 .no_primary_dac = BAD_NO_DAC,
1017 .no_dac = BAD_NO_DAC,
1018 .shared_primary = BAD_NO_EXTRA_DAC,
1019 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1020 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1021 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1022};
1023
Takashi Iwai7385df62013-01-07 09:50:52 +01001024/* get the DAC of the primary output corresponding to the given array index */
1025static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1026{
1027 struct hda_gen_spec *spec = codec->spec;
1028 struct auto_pin_cfg *cfg = &spec->autocfg;
1029
1030 if (cfg->line_outs > idx)
1031 return spec->private_dac_nids[idx];
1032 idx -= cfg->line_outs;
1033 if (spec->multi_ios > idx)
1034 return spec->multi_io[idx].dac;
1035 return 0;
1036}
1037
1038/* return the DAC if it's reachable, otherwise zero */
1039static inline hda_nid_t try_dac(struct hda_codec *codec,
1040 hda_nid_t dac, hda_nid_t pin)
1041{
1042 return is_reachable_path(codec, dac, pin) ? dac : 0;
1043}
1044
Takashi Iwai352f7f92012-12-19 12:52:06 +01001045/* try to assign DACs to pins and return the resultant badness */
1046static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1047 const hda_nid_t *pins, hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001048 int *path_idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001049 const struct badness_table *bad)
1050{
1051 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001052 int i, j;
1053 int badness = 0;
1054 hda_nid_t dac;
1055
1056 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 return 0;
1058
Takashi Iwai352f7f92012-12-19 12:52:06 +01001059 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001060 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001061 hda_nid_t pin = pins[i];
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001062
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001063 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1064 if (path) {
1065 badness += assign_out_path_ctls(codec, path);
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001066 continue;
1067 }
1068
1069 dacs[i] = look_for_dac(codec, pin, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001070 if (!dacs[i] && !i) {
Takashi Iwai980428c2013-01-09 09:28:20 +01001071 /* try to steal the DAC of surrounds for the front */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001072 for (j = 1; j < num_outs; j++) {
1073 if (is_reachable_path(codec, dacs[j], pin)) {
1074 dacs[0] = dacs[j];
1075 dacs[j] = 0;
Takashi Iwai980428c2013-01-09 09:28:20 +01001076 invalidate_nid_path(codec, path_idx[j]);
Takashi Iwai196c17662013-01-04 15:01:40 +01001077 path_idx[j] = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001078 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 }
1080 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001081 }
1082 dac = dacs[i];
1083 if (!dac) {
Takashi Iwai7385df62013-01-07 09:50:52 +01001084 if (num_outs > 2)
1085 dac = try_dac(codec, get_primary_out(codec, i), pin);
1086 if (!dac)
1087 dac = try_dac(codec, dacs[0], pin);
1088 if (!dac)
1089 dac = try_dac(codec, get_primary_out(codec, i), pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001090 if (dac) {
1091 if (!i)
1092 badness += bad->shared_primary;
1093 else if (i == 1)
1094 badness += bad->shared_surr;
1095 else
1096 badness += bad->shared_clfe;
1097 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1098 dac = spec->private_dac_nids[0];
1099 badness += bad->shared_surr_main;
1100 } else if (!i)
1101 badness += bad->no_primary_dac;
1102 else
1103 badness += bad->no_dac;
1104 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001105 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001106 if (!path && !i && spec->mixer_nid) {
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001107 /* try with aamix */
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001108 path = snd_hda_add_new_path(codec, dac, pin, 0);
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001109 }
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001110 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001111 dac = dacs[i] = 0;
Takashi Iwaie1284af2013-01-03 16:33:02 +01001112 else {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001113 print_nid_path("output", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001114 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001115 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001116 badness += assign_out_path_ctls(codec, path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001117 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001118 }
1119
1120 return badness;
1121}
1122
1123/* return NID if the given pin has only a single connection to a certain DAC */
1124static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1125{
1126 struct hda_gen_spec *spec = codec->spec;
1127 int i;
1128 hda_nid_t nid_found = 0;
1129
1130 for (i = 0; i < spec->num_all_dacs; i++) {
1131 hda_nid_t nid = spec->all_dacs[i];
1132 if (!nid || is_dac_already_used(codec, nid))
1133 continue;
1134 if (is_reachable_path(codec, nid, pin)) {
1135 if (nid_found)
1136 return 0;
1137 nid_found = nid;
1138 }
1139 }
1140 return nid_found;
1141}
1142
1143/* check whether the given pin can be a multi-io pin */
1144static bool can_be_multiio_pin(struct hda_codec *codec,
1145 unsigned int location, hda_nid_t nid)
1146{
1147 unsigned int defcfg, caps;
1148
1149 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1150 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1151 return false;
1152 if (location && get_defcfg_location(defcfg) != location)
1153 return false;
1154 caps = snd_hda_query_pin_caps(codec, nid);
1155 if (!(caps & AC_PINCAP_OUT))
1156 return false;
1157 return true;
1158}
1159
Takashi Iwaie22aab72013-01-04 14:50:04 +01001160/* count the number of input pins that are capable to be multi-io */
1161static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1162{
1163 struct hda_gen_spec *spec = codec->spec;
1164 struct auto_pin_cfg *cfg = &spec->autocfg;
1165 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1166 unsigned int location = get_defcfg_location(defcfg);
1167 int type, i;
1168 int num_pins = 0;
1169
1170 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1171 for (i = 0; i < cfg->num_inputs; i++) {
1172 if (cfg->inputs[i].type != type)
1173 continue;
1174 if (can_be_multiio_pin(codec, location,
1175 cfg->inputs[i].pin))
1176 num_pins++;
1177 }
1178 }
1179 return num_pins;
1180}
1181
Takashi Iwai352f7f92012-12-19 12:52:06 +01001182/*
1183 * multi-io helper
1184 *
1185 * When hardwired is set, try to fill ony hardwired pins, and returns
1186 * zero if any pins are filled, non-zero if nothing found.
1187 * When hardwired is off, try to fill possible input pins, and returns
1188 * the badness value.
1189 */
1190static int fill_multi_ios(struct hda_codec *codec,
1191 hda_nid_t reference_pin,
Takashi Iwaie22aab72013-01-04 14:50:04 +01001192 bool hardwired)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001193{
1194 struct hda_gen_spec *spec = codec->spec;
1195 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie22aab72013-01-04 14:50:04 +01001196 int type, i, j, num_pins, old_pins;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001197 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1198 unsigned int location = get_defcfg_location(defcfg);
1199 int badness = 0;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001200 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001201
1202 old_pins = spec->multi_ios;
1203 if (old_pins >= 2)
1204 goto end_fill;
1205
Takashi Iwaie22aab72013-01-04 14:50:04 +01001206 num_pins = count_multiio_pins(codec, reference_pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001207 if (num_pins < 2)
1208 goto end_fill;
1209
Takashi Iwai352f7f92012-12-19 12:52:06 +01001210 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1211 for (i = 0; i < cfg->num_inputs; i++) {
1212 hda_nid_t nid = cfg->inputs[i].pin;
1213 hda_nid_t dac = 0;
1214
1215 if (cfg->inputs[i].type != type)
1216 continue;
1217 if (!can_be_multiio_pin(codec, location, nid))
1218 continue;
1219 for (j = 0; j < spec->multi_ios; j++) {
1220 if (nid == spec->multi_io[j].pin)
1221 break;
1222 }
1223 if (j < spec->multi_ios)
1224 continue;
1225
Takashi Iwai352f7f92012-12-19 12:52:06 +01001226 if (hardwired)
1227 dac = get_dac_if_single(codec, nid);
1228 else if (!dac)
1229 dac = look_for_dac(codec, nid, false);
1230 if (!dac) {
1231 badness++;
1232 continue;
1233 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001234 path = snd_hda_add_new_path(codec, dac, nid,
1235 -spec->mixer_nid);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001236 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001237 badness++;
1238 continue;
1239 }
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001240 print_nid_path("multiio", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001241 spec->multi_io[spec->multi_ios].pin = nid;
1242 spec->multi_io[spec->multi_ios].dac = dac;
Takashi Iwai196c17662013-01-04 15:01:40 +01001243 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1244 snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001245 spec->multi_ios++;
1246 if (spec->multi_ios >= 2)
1247 break;
1248 }
1249 }
1250 end_fill:
1251 if (badness)
1252 badness = BAD_MULTI_IO;
1253 if (old_pins == spec->multi_ios) {
1254 if (hardwired)
1255 return 1; /* nothing found */
1256 else
1257 return badness; /* no badness if nothing found */
1258 }
1259 if (!hardwired && spec->multi_ios < 2) {
1260 /* cancel newly assigned paths */
1261 spec->paths.used -= spec->multi_ios - old_pins;
1262 spec->multi_ios = old_pins;
1263 return badness;
1264 }
1265
1266 /* assign volume and mute controls */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001267 for (i = old_pins; i < spec->multi_ios; i++) {
1268 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1269 badness += assign_out_path_ctls(codec, path);
1270 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001271
1272 return badness;
1273}
1274
1275/* map DACs for all pins in the list if they are single connections */
1276static bool map_singles(struct hda_codec *codec, int outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001277 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001278{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001279 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001280 int i;
1281 bool found = false;
1282 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001283 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001284 hda_nid_t dac;
1285 if (dacs[i])
1286 continue;
1287 dac = get_dac_if_single(codec, pins[i]);
1288 if (!dac)
1289 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001290 path = snd_hda_add_new_path(codec, dac, pins[i],
1291 -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001292 if (!path && !i && spec->mixer_nid)
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001293 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001294 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001295 dacs[i] = dac;
1296 found = true;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001297 print_nid_path("output", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001298 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001299 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001300 }
1301 }
1302 return found;
1303}
1304
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001305/* create a new path including aamix if available, and return its index */
1306static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1307{
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001308 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001309 struct nid_path *path;
1310
1311 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001312 if (!path || !path->depth ||
1313 is_nid_contained(path, spec->mixer_nid))
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001314 return 0;
1315 path = snd_hda_add_new_path(codec, path->path[0],
1316 path->path[path->depth - 1],
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001317 spec->mixer_nid);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001318 if (!path)
1319 return 0;
1320 print_nid_path("output-aamix", path);
1321 path->active = false; /* unused as default */
1322 return snd_hda_get_path_idx(codec, path);
1323}
1324
Takashi Iwaia07a9492013-01-07 16:44:06 +01001325/* fill the empty entries in the dac array for speaker/hp with the
1326 * shared dac pointed by the paths
1327 */
1328static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1329 hda_nid_t *dacs, int *path_idx)
1330{
1331 struct nid_path *path;
1332 int i;
1333
1334 for (i = 0; i < num_outs; i++) {
1335 if (dacs[i])
1336 continue;
1337 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1338 if (!path)
1339 continue;
1340 dacs[i] = path->path[0];
1341 }
1342}
1343
Takashi Iwai352f7f92012-12-19 12:52:06 +01001344/* fill in the dac_nids table from the parsed pin configuration */
1345static int fill_and_eval_dacs(struct hda_codec *codec,
1346 bool fill_hardwired,
1347 bool fill_mio_first)
1348{
1349 struct hda_gen_spec *spec = codec->spec;
1350 struct auto_pin_cfg *cfg = &spec->autocfg;
1351 int i, err, badness;
Takashi Iwaiea46c3c2013-01-15 18:45:53 +01001352 unsigned int val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001353
1354 /* set num_dacs once to full for look_for_dac() */
1355 spec->multiout.num_dacs = cfg->line_outs;
1356 spec->multiout.dac_nids = spec->private_dac_nids;
1357 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1358 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1359 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1360 spec->multi_ios = 0;
1361 snd_array_free(&spec->paths);
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001362
1363 /* clear path indices */
1364 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1365 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1366 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1367 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1368 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
Takashi Iwaic697b712013-01-07 17:09:26 +01001369 memset(spec->input_paths, 0, sizeof(spec->input_paths));
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001370 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1371 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1372
Takashi Iwai352f7f92012-12-19 12:52:06 +01001373 badness = 0;
1374
1375 /* fill hard-wired DACs first */
1376 if (fill_hardwired) {
1377 bool mapped;
1378 do {
1379 mapped = map_singles(codec, cfg->line_outs,
1380 cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001381 spec->private_dac_nids,
1382 spec->out_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001383 mapped |= map_singles(codec, cfg->hp_outs,
1384 cfg->hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001385 spec->multiout.hp_out_nid,
1386 spec->hp_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001387 mapped |= map_singles(codec, cfg->speaker_outs,
1388 cfg->speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001389 spec->multiout.extra_out_nid,
1390 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001391 if (fill_mio_first && cfg->line_outs == 1 &&
1392 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001393 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001394 if (!err)
1395 mapped = true;
1396 }
1397 } while (mapped);
1398 }
1399
1400 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001401 spec->private_dac_nids, spec->out_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001402 &main_out_badness);
1403
Takashi Iwai352f7f92012-12-19 12:52:06 +01001404 if (fill_mio_first &&
1405 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1406 /* try to fill multi-io first */
Takashi Iwaie22aab72013-01-04 14:50:04 +01001407 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001408 if (err < 0)
1409 return err;
1410 /* we don't count badness at this stage yet */
1411 }
1412
1413 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1414 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1415 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001416 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001417 &extra_out_badness);
1418 if (err < 0)
1419 return err;
1420 badness += err;
1421 }
1422 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1423 err = try_assign_dacs(codec, cfg->speaker_outs,
1424 cfg->speaker_pins,
1425 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001426 spec->speaker_paths,
1427 &extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001428 if (err < 0)
1429 return err;
1430 badness += err;
1431 }
1432 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001433 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001434 if (err < 0)
1435 return err;
1436 badness += err;
1437 }
Takashi Iwaie22aab72013-01-04 14:50:04 +01001438
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001439 if (spec->mixer_nid) {
1440 spec->aamix_out_paths[0] =
1441 check_aamix_out_path(codec, spec->out_paths[0]);
1442 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1443 spec->aamix_out_paths[1] =
1444 check_aamix_out_path(codec, spec->hp_paths[0]);
1445 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1446 spec->aamix_out_paths[2] =
1447 check_aamix_out_path(codec, spec->speaker_paths[0]);
1448 }
1449
Takashi Iwaie22aab72013-01-04 14:50:04 +01001450 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1451 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1452 spec->multi_ios = 1; /* give badness */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001453
Takashi Iwaia07a9492013-01-07 16:44:06 +01001454 /* re-count num_dacs and squash invalid entries */
1455 spec->multiout.num_dacs = 0;
1456 for (i = 0; i < cfg->line_outs; i++) {
1457 if (spec->private_dac_nids[i])
1458 spec->multiout.num_dacs++;
1459 else {
1460 memmove(spec->private_dac_nids + i,
1461 spec->private_dac_nids + i + 1,
1462 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1463 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1464 }
1465 }
1466
1467 spec->ext_channel_count = spec->min_channel_count =
David Henningssonc0f3b212013-01-16 11:45:37 +01001468 spec->multiout.num_dacs * 2;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001469
Takashi Iwai352f7f92012-12-19 12:52:06 +01001470 if (spec->multi_ios == 2) {
1471 for (i = 0; i < 2; i++)
1472 spec->private_dac_nids[spec->multiout.num_dacs++] =
1473 spec->multi_io[i].dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001474 } else if (spec->multi_ios) {
1475 spec->multi_ios = 0;
1476 badness += BAD_MULTI_IO;
1477 }
1478
Takashi Iwaia07a9492013-01-07 16:44:06 +01001479 /* re-fill the shared DAC for speaker / headphone */
1480 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1481 refill_shared_dacs(codec, cfg->hp_outs,
1482 spec->multiout.hp_out_nid,
1483 spec->hp_paths);
1484 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1485 refill_shared_dacs(codec, cfg->speaker_outs,
1486 spec->multiout.extra_out_nid,
1487 spec->speaker_paths);
1488
Takashi Iwai2c12c302013-01-10 09:33:29 +01001489 /* set initial pinctl targets */
Takashi Iwaiea46c3c2013-01-15 18:45:53 +01001490 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
1491 val = PIN_HP;
1492 else
1493 val = PIN_OUT;
1494 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
Takashi Iwai2c12c302013-01-10 09:33:29 +01001495 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1496 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
Takashi Iwaiea46c3c2013-01-15 18:45:53 +01001497 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1498 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
Takashi Iwai2c12c302013-01-10 09:33:29 +01001499 set_pin_targets(codec, cfg->speaker_outs,
Takashi Iwaiea46c3c2013-01-15 18:45:53 +01001500 cfg->speaker_pins, val);
1501 }
Takashi Iwai2c12c302013-01-10 09:33:29 +01001502
Takashi Iwai352f7f92012-12-19 12:52:06 +01001503 return badness;
1504}
1505
1506#define DEBUG_BADNESS
1507
1508#ifdef DEBUG_BADNESS
1509#define debug_badness snd_printdd
1510#else
1511#define debug_badness(...)
1512#endif
1513
1514static void debug_show_configs(struct hda_gen_spec *spec, struct auto_pin_cfg *cfg)
1515{
1516 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1517 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001518 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001519 spec->multiout.dac_nids[0],
1520 spec->multiout.dac_nids[1],
1521 spec->multiout.dac_nids[2],
1522 spec->multiout.dac_nids[3]);
1523 if (spec->multi_ios > 0)
1524 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1525 spec->multi_ios,
1526 spec->multi_io[0].pin, spec->multi_io[1].pin,
1527 spec->multi_io[0].dac, spec->multi_io[1].dac);
1528 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1529 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001530 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001531 spec->multiout.hp_out_nid[0],
1532 spec->multiout.hp_out_nid[1],
1533 spec->multiout.hp_out_nid[2],
1534 spec->multiout.hp_out_nid[3]);
1535 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1536 cfg->speaker_pins[0], cfg->speaker_pins[1],
1537 cfg->speaker_pins[2], cfg->speaker_pins[3],
1538 spec->multiout.extra_out_nid[0],
1539 spec->multiout.extra_out_nid[1],
1540 spec->multiout.extra_out_nid[2],
1541 spec->multiout.extra_out_nid[3]);
1542}
1543
1544/* find all available DACs of the codec */
1545static void fill_all_dac_nids(struct hda_codec *codec)
1546{
1547 struct hda_gen_spec *spec = codec->spec;
1548 int i;
1549 hda_nid_t nid = codec->start_nid;
1550
1551 spec->num_all_dacs = 0;
1552 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1553 for (i = 0; i < codec->num_nodes; i++, nid++) {
1554 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1555 continue;
1556 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1557 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1558 break;
1559 }
1560 spec->all_dacs[spec->num_all_dacs++] = nid;
1561 }
1562}
1563
1564static int parse_output_paths(struct hda_codec *codec)
1565{
1566 struct hda_gen_spec *spec = codec->spec;
1567 struct auto_pin_cfg *cfg = &spec->autocfg;
1568 struct auto_pin_cfg *best_cfg;
1569 int best_badness = INT_MAX;
1570 int badness;
1571 bool fill_hardwired = true, fill_mio_first = true;
1572 bool best_wired = true, best_mio = true;
1573 bool hp_spk_swapped = false;
1574
Takashi Iwai352f7f92012-12-19 12:52:06 +01001575 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1576 if (!best_cfg)
1577 return -ENOMEM;
1578 *best_cfg = *cfg;
1579
1580 for (;;) {
1581 badness = fill_and_eval_dacs(codec, fill_hardwired,
1582 fill_mio_first);
1583 if (badness < 0) {
1584 kfree(best_cfg);
1585 return badness;
1586 }
1587 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1588 cfg->line_out_type, fill_hardwired, fill_mio_first,
1589 badness);
1590 debug_show_configs(spec, cfg);
1591 if (badness < best_badness) {
1592 best_badness = badness;
1593 *best_cfg = *cfg;
1594 best_wired = fill_hardwired;
1595 best_mio = fill_mio_first;
1596 }
1597 if (!badness)
1598 break;
1599 fill_mio_first = !fill_mio_first;
1600 if (!fill_mio_first)
1601 continue;
1602 fill_hardwired = !fill_hardwired;
1603 if (!fill_hardwired)
1604 continue;
1605 if (hp_spk_swapped)
1606 break;
1607 hp_spk_swapped = true;
1608 if (cfg->speaker_outs > 0 &&
1609 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1610 cfg->hp_outs = cfg->line_outs;
1611 memcpy(cfg->hp_pins, cfg->line_out_pins,
1612 sizeof(cfg->hp_pins));
1613 cfg->line_outs = cfg->speaker_outs;
1614 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1615 sizeof(cfg->speaker_pins));
1616 cfg->speaker_outs = 0;
1617 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1618 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1619 fill_hardwired = true;
1620 continue;
1621 }
1622 if (cfg->hp_outs > 0 &&
1623 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1624 cfg->speaker_outs = cfg->line_outs;
1625 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1626 sizeof(cfg->speaker_pins));
1627 cfg->line_outs = cfg->hp_outs;
1628 memcpy(cfg->line_out_pins, cfg->hp_pins,
1629 sizeof(cfg->hp_pins));
1630 cfg->hp_outs = 0;
1631 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1632 cfg->line_out_type = AUTO_PIN_HP_OUT;
1633 fill_hardwired = true;
1634 continue;
1635 }
1636 break;
1637 }
1638
1639 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001640 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001641 *cfg = *best_cfg;
1642 fill_and_eval_dacs(codec, best_wired, best_mio);
1643 }
1644 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1645 cfg->line_out_type, best_wired, best_mio);
1646 debug_show_configs(spec, cfg);
1647
1648 if (cfg->line_out_pins[0]) {
1649 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01001650 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001651 if (path)
1652 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01001653 if (spec->vmaster_nid)
1654 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
1655 HDA_OUTPUT, spec->vmaster_tlv);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001656 }
1657
1658 kfree(best_cfg);
1659 return 0;
1660}
1661
1662/* add playback controls from the parsed DAC table */
1663static int create_multi_out_ctls(struct hda_codec *codec,
1664 const struct auto_pin_cfg *cfg)
1665{
1666 struct hda_gen_spec *spec = codec->spec;
1667 int i, err, noutputs;
1668
1669 noutputs = cfg->line_outs;
1670 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1671 noutputs += spec->multi_ios;
1672
1673 for (i = 0; i < noutputs; i++) {
1674 const char *name;
1675 int index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001676 struct nid_path *path;
1677
Takashi Iwai196c17662013-01-04 15:01:40 +01001678 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001679 if (!path)
1680 continue;
Takashi Iwai247d85e2013-01-17 16:18:11 +01001681
1682 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001683 if (!name || !strcmp(name, "CLFE")) {
1684 /* Center/LFE */
1685 err = add_vol_ctl(codec, "Center", 0, 1, path);
1686 if (err < 0)
1687 return err;
1688 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1689 if (err < 0)
1690 return err;
Takashi Iwai247d85e2013-01-17 16:18:11 +01001691 } else {
1692 err = add_stereo_vol(codec, name, index, path);
1693 if (err < 0)
1694 return err;
1695 }
1696
1697 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
1698 if (!name || !strcmp(name, "CLFE")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001699 err = add_sw_ctl(codec, "Center", 0, 1, path);
1700 if (err < 0)
1701 return err;
1702 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1703 if (err < 0)
1704 return err;
1705 } else {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001706 err = add_stereo_sw(codec, name, index, path);
1707 if (err < 0)
1708 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 }
1710 }
1711 return 0;
1712}
1713
Takashi Iwaic2c80382013-01-07 10:33:57 +01001714static int create_extra_out(struct hda_codec *codec, int path_idx,
Takashi Iwai196c17662013-01-04 15:01:40 +01001715 const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001717 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 int err;
1719
Takashi Iwai196c17662013-01-04 15:01:40 +01001720 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001721 if (!path)
1722 return 0;
Takashi Iwaic2c80382013-01-07 10:33:57 +01001723 err = add_stereo_vol(codec, pfx, cidx, path);
1724 if (err < 0)
1725 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001726 err = add_stereo_sw(codec, pfx, cidx, path);
1727 if (err < 0)
1728 return err;
1729 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730}
1731
Takashi Iwai352f7f92012-12-19 12:52:06 +01001732/* add playback controls for speaker and HP outputs */
1733static int create_extra_outs(struct hda_codec *codec, int num_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001734 const int *paths, const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735{
Takashi Iwaic2c80382013-01-07 10:33:57 +01001736 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001737
1738 for (i = 0; i < num_pins; i++) {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001739 const char *name;
1740 char tmp[44];
1741 int err, idx = 0;
1742
1743 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
1744 name = "Bass Speaker";
1745 else if (num_pins >= 3) {
1746 snprintf(tmp, sizeof(tmp), "%s %s",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001747 pfx, channel_name[i]);
Takashi Iwaic2c80382013-01-07 10:33:57 +01001748 name = tmp;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001749 } else {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001750 name = pfx;
1751 idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 }
Takashi Iwaic2c80382013-01-07 10:33:57 +01001753 err = create_extra_out(codec, paths[i], name, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001754 if (err < 0)
1755 return err;
1756 }
1757 return 0;
1758}
Takashi Iwai97ec5582006-03-21 11:29:07 +01001759
Takashi Iwai352f7f92012-12-19 12:52:06 +01001760static int create_hp_out_ctls(struct hda_codec *codec)
1761{
1762 struct hda_gen_spec *spec = codec->spec;
1763 return create_extra_outs(codec, spec->autocfg.hp_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001764 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001765 "Headphone");
1766}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
Takashi Iwai352f7f92012-12-19 12:52:06 +01001768static int create_speaker_out_ctls(struct hda_codec *codec)
1769{
1770 struct hda_gen_spec *spec = codec->spec;
1771 return create_extra_outs(codec, spec->autocfg.speaker_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001772 spec->speaker_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001773 "Speaker");
1774}
1775
1776/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001777 * independent HP controls
1778 */
1779
1780static int indep_hp_info(struct snd_kcontrol *kcontrol,
1781 struct snd_ctl_elem_info *uinfo)
1782{
1783 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
1784}
1785
1786static int indep_hp_get(struct snd_kcontrol *kcontrol,
1787 struct snd_ctl_elem_value *ucontrol)
1788{
1789 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1790 struct hda_gen_spec *spec = codec->spec;
1791 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
1792 return 0;
1793}
1794
1795static int indep_hp_put(struct snd_kcontrol *kcontrol,
1796 struct snd_ctl_elem_value *ucontrol)
1797{
1798 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1799 struct hda_gen_spec *spec = codec->spec;
1800 unsigned int select = ucontrol->value.enumerated.item[0];
1801 int ret = 0;
1802
1803 mutex_lock(&spec->pcm_mutex);
1804 if (spec->active_streams) {
1805 ret = -EBUSY;
1806 goto unlock;
1807 }
1808
1809 if (spec->indep_hp_enabled != select) {
1810 spec->indep_hp_enabled = select;
1811 if (spec->indep_hp_enabled)
1812 spec->multiout.hp_out_nid[0] = 0;
1813 else
1814 spec->multiout.hp_out_nid[0] = spec->alt_dac_nid;
1815 ret = 1;
1816 }
1817 unlock:
1818 mutex_unlock(&spec->pcm_mutex);
1819 return ret;
1820}
1821
1822static const struct snd_kcontrol_new indep_hp_ctl = {
1823 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1824 .name = "Independent HP",
1825 .info = indep_hp_info,
1826 .get = indep_hp_get,
1827 .put = indep_hp_put,
1828};
1829
1830
1831static int create_indep_hp_ctls(struct hda_codec *codec)
1832{
1833 struct hda_gen_spec *spec = codec->spec;
1834
1835 if (!spec->indep_hp)
1836 return 0;
1837 if (!spec->multiout.hp_out_nid[0]) {
1838 spec->indep_hp = 0;
1839 return 0;
1840 }
1841
1842 spec->indep_hp_enabled = false;
1843 spec->alt_dac_nid = spec->multiout.hp_out_nid[0];
1844 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
1845 return -ENOMEM;
1846 return 0;
1847}
1848
1849/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01001850 * channel mode enum control
1851 */
1852
1853static int ch_mode_info(struct snd_kcontrol *kcontrol,
1854 struct snd_ctl_elem_info *uinfo)
1855{
1856 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1857 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001858 int chs;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001859
1860 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1861 uinfo->count = 1;
1862 uinfo->value.enumerated.items = spec->multi_ios + 1;
1863 if (uinfo->value.enumerated.item > spec->multi_ios)
1864 uinfo->value.enumerated.item = spec->multi_ios;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001865 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
1866 sprintf(uinfo->value.enumerated.name, "%dch", chs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001867 return 0;
1868}
1869
1870static int ch_mode_get(struct snd_kcontrol *kcontrol,
1871 struct snd_ctl_elem_value *ucontrol)
1872{
1873 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1874 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001875 ucontrol->value.enumerated.item[0] =
1876 (spec->ext_channel_count - spec->min_channel_count) / 2;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001877 return 0;
1878}
1879
Takashi Iwai196c17662013-01-04 15:01:40 +01001880static inline struct nid_path *
1881get_multiio_path(struct hda_codec *codec, int idx)
1882{
1883 struct hda_gen_spec *spec = codec->spec;
1884 return snd_hda_get_path_from_idx(codec,
1885 spec->out_paths[spec->autocfg.line_outs + idx]);
1886}
1887
Takashi Iwaia5cc2502013-01-16 18:08:55 +01001888static void update_automute_all(struct hda_codec *codec);
1889
Takashi Iwai352f7f92012-12-19 12:52:06 +01001890static int set_multi_io(struct hda_codec *codec, int idx, bool output)
1891{
1892 struct hda_gen_spec *spec = codec->spec;
1893 hda_nid_t nid = spec->multi_io[idx].pin;
1894 struct nid_path *path;
1895
Takashi Iwai196c17662013-01-04 15:01:40 +01001896 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001897 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001899
1900 if (path->active == output)
1901 return 0;
1902
1903 if (output) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01001904 set_pin_target(codec, nid, PIN_OUT, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001905 snd_hda_activate_path(codec, path, true, true);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01001906 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001907 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01001908 set_pin_eapd(codec, nid, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001909 snd_hda_activate_path(codec, path, false, true);
Takashi Iwai2c12c302013-01-10 09:33:29 +01001910 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 }
Takashi Iwaia365fed2013-01-10 16:10:06 +01001912
1913 /* update jack retasking in case it modifies any of them */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01001914 update_automute_all(codec);
Takashi Iwaia365fed2013-01-10 16:10:06 +01001915
Takashi Iwai352f7f92012-12-19 12:52:06 +01001916 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917}
1918
Takashi Iwai352f7f92012-12-19 12:52:06 +01001919static int ch_mode_put(struct snd_kcontrol *kcontrol,
1920 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001922 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1923 struct hda_gen_spec *spec = codec->spec;
1924 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
Takashi Iwai352f7f92012-12-19 12:52:06 +01001926 ch = ucontrol->value.enumerated.item[0];
1927 if (ch < 0 || ch > spec->multi_ios)
1928 return -EINVAL;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001929 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001930 return 0;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001931 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001932 for (i = 0; i < spec->multi_ios; i++)
1933 set_multi_io(codec, i, i < ch);
1934 spec->multiout.max_channels = max(spec->ext_channel_count,
1935 spec->const_channel_count);
1936 if (spec->need_dac_fix)
1937 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 return 1;
1939}
1940
Takashi Iwai352f7f92012-12-19 12:52:06 +01001941static const struct snd_kcontrol_new channel_mode_enum = {
1942 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1943 .name = "Channel Mode",
1944 .info = ch_mode_info,
1945 .get = ch_mode_get,
1946 .put = ch_mode_put,
1947};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
Takashi Iwai352f7f92012-12-19 12:52:06 +01001949static int create_multi_channel_mode(struct hda_codec *codec)
1950{
1951 struct hda_gen_spec *spec = codec->spec;
1952
1953 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01001954 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01001955 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 return 0;
1958}
1959
Takashi Iwai352f7f92012-12-19 12:52:06 +01001960/*
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001961 * aamix loopback enable/disable switch
1962 */
1963
1964#define loopback_mixing_info indep_hp_info
1965
1966static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
1967 struct snd_ctl_elem_value *ucontrol)
1968{
1969 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1970 struct hda_gen_spec *spec = codec->spec;
1971 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
1972 return 0;
1973}
1974
1975static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
1976 int nomix_path_idx, int mix_path_idx)
1977{
1978 struct nid_path *nomix_path, *mix_path;
1979
1980 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
1981 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
1982 if (!nomix_path || !mix_path)
1983 return;
1984 if (do_mix) {
1985 snd_hda_activate_path(codec, nomix_path, false, true);
1986 snd_hda_activate_path(codec, mix_path, true, true);
1987 } else {
1988 snd_hda_activate_path(codec, mix_path, false, true);
1989 snd_hda_activate_path(codec, nomix_path, true, true);
1990 }
1991}
1992
1993static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
1994 struct snd_ctl_elem_value *ucontrol)
1995{
1996 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1997 struct hda_gen_spec *spec = codec->spec;
1998 unsigned int val = ucontrol->value.enumerated.item[0];
1999
2000 if (val == spec->aamix_mode)
2001 return 0;
2002 spec->aamix_mode = val;
2003 update_aamix_paths(codec, val, spec->out_paths[0],
2004 spec->aamix_out_paths[0]);
2005 update_aamix_paths(codec, val, spec->hp_paths[0],
2006 spec->aamix_out_paths[1]);
2007 update_aamix_paths(codec, val, spec->speaker_paths[0],
2008 spec->aamix_out_paths[2]);
2009 return 1;
2010}
2011
2012static const struct snd_kcontrol_new loopback_mixing_enum = {
2013 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2014 .name = "Loopback Mixing",
2015 .info = loopback_mixing_info,
2016 .get = loopback_mixing_get,
2017 .put = loopback_mixing_put,
2018};
2019
2020static int create_loopback_mixing_ctl(struct hda_codec *codec)
2021{
2022 struct hda_gen_spec *spec = codec->spec;
2023
2024 if (!spec->mixer_nid)
2025 return 0;
2026 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
2027 spec->aamix_out_paths[2]))
2028 return 0;
2029 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2030 return -ENOMEM;
2031 return 0;
2032}
2033
2034/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002035 * shared headphone/mic handling
2036 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002037
Takashi Iwai352f7f92012-12-19 12:52:06 +01002038static void call_update_outputs(struct hda_codec *codec);
2039
2040/* for shared I/O, change the pin-control accordingly */
2041static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic)
2042{
2043 struct hda_gen_spec *spec = codec->spec;
2044 unsigned int val;
2045 hda_nid_t pin = spec->autocfg.inputs[1].pin;
2046 /* NOTE: this assumes that there are only two inputs, the
2047 * first is the real internal mic and the second is HP/mic jack.
2048 */
2049
2050 val = snd_hda_get_default_vref(codec, pin);
2051
2052 /* This pin does not have vref caps - let's enable vref on pin 0x18
2053 instead, as suggested by Realtek */
2054 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2055 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2056 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2057 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01002058 snd_hda_set_pin_ctl_cache(codec, vref_pin,
2059 PIN_IN | (set_as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02002060 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002061
2062 val = set_as_mic ? val | PIN_IN : PIN_HP;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002063 set_pin_target(codec, pin, val, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002064
2065 spec->automute_speaker = !set_as_mic;
2066 call_update_outputs(codec);
2067}
2068
2069/* create a shared input with the headphone out */
2070static int create_shared_input(struct hda_codec *codec)
2071{
2072 struct hda_gen_spec *spec = codec->spec;
2073 struct auto_pin_cfg *cfg = &spec->autocfg;
2074 unsigned int defcfg;
2075 hda_nid_t nid;
2076
2077 /* only one internal input pin? */
2078 if (cfg->num_inputs != 1)
2079 return 0;
2080 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2081 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2082 return 0;
2083
2084 if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
2085 nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
2086 else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
2087 nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
2088 else
2089 return 0; /* both not available */
2090
2091 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2092 return 0; /* no input */
2093
2094 cfg->inputs[1].pin = nid;
2095 cfg->inputs[1].type = AUTO_PIN_MIC;
2096 cfg->num_inputs = 2;
2097 spec->shared_mic_hp = 1;
2098 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
2099 return 0;
2100}
2101
Takashi Iwai978e77e2013-01-10 16:57:58 +01002102/*
2103 * output jack mode
2104 */
2105static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2106 struct snd_ctl_elem_info *uinfo)
2107{
2108 static const char * const texts[] = {
2109 "Line Out", "Headphone Out",
2110 };
2111 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, texts);
2112}
2113
2114static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2115 struct snd_ctl_elem_value *ucontrol)
2116{
2117 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2118 hda_nid_t nid = kcontrol->private_value;
2119 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2120 ucontrol->value.enumerated.item[0] = 1;
2121 else
2122 ucontrol->value.enumerated.item[0] = 0;
2123 return 0;
2124}
2125
2126static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2127 struct snd_ctl_elem_value *ucontrol)
2128{
2129 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2130 hda_nid_t nid = kcontrol->private_value;
2131 unsigned int val;
2132
2133 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2134 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2135 return 0;
2136 snd_hda_set_pin_ctl_cache(codec, nid, val);
2137 return 1;
2138}
2139
2140static const struct snd_kcontrol_new out_jack_mode_enum = {
2141 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2142 .info = out_jack_mode_info,
2143 .get = out_jack_mode_get,
2144 .put = out_jack_mode_put,
2145};
2146
2147static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2148{
2149 struct hda_gen_spec *spec = codec->spec;
2150 int i;
2151
2152 for (i = 0; i < spec->kctls.used; i++) {
2153 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2154 if (!strcmp(kctl->name, name) && kctl->index == idx)
2155 return true;
2156 }
2157 return false;
2158}
2159
2160static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2161 char *name, size_t name_len)
2162{
2163 struct hda_gen_spec *spec = codec->spec;
2164 int idx = 0;
2165
2166 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2167 strlcat(name, " Jack Mode", name_len);
2168
2169 for (; find_kctl_name(codec, name, idx); idx++)
2170 ;
2171}
2172
2173static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2174 hda_nid_t *pins)
2175{
2176 struct hda_gen_spec *spec = codec->spec;
2177 int i;
2178
2179 for (i = 0; i < num_pins; i++) {
2180 hda_nid_t pin = pins[i];
2181 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2182 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV)) {
2183 struct snd_kcontrol_new *knew;
2184 char name[44];
2185 get_jack_mode_name(codec, pin, name, sizeof(name));
2186 knew = snd_hda_gen_add_kctl(spec, name,
2187 &out_jack_mode_enum);
2188 if (!knew)
2189 return -ENOMEM;
2190 knew->private_value = pin;
2191 }
2192 }
2193
2194 return 0;
2195}
2196
Takashi Iwai294765582013-01-17 09:52:11 +01002197/*
2198 * input jack mode
2199 */
2200
2201/* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2202#define NUM_VREFS 6
2203
2204static const char * const vref_texts[NUM_VREFS] = {
2205 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2206 "", "Mic 80pc Bias", "Mic 100pc Bias"
2207};
2208
2209static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2210{
2211 unsigned int pincap;
2212
2213 pincap = snd_hda_query_pin_caps(codec, pin);
2214 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2215 /* filter out unusual vrefs */
2216 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2217 return pincap;
2218}
2219
2220/* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2221static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2222{
2223 unsigned int i, n = 0;
2224
2225 for (i = 0; i < NUM_VREFS; i++) {
2226 if (vref_caps & (1 << i)) {
2227 if (n == item_idx)
2228 return i;
2229 n++;
2230 }
2231 }
2232 return 0;
2233}
2234
2235/* convert back from the vref ctl index to the enum item index */
2236static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2237{
2238 unsigned int i, n = 0;
2239
2240 for (i = 0; i < NUM_VREFS; i++) {
2241 if (i == idx)
2242 return n;
2243 if (vref_caps & (1 << i))
2244 n++;
2245 }
2246 return 0;
2247}
2248
2249static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2250 struct snd_ctl_elem_info *uinfo)
2251{
2252 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2253 hda_nid_t nid = kcontrol->private_value;
2254 unsigned int vref_caps = get_vref_caps(codec, nid);
2255
2256 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2257 vref_texts);
2258 /* set the right text */
2259 strcpy(uinfo->value.enumerated.name,
2260 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2261 return 0;
2262}
2263
2264static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2265 struct snd_ctl_elem_value *ucontrol)
2266{
2267 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2268 hda_nid_t nid = kcontrol->private_value;
2269 unsigned int vref_caps = get_vref_caps(codec, nid);
2270 unsigned int idx;
2271
2272 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2273 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2274 return 0;
2275}
2276
2277static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2278 struct snd_ctl_elem_value *ucontrol)
2279{
2280 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2281 hda_nid_t nid = kcontrol->private_value;
2282 unsigned int vref_caps = get_vref_caps(codec, nid);
2283 unsigned int val, idx;
2284
2285 val = snd_hda_codec_get_pin_target(codec, nid);
2286 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2287 if (idx == ucontrol->value.enumerated.item[0])
2288 return 0;
2289
2290 val &= ~AC_PINCTL_VREFEN;
2291 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2292 snd_hda_set_pin_ctl_cache(codec, nid, val);
2293 return 1;
2294}
2295
2296static const struct snd_kcontrol_new in_jack_mode_enum = {
2297 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2298 .info = in_jack_mode_info,
2299 .get = in_jack_mode_get,
2300 .put = in_jack_mode_put,
2301};
2302
2303static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2304{
2305 struct hda_gen_spec *spec = codec->spec;
2306 unsigned int defcfg;
2307 struct snd_kcontrol_new *knew;
2308 char name[44];
2309
2310 /* no jack mode for fixed pins */
2311 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2312 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2313 return 0;
2314
2315 /* no multiple vref caps? */
2316 if (hweight32(get_vref_caps(codec, pin)) <= 1)
2317 return 0;
2318
2319 get_jack_mode_name(codec, pin, name, sizeof(name));
2320 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2321 if (!knew)
2322 return -ENOMEM;
2323 knew->private_value = pin;
2324 return 0;
2325}
2326
Takashi Iwai352f7f92012-12-19 12:52:06 +01002327
2328/*
2329 * Parse input paths
2330 */
2331
2332#ifdef CONFIG_PM
2333/* add the powersave loopback-list entry */
2334static void add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
2335{
2336 struct hda_amp_list *list;
2337
2338 if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
2339 return;
2340 list = spec->loopback_list + spec->num_loopbacks;
2341 list->nid = mix;
2342 list->dir = HDA_INPUT;
2343 list->idx = idx;
2344 spec->num_loopbacks++;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002345 spec->loopback.amplist = spec->loopback_list;
2346}
2347#else
Takashi Iwai352f7f92012-12-19 12:52:06 +01002348#define add_loopback_list(spec, mix, idx) /* NOP */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002349#endif
2350
Takashi Iwai352f7f92012-12-19 12:52:06 +01002351/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01002352static int new_analog_input(struct hda_codec *codec, int input_idx,
2353 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002354 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002356 struct hda_gen_spec *spec = codec->spec;
2357 struct nid_path *path;
2358 unsigned int val;
2359 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
Takashi Iwai352f7f92012-12-19 12:52:06 +01002361 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
2362 !nid_has_mute(codec, mix_nid, HDA_INPUT))
2363 return 0; /* no need for analog loopback */
2364
Takashi Iwai3ca529d2013-01-07 17:25:08 +01002365 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002366 if (!path)
2367 return -EINVAL;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002368 print_nid_path("loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01002369 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002370
2371 idx = path->idx[path->depth - 1];
2372 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
2373 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2374 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002375 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002377 path->ctls[NID_PATH_VOL_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 }
2379
Takashi Iwai352f7f92012-12-19 12:52:06 +01002380 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
2381 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2382 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002383 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002385 path->ctls[NID_PATH_MUTE_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 }
2387
Takashi Iwai352f7f92012-12-19 12:52:06 +01002388 path->active = true;
2389 add_loopback_list(spec, mix_nid, idx);
2390 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391}
2392
Takashi Iwai352f7f92012-12-19 12:52:06 +01002393static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002395 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
2396 return (pincap & AC_PINCAP_IN) != 0;
2397}
2398
2399/* Parse the codec tree and retrieve ADCs */
2400static int fill_adc_nids(struct hda_codec *codec)
2401{
2402 struct hda_gen_spec *spec = codec->spec;
2403 hda_nid_t nid;
2404 hda_nid_t *adc_nids = spec->adc_nids;
2405 int max_nums = ARRAY_SIZE(spec->adc_nids);
2406 int i, nums = 0;
2407
2408 nid = codec->start_nid;
2409 for (i = 0; i < codec->num_nodes; i++, nid++) {
2410 unsigned int caps = get_wcaps(codec, nid);
2411 int type = get_wcaps_type(caps);
2412
2413 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2414 continue;
2415 adc_nids[nums] = nid;
2416 if (++nums >= max_nums)
2417 break;
2418 }
2419 spec->num_adc_nids = nums;
Takashi Iwai0ffd5342013-01-17 15:53:29 +01002420
2421 /* copy the detected ADCs to all_adcs[] */
2422 spec->num_all_adcs = nums;
2423 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
2424
Takashi Iwai352f7f92012-12-19 12:52:06 +01002425 return nums;
2426}
2427
2428/* filter out invalid adc_nids that don't give all active input pins;
2429 * if needed, check whether dynamic ADC-switching is available
2430 */
2431static int check_dyn_adc_switch(struct hda_codec *codec)
2432{
2433 struct hda_gen_spec *spec = codec->spec;
2434 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002435 unsigned int ok_bits;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002436 int i, n, nums;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002437
2438 again:
2439 nums = 0;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002440 ok_bits = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002441 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002442 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002443 if (!spec->input_paths[i][n])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002444 break;
2445 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002446 if (i >= imux->num_items) {
2447 ok_bits |= (1 << n);
2448 nums++;
2449 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002450 }
2451
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002452 if (!ok_bits) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002453 if (spec->shared_mic_hp) {
2454 spec->shared_mic_hp = 0;
2455 imux->num_items = 1;
2456 goto again;
2457 }
2458
2459 /* check whether ADC-switch is possible */
2460 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002461 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002462 if (spec->input_paths[i][n]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002463 spec->dyn_adc_idx[i] = n;
2464 break;
2465 }
2466 }
2467 }
2468
2469 snd_printdd("hda-codec: enabling ADC switching\n");
2470 spec->dyn_adc_switch = 1;
2471 } else if (nums != spec->num_adc_nids) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002472 /* shrink the invalid adcs and input paths */
2473 nums = 0;
2474 for (n = 0; n < spec->num_adc_nids; n++) {
2475 if (!(ok_bits & (1 << n)))
2476 continue;
2477 if (n != nums) {
2478 spec->adc_nids[nums] = spec->adc_nids[n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002479 for (i = 0; i < imux->num_items; i++) {
2480 invalidate_nid_path(codec,
2481 spec->input_paths[i][nums]);
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002482 spec->input_paths[i][nums] =
2483 spec->input_paths[i][n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002484 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002485 }
2486 nums++;
2487 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002488 spec->num_adc_nids = nums;
2489 }
2490
2491 if (imux->num_items == 1 || spec->shared_mic_hp) {
2492 snd_printdd("hda-codec: reducing to a single ADC\n");
2493 spec->num_adc_nids = 1; /* reduce to a single ADC */
2494 }
2495
2496 /* single index for individual volumes ctls */
2497 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
2498 spec->num_adc_nids = 1;
2499
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 return 0;
2501}
2502
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002503/* parse capture source paths from the given pin and create imux items */
2504static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
Takashi Iwai9dba2052013-01-18 10:01:15 +01002505 int cfg_idx, int num_adcs,
2506 const char *label, int anchor)
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002507{
2508 struct hda_gen_spec *spec = codec->spec;
2509 struct hda_input_mux *imux = &spec->input_mux;
2510 int imux_idx = imux->num_items;
2511 bool imux_added = false;
2512 int c;
2513
2514 for (c = 0; c < num_adcs; c++) {
2515 struct nid_path *path;
2516 hda_nid_t adc = spec->adc_nids[c];
2517
2518 if (!is_reachable_path(codec, pin, adc))
2519 continue;
2520 path = snd_hda_add_new_path(codec, pin, adc, anchor);
2521 if (!path)
2522 continue;
2523 print_nid_path("input", path);
2524 spec->input_paths[imux_idx][c] =
2525 snd_hda_get_path_idx(codec, path);
2526
2527 if (!imux_added) {
2528 spec->imux_pins[imux->num_items] = pin;
Takashi Iwai9dba2052013-01-18 10:01:15 +01002529 snd_hda_add_imux_item(imux, label, cfg_idx, NULL);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002530 imux_added = true;
2531 }
2532 }
2533
2534 return 0;
2535}
2536
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002538 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 */
Takashi Iwai9dba2052013-01-18 10:01:15 +01002540
Takashi Iwaic9700422013-01-18 10:17:30 +01002541/* fill the label for each input at first */
2542static int fill_input_pin_labels(struct hda_codec *codec)
2543{
2544 struct hda_gen_spec *spec = codec->spec;
2545 const struct auto_pin_cfg *cfg = &spec->autocfg;
2546 int i;
2547
2548 for (i = 0; i < cfg->num_inputs; i++) {
2549 hda_nid_t pin = cfg->inputs[i].pin;
2550 const char *label;
2551 int j, idx;
2552
2553 if (!is_input_pin(codec, pin))
2554 continue;
2555
2556 label = hda_get_autocfg_input_label(codec, cfg, i);
2557 idx = 0;
2558 for (j = i; j >= 0; j--) {
2559 if (spec->input_labels[j] &&
2560 !strcmp(spec->input_labels[j], label)) {
2561 idx = spec->input_label_idxs[j] + 1;
2562 break;
2563 }
2564 }
2565
2566 spec->input_labels[i] = label;
2567 spec->input_label_idxs[i] = idx;
2568 }
2569
2570 return 0;
2571}
2572
Takashi Iwai9dba2052013-01-18 10:01:15 +01002573#define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
2574
Takashi Iwai352f7f92012-12-19 12:52:06 +01002575static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002576{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002577 struct hda_gen_spec *spec = codec->spec;
2578 const struct auto_pin_cfg *cfg = &spec->autocfg;
2579 hda_nid_t mixer = spec->mixer_nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002580 int num_adcs;
Takashi Iwaic9700422013-01-18 10:17:30 +01002581 int i, err;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002582 unsigned int val;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002583
Takashi Iwai352f7f92012-12-19 12:52:06 +01002584 num_adcs = fill_adc_nids(codec);
2585 if (num_adcs < 0)
2586 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002587
Takashi Iwaic9700422013-01-18 10:17:30 +01002588 err = fill_input_pin_labels(codec);
2589 if (err < 0)
2590 return err;
2591
Takashi Iwai352f7f92012-12-19 12:52:06 +01002592 for (i = 0; i < cfg->num_inputs; i++) {
2593 hda_nid_t pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594
Takashi Iwai352f7f92012-12-19 12:52:06 +01002595 pin = cfg->inputs[i].pin;
2596 if (!is_input_pin(codec, pin))
2597 continue;
2598
Takashi Iwai2c12c302013-01-10 09:33:29 +01002599 val = PIN_IN;
2600 if (cfg->inputs[i].type == AUTO_PIN_MIC)
2601 val |= snd_hda_get_default_vref(codec, pin);
2602 set_pin_target(codec, pin, val, false);
2603
Takashi Iwai352f7f92012-12-19 12:52:06 +01002604 if (mixer) {
2605 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01002606 err = new_analog_input(codec, i, pin,
Takashi Iwaic9700422013-01-18 10:17:30 +01002607 spec->input_labels[i],
2608 spec->input_label_idxs[i],
2609 mixer);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002610 if (err < 0)
2611 return err;
2612 }
2613 }
2614
Takashi Iwaic9700422013-01-18 10:17:30 +01002615 err = parse_capture_source(codec, pin, i, num_adcs,
2616 spec->input_labels[i], -mixer);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002617 if (err < 0)
2618 return err;
Takashi Iwai294765582013-01-17 09:52:11 +01002619
2620 if (spec->add_in_jack_modes) {
2621 err = create_in_jack_mode(codec, pin);
2622 if (err < 0)
2623 return err;
2624 }
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002625 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002626
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002627 if (mixer && spec->add_stereo_mix_input) {
Takashi Iwai9dba2052013-01-18 10:01:15 +01002628 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002629 "Stereo Mix", 0);
2630 if (err < 0)
2631 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002632 }
2633
2634 return 0;
2635}
2636
2637
2638/*
2639 * input source mux
2640 */
2641
Takashi Iwaic697b712013-01-07 17:09:26 +01002642/* get the input path specified by the given adc and imux indices */
2643static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002644{
2645 struct hda_gen_spec *spec = codec->spec;
David Henningssonb56fa1e2013-01-16 11:45:35 +01002646 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
2647 snd_BUG();
2648 return NULL;
2649 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002650 if (spec->dyn_adc_switch)
2651 adc_idx = spec->dyn_adc_idx[imux_idx];
David Henningssonb56fa1e2013-01-16 11:45:35 +01002652 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_OUTS) {
2653 snd_BUG();
2654 return NULL;
2655 }
Takashi Iwaic697b712013-01-07 17:09:26 +01002656 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002657}
2658
2659static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2660 unsigned int idx);
2661
2662static int mux_enum_info(struct snd_kcontrol *kcontrol,
2663 struct snd_ctl_elem_info *uinfo)
2664{
2665 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2666 struct hda_gen_spec *spec = codec->spec;
2667 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
2668}
2669
2670static int mux_enum_get(struct snd_kcontrol *kcontrol,
2671 struct snd_ctl_elem_value *ucontrol)
2672{
2673 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2674 struct hda_gen_spec *spec = codec->spec;
David Henningssona053d1e2013-01-16 11:45:36 +01002675 unsigned int adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002676
2677 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
2678 return 0;
2679}
2680
2681static int mux_enum_put(struct snd_kcontrol *kcontrol,
2682 struct snd_ctl_elem_value *ucontrol)
2683{
2684 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
David Henningssona053d1e2013-01-16 11:45:36 +01002685 unsigned int adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002686 return mux_select(codec, adc_idx,
2687 ucontrol->value.enumerated.item[0]);
2688}
2689
Takashi Iwai352f7f92012-12-19 12:52:06 +01002690static const struct snd_kcontrol_new cap_src_temp = {
2691 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2692 .name = "Input Source",
2693 .info = mux_enum_info,
2694 .get = mux_enum_get,
2695 .put = mux_enum_put,
2696};
2697
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002698/*
2699 * capture volume and capture switch ctls
2700 */
2701
Takashi Iwai352f7f92012-12-19 12:52:06 +01002702typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
2703 struct snd_ctl_elem_value *ucontrol);
2704
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002705/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002706static int cap_put_caller(struct snd_kcontrol *kcontrol,
2707 struct snd_ctl_elem_value *ucontrol,
2708 put_call_t func, int type)
2709{
2710 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2711 struct hda_gen_spec *spec = codec->spec;
2712 const struct hda_input_mux *imux;
2713 struct nid_path *path;
2714 int i, adc_idx, err = 0;
2715
2716 imux = &spec->input_mux;
David Henningssona053d1e2013-01-16 11:45:36 +01002717 adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002718 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002719 /* we use the cache-only update at first since multiple input paths
2720 * may shared the same amp; by updating only caches, the redundant
2721 * writes to hardware can be reduced.
2722 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002723 codec->cached_write = 1;
2724 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01002725 path = get_input_path(codec, adc_idx, i);
2726 if (!path || !path->ctls[type])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002727 continue;
2728 kcontrol->private_value = path->ctls[type];
2729 err = func(kcontrol, ucontrol);
2730 if (err < 0)
2731 goto error;
2732 }
2733 error:
2734 codec->cached_write = 0;
2735 mutex_unlock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002736 snd_hda_codec_flush_amp_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002737 if (err >= 0 && spec->cap_sync_hook)
2738 spec->cap_sync_hook(codec);
2739 return err;
2740}
2741
2742/* capture volume ctl callbacks */
2743#define cap_vol_info snd_hda_mixer_amp_volume_info
2744#define cap_vol_get snd_hda_mixer_amp_volume_get
2745#define cap_vol_tlv snd_hda_mixer_amp_tlv
2746
2747static int cap_vol_put(struct snd_kcontrol *kcontrol,
2748 struct snd_ctl_elem_value *ucontrol)
2749{
2750 return cap_put_caller(kcontrol, ucontrol,
2751 snd_hda_mixer_amp_volume_put,
2752 NID_PATH_VOL_CTL);
2753}
2754
2755static const struct snd_kcontrol_new cap_vol_temp = {
2756 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2757 .name = "Capture Volume",
2758 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
2759 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2760 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
2761 .info = cap_vol_info,
2762 .get = cap_vol_get,
2763 .put = cap_vol_put,
2764 .tlv = { .c = cap_vol_tlv },
2765};
2766
2767/* capture switch ctl callbacks */
2768#define cap_sw_info snd_ctl_boolean_stereo_info
2769#define cap_sw_get snd_hda_mixer_amp_switch_get
2770
2771static int cap_sw_put(struct snd_kcontrol *kcontrol,
2772 struct snd_ctl_elem_value *ucontrol)
2773{
Takashi Iwaiae177c32013-01-14 12:13:06 +01002774 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2775 struct hda_gen_spec *spec = codec->spec;
2776 int ret;
2777
2778 ret = cap_put_caller(kcontrol, ucontrol,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002779 snd_hda_mixer_amp_switch_put,
2780 NID_PATH_MUTE_CTL);
Takashi Iwaiae177c32013-01-14 12:13:06 +01002781 if (ret < 0)
2782 return ret;
2783
2784 if (spec->capture_switch_hook) {
2785 bool enable = (ucontrol->value.integer.value[0] ||
2786 ucontrol->value.integer.value[1]);
2787 spec->capture_switch_hook(codec, enable);
2788 }
2789
2790 return ret;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002791}
2792
2793static const struct snd_kcontrol_new cap_sw_temp = {
2794 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2795 .name = "Capture Switch",
2796 .info = cap_sw_info,
2797 .get = cap_sw_get,
2798 .put = cap_sw_put,
2799};
2800
2801static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
2802{
2803 hda_nid_t nid;
2804 int i, depth;
2805
2806 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
2807 for (depth = 0; depth < 3; depth++) {
2808 if (depth >= path->depth)
2809 return -EINVAL;
2810 i = path->depth - depth - 1;
2811 nid = path->path[i];
2812 if (!path->ctls[NID_PATH_VOL_CTL]) {
2813 if (nid_has_volume(codec, nid, HDA_OUTPUT))
2814 path->ctls[NID_PATH_VOL_CTL] =
2815 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2816 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
2817 int idx = path->idx[i];
2818 if (!depth && codec->single_adc_amp)
2819 idx = 0;
2820 path->ctls[NID_PATH_VOL_CTL] =
2821 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2822 }
2823 }
2824 if (!path->ctls[NID_PATH_MUTE_CTL]) {
2825 if (nid_has_mute(codec, nid, HDA_OUTPUT))
2826 path->ctls[NID_PATH_MUTE_CTL] =
2827 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2828 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
2829 int idx = path->idx[i];
2830 if (!depth && codec->single_adc_amp)
2831 idx = 0;
2832 path->ctls[NID_PATH_MUTE_CTL] =
2833 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2834 }
2835 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01002836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 return 0;
2838}
2839
Takashi Iwai352f7f92012-12-19 12:52:06 +01002840static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002842 struct hda_gen_spec *spec = codec->spec;
2843 struct auto_pin_cfg *cfg = &spec->autocfg;
2844 unsigned int val;
2845 int i;
2846
2847 if (!spec->inv_dmic_split)
2848 return false;
2849 for (i = 0; i < cfg->num_inputs; i++) {
2850 if (cfg->inputs[i].pin != nid)
2851 continue;
2852 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2853 return false;
2854 val = snd_hda_codec_get_pincfg(codec, nid);
2855 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
2856 }
2857 return false;
2858}
2859
2860static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
2861 int idx, bool is_switch, unsigned int ctl,
2862 bool inv_dmic)
2863{
2864 struct hda_gen_spec *spec = codec->spec;
2865 char tmpname[44];
2866 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
2867 const char *sfx = is_switch ? "Switch" : "Volume";
2868 unsigned int chs = inv_dmic ? 1 : 3;
2869 int err;
2870
2871 if (!ctl)
2872 return 0;
2873
2874 if (label)
2875 snprintf(tmpname, sizeof(tmpname),
2876 "%s Capture %s", label, sfx);
2877 else
2878 snprintf(tmpname, sizeof(tmpname),
2879 "Capture %s", sfx);
2880 err = add_control(spec, type, tmpname, idx,
2881 amp_val_replace_channels(ctl, chs));
2882 if (err < 0 || !inv_dmic)
2883 return err;
2884
2885 /* Make independent right kcontrol */
2886 if (label)
2887 snprintf(tmpname, sizeof(tmpname),
2888 "Inverted %s Capture %s", label, sfx);
2889 else
2890 snprintf(tmpname, sizeof(tmpname),
2891 "Inverted Capture %s", sfx);
2892 return add_control(spec, type, tmpname, idx,
2893 amp_val_replace_channels(ctl, 2));
2894}
2895
2896/* create single (and simple) capture volume and switch controls */
2897static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
2898 unsigned int vol_ctl, unsigned int sw_ctl,
2899 bool inv_dmic)
2900{
2901 int err;
2902 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
2903 if (err < 0)
2904 return err;
2905 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
2906 if (err < 0)
2907 return err;
2908 return 0;
2909}
2910
2911/* create bound capture volume and switch controls */
2912static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
2913 unsigned int vol_ctl, unsigned int sw_ctl)
2914{
2915 struct hda_gen_spec *spec = codec->spec;
2916 struct snd_kcontrol_new *knew;
2917
2918 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002919 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002920 if (!knew)
2921 return -ENOMEM;
2922 knew->index = idx;
2923 knew->private_value = vol_ctl;
2924 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2925 }
2926 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002927 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002928 if (!knew)
2929 return -ENOMEM;
2930 knew->index = idx;
2931 knew->private_value = sw_ctl;
2932 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2933 }
2934 return 0;
2935}
2936
2937/* return the vol ctl when used first in the imux list */
2938static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
2939{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002940 struct nid_path *path;
2941 unsigned int ctl;
2942 int i;
2943
Takashi Iwaic697b712013-01-07 17:09:26 +01002944 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002945 if (!path)
2946 return 0;
2947 ctl = path->ctls[type];
2948 if (!ctl)
2949 return 0;
2950 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01002951 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002952 if (path && path->ctls[type] == ctl)
2953 return 0;
2954 }
2955 return ctl;
2956}
2957
2958/* create individual capture volume and switch controls per input */
2959static int create_multi_cap_vol_ctl(struct hda_codec *codec)
2960{
2961 struct hda_gen_spec *spec = codec->spec;
2962 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaic9700422013-01-18 10:17:30 +01002963 int i, err, type;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002964
2965 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002966 bool inv_dmic;
Takashi Iwaic9700422013-01-18 10:17:30 +01002967 int idx;
Takashi Iwai9dba2052013-01-18 10:01:15 +01002968
Takashi Iwaic9700422013-01-18 10:17:30 +01002969 idx = imux->items[i].index;
2970 if (idx >= spec->autocfg.num_inputs)
Takashi Iwai9dba2052013-01-18 10:01:15 +01002971 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002972 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
2973
2974 for (type = 0; type < 2; type++) {
Takashi Iwaic9700422013-01-18 10:17:30 +01002975 err = add_single_cap_ctl(codec,
2976 spec->input_labels[idx],
2977 spec->input_label_idxs[idx],
2978 type,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002979 get_first_cap_ctl(codec, i, type),
2980 inv_dmic);
2981 if (err < 0)
2982 return err;
2983 }
2984 }
2985 return 0;
2986}
2987
2988static int create_capture_mixers(struct hda_codec *codec)
2989{
2990 struct hda_gen_spec *spec = codec->spec;
2991 struct hda_input_mux *imux = &spec->input_mux;
2992 int i, n, nums, err;
2993
2994 if (spec->dyn_adc_switch)
2995 nums = 1;
2996 else
2997 nums = spec->num_adc_nids;
2998
2999 if (!spec->auto_mic && imux->num_items > 1) {
3000 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01003001 const char *name;
3002 name = nums > 1 ? "Input Source" : "Capture Source";
3003 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003004 if (!knew)
3005 return -ENOMEM;
3006 knew->count = nums;
3007 }
3008
3009 for (n = 0; n < nums; n++) {
3010 bool multi = false;
David Henningsson99a55922013-01-16 15:58:44 +01003011 bool multi_cap_vol = spec->multi_cap_vol;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003012 bool inv_dmic = false;
3013 int vol, sw;
3014
3015 vol = sw = 0;
3016 for (i = 0; i < imux->num_items; i++) {
3017 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01003018 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003019 if (!path)
3020 continue;
3021 parse_capvol_in_path(codec, path);
3022 if (!vol)
3023 vol = path->ctls[NID_PATH_VOL_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003024 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003025 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003026 if (!same_amp_caps(codec, vol,
3027 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3028 multi_cap_vol = true;
3029 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003030 if (!sw)
3031 sw = path->ctls[NID_PATH_MUTE_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003032 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003033 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003034 if (!same_amp_caps(codec, sw,
3035 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3036 multi_cap_vol = true;
3037 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003038 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3039 inv_dmic = true;
3040 }
3041
3042 if (!multi)
3043 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3044 inv_dmic);
David Henningsson99a55922013-01-16 15:58:44 +01003045 else if (!multi_cap_vol)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003046 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3047 else
3048 err = create_multi_cap_vol_ctl(codec);
3049 if (err < 0)
3050 return err;
3051 }
3052
3053 return 0;
3054}
3055
3056/*
3057 * add mic boosts if needed
3058 */
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003059
3060/* check whether the given amp is feasible as a boost volume */
3061static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3062 int dir, int idx)
3063{
3064 unsigned int step;
3065
3066 if (!nid_has_volume(codec, nid, dir) ||
3067 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3068 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3069 return false;
3070
3071 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3072 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3073 if (step < 0x20)
3074 return false;
3075 return true;
3076}
3077
3078/* look for a boost amp in a widget close to the pin */
3079static unsigned int look_for_boost_amp(struct hda_codec *codec,
3080 struct nid_path *path)
3081{
3082 unsigned int val = 0;
3083 hda_nid_t nid;
3084 int depth;
3085
3086 for (depth = 0; depth < 3; depth++) {
3087 if (depth >= path->depth - 1)
3088 break;
3089 nid = path->path[depth];
3090 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3091 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3092 break;
3093 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3094 path->idx[depth])) {
3095 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3096 HDA_INPUT);
3097 break;
3098 }
3099 }
3100
3101 return val;
3102}
3103
Takashi Iwai352f7f92012-12-19 12:52:06 +01003104static int parse_mic_boost(struct hda_codec *codec)
3105{
3106 struct hda_gen_spec *spec = codec->spec;
3107 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003108 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003109 int i, err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003110
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003111 if (!spec->num_adc_nids)
3112 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003113
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003114 for (i = 0; i < imux->num_items; i++) {
3115 struct nid_path *path;
3116 unsigned int val;
3117 int idx;
3118 char boost_label[44];
David Henningsson02aba552013-01-16 15:58:43 +01003119
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003120 idx = imux->items[i].index;
3121 if (idx >= imux->num_items)
3122 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003123
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003124 /* check only line-in and mic pins */
3125 if (cfg->inputs[idx].type > AUTO_PIN_MIC)
3126 continue;
3127
3128 path = get_input_path(codec, 0, i);
3129 if (!path)
3130 continue;
3131
3132 val = look_for_boost_amp(codec, path);
3133 if (!val)
3134 continue;
3135
3136 /* create a boost control */
3137 snprintf(boost_label, sizeof(boost_label),
3138 "%s Boost Volume", spec->input_labels[idx]);
3139 err = add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3140 spec->input_label_idxs[idx], val);
3141 if (err < 0)
3142 return err;
3143
3144 path->ctls[NID_PATH_BOOST_CTL] = val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003145 }
3146 return 0;
3147}
3148
3149/*
3150 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3151 */
3152static void parse_digital(struct hda_codec *codec)
3153{
3154 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003155 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003156 int i, nums;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003157 hda_nid_t dig_nid, pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003158
3159 /* support multiple SPDIFs; the secondary is set up as a slave */
3160 nums = 0;
3161 for (i = 0; i < spec->autocfg.dig_outs; i++) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003162 pin = spec->autocfg.dig_out_pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003163 dig_nid = look_for_dac(codec, pin, true);
3164 if (!dig_nid)
3165 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003166 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003167 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003168 continue;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003169 print_nid_path("digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01003170 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01003171 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003172 set_pin_target(codec, pin, PIN_OUT, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003173 if (!nums) {
3174 spec->multiout.dig_out_nid = dig_nid;
3175 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3176 } else {
3177 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3178 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
3179 break;
3180 spec->slave_dig_outs[nums - 1] = dig_nid;
3181 }
3182 nums++;
3183 }
3184
3185 if (spec->autocfg.dig_in_pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003186 pin = spec->autocfg.dig_in_pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003187 dig_nid = codec->start_nid;
3188 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003189 unsigned int wcaps = get_wcaps(codec, dig_nid);
3190 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3191 continue;
3192 if (!(wcaps & AC_WCAP_DIGITAL))
3193 continue;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003194 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003195 if (path) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003196 print_nid_path("digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003197 path->active = true;
3198 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003199 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003200 set_pin_target(codec, pin, PIN_IN, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003201 break;
3202 }
3203 }
3204 }
3205}
3206
3207
3208/*
3209 * input MUX handling
3210 */
3211
3212static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3213
3214/* select the given imux item; either unmute exclusively or select the route */
3215static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3216 unsigned int idx)
3217{
3218 struct hda_gen_spec *spec = codec->spec;
3219 const struct hda_input_mux *imux;
3220 struct nid_path *path;
3221
3222 imux = &spec->input_mux;
3223 if (!imux->num_items)
3224 return 0;
3225
3226 if (idx >= imux->num_items)
3227 idx = imux->num_items - 1;
3228 if (spec->cur_mux[adc_idx] == idx)
3229 return 0;
3230
Takashi Iwaic697b712013-01-07 17:09:26 +01003231 path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003232 if (!path)
3233 return 0;
3234 if (path->active)
3235 snd_hda_activate_path(codec, path, false, false);
3236
3237 spec->cur_mux[adc_idx] = idx;
3238
3239 if (spec->shared_mic_hp)
3240 update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
3241
3242 if (spec->dyn_adc_switch)
3243 dyn_adc_pcm_resetup(codec, idx);
3244
Takashi Iwaic697b712013-01-07 17:09:26 +01003245 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003246 if (!path)
3247 return 0;
3248 if (path->active)
3249 return 0;
3250 snd_hda_activate_path(codec, path, true, false);
3251 if (spec->cap_sync_hook)
3252 spec->cap_sync_hook(codec);
3253 return 1;
3254}
3255
3256
3257/*
3258 * Jack detections for HP auto-mute and mic-switch
3259 */
3260
3261/* check each pin in the given array; returns true if any of them is plugged */
3262static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
3263{
3264 int i, present = 0;
3265
3266 for (i = 0; i < num_pins; i++) {
3267 hda_nid_t nid = pins[i];
3268 if (!nid)
3269 break;
Takashi Iwai0b4df932013-01-10 09:45:13 +01003270 /* don't detect pins retasked as inputs */
3271 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
3272 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003273 present |= snd_hda_jack_detect(codec, nid);
3274 }
3275 return present;
3276}
3277
3278/* standard HP/line-out auto-mute helper */
3279static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
Takashi Iwai2c12c302013-01-10 09:33:29 +01003280 bool mute)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003281{
3282 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003283 int i;
3284
3285 for (i = 0; i < num_pins; i++) {
3286 hda_nid_t nid = pins[i];
3287 unsigned int val;
3288 if (!nid)
3289 break;
3290 /* don't reset VREF value in case it's controlling
3291 * the amp (see alc861_fixup_asus_amp_vref_0f())
3292 */
Takashi Iwai2c12c302013-01-10 09:33:29 +01003293 if (spec->keep_vref_in_automute)
3294 val = snd_hda_codec_get_pin_target(codec, nid) & ~PIN_HP;
3295 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01003296 val = 0;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003297 if (!mute)
3298 val |= snd_hda_codec_get_pin_target(codec, nid);
3299 /* here we call update_pin_ctl() so that the pinctl is changed
3300 * without changing the pinctl target value;
3301 * the original target value will be still referred at the
3302 * init / resume again
3303 */
3304 update_pin_ctl(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01003305 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003306 }
3307}
3308
3309/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003310void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003311{
3312 struct hda_gen_spec *spec = codec->spec;
3313 int on;
3314
3315 /* Control HP pins/amps depending on master_mute state;
3316 * in general, HP pins/amps control should be enabled in all cases,
3317 * but currently set only for master_mute, just to be safe
3318 */
3319 if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
3320 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003321 spec->autocfg.hp_pins, spec->master_mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003322
3323 if (!spec->automute_speaker)
3324 on = 0;
3325 else
3326 on = spec->hp_jack_present | spec->line_jack_present;
3327 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003328 spec->speaker_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003329 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003330 spec->autocfg.speaker_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003331
3332 /* toggle line-out mutes if needed, too */
3333 /* if LO is a copy of either HP or Speaker, don't need to handle it */
3334 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
3335 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
3336 return;
3337 if (!spec->automute_lo)
3338 on = 0;
3339 else
3340 on = spec->hp_jack_present;
3341 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003342 spec->line_out_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003343 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003344 spec->autocfg.line_out_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003345}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003346EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003347
3348static void call_update_outputs(struct hda_codec *codec)
3349{
3350 struct hda_gen_spec *spec = codec->spec;
3351 if (spec->automute_hook)
3352 spec->automute_hook(codec);
3353 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01003354 snd_hda_gen_update_outputs(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003355}
3356
3357/* standard HP-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003358void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003359{
3360 struct hda_gen_spec *spec = codec->spec;
3361
3362 spec->hp_jack_present =
3363 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
3364 spec->autocfg.hp_pins);
3365 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
3366 return;
3367 call_update_outputs(codec);
3368}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003369EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003370
3371/* standard line-out-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003372void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003373{
3374 struct hda_gen_spec *spec = codec->spec;
3375
3376 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3377 return;
3378 /* check LO jack only when it's different from HP */
3379 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
3380 return;
3381
3382 spec->line_jack_present =
3383 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3384 spec->autocfg.line_out_pins);
3385 if (!spec->automute_speaker || !spec->detect_lo)
3386 return;
3387 call_update_outputs(codec);
3388}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003389EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003390
3391/* standard mic auto-switch helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003392void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003393{
3394 struct hda_gen_spec *spec = codec->spec;
3395 int i;
3396
3397 if (!spec->auto_mic)
3398 return;
3399
3400 for (i = spec->am_num_entries - 1; i > 0; i--) {
Takashi Iwai0b4df932013-01-10 09:45:13 +01003401 hda_nid_t pin = spec->am_entry[i].pin;
3402 /* don't detect pins retasked as outputs */
3403 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
3404 continue;
3405 if (snd_hda_jack_detect(codec, pin)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003406 mux_select(codec, 0, spec->am_entry[i].idx);
3407 return;
3408 }
3409 }
3410 mux_select(codec, 0, spec->am_entry[0].idx);
3411}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003412EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003413
Takashi Iwaia5cc2502013-01-16 18:08:55 +01003414/* update jack retasking */
3415static void update_automute_all(struct hda_codec *codec)
3416{
3417 struct hda_gen_spec *spec = codec->spec;
3418
3419 if (spec->hp_automute_hook)
3420 spec->hp_automute_hook(codec, NULL);
3421 else
3422 snd_hda_gen_hp_automute(codec, NULL);
3423 if (spec->line_automute_hook)
3424 spec->line_automute_hook(codec, NULL);
3425 else
3426 snd_hda_gen_line_automute(codec, NULL);
3427 if (spec->mic_autoswitch_hook)
3428 spec->mic_autoswitch_hook(codec, NULL);
3429 else
3430 snd_hda_gen_mic_autoswitch(codec, NULL);
3431}
3432
Takashi Iwai352f7f92012-12-19 12:52:06 +01003433/*
3434 * Auto-Mute mode mixer enum support
3435 */
3436static int automute_mode_info(struct snd_kcontrol *kcontrol,
3437 struct snd_ctl_elem_info *uinfo)
3438{
3439 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3440 struct hda_gen_spec *spec = codec->spec;
3441 static const char * const texts3[] = {
3442 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02003443 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444
Takashi Iwai352f7f92012-12-19 12:52:06 +01003445 if (spec->automute_speaker_possible && spec->automute_lo_possible)
3446 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
3447 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
3448}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449
Takashi Iwai352f7f92012-12-19 12:52:06 +01003450static int automute_mode_get(struct snd_kcontrol *kcontrol,
3451 struct snd_ctl_elem_value *ucontrol)
3452{
3453 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3454 struct hda_gen_spec *spec = codec->spec;
3455 unsigned int val = 0;
3456 if (spec->automute_speaker)
3457 val++;
3458 if (spec->automute_lo)
3459 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003460
Takashi Iwai352f7f92012-12-19 12:52:06 +01003461 ucontrol->value.enumerated.item[0] = val;
3462 return 0;
3463}
3464
3465static int automute_mode_put(struct snd_kcontrol *kcontrol,
3466 struct snd_ctl_elem_value *ucontrol)
3467{
3468 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3469 struct hda_gen_spec *spec = codec->spec;
3470
3471 switch (ucontrol->value.enumerated.item[0]) {
3472 case 0:
3473 if (!spec->automute_speaker && !spec->automute_lo)
3474 return 0;
3475 spec->automute_speaker = 0;
3476 spec->automute_lo = 0;
3477 break;
3478 case 1:
3479 if (spec->automute_speaker_possible) {
3480 if (!spec->automute_lo && spec->automute_speaker)
3481 return 0;
3482 spec->automute_speaker = 1;
3483 spec->automute_lo = 0;
3484 } else if (spec->automute_lo_possible) {
3485 if (spec->automute_lo)
3486 return 0;
3487 spec->automute_lo = 1;
3488 } else
3489 return -EINVAL;
3490 break;
3491 case 2:
3492 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
3493 return -EINVAL;
3494 if (spec->automute_speaker && spec->automute_lo)
3495 return 0;
3496 spec->automute_speaker = 1;
3497 spec->automute_lo = 1;
3498 break;
3499 default:
3500 return -EINVAL;
3501 }
3502 call_update_outputs(codec);
3503 return 1;
3504}
3505
3506static const struct snd_kcontrol_new automute_mode_enum = {
3507 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3508 .name = "Auto-Mute Mode",
3509 .info = automute_mode_info,
3510 .get = automute_mode_get,
3511 .put = automute_mode_put,
3512};
3513
3514static int add_automute_mode_enum(struct hda_codec *codec)
3515{
3516 struct hda_gen_spec *spec = codec->spec;
3517
Takashi Iwai12c93df2012-12-19 14:38:33 +01003518 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01003519 return -ENOMEM;
3520 return 0;
3521}
3522
3523/*
3524 * Check the availability of HP/line-out auto-mute;
3525 * Set up appropriately if really supported
3526 */
3527static int check_auto_mute_availability(struct hda_codec *codec)
3528{
3529 struct hda_gen_spec *spec = codec->spec;
3530 struct auto_pin_cfg *cfg = &spec->autocfg;
3531 int present = 0;
3532 int i, err;
3533
Takashi Iwaif72706b2013-01-16 18:20:07 +01003534 if (spec->suppress_auto_mute)
3535 return 0;
3536
Takashi Iwai352f7f92012-12-19 12:52:06 +01003537 if (cfg->hp_pins[0])
3538 present++;
3539 if (cfg->line_out_pins[0])
3540 present++;
3541 if (cfg->speaker_pins[0])
3542 present++;
3543 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02003544 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003545
3546 if (!cfg->speaker_pins[0] &&
3547 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
3548 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3549 sizeof(cfg->speaker_pins));
3550 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552
Takashi Iwai352f7f92012-12-19 12:52:06 +01003553 if (!cfg->hp_pins[0] &&
3554 cfg->line_out_type == AUTO_PIN_HP_OUT) {
3555 memcpy(cfg->hp_pins, cfg->line_out_pins,
3556 sizeof(cfg->hp_pins));
3557 cfg->hp_outs = cfg->line_outs;
3558 }
3559
3560 for (i = 0; i < cfg->hp_outs; i++) {
3561 hda_nid_t nid = cfg->hp_pins[i];
3562 if (!is_jack_detectable(codec, nid))
3563 continue;
3564 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
3565 nid);
3566 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003567 spec->hp_automute_hook ?
3568 spec->hp_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003569 snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003570 spec->detect_hp = 1;
3571 }
3572
3573 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
3574 if (cfg->speaker_outs)
3575 for (i = 0; i < cfg->line_outs; i++) {
3576 hda_nid_t nid = cfg->line_out_pins[i];
3577 if (!is_jack_detectable(codec, nid))
3578 continue;
3579 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
3580 snd_hda_jack_detect_enable_callback(codec, nid,
3581 HDA_GEN_FRONT_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003582 spec->line_automute_hook ?
3583 spec->line_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003584 snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003585 spec->detect_lo = 1;
3586 }
3587 spec->automute_lo_possible = spec->detect_hp;
3588 }
3589
3590 spec->automute_speaker_possible = cfg->speaker_outs &&
3591 (spec->detect_hp || spec->detect_lo);
3592
3593 spec->automute_lo = spec->automute_lo_possible;
3594 spec->automute_speaker = spec->automute_speaker_possible;
3595
3596 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
3597 /* create a control for automute mode */
3598 err = add_automute_mode_enum(codec);
3599 if (err < 0)
3600 return err;
3601 }
3602 return 0;
3603}
3604
Takashi Iwai352f7f92012-12-19 12:52:06 +01003605/* check whether all auto-mic pins are valid; setup indices if OK */
3606static bool auto_mic_check_imux(struct hda_codec *codec)
3607{
3608 struct hda_gen_spec *spec = codec->spec;
3609 const struct hda_input_mux *imux;
3610 int i;
3611
3612 imux = &spec->input_mux;
3613 for (i = 0; i < spec->am_num_entries; i++) {
3614 spec->am_entry[i].idx =
3615 find_idx_in_nid_list(spec->am_entry[i].pin,
3616 spec->imux_pins, imux->num_items);
3617 if (spec->am_entry[i].idx < 0)
3618 return false; /* no corresponding imux */
3619 }
3620
3621 /* we don't need the jack detection for the first pin */
3622 for (i = 1; i < spec->am_num_entries; i++)
3623 snd_hda_jack_detect_enable_callback(codec,
3624 spec->am_entry[i].pin,
3625 HDA_GEN_MIC_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003626 spec->mic_autoswitch_hook ?
3627 spec->mic_autoswitch_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003628 snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003629 return true;
3630}
3631
3632static int compare_attr(const void *ap, const void *bp)
3633{
3634 const struct automic_entry *a = ap;
3635 const struct automic_entry *b = bp;
3636 return (int)(a->attr - b->attr);
3637}
3638
3639/*
3640 * Check the availability of auto-mic switch;
3641 * Set up if really supported
3642 */
3643static int check_auto_mic_availability(struct hda_codec *codec)
3644{
3645 struct hda_gen_spec *spec = codec->spec;
3646 struct auto_pin_cfg *cfg = &spec->autocfg;
3647 unsigned int types;
3648 int i, num_pins;
3649
Takashi Iwaid12daf62013-01-07 16:32:11 +01003650 if (spec->suppress_auto_mic)
3651 return 0;
3652
Takashi Iwai352f7f92012-12-19 12:52:06 +01003653 types = 0;
3654 num_pins = 0;
3655 for (i = 0; i < cfg->num_inputs; i++) {
3656 hda_nid_t nid = cfg->inputs[i].pin;
3657 unsigned int attr;
3658 attr = snd_hda_codec_get_pincfg(codec, nid);
3659 attr = snd_hda_get_input_pin_attr(attr);
3660 if (types & (1 << attr))
3661 return 0; /* already occupied */
3662 switch (attr) {
3663 case INPUT_PIN_ATTR_INT:
3664 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3665 return 0; /* invalid type */
3666 break;
3667 case INPUT_PIN_ATTR_UNUSED:
3668 return 0; /* invalid entry */
3669 default:
3670 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
3671 return 0; /* invalid type */
3672 if (!spec->line_in_auto_switch &&
3673 cfg->inputs[i].type != AUTO_PIN_MIC)
3674 return 0; /* only mic is allowed */
3675 if (!is_jack_detectable(codec, nid))
3676 return 0; /* no unsol support */
3677 break;
3678 }
3679 if (num_pins >= MAX_AUTO_MIC_PINS)
3680 return 0;
3681 types |= (1 << attr);
3682 spec->am_entry[num_pins].pin = nid;
3683 spec->am_entry[num_pins].attr = attr;
3684 num_pins++;
3685 }
3686
3687 if (num_pins < 2)
3688 return 0;
3689
3690 spec->am_num_entries = num_pins;
3691 /* sort the am_entry in the order of attr so that the pin with a
3692 * higher attr will be selected when the jack is plugged.
3693 */
3694 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
3695 compare_attr, NULL);
3696
3697 if (!auto_mic_check_imux(codec))
3698 return 0;
3699
3700 spec->auto_mic = 1;
3701 spec->num_adc_nids = 1;
3702 spec->cur_mux[0] = spec->am_entry[0].idx;
3703 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
3704 spec->am_entry[0].pin,
3705 spec->am_entry[1].pin,
3706 spec->am_entry[2].pin);
3707
3708 return 0;
3709}
3710
3711
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003712/*
3713 * Parse the given BIOS configuration and set up the hda_gen_spec
3714 *
3715 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003716 * or a negative error code
3717 */
3718int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003719 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003720{
3721 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003722 int err;
3723
Takashi Iwai1c70a582013-01-11 17:48:22 +01003724 parse_user_hints(codec);
3725
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003726 if (cfg != &spec->autocfg) {
3727 spec->autocfg = *cfg;
3728 cfg = &spec->autocfg;
3729 }
3730
David Henningsson6fc4cb92013-01-16 15:58:45 +01003731 fill_all_dac_nids(codec);
3732
Takashi Iwai352f7f92012-12-19 12:52:06 +01003733 if (!cfg->line_outs) {
3734 if (cfg->dig_outs || cfg->dig_in_pin) {
3735 spec->multiout.max_channels = 2;
3736 spec->no_analog = 1;
3737 goto dig_only;
3738 }
3739 return 0; /* can't find valid BIOS pin config */
3740 }
3741
3742 if (!spec->no_primary_hp &&
3743 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
3744 cfg->line_outs <= cfg->hp_outs) {
3745 /* use HP as primary out */
3746 cfg->speaker_outs = cfg->line_outs;
3747 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3748 sizeof(cfg->speaker_pins));
3749 cfg->line_outs = cfg->hp_outs;
3750 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
3751 cfg->hp_outs = 0;
3752 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3753 cfg->line_out_type = AUTO_PIN_HP_OUT;
3754 }
3755
3756 err = parse_output_paths(codec);
3757 if (err < 0)
3758 return err;
3759 err = create_multi_channel_mode(codec);
3760 if (err < 0)
3761 return err;
3762 err = create_multi_out_ctls(codec, cfg);
3763 if (err < 0)
3764 return err;
3765 err = create_hp_out_ctls(codec);
3766 if (err < 0)
3767 return err;
3768 err = create_speaker_out_ctls(codec);
3769 if (err < 0)
3770 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003771 err = create_indep_hp_ctls(codec);
3772 if (err < 0)
3773 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01003774 err = create_loopback_mixing_ctl(codec);
3775 if (err < 0)
3776 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003777 err = create_shared_input(codec);
3778 if (err < 0)
3779 return err;
3780 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003781 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02003782 return err;
3783
Takashi Iwaia07a9492013-01-07 16:44:06 +01003784 spec->const_channel_count = spec->ext_channel_count;
3785 /* check the multiple speaker and headphone pins */
3786 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
3787 spec->const_channel_count = max(spec->const_channel_count,
3788 cfg->speaker_outs * 2);
3789 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
3790 spec->const_channel_count = max(spec->const_channel_count,
3791 cfg->hp_outs * 2);
3792 spec->multiout.max_channels = max(spec->ext_channel_count,
3793 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003794
3795 err = check_auto_mute_availability(codec);
3796 if (err < 0)
3797 return err;
3798
3799 err = check_dyn_adc_switch(codec);
3800 if (err < 0)
3801 return err;
3802
3803 if (!spec->shared_mic_hp) {
3804 err = check_auto_mic_availability(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003805 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 }
Takashi Iwai071c73a2006-08-23 18:34:06 +02003808
Takashi Iwai352f7f92012-12-19 12:52:06 +01003809 err = create_capture_mixers(codec);
3810 if (err < 0)
3811 return err;
3812
3813 err = parse_mic_boost(codec);
3814 if (err < 0)
3815 return err;
3816
Takashi Iwai978e77e2013-01-10 16:57:58 +01003817 if (spec->add_out_jack_modes) {
3818 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
3819 err = create_out_jack_modes(codec, cfg->line_outs,
3820 cfg->line_out_pins);
3821 if (err < 0)
3822 return err;
3823 }
3824 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
3825 err = create_out_jack_modes(codec, cfg->hp_outs,
3826 cfg->hp_pins);
3827 if (err < 0)
3828 return err;
3829 }
3830 }
3831
Takashi Iwai352f7f92012-12-19 12:52:06 +01003832 dig_only:
3833 parse_digital(codec);
3834
3835 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003837EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838
3839
3840/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003841 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003843
3844/* slave controls for virtual master */
3845static const char * const slave_pfxs[] = {
3846 "Front", "Surround", "Center", "LFE", "Side",
3847 "Headphone", "Speaker", "Mono", "Line Out",
3848 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01003849 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
3850 "Headphone Front", "Headphone Surround", "Headphone CLFE",
3851 "Headphone Side",
Takashi Iwai352f7f92012-12-19 12:52:06 +01003852 NULL,
3853};
3854
3855int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003857 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859
Takashi Iwai36502d02012-12-19 15:15:10 +01003860 if (spec->kctls.used) {
3861 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
3862 if (err < 0)
3863 return err;
3864 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865
Takashi Iwai352f7f92012-12-19 12:52:06 +01003866 if (spec->multiout.dig_out_nid) {
3867 err = snd_hda_create_dig_out_ctls(codec,
3868 spec->multiout.dig_out_nid,
3869 spec->multiout.dig_out_nid,
3870 spec->pcm_rec[1].pcm_type);
3871 if (err < 0)
3872 return err;
3873 if (!spec->no_analog) {
3874 err = snd_hda_create_spdif_share_sw(codec,
3875 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 if (err < 0)
3877 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003878 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879 }
3880 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003881 if (spec->dig_in_nid) {
3882 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
3883 if (err < 0)
3884 return err;
3885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886
Takashi Iwai352f7f92012-12-19 12:52:06 +01003887 /* if we have no master control, let's create it */
3888 if (!spec->no_analog &&
3889 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003890 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01003891 spec->vmaster_tlv, slave_pfxs,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003892 "Playback Volume");
3893 if (err < 0)
3894 return err;
3895 }
3896 if (!spec->no_analog &&
3897 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
3898 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
3899 NULL, slave_pfxs,
3900 "Playback Switch",
3901 true, &spec->vmaster_mute.sw_kctl);
3902 if (err < 0)
3903 return err;
3904 if (spec->vmaster_mute.hook)
Takashi Iwaifd25a972012-12-20 14:57:18 +01003905 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
3906 spec->vmaster_mute_enum);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908
Takashi Iwai352f7f92012-12-19 12:52:06 +01003909 free_kctls(spec); /* no longer needed */
3910
3911 if (spec->shared_mic_hp) {
3912 int err;
3913 int nid = spec->autocfg.inputs[1].pin;
3914 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
3915 if (err < 0)
3916 return err;
3917 err = snd_hda_jack_detect_enable(codec, nid, 0);
3918 if (err < 0)
3919 return err;
3920 }
3921
3922 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
3923 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 return err;
3925
3926 return 0;
3927}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003928EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
3929
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930
3931/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003932 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003935static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
3936 struct hda_codec *codec,
3937 struct snd_pcm_substream *substream,
3938 int action)
3939{
3940 struct hda_gen_spec *spec = codec->spec;
3941 if (spec->pcm_playback_hook)
3942 spec->pcm_playback_hook(hinfo, codec, substream, action);
3943}
3944
Takashi Iwaiac2e8732013-01-17 15:57:10 +01003945static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
3946 struct hda_codec *codec,
3947 struct snd_pcm_substream *substream,
3948 int action)
3949{
3950 struct hda_gen_spec *spec = codec->spec;
3951 if (spec->pcm_capture_hook)
3952 spec->pcm_capture_hook(hinfo, codec, substream, action);
3953}
3954
Takashi Iwai352f7f92012-12-19 12:52:06 +01003955/*
3956 * Analog playback callbacks
3957 */
3958static int playback_pcm_open(struct hda_pcm_stream *hinfo,
3959 struct hda_codec *codec,
3960 struct snd_pcm_substream *substream)
3961{
3962 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003963 int err;
3964
3965 mutex_lock(&spec->pcm_mutex);
3966 err = snd_hda_multi_out_analog_open(codec,
3967 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003968 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003969 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003970 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003971 call_pcm_playback_hook(hinfo, codec, substream,
3972 HDA_GEN_PCM_ACT_OPEN);
3973 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003974 mutex_unlock(&spec->pcm_mutex);
3975 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003976}
3977
3978static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01003979 struct hda_codec *codec,
3980 unsigned int stream_tag,
3981 unsigned int format,
3982 struct snd_pcm_substream *substream)
3983{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003984 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003985 int err;
3986
3987 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
3988 stream_tag, format, substream);
3989 if (!err)
3990 call_pcm_playback_hook(hinfo, codec, substream,
3991 HDA_GEN_PCM_ACT_PREPARE);
3992 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003993}
Takashi Iwai97ec5582006-03-21 11:29:07 +01003994
Takashi Iwai352f7f92012-12-19 12:52:06 +01003995static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3996 struct hda_codec *codec,
3997 struct snd_pcm_substream *substream)
3998{
3999 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004000 int err;
4001
4002 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
4003 if (!err)
4004 call_pcm_playback_hook(hinfo, codec, substream,
4005 HDA_GEN_PCM_ACT_CLEANUP);
4006 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004007}
4008
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004009static int playback_pcm_close(struct hda_pcm_stream *hinfo,
4010 struct hda_codec *codec,
4011 struct snd_pcm_substream *substream)
4012{
4013 struct hda_gen_spec *spec = codec->spec;
4014 mutex_lock(&spec->pcm_mutex);
4015 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004016 call_pcm_playback_hook(hinfo, codec, substream,
4017 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004018 mutex_unlock(&spec->pcm_mutex);
4019 return 0;
4020}
4021
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004022static int capture_pcm_open(struct hda_pcm_stream *hinfo,
4023 struct hda_codec *codec,
4024 struct snd_pcm_substream *substream)
4025{
4026 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
4027 return 0;
4028}
4029
4030static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4031 struct hda_codec *codec,
4032 unsigned int stream_tag,
4033 unsigned int format,
4034 struct snd_pcm_substream *substream)
4035{
4036 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4037 call_pcm_capture_hook(hinfo, codec, substream,
4038 HDA_GEN_PCM_ACT_PREPARE);
4039 return 0;
4040}
4041
4042static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4043 struct hda_codec *codec,
4044 struct snd_pcm_substream *substream)
4045{
4046 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4047 call_pcm_capture_hook(hinfo, codec, substream,
4048 HDA_GEN_PCM_ACT_CLEANUP);
4049 return 0;
4050}
4051
4052static int capture_pcm_close(struct hda_pcm_stream *hinfo,
4053 struct hda_codec *codec,
4054 struct snd_pcm_substream *substream)
4055{
4056 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
4057 return 0;
4058}
4059
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004060static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
4061 struct hda_codec *codec,
4062 struct snd_pcm_substream *substream)
4063{
4064 struct hda_gen_spec *spec = codec->spec;
4065 int err = 0;
4066
4067 mutex_lock(&spec->pcm_mutex);
4068 if (!spec->indep_hp_enabled)
4069 err = -EBUSY;
4070 else
4071 spec->active_streams |= 1 << STREAM_INDEP_HP;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004072 call_pcm_playback_hook(hinfo, codec, substream,
4073 HDA_GEN_PCM_ACT_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004074 mutex_unlock(&spec->pcm_mutex);
4075 return err;
4076}
4077
4078static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
4079 struct hda_codec *codec,
4080 struct snd_pcm_substream *substream)
4081{
4082 struct hda_gen_spec *spec = codec->spec;
4083 mutex_lock(&spec->pcm_mutex);
4084 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004085 call_pcm_playback_hook(hinfo, codec, substream,
4086 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004087 mutex_unlock(&spec->pcm_mutex);
4088 return 0;
4089}
4090
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004091static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4092 struct hda_codec *codec,
4093 unsigned int stream_tag,
4094 unsigned int format,
4095 struct snd_pcm_substream *substream)
4096{
4097 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4098 call_pcm_playback_hook(hinfo, codec, substream,
4099 HDA_GEN_PCM_ACT_PREPARE);
4100 return 0;
4101}
4102
4103static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4104 struct hda_codec *codec,
4105 struct snd_pcm_substream *substream)
4106{
4107 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4108 call_pcm_playback_hook(hinfo, codec, substream,
4109 HDA_GEN_PCM_ACT_CLEANUP);
4110 return 0;
4111}
4112
Takashi Iwai352f7f92012-12-19 12:52:06 +01004113/*
4114 * Digital out
4115 */
4116static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
4117 struct hda_codec *codec,
4118 struct snd_pcm_substream *substream)
4119{
4120 struct hda_gen_spec *spec = codec->spec;
4121 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
4122}
4123
4124static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4125 struct hda_codec *codec,
4126 unsigned int stream_tag,
4127 unsigned int format,
4128 struct snd_pcm_substream *substream)
4129{
4130 struct hda_gen_spec *spec = codec->spec;
4131 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
4132 stream_tag, format, substream);
4133}
4134
4135static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4136 struct hda_codec *codec,
4137 struct snd_pcm_substream *substream)
4138{
4139 struct hda_gen_spec *spec = codec->spec;
4140 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
4141}
4142
4143static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
4144 struct hda_codec *codec,
4145 struct snd_pcm_substream *substream)
4146{
4147 struct hda_gen_spec *spec = codec->spec;
4148 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
4149}
4150
4151/*
4152 * Analog capture
4153 */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004154#define alt_capture_pcm_open capture_pcm_open
4155#define alt_capture_pcm_close capture_pcm_close
4156
Takashi Iwai352f7f92012-12-19 12:52:06 +01004157static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4158 struct hda_codec *codec,
4159 unsigned int stream_tag,
4160 unsigned int format,
4161 struct snd_pcm_substream *substream)
4162{
4163 struct hda_gen_spec *spec = codec->spec;
4164
4165 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01004166 stream_tag, 0, format);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004167 call_pcm_capture_hook(hinfo, codec, substream,
4168 HDA_GEN_PCM_ACT_PREPARE);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004169 return 0;
4170}
4171
Takashi Iwai352f7f92012-12-19 12:52:06 +01004172static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4173 struct hda_codec *codec,
4174 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01004175{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004176 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01004177
Takashi Iwai352f7f92012-12-19 12:52:06 +01004178 snd_hda_codec_cleanup_stream(codec,
4179 spec->adc_nids[substream->number + 1]);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004180 call_pcm_capture_hook(hinfo, codec, substream,
4181 HDA_GEN_PCM_ACT_CLEANUP);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004182 return 0;
4183}
4184
Takashi Iwai352f7f92012-12-19 12:52:06 +01004185/*
4186 */
4187static const struct hda_pcm_stream pcm_analog_playback = {
4188 .substreams = 1,
4189 .channels_min = 2,
4190 .channels_max = 8,
4191 /* NID is set in build_pcms */
4192 .ops = {
4193 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004194 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004195 .prepare = playback_pcm_prepare,
4196 .cleanup = playback_pcm_cleanup
4197 },
4198};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199
Takashi Iwai352f7f92012-12-19 12:52:06 +01004200static const struct hda_pcm_stream pcm_analog_capture = {
4201 .substreams = 1,
4202 .channels_min = 2,
4203 .channels_max = 2,
4204 /* NID is set in build_pcms */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004205 .ops = {
4206 .open = capture_pcm_open,
4207 .close = capture_pcm_close,
4208 .prepare = capture_pcm_prepare,
4209 .cleanup = capture_pcm_cleanup
4210 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004211};
4212
4213static const struct hda_pcm_stream pcm_analog_alt_playback = {
4214 .substreams = 1,
4215 .channels_min = 2,
4216 .channels_max = 2,
4217 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004218 .ops = {
4219 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004220 .close = alt_playback_pcm_close,
4221 .prepare = alt_playback_pcm_prepare,
4222 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004223 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004224};
4225
4226static const struct hda_pcm_stream pcm_analog_alt_capture = {
4227 .substreams = 2, /* can be overridden */
4228 .channels_min = 2,
4229 .channels_max = 2,
4230 /* NID is set in build_pcms */
4231 .ops = {
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004232 .open = alt_capture_pcm_open,
4233 .close = alt_capture_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004234 .prepare = alt_capture_pcm_prepare,
4235 .cleanup = alt_capture_pcm_cleanup
4236 },
4237};
4238
4239static const struct hda_pcm_stream pcm_digital_playback = {
4240 .substreams = 1,
4241 .channels_min = 2,
4242 .channels_max = 2,
4243 /* NID is set in build_pcms */
4244 .ops = {
4245 .open = dig_playback_pcm_open,
4246 .close = dig_playback_pcm_close,
4247 .prepare = dig_playback_pcm_prepare,
4248 .cleanup = dig_playback_pcm_cleanup
4249 },
4250};
4251
4252static const struct hda_pcm_stream pcm_digital_capture = {
4253 .substreams = 1,
4254 .channels_min = 2,
4255 .channels_max = 2,
4256 /* NID is set in build_pcms */
4257};
4258
4259/* Used by build_pcms to flag that a PCM has no playback stream */
4260static const struct hda_pcm_stream pcm_null_stream = {
4261 .substreams = 0,
4262 .channels_min = 0,
4263 .channels_max = 0,
4264};
4265
4266/*
4267 * dynamic changing ADC PCM streams
4268 */
4269static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
4270{
4271 struct hda_gen_spec *spec = codec->spec;
4272 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
4273
4274 if (spec->cur_adc && spec->cur_adc != new_adc) {
4275 /* stream is running, let's swap the current ADC */
4276 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
4277 spec->cur_adc = new_adc;
4278 snd_hda_codec_setup_stream(codec, new_adc,
4279 spec->cur_adc_stream_tag, 0,
4280 spec->cur_adc_format);
4281 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004283 return false;
4284}
4285
4286/* analog capture with dynamic dual-adc changes */
4287static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4288 struct hda_codec *codec,
4289 unsigned int stream_tag,
4290 unsigned int format,
4291 struct snd_pcm_substream *substream)
4292{
4293 struct hda_gen_spec *spec = codec->spec;
4294 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
4295 spec->cur_adc_stream_tag = stream_tag;
4296 spec->cur_adc_format = format;
4297 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
4298 return 0;
4299}
4300
4301static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4302 struct hda_codec *codec,
4303 struct snd_pcm_substream *substream)
4304{
4305 struct hda_gen_spec *spec = codec->spec;
4306 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
4307 spec->cur_adc = 0;
4308 return 0;
4309}
4310
4311static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
4312 .substreams = 1,
4313 .channels_min = 2,
4314 .channels_max = 2,
4315 .nid = 0, /* fill later */
4316 .ops = {
4317 .prepare = dyn_adc_capture_pcm_prepare,
4318 .cleanup = dyn_adc_capture_pcm_cleanup
4319 },
4320};
4321
Takashi Iwaif873e532012-12-20 16:58:39 +01004322static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
4323 const char *chip_name)
4324{
4325 char *p;
4326
4327 if (*str)
4328 return;
4329 strlcpy(str, chip_name, len);
4330
4331 /* drop non-alnum chars after a space */
4332 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
4333 if (!isalnum(p[1])) {
4334 *p = 0;
4335 break;
4336 }
4337 }
4338 strlcat(str, sfx, len);
4339}
4340
Takashi Iwai352f7f92012-12-19 12:52:06 +01004341/* build PCM streams based on the parsed results */
4342int snd_hda_gen_build_pcms(struct hda_codec *codec)
4343{
4344 struct hda_gen_spec *spec = codec->spec;
4345 struct hda_pcm *info = spec->pcm_rec;
4346 const struct hda_pcm_stream *p;
4347 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348
4349 codec->num_pcms = 1;
4350 codec->pcm_info = info;
4351
Takashi Iwai352f7f92012-12-19 12:52:06 +01004352 if (spec->no_analog)
4353 goto skip_analog;
4354
Takashi Iwaif873e532012-12-20 16:58:39 +01004355 fill_pcm_stream_name(spec->stream_name_analog,
4356 sizeof(spec->stream_name_analog),
4357 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004358 info->name = spec->stream_name_analog;
4359
4360 if (spec->multiout.num_dacs > 0) {
4361 p = spec->stream_analog_playback;
4362 if (!p)
4363 p = &pcm_analog_playback;
4364 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4365 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
4366 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
4367 spec->multiout.max_channels;
4368 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
4369 spec->autocfg.line_outs == 2)
4370 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
4371 snd_pcm_2_1_chmaps;
4372 }
4373 if (spec->num_adc_nids) {
4374 p = spec->stream_analog_capture;
4375 if (!p) {
4376 if (spec->dyn_adc_switch)
4377 p = &dyn_adc_pcm_analog_capture;
4378 else
4379 p = &pcm_analog_capture;
4380 }
4381 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4382 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
4383 }
4384
Takashi Iwai352f7f92012-12-19 12:52:06 +01004385 skip_analog:
4386 /* SPDIF for stream index #1 */
4387 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01004388 fill_pcm_stream_name(spec->stream_name_digital,
4389 sizeof(spec->stream_name_digital),
4390 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004391 codec->num_pcms = 2;
4392 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
4393 info = spec->pcm_rec + 1;
4394 info->name = spec->stream_name_digital;
4395 if (spec->dig_out_type)
4396 info->pcm_type = spec->dig_out_type;
4397 else
4398 info->pcm_type = HDA_PCM_TYPE_SPDIF;
4399 if (spec->multiout.dig_out_nid) {
4400 p = spec->stream_digital_playback;
4401 if (!p)
4402 p = &pcm_digital_playback;
4403 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4404 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
4405 }
4406 if (spec->dig_in_nid) {
4407 p = spec->stream_digital_capture;
4408 if (!p)
4409 p = &pcm_digital_capture;
4410 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4411 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
4412 }
4413 }
4414
4415 if (spec->no_analog)
4416 return 0;
4417
4418 /* If the use of more than one ADC is requested for the current
4419 * model, configure a second analog capture-only PCM.
4420 */
4421 have_multi_adcs = (spec->num_adc_nids > 1) &&
4422 !spec->dyn_adc_switch && !spec->auto_mic;
4423 /* Additional Analaog capture for index #2 */
4424 if (spec->alt_dac_nid || have_multi_adcs) {
4425 codec->num_pcms = 3;
4426 info = spec->pcm_rec + 2;
4427 info->name = spec->stream_name_analog;
4428 if (spec->alt_dac_nid) {
4429 p = spec->stream_analog_alt_playback;
4430 if (!p)
4431 p = &pcm_analog_alt_playback;
4432 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4433 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
4434 spec->alt_dac_nid;
4435 } else {
4436 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
4437 pcm_null_stream;
4438 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
4439 }
4440 if (have_multi_adcs) {
4441 p = spec->stream_analog_alt_capture;
4442 if (!p)
4443 p = &pcm_analog_alt_capture;
4444 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4445 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
4446 spec->adc_nids[1];
4447 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
4448 spec->num_adc_nids - 1;
4449 } else {
4450 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
4451 pcm_null_stream;
4452 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
4453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 }
4455
4456 return 0;
4457}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004458EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
4459
4460
4461/*
4462 * Standard auto-parser initializations
4463 */
4464
Takashi Iwaid4156932013-01-07 10:08:02 +01004465/* configure the given path as a proper output */
Takashi Iwai2c12c302013-01-10 09:33:29 +01004466static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004467{
4468 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01004469 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004470
Takashi Iwai196c17662013-01-04 15:01:40 +01004471 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01004472 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004473 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01004474 pin = path->path[path->depth - 1];
Takashi Iwai2c12c302013-01-10 09:33:29 +01004475 restore_pin_ctl(codec, pin);
Takashi Iwaie1284af2013-01-03 16:33:02 +01004476 snd_hda_activate_path(codec, path, path->active, true);
4477 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004478}
4479
4480/* initialize primary output paths */
4481static void init_multi_out(struct hda_codec *codec)
4482{
4483 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004484 int i;
4485
Takashi Iwaid4156932013-01-07 10:08:02 +01004486 for (i = 0; i < spec->autocfg.line_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004487 set_output_and_unmute(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004488}
4489
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004490
Takashi Iwai2c12c302013-01-10 09:33:29 +01004491static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004492{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004493 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004494
Takashi Iwaid4156932013-01-07 10:08:02 +01004495 for (i = 0; i < num_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004496 set_output_and_unmute(codec, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004497}
4498
4499/* initialize hp and speaker paths */
4500static void init_extra_out(struct hda_codec *codec)
4501{
4502 struct hda_gen_spec *spec = codec->spec;
4503
4504 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004505 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004506 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
4507 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai2c12c302013-01-10 09:33:29 +01004508 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004509}
4510
4511/* initialize multi-io paths */
4512static void init_multi_io(struct hda_codec *codec)
4513{
4514 struct hda_gen_spec *spec = codec->spec;
4515 int i;
4516
4517 for (i = 0; i < spec->multi_ios; i++) {
4518 hda_nid_t pin = spec->multi_io[i].pin;
4519 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01004520 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004521 if (!path)
4522 continue;
4523 if (!spec->multi_io[i].ctl_in)
4524 spec->multi_io[i].ctl_in =
Takashi Iwai2c12c302013-01-10 09:33:29 +01004525 snd_hda_codec_get_pin_target(codec, pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004526 snd_hda_activate_path(codec, path, path->active, true);
4527 }
4528}
4529
Takashi Iwai352f7f92012-12-19 12:52:06 +01004530/* set up input pins and loopback paths */
4531static void init_analog_input(struct hda_codec *codec)
4532{
4533 struct hda_gen_spec *spec = codec->spec;
4534 struct auto_pin_cfg *cfg = &spec->autocfg;
4535 int i;
4536
4537 for (i = 0; i < cfg->num_inputs; i++) {
4538 hda_nid_t nid = cfg->inputs[i].pin;
4539 if (is_input_pin(codec, nid))
Takashi Iwai2c12c302013-01-10 09:33:29 +01004540 restore_pin_ctl(codec, nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004541
4542 /* init loopback inputs */
4543 if (spec->mixer_nid) {
4544 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01004545 path = snd_hda_get_path_from_idx(codec, spec->loopback_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004546 if (path)
4547 snd_hda_activate_path(codec, path,
4548 path->active, false);
4549 }
4550 }
4551}
4552
4553/* initialize ADC paths */
4554static void init_input_src(struct hda_codec *codec)
4555{
4556 struct hda_gen_spec *spec = codec->spec;
4557 struct hda_input_mux *imux = &spec->input_mux;
4558 struct nid_path *path;
4559 int i, c, nums;
4560
4561 if (spec->dyn_adc_switch)
4562 nums = 1;
4563 else
4564 nums = spec->num_adc_nids;
4565
4566 for (c = 0; c < nums; c++) {
4567 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01004568 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004569 if (path) {
4570 bool active = path->active;
4571 if (i == spec->cur_mux[c])
4572 active = true;
4573 snd_hda_activate_path(codec, path, active, false);
4574 }
4575 }
4576 }
4577
4578 if (spec->shared_mic_hp)
4579 update_shared_mic_hp(codec, spec->cur_mux[0]);
4580
4581 if (spec->cap_sync_hook)
4582 spec->cap_sync_hook(codec);
4583}
4584
4585/* set right pin controls for digital I/O */
4586static void init_digital(struct hda_codec *codec)
4587{
4588 struct hda_gen_spec *spec = codec->spec;
4589 int i;
4590 hda_nid_t pin;
4591
Takashi Iwaid4156932013-01-07 10:08:02 +01004592 for (i = 0; i < spec->autocfg.dig_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004593 set_output_and_unmute(codec, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004594 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01004595 if (pin) {
4596 struct nid_path *path;
Takashi Iwai2c12c302013-01-10 09:33:29 +01004597 restore_pin_ctl(codec, pin);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01004598 path = snd_hda_get_path_from_idx(codec, spec->digin_path);
4599 if (path)
4600 snd_hda_activate_path(codec, path, path->active, false);
4601 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004602}
4603
Takashi Iwai973e4972012-12-20 15:16:09 +01004604/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
4605 * invalid unsol tags by some reason
4606 */
4607static void clear_unsol_on_unused_pins(struct hda_codec *codec)
4608{
4609 int i;
4610
4611 for (i = 0; i < codec->init_pins.used; i++) {
4612 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
4613 hda_nid_t nid = pin->nid;
4614 if (is_jack_detectable(codec, nid) &&
4615 !snd_hda_jack_tbl_get(codec, nid))
4616 snd_hda_codec_update_cache(codec, nid, 0,
4617 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
4618 }
4619}
4620
Takashi Iwai5187ac12013-01-07 12:52:16 +01004621/*
4622 * initialize the generic spec;
4623 * this can be put as patch_ops.init function
4624 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004625int snd_hda_gen_init(struct hda_codec *codec)
4626{
4627 struct hda_gen_spec *spec = codec->spec;
4628
4629 if (spec->init_hook)
4630 spec->init_hook(codec);
4631
4632 snd_hda_apply_verbs(codec);
4633
Takashi Iwai3bbcd272012-12-20 11:50:58 +01004634 codec->cached_write = 1;
4635
Takashi Iwai352f7f92012-12-19 12:52:06 +01004636 init_multi_out(codec);
4637 init_extra_out(codec);
4638 init_multi_io(codec);
4639 init_analog_input(codec);
4640 init_input_src(codec);
4641 init_digital(codec);
4642
Takashi Iwai973e4972012-12-20 15:16:09 +01004643 clear_unsol_on_unused_pins(codec);
4644
Takashi Iwai352f7f92012-12-19 12:52:06 +01004645 /* call init functions of standard auto-mute helpers */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01004646 update_automute_all(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004647
Takashi Iwai3bbcd272012-12-20 11:50:58 +01004648 snd_hda_codec_flush_amp_cache(codec);
4649 snd_hda_codec_flush_cmd_cache(codec);
4650
Takashi Iwai352f7f92012-12-19 12:52:06 +01004651 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
4652 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
4653
4654 hda_call_check_power_status(codec, 0x01);
4655 return 0;
4656}
Takashi Iwaifce52a32013-01-07 12:42:48 +01004657EXPORT_SYMBOL_HDA(snd_hda_gen_init);
4658
Takashi Iwai5187ac12013-01-07 12:52:16 +01004659/*
4660 * free the generic spec;
4661 * this can be put as patch_ops.free function
4662 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01004663void snd_hda_gen_free(struct hda_codec *codec)
4664{
4665 snd_hda_gen_spec_free(codec->spec);
4666 kfree(codec->spec);
4667 codec->spec = NULL;
4668}
4669EXPORT_SYMBOL_HDA(snd_hda_gen_free);
4670
4671#ifdef CONFIG_PM
Takashi Iwai5187ac12013-01-07 12:52:16 +01004672/*
4673 * check the loopback power save state;
4674 * this can be put as patch_ops.check_power_status function
4675 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01004676int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
4677{
4678 struct hda_gen_spec *spec = codec->spec;
4679 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
4680}
4681EXPORT_SYMBOL_HDA(snd_hda_gen_check_power_status);
4682#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01004683
4684
4685/*
4686 * the generic codec support
4687 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688
Takashi Iwai352f7f92012-12-19 12:52:06 +01004689static const struct hda_codec_ops generic_patch_ops = {
4690 .build_controls = snd_hda_gen_build_controls,
4691 .build_pcms = snd_hda_gen_build_pcms,
4692 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01004693 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004694 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02004695#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01004696 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02004697#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698};
4699
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700int snd_hda_parse_generic_codec(struct hda_codec *codec)
4701{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004702 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703 int err;
4704
Takashi Iwaie560d8d2005-09-09 14:21:46 +02004705 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004706 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004708 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004711 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
4712 if (err < 0)
4713 return err;
4714
4715 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004716 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717 goto error;
4718
4719 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720 return 0;
4721
Takashi Iwai352f7f92012-12-19 12:52:06 +01004722error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01004723 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724 return err;
4725}
Takashi Iwaifce52a32013-01-07 12:42:48 +01004726EXPORT_SYMBOL_HDA(snd_hda_parse_generic_codec);