blob: 9c06749738b192381a698a4a00c3b531634ee5fb [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
828static const char * const channel_name[4] = {
829 "Front", "Surround", "CLFE", "Side"
830};
831
832/* give some appropriate ctl name prefix for the given line out channel */
833static const char *get_line_out_pfx(struct hda_gen_spec *spec, int ch,
834 bool can_be_master, int *index)
835{
836 struct auto_pin_cfg *cfg = &spec->autocfg;
837
838 *index = 0;
839 if (cfg->line_outs == 1 && !spec->multi_ios &&
840 !cfg->hp_outs && !cfg->speaker_outs && can_be_master)
841 return spec->vmaster_mute.hook ? "PCM" : "Master";
842
843 /* if there is really a single DAC used in the whole output paths,
844 * use it master (or "PCM" if a vmaster hook is present)
845 */
846 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
847 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
848 return spec->vmaster_mute.hook ? "PCM" : "Master";
849
850 switch (cfg->line_out_type) {
851 case AUTO_PIN_SPEAKER_OUT:
852 if (cfg->line_outs == 1)
853 return "Speaker";
854 if (cfg->line_outs == 2)
855 return ch ? "Bass Speaker" : "Speaker";
856 break;
857 case AUTO_PIN_HP_OUT:
858 /* for multi-io case, only the primary out */
859 if (ch && spec->multi_ios)
860 break;
861 *index = ch;
862 return "Headphone";
863 default:
864 if (cfg->line_outs == 1 && !spec->multi_ios)
865 return "PCM";
866 break;
867 }
868 if (ch >= ARRAY_SIZE(channel_name)) {
869 snd_BUG();
870 return "PCM";
871 }
872
873 return channel_name[ch];
874}
875
876/*
877 * Parse output paths
878 */
879
880/* badness definition */
881enum {
882 /* No primary DAC is found for the main output */
883 BAD_NO_PRIMARY_DAC = 0x10000,
884 /* No DAC is found for the extra output */
885 BAD_NO_DAC = 0x4000,
886 /* No possible multi-ios */
887 BAD_MULTI_IO = 0x103,
888 /* No individual DAC for extra output */
889 BAD_NO_EXTRA_DAC = 0x102,
890 /* No individual DAC for extra surrounds */
891 BAD_NO_EXTRA_SURR_DAC = 0x101,
892 /* Primary DAC shared with main surrounds */
893 BAD_SHARED_SURROUND = 0x100,
894 /* Primary DAC shared with main CLFE */
895 BAD_SHARED_CLFE = 0x10,
896 /* Primary DAC shared with extra surrounds */
897 BAD_SHARED_EXTRA_SURROUND = 0x10,
898 /* Volume widget is shared */
899 BAD_SHARED_VOL = 0x10,
900};
901
Takashi Iwai0e614dd2013-01-07 15:11:44 +0100902/* look for widgets in the given path which are appropriate for
Takashi Iwai352f7f92012-12-19 12:52:06 +0100903 * volume and mute controls, and assign the values to ctls[].
904 *
905 * When no appropriate widget is found in the path, the badness value
906 * is incremented depending on the situation. The function returns the
907 * total badness for both volume and mute controls.
908 */
Takashi Iwai0e614dd2013-01-07 15:11:44 +0100909static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100910{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100911 hda_nid_t nid;
912 unsigned int val;
913 int badness = 0;
914
915 if (!path)
916 return BAD_SHARED_VOL * 2;
Takashi Iwai0e614dd2013-01-07 15:11:44 +0100917
918 if (path->ctls[NID_PATH_VOL_CTL] ||
919 path->ctls[NID_PATH_MUTE_CTL])
920 return 0; /* already evaluated */
921
Takashi Iwai352f7f92012-12-19 12:52:06 +0100922 nid = look_for_out_vol_nid(codec, path);
923 if (nid) {
924 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
925 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
926 badness += BAD_SHARED_VOL;
927 else
928 path->ctls[NID_PATH_VOL_CTL] = val;
929 } else
930 badness += BAD_SHARED_VOL;
931 nid = look_for_out_mute_nid(codec, path);
932 if (nid) {
933 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
934 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
935 nid_has_mute(codec, nid, HDA_OUTPUT))
936 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
937 else
938 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
939 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
940 badness += BAD_SHARED_VOL;
941 else
942 path->ctls[NID_PATH_MUTE_CTL] = val;
943 } else
944 badness += BAD_SHARED_VOL;
945 return badness;
946}
947
948struct badness_table {
949 int no_primary_dac; /* no primary DAC */
950 int no_dac; /* no secondary DACs */
951 int shared_primary; /* primary DAC is shared with main output */
952 int shared_surr; /* secondary DAC shared with main or primary */
953 int shared_clfe; /* third DAC shared with main or primary */
954 int shared_surr_main; /* secondary DAC sahred with main/DAC0 */
955};
956
957static struct badness_table main_out_badness = {
958 .no_primary_dac = BAD_NO_PRIMARY_DAC,
959 .no_dac = BAD_NO_DAC,
960 .shared_primary = BAD_NO_PRIMARY_DAC,
961 .shared_surr = BAD_SHARED_SURROUND,
962 .shared_clfe = BAD_SHARED_CLFE,
963 .shared_surr_main = BAD_SHARED_SURROUND,
964};
965
966static struct badness_table extra_out_badness = {
967 .no_primary_dac = BAD_NO_DAC,
968 .no_dac = BAD_NO_DAC,
969 .shared_primary = BAD_NO_EXTRA_DAC,
970 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
971 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
972 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
973};
974
Takashi Iwai7385df62013-01-07 09:50:52 +0100975/* get the DAC of the primary output corresponding to the given array index */
976static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
977{
978 struct hda_gen_spec *spec = codec->spec;
979 struct auto_pin_cfg *cfg = &spec->autocfg;
980
981 if (cfg->line_outs > idx)
982 return spec->private_dac_nids[idx];
983 idx -= cfg->line_outs;
984 if (spec->multi_ios > idx)
985 return spec->multi_io[idx].dac;
986 return 0;
987}
988
989/* return the DAC if it's reachable, otherwise zero */
990static inline hda_nid_t try_dac(struct hda_codec *codec,
991 hda_nid_t dac, hda_nid_t pin)
992{
993 return is_reachable_path(codec, dac, pin) ? dac : 0;
994}
995
Takashi Iwai352f7f92012-12-19 12:52:06 +0100996/* try to assign DACs to pins and return the resultant badness */
997static int try_assign_dacs(struct hda_codec *codec, int num_outs,
998 const hda_nid_t *pins, hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +0100999 int *path_idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001000 const struct badness_table *bad)
1001{
1002 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001003 int i, j;
1004 int badness = 0;
1005 hda_nid_t dac;
1006
1007 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 return 0;
1009
Takashi Iwai352f7f92012-12-19 12:52:06 +01001010 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001011 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001012 hda_nid_t pin = pins[i];
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001013
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001014 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1015 if (path) {
1016 badness += assign_out_path_ctls(codec, path);
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001017 continue;
1018 }
1019
1020 dacs[i] = look_for_dac(codec, pin, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001021 if (!dacs[i] && !i) {
Takashi Iwai980428c2013-01-09 09:28:20 +01001022 /* try to steal the DAC of surrounds for the front */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001023 for (j = 1; j < num_outs; j++) {
1024 if (is_reachable_path(codec, dacs[j], pin)) {
1025 dacs[0] = dacs[j];
1026 dacs[j] = 0;
Takashi Iwai980428c2013-01-09 09:28:20 +01001027 invalidate_nid_path(codec, path_idx[j]);
Takashi Iwai196c17662013-01-04 15:01:40 +01001028 path_idx[j] = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001029 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 }
1031 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001032 }
1033 dac = dacs[i];
1034 if (!dac) {
Takashi Iwai7385df62013-01-07 09:50:52 +01001035 if (num_outs > 2)
1036 dac = try_dac(codec, get_primary_out(codec, i), pin);
1037 if (!dac)
1038 dac = try_dac(codec, dacs[0], pin);
1039 if (!dac)
1040 dac = try_dac(codec, get_primary_out(codec, i), pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001041 if (dac) {
1042 if (!i)
1043 badness += bad->shared_primary;
1044 else if (i == 1)
1045 badness += bad->shared_surr;
1046 else
1047 badness += bad->shared_clfe;
1048 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1049 dac = spec->private_dac_nids[0];
1050 badness += bad->shared_surr_main;
1051 } else if (!i)
1052 badness += bad->no_primary_dac;
1053 else
1054 badness += bad->no_dac;
1055 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001056 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001057 if (!path && !i && spec->mixer_nid) {
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001058 /* try with aamix */
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001059 path = snd_hda_add_new_path(codec, dac, pin, 0);
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001060 }
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001061 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001062 dac = dacs[i] = 0;
Takashi Iwaie1284af2013-01-03 16:33:02 +01001063 else {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001064 print_nid_path("output", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001065 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001066 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001067 badness += assign_out_path_ctls(codec, path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001068 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001069 }
1070
1071 return badness;
1072}
1073
1074/* return NID if the given pin has only a single connection to a certain DAC */
1075static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1076{
1077 struct hda_gen_spec *spec = codec->spec;
1078 int i;
1079 hda_nid_t nid_found = 0;
1080
1081 for (i = 0; i < spec->num_all_dacs; i++) {
1082 hda_nid_t nid = spec->all_dacs[i];
1083 if (!nid || is_dac_already_used(codec, nid))
1084 continue;
1085 if (is_reachable_path(codec, nid, pin)) {
1086 if (nid_found)
1087 return 0;
1088 nid_found = nid;
1089 }
1090 }
1091 return nid_found;
1092}
1093
1094/* check whether the given pin can be a multi-io pin */
1095static bool can_be_multiio_pin(struct hda_codec *codec,
1096 unsigned int location, hda_nid_t nid)
1097{
1098 unsigned int defcfg, caps;
1099
1100 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1101 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1102 return false;
1103 if (location && get_defcfg_location(defcfg) != location)
1104 return false;
1105 caps = snd_hda_query_pin_caps(codec, nid);
1106 if (!(caps & AC_PINCAP_OUT))
1107 return false;
1108 return true;
1109}
1110
Takashi Iwaie22aab72013-01-04 14:50:04 +01001111/* count the number of input pins that are capable to be multi-io */
1112static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1113{
1114 struct hda_gen_spec *spec = codec->spec;
1115 struct auto_pin_cfg *cfg = &spec->autocfg;
1116 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1117 unsigned int location = get_defcfg_location(defcfg);
1118 int type, i;
1119 int num_pins = 0;
1120
1121 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1122 for (i = 0; i < cfg->num_inputs; i++) {
1123 if (cfg->inputs[i].type != type)
1124 continue;
1125 if (can_be_multiio_pin(codec, location,
1126 cfg->inputs[i].pin))
1127 num_pins++;
1128 }
1129 }
1130 return num_pins;
1131}
1132
Takashi Iwai352f7f92012-12-19 12:52:06 +01001133/*
1134 * multi-io helper
1135 *
1136 * When hardwired is set, try to fill ony hardwired pins, and returns
1137 * zero if any pins are filled, non-zero if nothing found.
1138 * When hardwired is off, try to fill possible input pins, and returns
1139 * the badness value.
1140 */
1141static int fill_multi_ios(struct hda_codec *codec,
1142 hda_nid_t reference_pin,
Takashi Iwaie22aab72013-01-04 14:50:04 +01001143 bool hardwired)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001144{
1145 struct hda_gen_spec *spec = codec->spec;
1146 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie22aab72013-01-04 14:50:04 +01001147 int type, i, j, num_pins, old_pins;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001148 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1149 unsigned int location = get_defcfg_location(defcfg);
1150 int badness = 0;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001151 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001152
1153 old_pins = spec->multi_ios;
1154 if (old_pins >= 2)
1155 goto end_fill;
1156
Takashi Iwaie22aab72013-01-04 14:50:04 +01001157 num_pins = count_multiio_pins(codec, reference_pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001158 if (num_pins < 2)
1159 goto end_fill;
1160
Takashi Iwai352f7f92012-12-19 12:52:06 +01001161 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1162 for (i = 0; i < cfg->num_inputs; i++) {
1163 hda_nid_t nid = cfg->inputs[i].pin;
1164 hda_nid_t dac = 0;
1165
1166 if (cfg->inputs[i].type != type)
1167 continue;
1168 if (!can_be_multiio_pin(codec, location, nid))
1169 continue;
1170 for (j = 0; j < spec->multi_ios; j++) {
1171 if (nid == spec->multi_io[j].pin)
1172 break;
1173 }
1174 if (j < spec->multi_ios)
1175 continue;
1176
Takashi Iwai352f7f92012-12-19 12:52:06 +01001177 if (hardwired)
1178 dac = get_dac_if_single(codec, nid);
1179 else if (!dac)
1180 dac = look_for_dac(codec, nid, false);
1181 if (!dac) {
1182 badness++;
1183 continue;
1184 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001185 path = snd_hda_add_new_path(codec, dac, nid,
1186 -spec->mixer_nid);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001187 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001188 badness++;
1189 continue;
1190 }
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001191 print_nid_path("multiio", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001192 spec->multi_io[spec->multi_ios].pin = nid;
1193 spec->multi_io[spec->multi_ios].dac = dac;
Takashi Iwai196c17662013-01-04 15:01:40 +01001194 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1195 snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001196 spec->multi_ios++;
1197 if (spec->multi_ios >= 2)
1198 break;
1199 }
1200 }
1201 end_fill:
1202 if (badness)
1203 badness = BAD_MULTI_IO;
1204 if (old_pins == spec->multi_ios) {
1205 if (hardwired)
1206 return 1; /* nothing found */
1207 else
1208 return badness; /* no badness if nothing found */
1209 }
1210 if (!hardwired && spec->multi_ios < 2) {
1211 /* cancel newly assigned paths */
1212 spec->paths.used -= spec->multi_ios - old_pins;
1213 spec->multi_ios = old_pins;
1214 return badness;
1215 }
1216
1217 /* assign volume and mute controls */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001218 for (i = old_pins; i < spec->multi_ios; i++) {
1219 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1220 badness += assign_out_path_ctls(codec, path);
1221 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001222
1223 return badness;
1224}
1225
1226/* map DACs for all pins in the list if they are single connections */
1227static bool map_singles(struct hda_codec *codec, int outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001228 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001229{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001230 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001231 int i;
1232 bool found = false;
1233 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001234 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001235 hda_nid_t dac;
1236 if (dacs[i])
1237 continue;
1238 dac = get_dac_if_single(codec, pins[i]);
1239 if (!dac)
1240 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001241 path = snd_hda_add_new_path(codec, dac, pins[i],
1242 -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001243 if (!path && !i && spec->mixer_nid)
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001244 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001245 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001246 dacs[i] = dac;
1247 found = true;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001248 print_nid_path("output", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001249 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001250 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001251 }
1252 }
1253 return found;
1254}
1255
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001256/* create a new path including aamix if available, and return its index */
1257static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1258{
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001259 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001260 struct nid_path *path;
1261
1262 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001263 if (!path || !path->depth ||
1264 is_nid_contained(path, spec->mixer_nid))
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001265 return 0;
1266 path = snd_hda_add_new_path(codec, path->path[0],
1267 path->path[path->depth - 1],
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001268 spec->mixer_nid);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001269 if (!path)
1270 return 0;
1271 print_nid_path("output-aamix", path);
1272 path->active = false; /* unused as default */
1273 return snd_hda_get_path_idx(codec, path);
1274}
1275
Takashi Iwaia07a9492013-01-07 16:44:06 +01001276/* fill the empty entries in the dac array for speaker/hp with the
1277 * shared dac pointed by the paths
1278 */
1279static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1280 hda_nid_t *dacs, int *path_idx)
1281{
1282 struct nid_path *path;
1283 int i;
1284
1285 for (i = 0; i < num_outs; i++) {
1286 if (dacs[i])
1287 continue;
1288 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1289 if (!path)
1290 continue;
1291 dacs[i] = path->path[0];
1292 }
1293}
1294
Takashi Iwai352f7f92012-12-19 12:52:06 +01001295/* fill in the dac_nids table from the parsed pin configuration */
1296static int fill_and_eval_dacs(struct hda_codec *codec,
1297 bool fill_hardwired,
1298 bool fill_mio_first)
1299{
1300 struct hda_gen_spec *spec = codec->spec;
1301 struct auto_pin_cfg *cfg = &spec->autocfg;
1302 int i, err, badness;
Takashi Iwaiea46c3c2013-01-15 18:45:53 +01001303 unsigned int val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001304
1305 /* set num_dacs once to full for look_for_dac() */
1306 spec->multiout.num_dacs = cfg->line_outs;
1307 spec->multiout.dac_nids = spec->private_dac_nids;
1308 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1309 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1310 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1311 spec->multi_ios = 0;
1312 snd_array_free(&spec->paths);
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001313
1314 /* clear path indices */
1315 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1316 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1317 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1318 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1319 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
Takashi Iwaic697b712013-01-07 17:09:26 +01001320 memset(spec->input_paths, 0, sizeof(spec->input_paths));
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001321 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1322 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1323
Takashi Iwai352f7f92012-12-19 12:52:06 +01001324 badness = 0;
1325
1326 /* fill hard-wired DACs first */
1327 if (fill_hardwired) {
1328 bool mapped;
1329 do {
1330 mapped = map_singles(codec, cfg->line_outs,
1331 cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001332 spec->private_dac_nids,
1333 spec->out_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001334 mapped |= map_singles(codec, cfg->hp_outs,
1335 cfg->hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001336 spec->multiout.hp_out_nid,
1337 spec->hp_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001338 mapped |= map_singles(codec, cfg->speaker_outs,
1339 cfg->speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001340 spec->multiout.extra_out_nid,
1341 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001342 if (fill_mio_first && cfg->line_outs == 1 &&
1343 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001344 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001345 if (!err)
1346 mapped = true;
1347 }
1348 } while (mapped);
1349 }
1350
1351 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001352 spec->private_dac_nids, spec->out_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001353 &main_out_badness);
1354
Takashi Iwai352f7f92012-12-19 12:52:06 +01001355 if (fill_mio_first &&
1356 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1357 /* try to fill multi-io first */
Takashi Iwaie22aab72013-01-04 14:50:04 +01001358 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001359 if (err < 0)
1360 return err;
1361 /* we don't count badness at this stage yet */
1362 }
1363
1364 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1365 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1366 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001367 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001368 &extra_out_badness);
1369 if (err < 0)
1370 return err;
1371 badness += err;
1372 }
1373 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1374 err = try_assign_dacs(codec, cfg->speaker_outs,
1375 cfg->speaker_pins,
1376 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001377 spec->speaker_paths,
1378 &extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001379 if (err < 0)
1380 return err;
1381 badness += err;
1382 }
1383 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001384 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001385 if (err < 0)
1386 return err;
1387 badness += err;
1388 }
Takashi Iwaie22aab72013-01-04 14:50:04 +01001389
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001390 if (spec->mixer_nid) {
1391 spec->aamix_out_paths[0] =
1392 check_aamix_out_path(codec, spec->out_paths[0]);
1393 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1394 spec->aamix_out_paths[1] =
1395 check_aamix_out_path(codec, spec->hp_paths[0]);
1396 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1397 spec->aamix_out_paths[2] =
1398 check_aamix_out_path(codec, spec->speaker_paths[0]);
1399 }
1400
Takashi Iwaie22aab72013-01-04 14:50:04 +01001401 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1402 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1403 spec->multi_ios = 1; /* give badness */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001404
Takashi Iwaia07a9492013-01-07 16:44:06 +01001405 /* re-count num_dacs and squash invalid entries */
1406 spec->multiout.num_dacs = 0;
1407 for (i = 0; i < cfg->line_outs; i++) {
1408 if (spec->private_dac_nids[i])
1409 spec->multiout.num_dacs++;
1410 else {
1411 memmove(spec->private_dac_nids + i,
1412 spec->private_dac_nids + i + 1,
1413 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1414 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1415 }
1416 }
1417
1418 spec->ext_channel_count = spec->min_channel_count =
David Henningssonc0f3b212013-01-16 11:45:37 +01001419 spec->multiout.num_dacs * 2;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001420
Takashi Iwai352f7f92012-12-19 12:52:06 +01001421 if (spec->multi_ios == 2) {
1422 for (i = 0; i < 2; i++)
1423 spec->private_dac_nids[spec->multiout.num_dacs++] =
1424 spec->multi_io[i].dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001425 } else if (spec->multi_ios) {
1426 spec->multi_ios = 0;
1427 badness += BAD_MULTI_IO;
1428 }
1429
Takashi Iwaia07a9492013-01-07 16:44:06 +01001430 /* re-fill the shared DAC for speaker / headphone */
1431 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1432 refill_shared_dacs(codec, cfg->hp_outs,
1433 spec->multiout.hp_out_nid,
1434 spec->hp_paths);
1435 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1436 refill_shared_dacs(codec, cfg->speaker_outs,
1437 spec->multiout.extra_out_nid,
1438 spec->speaker_paths);
1439
Takashi Iwai2c12c302013-01-10 09:33:29 +01001440 /* set initial pinctl targets */
Takashi Iwaiea46c3c2013-01-15 18:45:53 +01001441 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
1442 val = PIN_HP;
1443 else
1444 val = PIN_OUT;
1445 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
Takashi Iwai2c12c302013-01-10 09:33:29 +01001446 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1447 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
Takashi Iwaiea46c3c2013-01-15 18:45:53 +01001448 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1449 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
Takashi Iwai2c12c302013-01-10 09:33:29 +01001450 set_pin_targets(codec, cfg->speaker_outs,
Takashi Iwaiea46c3c2013-01-15 18:45:53 +01001451 cfg->speaker_pins, val);
1452 }
Takashi Iwai2c12c302013-01-10 09:33:29 +01001453
Takashi Iwai352f7f92012-12-19 12:52:06 +01001454 return badness;
1455}
1456
1457#define DEBUG_BADNESS
1458
1459#ifdef DEBUG_BADNESS
1460#define debug_badness snd_printdd
1461#else
1462#define debug_badness(...)
1463#endif
1464
1465static void debug_show_configs(struct hda_gen_spec *spec, struct auto_pin_cfg *cfg)
1466{
1467 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1468 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001469 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001470 spec->multiout.dac_nids[0],
1471 spec->multiout.dac_nids[1],
1472 spec->multiout.dac_nids[2],
1473 spec->multiout.dac_nids[3]);
1474 if (spec->multi_ios > 0)
1475 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1476 spec->multi_ios,
1477 spec->multi_io[0].pin, spec->multi_io[1].pin,
1478 spec->multi_io[0].dac, spec->multi_io[1].dac);
1479 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1480 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001481 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001482 spec->multiout.hp_out_nid[0],
1483 spec->multiout.hp_out_nid[1],
1484 spec->multiout.hp_out_nid[2],
1485 spec->multiout.hp_out_nid[3]);
1486 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1487 cfg->speaker_pins[0], cfg->speaker_pins[1],
1488 cfg->speaker_pins[2], cfg->speaker_pins[3],
1489 spec->multiout.extra_out_nid[0],
1490 spec->multiout.extra_out_nid[1],
1491 spec->multiout.extra_out_nid[2],
1492 spec->multiout.extra_out_nid[3]);
1493}
1494
1495/* find all available DACs of the codec */
1496static void fill_all_dac_nids(struct hda_codec *codec)
1497{
1498 struct hda_gen_spec *spec = codec->spec;
1499 int i;
1500 hda_nid_t nid = codec->start_nid;
1501
1502 spec->num_all_dacs = 0;
1503 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1504 for (i = 0; i < codec->num_nodes; i++, nid++) {
1505 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1506 continue;
1507 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1508 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1509 break;
1510 }
1511 spec->all_dacs[spec->num_all_dacs++] = nid;
1512 }
1513}
1514
1515static int parse_output_paths(struct hda_codec *codec)
1516{
1517 struct hda_gen_spec *spec = codec->spec;
1518 struct auto_pin_cfg *cfg = &spec->autocfg;
1519 struct auto_pin_cfg *best_cfg;
1520 int best_badness = INT_MAX;
1521 int badness;
1522 bool fill_hardwired = true, fill_mio_first = true;
1523 bool best_wired = true, best_mio = true;
1524 bool hp_spk_swapped = false;
1525
Takashi Iwai352f7f92012-12-19 12:52:06 +01001526 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1527 if (!best_cfg)
1528 return -ENOMEM;
1529 *best_cfg = *cfg;
1530
1531 for (;;) {
1532 badness = fill_and_eval_dacs(codec, fill_hardwired,
1533 fill_mio_first);
1534 if (badness < 0) {
1535 kfree(best_cfg);
1536 return badness;
1537 }
1538 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1539 cfg->line_out_type, fill_hardwired, fill_mio_first,
1540 badness);
1541 debug_show_configs(spec, cfg);
1542 if (badness < best_badness) {
1543 best_badness = badness;
1544 *best_cfg = *cfg;
1545 best_wired = fill_hardwired;
1546 best_mio = fill_mio_first;
1547 }
1548 if (!badness)
1549 break;
1550 fill_mio_first = !fill_mio_first;
1551 if (!fill_mio_first)
1552 continue;
1553 fill_hardwired = !fill_hardwired;
1554 if (!fill_hardwired)
1555 continue;
1556 if (hp_spk_swapped)
1557 break;
1558 hp_spk_swapped = true;
1559 if (cfg->speaker_outs > 0 &&
1560 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1561 cfg->hp_outs = cfg->line_outs;
1562 memcpy(cfg->hp_pins, cfg->line_out_pins,
1563 sizeof(cfg->hp_pins));
1564 cfg->line_outs = cfg->speaker_outs;
1565 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1566 sizeof(cfg->speaker_pins));
1567 cfg->speaker_outs = 0;
1568 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1569 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1570 fill_hardwired = true;
1571 continue;
1572 }
1573 if (cfg->hp_outs > 0 &&
1574 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1575 cfg->speaker_outs = cfg->line_outs;
1576 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1577 sizeof(cfg->speaker_pins));
1578 cfg->line_outs = cfg->hp_outs;
1579 memcpy(cfg->line_out_pins, cfg->hp_pins,
1580 sizeof(cfg->hp_pins));
1581 cfg->hp_outs = 0;
1582 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1583 cfg->line_out_type = AUTO_PIN_HP_OUT;
1584 fill_hardwired = true;
1585 continue;
1586 }
1587 break;
1588 }
1589
1590 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001591 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001592 *cfg = *best_cfg;
1593 fill_and_eval_dacs(codec, best_wired, best_mio);
1594 }
1595 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1596 cfg->line_out_type, best_wired, best_mio);
1597 debug_show_configs(spec, cfg);
1598
1599 if (cfg->line_out_pins[0]) {
1600 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01001601 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001602 if (path)
1603 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01001604 if (spec->vmaster_nid)
1605 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
1606 HDA_OUTPUT, spec->vmaster_tlv);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001607 }
1608
1609 kfree(best_cfg);
1610 return 0;
1611}
1612
1613/* add playback controls from the parsed DAC table */
1614static int create_multi_out_ctls(struct hda_codec *codec,
1615 const struct auto_pin_cfg *cfg)
1616{
1617 struct hda_gen_spec *spec = codec->spec;
1618 int i, err, noutputs;
1619
1620 noutputs = cfg->line_outs;
1621 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1622 noutputs += spec->multi_ios;
1623
1624 for (i = 0; i < noutputs; i++) {
1625 const char *name;
1626 int index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001627 struct nid_path *path;
1628
Takashi Iwai352f7f92012-12-19 12:52:06 +01001629 if (i >= cfg->line_outs) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001630 index = 0;
1631 name = channel_name[i];
1632 } else {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001633 name = get_line_out_pfx(spec, i, true, &index);
1634 }
1635
Takashi Iwai196c17662013-01-04 15:01:40 +01001636 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001637 if (!path)
1638 continue;
1639 if (!name || !strcmp(name, "CLFE")) {
1640 /* Center/LFE */
1641 err = add_vol_ctl(codec, "Center", 0, 1, path);
1642 if (err < 0)
1643 return err;
1644 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1645 if (err < 0)
1646 return err;
1647 err = add_sw_ctl(codec, "Center", 0, 1, path);
1648 if (err < 0)
1649 return err;
1650 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1651 if (err < 0)
1652 return err;
1653 } else {
1654 err = add_stereo_vol(codec, name, index, path);
1655 if (err < 0)
1656 return err;
1657 err = add_stereo_sw(codec, name, index, path);
1658 if (err < 0)
1659 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 }
1661 }
1662 return 0;
1663}
1664
Takashi Iwaic2c80382013-01-07 10:33:57 +01001665static int create_extra_out(struct hda_codec *codec, int path_idx,
Takashi Iwai196c17662013-01-04 15:01:40 +01001666 const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001668 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 int err;
1670
Takashi Iwai196c17662013-01-04 15:01:40 +01001671 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001672 if (!path)
1673 return 0;
Takashi Iwaic2c80382013-01-07 10:33:57 +01001674 err = add_stereo_vol(codec, pfx, cidx, path);
1675 if (err < 0)
1676 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001677 err = add_stereo_sw(codec, pfx, cidx, path);
1678 if (err < 0)
1679 return err;
1680 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681}
1682
Takashi Iwai352f7f92012-12-19 12:52:06 +01001683/* add playback controls for speaker and HP outputs */
1684static int create_extra_outs(struct hda_codec *codec, int num_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001685 const int *paths, const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686{
Takashi Iwaic2c80382013-01-07 10:33:57 +01001687 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001688
1689 for (i = 0; i < num_pins; i++) {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001690 const char *name;
1691 char tmp[44];
1692 int err, idx = 0;
1693
1694 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
1695 name = "Bass Speaker";
1696 else if (num_pins >= 3) {
1697 snprintf(tmp, sizeof(tmp), "%s %s",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001698 pfx, channel_name[i]);
Takashi Iwaic2c80382013-01-07 10:33:57 +01001699 name = tmp;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001700 } else {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001701 name = pfx;
1702 idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 }
Takashi Iwaic2c80382013-01-07 10:33:57 +01001704 err = create_extra_out(codec, paths[i], name, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001705 if (err < 0)
1706 return err;
1707 }
1708 return 0;
1709}
Takashi Iwai97ec5582006-03-21 11:29:07 +01001710
Takashi Iwai352f7f92012-12-19 12:52:06 +01001711static int create_hp_out_ctls(struct hda_codec *codec)
1712{
1713 struct hda_gen_spec *spec = codec->spec;
1714 return create_extra_outs(codec, spec->autocfg.hp_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001715 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001716 "Headphone");
1717}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
Takashi Iwai352f7f92012-12-19 12:52:06 +01001719static int create_speaker_out_ctls(struct hda_codec *codec)
1720{
1721 struct hda_gen_spec *spec = codec->spec;
1722 return create_extra_outs(codec, spec->autocfg.speaker_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001723 spec->speaker_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001724 "Speaker");
1725}
1726
1727/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001728 * independent HP controls
1729 */
1730
1731static int indep_hp_info(struct snd_kcontrol *kcontrol,
1732 struct snd_ctl_elem_info *uinfo)
1733{
1734 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
1735}
1736
1737static int indep_hp_get(struct snd_kcontrol *kcontrol,
1738 struct snd_ctl_elem_value *ucontrol)
1739{
1740 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1741 struct hda_gen_spec *spec = codec->spec;
1742 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
1743 return 0;
1744}
1745
1746static int indep_hp_put(struct snd_kcontrol *kcontrol,
1747 struct snd_ctl_elem_value *ucontrol)
1748{
1749 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1750 struct hda_gen_spec *spec = codec->spec;
1751 unsigned int select = ucontrol->value.enumerated.item[0];
1752 int ret = 0;
1753
1754 mutex_lock(&spec->pcm_mutex);
1755 if (spec->active_streams) {
1756 ret = -EBUSY;
1757 goto unlock;
1758 }
1759
1760 if (spec->indep_hp_enabled != select) {
1761 spec->indep_hp_enabled = select;
1762 if (spec->indep_hp_enabled)
1763 spec->multiout.hp_out_nid[0] = 0;
1764 else
1765 spec->multiout.hp_out_nid[0] = spec->alt_dac_nid;
1766 ret = 1;
1767 }
1768 unlock:
1769 mutex_unlock(&spec->pcm_mutex);
1770 return ret;
1771}
1772
1773static const struct snd_kcontrol_new indep_hp_ctl = {
1774 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1775 .name = "Independent HP",
1776 .info = indep_hp_info,
1777 .get = indep_hp_get,
1778 .put = indep_hp_put,
1779};
1780
1781
1782static int create_indep_hp_ctls(struct hda_codec *codec)
1783{
1784 struct hda_gen_spec *spec = codec->spec;
1785
1786 if (!spec->indep_hp)
1787 return 0;
1788 if (!spec->multiout.hp_out_nid[0]) {
1789 spec->indep_hp = 0;
1790 return 0;
1791 }
1792
1793 spec->indep_hp_enabled = false;
1794 spec->alt_dac_nid = spec->multiout.hp_out_nid[0];
1795 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
1796 return -ENOMEM;
1797 return 0;
1798}
1799
1800/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01001801 * channel mode enum control
1802 */
1803
1804static int ch_mode_info(struct snd_kcontrol *kcontrol,
1805 struct snd_ctl_elem_info *uinfo)
1806{
1807 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1808 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001809 int chs;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001810
1811 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1812 uinfo->count = 1;
1813 uinfo->value.enumerated.items = spec->multi_ios + 1;
1814 if (uinfo->value.enumerated.item > spec->multi_ios)
1815 uinfo->value.enumerated.item = spec->multi_ios;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001816 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
1817 sprintf(uinfo->value.enumerated.name, "%dch", chs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001818 return 0;
1819}
1820
1821static int ch_mode_get(struct snd_kcontrol *kcontrol,
1822 struct snd_ctl_elem_value *ucontrol)
1823{
1824 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1825 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001826 ucontrol->value.enumerated.item[0] =
1827 (spec->ext_channel_count - spec->min_channel_count) / 2;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001828 return 0;
1829}
1830
Takashi Iwai196c17662013-01-04 15:01:40 +01001831static inline struct nid_path *
1832get_multiio_path(struct hda_codec *codec, int idx)
1833{
1834 struct hda_gen_spec *spec = codec->spec;
1835 return snd_hda_get_path_from_idx(codec,
1836 spec->out_paths[spec->autocfg.line_outs + idx]);
1837}
1838
Takashi Iwaia5cc2502013-01-16 18:08:55 +01001839static void update_automute_all(struct hda_codec *codec);
1840
Takashi Iwai352f7f92012-12-19 12:52:06 +01001841static int set_multi_io(struct hda_codec *codec, int idx, bool output)
1842{
1843 struct hda_gen_spec *spec = codec->spec;
1844 hda_nid_t nid = spec->multi_io[idx].pin;
1845 struct nid_path *path;
1846
Takashi Iwai196c17662013-01-04 15:01:40 +01001847 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001848 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001850
1851 if (path->active == output)
1852 return 0;
1853
1854 if (output) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01001855 set_pin_target(codec, nid, PIN_OUT, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001856 snd_hda_activate_path(codec, path, true, true);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01001857 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001858 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01001859 set_pin_eapd(codec, nid, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001860 snd_hda_activate_path(codec, path, false, true);
Takashi Iwai2c12c302013-01-10 09:33:29 +01001861 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 }
Takashi Iwaia365fed2013-01-10 16:10:06 +01001863
1864 /* update jack retasking in case it modifies any of them */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01001865 update_automute_all(codec);
Takashi Iwaia365fed2013-01-10 16:10:06 +01001866
Takashi Iwai352f7f92012-12-19 12:52:06 +01001867 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868}
1869
Takashi Iwai352f7f92012-12-19 12:52:06 +01001870static int ch_mode_put(struct snd_kcontrol *kcontrol,
1871 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001873 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1874 struct hda_gen_spec *spec = codec->spec;
1875 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
Takashi Iwai352f7f92012-12-19 12:52:06 +01001877 ch = ucontrol->value.enumerated.item[0];
1878 if (ch < 0 || ch > spec->multi_ios)
1879 return -EINVAL;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001880 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001881 return 0;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001882 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001883 for (i = 0; i < spec->multi_ios; i++)
1884 set_multi_io(codec, i, i < ch);
1885 spec->multiout.max_channels = max(spec->ext_channel_count,
1886 spec->const_channel_count);
1887 if (spec->need_dac_fix)
1888 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 return 1;
1890}
1891
Takashi Iwai352f7f92012-12-19 12:52:06 +01001892static const struct snd_kcontrol_new channel_mode_enum = {
1893 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1894 .name = "Channel Mode",
1895 .info = ch_mode_info,
1896 .get = ch_mode_get,
1897 .put = ch_mode_put,
1898};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
Takashi Iwai352f7f92012-12-19 12:52:06 +01001900static int create_multi_channel_mode(struct hda_codec *codec)
1901{
1902 struct hda_gen_spec *spec = codec->spec;
1903
1904 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01001905 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01001906 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 return 0;
1909}
1910
Takashi Iwai352f7f92012-12-19 12:52:06 +01001911/*
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001912 * aamix loopback enable/disable switch
1913 */
1914
1915#define loopback_mixing_info indep_hp_info
1916
1917static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
1918 struct snd_ctl_elem_value *ucontrol)
1919{
1920 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1921 struct hda_gen_spec *spec = codec->spec;
1922 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
1923 return 0;
1924}
1925
1926static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
1927 int nomix_path_idx, int mix_path_idx)
1928{
1929 struct nid_path *nomix_path, *mix_path;
1930
1931 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
1932 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
1933 if (!nomix_path || !mix_path)
1934 return;
1935 if (do_mix) {
1936 snd_hda_activate_path(codec, nomix_path, false, true);
1937 snd_hda_activate_path(codec, mix_path, true, true);
1938 } else {
1939 snd_hda_activate_path(codec, mix_path, false, true);
1940 snd_hda_activate_path(codec, nomix_path, true, true);
1941 }
1942}
1943
1944static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
1945 struct snd_ctl_elem_value *ucontrol)
1946{
1947 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1948 struct hda_gen_spec *spec = codec->spec;
1949 unsigned int val = ucontrol->value.enumerated.item[0];
1950
1951 if (val == spec->aamix_mode)
1952 return 0;
1953 spec->aamix_mode = val;
1954 update_aamix_paths(codec, val, spec->out_paths[0],
1955 spec->aamix_out_paths[0]);
1956 update_aamix_paths(codec, val, spec->hp_paths[0],
1957 spec->aamix_out_paths[1]);
1958 update_aamix_paths(codec, val, spec->speaker_paths[0],
1959 spec->aamix_out_paths[2]);
1960 return 1;
1961}
1962
1963static const struct snd_kcontrol_new loopback_mixing_enum = {
1964 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1965 .name = "Loopback Mixing",
1966 .info = loopback_mixing_info,
1967 .get = loopback_mixing_get,
1968 .put = loopback_mixing_put,
1969};
1970
1971static int create_loopback_mixing_ctl(struct hda_codec *codec)
1972{
1973 struct hda_gen_spec *spec = codec->spec;
1974
1975 if (!spec->mixer_nid)
1976 return 0;
1977 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
1978 spec->aamix_out_paths[2]))
1979 return 0;
1980 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
1981 return -ENOMEM;
1982 return 0;
1983}
1984
1985/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01001986 * shared headphone/mic handling
1987 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02001988
Takashi Iwai352f7f92012-12-19 12:52:06 +01001989static void call_update_outputs(struct hda_codec *codec);
1990
1991/* for shared I/O, change the pin-control accordingly */
1992static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic)
1993{
1994 struct hda_gen_spec *spec = codec->spec;
1995 unsigned int val;
1996 hda_nid_t pin = spec->autocfg.inputs[1].pin;
1997 /* NOTE: this assumes that there are only two inputs, the
1998 * first is the real internal mic and the second is HP/mic jack.
1999 */
2000
2001 val = snd_hda_get_default_vref(codec, pin);
2002
2003 /* This pin does not have vref caps - let's enable vref on pin 0x18
2004 instead, as suggested by Realtek */
2005 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2006 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2007 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2008 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01002009 snd_hda_set_pin_ctl_cache(codec, vref_pin,
2010 PIN_IN | (set_as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02002011 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002012
2013 val = set_as_mic ? val | PIN_IN : PIN_HP;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002014 set_pin_target(codec, pin, val, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002015
2016 spec->automute_speaker = !set_as_mic;
2017 call_update_outputs(codec);
2018}
2019
2020/* create a shared input with the headphone out */
2021static int create_shared_input(struct hda_codec *codec)
2022{
2023 struct hda_gen_spec *spec = codec->spec;
2024 struct auto_pin_cfg *cfg = &spec->autocfg;
2025 unsigned int defcfg;
2026 hda_nid_t nid;
2027
2028 /* only one internal input pin? */
2029 if (cfg->num_inputs != 1)
2030 return 0;
2031 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2032 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2033 return 0;
2034
2035 if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
2036 nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
2037 else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
2038 nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
2039 else
2040 return 0; /* both not available */
2041
2042 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2043 return 0; /* no input */
2044
2045 cfg->inputs[1].pin = nid;
2046 cfg->inputs[1].type = AUTO_PIN_MIC;
2047 cfg->num_inputs = 2;
2048 spec->shared_mic_hp = 1;
2049 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
2050 return 0;
2051}
2052
Takashi Iwai978e77e2013-01-10 16:57:58 +01002053/*
2054 * output jack mode
2055 */
2056static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2057 struct snd_ctl_elem_info *uinfo)
2058{
2059 static const char * const texts[] = {
2060 "Line Out", "Headphone Out",
2061 };
2062 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, texts);
2063}
2064
2065static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2066 struct snd_ctl_elem_value *ucontrol)
2067{
2068 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2069 hda_nid_t nid = kcontrol->private_value;
2070 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2071 ucontrol->value.enumerated.item[0] = 1;
2072 else
2073 ucontrol->value.enumerated.item[0] = 0;
2074 return 0;
2075}
2076
2077static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2078 struct snd_ctl_elem_value *ucontrol)
2079{
2080 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2081 hda_nid_t nid = kcontrol->private_value;
2082 unsigned int val;
2083
2084 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2085 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2086 return 0;
2087 snd_hda_set_pin_ctl_cache(codec, nid, val);
2088 return 1;
2089}
2090
2091static const struct snd_kcontrol_new out_jack_mode_enum = {
2092 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2093 .info = out_jack_mode_info,
2094 .get = out_jack_mode_get,
2095 .put = out_jack_mode_put,
2096};
2097
2098static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2099{
2100 struct hda_gen_spec *spec = codec->spec;
2101 int i;
2102
2103 for (i = 0; i < spec->kctls.used; i++) {
2104 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2105 if (!strcmp(kctl->name, name) && kctl->index == idx)
2106 return true;
2107 }
2108 return false;
2109}
2110
2111static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2112 char *name, size_t name_len)
2113{
2114 struct hda_gen_spec *spec = codec->spec;
2115 int idx = 0;
2116
2117 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2118 strlcat(name, " Jack Mode", name_len);
2119
2120 for (; find_kctl_name(codec, name, idx); idx++)
2121 ;
2122}
2123
2124static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2125 hda_nid_t *pins)
2126{
2127 struct hda_gen_spec *spec = codec->spec;
2128 int i;
2129
2130 for (i = 0; i < num_pins; i++) {
2131 hda_nid_t pin = pins[i];
2132 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2133 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV)) {
2134 struct snd_kcontrol_new *knew;
2135 char name[44];
2136 get_jack_mode_name(codec, pin, name, sizeof(name));
2137 knew = snd_hda_gen_add_kctl(spec, name,
2138 &out_jack_mode_enum);
2139 if (!knew)
2140 return -ENOMEM;
2141 knew->private_value = pin;
2142 }
2143 }
2144
2145 return 0;
2146}
2147
Takashi Iwai294765582013-01-17 09:52:11 +01002148/*
2149 * input jack mode
2150 */
2151
2152/* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2153#define NUM_VREFS 6
2154
2155static const char * const vref_texts[NUM_VREFS] = {
2156 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2157 "", "Mic 80pc Bias", "Mic 100pc Bias"
2158};
2159
2160static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2161{
2162 unsigned int pincap;
2163
2164 pincap = snd_hda_query_pin_caps(codec, pin);
2165 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2166 /* filter out unusual vrefs */
2167 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2168 return pincap;
2169}
2170
2171/* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2172static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2173{
2174 unsigned int i, n = 0;
2175
2176 for (i = 0; i < NUM_VREFS; i++) {
2177 if (vref_caps & (1 << i)) {
2178 if (n == item_idx)
2179 return i;
2180 n++;
2181 }
2182 }
2183 return 0;
2184}
2185
2186/* convert back from the vref ctl index to the enum item index */
2187static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2188{
2189 unsigned int i, n = 0;
2190
2191 for (i = 0; i < NUM_VREFS; i++) {
2192 if (i == idx)
2193 return n;
2194 if (vref_caps & (1 << i))
2195 n++;
2196 }
2197 return 0;
2198}
2199
2200static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2201 struct snd_ctl_elem_info *uinfo)
2202{
2203 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2204 hda_nid_t nid = kcontrol->private_value;
2205 unsigned int vref_caps = get_vref_caps(codec, nid);
2206
2207 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2208 vref_texts);
2209 /* set the right text */
2210 strcpy(uinfo->value.enumerated.name,
2211 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2212 return 0;
2213}
2214
2215static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2216 struct snd_ctl_elem_value *ucontrol)
2217{
2218 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2219 hda_nid_t nid = kcontrol->private_value;
2220 unsigned int vref_caps = get_vref_caps(codec, nid);
2221 unsigned int idx;
2222
2223 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2224 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2225 return 0;
2226}
2227
2228static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2229 struct snd_ctl_elem_value *ucontrol)
2230{
2231 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2232 hda_nid_t nid = kcontrol->private_value;
2233 unsigned int vref_caps = get_vref_caps(codec, nid);
2234 unsigned int val, idx;
2235
2236 val = snd_hda_codec_get_pin_target(codec, nid);
2237 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2238 if (idx == ucontrol->value.enumerated.item[0])
2239 return 0;
2240
2241 val &= ~AC_PINCTL_VREFEN;
2242 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2243 snd_hda_set_pin_ctl_cache(codec, nid, val);
2244 return 1;
2245}
2246
2247static const struct snd_kcontrol_new in_jack_mode_enum = {
2248 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2249 .info = in_jack_mode_info,
2250 .get = in_jack_mode_get,
2251 .put = in_jack_mode_put,
2252};
2253
2254static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2255{
2256 struct hda_gen_spec *spec = codec->spec;
2257 unsigned int defcfg;
2258 struct snd_kcontrol_new *knew;
2259 char name[44];
2260
2261 /* no jack mode for fixed pins */
2262 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2263 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2264 return 0;
2265
2266 /* no multiple vref caps? */
2267 if (hweight32(get_vref_caps(codec, pin)) <= 1)
2268 return 0;
2269
2270 get_jack_mode_name(codec, pin, name, sizeof(name));
2271 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2272 if (!knew)
2273 return -ENOMEM;
2274 knew->private_value = pin;
2275 return 0;
2276}
2277
Takashi Iwai352f7f92012-12-19 12:52:06 +01002278
2279/*
2280 * Parse input paths
2281 */
2282
2283#ifdef CONFIG_PM
2284/* add the powersave loopback-list entry */
2285static void add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
2286{
2287 struct hda_amp_list *list;
2288
2289 if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
2290 return;
2291 list = spec->loopback_list + spec->num_loopbacks;
2292 list->nid = mix;
2293 list->dir = HDA_INPUT;
2294 list->idx = idx;
2295 spec->num_loopbacks++;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002296 spec->loopback.amplist = spec->loopback_list;
2297}
2298#else
Takashi Iwai352f7f92012-12-19 12:52:06 +01002299#define add_loopback_list(spec, mix, idx) /* NOP */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002300#endif
2301
Takashi Iwai352f7f92012-12-19 12:52:06 +01002302/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01002303static int new_analog_input(struct hda_codec *codec, int input_idx,
2304 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002305 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002307 struct hda_gen_spec *spec = codec->spec;
2308 struct nid_path *path;
2309 unsigned int val;
2310 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
Takashi Iwai352f7f92012-12-19 12:52:06 +01002312 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
2313 !nid_has_mute(codec, mix_nid, HDA_INPUT))
2314 return 0; /* no need for analog loopback */
2315
Takashi Iwai3ca529d2013-01-07 17:25:08 +01002316 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002317 if (!path)
2318 return -EINVAL;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002319 print_nid_path("loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01002320 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002321
2322 idx = path->idx[path->depth - 1];
2323 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
2324 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2325 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002326 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002328 path->ctls[NID_PATH_VOL_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 }
2330
Takashi Iwai352f7f92012-12-19 12:52:06 +01002331 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
2332 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2333 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002334 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002336 path->ctls[NID_PATH_MUTE_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 }
2338
Takashi Iwai352f7f92012-12-19 12:52:06 +01002339 path->active = true;
2340 add_loopback_list(spec, mix_nid, idx);
2341 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342}
2343
Takashi Iwai352f7f92012-12-19 12:52:06 +01002344static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002346 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
2347 return (pincap & AC_PINCAP_IN) != 0;
2348}
2349
2350/* Parse the codec tree and retrieve ADCs */
2351static int fill_adc_nids(struct hda_codec *codec)
2352{
2353 struct hda_gen_spec *spec = codec->spec;
2354 hda_nid_t nid;
2355 hda_nid_t *adc_nids = spec->adc_nids;
2356 int max_nums = ARRAY_SIZE(spec->adc_nids);
2357 int i, nums = 0;
2358
2359 nid = codec->start_nid;
2360 for (i = 0; i < codec->num_nodes; i++, nid++) {
2361 unsigned int caps = get_wcaps(codec, nid);
2362 int type = get_wcaps_type(caps);
2363
2364 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2365 continue;
2366 adc_nids[nums] = nid;
2367 if (++nums >= max_nums)
2368 break;
2369 }
2370 spec->num_adc_nids = nums;
2371 return nums;
2372}
2373
2374/* filter out invalid adc_nids that don't give all active input pins;
2375 * if needed, check whether dynamic ADC-switching is available
2376 */
2377static int check_dyn_adc_switch(struct hda_codec *codec)
2378{
2379 struct hda_gen_spec *spec = codec->spec;
2380 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002381 unsigned int ok_bits;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002382 int i, n, nums;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002383
2384 again:
2385 nums = 0;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002386 ok_bits = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002387 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002388 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002389 if (!spec->input_paths[i][n])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002390 break;
2391 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002392 if (i >= imux->num_items) {
2393 ok_bits |= (1 << n);
2394 nums++;
2395 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002396 }
2397
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002398 if (!ok_bits) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002399 if (spec->shared_mic_hp) {
2400 spec->shared_mic_hp = 0;
2401 imux->num_items = 1;
2402 goto again;
2403 }
2404
2405 /* check whether ADC-switch is possible */
2406 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002407 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002408 if (spec->input_paths[i][n]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002409 spec->dyn_adc_idx[i] = n;
2410 break;
2411 }
2412 }
2413 }
2414
2415 snd_printdd("hda-codec: enabling ADC switching\n");
2416 spec->dyn_adc_switch = 1;
2417 } else if (nums != spec->num_adc_nids) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002418 /* shrink the invalid adcs and input paths */
2419 nums = 0;
2420 for (n = 0; n < spec->num_adc_nids; n++) {
2421 if (!(ok_bits & (1 << n)))
2422 continue;
2423 if (n != nums) {
2424 spec->adc_nids[nums] = spec->adc_nids[n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002425 for (i = 0; i < imux->num_items; i++) {
2426 invalidate_nid_path(codec,
2427 spec->input_paths[i][nums]);
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002428 spec->input_paths[i][nums] =
2429 spec->input_paths[i][n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002430 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002431 }
2432 nums++;
2433 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002434 spec->num_adc_nids = nums;
2435 }
2436
2437 if (imux->num_items == 1 || spec->shared_mic_hp) {
2438 snd_printdd("hda-codec: reducing to a single ADC\n");
2439 spec->num_adc_nids = 1; /* reduce to a single ADC */
2440 }
2441
2442 /* single index for individual volumes ctls */
2443 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
2444 spec->num_adc_nids = 1;
2445
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 return 0;
2447}
2448
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002449/* parse capture source paths from the given pin and create imux items */
2450static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
2451 int num_adcs, const char *label, int anchor)
2452{
2453 struct hda_gen_spec *spec = codec->spec;
2454 struct hda_input_mux *imux = &spec->input_mux;
2455 int imux_idx = imux->num_items;
2456 bool imux_added = false;
2457 int c;
2458
2459 for (c = 0; c < num_adcs; c++) {
2460 struct nid_path *path;
2461 hda_nid_t adc = spec->adc_nids[c];
2462
2463 if (!is_reachable_path(codec, pin, adc))
2464 continue;
2465 path = snd_hda_add_new_path(codec, pin, adc, anchor);
2466 if (!path)
2467 continue;
2468 print_nid_path("input", path);
2469 spec->input_paths[imux_idx][c] =
2470 snd_hda_get_path_idx(codec, path);
2471
2472 if (!imux_added) {
2473 spec->imux_pins[imux->num_items] = pin;
2474 snd_hda_add_imux_item(imux, label,
2475 imux->num_items, NULL);
2476 imux_added = true;
2477 }
2478 }
2479
2480 return 0;
2481}
2482
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002484 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002486static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002487{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002488 struct hda_gen_spec *spec = codec->spec;
2489 const struct auto_pin_cfg *cfg = &spec->autocfg;
2490 hda_nid_t mixer = spec->mixer_nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002491 int num_adcs;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002492 int i, err, type_idx = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002493 const char *prev_label = NULL;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002494 unsigned int val;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002495
Takashi Iwai352f7f92012-12-19 12:52:06 +01002496 num_adcs = fill_adc_nids(codec);
2497 if (num_adcs < 0)
2498 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002499
Takashi Iwai352f7f92012-12-19 12:52:06 +01002500 for (i = 0; i < cfg->num_inputs; i++) {
2501 hda_nid_t pin;
2502 const char *label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503
Takashi Iwai352f7f92012-12-19 12:52:06 +01002504 pin = cfg->inputs[i].pin;
2505 if (!is_input_pin(codec, pin))
2506 continue;
2507
2508 label = hda_get_autocfg_input_label(codec, cfg, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002509 if (prev_label && !strcmp(label, prev_label))
2510 type_idx++;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002511 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01002512 type_idx = 0;
2513 prev_label = label;
2514
Takashi Iwai2c12c302013-01-10 09:33:29 +01002515 val = PIN_IN;
2516 if (cfg->inputs[i].type == AUTO_PIN_MIC)
2517 val |= snd_hda_get_default_vref(codec, pin);
2518 set_pin_target(codec, pin, val, false);
2519
Takashi Iwai352f7f92012-12-19 12:52:06 +01002520 if (mixer) {
2521 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01002522 err = new_analog_input(codec, i, pin,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002523 label, type_idx, mixer);
2524 if (err < 0)
2525 return err;
2526 }
2527 }
2528
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002529 err = parse_capture_source(codec, pin, num_adcs, label, -mixer);
2530 if (err < 0)
2531 return err;
Takashi Iwai294765582013-01-17 09:52:11 +01002532
2533 if (spec->add_in_jack_modes) {
2534 err = create_in_jack_mode(codec, pin);
2535 if (err < 0)
2536 return err;
2537 }
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002538 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002539
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002540 if (mixer && spec->add_stereo_mix_input) {
2541 err = parse_capture_source(codec, mixer, num_adcs,
2542 "Stereo Mix", 0);
2543 if (err < 0)
2544 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002545 }
2546
2547 return 0;
2548}
2549
2550
2551/*
2552 * input source mux
2553 */
2554
Takashi Iwaic697b712013-01-07 17:09:26 +01002555/* get the input path specified by the given adc and imux indices */
2556static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002557{
2558 struct hda_gen_spec *spec = codec->spec;
David Henningssonb56fa1e2013-01-16 11:45:35 +01002559 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
2560 snd_BUG();
2561 return NULL;
2562 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002563 if (spec->dyn_adc_switch)
2564 adc_idx = spec->dyn_adc_idx[imux_idx];
David Henningssonb56fa1e2013-01-16 11:45:35 +01002565 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_OUTS) {
2566 snd_BUG();
2567 return NULL;
2568 }
Takashi Iwaic697b712013-01-07 17:09:26 +01002569 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002570}
2571
2572static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2573 unsigned int idx);
2574
2575static int mux_enum_info(struct snd_kcontrol *kcontrol,
2576 struct snd_ctl_elem_info *uinfo)
2577{
2578 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2579 struct hda_gen_spec *spec = codec->spec;
2580 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
2581}
2582
2583static int mux_enum_get(struct snd_kcontrol *kcontrol,
2584 struct snd_ctl_elem_value *ucontrol)
2585{
2586 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2587 struct hda_gen_spec *spec = codec->spec;
David Henningssona053d1e2013-01-16 11:45:36 +01002588 unsigned int adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002589
2590 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
2591 return 0;
2592}
2593
2594static int mux_enum_put(struct snd_kcontrol *kcontrol,
2595 struct snd_ctl_elem_value *ucontrol)
2596{
2597 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
David Henningssona053d1e2013-01-16 11:45:36 +01002598 unsigned int adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002599 return mux_select(codec, adc_idx,
2600 ucontrol->value.enumerated.item[0]);
2601}
2602
Takashi Iwai352f7f92012-12-19 12:52:06 +01002603static const struct snd_kcontrol_new cap_src_temp = {
2604 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2605 .name = "Input Source",
2606 .info = mux_enum_info,
2607 .get = mux_enum_get,
2608 .put = mux_enum_put,
2609};
2610
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002611/*
2612 * capture volume and capture switch ctls
2613 */
2614
Takashi Iwai352f7f92012-12-19 12:52:06 +01002615typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
2616 struct snd_ctl_elem_value *ucontrol);
2617
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002618/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002619static int cap_put_caller(struct snd_kcontrol *kcontrol,
2620 struct snd_ctl_elem_value *ucontrol,
2621 put_call_t func, int type)
2622{
2623 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2624 struct hda_gen_spec *spec = codec->spec;
2625 const struct hda_input_mux *imux;
2626 struct nid_path *path;
2627 int i, adc_idx, err = 0;
2628
2629 imux = &spec->input_mux;
David Henningssona053d1e2013-01-16 11:45:36 +01002630 adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002631 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002632 /* we use the cache-only update at first since multiple input paths
2633 * may shared the same amp; by updating only caches, the redundant
2634 * writes to hardware can be reduced.
2635 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002636 codec->cached_write = 1;
2637 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01002638 path = get_input_path(codec, adc_idx, i);
2639 if (!path || !path->ctls[type])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002640 continue;
2641 kcontrol->private_value = path->ctls[type];
2642 err = func(kcontrol, ucontrol);
2643 if (err < 0)
2644 goto error;
2645 }
2646 error:
2647 codec->cached_write = 0;
2648 mutex_unlock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002649 snd_hda_codec_flush_amp_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002650 if (err >= 0 && spec->cap_sync_hook)
2651 spec->cap_sync_hook(codec);
2652 return err;
2653}
2654
2655/* capture volume ctl callbacks */
2656#define cap_vol_info snd_hda_mixer_amp_volume_info
2657#define cap_vol_get snd_hda_mixer_amp_volume_get
2658#define cap_vol_tlv snd_hda_mixer_amp_tlv
2659
2660static int cap_vol_put(struct snd_kcontrol *kcontrol,
2661 struct snd_ctl_elem_value *ucontrol)
2662{
2663 return cap_put_caller(kcontrol, ucontrol,
2664 snd_hda_mixer_amp_volume_put,
2665 NID_PATH_VOL_CTL);
2666}
2667
2668static const struct snd_kcontrol_new cap_vol_temp = {
2669 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2670 .name = "Capture Volume",
2671 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
2672 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2673 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
2674 .info = cap_vol_info,
2675 .get = cap_vol_get,
2676 .put = cap_vol_put,
2677 .tlv = { .c = cap_vol_tlv },
2678};
2679
2680/* capture switch ctl callbacks */
2681#define cap_sw_info snd_ctl_boolean_stereo_info
2682#define cap_sw_get snd_hda_mixer_amp_switch_get
2683
2684static int cap_sw_put(struct snd_kcontrol *kcontrol,
2685 struct snd_ctl_elem_value *ucontrol)
2686{
Takashi Iwaiae177c32013-01-14 12:13:06 +01002687 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2688 struct hda_gen_spec *spec = codec->spec;
2689 int ret;
2690
2691 ret = cap_put_caller(kcontrol, ucontrol,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002692 snd_hda_mixer_amp_switch_put,
2693 NID_PATH_MUTE_CTL);
Takashi Iwaiae177c32013-01-14 12:13:06 +01002694 if (ret < 0)
2695 return ret;
2696
2697 if (spec->capture_switch_hook) {
2698 bool enable = (ucontrol->value.integer.value[0] ||
2699 ucontrol->value.integer.value[1]);
2700 spec->capture_switch_hook(codec, enable);
2701 }
2702
2703 return ret;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002704}
2705
2706static const struct snd_kcontrol_new cap_sw_temp = {
2707 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2708 .name = "Capture Switch",
2709 .info = cap_sw_info,
2710 .get = cap_sw_get,
2711 .put = cap_sw_put,
2712};
2713
2714static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
2715{
2716 hda_nid_t nid;
2717 int i, depth;
2718
2719 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
2720 for (depth = 0; depth < 3; depth++) {
2721 if (depth >= path->depth)
2722 return -EINVAL;
2723 i = path->depth - depth - 1;
2724 nid = path->path[i];
2725 if (!path->ctls[NID_PATH_VOL_CTL]) {
2726 if (nid_has_volume(codec, nid, HDA_OUTPUT))
2727 path->ctls[NID_PATH_VOL_CTL] =
2728 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2729 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
2730 int idx = path->idx[i];
2731 if (!depth && codec->single_adc_amp)
2732 idx = 0;
2733 path->ctls[NID_PATH_VOL_CTL] =
2734 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2735 }
2736 }
2737 if (!path->ctls[NID_PATH_MUTE_CTL]) {
2738 if (nid_has_mute(codec, nid, HDA_OUTPUT))
2739 path->ctls[NID_PATH_MUTE_CTL] =
2740 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2741 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
2742 int idx = path->idx[i];
2743 if (!depth && codec->single_adc_amp)
2744 idx = 0;
2745 path->ctls[NID_PATH_MUTE_CTL] =
2746 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2747 }
2748 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01002749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 return 0;
2751}
2752
Takashi Iwai352f7f92012-12-19 12:52:06 +01002753static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002755 struct hda_gen_spec *spec = codec->spec;
2756 struct auto_pin_cfg *cfg = &spec->autocfg;
2757 unsigned int val;
2758 int i;
2759
2760 if (!spec->inv_dmic_split)
2761 return false;
2762 for (i = 0; i < cfg->num_inputs; i++) {
2763 if (cfg->inputs[i].pin != nid)
2764 continue;
2765 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2766 return false;
2767 val = snd_hda_codec_get_pincfg(codec, nid);
2768 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
2769 }
2770 return false;
2771}
2772
2773static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
2774 int idx, bool is_switch, unsigned int ctl,
2775 bool inv_dmic)
2776{
2777 struct hda_gen_spec *spec = codec->spec;
2778 char tmpname[44];
2779 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
2780 const char *sfx = is_switch ? "Switch" : "Volume";
2781 unsigned int chs = inv_dmic ? 1 : 3;
2782 int err;
2783
2784 if (!ctl)
2785 return 0;
2786
2787 if (label)
2788 snprintf(tmpname, sizeof(tmpname),
2789 "%s Capture %s", label, sfx);
2790 else
2791 snprintf(tmpname, sizeof(tmpname),
2792 "Capture %s", sfx);
2793 err = add_control(spec, type, tmpname, idx,
2794 amp_val_replace_channels(ctl, chs));
2795 if (err < 0 || !inv_dmic)
2796 return err;
2797
2798 /* Make independent right kcontrol */
2799 if (label)
2800 snprintf(tmpname, sizeof(tmpname),
2801 "Inverted %s Capture %s", label, sfx);
2802 else
2803 snprintf(tmpname, sizeof(tmpname),
2804 "Inverted Capture %s", sfx);
2805 return add_control(spec, type, tmpname, idx,
2806 amp_val_replace_channels(ctl, 2));
2807}
2808
2809/* create single (and simple) capture volume and switch controls */
2810static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
2811 unsigned int vol_ctl, unsigned int sw_ctl,
2812 bool inv_dmic)
2813{
2814 int err;
2815 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
2816 if (err < 0)
2817 return err;
2818 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
2819 if (err < 0)
2820 return err;
2821 return 0;
2822}
2823
2824/* create bound capture volume and switch controls */
2825static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
2826 unsigned int vol_ctl, unsigned int sw_ctl)
2827{
2828 struct hda_gen_spec *spec = codec->spec;
2829 struct snd_kcontrol_new *knew;
2830
2831 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002832 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002833 if (!knew)
2834 return -ENOMEM;
2835 knew->index = idx;
2836 knew->private_value = vol_ctl;
2837 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2838 }
2839 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002840 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002841 if (!knew)
2842 return -ENOMEM;
2843 knew->index = idx;
2844 knew->private_value = sw_ctl;
2845 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2846 }
2847 return 0;
2848}
2849
2850/* return the vol ctl when used first in the imux list */
2851static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
2852{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002853 struct nid_path *path;
2854 unsigned int ctl;
2855 int i;
2856
Takashi Iwaic697b712013-01-07 17:09:26 +01002857 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002858 if (!path)
2859 return 0;
2860 ctl = path->ctls[type];
2861 if (!ctl)
2862 return 0;
2863 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01002864 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002865 if (path && path->ctls[type] == ctl)
2866 return 0;
2867 }
2868 return ctl;
2869}
2870
2871/* create individual capture volume and switch controls per input */
2872static int create_multi_cap_vol_ctl(struct hda_codec *codec)
2873{
2874 struct hda_gen_spec *spec = codec->spec;
2875 struct hda_input_mux *imux = &spec->input_mux;
2876 int i, err, type, type_idx = 0;
2877 const char *prev_label = NULL;
2878
2879 for (i = 0; i < imux->num_items; i++) {
2880 const char *label;
2881 bool inv_dmic;
2882 label = hda_get_autocfg_input_label(codec, &spec->autocfg, i);
2883 if (prev_label && !strcmp(label, prev_label))
2884 type_idx++;
2885 else
2886 type_idx = 0;
2887 prev_label = label;
2888 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
2889
2890 for (type = 0; type < 2; type++) {
2891 err = add_single_cap_ctl(codec, label, type_idx, type,
2892 get_first_cap_ctl(codec, i, type),
2893 inv_dmic);
2894 if (err < 0)
2895 return err;
2896 }
2897 }
2898 return 0;
2899}
2900
2901static int create_capture_mixers(struct hda_codec *codec)
2902{
2903 struct hda_gen_spec *spec = codec->spec;
2904 struct hda_input_mux *imux = &spec->input_mux;
2905 int i, n, nums, err;
2906
2907 if (spec->dyn_adc_switch)
2908 nums = 1;
2909 else
2910 nums = spec->num_adc_nids;
2911
2912 if (!spec->auto_mic && imux->num_items > 1) {
2913 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01002914 const char *name;
2915 name = nums > 1 ? "Input Source" : "Capture Source";
2916 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002917 if (!knew)
2918 return -ENOMEM;
2919 knew->count = nums;
2920 }
2921
2922 for (n = 0; n < nums; n++) {
2923 bool multi = false;
David Henningsson99a55922013-01-16 15:58:44 +01002924 bool multi_cap_vol = spec->multi_cap_vol;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002925 bool inv_dmic = false;
2926 int vol, sw;
2927
2928 vol = sw = 0;
2929 for (i = 0; i < imux->num_items; i++) {
2930 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01002931 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002932 if (!path)
2933 continue;
2934 parse_capvol_in_path(codec, path);
2935 if (!vol)
2936 vol = path->ctls[NID_PATH_VOL_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01002937 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002938 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01002939 if (!same_amp_caps(codec, vol,
2940 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
2941 multi_cap_vol = true;
2942 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002943 if (!sw)
2944 sw = path->ctls[NID_PATH_MUTE_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01002945 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002946 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01002947 if (!same_amp_caps(codec, sw,
2948 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
2949 multi_cap_vol = true;
2950 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002951 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
2952 inv_dmic = true;
2953 }
2954
2955 if (!multi)
2956 err = create_single_cap_vol_ctl(codec, n, vol, sw,
2957 inv_dmic);
David Henningsson99a55922013-01-16 15:58:44 +01002958 else if (!multi_cap_vol)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002959 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
2960 else
2961 err = create_multi_cap_vol_ctl(codec);
2962 if (err < 0)
2963 return err;
2964 }
2965
2966 return 0;
2967}
2968
2969/*
2970 * add mic boosts if needed
2971 */
2972static int parse_mic_boost(struct hda_codec *codec)
2973{
2974 struct hda_gen_spec *spec = codec->spec;
2975 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002976 int i, err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002977 int type_idx = 0;
2978 hda_nid_t nid;
2979 const char *prev_label = NULL;
2980
2981 for (i = 0; i < cfg->num_inputs; i++) {
2982 if (cfg->inputs[i].type > AUTO_PIN_MIC)
2983 break;
2984 nid = cfg->inputs[i].pin;
2985 if (get_wcaps(codec, nid) & AC_WCAP_IN_AMP) {
2986 const char *label;
Takashi Iwai5abd4882013-01-07 09:43:18 +01002987 char boost_label[44];
Takashi Iwai352f7f92012-12-19 12:52:06 +01002988 struct nid_path *path;
2989 unsigned int val;
2990
David Henningsson02aba552013-01-16 15:58:43 +01002991 if (!nid_has_volume(codec, nid, HDA_INPUT))
2992 continue;
2993
Takashi Iwai352f7f92012-12-19 12:52:06 +01002994 label = hda_get_autocfg_input_label(codec, cfg, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002995 if (prev_label && !strcmp(label, prev_label))
2996 type_idx++;
2997 else
2998 type_idx = 0;
2999 prev_label = label;
3000
3001 snprintf(boost_label, sizeof(boost_label),
3002 "%s Boost Volume", label);
3003 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
3004 err = add_control(spec, HDA_CTL_WIDGET_VOL,
3005 boost_label, type_idx, val);
3006 if (err < 0)
3007 return err;
3008
3009 path = snd_hda_get_nid_path(codec, nid, 0);
3010 if (path)
3011 path->ctls[NID_PATH_BOOST_CTL] = val;
3012 }
3013 }
3014 return 0;
3015}
3016
3017/*
3018 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3019 */
3020static void parse_digital(struct hda_codec *codec)
3021{
3022 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003023 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003024 int i, nums;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003025 hda_nid_t dig_nid, pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003026
3027 /* support multiple SPDIFs; the secondary is set up as a slave */
3028 nums = 0;
3029 for (i = 0; i < spec->autocfg.dig_outs; i++) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003030 pin = spec->autocfg.dig_out_pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003031 dig_nid = look_for_dac(codec, pin, true);
3032 if (!dig_nid)
3033 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003034 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003035 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003036 continue;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003037 print_nid_path("digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01003038 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01003039 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003040 set_pin_target(codec, pin, PIN_OUT, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003041 if (!nums) {
3042 spec->multiout.dig_out_nid = dig_nid;
3043 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3044 } else {
3045 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3046 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
3047 break;
3048 spec->slave_dig_outs[nums - 1] = dig_nid;
3049 }
3050 nums++;
3051 }
3052
3053 if (spec->autocfg.dig_in_pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003054 pin = spec->autocfg.dig_in_pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003055 dig_nid = codec->start_nid;
3056 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003057 unsigned int wcaps = get_wcaps(codec, dig_nid);
3058 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3059 continue;
3060 if (!(wcaps & AC_WCAP_DIGITAL))
3061 continue;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003062 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003063 if (path) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003064 print_nid_path("digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003065 path->active = true;
3066 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003067 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003068 set_pin_target(codec, pin, PIN_IN, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003069 break;
3070 }
3071 }
3072 }
3073}
3074
3075
3076/*
3077 * input MUX handling
3078 */
3079
3080static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3081
3082/* select the given imux item; either unmute exclusively or select the route */
3083static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3084 unsigned int idx)
3085{
3086 struct hda_gen_spec *spec = codec->spec;
3087 const struct hda_input_mux *imux;
3088 struct nid_path *path;
3089
3090 imux = &spec->input_mux;
3091 if (!imux->num_items)
3092 return 0;
3093
3094 if (idx >= imux->num_items)
3095 idx = imux->num_items - 1;
3096 if (spec->cur_mux[adc_idx] == idx)
3097 return 0;
3098
Takashi Iwaic697b712013-01-07 17:09:26 +01003099 path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003100 if (!path)
3101 return 0;
3102 if (path->active)
3103 snd_hda_activate_path(codec, path, false, false);
3104
3105 spec->cur_mux[adc_idx] = idx;
3106
3107 if (spec->shared_mic_hp)
3108 update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
3109
3110 if (spec->dyn_adc_switch)
3111 dyn_adc_pcm_resetup(codec, idx);
3112
Takashi Iwaic697b712013-01-07 17:09:26 +01003113 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003114 if (!path)
3115 return 0;
3116 if (path->active)
3117 return 0;
3118 snd_hda_activate_path(codec, path, true, false);
3119 if (spec->cap_sync_hook)
3120 spec->cap_sync_hook(codec);
3121 return 1;
3122}
3123
3124
3125/*
3126 * Jack detections for HP auto-mute and mic-switch
3127 */
3128
3129/* check each pin in the given array; returns true if any of them is plugged */
3130static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
3131{
3132 int i, present = 0;
3133
3134 for (i = 0; i < num_pins; i++) {
3135 hda_nid_t nid = pins[i];
3136 if (!nid)
3137 break;
Takashi Iwai0b4df932013-01-10 09:45:13 +01003138 /* don't detect pins retasked as inputs */
3139 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
3140 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003141 present |= snd_hda_jack_detect(codec, nid);
3142 }
3143 return present;
3144}
3145
3146/* standard HP/line-out auto-mute helper */
3147static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
Takashi Iwai2c12c302013-01-10 09:33:29 +01003148 bool mute)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003149{
3150 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003151 int i;
3152
3153 for (i = 0; i < num_pins; i++) {
3154 hda_nid_t nid = pins[i];
3155 unsigned int val;
3156 if (!nid)
3157 break;
3158 /* don't reset VREF value in case it's controlling
3159 * the amp (see alc861_fixup_asus_amp_vref_0f())
3160 */
Takashi Iwai2c12c302013-01-10 09:33:29 +01003161 if (spec->keep_vref_in_automute)
3162 val = snd_hda_codec_get_pin_target(codec, nid) & ~PIN_HP;
3163 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01003164 val = 0;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003165 if (!mute)
3166 val |= snd_hda_codec_get_pin_target(codec, nid);
3167 /* here we call update_pin_ctl() so that the pinctl is changed
3168 * without changing the pinctl target value;
3169 * the original target value will be still referred at the
3170 * init / resume again
3171 */
3172 update_pin_ctl(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01003173 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003174 }
3175}
3176
3177/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003178void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003179{
3180 struct hda_gen_spec *spec = codec->spec;
3181 int on;
3182
3183 /* Control HP pins/amps depending on master_mute state;
3184 * in general, HP pins/amps control should be enabled in all cases,
3185 * but currently set only for master_mute, just to be safe
3186 */
3187 if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
3188 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003189 spec->autocfg.hp_pins, spec->master_mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003190
3191 if (!spec->automute_speaker)
3192 on = 0;
3193 else
3194 on = spec->hp_jack_present | spec->line_jack_present;
3195 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003196 spec->speaker_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003197 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003198 spec->autocfg.speaker_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003199
3200 /* toggle line-out mutes if needed, too */
3201 /* if LO is a copy of either HP or Speaker, don't need to handle it */
3202 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
3203 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
3204 return;
3205 if (!spec->automute_lo)
3206 on = 0;
3207 else
3208 on = spec->hp_jack_present;
3209 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003210 spec->line_out_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003211 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003212 spec->autocfg.line_out_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003213}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003214EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003215
3216static void call_update_outputs(struct hda_codec *codec)
3217{
3218 struct hda_gen_spec *spec = codec->spec;
3219 if (spec->automute_hook)
3220 spec->automute_hook(codec);
3221 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01003222 snd_hda_gen_update_outputs(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003223}
3224
3225/* standard HP-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003226void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003227{
3228 struct hda_gen_spec *spec = codec->spec;
3229
3230 spec->hp_jack_present =
3231 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
3232 spec->autocfg.hp_pins);
3233 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
3234 return;
3235 call_update_outputs(codec);
3236}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003237EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003238
3239/* standard line-out-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003240void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003241{
3242 struct hda_gen_spec *spec = codec->spec;
3243
3244 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3245 return;
3246 /* check LO jack only when it's different from HP */
3247 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
3248 return;
3249
3250 spec->line_jack_present =
3251 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3252 spec->autocfg.line_out_pins);
3253 if (!spec->automute_speaker || !spec->detect_lo)
3254 return;
3255 call_update_outputs(codec);
3256}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003257EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003258
3259/* standard mic auto-switch helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003260void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003261{
3262 struct hda_gen_spec *spec = codec->spec;
3263 int i;
3264
3265 if (!spec->auto_mic)
3266 return;
3267
3268 for (i = spec->am_num_entries - 1; i > 0; i--) {
Takashi Iwai0b4df932013-01-10 09:45:13 +01003269 hda_nid_t pin = spec->am_entry[i].pin;
3270 /* don't detect pins retasked as outputs */
3271 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
3272 continue;
3273 if (snd_hda_jack_detect(codec, pin)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003274 mux_select(codec, 0, spec->am_entry[i].idx);
3275 return;
3276 }
3277 }
3278 mux_select(codec, 0, spec->am_entry[0].idx);
3279}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003280EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003281
Takashi Iwaia5cc2502013-01-16 18:08:55 +01003282/* update jack retasking */
3283static void update_automute_all(struct hda_codec *codec)
3284{
3285 struct hda_gen_spec *spec = codec->spec;
3286
3287 if (spec->hp_automute_hook)
3288 spec->hp_automute_hook(codec, NULL);
3289 else
3290 snd_hda_gen_hp_automute(codec, NULL);
3291 if (spec->line_automute_hook)
3292 spec->line_automute_hook(codec, NULL);
3293 else
3294 snd_hda_gen_line_automute(codec, NULL);
3295 if (spec->mic_autoswitch_hook)
3296 spec->mic_autoswitch_hook(codec, NULL);
3297 else
3298 snd_hda_gen_mic_autoswitch(codec, NULL);
3299}
3300
Takashi Iwai352f7f92012-12-19 12:52:06 +01003301/*
3302 * Auto-Mute mode mixer enum support
3303 */
3304static int automute_mode_info(struct snd_kcontrol *kcontrol,
3305 struct snd_ctl_elem_info *uinfo)
3306{
3307 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3308 struct hda_gen_spec *spec = codec->spec;
3309 static const char * const texts3[] = {
3310 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02003311 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312
Takashi Iwai352f7f92012-12-19 12:52:06 +01003313 if (spec->automute_speaker_possible && spec->automute_lo_possible)
3314 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
3315 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
3316}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317
Takashi Iwai352f7f92012-12-19 12:52:06 +01003318static int automute_mode_get(struct snd_kcontrol *kcontrol,
3319 struct snd_ctl_elem_value *ucontrol)
3320{
3321 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3322 struct hda_gen_spec *spec = codec->spec;
3323 unsigned int val = 0;
3324 if (spec->automute_speaker)
3325 val++;
3326 if (spec->automute_lo)
3327 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003328
Takashi Iwai352f7f92012-12-19 12:52:06 +01003329 ucontrol->value.enumerated.item[0] = val;
3330 return 0;
3331}
3332
3333static int automute_mode_put(struct snd_kcontrol *kcontrol,
3334 struct snd_ctl_elem_value *ucontrol)
3335{
3336 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3337 struct hda_gen_spec *spec = codec->spec;
3338
3339 switch (ucontrol->value.enumerated.item[0]) {
3340 case 0:
3341 if (!spec->automute_speaker && !spec->automute_lo)
3342 return 0;
3343 spec->automute_speaker = 0;
3344 spec->automute_lo = 0;
3345 break;
3346 case 1:
3347 if (spec->automute_speaker_possible) {
3348 if (!spec->automute_lo && spec->automute_speaker)
3349 return 0;
3350 spec->automute_speaker = 1;
3351 spec->automute_lo = 0;
3352 } else if (spec->automute_lo_possible) {
3353 if (spec->automute_lo)
3354 return 0;
3355 spec->automute_lo = 1;
3356 } else
3357 return -EINVAL;
3358 break;
3359 case 2:
3360 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
3361 return -EINVAL;
3362 if (spec->automute_speaker && spec->automute_lo)
3363 return 0;
3364 spec->automute_speaker = 1;
3365 spec->automute_lo = 1;
3366 break;
3367 default:
3368 return -EINVAL;
3369 }
3370 call_update_outputs(codec);
3371 return 1;
3372}
3373
3374static const struct snd_kcontrol_new automute_mode_enum = {
3375 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3376 .name = "Auto-Mute Mode",
3377 .info = automute_mode_info,
3378 .get = automute_mode_get,
3379 .put = automute_mode_put,
3380};
3381
3382static int add_automute_mode_enum(struct hda_codec *codec)
3383{
3384 struct hda_gen_spec *spec = codec->spec;
3385
Takashi Iwai12c93df2012-12-19 14:38:33 +01003386 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01003387 return -ENOMEM;
3388 return 0;
3389}
3390
3391/*
3392 * Check the availability of HP/line-out auto-mute;
3393 * Set up appropriately if really supported
3394 */
3395static int check_auto_mute_availability(struct hda_codec *codec)
3396{
3397 struct hda_gen_spec *spec = codec->spec;
3398 struct auto_pin_cfg *cfg = &spec->autocfg;
3399 int present = 0;
3400 int i, err;
3401
Takashi Iwaif72706b2013-01-16 18:20:07 +01003402 if (spec->suppress_auto_mute)
3403 return 0;
3404
Takashi Iwai352f7f92012-12-19 12:52:06 +01003405 if (cfg->hp_pins[0])
3406 present++;
3407 if (cfg->line_out_pins[0])
3408 present++;
3409 if (cfg->speaker_pins[0])
3410 present++;
3411 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02003412 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003413
3414 if (!cfg->speaker_pins[0] &&
3415 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
3416 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3417 sizeof(cfg->speaker_pins));
3418 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420
Takashi Iwai352f7f92012-12-19 12:52:06 +01003421 if (!cfg->hp_pins[0] &&
3422 cfg->line_out_type == AUTO_PIN_HP_OUT) {
3423 memcpy(cfg->hp_pins, cfg->line_out_pins,
3424 sizeof(cfg->hp_pins));
3425 cfg->hp_outs = cfg->line_outs;
3426 }
3427
3428 for (i = 0; i < cfg->hp_outs; i++) {
3429 hda_nid_t nid = cfg->hp_pins[i];
3430 if (!is_jack_detectable(codec, nid))
3431 continue;
3432 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
3433 nid);
3434 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003435 spec->hp_automute_hook ?
3436 spec->hp_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003437 snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003438 spec->detect_hp = 1;
3439 }
3440
3441 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
3442 if (cfg->speaker_outs)
3443 for (i = 0; i < cfg->line_outs; i++) {
3444 hda_nid_t nid = cfg->line_out_pins[i];
3445 if (!is_jack_detectable(codec, nid))
3446 continue;
3447 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
3448 snd_hda_jack_detect_enable_callback(codec, nid,
3449 HDA_GEN_FRONT_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003450 spec->line_automute_hook ?
3451 spec->line_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003452 snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003453 spec->detect_lo = 1;
3454 }
3455 spec->automute_lo_possible = spec->detect_hp;
3456 }
3457
3458 spec->automute_speaker_possible = cfg->speaker_outs &&
3459 (spec->detect_hp || spec->detect_lo);
3460
3461 spec->automute_lo = spec->automute_lo_possible;
3462 spec->automute_speaker = spec->automute_speaker_possible;
3463
3464 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
3465 /* create a control for automute mode */
3466 err = add_automute_mode_enum(codec);
3467 if (err < 0)
3468 return err;
3469 }
3470 return 0;
3471}
3472
Takashi Iwai352f7f92012-12-19 12:52:06 +01003473/* check whether all auto-mic pins are valid; setup indices if OK */
3474static bool auto_mic_check_imux(struct hda_codec *codec)
3475{
3476 struct hda_gen_spec *spec = codec->spec;
3477 const struct hda_input_mux *imux;
3478 int i;
3479
3480 imux = &spec->input_mux;
3481 for (i = 0; i < spec->am_num_entries; i++) {
3482 spec->am_entry[i].idx =
3483 find_idx_in_nid_list(spec->am_entry[i].pin,
3484 spec->imux_pins, imux->num_items);
3485 if (spec->am_entry[i].idx < 0)
3486 return false; /* no corresponding imux */
3487 }
3488
3489 /* we don't need the jack detection for the first pin */
3490 for (i = 1; i < spec->am_num_entries; i++)
3491 snd_hda_jack_detect_enable_callback(codec,
3492 spec->am_entry[i].pin,
3493 HDA_GEN_MIC_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003494 spec->mic_autoswitch_hook ?
3495 spec->mic_autoswitch_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003496 snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003497 return true;
3498}
3499
3500static int compare_attr(const void *ap, const void *bp)
3501{
3502 const struct automic_entry *a = ap;
3503 const struct automic_entry *b = bp;
3504 return (int)(a->attr - b->attr);
3505}
3506
3507/*
3508 * Check the availability of auto-mic switch;
3509 * Set up if really supported
3510 */
3511static int check_auto_mic_availability(struct hda_codec *codec)
3512{
3513 struct hda_gen_spec *spec = codec->spec;
3514 struct auto_pin_cfg *cfg = &spec->autocfg;
3515 unsigned int types;
3516 int i, num_pins;
3517
Takashi Iwaid12daf62013-01-07 16:32:11 +01003518 if (spec->suppress_auto_mic)
3519 return 0;
3520
Takashi Iwai352f7f92012-12-19 12:52:06 +01003521 types = 0;
3522 num_pins = 0;
3523 for (i = 0; i < cfg->num_inputs; i++) {
3524 hda_nid_t nid = cfg->inputs[i].pin;
3525 unsigned int attr;
3526 attr = snd_hda_codec_get_pincfg(codec, nid);
3527 attr = snd_hda_get_input_pin_attr(attr);
3528 if (types & (1 << attr))
3529 return 0; /* already occupied */
3530 switch (attr) {
3531 case INPUT_PIN_ATTR_INT:
3532 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3533 return 0; /* invalid type */
3534 break;
3535 case INPUT_PIN_ATTR_UNUSED:
3536 return 0; /* invalid entry */
3537 default:
3538 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
3539 return 0; /* invalid type */
3540 if (!spec->line_in_auto_switch &&
3541 cfg->inputs[i].type != AUTO_PIN_MIC)
3542 return 0; /* only mic is allowed */
3543 if (!is_jack_detectable(codec, nid))
3544 return 0; /* no unsol support */
3545 break;
3546 }
3547 if (num_pins >= MAX_AUTO_MIC_PINS)
3548 return 0;
3549 types |= (1 << attr);
3550 spec->am_entry[num_pins].pin = nid;
3551 spec->am_entry[num_pins].attr = attr;
3552 num_pins++;
3553 }
3554
3555 if (num_pins < 2)
3556 return 0;
3557
3558 spec->am_num_entries = num_pins;
3559 /* sort the am_entry in the order of attr so that the pin with a
3560 * higher attr will be selected when the jack is plugged.
3561 */
3562 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
3563 compare_attr, NULL);
3564
3565 if (!auto_mic_check_imux(codec))
3566 return 0;
3567
3568 spec->auto_mic = 1;
3569 spec->num_adc_nids = 1;
3570 spec->cur_mux[0] = spec->am_entry[0].idx;
3571 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
3572 spec->am_entry[0].pin,
3573 spec->am_entry[1].pin,
3574 spec->am_entry[2].pin);
3575
3576 return 0;
3577}
3578
3579
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003580/*
3581 * Parse the given BIOS configuration and set up the hda_gen_spec
3582 *
3583 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003584 * or a negative error code
3585 */
3586int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003587 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003588{
3589 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003590 int err;
3591
Takashi Iwai1c70a582013-01-11 17:48:22 +01003592 parse_user_hints(codec);
3593
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003594 if (cfg != &spec->autocfg) {
3595 spec->autocfg = *cfg;
3596 cfg = &spec->autocfg;
3597 }
3598
David Henningsson6fc4cb92013-01-16 15:58:45 +01003599 fill_all_dac_nids(codec);
3600
Takashi Iwai352f7f92012-12-19 12:52:06 +01003601 if (!cfg->line_outs) {
3602 if (cfg->dig_outs || cfg->dig_in_pin) {
3603 spec->multiout.max_channels = 2;
3604 spec->no_analog = 1;
3605 goto dig_only;
3606 }
3607 return 0; /* can't find valid BIOS pin config */
3608 }
3609
3610 if (!spec->no_primary_hp &&
3611 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
3612 cfg->line_outs <= cfg->hp_outs) {
3613 /* use HP as primary out */
3614 cfg->speaker_outs = cfg->line_outs;
3615 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3616 sizeof(cfg->speaker_pins));
3617 cfg->line_outs = cfg->hp_outs;
3618 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
3619 cfg->hp_outs = 0;
3620 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3621 cfg->line_out_type = AUTO_PIN_HP_OUT;
3622 }
3623
3624 err = parse_output_paths(codec);
3625 if (err < 0)
3626 return err;
3627 err = create_multi_channel_mode(codec);
3628 if (err < 0)
3629 return err;
3630 err = create_multi_out_ctls(codec, cfg);
3631 if (err < 0)
3632 return err;
3633 err = create_hp_out_ctls(codec);
3634 if (err < 0)
3635 return err;
3636 err = create_speaker_out_ctls(codec);
3637 if (err < 0)
3638 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003639 err = create_indep_hp_ctls(codec);
3640 if (err < 0)
3641 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01003642 err = create_loopback_mixing_ctl(codec);
3643 if (err < 0)
3644 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003645 err = create_shared_input(codec);
3646 if (err < 0)
3647 return err;
3648 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003649 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02003650 return err;
3651
Takashi Iwaia07a9492013-01-07 16:44:06 +01003652 spec->const_channel_count = spec->ext_channel_count;
3653 /* check the multiple speaker and headphone pins */
3654 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
3655 spec->const_channel_count = max(spec->const_channel_count,
3656 cfg->speaker_outs * 2);
3657 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
3658 spec->const_channel_count = max(spec->const_channel_count,
3659 cfg->hp_outs * 2);
3660 spec->multiout.max_channels = max(spec->ext_channel_count,
3661 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003662
3663 err = check_auto_mute_availability(codec);
3664 if (err < 0)
3665 return err;
3666
3667 err = check_dyn_adc_switch(codec);
3668 if (err < 0)
3669 return err;
3670
3671 if (!spec->shared_mic_hp) {
3672 err = check_auto_mic_availability(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003673 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 }
Takashi Iwai071c73a2006-08-23 18:34:06 +02003676
Takashi Iwai352f7f92012-12-19 12:52:06 +01003677 err = create_capture_mixers(codec);
3678 if (err < 0)
3679 return err;
3680
3681 err = parse_mic_boost(codec);
3682 if (err < 0)
3683 return err;
3684
Takashi Iwai978e77e2013-01-10 16:57:58 +01003685 if (spec->add_out_jack_modes) {
3686 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
3687 err = create_out_jack_modes(codec, cfg->line_outs,
3688 cfg->line_out_pins);
3689 if (err < 0)
3690 return err;
3691 }
3692 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
3693 err = create_out_jack_modes(codec, cfg->hp_outs,
3694 cfg->hp_pins);
3695 if (err < 0)
3696 return err;
3697 }
3698 }
3699
Takashi Iwai352f7f92012-12-19 12:52:06 +01003700 dig_only:
3701 parse_digital(codec);
3702
3703 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003705EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706
3707
3708/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003709 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003711
3712/* slave controls for virtual master */
3713static const char * const slave_pfxs[] = {
3714 "Front", "Surround", "Center", "LFE", "Side",
3715 "Headphone", "Speaker", "Mono", "Line Out",
3716 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01003717 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
3718 "Headphone Front", "Headphone Surround", "Headphone CLFE",
3719 "Headphone Side",
Takashi Iwai352f7f92012-12-19 12:52:06 +01003720 NULL,
3721};
3722
3723int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003725 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727
Takashi Iwai36502d02012-12-19 15:15:10 +01003728 if (spec->kctls.used) {
3729 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
3730 if (err < 0)
3731 return err;
3732 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733
Takashi Iwai352f7f92012-12-19 12:52:06 +01003734 if (spec->multiout.dig_out_nid) {
3735 err = snd_hda_create_dig_out_ctls(codec,
3736 spec->multiout.dig_out_nid,
3737 spec->multiout.dig_out_nid,
3738 spec->pcm_rec[1].pcm_type);
3739 if (err < 0)
3740 return err;
3741 if (!spec->no_analog) {
3742 err = snd_hda_create_spdif_share_sw(codec,
3743 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 if (err < 0)
3745 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003746 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 }
3748 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003749 if (spec->dig_in_nid) {
3750 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
3751 if (err < 0)
3752 return err;
3753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754
Takashi Iwai352f7f92012-12-19 12:52:06 +01003755 /* if we have no master control, let's create it */
3756 if (!spec->no_analog &&
3757 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003758 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01003759 spec->vmaster_tlv, slave_pfxs,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003760 "Playback Volume");
3761 if (err < 0)
3762 return err;
3763 }
3764 if (!spec->no_analog &&
3765 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
3766 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
3767 NULL, slave_pfxs,
3768 "Playback Switch",
3769 true, &spec->vmaster_mute.sw_kctl);
3770 if (err < 0)
3771 return err;
3772 if (spec->vmaster_mute.hook)
Takashi Iwaifd25a972012-12-20 14:57:18 +01003773 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
3774 spec->vmaster_mute_enum);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776
Takashi Iwai352f7f92012-12-19 12:52:06 +01003777 free_kctls(spec); /* no longer needed */
3778
3779 if (spec->shared_mic_hp) {
3780 int err;
3781 int nid = spec->autocfg.inputs[1].pin;
3782 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
3783 if (err < 0)
3784 return err;
3785 err = snd_hda_jack_detect_enable(codec, nid, 0);
3786 if (err < 0)
3787 return err;
3788 }
3789
3790 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
3791 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 return err;
3793
3794 return 0;
3795}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003796EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
3797
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798
3799/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003800 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003803static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
3804 struct hda_codec *codec,
3805 struct snd_pcm_substream *substream,
3806 int action)
3807{
3808 struct hda_gen_spec *spec = codec->spec;
3809 if (spec->pcm_playback_hook)
3810 spec->pcm_playback_hook(hinfo, codec, substream, action);
3811}
3812
Takashi Iwai352f7f92012-12-19 12:52:06 +01003813/*
3814 * Analog playback callbacks
3815 */
3816static int playback_pcm_open(struct hda_pcm_stream *hinfo,
3817 struct hda_codec *codec,
3818 struct snd_pcm_substream *substream)
3819{
3820 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003821 int err;
3822
3823 mutex_lock(&spec->pcm_mutex);
3824 err = snd_hda_multi_out_analog_open(codec,
3825 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003826 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003827 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003828 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003829 call_pcm_playback_hook(hinfo, codec, substream,
3830 HDA_GEN_PCM_ACT_OPEN);
3831 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003832 mutex_unlock(&spec->pcm_mutex);
3833 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003834}
3835
3836static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01003837 struct hda_codec *codec,
3838 unsigned int stream_tag,
3839 unsigned int format,
3840 struct snd_pcm_substream *substream)
3841{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003842 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003843 int err;
3844
3845 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
3846 stream_tag, format, substream);
3847 if (!err)
3848 call_pcm_playback_hook(hinfo, codec, substream,
3849 HDA_GEN_PCM_ACT_PREPARE);
3850 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003851}
Takashi Iwai97ec5582006-03-21 11:29:07 +01003852
Takashi Iwai352f7f92012-12-19 12:52:06 +01003853static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3854 struct hda_codec *codec,
3855 struct snd_pcm_substream *substream)
3856{
3857 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003858 int err;
3859
3860 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
3861 if (!err)
3862 call_pcm_playback_hook(hinfo, codec, substream,
3863 HDA_GEN_PCM_ACT_CLEANUP);
3864 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003865}
3866
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003867static int playback_pcm_close(struct hda_pcm_stream *hinfo,
3868 struct hda_codec *codec,
3869 struct snd_pcm_substream *substream)
3870{
3871 struct hda_gen_spec *spec = codec->spec;
3872 mutex_lock(&spec->pcm_mutex);
3873 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003874 call_pcm_playback_hook(hinfo, codec, substream,
3875 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003876 mutex_unlock(&spec->pcm_mutex);
3877 return 0;
3878}
3879
3880static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
3881 struct hda_codec *codec,
3882 struct snd_pcm_substream *substream)
3883{
3884 struct hda_gen_spec *spec = codec->spec;
3885 int err = 0;
3886
3887 mutex_lock(&spec->pcm_mutex);
3888 if (!spec->indep_hp_enabled)
3889 err = -EBUSY;
3890 else
3891 spec->active_streams |= 1 << STREAM_INDEP_HP;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003892 call_pcm_playback_hook(hinfo, codec, substream,
3893 HDA_GEN_PCM_ACT_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003894 mutex_unlock(&spec->pcm_mutex);
3895 return err;
3896}
3897
3898static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
3899 struct hda_codec *codec,
3900 struct snd_pcm_substream *substream)
3901{
3902 struct hda_gen_spec *spec = codec->spec;
3903 mutex_lock(&spec->pcm_mutex);
3904 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003905 call_pcm_playback_hook(hinfo, codec, substream,
3906 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003907 mutex_unlock(&spec->pcm_mutex);
3908 return 0;
3909}
3910
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003911static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3912 struct hda_codec *codec,
3913 unsigned int stream_tag,
3914 unsigned int format,
3915 struct snd_pcm_substream *substream)
3916{
3917 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3918 call_pcm_playback_hook(hinfo, codec, substream,
3919 HDA_GEN_PCM_ACT_PREPARE);
3920 return 0;
3921}
3922
3923static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3924 struct hda_codec *codec,
3925 struct snd_pcm_substream *substream)
3926{
3927 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
3928 call_pcm_playback_hook(hinfo, codec, substream,
3929 HDA_GEN_PCM_ACT_CLEANUP);
3930 return 0;
3931}
3932
Takashi Iwai352f7f92012-12-19 12:52:06 +01003933/*
3934 * Digital out
3935 */
3936static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
3937 struct hda_codec *codec,
3938 struct snd_pcm_substream *substream)
3939{
3940 struct hda_gen_spec *spec = codec->spec;
3941 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3942}
3943
3944static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3945 struct hda_codec *codec,
3946 unsigned int stream_tag,
3947 unsigned int format,
3948 struct snd_pcm_substream *substream)
3949{
3950 struct hda_gen_spec *spec = codec->spec;
3951 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3952 stream_tag, format, substream);
3953}
3954
3955static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3956 struct hda_codec *codec,
3957 struct snd_pcm_substream *substream)
3958{
3959 struct hda_gen_spec *spec = codec->spec;
3960 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
3961}
3962
3963static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
3964 struct hda_codec *codec,
3965 struct snd_pcm_substream *substream)
3966{
3967 struct hda_gen_spec *spec = codec->spec;
3968 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3969}
3970
3971/*
3972 * Analog capture
3973 */
3974static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3975 struct hda_codec *codec,
3976 unsigned int stream_tag,
3977 unsigned int format,
3978 struct snd_pcm_substream *substream)
3979{
3980 struct hda_gen_spec *spec = codec->spec;
3981
3982 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01003983 stream_tag, 0, format);
3984 return 0;
3985}
3986
Takashi Iwai352f7f92012-12-19 12:52:06 +01003987static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3988 struct hda_codec *codec,
3989 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01003990{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003991 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01003992
Takashi Iwai352f7f92012-12-19 12:52:06 +01003993 snd_hda_codec_cleanup_stream(codec,
3994 spec->adc_nids[substream->number + 1]);
Takashi Iwai97ec5582006-03-21 11:29:07 +01003995 return 0;
3996}
3997
Takashi Iwai352f7f92012-12-19 12:52:06 +01003998/*
3999 */
4000static const struct hda_pcm_stream pcm_analog_playback = {
4001 .substreams = 1,
4002 .channels_min = 2,
4003 .channels_max = 8,
4004 /* NID is set in build_pcms */
4005 .ops = {
4006 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004007 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004008 .prepare = playback_pcm_prepare,
4009 .cleanup = playback_pcm_cleanup
4010 },
4011};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012
Takashi Iwai352f7f92012-12-19 12:52:06 +01004013static const struct hda_pcm_stream pcm_analog_capture = {
4014 .substreams = 1,
4015 .channels_min = 2,
4016 .channels_max = 2,
4017 /* NID is set in build_pcms */
4018};
4019
4020static const struct hda_pcm_stream pcm_analog_alt_playback = {
4021 .substreams = 1,
4022 .channels_min = 2,
4023 .channels_max = 2,
4024 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004025 .ops = {
4026 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004027 .close = alt_playback_pcm_close,
4028 .prepare = alt_playback_pcm_prepare,
4029 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004030 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004031};
4032
4033static const struct hda_pcm_stream pcm_analog_alt_capture = {
4034 .substreams = 2, /* can be overridden */
4035 .channels_min = 2,
4036 .channels_max = 2,
4037 /* NID is set in build_pcms */
4038 .ops = {
4039 .prepare = alt_capture_pcm_prepare,
4040 .cleanup = alt_capture_pcm_cleanup
4041 },
4042};
4043
4044static const struct hda_pcm_stream pcm_digital_playback = {
4045 .substreams = 1,
4046 .channels_min = 2,
4047 .channels_max = 2,
4048 /* NID is set in build_pcms */
4049 .ops = {
4050 .open = dig_playback_pcm_open,
4051 .close = dig_playback_pcm_close,
4052 .prepare = dig_playback_pcm_prepare,
4053 .cleanup = dig_playback_pcm_cleanup
4054 },
4055};
4056
4057static const struct hda_pcm_stream pcm_digital_capture = {
4058 .substreams = 1,
4059 .channels_min = 2,
4060 .channels_max = 2,
4061 /* NID is set in build_pcms */
4062};
4063
4064/* Used by build_pcms to flag that a PCM has no playback stream */
4065static const struct hda_pcm_stream pcm_null_stream = {
4066 .substreams = 0,
4067 .channels_min = 0,
4068 .channels_max = 0,
4069};
4070
4071/*
4072 * dynamic changing ADC PCM streams
4073 */
4074static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
4075{
4076 struct hda_gen_spec *spec = codec->spec;
4077 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
4078
4079 if (spec->cur_adc && spec->cur_adc != new_adc) {
4080 /* stream is running, let's swap the current ADC */
4081 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
4082 spec->cur_adc = new_adc;
4083 snd_hda_codec_setup_stream(codec, new_adc,
4084 spec->cur_adc_stream_tag, 0,
4085 spec->cur_adc_format);
4086 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004088 return false;
4089}
4090
4091/* analog capture with dynamic dual-adc changes */
4092static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4093 struct hda_codec *codec,
4094 unsigned int stream_tag,
4095 unsigned int format,
4096 struct snd_pcm_substream *substream)
4097{
4098 struct hda_gen_spec *spec = codec->spec;
4099 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
4100 spec->cur_adc_stream_tag = stream_tag;
4101 spec->cur_adc_format = format;
4102 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
4103 return 0;
4104}
4105
4106static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4107 struct hda_codec *codec,
4108 struct snd_pcm_substream *substream)
4109{
4110 struct hda_gen_spec *spec = codec->spec;
4111 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
4112 spec->cur_adc = 0;
4113 return 0;
4114}
4115
4116static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
4117 .substreams = 1,
4118 .channels_min = 2,
4119 .channels_max = 2,
4120 .nid = 0, /* fill later */
4121 .ops = {
4122 .prepare = dyn_adc_capture_pcm_prepare,
4123 .cleanup = dyn_adc_capture_pcm_cleanup
4124 },
4125};
4126
Takashi Iwaif873e532012-12-20 16:58:39 +01004127static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
4128 const char *chip_name)
4129{
4130 char *p;
4131
4132 if (*str)
4133 return;
4134 strlcpy(str, chip_name, len);
4135
4136 /* drop non-alnum chars after a space */
4137 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
4138 if (!isalnum(p[1])) {
4139 *p = 0;
4140 break;
4141 }
4142 }
4143 strlcat(str, sfx, len);
4144}
4145
Takashi Iwai352f7f92012-12-19 12:52:06 +01004146/* build PCM streams based on the parsed results */
4147int snd_hda_gen_build_pcms(struct hda_codec *codec)
4148{
4149 struct hda_gen_spec *spec = codec->spec;
4150 struct hda_pcm *info = spec->pcm_rec;
4151 const struct hda_pcm_stream *p;
4152 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153
4154 codec->num_pcms = 1;
4155 codec->pcm_info = info;
4156
Takashi Iwai352f7f92012-12-19 12:52:06 +01004157 if (spec->no_analog)
4158 goto skip_analog;
4159
Takashi Iwaif873e532012-12-20 16:58:39 +01004160 fill_pcm_stream_name(spec->stream_name_analog,
4161 sizeof(spec->stream_name_analog),
4162 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004163 info->name = spec->stream_name_analog;
4164
4165 if (spec->multiout.num_dacs > 0) {
4166 p = spec->stream_analog_playback;
4167 if (!p)
4168 p = &pcm_analog_playback;
4169 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4170 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
4171 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
4172 spec->multiout.max_channels;
4173 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
4174 spec->autocfg.line_outs == 2)
4175 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
4176 snd_pcm_2_1_chmaps;
4177 }
4178 if (spec->num_adc_nids) {
4179 p = spec->stream_analog_capture;
4180 if (!p) {
4181 if (spec->dyn_adc_switch)
4182 p = &dyn_adc_pcm_analog_capture;
4183 else
4184 p = &pcm_analog_capture;
4185 }
4186 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4187 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
4188 }
4189
Takashi Iwai352f7f92012-12-19 12:52:06 +01004190 skip_analog:
4191 /* SPDIF for stream index #1 */
4192 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01004193 fill_pcm_stream_name(spec->stream_name_digital,
4194 sizeof(spec->stream_name_digital),
4195 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004196 codec->num_pcms = 2;
4197 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
4198 info = spec->pcm_rec + 1;
4199 info->name = spec->stream_name_digital;
4200 if (spec->dig_out_type)
4201 info->pcm_type = spec->dig_out_type;
4202 else
4203 info->pcm_type = HDA_PCM_TYPE_SPDIF;
4204 if (spec->multiout.dig_out_nid) {
4205 p = spec->stream_digital_playback;
4206 if (!p)
4207 p = &pcm_digital_playback;
4208 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4209 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
4210 }
4211 if (spec->dig_in_nid) {
4212 p = spec->stream_digital_capture;
4213 if (!p)
4214 p = &pcm_digital_capture;
4215 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4216 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
4217 }
4218 }
4219
4220 if (spec->no_analog)
4221 return 0;
4222
4223 /* If the use of more than one ADC is requested for the current
4224 * model, configure a second analog capture-only PCM.
4225 */
4226 have_multi_adcs = (spec->num_adc_nids > 1) &&
4227 !spec->dyn_adc_switch && !spec->auto_mic;
4228 /* Additional Analaog capture for index #2 */
4229 if (spec->alt_dac_nid || have_multi_adcs) {
4230 codec->num_pcms = 3;
4231 info = spec->pcm_rec + 2;
4232 info->name = spec->stream_name_analog;
4233 if (spec->alt_dac_nid) {
4234 p = spec->stream_analog_alt_playback;
4235 if (!p)
4236 p = &pcm_analog_alt_playback;
4237 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4238 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
4239 spec->alt_dac_nid;
4240 } else {
4241 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
4242 pcm_null_stream;
4243 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
4244 }
4245 if (have_multi_adcs) {
4246 p = spec->stream_analog_alt_capture;
4247 if (!p)
4248 p = &pcm_analog_alt_capture;
4249 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4250 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
4251 spec->adc_nids[1];
4252 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
4253 spec->num_adc_nids - 1;
4254 } else {
4255 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
4256 pcm_null_stream;
4257 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
4258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 }
4260
4261 return 0;
4262}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004263EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
4264
4265
4266/*
4267 * Standard auto-parser initializations
4268 */
4269
Takashi Iwaid4156932013-01-07 10:08:02 +01004270/* configure the given path as a proper output */
Takashi Iwai2c12c302013-01-10 09:33:29 +01004271static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004272{
4273 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01004274 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004275
Takashi Iwai196c17662013-01-04 15:01:40 +01004276 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01004277 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004278 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01004279 pin = path->path[path->depth - 1];
Takashi Iwai2c12c302013-01-10 09:33:29 +01004280 restore_pin_ctl(codec, pin);
Takashi Iwaie1284af2013-01-03 16:33:02 +01004281 snd_hda_activate_path(codec, path, path->active, true);
4282 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004283}
4284
4285/* initialize primary output paths */
4286static void init_multi_out(struct hda_codec *codec)
4287{
4288 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004289 int i;
4290
Takashi Iwaid4156932013-01-07 10:08:02 +01004291 for (i = 0; i < spec->autocfg.line_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004292 set_output_and_unmute(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004293}
4294
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004295
Takashi Iwai2c12c302013-01-10 09:33:29 +01004296static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004297{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004298 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004299
Takashi Iwaid4156932013-01-07 10:08:02 +01004300 for (i = 0; i < num_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004301 set_output_and_unmute(codec, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004302}
4303
4304/* initialize hp and speaker paths */
4305static void init_extra_out(struct hda_codec *codec)
4306{
4307 struct hda_gen_spec *spec = codec->spec;
4308
4309 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004310 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004311 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
4312 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai2c12c302013-01-10 09:33:29 +01004313 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004314}
4315
4316/* initialize multi-io paths */
4317static void init_multi_io(struct hda_codec *codec)
4318{
4319 struct hda_gen_spec *spec = codec->spec;
4320 int i;
4321
4322 for (i = 0; i < spec->multi_ios; i++) {
4323 hda_nid_t pin = spec->multi_io[i].pin;
4324 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01004325 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004326 if (!path)
4327 continue;
4328 if (!spec->multi_io[i].ctl_in)
4329 spec->multi_io[i].ctl_in =
Takashi Iwai2c12c302013-01-10 09:33:29 +01004330 snd_hda_codec_get_pin_target(codec, pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004331 snd_hda_activate_path(codec, path, path->active, true);
4332 }
4333}
4334
Takashi Iwai352f7f92012-12-19 12:52:06 +01004335/* set up input pins and loopback paths */
4336static void init_analog_input(struct hda_codec *codec)
4337{
4338 struct hda_gen_spec *spec = codec->spec;
4339 struct auto_pin_cfg *cfg = &spec->autocfg;
4340 int i;
4341
4342 for (i = 0; i < cfg->num_inputs; i++) {
4343 hda_nid_t nid = cfg->inputs[i].pin;
4344 if (is_input_pin(codec, nid))
Takashi Iwai2c12c302013-01-10 09:33:29 +01004345 restore_pin_ctl(codec, nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004346
4347 /* init loopback inputs */
4348 if (spec->mixer_nid) {
4349 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01004350 path = snd_hda_get_path_from_idx(codec, spec->loopback_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004351 if (path)
4352 snd_hda_activate_path(codec, path,
4353 path->active, false);
4354 }
4355 }
4356}
4357
4358/* initialize ADC paths */
4359static void init_input_src(struct hda_codec *codec)
4360{
4361 struct hda_gen_spec *spec = codec->spec;
4362 struct hda_input_mux *imux = &spec->input_mux;
4363 struct nid_path *path;
4364 int i, c, nums;
4365
4366 if (spec->dyn_adc_switch)
4367 nums = 1;
4368 else
4369 nums = spec->num_adc_nids;
4370
4371 for (c = 0; c < nums; c++) {
4372 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01004373 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004374 if (path) {
4375 bool active = path->active;
4376 if (i == spec->cur_mux[c])
4377 active = true;
4378 snd_hda_activate_path(codec, path, active, false);
4379 }
4380 }
4381 }
4382
4383 if (spec->shared_mic_hp)
4384 update_shared_mic_hp(codec, spec->cur_mux[0]);
4385
4386 if (spec->cap_sync_hook)
4387 spec->cap_sync_hook(codec);
4388}
4389
4390/* set right pin controls for digital I/O */
4391static void init_digital(struct hda_codec *codec)
4392{
4393 struct hda_gen_spec *spec = codec->spec;
4394 int i;
4395 hda_nid_t pin;
4396
Takashi Iwaid4156932013-01-07 10:08:02 +01004397 for (i = 0; i < spec->autocfg.dig_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004398 set_output_and_unmute(codec, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004399 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01004400 if (pin) {
4401 struct nid_path *path;
Takashi Iwai2c12c302013-01-10 09:33:29 +01004402 restore_pin_ctl(codec, pin);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01004403 path = snd_hda_get_path_from_idx(codec, spec->digin_path);
4404 if (path)
4405 snd_hda_activate_path(codec, path, path->active, false);
4406 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004407}
4408
Takashi Iwai973e4972012-12-20 15:16:09 +01004409/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
4410 * invalid unsol tags by some reason
4411 */
4412static void clear_unsol_on_unused_pins(struct hda_codec *codec)
4413{
4414 int i;
4415
4416 for (i = 0; i < codec->init_pins.used; i++) {
4417 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
4418 hda_nid_t nid = pin->nid;
4419 if (is_jack_detectable(codec, nid) &&
4420 !snd_hda_jack_tbl_get(codec, nid))
4421 snd_hda_codec_update_cache(codec, nid, 0,
4422 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
4423 }
4424}
4425
Takashi Iwai5187ac12013-01-07 12:52:16 +01004426/*
4427 * initialize the generic spec;
4428 * this can be put as patch_ops.init function
4429 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004430int snd_hda_gen_init(struct hda_codec *codec)
4431{
4432 struct hda_gen_spec *spec = codec->spec;
4433
4434 if (spec->init_hook)
4435 spec->init_hook(codec);
4436
4437 snd_hda_apply_verbs(codec);
4438
Takashi Iwai3bbcd272012-12-20 11:50:58 +01004439 codec->cached_write = 1;
4440
Takashi Iwai352f7f92012-12-19 12:52:06 +01004441 init_multi_out(codec);
4442 init_extra_out(codec);
4443 init_multi_io(codec);
4444 init_analog_input(codec);
4445 init_input_src(codec);
4446 init_digital(codec);
4447
Takashi Iwai973e4972012-12-20 15:16:09 +01004448 clear_unsol_on_unused_pins(codec);
4449
Takashi Iwai352f7f92012-12-19 12:52:06 +01004450 /* call init functions of standard auto-mute helpers */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01004451 update_automute_all(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004452
Takashi Iwai3bbcd272012-12-20 11:50:58 +01004453 snd_hda_codec_flush_amp_cache(codec);
4454 snd_hda_codec_flush_cmd_cache(codec);
4455
Takashi Iwai352f7f92012-12-19 12:52:06 +01004456 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
4457 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
4458
4459 hda_call_check_power_status(codec, 0x01);
4460 return 0;
4461}
Takashi Iwaifce52a32013-01-07 12:42:48 +01004462EXPORT_SYMBOL_HDA(snd_hda_gen_init);
4463
Takashi Iwai5187ac12013-01-07 12:52:16 +01004464/*
4465 * free the generic spec;
4466 * this can be put as patch_ops.free function
4467 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01004468void snd_hda_gen_free(struct hda_codec *codec)
4469{
4470 snd_hda_gen_spec_free(codec->spec);
4471 kfree(codec->spec);
4472 codec->spec = NULL;
4473}
4474EXPORT_SYMBOL_HDA(snd_hda_gen_free);
4475
4476#ifdef CONFIG_PM
Takashi Iwai5187ac12013-01-07 12:52:16 +01004477/*
4478 * check the loopback power save state;
4479 * this can be put as patch_ops.check_power_status function
4480 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01004481int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
4482{
4483 struct hda_gen_spec *spec = codec->spec;
4484 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
4485}
4486EXPORT_SYMBOL_HDA(snd_hda_gen_check_power_status);
4487#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01004488
4489
4490/*
4491 * the generic codec support
4492 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493
Takashi Iwai352f7f92012-12-19 12:52:06 +01004494static const struct hda_codec_ops generic_patch_ops = {
4495 .build_controls = snd_hda_gen_build_controls,
4496 .build_pcms = snd_hda_gen_build_pcms,
4497 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01004498 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004499 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02004500#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01004501 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02004502#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503};
4504
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505int snd_hda_parse_generic_codec(struct hda_codec *codec)
4506{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004507 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 int err;
4509
Takashi Iwaie560d8d2005-09-09 14:21:46 +02004510 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004511 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004513 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004516 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
4517 if (err < 0)
4518 return err;
4519
4520 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004521 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522 goto error;
4523
4524 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525 return 0;
4526
Takashi Iwai352f7f92012-12-19 12:52:06 +01004527error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01004528 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 return err;
4530}
Takashi Iwaifce52a32013-01-07 12:42:48 +01004531EXPORT_SYMBOL_HDA(snd_hda_parse_generic_codec);