blob: ebb558414314b7db06229185702f4dfad70e411f [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
Takashi Iwaic9700422013-01-18 10:17:30 +01002517/* fill the label for each input at first */
2518static int fill_input_pin_labels(struct hda_codec *codec)
2519{
2520 struct hda_gen_spec *spec = codec->spec;
2521 const struct auto_pin_cfg *cfg = &spec->autocfg;
2522 int i;
2523
2524 for (i = 0; i < cfg->num_inputs; i++) {
2525 hda_nid_t pin = cfg->inputs[i].pin;
2526 const char *label;
2527 int j, idx;
2528
2529 if (!is_input_pin(codec, pin))
2530 continue;
2531
2532 label = hda_get_autocfg_input_label(codec, cfg, i);
2533 idx = 0;
2534 for (j = i; j >= 0; j--) {
2535 if (spec->input_labels[j] &&
2536 !strcmp(spec->input_labels[j], label)) {
2537 idx = spec->input_label_idxs[j] + 1;
2538 break;
2539 }
2540 }
2541
2542 spec->input_labels[i] = label;
2543 spec->input_label_idxs[i] = idx;
2544 }
2545
2546 return 0;
2547}
2548
Takashi Iwai9dba2052013-01-18 10:01:15 +01002549#define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
2550
Takashi Iwai352f7f92012-12-19 12:52:06 +01002551static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002552{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002553 struct hda_gen_spec *spec = codec->spec;
2554 const struct auto_pin_cfg *cfg = &spec->autocfg;
2555 hda_nid_t mixer = spec->mixer_nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002556 int num_adcs;
Takashi Iwaic9700422013-01-18 10:17:30 +01002557 int i, err;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002558 unsigned int val;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002559
Takashi Iwai352f7f92012-12-19 12:52:06 +01002560 num_adcs = fill_adc_nids(codec);
2561 if (num_adcs < 0)
2562 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002563
Takashi Iwaic9700422013-01-18 10:17:30 +01002564 err = fill_input_pin_labels(codec);
2565 if (err < 0)
2566 return err;
2567
Takashi Iwai352f7f92012-12-19 12:52:06 +01002568 for (i = 0; i < cfg->num_inputs; i++) {
2569 hda_nid_t pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570
Takashi Iwai352f7f92012-12-19 12:52:06 +01002571 pin = cfg->inputs[i].pin;
2572 if (!is_input_pin(codec, pin))
2573 continue;
2574
Takashi Iwai2c12c302013-01-10 09:33:29 +01002575 val = PIN_IN;
2576 if (cfg->inputs[i].type == AUTO_PIN_MIC)
2577 val |= snd_hda_get_default_vref(codec, pin);
2578 set_pin_target(codec, pin, val, false);
2579
Takashi Iwai352f7f92012-12-19 12:52:06 +01002580 if (mixer) {
2581 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01002582 err = new_analog_input(codec, i, pin,
Takashi Iwaic9700422013-01-18 10:17:30 +01002583 spec->input_labels[i],
2584 spec->input_label_idxs[i],
2585 mixer);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002586 if (err < 0)
2587 return err;
2588 }
2589 }
2590
Takashi Iwaic9700422013-01-18 10:17:30 +01002591 err = parse_capture_source(codec, pin, i, num_adcs,
2592 spec->input_labels[i], -mixer);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002593 if (err < 0)
2594 return err;
Takashi Iwai294765582013-01-17 09:52:11 +01002595
2596 if (spec->add_in_jack_modes) {
2597 err = create_in_jack_mode(codec, pin);
2598 if (err < 0)
2599 return err;
2600 }
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002601 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002602
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002603 if (mixer && spec->add_stereo_mix_input) {
Takashi Iwai9dba2052013-01-18 10:01:15 +01002604 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002605 "Stereo Mix", 0);
2606 if (err < 0)
2607 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002608 }
2609
2610 return 0;
2611}
2612
2613
2614/*
2615 * input source mux
2616 */
2617
Takashi Iwaic697b712013-01-07 17:09:26 +01002618/* get the input path specified by the given adc and imux indices */
2619static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002620{
2621 struct hda_gen_spec *spec = codec->spec;
David Henningssonb56fa1e2013-01-16 11:45:35 +01002622 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
2623 snd_BUG();
2624 return NULL;
2625 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002626 if (spec->dyn_adc_switch)
2627 adc_idx = spec->dyn_adc_idx[imux_idx];
David Henningssonb56fa1e2013-01-16 11:45:35 +01002628 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_OUTS) {
2629 snd_BUG();
2630 return NULL;
2631 }
Takashi Iwaic697b712013-01-07 17:09:26 +01002632 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002633}
2634
2635static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2636 unsigned int idx);
2637
2638static int mux_enum_info(struct snd_kcontrol *kcontrol,
2639 struct snd_ctl_elem_info *uinfo)
2640{
2641 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2642 struct hda_gen_spec *spec = codec->spec;
2643 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
2644}
2645
2646static int mux_enum_get(struct snd_kcontrol *kcontrol,
2647 struct snd_ctl_elem_value *ucontrol)
2648{
2649 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2650 struct hda_gen_spec *spec = codec->spec;
David Henningssona053d1e2013-01-16 11:45:36 +01002651 unsigned int adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002652
2653 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
2654 return 0;
2655}
2656
2657static int mux_enum_put(struct snd_kcontrol *kcontrol,
2658 struct snd_ctl_elem_value *ucontrol)
2659{
2660 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
David Henningssona053d1e2013-01-16 11:45:36 +01002661 unsigned int adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002662 return mux_select(codec, adc_idx,
2663 ucontrol->value.enumerated.item[0]);
2664}
2665
Takashi Iwai352f7f92012-12-19 12:52:06 +01002666static const struct snd_kcontrol_new cap_src_temp = {
2667 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2668 .name = "Input Source",
2669 .info = mux_enum_info,
2670 .get = mux_enum_get,
2671 .put = mux_enum_put,
2672};
2673
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002674/*
2675 * capture volume and capture switch ctls
2676 */
2677
Takashi Iwai352f7f92012-12-19 12:52:06 +01002678typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
2679 struct snd_ctl_elem_value *ucontrol);
2680
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002681/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002682static int cap_put_caller(struct snd_kcontrol *kcontrol,
2683 struct snd_ctl_elem_value *ucontrol,
2684 put_call_t func, int type)
2685{
2686 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2687 struct hda_gen_spec *spec = codec->spec;
2688 const struct hda_input_mux *imux;
2689 struct nid_path *path;
2690 int i, adc_idx, err = 0;
2691
2692 imux = &spec->input_mux;
David Henningssona053d1e2013-01-16 11:45:36 +01002693 adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002694 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002695 /* we use the cache-only update at first since multiple input paths
2696 * may shared the same amp; by updating only caches, the redundant
2697 * writes to hardware can be reduced.
2698 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002699 codec->cached_write = 1;
2700 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01002701 path = get_input_path(codec, adc_idx, i);
2702 if (!path || !path->ctls[type])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002703 continue;
2704 kcontrol->private_value = path->ctls[type];
2705 err = func(kcontrol, ucontrol);
2706 if (err < 0)
2707 goto error;
2708 }
2709 error:
2710 codec->cached_write = 0;
2711 mutex_unlock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002712 snd_hda_codec_flush_amp_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002713 if (err >= 0 && spec->cap_sync_hook)
2714 spec->cap_sync_hook(codec);
2715 return err;
2716}
2717
2718/* capture volume ctl callbacks */
2719#define cap_vol_info snd_hda_mixer_amp_volume_info
2720#define cap_vol_get snd_hda_mixer_amp_volume_get
2721#define cap_vol_tlv snd_hda_mixer_amp_tlv
2722
2723static int cap_vol_put(struct snd_kcontrol *kcontrol,
2724 struct snd_ctl_elem_value *ucontrol)
2725{
2726 return cap_put_caller(kcontrol, ucontrol,
2727 snd_hda_mixer_amp_volume_put,
2728 NID_PATH_VOL_CTL);
2729}
2730
2731static const struct snd_kcontrol_new cap_vol_temp = {
2732 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2733 .name = "Capture Volume",
2734 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
2735 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2736 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
2737 .info = cap_vol_info,
2738 .get = cap_vol_get,
2739 .put = cap_vol_put,
2740 .tlv = { .c = cap_vol_tlv },
2741};
2742
2743/* capture switch ctl callbacks */
2744#define cap_sw_info snd_ctl_boolean_stereo_info
2745#define cap_sw_get snd_hda_mixer_amp_switch_get
2746
2747static int cap_sw_put(struct snd_kcontrol *kcontrol,
2748 struct snd_ctl_elem_value *ucontrol)
2749{
Takashi Iwaiae177c32013-01-14 12:13:06 +01002750 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2751 struct hda_gen_spec *spec = codec->spec;
2752 int ret;
2753
2754 ret = cap_put_caller(kcontrol, ucontrol,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002755 snd_hda_mixer_amp_switch_put,
2756 NID_PATH_MUTE_CTL);
Takashi Iwaiae177c32013-01-14 12:13:06 +01002757 if (ret < 0)
2758 return ret;
2759
2760 if (spec->capture_switch_hook) {
2761 bool enable = (ucontrol->value.integer.value[0] ||
2762 ucontrol->value.integer.value[1]);
2763 spec->capture_switch_hook(codec, enable);
2764 }
2765
2766 return ret;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002767}
2768
2769static const struct snd_kcontrol_new cap_sw_temp = {
2770 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2771 .name = "Capture Switch",
2772 .info = cap_sw_info,
2773 .get = cap_sw_get,
2774 .put = cap_sw_put,
2775};
2776
2777static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
2778{
2779 hda_nid_t nid;
2780 int i, depth;
2781
2782 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
2783 for (depth = 0; depth < 3; depth++) {
2784 if (depth >= path->depth)
2785 return -EINVAL;
2786 i = path->depth - depth - 1;
2787 nid = path->path[i];
2788 if (!path->ctls[NID_PATH_VOL_CTL]) {
2789 if (nid_has_volume(codec, nid, HDA_OUTPUT))
2790 path->ctls[NID_PATH_VOL_CTL] =
2791 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2792 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
2793 int idx = path->idx[i];
2794 if (!depth && codec->single_adc_amp)
2795 idx = 0;
2796 path->ctls[NID_PATH_VOL_CTL] =
2797 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2798 }
2799 }
2800 if (!path->ctls[NID_PATH_MUTE_CTL]) {
2801 if (nid_has_mute(codec, nid, HDA_OUTPUT))
2802 path->ctls[NID_PATH_MUTE_CTL] =
2803 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2804 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
2805 int idx = path->idx[i];
2806 if (!depth && codec->single_adc_amp)
2807 idx = 0;
2808 path->ctls[NID_PATH_MUTE_CTL] =
2809 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2810 }
2811 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01002812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 return 0;
2814}
2815
Takashi Iwai352f7f92012-12-19 12:52:06 +01002816static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002818 struct hda_gen_spec *spec = codec->spec;
2819 struct auto_pin_cfg *cfg = &spec->autocfg;
2820 unsigned int val;
2821 int i;
2822
2823 if (!spec->inv_dmic_split)
2824 return false;
2825 for (i = 0; i < cfg->num_inputs; i++) {
2826 if (cfg->inputs[i].pin != nid)
2827 continue;
2828 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2829 return false;
2830 val = snd_hda_codec_get_pincfg(codec, nid);
2831 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
2832 }
2833 return false;
2834}
2835
2836static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
2837 int idx, bool is_switch, unsigned int ctl,
2838 bool inv_dmic)
2839{
2840 struct hda_gen_spec *spec = codec->spec;
2841 char tmpname[44];
2842 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
2843 const char *sfx = is_switch ? "Switch" : "Volume";
2844 unsigned int chs = inv_dmic ? 1 : 3;
2845 int err;
2846
2847 if (!ctl)
2848 return 0;
2849
2850 if (label)
2851 snprintf(tmpname, sizeof(tmpname),
2852 "%s Capture %s", label, sfx);
2853 else
2854 snprintf(tmpname, sizeof(tmpname),
2855 "Capture %s", sfx);
2856 err = add_control(spec, type, tmpname, idx,
2857 amp_val_replace_channels(ctl, chs));
2858 if (err < 0 || !inv_dmic)
2859 return err;
2860
2861 /* Make independent right kcontrol */
2862 if (label)
2863 snprintf(tmpname, sizeof(tmpname),
2864 "Inverted %s Capture %s", label, sfx);
2865 else
2866 snprintf(tmpname, sizeof(tmpname),
2867 "Inverted Capture %s", sfx);
2868 return add_control(spec, type, tmpname, idx,
2869 amp_val_replace_channels(ctl, 2));
2870}
2871
2872/* create single (and simple) capture volume and switch controls */
2873static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
2874 unsigned int vol_ctl, unsigned int sw_ctl,
2875 bool inv_dmic)
2876{
2877 int err;
2878 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
2879 if (err < 0)
2880 return err;
2881 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
2882 if (err < 0)
2883 return err;
2884 return 0;
2885}
2886
2887/* create bound capture volume and switch controls */
2888static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
2889 unsigned int vol_ctl, unsigned int sw_ctl)
2890{
2891 struct hda_gen_spec *spec = codec->spec;
2892 struct snd_kcontrol_new *knew;
2893
2894 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002895 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002896 if (!knew)
2897 return -ENOMEM;
2898 knew->index = idx;
2899 knew->private_value = vol_ctl;
2900 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2901 }
2902 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002903 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002904 if (!knew)
2905 return -ENOMEM;
2906 knew->index = idx;
2907 knew->private_value = sw_ctl;
2908 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2909 }
2910 return 0;
2911}
2912
2913/* return the vol ctl when used first in the imux list */
2914static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
2915{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002916 struct nid_path *path;
2917 unsigned int ctl;
2918 int i;
2919
Takashi Iwaic697b712013-01-07 17:09:26 +01002920 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002921 if (!path)
2922 return 0;
2923 ctl = path->ctls[type];
2924 if (!ctl)
2925 return 0;
2926 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01002927 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002928 if (path && path->ctls[type] == ctl)
2929 return 0;
2930 }
2931 return ctl;
2932}
2933
2934/* create individual capture volume and switch controls per input */
2935static int create_multi_cap_vol_ctl(struct hda_codec *codec)
2936{
2937 struct hda_gen_spec *spec = codec->spec;
2938 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaic9700422013-01-18 10:17:30 +01002939 int i, err, type;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002940
2941 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002942 bool inv_dmic;
Takashi Iwaic9700422013-01-18 10:17:30 +01002943 int idx;
Takashi Iwai9dba2052013-01-18 10:01:15 +01002944
Takashi Iwaic9700422013-01-18 10:17:30 +01002945 idx = imux->items[i].index;
2946 if (idx >= spec->autocfg.num_inputs)
Takashi Iwai9dba2052013-01-18 10:01:15 +01002947 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002948 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
2949
2950 for (type = 0; type < 2; type++) {
Takashi Iwaic9700422013-01-18 10:17:30 +01002951 err = add_single_cap_ctl(codec,
2952 spec->input_labels[idx],
2953 spec->input_label_idxs[idx],
2954 type,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002955 get_first_cap_ctl(codec, i, type),
2956 inv_dmic);
2957 if (err < 0)
2958 return err;
2959 }
2960 }
2961 return 0;
2962}
2963
2964static int create_capture_mixers(struct hda_codec *codec)
2965{
2966 struct hda_gen_spec *spec = codec->spec;
2967 struct hda_input_mux *imux = &spec->input_mux;
2968 int i, n, nums, err;
2969
2970 if (spec->dyn_adc_switch)
2971 nums = 1;
2972 else
2973 nums = spec->num_adc_nids;
2974
2975 if (!spec->auto_mic && imux->num_items > 1) {
2976 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01002977 const char *name;
2978 name = nums > 1 ? "Input Source" : "Capture Source";
2979 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002980 if (!knew)
2981 return -ENOMEM;
2982 knew->count = nums;
2983 }
2984
2985 for (n = 0; n < nums; n++) {
2986 bool multi = false;
David Henningsson99a55922013-01-16 15:58:44 +01002987 bool multi_cap_vol = spec->multi_cap_vol;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002988 bool inv_dmic = false;
2989 int vol, sw;
2990
2991 vol = sw = 0;
2992 for (i = 0; i < imux->num_items; i++) {
2993 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01002994 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002995 if (!path)
2996 continue;
2997 parse_capvol_in_path(codec, path);
2998 if (!vol)
2999 vol = path->ctls[NID_PATH_VOL_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003000 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003001 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003002 if (!same_amp_caps(codec, vol,
3003 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3004 multi_cap_vol = true;
3005 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003006 if (!sw)
3007 sw = path->ctls[NID_PATH_MUTE_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003008 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003009 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003010 if (!same_amp_caps(codec, sw,
3011 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3012 multi_cap_vol = true;
3013 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003014 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3015 inv_dmic = true;
3016 }
3017
3018 if (!multi)
3019 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3020 inv_dmic);
David Henningsson99a55922013-01-16 15:58:44 +01003021 else if (!multi_cap_vol)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003022 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3023 else
3024 err = create_multi_cap_vol_ctl(codec);
3025 if (err < 0)
3026 return err;
3027 }
3028
3029 return 0;
3030}
3031
3032/*
3033 * add mic boosts if needed
3034 */
3035static int parse_mic_boost(struct hda_codec *codec)
3036{
3037 struct hda_gen_spec *spec = codec->spec;
3038 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003039 int i, err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003040 hda_nid_t nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003041
3042 for (i = 0; i < cfg->num_inputs; i++) {
3043 if (cfg->inputs[i].type > AUTO_PIN_MIC)
3044 break;
3045 nid = cfg->inputs[i].pin;
3046 if (get_wcaps(codec, nid) & AC_WCAP_IN_AMP) {
Takashi Iwai5abd4882013-01-07 09:43:18 +01003047 char boost_label[44];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003048 struct nid_path *path;
3049 unsigned int val;
3050
David Henningsson02aba552013-01-16 15:58:43 +01003051 if (!nid_has_volume(codec, nid, HDA_INPUT))
3052 continue;
3053
Takashi Iwai352f7f92012-12-19 12:52:06 +01003054 snprintf(boost_label, sizeof(boost_label),
Takashi Iwaic9700422013-01-18 10:17:30 +01003055 "%s Boost Volume",
3056 spec->input_labels[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003057 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
3058 err = add_control(spec, HDA_CTL_WIDGET_VOL,
Takashi Iwaic9700422013-01-18 10:17:30 +01003059 boost_label,
3060 spec->input_label_idxs[i], val);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003061 if (err < 0)
3062 return err;
3063
3064 path = snd_hda_get_nid_path(codec, nid, 0);
3065 if (path)
3066 path->ctls[NID_PATH_BOOST_CTL] = val;
3067 }
3068 }
3069 return 0;
3070}
3071
3072/*
3073 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3074 */
3075static void parse_digital(struct hda_codec *codec)
3076{
3077 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003078 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003079 int i, nums;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003080 hda_nid_t dig_nid, pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003081
3082 /* support multiple SPDIFs; the secondary is set up as a slave */
3083 nums = 0;
3084 for (i = 0; i < spec->autocfg.dig_outs; i++) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003085 pin = spec->autocfg.dig_out_pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003086 dig_nid = look_for_dac(codec, pin, true);
3087 if (!dig_nid)
3088 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003089 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003090 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003091 continue;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003092 print_nid_path("digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01003093 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01003094 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003095 set_pin_target(codec, pin, PIN_OUT, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003096 if (!nums) {
3097 spec->multiout.dig_out_nid = dig_nid;
3098 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3099 } else {
3100 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3101 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
3102 break;
3103 spec->slave_dig_outs[nums - 1] = dig_nid;
3104 }
3105 nums++;
3106 }
3107
3108 if (spec->autocfg.dig_in_pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003109 pin = spec->autocfg.dig_in_pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003110 dig_nid = codec->start_nid;
3111 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003112 unsigned int wcaps = get_wcaps(codec, dig_nid);
3113 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3114 continue;
3115 if (!(wcaps & AC_WCAP_DIGITAL))
3116 continue;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003117 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003118 if (path) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003119 print_nid_path("digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003120 path->active = true;
3121 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003122 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003123 set_pin_target(codec, pin, PIN_IN, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003124 break;
3125 }
3126 }
3127 }
3128}
3129
3130
3131/*
3132 * input MUX handling
3133 */
3134
3135static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3136
3137/* select the given imux item; either unmute exclusively or select the route */
3138static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3139 unsigned int idx)
3140{
3141 struct hda_gen_spec *spec = codec->spec;
3142 const struct hda_input_mux *imux;
3143 struct nid_path *path;
3144
3145 imux = &spec->input_mux;
3146 if (!imux->num_items)
3147 return 0;
3148
3149 if (idx >= imux->num_items)
3150 idx = imux->num_items - 1;
3151 if (spec->cur_mux[adc_idx] == idx)
3152 return 0;
3153
Takashi Iwaic697b712013-01-07 17:09:26 +01003154 path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003155 if (!path)
3156 return 0;
3157 if (path->active)
3158 snd_hda_activate_path(codec, path, false, false);
3159
3160 spec->cur_mux[adc_idx] = idx;
3161
3162 if (spec->shared_mic_hp)
3163 update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
3164
3165 if (spec->dyn_adc_switch)
3166 dyn_adc_pcm_resetup(codec, idx);
3167
Takashi Iwaic697b712013-01-07 17:09:26 +01003168 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003169 if (!path)
3170 return 0;
3171 if (path->active)
3172 return 0;
3173 snd_hda_activate_path(codec, path, true, false);
3174 if (spec->cap_sync_hook)
3175 spec->cap_sync_hook(codec);
3176 return 1;
3177}
3178
3179
3180/*
3181 * Jack detections for HP auto-mute and mic-switch
3182 */
3183
3184/* check each pin in the given array; returns true if any of them is plugged */
3185static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
3186{
3187 int i, present = 0;
3188
3189 for (i = 0; i < num_pins; i++) {
3190 hda_nid_t nid = pins[i];
3191 if (!nid)
3192 break;
Takashi Iwai0b4df932013-01-10 09:45:13 +01003193 /* don't detect pins retasked as inputs */
3194 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
3195 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003196 present |= snd_hda_jack_detect(codec, nid);
3197 }
3198 return present;
3199}
3200
3201/* standard HP/line-out auto-mute helper */
3202static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
Takashi Iwai2c12c302013-01-10 09:33:29 +01003203 bool mute)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003204{
3205 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003206 int i;
3207
3208 for (i = 0; i < num_pins; i++) {
3209 hda_nid_t nid = pins[i];
3210 unsigned int val;
3211 if (!nid)
3212 break;
3213 /* don't reset VREF value in case it's controlling
3214 * the amp (see alc861_fixup_asus_amp_vref_0f())
3215 */
Takashi Iwai2c12c302013-01-10 09:33:29 +01003216 if (spec->keep_vref_in_automute)
3217 val = snd_hda_codec_get_pin_target(codec, nid) & ~PIN_HP;
3218 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01003219 val = 0;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003220 if (!mute)
3221 val |= snd_hda_codec_get_pin_target(codec, nid);
3222 /* here we call update_pin_ctl() so that the pinctl is changed
3223 * without changing the pinctl target value;
3224 * the original target value will be still referred at the
3225 * init / resume again
3226 */
3227 update_pin_ctl(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01003228 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003229 }
3230}
3231
3232/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003233void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003234{
3235 struct hda_gen_spec *spec = codec->spec;
3236 int on;
3237
3238 /* Control HP pins/amps depending on master_mute state;
3239 * in general, HP pins/amps control should be enabled in all cases,
3240 * but currently set only for master_mute, just to be safe
3241 */
3242 if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
3243 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003244 spec->autocfg.hp_pins, spec->master_mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003245
3246 if (!spec->automute_speaker)
3247 on = 0;
3248 else
3249 on = spec->hp_jack_present | spec->line_jack_present;
3250 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003251 spec->speaker_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003252 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003253 spec->autocfg.speaker_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003254
3255 /* toggle line-out mutes if needed, too */
3256 /* if LO is a copy of either HP or Speaker, don't need to handle it */
3257 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
3258 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
3259 return;
3260 if (!spec->automute_lo)
3261 on = 0;
3262 else
3263 on = spec->hp_jack_present;
3264 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003265 spec->line_out_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003266 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003267 spec->autocfg.line_out_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003268}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003269EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003270
3271static void call_update_outputs(struct hda_codec *codec)
3272{
3273 struct hda_gen_spec *spec = codec->spec;
3274 if (spec->automute_hook)
3275 spec->automute_hook(codec);
3276 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01003277 snd_hda_gen_update_outputs(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003278}
3279
3280/* standard HP-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003281void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003282{
3283 struct hda_gen_spec *spec = codec->spec;
3284
3285 spec->hp_jack_present =
3286 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
3287 spec->autocfg.hp_pins);
3288 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
3289 return;
3290 call_update_outputs(codec);
3291}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003292EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003293
3294/* standard line-out-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003295void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003296{
3297 struct hda_gen_spec *spec = codec->spec;
3298
3299 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3300 return;
3301 /* check LO jack only when it's different from HP */
3302 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
3303 return;
3304
3305 spec->line_jack_present =
3306 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3307 spec->autocfg.line_out_pins);
3308 if (!spec->automute_speaker || !spec->detect_lo)
3309 return;
3310 call_update_outputs(codec);
3311}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003312EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003313
3314/* standard mic auto-switch helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003315void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003316{
3317 struct hda_gen_spec *spec = codec->spec;
3318 int i;
3319
3320 if (!spec->auto_mic)
3321 return;
3322
3323 for (i = spec->am_num_entries - 1; i > 0; i--) {
Takashi Iwai0b4df932013-01-10 09:45:13 +01003324 hda_nid_t pin = spec->am_entry[i].pin;
3325 /* don't detect pins retasked as outputs */
3326 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
3327 continue;
3328 if (snd_hda_jack_detect(codec, pin)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003329 mux_select(codec, 0, spec->am_entry[i].idx);
3330 return;
3331 }
3332 }
3333 mux_select(codec, 0, spec->am_entry[0].idx);
3334}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003335EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003336
Takashi Iwaia5cc2502013-01-16 18:08:55 +01003337/* update jack retasking */
3338static void update_automute_all(struct hda_codec *codec)
3339{
3340 struct hda_gen_spec *spec = codec->spec;
3341
3342 if (spec->hp_automute_hook)
3343 spec->hp_automute_hook(codec, NULL);
3344 else
3345 snd_hda_gen_hp_automute(codec, NULL);
3346 if (spec->line_automute_hook)
3347 spec->line_automute_hook(codec, NULL);
3348 else
3349 snd_hda_gen_line_automute(codec, NULL);
3350 if (spec->mic_autoswitch_hook)
3351 spec->mic_autoswitch_hook(codec, NULL);
3352 else
3353 snd_hda_gen_mic_autoswitch(codec, NULL);
3354}
3355
Takashi Iwai352f7f92012-12-19 12:52:06 +01003356/*
3357 * Auto-Mute mode mixer enum support
3358 */
3359static int automute_mode_info(struct snd_kcontrol *kcontrol,
3360 struct snd_ctl_elem_info *uinfo)
3361{
3362 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3363 struct hda_gen_spec *spec = codec->spec;
3364 static const char * const texts3[] = {
3365 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02003366 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367
Takashi Iwai352f7f92012-12-19 12:52:06 +01003368 if (spec->automute_speaker_possible && spec->automute_lo_possible)
3369 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
3370 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
3371}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372
Takashi Iwai352f7f92012-12-19 12:52:06 +01003373static int automute_mode_get(struct snd_kcontrol *kcontrol,
3374 struct snd_ctl_elem_value *ucontrol)
3375{
3376 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3377 struct hda_gen_spec *spec = codec->spec;
3378 unsigned int val = 0;
3379 if (spec->automute_speaker)
3380 val++;
3381 if (spec->automute_lo)
3382 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003383
Takashi Iwai352f7f92012-12-19 12:52:06 +01003384 ucontrol->value.enumerated.item[0] = val;
3385 return 0;
3386}
3387
3388static int automute_mode_put(struct snd_kcontrol *kcontrol,
3389 struct snd_ctl_elem_value *ucontrol)
3390{
3391 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3392 struct hda_gen_spec *spec = codec->spec;
3393
3394 switch (ucontrol->value.enumerated.item[0]) {
3395 case 0:
3396 if (!spec->automute_speaker && !spec->automute_lo)
3397 return 0;
3398 spec->automute_speaker = 0;
3399 spec->automute_lo = 0;
3400 break;
3401 case 1:
3402 if (spec->automute_speaker_possible) {
3403 if (!spec->automute_lo && spec->automute_speaker)
3404 return 0;
3405 spec->automute_speaker = 1;
3406 spec->automute_lo = 0;
3407 } else if (spec->automute_lo_possible) {
3408 if (spec->automute_lo)
3409 return 0;
3410 spec->automute_lo = 1;
3411 } else
3412 return -EINVAL;
3413 break;
3414 case 2:
3415 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
3416 return -EINVAL;
3417 if (spec->automute_speaker && spec->automute_lo)
3418 return 0;
3419 spec->automute_speaker = 1;
3420 spec->automute_lo = 1;
3421 break;
3422 default:
3423 return -EINVAL;
3424 }
3425 call_update_outputs(codec);
3426 return 1;
3427}
3428
3429static const struct snd_kcontrol_new automute_mode_enum = {
3430 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3431 .name = "Auto-Mute Mode",
3432 .info = automute_mode_info,
3433 .get = automute_mode_get,
3434 .put = automute_mode_put,
3435};
3436
3437static int add_automute_mode_enum(struct hda_codec *codec)
3438{
3439 struct hda_gen_spec *spec = codec->spec;
3440
Takashi Iwai12c93df2012-12-19 14:38:33 +01003441 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01003442 return -ENOMEM;
3443 return 0;
3444}
3445
3446/*
3447 * Check the availability of HP/line-out auto-mute;
3448 * Set up appropriately if really supported
3449 */
3450static int check_auto_mute_availability(struct hda_codec *codec)
3451{
3452 struct hda_gen_spec *spec = codec->spec;
3453 struct auto_pin_cfg *cfg = &spec->autocfg;
3454 int present = 0;
3455 int i, err;
3456
Takashi Iwaif72706b2013-01-16 18:20:07 +01003457 if (spec->suppress_auto_mute)
3458 return 0;
3459
Takashi Iwai352f7f92012-12-19 12:52:06 +01003460 if (cfg->hp_pins[0])
3461 present++;
3462 if (cfg->line_out_pins[0])
3463 present++;
3464 if (cfg->speaker_pins[0])
3465 present++;
3466 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02003467 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003468
3469 if (!cfg->speaker_pins[0] &&
3470 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
3471 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3472 sizeof(cfg->speaker_pins));
3473 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475
Takashi Iwai352f7f92012-12-19 12:52:06 +01003476 if (!cfg->hp_pins[0] &&
3477 cfg->line_out_type == AUTO_PIN_HP_OUT) {
3478 memcpy(cfg->hp_pins, cfg->line_out_pins,
3479 sizeof(cfg->hp_pins));
3480 cfg->hp_outs = cfg->line_outs;
3481 }
3482
3483 for (i = 0; i < cfg->hp_outs; i++) {
3484 hda_nid_t nid = cfg->hp_pins[i];
3485 if (!is_jack_detectable(codec, nid))
3486 continue;
3487 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
3488 nid);
3489 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003490 spec->hp_automute_hook ?
3491 spec->hp_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003492 snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003493 spec->detect_hp = 1;
3494 }
3495
3496 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
3497 if (cfg->speaker_outs)
3498 for (i = 0; i < cfg->line_outs; i++) {
3499 hda_nid_t nid = cfg->line_out_pins[i];
3500 if (!is_jack_detectable(codec, nid))
3501 continue;
3502 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
3503 snd_hda_jack_detect_enable_callback(codec, nid,
3504 HDA_GEN_FRONT_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003505 spec->line_automute_hook ?
3506 spec->line_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003507 snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003508 spec->detect_lo = 1;
3509 }
3510 spec->automute_lo_possible = spec->detect_hp;
3511 }
3512
3513 spec->automute_speaker_possible = cfg->speaker_outs &&
3514 (spec->detect_hp || spec->detect_lo);
3515
3516 spec->automute_lo = spec->automute_lo_possible;
3517 spec->automute_speaker = spec->automute_speaker_possible;
3518
3519 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
3520 /* create a control for automute mode */
3521 err = add_automute_mode_enum(codec);
3522 if (err < 0)
3523 return err;
3524 }
3525 return 0;
3526}
3527
Takashi Iwai352f7f92012-12-19 12:52:06 +01003528/* check whether all auto-mic pins are valid; setup indices if OK */
3529static bool auto_mic_check_imux(struct hda_codec *codec)
3530{
3531 struct hda_gen_spec *spec = codec->spec;
3532 const struct hda_input_mux *imux;
3533 int i;
3534
3535 imux = &spec->input_mux;
3536 for (i = 0; i < spec->am_num_entries; i++) {
3537 spec->am_entry[i].idx =
3538 find_idx_in_nid_list(spec->am_entry[i].pin,
3539 spec->imux_pins, imux->num_items);
3540 if (spec->am_entry[i].idx < 0)
3541 return false; /* no corresponding imux */
3542 }
3543
3544 /* we don't need the jack detection for the first pin */
3545 for (i = 1; i < spec->am_num_entries; i++)
3546 snd_hda_jack_detect_enable_callback(codec,
3547 spec->am_entry[i].pin,
3548 HDA_GEN_MIC_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003549 spec->mic_autoswitch_hook ?
3550 spec->mic_autoswitch_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003551 snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003552 return true;
3553}
3554
3555static int compare_attr(const void *ap, const void *bp)
3556{
3557 const struct automic_entry *a = ap;
3558 const struct automic_entry *b = bp;
3559 return (int)(a->attr - b->attr);
3560}
3561
3562/*
3563 * Check the availability of auto-mic switch;
3564 * Set up if really supported
3565 */
3566static int check_auto_mic_availability(struct hda_codec *codec)
3567{
3568 struct hda_gen_spec *spec = codec->spec;
3569 struct auto_pin_cfg *cfg = &spec->autocfg;
3570 unsigned int types;
3571 int i, num_pins;
3572
Takashi Iwaid12daf62013-01-07 16:32:11 +01003573 if (spec->suppress_auto_mic)
3574 return 0;
3575
Takashi Iwai352f7f92012-12-19 12:52:06 +01003576 types = 0;
3577 num_pins = 0;
3578 for (i = 0; i < cfg->num_inputs; i++) {
3579 hda_nid_t nid = cfg->inputs[i].pin;
3580 unsigned int attr;
3581 attr = snd_hda_codec_get_pincfg(codec, nid);
3582 attr = snd_hda_get_input_pin_attr(attr);
3583 if (types & (1 << attr))
3584 return 0; /* already occupied */
3585 switch (attr) {
3586 case INPUT_PIN_ATTR_INT:
3587 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3588 return 0; /* invalid type */
3589 break;
3590 case INPUT_PIN_ATTR_UNUSED:
3591 return 0; /* invalid entry */
3592 default:
3593 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
3594 return 0; /* invalid type */
3595 if (!spec->line_in_auto_switch &&
3596 cfg->inputs[i].type != AUTO_PIN_MIC)
3597 return 0; /* only mic is allowed */
3598 if (!is_jack_detectable(codec, nid))
3599 return 0; /* no unsol support */
3600 break;
3601 }
3602 if (num_pins >= MAX_AUTO_MIC_PINS)
3603 return 0;
3604 types |= (1 << attr);
3605 spec->am_entry[num_pins].pin = nid;
3606 spec->am_entry[num_pins].attr = attr;
3607 num_pins++;
3608 }
3609
3610 if (num_pins < 2)
3611 return 0;
3612
3613 spec->am_num_entries = num_pins;
3614 /* sort the am_entry in the order of attr so that the pin with a
3615 * higher attr will be selected when the jack is plugged.
3616 */
3617 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
3618 compare_attr, NULL);
3619
3620 if (!auto_mic_check_imux(codec))
3621 return 0;
3622
3623 spec->auto_mic = 1;
3624 spec->num_adc_nids = 1;
3625 spec->cur_mux[0] = spec->am_entry[0].idx;
3626 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
3627 spec->am_entry[0].pin,
3628 spec->am_entry[1].pin,
3629 spec->am_entry[2].pin);
3630
3631 return 0;
3632}
3633
3634
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003635/*
3636 * Parse the given BIOS configuration and set up the hda_gen_spec
3637 *
3638 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003639 * or a negative error code
3640 */
3641int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003642 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003643{
3644 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003645 int err;
3646
Takashi Iwai1c70a582013-01-11 17:48:22 +01003647 parse_user_hints(codec);
3648
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003649 if (cfg != &spec->autocfg) {
3650 spec->autocfg = *cfg;
3651 cfg = &spec->autocfg;
3652 }
3653
David Henningsson6fc4cb92013-01-16 15:58:45 +01003654 fill_all_dac_nids(codec);
3655
Takashi Iwai352f7f92012-12-19 12:52:06 +01003656 if (!cfg->line_outs) {
3657 if (cfg->dig_outs || cfg->dig_in_pin) {
3658 spec->multiout.max_channels = 2;
3659 spec->no_analog = 1;
3660 goto dig_only;
3661 }
3662 return 0; /* can't find valid BIOS pin config */
3663 }
3664
3665 if (!spec->no_primary_hp &&
3666 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
3667 cfg->line_outs <= cfg->hp_outs) {
3668 /* use HP as primary out */
3669 cfg->speaker_outs = cfg->line_outs;
3670 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3671 sizeof(cfg->speaker_pins));
3672 cfg->line_outs = cfg->hp_outs;
3673 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
3674 cfg->hp_outs = 0;
3675 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3676 cfg->line_out_type = AUTO_PIN_HP_OUT;
3677 }
3678
3679 err = parse_output_paths(codec);
3680 if (err < 0)
3681 return err;
3682 err = create_multi_channel_mode(codec);
3683 if (err < 0)
3684 return err;
3685 err = create_multi_out_ctls(codec, cfg);
3686 if (err < 0)
3687 return err;
3688 err = create_hp_out_ctls(codec);
3689 if (err < 0)
3690 return err;
3691 err = create_speaker_out_ctls(codec);
3692 if (err < 0)
3693 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003694 err = create_indep_hp_ctls(codec);
3695 if (err < 0)
3696 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01003697 err = create_loopback_mixing_ctl(codec);
3698 if (err < 0)
3699 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003700 err = create_shared_input(codec);
3701 if (err < 0)
3702 return err;
3703 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003704 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02003705 return err;
3706
Takashi Iwaia07a9492013-01-07 16:44:06 +01003707 spec->const_channel_count = spec->ext_channel_count;
3708 /* check the multiple speaker and headphone pins */
3709 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
3710 spec->const_channel_count = max(spec->const_channel_count,
3711 cfg->speaker_outs * 2);
3712 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
3713 spec->const_channel_count = max(spec->const_channel_count,
3714 cfg->hp_outs * 2);
3715 spec->multiout.max_channels = max(spec->ext_channel_count,
3716 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003717
3718 err = check_auto_mute_availability(codec);
3719 if (err < 0)
3720 return err;
3721
3722 err = check_dyn_adc_switch(codec);
3723 if (err < 0)
3724 return err;
3725
3726 if (!spec->shared_mic_hp) {
3727 err = check_auto_mic_availability(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003728 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 }
Takashi Iwai071c73a2006-08-23 18:34:06 +02003731
Takashi Iwai352f7f92012-12-19 12:52:06 +01003732 err = create_capture_mixers(codec);
3733 if (err < 0)
3734 return err;
3735
3736 err = parse_mic_boost(codec);
3737 if (err < 0)
3738 return err;
3739
Takashi Iwai978e77e2013-01-10 16:57:58 +01003740 if (spec->add_out_jack_modes) {
3741 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
3742 err = create_out_jack_modes(codec, cfg->line_outs,
3743 cfg->line_out_pins);
3744 if (err < 0)
3745 return err;
3746 }
3747 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
3748 err = create_out_jack_modes(codec, cfg->hp_outs,
3749 cfg->hp_pins);
3750 if (err < 0)
3751 return err;
3752 }
3753 }
3754
Takashi Iwai352f7f92012-12-19 12:52:06 +01003755 dig_only:
3756 parse_digital(codec);
3757
3758 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003760EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761
3762
3763/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003764 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003766
3767/* slave controls for virtual master */
3768static const char * const slave_pfxs[] = {
3769 "Front", "Surround", "Center", "LFE", "Side",
3770 "Headphone", "Speaker", "Mono", "Line Out",
3771 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01003772 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
3773 "Headphone Front", "Headphone Surround", "Headphone CLFE",
3774 "Headphone Side",
Takashi Iwai352f7f92012-12-19 12:52:06 +01003775 NULL,
3776};
3777
3778int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003780 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782
Takashi Iwai36502d02012-12-19 15:15:10 +01003783 if (spec->kctls.used) {
3784 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
3785 if (err < 0)
3786 return err;
3787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788
Takashi Iwai352f7f92012-12-19 12:52:06 +01003789 if (spec->multiout.dig_out_nid) {
3790 err = snd_hda_create_dig_out_ctls(codec,
3791 spec->multiout.dig_out_nid,
3792 spec->multiout.dig_out_nid,
3793 spec->pcm_rec[1].pcm_type);
3794 if (err < 0)
3795 return err;
3796 if (!spec->no_analog) {
3797 err = snd_hda_create_spdif_share_sw(codec,
3798 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 if (err < 0)
3800 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003801 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 }
3803 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003804 if (spec->dig_in_nid) {
3805 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
3806 if (err < 0)
3807 return err;
3808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809
Takashi Iwai352f7f92012-12-19 12:52:06 +01003810 /* if we have no master control, let's create it */
3811 if (!spec->no_analog &&
3812 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003813 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01003814 spec->vmaster_tlv, slave_pfxs,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003815 "Playback Volume");
3816 if (err < 0)
3817 return err;
3818 }
3819 if (!spec->no_analog &&
3820 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
3821 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
3822 NULL, slave_pfxs,
3823 "Playback Switch",
3824 true, &spec->vmaster_mute.sw_kctl);
3825 if (err < 0)
3826 return err;
3827 if (spec->vmaster_mute.hook)
Takashi Iwaifd25a972012-12-20 14:57:18 +01003828 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
3829 spec->vmaster_mute_enum);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831
Takashi Iwai352f7f92012-12-19 12:52:06 +01003832 free_kctls(spec); /* no longer needed */
3833
3834 if (spec->shared_mic_hp) {
3835 int err;
3836 int nid = spec->autocfg.inputs[1].pin;
3837 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
3838 if (err < 0)
3839 return err;
3840 err = snd_hda_jack_detect_enable(codec, nid, 0);
3841 if (err < 0)
3842 return err;
3843 }
3844
3845 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
3846 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847 return err;
3848
3849 return 0;
3850}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003851EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
3852
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853
3854/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003855 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003858static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
3859 struct hda_codec *codec,
3860 struct snd_pcm_substream *substream,
3861 int action)
3862{
3863 struct hda_gen_spec *spec = codec->spec;
3864 if (spec->pcm_playback_hook)
3865 spec->pcm_playback_hook(hinfo, codec, substream, action);
3866}
3867
Takashi Iwaiac2e8732013-01-17 15:57:10 +01003868static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
3869 struct hda_codec *codec,
3870 struct snd_pcm_substream *substream,
3871 int action)
3872{
3873 struct hda_gen_spec *spec = codec->spec;
3874 if (spec->pcm_capture_hook)
3875 spec->pcm_capture_hook(hinfo, codec, substream, action);
3876}
3877
Takashi Iwai352f7f92012-12-19 12:52:06 +01003878/*
3879 * Analog playback callbacks
3880 */
3881static int playback_pcm_open(struct hda_pcm_stream *hinfo,
3882 struct hda_codec *codec,
3883 struct snd_pcm_substream *substream)
3884{
3885 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003886 int err;
3887
3888 mutex_lock(&spec->pcm_mutex);
3889 err = snd_hda_multi_out_analog_open(codec,
3890 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003891 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003892 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003893 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003894 call_pcm_playback_hook(hinfo, codec, substream,
3895 HDA_GEN_PCM_ACT_OPEN);
3896 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003897 mutex_unlock(&spec->pcm_mutex);
3898 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003899}
3900
3901static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01003902 struct hda_codec *codec,
3903 unsigned int stream_tag,
3904 unsigned int format,
3905 struct snd_pcm_substream *substream)
3906{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003907 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003908 int err;
3909
3910 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
3911 stream_tag, format, substream);
3912 if (!err)
3913 call_pcm_playback_hook(hinfo, codec, substream,
3914 HDA_GEN_PCM_ACT_PREPARE);
3915 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003916}
Takashi Iwai97ec5582006-03-21 11:29:07 +01003917
Takashi Iwai352f7f92012-12-19 12:52:06 +01003918static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3919 struct hda_codec *codec,
3920 struct snd_pcm_substream *substream)
3921{
3922 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003923 int err;
3924
3925 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
3926 if (!err)
3927 call_pcm_playback_hook(hinfo, codec, substream,
3928 HDA_GEN_PCM_ACT_CLEANUP);
3929 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003930}
3931
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003932static int playback_pcm_close(struct hda_pcm_stream *hinfo,
3933 struct hda_codec *codec,
3934 struct snd_pcm_substream *substream)
3935{
3936 struct hda_gen_spec *spec = codec->spec;
3937 mutex_lock(&spec->pcm_mutex);
3938 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003939 call_pcm_playback_hook(hinfo, codec, substream,
3940 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003941 mutex_unlock(&spec->pcm_mutex);
3942 return 0;
3943}
3944
Takashi Iwaiac2e8732013-01-17 15:57:10 +01003945static int capture_pcm_open(struct hda_pcm_stream *hinfo,
3946 struct hda_codec *codec,
3947 struct snd_pcm_substream *substream)
3948{
3949 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
3950 return 0;
3951}
3952
3953static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3954 struct hda_codec *codec,
3955 unsigned int stream_tag,
3956 unsigned int format,
3957 struct snd_pcm_substream *substream)
3958{
3959 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3960 call_pcm_capture_hook(hinfo, codec, substream,
3961 HDA_GEN_PCM_ACT_PREPARE);
3962 return 0;
3963}
3964
3965static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3966 struct hda_codec *codec,
3967 struct snd_pcm_substream *substream)
3968{
3969 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
3970 call_pcm_capture_hook(hinfo, codec, substream,
3971 HDA_GEN_PCM_ACT_CLEANUP);
3972 return 0;
3973}
3974
3975static int capture_pcm_close(struct hda_pcm_stream *hinfo,
3976 struct hda_codec *codec,
3977 struct snd_pcm_substream *substream)
3978{
3979 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
3980 return 0;
3981}
3982
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003983static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
3984 struct hda_codec *codec,
3985 struct snd_pcm_substream *substream)
3986{
3987 struct hda_gen_spec *spec = codec->spec;
3988 int err = 0;
3989
3990 mutex_lock(&spec->pcm_mutex);
3991 if (!spec->indep_hp_enabled)
3992 err = -EBUSY;
3993 else
3994 spec->active_streams |= 1 << STREAM_INDEP_HP;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003995 call_pcm_playback_hook(hinfo, codec, substream,
3996 HDA_GEN_PCM_ACT_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003997 mutex_unlock(&spec->pcm_mutex);
3998 return err;
3999}
4000
4001static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
4002 struct hda_codec *codec,
4003 struct snd_pcm_substream *substream)
4004{
4005 struct hda_gen_spec *spec = codec->spec;
4006 mutex_lock(&spec->pcm_mutex);
4007 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004008 call_pcm_playback_hook(hinfo, codec, substream,
4009 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004010 mutex_unlock(&spec->pcm_mutex);
4011 return 0;
4012}
4013
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004014static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4015 struct hda_codec *codec,
4016 unsigned int stream_tag,
4017 unsigned int format,
4018 struct snd_pcm_substream *substream)
4019{
4020 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4021 call_pcm_playback_hook(hinfo, codec, substream,
4022 HDA_GEN_PCM_ACT_PREPARE);
4023 return 0;
4024}
4025
4026static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4027 struct hda_codec *codec,
4028 struct snd_pcm_substream *substream)
4029{
4030 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4031 call_pcm_playback_hook(hinfo, codec, substream,
4032 HDA_GEN_PCM_ACT_CLEANUP);
4033 return 0;
4034}
4035
Takashi Iwai352f7f92012-12-19 12:52:06 +01004036/*
4037 * Digital out
4038 */
4039static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
4040 struct hda_codec *codec,
4041 struct snd_pcm_substream *substream)
4042{
4043 struct hda_gen_spec *spec = codec->spec;
4044 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
4045}
4046
4047static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4048 struct hda_codec *codec,
4049 unsigned int stream_tag,
4050 unsigned int format,
4051 struct snd_pcm_substream *substream)
4052{
4053 struct hda_gen_spec *spec = codec->spec;
4054 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
4055 stream_tag, format, substream);
4056}
4057
4058static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4059 struct hda_codec *codec,
4060 struct snd_pcm_substream *substream)
4061{
4062 struct hda_gen_spec *spec = codec->spec;
4063 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
4064}
4065
4066static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
4067 struct hda_codec *codec,
4068 struct snd_pcm_substream *substream)
4069{
4070 struct hda_gen_spec *spec = codec->spec;
4071 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
4072}
4073
4074/*
4075 * Analog capture
4076 */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004077#define alt_capture_pcm_open capture_pcm_open
4078#define alt_capture_pcm_close capture_pcm_close
4079
Takashi Iwai352f7f92012-12-19 12:52:06 +01004080static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4081 struct hda_codec *codec,
4082 unsigned int stream_tag,
4083 unsigned int format,
4084 struct snd_pcm_substream *substream)
4085{
4086 struct hda_gen_spec *spec = codec->spec;
4087
4088 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01004089 stream_tag, 0, format);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004090 call_pcm_capture_hook(hinfo, codec, substream,
4091 HDA_GEN_PCM_ACT_PREPARE);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004092 return 0;
4093}
4094
Takashi Iwai352f7f92012-12-19 12:52:06 +01004095static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4096 struct hda_codec *codec,
4097 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01004098{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004099 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01004100
Takashi Iwai352f7f92012-12-19 12:52:06 +01004101 snd_hda_codec_cleanup_stream(codec,
4102 spec->adc_nids[substream->number + 1]);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004103 call_pcm_capture_hook(hinfo, codec, substream,
4104 HDA_GEN_PCM_ACT_CLEANUP);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004105 return 0;
4106}
4107
Takashi Iwai352f7f92012-12-19 12:52:06 +01004108/*
4109 */
4110static const struct hda_pcm_stream pcm_analog_playback = {
4111 .substreams = 1,
4112 .channels_min = 2,
4113 .channels_max = 8,
4114 /* NID is set in build_pcms */
4115 .ops = {
4116 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004117 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004118 .prepare = playback_pcm_prepare,
4119 .cleanup = playback_pcm_cleanup
4120 },
4121};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122
Takashi Iwai352f7f92012-12-19 12:52:06 +01004123static const struct hda_pcm_stream pcm_analog_capture = {
4124 .substreams = 1,
4125 .channels_min = 2,
4126 .channels_max = 2,
4127 /* NID is set in build_pcms */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004128 .ops = {
4129 .open = capture_pcm_open,
4130 .close = capture_pcm_close,
4131 .prepare = capture_pcm_prepare,
4132 .cleanup = capture_pcm_cleanup
4133 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004134};
4135
4136static const struct hda_pcm_stream pcm_analog_alt_playback = {
4137 .substreams = 1,
4138 .channels_min = 2,
4139 .channels_max = 2,
4140 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004141 .ops = {
4142 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004143 .close = alt_playback_pcm_close,
4144 .prepare = alt_playback_pcm_prepare,
4145 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004146 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004147};
4148
4149static const struct hda_pcm_stream pcm_analog_alt_capture = {
4150 .substreams = 2, /* can be overridden */
4151 .channels_min = 2,
4152 .channels_max = 2,
4153 /* NID is set in build_pcms */
4154 .ops = {
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004155 .open = alt_capture_pcm_open,
4156 .close = alt_capture_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004157 .prepare = alt_capture_pcm_prepare,
4158 .cleanup = alt_capture_pcm_cleanup
4159 },
4160};
4161
4162static const struct hda_pcm_stream pcm_digital_playback = {
4163 .substreams = 1,
4164 .channels_min = 2,
4165 .channels_max = 2,
4166 /* NID is set in build_pcms */
4167 .ops = {
4168 .open = dig_playback_pcm_open,
4169 .close = dig_playback_pcm_close,
4170 .prepare = dig_playback_pcm_prepare,
4171 .cleanup = dig_playback_pcm_cleanup
4172 },
4173};
4174
4175static const struct hda_pcm_stream pcm_digital_capture = {
4176 .substreams = 1,
4177 .channels_min = 2,
4178 .channels_max = 2,
4179 /* NID is set in build_pcms */
4180};
4181
4182/* Used by build_pcms to flag that a PCM has no playback stream */
4183static const struct hda_pcm_stream pcm_null_stream = {
4184 .substreams = 0,
4185 .channels_min = 0,
4186 .channels_max = 0,
4187};
4188
4189/*
4190 * dynamic changing ADC PCM streams
4191 */
4192static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
4193{
4194 struct hda_gen_spec *spec = codec->spec;
4195 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
4196
4197 if (spec->cur_adc && spec->cur_adc != new_adc) {
4198 /* stream is running, let's swap the current ADC */
4199 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
4200 spec->cur_adc = new_adc;
4201 snd_hda_codec_setup_stream(codec, new_adc,
4202 spec->cur_adc_stream_tag, 0,
4203 spec->cur_adc_format);
4204 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004206 return false;
4207}
4208
4209/* analog capture with dynamic dual-adc changes */
4210static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4211 struct hda_codec *codec,
4212 unsigned int stream_tag,
4213 unsigned int format,
4214 struct snd_pcm_substream *substream)
4215{
4216 struct hda_gen_spec *spec = codec->spec;
4217 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
4218 spec->cur_adc_stream_tag = stream_tag;
4219 spec->cur_adc_format = format;
4220 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
4221 return 0;
4222}
4223
4224static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4225 struct hda_codec *codec,
4226 struct snd_pcm_substream *substream)
4227{
4228 struct hda_gen_spec *spec = codec->spec;
4229 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
4230 spec->cur_adc = 0;
4231 return 0;
4232}
4233
4234static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
4235 .substreams = 1,
4236 .channels_min = 2,
4237 .channels_max = 2,
4238 .nid = 0, /* fill later */
4239 .ops = {
4240 .prepare = dyn_adc_capture_pcm_prepare,
4241 .cleanup = dyn_adc_capture_pcm_cleanup
4242 },
4243};
4244
Takashi Iwaif873e532012-12-20 16:58:39 +01004245static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
4246 const char *chip_name)
4247{
4248 char *p;
4249
4250 if (*str)
4251 return;
4252 strlcpy(str, chip_name, len);
4253
4254 /* drop non-alnum chars after a space */
4255 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
4256 if (!isalnum(p[1])) {
4257 *p = 0;
4258 break;
4259 }
4260 }
4261 strlcat(str, sfx, len);
4262}
4263
Takashi Iwai352f7f92012-12-19 12:52:06 +01004264/* build PCM streams based on the parsed results */
4265int snd_hda_gen_build_pcms(struct hda_codec *codec)
4266{
4267 struct hda_gen_spec *spec = codec->spec;
4268 struct hda_pcm *info = spec->pcm_rec;
4269 const struct hda_pcm_stream *p;
4270 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271
4272 codec->num_pcms = 1;
4273 codec->pcm_info = info;
4274
Takashi Iwai352f7f92012-12-19 12:52:06 +01004275 if (spec->no_analog)
4276 goto skip_analog;
4277
Takashi Iwaif873e532012-12-20 16:58:39 +01004278 fill_pcm_stream_name(spec->stream_name_analog,
4279 sizeof(spec->stream_name_analog),
4280 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004281 info->name = spec->stream_name_analog;
4282
4283 if (spec->multiout.num_dacs > 0) {
4284 p = spec->stream_analog_playback;
4285 if (!p)
4286 p = &pcm_analog_playback;
4287 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4288 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
4289 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
4290 spec->multiout.max_channels;
4291 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
4292 spec->autocfg.line_outs == 2)
4293 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
4294 snd_pcm_2_1_chmaps;
4295 }
4296 if (spec->num_adc_nids) {
4297 p = spec->stream_analog_capture;
4298 if (!p) {
4299 if (spec->dyn_adc_switch)
4300 p = &dyn_adc_pcm_analog_capture;
4301 else
4302 p = &pcm_analog_capture;
4303 }
4304 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4305 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
4306 }
4307
Takashi Iwai352f7f92012-12-19 12:52:06 +01004308 skip_analog:
4309 /* SPDIF for stream index #1 */
4310 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01004311 fill_pcm_stream_name(spec->stream_name_digital,
4312 sizeof(spec->stream_name_digital),
4313 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004314 codec->num_pcms = 2;
4315 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
4316 info = spec->pcm_rec + 1;
4317 info->name = spec->stream_name_digital;
4318 if (spec->dig_out_type)
4319 info->pcm_type = spec->dig_out_type;
4320 else
4321 info->pcm_type = HDA_PCM_TYPE_SPDIF;
4322 if (spec->multiout.dig_out_nid) {
4323 p = spec->stream_digital_playback;
4324 if (!p)
4325 p = &pcm_digital_playback;
4326 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4327 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
4328 }
4329 if (spec->dig_in_nid) {
4330 p = spec->stream_digital_capture;
4331 if (!p)
4332 p = &pcm_digital_capture;
4333 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4334 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
4335 }
4336 }
4337
4338 if (spec->no_analog)
4339 return 0;
4340
4341 /* If the use of more than one ADC is requested for the current
4342 * model, configure a second analog capture-only PCM.
4343 */
4344 have_multi_adcs = (spec->num_adc_nids > 1) &&
4345 !spec->dyn_adc_switch && !spec->auto_mic;
4346 /* Additional Analaog capture for index #2 */
4347 if (spec->alt_dac_nid || have_multi_adcs) {
4348 codec->num_pcms = 3;
4349 info = spec->pcm_rec + 2;
4350 info->name = spec->stream_name_analog;
4351 if (spec->alt_dac_nid) {
4352 p = spec->stream_analog_alt_playback;
4353 if (!p)
4354 p = &pcm_analog_alt_playback;
4355 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4356 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
4357 spec->alt_dac_nid;
4358 } else {
4359 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
4360 pcm_null_stream;
4361 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
4362 }
4363 if (have_multi_adcs) {
4364 p = spec->stream_analog_alt_capture;
4365 if (!p)
4366 p = &pcm_analog_alt_capture;
4367 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4368 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
4369 spec->adc_nids[1];
4370 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
4371 spec->num_adc_nids - 1;
4372 } else {
4373 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
4374 pcm_null_stream;
4375 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
4376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 }
4378
4379 return 0;
4380}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004381EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
4382
4383
4384/*
4385 * Standard auto-parser initializations
4386 */
4387
Takashi Iwaid4156932013-01-07 10:08:02 +01004388/* configure the given path as a proper output */
Takashi Iwai2c12c302013-01-10 09:33:29 +01004389static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004390{
4391 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01004392 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004393
Takashi Iwai196c17662013-01-04 15:01:40 +01004394 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01004395 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004396 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01004397 pin = path->path[path->depth - 1];
Takashi Iwai2c12c302013-01-10 09:33:29 +01004398 restore_pin_ctl(codec, pin);
Takashi Iwaie1284af2013-01-03 16:33:02 +01004399 snd_hda_activate_path(codec, path, path->active, true);
4400 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004401}
4402
4403/* initialize primary output paths */
4404static void init_multi_out(struct hda_codec *codec)
4405{
4406 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004407 int i;
4408
Takashi Iwaid4156932013-01-07 10:08:02 +01004409 for (i = 0; i < spec->autocfg.line_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004410 set_output_and_unmute(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004411}
4412
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004413
Takashi Iwai2c12c302013-01-10 09:33:29 +01004414static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004415{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004416 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004417
Takashi Iwaid4156932013-01-07 10:08:02 +01004418 for (i = 0; i < num_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004419 set_output_and_unmute(codec, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004420}
4421
4422/* initialize hp and speaker paths */
4423static void init_extra_out(struct hda_codec *codec)
4424{
4425 struct hda_gen_spec *spec = codec->spec;
4426
4427 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004428 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004429 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
4430 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai2c12c302013-01-10 09:33:29 +01004431 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004432}
4433
4434/* initialize multi-io paths */
4435static void init_multi_io(struct hda_codec *codec)
4436{
4437 struct hda_gen_spec *spec = codec->spec;
4438 int i;
4439
4440 for (i = 0; i < spec->multi_ios; i++) {
4441 hda_nid_t pin = spec->multi_io[i].pin;
4442 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01004443 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004444 if (!path)
4445 continue;
4446 if (!spec->multi_io[i].ctl_in)
4447 spec->multi_io[i].ctl_in =
Takashi Iwai2c12c302013-01-10 09:33:29 +01004448 snd_hda_codec_get_pin_target(codec, pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004449 snd_hda_activate_path(codec, path, path->active, true);
4450 }
4451}
4452
Takashi Iwai352f7f92012-12-19 12:52:06 +01004453/* set up input pins and loopback paths */
4454static void init_analog_input(struct hda_codec *codec)
4455{
4456 struct hda_gen_spec *spec = codec->spec;
4457 struct auto_pin_cfg *cfg = &spec->autocfg;
4458 int i;
4459
4460 for (i = 0; i < cfg->num_inputs; i++) {
4461 hda_nid_t nid = cfg->inputs[i].pin;
4462 if (is_input_pin(codec, nid))
Takashi Iwai2c12c302013-01-10 09:33:29 +01004463 restore_pin_ctl(codec, nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004464
4465 /* init loopback inputs */
4466 if (spec->mixer_nid) {
4467 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01004468 path = snd_hda_get_path_from_idx(codec, spec->loopback_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004469 if (path)
4470 snd_hda_activate_path(codec, path,
4471 path->active, false);
4472 }
4473 }
4474}
4475
4476/* initialize ADC paths */
4477static void init_input_src(struct hda_codec *codec)
4478{
4479 struct hda_gen_spec *spec = codec->spec;
4480 struct hda_input_mux *imux = &spec->input_mux;
4481 struct nid_path *path;
4482 int i, c, nums;
4483
4484 if (spec->dyn_adc_switch)
4485 nums = 1;
4486 else
4487 nums = spec->num_adc_nids;
4488
4489 for (c = 0; c < nums; c++) {
4490 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01004491 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004492 if (path) {
4493 bool active = path->active;
4494 if (i == spec->cur_mux[c])
4495 active = true;
4496 snd_hda_activate_path(codec, path, active, false);
4497 }
4498 }
4499 }
4500
4501 if (spec->shared_mic_hp)
4502 update_shared_mic_hp(codec, spec->cur_mux[0]);
4503
4504 if (spec->cap_sync_hook)
4505 spec->cap_sync_hook(codec);
4506}
4507
4508/* set right pin controls for digital I/O */
4509static void init_digital(struct hda_codec *codec)
4510{
4511 struct hda_gen_spec *spec = codec->spec;
4512 int i;
4513 hda_nid_t pin;
4514
Takashi Iwaid4156932013-01-07 10:08:02 +01004515 for (i = 0; i < spec->autocfg.dig_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004516 set_output_and_unmute(codec, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004517 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01004518 if (pin) {
4519 struct nid_path *path;
Takashi Iwai2c12c302013-01-10 09:33:29 +01004520 restore_pin_ctl(codec, pin);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01004521 path = snd_hda_get_path_from_idx(codec, spec->digin_path);
4522 if (path)
4523 snd_hda_activate_path(codec, path, path->active, false);
4524 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004525}
4526
Takashi Iwai973e4972012-12-20 15:16:09 +01004527/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
4528 * invalid unsol tags by some reason
4529 */
4530static void clear_unsol_on_unused_pins(struct hda_codec *codec)
4531{
4532 int i;
4533
4534 for (i = 0; i < codec->init_pins.used; i++) {
4535 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
4536 hda_nid_t nid = pin->nid;
4537 if (is_jack_detectable(codec, nid) &&
4538 !snd_hda_jack_tbl_get(codec, nid))
4539 snd_hda_codec_update_cache(codec, nid, 0,
4540 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
4541 }
4542}
4543
Takashi Iwai5187ac12013-01-07 12:52:16 +01004544/*
4545 * initialize the generic spec;
4546 * this can be put as patch_ops.init function
4547 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004548int snd_hda_gen_init(struct hda_codec *codec)
4549{
4550 struct hda_gen_spec *spec = codec->spec;
4551
4552 if (spec->init_hook)
4553 spec->init_hook(codec);
4554
4555 snd_hda_apply_verbs(codec);
4556
Takashi Iwai3bbcd272012-12-20 11:50:58 +01004557 codec->cached_write = 1;
4558
Takashi Iwai352f7f92012-12-19 12:52:06 +01004559 init_multi_out(codec);
4560 init_extra_out(codec);
4561 init_multi_io(codec);
4562 init_analog_input(codec);
4563 init_input_src(codec);
4564 init_digital(codec);
4565
Takashi Iwai973e4972012-12-20 15:16:09 +01004566 clear_unsol_on_unused_pins(codec);
4567
Takashi Iwai352f7f92012-12-19 12:52:06 +01004568 /* call init functions of standard auto-mute helpers */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01004569 update_automute_all(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004570
Takashi Iwai3bbcd272012-12-20 11:50:58 +01004571 snd_hda_codec_flush_amp_cache(codec);
4572 snd_hda_codec_flush_cmd_cache(codec);
4573
Takashi Iwai352f7f92012-12-19 12:52:06 +01004574 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
4575 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
4576
4577 hda_call_check_power_status(codec, 0x01);
4578 return 0;
4579}
Takashi Iwaifce52a32013-01-07 12:42:48 +01004580EXPORT_SYMBOL_HDA(snd_hda_gen_init);
4581
Takashi Iwai5187ac12013-01-07 12:52:16 +01004582/*
4583 * free the generic spec;
4584 * this can be put as patch_ops.free function
4585 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01004586void snd_hda_gen_free(struct hda_codec *codec)
4587{
4588 snd_hda_gen_spec_free(codec->spec);
4589 kfree(codec->spec);
4590 codec->spec = NULL;
4591}
4592EXPORT_SYMBOL_HDA(snd_hda_gen_free);
4593
4594#ifdef CONFIG_PM
Takashi Iwai5187ac12013-01-07 12:52:16 +01004595/*
4596 * check the loopback power save state;
4597 * this can be put as patch_ops.check_power_status function
4598 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01004599int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
4600{
4601 struct hda_gen_spec *spec = codec->spec;
4602 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
4603}
4604EXPORT_SYMBOL_HDA(snd_hda_gen_check_power_status);
4605#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01004606
4607
4608/*
4609 * the generic codec support
4610 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611
Takashi Iwai352f7f92012-12-19 12:52:06 +01004612static const struct hda_codec_ops generic_patch_ops = {
4613 .build_controls = snd_hda_gen_build_controls,
4614 .build_pcms = snd_hda_gen_build_pcms,
4615 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01004616 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004617 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02004618#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01004619 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02004620#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621};
4622
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623int snd_hda_parse_generic_codec(struct hda_codec *codec)
4624{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004625 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626 int err;
4627
Takashi Iwaie560d8d2005-09-09 14:21:46 +02004628 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004629 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004631 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004634 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
4635 if (err < 0)
4636 return err;
4637
4638 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004639 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 goto error;
4641
4642 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 return 0;
4644
Takashi Iwai352f7f92012-12-19 12:52:06 +01004645error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01004646 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 return err;
4648}
Takashi Iwaifce52a32013-01-07 12:42:48 +01004649EXPORT_SYMBOL_HDA(snd_hda_parse_generic_codec);