blob: ef4c04adbc21f8084142d741ca89a8e0ac12fb1b [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,
318 int dir, int idx)
319{
320 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
321 return is_ctl_used(codec, val, NID_PATH_VOL_CTL) ||
322 is_ctl_used(codec, val, NID_PATH_MUTE_CTL);
323}
324
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100325static void print_nid_path(const char *pfx, struct nid_path *path)
326{
327 char buf[40];
328 int i;
329
330
331 buf[0] = 0;
332 for (i = 0; i < path->depth; i++) {
333 char tmp[4];
334 sprintf(tmp, ":%02x", path->path[i]);
335 strlcat(buf, tmp, sizeof(buf));
336 }
337 snd_printdd("%s path: depth=%d %s\n", pfx, path->depth, buf);
338}
339
Takashi Iwai352f7f92012-12-19 12:52:06 +0100340/* called recursively */
341static bool __parse_nid_path(struct hda_codec *codec,
342 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100343 int anchor_nid, struct nid_path *path,
344 int depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100345{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100346 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100347 int i, nums;
348
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100349 if (to_nid == anchor_nid)
350 anchor_nid = 0; /* anchor passed */
351 else if (to_nid == (hda_nid_t)(-anchor_nid))
352 return false; /* hit the exclusive nid */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100353
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100354 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100355 for (i = 0; i < nums; i++) {
356 if (conn[i] != from_nid) {
357 /* special case: when from_nid is 0,
358 * try to find an empty DAC
359 */
360 if (from_nid ||
361 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
362 is_dac_already_used(codec, conn[i]))
363 continue;
364 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100365 /* anchor is not requested or already passed? */
366 if (anchor_nid <= 0)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100367 goto found;
368 }
369 if (depth >= MAX_NID_PATH_DEPTH)
370 return false;
371 for (i = 0; i < nums; i++) {
372 unsigned int type;
373 type = get_wcaps_type(get_wcaps(codec, conn[i]));
374 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
375 type == AC_WID_PIN)
376 continue;
377 if (__parse_nid_path(codec, from_nid, conn[i],
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100378 anchor_nid, path, depth + 1))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100379 goto found;
380 }
381 return false;
382
383 found:
384 path->path[path->depth] = conn[i];
385 path->idx[path->depth + 1] = i;
386 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
387 path->multi[path->depth + 1] = 1;
388 path->depth++;
389 return true;
390}
391
392/* parse the widget path from the given nid to the target nid;
393 * when @from_nid is 0, try to find an empty DAC;
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100394 * when @anchor_nid is set to a positive value, only paths through the widget
395 * with the given value are evaluated.
396 * when @anchor_nid is set to a negative value, paths through the widget
397 * with the negative of given value are excluded, only other paths are chosen.
398 * when @anchor_nid is zero, no special handling about path selection.
Takashi Iwai352f7f92012-12-19 12:52:06 +0100399 */
400bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100401 hda_nid_t to_nid, int anchor_nid,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100402 struct nid_path *path)
403{
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100404 if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100405 path->path[path->depth] = to_nid;
406 path->depth++;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100407 return true;
408 }
409 return false;
410}
411EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100414 * parse the path between the given NIDs and add to the path list.
415 * if no valid path is found, return NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100417struct nid_path *
418snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100419 hda_nid_t to_nid, int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100421 struct hda_gen_spec *spec = codec->spec;
422 struct nid_path *path;
423
424 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
425 return NULL;
426
Takashi Iwaif5172a72013-01-04 13:19:55 +0100427 /* check whether the path has been already added */
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100428 path = get_nid_path(codec, from_nid, to_nid, anchor_nid);
Takashi Iwaif5172a72013-01-04 13:19:55 +0100429 if (path)
430 return path;
431
Takashi Iwai352f7f92012-12-19 12:52:06 +0100432 path = snd_array_new(&spec->paths);
433 if (!path)
434 return NULL;
435 memset(path, 0, sizeof(*path));
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100436 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100437 return path;
438 /* push back */
439 spec->paths.used--;
440 return NULL;
441}
442EXPORT_SYMBOL_HDA(snd_hda_add_new_path);
443
Takashi Iwai980428c2013-01-09 09:28:20 +0100444/* clear the given path as invalid so that it won't be picked up later */
445static void invalidate_nid_path(struct hda_codec *codec, int idx)
446{
447 struct nid_path *path = snd_hda_get_path_from_idx(codec, idx);
448 if (!path)
449 return;
450 memset(path, 0, sizeof(*path));
451}
452
Takashi Iwai352f7f92012-12-19 12:52:06 +0100453/* look for an empty DAC slot */
454static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
455 bool is_digital)
456{
457 struct hda_gen_spec *spec = codec->spec;
458 bool cap_digital;
459 int i;
460
461 for (i = 0; i < spec->num_all_dacs; i++) {
462 hda_nid_t nid = spec->all_dacs[i];
463 if (!nid || is_dac_already_used(codec, nid))
464 continue;
465 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
466 if (is_digital != cap_digital)
467 continue;
468 if (is_reachable_path(codec, nid, pin))
469 return nid;
470 }
471 return 0;
472}
473
474/* replace the channels in the composed amp value with the given number */
475static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
476{
477 val &= ~(0x3U << 16);
478 val |= chs << 16;
479 return val;
480}
481
482/* check whether the widget has the given amp capability for the direction */
483static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
484 int dir, unsigned int bits)
485{
486 if (!nid)
487 return false;
488 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
489 if (query_amp_caps(codec, nid, dir) & bits)
490 return true;
491 return false;
492}
493
David Henningsson99a55922013-01-16 15:58:44 +0100494static bool same_amp_caps(struct hda_codec *codec, hda_nid_t nid1,
495 hda_nid_t nid2, int dir)
496{
497 if (!(get_wcaps(codec, nid1) & (1 << (dir + 1))))
498 return !(get_wcaps(codec, nid2) & (1 << (dir + 1)));
499 return (query_amp_caps(codec, nid1, dir) ==
500 query_amp_caps(codec, nid2, dir));
501}
502
Takashi Iwai352f7f92012-12-19 12:52:06 +0100503#define nid_has_mute(codec, nid, dir) \
504 check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
505#define nid_has_volume(codec, nid, dir) \
506 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
507
508/* look for a widget suitable for assigning a mute switch in the path */
509static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
510 struct nid_path *path)
511{
512 int i;
513
514 for (i = path->depth - 1; i >= 0; i--) {
515 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
516 return path->path[i];
517 if (i != path->depth - 1 && i != 0 &&
518 nid_has_mute(codec, path->path[i], HDA_INPUT))
519 return path->path[i];
520 }
521 return 0;
522}
523
524/* look for a widget suitable for assigning a volume ctl in the path */
525static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
526 struct nid_path *path)
527{
528 int i;
529
530 for (i = path->depth - 1; i >= 0; i--) {
531 if (nid_has_volume(codec, path->path[i], HDA_OUTPUT))
532 return path->path[i];
533 }
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200534 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535}
536
537/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100538 * path activation / deactivation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100540
541/* can have the amp-in capability? */
542static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100544 hda_nid_t nid = path->path[idx];
545 unsigned int caps = get_wcaps(codec, nid);
546 unsigned int type = get_wcaps_type(caps);
547
548 if (!(caps & AC_WCAP_IN_AMP))
549 return false;
550 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
551 return false;
552 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553}
554
Takashi Iwai352f7f92012-12-19 12:52:06 +0100555/* can have the amp-out capability? */
556static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100558 hda_nid_t nid = path->path[idx];
559 unsigned int caps = get_wcaps(codec, nid);
560 unsigned int type = get_wcaps_type(caps);
561
562 if (!(caps & AC_WCAP_OUT_AMP))
563 return false;
564 if (type == AC_WID_PIN && !idx) /* only for output pins */
565 return false;
566 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567}
568
Takashi Iwai352f7f92012-12-19 12:52:06 +0100569/* check whether the given (nid,dir,idx) is active */
570static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
571 unsigned int idx, unsigned int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100573 struct hda_gen_spec *spec = codec->spec;
574 int i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Takashi Iwai352f7f92012-12-19 12:52:06 +0100576 for (n = 0; n < spec->paths.used; n++) {
577 struct nid_path *path = snd_array_elem(&spec->paths, n);
578 if (!path->active)
579 continue;
580 for (i = 0; i < path->depth; i++) {
581 if (path->path[i] == nid) {
582 if (dir == HDA_OUTPUT || path->idx[i] == idx)
583 return true;
584 break;
585 }
586 }
587 }
588 return false;
589}
590
591/* get the default amp value for the target state */
592static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
593 int dir, bool enable)
594{
595 unsigned int caps;
596 unsigned int val = 0;
597
598 caps = query_amp_caps(codec, nid, dir);
599 if (caps & AC_AMPCAP_NUM_STEPS) {
600 /* set to 0dB */
601 if (enable)
602 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
603 }
604 if (caps & AC_AMPCAP_MUTE) {
605 if (!enable)
606 val |= HDA_AMP_MUTE;
607 }
608 return val;
609}
610
611/* initialize the amp value (only at the first time) */
612static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
613{
614 int val = get_amp_val_to_activate(codec, nid, dir, false);
615 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
616}
617
618static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
619 int idx, bool enable)
620{
621 int val;
622 if (is_ctl_associated(codec, nid, dir, idx) ||
Takashi Iwai985803c2013-01-03 16:30:04 +0100623 (!enable && is_active_nid(codec, nid, dir, idx)))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100624 return;
625 val = get_amp_val_to_activate(codec, nid, dir, enable);
626 snd_hda_codec_amp_stereo(codec, nid, dir, idx, 0xff, val);
627}
628
629static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
630 int i, bool enable)
631{
632 hda_nid_t nid = path->path[i];
633 init_amp(codec, nid, HDA_OUTPUT, 0);
634 activate_amp(codec, nid, HDA_OUTPUT, 0, enable);
635}
636
637static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
638 int i, bool enable, bool add_aamix)
639{
640 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100641 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100642 int n, nums, idx;
643 int type;
644 hda_nid_t nid = path->path[i];
645
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100646 nums = snd_hda_get_conn_list(codec, nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100647 type = get_wcaps_type(get_wcaps(codec, nid));
648 if (type == AC_WID_PIN ||
649 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
650 nums = 1;
651 idx = 0;
652 } else
653 idx = path->idx[i];
654
655 for (n = 0; n < nums; n++)
656 init_amp(codec, nid, HDA_INPUT, n);
657
658 if (is_ctl_associated(codec, nid, HDA_INPUT, idx))
659 return;
660
661 /* here is a little bit tricky in comparison with activate_amp_out();
662 * when aa-mixer is available, we need to enable the path as well
663 */
664 for (n = 0; n < nums; n++) {
665 if (n != idx && (!add_aamix || conn[n] != spec->mixer_nid))
666 continue;
667 activate_amp(codec, nid, HDA_INPUT, n, enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 }
669}
670
Takashi Iwai352f7f92012-12-19 12:52:06 +0100671/* activate or deactivate the given path
672 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100674void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
675 bool enable, bool add_aamix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100677 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Takashi Iwai352f7f92012-12-19 12:52:06 +0100679 if (!enable)
680 path->active = false;
681
682 for (i = path->depth - 1; i >= 0; i--) {
683 if (enable && path->multi[i])
684 snd_hda_codec_write_cache(codec, path->path[i], 0,
685 AC_VERB_SET_CONNECT_SEL,
686 path->idx[i]);
687 if (has_amp_in(codec, path, i))
688 activate_amp_in(codec, path, i, enable, add_aamix);
689 if (has_amp_out(codec, path, i))
690 activate_amp_out(codec, path, i, enable);
691 }
692
693 if (enable)
694 path->active = true;
695}
696EXPORT_SYMBOL_HDA(snd_hda_activate_path);
697
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100698/* turn on/off EAPD on the given pin */
699static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
700{
701 struct hda_gen_spec *spec = codec->spec;
702 if (spec->own_eapd_ctl ||
703 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
704 return;
Takashi Iwaiecac3ed2012-12-21 15:23:01 +0100705 if (codec->inv_eapd)
706 enable = !enable;
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100707 snd_hda_codec_update_cache(codec, pin, 0,
708 AC_VERB_SET_EAPD_BTLENABLE,
709 enable ? 0x02 : 0x00);
710}
711
Takashi Iwai352f7f92012-12-19 12:52:06 +0100712
713/*
714 * Helper functions for creating mixer ctl elements
715 */
716
717enum {
718 HDA_CTL_WIDGET_VOL,
719 HDA_CTL_WIDGET_MUTE,
720 HDA_CTL_BIND_MUTE,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100721};
722static const struct snd_kcontrol_new control_templates[] = {
723 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
724 HDA_CODEC_MUTE(NULL, 0, 0, 0),
725 HDA_BIND_MUTE(NULL, 0, 0, 0),
Takashi Iwai352f7f92012-12-19 12:52:06 +0100726};
727
728/* add dynamic controls from template */
729static int add_control(struct hda_gen_spec *spec, int type, const char *name,
730 int cidx, unsigned long val)
731{
732 struct snd_kcontrol_new *knew;
733
Takashi Iwai12c93df2012-12-19 14:38:33 +0100734 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100735 if (!knew)
736 return -ENOMEM;
737 knew->index = cidx;
738 if (get_amp_nid_(val))
739 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
740 knew->private_value = val;
741 return 0;
742}
743
744static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
745 const char *pfx, const char *dir,
746 const char *sfx, int cidx, unsigned long val)
747{
748 char name[32];
749 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
750 return add_control(spec, type, name, cidx, val);
751}
752
753#define add_pb_vol_ctrl(spec, type, pfx, val) \
754 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
755#define add_pb_sw_ctrl(spec, type, pfx, val) \
756 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
757#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
758 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
759#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
760 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
761
762static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
763 unsigned int chs, struct nid_path *path)
764{
765 unsigned int val;
766 if (!path)
767 return 0;
768 val = path->ctls[NID_PATH_VOL_CTL];
769 if (!val)
770 return 0;
771 val = amp_val_replace_channels(val, chs);
772 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
773}
774
775/* return the channel bits suitable for the given path->ctls[] */
776static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
777 int type)
778{
779 int chs = 1; /* mono (left only) */
780 if (path) {
781 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
782 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
783 chs = 3; /* stereo */
784 }
785 return chs;
786}
787
788static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
789 struct nid_path *path)
790{
791 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
792 return add_vol_ctl(codec, pfx, cidx, chs, path);
793}
794
795/* create a mute-switch for the given mixer widget;
796 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
797 */
798static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
799 unsigned int chs, struct nid_path *path)
800{
801 unsigned int val;
802 int type = HDA_CTL_WIDGET_MUTE;
803
804 if (!path)
805 return 0;
806 val = path->ctls[NID_PATH_MUTE_CTL];
807 if (!val)
808 return 0;
809 val = amp_val_replace_channels(val, chs);
810 if (get_amp_direction_(val) == HDA_INPUT) {
811 hda_nid_t nid = get_amp_nid_(val);
812 int nums = snd_hda_get_num_conns(codec, nid);
813 if (nums > 1) {
814 type = HDA_CTL_BIND_MUTE;
815 val |= nums << 19;
816 }
817 }
818 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
819}
820
821static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
822 int cidx, struct nid_path *path)
823{
824 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
825 return add_sw_ctl(codec, pfx, cidx, chs, path);
826}
827
Takashi Iwai247d85e2013-01-17 16:18:11 +0100828/* any ctl assigned to the path with the given index? */
829static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
830{
831 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
832 return path && path->ctls[ctl_type];
833}
834
Takashi Iwai352f7f92012-12-19 12:52:06 +0100835static const char * const channel_name[4] = {
836 "Front", "Surround", "CLFE", "Side"
837};
838
839/* give some appropriate ctl name prefix for the given line out channel */
Takashi Iwai247d85e2013-01-17 16:18:11 +0100840static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
841 int *index, int ctl_type)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100842{
Takashi Iwai247d85e2013-01-17 16:18:11 +0100843 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100844 struct auto_pin_cfg *cfg = &spec->autocfg;
845
846 *index = 0;
847 if (cfg->line_outs == 1 && !spec->multi_ios &&
Takashi Iwai247d85e2013-01-17 16:18:11 +0100848 !cfg->hp_outs && !cfg->speaker_outs)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100849 return spec->vmaster_mute.hook ? "PCM" : "Master";
850
851 /* if there is really a single DAC used in the whole output paths,
852 * use it master (or "PCM" if a vmaster hook is present)
853 */
854 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
855 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
856 return spec->vmaster_mute.hook ? "PCM" : "Master";
857
Takashi Iwai247d85e2013-01-17 16:18:11 +0100858 /* multi-io channels */
859 if (ch >= cfg->line_outs)
860 return channel_name[ch];
861
Takashi Iwai352f7f92012-12-19 12:52:06 +0100862 switch (cfg->line_out_type) {
863 case AUTO_PIN_SPEAKER_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +0100864 /* if the primary channel vol/mute is shared with HP volume,
865 * don't name it as Speaker
866 */
867 if (!ch && cfg->hp_outs &&
868 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
869 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100870 if (cfg->line_outs == 1)
871 return "Speaker";
872 if (cfg->line_outs == 2)
873 return ch ? "Bass Speaker" : "Speaker";
874 break;
875 case AUTO_PIN_HP_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +0100876 /* if the primary channel vol/mute is shared with spk volume,
877 * don't name it as Headphone
878 */
879 if (!ch && cfg->speaker_outs &&
880 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
881 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100882 /* for multi-io case, only the primary out */
883 if (ch && spec->multi_ios)
884 break;
885 *index = ch;
886 return "Headphone";
Takashi Iwai352f7f92012-12-19 12:52:06 +0100887 }
Takashi Iwai247d85e2013-01-17 16:18:11 +0100888
889 /* for a single channel output, we don't have to name the channel */
890 if (cfg->line_outs == 1 && !spec->multi_ios)
891 return "PCM";
892
Takashi Iwai352f7f92012-12-19 12:52:06 +0100893 if (ch >= ARRAY_SIZE(channel_name)) {
894 snd_BUG();
895 return "PCM";
896 }
897
898 return channel_name[ch];
899}
900
901/*
902 * Parse output paths
903 */
904
905/* badness definition */
906enum {
907 /* No primary DAC is found for the main output */
908 BAD_NO_PRIMARY_DAC = 0x10000,
909 /* No DAC is found for the extra output */
910 BAD_NO_DAC = 0x4000,
911 /* No possible multi-ios */
912 BAD_MULTI_IO = 0x103,
913 /* No individual DAC for extra output */
914 BAD_NO_EXTRA_DAC = 0x102,
915 /* No individual DAC for extra surrounds */
916 BAD_NO_EXTRA_SURR_DAC = 0x101,
917 /* Primary DAC shared with main surrounds */
918 BAD_SHARED_SURROUND = 0x100,
919 /* Primary DAC shared with main CLFE */
920 BAD_SHARED_CLFE = 0x10,
921 /* Primary DAC shared with extra surrounds */
922 BAD_SHARED_EXTRA_SURROUND = 0x10,
923 /* Volume widget is shared */
924 BAD_SHARED_VOL = 0x10,
925};
926
Takashi Iwai0e614dd2013-01-07 15:11:44 +0100927/* look for widgets in the given path which are appropriate for
Takashi Iwai352f7f92012-12-19 12:52:06 +0100928 * volume and mute controls, and assign the values to ctls[].
929 *
930 * When no appropriate widget is found in the path, the badness value
931 * is incremented depending on the situation. The function returns the
932 * total badness for both volume and mute controls.
933 */
Takashi Iwai0e614dd2013-01-07 15:11:44 +0100934static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100935{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100936 hda_nid_t nid;
937 unsigned int val;
938 int badness = 0;
939
940 if (!path)
941 return BAD_SHARED_VOL * 2;
Takashi Iwai0e614dd2013-01-07 15:11:44 +0100942
943 if (path->ctls[NID_PATH_VOL_CTL] ||
944 path->ctls[NID_PATH_MUTE_CTL])
945 return 0; /* already evaluated */
946
Takashi Iwai352f7f92012-12-19 12:52:06 +0100947 nid = look_for_out_vol_nid(codec, path);
948 if (nid) {
949 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
950 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
951 badness += BAD_SHARED_VOL;
952 else
953 path->ctls[NID_PATH_VOL_CTL] = val;
954 } else
955 badness += BAD_SHARED_VOL;
956 nid = look_for_out_mute_nid(codec, path);
957 if (nid) {
958 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
959 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
960 nid_has_mute(codec, nid, HDA_OUTPUT))
961 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
962 else
963 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
964 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
965 badness += BAD_SHARED_VOL;
966 else
967 path->ctls[NID_PATH_MUTE_CTL] = val;
968 } else
969 badness += BAD_SHARED_VOL;
970 return badness;
971}
972
973struct badness_table {
974 int no_primary_dac; /* no primary DAC */
975 int no_dac; /* no secondary DACs */
976 int shared_primary; /* primary DAC is shared with main output */
977 int shared_surr; /* secondary DAC shared with main or primary */
978 int shared_clfe; /* third DAC shared with main or primary */
979 int shared_surr_main; /* secondary DAC sahred with main/DAC0 */
980};
981
982static struct badness_table main_out_badness = {
983 .no_primary_dac = BAD_NO_PRIMARY_DAC,
984 .no_dac = BAD_NO_DAC,
985 .shared_primary = BAD_NO_PRIMARY_DAC,
986 .shared_surr = BAD_SHARED_SURROUND,
987 .shared_clfe = BAD_SHARED_CLFE,
988 .shared_surr_main = BAD_SHARED_SURROUND,
989};
990
991static struct badness_table extra_out_badness = {
992 .no_primary_dac = BAD_NO_DAC,
993 .no_dac = BAD_NO_DAC,
994 .shared_primary = BAD_NO_EXTRA_DAC,
995 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
996 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
997 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
998};
999
Takashi Iwai7385df62013-01-07 09:50:52 +01001000/* get the DAC of the primary output corresponding to the given array index */
1001static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1002{
1003 struct hda_gen_spec *spec = codec->spec;
1004 struct auto_pin_cfg *cfg = &spec->autocfg;
1005
1006 if (cfg->line_outs > idx)
1007 return spec->private_dac_nids[idx];
1008 idx -= cfg->line_outs;
1009 if (spec->multi_ios > idx)
1010 return spec->multi_io[idx].dac;
1011 return 0;
1012}
1013
1014/* return the DAC if it's reachable, otherwise zero */
1015static inline hda_nid_t try_dac(struct hda_codec *codec,
1016 hda_nid_t dac, hda_nid_t pin)
1017{
1018 return is_reachable_path(codec, dac, pin) ? dac : 0;
1019}
1020
Takashi Iwai352f7f92012-12-19 12:52:06 +01001021/* try to assign DACs to pins and return the resultant badness */
1022static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1023 const hda_nid_t *pins, hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001024 int *path_idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001025 const struct badness_table *bad)
1026{
1027 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001028 int i, j;
1029 int badness = 0;
1030 hda_nid_t dac;
1031
1032 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 return 0;
1034
Takashi Iwai352f7f92012-12-19 12:52:06 +01001035 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001036 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001037 hda_nid_t pin = pins[i];
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001038
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001039 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1040 if (path) {
1041 badness += assign_out_path_ctls(codec, path);
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001042 continue;
1043 }
1044
1045 dacs[i] = look_for_dac(codec, pin, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001046 if (!dacs[i] && !i) {
Takashi Iwai980428c2013-01-09 09:28:20 +01001047 /* try to steal the DAC of surrounds for the front */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001048 for (j = 1; j < num_outs; j++) {
1049 if (is_reachable_path(codec, dacs[j], pin)) {
1050 dacs[0] = dacs[j];
1051 dacs[j] = 0;
Takashi Iwai980428c2013-01-09 09:28:20 +01001052 invalidate_nid_path(codec, path_idx[j]);
Takashi Iwai196c17662013-01-04 15:01:40 +01001053 path_idx[j] = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001054 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 }
1056 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001057 }
1058 dac = dacs[i];
1059 if (!dac) {
Takashi Iwai7385df62013-01-07 09:50:52 +01001060 if (num_outs > 2)
1061 dac = try_dac(codec, get_primary_out(codec, i), pin);
1062 if (!dac)
1063 dac = try_dac(codec, dacs[0], pin);
1064 if (!dac)
1065 dac = try_dac(codec, get_primary_out(codec, i), pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001066 if (dac) {
1067 if (!i)
1068 badness += bad->shared_primary;
1069 else if (i == 1)
1070 badness += bad->shared_surr;
1071 else
1072 badness += bad->shared_clfe;
1073 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1074 dac = spec->private_dac_nids[0];
1075 badness += bad->shared_surr_main;
1076 } else if (!i)
1077 badness += bad->no_primary_dac;
1078 else
1079 badness += bad->no_dac;
1080 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001081 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001082 if (!path && !i && spec->mixer_nid) {
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001083 /* try with aamix */
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001084 path = snd_hda_add_new_path(codec, dac, pin, 0);
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001085 }
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001086 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001087 dac = dacs[i] = 0;
Takashi Iwaie1284af2013-01-03 16:33:02 +01001088 else {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001089 print_nid_path("output", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001090 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001091 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001092 badness += assign_out_path_ctls(codec, path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001093 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001094 }
1095
1096 return badness;
1097}
1098
1099/* return NID if the given pin has only a single connection to a certain DAC */
1100static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1101{
1102 struct hda_gen_spec *spec = codec->spec;
1103 int i;
1104 hda_nid_t nid_found = 0;
1105
1106 for (i = 0; i < spec->num_all_dacs; i++) {
1107 hda_nid_t nid = spec->all_dacs[i];
1108 if (!nid || is_dac_already_used(codec, nid))
1109 continue;
1110 if (is_reachable_path(codec, nid, pin)) {
1111 if (nid_found)
1112 return 0;
1113 nid_found = nid;
1114 }
1115 }
1116 return nid_found;
1117}
1118
1119/* check whether the given pin can be a multi-io pin */
1120static bool can_be_multiio_pin(struct hda_codec *codec,
1121 unsigned int location, hda_nid_t nid)
1122{
1123 unsigned int defcfg, caps;
1124
1125 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1126 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1127 return false;
1128 if (location && get_defcfg_location(defcfg) != location)
1129 return false;
1130 caps = snd_hda_query_pin_caps(codec, nid);
1131 if (!(caps & AC_PINCAP_OUT))
1132 return false;
1133 return true;
1134}
1135
Takashi Iwaie22aab72013-01-04 14:50:04 +01001136/* count the number of input pins that are capable to be multi-io */
1137static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1138{
1139 struct hda_gen_spec *spec = codec->spec;
1140 struct auto_pin_cfg *cfg = &spec->autocfg;
1141 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1142 unsigned int location = get_defcfg_location(defcfg);
1143 int type, i;
1144 int num_pins = 0;
1145
1146 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1147 for (i = 0; i < cfg->num_inputs; i++) {
1148 if (cfg->inputs[i].type != type)
1149 continue;
1150 if (can_be_multiio_pin(codec, location,
1151 cfg->inputs[i].pin))
1152 num_pins++;
1153 }
1154 }
1155 return num_pins;
1156}
1157
Takashi Iwai352f7f92012-12-19 12:52:06 +01001158/*
1159 * multi-io helper
1160 *
1161 * When hardwired is set, try to fill ony hardwired pins, and returns
1162 * zero if any pins are filled, non-zero if nothing found.
1163 * When hardwired is off, try to fill possible input pins, and returns
1164 * the badness value.
1165 */
1166static int fill_multi_ios(struct hda_codec *codec,
1167 hda_nid_t reference_pin,
Takashi Iwaie22aab72013-01-04 14:50:04 +01001168 bool hardwired)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001169{
1170 struct hda_gen_spec *spec = codec->spec;
1171 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie22aab72013-01-04 14:50:04 +01001172 int type, i, j, num_pins, old_pins;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001173 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1174 unsigned int location = get_defcfg_location(defcfg);
1175 int badness = 0;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001176 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001177
1178 old_pins = spec->multi_ios;
1179 if (old_pins >= 2)
1180 goto end_fill;
1181
Takashi Iwaie22aab72013-01-04 14:50:04 +01001182 num_pins = count_multiio_pins(codec, reference_pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001183 if (num_pins < 2)
1184 goto end_fill;
1185
Takashi Iwai352f7f92012-12-19 12:52:06 +01001186 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1187 for (i = 0; i < cfg->num_inputs; i++) {
1188 hda_nid_t nid = cfg->inputs[i].pin;
1189 hda_nid_t dac = 0;
1190
1191 if (cfg->inputs[i].type != type)
1192 continue;
1193 if (!can_be_multiio_pin(codec, location, nid))
1194 continue;
1195 for (j = 0; j < spec->multi_ios; j++) {
1196 if (nid == spec->multi_io[j].pin)
1197 break;
1198 }
1199 if (j < spec->multi_ios)
1200 continue;
1201
Takashi Iwai352f7f92012-12-19 12:52:06 +01001202 if (hardwired)
1203 dac = get_dac_if_single(codec, nid);
1204 else if (!dac)
1205 dac = look_for_dac(codec, nid, false);
1206 if (!dac) {
1207 badness++;
1208 continue;
1209 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001210 path = snd_hda_add_new_path(codec, dac, nid,
1211 -spec->mixer_nid);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001212 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001213 badness++;
1214 continue;
1215 }
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001216 print_nid_path("multiio", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001217 spec->multi_io[spec->multi_ios].pin = nid;
1218 spec->multi_io[spec->multi_ios].dac = dac;
Takashi Iwai196c17662013-01-04 15:01:40 +01001219 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1220 snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001221 spec->multi_ios++;
1222 if (spec->multi_ios >= 2)
1223 break;
1224 }
1225 }
1226 end_fill:
1227 if (badness)
1228 badness = BAD_MULTI_IO;
1229 if (old_pins == spec->multi_ios) {
1230 if (hardwired)
1231 return 1; /* nothing found */
1232 else
1233 return badness; /* no badness if nothing found */
1234 }
1235 if (!hardwired && spec->multi_ios < 2) {
1236 /* cancel newly assigned paths */
1237 spec->paths.used -= spec->multi_ios - old_pins;
1238 spec->multi_ios = old_pins;
1239 return badness;
1240 }
1241
1242 /* assign volume and mute controls */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001243 for (i = old_pins; i < spec->multi_ios; i++) {
1244 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1245 badness += assign_out_path_ctls(codec, path);
1246 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001247
1248 return badness;
1249}
1250
1251/* map DACs for all pins in the list if they are single connections */
1252static bool map_singles(struct hda_codec *codec, int outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001253 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001254{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001255 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001256 int i;
1257 bool found = false;
1258 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001259 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001260 hda_nid_t dac;
1261 if (dacs[i])
1262 continue;
1263 dac = get_dac_if_single(codec, pins[i]);
1264 if (!dac)
1265 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001266 path = snd_hda_add_new_path(codec, dac, pins[i],
1267 -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001268 if (!path && !i && spec->mixer_nid)
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001269 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001270 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001271 dacs[i] = dac;
1272 found = true;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001273 print_nid_path("output", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001274 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001275 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001276 }
1277 }
1278 return found;
1279}
1280
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001281/* create a new path including aamix if available, and return its index */
1282static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1283{
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001284 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001285 struct nid_path *path;
1286
1287 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001288 if (!path || !path->depth ||
1289 is_nid_contained(path, spec->mixer_nid))
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001290 return 0;
1291 path = snd_hda_add_new_path(codec, path->path[0],
1292 path->path[path->depth - 1],
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001293 spec->mixer_nid);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001294 if (!path)
1295 return 0;
1296 print_nid_path("output-aamix", path);
1297 path->active = false; /* unused as default */
1298 return snd_hda_get_path_idx(codec, path);
1299}
1300
Takashi Iwaia07a9492013-01-07 16:44:06 +01001301/* fill the empty entries in the dac array for speaker/hp with the
1302 * shared dac pointed by the paths
1303 */
1304static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1305 hda_nid_t *dacs, int *path_idx)
1306{
1307 struct nid_path *path;
1308 int i;
1309
1310 for (i = 0; i < num_outs; i++) {
1311 if (dacs[i])
1312 continue;
1313 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1314 if (!path)
1315 continue;
1316 dacs[i] = path->path[0];
1317 }
1318}
1319
Takashi Iwai352f7f92012-12-19 12:52:06 +01001320/* fill in the dac_nids table from the parsed pin configuration */
1321static int fill_and_eval_dacs(struct hda_codec *codec,
1322 bool fill_hardwired,
1323 bool fill_mio_first)
1324{
1325 struct hda_gen_spec *spec = codec->spec;
1326 struct auto_pin_cfg *cfg = &spec->autocfg;
1327 int i, err, badness;
Takashi Iwaiea46c3c2013-01-15 18:45:53 +01001328 unsigned int val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001329
1330 /* set num_dacs once to full for look_for_dac() */
1331 spec->multiout.num_dacs = cfg->line_outs;
1332 spec->multiout.dac_nids = spec->private_dac_nids;
1333 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1334 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1335 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1336 spec->multi_ios = 0;
1337 snd_array_free(&spec->paths);
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001338
1339 /* clear path indices */
1340 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1341 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1342 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1343 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1344 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
Takashi Iwaic697b712013-01-07 17:09:26 +01001345 memset(spec->input_paths, 0, sizeof(spec->input_paths));
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001346 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1347 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1348
Takashi Iwai352f7f92012-12-19 12:52:06 +01001349 badness = 0;
1350
1351 /* fill hard-wired DACs first */
1352 if (fill_hardwired) {
1353 bool mapped;
1354 do {
1355 mapped = map_singles(codec, cfg->line_outs,
1356 cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001357 spec->private_dac_nids,
1358 spec->out_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001359 mapped |= map_singles(codec, cfg->hp_outs,
1360 cfg->hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001361 spec->multiout.hp_out_nid,
1362 spec->hp_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001363 mapped |= map_singles(codec, cfg->speaker_outs,
1364 cfg->speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001365 spec->multiout.extra_out_nid,
1366 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001367 if (fill_mio_first && cfg->line_outs == 1 &&
1368 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001369 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001370 if (!err)
1371 mapped = true;
1372 }
1373 } while (mapped);
1374 }
1375
1376 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001377 spec->private_dac_nids, spec->out_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001378 &main_out_badness);
1379
Takashi Iwai352f7f92012-12-19 12:52:06 +01001380 if (fill_mio_first &&
1381 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1382 /* try to fill multi-io first */
Takashi Iwaie22aab72013-01-04 14:50:04 +01001383 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001384 if (err < 0)
1385 return err;
1386 /* we don't count badness at this stage yet */
1387 }
1388
1389 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1390 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1391 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001392 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001393 &extra_out_badness);
1394 if (err < 0)
1395 return err;
1396 badness += err;
1397 }
1398 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1399 err = try_assign_dacs(codec, cfg->speaker_outs,
1400 cfg->speaker_pins,
1401 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001402 spec->speaker_paths,
1403 &extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001404 if (err < 0)
1405 return err;
1406 badness += err;
1407 }
1408 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001409 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001410 if (err < 0)
1411 return err;
1412 badness += err;
1413 }
Takashi Iwaie22aab72013-01-04 14:50:04 +01001414
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001415 if (spec->mixer_nid) {
1416 spec->aamix_out_paths[0] =
1417 check_aamix_out_path(codec, spec->out_paths[0]);
1418 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1419 spec->aamix_out_paths[1] =
1420 check_aamix_out_path(codec, spec->hp_paths[0]);
1421 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1422 spec->aamix_out_paths[2] =
1423 check_aamix_out_path(codec, spec->speaker_paths[0]);
1424 }
1425
Takashi Iwaie22aab72013-01-04 14:50:04 +01001426 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1427 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1428 spec->multi_ios = 1; /* give badness */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001429
Takashi Iwaia07a9492013-01-07 16:44:06 +01001430 /* re-count num_dacs and squash invalid entries */
1431 spec->multiout.num_dacs = 0;
1432 for (i = 0; i < cfg->line_outs; i++) {
1433 if (spec->private_dac_nids[i])
1434 spec->multiout.num_dacs++;
1435 else {
1436 memmove(spec->private_dac_nids + i,
1437 spec->private_dac_nids + i + 1,
1438 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1439 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1440 }
1441 }
1442
1443 spec->ext_channel_count = spec->min_channel_count =
David Henningssonc0f3b212013-01-16 11:45:37 +01001444 spec->multiout.num_dacs * 2;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001445
Takashi Iwai352f7f92012-12-19 12:52:06 +01001446 if (spec->multi_ios == 2) {
1447 for (i = 0; i < 2; i++)
1448 spec->private_dac_nids[spec->multiout.num_dacs++] =
1449 spec->multi_io[i].dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001450 } else if (spec->multi_ios) {
1451 spec->multi_ios = 0;
1452 badness += BAD_MULTI_IO;
1453 }
1454
Takashi Iwaia07a9492013-01-07 16:44:06 +01001455 /* re-fill the shared DAC for speaker / headphone */
1456 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1457 refill_shared_dacs(codec, cfg->hp_outs,
1458 spec->multiout.hp_out_nid,
1459 spec->hp_paths);
1460 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1461 refill_shared_dacs(codec, cfg->speaker_outs,
1462 spec->multiout.extra_out_nid,
1463 spec->speaker_paths);
1464
Takashi Iwai2c12c302013-01-10 09:33:29 +01001465 /* set initial pinctl targets */
Takashi Iwaiea46c3c2013-01-15 18:45:53 +01001466 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
1467 val = PIN_HP;
1468 else
1469 val = PIN_OUT;
1470 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
Takashi Iwai2c12c302013-01-10 09:33:29 +01001471 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1472 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
Takashi Iwaiea46c3c2013-01-15 18:45:53 +01001473 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1474 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
Takashi Iwai2c12c302013-01-10 09:33:29 +01001475 set_pin_targets(codec, cfg->speaker_outs,
Takashi Iwaiea46c3c2013-01-15 18:45:53 +01001476 cfg->speaker_pins, val);
1477 }
Takashi Iwai2c12c302013-01-10 09:33:29 +01001478
Takashi Iwai352f7f92012-12-19 12:52:06 +01001479 return badness;
1480}
1481
1482#define DEBUG_BADNESS
1483
1484#ifdef DEBUG_BADNESS
1485#define debug_badness snd_printdd
1486#else
1487#define debug_badness(...)
1488#endif
1489
1490static void debug_show_configs(struct hda_gen_spec *spec, struct auto_pin_cfg *cfg)
1491{
1492 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1493 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001494 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001495 spec->multiout.dac_nids[0],
1496 spec->multiout.dac_nids[1],
1497 spec->multiout.dac_nids[2],
1498 spec->multiout.dac_nids[3]);
1499 if (spec->multi_ios > 0)
1500 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1501 spec->multi_ios,
1502 spec->multi_io[0].pin, spec->multi_io[1].pin,
1503 spec->multi_io[0].dac, spec->multi_io[1].dac);
1504 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1505 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001506 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001507 spec->multiout.hp_out_nid[0],
1508 spec->multiout.hp_out_nid[1],
1509 spec->multiout.hp_out_nid[2],
1510 spec->multiout.hp_out_nid[3]);
1511 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1512 cfg->speaker_pins[0], cfg->speaker_pins[1],
1513 cfg->speaker_pins[2], cfg->speaker_pins[3],
1514 spec->multiout.extra_out_nid[0],
1515 spec->multiout.extra_out_nid[1],
1516 spec->multiout.extra_out_nid[2],
1517 spec->multiout.extra_out_nid[3]);
1518}
1519
1520/* find all available DACs of the codec */
1521static void fill_all_dac_nids(struct hda_codec *codec)
1522{
1523 struct hda_gen_spec *spec = codec->spec;
1524 int i;
1525 hda_nid_t nid = codec->start_nid;
1526
1527 spec->num_all_dacs = 0;
1528 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1529 for (i = 0; i < codec->num_nodes; i++, nid++) {
1530 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1531 continue;
1532 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1533 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1534 break;
1535 }
1536 spec->all_dacs[spec->num_all_dacs++] = nid;
1537 }
1538}
1539
1540static int parse_output_paths(struct hda_codec *codec)
1541{
1542 struct hda_gen_spec *spec = codec->spec;
1543 struct auto_pin_cfg *cfg = &spec->autocfg;
1544 struct auto_pin_cfg *best_cfg;
1545 int best_badness = INT_MAX;
1546 int badness;
1547 bool fill_hardwired = true, fill_mio_first = true;
1548 bool best_wired = true, best_mio = true;
1549 bool hp_spk_swapped = false;
1550
Takashi Iwai352f7f92012-12-19 12:52:06 +01001551 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1552 if (!best_cfg)
1553 return -ENOMEM;
1554 *best_cfg = *cfg;
1555
1556 for (;;) {
1557 badness = fill_and_eval_dacs(codec, fill_hardwired,
1558 fill_mio_first);
1559 if (badness < 0) {
1560 kfree(best_cfg);
1561 return badness;
1562 }
1563 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1564 cfg->line_out_type, fill_hardwired, fill_mio_first,
1565 badness);
1566 debug_show_configs(spec, cfg);
1567 if (badness < best_badness) {
1568 best_badness = badness;
1569 *best_cfg = *cfg;
1570 best_wired = fill_hardwired;
1571 best_mio = fill_mio_first;
1572 }
1573 if (!badness)
1574 break;
1575 fill_mio_first = !fill_mio_first;
1576 if (!fill_mio_first)
1577 continue;
1578 fill_hardwired = !fill_hardwired;
1579 if (!fill_hardwired)
1580 continue;
1581 if (hp_spk_swapped)
1582 break;
1583 hp_spk_swapped = true;
1584 if (cfg->speaker_outs > 0 &&
1585 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1586 cfg->hp_outs = cfg->line_outs;
1587 memcpy(cfg->hp_pins, cfg->line_out_pins,
1588 sizeof(cfg->hp_pins));
1589 cfg->line_outs = cfg->speaker_outs;
1590 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1591 sizeof(cfg->speaker_pins));
1592 cfg->speaker_outs = 0;
1593 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1594 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1595 fill_hardwired = true;
1596 continue;
1597 }
1598 if (cfg->hp_outs > 0 &&
1599 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1600 cfg->speaker_outs = cfg->line_outs;
1601 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1602 sizeof(cfg->speaker_pins));
1603 cfg->line_outs = cfg->hp_outs;
1604 memcpy(cfg->line_out_pins, cfg->hp_pins,
1605 sizeof(cfg->hp_pins));
1606 cfg->hp_outs = 0;
1607 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1608 cfg->line_out_type = AUTO_PIN_HP_OUT;
1609 fill_hardwired = true;
1610 continue;
1611 }
1612 break;
1613 }
1614
1615 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001616 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001617 *cfg = *best_cfg;
1618 fill_and_eval_dacs(codec, best_wired, best_mio);
1619 }
1620 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1621 cfg->line_out_type, best_wired, best_mio);
1622 debug_show_configs(spec, cfg);
1623
1624 if (cfg->line_out_pins[0]) {
1625 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01001626 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001627 if (path)
1628 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01001629 if (spec->vmaster_nid)
1630 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
1631 HDA_OUTPUT, spec->vmaster_tlv);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001632 }
1633
1634 kfree(best_cfg);
1635 return 0;
1636}
1637
1638/* add playback controls from the parsed DAC table */
1639static int create_multi_out_ctls(struct hda_codec *codec,
1640 const struct auto_pin_cfg *cfg)
1641{
1642 struct hda_gen_spec *spec = codec->spec;
1643 int i, err, noutputs;
1644
1645 noutputs = cfg->line_outs;
1646 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1647 noutputs += spec->multi_ios;
1648
1649 for (i = 0; i < noutputs; i++) {
1650 const char *name;
1651 int index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001652 struct nid_path *path;
1653
Takashi Iwai196c17662013-01-04 15:01:40 +01001654 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001655 if (!path)
1656 continue;
Takashi Iwai247d85e2013-01-17 16:18:11 +01001657
1658 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001659 if (!name || !strcmp(name, "CLFE")) {
1660 /* Center/LFE */
1661 err = add_vol_ctl(codec, "Center", 0, 1, path);
1662 if (err < 0)
1663 return err;
1664 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1665 if (err < 0)
1666 return err;
Takashi Iwai247d85e2013-01-17 16:18:11 +01001667 } else {
1668 err = add_stereo_vol(codec, name, index, path);
1669 if (err < 0)
1670 return err;
1671 }
1672
1673 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
1674 if (!name || !strcmp(name, "CLFE")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001675 err = add_sw_ctl(codec, "Center", 0, 1, path);
1676 if (err < 0)
1677 return err;
1678 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1679 if (err < 0)
1680 return err;
1681 } else {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001682 err = add_stereo_sw(codec, name, index, path);
1683 if (err < 0)
1684 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 }
1686 }
1687 return 0;
1688}
1689
Takashi Iwaic2c80382013-01-07 10:33:57 +01001690static int create_extra_out(struct hda_codec *codec, int path_idx,
Takashi Iwai196c17662013-01-04 15:01:40 +01001691 const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001693 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 int err;
1695
Takashi Iwai196c17662013-01-04 15:01:40 +01001696 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001697 if (!path)
1698 return 0;
Takashi Iwaic2c80382013-01-07 10:33:57 +01001699 err = add_stereo_vol(codec, pfx, cidx, path);
1700 if (err < 0)
1701 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001702 err = add_stereo_sw(codec, pfx, cidx, path);
1703 if (err < 0)
1704 return err;
1705 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706}
1707
Takashi Iwai352f7f92012-12-19 12:52:06 +01001708/* add playback controls for speaker and HP outputs */
1709static int create_extra_outs(struct hda_codec *codec, int num_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001710 const int *paths, const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711{
Takashi Iwaic2c80382013-01-07 10:33:57 +01001712 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001713
1714 for (i = 0; i < num_pins; i++) {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001715 const char *name;
1716 char tmp[44];
1717 int err, idx = 0;
1718
1719 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
1720 name = "Bass Speaker";
1721 else if (num_pins >= 3) {
1722 snprintf(tmp, sizeof(tmp), "%s %s",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001723 pfx, channel_name[i]);
Takashi Iwaic2c80382013-01-07 10:33:57 +01001724 name = tmp;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001725 } else {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001726 name = pfx;
1727 idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 }
Takashi Iwaic2c80382013-01-07 10:33:57 +01001729 err = create_extra_out(codec, paths[i], name, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001730 if (err < 0)
1731 return err;
1732 }
1733 return 0;
1734}
Takashi Iwai97ec5582006-03-21 11:29:07 +01001735
Takashi Iwai352f7f92012-12-19 12:52:06 +01001736static int create_hp_out_ctls(struct hda_codec *codec)
1737{
1738 struct hda_gen_spec *spec = codec->spec;
1739 return create_extra_outs(codec, spec->autocfg.hp_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001740 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001741 "Headphone");
1742}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
Takashi Iwai352f7f92012-12-19 12:52:06 +01001744static int create_speaker_out_ctls(struct hda_codec *codec)
1745{
1746 struct hda_gen_spec *spec = codec->spec;
1747 return create_extra_outs(codec, spec->autocfg.speaker_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001748 spec->speaker_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001749 "Speaker");
1750}
1751
1752/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001753 * independent HP controls
1754 */
1755
1756static int indep_hp_info(struct snd_kcontrol *kcontrol,
1757 struct snd_ctl_elem_info *uinfo)
1758{
1759 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
1760}
1761
1762static int indep_hp_get(struct snd_kcontrol *kcontrol,
1763 struct snd_ctl_elem_value *ucontrol)
1764{
1765 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1766 struct hda_gen_spec *spec = codec->spec;
1767 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
1768 return 0;
1769}
1770
1771static int indep_hp_put(struct snd_kcontrol *kcontrol,
1772 struct snd_ctl_elem_value *ucontrol)
1773{
1774 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1775 struct hda_gen_spec *spec = codec->spec;
1776 unsigned int select = ucontrol->value.enumerated.item[0];
1777 int ret = 0;
1778
1779 mutex_lock(&spec->pcm_mutex);
1780 if (spec->active_streams) {
1781 ret = -EBUSY;
1782 goto unlock;
1783 }
1784
1785 if (spec->indep_hp_enabled != select) {
1786 spec->indep_hp_enabled = select;
1787 if (spec->indep_hp_enabled)
1788 spec->multiout.hp_out_nid[0] = 0;
1789 else
1790 spec->multiout.hp_out_nid[0] = spec->alt_dac_nid;
1791 ret = 1;
1792 }
1793 unlock:
1794 mutex_unlock(&spec->pcm_mutex);
1795 return ret;
1796}
1797
1798static const struct snd_kcontrol_new indep_hp_ctl = {
1799 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1800 .name = "Independent HP",
1801 .info = indep_hp_info,
1802 .get = indep_hp_get,
1803 .put = indep_hp_put,
1804};
1805
1806
1807static int create_indep_hp_ctls(struct hda_codec *codec)
1808{
1809 struct hda_gen_spec *spec = codec->spec;
1810
1811 if (!spec->indep_hp)
1812 return 0;
1813 if (!spec->multiout.hp_out_nid[0]) {
1814 spec->indep_hp = 0;
1815 return 0;
1816 }
1817
1818 spec->indep_hp_enabled = false;
1819 spec->alt_dac_nid = spec->multiout.hp_out_nid[0];
1820 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
1821 return -ENOMEM;
1822 return 0;
1823}
1824
1825/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01001826 * channel mode enum control
1827 */
1828
1829static int ch_mode_info(struct snd_kcontrol *kcontrol,
1830 struct snd_ctl_elem_info *uinfo)
1831{
1832 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1833 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001834 int chs;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001835
1836 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1837 uinfo->count = 1;
1838 uinfo->value.enumerated.items = spec->multi_ios + 1;
1839 if (uinfo->value.enumerated.item > spec->multi_ios)
1840 uinfo->value.enumerated.item = spec->multi_ios;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001841 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
1842 sprintf(uinfo->value.enumerated.name, "%dch", chs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001843 return 0;
1844}
1845
1846static int ch_mode_get(struct snd_kcontrol *kcontrol,
1847 struct snd_ctl_elem_value *ucontrol)
1848{
1849 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1850 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001851 ucontrol->value.enumerated.item[0] =
1852 (spec->ext_channel_count - spec->min_channel_count) / 2;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001853 return 0;
1854}
1855
Takashi Iwai196c17662013-01-04 15:01:40 +01001856static inline struct nid_path *
1857get_multiio_path(struct hda_codec *codec, int idx)
1858{
1859 struct hda_gen_spec *spec = codec->spec;
1860 return snd_hda_get_path_from_idx(codec,
1861 spec->out_paths[spec->autocfg.line_outs + idx]);
1862}
1863
Takashi Iwaia5cc2502013-01-16 18:08:55 +01001864static void update_automute_all(struct hda_codec *codec);
1865
Takashi Iwai352f7f92012-12-19 12:52:06 +01001866static int set_multi_io(struct hda_codec *codec, int idx, bool output)
1867{
1868 struct hda_gen_spec *spec = codec->spec;
1869 hda_nid_t nid = spec->multi_io[idx].pin;
1870 struct nid_path *path;
1871
Takashi Iwai196c17662013-01-04 15:01:40 +01001872 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001873 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001875
1876 if (path->active == output)
1877 return 0;
1878
1879 if (output) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01001880 set_pin_target(codec, nid, PIN_OUT, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001881 snd_hda_activate_path(codec, path, true, true);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01001882 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001883 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01001884 set_pin_eapd(codec, nid, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001885 snd_hda_activate_path(codec, path, false, true);
Takashi Iwai2c12c302013-01-10 09:33:29 +01001886 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 }
Takashi Iwaia365fed2013-01-10 16:10:06 +01001888
1889 /* update jack retasking in case it modifies any of them */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01001890 update_automute_all(codec);
Takashi Iwaia365fed2013-01-10 16:10:06 +01001891
Takashi Iwai352f7f92012-12-19 12:52:06 +01001892 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893}
1894
Takashi Iwai352f7f92012-12-19 12:52:06 +01001895static int ch_mode_put(struct snd_kcontrol *kcontrol,
1896 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001898 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1899 struct hda_gen_spec *spec = codec->spec;
1900 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
Takashi Iwai352f7f92012-12-19 12:52:06 +01001902 ch = ucontrol->value.enumerated.item[0];
1903 if (ch < 0 || ch > spec->multi_ios)
1904 return -EINVAL;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001905 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001906 return 0;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001907 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001908 for (i = 0; i < spec->multi_ios; i++)
1909 set_multi_io(codec, i, i < ch);
1910 spec->multiout.max_channels = max(spec->ext_channel_count,
1911 spec->const_channel_count);
1912 if (spec->need_dac_fix)
1913 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 return 1;
1915}
1916
Takashi Iwai352f7f92012-12-19 12:52:06 +01001917static const struct snd_kcontrol_new channel_mode_enum = {
1918 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1919 .name = "Channel Mode",
1920 .info = ch_mode_info,
1921 .get = ch_mode_get,
1922 .put = ch_mode_put,
1923};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
Takashi Iwai352f7f92012-12-19 12:52:06 +01001925static int create_multi_channel_mode(struct hda_codec *codec)
1926{
1927 struct hda_gen_spec *spec = codec->spec;
1928
1929 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01001930 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01001931 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 return 0;
1934}
1935
Takashi Iwai352f7f92012-12-19 12:52:06 +01001936/*
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001937 * aamix loopback enable/disable switch
1938 */
1939
1940#define loopback_mixing_info indep_hp_info
1941
1942static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
1943 struct snd_ctl_elem_value *ucontrol)
1944{
1945 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1946 struct hda_gen_spec *spec = codec->spec;
1947 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
1948 return 0;
1949}
1950
1951static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
1952 int nomix_path_idx, int mix_path_idx)
1953{
1954 struct nid_path *nomix_path, *mix_path;
1955
1956 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
1957 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
1958 if (!nomix_path || !mix_path)
1959 return;
1960 if (do_mix) {
1961 snd_hda_activate_path(codec, nomix_path, false, true);
1962 snd_hda_activate_path(codec, mix_path, true, true);
1963 } else {
1964 snd_hda_activate_path(codec, mix_path, false, true);
1965 snd_hda_activate_path(codec, nomix_path, true, true);
1966 }
1967}
1968
1969static int loopback_mixing_put(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 unsigned int val = ucontrol->value.enumerated.item[0];
1975
1976 if (val == spec->aamix_mode)
1977 return 0;
1978 spec->aamix_mode = val;
1979 update_aamix_paths(codec, val, spec->out_paths[0],
1980 spec->aamix_out_paths[0]);
1981 update_aamix_paths(codec, val, spec->hp_paths[0],
1982 spec->aamix_out_paths[1]);
1983 update_aamix_paths(codec, val, spec->speaker_paths[0],
1984 spec->aamix_out_paths[2]);
1985 return 1;
1986}
1987
1988static const struct snd_kcontrol_new loopback_mixing_enum = {
1989 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1990 .name = "Loopback Mixing",
1991 .info = loopback_mixing_info,
1992 .get = loopback_mixing_get,
1993 .put = loopback_mixing_put,
1994};
1995
1996static int create_loopback_mixing_ctl(struct hda_codec *codec)
1997{
1998 struct hda_gen_spec *spec = codec->spec;
1999
2000 if (!spec->mixer_nid)
2001 return 0;
2002 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
2003 spec->aamix_out_paths[2]))
2004 return 0;
2005 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2006 return -ENOMEM;
2007 return 0;
2008}
2009
2010/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002011 * shared headphone/mic handling
2012 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002013
Takashi Iwai352f7f92012-12-19 12:52:06 +01002014static void call_update_outputs(struct hda_codec *codec);
2015
2016/* for shared I/O, change the pin-control accordingly */
2017static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic)
2018{
2019 struct hda_gen_spec *spec = codec->spec;
2020 unsigned int val;
2021 hda_nid_t pin = spec->autocfg.inputs[1].pin;
2022 /* NOTE: this assumes that there are only two inputs, the
2023 * first is the real internal mic and the second is HP/mic jack.
2024 */
2025
2026 val = snd_hda_get_default_vref(codec, pin);
2027
2028 /* This pin does not have vref caps - let's enable vref on pin 0x18
2029 instead, as suggested by Realtek */
2030 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2031 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2032 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2033 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01002034 snd_hda_set_pin_ctl_cache(codec, vref_pin,
2035 PIN_IN | (set_as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02002036 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002037
2038 val = set_as_mic ? val | PIN_IN : PIN_HP;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002039 set_pin_target(codec, pin, val, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002040
2041 spec->automute_speaker = !set_as_mic;
2042 call_update_outputs(codec);
2043}
2044
2045/* create a shared input with the headphone out */
2046static int create_shared_input(struct hda_codec *codec)
2047{
2048 struct hda_gen_spec *spec = codec->spec;
2049 struct auto_pin_cfg *cfg = &spec->autocfg;
2050 unsigned int defcfg;
2051 hda_nid_t nid;
2052
2053 /* only one internal input pin? */
2054 if (cfg->num_inputs != 1)
2055 return 0;
2056 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2057 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2058 return 0;
2059
2060 if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
2061 nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
2062 else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
2063 nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
2064 else
2065 return 0; /* both not available */
2066
2067 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2068 return 0; /* no input */
2069
2070 cfg->inputs[1].pin = nid;
2071 cfg->inputs[1].type = AUTO_PIN_MIC;
2072 cfg->num_inputs = 2;
2073 spec->shared_mic_hp = 1;
2074 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
2075 return 0;
2076}
2077
Takashi Iwai978e77e2013-01-10 16:57:58 +01002078/*
2079 * output jack mode
2080 */
2081static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2082 struct snd_ctl_elem_info *uinfo)
2083{
2084 static const char * const texts[] = {
2085 "Line Out", "Headphone Out",
2086 };
2087 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, texts);
2088}
2089
2090static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2091 struct snd_ctl_elem_value *ucontrol)
2092{
2093 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2094 hda_nid_t nid = kcontrol->private_value;
2095 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2096 ucontrol->value.enumerated.item[0] = 1;
2097 else
2098 ucontrol->value.enumerated.item[0] = 0;
2099 return 0;
2100}
2101
2102static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2103 struct snd_ctl_elem_value *ucontrol)
2104{
2105 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2106 hda_nid_t nid = kcontrol->private_value;
2107 unsigned int val;
2108
2109 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2110 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2111 return 0;
2112 snd_hda_set_pin_ctl_cache(codec, nid, val);
2113 return 1;
2114}
2115
2116static const struct snd_kcontrol_new out_jack_mode_enum = {
2117 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2118 .info = out_jack_mode_info,
2119 .get = out_jack_mode_get,
2120 .put = out_jack_mode_put,
2121};
2122
2123static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2124{
2125 struct hda_gen_spec *spec = codec->spec;
2126 int i;
2127
2128 for (i = 0; i < spec->kctls.used; i++) {
2129 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2130 if (!strcmp(kctl->name, name) && kctl->index == idx)
2131 return true;
2132 }
2133 return false;
2134}
2135
2136static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2137 char *name, size_t name_len)
2138{
2139 struct hda_gen_spec *spec = codec->spec;
2140 int idx = 0;
2141
2142 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2143 strlcat(name, " Jack Mode", name_len);
2144
2145 for (; find_kctl_name(codec, name, idx); idx++)
2146 ;
2147}
2148
2149static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2150 hda_nid_t *pins)
2151{
2152 struct hda_gen_spec *spec = codec->spec;
2153 int i;
2154
2155 for (i = 0; i < num_pins; i++) {
2156 hda_nid_t pin = pins[i];
2157 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2158 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV)) {
2159 struct snd_kcontrol_new *knew;
2160 char name[44];
2161 get_jack_mode_name(codec, pin, name, sizeof(name));
2162 knew = snd_hda_gen_add_kctl(spec, name,
2163 &out_jack_mode_enum);
2164 if (!knew)
2165 return -ENOMEM;
2166 knew->private_value = pin;
2167 }
2168 }
2169
2170 return 0;
2171}
2172
Takashi Iwai294765582013-01-17 09:52:11 +01002173/*
2174 * input jack mode
2175 */
2176
2177/* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2178#define NUM_VREFS 6
2179
2180static const char * const vref_texts[NUM_VREFS] = {
2181 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2182 "", "Mic 80pc Bias", "Mic 100pc Bias"
2183};
2184
2185static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2186{
2187 unsigned int pincap;
2188
2189 pincap = snd_hda_query_pin_caps(codec, pin);
2190 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2191 /* filter out unusual vrefs */
2192 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2193 return pincap;
2194}
2195
2196/* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2197static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2198{
2199 unsigned int i, n = 0;
2200
2201 for (i = 0; i < NUM_VREFS; i++) {
2202 if (vref_caps & (1 << i)) {
2203 if (n == item_idx)
2204 return i;
2205 n++;
2206 }
2207 }
2208 return 0;
2209}
2210
2211/* convert back from the vref ctl index to the enum item index */
2212static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2213{
2214 unsigned int i, n = 0;
2215
2216 for (i = 0; i < NUM_VREFS; i++) {
2217 if (i == idx)
2218 return n;
2219 if (vref_caps & (1 << i))
2220 n++;
2221 }
2222 return 0;
2223}
2224
2225static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2226 struct snd_ctl_elem_info *uinfo)
2227{
2228 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2229 hda_nid_t nid = kcontrol->private_value;
2230 unsigned int vref_caps = get_vref_caps(codec, nid);
2231
2232 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2233 vref_texts);
2234 /* set the right text */
2235 strcpy(uinfo->value.enumerated.name,
2236 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2237 return 0;
2238}
2239
2240static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2241 struct snd_ctl_elem_value *ucontrol)
2242{
2243 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2244 hda_nid_t nid = kcontrol->private_value;
2245 unsigned int vref_caps = get_vref_caps(codec, nid);
2246 unsigned int idx;
2247
2248 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2249 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2250 return 0;
2251}
2252
2253static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2254 struct snd_ctl_elem_value *ucontrol)
2255{
2256 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2257 hda_nid_t nid = kcontrol->private_value;
2258 unsigned int vref_caps = get_vref_caps(codec, nid);
2259 unsigned int val, idx;
2260
2261 val = snd_hda_codec_get_pin_target(codec, nid);
2262 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2263 if (idx == ucontrol->value.enumerated.item[0])
2264 return 0;
2265
2266 val &= ~AC_PINCTL_VREFEN;
2267 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2268 snd_hda_set_pin_ctl_cache(codec, nid, val);
2269 return 1;
2270}
2271
2272static const struct snd_kcontrol_new in_jack_mode_enum = {
2273 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2274 .info = in_jack_mode_info,
2275 .get = in_jack_mode_get,
2276 .put = in_jack_mode_put,
2277};
2278
2279static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2280{
2281 struct hda_gen_spec *spec = codec->spec;
2282 unsigned int defcfg;
2283 struct snd_kcontrol_new *knew;
2284 char name[44];
2285
2286 /* no jack mode for fixed pins */
2287 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2288 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2289 return 0;
2290
2291 /* no multiple vref caps? */
2292 if (hweight32(get_vref_caps(codec, pin)) <= 1)
2293 return 0;
2294
2295 get_jack_mode_name(codec, pin, name, sizeof(name));
2296 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2297 if (!knew)
2298 return -ENOMEM;
2299 knew->private_value = pin;
2300 return 0;
2301}
2302
Takashi Iwai352f7f92012-12-19 12:52:06 +01002303
2304/*
2305 * Parse input paths
2306 */
2307
2308#ifdef CONFIG_PM
2309/* add the powersave loopback-list entry */
2310static void add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
2311{
2312 struct hda_amp_list *list;
2313
2314 if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
2315 return;
2316 list = spec->loopback_list + spec->num_loopbacks;
2317 list->nid = mix;
2318 list->dir = HDA_INPUT;
2319 list->idx = idx;
2320 spec->num_loopbacks++;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002321 spec->loopback.amplist = spec->loopback_list;
2322}
2323#else
Takashi Iwai352f7f92012-12-19 12:52:06 +01002324#define add_loopback_list(spec, mix, idx) /* NOP */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002325#endif
2326
Takashi Iwai352f7f92012-12-19 12:52:06 +01002327/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01002328static int new_analog_input(struct hda_codec *codec, int input_idx,
2329 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002330 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002332 struct hda_gen_spec *spec = codec->spec;
2333 struct nid_path *path;
2334 unsigned int val;
2335 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
Takashi Iwai352f7f92012-12-19 12:52:06 +01002337 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
2338 !nid_has_mute(codec, mix_nid, HDA_INPUT))
2339 return 0; /* no need for analog loopback */
2340
Takashi Iwai3ca529d2013-01-07 17:25:08 +01002341 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002342 if (!path)
2343 return -EINVAL;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002344 print_nid_path("loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01002345 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002346
2347 idx = path->idx[path->depth - 1];
2348 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
2349 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2350 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002351 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002353 path->ctls[NID_PATH_VOL_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 }
2355
Takashi Iwai352f7f92012-12-19 12:52:06 +01002356 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
2357 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2358 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002359 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002361 path->ctls[NID_PATH_MUTE_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 }
2363
Takashi Iwai352f7f92012-12-19 12:52:06 +01002364 path->active = true;
2365 add_loopback_list(spec, mix_nid, idx);
2366 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367}
2368
Takashi Iwai352f7f92012-12-19 12:52:06 +01002369static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002371 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
2372 return (pincap & AC_PINCAP_IN) != 0;
2373}
2374
2375/* Parse the codec tree and retrieve ADCs */
2376static int fill_adc_nids(struct hda_codec *codec)
2377{
2378 struct hda_gen_spec *spec = codec->spec;
2379 hda_nid_t nid;
2380 hda_nid_t *adc_nids = spec->adc_nids;
2381 int max_nums = ARRAY_SIZE(spec->adc_nids);
2382 int i, nums = 0;
2383
2384 nid = codec->start_nid;
2385 for (i = 0; i < codec->num_nodes; i++, nid++) {
2386 unsigned int caps = get_wcaps(codec, nid);
2387 int type = get_wcaps_type(caps);
2388
2389 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2390 continue;
2391 adc_nids[nums] = nid;
2392 if (++nums >= max_nums)
2393 break;
2394 }
2395 spec->num_adc_nids = nums;
Takashi Iwai0ffd5342013-01-17 15:53:29 +01002396
2397 /* copy the detected ADCs to all_adcs[] */
2398 spec->num_all_adcs = nums;
2399 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
2400
Takashi Iwai352f7f92012-12-19 12:52:06 +01002401 return nums;
2402}
2403
2404/* filter out invalid adc_nids that don't give all active input pins;
2405 * if needed, check whether dynamic ADC-switching is available
2406 */
2407static int check_dyn_adc_switch(struct hda_codec *codec)
2408{
2409 struct hda_gen_spec *spec = codec->spec;
2410 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002411 unsigned int ok_bits;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002412 int i, n, nums;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002413
2414 again:
2415 nums = 0;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002416 ok_bits = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002417 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002418 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002419 if (!spec->input_paths[i][n])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002420 break;
2421 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002422 if (i >= imux->num_items) {
2423 ok_bits |= (1 << n);
2424 nums++;
2425 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002426 }
2427
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002428 if (!ok_bits) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002429 if (spec->shared_mic_hp) {
2430 spec->shared_mic_hp = 0;
2431 imux->num_items = 1;
2432 goto again;
2433 }
2434
2435 /* check whether ADC-switch is possible */
2436 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002437 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002438 if (spec->input_paths[i][n]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002439 spec->dyn_adc_idx[i] = n;
2440 break;
2441 }
2442 }
2443 }
2444
2445 snd_printdd("hda-codec: enabling ADC switching\n");
2446 spec->dyn_adc_switch = 1;
2447 } else if (nums != spec->num_adc_nids) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002448 /* shrink the invalid adcs and input paths */
2449 nums = 0;
2450 for (n = 0; n < spec->num_adc_nids; n++) {
2451 if (!(ok_bits & (1 << n)))
2452 continue;
2453 if (n != nums) {
2454 spec->adc_nids[nums] = spec->adc_nids[n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002455 for (i = 0; i < imux->num_items; i++) {
2456 invalidate_nid_path(codec,
2457 spec->input_paths[i][nums]);
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002458 spec->input_paths[i][nums] =
2459 spec->input_paths[i][n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002460 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002461 }
2462 nums++;
2463 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002464 spec->num_adc_nids = nums;
2465 }
2466
2467 if (imux->num_items == 1 || spec->shared_mic_hp) {
2468 snd_printdd("hda-codec: reducing to a single ADC\n");
2469 spec->num_adc_nids = 1; /* reduce to a single ADC */
2470 }
2471
2472 /* single index for individual volumes ctls */
2473 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
2474 spec->num_adc_nids = 1;
2475
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 return 0;
2477}
2478
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002479/* parse capture source paths from the given pin and create imux items */
2480static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
2481 int num_adcs, const char *label, int anchor)
2482{
2483 struct hda_gen_spec *spec = codec->spec;
2484 struct hda_input_mux *imux = &spec->input_mux;
2485 int imux_idx = imux->num_items;
2486 bool imux_added = false;
2487 int c;
2488
2489 for (c = 0; c < num_adcs; c++) {
2490 struct nid_path *path;
2491 hda_nid_t adc = spec->adc_nids[c];
2492
2493 if (!is_reachable_path(codec, pin, adc))
2494 continue;
2495 path = snd_hda_add_new_path(codec, pin, adc, anchor);
2496 if (!path)
2497 continue;
2498 print_nid_path("input", path);
2499 spec->input_paths[imux_idx][c] =
2500 snd_hda_get_path_idx(codec, path);
2501
2502 if (!imux_added) {
2503 spec->imux_pins[imux->num_items] = pin;
2504 snd_hda_add_imux_item(imux, label,
2505 imux->num_items, NULL);
2506 imux_added = true;
2507 }
2508 }
2509
2510 return 0;
2511}
2512
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002514 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002516static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002517{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002518 struct hda_gen_spec *spec = codec->spec;
2519 const struct auto_pin_cfg *cfg = &spec->autocfg;
2520 hda_nid_t mixer = spec->mixer_nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002521 int num_adcs;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002522 int i, err, type_idx = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002523 const char *prev_label = NULL;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002524 unsigned int val;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002525
Takashi Iwai352f7f92012-12-19 12:52:06 +01002526 num_adcs = fill_adc_nids(codec);
2527 if (num_adcs < 0)
2528 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002529
Takashi Iwai352f7f92012-12-19 12:52:06 +01002530 for (i = 0; i < cfg->num_inputs; i++) {
2531 hda_nid_t pin;
2532 const char *label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
Takashi Iwai352f7f92012-12-19 12:52:06 +01002534 pin = cfg->inputs[i].pin;
2535 if (!is_input_pin(codec, pin))
2536 continue;
2537
2538 label = hda_get_autocfg_input_label(codec, cfg, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002539 if (prev_label && !strcmp(label, prev_label))
2540 type_idx++;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002541 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01002542 type_idx = 0;
2543 prev_label = label;
2544
Takashi Iwai2c12c302013-01-10 09:33:29 +01002545 val = PIN_IN;
2546 if (cfg->inputs[i].type == AUTO_PIN_MIC)
2547 val |= snd_hda_get_default_vref(codec, pin);
2548 set_pin_target(codec, pin, val, false);
2549
Takashi Iwai352f7f92012-12-19 12:52:06 +01002550 if (mixer) {
2551 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01002552 err = new_analog_input(codec, i, pin,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002553 label, type_idx, mixer);
2554 if (err < 0)
2555 return err;
2556 }
2557 }
2558
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002559 err = parse_capture_source(codec, pin, num_adcs, label, -mixer);
2560 if (err < 0)
2561 return err;
Takashi Iwai294765582013-01-17 09:52:11 +01002562
2563 if (spec->add_in_jack_modes) {
2564 err = create_in_jack_mode(codec, pin);
2565 if (err < 0)
2566 return err;
2567 }
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002568 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002569
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002570 if (mixer && spec->add_stereo_mix_input) {
2571 err = parse_capture_source(codec, mixer, num_adcs,
2572 "Stereo Mix", 0);
2573 if (err < 0)
2574 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002575 }
2576
2577 return 0;
2578}
2579
2580
2581/*
2582 * input source mux
2583 */
2584
Takashi Iwaic697b712013-01-07 17:09:26 +01002585/* get the input path specified by the given adc and imux indices */
2586static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002587{
2588 struct hda_gen_spec *spec = codec->spec;
David Henningssonb56fa1e2013-01-16 11:45:35 +01002589 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
2590 snd_BUG();
2591 return NULL;
2592 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002593 if (spec->dyn_adc_switch)
2594 adc_idx = spec->dyn_adc_idx[imux_idx];
David Henningssonb56fa1e2013-01-16 11:45:35 +01002595 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_OUTS) {
2596 snd_BUG();
2597 return NULL;
2598 }
Takashi Iwaic697b712013-01-07 17:09:26 +01002599 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002600}
2601
2602static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2603 unsigned int idx);
2604
2605static int mux_enum_info(struct snd_kcontrol *kcontrol,
2606 struct snd_ctl_elem_info *uinfo)
2607{
2608 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2609 struct hda_gen_spec *spec = codec->spec;
2610 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
2611}
2612
2613static int mux_enum_get(struct snd_kcontrol *kcontrol,
2614 struct snd_ctl_elem_value *ucontrol)
2615{
2616 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2617 struct hda_gen_spec *spec = codec->spec;
David Henningssona053d1e2013-01-16 11:45:36 +01002618 unsigned int adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002619
2620 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
2621 return 0;
2622}
2623
2624static int mux_enum_put(struct snd_kcontrol *kcontrol,
2625 struct snd_ctl_elem_value *ucontrol)
2626{
2627 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
David Henningssona053d1e2013-01-16 11:45:36 +01002628 unsigned int adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002629 return mux_select(codec, adc_idx,
2630 ucontrol->value.enumerated.item[0]);
2631}
2632
Takashi Iwai352f7f92012-12-19 12:52:06 +01002633static const struct snd_kcontrol_new cap_src_temp = {
2634 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2635 .name = "Input Source",
2636 .info = mux_enum_info,
2637 .get = mux_enum_get,
2638 .put = mux_enum_put,
2639};
2640
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002641/*
2642 * capture volume and capture switch ctls
2643 */
2644
Takashi Iwai352f7f92012-12-19 12:52:06 +01002645typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
2646 struct snd_ctl_elem_value *ucontrol);
2647
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002648/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002649static int cap_put_caller(struct snd_kcontrol *kcontrol,
2650 struct snd_ctl_elem_value *ucontrol,
2651 put_call_t func, int type)
2652{
2653 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2654 struct hda_gen_spec *spec = codec->spec;
2655 const struct hda_input_mux *imux;
2656 struct nid_path *path;
2657 int i, adc_idx, err = 0;
2658
2659 imux = &spec->input_mux;
David Henningssona053d1e2013-01-16 11:45:36 +01002660 adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002661 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002662 /* we use the cache-only update at first since multiple input paths
2663 * may shared the same amp; by updating only caches, the redundant
2664 * writes to hardware can be reduced.
2665 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002666 codec->cached_write = 1;
2667 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01002668 path = get_input_path(codec, adc_idx, i);
2669 if (!path || !path->ctls[type])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002670 continue;
2671 kcontrol->private_value = path->ctls[type];
2672 err = func(kcontrol, ucontrol);
2673 if (err < 0)
2674 goto error;
2675 }
2676 error:
2677 codec->cached_write = 0;
2678 mutex_unlock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002679 snd_hda_codec_flush_amp_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002680 if (err >= 0 && spec->cap_sync_hook)
2681 spec->cap_sync_hook(codec);
2682 return err;
2683}
2684
2685/* capture volume ctl callbacks */
2686#define cap_vol_info snd_hda_mixer_amp_volume_info
2687#define cap_vol_get snd_hda_mixer_amp_volume_get
2688#define cap_vol_tlv snd_hda_mixer_amp_tlv
2689
2690static int cap_vol_put(struct snd_kcontrol *kcontrol,
2691 struct snd_ctl_elem_value *ucontrol)
2692{
2693 return cap_put_caller(kcontrol, ucontrol,
2694 snd_hda_mixer_amp_volume_put,
2695 NID_PATH_VOL_CTL);
2696}
2697
2698static const struct snd_kcontrol_new cap_vol_temp = {
2699 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2700 .name = "Capture Volume",
2701 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
2702 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2703 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
2704 .info = cap_vol_info,
2705 .get = cap_vol_get,
2706 .put = cap_vol_put,
2707 .tlv = { .c = cap_vol_tlv },
2708};
2709
2710/* capture switch ctl callbacks */
2711#define cap_sw_info snd_ctl_boolean_stereo_info
2712#define cap_sw_get snd_hda_mixer_amp_switch_get
2713
2714static int cap_sw_put(struct snd_kcontrol *kcontrol,
2715 struct snd_ctl_elem_value *ucontrol)
2716{
Takashi Iwaiae177c32013-01-14 12:13:06 +01002717 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2718 struct hda_gen_spec *spec = codec->spec;
2719 int ret;
2720
2721 ret = cap_put_caller(kcontrol, ucontrol,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002722 snd_hda_mixer_amp_switch_put,
2723 NID_PATH_MUTE_CTL);
Takashi Iwaiae177c32013-01-14 12:13:06 +01002724 if (ret < 0)
2725 return ret;
2726
2727 if (spec->capture_switch_hook) {
2728 bool enable = (ucontrol->value.integer.value[0] ||
2729 ucontrol->value.integer.value[1]);
2730 spec->capture_switch_hook(codec, enable);
2731 }
2732
2733 return ret;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002734}
2735
2736static const struct snd_kcontrol_new cap_sw_temp = {
2737 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2738 .name = "Capture Switch",
2739 .info = cap_sw_info,
2740 .get = cap_sw_get,
2741 .put = cap_sw_put,
2742};
2743
2744static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
2745{
2746 hda_nid_t nid;
2747 int i, depth;
2748
2749 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
2750 for (depth = 0; depth < 3; depth++) {
2751 if (depth >= path->depth)
2752 return -EINVAL;
2753 i = path->depth - depth - 1;
2754 nid = path->path[i];
2755 if (!path->ctls[NID_PATH_VOL_CTL]) {
2756 if (nid_has_volume(codec, nid, HDA_OUTPUT))
2757 path->ctls[NID_PATH_VOL_CTL] =
2758 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2759 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
2760 int idx = path->idx[i];
2761 if (!depth && codec->single_adc_amp)
2762 idx = 0;
2763 path->ctls[NID_PATH_VOL_CTL] =
2764 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2765 }
2766 }
2767 if (!path->ctls[NID_PATH_MUTE_CTL]) {
2768 if (nid_has_mute(codec, nid, HDA_OUTPUT))
2769 path->ctls[NID_PATH_MUTE_CTL] =
2770 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2771 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
2772 int idx = path->idx[i];
2773 if (!depth && codec->single_adc_amp)
2774 idx = 0;
2775 path->ctls[NID_PATH_MUTE_CTL] =
2776 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2777 }
2778 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01002779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 return 0;
2781}
2782
Takashi Iwai352f7f92012-12-19 12:52:06 +01002783static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002785 struct hda_gen_spec *spec = codec->spec;
2786 struct auto_pin_cfg *cfg = &spec->autocfg;
2787 unsigned int val;
2788 int i;
2789
2790 if (!spec->inv_dmic_split)
2791 return false;
2792 for (i = 0; i < cfg->num_inputs; i++) {
2793 if (cfg->inputs[i].pin != nid)
2794 continue;
2795 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2796 return false;
2797 val = snd_hda_codec_get_pincfg(codec, nid);
2798 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
2799 }
2800 return false;
2801}
2802
2803static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
2804 int idx, bool is_switch, unsigned int ctl,
2805 bool inv_dmic)
2806{
2807 struct hda_gen_spec *spec = codec->spec;
2808 char tmpname[44];
2809 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
2810 const char *sfx = is_switch ? "Switch" : "Volume";
2811 unsigned int chs = inv_dmic ? 1 : 3;
2812 int err;
2813
2814 if (!ctl)
2815 return 0;
2816
2817 if (label)
2818 snprintf(tmpname, sizeof(tmpname),
2819 "%s Capture %s", label, sfx);
2820 else
2821 snprintf(tmpname, sizeof(tmpname),
2822 "Capture %s", sfx);
2823 err = add_control(spec, type, tmpname, idx,
2824 amp_val_replace_channels(ctl, chs));
2825 if (err < 0 || !inv_dmic)
2826 return err;
2827
2828 /* Make independent right kcontrol */
2829 if (label)
2830 snprintf(tmpname, sizeof(tmpname),
2831 "Inverted %s Capture %s", label, sfx);
2832 else
2833 snprintf(tmpname, sizeof(tmpname),
2834 "Inverted Capture %s", sfx);
2835 return add_control(spec, type, tmpname, idx,
2836 amp_val_replace_channels(ctl, 2));
2837}
2838
2839/* create single (and simple) capture volume and switch controls */
2840static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
2841 unsigned int vol_ctl, unsigned int sw_ctl,
2842 bool inv_dmic)
2843{
2844 int err;
2845 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
2846 if (err < 0)
2847 return err;
2848 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
2849 if (err < 0)
2850 return err;
2851 return 0;
2852}
2853
2854/* create bound capture volume and switch controls */
2855static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
2856 unsigned int vol_ctl, unsigned int sw_ctl)
2857{
2858 struct hda_gen_spec *spec = codec->spec;
2859 struct snd_kcontrol_new *knew;
2860
2861 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002862 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002863 if (!knew)
2864 return -ENOMEM;
2865 knew->index = idx;
2866 knew->private_value = vol_ctl;
2867 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2868 }
2869 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002870 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002871 if (!knew)
2872 return -ENOMEM;
2873 knew->index = idx;
2874 knew->private_value = sw_ctl;
2875 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2876 }
2877 return 0;
2878}
2879
2880/* return the vol ctl when used first in the imux list */
2881static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
2882{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002883 struct nid_path *path;
2884 unsigned int ctl;
2885 int i;
2886
Takashi Iwaic697b712013-01-07 17:09:26 +01002887 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002888 if (!path)
2889 return 0;
2890 ctl = path->ctls[type];
2891 if (!ctl)
2892 return 0;
2893 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01002894 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002895 if (path && path->ctls[type] == ctl)
2896 return 0;
2897 }
2898 return ctl;
2899}
2900
2901/* create individual capture volume and switch controls per input */
2902static int create_multi_cap_vol_ctl(struct hda_codec *codec)
2903{
2904 struct hda_gen_spec *spec = codec->spec;
2905 struct hda_input_mux *imux = &spec->input_mux;
2906 int i, err, type, type_idx = 0;
2907 const char *prev_label = NULL;
2908
2909 for (i = 0; i < imux->num_items; i++) {
2910 const char *label;
2911 bool inv_dmic;
2912 label = hda_get_autocfg_input_label(codec, &spec->autocfg, i);
2913 if (prev_label && !strcmp(label, prev_label))
2914 type_idx++;
2915 else
2916 type_idx = 0;
2917 prev_label = label;
2918 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
2919
2920 for (type = 0; type < 2; type++) {
2921 err = add_single_cap_ctl(codec, label, type_idx, type,
2922 get_first_cap_ctl(codec, i, type),
2923 inv_dmic);
2924 if (err < 0)
2925 return err;
2926 }
2927 }
2928 return 0;
2929}
2930
2931static int create_capture_mixers(struct hda_codec *codec)
2932{
2933 struct hda_gen_spec *spec = codec->spec;
2934 struct hda_input_mux *imux = &spec->input_mux;
2935 int i, n, nums, err;
2936
2937 if (spec->dyn_adc_switch)
2938 nums = 1;
2939 else
2940 nums = spec->num_adc_nids;
2941
2942 if (!spec->auto_mic && imux->num_items > 1) {
2943 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01002944 const char *name;
2945 name = nums > 1 ? "Input Source" : "Capture Source";
2946 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002947 if (!knew)
2948 return -ENOMEM;
2949 knew->count = nums;
2950 }
2951
2952 for (n = 0; n < nums; n++) {
2953 bool multi = false;
David Henningsson99a55922013-01-16 15:58:44 +01002954 bool multi_cap_vol = spec->multi_cap_vol;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002955 bool inv_dmic = false;
2956 int vol, sw;
2957
2958 vol = sw = 0;
2959 for (i = 0; i < imux->num_items; i++) {
2960 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01002961 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002962 if (!path)
2963 continue;
2964 parse_capvol_in_path(codec, path);
2965 if (!vol)
2966 vol = path->ctls[NID_PATH_VOL_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01002967 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002968 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01002969 if (!same_amp_caps(codec, vol,
2970 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
2971 multi_cap_vol = true;
2972 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002973 if (!sw)
2974 sw = path->ctls[NID_PATH_MUTE_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01002975 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002976 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01002977 if (!same_amp_caps(codec, sw,
2978 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
2979 multi_cap_vol = true;
2980 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002981 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
2982 inv_dmic = true;
2983 }
2984
2985 if (!multi)
2986 err = create_single_cap_vol_ctl(codec, n, vol, sw,
2987 inv_dmic);
David Henningsson99a55922013-01-16 15:58:44 +01002988 else if (!multi_cap_vol)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002989 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
2990 else
2991 err = create_multi_cap_vol_ctl(codec);
2992 if (err < 0)
2993 return err;
2994 }
2995
2996 return 0;
2997}
2998
2999/*
3000 * add mic boosts if needed
3001 */
3002static int parse_mic_boost(struct hda_codec *codec)
3003{
3004 struct hda_gen_spec *spec = codec->spec;
3005 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003006 int i, err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003007 int type_idx = 0;
3008 hda_nid_t nid;
3009 const char *prev_label = NULL;
3010
3011 for (i = 0; i < cfg->num_inputs; i++) {
3012 if (cfg->inputs[i].type > AUTO_PIN_MIC)
3013 break;
3014 nid = cfg->inputs[i].pin;
3015 if (get_wcaps(codec, nid) & AC_WCAP_IN_AMP) {
3016 const char *label;
Takashi Iwai5abd4882013-01-07 09:43:18 +01003017 char boost_label[44];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003018 struct nid_path *path;
3019 unsigned int val;
3020
David Henningsson02aba552013-01-16 15:58:43 +01003021 if (!nid_has_volume(codec, nid, HDA_INPUT))
3022 continue;
3023
Takashi Iwai352f7f92012-12-19 12:52:06 +01003024 label = hda_get_autocfg_input_label(codec, cfg, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003025 if (prev_label && !strcmp(label, prev_label))
3026 type_idx++;
3027 else
3028 type_idx = 0;
3029 prev_label = label;
3030
3031 snprintf(boost_label, sizeof(boost_label),
3032 "%s Boost Volume", label);
3033 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
3034 err = add_control(spec, HDA_CTL_WIDGET_VOL,
3035 boost_label, type_idx, val);
3036 if (err < 0)
3037 return err;
3038
3039 path = snd_hda_get_nid_path(codec, nid, 0);
3040 if (path)
3041 path->ctls[NID_PATH_BOOST_CTL] = val;
3042 }
3043 }
3044 return 0;
3045}
3046
3047/*
3048 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3049 */
3050static void parse_digital(struct hda_codec *codec)
3051{
3052 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003053 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003054 int i, nums;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003055 hda_nid_t dig_nid, pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003056
3057 /* support multiple SPDIFs; the secondary is set up as a slave */
3058 nums = 0;
3059 for (i = 0; i < spec->autocfg.dig_outs; i++) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003060 pin = spec->autocfg.dig_out_pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003061 dig_nid = look_for_dac(codec, pin, true);
3062 if (!dig_nid)
3063 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003064 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003065 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003066 continue;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003067 print_nid_path("digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01003068 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01003069 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003070 set_pin_target(codec, pin, PIN_OUT, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003071 if (!nums) {
3072 spec->multiout.dig_out_nid = dig_nid;
3073 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3074 } else {
3075 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3076 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
3077 break;
3078 spec->slave_dig_outs[nums - 1] = dig_nid;
3079 }
3080 nums++;
3081 }
3082
3083 if (spec->autocfg.dig_in_pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003084 pin = spec->autocfg.dig_in_pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003085 dig_nid = codec->start_nid;
3086 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003087 unsigned int wcaps = get_wcaps(codec, dig_nid);
3088 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3089 continue;
3090 if (!(wcaps & AC_WCAP_DIGITAL))
3091 continue;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003092 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003093 if (path) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003094 print_nid_path("digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003095 path->active = true;
3096 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003097 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003098 set_pin_target(codec, pin, PIN_IN, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003099 break;
3100 }
3101 }
3102 }
3103}
3104
3105
3106/*
3107 * input MUX handling
3108 */
3109
3110static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3111
3112/* select the given imux item; either unmute exclusively or select the route */
3113static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3114 unsigned int idx)
3115{
3116 struct hda_gen_spec *spec = codec->spec;
3117 const struct hda_input_mux *imux;
3118 struct nid_path *path;
3119
3120 imux = &spec->input_mux;
3121 if (!imux->num_items)
3122 return 0;
3123
3124 if (idx >= imux->num_items)
3125 idx = imux->num_items - 1;
3126 if (spec->cur_mux[adc_idx] == idx)
3127 return 0;
3128
Takashi Iwaic697b712013-01-07 17:09:26 +01003129 path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003130 if (!path)
3131 return 0;
3132 if (path->active)
3133 snd_hda_activate_path(codec, path, false, false);
3134
3135 spec->cur_mux[adc_idx] = idx;
3136
3137 if (spec->shared_mic_hp)
3138 update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
3139
3140 if (spec->dyn_adc_switch)
3141 dyn_adc_pcm_resetup(codec, idx);
3142
Takashi Iwaic697b712013-01-07 17:09:26 +01003143 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003144 if (!path)
3145 return 0;
3146 if (path->active)
3147 return 0;
3148 snd_hda_activate_path(codec, path, true, false);
3149 if (spec->cap_sync_hook)
3150 spec->cap_sync_hook(codec);
3151 return 1;
3152}
3153
3154
3155/*
3156 * Jack detections for HP auto-mute and mic-switch
3157 */
3158
3159/* check each pin in the given array; returns true if any of them is plugged */
3160static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
3161{
3162 int i, present = 0;
3163
3164 for (i = 0; i < num_pins; i++) {
3165 hda_nid_t nid = pins[i];
3166 if (!nid)
3167 break;
Takashi Iwai0b4df932013-01-10 09:45:13 +01003168 /* don't detect pins retasked as inputs */
3169 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
3170 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003171 present |= snd_hda_jack_detect(codec, nid);
3172 }
3173 return present;
3174}
3175
3176/* standard HP/line-out auto-mute helper */
3177static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
Takashi Iwai2c12c302013-01-10 09:33:29 +01003178 bool mute)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003179{
3180 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003181 int i;
3182
3183 for (i = 0; i < num_pins; i++) {
3184 hda_nid_t nid = pins[i];
3185 unsigned int val;
3186 if (!nid)
3187 break;
3188 /* don't reset VREF value in case it's controlling
3189 * the amp (see alc861_fixup_asus_amp_vref_0f())
3190 */
Takashi Iwai2c12c302013-01-10 09:33:29 +01003191 if (spec->keep_vref_in_automute)
3192 val = snd_hda_codec_get_pin_target(codec, nid) & ~PIN_HP;
3193 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01003194 val = 0;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003195 if (!mute)
3196 val |= snd_hda_codec_get_pin_target(codec, nid);
3197 /* here we call update_pin_ctl() so that the pinctl is changed
3198 * without changing the pinctl target value;
3199 * the original target value will be still referred at the
3200 * init / resume again
3201 */
3202 update_pin_ctl(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01003203 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003204 }
3205}
3206
3207/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003208void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003209{
3210 struct hda_gen_spec *spec = codec->spec;
3211 int on;
3212
3213 /* Control HP pins/amps depending on master_mute state;
3214 * in general, HP pins/amps control should be enabled in all cases,
3215 * but currently set only for master_mute, just to be safe
3216 */
3217 if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
3218 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003219 spec->autocfg.hp_pins, spec->master_mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003220
3221 if (!spec->automute_speaker)
3222 on = 0;
3223 else
3224 on = spec->hp_jack_present | spec->line_jack_present;
3225 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003226 spec->speaker_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003227 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003228 spec->autocfg.speaker_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003229
3230 /* toggle line-out mutes if needed, too */
3231 /* if LO is a copy of either HP or Speaker, don't need to handle it */
3232 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
3233 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
3234 return;
3235 if (!spec->automute_lo)
3236 on = 0;
3237 else
3238 on = spec->hp_jack_present;
3239 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003240 spec->line_out_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003241 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003242 spec->autocfg.line_out_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003243}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003244EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003245
3246static void call_update_outputs(struct hda_codec *codec)
3247{
3248 struct hda_gen_spec *spec = codec->spec;
3249 if (spec->automute_hook)
3250 spec->automute_hook(codec);
3251 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01003252 snd_hda_gen_update_outputs(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003253}
3254
3255/* standard HP-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003256void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003257{
3258 struct hda_gen_spec *spec = codec->spec;
3259
3260 spec->hp_jack_present =
3261 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
3262 spec->autocfg.hp_pins);
3263 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
3264 return;
3265 call_update_outputs(codec);
3266}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003267EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003268
3269/* standard line-out-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003270void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003271{
3272 struct hda_gen_spec *spec = codec->spec;
3273
3274 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3275 return;
3276 /* check LO jack only when it's different from HP */
3277 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
3278 return;
3279
3280 spec->line_jack_present =
3281 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3282 spec->autocfg.line_out_pins);
3283 if (!spec->automute_speaker || !spec->detect_lo)
3284 return;
3285 call_update_outputs(codec);
3286}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003287EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003288
3289/* standard mic auto-switch helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003290void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003291{
3292 struct hda_gen_spec *spec = codec->spec;
3293 int i;
3294
3295 if (!spec->auto_mic)
3296 return;
3297
3298 for (i = spec->am_num_entries - 1; i > 0; i--) {
Takashi Iwai0b4df932013-01-10 09:45:13 +01003299 hda_nid_t pin = spec->am_entry[i].pin;
3300 /* don't detect pins retasked as outputs */
3301 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
3302 continue;
3303 if (snd_hda_jack_detect(codec, pin)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003304 mux_select(codec, 0, spec->am_entry[i].idx);
3305 return;
3306 }
3307 }
3308 mux_select(codec, 0, spec->am_entry[0].idx);
3309}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003310EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003311
Takashi Iwaia5cc2502013-01-16 18:08:55 +01003312/* update jack retasking */
3313static void update_automute_all(struct hda_codec *codec)
3314{
3315 struct hda_gen_spec *spec = codec->spec;
3316
3317 if (spec->hp_automute_hook)
3318 spec->hp_automute_hook(codec, NULL);
3319 else
3320 snd_hda_gen_hp_automute(codec, NULL);
3321 if (spec->line_automute_hook)
3322 spec->line_automute_hook(codec, NULL);
3323 else
3324 snd_hda_gen_line_automute(codec, NULL);
3325 if (spec->mic_autoswitch_hook)
3326 spec->mic_autoswitch_hook(codec, NULL);
3327 else
3328 snd_hda_gen_mic_autoswitch(codec, NULL);
3329}
3330
Takashi Iwai352f7f92012-12-19 12:52:06 +01003331/*
3332 * Auto-Mute mode mixer enum support
3333 */
3334static int automute_mode_info(struct snd_kcontrol *kcontrol,
3335 struct snd_ctl_elem_info *uinfo)
3336{
3337 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3338 struct hda_gen_spec *spec = codec->spec;
3339 static const char * const texts3[] = {
3340 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02003341 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342
Takashi Iwai352f7f92012-12-19 12:52:06 +01003343 if (spec->automute_speaker_possible && spec->automute_lo_possible)
3344 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
3345 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
3346}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347
Takashi Iwai352f7f92012-12-19 12:52:06 +01003348static int automute_mode_get(struct snd_kcontrol *kcontrol,
3349 struct snd_ctl_elem_value *ucontrol)
3350{
3351 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3352 struct hda_gen_spec *spec = codec->spec;
3353 unsigned int val = 0;
3354 if (spec->automute_speaker)
3355 val++;
3356 if (spec->automute_lo)
3357 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003358
Takashi Iwai352f7f92012-12-19 12:52:06 +01003359 ucontrol->value.enumerated.item[0] = val;
3360 return 0;
3361}
3362
3363static int automute_mode_put(struct snd_kcontrol *kcontrol,
3364 struct snd_ctl_elem_value *ucontrol)
3365{
3366 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3367 struct hda_gen_spec *spec = codec->spec;
3368
3369 switch (ucontrol->value.enumerated.item[0]) {
3370 case 0:
3371 if (!spec->automute_speaker && !spec->automute_lo)
3372 return 0;
3373 spec->automute_speaker = 0;
3374 spec->automute_lo = 0;
3375 break;
3376 case 1:
3377 if (spec->automute_speaker_possible) {
3378 if (!spec->automute_lo && spec->automute_speaker)
3379 return 0;
3380 spec->automute_speaker = 1;
3381 spec->automute_lo = 0;
3382 } else if (spec->automute_lo_possible) {
3383 if (spec->automute_lo)
3384 return 0;
3385 spec->automute_lo = 1;
3386 } else
3387 return -EINVAL;
3388 break;
3389 case 2:
3390 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
3391 return -EINVAL;
3392 if (spec->automute_speaker && spec->automute_lo)
3393 return 0;
3394 spec->automute_speaker = 1;
3395 spec->automute_lo = 1;
3396 break;
3397 default:
3398 return -EINVAL;
3399 }
3400 call_update_outputs(codec);
3401 return 1;
3402}
3403
3404static const struct snd_kcontrol_new automute_mode_enum = {
3405 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3406 .name = "Auto-Mute Mode",
3407 .info = automute_mode_info,
3408 .get = automute_mode_get,
3409 .put = automute_mode_put,
3410};
3411
3412static int add_automute_mode_enum(struct hda_codec *codec)
3413{
3414 struct hda_gen_spec *spec = codec->spec;
3415
Takashi Iwai12c93df2012-12-19 14:38:33 +01003416 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01003417 return -ENOMEM;
3418 return 0;
3419}
3420
3421/*
3422 * Check the availability of HP/line-out auto-mute;
3423 * Set up appropriately if really supported
3424 */
3425static int check_auto_mute_availability(struct hda_codec *codec)
3426{
3427 struct hda_gen_spec *spec = codec->spec;
3428 struct auto_pin_cfg *cfg = &spec->autocfg;
3429 int present = 0;
3430 int i, err;
3431
Takashi Iwaif72706b2013-01-16 18:20:07 +01003432 if (spec->suppress_auto_mute)
3433 return 0;
3434
Takashi Iwai352f7f92012-12-19 12:52:06 +01003435 if (cfg->hp_pins[0])
3436 present++;
3437 if (cfg->line_out_pins[0])
3438 present++;
3439 if (cfg->speaker_pins[0])
3440 present++;
3441 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02003442 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003443
3444 if (!cfg->speaker_pins[0] &&
3445 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
3446 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3447 sizeof(cfg->speaker_pins));
3448 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450
Takashi Iwai352f7f92012-12-19 12:52:06 +01003451 if (!cfg->hp_pins[0] &&
3452 cfg->line_out_type == AUTO_PIN_HP_OUT) {
3453 memcpy(cfg->hp_pins, cfg->line_out_pins,
3454 sizeof(cfg->hp_pins));
3455 cfg->hp_outs = cfg->line_outs;
3456 }
3457
3458 for (i = 0; i < cfg->hp_outs; i++) {
3459 hda_nid_t nid = cfg->hp_pins[i];
3460 if (!is_jack_detectable(codec, nid))
3461 continue;
3462 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
3463 nid);
3464 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003465 spec->hp_automute_hook ?
3466 spec->hp_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003467 snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003468 spec->detect_hp = 1;
3469 }
3470
3471 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
3472 if (cfg->speaker_outs)
3473 for (i = 0; i < cfg->line_outs; i++) {
3474 hda_nid_t nid = cfg->line_out_pins[i];
3475 if (!is_jack_detectable(codec, nid))
3476 continue;
3477 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
3478 snd_hda_jack_detect_enable_callback(codec, nid,
3479 HDA_GEN_FRONT_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003480 spec->line_automute_hook ?
3481 spec->line_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003482 snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003483 spec->detect_lo = 1;
3484 }
3485 spec->automute_lo_possible = spec->detect_hp;
3486 }
3487
3488 spec->automute_speaker_possible = cfg->speaker_outs &&
3489 (spec->detect_hp || spec->detect_lo);
3490
3491 spec->automute_lo = spec->automute_lo_possible;
3492 spec->automute_speaker = spec->automute_speaker_possible;
3493
3494 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
3495 /* create a control for automute mode */
3496 err = add_automute_mode_enum(codec);
3497 if (err < 0)
3498 return err;
3499 }
3500 return 0;
3501}
3502
Takashi Iwai352f7f92012-12-19 12:52:06 +01003503/* check whether all auto-mic pins are valid; setup indices if OK */
3504static bool auto_mic_check_imux(struct hda_codec *codec)
3505{
3506 struct hda_gen_spec *spec = codec->spec;
3507 const struct hda_input_mux *imux;
3508 int i;
3509
3510 imux = &spec->input_mux;
3511 for (i = 0; i < spec->am_num_entries; i++) {
3512 spec->am_entry[i].idx =
3513 find_idx_in_nid_list(spec->am_entry[i].pin,
3514 spec->imux_pins, imux->num_items);
3515 if (spec->am_entry[i].idx < 0)
3516 return false; /* no corresponding imux */
3517 }
3518
3519 /* we don't need the jack detection for the first pin */
3520 for (i = 1; i < spec->am_num_entries; i++)
3521 snd_hda_jack_detect_enable_callback(codec,
3522 spec->am_entry[i].pin,
3523 HDA_GEN_MIC_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003524 spec->mic_autoswitch_hook ?
3525 spec->mic_autoswitch_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003526 snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003527 return true;
3528}
3529
3530static int compare_attr(const void *ap, const void *bp)
3531{
3532 const struct automic_entry *a = ap;
3533 const struct automic_entry *b = bp;
3534 return (int)(a->attr - b->attr);
3535}
3536
3537/*
3538 * Check the availability of auto-mic switch;
3539 * Set up if really supported
3540 */
3541static int check_auto_mic_availability(struct hda_codec *codec)
3542{
3543 struct hda_gen_spec *spec = codec->spec;
3544 struct auto_pin_cfg *cfg = &spec->autocfg;
3545 unsigned int types;
3546 int i, num_pins;
3547
Takashi Iwaid12daf62013-01-07 16:32:11 +01003548 if (spec->suppress_auto_mic)
3549 return 0;
3550
Takashi Iwai352f7f92012-12-19 12:52:06 +01003551 types = 0;
3552 num_pins = 0;
3553 for (i = 0; i < cfg->num_inputs; i++) {
3554 hda_nid_t nid = cfg->inputs[i].pin;
3555 unsigned int attr;
3556 attr = snd_hda_codec_get_pincfg(codec, nid);
3557 attr = snd_hda_get_input_pin_attr(attr);
3558 if (types & (1 << attr))
3559 return 0; /* already occupied */
3560 switch (attr) {
3561 case INPUT_PIN_ATTR_INT:
3562 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3563 return 0; /* invalid type */
3564 break;
3565 case INPUT_PIN_ATTR_UNUSED:
3566 return 0; /* invalid entry */
3567 default:
3568 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
3569 return 0; /* invalid type */
3570 if (!spec->line_in_auto_switch &&
3571 cfg->inputs[i].type != AUTO_PIN_MIC)
3572 return 0; /* only mic is allowed */
3573 if (!is_jack_detectable(codec, nid))
3574 return 0; /* no unsol support */
3575 break;
3576 }
3577 if (num_pins >= MAX_AUTO_MIC_PINS)
3578 return 0;
3579 types |= (1 << attr);
3580 spec->am_entry[num_pins].pin = nid;
3581 spec->am_entry[num_pins].attr = attr;
3582 num_pins++;
3583 }
3584
3585 if (num_pins < 2)
3586 return 0;
3587
3588 spec->am_num_entries = num_pins;
3589 /* sort the am_entry in the order of attr so that the pin with a
3590 * higher attr will be selected when the jack is plugged.
3591 */
3592 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
3593 compare_attr, NULL);
3594
3595 if (!auto_mic_check_imux(codec))
3596 return 0;
3597
3598 spec->auto_mic = 1;
3599 spec->num_adc_nids = 1;
3600 spec->cur_mux[0] = spec->am_entry[0].idx;
3601 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
3602 spec->am_entry[0].pin,
3603 spec->am_entry[1].pin,
3604 spec->am_entry[2].pin);
3605
3606 return 0;
3607}
3608
3609
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003610/*
3611 * Parse the given BIOS configuration and set up the hda_gen_spec
3612 *
3613 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003614 * or a negative error code
3615 */
3616int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003617 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003618{
3619 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003620 int err;
3621
Takashi Iwai1c70a582013-01-11 17:48:22 +01003622 parse_user_hints(codec);
3623
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003624 if (cfg != &spec->autocfg) {
3625 spec->autocfg = *cfg;
3626 cfg = &spec->autocfg;
3627 }
3628
David Henningsson6fc4cb92013-01-16 15:58:45 +01003629 fill_all_dac_nids(codec);
3630
Takashi Iwai352f7f92012-12-19 12:52:06 +01003631 if (!cfg->line_outs) {
3632 if (cfg->dig_outs || cfg->dig_in_pin) {
3633 spec->multiout.max_channels = 2;
3634 spec->no_analog = 1;
3635 goto dig_only;
3636 }
3637 return 0; /* can't find valid BIOS pin config */
3638 }
3639
3640 if (!spec->no_primary_hp &&
3641 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
3642 cfg->line_outs <= cfg->hp_outs) {
3643 /* use HP as primary out */
3644 cfg->speaker_outs = cfg->line_outs;
3645 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3646 sizeof(cfg->speaker_pins));
3647 cfg->line_outs = cfg->hp_outs;
3648 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
3649 cfg->hp_outs = 0;
3650 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3651 cfg->line_out_type = AUTO_PIN_HP_OUT;
3652 }
3653
3654 err = parse_output_paths(codec);
3655 if (err < 0)
3656 return err;
3657 err = create_multi_channel_mode(codec);
3658 if (err < 0)
3659 return err;
3660 err = create_multi_out_ctls(codec, cfg);
3661 if (err < 0)
3662 return err;
3663 err = create_hp_out_ctls(codec);
3664 if (err < 0)
3665 return err;
3666 err = create_speaker_out_ctls(codec);
3667 if (err < 0)
3668 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003669 err = create_indep_hp_ctls(codec);
3670 if (err < 0)
3671 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01003672 err = create_loopback_mixing_ctl(codec);
3673 if (err < 0)
3674 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003675 err = create_shared_input(codec);
3676 if (err < 0)
3677 return err;
3678 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003679 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02003680 return err;
3681
Takashi Iwaia07a9492013-01-07 16:44:06 +01003682 spec->const_channel_count = spec->ext_channel_count;
3683 /* check the multiple speaker and headphone pins */
3684 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
3685 spec->const_channel_count = max(spec->const_channel_count,
3686 cfg->speaker_outs * 2);
3687 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
3688 spec->const_channel_count = max(spec->const_channel_count,
3689 cfg->hp_outs * 2);
3690 spec->multiout.max_channels = max(spec->ext_channel_count,
3691 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003692
3693 err = check_auto_mute_availability(codec);
3694 if (err < 0)
3695 return err;
3696
3697 err = check_dyn_adc_switch(codec);
3698 if (err < 0)
3699 return err;
3700
3701 if (!spec->shared_mic_hp) {
3702 err = check_auto_mic_availability(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003703 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 }
Takashi Iwai071c73a2006-08-23 18:34:06 +02003706
Takashi Iwai352f7f92012-12-19 12:52:06 +01003707 err = create_capture_mixers(codec);
3708 if (err < 0)
3709 return err;
3710
3711 err = parse_mic_boost(codec);
3712 if (err < 0)
3713 return err;
3714
Takashi Iwai978e77e2013-01-10 16:57:58 +01003715 if (spec->add_out_jack_modes) {
3716 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
3717 err = create_out_jack_modes(codec, cfg->line_outs,
3718 cfg->line_out_pins);
3719 if (err < 0)
3720 return err;
3721 }
3722 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
3723 err = create_out_jack_modes(codec, cfg->hp_outs,
3724 cfg->hp_pins);
3725 if (err < 0)
3726 return err;
3727 }
3728 }
3729
Takashi Iwai352f7f92012-12-19 12:52:06 +01003730 dig_only:
3731 parse_digital(codec);
3732
3733 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003735EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736
3737
3738/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003739 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003741
3742/* slave controls for virtual master */
3743static const char * const slave_pfxs[] = {
3744 "Front", "Surround", "Center", "LFE", "Side",
3745 "Headphone", "Speaker", "Mono", "Line Out",
3746 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01003747 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
3748 "Headphone Front", "Headphone Surround", "Headphone CLFE",
3749 "Headphone Side",
Takashi Iwai352f7f92012-12-19 12:52:06 +01003750 NULL,
3751};
3752
3753int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003755 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757
Takashi Iwai36502d02012-12-19 15:15:10 +01003758 if (spec->kctls.used) {
3759 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
3760 if (err < 0)
3761 return err;
3762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763
Takashi Iwai352f7f92012-12-19 12:52:06 +01003764 if (spec->multiout.dig_out_nid) {
3765 err = snd_hda_create_dig_out_ctls(codec,
3766 spec->multiout.dig_out_nid,
3767 spec->multiout.dig_out_nid,
3768 spec->pcm_rec[1].pcm_type);
3769 if (err < 0)
3770 return err;
3771 if (!spec->no_analog) {
3772 err = snd_hda_create_spdif_share_sw(codec,
3773 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 if (err < 0)
3775 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003776 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 }
3778 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003779 if (spec->dig_in_nid) {
3780 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
3781 if (err < 0)
3782 return err;
3783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784
Takashi Iwai352f7f92012-12-19 12:52:06 +01003785 /* if we have no master control, let's create it */
3786 if (!spec->no_analog &&
3787 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003788 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01003789 spec->vmaster_tlv, slave_pfxs,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003790 "Playback Volume");
3791 if (err < 0)
3792 return err;
3793 }
3794 if (!spec->no_analog &&
3795 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
3796 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
3797 NULL, slave_pfxs,
3798 "Playback Switch",
3799 true, &spec->vmaster_mute.sw_kctl);
3800 if (err < 0)
3801 return err;
3802 if (spec->vmaster_mute.hook)
Takashi Iwaifd25a972012-12-20 14:57:18 +01003803 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
3804 spec->vmaster_mute_enum);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003805 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806
Takashi Iwai352f7f92012-12-19 12:52:06 +01003807 free_kctls(spec); /* no longer needed */
3808
3809 if (spec->shared_mic_hp) {
3810 int err;
3811 int nid = spec->autocfg.inputs[1].pin;
3812 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
3813 if (err < 0)
3814 return err;
3815 err = snd_hda_jack_detect_enable(codec, nid, 0);
3816 if (err < 0)
3817 return err;
3818 }
3819
3820 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
3821 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 return err;
3823
3824 return 0;
3825}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003826EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
3827
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828
3829/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003830 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003833static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
3834 struct hda_codec *codec,
3835 struct snd_pcm_substream *substream,
3836 int action)
3837{
3838 struct hda_gen_spec *spec = codec->spec;
3839 if (spec->pcm_playback_hook)
3840 spec->pcm_playback_hook(hinfo, codec, substream, action);
3841}
3842
Takashi Iwaiac2e8732013-01-17 15:57:10 +01003843static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
3844 struct hda_codec *codec,
3845 struct snd_pcm_substream *substream,
3846 int action)
3847{
3848 struct hda_gen_spec *spec = codec->spec;
3849 if (spec->pcm_capture_hook)
3850 spec->pcm_capture_hook(hinfo, codec, substream, action);
3851}
3852
Takashi Iwai352f7f92012-12-19 12:52:06 +01003853/*
3854 * Analog playback callbacks
3855 */
3856static int playback_pcm_open(struct hda_pcm_stream *hinfo,
3857 struct hda_codec *codec,
3858 struct snd_pcm_substream *substream)
3859{
3860 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003861 int err;
3862
3863 mutex_lock(&spec->pcm_mutex);
3864 err = snd_hda_multi_out_analog_open(codec,
3865 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003866 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003867 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003868 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003869 call_pcm_playback_hook(hinfo, codec, substream,
3870 HDA_GEN_PCM_ACT_OPEN);
3871 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003872 mutex_unlock(&spec->pcm_mutex);
3873 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003874}
3875
3876static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01003877 struct hda_codec *codec,
3878 unsigned int stream_tag,
3879 unsigned int format,
3880 struct snd_pcm_substream *substream)
3881{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003882 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003883 int err;
3884
3885 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
3886 stream_tag, format, substream);
3887 if (!err)
3888 call_pcm_playback_hook(hinfo, codec, substream,
3889 HDA_GEN_PCM_ACT_PREPARE);
3890 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003891}
Takashi Iwai97ec5582006-03-21 11:29:07 +01003892
Takashi Iwai352f7f92012-12-19 12:52:06 +01003893static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3894 struct hda_codec *codec,
3895 struct snd_pcm_substream *substream)
3896{
3897 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003898 int err;
3899
3900 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
3901 if (!err)
3902 call_pcm_playback_hook(hinfo, codec, substream,
3903 HDA_GEN_PCM_ACT_CLEANUP);
3904 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003905}
3906
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003907static int playback_pcm_close(struct hda_pcm_stream *hinfo,
3908 struct hda_codec *codec,
3909 struct snd_pcm_substream *substream)
3910{
3911 struct hda_gen_spec *spec = codec->spec;
3912 mutex_lock(&spec->pcm_mutex);
3913 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003914 call_pcm_playback_hook(hinfo, codec, substream,
3915 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003916 mutex_unlock(&spec->pcm_mutex);
3917 return 0;
3918}
3919
Takashi Iwaiac2e8732013-01-17 15:57:10 +01003920static int capture_pcm_open(struct hda_pcm_stream *hinfo,
3921 struct hda_codec *codec,
3922 struct snd_pcm_substream *substream)
3923{
3924 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
3925 return 0;
3926}
3927
3928static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3929 struct hda_codec *codec,
3930 unsigned int stream_tag,
3931 unsigned int format,
3932 struct snd_pcm_substream *substream)
3933{
3934 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3935 call_pcm_capture_hook(hinfo, codec, substream,
3936 HDA_GEN_PCM_ACT_PREPARE);
3937 return 0;
3938}
3939
3940static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3941 struct hda_codec *codec,
3942 struct snd_pcm_substream *substream)
3943{
3944 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
3945 call_pcm_capture_hook(hinfo, codec, substream,
3946 HDA_GEN_PCM_ACT_CLEANUP);
3947 return 0;
3948}
3949
3950static int capture_pcm_close(struct hda_pcm_stream *hinfo,
3951 struct hda_codec *codec,
3952 struct snd_pcm_substream *substream)
3953{
3954 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
3955 return 0;
3956}
3957
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003958static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
3959 struct hda_codec *codec,
3960 struct snd_pcm_substream *substream)
3961{
3962 struct hda_gen_spec *spec = codec->spec;
3963 int err = 0;
3964
3965 mutex_lock(&spec->pcm_mutex);
3966 if (!spec->indep_hp_enabled)
3967 err = -EBUSY;
3968 else
3969 spec->active_streams |= 1 << STREAM_INDEP_HP;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003970 call_pcm_playback_hook(hinfo, codec, substream,
3971 HDA_GEN_PCM_ACT_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003972 mutex_unlock(&spec->pcm_mutex);
3973 return err;
3974}
3975
3976static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
3977 struct hda_codec *codec,
3978 struct snd_pcm_substream *substream)
3979{
3980 struct hda_gen_spec *spec = codec->spec;
3981 mutex_lock(&spec->pcm_mutex);
3982 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003983 call_pcm_playback_hook(hinfo, codec, substream,
3984 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003985 mutex_unlock(&spec->pcm_mutex);
3986 return 0;
3987}
3988
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003989static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3990 struct hda_codec *codec,
3991 unsigned int stream_tag,
3992 unsigned int format,
3993 struct snd_pcm_substream *substream)
3994{
3995 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3996 call_pcm_playback_hook(hinfo, codec, substream,
3997 HDA_GEN_PCM_ACT_PREPARE);
3998 return 0;
3999}
4000
4001static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4002 struct hda_codec *codec,
4003 struct snd_pcm_substream *substream)
4004{
4005 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4006 call_pcm_playback_hook(hinfo, codec, substream,
4007 HDA_GEN_PCM_ACT_CLEANUP);
4008 return 0;
4009}
4010
Takashi Iwai352f7f92012-12-19 12:52:06 +01004011/*
4012 * Digital out
4013 */
4014static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
4015 struct hda_codec *codec,
4016 struct snd_pcm_substream *substream)
4017{
4018 struct hda_gen_spec *spec = codec->spec;
4019 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
4020}
4021
4022static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4023 struct hda_codec *codec,
4024 unsigned int stream_tag,
4025 unsigned int format,
4026 struct snd_pcm_substream *substream)
4027{
4028 struct hda_gen_spec *spec = codec->spec;
4029 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
4030 stream_tag, format, substream);
4031}
4032
4033static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4034 struct hda_codec *codec,
4035 struct snd_pcm_substream *substream)
4036{
4037 struct hda_gen_spec *spec = codec->spec;
4038 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
4039}
4040
4041static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
4042 struct hda_codec *codec,
4043 struct snd_pcm_substream *substream)
4044{
4045 struct hda_gen_spec *spec = codec->spec;
4046 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
4047}
4048
4049/*
4050 * Analog capture
4051 */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004052#define alt_capture_pcm_open capture_pcm_open
4053#define alt_capture_pcm_close capture_pcm_close
4054
Takashi Iwai352f7f92012-12-19 12:52:06 +01004055static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4056 struct hda_codec *codec,
4057 unsigned int stream_tag,
4058 unsigned int format,
4059 struct snd_pcm_substream *substream)
4060{
4061 struct hda_gen_spec *spec = codec->spec;
4062
4063 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01004064 stream_tag, 0, format);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004065 call_pcm_capture_hook(hinfo, codec, substream,
4066 HDA_GEN_PCM_ACT_PREPARE);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004067 return 0;
4068}
4069
Takashi Iwai352f7f92012-12-19 12:52:06 +01004070static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4071 struct hda_codec *codec,
4072 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01004073{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004074 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01004075
Takashi Iwai352f7f92012-12-19 12:52:06 +01004076 snd_hda_codec_cleanup_stream(codec,
4077 spec->adc_nids[substream->number + 1]);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004078 call_pcm_capture_hook(hinfo, codec, substream,
4079 HDA_GEN_PCM_ACT_CLEANUP);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004080 return 0;
4081}
4082
Takashi Iwai352f7f92012-12-19 12:52:06 +01004083/*
4084 */
4085static const struct hda_pcm_stream pcm_analog_playback = {
4086 .substreams = 1,
4087 .channels_min = 2,
4088 .channels_max = 8,
4089 /* NID is set in build_pcms */
4090 .ops = {
4091 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004092 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004093 .prepare = playback_pcm_prepare,
4094 .cleanup = playback_pcm_cleanup
4095 },
4096};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097
Takashi Iwai352f7f92012-12-19 12:52:06 +01004098static const struct hda_pcm_stream pcm_analog_capture = {
4099 .substreams = 1,
4100 .channels_min = 2,
4101 .channels_max = 2,
4102 /* NID is set in build_pcms */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004103 .ops = {
4104 .open = capture_pcm_open,
4105 .close = capture_pcm_close,
4106 .prepare = capture_pcm_prepare,
4107 .cleanup = capture_pcm_cleanup
4108 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004109};
4110
4111static const struct hda_pcm_stream pcm_analog_alt_playback = {
4112 .substreams = 1,
4113 .channels_min = 2,
4114 .channels_max = 2,
4115 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004116 .ops = {
4117 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004118 .close = alt_playback_pcm_close,
4119 .prepare = alt_playback_pcm_prepare,
4120 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004121 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004122};
4123
4124static const struct hda_pcm_stream pcm_analog_alt_capture = {
4125 .substreams = 2, /* can be overridden */
4126 .channels_min = 2,
4127 .channels_max = 2,
4128 /* NID is set in build_pcms */
4129 .ops = {
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004130 .open = alt_capture_pcm_open,
4131 .close = alt_capture_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004132 .prepare = alt_capture_pcm_prepare,
4133 .cleanup = alt_capture_pcm_cleanup
4134 },
4135};
4136
4137static const struct hda_pcm_stream pcm_digital_playback = {
4138 .substreams = 1,
4139 .channels_min = 2,
4140 .channels_max = 2,
4141 /* NID is set in build_pcms */
4142 .ops = {
4143 .open = dig_playback_pcm_open,
4144 .close = dig_playback_pcm_close,
4145 .prepare = dig_playback_pcm_prepare,
4146 .cleanup = dig_playback_pcm_cleanup
4147 },
4148};
4149
4150static const struct hda_pcm_stream pcm_digital_capture = {
4151 .substreams = 1,
4152 .channels_min = 2,
4153 .channels_max = 2,
4154 /* NID is set in build_pcms */
4155};
4156
4157/* Used by build_pcms to flag that a PCM has no playback stream */
4158static const struct hda_pcm_stream pcm_null_stream = {
4159 .substreams = 0,
4160 .channels_min = 0,
4161 .channels_max = 0,
4162};
4163
4164/*
4165 * dynamic changing ADC PCM streams
4166 */
4167static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
4168{
4169 struct hda_gen_spec *spec = codec->spec;
4170 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
4171
4172 if (spec->cur_adc && spec->cur_adc != new_adc) {
4173 /* stream is running, let's swap the current ADC */
4174 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
4175 spec->cur_adc = new_adc;
4176 snd_hda_codec_setup_stream(codec, new_adc,
4177 spec->cur_adc_stream_tag, 0,
4178 spec->cur_adc_format);
4179 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004181 return false;
4182}
4183
4184/* analog capture with dynamic dual-adc changes */
4185static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4186 struct hda_codec *codec,
4187 unsigned int stream_tag,
4188 unsigned int format,
4189 struct snd_pcm_substream *substream)
4190{
4191 struct hda_gen_spec *spec = codec->spec;
4192 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
4193 spec->cur_adc_stream_tag = stream_tag;
4194 spec->cur_adc_format = format;
4195 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
4196 return 0;
4197}
4198
4199static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4200 struct hda_codec *codec,
4201 struct snd_pcm_substream *substream)
4202{
4203 struct hda_gen_spec *spec = codec->spec;
4204 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
4205 spec->cur_adc = 0;
4206 return 0;
4207}
4208
4209static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
4210 .substreams = 1,
4211 .channels_min = 2,
4212 .channels_max = 2,
4213 .nid = 0, /* fill later */
4214 .ops = {
4215 .prepare = dyn_adc_capture_pcm_prepare,
4216 .cleanup = dyn_adc_capture_pcm_cleanup
4217 },
4218};
4219
Takashi Iwaif873e532012-12-20 16:58:39 +01004220static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
4221 const char *chip_name)
4222{
4223 char *p;
4224
4225 if (*str)
4226 return;
4227 strlcpy(str, chip_name, len);
4228
4229 /* drop non-alnum chars after a space */
4230 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
4231 if (!isalnum(p[1])) {
4232 *p = 0;
4233 break;
4234 }
4235 }
4236 strlcat(str, sfx, len);
4237}
4238
Takashi Iwai352f7f92012-12-19 12:52:06 +01004239/* build PCM streams based on the parsed results */
4240int snd_hda_gen_build_pcms(struct hda_codec *codec)
4241{
4242 struct hda_gen_spec *spec = codec->spec;
4243 struct hda_pcm *info = spec->pcm_rec;
4244 const struct hda_pcm_stream *p;
4245 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246
4247 codec->num_pcms = 1;
4248 codec->pcm_info = info;
4249
Takashi Iwai352f7f92012-12-19 12:52:06 +01004250 if (spec->no_analog)
4251 goto skip_analog;
4252
Takashi Iwaif873e532012-12-20 16:58:39 +01004253 fill_pcm_stream_name(spec->stream_name_analog,
4254 sizeof(spec->stream_name_analog),
4255 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004256 info->name = spec->stream_name_analog;
4257
4258 if (spec->multiout.num_dacs > 0) {
4259 p = spec->stream_analog_playback;
4260 if (!p)
4261 p = &pcm_analog_playback;
4262 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4263 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
4264 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
4265 spec->multiout.max_channels;
4266 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
4267 spec->autocfg.line_outs == 2)
4268 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
4269 snd_pcm_2_1_chmaps;
4270 }
4271 if (spec->num_adc_nids) {
4272 p = spec->stream_analog_capture;
4273 if (!p) {
4274 if (spec->dyn_adc_switch)
4275 p = &dyn_adc_pcm_analog_capture;
4276 else
4277 p = &pcm_analog_capture;
4278 }
4279 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4280 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
4281 }
4282
Takashi Iwai352f7f92012-12-19 12:52:06 +01004283 skip_analog:
4284 /* SPDIF for stream index #1 */
4285 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01004286 fill_pcm_stream_name(spec->stream_name_digital,
4287 sizeof(spec->stream_name_digital),
4288 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004289 codec->num_pcms = 2;
4290 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
4291 info = spec->pcm_rec + 1;
4292 info->name = spec->stream_name_digital;
4293 if (spec->dig_out_type)
4294 info->pcm_type = spec->dig_out_type;
4295 else
4296 info->pcm_type = HDA_PCM_TYPE_SPDIF;
4297 if (spec->multiout.dig_out_nid) {
4298 p = spec->stream_digital_playback;
4299 if (!p)
4300 p = &pcm_digital_playback;
4301 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4302 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
4303 }
4304 if (spec->dig_in_nid) {
4305 p = spec->stream_digital_capture;
4306 if (!p)
4307 p = &pcm_digital_capture;
4308 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4309 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
4310 }
4311 }
4312
4313 if (spec->no_analog)
4314 return 0;
4315
4316 /* If the use of more than one ADC is requested for the current
4317 * model, configure a second analog capture-only PCM.
4318 */
4319 have_multi_adcs = (spec->num_adc_nids > 1) &&
4320 !spec->dyn_adc_switch && !spec->auto_mic;
4321 /* Additional Analaog capture for index #2 */
4322 if (spec->alt_dac_nid || have_multi_adcs) {
4323 codec->num_pcms = 3;
4324 info = spec->pcm_rec + 2;
4325 info->name = spec->stream_name_analog;
4326 if (spec->alt_dac_nid) {
4327 p = spec->stream_analog_alt_playback;
4328 if (!p)
4329 p = &pcm_analog_alt_playback;
4330 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4331 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
4332 spec->alt_dac_nid;
4333 } else {
4334 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
4335 pcm_null_stream;
4336 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
4337 }
4338 if (have_multi_adcs) {
4339 p = spec->stream_analog_alt_capture;
4340 if (!p)
4341 p = &pcm_analog_alt_capture;
4342 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4343 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
4344 spec->adc_nids[1];
4345 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
4346 spec->num_adc_nids - 1;
4347 } else {
4348 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
4349 pcm_null_stream;
4350 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
4351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 }
4353
4354 return 0;
4355}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004356EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
4357
4358
4359/*
4360 * Standard auto-parser initializations
4361 */
4362
Takashi Iwaid4156932013-01-07 10:08:02 +01004363/* configure the given path as a proper output */
Takashi Iwai2c12c302013-01-10 09:33:29 +01004364static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004365{
4366 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01004367 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004368
Takashi Iwai196c17662013-01-04 15:01:40 +01004369 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01004370 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004371 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01004372 pin = path->path[path->depth - 1];
Takashi Iwai2c12c302013-01-10 09:33:29 +01004373 restore_pin_ctl(codec, pin);
Takashi Iwaie1284af2013-01-03 16:33:02 +01004374 snd_hda_activate_path(codec, path, path->active, true);
4375 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004376}
4377
4378/* initialize primary output paths */
4379static void init_multi_out(struct hda_codec *codec)
4380{
4381 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004382 int i;
4383
Takashi Iwaid4156932013-01-07 10:08:02 +01004384 for (i = 0; i < spec->autocfg.line_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004385 set_output_and_unmute(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004386}
4387
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004388
Takashi Iwai2c12c302013-01-10 09:33:29 +01004389static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004390{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004391 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004392
Takashi Iwaid4156932013-01-07 10:08:02 +01004393 for (i = 0; i < num_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004394 set_output_and_unmute(codec, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004395}
4396
4397/* initialize hp and speaker paths */
4398static void init_extra_out(struct hda_codec *codec)
4399{
4400 struct hda_gen_spec *spec = codec->spec;
4401
4402 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004403 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004404 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
4405 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai2c12c302013-01-10 09:33:29 +01004406 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004407}
4408
4409/* initialize multi-io paths */
4410static void init_multi_io(struct hda_codec *codec)
4411{
4412 struct hda_gen_spec *spec = codec->spec;
4413 int i;
4414
4415 for (i = 0; i < spec->multi_ios; i++) {
4416 hda_nid_t pin = spec->multi_io[i].pin;
4417 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01004418 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004419 if (!path)
4420 continue;
4421 if (!spec->multi_io[i].ctl_in)
4422 spec->multi_io[i].ctl_in =
Takashi Iwai2c12c302013-01-10 09:33:29 +01004423 snd_hda_codec_get_pin_target(codec, pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004424 snd_hda_activate_path(codec, path, path->active, true);
4425 }
4426}
4427
Takashi Iwai352f7f92012-12-19 12:52:06 +01004428/* set up input pins and loopback paths */
4429static void init_analog_input(struct hda_codec *codec)
4430{
4431 struct hda_gen_spec *spec = codec->spec;
4432 struct auto_pin_cfg *cfg = &spec->autocfg;
4433 int i;
4434
4435 for (i = 0; i < cfg->num_inputs; i++) {
4436 hda_nid_t nid = cfg->inputs[i].pin;
4437 if (is_input_pin(codec, nid))
Takashi Iwai2c12c302013-01-10 09:33:29 +01004438 restore_pin_ctl(codec, nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004439
4440 /* init loopback inputs */
4441 if (spec->mixer_nid) {
4442 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01004443 path = snd_hda_get_path_from_idx(codec, spec->loopback_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004444 if (path)
4445 snd_hda_activate_path(codec, path,
4446 path->active, false);
4447 }
4448 }
4449}
4450
4451/* initialize ADC paths */
4452static void init_input_src(struct hda_codec *codec)
4453{
4454 struct hda_gen_spec *spec = codec->spec;
4455 struct hda_input_mux *imux = &spec->input_mux;
4456 struct nid_path *path;
4457 int i, c, nums;
4458
4459 if (spec->dyn_adc_switch)
4460 nums = 1;
4461 else
4462 nums = spec->num_adc_nids;
4463
4464 for (c = 0; c < nums; c++) {
4465 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01004466 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004467 if (path) {
4468 bool active = path->active;
4469 if (i == spec->cur_mux[c])
4470 active = true;
4471 snd_hda_activate_path(codec, path, active, false);
4472 }
4473 }
4474 }
4475
4476 if (spec->shared_mic_hp)
4477 update_shared_mic_hp(codec, spec->cur_mux[0]);
4478
4479 if (spec->cap_sync_hook)
4480 spec->cap_sync_hook(codec);
4481}
4482
4483/* set right pin controls for digital I/O */
4484static void init_digital(struct hda_codec *codec)
4485{
4486 struct hda_gen_spec *spec = codec->spec;
4487 int i;
4488 hda_nid_t pin;
4489
Takashi Iwaid4156932013-01-07 10:08:02 +01004490 for (i = 0; i < spec->autocfg.dig_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004491 set_output_and_unmute(codec, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004492 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01004493 if (pin) {
4494 struct nid_path *path;
Takashi Iwai2c12c302013-01-10 09:33:29 +01004495 restore_pin_ctl(codec, pin);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01004496 path = snd_hda_get_path_from_idx(codec, spec->digin_path);
4497 if (path)
4498 snd_hda_activate_path(codec, path, path->active, false);
4499 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004500}
4501
Takashi Iwai973e4972012-12-20 15:16:09 +01004502/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
4503 * invalid unsol tags by some reason
4504 */
4505static void clear_unsol_on_unused_pins(struct hda_codec *codec)
4506{
4507 int i;
4508
4509 for (i = 0; i < codec->init_pins.used; i++) {
4510 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
4511 hda_nid_t nid = pin->nid;
4512 if (is_jack_detectable(codec, nid) &&
4513 !snd_hda_jack_tbl_get(codec, nid))
4514 snd_hda_codec_update_cache(codec, nid, 0,
4515 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
4516 }
4517}
4518
Takashi Iwai5187ac12013-01-07 12:52:16 +01004519/*
4520 * initialize the generic spec;
4521 * this can be put as patch_ops.init function
4522 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004523int snd_hda_gen_init(struct hda_codec *codec)
4524{
4525 struct hda_gen_spec *spec = codec->spec;
4526
4527 if (spec->init_hook)
4528 spec->init_hook(codec);
4529
4530 snd_hda_apply_verbs(codec);
4531
Takashi Iwai3bbcd272012-12-20 11:50:58 +01004532 codec->cached_write = 1;
4533
Takashi Iwai352f7f92012-12-19 12:52:06 +01004534 init_multi_out(codec);
4535 init_extra_out(codec);
4536 init_multi_io(codec);
4537 init_analog_input(codec);
4538 init_input_src(codec);
4539 init_digital(codec);
4540
Takashi Iwai973e4972012-12-20 15:16:09 +01004541 clear_unsol_on_unused_pins(codec);
4542
Takashi Iwai352f7f92012-12-19 12:52:06 +01004543 /* call init functions of standard auto-mute helpers */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01004544 update_automute_all(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004545
Takashi Iwai3bbcd272012-12-20 11:50:58 +01004546 snd_hda_codec_flush_amp_cache(codec);
4547 snd_hda_codec_flush_cmd_cache(codec);
4548
Takashi Iwai352f7f92012-12-19 12:52:06 +01004549 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
4550 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
4551
4552 hda_call_check_power_status(codec, 0x01);
4553 return 0;
4554}
Takashi Iwaifce52a32013-01-07 12:42:48 +01004555EXPORT_SYMBOL_HDA(snd_hda_gen_init);
4556
Takashi Iwai5187ac12013-01-07 12:52:16 +01004557/*
4558 * free the generic spec;
4559 * this can be put as patch_ops.free function
4560 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01004561void snd_hda_gen_free(struct hda_codec *codec)
4562{
4563 snd_hda_gen_spec_free(codec->spec);
4564 kfree(codec->spec);
4565 codec->spec = NULL;
4566}
4567EXPORT_SYMBOL_HDA(snd_hda_gen_free);
4568
4569#ifdef CONFIG_PM
Takashi Iwai5187ac12013-01-07 12:52:16 +01004570/*
4571 * check the loopback power save state;
4572 * this can be put as patch_ops.check_power_status function
4573 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01004574int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
4575{
4576 struct hda_gen_spec *spec = codec->spec;
4577 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
4578}
4579EXPORT_SYMBOL_HDA(snd_hda_gen_check_power_status);
4580#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01004581
4582
4583/*
4584 * the generic codec support
4585 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586
Takashi Iwai352f7f92012-12-19 12:52:06 +01004587static const struct hda_codec_ops generic_patch_ops = {
4588 .build_controls = snd_hda_gen_build_controls,
4589 .build_pcms = snd_hda_gen_build_pcms,
4590 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01004591 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004592 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02004593#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01004594 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02004595#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596};
4597
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598int snd_hda_parse_generic_codec(struct hda_codec *codec)
4599{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004600 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601 int err;
4602
Takashi Iwaie560d8d2005-09-09 14:21:46 +02004603 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004604 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004606 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004609 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
4610 if (err < 0)
4611 return err;
4612
4613 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004614 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 goto error;
4616
4617 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 return 0;
4619
Takashi Iwai352f7f92012-12-19 12:52:06 +01004620error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01004621 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 return err;
4623}
Takashi Iwaifce52a32013-01-07 12:42:48 +01004624EXPORT_SYMBOL_HDA(snd_hda_parse_generic_codec);