blob: f0d83b2f42acad42b740478168a520f145ecad0a [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)
Takashi Iwaia90229e2013-01-18 14:10:00 +01002741 spec->cap_sync_hook(codec, ucontrol);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002742 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 Iwaia90229e2013-01-18 14:10:00 +01002777 return cap_put_caller(kcontrol, ucontrol,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002778 snd_hda_mixer_amp_switch_put,
2779 NID_PATH_MUTE_CTL);
2780}
2781
2782static const struct snd_kcontrol_new cap_sw_temp = {
2783 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2784 .name = "Capture Switch",
2785 .info = cap_sw_info,
2786 .get = cap_sw_get,
2787 .put = cap_sw_put,
2788};
2789
2790static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
2791{
2792 hda_nid_t nid;
2793 int i, depth;
2794
2795 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
2796 for (depth = 0; depth < 3; depth++) {
2797 if (depth >= path->depth)
2798 return -EINVAL;
2799 i = path->depth - depth - 1;
2800 nid = path->path[i];
2801 if (!path->ctls[NID_PATH_VOL_CTL]) {
2802 if (nid_has_volume(codec, nid, HDA_OUTPUT))
2803 path->ctls[NID_PATH_VOL_CTL] =
2804 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2805 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
2806 int idx = path->idx[i];
2807 if (!depth && codec->single_adc_amp)
2808 idx = 0;
2809 path->ctls[NID_PATH_VOL_CTL] =
2810 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2811 }
2812 }
2813 if (!path->ctls[NID_PATH_MUTE_CTL]) {
2814 if (nid_has_mute(codec, nid, HDA_OUTPUT))
2815 path->ctls[NID_PATH_MUTE_CTL] =
2816 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2817 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
2818 int idx = path->idx[i];
2819 if (!depth && codec->single_adc_amp)
2820 idx = 0;
2821 path->ctls[NID_PATH_MUTE_CTL] =
2822 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2823 }
2824 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01002825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 return 0;
2827}
2828
Takashi Iwai352f7f92012-12-19 12:52:06 +01002829static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002831 struct hda_gen_spec *spec = codec->spec;
2832 struct auto_pin_cfg *cfg = &spec->autocfg;
2833 unsigned int val;
2834 int i;
2835
2836 if (!spec->inv_dmic_split)
2837 return false;
2838 for (i = 0; i < cfg->num_inputs; i++) {
2839 if (cfg->inputs[i].pin != nid)
2840 continue;
2841 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2842 return false;
2843 val = snd_hda_codec_get_pincfg(codec, nid);
2844 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
2845 }
2846 return false;
2847}
2848
Takashi Iwaia90229e2013-01-18 14:10:00 +01002849/* capture switch put callback for a single control with hook call */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01002850static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
2851 struct snd_ctl_elem_value *ucontrol)
2852{
2853 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2854 struct hda_gen_spec *spec = codec->spec;
2855 int ret;
2856
2857 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2858 if (ret < 0)
2859 return ret;
2860
Takashi Iwaia90229e2013-01-18 14:10:00 +01002861 if (spec->cap_sync_hook)
2862 spec->cap_sync_hook(codec, ucontrol);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01002863
2864 return ret;
2865}
2866
Takashi Iwai352f7f92012-12-19 12:52:06 +01002867static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
2868 int idx, bool is_switch, unsigned int ctl,
2869 bool inv_dmic)
2870{
2871 struct hda_gen_spec *spec = codec->spec;
2872 char tmpname[44];
2873 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
2874 const char *sfx = is_switch ? "Switch" : "Volume";
2875 unsigned int chs = inv_dmic ? 1 : 3;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01002876 struct snd_kcontrol_new *knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002877
2878 if (!ctl)
2879 return 0;
2880
2881 if (label)
2882 snprintf(tmpname, sizeof(tmpname),
2883 "%s Capture %s", label, sfx);
2884 else
2885 snprintf(tmpname, sizeof(tmpname),
2886 "Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01002887 knew = add_control(spec, type, tmpname, idx,
2888 amp_val_replace_channels(ctl, chs));
2889 if (!knew)
2890 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01002891 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01002892 knew->put = cap_single_sw_put;
2893 if (!inv_dmic)
2894 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002895
2896 /* Make independent right kcontrol */
2897 if (label)
2898 snprintf(tmpname, sizeof(tmpname),
2899 "Inverted %s Capture %s", label, sfx);
2900 else
2901 snprintf(tmpname, sizeof(tmpname),
2902 "Inverted Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01002903 knew = add_control(spec, type, tmpname, idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002904 amp_val_replace_channels(ctl, 2));
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01002905 if (!knew)
2906 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01002907 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01002908 knew->put = cap_single_sw_put;
2909 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002910}
2911
2912/* create single (and simple) capture volume and switch controls */
2913static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
2914 unsigned int vol_ctl, unsigned int sw_ctl,
2915 bool inv_dmic)
2916{
2917 int err;
2918 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
2919 if (err < 0)
2920 return err;
2921 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
2922 if (err < 0)
2923 return err;
2924 return 0;
2925}
2926
2927/* create bound capture volume and switch controls */
2928static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
2929 unsigned int vol_ctl, unsigned int sw_ctl)
2930{
2931 struct hda_gen_spec *spec = codec->spec;
2932 struct snd_kcontrol_new *knew;
2933
2934 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002935 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002936 if (!knew)
2937 return -ENOMEM;
2938 knew->index = idx;
2939 knew->private_value = vol_ctl;
2940 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2941 }
2942 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002943 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002944 if (!knew)
2945 return -ENOMEM;
2946 knew->index = idx;
2947 knew->private_value = sw_ctl;
2948 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2949 }
2950 return 0;
2951}
2952
2953/* return the vol ctl when used first in the imux list */
2954static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
2955{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002956 struct nid_path *path;
2957 unsigned int ctl;
2958 int i;
2959
Takashi Iwaic697b712013-01-07 17:09:26 +01002960 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002961 if (!path)
2962 return 0;
2963 ctl = path->ctls[type];
2964 if (!ctl)
2965 return 0;
2966 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01002967 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002968 if (path && path->ctls[type] == ctl)
2969 return 0;
2970 }
2971 return ctl;
2972}
2973
2974/* create individual capture volume and switch controls per input */
2975static int create_multi_cap_vol_ctl(struct hda_codec *codec)
2976{
2977 struct hda_gen_spec *spec = codec->spec;
2978 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaic9700422013-01-18 10:17:30 +01002979 int i, err, type;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002980
2981 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002982 bool inv_dmic;
Takashi Iwaic9700422013-01-18 10:17:30 +01002983 int idx;
Takashi Iwai9dba2052013-01-18 10:01:15 +01002984
Takashi Iwaic9700422013-01-18 10:17:30 +01002985 idx = imux->items[i].index;
2986 if (idx >= spec->autocfg.num_inputs)
Takashi Iwai9dba2052013-01-18 10:01:15 +01002987 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002988 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
2989
2990 for (type = 0; type < 2; type++) {
Takashi Iwaic9700422013-01-18 10:17:30 +01002991 err = add_single_cap_ctl(codec,
2992 spec->input_labels[idx],
2993 spec->input_label_idxs[idx],
2994 type,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002995 get_first_cap_ctl(codec, i, type),
2996 inv_dmic);
2997 if (err < 0)
2998 return err;
2999 }
3000 }
3001 return 0;
3002}
3003
3004static int create_capture_mixers(struct hda_codec *codec)
3005{
3006 struct hda_gen_spec *spec = codec->spec;
3007 struct hda_input_mux *imux = &spec->input_mux;
3008 int i, n, nums, err;
3009
3010 if (spec->dyn_adc_switch)
3011 nums = 1;
3012 else
3013 nums = spec->num_adc_nids;
3014
3015 if (!spec->auto_mic && imux->num_items > 1) {
3016 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01003017 const char *name;
3018 name = nums > 1 ? "Input Source" : "Capture Source";
3019 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003020 if (!knew)
3021 return -ENOMEM;
3022 knew->count = nums;
3023 }
3024
3025 for (n = 0; n < nums; n++) {
3026 bool multi = false;
David Henningsson99a55922013-01-16 15:58:44 +01003027 bool multi_cap_vol = spec->multi_cap_vol;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003028 bool inv_dmic = false;
3029 int vol, sw;
3030
3031 vol = sw = 0;
3032 for (i = 0; i < imux->num_items; i++) {
3033 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01003034 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003035 if (!path)
3036 continue;
3037 parse_capvol_in_path(codec, path);
3038 if (!vol)
3039 vol = path->ctls[NID_PATH_VOL_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003040 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003041 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003042 if (!same_amp_caps(codec, vol,
3043 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3044 multi_cap_vol = true;
3045 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003046 if (!sw)
3047 sw = path->ctls[NID_PATH_MUTE_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003048 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003049 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003050 if (!same_amp_caps(codec, sw,
3051 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3052 multi_cap_vol = true;
3053 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003054 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3055 inv_dmic = true;
3056 }
3057
3058 if (!multi)
3059 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3060 inv_dmic);
David Henningsson99a55922013-01-16 15:58:44 +01003061 else if (!multi_cap_vol)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003062 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3063 else
3064 err = create_multi_cap_vol_ctl(codec);
3065 if (err < 0)
3066 return err;
3067 }
3068
3069 return 0;
3070}
3071
3072/*
3073 * add mic boosts if needed
3074 */
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003075
3076/* check whether the given amp is feasible as a boost volume */
3077static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3078 int dir, int idx)
3079{
3080 unsigned int step;
3081
3082 if (!nid_has_volume(codec, nid, dir) ||
3083 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3084 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3085 return false;
3086
3087 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3088 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3089 if (step < 0x20)
3090 return false;
3091 return true;
3092}
3093
3094/* look for a boost amp in a widget close to the pin */
3095static unsigned int look_for_boost_amp(struct hda_codec *codec,
3096 struct nid_path *path)
3097{
3098 unsigned int val = 0;
3099 hda_nid_t nid;
3100 int depth;
3101
3102 for (depth = 0; depth < 3; depth++) {
3103 if (depth >= path->depth - 1)
3104 break;
3105 nid = path->path[depth];
3106 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3107 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3108 break;
3109 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3110 path->idx[depth])) {
3111 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3112 HDA_INPUT);
3113 break;
3114 }
3115 }
3116
3117 return val;
3118}
3119
Takashi Iwai352f7f92012-12-19 12:52:06 +01003120static int parse_mic_boost(struct hda_codec *codec)
3121{
3122 struct hda_gen_spec *spec = codec->spec;
3123 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003124 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003125 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003126
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003127 if (!spec->num_adc_nids)
3128 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003129
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003130 for (i = 0; i < imux->num_items; i++) {
3131 struct nid_path *path;
3132 unsigned int val;
3133 int idx;
3134 char boost_label[44];
David Henningsson02aba552013-01-16 15:58:43 +01003135
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003136 idx = imux->items[i].index;
3137 if (idx >= imux->num_items)
3138 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003139
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003140 /* check only line-in and mic pins */
3141 if (cfg->inputs[idx].type > AUTO_PIN_MIC)
3142 continue;
3143
3144 path = get_input_path(codec, 0, i);
3145 if (!path)
3146 continue;
3147
3148 val = look_for_boost_amp(codec, path);
3149 if (!val)
3150 continue;
3151
3152 /* create a boost control */
3153 snprintf(boost_label, sizeof(boost_label),
3154 "%s Boost Volume", spec->input_labels[idx]);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003155 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3156 spec->input_label_idxs[idx], val))
3157 return -ENOMEM;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003158
3159 path->ctls[NID_PATH_BOOST_CTL] = val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003160 }
3161 return 0;
3162}
3163
3164/*
3165 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3166 */
3167static void parse_digital(struct hda_codec *codec)
3168{
3169 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003170 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003171 int i, nums;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003172 hda_nid_t dig_nid, pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003173
3174 /* support multiple SPDIFs; the secondary is set up as a slave */
3175 nums = 0;
3176 for (i = 0; i < spec->autocfg.dig_outs; i++) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003177 pin = spec->autocfg.dig_out_pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003178 dig_nid = look_for_dac(codec, pin, true);
3179 if (!dig_nid)
3180 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003181 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003182 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003183 continue;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003184 print_nid_path("digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01003185 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01003186 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003187 set_pin_target(codec, pin, PIN_OUT, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003188 if (!nums) {
3189 spec->multiout.dig_out_nid = dig_nid;
3190 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3191 } else {
3192 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3193 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
3194 break;
3195 spec->slave_dig_outs[nums - 1] = dig_nid;
3196 }
3197 nums++;
3198 }
3199
3200 if (spec->autocfg.dig_in_pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003201 pin = spec->autocfg.dig_in_pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003202 dig_nid = codec->start_nid;
3203 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003204 unsigned int wcaps = get_wcaps(codec, dig_nid);
3205 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3206 continue;
3207 if (!(wcaps & AC_WCAP_DIGITAL))
3208 continue;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003209 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003210 if (path) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003211 print_nid_path("digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003212 path->active = true;
3213 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003214 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003215 set_pin_target(codec, pin, PIN_IN, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003216 break;
3217 }
3218 }
3219 }
3220}
3221
3222
3223/*
3224 * input MUX handling
3225 */
3226
3227static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3228
3229/* select the given imux item; either unmute exclusively or select the route */
3230static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3231 unsigned int idx)
3232{
3233 struct hda_gen_spec *spec = codec->spec;
3234 const struct hda_input_mux *imux;
3235 struct nid_path *path;
3236
3237 imux = &spec->input_mux;
3238 if (!imux->num_items)
3239 return 0;
3240
3241 if (idx >= imux->num_items)
3242 idx = imux->num_items - 1;
3243 if (spec->cur_mux[adc_idx] == idx)
3244 return 0;
3245
Takashi Iwaic697b712013-01-07 17:09:26 +01003246 path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003247 if (!path)
3248 return 0;
3249 if (path->active)
3250 snd_hda_activate_path(codec, path, false, false);
3251
3252 spec->cur_mux[adc_idx] = idx;
3253
3254 if (spec->shared_mic_hp)
3255 update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
3256
3257 if (spec->dyn_adc_switch)
3258 dyn_adc_pcm_resetup(codec, idx);
3259
Takashi Iwaic697b712013-01-07 17:09:26 +01003260 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003261 if (!path)
3262 return 0;
3263 if (path->active)
3264 return 0;
3265 snd_hda_activate_path(codec, path, true, false);
3266 if (spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01003267 spec->cap_sync_hook(codec, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003268 return 1;
3269}
3270
3271
3272/*
3273 * Jack detections for HP auto-mute and mic-switch
3274 */
3275
3276/* check each pin in the given array; returns true if any of them is plugged */
3277static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
3278{
3279 int i, present = 0;
3280
3281 for (i = 0; i < num_pins; i++) {
3282 hda_nid_t nid = pins[i];
3283 if (!nid)
3284 break;
Takashi Iwai0b4df932013-01-10 09:45:13 +01003285 /* don't detect pins retasked as inputs */
3286 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
3287 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003288 present |= snd_hda_jack_detect(codec, nid);
3289 }
3290 return present;
3291}
3292
3293/* standard HP/line-out auto-mute helper */
3294static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
Takashi Iwai2c12c302013-01-10 09:33:29 +01003295 bool mute)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003296{
3297 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003298 int i;
3299
3300 for (i = 0; i < num_pins; i++) {
3301 hda_nid_t nid = pins[i];
3302 unsigned int val;
3303 if (!nid)
3304 break;
3305 /* don't reset VREF value in case it's controlling
3306 * the amp (see alc861_fixup_asus_amp_vref_0f())
3307 */
Takashi Iwai2c12c302013-01-10 09:33:29 +01003308 if (spec->keep_vref_in_automute)
3309 val = snd_hda_codec_get_pin_target(codec, nid) & ~PIN_HP;
3310 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01003311 val = 0;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003312 if (!mute)
3313 val |= snd_hda_codec_get_pin_target(codec, nid);
3314 /* here we call update_pin_ctl() so that the pinctl is changed
3315 * without changing the pinctl target value;
3316 * the original target value will be still referred at the
3317 * init / resume again
3318 */
3319 update_pin_ctl(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01003320 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003321 }
3322}
3323
3324/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003325void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003326{
3327 struct hda_gen_spec *spec = codec->spec;
3328 int on;
3329
3330 /* Control HP pins/amps depending on master_mute state;
3331 * in general, HP pins/amps control should be enabled in all cases,
3332 * but currently set only for master_mute, just to be safe
3333 */
3334 if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
3335 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003336 spec->autocfg.hp_pins, spec->master_mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003337
3338 if (!spec->automute_speaker)
3339 on = 0;
3340 else
3341 on = spec->hp_jack_present | spec->line_jack_present;
3342 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003343 spec->speaker_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003344 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003345 spec->autocfg.speaker_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003346
3347 /* toggle line-out mutes if needed, too */
3348 /* if LO is a copy of either HP or Speaker, don't need to handle it */
3349 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
3350 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
3351 return;
3352 if (!spec->automute_lo)
3353 on = 0;
3354 else
3355 on = spec->hp_jack_present;
3356 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003357 spec->line_out_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003358 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003359 spec->autocfg.line_out_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003360}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003361EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003362
3363static void call_update_outputs(struct hda_codec *codec)
3364{
3365 struct hda_gen_spec *spec = codec->spec;
3366 if (spec->automute_hook)
3367 spec->automute_hook(codec);
3368 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01003369 snd_hda_gen_update_outputs(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003370}
3371
3372/* standard HP-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003373void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003374{
3375 struct hda_gen_spec *spec = codec->spec;
3376
3377 spec->hp_jack_present =
3378 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
3379 spec->autocfg.hp_pins);
3380 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
3381 return;
3382 call_update_outputs(codec);
3383}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003384EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003385
3386/* standard line-out-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003387void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003388{
3389 struct hda_gen_spec *spec = codec->spec;
3390
3391 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3392 return;
3393 /* check LO jack only when it's different from HP */
3394 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
3395 return;
3396
3397 spec->line_jack_present =
3398 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3399 spec->autocfg.line_out_pins);
3400 if (!spec->automute_speaker || !spec->detect_lo)
3401 return;
3402 call_update_outputs(codec);
3403}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003404EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003405
3406/* standard mic auto-switch helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003407void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003408{
3409 struct hda_gen_spec *spec = codec->spec;
3410 int i;
3411
3412 if (!spec->auto_mic)
3413 return;
3414
3415 for (i = spec->am_num_entries - 1; i > 0; i--) {
Takashi Iwai0b4df932013-01-10 09:45:13 +01003416 hda_nid_t pin = spec->am_entry[i].pin;
3417 /* don't detect pins retasked as outputs */
3418 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
3419 continue;
3420 if (snd_hda_jack_detect(codec, pin)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003421 mux_select(codec, 0, spec->am_entry[i].idx);
3422 return;
3423 }
3424 }
3425 mux_select(codec, 0, spec->am_entry[0].idx);
3426}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003427EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003428
Takashi Iwaia5cc2502013-01-16 18:08:55 +01003429/* update jack retasking */
3430static void update_automute_all(struct hda_codec *codec)
3431{
3432 struct hda_gen_spec *spec = codec->spec;
3433
3434 if (spec->hp_automute_hook)
3435 spec->hp_automute_hook(codec, NULL);
3436 else
3437 snd_hda_gen_hp_automute(codec, NULL);
3438 if (spec->line_automute_hook)
3439 spec->line_automute_hook(codec, NULL);
3440 else
3441 snd_hda_gen_line_automute(codec, NULL);
3442 if (spec->mic_autoswitch_hook)
3443 spec->mic_autoswitch_hook(codec, NULL);
3444 else
3445 snd_hda_gen_mic_autoswitch(codec, NULL);
3446}
3447
Takashi Iwai352f7f92012-12-19 12:52:06 +01003448/*
3449 * Auto-Mute mode mixer enum support
3450 */
3451static int automute_mode_info(struct snd_kcontrol *kcontrol,
3452 struct snd_ctl_elem_info *uinfo)
3453{
3454 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3455 struct hda_gen_spec *spec = codec->spec;
3456 static const char * const texts3[] = {
3457 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02003458 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459
Takashi Iwai352f7f92012-12-19 12:52:06 +01003460 if (spec->automute_speaker_possible && spec->automute_lo_possible)
3461 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
3462 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
3463}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464
Takashi Iwai352f7f92012-12-19 12:52:06 +01003465static int automute_mode_get(struct snd_kcontrol *kcontrol,
3466 struct snd_ctl_elem_value *ucontrol)
3467{
3468 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3469 struct hda_gen_spec *spec = codec->spec;
3470 unsigned int val = 0;
3471 if (spec->automute_speaker)
3472 val++;
3473 if (spec->automute_lo)
3474 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003475
Takashi Iwai352f7f92012-12-19 12:52:06 +01003476 ucontrol->value.enumerated.item[0] = val;
3477 return 0;
3478}
3479
3480static int automute_mode_put(struct snd_kcontrol *kcontrol,
3481 struct snd_ctl_elem_value *ucontrol)
3482{
3483 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3484 struct hda_gen_spec *spec = codec->spec;
3485
3486 switch (ucontrol->value.enumerated.item[0]) {
3487 case 0:
3488 if (!spec->automute_speaker && !spec->automute_lo)
3489 return 0;
3490 spec->automute_speaker = 0;
3491 spec->automute_lo = 0;
3492 break;
3493 case 1:
3494 if (spec->automute_speaker_possible) {
3495 if (!spec->automute_lo && spec->automute_speaker)
3496 return 0;
3497 spec->automute_speaker = 1;
3498 spec->automute_lo = 0;
3499 } else if (spec->automute_lo_possible) {
3500 if (spec->automute_lo)
3501 return 0;
3502 spec->automute_lo = 1;
3503 } else
3504 return -EINVAL;
3505 break;
3506 case 2:
3507 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
3508 return -EINVAL;
3509 if (spec->automute_speaker && spec->automute_lo)
3510 return 0;
3511 spec->automute_speaker = 1;
3512 spec->automute_lo = 1;
3513 break;
3514 default:
3515 return -EINVAL;
3516 }
3517 call_update_outputs(codec);
3518 return 1;
3519}
3520
3521static const struct snd_kcontrol_new automute_mode_enum = {
3522 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3523 .name = "Auto-Mute Mode",
3524 .info = automute_mode_info,
3525 .get = automute_mode_get,
3526 .put = automute_mode_put,
3527};
3528
3529static int add_automute_mode_enum(struct hda_codec *codec)
3530{
3531 struct hda_gen_spec *spec = codec->spec;
3532
Takashi Iwai12c93df2012-12-19 14:38:33 +01003533 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01003534 return -ENOMEM;
3535 return 0;
3536}
3537
3538/*
3539 * Check the availability of HP/line-out auto-mute;
3540 * Set up appropriately if really supported
3541 */
3542static int check_auto_mute_availability(struct hda_codec *codec)
3543{
3544 struct hda_gen_spec *spec = codec->spec;
3545 struct auto_pin_cfg *cfg = &spec->autocfg;
3546 int present = 0;
3547 int i, err;
3548
Takashi Iwaif72706b2013-01-16 18:20:07 +01003549 if (spec->suppress_auto_mute)
3550 return 0;
3551
Takashi Iwai352f7f92012-12-19 12:52:06 +01003552 if (cfg->hp_pins[0])
3553 present++;
3554 if (cfg->line_out_pins[0])
3555 present++;
3556 if (cfg->speaker_pins[0])
3557 present++;
3558 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02003559 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003560
3561 if (!cfg->speaker_pins[0] &&
3562 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
3563 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3564 sizeof(cfg->speaker_pins));
3565 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003566 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567
Takashi Iwai352f7f92012-12-19 12:52:06 +01003568 if (!cfg->hp_pins[0] &&
3569 cfg->line_out_type == AUTO_PIN_HP_OUT) {
3570 memcpy(cfg->hp_pins, cfg->line_out_pins,
3571 sizeof(cfg->hp_pins));
3572 cfg->hp_outs = cfg->line_outs;
3573 }
3574
3575 for (i = 0; i < cfg->hp_outs; i++) {
3576 hda_nid_t nid = cfg->hp_pins[i];
3577 if (!is_jack_detectable(codec, nid))
3578 continue;
3579 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
3580 nid);
3581 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003582 spec->hp_automute_hook ?
3583 spec->hp_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003584 snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003585 spec->detect_hp = 1;
3586 }
3587
3588 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
3589 if (cfg->speaker_outs)
3590 for (i = 0; i < cfg->line_outs; i++) {
3591 hda_nid_t nid = cfg->line_out_pins[i];
3592 if (!is_jack_detectable(codec, nid))
3593 continue;
3594 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
3595 snd_hda_jack_detect_enable_callback(codec, nid,
3596 HDA_GEN_FRONT_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003597 spec->line_automute_hook ?
3598 spec->line_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003599 snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003600 spec->detect_lo = 1;
3601 }
3602 spec->automute_lo_possible = spec->detect_hp;
3603 }
3604
3605 spec->automute_speaker_possible = cfg->speaker_outs &&
3606 (spec->detect_hp || spec->detect_lo);
3607
3608 spec->automute_lo = spec->automute_lo_possible;
3609 spec->automute_speaker = spec->automute_speaker_possible;
3610
3611 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
3612 /* create a control for automute mode */
3613 err = add_automute_mode_enum(codec);
3614 if (err < 0)
3615 return err;
3616 }
3617 return 0;
3618}
3619
Takashi Iwai352f7f92012-12-19 12:52:06 +01003620/* check whether all auto-mic pins are valid; setup indices if OK */
3621static bool auto_mic_check_imux(struct hda_codec *codec)
3622{
3623 struct hda_gen_spec *spec = codec->spec;
3624 const struct hda_input_mux *imux;
3625 int i;
3626
3627 imux = &spec->input_mux;
3628 for (i = 0; i < spec->am_num_entries; i++) {
3629 spec->am_entry[i].idx =
3630 find_idx_in_nid_list(spec->am_entry[i].pin,
3631 spec->imux_pins, imux->num_items);
3632 if (spec->am_entry[i].idx < 0)
3633 return false; /* no corresponding imux */
3634 }
3635
3636 /* we don't need the jack detection for the first pin */
3637 for (i = 1; i < spec->am_num_entries; i++)
3638 snd_hda_jack_detect_enable_callback(codec,
3639 spec->am_entry[i].pin,
3640 HDA_GEN_MIC_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003641 spec->mic_autoswitch_hook ?
3642 spec->mic_autoswitch_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003643 snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003644 return true;
3645}
3646
3647static int compare_attr(const void *ap, const void *bp)
3648{
3649 const struct automic_entry *a = ap;
3650 const struct automic_entry *b = bp;
3651 return (int)(a->attr - b->attr);
3652}
3653
3654/*
3655 * Check the availability of auto-mic switch;
3656 * Set up if really supported
3657 */
3658static int check_auto_mic_availability(struct hda_codec *codec)
3659{
3660 struct hda_gen_spec *spec = codec->spec;
3661 struct auto_pin_cfg *cfg = &spec->autocfg;
3662 unsigned int types;
3663 int i, num_pins;
3664
Takashi Iwaid12daf62013-01-07 16:32:11 +01003665 if (spec->suppress_auto_mic)
3666 return 0;
3667
Takashi Iwai352f7f92012-12-19 12:52:06 +01003668 types = 0;
3669 num_pins = 0;
3670 for (i = 0; i < cfg->num_inputs; i++) {
3671 hda_nid_t nid = cfg->inputs[i].pin;
3672 unsigned int attr;
3673 attr = snd_hda_codec_get_pincfg(codec, nid);
3674 attr = snd_hda_get_input_pin_attr(attr);
3675 if (types & (1 << attr))
3676 return 0; /* already occupied */
3677 switch (attr) {
3678 case INPUT_PIN_ATTR_INT:
3679 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3680 return 0; /* invalid type */
3681 break;
3682 case INPUT_PIN_ATTR_UNUSED:
3683 return 0; /* invalid entry */
3684 default:
3685 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
3686 return 0; /* invalid type */
3687 if (!spec->line_in_auto_switch &&
3688 cfg->inputs[i].type != AUTO_PIN_MIC)
3689 return 0; /* only mic is allowed */
3690 if (!is_jack_detectable(codec, nid))
3691 return 0; /* no unsol support */
3692 break;
3693 }
3694 if (num_pins >= MAX_AUTO_MIC_PINS)
3695 return 0;
3696 types |= (1 << attr);
3697 spec->am_entry[num_pins].pin = nid;
3698 spec->am_entry[num_pins].attr = attr;
3699 num_pins++;
3700 }
3701
3702 if (num_pins < 2)
3703 return 0;
3704
3705 spec->am_num_entries = num_pins;
3706 /* sort the am_entry in the order of attr so that the pin with a
3707 * higher attr will be selected when the jack is plugged.
3708 */
3709 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
3710 compare_attr, NULL);
3711
3712 if (!auto_mic_check_imux(codec))
3713 return 0;
3714
3715 spec->auto_mic = 1;
3716 spec->num_adc_nids = 1;
3717 spec->cur_mux[0] = spec->am_entry[0].idx;
3718 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
3719 spec->am_entry[0].pin,
3720 spec->am_entry[1].pin,
3721 spec->am_entry[2].pin);
3722
3723 return 0;
3724}
3725
3726
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003727/*
3728 * Parse the given BIOS configuration and set up the hda_gen_spec
3729 *
3730 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003731 * or a negative error code
3732 */
3733int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003734 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003735{
3736 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003737 int err;
3738
Takashi Iwai1c70a582013-01-11 17:48:22 +01003739 parse_user_hints(codec);
3740
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003741 if (cfg != &spec->autocfg) {
3742 spec->autocfg = *cfg;
3743 cfg = &spec->autocfg;
3744 }
3745
David Henningsson6fc4cb92013-01-16 15:58:45 +01003746 fill_all_dac_nids(codec);
3747
Takashi Iwai352f7f92012-12-19 12:52:06 +01003748 if (!cfg->line_outs) {
3749 if (cfg->dig_outs || cfg->dig_in_pin) {
3750 spec->multiout.max_channels = 2;
3751 spec->no_analog = 1;
3752 goto dig_only;
3753 }
3754 return 0; /* can't find valid BIOS pin config */
3755 }
3756
3757 if (!spec->no_primary_hp &&
3758 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
3759 cfg->line_outs <= cfg->hp_outs) {
3760 /* use HP as primary out */
3761 cfg->speaker_outs = cfg->line_outs;
3762 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3763 sizeof(cfg->speaker_pins));
3764 cfg->line_outs = cfg->hp_outs;
3765 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
3766 cfg->hp_outs = 0;
3767 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3768 cfg->line_out_type = AUTO_PIN_HP_OUT;
3769 }
3770
3771 err = parse_output_paths(codec);
3772 if (err < 0)
3773 return err;
3774 err = create_multi_channel_mode(codec);
3775 if (err < 0)
3776 return err;
3777 err = create_multi_out_ctls(codec, cfg);
3778 if (err < 0)
3779 return err;
3780 err = create_hp_out_ctls(codec);
3781 if (err < 0)
3782 return err;
3783 err = create_speaker_out_ctls(codec);
3784 if (err < 0)
3785 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003786 err = create_indep_hp_ctls(codec);
3787 if (err < 0)
3788 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01003789 err = create_loopback_mixing_ctl(codec);
3790 if (err < 0)
3791 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003792 err = create_shared_input(codec);
3793 if (err < 0)
3794 return err;
3795 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003796 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02003797 return err;
3798
Takashi Iwaia07a9492013-01-07 16:44:06 +01003799 spec->const_channel_count = spec->ext_channel_count;
3800 /* check the multiple speaker and headphone pins */
3801 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
3802 spec->const_channel_count = max(spec->const_channel_count,
3803 cfg->speaker_outs * 2);
3804 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
3805 spec->const_channel_count = max(spec->const_channel_count,
3806 cfg->hp_outs * 2);
3807 spec->multiout.max_channels = max(spec->ext_channel_count,
3808 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003809
3810 err = check_auto_mute_availability(codec);
3811 if (err < 0)
3812 return err;
3813
3814 err = check_dyn_adc_switch(codec);
3815 if (err < 0)
3816 return err;
3817
3818 if (!spec->shared_mic_hp) {
3819 err = check_auto_mic_availability(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003820 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 }
Takashi Iwai071c73a2006-08-23 18:34:06 +02003823
Takashi Iwai352f7f92012-12-19 12:52:06 +01003824 err = create_capture_mixers(codec);
3825 if (err < 0)
3826 return err;
3827
3828 err = parse_mic_boost(codec);
3829 if (err < 0)
3830 return err;
3831
Takashi Iwai978e77e2013-01-10 16:57:58 +01003832 if (spec->add_out_jack_modes) {
3833 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
3834 err = create_out_jack_modes(codec, cfg->line_outs,
3835 cfg->line_out_pins);
3836 if (err < 0)
3837 return err;
3838 }
3839 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
3840 err = create_out_jack_modes(codec, cfg->hp_outs,
3841 cfg->hp_pins);
3842 if (err < 0)
3843 return err;
3844 }
3845 }
3846
Takashi Iwai352f7f92012-12-19 12:52:06 +01003847 dig_only:
3848 parse_digital(codec);
3849
3850 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003852EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853
3854
3855/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003856 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003858
3859/* slave controls for virtual master */
3860static const char * const slave_pfxs[] = {
3861 "Front", "Surround", "Center", "LFE", "Side",
3862 "Headphone", "Speaker", "Mono", "Line Out",
3863 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01003864 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
3865 "Headphone Front", "Headphone Surround", "Headphone CLFE",
3866 "Headphone Side",
Takashi Iwai352f7f92012-12-19 12:52:06 +01003867 NULL,
3868};
3869
3870int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003872 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874
Takashi Iwai36502d02012-12-19 15:15:10 +01003875 if (spec->kctls.used) {
3876 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
3877 if (err < 0)
3878 return err;
3879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880
Takashi Iwai352f7f92012-12-19 12:52:06 +01003881 if (spec->multiout.dig_out_nid) {
3882 err = snd_hda_create_dig_out_ctls(codec,
3883 spec->multiout.dig_out_nid,
3884 spec->multiout.dig_out_nid,
3885 spec->pcm_rec[1].pcm_type);
3886 if (err < 0)
3887 return err;
3888 if (!spec->no_analog) {
3889 err = snd_hda_create_spdif_share_sw(codec,
3890 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 if (err < 0)
3892 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003893 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894 }
3895 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003896 if (spec->dig_in_nid) {
3897 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
3898 if (err < 0)
3899 return err;
3900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901
Takashi Iwai352f7f92012-12-19 12:52:06 +01003902 /* if we have no master control, let's create it */
3903 if (!spec->no_analog &&
3904 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003905 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01003906 spec->vmaster_tlv, slave_pfxs,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003907 "Playback Volume");
3908 if (err < 0)
3909 return err;
3910 }
3911 if (!spec->no_analog &&
3912 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
3913 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
3914 NULL, slave_pfxs,
3915 "Playback Switch",
3916 true, &spec->vmaster_mute.sw_kctl);
3917 if (err < 0)
3918 return err;
3919 if (spec->vmaster_mute.hook)
Takashi Iwaifd25a972012-12-20 14:57:18 +01003920 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
3921 spec->vmaster_mute_enum);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923
Takashi Iwai352f7f92012-12-19 12:52:06 +01003924 free_kctls(spec); /* no longer needed */
3925
3926 if (spec->shared_mic_hp) {
3927 int err;
3928 int nid = spec->autocfg.inputs[1].pin;
3929 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
3930 if (err < 0)
3931 return err;
3932 err = snd_hda_jack_detect_enable(codec, nid, 0);
3933 if (err < 0)
3934 return err;
3935 }
3936
3937 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
3938 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 return err;
3940
3941 return 0;
3942}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003943EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
3944
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945
3946/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003947 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003950static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
3951 struct hda_codec *codec,
3952 struct snd_pcm_substream *substream,
3953 int action)
3954{
3955 struct hda_gen_spec *spec = codec->spec;
3956 if (spec->pcm_playback_hook)
3957 spec->pcm_playback_hook(hinfo, codec, substream, action);
3958}
3959
Takashi Iwaiac2e8732013-01-17 15:57:10 +01003960static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
3961 struct hda_codec *codec,
3962 struct snd_pcm_substream *substream,
3963 int action)
3964{
3965 struct hda_gen_spec *spec = codec->spec;
3966 if (spec->pcm_capture_hook)
3967 spec->pcm_capture_hook(hinfo, codec, substream, action);
3968}
3969
Takashi Iwai352f7f92012-12-19 12:52:06 +01003970/*
3971 * Analog playback callbacks
3972 */
3973static int playback_pcm_open(struct hda_pcm_stream *hinfo,
3974 struct hda_codec *codec,
3975 struct snd_pcm_substream *substream)
3976{
3977 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003978 int err;
3979
3980 mutex_lock(&spec->pcm_mutex);
3981 err = snd_hda_multi_out_analog_open(codec,
3982 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003983 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003984 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003985 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003986 call_pcm_playback_hook(hinfo, codec, substream,
3987 HDA_GEN_PCM_ACT_OPEN);
3988 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003989 mutex_unlock(&spec->pcm_mutex);
3990 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003991}
3992
3993static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01003994 struct hda_codec *codec,
3995 unsigned int stream_tag,
3996 unsigned int format,
3997 struct snd_pcm_substream *substream)
3998{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003999 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004000 int err;
4001
4002 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
4003 stream_tag, format, substream);
4004 if (!err)
4005 call_pcm_playback_hook(hinfo, codec, substream,
4006 HDA_GEN_PCM_ACT_PREPARE);
4007 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004008}
Takashi Iwai97ec5582006-03-21 11:29:07 +01004009
Takashi Iwai352f7f92012-12-19 12:52:06 +01004010static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4011 struct hda_codec *codec,
4012 struct snd_pcm_substream *substream)
4013{
4014 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004015 int err;
4016
4017 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
4018 if (!err)
4019 call_pcm_playback_hook(hinfo, codec, substream,
4020 HDA_GEN_PCM_ACT_CLEANUP);
4021 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004022}
4023
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004024static int playback_pcm_close(struct hda_pcm_stream *hinfo,
4025 struct hda_codec *codec,
4026 struct snd_pcm_substream *substream)
4027{
4028 struct hda_gen_spec *spec = codec->spec;
4029 mutex_lock(&spec->pcm_mutex);
4030 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004031 call_pcm_playback_hook(hinfo, codec, substream,
4032 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004033 mutex_unlock(&spec->pcm_mutex);
4034 return 0;
4035}
4036
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004037static int capture_pcm_open(struct hda_pcm_stream *hinfo,
4038 struct hda_codec *codec,
4039 struct snd_pcm_substream *substream)
4040{
4041 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
4042 return 0;
4043}
4044
4045static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4046 struct hda_codec *codec,
4047 unsigned int stream_tag,
4048 unsigned int format,
4049 struct snd_pcm_substream *substream)
4050{
4051 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4052 call_pcm_capture_hook(hinfo, codec, substream,
4053 HDA_GEN_PCM_ACT_PREPARE);
4054 return 0;
4055}
4056
4057static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4058 struct hda_codec *codec,
4059 struct snd_pcm_substream *substream)
4060{
4061 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4062 call_pcm_capture_hook(hinfo, codec, substream,
4063 HDA_GEN_PCM_ACT_CLEANUP);
4064 return 0;
4065}
4066
4067static int capture_pcm_close(struct hda_pcm_stream *hinfo,
4068 struct hda_codec *codec,
4069 struct snd_pcm_substream *substream)
4070{
4071 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
4072 return 0;
4073}
4074
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004075static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
4076 struct hda_codec *codec,
4077 struct snd_pcm_substream *substream)
4078{
4079 struct hda_gen_spec *spec = codec->spec;
4080 int err = 0;
4081
4082 mutex_lock(&spec->pcm_mutex);
4083 if (!spec->indep_hp_enabled)
4084 err = -EBUSY;
4085 else
4086 spec->active_streams |= 1 << STREAM_INDEP_HP;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004087 call_pcm_playback_hook(hinfo, codec, substream,
4088 HDA_GEN_PCM_ACT_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004089 mutex_unlock(&spec->pcm_mutex);
4090 return err;
4091}
4092
4093static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
4094 struct hda_codec *codec,
4095 struct snd_pcm_substream *substream)
4096{
4097 struct hda_gen_spec *spec = codec->spec;
4098 mutex_lock(&spec->pcm_mutex);
4099 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004100 call_pcm_playback_hook(hinfo, codec, substream,
4101 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004102 mutex_unlock(&spec->pcm_mutex);
4103 return 0;
4104}
4105
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004106static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4107 struct hda_codec *codec,
4108 unsigned int stream_tag,
4109 unsigned int format,
4110 struct snd_pcm_substream *substream)
4111{
4112 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4113 call_pcm_playback_hook(hinfo, codec, substream,
4114 HDA_GEN_PCM_ACT_PREPARE);
4115 return 0;
4116}
4117
4118static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4119 struct hda_codec *codec,
4120 struct snd_pcm_substream *substream)
4121{
4122 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4123 call_pcm_playback_hook(hinfo, codec, substream,
4124 HDA_GEN_PCM_ACT_CLEANUP);
4125 return 0;
4126}
4127
Takashi Iwai352f7f92012-12-19 12:52:06 +01004128/*
4129 * Digital out
4130 */
4131static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
4132 struct hda_codec *codec,
4133 struct snd_pcm_substream *substream)
4134{
4135 struct hda_gen_spec *spec = codec->spec;
4136 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
4137}
4138
4139static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4140 struct hda_codec *codec,
4141 unsigned int stream_tag,
4142 unsigned int format,
4143 struct snd_pcm_substream *substream)
4144{
4145 struct hda_gen_spec *spec = codec->spec;
4146 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
4147 stream_tag, format, substream);
4148}
4149
4150static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4151 struct hda_codec *codec,
4152 struct snd_pcm_substream *substream)
4153{
4154 struct hda_gen_spec *spec = codec->spec;
4155 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
4156}
4157
4158static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
4159 struct hda_codec *codec,
4160 struct snd_pcm_substream *substream)
4161{
4162 struct hda_gen_spec *spec = codec->spec;
4163 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
4164}
4165
4166/*
4167 * Analog capture
4168 */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004169#define alt_capture_pcm_open capture_pcm_open
4170#define alt_capture_pcm_close capture_pcm_close
4171
Takashi Iwai352f7f92012-12-19 12:52:06 +01004172static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4173 struct hda_codec *codec,
4174 unsigned int stream_tag,
4175 unsigned int format,
4176 struct snd_pcm_substream *substream)
4177{
4178 struct hda_gen_spec *spec = codec->spec;
4179
4180 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01004181 stream_tag, 0, format);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004182 call_pcm_capture_hook(hinfo, codec, substream,
4183 HDA_GEN_PCM_ACT_PREPARE);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004184 return 0;
4185}
4186
Takashi Iwai352f7f92012-12-19 12:52:06 +01004187static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4188 struct hda_codec *codec,
4189 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01004190{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004191 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01004192
Takashi Iwai352f7f92012-12-19 12:52:06 +01004193 snd_hda_codec_cleanup_stream(codec,
4194 spec->adc_nids[substream->number + 1]);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004195 call_pcm_capture_hook(hinfo, codec, substream,
4196 HDA_GEN_PCM_ACT_CLEANUP);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004197 return 0;
4198}
4199
Takashi Iwai352f7f92012-12-19 12:52:06 +01004200/*
4201 */
4202static const struct hda_pcm_stream pcm_analog_playback = {
4203 .substreams = 1,
4204 .channels_min = 2,
4205 .channels_max = 8,
4206 /* NID is set in build_pcms */
4207 .ops = {
4208 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004209 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004210 .prepare = playback_pcm_prepare,
4211 .cleanup = playback_pcm_cleanup
4212 },
4213};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214
Takashi Iwai352f7f92012-12-19 12:52:06 +01004215static const struct hda_pcm_stream pcm_analog_capture = {
4216 .substreams = 1,
4217 .channels_min = 2,
4218 .channels_max = 2,
4219 /* NID is set in build_pcms */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004220 .ops = {
4221 .open = capture_pcm_open,
4222 .close = capture_pcm_close,
4223 .prepare = capture_pcm_prepare,
4224 .cleanup = capture_pcm_cleanup
4225 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004226};
4227
4228static const struct hda_pcm_stream pcm_analog_alt_playback = {
4229 .substreams = 1,
4230 .channels_min = 2,
4231 .channels_max = 2,
4232 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004233 .ops = {
4234 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004235 .close = alt_playback_pcm_close,
4236 .prepare = alt_playback_pcm_prepare,
4237 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004238 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004239};
4240
4241static const struct hda_pcm_stream pcm_analog_alt_capture = {
4242 .substreams = 2, /* can be overridden */
4243 .channels_min = 2,
4244 .channels_max = 2,
4245 /* NID is set in build_pcms */
4246 .ops = {
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004247 .open = alt_capture_pcm_open,
4248 .close = alt_capture_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004249 .prepare = alt_capture_pcm_prepare,
4250 .cleanup = alt_capture_pcm_cleanup
4251 },
4252};
4253
4254static const struct hda_pcm_stream pcm_digital_playback = {
4255 .substreams = 1,
4256 .channels_min = 2,
4257 .channels_max = 2,
4258 /* NID is set in build_pcms */
4259 .ops = {
4260 .open = dig_playback_pcm_open,
4261 .close = dig_playback_pcm_close,
4262 .prepare = dig_playback_pcm_prepare,
4263 .cleanup = dig_playback_pcm_cleanup
4264 },
4265};
4266
4267static const struct hda_pcm_stream pcm_digital_capture = {
4268 .substreams = 1,
4269 .channels_min = 2,
4270 .channels_max = 2,
4271 /* NID is set in build_pcms */
4272};
4273
4274/* Used by build_pcms to flag that a PCM has no playback stream */
4275static const struct hda_pcm_stream pcm_null_stream = {
4276 .substreams = 0,
4277 .channels_min = 0,
4278 .channels_max = 0,
4279};
4280
4281/*
4282 * dynamic changing ADC PCM streams
4283 */
4284static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
4285{
4286 struct hda_gen_spec *spec = codec->spec;
4287 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
4288
4289 if (spec->cur_adc && spec->cur_adc != new_adc) {
4290 /* stream is running, let's swap the current ADC */
4291 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
4292 spec->cur_adc = new_adc;
4293 snd_hda_codec_setup_stream(codec, new_adc,
4294 spec->cur_adc_stream_tag, 0,
4295 spec->cur_adc_format);
4296 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004298 return false;
4299}
4300
4301/* analog capture with dynamic dual-adc changes */
4302static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4303 struct hda_codec *codec,
4304 unsigned int stream_tag,
4305 unsigned int format,
4306 struct snd_pcm_substream *substream)
4307{
4308 struct hda_gen_spec *spec = codec->spec;
4309 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
4310 spec->cur_adc_stream_tag = stream_tag;
4311 spec->cur_adc_format = format;
4312 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
4313 return 0;
4314}
4315
4316static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4317 struct hda_codec *codec,
4318 struct snd_pcm_substream *substream)
4319{
4320 struct hda_gen_spec *spec = codec->spec;
4321 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
4322 spec->cur_adc = 0;
4323 return 0;
4324}
4325
4326static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
4327 .substreams = 1,
4328 .channels_min = 2,
4329 .channels_max = 2,
4330 .nid = 0, /* fill later */
4331 .ops = {
4332 .prepare = dyn_adc_capture_pcm_prepare,
4333 .cleanup = dyn_adc_capture_pcm_cleanup
4334 },
4335};
4336
Takashi Iwaif873e532012-12-20 16:58:39 +01004337static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
4338 const char *chip_name)
4339{
4340 char *p;
4341
4342 if (*str)
4343 return;
4344 strlcpy(str, chip_name, len);
4345
4346 /* drop non-alnum chars after a space */
4347 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
4348 if (!isalnum(p[1])) {
4349 *p = 0;
4350 break;
4351 }
4352 }
4353 strlcat(str, sfx, len);
4354}
4355
Takashi Iwai352f7f92012-12-19 12:52:06 +01004356/* build PCM streams based on the parsed results */
4357int snd_hda_gen_build_pcms(struct hda_codec *codec)
4358{
4359 struct hda_gen_spec *spec = codec->spec;
4360 struct hda_pcm *info = spec->pcm_rec;
4361 const struct hda_pcm_stream *p;
4362 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363
4364 codec->num_pcms = 1;
4365 codec->pcm_info = info;
4366
Takashi Iwai352f7f92012-12-19 12:52:06 +01004367 if (spec->no_analog)
4368 goto skip_analog;
4369
Takashi Iwaif873e532012-12-20 16:58:39 +01004370 fill_pcm_stream_name(spec->stream_name_analog,
4371 sizeof(spec->stream_name_analog),
4372 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004373 info->name = spec->stream_name_analog;
4374
4375 if (spec->multiout.num_dacs > 0) {
4376 p = spec->stream_analog_playback;
4377 if (!p)
4378 p = &pcm_analog_playback;
4379 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4380 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
4381 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
4382 spec->multiout.max_channels;
4383 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
4384 spec->autocfg.line_outs == 2)
4385 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
4386 snd_pcm_2_1_chmaps;
4387 }
4388 if (spec->num_adc_nids) {
4389 p = spec->stream_analog_capture;
4390 if (!p) {
4391 if (spec->dyn_adc_switch)
4392 p = &dyn_adc_pcm_analog_capture;
4393 else
4394 p = &pcm_analog_capture;
4395 }
4396 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4397 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
4398 }
4399
Takashi Iwai352f7f92012-12-19 12:52:06 +01004400 skip_analog:
4401 /* SPDIF for stream index #1 */
4402 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01004403 fill_pcm_stream_name(spec->stream_name_digital,
4404 sizeof(spec->stream_name_digital),
4405 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004406 codec->num_pcms = 2;
4407 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
4408 info = spec->pcm_rec + 1;
4409 info->name = spec->stream_name_digital;
4410 if (spec->dig_out_type)
4411 info->pcm_type = spec->dig_out_type;
4412 else
4413 info->pcm_type = HDA_PCM_TYPE_SPDIF;
4414 if (spec->multiout.dig_out_nid) {
4415 p = spec->stream_digital_playback;
4416 if (!p)
4417 p = &pcm_digital_playback;
4418 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4419 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
4420 }
4421 if (spec->dig_in_nid) {
4422 p = spec->stream_digital_capture;
4423 if (!p)
4424 p = &pcm_digital_capture;
4425 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4426 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
4427 }
4428 }
4429
4430 if (spec->no_analog)
4431 return 0;
4432
4433 /* If the use of more than one ADC is requested for the current
4434 * model, configure a second analog capture-only PCM.
4435 */
4436 have_multi_adcs = (spec->num_adc_nids > 1) &&
4437 !spec->dyn_adc_switch && !spec->auto_mic;
4438 /* Additional Analaog capture for index #2 */
4439 if (spec->alt_dac_nid || have_multi_adcs) {
4440 codec->num_pcms = 3;
4441 info = spec->pcm_rec + 2;
4442 info->name = spec->stream_name_analog;
4443 if (spec->alt_dac_nid) {
4444 p = spec->stream_analog_alt_playback;
4445 if (!p)
4446 p = &pcm_analog_alt_playback;
4447 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4448 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
4449 spec->alt_dac_nid;
4450 } else {
4451 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
4452 pcm_null_stream;
4453 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
4454 }
4455 if (have_multi_adcs) {
4456 p = spec->stream_analog_alt_capture;
4457 if (!p)
4458 p = &pcm_analog_alt_capture;
4459 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4460 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
4461 spec->adc_nids[1];
4462 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
4463 spec->num_adc_nids - 1;
4464 } else {
4465 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
4466 pcm_null_stream;
4467 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
4468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 }
4470
4471 return 0;
4472}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004473EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
4474
4475
4476/*
4477 * Standard auto-parser initializations
4478 */
4479
Takashi Iwaid4156932013-01-07 10:08:02 +01004480/* configure the given path as a proper output */
Takashi Iwai2c12c302013-01-10 09:33:29 +01004481static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004482{
4483 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01004484 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004485
Takashi Iwai196c17662013-01-04 15:01:40 +01004486 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01004487 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004488 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01004489 pin = path->path[path->depth - 1];
Takashi Iwai2c12c302013-01-10 09:33:29 +01004490 restore_pin_ctl(codec, pin);
Takashi Iwaie1284af2013-01-03 16:33:02 +01004491 snd_hda_activate_path(codec, path, path->active, true);
4492 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004493}
4494
4495/* initialize primary output paths */
4496static void init_multi_out(struct hda_codec *codec)
4497{
4498 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004499 int i;
4500
Takashi Iwaid4156932013-01-07 10:08:02 +01004501 for (i = 0; i < spec->autocfg.line_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004502 set_output_and_unmute(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004503}
4504
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004505
Takashi Iwai2c12c302013-01-10 09:33:29 +01004506static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004507{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004508 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004509
Takashi Iwaid4156932013-01-07 10:08:02 +01004510 for (i = 0; i < num_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004511 set_output_and_unmute(codec, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004512}
4513
4514/* initialize hp and speaker paths */
4515static void init_extra_out(struct hda_codec *codec)
4516{
4517 struct hda_gen_spec *spec = codec->spec;
4518
4519 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004520 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004521 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
4522 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai2c12c302013-01-10 09:33:29 +01004523 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004524}
4525
4526/* initialize multi-io paths */
4527static void init_multi_io(struct hda_codec *codec)
4528{
4529 struct hda_gen_spec *spec = codec->spec;
4530 int i;
4531
4532 for (i = 0; i < spec->multi_ios; i++) {
4533 hda_nid_t pin = spec->multi_io[i].pin;
4534 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01004535 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004536 if (!path)
4537 continue;
4538 if (!spec->multi_io[i].ctl_in)
4539 spec->multi_io[i].ctl_in =
Takashi Iwai2c12c302013-01-10 09:33:29 +01004540 snd_hda_codec_get_pin_target(codec, pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004541 snd_hda_activate_path(codec, path, path->active, true);
4542 }
4543}
4544
Takashi Iwai352f7f92012-12-19 12:52:06 +01004545/* set up input pins and loopback paths */
4546static void init_analog_input(struct hda_codec *codec)
4547{
4548 struct hda_gen_spec *spec = codec->spec;
4549 struct auto_pin_cfg *cfg = &spec->autocfg;
4550 int i;
4551
4552 for (i = 0; i < cfg->num_inputs; i++) {
4553 hda_nid_t nid = cfg->inputs[i].pin;
4554 if (is_input_pin(codec, nid))
Takashi Iwai2c12c302013-01-10 09:33:29 +01004555 restore_pin_ctl(codec, nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004556
4557 /* init loopback inputs */
4558 if (spec->mixer_nid) {
4559 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01004560 path = snd_hda_get_path_from_idx(codec, spec->loopback_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004561 if (path)
4562 snd_hda_activate_path(codec, path,
4563 path->active, false);
4564 }
4565 }
4566}
4567
4568/* initialize ADC paths */
4569static void init_input_src(struct hda_codec *codec)
4570{
4571 struct hda_gen_spec *spec = codec->spec;
4572 struct hda_input_mux *imux = &spec->input_mux;
4573 struct nid_path *path;
4574 int i, c, nums;
4575
4576 if (spec->dyn_adc_switch)
4577 nums = 1;
4578 else
4579 nums = spec->num_adc_nids;
4580
4581 for (c = 0; c < nums; c++) {
4582 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01004583 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004584 if (path) {
4585 bool active = path->active;
4586 if (i == spec->cur_mux[c])
4587 active = true;
4588 snd_hda_activate_path(codec, path, active, false);
4589 }
4590 }
4591 }
4592
4593 if (spec->shared_mic_hp)
4594 update_shared_mic_hp(codec, spec->cur_mux[0]);
4595
4596 if (spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01004597 spec->cap_sync_hook(codec, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004598}
4599
4600/* set right pin controls for digital I/O */
4601static void init_digital(struct hda_codec *codec)
4602{
4603 struct hda_gen_spec *spec = codec->spec;
4604 int i;
4605 hda_nid_t pin;
4606
Takashi Iwaid4156932013-01-07 10:08:02 +01004607 for (i = 0; i < spec->autocfg.dig_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004608 set_output_and_unmute(codec, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004609 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01004610 if (pin) {
4611 struct nid_path *path;
Takashi Iwai2c12c302013-01-10 09:33:29 +01004612 restore_pin_ctl(codec, pin);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01004613 path = snd_hda_get_path_from_idx(codec, spec->digin_path);
4614 if (path)
4615 snd_hda_activate_path(codec, path, path->active, false);
4616 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004617}
4618
Takashi Iwai973e4972012-12-20 15:16:09 +01004619/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
4620 * invalid unsol tags by some reason
4621 */
4622static void clear_unsol_on_unused_pins(struct hda_codec *codec)
4623{
4624 int i;
4625
4626 for (i = 0; i < codec->init_pins.used; i++) {
4627 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
4628 hda_nid_t nid = pin->nid;
4629 if (is_jack_detectable(codec, nid) &&
4630 !snd_hda_jack_tbl_get(codec, nid))
4631 snd_hda_codec_update_cache(codec, nid, 0,
4632 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
4633 }
4634}
4635
Takashi Iwai5187ac12013-01-07 12:52:16 +01004636/*
4637 * initialize the generic spec;
4638 * this can be put as patch_ops.init function
4639 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004640int snd_hda_gen_init(struct hda_codec *codec)
4641{
4642 struct hda_gen_spec *spec = codec->spec;
4643
4644 if (spec->init_hook)
4645 spec->init_hook(codec);
4646
4647 snd_hda_apply_verbs(codec);
4648
Takashi Iwai3bbcd272012-12-20 11:50:58 +01004649 codec->cached_write = 1;
4650
Takashi Iwai352f7f92012-12-19 12:52:06 +01004651 init_multi_out(codec);
4652 init_extra_out(codec);
4653 init_multi_io(codec);
4654 init_analog_input(codec);
4655 init_input_src(codec);
4656 init_digital(codec);
4657
Takashi Iwai973e4972012-12-20 15:16:09 +01004658 clear_unsol_on_unused_pins(codec);
4659
Takashi Iwai352f7f92012-12-19 12:52:06 +01004660 /* call init functions of standard auto-mute helpers */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01004661 update_automute_all(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004662
Takashi Iwai3bbcd272012-12-20 11:50:58 +01004663 snd_hda_codec_flush_amp_cache(codec);
4664 snd_hda_codec_flush_cmd_cache(codec);
4665
Takashi Iwai352f7f92012-12-19 12:52:06 +01004666 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
4667 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
4668
4669 hda_call_check_power_status(codec, 0x01);
4670 return 0;
4671}
Takashi Iwaifce52a32013-01-07 12:42:48 +01004672EXPORT_SYMBOL_HDA(snd_hda_gen_init);
4673
Takashi Iwai5187ac12013-01-07 12:52:16 +01004674/*
4675 * free the generic spec;
4676 * this can be put as patch_ops.free function
4677 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01004678void snd_hda_gen_free(struct hda_codec *codec)
4679{
4680 snd_hda_gen_spec_free(codec->spec);
4681 kfree(codec->spec);
4682 codec->spec = NULL;
4683}
4684EXPORT_SYMBOL_HDA(snd_hda_gen_free);
4685
4686#ifdef CONFIG_PM
Takashi Iwai5187ac12013-01-07 12:52:16 +01004687/*
4688 * check the loopback power save state;
4689 * this can be put as patch_ops.check_power_status function
4690 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01004691int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
4692{
4693 struct hda_gen_spec *spec = codec->spec;
4694 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
4695}
4696EXPORT_SYMBOL_HDA(snd_hda_gen_check_power_status);
4697#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01004698
4699
4700/*
4701 * the generic codec support
4702 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703
Takashi Iwai352f7f92012-12-19 12:52:06 +01004704static const struct hda_codec_ops generic_patch_ops = {
4705 .build_controls = snd_hda_gen_build_controls,
4706 .build_pcms = snd_hda_gen_build_pcms,
4707 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01004708 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004709 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02004710#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01004711 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02004712#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713};
4714
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715int snd_hda_parse_generic_codec(struct hda_codec *codec)
4716{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004717 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718 int err;
4719
Takashi Iwaie560d8d2005-09-09 14:21:46 +02004720 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004721 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004723 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004726 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
4727 if (err < 0)
4728 return err;
4729
4730 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004731 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732 goto error;
4733
4734 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735 return 0;
4736
Takashi Iwai352f7f92012-12-19 12:52:06 +01004737error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01004738 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739 return err;
4740}
Takashi Iwaifce52a32013-01-07 12:42:48 +01004741EXPORT_SYMBOL_HDA(snd_hda_parse_generic_codec);