blob: b301952dc081077fda49a38d3addcda10f434483 [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}
Takashi Iwai4bd01e92013-01-22 15:17:20 +0100264EXPORT_SYMBOL_HDA(snd_hda_get_path_idx);
Takashi Iwai196c17662013-01-04 15:01:40 +0100265
266/* get the path instance corresponding to the given index number */
267struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
268{
269 struct hda_gen_spec *spec = codec->spec;
270
271 if (idx <= 0 || idx > spec->paths.used)
272 return NULL;
273 return snd_array_elem(&spec->paths, idx - 1);
274}
Takashi Iwai4bd01e92013-01-22 15:17:20 +0100275EXPORT_SYMBOL_HDA(snd_hda_get_path_from_idx);
Takashi Iwai196c17662013-01-04 15:01:40 +0100276
Takashi Iwai352f7f92012-12-19 12:52:06 +0100277/* check whether the given DAC is already found in any existing paths */
278static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
279{
280 struct hda_gen_spec *spec = codec->spec;
281 int i;
282
283 for (i = 0; i < spec->paths.used; i++) {
284 struct nid_path *path = snd_array_elem(&spec->paths, i);
285 if (path->path[0] == nid)
286 return true;
287 }
288 return false;
289}
290
291/* check whether the given two widgets can be connected */
292static bool is_reachable_path(struct hda_codec *codec,
293 hda_nid_t from_nid, hda_nid_t to_nid)
294{
295 if (!from_nid || !to_nid)
296 return false;
297 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
298}
299
300/* nid, dir and idx */
301#define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
302
303/* check whether the given ctl is already assigned in any path elements */
304static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
305{
306 struct hda_gen_spec *spec = codec->spec;
307 int i;
308
309 val &= AMP_VAL_COMPARE_MASK;
310 for (i = 0; i < spec->paths.used; i++) {
311 struct nid_path *path = snd_array_elem(&spec->paths, i);
312 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
313 return true;
314 }
315 return false;
316}
317
318/* check whether a control with the given (nid, dir, idx) was assigned */
319static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai8999bf02013-01-18 11:01:33 +0100320 int dir, int idx, int type)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100321{
322 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
Takashi Iwai8999bf02013-01-18 11:01:33 +0100323 return is_ctl_used(codec, val, type);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100324}
325
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100326static void print_nid_path(const char *pfx, struct nid_path *path)
327{
328 char buf[40];
329 int i;
330
331
332 buf[0] = 0;
333 for (i = 0; i < path->depth; i++) {
334 char tmp[4];
335 sprintf(tmp, ":%02x", path->path[i]);
336 strlcat(buf, tmp, sizeof(buf));
337 }
338 snd_printdd("%s path: depth=%d %s\n", pfx, path->depth, buf);
339}
340
Takashi Iwai352f7f92012-12-19 12:52:06 +0100341/* called recursively */
342static bool __parse_nid_path(struct hda_codec *codec,
343 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100344 int anchor_nid, struct nid_path *path,
345 int depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100346{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100347 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100348 int i, nums;
349
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100350 if (to_nid == anchor_nid)
351 anchor_nid = 0; /* anchor passed */
352 else if (to_nid == (hda_nid_t)(-anchor_nid))
353 return false; /* hit the exclusive nid */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100354
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100355 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100356 for (i = 0; i < nums; i++) {
357 if (conn[i] != from_nid) {
358 /* special case: when from_nid is 0,
359 * try to find an empty DAC
360 */
361 if (from_nid ||
362 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
363 is_dac_already_used(codec, conn[i]))
364 continue;
365 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100366 /* anchor is not requested or already passed? */
367 if (anchor_nid <= 0)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100368 goto found;
369 }
370 if (depth >= MAX_NID_PATH_DEPTH)
371 return false;
372 for (i = 0; i < nums; i++) {
373 unsigned int type;
374 type = get_wcaps_type(get_wcaps(codec, conn[i]));
375 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
376 type == AC_WID_PIN)
377 continue;
378 if (__parse_nid_path(codec, from_nid, conn[i],
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100379 anchor_nid, path, depth + 1))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100380 goto found;
381 }
382 return false;
383
384 found:
385 path->path[path->depth] = conn[i];
386 path->idx[path->depth + 1] = i;
387 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
388 path->multi[path->depth + 1] = 1;
389 path->depth++;
390 return true;
391}
392
393/* parse the widget path from the given nid to the target nid;
394 * when @from_nid is 0, try to find an empty DAC;
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100395 * when @anchor_nid is set to a positive value, only paths through the widget
396 * with the given value are evaluated.
397 * when @anchor_nid is set to a negative value, paths through the widget
398 * with the negative of given value are excluded, only other paths are chosen.
399 * when @anchor_nid is zero, no special handling about path selection.
Takashi Iwai352f7f92012-12-19 12:52:06 +0100400 */
401bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100402 hda_nid_t to_nid, int anchor_nid,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100403 struct nid_path *path)
404{
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100405 if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100406 path->path[path->depth] = to_nid;
407 path->depth++;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100408 return true;
409 }
410 return false;
411}
412EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100415 * parse the path between the given NIDs and add to the path list.
416 * if no valid path is found, return NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100418struct nid_path *
419snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100420 hda_nid_t to_nid, int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100422 struct hda_gen_spec *spec = codec->spec;
423 struct nid_path *path;
424
425 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
426 return NULL;
427
Takashi Iwaif5172a72013-01-04 13:19:55 +0100428 /* check whether the path has been already added */
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100429 path = get_nid_path(codec, from_nid, to_nid, anchor_nid);
Takashi Iwaif5172a72013-01-04 13:19:55 +0100430 if (path)
431 return path;
432
Takashi Iwai352f7f92012-12-19 12:52:06 +0100433 path = snd_array_new(&spec->paths);
434 if (!path)
435 return NULL;
436 memset(path, 0, sizeof(*path));
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100437 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100438 return path;
439 /* push back */
440 spec->paths.used--;
441 return NULL;
442}
443EXPORT_SYMBOL_HDA(snd_hda_add_new_path);
444
Takashi Iwai980428c2013-01-09 09:28:20 +0100445/* clear the given path as invalid so that it won't be picked up later */
446static void invalidate_nid_path(struct hda_codec *codec, int idx)
447{
448 struct nid_path *path = snd_hda_get_path_from_idx(codec, idx);
449 if (!path)
450 return;
451 memset(path, 0, sizeof(*path));
452}
453
Takashi Iwai352f7f92012-12-19 12:52:06 +0100454/* look for an empty DAC slot */
455static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
456 bool is_digital)
457{
458 struct hda_gen_spec *spec = codec->spec;
459 bool cap_digital;
460 int i;
461
462 for (i = 0; i < spec->num_all_dacs; i++) {
463 hda_nid_t nid = spec->all_dacs[i];
464 if (!nid || is_dac_already_used(codec, nid))
465 continue;
466 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
467 if (is_digital != cap_digital)
468 continue;
469 if (is_reachable_path(codec, nid, pin))
470 return nid;
471 }
472 return 0;
473}
474
475/* replace the channels in the composed amp value with the given number */
476static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
477{
478 val &= ~(0x3U << 16);
479 val |= chs << 16;
480 return val;
481}
482
483/* check whether the widget has the given amp capability for the direction */
484static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
485 int dir, unsigned int bits)
486{
487 if (!nid)
488 return false;
489 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
490 if (query_amp_caps(codec, nid, dir) & bits)
491 return true;
492 return false;
493}
494
David Henningsson99a55922013-01-16 15:58:44 +0100495static bool same_amp_caps(struct hda_codec *codec, hda_nid_t nid1,
496 hda_nid_t nid2, int dir)
497{
498 if (!(get_wcaps(codec, nid1) & (1 << (dir + 1))))
499 return !(get_wcaps(codec, nid2) & (1 << (dir + 1)));
500 return (query_amp_caps(codec, nid1, dir) ==
501 query_amp_caps(codec, nid2, dir));
502}
503
Takashi Iwai352f7f92012-12-19 12:52:06 +0100504#define nid_has_mute(codec, nid, dir) \
505 check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
506#define nid_has_volume(codec, nid, dir) \
507 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
508
509/* look for a widget suitable for assigning a mute switch in the path */
510static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
511 struct nid_path *path)
512{
513 int i;
514
515 for (i = path->depth - 1; i >= 0; i--) {
516 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
517 return path->path[i];
518 if (i != path->depth - 1 && i != 0 &&
519 nid_has_mute(codec, path->path[i], HDA_INPUT))
520 return path->path[i];
521 }
522 return 0;
523}
524
525/* look for a widget suitable for assigning a volume ctl in the path */
526static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
527 struct nid_path *path)
528{
529 int i;
530
531 for (i = path->depth - 1; i >= 0; i--) {
532 if (nid_has_volume(codec, path->path[i], HDA_OUTPUT))
533 return path->path[i];
534 }
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200535 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536}
537
538/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100539 * path activation / deactivation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100541
542/* can have the amp-in capability? */
543static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100545 hda_nid_t nid = path->path[idx];
546 unsigned int caps = get_wcaps(codec, nid);
547 unsigned int type = get_wcaps_type(caps);
548
549 if (!(caps & AC_WCAP_IN_AMP))
550 return false;
551 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
552 return false;
553 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554}
555
Takashi Iwai352f7f92012-12-19 12:52:06 +0100556/* can have the amp-out capability? */
557static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100559 hda_nid_t nid = path->path[idx];
560 unsigned int caps = get_wcaps(codec, nid);
561 unsigned int type = get_wcaps_type(caps);
562
563 if (!(caps & AC_WCAP_OUT_AMP))
564 return false;
565 if (type == AC_WID_PIN && !idx) /* only for output pins */
566 return false;
567 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568}
569
Takashi Iwai352f7f92012-12-19 12:52:06 +0100570/* check whether the given (nid,dir,idx) is active */
571static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
572 unsigned int idx, unsigned int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100574 struct hda_gen_spec *spec = codec->spec;
575 int i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Takashi Iwai352f7f92012-12-19 12:52:06 +0100577 for (n = 0; n < spec->paths.used; n++) {
578 struct nid_path *path = snd_array_elem(&spec->paths, n);
579 if (!path->active)
580 continue;
581 for (i = 0; i < path->depth; i++) {
582 if (path->path[i] == nid) {
583 if (dir == HDA_OUTPUT || path->idx[i] == idx)
584 return true;
585 break;
586 }
587 }
588 }
589 return false;
590}
591
592/* get the default amp value for the target state */
593static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai8999bf02013-01-18 11:01:33 +0100594 int dir, unsigned int caps, bool enable)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100595{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100596 unsigned int val = 0;
597
Takashi Iwai352f7f92012-12-19 12:52:06 +0100598 if (caps & AC_AMPCAP_NUM_STEPS) {
599 /* set to 0dB */
600 if (enable)
601 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
602 }
603 if (caps & AC_AMPCAP_MUTE) {
604 if (!enable)
605 val |= HDA_AMP_MUTE;
606 }
607 return val;
608}
609
610/* initialize the amp value (only at the first time) */
611static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
612{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100613 unsigned int caps = query_amp_caps(codec, nid, dir);
614 int val = get_amp_val_to_activate(codec, nid, dir, caps, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100615 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
616}
617
Takashi Iwai8999bf02013-01-18 11:01:33 +0100618/* calculate amp value mask we can modify;
619 * if the given amp is controlled by mixers, don't touch it
620 */
621static unsigned int get_amp_mask_to_modify(struct hda_codec *codec,
622 hda_nid_t nid, int dir, int idx,
623 unsigned int caps)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100624{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100625 unsigned int mask = 0xff;
626
627 if (caps & AC_AMPCAP_MUTE) {
628 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_MUTE_CTL))
629 mask &= ~0x80;
630 }
631 if (caps & AC_AMPCAP_NUM_STEPS) {
632 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
633 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
634 mask &= ~0x7f;
635 }
636 return mask;
637}
638
639static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
640 int idx, int idx_to_check, bool enable)
641{
642 unsigned int caps;
643 unsigned int mask, val;
644
645 if (!enable && is_active_nid(codec, nid, dir, idx))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100646 return;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100647
648 caps = query_amp_caps(codec, nid, dir);
649 val = get_amp_val_to_activate(codec, nid, dir, caps, enable);
650 mask = get_amp_mask_to_modify(codec, nid, dir, idx_to_check, caps);
651 if (!mask)
652 return;
653
654 val &= mask;
655 snd_hda_codec_amp_stereo(codec, nid, dir, idx, mask, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100656}
657
658static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
659 int i, bool enable)
660{
661 hda_nid_t nid = path->path[i];
662 init_amp(codec, nid, HDA_OUTPUT, 0);
Takashi Iwai8999bf02013-01-18 11:01:33 +0100663 activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100664}
665
666static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
667 int i, bool enable, bool add_aamix)
668{
669 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100670 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100671 int n, nums, idx;
672 int type;
673 hda_nid_t nid = path->path[i];
674
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100675 nums = snd_hda_get_conn_list(codec, nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100676 type = get_wcaps_type(get_wcaps(codec, nid));
677 if (type == AC_WID_PIN ||
678 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
679 nums = 1;
680 idx = 0;
681 } else
682 idx = path->idx[i];
683
684 for (n = 0; n < nums; n++)
685 init_amp(codec, nid, HDA_INPUT, n);
686
Takashi Iwai352f7f92012-12-19 12:52:06 +0100687 /* here is a little bit tricky in comparison with activate_amp_out();
688 * when aa-mixer is available, we need to enable the path as well
689 */
690 for (n = 0; n < nums; n++) {
691 if (n != idx && (!add_aamix || conn[n] != spec->mixer_nid))
692 continue;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100693 activate_amp(codec, nid, HDA_INPUT, n, idx, enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 }
695}
696
Takashi Iwai352f7f92012-12-19 12:52:06 +0100697/* activate or deactivate the given path
698 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100700void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
701 bool enable, bool add_aamix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100703 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Takashi Iwai352f7f92012-12-19 12:52:06 +0100705 if (!enable)
706 path->active = false;
707
708 for (i = path->depth - 1; i >= 0; i--) {
709 if (enable && path->multi[i])
710 snd_hda_codec_write_cache(codec, path->path[i], 0,
711 AC_VERB_SET_CONNECT_SEL,
712 path->idx[i]);
713 if (has_amp_in(codec, path, i))
714 activate_amp_in(codec, path, i, enable, add_aamix);
715 if (has_amp_out(codec, path, i))
716 activate_amp_out(codec, path, i, enable);
717 }
718
719 if (enable)
720 path->active = true;
721}
722EXPORT_SYMBOL_HDA(snd_hda_activate_path);
723
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100724/* turn on/off EAPD on the given pin */
725static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
726{
727 struct hda_gen_spec *spec = codec->spec;
728 if (spec->own_eapd_ctl ||
729 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
730 return;
Takashi Iwaiecac3ed2012-12-21 15:23:01 +0100731 if (codec->inv_eapd)
732 enable = !enable;
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100733 snd_hda_codec_update_cache(codec, pin, 0,
734 AC_VERB_SET_EAPD_BTLENABLE,
735 enable ? 0x02 : 0x00);
736}
737
Takashi Iwai352f7f92012-12-19 12:52:06 +0100738
739/*
740 * Helper functions for creating mixer ctl elements
741 */
742
743enum {
744 HDA_CTL_WIDGET_VOL,
745 HDA_CTL_WIDGET_MUTE,
746 HDA_CTL_BIND_MUTE,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100747};
748static const struct snd_kcontrol_new control_templates[] = {
749 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
750 HDA_CODEC_MUTE(NULL, 0, 0, 0),
751 HDA_BIND_MUTE(NULL, 0, 0, 0),
Takashi Iwai352f7f92012-12-19 12:52:06 +0100752};
753
754/* add dynamic controls from template */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100755static struct snd_kcontrol_new *
756add_control(struct hda_gen_spec *spec, int type, const char *name,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100757 int cidx, unsigned long val)
758{
759 struct snd_kcontrol_new *knew;
760
Takashi Iwai12c93df2012-12-19 14:38:33 +0100761 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100762 if (!knew)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100763 return NULL;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100764 knew->index = cidx;
765 if (get_amp_nid_(val))
766 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
767 knew->private_value = val;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100768 return knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100769}
770
771static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
772 const char *pfx, const char *dir,
773 const char *sfx, int cidx, unsigned long val)
774{
775 char name[32];
776 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100777 if (!add_control(spec, type, name, cidx, val))
778 return -ENOMEM;
779 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100780}
781
782#define add_pb_vol_ctrl(spec, type, pfx, val) \
783 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
784#define add_pb_sw_ctrl(spec, type, pfx, val) \
785 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
786#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
787 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
788#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
789 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
790
791static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
792 unsigned int chs, struct nid_path *path)
793{
794 unsigned int val;
795 if (!path)
796 return 0;
797 val = path->ctls[NID_PATH_VOL_CTL];
798 if (!val)
799 return 0;
800 val = amp_val_replace_channels(val, chs);
801 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
802}
803
804/* return the channel bits suitable for the given path->ctls[] */
805static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
806 int type)
807{
808 int chs = 1; /* mono (left only) */
809 if (path) {
810 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
811 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
812 chs = 3; /* stereo */
813 }
814 return chs;
815}
816
817static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
818 struct nid_path *path)
819{
820 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
821 return add_vol_ctl(codec, pfx, cidx, chs, path);
822}
823
824/* create a mute-switch for the given mixer widget;
825 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
826 */
827static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
828 unsigned int chs, struct nid_path *path)
829{
830 unsigned int val;
831 int type = HDA_CTL_WIDGET_MUTE;
832
833 if (!path)
834 return 0;
835 val = path->ctls[NID_PATH_MUTE_CTL];
836 if (!val)
837 return 0;
838 val = amp_val_replace_channels(val, chs);
839 if (get_amp_direction_(val) == HDA_INPUT) {
840 hda_nid_t nid = get_amp_nid_(val);
841 int nums = snd_hda_get_num_conns(codec, nid);
842 if (nums > 1) {
843 type = HDA_CTL_BIND_MUTE;
844 val |= nums << 19;
845 }
846 }
847 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
848}
849
850static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
851 int cidx, struct nid_path *path)
852{
853 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
854 return add_sw_ctl(codec, pfx, cidx, chs, path);
855}
856
Takashi Iwai247d85e2013-01-17 16:18:11 +0100857/* any ctl assigned to the path with the given index? */
858static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
859{
860 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
861 return path && path->ctls[ctl_type];
862}
863
Takashi Iwai352f7f92012-12-19 12:52:06 +0100864static const char * const channel_name[4] = {
865 "Front", "Surround", "CLFE", "Side"
866};
867
868/* give some appropriate ctl name prefix for the given line out channel */
Takashi Iwai247d85e2013-01-17 16:18:11 +0100869static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
870 int *index, int ctl_type)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100871{
Takashi Iwai247d85e2013-01-17 16:18:11 +0100872 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100873 struct auto_pin_cfg *cfg = &spec->autocfg;
874
875 *index = 0;
876 if (cfg->line_outs == 1 && !spec->multi_ios &&
Takashi Iwai247d85e2013-01-17 16:18:11 +0100877 !cfg->hp_outs && !cfg->speaker_outs)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100878 return spec->vmaster_mute.hook ? "PCM" : "Master";
879
880 /* if there is really a single DAC used in the whole output paths,
881 * use it master (or "PCM" if a vmaster hook is present)
882 */
883 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
884 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
885 return spec->vmaster_mute.hook ? "PCM" : "Master";
886
Takashi Iwai247d85e2013-01-17 16:18:11 +0100887 /* multi-io channels */
888 if (ch >= cfg->line_outs)
889 return channel_name[ch];
890
Takashi Iwai352f7f92012-12-19 12:52:06 +0100891 switch (cfg->line_out_type) {
892 case AUTO_PIN_SPEAKER_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +0100893 /* if the primary channel vol/mute is shared with HP volume,
894 * don't name it as Speaker
895 */
896 if (!ch && cfg->hp_outs &&
897 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
898 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100899 if (cfg->line_outs == 1)
900 return "Speaker";
901 if (cfg->line_outs == 2)
902 return ch ? "Bass Speaker" : "Speaker";
903 break;
904 case AUTO_PIN_HP_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +0100905 /* if the primary channel vol/mute is shared with spk volume,
906 * don't name it as Headphone
907 */
908 if (!ch && cfg->speaker_outs &&
909 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
910 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100911 /* for multi-io case, only the primary out */
912 if (ch && spec->multi_ios)
913 break;
914 *index = ch;
915 return "Headphone";
Takashi Iwai352f7f92012-12-19 12:52:06 +0100916 }
Takashi Iwai247d85e2013-01-17 16:18:11 +0100917
918 /* for a single channel output, we don't have to name the channel */
919 if (cfg->line_outs == 1 && !spec->multi_ios)
920 return "PCM";
921
Takashi Iwai352f7f92012-12-19 12:52:06 +0100922 if (ch >= ARRAY_SIZE(channel_name)) {
923 snd_BUG();
924 return "PCM";
925 }
926
927 return channel_name[ch];
928}
929
930/*
931 * Parse output paths
932 */
933
934/* badness definition */
935enum {
936 /* No primary DAC is found for the main output */
937 BAD_NO_PRIMARY_DAC = 0x10000,
938 /* No DAC is found for the extra output */
939 BAD_NO_DAC = 0x4000,
940 /* No possible multi-ios */
941 BAD_MULTI_IO = 0x103,
942 /* No individual DAC for extra output */
943 BAD_NO_EXTRA_DAC = 0x102,
944 /* No individual DAC for extra surrounds */
945 BAD_NO_EXTRA_SURR_DAC = 0x101,
946 /* Primary DAC shared with main surrounds */
947 BAD_SHARED_SURROUND = 0x100,
948 /* Primary DAC shared with main CLFE */
949 BAD_SHARED_CLFE = 0x10,
950 /* Primary DAC shared with extra surrounds */
951 BAD_SHARED_EXTRA_SURROUND = 0x10,
952 /* Volume widget is shared */
953 BAD_SHARED_VOL = 0x10,
954};
955
Takashi Iwai0e614dd2013-01-07 15:11:44 +0100956/* look for widgets in the given path which are appropriate for
Takashi Iwai352f7f92012-12-19 12:52:06 +0100957 * volume and mute controls, and assign the values to ctls[].
958 *
959 * When no appropriate widget is found in the path, the badness value
960 * is incremented depending on the situation. The function returns the
961 * total badness for both volume and mute controls.
962 */
Takashi Iwai0e614dd2013-01-07 15:11:44 +0100963static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100964{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100965 hda_nid_t nid;
966 unsigned int val;
967 int badness = 0;
968
969 if (!path)
970 return BAD_SHARED_VOL * 2;
Takashi Iwai0e614dd2013-01-07 15:11:44 +0100971
972 if (path->ctls[NID_PATH_VOL_CTL] ||
973 path->ctls[NID_PATH_MUTE_CTL])
974 return 0; /* already evaluated */
975
Takashi Iwai352f7f92012-12-19 12:52:06 +0100976 nid = look_for_out_vol_nid(codec, path);
977 if (nid) {
978 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
979 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
980 badness += BAD_SHARED_VOL;
981 else
982 path->ctls[NID_PATH_VOL_CTL] = val;
983 } else
984 badness += BAD_SHARED_VOL;
985 nid = look_for_out_mute_nid(codec, path);
986 if (nid) {
987 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
988 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
989 nid_has_mute(codec, nid, HDA_OUTPUT))
990 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
991 else
992 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
993 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
994 badness += BAD_SHARED_VOL;
995 else
996 path->ctls[NID_PATH_MUTE_CTL] = val;
997 } else
998 badness += BAD_SHARED_VOL;
999 return badness;
1000}
1001
1002struct badness_table {
1003 int no_primary_dac; /* no primary DAC */
1004 int no_dac; /* no secondary DACs */
1005 int shared_primary; /* primary DAC is shared with main output */
1006 int shared_surr; /* secondary DAC shared with main or primary */
1007 int shared_clfe; /* third DAC shared with main or primary */
1008 int shared_surr_main; /* secondary DAC sahred with main/DAC0 */
1009};
1010
1011static struct badness_table main_out_badness = {
1012 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1013 .no_dac = BAD_NO_DAC,
1014 .shared_primary = BAD_NO_PRIMARY_DAC,
1015 .shared_surr = BAD_SHARED_SURROUND,
1016 .shared_clfe = BAD_SHARED_CLFE,
1017 .shared_surr_main = BAD_SHARED_SURROUND,
1018};
1019
1020static struct badness_table extra_out_badness = {
1021 .no_primary_dac = BAD_NO_DAC,
1022 .no_dac = BAD_NO_DAC,
1023 .shared_primary = BAD_NO_EXTRA_DAC,
1024 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1025 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1026 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1027};
1028
Takashi Iwai7385df62013-01-07 09:50:52 +01001029/* get the DAC of the primary output corresponding to the given array index */
1030static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1031{
1032 struct hda_gen_spec *spec = codec->spec;
1033 struct auto_pin_cfg *cfg = &spec->autocfg;
1034
1035 if (cfg->line_outs > idx)
1036 return spec->private_dac_nids[idx];
1037 idx -= cfg->line_outs;
1038 if (spec->multi_ios > idx)
1039 return spec->multi_io[idx].dac;
1040 return 0;
1041}
1042
1043/* return the DAC if it's reachable, otherwise zero */
1044static inline hda_nid_t try_dac(struct hda_codec *codec,
1045 hda_nid_t dac, hda_nid_t pin)
1046{
1047 return is_reachable_path(codec, dac, pin) ? dac : 0;
1048}
1049
Takashi Iwai352f7f92012-12-19 12:52:06 +01001050/* try to assign DACs to pins and return the resultant badness */
1051static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1052 const hda_nid_t *pins, hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001053 int *path_idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001054 const struct badness_table *bad)
1055{
1056 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001057 int i, j;
1058 int badness = 0;
1059 hda_nid_t dac;
1060
1061 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 return 0;
1063
Takashi Iwai352f7f92012-12-19 12:52:06 +01001064 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001065 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001066 hda_nid_t pin = pins[i];
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001067
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001068 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1069 if (path) {
1070 badness += assign_out_path_ctls(codec, path);
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001071 continue;
1072 }
1073
1074 dacs[i] = look_for_dac(codec, pin, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001075 if (!dacs[i] && !i) {
Takashi Iwai980428c2013-01-09 09:28:20 +01001076 /* try to steal the DAC of surrounds for the front */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001077 for (j = 1; j < num_outs; j++) {
1078 if (is_reachable_path(codec, dacs[j], pin)) {
1079 dacs[0] = dacs[j];
1080 dacs[j] = 0;
Takashi Iwai980428c2013-01-09 09:28:20 +01001081 invalidate_nid_path(codec, path_idx[j]);
Takashi Iwai196c17662013-01-04 15:01:40 +01001082 path_idx[j] = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001083 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 }
1085 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001086 }
1087 dac = dacs[i];
1088 if (!dac) {
Takashi Iwai7385df62013-01-07 09:50:52 +01001089 if (num_outs > 2)
1090 dac = try_dac(codec, get_primary_out(codec, i), pin);
1091 if (!dac)
1092 dac = try_dac(codec, dacs[0], pin);
1093 if (!dac)
1094 dac = try_dac(codec, get_primary_out(codec, i), pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001095 if (dac) {
1096 if (!i)
1097 badness += bad->shared_primary;
1098 else if (i == 1)
1099 badness += bad->shared_surr;
1100 else
1101 badness += bad->shared_clfe;
1102 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1103 dac = spec->private_dac_nids[0];
1104 badness += bad->shared_surr_main;
1105 } else if (!i)
1106 badness += bad->no_primary_dac;
1107 else
1108 badness += bad->no_dac;
1109 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001110 if (!dac)
1111 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001112 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001113 if (!path && !i && spec->mixer_nid) {
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001114 /* try with aamix */
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001115 path = snd_hda_add_new_path(codec, dac, pin, 0);
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001116 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001117 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001118 dac = dacs[i] = 0;
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001119 badness += bad->no_dac;
1120 } else {
Takashi Iwaia7694092013-01-21 10:43:18 +01001121 /* print_nid_path("output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001122 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001123 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001124 badness += assign_out_path_ctls(codec, path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001125 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001126 }
1127
1128 return badness;
1129}
1130
1131/* return NID if the given pin has only a single connection to a certain DAC */
1132static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1133{
1134 struct hda_gen_spec *spec = codec->spec;
1135 int i;
1136 hda_nid_t nid_found = 0;
1137
1138 for (i = 0; i < spec->num_all_dacs; i++) {
1139 hda_nid_t nid = spec->all_dacs[i];
1140 if (!nid || is_dac_already_used(codec, nid))
1141 continue;
1142 if (is_reachable_path(codec, nid, pin)) {
1143 if (nid_found)
1144 return 0;
1145 nid_found = nid;
1146 }
1147 }
1148 return nid_found;
1149}
1150
1151/* check whether the given pin can be a multi-io pin */
1152static bool can_be_multiio_pin(struct hda_codec *codec,
1153 unsigned int location, hda_nid_t nid)
1154{
1155 unsigned int defcfg, caps;
1156
1157 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1158 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1159 return false;
1160 if (location && get_defcfg_location(defcfg) != location)
1161 return false;
1162 caps = snd_hda_query_pin_caps(codec, nid);
1163 if (!(caps & AC_PINCAP_OUT))
1164 return false;
1165 return true;
1166}
1167
Takashi Iwaie22aab72013-01-04 14:50:04 +01001168/* count the number of input pins that are capable to be multi-io */
1169static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1170{
1171 struct hda_gen_spec *spec = codec->spec;
1172 struct auto_pin_cfg *cfg = &spec->autocfg;
1173 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1174 unsigned int location = get_defcfg_location(defcfg);
1175 int type, i;
1176 int num_pins = 0;
1177
1178 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1179 for (i = 0; i < cfg->num_inputs; i++) {
1180 if (cfg->inputs[i].type != type)
1181 continue;
1182 if (can_be_multiio_pin(codec, location,
1183 cfg->inputs[i].pin))
1184 num_pins++;
1185 }
1186 }
1187 return num_pins;
1188}
1189
Takashi Iwai352f7f92012-12-19 12:52:06 +01001190/*
1191 * multi-io helper
1192 *
1193 * When hardwired is set, try to fill ony hardwired pins, and returns
1194 * zero if any pins are filled, non-zero if nothing found.
1195 * When hardwired is off, try to fill possible input pins, and returns
1196 * the badness value.
1197 */
1198static int fill_multi_ios(struct hda_codec *codec,
1199 hda_nid_t reference_pin,
Takashi Iwaie22aab72013-01-04 14:50:04 +01001200 bool hardwired)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001201{
1202 struct hda_gen_spec *spec = codec->spec;
1203 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie22aab72013-01-04 14:50:04 +01001204 int type, i, j, num_pins, old_pins;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001205 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1206 unsigned int location = get_defcfg_location(defcfg);
1207 int badness = 0;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001208 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001209
1210 old_pins = spec->multi_ios;
1211 if (old_pins >= 2)
1212 goto end_fill;
1213
Takashi Iwaie22aab72013-01-04 14:50:04 +01001214 num_pins = count_multiio_pins(codec, reference_pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001215 if (num_pins < 2)
1216 goto end_fill;
1217
Takashi Iwai352f7f92012-12-19 12:52:06 +01001218 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1219 for (i = 0; i < cfg->num_inputs; i++) {
1220 hda_nid_t nid = cfg->inputs[i].pin;
1221 hda_nid_t dac = 0;
1222
1223 if (cfg->inputs[i].type != type)
1224 continue;
1225 if (!can_be_multiio_pin(codec, location, nid))
1226 continue;
1227 for (j = 0; j < spec->multi_ios; j++) {
1228 if (nid == spec->multi_io[j].pin)
1229 break;
1230 }
1231 if (j < spec->multi_ios)
1232 continue;
1233
Takashi Iwai352f7f92012-12-19 12:52:06 +01001234 if (hardwired)
1235 dac = get_dac_if_single(codec, nid);
1236 else if (!dac)
1237 dac = look_for_dac(codec, nid, false);
1238 if (!dac) {
1239 badness++;
1240 continue;
1241 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001242 path = snd_hda_add_new_path(codec, dac, nid,
1243 -spec->mixer_nid);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001244 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001245 badness++;
1246 continue;
1247 }
Takashi Iwaia7694092013-01-21 10:43:18 +01001248 /* print_nid_path("multiio", path); */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001249 spec->multi_io[spec->multi_ios].pin = nid;
1250 spec->multi_io[spec->multi_ios].dac = dac;
Takashi Iwai196c17662013-01-04 15:01:40 +01001251 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1252 snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001253 spec->multi_ios++;
1254 if (spec->multi_ios >= 2)
1255 break;
1256 }
1257 }
1258 end_fill:
1259 if (badness)
1260 badness = BAD_MULTI_IO;
1261 if (old_pins == spec->multi_ios) {
1262 if (hardwired)
1263 return 1; /* nothing found */
1264 else
1265 return badness; /* no badness if nothing found */
1266 }
1267 if (!hardwired && spec->multi_ios < 2) {
1268 /* cancel newly assigned paths */
1269 spec->paths.used -= spec->multi_ios - old_pins;
1270 spec->multi_ios = old_pins;
1271 return badness;
1272 }
1273
1274 /* assign volume and mute controls */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001275 for (i = old_pins; i < spec->multi_ios; i++) {
1276 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1277 badness += assign_out_path_ctls(codec, path);
1278 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001279
1280 return badness;
1281}
1282
1283/* map DACs for all pins in the list if they are single connections */
1284static bool map_singles(struct hda_codec *codec, int outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001285 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001286{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001287 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001288 int i;
1289 bool found = false;
1290 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001291 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001292 hda_nid_t dac;
1293 if (dacs[i])
1294 continue;
1295 dac = get_dac_if_single(codec, pins[i]);
1296 if (!dac)
1297 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001298 path = snd_hda_add_new_path(codec, dac, pins[i],
1299 -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001300 if (!path && !i && spec->mixer_nid)
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001301 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001302 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001303 dacs[i] = dac;
1304 found = true;
Takashi Iwaia7694092013-01-21 10:43:18 +01001305 /* print_nid_path("output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001306 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001307 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001308 }
1309 }
1310 return found;
1311}
1312
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001313/* create a new path including aamix if available, and return its index */
1314static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1315{
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001316 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001317 struct nid_path *path;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001318 hda_nid_t dac, pin;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001319
1320 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001321 if (!path || !path->depth ||
1322 is_nid_contained(path, spec->mixer_nid))
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001323 return 0;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001324 dac = path->path[0];
1325 pin = path->path[path->depth - 1];
1326 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1327 if (!path) {
1328 if (dac != spec->multiout.dac_nids[0])
1329 dac = spec->multiout.dac_nids[0];
1330 else if (spec->multiout.hp_out_nid[0])
1331 dac = spec->multiout.hp_out_nid[0];
1332 else if (spec->multiout.extra_out_nid[0])
1333 dac = spec->multiout.extra_out_nid[0];
1334 if (dac)
1335 path = snd_hda_add_new_path(codec, dac, pin,
1336 spec->mixer_nid);
1337 }
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001338 if (!path)
1339 return 0;
Takashi Iwaia7694092013-01-21 10:43:18 +01001340 /* print_nid_path("output-aamix", path); */
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001341 path->active = false; /* unused as default */
1342 return snd_hda_get_path_idx(codec, path);
1343}
1344
Takashi Iwaia07a9492013-01-07 16:44:06 +01001345/* fill the empty entries in the dac array for speaker/hp with the
1346 * shared dac pointed by the paths
1347 */
1348static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1349 hda_nid_t *dacs, int *path_idx)
1350{
1351 struct nid_path *path;
1352 int i;
1353
1354 for (i = 0; i < num_outs; i++) {
1355 if (dacs[i])
1356 continue;
1357 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1358 if (!path)
1359 continue;
1360 dacs[i] = path->path[0];
1361 }
1362}
1363
Takashi Iwai352f7f92012-12-19 12:52:06 +01001364/* fill in the dac_nids table from the parsed pin configuration */
1365static int fill_and_eval_dacs(struct hda_codec *codec,
1366 bool fill_hardwired,
1367 bool fill_mio_first)
1368{
1369 struct hda_gen_spec *spec = codec->spec;
1370 struct auto_pin_cfg *cfg = &spec->autocfg;
1371 int i, err, badness;
1372
1373 /* set num_dacs once to full for look_for_dac() */
1374 spec->multiout.num_dacs = cfg->line_outs;
1375 spec->multiout.dac_nids = spec->private_dac_nids;
1376 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1377 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1378 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1379 spec->multi_ios = 0;
1380 snd_array_free(&spec->paths);
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001381
1382 /* clear path indices */
1383 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1384 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1385 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1386 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1387 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
Takashi Iwaic697b712013-01-07 17:09:26 +01001388 memset(spec->input_paths, 0, sizeof(spec->input_paths));
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001389 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1390 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1391
Takashi Iwai352f7f92012-12-19 12:52:06 +01001392 badness = 0;
1393
1394 /* fill hard-wired DACs first */
1395 if (fill_hardwired) {
1396 bool mapped;
1397 do {
1398 mapped = map_singles(codec, cfg->line_outs,
1399 cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001400 spec->private_dac_nids,
1401 spec->out_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001402 mapped |= map_singles(codec, cfg->hp_outs,
1403 cfg->hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001404 spec->multiout.hp_out_nid,
1405 spec->hp_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001406 mapped |= map_singles(codec, cfg->speaker_outs,
1407 cfg->speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001408 spec->multiout.extra_out_nid,
1409 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001410 if (fill_mio_first && cfg->line_outs == 1 &&
1411 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001412 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001413 if (!err)
1414 mapped = true;
1415 }
1416 } while (mapped);
1417 }
1418
1419 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001420 spec->private_dac_nids, spec->out_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001421 &main_out_badness);
1422
Takashi Iwai352f7f92012-12-19 12:52:06 +01001423 if (fill_mio_first &&
1424 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1425 /* try to fill multi-io first */
Takashi Iwaie22aab72013-01-04 14:50:04 +01001426 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001427 if (err < 0)
1428 return err;
1429 /* we don't count badness at this stage yet */
1430 }
1431
1432 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1433 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1434 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001435 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001436 &extra_out_badness);
1437 if (err < 0)
1438 return err;
1439 badness += err;
1440 }
1441 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1442 err = try_assign_dacs(codec, cfg->speaker_outs,
1443 cfg->speaker_pins,
1444 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001445 spec->speaker_paths,
1446 &extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001447 if (err < 0)
1448 return err;
1449 badness += err;
1450 }
1451 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001452 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001453 if (err < 0)
1454 return err;
1455 badness += err;
1456 }
Takashi Iwaie22aab72013-01-04 14:50:04 +01001457
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001458 if (spec->mixer_nid) {
1459 spec->aamix_out_paths[0] =
1460 check_aamix_out_path(codec, spec->out_paths[0]);
1461 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1462 spec->aamix_out_paths[1] =
1463 check_aamix_out_path(codec, spec->hp_paths[0]);
1464 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1465 spec->aamix_out_paths[2] =
1466 check_aamix_out_path(codec, spec->speaker_paths[0]);
1467 }
1468
Takashi Iwaie22aab72013-01-04 14:50:04 +01001469 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1470 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1471 spec->multi_ios = 1; /* give badness */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001472
Takashi Iwaia07a9492013-01-07 16:44:06 +01001473 /* re-count num_dacs and squash invalid entries */
1474 spec->multiout.num_dacs = 0;
1475 for (i = 0; i < cfg->line_outs; i++) {
1476 if (spec->private_dac_nids[i])
1477 spec->multiout.num_dacs++;
1478 else {
1479 memmove(spec->private_dac_nids + i,
1480 spec->private_dac_nids + i + 1,
1481 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1482 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1483 }
1484 }
1485
1486 spec->ext_channel_count = spec->min_channel_count =
David Henningssonc0f3b212013-01-16 11:45:37 +01001487 spec->multiout.num_dacs * 2;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001488
Takashi Iwai352f7f92012-12-19 12:52:06 +01001489 if (spec->multi_ios == 2) {
1490 for (i = 0; i < 2; i++)
1491 spec->private_dac_nids[spec->multiout.num_dacs++] =
1492 spec->multi_io[i].dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001493 } else if (spec->multi_ios) {
1494 spec->multi_ios = 0;
1495 badness += BAD_MULTI_IO;
1496 }
1497
Takashi Iwaia07a9492013-01-07 16:44:06 +01001498 /* re-fill the shared DAC for speaker / headphone */
1499 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1500 refill_shared_dacs(codec, cfg->hp_outs,
1501 spec->multiout.hp_out_nid,
1502 spec->hp_paths);
1503 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1504 refill_shared_dacs(codec, cfg->speaker_outs,
1505 spec->multiout.extra_out_nid,
1506 spec->speaker_paths);
1507
Takashi Iwai352f7f92012-12-19 12:52:06 +01001508 return badness;
1509}
1510
1511#define DEBUG_BADNESS
1512
1513#ifdef DEBUG_BADNESS
1514#define debug_badness snd_printdd
1515#else
1516#define debug_badness(...)
1517#endif
1518
Takashi Iwaia7694092013-01-21 10:43:18 +01001519#ifdef DEBUG_BADNESS
1520static inline void print_nid_path_idx(struct hda_codec *codec,
1521 const char *pfx, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001522{
Takashi Iwaia7694092013-01-21 10:43:18 +01001523 struct nid_path *path;
1524
1525 path = snd_hda_get_path_from_idx(codec, idx);
1526 if (path)
1527 print_nid_path(pfx, path);
1528}
1529
1530static void debug_show_configs(struct hda_codec *codec,
1531 struct auto_pin_cfg *cfg)
1532{
1533 struct hda_gen_spec *spec = codec->spec;
1534#ifdef CONFIG_SND_DEBUG_VERBOSE
1535 static const char * const lo_type[3] = { "LO", "SP", "HP" };
1536#endif
1537 int i;
1538
1539 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001540 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001541 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001542 spec->multiout.dac_nids[0],
1543 spec->multiout.dac_nids[1],
1544 spec->multiout.dac_nids[2],
Takashi Iwaia7694092013-01-21 10:43:18 +01001545 spec->multiout.dac_nids[3],
1546 lo_type[cfg->line_out_type]);
1547 for (i = 0; i < cfg->line_outs; i++)
1548 print_nid_path_idx(codec, " out", spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001549 if (spec->multi_ios > 0)
1550 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1551 spec->multi_ios,
1552 spec->multi_io[0].pin, spec->multi_io[1].pin,
1553 spec->multi_io[0].dac, spec->multi_io[1].dac);
Takashi Iwaia7694092013-01-21 10:43:18 +01001554 for (i = 0; i < spec->multi_ios; i++)
1555 print_nid_path_idx(codec, " mio",
1556 spec->out_paths[cfg->line_outs + i]);
1557 if (cfg->hp_outs)
1558 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001559 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001560 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001561 spec->multiout.hp_out_nid[0],
1562 spec->multiout.hp_out_nid[1],
1563 spec->multiout.hp_out_nid[2],
1564 spec->multiout.hp_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001565 for (i = 0; i < cfg->hp_outs; i++)
1566 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1567 if (cfg->speaker_outs)
1568 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001569 cfg->speaker_pins[0], cfg->speaker_pins[1],
1570 cfg->speaker_pins[2], cfg->speaker_pins[3],
1571 spec->multiout.extra_out_nid[0],
1572 spec->multiout.extra_out_nid[1],
1573 spec->multiout.extra_out_nid[2],
1574 spec->multiout.extra_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001575 for (i = 0; i < cfg->speaker_outs; i++)
1576 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1577 for (i = 0; i < 3; i++)
1578 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001579}
Takashi Iwaia7694092013-01-21 10:43:18 +01001580#else
1581#define debug_show_configs(codec, cfg) /* NOP */
1582#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01001583
1584/* find all available DACs of the codec */
1585static void fill_all_dac_nids(struct hda_codec *codec)
1586{
1587 struct hda_gen_spec *spec = codec->spec;
1588 int i;
1589 hda_nid_t nid = codec->start_nid;
1590
1591 spec->num_all_dacs = 0;
1592 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1593 for (i = 0; i < codec->num_nodes; i++, nid++) {
1594 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1595 continue;
1596 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1597 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1598 break;
1599 }
1600 spec->all_dacs[spec->num_all_dacs++] = nid;
1601 }
1602}
1603
1604static int parse_output_paths(struct hda_codec *codec)
1605{
1606 struct hda_gen_spec *spec = codec->spec;
1607 struct auto_pin_cfg *cfg = &spec->autocfg;
1608 struct auto_pin_cfg *best_cfg;
Takashi Iwai9314a582013-01-21 10:49:05 +01001609 unsigned int val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001610 int best_badness = INT_MAX;
1611 int badness;
1612 bool fill_hardwired = true, fill_mio_first = true;
1613 bool best_wired = true, best_mio = true;
1614 bool hp_spk_swapped = false;
1615
Takashi Iwai352f7f92012-12-19 12:52:06 +01001616 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1617 if (!best_cfg)
1618 return -ENOMEM;
1619 *best_cfg = *cfg;
1620
1621 for (;;) {
1622 badness = fill_and_eval_dacs(codec, fill_hardwired,
1623 fill_mio_first);
1624 if (badness < 0) {
1625 kfree(best_cfg);
1626 return badness;
1627 }
1628 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1629 cfg->line_out_type, fill_hardwired, fill_mio_first,
1630 badness);
Takashi Iwaia7694092013-01-21 10:43:18 +01001631 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001632 if (badness < best_badness) {
1633 best_badness = badness;
1634 *best_cfg = *cfg;
1635 best_wired = fill_hardwired;
1636 best_mio = fill_mio_first;
1637 }
1638 if (!badness)
1639 break;
1640 fill_mio_first = !fill_mio_first;
1641 if (!fill_mio_first)
1642 continue;
1643 fill_hardwired = !fill_hardwired;
1644 if (!fill_hardwired)
1645 continue;
1646 if (hp_spk_swapped)
1647 break;
1648 hp_spk_swapped = true;
1649 if (cfg->speaker_outs > 0 &&
1650 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1651 cfg->hp_outs = cfg->line_outs;
1652 memcpy(cfg->hp_pins, cfg->line_out_pins,
1653 sizeof(cfg->hp_pins));
1654 cfg->line_outs = cfg->speaker_outs;
1655 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1656 sizeof(cfg->speaker_pins));
1657 cfg->speaker_outs = 0;
1658 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1659 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1660 fill_hardwired = true;
1661 continue;
1662 }
1663 if (cfg->hp_outs > 0 &&
1664 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1665 cfg->speaker_outs = cfg->line_outs;
1666 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1667 sizeof(cfg->speaker_pins));
1668 cfg->line_outs = cfg->hp_outs;
1669 memcpy(cfg->line_out_pins, cfg->hp_pins,
1670 sizeof(cfg->hp_pins));
1671 cfg->hp_outs = 0;
1672 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1673 cfg->line_out_type = AUTO_PIN_HP_OUT;
1674 fill_hardwired = true;
1675 continue;
1676 }
1677 break;
1678 }
1679
1680 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001681 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001682 *cfg = *best_cfg;
1683 fill_and_eval_dacs(codec, best_wired, best_mio);
1684 }
1685 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1686 cfg->line_out_type, best_wired, best_mio);
Takashi Iwaia7694092013-01-21 10:43:18 +01001687 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001688
1689 if (cfg->line_out_pins[0]) {
1690 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01001691 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001692 if (path)
1693 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01001694 if (spec->vmaster_nid)
1695 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
1696 HDA_OUTPUT, spec->vmaster_tlv);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001697 }
1698
Takashi Iwai9314a582013-01-21 10:49:05 +01001699 /* set initial pinctl targets */
1700 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
1701 val = PIN_HP;
1702 else
1703 val = PIN_OUT;
1704 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
1705 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1706 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
1707 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1708 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
1709 set_pin_targets(codec, cfg->speaker_outs,
1710 cfg->speaker_pins, val);
1711 }
1712
Takashi Iwai352f7f92012-12-19 12:52:06 +01001713 kfree(best_cfg);
1714 return 0;
1715}
1716
1717/* add playback controls from the parsed DAC table */
1718static int create_multi_out_ctls(struct hda_codec *codec,
1719 const struct auto_pin_cfg *cfg)
1720{
1721 struct hda_gen_spec *spec = codec->spec;
1722 int i, err, noutputs;
1723
1724 noutputs = cfg->line_outs;
1725 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1726 noutputs += spec->multi_ios;
1727
1728 for (i = 0; i < noutputs; i++) {
1729 const char *name;
1730 int index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001731 struct nid_path *path;
1732
Takashi Iwai196c17662013-01-04 15:01:40 +01001733 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001734 if (!path)
1735 continue;
Takashi Iwai247d85e2013-01-17 16:18:11 +01001736
1737 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001738 if (!name || !strcmp(name, "CLFE")) {
1739 /* Center/LFE */
1740 err = add_vol_ctl(codec, "Center", 0, 1, path);
1741 if (err < 0)
1742 return err;
1743 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1744 if (err < 0)
1745 return err;
Takashi Iwai247d85e2013-01-17 16:18:11 +01001746 } else {
1747 err = add_stereo_vol(codec, name, index, path);
1748 if (err < 0)
1749 return err;
1750 }
1751
1752 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
1753 if (!name || !strcmp(name, "CLFE")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001754 err = add_sw_ctl(codec, "Center", 0, 1, path);
1755 if (err < 0)
1756 return err;
1757 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1758 if (err < 0)
1759 return err;
1760 } else {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001761 err = add_stereo_sw(codec, name, index, path);
1762 if (err < 0)
1763 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 }
1765 }
1766 return 0;
1767}
1768
Takashi Iwaic2c80382013-01-07 10:33:57 +01001769static int create_extra_out(struct hda_codec *codec, int path_idx,
Takashi Iwai196c17662013-01-04 15:01:40 +01001770 const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001772 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 int err;
1774
Takashi Iwai196c17662013-01-04 15:01:40 +01001775 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001776 if (!path)
1777 return 0;
Takashi Iwaic2c80382013-01-07 10:33:57 +01001778 err = add_stereo_vol(codec, pfx, cidx, path);
1779 if (err < 0)
1780 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001781 err = add_stereo_sw(codec, pfx, cidx, path);
1782 if (err < 0)
1783 return err;
1784 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785}
1786
Takashi Iwai352f7f92012-12-19 12:52:06 +01001787/* add playback controls for speaker and HP outputs */
1788static int create_extra_outs(struct hda_codec *codec, int num_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001789 const int *paths, const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790{
Takashi Iwaic2c80382013-01-07 10:33:57 +01001791 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001792
1793 for (i = 0; i < num_pins; i++) {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001794 const char *name;
1795 char tmp[44];
1796 int err, idx = 0;
1797
1798 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
1799 name = "Bass Speaker";
1800 else if (num_pins >= 3) {
1801 snprintf(tmp, sizeof(tmp), "%s %s",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001802 pfx, channel_name[i]);
Takashi Iwaic2c80382013-01-07 10:33:57 +01001803 name = tmp;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001804 } else {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001805 name = pfx;
1806 idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 }
Takashi Iwaic2c80382013-01-07 10:33:57 +01001808 err = create_extra_out(codec, paths[i], name, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001809 if (err < 0)
1810 return err;
1811 }
1812 return 0;
1813}
Takashi Iwai97ec5582006-03-21 11:29:07 +01001814
Takashi Iwai352f7f92012-12-19 12:52:06 +01001815static int create_hp_out_ctls(struct hda_codec *codec)
1816{
1817 struct hda_gen_spec *spec = codec->spec;
1818 return create_extra_outs(codec, spec->autocfg.hp_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001819 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001820 "Headphone");
1821}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
Takashi Iwai352f7f92012-12-19 12:52:06 +01001823static int create_speaker_out_ctls(struct hda_codec *codec)
1824{
1825 struct hda_gen_spec *spec = codec->spec;
1826 return create_extra_outs(codec, spec->autocfg.speaker_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001827 spec->speaker_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001828 "Speaker");
1829}
1830
1831/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001832 * independent HP controls
1833 */
1834
1835static int indep_hp_info(struct snd_kcontrol *kcontrol,
1836 struct snd_ctl_elem_info *uinfo)
1837{
1838 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
1839}
1840
1841static int indep_hp_get(struct snd_kcontrol *kcontrol,
1842 struct snd_ctl_elem_value *ucontrol)
1843{
1844 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1845 struct hda_gen_spec *spec = codec->spec;
1846 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
1847 return 0;
1848}
1849
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001850static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
1851 int nomix_path_idx, int mix_path_idx,
1852 int out_type);
1853
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001854static int indep_hp_put(struct snd_kcontrol *kcontrol,
1855 struct snd_ctl_elem_value *ucontrol)
1856{
1857 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1858 struct hda_gen_spec *spec = codec->spec;
1859 unsigned int select = ucontrol->value.enumerated.item[0];
1860 int ret = 0;
1861
1862 mutex_lock(&spec->pcm_mutex);
1863 if (spec->active_streams) {
1864 ret = -EBUSY;
1865 goto unlock;
1866 }
1867
1868 if (spec->indep_hp_enabled != select) {
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001869 hda_nid_t *dacp;
1870 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
1871 dacp = &spec->private_dac_nids[0];
1872 else
1873 dacp = &spec->multiout.hp_out_nid[0];
1874
1875 /* update HP aamix paths in case it conflicts with indep HP */
1876 if (spec->have_aamix_ctl) {
1877 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
1878 update_aamix_paths(codec, spec->aamix_mode,
1879 spec->out_paths[0],
1880 spec->aamix_out_paths[0],
1881 spec->autocfg.line_out_type);
1882 else
1883 update_aamix_paths(codec, spec->aamix_mode,
1884 spec->hp_paths[0],
1885 spec->aamix_out_paths[1],
1886 AUTO_PIN_HP_OUT);
1887 }
1888
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001889 spec->indep_hp_enabled = select;
1890 if (spec->indep_hp_enabled)
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001891 *dacp = 0;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001892 else
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001893 *dacp = spec->alt_dac_nid;
Takashi Iwai92603c52013-01-22 07:46:31 +01001894
1895 /* update HP auto-mute state too */
1896 if (spec->hp_automute_hook)
1897 spec->hp_automute_hook(codec, NULL);
1898 else
1899 snd_hda_gen_hp_automute(codec, NULL);
1900
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001901 ret = 1;
1902 }
1903 unlock:
1904 mutex_unlock(&spec->pcm_mutex);
1905 return ret;
1906}
1907
1908static const struct snd_kcontrol_new indep_hp_ctl = {
1909 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1910 .name = "Independent HP",
1911 .info = indep_hp_info,
1912 .get = indep_hp_get,
1913 .put = indep_hp_put,
1914};
1915
1916
1917static int create_indep_hp_ctls(struct hda_codec *codec)
1918{
1919 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001920 hda_nid_t dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001921
1922 if (!spec->indep_hp)
1923 return 0;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001924 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
1925 dac = spec->multiout.dac_nids[0];
1926 else
1927 dac = spec->multiout.hp_out_nid[0];
1928 if (!dac) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001929 spec->indep_hp = 0;
1930 return 0;
1931 }
1932
1933 spec->indep_hp_enabled = false;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001934 spec->alt_dac_nid = dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001935 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
1936 return -ENOMEM;
1937 return 0;
1938}
1939
1940/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01001941 * channel mode enum control
1942 */
1943
1944static int ch_mode_info(struct snd_kcontrol *kcontrol,
1945 struct snd_ctl_elem_info *uinfo)
1946{
1947 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1948 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001949 int chs;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001950
1951 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1952 uinfo->count = 1;
1953 uinfo->value.enumerated.items = spec->multi_ios + 1;
1954 if (uinfo->value.enumerated.item > spec->multi_ios)
1955 uinfo->value.enumerated.item = spec->multi_ios;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001956 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
1957 sprintf(uinfo->value.enumerated.name, "%dch", chs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001958 return 0;
1959}
1960
1961static int ch_mode_get(struct snd_kcontrol *kcontrol,
1962 struct snd_ctl_elem_value *ucontrol)
1963{
1964 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1965 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001966 ucontrol->value.enumerated.item[0] =
1967 (spec->ext_channel_count - spec->min_channel_count) / 2;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001968 return 0;
1969}
1970
Takashi Iwai196c17662013-01-04 15:01:40 +01001971static inline struct nid_path *
1972get_multiio_path(struct hda_codec *codec, int idx)
1973{
1974 struct hda_gen_spec *spec = codec->spec;
1975 return snd_hda_get_path_from_idx(codec,
1976 spec->out_paths[spec->autocfg.line_outs + idx]);
1977}
1978
Takashi Iwaia5cc2502013-01-16 18:08:55 +01001979static void update_automute_all(struct hda_codec *codec);
1980
Takashi Iwai352f7f92012-12-19 12:52:06 +01001981static int set_multi_io(struct hda_codec *codec, int idx, bool output)
1982{
1983 struct hda_gen_spec *spec = codec->spec;
1984 hda_nid_t nid = spec->multi_io[idx].pin;
1985 struct nid_path *path;
1986
Takashi Iwai196c17662013-01-04 15:01:40 +01001987 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001988 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001990
1991 if (path->active == output)
1992 return 0;
1993
1994 if (output) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01001995 set_pin_target(codec, nid, PIN_OUT, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001996 snd_hda_activate_path(codec, path, true, true);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01001997 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001998 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01001999 set_pin_eapd(codec, nid, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002000 snd_hda_activate_path(codec, path, false, true);
Takashi Iwai2c12c302013-01-10 09:33:29 +01002001 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 }
Takashi Iwaia365fed2013-01-10 16:10:06 +01002003
2004 /* update jack retasking in case it modifies any of them */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002005 update_automute_all(codec);
Takashi Iwaia365fed2013-01-10 16:10:06 +01002006
Takashi Iwai352f7f92012-12-19 12:52:06 +01002007 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008}
2009
Takashi Iwai352f7f92012-12-19 12:52:06 +01002010static int ch_mode_put(struct snd_kcontrol *kcontrol,
2011 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002013 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2014 struct hda_gen_spec *spec = codec->spec;
2015 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
Takashi Iwai352f7f92012-12-19 12:52:06 +01002017 ch = ucontrol->value.enumerated.item[0];
2018 if (ch < 0 || ch > spec->multi_ios)
2019 return -EINVAL;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002020 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002021 return 0;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002022 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002023 for (i = 0; i < spec->multi_ios; i++)
2024 set_multi_io(codec, i, i < ch);
2025 spec->multiout.max_channels = max(spec->ext_channel_count,
2026 spec->const_channel_count);
2027 if (spec->need_dac_fix)
2028 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 return 1;
2030}
2031
Takashi Iwai352f7f92012-12-19 12:52:06 +01002032static const struct snd_kcontrol_new channel_mode_enum = {
2033 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2034 .name = "Channel Mode",
2035 .info = ch_mode_info,
2036 .get = ch_mode_get,
2037 .put = ch_mode_put,
2038};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
Takashi Iwai352f7f92012-12-19 12:52:06 +01002040static int create_multi_channel_mode(struct hda_codec *codec)
2041{
2042 struct hda_gen_spec *spec = codec->spec;
2043
2044 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002045 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002046 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 return 0;
2049}
2050
Takashi Iwai352f7f92012-12-19 12:52:06 +01002051/*
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002052 * aamix loopback enable/disable switch
2053 */
2054
2055#define loopback_mixing_info indep_hp_info
2056
2057static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2058 struct snd_ctl_elem_value *ucontrol)
2059{
2060 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2061 struct hda_gen_spec *spec = codec->spec;
2062 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2063 return 0;
2064}
2065
2066static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002067 int nomix_path_idx, int mix_path_idx,
2068 int out_type)
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002069{
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002070 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002071 struct nid_path *nomix_path, *mix_path;
2072
2073 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2074 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2075 if (!nomix_path || !mix_path)
2076 return;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002077
2078 /* if HP aamix path is driven from a different DAC and the
2079 * independent HP mode is ON, can't turn on aamix path
2080 */
2081 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2082 mix_path->path[0] != spec->alt_dac_nid)
2083 do_mix = false;
2084
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002085 if (do_mix) {
2086 snd_hda_activate_path(codec, nomix_path, false, true);
2087 snd_hda_activate_path(codec, mix_path, true, true);
2088 } else {
2089 snd_hda_activate_path(codec, mix_path, false, true);
2090 snd_hda_activate_path(codec, nomix_path, true, true);
2091 }
2092}
2093
2094static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2095 struct snd_ctl_elem_value *ucontrol)
2096{
2097 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2098 struct hda_gen_spec *spec = codec->spec;
2099 unsigned int val = ucontrol->value.enumerated.item[0];
2100
2101 if (val == spec->aamix_mode)
2102 return 0;
2103 spec->aamix_mode = val;
2104 update_aamix_paths(codec, val, spec->out_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002105 spec->aamix_out_paths[0],
2106 spec->autocfg.line_out_type);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002107 update_aamix_paths(codec, val, spec->hp_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002108 spec->aamix_out_paths[1],
2109 AUTO_PIN_HP_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002110 update_aamix_paths(codec, val, spec->speaker_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002111 spec->aamix_out_paths[2],
2112 AUTO_PIN_SPEAKER_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002113 return 1;
2114}
2115
2116static const struct snd_kcontrol_new loopback_mixing_enum = {
2117 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2118 .name = "Loopback Mixing",
2119 .info = loopback_mixing_info,
2120 .get = loopback_mixing_get,
2121 .put = loopback_mixing_put,
2122};
2123
2124static int create_loopback_mixing_ctl(struct hda_codec *codec)
2125{
2126 struct hda_gen_spec *spec = codec->spec;
2127
2128 if (!spec->mixer_nid)
2129 return 0;
2130 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
2131 spec->aamix_out_paths[2]))
2132 return 0;
2133 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2134 return -ENOMEM;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002135 spec->have_aamix_ctl = 1;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002136 return 0;
2137}
2138
2139/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002140 * shared headphone/mic handling
2141 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002142
Takashi Iwai352f7f92012-12-19 12:52:06 +01002143static void call_update_outputs(struct hda_codec *codec);
2144
2145/* for shared I/O, change the pin-control accordingly */
2146static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic)
2147{
2148 struct hda_gen_spec *spec = codec->spec;
2149 unsigned int val;
2150 hda_nid_t pin = spec->autocfg.inputs[1].pin;
2151 /* NOTE: this assumes that there are only two inputs, the
2152 * first is the real internal mic and the second is HP/mic jack.
2153 */
2154
2155 val = snd_hda_get_default_vref(codec, pin);
2156
2157 /* This pin does not have vref caps - let's enable vref on pin 0x18
2158 instead, as suggested by Realtek */
2159 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2160 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2161 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2162 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01002163 snd_hda_set_pin_ctl_cache(codec, vref_pin,
2164 PIN_IN | (set_as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02002165 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002166
2167 val = set_as_mic ? val | PIN_IN : PIN_HP;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002168 set_pin_target(codec, pin, val, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002169
2170 spec->automute_speaker = !set_as_mic;
2171 call_update_outputs(codec);
2172}
2173
2174/* create a shared input with the headphone out */
2175static int create_shared_input(struct hda_codec *codec)
2176{
2177 struct hda_gen_spec *spec = codec->spec;
2178 struct auto_pin_cfg *cfg = &spec->autocfg;
2179 unsigned int defcfg;
2180 hda_nid_t nid;
2181
2182 /* only one internal input pin? */
2183 if (cfg->num_inputs != 1)
2184 return 0;
2185 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2186 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2187 return 0;
2188
2189 if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
2190 nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
2191 else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
2192 nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
2193 else
2194 return 0; /* both not available */
2195
2196 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2197 return 0; /* no input */
2198
2199 cfg->inputs[1].pin = nid;
2200 cfg->inputs[1].type = AUTO_PIN_MIC;
2201 cfg->num_inputs = 2;
2202 spec->shared_mic_hp = 1;
2203 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
2204 return 0;
2205}
2206
Takashi Iwai978e77e2013-01-10 16:57:58 +01002207/*
2208 * output jack mode
2209 */
2210static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2211 struct snd_ctl_elem_info *uinfo)
2212{
2213 static const char * const texts[] = {
2214 "Line Out", "Headphone Out",
2215 };
2216 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, texts);
2217}
2218
2219static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2220 struct snd_ctl_elem_value *ucontrol)
2221{
2222 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2223 hda_nid_t nid = kcontrol->private_value;
2224 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2225 ucontrol->value.enumerated.item[0] = 1;
2226 else
2227 ucontrol->value.enumerated.item[0] = 0;
2228 return 0;
2229}
2230
2231static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2232 struct snd_ctl_elem_value *ucontrol)
2233{
2234 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2235 hda_nid_t nid = kcontrol->private_value;
2236 unsigned int val;
2237
2238 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2239 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2240 return 0;
2241 snd_hda_set_pin_ctl_cache(codec, nid, val);
2242 return 1;
2243}
2244
2245static const struct snd_kcontrol_new out_jack_mode_enum = {
2246 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2247 .info = out_jack_mode_info,
2248 .get = out_jack_mode_get,
2249 .put = out_jack_mode_put,
2250};
2251
2252static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2253{
2254 struct hda_gen_spec *spec = codec->spec;
2255 int i;
2256
2257 for (i = 0; i < spec->kctls.used; i++) {
2258 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2259 if (!strcmp(kctl->name, name) && kctl->index == idx)
2260 return true;
2261 }
2262 return false;
2263}
2264
2265static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2266 char *name, size_t name_len)
2267{
2268 struct hda_gen_spec *spec = codec->spec;
2269 int idx = 0;
2270
2271 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2272 strlcat(name, " Jack Mode", name_len);
2273
2274 for (; find_kctl_name(codec, name, idx); idx++)
2275 ;
2276}
2277
2278static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2279 hda_nid_t *pins)
2280{
2281 struct hda_gen_spec *spec = codec->spec;
2282 int i;
2283
2284 for (i = 0; i < num_pins; i++) {
2285 hda_nid_t pin = pins[i];
2286 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2287 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV)) {
2288 struct snd_kcontrol_new *knew;
2289 char name[44];
2290 get_jack_mode_name(codec, pin, name, sizeof(name));
2291 knew = snd_hda_gen_add_kctl(spec, name,
2292 &out_jack_mode_enum);
2293 if (!knew)
2294 return -ENOMEM;
2295 knew->private_value = pin;
2296 }
2297 }
2298
2299 return 0;
2300}
2301
Takashi Iwai294765582013-01-17 09:52:11 +01002302/*
2303 * input jack mode
2304 */
2305
2306/* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2307#define NUM_VREFS 6
2308
2309static const char * const vref_texts[NUM_VREFS] = {
2310 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2311 "", "Mic 80pc Bias", "Mic 100pc Bias"
2312};
2313
2314static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2315{
2316 unsigned int pincap;
2317
2318 pincap = snd_hda_query_pin_caps(codec, pin);
2319 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2320 /* filter out unusual vrefs */
2321 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2322 return pincap;
2323}
2324
2325/* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2326static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2327{
2328 unsigned int i, n = 0;
2329
2330 for (i = 0; i < NUM_VREFS; i++) {
2331 if (vref_caps & (1 << i)) {
2332 if (n == item_idx)
2333 return i;
2334 n++;
2335 }
2336 }
2337 return 0;
2338}
2339
2340/* convert back from the vref ctl index to the enum item index */
2341static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2342{
2343 unsigned int i, n = 0;
2344
2345 for (i = 0; i < NUM_VREFS; i++) {
2346 if (i == idx)
2347 return n;
2348 if (vref_caps & (1 << i))
2349 n++;
2350 }
2351 return 0;
2352}
2353
2354static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2355 struct snd_ctl_elem_info *uinfo)
2356{
2357 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2358 hda_nid_t nid = kcontrol->private_value;
2359 unsigned int vref_caps = get_vref_caps(codec, nid);
2360
2361 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2362 vref_texts);
2363 /* set the right text */
2364 strcpy(uinfo->value.enumerated.name,
2365 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2366 return 0;
2367}
2368
2369static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2370 struct snd_ctl_elem_value *ucontrol)
2371{
2372 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2373 hda_nid_t nid = kcontrol->private_value;
2374 unsigned int vref_caps = get_vref_caps(codec, nid);
2375 unsigned int idx;
2376
2377 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2378 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2379 return 0;
2380}
2381
2382static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2383 struct snd_ctl_elem_value *ucontrol)
2384{
2385 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2386 hda_nid_t nid = kcontrol->private_value;
2387 unsigned int vref_caps = get_vref_caps(codec, nid);
2388 unsigned int val, idx;
2389
2390 val = snd_hda_codec_get_pin_target(codec, nid);
2391 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2392 if (idx == ucontrol->value.enumerated.item[0])
2393 return 0;
2394
2395 val &= ~AC_PINCTL_VREFEN;
2396 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2397 snd_hda_set_pin_ctl_cache(codec, nid, val);
2398 return 1;
2399}
2400
2401static const struct snd_kcontrol_new in_jack_mode_enum = {
2402 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2403 .info = in_jack_mode_info,
2404 .get = in_jack_mode_get,
2405 .put = in_jack_mode_put,
2406};
2407
2408static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2409{
2410 struct hda_gen_spec *spec = codec->spec;
2411 unsigned int defcfg;
2412 struct snd_kcontrol_new *knew;
2413 char name[44];
2414
2415 /* no jack mode for fixed pins */
2416 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2417 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2418 return 0;
2419
2420 /* no multiple vref caps? */
2421 if (hweight32(get_vref_caps(codec, pin)) <= 1)
2422 return 0;
2423
2424 get_jack_mode_name(codec, pin, name, sizeof(name));
2425 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2426 if (!knew)
2427 return -ENOMEM;
2428 knew->private_value = pin;
2429 return 0;
2430}
2431
Takashi Iwai352f7f92012-12-19 12:52:06 +01002432
2433/*
2434 * Parse input paths
2435 */
2436
2437#ifdef CONFIG_PM
2438/* add the powersave loopback-list entry */
2439static void add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
2440{
2441 struct hda_amp_list *list;
2442
2443 if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
2444 return;
2445 list = spec->loopback_list + spec->num_loopbacks;
2446 list->nid = mix;
2447 list->dir = HDA_INPUT;
2448 list->idx = idx;
2449 spec->num_loopbacks++;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002450 spec->loopback.amplist = spec->loopback_list;
2451}
2452#else
Takashi Iwai352f7f92012-12-19 12:52:06 +01002453#define add_loopback_list(spec, mix, idx) /* NOP */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002454#endif
2455
Takashi Iwai352f7f92012-12-19 12:52:06 +01002456/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01002457static int new_analog_input(struct hda_codec *codec, int input_idx,
2458 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002459 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002461 struct hda_gen_spec *spec = codec->spec;
2462 struct nid_path *path;
2463 unsigned int val;
2464 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465
Takashi Iwai352f7f92012-12-19 12:52:06 +01002466 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
2467 !nid_has_mute(codec, mix_nid, HDA_INPUT))
2468 return 0; /* no need for analog loopback */
2469
Takashi Iwai3ca529d2013-01-07 17:25:08 +01002470 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002471 if (!path)
2472 return -EINVAL;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002473 print_nid_path("loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01002474 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002475
2476 idx = path->idx[path->depth - 1];
2477 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
2478 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2479 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002480 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002482 path->ctls[NID_PATH_VOL_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 }
2484
Takashi Iwai352f7f92012-12-19 12:52:06 +01002485 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
2486 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2487 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002488 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002490 path->ctls[NID_PATH_MUTE_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 }
2492
Takashi Iwai352f7f92012-12-19 12:52:06 +01002493 path->active = true;
2494 add_loopback_list(spec, mix_nid, idx);
2495 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496}
2497
Takashi Iwai352f7f92012-12-19 12:52:06 +01002498static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002500 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
2501 return (pincap & AC_PINCAP_IN) != 0;
2502}
2503
2504/* Parse the codec tree and retrieve ADCs */
2505static int fill_adc_nids(struct hda_codec *codec)
2506{
2507 struct hda_gen_spec *spec = codec->spec;
2508 hda_nid_t nid;
2509 hda_nid_t *adc_nids = spec->adc_nids;
2510 int max_nums = ARRAY_SIZE(spec->adc_nids);
2511 int i, nums = 0;
2512
2513 nid = codec->start_nid;
2514 for (i = 0; i < codec->num_nodes; i++, nid++) {
2515 unsigned int caps = get_wcaps(codec, nid);
2516 int type = get_wcaps_type(caps);
2517
2518 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2519 continue;
2520 adc_nids[nums] = nid;
2521 if (++nums >= max_nums)
2522 break;
2523 }
2524 spec->num_adc_nids = nums;
Takashi Iwai0ffd5342013-01-17 15:53:29 +01002525
2526 /* copy the detected ADCs to all_adcs[] */
2527 spec->num_all_adcs = nums;
2528 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
2529
Takashi Iwai352f7f92012-12-19 12:52:06 +01002530 return nums;
2531}
2532
2533/* filter out invalid adc_nids that don't give all active input pins;
2534 * if needed, check whether dynamic ADC-switching is available
2535 */
2536static int check_dyn_adc_switch(struct hda_codec *codec)
2537{
2538 struct hda_gen_spec *spec = codec->spec;
2539 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002540 unsigned int ok_bits;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002541 int i, n, nums;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002542
2543 again:
2544 nums = 0;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002545 ok_bits = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002546 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002547 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002548 if (!spec->input_paths[i][n])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002549 break;
2550 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002551 if (i >= imux->num_items) {
2552 ok_bits |= (1 << n);
2553 nums++;
2554 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002555 }
2556
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002557 if (!ok_bits) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002558 if (spec->shared_mic_hp) {
2559 spec->shared_mic_hp = 0;
2560 imux->num_items = 1;
2561 goto again;
2562 }
2563
2564 /* check whether ADC-switch is possible */
2565 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002566 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002567 if (spec->input_paths[i][n]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002568 spec->dyn_adc_idx[i] = n;
2569 break;
2570 }
2571 }
2572 }
2573
2574 snd_printdd("hda-codec: enabling ADC switching\n");
2575 spec->dyn_adc_switch = 1;
2576 } else if (nums != spec->num_adc_nids) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002577 /* shrink the invalid adcs and input paths */
2578 nums = 0;
2579 for (n = 0; n < spec->num_adc_nids; n++) {
2580 if (!(ok_bits & (1 << n)))
2581 continue;
2582 if (n != nums) {
2583 spec->adc_nids[nums] = spec->adc_nids[n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002584 for (i = 0; i < imux->num_items; i++) {
2585 invalidate_nid_path(codec,
2586 spec->input_paths[i][nums]);
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002587 spec->input_paths[i][nums] =
2588 spec->input_paths[i][n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002589 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002590 }
2591 nums++;
2592 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002593 spec->num_adc_nids = nums;
2594 }
2595
2596 if (imux->num_items == 1 || spec->shared_mic_hp) {
2597 snd_printdd("hda-codec: reducing to a single ADC\n");
2598 spec->num_adc_nids = 1; /* reduce to a single ADC */
2599 }
2600
2601 /* single index for individual volumes ctls */
2602 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
2603 spec->num_adc_nids = 1;
2604
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 return 0;
2606}
2607
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002608/* parse capture source paths from the given pin and create imux items */
2609static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
Takashi Iwai9dba2052013-01-18 10:01:15 +01002610 int cfg_idx, int num_adcs,
2611 const char *label, int anchor)
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002612{
2613 struct hda_gen_spec *spec = codec->spec;
2614 struct hda_input_mux *imux = &spec->input_mux;
2615 int imux_idx = imux->num_items;
2616 bool imux_added = false;
2617 int c;
2618
2619 for (c = 0; c < num_adcs; c++) {
2620 struct nid_path *path;
2621 hda_nid_t adc = spec->adc_nids[c];
2622
2623 if (!is_reachable_path(codec, pin, adc))
2624 continue;
2625 path = snd_hda_add_new_path(codec, pin, adc, anchor);
2626 if (!path)
2627 continue;
2628 print_nid_path("input", path);
2629 spec->input_paths[imux_idx][c] =
2630 snd_hda_get_path_idx(codec, path);
2631
2632 if (!imux_added) {
2633 spec->imux_pins[imux->num_items] = pin;
Takashi Iwai9dba2052013-01-18 10:01:15 +01002634 snd_hda_add_imux_item(imux, label, cfg_idx, NULL);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002635 imux_added = true;
2636 }
2637 }
2638
2639 return 0;
2640}
2641
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002643 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 */
Takashi Iwai9dba2052013-01-18 10:01:15 +01002645
Takashi Iwaic9700422013-01-18 10:17:30 +01002646/* fill the label for each input at first */
2647static int fill_input_pin_labels(struct hda_codec *codec)
2648{
2649 struct hda_gen_spec *spec = codec->spec;
2650 const struct auto_pin_cfg *cfg = &spec->autocfg;
2651 int i;
2652
2653 for (i = 0; i < cfg->num_inputs; i++) {
2654 hda_nid_t pin = cfg->inputs[i].pin;
2655 const char *label;
2656 int j, idx;
2657
2658 if (!is_input_pin(codec, pin))
2659 continue;
2660
2661 label = hda_get_autocfg_input_label(codec, cfg, i);
2662 idx = 0;
David Henningsson8e8db7f2013-01-18 15:43:02 +01002663 for (j = i - 1; j >= 0; j--) {
Takashi Iwaic9700422013-01-18 10:17:30 +01002664 if (spec->input_labels[j] &&
2665 !strcmp(spec->input_labels[j], label)) {
2666 idx = spec->input_label_idxs[j] + 1;
2667 break;
2668 }
2669 }
2670
2671 spec->input_labels[i] = label;
2672 spec->input_label_idxs[i] = idx;
2673 }
2674
2675 return 0;
2676}
2677
Takashi Iwai9dba2052013-01-18 10:01:15 +01002678#define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
2679
Takashi Iwai352f7f92012-12-19 12:52:06 +01002680static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002681{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002682 struct hda_gen_spec *spec = codec->spec;
2683 const struct auto_pin_cfg *cfg = &spec->autocfg;
2684 hda_nid_t mixer = spec->mixer_nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002685 int num_adcs;
Takashi Iwaic9700422013-01-18 10:17:30 +01002686 int i, err;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002687 unsigned int val;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002688
Takashi Iwai352f7f92012-12-19 12:52:06 +01002689 num_adcs = fill_adc_nids(codec);
2690 if (num_adcs < 0)
2691 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002692
Takashi Iwaic9700422013-01-18 10:17:30 +01002693 err = fill_input_pin_labels(codec);
2694 if (err < 0)
2695 return err;
2696
Takashi Iwai352f7f92012-12-19 12:52:06 +01002697 for (i = 0; i < cfg->num_inputs; i++) {
2698 hda_nid_t pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699
Takashi Iwai352f7f92012-12-19 12:52:06 +01002700 pin = cfg->inputs[i].pin;
2701 if (!is_input_pin(codec, pin))
2702 continue;
2703
Takashi Iwai2c12c302013-01-10 09:33:29 +01002704 val = PIN_IN;
2705 if (cfg->inputs[i].type == AUTO_PIN_MIC)
2706 val |= snd_hda_get_default_vref(codec, pin);
2707 set_pin_target(codec, pin, val, false);
2708
Takashi Iwai352f7f92012-12-19 12:52:06 +01002709 if (mixer) {
2710 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01002711 err = new_analog_input(codec, i, pin,
Takashi Iwaic9700422013-01-18 10:17:30 +01002712 spec->input_labels[i],
2713 spec->input_label_idxs[i],
2714 mixer);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002715 if (err < 0)
2716 return err;
2717 }
2718 }
2719
Takashi Iwaic9700422013-01-18 10:17:30 +01002720 err = parse_capture_source(codec, pin, i, num_adcs,
2721 spec->input_labels[i], -mixer);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002722 if (err < 0)
2723 return err;
Takashi Iwai294765582013-01-17 09:52:11 +01002724
2725 if (spec->add_in_jack_modes) {
2726 err = create_in_jack_mode(codec, pin);
2727 if (err < 0)
2728 return err;
2729 }
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002730 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002731
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002732 if (mixer && spec->add_stereo_mix_input) {
Takashi Iwai9dba2052013-01-18 10:01:15 +01002733 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002734 "Stereo Mix", 0);
2735 if (err < 0)
2736 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002737 }
2738
2739 return 0;
2740}
2741
2742
2743/*
2744 * input source mux
2745 */
2746
Takashi Iwaic697b712013-01-07 17:09:26 +01002747/* get the input path specified by the given adc and imux indices */
2748static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002749{
2750 struct hda_gen_spec *spec = codec->spec;
David Henningssonb56fa1e2013-01-16 11:45:35 +01002751 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
2752 snd_BUG();
2753 return NULL;
2754 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002755 if (spec->dyn_adc_switch)
2756 adc_idx = spec->dyn_adc_idx[imux_idx];
David Henningssond3d982f2013-01-18 15:43:01 +01002757 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
David Henningssonb56fa1e2013-01-16 11:45:35 +01002758 snd_BUG();
2759 return NULL;
2760 }
Takashi Iwaic697b712013-01-07 17:09:26 +01002761 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002762}
2763
2764static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2765 unsigned int idx);
2766
2767static int mux_enum_info(struct snd_kcontrol *kcontrol,
2768 struct snd_ctl_elem_info *uinfo)
2769{
2770 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2771 struct hda_gen_spec *spec = codec->spec;
2772 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
2773}
2774
2775static int mux_enum_get(struct snd_kcontrol *kcontrol,
2776 struct snd_ctl_elem_value *ucontrol)
2777{
2778 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2779 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai2a8d5392013-01-18 16:23:25 +01002780 /* the ctls are created at once with multiple counts */
2781 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002782
2783 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
2784 return 0;
2785}
2786
2787static int mux_enum_put(struct snd_kcontrol *kcontrol,
2788 struct snd_ctl_elem_value *ucontrol)
2789{
2790 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Takashi Iwai2a8d5392013-01-18 16:23:25 +01002791 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002792 return mux_select(codec, adc_idx,
2793 ucontrol->value.enumerated.item[0]);
2794}
2795
Takashi Iwai352f7f92012-12-19 12:52:06 +01002796static const struct snd_kcontrol_new cap_src_temp = {
2797 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2798 .name = "Input Source",
2799 .info = mux_enum_info,
2800 .get = mux_enum_get,
2801 .put = mux_enum_put,
2802};
2803
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002804/*
2805 * capture volume and capture switch ctls
2806 */
2807
Takashi Iwai352f7f92012-12-19 12:52:06 +01002808typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
2809 struct snd_ctl_elem_value *ucontrol);
2810
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002811/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002812static int cap_put_caller(struct snd_kcontrol *kcontrol,
2813 struct snd_ctl_elem_value *ucontrol,
2814 put_call_t func, int type)
2815{
2816 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2817 struct hda_gen_spec *spec = codec->spec;
2818 const struct hda_input_mux *imux;
2819 struct nid_path *path;
2820 int i, adc_idx, err = 0;
2821
2822 imux = &spec->input_mux;
David Henningssona053d1e2013-01-16 11:45:36 +01002823 adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002824 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002825 /* we use the cache-only update at first since multiple input paths
2826 * may shared the same amp; by updating only caches, the redundant
2827 * writes to hardware can be reduced.
2828 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002829 codec->cached_write = 1;
2830 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01002831 path = get_input_path(codec, adc_idx, i);
2832 if (!path || !path->ctls[type])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002833 continue;
2834 kcontrol->private_value = path->ctls[type];
2835 err = func(kcontrol, ucontrol);
2836 if (err < 0)
2837 goto error;
2838 }
2839 error:
2840 codec->cached_write = 0;
2841 mutex_unlock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002842 snd_hda_codec_flush_amp_cache(codec); /* flush the updates */
Takashi Iwaia836dbf2013-01-22 15:18:17 +01002843 snd_hda_codec_flush_cmd_cache(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002844 if (err >= 0 && spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01002845 spec->cap_sync_hook(codec, ucontrol);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002846 return err;
2847}
2848
2849/* capture volume ctl callbacks */
2850#define cap_vol_info snd_hda_mixer_amp_volume_info
2851#define cap_vol_get snd_hda_mixer_amp_volume_get
2852#define cap_vol_tlv snd_hda_mixer_amp_tlv
2853
2854static int cap_vol_put(struct snd_kcontrol *kcontrol,
2855 struct snd_ctl_elem_value *ucontrol)
2856{
2857 return cap_put_caller(kcontrol, ucontrol,
2858 snd_hda_mixer_amp_volume_put,
2859 NID_PATH_VOL_CTL);
2860}
2861
2862static const struct snd_kcontrol_new cap_vol_temp = {
2863 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2864 .name = "Capture Volume",
2865 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
2866 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2867 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
2868 .info = cap_vol_info,
2869 .get = cap_vol_get,
2870 .put = cap_vol_put,
2871 .tlv = { .c = cap_vol_tlv },
2872};
2873
2874/* capture switch ctl callbacks */
2875#define cap_sw_info snd_ctl_boolean_stereo_info
2876#define cap_sw_get snd_hda_mixer_amp_switch_get
2877
2878static int cap_sw_put(struct snd_kcontrol *kcontrol,
2879 struct snd_ctl_elem_value *ucontrol)
2880{
Takashi Iwaia90229e2013-01-18 14:10:00 +01002881 return cap_put_caller(kcontrol, ucontrol,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002882 snd_hda_mixer_amp_switch_put,
2883 NID_PATH_MUTE_CTL);
2884}
2885
2886static const struct snd_kcontrol_new cap_sw_temp = {
2887 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2888 .name = "Capture Switch",
2889 .info = cap_sw_info,
2890 .get = cap_sw_get,
2891 .put = cap_sw_put,
2892};
2893
2894static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
2895{
2896 hda_nid_t nid;
2897 int i, depth;
2898
2899 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
2900 for (depth = 0; depth < 3; depth++) {
2901 if (depth >= path->depth)
2902 return -EINVAL;
2903 i = path->depth - depth - 1;
2904 nid = path->path[i];
2905 if (!path->ctls[NID_PATH_VOL_CTL]) {
2906 if (nid_has_volume(codec, nid, HDA_OUTPUT))
2907 path->ctls[NID_PATH_VOL_CTL] =
2908 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2909 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
2910 int idx = path->idx[i];
2911 if (!depth && codec->single_adc_amp)
2912 idx = 0;
2913 path->ctls[NID_PATH_VOL_CTL] =
2914 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2915 }
2916 }
2917 if (!path->ctls[NID_PATH_MUTE_CTL]) {
2918 if (nid_has_mute(codec, nid, HDA_OUTPUT))
2919 path->ctls[NID_PATH_MUTE_CTL] =
2920 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2921 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
2922 int idx = path->idx[i];
2923 if (!depth && codec->single_adc_amp)
2924 idx = 0;
2925 path->ctls[NID_PATH_MUTE_CTL] =
2926 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2927 }
2928 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01002929 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 return 0;
2931}
2932
Takashi Iwai352f7f92012-12-19 12:52:06 +01002933static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002935 struct hda_gen_spec *spec = codec->spec;
2936 struct auto_pin_cfg *cfg = &spec->autocfg;
2937 unsigned int val;
2938 int i;
2939
2940 if (!spec->inv_dmic_split)
2941 return false;
2942 for (i = 0; i < cfg->num_inputs; i++) {
2943 if (cfg->inputs[i].pin != nid)
2944 continue;
2945 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2946 return false;
2947 val = snd_hda_codec_get_pincfg(codec, nid);
2948 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
2949 }
2950 return false;
2951}
2952
Takashi Iwaia90229e2013-01-18 14:10:00 +01002953/* capture switch put callback for a single control with hook call */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01002954static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
2955 struct snd_ctl_elem_value *ucontrol)
2956{
2957 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2958 struct hda_gen_spec *spec = codec->spec;
2959 int ret;
2960
2961 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2962 if (ret < 0)
2963 return ret;
2964
Takashi Iwaia90229e2013-01-18 14:10:00 +01002965 if (spec->cap_sync_hook)
2966 spec->cap_sync_hook(codec, ucontrol);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01002967
2968 return ret;
2969}
2970
Takashi Iwai352f7f92012-12-19 12:52:06 +01002971static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
2972 int idx, bool is_switch, unsigned int ctl,
2973 bool inv_dmic)
2974{
2975 struct hda_gen_spec *spec = codec->spec;
2976 char tmpname[44];
2977 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
2978 const char *sfx = is_switch ? "Switch" : "Volume";
2979 unsigned int chs = inv_dmic ? 1 : 3;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01002980 struct snd_kcontrol_new *knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002981
2982 if (!ctl)
2983 return 0;
2984
2985 if (label)
2986 snprintf(tmpname, sizeof(tmpname),
2987 "%s Capture %s", label, sfx);
2988 else
2989 snprintf(tmpname, sizeof(tmpname),
2990 "Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01002991 knew = add_control(spec, type, tmpname, idx,
2992 amp_val_replace_channels(ctl, chs));
2993 if (!knew)
2994 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01002995 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01002996 knew->put = cap_single_sw_put;
2997 if (!inv_dmic)
2998 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002999
3000 /* Make independent right kcontrol */
3001 if (label)
3002 snprintf(tmpname, sizeof(tmpname),
3003 "Inverted %s Capture %s", label, sfx);
3004 else
3005 snprintf(tmpname, sizeof(tmpname),
3006 "Inverted Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003007 knew = add_control(spec, type, tmpname, idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003008 amp_val_replace_channels(ctl, 2));
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003009 if (!knew)
3010 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003011 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003012 knew->put = cap_single_sw_put;
3013 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003014}
3015
3016/* create single (and simple) capture volume and switch controls */
3017static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3018 unsigned int vol_ctl, unsigned int sw_ctl,
3019 bool inv_dmic)
3020{
3021 int err;
3022 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3023 if (err < 0)
3024 return err;
3025 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3026 if (err < 0)
3027 return err;
3028 return 0;
3029}
3030
3031/* create bound capture volume and switch controls */
3032static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3033 unsigned int vol_ctl, unsigned int sw_ctl)
3034{
3035 struct hda_gen_spec *spec = codec->spec;
3036 struct snd_kcontrol_new *knew;
3037
3038 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003039 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003040 if (!knew)
3041 return -ENOMEM;
3042 knew->index = idx;
3043 knew->private_value = vol_ctl;
3044 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3045 }
3046 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003047 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003048 if (!knew)
3049 return -ENOMEM;
3050 knew->index = idx;
3051 knew->private_value = sw_ctl;
3052 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3053 }
3054 return 0;
3055}
3056
3057/* return the vol ctl when used first in the imux list */
3058static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3059{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003060 struct nid_path *path;
3061 unsigned int ctl;
3062 int i;
3063
Takashi Iwaic697b712013-01-07 17:09:26 +01003064 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003065 if (!path)
3066 return 0;
3067 ctl = path->ctls[type];
3068 if (!ctl)
3069 return 0;
3070 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003071 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003072 if (path && path->ctls[type] == ctl)
3073 return 0;
3074 }
3075 return ctl;
3076}
3077
3078/* create individual capture volume and switch controls per input */
3079static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3080{
3081 struct hda_gen_spec *spec = codec->spec;
3082 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaic9700422013-01-18 10:17:30 +01003083 int i, err, type;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003084
3085 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003086 bool inv_dmic;
Takashi Iwaic9700422013-01-18 10:17:30 +01003087 int idx;
Takashi Iwai9dba2052013-01-18 10:01:15 +01003088
Takashi Iwaic9700422013-01-18 10:17:30 +01003089 idx = imux->items[i].index;
3090 if (idx >= spec->autocfg.num_inputs)
Takashi Iwai9dba2052013-01-18 10:01:15 +01003091 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003092 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3093
3094 for (type = 0; type < 2; type++) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003095 err = add_single_cap_ctl(codec,
3096 spec->input_labels[idx],
3097 spec->input_label_idxs[idx],
3098 type,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003099 get_first_cap_ctl(codec, i, type),
3100 inv_dmic);
3101 if (err < 0)
3102 return err;
3103 }
3104 }
3105 return 0;
3106}
3107
3108static int create_capture_mixers(struct hda_codec *codec)
3109{
3110 struct hda_gen_spec *spec = codec->spec;
3111 struct hda_input_mux *imux = &spec->input_mux;
3112 int i, n, nums, err;
3113
3114 if (spec->dyn_adc_switch)
3115 nums = 1;
3116 else
3117 nums = spec->num_adc_nids;
3118
3119 if (!spec->auto_mic && imux->num_items > 1) {
3120 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01003121 const char *name;
3122 name = nums > 1 ? "Input Source" : "Capture Source";
3123 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003124 if (!knew)
3125 return -ENOMEM;
3126 knew->count = nums;
3127 }
3128
3129 for (n = 0; n < nums; n++) {
3130 bool multi = false;
David Henningsson99a55922013-01-16 15:58:44 +01003131 bool multi_cap_vol = spec->multi_cap_vol;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003132 bool inv_dmic = false;
3133 int vol, sw;
3134
3135 vol = sw = 0;
3136 for (i = 0; i < imux->num_items; i++) {
3137 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01003138 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003139 if (!path)
3140 continue;
3141 parse_capvol_in_path(codec, path);
3142 if (!vol)
3143 vol = path->ctls[NID_PATH_VOL_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003144 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003145 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003146 if (!same_amp_caps(codec, vol,
3147 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3148 multi_cap_vol = true;
3149 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003150 if (!sw)
3151 sw = path->ctls[NID_PATH_MUTE_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003152 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003153 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003154 if (!same_amp_caps(codec, sw,
3155 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3156 multi_cap_vol = true;
3157 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003158 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3159 inv_dmic = true;
3160 }
3161
3162 if (!multi)
3163 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3164 inv_dmic);
David Henningsson99a55922013-01-16 15:58:44 +01003165 else if (!multi_cap_vol)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003166 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3167 else
3168 err = create_multi_cap_vol_ctl(codec);
3169 if (err < 0)
3170 return err;
3171 }
3172
3173 return 0;
3174}
3175
3176/*
3177 * add mic boosts if needed
3178 */
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003179
3180/* check whether the given amp is feasible as a boost volume */
3181static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3182 int dir, int idx)
3183{
3184 unsigned int step;
3185
3186 if (!nid_has_volume(codec, nid, dir) ||
3187 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3188 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3189 return false;
3190
3191 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3192 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3193 if (step < 0x20)
3194 return false;
3195 return true;
3196}
3197
3198/* look for a boost amp in a widget close to the pin */
3199static unsigned int look_for_boost_amp(struct hda_codec *codec,
3200 struct nid_path *path)
3201{
3202 unsigned int val = 0;
3203 hda_nid_t nid;
3204 int depth;
3205
3206 for (depth = 0; depth < 3; depth++) {
3207 if (depth >= path->depth - 1)
3208 break;
3209 nid = path->path[depth];
3210 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3211 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3212 break;
3213 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3214 path->idx[depth])) {
3215 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3216 HDA_INPUT);
3217 break;
3218 }
3219 }
3220
3221 return val;
3222}
3223
Takashi Iwai352f7f92012-12-19 12:52:06 +01003224static int parse_mic_boost(struct hda_codec *codec)
3225{
3226 struct hda_gen_spec *spec = codec->spec;
3227 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003228 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003229 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003230
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003231 if (!spec->num_adc_nids)
3232 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003233
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003234 for (i = 0; i < imux->num_items; i++) {
3235 struct nid_path *path;
3236 unsigned int val;
3237 int idx;
3238 char boost_label[44];
David Henningsson02aba552013-01-16 15:58:43 +01003239
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003240 idx = imux->items[i].index;
3241 if (idx >= imux->num_items)
3242 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003243
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003244 /* check only line-in and mic pins */
Takashi Iwai1799cdd52013-01-18 14:37:16 +01003245 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003246 continue;
3247
3248 path = get_input_path(codec, 0, i);
3249 if (!path)
3250 continue;
3251
3252 val = look_for_boost_amp(codec, path);
3253 if (!val)
3254 continue;
3255
3256 /* create a boost control */
3257 snprintf(boost_label, sizeof(boost_label),
3258 "%s Boost Volume", spec->input_labels[idx]);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003259 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3260 spec->input_label_idxs[idx], val))
3261 return -ENOMEM;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003262
3263 path->ctls[NID_PATH_BOOST_CTL] = val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003264 }
3265 return 0;
3266}
3267
3268/*
3269 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3270 */
3271static void parse_digital(struct hda_codec *codec)
3272{
3273 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003274 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003275 int i, nums;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003276 hda_nid_t dig_nid, pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003277
3278 /* support multiple SPDIFs; the secondary is set up as a slave */
3279 nums = 0;
3280 for (i = 0; i < spec->autocfg.dig_outs; i++) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003281 pin = spec->autocfg.dig_out_pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003282 dig_nid = look_for_dac(codec, pin, true);
3283 if (!dig_nid)
3284 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003285 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003286 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003287 continue;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003288 print_nid_path("digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01003289 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01003290 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003291 set_pin_target(codec, pin, PIN_OUT, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003292 if (!nums) {
3293 spec->multiout.dig_out_nid = dig_nid;
3294 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3295 } else {
3296 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3297 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
3298 break;
3299 spec->slave_dig_outs[nums - 1] = dig_nid;
3300 }
3301 nums++;
3302 }
3303
3304 if (spec->autocfg.dig_in_pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003305 pin = spec->autocfg.dig_in_pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003306 dig_nid = codec->start_nid;
3307 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003308 unsigned int wcaps = get_wcaps(codec, dig_nid);
3309 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3310 continue;
3311 if (!(wcaps & AC_WCAP_DIGITAL))
3312 continue;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003313 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003314 if (path) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003315 print_nid_path("digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003316 path->active = true;
3317 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003318 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003319 set_pin_target(codec, pin, PIN_IN, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003320 break;
3321 }
3322 }
3323 }
3324}
3325
3326
3327/*
3328 * input MUX handling
3329 */
3330
3331static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3332
3333/* select the given imux item; either unmute exclusively or select the route */
3334static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3335 unsigned int idx)
3336{
3337 struct hda_gen_spec *spec = codec->spec;
3338 const struct hda_input_mux *imux;
3339 struct nid_path *path;
3340
3341 imux = &spec->input_mux;
3342 if (!imux->num_items)
3343 return 0;
3344
3345 if (idx >= imux->num_items)
3346 idx = imux->num_items - 1;
3347 if (spec->cur_mux[adc_idx] == idx)
3348 return 0;
3349
Takashi Iwaic697b712013-01-07 17:09:26 +01003350 path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003351 if (!path)
3352 return 0;
3353 if (path->active)
3354 snd_hda_activate_path(codec, path, false, false);
3355
3356 spec->cur_mux[adc_idx] = idx;
3357
3358 if (spec->shared_mic_hp)
3359 update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
3360
3361 if (spec->dyn_adc_switch)
3362 dyn_adc_pcm_resetup(codec, idx);
3363
Takashi Iwaic697b712013-01-07 17:09:26 +01003364 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003365 if (!path)
3366 return 0;
3367 if (path->active)
3368 return 0;
3369 snd_hda_activate_path(codec, path, true, false);
3370 if (spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01003371 spec->cap_sync_hook(codec, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003372 return 1;
3373}
3374
3375
3376/*
3377 * Jack detections for HP auto-mute and mic-switch
3378 */
3379
3380/* check each pin in the given array; returns true if any of them is plugged */
3381static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
3382{
3383 int i, present = 0;
3384
3385 for (i = 0; i < num_pins; i++) {
3386 hda_nid_t nid = pins[i];
3387 if (!nid)
3388 break;
Takashi Iwai0b4df932013-01-10 09:45:13 +01003389 /* don't detect pins retasked as inputs */
3390 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
3391 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003392 present |= snd_hda_jack_detect(codec, nid);
3393 }
3394 return present;
3395}
3396
3397/* standard HP/line-out auto-mute helper */
3398static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
Takashi Iwai2c12c302013-01-10 09:33:29 +01003399 bool mute)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003400{
3401 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003402 int i;
3403
3404 for (i = 0; i < num_pins; i++) {
3405 hda_nid_t nid = pins[i];
3406 unsigned int val;
3407 if (!nid)
3408 break;
3409 /* don't reset VREF value in case it's controlling
3410 * the amp (see alc861_fixup_asus_amp_vref_0f())
3411 */
Takashi Iwai2c12c302013-01-10 09:33:29 +01003412 if (spec->keep_vref_in_automute)
3413 val = snd_hda_codec_get_pin_target(codec, nid) & ~PIN_HP;
3414 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01003415 val = 0;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003416 if (!mute)
3417 val |= snd_hda_codec_get_pin_target(codec, nid);
3418 /* here we call update_pin_ctl() so that the pinctl is changed
3419 * without changing the pinctl target value;
3420 * the original target value will be still referred at the
3421 * init / resume again
3422 */
3423 update_pin_ctl(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01003424 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003425 }
3426}
3427
3428/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003429void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003430{
3431 struct hda_gen_spec *spec = codec->spec;
3432 int on;
3433
3434 /* Control HP pins/amps depending on master_mute state;
3435 * in general, HP pins/amps control should be enabled in all cases,
3436 * but currently set only for master_mute, just to be safe
3437 */
3438 if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
3439 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003440 spec->autocfg.hp_pins, spec->master_mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003441
3442 if (!spec->automute_speaker)
3443 on = 0;
3444 else
3445 on = spec->hp_jack_present | spec->line_jack_present;
3446 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003447 spec->speaker_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003448 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003449 spec->autocfg.speaker_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003450
3451 /* toggle line-out mutes if needed, too */
3452 /* if LO is a copy of either HP or Speaker, don't need to handle it */
3453 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
3454 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
3455 return;
3456 if (!spec->automute_lo)
3457 on = 0;
3458 else
3459 on = spec->hp_jack_present;
3460 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003461 spec->line_out_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003462 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003463 spec->autocfg.line_out_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003464}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003465EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003466
3467static void call_update_outputs(struct hda_codec *codec)
3468{
3469 struct hda_gen_spec *spec = codec->spec;
3470 if (spec->automute_hook)
3471 spec->automute_hook(codec);
3472 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01003473 snd_hda_gen_update_outputs(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003474}
3475
3476/* standard HP-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003477void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003478{
3479 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai92603c52013-01-22 07:46:31 +01003480 hda_nid_t *pins = spec->autocfg.hp_pins;
3481 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003482
Takashi Iwai92603c52013-01-22 07:46:31 +01003483 /* No detection for the first HP jack during indep-HP mode */
3484 if (spec->indep_hp_enabled) {
3485 pins++;
3486 num_pins--;
3487 }
3488
3489 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003490 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
3491 return;
3492 call_update_outputs(codec);
3493}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003494EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003495
3496/* standard line-out-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003497void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003498{
3499 struct hda_gen_spec *spec = codec->spec;
3500
3501 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3502 return;
3503 /* check LO jack only when it's different from HP */
3504 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
3505 return;
3506
3507 spec->line_jack_present =
3508 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3509 spec->autocfg.line_out_pins);
3510 if (!spec->automute_speaker || !spec->detect_lo)
3511 return;
3512 call_update_outputs(codec);
3513}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003514EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003515
3516/* standard mic auto-switch helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003517void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003518{
3519 struct hda_gen_spec *spec = codec->spec;
3520 int i;
3521
3522 if (!spec->auto_mic)
3523 return;
3524
3525 for (i = spec->am_num_entries - 1; i > 0; i--) {
Takashi Iwai0b4df932013-01-10 09:45:13 +01003526 hda_nid_t pin = spec->am_entry[i].pin;
3527 /* don't detect pins retasked as outputs */
3528 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
3529 continue;
3530 if (snd_hda_jack_detect(codec, pin)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003531 mux_select(codec, 0, spec->am_entry[i].idx);
3532 return;
3533 }
3534 }
3535 mux_select(codec, 0, spec->am_entry[0].idx);
3536}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003537EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003538
Takashi Iwaia5cc2502013-01-16 18:08:55 +01003539/* update jack retasking */
3540static void update_automute_all(struct hda_codec *codec)
3541{
3542 struct hda_gen_spec *spec = codec->spec;
3543
3544 if (spec->hp_automute_hook)
3545 spec->hp_automute_hook(codec, NULL);
3546 else
3547 snd_hda_gen_hp_automute(codec, NULL);
3548 if (spec->line_automute_hook)
3549 spec->line_automute_hook(codec, NULL);
3550 else
3551 snd_hda_gen_line_automute(codec, NULL);
3552 if (spec->mic_autoswitch_hook)
3553 spec->mic_autoswitch_hook(codec, NULL);
3554 else
3555 snd_hda_gen_mic_autoswitch(codec, NULL);
3556}
3557
Takashi Iwai352f7f92012-12-19 12:52:06 +01003558/*
3559 * Auto-Mute mode mixer enum support
3560 */
3561static int automute_mode_info(struct snd_kcontrol *kcontrol,
3562 struct snd_ctl_elem_info *uinfo)
3563{
3564 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3565 struct hda_gen_spec *spec = codec->spec;
3566 static const char * const texts3[] = {
3567 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02003568 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569
Takashi Iwai352f7f92012-12-19 12:52:06 +01003570 if (spec->automute_speaker_possible && spec->automute_lo_possible)
3571 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
3572 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
3573}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574
Takashi Iwai352f7f92012-12-19 12:52:06 +01003575static int automute_mode_get(struct snd_kcontrol *kcontrol,
3576 struct snd_ctl_elem_value *ucontrol)
3577{
3578 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3579 struct hda_gen_spec *spec = codec->spec;
3580 unsigned int val = 0;
3581 if (spec->automute_speaker)
3582 val++;
3583 if (spec->automute_lo)
3584 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003585
Takashi Iwai352f7f92012-12-19 12:52:06 +01003586 ucontrol->value.enumerated.item[0] = val;
3587 return 0;
3588}
3589
3590static int automute_mode_put(struct snd_kcontrol *kcontrol,
3591 struct snd_ctl_elem_value *ucontrol)
3592{
3593 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3594 struct hda_gen_spec *spec = codec->spec;
3595
3596 switch (ucontrol->value.enumerated.item[0]) {
3597 case 0:
3598 if (!spec->automute_speaker && !spec->automute_lo)
3599 return 0;
3600 spec->automute_speaker = 0;
3601 spec->automute_lo = 0;
3602 break;
3603 case 1:
3604 if (spec->automute_speaker_possible) {
3605 if (!spec->automute_lo && spec->automute_speaker)
3606 return 0;
3607 spec->automute_speaker = 1;
3608 spec->automute_lo = 0;
3609 } else if (spec->automute_lo_possible) {
3610 if (spec->automute_lo)
3611 return 0;
3612 spec->automute_lo = 1;
3613 } else
3614 return -EINVAL;
3615 break;
3616 case 2:
3617 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
3618 return -EINVAL;
3619 if (spec->automute_speaker && spec->automute_lo)
3620 return 0;
3621 spec->automute_speaker = 1;
3622 spec->automute_lo = 1;
3623 break;
3624 default:
3625 return -EINVAL;
3626 }
3627 call_update_outputs(codec);
3628 return 1;
3629}
3630
3631static const struct snd_kcontrol_new automute_mode_enum = {
3632 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3633 .name = "Auto-Mute Mode",
3634 .info = automute_mode_info,
3635 .get = automute_mode_get,
3636 .put = automute_mode_put,
3637};
3638
3639static int add_automute_mode_enum(struct hda_codec *codec)
3640{
3641 struct hda_gen_spec *spec = codec->spec;
3642
Takashi Iwai12c93df2012-12-19 14:38:33 +01003643 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01003644 return -ENOMEM;
3645 return 0;
3646}
3647
3648/*
3649 * Check the availability of HP/line-out auto-mute;
3650 * Set up appropriately if really supported
3651 */
3652static int check_auto_mute_availability(struct hda_codec *codec)
3653{
3654 struct hda_gen_spec *spec = codec->spec;
3655 struct auto_pin_cfg *cfg = &spec->autocfg;
3656 int present = 0;
3657 int i, err;
3658
Takashi Iwaif72706b2013-01-16 18:20:07 +01003659 if (spec->suppress_auto_mute)
3660 return 0;
3661
Takashi Iwai352f7f92012-12-19 12:52:06 +01003662 if (cfg->hp_pins[0])
3663 present++;
3664 if (cfg->line_out_pins[0])
3665 present++;
3666 if (cfg->speaker_pins[0])
3667 present++;
3668 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02003669 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003670
3671 if (!cfg->speaker_pins[0] &&
3672 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
3673 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3674 sizeof(cfg->speaker_pins));
3675 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677
Takashi Iwai352f7f92012-12-19 12:52:06 +01003678 if (!cfg->hp_pins[0] &&
3679 cfg->line_out_type == AUTO_PIN_HP_OUT) {
3680 memcpy(cfg->hp_pins, cfg->line_out_pins,
3681 sizeof(cfg->hp_pins));
3682 cfg->hp_outs = cfg->line_outs;
3683 }
3684
3685 for (i = 0; i < cfg->hp_outs; i++) {
3686 hda_nid_t nid = cfg->hp_pins[i];
3687 if (!is_jack_detectable(codec, nid))
3688 continue;
3689 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
3690 nid);
3691 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003692 spec->hp_automute_hook ?
3693 spec->hp_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003694 snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003695 spec->detect_hp = 1;
3696 }
3697
3698 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
3699 if (cfg->speaker_outs)
3700 for (i = 0; i < cfg->line_outs; i++) {
3701 hda_nid_t nid = cfg->line_out_pins[i];
3702 if (!is_jack_detectable(codec, nid))
3703 continue;
3704 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
3705 snd_hda_jack_detect_enable_callback(codec, nid,
3706 HDA_GEN_FRONT_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003707 spec->line_automute_hook ?
3708 spec->line_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003709 snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003710 spec->detect_lo = 1;
3711 }
3712 spec->automute_lo_possible = spec->detect_hp;
3713 }
3714
3715 spec->automute_speaker_possible = cfg->speaker_outs &&
3716 (spec->detect_hp || spec->detect_lo);
3717
3718 spec->automute_lo = spec->automute_lo_possible;
3719 spec->automute_speaker = spec->automute_speaker_possible;
3720
3721 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
3722 /* create a control for automute mode */
3723 err = add_automute_mode_enum(codec);
3724 if (err < 0)
3725 return err;
3726 }
3727 return 0;
3728}
3729
Takashi Iwai352f7f92012-12-19 12:52:06 +01003730/* check whether all auto-mic pins are valid; setup indices if OK */
3731static bool auto_mic_check_imux(struct hda_codec *codec)
3732{
3733 struct hda_gen_spec *spec = codec->spec;
3734 const struct hda_input_mux *imux;
3735 int i;
3736
3737 imux = &spec->input_mux;
3738 for (i = 0; i < spec->am_num_entries; i++) {
3739 spec->am_entry[i].idx =
3740 find_idx_in_nid_list(spec->am_entry[i].pin,
3741 spec->imux_pins, imux->num_items);
3742 if (spec->am_entry[i].idx < 0)
3743 return false; /* no corresponding imux */
3744 }
3745
3746 /* we don't need the jack detection for the first pin */
3747 for (i = 1; i < spec->am_num_entries; i++)
3748 snd_hda_jack_detect_enable_callback(codec,
3749 spec->am_entry[i].pin,
3750 HDA_GEN_MIC_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003751 spec->mic_autoswitch_hook ?
3752 spec->mic_autoswitch_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003753 snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003754 return true;
3755}
3756
3757static int compare_attr(const void *ap, const void *bp)
3758{
3759 const struct automic_entry *a = ap;
3760 const struct automic_entry *b = bp;
3761 return (int)(a->attr - b->attr);
3762}
3763
3764/*
3765 * Check the availability of auto-mic switch;
3766 * Set up if really supported
3767 */
3768static int check_auto_mic_availability(struct hda_codec *codec)
3769{
3770 struct hda_gen_spec *spec = codec->spec;
3771 struct auto_pin_cfg *cfg = &spec->autocfg;
3772 unsigned int types;
3773 int i, num_pins;
3774
Takashi Iwaid12daf62013-01-07 16:32:11 +01003775 if (spec->suppress_auto_mic)
3776 return 0;
3777
Takashi Iwai352f7f92012-12-19 12:52:06 +01003778 types = 0;
3779 num_pins = 0;
3780 for (i = 0; i < cfg->num_inputs; i++) {
3781 hda_nid_t nid = cfg->inputs[i].pin;
3782 unsigned int attr;
3783 attr = snd_hda_codec_get_pincfg(codec, nid);
3784 attr = snd_hda_get_input_pin_attr(attr);
3785 if (types & (1 << attr))
3786 return 0; /* already occupied */
3787 switch (attr) {
3788 case INPUT_PIN_ATTR_INT:
3789 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3790 return 0; /* invalid type */
3791 break;
3792 case INPUT_PIN_ATTR_UNUSED:
3793 return 0; /* invalid entry */
3794 default:
3795 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
3796 return 0; /* invalid type */
3797 if (!spec->line_in_auto_switch &&
3798 cfg->inputs[i].type != AUTO_PIN_MIC)
3799 return 0; /* only mic is allowed */
3800 if (!is_jack_detectable(codec, nid))
3801 return 0; /* no unsol support */
3802 break;
3803 }
3804 if (num_pins >= MAX_AUTO_MIC_PINS)
3805 return 0;
3806 types |= (1 << attr);
3807 spec->am_entry[num_pins].pin = nid;
3808 spec->am_entry[num_pins].attr = attr;
3809 num_pins++;
3810 }
3811
3812 if (num_pins < 2)
3813 return 0;
3814
3815 spec->am_num_entries = num_pins;
3816 /* sort the am_entry in the order of attr so that the pin with a
3817 * higher attr will be selected when the jack is plugged.
3818 */
3819 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
3820 compare_attr, NULL);
3821
3822 if (!auto_mic_check_imux(codec))
3823 return 0;
3824
3825 spec->auto_mic = 1;
3826 spec->num_adc_nids = 1;
3827 spec->cur_mux[0] = spec->am_entry[0].idx;
3828 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
3829 spec->am_entry[0].pin,
3830 spec->am_entry[1].pin,
3831 spec->am_entry[2].pin);
3832
3833 return 0;
3834}
3835
3836
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003837/*
3838 * Parse the given BIOS configuration and set up the hda_gen_spec
3839 *
3840 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003841 * or a negative error code
3842 */
3843int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003844 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003845{
3846 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003847 int err;
3848
Takashi Iwai1c70a582013-01-11 17:48:22 +01003849 parse_user_hints(codec);
3850
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003851 if (cfg != &spec->autocfg) {
3852 spec->autocfg = *cfg;
3853 cfg = &spec->autocfg;
3854 }
3855
David Henningsson6fc4cb92013-01-16 15:58:45 +01003856 fill_all_dac_nids(codec);
3857
Takashi Iwai352f7f92012-12-19 12:52:06 +01003858 if (!cfg->line_outs) {
3859 if (cfg->dig_outs || cfg->dig_in_pin) {
3860 spec->multiout.max_channels = 2;
3861 spec->no_analog = 1;
3862 goto dig_only;
3863 }
3864 return 0; /* can't find valid BIOS pin config */
3865 }
3866
3867 if (!spec->no_primary_hp &&
3868 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
3869 cfg->line_outs <= cfg->hp_outs) {
3870 /* use HP as primary out */
3871 cfg->speaker_outs = cfg->line_outs;
3872 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3873 sizeof(cfg->speaker_pins));
3874 cfg->line_outs = cfg->hp_outs;
3875 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
3876 cfg->hp_outs = 0;
3877 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3878 cfg->line_out_type = AUTO_PIN_HP_OUT;
3879 }
3880
3881 err = parse_output_paths(codec);
3882 if (err < 0)
3883 return err;
3884 err = create_multi_channel_mode(codec);
3885 if (err < 0)
3886 return err;
3887 err = create_multi_out_ctls(codec, cfg);
3888 if (err < 0)
3889 return err;
3890 err = create_hp_out_ctls(codec);
3891 if (err < 0)
3892 return err;
3893 err = create_speaker_out_ctls(codec);
3894 if (err < 0)
3895 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003896 err = create_indep_hp_ctls(codec);
3897 if (err < 0)
3898 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01003899 err = create_loopback_mixing_ctl(codec);
3900 if (err < 0)
3901 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003902 err = create_shared_input(codec);
3903 if (err < 0)
3904 return err;
3905 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003906 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02003907 return err;
3908
Takashi Iwaia07a9492013-01-07 16:44:06 +01003909 spec->const_channel_count = spec->ext_channel_count;
3910 /* check the multiple speaker and headphone pins */
3911 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
3912 spec->const_channel_count = max(spec->const_channel_count,
3913 cfg->speaker_outs * 2);
3914 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
3915 spec->const_channel_count = max(spec->const_channel_count,
3916 cfg->hp_outs * 2);
3917 spec->multiout.max_channels = max(spec->ext_channel_count,
3918 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003919
3920 err = check_auto_mute_availability(codec);
3921 if (err < 0)
3922 return err;
3923
3924 err = check_dyn_adc_switch(codec);
3925 if (err < 0)
3926 return err;
3927
3928 if (!spec->shared_mic_hp) {
3929 err = check_auto_mic_availability(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003930 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932 }
Takashi Iwai071c73a2006-08-23 18:34:06 +02003933
Takashi Iwai352f7f92012-12-19 12:52:06 +01003934 err = create_capture_mixers(codec);
3935 if (err < 0)
3936 return err;
3937
3938 err = parse_mic_boost(codec);
3939 if (err < 0)
3940 return err;
3941
Takashi Iwai978e77e2013-01-10 16:57:58 +01003942 if (spec->add_out_jack_modes) {
3943 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
3944 err = create_out_jack_modes(codec, cfg->line_outs,
3945 cfg->line_out_pins);
3946 if (err < 0)
3947 return err;
3948 }
3949 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
3950 err = create_out_jack_modes(codec, cfg->hp_outs,
3951 cfg->hp_pins);
3952 if (err < 0)
3953 return err;
3954 }
3955 }
3956
Takashi Iwai352f7f92012-12-19 12:52:06 +01003957 dig_only:
3958 parse_digital(codec);
3959
3960 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003962EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963
3964
3965/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003966 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003968
3969/* slave controls for virtual master */
3970static const char * const slave_pfxs[] = {
3971 "Front", "Surround", "Center", "LFE", "Side",
3972 "Headphone", "Speaker", "Mono", "Line Out",
3973 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01003974 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
3975 "Headphone Front", "Headphone Surround", "Headphone CLFE",
3976 "Headphone Side",
Takashi Iwai352f7f92012-12-19 12:52:06 +01003977 NULL,
3978};
3979
3980int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003982 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984
Takashi Iwai36502d02012-12-19 15:15:10 +01003985 if (spec->kctls.used) {
3986 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
3987 if (err < 0)
3988 return err;
3989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990
Takashi Iwai352f7f92012-12-19 12:52:06 +01003991 if (spec->multiout.dig_out_nid) {
3992 err = snd_hda_create_dig_out_ctls(codec,
3993 spec->multiout.dig_out_nid,
3994 spec->multiout.dig_out_nid,
3995 spec->pcm_rec[1].pcm_type);
3996 if (err < 0)
3997 return err;
3998 if (!spec->no_analog) {
3999 err = snd_hda_create_spdif_share_sw(codec,
4000 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 if (err < 0)
4002 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004003 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 }
4005 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004006 if (spec->dig_in_nid) {
4007 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
4008 if (err < 0)
4009 return err;
4010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011
Takashi Iwai352f7f92012-12-19 12:52:06 +01004012 /* if we have no master control, let's create it */
4013 if (!spec->no_analog &&
4014 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004015 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01004016 spec->vmaster_tlv, slave_pfxs,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004017 "Playback Volume");
4018 if (err < 0)
4019 return err;
4020 }
4021 if (!spec->no_analog &&
4022 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
4023 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
4024 NULL, slave_pfxs,
4025 "Playback Switch",
4026 true, &spec->vmaster_mute.sw_kctl);
4027 if (err < 0)
4028 return err;
4029 if (spec->vmaster_mute.hook)
Takashi Iwaifd25a972012-12-20 14:57:18 +01004030 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
4031 spec->vmaster_mute_enum);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033
Takashi Iwai352f7f92012-12-19 12:52:06 +01004034 free_kctls(spec); /* no longer needed */
4035
4036 if (spec->shared_mic_hp) {
4037 int err;
4038 int nid = spec->autocfg.inputs[1].pin;
4039 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
4040 if (err < 0)
4041 return err;
4042 err = snd_hda_jack_detect_enable(codec, nid, 0);
4043 if (err < 0)
4044 return err;
4045 }
4046
4047 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
4048 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 return err;
4050
4051 return 0;
4052}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004053EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
4054
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055
4056/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004057 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004060static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
4061 struct hda_codec *codec,
4062 struct snd_pcm_substream *substream,
4063 int action)
4064{
4065 struct hda_gen_spec *spec = codec->spec;
4066 if (spec->pcm_playback_hook)
4067 spec->pcm_playback_hook(hinfo, codec, substream, action);
4068}
4069
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004070static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
4071 struct hda_codec *codec,
4072 struct snd_pcm_substream *substream,
4073 int action)
4074{
4075 struct hda_gen_spec *spec = codec->spec;
4076 if (spec->pcm_capture_hook)
4077 spec->pcm_capture_hook(hinfo, codec, substream, action);
4078}
4079
Takashi Iwai352f7f92012-12-19 12:52:06 +01004080/*
4081 * Analog playback callbacks
4082 */
4083static int playback_pcm_open(struct hda_pcm_stream *hinfo,
4084 struct hda_codec *codec,
4085 struct snd_pcm_substream *substream)
4086{
4087 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004088 int err;
4089
4090 mutex_lock(&spec->pcm_mutex);
4091 err = snd_hda_multi_out_analog_open(codec,
4092 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004093 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004094 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004095 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004096 call_pcm_playback_hook(hinfo, codec, substream,
4097 HDA_GEN_PCM_ACT_OPEN);
4098 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004099 mutex_unlock(&spec->pcm_mutex);
4100 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004101}
4102
4103static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01004104 struct hda_codec *codec,
4105 unsigned int stream_tag,
4106 unsigned int format,
4107 struct snd_pcm_substream *substream)
4108{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004109 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004110 int err;
4111
4112 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
4113 stream_tag, format, substream);
4114 if (!err)
4115 call_pcm_playback_hook(hinfo, codec, substream,
4116 HDA_GEN_PCM_ACT_PREPARE);
4117 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004118}
Takashi Iwai97ec5582006-03-21 11:29:07 +01004119
Takashi Iwai352f7f92012-12-19 12:52:06 +01004120static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4121 struct hda_codec *codec,
4122 struct snd_pcm_substream *substream)
4123{
4124 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004125 int err;
4126
4127 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
4128 if (!err)
4129 call_pcm_playback_hook(hinfo, codec, substream,
4130 HDA_GEN_PCM_ACT_CLEANUP);
4131 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004132}
4133
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004134static int playback_pcm_close(struct hda_pcm_stream *hinfo,
4135 struct hda_codec *codec,
4136 struct snd_pcm_substream *substream)
4137{
4138 struct hda_gen_spec *spec = codec->spec;
4139 mutex_lock(&spec->pcm_mutex);
4140 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004141 call_pcm_playback_hook(hinfo, codec, substream,
4142 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004143 mutex_unlock(&spec->pcm_mutex);
4144 return 0;
4145}
4146
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004147static int capture_pcm_open(struct hda_pcm_stream *hinfo,
4148 struct hda_codec *codec,
4149 struct snd_pcm_substream *substream)
4150{
4151 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
4152 return 0;
4153}
4154
4155static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4156 struct hda_codec *codec,
4157 unsigned int stream_tag,
4158 unsigned int format,
4159 struct snd_pcm_substream *substream)
4160{
4161 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4162 call_pcm_capture_hook(hinfo, codec, substream,
4163 HDA_GEN_PCM_ACT_PREPARE);
4164 return 0;
4165}
4166
4167static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4168 struct hda_codec *codec,
4169 struct snd_pcm_substream *substream)
4170{
4171 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4172 call_pcm_capture_hook(hinfo, codec, substream,
4173 HDA_GEN_PCM_ACT_CLEANUP);
4174 return 0;
4175}
4176
4177static int capture_pcm_close(struct hda_pcm_stream *hinfo,
4178 struct hda_codec *codec,
4179 struct snd_pcm_substream *substream)
4180{
4181 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
4182 return 0;
4183}
4184
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004185static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
4186 struct hda_codec *codec,
4187 struct snd_pcm_substream *substream)
4188{
4189 struct hda_gen_spec *spec = codec->spec;
4190 int err = 0;
4191
4192 mutex_lock(&spec->pcm_mutex);
4193 if (!spec->indep_hp_enabled)
4194 err = -EBUSY;
4195 else
4196 spec->active_streams |= 1 << STREAM_INDEP_HP;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004197 call_pcm_playback_hook(hinfo, codec, substream,
4198 HDA_GEN_PCM_ACT_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004199 mutex_unlock(&spec->pcm_mutex);
4200 return err;
4201}
4202
4203static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
4204 struct hda_codec *codec,
4205 struct snd_pcm_substream *substream)
4206{
4207 struct hda_gen_spec *spec = codec->spec;
4208 mutex_lock(&spec->pcm_mutex);
4209 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004210 call_pcm_playback_hook(hinfo, codec, substream,
4211 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004212 mutex_unlock(&spec->pcm_mutex);
4213 return 0;
4214}
4215
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004216static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4217 struct hda_codec *codec,
4218 unsigned int stream_tag,
4219 unsigned int format,
4220 struct snd_pcm_substream *substream)
4221{
4222 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4223 call_pcm_playback_hook(hinfo, codec, substream,
4224 HDA_GEN_PCM_ACT_PREPARE);
4225 return 0;
4226}
4227
4228static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4229 struct hda_codec *codec,
4230 struct snd_pcm_substream *substream)
4231{
4232 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4233 call_pcm_playback_hook(hinfo, codec, substream,
4234 HDA_GEN_PCM_ACT_CLEANUP);
4235 return 0;
4236}
4237
Takashi Iwai352f7f92012-12-19 12:52:06 +01004238/*
4239 * Digital out
4240 */
4241static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
4242 struct hda_codec *codec,
4243 struct snd_pcm_substream *substream)
4244{
4245 struct hda_gen_spec *spec = codec->spec;
4246 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
4247}
4248
4249static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4250 struct hda_codec *codec,
4251 unsigned int stream_tag,
4252 unsigned int format,
4253 struct snd_pcm_substream *substream)
4254{
4255 struct hda_gen_spec *spec = codec->spec;
4256 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
4257 stream_tag, format, substream);
4258}
4259
4260static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4261 struct hda_codec *codec,
4262 struct snd_pcm_substream *substream)
4263{
4264 struct hda_gen_spec *spec = codec->spec;
4265 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
4266}
4267
4268static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
4269 struct hda_codec *codec,
4270 struct snd_pcm_substream *substream)
4271{
4272 struct hda_gen_spec *spec = codec->spec;
4273 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
4274}
4275
4276/*
4277 * Analog capture
4278 */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004279#define alt_capture_pcm_open capture_pcm_open
4280#define alt_capture_pcm_close capture_pcm_close
4281
Takashi Iwai352f7f92012-12-19 12:52:06 +01004282static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4283 struct hda_codec *codec,
4284 unsigned int stream_tag,
4285 unsigned int format,
4286 struct snd_pcm_substream *substream)
4287{
4288 struct hda_gen_spec *spec = codec->spec;
4289
4290 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01004291 stream_tag, 0, format);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004292 call_pcm_capture_hook(hinfo, codec, substream,
4293 HDA_GEN_PCM_ACT_PREPARE);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004294 return 0;
4295}
4296
Takashi Iwai352f7f92012-12-19 12:52:06 +01004297static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4298 struct hda_codec *codec,
4299 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01004300{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004301 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01004302
Takashi Iwai352f7f92012-12-19 12:52:06 +01004303 snd_hda_codec_cleanup_stream(codec,
4304 spec->adc_nids[substream->number + 1]);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004305 call_pcm_capture_hook(hinfo, codec, substream,
4306 HDA_GEN_PCM_ACT_CLEANUP);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004307 return 0;
4308}
4309
Takashi Iwai352f7f92012-12-19 12:52:06 +01004310/*
4311 */
4312static const struct hda_pcm_stream pcm_analog_playback = {
4313 .substreams = 1,
4314 .channels_min = 2,
4315 .channels_max = 8,
4316 /* NID is set in build_pcms */
4317 .ops = {
4318 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004319 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004320 .prepare = playback_pcm_prepare,
4321 .cleanup = playback_pcm_cleanup
4322 },
4323};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324
Takashi Iwai352f7f92012-12-19 12:52:06 +01004325static const struct hda_pcm_stream pcm_analog_capture = {
4326 .substreams = 1,
4327 .channels_min = 2,
4328 .channels_max = 2,
4329 /* NID is set in build_pcms */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004330 .ops = {
4331 .open = capture_pcm_open,
4332 .close = capture_pcm_close,
4333 .prepare = capture_pcm_prepare,
4334 .cleanup = capture_pcm_cleanup
4335 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004336};
4337
4338static const struct hda_pcm_stream pcm_analog_alt_playback = {
4339 .substreams = 1,
4340 .channels_min = 2,
4341 .channels_max = 2,
4342 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004343 .ops = {
4344 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004345 .close = alt_playback_pcm_close,
4346 .prepare = alt_playback_pcm_prepare,
4347 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004348 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004349};
4350
4351static const struct hda_pcm_stream pcm_analog_alt_capture = {
4352 .substreams = 2, /* can be overridden */
4353 .channels_min = 2,
4354 .channels_max = 2,
4355 /* NID is set in build_pcms */
4356 .ops = {
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004357 .open = alt_capture_pcm_open,
4358 .close = alt_capture_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004359 .prepare = alt_capture_pcm_prepare,
4360 .cleanup = alt_capture_pcm_cleanup
4361 },
4362};
4363
4364static const struct hda_pcm_stream pcm_digital_playback = {
4365 .substreams = 1,
4366 .channels_min = 2,
4367 .channels_max = 2,
4368 /* NID is set in build_pcms */
4369 .ops = {
4370 .open = dig_playback_pcm_open,
4371 .close = dig_playback_pcm_close,
4372 .prepare = dig_playback_pcm_prepare,
4373 .cleanup = dig_playback_pcm_cleanup
4374 },
4375};
4376
4377static const struct hda_pcm_stream pcm_digital_capture = {
4378 .substreams = 1,
4379 .channels_min = 2,
4380 .channels_max = 2,
4381 /* NID is set in build_pcms */
4382};
4383
4384/* Used by build_pcms to flag that a PCM has no playback stream */
4385static const struct hda_pcm_stream pcm_null_stream = {
4386 .substreams = 0,
4387 .channels_min = 0,
4388 .channels_max = 0,
4389};
4390
4391/*
4392 * dynamic changing ADC PCM streams
4393 */
4394static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
4395{
4396 struct hda_gen_spec *spec = codec->spec;
4397 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
4398
4399 if (spec->cur_adc && spec->cur_adc != new_adc) {
4400 /* stream is running, let's swap the current ADC */
4401 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
4402 spec->cur_adc = new_adc;
4403 snd_hda_codec_setup_stream(codec, new_adc,
4404 spec->cur_adc_stream_tag, 0,
4405 spec->cur_adc_format);
4406 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004408 return false;
4409}
4410
4411/* analog capture with dynamic dual-adc changes */
4412static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4413 struct hda_codec *codec,
4414 unsigned int stream_tag,
4415 unsigned int format,
4416 struct snd_pcm_substream *substream)
4417{
4418 struct hda_gen_spec *spec = codec->spec;
4419 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
4420 spec->cur_adc_stream_tag = stream_tag;
4421 spec->cur_adc_format = format;
4422 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
4423 return 0;
4424}
4425
4426static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4427 struct hda_codec *codec,
4428 struct snd_pcm_substream *substream)
4429{
4430 struct hda_gen_spec *spec = codec->spec;
4431 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
4432 spec->cur_adc = 0;
4433 return 0;
4434}
4435
4436static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
4437 .substreams = 1,
4438 .channels_min = 2,
4439 .channels_max = 2,
4440 .nid = 0, /* fill later */
4441 .ops = {
4442 .prepare = dyn_adc_capture_pcm_prepare,
4443 .cleanup = dyn_adc_capture_pcm_cleanup
4444 },
4445};
4446
Takashi Iwaif873e532012-12-20 16:58:39 +01004447static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
4448 const char *chip_name)
4449{
4450 char *p;
4451
4452 if (*str)
4453 return;
4454 strlcpy(str, chip_name, len);
4455
4456 /* drop non-alnum chars after a space */
4457 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
4458 if (!isalnum(p[1])) {
4459 *p = 0;
4460 break;
4461 }
4462 }
4463 strlcat(str, sfx, len);
4464}
4465
Takashi Iwai352f7f92012-12-19 12:52:06 +01004466/* build PCM streams based on the parsed results */
4467int snd_hda_gen_build_pcms(struct hda_codec *codec)
4468{
4469 struct hda_gen_spec *spec = codec->spec;
4470 struct hda_pcm *info = spec->pcm_rec;
4471 const struct hda_pcm_stream *p;
4472 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473
4474 codec->num_pcms = 1;
4475 codec->pcm_info = info;
4476
Takashi Iwai352f7f92012-12-19 12:52:06 +01004477 if (spec->no_analog)
4478 goto skip_analog;
4479
Takashi Iwaif873e532012-12-20 16:58:39 +01004480 fill_pcm_stream_name(spec->stream_name_analog,
4481 sizeof(spec->stream_name_analog),
4482 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004483 info->name = spec->stream_name_analog;
4484
4485 if (spec->multiout.num_dacs > 0) {
4486 p = spec->stream_analog_playback;
4487 if (!p)
4488 p = &pcm_analog_playback;
4489 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4490 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
4491 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
4492 spec->multiout.max_channels;
4493 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
4494 spec->autocfg.line_outs == 2)
4495 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
4496 snd_pcm_2_1_chmaps;
4497 }
4498 if (spec->num_adc_nids) {
4499 p = spec->stream_analog_capture;
4500 if (!p) {
4501 if (spec->dyn_adc_switch)
4502 p = &dyn_adc_pcm_analog_capture;
4503 else
4504 p = &pcm_analog_capture;
4505 }
4506 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4507 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
4508 }
4509
Takashi Iwai352f7f92012-12-19 12:52:06 +01004510 skip_analog:
4511 /* SPDIF for stream index #1 */
4512 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01004513 fill_pcm_stream_name(spec->stream_name_digital,
4514 sizeof(spec->stream_name_digital),
4515 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004516 codec->num_pcms = 2;
4517 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
4518 info = spec->pcm_rec + 1;
4519 info->name = spec->stream_name_digital;
4520 if (spec->dig_out_type)
4521 info->pcm_type = spec->dig_out_type;
4522 else
4523 info->pcm_type = HDA_PCM_TYPE_SPDIF;
4524 if (spec->multiout.dig_out_nid) {
4525 p = spec->stream_digital_playback;
4526 if (!p)
4527 p = &pcm_digital_playback;
4528 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4529 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
4530 }
4531 if (spec->dig_in_nid) {
4532 p = spec->stream_digital_capture;
4533 if (!p)
4534 p = &pcm_digital_capture;
4535 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4536 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
4537 }
4538 }
4539
4540 if (spec->no_analog)
4541 return 0;
4542
4543 /* If the use of more than one ADC is requested for the current
4544 * model, configure a second analog capture-only PCM.
4545 */
4546 have_multi_adcs = (spec->num_adc_nids > 1) &&
4547 !spec->dyn_adc_switch && !spec->auto_mic;
4548 /* Additional Analaog capture for index #2 */
4549 if (spec->alt_dac_nid || have_multi_adcs) {
Takashi Iwaia6071482013-01-21 16:50:09 +01004550 fill_pcm_stream_name(spec->stream_name_alt_analog,
4551 sizeof(spec->stream_name_alt_analog),
4552 " Alt Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004553 codec->num_pcms = 3;
4554 info = spec->pcm_rec + 2;
Takashi Iwaia6071482013-01-21 16:50:09 +01004555 info->name = spec->stream_name_alt_analog;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004556 if (spec->alt_dac_nid) {
4557 p = spec->stream_analog_alt_playback;
4558 if (!p)
4559 p = &pcm_analog_alt_playback;
4560 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4561 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
4562 spec->alt_dac_nid;
4563 } else {
4564 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
4565 pcm_null_stream;
4566 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
4567 }
4568 if (have_multi_adcs) {
4569 p = spec->stream_analog_alt_capture;
4570 if (!p)
4571 p = &pcm_analog_alt_capture;
4572 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4573 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
4574 spec->adc_nids[1];
4575 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
4576 spec->num_adc_nids - 1;
4577 } else {
4578 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
4579 pcm_null_stream;
4580 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
4581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 }
4583
4584 return 0;
4585}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004586EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
4587
4588
4589/*
4590 * Standard auto-parser initializations
4591 */
4592
Takashi Iwaid4156932013-01-07 10:08:02 +01004593/* configure the given path as a proper output */
Takashi Iwai2c12c302013-01-10 09:33:29 +01004594static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004595{
4596 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01004597 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004598
Takashi Iwai196c17662013-01-04 15:01:40 +01004599 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01004600 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004601 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01004602 pin = path->path[path->depth - 1];
Takashi Iwai2c12c302013-01-10 09:33:29 +01004603 restore_pin_ctl(codec, pin);
Takashi Iwaie1284af2013-01-03 16:33:02 +01004604 snd_hda_activate_path(codec, path, path->active, true);
4605 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004606}
4607
4608/* initialize primary output paths */
4609static void init_multi_out(struct hda_codec *codec)
4610{
4611 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004612 int i;
4613
Takashi Iwaid4156932013-01-07 10:08:02 +01004614 for (i = 0; i < spec->autocfg.line_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004615 set_output_and_unmute(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004616}
4617
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004618
Takashi Iwai2c12c302013-01-10 09:33:29 +01004619static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004620{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004621 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004622
Takashi Iwaid4156932013-01-07 10:08:02 +01004623 for (i = 0; i < num_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004624 set_output_and_unmute(codec, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004625}
4626
4627/* initialize hp and speaker paths */
4628static void init_extra_out(struct hda_codec *codec)
4629{
4630 struct hda_gen_spec *spec = codec->spec;
4631
4632 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004633 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004634 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
4635 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai2c12c302013-01-10 09:33:29 +01004636 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004637}
4638
4639/* initialize multi-io paths */
4640static void init_multi_io(struct hda_codec *codec)
4641{
4642 struct hda_gen_spec *spec = codec->spec;
4643 int i;
4644
4645 for (i = 0; i < spec->multi_ios; i++) {
4646 hda_nid_t pin = spec->multi_io[i].pin;
4647 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01004648 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004649 if (!path)
4650 continue;
4651 if (!spec->multi_io[i].ctl_in)
4652 spec->multi_io[i].ctl_in =
Takashi Iwai2c12c302013-01-10 09:33:29 +01004653 snd_hda_codec_get_pin_target(codec, pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004654 snd_hda_activate_path(codec, path, path->active, true);
4655 }
4656}
4657
Takashi Iwai352f7f92012-12-19 12:52:06 +01004658/* set up input pins and loopback paths */
4659static void init_analog_input(struct hda_codec *codec)
4660{
4661 struct hda_gen_spec *spec = codec->spec;
4662 struct auto_pin_cfg *cfg = &spec->autocfg;
4663 int i;
4664
4665 for (i = 0; i < cfg->num_inputs; i++) {
4666 hda_nid_t nid = cfg->inputs[i].pin;
4667 if (is_input_pin(codec, nid))
Takashi Iwai2c12c302013-01-10 09:33:29 +01004668 restore_pin_ctl(codec, nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004669
4670 /* init loopback inputs */
4671 if (spec->mixer_nid) {
4672 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01004673 path = snd_hda_get_path_from_idx(codec, spec->loopback_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004674 if (path)
4675 snd_hda_activate_path(codec, path,
4676 path->active, false);
4677 }
4678 }
4679}
4680
4681/* initialize ADC paths */
4682static void init_input_src(struct hda_codec *codec)
4683{
4684 struct hda_gen_spec *spec = codec->spec;
4685 struct hda_input_mux *imux = &spec->input_mux;
4686 struct nid_path *path;
4687 int i, c, nums;
4688
4689 if (spec->dyn_adc_switch)
4690 nums = 1;
4691 else
4692 nums = spec->num_adc_nids;
4693
4694 for (c = 0; c < nums; c++) {
4695 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01004696 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004697 if (path) {
4698 bool active = path->active;
4699 if (i == spec->cur_mux[c])
4700 active = true;
4701 snd_hda_activate_path(codec, path, active, false);
4702 }
4703 }
4704 }
4705
4706 if (spec->shared_mic_hp)
4707 update_shared_mic_hp(codec, spec->cur_mux[0]);
4708
4709 if (spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01004710 spec->cap_sync_hook(codec, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004711}
4712
4713/* set right pin controls for digital I/O */
4714static void init_digital(struct hda_codec *codec)
4715{
4716 struct hda_gen_spec *spec = codec->spec;
4717 int i;
4718 hda_nid_t pin;
4719
Takashi Iwaid4156932013-01-07 10:08:02 +01004720 for (i = 0; i < spec->autocfg.dig_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004721 set_output_and_unmute(codec, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004722 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01004723 if (pin) {
4724 struct nid_path *path;
Takashi Iwai2c12c302013-01-10 09:33:29 +01004725 restore_pin_ctl(codec, pin);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01004726 path = snd_hda_get_path_from_idx(codec, spec->digin_path);
4727 if (path)
4728 snd_hda_activate_path(codec, path, path->active, false);
4729 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004730}
4731
Takashi Iwai973e4972012-12-20 15:16:09 +01004732/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
4733 * invalid unsol tags by some reason
4734 */
4735static void clear_unsol_on_unused_pins(struct hda_codec *codec)
4736{
4737 int i;
4738
4739 for (i = 0; i < codec->init_pins.used; i++) {
4740 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
4741 hda_nid_t nid = pin->nid;
4742 if (is_jack_detectable(codec, nid) &&
4743 !snd_hda_jack_tbl_get(codec, nid))
4744 snd_hda_codec_update_cache(codec, nid, 0,
4745 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
4746 }
4747}
4748
Takashi Iwai5187ac12013-01-07 12:52:16 +01004749/*
4750 * initialize the generic spec;
4751 * this can be put as patch_ops.init function
4752 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004753int snd_hda_gen_init(struct hda_codec *codec)
4754{
4755 struct hda_gen_spec *spec = codec->spec;
4756
4757 if (spec->init_hook)
4758 spec->init_hook(codec);
4759
4760 snd_hda_apply_verbs(codec);
4761
Takashi Iwai3bbcd272012-12-20 11:50:58 +01004762 codec->cached_write = 1;
4763
Takashi Iwai352f7f92012-12-19 12:52:06 +01004764 init_multi_out(codec);
4765 init_extra_out(codec);
4766 init_multi_io(codec);
4767 init_analog_input(codec);
4768 init_input_src(codec);
4769 init_digital(codec);
4770
Takashi Iwai973e4972012-12-20 15:16:09 +01004771 clear_unsol_on_unused_pins(codec);
4772
Takashi Iwai352f7f92012-12-19 12:52:06 +01004773 /* call init functions of standard auto-mute helpers */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01004774 update_automute_all(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004775
Takashi Iwai3bbcd272012-12-20 11:50:58 +01004776 snd_hda_codec_flush_amp_cache(codec);
4777 snd_hda_codec_flush_cmd_cache(codec);
4778
Takashi Iwai352f7f92012-12-19 12:52:06 +01004779 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
4780 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
4781
4782 hda_call_check_power_status(codec, 0x01);
4783 return 0;
4784}
Takashi Iwaifce52a32013-01-07 12:42:48 +01004785EXPORT_SYMBOL_HDA(snd_hda_gen_init);
4786
Takashi Iwai5187ac12013-01-07 12:52:16 +01004787/*
4788 * free the generic spec;
4789 * this can be put as patch_ops.free function
4790 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01004791void snd_hda_gen_free(struct hda_codec *codec)
4792{
4793 snd_hda_gen_spec_free(codec->spec);
4794 kfree(codec->spec);
4795 codec->spec = NULL;
4796}
4797EXPORT_SYMBOL_HDA(snd_hda_gen_free);
4798
4799#ifdef CONFIG_PM
Takashi Iwai5187ac12013-01-07 12:52:16 +01004800/*
4801 * check the loopback power save state;
4802 * this can be put as patch_ops.check_power_status function
4803 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01004804int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
4805{
4806 struct hda_gen_spec *spec = codec->spec;
4807 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
4808}
4809EXPORT_SYMBOL_HDA(snd_hda_gen_check_power_status);
4810#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01004811
4812
4813/*
4814 * the generic codec support
4815 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816
Takashi Iwai352f7f92012-12-19 12:52:06 +01004817static const struct hda_codec_ops generic_patch_ops = {
4818 .build_controls = snd_hda_gen_build_controls,
4819 .build_pcms = snd_hda_gen_build_pcms,
4820 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01004821 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004822 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02004823#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01004824 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02004825#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826};
4827
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828int snd_hda_parse_generic_codec(struct hda_codec *codec)
4829{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004830 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 int err;
4832
Takashi Iwaie560d8d2005-09-09 14:21:46 +02004833 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004834 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004836 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004839 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
4840 if (err < 0)
4841 return err;
4842
4843 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004844 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845 goto error;
4846
4847 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848 return 0;
4849
Takashi Iwai352f7f92012-12-19 12:52:06 +01004850error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01004851 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852 return err;
4853}
Takashi Iwaifce52a32013-01-07 12:42:48 +01004854EXPORT_SYMBOL_HDA(snd_hda_parse_generic_codec);