blob: 7444d2e7a82a7426795a333a2edb5925e35a1a52 [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,
Takashi Iwai9dba2052013-01-18 10:01:15 +01002481 int cfg_idx, int num_adcs,
2482 const char *label, int anchor)
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002483{
2484 struct hda_gen_spec *spec = codec->spec;
2485 struct hda_input_mux *imux = &spec->input_mux;
2486 int imux_idx = imux->num_items;
2487 bool imux_added = false;
2488 int c;
2489
2490 for (c = 0; c < num_adcs; c++) {
2491 struct nid_path *path;
2492 hda_nid_t adc = spec->adc_nids[c];
2493
2494 if (!is_reachable_path(codec, pin, adc))
2495 continue;
2496 path = snd_hda_add_new_path(codec, pin, adc, anchor);
2497 if (!path)
2498 continue;
2499 print_nid_path("input", path);
2500 spec->input_paths[imux_idx][c] =
2501 snd_hda_get_path_idx(codec, path);
2502
2503 if (!imux_added) {
2504 spec->imux_pins[imux->num_items] = pin;
Takashi Iwai9dba2052013-01-18 10:01:15 +01002505 snd_hda_add_imux_item(imux, label, cfg_idx, NULL);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002506 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 Iwai9dba2052013-01-18 10:01:15 +01002516
2517#define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
2518
Takashi Iwai352f7f92012-12-19 12:52:06 +01002519static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002520{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002521 struct hda_gen_spec *spec = codec->spec;
2522 const struct auto_pin_cfg *cfg = &spec->autocfg;
2523 hda_nid_t mixer = spec->mixer_nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002524 int num_adcs;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002525 int i, err, type_idx = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002526 const char *prev_label = NULL;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002527 unsigned int val;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002528
Takashi Iwai352f7f92012-12-19 12:52:06 +01002529 num_adcs = fill_adc_nids(codec);
2530 if (num_adcs < 0)
2531 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002532
Takashi Iwai352f7f92012-12-19 12:52:06 +01002533 for (i = 0; i < cfg->num_inputs; i++) {
2534 hda_nid_t pin;
2535 const char *label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536
Takashi Iwai352f7f92012-12-19 12:52:06 +01002537 pin = cfg->inputs[i].pin;
2538 if (!is_input_pin(codec, pin))
2539 continue;
2540
2541 label = hda_get_autocfg_input_label(codec, cfg, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002542 if (prev_label && !strcmp(label, prev_label))
2543 type_idx++;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002544 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01002545 type_idx = 0;
2546 prev_label = label;
2547
Takashi Iwai2c12c302013-01-10 09:33:29 +01002548 val = PIN_IN;
2549 if (cfg->inputs[i].type == AUTO_PIN_MIC)
2550 val |= snd_hda_get_default_vref(codec, pin);
2551 set_pin_target(codec, pin, val, false);
2552
Takashi Iwai352f7f92012-12-19 12:52:06 +01002553 if (mixer) {
2554 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01002555 err = new_analog_input(codec, i, pin,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002556 label, type_idx, mixer);
2557 if (err < 0)
2558 return err;
2559 }
2560 }
2561
Takashi Iwai9dba2052013-01-18 10:01:15 +01002562 err = parse_capture_source(codec, pin, i,
2563 num_adcs, label, -mixer);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002564 if (err < 0)
2565 return err;
Takashi Iwai294765582013-01-17 09:52:11 +01002566
2567 if (spec->add_in_jack_modes) {
2568 err = create_in_jack_mode(codec, pin);
2569 if (err < 0)
2570 return err;
2571 }
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002572 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002573
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002574 if (mixer && spec->add_stereo_mix_input) {
Takashi Iwai9dba2052013-01-18 10:01:15 +01002575 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002576 "Stereo Mix", 0);
2577 if (err < 0)
2578 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002579 }
2580
2581 return 0;
2582}
2583
2584
2585/*
2586 * input source mux
2587 */
2588
Takashi Iwaic697b712013-01-07 17:09:26 +01002589/* get the input path specified by the given adc and imux indices */
2590static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002591{
2592 struct hda_gen_spec *spec = codec->spec;
David Henningssonb56fa1e2013-01-16 11:45:35 +01002593 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
2594 snd_BUG();
2595 return NULL;
2596 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002597 if (spec->dyn_adc_switch)
2598 adc_idx = spec->dyn_adc_idx[imux_idx];
David Henningssonb56fa1e2013-01-16 11:45:35 +01002599 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_OUTS) {
2600 snd_BUG();
2601 return NULL;
2602 }
Takashi Iwaic697b712013-01-07 17:09:26 +01002603 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002604}
2605
2606static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2607 unsigned int idx);
2608
2609static int mux_enum_info(struct snd_kcontrol *kcontrol,
2610 struct snd_ctl_elem_info *uinfo)
2611{
2612 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2613 struct hda_gen_spec *spec = codec->spec;
2614 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
2615}
2616
2617static int mux_enum_get(struct snd_kcontrol *kcontrol,
2618 struct snd_ctl_elem_value *ucontrol)
2619{
2620 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2621 struct hda_gen_spec *spec = codec->spec;
David Henningssona053d1e2013-01-16 11:45:36 +01002622 unsigned int adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002623
2624 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
2625 return 0;
2626}
2627
2628static int mux_enum_put(struct snd_kcontrol *kcontrol,
2629 struct snd_ctl_elem_value *ucontrol)
2630{
2631 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
David Henningssona053d1e2013-01-16 11:45:36 +01002632 unsigned int adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002633 return mux_select(codec, adc_idx,
2634 ucontrol->value.enumerated.item[0]);
2635}
2636
Takashi Iwai352f7f92012-12-19 12:52:06 +01002637static const struct snd_kcontrol_new cap_src_temp = {
2638 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2639 .name = "Input Source",
2640 .info = mux_enum_info,
2641 .get = mux_enum_get,
2642 .put = mux_enum_put,
2643};
2644
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002645/*
2646 * capture volume and capture switch ctls
2647 */
2648
Takashi Iwai352f7f92012-12-19 12:52:06 +01002649typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
2650 struct snd_ctl_elem_value *ucontrol);
2651
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002652/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002653static int cap_put_caller(struct snd_kcontrol *kcontrol,
2654 struct snd_ctl_elem_value *ucontrol,
2655 put_call_t func, int type)
2656{
2657 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2658 struct hda_gen_spec *spec = codec->spec;
2659 const struct hda_input_mux *imux;
2660 struct nid_path *path;
2661 int i, adc_idx, err = 0;
2662
2663 imux = &spec->input_mux;
David Henningssona053d1e2013-01-16 11:45:36 +01002664 adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002665 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002666 /* we use the cache-only update at first since multiple input paths
2667 * may shared the same amp; by updating only caches, the redundant
2668 * writes to hardware can be reduced.
2669 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002670 codec->cached_write = 1;
2671 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01002672 path = get_input_path(codec, adc_idx, i);
2673 if (!path || !path->ctls[type])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002674 continue;
2675 kcontrol->private_value = path->ctls[type];
2676 err = func(kcontrol, ucontrol);
2677 if (err < 0)
2678 goto error;
2679 }
2680 error:
2681 codec->cached_write = 0;
2682 mutex_unlock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002683 snd_hda_codec_flush_amp_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002684 if (err >= 0 && spec->cap_sync_hook)
2685 spec->cap_sync_hook(codec);
2686 return err;
2687}
2688
2689/* capture volume ctl callbacks */
2690#define cap_vol_info snd_hda_mixer_amp_volume_info
2691#define cap_vol_get snd_hda_mixer_amp_volume_get
2692#define cap_vol_tlv snd_hda_mixer_amp_tlv
2693
2694static int cap_vol_put(struct snd_kcontrol *kcontrol,
2695 struct snd_ctl_elem_value *ucontrol)
2696{
2697 return cap_put_caller(kcontrol, ucontrol,
2698 snd_hda_mixer_amp_volume_put,
2699 NID_PATH_VOL_CTL);
2700}
2701
2702static const struct snd_kcontrol_new cap_vol_temp = {
2703 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2704 .name = "Capture Volume",
2705 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
2706 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2707 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
2708 .info = cap_vol_info,
2709 .get = cap_vol_get,
2710 .put = cap_vol_put,
2711 .tlv = { .c = cap_vol_tlv },
2712};
2713
2714/* capture switch ctl callbacks */
2715#define cap_sw_info snd_ctl_boolean_stereo_info
2716#define cap_sw_get snd_hda_mixer_amp_switch_get
2717
2718static int cap_sw_put(struct snd_kcontrol *kcontrol,
2719 struct snd_ctl_elem_value *ucontrol)
2720{
Takashi Iwaiae177c32013-01-14 12:13:06 +01002721 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2722 struct hda_gen_spec *spec = codec->spec;
2723 int ret;
2724
2725 ret = cap_put_caller(kcontrol, ucontrol,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002726 snd_hda_mixer_amp_switch_put,
2727 NID_PATH_MUTE_CTL);
Takashi Iwaiae177c32013-01-14 12:13:06 +01002728 if (ret < 0)
2729 return ret;
2730
2731 if (spec->capture_switch_hook) {
2732 bool enable = (ucontrol->value.integer.value[0] ||
2733 ucontrol->value.integer.value[1]);
2734 spec->capture_switch_hook(codec, enable);
2735 }
2736
2737 return ret;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002738}
2739
2740static const struct snd_kcontrol_new cap_sw_temp = {
2741 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2742 .name = "Capture Switch",
2743 .info = cap_sw_info,
2744 .get = cap_sw_get,
2745 .put = cap_sw_put,
2746};
2747
2748static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
2749{
2750 hda_nid_t nid;
2751 int i, depth;
2752
2753 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
2754 for (depth = 0; depth < 3; depth++) {
2755 if (depth >= path->depth)
2756 return -EINVAL;
2757 i = path->depth - depth - 1;
2758 nid = path->path[i];
2759 if (!path->ctls[NID_PATH_VOL_CTL]) {
2760 if (nid_has_volume(codec, nid, HDA_OUTPUT))
2761 path->ctls[NID_PATH_VOL_CTL] =
2762 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2763 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
2764 int idx = path->idx[i];
2765 if (!depth && codec->single_adc_amp)
2766 idx = 0;
2767 path->ctls[NID_PATH_VOL_CTL] =
2768 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2769 }
2770 }
2771 if (!path->ctls[NID_PATH_MUTE_CTL]) {
2772 if (nid_has_mute(codec, nid, HDA_OUTPUT))
2773 path->ctls[NID_PATH_MUTE_CTL] =
2774 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2775 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
2776 int idx = path->idx[i];
2777 if (!depth && codec->single_adc_amp)
2778 idx = 0;
2779 path->ctls[NID_PATH_MUTE_CTL] =
2780 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2781 }
2782 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01002783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 return 0;
2785}
2786
Takashi Iwai352f7f92012-12-19 12:52:06 +01002787static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002789 struct hda_gen_spec *spec = codec->spec;
2790 struct auto_pin_cfg *cfg = &spec->autocfg;
2791 unsigned int val;
2792 int i;
2793
2794 if (!spec->inv_dmic_split)
2795 return false;
2796 for (i = 0; i < cfg->num_inputs; i++) {
2797 if (cfg->inputs[i].pin != nid)
2798 continue;
2799 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2800 return false;
2801 val = snd_hda_codec_get_pincfg(codec, nid);
2802 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
2803 }
2804 return false;
2805}
2806
2807static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
2808 int idx, bool is_switch, unsigned int ctl,
2809 bool inv_dmic)
2810{
2811 struct hda_gen_spec *spec = codec->spec;
2812 char tmpname[44];
2813 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
2814 const char *sfx = is_switch ? "Switch" : "Volume";
2815 unsigned int chs = inv_dmic ? 1 : 3;
2816 int err;
2817
2818 if (!ctl)
2819 return 0;
2820
2821 if (label)
2822 snprintf(tmpname, sizeof(tmpname),
2823 "%s Capture %s", label, sfx);
2824 else
2825 snprintf(tmpname, sizeof(tmpname),
2826 "Capture %s", sfx);
2827 err = add_control(spec, type, tmpname, idx,
2828 amp_val_replace_channels(ctl, chs));
2829 if (err < 0 || !inv_dmic)
2830 return err;
2831
2832 /* Make independent right kcontrol */
2833 if (label)
2834 snprintf(tmpname, sizeof(tmpname),
2835 "Inverted %s Capture %s", label, sfx);
2836 else
2837 snprintf(tmpname, sizeof(tmpname),
2838 "Inverted Capture %s", sfx);
2839 return add_control(spec, type, tmpname, idx,
2840 amp_val_replace_channels(ctl, 2));
2841}
2842
2843/* create single (and simple) capture volume and switch controls */
2844static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
2845 unsigned int vol_ctl, unsigned int sw_ctl,
2846 bool inv_dmic)
2847{
2848 int err;
2849 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
2850 if (err < 0)
2851 return err;
2852 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
2853 if (err < 0)
2854 return err;
2855 return 0;
2856}
2857
2858/* create bound capture volume and switch controls */
2859static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
2860 unsigned int vol_ctl, unsigned int sw_ctl)
2861{
2862 struct hda_gen_spec *spec = codec->spec;
2863 struct snd_kcontrol_new *knew;
2864
2865 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002866 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002867 if (!knew)
2868 return -ENOMEM;
2869 knew->index = idx;
2870 knew->private_value = vol_ctl;
2871 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2872 }
2873 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002874 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002875 if (!knew)
2876 return -ENOMEM;
2877 knew->index = idx;
2878 knew->private_value = sw_ctl;
2879 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2880 }
2881 return 0;
2882}
2883
2884/* return the vol ctl when used first in the imux list */
2885static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
2886{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002887 struct nid_path *path;
2888 unsigned int ctl;
2889 int i;
2890
Takashi Iwaic697b712013-01-07 17:09:26 +01002891 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002892 if (!path)
2893 return 0;
2894 ctl = path->ctls[type];
2895 if (!ctl)
2896 return 0;
2897 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01002898 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002899 if (path && path->ctls[type] == ctl)
2900 return 0;
2901 }
2902 return ctl;
2903}
2904
2905/* create individual capture volume and switch controls per input */
2906static int create_multi_cap_vol_ctl(struct hda_codec *codec)
2907{
2908 struct hda_gen_spec *spec = codec->spec;
2909 struct hda_input_mux *imux = &spec->input_mux;
2910 int i, err, type, type_idx = 0;
2911 const char *prev_label = NULL;
2912
2913 for (i = 0; i < imux->num_items; i++) {
2914 const char *label;
2915 bool inv_dmic;
Takashi Iwai9dba2052013-01-18 10:01:15 +01002916
2917 if (imux->items[i].index >= spec->autocfg.num_inputs)
2918 continue;
2919 label = hda_get_autocfg_input_label(codec, &spec->autocfg,
2920 imux->items[i].index);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002921 if (prev_label && !strcmp(label, prev_label))
2922 type_idx++;
2923 else
2924 type_idx = 0;
2925 prev_label = label;
2926 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
2927
2928 for (type = 0; type < 2; type++) {
2929 err = add_single_cap_ctl(codec, label, type_idx, type,
2930 get_first_cap_ctl(codec, i, type),
2931 inv_dmic);
2932 if (err < 0)
2933 return err;
2934 }
2935 }
2936 return 0;
2937}
2938
2939static int create_capture_mixers(struct hda_codec *codec)
2940{
2941 struct hda_gen_spec *spec = codec->spec;
2942 struct hda_input_mux *imux = &spec->input_mux;
2943 int i, n, nums, err;
2944
2945 if (spec->dyn_adc_switch)
2946 nums = 1;
2947 else
2948 nums = spec->num_adc_nids;
2949
2950 if (!spec->auto_mic && imux->num_items > 1) {
2951 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01002952 const char *name;
2953 name = nums > 1 ? "Input Source" : "Capture Source";
2954 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002955 if (!knew)
2956 return -ENOMEM;
2957 knew->count = nums;
2958 }
2959
2960 for (n = 0; n < nums; n++) {
2961 bool multi = false;
David Henningsson99a55922013-01-16 15:58:44 +01002962 bool multi_cap_vol = spec->multi_cap_vol;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002963 bool inv_dmic = false;
2964 int vol, sw;
2965
2966 vol = sw = 0;
2967 for (i = 0; i < imux->num_items; i++) {
2968 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01002969 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002970 if (!path)
2971 continue;
2972 parse_capvol_in_path(codec, path);
2973 if (!vol)
2974 vol = path->ctls[NID_PATH_VOL_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01002975 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002976 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01002977 if (!same_amp_caps(codec, vol,
2978 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
2979 multi_cap_vol = true;
2980 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002981 if (!sw)
2982 sw = path->ctls[NID_PATH_MUTE_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01002983 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002984 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01002985 if (!same_amp_caps(codec, sw,
2986 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
2987 multi_cap_vol = true;
2988 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002989 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
2990 inv_dmic = true;
2991 }
2992
2993 if (!multi)
2994 err = create_single_cap_vol_ctl(codec, n, vol, sw,
2995 inv_dmic);
David Henningsson99a55922013-01-16 15:58:44 +01002996 else if (!multi_cap_vol)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002997 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
2998 else
2999 err = create_multi_cap_vol_ctl(codec);
3000 if (err < 0)
3001 return err;
3002 }
3003
3004 return 0;
3005}
3006
3007/*
3008 * add mic boosts if needed
3009 */
3010static int parse_mic_boost(struct hda_codec *codec)
3011{
3012 struct hda_gen_spec *spec = codec->spec;
3013 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003014 int i, err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003015 int type_idx = 0;
3016 hda_nid_t nid;
3017 const char *prev_label = NULL;
3018
3019 for (i = 0; i < cfg->num_inputs; i++) {
3020 if (cfg->inputs[i].type > AUTO_PIN_MIC)
3021 break;
3022 nid = cfg->inputs[i].pin;
3023 if (get_wcaps(codec, nid) & AC_WCAP_IN_AMP) {
3024 const char *label;
Takashi Iwai5abd4882013-01-07 09:43:18 +01003025 char boost_label[44];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003026 struct nid_path *path;
3027 unsigned int val;
3028
David Henningsson02aba552013-01-16 15:58:43 +01003029 if (!nid_has_volume(codec, nid, HDA_INPUT))
3030 continue;
3031
Takashi Iwai352f7f92012-12-19 12:52:06 +01003032 label = hda_get_autocfg_input_label(codec, cfg, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003033 if (prev_label && !strcmp(label, prev_label))
3034 type_idx++;
3035 else
3036 type_idx = 0;
3037 prev_label = label;
3038
3039 snprintf(boost_label, sizeof(boost_label),
3040 "%s Boost Volume", label);
3041 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
3042 err = add_control(spec, HDA_CTL_WIDGET_VOL,
3043 boost_label, type_idx, val);
3044 if (err < 0)
3045 return err;
3046
3047 path = snd_hda_get_nid_path(codec, nid, 0);
3048 if (path)
3049 path->ctls[NID_PATH_BOOST_CTL] = val;
3050 }
3051 }
3052 return 0;
3053}
3054
3055/*
3056 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3057 */
3058static void parse_digital(struct hda_codec *codec)
3059{
3060 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003061 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003062 int i, nums;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003063 hda_nid_t dig_nid, pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003064
3065 /* support multiple SPDIFs; the secondary is set up as a slave */
3066 nums = 0;
3067 for (i = 0; i < spec->autocfg.dig_outs; i++) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003068 pin = spec->autocfg.dig_out_pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003069 dig_nid = look_for_dac(codec, pin, true);
3070 if (!dig_nid)
3071 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003072 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003073 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003074 continue;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003075 print_nid_path("digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01003076 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01003077 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003078 set_pin_target(codec, pin, PIN_OUT, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003079 if (!nums) {
3080 spec->multiout.dig_out_nid = dig_nid;
3081 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3082 } else {
3083 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3084 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
3085 break;
3086 spec->slave_dig_outs[nums - 1] = dig_nid;
3087 }
3088 nums++;
3089 }
3090
3091 if (spec->autocfg.dig_in_pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003092 pin = spec->autocfg.dig_in_pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003093 dig_nid = codec->start_nid;
3094 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003095 unsigned int wcaps = get_wcaps(codec, dig_nid);
3096 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3097 continue;
3098 if (!(wcaps & AC_WCAP_DIGITAL))
3099 continue;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003100 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003101 if (path) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003102 print_nid_path("digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003103 path->active = true;
3104 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003105 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003106 set_pin_target(codec, pin, PIN_IN, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003107 break;
3108 }
3109 }
3110 }
3111}
3112
3113
3114/*
3115 * input MUX handling
3116 */
3117
3118static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3119
3120/* select the given imux item; either unmute exclusively or select the route */
3121static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3122 unsigned int idx)
3123{
3124 struct hda_gen_spec *spec = codec->spec;
3125 const struct hda_input_mux *imux;
3126 struct nid_path *path;
3127
3128 imux = &spec->input_mux;
3129 if (!imux->num_items)
3130 return 0;
3131
3132 if (idx >= imux->num_items)
3133 idx = imux->num_items - 1;
3134 if (spec->cur_mux[adc_idx] == idx)
3135 return 0;
3136
Takashi Iwaic697b712013-01-07 17:09:26 +01003137 path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003138 if (!path)
3139 return 0;
3140 if (path->active)
3141 snd_hda_activate_path(codec, path, false, false);
3142
3143 spec->cur_mux[adc_idx] = idx;
3144
3145 if (spec->shared_mic_hp)
3146 update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
3147
3148 if (spec->dyn_adc_switch)
3149 dyn_adc_pcm_resetup(codec, idx);
3150
Takashi Iwaic697b712013-01-07 17:09:26 +01003151 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003152 if (!path)
3153 return 0;
3154 if (path->active)
3155 return 0;
3156 snd_hda_activate_path(codec, path, true, false);
3157 if (spec->cap_sync_hook)
3158 spec->cap_sync_hook(codec);
3159 return 1;
3160}
3161
3162
3163/*
3164 * Jack detections for HP auto-mute and mic-switch
3165 */
3166
3167/* check each pin in the given array; returns true if any of them is plugged */
3168static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
3169{
3170 int i, present = 0;
3171
3172 for (i = 0; i < num_pins; i++) {
3173 hda_nid_t nid = pins[i];
3174 if (!nid)
3175 break;
Takashi Iwai0b4df932013-01-10 09:45:13 +01003176 /* don't detect pins retasked as inputs */
3177 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
3178 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003179 present |= snd_hda_jack_detect(codec, nid);
3180 }
3181 return present;
3182}
3183
3184/* standard HP/line-out auto-mute helper */
3185static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
Takashi Iwai2c12c302013-01-10 09:33:29 +01003186 bool mute)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003187{
3188 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003189 int i;
3190
3191 for (i = 0; i < num_pins; i++) {
3192 hda_nid_t nid = pins[i];
3193 unsigned int val;
3194 if (!nid)
3195 break;
3196 /* don't reset VREF value in case it's controlling
3197 * the amp (see alc861_fixup_asus_amp_vref_0f())
3198 */
Takashi Iwai2c12c302013-01-10 09:33:29 +01003199 if (spec->keep_vref_in_automute)
3200 val = snd_hda_codec_get_pin_target(codec, nid) & ~PIN_HP;
3201 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01003202 val = 0;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003203 if (!mute)
3204 val |= snd_hda_codec_get_pin_target(codec, nid);
3205 /* here we call update_pin_ctl() so that the pinctl is changed
3206 * without changing the pinctl target value;
3207 * the original target value will be still referred at the
3208 * init / resume again
3209 */
3210 update_pin_ctl(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01003211 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003212 }
3213}
3214
3215/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003216void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003217{
3218 struct hda_gen_spec *spec = codec->spec;
3219 int on;
3220
3221 /* Control HP pins/amps depending on master_mute state;
3222 * in general, HP pins/amps control should be enabled in all cases,
3223 * but currently set only for master_mute, just to be safe
3224 */
3225 if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
3226 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003227 spec->autocfg.hp_pins, spec->master_mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003228
3229 if (!spec->automute_speaker)
3230 on = 0;
3231 else
3232 on = spec->hp_jack_present | spec->line_jack_present;
3233 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003234 spec->speaker_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003235 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003236 spec->autocfg.speaker_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003237
3238 /* toggle line-out mutes if needed, too */
3239 /* if LO is a copy of either HP or Speaker, don't need to handle it */
3240 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
3241 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
3242 return;
3243 if (!spec->automute_lo)
3244 on = 0;
3245 else
3246 on = spec->hp_jack_present;
3247 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003248 spec->line_out_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003249 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003250 spec->autocfg.line_out_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003251}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003252EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003253
3254static void call_update_outputs(struct hda_codec *codec)
3255{
3256 struct hda_gen_spec *spec = codec->spec;
3257 if (spec->automute_hook)
3258 spec->automute_hook(codec);
3259 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01003260 snd_hda_gen_update_outputs(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003261}
3262
3263/* standard HP-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003264void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003265{
3266 struct hda_gen_spec *spec = codec->spec;
3267
3268 spec->hp_jack_present =
3269 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
3270 spec->autocfg.hp_pins);
3271 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
3272 return;
3273 call_update_outputs(codec);
3274}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003275EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003276
3277/* standard line-out-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003278void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003279{
3280 struct hda_gen_spec *spec = codec->spec;
3281
3282 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3283 return;
3284 /* check LO jack only when it's different from HP */
3285 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
3286 return;
3287
3288 spec->line_jack_present =
3289 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3290 spec->autocfg.line_out_pins);
3291 if (!spec->automute_speaker || !spec->detect_lo)
3292 return;
3293 call_update_outputs(codec);
3294}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003295EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003296
3297/* standard mic auto-switch helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003298void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003299{
3300 struct hda_gen_spec *spec = codec->spec;
3301 int i;
3302
3303 if (!spec->auto_mic)
3304 return;
3305
3306 for (i = spec->am_num_entries - 1; i > 0; i--) {
Takashi Iwai0b4df932013-01-10 09:45:13 +01003307 hda_nid_t pin = spec->am_entry[i].pin;
3308 /* don't detect pins retasked as outputs */
3309 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
3310 continue;
3311 if (snd_hda_jack_detect(codec, pin)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003312 mux_select(codec, 0, spec->am_entry[i].idx);
3313 return;
3314 }
3315 }
3316 mux_select(codec, 0, spec->am_entry[0].idx);
3317}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003318EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003319
Takashi Iwaia5cc2502013-01-16 18:08:55 +01003320/* update jack retasking */
3321static void update_automute_all(struct hda_codec *codec)
3322{
3323 struct hda_gen_spec *spec = codec->spec;
3324
3325 if (spec->hp_automute_hook)
3326 spec->hp_automute_hook(codec, NULL);
3327 else
3328 snd_hda_gen_hp_automute(codec, NULL);
3329 if (spec->line_automute_hook)
3330 spec->line_automute_hook(codec, NULL);
3331 else
3332 snd_hda_gen_line_automute(codec, NULL);
3333 if (spec->mic_autoswitch_hook)
3334 spec->mic_autoswitch_hook(codec, NULL);
3335 else
3336 snd_hda_gen_mic_autoswitch(codec, NULL);
3337}
3338
Takashi Iwai352f7f92012-12-19 12:52:06 +01003339/*
3340 * Auto-Mute mode mixer enum support
3341 */
3342static int automute_mode_info(struct snd_kcontrol *kcontrol,
3343 struct snd_ctl_elem_info *uinfo)
3344{
3345 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3346 struct hda_gen_spec *spec = codec->spec;
3347 static const char * const texts3[] = {
3348 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02003349 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350
Takashi Iwai352f7f92012-12-19 12:52:06 +01003351 if (spec->automute_speaker_possible && spec->automute_lo_possible)
3352 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
3353 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
3354}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355
Takashi Iwai352f7f92012-12-19 12:52:06 +01003356static int automute_mode_get(struct snd_kcontrol *kcontrol,
3357 struct snd_ctl_elem_value *ucontrol)
3358{
3359 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3360 struct hda_gen_spec *spec = codec->spec;
3361 unsigned int val = 0;
3362 if (spec->automute_speaker)
3363 val++;
3364 if (spec->automute_lo)
3365 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003366
Takashi Iwai352f7f92012-12-19 12:52:06 +01003367 ucontrol->value.enumerated.item[0] = val;
3368 return 0;
3369}
3370
3371static int automute_mode_put(struct snd_kcontrol *kcontrol,
3372 struct snd_ctl_elem_value *ucontrol)
3373{
3374 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3375 struct hda_gen_spec *spec = codec->spec;
3376
3377 switch (ucontrol->value.enumerated.item[0]) {
3378 case 0:
3379 if (!spec->automute_speaker && !spec->automute_lo)
3380 return 0;
3381 spec->automute_speaker = 0;
3382 spec->automute_lo = 0;
3383 break;
3384 case 1:
3385 if (spec->automute_speaker_possible) {
3386 if (!spec->automute_lo && spec->automute_speaker)
3387 return 0;
3388 spec->automute_speaker = 1;
3389 spec->automute_lo = 0;
3390 } else if (spec->automute_lo_possible) {
3391 if (spec->automute_lo)
3392 return 0;
3393 spec->automute_lo = 1;
3394 } else
3395 return -EINVAL;
3396 break;
3397 case 2:
3398 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
3399 return -EINVAL;
3400 if (spec->automute_speaker && spec->automute_lo)
3401 return 0;
3402 spec->automute_speaker = 1;
3403 spec->automute_lo = 1;
3404 break;
3405 default:
3406 return -EINVAL;
3407 }
3408 call_update_outputs(codec);
3409 return 1;
3410}
3411
3412static const struct snd_kcontrol_new automute_mode_enum = {
3413 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3414 .name = "Auto-Mute Mode",
3415 .info = automute_mode_info,
3416 .get = automute_mode_get,
3417 .put = automute_mode_put,
3418};
3419
3420static int add_automute_mode_enum(struct hda_codec *codec)
3421{
3422 struct hda_gen_spec *spec = codec->spec;
3423
Takashi Iwai12c93df2012-12-19 14:38:33 +01003424 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01003425 return -ENOMEM;
3426 return 0;
3427}
3428
3429/*
3430 * Check the availability of HP/line-out auto-mute;
3431 * Set up appropriately if really supported
3432 */
3433static int check_auto_mute_availability(struct hda_codec *codec)
3434{
3435 struct hda_gen_spec *spec = codec->spec;
3436 struct auto_pin_cfg *cfg = &spec->autocfg;
3437 int present = 0;
3438 int i, err;
3439
Takashi Iwaif72706b2013-01-16 18:20:07 +01003440 if (spec->suppress_auto_mute)
3441 return 0;
3442
Takashi Iwai352f7f92012-12-19 12:52:06 +01003443 if (cfg->hp_pins[0])
3444 present++;
3445 if (cfg->line_out_pins[0])
3446 present++;
3447 if (cfg->speaker_pins[0])
3448 present++;
3449 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02003450 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003451
3452 if (!cfg->speaker_pins[0] &&
3453 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
3454 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3455 sizeof(cfg->speaker_pins));
3456 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003457 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458
Takashi Iwai352f7f92012-12-19 12:52:06 +01003459 if (!cfg->hp_pins[0] &&
3460 cfg->line_out_type == AUTO_PIN_HP_OUT) {
3461 memcpy(cfg->hp_pins, cfg->line_out_pins,
3462 sizeof(cfg->hp_pins));
3463 cfg->hp_outs = cfg->line_outs;
3464 }
3465
3466 for (i = 0; i < cfg->hp_outs; i++) {
3467 hda_nid_t nid = cfg->hp_pins[i];
3468 if (!is_jack_detectable(codec, nid))
3469 continue;
3470 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
3471 nid);
3472 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003473 spec->hp_automute_hook ?
3474 spec->hp_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003475 snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003476 spec->detect_hp = 1;
3477 }
3478
3479 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
3480 if (cfg->speaker_outs)
3481 for (i = 0; i < cfg->line_outs; i++) {
3482 hda_nid_t nid = cfg->line_out_pins[i];
3483 if (!is_jack_detectable(codec, nid))
3484 continue;
3485 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
3486 snd_hda_jack_detect_enable_callback(codec, nid,
3487 HDA_GEN_FRONT_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003488 spec->line_automute_hook ?
3489 spec->line_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003490 snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003491 spec->detect_lo = 1;
3492 }
3493 spec->automute_lo_possible = spec->detect_hp;
3494 }
3495
3496 spec->automute_speaker_possible = cfg->speaker_outs &&
3497 (spec->detect_hp || spec->detect_lo);
3498
3499 spec->automute_lo = spec->automute_lo_possible;
3500 spec->automute_speaker = spec->automute_speaker_possible;
3501
3502 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
3503 /* create a control for automute mode */
3504 err = add_automute_mode_enum(codec);
3505 if (err < 0)
3506 return err;
3507 }
3508 return 0;
3509}
3510
Takashi Iwai352f7f92012-12-19 12:52:06 +01003511/* check whether all auto-mic pins are valid; setup indices if OK */
3512static bool auto_mic_check_imux(struct hda_codec *codec)
3513{
3514 struct hda_gen_spec *spec = codec->spec;
3515 const struct hda_input_mux *imux;
3516 int i;
3517
3518 imux = &spec->input_mux;
3519 for (i = 0; i < spec->am_num_entries; i++) {
3520 spec->am_entry[i].idx =
3521 find_idx_in_nid_list(spec->am_entry[i].pin,
3522 spec->imux_pins, imux->num_items);
3523 if (spec->am_entry[i].idx < 0)
3524 return false; /* no corresponding imux */
3525 }
3526
3527 /* we don't need the jack detection for the first pin */
3528 for (i = 1; i < spec->am_num_entries; i++)
3529 snd_hda_jack_detect_enable_callback(codec,
3530 spec->am_entry[i].pin,
3531 HDA_GEN_MIC_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003532 spec->mic_autoswitch_hook ?
3533 spec->mic_autoswitch_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003534 snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003535 return true;
3536}
3537
3538static int compare_attr(const void *ap, const void *bp)
3539{
3540 const struct automic_entry *a = ap;
3541 const struct automic_entry *b = bp;
3542 return (int)(a->attr - b->attr);
3543}
3544
3545/*
3546 * Check the availability of auto-mic switch;
3547 * Set up if really supported
3548 */
3549static int check_auto_mic_availability(struct hda_codec *codec)
3550{
3551 struct hda_gen_spec *spec = codec->spec;
3552 struct auto_pin_cfg *cfg = &spec->autocfg;
3553 unsigned int types;
3554 int i, num_pins;
3555
Takashi Iwaid12daf62013-01-07 16:32:11 +01003556 if (spec->suppress_auto_mic)
3557 return 0;
3558
Takashi Iwai352f7f92012-12-19 12:52:06 +01003559 types = 0;
3560 num_pins = 0;
3561 for (i = 0; i < cfg->num_inputs; i++) {
3562 hda_nid_t nid = cfg->inputs[i].pin;
3563 unsigned int attr;
3564 attr = snd_hda_codec_get_pincfg(codec, nid);
3565 attr = snd_hda_get_input_pin_attr(attr);
3566 if (types & (1 << attr))
3567 return 0; /* already occupied */
3568 switch (attr) {
3569 case INPUT_PIN_ATTR_INT:
3570 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3571 return 0; /* invalid type */
3572 break;
3573 case INPUT_PIN_ATTR_UNUSED:
3574 return 0; /* invalid entry */
3575 default:
3576 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
3577 return 0; /* invalid type */
3578 if (!spec->line_in_auto_switch &&
3579 cfg->inputs[i].type != AUTO_PIN_MIC)
3580 return 0; /* only mic is allowed */
3581 if (!is_jack_detectable(codec, nid))
3582 return 0; /* no unsol support */
3583 break;
3584 }
3585 if (num_pins >= MAX_AUTO_MIC_PINS)
3586 return 0;
3587 types |= (1 << attr);
3588 spec->am_entry[num_pins].pin = nid;
3589 spec->am_entry[num_pins].attr = attr;
3590 num_pins++;
3591 }
3592
3593 if (num_pins < 2)
3594 return 0;
3595
3596 spec->am_num_entries = num_pins;
3597 /* sort the am_entry in the order of attr so that the pin with a
3598 * higher attr will be selected when the jack is plugged.
3599 */
3600 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
3601 compare_attr, NULL);
3602
3603 if (!auto_mic_check_imux(codec))
3604 return 0;
3605
3606 spec->auto_mic = 1;
3607 spec->num_adc_nids = 1;
3608 spec->cur_mux[0] = spec->am_entry[0].idx;
3609 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
3610 spec->am_entry[0].pin,
3611 spec->am_entry[1].pin,
3612 spec->am_entry[2].pin);
3613
3614 return 0;
3615}
3616
3617
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003618/*
3619 * Parse the given BIOS configuration and set up the hda_gen_spec
3620 *
3621 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003622 * or a negative error code
3623 */
3624int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003625 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003626{
3627 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003628 int err;
3629
Takashi Iwai1c70a582013-01-11 17:48:22 +01003630 parse_user_hints(codec);
3631
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003632 if (cfg != &spec->autocfg) {
3633 spec->autocfg = *cfg;
3634 cfg = &spec->autocfg;
3635 }
3636
David Henningsson6fc4cb92013-01-16 15:58:45 +01003637 fill_all_dac_nids(codec);
3638
Takashi Iwai352f7f92012-12-19 12:52:06 +01003639 if (!cfg->line_outs) {
3640 if (cfg->dig_outs || cfg->dig_in_pin) {
3641 spec->multiout.max_channels = 2;
3642 spec->no_analog = 1;
3643 goto dig_only;
3644 }
3645 return 0; /* can't find valid BIOS pin config */
3646 }
3647
3648 if (!spec->no_primary_hp &&
3649 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
3650 cfg->line_outs <= cfg->hp_outs) {
3651 /* use HP as primary out */
3652 cfg->speaker_outs = cfg->line_outs;
3653 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3654 sizeof(cfg->speaker_pins));
3655 cfg->line_outs = cfg->hp_outs;
3656 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
3657 cfg->hp_outs = 0;
3658 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3659 cfg->line_out_type = AUTO_PIN_HP_OUT;
3660 }
3661
3662 err = parse_output_paths(codec);
3663 if (err < 0)
3664 return err;
3665 err = create_multi_channel_mode(codec);
3666 if (err < 0)
3667 return err;
3668 err = create_multi_out_ctls(codec, cfg);
3669 if (err < 0)
3670 return err;
3671 err = create_hp_out_ctls(codec);
3672 if (err < 0)
3673 return err;
3674 err = create_speaker_out_ctls(codec);
3675 if (err < 0)
3676 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003677 err = create_indep_hp_ctls(codec);
3678 if (err < 0)
3679 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01003680 err = create_loopback_mixing_ctl(codec);
3681 if (err < 0)
3682 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003683 err = create_shared_input(codec);
3684 if (err < 0)
3685 return err;
3686 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003687 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02003688 return err;
3689
Takashi Iwaia07a9492013-01-07 16:44:06 +01003690 spec->const_channel_count = spec->ext_channel_count;
3691 /* check the multiple speaker and headphone pins */
3692 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
3693 spec->const_channel_count = max(spec->const_channel_count,
3694 cfg->speaker_outs * 2);
3695 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
3696 spec->const_channel_count = max(spec->const_channel_count,
3697 cfg->hp_outs * 2);
3698 spec->multiout.max_channels = max(spec->ext_channel_count,
3699 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003700
3701 err = check_auto_mute_availability(codec);
3702 if (err < 0)
3703 return err;
3704
3705 err = check_dyn_adc_switch(codec);
3706 if (err < 0)
3707 return err;
3708
3709 if (!spec->shared_mic_hp) {
3710 err = check_auto_mic_availability(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003711 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 }
Takashi Iwai071c73a2006-08-23 18:34:06 +02003714
Takashi Iwai352f7f92012-12-19 12:52:06 +01003715 err = create_capture_mixers(codec);
3716 if (err < 0)
3717 return err;
3718
3719 err = parse_mic_boost(codec);
3720 if (err < 0)
3721 return err;
3722
Takashi Iwai978e77e2013-01-10 16:57:58 +01003723 if (spec->add_out_jack_modes) {
3724 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
3725 err = create_out_jack_modes(codec, cfg->line_outs,
3726 cfg->line_out_pins);
3727 if (err < 0)
3728 return err;
3729 }
3730 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
3731 err = create_out_jack_modes(codec, cfg->hp_outs,
3732 cfg->hp_pins);
3733 if (err < 0)
3734 return err;
3735 }
3736 }
3737
Takashi Iwai352f7f92012-12-19 12:52:06 +01003738 dig_only:
3739 parse_digital(codec);
3740
3741 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003743EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744
3745
3746/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003747 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003749
3750/* slave controls for virtual master */
3751static const char * const slave_pfxs[] = {
3752 "Front", "Surround", "Center", "LFE", "Side",
3753 "Headphone", "Speaker", "Mono", "Line Out",
3754 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01003755 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
3756 "Headphone Front", "Headphone Surround", "Headphone CLFE",
3757 "Headphone Side",
Takashi Iwai352f7f92012-12-19 12:52:06 +01003758 NULL,
3759};
3760
3761int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003763 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765
Takashi Iwai36502d02012-12-19 15:15:10 +01003766 if (spec->kctls.used) {
3767 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
3768 if (err < 0)
3769 return err;
3770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771
Takashi Iwai352f7f92012-12-19 12:52:06 +01003772 if (spec->multiout.dig_out_nid) {
3773 err = snd_hda_create_dig_out_ctls(codec,
3774 spec->multiout.dig_out_nid,
3775 spec->multiout.dig_out_nid,
3776 spec->pcm_rec[1].pcm_type);
3777 if (err < 0)
3778 return err;
3779 if (!spec->no_analog) {
3780 err = snd_hda_create_spdif_share_sw(codec,
3781 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 if (err < 0)
3783 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003784 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 }
3786 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003787 if (spec->dig_in_nid) {
3788 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
3789 if (err < 0)
3790 return err;
3791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792
Takashi Iwai352f7f92012-12-19 12:52:06 +01003793 /* if we have no master control, let's create it */
3794 if (!spec->no_analog &&
3795 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003796 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01003797 spec->vmaster_tlv, slave_pfxs,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003798 "Playback Volume");
3799 if (err < 0)
3800 return err;
3801 }
3802 if (!spec->no_analog &&
3803 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
3804 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
3805 NULL, slave_pfxs,
3806 "Playback Switch",
3807 true, &spec->vmaster_mute.sw_kctl);
3808 if (err < 0)
3809 return err;
3810 if (spec->vmaster_mute.hook)
Takashi Iwaifd25a972012-12-20 14:57:18 +01003811 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
3812 spec->vmaster_mute_enum);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814
Takashi Iwai352f7f92012-12-19 12:52:06 +01003815 free_kctls(spec); /* no longer needed */
3816
3817 if (spec->shared_mic_hp) {
3818 int err;
3819 int nid = spec->autocfg.inputs[1].pin;
3820 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
3821 if (err < 0)
3822 return err;
3823 err = snd_hda_jack_detect_enable(codec, nid, 0);
3824 if (err < 0)
3825 return err;
3826 }
3827
3828 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
3829 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830 return err;
3831
3832 return 0;
3833}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003834EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
3835
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836
3837/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003838 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003841static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
3842 struct hda_codec *codec,
3843 struct snd_pcm_substream *substream,
3844 int action)
3845{
3846 struct hda_gen_spec *spec = codec->spec;
3847 if (spec->pcm_playback_hook)
3848 spec->pcm_playback_hook(hinfo, codec, substream, action);
3849}
3850
Takashi Iwaiac2e8732013-01-17 15:57:10 +01003851static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
3852 struct hda_codec *codec,
3853 struct snd_pcm_substream *substream,
3854 int action)
3855{
3856 struct hda_gen_spec *spec = codec->spec;
3857 if (spec->pcm_capture_hook)
3858 spec->pcm_capture_hook(hinfo, codec, substream, action);
3859}
3860
Takashi Iwai352f7f92012-12-19 12:52:06 +01003861/*
3862 * Analog playback callbacks
3863 */
3864static int playback_pcm_open(struct hda_pcm_stream *hinfo,
3865 struct hda_codec *codec,
3866 struct snd_pcm_substream *substream)
3867{
3868 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003869 int err;
3870
3871 mutex_lock(&spec->pcm_mutex);
3872 err = snd_hda_multi_out_analog_open(codec,
3873 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003874 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003875 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003876 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003877 call_pcm_playback_hook(hinfo, codec, substream,
3878 HDA_GEN_PCM_ACT_OPEN);
3879 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003880 mutex_unlock(&spec->pcm_mutex);
3881 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003882}
3883
3884static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01003885 struct hda_codec *codec,
3886 unsigned int stream_tag,
3887 unsigned int format,
3888 struct snd_pcm_substream *substream)
3889{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003890 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003891 int err;
3892
3893 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
3894 stream_tag, format, substream);
3895 if (!err)
3896 call_pcm_playback_hook(hinfo, codec, substream,
3897 HDA_GEN_PCM_ACT_PREPARE);
3898 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003899}
Takashi Iwai97ec5582006-03-21 11:29:07 +01003900
Takashi Iwai352f7f92012-12-19 12:52:06 +01003901static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3902 struct hda_codec *codec,
3903 struct snd_pcm_substream *substream)
3904{
3905 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003906 int err;
3907
3908 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
3909 if (!err)
3910 call_pcm_playback_hook(hinfo, codec, substream,
3911 HDA_GEN_PCM_ACT_CLEANUP);
3912 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003913}
3914
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003915static int playback_pcm_close(struct hda_pcm_stream *hinfo,
3916 struct hda_codec *codec,
3917 struct snd_pcm_substream *substream)
3918{
3919 struct hda_gen_spec *spec = codec->spec;
3920 mutex_lock(&spec->pcm_mutex);
3921 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003922 call_pcm_playback_hook(hinfo, codec, substream,
3923 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003924 mutex_unlock(&spec->pcm_mutex);
3925 return 0;
3926}
3927
Takashi Iwaiac2e8732013-01-17 15:57:10 +01003928static int capture_pcm_open(struct hda_pcm_stream *hinfo,
3929 struct hda_codec *codec,
3930 struct snd_pcm_substream *substream)
3931{
3932 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
3933 return 0;
3934}
3935
3936static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3937 struct hda_codec *codec,
3938 unsigned int stream_tag,
3939 unsigned int format,
3940 struct snd_pcm_substream *substream)
3941{
3942 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3943 call_pcm_capture_hook(hinfo, codec, substream,
3944 HDA_GEN_PCM_ACT_PREPARE);
3945 return 0;
3946}
3947
3948static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3949 struct hda_codec *codec,
3950 struct snd_pcm_substream *substream)
3951{
3952 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
3953 call_pcm_capture_hook(hinfo, codec, substream,
3954 HDA_GEN_PCM_ACT_CLEANUP);
3955 return 0;
3956}
3957
3958static int capture_pcm_close(struct hda_pcm_stream *hinfo,
3959 struct hda_codec *codec,
3960 struct snd_pcm_substream *substream)
3961{
3962 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
3963 return 0;
3964}
3965
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003966static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
3967 struct hda_codec *codec,
3968 struct snd_pcm_substream *substream)
3969{
3970 struct hda_gen_spec *spec = codec->spec;
3971 int err = 0;
3972
3973 mutex_lock(&spec->pcm_mutex);
3974 if (!spec->indep_hp_enabled)
3975 err = -EBUSY;
3976 else
3977 spec->active_streams |= 1 << STREAM_INDEP_HP;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003978 call_pcm_playback_hook(hinfo, codec, substream,
3979 HDA_GEN_PCM_ACT_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003980 mutex_unlock(&spec->pcm_mutex);
3981 return err;
3982}
3983
3984static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
3985 struct hda_codec *codec,
3986 struct snd_pcm_substream *substream)
3987{
3988 struct hda_gen_spec *spec = codec->spec;
3989 mutex_lock(&spec->pcm_mutex);
3990 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003991 call_pcm_playback_hook(hinfo, codec, substream,
3992 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003993 mutex_unlock(&spec->pcm_mutex);
3994 return 0;
3995}
3996
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003997static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3998 struct hda_codec *codec,
3999 unsigned int stream_tag,
4000 unsigned int format,
4001 struct snd_pcm_substream *substream)
4002{
4003 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4004 call_pcm_playback_hook(hinfo, codec, substream,
4005 HDA_GEN_PCM_ACT_PREPARE);
4006 return 0;
4007}
4008
4009static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4010 struct hda_codec *codec,
4011 struct snd_pcm_substream *substream)
4012{
4013 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4014 call_pcm_playback_hook(hinfo, codec, substream,
4015 HDA_GEN_PCM_ACT_CLEANUP);
4016 return 0;
4017}
4018
Takashi Iwai352f7f92012-12-19 12:52:06 +01004019/*
4020 * Digital out
4021 */
4022static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
4023 struct hda_codec *codec,
4024 struct snd_pcm_substream *substream)
4025{
4026 struct hda_gen_spec *spec = codec->spec;
4027 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
4028}
4029
4030static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4031 struct hda_codec *codec,
4032 unsigned int stream_tag,
4033 unsigned int format,
4034 struct snd_pcm_substream *substream)
4035{
4036 struct hda_gen_spec *spec = codec->spec;
4037 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
4038 stream_tag, format, substream);
4039}
4040
4041static int dig_playback_pcm_cleanup(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_cleanup(codec, &spec->multiout);
4047}
4048
4049static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
4050 struct hda_codec *codec,
4051 struct snd_pcm_substream *substream)
4052{
4053 struct hda_gen_spec *spec = codec->spec;
4054 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
4055}
4056
4057/*
4058 * Analog capture
4059 */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004060#define alt_capture_pcm_open capture_pcm_open
4061#define alt_capture_pcm_close capture_pcm_close
4062
Takashi Iwai352f7f92012-12-19 12:52:06 +01004063static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4064 struct hda_codec *codec,
4065 unsigned int stream_tag,
4066 unsigned int format,
4067 struct snd_pcm_substream *substream)
4068{
4069 struct hda_gen_spec *spec = codec->spec;
4070
4071 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01004072 stream_tag, 0, format);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004073 call_pcm_capture_hook(hinfo, codec, substream,
4074 HDA_GEN_PCM_ACT_PREPARE);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004075 return 0;
4076}
4077
Takashi Iwai352f7f92012-12-19 12:52:06 +01004078static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4079 struct hda_codec *codec,
4080 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01004081{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004082 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01004083
Takashi Iwai352f7f92012-12-19 12:52:06 +01004084 snd_hda_codec_cleanup_stream(codec,
4085 spec->adc_nids[substream->number + 1]);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004086 call_pcm_capture_hook(hinfo, codec, substream,
4087 HDA_GEN_PCM_ACT_CLEANUP);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004088 return 0;
4089}
4090
Takashi Iwai352f7f92012-12-19 12:52:06 +01004091/*
4092 */
4093static const struct hda_pcm_stream pcm_analog_playback = {
4094 .substreams = 1,
4095 .channels_min = 2,
4096 .channels_max = 8,
4097 /* NID is set in build_pcms */
4098 .ops = {
4099 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004100 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004101 .prepare = playback_pcm_prepare,
4102 .cleanup = playback_pcm_cleanup
4103 },
4104};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105
Takashi Iwai352f7f92012-12-19 12:52:06 +01004106static const struct hda_pcm_stream pcm_analog_capture = {
4107 .substreams = 1,
4108 .channels_min = 2,
4109 .channels_max = 2,
4110 /* NID is set in build_pcms */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004111 .ops = {
4112 .open = capture_pcm_open,
4113 .close = capture_pcm_close,
4114 .prepare = capture_pcm_prepare,
4115 .cleanup = capture_pcm_cleanup
4116 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004117};
4118
4119static const struct hda_pcm_stream pcm_analog_alt_playback = {
4120 .substreams = 1,
4121 .channels_min = 2,
4122 .channels_max = 2,
4123 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004124 .ops = {
4125 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004126 .close = alt_playback_pcm_close,
4127 .prepare = alt_playback_pcm_prepare,
4128 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004129 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004130};
4131
4132static const struct hda_pcm_stream pcm_analog_alt_capture = {
4133 .substreams = 2, /* can be overridden */
4134 .channels_min = 2,
4135 .channels_max = 2,
4136 /* NID is set in build_pcms */
4137 .ops = {
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004138 .open = alt_capture_pcm_open,
4139 .close = alt_capture_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004140 .prepare = alt_capture_pcm_prepare,
4141 .cleanup = alt_capture_pcm_cleanup
4142 },
4143};
4144
4145static const struct hda_pcm_stream pcm_digital_playback = {
4146 .substreams = 1,
4147 .channels_min = 2,
4148 .channels_max = 2,
4149 /* NID is set in build_pcms */
4150 .ops = {
4151 .open = dig_playback_pcm_open,
4152 .close = dig_playback_pcm_close,
4153 .prepare = dig_playback_pcm_prepare,
4154 .cleanup = dig_playback_pcm_cleanup
4155 },
4156};
4157
4158static const struct hda_pcm_stream pcm_digital_capture = {
4159 .substreams = 1,
4160 .channels_min = 2,
4161 .channels_max = 2,
4162 /* NID is set in build_pcms */
4163};
4164
4165/* Used by build_pcms to flag that a PCM has no playback stream */
4166static const struct hda_pcm_stream pcm_null_stream = {
4167 .substreams = 0,
4168 .channels_min = 0,
4169 .channels_max = 0,
4170};
4171
4172/*
4173 * dynamic changing ADC PCM streams
4174 */
4175static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
4176{
4177 struct hda_gen_spec *spec = codec->spec;
4178 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
4179
4180 if (spec->cur_adc && spec->cur_adc != new_adc) {
4181 /* stream is running, let's swap the current ADC */
4182 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
4183 spec->cur_adc = new_adc;
4184 snd_hda_codec_setup_stream(codec, new_adc,
4185 spec->cur_adc_stream_tag, 0,
4186 spec->cur_adc_format);
4187 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004189 return false;
4190}
4191
4192/* analog capture with dynamic dual-adc changes */
4193static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4194 struct hda_codec *codec,
4195 unsigned int stream_tag,
4196 unsigned int format,
4197 struct snd_pcm_substream *substream)
4198{
4199 struct hda_gen_spec *spec = codec->spec;
4200 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
4201 spec->cur_adc_stream_tag = stream_tag;
4202 spec->cur_adc_format = format;
4203 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
4204 return 0;
4205}
4206
4207static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4208 struct hda_codec *codec,
4209 struct snd_pcm_substream *substream)
4210{
4211 struct hda_gen_spec *spec = codec->spec;
4212 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
4213 spec->cur_adc = 0;
4214 return 0;
4215}
4216
4217static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
4218 .substreams = 1,
4219 .channels_min = 2,
4220 .channels_max = 2,
4221 .nid = 0, /* fill later */
4222 .ops = {
4223 .prepare = dyn_adc_capture_pcm_prepare,
4224 .cleanup = dyn_adc_capture_pcm_cleanup
4225 },
4226};
4227
Takashi Iwaif873e532012-12-20 16:58:39 +01004228static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
4229 const char *chip_name)
4230{
4231 char *p;
4232
4233 if (*str)
4234 return;
4235 strlcpy(str, chip_name, len);
4236
4237 /* drop non-alnum chars after a space */
4238 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
4239 if (!isalnum(p[1])) {
4240 *p = 0;
4241 break;
4242 }
4243 }
4244 strlcat(str, sfx, len);
4245}
4246
Takashi Iwai352f7f92012-12-19 12:52:06 +01004247/* build PCM streams based on the parsed results */
4248int snd_hda_gen_build_pcms(struct hda_codec *codec)
4249{
4250 struct hda_gen_spec *spec = codec->spec;
4251 struct hda_pcm *info = spec->pcm_rec;
4252 const struct hda_pcm_stream *p;
4253 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254
4255 codec->num_pcms = 1;
4256 codec->pcm_info = info;
4257
Takashi Iwai352f7f92012-12-19 12:52:06 +01004258 if (spec->no_analog)
4259 goto skip_analog;
4260
Takashi Iwaif873e532012-12-20 16:58:39 +01004261 fill_pcm_stream_name(spec->stream_name_analog,
4262 sizeof(spec->stream_name_analog),
4263 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004264 info->name = spec->stream_name_analog;
4265
4266 if (spec->multiout.num_dacs > 0) {
4267 p = spec->stream_analog_playback;
4268 if (!p)
4269 p = &pcm_analog_playback;
4270 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4271 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
4272 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
4273 spec->multiout.max_channels;
4274 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
4275 spec->autocfg.line_outs == 2)
4276 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
4277 snd_pcm_2_1_chmaps;
4278 }
4279 if (spec->num_adc_nids) {
4280 p = spec->stream_analog_capture;
4281 if (!p) {
4282 if (spec->dyn_adc_switch)
4283 p = &dyn_adc_pcm_analog_capture;
4284 else
4285 p = &pcm_analog_capture;
4286 }
4287 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4288 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
4289 }
4290
Takashi Iwai352f7f92012-12-19 12:52:06 +01004291 skip_analog:
4292 /* SPDIF for stream index #1 */
4293 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01004294 fill_pcm_stream_name(spec->stream_name_digital,
4295 sizeof(spec->stream_name_digital),
4296 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004297 codec->num_pcms = 2;
4298 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
4299 info = spec->pcm_rec + 1;
4300 info->name = spec->stream_name_digital;
4301 if (spec->dig_out_type)
4302 info->pcm_type = spec->dig_out_type;
4303 else
4304 info->pcm_type = HDA_PCM_TYPE_SPDIF;
4305 if (spec->multiout.dig_out_nid) {
4306 p = spec->stream_digital_playback;
4307 if (!p)
4308 p = &pcm_digital_playback;
4309 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4310 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
4311 }
4312 if (spec->dig_in_nid) {
4313 p = spec->stream_digital_capture;
4314 if (!p)
4315 p = &pcm_digital_capture;
4316 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4317 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
4318 }
4319 }
4320
4321 if (spec->no_analog)
4322 return 0;
4323
4324 /* If the use of more than one ADC is requested for the current
4325 * model, configure a second analog capture-only PCM.
4326 */
4327 have_multi_adcs = (spec->num_adc_nids > 1) &&
4328 !spec->dyn_adc_switch && !spec->auto_mic;
4329 /* Additional Analaog capture for index #2 */
4330 if (spec->alt_dac_nid || have_multi_adcs) {
4331 codec->num_pcms = 3;
4332 info = spec->pcm_rec + 2;
4333 info->name = spec->stream_name_analog;
4334 if (spec->alt_dac_nid) {
4335 p = spec->stream_analog_alt_playback;
4336 if (!p)
4337 p = &pcm_analog_alt_playback;
4338 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4339 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
4340 spec->alt_dac_nid;
4341 } else {
4342 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
4343 pcm_null_stream;
4344 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
4345 }
4346 if (have_multi_adcs) {
4347 p = spec->stream_analog_alt_capture;
4348 if (!p)
4349 p = &pcm_analog_alt_capture;
4350 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4351 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
4352 spec->adc_nids[1];
4353 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
4354 spec->num_adc_nids - 1;
4355 } else {
4356 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
4357 pcm_null_stream;
4358 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
4359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 }
4361
4362 return 0;
4363}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004364EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
4365
4366
4367/*
4368 * Standard auto-parser initializations
4369 */
4370
Takashi Iwaid4156932013-01-07 10:08:02 +01004371/* configure the given path as a proper output */
Takashi Iwai2c12c302013-01-10 09:33:29 +01004372static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004373{
4374 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01004375 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004376
Takashi Iwai196c17662013-01-04 15:01:40 +01004377 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01004378 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004379 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01004380 pin = path->path[path->depth - 1];
Takashi Iwai2c12c302013-01-10 09:33:29 +01004381 restore_pin_ctl(codec, pin);
Takashi Iwaie1284af2013-01-03 16:33:02 +01004382 snd_hda_activate_path(codec, path, path->active, true);
4383 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004384}
4385
4386/* initialize primary output paths */
4387static void init_multi_out(struct hda_codec *codec)
4388{
4389 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004390 int i;
4391
Takashi Iwaid4156932013-01-07 10:08:02 +01004392 for (i = 0; i < spec->autocfg.line_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004393 set_output_and_unmute(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004394}
4395
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004396
Takashi Iwai2c12c302013-01-10 09:33:29 +01004397static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004398{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004399 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004400
Takashi Iwaid4156932013-01-07 10:08:02 +01004401 for (i = 0; i < num_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004402 set_output_and_unmute(codec, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004403}
4404
4405/* initialize hp and speaker paths */
4406static void init_extra_out(struct hda_codec *codec)
4407{
4408 struct hda_gen_spec *spec = codec->spec;
4409
4410 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004411 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004412 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
4413 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai2c12c302013-01-10 09:33:29 +01004414 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004415}
4416
4417/* initialize multi-io paths */
4418static void init_multi_io(struct hda_codec *codec)
4419{
4420 struct hda_gen_spec *spec = codec->spec;
4421 int i;
4422
4423 for (i = 0; i < spec->multi_ios; i++) {
4424 hda_nid_t pin = spec->multi_io[i].pin;
4425 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01004426 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004427 if (!path)
4428 continue;
4429 if (!spec->multi_io[i].ctl_in)
4430 spec->multi_io[i].ctl_in =
Takashi Iwai2c12c302013-01-10 09:33:29 +01004431 snd_hda_codec_get_pin_target(codec, pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004432 snd_hda_activate_path(codec, path, path->active, true);
4433 }
4434}
4435
Takashi Iwai352f7f92012-12-19 12:52:06 +01004436/* set up input pins and loopback paths */
4437static void init_analog_input(struct hda_codec *codec)
4438{
4439 struct hda_gen_spec *spec = codec->spec;
4440 struct auto_pin_cfg *cfg = &spec->autocfg;
4441 int i;
4442
4443 for (i = 0; i < cfg->num_inputs; i++) {
4444 hda_nid_t nid = cfg->inputs[i].pin;
4445 if (is_input_pin(codec, nid))
Takashi Iwai2c12c302013-01-10 09:33:29 +01004446 restore_pin_ctl(codec, nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004447
4448 /* init loopback inputs */
4449 if (spec->mixer_nid) {
4450 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01004451 path = snd_hda_get_path_from_idx(codec, spec->loopback_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004452 if (path)
4453 snd_hda_activate_path(codec, path,
4454 path->active, false);
4455 }
4456 }
4457}
4458
4459/* initialize ADC paths */
4460static void init_input_src(struct hda_codec *codec)
4461{
4462 struct hda_gen_spec *spec = codec->spec;
4463 struct hda_input_mux *imux = &spec->input_mux;
4464 struct nid_path *path;
4465 int i, c, nums;
4466
4467 if (spec->dyn_adc_switch)
4468 nums = 1;
4469 else
4470 nums = spec->num_adc_nids;
4471
4472 for (c = 0; c < nums; c++) {
4473 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01004474 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004475 if (path) {
4476 bool active = path->active;
4477 if (i == spec->cur_mux[c])
4478 active = true;
4479 snd_hda_activate_path(codec, path, active, false);
4480 }
4481 }
4482 }
4483
4484 if (spec->shared_mic_hp)
4485 update_shared_mic_hp(codec, spec->cur_mux[0]);
4486
4487 if (spec->cap_sync_hook)
4488 spec->cap_sync_hook(codec);
4489}
4490
4491/* set right pin controls for digital I/O */
4492static void init_digital(struct hda_codec *codec)
4493{
4494 struct hda_gen_spec *spec = codec->spec;
4495 int i;
4496 hda_nid_t pin;
4497
Takashi Iwaid4156932013-01-07 10:08:02 +01004498 for (i = 0; i < spec->autocfg.dig_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004499 set_output_and_unmute(codec, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004500 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01004501 if (pin) {
4502 struct nid_path *path;
Takashi Iwai2c12c302013-01-10 09:33:29 +01004503 restore_pin_ctl(codec, pin);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01004504 path = snd_hda_get_path_from_idx(codec, spec->digin_path);
4505 if (path)
4506 snd_hda_activate_path(codec, path, path->active, false);
4507 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004508}
4509
Takashi Iwai973e4972012-12-20 15:16:09 +01004510/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
4511 * invalid unsol tags by some reason
4512 */
4513static void clear_unsol_on_unused_pins(struct hda_codec *codec)
4514{
4515 int i;
4516
4517 for (i = 0; i < codec->init_pins.used; i++) {
4518 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
4519 hda_nid_t nid = pin->nid;
4520 if (is_jack_detectable(codec, nid) &&
4521 !snd_hda_jack_tbl_get(codec, nid))
4522 snd_hda_codec_update_cache(codec, nid, 0,
4523 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
4524 }
4525}
4526
Takashi Iwai5187ac12013-01-07 12:52:16 +01004527/*
4528 * initialize the generic spec;
4529 * this can be put as patch_ops.init function
4530 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004531int snd_hda_gen_init(struct hda_codec *codec)
4532{
4533 struct hda_gen_spec *spec = codec->spec;
4534
4535 if (spec->init_hook)
4536 spec->init_hook(codec);
4537
4538 snd_hda_apply_verbs(codec);
4539
Takashi Iwai3bbcd272012-12-20 11:50:58 +01004540 codec->cached_write = 1;
4541
Takashi Iwai352f7f92012-12-19 12:52:06 +01004542 init_multi_out(codec);
4543 init_extra_out(codec);
4544 init_multi_io(codec);
4545 init_analog_input(codec);
4546 init_input_src(codec);
4547 init_digital(codec);
4548
Takashi Iwai973e4972012-12-20 15:16:09 +01004549 clear_unsol_on_unused_pins(codec);
4550
Takashi Iwai352f7f92012-12-19 12:52:06 +01004551 /* call init functions of standard auto-mute helpers */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01004552 update_automute_all(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004553
Takashi Iwai3bbcd272012-12-20 11:50:58 +01004554 snd_hda_codec_flush_amp_cache(codec);
4555 snd_hda_codec_flush_cmd_cache(codec);
4556
Takashi Iwai352f7f92012-12-19 12:52:06 +01004557 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
4558 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
4559
4560 hda_call_check_power_status(codec, 0x01);
4561 return 0;
4562}
Takashi Iwaifce52a32013-01-07 12:42:48 +01004563EXPORT_SYMBOL_HDA(snd_hda_gen_init);
4564
Takashi Iwai5187ac12013-01-07 12:52:16 +01004565/*
4566 * free the generic spec;
4567 * this can be put as patch_ops.free function
4568 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01004569void snd_hda_gen_free(struct hda_codec *codec)
4570{
4571 snd_hda_gen_spec_free(codec->spec);
4572 kfree(codec->spec);
4573 codec->spec = NULL;
4574}
4575EXPORT_SYMBOL_HDA(snd_hda_gen_free);
4576
4577#ifdef CONFIG_PM
Takashi Iwai5187ac12013-01-07 12:52:16 +01004578/*
4579 * check the loopback power save state;
4580 * this can be put as patch_ops.check_power_status function
4581 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01004582int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
4583{
4584 struct hda_gen_spec *spec = codec->spec;
4585 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
4586}
4587EXPORT_SYMBOL_HDA(snd_hda_gen_check_power_status);
4588#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01004589
4590
4591/*
4592 * the generic codec support
4593 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594
Takashi Iwai352f7f92012-12-19 12:52:06 +01004595static const struct hda_codec_ops generic_patch_ops = {
4596 .build_controls = snd_hda_gen_build_controls,
4597 .build_pcms = snd_hda_gen_build_pcms,
4598 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01004599 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004600 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02004601#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01004602 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02004603#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604};
4605
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606int snd_hda_parse_generic_codec(struct hda_codec *codec)
4607{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004608 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 int err;
4610
Takashi Iwaie560d8d2005-09-09 14:21:46 +02004611 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004612 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004614 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004617 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
4618 if (err < 0)
4619 return err;
4620
4621 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004622 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623 goto error;
4624
4625 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626 return 0;
4627
Takashi Iwai352f7f92012-12-19 12:52:06 +01004628error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01004629 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 return err;
4631}
Takashi Iwaifce52a32013-01-07 12:42:48 +01004632EXPORT_SYMBOL_HDA(snd_hda_parse_generic_codec);