blob: 258fb5ee75c59bcc87c70d21d12a3096bf27d18a [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,
Takashi Iwai8999bf02013-01-18 11:01:33 +0100318 int dir, int idx, int type)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100319{
320 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
Takashi Iwai8999bf02013-01-18 11:01:33 +0100321 return is_ctl_used(codec, val, type);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100322}
323
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100324static void print_nid_path(const char *pfx, struct nid_path *path)
325{
326 char buf[40];
327 int i;
328
329
330 buf[0] = 0;
331 for (i = 0; i < path->depth; i++) {
332 char tmp[4];
333 sprintf(tmp, ":%02x", path->path[i]);
334 strlcat(buf, tmp, sizeof(buf));
335 }
336 snd_printdd("%s path: depth=%d %s\n", pfx, path->depth, buf);
337}
338
Takashi Iwai352f7f92012-12-19 12:52:06 +0100339/* called recursively */
340static bool __parse_nid_path(struct hda_codec *codec,
341 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100342 int anchor_nid, struct nid_path *path,
343 int depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100344{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100345 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100346 int i, nums;
347
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100348 if (to_nid == anchor_nid)
349 anchor_nid = 0; /* anchor passed */
350 else if (to_nid == (hda_nid_t)(-anchor_nid))
351 return false; /* hit the exclusive nid */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100352
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100353 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100354 for (i = 0; i < nums; i++) {
355 if (conn[i] != from_nid) {
356 /* special case: when from_nid is 0,
357 * try to find an empty DAC
358 */
359 if (from_nid ||
360 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
361 is_dac_already_used(codec, conn[i]))
362 continue;
363 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100364 /* anchor is not requested or already passed? */
365 if (anchor_nid <= 0)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100366 goto found;
367 }
368 if (depth >= MAX_NID_PATH_DEPTH)
369 return false;
370 for (i = 0; i < nums; i++) {
371 unsigned int type;
372 type = get_wcaps_type(get_wcaps(codec, conn[i]));
373 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
374 type == AC_WID_PIN)
375 continue;
376 if (__parse_nid_path(codec, from_nid, conn[i],
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100377 anchor_nid, path, depth + 1))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100378 goto found;
379 }
380 return false;
381
382 found:
383 path->path[path->depth] = conn[i];
384 path->idx[path->depth + 1] = i;
385 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
386 path->multi[path->depth + 1] = 1;
387 path->depth++;
388 return true;
389}
390
391/* parse the widget path from the given nid to the target nid;
392 * when @from_nid is 0, try to find an empty DAC;
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100393 * when @anchor_nid is set to a positive value, only paths through the widget
394 * with the given value are evaluated.
395 * when @anchor_nid is set to a negative value, paths through the widget
396 * with the negative of given value are excluded, only other paths are chosen.
397 * when @anchor_nid is zero, no special handling about path selection.
Takashi Iwai352f7f92012-12-19 12:52:06 +0100398 */
399bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100400 hda_nid_t to_nid, int anchor_nid,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100401 struct nid_path *path)
402{
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100403 if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100404 path->path[path->depth] = to_nid;
405 path->depth++;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100406 return true;
407 }
408 return false;
409}
410EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100413 * parse the path between the given NIDs and add to the path list.
414 * if no valid path is found, return NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100416struct nid_path *
417snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100418 hda_nid_t to_nid, int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100420 struct hda_gen_spec *spec = codec->spec;
421 struct nid_path *path;
422
423 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
424 return NULL;
425
Takashi Iwaif5172a72013-01-04 13:19:55 +0100426 /* check whether the path has been already added */
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100427 path = get_nid_path(codec, from_nid, to_nid, anchor_nid);
Takashi Iwaif5172a72013-01-04 13:19:55 +0100428 if (path)
429 return path;
430
Takashi Iwai352f7f92012-12-19 12:52:06 +0100431 path = snd_array_new(&spec->paths);
432 if (!path)
433 return NULL;
434 memset(path, 0, sizeof(*path));
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100435 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100436 return path;
437 /* push back */
438 spec->paths.used--;
439 return NULL;
440}
441EXPORT_SYMBOL_HDA(snd_hda_add_new_path);
442
Takashi Iwai980428c2013-01-09 09:28:20 +0100443/* clear the given path as invalid so that it won't be picked up later */
444static void invalidate_nid_path(struct hda_codec *codec, int idx)
445{
446 struct nid_path *path = snd_hda_get_path_from_idx(codec, idx);
447 if (!path)
448 return;
449 memset(path, 0, sizeof(*path));
450}
451
Takashi Iwai352f7f92012-12-19 12:52:06 +0100452/* look for an empty DAC slot */
453static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
454 bool is_digital)
455{
456 struct hda_gen_spec *spec = codec->spec;
457 bool cap_digital;
458 int i;
459
460 for (i = 0; i < spec->num_all_dacs; i++) {
461 hda_nid_t nid = spec->all_dacs[i];
462 if (!nid || is_dac_already_used(codec, nid))
463 continue;
464 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
465 if (is_digital != cap_digital)
466 continue;
467 if (is_reachable_path(codec, nid, pin))
468 return nid;
469 }
470 return 0;
471}
472
473/* replace the channels in the composed amp value with the given number */
474static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
475{
476 val &= ~(0x3U << 16);
477 val |= chs << 16;
478 return val;
479}
480
481/* check whether the widget has the given amp capability for the direction */
482static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
483 int dir, unsigned int bits)
484{
485 if (!nid)
486 return false;
487 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
488 if (query_amp_caps(codec, nid, dir) & bits)
489 return true;
490 return false;
491}
492
David Henningsson99a55922013-01-16 15:58:44 +0100493static bool same_amp_caps(struct hda_codec *codec, hda_nid_t nid1,
494 hda_nid_t nid2, int dir)
495{
496 if (!(get_wcaps(codec, nid1) & (1 << (dir + 1))))
497 return !(get_wcaps(codec, nid2) & (1 << (dir + 1)));
498 return (query_amp_caps(codec, nid1, dir) ==
499 query_amp_caps(codec, nid2, dir));
500}
501
Takashi Iwai352f7f92012-12-19 12:52:06 +0100502#define nid_has_mute(codec, nid, dir) \
503 check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
504#define nid_has_volume(codec, nid, dir) \
505 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
506
507/* look for a widget suitable for assigning a mute switch in the path */
508static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
509 struct nid_path *path)
510{
511 int i;
512
513 for (i = path->depth - 1; i >= 0; i--) {
514 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
515 return path->path[i];
516 if (i != path->depth - 1 && i != 0 &&
517 nid_has_mute(codec, path->path[i], HDA_INPUT))
518 return path->path[i];
519 }
520 return 0;
521}
522
523/* look for a widget suitable for assigning a volume ctl in the path */
524static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
525 struct nid_path *path)
526{
527 int i;
528
529 for (i = path->depth - 1; i >= 0; i--) {
530 if (nid_has_volume(codec, path->path[i], HDA_OUTPUT))
531 return path->path[i];
532 }
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200533 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
535
536/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100537 * path activation / deactivation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100539
540/* can have the amp-in capability? */
541static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100543 hda_nid_t nid = path->path[idx];
544 unsigned int caps = get_wcaps(codec, nid);
545 unsigned int type = get_wcaps_type(caps);
546
547 if (!(caps & AC_WCAP_IN_AMP))
548 return false;
549 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
550 return false;
551 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}
553
Takashi Iwai352f7f92012-12-19 12:52:06 +0100554/* can have the amp-out capability? */
555static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100557 hda_nid_t nid = path->path[idx];
558 unsigned int caps = get_wcaps(codec, nid);
559 unsigned int type = get_wcaps_type(caps);
560
561 if (!(caps & AC_WCAP_OUT_AMP))
562 return false;
563 if (type == AC_WID_PIN && !idx) /* only for output pins */
564 return false;
565 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566}
567
Takashi Iwai352f7f92012-12-19 12:52:06 +0100568/* check whether the given (nid,dir,idx) is active */
569static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
570 unsigned int idx, unsigned int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100572 struct hda_gen_spec *spec = codec->spec;
573 int i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Takashi Iwai352f7f92012-12-19 12:52:06 +0100575 for (n = 0; n < spec->paths.used; n++) {
576 struct nid_path *path = snd_array_elem(&spec->paths, n);
577 if (!path->active)
578 continue;
579 for (i = 0; i < path->depth; i++) {
580 if (path->path[i] == nid) {
581 if (dir == HDA_OUTPUT || path->idx[i] == idx)
582 return true;
583 break;
584 }
585 }
586 }
587 return false;
588}
589
590/* get the default amp value for the target state */
591static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai8999bf02013-01-18 11:01:33 +0100592 int dir, unsigned int caps, bool enable)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100593{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100594 unsigned int val = 0;
595
Takashi Iwai352f7f92012-12-19 12:52:06 +0100596 if (caps & AC_AMPCAP_NUM_STEPS) {
597 /* set to 0dB */
598 if (enable)
599 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
600 }
601 if (caps & AC_AMPCAP_MUTE) {
602 if (!enable)
603 val |= HDA_AMP_MUTE;
604 }
605 return val;
606}
607
608/* initialize the amp value (only at the first time) */
609static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
610{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100611 unsigned int caps = query_amp_caps(codec, nid, dir);
612 int val = get_amp_val_to_activate(codec, nid, dir, caps, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100613 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
614}
615
Takashi Iwai8999bf02013-01-18 11:01:33 +0100616/* calculate amp value mask we can modify;
617 * if the given amp is controlled by mixers, don't touch it
618 */
619static unsigned int get_amp_mask_to_modify(struct hda_codec *codec,
620 hda_nid_t nid, int dir, int idx,
621 unsigned int caps)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100622{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100623 unsigned int mask = 0xff;
624
625 if (caps & AC_AMPCAP_MUTE) {
626 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_MUTE_CTL))
627 mask &= ~0x80;
628 }
629 if (caps & AC_AMPCAP_NUM_STEPS) {
630 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
631 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
632 mask &= ~0x7f;
633 }
634 return mask;
635}
636
637static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
638 int idx, int idx_to_check, bool enable)
639{
640 unsigned int caps;
641 unsigned int mask, val;
642
643 if (!enable && is_active_nid(codec, nid, dir, idx))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100644 return;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100645
646 caps = query_amp_caps(codec, nid, dir);
647 val = get_amp_val_to_activate(codec, nid, dir, caps, enable);
648 mask = get_amp_mask_to_modify(codec, nid, dir, idx_to_check, caps);
649 if (!mask)
650 return;
651
652 val &= mask;
653 snd_hda_codec_amp_stereo(codec, nid, dir, idx, mask, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100654}
655
656static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
657 int i, bool enable)
658{
659 hda_nid_t nid = path->path[i];
660 init_amp(codec, nid, HDA_OUTPUT, 0);
Takashi Iwai8999bf02013-01-18 11:01:33 +0100661 activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100662}
663
664static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
665 int i, bool enable, bool add_aamix)
666{
667 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100668 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100669 int n, nums, idx;
670 int type;
671 hda_nid_t nid = path->path[i];
672
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100673 nums = snd_hda_get_conn_list(codec, nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100674 type = get_wcaps_type(get_wcaps(codec, nid));
675 if (type == AC_WID_PIN ||
676 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
677 nums = 1;
678 idx = 0;
679 } else
680 idx = path->idx[i];
681
682 for (n = 0; n < nums; n++)
683 init_amp(codec, nid, HDA_INPUT, n);
684
Takashi Iwai352f7f92012-12-19 12:52:06 +0100685 /* here is a little bit tricky in comparison with activate_amp_out();
686 * when aa-mixer is available, we need to enable the path as well
687 */
688 for (n = 0; n < nums; n++) {
689 if (n != idx && (!add_aamix || conn[n] != spec->mixer_nid))
690 continue;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100691 activate_amp(codec, nid, HDA_INPUT, n, idx, enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 }
693}
694
Takashi Iwai352f7f92012-12-19 12:52:06 +0100695/* activate or deactivate the given path
696 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100698void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
699 bool enable, bool add_aamix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100701 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Takashi Iwai352f7f92012-12-19 12:52:06 +0100703 if (!enable)
704 path->active = false;
705
706 for (i = path->depth - 1; i >= 0; i--) {
707 if (enable && path->multi[i])
708 snd_hda_codec_write_cache(codec, path->path[i], 0,
709 AC_VERB_SET_CONNECT_SEL,
710 path->idx[i]);
711 if (has_amp_in(codec, path, i))
712 activate_amp_in(codec, path, i, enable, add_aamix);
713 if (has_amp_out(codec, path, i))
714 activate_amp_out(codec, path, i, enable);
715 }
716
717 if (enable)
718 path->active = true;
719}
720EXPORT_SYMBOL_HDA(snd_hda_activate_path);
721
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100722/* turn on/off EAPD on the given pin */
723static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
724{
725 struct hda_gen_spec *spec = codec->spec;
726 if (spec->own_eapd_ctl ||
727 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
728 return;
Takashi Iwaiecac3ed2012-12-21 15:23:01 +0100729 if (codec->inv_eapd)
730 enable = !enable;
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100731 snd_hda_codec_update_cache(codec, pin, 0,
732 AC_VERB_SET_EAPD_BTLENABLE,
733 enable ? 0x02 : 0x00);
734}
735
Takashi Iwai352f7f92012-12-19 12:52:06 +0100736
737/*
738 * Helper functions for creating mixer ctl elements
739 */
740
741enum {
742 HDA_CTL_WIDGET_VOL,
743 HDA_CTL_WIDGET_MUTE,
744 HDA_CTL_BIND_MUTE,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100745};
746static const struct snd_kcontrol_new control_templates[] = {
747 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
748 HDA_CODEC_MUTE(NULL, 0, 0, 0),
749 HDA_BIND_MUTE(NULL, 0, 0, 0),
Takashi Iwai352f7f92012-12-19 12:52:06 +0100750};
751
752/* add dynamic controls from template */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100753static struct snd_kcontrol_new *
754add_control(struct hda_gen_spec *spec, int type, const char *name,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100755 int cidx, unsigned long val)
756{
757 struct snd_kcontrol_new *knew;
758
Takashi Iwai12c93df2012-12-19 14:38:33 +0100759 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100760 if (!knew)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100761 return NULL;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100762 knew->index = cidx;
763 if (get_amp_nid_(val))
764 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
765 knew->private_value = val;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100766 return knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100767}
768
769static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
770 const char *pfx, const char *dir,
771 const char *sfx, int cidx, unsigned long val)
772{
773 char name[32];
774 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100775 if (!add_control(spec, type, name, cidx, val))
776 return -ENOMEM;
777 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100778}
779
780#define add_pb_vol_ctrl(spec, type, pfx, val) \
781 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
782#define add_pb_sw_ctrl(spec, type, pfx, val) \
783 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
784#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
785 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
786#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
787 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
788
789static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
790 unsigned int chs, struct nid_path *path)
791{
792 unsigned int val;
793 if (!path)
794 return 0;
795 val = path->ctls[NID_PATH_VOL_CTL];
796 if (!val)
797 return 0;
798 val = amp_val_replace_channels(val, chs);
799 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
800}
801
802/* return the channel bits suitable for the given path->ctls[] */
803static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
804 int type)
805{
806 int chs = 1; /* mono (left only) */
807 if (path) {
808 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
809 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
810 chs = 3; /* stereo */
811 }
812 return chs;
813}
814
815static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
816 struct nid_path *path)
817{
818 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
819 return add_vol_ctl(codec, pfx, cidx, chs, path);
820}
821
822/* create a mute-switch for the given mixer widget;
823 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
824 */
825static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
826 unsigned int chs, struct nid_path *path)
827{
828 unsigned int val;
829 int type = HDA_CTL_WIDGET_MUTE;
830
831 if (!path)
832 return 0;
833 val = path->ctls[NID_PATH_MUTE_CTL];
834 if (!val)
835 return 0;
836 val = amp_val_replace_channels(val, chs);
837 if (get_amp_direction_(val) == HDA_INPUT) {
838 hda_nid_t nid = get_amp_nid_(val);
839 int nums = snd_hda_get_num_conns(codec, nid);
840 if (nums > 1) {
841 type = HDA_CTL_BIND_MUTE;
842 val |= nums << 19;
843 }
844 }
845 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
846}
847
848static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
849 int cidx, struct nid_path *path)
850{
851 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
852 return add_sw_ctl(codec, pfx, cidx, chs, path);
853}
854
Takashi Iwai247d85e2013-01-17 16:18:11 +0100855/* any ctl assigned to the path with the given index? */
856static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
857{
858 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
859 return path && path->ctls[ctl_type];
860}
861
Takashi Iwai352f7f92012-12-19 12:52:06 +0100862static const char * const channel_name[4] = {
863 "Front", "Surround", "CLFE", "Side"
864};
865
866/* give some appropriate ctl name prefix for the given line out channel */
Takashi Iwai247d85e2013-01-17 16:18:11 +0100867static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
868 int *index, int ctl_type)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100869{
Takashi Iwai247d85e2013-01-17 16:18:11 +0100870 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100871 struct auto_pin_cfg *cfg = &spec->autocfg;
872
873 *index = 0;
874 if (cfg->line_outs == 1 && !spec->multi_ios &&
Takashi Iwai247d85e2013-01-17 16:18:11 +0100875 !cfg->hp_outs && !cfg->speaker_outs)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100876 return spec->vmaster_mute.hook ? "PCM" : "Master";
877
878 /* if there is really a single DAC used in the whole output paths,
879 * use it master (or "PCM" if a vmaster hook is present)
880 */
881 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
882 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
883 return spec->vmaster_mute.hook ? "PCM" : "Master";
884
Takashi Iwai247d85e2013-01-17 16:18:11 +0100885 /* multi-io channels */
886 if (ch >= cfg->line_outs)
887 return channel_name[ch];
888
Takashi Iwai352f7f92012-12-19 12:52:06 +0100889 switch (cfg->line_out_type) {
890 case AUTO_PIN_SPEAKER_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +0100891 /* if the primary channel vol/mute is shared with HP volume,
892 * don't name it as Speaker
893 */
894 if (!ch && cfg->hp_outs &&
895 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
896 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100897 if (cfg->line_outs == 1)
898 return "Speaker";
899 if (cfg->line_outs == 2)
900 return ch ? "Bass Speaker" : "Speaker";
901 break;
902 case AUTO_PIN_HP_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +0100903 /* if the primary channel vol/mute is shared with spk volume,
904 * don't name it as Headphone
905 */
906 if (!ch && cfg->speaker_outs &&
907 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
908 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100909 /* for multi-io case, only the primary out */
910 if (ch && spec->multi_ios)
911 break;
912 *index = ch;
913 return "Headphone";
Takashi Iwai352f7f92012-12-19 12:52:06 +0100914 }
Takashi Iwai247d85e2013-01-17 16:18:11 +0100915
916 /* for a single channel output, we don't have to name the channel */
917 if (cfg->line_outs == 1 && !spec->multi_ios)
918 return "PCM";
919
Takashi Iwai352f7f92012-12-19 12:52:06 +0100920 if (ch >= ARRAY_SIZE(channel_name)) {
921 snd_BUG();
922 return "PCM";
923 }
924
925 return channel_name[ch];
926}
927
928/*
929 * Parse output paths
930 */
931
932/* badness definition */
933enum {
934 /* No primary DAC is found for the main output */
935 BAD_NO_PRIMARY_DAC = 0x10000,
936 /* No DAC is found for the extra output */
937 BAD_NO_DAC = 0x4000,
938 /* No possible multi-ios */
939 BAD_MULTI_IO = 0x103,
940 /* No individual DAC for extra output */
941 BAD_NO_EXTRA_DAC = 0x102,
942 /* No individual DAC for extra surrounds */
943 BAD_NO_EXTRA_SURR_DAC = 0x101,
944 /* Primary DAC shared with main surrounds */
945 BAD_SHARED_SURROUND = 0x100,
946 /* Primary DAC shared with main CLFE */
947 BAD_SHARED_CLFE = 0x10,
948 /* Primary DAC shared with extra surrounds */
949 BAD_SHARED_EXTRA_SURROUND = 0x10,
950 /* Volume widget is shared */
951 BAD_SHARED_VOL = 0x10,
952};
953
Takashi Iwai0e614dd2013-01-07 15:11:44 +0100954/* look for widgets in the given path which are appropriate for
Takashi Iwai352f7f92012-12-19 12:52:06 +0100955 * volume and mute controls, and assign the values to ctls[].
956 *
957 * When no appropriate widget is found in the path, the badness value
958 * is incremented depending on the situation. The function returns the
959 * total badness for both volume and mute controls.
960 */
Takashi Iwai0e614dd2013-01-07 15:11:44 +0100961static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100962{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100963 hda_nid_t nid;
964 unsigned int val;
965 int badness = 0;
966
967 if (!path)
968 return BAD_SHARED_VOL * 2;
Takashi Iwai0e614dd2013-01-07 15:11:44 +0100969
970 if (path->ctls[NID_PATH_VOL_CTL] ||
971 path->ctls[NID_PATH_MUTE_CTL])
972 return 0; /* already evaluated */
973
Takashi Iwai352f7f92012-12-19 12:52:06 +0100974 nid = look_for_out_vol_nid(codec, path);
975 if (nid) {
976 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
977 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
978 badness += BAD_SHARED_VOL;
979 else
980 path->ctls[NID_PATH_VOL_CTL] = val;
981 } else
982 badness += BAD_SHARED_VOL;
983 nid = look_for_out_mute_nid(codec, path);
984 if (nid) {
985 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
986 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
987 nid_has_mute(codec, nid, HDA_OUTPUT))
988 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
989 else
990 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
991 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
992 badness += BAD_SHARED_VOL;
993 else
994 path->ctls[NID_PATH_MUTE_CTL] = val;
995 } else
996 badness += BAD_SHARED_VOL;
997 return badness;
998}
999
1000struct badness_table {
1001 int no_primary_dac; /* no primary DAC */
1002 int no_dac; /* no secondary DACs */
1003 int shared_primary; /* primary DAC is shared with main output */
1004 int shared_surr; /* secondary DAC shared with main or primary */
1005 int shared_clfe; /* third DAC shared with main or primary */
1006 int shared_surr_main; /* secondary DAC sahred with main/DAC0 */
1007};
1008
1009static struct badness_table main_out_badness = {
1010 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1011 .no_dac = BAD_NO_DAC,
1012 .shared_primary = BAD_NO_PRIMARY_DAC,
1013 .shared_surr = BAD_SHARED_SURROUND,
1014 .shared_clfe = BAD_SHARED_CLFE,
1015 .shared_surr_main = BAD_SHARED_SURROUND,
1016};
1017
1018static struct badness_table extra_out_badness = {
1019 .no_primary_dac = BAD_NO_DAC,
1020 .no_dac = BAD_NO_DAC,
1021 .shared_primary = BAD_NO_EXTRA_DAC,
1022 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1023 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1024 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1025};
1026
Takashi Iwai7385df62013-01-07 09:50:52 +01001027/* get the DAC of the primary output corresponding to the given array index */
1028static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1029{
1030 struct hda_gen_spec *spec = codec->spec;
1031 struct auto_pin_cfg *cfg = &spec->autocfg;
1032
1033 if (cfg->line_outs > idx)
1034 return spec->private_dac_nids[idx];
1035 idx -= cfg->line_outs;
1036 if (spec->multi_ios > idx)
1037 return spec->multi_io[idx].dac;
1038 return 0;
1039}
1040
1041/* return the DAC if it's reachable, otherwise zero */
1042static inline hda_nid_t try_dac(struct hda_codec *codec,
1043 hda_nid_t dac, hda_nid_t pin)
1044{
1045 return is_reachable_path(codec, dac, pin) ? dac : 0;
1046}
1047
Takashi Iwai352f7f92012-12-19 12:52:06 +01001048/* try to assign DACs to pins and return the resultant badness */
1049static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1050 const hda_nid_t *pins, hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001051 int *path_idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001052 const struct badness_table *bad)
1053{
1054 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001055 int i, j;
1056 int badness = 0;
1057 hda_nid_t dac;
1058
1059 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 return 0;
1061
Takashi Iwai352f7f92012-12-19 12:52:06 +01001062 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001063 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001064 hda_nid_t pin = pins[i];
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001065
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001066 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1067 if (path) {
1068 badness += assign_out_path_ctls(codec, path);
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001069 continue;
1070 }
1071
1072 dacs[i] = look_for_dac(codec, pin, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001073 if (!dacs[i] && !i) {
Takashi Iwai980428c2013-01-09 09:28:20 +01001074 /* try to steal the DAC of surrounds for the front */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001075 for (j = 1; j < num_outs; j++) {
1076 if (is_reachable_path(codec, dacs[j], pin)) {
1077 dacs[0] = dacs[j];
1078 dacs[j] = 0;
Takashi Iwai980428c2013-01-09 09:28:20 +01001079 invalidate_nid_path(codec, path_idx[j]);
Takashi Iwai196c17662013-01-04 15:01:40 +01001080 path_idx[j] = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001081 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 }
1083 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001084 }
1085 dac = dacs[i];
1086 if (!dac) {
Takashi Iwai7385df62013-01-07 09:50:52 +01001087 if (num_outs > 2)
1088 dac = try_dac(codec, get_primary_out(codec, i), pin);
1089 if (!dac)
1090 dac = try_dac(codec, dacs[0], pin);
1091 if (!dac)
1092 dac = try_dac(codec, get_primary_out(codec, i), pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001093 if (dac) {
1094 if (!i)
1095 badness += bad->shared_primary;
1096 else if (i == 1)
1097 badness += bad->shared_surr;
1098 else
1099 badness += bad->shared_clfe;
1100 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1101 dac = spec->private_dac_nids[0];
1102 badness += bad->shared_surr_main;
1103 } else if (!i)
1104 badness += bad->no_primary_dac;
1105 else
1106 badness += bad->no_dac;
1107 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001108 if (!dac)
1109 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001110 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001111 if (!path && !i && spec->mixer_nid) {
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001112 /* try with aamix */
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001113 path = snd_hda_add_new_path(codec, dac, pin, 0);
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001114 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001115 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001116 dac = dacs[i] = 0;
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001117 badness += bad->no_dac;
1118 } else {
Takashi Iwaia7694092013-01-21 10:43:18 +01001119 /* print_nid_path("output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001120 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001121 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001122 badness += assign_out_path_ctls(codec, path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001123 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001124 }
1125
1126 return badness;
1127}
1128
1129/* return NID if the given pin has only a single connection to a certain DAC */
1130static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1131{
1132 struct hda_gen_spec *spec = codec->spec;
1133 int i;
1134 hda_nid_t nid_found = 0;
1135
1136 for (i = 0; i < spec->num_all_dacs; i++) {
1137 hda_nid_t nid = spec->all_dacs[i];
1138 if (!nid || is_dac_already_used(codec, nid))
1139 continue;
1140 if (is_reachable_path(codec, nid, pin)) {
1141 if (nid_found)
1142 return 0;
1143 nid_found = nid;
1144 }
1145 }
1146 return nid_found;
1147}
1148
1149/* check whether the given pin can be a multi-io pin */
1150static bool can_be_multiio_pin(struct hda_codec *codec,
1151 unsigned int location, hda_nid_t nid)
1152{
1153 unsigned int defcfg, caps;
1154
1155 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1156 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1157 return false;
1158 if (location && get_defcfg_location(defcfg) != location)
1159 return false;
1160 caps = snd_hda_query_pin_caps(codec, nid);
1161 if (!(caps & AC_PINCAP_OUT))
1162 return false;
1163 return true;
1164}
1165
Takashi Iwaie22aab72013-01-04 14:50:04 +01001166/* count the number of input pins that are capable to be multi-io */
1167static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1168{
1169 struct hda_gen_spec *spec = codec->spec;
1170 struct auto_pin_cfg *cfg = &spec->autocfg;
1171 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1172 unsigned int location = get_defcfg_location(defcfg);
1173 int type, i;
1174 int num_pins = 0;
1175
1176 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1177 for (i = 0; i < cfg->num_inputs; i++) {
1178 if (cfg->inputs[i].type != type)
1179 continue;
1180 if (can_be_multiio_pin(codec, location,
1181 cfg->inputs[i].pin))
1182 num_pins++;
1183 }
1184 }
1185 return num_pins;
1186}
1187
Takashi Iwai352f7f92012-12-19 12:52:06 +01001188/*
1189 * multi-io helper
1190 *
1191 * When hardwired is set, try to fill ony hardwired pins, and returns
1192 * zero if any pins are filled, non-zero if nothing found.
1193 * When hardwired is off, try to fill possible input pins, and returns
1194 * the badness value.
1195 */
1196static int fill_multi_ios(struct hda_codec *codec,
1197 hda_nid_t reference_pin,
Takashi Iwaie22aab72013-01-04 14:50:04 +01001198 bool hardwired)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001199{
1200 struct hda_gen_spec *spec = codec->spec;
1201 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie22aab72013-01-04 14:50:04 +01001202 int type, i, j, num_pins, old_pins;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001203 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1204 unsigned int location = get_defcfg_location(defcfg);
1205 int badness = 0;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001206 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001207
1208 old_pins = spec->multi_ios;
1209 if (old_pins >= 2)
1210 goto end_fill;
1211
Takashi Iwaie22aab72013-01-04 14:50:04 +01001212 num_pins = count_multiio_pins(codec, reference_pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001213 if (num_pins < 2)
1214 goto end_fill;
1215
Takashi Iwai352f7f92012-12-19 12:52:06 +01001216 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1217 for (i = 0; i < cfg->num_inputs; i++) {
1218 hda_nid_t nid = cfg->inputs[i].pin;
1219 hda_nid_t dac = 0;
1220
1221 if (cfg->inputs[i].type != type)
1222 continue;
1223 if (!can_be_multiio_pin(codec, location, nid))
1224 continue;
1225 for (j = 0; j < spec->multi_ios; j++) {
1226 if (nid == spec->multi_io[j].pin)
1227 break;
1228 }
1229 if (j < spec->multi_ios)
1230 continue;
1231
Takashi Iwai352f7f92012-12-19 12:52:06 +01001232 if (hardwired)
1233 dac = get_dac_if_single(codec, nid);
1234 else if (!dac)
1235 dac = look_for_dac(codec, nid, false);
1236 if (!dac) {
1237 badness++;
1238 continue;
1239 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001240 path = snd_hda_add_new_path(codec, dac, nid,
1241 -spec->mixer_nid);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001242 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001243 badness++;
1244 continue;
1245 }
Takashi Iwaia7694092013-01-21 10:43:18 +01001246 /* print_nid_path("multiio", path); */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001247 spec->multi_io[spec->multi_ios].pin = nid;
1248 spec->multi_io[spec->multi_ios].dac = dac;
Takashi Iwai196c17662013-01-04 15:01:40 +01001249 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1250 snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001251 spec->multi_ios++;
1252 if (spec->multi_ios >= 2)
1253 break;
1254 }
1255 }
1256 end_fill:
1257 if (badness)
1258 badness = BAD_MULTI_IO;
1259 if (old_pins == spec->multi_ios) {
1260 if (hardwired)
1261 return 1; /* nothing found */
1262 else
1263 return badness; /* no badness if nothing found */
1264 }
1265 if (!hardwired && spec->multi_ios < 2) {
1266 /* cancel newly assigned paths */
1267 spec->paths.used -= spec->multi_ios - old_pins;
1268 spec->multi_ios = old_pins;
1269 return badness;
1270 }
1271
1272 /* assign volume and mute controls */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001273 for (i = old_pins; i < spec->multi_ios; i++) {
1274 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1275 badness += assign_out_path_ctls(codec, path);
1276 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001277
1278 return badness;
1279}
1280
1281/* map DACs for all pins in the list if they are single connections */
1282static bool map_singles(struct hda_codec *codec, int outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001283 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001284{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001285 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001286 int i;
1287 bool found = false;
1288 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001289 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001290 hda_nid_t dac;
1291 if (dacs[i])
1292 continue;
1293 dac = get_dac_if_single(codec, pins[i]);
1294 if (!dac)
1295 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001296 path = snd_hda_add_new_path(codec, dac, pins[i],
1297 -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001298 if (!path && !i && spec->mixer_nid)
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001299 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001300 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001301 dacs[i] = dac;
1302 found = true;
Takashi Iwaia7694092013-01-21 10:43:18 +01001303 /* print_nid_path("output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001304 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001305 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001306 }
1307 }
1308 return found;
1309}
1310
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001311/* create a new path including aamix if available, and return its index */
1312static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1313{
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001314 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001315 struct nid_path *path;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001316 hda_nid_t dac, pin;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001317
1318 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001319 if (!path || !path->depth ||
1320 is_nid_contained(path, spec->mixer_nid))
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001321 return 0;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001322 dac = path->path[0];
1323 pin = path->path[path->depth - 1];
1324 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1325 if (!path) {
1326 if (dac != spec->multiout.dac_nids[0])
1327 dac = spec->multiout.dac_nids[0];
1328 else if (spec->multiout.hp_out_nid[0])
1329 dac = spec->multiout.hp_out_nid[0];
1330 else if (spec->multiout.extra_out_nid[0])
1331 dac = spec->multiout.extra_out_nid[0];
1332 if (dac)
1333 path = snd_hda_add_new_path(codec, dac, pin,
1334 spec->mixer_nid);
1335 }
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001336 if (!path)
1337 return 0;
Takashi Iwaia7694092013-01-21 10:43:18 +01001338 /* print_nid_path("output-aamix", path); */
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001339 path->active = false; /* unused as default */
1340 return snd_hda_get_path_idx(codec, path);
1341}
1342
Takashi Iwaia07a9492013-01-07 16:44:06 +01001343/* fill the empty entries in the dac array for speaker/hp with the
1344 * shared dac pointed by the paths
1345 */
1346static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1347 hda_nid_t *dacs, int *path_idx)
1348{
1349 struct nid_path *path;
1350 int i;
1351
1352 for (i = 0; i < num_outs; i++) {
1353 if (dacs[i])
1354 continue;
1355 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1356 if (!path)
1357 continue;
1358 dacs[i] = path->path[0];
1359 }
1360}
1361
Takashi Iwai352f7f92012-12-19 12:52:06 +01001362/* fill in the dac_nids table from the parsed pin configuration */
1363static int fill_and_eval_dacs(struct hda_codec *codec,
1364 bool fill_hardwired,
1365 bool fill_mio_first)
1366{
1367 struct hda_gen_spec *spec = codec->spec;
1368 struct auto_pin_cfg *cfg = &spec->autocfg;
1369 int i, err, badness;
1370
1371 /* set num_dacs once to full for look_for_dac() */
1372 spec->multiout.num_dacs = cfg->line_outs;
1373 spec->multiout.dac_nids = spec->private_dac_nids;
1374 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1375 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1376 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1377 spec->multi_ios = 0;
1378 snd_array_free(&spec->paths);
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001379
1380 /* clear path indices */
1381 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1382 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1383 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1384 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1385 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
Takashi Iwaic697b712013-01-07 17:09:26 +01001386 memset(spec->input_paths, 0, sizeof(spec->input_paths));
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001387 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1388 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1389
Takashi Iwai352f7f92012-12-19 12:52:06 +01001390 badness = 0;
1391
1392 /* fill hard-wired DACs first */
1393 if (fill_hardwired) {
1394 bool mapped;
1395 do {
1396 mapped = map_singles(codec, cfg->line_outs,
1397 cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001398 spec->private_dac_nids,
1399 spec->out_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001400 mapped |= map_singles(codec, cfg->hp_outs,
1401 cfg->hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001402 spec->multiout.hp_out_nid,
1403 spec->hp_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001404 mapped |= map_singles(codec, cfg->speaker_outs,
1405 cfg->speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001406 spec->multiout.extra_out_nid,
1407 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001408 if (fill_mio_first && cfg->line_outs == 1 &&
1409 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001410 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001411 if (!err)
1412 mapped = true;
1413 }
1414 } while (mapped);
1415 }
1416
1417 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001418 spec->private_dac_nids, spec->out_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001419 &main_out_badness);
1420
Takashi Iwai352f7f92012-12-19 12:52:06 +01001421 if (fill_mio_first &&
1422 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1423 /* try to fill multi-io first */
Takashi Iwaie22aab72013-01-04 14:50:04 +01001424 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001425 if (err < 0)
1426 return err;
1427 /* we don't count badness at this stage yet */
1428 }
1429
1430 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1431 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1432 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001433 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001434 &extra_out_badness);
1435 if (err < 0)
1436 return err;
1437 badness += err;
1438 }
1439 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1440 err = try_assign_dacs(codec, cfg->speaker_outs,
1441 cfg->speaker_pins,
1442 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001443 spec->speaker_paths,
1444 &extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001445 if (err < 0)
1446 return err;
1447 badness += err;
1448 }
1449 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001450 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001451 if (err < 0)
1452 return err;
1453 badness += err;
1454 }
Takashi Iwaie22aab72013-01-04 14:50:04 +01001455
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001456 if (spec->mixer_nid) {
1457 spec->aamix_out_paths[0] =
1458 check_aamix_out_path(codec, spec->out_paths[0]);
1459 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1460 spec->aamix_out_paths[1] =
1461 check_aamix_out_path(codec, spec->hp_paths[0]);
1462 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1463 spec->aamix_out_paths[2] =
1464 check_aamix_out_path(codec, spec->speaker_paths[0]);
1465 }
1466
Takashi Iwaie22aab72013-01-04 14:50:04 +01001467 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1468 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1469 spec->multi_ios = 1; /* give badness */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001470
Takashi Iwaia07a9492013-01-07 16:44:06 +01001471 /* re-count num_dacs and squash invalid entries */
1472 spec->multiout.num_dacs = 0;
1473 for (i = 0; i < cfg->line_outs; i++) {
1474 if (spec->private_dac_nids[i])
1475 spec->multiout.num_dacs++;
1476 else {
1477 memmove(spec->private_dac_nids + i,
1478 spec->private_dac_nids + i + 1,
1479 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1480 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1481 }
1482 }
1483
1484 spec->ext_channel_count = spec->min_channel_count =
David Henningssonc0f3b212013-01-16 11:45:37 +01001485 spec->multiout.num_dacs * 2;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001486
Takashi Iwai352f7f92012-12-19 12:52:06 +01001487 if (spec->multi_ios == 2) {
1488 for (i = 0; i < 2; i++)
1489 spec->private_dac_nids[spec->multiout.num_dacs++] =
1490 spec->multi_io[i].dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001491 } else if (spec->multi_ios) {
1492 spec->multi_ios = 0;
1493 badness += BAD_MULTI_IO;
1494 }
1495
Takashi Iwaia07a9492013-01-07 16:44:06 +01001496 /* re-fill the shared DAC for speaker / headphone */
1497 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1498 refill_shared_dacs(codec, cfg->hp_outs,
1499 spec->multiout.hp_out_nid,
1500 spec->hp_paths);
1501 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1502 refill_shared_dacs(codec, cfg->speaker_outs,
1503 spec->multiout.extra_out_nid,
1504 spec->speaker_paths);
1505
Takashi Iwai352f7f92012-12-19 12:52:06 +01001506 return badness;
1507}
1508
1509#define DEBUG_BADNESS
1510
1511#ifdef DEBUG_BADNESS
1512#define debug_badness snd_printdd
1513#else
1514#define debug_badness(...)
1515#endif
1516
Takashi Iwaia7694092013-01-21 10:43:18 +01001517#ifdef DEBUG_BADNESS
1518static inline void print_nid_path_idx(struct hda_codec *codec,
1519 const char *pfx, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001520{
Takashi Iwaia7694092013-01-21 10:43:18 +01001521 struct nid_path *path;
1522
1523 path = snd_hda_get_path_from_idx(codec, idx);
1524 if (path)
1525 print_nid_path(pfx, path);
1526}
1527
1528static void debug_show_configs(struct hda_codec *codec,
1529 struct auto_pin_cfg *cfg)
1530{
1531 struct hda_gen_spec *spec = codec->spec;
1532#ifdef CONFIG_SND_DEBUG_VERBOSE
1533 static const char * const lo_type[3] = { "LO", "SP", "HP" };
1534#endif
1535 int i;
1536
1537 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001538 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001539 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001540 spec->multiout.dac_nids[0],
1541 spec->multiout.dac_nids[1],
1542 spec->multiout.dac_nids[2],
Takashi Iwaia7694092013-01-21 10:43:18 +01001543 spec->multiout.dac_nids[3],
1544 lo_type[cfg->line_out_type]);
1545 for (i = 0; i < cfg->line_outs; i++)
1546 print_nid_path_idx(codec, " out", spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001547 if (spec->multi_ios > 0)
1548 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1549 spec->multi_ios,
1550 spec->multi_io[0].pin, spec->multi_io[1].pin,
1551 spec->multi_io[0].dac, spec->multi_io[1].dac);
Takashi Iwaia7694092013-01-21 10:43:18 +01001552 for (i = 0; i < spec->multi_ios; i++)
1553 print_nid_path_idx(codec, " mio",
1554 spec->out_paths[cfg->line_outs + i]);
1555 if (cfg->hp_outs)
1556 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001557 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001558 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001559 spec->multiout.hp_out_nid[0],
1560 spec->multiout.hp_out_nid[1],
1561 spec->multiout.hp_out_nid[2],
1562 spec->multiout.hp_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001563 for (i = 0; i < cfg->hp_outs; i++)
1564 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1565 if (cfg->speaker_outs)
1566 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001567 cfg->speaker_pins[0], cfg->speaker_pins[1],
1568 cfg->speaker_pins[2], cfg->speaker_pins[3],
1569 spec->multiout.extra_out_nid[0],
1570 spec->multiout.extra_out_nid[1],
1571 spec->multiout.extra_out_nid[2],
1572 spec->multiout.extra_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001573 for (i = 0; i < cfg->speaker_outs; i++)
1574 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1575 for (i = 0; i < 3; i++)
1576 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001577}
Takashi Iwaia7694092013-01-21 10:43:18 +01001578#else
1579#define debug_show_configs(codec, cfg) /* NOP */
1580#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01001581
1582/* find all available DACs of the codec */
1583static void fill_all_dac_nids(struct hda_codec *codec)
1584{
1585 struct hda_gen_spec *spec = codec->spec;
1586 int i;
1587 hda_nid_t nid = codec->start_nid;
1588
1589 spec->num_all_dacs = 0;
1590 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1591 for (i = 0; i < codec->num_nodes; i++, nid++) {
1592 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1593 continue;
1594 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1595 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1596 break;
1597 }
1598 spec->all_dacs[spec->num_all_dacs++] = nid;
1599 }
1600}
1601
1602static int parse_output_paths(struct hda_codec *codec)
1603{
1604 struct hda_gen_spec *spec = codec->spec;
1605 struct auto_pin_cfg *cfg = &spec->autocfg;
1606 struct auto_pin_cfg *best_cfg;
Takashi Iwai9314a582013-01-21 10:49:05 +01001607 unsigned int val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001608 int best_badness = INT_MAX;
1609 int badness;
1610 bool fill_hardwired = true, fill_mio_first = true;
1611 bool best_wired = true, best_mio = true;
1612 bool hp_spk_swapped = false;
1613
Takashi Iwai352f7f92012-12-19 12:52:06 +01001614 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1615 if (!best_cfg)
1616 return -ENOMEM;
1617 *best_cfg = *cfg;
1618
1619 for (;;) {
1620 badness = fill_and_eval_dacs(codec, fill_hardwired,
1621 fill_mio_first);
1622 if (badness < 0) {
1623 kfree(best_cfg);
1624 return badness;
1625 }
1626 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1627 cfg->line_out_type, fill_hardwired, fill_mio_first,
1628 badness);
Takashi Iwaia7694092013-01-21 10:43:18 +01001629 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001630 if (badness < best_badness) {
1631 best_badness = badness;
1632 *best_cfg = *cfg;
1633 best_wired = fill_hardwired;
1634 best_mio = fill_mio_first;
1635 }
1636 if (!badness)
1637 break;
1638 fill_mio_first = !fill_mio_first;
1639 if (!fill_mio_first)
1640 continue;
1641 fill_hardwired = !fill_hardwired;
1642 if (!fill_hardwired)
1643 continue;
1644 if (hp_spk_swapped)
1645 break;
1646 hp_spk_swapped = true;
1647 if (cfg->speaker_outs > 0 &&
1648 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1649 cfg->hp_outs = cfg->line_outs;
1650 memcpy(cfg->hp_pins, cfg->line_out_pins,
1651 sizeof(cfg->hp_pins));
1652 cfg->line_outs = cfg->speaker_outs;
1653 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1654 sizeof(cfg->speaker_pins));
1655 cfg->speaker_outs = 0;
1656 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1657 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1658 fill_hardwired = true;
1659 continue;
1660 }
1661 if (cfg->hp_outs > 0 &&
1662 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1663 cfg->speaker_outs = cfg->line_outs;
1664 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1665 sizeof(cfg->speaker_pins));
1666 cfg->line_outs = cfg->hp_outs;
1667 memcpy(cfg->line_out_pins, cfg->hp_pins,
1668 sizeof(cfg->hp_pins));
1669 cfg->hp_outs = 0;
1670 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1671 cfg->line_out_type = AUTO_PIN_HP_OUT;
1672 fill_hardwired = true;
1673 continue;
1674 }
1675 break;
1676 }
1677
1678 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001679 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001680 *cfg = *best_cfg;
1681 fill_and_eval_dacs(codec, best_wired, best_mio);
1682 }
1683 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1684 cfg->line_out_type, best_wired, best_mio);
Takashi Iwaia7694092013-01-21 10:43:18 +01001685 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001686
1687 if (cfg->line_out_pins[0]) {
1688 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01001689 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001690 if (path)
1691 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01001692 if (spec->vmaster_nid)
1693 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
1694 HDA_OUTPUT, spec->vmaster_tlv);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001695 }
1696
Takashi Iwai9314a582013-01-21 10:49:05 +01001697 /* set initial pinctl targets */
1698 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
1699 val = PIN_HP;
1700 else
1701 val = PIN_OUT;
1702 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
1703 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1704 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
1705 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1706 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
1707 set_pin_targets(codec, cfg->speaker_outs,
1708 cfg->speaker_pins, val);
1709 }
1710
Takashi Iwai352f7f92012-12-19 12:52:06 +01001711 kfree(best_cfg);
1712 return 0;
1713}
1714
1715/* add playback controls from the parsed DAC table */
1716static int create_multi_out_ctls(struct hda_codec *codec,
1717 const struct auto_pin_cfg *cfg)
1718{
1719 struct hda_gen_spec *spec = codec->spec;
1720 int i, err, noutputs;
1721
1722 noutputs = cfg->line_outs;
1723 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1724 noutputs += spec->multi_ios;
1725
1726 for (i = 0; i < noutputs; i++) {
1727 const char *name;
1728 int index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001729 struct nid_path *path;
1730
Takashi Iwai196c17662013-01-04 15:01:40 +01001731 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001732 if (!path)
1733 continue;
Takashi Iwai247d85e2013-01-17 16:18:11 +01001734
1735 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001736 if (!name || !strcmp(name, "CLFE")) {
1737 /* Center/LFE */
1738 err = add_vol_ctl(codec, "Center", 0, 1, path);
1739 if (err < 0)
1740 return err;
1741 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1742 if (err < 0)
1743 return err;
Takashi Iwai247d85e2013-01-17 16:18:11 +01001744 } else {
1745 err = add_stereo_vol(codec, name, index, path);
1746 if (err < 0)
1747 return err;
1748 }
1749
1750 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
1751 if (!name || !strcmp(name, "CLFE")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001752 err = add_sw_ctl(codec, "Center", 0, 1, path);
1753 if (err < 0)
1754 return err;
1755 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1756 if (err < 0)
1757 return err;
1758 } else {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001759 err = add_stereo_sw(codec, name, index, path);
1760 if (err < 0)
1761 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 }
1763 }
1764 return 0;
1765}
1766
Takashi Iwaic2c80382013-01-07 10:33:57 +01001767static int create_extra_out(struct hda_codec *codec, int path_idx,
Takashi Iwai196c17662013-01-04 15:01:40 +01001768 const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001770 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 int err;
1772
Takashi Iwai196c17662013-01-04 15:01:40 +01001773 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001774 if (!path)
1775 return 0;
Takashi Iwaic2c80382013-01-07 10:33:57 +01001776 err = add_stereo_vol(codec, pfx, cidx, path);
1777 if (err < 0)
1778 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001779 err = add_stereo_sw(codec, pfx, cidx, path);
1780 if (err < 0)
1781 return err;
1782 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783}
1784
Takashi Iwai352f7f92012-12-19 12:52:06 +01001785/* add playback controls for speaker and HP outputs */
1786static int create_extra_outs(struct hda_codec *codec, int num_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001787 const int *paths, const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788{
Takashi Iwaic2c80382013-01-07 10:33:57 +01001789 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001790
1791 for (i = 0; i < num_pins; i++) {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001792 const char *name;
1793 char tmp[44];
1794 int err, idx = 0;
1795
1796 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
1797 name = "Bass Speaker";
1798 else if (num_pins >= 3) {
1799 snprintf(tmp, sizeof(tmp), "%s %s",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001800 pfx, channel_name[i]);
Takashi Iwaic2c80382013-01-07 10:33:57 +01001801 name = tmp;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001802 } else {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001803 name = pfx;
1804 idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 }
Takashi Iwaic2c80382013-01-07 10:33:57 +01001806 err = create_extra_out(codec, paths[i], name, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001807 if (err < 0)
1808 return err;
1809 }
1810 return 0;
1811}
Takashi Iwai97ec5582006-03-21 11:29:07 +01001812
Takashi Iwai352f7f92012-12-19 12:52:06 +01001813static int create_hp_out_ctls(struct hda_codec *codec)
1814{
1815 struct hda_gen_spec *spec = codec->spec;
1816 return create_extra_outs(codec, spec->autocfg.hp_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001817 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001818 "Headphone");
1819}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
Takashi Iwai352f7f92012-12-19 12:52:06 +01001821static int create_speaker_out_ctls(struct hda_codec *codec)
1822{
1823 struct hda_gen_spec *spec = codec->spec;
1824 return create_extra_outs(codec, spec->autocfg.speaker_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001825 spec->speaker_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001826 "Speaker");
1827}
1828
1829/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001830 * independent HP controls
1831 */
1832
1833static int indep_hp_info(struct snd_kcontrol *kcontrol,
1834 struct snd_ctl_elem_info *uinfo)
1835{
1836 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
1837}
1838
1839static int indep_hp_get(struct snd_kcontrol *kcontrol,
1840 struct snd_ctl_elem_value *ucontrol)
1841{
1842 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1843 struct hda_gen_spec *spec = codec->spec;
1844 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
1845 return 0;
1846}
1847
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001848static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
1849 int nomix_path_idx, int mix_path_idx,
1850 int out_type);
1851
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001852static int indep_hp_put(struct snd_kcontrol *kcontrol,
1853 struct snd_ctl_elem_value *ucontrol)
1854{
1855 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1856 struct hda_gen_spec *spec = codec->spec;
1857 unsigned int select = ucontrol->value.enumerated.item[0];
1858 int ret = 0;
1859
1860 mutex_lock(&spec->pcm_mutex);
1861 if (spec->active_streams) {
1862 ret = -EBUSY;
1863 goto unlock;
1864 }
1865
1866 if (spec->indep_hp_enabled != select) {
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001867 hda_nid_t *dacp;
1868 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
1869 dacp = &spec->private_dac_nids[0];
1870 else
1871 dacp = &spec->multiout.hp_out_nid[0];
1872
1873 /* update HP aamix paths in case it conflicts with indep HP */
1874 if (spec->have_aamix_ctl) {
1875 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
1876 update_aamix_paths(codec, spec->aamix_mode,
1877 spec->out_paths[0],
1878 spec->aamix_out_paths[0],
1879 spec->autocfg.line_out_type);
1880 else
1881 update_aamix_paths(codec, spec->aamix_mode,
1882 spec->hp_paths[0],
1883 spec->aamix_out_paths[1],
1884 AUTO_PIN_HP_OUT);
1885 }
1886
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001887 spec->indep_hp_enabled = select;
1888 if (spec->indep_hp_enabled)
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001889 *dacp = 0;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001890 else
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001891 *dacp = spec->alt_dac_nid;
Takashi Iwai92603c52013-01-22 07:46:31 +01001892
1893 /* update HP auto-mute state too */
1894 if (spec->hp_automute_hook)
1895 spec->hp_automute_hook(codec, NULL);
1896 else
1897 snd_hda_gen_hp_automute(codec, NULL);
1898
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001899 ret = 1;
1900 }
1901 unlock:
1902 mutex_unlock(&spec->pcm_mutex);
1903 return ret;
1904}
1905
1906static const struct snd_kcontrol_new indep_hp_ctl = {
1907 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1908 .name = "Independent HP",
1909 .info = indep_hp_info,
1910 .get = indep_hp_get,
1911 .put = indep_hp_put,
1912};
1913
1914
1915static int create_indep_hp_ctls(struct hda_codec *codec)
1916{
1917 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001918 hda_nid_t dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001919
1920 if (!spec->indep_hp)
1921 return 0;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001922 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
1923 dac = spec->multiout.dac_nids[0];
1924 else
1925 dac = spec->multiout.hp_out_nid[0];
1926 if (!dac) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001927 spec->indep_hp = 0;
1928 return 0;
1929 }
1930
1931 spec->indep_hp_enabled = false;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001932 spec->alt_dac_nid = dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001933 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
1934 return -ENOMEM;
1935 return 0;
1936}
1937
1938/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01001939 * channel mode enum control
1940 */
1941
1942static int ch_mode_info(struct snd_kcontrol *kcontrol,
1943 struct snd_ctl_elem_info *uinfo)
1944{
1945 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1946 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001947 int chs;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001948
1949 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1950 uinfo->count = 1;
1951 uinfo->value.enumerated.items = spec->multi_ios + 1;
1952 if (uinfo->value.enumerated.item > spec->multi_ios)
1953 uinfo->value.enumerated.item = spec->multi_ios;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001954 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
1955 sprintf(uinfo->value.enumerated.name, "%dch", chs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001956 return 0;
1957}
1958
1959static int ch_mode_get(struct snd_kcontrol *kcontrol,
1960 struct snd_ctl_elem_value *ucontrol)
1961{
1962 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1963 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001964 ucontrol->value.enumerated.item[0] =
1965 (spec->ext_channel_count - spec->min_channel_count) / 2;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001966 return 0;
1967}
1968
Takashi Iwai196c17662013-01-04 15:01:40 +01001969static inline struct nid_path *
1970get_multiio_path(struct hda_codec *codec, int idx)
1971{
1972 struct hda_gen_spec *spec = codec->spec;
1973 return snd_hda_get_path_from_idx(codec,
1974 spec->out_paths[spec->autocfg.line_outs + idx]);
1975}
1976
Takashi Iwaia5cc2502013-01-16 18:08:55 +01001977static void update_automute_all(struct hda_codec *codec);
1978
Takashi Iwai352f7f92012-12-19 12:52:06 +01001979static int set_multi_io(struct hda_codec *codec, int idx, bool output)
1980{
1981 struct hda_gen_spec *spec = codec->spec;
1982 hda_nid_t nid = spec->multi_io[idx].pin;
1983 struct nid_path *path;
1984
Takashi Iwai196c17662013-01-04 15:01:40 +01001985 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001986 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001988
1989 if (path->active == output)
1990 return 0;
1991
1992 if (output) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01001993 set_pin_target(codec, nid, PIN_OUT, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001994 snd_hda_activate_path(codec, path, true, true);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01001995 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001996 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01001997 set_pin_eapd(codec, nid, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001998 snd_hda_activate_path(codec, path, false, true);
Takashi Iwai2c12c302013-01-10 09:33:29 +01001999 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 }
Takashi Iwaia365fed2013-01-10 16:10:06 +01002001
2002 /* update jack retasking in case it modifies any of them */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002003 update_automute_all(codec);
Takashi Iwaia365fed2013-01-10 16:10:06 +01002004
Takashi Iwai352f7f92012-12-19 12:52:06 +01002005 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006}
2007
Takashi Iwai352f7f92012-12-19 12:52:06 +01002008static int ch_mode_put(struct snd_kcontrol *kcontrol,
2009 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002011 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2012 struct hda_gen_spec *spec = codec->spec;
2013 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
Takashi Iwai352f7f92012-12-19 12:52:06 +01002015 ch = ucontrol->value.enumerated.item[0];
2016 if (ch < 0 || ch > spec->multi_ios)
2017 return -EINVAL;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002018 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002019 return 0;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002020 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002021 for (i = 0; i < spec->multi_ios; i++)
2022 set_multi_io(codec, i, i < ch);
2023 spec->multiout.max_channels = max(spec->ext_channel_count,
2024 spec->const_channel_count);
2025 if (spec->need_dac_fix)
2026 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 return 1;
2028}
2029
Takashi Iwai352f7f92012-12-19 12:52:06 +01002030static const struct snd_kcontrol_new channel_mode_enum = {
2031 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2032 .name = "Channel Mode",
2033 .info = ch_mode_info,
2034 .get = ch_mode_get,
2035 .put = ch_mode_put,
2036};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
Takashi Iwai352f7f92012-12-19 12:52:06 +01002038static int create_multi_channel_mode(struct hda_codec *codec)
2039{
2040 struct hda_gen_spec *spec = codec->spec;
2041
2042 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002043 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002044 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 return 0;
2047}
2048
Takashi Iwai352f7f92012-12-19 12:52:06 +01002049/*
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002050 * aamix loopback enable/disable switch
2051 */
2052
2053#define loopback_mixing_info indep_hp_info
2054
2055static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2056 struct snd_ctl_elem_value *ucontrol)
2057{
2058 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2059 struct hda_gen_spec *spec = codec->spec;
2060 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2061 return 0;
2062}
2063
2064static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002065 int nomix_path_idx, int mix_path_idx,
2066 int out_type)
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002067{
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002068 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002069 struct nid_path *nomix_path, *mix_path;
2070
2071 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2072 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2073 if (!nomix_path || !mix_path)
2074 return;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002075
2076 /* if HP aamix path is driven from a different DAC and the
2077 * independent HP mode is ON, can't turn on aamix path
2078 */
2079 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2080 mix_path->path[0] != spec->alt_dac_nid)
2081 do_mix = false;
2082
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002083 if (do_mix) {
2084 snd_hda_activate_path(codec, nomix_path, false, true);
2085 snd_hda_activate_path(codec, mix_path, true, true);
2086 } else {
2087 snd_hda_activate_path(codec, mix_path, false, true);
2088 snd_hda_activate_path(codec, nomix_path, true, true);
2089 }
2090}
2091
2092static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2093 struct snd_ctl_elem_value *ucontrol)
2094{
2095 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2096 struct hda_gen_spec *spec = codec->spec;
2097 unsigned int val = ucontrol->value.enumerated.item[0];
2098
2099 if (val == spec->aamix_mode)
2100 return 0;
2101 spec->aamix_mode = val;
2102 update_aamix_paths(codec, val, spec->out_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002103 spec->aamix_out_paths[0],
2104 spec->autocfg.line_out_type);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002105 update_aamix_paths(codec, val, spec->hp_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002106 spec->aamix_out_paths[1],
2107 AUTO_PIN_HP_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002108 update_aamix_paths(codec, val, spec->speaker_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002109 spec->aamix_out_paths[2],
2110 AUTO_PIN_SPEAKER_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002111 return 1;
2112}
2113
2114static const struct snd_kcontrol_new loopback_mixing_enum = {
2115 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2116 .name = "Loopback Mixing",
2117 .info = loopback_mixing_info,
2118 .get = loopback_mixing_get,
2119 .put = loopback_mixing_put,
2120};
2121
2122static int create_loopback_mixing_ctl(struct hda_codec *codec)
2123{
2124 struct hda_gen_spec *spec = codec->spec;
2125
2126 if (!spec->mixer_nid)
2127 return 0;
2128 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
2129 spec->aamix_out_paths[2]))
2130 return 0;
2131 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2132 return -ENOMEM;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002133 spec->have_aamix_ctl = 1;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002134 return 0;
2135}
2136
2137/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002138 * shared headphone/mic handling
2139 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002140
Takashi Iwai352f7f92012-12-19 12:52:06 +01002141static void call_update_outputs(struct hda_codec *codec);
2142
2143/* for shared I/O, change the pin-control accordingly */
2144static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic)
2145{
2146 struct hda_gen_spec *spec = codec->spec;
2147 unsigned int val;
2148 hda_nid_t pin = spec->autocfg.inputs[1].pin;
2149 /* NOTE: this assumes that there are only two inputs, the
2150 * first is the real internal mic and the second is HP/mic jack.
2151 */
2152
2153 val = snd_hda_get_default_vref(codec, pin);
2154
2155 /* This pin does not have vref caps - let's enable vref on pin 0x18
2156 instead, as suggested by Realtek */
2157 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2158 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2159 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2160 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01002161 snd_hda_set_pin_ctl_cache(codec, vref_pin,
2162 PIN_IN | (set_as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02002163 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002164
2165 val = set_as_mic ? val | PIN_IN : PIN_HP;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002166 set_pin_target(codec, pin, val, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002167
2168 spec->automute_speaker = !set_as_mic;
2169 call_update_outputs(codec);
2170}
2171
2172/* create a shared input with the headphone out */
2173static int create_shared_input(struct hda_codec *codec)
2174{
2175 struct hda_gen_spec *spec = codec->spec;
2176 struct auto_pin_cfg *cfg = &spec->autocfg;
2177 unsigned int defcfg;
2178 hda_nid_t nid;
2179
2180 /* only one internal input pin? */
2181 if (cfg->num_inputs != 1)
2182 return 0;
2183 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2184 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2185 return 0;
2186
2187 if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
2188 nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
2189 else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
2190 nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
2191 else
2192 return 0; /* both not available */
2193
2194 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2195 return 0; /* no input */
2196
2197 cfg->inputs[1].pin = nid;
2198 cfg->inputs[1].type = AUTO_PIN_MIC;
2199 cfg->num_inputs = 2;
2200 spec->shared_mic_hp = 1;
2201 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
2202 return 0;
2203}
2204
Takashi Iwai978e77e2013-01-10 16:57:58 +01002205/*
2206 * output jack mode
2207 */
2208static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2209 struct snd_ctl_elem_info *uinfo)
2210{
2211 static const char * const texts[] = {
2212 "Line Out", "Headphone Out",
2213 };
2214 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, texts);
2215}
2216
2217static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2218 struct snd_ctl_elem_value *ucontrol)
2219{
2220 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2221 hda_nid_t nid = kcontrol->private_value;
2222 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2223 ucontrol->value.enumerated.item[0] = 1;
2224 else
2225 ucontrol->value.enumerated.item[0] = 0;
2226 return 0;
2227}
2228
2229static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2230 struct snd_ctl_elem_value *ucontrol)
2231{
2232 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2233 hda_nid_t nid = kcontrol->private_value;
2234 unsigned int val;
2235
2236 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2237 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2238 return 0;
2239 snd_hda_set_pin_ctl_cache(codec, nid, val);
2240 return 1;
2241}
2242
2243static const struct snd_kcontrol_new out_jack_mode_enum = {
2244 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2245 .info = out_jack_mode_info,
2246 .get = out_jack_mode_get,
2247 .put = out_jack_mode_put,
2248};
2249
2250static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2251{
2252 struct hda_gen_spec *spec = codec->spec;
2253 int i;
2254
2255 for (i = 0; i < spec->kctls.used; i++) {
2256 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2257 if (!strcmp(kctl->name, name) && kctl->index == idx)
2258 return true;
2259 }
2260 return false;
2261}
2262
2263static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2264 char *name, size_t name_len)
2265{
2266 struct hda_gen_spec *spec = codec->spec;
2267 int idx = 0;
2268
2269 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2270 strlcat(name, " Jack Mode", name_len);
2271
2272 for (; find_kctl_name(codec, name, idx); idx++)
2273 ;
2274}
2275
2276static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2277 hda_nid_t *pins)
2278{
2279 struct hda_gen_spec *spec = codec->spec;
2280 int i;
2281
2282 for (i = 0; i < num_pins; i++) {
2283 hda_nid_t pin = pins[i];
2284 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2285 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV)) {
2286 struct snd_kcontrol_new *knew;
2287 char name[44];
2288 get_jack_mode_name(codec, pin, name, sizeof(name));
2289 knew = snd_hda_gen_add_kctl(spec, name,
2290 &out_jack_mode_enum);
2291 if (!knew)
2292 return -ENOMEM;
2293 knew->private_value = pin;
2294 }
2295 }
2296
2297 return 0;
2298}
2299
Takashi Iwai294765582013-01-17 09:52:11 +01002300/*
2301 * input jack mode
2302 */
2303
2304/* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2305#define NUM_VREFS 6
2306
2307static const char * const vref_texts[NUM_VREFS] = {
2308 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2309 "", "Mic 80pc Bias", "Mic 100pc Bias"
2310};
2311
2312static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2313{
2314 unsigned int pincap;
2315
2316 pincap = snd_hda_query_pin_caps(codec, pin);
2317 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2318 /* filter out unusual vrefs */
2319 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2320 return pincap;
2321}
2322
2323/* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2324static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2325{
2326 unsigned int i, n = 0;
2327
2328 for (i = 0; i < NUM_VREFS; i++) {
2329 if (vref_caps & (1 << i)) {
2330 if (n == item_idx)
2331 return i;
2332 n++;
2333 }
2334 }
2335 return 0;
2336}
2337
2338/* convert back from the vref ctl index to the enum item index */
2339static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2340{
2341 unsigned int i, n = 0;
2342
2343 for (i = 0; i < NUM_VREFS; i++) {
2344 if (i == idx)
2345 return n;
2346 if (vref_caps & (1 << i))
2347 n++;
2348 }
2349 return 0;
2350}
2351
2352static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2353 struct snd_ctl_elem_info *uinfo)
2354{
2355 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2356 hda_nid_t nid = kcontrol->private_value;
2357 unsigned int vref_caps = get_vref_caps(codec, nid);
2358
2359 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2360 vref_texts);
2361 /* set the right text */
2362 strcpy(uinfo->value.enumerated.name,
2363 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2364 return 0;
2365}
2366
2367static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2368 struct snd_ctl_elem_value *ucontrol)
2369{
2370 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2371 hda_nid_t nid = kcontrol->private_value;
2372 unsigned int vref_caps = get_vref_caps(codec, nid);
2373 unsigned int idx;
2374
2375 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2376 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2377 return 0;
2378}
2379
2380static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2381 struct snd_ctl_elem_value *ucontrol)
2382{
2383 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2384 hda_nid_t nid = kcontrol->private_value;
2385 unsigned int vref_caps = get_vref_caps(codec, nid);
2386 unsigned int val, idx;
2387
2388 val = snd_hda_codec_get_pin_target(codec, nid);
2389 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2390 if (idx == ucontrol->value.enumerated.item[0])
2391 return 0;
2392
2393 val &= ~AC_PINCTL_VREFEN;
2394 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2395 snd_hda_set_pin_ctl_cache(codec, nid, val);
2396 return 1;
2397}
2398
2399static const struct snd_kcontrol_new in_jack_mode_enum = {
2400 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2401 .info = in_jack_mode_info,
2402 .get = in_jack_mode_get,
2403 .put = in_jack_mode_put,
2404};
2405
2406static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2407{
2408 struct hda_gen_spec *spec = codec->spec;
2409 unsigned int defcfg;
2410 struct snd_kcontrol_new *knew;
2411 char name[44];
2412
2413 /* no jack mode for fixed pins */
2414 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2415 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2416 return 0;
2417
2418 /* no multiple vref caps? */
2419 if (hweight32(get_vref_caps(codec, pin)) <= 1)
2420 return 0;
2421
2422 get_jack_mode_name(codec, pin, name, sizeof(name));
2423 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2424 if (!knew)
2425 return -ENOMEM;
2426 knew->private_value = pin;
2427 return 0;
2428}
2429
Takashi Iwai352f7f92012-12-19 12:52:06 +01002430
2431/*
2432 * Parse input paths
2433 */
2434
2435#ifdef CONFIG_PM
2436/* add the powersave loopback-list entry */
2437static void add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
2438{
2439 struct hda_amp_list *list;
2440
2441 if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
2442 return;
2443 list = spec->loopback_list + spec->num_loopbacks;
2444 list->nid = mix;
2445 list->dir = HDA_INPUT;
2446 list->idx = idx;
2447 spec->num_loopbacks++;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002448 spec->loopback.amplist = spec->loopback_list;
2449}
2450#else
Takashi Iwai352f7f92012-12-19 12:52:06 +01002451#define add_loopback_list(spec, mix, idx) /* NOP */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002452#endif
2453
Takashi Iwai352f7f92012-12-19 12:52:06 +01002454/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01002455static int new_analog_input(struct hda_codec *codec, int input_idx,
2456 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002457 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002459 struct hda_gen_spec *spec = codec->spec;
2460 struct nid_path *path;
2461 unsigned int val;
2462 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463
Takashi Iwai352f7f92012-12-19 12:52:06 +01002464 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
2465 !nid_has_mute(codec, mix_nid, HDA_INPUT))
2466 return 0; /* no need for analog loopback */
2467
Takashi Iwai3ca529d2013-01-07 17:25:08 +01002468 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002469 if (!path)
2470 return -EINVAL;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002471 print_nid_path("loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01002472 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002473
2474 idx = path->idx[path->depth - 1];
2475 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
2476 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2477 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002478 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002480 path->ctls[NID_PATH_VOL_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 }
2482
Takashi Iwai352f7f92012-12-19 12:52:06 +01002483 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
2484 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2485 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002486 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002488 path->ctls[NID_PATH_MUTE_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 }
2490
Takashi Iwai352f7f92012-12-19 12:52:06 +01002491 path->active = true;
2492 add_loopback_list(spec, mix_nid, idx);
2493 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494}
2495
Takashi Iwai352f7f92012-12-19 12:52:06 +01002496static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002498 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
2499 return (pincap & AC_PINCAP_IN) != 0;
2500}
2501
2502/* Parse the codec tree and retrieve ADCs */
2503static int fill_adc_nids(struct hda_codec *codec)
2504{
2505 struct hda_gen_spec *spec = codec->spec;
2506 hda_nid_t nid;
2507 hda_nid_t *adc_nids = spec->adc_nids;
2508 int max_nums = ARRAY_SIZE(spec->adc_nids);
2509 int i, nums = 0;
2510
2511 nid = codec->start_nid;
2512 for (i = 0; i < codec->num_nodes; i++, nid++) {
2513 unsigned int caps = get_wcaps(codec, nid);
2514 int type = get_wcaps_type(caps);
2515
2516 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2517 continue;
2518 adc_nids[nums] = nid;
2519 if (++nums >= max_nums)
2520 break;
2521 }
2522 spec->num_adc_nids = nums;
Takashi Iwai0ffd5342013-01-17 15:53:29 +01002523
2524 /* copy the detected ADCs to all_adcs[] */
2525 spec->num_all_adcs = nums;
2526 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
2527
Takashi Iwai352f7f92012-12-19 12:52:06 +01002528 return nums;
2529}
2530
2531/* filter out invalid adc_nids that don't give all active input pins;
2532 * if needed, check whether dynamic ADC-switching is available
2533 */
2534static int check_dyn_adc_switch(struct hda_codec *codec)
2535{
2536 struct hda_gen_spec *spec = codec->spec;
2537 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002538 unsigned int ok_bits;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002539 int i, n, nums;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002540
2541 again:
2542 nums = 0;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002543 ok_bits = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002544 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002545 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002546 if (!spec->input_paths[i][n])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002547 break;
2548 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002549 if (i >= imux->num_items) {
2550 ok_bits |= (1 << n);
2551 nums++;
2552 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002553 }
2554
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002555 if (!ok_bits) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002556 if (spec->shared_mic_hp) {
2557 spec->shared_mic_hp = 0;
2558 imux->num_items = 1;
2559 goto again;
2560 }
2561
2562 /* check whether ADC-switch is possible */
2563 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002564 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002565 if (spec->input_paths[i][n]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002566 spec->dyn_adc_idx[i] = n;
2567 break;
2568 }
2569 }
2570 }
2571
2572 snd_printdd("hda-codec: enabling ADC switching\n");
2573 spec->dyn_adc_switch = 1;
2574 } else if (nums != spec->num_adc_nids) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002575 /* shrink the invalid adcs and input paths */
2576 nums = 0;
2577 for (n = 0; n < spec->num_adc_nids; n++) {
2578 if (!(ok_bits & (1 << n)))
2579 continue;
2580 if (n != nums) {
2581 spec->adc_nids[nums] = spec->adc_nids[n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002582 for (i = 0; i < imux->num_items; i++) {
2583 invalidate_nid_path(codec,
2584 spec->input_paths[i][nums]);
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002585 spec->input_paths[i][nums] =
2586 spec->input_paths[i][n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002587 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002588 }
2589 nums++;
2590 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002591 spec->num_adc_nids = nums;
2592 }
2593
2594 if (imux->num_items == 1 || spec->shared_mic_hp) {
2595 snd_printdd("hda-codec: reducing to a single ADC\n");
2596 spec->num_adc_nids = 1; /* reduce to a single ADC */
2597 }
2598
2599 /* single index for individual volumes ctls */
2600 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
2601 spec->num_adc_nids = 1;
2602
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 return 0;
2604}
2605
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002606/* parse capture source paths from the given pin and create imux items */
2607static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
Takashi Iwai9dba2052013-01-18 10:01:15 +01002608 int cfg_idx, int num_adcs,
2609 const char *label, int anchor)
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002610{
2611 struct hda_gen_spec *spec = codec->spec;
2612 struct hda_input_mux *imux = &spec->input_mux;
2613 int imux_idx = imux->num_items;
2614 bool imux_added = false;
2615 int c;
2616
2617 for (c = 0; c < num_adcs; c++) {
2618 struct nid_path *path;
2619 hda_nid_t adc = spec->adc_nids[c];
2620
2621 if (!is_reachable_path(codec, pin, adc))
2622 continue;
2623 path = snd_hda_add_new_path(codec, pin, adc, anchor);
2624 if (!path)
2625 continue;
2626 print_nid_path("input", path);
2627 spec->input_paths[imux_idx][c] =
2628 snd_hda_get_path_idx(codec, path);
2629
2630 if (!imux_added) {
2631 spec->imux_pins[imux->num_items] = pin;
Takashi Iwai9dba2052013-01-18 10:01:15 +01002632 snd_hda_add_imux_item(imux, label, cfg_idx, NULL);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002633 imux_added = true;
2634 }
2635 }
2636
2637 return 0;
2638}
2639
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002641 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 */
Takashi Iwai9dba2052013-01-18 10:01:15 +01002643
Takashi Iwaic9700422013-01-18 10:17:30 +01002644/* fill the label for each input at first */
2645static int fill_input_pin_labels(struct hda_codec *codec)
2646{
2647 struct hda_gen_spec *spec = codec->spec;
2648 const struct auto_pin_cfg *cfg = &spec->autocfg;
2649 int i;
2650
2651 for (i = 0; i < cfg->num_inputs; i++) {
2652 hda_nid_t pin = cfg->inputs[i].pin;
2653 const char *label;
2654 int j, idx;
2655
2656 if (!is_input_pin(codec, pin))
2657 continue;
2658
2659 label = hda_get_autocfg_input_label(codec, cfg, i);
2660 idx = 0;
David Henningsson8e8db7f2013-01-18 15:43:02 +01002661 for (j = i - 1; j >= 0; j--) {
Takashi Iwaic9700422013-01-18 10:17:30 +01002662 if (spec->input_labels[j] &&
2663 !strcmp(spec->input_labels[j], label)) {
2664 idx = spec->input_label_idxs[j] + 1;
2665 break;
2666 }
2667 }
2668
2669 spec->input_labels[i] = label;
2670 spec->input_label_idxs[i] = idx;
2671 }
2672
2673 return 0;
2674}
2675
Takashi Iwai9dba2052013-01-18 10:01:15 +01002676#define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
2677
Takashi Iwai352f7f92012-12-19 12:52:06 +01002678static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002679{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002680 struct hda_gen_spec *spec = codec->spec;
2681 const struct auto_pin_cfg *cfg = &spec->autocfg;
2682 hda_nid_t mixer = spec->mixer_nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002683 int num_adcs;
Takashi Iwaic9700422013-01-18 10:17:30 +01002684 int i, err;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002685 unsigned int val;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002686
Takashi Iwai352f7f92012-12-19 12:52:06 +01002687 num_adcs = fill_adc_nids(codec);
2688 if (num_adcs < 0)
2689 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002690
Takashi Iwaic9700422013-01-18 10:17:30 +01002691 err = fill_input_pin_labels(codec);
2692 if (err < 0)
2693 return err;
2694
Takashi Iwai352f7f92012-12-19 12:52:06 +01002695 for (i = 0; i < cfg->num_inputs; i++) {
2696 hda_nid_t pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697
Takashi Iwai352f7f92012-12-19 12:52:06 +01002698 pin = cfg->inputs[i].pin;
2699 if (!is_input_pin(codec, pin))
2700 continue;
2701
Takashi Iwai2c12c302013-01-10 09:33:29 +01002702 val = PIN_IN;
2703 if (cfg->inputs[i].type == AUTO_PIN_MIC)
2704 val |= snd_hda_get_default_vref(codec, pin);
2705 set_pin_target(codec, pin, val, false);
2706
Takashi Iwai352f7f92012-12-19 12:52:06 +01002707 if (mixer) {
2708 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01002709 err = new_analog_input(codec, i, pin,
Takashi Iwaic9700422013-01-18 10:17:30 +01002710 spec->input_labels[i],
2711 spec->input_label_idxs[i],
2712 mixer);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002713 if (err < 0)
2714 return err;
2715 }
2716 }
2717
Takashi Iwaic9700422013-01-18 10:17:30 +01002718 err = parse_capture_source(codec, pin, i, num_adcs,
2719 spec->input_labels[i], -mixer);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002720 if (err < 0)
2721 return err;
Takashi Iwai294765582013-01-17 09:52:11 +01002722
2723 if (spec->add_in_jack_modes) {
2724 err = create_in_jack_mode(codec, pin);
2725 if (err < 0)
2726 return err;
2727 }
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002728 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002729
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002730 if (mixer && spec->add_stereo_mix_input) {
Takashi Iwai9dba2052013-01-18 10:01:15 +01002731 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002732 "Stereo Mix", 0);
2733 if (err < 0)
2734 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002735 }
2736
2737 return 0;
2738}
2739
2740
2741/*
2742 * input source mux
2743 */
2744
Takashi Iwaic697b712013-01-07 17:09:26 +01002745/* get the input path specified by the given adc and imux indices */
2746static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002747{
2748 struct hda_gen_spec *spec = codec->spec;
David Henningssonb56fa1e2013-01-16 11:45:35 +01002749 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
2750 snd_BUG();
2751 return NULL;
2752 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002753 if (spec->dyn_adc_switch)
2754 adc_idx = spec->dyn_adc_idx[imux_idx];
David Henningssond3d982f2013-01-18 15:43:01 +01002755 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
David Henningssonb56fa1e2013-01-16 11:45:35 +01002756 snd_BUG();
2757 return NULL;
2758 }
Takashi Iwaic697b712013-01-07 17:09:26 +01002759 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002760}
2761
2762static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2763 unsigned int idx);
2764
2765static int mux_enum_info(struct snd_kcontrol *kcontrol,
2766 struct snd_ctl_elem_info *uinfo)
2767{
2768 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2769 struct hda_gen_spec *spec = codec->spec;
2770 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
2771}
2772
2773static int mux_enum_get(struct snd_kcontrol *kcontrol,
2774 struct snd_ctl_elem_value *ucontrol)
2775{
2776 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2777 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai2a8d5392013-01-18 16:23:25 +01002778 /* the ctls are created at once with multiple counts */
2779 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002780
2781 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
2782 return 0;
2783}
2784
2785static int mux_enum_put(struct snd_kcontrol *kcontrol,
2786 struct snd_ctl_elem_value *ucontrol)
2787{
2788 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Takashi Iwai2a8d5392013-01-18 16:23:25 +01002789 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002790 return mux_select(codec, adc_idx,
2791 ucontrol->value.enumerated.item[0]);
2792}
2793
Takashi Iwai352f7f92012-12-19 12:52:06 +01002794static const struct snd_kcontrol_new cap_src_temp = {
2795 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2796 .name = "Input Source",
2797 .info = mux_enum_info,
2798 .get = mux_enum_get,
2799 .put = mux_enum_put,
2800};
2801
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002802/*
2803 * capture volume and capture switch ctls
2804 */
2805
Takashi Iwai352f7f92012-12-19 12:52:06 +01002806typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
2807 struct snd_ctl_elem_value *ucontrol);
2808
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002809/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002810static int cap_put_caller(struct snd_kcontrol *kcontrol,
2811 struct snd_ctl_elem_value *ucontrol,
2812 put_call_t func, int type)
2813{
2814 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2815 struct hda_gen_spec *spec = codec->spec;
2816 const struct hda_input_mux *imux;
2817 struct nid_path *path;
2818 int i, adc_idx, err = 0;
2819
2820 imux = &spec->input_mux;
David Henningssona053d1e2013-01-16 11:45:36 +01002821 adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002822 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002823 /* we use the cache-only update at first since multiple input paths
2824 * may shared the same amp; by updating only caches, the redundant
2825 * writes to hardware can be reduced.
2826 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002827 codec->cached_write = 1;
2828 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01002829 path = get_input_path(codec, adc_idx, i);
2830 if (!path || !path->ctls[type])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002831 continue;
2832 kcontrol->private_value = path->ctls[type];
2833 err = func(kcontrol, ucontrol);
2834 if (err < 0)
2835 goto error;
2836 }
2837 error:
2838 codec->cached_write = 0;
2839 mutex_unlock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002840 snd_hda_codec_flush_amp_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002841 if (err >= 0 && spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01002842 spec->cap_sync_hook(codec, ucontrol);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002843 return err;
2844}
2845
2846/* capture volume ctl callbacks */
2847#define cap_vol_info snd_hda_mixer_amp_volume_info
2848#define cap_vol_get snd_hda_mixer_amp_volume_get
2849#define cap_vol_tlv snd_hda_mixer_amp_tlv
2850
2851static int cap_vol_put(struct snd_kcontrol *kcontrol,
2852 struct snd_ctl_elem_value *ucontrol)
2853{
2854 return cap_put_caller(kcontrol, ucontrol,
2855 snd_hda_mixer_amp_volume_put,
2856 NID_PATH_VOL_CTL);
2857}
2858
2859static const struct snd_kcontrol_new cap_vol_temp = {
2860 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2861 .name = "Capture Volume",
2862 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
2863 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2864 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
2865 .info = cap_vol_info,
2866 .get = cap_vol_get,
2867 .put = cap_vol_put,
2868 .tlv = { .c = cap_vol_tlv },
2869};
2870
2871/* capture switch ctl callbacks */
2872#define cap_sw_info snd_ctl_boolean_stereo_info
2873#define cap_sw_get snd_hda_mixer_amp_switch_get
2874
2875static int cap_sw_put(struct snd_kcontrol *kcontrol,
2876 struct snd_ctl_elem_value *ucontrol)
2877{
Takashi Iwaia90229e2013-01-18 14:10:00 +01002878 return cap_put_caller(kcontrol, ucontrol,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002879 snd_hda_mixer_amp_switch_put,
2880 NID_PATH_MUTE_CTL);
2881}
2882
2883static const struct snd_kcontrol_new cap_sw_temp = {
2884 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2885 .name = "Capture Switch",
2886 .info = cap_sw_info,
2887 .get = cap_sw_get,
2888 .put = cap_sw_put,
2889};
2890
2891static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
2892{
2893 hda_nid_t nid;
2894 int i, depth;
2895
2896 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
2897 for (depth = 0; depth < 3; depth++) {
2898 if (depth >= path->depth)
2899 return -EINVAL;
2900 i = path->depth - depth - 1;
2901 nid = path->path[i];
2902 if (!path->ctls[NID_PATH_VOL_CTL]) {
2903 if (nid_has_volume(codec, nid, HDA_OUTPUT))
2904 path->ctls[NID_PATH_VOL_CTL] =
2905 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2906 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
2907 int idx = path->idx[i];
2908 if (!depth && codec->single_adc_amp)
2909 idx = 0;
2910 path->ctls[NID_PATH_VOL_CTL] =
2911 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2912 }
2913 }
2914 if (!path->ctls[NID_PATH_MUTE_CTL]) {
2915 if (nid_has_mute(codec, nid, HDA_OUTPUT))
2916 path->ctls[NID_PATH_MUTE_CTL] =
2917 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2918 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
2919 int idx = path->idx[i];
2920 if (!depth && codec->single_adc_amp)
2921 idx = 0;
2922 path->ctls[NID_PATH_MUTE_CTL] =
2923 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2924 }
2925 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01002926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 return 0;
2928}
2929
Takashi Iwai352f7f92012-12-19 12:52:06 +01002930static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002932 struct hda_gen_spec *spec = codec->spec;
2933 struct auto_pin_cfg *cfg = &spec->autocfg;
2934 unsigned int val;
2935 int i;
2936
2937 if (!spec->inv_dmic_split)
2938 return false;
2939 for (i = 0; i < cfg->num_inputs; i++) {
2940 if (cfg->inputs[i].pin != nid)
2941 continue;
2942 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2943 return false;
2944 val = snd_hda_codec_get_pincfg(codec, nid);
2945 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
2946 }
2947 return false;
2948}
2949
Takashi Iwaia90229e2013-01-18 14:10:00 +01002950/* capture switch put callback for a single control with hook call */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01002951static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
2952 struct snd_ctl_elem_value *ucontrol)
2953{
2954 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2955 struct hda_gen_spec *spec = codec->spec;
2956 int ret;
2957
2958 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2959 if (ret < 0)
2960 return ret;
2961
Takashi Iwaia90229e2013-01-18 14:10:00 +01002962 if (spec->cap_sync_hook)
2963 spec->cap_sync_hook(codec, ucontrol);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01002964
2965 return ret;
2966}
2967
Takashi Iwai352f7f92012-12-19 12:52:06 +01002968static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
2969 int idx, bool is_switch, unsigned int ctl,
2970 bool inv_dmic)
2971{
2972 struct hda_gen_spec *spec = codec->spec;
2973 char tmpname[44];
2974 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
2975 const char *sfx = is_switch ? "Switch" : "Volume";
2976 unsigned int chs = inv_dmic ? 1 : 3;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01002977 struct snd_kcontrol_new *knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002978
2979 if (!ctl)
2980 return 0;
2981
2982 if (label)
2983 snprintf(tmpname, sizeof(tmpname),
2984 "%s Capture %s", label, sfx);
2985 else
2986 snprintf(tmpname, sizeof(tmpname),
2987 "Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01002988 knew = add_control(spec, type, tmpname, idx,
2989 amp_val_replace_channels(ctl, chs));
2990 if (!knew)
2991 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01002992 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01002993 knew->put = cap_single_sw_put;
2994 if (!inv_dmic)
2995 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002996
2997 /* Make independent right kcontrol */
2998 if (label)
2999 snprintf(tmpname, sizeof(tmpname),
3000 "Inverted %s Capture %s", label, sfx);
3001 else
3002 snprintf(tmpname, sizeof(tmpname),
3003 "Inverted Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003004 knew = add_control(spec, type, tmpname, idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003005 amp_val_replace_channels(ctl, 2));
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003006 if (!knew)
3007 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003008 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003009 knew->put = cap_single_sw_put;
3010 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003011}
3012
3013/* create single (and simple) capture volume and switch controls */
3014static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3015 unsigned int vol_ctl, unsigned int sw_ctl,
3016 bool inv_dmic)
3017{
3018 int err;
3019 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3020 if (err < 0)
3021 return err;
3022 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3023 if (err < 0)
3024 return err;
3025 return 0;
3026}
3027
3028/* create bound capture volume and switch controls */
3029static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3030 unsigned int vol_ctl, unsigned int sw_ctl)
3031{
3032 struct hda_gen_spec *spec = codec->spec;
3033 struct snd_kcontrol_new *knew;
3034
3035 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003036 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003037 if (!knew)
3038 return -ENOMEM;
3039 knew->index = idx;
3040 knew->private_value = vol_ctl;
3041 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3042 }
3043 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003044 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003045 if (!knew)
3046 return -ENOMEM;
3047 knew->index = idx;
3048 knew->private_value = sw_ctl;
3049 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3050 }
3051 return 0;
3052}
3053
3054/* return the vol ctl when used first in the imux list */
3055static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3056{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003057 struct nid_path *path;
3058 unsigned int ctl;
3059 int i;
3060
Takashi Iwaic697b712013-01-07 17:09:26 +01003061 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003062 if (!path)
3063 return 0;
3064 ctl = path->ctls[type];
3065 if (!ctl)
3066 return 0;
3067 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003068 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003069 if (path && path->ctls[type] == ctl)
3070 return 0;
3071 }
3072 return ctl;
3073}
3074
3075/* create individual capture volume and switch controls per input */
3076static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3077{
3078 struct hda_gen_spec *spec = codec->spec;
3079 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaic9700422013-01-18 10:17:30 +01003080 int i, err, type;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003081
3082 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003083 bool inv_dmic;
Takashi Iwaic9700422013-01-18 10:17:30 +01003084 int idx;
Takashi Iwai9dba2052013-01-18 10:01:15 +01003085
Takashi Iwaic9700422013-01-18 10:17:30 +01003086 idx = imux->items[i].index;
3087 if (idx >= spec->autocfg.num_inputs)
Takashi Iwai9dba2052013-01-18 10:01:15 +01003088 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003089 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3090
3091 for (type = 0; type < 2; type++) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003092 err = add_single_cap_ctl(codec,
3093 spec->input_labels[idx],
3094 spec->input_label_idxs[idx],
3095 type,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003096 get_first_cap_ctl(codec, i, type),
3097 inv_dmic);
3098 if (err < 0)
3099 return err;
3100 }
3101 }
3102 return 0;
3103}
3104
3105static int create_capture_mixers(struct hda_codec *codec)
3106{
3107 struct hda_gen_spec *spec = codec->spec;
3108 struct hda_input_mux *imux = &spec->input_mux;
3109 int i, n, nums, err;
3110
3111 if (spec->dyn_adc_switch)
3112 nums = 1;
3113 else
3114 nums = spec->num_adc_nids;
3115
3116 if (!spec->auto_mic && imux->num_items > 1) {
3117 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01003118 const char *name;
3119 name = nums > 1 ? "Input Source" : "Capture Source";
3120 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003121 if (!knew)
3122 return -ENOMEM;
3123 knew->count = nums;
3124 }
3125
3126 for (n = 0; n < nums; n++) {
3127 bool multi = false;
David Henningsson99a55922013-01-16 15:58:44 +01003128 bool multi_cap_vol = spec->multi_cap_vol;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003129 bool inv_dmic = false;
3130 int vol, sw;
3131
3132 vol = sw = 0;
3133 for (i = 0; i < imux->num_items; i++) {
3134 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01003135 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003136 if (!path)
3137 continue;
3138 parse_capvol_in_path(codec, path);
3139 if (!vol)
3140 vol = path->ctls[NID_PATH_VOL_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003141 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003142 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003143 if (!same_amp_caps(codec, vol,
3144 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3145 multi_cap_vol = true;
3146 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003147 if (!sw)
3148 sw = path->ctls[NID_PATH_MUTE_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003149 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003150 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003151 if (!same_amp_caps(codec, sw,
3152 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3153 multi_cap_vol = true;
3154 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003155 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3156 inv_dmic = true;
3157 }
3158
3159 if (!multi)
3160 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3161 inv_dmic);
David Henningsson99a55922013-01-16 15:58:44 +01003162 else if (!multi_cap_vol)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003163 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3164 else
3165 err = create_multi_cap_vol_ctl(codec);
3166 if (err < 0)
3167 return err;
3168 }
3169
3170 return 0;
3171}
3172
3173/*
3174 * add mic boosts if needed
3175 */
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003176
3177/* check whether the given amp is feasible as a boost volume */
3178static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3179 int dir, int idx)
3180{
3181 unsigned int step;
3182
3183 if (!nid_has_volume(codec, nid, dir) ||
3184 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3185 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3186 return false;
3187
3188 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3189 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3190 if (step < 0x20)
3191 return false;
3192 return true;
3193}
3194
3195/* look for a boost amp in a widget close to the pin */
3196static unsigned int look_for_boost_amp(struct hda_codec *codec,
3197 struct nid_path *path)
3198{
3199 unsigned int val = 0;
3200 hda_nid_t nid;
3201 int depth;
3202
3203 for (depth = 0; depth < 3; depth++) {
3204 if (depth >= path->depth - 1)
3205 break;
3206 nid = path->path[depth];
3207 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3208 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3209 break;
3210 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3211 path->idx[depth])) {
3212 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3213 HDA_INPUT);
3214 break;
3215 }
3216 }
3217
3218 return val;
3219}
3220
Takashi Iwai352f7f92012-12-19 12:52:06 +01003221static int parse_mic_boost(struct hda_codec *codec)
3222{
3223 struct hda_gen_spec *spec = codec->spec;
3224 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003225 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003226 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003227
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003228 if (!spec->num_adc_nids)
3229 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003230
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003231 for (i = 0; i < imux->num_items; i++) {
3232 struct nid_path *path;
3233 unsigned int val;
3234 int idx;
3235 char boost_label[44];
David Henningsson02aba552013-01-16 15:58:43 +01003236
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003237 idx = imux->items[i].index;
3238 if (idx >= imux->num_items)
3239 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003240
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003241 /* check only line-in and mic pins */
Takashi Iwai1799cdd2013-01-18 14:37:16 +01003242 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003243 continue;
3244
3245 path = get_input_path(codec, 0, i);
3246 if (!path)
3247 continue;
3248
3249 val = look_for_boost_amp(codec, path);
3250 if (!val)
3251 continue;
3252
3253 /* create a boost control */
3254 snprintf(boost_label, sizeof(boost_label),
3255 "%s Boost Volume", spec->input_labels[idx]);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003256 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3257 spec->input_label_idxs[idx], val))
3258 return -ENOMEM;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003259
3260 path->ctls[NID_PATH_BOOST_CTL] = val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003261 }
3262 return 0;
3263}
3264
3265/*
3266 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3267 */
3268static void parse_digital(struct hda_codec *codec)
3269{
3270 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003271 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003272 int i, nums;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003273 hda_nid_t dig_nid, pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003274
3275 /* support multiple SPDIFs; the secondary is set up as a slave */
3276 nums = 0;
3277 for (i = 0; i < spec->autocfg.dig_outs; i++) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003278 pin = spec->autocfg.dig_out_pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003279 dig_nid = look_for_dac(codec, pin, true);
3280 if (!dig_nid)
3281 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003282 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003283 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003284 continue;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003285 print_nid_path("digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01003286 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01003287 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003288 set_pin_target(codec, pin, PIN_OUT, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003289 if (!nums) {
3290 spec->multiout.dig_out_nid = dig_nid;
3291 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3292 } else {
3293 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3294 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
3295 break;
3296 spec->slave_dig_outs[nums - 1] = dig_nid;
3297 }
3298 nums++;
3299 }
3300
3301 if (spec->autocfg.dig_in_pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003302 pin = spec->autocfg.dig_in_pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003303 dig_nid = codec->start_nid;
3304 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003305 unsigned int wcaps = get_wcaps(codec, dig_nid);
3306 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3307 continue;
3308 if (!(wcaps & AC_WCAP_DIGITAL))
3309 continue;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003310 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003311 if (path) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003312 print_nid_path("digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003313 path->active = true;
3314 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003315 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003316 set_pin_target(codec, pin, PIN_IN, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003317 break;
3318 }
3319 }
3320 }
3321}
3322
3323
3324/*
3325 * input MUX handling
3326 */
3327
3328static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3329
3330/* select the given imux item; either unmute exclusively or select the route */
3331static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3332 unsigned int idx)
3333{
3334 struct hda_gen_spec *spec = codec->spec;
3335 const struct hda_input_mux *imux;
3336 struct nid_path *path;
3337
3338 imux = &spec->input_mux;
3339 if (!imux->num_items)
3340 return 0;
3341
3342 if (idx >= imux->num_items)
3343 idx = imux->num_items - 1;
3344 if (spec->cur_mux[adc_idx] == idx)
3345 return 0;
3346
Takashi Iwaic697b712013-01-07 17:09:26 +01003347 path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003348 if (!path)
3349 return 0;
3350 if (path->active)
3351 snd_hda_activate_path(codec, path, false, false);
3352
3353 spec->cur_mux[adc_idx] = idx;
3354
3355 if (spec->shared_mic_hp)
3356 update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
3357
3358 if (spec->dyn_adc_switch)
3359 dyn_adc_pcm_resetup(codec, idx);
3360
Takashi Iwaic697b712013-01-07 17:09:26 +01003361 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003362 if (!path)
3363 return 0;
3364 if (path->active)
3365 return 0;
3366 snd_hda_activate_path(codec, path, true, false);
3367 if (spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01003368 spec->cap_sync_hook(codec, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003369 return 1;
3370}
3371
3372
3373/*
3374 * Jack detections for HP auto-mute and mic-switch
3375 */
3376
3377/* check each pin in the given array; returns true if any of them is plugged */
3378static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
3379{
3380 int i, present = 0;
3381
3382 for (i = 0; i < num_pins; i++) {
3383 hda_nid_t nid = pins[i];
3384 if (!nid)
3385 break;
Takashi Iwai0b4df932013-01-10 09:45:13 +01003386 /* don't detect pins retasked as inputs */
3387 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
3388 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003389 present |= snd_hda_jack_detect(codec, nid);
3390 }
3391 return present;
3392}
3393
3394/* standard HP/line-out auto-mute helper */
3395static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
Takashi Iwai2c12c302013-01-10 09:33:29 +01003396 bool mute)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003397{
3398 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003399 int i;
3400
3401 for (i = 0; i < num_pins; i++) {
3402 hda_nid_t nid = pins[i];
3403 unsigned int val;
3404 if (!nid)
3405 break;
3406 /* don't reset VREF value in case it's controlling
3407 * the amp (see alc861_fixup_asus_amp_vref_0f())
3408 */
Takashi Iwai2c12c302013-01-10 09:33:29 +01003409 if (spec->keep_vref_in_automute)
3410 val = snd_hda_codec_get_pin_target(codec, nid) & ~PIN_HP;
3411 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01003412 val = 0;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003413 if (!mute)
3414 val |= snd_hda_codec_get_pin_target(codec, nid);
3415 /* here we call update_pin_ctl() so that the pinctl is changed
3416 * without changing the pinctl target value;
3417 * the original target value will be still referred at the
3418 * init / resume again
3419 */
3420 update_pin_ctl(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01003421 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003422 }
3423}
3424
3425/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003426void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003427{
3428 struct hda_gen_spec *spec = codec->spec;
3429 int on;
3430
3431 /* Control HP pins/amps depending on master_mute state;
3432 * in general, HP pins/amps control should be enabled in all cases,
3433 * but currently set only for master_mute, just to be safe
3434 */
3435 if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
3436 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003437 spec->autocfg.hp_pins, spec->master_mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003438
3439 if (!spec->automute_speaker)
3440 on = 0;
3441 else
3442 on = spec->hp_jack_present | spec->line_jack_present;
3443 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003444 spec->speaker_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003445 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003446 spec->autocfg.speaker_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003447
3448 /* toggle line-out mutes if needed, too */
3449 /* if LO is a copy of either HP or Speaker, don't need to handle it */
3450 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
3451 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
3452 return;
3453 if (!spec->automute_lo)
3454 on = 0;
3455 else
3456 on = spec->hp_jack_present;
3457 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003458 spec->line_out_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003459 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003460 spec->autocfg.line_out_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003461}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003462EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003463
3464static void call_update_outputs(struct hda_codec *codec)
3465{
3466 struct hda_gen_spec *spec = codec->spec;
3467 if (spec->automute_hook)
3468 spec->automute_hook(codec);
3469 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01003470 snd_hda_gen_update_outputs(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003471}
3472
3473/* standard HP-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003474void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003475{
3476 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai92603c52013-01-22 07:46:31 +01003477 hda_nid_t *pins = spec->autocfg.hp_pins;
3478 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003479
Takashi Iwai92603c52013-01-22 07:46:31 +01003480 /* No detection for the first HP jack during indep-HP mode */
3481 if (spec->indep_hp_enabled) {
3482 pins++;
3483 num_pins--;
3484 }
3485
3486 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003487 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
3488 return;
3489 call_update_outputs(codec);
3490}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003491EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003492
3493/* standard line-out-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003494void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003495{
3496 struct hda_gen_spec *spec = codec->spec;
3497
3498 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3499 return;
3500 /* check LO jack only when it's different from HP */
3501 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
3502 return;
3503
3504 spec->line_jack_present =
3505 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3506 spec->autocfg.line_out_pins);
3507 if (!spec->automute_speaker || !spec->detect_lo)
3508 return;
3509 call_update_outputs(codec);
3510}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003511EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003512
3513/* standard mic auto-switch helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003514void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003515{
3516 struct hda_gen_spec *spec = codec->spec;
3517 int i;
3518
3519 if (!spec->auto_mic)
3520 return;
3521
3522 for (i = spec->am_num_entries - 1; i > 0; i--) {
Takashi Iwai0b4df932013-01-10 09:45:13 +01003523 hda_nid_t pin = spec->am_entry[i].pin;
3524 /* don't detect pins retasked as outputs */
3525 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
3526 continue;
3527 if (snd_hda_jack_detect(codec, pin)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003528 mux_select(codec, 0, spec->am_entry[i].idx);
3529 return;
3530 }
3531 }
3532 mux_select(codec, 0, spec->am_entry[0].idx);
3533}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003534EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003535
Takashi Iwaia5cc2502013-01-16 18:08:55 +01003536/* update jack retasking */
3537static void update_automute_all(struct hda_codec *codec)
3538{
3539 struct hda_gen_spec *spec = codec->spec;
3540
3541 if (spec->hp_automute_hook)
3542 spec->hp_automute_hook(codec, NULL);
3543 else
3544 snd_hda_gen_hp_automute(codec, NULL);
3545 if (spec->line_automute_hook)
3546 spec->line_automute_hook(codec, NULL);
3547 else
3548 snd_hda_gen_line_automute(codec, NULL);
3549 if (spec->mic_autoswitch_hook)
3550 spec->mic_autoswitch_hook(codec, NULL);
3551 else
3552 snd_hda_gen_mic_autoswitch(codec, NULL);
3553}
3554
Takashi Iwai352f7f92012-12-19 12:52:06 +01003555/*
3556 * Auto-Mute mode mixer enum support
3557 */
3558static int automute_mode_info(struct snd_kcontrol *kcontrol,
3559 struct snd_ctl_elem_info *uinfo)
3560{
3561 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3562 struct hda_gen_spec *spec = codec->spec;
3563 static const char * const texts3[] = {
3564 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02003565 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566
Takashi Iwai352f7f92012-12-19 12:52:06 +01003567 if (spec->automute_speaker_possible && spec->automute_lo_possible)
3568 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
3569 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
3570}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571
Takashi Iwai352f7f92012-12-19 12:52:06 +01003572static int automute_mode_get(struct snd_kcontrol *kcontrol,
3573 struct snd_ctl_elem_value *ucontrol)
3574{
3575 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3576 struct hda_gen_spec *spec = codec->spec;
3577 unsigned int val = 0;
3578 if (spec->automute_speaker)
3579 val++;
3580 if (spec->automute_lo)
3581 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003582
Takashi Iwai352f7f92012-12-19 12:52:06 +01003583 ucontrol->value.enumerated.item[0] = val;
3584 return 0;
3585}
3586
3587static int automute_mode_put(struct snd_kcontrol *kcontrol,
3588 struct snd_ctl_elem_value *ucontrol)
3589{
3590 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3591 struct hda_gen_spec *spec = codec->spec;
3592
3593 switch (ucontrol->value.enumerated.item[0]) {
3594 case 0:
3595 if (!spec->automute_speaker && !spec->automute_lo)
3596 return 0;
3597 spec->automute_speaker = 0;
3598 spec->automute_lo = 0;
3599 break;
3600 case 1:
3601 if (spec->automute_speaker_possible) {
3602 if (!spec->automute_lo && spec->automute_speaker)
3603 return 0;
3604 spec->automute_speaker = 1;
3605 spec->automute_lo = 0;
3606 } else if (spec->automute_lo_possible) {
3607 if (spec->automute_lo)
3608 return 0;
3609 spec->automute_lo = 1;
3610 } else
3611 return -EINVAL;
3612 break;
3613 case 2:
3614 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
3615 return -EINVAL;
3616 if (spec->automute_speaker && spec->automute_lo)
3617 return 0;
3618 spec->automute_speaker = 1;
3619 spec->automute_lo = 1;
3620 break;
3621 default:
3622 return -EINVAL;
3623 }
3624 call_update_outputs(codec);
3625 return 1;
3626}
3627
3628static const struct snd_kcontrol_new automute_mode_enum = {
3629 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3630 .name = "Auto-Mute Mode",
3631 .info = automute_mode_info,
3632 .get = automute_mode_get,
3633 .put = automute_mode_put,
3634};
3635
3636static int add_automute_mode_enum(struct hda_codec *codec)
3637{
3638 struct hda_gen_spec *spec = codec->spec;
3639
Takashi Iwai12c93df2012-12-19 14:38:33 +01003640 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01003641 return -ENOMEM;
3642 return 0;
3643}
3644
3645/*
3646 * Check the availability of HP/line-out auto-mute;
3647 * Set up appropriately if really supported
3648 */
3649static int check_auto_mute_availability(struct hda_codec *codec)
3650{
3651 struct hda_gen_spec *spec = codec->spec;
3652 struct auto_pin_cfg *cfg = &spec->autocfg;
3653 int present = 0;
3654 int i, err;
3655
Takashi Iwaif72706b2013-01-16 18:20:07 +01003656 if (spec->suppress_auto_mute)
3657 return 0;
3658
Takashi Iwai352f7f92012-12-19 12:52:06 +01003659 if (cfg->hp_pins[0])
3660 present++;
3661 if (cfg->line_out_pins[0])
3662 present++;
3663 if (cfg->speaker_pins[0])
3664 present++;
3665 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02003666 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003667
3668 if (!cfg->speaker_pins[0] &&
3669 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
3670 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3671 sizeof(cfg->speaker_pins));
3672 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674
Takashi Iwai352f7f92012-12-19 12:52:06 +01003675 if (!cfg->hp_pins[0] &&
3676 cfg->line_out_type == AUTO_PIN_HP_OUT) {
3677 memcpy(cfg->hp_pins, cfg->line_out_pins,
3678 sizeof(cfg->hp_pins));
3679 cfg->hp_outs = cfg->line_outs;
3680 }
3681
3682 for (i = 0; i < cfg->hp_outs; i++) {
3683 hda_nid_t nid = cfg->hp_pins[i];
3684 if (!is_jack_detectable(codec, nid))
3685 continue;
3686 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
3687 nid);
3688 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003689 spec->hp_automute_hook ?
3690 spec->hp_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003691 snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003692 spec->detect_hp = 1;
3693 }
3694
3695 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
3696 if (cfg->speaker_outs)
3697 for (i = 0; i < cfg->line_outs; i++) {
3698 hda_nid_t nid = cfg->line_out_pins[i];
3699 if (!is_jack_detectable(codec, nid))
3700 continue;
3701 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
3702 snd_hda_jack_detect_enable_callback(codec, nid,
3703 HDA_GEN_FRONT_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003704 spec->line_automute_hook ?
3705 spec->line_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003706 snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003707 spec->detect_lo = 1;
3708 }
3709 spec->automute_lo_possible = spec->detect_hp;
3710 }
3711
3712 spec->automute_speaker_possible = cfg->speaker_outs &&
3713 (spec->detect_hp || spec->detect_lo);
3714
3715 spec->automute_lo = spec->automute_lo_possible;
3716 spec->automute_speaker = spec->automute_speaker_possible;
3717
3718 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
3719 /* create a control for automute mode */
3720 err = add_automute_mode_enum(codec);
3721 if (err < 0)
3722 return err;
3723 }
3724 return 0;
3725}
3726
Takashi Iwai352f7f92012-12-19 12:52:06 +01003727/* check whether all auto-mic pins are valid; setup indices if OK */
3728static bool auto_mic_check_imux(struct hda_codec *codec)
3729{
3730 struct hda_gen_spec *spec = codec->spec;
3731 const struct hda_input_mux *imux;
3732 int i;
3733
3734 imux = &spec->input_mux;
3735 for (i = 0; i < spec->am_num_entries; i++) {
3736 spec->am_entry[i].idx =
3737 find_idx_in_nid_list(spec->am_entry[i].pin,
3738 spec->imux_pins, imux->num_items);
3739 if (spec->am_entry[i].idx < 0)
3740 return false; /* no corresponding imux */
3741 }
3742
3743 /* we don't need the jack detection for the first pin */
3744 for (i = 1; i < spec->am_num_entries; i++)
3745 snd_hda_jack_detect_enable_callback(codec,
3746 spec->am_entry[i].pin,
3747 HDA_GEN_MIC_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003748 spec->mic_autoswitch_hook ?
3749 spec->mic_autoswitch_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003750 snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003751 return true;
3752}
3753
3754static int compare_attr(const void *ap, const void *bp)
3755{
3756 const struct automic_entry *a = ap;
3757 const struct automic_entry *b = bp;
3758 return (int)(a->attr - b->attr);
3759}
3760
3761/*
3762 * Check the availability of auto-mic switch;
3763 * Set up if really supported
3764 */
3765static int check_auto_mic_availability(struct hda_codec *codec)
3766{
3767 struct hda_gen_spec *spec = codec->spec;
3768 struct auto_pin_cfg *cfg = &spec->autocfg;
3769 unsigned int types;
3770 int i, num_pins;
3771
Takashi Iwaid12daf62013-01-07 16:32:11 +01003772 if (spec->suppress_auto_mic)
3773 return 0;
3774
Takashi Iwai352f7f92012-12-19 12:52:06 +01003775 types = 0;
3776 num_pins = 0;
3777 for (i = 0; i < cfg->num_inputs; i++) {
3778 hda_nid_t nid = cfg->inputs[i].pin;
3779 unsigned int attr;
3780 attr = snd_hda_codec_get_pincfg(codec, nid);
3781 attr = snd_hda_get_input_pin_attr(attr);
3782 if (types & (1 << attr))
3783 return 0; /* already occupied */
3784 switch (attr) {
3785 case INPUT_PIN_ATTR_INT:
3786 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3787 return 0; /* invalid type */
3788 break;
3789 case INPUT_PIN_ATTR_UNUSED:
3790 return 0; /* invalid entry */
3791 default:
3792 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
3793 return 0; /* invalid type */
3794 if (!spec->line_in_auto_switch &&
3795 cfg->inputs[i].type != AUTO_PIN_MIC)
3796 return 0; /* only mic is allowed */
3797 if (!is_jack_detectable(codec, nid))
3798 return 0; /* no unsol support */
3799 break;
3800 }
3801 if (num_pins >= MAX_AUTO_MIC_PINS)
3802 return 0;
3803 types |= (1 << attr);
3804 spec->am_entry[num_pins].pin = nid;
3805 spec->am_entry[num_pins].attr = attr;
3806 num_pins++;
3807 }
3808
3809 if (num_pins < 2)
3810 return 0;
3811
3812 spec->am_num_entries = num_pins;
3813 /* sort the am_entry in the order of attr so that the pin with a
3814 * higher attr will be selected when the jack is plugged.
3815 */
3816 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
3817 compare_attr, NULL);
3818
3819 if (!auto_mic_check_imux(codec))
3820 return 0;
3821
3822 spec->auto_mic = 1;
3823 spec->num_adc_nids = 1;
3824 spec->cur_mux[0] = spec->am_entry[0].idx;
3825 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
3826 spec->am_entry[0].pin,
3827 spec->am_entry[1].pin,
3828 spec->am_entry[2].pin);
3829
3830 return 0;
3831}
3832
3833
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003834/*
3835 * Parse the given BIOS configuration and set up the hda_gen_spec
3836 *
3837 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003838 * or a negative error code
3839 */
3840int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003841 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003842{
3843 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003844 int err;
3845
Takashi Iwai1c70a582013-01-11 17:48:22 +01003846 parse_user_hints(codec);
3847
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003848 if (cfg != &spec->autocfg) {
3849 spec->autocfg = *cfg;
3850 cfg = &spec->autocfg;
3851 }
3852
David Henningsson6fc4cb92013-01-16 15:58:45 +01003853 fill_all_dac_nids(codec);
3854
Takashi Iwai352f7f92012-12-19 12:52:06 +01003855 if (!cfg->line_outs) {
3856 if (cfg->dig_outs || cfg->dig_in_pin) {
3857 spec->multiout.max_channels = 2;
3858 spec->no_analog = 1;
3859 goto dig_only;
3860 }
3861 return 0; /* can't find valid BIOS pin config */
3862 }
3863
3864 if (!spec->no_primary_hp &&
3865 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
3866 cfg->line_outs <= cfg->hp_outs) {
3867 /* use HP as primary out */
3868 cfg->speaker_outs = cfg->line_outs;
3869 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3870 sizeof(cfg->speaker_pins));
3871 cfg->line_outs = cfg->hp_outs;
3872 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
3873 cfg->hp_outs = 0;
3874 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3875 cfg->line_out_type = AUTO_PIN_HP_OUT;
3876 }
3877
3878 err = parse_output_paths(codec);
3879 if (err < 0)
3880 return err;
3881 err = create_multi_channel_mode(codec);
3882 if (err < 0)
3883 return err;
3884 err = create_multi_out_ctls(codec, cfg);
3885 if (err < 0)
3886 return err;
3887 err = create_hp_out_ctls(codec);
3888 if (err < 0)
3889 return err;
3890 err = create_speaker_out_ctls(codec);
3891 if (err < 0)
3892 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003893 err = create_indep_hp_ctls(codec);
3894 if (err < 0)
3895 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01003896 err = create_loopback_mixing_ctl(codec);
3897 if (err < 0)
3898 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003899 err = create_shared_input(codec);
3900 if (err < 0)
3901 return err;
3902 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003903 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02003904 return err;
3905
Takashi Iwaia07a9492013-01-07 16:44:06 +01003906 spec->const_channel_count = spec->ext_channel_count;
3907 /* check the multiple speaker and headphone pins */
3908 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
3909 spec->const_channel_count = max(spec->const_channel_count,
3910 cfg->speaker_outs * 2);
3911 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
3912 spec->const_channel_count = max(spec->const_channel_count,
3913 cfg->hp_outs * 2);
3914 spec->multiout.max_channels = max(spec->ext_channel_count,
3915 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003916
3917 err = check_auto_mute_availability(codec);
3918 if (err < 0)
3919 return err;
3920
3921 err = check_dyn_adc_switch(codec);
3922 if (err < 0)
3923 return err;
3924
3925 if (!spec->shared_mic_hp) {
3926 err = check_auto_mic_availability(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003927 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 }
Takashi Iwai071c73a2006-08-23 18:34:06 +02003930
Takashi Iwai352f7f92012-12-19 12:52:06 +01003931 err = create_capture_mixers(codec);
3932 if (err < 0)
3933 return err;
3934
3935 err = parse_mic_boost(codec);
3936 if (err < 0)
3937 return err;
3938
Takashi Iwai978e77e2013-01-10 16:57:58 +01003939 if (spec->add_out_jack_modes) {
3940 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
3941 err = create_out_jack_modes(codec, cfg->line_outs,
3942 cfg->line_out_pins);
3943 if (err < 0)
3944 return err;
3945 }
3946 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
3947 err = create_out_jack_modes(codec, cfg->hp_outs,
3948 cfg->hp_pins);
3949 if (err < 0)
3950 return err;
3951 }
3952 }
3953
Takashi Iwai352f7f92012-12-19 12:52:06 +01003954 dig_only:
3955 parse_digital(codec);
3956
3957 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003959EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960
3961
3962/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003963 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003965
3966/* slave controls for virtual master */
3967static const char * const slave_pfxs[] = {
3968 "Front", "Surround", "Center", "LFE", "Side",
3969 "Headphone", "Speaker", "Mono", "Line Out",
3970 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01003971 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
3972 "Headphone Front", "Headphone Surround", "Headphone CLFE",
3973 "Headphone Side",
Takashi Iwai352f7f92012-12-19 12:52:06 +01003974 NULL,
3975};
3976
3977int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003979 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981
Takashi Iwai36502d02012-12-19 15:15:10 +01003982 if (spec->kctls.used) {
3983 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
3984 if (err < 0)
3985 return err;
3986 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987
Takashi Iwai352f7f92012-12-19 12:52:06 +01003988 if (spec->multiout.dig_out_nid) {
3989 err = snd_hda_create_dig_out_ctls(codec,
3990 spec->multiout.dig_out_nid,
3991 spec->multiout.dig_out_nid,
3992 spec->pcm_rec[1].pcm_type);
3993 if (err < 0)
3994 return err;
3995 if (!spec->no_analog) {
3996 err = snd_hda_create_spdif_share_sw(codec,
3997 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 if (err < 0)
3999 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004000 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 }
4002 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004003 if (spec->dig_in_nid) {
4004 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
4005 if (err < 0)
4006 return err;
4007 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008
Takashi Iwai352f7f92012-12-19 12:52:06 +01004009 /* if we have no master control, let's create it */
4010 if (!spec->no_analog &&
4011 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004012 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01004013 spec->vmaster_tlv, slave_pfxs,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004014 "Playback Volume");
4015 if (err < 0)
4016 return err;
4017 }
4018 if (!spec->no_analog &&
4019 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
4020 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
4021 NULL, slave_pfxs,
4022 "Playback Switch",
4023 true, &spec->vmaster_mute.sw_kctl);
4024 if (err < 0)
4025 return err;
4026 if (spec->vmaster_mute.hook)
Takashi Iwaifd25a972012-12-20 14:57:18 +01004027 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
4028 spec->vmaster_mute_enum);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004029 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030
Takashi Iwai352f7f92012-12-19 12:52:06 +01004031 free_kctls(spec); /* no longer needed */
4032
4033 if (spec->shared_mic_hp) {
4034 int err;
4035 int nid = spec->autocfg.inputs[1].pin;
4036 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
4037 if (err < 0)
4038 return err;
4039 err = snd_hda_jack_detect_enable(codec, nid, 0);
4040 if (err < 0)
4041 return err;
4042 }
4043
4044 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
4045 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 return err;
4047
4048 return 0;
4049}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004050EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
4051
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052
4053/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004054 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004057static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
4058 struct hda_codec *codec,
4059 struct snd_pcm_substream *substream,
4060 int action)
4061{
4062 struct hda_gen_spec *spec = codec->spec;
4063 if (spec->pcm_playback_hook)
4064 spec->pcm_playback_hook(hinfo, codec, substream, action);
4065}
4066
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004067static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
4068 struct hda_codec *codec,
4069 struct snd_pcm_substream *substream,
4070 int action)
4071{
4072 struct hda_gen_spec *spec = codec->spec;
4073 if (spec->pcm_capture_hook)
4074 spec->pcm_capture_hook(hinfo, codec, substream, action);
4075}
4076
Takashi Iwai352f7f92012-12-19 12:52:06 +01004077/*
4078 * Analog playback callbacks
4079 */
4080static int playback_pcm_open(struct hda_pcm_stream *hinfo,
4081 struct hda_codec *codec,
4082 struct snd_pcm_substream *substream)
4083{
4084 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004085 int err;
4086
4087 mutex_lock(&spec->pcm_mutex);
4088 err = snd_hda_multi_out_analog_open(codec,
4089 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004090 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004091 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004092 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004093 call_pcm_playback_hook(hinfo, codec, substream,
4094 HDA_GEN_PCM_ACT_OPEN);
4095 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004096 mutex_unlock(&spec->pcm_mutex);
4097 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004098}
4099
4100static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01004101 struct hda_codec *codec,
4102 unsigned int stream_tag,
4103 unsigned int format,
4104 struct snd_pcm_substream *substream)
4105{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004106 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004107 int err;
4108
4109 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
4110 stream_tag, format, substream);
4111 if (!err)
4112 call_pcm_playback_hook(hinfo, codec, substream,
4113 HDA_GEN_PCM_ACT_PREPARE);
4114 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004115}
Takashi Iwai97ec5582006-03-21 11:29:07 +01004116
Takashi Iwai352f7f92012-12-19 12:52:06 +01004117static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4118 struct hda_codec *codec,
4119 struct snd_pcm_substream *substream)
4120{
4121 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004122 int err;
4123
4124 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
4125 if (!err)
4126 call_pcm_playback_hook(hinfo, codec, substream,
4127 HDA_GEN_PCM_ACT_CLEANUP);
4128 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004129}
4130
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004131static int playback_pcm_close(struct hda_pcm_stream *hinfo,
4132 struct hda_codec *codec,
4133 struct snd_pcm_substream *substream)
4134{
4135 struct hda_gen_spec *spec = codec->spec;
4136 mutex_lock(&spec->pcm_mutex);
4137 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004138 call_pcm_playback_hook(hinfo, codec, substream,
4139 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004140 mutex_unlock(&spec->pcm_mutex);
4141 return 0;
4142}
4143
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004144static int capture_pcm_open(struct hda_pcm_stream *hinfo,
4145 struct hda_codec *codec,
4146 struct snd_pcm_substream *substream)
4147{
4148 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
4149 return 0;
4150}
4151
4152static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4153 struct hda_codec *codec,
4154 unsigned int stream_tag,
4155 unsigned int format,
4156 struct snd_pcm_substream *substream)
4157{
4158 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4159 call_pcm_capture_hook(hinfo, codec, substream,
4160 HDA_GEN_PCM_ACT_PREPARE);
4161 return 0;
4162}
4163
4164static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4165 struct hda_codec *codec,
4166 struct snd_pcm_substream *substream)
4167{
4168 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4169 call_pcm_capture_hook(hinfo, codec, substream,
4170 HDA_GEN_PCM_ACT_CLEANUP);
4171 return 0;
4172}
4173
4174static int capture_pcm_close(struct hda_pcm_stream *hinfo,
4175 struct hda_codec *codec,
4176 struct snd_pcm_substream *substream)
4177{
4178 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
4179 return 0;
4180}
4181
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004182static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
4183 struct hda_codec *codec,
4184 struct snd_pcm_substream *substream)
4185{
4186 struct hda_gen_spec *spec = codec->spec;
4187 int err = 0;
4188
4189 mutex_lock(&spec->pcm_mutex);
4190 if (!spec->indep_hp_enabled)
4191 err = -EBUSY;
4192 else
4193 spec->active_streams |= 1 << STREAM_INDEP_HP;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004194 call_pcm_playback_hook(hinfo, codec, substream,
4195 HDA_GEN_PCM_ACT_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004196 mutex_unlock(&spec->pcm_mutex);
4197 return err;
4198}
4199
4200static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
4201 struct hda_codec *codec,
4202 struct snd_pcm_substream *substream)
4203{
4204 struct hda_gen_spec *spec = codec->spec;
4205 mutex_lock(&spec->pcm_mutex);
4206 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004207 call_pcm_playback_hook(hinfo, codec, substream,
4208 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004209 mutex_unlock(&spec->pcm_mutex);
4210 return 0;
4211}
4212
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004213static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4214 struct hda_codec *codec,
4215 unsigned int stream_tag,
4216 unsigned int format,
4217 struct snd_pcm_substream *substream)
4218{
4219 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4220 call_pcm_playback_hook(hinfo, codec, substream,
4221 HDA_GEN_PCM_ACT_PREPARE);
4222 return 0;
4223}
4224
4225static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4226 struct hda_codec *codec,
4227 struct snd_pcm_substream *substream)
4228{
4229 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4230 call_pcm_playback_hook(hinfo, codec, substream,
4231 HDA_GEN_PCM_ACT_CLEANUP);
4232 return 0;
4233}
4234
Takashi Iwai352f7f92012-12-19 12:52:06 +01004235/*
4236 * Digital out
4237 */
4238static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
4239 struct hda_codec *codec,
4240 struct snd_pcm_substream *substream)
4241{
4242 struct hda_gen_spec *spec = codec->spec;
4243 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
4244}
4245
4246static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4247 struct hda_codec *codec,
4248 unsigned int stream_tag,
4249 unsigned int format,
4250 struct snd_pcm_substream *substream)
4251{
4252 struct hda_gen_spec *spec = codec->spec;
4253 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
4254 stream_tag, format, substream);
4255}
4256
4257static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4258 struct hda_codec *codec,
4259 struct snd_pcm_substream *substream)
4260{
4261 struct hda_gen_spec *spec = codec->spec;
4262 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
4263}
4264
4265static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
4266 struct hda_codec *codec,
4267 struct snd_pcm_substream *substream)
4268{
4269 struct hda_gen_spec *spec = codec->spec;
4270 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
4271}
4272
4273/*
4274 * Analog capture
4275 */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004276#define alt_capture_pcm_open capture_pcm_open
4277#define alt_capture_pcm_close capture_pcm_close
4278
Takashi Iwai352f7f92012-12-19 12:52:06 +01004279static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4280 struct hda_codec *codec,
4281 unsigned int stream_tag,
4282 unsigned int format,
4283 struct snd_pcm_substream *substream)
4284{
4285 struct hda_gen_spec *spec = codec->spec;
4286
4287 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01004288 stream_tag, 0, format);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004289 call_pcm_capture_hook(hinfo, codec, substream,
4290 HDA_GEN_PCM_ACT_PREPARE);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004291 return 0;
4292}
4293
Takashi Iwai352f7f92012-12-19 12:52:06 +01004294static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4295 struct hda_codec *codec,
4296 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01004297{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004298 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01004299
Takashi Iwai352f7f92012-12-19 12:52:06 +01004300 snd_hda_codec_cleanup_stream(codec,
4301 spec->adc_nids[substream->number + 1]);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004302 call_pcm_capture_hook(hinfo, codec, substream,
4303 HDA_GEN_PCM_ACT_CLEANUP);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004304 return 0;
4305}
4306
Takashi Iwai352f7f92012-12-19 12:52:06 +01004307/*
4308 */
4309static const struct hda_pcm_stream pcm_analog_playback = {
4310 .substreams = 1,
4311 .channels_min = 2,
4312 .channels_max = 8,
4313 /* NID is set in build_pcms */
4314 .ops = {
4315 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004316 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004317 .prepare = playback_pcm_prepare,
4318 .cleanup = playback_pcm_cleanup
4319 },
4320};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321
Takashi Iwai352f7f92012-12-19 12:52:06 +01004322static const struct hda_pcm_stream pcm_analog_capture = {
4323 .substreams = 1,
4324 .channels_min = 2,
4325 .channels_max = 2,
4326 /* NID is set in build_pcms */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004327 .ops = {
4328 .open = capture_pcm_open,
4329 .close = capture_pcm_close,
4330 .prepare = capture_pcm_prepare,
4331 .cleanup = capture_pcm_cleanup
4332 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004333};
4334
4335static const struct hda_pcm_stream pcm_analog_alt_playback = {
4336 .substreams = 1,
4337 .channels_min = 2,
4338 .channels_max = 2,
4339 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004340 .ops = {
4341 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004342 .close = alt_playback_pcm_close,
4343 .prepare = alt_playback_pcm_prepare,
4344 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004345 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004346};
4347
4348static const struct hda_pcm_stream pcm_analog_alt_capture = {
4349 .substreams = 2, /* can be overridden */
4350 .channels_min = 2,
4351 .channels_max = 2,
4352 /* NID is set in build_pcms */
4353 .ops = {
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004354 .open = alt_capture_pcm_open,
4355 .close = alt_capture_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004356 .prepare = alt_capture_pcm_prepare,
4357 .cleanup = alt_capture_pcm_cleanup
4358 },
4359};
4360
4361static const struct hda_pcm_stream pcm_digital_playback = {
4362 .substreams = 1,
4363 .channels_min = 2,
4364 .channels_max = 2,
4365 /* NID is set in build_pcms */
4366 .ops = {
4367 .open = dig_playback_pcm_open,
4368 .close = dig_playback_pcm_close,
4369 .prepare = dig_playback_pcm_prepare,
4370 .cleanup = dig_playback_pcm_cleanup
4371 },
4372};
4373
4374static const struct hda_pcm_stream pcm_digital_capture = {
4375 .substreams = 1,
4376 .channels_min = 2,
4377 .channels_max = 2,
4378 /* NID is set in build_pcms */
4379};
4380
4381/* Used by build_pcms to flag that a PCM has no playback stream */
4382static const struct hda_pcm_stream pcm_null_stream = {
4383 .substreams = 0,
4384 .channels_min = 0,
4385 .channels_max = 0,
4386};
4387
4388/*
4389 * dynamic changing ADC PCM streams
4390 */
4391static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
4392{
4393 struct hda_gen_spec *spec = codec->spec;
4394 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
4395
4396 if (spec->cur_adc && spec->cur_adc != new_adc) {
4397 /* stream is running, let's swap the current ADC */
4398 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
4399 spec->cur_adc = new_adc;
4400 snd_hda_codec_setup_stream(codec, new_adc,
4401 spec->cur_adc_stream_tag, 0,
4402 spec->cur_adc_format);
4403 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004405 return false;
4406}
4407
4408/* analog capture with dynamic dual-adc changes */
4409static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4410 struct hda_codec *codec,
4411 unsigned int stream_tag,
4412 unsigned int format,
4413 struct snd_pcm_substream *substream)
4414{
4415 struct hda_gen_spec *spec = codec->spec;
4416 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
4417 spec->cur_adc_stream_tag = stream_tag;
4418 spec->cur_adc_format = format;
4419 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
4420 return 0;
4421}
4422
4423static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4424 struct hda_codec *codec,
4425 struct snd_pcm_substream *substream)
4426{
4427 struct hda_gen_spec *spec = codec->spec;
4428 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
4429 spec->cur_adc = 0;
4430 return 0;
4431}
4432
4433static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
4434 .substreams = 1,
4435 .channels_min = 2,
4436 .channels_max = 2,
4437 .nid = 0, /* fill later */
4438 .ops = {
4439 .prepare = dyn_adc_capture_pcm_prepare,
4440 .cleanup = dyn_adc_capture_pcm_cleanup
4441 },
4442};
4443
Takashi Iwaif873e532012-12-20 16:58:39 +01004444static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
4445 const char *chip_name)
4446{
4447 char *p;
4448
4449 if (*str)
4450 return;
4451 strlcpy(str, chip_name, len);
4452
4453 /* drop non-alnum chars after a space */
4454 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
4455 if (!isalnum(p[1])) {
4456 *p = 0;
4457 break;
4458 }
4459 }
4460 strlcat(str, sfx, len);
4461}
4462
Takashi Iwai352f7f92012-12-19 12:52:06 +01004463/* build PCM streams based on the parsed results */
4464int snd_hda_gen_build_pcms(struct hda_codec *codec)
4465{
4466 struct hda_gen_spec *spec = codec->spec;
4467 struct hda_pcm *info = spec->pcm_rec;
4468 const struct hda_pcm_stream *p;
4469 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470
4471 codec->num_pcms = 1;
4472 codec->pcm_info = info;
4473
Takashi Iwai352f7f92012-12-19 12:52:06 +01004474 if (spec->no_analog)
4475 goto skip_analog;
4476
Takashi Iwaif873e532012-12-20 16:58:39 +01004477 fill_pcm_stream_name(spec->stream_name_analog,
4478 sizeof(spec->stream_name_analog),
4479 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004480 info->name = spec->stream_name_analog;
4481
4482 if (spec->multiout.num_dacs > 0) {
4483 p = spec->stream_analog_playback;
4484 if (!p)
4485 p = &pcm_analog_playback;
4486 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4487 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
4488 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
4489 spec->multiout.max_channels;
4490 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
4491 spec->autocfg.line_outs == 2)
4492 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
4493 snd_pcm_2_1_chmaps;
4494 }
4495 if (spec->num_adc_nids) {
4496 p = spec->stream_analog_capture;
4497 if (!p) {
4498 if (spec->dyn_adc_switch)
4499 p = &dyn_adc_pcm_analog_capture;
4500 else
4501 p = &pcm_analog_capture;
4502 }
4503 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4504 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
4505 }
4506
Takashi Iwai352f7f92012-12-19 12:52:06 +01004507 skip_analog:
4508 /* SPDIF for stream index #1 */
4509 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01004510 fill_pcm_stream_name(spec->stream_name_digital,
4511 sizeof(spec->stream_name_digital),
4512 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004513 codec->num_pcms = 2;
4514 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
4515 info = spec->pcm_rec + 1;
4516 info->name = spec->stream_name_digital;
4517 if (spec->dig_out_type)
4518 info->pcm_type = spec->dig_out_type;
4519 else
4520 info->pcm_type = HDA_PCM_TYPE_SPDIF;
4521 if (spec->multiout.dig_out_nid) {
4522 p = spec->stream_digital_playback;
4523 if (!p)
4524 p = &pcm_digital_playback;
4525 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4526 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
4527 }
4528 if (spec->dig_in_nid) {
4529 p = spec->stream_digital_capture;
4530 if (!p)
4531 p = &pcm_digital_capture;
4532 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4533 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
4534 }
4535 }
4536
4537 if (spec->no_analog)
4538 return 0;
4539
4540 /* If the use of more than one ADC is requested for the current
4541 * model, configure a second analog capture-only PCM.
4542 */
4543 have_multi_adcs = (spec->num_adc_nids > 1) &&
4544 !spec->dyn_adc_switch && !spec->auto_mic;
4545 /* Additional Analaog capture for index #2 */
4546 if (spec->alt_dac_nid || have_multi_adcs) {
Takashi Iwaia6071482013-01-21 16:50:09 +01004547 fill_pcm_stream_name(spec->stream_name_alt_analog,
4548 sizeof(spec->stream_name_alt_analog),
4549 " Alt Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004550 codec->num_pcms = 3;
4551 info = spec->pcm_rec + 2;
Takashi Iwaia6071482013-01-21 16:50:09 +01004552 info->name = spec->stream_name_alt_analog;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004553 if (spec->alt_dac_nid) {
4554 p = spec->stream_analog_alt_playback;
4555 if (!p)
4556 p = &pcm_analog_alt_playback;
4557 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4558 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
4559 spec->alt_dac_nid;
4560 } else {
4561 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
4562 pcm_null_stream;
4563 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
4564 }
4565 if (have_multi_adcs) {
4566 p = spec->stream_analog_alt_capture;
4567 if (!p)
4568 p = &pcm_analog_alt_capture;
4569 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4570 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
4571 spec->adc_nids[1];
4572 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
4573 spec->num_adc_nids - 1;
4574 } else {
4575 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
4576 pcm_null_stream;
4577 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
4578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 }
4580
4581 return 0;
4582}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004583EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
4584
4585
4586/*
4587 * Standard auto-parser initializations
4588 */
4589
Takashi Iwaid4156932013-01-07 10:08:02 +01004590/* configure the given path as a proper output */
Takashi Iwai2c12c302013-01-10 09:33:29 +01004591static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004592{
4593 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01004594 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004595
Takashi Iwai196c17662013-01-04 15:01:40 +01004596 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01004597 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004598 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01004599 pin = path->path[path->depth - 1];
Takashi Iwai2c12c302013-01-10 09:33:29 +01004600 restore_pin_ctl(codec, pin);
Takashi Iwaie1284af2013-01-03 16:33:02 +01004601 snd_hda_activate_path(codec, path, path->active, true);
4602 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004603}
4604
4605/* initialize primary output paths */
4606static void init_multi_out(struct hda_codec *codec)
4607{
4608 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004609 int i;
4610
Takashi Iwaid4156932013-01-07 10:08:02 +01004611 for (i = 0; i < spec->autocfg.line_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004612 set_output_and_unmute(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004613}
4614
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004615
Takashi Iwai2c12c302013-01-10 09:33:29 +01004616static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004617{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004618 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004619
Takashi Iwaid4156932013-01-07 10:08:02 +01004620 for (i = 0; i < num_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004621 set_output_and_unmute(codec, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004622}
4623
4624/* initialize hp and speaker paths */
4625static void init_extra_out(struct hda_codec *codec)
4626{
4627 struct hda_gen_spec *spec = codec->spec;
4628
4629 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004630 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004631 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
4632 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai2c12c302013-01-10 09:33:29 +01004633 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004634}
4635
4636/* initialize multi-io paths */
4637static void init_multi_io(struct hda_codec *codec)
4638{
4639 struct hda_gen_spec *spec = codec->spec;
4640 int i;
4641
4642 for (i = 0; i < spec->multi_ios; i++) {
4643 hda_nid_t pin = spec->multi_io[i].pin;
4644 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01004645 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004646 if (!path)
4647 continue;
4648 if (!spec->multi_io[i].ctl_in)
4649 spec->multi_io[i].ctl_in =
Takashi Iwai2c12c302013-01-10 09:33:29 +01004650 snd_hda_codec_get_pin_target(codec, pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004651 snd_hda_activate_path(codec, path, path->active, true);
4652 }
4653}
4654
Takashi Iwai352f7f92012-12-19 12:52:06 +01004655/* set up input pins and loopback paths */
4656static void init_analog_input(struct hda_codec *codec)
4657{
4658 struct hda_gen_spec *spec = codec->spec;
4659 struct auto_pin_cfg *cfg = &spec->autocfg;
4660 int i;
4661
4662 for (i = 0; i < cfg->num_inputs; i++) {
4663 hda_nid_t nid = cfg->inputs[i].pin;
4664 if (is_input_pin(codec, nid))
Takashi Iwai2c12c302013-01-10 09:33:29 +01004665 restore_pin_ctl(codec, nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004666
4667 /* init loopback inputs */
4668 if (spec->mixer_nid) {
4669 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01004670 path = snd_hda_get_path_from_idx(codec, spec->loopback_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004671 if (path)
4672 snd_hda_activate_path(codec, path,
4673 path->active, false);
4674 }
4675 }
4676}
4677
4678/* initialize ADC paths */
4679static void init_input_src(struct hda_codec *codec)
4680{
4681 struct hda_gen_spec *spec = codec->spec;
4682 struct hda_input_mux *imux = &spec->input_mux;
4683 struct nid_path *path;
4684 int i, c, nums;
4685
4686 if (spec->dyn_adc_switch)
4687 nums = 1;
4688 else
4689 nums = spec->num_adc_nids;
4690
4691 for (c = 0; c < nums; c++) {
4692 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01004693 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004694 if (path) {
4695 bool active = path->active;
4696 if (i == spec->cur_mux[c])
4697 active = true;
4698 snd_hda_activate_path(codec, path, active, false);
4699 }
4700 }
4701 }
4702
4703 if (spec->shared_mic_hp)
4704 update_shared_mic_hp(codec, spec->cur_mux[0]);
4705
4706 if (spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01004707 spec->cap_sync_hook(codec, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004708}
4709
4710/* set right pin controls for digital I/O */
4711static void init_digital(struct hda_codec *codec)
4712{
4713 struct hda_gen_spec *spec = codec->spec;
4714 int i;
4715 hda_nid_t pin;
4716
Takashi Iwaid4156932013-01-07 10:08:02 +01004717 for (i = 0; i < spec->autocfg.dig_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004718 set_output_and_unmute(codec, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004719 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01004720 if (pin) {
4721 struct nid_path *path;
Takashi Iwai2c12c302013-01-10 09:33:29 +01004722 restore_pin_ctl(codec, pin);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01004723 path = snd_hda_get_path_from_idx(codec, spec->digin_path);
4724 if (path)
4725 snd_hda_activate_path(codec, path, path->active, false);
4726 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004727}
4728
Takashi Iwai973e4972012-12-20 15:16:09 +01004729/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
4730 * invalid unsol tags by some reason
4731 */
4732static void clear_unsol_on_unused_pins(struct hda_codec *codec)
4733{
4734 int i;
4735
4736 for (i = 0; i < codec->init_pins.used; i++) {
4737 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
4738 hda_nid_t nid = pin->nid;
4739 if (is_jack_detectable(codec, nid) &&
4740 !snd_hda_jack_tbl_get(codec, nid))
4741 snd_hda_codec_update_cache(codec, nid, 0,
4742 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
4743 }
4744}
4745
Takashi Iwai5187ac12013-01-07 12:52:16 +01004746/*
4747 * initialize the generic spec;
4748 * this can be put as patch_ops.init function
4749 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004750int snd_hda_gen_init(struct hda_codec *codec)
4751{
4752 struct hda_gen_spec *spec = codec->spec;
4753
4754 if (spec->init_hook)
4755 spec->init_hook(codec);
4756
4757 snd_hda_apply_verbs(codec);
4758
Takashi Iwai3bbcd272012-12-20 11:50:58 +01004759 codec->cached_write = 1;
4760
Takashi Iwai352f7f92012-12-19 12:52:06 +01004761 init_multi_out(codec);
4762 init_extra_out(codec);
4763 init_multi_io(codec);
4764 init_analog_input(codec);
4765 init_input_src(codec);
4766 init_digital(codec);
4767
Takashi Iwai973e4972012-12-20 15:16:09 +01004768 clear_unsol_on_unused_pins(codec);
4769
Takashi Iwai352f7f92012-12-19 12:52:06 +01004770 /* call init functions of standard auto-mute helpers */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01004771 update_automute_all(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004772
Takashi Iwai3bbcd272012-12-20 11:50:58 +01004773 snd_hda_codec_flush_amp_cache(codec);
4774 snd_hda_codec_flush_cmd_cache(codec);
4775
Takashi Iwai352f7f92012-12-19 12:52:06 +01004776 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
4777 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
4778
4779 hda_call_check_power_status(codec, 0x01);
4780 return 0;
4781}
Takashi Iwaifce52a32013-01-07 12:42:48 +01004782EXPORT_SYMBOL_HDA(snd_hda_gen_init);
4783
Takashi Iwai5187ac12013-01-07 12:52:16 +01004784/*
4785 * free the generic spec;
4786 * this can be put as patch_ops.free function
4787 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01004788void snd_hda_gen_free(struct hda_codec *codec)
4789{
4790 snd_hda_gen_spec_free(codec->spec);
4791 kfree(codec->spec);
4792 codec->spec = NULL;
4793}
4794EXPORT_SYMBOL_HDA(snd_hda_gen_free);
4795
4796#ifdef CONFIG_PM
Takashi Iwai5187ac12013-01-07 12:52:16 +01004797/*
4798 * check the loopback power save state;
4799 * this can be put as patch_ops.check_power_status function
4800 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01004801int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
4802{
4803 struct hda_gen_spec *spec = codec->spec;
4804 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
4805}
4806EXPORT_SYMBOL_HDA(snd_hda_gen_check_power_status);
4807#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01004808
4809
4810/*
4811 * the generic codec support
4812 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813
Takashi Iwai352f7f92012-12-19 12:52:06 +01004814static const struct hda_codec_ops generic_patch_ops = {
4815 .build_controls = snd_hda_gen_build_controls,
4816 .build_pcms = snd_hda_gen_build_pcms,
4817 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01004818 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004819 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02004820#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01004821 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02004822#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823};
4824
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825int snd_hda_parse_generic_codec(struct hda_codec *codec)
4826{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004827 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828 int err;
4829
Takashi Iwaie560d8d2005-09-09 14:21:46 +02004830 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004831 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004833 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004836 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
4837 if (err < 0)
4838 return err;
4839
4840 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004841 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842 goto error;
4843
4844 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845 return 0;
4846
Takashi Iwai352f7f92012-12-19 12:52:06 +01004847error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01004848 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849 return err;
4850}
Takashi Iwaifce52a32013-01-07 12:42:48 +01004851EXPORT_SYMBOL_HDA(snd_hda_parse_generic_codec);