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