blob: 63d12efb7c1a5bbeda3e3210e0bbf7d2fa131b19 [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 Iwai38cf6f12012-12-21 14:09:42 +01001892 ret = 1;
1893 }
1894 unlock:
1895 mutex_unlock(&spec->pcm_mutex);
1896 return ret;
1897}
1898
1899static const struct snd_kcontrol_new indep_hp_ctl = {
1900 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1901 .name = "Independent HP",
1902 .info = indep_hp_info,
1903 .get = indep_hp_get,
1904 .put = indep_hp_put,
1905};
1906
1907
1908static int create_indep_hp_ctls(struct hda_codec *codec)
1909{
1910 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001911 hda_nid_t dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001912
1913 if (!spec->indep_hp)
1914 return 0;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001915 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
1916 dac = spec->multiout.dac_nids[0];
1917 else
1918 dac = spec->multiout.hp_out_nid[0];
1919 if (!dac) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001920 spec->indep_hp = 0;
1921 return 0;
1922 }
1923
1924 spec->indep_hp_enabled = false;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001925 spec->alt_dac_nid = dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001926 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
1927 return -ENOMEM;
1928 return 0;
1929}
1930
1931/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01001932 * channel mode enum control
1933 */
1934
1935static int ch_mode_info(struct snd_kcontrol *kcontrol,
1936 struct snd_ctl_elem_info *uinfo)
1937{
1938 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1939 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001940 int chs;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001941
1942 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1943 uinfo->count = 1;
1944 uinfo->value.enumerated.items = spec->multi_ios + 1;
1945 if (uinfo->value.enumerated.item > spec->multi_ios)
1946 uinfo->value.enumerated.item = spec->multi_ios;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001947 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
1948 sprintf(uinfo->value.enumerated.name, "%dch", chs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001949 return 0;
1950}
1951
1952static int ch_mode_get(struct snd_kcontrol *kcontrol,
1953 struct snd_ctl_elem_value *ucontrol)
1954{
1955 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1956 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001957 ucontrol->value.enumerated.item[0] =
1958 (spec->ext_channel_count - spec->min_channel_count) / 2;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001959 return 0;
1960}
1961
Takashi Iwai196c17662013-01-04 15:01:40 +01001962static inline struct nid_path *
1963get_multiio_path(struct hda_codec *codec, int idx)
1964{
1965 struct hda_gen_spec *spec = codec->spec;
1966 return snd_hda_get_path_from_idx(codec,
1967 spec->out_paths[spec->autocfg.line_outs + idx]);
1968}
1969
Takashi Iwaia5cc2502013-01-16 18:08:55 +01001970static void update_automute_all(struct hda_codec *codec);
1971
Takashi Iwai352f7f92012-12-19 12:52:06 +01001972static int set_multi_io(struct hda_codec *codec, int idx, bool output)
1973{
1974 struct hda_gen_spec *spec = codec->spec;
1975 hda_nid_t nid = spec->multi_io[idx].pin;
1976 struct nid_path *path;
1977
Takashi Iwai196c17662013-01-04 15:01:40 +01001978 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001979 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001981
1982 if (path->active == output)
1983 return 0;
1984
1985 if (output) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01001986 set_pin_target(codec, nid, PIN_OUT, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001987 snd_hda_activate_path(codec, path, true, true);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01001988 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001989 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01001990 set_pin_eapd(codec, nid, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001991 snd_hda_activate_path(codec, path, false, true);
Takashi Iwai2c12c302013-01-10 09:33:29 +01001992 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 }
Takashi Iwaia365fed2013-01-10 16:10:06 +01001994
1995 /* update jack retasking in case it modifies any of them */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01001996 update_automute_all(codec);
Takashi Iwaia365fed2013-01-10 16:10:06 +01001997
Takashi Iwai352f7f92012-12-19 12:52:06 +01001998 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999}
2000
Takashi Iwai352f7f92012-12-19 12:52:06 +01002001static int ch_mode_put(struct snd_kcontrol *kcontrol,
2002 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002004 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2005 struct hda_gen_spec *spec = codec->spec;
2006 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
Takashi Iwai352f7f92012-12-19 12:52:06 +01002008 ch = ucontrol->value.enumerated.item[0];
2009 if (ch < 0 || ch > spec->multi_ios)
2010 return -EINVAL;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002011 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002012 return 0;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002013 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002014 for (i = 0; i < spec->multi_ios; i++)
2015 set_multi_io(codec, i, i < ch);
2016 spec->multiout.max_channels = max(spec->ext_channel_count,
2017 spec->const_channel_count);
2018 if (spec->need_dac_fix)
2019 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 return 1;
2021}
2022
Takashi Iwai352f7f92012-12-19 12:52:06 +01002023static const struct snd_kcontrol_new channel_mode_enum = {
2024 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2025 .name = "Channel Mode",
2026 .info = ch_mode_info,
2027 .get = ch_mode_get,
2028 .put = ch_mode_put,
2029};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
Takashi Iwai352f7f92012-12-19 12:52:06 +01002031static int create_multi_channel_mode(struct hda_codec *codec)
2032{
2033 struct hda_gen_spec *spec = codec->spec;
2034
2035 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002036 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002037 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 return 0;
2040}
2041
Takashi Iwai352f7f92012-12-19 12:52:06 +01002042/*
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002043 * aamix loopback enable/disable switch
2044 */
2045
2046#define loopback_mixing_info indep_hp_info
2047
2048static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2049 struct snd_ctl_elem_value *ucontrol)
2050{
2051 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2052 struct hda_gen_spec *spec = codec->spec;
2053 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2054 return 0;
2055}
2056
2057static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002058 int nomix_path_idx, int mix_path_idx,
2059 int out_type)
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002060{
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002061 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002062 struct nid_path *nomix_path, *mix_path;
2063
2064 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2065 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2066 if (!nomix_path || !mix_path)
2067 return;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002068
2069 /* if HP aamix path is driven from a different DAC and the
2070 * independent HP mode is ON, can't turn on aamix path
2071 */
2072 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2073 mix_path->path[0] != spec->alt_dac_nid)
2074 do_mix = false;
2075
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002076 if (do_mix) {
2077 snd_hda_activate_path(codec, nomix_path, false, true);
2078 snd_hda_activate_path(codec, mix_path, true, true);
2079 } else {
2080 snd_hda_activate_path(codec, mix_path, false, true);
2081 snd_hda_activate_path(codec, nomix_path, true, true);
2082 }
2083}
2084
2085static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2086 struct snd_ctl_elem_value *ucontrol)
2087{
2088 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2089 struct hda_gen_spec *spec = codec->spec;
2090 unsigned int val = ucontrol->value.enumerated.item[0];
2091
2092 if (val == spec->aamix_mode)
2093 return 0;
2094 spec->aamix_mode = val;
2095 update_aamix_paths(codec, val, spec->out_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002096 spec->aamix_out_paths[0],
2097 spec->autocfg.line_out_type);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002098 update_aamix_paths(codec, val, spec->hp_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002099 spec->aamix_out_paths[1],
2100 AUTO_PIN_HP_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002101 update_aamix_paths(codec, val, spec->speaker_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002102 spec->aamix_out_paths[2],
2103 AUTO_PIN_SPEAKER_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002104 return 1;
2105}
2106
2107static const struct snd_kcontrol_new loopback_mixing_enum = {
2108 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2109 .name = "Loopback Mixing",
2110 .info = loopback_mixing_info,
2111 .get = loopback_mixing_get,
2112 .put = loopback_mixing_put,
2113};
2114
2115static int create_loopback_mixing_ctl(struct hda_codec *codec)
2116{
2117 struct hda_gen_spec *spec = codec->spec;
2118
2119 if (!spec->mixer_nid)
2120 return 0;
2121 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
2122 spec->aamix_out_paths[2]))
2123 return 0;
2124 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2125 return -ENOMEM;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002126 spec->have_aamix_ctl = 1;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002127 return 0;
2128}
2129
2130/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002131 * shared headphone/mic handling
2132 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002133
Takashi Iwai352f7f92012-12-19 12:52:06 +01002134static void call_update_outputs(struct hda_codec *codec);
2135
2136/* for shared I/O, change the pin-control accordingly */
2137static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic)
2138{
2139 struct hda_gen_spec *spec = codec->spec;
2140 unsigned int val;
2141 hda_nid_t pin = spec->autocfg.inputs[1].pin;
2142 /* NOTE: this assumes that there are only two inputs, the
2143 * first is the real internal mic and the second is HP/mic jack.
2144 */
2145
2146 val = snd_hda_get_default_vref(codec, pin);
2147
2148 /* This pin does not have vref caps - let's enable vref on pin 0x18
2149 instead, as suggested by Realtek */
2150 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2151 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2152 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2153 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01002154 snd_hda_set_pin_ctl_cache(codec, vref_pin,
2155 PIN_IN | (set_as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02002156 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002157
2158 val = set_as_mic ? val | PIN_IN : PIN_HP;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002159 set_pin_target(codec, pin, val, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002160
2161 spec->automute_speaker = !set_as_mic;
2162 call_update_outputs(codec);
2163}
2164
2165/* create a shared input with the headphone out */
2166static int create_shared_input(struct hda_codec *codec)
2167{
2168 struct hda_gen_spec *spec = codec->spec;
2169 struct auto_pin_cfg *cfg = &spec->autocfg;
2170 unsigned int defcfg;
2171 hda_nid_t nid;
2172
2173 /* only one internal input pin? */
2174 if (cfg->num_inputs != 1)
2175 return 0;
2176 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2177 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2178 return 0;
2179
2180 if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
2181 nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
2182 else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
2183 nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
2184 else
2185 return 0; /* both not available */
2186
2187 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2188 return 0; /* no input */
2189
2190 cfg->inputs[1].pin = nid;
2191 cfg->inputs[1].type = AUTO_PIN_MIC;
2192 cfg->num_inputs = 2;
2193 spec->shared_mic_hp = 1;
2194 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
2195 return 0;
2196}
2197
Takashi Iwai978e77e2013-01-10 16:57:58 +01002198/*
2199 * output jack mode
2200 */
2201static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2202 struct snd_ctl_elem_info *uinfo)
2203{
2204 static const char * const texts[] = {
2205 "Line Out", "Headphone Out",
2206 };
2207 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, texts);
2208}
2209
2210static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2211 struct snd_ctl_elem_value *ucontrol)
2212{
2213 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2214 hda_nid_t nid = kcontrol->private_value;
2215 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2216 ucontrol->value.enumerated.item[0] = 1;
2217 else
2218 ucontrol->value.enumerated.item[0] = 0;
2219 return 0;
2220}
2221
2222static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2223 struct snd_ctl_elem_value *ucontrol)
2224{
2225 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2226 hda_nid_t nid = kcontrol->private_value;
2227 unsigned int val;
2228
2229 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2230 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2231 return 0;
2232 snd_hda_set_pin_ctl_cache(codec, nid, val);
2233 return 1;
2234}
2235
2236static const struct snd_kcontrol_new out_jack_mode_enum = {
2237 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2238 .info = out_jack_mode_info,
2239 .get = out_jack_mode_get,
2240 .put = out_jack_mode_put,
2241};
2242
2243static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2244{
2245 struct hda_gen_spec *spec = codec->spec;
2246 int i;
2247
2248 for (i = 0; i < spec->kctls.used; i++) {
2249 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2250 if (!strcmp(kctl->name, name) && kctl->index == idx)
2251 return true;
2252 }
2253 return false;
2254}
2255
2256static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2257 char *name, size_t name_len)
2258{
2259 struct hda_gen_spec *spec = codec->spec;
2260 int idx = 0;
2261
2262 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2263 strlcat(name, " Jack Mode", name_len);
2264
2265 for (; find_kctl_name(codec, name, idx); idx++)
2266 ;
2267}
2268
2269static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2270 hda_nid_t *pins)
2271{
2272 struct hda_gen_spec *spec = codec->spec;
2273 int i;
2274
2275 for (i = 0; i < num_pins; i++) {
2276 hda_nid_t pin = pins[i];
2277 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2278 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV)) {
2279 struct snd_kcontrol_new *knew;
2280 char name[44];
2281 get_jack_mode_name(codec, pin, name, sizeof(name));
2282 knew = snd_hda_gen_add_kctl(spec, name,
2283 &out_jack_mode_enum);
2284 if (!knew)
2285 return -ENOMEM;
2286 knew->private_value = pin;
2287 }
2288 }
2289
2290 return 0;
2291}
2292
Takashi Iwai294765582013-01-17 09:52:11 +01002293/*
2294 * input jack mode
2295 */
2296
2297/* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2298#define NUM_VREFS 6
2299
2300static const char * const vref_texts[NUM_VREFS] = {
2301 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2302 "", "Mic 80pc Bias", "Mic 100pc Bias"
2303};
2304
2305static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2306{
2307 unsigned int pincap;
2308
2309 pincap = snd_hda_query_pin_caps(codec, pin);
2310 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2311 /* filter out unusual vrefs */
2312 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2313 return pincap;
2314}
2315
2316/* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2317static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2318{
2319 unsigned int i, n = 0;
2320
2321 for (i = 0; i < NUM_VREFS; i++) {
2322 if (vref_caps & (1 << i)) {
2323 if (n == item_idx)
2324 return i;
2325 n++;
2326 }
2327 }
2328 return 0;
2329}
2330
2331/* convert back from the vref ctl index to the enum item index */
2332static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2333{
2334 unsigned int i, n = 0;
2335
2336 for (i = 0; i < NUM_VREFS; i++) {
2337 if (i == idx)
2338 return n;
2339 if (vref_caps & (1 << i))
2340 n++;
2341 }
2342 return 0;
2343}
2344
2345static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2346 struct snd_ctl_elem_info *uinfo)
2347{
2348 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2349 hda_nid_t nid = kcontrol->private_value;
2350 unsigned int vref_caps = get_vref_caps(codec, nid);
2351
2352 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2353 vref_texts);
2354 /* set the right text */
2355 strcpy(uinfo->value.enumerated.name,
2356 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2357 return 0;
2358}
2359
2360static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2361 struct snd_ctl_elem_value *ucontrol)
2362{
2363 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2364 hda_nid_t nid = kcontrol->private_value;
2365 unsigned int vref_caps = get_vref_caps(codec, nid);
2366 unsigned int idx;
2367
2368 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2369 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2370 return 0;
2371}
2372
2373static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2374 struct snd_ctl_elem_value *ucontrol)
2375{
2376 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2377 hda_nid_t nid = kcontrol->private_value;
2378 unsigned int vref_caps = get_vref_caps(codec, nid);
2379 unsigned int val, idx;
2380
2381 val = snd_hda_codec_get_pin_target(codec, nid);
2382 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2383 if (idx == ucontrol->value.enumerated.item[0])
2384 return 0;
2385
2386 val &= ~AC_PINCTL_VREFEN;
2387 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2388 snd_hda_set_pin_ctl_cache(codec, nid, val);
2389 return 1;
2390}
2391
2392static const struct snd_kcontrol_new in_jack_mode_enum = {
2393 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2394 .info = in_jack_mode_info,
2395 .get = in_jack_mode_get,
2396 .put = in_jack_mode_put,
2397};
2398
2399static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2400{
2401 struct hda_gen_spec *spec = codec->spec;
2402 unsigned int defcfg;
2403 struct snd_kcontrol_new *knew;
2404 char name[44];
2405
2406 /* no jack mode for fixed pins */
2407 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2408 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2409 return 0;
2410
2411 /* no multiple vref caps? */
2412 if (hweight32(get_vref_caps(codec, pin)) <= 1)
2413 return 0;
2414
2415 get_jack_mode_name(codec, pin, name, sizeof(name));
2416 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2417 if (!knew)
2418 return -ENOMEM;
2419 knew->private_value = pin;
2420 return 0;
2421}
2422
Takashi Iwai352f7f92012-12-19 12:52:06 +01002423
2424/*
2425 * Parse input paths
2426 */
2427
2428#ifdef CONFIG_PM
2429/* add the powersave loopback-list entry */
2430static void add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
2431{
2432 struct hda_amp_list *list;
2433
2434 if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
2435 return;
2436 list = spec->loopback_list + spec->num_loopbacks;
2437 list->nid = mix;
2438 list->dir = HDA_INPUT;
2439 list->idx = idx;
2440 spec->num_loopbacks++;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002441 spec->loopback.amplist = spec->loopback_list;
2442}
2443#else
Takashi Iwai352f7f92012-12-19 12:52:06 +01002444#define add_loopback_list(spec, mix, idx) /* NOP */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002445#endif
2446
Takashi Iwai352f7f92012-12-19 12:52:06 +01002447/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01002448static int new_analog_input(struct hda_codec *codec, int input_idx,
2449 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002450 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002452 struct hda_gen_spec *spec = codec->spec;
2453 struct nid_path *path;
2454 unsigned int val;
2455 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456
Takashi Iwai352f7f92012-12-19 12:52:06 +01002457 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
2458 !nid_has_mute(codec, mix_nid, HDA_INPUT))
2459 return 0; /* no need for analog loopback */
2460
Takashi Iwai3ca529d2013-01-07 17:25:08 +01002461 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002462 if (!path)
2463 return -EINVAL;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002464 print_nid_path("loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01002465 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002466
2467 idx = path->idx[path->depth - 1];
2468 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
2469 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2470 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002471 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002473 path->ctls[NID_PATH_VOL_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 }
2475
Takashi Iwai352f7f92012-12-19 12:52:06 +01002476 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
2477 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2478 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002479 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002481 path->ctls[NID_PATH_MUTE_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 }
2483
Takashi Iwai352f7f92012-12-19 12:52:06 +01002484 path->active = true;
2485 add_loopback_list(spec, mix_nid, idx);
2486 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487}
2488
Takashi Iwai352f7f92012-12-19 12:52:06 +01002489static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002491 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
2492 return (pincap & AC_PINCAP_IN) != 0;
2493}
2494
2495/* Parse the codec tree and retrieve ADCs */
2496static int fill_adc_nids(struct hda_codec *codec)
2497{
2498 struct hda_gen_spec *spec = codec->spec;
2499 hda_nid_t nid;
2500 hda_nid_t *adc_nids = spec->adc_nids;
2501 int max_nums = ARRAY_SIZE(spec->adc_nids);
2502 int i, nums = 0;
2503
2504 nid = codec->start_nid;
2505 for (i = 0; i < codec->num_nodes; i++, nid++) {
2506 unsigned int caps = get_wcaps(codec, nid);
2507 int type = get_wcaps_type(caps);
2508
2509 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2510 continue;
2511 adc_nids[nums] = nid;
2512 if (++nums >= max_nums)
2513 break;
2514 }
2515 spec->num_adc_nids = nums;
Takashi Iwai0ffd5342013-01-17 15:53:29 +01002516
2517 /* copy the detected ADCs to all_adcs[] */
2518 spec->num_all_adcs = nums;
2519 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
2520
Takashi Iwai352f7f92012-12-19 12:52:06 +01002521 return nums;
2522}
2523
2524/* filter out invalid adc_nids that don't give all active input pins;
2525 * if needed, check whether dynamic ADC-switching is available
2526 */
2527static int check_dyn_adc_switch(struct hda_codec *codec)
2528{
2529 struct hda_gen_spec *spec = codec->spec;
2530 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002531 unsigned int ok_bits;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002532 int i, n, nums;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002533
2534 again:
2535 nums = 0;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002536 ok_bits = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002537 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002538 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002539 if (!spec->input_paths[i][n])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002540 break;
2541 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002542 if (i >= imux->num_items) {
2543 ok_bits |= (1 << n);
2544 nums++;
2545 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002546 }
2547
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002548 if (!ok_bits) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002549 if (spec->shared_mic_hp) {
2550 spec->shared_mic_hp = 0;
2551 imux->num_items = 1;
2552 goto again;
2553 }
2554
2555 /* check whether ADC-switch is possible */
2556 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002557 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002558 if (spec->input_paths[i][n]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002559 spec->dyn_adc_idx[i] = n;
2560 break;
2561 }
2562 }
2563 }
2564
2565 snd_printdd("hda-codec: enabling ADC switching\n");
2566 spec->dyn_adc_switch = 1;
2567 } else if (nums != spec->num_adc_nids) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002568 /* shrink the invalid adcs and input paths */
2569 nums = 0;
2570 for (n = 0; n < spec->num_adc_nids; n++) {
2571 if (!(ok_bits & (1 << n)))
2572 continue;
2573 if (n != nums) {
2574 spec->adc_nids[nums] = spec->adc_nids[n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002575 for (i = 0; i < imux->num_items; i++) {
2576 invalidate_nid_path(codec,
2577 spec->input_paths[i][nums]);
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002578 spec->input_paths[i][nums] =
2579 spec->input_paths[i][n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002580 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002581 }
2582 nums++;
2583 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002584 spec->num_adc_nids = nums;
2585 }
2586
2587 if (imux->num_items == 1 || spec->shared_mic_hp) {
2588 snd_printdd("hda-codec: reducing to a single ADC\n");
2589 spec->num_adc_nids = 1; /* reduce to a single ADC */
2590 }
2591
2592 /* single index for individual volumes ctls */
2593 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
2594 spec->num_adc_nids = 1;
2595
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 return 0;
2597}
2598
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002599/* parse capture source paths from the given pin and create imux items */
2600static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
Takashi Iwai9dba2052013-01-18 10:01:15 +01002601 int cfg_idx, int num_adcs,
2602 const char *label, int anchor)
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002603{
2604 struct hda_gen_spec *spec = codec->spec;
2605 struct hda_input_mux *imux = &spec->input_mux;
2606 int imux_idx = imux->num_items;
2607 bool imux_added = false;
2608 int c;
2609
2610 for (c = 0; c < num_adcs; c++) {
2611 struct nid_path *path;
2612 hda_nid_t adc = spec->adc_nids[c];
2613
2614 if (!is_reachable_path(codec, pin, adc))
2615 continue;
2616 path = snd_hda_add_new_path(codec, pin, adc, anchor);
2617 if (!path)
2618 continue;
2619 print_nid_path("input", path);
2620 spec->input_paths[imux_idx][c] =
2621 snd_hda_get_path_idx(codec, path);
2622
2623 if (!imux_added) {
2624 spec->imux_pins[imux->num_items] = pin;
Takashi Iwai9dba2052013-01-18 10:01:15 +01002625 snd_hda_add_imux_item(imux, label, cfg_idx, NULL);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002626 imux_added = true;
2627 }
2628 }
2629
2630 return 0;
2631}
2632
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002634 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 */
Takashi Iwai9dba2052013-01-18 10:01:15 +01002636
Takashi Iwaic9700422013-01-18 10:17:30 +01002637/* fill the label for each input at first */
2638static int fill_input_pin_labels(struct hda_codec *codec)
2639{
2640 struct hda_gen_spec *spec = codec->spec;
2641 const struct auto_pin_cfg *cfg = &spec->autocfg;
2642 int i;
2643
2644 for (i = 0; i < cfg->num_inputs; i++) {
2645 hda_nid_t pin = cfg->inputs[i].pin;
2646 const char *label;
2647 int j, idx;
2648
2649 if (!is_input_pin(codec, pin))
2650 continue;
2651
2652 label = hda_get_autocfg_input_label(codec, cfg, i);
2653 idx = 0;
David Henningsson8e8db7f2013-01-18 15:43:02 +01002654 for (j = i - 1; j >= 0; j--) {
Takashi Iwaic9700422013-01-18 10:17:30 +01002655 if (spec->input_labels[j] &&
2656 !strcmp(spec->input_labels[j], label)) {
2657 idx = spec->input_label_idxs[j] + 1;
2658 break;
2659 }
2660 }
2661
2662 spec->input_labels[i] = label;
2663 spec->input_label_idxs[i] = idx;
2664 }
2665
2666 return 0;
2667}
2668
Takashi Iwai9dba2052013-01-18 10:01:15 +01002669#define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
2670
Takashi Iwai352f7f92012-12-19 12:52:06 +01002671static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002672{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002673 struct hda_gen_spec *spec = codec->spec;
2674 const struct auto_pin_cfg *cfg = &spec->autocfg;
2675 hda_nid_t mixer = spec->mixer_nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002676 int num_adcs;
Takashi Iwaic9700422013-01-18 10:17:30 +01002677 int i, err;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002678 unsigned int val;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002679
Takashi Iwai352f7f92012-12-19 12:52:06 +01002680 num_adcs = fill_adc_nids(codec);
2681 if (num_adcs < 0)
2682 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002683
Takashi Iwaic9700422013-01-18 10:17:30 +01002684 err = fill_input_pin_labels(codec);
2685 if (err < 0)
2686 return err;
2687
Takashi Iwai352f7f92012-12-19 12:52:06 +01002688 for (i = 0; i < cfg->num_inputs; i++) {
2689 hda_nid_t pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690
Takashi Iwai352f7f92012-12-19 12:52:06 +01002691 pin = cfg->inputs[i].pin;
2692 if (!is_input_pin(codec, pin))
2693 continue;
2694
Takashi Iwai2c12c302013-01-10 09:33:29 +01002695 val = PIN_IN;
2696 if (cfg->inputs[i].type == AUTO_PIN_MIC)
2697 val |= snd_hda_get_default_vref(codec, pin);
2698 set_pin_target(codec, pin, val, false);
2699
Takashi Iwai352f7f92012-12-19 12:52:06 +01002700 if (mixer) {
2701 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01002702 err = new_analog_input(codec, i, pin,
Takashi Iwaic9700422013-01-18 10:17:30 +01002703 spec->input_labels[i],
2704 spec->input_label_idxs[i],
2705 mixer);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002706 if (err < 0)
2707 return err;
2708 }
2709 }
2710
Takashi Iwaic9700422013-01-18 10:17:30 +01002711 err = parse_capture_source(codec, pin, i, num_adcs,
2712 spec->input_labels[i], -mixer);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002713 if (err < 0)
2714 return err;
Takashi Iwai294765582013-01-17 09:52:11 +01002715
2716 if (spec->add_in_jack_modes) {
2717 err = create_in_jack_mode(codec, pin);
2718 if (err < 0)
2719 return err;
2720 }
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002721 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002722
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002723 if (mixer && spec->add_stereo_mix_input) {
Takashi Iwai9dba2052013-01-18 10:01:15 +01002724 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002725 "Stereo Mix", 0);
2726 if (err < 0)
2727 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002728 }
2729
2730 return 0;
2731}
2732
2733
2734/*
2735 * input source mux
2736 */
2737
Takashi Iwaic697b712013-01-07 17:09:26 +01002738/* get the input path specified by the given adc and imux indices */
2739static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002740{
2741 struct hda_gen_spec *spec = codec->spec;
David Henningssonb56fa1e2013-01-16 11:45:35 +01002742 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
2743 snd_BUG();
2744 return NULL;
2745 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002746 if (spec->dyn_adc_switch)
2747 adc_idx = spec->dyn_adc_idx[imux_idx];
David Henningssond3d982f2013-01-18 15:43:01 +01002748 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
David Henningssonb56fa1e2013-01-16 11:45:35 +01002749 snd_BUG();
2750 return NULL;
2751 }
Takashi Iwaic697b712013-01-07 17:09:26 +01002752 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002753}
2754
2755static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2756 unsigned int idx);
2757
2758static int mux_enum_info(struct snd_kcontrol *kcontrol,
2759 struct snd_ctl_elem_info *uinfo)
2760{
2761 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2762 struct hda_gen_spec *spec = codec->spec;
2763 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
2764}
2765
2766static int mux_enum_get(struct snd_kcontrol *kcontrol,
2767 struct snd_ctl_elem_value *ucontrol)
2768{
2769 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2770 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai2a8d5392013-01-18 16:23:25 +01002771 /* the ctls are created at once with multiple counts */
2772 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002773
2774 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
2775 return 0;
2776}
2777
2778static int mux_enum_put(struct snd_kcontrol *kcontrol,
2779 struct snd_ctl_elem_value *ucontrol)
2780{
2781 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Takashi Iwai2a8d5392013-01-18 16:23:25 +01002782 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002783 return mux_select(codec, adc_idx,
2784 ucontrol->value.enumerated.item[0]);
2785}
2786
Takashi Iwai352f7f92012-12-19 12:52:06 +01002787static const struct snd_kcontrol_new cap_src_temp = {
2788 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2789 .name = "Input Source",
2790 .info = mux_enum_info,
2791 .get = mux_enum_get,
2792 .put = mux_enum_put,
2793};
2794
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002795/*
2796 * capture volume and capture switch ctls
2797 */
2798
Takashi Iwai352f7f92012-12-19 12:52:06 +01002799typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
2800 struct snd_ctl_elem_value *ucontrol);
2801
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002802/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002803static int cap_put_caller(struct snd_kcontrol *kcontrol,
2804 struct snd_ctl_elem_value *ucontrol,
2805 put_call_t func, int type)
2806{
2807 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2808 struct hda_gen_spec *spec = codec->spec;
2809 const struct hda_input_mux *imux;
2810 struct nid_path *path;
2811 int i, adc_idx, err = 0;
2812
2813 imux = &spec->input_mux;
David Henningssona053d1e2013-01-16 11:45:36 +01002814 adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002815 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002816 /* we use the cache-only update at first since multiple input paths
2817 * may shared the same amp; by updating only caches, the redundant
2818 * writes to hardware can be reduced.
2819 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002820 codec->cached_write = 1;
2821 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01002822 path = get_input_path(codec, adc_idx, i);
2823 if (!path || !path->ctls[type])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002824 continue;
2825 kcontrol->private_value = path->ctls[type];
2826 err = func(kcontrol, ucontrol);
2827 if (err < 0)
2828 goto error;
2829 }
2830 error:
2831 codec->cached_write = 0;
2832 mutex_unlock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002833 snd_hda_codec_flush_amp_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002834 if (err >= 0 && spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01002835 spec->cap_sync_hook(codec, ucontrol);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002836 return err;
2837}
2838
2839/* capture volume ctl callbacks */
2840#define cap_vol_info snd_hda_mixer_amp_volume_info
2841#define cap_vol_get snd_hda_mixer_amp_volume_get
2842#define cap_vol_tlv snd_hda_mixer_amp_tlv
2843
2844static int cap_vol_put(struct snd_kcontrol *kcontrol,
2845 struct snd_ctl_elem_value *ucontrol)
2846{
2847 return cap_put_caller(kcontrol, ucontrol,
2848 snd_hda_mixer_amp_volume_put,
2849 NID_PATH_VOL_CTL);
2850}
2851
2852static const struct snd_kcontrol_new cap_vol_temp = {
2853 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2854 .name = "Capture Volume",
2855 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
2856 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2857 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
2858 .info = cap_vol_info,
2859 .get = cap_vol_get,
2860 .put = cap_vol_put,
2861 .tlv = { .c = cap_vol_tlv },
2862};
2863
2864/* capture switch ctl callbacks */
2865#define cap_sw_info snd_ctl_boolean_stereo_info
2866#define cap_sw_get snd_hda_mixer_amp_switch_get
2867
2868static int cap_sw_put(struct snd_kcontrol *kcontrol,
2869 struct snd_ctl_elem_value *ucontrol)
2870{
Takashi Iwaia90229e2013-01-18 14:10:00 +01002871 return cap_put_caller(kcontrol, ucontrol,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002872 snd_hda_mixer_amp_switch_put,
2873 NID_PATH_MUTE_CTL);
2874}
2875
2876static const struct snd_kcontrol_new cap_sw_temp = {
2877 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2878 .name = "Capture Switch",
2879 .info = cap_sw_info,
2880 .get = cap_sw_get,
2881 .put = cap_sw_put,
2882};
2883
2884static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
2885{
2886 hda_nid_t nid;
2887 int i, depth;
2888
2889 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
2890 for (depth = 0; depth < 3; depth++) {
2891 if (depth >= path->depth)
2892 return -EINVAL;
2893 i = path->depth - depth - 1;
2894 nid = path->path[i];
2895 if (!path->ctls[NID_PATH_VOL_CTL]) {
2896 if (nid_has_volume(codec, nid, HDA_OUTPUT))
2897 path->ctls[NID_PATH_VOL_CTL] =
2898 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2899 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
2900 int idx = path->idx[i];
2901 if (!depth && codec->single_adc_amp)
2902 idx = 0;
2903 path->ctls[NID_PATH_VOL_CTL] =
2904 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2905 }
2906 }
2907 if (!path->ctls[NID_PATH_MUTE_CTL]) {
2908 if (nid_has_mute(codec, nid, HDA_OUTPUT))
2909 path->ctls[NID_PATH_MUTE_CTL] =
2910 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2911 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
2912 int idx = path->idx[i];
2913 if (!depth && codec->single_adc_amp)
2914 idx = 0;
2915 path->ctls[NID_PATH_MUTE_CTL] =
2916 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2917 }
2918 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01002919 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 return 0;
2921}
2922
Takashi Iwai352f7f92012-12-19 12:52:06 +01002923static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002925 struct hda_gen_spec *spec = codec->spec;
2926 struct auto_pin_cfg *cfg = &spec->autocfg;
2927 unsigned int val;
2928 int i;
2929
2930 if (!spec->inv_dmic_split)
2931 return false;
2932 for (i = 0; i < cfg->num_inputs; i++) {
2933 if (cfg->inputs[i].pin != nid)
2934 continue;
2935 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2936 return false;
2937 val = snd_hda_codec_get_pincfg(codec, nid);
2938 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
2939 }
2940 return false;
2941}
2942
Takashi Iwaia90229e2013-01-18 14:10:00 +01002943/* capture switch put callback for a single control with hook call */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01002944static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
2945 struct snd_ctl_elem_value *ucontrol)
2946{
2947 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2948 struct hda_gen_spec *spec = codec->spec;
2949 int ret;
2950
2951 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2952 if (ret < 0)
2953 return ret;
2954
Takashi Iwaia90229e2013-01-18 14:10:00 +01002955 if (spec->cap_sync_hook)
2956 spec->cap_sync_hook(codec, ucontrol);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01002957
2958 return ret;
2959}
2960
Takashi Iwai352f7f92012-12-19 12:52:06 +01002961static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
2962 int idx, bool is_switch, unsigned int ctl,
2963 bool inv_dmic)
2964{
2965 struct hda_gen_spec *spec = codec->spec;
2966 char tmpname[44];
2967 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
2968 const char *sfx = is_switch ? "Switch" : "Volume";
2969 unsigned int chs = inv_dmic ? 1 : 3;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01002970 struct snd_kcontrol_new *knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002971
2972 if (!ctl)
2973 return 0;
2974
2975 if (label)
2976 snprintf(tmpname, sizeof(tmpname),
2977 "%s Capture %s", label, sfx);
2978 else
2979 snprintf(tmpname, sizeof(tmpname),
2980 "Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01002981 knew = add_control(spec, type, tmpname, idx,
2982 amp_val_replace_channels(ctl, chs));
2983 if (!knew)
2984 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01002985 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01002986 knew->put = cap_single_sw_put;
2987 if (!inv_dmic)
2988 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002989
2990 /* Make independent right kcontrol */
2991 if (label)
2992 snprintf(tmpname, sizeof(tmpname),
2993 "Inverted %s Capture %s", label, sfx);
2994 else
2995 snprintf(tmpname, sizeof(tmpname),
2996 "Inverted Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01002997 knew = add_control(spec, type, tmpname, idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002998 amp_val_replace_channels(ctl, 2));
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01002999 if (!knew)
3000 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003001 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003002 knew->put = cap_single_sw_put;
3003 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003004}
3005
3006/* create single (and simple) capture volume and switch controls */
3007static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3008 unsigned int vol_ctl, unsigned int sw_ctl,
3009 bool inv_dmic)
3010{
3011 int err;
3012 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3013 if (err < 0)
3014 return err;
3015 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3016 if (err < 0)
3017 return err;
3018 return 0;
3019}
3020
3021/* create bound capture volume and switch controls */
3022static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3023 unsigned int vol_ctl, unsigned int sw_ctl)
3024{
3025 struct hda_gen_spec *spec = codec->spec;
3026 struct snd_kcontrol_new *knew;
3027
3028 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003029 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003030 if (!knew)
3031 return -ENOMEM;
3032 knew->index = idx;
3033 knew->private_value = vol_ctl;
3034 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3035 }
3036 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003037 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003038 if (!knew)
3039 return -ENOMEM;
3040 knew->index = idx;
3041 knew->private_value = sw_ctl;
3042 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3043 }
3044 return 0;
3045}
3046
3047/* return the vol ctl when used first in the imux list */
3048static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3049{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003050 struct nid_path *path;
3051 unsigned int ctl;
3052 int i;
3053
Takashi Iwaic697b712013-01-07 17:09:26 +01003054 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003055 if (!path)
3056 return 0;
3057 ctl = path->ctls[type];
3058 if (!ctl)
3059 return 0;
3060 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003061 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003062 if (path && path->ctls[type] == ctl)
3063 return 0;
3064 }
3065 return ctl;
3066}
3067
3068/* create individual capture volume and switch controls per input */
3069static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3070{
3071 struct hda_gen_spec *spec = codec->spec;
3072 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaic9700422013-01-18 10:17:30 +01003073 int i, err, type;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003074
3075 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003076 bool inv_dmic;
Takashi Iwaic9700422013-01-18 10:17:30 +01003077 int idx;
Takashi Iwai9dba2052013-01-18 10:01:15 +01003078
Takashi Iwaic9700422013-01-18 10:17:30 +01003079 idx = imux->items[i].index;
3080 if (idx >= spec->autocfg.num_inputs)
Takashi Iwai9dba2052013-01-18 10:01:15 +01003081 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003082 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3083
3084 for (type = 0; type < 2; type++) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003085 err = add_single_cap_ctl(codec,
3086 spec->input_labels[idx],
3087 spec->input_label_idxs[idx],
3088 type,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003089 get_first_cap_ctl(codec, i, type),
3090 inv_dmic);
3091 if (err < 0)
3092 return err;
3093 }
3094 }
3095 return 0;
3096}
3097
3098static int create_capture_mixers(struct hda_codec *codec)
3099{
3100 struct hda_gen_spec *spec = codec->spec;
3101 struct hda_input_mux *imux = &spec->input_mux;
3102 int i, n, nums, err;
3103
3104 if (spec->dyn_adc_switch)
3105 nums = 1;
3106 else
3107 nums = spec->num_adc_nids;
3108
3109 if (!spec->auto_mic && imux->num_items > 1) {
3110 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01003111 const char *name;
3112 name = nums > 1 ? "Input Source" : "Capture Source";
3113 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003114 if (!knew)
3115 return -ENOMEM;
3116 knew->count = nums;
3117 }
3118
3119 for (n = 0; n < nums; n++) {
3120 bool multi = false;
David Henningsson99a55922013-01-16 15:58:44 +01003121 bool multi_cap_vol = spec->multi_cap_vol;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003122 bool inv_dmic = false;
3123 int vol, sw;
3124
3125 vol = sw = 0;
3126 for (i = 0; i < imux->num_items; i++) {
3127 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01003128 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003129 if (!path)
3130 continue;
3131 parse_capvol_in_path(codec, path);
3132 if (!vol)
3133 vol = path->ctls[NID_PATH_VOL_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003134 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003135 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003136 if (!same_amp_caps(codec, vol,
3137 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3138 multi_cap_vol = true;
3139 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003140 if (!sw)
3141 sw = path->ctls[NID_PATH_MUTE_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003142 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003143 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003144 if (!same_amp_caps(codec, sw,
3145 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3146 multi_cap_vol = true;
3147 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003148 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3149 inv_dmic = true;
3150 }
3151
3152 if (!multi)
3153 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3154 inv_dmic);
David Henningsson99a55922013-01-16 15:58:44 +01003155 else if (!multi_cap_vol)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003156 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3157 else
3158 err = create_multi_cap_vol_ctl(codec);
3159 if (err < 0)
3160 return err;
3161 }
3162
3163 return 0;
3164}
3165
3166/*
3167 * add mic boosts if needed
3168 */
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003169
3170/* check whether the given amp is feasible as a boost volume */
3171static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3172 int dir, int idx)
3173{
3174 unsigned int step;
3175
3176 if (!nid_has_volume(codec, nid, dir) ||
3177 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3178 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3179 return false;
3180
3181 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3182 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3183 if (step < 0x20)
3184 return false;
3185 return true;
3186}
3187
3188/* look for a boost amp in a widget close to the pin */
3189static unsigned int look_for_boost_amp(struct hda_codec *codec,
3190 struct nid_path *path)
3191{
3192 unsigned int val = 0;
3193 hda_nid_t nid;
3194 int depth;
3195
3196 for (depth = 0; depth < 3; depth++) {
3197 if (depth >= path->depth - 1)
3198 break;
3199 nid = path->path[depth];
3200 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3201 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3202 break;
3203 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3204 path->idx[depth])) {
3205 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3206 HDA_INPUT);
3207 break;
3208 }
3209 }
3210
3211 return val;
3212}
3213
Takashi Iwai352f7f92012-12-19 12:52:06 +01003214static int parse_mic_boost(struct hda_codec *codec)
3215{
3216 struct hda_gen_spec *spec = codec->spec;
3217 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003218 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003219 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003220
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003221 if (!spec->num_adc_nids)
3222 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003223
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003224 for (i = 0; i < imux->num_items; i++) {
3225 struct nid_path *path;
3226 unsigned int val;
3227 int idx;
3228 char boost_label[44];
David Henningsson02aba552013-01-16 15:58:43 +01003229
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003230 idx = imux->items[i].index;
3231 if (idx >= imux->num_items)
3232 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003233
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003234 /* check only line-in and mic pins */
Takashi Iwai1799cdd52013-01-18 14:37:16 +01003235 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003236 continue;
3237
3238 path = get_input_path(codec, 0, i);
3239 if (!path)
3240 continue;
3241
3242 val = look_for_boost_amp(codec, path);
3243 if (!val)
3244 continue;
3245
3246 /* create a boost control */
3247 snprintf(boost_label, sizeof(boost_label),
3248 "%s Boost Volume", spec->input_labels[idx]);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003249 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3250 spec->input_label_idxs[idx], val))
3251 return -ENOMEM;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003252
3253 path->ctls[NID_PATH_BOOST_CTL] = val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003254 }
3255 return 0;
3256}
3257
3258/*
3259 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3260 */
3261static void parse_digital(struct hda_codec *codec)
3262{
3263 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003264 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003265 int i, nums;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003266 hda_nid_t dig_nid, pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003267
3268 /* support multiple SPDIFs; the secondary is set up as a slave */
3269 nums = 0;
3270 for (i = 0; i < spec->autocfg.dig_outs; i++) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003271 pin = spec->autocfg.dig_out_pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003272 dig_nid = look_for_dac(codec, pin, true);
3273 if (!dig_nid)
3274 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003275 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003276 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003277 continue;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003278 print_nid_path("digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01003279 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01003280 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003281 set_pin_target(codec, pin, PIN_OUT, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003282 if (!nums) {
3283 spec->multiout.dig_out_nid = dig_nid;
3284 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3285 } else {
3286 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3287 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
3288 break;
3289 spec->slave_dig_outs[nums - 1] = dig_nid;
3290 }
3291 nums++;
3292 }
3293
3294 if (spec->autocfg.dig_in_pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003295 pin = spec->autocfg.dig_in_pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003296 dig_nid = codec->start_nid;
3297 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003298 unsigned int wcaps = get_wcaps(codec, dig_nid);
3299 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3300 continue;
3301 if (!(wcaps & AC_WCAP_DIGITAL))
3302 continue;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003303 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003304 if (path) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003305 print_nid_path("digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003306 path->active = true;
3307 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003308 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003309 set_pin_target(codec, pin, PIN_IN, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003310 break;
3311 }
3312 }
3313 }
3314}
3315
3316
3317/*
3318 * input MUX handling
3319 */
3320
3321static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3322
3323/* select the given imux item; either unmute exclusively or select the route */
3324static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3325 unsigned int idx)
3326{
3327 struct hda_gen_spec *spec = codec->spec;
3328 const struct hda_input_mux *imux;
3329 struct nid_path *path;
3330
3331 imux = &spec->input_mux;
3332 if (!imux->num_items)
3333 return 0;
3334
3335 if (idx >= imux->num_items)
3336 idx = imux->num_items - 1;
3337 if (spec->cur_mux[adc_idx] == idx)
3338 return 0;
3339
Takashi Iwaic697b712013-01-07 17:09:26 +01003340 path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003341 if (!path)
3342 return 0;
3343 if (path->active)
3344 snd_hda_activate_path(codec, path, false, false);
3345
3346 spec->cur_mux[adc_idx] = idx;
3347
3348 if (spec->shared_mic_hp)
3349 update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
3350
3351 if (spec->dyn_adc_switch)
3352 dyn_adc_pcm_resetup(codec, idx);
3353
Takashi Iwaic697b712013-01-07 17:09:26 +01003354 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003355 if (!path)
3356 return 0;
3357 if (path->active)
3358 return 0;
3359 snd_hda_activate_path(codec, path, true, false);
3360 if (spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01003361 spec->cap_sync_hook(codec, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003362 return 1;
3363}
3364
3365
3366/*
3367 * Jack detections for HP auto-mute and mic-switch
3368 */
3369
3370/* check each pin in the given array; returns true if any of them is plugged */
3371static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
3372{
3373 int i, present = 0;
3374
3375 for (i = 0; i < num_pins; i++) {
3376 hda_nid_t nid = pins[i];
3377 if (!nid)
3378 break;
Takashi Iwai0b4df932013-01-10 09:45:13 +01003379 /* don't detect pins retasked as inputs */
3380 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
3381 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003382 present |= snd_hda_jack_detect(codec, nid);
3383 }
3384 return present;
3385}
3386
3387/* standard HP/line-out auto-mute helper */
3388static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
Takashi Iwai2c12c302013-01-10 09:33:29 +01003389 bool mute)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003390{
3391 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003392 int i;
3393
3394 for (i = 0; i < num_pins; i++) {
3395 hda_nid_t nid = pins[i];
3396 unsigned int val;
3397 if (!nid)
3398 break;
3399 /* don't reset VREF value in case it's controlling
3400 * the amp (see alc861_fixup_asus_amp_vref_0f())
3401 */
Takashi Iwai2c12c302013-01-10 09:33:29 +01003402 if (spec->keep_vref_in_automute)
3403 val = snd_hda_codec_get_pin_target(codec, nid) & ~PIN_HP;
3404 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01003405 val = 0;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003406 if (!mute)
3407 val |= snd_hda_codec_get_pin_target(codec, nid);
3408 /* here we call update_pin_ctl() so that the pinctl is changed
3409 * without changing the pinctl target value;
3410 * the original target value will be still referred at the
3411 * init / resume again
3412 */
3413 update_pin_ctl(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01003414 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003415 }
3416}
3417
3418/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003419void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003420{
3421 struct hda_gen_spec *spec = codec->spec;
3422 int on;
3423
3424 /* Control HP pins/amps depending on master_mute state;
3425 * in general, HP pins/amps control should be enabled in all cases,
3426 * but currently set only for master_mute, just to be safe
3427 */
3428 if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
3429 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003430 spec->autocfg.hp_pins, spec->master_mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003431
3432 if (!spec->automute_speaker)
3433 on = 0;
3434 else
3435 on = spec->hp_jack_present | spec->line_jack_present;
3436 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003437 spec->speaker_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003438 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003439 spec->autocfg.speaker_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003440
3441 /* toggle line-out mutes if needed, too */
3442 /* if LO is a copy of either HP or Speaker, don't need to handle it */
3443 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
3444 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
3445 return;
3446 if (!spec->automute_lo)
3447 on = 0;
3448 else
3449 on = spec->hp_jack_present;
3450 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003451 spec->line_out_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003452 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003453 spec->autocfg.line_out_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003454}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003455EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003456
3457static void call_update_outputs(struct hda_codec *codec)
3458{
3459 struct hda_gen_spec *spec = codec->spec;
3460 if (spec->automute_hook)
3461 spec->automute_hook(codec);
3462 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01003463 snd_hda_gen_update_outputs(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003464}
3465
3466/* standard HP-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003467void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003468{
3469 struct hda_gen_spec *spec = codec->spec;
3470
3471 spec->hp_jack_present =
3472 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
3473 spec->autocfg.hp_pins);
3474 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
3475 return;
3476 call_update_outputs(codec);
3477}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003478EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003479
3480/* standard line-out-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003481void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003482{
3483 struct hda_gen_spec *spec = codec->spec;
3484
3485 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3486 return;
3487 /* check LO jack only when it's different from HP */
3488 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
3489 return;
3490
3491 spec->line_jack_present =
3492 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3493 spec->autocfg.line_out_pins);
3494 if (!spec->automute_speaker || !spec->detect_lo)
3495 return;
3496 call_update_outputs(codec);
3497}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003498EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003499
3500/* standard mic auto-switch helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003501void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003502{
3503 struct hda_gen_spec *spec = codec->spec;
3504 int i;
3505
3506 if (!spec->auto_mic)
3507 return;
3508
3509 for (i = spec->am_num_entries - 1; i > 0; i--) {
Takashi Iwai0b4df932013-01-10 09:45:13 +01003510 hda_nid_t pin = spec->am_entry[i].pin;
3511 /* don't detect pins retasked as outputs */
3512 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
3513 continue;
3514 if (snd_hda_jack_detect(codec, pin)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003515 mux_select(codec, 0, spec->am_entry[i].idx);
3516 return;
3517 }
3518 }
3519 mux_select(codec, 0, spec->am_entry[0].idx);
3520}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003521EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003522
Takashi Iwaia5cc2502013-01-16 18:08:55 +01003523/* update jack retasking */
3524static void update_automute_all(struct hda_codec *codec)
3525{
3526 struct hda_gen_spec *spec = codec->spec;
3527
3528 if (spec->hp_automute_hook)
3529 spec->hp_automute_hook(codec, NULL);
3530 else
3531 snd_hda_gen_hp_automute(codec, NULL);
3532 if (spec->line_automute_hook)
3533 spec->line_automute_hook(codec, NULL);
3534 else
3535 snd_hda_gen_line_automute(codec, NULL);
3536 if (spec->mic_autoswitch_hook)
3537 spec->mic_autoswitch_hook(codec, NULL);
3538 else
3539 snd_hda_gen_mic_autoswitch(codec, NULL);
3540}
3541
Takashi Iwai352f7f92012-12-19 12:52:06 +01003542/*
3543 * Auto-Mute mode mixer enum support
3544 */
3545static int automute_mode_info(struct snd_kcontrol *kcontrol,
3546 struct snd_ctl_elem_info *uinfo)
3547{
3548 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3549 struct hda_gen_spec *spec = codec->spec;
3550 static const char * const texts3[] = {
3551 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02003552 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553
Takashi Iwai352f7f92012-12-19 12:52:06 +01003554 if (spec->automute_speaker_possible && spec->automute_lo_possible)
3555 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
3556 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
3557}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558
Takashi Iwai352f7f92012-12-19 12:52:06 +01003559static int automute_mode_get(struct snd_kcontrol *kcontrol,
3560 struct snd_ctl_elem_value *ucontrol)
3561{
3562 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3563 struct hda_gen_spec *spec = codec->spec;
3564 unsigned int val = 0;
3565 if (spec->automute_speaker)
3566 val++;
3567 if (spec->automute_lo)
3568 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003569
Takashi Iwai352f7f92012-12-19 12:52:06 +01003570 ucontrol->value.enumerated.item[0] = val;
3571 return 0;
3572}
3573
3574static int automute_mode_put(struct snd_kcontrol *kcontrol,
3575 struct snd_ctl_elem_value *ucontrol)
3576{
3577 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3578 struct hda_gen_spec *spec = codec->spec;
3579
3580 switch (ucontrol->value.enumerated.item[0]) {
3581 case 0:
3582 if (!spec->automute_speaker && !spec->automute_lo)
3583 return 0;
3584 spec->automute_speaker = 0;
3585 spec->automute_lo = 0;
3586 break;
3587 case 1:
3588 if (spec->automute_speaker_possible) {
3589 if (!spec->automute_lo && spec->automute_speaker)
3590 return 0;
3591 spec->automute_speaker = 1;
3592 spec->automute_lo = 0;
3593 } else if (spec->automute_lo_possible) {
3594 if (spec->automute_lo)
3595 return 0;
3596 spec->automute_lo = 1;
3597 } else
3598 return -EINVAL;
3599 break;
3600 case 2:
3601 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
3602 return -EINVAL;
3603 if (spec->automute_speaker && spec->automute_lo)
3604 return 0;
3605 spec->automute_speaker = 1;
3606 spec->automute_lo = 1;
3607 break;
3608 default:
3609 return -EINVAL;
3610 }
3611 call_update_outputs(codec);
3612 return 1;
3613}
3614
3615static const struct snd_kcontrol_new automute_mode_enum = {
3616 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3617 .name = "Auto-Mute Mode",
3618 .info = automute_mode_info,
3619 .get = automute_mode_get,
3620 .put = automute_mode_put,
3621};
3622
3623static int add_automute_mode_enum(struct hda_codec *codec)
3624{
3625 struct hda_gen_spec *spec = codec->spec;
3626
Takashi Iwai12c93df2012-12-19 14:38:33 +01003627 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01003628 return -ENOMEM;
3629 return 0;
3630}
3631
3632/*
3633 * Check the availability of HP/line-out auto-mute;
3634 * Set up appropriately if really supported
3635 */
3636static int check_auto_mute_availability(struct hda_codec *codec)
3637{
3638 struct hda_gen_spec *spec = codec->spec;
3639 struct auto_pin_cfg *cfg = &spec->autocfg;
3640 int present = 0;
3641 int i, err;
3642
Takashi Iwaif72706b2013-01-16 18:20:07 +01003643 if (spec->suppress_auto_mute)
3644 return 0;
3645
Takashi Iwai352f7f92012-12-19 12:52:06 +01003646 if (cfg->hp_pins[0])
3647 present++;
3648 if (cfg->line_out_pins[0])
3649 present++;
3650 if (cfg->speaker_pins[0])
3651 present++;
3652 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02003653 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003654
3655 if (!cfg->speaker_pins[0] &&
3656 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
3657 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3658 sizeof(cfg->speaker_pins));
3659 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661
Takashi Iwai352f7f92012-12-19 12:52:06 +01003662 if (!cfg->hp_pins[0] &&
3663 cfg->line_out_type == AUTO_PIN_HP_OUT) {
3664 memcpy(cfg->hp_pins, cfg->line_out_pins,
3665 sizeof(cfg->hp_pins));
3666 cfg->hp_outs = cfg->line_outs;
3667 }
3668
3669 for (i = 0; i < cfg->hp_outs; i++) {
3670 hda_nid_t nid = cfg->hp_pins[i];
3671 if (!is_jack_detectable(codec, nid))
3672 continue;
3673 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
3674 nid);
3675 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003676 spec->hp_automute_hook ?
3677 spec->hp_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003678 snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003679 spec->detect_hp = 1;
3680 }
3681
3682 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
3683 if (cfg->speaker_outs)
3684 for (i = 0; i < cfg->line_outs; i++) {
3685 hda_nid_t nid = cfg->line_out_pins[i];
3686 if (!is_jack_detectable(codec, nid))
3687 continue;
3688 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
3689 snd_hda_jack_detect_enable_callback(codec, nid,
3690 HDA_GEN_FRONT_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003691 spec->line_automute_hook ?
3692 spec->line_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003693 snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003694 spec->detect_lo = 1;
3695 }
3696 spec->automute_lo_possible = spec->detect_hp;
3697 }
3698
3699 spec->automute_speaker_possible = cfg->speaker_outs &&
3700 (spec->detect_hp || spec->detect_lo);
3701
3702 spec->automute_lo = spec->automute_lo_possible;
3703 spec->automute_speaker = spec->automute_speaker_possible;
3704
3705 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
3706 /* create a control for automute mode */
3707 err = add_automute_mode_enum(codec);
3708 if (err < 0)
3709 return err;
3710 }
3711 return 0;
3712}
3713
Takashi Iwai352f7f92012-12-19 12:52:06 +01003714/* check whether all auto-mic pins are valid; setup indices if OK */
3715static bool auto_mic_check_imux(struct hda_codec *codec)
3716{
3717 struct hda_gen_spec *spec = codec->spec;
3718 const struct hda_input_mux *imux;
3719 int i;
3720
3721 imux = &spec->input_mux;
3722 for (i = 0; i < spec->am_num_entries; i++) {
3723 spec->am_entry[i].idx =
3724 find_idx_in_nid_list(spec->am_entry[i].pin,
3725 spec->imux_pins, imux->num_items);
3726 if (spec->am_entry[i].idx < 0)
3727 return false; /* no corresponding imux */
3728 }
3729
3730 /* we don't need the jack detection for the first pin */
3731 for (i = 1; i < spec->am_num_entries; i++)
3732 snd_hda_jack_detect_enable_callback(codec,
3733 spec->am_entry[i].pin,
3734 HDA_GEN_MIC_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003735 spec->mic_autoswitch_hook ?
3736 spec->mic_autoswitch_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003737 snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003738 return true;
3739}
3740
3741static int compare_attr(const void *ap, const void *bp)
3742{
3743 const struct automic_entry *a = ap;
3744 const struct automic_entry *b = bp;
3745 return (int)(a->attr - b->attr);
3746}
3747
3748/*
3749 * Check the availability of auto-mic switch;
3750 * Set up if really supported
3751 */
3752static int check_auto_mic_availability(struct hda_codec *codec)
3753{
3754 struct hda_gen_spec *spec = codec->spec;
3755 struct auto_pin_cfg *cfg = &spec->autocfg;
3756 unsigned int types;
3757 int i, num_pins;
3758
Takashi Iwaid12daf62013-01-07 16:32:11 +01003759 if (spec->suppress_auto_mic)
3760 return 0;
3761
Takashi Iwai352f7f92012-12-19 12:52:06 +01003762 types = 0;
3763 num_pins = 0;
3764 for (i = 0; i < cfg->num_inputs; i++) {
3765 hda_nid_t nid = cfg->inputs[i].pin;
3766 unsigned int attr;
3767 attr = snd_hda_codec_get_pincfg(codec, nid);
3768 attr = snd_hda_get_input_pin_attr(attr);
3769 if (types & (1 << attr))
3770 return 0; /* already occupied */
3771 switch (attr) {
3772 case INPUT_PIN_ATTR_INT:
3773 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3774 return 0; /* invalid type */
3775 break;
3776 case INPUT_PIN_ATTR_UNUSED:
3777 return 0; /* invalid entry */
3778 default:
3779 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
3780 return 0; /* invalid type */
3781 if (!spec->line_in_auto_switch &&
3782 cfg->inputs[i].type != AUTO_PIN_MIC)
3783 return 0; /* only mic is allowed */
3784 if (!is_jack_detectable(codec, nid))
3785 return 0; /* no unsol support */
3786 break;
3787 }
3788 if (num_pins >= MAX_AUTO_MIC_PINS)
3789 return 0;
3790 types |= (1 << attr);
3791 spec->am_entry[num_pins].pin = nid;
3792 spec->am_entry[num_pins].attr = attr;
3793 num_pins++;
3794 }
3795
3796 if (num_pins < 2)
3797 return 0;
3798
3799 spec->am_num_entries = num_pins;
3800 /* sort the am_entry in the order of attr so that the pin with a
3801 * higher attr will be selected when the jack is plugged.
3802 */
3803 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
3804 compare_attr, NULL);
3805
3806 if (!auto_mic_check_imux(codec))
3807 return 0;
3808
3809 spec->auto_mic = 1;
3810 spec->num_adc_nids = 1;
3811 spec->cur_mux[0] = spec->am_entry[0].idx;
3812 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
3813 spec->am_entry[0].pin,
3814 spec->am_entry[1].pin,
3815 spec->am_entry[2].pin);
3816
3817 return 0;
3818}
3819
3820
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003821/*
3822 * Parse the given BIOS configuration and set up the hda_gen_spec
3823 *
3824 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003825 * or a negative error code
3826 */
3827int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003828 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003829{
3830 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003831 int err;
3832
Takashi Iwai1c70a582013-01-11 17:48:22 +01003833 parse_user_hints(codec);
3834
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003835 if (cfg != &spec->autocfg) {
3836 spec->autocfg = *cfg;
3837 cfg = &spec->autocfg;
3838 }
3839
David Henningsson6fc4cb92013-01-16 15:58:45 +01003840 fill_all_dac_nids(codec);
3841
Takashi Iwai352f7f92012-12-19 12:52:06 +01003842 if (!cfg->line_outs) {
3843 if (cfg->dig_outs || cfg->dig_in_pin) {
3844 spec->multiout.max_channels = 2;
3845 spec->no_analog = 1;
3846 goto dig_only;
3847 }
3848 return 0; /* can't find valid BIOS pin config */
3849 }
3850
3851 if (!spec->no_primary_hp &&
3852 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
3853 cfg->line_outs <= cfg->hp_outs) {
3854 /* use HP as primary out */
3855 cfg->speaker_outs = cfg->line_outs;
3856 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3857 sizeof(cfg->speaker_pins));
3858 cfg->line_outs = cfg->hp_outs;
3859 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
3860 cfg->hp_outs = 0;
3861 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3862 cfg->line_out_type = AUTO_PIN_HP_OUT;
3863 }
3864
3865 err = parse_output_paths(codec);
3866 if (err < 0)
3867 return err;
3868 err = create_multi_channel_mode(codec);
3869 if (err < 0)
3870 return err;
3871 err = create_multi_out_ctls(codec, cfg);
3872 if (err < 0)
3873 return err;
3874 err = create_hp_out_ctls(codec);
3875 if (err < 0)
3876 return err;
3877 err = create_speaker_out_ctls(codec);
3878 if (err < 0)
3879 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003880 err = create_indep_hp_ctls(codec);
3881 if (err < 0)
3882 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01003883 err = create_loopback_mixing_ctl(codec);
3884 if (err < 0)
3885 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003886 err = create_shared_input(codec);
3887 if (err < 0)
3888 return err;
3889 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003890 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02003891 return err;
3892
Takashi Iwaia07a9492013-01-07 16:44:06 +01003893 spec->const_channel_count = spec->ext_channel_count;
3894 /* check the multiple speaker and headphone pins */
3895 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
3896 spec->const_channel_count = max(spec->const_channel_count,
3897 cfg->speaker_outs * 2);
3898 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
3899 spec->const_channel_count = max(spec->const_channel_count,
3900 cfg->hp_outs * 2);
3901 spec->multiout.max_channels = max(spec->ext_channel_count,
3902 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003903
3904 err = check_auto_mute_availability(codec);
3905 if (err < 0)
3906 return err;
3907
3908 err = check_dyn_adc_switch(codec);
3909 if (err < 0)
3910 return err;
3911
3912 if (!spec->shared_mic_hp) {
3913 err = check_auto_mic_availability(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003914 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 }
Takashi Iwai071c73a2006-08-23 18:34:06 +02003917
Takashi Iwai352f7f92012-12-19 12:52:06 +01003918 err = create_capture_mixers(codec);
3919 if (err < 0)
3920 return err;
3921
3922 err = parse_mic_boost(codec);
3923 if (err < 0)
3924 return err;
3925
Takashi Iwai978e77e2013-01-10 16:57:58 +01003926 if (spec->add_out_jack_modes) {
3927 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
3928 err = create_out_jack_modes(codec, cfg->line_outs,
3929 cfg->line_out_pins);
3930 if (err < 0)
3931 return err;
3932 }
3933 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
3934 err = create_out_jack_modes(codec, cfg->hp_outs,
3935 cfg->hp_pins);
3936 if (err < 0)
3937 return err;
3938 }
3939 }
3940
Takashi Iwai352f7f92012-12-19 12:52:06 +01003941 dig_only:
3942 parse_digital(codec);
3943
3944 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003946EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947
3948
3949/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003950 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003952
3953/* slave controls for virtual master */
3954static const char * const slave_pfxs[] = {
3955 "Front", "Surround", "Center", "LFE", "Side",
3956 "Headphone", "Speaker", "Mono", "Line Out",
3957 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01003958 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
3959 "Headphone Front", "Headphone Surround", "Headphone CLFE",
3960 "Headphone Side",
Takashi Iwai352f7f92012-12-19 12:52:06 +01003961 NULL,
3962};
3963
3964int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003966 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968
Takashi Iwai36502d02012-12-19 15:15:10 +01003969 if (spec->kctls.used) {
3970 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
3971 if (err < 0)
3972 return err;
3973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974
Takashi Iwai352f7f92012-12-19 12:52:06 +01003975 if (spec->multiout.dig_out_nid) {
3976 err = snd_hda_create_dig_out_ctls(codec,
3977 spec->multiout.dig_out_nid,
3978 spec->multiout.dig_out_nid,
3979 spec->pcm_rec[1].pcm_type);
3980 if (err < 0)
3981 return err;
3982 if (!spec->no_analog) {
3983 err = snd_hda_create_spdif_share_sw(codec,
3984 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 if (err < 0)
3986 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003987 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 }
3989 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003990 if (spec->dig_in_nid) {
3991 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
3992 if (err < 0)
3993 return err;
3994 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995
Takashi Iwai352f7f92012-12-19 12:52:06 +01003996 /* if we have no master control, let's create it */
3997 if (!spec->no_analog &&
3998 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003999 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01004000 spec->vmaster_tlv, slave_pfxs,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004001 "Playback Volume");
4002 if (err < 0)
4003 return err;
4004 }
4005 if (!spec->no_analog &&
4006 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
4007 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
4008 NULL, slave_pfxs,
4009 "Playback Switch",
4010 true, &spec->vmaster_mute.sw_kctl);
4011 if (err < 0)
4012 return err;
4013 if (spec->vmaster_mute.hook)
Takashi Iwaifd25a972012-12-20 14:57:18 +01004014 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
4015 spec->vmaster_mute_enum);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017
Takashi Iwai352f7f92012-12-19 12:52:06 +01004018 free_kctls(spec); /* no longer needed */
4019
4020 if (spec->shared_mic_hp) {
4021 int err;
4022 int nid = spec->autocfg.inputs[1].pin;
4023 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
4024 if (err < 0)
4025 return err;
4026 err = snd_hda_jack_detect_enable(codec, nid, 0);
4027 if (err < 0)
4028 return err;
4029 }
4030
4031 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
4032 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033 return err;
4034
4035 return 0;
4036}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004037EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
4038
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039
4040/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004041 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004044static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
4045 struct hda_codec *codec,
4046 struct snd_pcm_substream *substream,
4047 int action)
4048{
4049 struct hda_gen_spec *spec = codec->spec;
4050 if (spec->pcm_playback_hook)
4051 spec->pcm_playback_hook(hinfo, codec, substream, action);
4052}
4053
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004054static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
4055 struct hda_codec *codec,
4056 struct snd_pcm_substream *substream,
4057 int action)
4058{
4059 struct hda_gen_spec *spec = codec->spec;
4060 if (spec->pcm_capture_hook)
4061 spec->pcm_capture_hook(hinfo, codec, substream, action);
4062}
4063
Takashi Iwai352f7f92012-12-19 12:52:06 +01004064/*
4065 * Analog playback callbacks
4066 */
4067static int playback_pcm_open(struct hda_pcm_stream *hinfo,
4068 struct hda_codec *codec,
4069 struct snd_pcm_substream *substream)
4070{
4071 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004072 int err;
4073
4074 mutex_lock(&spec->pcm_mutex);
4075 err = snd_hda_multi_out_analog_open(codec,
4076 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004077 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004078 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004079 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004080 call_pcm_playback_hook(hinfo, codec, substream,
4081 HDA_GEN_PCM_ACT_OPEN);
4082 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004083 mutex_unlock(&spec->pcm_mutex);
4084 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004085}
4086
4087static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01004088 struct hda_codec *codec,
4089 unsigned int stream_tag,
4090 unsigned int format,
4091 struct snd_pcm_substream *substream)
4092{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004093 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004094 int err;
4095
4096 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
4097 stream_tag, format, substream);
4098 if (!err)
4099 call_pcm_playback_hook(hinfo, codec, substream,
4100 HDA_GEN_PCM_ACT_PREPARE);
4101 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004102}
Takashi Iwai97ec5582006-03-21 11:29:07 +01004103
Takashi Iwai352f7f92012-12-19 12:52:06 +01004104static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4105 struct hda_codec *codec,
4106 struct snd_pcm_substream *substream)
4107{
4108 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004109 int err;
4110
4111 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
4112 if (!err)
4113 call_pcm_playback_hook(hinfo, codec, substream,
4114 HDA_GEN_PCM_ACT_CLEANUP);
4115 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004116}
4117
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004118static int playback_pcm_close(struct hda_pcm_stream *hinfo,
4119 struct hda_codec *codec,
4120 struct snd_pcm_substream *substream)
4121{
4122 struct hda_gen_spec *spec = codec->spec;
4123 mutex_lock(&spec->pcm_mutex);
4124 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004125 call_pcm_playback_hook(hinfo, codec, substream,
4126 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004127 mutex_unlock(&spec->pcm_mutex);
4128 return 0;
4129}
4130
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004131static int capture_pcm_open(struct hda_pcm_stream *hinfo,
4132 struct hda_codec *codec,
4133 struct snd_pcm_substream *substream)
4134{
4135 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
4136 return 0;
4137}
4138
4139static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4140 struct hda_codec *codec,
4141 unsigned int stream_tag,
4142 unsigned int format,
4143 struct snd_pcm_substream *substream)
4144{
4145 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4146 call_pcm_capture_hook(hinfo, codec, substream,
4147 HDA_GEN_PCM_ACT_PREPARE);
4148 return 0;
4149}
4150
4151static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4152 struct hda_codec *codec,
4153 struct snd_pcm_substream *substream)
4154{
4155 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4156 call_pcm_capture_hook(hinfo, codec, substream,
4157 HDA_GEN_PCM_ACT_CLEANUP);
4158 return 0;
4159}
4160
4161static int capture_pcm_close(struct hda_pcm_stream *hinfo,
4162 struct hda_codec *codec,
4163 struct snd_pcm_substream *substream)
4164{
4165 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
4166 return 0;
4167}
4168
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004169static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
4170 struct hda_codec *codec,
4171 struct snd_pcm_substream *substream)
4172{
4173 struct hda_gen_spec *spec = codec->spec;
4174 int err = 0;
4175
4176 mutex_lock(&spec->pcm_mutex);
4177 if (!spec->indep_hp_enabled)
4178 err = -EBUSY;
4179 else
4180 spec->active_streams |= 1 << STREAM_INDEP_HP;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004181 call_pcm_playback_hook(hinfo, codec, substream,
4182 HDA_GEN_PCM_ACT_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004183 mutex_unlock(&spec->pcm_mutex);
4184 return err;
4185}
4186
4187static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
4188 struct hda_codec *codec,
4189 struct snd_pcm_substream *substream)
4190{
4191 struct hda_gen_spec *spec = codec->spec;
4192 mutex_lock(&spec->pcm_mutex);
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_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004196 mutex_unlock(&spec->pcm_mutex);
4197 return 0;
4198}
4199
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004200static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4201 struct hda_codec *codec,
4202 unsigned int stream_tag,
4203 unsigned int format,
4204 struct snd_pcm_substream *substream)
4205{
4206 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4207 call_pcm_playback_hook(hinfo, codec, substream,
4208 HDA_GEN_PCM_ACT_PREPARE);
4209 return 0;
4210}
4211
4212static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4213 struct hda_codec *codec,
4214 struct snd_pcm_substream *substream)
4215{
4216 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4217 call_pcm_playback_hook(hinfo, codec, substream,
4218 HDA_GEN_PCM_ACT_CLEANUP);
4219 return 0;
4220}
4221
Takashi Iwai352f7f92012-12-19 12:52:06 +01004222/*
4223 * Digital out
4224 */
4225static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
4226 struct hda_codec *codec,
4227 struct snd_pcm_substream *substream)
4228{
4229 struct hda_gen_spec *spec = codec->spec;
4230 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
4231}
4232
4233static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4234 struct hda_codec *codec,
4235 unsigned int stream_tag,
4236 unsigned int format,
4237 struct snd_pcm_substream *substream)
4238{
4239 struct hda_gen_spec *spec = codec->spec;
4240 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
4241 stream_tag, format, substream);
4242}
4243
4244static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4245 struct hda_codec *codec,
4246 struct snd_pcm_substream *substream)
4247{
4248 struct hda_gen_spec *spec = codec->spec;
4249 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
4250}
4251
4252static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
4253 struct hda_codec *codec,
4254 struct snd_pcm_substream *substream)
4255{
4256 struct hda_gen_spec *spec = codec->spec;
4257 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
4258}
4259
4260/*
4261 * Analog capture
4262 */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004263#define alt_capture_pcm_open capture_pcm_open
4264#define alt_capture_pcm_close capture_pcm_close
4265
Takashi Iwai352f7f92012-12-19 12:52:06 +01004266static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4267 struct hda_codec *codec,
4268 unsigned int stream_tag,
4269 unsigned int format,
4270 struct snd_pcm_substream *substream)
4271{
4272 struct hda_gen_spec *spec = codec->spec;
4273
4274 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01004275 stream_tag, 0, format);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004276 call_pcm_capture_hook(hinfo, codec, substream,
4277 HDA_GEN_PCM_ACT_PREPARE);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004278 return 0;
4279}
4280
Takashi Iwai352f7f92012-12-19 12:52:06 +01004281static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4282 struct hda_codec *codec,
4283 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01004284{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004285 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01004286
Takashi Iwai352f7f92012-12-19 12:52:06 +01004287 snd_hda_codec_cleanup_stream(codec,
4288 spec->adc_nids[substream->number + 1]);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004289 call_pcm_capture_hook(hinfo, codec, substream,
4290 HDA_GEN_PCM_ACT_CLEANUP);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004291 return 0;
4292}
4293
Takashi Iwai352f7f92012-12-19 12:52:06 +01004294/*
4295 */
4296static const struct hda_pcm_stream pcm_analog_playback = {
4297 .substreams = 1,
4298 .channels_min = 2,
4299 .channels_max = 8,
4300 /* NID is set in build_pcms */
4301 .ops = {
4302 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004303 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004304 .prepare = playback_pcm_prepare,
4305 .cleanup = playback_pcm_cleanup
4306 },
4307};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308
Takashi Iwai352f7f92012-12-19 12:52:06 +01004309static const struct hda_pcm_stream pcm_analog_capture = {
4310 .substreams = 1,
4311 .channels_min = 2,
4312 .channels_max = 2,
4313 /* NID is set in build_pcms */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004314 .ops = {
4315 .open = capture_pcm_open,
4316 .close = capture_pcm_close,
4317 .prepare = capture_pcm_prepare,
4318 .cleanup = capture_pcm_cleanup
4319 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004320};
4321
4322static const struct hda_pcm_stream pcm_analog_alt_playback = {
4323 .substreams = 1,
4324 .channels_min = 2,
4325 .channels_max = 2,
4326 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004327 .ops = {
4328 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004329 .close = alt_playback_pcm_close,
4330 .prepare = alt_playback_pcm_prepare,
4331 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004332 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004333};
4334
4335static const struct hda_pcm_stream pcm_analog_alt_capture = {
4336 .substreams = 2, /* can be overridden */
4337 .channels_min = 2,
4338 .channels_max = 2,
4339 /* NID is set in build_pcms */
4340 .ops = {
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004341 .open = alt_capture_pcm_open,
4342 .close = alt_capture_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004343 .prepare = alt_capture_pcm_prepare,
4344 .cleanup = alt_capture_pcm_cleanup
4345 },
4346};
4347
4348static const struct hda_pcm_stream pcm_digital_playback = {
4349 .substreams = 1,
4350 .channels_min = 2,
4351 .channels_max = 2,
4352 /* NID is set in build_pcms */
4353 .ops = {
4354 .open = dig_playback_pcm_open,
4355 .close = dig_playback_pcm_close,
4356 .prepare = dig_playback_pcm_prepare,
4357 .cleanup = dig_playback_pcm_cleanup
4358 },
4359};
4360
4361static const struct hda_pcm_stream pcm_digital_capture = {
4362 .substreams = 1,
4363 .channels_min = 2,
4364 .channels_max = 2,
4365 /* NID is set in build_pcms */
4366};
4367
4368/* Used by build_pcms to flag that a PCM has no playback stream */
4369static const struct hda_pcm_stream pcm_null_stream = {
4370 .substreams = 0,
4371 .channels_min = 0,
4372 .channels_max = 0,
4373};
4374
4375/*
4376 * dynamic changing ADC PCM streams
4377 */
4378static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
4379{
4380 struct hda_gen_spec *spec = codec->spec;
4381 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
4382
4383 if (spec->cur_adc && spec->cur_adc != new_adc) {
4384 /* stream is running, let's swap the current ADC */
4385 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
4386 spec->cur_adc = new_adc;
4387 snd_hda_codec_setup_stream(codec, new_adc,
4388 spec->cur_adc_stream_tag, 0,
4389 spec->cur_adc_format);
4390 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004392 return false;
4393}
4394
4395/* analog capture with dynamic dual-adc changes */
4396static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4397 struct hda_codec *codec,
4398 unsigned int stream_tag,
4399 unsigned int format,
4400 struct snd_pcm_substream *substream)
4401{
4402 struct hda_gen_spec *spec = codec->spec;
4403 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
4404 spec->cur_adc_stream_tag = stream_tag;
4405 spec->cur_adc_format = format;
4406 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
4407 return 0;
4408}
4409
4410static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4411 struct hda_codec *codec,
4412 struct snd_pcm_substream *substream)
4413{
4414 struct hda_gen_spec *spec = codec->spec;
4415 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
4416 spec->cur_adc = 0;
4417 return 0;
4418}
4419
4420static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
4421 .substreams = 1,
4422 .channels_min = 2,
4423 .channels_max = 2,
4424 .nid = 0, /* fill later */
4425 .ops = {
4426 .prepare = dyn_adc_capture_pcm_prepare,
4427 .cleanup = dyn_adc_capture_pcm_cleanup
4428 },
4429};
4430
Takashi Iwaif873e532012-12-20 16:58:39 +01004431static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
4432 const char *chip_name)
4433{
4434 char *p;
4435
4436 if (*str)
4437 return;
4438 strlcpy(str, chip_name, len);
4439
4440 /* drop non-alnum chars after a space */
4441 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
4442 if (!isalnum(p[1])) {
4443 *p = 0;
4444 break;
4445 }
4446 }
4447 strlcat(str, sfx, len);
4448}
4449
Takashi Iwai352f7f92012-12-19 12:52:06 +01004450/* build PCM streams based on the parsed results */
4451int snd_hda_gen_build_pcms(struct hda_codec *codec)
4452{
4453 struct hda_gen_spec *spec = codec->spec;
4454 struct hda_pcm *info = spec->pcm_rec;
4455 const struct hda_pcm_stream *p;
4456 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457
4458 codec->num_pcms = 1;
4459 codec->pcm_info = info;
4460
Takashi Iwai352f7f92012-12-19 12:52:06 +01004461 if (spec->no_analog)
4462 goto skip_analog;
4463
Takashi Iwaif873e532012-12-20 16:58:39 +01004464 fill_pcm_stream_name(spec->stream_name_analog,
4465 sizeof(spec->stream_name_analog),
4466 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004467 info->name = spec->stream_name_analog;
4468
4469 if (spec->multiout.num_dacs > 0) {
4470 p = spec->stream_analog_playback;
4471 if (!p)
4472 p = &pcm_analog_playback;
4473 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4474 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
4475 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
4476 spec->multiout.max_channels;
4477 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
4478 spec->autocfg.line_outs == 2)
4479 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
4480 snd_pcm_2_1_chmaps;
4481 }
4482 if (spec->num_adc_nids) {
4483 p = spec->stream_analog_capture;
4484 if (!p) {
4485 if (spec->dyn_adc_switch)
4486 p = &dyn_adc_pcm_analog_capture;
4487 else
4488 p = &pcm_analog_capture;
4489 }
4490 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4491 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
4492 }
4493
Takashi Iwai352f7f92012-12-19 12:52:06 +01004494 skip_analog:
4495 /* SPDIF for stream index #1 */
4496 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01004497 fill_pcm_stream_name(spec->stream_name_digital,
4498 sizeof(spec->stream_name_digital),
4499 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004500 codec->num_pcms = 2;
4501 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
4502 info = spec->pcm_rec + 1;
4503 info->name = spec->stream_name_digital;
4504 if (spec->dig_out_type)
4505 info->pcm_type = spec->dig_out_type;
4506 else
4507 info->pcm_type = HDA_PCM_TYPE_SPDIF;
4508 if (spec->multiout.dig_out_nid) {
4509 p = spec->stream_digital_playback;
4510 if (!p)
4511 p = &pcm_digital_playback;
4512 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4513 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
4514 }
4515 if (spec->dig_in_nid) {
4516 p = spec->stream_digital_capture;
4517 if (!p)
4518 p = &pcm_digital_capture;
4519 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4520 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
4521 }
4522 }
4523
4524 if (spec->no_analog)
4525 return 0;
4526
4527 /* If the use of more than one ADC is requested for the current
4528 * model, configure a second analog capture-only PCM.
4529 */
4530 have_multi_adcs = (spec->num_adc_nids > 1) &&
4531 !spec->dyn_adc_switch && !spec->auto_mic;
4532 /* Additional Analaog capture for index #2 */
4533 if (spec->alt_dac_nid || have_multi_adcs) {
Takashi Iwaia6071482013-01-21 16:50:09 +01004534 fill_pcm_stream_name(spec->stream_name_alt_analog,
4535 sizeof(spec->stream_name_alt_analog),
4536 " Alt Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004537 codec->num_pcms = 3;
4538 info = spec->pcm_rec + 2;
Takashi Iwaia6071482013-01-21 16:50:09 +01004539 info->name = spec->stream_name_alt_analog;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004540 if (spec->alt_dac_nid) {
4541 p = spec->stream_analog_alt_playback;
4542 if (!p)
4543 p = &pcm_analog_alt_playback;
4544 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4545 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
4546 spec->alt_dac_nid;
4547 } else {
4548 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
4549 pcm_null_stream;
4550 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
4551 }
4552 if (have_multi_adcs) {
4553 p = spec->stream_analog_alt_capture;
4554 if (!p)
4555 p = &pcm_analog_alt_capture;
4556 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4557 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
4558 spec->adc_nids[1];
4559 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
4560 spec->num_adc_nids - 1;
4561 } else {
4562 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
4563 pcm_null_stream;
4564 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
4565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 }
4567
4568 return 0;
4569}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004570EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
4571
4572
4573/*
4574 * Standard auto-parser initializations
4575 */
4576
Takashi Iwaid4156932013-01-07 10:08:02 +01004577/* configure the given path as a proper output */
Takashi Iwai2c12c302013-01-10 09:33:29 +01004578static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004579{
4580 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01004581 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004582
Takashi Iwai196c17662013-01-04 15:01:40 +01004583 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01004584 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004585 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01004586 pin = path->path[path->depth - 1];
Takashi Iwai2c12c302013-01-10 09:33:29 +01004587 restore_pin_ctl(codec, pin);
Takashi Iwaie1284af2013-01-03 16:33:02 +01004588 snd_hda_activate_path(codec, path, path->active, true);
4589 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004590}
4591
4592/* initialize primary output paths */
4593static void init_multi_out(struct hda_codec *codec)
4594{
4595 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004596 int i;
4597
Takashi Iwaid4156932013-01-07 10:08:02 +01004598 for (i = 0; i < spec->autocfg.line_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004599 set_output_and_unmute(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004600}
4601
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004602
Takashi Iwai2c12c302013-01-10 09:33:29 +01004603static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004604{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004605 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004606
Takashi Iwaid4156932013-01-07 10:08:02 +01004607 for (i = 0; i < num_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004608 set_output_and_unmute(codec, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004609}
4610
4611/* initialize hp and speaker paths */
4612static void init_extra_out(struct hda_codec *codec)
4613{
4614 struct hda_gen_spec *spec = codec->spec;
4615
4616 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004617 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004618 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
4619 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai2c12c302013-01-10 09:33:29 +01004620 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004621}
4622
4623/* initialize multi-io paths */
4624static void init_multi_io(struct hda_codec *codec)
4625{
4626 struct hda_gen_spec *spec = codec->spec;
4627 int i;
4628
4629 for (i = 0; i < spec->multi_ios; i++) {
4630 hda_nid_t pin = spec->multi_io[i].pin;
4631 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01004632 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004633 if (!path)
4634 continue;
4635 if (!spec->multi_io[i].ctl_in)
4636 spec->multi_io[i].ctl_in =
Takashi Iwai2c12c302013-01-10 09:33:29 +01004637 snd_hda_codec_get_pin_target(codec, pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004638 snd_hda_activate_path(codec, path, path->active, true);
4639 }
4640}
4641
Takashi Iwai352f7f92012-12-19 12:52:06 +01004642/* set up input pins and loopback paths */
4643static void init_analog_input(struct hda_codec *codec)
4644{
4645 struct hda_gen_spec *spec = codec->spec;
4646 struct auto_pin_cfg *cfg = &spec->autocfg;
4647 int i;
4648
4649 for (i = 0; i < cfg->num_inputs; i++) {
4650 hda_nid_t nid = cfg->inputs[i].pin;
4651 if (is_input_pin(codec, nid))
Takashi Iwai2c12c302013-01-10 09:33:29 +01004652 restore_pin_ctl(codec, nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004653
4654 /* init loopback inputs */
4655 if (spec->mixer_nid) {
4656 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01004657 path = snd_hda_get_path_from_idx(codec, spec->loopback_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004658 if (path)
4659 snd_hda_activate_path(codec, path,
4660 path->active, false);
4661 }
4662 }
4663}
4664
4665/* initialize ADC paths */
4666static void init_input_src(struct hda_codec *codec)
4667{
4668 struct hda_gen_spec *spec = codec->spec;
4669 struct hda_input_mux *imux = &spec->input_mux;
4670 struct nid_path *path;
4671 int i, c, nums;
4672
4673 if (spec->dyn_adc_switch)
4674 nums = 1;
4675 else
4676 nums = spec->num_adc_nids;
4677
4678 for (c = 0; c < nums; c++) {
4679 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01004680 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004681 if (path) {
4682 bool active = path->active;
4683 if (i == spec->cur_mux[c])
4684 active = true;
4685 snd_hda_activate_path(codec, path, active, false);
4686 }
4687 }
4688 }
4689
4690 if (spec->shared_mic_hp)
4691 update_shared_mic_hp(codec, spec->cur_mux[0]);
4692
4693 if (spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01004694 spec->cap_sync_hook(codec, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004695}
4696
4697/* set right pin controls for digital I/O */
4698static void init_digital(struct hda_codec *codec)
4699{
4700 struct hda_gen_spec *spec = codec->spec;
4701 int i;
4702 hda_nid_t pin;
4703
Takashi Iwaid4156932013-01-07 10:08:02 +01004704 for (i = 0; i < spec->autocfg.dig_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004705 set_output_and_unmute(codec, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004706 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01004707 if (pin) {
4708 struct nid_path *path;
Takashi Iwai2c12c302013-01-10 09:33:29 +01004709 restore_pin_ctl(codec, pin);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01004710 path = snd_hda_get_path_from_idx(codec, spec->digin_path);
4711 if (path)
4712 snd_hda_activate_path(codec, path, path->active, false);
4713 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004714}
4715
Takashi Iwai973e4972012-12-20 15:16:09 +01004716/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
4717 * invalid unsol tags by some reason
4718 */
4719static void clear_unsol_on_unused_pins(struct hda_codec *codec)
4720{
4721 int i;
4722
4723 for (i = 0; i < codec->init_pins.used; i++) {
4724 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
4725 hda_nid_t nid = pin->nid;
4726 if (is_jack_detectable(codec, nid) &&
4727 !snd_hda_jack_tbl_get(codec, nid))
4728 snd_hda_codec_update_cache(codec, nid, 0,
4729 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
4730 }
4731}
4732
Takashi Iwai5187ac12013-01-07 12:52:16 +01004733/*
4734 * initialize the generic spec;
4735 * this can be put as patch_ops.init function
4736 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004737int snd_hda_gen_init(struct hda_codec *codec)
4738{
4739 struct hda_gen_spec *spec = codec->spec;
4740
4741 if (spec->init_hook)
4742 spec->init_hook(codec);
4743
4744 snd_hda_apply_verbs(codec);
4745
Takashi Iwai3bbcd272012-12-20 11:50:58 +01004746 codec->cached_write = 1;
4747
Takashi Iwai352f7f92012-12-19 12:52:06 +01004748 init_multi_out(codec);
4749 init_extra_out(codec);
4750 init_multi_io(codec);
4751 init_analog_input(codec);
4752 init_input_src(codec);
4753 init_digital(codec);
4754
Takashi Iwai973e4972012-12-20 15:16:09 +01004755 clear_unsol_on_unused_pins(codec);
4756
Takashi Iwai352f7f92012-12-19 12:52:06 +01004757 /* call init functions of standard auto-mute helpers */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01004758 update_automute_all(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004759
Takashi Iwai3bbcd272012-12-20 11:50:58 +01004760 snd_hda_codec_flush_amp_cache(codec);
4761 snd_hda_codec_flush_cmd_cache(codec);
4762
Takashi Iwai352f7f92012-12-19 12:52:06 +01004763 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
4764 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
4765
4766 hda_call_check_power_status(codec, 0x01);
4767 return 0;
4768}
Takashi Iwaifce52a32013-01-07 12:42:48 +01004769EXPORT_SYMBOL_HDA(snd_hda_gen_init);
4770
Takashi Iwai5187ac12013-01-07 12:52:16 +01004771/*
4772 * free the generic spec;
4773 * this can be put as patch_ops.free function
4774 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01004775void snd_hda_gen_free(struct hda_codec *codec)
4776{
4777 snd_hda_gen_spec_free(codec->spec);
4778 kfree(codec->spec);
4779 codec->spec = NULL;
4780}
4781EXPORT_SYMBOL_HDA(snd_hda_gen_free);
4782
4783#ifdef CONFIG_PM
Takashi Iwai5187ac12013-01-07 12:52:16 +01004784/*
4785 * check the loopback power save state;
4786 * this can be put as patch_ops.check_power_status function
4787 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01004788int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
4789{
4790 struct hda_gen_spec *spec = codec->spec;
4791 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
4792}
4793EXPORT_SYMBOL_HDA(snd_hda_gen_check_power_status);
4794#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01004795
4796
4797/*
4798 * the generic codec support
4799 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800
Takashi Iwai352f7f92012-12-19 12:52:06 +01004801static const struct hda_codec_ops generic_patch_ops = {
4802 .build_controls = snd_hda_gen_build_controls,
4803 .build_pcms = snd_hda_gen_build_pcms,
4804 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01004805 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004806 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02004807#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01004808 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02004809#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810};
4811
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812int snd_hda_parse_generic_codec(struct hda_codec *codec)
4813{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004814 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815 int err;
4816
Takashi Iwaie560d8d2005-09-09 14:21:46 +02004817 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004818 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004820 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004823 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
4824 if (err < 0)
4825 return err;
4826
4827 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004828 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829 goto error;
4830
4831 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832 return 0;
4833
Takashi Iwai352f7f92012-12-19 12:52:06 +01004834error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01004835 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836 return err;
4837}
Takashi Iwaifce52a32013-01-07 12:42:48 +01004838EXPORT_SYMBOL_HDA(snd_hda_parse_generic_codec);