blob: 878556b8ea32a347c8ca67b0ce9b8d6461983699 [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);
1604 }
1605
1606 kfree(best_cfg);
1607 return 0;
1608}
1609
1610/* add playback controls from the parsed DAC table */
1611static int create_multi_out_ctls(struct hda_codec *codec,
1612 const struct auto_pin_cfg *cfg)
1613{
1614 struct hda_gen_spec *spec = codec->spec;
1615 int i, err, noutputs;
1616
1617 noutputs = cfg->line_outs;
1618 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1619 noutputs += spec->multi_ios;
1620
1621 for (i = 0; i < noutputs; i++) {
1622 const char *name;
1623 int index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001624 struct nid_path *path;
1625
Takashi Iwai352f7f92012-12-19 12:52:06 +01001626 if (i >= cfg->line_outs) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001627 index = 0;
1628 name = channel_name[i];
1629 } else {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001630 name = get_line_out_pfx(spec, i, true, &index);
1631 }
1632
Takashi Iwai196c17662013-01-04 15:01:40 +01001633 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001634 if (!path)
1635 continue;
1636 if (!name || !strcmp(name, "CLFE")) {
1637 /* Center/LFE */
1638 err = add_vol_ctl(codec, "Center", 0, 1, path);
1639 if (err < 0)
1640 return err;
1641 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1642 if (err < 0)
1643 return err;
1644 err = add_sw_ctl(codec, "Center", 0, 1, path);
1645 if (err < 0)
1646 return err;
1647 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1648 if (err < 0)
1649 return err;
1650 } else {
1651 err = add_stereo_vol(codec, name, index, path);
1652 if (err < 0)
1653 return err;
1654 err = add_stereo_sw(codec, name, index, path);
1655 if (err < 0)
1656 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 }
1658 }
1659 return 0;
1660}
1661
Takashi Iwaic2c80382013-01-07 10:33:57 +01001662static int create_extra_out(struct hda_codec *codec, int path_idx,
Takashi Iwai196c17662013-01-04 15:01:40 +01001663 const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001665 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 int err;
1667
Takashi Iwai196c17662013-01-04 15:01:40 +01001668 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001669 if (!path)
1670 return 0;
Takashi Iwaic2c80382013-01-07 10:33:57 +01001671 err = add_stereo_vol(codec, pfx, cidx, path);
1672 if (err < 0)
1673 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001674 err = add_stereo_sw(codec, pfx, cidx, path);
1675 if (err < 0)
1676 return err;
1677 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678}
1679
Takashi Iwai352f7f92012-12-19 12:52:06 +01001680/* add playback controls for speaker and HP outputs */
1681static int create_extra_outs(struct hda_codec *codec, int num_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001682 const int *paths, const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683{
Takashi Iwaic2c80382013-01-07 10:33:57 +01001684 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001685
1686 for (i = 0; i < num_pins; i++) {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001687 const char *name;
1688 char tmp[44];
1689 int err, idx = 0;
1690
1691 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
1692 name = "Bass Speaker";
1693 else if (num_pins >= 3) {
1694 snprintf(tmp, sizeof(tmp), "%s %s",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001695 pfx, channel_name[i]);
Takashi Iwaic2c80382013-01-07 10:33:57 +01001696 name = tmp;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001697 } else {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001698 name = pfx;
1699 idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 }
Takashi Iwaic2c80382013-01-07 10:33:57 +01001701 err = create_extra_out(codec, paths[i], name, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001702 if (err < 0)
1703 return err;
1704 }
1705 return 0;
1706}
Takashi Iwai97ec5582006-03-21 11:29:07 +01001707
Takashi Iwai352f7f92012-12-19 12:52:06 +01001708static int create_hp_out_ctls(struct hda_codec *codec)
1709{
1710 struct hda_gen_spec *spec = codec->spec;
1711 return create_extra_outs(codec, spec->autocfg.hp_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001712 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001713 "Headphone");
1714}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
Takashi Iwai352f7f92012-12-19 12:52:06 +01001716static int create_speaker_out_ctls(struct hda_codec *codec)
1717{
1718 struct hda_gen_spec *spec = codec->spec;
1719 return create_extra_outs(codec, spec->autocfg.speaker_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001720 spec->speaker_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001721 "Speaker");
1722}
1723
1724/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001725 * independent HP controls
1726 */
1727
1728static int indep_hp_info(struct snd_kcontrol *kcontrol,
1729 struct snd_ctl_elem_info *uinfo)
1730{
1731 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
1732}
1733
1734static int indep_hp_get(struct snd_kcontrol *kcontrol,
1735 struct snd_ctl_elem_value *ucontrol)
1736{
1737 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1738 struct hda_gen_spec *spec = codec->spec;
1739 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
1740 return 0;
1741}
1742
1743static int indep_hp_put(struct snd_kcontrol *kcontrol,
1744 struct snd_ctl_elem_value *ucontrol)
1745{
1746 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1747 struct hda_gen_spec *spec = codec->spec;
1748 unsigned int select = ucontrol->value.enumerated.item[0];
1749 int ret = 0;
1750
1751 mutex_lock(&spec->pcm_mutex);
1752 if (spec->active_streams) {
1753 ret = -EBUSY;
1754 goto unlock;
1755 }
1756
1757 if (spec->indep_hp_enabled != select) {
1758 spec->indep_hp_enabled = select;
1759 if (spec->indep_hp_enabled)
1760 spec->multiout.hp_out_nid[0] = 0;
1761 else
1762 spec->multiout.hp_out_nid[0] = spec->alt_dac_nid;
1763 ret = 1;
1764 }
1765 unlock:
1766 mutex_unlock(&spec->pcm_mutex);
1767 return ret;
1768}
1769
1770static const struct snd_kcontrol_new indep_hp_ctl = {
1771 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1772 .name = "Independent HP",
1773 .info = indep_hp_info,
1774 .get = indep_hp_get,
1775 .put = indep_hp_put,
1776};
1777
1778
1779static int create_indep_hp_ctls(struct hda_codec *codec)
1780{
1781 struct hda_gen_spec *spec = codec->spec;
1782
1783 if (!spec->indep_hp)
1784 return 0;
1785 if (!spec->multiout.hp_out_nid[0]) {
1786 spec->indep_hp = 0;
1787 return 0;
1788 }
1789
1790 spec->indep_hp_enabled = false;
1791 spec->alt_dac_nid = spec->multiout.hp_out_nid[0];
1792 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
1793 return -ENOMEM;
1794 return 0;
1795}
1796
1797/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01001798 * channel mode enum control
1799 */
1800
1801static int ch_mode_info(struct snd_kcontrol *kcontrol,
1802 struct snd_ctl_elem_info *uinfo)
1803{
1804 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1805 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001806 int chs;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001807
1808 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1809 uinfo->count = 1;
1810 uinfo->value.enumerated.items = spec->multi_ios + 1;
1811 if (uinfo->value.enumerated.item > spec->multi_ios)
1812 uinfo->value.enumerated.item = spec->multi_ios;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001813 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
1814 sprintf(uinfo->value.enumerated.name, "%dch", chs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001815 return 0;
1816}
1817
1818static int ch_mode_get(struct snd_kcontrol *kcontrol,
1819 struct snd_ctl_elem_value *ucontrol)
1820{
1821 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1822 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001823 ucontrol->value.enumerated.item[0] =
1824 (spec->ext_channel_count - spec->min_channel_count) / 2;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001825 return 0;
1826}
1827
Takashi Iwai196c17662013-01-04 15:01:40 +01001828static inline struct nid_path *
1829get_multiio_path(struct hda_codec *codec, int idx)
1830{
1831 struct hda_gen_spec *spec = codec->spec;
1832 return snd_hda_get_path_from_idx(codec,
1833 spec->out_paths[spec->autocfg.line_outs + idx]);
1834}
1835
Takashi Iwaia5cc2502013-01-16 18:08:55 +01001836static void update_automute_all(struct hda_codec *codec);
1837
Takashi Iwai352f7f92012-12-19 12:52:06 +01001838static int set_multi_io(struct hda_codec *codec, int idx, bool output)
1839{
1840 struct hda_gen_spec *spec = codec->spec;
1841 hda_nid_t nid = spec->multi_io[idx].pin;
1842 struct nid_path *path;
1843
Takashi Iwai196c17662013-01-04 15:01:40 +01001844 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001845 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001847
1848 if (path->active == output)
1849 return 0;
1850
1851 if (output) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01001852 set_pin_target(codec, nid, PIN_OUT, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001853 snd_hda_activate_path(codec, path, true, true);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01001854 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001855 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01001856 set_pin_eapd(codec, nid, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001857 snd_hda_activate_path(codec, path, false, true);
Takashi Iwai2c12c302013-01-10 09:33:29 +01001858 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 }
Takashi Iwaia365fed2013-01-10 16:10:06 +01001860
1861 /* update jack retasking in case it modifies any of them */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01001862 update_automute_all(codec);
Takashi Iwaia365fed2013-01-10 16:10:06 +01001863
Takashi Iwai352f7f92012-12-19 12:52:06 +01001864 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865}
1866
Takashi Iwai352f7f92012-12-19 12:52:06 +01001867static int ch_mode_put(struct snd_kcontrol *kcontrol,
1868 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001870 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1871 struct hda_gen_spec *spec = codec->spec;
1872 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
Takashi Iwai352f7f92012-12-19 12:52:06 +01001874 ch = ucontrol->value.enumerated.item[0];
1875 if (ch < 0 || ch > spec->multi_ios)
1876 return -EINVAL;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001877 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001878 return 0;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001879 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001880 for (i = 0; i < spec->multi_ios; i++)
1881 set_multi_io(codec, i, i < ch);
1882 spec->multiout.max_channels = max(spec->ext_channel_count,
1883 spec->const_channel_count);
1884 if (spec->need_dac_fix)
1885 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 return 1;
1887}
1888
Takashi Iwai352f7f92012-12-19 12:52:06 +01001889static const struct snd_kcontrol_new channel_mode_enum = {
1890 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1891 .name = "Channel Mode",
1892 .info = ch_mode_info,
1893 .get = ch_mode_get,
1894 .put = ch_mode_put,
1895};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
Takashi Iwai352f7f92012-12-19 12:52:06 +01001897static int create_multi_channel_mode(struct hda_codec *codec)
1898{
1899 struct hda_gen_spec *spec = codec->spec;
1900
1901 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01001902 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01001903 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 return 0;
1906}
1907
Takashi Iwai352f7f92012-12-19 12:52:06 +01001908/*
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001909 * aamix loopback enable/disable switch
1910 */
1911
1912#define loopback_mixing_info indep_hp_info
1913
1914static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
1915 struct snd_ctl_elem_value *ucontrol)
1916{
1917 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1918 struct hda_gen_spec *spec = codec->spec;
1919 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
1920 return 0;
1921}
1922
1923static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
1924 int nomix_path_idx, int mix_path_idx)
1925{
1926 struct nid_path *nomix_path, *mix_path;
1927
1928 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
1929 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
1930 if (!nomix_path || !mix_path)
1931 return;
1932 if (do_mix) {
1933 snd_hda_activate_path(codec, nomix_path, false, true);
1934 snd_hda_activate_path(codec, mix_path, true, true);
1935 } else {
1936 snd_hda_activate_path(codec, mix_path, false, true);
1937 snd_hda_activate_path(codec, nomix_path, true, true);
1938 }
1939}
1940
1941static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
1942 struct snd_ctl_elem_value *ucontrol)
1943{
1944 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1945 struct hda_gen_spec *spec = codec->spec;
1946 unsigned int val = ucontrol->value.enumerated.item[0];
1947
1948 if (val == spec->aamix_mode)
1949 return 0;
1950 spec->aamix_mode = val;
1951 update_aamix_paths(codec, val, spec->out_paths[0],
1952 spec->aamix_out_paths[0]);
1953 update_aamix_paths(codec, val, spec->hp_paths[0],
1954 spec->aamix_out_paths[1]);
1955 update_aamix_paths(codec, val, spec->speaker_paths[0],
1956 spec->aamix_out_paths[2]);
1957 return 1;
1958}
1959
1960static const struct snd_kcontrol_new loopback_mixing_enum = {
1961 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1962 .name = "Loopback Mixing",
1963 .info = loopback_mixing_info,
1964 .get = loopback_mixing_get,
1965 .put = loopback_mixing_put,
1966};
1967
1968static int create_loopback_mixing_ctl(struct hda_codec *codec)
1969{
1970 struct hda_gen_spec *spec = codec->spec;
1971
1972 if (!spec->mixer_nid)
1973 return 0;
1974 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
1975 spec->aamix_out_paths[2]))
1976 return 0;
1977 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
1978 return -ENOMEM;
1979 return 0;
1980}
1981
1982/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01001983 * shared headphone/mic handling
1984 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02001985
Takashi Iwai352f7f92012-12-19 12:52:06 +01001986static void call_update_outputs(struct hda_codec *codec);
1987
1988/* for shared I/O, change the pin-control accordingly */
1989static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic)
1990{
1991 struct hda_gen_spec *spec = codec->spec;
1992 unsigned int val;
1993 hda_nid_t pin = spec->autocfg.inputs[1].pin;
1994 /* NOTE: this assumes that there are only two inputs, the
1995 * first is the real internal mic and the second is HP/mic jack.
1996 */
1997
1998 val = snd_hda_get_default_vref(codec, pin);
1999
2000 /* This pin does not have vref caps - let's enable vref on pin 0x18
2001 instead, as suggested by Realtek */
2002 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2003 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2004 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2005 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01002006 snd_hda_set_pin_ctl_cache(codec, vref_pin,
2007 PIN_IN | (set_as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02002008 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002009
2010 val = set_as_mic ? val | PIN_IN : PIN_HP;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002011 set_pin_target(codec, pin, val, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002012
2013 spec->automute_speaker = !set_as_mic;
2014 call_update_outputs(codec);
2015}
2016
2017/* create a shared input with the headphone out */
2018static int create_shared_input(struct hda_codec *codec)
2019{
2020 struct hda_gen_spec *spec = codec->spec;
2021 struct auto_pin_cfg *cfg = &spec->autocfg;
2022 unsigned int defcfg;
2023 hda_nid_t nid;
2024
2025 /* only one internal input pin? */
2026 if (cfg->num_inputs != 1)
2027 return 0;
2028 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2029 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2030 return 0;
2031
2032 if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
2033 nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
2034 else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
2035 nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
2036 else
2037 return 0; /* both not available */
2038
2039 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2040 return 0; /* no input */
2041
2042 cfg->inputs[1].pin = nid;
2043 cfg->inputs[1].type = AUTO_PIN_MIC;
2044 cfg->num_inputs = 2;
2045 spec->shared_mic_hp = 1;
2046 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
2047 return 0;
2048}
2049
Takashi Iwai978e77e2013-01-10 16:57:58 +01002050/*
2051 * output jack mode
2052 */
2053static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2054 struct snd_ctl_elem_info *uinfo)
2055{
2056 static const char * const texts[] = {
2057 "Line Out", "Headphone Out",
2058 };
2059 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, texts);
2060}
2061
2062static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2063 struct snd_ctl_elem_value *ucontrol)
2064{
2065 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2066 hda_nid_t nid = kcontrol->private_value;
2067 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2068 ucontrol->value.enumerated.item[0] = 1;
2069 else
2070 ucontrol->value.enumerated.item[0] = 0;
2071 return 0;
2072}
2073
2074static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2075 struct snd_ctl_elem_value *ucontrol)
2076{
2077 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2078 hda_nid_t nid = kcontrol->private_value;
2079 unsigned int val;
2080
2081 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2082 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2083 return 0;
2084 snd_hda_set_pin_ctl_cache(codec, nid, val);
2085 return 1;
2086}
2087
2088static const struct snd_kcontrol_new out_jack_mode_enum = {
2089 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2090 .info = out_jack_mode_info,
2091 .get = out_jack_mode_get,
2092 .put = out_jack_mode_put,
2093};
2094
2095static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2096{
2097 struct hda_gen_spec *spec = codec->spec;
2098 int i;
2099
2100 for (i = 0; i < spec->kctls.used; i++) {
2101 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2102 if (!strcmp(kctl->name, name) && kctl->index == idx)
2103 return true;
2104 }
2105 return false;
2106}
2107
2108static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2109 char *name, size_t name_len)
2110{
2111 struct hda_gen_spec *spec = codec->spec;
2112 int idx = 0;
2113
2114 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2115 strlcat(name, " Jack Mode", name_len);
2116
2117 for (; find_kctl_name(codec, name, idx); idx++)
2118 ;
2119}
2120
2121static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2122 hda_nid_t *pins)
2123{
2124 struct hda_gen_spec *spec = codec->spec;
2125 int i;
2126
2127 for (i = 0; i < num_pins; i++) {
2128 hda_nid_t pin = pins[i];
2129 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2130 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV)) {
2131 struct snd_kcontrol_new *knew;
2132 char name[44];
2133 get_jack_mode_name(codec, pin, name, sizeof(name));
2134 knew = snd_hda_gen_add_kctl(spec, name,
2135 &out_jack_mode_enum);
2136 if (!knew)
2137 return -ENOMEM;
2138 knew->private_value = pin;
2139 }
2140 }
2141
2142 return 0;
2143}
2144
Takashi Iwai294765582013-01-17 09:52:11 +01002145/*
2146 * input jack mode
2147 */
2148
2149/* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2150#define NUM_VREFS 6
2151
2152static const char * const vref_texts[NUM_VREFS] = {
2153 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2154 "", "Mic 80pc Bias", "Mic 100pc Bias"
2155};
2156
2157static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2158{
2159 unsigned int pincap;
2160
2161 pincap = snd_hda_query_pin_caps(codec, pin);
2162 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2163 /* filter out unusual vrefs */
2164 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2165 return pincap;
2166}
2167
2168/* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2169static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2170{
2171 unsigned int i, n = 0;
2172
2173 for (i = 0; i < NUM_VREFS; i++) {
2174 if (vref_caps & (1 << i)) {
2175 if (n == item_idx)
2176 return i;
2177 n++;
2178 }
2179 }
2180 return 0;
2181}
2182
2183/* convert back from the vref ctl index to the enum item index */
2184static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2185{
2186 unsigned int i, n = 0;
2187
2188 for (i = 0; i < NUM_VREFS; i++) {
2189 if (i == idx)
2190 return n;
2191 if (vref_caps & (1 << i))
2192 n++;
2193 }
2194 return 0;
2195}
2196
2197static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2198 struct snd_ctl_elem_info *uinfo)
2199{
2200 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2201 hda_nid_t nid = kcontrol->private_value;
2202 unsigned int vref_caps = get_vref_caps(codec, nid);
2203
2204 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2205 vref_texts);
2206 /* set the right text */
2207 strcpy(uinfo->value.enumerated.name,
2208 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2209 return 0;
2210}
2211
2212static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2213 struct snd_ctl_elem_value *ucontrol)
2214{
2215 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2216 hda_nid_t nid = kcontrol->private_value;
2217 unsigned int vref_caps = get_vref_caps(codec, nid);
2218 unsigned int idx;
2219
2220 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2221 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2222 return 0;
2223}
2224
2225static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2226 struct snd_ctl_elem_value *ucontrol)
2227{
2228 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2229 hda_nid_t nid = kcontrol->private_value;
2230 unsigned int vref_caps = get_vref_caps(codec, nid);
2231 unsigned int val, idx;
2232
2233 val = snd_hda_codec_get_pin_target(codec, nid);
2234 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2235 if (idx == ucontrol->value.enumerated.item[0])
2236 return 0;
2237
2238 val &= ~AC_PINCTL_VREFEN;
2239 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2240 snd_hda_set_pin_ctl_cache(codec, nid, val);
2241 return 1;
2242}
2243
2244static const struct snd_kcontrol_new in_jack_mode_enum = {
2245 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2246 .info = in_jack_mode_info,
2247 .get = in_jack_mode_get,
2248 .put = in_jack_mode_put,
2249};
2250
2251static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2252{
2253 struct hda_gen_spec *spec = codec->spec;
2254 unsigned int defcfg;
2255 struct snd_kcontrol_new *knew;
2256 char name[44];
2257
2258 /* no jack mode for fixed pins */
2259 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2260 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2261 return 0;
2262
2263 /* no multiple vref caps? */
2264 if (hweight32(get_vref_caps(codec, pin)) <= 1)
2265 return 0;
2266
2267 get_jack_mode_name(codec, pin, name, sizeof(name));
2268 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2269 if (!knew)
2270 return -ENOMEM;
2271 knew->private_value = pin;
2272 return 0;
2273}
2274
Takashi Iwai352f7f92012-12-19 12:52:06 +01002275
2276/*
2277 * Parse input paths
2278 */
2279
2280#ifdef CONFIG_PM
2281/* add the powersave loopback-list entry */
2282static void add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
2283{
2284 struct hda_amp_list *list;
2285
2286 if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
2287 return;
2288 list = spec->loopback_list + spec->num_loopbacks;
2289 list->nid = mix;
2290 list->dir = HDA_INPUT;
2291 list->idx = idx;
2292 spec->num_loopbacks++;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002293 spec->loopback.amplist = spec->loopback_list;
2294}
2295#else
Takashi Iwai352f7f92012-12-19 12:52:06 +01002296#define add_loopback_list(spec, mix, idx) /* NOP */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002297#endif
2298
Takashi Iwai352f7f92012-12-19 12:52:06 +01002299/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01002300static int new_analog_input(struct hda_codec *codec, int input_idx,
2301 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002302 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002304 struct hda_gen_spec *spec = codec->spec;
2305 struct nid_path *path;
2306 unsigned int val;
2307 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
Takashi Iwai352f7f92012-12-19 12:52:06 +01002309 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
2310 !nid_has_mute(codec, mix_nid, HDA_INPUT))
2311 return 0; /* no need for analog loopback */
2312
Takashi Iwai3ca529d2013-01-07 17:25:08 +01002313 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002314 if (!path)
2315 return -EINVAL;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002316 print_nid_path("loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01002317 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002318
2319 idx = path->idx[path->depth - 1];
2320 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
2321 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2322 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002323 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002325 path->ctls[NID_PATH_VOL_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 }
2327
Takashi Iwai352f7f92012-12-19 12:52:06 +01002328 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
2329 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2330 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002331 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002333 path->ctls[NID_PATH_MUTE_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 }
2335
Takashi Iwai352f7f92012-12-19 12:52:06 +01002336 path->active = true;
2337 add_loopback_list(spec, mix_nid, idx);
2338 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339}
2340
Takashi Iwai352f7f92012-12-19 12:52:06 +01002341static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002343 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
2344 return (pincap & AC_PINCAP_IN) != 0;
2345}
2346
2347/* Parse the codec tree and retrieve ADCs */
2348static int fill_adc_nids(struct hda_codec *codec)
2349{
2350 struct hda_gen_spec *spec = codec->spec;
2351 hda_nid_t nid;
2352 hda_nid_t *adc_nids = spec->adc_nids;
2353 int max_nums = ARRAY_SIZE(spec->adc_nids);
2354 int i, nums = 0;
2355
2356 nid = codec->start_nid;
2357 for (i = 0; i < codec->num_nodes; i++, nid++) {
2358 unsigned int caps = get_wcaps(codec, nid);
2359 int type = get_wcaps_type(caps);
2360
2361 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2362 continue;
2363 adc_nids[nums] = nid;
2364 if (++nums >= max_nums)
2365 break;
2366 }
2367 spec->num_adc_nids = nums;
2368 return nums;
2369}
2370
2371/* filter out invalid adc_nids that don't give all active input pins;
2372 * if needed, check whether dynamic ADC-switching is available
2373 */
2374static int check_dyn_adc_switch(struct hda_codec *codec)
2375{
2376 struct hda_gen_spec *spec = codec->spec;
2377 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002378 unsigned int ok_bits;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002379 int i, n, nums;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002380
2381 again:
2382 nums = 0;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002383 ok_bits = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002384 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002385 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002386 if (!spec->input_paths[i][n])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002387 break;
2388 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002389 if (i >= imux->num_items) {
2390 ok_bits |= (1 << n);
2391 nums++;
2392 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002393 }
2394
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002395 if (!ok_bits) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002396 if (spec->shared_mic_hp) {
2397 spec->shared_mic_hp = 0;
2398 imux->num_items = 1;
2399 goto again;
2400 }
2401
2402 /* check whether ADC-switch is possible */
2403 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002404 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002405 if (spec->input_paths[i][n]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002406 spec->dyn_adc_idx[i] = n;
2407 break;
2408 }
2409 }
2410 }
2411
2412 snd_printdd("hda-codec: enabling ADC switching\n");
2413 spec->dyn_adc_switch = 1;
2414 } else if (nums != spec->num_adc_nids) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002415 /* shrink the invalid adcs and input paths */
2416 nums = 0;
2417 for (n = 0; n < spec->num_adc_nids; n++) {
2418 if (!(ok_bits & (1 << n)))
2419 continue;
2420 if (n != nums) {
2421 spec->adc_nids[nums] = spec->adc_nids[n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002422 for (i = 0; i < imux->num_items; i++) {
2423 invalidate_nid_path(codec,
2424 spec->input_paths[i][nums]);
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002425 spec->input_paths[i][nums] =
2426 spec->input_paths[i][n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002427 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002428 }
2429 nums++;
2430 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002431 spec->num_adc_nids = nums;
2432 }
2433
2434 if (imux->num_items == 1 || spec->shared_mic_hp) {
2435 snd_printdd("hda-codec: reducing to a single ADC\n");
2436 spec->num_adc_nids = 1; /* reduce to a single ADC */
2437 }
2438
2439 /* single index for individual volumes ctls */
2440 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
2441 spec->num_adc_nids = 1;
2442
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 return 0;
2444}
2445
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002446/* parse capture source paths from the given pin and create imux items */
2447static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
2448 int num_adcs, const char *label, int anchor)
2449{
2450 struct hda_gen_spec *spec = codec->spec;
2451 struct hda_input_mux *imux = &spec->input_mux;
2452 int imux_idx = imux->num_items;
2453 bool imux_added = false;
2454 int c;
2455
2456 for (c = 0; c < num_adcs; c++) {
2457 struct nid_path *path;
2458 hda_nid_t adc = spec->adc_nids[c];
2459
2460 if (!is_reachable_path(codec, pin, adc))
2461 continue;
2462 path = snd_hda_add_new_path(codec, pin, adc, anchor);
2463 if (!path)
2464 continue;
2465 print_nid_path("input", path);
2466 spec->input_paths[imux_idx][c] =
2467 snd_hda_get_path_idx(codec, path);
2468
2469 if (!imux_added) {
2470 spec->imux_pins[imux->num_items] = pin;
2471 snd_hda_add_imux_item(imux, label,
2472 imux->num_items, NULL);
2473 imux_added = true;
2474 }
2475 }
2476
2477 return 0;
2478}
2479
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002481 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002483static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002484{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002485 struct hda_gen_spec *spec = codec->spec;
2486 const struct auto_pin_cfg *cfg = &spec->autocfg;
2487 hda_nid_t mixer = spec->mixer_nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002488 int num_adcs;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002489 int i, err, type_idx = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002490 const char *prev_label = NULL;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002491 unsigned int val;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002492
Takashi Iwai352f7f92012-12-19 12:52:06 +01002493 num_adcs = fill_adc_nids(codec);
2494 if (num_adcs < 0)
2495 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002496
Takashi Iwai352f7f92012-12-19 12:52:06 +01002497 for (i = 0; i < cfg->num_inputs; i++) {
2498 hda_nid_t pin;
2499 const char *label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500
Takashi Iwai352f7f92012-12-19 12:52:06 +01002501 pin = cfg->inputs[i].pin;
2502 if (!is_input_pin(codec, pin))
2503 continue;
2504
2505 label = hda_get_autocfg_input_label(codec, cfg, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002506 if (prev_label && !strcmp(label, prev_label))
2507 type_idx++;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002508 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01002509 type_idx = 0;
2510 prev_label = label;
2511
Takashi Iwai2c12c302013-01-10 09:33:29 +01002512 val = PIN_IN;
2513 if (cfg->inputs[i].type == AUTO_PIN_MIC)
2514 val |= snd_hda_get_default_vref(codec, pin);
2515 set_pin_target(codec, pin, val, false);
2516
Takashi Iwai352f7f92012-12-19 12:52:06 +01002517 if (mixer) {
2518 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01002519 err = new_analog_input(codec, i, pin,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002520 label, type_idx, mixer);
2521 if (err < 0)
2522 return err;
2523 }
2524 }
2525
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002526 err = parse_capture_source(codec, pin, num_adcs, label, -mixer);
2527 if (err < 0)
2528 return err;
Takashi Iwai294765582013-01-17 09:52:11 +01002529
2530 if (spec->add_in_jack_modes) {
2531 err = create_in_jack_mode(codec, pin);
2532 if (err < 0)
2533 return err;
2534 }
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002535 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002536
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002537 if (mixer && spec->add_stereo_mix_input) {
2538 err = parse_capture_source(codec, mixer, num_adcs,
2539 "Stereo Mix", 0);
2540 if (err < 0)
2541 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002542 }
2543
2544 return 0;
2545}
2546
2547
2548/*
2549 * input source mux
2550 */
2551
Takashi Iwaic697b712013-01-07 17:09:26 +01002552/* get the input path specified by the given adc and imux indices */
2553static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002554{
2555 struct hda_gen_spec *spec = codec->spec;
David Henningssonb56fa1e2013-01-16 11:45:35 +01002556 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
2557 snd_BUG();
2558 return NULL;
2559 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002560 if (spec->dyn_adc_switch)
2561 adc_idx = spec->dyn_adc_idx[imux_idx];
David Henningssonb56fa1e2013-01-16 11:45:35 +01002562 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_OUTS) {
2563 snd_BUG();
2564 return NULL;
2565 }
Takashi Iwaic697b712013-01-07 17:09:26 +01002566 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002567}
2568
2569static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2570 unsigned int idx);
2571
2572static int mux_enum_info(struct snd_kcontrol *kcontrol,
2573 struct snd_ctl_elem_info *uinfo)
2574{
2575 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2576 struct hda_gen_spec *spec = codec->spec;
2577 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
2578}
2579
2580static int mux_enum_get(struct snd_kcontrol *kcontrol,
2581 struct snd_ctl_elem_value *ucontrol)
2582{
2583 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2584 struct hda_gen_spec *spec = codec->spec;
David Henningssona053d1e2013-01-16 11:45:36 +01002585 unsigned int adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002586
2587 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
2588 return 0;
2589}
2590
2591static int mux_enum_put(struct snd_kcontrol *kcontrol,
2592 struct snd_ctl_elem_value *ucontrol)
2593{
2594 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
David Henningssona053d1e2013-01-16 11:45:36 +01002595 unsigned int adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002596 return mux_select(codec, adc_idx,
2597 ucontrol->value.enumerated.item[0]);
2598}
2599
Takashi Iwai352f7f92012-12-19 12:52:06 +01002600static const struct snd_kcontrol_new cap_src_temp = {
2601 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2602 .name = "Input Source",
2603 .info = mux_enum_info,
2604 .get = mux_enum_get,
2605 .put = mux_enum_put,
2606};
2607
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002608/*
2609 * capture volume and capture switch ctls
2610 */
2611
Takashi Iwai352f7f92012-12-19 12:52:06 +01002612typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
2613 struct snd_ctl_elem_value *ucontrol);
2614
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002615/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002616static int cap_put_caller(struct snd_kcontrol *kcontrol,
2617 struct snd_ctl_elem_value *ucontrol,
2618 put_call_t func, int type)
2619{
2620 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2621 struct hda_gen_spec *spec = codec->spec;
2622 const struct hda_input_mux *imux;
2623 struct nid_path *path;
2624 int i, adc_idx, err = 0;
2625
2626 imux = &spec->input_mux;
David Henningssona053d1e2013-01-16 11:45:36 +01002627 adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002628 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002629 /* we use the cache-only update at first since multiple input paths
2630 * may shared the same amp; by updating only caches, the redundant
2631 * writes to hardware can be reduced.
2632 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002633 codec->cached_write = 1;
2634 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01002635 path = get_input_path(codec, adc_idx, i);
2636 if (!path || !path->ctls[type])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002637 continue;
2638 kcontrol->private_value = path->ctls[type];
2639 err = func(kcontrol, ucontrol);
2640 if (err < 0)
2641 goto error;
2642 }
2643 error:
2644 codec->cached_write = 0;
2645 mutex_unlock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002646 snd_hda_codec_flush_amp_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002647 if (err >= 0 && spec->cap_sync_hook)
2648 spec->cap_sync_hook(codec);
2649 return err;
2650}
2651
2652/* capture volume ctl callbacks */
2653#define cap_vol_info snd_hda_mixer_amp_volume_info
2654#define cap_vol_get snd_hda_mixer_amp_volume_get
2655#define cap_vol_tlv snd_hda_mixer_amp_tlv
2656
2657static int cap_vol_put(struct snd_kcontrol *kcontrol,
2658 struct snd_ctl_elem_value *ucontrol)
2659{
2660 return cap_put_caller(kcontrol, ucontrol,
2661 snd_hda_mixer_amp_volume_put,
2662 NID_PATH_VOL_CTL);
2663}
2664
2665static const struct snd_kcontrol_new cap_vol_temp = {
2666 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2667 .name = "Capture Volume",
2668 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
2669 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2670 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
2671 .info = cap_vol_info,
2672 .get = cap_vol_get,
2673 .put = cap_vol_put,
2674 .tlv = { .c = cap_vol_tlv },
2675};
2676
2677/* capture switch ctl callbacks */
2678#define cap_sw_info snd_ctl_boolean_stereo_info
2679#define cap_sw_get snd_hda_mixer_amp_switch_get
2680
2681static int cap_sw_put(struct snd_kcontrol *kcontrol,
2682 struct snd_ctl_elem_value *ucontrol)
2683{
Takashi Iwaiae177c32013-01-14 12:13:06 +01002684 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2685 struct hda_gen_spec *spec = codec->spec;
2686 int ret;
2687
2688 ret = cap_put_caller(kcontrol, ucontrol,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002689 snd_hda_mixer_amp_switch_put,
2690 NID_PATH_MUTE_CTL);
Takashi Iwaiae177c32013-01-14 12:13:06 +01002691 if (ret < 0)
2692 return ret;
2693
2694 if (spec->capture_switch_hook) {
2695 bool enable = (ucontrol->value.integer.value[0] ||
2696 ucontrol->value.integer.value[1]);
2697 spec->capture_switch_hook(codec, enable);
2698 }
2699
2700 return ret;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002701}
2702
2703static const struct snd_kcontrol_new cap_sw_temp = {
2704 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2705 .name = "Capture Switch",
2706 .info = cap_sw_info,
2707 .get = cap_sw_get,
2708 .put = cap_sw_put,
2709};
2710
2711static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
2712{
2713 hda_nid_t nid;
2714 int i, depth;
2715
2716 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
2717 for (depth = 0; depth < 3; depth++) {
2718 if (depth >= path->depth)
2719 return -EINVAL;
2720 i = path->depth - depth - 1;
2721 nid = path->path[i];
2722 if (!path->ctls[NID_PATH_VOL_CTL]) {
2723 if (nid_has_volume(codec, nid, HDA_OUTPUT))
2724 path->ctls[NID_PATH_VOL_CTL] =
2725 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2726 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
2727 int idx = path->idx[i];
2728 if (!depth && codec->single_adc_amp)
2729 idx = 0;
2730 path->ctls[NID_PATH_VOL_CTL] =
2731 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2732 }
2733 }
2734 if (!path->ctls[NID_PATH_MUTE_CTL]) {
2735 if (nid_has_mute(codec, nid, HDA_OUTPUT))
2736 path->ctls[NID_PATH_MUTE_CTL] =
2737 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2738 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
2739 int idx = path->idx[i];
2740 if (!depth && codec->single_adc_amp)
2741 idx = 0;
2742 path->ctls[NID_PATH_MUTE_CTL] =
2743 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2744 }
2745 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01002746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 return 0;
2748}
2749
Takashi Iwai352f7f92012-12-19 12:52:06 +01002750static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002752 struct hda_gen_spec *spec = codec->spec;
2753 struct auto_pin_cfg *cfg = &spec->autocfg;
2754 unsigned int val;
2755 int i;
2756
2757 if (!spec->inv_dmic_split)
2758 return false;
2759 for (i = 0; i < cfg->num_inputs; i++) {
2760 if (cfg->inputs[i].pin != nid)
2761 continue;
2762 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2763 return false;
2764 val = snd_hda_codec_get_pincfg(codec, nid);
2765 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
2766 }
2767 return false;
2768}
2769
2770static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
2771 int idx, bool is_switch, unsigned int ctl,
2772 bool inv_dmic)
2773{
2774 struct hda_gen_spec *spec = codec->spec;
2775 char tmpname[44];
2776 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
2777 const char *sfx = is_switch ? "Switch" : "Volume";
2778 unsigned int chs = inv_dmic ? 1 : 3;
2779 int err;
2780
2781 if (!ctl)
2782 return 0;
2783
2784 if (label)
2785 snprintf(tmpname, sizeof(tmpname),
2786 "%s Capture %s", label, sfx);
2787 else
2788 snprintf(tmpname, sizeof(tmpname),
2789 "Capture %s", sfx);
2790 err = add_control(spec, type, tmpname, idx,
2791 amp_val_replace_channels(ctl, chs));
2792 if (err < 0 || !inv_dmic)
2793 return err;
2794
2795 /* Make independent right kcontrol */
2796 if (label)
2797 snprintf(tmpname, sizeof(tmpname),
2798 "Inverted %s Capture %s", label, sfx);
2799 else
2800 snprintf(tmpname, sizeof(tmpname),
2801 "Inverted Capture %s", sfx);
2802 return add_control(spec, type, tmpname, idx,
2803 amp_val_replace_channels(ctl, 2));
2804}
2805
2806/* create single (and simple) capture volume and switch controls */
2807static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
2808 unsigned int vol_ctl, unsigned int sw_ctl,
2809 bool inv_dmic)
2810{
2811 int err;
2812 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
2813 if (err < 0)
2814 return err;
2815 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
2816 if (err < 0)
2817 return err;
2818 return 0;
2819}
2820
2821/* create bound capture volume and switch controls */
2822static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
2823 unsigned int vol_ctl, unsigned int sw_ctl)
2824{
2825 struct hda_gen_spec *spec = codec->spec;
2826 struct snd_kcontrol_new *knew;
2827
2828 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002829 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002830 if (!knew)
2831 return -ENOMEM;
2832 knew->index = idx;
2833 knew->private_value = vol_ctl;
2834 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2835 }
2836 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002837 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002838 if (!knew)
2839 return -ENOMEM;
2840 knew->index = idx;
2841 knew->private_value = sw_ctl;
2842 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2843 }
2844 return 0;
2845}
2846
2847/* return the vol ctl when used first in the imux list */
2848static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
2849{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002850 struct nid_path *path;
2851 unsigned int ctl;
2852 int i;
2853
Takashi Iwaic697b712013-01-07 17:09:26 +01002854 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002855 if (!path)
2856 return 0;
2857 ctl = path->ctls[type];
2858 if (!ctl)
2859 return 0;
2860 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01002861 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002862 if (path && path->ctls[type] == ctl)
2863 return 0;
2864 }
2865 return ctl;
2866}
2867
2868/* create individual capture volume and switch controls per input */
2869static int create_multi_cap_vol_ctl(struct hda_codec *codec)
2870{
2871 struct hda_gen_spec *spec = codec->spec;
2872 struct hda_input_mux *imux = &spec->input_mux;
2873 int i, err, type, type_idx = 0;
2874 const char *prev_label = NULL;
2875
2876 for (i = 0; i < imux->num_items; i++) {
2877 const char *label;
2878 bool inv_dmic;
2879 label = hda_get_autocfg_input_label(codec, &spec->autocfg, i);
2880 if (prev_label && !strcmp(label, prev_label))
2881 type_idx++;
2882 else
2883 type_idx = 0;
2884 prev_label = label;
2885 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
2886
2887 for (type = 0; type < 2; type++) {
2888 err = add_single_cap_ctl(codec, label, type_idx, type,
2889 get_first_cap_ctl(codec, i, type),
2890 inv_dmic);
2891 if (err < 0)
2892 return err;
2893 }
2894 }
2895 return 0;
2896}
2897
2898static int create_capture_mixers(struct hda_codec *codec)
2899{
2900 struct hda_gen_spec *spec = codec->spec;
2901 struct hda_input_mux *imux = &spec->input_mux;
2902 int i, n, nums, err;
2903
2904 if (spec->dyn_adc_switch)
2905 nums = 1;
2906 else
2907 nums = spec->num_adc_nids;
2908
2909 if (!spec->auto_mic && imux->num_items > 1) {
2910 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01002911 const char *name;
2912 name = nums > 1 ? "Input Source" : "Capture Source";
2913 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002914 if (!knew)
2915 return -ENOMEM;
2916 knew->count = nums;
2917 }
2918
2919 for (n = 0; n < nums; n++) {
2920 bool multi = false;
David Henningsson99a55922013-01-16 15:58:44 +01002921 bool multi_cap_vol = spec->multi_cap_vol;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002922 bool inv_dmic = false;
2923 int vol, sw;
2924
2925 vol = sw = 0;
2926 for (i = 0; i < imux->num_items; i++) {
2927 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01002928 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002929 if (!path)
2930 continue;
2931 parse_capvol_in_path(codec, path);
2932 if (!vol)
2933 vol = path->ctls[NID_PATH_VOL_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01002934 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002935 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01002936 if (!same_amp_caps(codec, vol,
2937 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
2938 multi_cap_vol = true;
2939 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002940 if (!sw)
2941 sw = path->ctls[NID_PATH_MUTE_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01002942 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002943 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01002944 if (!same_amp_caps(codec, sw,
2945 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
2946 multi_cap_vol = true;
2947 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002948 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
2949 inv_dmic = true;
2950 }
2951
2952 if (!multi)
2953 err = create_single_cap_vol_ctl(codec, n, vol, sw,
2954 inv_dmic);
David Henningsson99a55922013-01-16 15:58:44 +01002955 else if (!multi_cap_vol)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002956 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
2957 else
2958 err = create_multi_cap_vol_ctl(codec);
2959 if (err < 0)
2960 return err;
2961 }
2962
2963 return 0;
2964}
2965
2966/*
2967 * add mic boosts if needed
2968 */
2969static int parse_mic_boost(struct hda_codec *codec)
2970{
2971 struct hda_gen_spec *spec = codec->spec;
2972 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002973 int i, err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002974 int type_idx = 0;
2975 hda_nid_t nid;
2976 const char *prev_label = NULL;
2977
2978 for (i = 0; i < cfg->num_inputs; i++) {
2979 if (cfg->inputs[i].type > AUTO_PIN_MIC)
2980 break;
2981 nid = cfg->inputs[i].pin;
2982 if (get_wcaps(codec, nid) & AC_WCAP_IN_AMP) {
2983 const char *label;
Takashi Iwai5abd4882013-01-07 09:43:18 +01002984 char boost_label[44];
Takashi Iwai352f7f92012-12-19 12:52:06 +01002985 struct nid_path *path;
2986 unsigned int val;
2987
David Henningsson02aba552013-01-16 15:58:43 +01002988 if (!nid_has_volume(codec, nid, HDA_INPUT))
2989 continue;
2990
Takashi Iwai352f7f92012-12-19 12:52:06 +01002991 label = hda_get_autocfg_input_label(codec, cfg, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002992 if (prev_label && !strcmp(label, prev_label))
2993 type_idx++;
2994 else
2995 type_idx = 0;
2996 prev_label = label;
2997
2998 snprintf(boost_label, sizeof(boost_label),
2999 "%s Boost Volume", label);
3000 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
3001 err = add_control(spec, HDA_CTL_WIDGET_VOL,
3002 boost_label, type_idx, val);
3003 if (err < 0)
3004 return err;
3005
3006 path = snd_hda_get_nid_path(codec, nid, 0);
3007 if (path)
3008 path->ctls[NID_PATH_BOOST_CTL] = val;
3009 }
3010 }
3011 return 0;
3012}
3013
3014/*
3015 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3016 */
3017static void parse_digital(struct hda_codec *codec)
3018{
3019 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003020 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003021 int i, nums;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003022 hda_nid_t dig_nid, pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003023
3024 /* support multiple SPDIFs; the secondary is set up as a slave */
3025 nums = 0;
3026 for (i = 0; i < spec->autocfg.dig_outs; i++) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003027 pin = spec->autocfg.dig_out_pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003028 dig_nid = look_for_dac(codec, pin, true);
3029 if (!dig_nid)
3030 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003031 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003032 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003033 continue;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003034 print_nid_path("digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01003035 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01003036 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003037 set_pin_target(codec, pin, PIN_OUT, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003038 if (!nums) {
3039 spec->multiout.dig_out_nid = dig_nid;
3040 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3041 } else {
3042 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3043 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
3044 break;
3045 spec->slave_dig_outs[nums - 1] = dig_nid;
3046 }
3047 nums++;
3048 }
3049
3050 if (spec->autocfg.dig_in_pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003051 pin = spec->autocfg.dig_in_pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003052 dig_nid = codec->start_nid;
3053 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003054 unsigned int wcaps = get_wcaps(codec, dig_nid);
3055 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3056 continue;
3057 if (!(wcaps & AC_WCAP_DIGITAL))
3058 continue;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003059 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003060 if (path) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003061 print_nid_path("digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003062 path->active = true;
3063 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003064 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003065 set_pin_target(codec, pin, PIN_IN, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003066 break;
3067 }
3068 }
3069 }
3070}
3071
3072
3073/*
3074 * input MUX handling
3075 */
3076
3077static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3078
3079/* select the given imux item; either unmute exclusively or select the route */
3080static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3081 unsigned int idx)
3082{
3083 struct hda_gen_spec *spec = codec->spec;
3084 const struct hda_input_mux *imux;
3085 struct nid_path *path;
3086
3087 imux = &spec->input_mux;
3088 if (!imux->num_items)
3089 return 0;
3090
3091 if (idx >= imux->num_items)
3092 idx = imux->num_items - 1;
3093 if (spec->cur_mux[adc_idx] == idx)
3094 return 0;
3095
Takashi Iwaic697b712013-01-07 17:09:26 +01003096 path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003097 if (!path)
3098 return 0;
3099 if (path->active)
3100 snd_hda_activate_path(codec, path, false, false);
3101
3102 spec->cur_mux[adc_idx] = idx;
3103
3104 if (spec->shared_mic_hp)
3105 update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
3106
3107 if (spec->dyn_adc_switch)
3108 dyn_adc_pcm_resetup(codec, idx);
3109
Takashi Iwaic697b712013-01-07 17:09:26 +01003110 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003111 if (!path)
3112 return 0;
3113 if (path->active)
3114 return 0;
3115 snd_hda_activate_path(codec, path, true, false);
3116 if (spec->cap_sync_hook)
3117 spec->cap_sync_hook(codec);
3118 return 1;
3119}
3120
3121
3122/*
3123 * Jack detections for HP auto-mute and mic-switch
3124 */
3125
3126/* check each pin in the given array; returns true if any of them is plugged */
3127static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
3128{
3129 int i, present = 0;
3130
3131 for (i = 0; i < num_pins; i++) {
3132 hda_nid_t nid = pins[i];
3133 if (!nid)
3134 break;
Takashi Iwai0b4df932013-01-10 09:45:13 +01003135 /* don't detect pins retasked as inputs */
3136 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
3137 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003138 present |= snd_hda_jack_detect(codec, nid);
3139 }
3140 return present;
3141}
3142
3143/* standard HP/line-out auto-mute helper */
3144static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
Takashi Iwai2c12c302013-01-10 09:33:29 +01003145 bool mute)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003146{
3147 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003148 int i;
3149
3150 for (i = 0; i < num_pins; i++) {
3151 hda_nid_t nid = pins[i];
3152 unsigned int val;
3153 if (!nid)
3154 break;
3155 /* don't reset VREF value in case it's controlling
3156 * the amp (see alc861_fixup_asus_amp_vref_0f())
3157 */
Takashi Iwai2c12c302013-01-10 09:33:29 +01003158 if (spec->keep_vref_in_automute)
3159 val = snd_hda_codec_get_pin_target(codec, nid) & ~PIN_HP;
3160 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01003161 val = 0;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003162 if (!mute)
3163 val |= snd_hda_codec_get_pin_target(codec, nid);
3164 /* here we call update_pin_ctl() so that the pinctl is changed
3165 * without changing the pinctl target value;
3166 * the original target value will be still referred at the
3167 * init / resume again
3168 */
3169 update_pin_ctl(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01003170 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003171 }
3172}
3173
3174/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003175void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003176{
3177 struct hda_gen_spec *spec = codec->spec;
3178 int on;
3179
3180 /* Control HP pins/amps depending on master_mute state;
3181 * in general, HP pins/amps control should be enabled in all cases,
3182 * but currently set only for master_mute, just to be safe
3183 */
3184 if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
3185 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003186 spec->autocfg.hp_pins, spec->master_mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003187
3188 if (!spec->automute_speaker)
3189 on = 0;
3190 else
3191 on = spec->hp_jack_present | spec->line_jack_present;
3192 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003193 spec->speaker_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003194 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003195 spec->autocfg.speaker_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003196
3197 /* toggle line-out mutes if needed, too */
3198 /* if LO is a copy of either HP or Speaker, don't need to handle it */
3199 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
3200 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
3201 return;
3202 if (!spec->automute_lo)
3203 on = 0;
3204 else
3205 on = spec->hp_jack_present;
3206 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003207 spec->line_out_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003208 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003209 spec->autocfg.line_out_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003210}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003211EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003212
3213static void call_update_outputs(struct hda_codec *codec)
3214{
3215 struct hda_gen_spec *spec = codec->spec;
3216 if (spec->automute_hook)
3217 spec->automute_hook(codec);
3218 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01003219 snd_hda_gen_update_outputs(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003220}
3221
3222/* standard HP-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003223void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003224{
3225 struct hda_gen_spec *spec = codec->spec;
3226
3227 spec->hp_jack_present =
3228 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
3229 spec->autocfg.hp_pins);
3230 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
3231 return;
3232 call_update_outputs(codec);
3233}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003234EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003235
3236/* standard line-out-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003237void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003238{
3239 struct hda_gen_spec *spec = codec->spec;
3240
3241 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3242 return;
3243 /* check LO jack only when it's different from HP */
3244 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
3245 return;
3246
3247 spec->line_jack_present =
3248 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3249 spec->autocfg.line_out_pins);
3250 if (!spec->automute_speaker || !spec->detect_lo)
3251 return;
3252 call_update_outputs(codec);
3253}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003254EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003255
3256/* standard mic auto-switch helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003257void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003258{
3259 struct hda_gen_spec *spec = codec->spec;
3260 int i;
3261
3262 if (!spec->auto_mic)
3263 return;
3264
3265 for (i = spec->am_num_entries - 1; i > 0; i--) {
Takashi Iwai0b4df932013-01-10 09:45:13 +01003266 hda_nid_t pin = spec->am_entry[i].pin;
3267 /* don't detect pins retasked as outputs */
3268 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
3269 continue;
3270 if (snd_hda_jack_detect(codec, pin)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003271 mux_select(codec, 0, spec->am_entry[i].idx);
3272 return;
3273 }
3274 }
3275 mux_select(codec, 0, spec->am_entry[0].idx);
3276}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003277EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003278
Takashi Iwaia5cc2502013-01-16 18:08:55 +01003279/* update jack retasking */
3280static void update_automute_all(struct hda_codec *codec)
3281{
3282 struct hda_gen_spec *spec = codec->spec;
3283
3284 if (spec->hp_automute_hook)
3285 spec->hp_automute_hook(codec, NULL);
3286 else
3287 snd_hda_gen_hp_automute(codec, NULL);
3288 if (spec->line_automute_hook)
3289 spec->line_automute_hook(codec, NULL);
3290 else
3291 snd_hda_gen_line_automute(codec, NULL);
3292 if (spec->mic_autoswitch_hook)
3293 spec->mic_autoswitch_hook(codec, NULL);
3294 else
3295 snd_hda_gen_mic_autoswitch(codec, NULL);
3296}
3297
Takashi Iwai352f7f92012-12-19 12:52:06 +01003298/*
3299 * Auto-Mute mode mixer enum support
3300 */
3301static int automute_mode_info(struct snd_kcontrol *kcontrol,
3302 struct snd_ctl_elem_info *uinfo)
3303{
3304 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3305 struct hda_gen_spec *spec = codec->spec;
3306 static const char * const texts3[] = {
3307 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02003308 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309
Takashi Iwai352f7f92012-12-19 12:52:06 +01003310 if (spec->automute_speaker_possible && spec->automute_lo_possible)
3311 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
3312 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
3313}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314
Takashi Iwai352f7f92012-12-19 12:52:06 +01003315static int automute_mode_get(struct snd_kcontrol *kcontrol,
3316 struct snd_ctl_elem_value *ucontrol)
3317{
3318 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3319 struct hda_gen_spec *spec = codec->spec;
3320 unsigned int val = 0;
3321 if (spec->automute_speaker)
3322 val++;
3323 if (spec->automute_lo)
3324 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003325
Takashi Iwai352f7f92012-12-19 12:52:06 +01003326 ucontrol->value.enumerated.item[0] = val;
3327 return 0;
3328}
3329
3330static int automute_mode_put(struct snd_kcontrol *kcontrol,
3331 struct snd_ctl_elem_value *ucontrol)
3332{
3333 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3334 struct hda_gen_spec *spec = codec->spec;
3335
3336 switch (ucontrol->value.enumerated.item[0]) {
3337 case 0:
3338 if (!spec->automute_speaker && !spec->automute_lo)
3339 return 0;
3340 spec->automute_speaker = 0;
3341 spec->automute_lo = 0;
3342 break;
3343 case 1:
3344 if (spec->automute_speaker_possible) {
3345 if (!spec->automute_lo && spec->automute_speaker)
3346 return 0;
3347 spec->automute_speaker = 1;
3348 spec->automute_lo = 0;
3349 } else if (spec->automute_lo_possible) {
3350 if (spec->automute_lo)
3351 return 0;
3352 spec->automute_lo = 1;
3353 } else
3354 return -EINVAL;
3355 break;
3356 case 2:
3357 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
3358 return -EINVAL;
3359 if (spec->automute_speaker && spec->automute_lo)
3360 return 0;
3361 spec->automute_speaker = 1;
3362 spec->automute_lo = 1;
3363 break;
3364 default:
3365 return -EINVAL;
3366 }
3367 call_update_outputs(codec);
3368 return 1;
3369}
3370
3371static const struct snd_kcontrol_new automute_mode_enum = {
3372 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3373 .name = "Auto-Mute Mode",
3374 .info = automute_mode_info,
3375 .get = automute_mode_get,
3376 .put = automute_mode_put,
3377};
3378
3379static int add_automute_mode_enum(struct hda_codec *codec)
3380{
3381 struct hda_gen_spec *spec = codec->spec;
3382
Takashi Iwai12c93df2012-12-19 14:38:33 +01003383 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01003384 return -ENOMEM;
3385 return 0;
3386}
3387
3388/*
3389 * Check the availability of HP/line-out auto-mute;
3390 * Set up appropriately if really supported
3391 */
3392static int check_auto_mute_availability(struct hda_codec *codec)
3393{
3394 struct hda_gen_spec *spec = codec->spec;
3395 struct auto_pin_cfg *cfg = &spec->autocfg;
3396 int present = 0;
3397 int i, err;
3398
Takashi Iwaif72706b2013-01-16 18:20:07 +01003399 if (spec->suppress_auto_mute)
3400 return 0;
3401
Takashi Iwai352f7f92012-12-19 12:52:06 +01003402 if (cfg->hp_pins[0])
3403 present++;
3404 if (cfg->line_out_pins[0])
3405 present++;
3406 if (cfg->speaker_pins[0])
3407 present++;
3408 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02003409 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003410
3411 if (!cfg->speaker_pins[0] &&
3412 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
3413 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3414 sizeof(cfg->speaker_pins));
3415 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417
Takashi Iwai352f7f92012-12-19 12:52:06 +01003418 if (!cfg->hp_pins[0] &&
3419 cfg->line_out_type == AUTO_PIN_HP_OUT) {
3420 memcpy(cfg->hp_pins, cfg->line_out_pins,
3421 sizeof(cfg->hp_pins));
3422 cfg->hp_outs = cfg->line_outs;
3423 }
3424
3425 for (i = 0; i < cfg->hp_outs; i++) {
3426 hda_nid_t nid = cfg->hp_pins[i];
3427 if (!is_jack_detectable(codec, nid))
3428 continue;
3429 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
3430 nid);
3431 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003432 spec->hp_automute_hook ?
3433 spec->hp_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003434 snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003435 spec->detect_hp = 1;
3436 }
3437
3438 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
3439 if (cfg->speaker_outs)
3440 for (i = 0; i < cfg->line_outs; i++) {
3441 hda_nid_t nid = cfg->line_out_pins[i];
3442 if (!is_jack_detectable(codec, nid))
3443 continue;
3444 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
3445 snd_hda_jack_detect_enable_callback(codec, nid,
3446 HDA_GEN_FRONT_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003447 spec->line_automute_hook ?
3448 spec->line_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003449 snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003450 spec->detect_lo = 1;
3451 }
3452 spec->automute_lo_possible = spec->detect_hp;
3453 }
3454
3455 spec->automute_speaker_possible = cfg->speaker_outs &&
3456 (spec->detect_hp || spec->detect_lo);
3457
3458 spec->automute_lo = spec->automute_lo_possible;
3459 spec->automute_speaker = spec->automute_speaker_possible;
3460
3461 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
3462 /* create a control for automute mode */
3463 err = add_automute_mode_enum(codec);
3464 if (err < 0)
3465 return err;
3466 }
3467 return 0;
3468}
3469
Takashi Iwai352f7f92012-12-19 12:52:06 +01003470/* check whether all auto-mic pins are valid; setup indices if OK */
3471static bool auto_mic_check_imux(struct hda_codec *codec)
3472{
3473 struct hda_gen_spec *spec = codec->spec;
3474 const struct hda_input_mux *imux;
3475 int i;
3476
3477 imux = &spec->input_mux;
3478 for (i = 0; i < spec->am_num_entries; i++) {
3479 spec->am_entry[i].idx =
3480 find_idx_in_nid_list(spec->am_entry[i].pin,
3481 spec->imux_pins, imux->num_items);
3482 if (spec->am_entry[i].idx < 0)
3483 return false; /* no corresponding imux */
3484 }
3485
3486 /* we don't need the jack detection for the first pin */
3487 for (i = 1; i < spec->am_num_entries; i++)
3488 snd_hda_jack_detect_enable_callback(codec,
3489 spec->am_entry[i].pin,
3490 HDA_GEN_MIC_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003491 spec->mic_autoswitch_hook ?
3492 spec->mic_autoswitch_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003493 snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003494 return true;
3495}
3496
3497static int compare_attr(const void *ap, const void *bp)
3498{
3499 const struct automic_entry *a = ap;
3500 const struct automic_entry *b = bp;
3501 return (int)(a->attr - b->attr);
3502}
3503
3504/*
3505 * Check the availability of auto-mic switch;
3506 * Set up if really supported
3507 */
3508static int check_auto_mic_availability(struct hda_codec *codec)
3509{
3510 struct hda_gen_spec *spec = codec->spec;
3511 struct auto_pin_cfg *cfg = &spec->autocfg;
3512 unsigned int types;
3513 int i, num_pins;
3514
Takashi Iwaid12daf62013-01-07 16:32:11 +01003515 if (spec->suppress_auto_mic)
3516 return 0;
3517
Takashi Iwai352f7f92012-12-19 12:52:06 +01003518 types = 0;
3519 num_pins = 0;
3520 for (i = 0; i < cfg->num_inputs; i++) {
3521 hda_nid_t nid = cfg->inputs[i].pin;
3522 unsigned int attr;
3523 attr = snd_hda_codec_get_pincfg(codec, nid);
3524 attr = snd_hda_get_input_pin_attr(attr);
3525 if (types & (1 << attr))
3526 return 0; /* already occupied */
3527 switch (attr) {
3528 case INPUT_PIN_ATTR_INT:
3529 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3530 return 0; /* invalid type */
3531 break;
3532 case INPUT_PIN_ATTR_UNUSED:
3533 return 0; /* invalid entry */
3534 default:
3535 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
3536 return 0; /* invalid type */
3537 if (!spec->line_in_auto_switch &&
3538 cfg->inputs[i].type != AUTO_PIN_MIC)
3539 return 0; /* only mic is allowed */
3540 if (!is_jack_detectable(codec, nid))
3541 return 0; /* no unsol support */
3542 break;
3543 }
3544 if (num_pins >= MAX_AUTO_MIC_PINS)
3545 return 0;
3546 types |= (1 << attr);
3547 spec->am_entry[num_pins].pin = nid;
3548 spec->am_entry[num_pins].attr = attr;
3549 num_pins++;
3550 }
3551
3552 if (num_pins < 2)
3553 return 0;
3554
3555 spec->am_num_entries = num_pins;
3556 /* sort the am_entry in the order of attr so that the pin with a
3557 * higher attr will be selected when the jack is plugged.
3558 */
3559 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
3560 compare_attr, NULL);
3561
3562 if (!auto_mic_check_imux(codec))
3563 return 0;
3564
3565 spec->auto_mic = 1;
3566 spec->num_adc_nids = 1;
3567 spec->cur_mux[0] = spec->am_entry[0].idx;
3568 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
3569 spec->am_entry[0].pin,
3570 spec->am_entry[1].pin,
3571 spec->am_entry[2].pin);
3572
3573 return 0;
3574}
3575
3576
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003577/*
3578 * Parse the given BIOS configuration and set up the hda_gen_spec
3579 *
3580 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003581 * or a negative error code
3582 */
3583int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003584 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003585{
3586 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003587 int err;
3588
Takashi Iwai1c70a582013-01-11 17:48:22 +01003589 parse_user_hints(codec);
3590
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003591 if (cfg != &spec->autocfg) {
3592 spec->autocfg = *cfg;
3593 cfg = &spec->autocfg;
3594 }
3595
David Henningsson6fc4cb92013-01-16 15:58:45 +01003596 fill_all_dac_nids(codec);
3597
Takashi Iwai352f7f92012-12-19 12:52:06 +01003598 if (!cfg->line_outs) {
3599 if (cfg->dig_outs || cfg->dig_in_pin) {
3600 spec->multiout.max_channels = 2;
3601 spec->no_analog = 1;
3602 goto dig_only;
3603 }
3604 return 0; /* can't find valid BIOS pin config */
3605 }
3606
3607 if (!spec->no_primary_hp &&
3608 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
3609 cfg->line_outs <= cfg->hp_outs) {
3610 /* use HP as primary out */
3611 cfg->speaker_outs = cfg->line_outs;
3612 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3613 sizeof(cfg->speaker_pins));
3614 cfg->line_outs = cfg->hp_outs;
3615 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
3616 cfg->hp_outs = 0;
3617 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3618 cfg->line_out_type = AUTO_PIN_HP_OUT;
3619 }
3620
3621 err = parse_output_paths(codec);
3622 if (err < 0)
3623 return err;
3624 err = create_multi_channel_mode(codec);
3625 if (err < 0)
3626 return err;
3627 err = create_multi_out_ctls(codec, cfg);
3628 if (err < 0)
3629 return err;
3630 err = create_hp_out_ctls(codec);
3631 if (err < 0)
3632 return err;
3633 err = create_speaker_out_ctls(codec);
3634 if (err < 0)
3635 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003636 err = create_indep_hp_ctls(codec);
3637 if (err < 0)
3638 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01003639 err = create_loopback_mixing_ctl(codec);
3640 if (err < 0)
3641 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003642 err = create_shared_input(codec);
3643 if (err < 0)
3644 return err;
3645 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003646 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02003647 return err;
3648
Takashi Iwaia07a9492013-01-07 16:44:06 +01003649 spec->const_channel_count = spec->ext_channel_count;
3650 /* check the multiple speaker and headphone pins */
3651 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
3652 spec->const_channel_count = max(spec->const_channel_count,
3653 cfg->speaker_outs * 2);
3654 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
3655 spec->const_channel_count = max(spec->const_channel_count,
3656 cfg->hp_outs * 2);
3657 spec->multiout.max_channels = max(spec->ext_channel_count,
3658 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003659
3660 err = check_auto_mute_availability(codec);
3661 if (err < 0)
3662 return err;
3663
3664 err = check_dyn_adc_switch(codec);
3665 if (err < 0)
3666 return err;
3667
3668 if (!spec->shared_mic_hp) {
3669 err = check_auto_mic_availability(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003670 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 }
Takashi Iwai071c73a2006-08-23 18:34:06 +02003673
Takashi Iwai352f7f92012-12-19 12:52:06 +01003674 err = create_capture_mixers(codec);
3675 if (err < 0)
3676 return err;
3677
3678 err = parse_mic_boost(codec);
3679 if (err < 0)
3680 return err;
3681
Takashi Iwai978e77e2013-01-10 16:57:58 +01003682 if (spec->add_out_jack_modes) {
3683 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
3684 err = create_out_jack_modes(codec, cfg->line_outs,
3685 cfg->line_out_pins);
3686 if (err < 0)
3687 return err;
3688 }
3689 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
3690 err = create_out_jack_modes(codec, cfg->hp_outs,
3691 cfg->hp_pins);
3692 if (err < 0)
3693 return err;
3694 }
3695 }
3696
Takashi Iwai352f7f92012-12-19 12:52:06 +01003697 dig_only:
3698 parse_digital(codec);
3699
3700 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003702EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703
3704
3705/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003706 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003708
3709/* slave controls for virtual master */
3710static const char * const slave_pfxs[] = {
3711 "Front", "Surround", "Center", "LFE", "Side",
3712 "Headphone", "Speaker", "Mono", "Line Out",
3713 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01003714 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
3715 "Headphone Front", "Headphone Surround", "Headphone CLFE",
3716 "Headphone Side",
Takashi Iwai352f7f92012-12-19 12:52:06 +01003717 NULL,
3718};
3719
3720int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003722 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724
Takashi Iwai36502d02012-12-19 15:15:10 +01003725 if (spec->kctls.used) {
3726 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
3727 if (err < 0)
3728 return err;
3729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730
Takashi Iwai352f7f92012-12-19 12:52:06 +01003731 if (spec->multiout.dig_out_nid) {
3732 err = snd_hda_create_dig_out_ctls(codec,
3733 spec->multiout.dig_out_nid,
3734 spec->multiout.dig_out_nid,
3735 spec->pcm_rec[1].pcm_type);
3736 if (err < 0)
3737 return err;
3738 if (!spec->no_analog) {
3739 err = snd_hda_create_spdif_share_sw(codec,
3740 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741 if (err < 0)
3742 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003743 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 }
3745 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003746 if (spec->dig_in_nid) {
3747 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
3748 if (err < 0)
3749 return err;
3750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751
Takashi Iwai352f7f92012-12-19 12:52:06 +01003752 /* if we have no master control, let's create it */
3753 if (!spec->no_analog &&
3754 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
3755 unsigned int vmaster_tlv[4];
3756 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
3757 HDA_OUTPUT, vmaster_tlv);
3758 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
3759 vmaster_tlv, slave_pfxs,
3760 "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);